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_fast(virt_addr, 1, FOLL_WRITE, NULL);
  131. if (ret<=0) {
  132. #ifdef ERR_INJ_DEBUG
  133. printk("Virtual address %lx is not existing.\n",virt_addr);
  134. #endif
  135. return -EINVAL;
  136. }
  137. phys_addr[cpu] = ia64_tpa(virt_addr);
  138. return size;
  139. }
  140. show(err_struct_info)
  141. store(err_struct_info)
  142. static ssize_t
  143. show_err_data_buffer(struct device *dev,
  144. struct device_attribute *attr, char *buf)
  145. {
  146. unsigned int cpu=dev->id;
  147. return sprintf(buf, "%lx, %lx, %lx\n",
  148. err_data_buffer[cpu].data1,
  149. err_data_buffer[cpu].data2,
  150. err_data_buffer[cpu].data3);
  151. }
  152. static ssize_t
  153. store_err_data_buffer(struct device *dev,
  154. struct device_attribute *attr,
  155. const char *buf, size_t size)
  156. {
  157. unsigned int cpu=dev->id;
  158. int ret;
  159. #ifdef ERR_INJ_DEBUG
  160. printk("write err_data_buffer=[%lx,%lx,%lx] on cpu%d\n",
  161. err_data_buffer[cpu].data1,
  162. err_data_buffer[cpu].data2,
  163. err_data_buffer[cpu].data3,
  164. cpu);
  165. #endif
  166. ret=sscanf(buf, "%lx, %lx, %lx",
  167. &err_data_buffer[cpu].data1,
  168. &err_data_buffer[cpu].data2,
  169. &err_data_buffer[cpu].data3);
  170. if (ret!=ERR_DATA_BUFFER_SIZE)
  171. return -EINVAL;
  172. return size;
  173. }
  174. show(status)
  175. show(capabilities)
  176. show(resources)
  177. define_one_rw(call_start);
  178. define_one_rw(err_type_info);
  179. define_one_rw(err_struct_info);
  180. define_one_rw(err_data_buffer);
  181. define_one_rw(virtual_to_phys);
  182. define_one_ro(status);
  183. define_one_ro(capabilities);
  184. define_one_ro(resources);
  185. static struct attribute *default_attrs[] = {
  186. &dev_attr_call_start.attr,
  187. &dev_attr_virtual_to_phys.attr,
  188. &dev_attr_err_type_info.attr,
  189. &dev_attr_err_struct_info.attr,
  190. &dev_attr_err_data_buffer.attr,
  191. &dev_attr_status.attr,
  192. &dev_attr_capabilities.attr,
  193. &dev_attr_resources.attr,
  194. NULL
  195. };
  196. static struct attribute_group err_inject_attr_group = {
  197. .attrs = default_attrs,
  198. .name = "err_inject"
  199. };
  200. /* Add/Remove err_inject interface for CPU device */
  201. static int err_inject_add_dev(struct device *sys_dev)
  202. {
  203. return sysfs_create_group(&sys_dev->kobj, &err_inject_attr_group);
  204. }
  205. static int err_inject_remove_dev(struct device *sys_dev)
  206. {
  207. sysfs_remove_group(&sys_dev->kobj, &err_inject_attr_group);
  208. return 0;
  209. }
  210. static int err_inject_cpu_callback(struct notifier_block *nfb,
  211. unsigned long action, void *hcpu)
  212. {
  213. unsigned int cpu = (unsigned long)hcpu;
  214. struct device *sys_dev;
  215. sys_dev = get_cpu_device(cpu);
  216. switch (action) {
  217. case CPU_ONLINE:
  218. case CPU_ONLINE_FROZEN:
  219. err_inject_add_dev(sys_dev);
  220. break;
  221. case CPU_DEAD:
  222. case CPU_DEAD_FROZEN:
  223. err_inject_remove_dev(sys_dev);
  224. break;
  225. }
  226. return NOTIFY_OK;
  227. }
  228. static struct notifier_block err_inject_cpu_notifier =
  229. {
  230. .notifier_call = err_inject_cpu_callback,
  231. };
  232. static int __init
  233. err_inject_init(void)
  234. {
  235. int i;
  236. #ifdef ERR_INJ_DEBUG
  237. printk(KERN_INFO "Enter error injection driver.\n");
  238. #endif
  239. cpu_notifier_register_begin();
  240. for_each_online_cpu(i) {
  241. err_inject_cpu_callback(&err_inject_cpu_notifier, CPU_ONLINE,
  242. (void *)(long)i);
  243. }
  244. __register_hotcpu_notifier(&err_inject_cpu_notifier);
  245. cpu_notifier_register_done();
  246. return 0;
  247. }
  248. static void __exit
  249. err_inject_exit(void)
  250. {
  251. int i;
  252. struct device *sys_dev;
  253. #ifdef ERR_INJ_DEBUG
  254. printk(KERN_INFO "Exit error injection driver.\n");
  255. #endif
  256. cpu_notifier_register_begin();
  257. for_each_online_cpu(i) {
  258. sys_dev = get_cpu_device(i);
  259. sysfs_remove_group(&sys_dev->kobj, &err_inject_attr_group);
  260. }
  261. __unregister_hotcpu_notifier(&err_inject_cpu_notifier);
  262. cpu_notifier_register_done();
  263. }
  264. module_init(err_inject_init);
  265. module_exit(err_inject_exit);
  266. MODULE_AUTHOR("Fenghua Yu <fenghua.yu@intel.com>");
  267. MODULE_DESCRIPTION("MC error injection kernel sysfs interface");
  268. MODULE_LICENSE("GPL");