2 * QEMU PC System Emulator
4 * Copyright (c) 2003-2004 Fabrice Bellard
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
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"
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"
44 #include "migration/vmstate.h"
45 #include "multiboot.h"
46 #include "hw/rtc/mc146818rtc.h"
47 #include "hw/dma/i8257.h"
48 #include "hw/timer/i8254.h"
49 #include "hw/input/i8042.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"
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"
83 #include "hw/i386/intel_iommu.h"
84 #include "hw/net/ne2000-isa.h"
85 #include "standard-headers/asm-x86/bootparam.h"
86 #include "hw/virtio/virtio-pmem-pci.h"
87 #include "hw/mem/memory-device.h"
88 #include "sysemu/replay.h"
89 #include "qapi/qmp/qerror.h"
90 #include "config-devices.h"
91 #include "e820_memory_layout.h"
94 /* debug PC/ISA interrupts */
98 #define DPRINTF(fmt, ...) \
99 do { printf("CPUIRQ: " fmt , ## __VA_ARGS__); } while (0)
101 #define DPRINTF(fmt, ...)
104 struct hpet_fw_config hpet_cfg
= {.count
= UINT8_MAX
};
106 GlobalProperty pc_compat_4_1
[] = {};
107 const size_t pc_compat_4_1_len
= G_N_ELEMENTS(pc_compat_4_1
);
109 GlobalProperty pc_compat_4_0
[] = {};
110 const size_t pc_compat_4_0_len
= G_N_ELEMENTS(pc_compat_4_0
);
112 GlobalProperty pc_compat_3_1
[] = {
113 { "intel-iommu", "dma-drain", "off" },
114 { "Opteron_G3" "-" TYPE_X86_CPU
, "rdtscp", "off" },
115 { "Opteron_G4" "-" TYPE_X86_CPU
, "rdtscp", "off" },
116 { "Opteron_G4" "-" TYPE_X86_CPU
, "npt", "off" },
117 { "Opteron_G4" "-" TYPE_X86_CPU
, "nrip-save", "off" },
118 { "Opteron_G5" "-" TYPE_X86_CPU
, "rdtscp", "off" },
119 { "Opteron_G5" "-" TYPE_X86_CPU
, "npt", "off" },
120 { "Opteron_G5" "-" TYPE_X86_CPU
, "nrip-save", "off" },
121 { "EPYC" "-" TYPE_X86_CPU
, "npt", "off" },
122 { "EPYC" "-" TYPE_X86_CPU
, "nrip-save", "off" },
123 { "EPYC-IBPB" "-" TYPE_X86_CPU
, "npt", "off" },
124 { "EPYC-IBPB" "-" TYPE_X86_CPU
, "nrip-save", "off" },
125 { "Skylake-Client" "-" TYPE_X86_CPU
, "mpx", "on" },
126 { "Skylake-Client-IBRS" "-" TYPE_X86_CPU
, "mpx", "on" },
127 { "Skylake-Server" "-" TYPE_X86_CPU
, "mpx", "on" },
128 { "Skylake-Server-IBRS" "-" TYPE_X86_CPU
, "mpx", "on" },
129 { "Cascadelake-Server" "-" TYPE_X86_CPU
, "mpx", "on" },
130 { "Icelake-Client" "-" TYPE_X86_CPU
, "mpx", "on" },
131 { "Icelake-Server" "-" TYPE_X86_CPU
, "mpx", "on" },
132 { "Cascadelake-Server" "-" TYPE_X86_CPU
, "stepping", "5" },
133 { TYPE_X86_CPU
, "x-intel-pt-auto-level", "off" },
135 const size_t pc_compat_3_1_len
= G_N_ELEMENTS(pc_compat_3_1
);
137 GlobalProperty pc_compat_3_0
[] = {
138 { TYPE_X86_CPU
, "x-hv-synic-kvm-only", "on" },
139 { "Skylake-Server" "-" TYPE_X86_CPU
, "pku", "off" },
140 { "Skylake-Server-IBRS" "-" TYPE_X86_CPU
, "pku", "off" },
142 const size_t pc_compat_3_0_len
= G_N_ELEMENTS(pc_compat_3_0
);
144 GlobalProperty pc_compat_2_12
[] = {
145 { TYPE_X86_CPU
, "legacy-cache", "on" },
146 { TYPE_X86_CPU
, "topoext", "off" },
147 { "EPYC-" TYPE_X86_CPU
, "xlevel", "0x8000000a" },
148 { "EPYC-IBPB-" TYPE_X86_CPU
, "xlevel", "0x8000000a" },
150 const size_t pc_compat_2_12_len
= G_N_ELEMENTS(pc_compat_2_12
);
152 GlobalProperty pc_compat_2_11
[] = {
153 { TYPE_X86_CPU
, "x-migrate-smi-count", "off" },
154 { "Skylake-Server" "-" TYPE_X86_CPU
, "clflushopt", "off" },
156 const size_t pc_compat_2_11_len
= G_N_ELEMENTS(pc_compat_2_11
);
158 GlobalProperty pc_compat_2_10
[] = {
159 { TYPE_X86_CPU
, "x-hv-max-vps", "0x40" },
160 { "i440FX-pcihost", "x-pci-hole64-fix", "off" },
161 { "q35-pcihost", "x-pci-hole64-fix", "off" },
163 const size_t pc_compat_2_10_len
= G_N_ELEMENTS(pc_compat_2_10
);
165 GlobalProperty pc_compat_2_9
[] = {
166 { "mch", "extended-tseg-mbytes", "0" },
168 const size_t pc_compat_2_9_len
= G_N_ELEMENTS(pc_compat_2_9
);
170 GlobalProperty pc_compat_2_8
[] = {
171 { TYPE_X86_CPU
, "tcg-cpuid", "off" },
172 { "kvmclock", "x-mach-use-reliable-get-clock", "off" },
173 { "ICH9-LPC", "x-smi-broadcast", "off" },
174 { TYPE_X86_CPU
, "vmware-cpuid-freq", "off" },
175 { "Haswell-" TYPE_X86_CPU
, "stepping", "1" },
177 const size_t pc_compat_2_8_len
= G_N_ELEMENTS(pc_compat_2_8
);
179 GlobalProperty pc_compat_2_7
[] = {
180 { TYPE_X86_CPU
, "l3-cache", "off" },
181 { TYPE_X86_CPU
, "full-cpuid-auto-level", "off" },
182 { "Opteron_G3" "-" TYPE_X86_CPU
, "family", "15" },
183 { "Opteron_G3" "-" TYPE_X86_CPU
, "model", "6" },
184 { "Opteron_G3" "-" TYPE_X86_CPU
, "stepping", "1" },
185 { "isa-pcspk", "migrate", "off" },
187 const size_t pc_compat_2_7_len
= G_N_ELEMENTS(pc_compat_2_7
);
189 GlobalProperty pc_compat_2_6
[] = {
190 { TYPE_X86_CPU
, "cpuid-0xb", "off" },
191 { "vmxnet3", "romfile", "" },
192 { TYPE_X86_CPU
, "fill-mtrr-mask", "off" },
193 { "apic-common", "legacy-instance-id", "on", }
195 const size_t pc_compat_2_6_len
= G_N_ELEMENTS(pc_compat_2_6
);
197 GlobalProperty pc_compat_2_5
[] = {};
198 const size_t pc_compat_2_5_len
= G_N_ELEMENTS(pc_compat_2_5
);
200 GlobalProperty pc_compat_2_4
[] = {
201 PC_CPU_MODEL_IDS("2.4.0")
202 { "Haswell-" TYPE_X86_CPU
, "abm", "off" },
203 { "Haswell-noTSX-" TYPE_X86_CPU
, "abm", "off" },
204 { "Broadwell-" TYPE_X86_CPU
, "abm", "off" },
205 { "Broadwell-noTSX-" TYPE_X86_CPU
, "abm", "off" },
206 { "host" "-" TYPE_X86_CPU
, "host-cache-info", "on" },
207 { TYPE_X86_CPU
, "check", "off" },
208 { "qemu64" "-" TYPE_X86_CPU
, "sse4a", "on" },
209 { "qemu64" "-" TYPE_X86_CPU
, "abm", "on" },
210 { "qemu64" "-" TYPE_X86_CPU
, "popcnt", "on" },
211 { "qemu32" "-" TYPE_X86_CPU
, "popcnt", "on" },
212 { "Opteron_G2" "-" TYPE_X86_CPU
, "rdtscp", "on" },
213 { "Opteron_G3" "-" TYPE_X86_CPU
, "rdtscp", "on" },
214 { "Opteron_G4" "-" TYPE_X86_CPU
, "rdtscp", "on" },
215 { "Opteron_G5" "-" TYPE_X86_CPU
, "rdtscp", "on", }
217 const size_t pc_compat_2_4_len
= G_N_ELEMENTS(pc_compat_2_4
);
219 GlobalProperty pc_compat_2_3
[] = {
220 PC_CPU_MODEL_IDS("2.3.0")
221 { TYPE_X86_CPU
, "arat", "off" },
222 { "qemu64" "-" TYPE_X86_CPU
, "min-level", "4" },
223 { "kvm64" "-" TYPE_X86_CPU
, "min-level", "5" },
224 { "pentium3" "-" TYPE_X86_CPU
, "min-level", "2" },
225 { "n270" "-" TYPE_X86_CPU
, "min-level", "5" },
226 { "Conroe" "-" TYPE_X86_CPU
, "min-level", "4" },
227 { "Penryn" "-" TYPE_X86_CPU
, "min-level", "4" },
228 { "Nehalem" "-" TYPE_X86_CPU
, "min-level", "4" },
229 { "n270" "-" TYPE_X86_CPU
, "min-xlevel", "0x8000000a" },
230 { "Penryn" "-" TYPE_X86_CPU
, "min-xlevel", "0x8000000a" },
231 { "Conroe" "-" TYPE_X86_CPU
, "min-xlevel", "0x8000000a" },
232 { "Nehalem" "-" TYPE_X86_CPU
, "min-xlevel", "0x8000000a" },
233 { "Westmere" "-" TYPE_X86_CPU
, "min-xlevel", "0x8000000a" },
234 { "SandyBridge" "-" TYPE_X86_CPU
, "min-xlevel", "0x8000000a" },
235 { "IvyBridge" "-" TYPE_X86_CPU
, "min-xlevel", "0x8000000a" },
236 { "Haswell" "-" TYPE_X86_CPU
, "min-xlevel", "0x8000000a" },
237 { "Haswell-noTSX" "-" TYPE_X86_CPU
, "min-xlevel", "0x8000000a" },
238 { "Broadwell" "-" TYPE_X86_CPU
, "min-xlevel", "0x8000000a" },
239 { "Broadwell-noTSX" "-" TYPE_X86_CPU
, "min-xlevel", "0x8000000a" },
240 { TYPE_X86_CPU
, "kvm-no-smi-migration", "on" },
242 const size_t pc_compat_2_3_len
= G_N_ELEMENTS(pc_compat_2_3
);
244 GlobalProperty pc_compat_2_2
[] = {
245 PC_CPU_MODEL_IDS("2.2.0")
246 { "kvm64" "-" TYPE_X86_CPU
, "vme", "off" },
247 { "kvm32" "-" TYPE_X86_CPU
, "vme", "off" },
248 { "Conroe" "-" TYPE_X86_CPU
, "vme", "off" },
249 { "Penryn" "-" TYPE_X86_CPU
, "vme", "off" },
250 { "Nehalem" "-" TYPE_X86_CPU
, "vme", "off" },
251 { "Westmere" "-" TYPE_X86_CPU
, "vme", "off" },
252 { "SandyBridge" "-" TYPE_X86_CPU
, "vme", "off" },
253 { "Haswell" "-" TYPE_X86_CPU
, "vme", "off" },
254 { "Broadwell" "-" TYPE_X86_CPU
, "vme", "off" },
255 { "Opteron_G1" "-" TYPE_X86_CPU
, "vme", "off" },
256 { "Opteron_G2" "-" TYPE_X86_CPU
, "vme", "off" },
257 { "Opteron_G3" "-" TYPE_X86_CPU
, "vme", "off" },
258 { "Opteron_G4" "-" TYPE_X86_CPU
, "vme", "off" },
259 { "Opteron_G5" "-" TYPE_X86_CPU
, "vme", "off" },
260 { "Haswell" "-" TYPE_X86_CPU
, "f16c", "off" },
261 { "Haswell" "-" TYPE_X86_CPU
, "rdrand", "off" },
262 { "Broadwell" "-" TYPE_X86_CPU
, "f16c", "off" },
263 { "Broadwell" "-" TYPE_X86_CPU
, "rdrand", "off" },
265 const size_t pc_compat_2_2_len
= G_N_ELEMENTS(pc_compat_2_2
);
267 GlobalProperty pc_compat_2_1
[] = {
268 PC_CPU_MODEL_IDS("2.1.0")
269 { "coreduo" "-" TYPE_X86_CPU
, "vmx", "on" },
270 { "core2duo" "-" TYPE_X86_CPU
, "vmx", "on" },
272 const size_t pc_compat_2_1_len
= G_N_ELEMENTS(pc_compat_2_1
);
274 GlobalProperty pc_compat_2_0
[] = {
275 PC_CPU_MODEL_IDS("2.0.0")
276 { "virtio-scsi-pci", "any_layout", "off" },
277 { "PIIX4_PM", "memory-hotplug-support", "off" },
278 { "apic", "version", "0x11" },
279 { "nec-usb-xhci", "superspeed-ports-first", "off" },
280 { "nec-usb-xhci", "force-pcie-endcap", "on" },
281 { "pci-serial", "prog_if", "0" },
282 { "pci-serial-2x", "prog_if", "0" },
283 { "pci-serial-4x", "prog_if", "0" },
284 { "virtio-net-pci", "guest_announce", "off" },
285 { "ICH9-LPC", "memory-hotplug-support", "off" },
286 { "xio3130-downstream", COMPAT_PROP_PCP
, "off" },
287 { "ioh3420", COMPAT_PROP_PCP
, "off" },
289 const size_t pc_compat_2_0_len
= G_N_ELEMENTS(pc_compat_2_0
);
291 GlobalProperty pc_compat_1_7
[] = {
292 PC_CPU_MODEL_IDS("1.7.0")
293 { TYPE_USB_DEVICE
, "msos-desc", "no" },
294 { "PIIX4_PM", "acpi-pci-hotplug-with-bridge-support", "off" },
295 { "hpet", HPET_INTCAP
, "4" },
297 const size_t pc_compat_1_7_len
= G_N_ELEMENTS(pc_compat_1_7
);
299 GlobalProperty pc_compat_1_6
[] = {
300 PC_CPU_MODEL_IDS("1.6.0")
301 { "e1000", "mitigation", "off" },
302 { "qemu64-" TYPE_X86_CPU
, "model", "2" },
303 { "qemu32-" TYPE_X86_CPU
, "model", "3" },
304 { "i440FX-pcihost", "short_root_bus", "1" },
305 { "q35-pcihost", "short_root_bus", "1" },
307 const size_t pc_compat_1_6_len
= G_N_ELEMENTS(pc_compat_1_6
);
309 GlobalProperty pc_compat_1_5
[] = {
310 PC_CPU_MODEL_IDS("1.5.0")
311 { "Conroe-" TYPE_X86_CPU
, "model", "2" },
312 { "Conroe-" TYPE_X86_CPU
, "min-level", "2" },
313 { "Penryn-" TYPE_X86_CPU
, "model", "2" },
314 { "Penryn-" TYPE_X86_CPU
, "min-level", "2" },
315 { "Nehalem-" TYPE_X86_CPU
, "model", "2" },
316 { "Nehalem-" TYPE_X86_CPU
, "min-level", "2" },
317 { "virtio-net-pci", "any_layout", "off" },
318 { TYPE_X86_CPU
, "pmu", "on" },
319 { "i440FX-pcihost", "short_root_bus", "0" },
320 { "q35-pcihost", "short_root_bus", "0" },
322 const size_t pc_compat_1_5_len
= G_N_ELEMENTS(pc_compat_1_5
);
324 GlobalProperty pc_compat_1_4
[] = {
325 PC_CPU_MODEL_IDS("1.4.0")
326 { "scsi-hd", "discard_granularity", "0" },
327 { "scsi-cd", "discard_granularity", "0" },
328 { "scsi-disk", "discard_granularity", "0" },
329 { "ide-hd", "discard_granularity", "0" },
330 { "ide-cd", "discard_granularity", "0" },
331 { "ide-drive", "discard_granularity", "0" },
332 { "virtio-blk-pci", "discard_granularity", "0" },
333 /* DEV_NVECTORS_UNSPECIFIED as a uint32_t string: */
334 { "virtio-serial-pci", "vectors", "0xFFFFFFFF" },
335 { "virtio-net-pci", "ctrl_guest_offloads", "off" },
336 { "e1000", "romfile", "pxe-e1000.rom" },
337 { "ne2k_pci", "romfile", "pxe-ne2k_pci.rom" },
338 { "pcnet", "romfile", "pxe-pcnet.rom" },
339 { "rtl8139", "romfile", "pxe-rtl8139.rom" },
340 { "virtio-net-pci", "romfile", "pxe-virtio.rom" },
341 { "486-" TYPE_X86_CPU
, "model", "0" },
342 { "n270" "-" TYPE_X86_CPU
, "movbe", "off" },
343 { "Westmere" "-" TYPE_X86_CPU
, "pclmulqdq", "off" },
345 const size_t pc_compat_1_4_len
= G_N_ELEMENTS(pc_compat_1_4
);
347 void gsi_handler(void *opaque
, int n
, int level
)
349 GSIState
*s
= opaque
;
351 DPRINTF("pc: %s GSI %d\n", level
? "raising" : "lowering", n
);
352 if (n
< ISA_NUM_IRQS
) {
353 qemu_set_irq(s
->i8259_irq
[n
], level
);
355 qemu_set_irq(s
->ioapic_irq
[n
], level
);
358 GSIState
*pc_gsi_create(qemu_irq
**irqs
, bool pci_enabled
)
362 s
= g_new0(GSIState
, 1);
363 if (kvm_ioapic_in_kernel()) {
364 kvm_pc_setup_irq_routing(pci_enabled
);
365 *irqs
= qemu_allocate_irqs(kvm_pc_gsi_handler
, s
, GSI_NUM_PINS
);
367 *irqs
= qemu_allocate_irqs(gsi_handler
, s
, GSI_NUM_PINS
);
373 static void ioport80_write(void *opaque
, hwaddr addr
, uint64_t data
,
378 static uint64_t ioport80_read(void *opaque
, hwaddr addr
, unsigned size
)
380 return 0xffffffffffffffffULL
;
383 /* MSDOS compatibility mode FPU exception support */
384 static void ioportF0_write(void *opaque
, hwaddr addr
, uint64_t data
,
392 static uint64_t ioportF0_read(void *opaque
, hwaddr addr
, unsigned size
)
394 return 0xffffffffffffffffULL
;
398 uint64_t cpu_get_tsc(CPUX86State
*env
)
400 return cpu_get_ticks();
404 int cpu_get_pic_interrupt(CPUX86State
*env
)
406 X86CPU
*cpu
= env_archcpu(env
);
409 if (!kvm_irqchip_in_kernel()) {
410 intno
= apic_get_interrupt(cpu
->apic_state
);
414 /* read the irq from the PIC */
415 if (!apic_accept_pic_intr(cpu
->apic_state
)) {
420 intno
= pic_read_irq(isa_pic
);
424 static void pic_irq_request(void *opaque
, int irq
, int level
)
426 CPUState
*cs
= first_cpu
;
427 X86CPU
*cpu
= X86_CPU(cs
);
429 DPRINTF("pic_irqs: %s irq %d\n", level
? "raise" : "lower", irq
);
430 if (cpu
->apic_state
&& !kvm_irqchip_in_kernel()) {
433 if (apic_accept_pic_intr(cpu
->apic_state
)) {
434 apic_deliver_pic_intr(cpu
->apic_state
, level
);
439 cpu_interrupt(cs
, CPU_INTERRUPT_HARD
);
441 cpu_reset_interrupt(cs
, CPU_INTERRUPT_HARD
);
446 /* PC cmos mappings */
448 #define REG_EQUIPMENT_BYTE 0x14
450 int cmos_get_fd_drive_type(FloppyDriveType fd0
)
455 case FLOPPY_DRIVE_TYPE_144
:
456 /* 1.44 Mb 3"5 drive */
459 case FLOPPY_DRIVE_TYPE_288
:
460 /* 2.88 Mb 3"5 drive */
463 case FLOPPY_DRIVE_TYPE_120
:
464 /* 1.2 Mb 5"5 drive */
467 case FLOPPY_DRIVE_TYPE_NONE
:
475 static void cmos_init_hd(ISADevice
*s
, int type_ofs
, int info_ofs
,
476 int16_t cylinders
, int8_t heads
, int8_t sectors
)
478 rtc_set_memory(s
, type_ofs
, 47);
479 rtc_set_memory(s
, info_ofs
, cylinders
);
480 rtc_set_memory(s
, info_ofs
+ 1, cylinders
>> 8);
481 rtc_set_memory(s
, info_ofs
+ 2, heads
);
482 rtc_set_memory(s
, info_ofs
+ 3, 0xff);
483 rtc_set_memory(s
, info_ofs
+ 4, 0xff);
484 rtc_set_memory(s
, info_ofs
+ 5, 0xc0 | ((heads
> 8) << 3));
485 rtc_set_memory(s
, info_ofs
+ 6, cylinders
);
486 rtc_set_memory(s
, info_ofs
+ 7, cylinders
>> 8);
487 rtc_set_memory(s
, info_ofs
+ 8, sectors
);
490 /* convert boot_device letter to something recognizable by the bios */
491 static int boot_device2nibble(char boot_device
)
493 switch(boot_device
) {
496 return 0x01; /* floppy boot */
498 return 0x02; /* hard drive boot */
500 return 0x03; /* CD-ROM boot */
502 return 0x04; /* Network boot */
507 static void set_boot_dev(ISADevice
*s
, const char *boot_device
, Error
**errp
)
509 #define PC_MAX_BOOT_DEVICES 3
510 int nbds
, bds
[3] = { 0, };
513 nbds
= strlen(boot_device
);
514 if (nbds
> PC_MAX_BOOT_DEVICES
) {
515 error_setg(errp
, "Too many boot devices for PC");
518 for (i
= 0; i
< nbds
; i
++) {
519 bds
[i
] = boot_device2nibble(boot_device
[i
]);
521 error_setg(errp
, "Invalid boot device for PC: '%c'",
526 rtc_set_memory(s
, 0x3d, (bds
[1] << 4) | bds
[0]);
527 rtc_set_memory(s
, 0x38, (bds
[2] << 4) | (fd_bootchk
? 0x0 : 0x1));
530 static void pc_boot_set(void *opaque
, const char *boot_device
, Error
**errp
)
532 set_boot_dev(opaque
, boot_device
, errp
);
535 static void pc_cmos_init_floppy(ISADevice
*rtc_state
, ISADevice
*floppy
)
538 FloppyDriveType fd_type
[2] = { FLOPPY_DRIVE_TYPE_NONE
,
539 FLOPPY_DRIVE_TYPE_NONE
};
543 for (i
= 0; i
< 2; i
++) {
544 fd_type
[i
] = isa_fdc_get_drive_type(floppy
, i
);
547 val
= (cmos_get_fd_drive_type(fd_type
[0]) << 4) |
548 cmos_get_fd_drive_type(fd_type
[1]);
549 rtc_set_memory(rtc_state
, 0x10, val
);
551 val
= rtc_get_memory(rtc_state
, REG_EQUIPMENT_BYTE
);
553 if (fd_type
[0] != FLOPPY_DRIVE_TYPE_NONE
) {
556 if (fd_type
[1] != FLOPPY_DRIVE_TYPE_NONE
) {
563 val
|= 0x01; /* 1 drive, ready for boot */
566 val
|= 0x41; /* 2 drives, ready for boot */
569 rtc_set_memory(rtc_state
, REG_EQUIPMENT_BYTE
, val
);
572 typedef struct pc_cmos_init_late_arg
{
573 ISADevice
*rtc_state
;
575 } pc_cmos_init_late_arg
;
577 typedef struct check_fdc_state
{
582 static int check_fdc(Object
*obj
, void *opaque
)
584 CheckFdcState
*state
= opaque
;
587 Error
*local_err
= NULL
;
589 fdc
= object_dynamic_cast(obj
, TYPE_ISA_FDC
);
594 iobase
= object_property_get_uint(obj
, "iobase", &local_err
);
595 if (local_err
|| iobase
!= 0x3f0) {
596 error_free(local_err
);
601 state
->multiple
= true;
603 state
->floppy
= ISA_DEVICE(obj
);
608 static const char * const fdc_container_path
[] = {
609 "/unattached", "/peripheral", "/peripheral-anon"
613 * Locate the FDC at IO address 0x3f0, in order to configure the CMOS registers
616 ISADevice
*pc_find_fdc0(void)
620 CheckFdcState state
= { 0 };
622 for (i
= 0; i
< ARRAY_SIZE(fdc_container_path
); i
++) {
623 container
= container_get(qdev_get_machine(), fdc_container_path
[i
]);
624 object_child_foreach(container
, check_fdc
, &state
);
627 if (state
.multiple
) {
628 warn_report("multiple floppy disk controllers with "
629 "iobase=0x3f0 have been found");
630 error_printf("the one being picked for CMOS setup might not reflect "
637 static void pc_cmos_init_late(void *opaque
)
639 pc_cmos_init_late_arg
*arg
= opaque
;
640 ISADevice
*s
= arg
->rtc_state
;
642 int8_t heads
, sectors
;
647 if (arg
->idebus
[0] && ide_get_geometry(arg
->idebus
[0], 0,
648 &cylinders
, &heads
, §ors
) >= 0) {
649 cmos_init_hd(s
, 0x19, 0x1b, cylinders
, heads
, sectors
);
652 if (arg
->idebus
[0] && ide_get_geometry(arg
->idebus
[0], 1,
653 &cylinders
, &heads
, §ors
) >= 0) {
654 cmos_init_hd(s
, 0x1a, 0x24, cylinders
, heads
, sectors
);
657 rtc_set_memory(s
, 0x12, val
);
660 for (i
= 0; i
< 4; i
++) {
661 /* NOTE: ide_get_geometry() returns the physical
662 geometry. It is always such that: 1 <= sects <= 63, 1
663 <= heads <= 16, 1 <= cylinders <= 16383. The BIOS
664 geometry can be different if a translation is done. */
665 if (arg
->idebus
[i
/ 2] &&
666 ide_get_geometry(arg
->idebus
[i
/ 2], i
% 2,
667 &cylinders
, &heads
, §ors
) >= 0) {
668 trans
= ide_get_bios_chs_trans(arg
->idebus
[i
/ 2], i
% 2) - 1;
669 assert((trans
& ~3) == 0);
670 val
|= trans
<< (i
* 2);
673 rtc_set_memory(s
, 0x39, val
);
675 pc_cmos_init_floppy(s
, pc_find_fdc0());
677 qemu_unregister_reset(pc_cmos_init_late
, opaque
);
680 void pc_cmos_init(PCMachineState
*pcms
,
681 BusState
*idebus0
, BusState
*idebus1
,
685 static pc_cmos_init_late_arg arg
;
686 X86MachineState
*x86ms
= X86_MACHINE(pcms
);
688 /* various important CMOS locations needed by PC/Bochs bios */
691 /* base memory (first MiB) */
692 val
= MIN(x86ms
->below_4g_mem_size
/ KiB
, 640);
693 rtc_set_memory(s
, 0x15, val
);
694 rtc_set_memory(s
, 0x16, val
>> 8);
695 /* extended memory (next 64MiB) */
696 if (x86ms
->below_4g_mem_size
> 1 * MiB
) {
697 val
= (x86ms
->below_4g_mem_size
- 1 * MiB
) / KiB
;
703 rtc_set_memory(s
, 0x17, val
);
704 rtc_set_memory(s
, 0x18, val
>> 8);
705 rtc_set_memory(s
, 0x30, val
);
706 rtc_set_memory(s
, 0x31, val
>> 8);
707 /* memory between 16MiB and 4GiB */
708 if (x86ms
->below_4g_mem_size
> 16 * MiB
) {
709 val
= (x86ms
->below_4g_mem_size
- 16 * MiB
) / (64 * KiB
);
715 rtc_set_memory(s
, 0x34, val
);
716 rtc_set_memory(s
, 0x35, val
>> 8);
717 /* memory above 4GiB */
718 val
= x86ms
->above_4g_mem_size
/ 65536;
719 rtc_set_memory(s
, 0x5b, val
);
720 rtc_set_memory(s
, 0x5c, val
>> 8);
721 rtc_set_memory(s
, 0x5d, val
>> 16);
723 object_property_add_link(OBJECT(pcms
), "rtc_state",
725 (Object
**)&x86ms
->rtc
,
726 object_property_allow_set_link
,
727 OBJ_PROP_LINK_STRONG
, &error_abort
);
728 object_property_set_link(OBJECT(pcms
), OBJECT(s
),
729 "rtc_state", &error_abort
);
731 set_boot_dev(s
, MACHINE(pcms
)->boot_order
, &error_fatal
);
734 val
|= 0x02; /* FPU is there */
735 val
|= 0x04; /* PS/2 mouse installed */
736 rtc_set_memory(s
, REG_EQUIPMENT_BYTE
, val
);
738 /* hard drives and FDC */
740 arg
.idebus
[0] = idebus0
;
741 arg
.idebus
[1] = idebus1
;
742 qemu_register_reset(pc_cmos_init_late
, &arg
);
745 #define TYPE_PORT92 "port92"
746 #define PORT92(obj) OBJECT_CHECK(Port92State, (obj), TYPE_PORT92)
748 /* port 92 stuff: could be split off */
749 typedef struct Port92State
{
750 ISADevice parent_obj
;
757 static void port92_write(void *opaque
, hwaddr addr
, uint64_t val
,
760 Port92State
*s
= opaque
;
761 int oldval
= s
->outport
;
763 DPRINTF("port92: write 0x%02" PRIx64
"\n", val
);
765 qemu_set_irq(s
->a20_out
, (val
>> 1) & 1);
766 if ((val
& 1) && !(oldval
& 1)) {
767 qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET
);
771 static uint64_t port92_read(void *opaque
, hwaddr addr
,
774 Port92State
*s
= opaque
;
778 DPRINTF("port92: read 0x%02x\n", ret
);
782 static void port92_init(ISADevice
*dev
, qemu_irq a20_out
)
784 qdev_connect_gpio_out_named(DEVICE(dev
), PORT92_A20_LINE
, 0, a20_out
);
787 static const VMStateDescription vmstate_port92_isa
= {
790 .minimum_version_id
= 1,
791 .fields
= (VMStateField
[]) {
792 VMSTATE_UINT8(outport
, Port92State
),
793 VMSTATE_END_OF_LIST()
797 static void port92_reset(DeviceState
*d
)
799 Port92State
*s
= PORT92(d
);
804 static const MemoryRegionOps port92_ops
= {
806 .write
= port92_write
,
808 .min_access_size
= 1,
809 .max_access_size
= 1,
811 .endianness
= DEVICE_LITTLE_ENDIAN
,
814 static void port92_initfn(Object
*obj
)
816 Port92State
*s
= PORT92(obj
);
818 memory_region_init_io(&s
->io
, OBJECT(s
), &port92_ops
, s
, "port92", 1);
822 qdev_init_gpio_out_named(DEVICE(obj
), &s
->a20_out
, PORT92_A20_LINE
, 1);
825 static void port92_realizefn(DeviceState
*dev
, Error
**errp
)
827 ISADevice
*isadev
= ISA_DEVICE(dev
);
828 Port92State
*s
= PORT92(dev
);
830 isa_register_ioport(isadev
, &s
->io
, 0x92);
833 static void port92_class_initfn(ObjectClass
*klass
, void *data
)
835 DeviceClass
*dc
= DEVICE_CLASS(klass
);
837 dc
->realize
= port92_realizefn
;
838 dc
->reset
= port92_reset
;
839 dc
->vmsd
= &vmstate_port92_isa
;
841 * Reason: unlike ordinary ISA devices, this one needs additional
842 * wiring: its A20 output line needs to be wired up by
845 dc
->user_creatable
= false;
848 static const TypeInfo port92_info
= {
850 .parent
= TYPE_ISA_DEVICE
,
851 .instance_size
= sizeof(Port92State
),
852 .instance_init
= port92_initfn
,
853 .class_init
= port92_class_initfn
,
856 static void port92_register_types(void)
858 type_register_static(&port92_info
);
861 type_init(port92_register_types
)
863 static void handle_a20_line_change(void *opaque
, int irq
, int level
)
865 X86CPU
*cpu
= opaque
;
867 /* XXX: send to all CPUs ? */
868 /* XXX: add logic to handle multiple A20 line sources */
869 x86_cpu_set_a20(cpu
, level
);
872 #define NE2000_NB_MAX 6
874 static const int ne2000_io
[NE2000_NB_MAX
] = { 0x300, 0x320, 0x340, 0x360,
876 static const int ne2000_irq
[NE2000_NB_MAX
] = { 9, 10, 11, 3, 4, 5 };
878 void pc_init_ne2k_isa(ISABus
*bus
, NICInfo
*nd
)
880 static int nb_ne2k
= 0;
882 if (nb_ne2k
== NE2000_NB_MAX
)
884 isa_ne2000_init(bus
, ne2000_io
[nb_ne2k
],
885 ne2000_irq
[nb_ne2k
], nd
);
889 DeviceState
*cpu_get_current_apic(void)
892 X86CPU
*cpu
= X86_CPU(current_cpu
);
893 return cpu
->apic_state
;
899 void pc_acpi_smi_interrupt(void *opaque
, int irq
, int level
)
901 X86CPU
*cpu
= opaque
;
904 cpu_interrupt(CPU(cpu
), CPU_INTERRUPT_SMI
);
909 * This function is very similar to smp_parse()
910 * in hw/core/machine.c but includes CPU die support.
912 void pc_smp_parse(MachineState
*ms
, QemuOpts
*opts
)
914 X86MachineState
*x86ms
= X86_MACHINE(ms
);
917 unsigned cpus
= qemu_opt_get_number(opts
, "cpus", 0);
918 unsigned sockets
= qemu_opt_get_number(opts
, "sockets", 0);
919 unsigned dies
= qemu_opt_get_number(opts
, "dies", 1);
920 unsigned cores
= qemu_opt_get_number(opts
, "cores", 0);
921 unsigned threads
= qemu_opt_get_number(opts
, "threads", 0);
923 /* compute missing values, prefer sockets over cores over threads */
924 if (cpus
== 0 || sockets
== 0) {
925 cores
= cores
> 0 ? cores
: 1;
926 threads
= threads
> 0 ? threads
: 1;
928 sockets
= sockets
> 0 ? sockets
: 1;
929 cpus
= cores
* threads
* dies
* sockets
;
932 qemu_opt_get_number(opts
, "maxcpus", cpus
);
933 sockets
= ms
->smp
.max_cpus
/ (cores
* threads
* dies
);
935 } else if (cores
== 0) {
936 threads
= threads
> 0 ? threads
: 1;
937 cores
= cpus
/ (sockets
* dies
* threads
);
938 cores
= cores
> 0 ? cores
: 1;
939 } else if (threads
== 0) {
940 threads
= cpus
/ (cores
* dies
* sockets
);
941 threads
= threads
> 0 ? threads
: 1;
942 } else if (sockets
* dies
* cores
* threads
< cpus
) {
943 error_report("cpu topology: "
944 "sockets (%u) * dies (%u) * cores (%u) * threads (%u) < "
946 sockets
, dies
, cores
, threads
, cpus
);
951 qemu_opt_get_number(opts
, "maxcpus", cpus
);
953 if (ms
->smp
.max_cpus
< cpus
) {
954 error_report("maxcpus must be equal to or greater than smp");
958 if (sockets
* dies
* cores
* threads
> ms
->smp
.max_cpus
) {
959 error_report("cpu topology: "
960 "sockets (%u) * dies (%u) * cores (%u) * threads (%u) > "
962 sockets
, dies
, cores
, threads
,
967 if (sockets
* dies
* cores
* threads
!= ms
->smp
.max_cpus
) {
968 warn_report("Invalid CPU topology deprecated: "
969 "sockets (%u) * dies (%u) * cores (%u) * threads (%u) "
971 sockets
, dies
, cores
, threads
,
976 ms
->smp
.cores
= cores
;
977 ms
->smp
.threads
= threads
;
978 x86ms
->smp_dies
= dies
;
981 if (ms
->smp
.cpus
> 1) {
982 Error
*blocker
= NULL
;
983 error_setg(&blocker
, QERR_REPLAY_NOT_SUPPORTED
, "smp");
984 replay_add_blocker(blocker
);
988 void pc_hot_add_cpu(MachineState
*ms
, const int64_t id
, Error
**errp
)
990 X86MachineState
*x86ms
= X86_MACHINE(ms
);
991 int64_t apic_id
= x86_cpu_apic_id_from_index(x86ms
, id
);
992 Error
*local_err
= NULL
;
995 error_setg(errp
, "Invalid CPU id: %" PRIi64
, id
);
999 if (apic_id
>= ACPI_CPU_HOTPLUG_ID_LIMIT
) {
1000 error_setg(errp
, "Unable to add CPU: %" PRIi64
1001 ", resulting APIC ID (%" PRIi64
") is too large",
1007 x86_cpu_new(X86_MACHINE(ms
), apic_id
, &local_err
);
1009 error_propagate(errp
, local_err
);
1014 static void rtc_set_cpus_count(ISADevice
*rtc
, uint16_t cpus_count
)
1016 if (cpus_count
> 0xff) {
1017 /* If the number of CPUs can't be represented in 8 bits, the
1018 * BIOS must use "FW_CFG_NB_CPUS". Set RTC field to 0 just
1019 * to make old BIOSes fail more predictably.
1021 rtc_set_memory(rtc
, 0x5f, 0);
1023 rtc_set_memory(rtc
, 0x5f, cpus_count
- 1);
1028 void pc_machine_done(Notifier
*notifier
, void *data
)
1030 PCMachineState
*pcms
= container_of(notifier
,
1031 PCMachineState
, machine_done
);
1032 X86MachineState
*x86ms
= X86_MACHINE(pcms
);
1033 PCIBus
*bus
= pcms
->bus
;
1035 /* set the number of CPUs */
1036 rtc_set_cpus_count(x86ms
->rtc
, x86ms
->boot_cpus
);
1039 int extra_hosts
= 0;
1041 QLIST_FOREACH(bus
, &bus
->child
, sibling
) {
1042 /* look for expander root buses */
1043 if (pci_bus_is_root(bus
)) {
1047 if (extra_hosts
&& x86ms
->fw_cfg
) {
1048 uint64_t *val
= g_malloc(sizeof(*val
));
1049 *val
= cpu_to_le64(extra_hosts
);
1050 fw_cfg_add_file(x86ms
->fw_cfg
,
1051 "etc/extra-pci-roots", val
, sizeof(*val
));
1056 if (x86ms
->fw_cfg
) {
1057 fw_cfg_build_smbios(MACHINE(pcms
), x86ms
->fw_cfg
);
1058 fw_cfg_build_feature_control(MACHINE(pcms
), x86ms
->fw_cfg
);
1059 /* update FW_CFG_NB_CPUS to account for -device added CPUs */
1060 fw_cfg_modify_i16(x86ms
->fw_cfg
, FW_CFG_NB_CPUS
, x86ms
->boot_cpus
);
1063 if (x86ms
->apic_id_limit
> 255 && !xen_enabled()) {
1064 IntelIOMMUState
*iommu
= INTEL_IOMMU_DEVICE(x86_iommu_get_default());
1066 if (!iommu
|| !x86_iommu_ir_supported(X86_IOMMU_DEVICE(iommu
)) ||
1067 iommu
->intr_eim
!= ON_OFF_AUTO_ON
) {
1068 error_report("current -smp configuration requires "
1069 "Extended Interrupt Mode enabled. "
1070 "You can add an IOMMU using: "
1071 "-device intel-iommu,intremap=on,eim=on");
1077 void pc_guest_info_init(PCMachineState
*pcms
)
1080 MachineState
*ms
= MACHINE(pcms
);
1081 X86MachineState
*x86ms
= X86_MACHINE(pcms
);
1083 x86ms
->apic_xrupt_override
= kvm_allows_irq0_override();
1084 pcms
->numa_nodes
= ms
->numa_state
->num_nodes
;
1085 pcms
->node_mem
= g_malloc0(pcms
->numa_nodes
*
1086 sizeof *pcms
->node_mem
);
1087 for (i
= 0; i
< ms
->numa_state
->num_nodes
; i
++) {
1088 pcms
->node_mem
[i
] = ms
->numa_state
->nodes
[i
].node_mem
;
1091 pcms
->machine_done
.notify
= pc_machine_done
;
1092 qemu_add_machine_init_done_notifier(&pcms
->machine_done
);
1095 /* setup pci memory address space mapping into system address space */
1096 void pc_pci_as_mapping_init(Object
*owner
, MemoryRegion
*system_memory
,
1097 MemoryRegion
*pci_address_space
)
1099 /* Set to lower priority than RAM */
1100 memory_region_add_subregion_overlap(system_memory
, 0x0,
1101 pci_address_space
, -1);
1104 void xen_load_linux(PCMachineState
*pcms
)
1108 PCMachineClass
*pcmc
= PC_MACHINE_GET_CLASS(pcms
);
1109 X86MachineState
*x86ms
= X86_MACHINE(pcms
);
1111 assert(MACHINE(pcms
)->kernel_filename
!= NULL
);
1113 fw_cfg
= fw_cfg_init_io(FW_CFG_IO_BASE
);
1114 fw_cfg_add_i16(fw_cfg
, FW_CFG_NB_CPUS
, x86ms
->boot_cpus
);
1117 x86_load_linux(x86ms
, fw_cfg
, pcmc
->acpi_data_size
,
1118 pcmc
->pvh_enabled
, pcmc
->linuxboot_dma_enabled
);
1119 for (i
= 0; i
< nb_option_roms
; i
++) {
1120 assert(!strcmp(option_rom
[i
].name
, "linuxboot.bin") ||
1121 !strcmp(option_rom
[i
].name
, "linuxboot_dma.bin") ||
1122 !strcmp(option_rom
[i
].name
, "pvh.bin") ||
1123 !strcmp(option_rom
[i
].name
, "multiboot.bin"));
1124 rom_add_option(option_rom
[i
].name
, option_rom
[i
].bootindex
);
1126 x86ms
->fw_cfg
= fw_cfg
;
1129 void pc_memory_init(PCMachineState
*pcms
,
1130 MemoryRegion
*system_memory
,
1131 MemoryRegion
*rom_memory
,
1132 MemoryRegion
**ram_memory
)
1135 MemoryRegion
*ram
, *option_rom_mr
;
1136 MemoryRegion
*ram_below_4g
, *ram_above_4g
;
1138 MachineState
*machine
= MACHINE(pcms
);
1139 MachineClass
*mc
= MACHINE_GET_CLASS(machine
);
1140 PCMachineClass
*pcmc
= PC_MACHINE_GET_CLASS(pcms
);
1141 X86MachineState
*x86ms
= X86_MACHINE(pcms
);
1143 assert(machine
->ram_size
== x86ms
->below_4g_mem_size
+
1144 x86ms
->above_4g_mem_size
);
1146 linux_boot
= (machine
->kernel_filename
!= NULL
);
1148 /* Allocate RAM. We allocate it as a single memory region and use
1149 * aliases to address portions of it, mostly for backwards compatibility
1150 * with older qemus that used qemu_ram_alloc().
1152 ram
= g_malloc(sizeof(*ram
));
1153 memory_region_allocate_system_memory(ram
, NULL
, "pc.ram",
1156 ram_below_4g
= g_malloc(sizeof(*ram_below_4g
));
1157 memory_region_init_alias(ram_below_4g
, NULL
, "ram-below-4g", ram
,
1158 0, x86ms
->below_4g_mem_size
);
1159 memory_region_add_subregion(system_memory
, 0, ram_below_4g
);
1160 e820_add_entry(0, x86ms
->below_4g_mem_size
, E820_RAM
);
1161 if (x86ms
->above_4g_mem_size
> 0) {
1162 ram_above_4g
= g_malloc(sizeof(*ram_above_4g
));
1163 memory_region_init_alias(ram_above_4g
, NULL
, "ram-above-4g", ram
,
1164 x86ms
->below_4g_mem_size
,
1165 x86ms
->above_4g_mem_size
);
1166 memory_region_add_subregion(system_memory
, 0x100000000ULL
,
1168 e820_add_entry(0x100000000ULL
, x86ms
->above_4g_mem_size
, E820_RAM
);
1171 if (!pcmc
->has_reserved_memory
&&
1172 (machine
->ram_slots
||
1173 (machine
->maxram_size
> machine
->ram_size
))) {
1175 error_report("\"-memory 'slots|maxmem'\" is not supported by: %s",
1180 /* always allocate the device memory information */
1181 machine
->device_memory
= g_malloc0(sizeof(*machine
->device_memory
));
1183 /* initialize device memory address space */
1184 if (pcmc
->has_reserved_memory
&&
1185 (machine
->ram_size
< machine
->maxram_size
)) {
1186 ram_addr_t device_mem_size
= machine
->maxram_size
- machine
->ram_size
;
1188 if (machine
->ram_slots
> ACPI_MAX_RAM_SLOTS
) {
1189 error_report("unsupported amount of memory slots: %"PRIu64
,
1190 machine
->ram_slots
);
1194 if (QEMU_ALIGN_UP(machine
->maxram_size
,
1195 TARGET_PAGE_SIZE
) != machine
->maxram_size
) {
1196 error_report("maximum memory size must by aligned to multiple of "
1197 "%d bytes", TARGET_PAGE_SIZE
);
1201 machine
->device_memory
->base
=
1202 ROUND_UP(0x100000000ULL
+ x86ms
->above_4g_mem_size
, 1 * GiB
);
1204 if (pcmc
->enforce_aligned_dimm
) {
1205 /* size device region assuming 1G page max alignment per slot */
1206 device_mem_size
+= (1 * GiB
) * machine
->ram_slots
;
1209 if ((machine
->device_memory
->base
+ device_mem_size
) <
1211 error_report("unsupported amount of maximum memory: " RAM_ADDR_FMT
,
1212 machine
->maxram_size
);
1216 memory_region_init(&machine
->device_memory
->mr
, OBJECT(pcms
),
1217 "device-memory", device_mem_size
);
1218 memory_region_add_subregion(system_memory
, machine
->device_memory
->base
,
1219 &machine
->device_memory
->mr
);
1222 /* Initialize PC system firmware */
1223 pc_system_firmware_init(pcms
, rom_memory
);
1225 option_rom_mr
= g_malloc(sizeof(*option_rom_mr
));
1226 memory_region_init_ram(option_rom_mr
, NULL
, "pc.rom", PC_ROM_SIZE
,
1228 if (pcmc
->pci_enabled
) {
1229 memory_region_set_readonly(option_rom_mr
, true);
1231 memory_region_add_subregion_overlap(rom_memory
,
1236 fw_cfg
= fw_cfg_arch_create(machine
,
1237 x86ms
->boot_cpus
, x86ms
->apic_id_limit
);
1241 if (pcmc
->has_reserved_memory
&& machine
->device_memory
->base
) {
1242 uint64_t *val
= g_malloc(sizeof(*val
));
1243 PCMachineClass
*pcmc
= PC_MACHINE_GET_CLASS(pcms
);
1244 uint64_t res_mem_end
= machine
->device_memory
->base
;
1246 if (!pcmc
->broken_reserved_end
) {
1247 res_mem_end
+= memory_region_size(&machine
->device_memory
->mr
);
1249 *val
= cpu_to_le64(ROUND_UP(res_mem_end
, 1 * GiB
));
1250 fw_cfg_add_file(fw_cfg
, "etc/reserved-memory-end", val
, sizeof(*val
));
1254 x86_load_linux(x86ms
, fw_cfg
, pcmc
->acpi_data_size
,
1255 pcmc
->pvh_enabled
, pcmc
->linuxboot_dma_enabled
);
1258 for (i
= 0; i
< nb_option_roms
; i
++) {
1259 rom_add_option(option_rom
[i
].name
, option_rom
[i
].bootindex
);
1261 x86ms
->fw_cfg
= fw_cfg
;
1263 /* Init default IOAPIC address space */
1264 x86ms
->ioapic_as
= &address_space_memory
;
1266 /* Init ACPI memory hotplug IO base address */
1267 pcms
->memhp_io_base
= ACPI_MEMORY_HOTPLUG_BASE
;
1271 * The 64bit pci hole starts after "above 4G RAM" and
1272 * potentially the space reserved for memory hotplug.
1274 uint64_t pc_pci_hole64_start(void)
1276 PCMachineState
*pcms
= PC_MACHINE(qdev_get_machine());
1277 PCMachineClass
*pcmc
= PC_MACHINE_GET_CLASS(pcms
);
1278 MachineState
*ms
= MACHINE(pcms
);
1279 X86MachineState
*x86ms
= X86_MACHINE(pcms
);
1280 uint64_t hole64_start
= 0;
1282 if (pcmc
->has_reserved_memory
&& ms
->device_memory
->base
) {
1283 hole64_start
= ms
->device_memory
->base
;
1284 if (!pcmc
->broken_reserved_end
) {
1285 hole64_start
+= memory_region_size(&ms
->device_memory
->mr
);
1288 hole64_start
= 0x100000000ULL
+ x86ms
->above_4g_mem_size
;
1291 return ROUND_UP(hole64_start
, 1 * GiB
);
1294 qemu_irq
pc_allocate_cpu_irq(void)
1296 return qemu_allocate_irq(pic_irq_request
, NULL
, 0);
1299 DeviceState
*pc_vga_init(ISABus
*isa_bus
, PCIBus
*pci_bus
)
1301 DeviceState
*dev
= NULL
;
1303 rom_set_order_override(FW_CFG_ORDER_OVERRIDE_VGA
);
1305 PCIDevice
*pcidev
= pci_vga_init(pci_bus
);
1306 dev
= pcidev
? &pcidev
->qdev
: NULL
;
1307 } else if (isa_bus
) {
1308 ISADevice
*isadev
= isa_vga_init(isa_bus
);
1309 dev
= isadev
? DEVICE(isadev
) : NULL
;
1311 rom_reset_order_override();
1315 static const MemoryRegionOps ioport80_io_ops
= {
1316 .write
= ioport80_write
,
1317 .read
= ioport80_read
,
1318 .endianness
= DEVICE_NATIVE_ENDIAN
,
1320 .min_access_size
= 1,
1321 .max_access_size
= 1,
1325 static const MemoryRegionOps ioportF0_io_ops
= {
1326 .write
= ioportF0_write
,
1327 .read
= ioportF0_read
,
1328 .endianness
= DEVICE_NATIVE_ENDIAN
,
1330 .min_access_size
= 1,
1331 .max_access_size
= 1,
1335 static void pc_superio_init(ISABus
*isa_bus
, bool create_fdctrl
, bool no_vmport
)
1338 DriveInfo
*fd
[MAX_FD
];
1340 ISADevice
*i8042
, *port92
, *vmmouse
;
1342 serial_hds_isa_init(isa_bus
, 0, MAX_ISA_SERIAL_PORTS
);
1343 parallel_hds_isa_init(isa_bus
, MAX_PARALLEL_PORTS
);
1345 for (i
= 0; i
< MAX_FD
; i
++) {
1346 fd
[i
] = drive_get(IF_FLOPPY
, 0, i
);
1347 create_fdctrl
|= !!fd
[i
];
1349 if (create_fdctrl
) {
1350 fdctrl_init_isa(isa_bus
, fd
);
1353 i8042
= isa_create_simple(isa_bus
, "i8042");
1355 vmport_init(isa_bus
);
1356 vmmouse
= isa_try_create(isa_bus
, "vmmouse");
1361 DeviceState
*dev
= DEVICE(vmmouse
);
1362 qdev_prop_set_ptr(dev
, "ps2_mouse", i8042
);
1363 qdev_init_nofail(dev
);
1365 port92
= isa_create_simple(isa_bus
, "port92");
1367 a20_line
= qemu_allocate_irqs(handle_a20_line_change
, first_cpu
, 2);
1368 i8042_setup_a20_line(i8042
, a20_line
[0]);
1369 port92_init(port92
, a20_line
[1]);
1373 void pc_basic_device_init(ISABus
*isa_bus
, qemu_irq
*gsi
,
1374 ISADevice
**rtc_state
,
1381 DeviceState
*hpet
= NULL
;
1382 int pit_isa_irq
= 0;
1383 qemu_irq pit_alt_irq
= NULL
;
1384 qemu_irq rtc_irq
= NULL
;
1385 ISADevice
*pit
= NULL
;
1386 MemoryRegion
*ioport80_io
= g_new(MemoryRegion
, 1);
1387 MemoryRegion
*ioportF0_io
= g_new(MemoryRegion
, 1);
1389 memory_region_init_io(ioport80_io
, NULL
, &ioport80_io_ops
, NULL
, "ioport80", 1);
1390 memory_region_add_subregion(isa_bus
->address_space_io
, 0x80, ioport80_io
);
1392 memory_region_init_io(ioportF0_io
, NULL
, &ioportF0_io_ops
, NULL
, "ioportF0", 1);
1393 memory_region_add_subregion(isa_bus
->address_space_io
, 0xf0, ioportF0_io
);
1396 * Check if an HPET shall be created.
1398 * Without KVM_CAP_PIT_STATE2, we cannot switch off the in-kernel PIT
1399 * when the HPET wants to take over. Thus we have to disable the latter.
1401 if (!no_hpet
&& (!kvm_irqchip_in_kernel() || kvm_has_pit_state2())) {
1402 /* In order to set property, here not using sysbus_try_create_simple */
1403 hpet
= qdev_try_create(NULL
, TYPE_HPET
);
1405 /* For pc-piix-*, hpet's intcap is always IRQ2. For pc-q35-1.7
1406 * and earlier, use IRQ2 for compat. Otherwise, use IRQ16~23,
1409 uint8_t compat
= object_property_get_uint(OBJECT(hpet
),
1412 qdev_prop_set_uint32(hpet
, HPET_INTCAP
, hpet_irqs
);
1414 qdev_init_nofail(hpet
);
1415 sysbus_mmio_map(SYS_BUS_DEVICE(hpet
), 0, HPET_BASE
);
1417 for (i
= 0; i
< GSI_NUM_PINS
; i
++) {
1418 sysbus_connect_irq(SYS_BUS_DEVICE(hpet
), i
, gsi
[i
]);
1421 pit_alt_irq
= qdev_get_gpio_in(hpet
, HPET_LEGACY_PIT_INT
);
1422 rtc_irq
= qdev_get_gpio_in(hpet
, HPET_LEGACY_RTC_INT
);
1425 *rtc_state
= mc146818_rtc_init(isa_bus
, 2000, rtc_irq
);
1427 qemu_register_boot_set(pc_boot_set
, *rtc_state
);
1429 if (!xen_enabled() && has_pit
) {
1430 if (kvm_pit_in_kernel()) {
1431 pit
= kvm_pit_init(isa_bus
, 0x40);
1433 pit
= i8254_pit_init(isa_bus
, 0x40, pit_isa_irq
, pit_alt_irq
);
1436 /* connect PIT to output control line of the HPET */
1437 qdev_connect_gpio_out(hpet
, 0, qdev_get_gpio_in(DEVICE(pit
), 0));
1439 pcspk_init(isa_bus
, pit
);
1442 i8257_dma_init(isa_bus
, 0);
1445 pc_superio_init(isa_bus
, create_fdctrl
, no_vmport
);
1448 void pc_nic_init(PCMachineClass
*pcmc
, ISABus
*isa_bus
, PCIBus
*pci_bus
)
1452 rom_set_order_override(FW_CFG_ORDER_OVERRIDE_NIC
);
1453 for (i
= 0; i
< nb_nics
; i
++) {
1454 NICInfo
*nd
= &nd_table
[i
];
1455 const char *model
= nd
->model
? nd
->model
: pcmc
->default_nic_model
;
1457 if (g_str_equal(model
, "ne2k_isa")) {
1458 pc_init_ne2k_isa(isa_bus
, nd
);
1460 pci_nic_init_nofail(nd
, pci_bus
, model
, NULL
);
1463 rom_reset_order_override();
1466 void pc_i8259_create(ISABus
*isa_bus
, qemu_irq
*i8259_irqs
)
1470 if (kvm_pic_in_kernel()) {
1471 i8259
= kvm_i8259_init(isa_bus
);
1472 } else if (xen_enabled()) {
1473 i8259
= xen_interrupt_controller_init();
1475 i8259
= i8259_init(isa_bus
, pc_allocate_cpu_irq());
1478 for (size_t i
= 0; i
< ISA_NUM_IRQS
; i
++) {
1479 i8259_irqs
[i
] = i8259
[i
];
1485 void ioapic_init_gsi(GSIState
*gsi_state
, const char *parent_name
)
1491 if (kvm_ioapic_in_kernel()) {
1492 dev
= qdev_create(NULL
, TYPE_KVM_IOAPIC
);
1494 dev
= qdev_create(NULL
, TYPE_IOAPIC
);
1497 object_property_add_child(object_resolve_path(parent_name
, NULL
),
1498 "ioapic", OBJECT(dev
), NULL
);
1500 qdev_init_nofail(dev
);
1501 d
= SYS_BUS_DEVICE(dev
);
1502 sysbus_mmio_map(d
, 0, IO_APIC_DEFAULT_ADDRESS
);
1504 for (i
= 0; i
< IOAPIC_NUM_PINS
; i
++) {
1505 gsi_state
->ioapic_irq
[i
] = qdev_get_gpio_in(dev
, i
);
1509 static void pc_memory_pre_plug(HotplugHandler
*hotplug_dev
, DeviceState
*dev
,
1512 const PCMachineState
*pcms
= PC_MACHINE(hotplug_dev
);
1513 const PCMachineClass
*pcmc
= PC_MACHINE_GET_CLASS(pcms
);
1514 const MachineState
*ms
= MACHINE(hotplug_dev
);
1515 const bool is_nvdimm
= object_dynamic_cast(OBJECT(dev
), TYPE_NVDIMM
);
1516 const uint64_t legacy_align
= TARGET_PAGE_SIZE
;
1517 Error
*local_err
= NULL
;
1520 * When -no-acpi is used with Q35 machine type, no ACPI is built,
1521 * but pcms->acpi_dev is still created. Check !acpi_enabled in
1522 * addition to cover this case.
1524 if (!pcms
->acpi_dev
|| !acpi_enabled
) {
1526 "memory hotplug is not enabled: missing acpi device or acpi disabled");
1530 if (is_nvdimm
&& !ms
->nvdimms_state
->is_enabled
) {
1531 error_setg(errp
, "nvdimm is not enabled: missing 'nvdimm' in '-M'");
1535 hotplug_handler_pre_plug(pcms
->acpi_dev
, dev
, &local_err
);
1537 error_propagate(errp
, local_err
);
1541 pc_dimm_pre_plug(PC_DIMM(dev
), MACHINE(hotplug_dev
),
1542 pcmc
->enforce_aligned_dimm
? NULL
: &legacy_align
, errp
);
1545 static void pc_memory_plug(HotplugHandler
*hotplug_dev
,
1546 DeviceState
*dev
, Error
**errp
)
1548 Error
*local_err
= NULL
;
1549 PCMachineState
*pcms
= PC_MACHINE(hotplug_dev
);
1550 MachineState
*ms
= MACHINE(hotplug_dev
);
1551 bool is_nvdimm
= object_dynamic_cast(OBJECT(dev
), TYPE_NVDIMM
);
1553 pc_dimm_plug(PC_DIMM(dev
), MACHINE(pcms
), &local_err
);
1559 nvdimm_plug(ms
->nvdimms_state
);
1562 hotplug_handler_plug(HOTPLUG_HANDLER(pcms
->acpi_dev
), dev
, &error_abort
);
1564 error_propagate(errp
, local_err
);
1567 static void pc_memory_unplug_request(HotplugHandler
*hotplug_dev
,
1568 DeviceState
*dev
, Error
**errp
)
1570 Error
*local_err
= NULL
;
1571 PCMachineState
*pcms
= PC_MACHINE(hotplug_dev
);
1574 * When -no-acpi is used with Q35 machine type, no ACPI is built,
1575 * but pcms->acpi_dev is still created. Check !acpi_enabled in
1576 * addition to cover this case.
1578 if (!pcms
->acpi_dev
|| !acpi_enabled
) {
1579 error_setg(&local_err
,
1580 "memory hotplug is not enabled: missing acpi device or acpi disabled");
1584 if (object_dynamic_cast(OBJECT(dev
), TYPE_NVDIMM
)) {
1585 error_setg(&local_err
,
1586 "nvdimm device hot unplug is not supported yet.");
1590 hotplug_handler_unplug_request(HOTPLUG_HANDLER(pcms
->acpi_dev
), dev
,
1593 error_propagate(errp
, local_err
);
1596 static void pc_memory_unplug(HotplugHandler
*hotplug_dev
,
1597 DeviceState
*dev
, Error
**errp
)
1599 PCMachineState
*pcms
= PC_MACHINE(hotplug_dev
);
1600 Error
*local_err
= NULL
;
1602 hotplug_handler_unplug(HOTPLUG_HANDLER(pcms
->acpi_dev
), dev
, &local_err
);
1607 pc_dimm_unplug(PC_DIMM(dev
), MACHINE(pcms
));
1608 object_property_set_bool(OBJECT(dev
), false, "realized", NULL
);
1610 error_propagate(errp
, local_err
);
1613 static int pc_apic_cmp(const void *a
, const void *b
)
1615 CPUArchId
*apic_a
= (CPUArchId
*)a
;
1616 CPUArchId
*apic_b
= (CPUArchId
*)b
;
1618 return apic_a
->arch_id
- apic_b
->arch_id
;
1621 /* returns pointer to CPUArchId descriptor that matches CPU's apic_id
1622 * in ms->possible_cpus->cpus, if ms->possible_cpus->cpus has no
1623 * entry corresponding to CPU's apic_id returns NULL.
1625 static CPUArchId
*pc_find_cpu_slot(MachineState
*ms
, uint32_t id
, int *idx
)
1627 CPUArchId apic_id
, *found_cpu
;
1629 apic_id
.arch_id
= id
;
1630 found_cpu
= bsearch(&apic_id
, ms
->possible_cpus
->cpus
,
1631 ms
->possible_cpus
->len
, sizeof(*ms
->possible_cpus
->cpus
),
1633 if (found_cpu
&& idx
) {
1634 *idx
= found_cpu
- ms
->possible_cpus
->cpus
;
1639 static void pc_cpu_plug(HotplugHandler
*hotplug_dev
,
1640 DeviceState
*dev
, Error
**errp
)
1642 CPUArchId
*found_cpu
;
1643 Error
*local_err
= NULL
;
1644 X86CPU
*cpu
= X86_CPU(dev
);
1645 PCMachineState
*pcms
= PC_MACHINE(hotplug_dev
);
1646 X86MachineState
*x86ms
= X86_MACHINE(pcms
);
1648 if (pcms
->acpi_dev
) {
1649 hotplug_handler_plug(HOTPLUG_HANDLER(pcms
->acpi_dev
), dev
, &local_err
);
1655 /* increment the number of CPUs */
1658 rtc_set_cpus_count(x86ms
->rtc
, x86ms
->boot_cpus
);
1660 if (x86ms
->fw_cfg
) {
1661 fw_cfg_modify_i16(x86ms
->fw_cfg
, FW_CFG_NB_CPUS
, x86ms
->boot_cpus
);
1664 found_cpu
= pc_find_cpu_slot(MACHINE(pcms
), cpu
->apic_id
, NULL
);
1665 found_cpu
->cpu
= OBJECT(dev
);
1667 error_propagate(errp
, local_err
);
1669 static void pc_cpu_unplug_request_cb(HotplugHandler
*hotplug_dev
,
1670 DeviceState
*dev
, Error
**errp
)
1673 Error
*local_err
= NULL
;
1674 X86CPU
*cpu
= X86_CPU(dev
);
1675 PCMachineState
*pcms
= PC_MACHINE(hotplug_dev
);
1677 if (!pcms
->acpi_dev
) {
1678 error_setg(&local_err
, "CPU hot unplug not supported without ACPI");
1682 pc_find_cpu_slot(MACHINE(pcms
), cpu
->apic_id
, &idx
);
1685 error_setg(&local_err
, "Boot CPU is unpluggable");
1689 hotplug_handler_unplug_request(HOTPLUG_HANDLER(pcms
->acpi_dev
), dev
,
1696 error_propagate(errp
, local_err
);
1700 static void pc_cpu_unplug_cb(HotplugHandler
*hotplug_dev
,
1701 DeviceState
*dev
, Error
**errp
)
1703 CPUArchId
*found_cpu
;
1704 Error
*local_err
= NULL
;
1705 X86CPU
*cpu
= X86_CPU(dev
);
1706 PCMachineState
*pcms
= PC_MACHINE(hotplug_dev
);
1707 X86MachineState
*x86ms
= X86_MACHINE(pcms
);
1709 hotplug_handler_unplug(HOTPLUG_HANDLER(pcms
->acpi_dev
), dev
, &local_err
);
1714 found_cpu
= pc_find_cpu_slot(MACHINE(pcms
), cpu
->apic_id
, NULL
);
1715 found_cpu
->cpu
= NULL
;
1716 object_property_set_bool(OBJECT(dev
), false, "realized", NULL
);
1718 /* decrement the number of CPUs */
1720 /* Update the number of CPUs in CMOS */
1721 rtc_set_cpus_count(x86ms
->rtc
, x86ms
->boot_cpus
);
1722 fw_cfg_modify_i16(x86ms
->fw_cfg
, FW_CFG_NB_CPUS
, x86ms
->boot_cpus
);
1724 error_propagate(errp
, local_err
);
1727 static void pc_cpu_pre_plug(HotplugHandler
*hotplug_dev
,
1728 DeviceState
*dev
, Error
**errp
)
1732 CPUArchId
*cpu_slot
;
1733 X86CPUTopoInfo topo
;
1734 X86CPU
*cpu
= X86_CPU(dev
);
1735 CPUX86State
*env
= &cpu
->env
;
1736 MachineState
*ms
= MACHINE(hotplug_dev
);
1737 PCMachineState
*pcms
= PC_MACHINE(hotplug_dev
);
1738 X86MachineState
*x86ms
= X86_MACHINE(pcms
);
1739 unsigned int smp_cores
= ms
->smp
.cores
;
1740 unsigned int smp_threads
= ms
->smp
.threads
;
1742 if(!object_dynamic_cast(OBJECT(cpu
), ms
->cpu_type
)) {
1743 error_setg(errp
, "Invalid CPU type, expected cpu type: '%s'",
1748 env
->nr_dies
= x86ms
->smp_dies
;
1751 * If APIC ID is not set,
1752 * set it based on socket/die/core/thread properties.
1754 if (cpu
->apic_id
== UNASSIGNED_APIC_ID
) {
1755 int max_socket
= (ms
->smp
.max_cpus
- 1) /
1756 smp_threads
/ smp_cores
/ x86ms
->smp_dies
;
1759 * die-id was optional in QEMU 4.0 and older, so keep it optional
1760 * if there's only one die per socket.
1762 if (cpu
->die_id
< 0 && x86ms
->smp_dies
== 1) {
1766 if (cpu
->socket_id
< 0) {
1767 error_setg(errp
, "CPU socket-id is not set");
1769 } else if (cpu
->socket_id
> max_socket
) {
1770 error_setg(errp
, "Invalid CPU socket-id: %u must be in range 0:%u",
1771 cpu
->socket_id
, max_socket
);
1774 if (cpu
->die_id
< 0) {
1775 error_setg(errp
, "CPU die-id is not set");
1777 } else if (cpu
->die_id
> x86ms
->smp_dies
- 1) {
1778 error_setg(errp
, "Invalid CPU die-id: %u must be in range 0:%u",
1779 cpu
->die_id
, x86ms
->smp_dies
- 1);
1782 if (cpu
->core_id
< 0) {
1783 error_setg(errp
, "CPU core-id is not set");
1785 } else if (cpu
->core_id
> (smp_cores
- 1)) {
1786 error_setg(errp
, "Invalid CPU core-id: %u must be in range 0:%u",
1787 cpu
->core_id
, smp_cores
- 1);
1790 if (cpu
->thread_id
< 0) {
1791 error_setg(errp
, "CPU thread-id is not set");
1793 } else if (cpu
->thread_id
> (smp_threads
- 1)) {
1794 error_setg(errp
, "Invalid CPU thread-id: %u must be in range 0:%u",
1795 cpu
->thread_id
, smp_threads
- 1);
1799 topo
.pkg_id
= cpu
->socket_id
;
1800 topo
.die_id
= cpu
->die_id
;
1801 topo
.core_id
= cpu
->core_id
;
1802 topo
.smt_id
= cpu
->thread_id
;
1803 cpu
->apic_id
= apicid_from_topo_ids(x86ms
->smp_dies
, smp_cores
,
1804 smp_threads
, &topo
);
1807 cpu_slot
= pc_find_cpu_slot(MACHINE(pcms
), cpu
->apic_id
, &idx
);
1809 MachineState
*ms
= MACHINE(pcms
);
1811 x86_topo_ids_from_apicid(cpu
->apic_id
, x86ms
->smp_dies
,
1812 smp_cores
, smp_threads
, &topo
);
1814 "Invalid CPU [socket: %u, die: %u, core: %u, thread: %u] with"
1815 " APIC ID %" PRIu32
", valid index range 0:%d",
1816 topo
.pkg_id
, topo
.die_id
, topo
.core_id
, topo
.smt_id
,
1817 cpu
->apic_id
, ms
->possible_cpus
->len
- 1);
1821 if (cpu_slot
->cpu
) {
1822 error_setg(errp
, "CPU[%d] with APIC ID %" PRIu32
" exists",
1827 /* if 'address' properties socket-id/core-id/thread-id are not set, set them
1828 * so that machine_query_hotpluggable_cpus would show correct values
1830 /* TODO: move socket_id/core_id/thread_id checks into x86_cpu_realizefn()
1831 * once -smp refactoring is complete and there will be CPU private
1832 * CPUState::nr_cores and CPUState::nr_threads fields instead of globals */
1833 x86_topo_ids_from_apicid(cpu
->apic_id
, x86ms
->smp_dies
,
1834 smp_cores
, smp_threads
, &topo
);
1835 if (cpu
->socket_id
!= -1 && cpu
->socket_id
!= topo
.pkg_id
) {
1836 error_setg(errp
, "property socket-id: %u doesn't match set apic-id:"
1837 " 0x%x (socket-id: %u)", cpu
->socket_id
, cpu
->apic_id
, topo
.pkg_id
);
1840 cpu
->socket_id
= topo
.pkg_id
;
1842 if (cpu
->die_id
!= -1 && cpu
->die_id
!= topo
.die_id
) {
1843 error_setg(errp
, "property die-id: %u doesn't match set apic-id:"
1844 " 0x%x (die-id: %u)", cpu
->die_id
, cpu
->apic_id
, topo
.die_id
);
1847 cpu
->die_id
= topo
.die_id
;
1849 if (cpu
->core_id
!= -1 && cpu
->core_id
!= topo
.core_id
) {
1850 error_setg(errp
, "property core-id: %u doesn't match set apic-id:"
1851 " 0x%x (core-id: %u)", cpu
->core_id
, cpu
->apic_id
, topo
.core_id
);
1854 cpu
->core_id
= topo
.core_id
;
1856 if (cpu
->thread_id
!= -1 && cpu
->thread_id
!= topo
.smt_id
) {
1857 error_setg(errp
, "property thread-id: %u doesn't match set apic-id:"
1858 " 0x%x (thread-id: %u)", cpu
->thread_id
, cpu
->apic_id
, topo
.smt_id
);
1861 cpu
->thread_id
= topo
.smt_id
;
1863 if (hyperv_feat_enabled(cpu
, HYPERV_FEAT_VPINDEX
) &&
1864 !kvm_hv_vpindex_settable()) {
1865 error_setg(errp
, "kernel doesn't allow setting HyperV VP_INDEX");
1870 cs
->cpu_index
= idx
;
1872 numa_cpu_pre_plug(cpu_slot
, dev
, errp
);
1875 static void pc_virtio_pmem_pci_pre_plug(HotplugHandler
*hotplug_dev
,
1876 DeviceState
*dev
, Error
**errp
)
1878 HotplugHandler
*hotplug_dev2
= qdev_get_bus_hotplug_handler(dev
);
1879 Error
*local_err
= NULL
;
1881 if (!hotplug_dev2
) {
1883 * Without a bus hotplug handler, we cannot control the plug/unplug
1884 * order. This should never be the case on x86, however better add
1887 error_setg(errp
, "virtio-pmem-pci not supported on this bus.");
1891 * First, see if we can plug this memory device at all. If that
1892 * succeeds, branch of to the actual hotplug handler.
1894 memory_device_pre_plug(MEMORY_DEVICE(dev
), MACHINE(hotplug_dev
), NULL
,
1897 hotplug_handler_pre_plug(hotplug_dev2
, dev
, &local_err
);
1899 error_propagate(errp
, local_err
);
1902 static void pc_virtio_pmem_pci_plug(HotplugHandler
*hotplug_dev
,
1903 DeviceState
*dev
, Error
**errp
)
1905 HotplugHandler
*hotplug_dev2
= qdev_get_bus_hotplug_handler(dev
);
1906 Error
*local_err
= NULL
;
1909 * Plug the memory device first and then branch off to the actual
1910 * hotplug handler. If that one fails, we can easily undo the memory
1913 memory_device_plug(MEMORY_DEVICE(dev
), MACHINE(hotplug_dev
));
1914 hotplug_handler_plug(hotplug_dev2
, dev
, &local_err
);
1916 memory_device_unplug(MEMORY_DEVICE(dev
), MACHINE(hotplug_dev
));
1918 error_propagate(errp
, local_err
);
1921 static void pc_virtio_pmem_pci_unplug_request(HotplugHandler
*hotplug_dev
,
1922 DeviceState
*dev
, Error
**errp
)
1924 /* We don't support virtio pmem hot unplug */
1925 error_setg(errp
, "virtio pmem device unplug not supported.");
1928 static void pc_virtio_pmem_pci_unplug(HotplugHandler
*hotplug_dev
,
1929 DeviceState
*dev
, Error
**errp
)
1931 /* We don't support virtio pmem hot unplug */
1934 static void pc_machine_device_pre_plug_cb(HotplugHandler
*hotplug_dev
,
1935 DeviceState
*dev
, Error
**errp
)
1937 if (object_dynamic_cast(OBJECT(dev
), TYPE_PC_DIMM
)) {
1938 pc_memory_pre_plug(hotplug_dev
, dev
, errp
);
1939 } else if (object_dynamic_cast(OBJECT(dev
), TYPE_CPU
)) {
1940 pc_cpu_pre_plug(hotplug_dev
, dev
, errp
);
1941 } else if (object_dynamic_cast(OBJECT(dev
), TYPE_VIRTIO_PMEM_PCI
)) {
1942 pc_virtio_pmem_pci_pre_plug(hotplug_dev
, dev
, errp
);
1946 static void pc_machine_device_plug_cb(HotplugHandler
*hotplug_dev
,
1947 DeviceState
*dev
, Error
**errp
)
1949 if (object_dynamic_cast(OBJECT(dev
), TYPE_PC_DIMM
)) {
1950 pc_memory_plug(hotplug_dev
, dev
, errp
);
1951 } else if (object_dynamic_cast(OBJECT(dev
), TYPE_CPU
)) {
1952 pc_cpu_plug(hotplug_dev
, dev
, errp
);
1953 } else if (object_dynamic_cast(OBJECT(dev
), TYPE_VIRTIO_PMEM_PCI
)) {
1954 pc_virtio_pmem_pci_plug(hotplug_dev
, dev
, errp
);
1958 static void pc_machine_device_unplug_request_cb(HotplugHandler
*hotplug_dev
,
1959 DeviceState
*dev
, Error
**errp
)
1961 if (object_dynamic_cast(OBJECT(dev
), TYPE_PC_DIMM
)) {
1962 pc_memory_unplug_request(hotplug_dev
, dev
, errp
);
1963 } else if (object_dynamic_cast(OBJECT(dev
), TYPE_CPU
)) {
1964 pc_cpu_unplug_request_cb(hotplug_dev
, dev
, errp
);
1965 } else if (object_dynamic_cast(OBJECT(dev
), TYPE_VIRTIO_PMEM_PCI
)) {
1966 pc_virtio_pmem_pci_unplug_request(hotplug_dev
, dev
, errp
);
1968 error_setg(errp
, "acpi: device unplug request for not supported device"
1969 " type: %s", object_get_typename(OBJECT(dev
)));
1973 static void pc_machine_device_unplug_cb(HotplugHandler
*hotplug_dev
,
1974 DeviceState
*dev
, Error
**errp
)
1976 if (object_dynamic_cast(OBJECT(dev
), TYPE_PC_DIMM
)) {
1977 pc_memory_unplug(hotplug_dev
, dev
, errp
);
1978 } else if (object_dynamic_cast(OBJECT(dev
), TYPE_CPU
)) {
1979 pc_cpu_unplug_cb(hotplug_dev
, dev
, errp
);
1980 } else if (object_dynamic_cast(OBJECT(dev
), TYPE_VIRTIO_PMEM_PCI
)) {
1981 pc_virtio_pmem_pci_unplug(hotplug_dev
, dev
, errp
);
1983 error_setg(errp
, "acpi: device unplug for not supported device"
1984 " type: %s", object_get_typename(OBJECT(dev
)));
1988 static HotplugHandler
*pc_get_hotplug_handler(MachineState
*machine
,
1991 if (object_dynamic_cast(OBJECT(dev
), TYPE_PC_DIMM
) ||
1992 object_dynamic_cast(OBJECT(dev
), TYPE_CPU
) ||
1993 object_dynamic_cast(OBJECT(dev
), TYPE_VIRTIO_PMEM_PCI
)) {
1994 return HOTPLUG_HANDLER(machine
);
2001 pc_machine_get_device_memory_region_size(Object
*obj
, Visitor
*v
,
2002 const char *name
, void *opaque
,
2005 MachineState
*ms
= MACHINE(obj
);
2008 if (ms
->device_memory
) {
2009 value
= memory_region_size(&ms
->device_memory
->mr
);
2012 visit_type_int(v
, name
, &value
, errp
);
2015 static void pc_machine_get_vmport(Object
*obj
, Visitor
*v
, const char *name
,
2016 void *opaque
, Error
**errp
)
2018 PCMachineState
*pcms
= PC_MACHINE(obj
);
2019 OnOffAuto vmport
= pcms
->vmport
;
2021 visit_type_OnOffAuto(v
, name
, &vmport
, errp
);
2024 static void pc_machine_set_vmport(Object
*obj
, Visitor
*v
, const char *name
,
2025 void *opaque
, Error
**errp
)
2027 PCMachineState
*pcms
= PC_MACHINE(obj
);
2029 visit_type_OnOffAuto(v
, name
, &pcms
->vmport
, errp
);
2032 bool pc_machine_is_smm_enabled(PCMachineState
*pcms
)
2034 bool smm_available
= false;
2036 if (pcms
->smm
== ON_OFF_AUTO_OFF
) {
2040 if (tcg_enabled() || qtest_enabled()) {
2041 smm_available
= true;
2042 } else if (kvm_enabled()) {
2043 smm_available
= kvm_has_smm();
2046 if (smm_available
) {
2050 if (pcms
->smm
== ON_OFF_AUTO_ON
) {
2051 error_report("System Management Mode not supported by this hypervisor.");
2057 static void pc_machine_get_smm(Object
*obj
, Visitor
*v
, const char *name
,
2058 void *opaque
, Error
**errp
)
2060 PCMachineState
*pcms
= PC_MACHINE(obj
);
2061 OnOffAuto smm
= pcms
->smm
;
2063 visit_type_OnOffAuto(v
, name
, &smm
, errp
);
2066 static void pc_machine_set_smm(Object
*obj
, Visitor
*v
, const char *name
,
2067 void *opaque
, Error
**errp
)
2069 PCMachineState
*pcms
= PC_MACHINE(obj
);
2071 visit_type_OnOffAuto(v
, name
, &pcms
->smm
, errp
);
2074 static bool pc_machine_get_smbus(Object
*obj
, Error
**errp
)
2076 PCMachineState
*pcms
= PC_MACHINE(obj
);
2078 return pcms
->smbus_enabled
;
2081 static void pc_machine_set_smbus(Object
*obj
, bool value
, Error
**errp
)
2083 PCMachineState
*pcms
= PC_MACHINE(obj
);
2085 pcms
->smbus_enabled
= value
;
2088 static bool pc_machine_get_sata(Object
*obj
, Error
**errp
)
2090 PCMachineState
*pcms
= PC_MACHINE(obj
);
2092 return pcms
->sata_enabled
;
2095 static void pc_machine_set_sata(Object
*obj
, bool value
, Error
**errp
)
2097 PCMachineState
*pcms
= PC_MACHINE(obj
);
2099 pcms
->sata_enabled
= value
;
2102 static bool pc_machine_get_pit(Object
*obj
, Error
**errp
)
2104 PCMachineState
*pcms
= PC_MACHINE(obj
);
2106 return pcms
->pit_enabled
;
2109 static void pc_machine_set_pit(Object
*obj
, bool value
, Error
**errp
)
2111 PCMachineState
*pcms
= PC_MACHINE(obj
);
2113 pcms
->pit_enabled
= value
;
2116 static void pc_machine_initfn(Object
*obj
)
2118 PCMachineState
*pcms
= PC_MACHINE(obj
);
2120 pcms
->smm
= ON_OFF_AUTO_AUTO
;
2121 #ifdef CONFIG_VMPORT
2122 pcms
->vmport
= ON_OFF_AUTO_AUTO
;
2124 pcms
->vmport
= ON_OFF_AUTO_OFF
;
2125 #endif /* CONFIG_VMPORT */
2126 /* acpi build is enabled by default if machine supports it */
2127 pcms
->acpi_build_enabled
= PC_MACHINE_GET_CLASS(pcms
)->has_acpi_build
;
2128 pcms
->smbus_enabled
= true;
2129 pcms
->sata_enabled
= true;
2130 pcms
->pit_enabled
= true;
2132 pc_system_flash_create(pcms
);
2135 static void pc_machine_reset(MachineState
*machine
)
2140 qemu_devices_reset();
2142 /* Reset APIC after devices have been reset to cancel
2143 * any changes that qemu_devices_reset() might have done.
2148 if (cpu
->apic_state
) {
2149 device_reset(cpu
->apic_state
);
2154 static void pc_machine_wakeup(MachineState
*machine
)
2156 cpu_synchronize_all_states();
2157 pc_machine_reset(machine
);
2158 cpu_synchronize_all_post_reset();
2161 static bool pc_hotplug_allowed(MachineState
*ms
, DeviceState
*dev
, Error
**errp
)
2163 X86IOMMUState
*iommu
= x86_iommu_get_default();
2164 IntelIOMMUState
*intel_iommu
;
2167 object_dynamic_cast((Object
*)iommu
, TYPE_INTEL_IOMMU_DEVICE
) &&
2168 object_dynamic_cast((Object
*)dev
, "vfio-pci")) {
2169 intel_iommu
= INTEL_IOMMU_DEVICE(iommu
);
2170 if (!intel_iommu
->caching_mode
) {
2171 error_setg(errp
, "Device assignment is not allowed without "
2172 "enabling caching-mode=on for Intel IOMMU.");
2180 static void pc_machine_class_init(ObjectClass
*oc
, void *data
)
2182 MachineClass
*mc
= MACHINE_CLASS(oc
);
2183 PCMachineClass
*pcmc
= PC_MACHINE_CLASS(oc
);
2184 HotplugHandlerClass
*hc
= HOTPLUG_HANDLER_CLASS(oc
);
2186 pcmc
->pci_enabled
= true;
2187 pcmc
->has_acpi_build
= true;
2188 pcmc
->rsdp_in_ram
= true;
2189 pcmc
->smbios_defaults
= true;
2190 pcmc
->smbios_uuid_encoded
= true;
2191 pcmc
->gigabyte_align
= true;
2192 pcmc
->has_reserved_memory
= true;
2193 pcmc
->kvmclock_enabled
= true;
2194 pcmc
->enforce_aligned_dimm
= true;
2195 /* BIOS ACPI tables: 128K. Other BIOS datastructures: less than 4K reported
2196 * to be used at the moment, 32K should be enough for a while. */
2197 pcmc
->acpi_data_size
= 0x20000 + 0x8000;
2198 pcmc
->save_tsc_khz
= true;
2199 pcmc
->linuxboot_dma_enabled
= true;
2200 pcmc
->pvh_enabled
= true;
2201 assert(!mc
->get_hotplug_handler
);
2202 mc
->get_hotplug_handler
= pc_get_hotplug_handler
;
2203 mc
->hotplug_allowed
= pc_hotplug_allowed
;
2204 mc
->cpu_index_to_instance_props
= x86_cpu_index_to_props
;
2205 mc
->get_default_cpu_node_id
= x86_get_default_cpu_node_id
;
2206 mc
->possible_cpu_arch_ids
= x86_possible_cpu_arch_ids
;
2207 mc
->auto_enable_numa_with_memhp
= true;
2208 mc
->has_hotpluggable_cpus
= true;
2209 mc
->default_boot_order
= "cad";
2210 mc
->hot_add_cpu
= pc_hot_add_cpu
;
2211 mc
->smp_parse
= pc_smp_parse
;
2212 mc
->block_default_type
= IF_IDE
;
2214 mc
->reset
= pc_machine_reset
;
2215 mc
->wakeup
= pc_machine_wakeup
;
2216 hc
->pre_plug
= pc_machine_device_pre_plug_cb
;
2217 hc
->plug
= pc_machine_device_plug_cb
;
2218 hc
->unplug_request
= pc_machine_device_unplug_request_cb
;
2219 hc
->unplug
= pc_machine_device_unplug_cb
;
2220 mc
->default_cpu_type
= TARGET_DEFAULT_CPU_TYPE
;
2221 mc
->nvdimm_supported
= true;
2222 mc
->numa_mem_supported
= true;
2224 object_class_property_add(oc
, PC_MACHINE_DEVMEM_REGION_SIZE
, "int",
2225 pc_machine_get_device_memory_region_size
, NULL
,
2226 NULL
, NULL
, &error_abort
);
2228 object_class_property_add(oc
, PC_MACHINE_SMM
, "OnOffAuto",
2229 pc_machine_get_smm
, pc_machine_set_smm
,
2230 NULL
, NULL
, &error_abort
);
2231 object_class_property_set_description(oc
, PC_MACHINE_SMM
,
2232 "Enable SMM (pc & q35)", &error_abort
);
2234 object_class_property_add(oc
, PC_MACHINE_VMPORT
, "OnOffAuto",
2235 pc_machine_get_vmport
, pc_machine_set_vmport
,
2236 NULL
, NULL
, &error_abort
);
2237 object_class_property_set_description(oc
, PC_MACHINE_VMPORT
,
2238 "Enable vmport (pc & q35)", &error_abort
);
2240 object_class_property_add_bool(oc
, PC_MACHINE_SMBUS
,
2241 pc_machine_get_smbus
, pc_machine_set_smbus
, &error_abort
);
2243 object_class_property_add_bool(oc
, PC_MACHINE_SATA
,
2244 pc_machine_get_sata
, pc_machine_set_sata
, &error_abort
);
2246 object_class_property_add_bool(oc
, PC_MACHINE_PIT
,
2247 pc_machine_get_pit
, pc_machine_set_pit
, &error_abort
);
2250 static const TypeInfo pc_machine_info
= {
2251 .name
= TYPE_PC_MACHINE
,
2252 .parent
= TYPE_X86_MACHINE
,
2254 .instance_size
= sizeof(PCMachineState
),
2255 .instance_init
= pc_machine_initfn
,
2256 .class_size
= sizeof(PCMachineClass
),
2257 .class_init
= pc_machine_class_init
,
2258 .interfaces
= (InterfaceInfo
[]) {
2259 { TYPE_HOTPLUG_HANDLER
},
2264 static void pc_machine_register_types(void)
2266 type_register_static(&pc_machine_info
);
2269 type_init(pc_machine_register_types
)