mce-inject.c 5.4 KB

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