erst-dbg.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include <linux/uaccess.h>
  24. #include <acpi/apei.h>
  25. #include <linux/miscdevice.h>
  26. #include "apei-internal.h"
  27. #define ERST_DBG_PFX "ERST DBG: "
  28. #define ERST_DBG_RECORD_LEN_MAX 0x4000
  29. static void *erst_dbg_buf;
  30. static unsigned int erst_dbg_buf_len;
  31. /* Prevent erst_dbg_read/write from being invoked concurrently */
  32. static DEFINE_MUTEX(erst_dbg_mutex);
  33. static int erst_dbg_open(struct inode *inode, struct file *file)
  34. {
  35. int rc, *pos;
  36. if (erst_disable)
  37. return -ENODEV;
  38. pos = (int *)&file->private_data;
  39. rc = erst_get_record_id_begin(pos);
  40. if (rc)
  41. return rc;
  42. return nonseekable_open(inode, file);
  43. }
  44. static int erst_dbg_release(struct inode *inode, struct file *file)
  45. {
  46. erst_get_record_id_end();
  47. return 0;
  48. }
  49. static long erst_dbg_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
  50. {
  51. int rc;
  52. u64 record_id;
  53. u32 record_count;
  54. switch (cmd) {
  55. case APEI_ERST_CLEAR_RECORD:
  56. rc = copy_from_user(&record_id, (void __user *)arg,
  57. sizeof(record_id));
  58. if (rc)
  59. return -EFAULT;
  60. return erst_clear(record_id);
  61. case APEI_ERST_GET_RECORD_COUNT:
  62. rc = erst_get_record_count();
  63. if (rc < 0)
  64. return rc;
  65. record_count = rc;
  66. rc = put_user(record_count, (u32 __user *)arg);
  67. if (rc)
  68. return rc;
  69. return 0;
  70. default:
  71. return -ENOTTY;
  72. }
  73. }
  74. static ssize_t erst_dbg_read(struct file *filp, char __user *ubuf,
  75. size_t usize, loff_t *off)
  76. {
  77. int rc, *pos;
  78. ssize_t len = 0;
  79. u64 id;
  80. if (*off)
  81. return -EINVAL;
  82. if (mutex_lock_interruptible(&erst_dbg_mutex) != 0)
  83. return -EINTR;
  84. pos = (int *)&filp->private_data;
  85. retry_next:
  86. rc = erst_get_record_id_next(pos, &id);
  87. if (rc)
  88. goto out;
  89. /* no more record */
  90. if (id == APEI_ERST_INVALID_RECORD_ID) {
  91. /*
  92. * If the persistent store is empty initially, the function
  93. * 'erst_read' below will return "-ENOENT" value. This causes
  94. * 'retry_next' label is entered again. The returned value
  95. * should be zero indicating the read operation is EOF.
  96. */
  97. len = 0;
  98. goto out;
  99. }
  100. retry:
  101. rc = len = erst_read(id, erst_dbg_buf, erst_dbg_buf_len);
  102. /* The record may be cleared by others, try read next record */
  103. if (rc == -ENOENT)
  104. goto retry_next;
  105. if (rc < 0)
  106. goto out;
  107. if (len > ERST_DBG_RECORD_LEN_MAX) {
  108. pr_warning(ERST_DBG_PFX
  109. "Record (ID: 0x%llx) length is too long: %zd\n",
  110. id, len);
  111. rc = -EIO;
  112. goto out;
  113. }
  114. if (len > erst_dbg_buf_len) {
  115. void *p;
  116. rc = -ENOMEM;
  117. p = kmalloc(len, GFP_KERNEL);
  118. if (!p)
  119. goto out;
  120. kfree(erst_dbg_buf);
  121. erst_dbg_buf = p;
  122. erst_dbg_buf_len = len;
  123. goto retry;
  124. }
  125. rc = -EINVAL;
  126. if (len > usize)
  127. goto out;
  128. rc = -EFAULT;
  129. if (copy_to_user(ubuf, erst_dbg_buf, len))
  130. goto out;
  131. rc = 0;
  132. out:
  133. mutex_unlock(&erst_dbg_mutex);
  134. return rc ? rc : len;
  135. }
  136. static ssize_t erst_dbg_write(struct file *filp, const char __user *ubuf,
  137. size_t usize, loff_t *off)
  138. {
  139. int rc;
  140. struct cper_record_header *rcd;
  141. if (!capable(CAP_SYS_ADMIN))
  142. return -EPERM;
  143. if (usize > ERST_DBG_RECORD_LEN_MAX) {
  144. pr_err(ERST_DBG_PFX "Too long record to be written\n");
  145. return -EINVAL;
  146. }
  147. if (mutex_lock_interruptible(&erst_dbg_mutex))
  148. return -EINTR;
  149. if (usize > erst_dbg_buf_len) {
  150. void *p;
  151. rc = -ENOMEM;
  152. p = kmalloc(usize, GFP_KERNEL);
  153. if (!p)
  154. goto out;
  155. kfree(erst_dbg_buf);
  156. erst_dbg_buf = p;
  157. erst_dbg_buf_len = usize;
  158. }
  159. rc = copy_from_user(erst_dbg_buf, ubuf, usize);
  160. if (rc) {
  161. rc = -EFAULT;
  162. goto out;
  163. }
  164. rcd = erst_dbg_buf;
  165. rc = -EINVAL;
  166. if (rcd->record_length != usize)
  167. goto out;
  168. rc = erst_write(erst_dbg_buf);
  169. out:
  170. mutex_unlock(&erst_dbg_mutex);
  171. return rc < 0 ? rc : usize;
  172. }
  173. static const struct file_operations erst_dbg_ops = {
  174. .owner = THIS_MODULE,
  175. .open = erst_dbg_open,
  176. .release = erst_dbg_release,
  177. .read = erst_dbg_read,
  178. .write = erst_dbg_write,
  179. .unlocked_ioctl = erst_dbg_ioctl,
  180. .llseek = no_llseek,
  181. };
  182. static struct miscdevice erst_dbg_dev = {
  183. .minor = MISC_DYNAMIC_MINOR,
  184. .name = "erst_dbg",
  185. .fops = &erst_dbg_ops,
  186. };
  187. static __init int erst_dbg_init(void)
  188. {
  189. if (erst_disable) {
  190. pr_info(ERST_DBG_PFX "ERST support is disabled.\n");
  191. return -ENODEV;
  192. }
  193. return misc_register(&erst_dbg_dev);
  194. }
  195. static __exit void erst_dbg_exit(void)
  196. {
  197. misc_deregister(&erst_dbg_dev);
  198. kfree(erst_dbg_buf);
  199. }
  200. module_init(erst_dbg_init);
  201. module_exit(erst_dbg_exit);
  202. MODULE_AUTHOR("Huang Ying");
  203. MODULE_DESCRIPTION("APEI Error Record Serialization Table debug support");
  204. MODULE_LICENSE("GPL");