2 * QEMU PC System Emulator
4 * Copyright (c) 2003 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
42 #include <sys/ioctl.h>
43 #include <sys/socket.h>
45 #include <linux/if_tun.h>
53 #define DEBUG_LOGFILE "/tmp/vl.log"
54 #define DEFAULT_NETWORK_SCRIPT "/etc/vl-ifup"
55 #define BIOS_FILENAME "bios.bin"
56 #define VGABIOS_FILENAME "vgabios.bin"
58 //#define DEBUG_UNUSED_IOPORT
60 //#define DEBUG_IRQ_LATENCY
62 /* output Bochs bios info messages */
65 /* debug IDE devices */
71 /* debug NE2000 card */
72 //#define DEBUG_NE2000
74 /* debug PC keyboard */
77 /* debug PC keyboard : only mouse */
80 #define PHYS_RAM_BASE 0xac000000
81 #define PHYS_RAM_MAX_SIZE (256 * 1024 * 1024)
83 #define KERNEL_LOAD_ADDR 0x00100000
84 #define INITRD_LOAD_ADDR 0x00400000
85 #define KERNEL_PARAMS_ADDR 0x00090000
87 #define GUI_REFRESH_INTERVAL 30
91 /* from plex86 (BSD license) */
92 struct __attribute__ ((packed)) linux_params {
93 // For 0x00..0x3f, see 'struct screen_info' in linux/include/linux/tty.h.
94 // I just padded out the VESA parts, rather than define them.
96 /* 0x000 */ uint8_t orig_x;
97 /* 0x001 */ uint8_t orig_y;
98 /* 0x002 */ uint16_t ext_mem_k;
99 /* 0x004 */ uint16_t orig_video_page;
100 /* 0x006 */ uint8_t orig_video_mode;
101 /* 0x007 */ uint8_t orig_video_cols;
102 /* 0x008 */ uint16_t unused1;
103 /* 0x00a */ uint16_t orig_video_ega_bx;
104 /* 0x00c */ uint16_t unused2;
105 /* 0x00e */ uint8_t orig_video_lines;
106 /* 0x00f */ uint8_t orig_video_isVGA;
107 /* 0x010 */ uint16_t orig_video_points;
108 /* 0x012 */ uint8_t pad0[0x20 - 0x12]; // VESA info.
109 /* 0x020 */ uint16_t cl_magic; // Commandline magic number (0xA33F)
110 /* 0x022 */ uint16_t cl_offset; // Commandline offset. Address of commandline
111 // is calculated as 0x90000 + cl_offset, bu
112 // only if cl_magic == 0xA33F.
113 /* 0x024 */ uint8_t pad1[0x40 - 0x24]; // VESA info.
115 /* 0x040 */ uint8_t apm_bios_info[20]; // struct apm_bios_info
116 /* 0x054 */ uint8_t pad2[0x80 - 0x54];
118 // Following 2 from 'struct drive_info_struct' in drivers/block/cciss.h.
119 // Might be truncated?
120 /* 0x080 */ uint8_t hd0_info[16]; // hd0-disk-parameter from intvector 0x41
121 /* 0x090 */ uint8_t hd1_info[16]; // hd1-disk-parameter from intvector 0x46
123 // System description table truncated to 16 bytes
124 // From 'struct sys_desc_table_struct' in linux/arch/i386/kernel/setup.c.
125 /* 0x0a0 */ uint16_t sys_description_len;
126 /* 0x0a2 */ uint8_t sys_description_table[14];
128 // [1] machine submodel id
132 /* 0x0b0 */ uint8_t pad3[0x1e0 - 0xb0];
133 /* 0x1e0 */ uint32_t alt_mem_k;
134 /* 0x1e4 */ uint8_t pad4[4];
135 /* 0x1e8 */ uint8_t e820map_entries;
136 /* 0x1e9 */ uint8_t eddbuf_entries; // EDD_NR
137 /* 0x1ea */ uint8_t pad5[0x1f1 - 0x1ea];
138 /* 0x1f1 */ uint8_t setup_sects; // size of setup.S, number of sectors
139 /* 0x1f2 */ uint16_t mount_root_rdonly; // MOUNT_ROOT_RDONLY (if !=0)
140 /* 0x1f4 */ uint16_t sys_size; // size of compressed kernel-part in the
141 // (b)zImage-file (in 16 byte units, rounded up)
142 /* 0x1f6 */ uint16_t swap_dev; // (unused AFAIK)
143 /* 0x1f8 */ uint16_t ramdisk_flags;
144 /* 0x1fa */ uint16_t vga_mode; // (old one)
145 /* 0x1fc */ uint16_t orig_root_dev; // (high=Major, low=minor)
146 /* 0x1fe */ uint8_t pad6[1];
147 /* 0x1ff */ uint8_t aux_device_info;
148 /* 0x200 */ uint16_t jump_setup; // Jump to start of setup code,
149 // aka "reserved" field.
150 /* 0x202 */ uint8_t setup_signature[4]; // Signature for SETUP-header, ="HdrS"
151 /* 0x206 */ uint16_t header_format_version; // Version number of header format;
152 /* 0x208 */ uint8_t setup_S_temp0[8]; // Used by setup.S for communication with
153 // boot loaders, look there.
154 /* 0x210 */ uint8_t loader_type;
159 // T=2: bootsect-loader
163 /* 0x211 */ uint8_t loadflags;
164 // bit0 = 1: kernel is loaded high (bzImage)
165 // bit7 = 1: Heap and pointer (see below) set by boot
167 /* 0x212 */ uint16_t setup_S_temp1;
168 /* 0x214 */ uint32_t kernel_start;
169 /* 0x218 */ uint32_t initrd_start;
170 /* 0x21c */ uint32_t initrd_size;
171 /* 0x220 */ uint8_t setup_S_temp2[4];
172 /* 0x224 */ uint16_t setup_S_heap_end_pointer;
173 /* 0x226 */ uint8_t pad7[0x2d0 - 0x226];
175 /* 0x2d0 : Int 15, ax=e820 memory map. */
176 // (linux/include/asm-i386/e820.h, 'struct e820entry')
179 #define E820_RESERVED 2
180 #define E820_ACPI 3 /* usable as RAM once ACPI tables have been read */
188 /* 0x550 */ uint8_t pad8[0x600 - 0x550];
190 // BIOS Enhanced Disk Drive Services.
191 // (From linux/include/asm-i386/edd.h, 'struct edd_info')
192 // Each 'struct edd_info is 78 bytes, times a max of 6 structs in array.
193 /* 0x600 */ uint8_t eddbuf[0x7d4 - 0x600];
195 /* 0x7d4 */ uint8_t pad9[0x800 - 0x7d4];
196 /* 0x800 */ uint8_t commandline[0x800];
199 uint64_t gdt_table[256];
200 uint64_t idt_table[48];
203 #define KERNEL_CS 0x10
204 #define KERNEL_DS 0x18
206 #define MAX_IOPORTS 4096
208 static const char *interp_prefix = CONFIG_QEMU_PREFIX;
209 char phys_ram_file[1024];
210 CPUX86State *global_env;
211 CPUX86State *cpu_single_env;
212 FILE *logfile = NULL;
214 IOPortReadFunc *ioport_read_table[3][MAX_IOPORTS];
215 IOPortWriteFunc *ioport_write_table[3][MAX_IOPORTS];
216 BlockDriverState *bs_table[MAX_DISKS];
218 static DisplayState display_state;
221 int64_t ticks_per_sec;
223 /***********************************************************/
226 uint32_t default_ioport_readb(CPUX86State *env, uint32_t address)
228 #ifdef DEBUG_UNUSED_IOPORT
229 fprintf(stderr, "inb: port=0x%04x\n", address);
234 void default_ioport_writeb(CPUX86State *env, uint32_t address, uint32_t data)
236 #ifdef DEBUG_UNUSED_IOPORT
237 fprintf(stderr, "outb: port=0x%04x data=0x%02x\n", address, data);
241 /* default is to make two byte accesses */
242 uint32_t default_ioport_readw(CPUX86State *env, uint32_t address)
245 data = ioport_read_table[0][address & (MAX_IOPORTS - 1)](env, address);
246 data |= ioport_read_table[0][(address + 1) & (MAX_IOPORTS - 1)](env, address + 1) << 8;
250 void default_ioport_writew(CPUX86State *env, uint32_t address, uint32_t data)
252 ioport_write_table[0][address & (MAX_IOPORTS - 1)](env, address, data & 0xff);
253 ioport_write_table[0][(address + 1) & (MAX_IOPORTS - 1)](env, address + 1, (data >> 8) & 0xff);
256 uint32_t default_ioport_readl(CPUX86State *env, uint32_t address)
258 #ifdef DEBUG_UNUSED_IOPORT
259 fprintf(stderr, "inl: port=0x%04x\n", address);
264 void default_ioport_writel(CPUX86State *env, uint32_t address, uint32_t data)
266 #ifdef DEBUG_UNUSED_IOPORT
267 fprintf(stderr, "outl: port=0x%04x data=0x%02x\n", address, data);
271 void init_ioports(void)
275 for(i = 0; i < MAX_IOPORTS; i++) {
276 ioport_read_table[0][i] = default_ioport_readb;
277 ioport_write_table[0][i] = default_ioport_writeb;
278 ioport_read_table[1][i] = default_ioport_readw;
279 ioport_write_table[1][i] = default_ioport_writew;
280 ioport_read_table[2][i] = default_ioport_readl;
281 ioport_write_table[2][i] = default_ioport_writel;
285 /* size is the word size in byte */
286 int register_ioport_read(int start, int length, IOPortReadFunc *func, int size)
298 for(i = start; i < start + length; i += size)
299 ioport_read_table[bsize][i] = func;
303 /* size is the word size in byte */
304 int register_ioport_write(int start, int length, IOPortWriteFunc *func, int size)
316 for(i = start; i < start + length; i += size)
317 ioport_write_table[bsize][i] = func;
321 void pstrcpy(char *buf, int buf_size, const char *str)
331 if (c == 0 || q >= buf + buf_size - 1)
338 /* strcat and truncate. */
339 char *pstrcat(char *buf, int buf_size, const char *s)
344 pstrcpy(buf + len, buf_size - len, s);
348 int load_kernel(const char *filename, uint8_t *addr)
350 int fd, size, setup_sects;
351 uint8_t bootsect[512];
353 fd = open(filename, O_RDONLY);
356 if (read(fd, bootsect, 512) != 512)
358 setup_sects = bootsect[0x1F1];
361 /* skip 16 bit setup code */
362 lseek(fd, (setup_sects + 1) * 512, SEEK_SET);
363 size = read(fd, addr, 16 * 1024 * 1024);
373 /* return the size or -1 if error */
374 int load_image(const char *filename, uint8_t *addr)
377 fd = open(filename, O_RDONLY);
380 size = lseek(fd, 0, SEEK_END);
381 lseek(fd, 0, SEEK_SET);
382 if (read(fd, addr, size) != size) {
390 void cpu_x86_outb(CPUX86State *env, int addr, int val)
392 ioport_write_table[0][addr & (MAX_IOPORTS - 1)](env, addr, val);
395 void cpu_x86_outw(CPUX86State *env, int addr, int val)
397 ioport_write_table[1][addr & (MAX_IOPORTS - 1)](env, addr, val);
400 void cpu_x86_outl(CPUX86State *env, int addr, int val)
402 ioport_write_table[2][addr & (MAX_IOPORTS - 1)](env, addr, val);
405 int cpu_x86_inb(CPUX86State *env, int addr)
407 return ioport_read_table[0][addr & (MAX_IOPORTS - 1)](env, addr);
410 int cpu_x86_inw(CPUX86State *env, int addr)
412 return ioport_read_table[1][addr & (MAX_IOPORTS - 1)](env, addr);
415 int cpu_x86_inl(CPUX86State *env, int addr)
417 return ioport_read_table[2][addr & (MAX_IOPORTS - 1)](env, addr);
420 /***********************************************************/
421 void ioport80_write(CPUX86State *env, uint32_t addr, uint32_t data)
425 void hw_error(const char *fmt, ...)
430 fprintf(stderr, "qemu: hardware error: ");
431 vfprintf(stderr, fmt, ap);
432 fprintf(stderr, "\n");
434 cpu_x86_dump_state(global_env, stderr, X86_DUMP_FPU | X86_DUMP_CCOP);
440 /***********************************************************/
443 #define RTC_SECONDS 0
444 #define RTC_SECONDS_ALARM 1
445 #define RTC_MINUTES 2
446 #define RTC_MINUTES_ALARM 3
448 #define RTC_HOURS_ALARM 5
449 #define RTC_ALARM_DONT_CARE 0xC0
451 #define RTC_DAY_OF_WEEK 6
452 #define RTC_DAY_OF_MONTH 7
461 /* PC cmos mappings */
462 #define REG_EQUIPMENT_BYTE 0x14
464 uint8_t cmos_data[128];
467 void cmos_ioport_write(CPUX86State *env, uint32_t addr, uint32_t data)
470 cmos_index = data & 0x7f;
474 uint32_t cmos_ioport_read(CPUX86State *env, uint32_t addr)
481 /* toggle update-in-progress bit for Linux (same hack as
483 ret = cmos_data[cmos_index];
484 if (cmos_index == RTC_REG_A)
485 cmos_data[RTC_REG_A] ^= 0x80;
486 else if (cmos_index == RTC_REG_C)
487 cmos_data[RTC_REG_C] = 0x00;
493 static inline int to_bcd(int a)
495 return ((a / 10) << 4) | (a % 10);
506 cmos_data[RTC_SECONDS] = to_bcd(tm->tm_sec);
507 cmos_data[RTC_MINUTES] = to_bcd(tm->tm_min);
508 cmos_data[RTC_HOURS] = to_bcd(tm->tm_hour);
509 cmos_data[RTC_DAY_OF_WEEK] = to_bcd(tm->tm_wday);
510 cmos_data[RTC_DAY_OF_MONTH] = to_bcd(tm->tm_mday);
511 cmos_data[RTC_MONTH] = to_bcd(tm->tm_mon + 1);
512 cmos_data[RTC_YEAR] = to_bcd(tm->tm_year % 100);
514 cmos_data[RTC_REG_A] = 0x26;
515 cmos_data[RTC_REG_B] = 0x02;
516 cmos_data[RTC_REG_C] = 0x00;
517 cmos_data[RTC_REG_D] = 0x80;
519 /* various important CMOS locations needed by PC/Bochs bios */
521 cmos_data[REG_EQUIPMENT_BYTE] = 0x02; /* FPU is there */
522 cmos_data[REG_EQUIPMENT_BYTE] |= 0x04; /* PS/2 mouse installed */
525 val = (phys_ram_size / 1024) - 1024;
528 cmos_data[0x17] = val;
529 cmos_data[0x18] = val >> 8;
530 cmos_data[0x30] = val;
531 cmos_data[0x31] = val >> 8;
533 val = (phys_ram_size / 65536) - ((16 * 1024 * 1024) / 65536);
536 cmos_data[0x34] = val;
537 cmos_data[0x35] = val >> 8;
539 cmos_data[0x3d] = 0x02; /* hard drive boot */
541 register_ioport_write(0x70, 2, cmos_ioport_write, 1);
542 register_ioport_read(0x70, 2, cmos_ioport_read, 1);
545 /***********************************************************/
546 /* 8259 pic emulation */
548 typedef struct PicState {
549 uint8_t last_irr; /* edge detection */
550 uint8_t irr; /* interrupt request register */
551 uint8_t imr; /* interrupt mask register */
552 uint8_t isr; /* interrupt service register */
553 uint8_t priority_add; /* used to compute irq priority */
555 uint8_t read_reg_select;
556 uint8_t special_mask;
559 uint8_t rotate_on_autoeoi;
560 uint8_t init4; /* true if 4 byte init */
563 /* 0 is master pic, 1 is slave pic */
565 int pic_irq_requested;
567 /* set irq level. If an edge is detected, then the IRR is set to 1 */
568 static inline void pic_set_irq1(PicState *s, int irq, int level)
573 if ((s->last_irr & mask) == 0)
577 s->last_irr &= ~mask;
581 static inline int get_priority(PicState *s, int mask)
587 while ((mask & (1 << ((priority + s->priority_add) & 7))) == 0)
592 /* return the pic wanted interrupt. return -1 if none */
593 static int pic_get_irq(PicState *s)
595 int mask, cur_priority, priority;
597 mask = s->irr & ~s->imr;
598 priority = get_priority(s, mask);
601 /* compute current priority */
602 cur_priority = get_priority(s, s->isr);
603 if (priority > cur_priority) {
604 /* higher priority found: an irq should be generated */
611 /* raise irq to CPU if necessary. must be called every time the active
613 static void pic_update_irq(void)
617 /* first look at slave pic */
618 irq2 = pic_get_irq(&pics[1]);
620 /* if irq request by slave pic, signal master PIC */
621 pic_set_irq1(&pics[0], 2, 1);
622 pic_set_irq1(&pics[0], 2, 0);
624 /* look at requested irq */
625 irq = pic_get_irq(&pics[0]);
629 pic_irq_requested = 8 + irq2;
631 /* from master pic */
632 pic_irq_requested = irq;
634 cpu_x86_interrupt(global_env, CPU_INTERRUPT_HARD);
638 #ifdef DEBUG_IRQ_LATENCY
639 int64_t irq_time[16];
640 int64_t cpu_get_ticks(void);
642 #if defined(DEBUG_PIC)
646 void pic_set_irq(int irq, int level)
648 #if defined(DEBUG_PIC)
649 if (level != irq_level[irq]) {
650 printf("pic_set_irq: irq=%d level=%d\n", irq, level);
651 irq_level[irq] = level;
654 #ifdef DEBUG_IRQ_LATENCY
656 irq_time[irq] = cpu_get_ticks();
659 pic_set_irq1(&pics[irq >> 3], irq & 7, level);
663 int cpu_x86_get_pic_interrupt(CPUX86State *env)
665 int irq, irq2, intno;
667 /* signal the pic that the irq was acked by the CPU */
668 irq = pic_irq_requested;
669 #ifdef DEBUG_IRQ_LATENCY
670 printf("IRQ%d latency=%0.3fus\n",
672 (double)(cpu_get_ticks() - irq_time[irq]) * 1000000.0 / ticks_per_sec);
675 printf("pic_interrupt: irq=%d\n", irq);
680 pics[1].isr |= (1 << irq2);
681 pics[1].irr &= ~(1 << irq2);
683 intno = pics[1].irq_base + irq2;
685 intno = pics[0].irq_base + irq;
687 pics[0].isr |= (1 << irq);
688 pics[0].irr &= ~(1 << irq);
692 void pic_ioport_write(CPUX86State *env, uint32_t addr, uint32_t val)
698 printf("pic_write: addr=0x%02x val=0x%02x\n", addr, val);
700 s = &pics[addr >> 7];
705 memset(s, 0, sizeof(PicState));
709 hw_error("single mode not supported");
711 hw_error("level sensitive irq not supported");
712 } else if (val & 0x08) {
714 s->read_reg_select = val & 1;
716 s->special_mask = (val >> 5) & 1;
721 s->rotate_on_autoeoi = val >> 7;
723 case 0x20: /* end of interrupt */
725 priority = get_priority(s, s->isr);
727 s->isr &= ~(1 << ((priority + s->priority_add) & 7));
730 s->priority_add = (s->priority_add + 1) & 7;
735 s->isr &= ~(1 << priority);
739 s->priority_add = (val + 1) & 7;
744 s->isr &= ~(1 << priority);
745 s->priority_add = (priority + 1) & 7;
751 switch(s->init_state) {
758 s->irq_base = val & 0xf8;
769 s->auto_eoi = (val >> 1) & 1;
776 uint32_t pic_ioport_read(CPUX86State *env, uint32_t addr1)
783 s = &pics[addr >> 7];
786 if (s->read_reg_select)
794 printf("pic_read: addr=0x%02x val=0x%02x\n", addr1, ret);
801 register_ioport_write(0x20, 2, pic_ioport_write, 1);
802 register_ioport_read(0x20, 2, pic_ioport_read, 1);
803 register_ioport_write(0xa0, 2, pic_ioport_write, 1);
804 register_ioport_read(0xa0, 2, pic_ioport_read, 1);
807 /***********************************************************/
808 /* 8253 PIT emulation */
810 #define PIT_FREQ 1193182
812 #define RW_STATE_LSB 0
813 #define RW_STATE_MSB 1
814 #define RW_STATE_WORD0 2
815 #define RW_STATE_WORD1 3
816 #define RW_STATE_LATCHED_WORD0 4
817 #define RW_STATE_LATCHED_WORD1 5
819 typedef struct PITChannelState {
820 int count; /* can be 65536 */
821 uint16_t latched_count;
824 uint8_t bcd; /* not supported */
825 uint8_t gate; /* timer start */
826 int64_t count_load_time;
827 int64_t count_last_edge_check_time;
830 PITChannelState pit_channels[3];
832 int dummy_refresh_clock;
833 int pit_min_timer_count = 0;
835 int64_t get_clock(void)
838 gettimeofday(&tv, NULL);
839 return tv.tv_sec * 1000000LL + tv.tv_usec;
842 int64_t cpu_get_ticks(void)
845 asm("rdtsc" : "=A" (val));
849 void cpu_calibrate_ticks(void)
854 ticks = cpu_get_ticks();
856 usec = get_clock() - usec;
857 ticks = cpu_get_ticks() - ticks;
858 ticks_per_sec = (ticks * 1000000LL + (usec >> 1)) / usec;
861 /* compute with 96 bit intermediate result: (a*b)/c */
862 static uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
867 #ifdef WORDS_BIGENDIAN
877 rl = (uint64_t)u.l.low * (uint64_t)b;
878 rh = (uint64_t)u.l.high * (uint64_t)b;
881 res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c;
885 static int pit_get_count(PITChannelState *s)
890 d = muldiv64(cpu_get_ticks() - s->count_load_time, PIT_FREQ, ticks_per_sec);
896 counter = (s->count - d) & 0xffff;
899 counter = s->count - (d % s->count);
905 /* get pit output bit */
906 static int pit_get_out(PITChannelState *s)
911 d = muldiv64(cpu_get_ticks() - s->count_load_time, PIT_FREQ, ticks_per_sec);
915 out = (d >= s->count);
918 out = (d < s->count);
921 if ((d % s->count) == 0 && d != 0)
927 out = (d % s->count) < (s->count >> 1);
931 out = (d == s->count);
937 /* get the number of 0 to 1 transitions we had since we call this
939 /* XXX: maybe better to use ticks precision to avoid getting edges
940 twice if checks are done at very small intervals */
941 static int pit_get_out_edges(PITChannelState *s)
947 ticks = cpu_get_ticks();
948 d1 = muldiv64(s->count_last_edge_check_time - s->count_load_time,
949 PIT_FREQ, ticks_per_sec);
950 d2 = muldiv64(ticks - s->count_load_time,
951 PIT_FREQ, ticks_per_sec);
952 s->count_last_edge_check_time = ticks;
956 if (d1 < s->count && d2 >= s->count)
970 v = s->count - (s->count >> 1);
971 d1 = (d1 + v) / s->count;
972 d2 = (d2 + v) / s->count;
977 if (d1 < s->count && d2 >= s->count)
986 static inline void pit_load_count(PITChannelState *s, int val)
990 s->count_load_time = cpu_get_ticks();
991 s->count_last_edge_check_time = s->count_load_time;
993 if (s == &pit_channels[0] && val <= pit_min_timer_count) {
995 "\nWARNING: vl: on your system, accurate timer emulation is impossible if its frequency is more than %d Hz. If using a 2.5.xx Linux kernel, you must patch asm/param.h to change HZ from 1000 to 100.\n\n",
996 PIT_FREQ / pit_min_timer_count);
1000 void pit_ioport_write(CPUX86State *env, uint32_t addr, uint32_t val)
1002 int channel, access;
1010 s = &pit_channels[channel];
1011 access = (val >> 4) & 3;
1014 s->latched_count = pit_get_count(s);
1015 s->rw_state = RW_STATE_LATCHED_WORD0;
1018 s->mode = (val >> 1) & 7;
1020 s->rw_state = access - 1 + RW_STATE_LSB;
1024 s = &pit_channels[addr];
1025 switch(s->rw_state) {
1027 pit_load_count(s, val);
1030 pit_load_count(s, val << 8);
1032 case RW_STATE_WORD0:
1033 case RW_STATE_WORD1:
1034 if (s->rw_state & 1) {
1035 pit_load_count(s, (s->latched_count & 0xff) | (val << 8));
1037 s->latched_count = val;
1045 uint32_t pit_ioport_read(CPUX86State *env, uint32_t addr)
1051 s = &pit_channels[addr];
1052 switch(s->rw_state) {
1055 case RW_STATE_WORD0:
1056 case RW_STATE_WORD1:
1057 count = pit_get_count(s);
1058 if (s->rw_state & 1)
1059 ret = (count >> 8) & 0xff;
1062 if (s->rw_state & 2)
1066 case RW_STATE_LATCHED_WORD0:
1067 case RW_STATE_LATCHED_WORD1:
1068 if (s->rw_state & 1)
1069 ret = s->latched_count >> 8;
1071 ret = s->latched_count & 0xff;
1078 void speaker_ioport_write(CPUX86State *env, uint32_t addr, uint32_t val)
1080 speaker_data_on = (val >> 1) & 1;
1081 pit_channels[2].gate = val & 1;
1084 uint32_t speaker_ioport_read(CPUX86State *env, uint32_t addr)
1087 out = pit_get_out(&pit_channels[2]);
1088 dummy_refresh_clock ^= 1;
1089 return (speaker_data_on << 1) | pit_channels[2].gate | (out << 5) |
1090 (dummy_refresh_clock << 4);
1098 cpu_calibrate_ticks();
1100 for(i = 0;i < 3; i++) {
1101 s = &pit_channels[i];
1104 pit_load_count(s, 0);
1107 register_ioport_write(0x40, 4, pit_ioport_write, 1);
1108 register_ioport_read(0x40, 3, pit_ioport_read, 1);
1110 register_ioport_read(0x61, 1, speaker_ioport_read, 1);
1111 register_ioport_write(0x61, 1, speaker_ioport_write, 1);
1114 /***********************************************************/
1115 /* serial port emulation */
1119 #define UART_LCR_DLAB 0x80 /* Divisor latch access bit */
1121 #define UART_IER_MSI 0x08 /* Enable Modem status interrupt */
1122 #define UART_IER_RLSI 0x04 /* Enable receiver line status interrupt */
1123 #define UART_IER_THRI 0x02 /* Enable Transmitter holding register int. */
1124 #define UART_IER_RDI 0x01 /* Enable receiver data interrupt */
1126 #define UART_IIR_NO_INT 0x01 /* No interrupts pending */
1127 #define UART_IIR_ID 0x06 /* Mask for the interrupt ID */
1129 #define UART_IIR_MSI 0x00 /* Modem status interrupt */
1130 #define UART_IIR_THRI 0x02 /* Transmitter holding register empty */
1131 #define UART_IIR_RDI 0x04 /* Receiver data interrupt */
1132 #define UART_IIR_RLSI 0x06 /* Receiver line status interrupt */
1134 #define UART_LSR_TEMT 0x40 /* Transmitter empty */
1135 #define UART_LSR_THRE 0x20 /* Transmit-hold-register empty */
1136 #define UART_LSR_BI 0x10 /* Break interrupt indicator */
1137 #define UART_LSR_FE 0x08 /* Frame error indicator */
1138 #define UART_LSR_PE 0x04 /* Parity error indicator */
1139 #define UART_LSR_OE 0x02 /* Overrun error indicator */
1140 #define UART_LSR_DR 0x01 /* Receiver data ready */
1142 typedef struct SerialState {
1144 uint8_t rbr; /* receive register */
1146 uint8_t iir; /* read only */
1149 uint8_t lsr; /* read only */
1154 SerialState serial_ports[1];
1156 void serial_update_irq(void)
1158 SerialState *s = &serial_ports[0];
1160 if ((s->lsr & UART_LSR_DR) && (s->ier & UART_IER_RDI)) {
1161 s->iir = UART_IIR_RDI;
1162 } else if ((s->lsr & UART_LSR_THRE) && (s->ier & UART_IER_THRI)) {
1163 s->iir = UART_IIR_THRI;
1165 s->iir = UART_IIR_NO_INT;
1167 if (s->iir != UART_IIR_NO_INT) {
1168 pic_set_irq(UART_IRQ, 1);
1170 pic_set_irq(UART_IRQ, 0);
1174 void serial_ioport_write(CPUX86State *env, uint32_t addr, uint32_t val)
1176 SerialState *s = &serial_ports[0];
1184 if (s->lcr & UART_LCR_DLAB) {
1185 s->divider = (s->divider & 0xff00) | val;
1187 s->lsr &= ~UART_LSR_THRE;
1188 serial_update_irq();
1192 ret = write(1, &ch, 1);
1194 s->lsr |= UART_LSR_THRE;
1195 s->lsr |= UART_LSR_TEMT;
1196 serial_update_irq();
1200 if (s->lcr & UART_LCR_DLAB) {
1201 s->divider = (s->divider & 0x00ff) | (val << 8);
1204 serial_update_irq();
1226 uint32_t serial_ioport_read(CPUX86State *env, uint32_t addr)
1228 SerialState *s = &serial_ports[0];
1235 if (s->lcr & UART_LCR_DLAB) {
1236 ret = s->divider & 0xff;
1239 s->lsr &= ~(UART_LSR_DR | UART_LSR_BI);
1240 serial_update_irq();
1244 if (s->lcr & UART_LCR_DLAB) {
1245 ret = (s->divider >> 8) & 0xff;
1272 #define TERM_ESCAPE 0x01 /* ctrl-a is used for escape */
1273 static int term_got_escape;
1275 void term_print_help(void)
1278 "C-a h print this help\n"
1279 "C-a x exit emulatior\n"
1280 "C-a s save disk data back to file (if -snapshot)\n"
1281 "C-a b send break (magic sysrq)\n"
1282 "C-a C-a send C-a\n"
1286 /* called when a char is received */
1287 void serial_received_byte(SerialState *s, int ch)
1289 if (term_got_escape) {
1290 term_got_escape = 0;
1301 for (i = 0; i < MAX_DISKS; i++) {
1303 bdrv_commit(bs_table[i]);
1310 s->lsr |= UART_LSR_BI | UART_LSR_DR;
1311 serial_update_irq();
1316 } else if (ch == TERM_ESCAPE) {
1317 term_got_escape = 1;
1321 s->lsr |= UART_LSR_DR;
1322 serial_update_irq();
1326 void serial_init(void)
1328 SerialState *s = &serial_ports[0];
1330 s->lsr = UART_LSR_TEMT | UART_LSR_THRE;
1332 register_ioport_write(0x3f8, 8, serial_ioport_write, 1);
1333 register_ioport_read(0x3f8, 8, serial_ioport_read, 1);
1336 /***********************************************************/
1337 /* ne2000 emulation */
1339 #define NE2000_IOPORT 0x300
1340 #define NE2000_IRQ 9
1342 #define MAX_ETH_FRAME_SIZE 1514
1344 #define E8390_CMD 0x00 /* The command register (for all pages) */
1345 /* Page 0 register offsets. */
1346 #define EN0_CLDALO 0x01 /* Low byte of current local dma addr RD */
1347 #define EN0_STARTPG 0x01 /* Starting page of ring bfr WR */
1348 #define EN0_CLDAHI 0x02 /* High byte of current local dma addr RD */
1349 #define EN0_STOPPG 0x02 /* Ending page +1 of ring bfr WR */
1350 #define EN0_BOUNDARY 0x03 /* Boundary page of ring bfr RD WR */
1351 #define EN0_TSR 0x04 /* Transmit status reg RD */
1352 #define EN0_TPSR 0x04 /* Transmit starting page WR */
1353 #define EN0_NCR 0x05 /* Number of collision reg RD */
1354 #define EN0_TCNTLO 0x05 /* Low byte of tx byte count WR */
1355 #define EN0_FIFO 0x06 /* FIFO RD */
1356 #define EN0_TCNTHI 0x06 /* High byte of tx byte count WR */
1357 #define EN0_ISR 0x07 /* Interrupt status reg RD WR */
1358 #define EN0_CRDALO 0x08 /* low byte of current remote dma address RD */
1359 #define EN0_RSARLO 0x08 /* Remote start address reg 0 */
1360 #define EN0_CRDAHI 0x09 /* high byte, current remote dma address RD */
1361 #define EN0_RSARHI 0x09 /* Remote start address reg 1 */
1362 #define EN0_RCNTLO 0x0a /* Remote byte count reg WR */
1363 #define EN0_RCNTHI 0x0b /* Remote byte count reg WR */
1364 #define EN0_RSR 0x0c /* rx status reg RD */
1365 #define EN0_RXCR 0x0c /* RX configuration reg WR */
1366 #define EN0_TXCR 0x0d /* TX configuration reg WR */
1367 #define EN0_COUNTER0 0x0d /* Rcv alignment error counter RD */
1368 #define EN0_DCFG 0x0e /* Data configuration reg WR */
1369 #define EN0_COUNTER1 0x0e /* Rcv CRC error counter RD */
1370 #define EN0_IMR 0x0f /* Interrupt mask reg WR */
1371 #define EN0_COUNTER2 0x0f /* Rcv missed frame error counter RD */
1373 #define EN1_PHYS 0x11
1374 #define EN1_CURPAG 0x17
1375 #define EN1_MULT 0x18
1377 /* Register accessed at EN_CMD, the 8390 base addr. */
1378 #define E8390_STOP 0x01 /* Stop and reset the chip */
1379 #define E8390_START 0x02 /* Start the chip, clear reset */
1380 #define E8390_TRANS 0x04 /* Transmit a frame */
1381 #define E8390_RREAD 0x08 /* Remote read */
1382 #define E8390_RWRITE 0x10 /* Remote write */
1383 #define E8390_NODMA 0x20 /* Remote DMA */
1384 #define E8390_PAGE0 0x00 /* Select page chip registers */
1385 #define E8390_PAGE1 0x40 /* using the two high-order bits */
1386 #define E8390_PAGE2 0x80 /* Page 3 is invalid. */
1388 /* Bits in EN0_ISR - Interrupt status register */
1389 #define ENISR_RX 0x01 /* Receiver, no error */
1390 #define ENISR_TX 0x02 /* Transmitter, no error */
1391 #define ENISR_RX_ERR 0x04 /* Receiver, with error */
1392 #define ENISR_TX_ERR 0x08 /* Transmitter, with error */
1393 #define ENISR_OVER 0x10 /* Receiver overwrote the ring */
1394 #define ENISR_COUNTERS 0x20 /* Counters need emptying */
1395 #define ENISR_RDC 0x40 /* remote dma complete */
1396 #define ENISR_RESET 0x80 /* Reset completed */
1397 #define ENISR_ALL 0x3f /* Interrupts we will enable */
1399 /* Bits in received packet status byte and EN0_RSR*/
1400 #define ENRSR_RXOK 0x01 /* Received a good packet */
1401 #define ENRSR_CRC 0x02 /* CRC error */
1402 #define ENRSR_FAE 0x04 /* frame alignment error */
1403 #define ENRSR_FO 0x08 /* FIFO overrun */
1404 #define ENRSR_MPA 0x10 /* missed pkt */
1405 #define ENRSR_PHY 0x20 /* physical/multicast address */
1406 #define ENRSR_DIS 0x40 /* receiver disable. set in monitor mode */
1407 #define ENRSR_DEF 0x80 /* deferring */
1409 /* Transmitted packet status, EN0_TSR. */
1410 #define ENTSR_PTX 0x01 /* Packet transmitted without error */
1411 #define ENTSR_ND 0x02 /* The transmit wasn't deferred. */
1412 #define ENTSR_COL 0x04 /* The transmit collided at least once. */
1413 #define ENTSR_ABT 0x08 /* The transmit collided 16 times, and was deferred. */
1414 #define ENTSR_CRS 0x10 /* The carrier sense was lost. */
1415 #define ENTSR_FU 0x20 /* A "FIFO underrun" occurred during transmit. */
1416 #define ENTSR_CDH 0x40 /* The collision detect "heartbeat" signal was lost. */
1417 #define ENTSR_OWC 0x80 /* There was an out-of-window collision. */
1419 #define NE2000_MEM_SIZE 32768
1421 typedef struct NE2000State {
1434 uint8_t phys[6]; /* mac address */
1436 uint8_t mult[8]; /* multicast mask array */
1437 uint8_t mem[NE2000_MEM_SIZE];
1440 NE2000State ne2000_state;
1442 char network_script[1024];
1444 void ne2000_reset(void)
1446 NE2000State *s = &ne2000_state;
1449 s->isr = ENISR_RESET;
1459 /* duplicate prom data */
1460 for(i = 15;i >= 0; i--) {
1461 s->mem[2 * i] = s->mem[i];
1462 s->mem[2 * i + 1] = s->mem[i];
1466 void ne2000_update_irq(NE2000State *s)
1469 isr = s->isr & s->imr;
1471 pic_set_irq(NE2000_IRQ, 1);
1473 pic_set_irq(NE2000_IRQ, 0);
1479 int fd, ret, pid, status;
1481 fd = open("/dev/net/tun", O_RDWR);
1483 fprintf(stderr, "warning: could not open /dev/net/tun: no virtual network emulation\n");
1486 memset(&ifr, 0, sizeof(ifr));
1487 ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
1488 pstrcpy(ifr.ifr_name, IFNAMSIZ, "tun%d");
1489 ret = ioctl(fd, TUNSETIFF, (void *) &ifr);
1491 fprintf(stderr, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
1495 printf("Connected to host network interface: %s\n", ifr.ifr_name);
1496 fcntl(fd, F_SETFL, O_NONBLOCK);
1499 /* try to launch network init script */
1503 execl(network_script, network_script, ifr.ifr_name, NULL);
1506 while (waitpid(pid, &status, 0) != pid);
1507 if (!WIFEXITED(status) ||
1508 WEXITSTATUS(status) != 0) {
1509 fprintf(stderr, "%s: could not launch network script for '%s'\n",
1510 network_script, ifr.ifr_name);
1516 void net_send_packet(NE2000State *s, const uint8_t *buf, int size)
1519 printf("NE2000: sending packet size=%d\n", size);
1521 write(net_fd, buf, size);
1524 /* return true if the NE2000 can receive more data */
1525 int ne2000_can_receive(NE2000State *s)
1527 int avail, index, boundary;
1529 if (s->cmd & E8390_STOP)
1531 index = s->curpag << 8;
1532 boundary = s->boundary << 8;
1533 if (index < boundary)
1534 avail = boundary - index;
1536 avail = (s->stop - s->start) - (index - boundary);
1537 if (avail < (MAX_ETH_FRAME_SIZE + 4))
1542 void ne2000_receive(NE2000State *s, uint8_t *buf, int size)
1545 int total_len, next, avail, len, index;
1547 #if defined(DEBUG_NE2000)
1548 printf("NE2000: received len=%d\n", size);
1551 index = s->curpag << 8;
1552 /* 4 bytes for header */
1553 total_len = size + 4;
1554 /* address for next packet (4 bytes for CRC) */
1555 next = index + ((total_len + 4 + 255) & ~0xff);
1556 if (next >= s->stop)
1557 next -= (s->stop - s->start);
1558 /* prepare packet header */
1560 p[0] = ENRSR_RXOK; /* receive status */
1563 p[3] = total_len >> 8;
1566 /* write packet data */
1568 avail = s->stop - index;
1572 memcpy(s->mem + index, buf, len);
1575 if (index == s->stop)
1579 s->curpag = next >> 8;
1581 /* now we can signal we have receive something */
1583 ne2000_update_irq(s);
1586 void ne2000_ioport_write(CPUX86State *env, uint32_t addr, uint32_t val)
1588 NE2000State *s = &ne2000_state;
1593 printf("NE2000: write addr=0x%x val=0x%02x\n", addr, val);
1595 if (addr == E8390_CMD) {
1596 /* control register */
1598 if (val & E8390_START) {
1599 /* test specific case: zero length transfert */
1600 if ((val & (E8390_RREAD | E8390_RWRITE)) &&
1602 s->isr |= ENISR_RDC;
1603 ne2000_update_irq(s);
1605 if (val & E8390_TRANS) {
1606 net_send_packet(s, s->mem + (s->tpsr << 8), s->tcnt);
1607 /* signal end of transfert */
1610 ne2000_update_irq(s);
1615 offset = addr | (page << 4);
1618 s->start = val << 8;
1628 ne2000_update_irq(s);
1634 s->tcnt = (s->tcnt & 0xff00) | val;
1637 s->tcnt = (s->tcnt & 0x00ff) | (val << 8);
1640 s->rsar = (s->rsar & 0xff00) | val;
1643 s->rsar = (s->rsar & 0x00ff) | (val << 8);
1646 s->rcnt = (s->rcnt & 0xff00) | val;
1649 s->rcnt = (s->rcnt & 0x00ff) | (val << 8);
1656 ne2000_update_irq(s);
1658 case EN1_PHYS ... EN1_PHYS + 5:
1659 s->phys[offset - EN1_PHYS] = val;
1664 case EN1_MULT ... EN1_MULT + 7:
1665 s->mult[offset - EN1_MULT] = val;
1671 uint32_t ne2000_ioport_read(CPUX86State *env, uint32_t addr)
1673 NE2000State *s = &ne2000_state;
1674 int offset, page, ret;
1677 if (addr == E8390_CMD) {
1681 offset = addr | (page << 4);
1692 case EN1_PHYS ... EN1_PHYS + 5:
1693 ret = s->phys[offset - EN1_PHYS];
1698 case EN1_MULT ... EN1_MULT + 7:
1699 ret = s->mult[offset - EN1_MULT];
1707 printf("NE2000: read addr=0x%x val=%02x\n", addr, ret);
1712 void ne2000_asic_ioport_write(CPUX86State *env, uint32_t addr, uint32_t val)
1714 NE2000State *s = &ne2000_state;
1718 printf("NE2000: asic write val=0x%04x\n", val);
1720 p = s->mem + s->rsar;
1721 if (s->dcfg & 0x01) {
1734 if (s->rsar == s->stop)
1737 /* signal end of transfert */
1738 s->isr |= ENISR_RDC;
1739 ne2000_update_irq(s);
1743 uint32_t ne2000_asic_ioport_read(CPUX86State *env, uint32_t addr)
1745 NE2000State *s = &ne2000_state;
1749 p = s->mem + s->rsar;
1750 if (s->dcfg & 0x01) {
1752 ret = p[0] | (p[1] << 8);
1762 if (s->rsar == s->stop)
1765 /* signal end of transfert */
1766 s->isr |= ENISR_RDC;
1767 ne2000_update_irq(s);
1770 printf("NE2000: asic read val=0x%04x\n", ret);
1775 void ne2000_reset_ioport_write(CPUX86State *env, uint32_t addr, uint32_t val)
1777 /* nothing to do (end of reset pulse) */
1780 uint32_t ne2000_reset_ioport_read(CPUX86State *env, uint32_t addr)
1786 void ne2000_init(void)
1788 register_ioport_write(NE2000_IOPORT, 16, ne2000_ioport_write, 1);
1789 register_ioport_read(NE2000_IOPORT, 16, ne2000_ioport_read, 1);
1791 register_ioport_write(NE2000_IOPORT + 0x10, 1, ne2000_asic_ioport_write, 1);
1792 register_ioport_read(NE2000_IOPORT + 0x10, 1, ne2000_asic_ioport_read, 1);
1793 register_ioport_write(NE2000_IOPORT + 0x10, 2, ne2000_asic_ioport_write, 2);
1794 register_ioport_read(NE2000_IOPORT + 0x10, 2, ne2000_asic_ioport_read, 2);
1796 register_ioport_write(NE2000_IOPORT + 0x1f, 1, ne2000_reset_ioport_write, 1);
1797 register_ioport_read(NE2000_IOPORT + 0x1f, 1, ne2000_reset_ioport_read, 1);
1801 /***********************************************************/
1804 /* Bits of HD_STATUS */
1805 #define ERR_STAT 0x01
1806 #define INDEX_STAT 0x02
1807 #define ECC_STAT 0x04 /* Corrected error */
1808 #define DRQ_STAT 0x08
1809 #define SEEK_STAT 0x10
1810 #define SRV_STAT 0x10
1811 #define WRERR_STAT 0x20
1812 #define READY_STAT 0x40
1813 #define BUSY_STAT 0x80
1815 /* Bits for HD_ERROR */
1816 #define MARK_ERR 0x01 /* Bad address mark */
1817 #define TRK0_ERR 0x02 /* couldn't find track 0 */
1818 #define ABRT_ERR 0x04 /* Command aborted */
1819 #define MCR_ERR 0x08 /* media change request */
1820 #define ID_ERR 0x10 /* ID field not found */
1821 #define MC_ERR 0x20 /* media changed */
1822 #define ECC_ERR 0x40 /* Uncorrectable ECC error */
1823 #define BBD_ERR 0x80 /* pre-EIDE meaning: block marked bad */
1824 #define ICRC_ERR 0x80 /* new meaning: CRC error during transfer */
1826 /* Bits of HD_NSECTOR */
1830 #define TAG_MASK 0xf8
1832 #define IDE_CMD_RESET 0x04
1833 #define IDE_CMD_DISABLE_IRQ 0x02
1835 /* ATA/ATAPI Commands pre T13 Spec */
1836 #define WIN_NOP 0x00
1838 * 0x01->0x02 Reserved
1840 #define CFA_REQ_EXT_ERROR_CODE 0x03 /* CFA Request Extended Error Code */
1842 * 0x04->0x07 Reserved
1844 #define WIN_SRST 0x08 /* ATAPI soft reset command */
1845 #define WIN_DEVICE_RESET 0x08
1847 * 0x09->0x0F Reserved
1849 #define WIN_RECAL 0x10
1850 #define WIN_RESTORE WIN_RECAL
1852 * 0x10->0x1F Reserved
1854 #define WIN_READ 0x20 /* 28-Bit */
1855 #define WIN_READ_ONCE 0x21 /* 28-Bit without retries */
1856 #define WIN_READ_LONG 0x22 /* 28-Bit */
1857 #define WIN_READ_LONG_ONCE 0x23 /* 28-Bit without retries */
1858 #define WIN_READ_EXT 0x24 /* 48-Bit */
1859 #define WIN_READDMA_EXT 0x25 /* 48-Bit */
1860 #define WIN_READDMA_QUEUED_EXT 0x26 /* 48-Bit */
1861 #define WIN_READ_NATIVE_MAX_EXT 0x27 /* 48-Bit */
1865 #define WIN_MULTREAD_EXT 0x29 /* 48-Bit */
1867 * 0x2A->0x2F Reserved
1869 #define WIN_WRITE 0x30 /* 28-Bit */
1870 #define WIN_WRITE_ONCE 0x31 /* 28-Bit without retries */
1871 #define WIN_WRITE_LONG 0x32 /* 28-Bit */
1872 #define WIN_WRITE_LONG_ONCE 0x33 /* 28-Bit without retries */
1873 #define WIN_WRITE_EXT 0x34 /* 48-Bit */
1874 #define WIN_WRITEDMA_EXT 0x35 /* 48-Bit */
1875 #define WIN_WRITEDMA_QUEUED_EXT 0x36 /* 48-Bit */
1876 #define WIN_SET_MAX_EXT 0x37 /* 48-Bit */
1877 #define CFA_WRITE_SECT_WO_ERASE 0x38 /* CFA Write Sectors without erase */
1878 #define WIN_MULTWRITE_EXT 0x39 /* 48-Bit */
1880 * 0x3A->0x3B Reserved
1882 #define WIN_WRITE_VERIFY 0x3C /* 28-Bit */
1884 * 0x3D->0x3F Reserved
1886 #define WIN_VERIFY 0x40 /* 28-Bit - Read Verify Sectors */
1887 #define WIN_VERIFY_ONCE 0x41 /* 28-Bit - without retries */
1888 #define WIN_VERIFY_EXT 0x42 /* 48-Bit */
1890 * 0x43->0x4F Reserved
1892 #define WIN_FORMAT 0x50
1894 * 0x51->0x5F Reserved
1896 #define WIN_INIT 0x60
1898 * 0x61->0x5F Reserved
1900 #define WIN_SEEK 0x70 /* 0x70-0x7F Reserved */
1901 #define CFA_TRANSLATE_SECTOR 0x87 /* CFA Translate Sector */
1902 #define WIN_DIAGNOSE 0x90
1903 #define WIN_SPECIFY 0x91 /* set drive geometry translation */
1904 #define WIN_DOWNLOAD_MICROCODE 0x92
1905 #define WIN_STANDBYNOW2 0x94
1906 #define WIN_STANDBY2 0x96
1907 #define WIN_SETIDLE2 0x97
1908 #define WIN_CHECKPOWERMODE2 0x98
1909 #define WIN_SLEEPNOW2 0x99
1913 #define WIN_PACKETCMD 0xA0 /* Send a packet command. */
1914 #define WIN_PIDENTIFY 0xA1 /* identify ATAPI device */
1915 #define WIN_QUEUED_SERVICE 0xA2
1916 #define WIN_SMART 0xB0 /* self-monitoring and reporting */
1917 #define CFA_ERASE_SECTORS 0xC0
1918 #define WIN_MULTREAD 0xC4 /* read sectors using multiple mode*/
1919 #define WIN_MULTWRITE 0xC5 /* write sectors using multiple mode */
1920 #define WIN_SETMULT 0xC6 /* enable/disable multiple mode */
1921 #define WIN_READDMA_QUEUED 0xC7 /* read sectors using Queued DMA transfers */
1922 #define WIN_READDMA 0xC8 /* read sectors using DMA transfers */
1923 #define WIN_READDMA_ONCE 0xC9 /* 28-Bit - without retries */
1924 #define WIN_WRITEDMA 0xCA /* write sectors using DMA transfers */
1925 #define WIN_WRITEDMA_ONCE 0xCB /* 28-Bit - without retries */
1926 #define WIN_WRITEDMA_QUEUED 0xCC /* write sectors using Queued DMA transfers */
1927 #define CFA_WRITE_MULTI_WO_ERASE 0xCD /* CFA Write multiple without erase */
1928 #define WIN_GETMEDIASTATUS 0xDA
1929 #define WIN_ACKMEDIACHANGE 0xDB /* ATA-1, ATA-2 vendor */
1930 #define WIN_POSTBOOT 0xDC
1931 #define WIN_PREBOOT 0xDD
1932 #define WIN_DOORLOCK 0xDE /* lock door on removable drives */
1933 #define WIN_DOORUNLOCK 0xDF /* unlock door on removable drives */
1934 #define WIN_STANDBYNOW1 0xE0
1935 #define WIN_IDLEIMMEDIATE 0xE1 /* force drive to become "ready" */
1936 #define WIN_STANDBY 0xE2 /* Set device in Standby Mode */
1937 #define WIN_SETIDLE1 0xE3
1938 #define WIN_READ_BUFFER 0xE4 /* force read only 1 sector */
1939 #define WIN_CHECKPOWERMODE1 0xE5
1940 #define WIN_SLEEPNOW1 0xE6
1941 #define WIN_FLUSH_CACHE 0xE7
1942 #define WIN_WRITE_BUFFER 0xE8 /* force write only 1 sector */
1943 #define WIN_WRITE_SAME 0xE9 /* read ata-2 to use */
1944 /* SET_FEATURES 0x22 or 0xDD */
1945 #define WIN_FLUSH_CACHE_EXT 0xEA /* 48-Bit */
1946 #define WIN_IDENTIFY 0xEC /* ask drive to identify itself */
1947 #define WIN_MEDIAEJECT 0xED
1948 #define WIN_IDENTIFY_DMA 0xEE /* same as WIN_IDENTIFY, but DMA */
1949 #define WIN_SETFEATURES 0xEF /* set special drive features */
1950 #define EXABYTE_ENABLE_NEST 0xF0
1951 #define WIN_SECURITY_SET_PASS 0xF1
1952 #define WIN_SECURITY_UNLOCK 0xF2
1953 #define WIN_SECURITY_ERASE_PREPARE 0xF3
1954 #define WIN_SECURITY_ERASE_UNIT 0xF4
1955 #define WIN_SECURITY_FREEZE_LOCK 0xF5
1956 #define WIN_SECURITY_DISABLE 0xF6
1957 #define WIN_READ_NATIVE_MAX 0xF8 /* return the native maximum address */
1958 #define WIN_SET_MAX 0xF9
1959 #define DISABLE_SEAGATE 0xFB
1961 /* set to 1 set disable mult support */
1962 #define MAX_MULT_SECTORS 8
1966 typedef void EndTransferFunc(struct IDEState *);
1968 typedef struct IDEState {
1970 int cylinders, heads, sectors;
1977 uint16_t nsector; /* 0 is 256 to ease computations */
1983 /* 0x3f6 command, only meaningful for drive 0 */
1985 /* depends on bit 4 in select, only meaningful for drive 0 */
1986 struct IDEState *cur_drive;
1987 BlockDriverState *bs;
1988 int req_nb_sectors; /* number of sectors per interrupt */
1989 EndTransferFunc *end_transfer_func;
1992 uint8_t io_buffer[MAX_MULT_SECTORS*512 + 4];
1995 IDEState ide_state[MAX_DISKS];
1997 static void padstr(char *str, const char *src, int len)
2000 for(i = 0; i < len; i++) {
2005 *(char *)((long)str ^ 1) = v;
2010 static void ide_identify(IDEState *s)
2013 unsigned int oldsize;
2015 memset(s->io_buffer, 0, 512);
2016 p = (uint16_t *)s->io_buffer;
2018 stw(p + 1, s->cylinders);
2019 stw(p + 3, s->heads);
2020 stw(p + 4, 512 * s->sectors); /* sectors */
2021 stw(p + 5, 512); /* sector size */
2022 stw(p + 6, s->sectors);
2023 stw(p + 20, 3); /* buffer type */
2024 stw(p + 21, 512); /* cache size in sectors */
2025 stw(p + 22, 4); /* ecc bytes */
2026 padstr((uint8_t *)(p + 27), "QEMU HARDDISK", 40);
2027 #if MAX_MULT_SECTORS > 1
2028 stw(p + 47, MAX_MULT_SECTORS);
2030 stw(p + 48, 1); /* dword I/O */
2031 stw(p + 49, 1 << 9); /* LBA supported, no DMA */
2032 stw(p + 51, 0x200); /* PIO transfer cycle */
2033 stw(p + 52, 0x200); /* DMA transfer cycle */
2034 stw(p + 54, s->cylinders);
2035 stw(p + 55, s->heads);
2036 stw(p + 56, s->sectors);
2037 oldsize = s->cylinders * s->heads * s->sectors;
2038 stw(p + 57, oldsize);
2039 stw(p + 58, oldsize >> 16);
2040 if (s->mult_sectors)
2041 stw(p + 59, 0x100 | s->mult_sectors);
2042 stw(p + 60, s->nb_sectors);
2043 stw(p + 61, s->nb_sectors >> 16);
2044 stw(p + 80, (1 << 1) | (1 << 2));
2045 stw(p + 82, (1 << 14));
2046 stw(p + 83, (1 << 14));
2047 stw(p + 84, (1 << 14));
2048 stw(p + 85, (1 << 14));
2050 stw(p + 87, (1 << 14));
2053 static inline void ide_abort_command(IDEState *s)
2055 s->status = READY_STAT | ERR_STAT;
2056 s->error = ABRT_ERR;
2059 static inline void ide_set_irq(IDEState *s)
2061 if (!(ide_state[0].cmd & IDE_CMD_DISABLE_IRQ)) {
2062 pic_set_irq(s->irq, 1);
2066 /* prepare data transfer and tell what to do after */
2067 static void ide_transfer_start(IDEState *s, int size,
2068 EndTransferFunc *end_transfer_func)
2070 s->end_transfer_func = end_transfer_func;
2071 s->data_ptr = s->io_buffer;
2072 s->data_end = s->io_buffer + size;
2073 s->status |= DRQ_STAT;
2076 static void ide_transfer_stop(IDEState *s)
2078 s->end_transfer_func = ide_transfer_stop;
2079 s->data_ptr = s->io_buffer;
2080 s->data_end = s->io_buffer;
2081 s->status &= ~DRQ_STAT;
2084 static int64_t ide_get_sector(IDEState *s)
2087 if (s->select & 0x40) {
2089 sector_num = ((s->select & 0x0f) << 24) | (s->hcyl << 16) |
2090 (s->lcyl << 8) | s->sector;
2092 sector_num = ((s->hcyl << 8) | s->lcyl) * s->heads * s->sectors +
2093 (s->select & 0x0f) * s->sectors +
2099 static void ide_set_sector(IDEState *s, int64_t sector_num)
2101 unsigned int cyl, r;
2102 if (s->select & 0x40) {
2103 s->select = (s->select & 0xf0) | (sector_num >> 24);
2104 s->hcyl = (sector_num >> 16);
2105 s->lcyl = (sector_num >> 8);
2106 s->sector = (sector_num);
2108 cyl = sector_num / (s->heads * s->sectors);
2109 r = sector_num % (s->heads * s->sectors);
2112 s->select = (s->select & 0xf0) | (r / s->sectors);
2113 s->sector = (r % s->sectors) + 1;
2117 static void ide_sector_read(IDEState *s)
2122 s->status = READY_STAT | SEEK_STAT;
2123 sector_num = ide_get_sector(s);
2126 /* no more sector to read from disk */
2127 ide_transfer_stop(s);
2129 #if defined(DEBUG_IDE)
2130 printf("read sector=%Ld\n", sector_num);
2132 if (n > s->req_nb_sectors)
2133 n = s->req_nb_sectors;
2134 ret = bdrv_read(s->bs, sector_num, s->io_buffer, n);
2135 ide_transfer_start(s, 512 * n, ide_sector_read);
2137 ide_set_sector(s, sector_num + n);
2142 static void ide_sector_write(IDEState *s)
2147 s->status = READY_STAT | SEEK_STAT;
2148 sector_num = ide_get_sector(s);
2149 #if defined(DEBUG_IDE)
2150 printf("write sector=%Ld\n", sector_num);
2153 if (n > s->req_nb_sectors)
2154 n = s->req_nb_sectors;
2155 ret = bdrv_write(s->bs, sector_num, s->io_buffer, n);
2157 if (s->nsector == 0) {
2158 /* no more sector to write */
2159 ide_transfer_stop(s);
2162 if (n1 > s->req_nb_sectors)
2163 n1 = s->req_nb_sectors;
2164 ide_transfer_start(s, 512 * n1, ide_sector_write);
2166 ide_set_sector(s, sector_num + n);
2170 void ide_ioport_write(CPUX86State *env, uint32_t addr, uint32_t val)
2172 IDEState *s = ide_state[0].cur_drive;
2177 printf("IDE: write addr=0x%x val=0x%02x\n", addr, val);
2201 unit = (val >> 4) & 1;
2202 s = &ide_state[unit];
2203 ide_state[0].cur_drive = s;
2209 #if defined(DEBUG_IDE)
2210 printf("ide: CMD=%02x\n", val);
2217 s->status = READY_STAT;
2218 ide_transfer_start(s, 512, ide_transfer_stop);
2220 ide_abort_command(s);
2226 s->status = READY_STAT;
2230 if (s->nsector > MAX_MULT_SECTORS ||
2232 (s->nsector & (s->nsector - 1)) != 0) {
2233 ide_abort_command(s);
2235 s->mult_sectors = s->nsector;
2236 s->status = READY_STAT;
2242 s->req_nb_sectors = 1;
2246 case WIN_WRITE_ONCE:
2247 s->status = SEEK_STAT;
2248 s->req_nb_sectors = 1;
2249 ide_transfer_start(s, 512, ide_sector_write);
2252 if (!s->mult_sectors)
2254 s->req_nb_sectors = s->mult_sectors;
2258 if (!s->mult_sectors)
2260 s->status = SEEK_STAT;
2261 s->req_nb_sectors = s->mult_sectors;
2263 if (n > s->req_nb_sectors)
2264 n = s->req_nb_sectors;
2265 ide_transfer_start(s, 512 * n, ide_sector_write);
2267 case WIN_READ_NATIVE_MAX:
2268 ide_set_sector(s, s->nb_sectors - 1);
2269 s->status = READY_STAT;
2274 ide_abort_command(s);
2281 uint32_t ide_ioport_read(CPUX86State *env, uint32_t addr)
2283 IDEState *s = ide_state[0].cur_drive;
2295 ret = s->nsector & 0xff;
2312 pic_set_irq(s->irq, 0);
2316 printf("ide: read addr=0x%x val=%02x\n", addr, ret);
2321 uint32_t ide_status_read(CPUX86State *env, uint32_t addr)
2323 IDEState *s = ide_state[0].cur_drive;
2327 printf("ide: read status val=%02x\n", ret);
2332 void ide_cmd_write(CPUX86State *env, uint32_t addr, uint32_t val)
2338 printf("ide: write control val=%02x\n", val);
2340 /* common for both drives */
2341 if (!(ide_state[0].cmd & IDE_CMD_RESET) &&
2342 (val & IDE_CMD_RESET)) {
2343 /* reset low to high */
2344 for(i = 0;i < 2; i++) {
2346 s->status = BUSY_STAT | SEEK_STAT;
2349 } else if ((ide_state[0].cmd & IDE_CMD_RESET) &&
2350 !(val & IDE_CMD_RESET)) {
2352 for(i = 0;i < 2; i++) {
2354 s->status = READY_STAT;
2355 /* set hard disk drive ID */
2356 s->select &= 0xf0; /* clear head */
2359 if (s->nb_sectors == 0) {
2360 /* no disk present */
2370 ide_state[0].cmd = val;
2373 void ide_data_writew(CPUX86State *env, uint32_t addr, uint32_t val)
2375 IDEState *s = ide_state[0].cur_drive;
2379 *(uint16_t *)p = tswap16(val);
2382 if (p >= s->data_end)
2383 s->end_transfer_func(s);
2386 uint32_t ide_data_readw(CPUX86State *env, uint32_t addr)
2388 IDEState *s = ide_state[0].cur_drive;
2393 ret = tswap16(*(uint16_t *)p);
2396 if (p >= s->data_end)
2397 s->end_transfer_func(s);
2401 void ide_data_writel(CPUX86State *env, uint32_t addr, uint32_t val)
2403 IDEState *s = ide_state[0].cur_drive;
2407 *(uint32_t *)p = tswap32(val);
2410 if (p >= s->data_end)
2411 s->end_transfer_func(s);
2414 uint32_t ide_data_readl(CPUX86State *env, uint32_t addr)
2416 IDEState *s = ide_state[0].cur_drive;
2421 ret = tswap32(*(uint32_t *)p);
2424 if (p >= s->data_end)
2425 s->end_transfer_func(s);
2429 void ide_reset(IDEState *s)
2431 s->mult_sectors = MAX_MULT_SECTORS;
2432 s->status = READY_STAT;
2443 for(i = 0; i < MAX_DISKS; i++) {
2445 s->bs = bs_table[i];
2447 bdrv_get_geometry(s->bs, &nb_sectors);
2448 if (s->cylinders == 0) {
2449 /* if no geometry, use a LBA compatible one */
2450 cylinders = nb_sectors / (16 * 63);
2451 if (cylinders > 16383)
2453 else if (cylinders < 2)
2455 s->cylinders = cylinders;
2459 s->nb_sectors = nb_sectors;
2464 register_ioport_write(0x1f0, 8, ide_ioport_write, 1);
2465 register_ioport_read(0x1f0, 8, ide_ioport_read, 1);
2466 register_ioport_read(0x3f6, 1, ide_status_read, 1);
2467 register_ioport_write(0x3f6, 1, ide_cmd_write, 1);
2470 register_ioport_write(0x1f0, 2, ide_data_writew, 2);
2471 register_ioport_read(0x1f0, 2, ide_data_readw, 2);
2472 register_ioport_write(0x1f0, 4, ide_data_writel, 4);
2473 register_ioport_read(0x1f0, 4, ide_data_readl, 4);
2476 /***********************************************************/
2477 /* keyboard emulation */
2479 /* Keyboard Controller Commands */
2480 #define KBD_CCMD_READ_MODE 0x20 /* Read mode bits */
2481 #define KBD_CCMD_WRITE_MODE 0x60 /* Write mode bits */
2482 #define KBD_CCMD_GET_VERSION 0xA1 /* Get controller version */
2483 #define KBD_CCMD_MOUSE_DISABLE 0xA7 /* Disable mouse interface */
2484 #define KBD_CCMD_MOUSE_ENABLE 0xA8 /* Enable mouse interface */
2485 #define KBD_CCMD_TEST_MOUSE 0xA9 /* Mouse interface test */
2486 #define KBD_CCMD_SELF_TEST 0xAA /* Controller self test */
2487 #define KBD_CCMD_KBD_TEST 0xAB /* Keyboard interface test */
2488 #define KBD_CCMD_KBD_DISABLE 0xAD /* Keyboard interface disable */
2489 #define KBD_CCMD_KBD_ENABLE 0xAE /* Keyboard interface enable */
2490 #define KBD_CCMD_READ_INPORT 0xC0 /* read input port */
2491 #define KBD_CCMD_READ_OUTPORT 0xD0 /* read output port */
2492 #define KBD_CCMD_WRITE_OUTPORT 0xD1 /* write output port */
2493 #define KBD_CCMD_WRITE_OBUF 0xD2
2494 #define KBD_CCMD_WRITE_AUX_OBUF 0xD3 /* Write to output buffer as if
2495 initiated by the auxiliary device */
2496 #define KBD_CCMD_WRITE_MOUSE 0xD4 /* Write the following byte to the mouse */
2497 #define KBD_CCMD_ENABLE_A20 0xDD
2498 #define KBD_CCMD_DISABLE_A20 0xDF
2499 #define KBD_CCMD_RESET 0xFE
2501 /* Keyboard Commands */
2502 #define KBD_CMD_SET_LEDS 0xED /* Set keyboard leds */
2503 #define KBD_CMD_ECHO 0xEE
2504 #define KBD_CMD_SET_RATE 0xF3 /* Set typematic rate */
2505 #define KBD_CMD_ENABLE 0xF4 /* Enable scanning */
2506 #define KBD_CMD_RESET_DISABLE 0xF5 /* reset and disable scanning */
2507 #define KBD_CMD_RESET_ENABLE 0xF6 /* reset and enable scanning */
2508 #define KBD_CMD_RESET 0xFF /* Reset */
2510 /* Keyboard Replies */
2511 #define KBD_REPLY_POR 0xAA /* Power on reset */
2512 #define KBD_REPLY_ACK 0xFA /* Command ACK */
2513 #define KBD_REPLY_RESEND 0xFE /* Command NACK, send the cmd again */
2515 /* Status Register Bits */
2516 #define KBD_STAT_OBF 0x01 /* Keyboard output buffer full */
2517 #define KBD_STAT_IBF 0x02 /* Keyboard input buffer full */
2518 #define KBD_STAT_SELFTEST 0x04 /* Self test successful */
2519 #define KBD_STAT_CMD 0x08 /* Last write was a command write (0=data) */
2520 #define KBD_STAT_UNLOCKED 0x10 /* Zero if keyboard locked */
2521 #define KBD_STAT_MOUSE_OBF 0x20 /* Mouse output buffer full */
2522 #define KBD_STAT_GTO 0x40 /* General receive/xmit timeout */
2523 #define KBD_STAT_PERR 0x80 /* Parity error */
2525 /* Controller Mode Register Bits */
2526 #define KBD_MODE_KBD_INT 0x01 /* Keyboard data generate IRQ1 */
2527 #define KBD_MODE_MOUSE_INT 0x02 /* Mouse data generate IRQ12 */
2528 #define KBD_MODE_SYS 0x04 /* The system flag (?) */
2529 #define KBD_MODE_NO_KEYLOCK 0x08 /* The keylock doesn't affect the keyboard if set */
2530 #define KBD_MODE_DISABLE_KBD 0x10 /* Disable keyboard interface */
2531 #define KBD_MODE_DISABLE_MOUSE 0x20 /* Disable mouse interface */
2532 #define KBD_MODE_KCC 0x40 /* Scan code conversion to PC format */
2533 #define KBD_MODE_RFU 0x80
2535 /* Mouse Commands */
2536 #define AUX_SET_SCALE11 0xE6 /* Set 1:1 scaling */
2537 #define AUX_SET_SCALE21 0xE7 /* Set 2:1 scaling */
2538 #define AUX_SET_RES 0xE8 /* Set resolution */
2539 #define AUX_GET_SCALE 0xE9 /* Get scaling factor */
2540 #define AUX_SET_STREAM 0xEA /* Set stream mode */
2541 #define AUX_POLL 0xEB /* Poll */
2542 #define AUX_RESET_WRAP 0xEC /* Reset wrap mode */
2543 #define AUX_SET_WRAP 0xEE /* Set wrap mode */
2544 #define AUX_SET_REMOTE 0xF0 /* Set remote mode */
2545 #define AUX_GET_TYPE 0xF2 /* Get type */
2546 #define AUX_SET_SAMPLE 0xF3 /* Set sample rate */
2547 #define AUX_ENABLE_DEV 0xF4 /* Enable aux device */
2548 #define AUX_DISABLE_DEV 0xF5 /* Disable aux device */
2549 #define AUX_SET_DEFAULT 0xF6
2550 #define AUX_RESET 0xFF /* Reset aux device */
2551 #define AUX_ACK 0xFA /* Command byte ACK. */
2553 #define MOUSE_STATUS_REMOTE 0x40
2554 #define MOUSE_STATUS_ENABLED 0x20
2555 #define MOUSE_STATUS_SCALE21 0x10
2557 #define KBD_QUEUE_SIZE 256
2560 uint8_t data[KBD_QUEUE_SIZE];
2561 int rptr, wptr, count;
2564 typedef struct KBDState {
2566 uint8_t write_cmd; /* if non zero, write data to port 60 is expected */
2569 /* keyboard state */
2573 int mouse_write_cmd;
2574 uint8_t mouse_status;
2575 uint8_t mouse_resolution;
2576 uint8_t mouse_sample_rate;
2578 uint8_t mouse_type; /* 0 = PS2, 3 = IMPS/2, 4 = IMEX */
2579 uint8_t mouse_detect_state;
2580 int mouse_dx; /* current values, needed for 'poll' mode */
2583 uint8_t mouse_buttons;
2587 int reset_requested;
2590 /* update irq and KBD_STAT_[MOUSE_]OBF */
2591 static void kbd_update_irq(KBDState *s)
2593 int irq12_level, irq1_level;
2597 s->status &= ~(KBD_STAT_OBF | KBD_STAT_MOUSE_OBF);
2598 if (s->queues[0].count != 0 ||
2599 s->queues[1].count != 0) {
2600 s->status |= KBD_STAT_OBF;
2601 if (s->queues[1].count != 0) {
2602 s->status |= KBD_STAT_MOUSE_OBF;
2603 if (s->mode & KBD_MODE_MOUSE_INT)
2606 if (s->mode & KBD_MODE_KBD_INT)
2610 pic_set_irq(1, irq1_level);
2611 pic_set_irq(12, irq12_level);
2614 static void kbd_queue(KBDState *s, int b, int aux)
2616 KBDQueue *q = &kbd_state.queues[aux];
2618 #if defined(DEBUG_MOUSE) || defined(DEBUG_KBD)
2620 printf("mouse event: 0x%02x\n", b);
2623 printf("kbd event: 0x%02x\n", b);
2626 if (q->count >= KBD_QUEUE_SIZE)
2628 q->data[q->wptr] = b;
2629 if (++q->wptr == KBD_QUEUE_SIZE)
2635 void kbd_put_keycode(int keycode)
2637 KBDState *s = &kbd_state;
2638 kbd_queue(s, keycode, 0);
2641 uint32_t kbd_read_status(CPUX86State *env, uint32_t addr)
2643 KBDState *s = &kbd_state;
2646 #if defined(DEBUG_KBD) && 0
2647 printf("kbd: read status=0x%02x\n", val);
2652 void kbd_write_command(CPUX86State *env, uint32_t addr, uint32_t val)
2654 KBDState *s = &kbd_state;
2657 printf("kbd: write cmd=0x%02x\n", val);
2660 case KBD_CCMD_READ_MODE:
2661 kbd_queue(s, s->mode, 0);
2663 case KBD_CCMD_WRITE_MODE:
2664 case KBD_CCMD_WRITE_OBUF:
2665 case KBD_CCMD_WRITE_AUX_OBUF:
2666 case KBD_CCMD_WRITE_MOUSE:
2667 case KBD_CCMD_WRITE_OUTPORT:
2670 case KBD_CCMD_MOUSE_DISABLE:
2671 s->mode |= KBD_MODE_DISABLE_MOUSE;
2673 case KBD_CCMD_MOUSE_ENABLE:
2674 s->mode &= ~KBD_MODE_DISABLE_MOUSE;
2676 case KBD_CCMD_TEST_MOUSE:
2677 kbd_queue(s, 0x00, 0);
2679 case KBD_CCMD_SELF_TEST:
2680 s->status |= KBD_STAT_SELFTEST;
2681 kbd_queue(s, 0x55, 0);
2683 case KBD_CCMD_KBD_TEST:
2684 kbd_queue(s, 0x00, 0);
2686 case KBD_CCMD_KBD_DISABLE:
2687 s->mode |= KBD_MODE_DISABLE_KBD;
2689 case KBD_CCMD_KBD_ENABLE:
2690 s->mode &= ~KBD_MODE_DISABLE_KBD;
2692 case KBD_CCMD_READ_INPORT:
2693 kbd_queue(s, 0x00, 0);
2695 case KBD_CCMD_READ_OUTPORT:
2696 /* XXX: check that */
2697 val = 0x01 | (a20_enabled << 1);
2698 if (s->status & KBD_STAT_OBF)
2700 if (s->status & KBD_STAT_MOUSE_OBF)
2702 kbd_queue(s, val, 0);
2704 case KBD_CCMD_ENABLE_A20:
2707 case KBD_CCMD_DISABLE_A20:
2710 case KBD_CCMD_RESET:
2711 reset_requested = 1;
2712 cpu_x86_interrupt(global_env, CPU_INTERRUPT_EXIT);
2715 fprintf(stderr, "vl: unsupported keyboard cmd=0x%02x\n", val);
2720 uint32_t kbd_read_data(CPUX86State *env, uint32_t addr)
2722 KBDState *s = &kbd_state;
2726 q = &s->queues[0]; /* first check KBD data */
2728 q = &s->queues[1]; /* then check AUX data */
2729 if (q->count == 0) {
2730 /* XXX: return something else ? */
2733 val = q->data[q->rptr];
2734 if (++q->rptr == KBD_QUEUE_SIZE)
2737 /* reading deasserts IRQ */
2738 if (q == &s->queues[0])
2743 /* reassert IRQs if data left */
2746 printf("kbd: read data=0x%02x\n", val);
2751 static void kbd_reset_keyboard(KBDState *s)
2753 s->scan_enabled = 1;
2756 static void kbd_write_keyboard(KBDState *s, int val)
2758 switch(s->kbd_write_cmd) {
2763 kbd_queue(s, KBD_REPLY_ACK, 0);
2766 kbd_queue(s, KBD_REPLY_RESEND, 0);
2769 kbd_queue(s, KBD_CMD_ECHO, 0);
2771 case KBD_CMD_ENABLE:
2772 s->scan_enabled = 1;
2773 kbd_queue(s, KBD_REPLY_ACK, 0);
2775 case KBD_CMD_SET_LEDS:
2776 case KBD_CMD_SET_RATE:
2777 s->kbd_write_cmd = val;
2779 case KBD_CMD_RESET_DISABLE:
2780 kbd_reset_keyboard(s);
2781 s->scan_enabled = 0;
2782 kbd_queue(s, KBD_REPLY_ACK, 0);
2784 case KBD_CMD_RESET_ENABLE:
2785 kbd_reset_keyboard(s);
2786 s->scan_enabled = 1;
2787 kbd_queue(s, KBD_REPLY_ACK, 0);
2790 kbd_reset_keyboard(s);
2791 kbd_queue(s, KBD_REPLY_ACK, 0);
2792 kbd_queue(s, KBD_REPLY_POR, 0);
2795 kbd_queue(s, KBD_REPLY_ACK, 0);
2799 case KBD_CMD_SET_LEDS:
2800 kbd_queue(s, KBD_REPLY_ACK, 0);
2801 s->kbd_write_cmd = -1;
2803 case KBD_CMD_SET_RATE:
2804 kbd_queue(s, KBD_REPLY_ACK, 0);
2805 s->kbd_write_cmd = -1;
2810 static void kbd_mouse_send_packet(KBDState *s)
2818 /* XXX: increase range to 8 bits ? */
2821 else if (dx1 < -127)
2825 else if (dy1 < -127)
2827 b = 0x08 | ((dx1 < 0) << 4) | ((dy1 < 0) << 5) | (s->mouse_buttons & 0x07);
2829 kbd_queue(s, dx1 & 0xff, 1);
2830 kbd_queue(s, dy1 & 0xff, 1);
2831 /* extra byte for IMPS/2 or IMEX */
2832 switch(s->mouse_type) {
2838 else if (dz1 < -127)
2840 kbd_queue(s, dz1 & 0xff, 1);
2847 b = (dz1 & 0x0f) | ((s->mouse_buttons & 0x18) << 1);
2858 void kbd_mouse_event(int dx, int dy, int dz, int buttons_state)
2860 KBDState *s = &kbd_state;
2862 /* check if deltas are recorded when disabled */
2863 if (!(s->mouse_status & MOUSE_STATUS_ENABLED))
2869 s->mouse_buttons = buttons_state;
2871 if (!(s->mouse_status & MOUSE_STATUS_REMOTE) &&
2872 (s->queues[1].count < (KBD_QUEUE_SIZE - 16))) {
2874 /* if not remote, send event. Multiple events are sent if
2876 kbd_mouse_send_packet(s);
2877 if (s->mouse_dx == 0 && s->mouse_dy == 0 && s->mouse_dz == 0)
2883 static void kbd_write_mouse(KBDState *s, int val)
2886 printf("kbd: write mouse 0x%02x\n", val);
2888 switch(s->mouse_write_cmd) {
2892 if (s->mouse_wrap) {
2893 if (val == AUX_RESET_WRAP) {
2895 kbd_queue(s, AUX_ACK, 1);
2897 } else if (val != AUX_RESET) {
2898 kbd_queue(s, val, 1);
2903 case AUX_SET_SCALE11:
2904 s->mouse_status &= ~MOUSE_STATUS_SCALE21;
2905 kbd_queue(s, AUX_ACK, 1);
2907 case AUX_SET_SCALE21:
2908 s->mouse_status |= MOUSE_STATUS_SCALE21;
2909 kbd_queue(s, AUX_ACK, 1);
2911 case AUX_SET_STREAM:
2912 s->mouse_status &= ~MOUSE_STATUS_REMOTE;
2913 kbd_queue(s, AUX_ACK, 1);
2917 kbd_queue(s, AUX_ACK, 1);
2919 case AUX_SET_REMOTE:
2920 s->mouse_status |= MOUSE_STATUS_REMOTE;
2921 kbd_queue(s, AUX_ACK, 1);
2924 kbd_queue(s, AUX_ACK, 1);
2925 kbd_queue(s, s->mouse_type, 1);
2928 case AUX_SET_SAMPLE:
2929 s->mouse_write_cmd = val;
2930 kbd_queue(s, AUX_ACK, 1);
2933 kbd_queue(s, AUX_ACK, 1);
2934 kbd_queue(s, s->mouse_status, 1);
2935 kbd_queue(s, s->mouse_resolution, 1);
2936 kbd_queue(s, s->mouse_sample_rate, 1);
2939 kbd_queue(s, AUX_ACK, 1);
2940 kbd_mouse_send_packet(s);
2942 case AUX_ENABLE_DEV:
2943 s->mouse_status |= MOUSE_STATUS_ENABLED;
2944 kbd_queue(s, AUX_ACK, 1);
2946 case AUX_DISABLE_DEV:
2947 s->mouse_status &= ~MOUSE_STATUS_ENABLED;
2948 kbd_queue(s, AUX_ACK, 1);
2950 case AUX_SET_DEFAULT:
2951 s->mouse_sample_rate = 100;
2952 s->mouse_resolution = 2;
2953 s->mouse_status = 0;
2954 kbd_queue(s, AUX_ACK, 1);
2957 s->mouse_sample_rate = 100;
2958 s->mouse_resolution = 2;
2959 s->mouse_status = 0;
2960 kbd_queue(s, AUX_ACK, 1);
2961 kbd_queue(s, 0xaa, 1);
2962 kbd_queue(s, s->mouse_type, 1);
2968 case AUX_SET_SAMPLE:
2969 s->mouse_sample_rate = val;
2971 /* detect IMPS/2 or IMEX */
2972 switch(s->mouse_detect_state) {
2976 s->mouse_detect_state = 1;
2980 s->mouse_detect_state = 2;
2981 else if (val == 200)
2982 s->mouse_detect_state = 3;
2984 s->mouse_detect_state = 0;
2988 s->mouse_type = 3; /* IMPS/2 */
2989 s->mouse_detect_state = 0;
2993 s->mouse_type = 4; /* IMEX */
2994 s->mouse_detect_state = 0;
2998 kbd_queue(s, AUX_ACK, 1);
2999 s->mouse_write_cmd = -1;
3002 s->mouse_resolution = val;
3003 kbd_queue(s, AUX_ACK, 1);
3004 s->mouse_write_cmd = -1;
3009 void kbd_write_data(CPUX86State *env, uint32_t addr, uint32_t val)
3011 KBDState *s = &kbd_state;
3014 printf("kbd: write data=0x%02x\n", val);
3017 switch(s->write_cmd) {
3019 kbd_write_keyboard(s, val);
3021 case KBD_CCMD_WRITE_MODE:
3025 case KBD_CCMD_WRITE_OBUF:
3026 kbd_queue(s, val, 0);
3028 case KBD_CCMD_WRITE_AUX_OBUF:
3029 kbd_queue(s, val, 1);
3031 case KBD_CCMD_WRITE_OUTPORT:
3032 a20_enabled = (val >> 1) & 1;
3034 reset_requested = 1;
3035 cpu_x86_interrupt(global_env, CPU_INTERRUPT_EXIT);
3038 case KBD_CCMD_WRITE_MOUSE:
3039 kbd_write_mouse(s, val);
3047 void kbd_reset(KBDState *s)
3052 s->kbd_write_cmd = -1;
3053 s->mouse_write_cmd = -1;
3054 s->mode = KBD_MODE_KBD_INT | KBD_MODE_MOUSE_INT;
3055 s->status = KBD_STAT_CMD | KBD_STAT_UNLOCKED;
3056 for(i = 0; i < 2; i++) {
3066 kbd_reset(&kbd_state);
3067 register_ioport_read(0x60, 1, kbd_read_data, 1);
3068 register_ioport_write(0x60, 1, kbd_write_data, 1);
3069 register_ioport_read(0x64, 1, kbd_read_status, 1);
3070 register_ioport_write(0x64, 1, kbd_write_command, 1);
3073 /***********************************************************/
3074 /* Bochs BIOS debug ports */
3076 void bochs_bios_write(CPUX86State *env, uint32_t addr, uint32_t val)
3079 /* Bochs BIOS messages */
3082 fprintf(stderr, "BIOS panic at rombios.c, line %d\n", val);
3087 fprintf(stderr, "%c", val);
3091 /* LGPL'ed VGA BIOS messages */
3094 fprintf(stderr, "VGA BIOS panic, line %d\n", val);
3099 fprintf(stderr, "%c", val);
3105 void bochs_bios_init(void)
3107 register_ioport_write(0x400, 1, bochs_bios_write, 2);
3108 register_ioport_write(0x401, 1, bochs_bios_write, 2);
3109 register_ioport_write(0x402, 1, bochs_bios_write, 1);
3110 register_ioport_write(0x403, 1, bochs_bios_write, 1);
3112 register_ioport_write(0x501, 1, bochs_bios_write, 2);
3113 register_ioport_write(0x502, 1, bochs_bios_write, 2);
3114 register_ioport_write(0x500, 1, bochs_bios_write, 1);
3115 register_ioport_write(0x503, 1, bochs_bios_write, 1);
3118 /***********************************************************/
3121 /* init terminal so that we can grab keys */
3122 static struct termios oldtty;
3124 static void term_exit(void)
3126 tcsetattr (0, TCSANOW, &oldtty);
3129 static void term_init(void)
3133 tcgetattr (0, &tty);
3136 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
3137 |INLCR|IGNCR|ICRNL|IXON);
3138 tty.c_oflag |= OPOST;
3139 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN|ISIG);
3140 tty.c_cflag &= ~(CSIZE|PARENB);
3143 tty.c_cc[VTIME] = 0;
3145 tcsetattr (0, TCSANOW, &tty);
3149 fcntl(0, F_SETFL, O_NONBLOCK);
3152 static void dumb_update(DisplayState *ds, int x, int y, int w, int h)
3156 static void dumb_resize(DisplayState *ds, int w, int h)
3160 static void dumb_refresh(DisplayState *ds)
3162 vga_update_display();
3165 void dumb_display_init(DisplayState *ds)
3170 ds->dpy_update = dumb_update;
3171 ds->dpy_resize = dumb_resize;
3172 ds->dpy_refresh = dumb_refresh;
3175 /***********************************************************/
3176 /* cpu signal handler */
3177 static void host_segv_handler(int host_signum, siginfo_t *info,
3180 if (cpu_signal_handler(host_signum, info, puc))
3186 static int timer_irq_pending;
3187 static int timer_irq_count;
3189 static int timer_ms;
3190 static int gui_refresh_pending, gui_refresh_count;
3192 static void host_alarm_handler(int host_signum, siginfo_t *info,
3195 /* NOTE: since usually the OS asks a 100 Hz clock, there can be
3196 some drift between cpu_get_ticks() and the interrupt time. So
3197 we queue some interrupts to avoid missing some */
3198 timer_irq_count += pit_get_out_edges(&pit_channels[0]);
3199 if (timer_irq_count) {
3200 if (timer_irq_count > 2)
3201 timer_irq_count = 2;
3203 timer_irq_pending = 1;
3205 gui_refresh_count += timer_ms;
3206 if (gui_refresh_count >= GUI_REFRESH_INTERVAL) {
3207 gui_refresh_count = 0;
3208 gui_refresh_pending = 1;
3211 if (gui_refresh_pending || timer_irq_pending) {
3212 /* just exit from the cpu to have a chance to handle timers */
3213 cpu_x86_interrupt(global_env, CPU_INTERRUPT_EXIT);
3217 unsigned long mmap_addr = PHYS_RAM_BASE;
3219 void *get_mmap_addr(unsigned long size)
3223 mmap_addr += ((size + 4095) & ~4095) + 4096;
3224 return (void *)addr;
3227 /* main execution loop */
3229 CPUState *cpu_gdbstub_get_env(void *opaque)
3234 int main_loop(void *opaque)
3236 struct pollfd ufds[2], *pf, *serial_ufd, *net_ufd, *gdb_ufd;
3237 int ret, n, timeout;
3239 CPUState *env = global_env;
3241 if (nodisp && !term_inited) {
3242 /* initialize terminal only there so that the user has a
3243 chance to stop QEMU with Ctrl-C before the gdb connection
3251 ret = cpu_x86_exec(env);
3252 if (reset_requested)
3254 if (ret == EXCP_DEBUG)
3256 /* if hlt instruction, we wait until the next IRQ */
3257 if (ret == EXCP_HLT)
3261 /* poll any events */
3264 if (!(serial_ports[0].lsr & UART_LSR_DR)) {
3267 pf->events = POLLIN;
3271 if (net_fd > 0 && ne2000_can_receive(&ne2000_state)) {
3274 pf->events = POLLIN;
3278 if (gdbstub_fd > 0) {
3280 pf->fd = gdbstub_fd;
3281 pf->events = POLLIN;
3285 ret = poll(ufds, pf - ufds, timeout);
3287 if (serial_ufd && (serial_ufd->revents & POLLIN)) {
3288 n = read(0, &ch, 1);
3290 serial_received_byte(&serial_ports[0], ch);
3293 if (net_ufd && (net_ufd->revents & POLLIN)) {
3294 uint8_t buf[MAX_ETH_FRAME_SIZE];
3296 n = read(net_fd, buf, MAX_ETH_FRAME_SIZE);
3299 memset(buf + n, 0, 60 - n);
3302 ne2000_receive(&ne2000_state, buf, n);
3305 if (gdb_ufd && (gdb_ufd->revents & POLLIN)) {
3307 /* stop emulation if requested by gdb */
3308 n = read(gdbstub_fd, buf, 1);
3315 if (timer_irq_pending) {
3318 timer_irq_pending = 0;
3322 if (gui_refresh_pending) {
3323 display_state.dpy_refresh(&display_state);
3324 gui_refresh_pending = 0;
3327 return EXCP_INTERRUPT;
3332 printf("Virtual Linux version " QEMU_VERSION ", Copyright (c) 2003 Fabrice Bellard\n"
3333 "usage: vl [options] [bzImage [kernel parameters...]]\n"
3335 "'bzImage' is a Linux kernel image (PAGE_OFFSET must be defined\n"
3336 "to 0x90000000 in asm/page.h and arch/i386/vmlinux.lds)\n"
3338 "General options:\n"
3339 "-initrd file use 'file' as initial ram disk\n"
3340 "-hda file use 'file' as hard disk 0 image\n"
3341 "-hdb file use 'file' as hard disk 1 image\n"
3342 "-snapshot write to temporary files instead of disk image files\n"
3343 "-m megs set virtual RAM size to megs MB\n"
3344 "-n script set network init script [default=%s]\n"
3346 "Debug/Expert options:\n"
3347 "-s wait gdb connection to port %d\n"
3348 "-p port change gdb connection port\n"
3349 "-d output log in /tmp/vl.log\n"
3350 "-hdachs c,h,s force hard disk 0 geometry for non LBA disk images\n"
3351 "-L path set the directory for the BIOS and VGA BIOS\n"
3353 "During emulation, use C-a h to get terminal commands:\n",
3354 DEFAULT_NETWORK_SCRIPT, DEFAULT_GDBSTUB_PORT);
3359 struct option long_options[] = {
3360 { "initrd", 1, NULL, 0, },
3361 { "hda", 1, NULL, 0, },
3362 { "hdb", 1, NULL, 0, },
3363 { "snapshot", 0, NULL, 0, },
3364 { "hdachs", 1, NULL, 0, },
3365 { "nodisp", 0, NULL, 0, },
3366 { NULL, 0, NULL, 0 },
3369 int main(int argc, char **argv)
3371 int c, ret, initrd_size, i, use_gdbstub, gdbstub_port, long_index;
3372 int snapshot, linux_boot, total_ram_size;
3373 struct linux_params *params;
3374 struct sigaction act;
3375 struct itimerval itv;
3377 const char *tmpdir, *initrd_filename;
3378 const char *hd_filename[MAX_DISKS];
3379 DisplayState *ds = &display_state;
3381 /* we never want that malloc() uses mmap() */
3382 mallopt(M_MMAP_THRESHOLD, 4096 * 1024);
3383 initrd_filename = NULL;
3384 for(i = 0; i < MAX_DISKS; i++)
3385 hd_filename[i] = NULL;
3386 phys_ram_size = 32 * 1024 * 1024;
3387 vga_ram_size = VGA_RAM_SIZE;
3388 pstrcpy(network_script, sizeof(network_script), DEFAULT_NETWORK_SCRIPT);
3390 gdbstub_port = DEFAULT_GDBSTUB_PORT;
3395 c = getopt_long_only(argc, argv, "hm:dn:sp:L:", long_options, &long_index);
3400 switch(long_index) {
3402 initrd_filename = optarg;
3405 hd_filename[0] = optarg;
3408 hd_filename[1] = optarg;
3415 int cyls, heads, secs;
3418 cyls = strtol(p, (char **)&p, 0);
3422 heads = strtol(p, (char **)&p, 0);
3426 secs = strtol(p, (char **)&p, 0);
3429 ide_state[0].cylinders = cyls;
3430 ide_state[0].heads = heads;
3431 ide_state[0].sectors = secs;
3444 phys_ram_size = atoi(optarg) * 1024 * 1024;
3445 if (phys_ram_size <= 0)
3447 if (phys_ram_size > PHYS_RAM_MAX_SIZE) {
3448 fprintf(stderr, "vl: at most %d MB RAM can be simulated\n",
3449 PHYS_RAM_MAX_SIZE / (1024 * 1024));
3457 pstrcpy(network_script, sizeof(network_script), optarg);
3463 gdbstub_port = atoi(optarg);
3466 interp_prefix = optarg;
3471 linux_boot = (optind < argc);
3473 if (!linux_boot && hd_filename[0] == '\0')
3477 setvbuf(stdout, NULL, _IOLBF, 0);
3479 logfile = fopen(DEBUG_LOGFILE, "w");
3481 perror(DEBUG_LOGFILE);
3484 setvbuf(logfile, NULL, _IOLBF, 0);
3487 /* init network tun interface */
3490 /* init the memory */
3491 tmpdir = getenv("VLTMPDIR");
3494 snprintf(phys_ram_file, sizeof(phys_ram_file), "%s/vlXXXXXX", tmpdir);
3495 if (mkstemp(phys_ram_file) < 0) {
3496 fprintf(stderr, "Could not create temporary memory file '%s'\n",
3500 phys_ram_fd = open(phys_ram_file, O_CREAT | O_TRUNC | O_RDWR, 0600);
3501 if (phys_ram_fd < 0) {
3502 fprintf(stderr, "Could not open temporary memory file '%s'\n",
3506 total_ram_size = phys_ram_size + vga_ram_size;
3507 ftruncate(phys_ram_fd, total_ram_size);
3508 unlink(phys_ram_file);
3509 phys_ram_base = mmap(get_mmap_addr(total_ram_size),
3511 PROT_WRITE | PROT_READ, MAP_SHARED | MAP_FIXED,
3513 if (phys_ram_base == MAP_FAILED) {
3514 fprintf(stderr, "Could not map physical memory\n");
3518 /* open the virtual block devices */
3519 for(i = 0; i < MAX_DISKS; i++) {
3520 if (hd_filename[i]) {
3521 bs_table[i] = bdrv_open(hd_filename[i], snapshot);
3523 fprintf(stderr, "vl: could not open hard disk image '%s\n",
3530 /* init CPU state */
3533 cpu_single_env = env;
3538 cpu_register_physical_memory(0, phys_ram_size, 0);
3541 /* now we can load the kernel */
3542 ret = load_kernel(argv[optind], phys_ram_base + KERNEL_LOAD_ADDR);
3544 fprintf(stderr, "vl: could not load kernel '%s'\n", argv[optind]);
3550 if (initrd_filename) {
3551 initrd_size = load_image(initrd_filename, phys_ram_base + INITRD_LOAD_ADDR);
3552 if (initrd_size < 0) {
3553 fprintf(stderr, "vl: could not load initial ram disk '%s'\n",
3559 /* init kernel params */
3560 params = (void *)(phys_ram_base + KERNEL_PARAMS_ADDR);
3561 memset(params, 0, sizeof(struct linux_params));
3562 params->mount_root_rdonly = 0;
3563 params->cl_magic = 0xA33F;
3564 params->cl_offset = params->commandline - (uint8_t *)params;
3565 params->alt_mem_k = (phys_ram_size / 1024) - 1024;
3566 for(i = optind + 1; i < argc; i++) {
3567 if (i != optind + 1)
3568 pstrcat(params->commandline, sizeof(params->commandline), " ");
3569 pstrcat(params->commandline, sizeof(params->commandline), argv[i]);
3571 params->loader_type = 0x01;
3572 if (initrd_size > 0) {
3573 params->initrd_start = INITRD_LOAD_ADDR;
3574 params->initrd_size = initrd_size;
3576 params->orig_video_lines = 25;
3577 params->orig_video_cols = 80;
3579 /* setup basic memory access */
3580 env->cr[0] = 0x00000033;
3581 cpu_x86_init_mmu(env);
3583 memset(params->idt_table, 0, sizeof(params->idt_table));
3585 params->gdt_table[2] = 0x00cf9a000000ffffLL; /* KERNEL_CS */
3586 params->gdt_table[3] = 0x00cf92000000ffffLL; /* KERNEL_DS */
3588 env->idt.base = (void *)params->idt_table;
3589 env->idt.limit = sizeof(params->idt_table) - 1;
3590 env->gdt.base = (void *)params->gdt_table;
3591 env->gdt.limit = sizeof(params->gdt_table) - 1;
3593 cpu_x86_load_seg(env, R_CS, KERNEL_CS);
3594 cpu_x86_load_seg(env, R_DS, KERNEL_DS);
3595 cpu_x86_load_seg(env, R_ES, KERNEL_DS);
3596 cpu_x86_load_seg(env, R_SS, KERNEL_DS);
3597 cpu_x86_load_seg(env, R_FS, KERNEL_DS);
3598 cpu_x86_load_seg(env, R_GS, KERNEL_DS);
3600 env->eip = KERNEL_LOAD_ADDR;
3601 env->regs[R_ESI] = KERNEL_PARAMS_ADDR;
3610 snprintf(buf, sizeof(buf), "%s/%s", interp_prefix, BIOS_FILENAME);
3611 ret = load_image(buf, phys_ram_base + 0x000f0000);
3612 if (ret != 0x10000) {
3613 fprintf(stderr, "vl: could not load PC bios '%s'\n", BIOS_FILENAME);
3618 snprintf(buf, sizeof(buf), "%s/%s", interp_prefix, VGABIOS_FILENAME);
3619 ret = load_image(buf, phys_ram_base + 0x000c0000);
3621 /* setup basic memory access */
3622 env->cr[0] = 0x60000010;
3623 cpu_x86_init_mmu(env);
3625 env->idt.limit = 0xffff;
3626 env->gdt.limit = 0xffff;
3627 env->ldt.limit = 0xffff;
3629 /* not correct (CS base=0xffff0000) */
3630 cpu_x86_load_seg(env, R_CS, 0xf000);
3631 cpu_x86_load_seg(env, R_DS, 0);
3632 cpu_x86_load_seg(env, R_ES, 0);
3633 cpu_x86_load_seg(env, R_SS, 0);
3634 cpu_x86_load_seg(env, R_FS, 0);
3635 cpu_x86_load_seg(env, R_GS, 0);
3638 env->regs[R_EDX] = 0x600; /* indicate P6 processor */
3647 dumb_display_init(ds);
3650 sdl_display_init(ds);
3651 /* the pthreads modify sigaction. We don't want that. */
3652 #define sigaction __sigaction
3654 dumb_display_init(ds);
3657 /* init basic PC hardware */
3658 register_ioport_write(0x80, 1, ioport80_write, 1);
3660 vga_init(ds, phys_ram_base + phys_ram_size, phys_ram_size,
3670 /* setup cpu signal handlers for MMU / self modifying code handling */
3671 sigfillset(&act.sa_mask);
3672 act.sa_flags = SA_SIGINFO;
3673 act.sa_sigaction = host_segv_handler;
3674 sigaction(SIGSEGV, &act, NULL);
3675 sigaction(SIGBUS, &act, NULL);
3677 act.sa_sigaction = host_alarm_handler;
3678 sigaction(SIGALRM, &act, NULL);
3680 itv.it_interval.tv_sec = 0;
3681 itv.it_interval.tv_usec = 1000;
3682 itv.it_value.tv_sec = 0;
3683 itv.it_value.tv_usec = 10 * 1000;
3684 setitimer(ITIMER_REAL, &itv, NULL);
3685 /* we probe the tick duration of the kernel to inform the user if
3686 the emulated kernel requested a too high timer frequency */
3687 getitimer(ITIMER_REAL, &itv);
3688 timer_ms = itv.it_interval.tv_usec / 1000;
3689 pit_min_timer_count = ((uint64_t)itv.it_interval.tv_usec * PIT_FREQ) /
3693 cpu_gdbstub(NULL, main_loop, gdbstub_port);