err_inject.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /*
  2. * err_inject.c -
  3. * 1.) Inject errors to a processor.
  4. * 2.) Query error injection capabilities.
  5. * This driver along with user space code can be acting as an error
  6. * injection tool.
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  16. * NON INFRINGEMENT. See the GNU General Public License for more
  17. * details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  22. *
  23. * Written by: Fenghua Yu <fenghua.yu@intel.com>, Intel Corporation
  24. * Copyright (C) 2006, Intel Corp. All rights reserved.
  25. *
  26. */
  27. #include <linux/device.h>
  28. #include <linux/init.h>
  29. #include <linux/mm.h>
  30. #include <linux/cpu.h>
  31. #include <linux/module.h>
  32. #define ERR_INJ_DEBUG
  33. #define ERR_DATA_BUFFER_SIZE 3 // Three 8-byte;
  34. #define define_one_ro(name) \
  35. static DEVICE_ATTR(name, 0444, show_##name, NULL)
  36. #define define_one_rw(name) \
  37. static DEVICE_ATTR(name, 0644, show_##name, store_##name)
  38. static u64 call_start[NR_CPUS];
  39. static u64 phys_addr[NR_CPUS];
  40. static u64 err_type_info[NR_CPUS];
  41. static u64 err_struct_info[NR_CPUS];
  42. static struct {
  43. u64 data1;
  44. u64 data2;
  45. u64 data3;
  46. } __attribute__((__aligned__(16))) err_data_buffer[NR_CPUS];
  47. static s64 status[NR_CPUS];
  48. static u64 capabilities[NR_CPUS];
  49. static u64 resources[NR_CPUS];
  50. #define show(name) \
  51. static ssize_t \
  52. show_##name(struct device *dev, struct device_attribute *attr, \
  53. char *buf) \
  54. { \
  55. u32 cpu=dev->id; \
  56. return sprintf(buf, "%lx\n", name[cpu]); \
  57. }
  58. #define store(name) \
  59. static ssize_t \
  60. store_##name(struct device *dev, struct device_attribute *attr, \
  61. const char *buf, size_t size) \
  62. { \
  63. unsigned int cpu=dev->id; \
  64. name[cpu] = simple_strtoull(buf, NULL, 16); \
  65. return size; \
  66. }
  67. show(call_start)
  68. /* It's user's responsibility to call the PAL procedure on a specific
  69. * processor. The cpu number in driver is only used for storing data.
  70. */
  71. static ssize_t
  72. store_call_start(struct device *dev, struct device_attribute *attr,
  73. const char *buf, size_t size)
  74. {
  75. unsigned int cpu=dev->id;
  76. unsigned long call_start = simple_strtoull(buf, NULL, 16);
  77. #ifdef ERR_INJ_DEBUG
  78. printk(KERN_DEBUG "pal_mc_err_inject for cpu%d:\n", cpu);
  79. printk(KERN_DEBUG "err_type_info=%lx,\n", err_type_info[cpu]);
  80. printk(KERN_DEBUG "err_struct_info=%lx,\n", err_struct_info[cpu]);
  81. printk(KERN_DEBUG "err_data_buffer=%lx, %lx, %lx.\n",
  82. err_data_buffer[cpu].data1,
  83. err_data_buffer[cpu].data2,
  84. err_data_buffer[cpu].data3);
  85. #endif
  86. switch (call_start) {
  87. case 0: /* Do nothing. */
  88. break;
  89. case 1: /* Call pal_mc_error_inject in physical mode. */
  90. status[cpu]=ia64_pal_mc_error_inject_phys(err_type_info[cpu],
  91. err_struct_info[cpu],
  92. ia64_tpa(&err_data_buffer[cpu]),
  93. &capabilities[cpu],
  94. &resources[cpu]);
  95. break;
  96. case 2: /* Call pal_mc_error_inject in virtual mode. */
  97. status[cpu]=ia64_pal_mc_error_inject_virt(err_type_info[cpu],
  98. err_struct_info[cpu],
  99. ia64_tpa(&err_data_buffer[cpu]),
  100. &capabilities[cpu],
  101. &resources[cpu]);
  102. break;
  103. default:
  104. status[cpu] = -EINVAL;
  105. break;
  106. }
  107. #ifdef ERR_INJ_DEBUG
  108. printk(KERN_DEBUG "Returns: status=%d,\n", (int)status[cpu]);
  109. printk(KERN_DEBUG "capapbilities=%lx,\n", capabilities[cpu]);
  110. printk(KERN_DEBUG "resources=%lx\n", resources[cpu]);
  111. #endif
  112. return size;
  113. }
  114. show(err_type_info)
  115. store(err_type_info)
  116. static ssize_t
  117. show_virtual_to_phys(struct device *dev, struct device_attribute *attr,
  118. char *buf)
  119. {
  120. unsigned int cpu=dev->id;
  121. return sprintf(buf, "%lx\n", phys_addr[cpu]);
  122. }
  123. static ssize_t
  124. store_virtual_to_phys(struct device *dev, struct device_attribute *attr,
  125. const char *buf, size_t size)
  126. {
  127. unsigned int cpu=dev->id;
  128. u64 virt_addr=simple_strtoull(buf, NULL, 16);
  129. int ret;
  130. ret = get_user_pages(current, current->mm, virt_addr,
  131. 1, FOLL_WRITE, NULL, NULL);
  132. if (ret<=0) {
  133. #ifdef ERR_INJ_DEBUG
  134. printk("Virtual address %lx is not existing.\n",virt_addr);
  135. #endif
  136. return -EINVAL;
  137. }
  138. phys_addr[cpu] = ia64_tpa(virt_addr);
  139. return size;
  140. }
  141. show(err_struct_info)
  142. store(err_struct_info)
  143. static ssize_t
  144. show_err_data_buffer(struct device *dev,
  145. struct device_attribute *attr, char *buf)
  146. {
  147. unsigned int cpu=dev->id;
  148. return sprintf(buf, "%lx, %lx, %lx\n",
  149. err_data_buffer[cpu].data1,
  150. err_data_buffer[cpu].data2,
  151. err_data_buffer[cpu].data3);
  152. }
  153. static ssize_t
  154. store_err_data_buffer(struct device *dev,
  155. struct device_attribute *attr,
  156. const char *buf, size_t size)
  157. {
  158. unsigned int cpu=dev->id;
  159. int ret;
  160. #ifdef ERR_INJ_DEBUG
  161. printk("write err_data_buffer=[%lx,%lx,%lx] on cpu%d\n",
  162. err_data_buffer[cpu].data1,
  163. err_data_buffer[cpu].data2,
  164. err_data_buffer[cpu].data3,
  165. cpu);
  166. #endif
  167. ret=sscanf(buf, "%lx, %lx, %lx",
  168. &err_data_buffer[cpu].data1,
  169. &err_data_buffer[cpu].data2,
  170. &err_data_buffer[cpu].data3);
  171. if (ret!=ERR_DATA_BUFFER_SIZE)
  172. return -EINVAL;
  173. return size;
  174. }
  175. show(status)
  176. show(capabilities)
  177. show(resources)
  178. define_one_rw(call_start);
  179. define_one_rw(err_type_info);
  180. define_one_rw(err_struct_info);
  181. define_one_rw(err_data_buffer);
  182. define_one_rw(virtual_to_phys);
  183. define_one_ro(status);
  184. define_one_ro(capabilities);
  185. define_one_ro(resources);
  186. static struct attribute *default_attrs[] = {
  187. &dev_attr_call_start.attr,
  188. &dev_attr_virtual_to_phys.attr,
  189. &dev_attr_err_type_info.attr,
  190. &dev_attr_err_struct_info.attr,
  191. &dev_attr_err_data_buffer.attr,
  192. &dev_attr_status.attr,
  193. &dev_attr_capabilities.attr,
  194. &dev_attr_resources.attr,
  195. NULL
  196. };
  197. static struct attribute_group err_inject_attr_group = {
  198. .attrs = default_attrs,
  199. .name = "err_inject"
  200. };
  201. /* Add/Remove err_inject interface for CPU device */
  202. static int err_inject_add_dev(struct device *sys_dev)
  203. {
  204. return sysfs_create_group(&sys_dev->kobj, &err_inject_attr_group);
  205. }
  206. static int err_inject_remove_dev(struct device *sys_dev)
  207. {
  208. sysfs_remove_group(&sys_dev->kobj, &err_inject_attr_group);
  209. return 0;
  210. }
  211. static int err_inject_cpu_callback(struct notifier_block *nfb,
  212. unsigned long action, void *hcpu)
  213. {
  214. unsigned int cpu = (unsigned long)hcpu;
  215. struct device *sys_dev;
  216. sys_dev = get_cpu_device(cpu);
  217. switch (action) {
  218. case CPU_ONLINE:
  219. case CPU_ONLINE_FROZEN:
  220. err_inject_add_dev(sys_dev);
  221. break;
  222. case CPU_DEAD:
  223. case CPU_DEAD_FROZEN:
  224. err_inject_remove_dev(sys_dev);
  225. break;
  226. }
  227. return NOTIFY_OK;
  228. }
  229. static struct notifier_block err_inject_cpu_notifier =
  230. {
  231. .notifier_call = err_inject_cpu_callback,
  232. };
  233. static int __init
  234. err_inject_init(void)
  235. {
  236. int i;
  237. #ifdef ERR_INJ_DEBUG
  238. printk(KERN_INFO "Enter error injection driver.\n");
  239. #endif
  240. cpu_notifier_register_begin();
  241. for_each_online_cpu(i) {
  242. err_inject_cpu_callback(&err_inject_cpu_notifier, CPU_ONLINE,
  243. (void *)(long)i);
  244. }
  245. __register_hotcpu_notifier(&err_inject_cpu_notifier);
  246. cpu_notifier_register_done();
  247. return 0;
  248. }
  249. static void __exit
  250. err_inject_exit(void)
  251. {
  252. int i;
  253. struct device *sys_dev;
  254. #ifdef ERR_INJ_DEBUG
  255. printk(KERN_INFO "Exit error injection driver.\n");
  256. #endif
  257. cpu_notifier_register_begin();
  258. for_each_online_cpu(i) {
  259. sys_dev = get_cpu_device(i);
  260. sysfs_remove_group(&sys_dev->kobj, &err_inject_attr_group);
  261. }
  262. __unregister_hotcpu_notifier(&err_inject_cpu_notifier);
  263. cpu_notifier_register_done();
  264. }
  265. module_init(err_inject_init);
  266. module_exit(err_inject_exit);
  267. MODULE_AUTHOR("Fenghua Yu <fenghua.yu@intel.com>");
  268. MODULE_DESCRIPTION("MC error injection kernel sysfs interface");
  269. MODULE_LICENSE("GPL");