hw/riscv: Move sifive_plic model to hw/intc
[qemu/ar7.git] / hw / i386 / x86.c
blobc1954db152eb191f66efcb08ec8684a2141735a1
1 /*
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
21 * THE SOFTWARE.
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"
37 #include "trace.h"
39 #include "hw/i386/x86.h"
40 #include "target/i386/cpu.h"
41 #include "hw/i386/topology.h"
42 #include "hw/i386/fw_cfg.h"
43 #include "hw/intc/i8259.h"
45 #include "hw/acpi/cpu_hotplug.h"
46 #include "hw/irq.h"
47 #include "hw/nmi.h"
48 #include "hw/loader.h"
49 #include "multiboot.h"
50 #include "elf.h"
51 #include "standard-headers/asm-x86/bootparam.h"
52 #include CONFIG_DEVICES
53 #include "kvm_i386.h"
55 #define BIOS_FILENAME "bios.bin"
57 /* Physical Address of PVH entry point read from kernel ELF NOTE */
58 static size_t pvh_start_addr;
60 inline void init_topo_info(X86CPUTopoInfo *topo_info,
61 const X86MachineState *x86ms)
63 MachineState *ms = MACHINE(x86ms);
65 topo_info->dies_per_pkg = x86ms->smp_dies;
66 topo_info->cores_per_die = ms->smp.cores;
67 topo_info->threads_per_core = ms->smp.threads;
71 * Calculates initial APIC ID for a specific CPU index
73 * Currently we need to be able to calculate the APIC ID from the CPU index
74 * alone (without requiring a CPU object), as the QEMU<->Seabios interfaces have
75 * no concept of "CPU index", and the NUMA tables on fw_cfg need the APIC ID of
76 * all CPUs up to max_cpus.
78 uint32_t x86_cpu_apic_id_from_index(X86MachineState *x86ms,
79 unsigned int cpu_index)
81 X86MachineClass *x86mc = X86_MACHINE_GET_CLASS(x86ms);
82 X86CPUTopoInfo topo_info;
83 uint32_t correct_id;
84 static bool warned;
86 init_topo_info(&topo_info, x86ms);
88 correct_id = x86_apicid_from_cpu_idx(&topo_info, cpu_index);
89 if (x86mc->compat_apic_id_mode) {
90 if (cpu_index != correct_id && !warned && !qtest_enabled()) {
91 error_report("APIC IDs set in compatibility mode, "
92 "CPU topology won't match the configuration");
93 warned = true;
95 return cpu_index;
96 } else {
97 return correct_id;
102 void x86_cpu_new(X86MachineState *x86ms, int64_t apic_id, Error **errp)
104 Object *cpu = object_new(MACHINE(x86ms)->cpu_type);
106 if (!object_property_set_uint(cpu, "apic-id", apic_id, errp)) {
107 goto out;
109 qdev_realize(DEVICE(cpu), NULL, errp);
111 out:
112 object_unref(cpu);
115 void x86_cpus_init(X86MachineState *x86ms, int default_cpu_version)
117 int i;
118 const CPUArchIdList *possible_cpus;
119 MachineState *ms = MACHINE(x86ms);
120 MachineClass *mc = MACHINE_GET_CLASS(x86ms);
122 x86_cpu_set_default_version(default_cpu_version);
125 * Calculates the limit to CPU APIC ID values
127 * Limit for the APIC ID value, so that all
128 * CPU APIC IDs are < x86ms->apic_id_limit.
130 * This is used for FW_CFG_MAX_CPUS. See comments on fw_cfg_arch_create().
132 x86ms->apic_id_limit = x86_cpu_apic_id_from_index(x86ms,
133 ms->smp.max_cpus - 1) + 1;
134 possible_cpus = mc->possible_cpu_arch_ids(ms);
135 for (i = 0; i < ms->smp.cpus; i++) {
136 x86_cpu_new(x86ms, possible_cpus->cpus[i].arch_id, &error_fatal);
140 CpuInstanceProperties
141 x86_cpu_index_to_props(MachineState *ms, unsigned cpu_index)
143 MachineClass *mc = MACHINE_GET_CLASS(ms);
144 const CPUArchIdList *possible_cpus = mc->possible_cpu_arch_ids(ms);
146 assert(cpu_index < possible_cpus->len);
147 return possible_cpus->cpus[cpu_index].props;
150 int64_t x86_get_default_cpu_node_id(const MachineState *ms, int idx)
152 X86CPUTopoIDs topo_ids;
153 X86MachineState *x86ms = X86_MACHINE(ms);
154 X86CPUTopoInfo topo_info;
156 init_topo_info(&topo_info, x86ms);
158 assert(idx < ms->possible_cpus->len);
159 x86_topo_ids_from_apicid(ms->possible_cpus->cpus[idx].arch_id,
160 &topo_info, &topo_ids);
161 return topo_ids.pkg_id % ms->numa_state->num_nodes;
164 const CPUArchIdList *x86_possible_cpu_arch_ids(MachineState *ms)
166 X86MachineState *x86ms = X86_MACHINE(ms);
167 unsigned int max_cpus = ms->smp.max_cpus;
168 X86CPUTopoInfo topo_info;
169 int i;
171 if (ms->possible_cpus) {
173 * make sure that max_cpus hasn't changed since the first use, i.e.
174 * -smp hasn't been parsed after it
176 assert(ms->possible_cpus->len == max_cpus);
177 return ms->possible_cpus;
180 ms->possible_cpus = g_malloc0(sizeof(CPUArchIdList) +
181 sizeof(CPUArchId) * max_cpus);
182 ms->possible_cpus->len = max_cpus;
184 init_topo_info(&topo_info, x86ms);
186 for (i = 0; i < ms->possible_cpus->len; i++) {
187 X86CPUTopoIDs topo_ids;
189 ms->possible_cpus->cpus[i].type = ms->cpu_type;
190 ms->possible_cpus->cpus[i].vcpus_count = 1;
191 ms->possible_cpus->cpus[i].arch_id =
192 x86_cpu_apic_id_from_index(x86ms, i);
193 x86_topo_ids_from_apicid(ms->possible_cpus->cpus[i].arch_id,
194 &topo_info, &topo_ids);
195 ms->possible_cpus->cpus[i].props.has_socket_id = true;
196 ms->possible_cpus->cpus[i].props.socket_id = topo_ids.pkg_id;
197 if (x86ms->smp_dies > 1) {
198 ms->possible_cpus->cpus[i].props.has_die_id = true;
199 ms->possible_cpus->cpus[i].props.die_id = topo_ids.die_id;
201 ms->possible_cpus->cpus[i].props.has_core_id = true;
202 ms->possible_cpus->cpus[i].props.core_id = topo_ids.core_id;
203 ms->possible_cpus->cpus[i].props.has_thread_id = true;
204 ms->possible_cpus->cpus[i].props.thread_id = topo_ids.smt_id;
206 return ms->possible_cpus;
209 static void x86_nmi(NMIState *n, int cpu_index, Error **errp)
211 /* cpu index isn't used */
212 CPUState *cs;
214 CPU_FOREACH(cs) {
215 X86CPU *cpu = X86_CPU(cs);
217 if (!cpu->apic_state) {
218 cpu_interrupt(cs, CPU_INTERRUPT_NMI);
219 } else {
220 apic_deliver_nmi(cpu->apic_state);
225 static long get_file_size(FILE *f)
227 long where, size;
229 /* XXX: on Unix systems, using fstat() probably makes more sense */
231 where = ftell(f);
232 fseek(f, 0, SEEK_END);
233 size = ftell(f);
234 fseek(f, where, SEEK_SET);
236 return size;
239 /* TSC handling */
240 uint64_t cpu_get_tsc(CPUX86State *env)
242 return cpu_get_ticks();
245 /* IRQ handling */
246 static void pic_irq_request(void *opaque, int irq, int level)
248 CPUState *cs = first_cpu;
249 X86CPU *cpu = X86_CPU(cs);
251 trace_x86_pic_interrupt(irq, level);
252 if (cpu->apic_state && !kvm_irqchip_in_kernel()) {
253 CPU_FOREACH(cs) {
254 cpu = X86_CPU(cs);
255 if (apic_accept_pic_intr(cpu->apic_state)) {
256 apic_deliver_pic_intr(cpu->apic_state, level);
259 } else {
260 if (level) {
261 cpu_interrupt(cs, CPU_INTERRUPT_HARD);
262 } else {
263 cpu_reset_interrupt(cs, CPU_INTERRUPT_HARD);
268 qemu_irq x86_allocate_cpu_irq(void)
270 return qemu_allocate_irq(pic_irq_request, NULL, 0);
273 int cpu_get_pic_interrupt(CPUX86State *env)
275 X86CPU *cpu = env_archcpu(env);
276 int intno;
278 if (!kvm_irqchip_in_kernel()) {
279 intno = apic_get_interrupt(cpu->apic_state);
280 if (intno >= 0) {
281 return intno;
283 /* read the irq from the PIC */
284 if (!apic_accept_pic_intr(cpu->apic_state)) {
285 return -1;
289 intno = pic_read_irq(isa_pic);
290 return intno;
293 DeviceState *cpu_get_current_apic(void)
295 if (current_cpu) {
296 X86CPU *cpu = X86_CPU(current_cpu);
297 return cpu->apic_state;
298 } else {
299 return NULL;
303 void gsi_handler(void *opaque, int n, int level)
305 GSIState *s = opaque;
307 trace_x86_gsi_interrupt(n, level);
308 if (n < ISA_NUM_IRQS) {
309 /* Under KVM, Kernel will forward to both PIC and IOAPIC */
310 qemu_set_irq(s->i8259_irq[n], level);
312 qemu_set_irq(s->ioapic_irq[n], level);
315 void ioapic_init_gsi(GSIState *gsi_state, const char *parent_name)
317 DeviceState *dev;
318 SysBusDevice *d;
319 unsigned int i;
321 assert(parent_name);
322 if (kvm_ioapic_in_kernel()) {
323 dev = qdev_new(TYPE_KVM_IOAPIC);
324 } else {
325 dev = qdev_new(TYPE_IOAPIC);
327 object_property_add_child(object_resolve_path(parent_name, NULL),
328 "ioapic", OBJECT(dev));
329 d = SYS_BUS_DEVICE(dev);
330 sysbus_realize_and_unref(d, &error_fatal);
331 sysbus_mmio_map(d, 0, IO_APIC_DEFAULT_ADDRESS);
333 for (i = 0; i < IOAPIC_NUM_PINS; i++) {
334 gsi_state->ioapic_irq[i] = qdev_get_gpio_in(dev, i);
338 struct setup_data {
339 uint64_t next;
340 uint32_t type;
341 uint32_t len;
342 uint8_t data[];
343 } __attribute__((packed));
347 * The entry point into the kernel for PVH boot is different from
348 * the native entry point. The PVH entry is defined by the x86/HVM
349 * direct boot ABI and is available in an ELFNOTE in the kernel binary.
351 * This function is passed to load_elf() when it is called from
352 * load_elfboot() which then additionally checks for an ELF Note of
353 * type XEN_ELFNOTE_PHYS32_ENTRY and passes it to this function to
354 * parse the PVH entry address from the ELF Note.
356 * Due to trickery in elf_opts.h, load_elf() is actually available as
357 * load_elf32() or load_elf64() and this routine needs to be able
358 * to deal with being called as 32 or 64 bit.
360 * The address of the PVH entry point is saved to the 'pvh_start_addr'
361 * global variable. (although the entry point is 32-bit, the kernel
362 * binary can be either 32-bit or 64-bit).
364 static uint64_t read_pvh_start_addr(void *arg1, void *arg2, bool is64)
366 size_t *elf_note_data_addr;
368 /* Check if ELF Note header passed in is valid */
369 if (arg1 == NULL) {
370 return 0;
373 if (is64) {
374 struct elf64_note *nhdr64 = (struct elf64_note *)arg1;
375 uint64_t nhdr_size64 = sizeof(struct elf64_note);
376 uint64_t phdr_align = *(uint64_t *)arg2;
377 uint64_t nhdr_namesz = nhdr64->n_namesz;
379 elf_note_data_addr =
380 ((void *)nhdr64) + nhdr_size64 +
381 QEMU_ALIGN_UP(nhdr_namesz, phdr_align);
382 } else {
383 struct elf32_note *nhdr32 = (struct elf32_note *)arg1;
384 uint32_t nhdr_size32 = sizeof(struct elf32_note);
385 uint32_t phdr_align = *(uint32_t *)arg2;
386 uint32_t nhdr_namesz = nhdr32->n_namesz;
388 elf_note_data_addr =
389 ((void *)nhdr32) + nhdr_size32 +
390 QEMU_ALIGN_UP(nhdr_namesz, phdr_align);
393 pvh_start_addr = *elf_note_data_addr;
395 return pvh_start_addr;
398 static bool load_elfboot(const char *kernel_filename,
399 int kernel_file_size,
400 uint8_t *header,
401 size_t pvh_xen_start_addr,
402 FWCfgState *fw_cfg)
404 uint32_t flags = 0;
405 uint32_t mh_load_addr = 0;
406 uint32_t elf_kernel_size = 0;
407 uint64_t elf_entry;
408 uint64_t elf_low, elf_high;
409 int kernel_size;
411 if (ldl_p(header) != 0x464c457f) {
412 return false; /* no elfboot */
415 bool elf_is64 = header[EI_CLASS] == ELFCLASS64;
416 flags = elf_is64 ?
417 ((Elf64_Ehdr *)header)->e_flags : ((Elf32_Ehdr *)header)->e_flags;
419 if (flags & 0x00010004) { /* LOAD_ELF_HEADER_HAS_ADDR */
420 error_report("elfboot unsupported flags = %x", flags);
421 exit(1);
424 uint64_t elf_note_type = XEN_ELFNOTE_PHYS32_ENTRY;
425 kernel_size = load_elf(kernel_filename, read_pvh_start_addr,
426 NULL, &elf_note_type, &elf_entry,
427 &elf_low, &elf_high, NULL, 0, I386_ELF_MACHINE,
428 0, 0);
430 if (kernel_size < 0) {
431 error_report("Error while loading elf kernel");
432 exit(1);
434 mh_load_addr = elf_low;
435 elf_kernel_size = elf_high - elf_low;
437 if (pvh_start_addr == 0) {
438 error_report("Error loading uncompressed kernel without PVH ELF Note");
439 exit(1);
441 fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_ENTRY, pvh_start_addr);
442 fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_ADDR, mh_load_addr);
443 fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_SIZE, elf_kernel_size);
445 return true;
448 void x86_load_linux(X86MachineState *x86ms,
449 FWCfgState *fw_cfg,
450 int acpi_data_size,
451 bool pvh_enabled,
452 bool linuxboot_dma_enabled)
454 uint16_t protocol;
455 int setup_size, kernel_size, cmdline_size;
456 int dtb_size, setup_data_offset;
457 uint32_t initrd_max;
458 uint8_t header[8192], *setup, *kernel;
459 hwaddr real_addr, prot_addr, cmdline_addr, initrd_addr = 0;
460 FILE *f;
461 char *vmode;
462 MachineState *machine = MACHINE(x86ms);
463 struct setup_data *setup_data;
464 const char *kernel_filename = machine->kernel_filename;
465 const char *initrd_filename = machine->initrd_filename;
466 const char *dtb_filename = machine->dtb;
467 const char *kernel_cmdline = machine->kernel_cmdline;
469 /* Align to 16 bytes as a paranoia measure */
470 cmdline_size = (strlen(kernel_cmdline) + 16) & ~15;
472 /* load the kernel header */
473 f = fopen(kernel_filename, "rb");
474 if (!f) {
475 fprintf(stderr, "qemu: could not open kernel file '%s': %s\n",
476 kernel_filename, strerror(errno));
477 exit(1);
480 kernel_size = get_file_size(f);
481 if (!kernel_size ||
482 fread(header, 1, MIN(ARRAY_SIZE(header), kernel_size), f) !=
483 MIN(ARRAY_SIZE(header), kernel_size)) {
484 fprintf(stderr, "qemu: could not load kernel '%s': %s\n",
485 kernel_filename, strerror(errno));
486 exit(1);
489 /* kernel protocol version */
490 if (ldl_p(header + 0x202) == 0x53726448) {
491 protocol = lduw_p(header + 0x206);
492 } else {
494 * This could be a multiboot kernel. If it is, let's stop treating it
495 * like a Linux kernel.
496 * Note: some multiboot images could be in the ELF format (the same of
497 * PVH), so we try multiboot first since we check the multiboot magic
498 * header before to load it.
500 if (load_multiboot(fw_cfg, f, kernel_filename, initrd_filename,
501 kernel_cmdline, kernel_size, header)) {
502 return;
505 * Check if the file is an uncompressed kernel file (ELF) and load it,
506 * saving the PVH entry point used by the x86/HVM direct boot ABI.
507 * If load_elfboot() is successful, populate the fw_cfg info.
509 if (pvh_enabled &&
510 load_elfboot(kernel_filename, kernel_size,
511 header, pvh_start_addr, fw_cfg)) {
512 fclose(f);
514 fw_cfg_add_i32(fw_cfg, FW_CFG_CMDLINE_SIZE,
515 strlen(kernel_cmdline) + 1);
516 fw_cfg_add_string(fw_cfg, FW_CFG_CMDLINE_DATA, kernel_cmdline);
518 fw_cfg_add_i32(fw_cfg, FW_CFG_SETUP_SIZE, sizeof(header));
519 fw_cfg_add_bytes(fw_cfg, FW_CFG_SETUP_DATA,
520 header, sizeof(header));
522 /* load initrd */
523 if (initrd_filename) {
524 GMappedFile *mapped_file;
525 gsize initrd_size;
526 gchar *initrd_data;
527 GError *gerr = NULL;
529 mapped_file = g_mapped_file_new(initrd_filename, false, &gerr);
530 if (!mapped_file) {
531 fprintf(stderr, "qemu: error reading initrd %s: %s\n",
532 initrd_filename, gerr->message);
533 exit(1);
535 x86ms->initrd_mapped_file = mapped_file;
537 initrd_data = g_mapped_file_get_contents(mapped_file);
538 initrd_size = g_mapped_file_get_length(mapped_file);
539 initrd_max = x86ms->below_4g_mem_size - acpi_data_size - 1;
540 if (initrd_size >= initrd_max) {
541 fprintf(stderr, "qemu: initrd is too large, cannot support."
542 "(max: %"PRIu32", need %"PRId64")\n",
543 initrd_max, (uint64_t)initrd_size);
544 exit(1);
547 initrd_addr = (initrd_max - initrd_size) & ~4095;
549 fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_ADDR, initrd_addr);
550 fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_SIZE, initrd_size);
551 fw_cfg_add_bytes(fw_cfg, FW_CFG_INITRD_DATA, initrd_data,
552 initrd_size);
555 option_rom[nb_option_roms].bootindex = 0;
556 option_rom[nb_option_roms].name = "pvh.bin";
557 nb_option_roms++;
559 return;
561 protocol = 0;
564 if (protocol < 0x200 || !(header[0x211] & 0x01)) {
565 /* Low kernel */
566 real_addr = 0x90000;
567 cmdline_addr = 0x9a000 - cmdline_size;
568 prot_addr = 0x10000;
569 } else if (protocol < 0x202) {
570 /* High but ancient kernel */
571 real_addr = 0x90000;
572 cmdline_addr = 0x9a000 - cmdline_size;
573 prot_addr = 0x100000;
574 } else {
575 /* High and recent kernel */
576 real_addr = 0x10000;
577 cmdline_addr = 0x20000;
578 prot_addr = 0x100000;
581 /* highest address for loading the initrd */
582 if (protocol >= 0x20c &&
583 lduw_p(header + 0x236) & XLF_CAN_BE_LOADED_ABOVE_4G) {
585 * Linux has supported initrd up to 4 GB for a very long time (2007,
586 * long before XLF_CAN_BE_LOADED_ABOVE_4G which was added in 2013),
587 * though it only sets initrd_max to 2 GB to "work around bootloader
588 * bugs". Luckily, QEMU firmware(which does something like bootloader)
589 * has supported this.
591 * It's believed that if XLF_CAN_BE_LOADED_ABOVE_4G is set, initrd can
592 * be loaded into any address.
594 * In addition, initrd_max is uint32_t simply because QEMU doesn't
595 * support the 64-bit boot protocol (specifically the ext_ramdisk_image
596 * field).
598 * Therefore here just limit initrd_max to UINT32_MAX simply as well.
600 initrd_max = UINT32_MAX;
601 } else if (protocol >= 0x203) {
602 initrd_max = ldl_p(header + 0x22c);
603 } else {
604 initrd_max = 0x37ffffff;
607 if (initrd_max >= x86ms->below_4g_mem_size - acpi_data_size) {
608 initrd_max = x86ms->below_4g_mem_size - acpi_data_size - 1;
611 fw_cfg_add_i32(fw_cfg, FW_CFG_CMDLINE_ADDR, cmdline_addr);
612 fw_cfg_add_i32(fw_cfg, FW_CFG_CMDLINE_SIZE, strlen(kernel_cmdline) + 1);
613 fw_cfg_add_string(fw_cfg, FW_CFG_CMDLINE_DATA, kernel_cmdline);
615 if (protocol >= 0x202) {
616 stl_p(header + 0x228, cmdline_addr);
617 } else {
618 stw_p(header + 0x20, 0xA33F);
619 stw_p(header + 0x22, cmdline_addr - real_addr);
622 /* handle vga= parameter */
623 vmode = strstr(kernel_cmdline, "vga=");
624 if (vmode) {
625 unsigned int video_mode;
626 const char *end;
627 int ret;
628 /* skip "vga=" */
629 vmode += 4;
630 if (!strncmp(vmode, "normal", 6)) {
631 video_mode = 0xffff;
632 } else if (!strncmp(vmode, "ext", 3)) {
633 video_mode = 0xfffe;
634 } else if (!strncmp(vmode, "ask", 3)) {
635 video_mode = 0xfffd;
636 } else {
637 ret = qemu_strtoui(vmode, &end, 0, &video_mode);
638 if (ret != 0 || (*end && *end != ' ')) {
639 fprintf(stderr, "qemu: invalid 'vga=' kernel parameter.\n");
640 exit(1);
643 stw_p(header + 0x1fa, video_mode);
646 /* loader type */
648 * High nybble = B reserved for QEMU; low nybble is revision number.
649 * If this code is substantially changed, you may want to consider
650 * incrementing the revision.
652 if (protocol >= 0x200) {
653 header[0x210] = 0xB0;
655 /* heap */
656 if (protocol >= 0x201) {
657 header[0x211] |= 0x80; /* CAN_USE_HEAP */
658 stw_p(header + 0x224, cmdline_addr - real_addr - 0x200);
661 /* load initrd */
662 if (initrd_filename) {
663 GMappedFile *mapped_file;
664 gsize initrd_size;
665 gchar *initrd_data;
666 GError *gerr = NULL;
668 if (protocol < 0x200) {
669 fprintf(stderr, "qemu: linux kernel too old to load a ram disk\n");
670 exit(1);
673 mapped_file = g_mapped_file_new(initrd_filename, false, &gerr);
674 if (!mapped_file) {
675 fprintf(stderr, "qemu: error reading initrd %s: %s\n",
676 initrd_filename, gerr->message);
677 exit(1);
679 x86ms->initrd_mapped_file = mapped_file;
681 initrd_data = g_mapped_file_get_contents(mapped_file);
682 initrd_size = g_mapped_file_get_length(mapped_file);
683 if (initrd_size >= initrd_max) {
684 fprintf(stderr, "qemu: initrd is too large, cannot support."
685 "(max: %"PRIu32", need %"PRId64")\n",
686 initrd_max, (uint64_t)initrd_size);
687 exit(1);
690 initrd_addr = (initrd_max - initrd_size) & ~4095;
692 fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_ADDR, initrd_addr);
693 fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_SIZE, initrd_size);
694 fw_cfg_add_bytes(fw_cfg, FW_CFG_INITRD_DATA, initrd_data, initrd_size);
696 stl_p(header + 0x218, initrd_addr);
697 stl_p(header + 0x21c, initrd_size);
700 /* load kernel and setup */
701 setup_size = header[0x1f1];
702 if (setup_size == 0) {
703 setup_size = 4;
705 setup_size = (setup_size + 1) * 512;
706 if (setup_size > kernel_size) {
707 fprintf(stderr, "qemu: invalid kernel header\n");
708 exit(1);
710 kernel_size -= setup_size;
712 setup = g_malloc(setup_size);
713 kernel = g_malloc(kernel_size);
714 fseek(f, 0, SEEK_SET);
715 if (fread(setup, 1, setup_size, f) != setup_size) {
716 fprintf(stderr, "fread() failed\n");
717 exit(1);
719 if (fread(kernel, 1, kernel_size, f) != kernel_size) {
720 fprintf(stderr, "fread() failed\n");
721 exit(1);
723 fclose(f);
725 /* append dtb to kernel */
726 if (dtb_filename) {
727 if (protocol < 0x209) {
728 fprintf(stderr, "qemu: Linux kernel too old to load a dtb\n");
729 exit(1);
732 dtb_size = get_image_size(dtb_filename);
733 if (dtb_size <= 0) {
734 fprintf(stderr, "qemu: error reading dtb %s: %s\n",
735 dtb_filename, strerror(errno));
736 exit(1);
739 setup_data_offset = QEMU_ALIGN_UP(kernel_size, 16);
740 kernel_size = setup_data_offset + sizeof(struct setup_data) + dtb_size;
741 kernel = g_realloc(kernel, kernel_size);
743 stq_p(header + 0x250, prot_addr + setup_data_offset);
745 setup_data = (struct setup_data *)(kernel + setup_data_offset);
746 setup_data->next = 0;
747 setup_data->type = cpu_to_le32(SETUP_DTB);
748 setup_data->len = cpu_to_le32(dtb_size);
750 load_image_size(dtb_filename, setup_data->data, dtb_size);
753 memcpy(setup, header, MIN(sizeof(header), setup_size));
755 fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_ADDR, prot_addr);
756 fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_SIZE, kernel_size);
757 fw_cfg_add_bytes(fw_cfg, FW_CFG_KERNEL_DATA, kernel, kernel_size);
759 fw_cfg_add_i32(fw_cfg, FW_CFG_SETUP_ADDR, real_addr);
760 fw_cfg_add_i32(fw_cfg, FW_CFG_SETUP_SIZE, setup_size);
761 fw_cfg_add_bytes(fw_cfg, FW_CFG_SETUP_DATA, setup, setup_size);
763 option_rom[nb_option_roms].bootindex = 0;
764 option_rom[nb_option_roms].name = "linuxboot.bin";
765 if (linuxboot_dma_enabled && fw_cfg_dma_enabled(fw_cfg)) {
766 option_rom[nb_option_roms].name = "linuxboot_dma.bin";
768 nb_option_roms++;
771 void x86_bios_rom_init(MemoryRegion *rom_memory, bool isapc_ram_fw)
773 char *filename;
774 MemoryRegion *bios, *isa_bios;
775 int bios_size, isa_bios_size;
776 int ret;
778 /* BIOS load */
779 if (bios_name == NULL) {
780 bios_name = BIOS_FILENAME;
782 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
783 if (filename) {
784 bios_size = get_image_size(filename);
785 } else {
786 bios_size = -1;
788 if (bios_size <= 0 ||
789 (bios_size % 65536) != 0) {
790 goto bios_error;
792 bios = g_malloc(sizeof(*bios));
793 memory_region_init_ram(bios, NULL, "pc.bios", bios_size, &error_fatal);
794 if (!isapc_ram_fw) {
795 memory_region_set_readonly(bios, true);
797 ret = rom_add_file_fixed(bios_name, (uint32_t)(-bios_size), -1);
798 if (ret != 0) {
799 bios_error:
800 fprintf(stderr, "qemu: could not load PC BIOS '%s'\n", bios_name);
801 exit(1);
803 g_free(filename);
805 /* map the last 128KB of the BIOS in ISA space */
806 isa_bios_size = MIN(bios_size, 128 * KiB);
807 isa_bios = g_malloc(sizeof(*isa_bios));
808 memory_region_init_alias(isa_bios, NULL, "isa-bios", bios,
809 bios_size - isa_bios_size, isa_bios_size);
810 memory_region_add_subregion_overlap(rom_memory,
811 0x100000 - isa_bios_size,
812 isa_bios,
814 if (!isapc_ram_fw) {
815 memory_region_set_readonly(isa_bios, true);
818 /* map all the bios at the top of memory */
819 memory_region_add_subregion(rom_memory,
820 (uint32_t)(-bios_size),
821 bios);
824 bool x86_machine_is_smm_enabled(X86MachineState *x86ms)
826 bool smm_available = false;
828 if (x86ms->smm == ON_OFF_AUTO_OFF) {
829 return false;
832 if (tcg_enabled() || qtest_enabled()) {
833 smm_available = true;
834 } else if (kvm_enabled()) {
835 smm_available = kvm_has_smm();
838 if (smm_available) {
839 return true;
842 if (x86ms->smm == ON_OFF_AUTO_ON) {
843 error_report("System Management Mode not supported by this hypervisor.");
844 exit(1);
846 return false;
849 static void x86_machine_get_smm(Object *obj, Visitor *v, const char *name,
850 void *opaque, Error **errp)
852 X86MachineState *x86ms = X86_MACHINE(obj);
853 OnOffAuto smm = x86ms->smm;
855 visit_type_OnOffAuto(v, name, &smm, errp);
858 static void x86_machine_set_smm(Object *obj, Visitor *v, const char *name,
859 void *opaque, Error **errp)
861 X86MachineState *x86ms = X86_MACHINE(obj);
863 visit_type_OnOffAuto(v, name, &x86ms->smm, errp);
866 bool x86_machine_is_acpi_enabled(X86MachineState *x86ms)
868 if (x86ms->acpi == ON_OFF_AUTO_OFF) {
869 return false;
871 return true;
874 static void x86_machine_get_acpi(Object *obj, Visitor *v, const char *name,
875 void *opaque, Error **errp)
877 X86MachineState *x86ms = X86_MACHINE(obj);
878 OnOffAuto acpi = x86ms->acpi;
880 visit_type_OnOffAuto(v, name, &acpi, errp);
883 static void x86_machine_set_acpi(Object *obj, Visitor *v, const char *name,
884 void *opaque, Error **errp)
886 X86MachineState *x86ms = X86_MACHINE(obj);
888 visit_type_OnOffAuto(v, name, &x86ms->acpi, errp);
891 static void x86_machine_initfn(Object *obj)
893 X86MachineState *x86ms = X86_MACHINE(obj);
895 x86ms->smm = ON_OFF_AUTO_AUTO;
896 x86ms->acpi = ON_OFF_AUTO_AUTO;
897 x86ms->smp_dies = 1;
900 static void x86_machine_class_init(ObjectClass *oc, void *data)
902 MachineClass *mc = MACHINE_CLASS(oc);
903 X86MachineClass *x86mc = X86_MACHINE_CLASS(oc);
904 NMIClass *nc = NMI_CLASS(oc);
906 mc->cpu_index_to_instance_props = x86_cpu_index_to_props;
907 mc->get_default_cpu_node_id = x86_get_default_cpu_node_id;
908 mc->possible_cpu_arch_ids = x86_possible_cpu_arch_ids;
909 x86mc->compat_apic_id_mode = false;
910 x86mc->save_tsc_khz = true;
911 nc->nmi_monitor_handler = x86_nmi;
913 object_class_property_add(oc, X86_MACHINE_SMM, "OnOffAuto",
914 x86_machine_get_smm, x86_machine_set_smm,
915 NULL, NULL);
916 object_class_property_set_description(oc, X86_MACHINE_SMM,
917 "Enable SMM");
919 object_class_property_add(oc, X86_MACHINE_ACPI, "OnOffAuto",
920 x86_machine_get_acpi, x86_machine_set_acpi,
921 NULL, NULL);
922 object_class_property_set_description(oc, X86_MACHINE_ACPI,
923 "Enable ACPI");
926 static const TypeInfo x86_machine_info = {
927 .name = TYPE_X86_MACHINE,
928 .parent = TYPE_MACHINE,
929 .abstract = true,
930 .instance_size = sizeof(X86MachineState),
931 .instance_init = x86_machine_initfn,
932 .class_size = sizeof(X86MachineClass),
933 .class_init = x86_machine_class_init,
934 .interfaces = (InterfaceInfo[]) {
935 { TYPE_NMI },
940 static void x86_machine_register_types(void)
942 type_register_static(&x86_machine_info);
945 type_init(x86_machine_register_types)