2 * QEMU PC System Emulator
4 * Copyright (c) 2003-2004 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
28 #include "vmware_vga.h"
35 #include "audio/audio.h"
41 #include "hpet_emul.h"
47 #include "device-assignment.h"
51 /* output Bochs bios info messages */
54 /* Show multiboot debug output */
55 //#define DEBUG_MULTIBOOT
57 #define BIOS_FILENAME "bios.bin"
58 #define EXTBOOT_FILENAME "extboot.bin"
59 #define VAPIC_FILENAME "vapic.bin"
61 #define PC_MAX_BIOS_SIZE (4 * 1024 * 1024)
63 /* Leave a chunk of memory at the top of RAM for the BIOS ACPI tables. */
64 #define ACPI_DATA_SIZE 0x10000
65 #define BIOS_CFG_IOPORT 0x510
66 #define FW_CFG_ACPI_TABLES (FW_CFG_ARCH_LOCAL + 0)
67 #define FW_CFG_SMBIOS_ENTRIES (FW_CFG_ARCH_LOCAL + 1)
68 #define FW_CFG_IRQ0_OVERRIDE (FW_CFG_ARCH_LOCAL + 2)
72 static fdctrl_t
*floppy_controller
;
73 static RTCState
*rtc_state
;
75 static PCII440FXState
*i440fx_state
;
77 qemu_irq
*ioapic_irq_hack
;
79 typedef struct isa_irq_state
{
84 static void isa_irq_handler(void *opaque
, int n
, int level
)
86 IsaIrqState
*isa
= (IsaIrqState
*)opaque
;
89 qemu_set_irq(isa
->i8259
[n
], level
);
92 qemu_set_irq(isa
->ioapic
[n
], level
);
95 static void ioport80_write(void *opaque
, uint32_t addr
, uint32_t data
)
99 /* MSDOS compatibility mode FPU exception support */
100 static qemu_irq ferr_irq
;
101 /* XXX: add IGNNE support */
102 void cpu_set_ferr(CPUX86State
*s
)
104 qemu_irq_raise(ferr_irq
);
107 static void ioportF0_write(void *opaque
, uint32_t addr
, uint32_t data
)
109 qemu_irq_lower(ferr_irq
);
113 uint64_t cpu_get_tsc(CPUX86State
*env
)
115 return cpu_get_ticks();
119 void cpu_smm_update(CPUState
*env
)
121 if (i440fx_state
&& env
== first_cpu
)
122 i440fx_set_smm(i440fx_state
, (env
->hflags
>> HF_SMM_SHIFT
) & 1);
127 int cpu_get_pic_interrupt(CPUState
*env
)
131 intno
= apic_get_interrupt(env
);
133 /* set irq request if a PIC irq is still pending */
134 /* XXX: improve that */
135 pic_update_irq(isa_pic
);
138 /* read the irq from the PIC */
139 if (!apic_accept_pic_intr(env
))
142 intno
= pic_read_irq(isa_pic
);
146 static void pic_irq_request(void *opaque
, int irq
, int level
)
148 CPUState
*env
= first_cpu
;
150 if (env
->apic_state
) {
152 if (apic_accept_pic_intr(env
))
153 apic_deliver_pic_intr(env
, level
);
158 cpu_interrupt(env
, CPU_INTERRUPT_HARD
);
160 cpu_reset_interrupt(env
, CPU_INTERRUPT_HARD
);
164 /* PC cmos mappings */
166 #define REG_EQUIPMENT_BYTE 0x14
168 static int cmos_get_fd_drive_type(int fd0
)
174 /* 1.44 Mb 3"5 drive */
178 /* 2.88 Mb 3"5 drive */
182 /* 1.2 Mb 5"5 drive */
192 static void cmos_init_hd(int type_ofs
, int info_ofs
, BlockDriverState
*hd
)
194 RTCState
*s
= rtc_state
;
195 int cylinders
, heads
, sectors
;
196 bdrv_get_geometry_hint(hd
, &cylinders
, &heads
, §ors
);
197 rtc_set_memory(s
, type_ofs
, 47);
198 rtc_set_memory(s
, info_ofs
, cylinders
);
199 rtc_set_memory(s
, info_ofs
+ 1, cylinders
>> 8);
200 rtc_set_memory(s
, info_ofs
+ 2, heads
);
201 rtc_set_memory(s
, info_ofs
+ 3, 0xff);
202 rtc_set_memory(s
, info_ofs
+ 4, 0xff);
203 rtc_set_memory(s
, info_ofs
+ 5, 0xc0 | ((heads
> 8) << 3));
204 rtc_set_memory(s
, info_ofs
+ 6, cylinders
);
205 rtc_set_memory(s
, info_ofs
+ 7, cylinders
>> 8);
206 rtc_set_memory(s
, info_ofs
+ 8, sectors
);
209 /* convert boot_device letter to something recognizable by the bios */
210 static int boot_device2nibble(char boot_device
)
212 switch(boot_device
) {
215 return 0x01; /* floppy boot */
217 return 0x02; /* hard drive boot */
219 return 0x03; /* CD-ROM boot */
221 return 0x04; /* Network boot */
226 /* copy/pasted from cmos_init, should be made a general function
227 and used there as well */
228 static int pc_boot_set(void *opaque
, const char *boot_device
)
230 Monitor
*mon
= cur_mon
;
231 #define PC_MAX_BOOT_DEVICES 3
232 RTCState
*s
= (RTCState
*)opaque
;
233 int nbds
, bds
[3] = { 0, };
236 nbds
= strlen(boot_device
);
237 if (nbds
> PC_MAX_BOOT_DEVICES
) {
238 monitor_printf(mon
, "Too many boot devices for PC\n");
241 for (i
= 0; i
< nbds
; i
++) {
242 bds
[i
] = boot_device2nibble(boot_device
[i
]);
244 monitor_printf(mon
, "Invalid boot device for PC: '%c'\n",
249 rtc_set_memory(s
, 0x3d, (bds
[1] << 4) | bds
[0]);
250 rtc_set_memory(s
, 0x38, (bds
[2] << 4));
254 /* hd_table must contain 4 block drivers */
255 static void cmos_init(ram_addr_t ram_size
, ram_addr_t above_4g_mem_size
,
256 const char *boot_device
, DriveInfo
**hd_table
)
258 RTCState
*s
= rtc_state
;
259 int nbds
, bds
[3] = { 0, };
264 /* various important CMOS locations needed by PC/Bochs bios */
267 val
= 640; /* base memory in K */
268 rtc_set_memory(s
, 0x15, val
);
269 rtc_set_memory(s
, 0x16, val
>> 8);
271 val
= (ram_size
/ 1024) - 1024;
274 rtc_set_memory(s
, 0x17, val
);
275 rtc_set_memory(s
, 0x18, val
>> 8);
276 rtc_set_memory(s
, 0x30, val
);
277 rtc_set_memory(s
, 0x31, val
>> 8);
279 if (above_4g_mem_size
) {
280 rtc_set_memory(s
, 0x5b, (unsigned int)above_4g_mem_size
>> 16);
281 rtc_set_memory(s
, 0x5c, (unsigned int)above_4g_mem_size
>> 24);
282 rtc_set_memory(s
, 0x5d, (uint64_t)above_4g_mem_size
>> 32);
285 if (ram_size
> (16 * 1024 * 1024))
286 val
= (ram_size
/ 65536) - ((16 * 1024 * 1024) / 65536);
291 rtc_set_memory(s
, 0x34, val
);
292 rtc_set_memory(s
, 0x35, val
>> 8);
294 /* set the number of CPU */
295 rtc_set_memory(s
, 0x5f, smp_cpus
- 1);
297 /* set boot devices, and disable floppy signature check if requested */
298 #define PC_MAX_BOOT_DEVICES 3
299 nbds
= strlen(boot_device
);
300 if (nbds
> PC_MAX_BOOT_DEVICES
) {
301 fprintf(stderr
, "Too many boot devices for PC\n");
304 for (i
= 0; i
< nbds
; i
++) {
305 bds
[i
] = boot_device2nibble(boot_device
[i
]);
307 fprintf(stderr
, "Invalid boot device for PC: '%c'\n",
312 rtc_set_memory(s
, 0x3d, (bds
[1] << 4) | bds
[0]);
313 rtc_set_memory(s
, 0x38, (bds
[2] << 4) | (fd_bootchk
? 0x0 : 0x1));
317 fd0
= fdctrl_get_drive_type(floppy_controller
, 0);
318 fd1
= fdctrl_get_drive_type(floppy_controller
, 1);
320 val
= (cmos_get_fd_drive_type(fd0
) << 4) | cmos_get_fd_drive_type(fd1
);
321 rtc_set_memory(s
, 0x10, val
);
333 val
|= 0x01; /* 1 drive, ready for boot */
336 val
|= 0x41; /* 2 drives, ready for boot */
339 val
|= 0x02; /* FPU is there */
340 val
|= 0x04; /* PS/2 mouse installed */
341 rtc_set_memory(s
, REG_EQUIPMENT_BYTE
, val
);
345 rtc_set_memory(s
, 0x12, (hd_table
[0] ? 0xf0 : 0) | (hd_table
[1] ? 0x0f : 0));
347 cmos_init_hd(0x19, 0x1b, hd_table
[0]->bdrv
);
349 cmos_init_hd(0x1a, 0x24, hd_table
[1]->bdrv
);
352 for (i
= 0; i
< 4; i
++) {
354 int cylinders
, heads
, sectors
, translation
;
355 /* NOTE: bdrv_get_geometry_hint() returns the physical
356 geometry. It is always such that: 1 <= sects <= 63, 1
357 <= heads <= 16, 1 <= cylinders <= 16383. The BIOS
358 geometry can be different if a translation is done. */
359 translation
= bdrv_get_translation_hint(hd_table
[i
]->bdrv
);
360 if (translation
== BIOS_ATA_TRANSLATION_AUTO
) {
361 bdrv_get_geometry_hint(hd_table
[i
]->bdrv
, &cylinders
, &heads
, §ors
);
362 if (cylinders
<= 1024 && heads
<= 16 && sectors
<= 63) {
363 /* No translation. */
366 /* LBA translation. */
372 val
|= translation
<< (i
* 2);
375 rtc_set_memory(s
, 0x39, val
);
378 void ioport_set_a20(int enable
)
380 /* XXX: send to all CPUs ? */
381 cpu_x86_set_a20(first_cpu
, enable
);
384 int ioport_get_a20(void)
386 return ((first_cpu
->a20_mask
>> 20) & 1);
389 static void ioport92_write(void *opaque
, uint32_t addr
, uint32_t val
)
391 ioport_set_a20((val
>> 1) & 1);
392 /* XXX: bit 0 is fast reset */
395 static uint32_t ioport92_read(void *opaque
, uint32_t addr
)
397 return ioport_get_a20() << 1;
400 /***********************************************************/
401 /* Bochs BIOS debug ports */
403 static void bochs_bios_write(void *opaque
, uint32_t addr
, uint32_t val
)
405 static const char shutdown_str
[8] = "Shutdown";
406 static int shutdown_index
= 0;
409 /* Bochs BIOS messages */
412 fprintf(stderr
, "BIOS panic at rombios.c, line %d\n", val
);
417 fprintf(stderr
, "%c", val
);
421 /* same as Bochs power off */
422 if (val
== shutdown_str
[shutdown_index
]) {
424 if (shutdown_index
== 8) {
426 qemu_system_shutdown_request();
433 /* LGPL'ed VGA BIOS messages */
436 fprintf(stderr
, "VGA BIOS panic, line %d\n", val
);
441 fprintf(stderr
, "%c", val
);
447 static void *bochs_bios_init(void)
450 uint8_t *smbios_table
;
452 uint64_t *numa_fw_cfg
;
455 register_ioport_write(0x400, 1, 2, bochs_bios_write
, NULL
);
456 register_ioport_write(0x401, 1, 2, bochs_bios_write
, NULL
);
457 register_ioport_write(0x402, 1, 1, bochs_bios_write
, NULL
);
458 register_ioport_write(0x403, 1, 1, bochs_bios_write
, NULL
);
459 register_ioport_write(0x8900, 1, 1, bochs_bios_write
, NULL
);
461 register_ioport_write(0x501, 1, 2, bochs_bios_write
, NULL
);
462 register_ioport_write(0x502, 1, 2, bochs_bios_write
, NULL
);
463 register_ioport_write(0x500, 1, 1, bochs_bios_write
, NULL
);
464 register_ioport_write(0x503, 1, 1, bochs_bios_write
, NULL
);
466 fw_cfg
= fw_cfg_init(BIOS_CFG_IOPORT
, BIOS_CFG_IOPORT
+ 1, 0, 0);
468 fw_cfg_add_i32(fw_cfg
, FW_CFG_ID
, 1);
469 fw_cfg_add_i64(fw_cfg
, FW_CFG_RAM_SIZE
, (uint64_t)ram_size
);
470 fw_cfg_add_bytes(fw_cfg
, FW_CFG_ACPI_TABLES
, (uint8_t *)acpi_tables
,
472 fw_cfg_add_bytes(fw_cfg
, FW_CFG_IRQ0_OVERRIDE
, &irq0override
, 1);
474 smbios_table
= smbios_get_table(&smbios_len
);
476 fw_cfg_add_bytes(fw_cfg
, FW_CFG_SMBIOS_ENTRIES
,
477 smbios_table
, smbios_len
);
479 /* allocate memory for the NUMA channel: one (64bit) word for the number
480 * of nodes, one word for each VCPU->node and one word for each node to
481 * hold the amount of memory.
483 numa_fw_cfg
= qemu_mallocz((1 + smp_cpus
+ nb_numa_nodes
) * 8);
484 numa_fw_cfg
[0] = cpu_to_le64(nb_numa_nodes
);
485 for (i
= 0; i
< smp_cpus
; i
++) {
486 for (j
= 0; j
< nb_numa_nodes
; j
++) {
487 if (node_cpumask
[j
] & (1 << i
)) {
488 numa_fw_cfg
[i
+ 1] = cpu_to_le64(j
);
493 for (i
= 0; i
< nb_numa_nodes
; i
++) {
494 numa_fw_cfg
[smp_cpus
+ 1 + i
] = cpu_to_le64(node_mem
[i
]);
496 fw_cfg_add_bytes(fw_cfg
, FW_CFG_NUMA
, (uint8_t *)numa_fw_cfg
,
497 (1 + smp_cpus
+ nb_numa_nodes
) * 8);
502 static long get_file_size(FILE *f
)
506 /* XXX: on Unix systems, using fstat() probably makes more sense */
509 fseek(f
, 0, SEEK_END
);
511 fseek(f
, where
, SEEK_SET
);
516 #define MULTIBOOT_STRUCT_ADDR 0x9000
518 #if MULTIBOOT_STRUCT_ADDR > 0xf0000
519 #error multiboot struct needs to fit in 16 bit real mode
522 static int load_multiboot(void *fw_cfg
,
524 const char *kernel_filename
,
525 const char *initrd_filename
,
526 const char *kernel_cmdline
,
529 int i
, is_multiboot
= 0;
531 uint32_t mh_entry_addr
;
532 uint32_t mh_load_addr
;
533 uint32_t mb_kernel_size
;
534 uint32_t mmap_addr
= MULTIBOOT_STRUCT_ADDR
;
535 uint32_t mb_bootinfo
= MULTIBOOT_STRUCT_ADDR
+ 0x500;
537 uint8_t bootinfo
[0x500];
538 uint32_t cmdline
= 0x200;
539 uint8_t *mb_kernel_data
;
540 uint8_t *mb_bootinfo_data
;
542 /* Ok, let's see if it is a multiboot image.
543 The header is 12x32bit long, so the latest entry may be 8192 - 48. */
544 for (i
= 0; i
< (8192 - 48); i
+= 4) {
545 if (ldl_p(header
+i
) == 0x1BADB002) {
546 uint32_t checksum
= ldl_p(header
+i
+8);
547 flags
= ldl_p(header
+i
+4);
549 checksum
+= (uint32_t)0x1BADB002;
558 return 0; /* no multiboot */
560 #ifdef DEBUG_MULTIBOOT
561 fprintf(stderr
, "qemu: I believe we found a multiboot image!\n");
563 memset(bootinfo
, 0, sizeof(bootinfo
));
565 if (flags
& 0x00000004) { /* MULTIBOOT_HEADER_HAS_VBE */
566 fprintf(stderr
, "qemu: multiboot knows VBE. we don't.\n");
568 if (!(flags
& 0x00010000)) { /* MULTIBOOT_HEADER_HAS_ADDR */
570 uint64_t elf_low
, elf_high
;
573 kernel_size
= load_elf(kernel_filename
, 0, &elf_entry
, &elf_low
, &elf_high
,
575 if (kernel_size
< 0) {
576 fprintf(stderr
, "Error while loading elf kernel\n");
579 mh_load_addr
= elf_low
;
580 mb_kernel_size
= elf_high
- elf_low
;
581 mh_entry_addr
= elf_entry
;
583 mb_kernel_data
= qemu_malloc(mb_kernel_size
);
584 if (rom_copy(mb_kernel_data
, mh_load_addr
, mb_kernel_size
) != mb_kernel_size
) {
585 fprintf(stderr
, "Error while fetching elf kernel from rom\n");
589 #ifdef DEBUG_MULTIBOOT
590 fprintf(stderr
, "qemu: loading multiboot-elf kernel (%#x bytes) with entry %#zx\n",
591 mb_kernel_size
, (size_t)mh_entry_addr
);
594 /* Valid if mh_flags sets MULTIBOOT_HEADER_HAS_ADDR. */
595 uint32_t mh_header_addr
= ldl_p(header
+i
+12);
596 mh_load_addr
= ldl_p(header
+i
+16);
597 #ifdef DEBUG_MULTIBOOT
598 uint32_t mh_load_end_addr
= ldl_p(header
+i
+20);
599 uint32_t mh_bss_end_addr
= ldl_p(header
+i
+24);
601 uint32_t mb_kernel_text_offset
= i
- (mh_header_addr
- mh_load_addr
);
603 mh_entry_addr
= ldl_p(header
+i
+28);
604 mb_kernel_size
= get_file_size(f
) - mb_kernel_text_offset
;
606 /* Valid if mh_flags sets MULTIBOOT_HEADER_HAS_VBE.
607 uint32_t mh_mode_type = ldl_p(header+i+32);
608 uint32_t mh_width = ldl_p(header+i+36);
609 uint32_t mh_height = ldl_p(header+i+40);
610 uint32_t mh_depth = ldl_p(header+i+44); */
612 #ifdef DEBUG_MULTIBOOT
613 fprintf(stderr
, "multiboot: mh_header_addr = %#x\n", mh_header_addr
);
614 fprintf(stderr
, "multiboot: mh_load_addr = %#x\n", mh_load_addr
);
615 fprintf(stderr
, "multiboot: mh_load_end_addr = %#x\n", mh_load_end_addr
);
616 fprintf(stderr
, "multiboot: mh_bss_end_addr = %#x\n", mh_bss_end_addr
);
617 fprintf(stderr
, "qemu: loading multiboot kernel (%#x bytes) at %#x\n",
618 mb_kernel_size
, mh_load_addr
);
621 mb_kernel_data
= qemu_malloc(mb_kernel_size
);
622 fseek(f
, mb_kernel_text_offset
, SEEK_SET
);
623 fread(mb_kernel_data
, 1, mb_kernel_size
, f
);
627 /* blob size is only the kernel for now */
628 mb_mod_end
= mh_load_addr
+ mb_kernel_size
;
631 stl_p(bootinfo
+ 20, 0x0); /* mods_count */
632 if (initrd_filename
) {
633 uint32_t mb_mod_info
= 0x100;
634 uint32_t mb_mod_cmdline
= 0x300;
635 uint32_t mb_mod_start
= mh_load_addr
;
636 uint32_t mb_mod_length
= mb_kernel_size
;
639 int mb_mod_count
= 0;
642 if (mb_mod_info
+ 16 > mb_mod_cmdline
) {
643 printf("WARNING: Too many modules loaded, aborting.\n");
647 next_initrd
= strchr(initrd_filename
, ',');
650 /* if a space comes after the module filename, treat everything
651 after that as parameters */
652 pstrcpy((char*)bootinfo
+ mb_mod_cmdline
,
653 sizeof(bootinfo
) - mb_mod_cmdline
,
655 stl_p(bootinfo
+ mb_mod_info
+ 8, mb_bootinfo
+ mb_mod_cmdline
); /* string */
656 mb_mod_cmdline
+= strlen(initrd_filename
) + 1;
657 if (mb_mod_cmdline
> sizeof(bootinfo
)) {
658 mb_mod_cmdline
= sizeof(bootinfo
);
659 printf("WARNING: Too many module cmdlines loaded, aborting.\n");
662 if ((next_space
= strchr(initrd_filename
, ' ')))
664 #ifdef DEBUG_MULTIBOOT
665 printf("multiboot loading module: %s\n", initrd_filename
);
667 mb_mod_start
= (mb_mod_start
+ mb_mod_length
+ (TARGET_PAGE_SIZE
- 1))
668 & (TARGET_PAGE_MASK
);
669 mb_mod_length
= get_image_size(initrd_filename
);
670 if (mb_mod_length
< 0) {
671 fprintf(stderr
, "failed to get %s image size\n", initrd_filename
);
674 mb_mod_end
= mb_mod_start
+ mb_mod_length
;
677 /* append module data at the end of last module */
678 mb_kernel_data
= qemu_realloc(mb_kernel_data
,
679 mb_mod_end
- mh_load_addr
);
680 load_image(initrd_filename
,
681 mb_kernel_data
+ mb_mod_start
- mh_load_addr
);
683 stl_p(bootinfo
+ mb_mod_info
+ 0, mb_mod_start
);
684 stl_p(bootinfo
+ mb_mod_info
+ 4, mb_mod_start
+ mb_mod_length
);
685 stl_p(bootinfo
+ mb_mod_info
+ 12, 0x0); /* reserved */
686 #ifdef DEBUG_MULTIBOOT
687 printf("mod_start: %#x\nmod_end: %#x\n", mb_mod_start
,
688 mb_mod_start
+ mb_mod_length
);
690 initrd_filename
= next_initrd
+1;
692 } while (next_initrd
);
693 stl_p(bootinfo
+ 20, mb_mod_count
); /* mods_count */
694 stl_p(bootinfo
+ 24, mb_bootinfo
+ 0x100); /* mods_addr */
697 /* Commandline support */
698 stl_p(bootinfo
+ 16, mb_bootinfo
+ cmdline
);
699 snprintf((char*)bootinfo
+ cmdline
, 0x100, "%s %s",
700 kernel_filename
, kernel_cmdline
);
702 /* the kernel is where we want it to be now */
703 #define MULTIBOOT_FLAGS_MEMORY (1 << 0)
704 #define MULTIBOOT_FLAGS_BOOT_DEVICE (1 << 1)
705 #define MULTIBOOT_FLAGS_CMDLINE (1 << 2)
706 #define MULTIBOOT_FLAGS_MODULES (1 << 3)
707 #define MULTIBOOT_FLAGS_MMAP (1 << 6)
708 stl_p(bootinfo
, MULTIBOOT_FLAGS_MEMORY
709 | MULTIBOOT_FLAGS_BOOT_DEVICE
710 | MULTIBOOT_FLAGS_CMDLINE
711 | MULTIBOOT_FLAGS_MODULES
712 | MULTIBOOT_FLAGS_MMAP
);
713 stl_p(bootinfo
+ 4, 640); /* mem_lower */
714 stl_p(bootinfo
+ 8, ram_size
/ 1024); /* mem_upper */
715 stl_p(bootinfo
+ 12, 0x8001ffff); /* XXX: use the -boot switch? */
716 stl_p(bootinfo
+ 48, mmap_addr
); /* mmap_addr */
718 #ifdef DEBUG_MULTIBOOT
719 fprintf(stderr
, "multiboot: mh_entry_addr = %#x\n", mh_entry_addr
);
722 /* save bootinfo off the stack */
723 mb_bootinfo_data
= qemu_malloc(sizeof(bootinfo
));
724 memcpy(mb_bootinfo_data
, bootinfo
, sizeof(bootinfo
));
726 /* Pass variables to option rom */
727 fw_cfg_add_i32(fw_cfg
, FW_CFG_KERNEL_ENTRY
, mh_entry_addr
);
728 fw_cfg_add_i32(fw_cfg
, FW_CFG_KERNEL_ADDR
, mh_load_addr
);
729 fw_cfg_add_i32(fw_cfg
, FW_CFG_KERNEL_SIZE
, mb_mod_end
- mh_load_addr
);
730 fw_cfg_add_bytes(fw_cfg
, FW_CFG_KERNEL_DATA
, mb_kernel_data
,
731 mb_mod_end
- mh_load_addr
);
733 fw_cfg_add_i32(fw_cfg
, FW_CFG_INITRD_ADDR
, mb_bootinfo
);
734 fw_cfg_add_i32(fw_cfg
, FW_CFG_INITRD_SIZE
, sizeof(bootinfo
));
735 fw_cfg_add_bytes(fw_cfg
, FW_CFG_INITRD_DATA
, mb_bootinfo_data
,
738 option_rom
[nb_option_roms
] = "multiboot.bin";
741 return 1; /* yes, we are multiboot */
744 static void load_linux(void *fw_cfg
,
745 const char *kernel_filename
,
746 const char *initrd_filename
,
747 const char *kernel_cmdline
,
748 target_phys_addr_t max_ram_size
)
751 int setup_size
, kernel_size
, initrd_size
= 0, cmdline_size
;
753 uint8_t header
[8192], *setup
, *kernel
, *initrd_data
;
754 target_phys_addr_t real_addr
, prot_addr
, cmdline_addr
, initrd_addr
= 0;
758 /* Align to 16 bytes as a paranoia measure */
759 cmdline_size
= (strlen(kernel_cmdline
)+16) & ~15;
761 /* load the kernel header */
762 f
= fopen(kernel_filename
, "rb");
763 if (!f
|| !(kernel_size
= get_file_size(f
)) ||
764 fread(header
, 1, MIN(ARRAY_SIZE(header
), kernel_size
), f
) !=
765 MIN(ARRAY_SIZE(header
), kernel_size
)) {
766 fprintf(stderr
, "qemu: could not load kernel '%s': %s\n",
767 kernel_filename
, strerror(errno
));
771 /* kernel protocol version */
773 fprintf(stderr
, "header magic: %#x\n", ldl_p(header
+0x202));
775 if (ldl_p(header
+0x202) == 0x53726448)
776 protocol
= lduw_p(header
+0x206);
778 /* This looks like a multiboot kernel. If it is, let's stop
779 treating it like a Linux kernel. */
780 if (load_multiboot(fw_cfg
, f
, kernel_filename
,
781 initrd_filename
, kernel_cmdline
, header
))
786 if (protocol
< 0x200 || !(header
[0x211] & 0x01)) {
789 cmdline_addr
= 0x9a000 - cmdline_size
;
791 } else if (protocol
< 0x202) {
792 /* High but ancient kernel */
794 cmdline_addr
= 0x9a000 - cmdline_size
;
795 prot_addr
= 0x100000;
797 /* High and recent kernel */
799 cmdline_addr
= 0x20000;
800 prot_addr
= 0x100000;
805 "qemu: real_addr = 0x" TARGET_FMT_plx
"\n"
806 "qemu: cmdline_addr = 0x" TARGET_FMT_plx
"\n"
807 "qemu: prot_addr = 0x" TARGET_FMT_plx
"\n",
813 /* highest address for loading the initrd */
814 if (protocol
>= 0x203)
815 initrd_max
= ldl_p(header
+0x22c);
817 initrd_max
= 0x37ffffff;
819 if (initrd_max
>= max_ram_size
-ACPI_DATA_SIZE
)
820 initrd_max
= max_ram_size
-ACPI_DATA_SIZE
-1;
822 fw_cfg_add_i32(fw_cfg
, FW_CFG_CMDLINE_ADDR
, cmdline_addr
);
823 fw_cfg_add_i32(fw_cfg
, FW_CFG_CMDLINE_SIZE
, strlen(kernel_cmdline
)+1);
824 fw_cfg_add_bytes(fw_cfg
, FW_CFG_CMDLINE_DATA
,
825 (uint8_t*)strdup(kernel_cmdline
),
826 strlen(kernel_cmdline
)+1);
828 if (protocol
>= 0x202) {
829 stl_p(header
+0x228, cmdline_addr
);
831 stw_p(header
+0x20, 0xA33F);
832 stw_p(header
+0x22, cmdline_addr
-real_addr
);
835 /* handle vga= parameter */
836 vmode
= strstr(kernel_cmdline
, "vga=");
838 unsigned int video_mode
;
841 if (!strncmp(vmode
, "normal", 6)) {
843 } else if (!strncmp(vmode
, "ext", 3)) {
845 } else if (!strncmp(vmode
, "ask", 3)) {
848 video_mode
= strtol(vmode
, NULL
, 0);
850 stw_p(header
+0x1fa, video_mode
);
854 /* High nybble = B reserved for Qemu; low nybble is revision number.
855 If this code is substantially changed, you may want to consider
856 incrementing the revision. */
857 if (protocol
>= 0x200)
858 header
[0x210] = 0xB0;
861 if (protocol
>= 0x201) {
862 header
[0x211] |= 0x80; /* CAN_USE_HEAP */
863 stw_p(header
+0x224, cmdline_addr
-real_addr
-0x200);
867 if (initrd_filename
) {
868 if (protocol
< 0x200) {
869 fprintf(stderr
, "qemu: linux kernel too old to load a ram disk\n");
873 initrd_size
= get_image_size(initrd_filename
);
874 initrd_addr
= (initrd_max
-initrd_size
) & ~4095;
876 initrd_data
= qemu_malloc(initrd_size
);
877 load_image(initrd_filename
, initrd_data
);
879 fw_cfg_add_i32(fw_cfg
, FW_CFG_INITRD_ADDR
, initrd_addr
);
880 fw_cfg_add_i32(fw_cfg
, FW_CFG_INITRD_SIZE
, initrd_size
);
881 fw_cfg_add_bytes(fw_cfg
, FW_CFG_INITRD_DATA
, initrd_data
, initrd_size
);
883 stl_p(header
+0x218, initrd_addr
);
884 stl_p(header
+0x21c, initrd_size
);
887 /* load kernel and setup */
888 setup_size
= header
[0x1f1];
891 setup_size
= (setup_size
+1)*512;
892 kernel_size
-= setup_size
;
894 setup
= qemu_malloc(setup_size
);
895 kernel
= qemu_malloc(kernel_size
);
896 fseek(f
, 0, SEEK_SET
);
897 fread(setup
, 1, setup_size
, f
);
898 fread(kernel
, 1, kernel_size
, f
);
900 memcpy(setup
, header
, MIN(sizeof(header
), setup_size
));
902 fw_cfg_add_i32(fw_cfg
, FW_CFG_KERNEL_ADDR
, prot_addr
);
903 fw_cfg_add_i32(fw_cfg
, FW_CFG_KERNEL_SIZE
, kernel_size
);
904 fw_cfg_add_bytes(fw_cfg
, FW_CFG_KERNEL_DATA
, kernel
, kernel_size
);
906 fw_cfg_add_i32(fw_cfg
, FW_CFG_SETUP_ADDR
, real_addr
);
907 fw_cfg_add_i32(fw_cfg
, FW_CFG_SETUP_SIZE
, setup_size
);
908 fw_cfg_add_bytes(fw_cfg
, FW_CFG_SETUP_DATA
, setup
, setup_size
);
910 option_rom
[nb_option_roms
] = "linuxboot.bin";
914 static const int ide_iobase
[2] = { 0x1f0, 0x170 };
915 static const int ide_iobase2
[2] = { 0x3f6, 0x376 };
916 static const int ide_irq
[2] = { 14, 15 };
918 #define NE2000_NB_MAX 6
920 static const int ne2000_io
[NE2000_NB_MAX
] = { 0x300, 0x320, 0x340, 0x360,
922 static const int ne2000_irq
[NE2000_NB_MAX
] = { 9, 10, 11, 3, 4, 5 };
924 static const int parallel_io
[MAX_PARALLEL_PORTS
] = { 0x378, 0x278, 0x3bc };
925 static const int parallel_irq
[MAX_PARALLEL_PORTS
] = { 7, 7, 7 };
928 static void audio_init (PCIBus
*pci_bus
, qemu_irq
*pic
)
932 for (c
= soundhw
; c
->name
; ++c
) {
935 c
->init
.init_isa(pic
);
938 c
->init
.init_pci(pci_bus
);
946 static void pc_init_ne2k_isa(NICInfo
*nd
)
948 static int nb_ne2k
= 0;
950 if (nb_ne2k
== NE2000_NB_MAX
)
952 isa_ne2000_init(ne2000_io
[nb_ne2k
],
953 ne2000_irq
[nb_ne2k
], nd
);
957 int cpu_is_bsp(CPUState
*env
)
959 return env
->cpuid_apic_id
== 0;
962 CPUState
*pc_new_cpu(const char *cpu_model
)
966 env
= cpu_init(cpu_model
);
968 fprintf(stderr
, "Unable to find x86 CPU definition\n");
971 env
->kvm_cpu_state
.regs_modified
= 1;
972 if ((env
->cpuid_features
& CPUID_APIC
) || smp_cpus
> 1) {
973 env
->cpuid_apic_id
= env
->cpu_index
;
974 /* APIC reset callback resets cpu */
977 qemu_register_reset((QEMUResetHandler
*)cpu_reset
, env
);
980 /* kvm needs this to run after the apic is initialized. Otherwise,
981 * it can access invalid state and crash.
987 /* PC hardware initialisation */
988 static void pc_init1(ram_addr_t ram_size
,
989 const char *boot_device
,
990 const char *kernel_filename
,
991 const char *kernel_cmdline
,
992 const char *initrd_filename
,
993 const char *cpu_model
,
997 int ret
, linux_boot
, i
;
998 ram_addr_t ram_addr
, bios_offset
, option_rom_offset
;
999 ram_addr_t below_4g_mem_size
, above_4g_mem_size
= 0;
1000 int bios_size
, isa_bios_size
;
1003 int piix3_devfn
= -1;
1008 IsaIrqState
*isa_irq_state
;
1009 DriveInfo
*hd
[MAX_IDE_BUS
* MAX_IDE_DEVS
];
1010 DriveInfo
*fd
[MAX_FD
];
1013 if (ram_size
>= 0xe0000000 ) {
1014 above_4g_mem_size
= ram_size
- 0xe0000000;
1015 below_4g_mem_size
= 0xe0000000;
1017 below_4g_mem_size
= ram_size
;
1020 linux_boot
= (kernel_filename
!= NULL
);
1023 if (cpu_model
== NULL
) {
1024 #ifdef TARGET_X86_64
1025 cpu_model
= "qemu64";
1027 cpu_model
= "qemu32";
1031 if (kvm_enabled()) {
1032 kvm_set_boot_cpu_id(0);
1034 for (i
= 0; i
< smp_cpus
; i
++) {
1035 env
= pc_new_cpu(cpu_model
);
1041 ram_addr
= qemu_ram_alloc(below_4g_mem_size
);
1042 cpu_register_physical_memory(0, 0xa0000, ram_addr
);
1043 cpu_register_physical_memory(0x100000,
1044 below_4g_mem_size
- 0x100000,
1045 ram_addr
+ 0x100000);
1047 /* above 4giga memory allocation */
1048 if (above_4g_mem_size
> 0) {
1049 #if TARGET_PHYS_ADDR_BITS == 32
1050 hw_error("To much RAM for 32-bit physical address");
1052 ram_addr
= qemu_ram_alloc(above_4g_mem_size
);
1053 cpu_register_physical_memory(0x100000000ULL
,
1061 if (bios_name
== NULL
)
1062 bios_name
= BIOS_FILENAME
;
1063 filename
= qemu_find_file(QEMU_FILE_TYPE_BIOS
, bios_name
);
1065 bios_size
= get_image_size(filename
);
1069 if (bios_size
<= 0 ||
1070 (bios_size
% 65536) != 0) {
1073 bios_offset
= qemu_ram_alloc(bios_size
);
1074 ret
= rom_add_file_fixed(bios_name
, (uint32_t)(-bios_size
));
1077 fprintf(stderr
, "qemu: could not load PC BIOS '%s'\n", bios_name
);
1081 qemu_free(filename
);
1083 /* map the last 128KB of the BIOS in ISA space */
1084 isa_bios_size
= bios_size
;
1085 if (isa_bios_size
> (128 * 1024))
1086 isa_bios_size
= 128 * 1024;
1087 cpu_register_physical_memory(0xd0000, (192 * 1024) - isa_bios_size
,
1089 /* kvm tpr optimization needs the bios accessible for write, at least to qemu itself */
1090 cpu_register_physical_memory(0x100000 - isa_bios_size
,
1092 (bios_offset
+ bios_size
- isa_bios_size
) /* | IO_MEM_ROM */);
1094 if (extboot_drive
) {
1095 option_rom
[nb_option_roms
++] = qemu_strdup(EXTBOOT_FILENAME
);
1097 option_rom
[nb_option_roms
++] = qemu_strdup(VAPIC_FILENAME
);
1099 rom_enable_driver_roms
= 1;
1100 option_rom_offset
= qemu_ram_alloc(PC_ROM_SIZE
);
1101 cpu_register_physical_memory(PC_ROM_MIN_VGA
, PC_ROM_SIZE
, option_rom_offset
);
1103 /* map all the bios at the top of memory */
1104 cpu_register_physical_memory((uint32_t)(-bios_size
),
1105 bios_size
, bios_offset
| IO_MEM_ROM
);
1107 fw_cfg
= bochs_bios_init();
1110 load_linux(fw_cfg
, kernel_filename
, initrd_filename
, kernel_cmdline
, below_4g_mem_size
);
1113 for (i
= 0; i
< nb_option_roms
; i
++) {
1114 rom_add_option(option_rom
[i
]);
1117 cpu_irq
= qemu_allocate_irqs(pic_irq_request
, NULL
, 1);
1118 #ifdef KVM_CAP_IRQCHIP
1119 if (kvm_enabled() && kvm_irqchip_in_kernel()) {
1120 isa_irq_state
= qemu_mallocz(sizeof(*isa_irq_state
));
1121 isa_irq
= i8259
= kvm_i8259_init(cpu_irq
[0]);
1125 i8259
= i8259_init(cpu_irq
[0]);
1126 isa_irq_state
= qemu_mallocz(sizeof(*isa_irq_state
));
1127 isa_irq_state
->i8259
= i8259
;
1128 isa_irq
= qemu_allocate_irqs(isa_irq_handler
, isa_irq_state
, 24);
1132 pci_bus
= i440fx_init(&i440fx_state
, &piix3_devfn
, isa_irq
);
1137 isa_bus_irqs(isa_irq
);
1139 ferr_irq
= isa_reserve_irq(13);
1141 /* init basic PC hardware */
1142 register_ioport_write(0x80, 1, 1, ioport80_write
, NULL
);
1144 register_ioport_write(0xf0, 1, 1, ioportF0_write
, NULL
);
1146 if (cirrus_vga_enabled
) {
1148 pci_cirrus_vga_init(pci_bus
);
1150 isa_cirrus_vga_init();
1152 } else if (vmsvga_enabled
) {
1154 pci_vmsvga_init(pci_bus
);
1156 fprintf(stderr
, "%s: vmware_vga: no PCI bus\n", __FUNCTION__
);
1157 } else if (std_vga_enabled
) {
1159 pci_vga_init(pci_bus
, 0, 0);
1165 rtc_state
= rtc_init(2000);
1167 qemu_register_boot_set(pc_boot_set
, rtc_state
);
1169 register_ioport_read(0x92, 1, 1, ioport92_read
, NULL
);
1170 register_ioport_write(0x92, 1, 1, ioport92_write
, NULL
);
1173 isa_irq_state
->ioapic
= ioapic_init();
1174 ioapic_irq_hack
= isa_irq
;
1176 #ifdef CONFIG_KVM_PIT
1177 if (kvm_enabled() && qemu_kvm_pit_in_kernel())
1178 pit
= kvm_pit_init(0x40, isa_reserve_irq(0));
1181 pit
= pit_init(0x40, isa_reserve_irq(0));
1187 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
1188 if (serial_hds
[i
]) {
1189 serial_isa_init(i
, serial_hds
[i
]);
1193 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
1194 if (parallel_hds
[i
]) {
1195 parallel_init(i
, parallel_hds
[i
]);
1199 for(i
= 0; i
< nb_nics
; i
++) {
1200 NICInfo
*nd
= &nd_table
[i
];
1202 if (!pci_enabled
|| (nd
->model
&& strcmp(nd
->model
, "ne2k_isa") == 0))
1203 pc_init_ne2k_isa(nd
);
1205 pci_nic_init_nofail(nd
, "rtl8139", NULL
);
1208 if (drive_get_max_bus(IF_IDE
) >= MAX_IDE_BUS
) {
1209 fprintf(stderr
, "qemu: too many IDE bus\n");
1213 for(i
= 0; i
< MAX_IDE_BUS
* MAX_IDE_DEVS
; i
++) {
1214 hd
[i
] = drive_get(IF_IDE
, i
/ MAX_IDE_DEVS
, i
% MAX_IDE_DEVS
);
1218 pci_piix3_ide_init(pci_bus
, hd
, piix3_devfn
+ 1);
1220 for(i
= 0; i
< MAX_IDE_BUS
; i
++) {
1221 isa_ide_init(ide_iobase
[i
], ide_iobase2
[i
], ide_irq
[i
],
1222 hd
[MAX_IDE_DEVS
* i
], hd
[MAX_IDE_DEVS
* i
+ 1]);
1226 isa_dev
= isa_create_simple("i8042");
1229 audio_init(pci_enabled
? pci_bus
: NULL
, isa_irq
);
1232 for(i
= 0; i
< MAX_FD
; i
++) {
1233 fd
[i
] = drive_get(IF_FLOPPY
, 0, i
);
1235 floppy_controller
= fdctrl_init_isa(fd
);
1237 cmos_init(below_4g_mem_size
, above_4g_mem_size
, boot_device
, hd
);
1239 if (pci_enabled
&& usb_enabled
) {
1240 usb_uhci_piix3_init(pci_bus
, piix3_devfn
+ 2);
1243 if (pci_enabled
&& acpi_enabled
) {
1244 uint8_t *eeprom_buf
= qemu_mallocz(8 * 256); /* XXX: make this persistent */
1247 /* TODO: Populate SPD eeprom data. */
1248 smbus
= piix4_pm_init(pci_bus
, piix3_devfn
+ 3, 0xb100,
1249 isa_reserve_irq(9));
1250 for (i
= 0; i
< 8; i
++) {
1251 DeviceState
*eeprom
;
1252 eeprom
= qdev_create((BusState
*)smbus
, "smbus-eeprom");
1253 qdev_prop_set_uint8(eeprom
, "address", 0x50 + i
);
1254 qdev_prop_set_ptr(eeprom
, "data", eeprom_buf
+ (i
* 256));
1255 qdev_init_nofail(eeprom
);
1257 piix4_acpi_system_hot_add_init(pci_bus
, cpu_model
);
1261 i440fx_init_memory_mappings(i440fx_state
);
1268 max_bus
= drive_get_max_bus(IF_SCSI
);
1269 for (bus
= 0; bus
<= max_bus
; bus
++) {
1270 pci_create_simple(pci_bus
, -1, "lsi53c895a");
1274 if (extboot_drive
) {
1275 DriveInfo
*info
= extboot_drive
;
1276 int cyls
, heads
, secs
;
1278 if (info
->type
!= IF_IDE
&& info
->type
!= IF_VIRTIO
) {
1279 bdrv_guess_geometry(info
->bdrv
, &cyls
, &heads
, &secs
);
1280 bdrv_set_geometry_hint(info
->bdrv
, cyls
, heads
, secs
);
1283 extboot_init(info
->bdrv
, 1);
1286 /* Add virtio console devices */
1288 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++) {
1289 if (virtcon_hds
[i
]) {
1290 pci_create_simple(pci_bus
, -1, "virtio-console-pci");
1295 #ifdef CONFIG_KVM_DEVICE_ASSIGNMENT
1296 if (kvm_enabled()) {
1297 add_assigned_devices(pci_bus
, assigned_devices
, assigned_devices_index
);
1299 #endif /* CONFIG_KVM_DEVICE_ASSIGNMENT */
1302 static void pc_init_pci(ram_addr_t ram_size
,
1303 const char *boot_device
,
1304 const char *kernel_filename
,
1305 const char *kernel_cmdline
,
1306 const char *initrd_filename
,
1307 const char *cpu_model
)
1309 pc_init1(ram_size
, boot_device
,
1310 kernel_filename
, kernel_cmdline
,
1311 initrd_filename
, cpu_model
, 1);
1314 static void pc_init_isa(ram_addr_t ram_size
,
1315 const char *boot_device
,
1316 const char *kernel_filename
,
1317 const char *kernel_cmdline
,
1318 const char *initrd_filename
,
1319 const char *cpu_model
)
1321 if (cpu_model
== NULL
)
1323 pc_init1(ram_size
, boot_device
,
1324 kernel_filename
, kernel_cmdline
,
1325 initrd_filename
, cpu_model
, 0);
1328 /* set CMOS shutdown status register (index 0xF) as S3_resume(0xFE)
1329 BIOS will read it and start S3 resume at POST Entry */
1330 void cmos_set_s3_resume(void)
1333 rtc_set_memory(rtc_state
, 0xF, 0xFE);
1336 static QEMUMachine pc_machine
= {
1339 .desc
= "Standard PC",
1340 .init
= pc_init_pci
,
1345 static QEMUMachine pc_machine_v0_10
= {
1347 .desc
= "Standard PC, qemu 0.10",
1348 .init
= pc_init_pci
,
1350 .compat_props
= (GlobalProperty
[]) {
1352 .driver
= "virtio-blk-pci",
1353 .property
= "class",
1354 .value
= stringify(PCI_CLASS_STORAGE_OTHER
),
1356 .driver
= "virtio-console-pci",
1357 .property
= "class",
1358 .value
= stringify(PCI_CLASS_DISPLAY_OTHER
),
1360 .driver
= "virtio-net-pci",
1361 .property
= "vectors",
1362 .value
= stringify(0),
1364 .driver
= "virtio-blk-pci",
1365 .property
= "vectors",
1366 .value
= stringify(0),
1368 { /* end of list */ }
1372 static QEMUMachine isapc_machine
= {
1374 .desc
= "ISA-only PC",
1375 .init
= pc_init_isa
,
1379 static void pc_machine_init(void)
1381 qemu_register_machine(&pc_machine
);
1382 qemu_register_machine(&pc_machine_v0_10
);
1383 qemu_register_machine(&isapc_machine
);
1386 machine_init(pc_machine_init
);