xref: /openbmc/qemu/hw/core/nmi.c (revision c6bd8c706a799eb0fece99f468aaa22b818036f3)
19cb805fdSAlexey Kardashevskiy /*
29cb805fdSAlexey Kardashevskiy  *  NMI monitor handler class and helpers.
39cb805fdSAlexey Kardashevskiy  *
49cb805fdSAlexey Kardashevskiy  *  Copyright IBM Corp., 2014
59cb805fdSAlexey Kardashevskiy  *
69cb805fdSAlexey Kardashevskiy  *  Author: Alexey Kardashevskiy <aik@ozlabs.ru>
79cb805fdSAlexey Kardashevskiy  *
89cb805fdSAlexey Kardashevskiy  *  This program is free software; you can redistribute it and/or modify
99cb805fdSAlexey Kardashevskiy  *  it under the terms of the GNU General Public License as published by
109cb805fdSAlexey Kardashevskiy  *  the Free Software Foundation; either version 2 of the License,
119cb805fdSAlexey Kardashevskiy  *  or (at your option) any later version.
129cb805fdSAlexey Kardashevskiy  *
139cb805fdSAlexey Kardashevskiy  *  This program is distributed in the hope that it will be useful,
149cb805fdSAlexey Kardashevskiy  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
159cb805fdSAlexey Kardashevskiy  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
169cb805fdSAlexey Kardashevskiy  *  GNU General Public License for more details.
179cb805fdSAlexey Kardashevskiy  *
189cb805fdSAlexey Kardashevskiy  *  You should have received a copy of the GNU General Public License
199cb805fdSAlexey Kardashevskiy  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
209cb805fdSAlexey Kardashevskiy  */
219cb805fdSAlexey Kardashevskiy 
229cb805fdSAlexey Kardashevskiy #include "hw/nmi.h"
239cb805fdSAlexey Kardashevskiy #include "qapi/qmp/qerror.h"
24f9a535e0SXu Wang #include "monitor/monitor.h"
259cb805fdSAlexey Kardashevskiy 
269cb805fdSAlexey Kardashevskiy struct do_nmi_s {
279cb805fdSAlexey Kardashevskiy     int cpu_index;
289cb805fdSAlexey Kardashevskiy     Error *errp;
299cb805fdSAlexey Kardashevskiy     bool handled;
309cb805fdSAlexey Kardashevskiy };
319cb805fdSAlexey Kardashevskiy 
329cb805fdSAlexey Kardashevskiy static void nmi_children(Object *o, struct do_nmi_s *ns);
339cb805fdSAlexey Kardashevskiy 
349cb805fdSAlexey Kardashevskiy static int do_nmi(Object *o, void *opaque)
359cb805fdSAlexey Kardashevskiy {
369cb805fdSAlexey Kardashevskiy     struct do_nmi_s *ns = opaque;
379cb805fdSAlexey Kardashevskiy     NMIState *n = (NMIState *) object_dynamic_cast(o, TYPE_NMI);
389cb805fdSAlexey Kardashevskiy 
399cb805fdSAlexey Kardashevskiy     if (n) {
409cb805fdSAlexey Kardashevskiy         NMIClass *nc = NMI_GET_CLASS(n);
419cb805fdSAlexey Kardashevskiy 
429cb805fdSAlexey Kardashevskiy         ns->handled = true;
439cb805fdSAlexey Kardashevskiy         nc->nmi_monitor_handler(n, ns->cpu_index, &ns->errp);
449cb805fdSAlexey Kardashevskiy         if (ns->errp) {
459cb805fdSAlexey Kardashevskiy             return -1;
469cb805fdSAlexey Kardashevskiy         }
479cb805fdSAlexey Kardashevskiy     }
489cb805fdSAlexey Kardashevskiy     nmi_children(o, ns);
499cb805fdSAlexey Kardashevskiy 
509cb805fdSAlexey Kardashevskiy     return 0;
519cb805fdSAlexey Kardashevskiy }
529cb805fdSAlexey Kardashevskiy 
539cb805fdSAlexey Kardashevskiy static void nmi_children(Object *o, struct do_nmi_s *ns)
549cb805fdSAlexey Kardashevskiy {
559cb805fdSAlexey Kardashevskiy     object_child_foreach(o, do_nmi, ns);
569cb805fdSAlexey Kardashevskiy }
579cb805fdSAlexey Kardashevskiy 
589cb805fdSAlexey Kardashevskiy void nmi_monitor_handle(int cpu_index, Error **errp)
599cb805fdSAlexey Kardashevskiy {
609cb805fdSAlexey Kardashevskiy     struct do_nmi_s ns = {
619cb805fdSAlexey Kardashevskiy         .cpu_index = cpu_index,
629cb805fdSAlexey Kardashevskiy         .errp = NULL,
639cb805fdSAlexey Kardashevskiy         .handled = false
649cb805fdSAlexey Kardashevskiy     };
659cb805fdSAlexey Kardashevskiy 
669cb805fdSAlexey Kardashevskiy     nmi_children(object_get_root(), &ns);
679cb805fdSAlexey Kardashevskiy     if (ns.handled) {
689cb805fdSAlexey Kardashevskiy         error_propagate(errp, ns.errp);
699cb805fdSAlexey Kardashevskiy     } else {
70*c6bd8c70SMarkus Armbruster         error_setg(errp, QERR_UNSUPPORTED);
719cb805fdSAlexey Kardashevskiy     }
729cb805fdSAlexey Kardashevskiy }
739cb805fdSAlexey Kardashevskiy 
74f9a535e0SXu Wang void inject_nmi(void)
75f9a535e0SXu Wang {
76f9a535e0SXu Wang #if defined(TARGET_I386)
77f9a535e0SXu Wang     CPUState *cs;
78f9a535e0SXu Wang 
79f9a535e0SXu Wang     CPU_FOREACH(cs) {
80f9a535e0SXu Wang         X86CPU *cpu = X86_CPU(cs);
81f9a535e0SXu Wang 
82f9a535e0SXu Wang         if (!cpu->apic_state) {
83f9a535e0SXu Wang             cpu_interrupt(cs, CPU_INTERRUPT_NMI);
84f9a535e0SXu Wang         } else {
85f9a535e0SXu Wang             apic_deliver_nmi(cpu->apic_state);
86f9a535e0SXu Wang         }
87f9a535e0SXu Wang     }
88f9a535e0SXu Wang #else
89f9a535e0SXu Wang     nmi_monitor_handle(0, NULL);
90f9a535e0SXu Wang #endif
91f9a535e0SXu Wang }
92f9a535e0SXu Wang 
939cb805fdSAlexey Kardashevskiy static const TypeInfo nmi_info = {
949cb805fdSAlexey Kardashevskiy     .name          = TYPE_NMI,
959cb805fdSAlexey Kardashevskiy     .parent        = TYPE_INTERFACE,
969cb805fdSAlexey Kardashevskiy     .class_size    = sizeof(NMIClass),
979cb805fdSAlexey Kardashevskiy };
989cb805fdSAlexey Kardashevskiy 
999cb805fdSAlexey Kardashevskiy static void nmi_register_types(void)
1009cb805fdSAlexey Kardashevskiy {
1019cb805fdSAlexey Kardashevskiy     type_register_static(&nmi_info);
1029cb805fdSAlexey Kardashevskiy }
1039cb805fdSAlexey Kardashevskiy 
1049cb805fdSAlexey Kardashevskiy type_init(nmi_register_types)
105