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
26 /* output Bochs bios info messages */
29 #define BIOS_FILENAME "bios.bin"
30 #define VGABIOS_FILENAME "vgabios.bin"
31 #define VGABIOS_CIRRUS_FILENAME "vgabios-cirrus.bin"
32 #define LINUX_BOOT_FILENAME "linux_boot.bin"
34 #define KERNEL_LOAD_ADDR 0x00100000
35 #define INITRD_LOAD_ADDR 0x00600000
36 #define KERNEL_PARAMS_ADDR 0x00090000
37 #define KERNEL_CMDLINE_ADDR 0x00099000
39 static fdctrl_t
*floppy_controller
;
40 static RTCState
*rtc_state
;
42 static IOAPICState
*ioapic
;
43 static PCIDevice
*i440fx_state
;
45 static void ioport80_write(void *opaque
, uint32_t addr
, uint32_t data
)
49 /* MSDOS compatibility mode FPU exception support */
50 /* XXX: add IGNNE support */
51 void cpu_set_ferr(CPUX86State
*s
)
56 static void ioportF0_write(void *opaque
, uint32_t addr
, uint32_t data
)
62 uint64_t cpu_get_tsc(CPUX86State
*env
)
64 /* Note: when using kqemu, it is more logical to return the host TSC
65 because kqemu does not trap the RDTSC instruction for
66 performance reasons */
68 if (env
->kqemu_enabled
) {
69 return cpu_get_real_ticks();
73 return cpu_get_ticks();
78 void cpu_smm_update(CPUState
*env
)
80 if (i440fx_state
&& env
== first_cpu
)
81 i440fx_set_smm(i440fx_state
, (env
->hflags
>> HF_SMM_SHIFT
) & 1);
86 int cpu_get_pic_interrupt(CPUState
*env
)
90 intno
= apic_get_interrupt(env
);
92 /* set irq request if a PIC irq is still pending */
93 /* XXX: improve that */
94 pic_update_irq(isa_pic
);
97 /* read the irq from the PIC */
98 intno
= pic_read_irq(isa_pic
);
102 static void pic_irq_request(void *opaque
, int level
)
104 CPUState
*env
= opaque
;
106 cpu_interrupt(env
, CPU_INTERRUPT_HARD
);
108 cpu_reset_interrupt(env
, CPU_INTERRUPT_HARD
);
111 /* PC cmos mappings */
113 #define REG_EQUIPMENT_BYTE 0x14
114 #define REG_IBM_CENTURY_BYTE 0x32
115 #define REG_IBM_PS2_CENTURY_BYTE 0x37
118 static inline int to_bcd(RTCState
*s
, int a
)
120 return ((a
/ 10) << 4) | (a
% 10);
123 static int cmos_get_fd_drive_type(int fd0
)
129 /* 1.44 Mb 3"5 drive */
133 /* 2.88 Mb 3"5 drive */
137 /* 1.2 Mb 5"5 drive */
147 static void cmos_init_hd(int type_ofs
, int info_ofs
, BlockDriverState
*hd
)
149 RTCState
*s
= rtc_state
;
150 int cylinders
, heads
, sectors
;
151 bdrv_get_geometry_hint(hd
, &cylinders
, &heads
, §ors
);
152 rtc_set_memory(s
, type_ofs
, 47);
153 rtc_set_memory(s
, info_ofs
, cylinders
);
154 rtc_set_memory(s
, info_ofs
+ 1, cylinders
>> 8);
155 rtc_set_memory(s
, info_ofs
+ 2, heads
);
156 rtc_set_memory(s
, info_ofs
+ 3, 0xff);
157 rtc_set_memory(s
, info_ofs
+ 4, 0xff);
158 rtc_set_memory(s
, info_ofs
+ 5, 0xc0 | ((heads
> 8) << 3));
159 rtc_set_memory(s
, info_ofs
+ 6, cylinders
);
160 rtc_set_memory(s
, info_ofs
+ 7, cylinders
>> 8);
161 rtc_set_memory(s
, info_ofs
+ 8, sectors
);
164 /* hd_table must contain 4 block drivers */
165 static void cmos_init(int ram_size
, int boot_device
, BlockDriverState
**hd_table
)
167 RTCState
*s
= rtc_state
;
174 /* set the CMOS date */
182 val
= to_bcd(s
, (tm
->tm_year
/ 100) + 19);
183 rtc_set_memory(s
, REG_IBM_CENTURY_BYTE
, val
);
184 rtc_set_memory(s
, REG_IBM_PS2_CENTURY_BYTE
, val
);
186 /* various important CMOS locations needed by PC/Bochs bios */
189 val
= 640; /* base memory in K */
190 rtc_set_memory(s
, 0x15, val
);
191 rtc_set_memory(s
, 0x16, val
>> 8);
193 val
= (ram_size
/ 1024) - 1024;
196 rtc_set_memory(s
, 0x17, val
);
197 rtc_set_memory(s
, 0x18, val
>> 8);
198 rtc_set_memory(s
, 0x30, val
);
199 rtc_set_memory(s
, 0x31, val
>> 8);
201 if (ram_size
> (16 * 1024 * 1024))
202 val
= (ram_size
/ 65536) - ((16 * 1024 * 1024) / 65536);
207 rtc_set_memory(s
, 0x34, val
);
208 rtc_set_memory(s
, 0x35, val
>> 8);
210 switch(boot_device
) {
213 rtc_set_memory(s
, 0x3d, 0x01); /* floppy boot */
215 rtc_set_memory(s
, 0x38, 0x01); /* disable signature check */
219 rtc_set_memory(s
, 0x3d, 0x02); /* hard drive boot */
222 rtc_set_memory(s
, 0x3d, 0x03); /* CD-ROM boot */
228 fd0
= fdctrl_get_drive_type(floppy_controller
, 0);
229 fd1
= fdctrl_get_drive_type(floppy_controller
, 1);
231 val
= (cmos_get_fd_drive_type(fd0
) << 4) | cmos_get_fd_drive_type(fd1
);
232 rtc_set_memory(s
, 0x10, val
);
244 val
|= 0x01; /* 1 drive, ready for boot */
247 val
|= 0x41; /* 2 drives, ready for boot */
250 val
|= 0x02; /* FPU is there */
251 val
|= 0x04; /* PS/2 mouse installed */
252 rtc_set_memory(s
, REG_EQUIPMENT_BYTE
, val
);
256 rtc_set_memory(s
, 0x12, (hd_table
[0] ? 0xf0 : 0) | (hd_table
[1] ? 0x0f : 0));
258 cmos_init_hd(0x19, 0x1b, hd_table
[0]);
260 cmos_init_hd(0x1a, 0x24, hd_table
[1]);
263 for (i
= 0; i
< 4; i
++) {
265 int cylinders
, heads
, sectors
, translation
;
266 /* NOTE: bdrv_get_geometry_hint() returns the physical
267 geometry. It is always such that: 1 <= sects <= 63, 1
268 <= heads <= 16, 1 <= cylinders <= 16383. The BIOS
269 geometry can be different if a translation is done. */
270 translation
= bdrv_get_translation_hint(hd_table
[i
]);
271 if (translation
== BIOS_ATA_TRANSLATION_AUTO
) {
272 bdrv_get_geometry_hint(hd_table
[i
], &cylinders
, &heads
, §ors
);
273 if (cylinders
<= 1024 && heads
<= 16 && sectors
<= 63) {
274 /* No translation. */
277 /* LBA translation. */
283 val
|= translation
<< (i
* 2);
286 rtc_set_memory(s
, 0x39, val
);
289 void ioport_set_a20(int enable
)
291 /* XXX: send to all CPUs ? */
292 cpu_x86_set_a20(first_cpu
, enable
);
295 int ioport_get_a20(void)
297 return ((first_cpu
->a20_mask
>> 20) & 1);
300 static void ioport92_write(void *opaque
, uint32_t addr
, uint32_t val
)
302 ioport_set_a20((val
>> 1) & 1);
303 /* XXX: bit 0 is fast reset */
306 static uint32_t ioport92_read(void *opaque
, uint32_t addr
)
308 return ioport_get_a20() << 1;
311 /***********************************************************/
312 /* Bochs BIOS debug ports */
314 void bochs_bios_write(void *opaque
, uint32_t addr
, uint32_t val
)
316 static const char shutdown_str
[8] = "Shutdown";
317 static int shutdown_index
= 0;
320 /* Bochs BIOS messages */
323 fprintf(stderr
, "BIOS panic at rombios.c, line %d\n", val
);
328 fprintf(stderr
, "%c", val
);
332 /* same as Bochs power off */
333 if (val
== shutdown_str
[shutdown_index
]) {
335 if (shutdown_index
== 8) {
337 qemu_system_shutdown_request();
344 /* LGPL'ed VGA BIOS messages */
347 fprintf(stderr
, "VGA BIOS panic, line %d\n", val
);
352 fprintf(stderr
, "%c", val
);
358 void bochs_bios_init(void)
360 register_ioport_write(0x400, 1, 2, bochs_bios_write
, NULL
);
361 register_ioport_write(0x401, 1, 2, bochs_bios_write
, NULL
);
362 register_ioport_write(0x402, 1, 1, bochs_bios_write
, NULL
);
363 register_ioport_write(0x403, 1, 1, bochs_bios_write
, NULL
);
364 register_ioport_write(0x8900, 1, 1, bochs_bios_write
, NULL
);
366 register_ioport_write(0x501, 1, 2, bochs_bios_write
, NULL
);
367 register_ioport_write(0x502, 1, 2, bochs_bios_write
, NULL
);
368 register_ioport_write(0x500, 1, 1, bochs_bios_write
, NULL
);
369 register_ioport_write(0x503, 1, 1, bochs_bios_write
, NULL
);
373 int load_kernel(const char *filename
, uint8_t *addr
,
379 fd
= open(filename
, O_RDONLY
| O_BINARY
);
383 /* load 16 bit code */
384 if (read(fd
, real_addr
, 512) != 512)
386 setup_sects
= real_addr
[0x1F1];
389 if (read(fd
, real_addr
+ 512, setup_sects
* 512) !=
393 /* load 32 bit code */
394 size
= read(fd
, addr
, 16 * 1024 * 1024);
404 static void main_cpu_reset(void *opaque
)
406 CPUState
*env
= opaque
;
410 static const int ide_iobase
[2] = { 0x1f0, 0x170 };
411 static const int ide_iobase2
[2] = { 0x3f6, 0x376 };
412 static const int ide_irq
[2] = { 14, 15 };
414 #define NE2000_NB_MAX 6
416 static int ne2000_io
[NE2000_NB_MAX
] = { 0x300, 0x320, 0x340, 0x360, 0x280, 0x380 };
417 static int ne2000_irq
[NE2000_NB_MAX
] = { 9, 10, 11, 3, 4, 5 };
419 static int serial_io
[MAX_SERIAL_PORTS
] = { 0x3f8, 0x2f8, 0x3e8, 0x2e8 };
420 static int serial_irq
[MAX_SERIAL_PORTS
] = { 4, 3, 4, 3 };
422 static int parallel_io
[MAX_PARALLEL_PORTS
] = { 0x378, 0x278, 0x3bc };
423 static int parallel_irq
[MAX_PARALLEL_PORTS
] = { 7, 7, 7 };
426 static void audio_init (PCIBus
*pci_bus
)
429 int audio_enabled
= 0;
431 for (c
= soundhw
; !audio_enabled
&& c
->name
; ++c
) {
432 audio_enabled
= c
->enabled
;
440 for (c
= soundhw
; c
->name
; ++c
) {
443 c
->init
.init_isa (s
);
447 c
->init
.init_pci (pci_bus
, s
);
457 static void pc_init_ne2k_isa(NICInfo
*nd
)
459 static int nb_ne2k
= 0;
461 if (nb_ne2k
== NE2000_NB_MAX
)
463 isa_ne2000_init(ne2000_io
[nb_ne2k
], ne2000_irq
[nb_ne2k
], nd
);
467 /* PC hardware initialisation */
468 static void pc_init1(int ram_size
, int vga_ram_size
, int boot_device
,
469 DisplayState
*ds
, const char **fd_filename
, int snapshot
,
470 const char *kernel_filename
, const char *kernel_cmdline
,
471 const char *initrd_filename
,
475 int ret
, linux_boot
, initrd_size
, i
;
476 unsigned long bios_offset
, vga_bios_offset
;
477 int bios_size
, isa_bios_size
;
479 int piix3_devfn
= -1;
483 linux_boot
= (kernel_filename
!= NULL
);
486 for(i
= 0; i
< smp_cpus
; i
++) {
489 env
->hflags
|= HF_HALTED_MASK
;
491 /* XXX: enable it in all cases */
492 env
->cpuid_features
|= CPUID_APIC
;
494 register_savevm("cpu", i
, 4, cpu_save
, cpu_load
, env
);
495 qemu_register_reset(main_cpu_reset
, env
);
502 cpu_register_physical_memory(0, ram_size
, 0);
505 bios_offset
= ram_size
+ vga_ram_size
;
506 vga_bios_offset
= bios_offset
+ 256 * 1024;
508 snprintf(buf
, sizeof(buf
), "%s/%s", bios_dir
, BIOS_FILENAME
);
509 bios_size
= get_image_size(buf
);
510 if (bios_size
<= 0 ||
511 (bios_size
% 65536) != 0 ||
512 bios_size
> (256 * 1024)) {
515 ret
= load_image(buf
, phys_ram_base
+ bios_offset
);
516 if (ret
!= bios_size
) {
518 fprintf(stderr
, "qemu: could not load PC bios '%s'\n", buf
);
523 if (cirrus_vga_enabled
) {
524 snprintf(buf
, sizeof(buf
), "%s/%s", bios_dir
, VGABIOS_CIRRUS_FILENAME
);
526 snprintf(buf
, sizeof(buf
), "%s/%s", bios_dir
, VGABIOS_FILENAME
);
528 ret
= load_image(buf
, phys_ram_base
+ vga_bios_offset
);
530 /* setup basic memory access */
531 cpu_register_physical_memory(0xc0000, 0x10000,
532 vga_bios_offset
| IO_MEM_ROM
);
534 /* map the last 128KB of the BIOS in ISA space */
535 isa_bios_size
= bios_size
;
536 if (isa_bios_size
> (128 * 1024))
537 isa_bios_size
= 128 * 1024;
538 cpu_register_physical_memory(0xd0000, (192 * 1024) - isa_bios_size
,
540 cpu_register_physical_memory(0x100000 - isa_bios_size
,
542 (bios_offset
+ bios_size
- isa_bios_size
) | IO_MEM_ROM
);
543 /* map all the bios at the top of memory */
544 cpu_register_physical_memory((uint32_t)(-bios_size
),
545 bios_size
, bios_offset
| IO_MEM_ROM
);
550 uint8_t bootsect
[512];
551 uint8_t old_bootsect
[512];
553 if (bs_table
[0] == NULL
) {
554 fprintf(stderr
, "A disk image must be given for 'hda' when booting a Linux kernel\n");
557 snprintf(buf
, sizeof(buf
), "%s/%s", bios_dir
, LINUX_BOOT_FILENAME
);
558 ret
= load_image(buf
, bootsect
);
559 if (ret
!= sizeof(bootsect
)) {
560 fprintf(stderr
, "qemu: could not load linux boot sector '%s'\n",
565 if (bdrv_read(bs_table
[0], 0, old_bootsect
, 1) >= 0) {
566 /* copy the MSDOS partition table */
567 memcpy(bootsect
+ 0x1be, old_bootsect
+ 0x1be, 0x40);
570 bdrv_set_boot_sector(bs_table
[0], bootsect
, sizeof(bootsect
));
572 /* now we can load the kernel */
573 ret
= load_kernel(kernel_filename
,
574 phys_ram_base
+ KERNEL_LOAD_ADDR
,
575 phys_ram_base
+ KERNEL_PARAMS_ADDR
);
577 fprintf(stderr
, "qemu: could not load kernel '%s'\n",
584 if (initrd_filename
) {
585 initrd_size
= load_image(initrd_filename
, phys_ram_base
+ INITRD_LOAD_ADDR
);
586 if (initrd_size
< 0) {
587 fprintf(stderr
, "qemu: could not load initial ram disk '%s'\n",
592 if (initrd_size
> 0) {
593 stl_raw(phys_ram_base
+ KERNEL_PARAMS_ADDR
+ 0x218, INITRD_LOAD_ADDR
);
594 stl_raw(phys_ram_base
+ KERNEL_PARAMS_ADDR
+ 0x21c, initrd_size
);
596 pstrcpy(phys_ram_base
+ KERNEL_CMDLINE_ADDR
, 4096,
598 stw_raw(phys_ram_base
+ KERNEL_PARAMS_ADDR
+ 0x20, 0xA33F);
599 stw_raw(phys_ram_base
+ KERNEL_PARAMS_ADDR
+ 0x22,
600 KERNEL_CMDLINE_ADDR
- KERNEL_PARAMS_ADDR
);
602 stw_raw(phys_ram_base
+ KERNEL_PARAMS_ADDR
+ 0x210, 0x01);
606 pci_bus
= i440fx_init(&i440fx_state
);
607 piix3_devfn
= piix3_init(pci_bus
);
612 /* init basic PC hardware */
613 register_ioport_write(0x80, 1, 1, ioport80_write
, NULL
);
615 register_ioport_write(0xf0, 1, 1, ioportF0_write
, NULL
);
617 if (cirrus_vga_enabled
) {
619 pci_cirrus_vga_init(pci_bus
,
620 ds
, phys_ram_base
+ ram_size
, ram_size
,
623 isa_cirrus_vga_init(ds
, phys_ram_base
+ ram_size
, ram_size
,
628 pci_vga_init(pci_bus
, ds
, phys_ram_base
+ ram_size
, ram_size
,
631 isa_vga_init(ds
, phys_ram_base
+ ram_size
, ram_size
,
636 rtc_state
= rtc_init(0x70, 8);
638 register_ioport_read(0x92, 1, 1, ioport92_read
, NULL
);
639 register_ioport_write(0x92, 1, 1, ioport92_write
, NULL
);
642 ioapic
= ioapic_init();
644 isa_pic
= pic_init(pic_irq_request
, first_cpu
);
645 pit
= pit_init(0x40, 0);
648 pic_set_alt_irq_func(isa_pic
, ioapic_set_irq
, ioapic
);
651 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
653 serial_init(&pic_set_irq_new
, isa_pic
,
654 serial_io
[i
], serial_irq
[i
], serial_hds
[i
]);
658 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
659 if (parallel_hds
[i
]) {
660 parallel_init(parallel_io
[i
], parallel_irq
[i
], parallel_hds
[i
]);
664 for(i
= 0; i
< nb_nics
; i
++) {
668 nd
->model
= "ne2k_pci";
670 nd
->model
= "ne2k_isa";
673 if (strcmp(nd
->model
, "ne2k_isa") == 0) {
674 pc_init_ne2k_isa(nd
);
675 } else if (pci_enabled
) {
676 pci_nic_init(pci_bus
, nd
);
678 fprintf(stderr
, "qemu: Unsupported NIC: %s\n", nd
->model
);
684 pci_piix3_ide_init(pci_bus
, bs_table
, piix3_devfn
+ 1);
686 for(i
= 0; i
< 2; i
++) {
687 isa_ide_init(ide_iobase
[i
], ide_iobase2
[i
], ide_irq
[i
],
688 bs_table
[2 * i
], bs_table
[2 * i
+ 1]);
695 audio_init(pci_enabled
? pci_bus
: NULL
);
698 floppy_controller
= fdctrl_init(6, 2, 0, 0x3f0, fd_table
);
700 cmos_init(ram_size
, boot_device
, bs_table
);
702 if (pci_enabled
&& usb_enabled
) {
703 usb_uhci_init(pci_bus
, piix3_devfn
+ 2);
706 if (pci_enabled
&& acpi_enabled
) {
707 piix4_pm_init(pci_bus
, piix3_devfn
+ 3);
711 i440fx_init_memory_mappings(i440fx_state
);
714 /* ??? Need to figure out some way for the user to
715 specify SCSI devices. */
718 BlockDriverState
*bdrv
;
720 scsi
= lsi_scsi_init(pci_bus
, -1);
721 bdrv
= bdrv_new("scsidisk");
722 bdrv_open(bdrv
, "scsi_disk.img", 0);
723 lsi_scsi_attach(scsi
, bdrv
, -1);
724 bdrv
= bdrv_new("scsicd");
725 bdrv_open(bdrv
, "scsi_cd.iso", 0);
726 bdrv_set_type_hint(bdrv
, BDRV_TYPE_CDROM
);
727 lsi_scsi_attach(scsi
, bdrv
, -1);
732 static void pc_init_pci(int ram_size
, int vga_ram_size
, int boot_device
,
733 DisplayState
*ds
, const char **fd_filename
,
735 const char *kernel_filename
,
736 const char *kernel_cmdline
,
737 const char *initrd_filename
)
739 pc_init1(ram_size
, vga_ram_size
, boot_device
,
740 ds
, fd_filename
, snapshot
,
741 kernel_filename
, kernel_cmdline
,
745 static void pc_init_isa(int ram_size
, int vga_ram_size
, int boot_device
,
746 DisplayState
*ds
, const char **fd_filename
,
748 const char *kernel_filename
,
749 const char *kernel_cmdline
,
750 const char *initrd_filename
)
752 pc_init1(ram_size
, vga_ram_size
, boot_device
,
753 ds
, fd_filename
, snapshot
,
754 kernel_filename
, kernel_cmdline
,
758 QEMUMachine pc_machine
= {
764 QEMUMachine isapc_machine
= {