mce-inject.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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 <asm/mce.h>
  26. #include <asm/apic.h>
  27. /* Update fake mce registers on current CPU. */
  28. static void inject_mce(struct mce *m)
  29. {
  30. struct mce *i = &per_cpu(injectm, m->extcpu);
  31. /* Make sure noone reads partially written injectm */
  32. i->finished = 0;
  33. mb();
  34. m->finished = 0;
  35. /* First set the fields after finished */
  36. i->extcpu = m->extcpu;
  37. mb();
  38. /* Now write record in order, finished last (except above) */
  39. memcpy(i, m, sizeof(struct mce));
  40. /* Finally activate it */
  41. mb();
  42. i->finished = 1;
  43. }
  44. static void raise_poll(struct mce *m)
  45. {
  46. unsigned long flags;
  47. mce_banks_t b;
  48. memset(&b, 0xff, sizeof(mce_banks_t));
  49. local_irq_save(flags);
  50. machine_check_poll(0, &b);
  51. local_irq_restore(flags);
  52. m->finished = 0;
  53. }
  54. static void raise_exception(struct mce *m, struct pt_regs *pregs)
  55. {
  56. struct pt_regs regs;
  57. unsigned long flags;
  58. if (!pregs) {
  59. memset(&regs, 0, sizeof(struct pt_regs));
  60. regs.ip = m->ip;
  61. regs.cs = m->cs;
  62. pregs = &regs;
  63. }
  64. /* in mcheck exeception handler, irq will be disabled */
  65. local_irq_save(flags);
  66. do_machine_check(pregs, 0);
  67. local_irq_restore(flags);
  68. m->finished = 0;
  69. }
  70. static cpumask_t mce_inject_cpumask;
  71. static int mce_raise_notify(struct notifier_block *self,
  72. unsigned long val, void *data)
  73. {
  74. struct die_args *args = (struct die_args *)data;
  75. int cpu = smp_processor_id();
  76. struct mce *m = &__get_cpu_var(injectm);
  77. if (val != DIE_NMI_IPI || !cpu_isset(cpu, mce_inject_cpumask))
  78. return NOTIFY_DONE;
  79. cpu_clear(cpu, mce_inject_cpumask);
  80. if (m->inject_flags & MCJ_EXCEPTION)
  81. raise_exception(m, args->regs);
  82. else if (m->status)
  83. raise_poll(m);
  84. return NOTIFY_STOP;
  85. }
  86. static struct notifier_block mce_raise_nb = {
  87. .notifier_call = mce_raise_notify,
  88. .priority = 1000,
  89. };
  90. /* Inject mce on current CPU */
  91. static int raise_local(struct mce *m)
  92. {
  93. int context = MCJ_CTX(m->inject_flags);
  94. int ret = 0;
  95. int cpu = m->extcpu;
  96. if (m->inject_flags & MCJ_EXCEPTION) {
  97. printk(KERN_INFO "Triggering MCE exception on CPU %d\n", cpu);
  98. switch (context) {
  99. case MCJ_CTX_IRQ:
  100. /*
  101. * Could do more to fake interrupts like
  102. * calling irq_enter, but the necessary
  103. * machinery isn't exported currently.
  104. */
  105. /*FALL THROUGH*/
  106. case MCJ_CTX_PROCESS:
  107. raise_exception(m, NULL);
  108. break;
  109. default:
  110. printk(KERN_INFO "Invalid MCE context\n");
  111. ret = -EINVAL;
  112. }
  113. printk(KERN_INFO "MCE exception done on CPU %d\n", cpu);
  114. } else if (m->status) {
  115. printk(KERN_INFO "Starting machine check poll CPU %d\n", cpu);
  116. raise_poll(m);
  117. mce_notify_irq();
  118. printk(KERN_INFO "Machine check poll done on CPU %d\n", cpu);
  119. } else
  120. m->finished = 0;
  121. return ret;
  122. }
  123. static void raise_mce(struct mce *m)
  124. {
  125. int context = MCJ_CTX(m->inject_flags);
  126. inject_mce(m);
  127. if (context == MCJ_CTX_RANDOM)
  128. return;
  129. #ifdef CONFIG_X86_LOCAL_APIC
  130. if (m->inject_flags & MCJ_NMI_BROADCAST) {
  131. unsigned long start;
  132. int cpu;
  133. get_online_cpus();
  134. mce_inject_cpumask = cpu_online_map;
  135. cpu_clear(get_cpu(), mce_inject_cpumask);
  136. for_each_online_cpu(cpu) {
  137. struct mce *mcpu = &per_cpu(injectm, cpu);
  138. if (!mcpu->finished ||
  139. MCJ_CTX(mcpu->inject_flags) != MCJ_CTX_RANDOM)
  140. cpu_clear(cpu, mce_inject_cpumask);
  141. }
  142. if (!cpus_empty(mce_inject_cpumask))
  143. apic->send_IPI_mask(&mce_inject_cpumask, NMI_VECTOR);
  144. start = jiffies;
  145. while (!cpus_empty(mce_inject_cpumask)) {
  146. if (!time_before(jiffies, start + 2*HZ)) {
  147. printk(KERN_ERR
  148. "Timeout waiting for mce inject NMI %lx\n",
  149. *cpus_addr(mce_inject_cpumask));
  150. break;
  151. }
  152. cpu_relax();
  153. }
  154. raise_local(m);
  155. put_cpu();
  156. put_online_cpus();
  157. } else
  158. #endif
  159. raise_local(m);
  160. }
  161. /* Error injection interface */
  162. static ssize_t mce_write(struct file *filp, const char __user *ubuf,
  163. size_t usize, loff_t *off)
  164. {
  165. struct mce m;
  166. if (!capable(CAP_SYS_ADMIN))
  167. return -EPERM;
  168. /*
  169. * There are some cases where real MSR reads could slip
  170. * through.
  171. */
  172. if (!boot_cpu_has(X86_FEATURE_MCE) || !boot_cpu_has(X86_FEATURE_MCA))
  173. return -EIO;
  174. if ((unsigned long)usize > sizeof(struct mce))
  175. usize = sizeof(struct mce);
  176. if (copy_from_user(&m, ubuf, usize))
  177. return -EFAULT;
  178. if (m.extcpu >= num_possible_cpus() || !cpu_online(m.extcpu))
  179. return -EINVAL;
  180. /*
  181. * Need to give user space some time to set everything up,
  182. * so do it a jiffie or two later everywhere.
  183. */
  184. schedule_timeout(2);
  185. raise_mce(&m);
  186. return usize;
  187. }
  188. static int inject_init(void)
  189. {
  190. printk(KERN_INFO "Machine check injector initialized\n");
  191. mce_chrdev_ops.write = mce_write;
  192. register_die_notifier(&mce_raise_nb);
  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");