1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22#include "qemu/osdep.h"
23#include "hw/nmi.h"
24#include "qapi/error.h"
25#include "qapi/qmp/qerror.h"
26#include "monitor/monitor.h"
27
28struct do_nmi_s {
29 int cpu_index;
30 Error *err;
31 bool handled;
32};
33
34static void nmi_children(Object *o, struct do_nmi_s *ns);
35
36static int do_nmi(Object *o, void *opaque)
37{
38 struct do_nmi_s *ns = opaque;
39 NMIState *n = (NMIState *) object_dynamic_cast(o, TYPE_NMI);
40
41 if (n) {
42 NMIClass *nc = NMI_GET_CLASS(n);
43
44 ns->handled = true;
45 nc->nmi_monitor_handler(n, ns->cpu_index, &ns->err);
46 if (ns->err) {
47 return -1;
48 }
49 }
50 nmi_children(o, ns);
51
52 return 0;
53}
54
55static void nmi_children(Object *o, struct do_nmi_s *ns)
56{
57 object_child_foreach(o, do_nmi, ns);
58}
59
60void nmi_monitor_handle(int cpu_index, Error **errp)
61{
62 struct do_nmi_s ns = {
63 .cpu_index = cpu_index,
64 .err = NULL,
65 .handled = false
66 };
67
68 nmi_children(object_get_root(), &ns);
69 if (ns.handled) {
70 error_propagate(errp, ns.err);
71 } else {
72 error_setg(errp, QERR_UNSUPPORTED);
73 }
74}
75
76void inject_nmi(void)
77{
78#if defined(TARGET_I386)
79 CPUState *cs;
80
81 CPU_FOREACH(cs) {
82 X86CPU *cpu = X86_CPU(cs);
83
84 if (!cpu->apic_state) {
85 cpu_interrupt(cs, CPU_INTERRUPT_NMI);
86 } else {
87 apic_deliver_nmi(cpu->apic_state);
88 }
89 }
90#else
91 nmi_monitor_handle(0, NULL);
92#endif
93}
94
95static const TypeInfo nmi_info = {
96 .name = TYPE_NMI,
97 .parent = TYPE_INTERFACE,
98 .class_size = sizeof(NMIClass),
99};
100
101static void nmi_register_types(void)
102{
103 type_register_static(&nmi_info);
104}
105
106type_init(nmi_register_types)
107