Coverage Report

Created: 2026-11-20 08:13

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