erst-dbg.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. /*
  96. * If the persistent store is empty initially, the function
  97. * 'erst_read' below will return "-ENOENT" value. This causes
  98. * 'retry_next' label is entered again. The returned value
  99. * should be zero indicating the read operation is EOF.
  100. */
  101. len = 0;
  102. goto out;
  103. }
  104. retry:
  105. rc = len = erst_read(id, erst_dbg_buf, erst_dbg_buf_len);
  106. /* The record may be cleared by others, try read next record */
  107. if (rc == -ENOENT)
  108. goto retry_next;
  109. if (rc < 0)
  110. goto out;
  111. if (len > ERST_DBG_RECORD_LEN_MAX) {
  112. pr_warning(ERST_DBG_PFX
  113. "Record (ID: 0x%llx) length is too long: %zd\n",
  114. id, len);
  115. rc = -EIO;
  116. goto out;
  117. }
  118. if (len > erst_dbg_buf_len) {
  119. void *p;
  120. rc = -ENOMEM;
  121. p = kmalloc(len, GFP_KERNEL);
  122. if (!p)
  123. goto out;
  124. kfree(erst_dbg_buf);
  125. erst_dbg_buf = p;
  126. erst_dbg_buf_len = len;
  127. goto retry;
  128. }
  129. rc = -EINVAL;
  130. if (len > usize)
  131. goto out;
  132. rc = -EFAULT;
  133. if (copy_to_user(ubuf, erst_dbg_buf, len))
  134. goto out;
  135. rc = 0;
  136. out:
  137. mutex_unlock(&erst_dbg_mutex);
  138. return rc ? rc : len;
  139. }
  140. static ssize_t erst_dbg_write(struct file *filp, const char __user *ubuf,
  141. size_t usize, loff_t *off)
  142. {
  143. int rc;
  144. struct cper_record_header *rcd;
  145. if (!capable(CAP_SYS_ADMIN))
  146. return -EPERM;
  147. if (usize > ERST_DBG_RECORD_LEN_MAX) {
  148. pr_err(ERST_DBG_PFX "Too long record to be written\n");
  149. return -EINVAL;
  150. }
  151. if (mutex_lock_interruptible(&erst_dbg_mutex))
  152. return -EINTR;
  153. if (usize > erst_dbg_buf_len) {
  154. void *p;
  155. rc = -ENOMEM;
  156. p = kmalloc(usize, GFP_KERNEL);
  157. if (!p)
  158. goto out;
  159. kfree(erst_dbg_buf);
  160. erst_dbg_buf = p;
  161. erst_dbg_buf_len = usize;
  162. }
  163. rc = copy_from_user(erst_dbg_buf, ubuf, usize);
  164. if (rc) {
  165. rc = -EFAULT;
  166. goto out;
  167. }
  168. rcd = erst_dbg_buf;
  169. rc = -EINVAL;
  170. if (rcd->record_length != usize)
  171. goto out;
  172. rc = erst_write(erst_dbg_buf);
  173. out:
  174. mutex_unlock(&erst_dbg_mutex);
  175. return rc < 0 ? rc : usize;
  176. }
  177. static const struct file_operations erst_dbg_ops = {
  178. .owner = THIS_MODULE,
  179. .open = erst_dbg_open,
  180. .release = erst_dbg_release,
  181. .read = erst_dbg_read,
  182. .write = erst_dbg_write,
  183. .unlocked_ioctl = erst_dbg_ioctl,
  184. .llseek = no_llseek,
  185. };
  186. static struct miscdevice erst_dbg_dev = {
  187. .minor = MISC_DYNAMIC_MINOR,
  188. .name = "erst_dbg",
  189. .fops = &erst_dbg_ops,
  190. };
  191. static __init int erst_dbg_init(void)
  192. {
  193. if (erst_disable) {
  194. pr_info(ERST_DBG_PFX "ERST support is disabled.\n");
  195. return -ENODEV;
  196. }
  197. return misc_register(&erst_dbg_dev);
  198. }
  199. static __exit void erst_dbg_exit(void)
  200. {
  201. misc_deregister(&erst_dbg_dev);
  202. kfree(erst_dbg_buf);
  203. }
  204. module_init(erst_dbg_init);
  205. module_exit(erst_dbg_exit);
  206. MODULE_AUTHOR("Huang Ying");
  207. MODULE_DESCRIPTION("APEI Error Record Serialization Table debug support");
  208. MODULE_LICENSE("GPL");