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