1 /* 2 * Copyright (c) 2003-2004 Fabrice Bellard 3 * Copyright (c) 2019 Red Hat, Inc. 4 * 5 * Permission is hereby granted, free of charge, to any person obtaining a copy 6 * of this software and associated documentation files (the "Software"), to deal 7 * in the Software without restriction, including without limitation the rights 8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 * copies of the Software, and to permit persons to whom the Software is 10 * furnished to do so, subject to the following conditions: 11 * 12 * The above copyright notice and this permission notice shall be included in 13 * all copies or substantial portions of the Software. 14 * 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 * THE SOFTWARE. 22 */ 23 #include "qemu/osdep.h" 24 #include "qemu/error-report.h" 25 #include "qemu/option.h" 26 #include "qemu/cutils.h" 27 #include "qemu/units.h" 28 #include "qemu-common.h" 29 #include "qapi/error.h" 30 #include "qapi/qmp/qerror.h" 31 #include "qapi/qapi-visit-common.h" 32 #include "qapi/visitor.h" 33 #include "sysemu/qtest.h" 34 #include "sysemu/numa.h" 35 #include "sysemu/replay.h" 36 #include "sysemu/sysemu.h" 37 #include "trace.h" 38 39 #include "hw/i386/x86.h" 40 #include "target/i386/cpu.h" 41 #include "hw/i386/topology.h" 42 #include "hw/i386/fw_cfg.h" 43 #include "hw/intc/i8259.h" 44 45 #include "hw/acpi/cpu_hotplug.h" 46 #include "hw/irq.h" 47 #include "hw/nmi.h" 48 #include "hw/loader.h" 49 #include "multiboot.h" 50 #include "elf.h" 51 #include "standard-headers/asm-x86/bootparam.h" 52 #include "config-devices.h" 53 #include "kvm_i386.h" 54 55 #define BIOS_FILENAME "bios.bin" 56 57 /* Physical Address of PVH entry point read from kernel ELF NOTE */ 58 static size_t pvh_start_addr; 59 60 inline void init_topo_info(X86CPUTopoInfo *topo_info, 61 const X86MachineState *x86ms) 62 { 63 MachineState *ms = MACHINE(x86ms); 64 65 topo_info->nodes_per_pkg = ms->numa_state->num_nodes / ms->smp.sockets; 66 topo_info->dies_per_pkg = x86ms->smp_dies; 67 topo_info->cores_per_die = ms->smp.cores; 68 topo_info->threads_per_core = ms->smp.threads; 69 } 70 71 /* 72 * Set up with the new EPYC topology handlers 73 * 74 * AMD uses different apic id encoding for EPYC based cpus. Override 75 * the default topo handlers with EPYC encoding handlers. 76 */ 77 static void x86_set_epyc_topo_handlers(MachineState *machine) 78 { 79 X86MachineState *x86ms = X86_MACHINE(machine); 80 81 x86ms->apicid_from_cpu_idx = x86_apicid_from_cpu_idx_epyc; 82 x86ms->topo_ids_from_apicid = x86_topo_ids_from_apicid_epyc; 83 x86ms->apicid_from_topo_ids = x86_apicid_from_topo_ids_epyc; 84 x86ms->apicid_pkg_offset = apicid_pkg_offset_epyc; 85 } 86 87 /* 88 * Calculates initial APIC ID for a specific CPU index 89 * 90 * Currently we need to be able to calculate the APIC ID from the CPU index 91 * alone (without requiring a CPU object), as the QEMU<->Seabios interfaces have 92 * no concept of "CPU index", and the NUMA tables on fw_cfg need the APIC ID of 93 * all CPUs up to max_cpus. 94 */ 95 uint32_t x86_cpu_apic_id_from_index(X86MachineState *x86ms, 96 unsigned int cpu_index) 97 { 98 X86MachineClass *x86mc = X86_MACHINE_GET_CLASS(x86ms); 99 X86CPUTopoInfo topo_info; 100 uint32_t correct_id; 101 static bool warned; 102 103 init_topo_info(&topo_info, x86ms); 104 105 correct_id = x86ms->apicid_from_cpu_idx(&topo_info, cpu_index); 106 if (x86mc->compat_apic_id_mode) { 107 if (cpu_index != correct_id && !warned && !qtest_enabled()) { 108 error_report("APIC IDs set in compatibility mode, " 109 "CPU topology won't match the configuration"); 110 warned = true; 111 } 112 return cpu_index; 113 } else { 114 return correct_id; 115 } 116 } 117 118 119 void x86_cpu_new(X86MachineState *x86ms, int64_t apic_id, Error **errp) 120 { 121 Error *local_err = NULL; 122 Object *cpu = object_new(MACHINE(x86ms)->cpu_type); 123 124 object_property_set_uint(cpu, apic_id, "apic-id", &local_err); 125 if (local_err) { 126 goto out; 127 } 128 qdev_realize(DEVICE(cpu), NULL, &local_err); 129 130 out: 131 object_unref(cpu); 132 error_propagate(errp, local_err); 133 } 134 135 void x86_cpus_init(X86MachineState *x86ms, int default_cpu_version) 136 { 137 int i; 138 const CPUArchIdList *possible_cpus; 139 MachineState *ms = MACHINE(x86ms); 140 MachineClass *mc = MACHINE_GET_CLASS(x86ms); 141 142 /* Check for apicid encoding */ 143 if (cpu_x86_use_epyc_apic_id_encoding(ms->cpu_type)) { 144 x86_set_epyc_topo_handlers(ms); 145 } 146 147 x86_cpu_set_default_version(default_cpu_version); 148 149 /* 150 * Calculates the limit to CPU APIC ID values 151 * 152 * Limit for the APIC ID value, so that all 153 * CPU APIC IDs are < x86ms->apic_id_limit. 154 * 155 * This is used for FW_CFG_MAX_CPUS. See comments on fw_cfg_arch_create(). 156 */ 157 x86ms->apic_id_limit = x86_cpu_apic_id_from_index(x86ms, 158 ms->smp.max_cpus - 1) + 1; 159 possible_cpus = mc->possible_cpu_arch_ids(ms); 160 161 for (i = 0; i < ms->possible_cpus->len; i++) { 162 ms->possible_cpus->cpus[i].arch_id = 163 x86_cpu_apic_id_from_index(x86ms, i); 164 } 165 166 for (i = 0; i < ms->smp.cpus; i++) { 167 x86_cpu_new(x86ms, possible_cpus->cpus[i].arch_id, &error_fatal); 168 } 169 } 170 171 CpuInstanceProperties 172 x86_cpu_index_to_props(MachineState *ms, unsigned cpu_index) 173 { 174 MachineClass *mc = MACHINE_GET_CLASS(ms); 175 const CPUArchIdList *possible_cpus = mc->possible_cpu_arch_ids(ms); 176 177 assert(cpu_index < possible_cpus->len); 178 return possible_cpus->cpus[cpu_index].props; 179 } 180 181 int64_t x86_get_default_cpu_node_id(const MachineState *ms, int idx) 182 { 183 X86CPUTopoIDs topo_ids; 184 X86MachineState *x86ms = X86_MACHINE(ms); 185 X86CPUTopoInfo topo_info; 186 187 init_topo_info(&topo_info, x86ms); 188 189 assert(idx < ms->possible_cpus->len); 190 x86_topo_ids_from_idx(&topo_info, idx, &topo_ids); 191 return topo_ids.pkg_id % ms->numa_state->num_nodes; 192 } 193 194 const CPUArchIdList *x86_possible_cpu_arch_ids(MachineState *ms) 195 { 196 X86MachineState *x86ms = X86_MACHINE(ms); 197 unsigned int max_cpus = ms->smp.max_cpus; 198 X86CPUTopoInfo topo_info; 199 int i; 200 201 if (ms->possible_cpus) { 202 /* 203 * make sure that max_cpus hasn't changed since the first use, i.e. 204 * -smp hasn't been parsed after it 205 */ 206 assert(ms->possible_cpus->len == max_cpus); 207 return ms->possible_cpus; 208 } 209 210 ms->possible_cpus = g_malloc0(sizeof(CPUArchIdList) + 211 sizeof(CPUArchId) * max_cpus); 212 ms->possible_cpus->len = max_cpus; 213 214 init_topo_info(&topo_info, x86ms); 215 216 for (i = 0; i < ms->possible_cpus->len; i++) { 217 X86CPUTopoIDs topo_ids; 218 219 ms->possible_cpus->cpus[i].type = ms->cpu_type; 220 ms->possible_cpus->cpus[i].vcpus_count = 1; 221 x86_topo_ids_from_idx(&topo_info, i, &topo_ids); 222 ms->possible_cpus->cpus[i].props.has_socket_id = true; 223 ms->possible_cpus->cpus[i].props.socket_id = topo_ids.pkg_id; 224 if (x86ms->smp_dies > 1) { 225 ms->possible_cpus->cpus[i].props.has_die_id = true; 226 ms->possible_cpus->cpus[i].props.die_id = topo_ids.die_id; 227 } 228 ms->possible_cpus->cpus[i].props.has_core_id = true; 229 ms->possible_cpus->cpus[i].props.core_id = topo_ids.core_id; 230 ms->possible_cpus->cpus[i].props.has_thread_id = true; 231 ms->possible_cpus->cpus[i].props.thread_id = topo_ids.smt_id; 232 } 233 return ms->possible_cpus; 234 } 235 236 static void x86_nmi(NMIState *n, int cpu_index, Error **errp) 237 { 238 /* cpu index isn't used */ 239 CPUState *cs; 240 241 CPU_FOREACH(cs) { 242 X86CPU *cpu = X86_CPU(cs); 243 244 if (!cpu->apic_state) { 245 cpu_interrupt(cs, CPU_INTERRUPT_NMI); 246 } else { 247 apic_deliver_nmi(cpu->apic_state); 248 } 249 } 250 } 251 252 static long get_file_size(FILE *f) 253 { 254 long where, size; 255 256 /* XXX: on Unix systems, using fstat() probably makes more sense */ 257 258 where = ftell(f); 259 fseek(f, 0, SEEK_END); 260 size = ftell(f); 261 fseek(f, where, SEEK_SET); 262 263 return size; 264 } 265 266 /* TSC handling */ 267 uint64_t cpu_get_tsc(CPUX86State *env) 268 { 269 return cpu_get_ticks(); 270 } 271 272 /* IRQ handling */ 273 static void pic_irq_request(void *opaque, int irq, int level) 274 { 275 CPUState *cs = first_cpu; 276 X86CPU *cpu = X86_CPU(cs); 277 278 trace_x86_pic_interrupt(irq, level); 279 if (cpu->apic_state && !kvm_irqchip_in_kernel()) { 280 CPU_FOREACH(cs) { 281 cpu = X86_CPU(cs); 282 if (apic_accept_pic_intr(cpu->apic_state)) { 283 apic_deliver_pic_intr(cpu->apic_state, level); 284 } 285 } 286 } else { 287 if (level) { 288 cpu_interrupt(cs, CPU_INTERRUPT_HARD); 289 } else { 290 cpu_reset_interrupt(cs, CPU_INTERRUPT_HARD); 291 } 292 } 293 } 294 295 qemu_irq x86_allocate_cpu_irq(void) 296 { 297 return qemu_allocate_irq(pic_irq_request, NULL, 0); 298 } 299 300 int cpu_get_pic_interrupt(CPUX86State *env) 301 { 302 X86CPU *cpu = env_archcpu(env); 303 int intno; 304 305 if (!kvm_irqchip_in_kernel()) { 306 intno = apic_get_interrupt(cpu->apic_state); 307 if (intno >= 0) { 308 return intno; 309 } 310 /* read the irq from the PIC */ 311 if (!apic_accept_pic_intr(cpu->apic_state)) { 312 return -1; 313 } 314 } 315 316 intno = pic_read_irq(isa_pic); 317 return intno; 318 } 319 320 DeviceState *cpu_get_current_apic(void) 321 { 322 if (current_cpu) { 323 X86CPU *cpu = X86_CPU(current_cpu); 324 return cpu->apic_state; 325 } else { 326 return NULL; 327 } 328 } 329 330 void gsi_handler(void *opaque, int n, int level) 331 { 332 GSIState *s = opaque; 333 334 trace_x86_gsi_interrupt(n, level); 335 if (n < ISA_NUM_IRQS) { 336 /* Under KVM, Kernel will forward to both PIC and IOAPIC */ 337 qemu_set_irq(s->i8259_irq[n], level); 338 } 339 qemu_set_irq(s->ioapic_irq[n], level); 340 } 341 342 void ioapic_init_gsi(GSIState *gsi_state, const char *parent_name) 343 { 344 DeviceState *dev; 345 SysBusDevice *d; 346 unsigned int i; 347 348 assert(parent_name); 349 if (kvm_ioapic_in_kernel()) { 350 dev = qdev_new(TYPE_KVM_IOAPIC); 351 } else { 352 dev = qdev_new(TYPE_IOAPIC); 353 } 354 object_property_add_child(object_resolve_path(parent_name, NULL), 355 "ioapic", OBJECT(dev)); 356 d = SYS_BUS_DEVICE(dev); 357 sysbus_realize_and_unref(d, &error_fatal); 358 sysbus_mmio_map(d, 0, IO_APIC_DEFAULT_ADDRESS); 359 360 for (i = 0; i < IOAPIC_NUM_PINS; i++) { 361 gsi_state->ioapic_irq[i] = qdev_get_gpio_in(dev, i); 362 } 363 } 364 365 struct setup_data { 366 uint64_t next; 367 uint32_t type; 368 uint32_t len; 369 uint8_t data[]; 370 } __attribute__((packed)); 371 372 373 /* 374 * The entry point into the kernel for PVH boot is different from 375 * the native entry point. The PVH entry is defined by the x86/HVM 376 * direct boot ABI and is available in an ELFNOTE in the kernel binary. 377 * 378 * This function is passed to load_elf() when it is called from 379 * load_elfboot() which then additionally checks for an ELF Note of 380 * type XEN_ELFNOTE_PHYS32_ENTRY and passes it to this function to 381 * parse the PVH entry address from the ELF Note. 382 * 383 * Due to trickery in elf_opts.h, load_elf() is actually available as 384 * load_elf32() or load_elf64() and this routine needs to be able 385 * to deal with being called as 32 or 64 bit. 386 * 387 * The address of the PVH entry point is saved to the 'pvh_start_addr' 388 * global variable. (although the entry point is 32-bit, the kernel 389 * binary can be either 32-bit or 64-bit). 390 */ 391 static uint64_t read_pvh_start_addr(void *arg1, void *arg2, bool is64) 392 { 393 size_t *elf_note_data_addr; 394 395 /* Check if ELF Note header passed in is valid */ 396 if (arg1 == NULL) { 397 return 0; 398 } 399 400 if (is64) { 401 struct elf64_note *nhdr64 = (struct elf64_note *)arg1; 402 uint64_t nhdr_size64 = sizeof(struct elf64_note); 403 uint64_t phdr_align = *(uint64_t *)arg2; 404 uint64_t nhdr_namesz = nhdr64->n_namesz; 405 406 elf_note_data_addr = 407 ((void *)nhdr64) + nhdr_size64 + 408 QEMU_ALIGN_UP(nhdr_namesz, phdr_align); 409 } else { 410 struct elf32_note *nhdr32 = (struct elf32_note *)arg1; 411 uint32_t nhdr_size32 = sizeof(struct elf32_note); 412 uint32_t phdr_align = *(uint32_t *)arg2; 413 uint32_t nhdr_namesz = nhdr32->n_namesz; 414 415 elf_note_data_addr = 416 ((void *)nhdr32) + nhdr_size32 + 417 QEMU_ALIGN_UP(nhdr_namesz, phdr_align); 418 } 419 420 pvh_start_addr = *elf_note_data_addr; 421 422 return pvh_start_addr; 423 } 424 425 static bool load_elfboot(const char *kernel_filename, 426 int kernel_file_size, 427 uint8_t *header, 428 size_t pvh_xen_start_addr, 429 FWCfgState *fw_cfg) 430 { 431 uint32_t flags = 0; 432 uint32_t mh_load_addr = 0; 433 uint32_t elf_kernel_size = 0; 434 uint64_t elf_entry; 435 uint64_t elf_low, elf_high; 436 int kernel_size; 437 438 if (ldl_p(header) != 0x464c457f) { 439 return false; /* no elfboot */ 440 } 441 442 bool elf_is64 = header[EI_CLASS] == ELFCLASS64; 443 flags = elf_is64 ? 444 ((Elf64_Ehdr *)header)->e_flags : ((Elf32_Ehdr *)header)->e_flags; 445 446 if (flags & 0x00010004) { /* LOAD_ELF_HEADER_HAS_ADDR */ 447 error_report("elfboot unsupported flags = %x", flags); 448 exit(1); 449 } 450 451 uint64_t elf_note_type = XEN_ELFNOTE_PHYS32_ENTRY; 452 kernel_size = load_elf(kernel_filename, read_pvh_start_addr, 453 NULL, &elf_note_type, &elf_entry, 454 &elf_low, &elf_high, NULL, 0, I386_ELF_MACHINE, 455 0, 0); 456 457 if (kernel_size < 0) { 458 error_report("Error while loading elf kernel"); 459 exit(1); 460 } 461 mh_load_addr = elf_low; 462 elf_kernel_size = elf_high - elf_low; 463 464 if (pvh_start_addr == 0) { 465 error_report("Error loading uncompressed kernel without PVH ELF Note"); 466 exit(1); 467 } 468 fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_ENTRY, pvh_start_addr); 469 fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_ADDR, mh_load_addr); 470 fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_SIZE, elf_kernel_size); 471 472 return true; 473 } 474 475 void x86_load_linux(X86MachineState *x86ms, 476 FWCfgState *fw_cfg, 477 int acpi_data_size, 478 bool pvh_enabled, 479 bool linuxboot_dma_enabled) 480 { 481 uint16_t protocol; 482 int setup_size, kernel_size, cmdline_size; 483 int dtb_size, setup_data_offset; 484 uint32_t initrd_max; 485 uint8_t header[8192], *setup, *kernel; 486 hwaddr real_addr, prot_addr, cmdline_addr, initrd_addr = 0; 487 FILE *f; 488 char *vmode; 489 MachineState *machine = MACHINE(x86ms); 490 struct setup_data *setup_data; 491 const char *kernel_filename = machine->kernel_filename; 492 const char *initrd_filename = machine->initrd_filename; 493 const char *dtb_filename = machine->dtb; 494 const char *kernel_cmdline = machine->kernel_cmdline; 495 496 /* Align to 16 bytes as a paranoia measure */ 497 cmdline_size = (strlen(kernel_cmdline) + 16) & ~15; 498 499 /* load the kernel header */ 500 f = fopen(kernel_filename, "rb"); 501 if (!f) { 502 fprintf(stderr, "qemu: could not open kernel file '%s': %s\n", 503 kernel_filename, strerror(errno)); 504 exit(1); 505 } 506 507 kernel_size = get_file_size(f); 508 if (!kernel_size || 509 fread(header, 1, MIN(ARRAY_SIZE(header), kernel_size), f) != 510 MIN(ARRAY_SIZE(header), kernel_size)) { 511 fprintf(stderr, "qemu: could not load kernel '%s': %s\n", 512 kernel_filename, strerror(errno)); 513 exit(1); 514 } 515 516 /* kernel protocol version */ 517 if (ldl_p(header + 0x202) == 0x53726448) { 518 protocol = lduw_p(header + 0x206); 519 } else { 520 /* 521 * This could be a multiboot kernel. If it is, let's stop treating it 522 * like a Linux kernel. 523 * Note: some multiboot images could be in the ELF format (the same of 524 * PVH), so we try multiboot first since we check the multiboot magic 525 * header before to load it. 526 */ 527 if (load_multiboot(fw_cfg, f, kernel_filename, initrd_filename, 528 kernel_cmdline, kernel_size, header)) { 529 return; 530 } 531 /* 532 * Check if the file is an uncompressed kernel file (ELF) and load it, 533 * saving the PVH entry point used by the x86/HVM direct boot ABI. 534 * If load_elfboot() is successful, populate the fw_cfg info. 535 */ 536 if (pvh_enabled && 537 load_elfboot(kernel_filename, kernel_size, 538 header, pvh_start_addr, fw_cfg)) { 539 fclose(f); 540 541 fw_cfg_add_i32(fw_cfg, FW_CFG_CMDLINE_SIZE, 542 strlen(kernel_cmdline) + 1); 543 fw_cfg_add_string(fw_cfg, FW_CFG_CMDLINE_DATA, kernel_cmdline); 544 545 fw_cfg_add_i32(fw_cfg, FW_CFG_SETUP_SIZE, sizeof(header)); 546 fw_cfg_add_bytes(fw_cfg, FW_CFG_SETUP_DATA, 547 header, sizeof(header)); 548 549 /* load initrd */ 550 if (initrd_filename) { 551 GMappedFile *mapped_file; 552 gsize initrd_size; 553 gchar *initrd_data; 554 GError *gerr = NULL; 555 556 mapped_file = g_mapped_file_new(initrd_filename, false, &gerr); 557 if (!mapped_file) { 558 fprintf(stderr, "qemu: error reading initrd %s: %s\n", 559 initrd_filename, gerr->message); 560 exit(1); 561 } 562 x86ms->initrd_mapped_file = mapped_file; 563 564 initrd_data = g_mapped_file_get_contents(mapped_file); 565 initrd_size = g_mapped_file_get_length(mapped_file); 566 initrd_max = x86ms->below_4g_mem_size - acpi_data_size - 1; 567 if (initrd_size >= initrd_max) { 568 fprintf(stderr, "qemu: initrd is too large, cannot support." 569 "(max: %"PRIu32", need %"PRId64")\n", 570 initrd_max, (uint64_t)initrd_size); 571 exit(1); 572 } 573 574 initrd_addr = (initrd_max - initrd_size) & ~4095; 575 576 fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_ADDR, initrd_addr); 577 fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_SIZE, initrd_size); 578 fw_cfg_add_bytes(fw_cfg, FW_CFG_INITRD_DATA, initrd_data, 579 initrd_size); 580 } 581 582 option_rom[nb_option_roms].bootindex = 0; 583 option_rom[nb_option_roms].name = "pvh.bin"; 584 nb_option_roms++; 585 586 return; 587 } 588 protocol = 0; 589 } 590 591 if (protocol < 0x200 || !(header[0x211] & 0x01)) { 592 /* Low kernel */ 593 real_addr = 0x90000; 594 cmdline_addr = 0x9a000 - cmdline_size; 595 prot_addr = 0x10000; 596 } else if (protocol < 0x202) { 597 /* High but ancient kernel */ 598 real_addr = 0x90000; 599 cmdline_addr = 0x9a000 - cmdline_size; 600 prot_addr = 0x100000; 601 } else { 602 /* High and recent kernel */ 603 real_addr = 0x10000; 604 cmdline_addr = 0x20000; 605 prot_addr = 0x100000; 606 } 607 608 /* highest address for loading the initrd */ 609 if (protocol >= 0x20c && 610 lduw_p(header + 0x236) & XLF_CAN_BE_LOADED_ABOVE_4G) { 611 /* 612 * Linux has supported initrd up to 4 GB for a very long time (2007, 613 * long before XLF_CAN_BE_LOADED_ABOVE_4G which was added in 2013), 614 * though it only sets initrd_max to 2 GB to "work around bootloader 615 * bugs". Luckily, QEMU firmware(which does something like bootloader) 616 * has supported this. 617 * 618 * It's believed that if XLF_CAN_BE_LOADED_ABOVE_4G is set, initrd can 619 * be loaded into any address. 620 * 621 * In addition, initrd_max is uint32_t simply because QEMU doesn't 622 * support the 64-bit boot protocol (specifically the ext_ramdisk_image 623 * field). 624 * 625 * Therefore here just limit initrd_max to UINT32_MAX simply as well. 626 */ 627 initrd_max = UINT32_MAX; 628 } else if (protocol >= 0x203) { 629 initrd_max = ldl_p(header + 0x22c); 630 } else { 631 initrd_max = 0x37ffffff; 632 } 633 634 if (initrd_max >= x86ms->below_4g_mem_size - acpi_data_size) { 635 initrd_max = x86ms->below_4g_mem_size - acpi_data_size - 1; 636 } 637 638 fw_cfg_add_i32(fw_cfg, FW_CFG_CMDLINE_ADDR, cmdline_addr); 639 fw_cfg_add_i32(fw_cfg, FW_CFG_CMDLINE_SIZE, strlen(kernel_cmdline) + 1); 640 fw_cfg_add_string(fw_cfg, FW_CFG_CMDLINE_DATA, kernel_cmdline); 641 642 if (protocol >= 0x202) { 643 stl_p(header + 0x228, cmdline_addr); 644 } else { 645 stw_p(header + 0x20, 0xA33F); 646 stw_p(header + 0x22, cmdline_addr - real_addr); 647 } 648 649 /* handle vga= parameter */ 650 vmode = strstr(kernel_cmdline, "vga="); 651 if (vmode) { 652 unsigned int video_mode; 653 const char *end; 654 int ret; 655 /* skip "vga=" */ 656 vmode += 4; 657 if (!strncmp(vmode, "normal", 6)) { 658 video_mode = 0xffff; 659 } else if (!strncmp(vmode, "ext", 3)) { 660 video_mode = 0xfffe; 661 } else if (!strncmp(vmode, "ask", 3)) { 662 video_mode = 0xfffd; 663 } else { 664 ret = qemu_strtoui(vmode, &end, 0, &video_mode); 665 if (ret != 0 || (*end && *end != ' ')) { 666 fprintf(stderr, "qemu: invalid 'vga=' kernel parameter.\n"); 667 exit(1); 668 } 669 } 670 stw_p(header + 0x1fa, video_mode); 671 } 672 673 /* loader type */ 674 /* 675 * High nybble = B reserved for QEMU; low nybble is revision number. 676 * If this code is substantially changed, you may want to consider 677 * incrementing the revision. 678 */ 679 if (protocol >= 0x200) { 680 header[0x210] = 0xB0; 681 } 682 /* heap */ 683 if (protocol >= 0x201) { 684 header[0x211] |= 0x80; /* CAN_USE_HEAP */ 685 stw_p(header + 0x224, cmdline_addr - real_addr - 0x200); 686 } 687 688 /* load initrd */ 689 if (initrd_filename) { 690 GMappedFile *mapped_file; 691 gsize initrd_size; 692 gchar *initrd_data; 693 GError *gerr = NULL; 694 695 if (protocol < 0x200) { 696 fprintf(stderr, "qemu: linux kernel too old to load a ram disk\n"); 697 exit(1); 698 } 699 700 mapped_file = g_mapped_file_new(initrd_filename, false, &gerr); 701 if (!mapped_file) { 702 fprintf(stderr, "qemu: error reading initrd %s: %s\n", 703 initrd_filename, gerr->message); 704 exit(1); 705 } 706 x86ms->initrd_mapped_file = mapped_file; 707 708 initrd_data = g_mapped_file_get_contents(mapped_file); 709 initrd_size = g_mapped_file_get_length(mapped_file); 710 if (initrd_size >= initrd_max) { 711 fprintf(stderr, "qemu: initrd is too large, cannot support." 712 "(max: %"PRIu32", need %"PRId64")\n", 713 initrd_max, (uint64_t)initrd_size); 714 exit(1); 715 } 716 717 initrd_addr = (initrd_max - initrd_size) & ~4095; 718 719 fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_ADDR, initrd_addr); 720 fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_SIZE, initrd_size); 721 fw_cfg_add_bytes(fw_cfg, FW_CFG_INITRD_DATA, initrd_data, initrd_size); 722 723 stl_p(header + 0x218, initrd_addr); 724 stl_p(header + 0x21c, initrd_size); 725 } 726 727 /* load kernel and setup */ 728 setup_size = header[0x1f1]; 729 if (setup_size == 0) { 730 setup_size = 4; 731 } 732 setup_size = (setup_size + 1) * 512; 733 if (setup_size > kernel_size) { 734 fprintf(stderr, "qemu: invalid kernel header\n"); 735 exit(1); 736 } 737 kernel_size -= setup_size; 738 739 setup = g_malloc(setup_size); 740 kernel = g_malloc(kernel_size); 741 fseek(f, 0, SEEK_SET); 742 if (fread(setup, 1, setup_size, f) != setup_size) { 743 fprintf(stderr, "fread() failed\n"); 744 exit(1); 745 } 746 if (fread(kernel, 1, kernel_size, f) != kernel_size) { 747 fprintf(stderr, "fread() failed\n"); 748 exit(1); 749 } 750 fclose(f); 751 752 /* append dtb to kernel */ 753 if (dtb_filename) { 754 if (protocol < 0x209) { 755 fprintf(stderr, "qemu: Linux kernel too old to load a dtb\n"); 756 exit(1); 757 } 758 759 dtb_size = get_image_size(dtb_filename); 760 if (dtb_size <= 0) { 761 fprintf(stderr, "qemu: error reading dtb %s: %s\n", 762 dtb_filename, strerror(errno)); 763 exit(1); 764 } 765 766 setup_data_offset = QEMU_ALIGN_UP(kernel_size, 16); 767 kernel_size = setup_data_offset + sizeof(struct setup_data) + dtb_size; 768 kernel = g_realloc(kernel, kernel_size); 769 770 stq_p(header + 0x250, prot_addr + setup_data_offset); 771 772 setup_data = (struct setup_data *)(kernel + setup_data_offset); 773 setup_data->next = 0; 774 setup_data->type = cpu_to_le32(SETUP_DTB); 775 setup_data->len = cpu_to_le32(dtb_size); 776 777 load_image_size(dtb_filename, setup_data->data, dtb_size); 778 } 779 780 memcpy(setup, header, MIN(sizeof(header), setup_size)); 781 782 fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_ADDR, prot_addr); 783 fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_SIZE, kernel_size); 784 fw_cfg_add_bytes(fw_cfg, FW_CFG_KERNEL_DATA, kernel, kernel_size); 785 786 fw_cfg_add_i32(fw_cfg, FW_CFG_SETUP_ADDR, real_addr); 787 fw_cfg_add_i32(fw_cfg, FW_CFG_SETUP_SIZE, setup_size); 788 fw_cfg_add_bytes(fw_cfg, FW_CFG_SETUP_DATA, setup, setup_size); 789 790 option_rom[nb_option_roms].bootindex = 0; 791 option_rom[nb_option_roms].name = "linuxboot.bin"; 792 if (linuxboot_dma_enabled && fw_cfg_dma_enabled(fw_cfg)) { 793 option_rom[nb_option_roms].name = "linuxboot_dma.bin"; 794 } 795 nb_option_roms++; 796 } 797 798 void x86_bios_rom_init(MemoryRegion *rom_memory, bool isapc_ram_fw) 799 { 800 char *filename; 801 MemoryRegion *bios, *isa_bios; 802 int bios_size, isa_bios_size; 803 int ret; 804 805 /* BIOS load */ 806 if (bios_name == NULL) { 807 bios_name = BIOS_FILENAME; 808 } 809 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name); 810 if (filename) { 811 bios_size = get_image_size(filename); 812 } else { 813 bios_size = -1; 814 } 815 if (bios_size <= 0 || 816 (bios_size % 65536) != 0) { 817 goto bios_error; 818 } 819 bios = g_malloc(sizeof(*bios)); 820 memory_region_init_ram(bios, NULL, "pc.bios", bios_size, &error_fatal); 821 if (!isapc_ram_fw) { 822 memory_region_set_readonly(bios, true); 823 } 824 ret = rom_add_file_fixed(bios_name, (uint32_t)(-bios_size), -1); 825 if (ret != 0) { 826 bios_error: 827 fprintf(stderr, "qemu: could not load PC BIOS '%s'\n", bios_name); 828 exit(1); 829 } 830 g_free(filename); 831 832 /* map the last 128KB of the BIOS in ISA space */ 833 isa_bios_size = MIN(bios_size, 128 * KiB); 834 isa_bios = g_malloc(sizeof(*isa_bios)); 835 memory_region_init_alias(isa_bios, NULL, "isa-bios", bios, 836 bios_size - isa_bios_size, isa_bios_size); 837 memory_region_add_subregion_overlap(rom_memory, 838 0x100000 - isa_bios_size, 839 isa_bios, 840 1); 841 if (!isapc_ram_fw) { 842 memory_region_set_readonly(isa_bios, true); 843 } 844 845 /* map all the bios at the top of memory */ 846 memory_region_add_subregion(rom_memory, 847 (uint32_t)(-bios_size), 848 bios); 849 } 850 851 bool x86_machine_is_smm_enabled(X86MachineState *x86ms) 852 { 853 bool smm_available = false; 854 855 if (x86ms->smm == ON_OFF_AUTO_OFF) { 856 return false; 857 } 858 859 if (tcg_enabled() || qtest_enabled()) { 860 smm_available = true; 861 } else if (kvm_enabled()) { 862 smm_available = kvm_has_smm(); 863 } 864 865 if (smm_available) { 866 return true; 867 } 868 869 if (x86ms->smm == ON_OFF_AUTO_ON) { 870 error_report("System Management Mode not supported by this hypervisor."); 871 exit(1); 872 } 873 return false; 874 } 875 876 static void x86_machine_get_smm(Object *obj, Visitor *v, const char *name, 877 void *opaque, Error **errp) 878 { 879 X86MachineState *x86ms = X86_MACHINE(obj); 880 OnOffAuto smm = x86ms->smm; 881 882 visit_type_OnOffAuto(v, name, &smm, errp); 883 } 884 885 static void x86_machine_set_smm(Object *obj, Visitor *v, const char *name, 886 void *opaque, Error **errp) 887 { 888 X86MachineState *x86ms = X86_MACHINE(obj); 889 890 visit_type_OnOffAuto(v, name, &x86ms->smm, errp); 891 } 892 893 bool x86_machine_is_acpi_enabled(X86MachineState *x86ms) 894 { 895 if (x86ms->acpi == ON_OFF_AUTO_OFF) { 896 return false; 897 } 898 return true; 899 } 900 901 static void x86_machine_get_acpi(Object *obj, Visitor *v, const char *name, 902 void *opaque, Error **errp) 903 { 904 X86MachineState *x86ms = X86_MACHINE(obj); 905 OnOffAuto acpi = x86ms->acpi; 906 907 visit_type_OnOffAuto(v, name, &acpi, errp); 908 } 909 910 static void x86_machine_set_acpi(Object *obj, Visitor *v, const char *name, 911 void *opaque, Error **errp) 912 { 913 X86MachineState *x86ms = X86_MACHINE(obj); 914 915 visit_type_OnOffAuto(v, name, &x86ms->acpi, errp); 916 } 917 918 static void x86_machine_initfn(Object *obj) 919 { 920 X86MachineState *x86ms = X86_MACHINE(obj); 921 922 x86ms->smm = ON_OFF_AUTO_AUTO; 923 x86ms->acpi = ON_OFF_AUTO_AUTO; 924 x86ms->smp_dies = 1; 925 926 x86ms->apicid_from_cpu_idx = x86_apicid_from_cpu_idx; 927 x86ms->topo_ids_from_apicid = x86_topo_ids_from_apicid; 928 x86ms->apicid_from_topo_ids = x86_apicid_from_topo_ids; 929 x86ms->apicid_pkg_offset = apicid_pkg_offset; 930 } 931 932 static void x86_machine_class_init(ObjectClass *oc, void *data) 933 { 934 MachineClass *mc = MACHINE_CLASS(oc); 935 X86MachineClass *x86mc = X86_MACHINE_CLASS(oc); 936 NMIClass *nc = NMI_CLASS(oc); 937 938 mc->cpu_index_to_instance_props = x86_cpu_index_to_props; 939 mc->get_default_cpu_node_id = x86_get_default_cpu_node_id; 940 mc->possible_cpu_arch_ids = x86_possible_cpu_arch_ids; 941 x86mc->compat_apic_id_mode = false; 942 x86mc->save_tsc_khz = true; 943 nc->nmi_monitor_handler = x86_nmi; 944 945 object_class_property_add(oc, X86_MACHINE_SMM, "OnOffAuto", 946 x86_machine_get_smm, x86_machine_set_smm, 947 NULL, NULL); 948 object_class_property_set_description(oc, X86_MACHINE_SMM, 949 "Enable SMM"); 950 951 object_class_property_add(oc, X86_MACHINE_ACPI, "OnOffAuto", 952 x86_machine_get_acpi, x86_machine_set_acpi, 953 NULL, NULL); 954 object_class_property_set_description(oc, X86_MACHINE_ACPI, 955 "Enable ACPI"); 956 } 957 958 static const TypeInfo x86_machine_info = { 959 .name = TYPE_X86_MACHINE, 960 .parent = TYPE_MACHINE, 961 .abstract = true, 962 .instance_size = sizeof(X86MachineState), 963 .instance_init = x86_machine_initfn, 964 .class_size = sizeof(X86MachineClass), 965 .class_init = x86_machine_class_init, 966 .interfaces = (InterfaceInfo[]) { 967 { TYPE_NMI }, 968 { } 969 }, 970 }; 971 972 static void x86_machine_register_types(void) 973 { 974 type_register_static(&x86_machine_info); 975 } 976 977 type_init(x86_machine_register_types) 978