xref: /openbmc/qemu/hw/i386/pc.c (revision e4ec5ad4)
1 /*
2  * QEMU PC System Emulator
3  *
4  * Copyright (c) 2003-2004 Fabrice Bellard
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24 
25 #include "qemu/osdep.h"
26 #include "qemu/units.h"
27 #include "hw/i386/pc.h"
28 #include "hw/char/serial.h"
29 #include "hw/char/parallel.h"
30 #include "hw/i386/apic.h"
31 #include "hw/i386/topology.h"
32 #include "hw/i386/fw_cfg.h"
33 #include "sysemu/cpus.h"
34 #include "hw/block/fdc.h"
35 #include "hw/ide.h"
36 #include "hw/pci/pci.h"
37 #include "hw/pci/pci_bus.h"
38 #include "hw/nvram/fw_cfg.h"
39 #include "hw/timer/hpet.h"
40 #include "hw/firmware/smbios.h"
41 #include "hw/loader.h"
42 #include "elf.h"
43 #include "migration/vmstate.h"
44 #include "multiboot.h"
45 #include "hw/timer/mc146818rtc.h"
46 #include "hw/dma/i8257.h"
47 #include "hw/timer/i8254.h"
48 #include "hw/input/i8042.h"
49 #include "hw/irq.h"
50 #include "hw/audio/pcspk.h"
51 #include "hw/pci/msi.h"
52 #include "hw/sysbus.h"
53 #include "sysemu/sysemu.h"
54 #include "sysemu/tcg.h"
55 #include "sysemu/numa.h"
56 #include "sysemu/kvm.h"
57 #include "sysemu/qtest.h"
58 #include "sysemu/reset.h"
59 #include "sysemu/runstate.h"
60 #include "kvm_i386.h"
61 #include "hw/xen/xen.h"
62 #include "hw/xen/start_info.h"
63 #include "ui/qemu-spice.h"
64 #include "exec/memory.h"
65 #include "exec/address-spaces.h"
66 #include "sysemu/arch_init.h"
67 #include "qemu/bitmap.h"
68 #include "qemu/config-file.h"
69 #include "qemu/error-report.h"
70 #include "qemu/option.h"
71 #include "hw/acpi/acpi.h"
72 #include "hw/acpi/cpu_hotplug.h"
73 #include "hw/boards.h"
74 #include "acpi-build.h"
75 #include "hw/mem/pc-dimm.h"
76 #include "qapi/error.h"
77 #include "qapi/qapi-visit-common.h"
78 #include "qapi/visitor.h"
79 #include "hw/core/cpu.h"
80 #include "hw/nmi.h"
81 #include "hw/usb.h"
82 #include "hw/i386/intel_iommu.h"
83 #include "hw/net/ne2000-isa.h"
84 #include "standard-headers/asm-x86/bootparam.h"
85 #include "hw/virtio/virtio-pmem-pci.h"
86 #include "hw/mem/memory-device.h"
87 #include "sysemu/replay.h"
88 #include "qapi/qmp/qerror.h"
89 #include "config-devices.h"
90 #include "e820_memory_layout.h"
91 #include "fw_cfg.h"
92 
93 /* debug PC/ISA interrupts */
94 //#define DEBUG_IRQ
95 
96 #ifdef DEBUG_IRQ
97 #define DPRINTF(fmt, ...)                                       \
98     do { printf("CPUIRQ: " fmt , ## __VA_ARGS__); } while (0)
99 #else
100 #define DPRINTF(fmt, ...)
101 #endif
102 
103 struct hpet_fw_config hpet_cfg = {.count = UINT8_MAX};
104 
105 /* Physical Address of PVH entry point read from kernel ELF NOTE */
106 static size_t pvh_start_addr;
107 
108 GlobalProperty pc_compat_4_1[] = {};
109 const size_t pc_compat_4_1_len = G_N_ELEMENTS(pc_compat_4_1);
110 
111 GlobalProperty pc_compat_4_0[] = {};
112 const size_t pc_compat_4_0_len = G_N_ELEMENTS(pc_compat_4_0);
113 
114 GlobalProperty pc_compat_3_1[] = {
115     { "intel-iommu", "dma-drain", "off" },
116     { "Opteron_G3" "-" TYPE_X86_CPU, "rdtscp", "off" },
117     { "Opteron_G4" "-" TYPE_X86_CPU, "rdtscp", "off" },
118     { "Opteron_G4" "-" TYPE_X86_CPU, "npt", "off" },
119     { "Opteron_G4" "-" TYPE_X86_CPU, "nrip-save", "off" },
120     { "Opteron_G5" "-" TYPE_X86_CPU, "rdtscp", "off" },
121     { "Opteron_G5" "-" TYPE_X86_CPU, "npt", "off" },
122     { "Opteron_G5" "-" TYPE_X86_CPU, "nrip-save", "off" },
123     { "EPYC" "-" TYPE_X86_CPU, "npt", "off" },
124     { "EPYC" "-" TYPE_X86_CPU, "nrip-save", "off" },
125     { "EPYC-IBPB" "-" TYPE_X86_CPU, "npt", "off" },
126     { "EPYC-IBPB" "-" TYPE_X86_CPU, "nrip-save", "off" },
127     { "Skylake-Client" "-" TYPE_X86_CPU,      "mpx", "on" },
128     { "Skylake-Client-IBRS" "-" TYPE_X86_CPU, "mpx", "on" },
129     { "Skylake-Server" "-" TYPE_X86_CPU,      "mpx", "on" },
130     { "Skylake-Server-IBRS" "-" TYPE_X86_CPU, "mpx", "on" },
131     { "Cascadelake-Server" "-" TYPE_X86_CPU,  "mpx", "on" },
132     { "Icelake-Client" "-" TYPE_X86_CPU,      "mpx", "on" },
133     { "Icelake-Server" "-" TYPE_X86_CPU,      "mpx", "on" },
134     { "Cascadelake-Server" "-" TYPE_X86_CPU, "stepping", "5" },
135     { TYPE_X86_CPU, "x-intel-pt-auto-level", "off" },
136 };
137 const size_t pc_compat_3_1_len = G_N_ELEMENTS(pc_compat_3_1);
138 
139 GlobalProperty pc_compat_3_0[] = {
140     { TYPE_X86_CPU, "x-hv-synic-kvm-only", "on" },
141     { "Skylake-Server" "-" TYPE_X86_CPU, "pku", "off" },
142     { "Skylake-Server-IBRS" "-" TYPE_X86_CPU, "pku", "off" },
143 };
144 const size_t pc_compat_3_0_len = G_N_ELEMENTS(pc_compat_3_0);
145 
146 GlobalProperty pc_compat_2_12[] = {
147     { TYPE_X86_CPU, "legacy-cache", "on" },
148     { TYPE_X86_CPU, "topoext", "off" },
149     { "EPYC-" TYPE_X86_CPU, "xlevel", "0x8000000a" },
150     { "EPYC-IBPB-" TYPE_X86_CPU, "xlevel", "0x8000000a" },
151 };
152 const size_t pc_compat_2_12_len = G_N_ELEMENTS(pc_compat_2_12);
153 
154 GlobalProperty pc_compat_2_11[] = {
155     { TYPE_X86_CPU, "x-migrate-smi-count", "off" },
156     { "Skylake-Server" "-" TYPE_X86_CPU, "clflushopt", "off" },
157 };
158 const size_t pc_compat_2_11_len = G_N_ELEMENTS(pc_compat_2_11);
159 
160 GlobalProperty pc_compat_2_10[] = {
161     { TYPE_X86_CPU, "x-hv-max-vps", "0x40" },
162     { "i440FX-pcihost", "x-pci-hole64-fix", "off" },
163     { "q35-pcihost", "x-pci-hole64-fix", "off" },
164 };
165 const size_t pc_compat_2_10_len = G_N_ELEMENTS(pc_compat_2_10);
166 
167 GlobalProperty pc_compat_2_9[] = {
168     { "mch", "extended-tseg-mbytes", "0" },
169 };
170 const size_t pc_compat_2_9_len = G_N_ELEMENTS(pc_compat_2_9);
171 
172 GlobalProperty pc_compat_2_8[] = {
173     { TYPE_X86_CPU, "tcg-cpuid", "off" },
174     { "kvmclock", "x-mach-use-reliable-get-clock", "off" },
175     { "ICH9-LPC", "x-smi-broadcast", "off" },
176     { TYPE_X86_CPU, "vmware-cpuid-freq", "off" },
177     { "Haswell-" TYPE_X86_CPU, "stepping", "1" },
178 };
179 const size_t pc_compat_2_8_len = G_N_ELEMENTS(pc_compat_2_8);
180 
181 GlobalProperty pc_compat_2_7[] = {
182     { TYPE_X86_CPU, "l3-cache", "off" },
183     { TYPE_X86_CPU, "full-cpuid-auto-level", "off" },
184     { "Opteron_G3" "-" TYPE_X86_CPU, "family", "15" },
185     { "Opteron_G3" "-" TYPE_X86_CPU, "model", "6" },
186     { "Opteron_G3" "-" TYPE_X86_CPU, "stepping", "1" },
187     { "isa-pcspk", "migrate", "off" },
188 };
189 const size_t pc_compat_2_7_len = G_N_ELEMENTS(pc_compat_2_7);
190 
191 GlobalProperty pc_compat_2_6[] = {
192     { TYPE_X86_CPU, "cpuid-0xb", "off" },
193     { "vmxnet3", "romfile", "" },
194     { TYPE_X86_CPU, "fill-mtrr-mask", "off" },
195     { "apic-common", "legacy-instance-id", "on", }
196 };
197 const size_t pc_compat_2_6_len = G_N_ELEMENTS(pc_compat_2_6);
198 
199 GlobalProperty pc_compat_2_5[] = {};
200 const size_t pc_compat_2_5_len = G_N_ELEMENTS(pc_compat_2_5);
201 
202 GlobalProperty pc_compat_2_4[] = {
203     PC_CPU_MODEL_IDS("2.4.0")
204     { "Haswell-" TYPE_X86_CPU, "abm", "off" },
205     { "Haswell-noTSX-" TYPE_X86_CPU, "abm", "off" },
206     { "Broadwell-" TYPE_X86_CPU, "abm", "off" },
207     { "Broadwell-noTSX-" TYPE_X86_CPU, "abm", "off" },
208     { "host" "-" TYPE_X86_CPU, "host-cache-info", "on" },
209     { TYPE_X86_CPU, "check", "off" },
210     { "qemu64" "-" TYPE_X86_CPU, "sse4a", "on" },
211     { "qemu64" "-" TYPE_X86_CPU, "abm", "on" },
212     { "qemu64" "-" TYPE_X86_CPU, "popcnt", "on" },
213     { "qemu32" "-" TYPE_X86_CPU, "popcnt", "on" },
214     { "Opteron_G2" "-" TYPE_X86_CPU, "rdtscp", "on" },
215     { "Opteron_G3" "-" TYPE_X86_CPU, "rdtscp", "on" },
216     { "Opteron_G4" "-" TYPE_X86_CPU, "rdtscp", "on" },
217     { "Opteron_G5" "-" TYPE_X86_CPU, "rdtscp", "on", }
218 };
219 const size_t pc_compat_2_4_len = G_N_ELEMENTS(pc_compat_2_4);
220 
221 GlobalProperty pc_compat_2_3[] = {
222     PC_CPU_MODEL_IDS("2.3.0")
223     { TYPE_X86_CPU, "arat", "off" },
224     { "qemu64" "-" TYPE_X86_CPU, "min-level", "4" },
225     { "kvm64" "-" TYPE_X86_CPU, "min-level", "5" },
226     { "pentium3" "-" TYPE_X86_CPU, "min-level", "2" },
227     { "n270" "-" TYPE_X86_CPU, "min-level", "5" },
228     { "Conroe" "-" TYPE_X86_CPU, "min-level", "4" },
229     { "Penryn" "-" TYPE_X86_CPU, "min-level", "4" },
230     { "Nehalem" "-" TYPE_X86_CPU, "min-level", "4" },
231     { "n270" "-" TYPE_X86_CPU, "min-xlevel", "0x8000000a" },
232     { "Penryn" "-" TYPE_X86_CPU, "min-xlevel", "0x8000000a" },
233     { "Conroe" "-" TYPE_X86_CPU, "min-xlevel", "0x8000000a" },
234     { "Nehalem" "-" TYPE_X86_CPU, "min-xlevel", "0x8000000a" },
235     { "Westmere" "-" TYPE_X86_CPU, "min-xlevel", "0x8000000a" },
236     { "SandyBridge" "-" TYPE_X86_CPU, "min-xlevel", "0x8000000a" },
237     { "IvyBridge" "-" TYPE_X86_CPU, "min-xlevel", "0x8000000a" },
238     { "Haswell" "-" TYPE_X86_CPU, "min-xlevel", "0x8000000a" },
239     { "Haswell-noTSX" "-" TYPE_X86_CPU, "min-xlevel", "0x8000000a" },
240     { "Broadwell" "-" TYPE_X86_CPU, "min-xlevel", "0x8000000a" },
241     { "Broadwell-noTSX" "-" TYPE_X86_CPU, "min-xlevel", "0x8000000a" },
242     { TYPE_X86_CPU, "kvm-no-smi-migration", "on" },
243 };
244 const size_t pc_compat_2_3_len = G_N_ELEMENTS(pc_compat_2_3);
245 
246 GlobalProperty pc_compat_2_2[] = {
247     PC_CPU_MODEL_IDS("2.2.0")
248     { "kvm64" "-" TYPE_X86_CPU, "vme", "off" },
249     { "kvm32" "-" TYPE_X86_CPU, "vme", "off" },
250     { "Conroe" "-" TYPE_X86_CPU, "vme", "off" },
251     { "Penryn" "-" TYPE_X86_CPU, "vme", "off" },
252     { "Nehalem" "-" TYPE_X86_CPU, "vme", "off" },
253     { "Westmere" "-" TYPE_X86_CPU, "vme", "off" },
254     { "SandyBridge" "-" TYPE_X86_CPU, "vme", "off" },
255     { "Haswell" "-" TYPE_X86_CPU, "vme", "off" },
256     { "Broadwell" "-" TYPE_X86_CPU, "vme", "off" },
257     { "Opteron_G1" "-" TYPE_X86_CPU, "vme", "off" },
258     { "Opteron_G2" "-" TYPE_X86_CPU, "vme", "off" },
259     { "Opteron_G3" "-" TYPE_X86_CPU, "vme", "off" },
260     { "Opteron_G4" "-" TYPE_X86_CPU, "vme", "off" },
261     { "Opteron_G5" "-" TYPE_X86_CPU, "vme", "off" },
262     { "Haswell" "-" TYPE_X86_CPU, "f16c", "off" },
263     { "Haswell" "-" TYPE_X86_CPU, "rdrand", "off" },
264     { "Broadwell" "-" TYPE_X86_CPU, "f16c", "off" },
265     { "Broadwell" "-" TYPE_X86_CPU, "rdrand", "off" },
266 };
267 const size_t pc_compat_2_2_len = G_N_ELEMENTS(pc_compat_2_2);
268 
269 GlobalProperty pc_compat_2_1[] = {
270     PC_CPU_MODEL_IDS("2.1.0")
271     { "coreduo" "-" TYPE_X86_CPU, "vmx", "on" },
272     { "core2duo" "-" TYPE_X86_CPU, "vmx", "on" },
273 };
274 const size_t pc_compat_2_1_len = G_N_ELEMENTS(pc_compat_2_1);
275 
276 GlobalProperty pc_compat_2_0[] = {
277     PC_CPU_MODEL_IDS("2.0.0")
278     { "virtio-scsi-pci", "any_layout", "off" },
279     { "PIIX4_PM", "memory-hotplug-support", "off" },
280     { "apic", "version", "0x11" },
281     { "nec-usb-xhci", "superspeed-ports-first", "off" },
282     { "nec-usb-xhci", "force-pcie-endcap", "on" },
283     { "pci-serial", "prog_if", "0" },
284     { "pci-serial-2x", "prog_if", "0" },
285     { "pci-serial-4x", "prog_if", "0" },
286     { "virtio-net-pci", "guest_announce", "off" },
287     { "ICH9-LPC", "memory-hotplug-support", "off" },
288     { "xio3130-downstream", COMPAT_PROP_PCP, "off" },
289     { "ioh3420", COMPAT_PROP_PCP, "off" },
290 };
291 const size_t pc_compat_2_0_len = G_N_ELEMENTS(pc_compat_2_0);
292 
293 GlobalProperty pc_compat_1_7[] = {
294     PC_CPU_MODEL_IDS("1.7.0")
295     { TYPE_USB_DEVICE, "msos-desc", "no" },
296     { "PIIX4_PM", "acpi-pci-hotplug-with-bridge-support", "off" },
297     { "hpet", HPET_INTCAP, "4" },
298 };
299 const size_t pc_compat_1_7_len = G_N_ELEMENTS(pc_compat_1_7);
300 
301 GlobalProperty pc_compat_1_6[] = {
302     PC_CPU_MODEL_IDS("1.6.0")
303     { "e1000", "mitigation", "off" },
304     { "qemu64-" TYPE_X86_CPU, "model", "2" },
305     { "qemu32-" TYPE_X86_CPU, "model", "3" },
306     { "i440FX-pcihost", "short_root_bus", "1" },
307     { "q35-pcihost", "short_root_bus", "1" },
308 };
309 const size_t pc_compat_1_6_len = G_N_ELEMENTS(pc_compat_1_6);
310 
311 GlobalProperty pc_compat_1_5[] = {
312     PC_CPU_MODEL_IDS("1.5.0")
313     { "Conroe-" TYPE_X86_CPU, "model", "2" },
314     { "Conroe-" TYPE_X86_CPU, "min-level", "2" },
315     { "Penryn-" TYPE_X86_CPU, "model", "2" },
316     { "Penryn-" TYPE_X86_CPU, "min-level", "2" },
317     { "Nehalem-" TYPE_X86_CPU, "model", "2" },
318     { "Nehalem-" TYPE_X86_CPU, "min-level", "2" },
319     { "virtio-net-pci", "any_layout", "off" },
320     { TYPE_X86_CPU, "pmu", "on" },
321     { "i440FX-pcihost", "short_root_bus", "0" },
322     { "q35-pcihost", "short_root_bus", "0" },
323 };
324 const size_t pc_compat_1_5_len = G_N_ELEMENTS(pc_compat_1_5);
325 
326 GlobalProperty pc_compat_1_4[] = {
327     PC_CPU_MODEL_IDS("1.4.0")
328     { "scsi-hd", "discard_granularity", "0" },
329     { "scsi-cd", "discard_granularity", "0" },
330     { "scsi-disk", "discard_granularity", "0" },
331     { "ide-hd", "discard_granularity", "0" },
332     { "ide-cd", "discard_granularity", "0" },
333     { "ide-drive", "discard_granularity", "0" },
334     { "virtio-blk-pci", "discard_granularity", "0" },
335     /* DEV_NVECTORS_UNSPECIFIED as a uint32_t string: */
336     { "virtio-serial-pci", "vectors", "0xFFFFFFFF" },
337     { "virtio-net-pci", "ctrl_guest_offloads", "off" },
338     { "e1000", "romfile", "pxe-e1000.rom" },
339     { "ne2k_pci", "romfile", "pxe-ne2k_pci.rom" },
340     { "pcnet", "romfile", "pxe-pcnet.rom" },
341     { "rtl8139", "romfile", "pxe-rtl8139.rom" },
342     { "virtio-net-pci", "romfile", "pxe-virtio.rom" },
343     { "486-" TYPE_X86_CPU, "model", "0" },
344     { "n270" "-" TYPE_X86_CPU, "movbe", "off" },
345     { "Westmere" "-" TYPE_X86_CPU, "pclmulqdq", "off" },
346 };
347 const size_t pc_compat_1_4_len = G_N_ELEMENTS(pc_compat_1_4);
348 
349 void gsi_handler(void *opaque, int n, int level)
350 {
351     GSIState *s = opaque;
352 
353     DPRINTF("pc: %s GSI %d\n", level ? "raising" : "lowering", n);
354     if (n < ISA_NUM_IRQS) {
355         qemu_set_irq(s->i8259_irq[n], level);
356     }
357     qemu_set_irq(s->ioapic_irq[n], level);
358 }
359 
360 static void ioport80_write(void *opaque, hwaddr addr, uint64_t data,
361                            unsigned size)
362 {
363 }
364 
365 static uint64_t ioport80_read(void *opaque, hwaddr addr, unsigned size)
366 {
367     return 0xffffffffffffffffULL;
368 }
369 
370 /* MSDOS compatibility mode FPU exception support */
371 static qemu_irq ferr_irq;
372 
373 void pc_register_ferr_irq(qemu_irq irq)
374 {
375     ferr_irq = irq;
376 }
377 
378 /* XXX: add IGNNE support */
379 void cpu_set_ferr(CPUX86State *s)
380 {
381     qemu_irq_raise(ferr_irq);
382 }
383 
384 static void ioportF0_write(void *opaque, hwaddr addr, uint64_t data,
385                            unsigned size)
386 {
387     qemu_irq_lower(ferr_irq);
388 }
389 
390 static uint64_t ioportF0_read(void *opaque, hwaddr addr, unsigned size)
391 {
392     return 0xffffffffffffffffULL;
393 }
394 
395 /* TSC handling */
396 uint64_t cpu_get_tsc(CPUX86State *env)
397 {
398     return cpu_get_ticks();
399 }
400 
401 /* IRQ handling */
402 int cpu_get_pic_interrupt(CPUX86State *env)
403 {
404     X86CPU *cpu = env_archcpu(env);
405     int intno;
406 
407     if (!kvm_irqchip_in_kernel()) {
408         intno = apic_get_interrupt(cpu->apic_state);
409         if (intno >= 0) {
410             return intno;
411         }
412         /* read the irq from the PIC */
413         if (!apic_accept_pic_intr(cpu->apic_state)) {
414             return -1;
415         }
416     }
417 
418     intno = pic_read_irq(isa_pic);
419     return intno;
420 }
421 
422 static void pic_irq_request(void *opaque, int irq, int level)
423 {
424     CPUState *cs = first_cpu;
425     X86CPU *cpu = X86_CPU(cs);
426 
427     DPRINTF("pic_irqs: %s irq %d\n", level? "raise" : "lower", irq);
428     if (cpu->apic_state && !kvm_irqchip_in_kernel()) {
429         CPU_FOREACH(cs) {
430             cpu = X86_CPU(cs);
431             if (apic_accept_pic_intr(cpu->apic_state)) {
432                 apic_deliver_pic_intr(cpu->apic_state, level);
433             }
434         }
435     } else {
436         if (level) {
437             cpu_interrupt(cs, CPU_INTERRUPT_HARD);
438         } else {
439             cpu_reset_interrupt(cs, CPU_INTERRUPT_HARD);
440         }
441     }
442 }
443 
444 /* PC cmos mappings */
445 
446 #define REG_EQUIPMENT_BYTE          0x14
447 
448 int cmos_get_fd_drive_type(FloppyDriveType fd0)
449 {
450     int val;
451 
452     switch (fd0) {
453     case FLOPPY_DRIVE_TYPE_144:
454         /* 1.44 Mb 3"5 drive */
455         val = 4;
456         break;
457     case FLOPPY_DRIVE_TYPE_288:
458         /* 2.88 Mb 3"5 drive */
459         val = 5;
460         break;
461     case FLOPPY_DRIVE_TYPE_120:
462         /* 1.2 Mb 5"5 drive */
463         val = 2;
464         break;
465     case FLOPPY_DRIVE_TYPE_NONE:
466     default:
467         val = 0;
468         break;
469     }
470     return val;
471 }
472 
473 static void cmos_init_hd(ISADevice *s, int type_ofs, int info_ofs,
474                          int16_t cylinders, int8_t heads, int8_t sectors)
475 {
476     rtc_set_memory(s, type_ofs, 47);
477     rtc_set_memory(s, info_ofs, cylinders);
478     rtc_set_memory(s, info_ofs + 1, cylinders >> 8);
479     rtc_set_memory(s, info_ofs + 2, heads);
480     rtc_set_memory(s, info_ofs + 3, 0xff);
481     rtc_set_memory(s, info_ofs + 4, 0xff);
482     rtc_set_memory(s, info_ofs + 5, 0xc0 | ((heads > 8) << 3));
483     rtc_set_memory(s, info_ofs + 6, cylinders);
484     rtc_set_memory(s, info_ofs + 7, cylinders >> 8);
485     rtc_set_memory(s, info_ofs + 8, sectors);
486 }
487 
488 /* convert boot_device letter to something recognizable by the bios */
489 static int boot_device2nibble(char boot_device)
490 {
491     switch(boot_device) {
492     case 'a':
493     case 'b':
494         return 0x01; /* floppy boot */
495     case 'c':
496         return 0x02; /* hard drive boot */
497     case 'd':
498         return 0x03; /* CD-ROM boot */
499     case 'n':
500         return 0x04; /* Network boot */
501     }
502     return 0;
503 }
504 
505 static void set_boot_dev(ISADevice *s, const char *boot_device, Error **errp)
506 {
507 #define PC_MAX_BOOT_DEVICES 3
508     int nbds, bds[3] = { 0, };
509     int i;
510 
511     nbds = strlen(boot_device);
512     if (nbds > PC_MAX_BOOT_DEVICES) {
513         error_setg(errp, "Too many boot devices for PC");
514         return;
515     }
516     for (i = 0; i < nbds; i++) {
517         bds[i] = boot_device2nibble(boot_device[i]);
518         if (bds[i] == 0) {
519             error_setg(errp, "Invalid boot device for PC: '%c'",
520                        boot_device[i]);
521             return;
522         }
523     }
524     rtc_set_memory(s, 0x3d, (bds[1] << 4) | bds[0]);
525     rtc_set_memory(s, 0x38, (bds[2] << 4) | (fd_bootchk ? 0x0 : 0x1));
526 }
527 
528 static void pc_boot_set(void *opaque, const char *boot_device, Error **errp)
529 {
530     set_boot_dev(opaque, boot_device, errp);
531 }
532 
533 static void pc_cmos_init_floppy(ISADevice *rtc_state, ISADevice *floppy)
534 {
535     int val, nb, i;
536     FloppyDriveType fd_type[2] = { FLOPPY_DRIVE_TYPE_NONE,
537                                    FLOPPY_DRIVE_TYPE_NONE };
538 
539     /* floppy type */
540     if (floppy) {
541         for (i = 0; i < 2; i++) {
542             fd_type[i] = isa_fdc_get_drive_type(floppy, i);
543         }
544     }
545     val = (cmos_get_fd_drive_type(fd_type[0]) << 4) |
546         cmos_get_fd_drive_type(fd_type[1]);
547     rtc_set_memory(rtc_state, 0x10, val);
548 
549     val = rtc_get_memory(rtc_state, REG_EQUIPMENT_BYTE);
550     nb = 0;
551     if (fd_type[0] != FLOPPY_DRIVE_TYPE_NONE) {
552         nb++;
553     }
554     if (fd_type[1] != FLOPPY_DRIVE_TYPE_NONE) {
555         nb++;
556     }
557     switch (nb) {
558     case 0:
559         break;
560     case 1:
561         val |= 0x01; /* 1 drive, ready for boot */
562         break;
563     case 2:
564         val |= 0x41; /* 2 drives, ready for boot */
565         break;
566     }
567     rtc_set_memory(rtc_state, REG_EQUIPMENT_BYTE, val);
568 }
569 
570 typedef struct pc_cmos_init_late_arg {
571     ISADevice *rtc_state;
572     BusState *idebus[2];
573 } pc_cmos_init_late_arg;
574 
575 typedef struct check_fdc_state {
576     ISADevice *floppy;
577     bool multiple;
578 } CheckFdcState;
579 
580 static int check_fdc(Object *obj, void *opaque)
581 {
582     CheckFdcState *state = opaque;
583     Object *fdc;
584     uint32_t iobase;
585     Error *local_err = NULL;
586 
587     fdc = object_dynamic_cast(obj, TYPE_ISA_FDC);
588     if (!fdc) {
589         return 0;
590     }
591 
592     iobase = object_property_get_uint(obj, "iobase", &local_err);
593     if (local_err || iobase != 0x3f0) {
594         error_free(local_err);
595         return 0;
596     }
597 
598     if (state->floppy) {
599         state->multiple = true;
600     } else {
601         state->floppy = ISA_DEVICE(obj);
602     }
603     return 0;
604 }
605 
606 static const char * const fdc_container_path[] = {
607     "/unattached", "/peripheral", "/peripheral-anon"
608 };
609 
610 /*
611  * Locate the FDC at IO address 0x3f0, in order to configure the CMOS registers
612  * and ACPI objects.
613  */
614 ISADevice *pc_find_fdc0(void)
615 {
616     int i;
617     Object *container;
618     CheckFdcState state = { 0 };
619 
620     for (i = 0; i < ARRAY_SIZE(fdc_container_path); i++) {
621         container = container_get(qdev_get_machine(), fdc_container_path[i]);
622         object_child_foreach(container, check_fdc, &state);
623     }
624 
625     if (state.multiple) {
626         warn_report("multiple floppy disk controllers with "
627                     "iobase=0x3f0 have been found");
628         error_printf("the one being picked for CMOS setup might not reflect "
629                      "your intent");
630     }
631 
632     return state.floppy;
633 }
634 
635 static void pc_cmos_init_late(void *opaque)
636 {
637     pc_cmos_init_late_arg *arg = opaque;
638     ISADevice *s = arg->rtc_state;
639     int16_t cylinders;
640     int8_t heads, sectors;
641     int val;
642     int i, trans;
643 
644     val = 0;
645     if (arg->idebus[0] && ide_get_geometry(arg->idebus[0], 0,
646                                            &cylinders, &heads, &sectors) >= 0) {
647         cmos_init_hd(s, 0x19, 0x1b, cylinders, heads, sectors);
648         val |= 0xf0;
649     }
650     if (arg->idebus[0] && ide_get_geometry(arg->idebus[0], 1,
651                                            &cylinders, &heads, &sectors) >= 0) {
652         cmos_init_hd(s, 0x1a, 0x24, cylinders, heads, sectors);
653         val |= 0x0f;
654     }
655     rtc_set_memory(s, 0x12, val);
656 
657     val = 0;
658     for (i = 0; i < 4; i++) {
659         /* NOTE: ide_get_geometry() returns the physical
660            geometry.  It is always such that: 1 <= sects <= 63, 1
661            <= heads <= 16, 1 <= cylinders <= 16383. The BIOS
662            geometry can be different if a translation is done. */
663         if (arg->idebus[i / 2] &&
664             ide_get_geometry(arg->idebus[i / 2], i % 2,
665                              &cylinders, &heads, &sectors) >= 0) {
666             trans = ide_get_bios_chs_trans(arg->idebus[i / 2], i % 2) - 1;
667             assert((trans & ~3) == 0);
668             val |= trans << (i * 2);
669         }
670     }
671     rtc_set_memory(s, 0x39, val);
672 
673     pc_cmos_init_floppy(s, pc_find_fdc0());
674 
675     qemu_unregister_reset(pc_cmos_init_late, opaque);
676 }
677 
678 void pc_cmos_init(PCMachineState *pcms,
679                   BusState *idebus0, BusState *idebus1,
680                   ISADevice *s)
681 {
682     int val;
683     static pc_cmos_init_late_arg arg;
684 
685     /* various important CMOS locations needed by PC/Bochs bios */
686 
687     /* memory size */
688     /* base memory (first MiB) */
689     val = MIN(pcms->below_4g_mem_size / KiB, 640);
690     rtc_set_memory(s, 0x15, val);
691     rtc_set_memory(s, 0x16, val >> 8);
692     /* extended memory (next 64MiB) */
693     if (pcms->below_4g_mem_size > 1 * MiB) {
694         val = (pcms->below_4g_mem_size - 1 * MiB) / KiB;
695     } else {
696         val = 0;
697     }
698     if (val > 65535)
699         val = 65535;
700     rtc_set_memory(s, 0x17, val);
701     rtc_set_memory(s, 0x18, val >> 8);
702     rtc_set_memory(s, 0x30, val);
703     rtc_set_memory(s, 0x31, val >> 8);
704     /* memory between 16MiB and 4GiB */
705     if (pcms->below_4g_mem_size > 16 * MiB) {
706         val = (pcms->below_4g_mem_size - 16 * MiB) / (64 * KiB);
707     } else {
708         val = 0;
709     }
710     if (val > 65535)
711         val = 65535;
712     rtc_set_memory(s, 0x34, val);
713     rtc_set_memory(s, 0x35, val >> 8);
714     /* memory above 4GiB */
715     val = pcms->above_4g_mem_size / 65536;
716     rtc_set_memory(s, 0x5b, val);
717     rtc_set_memory(s, 0x5c, val >> 8);
718     rtc_set_memory(s, 0x5d, val >> 16);
719 
720     object_property_add_link(OBJECT(pcms), "rtc_state",
721                              TYPE_ISA_DEVICE,
722                              (Object **)&pcms->rtc,
723                              object_property_allow_set_link,
724                              OBJ_PROP_LINK_STRONG, &error_abort);
725     object_property_set_link(OBJECT(pcms), OBJECT(s),
726                              "rtc_state", &error_abort);
727 
728     set_boot_dev(s, MACHINE(pcms)->boot_order, &error_fatal);
729 
730     val = 0;
731     val |= 0x02; /* FPU is there */
732     val |= 0x04; /* PS/2 mouse installed */
733     rtc_set_memory(s, REG_EQUIPMENT_BYTE, val);
734 
735     /* hard drives and FDC */
736     arg.rtc_state = s;
737     arg.idebus[0] = idebus0;
738     arg.idebus[1] = idebus1;
739     qemu_register_reset(pc_cmos_init_late, &arg);
740 }
741 
742 #define TYPE_PORT92 "port92"
743 #define PORT92(obj) OBJECT_CHECK(Port92State, (obj), TYPE_PORT92)
744 
745 /* port 92 stuff: could be split off */
746 typedef struct Port92State {
747     ISADevice parent_obj;
748 
749     MemoryRegion io;
750     uint8_t outport;
751     qemu_irq a20_out;
752 } Port92State;
753 
754 static void port92_write(void *opaque, hwaddr addr, uint64_t val,
755                          unsigned size)
756 {
757     Port92State *s = opaque;
758     int oldval = s->outport;
759 
760     DPRINTF("port92: write 0x%02" PRIx64 "\n", val);
761     s->outport = val;
762     qemu_set_irq(s->a20_out, (val >> 1) & 1);
763     if ((val & 1) && !(oldval & 1)) {
764         qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET);
765     }
766 }
767 
768 static uint64_t port92_read(void *opaque, hwaddr addr,
769                             unsigned size)
770 {
771     Port92State *s = opaque;
772     uint32_t ret;
773 
774     ret = s->outport;
775     DPRINTF("port92: read 0x%02x\n", ret);
776     return ret;
777 }
778 
779 static void port92_init(ISADevice *dev, qemu_irq a20_out)
780 {
781     qdev_connect_gpio_out_named(DEVICE(dev), PORT92_A20_LINE, 0, a20_out);
782 }
783 
784 static const VMStateDescription vmstate_port92_isa = {
785     .name = "port92",
786     .version_id = 1,
787     .minimum_version_id = 1,
788     .fields = (VMStateField[]) {
789         VMSTATE_UINT8(outport, Port92State),
790         VMSTATE_END_OF_LIST()
791     }
792 };
793 
794 static void port92_reset(DeviceState *d)
795 {
796     Port92State *s = PORT92(d);
797 
798     s->outport &= ~1;
799 }
800 
801 static const MemoryRegionOps port92_ops = {
802     .read = port92_read,
803     .write = port92_write,
804     .impl = {
805         .min_access_size = 1,
806         .max_access_size = 1,
807     },
808     .endianness = DEVICE_LITTLE_ENDIAN,
809 };
810 
811 static void port92_initfn(Object *obj)
812 {
813     Port92State *s = PORT92(obj);
814 
815     memory_region_init_io(&s->io, OBJECT(s), &port92_ops, s, "port92", 1);
816 
817     s->outport = 0;
818 
819     qdev_init_gpio_out_named(DEVICE(obj), &s->a20_out, PORT92_A20_LINE, 1);
820 }
821 
822 static void port92_realizefn(DeviceState *dev, Error **errp)
823 {
824     ISADevice *isadev = ISA_DEVICE(dev);
825     Port92State *s = PORT92(dev);
826 
827     isa_register_ioport(isadev, &s->io, 0x92);
828 }
829 
830 static void port92_class_initfn(ObjectClass *klass, void *data)
831 {
832     DeviceClass *dc = DEVICE_CLASS(klass);
833 
834     dc->realize = port92_realizefn;
835     dc->reset = port92_reset;
836     dc->vmsd = &vmstate_port92_isa;
837     /*
838      * Reason: unlike ordinary ISA devices, this one needs additional
839      * wiring: its A20 output line needs to be wired up by
840      * port92_init().
841      */
842     dc->user_creatable = false;
843 }
844 
845 static const TypeInfo port92_info = {
846     .name          = TYPE_PORT92,
847     .parent        = TYPE_ISA_DEVICE,
848     .instance_size = sizeof(Port92State),
849     .instance_init = port92_initfn,
850     .class_init    = port92_class_initfn,
851 };
852 
853 static void port92_register_types(void)
854 {
855     type_register_static(&port92_info);
856 }
857 
858 type_init(port92_register_types)
859 
860 static void handle_a20_line_change(void *opaque, int irq, int level)
861 {
862     X86CPU *cpu = opaque;
863 
864     /* XXX: send to all CPUs ? */
865     /* XXX: add logic to handle multiple A20 line sources */
866     x86_cpu_set_a20(cpu, level);
867 }
868 
869 /* Calculates initial APIC ID for a specific CPU index
870  *
871  * Currently we need to be able to calculate the APIC ID from the CPU index
872  * alone (without requiring a CPU object), as the QEMU<->Seabios interfaces have
873  * no concept of "CPU index", and the NUMA tables on fw_cfg need the APIC ID of
874  * all CPUs up to max_cpus.
875  */
876 static uint32_t x86_cpu_apic_id_from_index(PCMachineState *pcms,
877                                            unsigned int cpu_index)
878 {
879     MachineState *ms = MACHINE(pcms);
880     PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms);
881     uint32_t correct_id;
882     static bool warned;
883 
884     correct_id = x86_apicid_from_cpu_idx(pcms->smp_dies, ms->smp.cores,
885                                          ms->smp.threads, cpu_index);
886     if (pcmc->compat_apic_id_mode) {
887         if (cpu_index != correct_id && !warned && !qtest_enabled()) {
888             error_report("APIC IDs set in compatibility mode, "
889                          "CPU topology won't match the configuration");
890             warned = true;
891         }
892         return cpu_index;
893     } else {
894         return correct_id;
895     }
896 }
897 
898 static long get_file_size(FILE *f)
899 {
900     long where, size;
901 
902     /* XXX: on Unix systems, using fstat() probably makes more sense */
903 
904     where = ftell(f);
905     fseek(f, 0, SEEK_END);
906     size = ftell(f);
907     fseek(f, where, SEEK_SET);
908 
909     return size;
910 }
911 
912 struct setup_data {
913     uint64_t next;
914     uint32_t type;
915     uint32_t len;
916     uint8_t data[0];
917 } __attribute__((packed));
918 
919 
920 /*
921  * The entry point into the kernel for PVH boot is different from
922  * the native entry point.  The PVH entry is defined by the x86/HVM
923  * direct boot ABI and is available in an ELFNOTE in the kernel binary.
924  *
925  * This function is passed to load_elf() when it is called from
926  * load_elfboot() which then additionally checks for an ELF Note of
927  * type XEN_ELFNOTE_PHYS32_ENTRY and passes it to this function to
928  * parse the PVH entry address from the ELF Note.
929  *
930  * Due to trickery in elf_opts.h, load_elf() is actually available as
931  * load_elf32() or load_elf64() and this routine needs to be able
932  * to deal with being called as 32 or 64 bit.
933  *
934  * The address of the PVH entry point is saved to the 'pvh_start_addr'
935  * global variable.  (although the entry point is 32-bit, the kernel
936  * binary can be either 32-bit or 64-bit).
937  */
938 static uint64_t read_pvh_start_addr(void *arg1, void *arg2, bool is64)
939 {
940     size_t *elf_note_data_addr;
941 
942     /* Check if ELF Note header passed in is valid */
943     if (arg1 == NULL) {
944         return 0;
945     }
946 
947     if (is64) {
948         struct elf64_note *nhdr64 = (struct elf64_note *)arg1;
949         uint64_t nhdr_size64 = sizeof(struct elf64_note);
950         uint64_t phdr_align = *(uint64_t *)arg2;
951         uint64_t nhdr_namesz = nhdr64->n_namesz;
952 
953         elf_note_data_addr =
954             ((void *)nhdr64) + nhdr_size64 +
955             QEMU_ALIGN_UP(nhdr_namesz, phdr_align);
956     } else {
957         struct elf32_note *nhdr32 = (struct elf32_note *)arg1;
958         uint32_t nhdr_size32 = sizeof(struct elf32_note);
959         uint32_t phdr_align = *(uint32_t *)arg2;
960         uint32_t nhdr_namesz = nhdr32->n_namesz;
961 
962         elf_note_data_addr =
963             ((void *)nhdr32) + nhdr_size32 +
964             QEMU_ALIGN_UP(nhdr_namesz, phdr_align);
965     }
966 
967     pvh_start_addr = *elf_note_data_addr;
968 
969     return pvh_start_addr;
970 }
971 
972 static bool load_elfboot(const char *kernel_filename,
973                    int kernel_file_size,
974                    uint8_t *header,
975                    size_t pvh_xen_start_addr,
976                    FWCfgState *fw_cfg)
977 {
978     uint32_t flags = 0;
979     uint32_t mh_load_addr = 0;
980     uint32_t elf_kernel_size = 0;
981     uint64_t elf_entry;
982     uint64_t elf_low, elf_high;
983     int kernel_size;
984 
985     if (ldl_p(header) != 0x464c457f) {
986         return false; /* no elfboot */
987     }
988 
989     bool elf_is64 = header[EI_CLASS] == ELFCLASS64;
990     flags = elf_is64 ?
991         ((Elf64_Ehdr *)header)->e_flags : ((Elf32_Ehdr *)header)->e_flags;
992 
993     if (flags & 0x00010004) { /* LOAD_ELF_HEADER_HAS_ADDR */
994         error_report("elfboot unsupported flags = %x", flags);
995         exit(1);
996     }
997 
998     uint64_t elf_note_type = XEN_ELFNOTE_PHYS32_ENTRY;
999     kernel_size = load_elf(kernel_filename, read_pvh_start_addr,
1000                            NULL, &elf_note_type, &elf_entry,
1001                            &elf_low, &elf_high, 0, I386_ELF_MACHINE,
1002                            0, 0);
1003 
1004     if (kernel_size < 0) {
1005         error_report("Error while loading elf kernel");
1006         exit(1);
1007     }
1008     mh_load_addr = elf_low;
1009     elf_kernel_size = elf_high - elf_low;
1010 
1011     if (pvh_start_addr == 0) {
1012         error_report("Error loading uncompressed kernel without PVH ELF Note");
1013         exit(1);
1014     }
1015     fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_ENTRY, pvh_start_addr);
1016     fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_ADDR, mh_load_addr);
1017     fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_SIZE, elf_kernel_size);
1018 
1019     return true;
1020 }
1021 
1022 static void load_linux(PCMachineState *pcms,
1023                        FWCfgState *fw_cfg)
1024 {
1025     uint16_t protocol;
1026     int setup_size, kernel_size, cmdline_size;
1027     int dtb_size, setup_data_offset;
1028     uint32_t initrd_max;
1029     uint8_t header[8192], *setup, *kernel;
1030     hwaddr real_addr, prot_addr, cmdline_addr, initrd_addr = 0;
1031     FILE *f;
1032     char *vmode;
1033     MachineState *machine = MACHINE(pcms);
1034     PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms);
1035     struct setup_data *setup_data;
1036     const char *kernel_filename = machine->kernel_filename;
1037     const char *initrd_filename = machine->initrd_filename;
1038     const char *dtb_filename = machine->dtb;
1039     const char *kernel_cmdline = machine->kernel_cmdline;
1040 
1041     /* Align to 16 bytes as a paranoia measure */
1042     cmdline_size = (strlen(kernel_cmdline)+16) & ~15;
1043 
1044     /* load the kernel header */
1045     f = fopen(kernel_filename, "rb");
1046     if (!f || !(kernel_size = get_file_size(f)) ||
1047         fread(header, 1, MIN(ARRAY_SIZE(header), kernel_size), f) !=
1048         MIN(ARRAY_SIZE(header), kernel_size)) {
1049         fprintf(stderr, "qemu: could not load kernel '%s': %s\n",
1050                 kernel_filename, strerror(errno));
1051         exit(1);
1052     }
1053 
1054     /* kernel protocol version */
1055 #if 0
1056     fprintf(stderr, "header magic: %#x\n", ldl_p(header+0x202));
1057 #endif
1058     if (ldl_p(header+0x202) == 0x53726448) {
1059         protocol = lduw_p(header+0x206);
1060     } else {
1061         /*
1062          * This could be a multiboot kernel. If it is, let's stop treating it
1063          * like a Linux kernel.
1064          * Note: some multiboot images could be in the ELF format (the same of
1065          * PVH), so we try multiboot first since we check the multiboot magic
1066          * header before to load it.
1067          */
1068         if (load_multiboot(fw_cfg, f, kernel_filename, initrd_filename,
1069                            kernel_cmdline, kernel_size, header)) {
1070             return;
1071         }
1072         /*
1073          * Check if the file is an uncompressed kernel file (ELF) and load it,
1074          * saving the PVH entry point used by the x86/HVM direct boot ABI.
1075          * If load_elfboot() is successful, populate the fw_cfg info.
1076          */
1077         if (pcmc->pvh_enabled &&
1078             load_elfboot(kernel_filename, kernel_size,
1079                          header, pvh_start_addr, fw_cfg)) {
1080             fclose(f);
1081 
1082             fw_cfg_add_i32(fw_cfg, FW_CFG_CMDLINE_SIZE,
1083                 strlen(kernel_cmdline) + 1);
1084             fw_cfg_add_string(fw_cfg, FW_CFG_CMDLINE_DATA, kernel_cmdline);
1085 
1086             fw_cfg_add_i32(fw_cfg, FW_CFG_SETUP_SIZE, sizeof(header));
1087             fw_cfg_add_bytes(fw_cfg, FW_CFG_SETUP_DATA,
1088                              header, sizeof(header));
1089 
1090             /* load initrd */
1091             if (initrd_filename) {
1092                 GMappedFile *mapped_file;
1093                 gsize initrd_size;
1094                 gchar *initrd_data;
1095                 GError *gerr = NULL;
1096 
1097                 mapped_file = g_mapped_file_new(initrd_filename, false, &gerr);
1098                 if (!mapped_file) {
1099                     fprintf(stderr, "qemu: error reading initrd %s: %s\n",
1100                             initrd_filename, gerr->message);
1101                     exit(1);
1102                 }
1103                 pcms->initrd_mapped_file = mapped_file;
1104 
1105                 initrd_data = g_mapped_file_get_contents(mapped_file);
1106                 initrd_size = g_mapped_file_get_length(mapped_file);
1107                 initrd_max = pcms->below_4g_mem_size - pcmc->acpi_data_size - 1;
1108                 if (initrd_size >= initrd_max) {
1109                     fprintf(stderr, "qemu: initrd is too large, cannot support."
1110                             "(max: %"PRIu32", need %"PRId64")\n",
1111                             initrd_max, (uint64_t)initrd_size);
1112                     exit(1);
1113                 }
1114 
1115                 initrd_addr = (initrd_max - initrd_size) & ~4095;
1116 
1117                 fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_ADDR, initrd_addr);
1118                 fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_SIZE, initrd_size);
1119                 fw_cfg_add_bytes(fw_cfg, FW_CFG_INITRD_DATA, initrd_data,
1120                                  initrd_size);
1121             }
1122 
1123             option_rom[nb_option_roms].bootindex = 0;
1124             option_rom[nb_option_roms].name = "pvh.bin";
1125             nb_option_roms++;
1126 
1127             return;
1128         }
1129         protocol = 0;
1130     }
1131 
1132     if (protocol < 0x200 || !(header[0x211] & 0x01)) {
1133         /* Low kernel */
1134         real_addr    = 0x90000;
1135         cmdline_addr = 0x9a000 - cmdline_size;
1136         prot_addr    = 0x10000;
1137     } else if (protocol < 0x202) {
1138         /* High but ancient kernel */
1139         real_addr    = 0x90000;
1140         cmdline_addr = 0x9a000 - cmdline_size;
1141         prot_addr    = 0x100000;
1142     } else {
1143         /* High and recent kernel */
1144         real_addr    = 0x10000;
1145         cmdline_addr = 0x20000;
1146         prot_addr    = 0x100000;
1147     }
1148 
1149 #if 0
1150     fprintf(stderr,
1151             "qemu: real_addr     = 0x" TARGET_FMT_plx "\n"
1152             "qemu: cmdline_addr  = 0x" TARGET_FMT_plx "\n"
1153             "qemu: prot_addr     = 0x" TARGET_FMT_plx "\n",
1154             real_addr,
1155             cmdline_addr,
1156             prot_addr);
1157 #endif
1158 
1159     /* highest address for loading the initrd */
1160     if (protocol >= 0x20c &&
1161         lduw_p(header+0x236) & XLF_CAN_BE_LOADED_ABOVE_4G) {
1162         /*
1163          * Linux has supported initrd up to 4 GB for a very long time (2007,
1164          * long before XLF_CAN_BE_LOADED_ABOVE_4G which was added in 2013),
1165          * though it only sets initrd_max to 2 GB to "work around bootloader
1166          * bugs". Luckily, QEMU firmware(which does something like bootloader)
1167          * has supported this.
1168          *
1169          * It's believed that if XLF_CAN_BE_LOADED_ABOVE_4G is set, initrd can
1170          * be loaded into any address.
1171          *
1172          * In addition, initrd_max is uint32_t simply because QEMU doesn't
1173          * support the 64-bit boot protocol (specifically the ext_ramdisk_image
1174          * field).
1175          *
1176          * Therefore here just limit initrd_max to UINT32_MAX simply as well.
1177          */
1178         initrd_max = UINT32_MAX;
1179     } else if (protocol >= 0x203) {
1180         initrd_max = ldl_p(header+0x22c);
1181     } else {
1182         initrd_max = 0x37ffffff;
1183     }
1184 
1185     if (initrd_max >= pcms->below_4g_mem_size - pcmc->acpi_data_size) {
1186         initrd_max = pcms->below_4g_mem_size - pcmc->acpi_data_size - 1;
1187     }
1188 
1189     fw_cfg_add_i32(fw_cfg, FW_CFG_CMDLINE_ADDR, cmdline_addr);
1190     fw_cfg_add_i32(fw_cfg, FW_CFG_CMDLINE_SIZE, strlen(kernel_cmdline)+1);
1191     fw_cfg_add_string(fw_cfg, FW_CFG_CMDLINE_DATA, kernel_cmdline);
1192 
1193     if (protocol >= 0x202) {
1194         stl_p(header+0x228, cmdline_addr);
1195     } else {
1196         stw_p(header+0x20, 0xA33F);
1197         stw_p(header+0x22, cmdline_addr-real_addr);
1198     }
1199 
1200     /* handle vga= parameter */
1201     vmode = strstr(kernel_cmdline, "vga=");
1202     if (vmode) {
1203         unsigned int video_mode;
1204         /* skip "vga=" */
1205         vmode += 4;
1206         if (!strncmp(vmode, "normal", 6)) {
1207             video_mode = 0xffff;
1208         } else if (!strncmp(vmode, "ext", 3)) {
1209             video_mode = 0xfffe;
1210         } else if (!strncmp(vmode, "ask", 3)) {
1211             video_mode = 0xfffd;
1212         } else {
1213             video_mode = strtol(vmode, NULL, 0);
1214         }
1215         stw_p(header+0x1fa, video_mode);
1216     }
1217 
1218     /* loader type */
1219     /* High nybble = B reserved for QEMU; low nybble is revision number.
1220        If this code is substantially changed, you may want to consider
1221        incrementing the revision. */
1222     if (protocol >= 0x200) {
1223         header[0x210] = 0xB0;
1224     }
1225     /* heap */
1226     if (protocol >= 0x201) {
1227         header[0x211] |= 0x80;	/* CAN_USE_HEAP */
1228         stw_p(header+0x224, cmdline_addr-real_addr-0x200);
1229     }
1230 
1231     /* load initrd */
1232     if (initrd_filename) {
1233         GMappedFile *mapped_file;
1234         gsize initrd_size;
1235         gchar *initrd_data;
1236         GError *gerr = NULL;
1237 
1238         if (protocol < 0x200) {
1239             fprintf(stderr, "qemu: linux kernel too old to load a ram disk\n");
1240             exit(1);
1241         }
1242 
1243         mapped_file = g_mapped_file_new(initrd_filename, false, &gerr);
1244         if (!mapped_file) {
1245             fprintf(stderr, "qemu: error reading initrd %s: %s\n",
1246                     initrd_filename, gerr->message);
1247             exit(1);
1248         }
1249         pcms->initrd_mapped_file = mapped_file;
1250 
1251         initrd_data = g_mapped_file_get_contents(mapped_file);
1252         initrd_size = g_mapped_file_get_length(mapped_file);
1253         if (initrd_size >= initrd_max) {
1254             fprintf(stderr, "qemu: initrd is too large, cannot support."
1255                     "(max: %"PRIu32", need %"PRId64")\n",
1256                     initrd_max, (uint64_t)initrd_size);
1257             exit(1);
1258         }
1259 
1260         initrd_addr = (initrd_max-initrd_size) & ~4095;
1261 
1262         fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_ADDR, initrd_addr);
1263         fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_SIZE, initrd_size);
1264         fw_cfg_add_bytes(fw_cfg, FW_CFG_INITRD_DATA, initrd_data, initrd_size);
1265 
1266         stl_p(header+0x218, initrd_addr);
1267         stl_p(header+0x21c, initrd_size);
1268     }
1269 
1270     /* load kernel and setup */
1271     setup_size = header[0x1f1];
1272     if (setup_size == 0) {
1273         setup_size = 4;
1274     }
1275     setup_size = (setup_size+1)*512;
1276     if (setup_size > kernel_size) {
1277         fprintf(stderr, "qemu: invalid kernel header\n");
1278         exit(1);
1279     }
1280     kernel_size -= setup_size;
1281 
1282     setup  = g_malloc(setup_size);
1283     kernel = g_malloc(kernel_size);
1284     fseek(f, 0, SEEK_SET);
1285     if (fread(setup, 1, setup_size, f) != setup_size) {
1286         fprintf(stderr, "fread() failed\n");
1287         exit(1);
1288     }
1289     if (fread(kernel, 1, kernel_size, f) != kernel_size) {
1290         fprintf(stderr, "fread() failed\n");
1291         exit(1);
1292     }
1293     fclose(f);
1294 
1295     /* append dtb to kernel */
1296     if (dtb_filename) {
1297         if (protocol < 0x209) {
1298             fprintf(stderr, "qemu: Linux kernel too old to load a dtb\n");
1299             exit(1);
1300         }
1301 
1302         dtb_size = get_image_size(dtb_filename);
1303         if (dtb_size <= 0) {
1304             fprintf(stderr, "qemu: error reading dtb %s: %s\n",
1305                     dtb_filename, strerror(errno));
1306             exit(1);
1307         }
1308 
1309         setup_data_offset = QEMU_ALIGN_UP(kernel_size, 16);
1310         kernel_size = setup_data_offset + sizeof(struct setup_data) + dtb_size;
1311         kernel = g_realloc(kernel, kernel_size);
1312 
1313         stq_p(header+0x250, prot_addr + setup_data_offset);
1314 
1315         setup_data = (struct setup_data *)(kernel + setup_data_offset);
1316         setup_data->next = 0;
1317         setup_data->type = cpu_to_le32(SETUP_DTB);
1318         setup_data->len = cpu_to_le32(dtb_size);
1319 
1320         load_image_size(dtb_filename, setup_data->data, dtb_size);
1321     }
1322 
1323     memcpy(setup, header, MIN(sizeof(header), setup_size));
1324 
1325     fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_ADDR, prot_addr);
1326     fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_SIZE, kernel_size);
1327     fw_cfg_add_bytes(fw_cfg, FW_CFG_KERNEL_DATA, kernel, kernel_size);
1328 
1329     fw_cfg_add_i32(fw_cfg, FW_CFG_SETUP_ADDR, real_addr);
1330     fw_cfg_add_i32(fw_cfg, FW_CFG_SETUP_SIZE, setup_size);
1331     fw_cfg_add_bytes(fw_cfg, FW_CFG_SETUP_DATA, setup, setup_size);
1332 
1333     option_rom[nb_option_roms].bootindex = 0;
1334     option_rom[nb_option_roms].name = "linuxboot.bin";
1335     if (pcmc->linuxboot_dma_enabled && fw_cfg_dma_enabled(fw_cfg)) {
1336         option_rom[nb_option_roms].name = "linuxboot_dma.bin";
1337     }
1338     nb_option_roms++;
1339 }
1340 
1341 #define NE2000_NB_MAX 6
1342 
1343 static const int ne2000_io[NE2000_NB_MAX] = { 0x300, 0x320, 0x340, 0x360,
1344                                               0x280, 0x380 };
1345 static const int ne2000_irq[NE2000_NB_MAX] = { 9, 10, 11, 3, 4, 5 };
1346 
1347 void pc_init_ne2k_isa(ISABus *bus, NICInfo *nd)
1348 {
1349     static int nb_ne2k = 0;
1350 
1351     if (nb_ne2k == NE2000_NB_MAX)
1352         return;
1353     isa_ne2000_init(bus, ne2000_io[nb_ne2k],
1354                     ne2000_irq[nb_ne2k], nd);
1355     nb_ne2k++;
1356 }
1357 
1358 DeviceState *cpu_get_current_apic(void)
1359 {
1360     if (current_cpu) {
1361         X86CPU *cpu = X86_CPU(current_cpu);
1362         return cpu->apic_state;
1363     } else {
1364         return NULL;
1365     }
1366 }
1367 
1368 void pc_acpi_smi_interrupt(void *opaque, int irq, int level)
1369 {
1370     X86CPU *cpu = opaque;
1371 
1372     if (level) {
1373         cpu_interrupt(CPU(cpu), CPU_INTERRUPT_SMI);
1374     }
1375 }
1376 
1377 static void pc_new_cpu(PCMachineState *pcms, int64_t apic_id, Error **errp)
1378 {
1379     Object *cpu = NULL;
1380     Error *local_err = NULL;
1381     CPUX86State *env = NULL;
1382 
1383     cpu = object_new(MACHINE(pcms)->cpu_type);
1384 
1385     env = &X86_CPU(cpu)->env;
1386     env->nr_dies = pcms->smp_dies;
1387 
1388     object_property_set_uint(cpu, apic_id, "apic-id", &local_err);
1389     object_property_set_bool(cpu, true, "realized", &local_err);
1390 
1391     object_unref(cpu);
1392     error_propagate(errp, local_err);
1393 }
1394 
1395 /*
1396  * This function is very similar to smp_parse()
1397  * in hw/core/machine.c but includes CPU die support.
1398  */
1399 void pc_smp_parse(MachineState *ms, QemuOpts *opts)
1400 {
1401     PCMachineState *pcms = PC_MACHINE(ms);
1402 
1403     if (opts) {
1404         unsigned cpus    = qemu_opt_get_number(opts, "cpus", 0);
1405         unsigned sockets = qemu_opt_get_number(opts, "sockets", 0);
1406         unsigned dies = qemu_opt_get_number(opts, "dies", 1);
1407         unsigned cores   = qemu_opt_get_number(opts, "cores", 0);
1408         unsigned threads = qemu_opt_get_number(opts, "threads", 0);
1409 
1410         /* compute missing values, prefer sockets over cores over threads */
1411         if (cpus == 0 || sockets == 0) {
1412             cores = cores > 0 ? cores : 1;
1413             threads = threads > 0 ? threads : 1;
1414             if (cpus == 0) {
1415                 sockets = sockets > 0 ? sockets : 1;
1416                 cpus = cores * threads * dies * sockets;
1417             } else {
1418                 ms->smp.max_cpus =
1419                         qemu_opt_get_number(opts, "maxcpus", cpus);
1420                 sockets = ms->smp.max_cpus / (cores * threads * dies);
1421             }
1422         } else if (cores == 0) {
1423             threads = threads > 0 ? threads : 1;
1424             cores = cpus / (sockets * dies * threads);
1425             cores = cores > 0 ? cores : 1;
1426         } else if (threads == 0) {
1427             threads = cpus / (cores * dies * sockets);
1428             threads = threads > 0 ? threads : 1;
1429         } else if (sockets * dies * cores * threads < cpus) {
1430             error_report("cpu topology: "
1431                          "sockets (%u) * dies (%u) * cores (%u) * threads (%u) < "
1432                          "smp_cpus (%u)",
1433                          sockets, dies, cores, threads, cpus);
1434             exit(1);
1435         }
1436 
1437         ms->smp.max_cpus =
1438                 qemu_opt_get_number(opts, "maxcpus", cpus);
1439 
1440         if (ms->smp.max_cpus < cpus) {
1441             error_report("maxcpus must be equal to or greater than smp");
1442             exit(1);
1443         }
1444 
1445         if (sockets * dies * cores * threads > ms->smp.max_cpus) {
1446             error_report("cpu topology: "
1447                          "sockets (%u) * dies (%u) * cores (%u) * threads (%u) > "
1448                          "maxcpus (%u)",
1449                          sockets, dies, cores, threads,
1450                          ms->smp.max_cpus);
1451             exit(1);
1452         }
1453 
1454         if (sockets * dies * cores * threads != ms->smp.max_cpus) {
1455             warn_report("Invalid CPU topology deprecated: "
1456                         "sockets (%u) * dies (%u) * cores (%u) * threads (%u) "
1457                         "!= maxcpus (%u)",
1458                         sockets, dies, cores, threads,
1459                         ms->smp.max_cpus);
1460         }
1461 
1462         ms->smp.cpus = cpus;
1463         ms->smp.cores = cores;
1464         ms->smp.threads = threads;
1465         pcms->smp_dies = dies;
1466     }
1467 
1468     if (ms->smp.cpus > 1) {
1469         Error *blocker = NULL;
1470         error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED, "smp");
1471         replay_add_blocker(blocker);
1472     }
1473 }
1474 
1475 void pc_hot_add_cpu(MachineState *ms, const int64_t id, Error **errp)
1476 {
1477     PCMachineState *pcms = PC_MACHINE(ms);
1478     int64_t apic_id = x86_cpu_apic_id_from_index(pcms, id);
1479     Error *local_err = NULL;
1480 
1481     if (id < 0) {
1482         error_setg(errp, "Invalid CPU id: %" PRIi64, id);
1483         return;
1484     }
1485 
1486     if (apic_id >= ACPI_CPU_HOTPLUG_ID_LIMIT) {
1487         error_setg(errp, "Unable to add CPU: %" PRIi64
1488                    ", resulting APIC ID (%" PRIi64 ") is too large",
1489                    id, apic_id);
1490         return;
1491     }
1492 
1493     pc_new_cpu(PC_MACHINE(ms), apic_id, &local_err);
1494     if (local_err) {
1495         error_propagate(errp, local_err);
1496         return;
1497     }
1498 }
1499 
1500 void pc_cpus_init(PCMachineState *pcms)
1501 {
1502     int i;
1503     const CPUArchIdList *possible_cpus;
1504     MachineState *ms = MACHINE(pcms);
1505     MachineClass *mc = MACHINE_GET_CLASS(pcms);
1506     PCMachineClass *pcmc = PC_MACHINE_CLASS(mc);
1507 
1508     x86_cpu_set_default_version(pcmc->default_cpu_version);
1509 
1510     /* Calculates the limit to CPU APIC ID values
1511      *
1512      * Limit for the APIC ID value, so that all
1513      * CPU APIC IDs are < pcms->apic_id_limit.
1514      *
1515      * This is used for FW_CFG_MAX_CPUS. See comments on fw_cfg_arch_create().
1516      */
1517     pcms->apic_id_limit = x86_cpu_apic_id_from_index(pcms,
1518                                                      ms->smp.max_cpus - 1) + 1;
1519     possible_cpus = mc->possible_cpu_arch_ids(ms);
1520     for (i = 0; i < ms->smp.cpus; i++) {
1521         pc_new_cpu(pcms, possible_cpus->cpus[i].arch_id, &error_fatal);
1522     }
1523 }
1524 
1525 static void rtc_set_cpus_count(ISADevice *rtc, uint16_t cpus_count)
1526 {
1527     if (cpus_count > 0xff) {
1528         /* If the number of CPUs can't be represented in 8 bits, the
1529          * BIOS must use "FW_CFG_NB_CPUS". Set RTC field to 0 just
1530          * to make old BIOSes fail more predictably.
1531          */
1532         rtc_set_memory(rtc, 0x5f, 0);
1533     } else {
1534         rtc_set_memory(rtc, 0x5f, cpus_count - 1);
1535     }
1536 }
1537 
1538 static
1539 void pc_machine_done(Notifier *notifier, void *data)
1540 {
1541     PCMachineState *pcms = container_of(notifier,
1542                                         PCMachineState, machine_done);
1543     PCIBus *bus = pcms->bus;
1544 
1545     /* set the number of CPUs */
1546     rtc_set_cpus_count(pcms->rtc, pcms->boot_cpus);
1547 
1548     if (bus) {
1549         int extra_hosts = 0;
1550 
1551         QLIST_FOREACH(bus, &bus->child, sibling) {
1552             /* look for expander root buses */
1553             if (pci_bus_is_root(bus)) {
1554                 extra_hosts++;
1555             }
1556         }
1557         if (extra_hosts && pcms->fw_cfg) {
1558             uint64_t *val = g_malloc(sizeof(*val));
1559             *val = cpu_to_le64(extra_hosts);
1560             fw_cfg_add_file(pcms->fw_cfg,
1561                     "etc/extra-pci-roots", val, sizeof(*val));
1562         }
1563     }
1564 
1565     acpi_setup();
1566     if (pcms->fw_cfg) {
1567         fw_cfg_build_smbios(MACHINE(pcms), pcms->fw_cfg);
1568         fw_cfg_build_feature_control(MACHINE(pcms), pcms->fw_cfg);
1569         /* update FW_CFG_NB_CPUS to account for -device added CPUs */
1570         fw_cfg_modify_i16(pcms->fw_cfg, FW_CFG_NB_CPUS, pcms->boot_cpus);
1571     }
1572 
1573     if (pcms->apic_id_limit > 255 && !xen_enabled()) {
1574         IntelIOMMUState *iommu = INTEL_IOMMU_DEVICE(x86_iommu_get_default());
1575 
1576         if (!iommu || !x86_iommu_ir_supported(X86_IOMMU_DEVICE(iommu)) ||
1577             iommu->intr_eim != ON_OFF_AUTO_ON) {
1578             error_report("current -smp configuration requires "
1579                          "Extended Interrupt Mode enabled. "
1580                          "You can add an IOMMU using: "
1581                          "-device intel-iommu,intremap=on,eim=on");
1582             exit(EXIT_FAILURE);
1583         }
1584     }
1585 }
1586 
1587 void pc_guest_info_init(PCMachineState *pcms)
1588 {
1589     int i;
1590     MachineState *ms = MACHINE(pcms);
1591 
1592     pcms->apic_xrupt_override = kvm_allows_irq0_override();
1593     pcms->numa_nodes = ms->numa_state->num_nodes;
1594     pcms->node_mem = g_malloc0(pcms->numa_nodes *
1595                                     sizeof *pcms->node_mem);
1596     for (i = 0; i < ms->numa_state->num_nodes; i++) {
1597         pcms->node_mem[i] = ms->numa_state->nodes[i].node_mem;
1598     }
1599 
1600     pcms->machine_done.notify = pc_machine_done;
1601     qemu_add_machine_init_done_notifier(&pcms->machine_done);
1602 }
1603 
1604 /* setup pci memory address space mapping into system address space */
1605 void pc_pci_as_mapping_init(Object *owner, MemoryRegion *system_memory,
1606                             MemoryRegion *pci_address_space)
1607 {
1608     /* Set to lower priority than RAM */
1609     memory_region_add_subregion_overlap(system_memory, 0x0,
1610                                         pci_address_space, -1);
1611 }
1612 
1613 void xen_load_linux(PCMachineState *pcms)
1614 {
1615     int i;
1616     FWCfgState *fw_cfg;
1617 
1618     assert(MACHINE(pcms)->kernel_filename != NULL);
1619 
1620     fw_cfg = fw_cfg_init_io(FW_CFG_IO_BASE);
1621     fw_cfg_add_i16(fw_cfg, FW_CFG_NB_CPUS, pcms->boot_cpus);
1622     rom_set_fw(fw_cfg);
1623 
1624     load_linux(pcms, fw_cfg);
1625     for (i = 0; i < nb_option_roms; i++) {
1626         assert(!strcmp(option_rom[i].name, "linuxboot.bin") ||
1627                !strcmp(option_rom[i].name, "linuxboot_dma.bin") ||
1628                !strcmp(option_rom[i].name, "pvh.bin") ||
1629                !strcmp(option_rom[i].name, "multiboot.bin"));
1630         rom_add_option(option_rom[i].name, option_rom[i].bootindex);
1631     }
1632     pcms->fw_cfg = fw_cfg;
1633 }
1634 
1635 void pc_memory_init(PCMachineState *pcms,
1636                     MemoryRegion *system_memory,
1637                     MemoryRegion *rom_memory,
1638                     MemoryRegion **ram_memory)
1639 {
1640     int linux_boot, i;
1641     MemoryRegion *ram, *option_rom_mr;
1642     MemoryRegion *ram_below_4g, *ram_above_4g;
1643     FWCfgState *fw_cfg;
1644     MachineState *machine = MACHINE(pcms);
1645     MachineClass *mc = MACHINE_GET_CLASS(machine);
1646     PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms);
1647 
1648     assert(machine->ram_size == pcms->below_4g_mem_size +
1649                                 pcms->above_4g_mem_size);
1650 
1651     linux_boot = (machine->kernel_filename != NULL);
1652 
1653     /* Allocate RAM.  We allocate it as a single memory region and use
1654      * aliases to address portions of it, mostly for backwards compatibility
1655      * with older qemus that used qemu_ram_alloc().
1656      */
1657     ram = g_malloc(sizeof(*ram));
1658     memory_region_allocate_system_memory(ram, NULL, "pc.ram",
1659                                          machine->ram_size);
1660     *ram_memory = ram;
1661     ram_below_4g = g_malloc(sizeof(*ram_below_4g));
1662     memory_region_init_alias(ram_below_4g, NULL, "ram-below-4g", ram,
1663                              0, pcms->below_4g_mem_size);
1664     memory_region_add_subregion(system_memory, 0, ram_below_4g);
1665     e820_add_entry(0, pcms->below_4g_mem_size, E820_RAM);
1666     if (pcms->above_4g_mem_size > 0) {
1667         ram_above_4g = g_malloc(sizeof(*ram_above_4g));
1668         memory_region_init_alias(ram_above_4g, NULL, "ram-above-4g", ram,
1669                                  pcms->below_4g_mem_size,
1670                                  pcms->above_4g_mem_size);
1671         memory_region_add_subregion(system_memory, 0x100000000ULL,
1672                                     ram_above_4g);
1673         e820_add_entry(0x100000000ULL, pcms->above_4g_mem_size, E820_RAM);
1674     }
1675 
1676     if (!pcmc->has_reserved_memory &&
1677         (machine->ram_slots ||
1678          (machine->maxram_size > machine->ram_size))) {
1679 
1680         error_report("\"-memory 'slots|maxmem'\" is not supported by: %s",
1681                      mc->name);
1682         exit(EXIT_FAILURE);
1683     }
1684 
1685     /* always allocate the device memory information */
1686     machine->device_memory = g_malloc0(sizeof(*machine->device_memory));
1687 
1688     /* initialize device memory address space */
1689     if (pcmc->has_reserved_memory &&
1690         (machine->ram_size < machine->maxram_size)) {
1691         ram_addr_t device_mem_size = machine->maxram_size - machine->ram_size;
1692 
1693         if (machine->ram_slots > ACPI_MAX_RAM_SLOTS) {
1694             error_report("unsupported amount of memory slots: %"PRIu64,
1695                          machine->ram_slots);
1696             exit(EXIT_FAILURE);
1697         }
1698 
1699         if (QEMU_ALIGN_UP(machine->maxram_size,
1700                           TARGET_PAGE_SIZE) != machine->maxram_size) {
1701             error_report("maximum memory size must by aligned to multiple of "
1702                          "%d bytes", TARGET_PAGE_SIZE);
1703             exit(EXIT_FAILURE);
1704         }
1705 
1706         machine->device_memory->base =
1707             ROUND_UP(0x100000000ULL + pcms->above_4g_mem_size, 1 * GiB);
1708 
1709         if (pcmc->enforce_aligned_dimm) {
1710             /* size device region assuming 1G page max alignment per slot */
1711             device_mem_size += (1 * GiB) * machine->ram_slots;
1712         }
1713 
1714         if ((machine->device_memory->base + device_mem_size) <
1715             device_mem_size) {
1716             error_report("unsupported amount of maximum memory: " RAM_ADDR_FMT,
1717                          machine->maxram_size);
1718             exit(EXIT_FAILURE);
1719         }
1720 
1721         memory_region_init(&machine->device_memory->mr, OBJECT(pcms),
1722                            "device-memory", device_mem_size);
1723         memory_region_add_subregion(system_memory, machine->device_memory->base,
1724                                     &machine->device_memory->mr);
1725     }
1726 
1727     /* Initialize PC system firmware */
1728     pc_system_firmware_init(pcms, rom_memory);
1729 
1730     option_rom_mr = g_malloc(sizeof(*option_rom_mr));
1731     memory_region_init_ram(option_rom_mr, NULL, "pc.rom", PC_ROM_SIZE,
1732                            &error_fatal);
1733     if (pcmc->pci_enabled) {
1734         memory_region_set_readonly(option_rom_mr, true);
1735     }
1736     memory_region_add_subregion_overlap(rom_memory,
1737                                         PC_ROM_MIN_VGA,
1738                                         option_rom_mr,
1739                                         1);
1740 
1741     fw_cfg = fw_cfg_arch_create(machine,
1742                                 pcms->boot_cpus, pcms->apic_id_limit);
1743 
1744     rom_set_fw(fw_cfg);
1745 
1746     if (pcmc->has_reserved_memory && machine->device_memory->base) {
1747         uint64_t *val = g_malloc(sizeof(*val));
1748         PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms);
1749         uint64_t res_mem_end = machine->device_memory->base;
1750 
1751         if (!pcmc->broken_reserved_end) {
1752             res_mem_end += memory_region_size(&machine->device_memory->mr);
1753         }
1754         *val = cpu_to_le64(ROUND_UP(res_mem_end, 1 * GiB));
1755         fw_cfg_add_file(fw_cfg, "etc/reserved-memory-end", val, sizeof(*val));
1756     }
1757 
1758     if (linux_boot) {
1759         load_linux(pcms, fw_cfg);
1760     }
1761 
1762     for (i = 0; i < nb_option_roms; i++) {
1763         rom_add_option(option_rom[i].name, option_rom[i].bootindex);
1764     }
1765     pcms->fw_cfg = fw_cfg;
1766 
1767     /* Init default IOAPIC address space */
1768     pcms->ioapic_as = &address_space_memory;
1769 }
1770 
1771 /*
1772  * The 64bit pci hole starts after "above 4G RAM" and
1773  * potentially the space reserved for memory hotplug.
1774  */
1775 uint64_t pc_pci_hole64_start(void)
1776 {
1777     PCMachineState *pcms = PC_MACHINE(qdev_get_machine());
1778     PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms);
1779     MachineState *ms = MACHINE(pcms);
1780     uint64_t hole64_start = 0;
1781 
1782     if (pcmc->has_reserved_memory && ms->device_memory->base) {
1783         hole64_start = ms->device_memory->base;
1784         if (!pcmc->broken_reserved_end) {
1785             hole64_start += memory_region_size(&ms->device_memory->mr);
1786         }
1787     } else {
1788         hole64_start = 0x100000000ULL + pcms->above_4g_mem_size;
1789     }
1790 
1791     return ROUND_UP(hole64_start, 1 * GiB);
1792 }
1793 
1794 qemu_irq pc_allocate_cpu_irq(void)
1795 {
1796     return qemu_allocate_irq(pic_irq_request, NULL, 0);
1797 }
1798 
1799 DeviceState *pc_vga_init(ISABus *isa_bus, PCIBus *pci_bus)
1800 {
1801     DeviceState *dev = NULL;
1802 
1803     rom_set_order_override(FW_CFG_ORDER_OVERRIDE_VGA);
1804     if (pci_bus) {
1805         PCIDevice *pcidev = pci_vga_init(pci_bus);
1806         dev = pcidev ? &pcidev->qdev : NULL;
1807     } else if (isa_bus) {
1808         ISADevice *isadev = isa_vga_init(isa_bus);
1809         dev = isadev ? DEVICE(isadev) : NULL;
1810     }
1811     rom_reset_order_override();
1812     return dev;
1813 }
1814 
1815 static const MemoryRegionOps ioport80_io_ops = {
1816     .write = ioport80_write,
1817     .read = ioport80_read,
1818     .endianness = DEVICE_NATIVE_ENDIAN,
1819     .impl = {
1820         .min_access_size = 1,
1821         .max_access_size = 1,
1822     },
1823 };
1824 
1825 static const MemoryRegionOps ioportF0_io_ops = {
1826     .write = ioportF0_write,
1827     .read = ioportF0_read,
1828     .endianness = DEVICE_NATIVE_ENDIAN,
1829     .impl = {
1830         .min_access_size = 1,
1831         .max_access_size = 1,
1832     },
1833 };
1834 
1835 static void pc_superio_init(ISABus *isa_bus, bool create_fdctrl, bool no_vmport)
1836 {
1837     int i;
1838     DriveInfo *fd[MAX_FD];
1839     qemu_irq *a20_line;
1840     ISADevice *i8042, *port92, *vmmouse;
1841 
1842     serial_hds_isa_init(isa_bus, 0, MAX_ISA_SERIAL_PORTS);
1843     parallel_hds_isa_init(isa_bus, MAX_PARALLEL_PORTS);
1844 
1845     for (i = 0; i < MAX_FD; i++) {
1846         fd[i] = drive_get(IF_FLOPPY, 0, i);
1847         create_fdctrl |= !!fd[i];
1848     }
1849     if (create_fdctrl) {
1850         fdctrl_init_isa(isa_bus, fd);
1851     }
1852 
1853     i8042 = isa_create_simple(isa_bus, "i8042");
1854     if (!no_vmport) {
1855         vmport_init(isa_bus);
1856         vmmouse = isa_try_create(isa_bus, "vmmouse");
1857     } else {
1858         vmmouse = NULL;
1859     }
1860     if (vmmouse) {
1861         DeviceState *dev = DEVICE(vmmouse);
1862         qdev_prop_set_ptr(dev, "ps2_mouse", i8042);
1863         qdev_init_nofail(dev);
1864     }
1865     port92 = isa_create_simple(isa_bus, "port92");
1866 
1867     a20_line = qemu_allocate_irqs(handle_a20_line_change, first_cpu, 2);
1868     i8042_setup_a20_line(i8042, a20_line[0]);
1869     port92_init(port92, a20_line[1]);
1870     g_free(a20_line);
1871 }
1872 
1873 void pc_basic_device_init(ISABus *isa_bus, qemu_irq *gsi,
1874                           ISADevice **rtc_state,
1875                           bool create_fdctrl,
1876                           bool no_vmport,
1877                           bool has_pit,
1878                           uint32_t hpet_irqs)
1879 {
1880     int i;
1881     DeviceState *hpet = NULL;
1882     int pit_isa_irq = 0;
1883     qemu_irq pit_alt_irq = NULL;
1884     qemu_irq rtc_irq = NULL;
1885     ISADevice *pit = NULL;
1886     MemoryRegion *ioport80_io = g_new(MemoryRegion, 1);
1887     MemoryRegion *ioportF0_io = g_new(MemoryRegion, 1);
1888 
1889     memory_region_init_io(ioport80_io, NULL, &ioport80_io_ops, NULL, "ioport80", 1);
1890     memory_region_add_subregion(isa_bus->address_space_io, 0x80, ioport80_io);
1891 
1892     memory_region_init_io(ioportF0_io, NULL, &ioportF0_io_ops, NULL, "ioportF0", 1);
1893     memory_region_add_subregion(isa_bus->address_space_io, 0xf0, ioportF0_io);
1894 
1895     /*
1896      * Check if an HPET shall be created.
1897      *
1898      * Without KVM_CAP_PIT_STATE2, we cannot switch off the in-kernel PIT
1899      * when the HPET wants to take over. Thus we have to disable the latter.
1900      */
1901     if (!no_hpet && (!kvm_irqchip_in_kernel() || kvm_has_pit_state2())) {
1902         /* In order to set property, here not using sysbus_try_create_simple */
1903         hpet = qdev_try_create(NULL, TYPE_HPET);
1904         if (hpet) {
1905             /* For pc-piix-*, hpet's intcap is always IRQ2. For pc-q35-1.7
1906              * and earlier, use IRQ2 for compat. Otherwise, use IRQ16~23,
1907              * IRQ8 and IRQ2.
1908              */
1909             uint8_t compat = object_property_get_uint(OBJECT(hpet),
1910                     HPET_INTCAP, NULL);
1911             if (!compat) {
1912                 qdev_prop_set_uint32(hpet, HPET_INTCAP, hpet_irqs);
1913             }
1914             qdev_init_nofail(hpet);
1915             sysbus_mmio_map(SYS_BUS_DEVICE(hpet), 0, HPET_BASE);
1916 
1917             for (i = 0; i < GSI_NUM_PINS; i++) {
1918                 sysbus_connect_irq(SYS_BUS_DEVICE(hpet), i, gsi[i]);
1919             }
1920             pit_isa_irq = -1;
1921             pit_alt_irq = qdev_get_gpio_in(hpet, HPET_LEGACY_PIT_INT);
1922             rtc_irq = qdev_get_gpio_in(hpet, HPET_LEGACY_RTC_INT);
1923         }
1924     }
1925     *rtc_state = mc146818_rtc_init(isa_bus, 2000, rtc_irq);
1926 
1927     qemu_register_boot_set(pc_boot_set, *rtc_state);
1928 
1929     if (!xen_enabled() && has_pit) {
1930         if (kvm_pit_in_kernel()) {
1931             pit = kvm_pit_init(isa_bus, 0x40);
1932         } else {
1933             pit = i8254_pit_init(isa_bus, 0x40, pit_isa_irq, pit_alt_irq);
1934         }
1935         if (hpet) {
1936             /* connect PIT to output control line of the HPET */
1937             qdev_connect_gpio_out(hpet, 0, qdev_get_gpio_in(DEVICE(pit), 0));
1938         }
1939         pcspk_init(isa_bus, pit);
1940     }
1941 
1942     i8257_dma_init(isa_bus, 0);
1943 
1944     /* Super I/O */
1945     pc_superio_init(isa_bus, create_fdctrl, no_vmport);
1946 }
1947 
1948 void pc_nic_init(PCMachineClass *pcmc, ISABus *isa_bus, PCIBus *pci_bus)
1949 {
1950     int i;
1951 
1952     rom_set_order_override(FW_CFG_ORDER_OVERRIDE_NIC);
1953     for (i = 0; i < nb_nics; i++) {
1954         NICInfo *nd = &nd_table[i];
1955         const char *model = nd->model ? nd->model : pcmc->default_nic_model;
1956 
1957         if (g_str_equal(model, "ne2k_isa")) {
1958             pc_init_ne2k_isa(isa_bus, nd);
1959         } else {
1960             pci_nic_init_nofail(nd, pci_bus, model, NULL);
1961         }
1962     }
1963     rom_reset_order_override();
1964 }
1965 
1966 void ioapic_init_gsi(GSIState *gsi_state, const char *parent_name)
1967 {
1968     DeviceState *dev;
1969     SysBusDevice *d;
1970     unsigned int i;
1971 
1972     if (kvm_ioapic_in_kernel()) {
1973         dev = qdev_create(NULL, TYPE_KVM_IOAPIC);
1974     } else {
1975         dev = qdev_create(NULL, TYPE_IOAPIC);
1976     }
1977     if (parent_name) {
1978         object_property_add_child(object_resolve_path(parent_name, NULL),
1979                                   "ioapic", OBJECT(dev), NULL);
1980     }
1981     qdev_init_nofail(dev);
1982     d = SYS_BUS_DEVICE(dev);
1983     sysbus_mmio_map(d, 0, IO_APIC_DEFAULT_ADDRESS);
1984 
1985     for (i = 0; i < IOAPIC_NUM_PINS; i++) {
1986         gsi_state->ioapic_irq[i] = qdev_get_gpio_in(dev, i);
1987     }
1988 }
1989 
1990 static void pc_memory_pre_plug(HotplugHandler *hotplug_dev, DeviceState *dev,
1991                                Error **errp)
1992 {
1993     const PCMachineState *pcms = PC_MACHINE(hotplug_dev);
1994     const PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms);
1995     const MachineState *ms = MACHINE(hotplug_dev);
1996     const bool is_nvdimm = object_dynamic_cast(OBJECT(dev), TYPE_NVDIMM);
1997     const uint64_t legacy_align = TARGET_PAGE_SIZE;
1998     Error *local_err = NULL;
1999 
2000     /*
2001      * When -no-acpi is used with Q35 machine type, no ACPI is built,
2002      * but pcms->acpi_dev is still created. Check !acpi_enabled in
2003      * addition to cover this case.
2004      */
2005     if (!pcms->acpi_dev || !acpi_enabled) {
2006         error_setg(errp,
2007                    "memory hotplug is not enabled: missing acpi device or acpi disabled");
2008         return;
2009     }
2010 
2011     if (is_nvdimm && !ms->nvdimms_state->is_enabled) {
2012         error_setg(errp, "nvdimm is not enabled: missing 'nvdimm' in '-M'");
2013         return;
2014     }
2015 
2016     hotplug_handler_pre_plug(pcms->acpi_dev, dev, &local_err);
2017     if (local_err) {
2018         error_propagate(errp, local_err);
2019         return;
2020     }
2021 
2022     pc_dimm_pre_plug(PC_DIMM(dev), MACHINE(hotplug_dev),
2023                      pcmc->enforce_aligned_dimm ? NULL : &legacy_align, errp);
2024 }
2025 
2026 static void pc_memory_plug(HotplugHandler *hotplug_dev,
2027                            DeviceState *dev, Error **errp)
2028 {
2029     Error *local_err = NULL;
2030     PCMachineState *pcms = PC_MACHINE(hotplug_dev);
2031     MachineState *ms = MACHINE(hotplug_dev);
2032     bool is_nvdimm = object_dynamic_cast(OBJECT(dev), TYPE_NVDIMM);
2033 
2034     pc_dimm_plug(PC_DIMM(dev), MACHINE(pcms), &local_err);
2035     if (local_err) {
2036         goto out;
2037     }
2038 
2039     if (is_nvdimm) {
2040         nvdimm_plug(ms->nvdimms_state);
2041     }
2042 
2043     hotplug_handler_plug(HOTPLUG_HANDLER(pcms->acpi_dev), dev, &error_abort);
2044 out:
2045     error_propagate(errp, local_err);
2046 }
2047 
2048 static void pc_memory_unplug_request(HotplugHandler *hotplug_dev,
2049                                      DeviceState *dev, Error **errp)
2050 {
2051     Error *local_err = NULL;
2052     PCMachineState *pcms = PC_MACHINE(hotplug_dev);
2053 
2054     /*
2055      * When -no-acpi is used with Q35 machine type, no ACPI is built,
2056      * but pcms->acpi_dev is still created. Check !acpi_enabled in
2057      * addition to cover this case.
2058      */
2059     if (!pcms->acpi_dev || !acpi_enabled) {
2060         error_setg(&local_err,
2061                    "memory hotplug is not enabled: missing acpi device or acpi disabled");
2062         goto out;
2063     }
2064 
2065     if (object_dynamic_cast(OBJECT(dev), TYPE_NVDIMM)) {
2066         error_setg(&local_err,
2067                    "nvdimm device hot unplug is not supported yet.");
2068         goto out;
2069     }
2070 
2071     hotplug_handler_unplug_request(HOTPLUG_HANDLER(pcms->acpi_dev), dev,
2072                                    &local_err);
2073 out:
2074     error_propagate(errp, local_err);
2075 }
2076 
2077 static void pc_memory_unplug(HotplugHandler *hotplug_dev,
2078                              DeviceState *dev, Error **errp)
2079 {
2080     PCMachineState *pcms = PC_MACHINE(hotplug_dev);
2081     Error *local_err = NULL;
2082 
2083     hotplug_handler_unplug(HOTPLUG_HANDLER(pcms->acpi_dev), dev, &local_err);
2084     if (local_err) {
2085         goto out;
2086     }
2087 
2088     pc_dimm_unplug(PC_DIMM(dev), MACHINE(pcms));
2089     object_property_set_bool(OBJECT(dev), false, "realized", NULL);
2090  out:
2091     error_propagate(errp, local_err);
2092 }
2093 
2094 static int pc_apic_cmp(const void *a, const void *b)
2095 {
2096    CPUArchId *apic_a = (CPUArchId *)a;
2097    CPUArchId *apic_b = (CPUArchId *)b;
2098 
2099    return apic_a->arch_id - apic_b->arch_id;
2100 }
2101 
2102 /* returns pointer to CPUArchId descriptor that matches CPU's apic_id
2103  * in ms->possible_cpus->cpus, if ms->possible_cpus->cpus has no
2104  * entry corresponding to CPU's apic_id returns NULL.
2105  */
2106 static CPUArchId *pc_find_cpu_slot(MachineState *ms, uint32_t id, int *idx)
2107 {
2108     CPUArchId apic_id, *found_cpu;
2109 
2110     apic_id.arch_id = id;
2111     found_cpu = bsearch(&apic_id, ms->possible_cpus->cpus,
2112         ms->possible_cpus->len, sizeof(*ms->possible_cpus->cpus),
2113         pc_apic_cmp);
2114     if (found_cpu && idx) {
2115         *idx = found_cpu - ms->possible_cpus->cpus;
2116     }
2117     return found_cpu;
2118 }
2119 
2120 static void pc_cpu_plug(HotplugHandler *hotplug_dev,
2121                         DeviceState *dev, Error **errp)
2122 {
2123     CPUArchId *found_cpu;
2124     Error *local_err = NULL;
2125     X86CPU *cpu = X86_CPU(dev);
2126     PCMachineState *pcms = PC_MACHINE(hotplug_dev);
2127 
2128     if (pcms->acpi_dev) {
2129         hotplug_handler_plug(HOTPLUG_HANDLER(pcms->acpi_dev), dev, &local_err);
2130         if (local_err) {
2131             goto out;
2132         }
2133     }
2134 
2135     /* increment the number of CPUs */
2136     pcms->boot_cpus++;
2137     if (pcms->rtc) {
2138         rtc_set_cpus_count(pcms->rtc, pcms->boot_cpus);
2139     }
2140     if (pcms->fw_cfg) {
2141         fw_cfg_modify_i16(pcms->fw_cfg, FW_CFG_NB_CPUS, pcms->boot_cpus);
2142     }
2143 
2144     found_cpu = pc_find_cpu_slot(MACHINE(pcms), cpu->apic_id, NULL);
2145     found_cpu->cpu = OBJECT(dev);
2146 out:
2147     error_propagate(errp, local_err);
2148 }
2149 static void pc_cpu_unplug_request_cb(HotplugHandler *hotplug_dev,
2150                                      DeviceState *dev, Error **errp)
2151 {
2152     int idx = -1;
2153     Error *local_err = NULL;
2154     X86CPU *cpu = X86_CPU(dev);
2155     PCMachineState *pcms = PC_MACHINE(hotplug_dev);
2156 
2157     if (!pcms->acpi_dev) {
2158         error_setg(&local_err, "CPU hot unplug not supported without ACPI");
2159         goto out;
2160     }
2161 
2162     pc_find_cpu_slot(MACHINE(pcms), cpu->apic_id, &idx);
2163     assert(idx != -1);
2164     if (idx == 0) {
2165         error_setg(&local_err, "Boot CPU is unpluggable");
2166         goto out;
2167     }
2168 
2169     hotplug_handler_unplug_request(HOTPLUG_HANDLER(pcms->acpi_dev), dev,
2170                                    &local_err);
2171     if (local_err) {
2172         goto out;
2173     }
2174 
2175  out:
2176     error_propagate(errp, local_err);
2177 
2178 }
2179 
2180 static void pc_cpu_unplug_cb(HotplugHandler *hotplug_dev,
2181                              DeviceState *dev, Error **errp)
2182 {
2183     CPUArchId *found_cpu;
2184     Error *local_err = NULL;
2185     X86CPU *cpu = X86_CPU(dev);
2186     PCMachineState *pcms = PC_MACHINE(hotplug_dev);
2187 
2188     hotplug_handler_unplug(HOTPLUG_HANDLER(pcms->acpi_dev), dev, &local_err);
2189     if (local_err) {
2190         goto out;
2191     }
2192 
2193     found_cpu = pc_find_cpu_slot(MACHINE(pcms), cpu->apic_id, NULL);
2194     found_cpu->cpu = NULL;
2195     object_property_set_bool(OBJECT(dev), false, "realized", NULL);
2196 
2197     /* decrement the number of CPUs */
2198     pcms->boot_cpus--;
2199     /* Update the number of CPUs in CMOS */
2200     rtc_set_cpus_count(pcms->rtc, pcms->boot_cpus);
2201     fw_cfg_modify_i16(pcms->fw_cfg, FW_CFG_NB_CPUS, pcms->boot_cpus);
2202  out:
2203     error_propagate(errp, local_err);
2204 }
2205 
2206 static void pc_cpu_pre_plug(HotplugHandler *hotplug_dev,
2207                             DeviceState *dev, Error **errp)
2208 {
2209     int idx;
2210     CPUState *cs;
2211     CPUArchId *cpu_slot;
2212     X86CPUTopoInfo topo;
2213     X86CPU *cpu = X86_CPU(dev);
2214     CPUX86State *env = &cpu->env;
2215     MachineState *ms = MACHINE(hotplug_dev);
2216     PCMachineState *pcms = PC_MACHINE(hotplug_dev);
2217     unsigned int smp_cores = ms->smp.cores;
2218     unsigned int smp_threads = ms->smp.threads;
2219 
2220     if(!object_dynamic_cast(OBJECT(cpu), ms->cpu_type)) {
2221         error_setg(errp, "Invalid CPU type, expected cpu type: '%s'",
2222                    ms->cpu_type);
2223         return;
2224     }
2225 
2226     env->nr_dies = pcms->smp_dies;
2227 
2228     /*
2229      * If APIC ID is not set,
2230      * set it based on socket/die/core/thread properties.
2231      */
2232     if (cpu->apic_id == UNASSIGNED_APIC_ID) {
2233         int max_socket = (ms->smp.max_cpus - 1) /
2234                                 smp_threads / smp_cores / pcms->smp_dies;
2235 
2236         /*
2237          * die-id was optional in QEMU 4.0 and older, so keep it optional
2238          * if there's only one die per socket.
2239          */
2240         if (cpu->die_id < 0 && pcms->smp_dies == 1) {
2241             cpu->die_id = 0;
2242         }
2243 
2244         if (cpu->socket_id < 0) {
2245             error_setg(errp, "CPU socket-id is not set");
2246             return;
2247         } else if (cpu->socket_id > max_socket) {
2248             error_setg(errp, "Invalid CPU socket-id: %u must be in range 0:%u",
2249                        cpu->socket_id, max_socket);
2250             return;
2251         }
2252         if (cpu->die_id < 0) {
2253             error_setg(errp, "CPU die-id is not set");
2254             return;
2255         } else if (cpu->die_id > pcms->smp_dies - 1) {
2256             error_setg(errp, "Invalid CPU die-id: %u must be in range 0:%u",
2257                        cpu->die_id, pcms->smp_dies - 1);
2258             return;
2259         }
2260         if (cpu->core_id < 0) {
2261             error_setg(errp, "CPU core-id is not set");
2262             return;
2263         } else if (cpu->core_id > (smp_cores - 1)) {
2264             error_setg(errp, "Invalid CPU core-id: %u must be in range 0:%u",
2265                        cpu->core_id, smp_cores - 1);
2266             return;
2267         }
2268         if (cpu->thread_id < 0) {
2269             error_setg(errp, "CPU thread-id is not set");
2270             return;
2271         } else if (cpu->thread_id > (smp_threads - 1)) {
2272             error_setg(errp, "Invalid CPU thread-id: %u must be in range 0:%u",
2273                        cpu->thread_id, smp_threads - 1);
2274             return;
2275         }
2276 
2277         topo.pkg_id = cpu->socket_id;
2278         topo.die_id = cpu->die_id;
2279         topo.core_id = cpu->core_id;
2280         topo.smt_id = cpu->thread_id;
2281         cpu->apic_id = apicid_from_topo_ids(pcms->smp_dies, smp_cores,
2282                                             smp_threads, &topo);
2283     }
2284 
2285     cpu_slot = pc_find_cpu_slot(MACHINE(pcms), cpu->apic_id, &idx);
2286     if (!cpu_slot) {
2287         MachineState *ms = MACHINE(pcms);
2288 
2289         x86_topo_ids_from_apicid(cpu->apic_id, pcms->smp_dies,
2290                                  smp_cores, smp_threads, &topo);
2291         error_setg(errp,
2292             "Invalid CPU [socket: %u, die: %u, core: %u, thread: %u] with"
2293             " APIC ID %" PRIu32 ", valid index range 0:%d",
2294             topo.pkg_id, topo.die_id, topo.core_id, topo.smt_id,
2295             cpu->apic_id, ms->possible_cpus->len - 1);
2296         return;
2297     }
2298 
2299     if (cpu_slot->cpu) {
2300         error_setg(errp, "CPU[%d] with APIC ID %" PRIu32 " exists",
2301                    idx, cpu->apic_id);
2302         return;
2303     }
2304 
2305     /* if 'address' properties socket-id/core-id/thread-id are not set, set them
2306      * so that machine_query_hotpluggable_cpus would show correct values
2307      */
2308     /* TODO: move socket_id/core_id/thread_id checks into x86_cpu_realizefn()
2309      * once -smp refactoring is complete and there will be CPU private
2310      * CPUState::nr_cores and CPUState::nr_threads fields instead of globals */
2311     x86_topo_ids_from_apicid(cpu->apic_id, pcms->smp_dies,
2312                              smp_cores, smp_threads, &topo);
2313     if (cpu->socket_id != -1 && cpu->socket_id != topo.pkg_id) {
2314         error_setg(errp, "property socket-id: %u doesn't match set apic-id:"
2315             " 0x%x (socket-id: %u)", cpu->socket_id, cpu->apic_id, topo.pkg_id);
2316         return;
2317     }
2318     cpu->socket_id = topo.pkg_id;
2319 
2320     if (cpu->die_id != -1 && cpu->die_id != topo.die_id) {
2321         error_setg(errp, "property die-id: %u doesn't match set apic-id:"
2322             " 0x%x (die-id: %u)", cpu->die_id, cpu->apic_id, topo.die_id);
2323         return;
2324     }
2325     cpu->die_id = topo.die_id;
2326 
2327     if (cpu->core_id != -1 && cpu->core_id != topo.core_id) {
2328         error_setg(errp, "property core-id: %u doesn't match set apic-id:"
2329             " 0x%x (core-id: %u)", cpu->core_id, cpu->apic_id, topo.core_id);
2330         return;
2331     }
2332     cpu->core_id = topo.core_id;
2333 
2334     if (cpu->thread_id != -1 && cpu->thread_id != topo.smt_id) {
2335         error_setg(errp, "property thread-id: %u doesn't match set apic-id:"
2336             " 0x%x (thread-id: %u)", cpu->thread_id, cpu->apic_id, topo.smt_id);
2337         return;
2338     }
2339     cpu->thread_id = topo.smt_id;
2340 
2341     if (hyperv_feat_enabled(cpu, HYPERV_FEAT_VPINDEX) &&
2342         !kvm_hv_vpindex_settable()) {
2343         error_setg(errp, "kernel doesn't allow setting HyperV VP_INDEX");
2344         return;
2345     }
2346 
2347     cs = CPU(cpu);
2348     cs->cpu_index = idx;
2349 
2350     numa_cpu_pre_plug(cpu_slot, dev, errp);
2351 }
2352 
2353 static void pc_virtio_pmem_pci_pre_plug(HotplugHandler *hotplug_dev,
2354                                         DeviceState *dev, Error **errp)
2355 {
2356     HotplugHandler *hotplug_dev2 = qdev_get_bus_hotplug_handler(dev);
2357     Error *local_err = NULL;
2358 
2359     if (!hotplug_dev2) {
2360         /*
2361          * Without a bus hotplug handler, we cannot control the plug/unplug
2362          * order. This should never be the case on x86, however better add
2363          * a safety net.
2364          */
2365         error_setg(errp, "virtio-pmem-pci not supported on this bus.");
2366         return;
2367     }
2368     /*
2369      * First, see if we can plug this memory device at all. If that
2370      * succeeds, branch of to the actual hotplug handler.
2371      */
2372     memory_device_pre_plug(MEMORY_DEVICE(dev), MACHINE(hotplug_dev), NULL,
2373                            &local_err);
2374     if (!local_err) {
2375         hotplug_handler_pre_plug(hotplug_dev2, dev, &local_err);
2376     }
2377     error_propagate(errp, local_err);
2378 }
2379 
2380 static void pc_virtio_pmem_pci_plug(HotplugHandler *hotplug_dev,
2381                                     DeviceState *dev, Error **errp)
2382 {
2383     HotplugHandler *hotplug_dev2 = qdev_get_bus_hotplug_handler(dev);
2384     Error *local_err = NULL;
2385 
2386     /*
2387      * Plug the memory device first and then branch off to the actual
2388      * hotplug handler. If that one fails, we can easily undo the memory
2389      * device bits.
2390      */
2391     memory_device_plug(MEMORY_DEVICE(dev), MACHINE(hotplug_dev));
2392     hotplug_handler_plug(hotplug_dev2, dev, &local_err);
2393     if (local_err) {
2394         memory_device_unplug(MEMORY_DEVICE(dev), MACHINE(hotplug_dev));
2395     }
2396     error_propagate(errp, local_err);
2397 }
2398 
2399 static void pc_virtio_pmem_pci_unplug_request(HotplugHandler *hotplug_dev,
2400                                               DeviceState *dev, Error **errp)
2401 {
2402     /* We don't support virtio pmem hot unplug */
2403     error_setg(errp, "virtio pmem device unplug not supported.");
2404 }
2405 
2406 static void pc_virtio_pmem_pci_unplug(HotplugHandler *hotplug_dev,
2407                                       DeviceState *dev, Error **errp)
2408 {
2409     /* We don't support virtio pmem hot unplug */
2410 }
2411 
2412 static void pc_machine_device_pre_plug_cb(HotplugHandler *hotplug_dev,
2413                                           DeviceState *dev, Error **errp)
2414 {
2415     if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) {
2416         pc_memory_pre_plug(hotplug_dev, dev, errp);
2417     } else if (object_dynamic_cast(OBJECT(dev), TYPE_CPU)) {
2418         pc_cpu_pre_plug(hotplug_dev, dev, errp);
2419     } else if (object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_PMEM_PCI)) {
2420         pc_virtio_pmem_pci_pre_plug(hotplug_dev, dev, errp);
2421     }
2422 }
2423 
2424 static void pc_machine_device_plug_cb(HotplugHandler *hotplug_dev,
2425                                       DeviceState *dev, Error **errp)
2426 {
2427     if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) {
2428         pc_memory_plug(hotplug_dev, dev, errp);
2429     } else if (object_dynamic_cast(OBJECT(dev), TYPE_CPU)) {
2430         pc_cpu_plug(hotplug_dev, dev, errp);
2431     } else if (object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_PMEM_PCI)) {
2432         pc_virtio_pmem_pci_plug(hotplug_dev, dev, errp);
2433     }
2434 }
2435 
2436 static void pc_machine_device_unplug_request_cb(HotplugHandler *hotplug_dev,
2437                                                 DeviceState *dev, Error **errp)
2438 {
2439     if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) {
2440         pc_memory_unplug_request(hotplug_dev, dev, errp);
2441     } else if (object_dynamic_cast(OBJECT(dev), TYPE_CPU)) {
2442         pc_cpu_unplug_request_cb(hotplug_dev, dev, errp);
2443     } else if (object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_PMEM_PCI)) {
2444         pc_virtio_pmem_pci_unplug_request(hotplug_dev, dev, errp);
2445     } else {
2446         error_setg(errp, "acpi: device unplug request for not supported device"
2447                    " type: %s", object_get_typename(OBJECT(dev)));
2448     }
2449 }
2450 
2451 static void pc_machine_device_unplug_cb(HotplugHandler *hotplug_dev,
2452                                         DeviceState *dev, Error **errp)
2453 {
2454     if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) {
2455         pc_memory_unplug(hotplug_dev, dev, errp);
2456     } else if (object_dynamic_cast(OBJECT(dev), TYPE_CPU)) {
2457         pc_cpu_unplug_cb(hotplug_dev, dev, errp);
2458     } else if (object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_PMEM_PCI)) {
2459         pc_virtio_pmem_pci_unplug(hotplug_dev, dev, errp);
2460     } else {
2461         error_setg(errp, "acpi: device unplug for not supported device"
2462                    " type: %s", object_get_typename(OBJECT(dev)));
2463     }
2464 }
2465 
2466 static HotplugHandler *pc_get_hotplug_handler(MachineState *machine,
2467                                              DeviceState *dev)
2468 {
2469     if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM) ||
2470         object_dynamic_cast(OBJECT(dev), TYPE_CPU) ||
2471         object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_PMEM_PCI)) {
2472         return HOTPLUG_HANDLER(machine);
2473     }
2474 
2475     return NULL;
2476 }
2477 
2478 static void
2479 pc_machine_get_device_memory_region_size(Object *obj, Visitor *v,
2480                                          const char *name, void *opaque,
2481                                          Error **errp)
2482 {
2483     MachineState *ms = MACHINE(obj);
2484     int64_t value = 0;
2485 
2486     if (ms->device_memory) {
2487         value = memory_region_size(&ms->device_memory->mr);
2488     }
2489 
2490     visit_type_int(v, name, &value, errp);
2491 }
2492 
2493 static void pc_machine_get_max_ram_below_4g(Object *obj, Visitor *v,
2494                                             const char *name, void *opaque,
2495                                             Error **errp)
2496 {
2497     PCMachineState *pcms = PC_MACHINE(obj);
2498     uint64_t value = pcms->max_ram_below_4g;
2499 
2500     visit_type_size(v, name, &value, errp);
2501 }
2502 
2503 static void pc_machine_set_max_ram_below_4g(Object *obj, Visitor *v,
2504                                             const char *name, void *opaque,
2505                                             Error **errp)
2506 {
2507     PCMachineState *pcms = PC_MACHINE(obj);
2508     Error *error = NULL;
2509     uint64_t value;
2510 
2511     visit_type_size(v, name, &value, &error);
2512     if (error) {
2513         error_propagate(errp, error);
2514         return;
2515     }
2516     if (value > 4 * GiB) {
2517         error_setg(&error,
2518                    "Machine option 'max-ram-below-4g=%"PRIu64
2519                    "' expects size less than or equal to 4G", value);
2520         error_propagate(errp, error);
2521         return;
2522     }
2523 
2524     if (value < 1 * MiB) {
2525         warn_report("Only %" PRIu64 " bytes of RAM below the 4GiB boundary,"
2526                     "BIOS may not work with less than 1MiB", value);
2527     }
2528 
2529     pcms->max_ram_below_4g = value;
2530 }
2531 
2532 static void pc_machine_get_vmport(Object *obj, Visitor *v, const char *name,
2533                                   void *opaque, Error **errp)
2534 {
2535     PCMachineState *pcms = PC_MACHINE(obj);
2536     OnOffAuto vmport = pcms->vmport;
2537 
2538     visit_type_OnOffAuto(v, name, &vmport, errp);
2539 }
2540 
2541 static void pc_machine_set_vmport(Object *obj, Visitor *v, const char *name,
2542                                   void *opaque, Error **errp)
2543 {
2544     PCMachineState *pcms = PC_MACHINE(obj);
2545 
2546     visit_type_OnOffAuto(v, name, &pcms->vmport, errp);
2547 }
2548 
2549 bool pc_machine_is_smm_enabled(PCMachineState *pcms)
2550 {
2551     bool smm_available = false;
2552 
2553     if (pcms->smm == ON_OFF_AUTO_OFF) {
2554         return false;
2555     }
2556 
2557     if (tcg_enabled() || qtest_enabled()) {
2558         smm_available = true;
2559     } else if (kvm_enabled()) {
2560         smm_available = kvm_has_smm();
2561     }
2562 
2563     if (smm_available) {
2564         return true;
2565     }
2566 
2567     if (pcms->smm == ON_OFF_AUTO_ON) {
2568         error_report("System Management Mode not supported by this hypervisor.");
2569         exit(1);
2570     }
2571     return false;
2572 }
2573 
2574 static void pc_machine_get_smm(Object *obj, Visitor *v, const char *name,
2575                                void *opaque, Error **errp)
2576 {
2577     PCMachineState *pcms = PC_MACHINE(obj);
2578     OnOffAuto smm = pcms->smm;
2579 
2580     visit_type_OnOffAuto(v, name, &smm, errp);
2581 }
2582 
2583 static void pc_machine_set_smm(Object *obj, Visitor *v, const char *name,
2584                                void *opaque, Error **errp)
2585 {
2586     PCMachineState *pcms = PC_MACHINE(obj);
2587 
2588     visit_type_OnOffAuto(v, name, &pcms->smm, errp);
2589 }
2590 
2591 static bool pc_machine_get_smbus(Object *obj, Error **errp)
2592 {
2593     PCMachineState *pcms = PC_MACHINE(obj);
2594 
2595     return pcms->smbus_enabled;
2596 }
2597 
2598 static void pc_machine_set_smbus(Object *obj, bool value, Error **errp)
2599 {
2600     PCMachineState *pcms = PC_MACHINE(obj);
2601 
2602     pcms->smbus_enabled = value;
2603 }
2604 
2605 static bool pc_machine_get_sata(Object *obj, Error **errp)
2606 {
2607     PCMachineState *pcms = PC_MACHINE(obj);
2608 
2609     return pcms->sata_enabled;
2610 }
2611 
2612 static void pc_machine_set_sata(Object *obj, bool value, Error **errp)
2613 {
2614     PCMachineState *pcms = PC_MACHINE(obj);
2615 
2616     pcms->sata_enabled = value;
2617 }
2618 
2619 static bool pc_machine_get_pit(Object *obj, Error **errp)
2620 {
2621     PCMachineState *pcms = PC_MACHINE(obj);
2622 
2623     return pcms->pit_enabled;
2624 }
2625 
2626 static void pc_machine_set_pit(Object *obj, bool value, Error **errp)
2627 {
2628     PCMachineState *pcms = PC_MACHINE(obj);
2629 
2630     pcms->pit_enabled = value;
2631 }
2632 
2633 static void pc_machine_initfn(Object *obj)
2634 {
2635     PCMachineState *pcms = PC_MACHINE(obj);
2636 
2637     pcms->max_ram_below_4g = 0; /* use default */
2638     pcms->smm = ON_OFF_AUTO_AUTO;
2639 #ifdef CONFIG_VMPORT
2640     pcms->vmport = ON_OFF_AUTO_AUTO;
2641 #else
2642     pcms->vmport = ON_OFF_AUTO_OFF;
2643 #endif /* CONFIG_VMPORT */
2644     /* acpi build is enabled by default if machine supports it */
2645     pcms->acpi_build_enabled = PC_MACHINE_GET_CLASS(pcms)->has_acpi_build;
2646     pcms->smbus_enabled = true;
2647     pcms->sata_enabled = true;
2648     pcms->pit_enabled = true;
2649     pcms->smp_dies = 1;
2650 
2651     pc_system_flash_create(pcms);
2652 }
2653 
2654 static void pc_machine_reset(MachineState *machine)
2655 {
2656     CPUState *cs;
2657     X86CPU *cpu;
2658 
2659     qemu_devices_reset();
2660 
2661     /* Reset APIC after devices have been reset to cancel
2662      * any changes that qemu_devices_reset() might have done.
2663      */
2664     CPU_FOREACH(cs) {
2665         cpu = X86_CPU(cs);
2666 
2667         if (cpu->apic_state) {
2668             device_reset(cpu->apic_state);
2669         }
2670     }
2671 }
2672 
2673 static void pc_machine_wakeup(MachineState *machine)
2674 {
2675     cpu_synchronize_all_states();
2676     pc_machine_reset(machine);
2677     cpu_synchronize_all_post_reset();
2678 }
2679 
2680 static CpuInstanceProperties
2681 pc_cpu_index_to_props(MachineState *ms, unsigned cpu_index)
2682 {
2683     MachineClass *mc = MACHINE_GET_CLASS(ms);
2684     const CPUArchIdList *possible_cpus = mc->possible_cpu_arch_ids(ms);
2685 
2686     assert(cpu_index < possible_cpus->len);
2687     return possible_cpus->cpus[cpu_index].props;
2688 }
2689 
2690 static int64_t pc_get_default_cpu_node_id(const MachineState *ms, int idx)
2691 {
2692    X86CPUTopoInfo topo;
2693    PCMachineState *pcms = PC_MACHINE(ms);
2694 
2695    assert(idx < ms->possible_cpus->len);
2696    x86_topo_ids_from_apicid(ms->possible_cpus->cpus[idx].arch_id,
2697                             pcms->smp_dies, ms->smp.cores,
2698                             ms->smp.threads, &topo);
2699    return topo.pkg_id % ms->numa_state->num_nodes;
2700 }
2701 
2702 static const CPUArchIdList *pc_possible_cpu_arch_ids(MachineState *ms)
2703 {
2704     PCMachineState *pcms = PC_MACHINE(ms);
2705     int i;
2706     unsigned int max_cpus = ms->smp.max_cpus;
2707 
2708     if (ms->possible_cpus) {
2709         /*
2710          * make sure that max_cpus hasn't changed since the first use, i.e.
2711          * -smp hasn't been parsed after it
2712         */
2713         assert(ms->possible_cpus->len == max_cpus);
2714         return ms->possible_cpus;
2715     }
2716 
2717     ms->possible_cpus = g_malloc0(sizeof(CPUArchIdList) +
2718                                   sizeof(CPUArchId) * max_cpus);
2719     ms->possible_cpus->len = max_cpus;
2720     for (i = 0; i < ms->possible_cpus->len; i++) {
2721         X86CPUTopoInfo topo;
2722 
2723         ms->possible_cpus->cpus[i].type = ms->cpu_type;
2724         ms->possible_cpus->cpus[i].vcpus_count = 1;
2725         ms->possible_cpus->cpus[i].arch_id = x86_cpu_apic_id_from_index(pcms, i);
2726         x86_topo_ids_from_apicid(ms->possible_cpus->cpus[i].arch_id,
2727                                  pcms->smp_dies, ms->smp.cores,
2728                                  ms->smp.threads, &topo);
2729         ms->possible_cpus->cpus[i].props.has_socket_id = true;
2730         ms->possible_cpus->cpus[i].props.socket_id = topo.pkg_id;
2731         if (pcms->smp_dies > 1) {
2732             ms->possible_cpus->cpus[i].props.has_die_id = true;
2733             ms->possible_cpus->cpus[i].props.die_id = topo.die_id;
2734         }
2735         ms->possible_cpus->cpus[i].props.has_core_id = true;
2736         ms->possible_cpus->cpus[i].props.core_id = topo.core_id;
2737         ms->possible_cpus->cpus[i].props.has_thread_id = true;
2738         ms->possible_cpus->cpus[i].props.thread_id = topo.smt_id;
2739     }
2740     return ms->possible_cpus;
2741 }
2742 
2743 static void x86_nmi(NMIState *n, int cpu_index, Error **errp)
2744 {
2745     /* cpu index isn't used */
2746     CPUState *cs;
2747 
2748     CPU_FOREACH(cs) {
2749         X86CPU *cpu = X86_CPU(cs);
2750 
2751         if (!cpu->apic_state) {
2752             cpu_interrupt(cs, CPU_INTERRUPT_NMI);
2753         } else {
2754             apic_deliver_nmi(cpu->apic_state);
2755         }
2756     }
2757 }
2758 
2759 
2760 static bool pc_hotplug_allowed(MachineState *ms, DeviceState *dev, Error **errp)
2761 {
2762     X86IOMMUState *iommu = x86_iommu_get_default();
2763     IntelIOMMUState *intel_iommu;
2764 
2765     if (iommu &&
2766         object_dynamic_cast((Object *)iommu, TYPE_INTEL_IOMMU_DEVICE) &&
2767         object_dynamic_cast((Object *)dev, "vfio-pci")) {
2768         intel_iommu = INTEL_IOMMU_DEVICE(iommu);
2769         if (!intel_iommu->caching_mode) {
2770             error_setg(errp, "Device assignment is not allowed without "
2771                        "enabling caching-mode=on for Intel IOMMU.");
2772             return false;
2773         }
2774     }
2775 
2776     return true;
2777 }
2778 
2779 static void pc_machine_class_init(ObjectClass *oc, void *data)
2780 {
2781     MachineClass *mc = MACHINE_CLASS(oc);
2782     PCMachineClass *pcmc = PC_MACHINE_CLASS(oc);
2783     HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(oc);
2784     NMIClass *nc = NMI_CLASS(oc);
2785 
2786     pcmc->pci_enabled = true;
2787     pcmc->has_acpi_build = true;
2788     pcmc->rsdp_in_ram = true;
2789     pcmc->smbios_defaults = true;
2790     pcmc->smbios_uuid_encoded = true;
2791     pcmc->gigabyte_align = true;
2792     pcmc->has_reserved_memory = true;
2793     pcmc->kvmclock_enabled = true;
2794     pcmc->enforce_aligned_dimm = true;
2795     /* BIOS ACPI tables: 128K. Other BIOS datastructures: less than 4K reported
2796      * to be used at the moment, 32K should be enough for a while.  */
2797     pcmc->acpi_data_size = 0x20000 + 0x8000;
2798     pcmc->save_tsc_khz = true;
2799     pcmc->linuxboot_dma_enabled = true;
2800     pcmc->pvh_enabled = true;
2801     assert(!mc->get_hotplug_handler);
2802     mc->get_hotplug_handler = pc_get_hotplug_handler;
2803     mc->hotplug_allowed = pc_hotplug_allowed;
2804     mc->cpu_index_to_instance_props = pc_cpu_index_to_props;
2805     mc->get_default_cpu_node_id = pc_get_default_cpu_node_id;
2806     mc->possible_cpu_arch_ids = pc_possible_cpu_arch_ids;
2807     mc->auto_enable_numa_with_memhp = true;
2808     mc->has_hotpluggable_cpus = true;
2809     mc->default_boot_order = "cad";
2810     mc->hot_add_cpu = pc_hot_add_cpu;
2811     mc->smp_parse = pc_smp_parse;
2812     mc->block_default_type = IF_IDE;
2813     mc->max_cpus = 255;
2814     mc->reset = pc_machine_reset;
2815     mc->wakeup = pc_machine_wakeup;
2816     hc->pre_plug = pc_machine_device_pre_plug_cb;
2817     hc->plug = pc_machine_device_plug_cb;
2818     hc->unplug_request = pc_machine_device_unplug_request_cb;
2819     hc->unplug = pc_machine_device_unplug_cb;
2820     nc->nmi_monitor_handler = x86_nmi;
2821     mc->default_cpu_type = TARGET_DEFAULT_CPU_TYPE;
2822     mc->nvdimm_supported = true;
2823     mc->numa_mem_supported = true;
2824 
2825     object_class_property_add(oc, PC_MACHINE_DEVMEM_REGION_SIZE, "int",
2826         pc_machine_get_device_memory_region_size, NULL,
2827         NULL, NULL, &error_abort);
2828 
2829     object_class_property_add(oc, PC_MACHINE_MAX_RAM_BELOW_4G, "size",
2830         pc_machine_get_max_ram_below_4g, pc_machine_set_max_ram_below_4g,
2831         NULL, NULL, &error_abort);
2832 
2833     object_class_property_set_description(oc, PC_MACHINE_MAX_RAM_BELOW_4G,
2834         "Maximum ram below the 4G boundary (32bit boundary)", &error_abort);
2835 
2836     object_class_property_add(oc, PC_MACHINE_SMM, "OnOffAuto",
2837         pc_machine_get_smm, pc_machine_set_smm,
2838         NULL, NULL, &error_abort);
2839     object_class_property_set_description(oc, PC_MACHINE_SMM,
2840         "Enable SMM (pc & q35)", &error_abort);
2841 
2842     object_class_property_add(oc, PC_MACHINE_VMPORT, "OnOffAuto",
2843         pc_machine_get_vmport, pc_machine_set_vmport,
2844         NULL, NULL, &error_abort);
2845     object_class_property_set_description(oc, PC_MACHINE_VMPORT,
2846         "Enable vmport (pc & q35)", &error_abort);
2847 
2848     object_class_property_add_bool(oc, PC_MACHINE_SMBUS,
2849         pc_machine_get_smbus, pc_machine_set_smbus, &error_abort);
2850 
2851     object_class_property_add_bool(oc, PC_MACHINE_SATA,
2852         pc_machine_get_sata, pc_machine_set_sata, &error_abort);
2853 
2854     object_class_property_add_bool(oc, PC_MACHINE_PIT,
2855         pc_machine_get_pit, pc_machine_set_pit, &error_abort);
2856 }
2857 
2858 static const TypeInfo pc_machine_info = {
2859     .name = TYPE_PC_MACHINE,
2860     .parent = TYPE_MACHINE,
2861     .abstract = true,
2862     .instance_size = sizeof(PCMachineState),
2863     .instance_init = pc_machine_initfn,
2864     .class_size = sizeof(PCMachineClass),
2865     .class_init = pc_machine_class_init,
2866     .interfaces = (InterfaceInfo[]) {
2867          { TYPE_HOTPLUG_HANDLER },
2868          { TYPE_NMI },
2869          { }
2870     },
2871 };
2872 
2873 static void pc_machine_register_types(void)
2874 {
2875     type_register_static(&pc_machine_info);
2876 }
2877 
2878 type_init(pc_machine_register_types)
2879