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