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 MAX_INITRD_LOAD_ADDR 0x38000000
36 #define KERNEL_PARAMS_ADDR 0x00090000
37 #define KERNEL_CMDLINE_ADDR 0x00099000
38 /* Leave a chunk of memory at the top of RAM for the BIOS ACPI tables. */
39 #define ACPI_DATA_SIZE 0x10000
41 static fdctrl_t *floppy_controller;
42 static RTCState *rtc_state;
44 static IOAPICState *ioapic;
45 static PCIDevice *i440fx_state;
47 static void ioport80_write(void *opaque, uint32_t addr, uint32_t data)
51 /* MSDOS compatibility mode FPU exception support */
52 /* XXX: add IGNNE support */
53 void cpu_set_ferr(CPUX86State *s)
58 static void ioportF0_write(void *opaque, uint32_t addr, uint32_t data)
64 uint64_t cpu_get_tsc(CPUX86State *env)
66 /* Note: when using kqemu, it is more logical to return the host TSC
67 because kqemu does not trap the RDTSC instruction for
68 performance reasons */
70 if (env->kqemu_enabled) {
71 return cpu_get_real_ticks();
75 return cpu_get_ticks();
80 void cpu_smm_update(CPUState *env)
82 if (i440fx_state && env == first_cpu)
83 i440fx_set_smm(i440fx_state, (env->hflags >> HF_SMM_SHIFT) & 1);
88 int cpu_get_pic_interrupt(CPUState *env)
92 intno = apic_get_interrupt(env);
94 /* set irq request if a PIC irq is still pending */
95 /* XXX: improve that */
96 pic_update_irq(isa_pic);
99 /* read the irq from the PIC */
100 intno = pic_read_irq(isa_pic);
104 static void pic_irq_request(void *opaque, int level)
106 CPUState *env = opaque;
108 cpu_interrupt(env, CPU_INTERRUPT_HARD);
110 cpu_reset_interrupt(env, CPU_INTERRUPT_HARD);
113 /* PC cmos mappings */
115 #define REG_EQUIPMENT_BYTE 0x14
117 static int cmos_get_fd_drive_type(int fd0)
123 /* 1.44 Mb 3"5 drive */
127 /* 2.88 Mb 3"5 drive */
131 /* 1.2 Mb 5"5 drive */
141 static void cmos_init_hd(int type_ofs, int info_ofs, BlockDriverState *hd)
143 RTCState *s = rtc_state;
144 int cylinders, heads, sectors;
145 bdrv_get_geometry_hint(hd, &cylinders, &heads, §ors);
146 rtc_set_memory(s, type_ofs, 47);
147 rtc_set_memory(s, info_ofs, cylinders);
148 rtc_set_memory(s, info_ofs + 1, cylinders >> 8);
149 rtc_set_memory(s, info_ofs + 2, heads);
150 rtc_set_memory(s, info_ofs + 3, 0xff);
151 rtc_set_memory(s, info_ofs + 4, 0xff);
152 rtc_set_memory(s, info_ofs + 5, 0xc0 | ((heads > 8) << 3));
153 rtc_set_memory(s, info_ofs + 6, cylinders);
154 rtc_set_memory(s, info_ofs + 7, cylinders >> 8);
155 rtc_set_memory(s, info_ofs + 8, sectors);
158 /* hd_table must contain 4 block drivers */
159 static void cmos_init(int ram_size, int boot_device, BlockDriverState **hd_table)
161 RTCState *s = rtc_state;
166 /* various important CMOS locations needed by PC/Bochs bios */
169 val = 640; /* base memory in K */
170 rtc_set_memory(s, 0x15, val);
171 rtc_set_memory(s, 0x16, val >> 8);
173 val = (ram_size / 1024) - 1024;
176 rtc_set_memory(s, 0x17, val);
177 rtc_set_memory(s, 0x18, val >> 8);
178 rtc_set_memory(s, 0x30, val);
179 rtc_set_memory(s, 0x31, val >> 8);
181 if (ram_size > (16 * 1024 * 1024))
182 val = (ram_size / 65536) - ((16 * 1024 * 1024) / 65536);
187 rtc_set_memory(s, 0x34, val);
188 rtc_set_memory(s, 0x35, val >> 8);
190 switch(boot_device) {
193 rtc_set_memory(s, 0x3d, 0x01); /* floppy boot */
195 rtc_set_memory(s, 0x38, 0x01); /* disable signature check */
199 rtc_set_memory(s, 0x3d, 0x02); /* hard drive boot */
202 rtc_set_memory(s, 0x3d, 0x03); /* CD-ROM boot */
208 fd0 = fdctrl_get_drive_type(floppy_controller, 0);
209 fd1 = fdctrl_get_drive_type(floppy_controller, 1);
211 val = (cmos_get_fd_drive_type(fd0) << 4) | cmos_get_fd_drive_type(fd1);
212 rtc_set_memory(s, 0x10, val);
224 val |= 0x01; /* 1 drive, ready for boot */
227 val |= 0x41; /* 2 drives, ready for boot */
230 val |= 0x02; /* FPU is there */
231 val |= 0x04; /* PS/2 mouse installed */
232 rtc_set_memory(s, REG_EQUIPMENT_BYTE, val);
236 rtc_set_memory(s, 0x12, (hd_table[0] ? 0xf0 : 0) | (hd_table[1] ? 0x0f : 0));
238 cmos_init_hd(0x19, 0x1b, hd_table[0]);
240 cmos_init_hd(0x1a, 0x24, hd_table[1]);
243 for (i = 0; i < 4; i++) {
245 int cylinders, heads, sectors, translation;
246 /* NOTE: bdrv_get_geometry_hint() returns the physical
247 geometry. It is always such that: 1 <= sects <= 63, 1
248 <= heads <= 16, 1 <= cylinders <= 16383. The BIOS
249 geometry can be different if a translation is done. */
250 translation = bdrv_get_translation_hint(hd_table[i]);
251 if (translation == BIOS_ATA_TRANSLATION_AUTO) {
252 bdrv_get_geometry_hint(hd_table[i], &cylinders, &heads, §ors);
253 if (cylinders <= 1024 && heads <= 16 && sectors <= 63) {
254 /* No translation. */
257 /* LBA translation. */
263 val |= translation << (i * 2);
266 rtc_set_memory(s, 0x39, val);
269 void ioport_set_a20(int enable)
271 /* XXX: send to all CPUs ? */
272 cpu_x86_set_a20(first_cpu, enable);
275 int ioport_get_a20(void)
277 return ((first_cpu->a20_mask >> 20) & 1);
280 static void ioport92_write(void *opaque, uint32_t addr, uint32_t val)
282 ioport_set_a20((val >> 1) & 1);
283 /* XXX: bit 0 is fast reset */
286 static uint32_t ioport92_read(void *opaque, uint32_t addr)
288 return ioport_get_a20() << 1;
291 /***********************************************************/
292 /* Bochs BIOS debug ports */
294 void bochs_bios_write(void *opaque, uint32_t addr, uint32_t val)
296 static const char shutdown_str[8] = "Shutdown";
297 static int shutdown_index = 0;
300 /* Bochs BIOS messages */
303 fprintf(stderr, "BIOS panic at rombios.c, line %d\n", val);
308 fprintf(stderr, "%c", val);
312 /* same as Bochs power off */
313 if (val == shutdown_str[shutdown_index]) {
315 if (shutdown_index == 8) {
317 qemu_system_shutdown_request();
324 /* LGPL'ed VGA BIOS messages */
327 fprintf(stderr, "VGA BIOS panic, line %d\n", val);
332 fprintf(stderr, "%c", val);
338 void bochs_bios_init(void)
340 register_ioport_write(0x400, 1, 2, bochs_bios_write, NULL);
341 register_ioport_write(0x401, 1, 2, bochs_bios_write, NULL);
342 register_ioport_write(0x402, 1, 1, bochs_bios_write, NULL);
343 register_ioport_write(0x403, 1, 1, bochs_bios_write, NULL);
344 register_ioport_write(0x8900, 1, 1, bochs_bios_write, NULL);
346 register_ioport_write(0x501, 1, 2, bochs_bios_write, NULL);
347 register_ioport_write(0x502, 1, 2, bochs_bios_write, NULL);
348 register_ioport_write(0x500, 1, 1, bochs_bios_write, NULL);
349 register_ioport_write(0x503, 1, 1, bochs_bios_write, NULL);
353 int load_kernel(const char *filename, uint8_t *addr,
359 fd = open(filename, O_RDONLY | O_BINARY);
363 /* load 16 bit code */
364 if (read(fd, real_addr, 512) != 512)
366 setup_sects = real_addr[0x1F1];
369 if (read(fd, real_addr + 512, setup_sects * 512) !=
373 /* load 32 bit code */
374 size = read(fd, addr, 16 * 1024 * 1024);
384 static void main_cpu_reset(void *opaque)
386 CPUState *env = opaque;
390 static const int ide_iobase[2] = { 0x1f0, 0x170 };
391 static const int ide_iobase2[2] = { 0x3f6, 0x376 };
392 static const int ide_irq[2] = { 14, 15 };
394 #define NE2000_NB_MAX 6
396 static int ne2000_io[NE2000_NB_MAX] = { 0x300, 0x320, 0x340, 0x360, 0x280, 0x380 };
397 static int ne2000_irq[NE2000_NB_MAX] = { 9, 10, 11, 3, 4, 5 };
399 static int serial_io[MAX_SERIAL_PORTS] = { 0x3f8, 0x2f8, 0x3e8, 0x2e8 };
400 static int serial_irq[MAX_SERIAL_PORTS] = { 4, 3, 4, 3 };
402 static int parallel_io[MAX_PARALLEL_PORTS] = { 0x378, 0x278, 0x3bc };
403 static int parallel_irq[MAX_PARALLEL_PORTS] = { 7, 7, 7 };
406 static void audio_init (PCIBus *pci_bus)
409 int audio_enabled = 0;
411 for (c = soundhw; !audio_enabled && c->name; ++c) {
412 audio_enabled = c->enabled;
420 for (c = soundhw; c->name; ++c) {
423 c->init.init_isa (s);
427 c->init.init_pci (pci_bus, s);
437 static void pc_init_ne2k_isa(NICInfo *nd)
439 static int nb_ne2k = 0;
441 if (nb_ne2k == NE2000_NB_MAX)
443 isa_ne2000_init(ne2000_io[nb_ne2k], ne2000_irq[nb_ne2k], nd);
447 /* PC hardware initialisation */
448 static void pc_init1(int ram_size, int vga_ram_size, int boot_device,
449 DisplayState *ds, const char **fd_filename, int snapshot,
450 const char *kernel_filename, const char *kernel_cmdline,
451 const char *initrd_filename,
455 int ret, linux_boot, initrd_size, i;
456 ram_addr_t ram_addr, vga_ram_addr, bios_offset, vga_bios_offset;
457 ram_addr_t initrd_offset;
458 int bios_size, isa_bios_size, vga_bios_size;
460 int piix3_devfn = -1;
464 linux_boot = (kernel_filename != NULL);
467 for(i = 0; i < smp_cpus; i++) {
470 env->hflags |= HF_HALTED_MASK;
472 /* XXX: enable it in all cases */
473 env->cpuid_features |= CPUID_APIC;
475 register_savevm("cpu", i, 4, cpu_save, cpu_load, env);
476 qemu_register_reset(main_cpu_reset, env);
483 ram_addr = qemu_ram_alloc(ram_size);
484 cpu_register_physical_memory(0, ram_size, ram_addr);
486 /* allocate VGA RAM */
487 vga_ram_addr = qemu_ram_alloc(vga_ram_size);
490 snprintf(buf, sizeof(buf), "%s/%s", bios_dir, BIOS_FILENAME);
491 bios_size = get_image_size(buf);
492 if (bios_size <= 0 ||
493 (bios_size % 65536) != 0) {
496 bios_offset = qemu_ram_alloc(bios_size);
497 ret = load_image(buf, phys_ram_base + bios_offset);
498 if (ret != bios_size) {
500 fprintf(stderr, "qemu: could not load PC BIOS '%s'\n", buf);
505 if (cirrus_vga_enabled) {
506 snprintf(buf, sizeof(buf), "%s/%s", bios_dir, VGABIOS_CIRRUS_FILENAME);
508 snprintf(buf, sizeof(buf), "%s/%s", bios_dir, VGABIOS_FILENAME);
510 vga_bios_size = get_image_size(buf);
511 if (vga_bios_size <= 0 || vga_bios_size > 65536)
513 vga_bios_offset = qemu_ram_alloc(65536);
515 ret = load_image(buf, phys_ram_base + vga_bios_offset);
516 if (ret != vga_bios_size) {
518 fprintf(stderr, "qemu: could not load VGA BIOS '%s'\n", buf);
522 /* setup basic memory access */
523 cpu_register_physical_memory(0xc0000, 0x10000,
524 vga_bios_offset | IO_MEM_ROM);
526 /* map the last 128KB of the BIOS in ISA space */
527 isa_bios_size = bios_size;
528 if (isa_bios_size > (128 * 1024))
529 isa_bios_size = 128 * 1024;
530 cpu_register_physical_memory(0xd0000, (192 * 1024) - isa_bios_size,
532 cpu_register_physical_memory(0x100000 - isa_bios_size,
534 (bios_offset + bios_size - isa_bios_size) | IO_MEM_ROM);
537 ram_addr_t option_rom_offset;
541 for (i = 0; i < nb_option_roms; i++) {
542 size = get_image_size(option_rom[i]);
544 fprintf(stderr, "Could not load option rom '%s'\n",
548 if (size > (0x10000 - offset))
549 goto option_rom_error;
550 option_rom_offset = qemu_ram_alloc(size);
551 ret = load_image(option_rom[i], phys_ram_base + option_rom_offset);
554 fprintf(stderr, "Too many option ROMS\n");
557 size = (size + 4095) & ~4095;
558 cpu_register_physical_memory(0xd0000 + offset,
559 size, option_rom_offset | IO_MEM_ROM);
564 /* map all the bios at the top of memory */
565 cpu_register_physical_memory((uint32_t)(-bios_size),
566 bios_size, bios_offset | IO_MEM_ROM);
571 uint8_t bootsect[512];
572 uint8_t old_bootsect[512];
574 if (bs_table[0] == NULL) {
575 fprintf(stderr, "A disk image must be given for 'hda' when booting a Linux kernel\n");
578 snprintf(buf, sizeof(buf), "%s/%s", bios_dir, LINUX_BOOT_FILENAME);
579 ret = load_image(buf, bootsect);
580 if (ret != sizeof(bootsect)) {
581 fprintf(stderr, "qemu: could not load linux boot sector '%s'\n",
586 if (bdrv_read(bs_table[0], 0, old_bootsect, 1) >= 0) {
587 /* copy the MSDOS partition table */
588 memcpy(bootsect + 0x1be, old_bootsect + 0x1be, 0x40);
591 bdrv_set_boot_sector(bs_table[0], bootsect, sizeof(bootsect));
593 /* now we can load the kernel */
594 ret = load_kernel(kernel_filename,
595 phys_ram_base + KERNEL_LOAD_ADDR,
596 phys_ram_base + KERNEL_PARAMS_ADDR);
598 fprintf(stderr, "qemu: could not load kernel '%s'\n",
606 if (initrd_filename) {
607 initrd_size = get_image_size (initrd_filename);
608 if (initrd_size > 0) {
609 initrd_offset = (ram_size - initrd_size) & TARGET_PAGE_MASK;
610 /* Leave space for BIOS ACPI tables. */
611 initrd_offset -= ACPI_DATA_SIZE;
612 /* Avoid the last 64k to avoid 2.2.x kernel bugs. */
613 initrd_offset -= 0x10000;
614 if (initrd_offset > MAX_INITRD_LOAD_ADDR)
615 initrd_offset = MAX_INITRD_LOAD_ADDR;
617 if (initrd_size > ram_size
618 || initrd_offset < KERNEL_LOAD_ADDR + ret) {
620 "qemu: memory too small for initial ram disk '%s'\n",
624 initrd_size = load_image(initrd_filename,
625 phys_ram_base + initrd_offset);
627 if (initrd_size < 0) {
628 fprintf(stderr, "qemu: could not load initial ram disk '%s'\n",
633 if (initrd_size > 0) {
634 stl_raw(phys_ram_base + KERNEL_PARAMS_ADDR + 0x218, initrd_offset);
635 stl_raw(phys_ram_base + KERNEL_PARAMS_ADDR + 0x21c, initrd_size);
637 pstrcpy(phys_ram_base + KERNEL_CMDLINE_ADDR, 4096,
639 stw_raw(phys_ram_base + KERNEL_PARAMS_ADDR + 0x20, 0xA33F);
640 stw_raw(phys_ram_base + KERNEL_PARAMS_ADDR + 0x22,
641 KERNEL_CMDLINE_ADDR - KERNEL_PARAMS_ADDR);
643 stw_raw(phys_ram_base + KERNEL_PARAMS_ADDR + 0x210, 0x01);
647 pci_bus = i440fx_init(&i440fx_state);
648 piix3_devfn = piix3_init(pci_bus, -1);
653 /* init basic PC hardware */
654 register_ioport_write(0x80, 1, 1, ioport80_write, NULL);
656 register_ioport_write(0xf0, 1, 1, ioportF0_write, NULL);
658 if (cirrus_vga_enabled) {
660 pci_cirrus_vga_init(pci_bus,
661 ds, phys_ram_base + vga_ram_addr,
662 vga_ram_addr, vga_ram_size);
664 isa_cirrus_vga_init(ds, phys_ram_base + vga_ram_addr,
665 vga_ram_addr, vga_ram_size);
669 pci_vga_init(pci_bus, ds, phys_ram_base + vga_ram_addr,
670 vga_ram_addr, vga_ram_size, 0, 0);
672 isa_vga_init(ds, phys_ram_base + vga_ram_addr,
673 vga_ram_addr, vga_ram_size);
677 rtc_state = rtc_init(0x70, 8);
679 register_ioport_read(0x92, 1, 1, ioport92_read, NULL);
680 register_ioport_write(0x92, 1, 1, ioport92_write, NULL);
683 ioapic = ioapic_init();
685 isa_pic = pic_init(pic_irq_request, first_cpu);
686 pit = pit_init(0x40, 0);
689 pic_set_alt_irq_func(isa_pic, ioapic_set_irq, ioapic);
692 for(i = 0; i < MAX_SERIAL_PORTS; i++) {
694 serial_init(&pic_set_irq_new, isa_pic,
695 serial_io[i], serial_irq[i], serial_hds[i]);
699 for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
700 if (parallel_hds[i]) {
701 parallel_init(parallel_io[i], parallel_irq[i], parallel_hds[i]);
705 for(i = 0; i < nb_nics; i++) {
709 nd->model = "ne2k_pci";
711 nd->model = "ne2k_isa";
714 if (strcmp(nd->model, "ne2k_isa") == 0) {
715 pc_init_ne2k_isa(nd);
716 } else if (pci_enabled) {
717 pci_nic_init(pci_bus, nd, -1);
719 fprintf(stderr, "qemu: Unsupported NIC: %s\n", nd->model);
725 pci_piix3_ide_init(pci_bus, bs_table, piix3_devfn + 1);
727 for(i = 0; i < 2; i++) {
728 isa_ide_init(ide_iobase[i], ide_iobase2[i], ide_irq[i],
729 bs_table[2 * i], bs_table[2 * i + 1]);
736 audio_init(pci_enabled ? pci_bus : NULL);
739 floppy_controller = fdctrl_init(6, 2, 0, 0x3f0, fd_table);
741 cmos_init(ram_size, boot_device, bs_table);
743 if (pci_enabled && usb_enabled) {
744 usb_uhci_init(pci_bus, piix3_devfn + 2);
747 if (pci_enabled && acpi_enabled) {
748 uint8_t *eeprom_buf = qemu_mallocz(8 * 256); /* XXX: make this persistent */
749 piix4_pm_init(pci_bus, piix3_devfn + 3);
750 for (i = 0; i < 8; i++) {
751 SMBusDevice *eeprom = smbus_eeprom_device_init(0x50 + i,
752 eeprom_buf + (i * 256));
753 piix4_smbus_register_device(eeprom, 0x50 + i);
758 i440fx_init_memory_mappings(i440fx_state);
761 /* ??? Need to figure out some way for the user to
762 specify SCSI devices. */
765 BlockDriverState *bdrv;
767 scsi = lsi_scsi_init(pci_bus, -1);
768 bdrv = bdrv_new("scsidisk");
769 bdrv_open(bdrv, "scsi_disk.img", 0);
770 lsi_scsi_attach(scsi, bdrv, -1);
771 bdrv = bdrv_new("scsicd");
772 bdrv_open(bdrv, "scsi_cd.iso", 0);
773 bdrv_set_type_hint(bdrv, BDRV_TYPE_CDROM);
774 lsi_scsi_attach(scsi, bdrv, -1);
779 static void pc_init_pci(int ram_size, int vga_ram_size, int boot_device,
780 DisplayState *ds, const char **fd_filename,
782 const char *kernel_filename,
783 const char *kernel_cmdline,
784 const char *initrd_filename,
785 const char *cpu_model)
787 pc_init1(ram_size, vga_ram_size, boot_device,
788 ds, fd_filename, snapshot,
789 kernel_filename, kernel_cmdline,
793 static void pc_init_isa(int ram_size, int vga_ram_size, int boot_device,
794 DisplayState *ds, const char **fd_filename,
796 const char *kernel_filename,
797 const char *kernel_cmdline,
798 const char *initrd_filename,
799 const char *cpu_model)
801 pc_init1(ram_size, vga_ram_size, boot_device,
802 ds, fd_filename, snapshot,
803 kernel_filename, kernel_cmdline,
807 QEMUMachine pc_machine = {
813 QEMUMachine isapc_machine = {