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