erst-dbg.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. * APEI Error Record Serialization Table debug support
  3. *
  4. * ERST is a way provided by APEI to save and retrieve hardware error
  5. * information to and from a persistent store. This file provide the
  6. * debugging/testing support for ERST kernel support and firmware
  7. * implementation.
  8. *
  9. * Copyright 2010 Intel Corp.
  10. * Author: Huang Ying <ying.huang@intel.com>
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License version
  14. * 2 as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  24. */
  25. #include <linux/kernel.h>
  26. #include <linux/module.h>
  27. #include <linux/uaccess.h>
  28. #include <acpi/apei.h>
  29. #include <linux/miscdevice.h>
  30. #include "apei-internal.h"
  31. #define ERST_DBG_PFX "ERST DBG: "
  32. #define ERST_DBG_RECORD_LEN_MAX 0x4000
  33. static void *erst_dbg_buf;
  34. static unsigned int erst_dbg_buf_len;
  35. /* Prevent erst_dbg_read/write from being invoked concurrently */
  36. static DEFINE_MUTEX(erst_dbg_mutex);
  37. static int erst_dbg_open(struct inode *inode, struct file *file)
  38. {
  39. int rc, *pos;
  40. if (erst_disable)
  41. return -ENODEV;
  42. pos = (int *)&file->private_data;
  43. rc = erst_get_record_id_begin(pos);
  44. if (rc)
  45. return rc;
  46. return nonseekable_open(inode, file);
  47. }
  48. static int erst_dbg_release(struct inode *inode, struct file *file)
  49. {
  50. erst_get_record_id_end();
  51. return 0;
  52. }
  53. static long erst_dbg_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
  54. {
  55. int rc;
  56. u64 record_id;
  57. u32 record_count;
  58. switch (cmd) {
  59. case APEI_ERST_CLEAR_RECORD:
  60. rc = copy_from_user(&record_id, (void __user *)arg,
  61. sizeof(record_id));
  62. if (rc)
  63. return -EFAULT;
  64. return erst_clear(record_id);
  65. case APEI_ERST_GET_RECORD_COUNT:
  66. rc = erst_get_record_count();
  67. if (rc < 0)
  68. return rc;
  69. record_count = rc;
  70. rc = put_user(record_count, (u32 __user *)arg);
  71. if (rc)
  72. return rc;
  73. return 0;
  74. default:
  75. return -ENOTTY;
  76. }
  77. }
  78. static ssize_t erst_dbg_read(struct file *filp, char __user *ubuf,
  79. size_t usize, loff_t *off)
  80. {
  81. int rc, *pos;
  82. ssize_t len = 0;
  83. u64 id;
  84. if (*off)
  85. return -EINVAL;
  86. if (mutex_lock_interruptible(&erst_dbg_mutex) != 0)
  87. return -EINTR;
  88. pos = (int *)&filp->private_data;
  89. retry_next:
  90. rc = erst_get_record_id_next(pos, &id);
  91. if (rc)
  92. goto out;
  93. /* no more record */
  94. if (id == APEI_ERST_INVALID_RECORD_ID)
  95. goto out;
  96. retry:
  97. rc = len = erst_read(id, erst_dbg_buf, erst_dbg_buf_len);
  98. /* The record may be cleared by others, try read next record */
  99. if (rc == -ENOENT)
  100. goto retry_next;
  101. if (rc < 0)
  102. goto out;
  103. if (len > ERST_DBG_RECORD_LEN_MAX) {
  104. pr_warning(ERST_DBG_PFX
  105. "Record (ID: 0x%llx) length is too long: %zd\n",
  106. id, len);
  107. rc = -EIO;
  108. goto out;
  109. }
  110. if (len > erst_dbg_buf_len) {
  111. void *p;
  112. rc = -ENOMEM;
  113. p = kmalloc(len, GFP_KERNEL);
  114. if (!p)
  115. goto out;
  116. kfree(erst_dbg_buf);
  117. erst_dbg_buf = p;
  118. erst_dbg_buf_len = len;
  119. goto retry;
  120. }
  121. rc = -EINVAL;
  122. if (len > usize)
  123. goto out;
  124. rc = -EFAULT;
  125. if (copy_to_user(ubuf, erst_dbg_buf, len))
  126. goto out;
  127. rc = 0;
  128. out:
  129. mutex_unlock(&erst_dbg_mutex);
  130. return rc ? rc : len;
  131. }
  132. static ssize_t erst_dbg_write(struct file *filp, const char __user *ubuf,
  133. size_t usize, loff_t *off)
  134. {
  135. int rc;
  136. struct cper_record_header *rcd;
  137. if (!capable(CAP_SYS_ADMIN))
  138. return -EPERM;
  139. if (usize > ERST_DBG_RECORD_LEN_MAX) {
  140. pr_err(ERST_DBG_PFX "Too long record to be written\n");
  141. return -EINVAL;
  142. }
  143. if (mutex_lock_interruptible(&erst_dbg_mutex))
  144. return -EINTR;
  145. if (usize > erst_dbg_buf_len) {
  146. void *p;
  147. rc = -ENOMEM;
  148. p = kmalloc(usize, GFP_KERNEL);
  149. if (!p)
  150. goto out;
  151. kfree(erst_dbg_buf);
  152. erst_dbg_buf = p;
  153. erst_dbg_buf_len = usize;
  154. }
  155. rc = copy_from_user(erst_dbg_buf, ubuf, usize);
  156. if (rc) {
  157. rc = -EFAULT;
  158. goto out;
  159. }
  160. rcd = erst_dbg_buf;
  161. rc = -EINVAL;
  162. if (rcd->record_length != usize)
  163. goto out;
  164. rc = erst_write(erst_dbg_buf);
  165. out:
  166. mutex_unlock(&erst_dbg_mutex);
  167. return rc < 0 ? rc : usize;
  168. }
  169. static const struct file_operations erst_dbg_ops = {
  170. .owner = THIS_MODULE,
  171. .open = erst_dbg_open,
  172. .release = erst_dbg_release,
  173. .read = erst_dbg_read,
  174. .write = erst_dbg_write,
  175. .unlocked_ioctl = erst_dbg_ioctl,
  176. .llseek = no_llseek,
  177. };
  178. static struct miscdevice erst_dbg_dev = {
  179. .minor = MISC_DYNAMIC_MINOR,
  180. .name = "erst_dbg",
  181. .fops = &erst_dbg_ops,
  182. };
  183. static __init int erst_dbg_init(void)
  184. {
  185. if (erst_disable) {
  186. pr_info(ERST_DBG_PFX "ERST support is disabled.\n");
  187. return -ENODEV;
  188. }
  189. return misc_register(&erst_dbg_dev);
  190. }
  191. static __exit void erst_dbg_exit(void)
  192. {
  193. misc_deregister(&erst_dbg_dev);
  194. kfree(erst_dbg_buf);
  195. }
  196. module_init(erst_dbg_init);
  197. module_exit(erst_dbg_exit);
  198. MODULE_AUTHOR("Huang Ying");
  199. MODULE_DESCRIPTION("APEI Error Record Serialization Table debug support");
  200. MODULE_LICENSE("GPL");