Merge remote-tracking branch 'qemu-project/master'
[qemu/ar7.git] / hw / i386 / x86-common.c
blobc0c66a0eb52bf4f873b751707351eb1c7755fe69
1 /*
2 * Copyright (c) 2003-2004 Fabrice Bellard
3 * Copyright (c) 2019, 2024 Red Hat, Inc.
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:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
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.
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 "sysemu/numa.h"
30 #include "sysemu/sysemu.h"
31 #include "sysemu/xen.h"
32 #include "trace.h"
34 #include "hw/i386/x86.h"
35 #include "target/i386/cpu.h"
36 #include "hw/rtc/mc146818rtc.h"
37 #include "target/i386/sev.h"
39 #include "hw/acpi/cpu_hotplug.h"
40 #include "hw/irq.h"
41 #include "hw/loader.h"
42 #include "multiboot.h"
43 #include "elf.h"
44 #include "standard-headers/asm-x86/bootparam.h"
45 #include CONFIG_DEVICES
46 #include "kvm/kvm_i386.h"
48 #ifdef CONFIG_XEN_EMU
49 #include "hw/xen/xen.h"
50 #include "hw/i386/kvm/xen_evtchn.h"
51 #endif
53 /* Physical Address of PVH entry point read from kernel ELF NOTE */
54 static size_t pvh_start_addr;
56 static void x86_cpu_new(X86MachineState *x86ms, int64_t apic_id, Error **errp)
58 Object *cpu = object_new(MACHINE(x86ms)->cpu_type);
60 if (!object_property_set_uint(cpu, "apic-id", apic_id, errp)) {
61 goto out;
63 qdev_realize(DEVICE(cpu), NULL, errp);
65 out:
66 object_unref(cpu);
69 void x86_cpus_init(X86MachineState *x86ms, int default_cpu_version)
71 int i;
72 const CPUArchIdList *possible_cpus;
73 MachineState *ms = MACHINE(x86ms);
74 MachineClass *mc = MACHINE_GET_CLASS(x86ms);
76 x86_cpu_set_default_version(default_cpu_version);
79 * Calculates the limit to CPU APIC ID values
81 * Limit for the APIC ID value, so that all
82 * CPU APIC IDs are < x86ms->apic_id_limit.
84 * This is used for FW_CFG_MAX_CPUS. See comments on fw_cfg_arch_create().
86 x86ms->apic_id_limit = x86_cpu_apic_id_from_index(x86ms,
87 ms->smp.max_cpus - 1) + 1;
90 * Can we support APIC ID 255 or higher? With KVM, that requires
91 * both in-kernel lapic and X2APIC userspace API.
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)
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);
104 if (kvm_enabled()) {
105 kvm_set_max_apic_id(x86ms->apic_id_limit);
108 if (!kvm_irqchip_in_kernel()) {
109 apic_set_max_apic_id(x86ms->apic_id_limit);
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);
118 void x86_rtc_set_cpus_count(ISADevice *s, uint16_t cpus_count)
120 MC146818RtcState *rtc = MC146818_RTC(s);
122 if (cpus_count > 0xff) {
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.
128 mc146818rtc_set_cmos_data(rtc, 0x5f, 0);
129 } else {
130 mc146818rtc_set_cmos_data(rtc, 0x5f, cpus_count - 1);
134 static int x86_apic_cmp(const void *a, const void *b)
136 CPUArchId *apic_a = (CPUArchId *)a;
137 CPUArchId *apic_b = (CPUArchId *)b;
139 return apic_a->arch_id - apic_b->arch_id;
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.
147 static CPUArchId *x86_find_cpu_slot(MachineState *ms, uint32_t id, int *idx)
149 CPUArchId apic_id, *found_cpu;
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;
158 return found_cpu;
161 void x86_cpu_plug(HotplugHandler *hotplug_dev,
162 DeviceState *dev, Error **errp)
164 CPUArchId *found_cpu;
165 Error *local_err = NULL;
166 X86CPU *cpu = X86_CPU(dev);
167 X86MachineState *x86ms = X86_MACHINE(hotplug_dev);
169 if (x86ms->acpi_dev) {
170 hotplug_handler_plug(x86ms->acpi_dev, dev, &local_err);
171 if (local_err) {
172 goto out;
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);
181 if (x86ms->fw_cfg) {
182 fw_cfg_modify_i16(x86ms->fw_cfg, FW_CFG_NB_CPUS, x86ms->boot_cpus);
185 found_cpu = x86_find_cpu_slot(MACHINE(x86ms), cpu->apic_id, NULL);
186 found_cpu->cpu = CPU(dev);
187 out:
188 error_propagate(errp, local_err);
191 void x86_cpu_unplug_request_cb(HotplugHandler *hotplug_dev,
192 DeviceState *dev, Error **errp)
194 int idx = -1;
195 X86CPU *cpu = X86_CPU(dev);
196 X86MachineState *x86ms = X86_MACHINE(hotplug_dev);
198 if (!x86ms->acpi_dev) {
199 error_setg(errp, "CPU hot unplug not supported without ACPI");
200 return;
203 x86_find_cpu_slot(MACHINE(x86ms), cpu->apic_id, &idx);
204 assert(idx != -1);
205 if (idx == 0) {
206 error_setg(errp, "Boot CPU is unpluggable");
207 return;
210 hotplug_handler_unplug_request(x86ms->acpi_dev, dev,
211 errp);
214 void x86_cpu_unplug_cb(HotplugHandler *hotplug_dev,
215 DeviceState *dev, Error **errp)
217 CPUArchId *found_cpu;
218 Error *local_err = NULL;
219 X86CPU *cpu = X86_CPU(dev);
220 X86MachineState *x86ms = X86_MACHINE(hotplug_dev);
222 hotplug_handler_unplug(x86ms->acpi_dev, dev, &local_err);
223 if (local_err) {
224 goto out;
227 found_cpu = x86_find_cpu_slot(MACHINE(x86ms), cpu->apic_id, NULL);
228 found_cpu->cpu = NULL;
229 qdev_unrealize(dev);
231 /* decrement the number of CPUs */
232 x86ms->boot_cpus--;
233 /* Update the number of CPUs in CMOS */
234 x86_rtc_set_cpus_count(x86ms->rtc, x86ms->boot_cpus);
235 fw_cfg_modify_i16(x86ms->fw_cfg, FW_CFG_NB_CPUS, x86ms->boot_cpus);
236 out:
237 error_propagate(errp, local_err);
240 void x86_cpu_pre_plug(HotplugHandler *hotplug_dev,
241 DeviceState *dev, Error **errp)
243 int idx;
244 CPUState *cs;
245 CPUArchId *cpu_slot;
246 X86CPUTopoIDs topo_ids;
247 X86CPU *cpu = X86_CPU(dev);
248 CPUX86State *env = &cpu->env;
249 MachineState *ms = MACHINE(hotplug_dev);
250 X86MachineState *x86ms = X86_MACHINE(hotplug_dev);
251 unsigned int smp_cores = ms->smp.cores;
252 unsigned int smp_threads = ms->smp.threads;
253 X86CPUTopoInfo topo_info;
255 if (!object_dynamic_cast(OBJECT(cpu), ms->cpu_type)) {
256 error_setg(errp, "Invalid CPU type, expected cpu type: '%s'",
257 ms->cpu_type);
258 return;
261 if (x86ms->acpi_dev) {
262 Error *local_err = NULL;
264 hotplug_handler_pre_plug(HOTPLUG_HANDLER(x86ms->acpi_dev), dev,
265 &local_err);
266 if (local_err) {
267 error_propagate(errp, local_err);
268 return;
272 init_topo_info(&topo_info, x86ms);
274 if (ms->smp.modules > 1) {
275 env->nr_modules = ms->smp.modules;
276 set_bit(CPU_TOPO_LEVEL_MODULE, env->avail_cpu_topo);
279 if (ms->smp.dies > 1) {
280 env->nr_dies = ms->smp.dies;
281 set_bit(CPU_TOPO_LEVEL_DIE, env->avail_cpu_topo);
285 * If APIC ID is not set,
286 * set it based on socket/die/module/core/thread properties.
288 if (cpu->apic_id == UNASSIGNED_APIC_ID) {
290 * die-id was optional in QEMU 4.0 and older, so keep it optional
291 * if there's only one die per socket.
293 if (cpu->die_id < 0 && ms->smp.dies == 1) {
294 cpu->die_id = 0;
298 * module-id was optional in QEMU 9.0 and older, so keep it optional
299 * if there's only one module per die.
301 if (cpu->module_id < 0 && ms->smp.modules == 1) {
302 cpu->module_id = 0;
305 if (cpu->socket_id < 0) {
306 error_setg(errp, "CPU socket-id is not set");
307 return;
308 } else if (cpu->socket_id > ms->smp.sockets - 1) {
309 error_setg(errp, "Invalid CPU socket-id: %u must be in range 0:%u",
310 cpu->socket_id, ms->smp.sockets - 1);
311 return;
313 if (cpu->die_id < 0) {
314 error_setg(errp, "CPU die-id is not set");
315 return;
316 } else if (cpu->die_id > ms->smp.dies - 1) {
317 error_setg(errp, "Invalid CPU die-id: %u must be in range 0:%u",
318 cpu->die_id, ms->smp.dies - 1);
319 return;
321 if (cpu->module_id < 0) {
322 error_setg(errp, "CPU module-id is not set");
323 return;
324 } else if (cpu->module_id > ms->smp.modules - 1) {
325 error_setg(errp, "Invalid CPU module-id: %u must be in range 0:%u",
326 cpu->module_id, ms->smp.modules - 1);
327 return;
329 if (cpu->core_id < 0) {
330 error_setg(errp, "CPU core-id is not set");
331 return;
332 } else if (cpu->core_id > (smp_cores - 1)) {
333 error_setg(errp, "Invalid CPU core-id: %u must be in range 0:%u",
334 cpu->core_id, smp_cores - 1);
335 return;
337 if (cpu->thread_id < 0) {
338 error_setg(errp, "CPU thread-id is not set");
339 return;
340 } else if (cpu->thread_id > (smp_threads - 1)) {
341 error_setg(errp, "Invalid CPU thread-id: %u must be in range 0:%u",
342 cpu->thread_id, smp_threads - 1);
343 return;
346 topo_ids.pkg_id = cpu->socket_id;
347 topo_ids.die_id = cpu->die_id;
348 topo_ids.module_id = cpu->module_id;
349 topo_ids.core_id = cpu->core_id;
350 topo_ids.smt_id = cpu->thread_id;
351 cpu->apic_id = x86_apicid_from_topo_ids(&topo_info, &topo_ids);
354 cpu_slot = x86_find_cpu_slot(MACHINE(x86ms), cpu->apic_id, &idx);
355 if (!cpu_slot) {
356 x86_topo_ids_from_apicid(cpu->apic_id, &topo_info, &topo_ids);
358 error_setg(errp,
359 "Invalid CPU [socket: %u, die: %u, module: %u, core: %u, thread: %u]"
360 " with APIC ID %" PRIu32 ", valid index range 0:%d",
361 topo_ids.pkg_id, topo_ids.die_id, topo_ids.module_id,
362 topo_ids.core_id, topo_ids.smt_id, cpu->apic_id,
363 ms->possible_cpus->len - 1);
364 return;
367 if (cpu_slot->cpu) {
368 error_setg(errp, "CPU[%d] with APIC ID %" PRIu32 " exists",
369 idx, cpu->apic_id);
370 return;
373 /* if 'address' properties socket-id/core-id/thread-id are not set, set them
374 * so that machine_query_hotpluggable_cpus would show correct values
376 /* TODO: move socket_id/core_id/thread_id checks into x86_cpu_realizefn()
377 * once -smp refactoring is complete and there will be CPU private
378 * CPUState::nr_cores and CPUState::nr_threads fields instead of globals */
379 x86_topo_ids_from_apicid(cpu->apic_id, &topo_info, &topo_ids);
380 if (cpu->socket_id != -1 && cpu->socket_id != topo_ids.pkg_id) {
381 error_setg(errp, "property socket-id: %u doesn't match set apic-id:"
382 " 0x%x (socket-id: %u)", cpu->socket_id, cpu->apic_id,
383 topo_ids.pkg_id);
384 return;
386 cpu->socket_id = topo_ids.pkg_id;
388 if (cpu->die_id != -1 && cpu->die_id != topo_ids.die_id) {
389 error_setg(errp, "property die-id: %u doesn't match set apic-id:"
390 " 0x%x (die-id: %u)", cpu->die_id, cpu->apic_id, topo_ids.die_id);
391 return;
393 cpu->die_id = topo_ids.die_id;
395 if (cpu->module_id != -1 && cpu->module_id != topo_ids.module_id) {
396 error_setg(errp, "property module-id: %u doesn't match set apic-id:"
397 " 0x%x (module-id: %u)", cpu->module_id, cpu->apic_id,
398 topo_ids.module_id);
399 return;
401 cpu->module_id = topo_ids.module_id;
403 if (cpu->core_id != -1 && cpu->core_id != topo_ids.core_id) {
404 error_setg(errp, "property core-id: %u doesn't match set apic-id:"
405 " 0x%x (core-id: %u)", cpu->core_id, cpu->apic_id,
406 topo_ids.core_id);
407 return;
409 cpu->core_id = topo_ids.core_id;
411 if (cpu->thread_id != -1 && cpu->thread_id != topo_ids.smt_id) {
412 error_setg(errp, "property thread-id: %u doesn't match set apic-id:"
413 " 0x%x (thread-id: %u)", cpu->thread_id, cpu->apic_id,
414 topo_ids.smt_id);
415 return;
417 cpu->thread_id = topo_ids.smt_id;
420 * kvm_enabled() must go first to ensure that kvm_* references are
421 * not emitted for the linker to consume (kvm_enabled() is
422 * a literal `0` in configurations where kvm_* aren't defined)
424 if (kvm_enabled() && hyperv_feat_enabled(cpu, HYPERV_FEAT_VPINDEX) &&
425 !kvm_hv_vpindex_settable()) {
426 error_setg(errp, "kernel doesn't allow setting HyperV VP_INDEX");
427 return;
430 cs = CPU(cpu);
431 cs->cpu_index = idx;
433 numa_cpu_pre_plug(cpu_slot, dev, errp);
436 static long get_file_size(FILE *f)
438 long where, size;
440 /* XXX: on Unix systems, using fstat() probably makes more sense */
442 where = ftell(f);
443 fseek(f, 0, SEEK_END);
444 size = ftell(f);
445 fseek(f, where, SEEK_SET);
447 return size;
450 void gsi_handler(void *opaque, int n, int level)
452 GSIState *s = opaque;
454 trace_x86_gsi_interrupt(n, level);
455 switch (n) {
456 case 0 ... ISA_NUM_IRQS - 1:
457 if (s->i8259_irq[n]) {
458 /* Under KVM, Kernel will forward to both PIC and IOAPIC */
459 qemu_set_irq(s->i8259_irq[n], level);
461 /* fall through */
462 case ISA_NUM_IRQS ... IOAPIC_NUM_PINS - 1:
463 #ifdef CONFIG_XEN_EMU
465 * Xen delivers the GSI to the Legacy PIC (not that Legacy PIC
466 * routing actually works properly under Xen). And then to
467 * *either* the PIRQ handling or the I/OAPIC depending on
468 * whether the former wants it.
470 if (xen_mode == XEN_EMULATE && xen_evtchn_set_gsi(n, level)) {
471 break;
473 #endif
474 qemu_set_irq(s->ioapic_irq[n], level);
475 break;
476 case IO_APIC_SECONDARY_IRQBASE
477 ... IO_APIC_SECONDARY_IRQBASE + IOAPIC_NUM_PINS - 1:
478 qemu_set_irq(s->ioapic2_irq[n - IO_APIC_SECONDARY_IRQBASE], level);
479 break;
483 void ioapic_init_gsi(GSIState *gsi_state, Object *parent)
485 DeviceState *dev;
486 SysBusDevice *d;
487 unsigned int i;
489 assert(parent);
490 if (kvm_ioapic_in_kernel()) {
491 dev = qdev_new(TYPE_KVM_IOAPIC);
492 } else {
493 dev = qdev_new(TYPE_IOAPIC);
495 object_property_add_child(parent, "ioapic", OBJECT(dev));
496 d = SYS_BUS_DEVICE(dev);
497 sysbus_realize_and_unref(d, &error_fatal);
498 sysbus_mmio_map(d, 0, IO_APIC_DEFAULT_ADDRESS);
500 for (i = 0; i < IOAPIC_NUM_PINS; i++) {
501 gsi_state->ioapic_irq[i] = qdev_get_gpio_in(dev, i);
505 DeviceState *ioapic_init_secondary(GSIState *gsi_state)
507 DeviceState *dev;
508 SysBusDevice *d;
509 unsigned int i;
511 dev = qdev_new(TYPE_IOAPIC);
512 d = SYS_BUS_DEVICE(dev);
513 sysbus_realize_and_unref(d, &error_fatal);
514 sysbus_mmio_map(d, 0, IO_APIC_SECONDARY_ADDRESS);
516 for (i = 0; i < IOAPIC_NUM_PINS; i++) {
517 gsi_state->ioapic2_irq[i] = qdev_get_gpio_in(dev, i);
519 return dev;
523 * The entry point into the kernel for PVH boot is different from
524 * the native entry point. The PVH entry is defined by the x86/HVM
525 * direct boot ABI and is available in an ELFNOTE in the kernel binary.
527 * This function is passed to load_elf() when it is called from
528 * load_elfboot() which then additionally checks for an ELF Note of
529 * type XEN_ELFNOTE_PHYS32_ENTRY and passes it to this function to
530 * parse the PVH entry address from the ELF Note.
532 * Due to trickery in elf_opts.h, load_elf() is actually available as
533 * load_elf32() or load_elf64() and this routine needs to be able
534 * to deal with being called as 32 or 64 bit.
536 * The address of the PVH entry point is saved to the 'pvh_start_addr'
537 * global variable. (although the entry point is 32-bit, the kernel
538 * binary can be either 32-bit or 64-bit).
540 static uint64_t read_pvh_start_addr(void *arg1, void *arg2, bool is64)
542 size_t *elf_note_data_addr;
544 /* Check if ELF Note header passed in is valid */
545 if (arg1 == NULL) {
546 return 0;
549 if (is64) {
550 struct elf64_note *nhdr64 = (struct elf64_note *)arg1;
551 uint64_t nhdr_size64 = sizeof(struct elf64_note);
552 uint64_t phdr_align = *(uint64_t *)arg2;
553 uint64_t nhdr_namesz = nhdr64->n_namesz;
555 elf_note_data_addr =
556 ((void *)nhdr64) + nhdr_size64 +
557 QEMU_ALIGN_UP(nhdr_namesz, phdr_align);
559 pvh_start_addr = *elf_note_data_addr;
560 } else {
561 struct elf32_note *nhdr32 = (struct elf32_note *)arg1;
562 uint32_t nhdr_size32 = sizeof(struct elf32_note);
563 uint32_t phdr_align = *(uint32_t *)arg2;
564 uint32_t nhdr_namesz = nhdr32->n_namesz;
566 elf_note_data_addr =
567 ((void *)nhdr32) + nhdr_size32 +
568 QEMU_ALIGN_UP(nhdr_namesz, phdr_align);
570 pvh_start_addr = *(uint32_t *)elf_note_data_addr;
573 return pvh_start_addr;
576 static bool load_elfboot(const char *kernel_filename,
577 int kernel_file_size,
578 uint8_t *header,
579 size_t pvh_xen_start_addr,
580 FWCfgState *fw_cfg)
582 uint32_t flags = 0;
583 uint32_t mh_load_addr = 0;
584 uint32_t elf_kernel_size = 0;
585 uint64_t elf_entry;
586 uint64_t elf_low, elf_high;
587 int kernel_size;
589 if (ldl_p(header) != 0x464c457f) {
590 return false; /* no elfboot */
593 bool elf_is64 = header[EI_CLASS] == ELFCLASS64;
594 flags = elf_is64 ?
595 ((Elf64_Ehdr *)header)->e_flags : ((Elf32_Ehdr *)header)->e_flags;
597 if (flags & 0x00010004) { /* LOAD_ELF_HEADER_HAS_ADDR */
598 error_report("elfboot unsupported flags = %x", flags);
599 exit(1);
602 uint64_t elf_note_type = XEN_ELFNOTE_PHYS32_ENTRY;
603 kernel_size = load_elf(kernel_filename, read_pvh_start_addr,
604 NULL, &elf_note_type, &elf_entry,
605 &elf_low, &elf_high, NULL, 0, I386_ELF_MACHINE,
606 0, 0);
608 if (kernel_size < 0) {
609 error_report("Error while loading elf kernel");
610 exit(1);
612 mh_load_addr = elf_low;
613 elf_kernel_size = elf_high - elf_low;
615 if (pvh_start_addr == 0) {
616 error_report("Error loading uncompressed kernel without PVH ELF Note");
617 exit(1);
619 fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_ENTRY, pvh_start_addr);
620 fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_ADDR, mh_load_addr);
621 fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_SIZE, elf_kernel_size);
623 return true;
626 void x86_load_linux(X86MachineState *x86ms,
627 FWCfgState *fw_cfg,
628 int acpi_data_size,
629 bool pvh_enabled)
631 bool linuxboot_dma_enabled = X86_MACHINE_GET_CLASS(x86ms)->fwcfg_dma_enabled;
632 uint16_t protocol;
633 int setup_size, kernel_size, cmdline_size;
634 int dtb_size, setup_data_offset;
635 uint32_t initrd_max;
636 uint8_t header[8192], *setup, *kernel;
637 hwaddr real_addr, prot_addr, cmdline_addr, initrd_addr = 0;
638 FILE *f;
639 char *vmode;
640 MachineState *machine = MACHINE(x86ms);
641 struct setup_data *setup_data;
642 const char *kernel_filename = machine->kernel_filename;
643 const char *initrd_filename = machine->initrd_filename;
644 const char *dtb_filename = machine->dtb;
645 const char *kernel_cmdline = machine->kernel_cmdline;
646 SevKernelLoaderContext sev_load_ctx = {};
648 /* Align to 16 bytes as a paranoia measure */
649 cmdline_size = (strlen(kernel_cmdline) + 16) & ~15;
651 /* load the kernel header */
652 f = fopen(kernel_filename, "rb");
653 if (!f) {
654 fprintf(stderr, "qemu: could not open kernel file '%s': %s\n",
655 kernel_filename, strerror(errno));
656 exit(1);
659 kernel_size = get_file_size(f);
660 if (!kernel_size ||
661 fread(header, 1, MIN(ARRAY_SIZE(header), kernel_size), f) !=
662 MIN(ARRAY_SIZE(header), kernel_size)) {
663 fprintf(stderr, "qemu: could not load kernel '%s': %s\n",
664 kernel_filename, strerror(errno));
665 exit(1);
668 /* kernel protocol version */
669 if (ldl_p(header + 0x202) == 0x53726448) {
670 protocol = lduw_p(header + 0x206);
671 } else {
673 * This could be a multiboot kernel. If it is, let's stop treating it
674 * like a Linux kernel.
675 * Note: some multiboot images could be in the ELF format (the same of
676 * PVH), so we try multiboot first since we check the multiboot magic
677 * header before to load it.
679 if (load_multiboot(x86ms, fw_cfg, f, kernel_filename, initrd_filename,
680 kernel_cmdline, kernel_size, header)) {
681 return;
684 * Check if the file is an uncompressed kernel file (ELF) and load it,
685 * saving the PVH entry point used by the x86/HVM direct boot ABI.
686 * If load_elfboot() is successful, populate the fw_cfg info.
688 if (pvh_enabled &&
689 load_elfboot(kernel_filename, kernel_size,
690 header, pvh_start_addr, fw_cfg)) {
691 fclose(f);
693 fw_cfg_add_i32(fw_cfg, FW_CFG_CMDLINE_SIZE,
694 strlen(kernel_cmdline) + 1);
695 fw_cfg_add_string(fw_cfg, FW_CFG_CMDLINE_DATA, kernel_cmdline);
697 fw_cfg_add_i32(fw_cfg, FW_CFG_SETUP_SIZE, sizeof(header));
698 fw_cfg_add_bytes(fw_cfg, FW_CFG_SETUP_DATA,
699 header, sizeof(header));
701 /* load initrd */
702 if (initrd_filename) {
703 GMappedFile *mapped_file;
704 gsize initrd_size;
705 gchar *initrd_data;
706 GError *gerr = NULL;
708 mapped_file = g_mapped_file_new(initrd_filename, false, &gerr);
709 if (!mapped_file) {
710 fprintf(stderr, "qemu: error reading initrd %s: %s\n",
711 initrd_filename, gerr->message);
712 exit(1);
714 x86ms->initrd_mapped_file = mapped_file;
716 initrd_data = g_mapped_file_get_contents(mapped_file);
717 initrd_size = g_mapped_file_get_length(mapped_file);
718 initrd_max = x86ms->below_4g_mem_size - acpi_data_size - 1;
719 if (initrd_size >= initrd_max) {
720 fprintf(stderr, "qemu: initrd is too large, cannot support."
721 "(max: %"PRIu32", need %"PRId64")\n",
722 initrd_max, (uint64_t)initrd_size);
723 exit(1);
726 initrd_addr = (initrd_max - initrd_size) & ~4095;
728 fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_ADDR, initrd_addr);
729 fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_SIZE, initrd_size);
730 fw_cfg_add_bytes(fw_cfg, FW_CFG_INITRD_DATA, initrd_data,
731 initrd_size);
734 option_rom[nb_option_roms].bootindex = 0;
735 option_rom[nb_option_roms].name = "pvh.bin";
736 nb_option_roms++;
738 return;
740 protocol = 0;
743 if (protocol < 0x200 || !(header[0x211] & 0x01)) {
744 /* Low kernel */
745 real_addr = 0x90000;
746 cmdline_addr = 0x9a000 - cmdline_size;
747 prot_addr = 0x10000;
748 } else if (protocol < 0x202) {
749 /* High but ancient kernel */
750 real_addr = 0x90000;
751 cmdline_addr = 0x9a000 - cmdline_size;
752 prot_addr = 0x100000;
753 } else {
754 /* High and recent kernel */
755 real_addr = 0x10000;
756 cmdline_addr = 0x20000;
757 prot_addr = 0x100000;
760 /* highest address for loading the initrd */
761 if (protocol >= 0x20c &&
762 lduw_p(header + 0x236) & XLF_CAN_BE_LOADED_ABOVE_4G) {
764 * Linux has supported initrd up to 4 GB for a very long time (2007,
765 * long before XLF_CAN_BE_LOADED_ABOVE_4G which was added in 2013),
766 * though it only sets initrd_max to 2 GB to "work around bootloader
767 * bugs". Luckily, QEMU firmware(which does something like bootloader)
768 * has supported this.
770 * It's believed that if XLF_CAN_BE_LOADED_ABOVE_4G is set, initrd can
771 * be loaded into any address.
773 * In addition, initrd_max is uint32_t simply because QEMU doesn't
774 * support the 64-bit boot protocol (specifically the ext_ramdisk_image
775 * field).
777 * Therefore here just limit initrd_max to UINT32_MAX simply as well.
779 initrd_max = UINT32_MAX;
780 } else if (protocol >= 0x203) {
781 initrd_max = ldl_p(header + 0x22c);
782 } else {
783 initrd_max = 0x37ffffff;
786 if (initrd_max >= x86ms->below_4g_mem_size - acpi_data_size) {
787 initrd_max = x86ms->below_4g_mem_size - acpi_data_size - 1;
790 fw_cfg_add_i32(fw_cfg, FW_CFG_CMDLINE_ADDR, cmdline_addr);
791 fw_cfg_add_i32(fw_cfg, FW_CFG_CMDLINE_SIZE, strlen(kernel_cmdline) + 1);
792 fw_cfg_add_string(fw_cfg, FW_CFG_CMDLINE_DATA, kernel_cmdline);
793 sev_load_ctx.cmdline_data = (char *)kernel_cmdline;
794 sev_load_ctx.cmdline_size = strlen(kernel_cmdline) + 1;
796 if (protocol >= 0x202) {
797 stl_p(header + 0x228, cmdline_addr);
798 } else {
799 stw_p(header + 0x20, 0xA33F);
800 stw_p(header + 0x22, cmdline_addr - real_addr);
803 /* handle vga= parameter */
804 vmode = strstr(kernel_cmdline, "vga=");
805 if (vmode) {
806 unsigned int video_mode;
807 const char *end;
808 int ret;
809 /* skip "vga=" */
810 vmode += 4;
811 if (!strncmp(vmode, "normal", 6)) {
812 video_mode = 0xffff;
813 } else if (!strncmp(vmode, "ext", 3)) {
814 video_mode = 0xfffe;
815 } else if (!strncmp(vmode, "ask", 3)) {
816 video_mode = 0xfffd;
817 } else {
818 ret = qemu_strtoui(vmode, &end, 0, &video_mode);
819 if (ret != 0 || (*end && *end != ' ')) {
820 fprintf(stderr, "qemu: invalid 'vga=' kernel parameter.\n");
821 exit(1);
824 stw_p(header + 0x1fa, video_mode);
827 /* loader type */
829 * High nybble = B reserved for QEMU; low nybble is revision number.
830 * If this code is substantially changed, you may want to consider
831 * incrementing the revision.
833 if (protocol >= 0x200) {
834 header[0x210] = 0xB0;
836 /* heap */
837 if (protocol >= 0x201) {
838 header[0x211] |= 0x80; /* CAN_USE_HEAP */
839 stw_p(header + 0x224, cmdline_addr - real_addr - 0x200);
842 /* load initrd */
843 if (initrd_filename) {
844 GMappedFile *mapped_file;
845 gsize initrd_size;
846 gchar *initrd_data;
847 GError *gerr = NULL;
849 if (protocol < 0x200) {
850 fprintf(stderr, "qemu: linux kernel too old to load a ram disk\n");
851 exit(1);
854 mapped_file = g_mapped_file_new(initrd_filename, false, &gerr);
855 if (!mapped_file) {
856 fprintf(stderr, "qemu: error reading initrd %s: %s\n",
857 initrd_filename, gerr->message);
858 exit(1);
860 x86ms->initrd_mapped_file = mapped_file;
862 initrd_data = g_mapped_file_get_contents(mapped_file);
863 initrd_size = g_mapped_file_get_length(mapped_file);
864 if (initrd_size >= initrd_max) {
865 fprintf(stderr, "qemu: initrd is too large, cannot support."
866 "(max: %"PRIu32", need %"PRId64")\n",
867 initrd_max, (uint64_t)initrd_size);
868 exit(1);
871 initrd_addr = (initrd_max - initrd_size) & ~4095;
873 fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_ADDR, initrd_addr);
874 fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_SIZE, initrd_size);
875 fw_cfg_add_bytes(fw_cfg, FW_CFG_INITRD_DATA, initrd_data, initrd_size);
876 sev_load_ctx.initrd_data = initrd_data;
877 sev_load_ctx.initrd_size = initrd_size;
879 stl_p(header + 0x218, initrd_addr);
880 stl_p(header + 0x21c, initrd_size);
883 /* load kernel and setup */
884 setup_size = header[0x1f1];
885 if (setup_size == 0) {
886 setup_size = 4;
888 setup_size = (setup_size + 1) * 512;
889 if (setup_size > kernel_size) {
890 fprintf(stderr, "qemu: invalid kernel header\n");
891 exit(1);
893 kernel_size -= setup_size;
895 setup = g_malloc(setup_size);
896 kernel = g_malloc(kernel_size);
897 fseek(f, 0, SEEK_SET);
898 if (fread(setup, 1, setup_size, f) != setup_size) {
899 fprintf(stderr, "fread() failed\n");
900 exit(1);
902 if (fread(kernel, 1, kernel_size, f) != kernel_size) {
903 fprintf(stderr, "fread() failed\n");
904 exit(1);
906 fclose(f);
908 /* append dtb to kernel */
909 if (dtb_filename) {
910 if (protocol < 0x209) {
911 fprintf(stderr, "qemu: Linux kernel too old to load a dtb\n");
912 exit(1);
915 dtb_size = get_image_size(dtb_filename);
916 if (dtb_size <= 0) {
917 fprintf(stderr, "qemu: error reading dtb %s: %s\n",
918 dtb_filename, strerror(errno));
919 exit(1);
922 setup_data_offset = QEMU_ALIGN_UP(kernel_size, 16);
923 kernel_size = setup_data_offset + sizeof(struct setup_data) + dtb_size;
924 kernel = g_realloc(kernel, kernel_size);
926 stq_p(header + 0x250, prot_addr + setup_data_offset);
928 setup_data = (struct setup_data *)(kernel + setup_data_offset);
929 setup_data->next = 0;
930 setup_data->type = cpu_to_le32(SETUP_DTB);
931 setup_data->len = cpu_to_le32(dtb_size);
933 load_image_size(dtb_filename, setup_data->data, dtb_size);
937 * If we're starting an encrypted VM, it will be OVMF based, which uses the
938 * efi stub for booting and doesn't require any values to be placed in the
939 * kernel header. We therefore don't update the header so the hash of the
940 * kernel on the other side of the fw_cfg interface matches the hash of the
941 * file the user passed in.
943 if (!sev_enabled()) {
944 memcpy(setup, header, MIN(sizeof(header), setup_size));
947 fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_ADDR, prot_addr);
948 fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_SIZE, kernel_size);
949 fw_cfg_add_bytes(fw_cfg, FW_CFG_KERNEL_DATA, kernel, kernel_size);
950 sev_load_ctx.kernel_data = (char *)kernel;
951 sev_load_ctx.kernel_size = kernel_size;
953 fw_cfg_add_i32(fw_cfg, FW_CFG_SETUP_ADDR, real_addr);
954 fw_cfg_add_i32(fw_cfg, FW_CFG_SETUP_SIZE, setup_size);
955 fw_cfg_add_bytes(fw_cfg, FW_CFG_SETUP_DATA, setup, setup_size);
956 sev_load_ctx.setup_data = (char *)setup;
957 sev_load_ctx.setup_size = setup_size;
959 if (sev_enabled()) {
960 sev_add_kernel_loader_hashes(&sev_load_ctx, &error_fatal);
963 option_rom[nb_option_roms].bootindex = 0;
964 option_rom[nb_option_roms].name = "linuxboot.bin";
965 if (linuxboot_dma_enabled && fw_cfg_dma_enabled(fw_cfg)) {
966 option_rom[nb_option_roms].name = "linuxboot_dma.bin";
968 nb_option_roms++;
971 void x86_isa_bios_init(MemoryRegion *isa_bios, MemoryRegion *isa_memory,
972 MemoryRegion *bios, bool read_only)
974 uint64_t bios_size = memory_region_size(bios);
975 uint64_t isa_bios_size = MIN(bios_size, 128 * KiB);
977 memory_region_init_alias(isa_bios, NULL, "isa-bios", bios,
978 bios_size - isa_bios_size, isa_bios_size);
979 memory_region_add_subregion_overlap(isa_memory, 1 * MiB - isa_bios_size,
980 isa_bios, 1);
981 memory_region_set_readonly(isa_bios, read_only);
984 void x86_bios_rom_init(X86MachineState *x86ms, const char *default_firmware,
985 MemoryRegion *rom_memory, bool isapc_ram_fw)
987 const char *bios_name;
988 char *filename;
989 int bios_size;
990 ssize_t ret;
992 /* BIOS load */
993 bios_name = MACHINE(x86ms)->firmware ?: default_firmware;
994 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
995 if (filename) {
996 bios_size = get_image_size(filename);
997 } else {
998 bios_size = -1;
1000 if (bios_size <= 0 ||
1001 (bios_size % 65536) != 0) {
1002 goto bios_error;
1004 if (machine_require_guest_memfd(MACHINE(x86ms))) {
1005 memory_region_init_ram_guest_memfd(&x86ms->bios, NULL, "pc.bios",
1006 bios_size, &error_fatal);
1007 } else {
1008 memory_region_init_ram(&x86ms->bios, NULL, "pc.bios",
1009 bios_size, &error_fatal);
1011 if (sev_enabled()) {
1013 * The concept of a "reset" simply doesn't exist for
1014 * confidential computing guests, we have to destroy and
1015 * re-launch them instead. So there is no need to register
1016 * the firmware as rom to properly re-initialize on reset.
1017 * Just go for a straight file load instead.
1019 void *ptr = memory_region_get_ram_ptr(&x86ms->bios);
1020 load_image_size(filename, ptr, bios_size);
1021 x86_firmware_configure(0x100000000ULL - bios_size, ptr, bios_size);
1022 } else {
1023 memory_region_set_readonly(&x86ms->bios, !isapc_ram_fw);
1024 ret = rom_add_file_fixed(bios_name, (uint32_t)(-bios_size), -1);
1025 if (ret != 0) {
1026 goto bios_error;
1029 g_free(filename);
1031 if (!machine_require_guest_memfd(MACHINE(x86ms))) {
1032 /* map the last 128KB of the BIOS in ISA space */
1033 x86_isa_bios_init(&x86ms->isa_bios, rom_memory, &x86ms->bios,
1034 !isapc_ram_fw);
1037 /* map all the bios at the top of memory */
1038 memory_region_add_subregion(rom_memory,
1039 (uint32_t)(-bios_size),
1040 &x86ms->bios);
1041 return;
1043 bios_error:
1044 fprintf(stderr, "qemu: could not load PC BIOS '%s'\n", bios_name);
1045 exit(1);