2 * Copyright (c) 2003-2004 Fabrice Bellard
3 * Copyright (c) 2019 Red Hat, Inc.
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 #include "qemu/osdep.h"
24 #include "qemu/error-report.h"
25 #include "qemu/option.h"
26 #include "qemu/cutils.h"
27 #include "qemu/units.h"
28 #include "qemu-common.h"
29 #include "qemu/datadir.h"
30 #include "qapi/error.h"
31 #include "qapi/qmp/qerror.h"
32 #include "qapi/qapi-visit-common.h"
33 #include "qapi/visitor.h"
34 #include "sysemu/qtest.h"
35 #include "sysemu/whpx.h"
36 #include "sysemu/numa.h"
37 #include "sysemu/replay.h"
38 #include "sysemu/sysemu.h"
39 #include "sysemu/cpu-timers.h"
42 #include "hw/i386/x86.h"
43 #include "target/i386/cpu.h"
44 #include "hw/i386/topology.h"
45 #include "hw/i386/fw_cfg.h"
46 #include "hw/intc/i8259.h"
47 #include "hw/rtc/mc146818rtc.h"
49 #include "hw/acpi/cpu_hotplug.h"
52 #include "hw/loader.h"
53 #include "multiboot.h"
55 #include "standard-headers/asm-x86/bootparam.h"
56 #include CONFIG_DEVICES
57 #include "kvm/kvm_i386.h"
59 /* Physical Address of PVH entry point read from kernel ELF NOTE */
60 static size_t pvh_start_addr
;
62 inline void init_topo_info(X86CPUTopoInfo
*topo_info
,
63 const X86MachineState
*x86ms
)
65 MachineState
*ms
= MACHINE(x86ms
);
67 topo_info
->dies_per_pkg
= ms
->smp
.dies
;
68 topo_info
->cores_per_die
= ms
->smp
.cores
;
69 topo_info
->threads_per_core
= ms
->smp
.threads
;
73 * Calculates initial APIC ID for a specific CPU index
75 * Currently we need to be able to calculate the APIC ID from the CPU index
76 * alone (without requiring a CPU object), as the QEMU<->Seabios interfaces have
77 * no concept of "CPU index", and the NUMA tables on fw_cfg need the APIC ID of
78 * all CPUs up to max_cpus.
80 uint32_t x86_cpu_apic_id_from_index(X86MachineState
*x86ms
,
81 unsigned int cpu_index
)
83 X86MachineClass
*x86mc
= X86_MACHINE_GET_CLASS(x86ms
);
84 X86CPUTopoInfo topo_info
;
88 init_topo_info(&topo_info
, x86ms
);
90 correct_id
= x86_apicid_from_cpu_idx(&topo_info
, cpu_index
);
91 if (x86mc
->compat_apic_id_mode
) {
92 if (cpu_index
!= correct_id
&& !warned
&& !qtest_enabled()) {
93 error_report("APIC IDs set in compatibility mode, "
94 "CPU topology won't match the configuration");
104 void x86_cpu_new(X86MachineState
*x86ms
, int64_t apic_id
, Error
**errp
)
106 Object
*cpu
= object_new(MACHINE(x86ms
)->cpu_type
);
108 if (!object_property_set_uint(cpu
, "apic-id", apic_id
, errp
)) {
111 qdev_realize(DEVICE(cpu
), NULL
, errp
);
117 void x86_cpus_init(X86MachineState
*x86ms
, int default_cpu_version
)
120 const CPUArchIdList
*possible_cpus
;
121 MachineState
*ms
= MACHINE(x86ms
);
122 MachineClass
*mc
= MACHINE_GET_CLASS(x86ms
);
124 x86_cpu_set_default_version(default_cpu_version
);
127 * Calculates the limit to CPU APIC ID values
129 * Limit for the APIC ID value, so that all
130 * CPU APIC IDs are < x86ms->apic_id_limit.
132 * This is used for FW_CFG_MAX_CPUS. See comments on fw_cfg_arch_create().
134 x86ms
->apic_id_limit
= x86_cpu_apic_id_from_index(x86ms
,
135 ms
->smp
.max_cpus
- 1) + 1;
136 possible_cpus
= mc
->possible_cpu_arch_ids(ms
);
137 for (i
= 0; i
< ms
->smp
.cpus
; i
++) {
138 x86_cpu_new(x86ms
, possible_cpus
->cpus
[i
].arch_id
, &error_fatal
);
142 void x86_rtc_set_cpus_count(ISADevice
*rtc
, uint16_t cpus_count
)
144 if (cpus_count
> 0xff) {
146 * If the number of CPUs can't be represented in 8 bits, the
147 * BIOS must use "FW_CFG_NB_CPUS". Set RTC field to 0 just
148 * to make old BIOSes fail more predictably.
150 rtc_set_memory(rtc
, 0x5f, 0);
152 rtc_set_memory(rtc
, 0x5f, cpus_count
- 1);
156 static int x86_apic_cmp(const void *a
, const void *b
)
158 CPUArchId
*apic_a
= (CPUArchId
*)a
;
159 CPUArchId
*apic_b
= (CPUArchId
*)b
;
161 return apic_a
->arch_id
- apic_b
->arch_id
;
165 * returns pointer to CPUArchId descriptor that matches CPU's apic_id
166 * in ms->possible_cpus->cpus, if ms->possible_cpus->cpus has no
167 * entry corresponding to CPU's apic_id returns NULL.
169 CPUArchId
*x86_find_cpu_slot(MachineState
*ms
, uint32_t id
, int *idx
)
171 CPUArchId apic_id
, *found_cpu
;
173 apic_id
.arch_id
= id
;
174 found_cpu
= bsearch(&apic_id
, ms
->possible_cpus
->cpus
,
175 ms
->possible_cpus
->len
, sizeof(*ms
->possible_cpus
->cpus
),
177 if (found_cpu
&& idx
) {
178 *idx
= found_cpu
- ms
->possible_cpus
->cpus
;
183 void x86_cpu_plug(HotplugHandler
*hotplug_dev
,
184 DeviceState
*dev
, Error
**errp
)
186 CPUArchId
*found_cpu
;
187 Error
*local_err
= NULL
;
188 X86CPU
*cpu
= X86_CPU(dev
);
189 X86MachineState
*x86ms
= X86_MACHINE(hotplug_dev
);
191 if (x86ms
->acpi_dev
) {
192 hotplug_handler_plug(x86ms
->acpi_dev
, dev
, &local_err
);
198 /* increment the number of CPUs */
201 x86_rtc_set_cpus_count(x86ms
->rtc
, x86ms
->boot_cpus
);
204 fw_cfg_modify_i16(x86ms
->fw_cfg
, FW_CFG_NB_CPUS
, x86ms
->boot_cpus
);
207 found_cpu
= x86_find_cpu_slot(MACHINE(x86ms
), cpu
->apic_id
, NULL
);
208 found_cpu
->cpu
= OBJECT(dev
);
210 error_propagate(errp
, local_err
);
213 void x86_cpu_unplug_request_cb(HotplugHandler
*hotplug_dev
,
214 DeviceState
*dev
, Error
**errp
)
217 X86CPU
*cpu
= X86_CPU(dev
);
218 X86MachineState
*x86ms
= X86_MACHINE(hotplug_dev
);
220 if (!x86ms
->acpi_dev
) {
221 error_setg(errp
, "CPU hot unplug not supported without ACPI");
225 x86_find_cpu_slot(MACHINE(x86ms
), cpu
->apic_id
, &idx
);
228 error_setg(errp
, "Boot CPU is unpluggable");
232 hotplug_handler_unplug_request(x86ms
->acpi_dev
, dev
,
236 void x86_cpu_unplug_cb(HotplugHandler
*hotplug_dev
,
237 DeviceState
*dev
, Error
**errp
)
239 CPUArchId
*found_cpu
;
240 Error
*local_err
= NULL
;
241 X86CPU
*cpu
= X86_CPU(dev
);
242 X86MachineState
*x86ms
= X86_MACHINE(hotplug_dev
);
244 hotplug_handler_unplug(x86ms
->acpi_dev
, dev
, &local_err
);
249 found_cpu
= x86_find_cpu_slot(MACHINE(x86ms
), cpu
->apic_id
, NULL
);
250 found_cpu
->cpu
= NULL
;
253 /* decrement the number of CPUs */
255 /* Update the number of CPUs in CMOS */
256 x86_rtc_set_cpus_count(x86ms
->rtc
, x86ms
->boot_cpus
);
257 fw_cfg_modify_i16(x86ms
->fw_cfg
, FW_CFG_NB_CPUS
, x86ms
->boot_cpus
);
259 error_propagate(errp
, local_err
);
262 void x86_cpu_pre_plug(HotplugHandler
*hotplug_dev
,
263 DeviceState
*dev
, Error
**errp
)
268 X86CPUTopoIDs topo_ids
;
269 X86CPU
*cpu
= X86_CPU(dev
);
270 CPUX86State
*env
= &cpu
->env
;
271 MachineState
*ms
= MACHINE(hotplug_dev
);
272 X86MachineState
*x86ms
= X86_MACHINE(hotplug_dev
);
273 unsigned int smp_cores
= ms
->smp
.cores
;
274 unsigned int smp_threads
= ms
->smp
.threads
;
275 X86CPUTopoInfo topo_info
;
277 if (!object_dynamic_cast(OBJECT(cpu
), ms
->cpu_type
)) {
278 error_setg(errp
, "Invalid CPU type, expected cpu type: '%s'",
283 if (x86ms
->acpi_dev
) {
284 Error
*local_err
= NULL
;
286 hotplug_handler_pre_plug(HOTPLUG_HANDLER(x86ms
->acpi_dev
), dev
,
289 error_propagate(errp
, local_err
);
294 init_topo_info(&topo_info
, x86ms
);
296 env
->nr_dies
= ms
->smp
.dies
;
299 * If APIC ID is not set,
300 * set it based on socket/die/core/thread properties.
302 if (cpu
->apic_id
== UNASSIGNED_APIC_ID
) {
303 int max_socket
= (ms
->smp
.max_cpus
- 1) /
304 smp_threads
/ smp_cores
/ ms
->smp
.dies
;
307 * die-id was optional in QEMU 4.0 and older, so keep it optional
308 * if there's only one die per socket.
310 if (cpu
->die_id
< 0 && ms
->smp
.dies
== 1) {
314 if (cpu
->socket_id
< 0) {
315 error_setg(errp
, "CPU socket-id is not set");
317 } else if (cpu
->socket_id
> max_socket
) {
318 error_setg(errp
, "Invalid CPU socket-id: %u must be in range 0:%u",
319 cpu
->socket_id
, max_socket
);
322 if (cpu
->die_id
< 0) {
323 error_setg(errp
, "CPU die-id is not set");
325 } else if (cpu
->die_id
> ms
->smp
.dies
- 1) {
326 error_setg(errp
, "Invalid CPU die-id: %u must be in range 0:%u",
327 cpu
->die_id
, ms
->smp
.dies
- 1);
330 if (cpu
->core_id
< 0) {
331 error_setg(errp
, "CPU core-id is not set");
333 } else if (cpu
->core_id
> (smp_cores
- 1)) {
334 error_setg(errp
, "Invalid CPU core-id: %u must be in range 0:%u",
335 cpu
->core_id
, smp_cores
- 1);
338 if (cpu
->thread_id
< 0) {
339 error_setg(errp
, "CPU thread-id is not set");
341 } else if (cpu
->thread_id
> (smp_threads
- 1)) {
342 error_setg(errp
, "Invalid CPU thread-id: %u must be in range 0:%u",
343 cpu
->thread_id
, smp_threads
- 1);
347 topo_ids
.pkg_id
= cpu
->socket_id
;
348 topo_ids
.die_id
= cpu
->die_id
;
349 topo_ids
.core_id
= cpu
->core_id
;
350 topo_ids
.smt_id
= cpu
->thread_id
;
351 cpu
->apic_id
= x86_apicid_from_topo_ids(&topo_info
, &topo_ids
);
354 cpu_slot
= x86_find_cpu_slot(MACHINE(x86ms
), cpu
->apic_id
, &idx
);
356 MachineState
*ms
= MACHINE(x86ms
);
358 x86_topo_ids_from_apicid(cpu
->apic_id
, &topo_info
, &topo_ids
);
360 "Invalid CPU [socket: %u, die: %u, core: %u, thread: %u] with"
361 " APIC ID %" PRIu32
", valid index range 0:%d",
362 topo_ids
.pkg_id
, topo_ids
.die_id
, topo_ids
.core_id
, topo_ids
.smt_id
,
363 cpu
->apic_id
, ms
->possible_cpus
->len
- 1);
368 error_setg(errp
, "CPU[%d] with APIC ID %" PRIu32
" exists",
373 /* if 'address' properties socket-id/core-id/thread-id are not set, set them
374 * so that machine_query_hotpluggable_cpus would show correct values
376 /* TODO: move socket_id/core_id/thread_id checks into x86_cpu_realizefn()
377 * once -smp refactoring is complete and there will be CPU private
378 * CPUState::nr_cores and CPUState::nr_threads fields instead of globals */
379 x86_topo_ids_from_apicid(cpu
->apic_id
, &topo_info
, &topo_ids
);
380 if (cpu
->socket_id
!= -1 && cpu
->socket_id
!= topo_ids
.pkg_id
) {
381 error_setg(errp
, "property socket-id: %u doesn't match set apic-id:"
382 " 0x%x (socket-id: %u)", cpu
->socket_id
, cpu
->apic_id
,
386 cpu
->socket_id
= topo_ids
.pkg_id
;
388 if (cpu
->die_id
!= -1 && cpu
->die_id
!= topo_ids
.die_id
) {
389 error_setg(errp
, "property die-id: %u doesn't match set apic-id:"
390 " 0x%x (die-id: %u)", cpu
->die_id
, cpu
->apic_id
, topo_ids
.die_id
);
393 cpu
->die_id
= topo_ids
.die_id
;
395 if (cpu
->core_id
!= -1 && cpu
->core_id
!= topo_ids
.core_id
) {
396 error_setg(errp
, "property core-id: %u doesn't match set apic-id:"
397 " 0x%x (core-id: %u)", cpu
->core_id
, cpu
->apic_id
,
401 cpu
->core_id
= topo_ids
.core_id
;
403 if (cpu
->thread_id
!= -1 && cpu
->thread_id
!= topo_ids
.smt_id
) {
404 error_setg(errp
, "property thread-id: %u doesn't match set apic-id:"
405 " 0x%x (thread-id: %u)", cpu
->thread_id
, cpu
->apic_id
,
409 cpu
->thread_id
= topo_ids
.smt_id
;
411 if (hyperv_feat_enabled(cpu
, HYPERV_FEAT_VPINDEX
) &&
412 !kvm_hv_vpindex_settable()) {
413 error_setg(errp
, "kernel doesn't allow setting HyperV VP_INDEX");
420 numa_cpu_pre_plug(cpu_slot
, dev
, errp
);
423 CpuInstanceProperties
424 x86_cpu_index_to_props(MachineState
*ms
, unsigned cpu_index
)
426 MachineClass
*mc
= MACHINE_GET_CLASS(ms
);
427 const CPUArchIdList
*possible_cpus
= mc
->possible_cpu_arch_ids(ms
);
429 assert(cpu_index
< possible_cpus
->len
);
430 return possible_cpus
->cpus
[cpu_index
].props
;
433 int64_t x86_get_default_cpu_node_id(const MachineState
*ms
, int idx
)
435 X86CPUTopoIDs topo_ids
;
436 X86MachineState
*x86ms
= X86_MACHINE(ms
);
437 X86CPUTopoInfo topo_info
;
439 init_topo_info(&topo_info
, x86ms
);
441 assert(idx
< ms
->possible_cpus
->len
);
442 x86_topo_ids_from_apicid(ms
->possible_cpus
->cpus
[idx
].arch_id
,
443 &topo_info
, &topo_ids
);
444 return topo_ids
.pkg_id
% ms
->numa_state
->num_nodes
;
447 const CPUArchIdList
*x86_possible_cpu_arch_ids(MachineState
*ms
)
449 X86MachineState
*x86ms
= X86_MACHINE(ms
);
450 unsigned int max_cpus
= ms
->smp
.max_cpus
;
451 X86CPUTopoInfo topo_info
;
454 if (ms
->possible_cpus
) {
456 * make sure that max_cpus hasn't changed since the first use, i.e.
457 * -smp hasn't been parsed after it
459 assert(ms
->possible_cpus
->len
== max_cpus
);
460 return ms
->possible_cpus
;
463 ms
->possible_cpus
= g_malloc0(sizeof(CPUArchIdList
) +
464 sizeof(CPUArchId
) * max_cpus
);
465 ms
->possible_cpus
->len
= max_cpus
;
467 init_topo_info(&topo_info
, x86ms
);
469 for (i
= 0; i
< ms
->possible_cpus
->len
; i
++) {
470 X86CPUTopoIDs topo_ids
;
472 ms
->possible_cpus
->cpus
[i
].type
= ms
->cpu_type
;
473 ms
->possible_cpus
->cpus
[i
].vcpus_count
= 1;
474 ms
->possible_cpus
->cpus
[i
].arch_id
=
475 x86_cpu_apic_id_from_index(x86ms
, i
);
476 x86_topo_ids_from_apicid(ms
->possible_cpus
->cpus
[i
].arch_id
,
477 &topo_info
, &topo_ids
);
478 ms
->possible_cpus
->cpus
[i
].props
.has_socket_id
= true;
479 ms
->possible_cpus
->cpus
[i
].props
.socket_id
= topo_ids
.pkg_id
;
480 if (ms
->smp
.dies
> 1) {
481 ms
->possible_cpus
->cpus
[i
].props
.has_die_id
= true;
482 ms
->possible_cpus
->cpus
[i
].props
.die_id
= topo_ids
.die_id
;
484 ms
->possible_cpus
->cpus
[i
].props
.has_core_id
= true;
485 ms
->possible_cpus
->cpus
[i
].props
.core_id
= topo_ids
.core_id
;
486 ms
->possible_cpus
->cpus
[i
].props
.has_thread_id
= true;
487 ms
->possible_cpus
->cpus
[i
].props
.thread_id
= topo_ids
.smt_id
;
489 return ms
->possible_cpus
;
492 static void x86_nmi(NMIState
*n
, int cpu_index
, Error
**errp
)
494 /* cpu index isn't used */
498 X86CPU
*cpu
= X86_CPU(cs
);
500 if (!cpu
->apic_state
) {
501 cpu_interrupt(cs
, CPU_INTERRUPT_NMI
);
503 apic_deliver_nmi(cpu
->apic_state
);
508 static long get_file_size(FILE *f
)
512 /* XXX: on Unix systems, using fstat() probably makes more sense */
515 fseek(f
, 0, SEEK_END
);
517 fseek(f
, where
, SEEK_SET
);
523 uint64_t cpu_get_tsc(CPUX86State
*env
)
525 return cpus_get_elapsed_ticks();
529 static void pic_irq_request(void *opaque
, int irq
, int level
)
531 CPUState
*cs
= first_cpu
;
532 X86CPU
*cpu
= X86_CPU(cs
);
534 trace_x86_pic_interrupt(irq
, level
);
535 if (cpu
->apic_state
&& !kvm_irqchip_in_kernel() &&
536 !whpx_apic_in_platform()) {
539 if (apic_accept_pic_intr(cpu
->apic_state
)) {
540 apic_deliver_pic_intr(cpu
->apic_state
, level
);
545 cpu_interrupt(cs
, CPU_INTERRUPT_HARD
);
547 cpu_reset_interrupt(cs
, CPU_INTERRUPT_HARD
);
552 qemu_irq
x86_allocate_cpu_irq(void)
554 return qemu_allocate_irq(pic_irq_request
, NULL
, 0);
557 int cpu_get_pic_interrupt(CPUX86State
*env
)
559 X86CPU
*cpu
= env_archcpu(env
);
562 if (!kvm_irqchip_in_kernel() && !whpx_apic_in_platform()) {
563 intno
= apic_get_interrupt(cpu
->apic_state
);
567 /* read the irq from the PIC */
568 if (!apic_accept_pic_intr(cpu
->apic_state
)) {
573 intno
= pic_read_irq(isa_pic
);
577 DeviceState
*cpu_get_current_apic(void)
580 X86CPU
*cpu
= X86_CPU(current_cpu
);
581 return cpu
->apic_state
;
587 void gsi_handler(void *opaque
, int n
, int level
)
589 GSIState
*s
= opaque
;
591 trace_x86_gsi_interrupt(n
, level
);
593 case 0 ... ISA_NUM_IRQS
- 1:
594 if (s
->i8259_irq
[n
]) {
595 /* Under KVM, Kernel will forward to both PIC and IOAPIC */
596 qemu_set_irq(s
->i8259_irq
[n
], level
);
599 case ISA_NUM_IRQS
... IOAPIC_NUM_PINS
- 1:
600 qemu_set_irq(s
->ioapic_irq
[n
], level
);
602 case IO_APIC_SECONDARY_IRQBASE
603 ... IO_APIC_SECONDARY_IRQBASE
+ IOAPIC_NUM_PINS
- 1:
604 qemu_set_irq(s
->ioapic2_irq
[n
- IO_APIC_SECONDARY_IRQBASE
], level
);
609 void ioapic_init_gsi(GSIState
*gsi_state
, const char *parent_name
)
616 if (kvm_ioapic_in_kernel()) {
617 dev
= qdev_new(TYPE_KVM_IOAPIC
);
619 dev
= qdev_new(TYPE_IOAPIC
);
621 object_property_add_child(object_resolve_path(parent_name
, NULL
),
622 "ioapic", OBJECT(dev
));
623 d
= SYS_BUS_DEVICE(dev
);
624 sysbus_realize_and_unref(d
, &error_fatal
);
625 sysbus_mmio_map(d
, 0, IO_APIC_DEFAULT_ADDRESS
);
627 for (i
= 0; i
< IOAPIC_NUM_PINS
; i
++) {
628 gsi_state
->ioapic_irq
[i
] = qdev_get_gpio_in(dev
, i
);
632 DeviceState
*ioapic_init_secondary(GSIState
*gsi_state
)
638 dev
= qdev_new(TYPE_IOAPIC
);
639 d
= SYS_BUS_DEVICE(dev
);
640 sysbus_realize_and_unref(d
, &error_fatal
);
641 sysbus_mmio_map(d
, 0, IO_APIC_SECONDARY_ADDRESS
);
643 for (i
= 0; i
< IOAPIC_NUM_PINS
; i
++) {
644 gsi_state
->ioapic2_irq
[i
] = qdev_get_gpio_in(dev
, i
);
654 } __attribute__((packed
));
658 * The entry point into the kernel for PVH boot is different from
659 * the native entry point. The PVH entry is defined by the x86/HVM
660 * direct boot ABI and is available in an ELFNOTE in the kernel binary.
662 * This function is passed to load_elf() when it is called from
663 * load_elfboot() which then additionally checks for an ELF Note of
664 * type XEN_ELFNOTE_PHYS32_ENTRY and passes it to this function to
665 * parse the PVH entry address from the ELF Note.
667 * Due to trickery in elf_opts.h, load_elf() is actually available as
668 * load_elf32() or load_elf64() and this routine needs to be able
669 * to deal with being called as 32 or 64 bit.
671 * The address of the PVH entry point is saved to the 'pvh_start_addr'
672 * global variable. (although the entry point is 32-bit, the kernel
673 * binary can be either 32-bit or 64-bit).
675 static uint64_t read_pvh_start_addr(void *arg1
, void *arg2
, bool is64
)
677 size_t *elf_note_data_addr
;
679 /* Check if ELF Note header passed in is valid */
685 struct elf64_note
*nhdr64
= (struct elf64_note
*)arg1
;
686 uint64_t nhdr_size64
= sizeof(struct elf64_note
);
687 uint64_t phdr_align
= *(uint64_t *)arg2
;
688 uint64_t nhdr_namesz
= nhdr64
->n_namesz
;
691 ((void *)nhdr64
) + nhdr_size64
+
692 QEMU_ALIGN_UP(nhdr_namesz
, phdr_align
);
694 pvh_start_addr
= *elf_note_data_addr
;
696 struct elf32_note
*nhdr32
= (struct elf32_note
*)arg1
;
697 uint32_t nhdr_size32
= sizeof(struct elf32_note
);
698 uint32_t phdr_align
= *(uint32_t *)arg2
;
699 uint32_t nhdr_namesz
= nhdr32
->n_namesz
;
702 ((void *)nhdr32
) + nhdr_size32
+
703 QEMU_ALIGN_UP(nhdr_namesz
, phdr_align
);
705 pvh_start_addr
= *(uint32_t *)elf_note_data_addr
;
708 return pvh_start_addr
;
711 static bool load_elfboot(const char *kernel_filename
,
712 int kernel_file_size
,
714 size_t pvh_xen_start_addr
,
718 uint32_t mh_load_addr
= 0;
719 uint32_t elf_kernel_size
= 0;
721 uint64_t elf_low
, elf_high
;
724 if (ldl_p(header
) != 0x464c457f) {
725 return false; /* no elfboot */
728 bool elf_is64
= header
[EI_CLASS
] == ELFCLASS64
;
730 ((Elf64_Ehdr
*)header
)->e_flags
: ((Elf32_Ehdr
*)header
)->e_flags
;
732 if (flags
& 0x00010004) { /* LOAD_ELF_HEADER_HAS_ADDR */
733 error_report("elfboot unsupported flags = %x", flags
);
737 uint64_t elf_note_type
= XEN_ELFNOTE_PHYS32_ENTRY
;
738 kernel_size
= load_elf(kernel_filename
, read_pvh_start_addr
,
739 NULL
, &elf_note_type
, &elf_entry
,
740 &elf_low
, &elf_high
, NULL
, 0, I386_ELF_MACHINE
,
743 if (kernel_size
< 0) {
744 error_report("Error while loading elf kernel");
747 mh_load_addr
= elf_low
;
748 elf_kernel_size
= elf_high
- elf_low
;
750 if (pvh_start_addr
== 0) {
751 error_report("Error loading uncompressed kernel without PVH ELF Note");
754 fw_cfg_add_i32(fw_cfg
, FW_CFG_KERNEL_ENTRY
, pvh_start_addr
);
755 fw_cfg_add_i32(fw_cfg
, FW_CFG_KERNEL_ADDR
, mh_load_addr
);
756 fw_cfg_add_i32(fw_cfg
, FW_CFG_KERNEL_SIZE
, elf_kernel_size
);
761 void x86_load_linux(X86MachineState
*x86ms
,
765 bool linuxboot_dma_enabled
)
768 int setup_size
, kernel_size
, cmdline_size
;
769 int dtb_size
, setup_data_offset
;
771 uint8_t header
[8192], *setup
, *kernel
;
772 hwaddr real_addr
, prot_addr
, cmdline_addr
, initrd_addr
= 0;
775 MachineState
*machine
= MACHINE(x86ms
);
776 struct setup_data
*setup_data
;
777 const char *kernel_filename
= machine
->kernel_filename
;
778 const char *initrd_filename
= machine
->initrd_filename
;
779 const char *dtb_filename
= machine
->dtb
;
780 const char *kernel_cmdline
= machine
->kernel_cmdline
;
782 /* Align to 16 bytes as a paranoia measure */
783 cmdline_size
= (strlen(kernel_cmdline
) + 16) & ~15;
785 /* load the kernel header */
786 f
= fopen(kernel_filename
, "rb");
788 fprintf(stderr
, "qemu: could not open kernel file '%s': %s\n",
789 kernel_filename
, strerror(errno
));
793 kernel_size
= get_file_size(f
);
795 fread(header
, 1, MIN(ARRAY_SIZE(header
), kernel_size
), f
) !=
796 MIN(ARRAY_SIZE(header
), kernel_size
)) {
797 fprintf(stderr
, "qemu: could not load kernel '%s': %s\n",
798 kernel_filename
, strerror(errno
));
802 /* kernel protocol version */
803 if (ldl_p(header
+ 0x202) == 0x53726448) {
804 protocol
= lduw_p(header
+ 0x206);
807 * This could be a multiboot kernel. If it is, let's stop treating it
808 * like a Linux kernel.
809 * Note: some multiboot images could be in the ELF format (the same of
810 * PVH), so we try multiboot first since we check the multiboot magic
811 * header before to load it.
813 if (load_multiboot(fw_cfg
, f
, kernel_filename
, initrd_filename
,
814 kernel_cmdline
, kernel_size
, header
)) {
818 * Check if the file is an uncompressed kernel file (ELF) and load it,
819 * saving the PVH entry point used by the x86/HVM direct boot ABI.
820 * If load_elfboot() is successful, populate the fw_cfg info.
823 load_elfboot(kernel_filename
, kernel_size
,
824 header
, pvh_start_addr
, fw_cfg
)) {
827 fw_cfg_add_i32(fw_cfg
, FW_CFG_CMDLINE_SIZE
,
828 strlen(kernel_cmdline
) + 1);
829 fw_cfg_add_string(fw_cfg
, FW_CFG_CMDLINE_DATA
, kernel_cmdline
);
831 fw_cfg_add_i32(fw_cfg
, FW_CFG_SETUP_SIZE
, sizeof(header
));
832 fw_cfg_add_bytes(fw_cfg
, FW_CFG_SETUP_DATA
,
833 header
, sizeof(header
));
836 if (initrd_filename
) {
837 GMappedFile
*mapped_file
;
842 mapped_file
= g_mapped_file_new(initrd_filename
, false, &gerr
);
844 fprintf(stderr
, "qemu: error reading initrd %s: %s\n",
845 initrd_filename
, gerr
->message
);
848 x86ms
->initrd_mapped_file
= mapped_file
;
850 initrd_data
= g_mapped_file_get_contents(mapped_file
);
851 initrd_size
= g_mapped_file_get_length(mapped_file
);
852 initrd_max
= x86ms
->below_4g_mem_size
- acpi_data_size
- 1;
853 if (initrd_size
>= initrd_max
) {
854 fprintf(stderr
, "qemu: initrd is too large, cannot support."
855 "(max: %"PRIu32
", need %"PRId64
")\n",
856 initrd_max
, (uint64_t)initrd_size
);
860 initrd_addr
= (initrd_max
- initrd_size
) & ~4095;
862 fw_cfg_add_i32(fw_cfg
, FW_CFG_INITRD_ADDR
, initrd_addr
);
863 fw_cfg_add_i32(fw_cfg
, FW_CFG_INITRD_SIZE
, initrd_size
);
864 fw_cfg_add_bytes(fw_cfg
, FW_CFG_INITRD_DATA
, initrd_data
,
868 option_rom
[nb_option_roms
].bootindex
= 0;
869 option_rom
[nb_option_roms
].name
= "pvh.bin";
877 if (protocol
< 0x200 || !(header
[0x211] & 0x01)) {
880 cmdline_addr
= 0x9a000 - cmdline_size
;
882 } else if (protocol
< 0x202) {
883 /* High but ancient kernel */
885 cmdline_addr
= 0x9a000 - cmdline_size
;
886 prot_addr
= 0x100000;
888 /* High and recent kernel */
890 cmdline_addr
= 0x20000;
891 prot_addr
= 0x100000;
894 /* highest address for loading the initrd */
895 if (protocol
>= 0x20c &&
896 lduw_p(header
+ 0x236) & XLF_CAN_BE_LOADED_ABOVE_4G
) {
898 * Linux has supported initrd up to 4 GB for a very long time (2007,
899 * long before XLF_CAN_BE_LOADED_ABOVE_4G which was added in 2013),
900 * though it only sets initrd_max to 2 GB to "work around bootloader
901 * bugs". Luckily, QEMU firmware(which does something like bootloader)
902 * has supported this.
904 * It's believed that if XLF_CAN_BE_LOADED_ABOVE_4G is set, initrd can
905 * be loaded into any address.
907 * In addition, initrd_max is uint32_t simply because QEMU doesn't
908 * support the 64-bit boot protocol (specifically the ext_ramdisk_image
911 * Therefore here just limit initrd_max to UINT32_MAX simply as well.
913 initrd_max
= UINT32_MAX
;
914 } else if (protocol
>= 0x203) {
915 initrd_max
= ldl_p(header
+ 0x22c);
917 initrd_max
= 0x37ffffff;
920 if (initrd_max
>= x86ms
->below_4g_mem_size
- acpi_data_size
) {
921 initrd_max
= x86ms
->below_4g_mem_size
- acpi_data_size
- 1;
924 fw_cfg_add_i32(fw_cfg
, FW_CFG_CMDLINE_ADDR
, cmdline_addr
);
925 fw_cfg_add_i32(fw_cfg
, FW_CFG_CMDLINE_SIZE
, strlen(kernel_cmdline
) + 1);
926 fw_cfg_add_string(fw_cfg
, FW_CFG_CMDLINE_DATA
, kernel_cmdline
);
928 if (protocol
>= 0x202) {
929 stl_p(header
+ 0x228, cmdline_addr
);
931 stw_p(header
+ 0x20, 0xA33F);
932 stw_p(header
+ 0x22, cmdline_addr
- real_addr
);
935 /* handle vga= parameter */
936 vmode
= strstr(kernel_cmdline
, "vga=");
938 unsigned int video_mode
;
943 if (!strncmp(vmode
, "normal", 6)) {
945 } else if (!strncmp(vmode
, "ext", 3)) {
947 } else if (!strncmp(vmode
, "ask", 3)) {
950 ret
= qemu_strtoui(vmode
, &end
, 0, &video_mode
);
951 if (ret
!= 0 || (*end
&& *end
!= ' ')) {
952 fprintf(stderr
, "qemu: invalid 'vga=' kernel parameter.\n");
956 stw_p(header
+ 0x1fa, video_mode
);
961 * High nybble = B reserved for QEMU; low nybble is revision number.
962 * If this code is substantially changed, you may want to consider
963 * incrementing the revision.
965 if (protocol
>= 0x200) {
966 header
[0x210] = 0xB0;
969 if (protocol
>= 0x201) {
970 header
[0x211] |= 0x80; /* CAN_USE_HEAP */
971 stw_p(header
+ 0x224, cmdline_addr
- real_addr
- 0x200);
975 if (initrd_filename
) {
976 GMappedFile
*mapped_file
;
981 if (protocol
< 0x200) {
982 fprintf(stderr
, "qemu: linux kernel too old to load a ram disk\n");
986 mapped_file
= g_mapped_file_new(initrd_filename
, false, &gerr
);
988 fprintf(stderr
, "qemu: error reading initrd %s: %s\n",
989 initrd_filename
, gerr
->message
);
992 x86ms
->initrd_mapped_file
= mapped_file
;
994 initrd_data
= g_mapped_file_get_contents(mapped_file
);
995 initrd_size
= g_mapped_file_get_length(mapped_file
);
996 if (initrd_size
>= initrd_max
) {
997 fprintf(stderr
, "qemu: initrd is too large, cannot support."
998 "(max: %"PRIu32
", need %"PRId64
")\n",
999 initrd_max
, (uint64_t)initrd_size
);
1003 initrd_addr
= (initrd_max
- initrd_size
) & ~4095;
1005 fw_cfg_add_i32(fw_cfg
, FW_CFG_INITRD_ADDR
, initrd_addr
);
1006 fw_cfg_add_i32(fw_cfg
, FW_CFG_INITRD_SIZE
, initrd_size
);
1007 fw_cfg_add_bytes(fw_cfg
, FW_CFG_INITRD_DATA
, initrd_data
, initrd_size
);
1009 stl_p(header
+ 0x218, initrd_addr
);
1010 stl_p(header
+ 0x21c, initrd_size
);
1013 /* load kernel and setup */
1014 setup_size
= header
[0x1f1];
1015 if (setup_size
== 0) {
1018 setup_size
= (setup_size
+ 1) * 512;
1019 if (setup_size
> kernel_size
) {
1020 fprintf(stderr
, "qemu: invalid kernel header\n");
1023 kernel_size
-= setup_size
;
1025 setup
= g_malloc(setup_size
);
1026 kernel
= g_malloc(kernel_size
);
1027 fseek(f
, 0, SEEK_SET
);
1028 if (fread(setup
, 1, setup_size
, f
) != setup_size
) {
1029 fprintf(stderr
, "fread() failed\n");
1032 if (fread(kernel
, 1, kernel_size
, f
) != kernel_size
) {
1033 fprintf(stderr
, "fread() failed\n");
1038 /* append dtb to kernel */
1040 if (protocol
< 0x209) {
1041 fprintf(stderr
, "qemu: Linux kernel too old to load a dtb\n");
1045 dtb_size
= get_image_size(dtb_filename
);
1046 if (dtb_size
<= 0) {
1047 fprintf(stderr
, "qemu: error reading dtb %s: %s\n",
1048 dtb_filename
, strerror(errno
));
1052 setup_data_offset
= QEMU_ALIGN_UP(kernel_size
, 16);
1053 kernel_size
= setup_data_offset
+ sizeof(struct setup_data
) + dtb_size
;
1054 kernel
= g_realloc(kernel
, kernel_size
);
1056 stq_p(header
+ 0x250, prot_addr
+ setup_data_offset
);
1058 setup_data
= (struct setup_data
*)(kernel
+ setup_data_offset
);
1059 setup_data
->next
= 0;
1060 setup_data
->type
= cpu_to_le32(SETUP_DTB
);
1061 setup_data
->len
= cpu_to_le32(dtb_size
);
1063 load_image_size(dtb_filename
, setup_data
->data
, dtb_size
);
1066 memcpy(setup
, header
, MIN(sizeof(header
), setup_size
));
1068 fw_cfg_add_i32(fw_cfg
, FW_CFG_KERNEL_ADDR
, prot_addr
);
1069 fw_cfg_add_i32(fw_cfg
, FW_CFG_KERNEL_SIZE
, kernel_size
);
1070 fw_cfg_add_bytes(fw_cfg
, FW_CFG_KERNEL_DATA
, kernel
, kernel_size
);
1072 fw_cfg_add_i32(fw_cfg
, FW_CFG_SETUP_ADDR
, real_addr
);
1073 fw_cfg_add_i32(fw_cfg
, FW_CFG_SETUP_SIZE
, setup_size
);
1074 fw_cfg_add_bytes(fw_cfg
, FW_CFG_SETUP_DATA
, setup
, setup_size
);
1076 option_rom
[nb_option_roms
].bootindex
= 0;
1077 option_rom
[nb_option_roms
].name
= "linuxboot.bin";
1078 if (linuxboot_dma_enabled
&& fw_cfg_dma_enabled(fw_cfg
)) {
1079 option_rom
[nb_option_roms
].name
= "linuxboot_dma.bin";
1084 void x86_bios_rom_init(MachineState
*ms
, const char *default_firmware
,
1085 MemoryRegion
*rom_memory
, bool isapc_ram_fw
)
1087 const char *bios_name
;
1089 MemoryRegion
*bios
, *isa_bios
;
1090 int bios_size
, isa_bios_size
;
1094 bios_name
= ms
->firmware
?: default_firmware
;
1095 filename
= qemu_find_file(QEMU_FILE_TYPE_BIOS
, bios_name
);
1097 bios_size
= get_image_size(filename
);
1101 if (bios_size
<= 0 ||
1102 (bios_size
% 65536) != 0) {
1105 bios
= g_malloc(sizeof(*bios
));
1106 memory_region_init_ram(bios
, NULL
, "pc.bios", bios_size
, &error_fatal
);
1107 if (!isapc_ram_fw
) {
1108 memory_region_set_readonly(bios
, true);
1110 ret
= rom_add_file_fixed(bios_name
, (uint32_t)(-bios_size
), -1);
1113 fprintf(stderr
, "qemu: could not load PC BIOS '%s'\n", bios_name
);
1118 /* map the last 128KB of the BIOS in ISA space */
1119 isa_bios_size
= MIN(bios_size
, 128 * KiB
);
1120 isa_bios
= g_malloc(sizeof(*isa_bios
));
1121 memory_region_init_alias(isa_bios
, NULL
, "isa-bios", bios
,
1122 bios_size
- isa_bios_size
, isa_bios_size
);
1123 memory_region_add_subregion_overlap(rom_memory
,
1124 0x100000 - isa_bios_size
,
1127 if (!isapc_ram_fw
) {
1128 memory_region_set_readonly(isa_bios
, true);
1131 /* map all the bios at the top of memory */
1132 memory_region_add_subregion(rom_memory
,
1133 (uint32_t)(-bios_size
),
1137 bool x86_machine_is_smm_enabled(const X86MachineState
*x86ms
)
1139 bool smm_available
= false;
1141 if (x86ms
->smm
== ON_OFF_AUTO_OFF
) {
1145 if (tcg_enabled() || qtest_enabled()) {
1146 smm_available
= true;
1147 } else if (kvm_enabled()) {
1148 smm_available
= kvm_has_smm();
1151 if (smm_available
) {
1155 if (x86ms
->smm
== ON_OFF_AUTO_ON
) {
1156 error_report("System Management Mode not supported by this hypervisor.");
1162 static void x86_machine_get_smm(Object
*obj
, Visitor
*v
, const char *name
,
1163 void *opaque
, Error
**errp
)
1165 X86MachineState
*x86ms
= X86_MACHINE(obj
);
1166 OnOffAuto smm
= x86ms
->smm
;
1168 visit_type_OnOffAuto(v
, name
, &smm
, errp
);
1171 static void x86_machine_set_smm(Object
*obj
, Visitor
*v
, const char *name
,
1172 void *opaque
, Error
**errp
)
1174 X86MachineState
*x86ms
= X86_MACHINE(obj
);
1176 visit_type_OnOffAuto(v
, name
, &x86ms
->smm
, errp
);
1179 bool x86_machine_is_acpi_enabled(const X86MachineState
*x86ms
)
1181 if (x86ms
->acpi
== ON_OFF_AUTO_OFF
) {
1187 static void x86_machine_get_acpi(Object
*obj
, Visitor
*v
, const char *name
,
1188 void *opaque
, Error
**errp
)
1190 X86MachineState
*x86ms
= X86_MACHINE(obj
);
1191 OnOffAuto acpi
= x86ms
->acpi
;
1193 visit_type_OnOffAuto(v
, name
, &acpi
, errp
);
1196 static void x86_machine_set_acpi(Object
*obj
, Visitor
*v
, const char *name
,
1197 void *opaque
, Error
**errp
)
1199 X86MachineState
*x86ms
= X86_MACHINE(obj
);
1201 visit_type_OnOffAuto(v
, name
, &x86ms
->acpi
, errp
);
1204 static char *x86_machine_get_oem_id(Object
*obj
, Error
**errp
)
1206 X86MachineState
*x86ms
= X86_MACHINE(obj
);
1208 return g_strdup(x86ms
->oem_id
);
1211 static void x86_machine_set_oem_id(Object
*obj
, const char *value
, Error
**errp
)
1213 X86MachineState
*x86ms
= X86_MACHINE(obj
);
1214 size_t len
= strlen(value
);
1218 "User specified "X86_MACHINE_OEM_ID
" value is bigger than "
1223 strncpy(x86ms
->oem_id
, value
, 6);
1226 static char *x86_machine_get_oem_table_id(Object
*obj
, Error
**errp
)
1228 X86MachineState
*x86ms
= X86_MACHINE(obj
);
1230 return g_strdup(x86ms
->oem_table_id
);
1233 static void x86_machine_set_oem_table_id(Object
*obj
, const char *value
,
1236 X86MachineState
*x86ms
= X86_MACHINE(obj
);
1237 size_t len
= strlen(value
);
1241 "User specified "X86_MACHINE_OEM_TABLE_ID
1242 " value is bigger than "
1246 strncpy(x86ms
->oem_table_id
, value
, 8);
1249 static void x86_machine_get_bus_lock_ratelimit(Object
*obj
, Visitor
*v
,
1250 const char *name
, void *opaque
, Error
**errp
)
1252 X86MachineState
*x86ms
= X86_MACHINE(obj
);
1253 uint64_t bus_lock_ratelimit
= x86ms
->bus_lock_ratelimit
;
1255 visit_type_uint64(v
, name
, &bus_lock_ratelimit
, errp
);
1258 static void x86_machine_set_bus_lock_ratelimit(Object
*obj
, Visitor
*v
,
1259 const char *name
, void *opaque
, Error
**errp
)
1261 X86MachineState
*x86ms
= X86_MACHINE(obj
);
1263 visit_type_uint64(v
, name
, &x86ms
->bus_lock_ratelimit
, errp
);
1266 static void x86_machine_initfn(Object
*obj
)
1268 X86MachineState
*x86ms
= X86_MACHINE(obj
);
1270 x86ms
->smm
= ON_OFF_AUTO_AUTO
;
1271 x86ms
->acpi
= ON_OFF_AUTO_AUTO
;
1272 x86ms
->pci_irq_mask
= ACPI_BUILD_PCI_IRQS
;
1273 x86ms
->oem_id
= g_strndup(ACPI_BUILD_APPNAME6
, 6);
1274 x86ms
->oem_table_id
= g_strndup(ACPI_BUILD_APPNAME8
, 8);
1275 x86ms
->bus_lock_ratelimit
= 0;
1278 static void x86_machine_class_init(ObjectClass
*oc
, void *data
)
1280 MachineClass
*mc
= MACHINE_CLASS(oc
);
1281 X86MachineClass
*x86mc
= X86_MACHINE_CLASS(oc
);
1282 NMIClass
*nc
= NMI_CLASS(oc
);
1284 mc
->cpu_index_to_instance_props
= x86_cpu_index_to_props
;
1285 mc
->get_default_cpu_node_id
= x86_get_default_cpu_node_id
;
1286 mc
->possible_cpu_arch_ids
= x86_possible_cpu_arch_ids
;
1287 x86mc
->compat_apic_id_mode
= false;
1288 x86mc
->save_tsc_khz
= true;
1289 nc
->nmi_monitor_handler
= x86_nmi
;
1291 object_class_property_add(oc
, X86_MACHINE_SMM
, "OnOffAuto",
1292 x86_machine_get_smm
, x86_machine_set_smm
,
1294 object_class_property_set_description(oc
, X86_MACHINE_SMM
,
1297 object_class_property_add(oc
, X86_MACHINE_ACPI
, "OnOffAuto",
1298 x86_machine_get_acpi
, x86_machine_set_acpi
,
1300 object_class_property_set_description(oc
, X86_MACHINE_ACPI
,
1303 object_class_property_add_str(oc
, X86_MACHINE_OEM_ID
,
1304 x86_machine_get_oem_id
,
1305 x86_machine_set_oem_id
);
1306 object_class_property_set_description(oc
, X86_MACHINE_OEM_ID
,
1307 "Override the default value of field OEMID "
1308 "in ACPI table header."
1309 "The string may be up to 6 bytes in size");
1312 object_class_property_add_str(oc
, X86_MACHINE_OEM_TABLE_ID
,
1313 x86_machine_get_oem_table_id
,
1314 x86_machine_set_oem_table_id
);
1315 object_class_property_set_description(oc
, X86_MACHINE_OEM_TABLE_ID
,
1316 "Override the default value of field OEM Table ID "
1317 "in ACPI table header."
1318 "The string may be up to 8 bytes in size");
1320 object_class_property_add(oc
, X86_MACHINE_BUS_LOCK_RATELIMIT
, "uint64_t",
1321 x86_machine_get_bus_lock_ratelimit
,
1322 x86_machine_set_bus_lock_ratelimit
, NULL
, NULL
);
1323 object_class_property_set_description(oc
, X86_MACHINE_BUS_LOCK_RATELIMIT
,
1324 "Set the ratelimit for the bus locks acquired in VMs");
1327 static const TypeInfo x86_machine_info
= {
1328 .name
= TYPE_X86_MACHINE
,
1329 .parent
= TYPE_MACHINE
,
1331 .instance_size
= sizeof(X86MachineState
),
1332 .instance_init
= x86_machine_initfn
,
1333 .class_size
= sizeof(X86MachineClass
),
1334 .class_init
= x86_machine_class_init
,
1335 .interfaces
= (InterfaceInfo
[]) {
1341 static void x86_machine_register_types(void)
1343 type_register_static(&x86_machine_info
);
1346 type_init(x86_machine_register_types
)