i386/pc: bounds check phys-bits against max used GPA
[qemu.git] / hw / i386 / pc.c
blob56d8c179ea1b90d6927d5d549a48842ea824e7c5
1 /*
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
22 * THE SOFTWARE.
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 "hw/i386/vmport.h"
35 #include "sysemu/cpus.h"
36 #include "hw/block/fdc.h"
37 #include "hw/ide.h"
38 #include "hw/pci/pci.h"
39 #include "hw/pci/pci_bus.h"
40 #include "hw/pci-bridge/pci_expander_bridge.h"
41 #include "hw/nvram/fw_cfg.h"
42 #include "hw/timer/hpet.h"
43 #include "hw/firmware/smbios.h"
44 #include "hw/loader.h"
45 #include "elf.h"
46 #include "migration/vmstate.h"
47 #include "multiboot.h"
48 #include "hw/rtc/mc146818rtc.h"
49 #include "hw/intc/i8259.h"
50 #include "hw/dma/i8257.h"
51 #include "hw/timer/i8254.h"
52 #include "hw/input/i8042.h"
53 #include "hw/irq.h"
54 #include "hw/audio/pcspk.h"
55 #include "hw/pci/msi.h"
56 #include "hw/sysbus.h"
57 #include "sysemu/sysemu.h"
58 #include "sysemu/tcg.h"
59 #include "sysemu/numa.h"
60 #include "sysemu/kvm.h"
61 #include "sysemu/xen.h"
62 #include "sysemu/reset.h"
63 #include "sysemu/runstate.h"
64 #include "kvm/kvm_i386.h"
65 #include "hw/xen/xen.h"
66 #include "hw/xen/start_info.h"
67 #include "ui/qemu-spice.h"
68 #include "exec/memory.h"
69 #include "qemu/bitmap.h"
70 #include "qemu/config-file.h"
71 #include "qemu/error-report.h"
72 #include "qemu/option.h"
73 #include "qemu/cutils.h"
74 #include "hw/acpi/acpi.h"
75 #include "hw/acpi/cpu_hotplug.h"
76 #include "acpi-build.h"
77 #include "hw/mem/pc-dimm.h"
78 #include "hw/mem/nvdimm.h"
79 #include "hw/cxl/cxl.h"
80 #include "hw/cxl/cxl_host.h"
81 #include "qapi/error.h"
82 #include "qapi/qapi-visit-common.h"
83 #include "qapi/qapi-visit-machine.h"
84 #include "qapi/visitor.h"
85 #include "hw/core/cpu.h"
86 #include "hw/usb.h"
87 #include "hw/i386/intel_iommu.h"
88 #include "hw/net/ne2000-isa.h"
89 #include "standard-headers/asm-x86/bootparam.h"
90 #include "hw/virtio/virtio-iommu.h"
91 #include "hw/virtio/virtio-pmem-pci.h"
92 #include "hw/virtio/virtio-mem-pci.h"
93 #include "hw/mem/memory-device.h"
94 #include "sysemu/replay.h"
95 #include "qapi/qmp/qerror.h"
96 #include "e820_memory_layout.h"
97 #include "fw_cfg.h"
98 #include "trace.h"
99 #include CONFIG_DEVICES
102 * Helper for setting model-id for CPU models that changed model-id
103 * depending on QEMU versions up to QEMU 2.4.
105 #define PC_CPU_MODEL_IDS(v) \
106 { "qemu32-" TYPE_X86_CPU, "model-id", "QEMU Virtual CPU version " v, },\
107 { "qemu64-" TYPE_X86_CPU, "model-id", "QEMU Virtual CPU version " v, },\
108 { "athlon-" TYPE_X86_CPU, "model-id", "QEMU Virtual CPU version " v, },
110 GlobalProperty pc_compat_7_0[] = {};
111 const size_t pc_compat_7_0_len = G_N_ELEMENTS(pc_compat_7_0);
113 GlobalProperty pc_compat_6_2[] = {
114 { "virtio-mem", "unplugged-inaccessible", "off" },
116 const size_t pc_compat_6_2_len = G_N_ELEMENTS(pc_compat_6_2);
118 GlobalProperty pc_compat_6_1[] = {
119 { TYPE_X86_CPU, "hv-version-id-build", "0x1bbc" },
120 { TYPE_X86_CPU, "hv-version-id-major", "0x0006" },
121 { TYPE_X86_CPU, "hv-version-id-minor", "0x0001" },
122 { "ICH9-LPC", "x-keep-pci-slot-hpc", "false" },
124 const size_t pc_compat_6_1_len = G_N_ELEMENTS(pc_compat_6_1);
126 GlobalProperty pc_compat_6_0[] = {
127 { "qemu64" "-" TYPE_X86_CPU, "family", "6" },
128 { "qemu64" "-" TYPE_X86_CPU, "model", "6" },
129 { "qemu64" "-" TYPE_X86_CPU, "stepping", "3" },
130 { TYPE_X86_CPU, "x-vendor-cpuid-only", "off" },
131 { "ICH9-LPC", ACPI_PM_PROP_ACPI_PCIHP_BRIDGE, "off" },
132 { "ICH9-LPC", "x-keep-pci-slot-hpc", "true" },
134 const size_t pc_compat_6_0_len = G_N_ELEMENTS(pc_compat_6_0);
136 GlobalProperty pc_compat_5_2[] = {
137 { "ICH9-LPC", "x-smi-cpu-hotunplug", "off" },
139 const size_t pc_compat_5_2_len = G_N_ELEMENTS(pc_compat_5_2);
141 GlobalProperty pc_compat_5_1[] = {
142 { "ICH9-LPC", "x-smi-cpu-hotplug", "off" },
143 { TYPE_X86_CPU, "kvm-msi-ext-dest-id", "off" },
145 const size_t pc_compat_5_1_len = G_N_ELEMENTS(pc_compat_5_1);
147 GlobalProperty pc_compat_5_0[] = {
149 const size_t pc_compat_5_0_len = G_N_ELEMENTS(pc_compat_5_0);
151 GlobalProperty pc_compat_4_2[] = {
152 { "mch", "smbase-smram", "off" },
154 const size_t pc_compat_4_2_len = G_N_ELEMENTS(pc_compat_4_2);
156 GlobalProperty pc_compat_4_1[] = {};
157 const size_t pc_compat_4_1_len = G_N_ELEMENTS(pc_compat_4_1);
159 GlobalProperty pc_compat_4_0[] = {};
160 const size_t pc_compat_4_0_len = G_N_ELEMENTS(pc_compat_4_0);
162 GlobalProperty pc_compat_3_1[] = {
163 { "intel-iommu", "dma-drain", "off" },
164 { "Opteron_G3" "-" TYPE_X86_CPU, "rdtscp", "off" },
165 { "Opteron_G4" "-" TYPE_X86_CPU, "rdtscp", "off" },
166 { "Opteron_G4" "-" TYPE_X86_CPU, "npt", "off" },
167 { "Opteron_G4" "-" TYPE_X86_CPU, "nrip-save", "off" },
168 { "Opteron_G5" "-" TYPE_X86_CPU, "rdtscp", "off" },
169 { "Opteron_G5" "-" TYPE_X86_CPU, "npt", "off" },
170 { "Opteron_G5" "-" TYPE_X86_CPU, "nrip-save", "off" },
171 { "EPYC" "-" TYPE_X86_CPU, "npt", "off" },
172 { "EPYC" "-" TYPE_X86_CPU, "nrip-save", "off" },
173 { "EPYC-IBPB" "-" TYPE_X86_CPU, "npt", "off" },
174 { "EPYC-IBPB" "-" TYPE_X86_CPU, "nrip-save", "off" },
175 { "Skylake-Client" "-" TYPE_X86_CPU, "mpx", "on" },
176 { "Skylake-Client-IBRS" "-" TYPE_X86_CPU, "mpx", "on" },
177 { "Skylake-Server" "-" TYPE_X86_CPU, "mpx", "on" },
178 { "Skylake-Server-IBRS" "-" TYPE_X86_CPU, "mpx", "on" },
179 { "Cascadelake-Server" "-" TYPE_X86_CPU, "mpx", "on" },
180 { "Icelake-Client" "-" TYPE_X86_CPU, "mpx", "on" },
181 { "Icelake-Server" "-" TYPE_X86_CPU, "mpx", "on" },
182 { "Cascadelake-Server" "-" TYPE_X86_CPU, "stepping", "5" },
183 { TYPE_X86_CPU, "x-intel-pt-auto-level", "off" },
185 const size_t pc_compat_3_1_len = G_N_ELEMENTS(pc_compat_3_1);
187 GlobalProperty pc_compat_3_0[] = {
188 { TYPE_X86_CPU, "x-hv-synic-kvm-only", "on" },
189 { "Skylake-Server" "-" TYPE_X86_CPU, "pku", "off" },
190 { "Skylake-Server-IBRS" "-" TYPE_X86_CPU, "pku", "off" },
192 const size_t pc_compat_3_0_len = G_N_ELEMENTS(pc_compat_3_0);
194 GlobalProperty pc_compat_2_12[] = {
195 { TYPE_X86_CPU, "legacy-cache", "on" },
196 { TYPE_X86_CPU, "topoext", "off" },
197 { "EPYC-" TYPE_X86_CPU, "xlevel", "0x8000000a" },
198 { "EPYC-IBPB-" TYPE_X86_CPU, "xlevel", "0x8000000a" },
200 const size_t pc_compat_2_12_len = G_N_ELEMENTS(pc_compat_2_12);
202 GlobalProperty pc_compat_2_11[] = {
203 { TYPE_X86_CPU, "x-migrate-smi-count", "off" },
204 { "Skylake-Server" "-" TYPE_X86_CPU, "clflushopt", "off" },
206 const size_t pc_compat_2_11_len = G_N_ELEMENTS(pc_compat_2_11);
208 GlobalProperty pc_compat_2_10[] = {
209 { TYPE_X86_CPU, "x-hv-max-vps", "0x40" },
210 { "i440FX-pcihost", "x-pci-hole64-fix", "off" },
211 { "q35-pcihost", "x-pci-hole64-fix", "off" },
213 const size_t pc_compat_2_10_len = G_N_ELEMENTS(pc_compat_2_10);
215 GlobalProperty pc_compat_2_9[] = {
216 { "mch", "extended-tseg-mbytes", "0" },
218 const size_t pc_compat_2_9_len = G_N_ELEMENTS(pc_compat_2_9);
220 GlobalProperty pc_compat_2_8[] = {
221 { TYPE_X86_CPU, "tcg-cpuid", "off" },
222 { "kvmclock", "x-mach-use-reliable-get-clock", "off" },
223 { "ICH9-LPC", "x-smi-broadcast", "off" },
224 { TYPE_X86_CPU, "vmware-cpuid-freq", "off" },
225 { "Haswell-" TYPE_X86_CPU, "stepping", "1" },
227 const size_t pc_compat_2_8_len = G_N_ELEMENTS(pc_compat_2_8);
229 GlobalProperty pc_compat_2_7[] = {
230 { TYPE_X86_CPU, "l3-cache", "off" },
231 { TYPE_X86_CPU, "full-cpuid-auto-level", "off" },
232 { "Opteron_G3" "-" TYPE_X86_CPU, "family", "15" },
233 { "Opteron_G3" "-" TYPE_X86_CPU, "model", "6" },
234 { "Opteron_G3" "-" TYPE_X86_CPU, "stepping", "1" },
235 { "isa-pcspk", "migrate", "off" },
237 const size_t pc_compat_2_7_len = G_N_ELEMENTS(pc_compat_2_7);
239 GlobalProperty pc_compat_2_6[] = {
240 { TYPE_X86_CPU, "cpuid-0xb", "off" },
241 { "vmxnet3", "romfile", "" },
242 { TYPE_X86_CPU, "fill-mtrr-mask", "off" },
243 { "apic-common", "legacy-instance-id", "on", }
245 const size_t pc_compat_2_6_len = G_N_ELEMENTS(pc_compat_2_6);
247 GlobalProperty pc_compat_2_5[] = {};
248 const size_t pc_compat_2_5_len = G_N_ELEMENTS(pc_compat_2_5);
250 GlobalProperty pc_compat_2_4[] = {
251 PC_CPU_MODEL_IDS("2.4.0")
252 { "Haswell-" TYPE_X86_CPU, "abm", "off" },
253 { "Haswell-noTSX-" TYPE_X86_CPU, "abm", "off" },
254 { "Broadwell-" TYPE_X86_CPU, "abm", "off" },
255 { "Broadwell-noTSX-" TYPE_X86_CPU, "abm", "off" },
256 { "host" "-" TYPE_X86_CPU, "host-cache-info", "on" },
257 { TYPE_X86_CPU, "check", "off" },
258 { "qemu64" "-" TYPE_X86_CPU, "sse4a", "on" },
259 { "qemu64" "-" TYPE_X86_CPU, "abm", "on" },
260 { "qemu64" "-" TYPE_X86_CPU, "popcnt", "on" },
261 { "qemu32" "-" TYPE_X86_CPU, "popcnt", "on" },
262 { "Opteron_G2" "-" TYPE_X86_CPU, "rdtscp", "on" },
263 { "Opteron_G3" "-" TYPE_X86_CPU, "rdtscp", "on" },
264 { "Opteron_G4" "-" TYPE_X86_CPU, "rdtscp", "on" },
265 { "Opteron_G5" "-" TYPE_X86_CPU, "rdtscp", "on", }
267 const size_t pc_compat_2_4_len = G_N_ELEMENTS(pc_compat_2_4);
269 GlobalProperty pc_compat_2_3[] = {
270 PC_CPU_MODEL_IDS("2.3.0")
271 { TYPE_X86_CPU, "arat", "off" },
272 { "qemu64" "-" TYPE_X86_CPU, "min-level", "4" },
273 { "kvm64" "-" TYPE_X86_CPU, "min-level", "5" },
274 { "pentium3" "-" TYPE_X86_CPU, "min-level", "2" },
275 { "n270" "-" TYPE_X86_CPU, "min-level", "5" },
276 { "Conroe" "-" TYPE_X86_CPU, "min-level", "4" },
277 { "Penryn" "-" TYPE_X86_CPU, "min-level", "4" },
278 { "Nehalem" "-" TYPE_X86_CPU, "min-level", "4" },
279 { "n270" "-" TYPE_X86_CPU, "min-xlevel", "0x8000000a" },
280 { "Penryn" "-" TYPE_X86_CPU, "min-xlevel", "0x8000000a" },
281 { "Conroe" "-" TYPE_X86_CPU, "min-xlevel", "0x8000000a" },
282 { "Nehalem" "-" TYPE_X86_CPU, "min-xlevel", "0x8000000a" },
283 { "Westmere" "-" TYPE_X86_CPU, "min-xlevel", "0x8000000a" },
284 { "SandyBridge" "-" TYPE_X86_CPU, "min-xlevel", "0x8000000a" },
285 { "IvyBridge" "-" TYPE_X86_CPU, "min-xlevel", "0x8000000a" },
286 { "Haswell" "-" TYPE_X86_CPU, "min-xlevel", "0x8000000a" },
287 { "Haswell-noTSX" "-" TYPE_X86_CPU, "min-xlevel", "0x8000000a" },
288 { "Broadwell" "-" TYPE_X86_CPU, "min-xlevel", "0x8000000a" },
289 { "Broadwell-noTSX" "-" TYPE_X86_CPU, "min-xlevel", "0x8000000a" },
290 { TYPE_X86_CPU, "kvm-no-smi-migration", "on" },
292 const size_t pc_compat_2_3_len = G_N_ELEMENTS(pc_compat_2_3);
294 GlobalProperty pc_compat_2_2[] = {
295 PC_CPU_MODEL_IDS("2.2.0")
296 { "kvm64" "-" TYPE_X86_CPU, "vme", "off" },
297 { "kvm32" "-" TYPE_X86_CPU, "vme", "off" },
298 { "Conroe" "-" TYPE_X86_CPU, "vme", "off" },
299 { "Penryn" "-" TYPE_X86_CPU, "vme", "off" },
300 { "Nehalem" "-" TYPE_X86_CPU, "vme", "off" },
301 { "Westmere" "-" TYPE_X86_CPU, "vme", "off" },
302 { "SandyBridge" "-" TYPE_X86_CPU, "vme", "off" },
303 { "Haswell" "-" TYPE_X86_CPU, "vme", "off" },
304 { "Broadwell" "-" TYPE_X86_CPU, "vme", "off" },
305 { "Opteron_G1" "-" TYPE_X86_CPU, "vme", "off" },
306 { "Opteron_G2" "-" TYPE_X86_CPU, "vme", "off" },
307 { "Opteron_G3" "-" TYPE_X86_CPU, "vme", "off" },
308 { "Opteron_G4" "-" TYPE_X86_CPU, "vme", "off" },
309 { "Opteron_G5" "-" TYPE_X86_CPU, "vme", "off" },
310 { "Haswell" "-" TYPE_X86_CPU, "f16c", "off" },
311 { "Haswell" "-" TYPE_X86_CPU, "rdrand", "off" },
312 { "Broadwell" "-" TYPE_X86_CPU, "f16c", "off" },
313 { "Broadwell" "-" TYPE_X86_CPU, "rdrand", "off" },
315 const size_t pc_compat_2_2_len = G_N_ELEMENTS(pc_compat_2_2);
317 GlobalProperty pc_compat_2_1[] = {
318 PC_CPU_MODEL_IDS("2.1.0")
319 { "coreduo" "-" TYPE_X86_CPU, "vmx", "on" },
320 { "core2duo" "-" TYPE_X86_CPU, "vmx", "on" },
322 const size_t pc_compat_2_1_len = G_N_ELEMENTS(pc_compat_2_1);
324 GlobalProperty pc_compat_2_0[] = {
325 PC_CPU_MODEL_IDS("2.0.0")
326 { "virtio-scsi-pci", "any_layout", "off" },
327 { "PIIX4_PM", "memory-hotplug-support", "off" },
328 { "apic", "version", "0x11" },
329 { "nec-usb-xhci", "superspeed-ports-first", "off" },
330 { "nec-usb-xhci", "force-pcie-endcap", "on" },
331 { "pci-serial", "prog_if", "0" },
332 { "pci-serial-2x", "prog_if", "0" },
333 { "pci-serial-4x", "prog_if", "0" },
334 { "virtio-net-pci", "guest_announce", "off" },
335 { "ICH9-LPC", "memory-hotplug-support", "off" },
337 const size_t pc_compat_2_0_len = G_N_ELEMENTS(pc_compat_2_0);
339 GlobalProperty pc_compat_1_7[] = {
340 PC_CPU_MODEL_IDS("1.7.0")
341 { TYPE_USB_DEVICE, "msos-desc", "no" },
342 { "PIIX4_PM", ACPI_PM_PROP_ACPI_PCIHP_BRIDGE, "off" },
343 { "hpet", HPET_INTCAP, "4" },
345 const size_t pc_compat_1_7_len = G_N_ELEMENTS(pc_compat_1_7);
347 GlobalProperty pc_compat_1_6[] = {
348 PC_CPU_MODEL_IDS("1.6.0")
349 { "e1000", "mitigation", "off" },
350 { "qemu64-" TYPE_X86_CPU, "model", "2" },
351 { "qemu32-" TYPE_X86_CPU, "model", "3" },
352 { "i440FX-pcihost", "short_root_bus", "1" },
353 { "q35-pcihost", "short_root_bus", "1" },
355 const size_t pc_compat_1_6_len = G_N_ELEMENTS(pc_compat_1_6);
357 GlobalProperty pc_compat_1_5[] = {
358 PC_CPU_MODEL_IDS("1.5.0")
359 { "Conroe-" TYPE_X86_CPU, "model", "2" },
360 { "Conroe-" TYPE_X86_CPU, "min-level", "2" },
361 { "Penryn-" TYPE_X86_CPU, "model", "2" },
362 { "Penryn-" TYPE_X86_CPU, "min-level", "2" },
363 { "Nehalem-" TYPE_X86_CPU, "model", "2" },
364 { "Nehalem-" TYPE_X86_CPU, "min-level", "2" },
365 { "virtio-net-pci", "any_layout", "off" },
366 { TYPE_X86_CPU, "pmu", "on" },
367 { "i440FX-pcihost", "short_root_bus", "0" },
368 { "q35-pcihost", "short_root_bus", "0" },
370 const size_t pc_compat_1_5_len = G_N_ELEMENTS(pc_compat_1_5);
372 GlobalProperty pc_compat_1_4[] = {
373 PC_CPU_MODEL_IDS("1.4.0")
374 { "scsi-hd", "discard_granularity", "0" },
375 { "scsi-cd", "discard_granularity", "0" },
376 { "ide-hd", "discard_granularity", "0" },
377 { "ide-cd", "discard_granularity", "0" },
378 { "virtio-blk-pci", "discard_granularity", "0" },
379 /* DEV_NVECTORS_UNSPECIFIED as a uint32_t string: */
380 { "virtio-serial-pci", "vectors", "0xFFFFFFFF" },
381 { "virtio-net-pci", "ctrl_guest_offloads", "off" },
382 { "e1000", "romfile", "pxe-e1000.rom" },
383 { "ne2k_pci", "romfile", "pxe-ne2k_pci.rom" },
384 { "pcnet", "romfile", "pxe-pcnet.rom" },
385 { "rtl8139", "romfile", "pxe-rtl8139.rom" },
386 { "virtio-net-pci", "romfile", "pxe-virtio.rom" },
387 { "486-" TYPE_X86_CPU, "model", "0" },
388 { "n270" "-" TYPE_X86_CPU, "movbe", "off" },
389 { "Westmere" "-" TYPE_X86_CPU, "pclmulqdq", "off" },
391 const size_t pc_compat_1_4_len = G_N_ELEMENTS(pc_compat_1_4);
393 GSIState *pc_gsi_create(qemu_irq **irqs, bool pci_enabled)
395 GSIState *s;
397 s = g_new0(GSIState, 1);
398 if (kvm_ioapic_in_kernel()) {
399 kvm_pc_setup_irq_routing(pci_enabled);
401 *irqs = qemu_allocate_irqs(gsi_handler, s, GSI_NUM_PINS);
403 return s;
406 static void ioport80_write(void *opaque, hwaddr addr, uint64_t data,
407 unsigned size)
411 static uint64_t ioport80_read(void *opaque, hwaddr addr, unsigned size)
413 return 0xffffffffffffffffULL;
416 /* MSDOS compatibility mode FPU exception support */
417 static void ioportF0_write(void *opaque, hwaddr addr, uint64_t data,
418 unsigned size)
420 if (tcg_enabled()) {
421 cpu_set_ignne();
425 static uint64_t ioportF0_read(void *opaque, hwaddr addr, unsigned size)
427 return 0xffffffffffffffffULL;
430 /* PC cmos mappings */
432 #define REG_EQUIPMENT_BYTE 0x14
434 static void cmos_init_hd(ISADevice *s, int type_ofs, int info_ofs,
435 int16_t cylinders, int8_t heads, int8_t sectors)
437 rtc_set_memory(s, type_ofs, 47);
438 rtc_set_memory(s, info_ofs, cylinders);
439 rtc_set_memory(s, info_ofs + 1, cylinders >> 8);
440 rtc_set_memory(s, info_ofs + 2, heads);
441 rtc_set_memory(s, info_ofs + 3, 0xff);
442 rtc_set_memory(s, info_ofs + 4, 0xff);
443 rtc_set_memory(s, info_ofs + 5, 0xc0 | ((heads > 8) << 3));
444 rtc_set_memory(s, info_ofs + 6, cylinders);
445 rtc_set_memory(s, info_ofs + 7, cylinders >> 8);
446 rtc_set_memory(s, info_ofs + 8, sectors);
449 /* convert boot_device letter to something recognizable by the bios */
450 static int boot_device2nibble(char boot_device)
452 switch(boot_device) {
453 case 'a':
454 case 'b':
455 return 0x01; /* floppy boot */
456 case 'c':
457 return 0x02; /* hard drive boot */
458 case 'd':
459 return 0x03; /* CD-ROM boot */
460 case 'n':
461 return 0x04; /* Network boot */
463 return 0;
466 static void set_boot_dev(ISADevice *s, const char *boot_device, Error **errp)
468 #define PC_MAX_BOOT_DEVICES 3
469 int nbds, bds[3] = { 0, };
470 int i;
472 nbds = strlen(boot_device);
473 if (nbds > PC_MAX_BOOT_DEVICES) {
474 error_setg(errp, "Too many boot devices for PC");
475 return;
477 for (i = 0; i < nbds; i++) {
478 bds[i] = boot_device2nibble(boot_device[i]);
479 if (bds[i] == 0) {
480 error_setg(errp, "Invalid boot device for PC: '%c'",
481 boot_device[i]);
482 return;
485 rtc_set_memory(s, 0x3d, (bds[1] << 4) | bds[0]);
486 rtc_set_memory(s, 0x38, (bds[2] << 4) | (fd_bootchk ? 0x0 : 0x1));
489 static void pc_boot_set(void *opaque, const char *boot_device, Error **errp)
491 set_boot_dev(opaque, boot_device, errp);
494 static void pc_cmos_init_floppy(ISADevice *rtc_state, ISADevice *floppy)
496 int val, nb, i;
497 FloppyDriveType fd_type[2] = { FLOPPY_DRIVE_TYPE_NONE,
498 FLOPPY_DRIVE_TYPE_NONE };
500 /* floppy type */
501 if (floppy) {
502 for (i = 0; i < 2; i++) {
503 fd_type[i] = isa_fdc_get_drive_type(floppy, i);
506 val = (cmos_get_fd_drive_type(fd_type[0]) << 4) |
507 cmos_get_fd_drive_type(fd_type[1]);
508 rtc_set_memory(rtc_state, 0x10, val);
510 val = rtc_get_memory(rtc_state, REG_EQUIPMENT_BYTE);
511 nb = 0;
512 if (fd_type[0] != FLOPPY_DRIVE_TYPE_NONE) {
513 nb++;
515 if (fd_type[1] != FLOPPY_DRIVE_TYPE_NONE) {
516 nb++;
518 switch (nb) {
519 case 0:
520 break;
521 case 1:
522 val |= 0x01; /* 1 drive, ready for boot */
523 break;
524 case 2:
525 val |= 0x41; /* 2 drives, ready for boot */
526 break;
528 rtc_set_memory(rtc_state, REG_EQUIPMENT_BYTE, val);
531 typedef struct pc_cmos_init_late_arg {
532 ISADevice *rtc_state;
533 BusState *idebus[2];
534 } pc_cmos_init_late_arg;
536 typedef struct check_fdc_state {
537 ISADevice *floppy;
538 bool multiple;
539 } CheckFdcState;
541 static int check_fdc(Object *obj, void *opaque)
543 CheckFdcState *state = opaque;
544 Object *fdc;
545 uint32_t iobase;
546 Error *local_err = NULL;
548 fdc = object_dynamic_cast(obj, TYPE_ISA_FDC);
549 if (!fdc) {
550 return 0;
553 iobase = object_property_get_uint(obj, "iobase", &local_err);
554 if (local_err || iobase != 0x3f0) {
555 error_free(local_err);
556 return 0;
559 if (state->floppy) {
560 state->multiple = true;
561 } else {
562 state->floppy = ISA_DEVICE(obj);
564 return 0;
567 static const char * const fdc_container_path[] = {
568 "/unattached", "/peripheral", "/peripheral-anon"
572 * Locate the FDC at IO address 0x3f0, in order to configure the CMOS registers
573 * and ACPI objects.
575 static ISADevice *pc_find_fdc0(void)
577 int i;
578 Object *container;
579 CheckFdcState state = { 0 };
581 for (i = 0; i < ARRAY_SIZE(fdc_container_path); i++) {
582 container = container_get(qdev_get_machine(), fdc_container_path[i]);
583 object_child_foreach(container, check_fdc, &state);
586 if (state.multiple) {
587 warn_report("multiple floppy disk controllers with "
588 "iobase=0x3f0 have been found");
589 error_printf("the one being picked for CMOS setup might not reflect "
590 "your intent");
593 return state.floppy;
596 static void pc_cmos_init_late(void *opaque)
598 pc_cmos_init_late_arg *arg = opaque;
599 ISADevice *s = arg->rtc_state;
600 int16_t cylinders;
601 int8_t heads, sectors;
602 int val;
603 int i, trans;
605 val = 0;
606 if (arg->idebus[0] && ide_get_geometry(arg->idebus[0], 0,
607 &cylinders, &heads, &sectors) >= 0) {
608 cmos_init_hd(s, 0x19, 0x1b, cylinders, heads, sectors);
609 val |= 0xf0;
611 if (arg->idebus[0] && ide_get_geometry(arg->idebus[0], 1,
612 &cylinders, &heads, &sectors) >= 0) {
613 cmos_init_hd(s, 0x1a, 0x24, cylinders, heads, sectors);
614 val |= 0x0f;
616 rtc_set_memory(s, 0x12, val);
618 val = 0;
619 for (i = 0; i < 4; i++) {
620 /* NOTE: ide_get_geometry() returns the physical
621 geometry. It is always such that: 1 <= sects <= 63, 1
622 <= heads <= 16, 1 <= cylinders <= 16383. The BIOS
623 geometry can be different if a translation is done. */
624 if (arg->idebus[i / 2] &&
625 ide_get_geometry(arg->idebus[i / 2], i % 2,
626 &cylinders, &heads, &sectors) >= 0) {
627 trans = ide_get_bios_chs_trans(arg->idebus[i / 2], i % 2) - 1;
628 assert((trans & ~3) == 0);
629 val |= trans << (i * 2);
632 rtc_set_memory(s, 0x39, val);
634 pc_cmos_init_floppy(s, pc_find_fdc0());
636 qemu_unregister_reset(pc_cmos_init_late, opaque);
639 void pc_cmos_init(PCMachineState *pcms,
640 BusState *idebus0, BusState *idebus1,
641 ISADevice *s)
643 int val;
644 static pc_cmos_init_late_arg arg;
645 X86MachineState *x86ms = X86_MACHINE(pcms);
647 /* various important CMOS locations needed by PC/Bochs bios */
649 /* memory size */
650 /* base memory (first MiB) */
651 val = MIN(x86ms->below_4g_mem_size / KiB, 640);
652 rtc_set_memory(s, 0x15, val);
653 rtc_set_memory(s, 0x16, val >> 8);
654 /* extended memory (next 64MiB) */
655 if (x86ms->below_4g_mem_size > 1 * MiB) {
656 val = (x86ms->below_4g_mem_size - 1 * MiB) / KiB;
657 } else {
658 val = 0;
660 if (val > 65535)
661 val = 65535;
662 rtc_set_memory(s, 0x17, val);
663 rtc_set_memory(s, 0x18, val >> 8);
664 rtc_set_memory(s, 0x30, val);
665 rtc_set_memory(s, 0x31, val >> 8);
666 /* memory between 16MiB and 4GiB */
667 if (x86ms->below_4g_mem_size > 16 * MiB) {
668 val = (x86ms->below_4g_mem_size - 16 * MiB) / (64 * KiB);
669 } else {
670 val = 0;
672 if (val > 65535)
673 val = 65535;
674 rtc_set_memory(s, 0x34, val);
675 rtc_set_memory(s, 0x35, val >> 8);
676 /* memory above 4GiB */
677 val = x86ms->above_4g_mem_size / 65536;
678 rtc_set_memory(s, 0x5b, val);
679 rtc_set_memory(s, 0x5c, val >> 8);
680 rtc_set_memory(s, 0x5d, val >> 16);
682 object_property_add_link(OBJECT(pcms), "rtc_state",
683 TYPE_ISA_DEVICE,
684 (Object **)&x86ms->rtc,
685 object_property_allow_set_link,
686 OBJ_PROP_LINK_STRONG);
687 object_property_set_link(OBJECT(pcms), "rtc_state", OBJECT(s),
688 &error_abort);
690 set_boot_dev(s, MACHINE(pcms)->boot_config.order, &error_fatal);
692 val = 0;
693 val |= 0x02; /* FPU is there */
694 val |= 0x04; /* PS/2 mouse installed */
695 rtc_set_memory(s, REG_EQUIPMENT_BYTE, val);
697 /* hard drives and FDC */
698 arg.rtc_state = s;
699 arg.idebus[0] = idebus0;
700 arg.idebus[1] = idebus1;
701 qemu_register_reset(pc_cmos_init_late, &arg);
704 static void handle_a20_line_change(void *opaque, int irq, int level)
706 X86CPU *cpu = opaque;
708 /* XXX: send to all CPUs ? */
709 /* XXX: add logic to handle multiple A20 line sources */
710 x86_cpu_set_a20(cpu, level);
713 #define NE2000_NB_MAX 6
715 static const int ne2000_io[NE2000_NB_MAX] = { 0x300, 0x320, 0x340, 0x360,
716 0x280, 0x380 };
717 static const int ne2000_irq[NE2000_NB_MAX] = { 9, 10, 11, 3, 4, 5 };
719 static void pc_init_ne2k_isa(ISABus *bus, NICInfo *nd)
721 static int nb_ne2k = 0;
723 if (nb_ne2k == NE2000_NB_MAX)
724 return;
725 isa_ne2000_init(bus, ne2000_io[nb_ne2k],
726 ne2000_irq[nb_ne2k], nd);
727 nb_ne2k++;
730 void pc_acpi_smi_interrupt(void *opaque, int irq, int level)
732 X86CPU *cpu = opaque;
734 if (level) {
735 cpu_interrupt(CPU(cpu), CPU_INTERRUPT_SMI);
739 static
740 void pc_machine_done(Notifier *notifier, void *data)
742 PCMachineState *pcms = container_of(notifier,
743 PCMachineState, machine_done);
744 X86MachineState *x86ms = X86_MACHINE(pcms);
746 cxl_hook_up_pxb_registers(pcms->bus, &pcms->cxl_devices_state,
747 &error_fatal);
749 if (pcms->cxl_devices_state.is_enabled) {
750 cxl_fmws_link_targets(&pcms->cxl_devices_state, &error_fatal);
753 /* set the number of CPUs */
754 x86_rtc_set_cpus_count(x86ms->rtc, x86ms->boot_cpus);
756 fw_cfg_add_extra_pci_roots(pcms->bus, x86ms->fw_cfg);
758 acpi_setup();
759 if (x86ms->fw_cfg) {
760 fw_cfg_build_smbios(MACHINE(pcms), x86ms->fw_cfg);
761 fw_cfg_build_feature_control(MACHINE(pcms), x86ms->fw_cfg);
762 /* update FW_CFG_NB_CPUS to account for -device added CPUs */
763 fw_cfg_modify_i16(x86ms->fw_cfg, FW_CFG_NB_CPUS, x86ms->boot_cpus);
767 void pc_guest_info_init(PCMachineState *pcms)
769 X86MachineState *x86ms = X86_MACHINE(pcms);
771 x86ms->apic_xrupt_override = true;
772 pcms->machine_done.notify = pc_machine_done;
773 qemu_add_machine_init_done_notifier(&pcms->machine_done);
776 /* setup pci memory address space mapping into system address space */
777 void pc_pci_as_mapping_init(Object *owner, MemoryRegion *system_memory,
778 MemoryRegion *pci_address_space)
780 /* Set to lower priority than RAM */
781 memory_region_add_subregion_overlap(system_memory, 0x0,
782 pci_address_space, -1);
785 void xen_load_linux(PCMachineState *pcms)
787 int i;
788 FWCfgState *fw_cfg;
789 PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms);
790 X86MachineState *x86ms = X86_MACHINE(pcms);
792 assert(MACHINE(pcms)->kernel_filename != NULL);
794 fw_cfg = fw_cfg_init_io(FW_CFG_IO_BASE);
795 fw_cfg_add_i16(fw_cfg, FW_CFG_NB_CPUS, x86ms->boot_cpus);
796 rom_set_fw(fw_cfg);
798 x86_load_linux(x86ms, fw_cfg, pcmc->acpi_data_size,
799 pcmc->pvh_enabled, pcmc->legacy_no_rng_seed);
800 for (i = 0; i < nb_option_roms; i++) {
801 assert(!strcmp(option_rom[i].name, "linuxboot.bin") ||
802 !strcmp(option_rom[i].name, "linuxboot_dma.bin") ||
803 !strcmp(option_rom[i].name, "pvh.bin") ||
804 !strcmp(option_rom[i].name, "multiboot.bin") ||
805 !strcmp(option_rom[i].name, "multiboot_dma.bin"));
806 rom_add_option(option_rom[i].name, option_rom[i].bootindex);
808 x86ms->fw_cfg = fw_cfg;
811 #define PC_ROM_MIN_VGA 0xc0000
812 #define PC_ROM_MIN_OPTION 0xc8000
813 #define PC_ROM_MAX 0xe0000
814 #define PC_ROM_ALIGN 0x800
815 #define PC_ROM_SIZE (PC_ROM_MAX - PC_ROM_MIN_VGA)
817 static hwaddr pc_above_4g_end(PCMachineState *pcms)
819 X86MachineState *x86ms = X86_MACHINE(pcms);
821 if (pcms->sgx_epc.size != 0) {
822 return sgx_epc_above_4g_end(&pcms->sgx_epc);
825 return x86ms->above_4g_mem_start + x86ms->above_4g_mem_size;
828 static void pc_get_device_memory_range(PCMachineState *pcms,
829 hwaddr *base,
830 ram_addr_t *device_mem_size)
832 PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms);
833 MachineState *machine = MACHINE(pcms);
834 ram_addr_t size;
835 hwaddr addr;
837 size = machine->maxram_size - machine->ram_size;
838 addr = ROUND_UP(pc_above_4g_end(pcms), 1 * GiB);
840 if (pcmc->enforce_aligned_dimm) {
841 /* size device region assuming 1G page max alignment per slot */
842 size += (1 * GiB) * machine->ram_slots;
845 *base = addr;
846 *device_mem_size = size;
849 static uint64_t pc_get_cxl_range_start(PCMachineState *pcms)
851 PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms);
852 hwaddr cxl_base;
853 ram_addr_t size;
855 if (pcmc->has_reserved_memory) {
856 pc_get_device_memory_range(pcms, &cxl_base, &size);
857 cxl_base += size;
858 } else {
859 cxl_base = pc_above_4g_end(pcms);
862 return cxl_base;
865 static uint64_t pc_get_cxl_range_end(PCMachineState *pcms)
867 uint64_t start = pc_get_cxl_range_start(pcms) + MiB;
869 if (pcms->cxl_devices_state.fixed_windows) {
870 GList *it;
872 start = ROUND_UP(start, 256 * MiB);
873 for (it = pcms->cxl_devices_state.fixed_windows; it; it = it->next) {
874 CXLFixedWindow *fw = it->data;
875 start += fw->size;
879 return start;
882 static hwaddr pc_max_used_gpa(PCMachineState *pcms, uint64_t pci_hole64_size)
884 X86CPU *cpu = X86_CPU(first_cpu);
886 /* 32-bit systems don't have hole64 thus return max CPU address */
887 if (cpu->phys_bits <= 32) {
888 return ((hwaddr)1 << cpu->phys_bits) - 1;
891 return pc_pci_hole64_start() + pci_hole64_size - 1;
894 void pc_memory_init(PCMachineState *pcms,
895 MemoryRegion *system_memory,
896 MemoryRegion *rom_memory,
897 MemoryRegion **ram_memory,
898 uint64_t pci_hole64_size)
900 int linux_boot, i;
901 MemoryRegion *option_rom_mr;
902 MemoryRegion *ram_below_4g, *ram_above_4g;
903 FWCfgState *fw_cfg;
904 MachineState *machine = MACHINE(pcms);
905 MachineClass *mc = MACHINE_GET_CLASS(machine);
906 PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms);
907 X86MachineState *x86ms = X86_MACHINE(pcms);
908 hwaddr maxphysaddr, maxusedaddr;
909 hwaddr cxl_base, cxl_resv_end = 0;
910 X86CPU *cpu = X86_CPU(first_cpu);
912 assert(machine->ram_size == x86ms->below_4g_mem_size +
913 x86ms->above_4g_mem_size);
915 linux_boot = (machine->kernel_filename != NULL);
918 * phys-bits is required to be appropriately configured
919 * to make sure max used GPA is reachable.
921 maxusedaddr = pc_max_used_gpa(pcms, pci_hole64_size);
922 maxphysaddr = ((hwaddr)1 << cpu->phys_bits) - 1;
923 if (maxphysaddr < maxusedaddr) {
924 error_report("Address space limit 0x%"PRIx64" < 0x%"PRIx64
925 " phys-bits too low (%u)",
926 maxphysaddr, maxusedaddr, cpu->phys_bits);
927 exit(EXIT_FAILURE);
931 * Split single memory region and use aliases to address portions of it,
932 * done for backwards compatibility with older qemus.
934 *ram_memory = machine->ram;
935 ram_below_4g = g_malloc(sizeof(*ram_below_4g));
936 memory_region_init_alias(ram_below_4g, NULL, "ram-below-4g", machine->ram,
937 0, x86ms->below_4g_mem_size);
938 memory_region_add_subregion(system_memory, 0, ram_below_4g);
939 e820_add_entry(0, x86ms->below_4g_mem_size, E820_RAM);
940 if (x86ms->above_4g_mem_size > 0) {
941 ram_above_4g = g_malloc(sizeof(*ram_above_4g));
942 memory_region_init_alias(ram_above_4g, NULL, "ram-above-4g",
943 machine->ram,
944 x86ms->below_4g_mem_size,
945 x86ms->above_4g_mem_size);
946 memory_region_add_subregion(system_memory, x86ms->above_4g_mem_start,
947 ram_above_4g);
948 e820_add_entry(x86ms->above_4g_mem_start, x86ms->above_4g_mem_size,
949 E820_RAM);
952 if (pcms->sgx_epc.size != 0) {
953 e820_add_entry(pcms->sgx_epc.base, pcms->sgx_epc.size, E820_RESERVED);
956 if (!pcmc->has_reserved_memory &&
957 (machine->ram_slots ||
958 (machine->maxram_size > machine->ram_size))) {
960 error_report("\"-memory 'slots|maxmem'\" is not supported by: %s",
961 mc->name);
962 exit(EXIT_FAILURE);
965 /* always allocate the device memory information */
966 machine->device_memory = g_malloc0(sizeof(*machine->device_memory));
968 /* initialize device memory address space */
969 if (pcmc->has_reserved_memory &&
970 (machine->ram_size < machine->maxram_size)) {
971 ram_addr_t device_mem_size;
973 if (machine->ram_slots > ACPI_MAX_RAM_SLOTS) {
974 error_report("unsupported amount of memory slots: %"PRIu64,
975 machine->ram_slots);
976 exit(EXIT_FAILURE);
979 if (QEMU_ALIGN_UP(machine->maxram_size,
980 TARGET_PAGE_SIZE) != machine->maxram_size) {
981 error_report("maximum memory size must by aligned to multiple of "
982 "%d bytes", TARGET_PAGE_SIZE);
983 exit(EXIT_FAILURE);
986 pc_get_device_memory_range(pcms, &machine->device_memory->base, &device_mem_size);
988 if ((machine->device_memory->base + device_mem_size) <
989 device_mem_size) {
990 error_report("unsupported amount of maximum memory: " RAM_ADDR_FMT,
991 machine->maxram_size);
992 exit(EXIT_FAILURE);
995 memory_region_init(&machine->device_memory->mr, OBJECT(pcms),
996 "device-memory", device_mem_size);
997 memory_region_add_subregion(system_memory, machine->device_memory->base,
998 &machine->device_memory->mr);
1001 if (pcms->cxl_devices_state.is_enabled) {
1002 MemoryRegion *mr = &pcms->cxl_devices_state.host_mr;
1003 hwaddr cxl_size = MiB;
1005 cxl_base = pc_get_cxl_range_start(pcms);
1006 e820_add_entry(cxl_base, cxl_size, E820_RESERVED);
1007 memory_region_init(mr, OBJECT(machine), "cxl_host_reg", cxl_size);
1008 memory_region_add_subregion(system_memory, cxl_base, mr);
1009 cxl_resv_end = cxl_base + cxl_size;
1010 if (pcms->cxl_devices_state.fixed_windows) {
1011 hwaddr cxl_fmw_base;
1012 GList *it;
1014 cxl_fmw_base = ROUND_UP(cxl_base + cxl_size, 256 * MiB);
1015 for (it = pcms->cxl_devices_state.fixed_windows; it; it = it->next) {
1016 CXLFixedWindow *fw = it->data;
1018 fw->base = cxl_fmw_base;
1019 memory_region_init_io(&fw->mr, OBJECT(machine), &cfmws_ops, fw,
1020 "cxl-fixed-memory-region", fw->size);
1021 memory_region_add_subregion(system_memory, fw->base, &fw->mr);
1022 e820_add_entry(fw->base, fw->size, E820_RESERVED);
1023 cxl_fmw_base += fw->size;
1024 cxl_resv_end = cxl_fmw_base;
1029 /* Initialize PC system firmware */
1030 pc_system_firmware_init(pcms, rom_memory);
1032 option_rom_mr = g_malloc(sizeof(*option_rom_mr));
1033 memory_region_init_ram(option_rom_mr, NULL, "pc.rom", PC_ROM_SIZE,
1034 &error_fatal);
1035 if (pcmc->pci_enabled) {
1036 memory_region_set_readonly(option_rom_mr, true);
1038 memory_region_add_subregion_overlap(rom_memory,
1039 PC_ROM_MIN_VGA,
1040 option_rom_mr,
1043 fw_cfg = fw_cfg_arch_create(machine,
1044 x86ms->boot_cpus, x86ms->apic_id_limit);
1046 rom_set_fw(fw_cfg);
1048 if (pcmc->has_reserved_memory && machine->device_memory->base) {
1049 uint64_t *val = g_malloc(sizeof(*val));
1050 PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms);
1051 uint64_t res_mem_end = machine->device_memory->base;
1053 if (!pcmc->broken_reserved_end) {
1054 res_mem_end += memory_region_size(&machine->device_memory->mr);
1057 if (pcms->cxl_devices_state.is_enabled) {
1058 res_mem_end = cxl_resv_end;
1060 *val = cpu_to_le64(ROUND_UP(res_mem_end, 1 * GiB));
1061 fw_cfg_add_file(fw_cfg, "etc/reserved-memory-end", val, sizeof(*val));
1064 if (linux_boot) {
1065 x86_load_linux(x86ms, fw_cfg, pcmc->acpi_data_size,
1066 pcmc->pvh_enabled, pcmc->legacy_no_rng_seed);
1069 for (i = 0; i < nb_option_roms; i++) {
1070 rom_add_option(option_rom[i].name, option_rom[i].bootindex);
1072 x86ms->fw_cfg = fw_cfg;
1074 /* Init default IOAPIC address space */
1075 x86ms->ioapic_as = &address_space_memory;
1077 /* Init ACPI memory hotplug IO base address */
1078 pcms->memhp_io_base = ACPI_MEMORY_HOTPLUG_BASE;
1082 * The 64bit pci hole starts after "above 4G RAM" and
1083 * potentially the space reserved for memory hotplug.
1085 uint64_t pc_pci_hole64_start(void)
1087 PCMachineState *pcms = PC_MACHINE(qdev_get_machine());
1088 PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms);
1089 MachineState *ms = MACHINE(pcms);
1090 uint64_t hole64_start = 0;
1091 ram_addr_t size = 0;
1093 if (pcms->cxl_devices_state.is_enabled) {
1094 hole64_start = pc_get_cxl_range_end(pcms);
1095 } else if (pcmc->has_reserved_memory && (ms->ram_size < ms->maxram_size)) {
1096 pc_get_device_memory_range(pcms, &hole64_start, &size);
1097 if (!pcmc->broken_reserved_end) {
1098 hole64_start += size;
1100 } else {
1101 hole64_start = pc_above_4g_end(pcms);
1104 return ROUND_UP(hole64_start, 1 * GiB);
1107 DeviceState *pc_vga_init(ISABus *isa_bus, PCIBus *pci_bus)
1109 DeviceState *dev = NULL;
1111 rom_set_order_override(FW_CFG_ORDER_OVERRIDE_VGA);
1112 if (pci_bus) {
1113 PCIDevice *pcidev = pci_vga_init(pci_bus);
1114 dev = pcidev ? &pcidev->qdev : NULL;
1115 } else if (isa_bus) {
1116 ISADevice *isadev = isa_vga_init(isa_bus);
1117 dev = isadev ? DEVICE(isadev) : NULL;
1119 rom_reset_order_override();
1120 return dev;
1123 static const MemoryRegionOps ioport80_io_ops = {
1124 .write = ioport80_write,
1125 .read = ioport80_read,
1126 .endianness = DEVICE_NATIVE_ENDIAN,
1127 .impl = {
1128 .min_access_size = 1,
1129 .max_access_size = 1,
1133 static const MemoryRegionOps ioportF0_io_ops = {
1134 .write = ioportF0_write,
1135 .read = ioportF0_read,
1136 .endianness = DEVICE_NATIVE_ENDIAN,
1137 .impl = {
1138 .min_access_size = 1,
1139 .max_access_size = 1,
1143 static void pc_superio_init(ISABus *isa_bus, bool create_fdctrl,
1144 bool create_i8042, bool no_vmport)
1146 int i;
1147 DriveInfo *fd[MAX_FD];
1148 qemu_irq *a20_line;
1149 ISADevice *fdc, *i8042, *port92, *vmmouse;
1151 serial_hds_isa_init(isa_bus, 0, MAX_ISA_SERIAL_PORTS);
1152 parallel_hds_isa_init(isa_bus, MAX_PARALLEL_PORTS);
1154 for (i = 0; i < MAX_FD; i++) {
1155 fd[i] = drive_get(IF_FLOPPY, 0, i);
1156 create_fdctrl |= !!fd[i];
1158 if (create_fdctrl) {
1159 fdc = isa_new(TYPE_ISA_FDC);
1160 if (fdc) {
1161 isa_realize_and_unref(fdc, isa_bus, &error_fatal);
1162 isa_fdc_init_drives(fdc, fd);
1166 if (!create_i8042) {
1167 return;
1170 i8042 = isa_create_simple(isa_bus, TYPE_I8042);
1171 if (!no_vmport) {
1172 isa_create_simple(isa_bus, TYPE_VMPORT);
1173 vmmouse = isa_try_new("vmmouse");
1174 } else {
1175 vmmouse = NULL;
1177 if (vmmouse) {
1178 object_property_set_link(OBJECT(vmmouse), TYPE_I8042, OBJECT(i8042),
1179 &error_abort);
1180 isa_realize_and_unref(vmmouse, isa_bus, &error_fatal);
1182 port92 = isa_create_simple(isa_bus, TYPE_PORT92);
1184 a20_line = qemu_allocate_irqs(handle_a20_line_change, first_cpu, 2);
1185 i8042_setup_a20_line(i8042, a20_line[0]);
1186 qdev_connect_gpio_out_named(DEVICE(port92),
1187 PORT92_A20_LINE, 0, a20_line[1]);
1188 g_free(a20_line);
1191 void pc_basic_device_init(struct PCMachineState *pcms,
1192 ISABus *isa_bus, qemu_irq *gsi,
1193 ISADevice **rtc_state,
1194 bool create_fdctrl,
1195 uint32_t hpet_irqs)
1197 int i;
1198 DeviceState *hpet = NULL;
1199 int pit_isa_irq = 0;
1200 qemu_irq pit_alt_irq = NULL;
1201 qemu_irq rtc_irq = NULL;
1202 ISADevice *pit = NULL;
1203 MemoryRegion *ioport80_io = g_new(MemoryRegion, 1);
1204 MemoryRegion *ioportF0_io = g_new(MemoryRegion, 1);
1205 X86MachineState *x86ms = X86_MACHINE(pcms);
1207 memory_region_init_io(ioport80_io, NULL, &ioport80_io_ops, NULL, "ioport80", 1);
1208 memory_region_add_subregion(isa_bus->address_space_io, 0x80, ioport80_io);
1210 memory_region_init_io(ioportF0_io, NULL, &ioportF0_io_ops, NULL, "ioportF0", 1);
1211 memory_region_add_subregion(isa_bus->address_space_io, 0xf0, ioportF0_io);
1214 * Check if an HPET shall be created.
1216 * Without KVM_CAP_PIT_STATE2, we cannot switch off the in-kernel PIT
1217 * when the HPET wants to take over. Thus we have to disable the latter.
1219 if (pcms->hpet_enabled && (!kvm_irqchip_in_kernel() ||
1220 kvm_has_pit_state2())) {
1221 hpet = qdev_try_new(TYPE_HPET);
1222 if (!hpet) {
1223 error_report("couldn't create HPET device");
1224 exit(1);
1227 * For pc-piix-*, hpet's intcap is always IRQ2. For pc-q35-1.7 and
1228 * earlier, use IRQ2 for compat. Otherwise, use IRQ16~23, IRQ8 and
1229 * IRQ2.
1231 uint8_t compat = object_property_get_uint(OBJECT(hpet),
1232 HPET_INTCAP, NULL);
1233 if (!compat) {
1234 qdev_prop_set_uint32(hpet, HPET_INTCAP, hpet_irqs);
1236 sysbus_realize_and_unref(SYS_BUS_DEVICE(hpet), &error_fatal);
1237 sysbus_mmio_map(SYS_BUS_DEVICE(hpet), 0, HPET_BASE);
1239 for (i = 0; i < GSI_NUM_PINS; i++) {
1240 sysbus_connect_irq(SYS_BUS_DEVICE(hpet), i, gsi[i]);
1242 pit_isa_irq = -1;
1243 pit_alt_irq = qdev_get_gpio_in(hpet, HPET_LEGACY_PIT_INT);
1244 rtc_irq = qdev_get_gpio_in(hpet, HPET_LEGACY_RTC_INT);
1246 *rtc_state = mc146818_rtc_init(isa_bus, 2000, rtc_irq);
1248 qemu_register_boot_set(pc_boot_set, *rtc_state);
1250 if (!xen_enabled() &&
1251 (x86ms->pit == ON_OFF_AUTO_AUTO || x86ms->pit == ON_OFF_AUTO_ON)) {
1252 if (kvm_pit_in_kernel()) {
1253 pit = kvm_pit_init(isa_bus, 0x40);
1254 } else {
1255 pit = i8254_pit_init(isa_bus, 0x40, pit_isa_irq, pit_alt_irq);
1257 if (hpet) {
1258 /* connect PIT to output control line of the HPET */
1259 qdev_connect_gpio_out(hpet, 0, qdev_get_gpio_in(DEVICE(pit), 0));
1261 pcspk_init(pcms->pcspk, isa_bus, pit);
1264 i8257_dma_init(isa_bus, 0);
1266 /* Super I/O */
1267 pc_superio_init(isa_bus, create_fdctrl, pcms->i8042_enabled,
1268 pcms->vmport != ON_OFF_AUTO_ON);
1271 void pc_nic_init(PCMachineClass *pcmc, ISABus *isa_bus, PCIBus *pci_bus)
1273 int i;
1275 rom_set_order_override(FW_CFG_ORDER_OVERRIDE_NIC);
1276 for (i = 0; i < nb_nics; i++) {
1277 NICInfo *nd = &nd_table[i];
1278 const char *model = nd->model ? nd->model : pcmc->default_nic_model;
1280 if (g_str_equal(model, "ne2k_isa")) {
1281 pc_init_ne2k_isa(isa_bus, nd);
1282 } else {
1283 pci_nic_init_nofail(nd, pci_bus, model, NULL);
1286 rom_reset_order_override();
1289 void pc_i8259_create(ISABus *isa_bus, qemu_irq *i8259_irqs)
1291 qemu_irq *i8259;
1293 if (kvm_pic_in_kernel()) {
1294 i8259 = kvm_i8259_init(isa_bus);
1295 } else if (xen_enabled()) {
1296 i8259 = xen_interrupt_controller_init();
1297 } else {
1298 i8259 = i8259_init(isa_bus, x86_allocate_cpu_irq());
1301 for (size_t i = 0; i < ISA_NUM_IRQS; i++) {
1302 i8259_irqs[i] = i8259[i];
1305 g_free(i8259);
1308 static void pc_memory_pre_plug(HotplugHandler *hotplug_dev, DeviceState *dev,
1309 Error **errp)
1311 const PCMachineState *pcms = PC_MACHINE(hotplug_dev);
1312 const X86MachineState *x86ms = X86_MACHINE(hotplug_dev);
1313 const PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms);
1314 const MachineState *ms = MACHINE(hotplug_dev);
1315 const bool is_nvdimm = object_dynamic_cast(OBJECT(dev), TYPE_NVDIMM);
1316 const uint64_t legacy_align = TARGET_PAGE_SIZE;
1317 Error *local_err = NULL;
1320 * When -no-acpi is used with Q35 machine type, no ACPI is built,
1321 * but pcms->acpi_dev is still created. Check !acpi_enabled in
1322 * addition to cover this case.
1324 if (!x86ms->acpi_dev || !x86_machine_is_acpi_enabled(x86ms)) {
1325 error_setg(errp,
1326 "memory hotplug is not enabled: missing acpi device or acpi disabled");
1327 return;
1330 if (is_nvdimm && !ms->nvdimms_state->is_enabled) {
1331 error_setg(errp, "nvdimm is not enabled: missing 'nvdimm' in '-M'");
1332 return;
1335 hotplug_handler_pre_plug(x86ms->acpi_dev, dev, &local_err);
1336 if (local_err) {
1337 error_propagate(errp, local_err);
1338 return;
1341 pc_dimm_pre_plug(PC_DIMM(dev), MACHINE(hotplug_dev),
1342 pcmc->enforce_aligned_dimm ? NULL : &legacy_align, errp);
1345 static void pc_memory_plug(HotplugHandler *hotplug_dev,
1346 DeviceState *dev, Error **errp)
1348 PCMachineState *pcms = PC_MACHINE(hotplug_dev);
1349 X86MachineState *x86ms = X86_MACHINE(hotplug_dev);
1350 MachineState *ms = MACHINE(hotplug_dev);
1351 bool is_nvdimm = object_dynamic_cast(OBJECT(dev), TYPE_NVDIMM);
1353 pc_dimm_plug(PC_DIMM(dev), MACHINE(pcms));
1355 if (is_nvdimm) {
1356 nvdimm_plug(ms->nvdimms_state);
1359 hotplug_handler_plug(x86ms->acpi_dev, dev, &error_abort);
1362 static void pc_memory_unplug_request(HotplugHandler *hotplug_dev,
1363 DeviceState *dev, Error **errp)
1365 X86MachineState *x86ms = X86_MACHINE(hotplug_dev);
1368 * When -no-acpi is used with Q35 machine type, no ACPI is built,
1369 * but pcms->acpi_dev is still created. Check !acpi_enabled in
1370 * addition to cover this case.
1372 if (!x86ms->acpi_dev || !x86_machine_is_acpi_enabled(x86ms)) {
1373 error_setg(errp,
1374 "memory hotplug is not enabled: missing acpi device or acpi disabled");
1375 return;
1378 if (object_dynamic_cast(OBJECT(dev), TYPE_NVDIMM)) {
1379 error_setg(errp, "nvdimm device hot unplug is not supported yet.");
1380 return;
1383 hotplug_handler_unplug_request(x86ms->acpi_dev, dev,
1384 errp);
1387 static void pc_memory_unplug(HotplugHandler *hotplug_dev,
1388 DeviceState *dev, Error **errp)
1390 PCMachineState *pcms = PC_MACHINE(hotplug_dev);
1391 X86MachineState *x86ms = X86_MACHINE(hotplug_dev);
1392 Error *local_err = NULL;
1394 hotplug_handler_unplug(x86ms->acpi_dev, dev, &local_err);
1395 if (local_err) {
1396 goto out;
1399 pc_dimm_unplug(PC_DIMM(dev), MACHINE(pcms));
1400 qdev_unrealize(dev);
1401 out:
1402 error_propagate(errp, local_err);
1405 static void pc_virtio_md_pci_pre_plug(HotplugHandler *hotplug_dev,
1406 DeviceState *dev, Error **errp)
1408 HotplugHandler *hotplug_dev2 = qdev_get_bus_hotplug_handler(dev);
1409 Error *local_err = NULL;
1411 if (!hotplug_dev2 && dev->hotplugged) {
1413 * Without a bus hotplug handler, we cannot control the plug/unplug
1414 * order. We should never reach this point when hotplugging on x86,
1415 * however, better add a safety net.
1417 error_setg(errp, "hotplug of virtio based memory devices not supported"
1418 " on this bus.");
1419 return;
1422 * First, see if we can plug this memory device at all. If that
1423 * succeeds, branch of to the actual hotplug handler.
1425 memory_device_pre_plug(MEMORY_DEVICE(dev), MACHINE(hotplug_dev), NULL,
1426 &local_err);
1427 if (!local_err && hotplug_dev2) {
1428 hotplug_handler_pre_plug(hotplug_dev2, dev, &local_err);
1430 error_propagate(errp, local_err);
1433 static void pc_virtio_md_pci_plug(HotplugHandler *hotplug_dev,
1434 DeviceState *dev, Error **errp)
1436 HotplugHandler *hotplug_dev2 = qdev_get_bus_hotplug_handler(dev);
1437 Error *local_err = NULL;
1440 * Plug the memory device first and then branch off to the actual
1441 * hotplug handler. If that one fails, we can easily undo the memory
1442 * device bits.
1444 memory_device_plug(MEMORY_DEVICE(dev), MACHINE(hotplug_dev));
1445 if (hotplug_dev2) {
1446 hotplug_handler_plug(hotplug_dev2, dev, &local_err);
1447 if (local_err) {
1448 memory_device_unplug(MEMORY_DEVICE(dev), MACHINE(hotplug_dev));
1451 error_propagate(errp, local_err);
1454 static void pc_virtio_md_pci_unplug_request(HotplugHandler *hotplug_dev,
1455 DeviceState *dev, Error **errp)
1457 /* We don't support hot unplug of virtio based memory devices */
1458 error_setg(errp, "virtio based memory devices cannot be unplugged.");
1461 static void pc_virtio_md_pci_unplug(HotplugHandler *hotplug_dev,
1462 DeviceState *dev, Error **errp)
1464 /* We don't support hot unplug of virtio based memory devices */
1467 static void pc_machine_device_pre_plug_cb(HotplugHandler *hotplug_dev,
1468 DeviceState *dev, Error **errp)
1470 if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) {
1471 pc_memory_pre_plug(hotplug_dev, dev, errp);
1472 } else if (object_dynamic_cast(OBJECT(dev), TYPE_CPU)) {
1473 x86_cpu_pre_plug(hotplug_dev, dev, errp);
1474 } else if (object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_PMEM_PCI) ||
1475 object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_MEM_PCI)) {
1476 pc_virtio_md_pci_pre_plug(hotplug_dev, dev, errp);
1477 } else if (object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_IOMMU_PCI)) {
1478 /* Declare the APIC range as the reserved MSI region */
1479 char *resv_prop_str = g_strdup_printf("0xfee00000:0xfeefffff:%d",
1480 VIRTIO_IOMMU_RESV_MEM_T_MSI);
1482 object_property_set_uint(OBJECT(dev), "len-reserved-regions", 1, errp);
1483 object_property_set_str(OBJECT(dev), "reserved-regions[0]",
1484 resv_prop_str, errp);
1485 g_free(resv_prop_str);
1488 if (object_dynamic_cast(OBJECT(dev), TYPE_X86_IOMMU_DEVICE) ||
1489 object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_IOMMU_PCI)) {
1490 PCMachineState *pcms = PC_MACHINE(hotplug_dev);
1492 if (pcms->iommu) {
1493 error_setg(errp, "QEMU does not support multiple vIOMMUs "
1494 "for x86 yet.");
1495 return;
1497 pcms->iommu = dev;
1501 static void pc_machine_device_plug_cb(HotplugHandler *hotplug_dev,
1502 DeviceState *dev, Error **errp)
1504 if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) {
1505 pc_memory_plug(hotplug_dev, dev, errp);
1506 } else if (object_dynamic_cast(OBJECT(dev), TYPE_CPU)) {
1507 x86_cpu_plug(hotplug_dev, dev, errp);
1508 } else if (object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_PMEM_PCI) ||
1509 object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_MEM_PCI)) {
1510 pc_virtio_md_pci_plug(hotplug_dev, dev, errp);
1514 static void pc_machine_device_unplug_request_cb(HotplugHandler *hotplug_dev,
1515 DeviceState *dev, Error **errp)
1517 if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) {
1518 pc_memory_unplug_request(hotplug_dev, dev, errp);
1519 } else if (object_dynamic_cast(OBJECT(dev), TYPE_CPU)) {
1520 x86_cpu_unplug_request_cb(hotplug_dev, dev, errp);
1521 } else if (object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_PMEM_PCI) ||
1522 object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_MEM_PCI)) {
1523 pc_virtio_md_pci_unplug_request(hotplug_dev, dev, errp);
1524 } else {
1525 error_setg(errp, "acpi: device unplug request for not supported device"
1526 " type: %s", object_get_typename(OBJECT(dev)));
1530 static void pc_machine_device_unplug_cb(HotplugHandler *hotplug_dev,
1531 DeviceState *dev, Error **errp)
1533 if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) {
1534 pc_memory_unplug(hotplug_dev, dev, errp);
1535 } else if (object_dynamic_cast(OBJECT(dev), TYPE_CPU)) {
1536 x86_cpu_unplug_cb(hotplug_dev, dev, errp);
1537 } else if (object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_PMEM_PCI) ||
1538 object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_MEM_PCI)) {
1539 pc_virtio_md_pci_unplug(hotplug_dev, dev, errp);
1540 } else {
1541 error_setg(errp, "acpi: device unplug for not supported device"
1542 " type: %s", object_get_typename(OBJECT(dev)));
1546 static HotplugHandler *pc_get_hotplug_handler(MachineState *machine,
1547 DeviceState *dev)
1549 if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM) ||
1550 object_dynamic_cast(OBJECT(dev), TYPE_CPU) ||
1551 object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_PMEM_PCI) ||
1552 object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_MEM_PCI) ||
1553 object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_IOMMU_PCI) ||
1554 object_dynamic_cast(OBJECT(dev), TYPE_X86_IOMMU_DEVICE)) {
1555 return HOTPLUG_HANDLER(machine);
1558 return NULL;
1561 static void
1562 pc_machine_get_device_memory_region_size(Object *obj, Visitor *v,
1563 const char *name, void *opaque,
1564 Error **errp)
1566 MachineState *ms = MACHINE(obj);
1567 int64_t value = 0;
1569 if (ms->device_memory) {
1570 value = memory_region_size(&ms->device_memory->mr);
1573 visit_type_int(v, name, &value, errp);
1576 static void pc_machine_get_vmport(Object *obj, Visitor *v, const char *name,
1577 void *opaque, Error **errp)
1579 PCMachineState *pcms = PC_MACHINE(obj);
1580 OnOffAuto vmport = pcms->vmport;
1582 visit_type_OnOffAuto(v, name, &vmport, errp);
1585 static void pc_machine_set_vmport(Object *obj, Visitor *v, const char *name,
1586 void *opaque, Error **errp)
1588 PCMachineState *pcms = PC_MACHINE(obj);
1590 visit_type_OnOffAuto(v, name, &pcms->vmport, errp);
1593 static bool pc_machine_get_smbus(Object *obj, Error **errp)
1595 PCMachineState *pcms = PC_MACHINE(obj);
1597 return pcms->smbus_enabled;
1600 static void pc_machine_set_smbus(Object *obj, bool value, Error **errp)
1602 PCMachineState *pcms = PC_MACHINE(obj);
1604 pcms->smbus_enabled = value;
1607 static bool pc_machine_get_sata(Object *obj, Error **errp)
1609 PCMachineState *pcms = PC_MACHINE(obj);
1611 return pcms->sata_enabled;
1614 static void pc_machine_set_sata(Object *obj, bool value, Error **errp)
1616 PCMachineState *pcms = PC_MACHINE(obj);
1618 pcms->sata_enabled = value;
1621 static bool pc_machine_get_hpet(Object *obj, Error **errp)
1623 PCMachineState *pcms = PC_MACHINE(obj);
1625 return pcms->hpet_enabled;
1628 static void pc_machine_set_hpet(Object *obj, bool value, Error **errp)
1630 PCMachineState *pcms = PC_MACHINE(obj);
1632 pcms->hpet_enabled = value;
1635 static bool pc_machine_get_i8042(Object *obj, Error **errp)
1637 PCMachineState *pcms = PC_MACHINE(obj);
1639 return pcms->i8042_enabled;
1642 static void pc_machine_set_i8042(Object *obj, bool value, Error **errp)
1644 PCMachineState *pcms = PC_MACHINE(obj);
1646 pcms->i8042_enabled = value;
1649 static bool pc_machine_get_default_bus_bypass_iommu(Object *obj, Error **errp)
1651 PCMachineState *pcms = PC_MACHINE(obj);
1653 return pcms->default_bus_bypass_iommu;
1656 static void pc_machine_set_default_bus_bypass_iommu(Object *obj, bool value,
1657 Error **errp)
1659 PCMachineState *pcms = PC_MACHINE(obj);
1661 pcms->default_bus_bypass_iommu = value;
1664 static void pc_machine_get_smbios_ep(Object *obj, Visitor *v, const char *name,
1665 void *opaque, Error **errp)
1667 PCMachineState *pcms = PC_MACHINE(obj);
1668 SmbiosEntryPointType smbios_entry_point_type = pcms->smbios_entry_point_type;
1670 visit_type_SmbiosEntryPointType(v, name, &smbios_entry_point_type, errp);
1673 static void pc_machine_set_smbios_ep(Object *obj, Visitor *v, const char *name,
1674 void *opaque, Error **errp)
1676 PCMachineState *pcms = PC_MACHINE(obj);
1678 visit_type_SmbiosEntryPointType(v, name, &pcms->smbios_entry_point_type, errp);
1681 static void pc_machine_get_max_ram_below_4g(Object *obj, Visitor *v,
1682 const char *name, void *opaque,
1683 Error **errp)
1685 PCMachineState *pcms = PC_MACHINE(obj);
1686 uint64_t value = pcms->max_ram_below_4g;
1688 visit_type_size(v, name, &value, errp);
1691 static void pc_machine_set_max_ram_below_4g(Object *obj, Visitor *v,
1692 const char *name, void *opaque,
1693 Error **errp)
1695 PCMachineState *pcms = PC_MACHINE(obj);
1696 uint64_t value;
1698 if (!visit_type_size(v, name, &value, errp)) {
1699 return;
1701 if (value > 4 * GiB) {
1702 error_setg(errp,
1703 "Machine option 'max-ram-below-4g=%"PRIu64
1704 "' expects size less than or equal to 4G", value);
1705 return;
1708 if (value < 1 * MiB) {
1709 warn_report("Only %" PRIu64 " bytes of RAM below the 4GiB boundary,"
1710 "BIOS may not work with less than 1MiB", value);
1713 pcms->max_ram_below_4g = value;
1716 static void pc_machine_get_max_fw_size(Object *obj, Visitor *v,
1717 const char *name, void *opaque,
1718 Error **errp)
1720 PCMachineState *pcms = PC_MACHINE(obj);
1721 uint64_t value = pcms->max_fw_size;
1723 visit_type_size(v, name, &value, errp);
1726 static void pc_machine_set_max_fw_size(Object *obj, Visitor *v,
1727 const char *name, void *opaque,
1728 Error **errp)
1730 PCMachineState *pcms = PC_MACHINE(obj);
1731 Error *error = NULL;
1732 uint64_t value;
1734 visit_type_size(v, name, &value, &error);
1735 if (error) {
1736 error_propagate(errp, error);
1737 return;
1741 * We don't have a theoretically justifiable exact lower bound on the base
1742 * address of any flash mapping. In practice, the IO-APIC MMIO range is
1743 * [0xFEE00000..0xFEE01000] -- see IO_APIC_DEFAULT_ADDRESS --, leaving free
1744 * only 18MB-4KB below 4G. For now, restrict the cumulative mapping to 8MB in
1745 * size.
1747 if (value > 16 * MiB) {
1748 error_setg(errp,
1749 "User specified max allowed firmware size %" PRIu64 " is "
1750 "greater than 16MiB. If combined firwmare size exceeds "
1751 "16MiB the system may not boot, or experience intermittent"
1752 "stability issues.",
1753 value);
1754 return;
1757 pcms->max_fw_size = value;
1761 static void pc_machine_initfn(Object *obj)
1763 PCMachineState *pcms = PC_MACHINE(obj);
1765 #ifdef CONFIG_VMPORT
1766 pcms->vmport = ON_OFF_AUTO_AUTO;
1767 #else
1768 pcms->vmport = ON_OFF_AUTO_OFF;
1769 #endif /* CONFIG_VMPORT */
1770 pcms->max_ram_below_4g = 0; /* use default */
1771 pcms->smbios_entry_point_type = SMBIOS_ENTRY_POINT_TYPE_32;
1773 /* acpi build is enabled by default if machine supports it */
1774 pcms->acpi_build_enabled = PC_MACHINE_GET_CLASS(pcms)->has_acpi_build;
1775 pcms->smbus_enabled = true;
1776 pcms->sata_enabled = true;
1777 pcms->i8042_enabled = true;
1778 pcms->max_fw_size = 8 * MiB;
1779 #ifdef CONFIG_HPET
1780 pcms->hpet_enabled = true;
1781 #endif
1782 pcms->default_bus_bypass_iommu = false;
1784 pc_system_flash_create(pcms);
1785 pcms->pcspk = isa_new(TYPE_PC_SPEAKER);
1786 object_property_add_alias(OBJECT(pcms), "pcspk-audiodev",
1787 OBJECT(pcms->pcspk), "audiodev");
1788 cxl_machine_init(obj, &pcms->cxl_devices_state);
1791 static void pc_machine_reset(MachineState *machine)
1793 CPUState *cs;
1794 X86CPU *cpu;
1796 qemu_devices_reset();
1798 /* Reset APIC after devices have been reset to cancel
1799 * any changes that qemu_devices_reset() might have done.
1801 CPU_FOREACH(cs) {
1802 cpu = X86_CPU(cs);
1804 if (cpu->apic_state) {
1805 device_legacy_reset(cpu->apic_state);
1810 static void pc_machine_wakeup(MachineState *machine)
1812 cpu_synchronize_all_states();
1813 pc_machine_reset(machine);
1814 cpu_synchronize_all_post_reset();
1817 static bool pc_hotplug_allowed(MachineState *ms, DeviceState *dev, Error **errp)
1819 X86IOMMUState *iommu = x86_iommu_get_default();
1820 IntelIOMMUState *intel_iommu;
1822 if (iommu &&
1823 object_dynamic_cast((Object *)iommu, TYPE_INTEL_IOMMU_DEVICE) &&
1824 object_dynamic_cast((Object *)dev, "vfio-pci")) {
1825 intel_iommu = INTEL_IOMMU_DEVICE(iommu);
1826 if (!intel_iommu->caching_mode) {
1827 error_setg(errp, "Device assignment is not allowed without "
1828 "enabling caching-mode=on for Intel IOMMU.");
1829 return false;
1833 return true;
1836 static void pc_machine_class_init(ObjectClass *oc, void *data)
1838 MachineClass *mc = MACHINE_CLASS(oc);
1839 PCMachineClass *pcmc = PC_MACHINE_CLASS(oc);
1840 HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(oc);
1842 pcmc->pci_enabled = true;
1843 pcmc->has_acpi_build = true;
1844 pcmc->rsdp_in_ram = true;
1845 pcmc->smbios_defaults = true;
1846 pcmc->smbios_uuid_encoded = true;
1847 pcmc->gigabyte_align = true;
1848 pcmc->has_reserved_memory = true;
1849 pcmc->kvmclock_enabled = true;
1850 pcmc->enforce_aligned_dimm = true;
1851 /* BIOS ACPI tables: 128K. Other BIOS datastructures: less than 4K reported
1852 * to be used at the moment, 32K should be enough for a while. */
1853 pcmc->acpi_data_size = 0x20000 + 0x8000;
1854 pcmc->pvh_enabled = true;
1855 pcmc->kvmclock_create_always = true;
1856 assert(!mc->get_hotplug_handler);
1857 mc->get_hotplug_handler = pc_get_hotplug_handler;
1858 mc->hotplug_allowed = pc_hotplug_allowed;
1859 mc->cpu_index_to_instance_props = x86_cpu_index_to_props;
1860 mc->get_default_cpu_node_id = x86_get_default_cpu_node_id;
1861 mc->possible_cpu_arch_ids = x86_possible_cpu_arch_ids;
1862 mc->auto_enable_numa_with_memhp = true;
1863 mc->auto_enable_numa_with_memdev = true;
1864 mc->has_hotpluggable_cpus = true;
1865 mc->default_boot_order = "cad";
1866 mc->block_default_type = IF_IDE;
1867 mc->max_cpus = 255;
1868 mc->reset = pc_machine_reset;
1869 mc->wakeup = pc_machine_wakeup;
1870 hc->pre_plug = pc_machine_device_pre_plug_cb;
1871 hc->plug = pc_machine_device_plug_cb;
1872 hc->unplug_request = pc_machine_device_unplug_request_cb;
1873 hc->unplug = pc_machine_device_unplug_cb;
1874 mc->default_cpu_type = TARGET_DEFAULT_CPU_TYPE;
1875 mc->nvdimm_supported = true;
1876 mc->smp_props.dies_supported = true;
1877 mc->default_ram_id = "pc.ram";
1879 object_class_property_add(oc, PC_MACHINE_MAX_RAM_BELOW_4G, "size",
1880 pc_machine_get_max_ram_below_4g, pc_machine_set_max_ram_below_4g,
1881 NULL, NULL);
1882 object_class_property_set_description(oc, PC_MACHINE_MAX_RAM_BELOW_4G,
1883 "Maximum ram below the 4G boundary (32bit boundary)");
1885 object_class_property_add(oc, PC_MACHINE_DEVMEM_REGION_SIZE, "int",
1886 pc_machine_get_device_memory_region_size, NULL,
1887 NULL, NULL);
1889 object_class_property_add(oc, PC_MACHINE_VMPORT, "OnOffAuto",
1890 pc_machine_get_vmport, pc_machine_set_vmport,
1891 NULL, NULL);
1892 object_class_property_set_description(oc, PC_MACHINE_VMPORT,
1893 "Enable vmport (pc & q35)");
1895 object_class_property_add_bool(oc, PC_MACHINE_SMBUS,
1896 pc_machine_get_smbus, pc_machine_set_smbus);
1897 object_class_property_set_description(oc, PC_MACHINE_SMBUS,
1898 "Enable/disable system management bus");
1900 object_class_property_add_bool(oc, PC_MACHINE_SATA,
1901 pc_machine_get_sata, pc_machine_set_sata);
1902 object_class_property_set_description(oc, PC_MACHINE_SATA,
1903 "Enable/disable Serial ATA bus");
1905 object_class_property_add_bool(oc, "hpet",
1906 pc_machine_get_hpet, pc_machine_set_hpet);
1907 object_class_property_set_description(oc, "hpet",
1908 "Enable/disable high precision event timer emulation");
1910 object_class_property_add_bool(oc, PC_MACHINE_I8042,
1911 pc_machine_get_i8042, pc_machine_set_i8042);
1913 object_class_property_add_bool(oc, "default-bus-bypass-iommu",
1914 pc_machine_get_default_bus_bypass_iommu,
1915 pc_machine_set_default_bus_bypass_iommu);
1917 object_class_property_add(oc, PC_MACHINE_MAX_FW_SIZE, "size",
1918 pc_machine_get_max_fw_size, pc_machine_set_max_fw_size,
1919 NULL, NULL);
1920 object_class_property_set_description(oc, PC_MACHINE_MAX_FW_SIZE,
1921 "Maximum combined firmware size");
1923 object_class_property_add(oc, PC_MACHINE_SMBIOS_EP, "str",
1924 pc_machine_get_smbios_ep, pc_machine_set_smbios_ep,
1925 NULL, NULL);
1926 object_class_property_set_description(oc, PC_MACHINE_SMBIOS_EP,
1927 "SMBIOS Entry Point type [32, 64]");
1930 static const TypeInfo pc_machine_info = {
1931 .name = TYPE_PC_MACHINE,
1932 .parent = TYPE_X86_MACHINE,
1933 .abstract = true,
1934 .instance_size = sizeof(PCMachineState),
1935 .instance_init = pc_machine_initfn,
1936 .class_size = sizeof(PCMachineClass),
1937 .class_init = pc_machine_class_init,
1938 .interfaces = (InterfaceInfo[]) {
1939 { TYPE_HOTPLUG_HANDLER },
1944 static void pc_machine_register_types(void)
1946 type_register_static(&pc_machine_info);
1949 type_init(pc_machine_register_types)