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