erst-dbg.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 4096
  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. if (erst_disable)
  40. return -ENODEV;
  41. return nonseekable_open(inode, file);
  42. }
  43. static long erst_dbg_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
  44. {
  45. int rc;
  46. u64 record_id;
  47. u32 record_count;
  48. switch (cmd) {
  49. case APEI_ERST_CLEAR_RECORD:
  50. rc = copy_from_user(&record_id, (void __user *)arg,
  51. sizeof(record_id));
  52. if (rc)
  53. return -EFAULT;
  54. return erst_clear(record_id);
  55. case APEI_ERST_GET_RECORD_COUNT:
  56. rc = erst_get_record_count();
  57. if (rc < 0)
  58. return rc;
  59. record_count = rc;
  60. rc = put_user(record_count, (u32 __user *)arg);
  61. if (rc)
  62. return rc;
  63. return 0;
  64. default:
  65. return -ENOTTY;
  66. }
  67. }
  68. static ssize_t erst_dbg_read(struct file *filp, char __user *ubuf,
  69. size_t usize, loff_t *off)
  70. {
  71. int rc;
  72. ssize_t len = 0;
  73. u64 id;
  74. if (*off != 0)
  75. return -EINVAL;
  76. if (mutex_lock_interruptible(&erst_dbg_mutex) != 0)
  77. return -EINTR;
  78. retry_next:
  79. rc = erst_get_next_record_id(&id);
  80. if (rc)
  81. goto out;
  82. /* no more record */
  83. if (id == APEI_ERST_INVALID_RECORD_ID)
  84. goto out;
  85. retry:
  86. rc = len = erst_read(id, erst_dbg_buf, erst_dbg_buf_len);
  87. /* The record may be cleared by others, try read next record */
  88. if (rc == -ENOENT)
  89. goto retry_next;
  90. if (rc < 0)
  91. goto out;
  92. if (len > ERST_DBG_RECORD_LEN_MAX) {
  93. pr_warning(ERST_DBG_PFX
  94. "Record (ID: 0x%llx) length is too long: %zd\n",
  95. id, len);
  96. rc = -EIO;
  97. goto out;
  98. }
  99. if (len > erst_dbg_buf_len) {
  100. void *p;
  101. rc = -ENOMEM;
  102. p = kmalloc(len, GFP_KERNEL);
  103. if (!p)
  104. goto out;
  105. kfree(erst_dbg_buf);
  106. erst_dbg_buf = p;
  107. erst_dbg_buf_len = len;
  108. goto retry;
  109. }
  110. rc = -EINVAL;
  111. if (len > usize)
  112. goto out;
  113. rc = -EFAULT;
  114. if (copy_to_user(ubuf, erst_dbg_buf, len))
  115. goto out;
  116. rc = 0;
  117. out:
  118. mutex_unlock(&erst_dbg_mutex);
  119. return rc ? rc : len;
  120. }
  121. static ssize_t erst_dbg_write(struct file *filp, const char __user *ubuf,
  122. size_t usize, loff_t *off)
  123. {
  124. int rc;
  125. struct cper_record_header *rcd;
  126. if (!capable(CAP_SYS_ADMIN))
  127. return -EPERM;
  128. if (usize > ERST_DBG_RECORD_LEN_MAX) {
  129. pr_err(ERST_DBG_PFX "Too long record to be written\n");
  130. return -EINVAL;
  131. }
  132. if (mutex_lock_interruptible(&erst_dbg_mutex))
  133. return -EINTR;
  134. if (usize > erst_dbg_buf_len) {
  135. void *p;
  136. rc = -ENOMEM;
  137. p = kmalloc(usize, GFP_KERNEL);
  138. if (!p)
  139. goto out;
  140. kfree(erst_dbg_buf);
  141. erst_dbg_buf = p;
  142. erst_dbg_buf_len = usize;
  143. }
  144. rc = copy_from_user(erst_dbg_buf, ubuf, usize);
  145. if (rc) {
  146. rc = -EFAULT;
  147. goto out;
  148. }
  149. rcd = erst_dbg_buf;
  150. rc = -EINVAL;
  151. if (rcd->record_length != usize)
  152. goto out;
  153. rc = erst_write(erst_dbg_buf);
  154. out:
  155. mutex_unlock(&erst_dbg_mutex);
  156. return rc < 0 ? rc : usize;
  157. }
  158. static const struct file_operations erst_dbg_ops = {
  159. .owner = THIS_MODULE,
  160. .open = erst_dbg_open,
  161. .read = erst_dbg_read,
  162. .write = erst_dbg_write,
  163. .unlocked_ioctl = erst_dbg_ioctl,
  164. .llseek = no_llseek,
  165. };
  166. static struct miscdevice erst_dbg_dev = {
  167. .minor = MISC_DYNAMIC_MINOR,
  168. .name = "erst_dbg",
  169. .fops = &erst_dbg_ops,
  170. };
  171. static __init int erst_dbg_init(void)
  172. {
  173. return misc_register(&erst_dbg_dev);
  174. }
  175. static __exit void erst_dbg_exit(void)
  176. {
  177. misc_deregister(&erst_dbg_dev);
  178. kfree(erst_dbg_buf);
  179. }
  180. module_init(erst_dbg_init);
  181. module_exit(erst_dbg_exit);
  182. MODULE_AUTHOR("Huang Ying");
  183. MODULE_DESCRIPTION("APEI Error Record Serialization Table debug support");
  184. MODULE_LICENSE("GPL");