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