mce_amd_inj.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. /*
  2. * A simple MCE injection facility for testing different aspects of the RAS
  3. * code. This driver should be built as module so that it can be loaded
  4. * on production kernels for testing purposes.
  5. *
  6. * This file may be distributed under the terms of the GNU General Public
  7. * License version 2.
  8. *
  9. * Copyright (c) 2010-15: Borislav Petkov <bp@alien8.de>
  10. * Advanced Micro Devices Inc.
  11. */
  12. #include <linux/kobject.h>
  13. #include <linux/debugfs.h>
  14. #include <linux/device.h>
  15. #include <linux/module.h>
  16. #include <linux/cpu.h>
  17. #include <linux/string.h>
  18. #include <linux/uaccess.h>
  19. #include <linux/pci.h>
  20. #include <asm/mce.h>
  21. #include <asm/amd_nb.h>
  22. #include <asm/irq_vectors.h>
  23. #include "../kernel/cpu/mcheck/mce-internal.h"
  24. /*
  25. * Collect all the MCi_XXX settings
  26. */
  27. static struct mce i_mce;
  28. static struct dentry *dfs_inj;
  29. static u8 n_banks;
  30. #define MAX_FLAG_OPT_SIZE 3
  31. #define NBCFG 0x44
  32. enum injection_type {
  33. SW_INJ = 0, /* SW injection, simply decode the error */
  34. HW_INJ, /* Trigger a #MC */
  35. DFR_INT_INJ, /* Trigger Deferred error interrupt */
  36. THR_INT_INJ, /* Trigger threshold interrupt */
  37. N_INJ_TYPES,
  38. };
  39. static const char * const flags_options[] = {
  40. [SW_INJ] = "sw",
  41. [HW_INJ] = "hw",
  42. [DFR_INT_INJ] = "df",
  43. [THR_INT_INJ] = "th",
  44. NULL
  45. };
  46. /* Set default injection to SW_INJ */
  47. static enum injection_type inj_type = SW_INJ;
  48. #define MCE_INJECT_SET(reg) \
  49. static int inj_##reg##_set(void *data, u64 val) \
  50. { \
  51. struct mce *m = (struct mce *)data; \
  52. \
  53. m->reg = val; \
  54. return 0; \
  55. }
  56. MCE_INJECT_SET(status);
  57. MCE_INJECT_SET(misc);
  58. MCE_INJECT_SET(addr);
  59. #define MCE_INJECT_GET(reg) \
  60. static int inj_##reg##_get(void *data, u64 *val) \
  61. { \
  62. struct mce *m = (struct mce *)data; \
  63. \
  64. *val = m->reg; \
  65. return 0; \
  66. }
  67. MCE_INJECT_GET(status);
  68. MCE_INJECT_GET(misc);
  69. MCE_INJECT_GET(addr);
  70. DEFINE_SIMPLE_ATTRIBUTE(status_fops, inj_status_get, inj_status_set, "%llx\n");
  71. DEFINE_SIMPLE_ATTRIBUTE(misc_fops, inj_misc_get, inj_misc_set, "%llx\n");
  72. DEFINE_SIMPLE_ATTRIBUTE(addr_fops, inj_addr_get, inj_addr_set, "%llx\n");
  73. /*
  74. * Caller needs to be make sure this cpu doesn't disappear
  75. * from under us, i.e.: get_cpu/put_cpu.
  76. */
  77. static int toggle_hw_mce_inject(unsigned int cpu, bool enable)
  78. {
  79. u32 l, h;
  80. int err;
  81. err = rdmsr_on_cpu(cpu, MSR_K7_HWCR, &l, &h);
  82. if (err) {
  83. pr_err("%s: error reading HWCR\n", __func__);
  84. return err;
  85. }
  86. enable ? (l |= BIT(18)) : (l &= ~BIT(18));
  87. err = wrmsr_on_cpu(cpu, MSR_K7_HWCR, l, h);
  88. if (err)
  89. pr_err("%s: error writing HWCR\n", __func__);
  90. return err;
  91. }
  92. static int __set_inj(const char *buf)
  93. {
  94. int i;
  95. for (i = 0; i < N_INJ_TYPES; i++) {
  96. if (!strncmp(flags_options[i], buf, strlen(flags_options[i]))) {
  97. inj_type = i;
  98. return 0;
  99. }
  100. }
  101. return -EINVAL;
  102. }
  103. static ssize_t flags_read(struct file *filp, char __user *ubuf,
  104. size_t cnt, loff_t *ppos)
  105. {
  106. char buf[MAX_FLAG_OPT_SIZE];
  107. int n;
  108. n = sprintf(buf, "%s\n", flags_options[inj_type]);
  109. return simple_read_from_buffer(ubuf, cnt, ppos, buf, n);
  110. }
  111. static ssize_t flags_write(struct file *filp, const char __user *ubuf,
  112. size_t cnt, loff_t *ppos)
  113. {
  114. char buf[MAX_FLAG_OPT_SIZE], *__buf;
  115. int err;
  116. if (cnt > MAX_FLAG_OPT_SIZE)
  117. return -EINVAL;
  118. if (copy_from_user(&buf, ubuf, cnt))
  119. return -EFAULT;
  120. buf[cnt - 1] = 0;
  121. /* strip whitespace */
  122. __buf = strstrip(buf);
  123. err = __set_inj(__buf);
  124. if (err) {
  125. pr_err("%s: Invalid flags value: %s\n", __func__, __buf);
  126. return err;
  127. }
  128. *ppos += cnt;
  129. return cnt;
  130. }
  131. static const struct file_operations flags_fops = {
  132. .read = flags_read,
  133. .write = flags_write,
  134. .llseek = generic_file_llseek,
  135. };
  136. /*
  137. * On which CPU to inject?
  138. */
  139. MCE_INJECT_GET(extcpu);
  140. static int inj_extcpu_set(void *data, u64 val)
  141. {
  142. struct mce *m = (struct mce *)data;
  143. if (val >= nr_cpu_ids || !cpu_online(val)) {
  144. pr_err("%s: Invalid CPU: %llu\n", __func__, val);
  145. return -EINVAL;
  146. }
  147. m->extcpu = val;
  148. return 0;
  149. }
  150. DEFINE_SIMPLE_ATTRIBUTE(extcpu_fops, inj_extcpu_get, inj_extcpu_set, "%llu\n");
  151. static void trigger_mce(void *info)
  152. {
  153. asm volatile("int $18");
  154. }
  155. static void trigger_dfr_int(void *info)
  156. {
  157. asm volatile("int %0" :: "i" (DEFERRED_ERROR_VECTOR));
  158. }
  159. static void trigger_thr_int(void *info)
  160. {
  161. asm volatile("int %0" :: "i" (THRESHOLD_APIC_VECTOR));
  162. }
  163. static u32 get_nbc_for_node(int node_id)
  164. {
  165. struct cpuinfo_x86 *c = &boot_cpu_data;
  166. u32 cores_per_node;
  167. cores_per_node = c->x86_max_cores / amd_get_nodes_per_socket();
  168. return cores_per_node * node_id;
  169. }
  170. static void toggle_nb_mca_mst_cpu(u16 nid)
  171. {
  172. struct pci_dev *F3 = node_to_amd_nb(nid)->misc;
  173. u32 val;
  174. int err;
  175. if (!F3)
  176. return;
  177. err = pci_read_config_dword(F3, NBCFG, &val);
  178. if (err) {
  179. pr_err("%s: Error reading F%dx%03x.\n",
  180. __func__, PCI_FUNC(F3->devfn), NBCFG);
  181. return;
  182. }
  183. if (val & BIT(27))
  184. return;
  185. pr_err("%s: Set D18F3x44[NbMcaToMstCpuEn] which BIOS hasn't done.\n",
  186. __func__);
  187. val |= BIT(27);
  188. err = pci_write_config_dword(F3, NBCFG, val);
  189. if (err)
  190. pr_err("%s: Error writing F%dx%03x.\n",
  191. __func__, PCI_FUNC(F3->devfn), NBCFG);
  192. }
  193. static void do_inject(void)
  194. {
  195. u64 mcg_status = 0;
  196. unsigned int cpu = i_mce.extcpu;
  197. u8 b = i_mce.bank;
  198. if (i_mce.misc)
  199. i_mce.status |= MCI_STATUS_MISCV;
  200. if (inj_type == SW_INJ) {
  201. mce_inject_log(&i_mce);
  202. return;
  203. }
  204. /* prep MCE global settings for the injection */
  205. mcg_status = MCG_STATUS_MCIP | MCG_STATUS_EIPV;
  206. if (!(i_mce.status & MCI_STATUS_PCC))
  207. mcg_status |= MCG_STATUS_RIPV;
  208. /*
  209. * Ensure necessary status bits for deferred errors:
  210. * - MCx_STATUS[Deferred]: make sure it is a deferred error
  211. * - MCx_STATUS[UC] cleared: deferred errors are _not_ UC
  212. */
  213. if (inj_type == DFR_INT_INJ) {
  214. i_mce.status |= MCI_STATUS_DEFERRED;
  215. i_mce.status |= (i_mce.status & ~MCI_STATUS_UC);
  216. }
  217. /*
  218. * For multi node CPUs, logging and reporting of bank 4 errors happens
  219. * only on the node base core. Refer to D18F3x44[NbMcaToMstCpuEn] for
  220. * Fam10h and later BKDGs.
  221. */
  222. if (static_cpu_has(X86_FEATURE_AMD_DCM) && b == 4) {
  223. toggle_nb_mca_mst_cpu(amd_get_nb_id(cpu));
  224. cpu = get_nbc_for_node(amd_get_nb_id(cpu));
  225. }
  226. get_online_cpus();
  227. if (!cpu_online(cpu))
  228. goto err;
  229. toggle_hw_mce_inject(cpu, true);
  230. wrmsr_on_cpu(cpu, MSR_IA32_MCG_STATUS,
  231. (u32)mcg_status, (u32)(mcg_status >> 32));
  232. wrmsr_on_cpu(cpu, MSR_IA32_MCx_STATUS(b),
  233. (u32)i_mce.status, (u32)(i_mce.status >> 32));
  234. wrmsr_on_cpu(cpu, MSR_IA32_MCx_ADDR(b),
  235. (u32)i_mce.addr, (u32)(i_mce.addr >> 32));
  236. wrmsr_on_cpu(cpu, MSR_IA32_MCx_MISC(b),
  237. (u32)i_mce.misc, (u32)(i_mce.misc >> 32));
  238. toggle_hw_mce_inject(cpu, false);
  239. switch (inj_type) {
  240. case DFR_INT_INJ:
  241. smp_call_function_single(cpu, trigger_dfr_int, NULL, 0);
  242. break;
  243. case THR_INT_INJ:
  244. smp_call_function_single(cpu, trigger_thr_int, NULL, 0);
  245. break;
  246. default:
  247. smp_call_function_single(cpu, trigger_mce, NULL, 0);
  248. }
  249. err:
  250. put_online_cpus();
  251. }
  252. /*
  253. * This denotes into which bank we're injecting and triggers
  254. * the injection, at the same time.
  255. */
  256. static int inj_bank_set(void *data, u64 val)
  257. {
  258. struct mce *m = (struct mce *)data;
  259. if (val >= n_banks) {
  260. pr_err("Non-existent MCE bank: %llu\n", val);
  261. return -EINVAL;
  262. }
  263. m->bank = val;
  264. do_inject();
  265. return 0;
  266. }
  267. MCE_INJECT_GET(bank);
  268. DEFINE_SIMPLE_ATTRIBUTE(bank_fops, inj_bank_get, inj_bank_set, "%llu\n");
  269. static const char readme_msg[] =
  270. "Description of the files and their usages:\n"
  271. "\n"
  272. "Note1: i refers to the bank number below.\n"
  273. "Note2: See respective BKDGs for the exact bit definitions of the files below\n"
  274. "as they mirror the hardware registers.\n"
  275. "\n"
  276. "status:\t Set MCi_STATUS: the bits in that MSR control the error type and\n"
  277. "\t attributes of the error which caused the MCE.\n"
  278. "\n"
  279. "misc:\t Set MCi_MISC: provide auxiliary info about the error. It is mostly\n"
  280. "\t used for error thresholding purposes and its validity is indicated by\n"
  281. "\t MCi_STATUS[MiscV].\n"
  282. "\n"
  283. "addr:\t Error address value to be written to MCi_ADDR. Log address information\n"
  284. "\t associated with the error.\n"
  285. "\n"
  286. "cpu:\t The CPU to inject the error on.\n"
  287. "\n"
  288. "bank:\t Specify the bank you want to inject the error into: the number of\n"
  289. "\t banks in a processor varies and is family/model-specific, therefore, the\n"
  290. "\t supplied value is sanity-checked. Setting the bank value also triggers the\n"
  291. "\t injection.\n"
  292. "\n"
  293. "flags:\t Injection type to be performed. Writing to this file will trigger a\n"
  294. "\t real machine check, an APIC interrupt or invoke the error decoder routines\n"
  295. "\t for AMD processors.\n"
  296. "\n"
  297. "\t Allowed error injection types:\n"
  298. "\t - \"sw\": Software error injection. Decode error to a human-readable \n"
  299. "\t format only. Safe to use.\n"
  300. "\t - \"hw\": Hardware error injection. Causes the #MC exception handler to \n"
  301. "\t handle the error. Be warned: might cause system panic if MCi_STATUS[PCC] \n"
  302. "\t is set. Therefore, consider setting (debugfs_mountpoint)/mce/fake_panic \n"
  303. "\t before injecting.\n"
  304. "\t - \"df\": Trigger APIC interrupt for Deferred error. Causes deferred \n"
  305. "\t error APIC interrupt handler to handle the error if the feature is \n"
  306. "\t is present in hardware. \n"
  307. "\t - \"th\": Trigger APIC interrupt for Threshold errors. Causes threshold \n"
  308. "\t APIC interrupt handler to handle the error. \n"
  309. "\n";
  310. static ssize_t
  311. inj_readme_read(struct file *filp, char __user *ubuf,
  312. size_t cnt, loff_t *ppos)
  313. {
  314. return simple_read_from_buffer(ubuf, cnt, ppos,
  315. readme_msg, strlen(readme_msg));
  316. }
  317. static const struct file_operations readme_fops = {
  318. .read = inj_readme_read,
  319. };
  320. static struct dfs_node {
  321. char *name;
  322. struct dentry *d;
  323. const struct file_operations *fops;
  324. umode_t perm;
  325. } dfs_fls[] = {
  326. { .name = "status", .fops = &status_fops, .perm = S_IRUSR | S_IWUSR },
  327. { .name = "misc", .fops = &misc_fops, .perm = S_IRUSR | S_IWUSR },
  328. { .name = "addr", .fops = &addr_fops, .perm = S_IRUSR | S_IWUSR },
  329. { .name = "bank", .fops = &bank_fops, .perm = S_IRUSR | S_IWUSR },
  330. { .name = "flags", .fops = &flags_fops, .perm = S_IRUSR | S_IWUSR },
  331. { .name = "cpu", .fops = &extcpu_fops, .perm = S_IRUSR | S_IWUSR },
  332. { .name = "README", .fops = &readme_fops, .perm = S_IRUSR | S_IRGRP | S_IROTH },
  333. };
  334. static int __init init_mce_inject(void)
  335. {
  336. int i;
  337. u64 cap;
  338. rdmsrl(MSR_IA32_MCG_CAP, cap);
  339. n_banks = cap & MCG_BANKCNT_MASK;
  340. dfs_inj = debugfs_create_dir("mce-inject", NULL);
  341. if (!dfs_inj)
  342. return -EINVAL;
  343. for (i = 0; i < ARRAY_SIZE(dfs_fls); i++) {
  344. dfs_fls[i].d = debugfs_create_file(dfs_fls[i].name,
  345. dfs_fls[i].perm,
  346. dfs_inj,
  347. &i_mce,
  348. dfs_fls[i].fops);
  349. if (!dfs_fls[i].d)
  350. goto err_dfs_add;
  351. }
  352. return 0;
  353. err_dfs_add:
  354. while (--i >= 0)
  355. debugfs_remove(dfs_fls[i].d);
  356. debugfs_remove(dfs_inj);
  357. dfs_inj = NULL;
  358. return -ENOMEM;
  359. }
  360. static void __exit exit_mce_inject(void)
  361. {
  362. int i;
  363. for (i = 0; i < ARRAY_SIZE(dfs_fls); i++)
  364. debugfs_remove(dfs_fls[i].d);
  365. memset(&dfs_fls, 0, sizeof(dfs_fls));
  366. debugfs_remove(dfs_inj);
  367. dfs_inj = NULL;
  368. }
  369. module_init(init_mce_inject);
  370. module_exit(exit_mce_inject);
  371. MODULE_LICENSE("GPL");
  372. MODULE_AUTHOR("Borislav Petkov <bp@alien8.de>");
  373. MODULE_AUTHOR("AMD Inc.");
  374. MODULE_DESCRIPTION("MCE injection facility for RAS testing");