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 "qapi/error.h"
30 #include "qapi/qmp/qerror.h"
31 #include "qapi/qapi-visit-common.h"
32 #include "qapi/visitor.h"
33 #include "sysemu/qtest.h"
34 #include "sysemu/numa.h"
35 #include "sysemu/replay.h"
36 #include "sysemu/sysemu.h"
38 #include "hw/i386/x86.h"
39 #include "target/i386/cpu.h"
40 #include "hw/i386/topology.h"
41 #include "hw/i386/fw_cfg.h"
43 #include "hw/acpi/cpu_hotplug.h"
45 #include "hw/loader.h"
46 #include "multiboot.h"
48 #include "standard-headers/asm-x86/bootparam.h"
50 #define BIOS_FILENAME "bios.bin"
52 /* Physical Address of PVH entry point read from kernel ELF NOTE */
53 static size_t pvh_start_addr
;
56 * Calculates initial APIC ID for a specific CPU index
58 * Currently we need to be able to calculate the APIC ID from the CPU index
59 * alone (without requiring a CPU object), as the QEMU<->Seabios interfaces have
60 * no concept of "CPU index", and the NUMA tables on fw_cfg need the APIC ID of
61 * all CPUs up to max_cpus.
63 uint32_t x86_cpu_apic_id_from_index(X86MachineState
*x86ms
,
64 unsigned int cpu_index
)
66 MachineState
*ms
= MACHINE(x86ms
);
67 X86MachineClass
*x86mc
= X86_MACHINE_GET_CLASS(x86ms
);
71 correct_id
= x86_apicid_from_cpu_idx(x86ms
->smp_dies
, ms
->smp
.cores
,
72 ms
->smp
.threads
, cpu_index
);
73 if (x86mc
->compat_apic_id_mode
) {
74 if (cpu_index
!= correct_id
&& !warned
&& !qtest_enabled()) {
75 error_report("APIC IDs set in compatibility mode, "
76 "CPU topology won't match the configuration");
86 void x86_cpu_new(X86MachineState
*x86ms
, int64_t apic_id
, Error
**errp
)
89 Error
*local_err
= NULL
;
90 CPUX86State
*env
= NULL
;
92 cpu
= object_new(MACHINE(x86ms
)->cpu_type
);
94 env
= &X86_CPU(cpu
)->env
;
95 env
->nr_dies
= x86ms
->smp_dies
;
97 object_property_set_uint(cpu
, apic_id
, "apic-id", &local_err
);
98 object_property_set_bool(cpu
, true, "realized", &local_err
);
101 error_propagate(errp
, local_err
);
104 void x86_cpus_init(X86MachineState
*x86ms
, int default_cpu_version
)
107 const CPUArchIdList
*possible_cpus
;
108 MachineState
*ms
= MACHINE(x86ms
);
109 MachineClass
*mc
= MACHINE_GET_CLASS(x86ms
);
111 x86_cpu_set_default_version(default_cpu_version
);
114 * Calculates the limit to CPU APIC ID values
116 * Limit for the APIC ID value, so that all
117 * CPU APIC IDs are < x86ms->apic_id_limit.
119 * This is used for FW_CFG_MAX_CPUS. See comments on fw_cfg_arch_create().
121 x86ms
->apic_id_limit
= x86_cpu_apic_id_from_index(x86ms
,
122 ms
->smp
.max_cpus
- 1) + 1;
123 possible_cpus
= mc
->possible_cpu_arch_ids(ms
);
124 for (i
= 0; i
< ms
->smp
.cpus
; i
++) {
125 x86_cpu_new(x86ms
, possible_cpus
->cpus
[i
].arch_id
, &error_fatal
);
129 CpuInstanceProperties
130 x86_cpu_index_to_props(MachineState
*ms
, unsigned cpu_index
)
132 MachineClass
*mc
= MACHINE_GET_CLASS(ms
);
133 const CPUArchIdList
*possible_cpus
= mc
->possible_cpu_arch_ids(ms
);
135 assert(cpu_index
< possible_cpus
->len
);
136 return possible_cpus
->cpus
[cpu_index
].props
;
139 int64_t x86_get_default_cpu_node_id(const MachineState
*ms
, int idx
)
142 X86MachineState
*x86ms
= X86_MACHINE(ms
);
144 assert(idx
< ms
->possible_cpus
->len
);
145 x86_topo_ids_from_apicid(ms
->possible_cpus
->cpus
[idx
].arch_id
,
146 x86ms
->smp_dies
, ms
->smp
.cores
,
147 ms
->smp
.threads
, &topo
);
148 return topo
.pkg_id
% ms
->numa_state
->num_nodes
;
151 const CPUArchIdList
*x86_possible_cpu_arch_ids(MachineState
*ms
)
153 X86MachineState
*x86ms
= X86_MACHINE(ms
);
155 unsigned int max_cpus
= ms
->smp
.max_cpus
;
157 if (ms
->possible_cpus
) {
159 * make sure that max_cpus hasn't changed since the first use, i.e.
160 * -smp hasn't been parsed after it
162 assert(ms
->possible_cpus
->len
== max_cpus
);
163 return ms
->possible_cpus
;
166 ms
->possible_cpus
= g_malloc0(sizeof(CPUArchIdList
) +
167 sizeof(CPUArchId
) * max_cpus
);
168 ms
->possible_cpus
->len
= max_cpus
;
169 for (i
= 0; i
< ms
->possible_cpus
->len
; i
++) {
172 ms
->possible_cpus
->cpus
[i
].type
= ms
->cpu_type
;
173 ms
->possible_cpus
->cpus
[i
].vcpus_count
= 1;
174 ms
->possible_cpus
->cpus
[i
].arch_id
=
175 x86_cpu_apic_id_from_index(x86ms
, i
);
176 x86_topo_ids_from_apicid(ms
->possible_cpus
->cpus
[i
].arch_id
,
177 x86ms
->smp_dies
, ms
->smp
.cores
,
178 ms
->smp
.threads
, &topo
);
179 ms
->possible_cpus
->cpus
[i
].props
.has_socket_id
= true;
180 ms
->possible_cpus
->cpus
[i
].props
.socket_id
= topo
.pkg_id
;
181 if (x86ms
->smp_dies
> 1) {
182 ms
->possible_cpus
->cpus
[i
].props
.has_die_id
= true;
183 ms
->possible_cpus
->cpus
[i
].props
.die_id
= topo
.die_id
;
185 ms
->possible_cpus
->cpus
[i
].props
.has_core_id
= true;
186 ms
->possible_cpus
->cpus
[i
].props
.core_id
= topo
.core_id
;
187 ms
->possible_cpus
->cpus
[i
].props
.has_thread_id
= true;
188 ms
->possible_cpus
->cpus
[i
].props
.thread_id
= topo
.smt_id
;
190 return ms
->possible_cpus
;
193 static void x86_nmi(NMIState
*n
, int cpu_index
, Error
**errp
)
195 /* cpu index isn't used */
199 X86CPU
*cpu
= X86_CPU(cs
);
201 if (!cpu
->apic_state
) {
202 cpu_interrupt(cs
, CPU_INTERRUPT_NMI
);
204 apic_deliver_nmi(cpu
->apic_state
);
209 static long get_file_size(FILE *f
)
213 /* XXX: on Unix systems, using fstat() probably makes more sense */
216 fseek(f
, 0, SEEK_END
);
218 fseek(f
, where
, SEEK_SET
);
228 } __attribute__((packed
));
232 * The entry point into the kernel for PVH boot is different from
233 * the native entry point. The PVH entry is defined by the x86/HVM
234 * direct boot ABI and is available in an ELFNOTE in the kernel binary.
236 * This function is passed to load_elf() when it is called from
237 * load_elfboot() which then additionally checks for an ELF Note of
238 * type XEN_ELFNOTE_PHYS32_ENTRY and passes it to this function to
239 * parse the PVH entry address from the ELF Note.
241 * Due to trickery in elf_opts.h, load_elf() is actually available as
242 * load_elf32() or load_elf64() and this routine needs to be able
243 * to deal with being called as 32 or 64 bit.
245 * The address of the PVH entry point is saved to the 'pvh_start_addr'
246 * global variable. (although the entry point is 32-bit, the kernel
247 * binary can be either 32-bit or 64-bit).
249 static uint64_t read_pvh_start_addr(void *arg1
, void *arg2
, bool is64
)
251 size_t *elf_note_data_addr
;
253 /* Check if ELF Note header passed in is valid */
259 struct elf64_note
*nhdr64
= (struct elf64_note
*)arg1
;
260 uint64_t nhdr_size64
= sizeof(struct elf64_note
);
261 uint64_t phdr_align
= *(uint64_t *)arg2
;
262 uint64_t nhdr_namesz
= nhdr64
->n_namesz
;
265 ((void *)nhdr64
) + nhdr_size64
+
266 QEMU_ALIGN_UP(nhdr_namesz
, phdr_align
);
268 struct elf32_note
*nhdr32
= (struct elf32_note
*)arg1
;
269 uint32_t nhdr_size32
= sizeof(struct elf32_note
);
270 uint32_t phdr_align
= *(uint32_t *)arg2
;
271 uint32_t nhdr_namesz
= nhdr32
->n_namesz
;
274 ((void *)nhdr32
) + nhdr_size32
+
275 QEMU_ALIGN_UP(nhdr_namesz
, phdr_align
);
278 pvh_start_addr
= *elf_note_data_addr
;
280 return pvh_start_addr
;
283 static bool load_elfboot(const char *kernel_filename
,
284 int kernel_file_size
,
286 size_t pvh_xen_start_addr
,
290 uint32_t mh_load_addr
= 0;
291 uint32_t elf_kernel_size
= 0;
293 uint64_t elf_low
, elf_high
;
296 if (ldl_p(header
) != 0x464c457f) {
297 return false; /* no elfboot */
300 bool elf_is64
= header
[EI_CLASS
] == ELFCLASS64
;
302 ((Elf64_Ehdr
*)header
)->e_flags
: ((Elf32_Ehdr
*)header
)->e_flags
;
304 if (flags
& 0x00010004) { /* LOAD_ELF_HEADER_HAS_ADDR */
305 error_report("elfboot unsupported flags = %x", flags
);
309 uint64_t elf_note_type
= XEN_ELFNOTE_PHYS32_ENTRY
;
310 kernel_size
= load_elf(kernel_filename
, read_pvh_start_addr
,
311 NULL
, &elf_note_type
, &elf_entry
,
312 &elf_low
, &elf_high
, 0, I386_ELF_MACHINE
,
315 if (kernel_size
< 0) {
316 error_report("Error while loading elf kernel");
319 mh_load_addr
= elf_low
;
320 elf_kernel_size
= elf_high
- elf_low
;
322 if (pvh_start_addr
== 0) {
323 error_report("Error loading uncompressed kernel without PVH ELF Note");
326 fw_cfg_add_i32(fw_cfg
, FW_CFG_KERNEL_ENTRY
, pvh_start_addr
);
327 fw_cfg_add_i32(fw_cfg
, FW_CFG_KERNEL_ADDR
, mh_load_addr
);
328 fw_cfg_add_i32(fw_cfg
, FW_CFG_KERNEL_SIZE
, elf_kernel_size
);
333 void x86_load_linux(X86MachineState
*x86ms
,
337 bool linuxboot_dma_enabled
)
340 int setup_size
, kernel_size
, cmdline_size
;
341 int dtb_size
, setup_data_offset
;
343 uint8_t header
[8192], *setup
, *kernel
;
344 hwaddr real_addr
, prot_addr
, cmdline_addr
, initrd_addr
= 0;
347 MachineState
*machine
= MACHINE(x86ms
);
348 struct setup_data
*setup_data
;
349 const char *kernel_filename
= machine
->kernel_filename
;
350 const char *initrd_filename
= machine
->initrd_filename
;
351 const char *dtb_filename
= machine
->dtb
;
352 const char *kernel_cmdline
= machine
->kernel_cmdline
;
354 /* Align to 16 bytes as a paranoia measure */
355 cmdline_size
= (strlen(kernel_cmdline
) + 16) & ~15;
357 /* load the kernel header */
358 f
= fopen(kernel_filename
, "rb");
360 fprintf(stderr
, "qemu: could not open kernel file '%s': %s\n",
361 kernel_filename
, strerror(errno
));
365 kernel_size
= get_file_size(f
);
367 fread(header
, 1, MIN(ARRAY_SIZE(header
), kernel_size
), f
) !=
368 MIN(ARRAY_SIZE(header
), kernel_size
)) {
369 fprintf(stderr
, "qemu: could not load kernel '%s': %s\n",
370 kernel_filename
, strerror(errno
));
374 /* kernel protocol version */
375 if (ldl_p(header
+ 0x202) == 0x53726448) {
376 protocol
= lduw_p(header
+ 0x206);
379 * This could be a multiboot kernel. If it is, let's stop treating it
380 * like a Linux kernel.
381 * Note: some multiboot images could be in the ELF format (the same of
382 * PVH), so we try multiboot first since we check the multiboot magic
383 * header before to load it.
385 if (load_multiboot(fw_cfg
, f
, kernel_filename
, initrd_filename
,
386 kernel_cmdline
, kernel_size
, header
)) {
390 * Check if the file is an uncompressed kernel file (ELF) and load it,
391 * saving the PVH entry point used by the x86/HVM direct boot ABI.
392 * If load_elfboot() is successful, populate the fw_cfg info.
395 load_elfboot(kernel_filename
, kernel_size
,
396 header
, pvh_start_addr
, fw_cfg
)) {
399 fw_cfg_add_i32(fw_cfg
, FW_CFG_CMDLINE_SIZE
,
400 strlen(kernel_cmdline
) + 1);
401 fw_cfg_add_string(fw_cfg
, FW_CFG_CMDLINE_DATA
, kernel_cmdline
);
403 fw_cfg_add_i32(fw_cfg
, FW_CFG_SETUP_SIZE
, sizeof(header
));
404 fw_cfg_add_bytes(fw_cfg
, FW_CFG_SETUP_DATA
,
405 header
, sizeof(header
));
408 if (initrd_filename
) {
409 GMappedFile
*mapped_file
;
414 mapped_file
= g_mapped_file_new(initrd_filename
, false, &gerr
);
416 fprintf(stderr
, "qemu: error reading initrd %s: %s\n",
417 initrd_filename
, gerr
->message
);
420 x86ms
->initrd_mapped_file
= mapped_file
;
422 initrd_data
= g_mapped_file_get_contents(mapped_file
);
423 initrd_size
= g_mapped_file_get_length(mapped_file
);
424 initrd_max
= x86ms
->below_4g_mem_size
- acpi_data_size
- 1;
425 if (initrd_size
>= initrd_max
) {
426 fprintf(stderr
, "qemu: initrd is too large, cannot support."
427 "(max: %"PRIu32
", need %"PRId64
")\n",
428 initrd_max
, (uint64_t)initrd_size
);
432 initrd_addr
= (initrd_max
- initrd_size
) & ~4095;
434 fw_cfg_add_i32(fw_cfg
, FW_CFG_INITRD_ADDR
, initrd_addr
);
435 fw_cfg_add_i32(fw_cfg
, FW_CFG_INITRD_SIZE
, initrd_size
);
436 fw_cfg_add_bytes(fw_cfg
, FW_CFG_INITRD_DATA
, initrd_data
,
440 option_rom
[nb_option_roms
].bootindex
= 0;
441 option_rom
[nb_option_roms
].name
= "pvh.bin";
449 if (protocol
< 0x200 || !(header
[0x211] & 0x01)) {
452 cmdline_addr
= 0x9a000 - cmdline_size
;
454 } else if (protocol
< 0x202) {
455 /* High but ancient kernel */
457 cmdline_addr
= 0x9a000 - cmdline_size
;
458 prot_addr
= 0x100000;
460 /* High and recent kernel */
462 cmdline_addr
= 0x20000;
463 prot_addr
= 0x100000;
466 /* highest address for loading the initrd */
467 if (protocol
>= 0x20c &&
468 lduw_p(header
+ 0x236) & XLF_CAN_BE_LOADED_ABOVE_4G
) {
470 * Linux has supported initrd up to 4 GB for a very long time (2007,
471 * long before XLF_CAN_BE_LOADED_ABOVE_4G which was added in 2013),
472 * though it only sets initrd_max to 2 GB to "work around bootloader
473 * bugs". Luckily, QEMU firmware(which does something like bootloader)
474 * has supported this.
476 * It's believed that if XLF_CAN_BE_LOADED_ABOVE_4G is set, initrd can
477 * be loaded into any address.
479 * In addition, initrd_max is uint32_t simply because QEMU doesn't
480 * support the 64-bit boot protocol (specifically the ext_ramdisk_image
483 * Therefore here just limit initrd_max to UINT32_MAX simply as well.
485 initrd_max
= UINT32_MAX
;
486 } else if (protocol
>= 0x203) {
487 initrd_max
= ldl_p(header
+ 0x22c);
489 initrd_max
= 0x37ffffff;
492 if (initrd_max
>= x86ms
->below_4g_mem_size
- acpi_data_size
) {
493 initrd_max
= x86ms
->below_4g_mem_size
- acpi_data_size
- 1;
496 fw_cfg_add_i32(fw_cfg
, FW_CFG_CMDLINE_ADDR
, cmdline_addr
);
497 fw_cfg_add_i32(fw_cfg
, FW_CFG_CMDLINE_SIZE
, strlen(kernel_cmdline
) + 1);
498 fw_cfg_add_string(fw_cfg
, FW_CFG_CMDLINE_DATA
, kernel_cmdline
);
500 if (protocol
>= 0x202) {
501 stl_p(header
+ 0x228, cmdline_addr
);
503 stw_p(header
+ 0x20, 0xA33F);
504 stw_p(header
+ 0x22, cmdline_addr
- real_addr
);
507 /* handle vga= parameter */
508 vmode
= strstr(kernel_cmdline
, "vga=");
510 unsigned int video_mode
;
514 if (!strncmp(vmode
, "normal", 6)) {
516 } else if (!strncmp(vmode
, "ext", 3)) {
518 } else if (!strncmp(vmode
, "ask", 3)) {
521 ret
= qemu_strtoui(vmode
, NULL
, 0, &video_mode
);
523 fprintf(stderr
, "qemu: can't parse 'vga' parameter: %s\n",
528 stw_p(header
+ 0x1fa, video_mode
);
533 * High nybble = B reserved for QEMU; low nybble is revision number.
534 * If this code is substantially changed, you may want to consider
535 * incrementing the revision.
537 if (protocol
>= 0x200) {
538 header
[0x210] = 0xB0;
541 if (protocol
>= 0x201) {
542 header
[0x211] |= 0x80; /* CAN_USE_HEAP */
543 stw_p(header
+ 0x224, cmdline_addr
- real_addr
- 0x200);
547 if (initrd_filename
) {
548 GMappedFile
*mapped_file
;
553 if (protocol
< 0x200) {
554 fprintf(stderr
, "qemu: linux kernel too old to load a ram disk\n");
558 mapped_file
= g_mapped_file_new(initrd_filename
, false, &gerr
);
560 fprintf(stderr
, "qemu: error reading initrd %s: %s\n",
561 initrd_filename
, gerr
->message
);
564 x86ms
->initrd_mapped_file
= mapped_file
;
566 initrd_data
= g_mapped_file_get_contents(mapped_file
);
567 initrd_size
= g_mapped_file_get_length(mapped_file
);
568 if (initrd_size
>= initrd_max
) {
569 fprintf(stderr
, "qemu: initrd is too large, cannot support."
570 "(max: %"PRIu32
", need %"PRId64
")\n",
571 initrd_max
, (uint64_t)initrd_size
);
575 initrd_addr
= (initrd_max
- initrd_size
) & ~4095;
577 fw_cfg_add_i32(fw_cfg
, FW_CFG_INITRD_ADDR
, initrd_addr
);
578 fw_cfg_add_i32(fw_cfg
, FW_CFG_INITRD_SIZE
, initrd_size
);
579 fw_cfg_add_bytes(fw_cfg
, FW_CFG_INITRD_DATA
, initrd_data
, initrd_size
);
581 stl_p(header
+ 0x218, initrd_addr
);
582 stl_p(header
+ 0x21c, initrd_size
);
585 /* load kernel and setup */
586 setup_size
= header
[0x1f1];
587 if (setup_size
== 0) {
590 setup_size
= (setup_size
+ 1) * 512;
591 if (setup_size
> kernel_size
) {
592 fprintf(stderr
, "qemu: invalid kernel header\n");
595 kernel_size
-= setup_size
;
597 setup
= g_malloc(setup_size
);
598 kernel
= g_malloc(kernel_size
);
599 fseek(f
, 0, SEEK_SET
);
600 if (fread(setup
, 1, setup_size
, f
) != setup_size
) {
601 fprintf(stderr
, "fread() failed\n");
604 if (fread(kernel
, 1, kernel_size
, f
) != kernel_size
) {
605 fprintf(stderr
, "fread() failed\n");
610 /* append dtb to kernel */
612 if (protocol
< 0x209) {
613 fprintf(stderr
, "qemu: Linux kernel too old to load a dtb\n");
617 dtb_size
= get_image_size(dtb_filename
);
619 fprintf(stderr
, "qemu: error reading dtb %s: %s\n",
620 dtb_filename
, strerror(errno
));
624 setup_data_offset
= QEMU_ALIGN_UP(kernel_size
, 16);
625 kernel_size
= setup_data_offset
+ sizeof(struct setup_data
) + dtb_size
;
626 kernel
= g_realloc(kernel
, kernel_size
);
628 stq_p(header
+ 0x250, prot_addr
+ setup_data_offset
);
630 setup_data
= (struct setup_data
*)(kernel
+ setup_data_offset
);
631 setup_data
->next
= 0;
632 setup_data
->type
= cpu_to_le32(SETUP_DTB
);
633 setup_data
->len
= cpu_to_le32(dtb_size
);
635 load_image_size(dtb_filename
, setup_data
->data
, dtb_size
);
638 memcpy(setup
, header
, MIN(sizeof(header
), setup_size
));
640 fw_cfg_add_i32(fw_cfg
, FW_CFG_KERNEL_ADDR
, prot_addr
);
641 fw_cfg_add_i32(fw_cfg
, FW_CFG_KERNEL_SIZE
, kernel_size
);
642 fw_cfg_add_bytes(fw_cfg
, FW_CFG_KERNEL_DATA
, kernel
, kernel_size
);
644 fw_cfg_add_i32(fw_cfg
, FW_CFG_SETUP_ADDR
, real_addr
);
645 fw_cfg_add_i32(fw_cfg
, FW_CFG_SETUP_SIZE
, setup_size
);
646 fw_cfg_add_bytes(fw_cfg
, FW_CFG_SETUP_DATA
, setup
, setup_size
);
648 option_rom
[nb_option_roms
].bootindex
= 0;
649 option_rom
[nb_option_roms
].name
= "linuxboot.bin";
650 if (linuxboot_dma_enabled
&& fw_cfg_dma_enabled(fw_cfg
)) {
651 option_rom
[nb_option_roms
].name
= "linuxboot_dma.bin";
656 void x86_bios_rom_init(MemoryRegion
*rom_memory
, bool isapc_ram_fw
)
659 MemoryRegion
*bios
, *isa_bios
;
660 int bios_size
, isa_bios_size
;
664 if (bios_name
== NULL
) {
665 bios_name
= BIOS_FILENAME
;
667 filename
= qemu_find_file(QEMU_FILE_TYPE_BIOS
, bios_name
);
669 bios_size
= get_image_size(filename
);
673 if (bios_size
<= 0 ||
674 (bios_size
% 65536) != 0) {
677 bios
= g_malloc(sizeof(*bios
));
678 memory_region_init_ram(bios
, NULL
, "pc.bios", bios_size
, &error_fatal
);
680 memory_region_set_readonly(bios
, true);
682 ret
= rom_add_file_fixed(bios_name
, (uint32_t)(-bios_size
), -1);
685 fprintf(stderr
, "qemu: could not load PC BIOS '%s'\n", bios_name
);
690 /* map the last 128KB of the BIOS in ISA space */
691 isa_bios_size
= MIN(bios_size
, 128 * KiB
);
692 isa_bios
= g_malloc(sizeof(*isa_bios
));
693 memory_region_init_alias(isa_bios
, NULL
, "isa-bios", bios
,
694 bios_size
- isa_bios_size
, isa_bios_size
);
695 memory_region_add_subregion_overlap(rom_memory
,
696 0x100000 - isa_bios_size
,
700 memory_region_set_readonly(isa_bios
, true);
703 /* map all the bios at the top of memory */
704 memory_region_add_subregion(rom_memory
,
705 (uint32_t)(-bios_size
),
709 static void x86_machine_get_max_ram_below_4g(Object
*obj
, Visitor
*v
,
710 const char *name
, void *opaque
,
713 X86MachineState
*x86ms
= X86_MACHINE(obj
);
714 uint64_t value
= x86ms
->max_ram_below_4g
;
716 visit_type_size(v
, name
, &value
, errp
);
719 static void x86_machine_set_max_ram_below_4g(Object
*obj
, Visitor
*v
,
720 const char *name
, void *opaque
,
723 X86MachineState
*x86ms
= X86_MACHINE(obj
);
727 visit_type_size(v
, name
, &value
, &error
);
729 error_propagate(errp
, error
);
732 if (value
> 4 * GiB
) {
734 "Machine option 'max-ram-below-4g=%"PRIu64
735 "' expects size less than or equal to 4G", value
);
736 error_propagate(errp
, error
);
740 if (value
< 1 * MiB
) {
741 warn_report("Only %" PRIu64
" bytes of RAM below the 4GiB boundary,"
742 "BIOS may not work with less than 1MiB", value
);
745 x86ms
->max_ram_below_4g
= value
;
748 static void x86_machine_initfn(Object
*obj
)
750 X86MachineState
*x86ms
= X86_MACHINE(obj
);
752 x86ms
->max_ram_below_4g
= 0; /* use default */
756 static void x86_machine_class_init(ObjectClass
*oc
, void *data
)
758 MachineClass
*mc
= MACHINE_CLASS(oc
);
759 X86MachineClass
*x86mc
= X86_MACHINE_CLASS(oc
);
760 NMIClass
*nc
= NMI_CLASS(oc
);
762 mc
->cpu_index_to_instance_props
= x86_cpu_index_to_props
;
763 mc
->get_default_cpu_node_id
= x86_get_default_cpu_node_id
;
764 mc
->possible_cpu_arch_ids
= x86_possible_cpu_arch_ids
;
765 x86mc
->compat_apic_id_mode
= false;
766 nc
->nmi_monitor_handler
= x86_nmi
;
768 object_class_property_add(oc
, X86_MACHINE_MAX_RAM_BELOW_4G
, "size",
769 x86_machine_get_max_ram_below_4g
, x86_machine_set_max_ram_below_4g
,
770 NULL
, NULL
, &error_abort
);
772 object_class_property_set_description(oc
, X86_MACHINE_MAX_RAM_BELOW_4G
,
773 "Maximum ram below the 4G boundary (32bit boundary)", &error_abort
);
776 static const TypeInfo x86_machine_info
= {
777 .name
= TYPE_X86_MACHINE
,
778 .parent
= TYPE_MACHINE
,
780 .instance_size
= sizeof(X86MachineState
),
781 .instance_init
= x86_machine_initfn
,
782 .class_size
= sizeof(X86MachineClass
),
783 .class_init
= x86_machine_class_init
,
784 .interfaces
= (InterfaceInfo
[]) {
790 static void x86_machine_register_types(void)
792 type_register_static(&x86_machine_info
);
795 type_init(x86_machine_register_types
)