1b061f059SPaolo Bonzini /* 2b061f059SPaolo Bonzini * Copyright (c) 2003-2004 Fabrice Bellard 3b061f059SPaolo Bonzini * Copyright (c) 2019, 2024 Red Hat, Inc. 4b061f059SPaolo Bonzini * 5b061f059SPaolo Bonzini * Permission is hereby granted, free of charge, to any person obtaining a copy 6b061f059SPaolo Bonzini * of this software and associated documentation files (the "Software"), to deal 7b061f059SPaolo Bonzini * in the Software without restriction, including without limitation the rights 8b061f059SPaolo Bonzini * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9b061f059SPaolo Bonzini * copies of the Software, and to permit persons to whom the Software is 10b061f059SPaolo Bonzini * furnished to do so, subject to the following conditions: 11b061f059SPaolo Bonzini * 12b061f059SPaolo Bonzini * The above copyright notice and this permission notice shall be included in 13b061f059SPaolo Bonzini * all copies or substantial portions of the Software. 14b061f059SPaolo Bonzini * 15b061f059SPaolo Bonzini * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16b061f059SPaolo Bonzini * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17b061f059SPaolo Bonzini * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18b061f059SPaolo Bonzini * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19b061f059SPaolo Bonzini * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20b061f059SPaolo Bonzini * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21b061f059SPaolo Bonzini * THE SOFTWARE. 22b061f059SPaolo Bonzini */ 23b061f059SPaolo Bonzini #include "qemu/osdep.h" 24b061f059SPaolo Bonzini #include "qemu/error-report.h" 25b061f059SPaolo Bonzini #include "qemu/cutils.h" 26b061f059SPaolo Bonzini #include "qemu/units.h" 27b061f059SPaolo Bonzini #include "qemu/datadir.h" 28b061f059SPaolo Bonzini #include "qapi/error.h" 29b061f059SPaolo Bonzini #include "sysemu/numa.h" 30b061f059SPaolo Bonzini #include "sysemu/sysemu.h" 31b061f059SPaolo Bonzini #include "sysemu/xen.h" 32b061f059SPaolo Bonzini #include "trace.h" 33b061f059SPaolo Bonzini 34b061f059SPaolo Bonzini #include "hw/i386/x86.h" 35b061f059SPaolo Bonzini #include "target/i386/cpu.h" 36b061f059SPaolo Bonzini #include "hw/rtc/mc146818rtc.h" 37b061f059SPaolo Bonzini #include "target/i386/sev.h" 38b061f059SPaolo Bonzini 39b061f059SPaolo Bonzini #include "hw/acpi/cpu_hotplug.h" 40b061f059SPaolo Bonzini #include "hw/irq.h" 41b061f059SPaolo Bonzini #include "hw/loader.h" 42b061f059SPaolo Bonzini #include "multiboot.h" 43b061f059SPaolo Bonzini #include "elf.h" 44b061f059SPaolo Bonzini #include "standard-headers/asm-x86/bootparam.h" 45b061f059SPaolo Bonzini #include CONFIG_DEVICES 46b061f059SPaolo Bonzini #include "kvm/kvm_i386.h" 47b061f059SPaolo Bonzini 48b061f059SPaolo Bonzini #ifdef CONFIG_XEN_EMU 49b061f059SPaolo Bonzini #include "hw/xen/xen.h" 50b061f059SPaolo Bonzini #include "hw/i386/kvm/xen_evtchn.h" 51b061f059SPaolo Bonzini #endif 52b061f059SPaolo Bonzini 53b061f059SPaolo Bonzini /* Physical Address of PVH entry point read from kernel ELF NOTE */ 54b061f059SPaolo Bonzini static size_t pvh_start_addr; 55b061f059SPaolo Bonzini 56b061f059SPaolo Bonzini static void x86_cpu_new(X86MachineState *x86ms, int64_t apic_id, Error **errp) 57b061f059SPaolo Bonzini { 58b061f059SPaolo Bonzini Object *cpu = object_new(MACHINE(x86ms)->cpu_type); 59b061f059SPaolo Bonzini 60b061f059SPaolo Bonzini if (!object_property_set_uint(cpu, "apic-id", apic_id, errp)) { 61b061f059SPaolo Bonzini goto out; 62b061f059SPaolo Bonzini } 63b061f059SPaolo Bonzini qdev_realize(DEVICE(cpu), NULL, errp); 64b061f059SPaolo Bonzini 65b061f059SPaolo Bonzini out: 66b061f059SPaolo Bonzini object_unref(cpu); 67b061f059SPaolo Bonzini } 68b061f059SPaolo Bonzini 69b061f059SPaolo Bonzini void x86_cpus_init(X86MachineState *x86ms, int default_cpu_version) 70b061f059SPaolo Bonzini { 71b061f059SPaolo Bonzini int i; 72b061f059SPaolo Bonzini const CPUArchIdList *possible_cpus; 73b061f059SPaolo Bonzini MachineState *ms = MACHINE(x86ms); 74b061f059SPaolo Bonzini MachineClass *mc = MACHINE_GET_CLASS(x86ms); 75b061f059SPaolo Bonzini 76b061f059SPaolo Bonzini x86_cpu_set_default_version(default_cpu_version); 77b061f059SPaolo Bonzini 78b061f059SPaolo Bonzini /* 79b061f059SPaolo Bonzini * Calculates the limit to CPU APIC ID values 80b061f059SPaolo Bonzini * 81b061f059SPaolo Bonzini * Limit for the APIC ID value, so that all 82b061f059SPaolo Bonzini * CPU APIC IDs are < x86ms->apic_id_limit. 83b061f059SPaolo Bonzini * 84b061f059SPaolo Bonzini * This is used for FW_CFG_MAX_CPUS. See comments on fw_cfg_arch_create(). 85b061f059SPaolo Bonzini */ 86b061f059SPaolo Bonzini x86ms->apic_id_limit = x86_cpu_apic_id_from_index(x86ms, 87b061f059SPaolo Bonzini ms->smp.max_cpus - 1) + 1; 88b061f059SPaolo Bonzini 89b061f059SPaolo Bonzini /* 90b061f059SPaolo Bonzini * Can we support APIC ID 255 or higher? With KVM, that requires 91b061f059SPaolo Bonzini * both in-kernel lapic and X2APIC userspace API. 92b061f059SPaolo Bonzini * 93b061f059SPaolo Bonzini * kvm_enabled() must go first to ensure that kvm_* references are 94b061f059SPaolo Bonzini * not emitted for the linker to consume (kvm_enabled() is 95b061f059SPaolo Bonzini * a literal `0` in configurations where kvm_* aren't defined) 96b061f059SPaolo Bonzini */ 97b061f059SPaolo Bonzini if (kvm_enabled() && x86ms->apic_id_limit > 255 && 98b061f059SPaolo Bonzini kvm_irqchip_in_kernel() && !kvm_enable_x2apic()) { 99b061f059SPaolo Bonzini error_report("current -smp configuration requires kernel " 100b061f059SPaolo Bonzini "irqchip and X2APIC API support."); 101b061f059SPaolo Bonzini exit(EXIT_FAILURE); 102b061f059SPaolo Bonzini } 103b061f059SPaolo Bonzini 104b061f059SPaolo Bonzini if (kvm_enabled()) { 105b061f059SPaolo Bonzini kvm_set_max_apic_id(x86ms->apic_id_limit); 106b061f059SPaolo Bonzini } 107b061f059SPaolo Bonzini 108b061f059SPaolo Bonzini if (!kvm_irqchip_in_kernel()) { 109b061f059SPaolo Bonzini apic_set_max_apic_id(x86ms->apic_id_limit); 110b061f059SPaolo Bonzini } 111b061f059SPaolo Bonzini 112b061f059SPaolo Bonzini possible_cpus = mc->possible_cpu_arch_ids(ms); 113b061f059SPaolo Bonzini for (i = 0; i < ms->smp.cpus; i++) { 114b061f059SPaolo Bonzini x86_cpu_new(x86ms, possible_cpus->cpus[i].arch_id, &error_fatal); 115b061f059SPaolo Bonzini } 116b061f059SPaolo Bonzini } 117b061f059SPaolo Bonzini 118b061f059SPaolo Bonzini void x86_rtc_set_cpus_count(ISADevice *s, uint16_t cpus_count) 119b061f059SPaolo Bonzini { 120b061f059SPaolo Bonzini MC146818RtcState *rtc = MC146818_RTC(s); 121b061f059SPaolo Bonzini 122b061f059SPaolo Bonzini if (cpus_count > 0xff) { 123b061f059SPaolo Bonzini /* 124b061f059SPaolo Bonzini * If the number of CPUs can't be represented in 8 bits, the 125b061f059SPaolo Bonzini * BIOS must use "FW_CFG_NB_CPUS". Set RTC field to 0 just 126b061f059SPaolo Bonzini * to make old BIOSes fail more predictably. 127b061f059SPaolo Bonzini */ 128b061f059SPaolo Bonzini mc146818rtc_set_cmos_data(rtc, 0x5f, 0); 129b061f059SPaolo Bonzini } else { 130b061f059SPaolo Bonzini mc146818rtc_set_cmos_data(rtc, 0x5f, cpus_count - 1); 131b061f059SPaolo Bonzini } 132b061f059SPaolo Bonzini } 133b061f059SPaolo Bonzini 134b061f059SPaolo Bonzini static int x86_apic_cmp(const void *a, const void *b) 135b061f059SPaolo Bonzini { 136b061f059SPaolo Bonzini CPUArchId *apic_a = (CPUArchId *)a; 137b061f059SPaolo Bonzini CPUArchId *apic_b = (CPUArchId *)b; 138b061f059SPaolo Bonzini 139b061f059SPaolo Bonzini return apic_a->arch_id - apic_b->arch_id; 140b061f059SPaolo Bonzini } 141b061f059SPaolo Bonzini 142b061f059SPaolo Bonzini /* 143b061f059SPaolo Bonzini * returns pointer to CPUArchId descriptor that matches CPU's apic_id 144b061f059SPaolo Bonzini * in ms->possible_cpus->cpus, if ms->possible_cpus->cpus has no 145b061f059SPaolo Bonzini * entry corresponding to CPU's apic_id returns NULL. 146b061f059SPaolo Bonzini */ 147b061f059SPaolo Bonzini static CPUArchId *x86_find_cpu_slot(MachineState *ms, uint32_t id, int *idx) 148b061f059SPaolo Bonzini { 149b061f059SPaolo Bonzini CPUArchId apic_id, *found_cpu; 150b061f059SPaolo Bonzini 151b061f059SPaolo Bonzini apic_id.arch_id = id; 152b061f059SPaolo Bonzini found_cpu = bsearch(&apic_id, ms->possible_cpus->cpus, 153b061f059SPaolo Bonzini ms->possible_cpus->len, sizeof(*ms->possible_cpus->cpus), 154b061f059SPaolo Bonzini x86_apic_cmp); 155b061f059SPaolo Bonzini if (found_cpu && idx) { 156b061f059SPaolo Bonzini *idx = found_cpu - ms->possible_cpus->cpus; 157b061f059SPaolo Bonzini } 158b061f059SPaolo Bonzini return found_cpu; 159b061f059SPaolo Bonzini } 160b061f059SPaolo Bonzini 161b061f059SPaolo Bonzini void x86_cpu_plug(HotplugHandler *hotplug_dev, 162b061f059SPaolo Bonzini DeviceState *dev, Error **errp) 163b061f059SPaolo Bonzini { 164b061f059SPaolo Bonzini CPUArchId *found_cpu; 165b061f059SPaolo Bonzini Error *local_err = NULL; 166b061f059SPaolo Bonzini X86CPU *cpu = X86_CPU(dev); 167b061f059SPaolo Bonzini X86MachineState *x86ms = X86_MACHINE(hotplug_dev); 168b061f059SPaolo Bonzini 169b061f059SPaolo Bonzini if (x86ms->acpi_dev) { 170b061f059SPaolo Bonzini hotplug_handler_plug(x86ms->acpi_dev, dev, &local_err); 171b061f059SPaolo Bonzini if (local_err) { 172b061f059SPaolo Bonzini goto out; 173b061f059SPaolo Bonzini } 174b061f059SPaolo Bonzini } 175b061f059SPaolo Bonzini 176b061f059SPaolo Bonzini /* increment the number of CPUs */ 177b061f059SPaolo Bonzini x86ms->boot_cpus++; 178b061f059SPaolo Bonzini if (x86ms->rtc) { 179b061f059SPaolo Bonzini x86_rtc_set_cpus_count(x86ms->rtc, x86ms->boot_cpus); 180b061f059SPaolo Bonzini } 181b061f059SPaolo Bonzini if (x86ms->fw_cfg) { 182b061f059SPaolo Bonzini fw_cfg_modify_i16(x86ms->fw_cfg, FW_CFG_NB_CPUS, x86ms->boot_cpus); 183b061f059SPaolo Bonzini } 184b061f059SPaolo Bonzini 185b061f059SPaolo Bonzini found_cpu = x86_find_cpu_slot(MACHINE(x86ms), cpu->apic_id, NULL); 186b061f059SPaolo Bonzini found_cpu->cpu = CPU(dev); 187b061f059SPaolo Bonzini out: 188b061f059SPaolo Bonzini error_propagate(errp, local_err); 189b061f059SPaolo Bonzini } 190b061f059SPaolo Bonzini 191b061f059SPaolo Bonzini void x86_cpu_unplug_request_cb(HotplugHandler *hotplug_dev, 192b061f059SPaolo Bonzini DeviceState *dev, Error **errp) 193b061f059SPaolo Bonzini { 194b061f059SPaolo Bonzini int idx = -1; 195b061f059SPaolo Bonzini X86CPU *cpu = X86_CPU(dev); 196b061f059SPaolo Bonzini X86MachineState *x86ms = X86_MACHINE(hotplug_dev); 197b061f059SPaolo Bonzini 198b061f059SPaolo Bonzini if (!x86ms->acpi_dev) { 199b061f059SPaolo Bonzini error_setg(errp, "CPU hot unplug not supported without ACPI"); 200b061f059SPaolo Bonzini return; 201b061f059SPaolo Bonzini } 202b061f059SPaolo Bonzini 203b061f059SPaolo Bonzini x86_find_cpu_slot(MACHINE(x86ms), cpu->apic_id, &idx); 204b061f059SPaolo Bonzini assert(idx != -1); 205b061f059SPaolo Bonzini if (idx == 0) { 206b061f059SPaolo Bonzini error_setg(errp, "Boot CPU is unpluggable"); 207b061f059SPaolo Bonzini return; 208b061f059SPaolo Bonzini } 209b061f059SPaolo Bonzini 210b061f059SPaolo Bonzini hotplug_handler_unplug_request(x86ms->acpi_dev, dev, 211b061f059SPaolo Bonzini errp); 212b061f059SPaolo Bonzini } 213b061f059SPaolo Bonzini 214b061f059SPaolo Bonzini void x86_cpu_unplug_cb(HotplugHandler *hotplug_dev, 215b061f059SPaolo Bonzini DeviceState *dev, Error **errp) 216b061f059SPaolo Bonzini { 217b061f059SPaolo Bonzini CPUArchId *found_cpu; 218b061f059SPaolo Bonzini Error *local_err = NULL; 219b061f059SPaolo Bonzini X86CPU *cpu = X86_CPU(dev); 220b061f059SPaolo Bonzini X86MachineState *x86ms = X86_MACHINE(hotplug_dev); 221b061f059SPaolo Bonzini 222b061f059SPaolo Bonzini hotplug_handler_unplug(x86ms->acpi_dev, dev, &local_err); 223b061f059SPaolo Bonzini if (local_err) { 224b061f059SPaolo Bonzini goto out; 225b061f059SPaolo Bonzini } 226b061f059SPaolo Bonzini 227b061f059SPaolo Bonzini found_cpu = x86_find_cpu_slot(MACHINE(x86ms), cpu->apic_id, NULL); 228b061f059SPaolo Bonzini found_cpu->cpu = NULL; 229b061f059SPaolo Bonzini qdev_unrealize(dev); 230b061f059SPaolo Bonzini 231b061f059SPaolo Bonzini /* decrement the number of CPUs */ 232b061f059SPaolo Bonzini x86ms->boot_cpus--; 233b061f059SPaolo Bonzini /* Update the number of CPUs in CMOS */ 234b061f059SPaolo Bonzini x86_rtc_set_cpus_count(x86ms->rtc, x86ms->boot_cpus); 235b061f059SPaolo Bonzini fw_cfg_modify_i16(x86ms->fw_cfg, FW_CFG_NB_CPUS, x86ms->boot_cpus); 236b061f059SPaolo Bonzini out: 237b061f059SPaolo Bonzini error_propagate(errp, local_err); 238b061f059SPaolo Bonzini } 239b061f059SPaolo Bonzini 240b061f059SPaolo Bonzini void x86_cpu_pre_plug(HotplugHandler *hotplug_dev, 241b061f059SPaolo Bonzini DeviceState *dev, Error **errp) 242b061f059SPaolo Bonzini { 243b061f059SPaolo Bonzini int idx; 244b061f059SPaolo Bonzini CPUState *cs; 245b061f059SPaolo Bonzini CPUArchId *cpu_slot; 246b061f059SPaolo Bonzini X86CPUTopoIDs topo_ids; 247b061f059SPaolo Bonzini X86CPU *cpu = X86_CPU(dev); 248b061f059SPaolo Bonzini CPUX86State *env = &cpu->env; 249b061f059SPaolo Bonzini MachineState *ms = MACHINE(hotplug_dev); 250b061f059SPaolo Bonzini X86MachineState *x86ms = X86_MACHINE(hotplug_dev); 251b061f059SPaolo Bonzini unsigned int smp_cores = ms->smp.cores; 252b061f059SPaolo Bonzini unsigned int smp_threads = ms->smp.threads; 253b061f059SPaolo Bonzini X86CPUTopoInfo topo_info; 254b061f059SPaolo Bonzini 255b061f059SPaolo Bonzini if (!object_dynamic_cast(OBJECT(cpu), ms->cpu_type)) { 256b061f059SPaolo Bonzini error_setg(errp, "Invalid CPU type, expected cpu type: '%s'", 257b061f059SPaolo Bonzini ms->cpu_type); 258b061f059SPaolo Bonzini return; 259b061f059SPaolo Bonzini } 260b061f059SPaolo Bonzini 261b061f059SPaolo Bonzini if (x86ms->acpi_dev) { 262b061f059SPaolo Bonzini Error *local_err = NULL; 263b061f059SPaolo Bonzini 264b061f059SPaolo Bonzini hotplug_handler_pre_plug(HOTPLUG_HANDLER(x86ms->acpi_dev), dev, 265b061f059SPaolo Bonzini &local_err); 266b061f059SPaolo Bonzini if (local_err) { 267b061f059SPaolo Bonzini error_propagate(errp, local_err); 268b061f059SPaolo Bonzini return; 269b061f059SPaolo Bonzini } 270b061f059SPaolo Bonzini } 271b061f059SPaolo Bonzini 272b061f059SPaolo Bonzini init_topo_info(&topo_info, x86ms); 273b061f059SPaolo Bonzini 27481c392abSZhao Liu if (ms->smp.modules > 1) { 27581c392abSZhao Liu env->nr_modules = ms->smp.modules; 2765304873aSZhao Liu set_bit(CPU_TOPO_LEVEL_MODULE, env->avail_cpu_topo); 27781c392abSZhao Liu } 27881c392abSZhao Liu 2796ddeb0ecSZhao Liu if (ms->smp.dies > 1) { 280b061f059SPaolo Bonzini env->nr_dies = ms->smp.dies; 2816ddeb0ecSZhao Liu set_bit(CPU_TOPO_LEVEL_DIE, env->avail_cpu_topo); 2826ddeb0ecSZhao Liu } 283b061f059SPaolo Bonzini 284b061f059SPaolo Bonzini /* 285b061f059SPaolo Bonzini * If APIC ID is not set, 286b17a26bcSZhao Liu * set it based on socket/die/module/core/thread properties. 287b061f059SPaolo Bonzini */ 288b061f059SPaolo Bonzini if (cpu->apic_id == UNASSIGNED_APIC_ID) { 289b061f059SPaolo Bonzini /* 290b061f059SPaolo Bonzini * die-id was optional in QEMU 4.0 and older, so keep it optional 291b061f059SPaolo Bonzini * if there's only one die per socket. 292b061f059SPaolo Bonzini */ 293b061f059SPaolo Bonzini if (cpu->die_id < 0 && ms->smp.dies == 1) { 294b061f059SPaolo Bonzini cpu->die_id = 0; 295b061f059SPaolo Bonzini } 296b061f059SPaolo Bonzini 297*58820834SZhao Liu /* 298*58820834SZhao Liu * module-id was optional in QEMU 9.0 and older, so keep it optional 299*58820834SZhao Liu * if there's only one module per die. 300*58820834SZhao Liu */ 301*58820834SZhao Liu if (cpu->module_id < 0 && ms->smp.modules == 1) { 302*58820834SZhao Liu cpu->module_id = 0; 303*58820834SZhao Liu } 304*58820834SZhao Liu 305b061f059SPaolo Bonzini if (cpu->socket_id < 0) { 306b061f059SPaolo Bonzini error_setg(errp, "CPU socket-id is not set"); 307b061f059SPaolo Bonzini return; 308b17a26bcSZhao Liu } else if (cpu->socket_id > ms->smp.sockets - 1) { 309b061f059SPaolo Bonzini error_setg(errp, "Invalid CPU socket-id: %u must be in range 0:%u", 310b17a26bcSZhao Liu cpu->socket_id, ms->smp.sockets - 1); 311b061f059SPaolo Bonzini return; 312b061f059SPaolo Bonzini } 313b061f059SPaolo Bonzini if (cpu->die_id < 0) { 314b061f059SPaolo Bonzini error_setg(errp, "CPU die-id is not set"); 315b061f059SPaolo Bonzini return; 316b061f059SPaolo Bonzini } else if (cpu->die_id > ms->smp.dies - 1) { 317b061f059SPaolo Bonzini error_setg(errp, "Invalid CPU die-id: %u must be in range 0:%u", 318b061f059SPaolo Bonzini cpu->die_id, ms->smp.dies - 1); 319b061f059SPaolo Bonzini return; 320b061f059SPaolo Bonzini } 321*58820834SZhao Liu if (cpu->module_id < 0) { 322*58820834SZhao Liu error_setg(errp, "CPU module-id is not set"); 323*58820834SZhao Liu return; 324*58820834SZhao Liu } else if (cpu->module_id > ms->smp.modules - 1) { 325*58820834SZhao Liu error_setg(errp, "Invalid CPU module-id: %u must be in range 0:%u", 326*58820834SZhao Liu cpu->module_id, ms->smp.modules - 1); 327*58820834SZhao Liu return; 328*58820834SZhao Liu } 329b061f059SPaolo Bonzini if (cpu->core_id < 0) { 330b061f059SPaolo Bonzini error_setg(errp, "CPU core-id is not set"); 331b061f059SPaolo Bonzini return; 332b061f059SPaolo Bonzini } else if (cpu->core_id > (smp_cores - 1)) { 333b061f059SPaolo Bonzini error_setg(errp, "Invalid CPU core-id: %u must be in range 0:%u", 334b061f059SPaolo Bonzini cpu->core_id, smp_cores - 1); 335b061f059SPaolo Bonzini return; 336b061f059SPaolo Bonzini } 337b061f059SPaolo Bonzini if (cpu->thread_id < 0) { 338b061f059SPaolo Bonzini error_setg(errp, "CPU thread-id is not set"); 339b061f059SPaolo Bonzini return; 340b061f059SPaolo Bonzini } else if (cpu->thread_id > (smp_threads - 1)) { 341b061f059SPaolo Bonzini error_setg(errp, "Invalid CPU thread-id: %u must be in range 0:%u", 342b061f059SPaolo Bonzini cpu->thread_id, smp_threads - 1); 343b061f059SPaolo Bonzini return; 344b061f059SPaolo Bonzini } 345b061f059SPaolo Bonzini 346b061f059SPaolo Bonzini topo_ids.pkg_id = cpu->socket_id; 347b061f059SPaolo Bonzini topo_ids.die_id = cpu->die_id; 348*58820834SZhao Liu topo_ids.module_id = cpu->module_id; 349b061f059SPaolo Bonzini topo_ids.core_id = cpu->core_id; 350b061f059SPaolo Bonzini topo_ids.smt_id = cpu->thread_id; 351b061f059SPaolo Bonzini cpu->apic_id = x86_apicid_from_topo_ids(&topo_info, &topo_ids); 352b061f059SPaolo Bonzini } 353b061f059SPaolo Bonzini 354b061f059SPaolo Bonzini cpu_slot = x86_find_cpu_slot(MACHINE(x86ms), cpu->apic_id, &idx); 355b061f059SPaolo Bonzini if (!cpu_slot) { 356b061f059SPaolo Bonzini x86_topo_ids_from_apicid(cpu->apic_id, &topo_info, &topo_ids); 357b17a26bcSZhao Liu 358b061f059SPaolo Bonzini error_setg(errp, 359b17a26bcSZhao Liu "Invalid CPU [socket: %u, die: %u, module: %u, core: %u, thread: %u]" 360b17a26bcSZhao Liu " with APIC ID %" PRIu32 ", valid index range 0:%d", 361b17a26bcSZhao Liu topo_ids.pkg_id, topo_ids.die_id, topo_ids.module_id, 362b17a26bcSZhao Liu topo_ids.core_id, topo_ids.smt_id, cpu->apic_id, 363b17a26bcSZhao Liu ms->possible_cpus->len - 1); 364b061f059SPaolo Bonzini return; 365b061f059SPaolo Bonzini } 366b061f059SPaolo Bonzini 367b061f059SPaolo Bonzini if (cpu_slot->cpu) { 368b061f059SPaolo Bonzini error_setg(errp, "CPU[%d] with APIC ID %" PRIu32 " exists", 369b061f059SPaolo Bonzini idx, cpu->apic_id); 370b061f059SPaolo Bonzini return; 371b061f059SPaolo Bonzini } 372b061f059SPaolo Bonzini 373b061f059SPaolo Bonzini /* if 'address' properties socket-id/core-id/thread-id are not set, set them 374b061f059SPaolo Bonzini * so that machine_query_hotpluggable_cpus would show correct values 375b061f059SPaolo Bonzini */ 376b061f059SPaolo Bonzini /* TODO: move socket_id/core_id/thread_id checks into x86_cpu_realizefn() 377b061f059SPaolo Bonzini * once -smp refactoring is complete and there will be CPU private 378b061f059SPaolo Bonzini * CPUState::nr_cores and CPUState::nr_threads fields instead of globals */ 379b061f059SPaolo Bonzini x86_topo_ids_from_apicid(cpu->apic_id, &topo_info, &topo_ids); 380b061f059SPaolo Bonzini if (cpu->socket_id != -1 && cpu->socket_id != topo_ids.pkg_id) { 381b061f059SPaolo Bonzini error_setg(errp, "property socket-id: %u doesn't match set apic-id:" 382b061f059SPaolo Bonzini " 0x%x (socket-id: %u)", cpu->socket_id, cpu->apic_id, 383b061f059SPaolo Bonzini topo_ids.pkg_id); 384b061f059SPaolo Bonzini return; 385b061f059SPaolo Bonzini } 386b061f059SPaolo Bonzini cpu->socket_id = topo_ids.pkg_id; 387b061f059SPaolo Bonzini 388b061f059SPaolo Bonzini if (cpu->die_id != -1 && cpu->die_id != topo_ids.die_id) { 389b061f059SPaolo Bonzini error_setg(errp, "property die-id: %u doesn't match set apic-id:" 390b061f059SPaolo Bonzini " 0x%x (die-id: %u)", cpu->die_id, cpu->apic_id, topo_ids.die_id); 391b061f059SPaolo Bonzini return; 392b061f059SPaolo Bonzini } 393b061f059SPaolo Bonzini cpu->die_id = topo_ids.die_id; 394b061f059SPaolo Bonzini 395*58820834SZhao Liu if (cpu->module_id != -1 && cpu->module_id != topo_ids.module_id) { 396*58820834SZhao Liu error_setg(errp, "property module-id: %u doesn't match set apic-id:" 397*58820834SZhao Liu " 0x%x (module-id: %u)", cpu->module_id, cpu->apic_id, 398*58820834SZhao Liu topo_ids.module_id); 399*58820834SZhao Liu return; 400*58820834SZhao Liu } 401*58820834SZhao Liu cpu->module_id = topo_ids.module_id; 402*58820834SZhao Liu 403b061f059SPaolo Bonzini if (cpu->core_id != -1 && cpu->core_id != topo_ids.core_id) { 404b061f059SPaolo Bonzini error_setg(errp, "property core-id: %u doesn't match set apic-id:" 405b061f059SPaolo Bonzini " 0x%x (core-id: %u)", cpu->core_id, cpu->apic_id, 406b061f059SPaolo Bonzini topo_ids.core_id); 407b061f059SPaolo Bonzini return; 408b061f059SPaolo Bonzini } 409b061f059SPaolo Bonzini cpu->core_id = topo_ids.core_id; 410b061f059SPaolo Bonzini 411b061f059SPaolo Bonzini if (cpu->thread_id != -1 && cpu->thread_id != topo_ids.smt_id) { 412b061f059SPaolo Bonzini error_setg(errp, "property thread-id: %u doesn't match set apic-id:" 413b061f059SPaolo Bonzini " 0x%x (thread-id: %u)", cpu->thread_id, cpu->apic_id, 414b061f059SPaolo Bonzini topo_ids.smt_id); 415b061f059SPaolo Bonzini return; 416b061f059SPaolo Bonzini } 417b061f059SPaolo Bonzini cpu->thread_id = topo_ids.smt_id; 418b061f059SPaolo Bonzini 419b061f059SPaolo Bonzini /* 420b061f059SPaolo Bonzini * kvm_enabled() must go first to ensure that kvm_* references are 421b061f059SPaolo Bonzini * not emitted for the linker to consume (kvm_enabled() is 422b061f059SPaolo Bonzini * a literal `0` in configurations where kvm_* aren't defined) 423b061f059SPaolo Bonzini */ 424b061f059SPaolo Bonzini if (kvm_enabled() && hyperv_feat_enabled(cpu, HYPERV_FEAT_VPINDEX) && 425b061f059SPaolo Bonzini !kvm_hv_vpindex_settable()) { 426b061f059SPaolo Bonzini error_setg(errp, "kernel doesn't allow setting HyperV VP_INDEX"); 427b061f059SPaolo Bonzini return; 428b061f059SPaolo Bonzini } 429b061f059SPaolo Bonzini 430b061f059SPaolo Bonzini cs = CPU(cpu); 431b061f059SPaolo Bonzini cs->cpu_index = idx; 432b061f059SPaolo Bonzini 433b061f059SPaolo Bonzini numa_cpu_pre_plug(cpu_slot, dev, errp); 434b061f059SPaolo Bonzini } 435b061f059SPaolo Bonzini 436b061f059SPaolo Bonzini static long get_file_size(FILE *f) 437b061f059SPaolo Bonzini { 438b061f059SPaolo Bonzini long where, size; 439b061f059SPaolo Bonzini 440b061f059SPaolo Bonzini /* XXX: on Unix systems, using fstat() probably makes more sense */ 441b061f059SPaolo Bonzini 442b061f059SPaolo Bonzini where = ftell(f); 443b061f059SPaolo Bonzini fseek(f, 0, SEEK_END); 444b061f059SPaolo Bonzini size = ftell(f); 445b061f059SPaolo Bonzini fseek(f, where, SEEK_SET); 446b061f059SPaolo Bonzini 447b061f059SPaolo Bonzini return size; 448b061f059SPaolo Bonzini } 449b061f059SPaolo Bonzini 450b061f059SPaolo Bonzini void gsi_handler(void *opaque, int n, int level) 451b061f059SPaolo Bonzini { 452b061f059SPaolo Bonzini GSIState *s = opaque; 453b061f059SPaolo Bonzini 454b061f059SPaolo Bonzini trace_x86_gsi_interrupt(n, level); 455b061f059SPaolo Bonzini switch (n) { 456b061f059SPaolo Bonzini case 0 ... ISA_NUM_IRQS - 1: 457b061f059SPaolo Bonzini if (s->i8259_irq[n]) { 458b061f059SPaolo Bonzini /* Under KVM, Kernel will forward to both PIC and IOAPIC */ 459b061f059SPaolo Bonzini qemu_set_irq(s->i8259_irq[n], level); 460b061f059SPaolo Bonzini } 461b061f059SPaolo Bonzini /* fall through */ 462b061f059SPaolo Bonzini case ISA_NUM_IRQS ... IOAPIC_NUM_PINS - 1: 463b061f059SPaolo Bonzini #ifdef CONFIG_XEN_EMU 464b061f059SPaolo Bonzini /* 465b061f059SPaolo Bonzini * Xen delivers the GSI to the Legacy PIC (not that Legacy PIC 466b061f059SPaolo Bonzini * routing actually works properly under Xen). And then to 467b061f059SPaolo Bonzini * *either* the PIRQ handling or the I/OAPIC depending on 468b061f059SPaolo Bonzini * whether the former wants it. 469b061f059SPaolo Bonzini */ 470b061f059SPaolo Bonzini if (xen_mode == XEN_EMULATE && xen_evtchn_set_gsi(n, level)) { 471b061f059SPaolo Bonzini break; 472b061f059SPaolo Bonzini } 473b061f059SPaolo Bonzini #endif 474b061f059SPaolo Bonzini qemu_set_irq(s->ioapic_irq[n], level); 475b061f059SPaolo Bonzini break; 476b061f059SPaolo Bonzini case IO_APIC_SECONDARY_IRQBASE 477b061f059SPaolo Bonzini ... IO_APIC_SECONDARY_IRQBASE + IOAPIC_NUM_PINS - 1: 478b061f059SPaolo Bonzini qemu_set_irq(s->ioapic2_irq[n - IO_APIC_SECONDARY_IRQBASE], level); 479b061f059SPaolo Bonzini break; 480b061f059SPaolo Bonzini } 481b061f059SPaolo Bonzini } 482b061f059SPaolo Bonzini 483b061f059SPaolo Bonzini void ioapic_init_gsi(GSIState *gsi_state, Object *parent) 484b061f059SPaolo Bonzini { 485b061f059SPaolo Bonzini DeviceState *dev; 486b061f059SPaolo Bonzini SysBusDevice *d; 487b061f059SPaolo Bonzini unsigned int i; 488b061f059SPaolo Bonzini 489b061f059SPaolo Bonzini assert(parent); 490b061f059SPaolo Bonzini if (kvm_ioapic_in_kernel()) { 491b061f059SPaolo Bonzini dev = qdev_new(TYPE_KVM_IOAPIC); 492b061f059SPaolo Bonzini } else { 493b061f059SPaolo Bonzini dev = qdev_new(TYPE_IOAPIC); 494b061f059SPaolo Bonzini } 495b061f059SPaolo Bonzini object_property_add_child(parent, "ioapic", OBJECT(dev)); 496b061f059SPaolo Bonzini d = SYS_BUS_DEVICE(dev); 497b061f059SPaolo Bonzini sysbus_realize_and_unref(d, &error_fatal); 498b061f059SPaolo Bonzini sysbus_mmio_map(d, 0, IO_APIC_DEFAULT_ADDRESS); 499b061f059SPaolo Bonzini 500b061f059SPaolo Bonzini for (i = 0; i < IOAPIC_NUM_PINS; i++) { 501b061f059SPaolo Bonzini gsi_state->ioapic_irq[i] = qdev_get_gpio_in(dev, i); 502b061f059SPaolo Bonzini } 503b061f059SPaolo Bonzini } 504b061f059SPaolo Bonzini 505b061f059SPaolo Bonzini DeviceState *ioapic_init_secondary(GSIState *gsi_state) 506b061f059SPaolo Bonzini { 507b061f059SPaolo Bonzini DeviceState *dev; 508b061f059SPaolo Bonzini SysBusDevice *d; 509b061f059SPaolo Bonzini unsigned int i; 510b061f059SPaolo Bonzini 511b061f059SPaolo Bonzini dev = qdev_new(TYPE_IOAPIC); 512b061f059SPaolo Bonzini d = SYS_BUS_DEVICE(dev); 513b061f059SPaolo Bonzini sysbus_realize_and_unref(d, &error_fatal); 514b061f059SPaolo Bonzini sysbus_mmio_map(d, 0, IO_APIC_SECONDARY_ADDRESS); 515b061f059SPaolo Bonzini 516b061f059SPaolo Bonzini for (i = 0; i < IOAPIC_NUM_PINS; i++) { 517b061f059SPaolo Bonzini gsi_state->ioapic2_irq[i] = qdev_get_gpio_in(dev, i); 518b061f059SPaolo Bonzini } 519b061f059SPaolo Bonzini return dev; 520b061f059SPaolo Bonzini } 521b061f059SPaolo Bonzini 522b061f059SPaolo Bonzini /* 523b061f059SPaolo Bonzini * The entry point into the kernel for PVH boot is different from 524b061f059SPaolo Bonzini * the native entry point. The PVH entry is defined by the x86/HVM 525b061f059SPaolo Bonzini * direct boot ABI and is available in an ELFNOTE in the kernel binary. 526b061f059SPaolo Bonzini * 527b061f059SPaolo Bonzini * This function is passed to load_elf() when it is called from 528b061f059SPaolo Bonzini * load_elfboot() which then additionally checks for an ELF Note of 529b061f059SPaolo Bonzini * type XEN_ELFNOTE_PHYS32_ENTRY and passes it to this function to 530b061f059SPaolo Bonzini * parse the PVH entry address from the ELF Note. 531b061f059SPaolo Bonzini * 532b061f059SPaolo Bonzini * Due to trickery in elf_opts.h, load_elf() is actually available as 533b061f059SPaolo Bonzini * load_elf32() or load_elf64() and this routine needs to be able 534b061f059SPaolo Bonzini * to deal with being called as 32 or 64 bit. 535b061f059SPaolo Bonzini * 536b061f059SPaolo Bonzini * The address of the PVH entry point is saved to the 'pvh_start_addr' 537b061f059SPaolo Bonzini * global variable. (although the entry point is 32-bit, the kernel 538b061f059SPaolo Bonzini * binary can be either 32-bit or 64-bit). 539b061f059SPaolo Bonzini */ 540b061f059SPaolo Bonzini static uint64_t read_pvh_start_addr(void *arg1, void *arg2, bool is64) 541b061f059SPaolo Bonzini { 542b061f059SPaolo Bonzini size_t *elf_note_data_addr; 543b061f059SPaolo Bonzini 544b061f059SPaolo Bonzini /* Check if ELF Note header passed in is valid */ 545b061f059SPaolo Bonzini if (arg1 == NULL) { 546b061f059SPaolo Bonzini return 0; 547b061f059SPaolo Bonzini } 548b061f059SPaolo Bonzini 549b061f059SPaolo Bonzini if (is64) { 550b061f059SPaolo Bonzini struct elf64_note *nhdr64 = (struct elf64_note *)arg1; 551b061f059SPaolo Bonzini uint64_t nhdr_size64 = sizeof(struct elf64_note); 552b061f059SPaolo Bonzini uint64_t phdr_align = *(uint64_t *)arg2; 553b061f059SPaolo Bonzini uint64_t nhdr_namesz = nhdr64->n_namesz; 554b061f059SPaolo Bonzini 555b061f059SPaolo Bonzini elf_note_data_addr = 556b061f059SPaolo Bonzini ((void *)nhdr64) + nhdr_size64 + 557b061f059SPaolo Bonzini QEMU_ALIGN_UP(nhdr_namesz, phdr_align); 558b061f059SPaolo Bonzini 559b061f059SPaolo Bonzini pvh_start_addr = *elf_note_data_addr; 560b061f059SPaolo Bonzini } else { 561b061f059SPaolo Bonzini struct elf32_note *nhdr32 = (struct elf32_note *)arg1; 562b061f059SPaolo Bonzini uint32_t nhdr_size32 = sizeof(struct elf32_note); 563b061f059SPaolo Bonzini uint32_t phdr_align = *(uint32_t *)arg2; 564b061f059SPaolo Bonzini uint32_t nhdr_namesz = nhdr32->n_namesz; 565b061f059SPaolo Bonzini 566b061f059SPaolo Bonzini elf_note_data_addr = 567b061f059SPaolo Bonzini ((void *)nhdr32) + nhdr_size32 + 568b061f059SPaolo Bonzini QEMU_ALIGN_UP(nhdr_namesz, phdr_align); 569b061f059SPaolo Bonzini 570b061f059SPaolo Bonzini pvh_start_addr = *(uint32_t *)elf_note_data_addr; 571b061f059SPaolo Bonzini } 572b061f059SPaolo Bonzini 573b061f059SPaolo Bonzini return pvh_start_addr; 574b061f059SPaolo Bonzini } 575b061f059SPaolo Bonzini 576b061f059SPaolo Bonzini static bool load_elfboot(const char *kernel_filename, 577b061f059SPaolo Bonzini int kernel_file_size, 578b061f059SPaolo Bonzini uint8_t *header, 579b061f059SPaolo Bonzini size_t pvh_xen_start_addr, 580b061f059SPaolo Bonzini FWCfgState *fw_cfg) 581b061f059SPaolo Bonzini { 582b061f059SPaolo Bonzini uint32_t flags = 0; 583b061f059SPaolo Bonzini uint32_t mh_load_addr = 0; 584b061f059SPaolo Bonzini uint32_t elf_kernel_size = 0; 585b061f059SPaolo Bonzini uint64_t elf_entry; 586b061f059SPaolo Bonzini uint64_t elf_low, elf_high; 587b061f059SPaolo Bonzini int kernel_size; 588b061f059SPaolo Bonzini 589b061f059SPaolo Bonzini if (ldl_p(header) != 0x464c457f) { 590b061f059SPaolo Bonzini return false; /* no elfboot */ 591b061f059SPaolo Bonzini } 592b061f059SPaolo Bonzini 593b061f059SPaolo Bonzini bool elf_is64 = header[EI_CLASS] == ELFCLASS64; 594b061f059SPaolo Bonzini flags = elf_is64 ? 595b061f059SPaolo Bonzini ((Elf64_Ehdr *)header)->e_flags : ((Elf32_Ehdr *)header)->e_flags; 596b061f059SPaolo Bonzini 597b061f059SPaolo Bonzini if (flags & 0x00010004) { /* LOAD_ELF_HEADER_HAS_ADDR */ 598b061f059SPaolo Bonzini error_report("elfboot unsupported flags = %x", flags); 599b061f059SPaolo Bonzini exit(1); 600b061f059SPaolo Bonzini } 601b061f059SPaolo Bonzini 602b061f059SPaolo Bonzini uint64_t elf_note_type = XEN_ELFNOTE_PHYS32_ENTRY; 603b061f059SPaolo Bonzini kernel_size = load_elf(kernel_filename, read_pvh_start_addr, 604b061f059SPaolo Bonzini NULL, &elf_note_type, &elf_entry, 605b061f059SPaolo Bonzini &elf_low, &elf_high, NULL, 0, I386_ELF_MACHINE, 606b061f059SPaolo Bonzini 0, 0); 607b061f059SPaolo Bonzini 608b061f059SPaolo Bonzini if (kernel_size < 0) { 609b061f059SPaolo Bonzini error_report("Error while loading elf kernel"); 610b061f059SPaolo Bonzini exit(1); 611b061f059SPaolo Bonzini } 612b061f059SPaolo Bonzini mh_load_addr = elf_low; 613b061f059SPaolo Bonzini elf_kernel_size = elf_high - elf_low; 614b061f059SPaolo Bonzini 615b061f059SPaolo Bonzini if (pvh_start_addr == 0) { 616b061f059SPaolo Bonzini error_report("Error loading uncompressed kernel without PVH ELF Note"); 617b061f059SPaolo Bonzini exit(1); 618b061f059SPaolo Bonzini } 619b061f059SPaolo Bonzini fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_ENTRY, pvh_start_addr); 620b061f059SPaolo Bonzini fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_ADDR, mh_load_addr); 621b061f059SPaolo Bonzini fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_SIZE, elf_kernel_size); 622b061f059SPaolo Bonzini 623b061f059SPaolo Bonzini return true; 624b061f059SPaolo Bonzini } 625b061f059SPaolo Bonzini 626b061f059SPaolo Bonzini void x86_load_linux(X86MachineState *x86ms, 627b061f059SPaolo Bonzini FWCfgState *fw_cfg, 628b061f059SPaolo Bonzini int acpi_data_size, 629b061f059SPaolo Bonzini bool pvh_enabled) 630b061f059SPaolo Bonzini { 631b061f059SPaolo Bonzini bool linuxboot_dma_enabled = X86_MACHINE_GET_CLASS(x86ms)->fwcfg_dma_enabled; 632b061f059SPaolo Bonzini uint16_t protocol; 633b061f059SPaolo Bonzini int setup_size, kernel_size, cmdline_size; 634b061f059SPaolo Bonzini int dtb_size, setup_data_offset; 635b061f059SPaolo Bonzini uint32_t initrd_max; 636b061f059SPaolo Bonzini uint8_t header[8192], *setup, *kernel; 637b061f059SPaolo Bonzini hwaddr real_addr, prot_addr, cmdline_addr, initrd_addr = 0; 638b061f059SPaolo Bonzini FILE *f; 639b061f059SPaolo Bonzini char *vmode; 640b061f059SPaolo Bonzini MachineState *machine = MACHINE(x86ms); 641b061f059SPaolo Bonzini struct setup_data *setup_data; 642b061f059SPaolo Bonzini const char *kernel_filename = machine->kernel_filename; 643b061f059SPaolo Bonzini const char *initrd_filename = machine->initrd_filename; 644b061f059SPaolo Bonzini const char *dtb_filename = machine->dtb; 645b061f059SPaolo Bonzini const char *kernel_cmdline = machine->kernel_cmdline; 646b061f059SPaolo Bonzini SevKernelLoaderContext sev_load_ctx = {}; 647b061f059SPaolo Bonzini 648b061f059SPaolo Bonzini /* Align to 16 bytes as a paranoia measure */ 649b061f059SPaolo Bonzini cmdline_size = (strlen(kernel_cmdline) + 16) & ~15; 650b061f059SPaolo Bonzini 651b061f059SPaolo Bonzini /* load the kernel header */ 652b061f059SPaolo Bonzini f = fopen(kernel_filename, "rb"); 653b061f059SPaolo Bonzini if (!f) { 654b061f059SPaolo Bonzini fprintf(stderr, "qemu: could not open kernel file '%s': %s\n", 655b061f059SPaolo Bonzini kernel_filename, strerror(errno)); 656b061f059SPaolo Bonzini exit(1); 657b061f059SPaolo Bonzini } 658b061f059SPaolo Bonzini 659b061f059SPaolo Bonzini kernel_size = get_file_size(f); 660b061f059SPaolo Bonzini if (!kernel_size || 661b061f059SPaolo Bonzini fread(header, 1, MIN(ARRAY_SIZE(header), kernel_size), f) != 662b061f059SPaolo Bonzini MIN(ARRAY_SIZE(header), kernel_size)) { 663b061f059SPaolo Bonzini fprintf(stderr, "qemu: could not load kernel '%s': %s\n", 664b061f059SPaolo Bonzini kernel_filename, strerror(errno)); 665b061f059SPaolo Bonzini exit(1); 666b061f059SPaolo Bonzini } 667b061f059SPaolo Bonzini 668b061f059SPaolo Bonzini /* kernel protocol version */ 669b061f059SPaolo Bonzini if (ldl_p(header + 0x202) == 0x53726448) { 670b061f059SPaolo Bonzini protocol = lduw_p(header + 0x206); 671b061f059SPaolo Bonzini } else { 672b061f059SPaolo Bonzini /* 673b061f059SPaolo Bonzini * This could be a multiboot kernel. If it is, let's stop treating it 674b061f059SPaolo Bonzini * like a Linux kernel. 675b061f059SPaolo Bonzini * Note: some multiboot images could be in the ELF format (the same of 676b061f059SPaolo Bonzini * PVH), so we try multiboot first since we check the multiboot magic 677b061f059SPaolo Bonzini * header before to load it. 678b061f059SPaolo Bonzini */ 679b061f059SPaolo Bonzini if (load_multiboot(x86ms, fw_cfg, f, kernel_filename, initrd_filename, 680b061f059SPaolo Bonzini kernel_cmdline, kernel_size, header)) { 681b061f059SPaolo Bonzini return; 682b061f059SPaolo Bonzini } 683b061f059SPaolo Bonzini /* 684b061f059SPaolo Bonzini * Check if the file is an uncompressed kernel file (ELF) and load it, 685b061f059SPaolo Bonzini * saving the PVH entry point used by the x86/HVM direct boot ABI. 686b061f059SPaolo Bonzini * If load_elfboot() is successful, populate the fw_cfg info. 687b061f059SPaolo Bonzini */ 688b061f059SPaolo Bonzini if (pvh_enabled && 689b061f059SPaolo Bonzini load_elfboot(kernel_filename, kernel_size, 690b061f059SPaolo Bonzini header, pvh_start_addr, fw_cfg)) { 691b061f059SPaolo Bonzini fclose(f); 692b061f059SPaolo Bonzini 693b061f059SPaolo Bonzini fw_cfg_add_i32(fw_cfg, FW_CFG_CMDLINE_SIZE, 694b061f059SPaolo Bonzini strlen(kernel_cmdline) + 1); 695b061f059SPaolo Bonzini fw_cfg_add_string(fw_cfg, FW_CFG_CMDLINE_DATA, kernel_cmdline); 696b061f059SPaolo Bonzini 697b061f059SPaolo Bonzini fw_cfg_add_i32(fw_cfg, FW_CFG_SETUP_SIZE, sizeof(header)); 698b061f059SPaolo Bonzini fw_cfg_add_bytes(fw_cfg, FW_CFG_SETUP_DATA, 699b061f059SPaolo Bonzini header, sizeof(header)); 700b061f059SPaolo Bonzini 701b061f059SPaolo Bonzini /* load initrd */ 702b061f059SPaolo Bonzini if (initrd_filename) { 703b061f059SPaolo Bonzini GMappedFile *mapped_file; 704b061f059SPaolo Bonzini gsize initrd_size; 705b061f059SPaolo Bonzini gchar *initrd_data; 706b061f059SPaolo Bonzini GError *gerr = NULL; 707b061f059SPaolo Bonzini 708b061f059SPaolo Bonzini mapped_file = g_mapped_file_new(initrd_filename, false, &gerr); 709b061f059SPaolo Bonzini if (!mapped_file) { 710b061f059SPaolo Bonzini fprintf(stderr, "qemu: error reading initrd %s: %s\n", 711b061f059SPaolo Bonzini initrd_filename, gerr->message); 712b061f059SPaolo Bonzini exit(1); 713b061f059SPaolo Bonzini } 714b061f059SPaolo Bonzini x86ms->initrd_mapped_file = mapped_file; 715b061f059SPaolo Bonzini 716b061f059SPaolo Bonzini initrd_data = g_mapped_file_get_contents(mapped_file); 717b061f059SPaolo Bonzini initrd_size = g_mapped_file_get_length(mapped_file); 718b061f059SPaolo Bonzini initrd_max = x86ms->below_4g_mem_size - acpi_data_size - 1; 719b061f059SPaolo Bonzini if (initrd_size >= initrd_max) { 720b061f059SPaolo Bonzini fprintf(stderr, "qemu: initrd is too large, cannot support." 721b061f059SPaolo Bonzini "(max: %"PRIu32", need %"PRId64")\n", 722b061f059SPaolo Bonzini initrd_max, (uint64_t)initrd_size); 723b061f059SPaolo Bonzini exit(1); 724b061f059SPaolo Bonzini } 725b061f059SPaolo Bonzini 726b061f059SPaolo Bonzini initrd_addr = (initrd_max - initrd_size) & ~4095; 727b061f059SPaolo Bonzini 728b061f059SPaolo Bonzini fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_ADDR, initrd_addr); 729b061f059SPaolo Bonzini fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_SIZE, initrd_size); 730b061f059SPaolo Bonzini fw_cfg_add_bytes(fw_cfg, FW_CFG_INITRD_DATA, initrd_data, 731b061f059SPaolo Bonzini initrd_size); 732b061f059SPaolo Bonzini } 733b061f059SPaolo Bonzini 734b061f059SPaolo Bonzini option_rom[nb_option_roms].bootindex = 0; 735b061f059SPaolo Bonzini option_rom[nb_option_roms].name = "pvh.bin"; 736b061f059SPaolo Bonzini nb_option_roms++; 737b061f059SPaolo Bonzini 738b061f059SPaolo Bonzini return; 739b061f059SPaolo Bonzini } 740b061f059SPaolo Bonzini protocol = 0; 741b061f059SPaolo Bonzini } 742b061f059SPaolo Bonzini 743b061f059SPaolo Bonzini if (protocol < 0x200 || !(header[0x211] & 0x01)) { 744b061f059SPaolo Bonzini /* Low kernel */ 745b061f059SPaolo Bonzini real_addr = 0x90000; 746b061f059SPaolo Bonzini cmdline_addr = 0x9a000 - cmdline_size; 747b061f059SPaolo Bonzini prot_addr = 0x10000; 748b061f059SPaolo Bonzini } else if (protocol < 0x202) { 749b061f059SPaolo Bonzini /* High but ancient kernel */ 750b061f059SPaolo Bonzini real_addr = 0x90000; 751b061f059SPaolo Bonzini cmdline_addr = 0x9a000 - cmdline_size; 752b061f059SPaolo Bonzini prot_addr = 0x100000; 753b061f059SPaolo Bonzini } else { 754b061f059SPaolo Bonzini /* High and recent kernel */ 755b061f059SPaolo Bonzini real_addr = 0x10000; 756b061f059SPaolo Bonzini cmdline_addr = 0x20000; 757b061f059SPaolo Bonzini prot_addr = 0x100000; 758b061f059SPaolo Bonzini } 759b061f059SPaolo Bonzini 760b061f059SPaolo Bonzini /* highest address for loading the initrd */ 761b061f059SPaolo Bonzini if (protocol >= 0x20c && 762b061f059SPaolo Bonzini lduw_p(header + 0x236) & XLF_CAN_BE_LOADED_ABOVE_4G) { 763b061f059SPaolo Bonzini /* 764b061f059SPaolo Bonzini * Linux has supported initrd up to 4 GB for a very long time (2007, 765b061f059SPaolo Bonzini * long before XLF_CAN_BE_LOADED_ABOVE_4G which was added in 2013), 766b061f059SPaolo Bonzini * though it only sets initrd_max to 2 GB to "work around bootloader 767b061f059SPaolo Bonzini * bugs". Luckily, QEMU firmware(which does something like bootloader) 768b061f059SPaolo Bonzini * has supported this. 769b061f059SPaolo Bonzini * 770b061f059SPaolo Bonzini * It's believed that if XLF_CAN_BE_LOADED_ABOVE_4G is set, initrd can 771b061f059SPaolo Bonzini * be loaded into any address. 772b061f059SPaolo Bonzini * 773b061f059SPaolo Bonzini * In addition, initrd_max is uint32_t simply because QEMU doesn't 774b061f059SPaolo Bonzini * support the 64-bit boot protocol (specifically the ext_ramdisk_image 775b061f059SPaolo Bonzini * field). 776b061f059SPaolo Bonzini * 777b061f059SPaolo Bonzini * Therefore here just limit initrd_max to UINT32_MAX simply as well. 778b061f059SPaolo Bonzini */ 779b061f059SPaolo Bonzini initrd_max = UINT32_MAX; 780b061f059SPaolo Bonzini } else if (protocol >= 0x203) { 781b061f059SPaolo Bonzini initrd_max = ldl_p(header + 0x22c); 782b061f059SPaolo Bonzini } else { 783b061f059SPaolo Bonzini initrd_max = 0x37ffffff; 784b061f059SPaolo Bonzini } 785b061f059SPaolo Bonzini 786b061f059SPaolo Bonzini if (initrd_max >= x86ms->below_4g_mem_size - acpi_data_size) { 787b061f059SPaolo Bonzini initrd_max = x86ms->below_4g_mem_size - acpi_data_size - 1; 788b061f059SPaolo Bonzini } 789b061f059SPaolo Bonzini 790b061f059SPaolo Bonzini fw_cfg_add_i32(fw_cfg, FW_CFG_CMDLINE_ADDR, cmdline_addr); 791b061f059SPaolo Bonzini fw_cfg_add_i32(fw_cfg, FW_CFG_CMDLINE_SIZE, strlen(kernel_cmdline) + 1); 792b061f059SPaolo Bonzini fw_cfg_add_string(fw_cfg, FW_CFG_CMDLINE_DATA, kernel_cmdline); 793b061f059SPaolo Bonzini sev_load_ctx.cmdline_data = (char *)kernel_cmdline; 794b061f059SPaolo Bonzini sev_load_ctx.cmdline_size = strlen(kernel_cmdline) + 1; 795b061f059SPaolo Bonzini 796b061f059SPaolo Bonzini if (protocol >= 0x202) { 797b061f059SPaolo Bonzini stl_p(header + 0x228, cmdline_addr); 798b061f059SPaolo Bonzini } else { 799b061f059SPaolo Bonzini stw_p(header + 0x20, 0xA33F); 800b061f059SPaolo Bonzini stw_p(header + 0x22, cmdline_addr - real_addr); 801b061f059SPaolo Bonzini } 802b061f059SPaolo Bonzini 803b061f059SPaolo Bonzini /* handle vga= parameter */ 804b061f059SPaolo Bonzini vmode = strstr(kernel_cmdline, "vga="); 805b061f059SPaolo Bonzini if (vmode) { 806b061f059SPaolo Bonzini unsigned int video_mode; 807b061f059SPaolo Bonzini const char *end; 808b061f059SPaolo Bonzini int ret; 809b061f059SPaolo Bonzini /* skip "vga=" */ 810b061f059SPaolo Bonzini vmode += 4; 811b061f059SPaolo Bonzini if (!strncmp(vmode, "normal", 6)) { 812b061f059SPaolo Bonzini video_mode = 0xffff; 813b061f059SPaolo Bonzini } else if (!strncmp(vmode, "ext", 3)) { 814b061f059SPaolo Bonzini video_mode = 0xfffe; 815b061f059SPaolo Bonzini } else if (!strncmp(vmode, "ask", 3)) { 816b061f059SPaolo Bonzini video_mode = 0xfffd; 817b061f059SPaolo Bonzini } else { 818b061f059SPaolo Bonzini ret = qemu_strtoui(vmode, &end, 0, &video_mode); 819b061f059SPaolo Bonzini if (ret != 0 || (*end && *end != ' ')) { 820b061f059SPaolo Bonzini fprintf(stderr, "qemu: invalid 'vga=' kernel parameter.\n"); 821b061f059SPaolo Bonzini exit(1); 822b061f059SPaolo Bonzini } 823b061f059SPaolo Bonzini } 824b061f059SPaolo Bonzini stw_p(header + 0x1fa, video_mode); 825b061f059SPaolo Bonzini } 826b061f059SPaolo Bonzini 827b061f059SPaolo Bonzini /* loader type */ 828b061f059SPaolo Bonzini /* 829b061f059SPaolo Bonzini * High nybble = B reserved for QEMU; low nybble is revision number. 830b061f059SPaolo Bonzini * If this code is substantially changed, you may want to consider 831b061f059SPaolo Bonzini * incrementing the revision. 832b061f059SPaolo Bonzini */ 833b061f059SPaolo Bonzini if (protocol >= 0x200) { 834b061f059SPaolo Bonzini header[0x210] = 0xB0; 835b061f059SPaolo Bonzini } 836b061f059SPaolo Bonzini /* heap */ 837b061f059SPaolo Bonzini if (protocol >= 0x201) { 838b061f059SPaolo Bonzini header[0x211] |= 0x80; /* CAN_USE_HEAP */ 839b061f059SPaolo Bonzini stw_p(header + 0x224, cmdline_addr - real_addr - 0x200); 840b061f059SPaolo Bonzini } 841b061f059SPaolo Bonzini 842b061f059SPaolo Bonzini /* load initrd */ 843b061f059SPaolo Bonzini if (initrd_filename) { 844b061f059SPaolo Bonzini GMappedFile *mapped_file; 845b061f059SPaolo Bonzini gsize initrd_size; 846b061f059SPaolo Bonzini gchar *initrd_data; 847b061f059SPaolo Bonzini GError *gerr = NULL; 848b061f059SPaolo Bonzini 849b061f059SPaolo Bonzini if (protocol < 0x200) { 850b061f059SPaolo Bonzini fprintf(stderr, "qemu: linux kernel too old to load a ram disk\n"); 851b061f059SPaolo Bonzini exit(1); 852b061f059SPaolo Bonzini } 853b061f059SPaolo Bonzini 854b061f059SPaolo Bonzini mapped_file = g_mapped_file_new(initrd_filename, false, &gerr); 855b061f059SPaolo Bonzini if (!mapped_file) { 856b061f059SPaolo Bonzini fprintf(stderr, "qemu: error reading initrd %s: %s\n", 857b061f059SPaolo Bonzini initrd_filename, gerr->message); 858b061f059SPaolo Bonzini exit(1); 859b061f059SPaolo Bonzini } 860b061f059SPaolo Bonzini x86ms->initrd_mapped_file = mapped_file; 861b061f059SPaolo Bonzini 862b061f059SPaolo Bonzini initrd_data = g_mapped_file_get_contents(mapped_file); 863b061f059SPaolo Bonzini initrd_size = g_mapped_file_get_length(mapped_file); 864b061f059SPaolo Bonzini if (initrd_size >= initrd_max) { 865b061f059SPaolo Bonzini fprintf(stderr, "qemu: initrd is too large, cannot support." 866b061f059SPaolo Bonzini "(max: %"PRIu32", need %"PRId64")\n", 867b061f059SPaolo Bonzini initrd_max, (uint64_t)initrd_size); 868b061f059SPaolo Bonzini exit(1); 869b061f059SPaolo Bonzini } 870b061f059SPaolo Bonzini 871b061f059SPaolo Bonzini initrd_addr = (initrd_max - initrd_size) & ~4095; 872b061f059SPaolo Bonzini 873b061f059SPaolo Bonzini fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_ADDR, initrd_addr); 874b061f059SPaolo Bonzini fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_SIZE, initrd_size); 875b061f059SPaolo Bonzini fw_cfg_add_bytes(fw_cfg, FW_CFG_INITRD_DATA, initrd_data, initrd_size); 876b061f059SPaolo Bonzini sev_load_ctx.initrd_data = initrd_data; 877b061f059SPaolo Bonzini sev_load_ctx.initrd_size = initrd_size; 878b061f059SPaolo Bonzini 879b061f059SPaolo Bonzini stl_p(header + 0x218, initrd_addr); 880b061f059SPaolo Bonzini stl_p(header + 0x21c, initrd_size); 881b061f059SPaolo Bonzini } 882b061f059SPaolo Bonzini 883b061f059SPaolo Bonzini /* load kernel and setup */ 884b061f059SPaolo Bonzini setup_size = header[0x1f1]; 885b061f059SPaolo Bonzini if (setup_size == 0) { 886b061f059SPaolo Bonzini setup_size = 4; 887b061f059SPaolo Bonzini } 888b061f059SPaolo Bonzini setup_size = (setup_size + 1) * 512; 889b061f059SPaolo Bonzini if (setup_size > kernel_size) { 890b061f059SPaolo Bonzini fprintf(stderr, "qemu: invalid kernel header\n"); 891b061f059SPaolo Bonzini exit(1); 892b061f059SPaolo Bonzini } 893b061f059SPaolo Bonzini kernel_size -= setup_size; 894b061f059SPaolo Bonzini 895b061f059SPaolo Bonzini setup = g_malloc(setup_size); 896b061f059SPaolo Bonzini kernel = g_malloc(kernel_size); 897b061f059SPaolo Bonzini fseek(f, 0, SEEK_SET); 898b061f059SPaolo Bonzini if (fread(setup, 1, setup_size, f) != setup_size) { 899b061f059SPaolo Bonzini fprintf(stderr, "fread() failed\n"); 900b061f059SPaolo Bonzini exit(1); 901b061f059SPaolo Bonzini } 902b061f059SPaolo Bonzini if (fread(kernel, 1, kernel_size, f) != kernel_size) { 903b061f059SPaolo Bonzini fprintf(stderr, "fread() failed\n"); 904b061f059SPaolo Bonzini exit(1); 905b061f059SPaolo Bonzini } 906b061f059SPaolo Bonzini fclose(f); 907b061f059SPaolo Bonzini 908b061f059SPaolo Bonzini /* append dtb to kernel */ 909b061f059SPaolo Bonzini if (dtb_filename) { 910b061f059SPaolo Bonzini if (protocol < 0x209) { 911b061f059SPaolo Bonzini fprintf(stderr, "qemu: Linux kernel too old to load a dtb\n"); 912b061f059SPaolo Bonzini exit(1); 913b061f059SPaolo Bonzini } 914b061f059SPaolo Bonzini 915b061f059SPaolo Bonzini dtb_size = get_image_size(dtb_filename); 916b061f059SPaolo Bonzini if (dtb_size <= 0) { 917b061f059SPaolo Bonzini fprintf(stderr, "qemu: error reading dtb %s: %s\n", 918b061f059SPaolo Bonzini dtb_filename, strerror(errno)); 919b061f059SPaolo Bonzini exit(1); 920b061f059SPaolo Bonzini } 921b061f059SPaolo Bonzini 922b061f059SPaolo Bonzini setup_data_offset = QEMU_ALIGN_UP(kernel_size, 16); 923b061f059SPaolo Bonzini kernel_size = setup_data_offset + sizeof(struct setup_data) + dtb_size; 924b061f059SPaolo Bonzini kernel = g_realloc(kernel, kernel_size); 925b061f059SPaolo Bonzini 926b061f059SPaolo Bonzini stq_p(header + 0x250, prot_addr + setup_data_offset); 927b061f059SPaolo Bonzini 928b061f059SPaolo Bonzini setup_data = (struct setup_data *)(kernel + setup_data_offset); 929b061f059SPaolo Bonzini setup_data->next = 0; 930b061f059SPaolo Bonzini setup_data->type = cpu_to_le32(SETUP_DTB); 931b061f059SPaolo Bonzini setup_data->len = cpu_to_le32(dtb_size); 932b061f059SPaolo Bonzini 933b061f059SPaolo Bonzini load_image_size(dtb_filename, setup_data->data, dtb_size); 934b061f059SPaolo Bonzini } 935b061f059SPaolo Bonzini 936b061f059SPaolo Bonzini /* 937b061f059SPaolo Bonzini * If we're starting an encrypted VM, it will be OVMF based, which uses the 938b061f059SPaolo Bonzini * efi stub for booting and doesn't require any values to be placed in the 939b061f059SPaolo Bonzini * kernel header. We therefore don't update the header so the hash of the 940b061f059SPaolo Bonzini * kernel on the other side of the fw_cfg interface matches the hash of the 941b061f059SPaolo Bonzini * file the user passed in. 942b061f059SPaolo Bonzini */ 943b061f059SPaolo Bonzini if (!sev_enabled()) { 944b061f059SPaolo Bonzini memcpy(setup, header, MIN(sizeof(header), setup_size)); 945b061f059SPaolo Bonzini } 946b061f059SPaolo Bonzini 947b061f059SPaolo Bonzini fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_ADDR, prot_addr); 948b061f059SPaolo Bonzini fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_SIZE, kernel_size); 949b061f059SPaolo Bonzini fw_cfg_add_bytes(fw_cfg, FW_CFG_KERNEL_DATA, kernel, kernel_size); 950b061f059SPaolo Bonzini sev_load_ctx.kernel_data = (char *)kernel; 951b061f059SPaolo Bonzini sev_load_ctx.kernel_size = kernel_size; 952b061f059SPaolo Bonzini 953b061f059SPaolo Bonzini fw_cfg_add_i32(fw_cfg, FW_CFG_SETUP_ADDR, real_addr); 954b061f059SPaolo Bonzini fw_cfg_add_i32(fw_cfg, FW_CFG_SETUP_SIZE, setup_size); 955b061f059SPaolo Bonzini fw_cfg_add_bytes(fw_cfg, FW_CFG_SETUP_DATA, setup, setup_size); 956b061f059SPaolo Bonzini sev_load_ctx.setup_data = (char *)setup; 957b061f059SPaolo Bonzini sev_load_ctx.setup_size = setup_size; 958b061f059SPaolo Bonzini 959b061f059SPaolo Bonzini if (sev_enabled()) { 960b061f059SPaolo Bonzini sev_add_kernel_loader_hashes(&sev_load_ctx, &error_fatal); 961b061f059SPaolo Bonzini } 962b061f059SPaolo Bonzini 963b061f059SPaolo Bonzini option_rom[nb_option_roms].bootindex = 0; 964b061f059SPaolo Bonzini option_rom[nb_option_roms].name = "linuxboot.bin"; 965b061f059SPaolo Bonzini if (linuxboot_dma_enabled && fw_cfg_dma_enabled(fw_cfg)) { 966b061f059SPaolo Bonzini option_rom[nb_option_roms].name = "linuxboot_dma.bin"; 967b061f059SPaolo Bonzini } 968b061f059SPaolo Bonzini nb_option_roms++; 969b061f059SPaolo Bonzini } 970b061f059SPaolo Bonzini 971b061f059SPaolo Bonzini void x86_isa_bios_init(MemoryRegion *isa_bios, MemoryRegion *isa_memory, 972b061f059SPaolo Bonzini MemoryRegion *bios, bool read_only) 973b061f059SPaolo Bonzini { 974b061f059SPaolo Bonzini uint64_t bios_size = memory_region_size(bios); 975b061f059SPaolo Bonzini uint64_t isa_bios_size = MIN(bios_size, 128 * KiB); 976b061f059SPaolo Bonzini 977b061f059SPaolo Bonzini memory_region_init_alias(isa_bios, NULL, "isa-bios", bios, 978b061f059SPaolo Bonzini bios_size - isa_bios_size, isa_bios_size); 979b061f059SPaolo Bonzini memory_region_add_subregion_overlap(isa_memory, 1 * MiB - isa_bios_size, 980b061f059SPaolo Bonzini isa_bios, 1); 981b061f059SPaolo Bonzini memory_region_set_readonly(isa_bios, read_only); 982b061f059SPaolo Bonzini } 983b061f059SPaolo Bonzini 984b061f059SPaolo Bonzini void x86_bios_rom_init(X86MachineState *x86ms, const char *default_firmware, 985b061f059SPaolo Bonzini MemoryRegion *rom_memory, bool isapc_ram_fw) 986b061f059SPaolo Bonzini { 987b061f059SPaolo Bonzini const char *bios_name; 988b061f059SPaolo Bonzini char *filename; 989b061f059SPaolo Bonzini int bios_size; 990b061f059SPaolo Bonzini ssize_t ret; 991b061f059SPaolo Bonzini 992b061f059SPaolo Bonzini /* BIOS load */ 993b061f059SPaolo Bonzini bios_name = MACHINE(x86ms)->firmware ?: default_firmware; 994b061f059SPaolo Bonzini filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name); 995b061f059SPaolo Bonzini if (filename) { 996b061f059SPaolo Bonzini bios_size = get_image_size(filename); 997b061f059SPaolo Bonzini } else { 998b061f059SPaolo Bonzini bios_size = -1; 999b061f059SPaolo Bonzini } 1000b061f059SPaolo Bonzini if (bios_size <= 0 || 1001b061f059SPaolo Bonzini (bios_size % 65536) != 0) { 1002b061f059SPaolo Bonzini goto bios_error; 1003b061f059SPaolo Bonzini } 1004b061f059SPaolo Bonzini memory_region_init_ram(&x86ms->bios, NULL, "pc.bios", bios_size, 1005b061f059SPaolo Bonzini &error_fatal); 1006b061f059SPaolo Bonzini if (sev_enabled()) { 1007b061f059SPaolo Bonzini /* 1008b061f059SPaolo Bonzini * The concept of a "reset" simply doesn't exist for 1009b061f059SPaolo Bonzini * confidential computing guests, we have to destroy and 1010b061f059SPaolo Bonzini * re-launch them instead. So there is no need to register 1011b061f059SPaolo Bonzini * the firmware as rom to properly re-initialize on reset. 1012b061f059SPaolo Bonzini * Just go for a straight file load instead. 1013b061f059SPaolo Bonzini */ 1014b061f059SPaolo Bonzini void *ptr = memory_region_get_ram_ptr(&x86ms->bios); 1015b061f059SPaolo Bonzini load_image_size(filename, ptr, bios_size); 1016b061f059SPaolo Bonzini x86_firmware_configure(ptr, bios_size); 1017b061f059SPaolo Bonzini } else { 1018b061f059SPaolo Bonzini memory_region_set_readonly(&x86ms->bios, !isapc_ram_fw); 1019b061f059SPaolo Bonzini ret = rom_add_file_fixed(bios_name, (uint32_t)(-bios_size), -1); 1020b061f059SPaolo Bonzini if (ret != 0) { 1021b061f059SPaolo Bonzini goto bios_error; 1022b061f059SPaolo Bonzini } 1023b061f059SPaolo Bonzini } 1024b061f059SPaolo Bonzini g_free(filename); 1025b061f059SPaolo Bonzini 1026b061f059SPaolo Bonzini /* map the last 128KB of the BIOS in ISA space */ 1027b061f059SPaolo Bonzini x86_isa_bios_init(&x86ms->isa_bios, rom_memory, &x86ms->bios, 1028b061f059SPaolo Bonzini !isapc_ram_fw); 1029b061f059SPaolo Bonzini 1030b061f059SPaolo Bonzini /* map all the bios at the top of memory */ 1031b061f059SPaolo Bonzini memory_region_add_subregion(rom_memory, 1032b061f059SPaolo Bonzini (uint32_t)(-bios_size), 1033b061f059SPaolo Bonzini &x86ms->bios); 1034b061f059SPaolo Bonzini return; 1035b061f059SPaolo Bonzini 1036b061f059SPaolo Bonzini bios_error: 1037b061f059SPaolo Bonzini fprintf(stderr, "qemu: could not load PC BIOS '%s'\n", bios_name); 1038b061f059SPaolo Bonzini exit(1); 1039b061f059SPaolo Bonzini } 1040