]> Git Repo - qemu.git/blob - hw/i386/pc.c
hw/i386/pc: move shared x86 functions to x86.c and export them
[qemu.git] / hw / i386 / pc.c
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/x86.h"
28 #include "hw/i386/pc.h"
29 #include "hw/char/serial.h"
30 #include "hw/char/parallel.h"
31 #include "hw/i386/apic.h"
32 #include "hw/i386/topology.h"
33 #include "hw/i386/fw_cfg.h"
34 #include "sysemu/cpus.h"
35 #include "hw/block/fdc.h"
36 #include "hw/ide.h"
37 #include "hw/pci/pci.h"
38 #include "hw/pci/pci_bus.h"
39 #include "hw/nvram/fw_cfg.h"
40 #include "hw/timer/hpet.h"
41 #include "hw/firmware/smbios.h"
42 #include "hw/loader.h"
43 #include "elf.h"
44 #include "migration/vmstate.h"
45 #include "multiboot.h"
46 #include "hw/timer/mc146818rtc.h"
47 #include "hw/dma/i8257.h"
48 #include "hw/timer/i8254.h"
49 #include "hw/input/i8042.h"
50 #include "hw/irq.h"
51 #include "hw/audio/pcspk.h"
52 #include "hw/pci/msi.h"
53 #include "hw/sysbus.h"
54 #include "sysemu/sysemu.h"
55 #include "sysemu/tcg.h"
56 #include "sysemu/numa.h"
57 #include "sysemu/kvm.h"
58 #include "sysemu/qtest.h"
59 #include "sysemu/reset.h"
60 #include "sysemu/runstate.h"
61 #include "kvm_i386.h"
62 #include "hw/xen/xen.h"
63 #include "hw/xen/start_info.h"
64 #include "ui/qemu-spice.h"
65 #include "exec/memory.h"
66 #include "exec/address-spaces.h"
67 #include "sysemu/arch_init.h"
68 #include "qemu/bitmap.h"
69 #include "qemu/config-file.h"
70 #include "qemu/error-report.h"
71 #include "qemu/option.h"
72 #include "qemu/cutils.h"
73 #include "hw/acpi/acpi.h"
74 #include "hw/acpi/cpu_hotplug.h"
75 #include "hw/boards.h"
76 #include "acpi-build.h"
77 #include "hw/mem/pc-dimm.h"
78 #include "qapi/error.h"
79 #include "qapi/qapi-visit-common.h"
80 #include "qapi/visitor.h"
81 #include "hw/core/cpu.h"
82 #include "hw/nmi.h"
83 #include "hw/usb.h"
84 #include "hw/i386/intel_iommu.h"
85 #include "hw/net/ne2000-isa.h"
86 #include "standard-headers/asm-x86/bootparam.h"
87 #include "hw/virtio/virtio-pmem-pci.h"
88 #include "hw/mem/memory-device.h"
89 #include "sysemu/replay.h"
90 #include "qapi/qmp/qerror.h"
91 #include "config-devices.h"
92 #include "e820_memory_layout.h"
93 #include "fw_cfg.h"
94
95 /* debug PC/ISA interrupts */
96 //#define DEBUG_IRQ
97
98 #ifdef DEBUG_IRQ
99 #define DPRINTF(fmt, ...)                                       \
100     do { printf("CPUIRQ: " fmt , ## __VA_ARGS__); } while (0)
101 #else
102 #define DPRINTF(fmt, ...)
103 #endif
104
105 struct hpet_fw_config hpet_cfg = {.count = UINT8_MAX};
106
107 GlobalProperty pc_compat_4_1[] = {};
108 const size_t pc_compat_4_1_len = G_N_ELEMENTS(pc_compat_4_1);
109
110 GlobalProperty pc_compat_4_0[] = {};
111 const size_t pc_compat_4_0_len = G_N_ELEMENTS(pc_compat_4_0);
112
113 GlobalProperty pc_compat_3_1[] = {
114     { "intel-iommu", "dma-drain", "off" },
115     { "Opteron_G3" "-" TYPE_X86_CPU, "rdtscp", "off" },
116     { "Opteron_G4" "-" TYPE_X86_CPU, "rdtscp", "off" },
117     { "Opteron_G4" "-" TYPE_X86_CPU, "npt", "off" },
118     { "Opteron_G4" "-" TYPE_X86_CPU, "nrip-save", "off" },
119     { "Opteron_G5" "-" TYPE_X86_CPU, "rdtscp", "off" },
120     { "Opteron_G5" "-" TYPE_X86_CPU, "npt", "off" },
121     { "Opteron_G5" "-" TYPE_X86_CPU, "nrip-save", "off" },
122     { "EPYC" "-" TYPE_X86_CPU, "npt", "off" },
123     { "EPYC" "-" TYPE_X86_CPU, "nrip-save", "off" },
124     { "EPYC-IBPB" "-" TYPE_X86_CPU, "npt", "off" },
125     { "EPYC-IBPB" "-" TYPE_X86_CPU, "nrip-save", "off" },
126     { "Skylake-Client" "-" TYPE_X86_CPU,      "mpx", "on" },
127     { "Skylake-Client-IBRS" "-" TYPE_X86_CPU, "mpx", "on" },
128     { "Skylake-Server" "-" TYPE_X86_CPU,      "mpx", "on" },
129     { "Skylake-Server-IBRS" "-" TYPE_X86_CPU, "mpx", "on" },
130     { "Cascadelake-Server" "-" TYPE_X86_CPU,  "mpx", "on" },
131     { "Icelake-Client" "-" TYPE_X86_CPU,      "mpx", "on" },
132     { "Icelake-Server" "-" TYPE_X86_CPU,      "mpx", "on" },
133     { "Cascadelake-Server" "-" TYPE_X86_CPU, "stepping", "5" },
134     { TYPE_X86_CPU, "x-intel-pt-auto-level", "off" },
135 };
136 const size_t pc_compat_3_1_len = G_N_ELEMENTS(pc_compat_3_1);
137
138 GlobalProperty pc_compat_3_0[] = {
139     { TYPE_X86_CPU, "x-hv-synic-kvm-only", "on" },
140     { "Skylake-Server" "-" TYPE_X86_CPU, "pku", "off" },
141     { "Skylake-Server-IBRS" "-" TYPE_X86_CPU, "pku", "off" },
142 };
143 const size_t pc_compat_3_0_len = G_N_ELEMENTS(pc_compat_3_0);
144
145 GlobalProperty pc_compat_2_12[] = {
146     { TYPE_X86_CPU, "legacy-cache", "on" },
147     { TYPE_X86_CPU, "topoext", "off" },
148     { "EPYC-" TYPE_X86_CPU, "xlevel", "0x8000000a" },
149     { "EPYC-IBPB-" TYPE_X86_CPU, "xlevel", "0x8000000a" },
150 };
151 const size_t pc_compat_2_12_len = G_N_ELEMENTS(pc_compat_2_12);
152
153 GlobalProperty pc_compat_2_11[] = {
154     { TYPE_X86_CPU, "x-migrate-smi-count", "off" },
155     { "Skylake-Server" "-" TYPE_X86_CPU, "clflushopt", "off" },
156 };
157 const size_t pc_compat_2_11_len = G_N_ELEMENTS(pc_compat_2_11);
158
159 GlobalProperty pc_compat_2_10[] = {
160     { TYPE_X86_CPU, "x-hv-max-vps", "0x40" },
161     { "i440FX-pcihost", "x-pci-hole64-fix", "off" },
162     { "q35-pcihost", "x-pci-hole64-fix", "off" },
163 };
164 const size_t pc_compat_2_10_len = G_N_ELEMENTS(pc_compat_2_10);
165
166 GlobalProperty pc_compat_2_9[] = {
167     { "mch", "extended-tseg-mbytes", "0" },
168 };
169 const size_t pc_compat_2_9_len = G_N_ELEMENTS(pc_compat_2_9);
170
171 GlobalProperty pc_compat_2_8[] = {
172     { TYPE_X86_CPU, "tcg-cpuid", "off" },
173     { "kvmclock", "x-mach-use-reliable-get-clock", "off" },
174     { "ICH9-LPC", "x-smi-broadcast", "off" },
175     { TYPE_X86_CPU, "vmware-cpuid-freq", "off" },
176     { "Haswell-" TYPE_X86_CPU, "stepping", "1" },
177 };
178 const size_t pc_compat_2_8_len = G_N_ELEMENTS(pc_compat_2_8);
179
180 GlobalProperty pc_compat_2_7[] = {
181     { TYPE_X86_CPU, "l3-cache", "off" },
182     { TYPE_X86_CPU, "full-cpuid-auto-level", "off" },
183     { "Opteron_G3" "-" TYPE_X86_CPU, "family", "15" },
184     { "Opteron_G3" "-" TYPE_X86_CPU, "model", "6" },
185     { "Opteron_G3" "-" TYPE_X86_CPU, "stepping", "1" },
186     { "isa-pcspk", "migrate", "off" },
187 };
188 const size_t pc_compat_2_7_len = G_N_ELEMENTS(pc_compat_2_7);
189
190 GlobalProperty pc_compat_2_6[] = {
191     { TYPE_X86_CPU, "cpuid-0xb", "off" },
192     { "vmxnet3", "romfile", "" },
193     { TYPE_X86_CPU, "fill-mtrr-mask", "off" },
194     { "apic-common", "legacy-instance-id", "on", }
195 };
196 const size_t pc_compat_2_6_len = G_N_ELEMENTS(pc_compat_2_6);
197
198 GlobalProperty pc_compat_2_5[] = {};
199 const size_t pc_compat_2_5_len = G_N_ELEMENTS(pc_compat_2_5);
200
201 GlobalProperty pc_compat_2_4[] = {
202     PC_CPU_MODEL_IDS("2.4.0")
203     { "Haswell-" TYPE_X86_CPU, "abm", "off" },
204     { "Haswell-noTSX-" TYPE_X86_CPU, "abm", "off" },
205     { "Broadwell-" TYPE_X86_CPU, "abm", "off" },
206     { "Broadwell-noTSX-" TYPE_X86_CPU, "abm", "off" },
207     { "host" "-" TYPE_X86_CPU, "host-cache-info", "on" },
208     { TYPE_X86_CPU, "check", "off" },
209     { "qemu64" "-" TYPE_X86_CPU, "sse4a", "on" },
210     { "qemu64" "-" TYPE_X86_CPU, "abm", "on" },
211     { "qemu64" "-" TYPE_X86_CPU, "popcnt", "on" },
212     { "qemu32" "-" TYPE_X86_CPU, "popcnt", "on" },
213     { "Opteron_G2" "-" TYPE_X86_CPU, "rdtscp", "on" },
214     { "Opteron_G3" "-" TYPE_X86_CPU, "rdtscp", "on" },
215     { "Opteron_G4" "-" TYPE_X86_CPU, "rdtscp", "on" },
216     { "Opteron_G5" "-" TYPE_X86_CPU, "rdtscp", "on", }
217 };
218 const size_t pc_compat_2_4_len = G_N_ELEMENTS(pc_compat_2_4);
219
220 GlobalProperty pc_compat_2_3[] = {
221     PC_CPU_MODEL_IDS("2.3.0")
222     { TYPE_X86_CPU, "arat", "off" },
223     { "qemu64" "-" TYPE_X86_CPU, "min-level", "4" },
224     { "kvm64" "-" TYPE_X86_CPU, "min-level", "5" },
225     { "pentium3" "-" TYPE_X86_CPU, "min-level", "2" },
226     { "n270" "-" TYPE_X86_CPU, "min-level", "5" },
227     { "Conroe" "-" TYPE_X86_CPU, "min-level", "4" },
228     { "Penryn" "-" TYPE_X86_CPU, "min-level", "4" },
229     { "Nehalem" "-" TYPE_X86_CPU, "min-level", "4" },
230     { "n270" "-" TYPE_X86_CPU, "min-xlevel", "0x8000000a" },
231     { "Penryn" "-" TYPE_X86_CPU, "min-xlevel", "0x8000000a" },
232     { "Conroe" "-" TYPE_X86_CPU, "min-xlevel", "0x8000000a" },
233     { "Nehalem" "-" TYPE_X86_CPU, "min-xlevel", "0x8000000a" },
234     { "Westmere" "-" TYPE_X86_CPU, "min-xlevel", "0x8000000a" },
235     { "SandyBridge" "-" TYPE_X86_CPU, "min-xlevel", "0x8000000a" },
236     { "IvyBridge" "-" TYPE_X86_CPU, "min-xlevel", "0x8000000a" },
237     { "Haswell" "-" TYPE_X86_CPU, "min-xlevel", "0x8000000a" },
238     { "Haswell-noTSX" "-" TYPE_X86_CPU, "min-xlevel", "0x8000000a" },
239     { "Broadwell" "-" TYPE_X86_CPU, "min-xlevel", "0x8000000a" },
240     { "Broadwell-noTSX" "-" TYPE_X86_CPU, "min-xlevel", "0x8000000a" },
241     { TYPE_X86_CPU, "kvm-no-smi-migration", "on" },
242 };
243 const size_t pc_compat_2_3_len = G_N_ELEMENTS(pc_compat_2_3);
244
245 GlobalProperty pc_compat_2_2[] = {
246     PC_CPU_MODEL_IDS("2.2.0")
247     { "kvm64" "-" TYPE_X86_CPU, "vme", "off" },
248     { "kvm32" "-" TYPE_X86_CPU, "vme", "off" },
249     { "Conroe" "-" TYPE_X86_CPU, "vme", "off" },
250     { "Penryn" "-" TYPE_X86_CPU, "vme", "off" },
251     { "Nehalem" "-" TYPE_X86_CPU, "vme", "off" },
252     { "Westmere" "-" TYPE_X86_CPU, "vme", "off" },
253     { "SandyBridge" "-" TYPE_X86_CPU, "vme", "off" },
254     { "Haswell" "-" TYPE_X86_CPU, "vme", "off" },
255     { "Broadwell" "-" TYPE_X86_CPU, "vme", "off" },
256     { "Opteron_G1" "-" TYPE_X86_CPU, "vme", "off" },
257     { "Opteron_G2" "-" TYPE_X86_CPU, "vme", "off" },
258     { "Opteron_G3" "-" TYPE_X86_CPU, "vme", "off" },
259     { "Opteron_G4" "-" TYPE_X86_CPU, "vme", "off" },
260     { "Opteron_G5" "-" TYPE_X86_CPU, "vme", "off" },
261     { "Haswell" "-" TYPE_X86_CPU, "f16c", "off" },
262     { "Haswell" "-" TYPE_X86_CPU, "rdrand", "off" },
263     { "Broadwell" "-" TYPE_X86_CPU, "f16c", "off" },
264     { "Broadwell" "-" TYPE_X86_CPU, "rdrand", "off" },
265 };
266 const size_t pc_compat_2_2_len = G_N_ELEMENTS(pc_compat_2_2);
267
268 GlobalProperty pc_compat_2_1[] = {
269     PC_CPU_MODEL_IDS("2.1.0")
270     { "coreduo" "-" TYPE_X86_CPU, "vmx", "on" },
271     { "core2duo" "-" TYPE_X86_CPU, "vmx", "on" },
272 };
273 const size_t pc_compat_2_1_len = G_N_ELEMENTS(pc_compat_2_1);
274
275 GlobalProperty pc_compat_2_0[] = {
276     PC_CPU_MODEL_IDS("2.0.0")
277     { "virtio-scsi-pci", "any_layout", "off" },
278     { "PIIX4_PM", "memory-hotplug-support", "off" },
279     { "apic", "version", "0x11" },
280     { "nec-usb-xhci", "superspeed-ports-first", "off" },
281     { "nec-usb-xhci", "force-pcie-endcap", "on" },
282     { "pci-serial", "prog_if", "0" },
283     { "pci-serial-2x", "prog_if", "0" },
284     { "pci-serial-4x", "prog_if", "0" },
285     { "virtio-net-pci", "guest_announce", "off" },
286     { "ICH9-LPC", "memory-hotplug-support", "off" },
287     { "xio3130-downstream", COMPAT_PROP_PCP, "off" },
288     { "ioh3420", COMPAT_PROP_PCP, "off" },
289 };
290 const size_t pc_compat_2_0_len = G_N_ELEMENTS(pc_compat_2_0);
291
292 GlobalProperty pc_compat_1_7[] = {
293     PC_CPU_MODEL_IDS("1.7.0")
294     { TYPE_USB_DEVICE, "msos-desc", "no" },
295     { "PIIX4_PM", "acpi-pci-hotplug-with-bridge-support", "off" },
296     { "hpet", HPET_INTCAP, "4" },
297 };
298 const size_t pc_compat_1_7_len = G_N_ELEMENTS(pc_compat_1_7);
299
300 GlobalProperty pc_compat_1_6[] = {
301     PC_CPU_MODEL_IDS("1.6.0")
302     { "e1000", "mitigation", "off" },
303     { "qemu64-" TYPE_X86_CPU, "model", "2" },
304     { "qemu32-" TYPE_X86_CPU, "model", "3" },
305     { "i440FX-pcihost", "short_root_bus", "1" },
306     { "q35-pcihost", "short_root_bus", "1" },
307 };
308 const size_t pc_compat_1_6_len = G_N_ELEMENTS(pc_compat_1_6);
309
310 GlobalProperty pc_compat_1_5[] = {
311     PC_CPU_MODEL_IDS("1.5.0")
312     { "Conroe-" TYPE_X86_CPU, "model", "2" },
313     { "Conroe-" TYPE_X86_CPU, "min-level", "2" },
314     { "Penryn-" TYPE_X86_CPU, "model", "2" },
315     { "Penryn-" TYPE_X86_CPU, "min-level", "2" },
316     { "Nehalem-" TYPE_X86_CPU, "model", "2" },
317     { "Nehalem-" TYPE_X86_CPU, "min-level", "2" },
318     { "virtio-net-pci", "any_layout", "off" },
319     { TYPE_X86_CPU, "pmu", "on" },
320     { "i440FX-pcihost", "short_root_bus", "0" },
321     { "q35-pcihost", "short_root_bus", "0" },
322 };
323 const size_t pc_compat_1_5_len = G_N_ELEMENTS(pc_compat_1_5);
324
325 GlobalProperty pc_compat_1_4[] = {
326     PC_CPU_MODEL_IDS("1.4.0")
327     { "scsi-hd", "discard_granularity", "0" },
328     { "scsi-cd", "discard_granularity", "0" },
329     { "scsi-disk", "discard_granularity", "0" },
330     { "ide-hd", "discard_granularity", "0" },
331     { "ide-cd", "discard_granularity", "0" },
332     { "ide-drive", "discard_granularity", "0" },
333     { "virtio-blk-pci", "discard_granularity", "0" },
334     /* DEV_NVECTORS_UNSPECIFIED as a uint32_t string: */
335     { "virtio-serial-pci", "vectors", "0xFFFFFFFF" },
336     { "virtio-net-pci", "ctrl_guest_offloads", "off" },
337     { "e1000", "romfile", "pxe-e1000.rom" },
338     { "ne2k_pci", "romfile", "pxe-ne2k_pci.rom" },
339     { "pcnet", "romfile", "pxe-pcnet.rom" },
340     { "rtl8139", "romfile", "pxe-rtl8139.rom" },
341     { "virtio-net-pci", "romfile", "pxe-virtio.rom" },
342     { "486-" TYPE_X86_CPU, "model", "0" },
343     { "n270" "-" TYPE_X86_CPU, "movbe", "off" },
344     { "Westmere" "-" TYPE_X86_CPU, "pclmulqdq", "off" },
345 };
346 const size_t pc_compat_1_4_len = G_N_ELEMENTS(pc_compat_1_4);
347
348 void gsi_handler(void *opaque, int n, int level)
349 {
350     GSIState *s = opaque;
351
352     DPRINTF("pc: %s GSI %d\n", level ? "raising" : "lowering", n);
353     if (n < ISA_NUM_IRQS) {
354         qemu_set_irq(s->i8259_irq[n], level);
355     }
356     qemu_set_irq(s->ioapic_irq[n], level);
357 }
358
359 static void ioport80_write(void *opaque, hwaddr addr, uint64_t data,
360                            unsigned size)
361 {
362 }
363
364 static uint64_t ioport80_read(void *opaque, hwaddr addr, unsigned size)
365 {
366     return 0xffffffffffffffffULL;
367 }
368
369 /* MSDOS compatibility mode FPU exception support */
370 static qemu_irq ferr_irq;
371
372 void pc_register_ferr_irq(qemu_irq irq)
373 {
374     ferr_irq = irq;
375 }
376
377 /* XXX: add IGNNE support */
378 void cpu_set_ferr(CPUX86State *s)
379 {
380     qemu_irq_raise(ferr_irq);
381 }
382
383 static void ioportF0_write(void *opaque, hwaddr addr, uint64_t data,
384                            unsigned size)
385 {
386     qemu_irq_lower(ferr_irq);
387 }
388
389 static uint64_t ioportF0_read(void *opaque, hwaddr addr, unsigned size)
390 {
391     return 0xffffffffffffffffULL;
392 }
393
394 /* TSC handling */
395 uint64_t cpu_get_tsc(CPUX86State *env)
396 {
397     return cpu_get_ticks();
398 }
399
400 /* IRQ handling */
401 int cpu_get_pic_interrupt(CPUX86State *env)
402 {
403     X86CPU *cpu = env_archcpu(env);
404     int intno;
405
406     if (!kvm_irqchip_in_kernel()) {
407         intno = apic_get_interrupt(cpu->apic_state);
408         if (intno >= 0) {
409             return intno;
410         }
411         /* read the irq from the PIC */
412         if (!apic_accept_pic_intr(cpu->apic_state)) {
413             return -1;
414         }
415     }
416
417     intno = pic_read_irq(isa_pic);
418     return intno;
419 }
420
421 static void pic_irq_request(void *opaque, int irq, int level)
422 {
423     CPUState *cs = first_cpu;
424     X86CPU *cpu = X86_CPU(cs);
425
426     DPRINTF("pic_irqs: %s irq %d\n", level? "raise" : "lower", irq);
427     if (cpu->apic_state && !kvm_irqchip_in_kernel()) {
428         CPU_FOREACH(cs) {
429             cpu = X86_CPU(cs);
430             if (apic_accept_pic_intr(cpu->apic_state)) {
431                 apic_deliver_pic_intr(cpu->apic_state, level);
432             }
433         }
434     } else {
435         if (level) {
436             cpu_interrupt(cs, CPU_INTERRUPT_HARD);
437         } else {
438             cpu_reset_interrupt(cs, CPU_INTERRUPT_HARD);
439         }
440     }
441 }
442
443 /* PC cmos mappings */
444
445 #define REG_EQUIPMENT_BYTE          0x14
446
447 int cmos_get_fd_drive_type(FloppyDriveType fd0)
448 {
449     int val;
450
451     switch (fd0) {
452     case FLOPPY_DRIVE_TYPE_144:
453         /* 1.44 Mb 3"5 drive */
454         val = 4;
455         break;
456     case FLOPPY_DRIVE_TYPE_288:
457         /* 2.88 Mb 3"5 drive */
458         val = 5;
459         break;
460     case FLOPPY_DRIVE_TYPE_120:
461         /* 1.2 Mb 5"5 drive */
462         val = 2;
463         break;
464     case FLOPPY_DRIVE_TYPE_NONE:
465     default:
466         val = 0;
467         break;
468     }
469     return val;
470 }
471
472 static void cmos_init_hd(ISADevice *s, int type_ofs, int info_ofs,
473                          int16_t cylinders, int8_t heads, int8_t sectors)
474 {
475     rtc_set_memory(s, type_ofs, 47);
476     rtc_set_memory(s, info_ofs, cylinders);
477     rtc_set_memory(s, info_ofs + 1, cylinders >> 8);
478     rtc_set_memory(s, info_ofs + 2, heads);
479     rtc_set_memory(s, info_ofs + 3, 0xff);
480     rtc_set_memory(s, info_ofs + 4, 0xff);
481     rtc_set_memory(s, info_ofs + 5, 0xc0 | ((heads > 8) << 3));
482     rtc_set_memory(s, info_ofs + 6, cylinders);
483     rtc_set_memory(s, info_ofs + 7, cylinders >> 8);
484     rtc_set_memory(s, info_ofs + 8, sectors);
485 }
486
487 /* convert boot_device letter to something recognizable by the bios */
488 static int boot_device2nibble(char boot_device)
489 {
490     switch(boot_device) {
491     case 'a':
492     case 'b':
493         return 0x01; /* floppy boot */
494     case 'c':
495         return 0x02; /* hard drive boot */
496     case 'd':
497         return 0x03; /* CD-ROM boot */
498     case 'n':
499         return 0x04; /* Network boot */
500     }
501     return 0;
502 }
503
504 static void set_boot_dev(ISADevice *s, const char *boot_device, Error **errp)
505 {
506 #define PC_MAX_BOOT_DEVICES 3
507     int nbds, bds[3] = { 0, };
508     int i;
509
510     nbds = strlen(boot_device);
511     if (nbds > PC_MAX_BOOT_DEVICES) {
512         error_setg(errp, "Too many boot devices for PC");
513         return;
514     }
515     for (i = 0; i < nbds; i++) {
516         bds[i] = boot_device2nibble(boot_device[i]);
517         if (bds[i] == 0) {
518             error_setg(errp, "Invalid boot device for PC: '%c'",
519                        boot_device[i]);
520             return;
521         }
522     }
523     rtc_set_memory(s, 0x3d, (bds[1] << 4) | bds[0]);
524     rtc_set_memory(s, 0x38, (bds[2] << 4) | (fd_bootchk ? 0x0 : 0x1));
525 }
526
527 static void pc_boot_set(void *opaque, const char *boot_device, Error **errp)
528 {
529     set_boot_dev(opaque, boot_device, errp);
530 }
531
532 static void pc_cmos_init_floppy(ISADevice *rtc_state, ISADevice *floppy)
533 {
534     int val, nb, i;
535     FloppyDriveType fd_type[2] = { FLOPPY_DRIVE_TYPE_NONE,
536                                    FLOPPY_DRIVE_TYPE_NONE };
537
538     /* floppy type */
539     if (floppy) {
540         for (i = 0; i < 2; i++) {
541             fd_type[i] = isa_fdc_get_drive_type(floppy, i);
542         }
543     }
544     val = (cmos_get_fd_drive_type(fd_type[0]) << 4) |
545         cmos_get_fd_drive_type(fd_type[1]);
546     rtc_set_memory(rtc_state, 0x10, val);
547
548     val = rtc_get_memory(rtc_state, REG_EQUIPMENT_BYTE);
549     nb = 0;
550     if (fd_type[0] != FLOPPY_DRIVE_TYPE_NONE) {
551         nb++;
552     }
553     if (fd_type[1] != FLOPPY_DRIVE_TYPE_NONE) {
554         nb++;
555     }
556     switch (nb) {
557     case 0:
558         break;
559     case 1:
560         val |= 0x01; /* 1 drive, ready for boot */
561         break;
562     case 2:
563         val |= 0x41; /* 2 drives, ready for boot */
564         break;
565     }
566     rtc_set_memory(rtc_state, REG_EQUIPMENT_BYTE, val);
567 }
568
569 typedef struct pc_cmos_init_late_arg {
570     ISADevice *rtc_state;
571     BusState *idebus[2];
572 } pc_cmos_init_late_arg;
573
574 typedef struct check_fdc_state {
575     ISADevice *floppy;
576     bool multiple;
577 } CheckFdcState;
578
579 static int check_fdc(Object *obj, void *opaque)
580 {
581     CheckFdcState *state = opaque;
582     Object *fdc;
583     uint32_t iobase;
584     Error *local_err = NULL;
585
586     fdc = object_dynamic_cast(obj, TYPE_ISA_FDC);
587     if (!fdc) {
588         return 0;
589     }
590
591     iobase = object_property_get_uint(obj, "iobase", &local_err);
592     if (local_err || iobase != 0x3f0) {
593         error_free(local_err);
594         return 0;
595     }
596
597     if (state->floppy) {
598         state->multiple = true;
599     } else {
600         state->floppy = ISA_DEVICE(obj);
601     }
602     return 0;
603 }
604
605 static const char * const fdc_container_path[] = {
606     "/unattached", "/peripheral", "/peripheral-anon"
607 };
608
609 /*
610  * Locate the FDC at IO address 0x3f0, in order to configure the CMOS registers
611  * and ACPI objects.
612  */
613 ISADevice *pc_find_fdc0(void)
614 {
615     int i;
616     Object *container;
617     CheckFdcState state = { 0 };
618
619     for (i = 0; i < ARRAY_SIZE(fdc_container_path); i++) {
620         container = container_get(qdev_get_machine(), fdc_container_path[i]);
621         object_child_foreach(container, check_fdc, &state);
622     }
623
624     if (state.multiple) {
625         warn_report("multiple floppy disk controllers with "
626                     "iobase=0x3f0 have been found");
627         error_printf("the one being picked for CMOS setup might not reflect "
628                      "your intent");
629     }
630
631     return state.floppy;
632 }
633
634 static void pc_cmos_init_late(void *opaque)
635 {
636     pc_cmos_init_late_arg *arg = opaque;
637     ISADevice *s = arg->rtc_state;
638     int16_t cylinders;
639     int8_t heads, sectors;
640     int val;
641     int i, trans;
642
643     val = 0;
644     if (arg->idebus[0] && ide_get_geometry(arg->idebus[0], 0,
645                                            &cylinders, &heads, &sectors) >= 0) {
646         cmos_init_hd(s, 0x19, 0x1b, cylinders, heads, sectors);
647         val |= 0xf0;
648     }
649     if (arg->idebus[0] && ide_get_geometry(arg->idebus[0], 1,
650                                            &cylinders, &heads, &sectors) >= 0) {
651         cmos_init_hd(s, 0x1a, 0x24, cylinders, heads, sectors);
652         val |= 0x0f;
653     }
654     rtc_set_memory(s, 0x12, val);
655
656     val = 0;
657     for (i = 0; i < 4; i++) {
658         /* NOTE: ide_get_geometry() returns the physical
659            geometry.  It is always such that: 1 <= sects <= 63, 1
660            <= heads <= 16, 1 <= cylinders <= 16383. The BIOS
661            geometry can be different if a translation is done. */
662         if (arg->idebus[i / 2] &&
663             ide_get_geometry(arg->idebus[i / 2], i % 2,
664                              &cylinders, &heads, &sectors) >= 0) {
665             trans = ide_get_bios_chs_trans(arg->idebus[i / 2], i % 2) - 1;
666             assert((trans & ~3) == 0);
667             val |= trans << (i * 2);
668         }
669     }
670     rtc_set_memory(s, 0x39, val);
671
672     pc_cmos_init_floppy(s, pc_find_fdc0());
673
674     qemu_unregister_reset(pc_cmos_init_late, opaque);
675 }
676
677 void pc_cmos_init(PCMachineState *pcms,
678                   BusState *idebus0, BusState *idebus1,
679                   ISADevice *s)
680 {
681     int val;
682     static pc_cmos_init_late_arg arg;
683
684     /* various important CMOS locations needed by PC/Bochs bios */
685
686     /* memory size */
687     /* base memory (first MiB) */
688     val = MIN(pcms->below_4g_mem_size / KiB, 640);
689     rtc_set_memory(s, 0x15, val);
690     rtc_set_memory(s, 0x16, val >> 8);
691     /* extended memory (next 64MiB) */
692     if (pcms->below_4g_mem_size > 1 * MiB) {
693         val = (pcms->below_4g_mem_size - 1 * MiB) / KiB;
694     } else {
695         val = 0;
696     }
697     if (val > 65535)
698         val = 65535;
699     rtc_set_memory(s, 0x17, val);
700     rtc_set_memory(s, 0x18, val >> 8);
701     rtc_set_memory(s, 0x30, val);
702     rtc_set_memory(s, 0x31, val >> 8);
703     /* memory between 16MiB and 4GiB */
704     if (pcms->below_4g_mem_size > 16 * MiB) {
705         val = (pcms->below_4g_mem_size - 16 * MiB) / (64 * KiB);
706     } else {
707         val = 0;
708     }
709     if (val > 65535)
710         val = 65535;
711     rtc_set_memory(s, 0x34, val);
712     rtc_set_memory(s, 0x35, val >> 8);
713     /* memory above 4GiB */
714     val = pcms->above_4g_mem_size / 65536;
715     rtc_set_memory(s, 0x5b, val);
716     rtc_set_memory(s, 0x5c, val >> 8);
717     rtc_set_memory(s, 0x5d, val >> 16);
718
719     object_property_add_link(OBJECT(pcms), "rtc_state",
720                              TYPE_ISA_DEVICE,
721                              (Object **)&pcms->rtc,
722                              object_property_allow_set_link,
723                              OBJ_PROP_LINK_STRONG, &error_abort);
724     object_property_set_link(OBJECT(pcms), OBJECT(s),
725                              "rtc_state", &error_abort);
726
727     set_boot_dev(s, MACHINE(pcms)->boot_order, &error_fatal);
728
729     val = 0;
730     val |= 0x02; /* FPU is there */
731     val |= 0x04; /* PS/2 mouse installed */
732     rtc_set_memory(s, REG_EQUIPMENT_BYTE, val);
733
734     /* hard drives and FDC */
735     arg.rtc_state = s;
736     arg.idebus[0] = idebus0;
737     arg.idebus[1] = idebus1;
738     qemu_register_reset(pc_cmos_init_late, &arg);
739 }
740
741 #define TYPE_PORT92 "port92"
742 #define PORT92(obj) OBJECT_CHECK(Port92State, (obj), TYPE_PORT92)
743
744 /* port 92 stuff: could be split off */
745 typedef struct Port92State {
746     ISADevice parent_obj;
747
748     MemoryRegion io;
749     uint8_t outport;
750     qemu_irq a20_out;
751 } Port92State;
752
753 static void port92_write(void *opaque, hwaddr addr, uint64_t val,
754                          unsigned size)
755 {
756     Port92State *s = opaque;
757     int oldval = s->outport;
758
759     DPRINTF("port92: write 0x%02" PRIx64 "\n", val);
760     s->outport = val;
761     qemu_set_irq(s->a20_out, (val >> 1) & 1);
762     if ((val & 1) && !(oldval & 1)) {
763         qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET);
764     }
765 }
766
767 static uint64_t port92_read(void *opaque, hwaddr addr,
768                             unsigned size)
769 {
770     Port92State *s = opaque;
771     uint32_t ret;
772
773     ret = s->outport;
774     DPRINTF("port92: read 0x%02x\n", ret);
775     return ret;
776 }
777
778 static void port92_init(ISADevice *dev, qemu_irq a20_out)
779 {
780     qdev_connect_gpio_out_named(DEVICE(dev), PORT92_A20_LINE, 0, a20_out);
781 }
782
783 static const VMStateDescription vmstate_port92_isa = {
784     .name = "port92",
785     .version_id = 1,
786     .minimum_version_id = 1,
787     .fields = (VMStateField[]) {
788         VMSTATE_UINT8(outport, Port92State),
789         VMSTATE_END_OF_LIST()
790     }
791 };
792
793 static void port92_reset(DeviceState *d)
794 {
795     Port92State *s = PORT92(d);
796
797     s->outport &= ~1;
798 }
799
800 static const MemoryRegionOps port92_ops = {
801     .read = port92_read,
802     .write = port92_write,
803     .impl = {
804         .min_access_size = 1,
805         .max_access_size = 1,
806     },
807     .endianness = DEVICE_LITTLE_ENDIAN,
808 };
809
810 static void port92_initfn(Object *obj)
811 {
812     Port92State *s = PORT92(obj);
813
814     memory_region_init_io(&s->io, OBJECT(s), &port92_ops, s, "port92", 1);
815
816     s->outport = 0;
817
818     qdev_init_gpio_out_named(DEVICE(obj), &s->a20_out, PORT92_A20_LINE, 1);
819 }
820
821 static void port92_realizefn(DeviceState *dev, Error **errp)
822 {
823     ISADevice *isadev = ISA_DEVICE(dev);
824     Port92State *s = PORT92(dev);
825
826     isa_register_ioport(isadev, &s->io, 0x92);
827 }
828
829 static void port92_class_initfn(ObjectClass *klass, void *data)
830 {
831     DeviceClass *dc = DEVICE_CLASS(klass);
832
833     dc->realize = port92_realizefn;
834     dc->reset = port92_reset;
835     dc->vmsd = &vmstate_port92_isa;
836     /*
837      * Reason: unlike ordinary ISA devices, this one needs additional
838      * wiring: its A20 output line needs to be wired up by
839      * port92_init().
840      */
841     dc->user_creatable = false;
842 }
843
844 static const TypeInfo port92_info = {
845     .name          = TYPE_PORT92,
846     .parent        = TYPE_ISA_DEVICE,
847     .instance_size = sizeof(Port92State),
848     .instance_init = port92_initfn,
849     .class_init    = port92_class_initfn,
850 };
851
852 static void port92_register_types(void)
853 {
854     type_register_static(&port92_info);
855 }
856
857 type_init(port92_register_types)
858
859 static void handle_a20_line_change(void *opaque, int irq, int level)
860 {
861     X86CPU *cpu = opaque;
862
863     /* XXX: send to all CPUs ? */
864     /* XXX: add logic to handle multiple A20 line sources */
865     x86_cpu_set_a20(cpu, level);
866 }
867
868 #define NE2000_NB_MAX 6
869
870 static const int ne2000_io[NE2000_NB_MAX] = { 0x300, 0x320, 0x340, 0x360,
871                                               0x280, 0x380 };
872 static const int ne2000_irq[NE2000_NB_MAX] = { 9, 10, 11, 3, 4, 5 };
873
874 void pc_init_ne2k_isa(ISABus *bus, NICInfo *nd)
875 {
876     static int nb_ne2k = 0;
877
878     if (nb_ne2k == NE2000_NB_MAX)
879         return;
880     isa_ne2000_init(bus, ne2000_io[nb_ne2k],
881                     ne2000_irq[nb_ne2k], nd);
882     nb_ne2k++;
883 }
884
885 DeviceState *cpu_get_current_apic(void)
886 {
887     if (current_cpu) {
888         X86CPU *cpu = X86_CPU(current_cpu);
889         return cpu->apic_state;
890     } else {
891         return NULL;
892     }
893 }
894
895 void pc_acpi_smi_interrupt(void *opaque, int irq, int level)
896 {
897     X86CPU *cpu = opaque;
898
899     if (level) {
900         cpu_interrupt(CPU(cpu), CPU_INTERRUPT_SMI);
901     }
902 }
903
904 /*
905  * This function is very similar to smp_parse()
906  * in hw/core/machine.c but includes CPU die support.
907  */
908 void pc_smp_parse(MachineState *ms, QemuOpts *opts)
909 {
910     PCMachineState *pcms = PC_MACHINE(ms);
911
912     if (opts) {
913         unsigned cpus    = qemu_opt_get_number(opts, "cpus", 0);
914         unsigned sockets = qemu_opt_get_number(opts, "sockets", 0);
915         unsigned dies = qemu_opt_get_number(opts, "dies", 1);
916         unsigned cores   = qemu_opt_get_number(opts, "cores", 0);
917         unsigned threads = qemu_opt_get_number(opts, "threads", 0);
918
919         /* compute missing values, prefer sockets over cores over threads */
920         if (cpus == 0 || sockets == 0) {
921             cores = cores > 0 ? cores : 1;
922             threads = threads > 0 ? threads : 1;
923             if (cpus == 0) {
924                 sockets = sockets > 0 ? sockets : 1;
925                 cpus = cores * threads * dies * sockets;
926             } else {
927                 ms->smp.max_cpus =
928                         qemu_opt_get_number(opts, "maxcpus", cpus);
929                 sockets = ms->smp.max_cpus / (cores * threads * dies);
930             }
931         } else if (cores == 0) {
932             threads = threads > 0 ? threads : 1;
933             cores = cpus / (sockets * dies * threads);
934             cores = cores > 0 ? cores : 1;
935         } else if (threads == 0) {
936             threads = cpus / (cores * dies * sockets);
937             threads = threads > 0 ? threads : 1;
938         } else if (sockets * dies * cores * threads < cpus) {
939             error_report("cpu topology: "
940                          "sockets (%u) * dies (%u) * cores (%u) * threads (%u) < "
941                          "smp_cpus (%u)",
942                          sockets, dies, cores, threads, cpus);
943             exit(1);
944         }
945
946         ms->smp.max_cpus =
947                 qemu_opt_get_number(opts, "maxcpus", cpus);
948
949         if (ms->smp.max_cpus < cpus) {
950             error_report("maxcpus must be equal to or greater than smp");
951             exit(1);
952         }
953
954         if (sockets * dies * cores * threads > ms->smp.max_cpus) {
955             error_report("cpu topology: "
956                          "sockets (%u) * dies (%u) * cores (%u) * threads (%u) > "
957                          "maxcpus (%u)",
958                          sockets, dies, cores, threads,
959                          ms->smp.max_cpus);
960             exit(1);
961         }
962
963         if (sockets * dies * cores * threads != ms->smp.max_cpus) {
964             warn_report("Invalid CPU topology deprecated: "
965                         "sockets (%u) * dies (%u) * cores (%u) * threads (%u) "
966                         "!= maxcpus (%u)",
967                         sockets, dies, cores, threads,
968                         ms->smp.max_cpus);
969         }
970
971         ms->smp.cpus = cpus;
972         ms->smp.cores = cores;
973         ms->smp.threads = threads;
974         pcms->smp_dies = dies;
975     }
976
977     if (ms->smp.cpus > 1) {
978         Error *blocker = NULL;
979         error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED, "smp");
980         replay_add_blocker(blocker);
981     }
982 }
983
984 void pc_hot_add_cpu(MachineState *ms, const int64_t id, Error **errp)
985 {
986     PCMachineState *pcms = PC_MACHINE(ms);
987     int64_t apic_id = x86_cpu_apic_id_from_index(pcms, id);
988     Error *local_err = NULL;
989
990     if (id < 0) {
991         error_setg(errp, "Invalid CPU id: %" PRIi64, id);
992         return;
993     }
994
995     if (apic_id >= ACPI_CPU_HOTPLUG_ID_LIMIT) {
996         error_setg(errp, "Unable to add CPU: %" PRIi64
997                    ", resulting APIC ID (%" PRIi64 ") is too large",
998                    id, apic_id);
999         return;
1000     }
1001
1002     x86_cpu_new(PC_MACHINE(ms), apic_id, &local_err);
1003     if (local_err) {
1004         error_propagate(errp, local_err);
1005         return;
1006     }
1007 }
1008
1009 static void rtc_set_cpus_count(ISADevice *rtc, uint16_t cpus_count)
1010 {
1011     if (cpus_count > 0xff) {
1012         /* If the number of CPUs can't be represented in 8 bits, the
1013          * BIOS must use "FW_CFG_NB_CPUS". Set RTC field to 0 just
1014          * to make old BIOSes fail more predictably.
1015          */
1016         rtc_set_memory(rtc, 0x5f, 0);
1017     } else {
1018         rtc_set_memory(rtc, 0x5f, cpus_count - 1);
1019     }
1020 }
1021
1022 static
1023 void pc_machine_done(Notifier *notifier, void *data)
1024 {
1025     PCMachineState *pcms = container_of(notifier,
1026                                         PCMachineState, machine_done);
1027     PCIBus *bus = pcms->bus;
1028
1029     /* set the number of CPUs */
1030     rtc_set_cpus_count(pcms->rtc, pcms->boot_cpus);
1031
1032     if (bus) {
1033         int extra_hosts = 0;
1034
1035         QLIST_FOREACH(bus, &bus->child, sibling) {
1036             /* look for expander root buses */
1037             if (pci_bus_is_root(bus)) {
1038                 extra_hosts++;
1039             }
1040         }
1041         if (extra_hosts && pcms->fw_cfg) {
1042             uint64_t *val = g_malloc(sizeof(*val));
1043             *val = cpu_to_le64(extra_hosts);
1044             fw_cfg_add_file(pcms->fw_cfg,
1045                     "etc/extra-pci-roots", val, sizeof(*val));
1046         }
1047     }
1048
1049     acpi_setup();
1050     if (pcms->fw_cfg) {
1051         fw_cfg_build_smbios(MACHINE(pcms), pcms->fw_cfg);
1052         fw_cfg_build_feature_control(MACHINE(pcms), pcms->fw_cfg);
1053         /* update FW_CFG_NB_CPUS to account for -device added CPUs */
1054         fw_cfg_modify_i16(pcms->fw_cfg, FW_CFG_NB_CPUS, pcms->boot_cpus);
1055     }
1056
1057     if (pcms->apic_id_limit > 255 && !xen_enabled()) {
1058         IntelIOMMUState *iommu = INTEL_IOMMU_DEVICE(x86_iommu_get_default());
1059
1060         if (!iommu || !x86_iommu_ir_supported(X86_IOMMU_DEVICE(iommu)) ||
1061             iommu->intr_eim != ON_OFF_AUTO_ON) {
1062             error_report("current -smp configuration requires "
1063                          "Extended Interrupt Mode enabled. "
1064                          "You can add an IOMMU using: "
1065                          "-device intel-iommu,intremap=on,eim=on");
1066             exit(EXIT_FAILURE);
1067         }
1068     }
1069 }
1070
1071 void pc_guest_info_init(PCMachineState *pcms)
1072 {
1073     int i;
1074     MachineState *ms = MACHINE(pcms);
1075
1076     pcms->apic_xrupt_override = kvm_allows_irq0_override();
1077     pcms->numa_nodes = ms->numa_state->num_nodes;
1078     pcms->node_mem = g_malloc0(pcms->numa_nodes *
1079                                     sizeof *pcms->node_mem);
1080     for (i = 0; i < ms->numa_state->num_nodes; i++) {
1081         pcms->node_mem[i] = ms->numa_state->nodes[i].node_mem;
1082     }
1083
1084     pcms->machine_done.notify = pc_machine_done;
1085     qemu_add_machine_init_done_notifier(&pcms->machine_done);
1086 }
1087
1088 /* setup pci memory address space mapping into system address space */
1089 void pc_pci_as_mapping_init(Object *owner, MemoryRegion *system_memory,
1090                             MemoryRegion *pci_address_space)
1091 {
1092     /* Set to lower priority than RAM */
1093     memory_region_add_subregion_overlap(system_memory, 0x0,
1094                                         pci_address_space, -1);
1095 }
1096
1097 void xen_load_linux(PCMachineState *pcms)
1098 {
1099     int i;
1100     FWCfgState *fw_cfg;
1101
1102     assert(MACHINE(pcms)->kernel_filename != NULL);
1103
1104     fw_cfg = fw_cfg_init_io(FW_CFG_IO_BASE);
1105     fw_cfg_add_i16(fw_cfg, FW_CFG_NB_CPUS, pcms->boot_cpus);
1106     rom_set_fw(fw_cfg);
1107
1108     x86_load_linux(pcms, fw_cfg);
1109     for (i = 0; i < nb_option_roms; i++) {
1110         assert(!strcmp(option_rom[i].name, "linuxboot.bin") ||
1111                !strcmp(option_rom[i].name, "linuxboot_dma.bin") ||
1112                !strcmp(option_rom[i].name, "pvh.bin") ||
1113                !strcmp(option_rom[i].name, "multiboot.bin"));
1114         rom_add_option(option_rom[i].name, option_rom[i].bootindex);
1115     }
1116     pcms->fw_cfg = fw_cfg;
1117 }
1118
1119 void pc_memory_init(PCMachineState *pcms,
1120                     MemoryRegion *system_memory,
1121                     MemoryRegion *rom_memory,
1122                     MemoryRegion **ram_memory)
1123 {
1124     int linux_boot, i;
1125     MemoryRegion *ram, *option_rom_mr;
1126     MemoryRegion *ram_below_4g, *ram_above_4g;
1127     FWCfgState *fw_cfg;
1128     MachineState *machine = MACHINE(pcms);
1129     MachineClass *mc = MACHINE_GET_CLASS(machine);
1130     PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms);
1131
1132     assert(machine->ram_size == pcms->below_4g_mem_size +
1133                                 pcms->above_4g_mem_size);
1134
1135     linux_boot = (machine->kernel_filename != NULL);
1136
1137     /* Allocate RAM.  We allocate it as a single memory region and use
1138      * aliases to address portions of it, mostly for backwards compatibility
1139      * with older qemus that used qemu_ram_alloc().
1140      */
1141     ram = g_malloc(sizeof(*ram));
1142     memory_region_allocate_system_memory(ram, NULL, "pc.ram",
1143                                          machine->ram_size);
1144     *ram_memory = ram;
1145     ram_below_4g = g_malloc(sizeof(*ram_below_4g));
1146     memory_region_init_alias(ram_below_4g, NULL, "ram-below-4g", ram,
1147                              0, pcms->below_4g_mem_size);
1148     memory_region_add_subregion(system_memory, 0, ram_below_4g);
1149     e820_add_entry(0, pcms->below_4g_mem_size, E820_RAM);
1150     if (pcms->above_4g_mem_size > 0) {
1151         ram_above_4g = g_malloc(sizeof(*ram_above_4g));
1152         memory_region_init_alias(ram_above_4g, NULL, "ram-above-4g", ram,
1153                                  pcms->below_4g_mem_size,
1154                                  pcms->above_4g_mem_size);
1155         memory_region_add_subregion(system_memory, 0x100000000ULL,
1156                                     ram_above_4g);
1157         e820_add_entry(0x100000000ULL, pcms->above_4g_mem_size, E820_RAM);
1158     }
1159
1160     if (!pcmc->has_reserved_memory &&
1161         (machine->ram_slots ||
1162          (machine->maxram_size > machine->ram_size))) {
1163
1164         error_report("\"-memory 'slots|maxmem'\" is not supported by: %s",
1165                      mc->name);
1166         exit(EXIT_FAILURE);
1167     }
1168
1169     /* always allocate the device memory information */
1170     machine->device_memory = g_malloc0(sizeof(*machine->device_memory));
1171
1172     /* initialize device memory address space */
1173     if (pcmc->has_reserved_memory &&
1174         (machine->ram_size < machine->maxram_size)) {
1175         ram_addr_t device_mem_size = machine->maxram_size - machine->ram_size;
1176
1177         if (machine->ram_slots > ACPI_MAX_RAM_SLOTS) {
1178             error_report("unsupported amount of memory slots: %"PRIu64,
1179                          machine->ram_slots);
1180             exit(EXIT_FAILURE);
1181         }
1182
1183         if (QEMU_ALIGN_UP(machine->maxram_size,
1184                           TARGET_PAGE_SIZE) != machine->maxram_size) {
1185             error_report("maximum memory size must by aligned to multiple of "
1186                          "%d bytes", TARGET_PAGE_SIZE);
1187             exit(EXIT_FAILURE);
1188         }
1189
1190         machine->device_memory->base =
1191             ROUND_UP(0x100000000ULL + pcms->above_4g_mem_size, 1 * GiB);
1192
1193         if (pcmc->enforce_aligned_dimm) {
1194             /* size device region assuming 1G page max alignment per slot */
1195             device_mem_size += (1 * GiB) * machine->ram_slots;
1196         }
1197
1198         if ((machine->device_memory->base + device_mem_size) <
1199             device_mem_size) {
1200             error_report("unsupported amount of maximum memory: " RAM_ADDR_FMT,
1201                          machine->maxram_size);
1202             exit(EXIT_FAILURE);
1203         }
1204
1205         memory_region_init(&machine->device_memory->mr, OBJECT(pcms),
1206                            "device-memory", device_mem_size);
1207         memory_region_add_subregion(system_memory, machine->device_memory->base,
1208                                     &machine->device_memory->mr);
1209     }
1210
1211     /* Initialize PC system firmware */
1212     pc_system_firmware_init(pcms, rom_memory);
1213
1214     option_rom_mr = g_malloc(sizeof(*option_rom_mr));
1215     memory_region_init_ram(option_rom_mr, NULL, "pc.rom", PC_ROM_SIZE,
1216                            &error_fatal);
1217     if (pcmc->pci_enabled) {
1218         memory_region_set_readonly(option_rom_mr, true);
1219     }
1220     memory_region_add_subregion_overlap(rom_memory,
1221                                         PC_ROM_MIN_VGA,
1222                                         option_rom_mr,
1223                                         1);
1224
1225     fw_cfg = fw_cfg_arch_create(machine,
1226                                 pcms->boot_cpus, pcms->apic_id_limit);
1227
1228     rom_set_fw(fw_cfg);
1229
1230     if (pcmc->has_reserved_memory && machine->device_memory->base) {
1231         uint64_t *val = g_malloc(sizeof(*val));
1232         PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms);
1233         uint64_t res_mem_end = machine->device_memory->base;
1234
1235         if (!pcmc->broken_reserved_end) {
1236             res_mem_end += memory_region_size(&machine->device_memory->mr);
1237         }
1238         *val = cpu_to_le64(ROUND_UP(res_mem_end, 1 * GiB));
1239         fw_cfg_add_file(fw_cfg, "etc/reserved-memory-end", val, sizeof(*val));
1240     }
1241
1242     if (linux_boot) {
1243         x86_load_linux(pcms, fw_cfg);
1244     }
1245
1246     for (i = 0; i < nb_option_roms; i++) {
1247         rom_add_option(option_rom[i].name, option_rom[i].bootindex);
1248     }
1249     pcms->fw_cfg = fw_cfg;
1250
1251     /* Init default IOAPIC address space */
1252     pcms->ioapic_as = &address_space_memory;
1253
1254     /* Init ACPI memory hotplug IO base address */
1255     pcms->memhp_io_base = ACPI_MEMORY_HOTPLUG_BASE;
1256 }
1257
1258 /*
1259  * The 64bit pci hole starts after "above 4G RAM" and
1260  * potentially the space reserved for memory hotplug.
1261  */
1262 uint64_t pc_pci_hole64_start(void)
1263 {
1264     PCMachineState *pcms = PC_MACHINE(qdev_get_machine());
1265     PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms);
1266     MachineState *ms = MACHINE(pcms);
1267     uint64_t hole64_start = 0;
1268
1269     if (pcmc->has_reserved_memory && ms->device_memory->base) {
1270         hole64_start = ms->device_memory->base;
1271         if (!pcmc->broken_reserved_end) {
1272             hole64_start += memory_region_size(&ms->device_memory->mr);
1273         }
1274     } else {
1275         hole64_start = 0x100000000ULL + pcms->above_4g_mem_size;
1276     }
1277
1278     return ROUND_UP(hole64_start, 1 * GiB);
1279 }
1280
1281 qemu_irq pc_allocate_cpu_irq(void)
1282 {
1283     return qemu_allocate_irq(pic_irq_request, NULL, 0);
1284 }
1285
1286 DeviceState *pc_vga_init(ISABus *isa_bus, PCIBus *pci_bus)
1287 {
1288     DeviceState *dev = NULL;
1289
1290     rom_set_order_override(FW_CFG_ORDER_OVERRIDE_VGA);
1291     if (pci_bus) {
1292         PCIDevice *pcidev = pci_vga_init(pci_bus);
1293         dev = pcidev ? &pcidev->qdev : NULL;
1294     } else if (isa_bus) {
1295         ISADevice *isadev = isa_vga_init(isa_bus);
1296         dev = isadev ? DEVICE(isadev) : NULL;
1297     }
1298     rom_reset_order_override();
1299     return dev;
1300 }
1301
1302 static const MemoryRegionOps ioport80_io_ops = {
1303     .write = ioport80_write,
1304     .read = ioport80_read,
1305     .endianness = DEVICE_NATIVE_ENDIAN,
1306     .impl = {
1307         .min_access_size = 1,
1308         .max_access_size = 1,
1309     },
1310 };
1311
1312 static const MemoryRegionOps ioportF0_io_ops = {
1313     .write = ioportF0_write,
1314     .read = ioportF0_read,
1315     .endianness = DEVICE_NATIVE_ENDIAN,
1316     .impl = {
1317         .min_access_size = 1,
1318         .max_access_size = 1,
1319     },
1320 };
1321
1322 static void pc_superio_init(ISABus *isa_bus, bool create_fdctrl, bool no_vmport)
1323 {
1324     int i;
1325     DriveInfo *fd[MAX_FD];
1326     qemu_irq *a20_line;
1327     ISADevice *i8042, *port92, *vmmouse;
1328
1329     serial_hds_isa_init(isa_bus, 0, MAX_ISA_SERIAL_PORTS);
1330     parallel_hds_isa_init(isa_bus, MAX_PARALLEL_PORTS);
1331
1332     for (i = 0; i < MAX_FD; i++) {
1333         fd[i] = drive_get(IF_FLOPPY, 0, i);
1334         create_fdctrl |= !!fd[i];
1335     }
1336     if (create_fdctrl) {
1337         fdctrl_init_isa(isa_bus, fd);
1338     }
1339
1340     i8042 = isa_create_simple(isa_bus, "i8042");
1341     if (!no_vmport) {
1342         vmport_init(isa_bus);
1343         vmmouse = isa_try_create(isa_bus, "vmmouse");
1344     } else {
1345         vmmouse = NULL;
1346     }
1347     if (vmmouse) {
1348         DeviceState *dev = DEVICE(vmmouse);
1349         qdev_prop_set_ptr(dev, "ps2_mouse", i8042);
1350         qdev_init_nofail(dev);
1351     }
1352     port92 = isa_create_simple(isa_bus, "port92");
1353
1354     a20_line = qemu_allocate_irqs(handle_a20_line_change, first_cpu, 2);
1355     i8042_setup_a20_line(i8042, a20_line[0]);
1356     port92_init(port92, a20_line[1]);
1357     g_free(a20_line);
1358 }
1359
1360 void pc_basic_device_init(ISABus *isa_bus, qemu_irq *gsi,
1361                           ISADevice **rtc_state,
1362                           bool create_fdctrl,
1363                           bool no_vmport,
1364                           bool has_pit,
1365                           uint32_t hpet_irqs)
1366 {
1367     int i;
1368     DeviceState *hpet = NULL;
1369     int pit_isa_irq = 0;
1370     qemu_irq pit_alt_irq = NULL;
1371     qemu_irq rtc_irq = NULL;
1372     ISADevice *pit = NULL;
1373     MemoryRegion *ioport80_io = g_new(MemoryRegion, 1);
1374     MemoryRegion *ioportF0_io = g_new(MemoryRegion, 1);
1375
1376     memory_region_init_io(ioport80_io, NULL, &ioport80_io_ops, NULL, "ioport80", 1);
1377     memory_region_add_subregion(isa_bus->address_space_io, 0x80, ioport80_io);
1378
1379     memory_region_init_io(ioportF0_io, NULL, &ioportF0_io_ops, NULL, "ioportF0", 1);
1380     memory_region_add_subregion(isa_bus->address_space_io, 0xf0, ioportF0_io);
1381
1382     /*
1383      * Check if an HPET shall be created.
1384      *
1385      * Without KVM_CAP_PIT_STATE2, we cannot switch off the in-kernel PIT
1386      * when the HPET wants to take over. Thus we have to disable the latter.
1387      */
1388     if (!no_hpet && (!kvm_irqchip_in_kernel() || kvm_has_pit_state2())) {
1389         /* In order to set property, here not using sysbus_try_create_simple */
1390         hpet = qdev_try_create(NULL, TYPE_HPET);
1391         if (hpet) {
1392             /* For pc-piix-*, hpet's intcap is always IRQ2. For pc-q35-1.7
1393              * and earlier, use IRQ2 for compat. Otherwise, use IRQ16~23,
1394              * IRQ8 and IRQ2.
1395              */
1396             uint8_t compat = object_property_get_uint(OBJECT(hpet),
1397                     HPET_INTCAP, NULL);
1398             if (!compat) {
1399                 qdev_prop_set_uint32(hpet, HPET_INTCAP, hpet_irqs);
1400             }
1401             qdev_init_nofail(hpet);
1402             sysbus_mmio_map(SYS_BUS_DEVICE(hpet), 0, HPET_BASE);
1403
1404             for (i = 0; i < GSI_NUM_PINS; i++) {
1405                 sysbus_connect_irq(SYS_BUS_DEVICE(hpet), i, gsi[i]);
1406             }
1407             pit_isa_irq = -1;
1408             pit_alt_irq = qdev_get_gpio_in(hpet, HPET_LEGACY_PIT_INT);
1409             rtc_irq = qdev_get_gpio_in(hpet, HPET_LEGACY_RTC_INT);
1410         }
1411     }
1412     *rtc_state = mc146818_rtc_init(isa_bus, 2000, rtc_irq);
1413
1414     qemu_register_boot_set(pc_boot_set, *rtc_state);
1415
1416     if (!xen_enabled() && has_pit) {
1417         if (kvm_pit_in_kernel()) {
1418             pit = kvm_pit_init(isa_bus, 0x40);
1419         } else {
1420             pit = i8254_pit_init(isa_bus, 0x40, pit_isa_irq, pit_alt_irq);
1421         }
1422         if (hpet) {
1423             /* connect PIT to output control line of the HPET */
1424             qdev_connect_gpio_out(hpet, 0, qdev_get_gpio_in(DEVICE(pit), 0));
1425         }
1426         pcspk_init(isa_bus, pit);
1427     }
1428
1429     i8257_dma_init(isa_bus, 0);
1430
1431     /* Super I/O */
1432     pc_superio_init(isa_bus, create_fdctrl, no_vmport);
1433 }
1434
1435 void pc_nic_init(PCMachineClass *pcmc, ISABus *isa_bus, PCIBus *pci_bus)
1436 {
1437     int i;
1438
1439     rom_set_order_override(FW_CFG_ORDER_OVERRIDE_NIC);
1440     for (i = 0; i < nb_nics; i++) {
1441         NICInfo *nd = &nd_table[i];
1442         const char *model = nd->model ? nd->model : pcmc->default_nic_model;
1443
1444         if (g_str_equal(model, "ne2k_isa")) {
1445             pc_init_ne2k_isa(isa_bus, nd);
1446         } else {
1447             pci_nic_init_nofail(nd, pci_bus, model, NULL);
1448         }
1449     }
1450     rom_reset_order_override();
1451 }
1452
1453 void ioapic_init_gsi(GSIState *gsi_state, const char *parent_name)
1454 {
1455     DeviceState *dev;
1456     SysBusDevice *d;
1457     unsigned int i;
1458
1459     if (kvm_ioapic_in_kernel()) {
1460         dev = qdev_create(NULL, TYPE_KVM_IOAPIC);
1461     } else {
1462         dev = qdev_create(NULL, TYPE_IOAPIC);
1463     }
1464     if (parent_name) {
1465         object_property_add_child(object_resolve_path(parent_name, NULL),
1466                                   "ioapic", OBJECT(dev), NULL);
1467     }
1468     qdev_init_nofail(dev);
1469     d = SYS_BUS_DEVICE(dev);
1470     sysbus_mmio_map(d, 0, IO_APIC_DEFAULT_ADDRESS);
1471
1472     for (i = 0; i < IOAPIC_NUM_PINS; i++) {
1473         gsi_state->ioapic_irq[i] = qdev_get_gpio_in(dev, i);
1474     }
1475 }
1476
1477 static void pc_memory_pre_plug(HotplugHandler *hotplug_dev, DeviceState *dev,
1478                                Error **errp)
1479 {
1480     const PCMachineState *pcms = PC_MACHINE(hotplug_dev);
1481     const PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms);
1482     const MachineState *ms = MACHINE(hotplug_dev);
1483     const bool is_nvdimm = object_dynamic_cast(OBJECT(dev), TYPE_NVDIMM);
1484     const uint64_t legacy_align = TARGET_PAGE_SIZE;
1485     Error *local_err = NULL;
1486
1487     /*
1488      * When -no-acpi is used with Q35 machine type, no ACPI is built,
1489      * but pcms->acpi_dev is still created. Check !acpi_enabled in
1490      * addition to cover this case.
1491      */
1492     if (!pcms->acpi_dev || !acpi_enabled) {
1493         error_setg(errp,
1494                    "memory hotplug is not enabled: missing acpi device or acpi disabled");
1495         return;
1496     }
1497
1498     if (is_nvdimm && !ms->nvdimms_state->is_enabled) {
1499         error_setg(errp, "nvdimm is not enabled: missing 'nvdimm' in '-M'");
1500         return;
1501     }
1502
1503     hotplug_handler_pre_plug(pcms->acpi_dev, dev, &local_err);
1504     if (local_err) {
1505         error_propagate(errp, local_err);
1506         return;
1507     }
1508
1509     pc_dimm_pre_plug(PC_DIMM(dev), MACHINE(hotplug_dev),
1510                      pcmc->enforce_aligned_dimm ? NULL : &legacy_align, errp);
1511 }
1512
1513 static void pc_memory_plug(HotplugHandler *hotplug_dev,
1514                            DeviceState *dev, Error **errp)
1515 {
1516     Error *local_err = NULL;
1517     PCMachineState *pcms = PC_MACHINE(hotplug_dev);
1518     MachineState *ms = MACHINE(hotplug_dev);
1519     bool is_nvdimm = object_dynamic_cast(OBJECT(dev), TYPE_NVDIMM);
1520
1521     pc_dimm_plug(PC_DIMM(dev), MACHINE(pcms), &local_err);
1522     if (local_err) {
1523         goto out;
1524     }
1525
1526     if (is_nvdimm) {
1527         nvdimm_plug(ms->nvdimms_state);
1528     }
1529
1530     hotplug_handler_plug(HOTPLUG_HANDLER(pcms->acpi_dev), dev, &error_abort);
1531 out:
1532     error_propagate(errp, local_err);
1533 }
1534
1535 static void pc_memory_unplug_request(HotplugHandler *hotplug_dev,
1536                                      DeviceState *dev, Error **errp)
1537 {
1538     Error *local_err = NULL;
1539     PCMachineState *pcms = PC_MACHINE(hotplug_dev);
1540
1541     /*
1542      * When -no-acpi is used with Q35 machine type, no ACPI is built,
1543      * but pcms->acpi_dev is still created. Check !acpi_enabled in
1544      * addition to cover this case.
1545      */
1546     if (!pcms->acpi_dev || !acpi_enabled) {
1547         error_setg(&local_err,
1548                    "memory hotplug is not enabled: missing acpi device or acpi disabled");
1549         goto out;
1550     }
1551
1552     if (object_dynamic_cast(OBJECT(dev), TYPE_NVDIMM)) {
1553         error_setg(&local_err,
1554                    "nvdimm device hot unplug is not supported yet.");
1555         goto out;
1556     }
1557
1558     hotplug_handler_unplug_request(HOTPLUG_HANDLER(pcms->acpi_dev), dev,
1559                                    &local_err);
1560 out:
1561     error_propagate(errp, local_err);
1562 }
1563
1564 static void pc_memory_unplug(HotplugHandler *hotplug_dev,
1565                              DeviceState *dev, Error **errp)
1566 {
1567     PCMachineState *pcms = PC_MACHINE(hotplug_dev);
1568     Error *local_err = NULL;
1569
1570     hotplug_handler_unplug(HOTPLUG_HANDLER(pcms->acpi_dev), dev, &local_err);
1571     if (local_err) {
1572         goto out;
1573     }
1574
1575     pc_dimm_unplug(PC_DIMM(dev), MACHINE(pcms));
1576     object_property_set_bool(OBJECT(dev), false, "realized", NULL);
1577  out:
1578     error_propagate(errp, local_err);
1579 }
1580
1581 static int pc_apic_cmp(const void *a, const void *b)
1582 {
1583    CPUArchId *apic_a = (CPUArchId *)a;
1584    CPUArchId *apic_b = (CPUArchId *)b;
1585
1586    return apic_a->arch_id - apic_b->arch_id;
1587 }
1588
1589 /* returns pointer to CPUArchId descriptor that matches CPU's apic_id
1590  * in ms->possible_cpus->cpus, if ms->possible_cpus->cpus has no
1591  * entry corresponding to CPU's apic_id returns NULL.
1592  */
1593 static CPUArchId *pc_find_cpu_slot(MachineState *ms, uint32_t id, int *idx)
1594 {
1595     CPUArchId apic_id, *found_cpu;
1596
1597     apic_id.arch_id = id;
1598     found_cpu = bsearch(&apic_id, ms->possible_cpus->cpus,
1599         ms->possible_cpus->len, sizeof(*ms->possible_cpus->cpus),
1600         pc_apic_cmp);
1601     if (found_cpu && idx) {
1602         *idx = found_cpu - ms->possible_cpus->cpus;
1603     }
1604     return found_cpu;
1605 }
1606
1607 static void pc_cpu_plug(HotplugHandler *hotplug_dev,
1608                         DeviceState *dev, Error **errp)
1609 {
1610     CPUArchId *found_cpu;
1611     Error *local_err = NULL;
1612     X86CPU *cpu = X86_CPU(dev);
1613     PCMachineState *pcms = PC_MACHINE(hotplug_dev);
1614
1615     if (pcms->acpi_dev) {
1616         hotplug_handler_plug(HOTPLUG_HANDLER(pcms->acpi_dev), dev, &local_err);
1617         if (local_err) {
1618             goto out;
1619         }
1620     }
1621
1622     /* increment the number of CPUs */
1623     pcms->boot_cpus++;
1624     if (pcms->rtc) {
1625         rtc_set_cpus_count(pcms->rtc, pcms->boot_cpus);
1626     }
1627     if (pcms->fw_cfg) {
1628         fw_cfg_modify_i16(pcms->fw_cfg, FW_CFG_NB_CPUS, pcms->boot_cpus);
1629     }
1630
1631     found_cpu = pc_find_cpu_slot(MACHINE(pcms), cpu->apic_id, NULL);
1632     found_cpu->cpu = OBJECT(dev);
1633 out:
1634     error_propagate(errp, local_err);
1635 }
1636 static void pc_cpu_unplug_request_cb(HotplugHandler *hotplug_dev,
1637                                      DeviceState *dev, Error **errp)
1638 {
1639     int idx = -1;
1640     Error *local_err = NULL;
1641     X86CPU *cpu = X86_CPU(dev);
1642     PCMachineState *pcms = PC_MACHINE(hotplug_dev);
1643
1644     if (!pcms->acpi_dev) {
1645         error_setg(&local_err, "CPU hot unplug not supported without ACPI");
1646         goto out;
1647     }
1648
1649     pc_find_cpu_slot(MACHINE(pcms), cpu->apic_id, &idx);
1650     assert(idx != -1);
1651     if (idx == 0) {
1652         error_setg(&local_err, "Boot CPU is unpluggable");
1653         goto out;
1654     }
1655
1656     hotplug_handler_unplug_request(HOTPLUG_HANDLER(pcms->acpi_dev), dev,
1657                                    &local_err);
1658     if (local_err) {
1659         goto out;
1660     }
1661
1662  out:
1663     error_propagate(errp, local_err);
1664
1665 }
1666
1667 static void pc_cpu_unplug_cb(HotplugHandler *hotplug_dev,
1668                              DeviceState *dev, Error **errp)
1669 {
1670     CPUArchId *found_cpu;
1671     Error *local_err = NULL;
1672     X86CPU *cpu = X86_CPU(dev);
1673     PCMachineState *pcms = PC_MACHINE(hotplug_dev);
1674
1675     hotplug_handler_unplug(HOTPLUG_HANDLER(pcms->acpi_dev), dev, &local_err);
1676     if (local_err) {
1677         goto out;
1678     }
1679
1680     found_cpu = pc_find_cpu_slot(MACHINE(pcms), cpu->apic_id, NULL);
1681     found_cpu->cpu = NULL;
1682     object_property_set_bool(OBJECT(dev), false, "realized", NULL);
1683
1684     /* decrement the number of CPUs */
1685     pcms->boot_cpus--;
1686     /* Update the number of CPUs in CMOS */
1687     rtc_set_cpus_count(pcms->rtc, pcms->boot_cpus);
1688     fw_cfg_modify_i16(pcms->fw_cfg, FW_CFG_NB_CPUS, pcms->boot_cpus);
1689  out:
1690     error_propagate(errp, local_err);
1691 }
1692
1693 static void pc_cpu_pre_plug(HotplugHandler *hotplug_dev,
1694                             DeviceState *dev, Error **errp)
1695 {
1696     int idx;
1697     CPUState *cs;
1698     CPUArchId *cpu_slot;
1699     X86CPUTopoInfo topo;
1700     X86CPU *cpu = X86_CPU(dev);
1701     CPUX86State *env = &cpu->env;
1702     MachineState *ms = MACHINE(hotplug_dev);
1703     PCMachineState *pcms = PC_MACHINE(hotplug_dev);
1704     unsigned int smp_cores = ms->smp.cores;
1705     unsigned int smp_threads = ms->smp.threads;
1706
1707     if(!object_dynamic_cast(OBJECT(cpu), ms->cpu_type)) {
1708         error_setg(errp, "Invalid CPU type, expected cpu type: '%s'",
1709                    ms->cpu_type);
1710         return;
1711     }
1712
1713     env->nr_dies = pcms->smp_dies;
1714
1715     /*
1716      * If APIC ID is not set,
1717      * set it based on socket/die/core/thread properties.
1718      */
1719     if (cpu->apic_id == UNASSIGNED_APIC_ID) {
1720         int max_socket = (ms->smp.max_cpus - 1) /
1721                                 smp_threads / smp_cores / pcms->smp_dies;
1722
1723         /*
1724          * die-id was optional in QEMU 4.0 and older, so keep it optional
1725          * if there's only one die per socket.
1726          */
1727         if (cpu->die_id < 0 && pcms->smp_dies == 1) {
1728             cpu->die_id = 0;
1729         }
1730
1731         if (cpu->socket_id < 0) {
1732             error_setg(errp, "CPU socket-id is not set");
1733             return;
1734         } else if (cpu->socket_id > max_socket) {
1735             error_setg(errp, "Invalid CPU socket-id: %u must be in range 0:%u",
1736                        cpu->socket_id, max_socket);
1737             return;
1738         }
1739         if (cpu->die_id < 0) {
1740             error_setg(errp, "CPU die-id is not set");
1741             return;
1742         } else if (cpu->die_id > pcms->smp_dies - 1) {
1743             error_setg(errp, "Invalid CPU die-id: %u must be in range 0:%u",
1744                        cpu->die_id, pcms->smp_dies - 1);
1745             return;
1746         }
1747         if (cpu->core_id < 0) {
1748             error_setg(errp, "CPU core-id is not set");
1749             return;
1750         } else if (cpu->core_id > (smp_cores - 1)) {
1751             error_setg(errp, "Invalid CPU core-id: %u must be in range 0:%u",
1752                        cpu->core_id, smp_cores - 1);
1753             return;
1754         }
1755         if (cpu->thread_id < 0) {
1756             error_setg(errp, "CPU thread-id is not set");
1757             return;
1758         } else if (cpu->thread_id > (smp_threads - 1)) {
1759             error_setg(errp, "Invalid CPU thread-id: %u must be in range 0:%u",
1760                        cpu->thread_id, smp_threads - 1);
1761             return;
1762         }
1763
1764         topo.pkg_id = cpu->socket_id;
1765         topo.die_id = cpu->die_id;
1766         topo.core_id = cpu->core_id;
1767         topo.smt_id = cpu->thread_id;
1768         cpu->apic_id = apicid_from_topo_ids(pcms->smp_dies, smp_cores,
1769                                             smp_threads, &topo);
1770     }
1771
1772     cpu_slot = pc_find_cpu_slot(MACHINE(pcms), cpu->apic_id, &idx);
1773     if (!cpu_slot) {
1774         MachineState *ms = MACHINE(pcms);
1775
1776         x86_topo_ids_from_apicid(cpu->apic_id, pcms->smp_dies,
1777                                  smp_cores, smp_threads, &topo);
1778         error_setg(errp,
1779             "Invalid CPU [socket: %u, die: %u, core: %u, thread: %u] with"
1780             " APIC ID %" PRIu32 ", valid index range 0:%d",
1781             topo.pkg_id, topo.die_id, topo.core_id, topo.smt_id,
1782             cpu->apic_id, ms->possible_cpus->len - 1);
1783         return;
1784     }
1785
1786     if (cpu_slot->cpu) {
1787         error_setg(errp, "CPU[%d] with APIC ID %" PRIu32 " exists",
1788                    idx, cpu->apic_id);
1789         return;
1790     }
1791
1792     /* if 'address' properties socket-id/core-id/thread-id are not set, set them
1793      * so that machine_query_hotpluggable_cpus would show correct values
1794      */
1795     /* TODO: move socket_id/core_id/thread_id checks into x86_cpu_realizefn()
1796      * once -smp refactoring is complete and there will be CPU private
1797      * CPUState::nr_cores and CPUState::nr_threads fields instead of globals */
1798     x86_topo_ids_from_apicid(cpu->apic_id, pcms->smp_dies,
1799                              smp_cores, smp_threads, &topo);
1800     if (cpu->socket_id != -1 && cpu->socket_id != topo.pkg_id) {
1801         error_setg(errp, "property socket-id: %u doesn't match set apic-id:"
1802             " 0x%x (socket-id: %u)", cpu->socket_id, cpu->apic_id, topo.pkg_id);
1803         return;
1804     }
1805     cpu->socket_id = topo.pkg_id;
1806
1807     if (cpu->die_id != -1 && cpu->die_id != topo.die_id) {
1808         error_setg(errp, "property die-id: %u doesn't match set apic-id:"
1809             " 0x%x (die-id: %u)", cpu->die_id, cpu->apic_id, topo.die_id);
1810         return;
1811     }
1812     cpu->die_id = topo.die_id;
1813
1814     if (cpu->core_id != -1 && cpu->core_id != topo.core_id) {
1815         error_setg(errp, "property core-id: %u doesn't match set apic-id:"
1816             " 0x%x (core-id: %u)", cpu->core_id, cpu->apic_id, topo.core_id);
1817         return;
1818     }
1819     cpu->core_id = topo.core_id;
1820
1821     if (cpu->thread_id != -1 && cpu->thread_id != topo.smt_id) {
1822         error_setg(errp, "property thread-id: %u doesn't match set apic-id:"
1823             " 0x%x (thread-id: %u)", cpu->thread_id, cpu->apic_id, topo.smt_id);
1824         return;
1825     }
1826     cpu->thread_id = topo.smt_id;
1827
1828     if (hyperv_feat_enabled(cpu, HYPERV_FEAT_VPINDEX) &&
1829         !kvm_hv_vpindex_settable()) {
1830         error_setg(errp, "kernel doesn't allow setting HyperV VP_INDEX");
1831         return;
1832     }
1833
1834     cs = CPU(cpu);
1835     cs->cpu_index = idx;
1836
1837     numa_cpu_pre_plug(cpu_slot, dev, errp);
1838 }
1839
1840 static void pc_virtio_pmem_pci_pre_plug(HotplugHandler *hotplug_dev,
1841                                         DeviceState *dev, Error **errp)
1842 {
1843     HotplugHandler *hotplug_dev2 = qdev_get_bus_hotplug_handler(dev);
1844     Error *local_err = NULL;
1845
1846     if (!hotplug_dev2) {
1847         /*
1848          * Without a bus hotplug handler, we cannot control the plug/unplug
1849          * order. This should never be the case on x86, however better add
1850          * a safety net.
1851          */
1852         error_setg(errp, "virtio-pmem-pci not supported on this bus.");
1853         return;
1854     }
1855     /*
1856      * First, see if we can plug this memory device at all. If that
1857      * succeeds, branch of to the actual hotplug handler.
1858      */
1859     memory_device_pre_plug(MEMORY_DEVICE(dev), MACHINE(hotplug_dev), NULL,
1860                            &local_err);
1861     if (!local_err) {
1862         hotplug_handler_pre_plug(hotplug_dev2, dev, &local_err);
1863     }
1864     error_propagate(errp, local_err);
1865 }
1866
1867 static void pc_virtio_pmem_pci_plug(HotplugHandler *hotplug_dev,
1868                                     DeviceState *dev, Error **errp)
1869 {
1870     HotplugHandler *hotplug_dev2 = qdev_get_bus_hotplug_handler(dev);
1871     Error *local_err = NULL;
1872
1873     /*
1874      * Plug the memory device first and then branch off to the actual
1875      * hotplug handler. If that one fails, we can easily undo the memory
1876      * device bits.
1877      */
1878     memory_device_plug(MEMORY_DEVICE(dev), MACHINE(hotplug_dev));
1879     hotplug_handler_plug(hotplug_dev2, dev, &local_err);
1880     if (local_err) {
1881         memory_device_unplug(MEMORY_DEVICE(dev), MACHINE(hotplug_dev));
1882     }
1883     error_propagate(errp, local_err);
1884 }
1885
1886 static void pc_virtio_pmem_pci_unplug_request(HotplugHandler *hotplug_dev,
1887                                               DeviceState *dev, Error **errp)
1888 {
1889     /* We don't support virtio pmem hot unplug */
1890     error_setg(errp, "virtio pmem device unplug not supported.");
1891 }
1892
1893 static void pc_virtio_pmem_pci_unplug(HotplugHandler *hotplug_dev,
1894                                       DeviceState *dev, Error **errp)
1895 {
1896     /* We don't support virtio pmem hot unplug */
1897 }
1898
1899 static void pc_machine_device_pre_plug_cb(HotplugHandler *hotplug_dev,
1900                                           DeviceState *dev, Error **errp)
1901 {
1902     if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) {
1903         pc_memory_pre_plug(hotplug_dev, dev, errp);
1904     } else if (object_dynamic_cast(OBJECT(dev), TYPE_CPU)) {
1905         pc_cpu_pre_plug(hotplug_dev, dev, errp);
1906     } else if (object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_PMEM_PCI)) {
1907         pc_virtio_pmem_pci_pre_plug(hotplug_dev, dev, errp);
1908     }
1909 }
1910
1911 static void pc_machine_device_plug_cb(HotplugHandler *hotplug_dev,
1912                                       DeviceState *dev, Error **errp)
1913 {
1914     if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) {
1915         pc_memory_plug(hotplug_dev, dev, errp);
1916     } else if (object_dynamic_cast(OBJECT(dev), TYPE_CPU)) {
1917         pc_cpu_plug(hotplug_dev, dev, errp);
1918     } else if (object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_PMEM_PCI)) {
1919         pc_virtio_pmem_pci_plug(hotplug_dev, dev, errp);
1920     }
1921 }
1922
1923 static void pc_machine_device_unplug_request_cb(HotplugHandler *hotplug_dev,
1924                                                 DeviceState *dev, Error **errp)
1925 {
1926     if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) {
1927         pc_memory_unplug_request(hotplug_dev, dev, errp);
1928     } else if (object_dynamic_cast(OBJECT(dev), TYPE_CPU)) {
1929         pc_cpu_unplug_request_cb(hotplug_dev, dev, errp);
1930     } else if (object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_PMEM_PCI)) {
1931         pc_virtio_pmem_pci_unplug_request(hotplug_dev, dev, errp);
1932     } else {
1933         error_setg(errp, "acpi: device unplug request for not supported device"
1934                    " type: %s", object_get_typename(OBJECT(dev)));
1935     }
1936 }
1937
1938 static void pc_machine_device_unplug_cb(HotplugHandler *hotplug_dev,
1939                                         DeviceState *dev, Error **errp)
1940 {
1941     if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) {
1942         pc_memory_unplug(hotplug_dev, dev, errp);
1943     } else if (object_dynamic_cast(OBJECT(dev), TYPE_CPU)) {
1944         pc_cpu_unplug_cb(hotplug_dev, dev, errp);
1945     } else if (object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_PMEM_PCI)) {
1946         pc_virtio_pmem_pci_unplug(hotplug_dev, dev, errp);
1947     } else {
1948         error_setg(errp, "acpi: device unplug for not supported device"
1949                    " type: %s", object_get_typename(OBJECT(dev)));
1950     }
1951 }
1952
1953 static HotplugHandler *pc_get_hotplug_handler(MachineState *machine,
1954                                              DeviceState *dev)
1955 {
1956     if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM) ||
1957         object_dynamic_cast(OBJECT(dev), TYPE_CPU) ||
1958         object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_PMEM_PCI)) {
1959         return HOTPLUG_HANDLER(machine);
1960     }
1961
1962     return NULL;
1963 }
1964
1965 static void
1966 pc_machine_get_device_memory_region_size(Object *obj, Visitor *v,
1967                                          const char *name, void *opaque,
1968                                          Error **errp)
1969 {
1970     MachineState *ms = MACHINE(obj);
1971     int64_t value = 0;
1972
1973     if (ms->device_memory) {
1974         value = memory_region_size(&ms->device_memory->mr);
1975     }
1976
1977     visit_type_int(v, name, &value, errp);
1978 }
1979
1980 static void pc_machine_get_max_ram_below_4g(Object *obj, Visitor *v,
1981                                             const char *name, void *opaque,
1982                                             Error **errp)
1983 {
1984     PCMachineState *pcms = PC_MACHINE(obj);
1985     uint64_t value = pcms->max_ram_below_4g;
1986
1987     visit_type_size(v, name, &value, errp);
1988 }
1989
1990 static void pc_machine_set_max_ram_below_4g(Object *obj, Visitor *v,
1991                                             const char *name, void *opaque,
1992                                             Error **errp)
1993 {
1994     PCMachineState *pcms = PC_MACHINE(obj);
1995     Error *error = NULL;
1996     uint64_t value;
1997
1998     visit_type_size(v, name, &value, &error);
1999     if (error) {
2000         error_propagate(errp, error);
2001         return;
2002     }
2003     if (value > 4 * GiB) {
2004         error_setg(&error,
2005                    "Machine option 'max-ram-below-4g=%"PRIu64
2006                    "' expects size less than or equal to 4G", value);
2007         error_propagate(errp, error);
2008         return;
2009     }
2010
2011     if (value < 1 * MiB) {
2012         warn_report("Only %" PRIu64 " bytes of RAM below the 4GiB boundary,"
2013                     "BIOS may not work with less than 1MiB", value);
2014     }
2015
2016     pcms->max_ram_below_4g = value;
2017 }
2018
2019 static void pc_machine_get_vmport(Object *obj, Visitor *v, const char *name,
2020                                   void *opaque, Error **errp)
2021 {
2022     PCMachineState *pcms = PC_MACHINE(obj);
2023     OnOffAuto vmport = pcms->vmport;
2024
2025     visit_type_OnOffAuto(v, name, &vmport, errp);
2026 }
2027
2028 static void pc_machine_set_vmport(Object *obj, Visitor *v, const char *name,
2029                                   void *opaque, Error **errp)
2030 {
2031     PCMachineState *pcms = PC_MACHINE(obj);
2032
2033     visit_type_OnOffAuto(v, name, &pcms->vmport, errp);
2034 }
2035
2036 bool pc_machine_is_smm_enabled(PCMachineState *pcms)
2037 {
2038     bool smm_available = false;
2039
2040     if (pcms->smm == ON_OFF_AUTO_OFF) {
2041         return false;
2042     }
2043
2044     if (tcg_enabled() || qtest_enabled()) {
2045         smm_available = true;
2046     } else if (kvm_enabled()) {
2047         smm_available = kvm_has_smm();
2048     }
2049
2050     if (smm_available) {
2051         return true;
2052     }
2053
2054     if (pcms->smm == ON_OFF_AUTO_ON) {
2055         error_report("System Management Mode not supported by this hypervisor.");
2056         exit(1);
2057     }
2058     return false;
2059 }
2060
2061 static void pc_machine_get_smm(Object *obj, Visitor *v, const char *name,
2062                                void *opaque, Error **errp)
2063 {
2064     PCMachineState *pcms = PC_MACHINE(obj);
2065     OnOffAuto smm = pcms->smm;
2066
2067     visit_type_OnOffAuto(v, name, &smm, errp);
2068 }
2069
2070 static void pc_machine_set_smm(Object *obj, Visitor *v, const char *name,
2071                                void *opaque, Error **errp)
2072 {
2073     PCMachineState *pcms = PC_MACHINE(obj);
2074
2075     visit_type_OnOffAuto(v, name, &pcms->smm, errp);
2076 }
2077
2078 static bool pc_machine_get_smbus(Object *obj, Error **errp)
2079 {
2080     PCMachineState *pcms = PC_MACHINE(obj);
2081
2082     return pcms->smbus_enabled;
2083 }
2084
2085 static void pc_machine_set_smbus(Object *obj, bool value, Error **errp)
2086 {
2087     PCMachineState *pcms = PC_MACHINE(obj);
2088
2089     pcms->smbus_enabled = value;
2090 }
2091
2092 static bool pc_machine_get_sata(Object *obj, Error **errp)
2093 {
2094     PCMachineState *pcms = PC_MACHINE(obj);
2095
2096     return pcms->sata_enabled;
2097 }
2098
2099 static void pc_machine_set_sata(Object *obj, bool value, Error **errp)
2100 {
2101     PCMachineState *pcms = PC_MACHINE(obj);
2102
2103     pcms->sata_enabled = value;
2104 }
2105
2106 static bool pc_machine_get_pit(Object *obj, Error **errp)
2107 {
2108     PCMachineState *pcms = PC_MACHINE(obj);
2109
2110     return pcms->pit_enabled;
2111 }
2112
2113 static void pc_machine_set_pit(Object *obj, bool value, Error **errp)
2114 {
2115     PCMachineState *pcms = PC_MACHINE(obj);
2116
2117     pcms->pit_enabled = value;
2118 }
2119
2120 static void pc_machine_initfn(Object *obj)
2121 {
2122     PCMachineState *pcms = PC_MACHINE(obj);
2123
2124     pcms->max_ram_below_4g = 0; /* use default */
2125     pcms->smm = ON_OFF_AUTO_AUTO;
2126 #ifdef CONFIG_VMPORT
2127     pcms->vmport = ON_OFF_AUTO_AUTO;
2128 #else
2129     pcms->vmport = ON_OFF_AUTO_OFF;
2130 #endif /* CONFIG_VMPORT */
2131     /* acpi build is enabled by default if machine supports it */
2132     pcms->acpi_build_enabled = PC_MACHINE_GET_CLASS(pcms)->has_acpi_build;
2133     pcms->smbus_enabled = true;
2134     pcms->sata_enabled = true;
2135     pcms->pit_enabled = true;
2136     pcms->smp_dies = 1;
2137
2138     pc_system_flash_create(pcms);
2139 }
2140
2141 static void pc_machine_reset(MachineState *machine)
2142 {
2143     CPUState *cs;
2144     X86CPU *cpu;
2145
2146     qemu_devices_reset();
2147
2148     /* Reset APIC after devices have been reset to cancel
2149      * any changes that qemu_devices_reset() might have done.
2150      */
2151     CPU_FOREACH(cs) {
2152         cpu = X86_CPU(cs);
2153
2154         if (cpu->apic_state) {
2155             device_reset(cpu->apic_state);
2156         }
2157     }
2158 }
2159
2160 static void pc_machine_wakeup(MachineState *machine)
2161 {
2162     cpu_synchronize_all_states();
2163     pc_machine_reset(machine);
2164     cpu_synchronize_all_post_reset();
2165 }
2166
2167 static void x86_nmi(NMIState *n, int cpu_index, Error **errp)
2168 {
2169     /* cpu index isn't used */
2170     CPUState *cs;
2171
2172     CPU_FOREACH(cs) {
2173         X86CPU *cpu = X86_CPU(cs);
2174
2175         if (!cpu->apic_state) {
2176             cpu_interrupt(cs, CPU_INTERRUPT_NMI);
2177         } else {
2178             apic_deliver_nmi(cpu->apic_state);
2179         }
2180     }
2181 }
2182
2183
2184 static bool pc_hotplug_allowed(MachineState *ms, DeviceState *dev, Error **errp)
2185 {
2186     X86IOMMUState *iommu = x86_iommu_get_default();
2187     IntelIOMMUState *intel_iommu;
2188
2189     if (iommu &&
2190         object_dynamic_cast((Object *)iommu, TYPE_INTEL_IOMMU_DEVICE) &&
2191         object_dynamic_cast((Object *)dev, "vfio-pci")) {
2192         intel_iommu = INTEL_IOMMU_DEVICE(iommu);
2193         if (!intel_iommu->caching_mode) {
2194             error_setg(errp, "Device assignment is not allowed without "
2195                        "enabling caching-mode=on for Intel IOMMU.");
2196             return false;
2197         }
2198     }
2199
2200     return true;
2201 }
2202
2203 static void pc_machine_class_init(ObjectClass *oc, void *data)
2204 {
2205     MachineClass *mc = MACHINE_CLASS(oc);
2206     PCMachineClass *pcmc = PC_MACHINE_CLASS(oc);
2207     HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(oc);
2208     NMIClass *nc = NMI_CLASS(oc);
2209
2210     pcmc->pci_enabled = true;
2211     pcmc->has_acpi_build = true;
2212     pcmc->rsdp_in_ram = true;
2213     pcmc->smbios_defaults = true;
2214     pcmc->smbios_uuid_encoded = true;
2215     pcmc->gigabyte_align = true;
2216     pcmc->has_reserved_memory = true;
2217     pcmc->kvmclock_enabled = true;
2218     pcmc->enforce_aligned_dimm = true;
2219     /* BIOS ACPI tables: 128K. Other BIOS datastructures: less than 4K reported
2220      * to be used at the moment, 32K should be enough for a while.  */
2221     pcmc->acpi_data_size = 0x20000 + 0x8000;
2222     pcmc->save_tsc_khz = true;
2223     pcmc->linuxboot_dma_enabled = true;
2224     pcmc->pvh_enabled = true;
2225     assert(!mc->get_hotplug_handler);
2226     mc->get_hotplug_handler = pc_get_hotplug_handler;
2227     mc->hotplug_allowed = pc_hotplug_allowed;
2228     mc->cpu_index_to_instance_props = x86_cpu_index_to_props;
2229     mc->get_default_cpu_node_id = x86_get_default_cpu_node_id;
2230     mc->possible_cpu_arch_ids = x86_possible_cpu_arch_ids;
2231     mc->auto_enable_numa_with_memhp = true;
2232     mc->has_hotpluggable_cpus = true;
2233     mc->default_boot_order = "cad";
2234     mc->hot_add_cpu = pc_hot_add_cpu;
2235     mc->smp_parse = pc_smp_parse;
2236     mc->block_default_type = IF_IDE;
2237     mc->max_cpus = 255;
2238     mc->reset = pc_machine_reset;
2239     mc->wakeup = pc_machine_wakeup;
2240     hc->pre_plug = pc_machine_device_pre_plug_cb;
2241     hc->plug = pc_machine_device_plug_cb;
2242     hc->unplug_request = pc_machine_device_unplug_request_cb;
2243     hc->unplug = pc_machine_device_unplug_cb;
2244     nc->nmi_monitor_handler = x86_nmi;
2245     mc->default_cpu_type = TARGET_DEFAULT_CPU_TYPE;
2246     mc->nvdimm_supported = true;
2247     mc->numa_mem_supported = true;
2248
2249     object_class_property_add(oc, PC_MACHINE_DEVMEM_REGION_SIZE, "int",
2250         pc_machine_get_device_memory_region_size, NULL,
2251         NULL, NULL, &error_abort);
2252
2253     object_class_property_add(oc, PC_MACHINE_MAX_RAM_BELOW_4G, "size",
2254         pc_machine_get_max_ram_below_4g, pc_machine_set_max_ram_below_4g,
2255         NULL, NULL, &error_abort);
2256
2257     object_class_property_set_description(oc, PC_MACHINE_MAX_RAM_BELOW_4G,
2258         "Maximum ram below the 4G boundary (32bit boundary)", &error_abort);
2259
2260     object_class_property_add(oc, PC_MACHINE_SMM, "OnOffAuto",
2261         pc_machine_get_smm, pc_machine_set_smm,
2262         NULL, NULL, &error_abort);
2263     object_class_property_set_description(oc, PC_MACHINE_SMM,
2264         "Enable SMM (pc & q35)", &error_abort);
2265
2266     object_class_property_add(oc, PC_MACHINE_VMPORT, "OnOffAuto",
2267         pc_machine_get_vmport, pc_machine_set_vmport,
2268         NULL, NULL, &error_abort);
2269     object_class_property_set_description(oc, PC_MACHINE_VMPORT,
2270         "Enable vmport (pc & q35)", &error_abort);
2271
2272     object_class_property_add_bool(oc, PC_MACHINE_SMBUS,
2273         pc_machine_get_smbus, pc_machine_set_smbus, &error_abort);
2274
2275     object_class_property_add_bool(oc, PC_MACHINE_SATA,
2276         pc_machine_get_sata, pc_machine_set_sata, &error_abort);
2277
2278     object_class_property_add_bool(oc, PC_MACHINE_PIT,
2279         pc_machine_get_pit, pc_machine_set_pit, &error_abort);
2280 }
2281
2282 static const TypeInfo pc_machine_info = {
2283     .name = TYPE_PC_MACHINE,
2284     .parent = TYPE_MACHINE,
2285     .abstract = true,
2286     .instance_size = sizeof(PCMachineState),
2287     .instance_init = pc_machine_initfn,
2288     .class_size = sizeof(PCMachineClass),
2289     .class_init = pc_machine_class_init,
2290     .interfaces = (InterfaceInfo[]) {
2291          { TYPE_HOTPLUG_HANDLER },
2292          { TYPE_NMI },
2293          { }
2294     },
2295 };
2296
2297 static void pc_machine_register_types(void)
2298 {
2299     type_register_static(&pc_machine_info);
2300 }
2301
2302 type_init(pc_machine_register_types)
This page took 0.153349 seconds and 4 git commands to generate.