mce-inject.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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(unsigned int cmd, struct pt_regs *regs)
  74. {
  75. int cpu = smp_processor_id();
  76. struct mce *m = &__get_cpu_var(injectm);
  77. if (!cpumask_test_cpu(cpu, mce_inject_cpumask))
  78. return NMI_DONE;
  79. cpumask_clear_cpu(cpu, mce_inject_cpumask);
  80. if (m->inject_flags & MCJ_EXCEPTION)
  81. raise_exception(m, regs);
  82. else if (m->status)
  83. raise_poll(m);
  84. return NMI_HANDLED;
  85. }
  86. /* Inject mce on current CPU */
  87. static int raise_local(void)
  88. {
  89. struct mce *m = &__get_cpu_var(injectm);
  90. int context = MCJ_CTX(m->inject_flags);
  91. int ret = 0;
  92. int cpu = m->extcpu;
  93. if (m->inject_flags & MCJ_EXCEPTION) {
  94. printk(KERN_INFO "Triggering MCE exception on CPU %d\n", cpu);
  95. switch (context) {
  96. case MCJ_CTX_IRQ:
  97. /*
  98. * Could do more to fake interrupts like
  99. * calling irq_enter, but the necessary
  100. * machinery isn't exported currently.
  101. */
  102. /*FALL THROUGH*/
  103. case MCJ_CTX_PROCESS:
  104. raise_exception(m, NULL);
  105. break;
  106. default:
  107. printk(KERN_INFO "Invalid MCE context\n");
  108. ret = -EINVAL;
  109. }
  110. printk(KERN_INFO "MCE exception done on CPU %d\n", cpu);
  111. } else if (m->status) {
  112. printk(KERN_INFO "Starting machine check poll CPU %d\n", cpu);
  113. raise_poll(m);
  114. mce_notify_irq();
  115. printk(KERN_INFO "Machine check poll done on CPU %d\n", cpu);
  116. } else
  117. m->finished = 0;
  118. return ret;
  119. }
  120. static void raise_mce(struct mce *m)
  121. {
  122. int context = MCJ_CTX(m->inject_flags);
  123. inject_mce(m);
  124. if (context == MCJ_CTX_RANDOM)
  125. return;
  126. #ifdef CONFIG_X86_LOCAL_APIC
  127. if (m->inject_flags & MCJ_NMI_BROADCAST) {
  128. unsigned long start;
  129. int cpu;
  130. get_online_cpus();
  131. cpumask_copy(mce_inject_cpumask, cpu_online_mask);
  132. cpumask_clear_cpu(get_cpu(), mce_inject_cpumask);
  133. for_each_online_cpu(cpu) {
  134. struct mce *mcpu = &per_cpu(injectm, cpu);
  135. if (!mcpu->finished ||
  136. MCJ_CTX(mcpu->inject_flags) != MCJ_CTX_RANDOM)
  137. cpumask_clear_cpu(cpu, mce_inject_cpumask);
  138. }
  139. if (!cpumask_empty(mce_inject_cpumask))
  140. apic->send_IPI_mask(mce_inject_cpumask, NMI_VECTOR);
  141. start = jiffies;
  142. while (!cpumask_empty(mce_inject_cpumask)) {
  143. if (!time_before(jiffies, start + 2*HZ)) {
  144. printk(KERN_ERR
  145. "Timeout waiting for mce inject NMI %lx\n",
  146. *cpumask_bits(mce_inject_cpumask));
  147. break;
  148. }
  149. cpu_relax();
  150. }
  151. raise_local();
  152. put_cpu();
  153. put_online_cpus();
  154. } else
  155. #endif
  156. raise_local();
  157. }
  158. /* Error injection interface */
  159. static ssize_t mce_write(struct file *filp, const char __user *ubuf,
  160. size_t usize, loff_t *off)
  161. {
  162. struct mce m;
  163. if (!capable(CAP_SYS_ADMIN))
  164. return -EPERM;
  165. /*
  166. * There are some cases where real MSR reads could slip
  167. * through.
  168. */
  169. if (!boot_cpu_has(X86_FEATURE_MCE) || !boot_cpu_has(X86_FEATURE_MCA))
  170. return -EIO;
  171. if ((unsigned long)usize > sizeof(struct mce))
  172. usize = sizeof(struct mce);
  173. if (copy_from_user(&m, ubuf, usize))
  174. return -EFAULT;
  175. if (m.extcpu >= num_possible_cpus() || !cpu_online(m.extcpu))
  176. return -EINVAL;
  177. /*
  178. * Need to give user space some time to set everything up,
  179. * so do it a jiffie or two later everywhere.
  180. */
  181. schedule_timeout(2);
  182. raise_mce(&m);
  183. return usize;
  184. }
  185. static int inject_init(void)
  186. {
  187. if (!alloc_cpumask_var(&mce_inject_cpumask, GFP_KERNEL))
  188. return -ENOMEM;
  189. printk(KERN_INFO "Machine check injector initialized\n");
  190. mce_chrdev_ops.write = mce_write;
  191. register_nmi_handler(NMI_LOCAL, mce_raise_notify, 0,
  192. "mce_notify");
  193. return 0;
  194. }
  195. module_init(inject_init);
  196. /*
  197. * Cannot tolerate unloading currently because we cannot
  198. * guarantee all openers of mce_chrdev will get a reference to us.
  199. */
  200. MODULE_LICENSE("GPL");