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