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