xref: /qemu/hw/i386/x86.c (revision bd2142c3)
1 /*
2  * Copyright (c) 2003-2004 Fabrice Bellard
3  * Copyright (c) 2019 Red Hat, Inc.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy
6  * of this software and associated documentation files (the "Software"), to deal
7  * in the Software without restriction, including without limitation the rights
8  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9  * copies of the Software, and to permit persons to whom the Software is
10  * furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21  * THE SOFTWARE.
22  */
23 #include "qemu/osdep.h"
24 #include "qemu/error-report.h"
25 #include "qemu/option.h"
26 #include "qemu/cutils.h"
27 #include "qemu/units.h"
28 #include "qemu/datadir.h"
29 #include "qapi/error.h"
30 #include "qapi/qmp/qerror.h"
31 #include "qapi/qapi-visit-common.h"
32 #include "qapi/clone-visitor.h"
33 #include "qapi/qapi-visit-machine.h"
34 #include "qapi/visitor.h"
35 #include "sysemu/qtest.h"
36 #include "sysemu/whpx.h"
37 #include "sysemu/numa.h"
38 #include "sysemu/replay.h"
39 #include "sysemu/sysemu.h"
40 #include "sysemu/cpu-timers.h"
41 #include "trace.h"
42 
43 #include "hw/i386/x86.h"
44 #include "target/i386/cpu.h"
45 #include "hw/i386/topology.h"
46 #include "hw/i386/fw_cfg.h"
47 #include "hw/intc/i8259.h"
48 #include "hw/rtc/mc146818rtc.h"
49 #include "target/i386/sev.h"
50 
51 #include "hw/acpi/cpu_hotplug.h"
52 #include "hw/irq.h"
53 #include "hw/nmi.h"
54 #include "hw/loader.h"
55 #include "multiboot.h"
56 #include "elf.h"
57 #include "standard-headers/asm-x86/bootparam.h"
58 #include CONFIG_DEVICES
59 #include "kvm/kvm_i386.h"
60 
61 /* Physical Address of PVH entry point read from kernel ELF NOTE */
62 static size_t pvh_start_addr;
63 
64 inline void init_topo_info(X86CPUTopoInfo *topo_info,
65                            const X86MachineState *x86ms)
66 {
67     MachineState *ms = MACHINE(x86ms);
68 
69     topo_info->dies_per_pkg = ms->smp.dies;
70     topo_info->cores_per_die = ms->smp.cores;
71     topo_info->threads_per_core = ms->smp.threads;
72 }
73 
74 /*
75  * Calculates initial APIC ID for a specific CPU index
76  *
77  * Currently we need to be able to calculate the APIC ID from the CPU index
78  * alone (without requiring a CPU object), as the QEMU<->Seabios interfaces have
79  * no concept of "CPU index", and the NUMA tables on fw_cfg need the APIC ID of
80  * all CPUs up to max_cpus.
81  */
82 uint32_t x86_cpu_apic_id_from_index(X86MachineState *x86ms,
83                                     unsigned int cpu_index)
84 {
85     X86CPUTopoInfo topo_info;
86 
87     init_topo_info(&topo_info, x86ms);
88 
89     return x86_apicid_from_cpu_idx(&topo_info, cpu_index);
90 }
91 
92 
93 void x86_cpu_new(X86MachineState *x86ms, int64_t apic_id, Error **errp)
94 {
95     Object *cpu = object_new(MACHINE(x86ms)->cpu_type);
96 
97     if (!object_property_set_uint(cpu, "apic-id", apic_id, errp)) {
98         goto out;
99     }
100     qdev_realize(DEVICE(cpu), NULL, errp);
101 
102 out:
103     object_unref(cpu);
104 }
105 
106 void x86_cpus_init(X86MachineState *x86ms, int default_cpu_version)
107 {
108     int i;
109     const CPUArchIdList *possible_cpus;
110     MachineState *ms = MACHINE(x86ms);
111     MachineClass *mc = MACHINE_GET_CLASS(x86ms);
112 
113     x86_cpu_set_default_version(default_cpu_version);
114 
115     /*
116      * Calculates the limit to CPU APIC ID values
117      *
118      * Limit for the APIC ID value, so that all
119      * CPU APIC IDs are < x86ms->apic_id_limit.
120      *
121      * This is used for FW_CFG_MAX_CPUS. See comments on fw_cfg_arch_create().
122      */
123     x86ms->apic_id_limit = x86_cpu_apic_id_from_index(x86ms,
124                                                       ms->smp.max_cpus - 1) + 1;
125     possible_cpus = mc->possible_cpu_arch_ids(ms);
126     for (i = 0; i < ms->smp.cpus; i++) {
127         x86_cpu_new(x86ms, possible_cpus->cpus[i].arch_id, &error_fatal);
128     }
129 }
130 
131 void x86_rtc_set_cpus_count(ISADevice *rtc, uint16_t cpus_count)
132 {
133     if (cpus_count > 0xff) {
134         /*
135          * If the number of CPUs can't be represented in 8 bits, the
136          * BIOS must use "FW_CFG_NB_CPUS". Set RTC field to 0 just
137          * to make old BIOSes fail more predictably.
138          */
139         rtc_set_memory(rtc, 0x5f, 0);
140     } else {
141         rtc_set_memory(rtc, 0x5f, cpus_count - 1);
142     }
143 }
144 
145 static int x86_apic_cmp(const void *a, const void *b)
146 {
147    CPUArchId *apic_a = (CPUArchId *)a;
148    CPUArchId *apic_b = (CPUArchId *)b;
149 
150    return apic_a->arch_id - apic_b->arch_id;
151 }
152 
153 /*
154  * returns pointer to CPUArchId descriptor that matches CPU's apic_id
155  * in ms->possible_cpus->cpus, if ms->possible_cpus->cpus has no
156  * entry corresponding to CPU's apic_id returns NULL.
157  */
158 CPUArchId *x86_find_cpu_slot(MachineState *ms, uint32_t id, int *idx)
159 {
160     CPUArchId apic_id, *found_cpu;
161 
162     apic_id.arch_id = id;
163     found_cpu = bsearch(&apic_id, ms->possible_cpus->cpus,
164         ms->possible_cpus->len, sizeof(*ms->possible_cpus->cpus),
165         x86_apic_cmp);
166     if (found_cpu && idx) {
167         *idx = found_cpu - ms->possible_cpus->cpus;
168     }
169     return found_cpu;
170 }
171 
172 void x86_cpu_plug(HotplugHandler *hotplug_dev,
173                   DeviceState *dev, Error **errp)
174 {
175     CPUArchId *found_cpu;
176     Error *local_err = NULL;
177     X86CPU *cpu = X86_CPU(dev);
178     X86MachineState *x86ms = X86_MACHINE(hotplug_dev);
179 
180     if (x86ms->acpi_dev) {
181         hotplug_handler_plug(x86ms->acpi_dev, dev, &local_err);
182         if (local_err) {
183             goto out;
184         }
185     }
186 
187     /* increment the number of CPUs */
188     x86ms->boot_cpus++;
189     if (x86ms->rtc) {
190         x86_rtc_set_cpus_count(x86ms->rtc, x86ms->boot_cpus);
191     }
192     if (x86ms->fw_cfg) {
193         fw_cfg_modify_i16(x86ms->fw_cfg, FW_CFG_NB_CPUS, x86ms->boot_cpus);
194     }
195 
196     found_cpu = x86_find_cpu_slot(MACHINE(x86ms), cpu->apic_id, NULL);
197     found_cpu->cpu = OBJECT(dev);
198 out:
199     error_propagate(errp, local_err);
200 }
201 
202 void x86_cpu_unplug_request_cb(HotplugHandler *hotplug_dev,
203                                DeviceState *dev, Error **errp)
204 {
205     int idx = -1;
206     X86CPU *cpu = X86_CPU(dev);
207     X86MachineState *x86ms = X86_MACHINE(hotplug_dev);
208 
209     if (!x86ms->acpi_dev) {
210         error_setg(errp, "CPU hot unplug not supported without ACPI");
211         return;
212     }
213 
214     x86_find_cpu_slot(MACHINE(x86ms), cpu->apic_id, &idx);
215     assert(idx != -1);
216     if (idx == 0) {
217         error_setg(errp, "Boot CPU is unpluggable");
218         return;
219     }
220 
221     hotplug_handler_unplug_request(x86ms->acpi_dev, dev,
222                                    errp);
223 }
224 
225 void x86_cpu_unplug_cb(HotplugHandler *hotplug_dev,
226                        DeviceState *dev, Error **errp)
227 {
228     CPUArchId *found_cpu;
229     Error *local_err = NULL;
230     X86CPU *cpu = X86_CPU(dev);
231     X86MachineState *x86ms = X86_MACHINE(hotplug_dev);
232 
233     hotplug_handler_unplug(x86ms->acpi_dev, dev, &local_err);
234     if (local_err) {
235         goto out;
236     }
237 
238     found_cpu = x86_find_cpu_slot(MACHINE(x86ms), cpu->apic_id, NULL);
239     found_cpu->cpu = NULL;
240     qdev_unrealize(dev);
241 
242     /* decrement the number of CPUs */
243     x86ms->boot_cpus--;
244     /* Update the number of CPUs in CMOS */
245     x86_rtc_set_cpus_count(x86ms->rtc, x86ms->boot_cpus);
246     fw_cfg_modify_i16(x86ms->fw_cfg, FW_CFG_NB_CPUS, x86ms->boot_cpus);
247  out:
248     error_propagate(errp, local_err);
249 }
250 
251 void x86_cpu_pre_plug(HotplugHandler *hotplug_dev,
252                       DeviceState *dev, Error **errp)
253 {
254     int idx;
255     CPUState *cs;
256     CPUArchId *cpu_slot;
257     X86CPUTopoIDs topo_ids;
258     X86CPU *cpu = X86_CPU(dev);
259     CPUX86State *env = &cpu->env;
260     MachineState *ms = MACHINE(hotplug_dev);
261     X86MachineState *x86ms = X86_MACHINE(hotplug_dev);
262     unsigned int smp_cores = ms->smp.cores;
263     unsigned int smp_threads = ms->smp.threads;
264     X86CPUTopoInfo topo_info;
265 
266     if (!object_dynamic_cast(OBJECT(cpu), ms->cpu_type)) {
267         error_setg(errp, "Invalid CPU type, expected cpu type: '%s'",
268                    ms->cpu_type);
269         return;
270     }
271 
272     if (x86ms->acpi_dev) {
273         Error *local_err = NULL;
274 
275         hotplug_handler_pre_plug(HOTPLUG_HANDLER(x86ms->acpi_dev), dev,
276                                  &local_err);
277         if (local_err) {
278             error_propagate(errp, local_err);
279             return;
280         }
281     }
282 
283     init_topo_info(&topo_info, x86ms);
284 
285     env->nr_dies = ms->smp.dies;
286 
287     /*
288      * If APIC ID is not set,
289      * set it based on socket/die/core/thread properties.
290      */
291     if (cpu->apic_id == UNASSIGNED_APIC_ID) {
292         int max_socket = (ms->smp.max_cpus - 1) /
293                                 smp_threads / smp_cores / ms->smp.dies;
294 
295         /*
296          * die-id was optional in QEMU 4.0 and older, so keep it optional
297          * if there's only one die per socket.
298          */
299         if (cpu->die_id < 0 && ms->smp.dies == 1) {
300             cpu->die_id = 0;
301         }
302 
303         if (cpu->socket_id < 0) {
304             error_setg(errp, "CPU socket-id is not set");
305             return;
306         } else if (cpu->socket_id > max_socket) {
307             error_setg(errp, "Invalid CPU socket-id: %u must be in range 0:%u",
308                        cpu->socket_id, max_socket);
309             return;
310         }
311         if (cpu->die_id < 0) {
312             error_setg(errp, "CPU die-id is not set");
313             return;
314         } else if (cpu->die_id > ms->smp.dies - 1) {
315             error_setg(errp, "Invalid CPU die-id: %u must be in range 0:%u",
316                        cpu->die_id, ms->smp.dies - 1);
317             return;
318         }
319         if (cpu->core_id < 0) {
320             error_setg(errp, "CPU core-id is not set");
321             return;
322         } else if (cpu->core_id > (smp_cores - 1)) {
323             error_setg(errp, "Invalid CPU core-id: %u must be in range 0:%u",
324                        cpu->core_id, smp_cores - 1);
325             return;
326         }
327         if (cpu->thread_id < 0) {
328             error_setg(errp, "CPU thread-id is not set");
329             return;
330         } else if (cpu->thread_id > (smp_threads - 1)) {
331             error_setg(errp, "Invalid CPU thread-id: %u must be in range 0:%u",
332                        cpu->thread_id, smp_threads - 1);
333             return;
334         }
335 
336         topo_ids.pkg_id = cpu->socket_id;
337         topo_ids.die_id = cpu->die_id;
338         topo_ids.core_id = cpu->core_id;
339         topo_ids.smt_id = cpu->thread_id;
340         cpu->apic_id = x86_apicid_from_topo_ids(&topo_info, &topo_ids);
341     }
342 
343     cpu_slot = x86_find_cpu_slot(MACHINE(x86ms), cpu->apic_id, &idx);
344     if (!cpu_slot) {
345         MachineState *ms = MACHINE(x86ms);
346 
347         x86_topo_ids_from_apicid(cpu->apic_id, &topo_info, &topo_ids);
348         error_setg(errp,
349             "Invalid CPU [socket: %u, die: %u, core: %u, thread: %u] with"
350             " APIC ID %" PRIu32 ", valid index range 0:%d",
351             topo_ids.pkg_id, topo_ids.die_id, topo_ids.core_id, topo_ids.smt_id,
352             cpu->apic_id, ms->possible_cpus->len - 1);
353         return;
354     }
355 
356     if (cpu_slot->cpu) {
357         error_setg(errp, "CPU[%d] with APIC ID %" PRIu32 " exists",
358                    idx, cpu->apic_id);
359         return;
360     }
361 
362     /* if 'address' properties socket-id/core-id/thread-id are not set, set them
363      * so that machine_query_hotpluggable_cpus would show correct values
364      */
365     /* TODO: move socket_id/core_id/thread_id checks into x86_cpu_realizefn()
366      * once -smp refactoring is complete and there will be CPU private
367      * CPUState::nr_cores and CPUState::nr_threads fields instead of globals */
368     x86_topo_ids_from_apicid(cpu->apic_id, &topo_info, &topo_ids);
369     if (cpu->socket_id != -1 && cpu->socket_id != topo_ids.pkg_id) {
370         error_setg(errp, "property socket-id: %u doesn't match set apic-id:"
371             " 0x%x (socket-id: %u)", cpu->socket_id, cpu->apic_id,
372             topo_ids.pkg_id);
373         return;
374     }
375     cpu->socket_id = topo_ids.pkg_id;
376 
377     if (cpu->die_id != -1 && cpu->die_id != topo_ids.die_id) {
378         error_setg(errp, "property die-id: %u doesn't match set apic-id:"
379             " 0x%x (die-id: %u)", cpu->die_id, cpu->apic_id, topo_ids.die_id);
380         return;
381     }
382     cpu->die_id = topo_ids.die_id;
383 
384     if (cpu->core_id != -1 && cpu->core_id != topo_ids.core_id) {
385         error_setg(errp, "property core-id: %u doesn't match set apic-id:"
386             " 0x%x (core-id: %u)", cpu->core_id, cpu->apic_id,
387             topo_ids.core_id);
388         return;
389     }
390     cpu->core_id = topo_ids.core_id;
391 
392     if (cpu->thread_id != -1 && cpu->thread_id != topo_ids.smt_id) {
393         error_setg(errp, "property thread-id: %u doesn't match set apic-id:"
394             " 0x%x (thread-id: %u)", cpu->thread_id, cpu->apic_id,
395             topo_ids.smt_id);
396         return;
397     }
398     cpu->thread_id = topo_ids.smt_id;
399 
400     if (hyperv_feat_enabled(cpu, HYPERV_FEAT_VPINDEX) &&
401         !kvm_hv_vpindex_settable()) {
402         error_setg(errp, "kernel doesn't allow setting HyperV VP_INDEX");
403         return;
404     }
405 
406     cs = CPU(cpu);
407     cs->cpu_index = idx;
408 
409     numa_cpu_pre_plug(cpu_slot, dev, errp);
410 }
411 
412 CpuInstanceProperties
413 x86_cpu_index_to_props(MachineState *ms, unsigned cpu_index)
414 {
415     MachineClass *mc = MACHINE_GET_CLASS(ms);
416     const CPUArchIdList *possible_cpus = mc->possible_cpu_arch_ids(ms);
417 
418     assert(cpu_index < possible_cpus->len);
419     return possible_cpus->cpus[cpu_index].props;
420 }
421 
422 int64_t x86_get_default_cpu_node_id(const MachineState *ms, int idx)
423 {
424    X86CPUTopoIDs topo_ids;
425    X86MachineState *x86ms = X86_MACHINE(ms);
426    X86CPUTopoInfo topo_info;
427 
428    init_topo_info(&topo_info, x86ms);
429 
430    assert(idx < ms->possible_cpus->len);
431    x86_topo_ids_from_apicid(ms->possible_cpus->cpus[idx].arch_id,
432                             &topo_info, &topo_ids);
433    return topo_ids.pkg_id % ms->numa_state->num_nodes;
434 }
435 
436 const CPUArchIdList *x86_possible_cpu_arch_ids(MachineState *ms)
437 {
438     X86MachineState *x86ms = X86_MACHINE(ms);
439     unsigned int max_cpus = ms->smp.max_cpus;
440     X86CPUTopoInfo topo_info;
441     int i;
442 
443     if (ms->possible_cpus) {
444         /*
445          * make sure that max_cpus hasn't changed since the first use, i.e.
446          * -smp hasn't been parsed after it
447          */
448         assert(ms->possible_cpus->len == max_cpus);
449         return ms->possible_cpus;
450     }
451 
452     ms->possible_cpus = g_malloc0(sizeof(CPUArchIdList) +
453                                   sizeof(CPUArchId) * max_cpus);
454     ms->possible_cpus->len = max_cpus;
455 
456     init_topo_info(&topo_info, x86ms);
457 
458     for (i = 0; i < ms->possible_cpus->len; i++) {
459         X86CPUTopoIDs topo_ids;
460 
461         ms->possible_cpus->cpus[i].type = ms->cpu_type;
462         ms->possible_cpus->cpus[i].vcpus_count = 1;
463         ms->possible_cpus->cpus[i].arch_id =
464             x86_cpu_apic_id_from_index(x86ms, i);
465         x86_topo_ids_from_apicid(ms->possible_cpus->cpus[i].arch_id,
466                                  &topo_info, &topo_ids);
467         ms->possible_cpus->cpus[i].props.has_socket_id = true;
468         ms->possible_cpus->cpus[i].props.socket_id = topo_ids.pkg_id;
469         if (ms->smp.dies > 1) {
470             ms->possible_cpus->cpus[i].props.has_die_id = true;
471             ms->possible_cpus->cpus[i].props.die_id = topo_ids.die_id;
472         }
473         ms->possible_cpus->cpus[i].props.has_core_id = true;
474         ms->possible_cpus->cpus[i].props.core_id = topo_ids.core_id;
475         ms->possible_cpus->cpus[i].props.has_thread_id = true;
476         ms->possible_cpus->cpus[i].props.thread_id = topo_ids.smt_id;
477     }
478     return ms->possible_cpus;
479 }
480 
481 static void x86_nmi(NMIState *n, int cpu_index, Error **errp)
482 {
483     /* cpu index isn't used */
484     CPUState *cs;
485 
486     CPU_FOREACH(cs) {
487         X86CPU *cpu = X86_CPU(cs);
488 
489         if (!cpu->apic_state) {
490             cpu_interrupt(cs, CPU_INTERRUPT_NMI);
491         } else {
492             apic_deliver_nmi(cpu->apic_state);
493         }
494     }
495 }
496 
497 static long get_file_size(FILE *f)
498 {
499     long where, size;
500 
501     /* XXX: on Unix systems, using fstat() probably makes more sense */
502 
503     where = ftell(f);
504     fseek(f, 0, SEEK_END);
505     size = ftell(f);
506     fseek(f, where, SEEK_SET);
507 
508     return size;
509 }
510 
511 /* TSC handling */
512 uint64_t cpu_get_tsc(CPUX86State *env)
513 {
514     return cpus_get_elapsed_ticks();
515 }
516 
517 /* IRQ handling */
518 static void pic_irq_request(void *opaque, int irq, int level)
519 {
520     CPUState *cs = first_cpu;
521     X86CPU *cpu = X86_CPU(cs);
522 
523     trace_x86_pic_interrupt(irq, level);
524     if (cpu->apic_state && !kvm_irqchip_in_kernel() &&
525         !whpx_apic_in_platform()) {
526         CPU_FOREACH(cs) {
527             cpu = X86_CPU(cs);
528             if (apic_accept_pic_intr(cpu->apic_state)) {
529                 apic_deliver_pic_intr(cpu->apic_state, level);
530             }
531         }
532     } else {
533         if (level) {
534             cpu_interrupt(cs, CPU_INTERRUPT_HARD);
535         } else {
536             cpu_reset_interrupt(cs, CPU_INTERRUPT_HARD);
537         }
538     }
539 }
540 
541 qemu_irq x86_allocate_cpu_irq(void)
542 {
543     return qemu_allocate_irq(pic_irq_request, NULL, 0);
544 }
545 
546 int cpu_get_pic_interrupt(CPUX86State *env)
547 {
548     X86CPU *cpu = env_archcpu(env);
549     int intno;
550 
551     if (!kvm_irqchip_in_kernel() && !whpx_apic_in_platform()) {
552         intno = apic_get_interrupt(cpu->apic_state);
553         if (intno >= 0) {
554             return intno;
555         }
556         /* read the irq from the PIC */
557         if (!apic_accept_pic_intr(cpu->apic_state)) {
558             return -1;
559         }
560     }
561 
562     intno = pic_read_irq(isa_pic);
563     return intno;
564 }
565 
566 DeviceState *cpu_get_current_apic(void)
567 {
568     if (current_cpu) {
569         X86CPU *cpu = X86_CPU(current_cpu);
570         return cpu->apic_state;
571     } else {
572         return NULL;
573     }
574 }
575 
576 void gsi_handler(void *opaque, int n, int level)
577 {
578     GSIState *s = opaque;
579 
580     trace_x86_gsi_interrupt(n, level);
581     switch (n) {
582     case 0 ... ISA_NUM_IRQS - 1:
583         if (s->i8259_irq[n]) {
584             /* Under KVM, Kernel will forward to both PIC and IOAPIC */
585             qemu_set_irq(s->i8259_irq[n], level);
586         }
587         /* fall through */
588     case ISA_NUM_IRQS ... IOAPIC_NUM_PINS - 1:
589         qemu_set_irq(s->ioapic_irq[n], level);
590         break;
591     case IO_APIC_SECONDARY_IRQBASE
592         ... IO_APIC_SECONDARY_IRQBASE + IOAPIC_NUM_PINS - 1:
593         qemu_set_irq(s->ioapic2_irq[n - IO_APIC_SECONDARY_IRQBASE], level);
594         break;
595     }
596 }
597 
598 void ioapic_init_gsi(GSIState *gsi_state, const char *parent_name)
599 {
600     DeviceState *dev;
601     SysBusDevice *d;
602     unsigned int i;
603 
604     assert(parent_name);
605     if (kvm_ioapic_in_kernel()) {
606         dev = qdev_new(TYPE_KVM_IOAPIC);
607     } else {
608         dev = qdev_new(TYPE_IOAPIC);
609     }
610     object_property_add_child(object_resolve_path(parent_name, NULL),
611                               "ioapic", OBJECT(dev));
612     d = SYS_BUS_DEVICE(dev);
613     sysbus_realize_and_unref(d, &error_fatal);
614     sysbus_mmio_map(d, 0, IO_APIC_DEFAULT_ADDRESS);
615 
616     for (i = 0; i < IOAPIC_NUM_PINS; i++) {
617         gsi_state->ioapic_irq[i] = qdev_get_gpio_in(dev, i);
618     }
619 }
620 
621 DeviceState *ioapic_init_secondary(GSIState *gsi_state)
622 {
623     DeviceState *dev;
624     SysBusDevice *d;
625     unsigned int i;
626 
627     dev = qdev_new(TYPE_IOAPIC);
628     d = SYS_BUS_DEVICE(dev);
629     sysbus_realize_and_unref(d, &error_fatal);
630     sysbus_mmio_map(d, 0, IO_APIC_SECONDARY_ADDRESS);
631 
632     for (i = 0; i < IOAPIC_NUM_PINS; i++) {
633         gsi_state->ioapic2_irq[i] = qdev_get_gpio_in(dev, i);
634     }
635     return dev;
636 }
637 
638 struct setup_data {
639     uint64_t next;
640     uint32_t type;
641     uint32_t len;
642     uint8_t data[];
643 } __attribute__((packed));
644 
645 
646 /*
647  * The entry point into the kernel for PVH boot is different from
648  * the native entry point.  The PVH entry is defined by the x86/HVM
649  * direct boot ABI and is available in an ELFNOTE in the kernel binary.
650  *
651  * This function is passed to load_elf() when it is called from
652  * load_elfboot() which then additionally checks for an ELF Note of
653  * type XEN_ELFNOTE_PHYS32_ENTRY and passes it to this function to
654  * parse the PVH entry address from the ELF Note.
655  *
656  * Due to trickery in elf_opts.h, load_elf() is actually available as
657  * load_elf32() or load_elf64() and this routine needs to be able
658  * to deal with being called as 32 or 64 bit.
659  *
660  * The address of the PVH entry point is saved to the 'pvh_start_addr'
661  * global variable.  (although the entry point is 32-bit, the kernel
662  * binary can be either 32-bit or 64-bit).
663  */
664 static uint64_t read_pvh_start_addr(void *arg1, void *arg2, bool is64)
665 {
666     size_t *elf_note_data_addr;
667 
668     /* Check if ELF Note header passed in is valid */
669     if (arg1 == NULL) {
670         return 0;
671     }
672 
673     if (is64) {
674         struct elf64_note *nhdr64 = (struct elf64_note *)arg1;
675         uint64_t nhdr_size64 = sizeof(struct elf64_note);
676         uint64_t phdr_align = *(uint64_t *)arg2;
677         uint64_t nhdr_namesz = nhdr64->n_namesz;
678 
679         elf_note_data_addr =
680             ((void *)nhdr64) + nhdr_size64 +
681             QEMU_ALIGN_UP(nhdr_namesz, phdr_align);
682 
683         pvh_start_addr = *elf_note_data_addr;
684     } else {
685         struct elf32_note *nhdr32 = (struct elf32_note *)arg1;
686         uint32_t nhdr_size32 = sizeof(struct elf32_note);
687         uint32_t phdr_align = *(uint32_t *)arg2;
688         uint32_t nhdr_namesz = nhdr32->n_namesz;
689 
690         elf_note_data_addr =
691             ((void *)nhdr32) + nhdr_size32 +
692             QEMU_ALIGN_UP(nhdr_namesz, phdr_align);
693 
694         pvh_start_addr = *(uint32_t *)elf_note_data_addr;
695     }
696 
697     return pvh_start_addr;
698 }
699 
700 static bool load_elfboot(const char *kernel_filename,
701                          int kernel_file_size,
702                          uint8_t *header,
703                          size_t pvh_xen_start_addr,
704                          FWCfgState *fw_cfg)
705 {
706     uint32_t flags = 0;
707     uint32_t mh_load_addr = 0;
708     uint32_t elf_kernel_size = 0;
709     uint64_t elf_entry;
710     uint64_t elf_low, elf_high;
711     int kernel_size;
712 
713     if (ldl_p(header) != 0x464c457f) {
714         return false; /* no elfboot */
715     }
716 
717     bool elf_is64 = header[EI_CLASS] == ELFCLASS64;
718     flags = elf_is64 ?
719         ((Elf64_Ehdr *)header)->e_flags : ((Elf32_Ehdr *)header)->e_flags;
720 
721     if (flags & 0x00010004) { /* LOAD_ELF_HEADER_HAS_ADDR */
722         error_report("elfboot unsupported flags = %x", flags);
723         exit(1);
724     }
725 
726     uint64_t elf_note_type = XEN_ELFNOTE_PHYS32_ENTRY;
727     kernel_size = load_elf(kernel_filename, read_pvh_start_addr,
728                            NULL, &elf_note_type, &elf_entry,
729                            &elf_low, &elf_high, NULL, 0, I386_ELF_MACHINE,
730                            0, 0);
731 
732     if (kernel_size < 0) {
733         error_report("Error while loading elf kernel");
734         exit(1);
735     }
736     mh_load_addr = elf_low;
737     elf_kernel_size = elf_high - elf_low;
738 
739     if (pvh_start_addr == 0) {
740         error_report("Error loading uncompressed kernel without PVH ELF Note");
741         exit(1);
742     }
743     fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_ENTRY, pvh_start_addr);
744     fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_ADDR, mh_load_addr);
745     fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_SIZE, elf_kernel_size);
746 
747     return true;
748 }
749 
750 void x86_load_linux(X86MachineState *x86ms,
751                     FWCfgState *fw_cfg,
752                     int acpi_data_size,
753                     bool pvh_enabled)
754 {
755     bool linuxboot_dma_enabled = X86_MACHINE_GET_CLASS(x86ms)->fwcfg_dma_enabled;
756     uint16_t protocol;
757     int setup_size, kernel_size, cmdline_size;
758     int dtb_size, setup_data_offset;
759     uint32_t initrd_max;
760     uint8_t header[8192], *setup, *kernel;
761     hwaddr real_addr, prot_addr, cmdline_addr, initrd_addr = 0;
762     FILE *f;
763     char *vmode;
764     MachineState *machine = MACHINE(x86ms);
765     struct setup_data *setup_data;
766     const char *kernel_filename = machine->kernel_filename;
767     const char *initrd_filename = machine->initrd_filename;
768     const char *dtb_filename = machine->dtb;
769     const char *kernel_cmdline = machine->kernel_cmdline;
770     SevKernelLoaderContext sev_load_ctx = {};
771 
772     /* Align to 16 bytes as a paranoia measure */
773     cmdline_size = (strlen(kernel_cmdline) + 16) & ~15;
774 
775     /* load the kernel header */
776     f = fopen(kernel_filename, "rb");
777     if (!f) {
778         fprintf(stderr, "qemu: could not open kernel file '%s': %s\n",
779                 kernel_filename, strerror(errno));
780         exit(1);
781     }
782 
783     kernel_size = get_file_size(f);
784     if (!kernel_size ||
785         fread(header, 1, MIN(ARRAY_SIZE(header), kernel_size), f) !=
786         MIN(ARRAY_SIZE(header), kernel_size)) {
787         fprintf(stderr, "qemu: could not load kernel '%s': %s\n",
788                 kernel_filename, strerror(errno));
789         exit(1);
790     }
791 
792     /* kernel protocol version */
793     if (ldl_p(header + 0x202) == 0x53726448) {
794         protocol = lduw_p(header + 0x206);
795     } else {
796         /*
797          * This could be a multiboot kernel. If it is, let's stop treating it
798          * like a Linux kernel.
799          * Note: some multiboot images could be in the ELF format (the same of
800          * PVH), so we try multiboot first since we check the multiboot magic
801          * header before to load it.
802          */
803         if (load_multiboot(x86ms, fw_cfg, f, kernel_filename, initrd_filename,
804                            kernel_cmdline, kernel_size, header)) {
805             return;
806         }
807         /*
808          * Check if the file is an uncompressed kernel file (ELF) and load it,
809          * saving the PVH entry point used by the x86/HVM direct boot ABI.
810          * If load_elfboot() is successful, populate the fw_cfg info.
811          */
812         if (pvh_enabled &&
813             load_elfboot(kernel_filename, kernel_size,
814                          header, pvh_start_addr, fw_cfg)) {
815             fclose(f);
816 
817             fw_cfg_add_i32(fw_cfg, FW_CFG_CMDLINE_SIZE,
818                 strlen(kernel_cmdline) + 1);
819             fw_cfg_add_string(fw_cfg, FW_CFG_CMDLINE_DATA, kernel_cmdline);
820 
821             fw_cfg_add_i32(fw_cfg, FW_CFG_SETUP_SIZE, sizeof(header));
822             fw_cfg_add_bytes(fw_cfg, FW_CFG_SETUP_DATA,
823                              header, sizeof(header));
824 
825             /* load initrd */
826             if (initrd_filename) {
827                 GMappedFile *mapped_file;
828                 gsize initrd_size;
829                 gchar *initrd_data;
830                 GError *gerr = NULL;
831 
832                 mapped_file = g_mapped_file_new(initrd_filename, false, &gerr);
833                 if (!mapped_file) {
834                     fprintf(stderr, "qemu: error reading initrd %s: %s\n",
835                             initrd_filename, gerr->message);
836                     exit(1);
837                 }
838                 x86ms->initrd_mapped_file = mapped_file;
839 
840                 initrd_data = g_mapped_file_get_contents(mapped_file);
841                 initrd_size = g_mapped_file_get_length(mapped_file);
842                 initrd_max = x86ms->below_4g_mem_size - acpi_data_size - 1;
843                 if (initrd_size >= initrd_max) {
844                     fprintf(stderr, "qemu: initrd is too large, cannot support."
845                             "(max: %"PRIu32", need %"PRId64")\n",
846                             initrd_max, (uint64_t)initrd_size);
847                     exit(1);
848                 }
849 
850                 initrd_addr = (initrd_max - initrd_size) & ~4095;
851 
852                 fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_ADDR, initrd_addr);
853                 fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_SIZE, initrd_size);
854                 fw_cfg_add_bytes(fw_cfg, FW_CFG_INITRD_DATA, initrd_data,
855                                  initrd_size);
856             }
857 
858             option_rom[nb_option_roms].bootindex = 0;
859             option_rom[nb_option_roms].name = "pvh.bin";
860             nb_option_roms++;
861 
862             return;
863         }
864         protocol = 0;
865     }
866 
867     if (protocol < 0x200 || !(header[0x211] & 0x01)) {
868         /* Low kernel */
869         real_addr    = 0x90000;
870         cmdline_addr = 0x9a000 - cmdline_size;
871         prot_addr    = 0x10000;
872     } else if (protocol < 0x202) {
873         /* High but ancient kernel */
874         real_addr    = 0x90000;
875         cmdline_addr = 0x9a000 - cmdline_size;
876         prot_addr    = 0x100000;
877     } else {
878         /* High and recent kernel */
879         real_addr    = 0x10000;
880         cmdline_addr = 0x20000;
881         prot_addr    = 0x100000;
882     }
883 
884     /* highest address for loading the initrd */
885     if (protocol >= 0x20c &&
886         lduw_p(header + 0x236) & XLF_CAN_BE_LOADED_ABOVE_4G) {
887         /*
888          * Linux has supported initrd up to 4 GB for a very long time (2007,
889          * long before XLF_CAN_BE_LOADED_ABOVE_4G which was added in 2013),
890          * though it only sets initrd_max to 2 GB to "work around bootloader
891          * bugs". Luckily, QEMU firmware(which does something like bootloader)
892          * has supported this.
893          *
894          * It's believed that if XLF_CAN_BE_LOADED_ABOVE_4G is set, initrd can
895          * be loaded into any address.
896          *
897          * In addition, initrd_max is uint32_t simply because QEMU doesn't
898          * support the 64-bit boot protocol (specifically the ext_ramdisk_image
899          * field).
900          *
901          * Therefore here just limit initrd_max to UINT32_MAX simply as well.
902          */
903         initrd_max = UINT32_MAX;
904     } else if (protocol >= 0x203) {
905         initrd_max = ldl_p(header + 0x22c);
906     } else {
907         initrd_max = 0x37ffffff;
908     }
909 
910     if (initrd_max >= x86ms->below_4g_mem_size - acpi_data_size) {
911         initrd_max = x86ms->below_4g_mem_size - acpi_data_size - 1;
912     }
913 
914     fw_cfg_add_i32(fw_cfg, FW_CFG_CMDLINE_ADDR, cmdline_addr);
915     fw_cfg_add_i32(fw_cfg, FW_CFG_CMDLINE_SIZE, strlen(kernel_cmdline) + 1);
916     fw_cfg_add_string(fw_cfg, FW_CFG_CMDLINE_DATA, kernel_cmdline);
917     sev_load_ctx.cmdline_data = (char *)kernel_cmdline;
918     sev_load_ctx.cmdline_size = strlen(kernel_cmdline) + 1;
919 
920     if (protocol >= 0x202) {
921         stl_p(header + 0x228, cmdline_addr);
922     } else {
923         stw_p(header + 0x20, 0xA33F);
924         stw_p(header + 0x22, cmdline_addr - real_addr);
925     }
926 
927     /* handle vga= parameter */
928     vmode = strstr(kernel_cmdline, "vga=");
929     if (vmode) {
930         unsigned int video_mode;
931         const char *end;
932         int ret;
933         /* skip "vga=" */
934         vmode += 4;
935         if (!strncmp(vmode, "normal", 6)) {
936             video_mode = 0xffff;
937         } else if (!strncmp(vmode, "ext", 3)) {
938             video_mode = 0xfffe;
939         } else if (!strncmp(vmode, "ask", 3)) {
940             video_mode = 0xfffd;
941         } else {
942             ret = qemu_strtoui(vmode, &end, 0, &video_mode);
943             if (ret != 0 || (*end && *end != ' ')) {
944                 fprintf(stderr, "qemu: invalid 'vga=' kernel parameter.\n");
945                 exit(1);
946             }
947         }
948         stw_p(header + 0x1fa, video_mode);
949     }
950 
951     /* loader type */
952     /*
953      * High nybble = B reserved for QEMU; low nybble is revision number.
954      * If this code is substantially changed, you may want to consider
955      * incrementing the revision.
956      */
957     if (protocol >= 0x200) {
958         header[0x210] = 0xB0;
959     }
960     /* heap */
961     if (protocol >= 0x201) {
962         header[0x211] |= 0x80; /* CAN_USE_HEAP */
963         stw_p(header + 0x224, cmdline_addr - real_addr - 0x200);
964     }
965 
966     /* load initrd */
967     if (initrd_filename) {
968         GMappedFile *mapped_file;
969         gsize initrd_size;
970         gchar *initrd_data;
971         GError *gerr = NULL;
972 
973         if (protocol < 0x200) {
974             fprintf(stderr, "qemu: linux kernel too old to load a ram disk\n");
975             exit(1);
976         }
977 
978         mapped_file = g_mapped_file_new(initrd_filename, false, &gerr);
979         if (!mapped_file) {
980             fprintf(stderr, "qemu: error reading initrd %s: %s\n",
981                     initrd_filename, gerr->message);
982             exit(1);
983         }
984         x86ms->initrd_mapped_file = mapped_file;
985 
986         initrd_data = g_mapped_file_get_contents(mapped_file);
987         initrd_size = g_mapped_file_get_length(mapped_file);
988         if (initrd_size >= initrd_max) {
989             fprintf(stderr, "qemu: initrd is too large, cannot support."
990                     "(max: %"PRIu32", need %"PRId64")\n",
991                     initrd_max, (uint64_t)initrd_size);
992             exit(1);
993         }
994 
995         initrd_addr = (initrd_max - initrd_size) & ~4095;
996 
997         fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_ADDR, initrd_addr);
998         fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_SIZE, initrd_size);
999         fw_cfg_add_bytes(fw_cfg, FW_CFG_INITRD_DATA, initrd_data, initrd_size);
1000         sev_load_ctx.initrd_data = initrd_data;
1001         sev_load_ctx.initrd_size = initrd_size;
1002 
1003         stl_p(header + 0x218, initrd_addr);
1004         stl_p(header + 0x21c, initrd_size);
1005     }
1006 
1007     /* load kernel and setup */
1008     setup_size = header[0x1f1];
1009     if (setup_size == 0) {
1010         setup_size = 4;
1011     }
1012     setup_size = (setup_size + 1) * 512;
1013     if (setup_size > kernel_size) {
1014         fprintf(stderr, "qemu: invalid kernel header\n");
1015         exit(1);
1016     }
1017     kernel_size -= setup_size;
1018 
1019     setup  = g_malloc(setup_size);
1020     kernel = g_malloc(kernel_size);
1021     fseek(f, 0, SEEK_SET);
1022     if (fread(setup, 1, setup_size, f) != setup_size) {
1023         fprintf(stderr, "fread() failed\n");
1024         exit(1);
1025     }
1026     if (fread(kernel, 1, kernel_size, f) != kernel_size) {
1027         fprintf(stderr, "fread() failed\n");
1028         exit(1);
1029     }
1030     fclose(f);
1031 
1032     /* append dtb to kernel */
1033     if (dtb_filename) {
1034         if (protocol < 0x209) {
1035             fprintf(stderr, "qemu: Linux kernel too old to load a dtb\n");
1036             exit(1);
1037         }
1038 
1039         dtb_size = get_image_size(dtb_filename);
1040         if (dtb_size <= 0) {
1041             fprintf(stderr, "qemu: error reading dtb %s: %s\n",
1042                     dtb_filename, strerror(errno));
1043             exit(1);
1044         }
1045 
1046         setup_data_offset = QEMU_ALIGN_UP(kernel_size, 16);
1047         kernel_size = setup_data_offset + sizeof(struct setup_data) + dtb_size;
1048         kernel = g_realloc(kernel, kernel_size);
1049 
1050         stq_p(header + 0x250, prot_addr + setup_data_offset);
1051 
1052         setup_data = (struct setup_data *)(kernel + setup_data_offset);
1053         setup_data->next = 0;
1054         setup_data->type = cpu_to_le32(SETUP_DTB);
1055         setup_data->len = cpu_to_le32(dtb_size);
1056 
1057         load_image_size(dtb_filename, setup_data->data, dtb_size);
1058     }
1059 
1060     /*
1061      * If we're starting an encrypted VM, it will be OVMF based, which uses the
1062      * efi stub for booting and doesn't require any values to be placed in the
1063      * kernel header.  We therefore don't update the header so the hash of the
1064      * kernel on the other side of the fw_cfg interface matches the hash of the
1065      * file the user passed in.
1066      */
1067     if (!sev_enabled()) {
1068         memcpy(setup, header, MIN(sizeof(header), setup_size));
1069     }
1070 
1071     fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_ADDR, prot_addr);
1072     fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_SIZE, kernel_size);
1073     fw_cfg_add_bytes(fw_cfg, FW_CFG_KERNEL_DATA, kernel, kernel_size);
1074     sev_load_ctx.kernel_data = (char *)kernel;
1075     sev_load_ctx.kernel_size = kernel_size;
1076 
1077     fw_cfg_add_i32(fw_cfg, FW_CFG_SETUP_ADDR, real_addr);
1078     fw_cfg_add_i32(fw_cfg, FW_CFG_SETUP_SIZE, setup_size);
1079     fw_cfg_add_bytes(fw_cfg, FW_CFG_SETUP_DATA, setup, setup_size);
1080     sev_load_ctx.setup_data = (char *)setup;
1081     sev_load_ctx.setup_size = setup_size;
1082 
1083     if (sev_enabled()) {
1084         sev_add_kernel_loader_hashes(&sev_load_ctx, &error_fatal);
1085     }
1086 
1087     option_rom[nb_option_roms].bootindex = 0;
1088     option_rom[nb_option_roms].name = "linuxboot.bin";
1089     if (linuxboot_dma_enabled && fw_cfg_dma_enabled(fw_cfg)) {
1090         option_rom[nb_option_roms].name = "linuxboot_dma.bin";
1091     }
1092     nb_option_roms++;
1093 }
1094 
1095 void x86_bios_rom_init(MachineState *ms, const char *default_firmware,
1096                        MemoryRegion *rom_memory, bool isapc_ram_fw)
1097 {
1098     const char *bios_name;
1099     char *filename;
1100     MemoryRegion *bios, *isa_bios;
1101     int bios_size, isa_bios_size;
1102     int ret;
1103 
1104     /* BIOS load */
1105     bios_name = ms->firmware ?: default_firmware;
1106     filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
1107     if (filename) {
1108         bios_size = get_image_size(filename);
1109     } else {
1110         bios_size = -1;
1111     }
1112     if (bios_size <= 0 ||
1113         (bios_size % 65536) != 0) {
1114         goto bios_error;
1115     }
1116     bios = g_malloc(sizeof(*bios));
1117     memory_region_init_ram(bios, NULL, "pc.bios", bios_size, &error_fatal);
1118     if (sev_enabled()) {
1119         /*
1120          * The concept of a "reset" simply doesn't exist for
1121          * confidential computing guests, we have to destroy and
1122          * re-launch them instead.  So there is no need to register
1123          * the firmware as rom to properly re-initialize on reset.
1124          * Just go for a straight file load instead.
1125          */
1126         void *ptr = memory_region_get_ram_ptr(bios);
1127         load_image_size(filename, ptr, bios_size);
1128         x86_firmware_configure(ptr, bios_size);
1129     } else {
1130         if (!isapc_ram_fw) {
1131             memory_region_set_readonly(bios, true);
1132         }
1133         ret = rom_add_file_fixed(bios_name, (uint32_t)(-bios_size), -1);
1134         if (ret != 0) {
1135             goto bios_error;
1136         }
1137     }
1138     g_free(filename);
1139 
1140     /* map the last 128KB of the BIOS in ISA space */
1141     isa_bios_size = MIN(bios_size, 128 * KiB);
1142     isa_bios = g_malloc(sizeof(*isa_bios));
1143     memory_region_init_alias(isa_bios, NULL, "isa-bios", bios,
1144                              bios_size - isa_bios_size, isa_bios_size);
1145     memory_region_add_subregion_overlap(rom_memory,
1146                                         0x100000 - isa_bios_size,
1147                                         isa_bios,
1148                                         1);
1149     if (!isapc_ram_fw) {
1150         memory_region_set_readonly(isa_bios, true);
1151     }
1152 
1153     /* map all the bios at the top of memory */
1154     memory_region_add_subregion(rom_memory,
1155                                 (uint32_t)(-bios_size),
1156                                 bios);
1157     return;
1158 
1159 bios_error:
1160     fprintf(stderr, "qemu: could not load PC BIOS '%s'\n", bios_name);
1161     exit(1);
1162 }
1163 
1164 bool x86_machine_is_smm_enabled(const X86MachineState *x86ms)
1165 {
1166     bool smm_available = false;
1167 
1168     if (x86ms->smm == ON_OFF_AUTO_OFF) {
1169         return false;
1170     }
1171 
1172     if (tcg_enabled() || qtest_enabled()) {
1173         smm_available = true;
1174     } else if (kvm_enabled()) {
1175         smm_available = kvm_has_smm();
1176     }
1177 
1178     if (smm_available) {
1179         return true;
1180     }
1181 
1182     if (x86ms->smm == ON_OFF_AUTO_ON) {
1183         error_report("System Management Mode not supported by this hypervisor.");
1184         exit(1);
1185     }
1186     return false;
1187 }
1188 
1189 static void x86_machine_get_smm(Object *obj, Visitor *v, const char *name,
1190                                void *opaque, Error **errp)
1191 {
1192     X86MachineState *x86ms = X86_MACHINE(obj);
1193     OnOffAuto smm = x86ms->smm;
1194 
1195     visit_type_OnOffAuto(v, name, &smm, errp);
1196 }
1197 
1198 static void x86_machine_set_smm(Object *obj, Visitor *v, const char *name,
1199                                void *opaque, Error **errp)
1200 {
1201     X86MachineState *x86ms = X86_MACHINE(obj);
1202 
1203     visit_type_OnOffAuto(v, name, &x86ms->smm, errp);
1204 }
1205 
1206 bool x86_machine_is_acpi_enabled(const X86MachineState *x86ms)
1207 {
1208     if (x86ms->acpi == ON_OFF_AUTO_OFF) {
1209         return false;
1210     }
1211     return true;
1212 }
1213 
1214 static void x86_machine_get_acpi(Object *obj, Visitor *v, const char *name,
1215                                  void *opaque, Error **errp)
1216 {
1217     X86MachineState *x86ms = X86_MACHINE(obj);
1218     OnOffAuto acpi = x86ms->acpi;
1219 
1220     visit_type_OnOffAuto(v, name, &acpi, errp);
1221 }
1222 
1223 static void x86_machine_set_acpi(Object *obj, Visitor *v, const char *name,
1224                                  void *opaque, Error **errp)
1225 {
1226     X86MachineState *x86ms = X86_MACHINE(obj);
1227 
1228     visit_type_OnOffAuto(v, name, &x86ms->acpi, errp);
1229 }
1230 
1231 static char *x86_machine_get_oem_id(Object *obj, Error **errp)
1232 {
1233     X86MachineState *x86ms = X86_MACHINE(obj);
1234 
1235     return g_strdup(x86ms->oem_id);
1236 }
1237 
1238 static void x86_machine_set_oem_id(Object *obj, const char *value, Error **errp)
1239 {
1240     X86MachineState *x86ms = X86_MACHINE(obj);
1241     size_t len = strlen(value);
1242 
1243     if (len > 6) {
1244         error_setg(errp,
1245                    "User specified "X86_MACHINE_OEM_ID" value is bigger than "
1246                    "6 bytes in size");
1247         return;
1248     }
1249 
1250     strncpy(x86ms->oem_id, value, 6);
1251 }
1252 
1253 static char *x86_machine_get_oem_table_id(Object *obj, Error **errp)
1254 {
1255     X86MachineState *x86ms = X86_MACHINE(obj);
1256 
1257     return g_strdup(x86ms->oem_table_id);
1258 }
1259 
1260 static void x86_machine_set_oem_table_id(Object *obj, const char *value,
1261                                          Error **errp)
1262 {
1263     X86MachineState *x86ms = X86_MACHINE(obj);
1264     size_t len = strlen(value);
1265 
1266     if (len > 8) {
1267         error_setg(errp,
1268                    "User specified "X86_MACHINE_OEM_TABLE_ID
1269                    " value is bigger than "
1270                    "8 bytes in size");
1271         return;
1272     }
1273     strncpy(x86ms->oem_table_id, value, 8);
1274 }
1275 
1276 static void x86_machine_get_bus_lock_ratelimit(Object *obj, Visitor *v,
1277                                 const char *name, void *opaque, Error **errp)
1278 {
1279     X86MachineState *x86ms = X86_MACHINE(obj);
1280     uint64_t bus_lock_ratelimit = x86ms->bus_lock_ratelimit;
1281 
1282     visit_type_uint64(v, name, &bus_lock_ratelimit, errp);
1283 }
1284 
1285 static void x86_machine_set_bus_lock_ratelimit(Object *obj, Visitor *v,
1286                                const char *name, void *opaque, Error **errp)
1287 {
1288     X86MachineState *x86ms = X86_MACHINE(obj);
1289 
1290     visit_type_uint64(v, name, &x86ms->bus_lock_ratelimit, errp);
1291 }
1292 
1293 static void machine_get_sgx_epc(Object *obj, Visitor *v, const char *name,
1294                                 void *opaque, Error **errp)
1295 {
1296     X86MachineState *x86ms = X86_MACHINE(obj);
1297     SgxEPCList *list = x86ms->sgx_epc_list;
1298 
1299     visit_type_SgxEPCList(v, name, &list, errp);
1300 }
1301 
1302 static void machine_set_sgx_epc(Object *obj, Visitor *v, const char *name,
1303                                 void *opaque, Error **errp)
1304 {
1305     X86MachineState *x86ms = X86_MACHINE(obj);
1306     SgxEPCList *list;
1307 
1308     list = x86ms->sgx_epc_list;
1309     visit_type_SgxEPCList(v, name, &x86ms->sgx_epc_list, errp);
1310 
1311     qapi_free_SgxEPCList(list);
1312 }
1313 
1314 static void x86_machine_initfn(Object *obj)
1315 {
1316     X86MachineState *x86ms = X86_MACHINE(obj);
1317 
1318     x86ms->smm = ON_OFF_AUTO_AUTO;
1319     x86ms->acpi = ON_OFF_AUTO_AUTO;
1320     x86ms->pci_irq_mask = ACPI_BUILD_PCI_IRQS;
1321     x86ms->oem_id = g_strndup(ACPI_BUILD_APPNAME6, 6);
1322     x86ms->oem_table_id = g_strndup(ACPI_BUILD_APPNAME8, 8);
1323     x86ms->bus_lock_ratelimit = 0;
1324 }
1325 
1326 static void x86_machine_class_init(ObjectClass *oc, void *data)
1327 {
1328     MachineClass *mc = MACHINE_CLASS(oc);
1329     X86MachineClass *x86mc = X86_MACHINE_CLASS(oc);
1330     NMIClass *nc = NMI_CLASS(oc);
1331 
1332     mc->cpu_index_to_instance_props = x86_cpu_index_to_props;
1333     mc->get_default_cpu_node_id = x86_get_default_cpu_node_id;
1334     mc->possible_cpu_arch_ids = x86_possible_cpu_arch_ids;
1335     x86mc->save_tsc_khz = true;
1336     x86mc->fwcfg_dma_enabled = true;
1337     nc->nmi_monitor_handler = x86_nmi;
1338 
1339     object_class_property_add(oc, X86_MACHINE_SMM, "OnOffAuto",
1340         x86_machine_get_smm, x86_machine_set_smm,
1341         NULL, NULL);
1342     object_class_property_set_description(oc, X86_MACHINE_SMM,
1343         "Enable SMM");
1344 
1345     object_class_property_add(oc, X86_MACHINE_ACPI, "OnOffAuto",
1346         x86_machine_get_acpi, x86_machine_set_acpi,
1347         NULL, NULL);
1348     object_class_property_set_description(oc, X86_MACHINE_ACPI,
1349         "Enable ACPI");
1350 
1351     object_class_property_add_str(oc, X86_MACHINE_OEM_ID,
1352                                   x86_machine_get_oem_id,
1353                                   x86_machine_set_oem_id);
1354     object_class_property_set_description(oc, X86_MACHINE_OEM_ID,
1355                                           "Override the default value of field OEMID "
1356                                           "in ACPI table header."
1357                                           "The string may be up to 6 bytes in size");
1358 
1359 
1360     object_class_property_add_str(oc, X86_MACHINE_OEM_TABLE_ID,
1361                                   x86_machine_get_oem_table_id,
1362                                   x86_machine_set_oem_table_id);
1363     object_class_property_set_description(oc, X86_MACHINE_OEM_TABLE_ID,
1364                                           "Override the default value of field OEM Table ID "
1365                                           "in ACPI table header."
1366                                           "The string may be up to 8 bytes in size");
1367 
1368     object_class_property_add(oc, X86_MACHINE_BUS_LOCK_RATELIMIT, "uint64_t",
1369                                 x86_machine_get_bus_lock_ratelimit,
1370                                 x86_machine_set_bus_lock_ratelimit, NULL, NULL);
1371     object_class_property_set_description(oc, X86_MACHINE_BUS_LOCK_RATELIMIT,
1372             "Set the ratelimit for the bus locks acquired in VMs");
1373 
1374     object_class_property_add(oc, "sgx-epc", "SgxEPC",
1375         machine_get_sgx_epc, machine_set_sgx_epc,
1376         NULL, NULL);
1377     object_class_property_set_description(oc, "sgx-epc",
1378         "SGX EPC device");
1379 }
1380 
1381 static const TypeInfo x86_machine_info = {
1382     .name = TYPE_X86_MACHINE,
1383     .parent = TYPE_MACHINE,
1384     .abstract = true,
1385     .instance_size = sizeof(X86MachineState),
1386     .instance_init = x86_machine_initfn,
1387     .class_size = sizeof(X86MachineClass),
1388     .class_init = x86_machine_class_init,
1389     .interfaces = (InterfaceInfo[]) {
1390          { TYPE_NMI },
1391          { }
1392     },
1393 };
1394 
1395 static void x86_machine_register_types(void)
1396 {
1397     type_register_static(&x86_machine_info);
1398 }
1399 
1400 type_init(x86_machine_register_types)
1401