Path: blob/master/arch/x86/kernel/cpu/mcheck/mce-inject.c
10775 views
/*1* Machine check injection support.2* Copyright 2008 Intel Corporation.3*4* This program is free software; you can redistribute it and/or5* modify it under the terms of the GNU General Public License6* as published by the Free Software Foundation; version 27* of the License.8*9* Authors:10* Andi Kleen11* Ying Huang12*/13#include <linux/uaccess.h>14#include <linux/module.h>15#include <linux/timer.h>16#include <linux/kernel.h>17#include <linux/string.h>18#include <linux/fs.h>19#include <linux/smp.h>20#include <linux/notifier.h>21#include <linux/kdebug.h>22#include <linux/cpu.h>23#include <linux/sched.h>24#include <linux/gfp.h>25#include <asm/mce.h>26#include <asm/apic.h>27#include <asm/nmi.h>2829/* Update fake mce registers on current CPU. */30static void inject_mce(struct mce *m)31{32struct mce *i = &per_cpu(injectm, m->extcpu);3334/* Make sure no one reads partially written injectm */35i->finished = 0;36mb();37m->finished = 0;38/* First set the fields after finished */39i->extcpu = m->extcpu;40mb();41/* Now write record in order, finished last (except above) */42memcpy(i, m, sizeof(struct mce));43/* Finally activate it */44mb();45i->finished = 1;46}4748static void raise_poll(struct mce *m)49{50unsigned long flags;51mce_banks_t b;5253memset(&b, 0xff, sizeof(mce_banks_t));54local_irq_save(flags);55machine_check_poll(0, &b);56local_irq_restore(flags);57m->finished = 0;58}5960static void raise_exception(struct mce *m, struct pt_regs *pregs)61{62struct pt_regs regs;63unsigned long flags;6465if (!pregs) {66memset(®s, 0, sizeof(struct pt_regs));67regs.ip = m->ip;68regs.cs = m->cs;69pregs = ®s;70}71/* in mcheck exeception handler, irq will be disabled */72local_irq_save(flags);73do_machine_check(pregs, 0);74local_irq_restore(flags);75m->finished = 0;76}7778static cpumask_var_t mce_inject_cpumask;7980static int mce_raise_notify(struct notifier_block *self,81unsigned long val, void *data)82{83struct die_args *args = (struct die_args *)data;84int cpu = smp_processor_id();85struct mce *m = &__get_cpu_var(injectm);86if (val != DIE_NMI || !cpumask_test_cpu(cpu, mce_inject_cpumask))87return NOTIFY_DONE;88cpumask_clear_cpu(cpu, mce_inject_cpumask);89if (m->inject_flags & MCJ_EXCEPTION)90raise_exception(m, args->regs);91else if (m->status)92raise_poll(m);93return NOTIFY_STOP;94}9596static struct notifier_block mce_raise_nb = {97.notifier_call = mce_raise_notify,98.priority = NMI_LOCAL_NORMAL_PRIOR,99};100101/* Inject mce on current CPU */102static int raise_local(void)103{104struct mce *m = &__get_cpu_var(injectm);105int context = MCJ_CTX(m->inject_flags);106int ret = 0;107int cpu = m->extcpu;108109if (m->inject_flags & MCJ_EXCEPTION) {110printk(KERN_INFO "Triggering MCE exception on CPU %d\n", cpu);111switch (context) {112case MCJ_CTX_IRQ:113/*114* Could do more to fake interrupts like115* calling irq_enter, but the necessary116* machinery isn't exported currently.117*/118/*FALL THROUGH*/119case MCJ_CTX_PROCESS:120raise_exception(m, NULL);121break;122default:123printk(KERN_INFO "Invalid MCE context\n");124ret = -EINVAL;125}126printk(KERN_INFO "MCE exception done on CPU %d\n", cpu);127} else if (m->status) {128printk(KERN_INFO "Starting machine check poll CPU %d\n", cpu);129raise_poll(m);130mce_notify_irq();131printk(KERN_INFO "Machine check poll done on CPU %d\n", cpu);132} else133m->finished = 0;134135return ret;136}137138static void raise_mce(struct mce *m)139{140int context = MCJ_CTX(m->inject_flags);141142inject_mce(m);143144if (context == MCJ_CTX_RANDOM)145return;146147#ifdef CONFIG_X86_LOCAL_APIC148if (m->inject_flags & MCJ_NMI_BROADCAST) {149unsigned long start;150int cpu;151get_online_cpus();152cpumask_copy(mce_inject_cpumask, cpu_online_mask);153cpumask_clear_cpu(get_cpu(), mce_inject_cpumask);154for_each_online_cpu(cpu) {155struct mce *mcpu = &per_cpu(injectm, cpu);156if (!mcpu->finished ||157MCJ_CTX(mcpu->inject_flags) != MCJ_CTX_RANDOM)158cpumask_clear_cpu(cpu, mce_inject_cpumask);159}160if (!cpumask_empty(mce_inject_cpumask))161apic->send_IPI_mask(mce_inject_cpumask, NMI_VECTOR);162start = jiffies;163while (!cpumask_empty(mce_inject_cpumask)) {164if (!time_before(jiffies, start + 2*HZ)) {165printk(KERN_ERR166"Timeout waiting for mce inject NMI %lx\n",167*cpumask_bits(mce_inject_cpumask));168break;169}170cpu_relax();171}172raise_local();173put_cpu();174put_online_cpus();175} else176#endif177raise_local();178}179180/* Error injection interface */181static ssize_t mce_write(struct file *filp, const char __user *ubuf,182size_t usize, loff_t *off)183{184struct mce m;185186if (!capable(CAP_SYS_ADMIN))187return -EPERM;188/*189* There are some cases where real MSR reads could slip190* through.191*/192if (!boot_cpu_has(X86_FEATURE_MCE) || !boot_cpu_has(X86_FEATURE_MCA))193return -EIO;194195if ((unsigned long)usize > sizeof(struct mce))196usize = sizeof(struct mce);197if (copy_from_user(&m, ubuf, usize))198return -EFAULT;199200if (m.extcpu >= num_possible_cpus() || !cpu_online(m.extcpu))201return -EINVAL;202203/*204* Need to give user space some time to set everything up,205* so do it a jiffie or two later everywhere.206*/207schedule_timeout(2);208raise_mce(&m);209return usize;210}211212static int inject_init(void)213{214if (!alloc_cpumask_var(&mce_inject_cpumask, GFP_KERNEL))215return -ENOMEM;216printk(KERN_INFO "Machine check injector initialized\n");217mce_chrdev_ops.write = mce_write;218register_die_notifier(&mce_raise_nb);219return 0;220}221222module_init(inject_init);223/*224* Cannot tolerate unloading currently because we cannot225* guarantee all openers of mce_chrdev will get a reference to us.226*/227MODULE_LICENSE("GPL");228229230