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