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