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; 276*5304873aSZhao 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, 286b061f059SPaolo Bonzini * set it based on socket/die/core/thread properties. 287b061f059SPaolo Bonzini */ 288b061f059SPaolo Bonzini if (cpu->apic_id == UNASSIGNED_APIC_ID) { 289b061f059SPaolo Bonzini int max_socket = (ms->smp.max_cpus - 1) / 290b061f059SPaolo Bonzini smp_threads / smp_cores / ms->smp.dies; 291b061f059SPaolo Bonzini 292b061f059SPaolo Bonzini /* 293b061f059SPaolo Bonzini * die-id was optional in QEMU 4.0 and older, so keep it optional 294b061f059SPaolo Bonzini * if there's only one die per socket. 295b061f059SPaolo Bonzini */ 296b061f059SPaolo Bonzini if (cpu->die_id < 0 && ms->smp.dies == 1) { 297b061f059SPaolo Bonzini cpu->die_id = 0; 298b061f059SPaolo Bonzini } 299b061f059SPaolo Bonzini 300b061f059SPaolo Bonzini if (cpu->socket_id < 0) { 301b061f059SPaolo Bonzini error_setg(errp, "CPU socket-id is not set"); 302b061f059SPaolo Bonzini return; 303b061f059SPaolo Bonzini } else if (cpu->socket_id > max_socket) { 304b061f059SPaolo Bonzini error_setg(errp, "Invalid CPU socket-id: %u must be in range 0:%u", 305b061f059SPaolo Bonzini cpu->socket_id, max_socket); 306b061f059SPaolo Bonzini return; 307b061f059SPaolo Bonzini } 308b061f059SPaolo Bonzini if (cpu->die_id < 0) { 309b061f059SPaolo Bonzini error_setg(errp, "CPU die-id is not set"); 310b061f059SPaolo Bonzini return; 311b061f059SPaolo Bonzini } else if (cpu->die_id > ms->smp.dies - 1) { 312b061f059SPaolo Bonzini error_setg(errp, "Invalid CPU die-id: %u must be in range 0:%u", 313b061f059SPaolo Bonzini cpu->die_id, ms->smp.dies - 1); 314b061f059SPaolo Bonzini return; 315b061f059SPaolo Bonzini } 316b061f059SPaolo Bonzini if (cpu->core_id < 0) { 317b061f059SPaolo Bonzini error_setg(errp, "CPU core-id is not set"); 318b061f059SPaolo Bonzini return; 319b061f059SPaolo Bonzini } else if (cpu->core_id > (smp_cores - 1)) { 320b061f059SPaolo Bonzini error_setg(errp, "Invalid CPU core-id: %u must be in range 0:%u", 321b061f059SPaolo Bonzini cpu->core_id, smp_cores - 1); 322b061f059SPaolo Bonzini return; 323b061f059SPaolo Bonzini } 324b061f059SPaolo Bonzini if (cpu->thread_id < 0) { 325b061f059SPaolo Bonzini error_setg(errp, "CPU thread-id is not set"); 326b061f059SPaolo Bonzini return; 327b061f059SPaolo Bonzini } else if (cpu->thread_id > (smp_threads - 1)) { 328b061f059SPaolo Bonzini error_setg(errp, "Invalid CPU thread-id: %u must be in range 0:%u", 329b061f059SPaolo Bonzini cpu->thread_id, smp_threads - 1); 330b061f059SPaolo Bonzini return; 331b061f059SPaolo Bonzini } 332b061f059SPaolo Bonzini 333b061f059SPaolo Bonzini topo_ids.pkg_id = cpu->socket_id; 334b061f059SPaolo Bonzini topo_ids.die_id = cpu->die_id; 335b061f059SPaolo Bonzini topo_ids.core_id = cpu->core_id; 336b061f059SPaolo Bonzini topo_ids.smt_id = cpu->thread_id; 337b061f059SPaolo Bonzini cpu->apic_id = x86_apicid_from_topo_ids(&topo_info, &topo_ids); 338b061f059SPaolo Bonzini } 339b061f059SPaolo Bonzini 340b061f059SPaolo Bonzini cpu_slot = x86_find_cpu_slot(MACHINE(x86ms), cpu->apic_id, &idx); 341b061f059SPaolo Bonzini if (!cpu_slot) { 342b061f059SPaolo Bonzini x86_topo_ids_from_apicid(cpu->apic_id, &topo_info, &topo_ids); 343b061f059SPaolo Bonzini error_setg(errp, 344b061f059SPaolo Bonzini "Invalid CPU [socket: %u, die: %u, core: %u, thread: %u] with" 345b061f059SPaolo Bonzini " APIC ID %" PRIu32 ", valid index range 0:%d", 346b061f059SPaolo Bonzini topo_ids.pkg_id, topo_ids.die_id, topo_ids.core_id, topo_ids.smt_id, 347b061f059SPaolo Bonzini cpu->apic_id, ms->possible_cpus->len - 1); 348b061f059SPaolo Bonzini return; 349b061f059SPaolo Bonzini } 350b061f059SPaolo Bonzini 351b061f059SPaolo Bonzini if (cpu_slot->cpu) { 352b061f059SPaolo Bonzini error_setg(errp, "CPU[%d] with APIC ID %" PRIu32 " exists", 353b061f059SPaolo Bonzini idx, cpu->apic_id); 354b061f059SPaolo Bonzini return; 355b061f059SPaolo Bonzini } 356b061f059SPaolo Bonzini 357b061f059SPaolo Bonzini /* if 'address' properties socket-id/core-id/thread-id are not set, set them 358b061f059SPaolo Bonzini * so that machine_query_hotpluggable_cpus would show correct values 359b061f059SPaolo Bonzini */ 360b061f059SPaolo Bonzini /* TODO: move socket_id/core_id/thread_id checks into x86_cpu_realizefn() 361b061f059SPaolo Bonzini * once -smp refactoring is complete and there will be CPU private 362b061f059SPaolo Bonzini * CPUState::nr_cores and CPUState::nr_threads fields instead of globals */ 363b061f059SPaolo Bonzini x86_topo_ids_from_apicid(cpu->apic_id, &topo_info, &topo_ids); 364b061f059SPaolo Bonzini if (cpu->socket_id != -1 && cpu->socket_id != topo_ids.pkg_id) { 365b061f059SPaolo Bonzini error_setg(errp, "property socket-id: %u doesn't match set apic-id:" 366b061f059SPaolo Bonzini " 0x%x (socket-id: %u)", cpu->socket_id, cpu->apic_id, 367b061f059SPaolo Bonzini topo_ids.pkg_id); 368b061f059SPaolo Bonzini return; 369b061f059SPaolo Bonzini } 370b061f059SPaolo Bonzini cpu->socket_id = topo_ids.pkg_id; 371b061f059SPaolo Bonzini 372b061f059SPaolo Bonzini if (cpu->die_id != -1 && cpu->die_id != topo_ids.die_id) { 373b061f059SPaolo Bonzini error_setg(errp, "property die-id: %u doesn't match set apic-id:" 374b061f059SPaolo Bonzini " 0x%x (die-id: %u)", cpu->die_id, cpu->apic_id, topo_ids.die_id); 375b061f059SPaolo Bonzini return; 376b061f059SPaolo Bonzini } 377b061f059SPaolo Bonzini cpu->die_id = topo_ids.die_id; 378b061f059SPaolo Bonzini 379b061f059SPaolo Bonzini if (cpu->core_id != -1 && cpu->core_id != topo_ids.core_id) { 380b061f059SPaolo Bonzini error_setg(errp, "property core-id: %u doesn't match set apic-id:" 381b061f059SPaolo Bonzini " 0x%x (core-id: %u)", cpu->core_id, cpu->apic_id, 382b061f059SPaolo Bonzini topo_ids.core_id); 383b061f059SPaolo Bonzini return; 384b061f059SPaolo Bonzini } 385b061f059SPaolo Bonzini cpu->core_id = topo_ids.core_id; 386b061f059SPaolo Bonzini 387b061f059SPaolo Bonzini if (cpu->thread_id != -1 && cpu->thread_id != topo_ids.smt_id) { 388b061f059SPaolo Bonzini error_setg(errp, "property thread-id: %u doesn't match set apic-id:" 389b061f059SPaolo Bonzini " 0x%x (thread-id: %u)", cpu->thread_id, cpu->apic_id, 390b061f059SPaolo Bonzini topo_ids.smt_id); 391b061f059SPaolo Bonzini return; 392b061f059SPaolo Bonzini } 393b061f059SPaolo Bonzini cpu->thread_id = topo_ids.smt_id; 394b061f059SPaolo Bonzini 395b061f059SPaolo Bonzini /* 396b061f059SPaolo Bonzini * kvm_enabled() must go first to ensure that kvm_* references are 397b061f059SPaolo Bonzini * not emitted for the linker to consume (kvm_enabled() is 398b061f059SPaolo Bonzini * a literal `0` in configurations where kvm_* aren't defined) 399b061f059SPaolo Bonzini */ 400b061f059SPaolo Bonzini if (kvm_enabled() && hyperv_feat_enabled(cpu, HYPERV_FEAT_VPINDEX) && 401b061f059SPaolo Bonzini !kvm_hv_vpindex_settable()) { 402b061f059SPaolo Bonzini error_setg(errp, "kernel doesn't allow setting HyperV VP_INDEX"); 403b061f059SPaolo Bonzini return; 404b061f059SPaolo Bonzini } 405b061f059SPaolo Bonzini 406b061f059SPaolo Bonzini cs = CPU(cpu); 407b061f059SPaolo Bonzini cs->cpu_index = idx; 408b061f059SPaolo Bonzini 409b061f059SPaolo Bonzini numa_cpu_pre_plug(cpu_slot, dev, errp); 410b061f059SPaolo Bonzini } 411b061f059SPaolo Bonzini 412b061f059SPaolo Bonzini static long get_file_size(FILE *f) 413b061f059SPaolo Bonzini { 414b061f059SPaolo Bonzini long where, size; 415b061f059SPaolo Bonzini 416b061f059SPaolo Bonzini /* XXX: on Unix systems, using fstat() probably makes more sense */ 417b061f059SPaolo Bonzini 418b061f059SPaolo Bonzini where = ftell(f); 419b061f059SPaolo Bonzini fseek(f, 0, SEEK_END); 420b061f059SPaolo Bonzini size = ftell(f); 421b061f059SPaolo Bonzini fseek(f, where, SEEK_SET); 422b061f059SPaolo Bonzini 423b061f059SPaolo Bonzini return size; 424b061f059SPaolo Bonzini } 425b061f059SPaolo Bonzini 426b061f059SPaolo Bonzini void gsi_handler(void *opaque, int n, int level) 427b061f059SPaolo Bonzini { 428b061f059SPaolo Bonzini GSIState *s = opaque; 429b061f059SPaolo Bonzini 430b061f059SPaolo Bonzini trace_x86_gsi_interrupt(n, level); 431b061f059SPaolo Bonzini switch (n) { 432b061f059SPaolo Bonzini case 0 ... ISA_NUM_IRQS - 1: 433b061f059SPaolo Bonzini if (s->i8259_irq[n]) { 434b061f059SPaolo Bonzini /* Under KVM, Kernel will forward to both PIC and IOAPIC */ 435b061f059SPaolo Bonzini qemu_set_irq(s->i8259_irq[n], level); 436b061f059SPaolo Bonzini } 437b061f059SPaolo Bonzini /* fall through */ 438b061f059SPaolo Bonzini case ISA_NUM_IRQS ... IOAPIC_NUM_PINS - 1: 439b061f059SPaolo Bonzini #ifdef CONFIG_XEN_EMU 440b061f059SPaolo Bonzini /* 441b061f059SPaolo Bonzini * Xen delivers the GSI to the Legacy PIC (not that Legacy PIC 442b061f059SPaolo Bonzini * routing actually works properly under Xen). And then to 443b061f059SPaolo Bonzini * *either* the PIRQ handling or the I/OAPIC depending on 444b061f059SPaolo Bonzini * whether the former wants it. 445b061f059SPaolo Bonzini */ 446b061f059SPaolo Bonzini if (xen_mode == XEN_EMULATE && xen_evtchn_set_gsi(n, level)) { 447b061f059SPaolo Bonzini break; 448b061f059SPaolo Bonzini } 449b061f059SPaolo Bonzini #endif 450b061f059SPaolo Bonzini qemu_set_irq(s->ioapic_irq[n], level); 451b061f059SPaolo Bonzini break; 452b061f059SPaolo Bonzini case IO_APIC_SECONDARY_IRQBASE 453b061f059SPaolo Bonzini ... IO_APIC_SECONDARY_IRQBASE + IOAPIC_NUM_PINS - 1: 454b061f059SPaolo Bonzini qemu_set_irq(s->ioapic2_irq[n - IO_APIC_SECONDARY_IRQBASE], level); 455b061f059SPaolo Bonzini break; 456b061f059SPaolo Bonzini } 457b061f059SPaolo Bonzini } 458b061f059SPaolo Bonzini 459b061f059SPaolo Bonzini void ioapic_init_gsi(GSIState *gsi_state, Object *parent) 460b061f059SPaolo Bonzini { 461b061f059SPaolo Bonzini DeviceState *dev; 462b061f059SPaolo Bonzini SysBusDevice *d; 463b061f059SPaolo Bonzini unsigned int i; 464b061f059SPaolo Bonzini 465b061f059SPaolo Bonzini assert(parent); 466b061f059SPaolo Bonzini if (kvm_ioapic_in_kernel()) { 467b061f059SPaolo Bonzini dev = qdev_new(TYPE_KVM_IOAPIC); 468b061f059SPaolo Bonzini } else { 469b061f059SPaolo Bonzini dev = qdev_new(TYPE_IOAPIC); 470b061f059SPaolo Bonzini } 471b061f059SPaolo Bonzini object_property_add_child(parent, "ioapic", OBJECT(dev)); 472b061f059SPaolo Bonzini d = SYS_BUS_DEVICE(dev); 473b061f059SPaolo Bonzini sysbus_realize_and_unref(d, &error_fatal); 474b061f059SPaolo Bonzini sysbus_mmio_map(d, 0, IO_APIC_DEFAULT_ADDRESS); 475b061f059SPaolo Bonzini 476b061f059SPaolo Bonzini for (i = 0; i < IOAPIC_NUM_PINS; i++) { 477b061f059SPaolo Bonzini gsi_state->ioapic_irq[i] = qdev_get_gpio_in(dev, i); 478b061f059SPaolo Bonzini } 479b061f059SPaolo Bonzini } 480b061f059SPaolo Bonzini 481b061f059SPaolo Bonzini DeviceState *ioapic_init_secondary(GSIState *gsi_state) 482b061f059SPaolo Bonzini { 483b061f059SPaolo Bonzini DeviceState *dev; 484b061f059SPaolo Bonzini SysBusDevice *d; 485b061f059SPaolo Bonzini unsigned int i; 486b061f059SPaolo Bonzini 487b061f059SPaolo Bonzini dev = qdev_new(TYPE_IOAPIC); 488b061f059SPaolo Bonzini d = SYS_BUS_DEVICE(dev); 489b061f059SPaolo Bonzini sysbus_realize_and_unref(d, &error_fatal); 490b061f059SPaolo Bonzini sysbus_mmio_map(d, 0, IO_APIC_SECONDARY_ADDRESS); 491b061f059SPaolo Bonzini 492b061f059SPaolo Bonzini for (i = 0; i < IOAPIC_NUM_PINS; i++) { 493b061f059SPaolo Bonzini gsi_state->ioapic2_irq[i] = qdev_get_gpio_in(dev, i); 494b061f059SPaolo Bonzini } 495b061f059SPaolo Bonzini return dev; 496b061f059SPaolo Bonzini } 497b061f059SPaolo Bonzini 498b061f059SPaolo Bonzini /* 499b061f059SPaolo Bonzini * The entry point into the kernel for PVH boot is different from 500b061f059SPaolo Bonzini * the native entry point. The PVH entry is defined by the x86/HVM 501b061f059SPaolo Bonzini * direct boot ABI and is available in an ELFNOTE in the kernel binary. 502b061f059SPaolo Bonzini * 503b061f059SPaolo Bonzini * This function is passed to load_elf() when it is called from 504b061f059SPaolo Bonzini * load_elfboot() which then additionally checks for an ELF Note of 505b061f059SPaolo Bonzini * type XEN_ELFNOTE_PHYS32_ENTRY and passes it to this function to 506b061f059SPaolo Bonzini * parse the PVH entry address from the ELF Note. 507b061f059SPaolo Bonzini * 508b061f059SPaolo Bonzini * Due to trickery in elf_opts.h, load_elf() is actually available as 509b061f059SPaolo Bonzini * load_elf32() or load_elf64() and this routine needs to be able 510b061f059SPaolo Bonzini * to deal with being called as 32 or 64 bit. 511b061f059SPaolo Bonzini * 512b061f059SPaolo Bonzini * The address of the PVH entry point is saved to the 'pvh_start_addr' 513b061f059SPaolo Bonzini * global variable. (although the entry point is 32-bit, the kernel 514b061f059SPaolo Bonzini * binary can be either 32-bit or 64-bit). 515b061f059SPaolo Bonzini */ 516b061f059SPaolo Bonzini static uint64_t read_pvh_start_addr(void *arg1, void *arg2, bool is64) 517b061f059SPaolo Bonzini { 518b061f059SPaolo Bonzini size_t *elf_note_data_addr; 519b061f059SPaolo Bonzini 520b061f059SPaolo Bonzini /* Check if ELF Note header passed in is valid */ 521b061f059SPaolo Bonzini if (arg1 == NULL) { 522b061f059SPaolo Bonzini return 0; 523b061f059SPaolo Bonzini } 524b061f059SPaolo Bonzini 525b061f059SPaolo Bonzini if (is64) { 526b061f059SPaolo Bonzini struct elf64_note *nhdr64 = (struct elf64_note *)arg1; 527b061f059SPaolo Bonzini uint64_t nhdr_size64 = sizeof(struct elf64_note); 528b061f059SPaolo Bonzini uint64_t phdr_align = *(uint64_t *)arg2; 529b061f059SPaolo Bonzini uint64_t nhdr_namesz = nhdr64->n_namesz; 530b061f059SPaolo Bonzini 531b061f059SPaolo Bonzini elf_note_data_addr = 532b061f059SPaolo Bonzini ((void *)nhdr64) + nhdr_size64 + 533b061f059SPaolo Bonzini QEMU_ALIGN_UP(nhdr_namesz, phdr_align); 534b061f059SPaolo Bonzini 535b061f059SPaolo Bonzini pvh_start_addr = *elf_note_data_addr; 536b061f059SPaolo Bonzini } else { 537b061f059SPaolo Bonzini struct elf32_note *nhdr32 = (struct elf32_note *)arg1; 538b061f059SPaolo Bonzini uint32_t nhdr_size32 = sizeof(struct elf32_note); 539b061f059SPaolo Bonzini uint32_t phdr_align = *(uint32_t *)arg2; 540b061f059SPaolo Bonzini uint32_t nhdr_namesz = nhdr32->n_namesz; 541b061f059SPaolo Bonzini 542b061f059SPaolo Bonzini elf_note_data_addr = 543b061f059SPaolo Bonzini ((void *)nhdr32) + nhdr_size32 + 544b061f059SPaolo Bonzini QEMU_ALIGN_UP(nhdr_namesz, phdr_align); 545b061f059SPaolo Bonzini 546b061f059SPaolo Bonzini pvh_start_addr = *(uint32_t *)elf_note_data_addr; 547b061f059SPaolo Bonzini } 548b061f059SPaolo Bonzini 549b061f059SPaolo Bonzini return pvh_start_addr; 550b061f059SPaolo Bonzini } 551b061f059SPaolo Bonzini 552b061f059SPaolo Bonzini static bool load_elfboot(const char *kernel_filename, 553b061f059SPaolo Bonzini int kernel_file_size, 554b061f059SPaolo Bonzini uint8_t *header, 555b061f059SPaolo Bonzini size_t pvh_xen_start_addr, 556b061f059SPaolo Bonzini FWCfgState *fw_cfg) 557b061f059SPaolo Bonzini { 558b061f059SPaolo Bonzini uint32_t flags = 0; 559b061f059SPaolo Bonzini uint32_t mh_load_addr = 0; 560b061f059SPaolo Bonzini uint32_t elf_kernel_size = 0; 561b061f059SPaolo Bonzini uint64_t elf_entry; 562b061f059SPaolo Bonzini uint64_t elf_low, elf_high; 563b061f059SPaolo Bonzini int kernel_size; 564b061f059SPaolo Bonzini 565b061f059SPaolo Bonzini if (ldl_p(header) != 0x464c457f) { 566b061f059SPaolo Bonzini return false; /* no elfboot */ 567b061f059SPaolo Bonzini } 568b061f059SPaolo Bonzini 569b061f059SPaolo Bonzini bool elf_is64 = header[EI_CLASS] == ELFCLASS64; 570b061f059SPaolo Bonzini flags = elf_is64 ? 571b061f059SPaolo Bonzini ((Elf64_Ehdr *)header)->e_flags : ((Elf32_Ehdr *)header)->e_flags; 572b061f059SPaolo Bonzini 573b061f059SPaolo Bonzini if (flags & 0x00010004) { /* LOAD_ELF_HEADER_HAS_ADDR */ 574b061f059SPaolo Bonzini error_report("elfboot unsupported flags = %x", flags); 575b061f059SPaolo Bonzini exit(1); 576b061f059SPaolo Bonzini } 577b061f059SPaolo Bonzini 578b061f059SPaolo Bonzini uint64_t elf_note_type = XEN_ELFNOTE_PHYS32_ENTRY; 579b061f059SPaolo Bonzini kernel_size = load_elf(kernel_filename, read_pvh_start_addr, 580b061f059SPaolo Bonzini NULL, &elf_note_type, &elf_entry, 581b061f059SPaolo Bonzini &elf_low, &elf_high, NULL, 0, I386_ELF_MACHINE, 582b061f059SPaolo Bonzini 0, 0); 583b061f059SPaolo Bonzini 584b061f059SPaolo Bonzini if (kernel_size < 0) { 585b061f059SPaolo Bonzini error_report("Error while loading elf kernel"); 586b061f059SPaolo Bonzini exit(1); 587b061f059SPaolo Bonzini } 588b061f059SPaolo Bonzini mh_load_addr = elf_low; 589b061f059SPaolo Bonzini elf_kernel_size = elf_high - elf_low; 590b061f059SPaolo Bonzini 591b061f059SPaolo Bonzini if (pvh_start_addr == 0) { 592b061f059SPaolo Bonzini error_report("Error loading uncompressed kernel without PVH ELF Note"); 593b061f059SPaolo Bonzini exit(1); 594b061f059SPaolo Bonzini } 595b061f059SPaolo Bonzini fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_ENTRY, pvh_start_addr); 596b061f059SPaolo Bonzini fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_ADDR, mh_load_addr); 597b061f059SPaolo Bonzini fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_SIZE, elf_kernel_size); 598b061f059SPaolo Bonzini 599b061f059SPaolo Bonzini return true; 600b061f059SPaolo Bonzini } 601b061f059SPaolo Bonzini 602b061f059SPaolo Bonzini void x86_load_linux(X86MachineState *x86ms, 603b061f059SPaolo Bonzini FWCfgState *fw_cfg, 604b061f059SPaolo Bonzini int acpi_data_size, 605b061f059SPaolo Bonzini bool pvh_enabled) 606b061f059SPaolo Bonzini { 607b061f059SPaolo Bonzini bool linuxboot_dma_enabled = X86_MACHINE_GET_CLASS(x86ms)->fwcfg_dma_enabled; 608b061f059SPaolo Bonzini uint16_t protocol; 609b061f059SPaolo Bonzini int setup_size, kernel_size, cmdline_size; 610b061f059SPaolo Bonzini int dtb_size, setup_data_offset; 611b061f059SPaolo Bonzini uint32_t initrd_max; 612b061f059SPaolo Bonzini uint8_t header[8192], *setup, *kernel; 613b061f059SPaolo Bonzini hwaddr real_addr, prot_addr, cmdline_addr, initrd_addr = 0; 614b061f059SPaolo Bonzini FILE *f; 615b061f059SPaolo Bonzini char *vmode; 616b061f059SPaolo Bonzini MachineState *machine = MACHINE(x86ms); 617b061f059SPaolo Bonzini struct setup_data *setup_data; 618b061f059SPaolo Bonzini const char *kernel_filename = machine->kernel_filename; 619b061f059SPaolo Bonzini const char *initrd_filename = machine->initrd_filename; 620b061f059SPaolo Bonzini const char *dtb_filename = machine->dtb; 621b061f059SPaolo Bonzini const char *kernel_cmdline = machine->kernel_cmdline; 622b061f059SPaolo Bonzini SevKernelLoaderContext sev_load_ctx = {}; 623b061f059SPaolo Bonzini 624b061f059SPaolo Bonzini /* Align to 16 bytes as a paranoia measure */ 625b061f059SPaolo Bonzini cmdline_size = (strlen(kernel_cmdline) + 16) & ~15; 626b061f059SPaolo Bonzini 627b061f059SPaolo Bonzini /* load the kernel header */ 628b061f059SPaolo Bonzini f = fopen(kernel_filename, "rb"); 629b061f059SPaolo Bonzini if (!f) { 630b061f059SPaolo Bonzini fprintf(stderr, "qemu: could not open kernel file '%s': %s\n", 631b061f059SPaolo Bonzini kernel_filename, strerror(errno)); 632b061f059SPaolo Bonzini exit(1); 633b061f059SPaolo Bonzini } 634b061f059SPaolo Bonzini 635b061f059SPaolo Bonzini kernel_size = get_file_size(f); 636b061f059SPaolo Bonzini if (!kernel_size || 637b061f059SPaolo Bonzini fread(header, 1, MIN(ARRAY_SIZE(header), kernel_size), f) != 638b061f059SPaolo Bonzini MIN(ARRAY_SIZE(header), kernel_size)) { 639b061f059SPaolo Bonzini fprintf(stderr, "qemu: could not load kernel '%s': %s\n", 640b061f059SPaolo Bonzini kernel_filename, strerror(errno)); 641b061f059SPaolo Bonzini exit(1); 642b061f059SPaolo Bonzini } 643b061f059SPaolo Bonzini 644b061f059SPaolo Bonzini /* kernel protocol version */ 645b061f059SPaolo Bonzini if (ldl_p(header + 0x202) == 0x53726448) { 646b061f059SPaolo Bonzini protocol = lduw_p(header + 0x206); 647b061f059SPaolo Bonzini } else { 648b061f059SPaolo Bonzini /* 649b061f059SPaolo Bonzini * This could be a multiboot kernel. If it is, let's stop treating it 650b061f059SPaolo Bonzini * like a Linux kernel. 651b061f059SPaolo Bonzini * Note: some multiboot images could be in the ELF format (the same of 652b061f059SPaolo Bonzini * PVH), so we try multiboot first since we check the multiboot magic 653b061f059SPaolo Bonzini * header before to load it. 654b061f059SPaolo Bonzini */ 655b061f059SPaolo Bonzini if (load_multiboot(x86ms, fw_cfg, f, kernel_filename, initrd_filename, 656b061f059SPaolo Bonzini kernel_cmdline, kernel_size, header)) { 657b061f059SPaolo Bonzini return; 658b061f059SPaolo Bonzini } 659b061f059SPaolo Bonzini /* 660b061f059SPaolo Bonzini * Check if the file is an uncompressed kernel file (ELF) and load it, 661b061f059SPaolo Bonzini * saving the PVH entry point used by the x86/HVM direct boot ABI. 662b061f059SPaolo Bonzini * If load_elfboot() is successful, populate the fw_cfg info. 663b061f059SPaolo Bonzini */ 664b061f059SPaolo Bonzini if (pvh_enabled && 665b061f059SPaolo Bonzini load_elfboot(kernel_filename, kernel_size, 666b061f059SPaolo Bonzini header, pvh_start_addr, fw_cfg)) { 667b061f059SPaolo Bonzini fclose(f); 668b061f059SPaolo Bonzini 669b061f059SPaolo Bonzini fw_cfg_add_i32(fw_cfg, FW_CFG_CMDLINE_SIZE, 670b061f059SPaolo Bonzini strlen(kernel_cmdline) + 1); 671b061f059SPaolo Bonzini fw_cfg_add_string(fw_cfg, FW_CFG_CMDLINE_DATA, kernel_cmdline); 672b061f059SPaolo Bonzini 673b061f059SPaolo Bonzini fw_cfg_add_i32(fw_cfg, FW_CFG_SETUP_SIZE, sizeof(header)); 674b061f059SPaolo Bonzini fw_cfg_add_bytes(fw_cfg, FW_CFG_SETUP_DATA, 675b061f059SPaolo Bonzini header, sizeof(header)); 676b061f059SPaolo Bonzini 677b061f059SPaolo Bonzini /* load initrd */ 678b061f059SPaolo Bonzini if (initrd_filename) { 679b061f059SPaolo Bonzini GMappedFile *mapped_file; 680b061f059SPaolo Bonzini gsize initrd_size; 681b061f059SPaolo Bonzini gchar *initrd_data; 682b061f059SPaolo Bonzini GError *gerr = NULL; 683b061f059SPaolo Bonzini 684b061f059SPaolo Bonzini mapped_file = g_mapped_file_new(initrd_filename, false, &gerr); 685b061f059SPaolo Bonzini if (!mapped_file) { 686b061f059SPaolo Bonzini fprintf(stderr, "qemu: error reading initrd %s: %s\n", 687b061f059SPaolo Bonzini initrd_filename, gerr->message); 688b061f059SPaolo Bonzini exit(1); 689b061f059SPaolo Bonzini } 690b061f059SPaolo Bonzini x86ms->initrd_mapped_file = mapped_file; 691b061f059SPaolo Bonzini 692b061f059SPaolo Bonzini initrd_data = g_mapped_file_get_contents(mapped_file); 693b061f059SPaolo Bonzini initrd_size = g_mapped_file_get_length(mapped_file); 694b061f059SPaolo Bonzini initrd_max = x86ms->below_4g_mem_size - acpi_data_size - 1; 695b061f059SPaolo Bonzini if (initrd_size >= initrd_max) { 696b061f059SPaolo Bonzini fprintf(stderr, "qemu: initrd is too large, cannot support." 697b061f059SPaolo Bonzini "(max: %"PRIu32", need %"PRId64")\n", 698b061f059SPaolo Bonzini initrd_max, (uint64_t)initrd_size); 699b061f059SPaolo Bonzini exit(1); 700b061f059SPaolo Bonzini } 701b061f059SPaolo Bonzini 702b061f059SPaolo Bonzini initrd_addr = (initrd_max - initrd_size) & ~4095; 703b061f059SPaolo Bonzini 704b061f059SPaolo Bonzini fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_ADDR, initrd_addr); 705b061f059SPaolo Bonzini fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_SIZE, initrd_size); 706b061f059SPaolo Bonzini fw_cfg_add_bytes(fw_cfg, FW_CFG_INITRD_DATA, initrd_data, 707b061f059SPaolo Bonzini initrd_size); 708b061f059SPaolo Bonzini } 709b061f059SPaolo Bonzini 710b061f059SPaolo Bonzini option_rom[nb_option_roms].bootindex = 0; 711b061f059SPaolo Bonzini option_rom[nb_option_roms].name = "pvh.bin"; 712b061f059SPaolo Bonzini nb_option_roms++; 713b061f059SPaolo Bonzini 714b061f059SPaolo Bonzini return; 715b061f059SPaolo Bonzini } 716b061f059SPaolo Bonzini protocol = 0; 717b061f059SPaolo Bonzini } 718b061f059SPaolo Bonzini 719b061f059SPaolo Bonzini if (protocol < 0x200 || !(header[0x211] & 0x01)) { 720b061f059SPaolo Bonzini /* Low kernel */ 721b061f059SPaolo Bonzini real_addr = 0x90000; 722b061f059SPaolo Bonzini cmdline_addr = 0x9a000 - cmdline_size; 723b061f059SPaolo Bonzini prot_addr = 0x10000; 724b061f059SPaolo Bonzini } else if (protocol < 0x202) { 725b061f059SPaolo Bonzini /* High but ancient kernel */ 726b061f059SPaolo Bonzini real_addr = 0x90000; 727b061f059SPaolo Bonzini cmdline_addr = 0x9a000 - cmdline_size; 728b061f059SPaolo Bonzini prot_addr = 0x100000; 729b061f059SPaolo Bonzini } else { 730b061f059SPaolo Bonzini /* High and recent kernel */ 731b061f059SPaolo Bonzini real_addr = 0x10000; 732b061f059SPaolo Bonzini cmdline_addr = 0x20000; 733b061f059SPaolo Bonzini prot_addr = 0x100000; 734b061f059SPaolo Bonzini } 735b061f059SPaolo Bonzini 736b061f059SPaolo Bonzini /* highest address for loading the initrd */ 737b061f059SPaolo Bonzini if (protocol >= 0x20c && 738b061f059SPaolo Bonzini lduw_p(header + 0x236) & XLF_CAN_BE_LOADED_ABOVE_4G) { 739b061f059SPaolo Bonzini /* 740b061f059SPaolo Bonzini * Linux has supported initrd up to 4 GB for a very long time (2007, 741b061f059SPaolo Bonzini * long before XLF_CAN_BE_LOADED_ABOVE_4G which was added in 2013), 742b061f059SPaolo Bonzini * though it only sets initrd_max to 2 GB to "work around bootloader 743b061f059SPaolo Bonzini * bugs". Luckily, QEMU firmware(which does something like bootloader) 744b061f059SPaolo Bonzini * has supported this. 745b061f059SPaolo Bonzini * 746b061f059SPaolo Bonzini * It's believed that if XLF_CAN_BE_LOADED_ABOVE_4G is set, initrd can 747b061f059SPaolo Bonzini * be loaded into any address. 748b061f059SPaolo Bonzini * 749b061f059SPaolo Bonzini * In addition, initrd_max is uint32_t simply because QEMU doesn't 750b061f059SPaolo Bonzini * support the 64-bit boot protocol (specifically the ext_ramdisk_image 751b061f059SPaolo Bonzini * field). 752b061f059SPaolo Bonzini * 753b061f059SPaolo Bonzini * Therefore here just limit initrd_max to UINT32_MAX simply as well. 754b061f059SPaolo Bonzini */ 755b061f059SPaolo Bonzini initrd_max = UINT32_MAX; 756b061f059SPaolo Bonzini } else if (protocol >= 0x203) { 757b061f059SPaolo Bonzini initrd_max = ldl_p(header + 0x22c); 758b061f059SPaolo Bonzini } else { 759b061f059SPaolo Bonzini initrd_max = 0x37ffffff; 760b061f059SPaolo Bonzini } 761b061f059SPaolo Bonzini 762b061f059SPaolo Bonzini if (initrd_max >= x86ms->below_4g_mem_size - acpi_data_size) { 763b061f059SPaolo Bonzini initrd_max = x86ms->below_4g_mem_size - acpi_data_size - 1; 764b061f059SPaolo Bonzini } 765b061f059SPaolo Bonzini 766b061f059SPaolo Bonzini fw_cfg_add_i32(fw_cfg, FW_CFG_CMDLINE_ADDR, cmdline_addr); 767b061f059SPaolo Bonzini fw_cfg_add_i32(fw_cfg, FW_CFG_CMDLINE_SIZE, strlen(kernel_cmdline) + 1); 768b061f059SPaolo Bonzini fw_cfg_add_string(fw_cfg, FW_CFG_CMDLINE_DATA, kernel_cmdline); 769b061f059SPaolo Bonzini sev_load_ctx.cmdline_data = (char *)kernel_cmdline; 770b061f059SPaolo Bonzini sev_load_ctx.cmdline_size = strlen(kernel_cmdline) + 1; 771b061f059SPaolo Bonzini 772b061f059SPaolo Bonzini if (protocol >= 0x202) { 773b061f059SPaolo Bonzini stl_p(header + 0x228, cmdline_addr); 774b061f059SPaolo Bonzini } else { 775b061f059SPaolo Bonzini stw_p(header + 0x20, 0xA33F); 776b061f059SPaolo Bonzini stw_p(header + 0x22, cmdline_addr - real_addr); 777b061f059SPaolo Bonzini } 778b061f059SPaolo Bonzini 779b061f059SPaolo Bonzini /* handle vga= parameter */ 780b061f059SPaolo Bonzini vmode = strstr(kernel_cmdline, "vga="); 781b061f059SPaolo Bonzini if (vmode) { 782b061f059SPaolo Bonzini unsigned int video_mode; 783b061f059SPaolo Bonzini const char *end; 784b061f059SPaolo Bonzini int ret; 785b061f059SPaolo Bonzini /* skip "vga=" */ 786b061f059SPaolo Bonzini vmode += 4; 787b061f059SPaolo Bonzini if (!strncmp(vmode, "normal", 6)) { 788b061f059SPaolo Bonzini video_mode = 0xffff; 789b061f059SPaolo Bonzini } else if (!strncmp(vmode, "ext", 3)) { 790b061f059SPaolo Bonzini video_mode = 0xfffe; 791b061f059SPaolo Bonzini } else if (!strncmp(vmode, "ask", 3)) { 792b061f059SPaolo Bonzini video_mode = 0xfffd; 793b061f059SPaolo Bonzini } else { 794b061f059SPaolo Bonzini ret = qemu_strtoui(vmode, &end, 0, &video_mode); 795b061f059SPaolo Bonzini if (ret != 0 || (*end && *end != ' ')) { 796b061f059SPaolo Bonzini fprintf(stderr, "qemu: invalid 'vga=' kernel parameter.\n"); 797b061f059SPaolo Bonzini exit(1); 798b061f059SPaolo Bonzini } 799b061f059SPaolo Bonzini } 800b061f059SPaolo Bonzini stw_p(header + 0x1fa, video_mode); 801b061f059SPaolo Bonzini } 802b061f059SPaolo Bonzini 803b061f059SPaolo Bonzini /* loader type */ 804b061f059SPaolo Bonzini /* 805b061f059SPaolo Bonzini * High nybble = B reserved for QEMU; low nybble is revision number. 806b061f059SPaolo Bonzini * If this code is substantially changed, you may want to consider 807b061f059SPaolo Bonzini * incrementing the revision. 808b061f059SPaolo Bonzini */ 809b061f059SPaolo Bonzini if (protocol >= 0x200) { 810b061f059SPaolo Bonzini header[0x210] = 0xB0; 811b061f059SPaolo Bonzini } 812b061f059SPaolo Bonzini /* heap */ 813b061f059SPaolo Bonzini if (protocol >= 0x201) { 814b061f059SPaolo Bonzini header[0x211] |= 0x80; /* CAN_USE_HEAP */ 815b061f059SPaolo Bonzini stw_p(header + 0x224, cmdline_addr - real_addr - 0x200); 816b061f059SPaolo Bonzini } 817b061f059SPaolo Bonzini 818b061f059SPaolo Bonzini /* load initrd */ 819b061f059SPaolo Bonzini if (initrd_filename) { 820b061f059SPaolo Bonzini GMappedFile *mapped_file; 821b061f059SPaolo Bonzini gsize initrd_size; 822b061f059SPaolo Bonzini gchar *initrd_data; 823b061f059SPaolo Bonzini GError *gerr = NULL; 824b061f059SPaolo Bonzini 825b061f059SPaolo Bonzini if (protocol < 0x200) { 826b061f059SPaolo Bonzini fprintf(stderr, "qemu: linux kernel too old to load a ram disk\n"); 827b061f059SPaolo Bonzini exit(1); 828b061f059SPaolo Bonzini } 829b061f059SPaolo Bonzini 830b061f059SPaolo Bonzini mapped_file = g_mapped_file_new(initrd_filename, false, &gerr); 831b061f059SPaolo Bonzini if (!mapped_file) { 832b061f059SPaolo Bonzini fprintf(stderr, "qemu: error reading initrd %s: %s\n", 833b061f059SPaolo Bonzini initrd_filename, gerr->message); 834b061f059SPaolo Bonzini exit(1); 835b061f059SPaolo Bonzini } 836b061f059SPaolo Bonzini x86ms->initrd_mapped_file = mapped_file; 837b061f059SPaolo Bonzini 838b061f059SPaolo Bonzini initrd_data = g_mapped_file_get_contents(mapped_file); 839b061f059SPaolo Bonzini initrd_size = g_mapped_file_get_length(mapped_file); 840b061f059SPaolo Bonzini if (initrd_size >= initrd_max) { 841b061f059SPaolo Bonzini fprintf(stderr, "qemu: initrd is too large, cannot support." 842b061f059SPaolo Bonzini "(max: %"PRIu32", need %"PRId64")\n", 843b061f059SPaolo Bonzini initrd_max, (uint64_t)initrd_size); 844b061f059SPaolo Bonzini exit(1); 845b061f059SPaolo Bonzini } 846b061f059SPaolo Bonzini 847b061f059SPaolo Bonzini initrd_addr = (initrd_max - initrd_size) & ~4095; 848b061f059SPaolo Bonzini 849b061f059SPaolo Bonzini fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_ADDR, initrd_addr); 850b061f059SPaolo Bonzini fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_SIZE, initrd_size); 851b061f059SPaolo Bonzini fw_cfg_add_bytes(fw_cfg, FW_CFG_INITRD_DATA, initrd_data, initrd_size); 852b061f059SPaolo Bonzini sev_load_ctx.initrd_data = initrd_data; 853b061f059SPaolo Bonzini sev_load_ctx.initrd_size = initrd_size; 854b061f059SPaolo Bonzini 855b061f059SPaolo Bonzini stl_p(header + 0x218, initrd_addr); 856b061f059SPaolo Bonzini stl_p(header + 0x21c, initrd_size); 857b061f059SPaolo Bonzini } 858b061f059SPaolo Bonzini 859b061f059SPaolo Bonzini /* load kernel and setup */ 860b061f059SPaolo Bonzini setup_size = header[0x1f1]; 861b061f059SPaolo Bonzini if (setup_size == 0) { 862b061f059SPaolo Bonzini setup_size = 4; 863b061f059SPaolo Bonzini } 864b061f059SPaolo Bonzini setup_size = (setup_size + 1) * 512; 865b061f059SPaolo Bonzini if (setup_size > kernel_size) { 866b061f059SPaolo Bonzini fprintf(stderr, "qemu: invalid kernel header\n"); 867b061f059SPaolo Bonzini exit(1); 868b061f059SPaolo Bonzini } 869b061f059SPaolo Bonzini kernel_size -= setup_size; 870b061f059SPaolo Bonzini 871b061f059SPaolo Bonzini setup = g_malloc(setup_size); 872b061f059SPaolo Bonzini kernel = g_malloc(kernel_size); 873b061f059SPaolo Bonzini fseek(f, 0, SEEK_SET); 874b061f059SPaolo Bonzini if (fread(setup, 1, setup_size, f) != setup_size) { 875b061f059SPaolo Bonzini fprintf(stderr, "fread() failed\n"); 876b061f059SPaolo Bonzini exit(1); 877b061f059SPaolo Bonzini } 878b061f059SPaolo Bonzini if (fread(kernel, 1, kernel_size, f) != kernel_size) { 879b061f059SPaolo Bonzini fprintf(stderr, "fread() failed\n"); 880b061f059SPaolo Bonzini exit(1); 881b061f059SPaolo Bonzini } 882b061f059SPaolo Bonzini fclose(f); 883b061f059SPaolo Bonzini 884b061f059SPaolo Bonzini /* append dtb to kernel */ 885b061f059SPaolo Bonzini if (dtb_filename) { 886b061f059SPaolo Bonzini if (protocol < 0x209) { 887b061f059SPaolo Bonzini fprintf(stderr, "qemu: Linux kernel too old to load a dtb\n"); 888b061f059SPaolo Bonzini exit(1); 889b061f059SPaolo Bonzini } 890b061f059SPaolo Bonzini 891b061f059SPaolo Bonzini dtb_size = get_image_size(dtb_filename); 892b061f059SPaolo Bonzini if (dtb_size <= 0) { 893b061f059SPaolo Bonzini fprintf(stderr, "qemu: error reading dtb %s: %s\n", 894b061f059SPaolo Bonzini dtb_filename, strerror(errno)); 895b061f059SPaolo Bonzini exit(1); 896b061f059SPaolo Bonzini } 897b061f059SPaolo Bonzini 898b061f059SPaolo Bonzini setup_data_offset = QEMU_ALIGN_UP(kernel_size, 16); 899b061f059SPaolo Bonzini kernel_size = setup_data_offset + sizeof(struct setup_data) + dtb_size; 900b061f059SPaolo Bonzini kernel = g_realloc(kernel, kernel_size); 901b061f059SPaolo Bonzini 902b061f059SPaolo Bonzini stq_p(header + 0x250, prot_addr + setup_data_offset); 903b061f059SPaolo Bonzini 904b061f059SPaolo Bonzini setup_data = (struct setup_data *)(kernel + setup_data_offset); 905b061f059SPaolo Bonzini setup_data->next = 0; 906b061f059SPaolo Bonzini setup_data->type = cpu_to_le32(SETUP_DTB); 907b061f059SPaolo Bonzini setup_data->len = cpu_to_le32(dtb_size); 908b061f059SPaolo Bonzini 909b061f059SPaolo Bonzini load_image_size(dtb_filename, setup_data->data, dtb_size); 910b061f059SPaolo Bonzini } 911b061f059SPaolo Bonzini 912b061f059SPaolo Bonzini /* 913b061f059SPaolo Bonzini * If we're starting an encrypted VM, it will be OVMF based, which uses the 914b061f059SPaolo Bonzini * efi stub for booting and doesn't require any values to be placed in the 915b061f059SPaolo Bonzini * kernel header. We therefore don't update the header so the hash of the 916b061f059SPaolo Bonzini * kernel on the other side of the fw_cfg interface matches the hash of the 917b061f059SPaolo Bonzini * file the user passed in. 918b061f059SPaolo Bonzini */ 919b061f059SPaolo Bonzini if (!sev_enabled()) { 920b061f059SPaolo Bonzini memcpy(setup, header, MIN(sizeof(header), setup_size)); 921b061f059SPaolo Bonzini } 922b061f059SPaolo Bonzini 923b061f059SPaolo Bonzini fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_ADDR, prot_addr); 924b061f059SPaolo Bonzini fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_SIZE, kernel_size); 925b061f059SPaolo Bonzini fw_cfg_add_bytes(fw_cfg, FW_CFG_KERNEL_DATA, kernel, kernel_size); 926b061f059SPaolo Bonzini sev_load_ctx.kernel_data = (char *)kernel; 927b061f059SPaolo Bonzini sev_load_ctx.kernel_size = kernel_size; 928b061f059SPaolo Bonzini 929b061f059SPaolo Bonzini fw_cfg_add_i32(fw_cfg, FW_CFG_SETUP_ADDR, real_addr); 930b061f059SPaolo Bonzini fw_cfg_add_i32(fw_cfg, FW_CFG_SETUP_SIZE, setup_size); 931b061f059SPaolo Bonzini fw_cfg_add_bytes(fw_cfg, FW_CFG_SETUP_DATA, setup, setup_size); 932b061f059SPaolo Bonzini sev_load_ctx.setup_data = (char *)setup; 933b061f059SPaolo Bonzini sev_load_ctx.setup_size = setup_size; 934b061f059SPaolo Bonzini 935b061f059SPaolo Bonzini if (sev_enabled()) { 936b061f059SPaolo Bonzini sev_add_kernel_loader_hashes(&sev_load_ctx, &error_fatal); 937b061f059SPaolo Bonzini } 938b061f059SPaolo Bonzini 939b061f059SPaolo Bonzini option_rom[nb_option_roms].bootindex = 0; 940b061f059SPaolo Bonzini option_rom[nb_option_roms].name = "linuxboot.bin"; 941b061f059SPaolo Bonzini if (linuxboot_dma_enabled && fw_cfg_dma_enabled(fw_cfg)) { 942b061f059SPaolo Bonzini option_rom[nb_option_roms].name = "linuxboot_dma.bin"; 943b061f059SPaolo Bonzini } 944b061f059SPaolo Bonzini nb_option_roms++; 945b061f059SPaolo Bonzini } 946b061f059SPaolo Bonzini 947b061f059SPaolo Bonzini void x86_isa_bios_init(MemoryRegion *isa_bios, MemoryRegion *isa_memory, 948b061f059SPaolo Bonzini MemoryRegion *bios, bool read_only) 949b061f059SPaolo Bonzini { 950b061f059SPaolo Bonzini uint64_t bios_size = memory_region_size(bios); 951b061f059SPaolo Bonzini uint64_t isa_bios_size = MIN(bios_size, 128 * KiB); 952b061f059SPaolo Bonzini 953b061f059SPaolo Bonzini memory_region_init_alias(isa_bios, NULL, "isa-bios", bios, 954b061f059SPaolo Bonzini bios_size - isa_bios_size, isa_bios_size); 955b061f059SPaolo Bonzini memory_region_add_subregion_overlap(isa_memory, 1 * MiB - isa_bios_size, 956b061f059SPaolo Bonzini isa_bios, 1); 957b061f059SPaolo Bonzini memory_region_set_readonly(isa_bios, read_only); 958b061f059SPaolo Bonzini } 959b061f059SPaolo Bonzini 960b061f059SPaolo Bonzini void x86_bios_rom_init(X86MachineState *x86ms, const char *default_firmware, 961b061f059SPaolo Bonzini MemoryRegion *rom_memory, bool isapc_ram_fw) 962b061f059SPaolo Bonzini { 963b061f059SPaolo Bonzini const char *bios_name; 964b061f059SPaolo Bonzini char *filename; 965b061f059SPaolo Bonzini int bios_size; 966b061f059SPaolo Bonzini ssize_t ret; 967b061f059SPaolo Bonzini 968b061f059SPaolo Bonzini /* BIOS load */ 969b061f059SPaolo Bonzini bios_name = MACHINE(x86ms)->firmware ?: default_firmware; 970b061f059SPaolo Bonzini filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name); 971b061f059SPaolo Bonzini if (filename) { 972b061f059SPaolo Bonzini bios_size = get_image_size(filename); 973b061f059SPaolo Bonzini } else { 974b061f059SPaolo Bonzini bios_size = -1; 975b061f059SPaolo Bonzini } 976b061f059SPaolo Bonzini if (bios_size <= 0 || 977b061f059SPaolo Bonzini (bios_size % 65536) != 0) { 978b061f059SPaolo Bonzini goto bios_error; 979b061f059SPaolo Bonzini } 980b061f059SPaolo Bonzini memory_region_init_ram(&x86ms->bios, NULL, "pc.bios", bios_size, 981b061f059SPaolo Bonzini &error_fatal); 982b061f059SPaolo Bonzini if (sev_enabled()) { 983b061f059SPaolo Bonzini /* 984b061f059SPaolo Bonzini * The concept of a "reset" simply doesn't exist for 985b061f059SPaolo Bonzini * confidential computing guests, we have to destroy and 986b061f059SPaolo Bonzini * re-launch them instead. So there is no need to register 987b061f059SPaolo Bonzini * the firmware as rom to properly re-initialize on reset. 988b061f059SPaolo Bonzini * Just go for a straight file load instead. 989b061f059SPaolo Bonzini */ 990b061f059SPaolo Bonzini void *ptr = memory_region_get_ram_ptr(&x86ms->bios); 991b061f059SPaolo Bonzini load_image_size(filename, ptr, bios_size); 992b061f059SPaolo Bonzini x86_firmware_configure(ptr, bios_size); 993b061f059SPaolo Bonzini } else { 994b061f059SPaolo Bonzini memory_region_set_readonly(&x86ms->bios, !isapc_ram_fw); 995b061f059SPaolo Bonzini ret = rom_add_file_fixed(bios_name, (uint32_t)(-bios_size), -1); 996b061f059SPaolo Bonzini if (ret != 0) { 997b061f059SPaolo Bonzini goto bios_error; 998b061f059SPaolo Bonzini } 999b061f059SPaolo Bonzini } 1000b061f059SPaolo Bonzini g_free(filename); 1001b061f059SPaolo Bonzini 1002b061f059SPaolo Bonzini /* map the last 128KB of the BIOS in ISA space */ 1003b061f059SPaolo Bonzini x86_isa_bios_init(&x86ms->isa_bios, rom_memory, &x86ms->bios, 1004b061f059SPaolo Bonzini !isapc_ram_fw); 1005b061f059SPaolo Bonzini 1006b061f059SPaolo Bonzini /* map all the bios at the top of memory */ 1007b061f059SPaolo Bonzini memory_region_add_subregion(rom_memory, 1008b061f059SPaolo Bonzini (uint32_t)(-bios_size), 1009b061f059SPaolo Bonzini &x86ms->bios); 1010b061f059SPaolo Bonzini return; 1011b061f059SPaolo Bonzini 1012b061f059SPaolo Bonzini bios_error: 1013b061f059SPaolo Bonzini fprintf(stderr, "qemu: could not load PC BIOS '%s'\n", bios_name); 1014b061f059SPaolo Bonzini exit(1); 1015b061f059SPaolo Bonzini } 1016