mce-inject.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. * Machine check injection support.
  3. * Copyright 2008 Intel Corporation.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; version 2
  8. * of the License.
  9. *
  10. * Authors:
  11. * Andi Kleen
  12. * Ying Huang
  13. */
  14. #include <linux/uaccess.h>
  15. #include <linux/module.h>
  16. #include <linux/timer.h>
  17. #include <linux/kernel.h>
  18. #include <linux/string.h>
  19. #include <linux/fs.h>
  20. #include <linux/smp.h>
  21. #include <linux/notifier.h>
  22. #include <linux/kdebug.h>
  23. #include <linux/cpu.h>
  24. #include <linux/sched.h>
  25. #include <linux/gfp.h>
  26. #include <asm/mce.h>
  27. #include <asm/apic.h>
  28. #include <asm/nmi.h>
  29. /* Update fake mce registers on current CPU. */
  30. static void inject_mce(struct mce *m)
  31. {
  32. struct mce *i = &per_cpu(injectm, m->extcpu);
  33. /* Make sure no one reads partially written injectm */
  34. i->finished = 0;
  35. mb();
  36. m->finished = 0;
  37. /* First set the fields after finished */
  38. i->extcpu = m->extcpu;
  39. mb();
  40. /* Now write record in order, finished last (except above) */
  41. memcpy(i, m, sizeof(struct mce));
  42. /* Finally activate it */
  43. mb();
  44. i->finished = 1;
  45. }
  46. static void raise_poll(struct mce *m)
  47. {
  48. unsigned long flags;
  49. mce_banks_t b;
  50. memset(&b, 0xff, sizeof(mce_banks_t));
  51. local_irq_save(flags);
  52. machine_check_poll(0, &b);
  53. local_irq_restore(flags);
  54. m->finished = 0;
  55. }
  56. static void raise_exception(struct mce *m, struct pt_regs *pregs)
  57. {
  58. struct pt_regs regs;
  59. unsigned long flags;
  60. if (!pregs) {
  61. memset(&regs, 0, sizeof(struct pt_regs));
  62. regs.ip = m->ip;
  63. regs.cs = m->cs;
  64. pregs = &regs;
  65. }
  66. /* in mcheck exeception handler, irq will be disabled */
  67. local_irq_save(flags);
  68. do_machine_check(pregs, 0);
  69. local_irq_restore(flags);
  70. m->finished = 0;
  71. }
  72. static cpumask_var_t mce_inject_cpumask;
  73. static int mce_raise_notify(struct notifier_block *self,
  74. unsigned long val, void *data)
  75. {
  76. struct die_args *args = (struct die_args *)data;
  77. int cpu = smp_processor_id();
  78. struct mce *m = &__get_cpu_var(injectm);
  79. if (val != DIE_NMI || !cpumask_test_cpu(cpu, mce_inject_cpumask))
  80. return NOTIFY_DONE;
  81. cpumask_clear_cpu(cpu, mce_inject_cpumask);
  82. if (m->inject_flags & MCJ_EXCEPTION)
  83. raise_exception(m, args->regs);
  84. else if (m->status)
  85. raise_poll(m);
  86. return NOTIFY_STOP;
  87. }
  88. static struct notifier_block mce_raise_nb = {
  89. .notifier_call = mce_raise_notify,
  90. .priority = NMI_LOCAL_NORMAL_PRIOR,
  91. };
  92. /* Inject mce on current CPU */
  93. static int raise_local(void)
  94. {
  95. struct mce *m = &__get_cpu_var(injectm);
  96. int context = MCJ_CTX(m->inject_flags);
  97. int ret = 0;
  98. int cpu = m->extcpu;
  99. if (m->inject_flags & MCJ_EXCEPTION) {
  100. printk(KERN_INFO "Triggering MCE exception on CPU %d\n", cpu);
  101. switch (context) {
  102. case MCJ_CTX_IRQ:
  103. /*
  104. * Could do more to fake interrupts like
  105. * calling irq_enter, but the necessary
  106. * machinery isn't exported currently.
  107. */
  108. /*FALL THROUGH*/
  109. case MCJ_CTX_PROCESS:
  110. raise_exception(m, NULL);
  111. break;
  112. default:
  113. printk(KERN_INFO "Invalid MCE context\n");
  114. ret = -EINVAL;
  115. }
  116. printk(KERN_INFO "MCE exception done on CPU %d\n", cpu);
  117. } else if (m->status) {
  118. printk(KERN_INFO "Starting machine check poll CPU %d\n", cpu);
  119. raise_poll(m);
  120. mce_notify_irq();
  121. printk(KERN_INFO "Machine check poll done on CPU %d\n", cpu);
  122. } else
  123. m->finished = 0;
  124. return ret;
  125. }
  126. static void raise_mce(struct mce *m)
  127. {
  128. int context = MCJ_CTX(m->inject_flags);
  129. inject_mce(m);
  130. if (context == MCJ_CTX_RANDOM)
  131. return;
  132. #ifdef CONFIG_X86_LOCAL_APIC
  133. if (m->inject_flags & MCJ_NMI_BROADCAST) {
  134. unsigned long start;
  135. int cpu;
  136. get_online_cpus();
  137. cpumask_copy(mce_inject_cpumask, cpu_online_mask);
  138. cpumask_clear_cpu(get_cpu(), mce_inject_cpumask);
  139. for_each_online_cpu(cpu) {
  140. struct mce *mcpu = &per_cpu(injectm, cpu);
  141. if (!mcpu->finished ||
  142. MCJ_CTX(mcpu->inject_flags) != MCJ_CTX_RANDOM)
  143. cpumask_clear_cpu(cpu, mce_inject_cpumask);
  144. }
  145. if (!cpumask_empty(mce_inject_cpumask))
  146. apic->send_IPI_mask(mce_inject_cpumask, NMI_VECTOR);
  147. start = jiffies;
  148. while (!cpumask_empty(mce_inject_cpumask)) {
  149. if (!time_before(jiffies, start + 2*HZ)) {
  150. printk(KERN_ERR
  151. "Timeout waiting for mce inject NMI %lx\n",
  152. *cpumask_bits(mce_inject_cpumask));
  153. break;
  154. }
  155. cpu_relax();
  156. }
  157. raise_local();
  158. put_cpu();
  159. put_online_cpus();
  160. } else
  161. #endif
  162. raise_local();
  163. }
  164. /* Error injection interface */
  165. static ssize_t mce_write(struct file *filp, const char __user *ubuf,
  166. size_t usize, loff_t *off)
  167. {
  168. struct mce m;
  169. if (!capable(CAP_SYS_ADMIN))
  170. return -EPERM;
  171. /*
  172. * There are some cases where real MSR reads could slip
  173. * through.
  174. */
  175. if (!boot_cpu_has(X86_FEATURE_MCE) || !boot_cpu_has(X86_FEATURE_MCA))
  176. return -EIO;
  177. if ((unsigned long)usize > sizeof(struct mce))
  178. usize = sizeof(struct mce);
  179. if (copy_from_user(&m, ubuf, usize))
  180. return -EFAULT;
  181. if (m.extcpu >= num_possible_cpus() || !cpu_online(m.extcpu))
  182. return -EINVAL;
  183. /*
  184. * Need to give user space some time to set everything up,
  185. * so do it a jiffie or two later everywhere.
  186. */
  187. schedule_timeout(2);
  188. raise_mce(&m);
  189. return usize;
  190. }
  191. static int inject_init(void)
  192. {
  193. if (!alloc_cpumask_var(&mce_inject_cpumask, GFP_KERNEL))
  194. return -ENOMEM;
  195. printk(KERN_INFO "Machine check injector initialized\n");
  196. mce_chrdev_ops.write = mce_write;
  197. register_die_notifier(&mce_raise_nb);
  198. return 0;
  199. }
  200. module_init(inject_init);
  201. /*
  202. * Cannot tolerate unloading currently because we cannot
  203. * guarantee all openers of mce_chrdev will get a reference to us.
  204. */
  205. MODULE_LICENSE("GPL");