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