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"
48 /* output Bochs bios info messages */
51 /* Show multiboot debug output */
52 //#define DEBUG_MULTIBOOT
54 #ifdef DEBUG_MULTIBOOT
55 #define mb_debug(a...) fprintf(stderr, ## a)
57 #define mb_debug(a...)
60 #define BIOS_FILENAME "bios.bin"
62 #define PC_MAX_BIOS_SIZE (4 * 1024 * 1024)
64 /* Leave a chunk of memory at the top of RAM for the BIOS ACPI tables. */
65 #define ACPI_DATA_SIZE 0x10000
66 #define BIOS_CFG_IOPORT 0x510
67 #define FW_CFG_ACPI_TABLES (FW_CFG_ARCH_LOCAL + 0)
68 #define FW_CFG_SMBIOS_ENTRIES (FW_CFG_ARCH_LOCAL + 1)
69 #define FW_CFG_IRQ0_OVERRIDE (FW_CFG_ARCH_LOCAL + 2)
73 static fdctrl_t
*floppy_controller
;
74 static RTCState
*rtc_state
;
76 static PCII440FXState
*i440fx_state
;
78 typedef struct isa_irq_state
{
83 static void isa_irq_handler(void *opaque
, int n
, int level
)
85 IsaIrqState
*isa
= (IsaIrqState
*)opaque
;
88 qemu_set_irq(isa
->i8259
[n
], level
);
91 qemu_set_irq(isa
->ioapic
[n
], level
);
94 static void ioport80_write(void *opaque
, uint32_t addr
, uint32_t data
)
98 /* MSDOS compatibility mode FPU exception support */
99 static qemu_irq ferr_irq
;
100 /* XXX: add IGNNE support */
101 void cpu_set_ferr(CPUX86State
*s
)
103 qemu_irq_raise(ferr_irq
);
106 static void ioportF0_write(void *opaque
, uint32_t addr
, uint32_t data
)
108 qemu_irq_lower(ferr_irq
);
112 uint64_t cpu_get_tsc(CPUX86State
*env
)
114 return cpu_get_ticks();
118 void cpu_smm_update(CPUState
*env
)
120 if (i440fx_state
&& env
== first_cpu
)
121 i440fx_set_smm(i440fx_state
, (env
->hflags
>> HF_SMM_SHIFT
) & 1);
126 int cpu_get_pic_interrupt(CPUState
*env
)
130 intno
= apic_get_interrupt(env
);
132 /* set irq request if a PIC irq is still pending */
133 /* XXX: improve that */
134 pic_update_irq(isa_pic
);
137 /* read the irq from the PIC */
138 if (!apic_accept_pic_intr(env
))
141 intno
= pic_read_irq(isa_pic
);
145 static void pic_irq_request(void *opaque
, int irq
, int level
)
147 CPUState
*env
= first_cpu
;
149 if (env
->apic_state
) {
151 if (apic_accept_pic_intr(env
))
152 apic_deliver_pic_intr(env
, level
);
157 cpu_interrupt(env
, CPU_INTERRUPT_HARD
);
159 cpu_reset_interrupt(env
, CPU_INTERRUPT_HARD
);
163 /* PC cmos mappings */
165 #define REG_EQUIPMENT_BYTE 0x14
167 static int cmos_get_fd_drive_type(int fd0
)
173 /* 1.44 Mb 3"5 drive */
177 /* 2.88 Mb 3"5 drive */
181 /* 1.2 Mb 5"5 drive */
191 static void cmos_init_hd(int type_ofs
, int info_ofs
, BlockDriverState
*hd
)
193 RTCState
*s
= rtc_state
;
194 int cylinders
, heads
, sectors
;
195 bdrv_get_geometry_hint(hd
, &cylinders
, &heads
, §ors
);
196 rtc_set_memory(s
, type_ofs
, 47);
197 rtc_set_memory(s
, info_ofs
, cylinders
);
198 rtc_set_memory(s
, info_ofs
+ 1, cylinders
>> 8);
199 rtc_set_memory(s
, info_ofs
+ 2, heads
);
200 rtc_set_memory(s
, info_ofs
+ 3, 0xff);
201 rtc_set_memory(s
, info_ofs
+ 4, 0xff);
202 rtc_set_memory(s
, info_ofs
+ 5, 0xc0 | ((heads
> 8) << 3));
203 rtc_set_memory(s
, info_ofs
+ 6, cylinders
);
204 rtc_set_memory(s
, info_ofs
+ 7, cylinders
>> 8);
205 rtc_set_memory(s
, info_ofs
+ 8, sectors
);
208 /* convert boot_device letter to something recognizable by the bios */
209 static int boot_device2nibble(char boot_device
)
211 switch(boot_device
) {
214 return 0x01; /* floppy boot */
216 return 0x02; /* hard drive boot */
218 return 0x03; /* CD-ROM boot */
220 return 0x04; /* Network boot */
225 /* copy/pasted from cmos_init, should be made a general function
226 and used there as well */
227 static int pc_boot_set(void *opaque
, const char *boot_device
)
229 Monitor
*mon
= cur_mon
;
230 #define PC_MAX_BOOT_DEVICES 3
231 RTCState
*s
= (RTCState
*)opaque
;
232 int nbds
, bds
[3] = { 0, };
235 nbds
= strlen(boot_device
);
236 if (nbds
> PC_MAX_BOOT_DEVICES
) {
237 monitor_printf(mon
, "Too many boot devices for PC\n");
240 for (i
= 0; i
< nbds
; i
++) {
241 bds
[i
] = boot_device2nibble(boot_device
[i
]);
243 monitor_printf(mon
, "Invalid boot device for PC: '%c'\n",
248 rtc_set_memory(s
, 0x3d, (bds
[1] << 4) | bds
[0]);
249 rtc_set_memory(s
, 0x38, (bds
[2] << 4));
253 /* hd_table must contain 4 block drivers */
254 static void cmos_init(ram_addr_t ram_size
, ram_addr_t above_4g_mem_size
,
255 const char *boot_device
, DriveInfo
**hd_table
)
257 RTCState
*s
= rtc_state
;
258 int nbds
, bds
[3] = { 0, };
263 /* various important CMOS locations needed by PC/Bochs bios */
266 val
= 640; /* base memory in K */
267 rtc_set_memory(s
, 0x15, val
);
268 rtc_set_memory(s
, 0x16, val
>> 8);
270 val
= (ram_size
/ 1024) - 1024;
273 rtc_set_memory(s
, 0x17, val
);
274 rtc_set_memory(s
, 0x18, val
>> 8);
275 rtc_set_memory(s
, 0x30, val
);
276 rtc_set_memory(s
, 0x31, val
>> 8);
278 if (above_4g_mem_size
) {
279 rtc_set_memory(s
, 0x5b, (unsigned int)above_4g_mem_size
>> 16);
280 rtc_set_memory(s
, 0x5c, (unsigned int)above_4g_mem_size
>> 24);
281 rtc_set_memory(s
, 0x5d, (uint64_t)above_4g_mem_size
>> 32);
284 if (ram_size
> (16 * 1024 * 1024))
285 val
= (ram_size
/ 65536) - ((16 * 1024 * 1024) / 65536);
290 rtc_set_memory(s
, 0x34, val
);
291 rtc_set_memory(s
, 0x35, val
>> 8);
293 /* set the number of CPU */
294 rtc_set_memory(s
, 0x5f, smp_cpus
- 1);
296 /* set boot devices, and disable floppy signature check if requested */
297 #define PC_MAX_BOOT_DEVICES 3
298 nbds
= strlen(boot_device
);
299 if (nbds
> PC_MAX_BOOT_DEVICES
) {
300 fprintf(stderr
, "Too many boot devices for PC\n");
303 for (i
= 0; i
< nbds
; i
++) {
304 bds
[i
] = boot_device2nibble(boot_device
[i
]);
306 fprintf(stderr
, "Invalid boot device for PC: '%c'\n",
311 rtc_set_memory(s
, 0x3d, (bds
[1] << 4) | bds
[0]);
312 rtc_set_memory(s
, 0x38, (bds
[2] << 4) | (fd_bootchk
? 0x0 : 0x1));
316 fd0
= fdctrl_get_drive_type(floppy_controller
, 0);
317 fd1
= fdctrl_get_drive_type(floppy_controller
, 1);
319 val
= (cmos_get_fd_drive_type(fd0
) << 4) | cmos_get_fd_drive_type(fd1
);
320 rtc_set_memory(s
, 0x10, val
);
332 val
|= 0x01; /* 1 drive, ready for boot */
335 val
|= 0x41; /* 2 drives, ready for boot */
338 val
|= 0x02; /* FPU is there */
339 val
|= 0x04; /* PS/2 mouse installed */
340 rtc_set_memory(s
, REG_EQUIPMENT_BYTE
, val
);
344 rtc_set_memory(s
, 0x12, (hd_table
[0] ? 0xf0 : 0) | (hd_table
[1] ? 0x0f : 0));
346 cmos_init_hd(0x19, 0x1b, hd_table
[0]->bdrv
);
348 cmos_init_hd(0x1a, 0x24, hd_table
[1]->bdrv
);
351 for (i
= 0; i
< 4; i
++) {
353 int cylinders
, heads
, sectors
, translation
;
354 /* NOTE: bdrv_get_geometry_hint() returns the physical
355 geometry. It is always such that: 1 <= sects <= 63, 1
356 <= heads <= 16, 1 <= cylinders <= 16383. The BIOS
357 geometry can be different if a translation is done. */
358 translation
= bdrv_get_translation_hint(hd_table
[i
]->bdrv
);
359 if (translation
== BIOS_ATA_TRANSLATION_AUTO
) {
360 bdrv_get_geometry_hint(hd_table
[i
]->bdrv
, &cylinders
, &heads
, §ors
);
361 if (cylinders
<= 1024 && heads
<= 16 && sectors
<= 63) {
362 /* No translation. */
365 /* LBA translation. */
371 val
|= translation
<< (i
* 2);
374 rtc_set_memory(s
, 0x39, val
);
377 void ioport_set_a20(int enable
)
379 /* XXX: send to all CPUs ? */
380 cpu_x86_set_a20(first_cpu
, enable
);
383 int ioport_get_a20(void)
385 return ((first_cpu
->a20_mask
>> 20) & 1);
388 static void ioport92_write(void *opaque
, uint32_t addr
, uint32_t val
)
390 ioport_set_a20((val
>> 1) & 1);
391 /* XXX: bit 0 is fast reset */
394 static uint32_t ioport92_read(void *opaque
, uint32_t addr
)
396 return ioport_get_a20() << 1;
399 /***********************************************************/
400 /* Bochs BIOS debug ports */
402 static void bochs_bios_write(void *opaque
, uint32_t addr
, uint32_t val
)
404 static const char shutdown_str
[8] = "Shutdown";
405 static int shutdown_index
= 0;
408 /* Bochs BIOS messages */
411 fprintf(stderr
, "BIOS panic at rombios.c, line %d\n", val
);
416 fprintf(stderr
, "%c", val
);
420 /* same as Bochs power off */
421 if (val
== shutdown_str
[shutdown_index
]) {
423 if (shutdown_index
== 8) {
425 qemu_system_shutdown_request();
432 /* LGPL'ed VGA BIOS messages */
435 fprintf(stderr
, "VGA BIOS panic, line %d\n", val
);
440 fprintf(stderr
, "%c", val
);
446 static void *bochs_bios_init(void)
449 uint8_t *smbios_table
;
451 uint64_t *numa_fw_cfg
;
454 register_ioport_write(0x400, 1, 2, bochs_bios_write
, NULL
);
455 register_ioport_write(0x401, 1, 2, bochs_bios_write
, NULL
);
456 register_ioport_write(0x402, 1, 1, bochs_bios_write
, NULL
);
457 register_ioport_write(0x403, 1, 1, bochs_bios_write
, NULL
);
458 register_ioport_write(0x8900, 1, 1, bochs_bios_write
, NULL
);
460 register_ioport_write(0x501, 1, 2, bochs_bios_write
, NULL
);
461 register_ioport_write(0x502, 1, 2, bochs_bios_write
, NULL
);
462 register_ioport_write(0x500, 1, 1, bochs_bios_write
, NULL
);
463 register_ioport_write(0x503, 1, 1, bochs_bios_write
, NULL
);
465 fw_cfg
= fw_cfg_init(BIOS_CFG_IOPORT
, BIOS_CFG_IOPORT
+ 1, 0, 0);
467 fw_cfg_add_i32(fw_cfg
, FW_CFG_ID
, 1);
468 fw_cfg_add_i64(fw_cfg
, FW_CFG_RAM_SIZE
, (uint64_t)ram_size
);
469 fw_cfg_add_bytes(fw_cfg
, FW_CFG_ACPI_TABLES
, (uint8_t *)acpi_tables
,
471 fw_cfg_add_bytes(fw_cfg
, FW_CFG_IRQ0_OVERRIDE
, &irq0override
, 1);
473 smbios_table
= smbios_get_table(&smbios_len
);
475 fw_cfg_add_bytes(fw_cfg
, FW_CFG_SMBIOS_ENTRIES
,
476 smbios_table
, smbios_len
);
478 /* allocate memory for the NUMA channel: one (64bit) word for the number
479 * of nodes, one word for each VCPU->node and one word for each node to
480 * hold the amount of memory.
482 numa_fw_cfg
= qemu_mallocz((1 + smp_cpus
+ nb_numa_nodes
) * 8);
483 numa_fw_cfg
[0] = cpu_to_le64(nb_numa_nodes
);
484 for (i
= 0; i
< smp_cpus
; i
++) {
485 for (j
= 0; j
< nb_numa_nodes
; j
++) {
486 if (node_cpumask
[j
] & (1 << i
)) {
487 numa_fw_cfg
[i
+ 1] = cpu_to_le64(j
);
492 for (i
= 0; i
< nb_numa_nodes
; i
++) {
493 numa_fw_cfg
[smp_cpus
+ 1 + i
] = cpu_to_le64(node_mem
[i
]);
495 fw_cfg_add_bytes(fw_cfg
, FW_CFG_NUMA
, (uint8_t *)numa_fw_cfg
,
496 (1 + smp_cpus
+ nb_numa_nodes
) * 8);
501 static long get_file_size(FILE *f
)
505 /* XXX: on Unix systems, using fstat() probably makes more sense */
508 fseek(f
, 0, SEEK_END
);
510 fseek(f
, where
, SEEK_SET
);
515 #define MULTIBOOT_STRUCT_ADDR 0x9000
517 #if MULTIBOOT_STRUCT_ADDR > 0xf0000
518 #error multiboot struct needs to fit in 16 bit real mode
526 MBI_BOOT_DEVICE
= 12,
534 /* Multiboot modules */
542 ADDR_E820_MAP
= MULTIBOOT_STRUCT_ADDR
+ 0,
543 ADDR_MBI
= ADDR_E820_MAP
+ 0x500,
545 /* Multiboot flags */
546 MULTIBOOT_FLAGS_MEMORY
= 1 << 0,
547 MULTIBOOT_FLAGS_BOOT_DEVICE
= 1 << 1,
548 MULTIBOOT_FLAGS_CMDLINE
= 1 << 2,
549 MULTIBOOT_FLAGS_MODULES
= 1 << 3,
550 MULTIBOOT_FLAGS_MMAP
= 1 << 6,
554 /* buffer holding kernel, cmdlines and mb_infos */
556 /* address in target */
557 target_phys_addr_t mb_buf_phys
;
558 /* size of mb_buf in bytes */
559 unsigned mb_buf_size
;
560 /* offset of mb-info's in bytes */
561 target_phys_addr_t offset_mbinfo
;
562 /* offset in buffer for cmdlines in bytes */
563 target_phys_addr_t offset_cmdlines
;
564 /* offset of modules in bytes */
565 target_phys_addr_t offset_mods
;
566 /* available slots for mb modules infos */
568 /* currently used slots of mb modules */
572 static uint32_t mb_add_cmdline(MultibootState
*s
, const char *cmdline
)
574 int len
= strlen(cmdline
) + 1;
575 target_phys_addr_t p
= s
->offset_cmdlines
;
577 pstrcpy((char *)s
->mb_buf
+ p
, len
, cmdline
);
578 s
->offset_cmdlines
+= len
;
579 return s
->mb_buf_phys
+ p
;
582 static void mb_add_mod(MultibootState
*s
,
583 target_phys_addr_t start
, target_phys_addr_t end
,
584 target_phys_addr_t cmdline_phys
)
587 assert(s
->mb_mods_count
< s
->mb_mods_avail
);
589 p
= (char *)s
->mb_buf
+ s
->offset_mbinfo
+ MB_MOD_SIZE
* s
->mb_mods_count
;
591 stl_p(p
+ MB_MOD_START
, start
);
592 stl_p(p
+ MB_MOD_END
, end
);
593 stl_p(p
+ MB_MOD_CMDLINE
, cmdline_phys
);
595 mb_debug("mod%02d: %08x - %08x\n", s
->mb_mods_count
, start
, end
);
600 static int load_multiboot(void *fw_cfg
,
602 const char *kernel_filename
,
603 const char *initrd_filename
,
604 const char *kernel_cmdline
,
607 int i
, is_multiboot
= 0;
609 uint32_t mh_entry_addr
;
610 uint32_t mh_load_addr
;
611 uint32_t mb_kernel_size
;
613 uint8_t bootinfo
[MBI_SIZE
];
614 uint8_t *mb_bootinfo_data
;
616 /* Ok, let's see if it is a multiboot image.
617 The header is 12x32bit long, so the latest entry may be 8192 - 48. */
618 for (i
= 0; i
< (8192 - 48); i
+= 4) {
619 if (ldl_p(header
+i
) == 0x1BADB002) {
620 uint32_t checksum
= ldl_p(header
+i
+8);
621 flags
= ldl_p(header
+i
+4);
623 checksum
+= (uint32_t)0x1BADB002;
632 return 0; /* no multiboot */
634 mb_debug("qemu: I believe we found a multiboot image!\n");
635 memset(bootinfo
, 0, sizeof(bootinfo
));
636 memset(&mbs
, 0, sizeof(mbs
));
638 if (flags
& 0x00000004) { /* MULTIBOOT_HEADER_HAS_VBE */
639 fprintf(stderr
, "qemu: multiboot knows VBE. we don't.\n");
641 if (!(flags
& 0x00010000)) { /* MULTIBOOT_HEADER_HAS_ADDR */
643 uint64_t elf_low
, elf_high
;
646 kernel_size
= load_elf(kernel_filename
, 0, &elf_entry
, &elf_low
, &elf_high
,
648 if (kernel_size
< 0) {
649 fprintf(stderr
, "Error while loading elf kernel\n");
652 mh_load_addr
= elf_low
;
653 mb_kernel_size
= elf_high
- elf_low
;
654 mh_entry_addr
= elf_entry
;
656 mbs
.mb_buf
= qemu_malloc(mb_kernel_size
);
657 if (rom_copy(mbs
.mb_buf
, mh_load_addr
, mb_kernel_size
) != mb_kernel_size
) {
658 fprintf(stderr
, "Error while fetching elf kernel from rom\n");
662 mb_debug("qemu: loading multiboot-elf kernel (%#x bytes) with entry %#zx\n",
663 mb_kernel_size
, (size_t)mh_entry_addr
);
665 /* Valid if mh_flags sets MULTIBOOT_HEADER_HAS_ADDR. */
666 uint32_t mh_header_addr
= ldl_p(header
+i
+12);
667 mh_load_addr
= ldl_p(header
+i
+16);
668 uint32_t mb_kernel_text_offset
= i
- (mh_header_addr
- mh_load_addr
);
670 mh_entry_addr
= ldl_p(header
+i
+28);
671 mb_kernel_size
= get_file_size(f
) - mb_kernel_text_offset
;
673 /* Valid if mh_flags sets MULTIBOOT_HEADER_HAS_VBE.
674 uint32_t mh_mode_type = ldl_p(header+i+32);
675 uint32_t mh_width = ldl_p(header+i+36);
676 uint32_t mh_height = ldl_p(header+i+40);
677 uint32_t mh_depth = ldl_p(header+i+44); */
679 mb_debug("multiboot: mh_header_addr = %#x\n", mh_header_addr
);
680 mb_debug("multiboot: mh_load_addr = %#x\n", mh_load_addr
);
681 mb_debug("multiboot: mh_load_end_addr = %#x\n", ldl_p(header
+i
+20));
682 mb_debug("multiboot: mh_bss_end_addr = %#x\n", ldl_p(header
+i
+24));
683 mb_debug("qemu: loading multiboot kernel (%#x bytes) at %#x\n",
684 mb_kernel_size
, mh_load_addr
);
686 mbs
.mb_buf
= qemu_malloc(mb_kernel_size
);
687 fseek(f
, mb_kernel_text_offset
, SEEK_SET
);
688 if (fread(mbs
.mb_buf
, 1, mb_kernel_size
, f
) != mb_kernel_size
) {
689 fprintf(stderr
, "fread() failed\n");
695 mbs
.mb_buf_phys
= mh_load_addr
;
697 mbs
.mb_buf_size
= TARGET_PAGE_ALIGN(mb_kernel_size
);
698 mbs
.offset_mbinfo
= mbs
.mb_buf_size
;
700 /* Calculate space for cmdlines and mb_mods */
701 mbs
.mb_buf_size
+= strlen(kernel_filename
) + 1;
702 mbs
.mb_buf_size
+= strlen(kernel_cmdline
) + 1;
703 if (initrd_filename
) {
704 const char *r
= initrd_filename
;
705 mbs
.mb_buf_size
+= strlen(r
) + 1;
706 mbs
.mb_mods_avail
= 1;
707 while ((r
= strchr(r
, ','))) {
711 mbs
.mb_buf_size
+= MB_MOD_SIZE
* mbs
.mb_mods_avail
;
714 mbs
.mb_buf_size
= TARGET_PAGE_ALIGN(mbs
.mb_buf_size
);
716 /* enlarge mb_buf to hold cmdlines and mb-info structs */
717 mbs
.mb_buf
= qemu_realloc(mbs
.mb_buf
, mbs
.mb_buf_size
);
718 mbs
.offset_cmdlines
= mbs
.offset_mbinfo
+ mbs
.mb_mods_avail
* MB_MOD_SIZE
;
720 if (initrd_filename
) {
723 mbs
.offset_mods
= mbs
.mb_buf_size
;
727 uint32_t mb_mod_length
;
728 uint32_t offs
= mbs
.mb_buf_size
;
730 next_initrd
= strchr(initrd_filename
, ',');
733 /* if a space comes after the module filename, treat everything
734 after that as parameters */
735 target_phys_addr_t c
= mb_add_cmdline(&mbs
, initrd_filename
);
736 if ((next_space
= strchr(initrd_filename
, ' ')))
738 mb_debug("multiboot loading module: %s\n", initrd_filename
);
739 mb_mod_length
= get_image_size(initrd_filename
);
740 if (mb_mod_length
< 0) {
741 fprintf(stderr
, "failed to get %s image size\n", initrd_filename
);
745 mbs
.mb_buf_size
= TARGET_PAGE_ALIGN(mb_mod_length
+ mbs
.mb_buf_size
);
746 mbs
.mb_buf
= qemu_realloc(mbs
.mb_buf
, mbs
.mb_buf_size
);
748 load_image(initrd_filename
, (unsigned char *)mbs
.mb_buf
+ offs
);
749 mb_add_mod(&mbs
, mbs
.mb_buf_phys
+ offs
,
750 mbs
.mb_buf_phys
+ offs
+ mb_mod_length
, c
);
752 mb_debug("mod_start: %p\nmod_end: %p\n cmdline: %#x\n",
753 (char *)mbs
.mb_buf
+ offs
,
754 (char *)mbs
.mb_buf
+ offs
+ mb_mod_length
, c
);
755 initrd_filename
= next_initrd
+1;
756 } while (next_initrd
);
759 /* Commandline support */
760 char kcmdline
[strlen(kernel_filename
) + strlen(kernel_cmdline
) + 2];
761 snprintf(kcmdline
, sizeof(kcmdline
), "%s %s",
762 kernel_filename
, kernel_cmdline
);
763 stl_p(bootinfo
+ MBI_CMDLINE
, mb_add_cmdline(&mbs
, kcmdline
));
765 stl_p(bootinfo
+ MBI_MODS_ADDR
, mbs
.mb_buf_phys
+ mbs
.offset_mbinfo
);
766 stl_p(bootinfo
+ MBI_MODS_COUNT
, mbs
.mb_mods_count
); /* mods_count */
768 /* the kernel is where we want it to be now */
769 stl_p(bootinfo
+ MBI_FLAGS
, MULTIBOOT_FLAGS_MEMORY
770 | MULTIBOOT_FLAGS_BOOT_DEVICE
771 | MULTIBOOT_FLAGS_CMDLINE
772 | MULTIBOOT_FLAGS_MODULES
773 | MULTIBOOT_FLAGS_MMAP
);
774 stl_p(bootinfo
+ MBI_MEM_LOWER
, 640);
775 stl_p(bootinfo
+ MBI_MEM_UPPER
, ram_size
/ 1024);
776 stl_p(bootinfo
+ MBI_BOOT_DEVICE
, 0x8001ffff); /* XXX: use the -boot switch? */
777 stl_p(bootinfo
+ MBI_MMAP_ADDR
, ADDR_E820_MAP
);
779 mb_debug("multiboot: mh_entry_addr = %#x\n", mh_entry_addr
);
780 mb_debug(" mb_buf_phys = %x\n", mbs
.mb_buf_phys
);
781 mb_debug(" mod_start = %x\n", mbs
.mb_buf_phys
+ mbs
.offset_mods
);
782 mb_debug(" mb_mods_count = %d\n", mbs
.mb_mods_count
);
784 /* save bootinfo off the stack */
785 mb_bootinfo_data
= qemu_malloc(sizeof(bootinfo
));
786 memcpy(mb_bootinfo_data
, bootinfo
, sizeof(bootinfo
));
788 /* Pass variables to option rom */
789 fw_cfg_add_i32(fw_cfg
, FW_CFG_KERNEL_ENTRY
, mh_entry_addr
);
790 fw_cfg_add_i32(fw_cfg
, FW_CFG_KERNEL_ADDR
, mh_load_addr
);
791 fw_cfg_add_i32(fw_cfg
, FW_CFG_KERNEL_SIZE
, mbs
.mb_buf_size
);
792 fw_cfg_add_bytes(fw_cfg
, FW_CFG_KERNEL_DATA
,
793 mbs
.mb_buf
, mbs
.mb_buf_size
);
795 fw_cfg_add_i32(fw_cfg
, FW_CFG_INITRD_ADDR
, ADDR_MBI
);
796 fw_cfg_add_i32(fw_cfg
, FW_CFG_INITRD_SIZE
, sizeof(bootinfo
));
797 fw_cfg_add_bytes(fw_cfg
, FW_CFG_INITRD_DATA
, mb_bootinfo_data
,
800 option_rom
[nb_option_roms
] = "multiboot.bin";
803 return 1; /* yes, we are multiboot */
806 static void load_linux(void *fw_cfg
,
807 const char *kernel_filename
,
808 const char *initrd_filename
,
809 const char *kernel_cmdline
,
810 target_phys_addr_t max_ram_size
)
813 int setup_size
, kernel_size
, initrd_size
= 0, cmdline_size
;
815 uint8_t header
[8192], *setup
, *kernel
, *initrd_data
;
816 target_phys_addr_t real_addr
, prot_addr
, cmdline_addr
, initrd_addr
= 0;
820 /* Align to 16 bytes as a paranoia measure */
821 cmdline_size
= (strlen(kernel_cmdline
)+16) & ~15;
823 /* load the kernel header */
824 f
= fopen(kernel_filename
, "rb");
825 if (!f
|| !(kernel_size
= get_file_size(f
)) ||
826 fread(header
, 1, MIN(ARRAY_SIZE(header
), kernel_size
), f
) !=
827 MIN(ARRAY_SIZE(header
), kernel_size
)) {
828 fprintf(stderr
, "qemu: could not load kernel '%s': %s\n",
829 kernel_filename
, strerror(errno
));
833 /* kernel protocol version */
835 fprintf(stderr
, "header magic: %#x\n", ldl_p(header
+0x202));
837 if (ldl_p(header
+0x202) == 0x53726448)
838 protocol
= lduw_p(header
+0x206);
840 /* This looks like a multiboot kernel. If it is, let's stop
841 treating it like a Linux kernel. */
842 if (load_multiboot(fw_cfg
, f
, kernel_filename
,
843 initrd_filename
, kernel_cmdline
, header
))
848 if (protocol
< 0x200 || !(header
[0x211] & 0x01)) {
851 cmdline_addr
= 0x9a000 - cmdline_size
;
853 } else if (protocol
< 0x202) {
854 /* High but ancient kernel */
856 cmdline_addr
= 0x9a000 - cmdline_size
;
857 prot_addr
= 0x100000;
859 /* High and recent kernel */
861 cmdline_addr
= 0x20000;
862 prot_addr
= 0x100000;
867 "qemu: real_addr = 0x" TARGET_FMT_plx
"\n"
868 "qemu: cmdline_addr = 0x" TARGET_FMT_plx
"\n"
869 "qemu: prot_addr = 0x" TARGET_FMT_plx
"\n",
875 /* highest address for loading the initrd */
876 if (protocol
>= 0x203)
877 initrd_max
= ldl_p(header
+0x22c);
879 initrd_max
= 0x37ffffff;
881 if (initrd_max
>= max_ram_size
-ACPI_DATA_SIZE
)
882 initrd_max
= max_ram_size
-ACPI_DATA_SIZE
-1;
884 fw_cfg_add_i32(fw_cfg
, FW_CFG_CMDLINE_ADDR
, cmdline_addr
);
885 fw_cfg_add_i32(fw_cfg
, FW_CFG_CMDLINE_SIZE
, strlen(kernel_cmdline
)+1);
886 fw_cfg_add_bytes(fw_cfg
, FW_CFG_CMDLINE_DATA
,
887 (uint8_t*)strdup(kernel_cmdline
),
888 strlen(kernel_cmdline
)+1);
890 if (protocol
>= 0x202) {
891 stl_p(header
+0x228, cmdline_addr
);
893 stw_p(header
+0x20, 0xA33F);
894 stw_p(header
+0x22, cmdline_addr
-real_addr
);
897 /* handle vga= parameter */
898 vmode
= strstr(kernel_cmdline
, "vga=");
900 unsigned int video_mode
;
903 if (!strncmp(vmode
, "normal", 6)) {
905 } else if (!strncmp(vmode
, "ext", 3)) {
907 } else if (!strncmp(vmode
, "ask", 3)) {
910 video_mode
= strtol(vmode
, NULL
, 0);
912 stw_p(header
+0x1fa, video_mode
);
916 /* High nybble = B reserved for Qemu; low nybble is revision number.
917 If this code is substantially changed, you may want to consider
918 incrementing the revision. */
919 if (protocol
>= 0x200)
920 header
[0x210] = 0xB0;
923 if (protocol
>= 0x201) {
924 header
[0x211] |= 0x80; /* CAN_USE_HEAP */
925 stw_p(header
+0x224, cmdline_addr
-real_addr
-0x200);
929 if (initrd_filename
) {
930 if (protocol
< 0x200) {
931 fprintf(stderr
, "qemu: linux kernel too old to load a ram disk\n");
935 initrd_size
= get_image_size(initrd_filename
);
936 initrd_addr
= (initrd_max
-initrd_size
) & ~4095;
938 initrd_data
= qemu_malloc(initrd_size
);
939 load_image(initrd_filename
, initrd_data
);
941 fw_cfg_add_i32(fw_cfg
, FW_CFG_INITRD_ADDR
, initrd_addr
);
942 fw_cfg_add_i32(fw_cfg
, FW_CFG_INITRD_SIZE
, initrd_size
);
943 fw_cfg_add_bytes(fw_cfg
, FW_CFG_INITRD_DATA
, initrd_data
, initrd_size
);
945 stl_p(header
+0x218, initrd_addr
);
946 stl_p(header
+0x21c, initrd_size
);
949 /* load kernel and setup */
950 setup_size
= header
[0x1f1];
953 setup_size
= (setup_size
+1)*512;
954 kernel_size
-= setup_size
;
956 setup
= qemu_malloc(setup_size
);
957 kernel
= qemu_malloc(kernel_size
);
958 fseek(f
, 0, SEEK_SET
);
959 if (fread(setup
, 1, setup_size
, f
) != setup_size
) {
960 fprintf(stderr
, "fread() failed\n");
963 if (fread(kernel
, 1, kernel_size
, f
) != kernel_size
) {
964 fprintf(stderr
, "fread() failed\n");
968 memcpy(setup
, header
, MIN(sizeof(header
), setup_size
));
970 fw_cfg_add_i32(fw_cfg
, FW_CFG_KERNEL_ADDR
, prot_addr
);
971 fw_cfg_add_i32(fw_cfg
, FW_CFG_KERNEL_SIZE
, kernel_size
);
972 fw_cfg_add_bytes(fw_cfg
, FW_CFG_KERNEL_DATA
, kernel
, kernel_size
);
974 fw_cfg_add_i32(fw_cfg
, FW_CFG_SETUP_ADDR
, real_addr
);
975 fw_cfg_add_i32(fw_cfg
, FW_CFG_SETUP_SIZE
, setup_size
);
976 fw_cfg_add_bytes(fw_cfg
, FW_CFG_SETUP_DATA
, setup
, setup_size
);
978 option_rom
[nb_option_roms
] = "linuxboot.bin";
982 static const int ide_iobase
[2] = { 0x1f0, 0x170 };
983 static const int ide_iobase2
[2] = { 0x3f6, 0x376 };
984 static const int ide_irq
[2] = { 14, 15 };
986 #define NE2000_NB_MAX 6
988 static const int ne2000_io
[NE2000_NB_MAX
] = { 0x300, 0x320, 0x340, 0x360,
990 static const int ne2000_irq
[NE2000_NB_MAX
] = { 9, 10, 11, 3, 4, 5 };
992 static const int parallel_io
[MAX_PARALLEL_PORTS
] = { 0x378, 0x278, 0x3bc };
993 static const int parallel_irq
[MAX_PARALLEL_PORTS
] = { 7, 7, 7 };
996 static void audio_init (PCIBus
*pci_bus
, qemu_irq
*pic
)
1000 for (c
= soundhw
; c
->name
; ++c
) {
1003 c
->init
.init_isa(pic
);
1006 c
->init
.init_pci(pci_bus
);
1014 static void pc_init_ne2k_isa(NICInfo
*nd
)
1016 static int nb_ne2k
= 0;
1018 if (nb_ne2k
== NE2000_NB_MAX
)
1020 isa_ne2000_init(ne2000_io
[nb_ne2k
],
1021 ne2000_irq
[nb_ne2k
], nd
);
1025 int cpu_is_bsp(CPUState
*env
)
1027 return env
->cpuid_apic_id
== 0;
1030 static CPUState
*pc_new_cpu(const char *cpu_model
)
1034 env
= cpu_init(cpu_model
);
1036 fprintf(stderr
, "Unable to find x86 CPU definition\n");
1039 if ((env
->cpuid_features
& CPUID_APIC
) || smp_cpus
> 1) {
1040 env
->cpuid_apic_id
= env
->cpu_index
;
1041 /* APIC reset callback resets cpu */
1044 qemu_register_reset((QEMUResetHandler
*)cpu_reset
, env
);
1049 /* PC hardware initialisation */
1050 static void pc_init1(ram_addr_t ram_size
,
1051 const char *boot_device
,
1052 const char *kernel_filename
,
1053 const char *kernel_cmdline
,
1054 const char *initrd_filename
,
1055 const char *cpu_model
,
1059 int ret
, linux_boot
, i
;
1060 ram_addr_t ram_addr
, bios_offset
, option_rom_offset
;
1061 ram_addr_t below_4g_mem_size
, above_4g_mem_size
= 0;
1062 int bios_size
, isa_bios_size
;
1065 int piix3_devfn
= -1;
1070 IsaIrqState
*isa_irq_state
;
1071 DriveInfo
*hd
[MAX_IDE_BUS
* MAX_IDE_DEVS
];
1072 DriveInfo
*fd
[MAX_FD
];
1075 if (ram_size
>= 0xe0000000 ) {
1076 above_4g_mem_size
= ram_size
- 0xe0000000;
1077 below_4g_mem_size
= 0xe0000000;
1079 below_4g_mem_size
= ram_size
;
1082 linux_boot
= (kernel_filename
!= NULL
);
1085 if (cpu_model
== NULL
) {
1086 #ifdef TARGET_X86_64
1087 cpu_model
= "qemu64";
1089 cpu_model
= "qemu32";
1093 for (i
= 0; i
< smp_cpus
; i
++) {
1094 env
= pc_new_cpu(cpu_model
);
1100 ram_addr
= qemu_ram_alloc(0xa0000);
1101 cpu_register_physical_memory(0, 0xa0000, ram_addr
);
1103 /* Allocate, even though we won't register, so we don't break the
1104 * phys_ram_base + PA assumption. This range includes vga (0xa0000 - 0xc0000),
1105 * and some bios areas, which will be registered later
1107 ram_addr
= qemu_ram_alloc(0x100000 - 0xa0000);
1108 ram_addr
= qemu_ram_alloc(below_4g_mem_size
- 0x100000);
1109 cpu_register_physical_memory(0x100000,
1110 below_4g_mem_size
- 0x100000,
1113 /* above 4giga memory allocation */
1114 if (above_4g_mem_size
> 0) {
1115 #if TARGET_PHYS_ADDR_BITS == 32
1116 hw_error("To much RAM for 32-bit physical address");
1118 ram_addr
= qemu_ram_alloc(above_4g_mem_size
);
1119 cpu_register_physical_memory(0x100000000ULL
,
1127 if (bios_name
== NULL
)
1128 bios_name
= BIOS_FILENAME
;
1129 filename
= qemu_find_file(QEMU_FILE_TYPE_BIOS
, bios_name
);
1131 bios_size
= get_image_size(filename
);
1135 if (bios_size
<= 0 ||
1136 (bios_size
% 65536) != 0) {
1139 bios_offset
= qemu_ram_alloc(bios_size
);
1140 ret
= rom_add_file_fixed(bios_name
, (uint32_t)(-bios_size
));
1143 fprintf(stderr
, "qemu: could not load PC BIOS '%s'\n", bios_name
);
1147 qemu_free(filename
);
1149 /* map the last 128KB of the BIOS in ISA space */
1150 isa_bios_size
= bios_size
;
1151 if (isa_bios_size
> (128 * 1024))
1152 isa_bios_size
= 128 * 1024;
1153 cpu_register_physical_memory(0x100000 - isa_bios_size
,
1155 (bios_offset
+ bios_size
- isa_bios_size
) | IO_MEM_ROM
);
1159 rom_enable_driver_roms
= 1;
1160 option_rom_offset
= qemu_ram_alloc(PC_ROM_SIZE
);
1161 cpu_register_physical_memory(PC_ROM_MIN_VGA
, PC_ROM_SIZE
, option_rom_offset
);
1163 /* map all the bios at the top of memory */
1164 cpu_register_physical_memory((uint32_t)(-bios_size
),
1165 bios_size
, bios_offset
| IO_MEM_ROM
);
1167 fw_cfg
= bochs_bios_init();
1170 load_linux(fw_cfg
, kernel_filename
, initrd_filename
, kernel_cmdline
, below_4g_mem_size
);
1173 for (i
= 0; i
< nb_option_roms
; i
++) {
1174 rom_add_option(option_rom
[i
]);
1177 cpu_irq
= qemu_allocate_irqs(pic_irq_request
, NULL
, 1);
1178 i8259
= i8259_init(cpu_irq
[0]);
1179 isa_irq_state
= qemu_mallocz(sizeof(*isa_irq_state
));
1180 isa_irq_state
->i8259
= i8259
;
1181 isa_irq
= qemu_allocate_irqs(isa_irq_handler
, isa_irq_state
, 24);
1184 pci_bus
= i440fx_init(&i440fx_state
, &piix3_devfn
, isa_irq
);
1189 isa_bus_irqs(isa_irq
);
1191 ferr_irq
= isa_reserve_irq(13);
1193 /* init basic PC hardware */
1194 register_ioport_write(0x80, 1, 1, ioport80_write
, NULL
);
1196 register_ioport_write(0xf0, 1, 1, ioportF0_write
, NULL
);
1198 if (cirrus_vga_enabled
) {
1200 pci_cirrus_vga_init(pci_bus
);
1202 isa_cirrus_vga_init();
1204 } else if (vmsvga_enabled
) {
1206 pci_vmsvga_init(pci_bus
);
1208 fprintf(stderr
, "%s: vmware_vga: no PCI bus\n", __FUNCTION__
);
1209 } else if (std_vga_enabled
) {
1211 pci_vga_init(pci_bus
, 0, 0);
1217 rtc_state
= rtc_init(2000);
1219 qemu_register_boot_set(pc_boot_set
, rtc_state
);
1221 register_ioport_read(0x92, 1, 1, ioport92_read
, NULL
);
1222 register_ioport_write(0x92, 1, 1, ioport92_write
, NULL
);
1225 isa_irq_state
->ioapic
= ioapic_init();
1227 pit
= pit_init(0x40, isa_reserve_irq(0));
1233 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
1234 if (serial_hds
[i
]) {
1235 serial_isa_init(i
, serial_hds
[i
]);
1239 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
1240 if (parallel_hds
[i
]) {
1241 parallel_init(i
, parallel_hds
[i
]);
1245 for(i
= 0; i
< nb_nics
; i
++) {
1246 NICInfo
*nd
= &nd_table
[i
];
1248 if (!pci_enabled
|| (nd
->model
&& strcmp(nd
->model
, "ne2k_isa") == 0))
1249 pc_init_ne2k_isa(nd
);
1251 pci_nic_init_nofail(nd
, "e1000", NULL
);
1254 if (drive_get_max_bus(IF_IDE
) >= MAX_IDE_BUS
) {
1255 fprintf(stderr
, "qemu: too many IDE bus\n");
1259 for(i
= 0; i
< MAX_IDE_BUS
* MAX_IDE_DEVS
; i
++) {
1260 hd
[i
] = drive_get(IF_IDE
, i
/ MAX_IDE_DEVS
, i
% MAX_IDE_DEVS
);
1264 pci_piix3_ide_init(pci_bus
, hd
, piix3_devfn
+ 1);
1266 for(i
= 0; i
< MAX_IDE_BUS
; i
++) {
1267 isa_ide_init(ide_iobase
[i
], ide_iobase2
[i
], ide_irq
[i
],
1268 hd
[MAX_IDE_DEVS
* i
], hd
[MAX_IDE_DEVS
* i
+ 1]);
1272 isa_dev
= isa_create_simple("i8042");
1275 audio_init(pci_enabled
? pci_bus
: NULL
, isa_irq
);
1278 for(i
= 0; i
< MAX_FD
; i
++) {
1279 fd
[i
] = drive_get(IF_FLOPPY
, 0, i
);
1281 floppy_controller
= fdctrl_init_isa(fd
);
1283 cmos_init(below_4g_mem_size
, above_4g_mem_size
, boot_device
, hd
);
1285 if (pci_enabled
&& usb_enabled
) {
1286 usb_uhci_piix3_init(pci_bus
, piix3_devfn
+ 2);
1289 if (pci_enabled
&& acpi_enabled
) {
1290 uint8_t *eeprom_buf
= qemu_mallocz(8 * 256); /* XXX: make this persistent */
1293 /* TODO: Populate SPD eeprom data. */
1294 smbus
= piix4_pm_init(pci_bus
, piix3_devfn
+ 3, 0xb100,
1295 isa_reserve_irq(9));
1296 for (i
= 0; i
< 8; i
++) {
1297 DeviceState
*eeprom
;
1298 eeprom
= qdev_create((BusState
*)smbus
, "smbus-eeprom");
1299 qdev_prop_set_uint8(eeprom
, "address", 0x50 + i
);
1300 qdev_prop_set_ptr(eeprom
, "data", eeprom_buf
+ (i
* 256));
1301 qdev_init_nofail(eeprom
);
1303 piix4_acpi_system_hot_add_init(pci_bus
);
1307 i440fx_init_memory_mappings(i440fx_state
);
1314 max_bus
= drive_get_max_bus(IF_SCSI
);
1315 for (bus
= 0; bus
<= max_bus
; bus
++) {
1316 pci_create_simple(pci_bus
, -1, "lsi53c895a");
1320 /* Add virtio console devices */
1322 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++) {
1323 if (virtcon_hds
[i
]) {
1324 pci_create_simple(pci_bus
, -1, "virtio-console-pci");
1329 rom_load_fw(fw_cfg
);
1332 static void pc_init_pci(ram_addr_t ram_size
,
1333 const char *boot_device
,
1334 const char *kernel_filename
,
1335 const char *kernel_cmdline
,
1336 const char *initrd_filename
,
1337 const char *cpu_model
)
1339 pc_init1(ram_size
, boot_device
,
1340 kernel_filename
, kernel_cmdline
,
1341 initrd_filename
, cpu_model
, 1);
1344 static void pc_init_isa(ram_addr_t ram_size
,
1345 const char *boot_device
,
1346 const char *kernel_filename
,
1347 const char *kernel_cmdline
,
1348 const char *initrd_filename
,
1349 const char *cpu_model
)
1351 if (cpu_model
== NULL
)
1353 pc_init1(ram_size
, boot_device
,
1354 kernel_filename
, kernel_cmdline
,
1355 initrd_filename
, cpu_model
, 0);
1358 /* set CMOS shutdown status register (index 0xF) as S3_resume(0xFE)
1359 BIOS will read it and start S3 resume at POST Entry */
1360 void cmos_set_s3_resume(void)
1363 rtc_set_memory(rtc_state
, 0xF, 0xFE);
1366 static QEMUMachine pc_machine
= {
1369 .desc
= "Standard PC",
1370 .init
= pc_init_pci
,
1375 static QEMUMachine pc_machine_v0_10
= {
1377 .desc
= "Standard PC, qemu 0.10",
1378 .init
= pc_init_pci
,
1380 .compat_props
= (GlobalProperty
[]) {
1382 .driver
= "virtio-blk-pci",
1383 .property
= "class",
1384 .value
= stringify(PCI_CLASS_STORAGE_OTHER
),
1386 .driver
= "virtio-console-pci",
1387 .property
= "class",
1388 .value
= stringify(PCI_CLASS_DISPLAY_OTHER
),
1390 .driver
= "virtio-net-pci",
1391 .property
= "vectors",
1392 .value
= stringify(0),
1394 .driver
= "virtio-blk-pci",
1395 .property
= "vectors",
1396 .value
= stringify(0),
1398 { /* end of list */ }
1402 static QEMUMachine isapc_machine
= {
1404 .desc
= "ISA-only PC",
1405 .init
= pc_init_isa
,
1409 static void pc_machine_init(void)
1411 qemu_register_machine(&pc_machine
);
1412 qemu_register_machine(&pc_machine_v0_10
);
1413 qemu_register_machine(&isapc_machine
);
1416 machine_init(pc_machine_init
);