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
;
2438 uint8_t boot_ind
; /* 0x80 - active */
2439 uint8_t head
; /* starting head */
2440 uint8_t sector
; /* starting sector */
2441 uint8_t cyl
; /* starting cylinder */
2442 uint8_t sys_ind
; /* What partition type */
2443 uint8_t end_head
; /* end head */
2444 uint8_t end_sector
; /* end sector */
2445 uint8_t end_cyl
; /* end cylinder */
2446 uint32_t start_sect
; /* starting sector counting from 0 */
2447 uint32_t nr_sects
; /* nr of sectors in partition */
2448 } __attribute__((packed
));
2450 /* try to guess the IDE geometry from the MSDOS partition table */
2451 void ide_guess_geometry(IDEState
*s
)
2455 struct partition
*p
;
2458 if (s
->cylinders
!= 0)
2460 ret
= bdrv_read(s
->bs
, 0, buf
, 1);
2463 /* test msdos magic */
2464 if (buf
[510] != 0x55 || buf
[511] != 0xaa)
2466 for(i
= 0; i
< 4; i
++) {
2467 p
= ((struct partition
*)(buf
+ 0x1be)) + i
;
2468 nr_sects
= tswap32(p
->nr_sects
);
2469 if (nr_sects
&& p
->end_head
) {
2470 /* We make the assumption that the partition terminates on
2471 a cylinder boundary */
2472 s
->heads
= p
->end_head
+ 1;
2473 s
->sectors
= p
->end_sector
& 63;
2474 s
->cylinders
= s
->nb_sectors
/ (s
->heads
* s
->sectors
);
2476 printf("guessed partition: CHS=%d %d %d\n",
2477 s
->cylinders
, s
->heads
, s
->sectors
);
2489 for(i
= 0; i
< MAX_DISKS
; i
++) {
2491 s
->bs
= bs_table
[i
];
2493 bdrv_get_geometry(s
->bs
, &nb_sectors
);
2494 s
->nb_sectors
= nb_sectors
;
2495 ide_guess_geometry(s
);
2496 if (s
->cylinders
== 0) {
2497 /* if no geometry, use a LBA compatible one */
2498 cylinders
= nb_sectors
/ (16 * 63);
2499 if (cylinders
> 16383)
2501 else if (cylinders
< 2)
2503 s
->cylinders
= cylinders
;
2511 register_ioport_write(0x1f0, 8, ide_ioport_write
, 1);
2512 register_ioport_read(0x1f0, 8, ide_ioport_read
, 1);
2513 register_ioport_read(0x3f6, 1, ide_status_read
, 1);
2514 register_ioport_write(0x3f6, 1, ide_cmd_write
, 1);
2517 register_ioport_write(0x1f0, 2, ide_data_writew
, 2);
2518 register_ioport_read(0x1f0, 2, ide_data_readw
, 2);
2519 register_ioport_write(0x1f0, 4, ide_data_writel
, 4);
2520 register_ioport_read(0x1f0, 4, ide_data_readl
, 4);
2523 /***********************************************************/
2524 /* keyboard emulation */
2526 /* Keyboard Controller Commands */
2527 #define KBD_CCMD_READ_MODE 0x20 /* Read mode bits */
2528 #define KBD_CCMD_WRITE_MODE 0x60 /* Write mode bits */
2529 #define KBD_CCMD_GET_VERSION 0xA1 /* Get controller version */
2530 #define KBD_CCMD_MOUSE_DISABLE 0xA7 /* Disable mouse interface */
2531 #define KBD_CCMD_MOUSE_ENABLE 0xA8 /* Enable mouse interface */
2532 #define KBD_CCMD_TEST_MOUSE 0xA9 /* Mouse interface test */
2533 #define KBD_CCMD_SELF_TEST 0xAA /* Controller self test */
2534 #define KBD_CCMD_KBD_TEST 0xAB /* Keyboard interface test */
2535 #define KBD_CCMD_KBD_DISABLE 0xAD /* Keyboard interface disable */
2536 #define KBD_CCMD_KBD_ENABLE 0xAE /* Keyboard interface enable */
2537 #define KBD_CCMD_READ_INPORT 0xC0 /* read input port */
2538 #define KBD_CCMD_READ_OUTPORT 0xD0 /* read output port */
2539 #define KBD_CCMD_WRITE_OUTPORT 0xD1 /* write output port */
2540 #define KBD_CCMD_WRITE_OBUF 0xD2
2541 #define KBD_CCMD_WRITE_AUX_OBUF 0xD3 /* Write to output buffer as if
2542 initiated by the auxiliary device */
2543 #define KBD_CCMD_WRITE_MOUSE 0xD4 /* Write the following byte to the mouse */
2544 #define KBD_CCMD_ENABLE_A20 0xDD
2545 #define KBD_CCMD_DISABLE_A20 0xDF
2546 #define KBD_CCMD_RESET 0xFE
2548 /* Keyboard Commands */
2549 #define KBD_CMD_SET_LEDS 0xED /* Set keyboard leds */
2550 #define KBD_CMD_ECHO 0xEE
2551 #define KBD_CMD_SET_RATE 0xF3 /* Set typematic rate */
2552 #define KBD_CMD_ENABLE 0xF4 /* Enable scanning */
2553 #define KBD_CMD_RESET_DISABLE 0xF5 /* reset and disable scanning */
2554 #define KBD_CMD_RESET_ENABLE 0xF6 /* reset and enable scanning */
2555 #define KBD_CMD_RESET 0xFF /* Reset */
2557 /* Keyboard Replies */
2558 #define KBD_REPLY_POR 0xAA /* Power on reset */
2559 #define KBD_REPLY_ACK 0xFA /* Command ACK */
2560 #define KBD_REPLY_RESEND 0xFE /* Command NACK, send the cmd again */
2562 /* Status Register Bits */
2563 #define KBD_STAT_OBF 0x01 /* Keyboard output buffer full */
2564 #define KBD_STAT_IBF 0x02 /* Keyboard input buffer full */
2565 #define KBD_STAT_SELFTEST 0x04 /* Self test successful */
2566 #define KBD_STAT_CMD 0x08 /* Last write was a command write (0=data) */
2567 #define KBD_STAT_UNLOCKED 0x10 /* Zero if keyboard locked */
2568 #define KBD_STAT_MOUSE_OBF 0x20 /* Mouse output buffer full */
2569 #define KBD_STAT_GTO 0x40 /* General receive/xmit timeout */
2570 #define KBD_STAT_PERR 0x80 /* Parity error */
2572 /* Controller Mode Register Bits */
2573 #define KBD_MODE_KBD_INT 0x01 /* Keyboard data generate IRQ1 */
2574 #define KBD_MODE_MOUSE_INT 0x02 /* Mouse data generate IRQ12 */
2575 #define KBD_MODE_SYS 0x04 /* The system flag (?) */
2576 #define KBD_MODE_NO_KEYLOCK 0x08 /* The keylock doesn't affect the keyboard if set */
2577 #define KBD_MODE_DISABLE_KBD 0x10 /* Disable keyboard interface */
2578 #define KBD_MODE_DISABLE_MOUSE 0x20 /* Disable mouse interface */
2579 #define KBD_MODE_KCC 0x40 /* Scan code conversion to PC format */
2580 #define KBD_MODE_RFU 0x80
2582 /* Mouse Commands */
2583 #define AUX_SET_SCALE11 0xE6 /* Set 1:1 scaling */
2584 #define AUX_SET_SCALE21 0xE7 /* Set 2:1 scaling */
2585 #define AUX_SET_RES 0xE8 /* Set resolution */
2586 #define AUX_GET_SCALE 0xE9 /* Get scaling factor */
2587 #define AUX_SET_STREAM 0xEA /* Set stream mode */
2588 #define AUX_POLL 0xEB /* Poll */
2589 #define AUX_RESET_WRAP 0xEC /* Reset wrap mode */
2590 #define AUX_SET_WRAP 0xEE /* Set wrap mode */
2591 #define AUX_SET_REMOTE 0xF0 /* Set remote mode */
2592 #define AUX_GET_TYPE 0xF2 /* Get type */
2593 #define AUX_SET_SAMPLE 0xF3 /* Set sample rate */
2594 #define AUX_ENABLE_DEV 0xF4 /* Enable aux device */
2595 #define AUX_DISABLE_DEV 0xF5 /* Disable aux device */
2596 #define AUX_SET_DEFAULT 0xF6
2597 #define AUX_RESET 0xFF /* Reset aux device */
2598 #define AUX_ACK 0xFA /* Command byte ACK. */
2600 #define MOUSE_STATUS_REMOTE 0x40
2601 #define MOUSE_STATUS_ENABLED 0x20
2602 #define MOUSE_STATUS_SCALE21 0x10
2604 #define KBD_QUEUE_SIZE 256
2607 uint8_t data
[KBD_QUEUE_SIZE
];
2608 int rptr
, wptr
, count
;
2611 typedef struct KBDState
{
2613 uint8_t write_cmd
; /* if non zero, write data to port 60 is expected */
2616 /* keyboard state */
2620 int mouse_write_cmd
;
2621 uint8_t mouse_status
;
2622 uint8_t mouse_resolution
;
2623 uint8_t mouse_sample_rate
;
2625 uint8_t mouse_type
; /* 0 = PS2, 3 = IMPS/2, 4 = IMEX */
2626 uint8_t mouse_detect_state
;
2627 int mouse_dx
; /* current values, needed for 'poll' mode */
2630 uint8_t mouse_buttons
;
2634 int reset_requested
;
2637 /* update irq and KBD_STAT_[MOUSE_]OBF */
2638 static void kbd_update_irq(KBDState
*s
)
2640 int irq12_level
, irq1_level
;
2644 s
->status
&= ~(KBD_STAT_OBF
| KBD_STAT_MOUSE_OBF
);
2645 if (s
->queues
[0].count
!= 0 ||
2646 s
->queues
[1].count
!= 0) {
2647 s
->status
|= KBD_STAT_OBF
;
2648 if (s
->queues
[1].count
!= 0) {
2649 s
->status
|= KBD_STAT_MOUSE_OBF
;
2650 if (s
->mode
& KBD_MODE_MOUSE_INT
)
2653 if (s
->mode
& KBD_MODE_KBD_INT
)
2657 pic_set_irq(1, irq1_level
);
2658 pic_set_irq(12, irq12_level
);
2661 static void kbd_queue(KBDState
*s
, int b
, int aux
)
2663 KBDQueue
*q
= &kbd_state
.queues
[aux
];
2665 #if defined(DEBUG_MOUSE) || defined(DEBUG_KBD)
2667 printf("mouse event: 0x%02x\n", b
);
2670 printf("kbd event: 0x%02x\n", b
);
2673 if (q
->count
>= KBD_QUEUE_SIZE
)
2675 q
->data
[q
->wptr
] = b
;
2676 if (++q
->wptr
== KBD_QUEUE_SIZE
)
2682 void kbd_put_keycode(int keycode
)
2684 KBDState
*s
= &kbd_state
;
2685 kbd_queue(s
, keycode
, 0);
2688 uint32_t kbd_read_status(CPUX86State
*env
, uint32_t addr
)
2690 KBDState
*s
= &kbd_state
;
2693 #if defined(DEBUG_KBD) && 0
2694 printf("kbd: read status=0x%02x\n", val
);
2699 void kbd_write_command(CPUX86State
*env
, uint32_t addr
, uint32_t val
)
2701 KBDState
*s
= &kbd_state
;
2704 printf("kbd: write cmd=0x%02x\n", val
);
2707 case KBD_CCMD_READ_MODE
:
2708 kbd_queue(s
, s
->mode
, 0);
2710 case KBD_CCMD_WRITE_MODE
:
2711 case KBD_CCMD_WRITE_OBUF
:
2712 case KBD_CCMD_WRITE_AUX_OBUF
:
2713 case KBD_CCMD_WRITE_MOUSE
:
2714 case KBD_CCMD_WRITE_OUTPORT
:
2717 case KBD_CCMD_MOUSE_DISABLE
:
2718 s
->mode
|= KBD_MODE_DISABLE_MOUSE
;
2720 case KBD_CCMD_MOUSE_ENABLE
:
2721 s
->mode
&= ~KBD_MODE_DISABLE_MOUSE
;
2723 case KBD_CCMD_TEST_MOUSE
:
2724 kbd_queue(s
, 0x00, 0);
2726 case KBD_CCMD_SELF_TEST
:
2727 s
->status
|= KBD_STAT_SELFTEST
;
2728 kbd_queue(s
, 0x55, 0);
2730 case KBD_CCMD_KBD_TEST
:
2731 kbd_queue(s
, 0x00, 0);
2733 case KBD_CCMD_KBD_DISABLE
:
2734 s
->mode
|= KBD_MODE_DISABLE_KBD
;
2736 case KBD_CCMD_KBD_ENABLE
:
2737 s
->mode
&= ~KBD_MODE_DISABLE_KBD
;
2739 case KBD_CCMD_READ_INPORT
:
2740 kbd_queue(s
, 0x00, 0);
2742 case KBD_CCMD_READ_OUTPORT
:
2743 /* XXX: check that */
2744 val
= 0x01 | (a20_enabled
<< 1);
2745 if (s
->status
& KBD_STAT_OBF
)
2747 if (s
->status
& KBD_STAT_MOUSE_OBF
)
2749 kbd_queue(s
, val
, 0);
2751 case KBD_CCMD_ENABLE_A20
:
2754 case KBD_CCMD_DISABLE_A20
:
2757 case KBD_CCMD_RESET
:
2758 reset_requested
= 1;
2759 cpu_x86_interrupt(global_env
, CPU_INTERRUPT_EXIT
);
2762 fprintf(stderr
, "vl: unsupported keyboard cmd=0x%02x\n", val
);
2767 uint32_t kbd_read_data(CPUX86State
*env
, uint32_t addr
)
2769 KBDState
*s
= &kbd_state
;
2773 q
= &s
->queues
[0]; /* first check KBD data */
2775 q
= &s
->queues
[1]; /* then check AUX data */
2776 if (q
->count
== 0) {
2777 /* XXX: return something else ? */
2780 val
= q
->data
[q
->rptr
];
2781 if (++q
->rptr
== KBD_QUEUE_SIZE
)
2784 /* reading deasserts IRQ */
2785 if (q
== &s
->queues
[0])
2790 /* reassert IRQs if data left */
2793 printf("kbd: read data=0x%02x\n", val
);
2798 static void kbd_reset_keyboard(KBDState
*s
)
2800 s
->scan_enabled
= 1;
2803 static void kbd_write_keyboard(KBDState
*s
, int val
)
2805 switch(s
->kbd_write_cmd
) {
2810 kbd_queue(s
, KBD_REPLY_ACK
, 0);
2813 kbd_queue(s
, KBD_REPLY_RESEND
, 0);
2816 kbd_queue(s
, KBD_CMD_ECHO
, 0);
2818 case KBD_CMD_ENABLE
:
2819 s
->scan_enabled
= 1;
2820 kbd_queue(s
, KBD_REPLY_ACK
, 0);
2822 case KBD_CMD_SET_LEDS
:
2823 case KBD_CMD_SET_RATE
:
2824 s
->kbd_write_cmd
= val
;
2826 case KBD_CMD_RESET_DISABLE
:
2827 kbd_reset_keyboard(s
);
2828 s
->scan_enabled
= 0;
2829 kbd_queue(s
, KBD_REPLY_ACK
, 0);
2831 case KBD_CMD_RESET_ENABLE
:
2832 kbd_reset_keyboard(s
);
2833 s
->scan_enabled
= 1;
2834 kbd_queue(s
, KBD_REPLY_ACK
, 0);
2837 kbd_reset_keyboard(s
);
2838 kbd_queue(s
, KBD_REPLY_ACK
, 0);
2839 kbd_queue(s
, KBD_REPLY_POR
, 0);
2842 kbd_queue(s
, KBD_REPLY_ACK
, 0);
2846 case KBD_CMD_SET_LEDS
:
2847 kbd_queue(s
, KBD_REPLY_ACK
, 0);
2848 s
->kbd_write_cmd
= -1;
2850 case KBD_CMD_SET_RATE
:
2851 kbd_queue(s
, KBD_REPLY_ACK
, 0);
2852 s
->kbd_write_cmd
= -1;
2857 static void kbd_mouse_send_packet(KBDState
*s
)
2865 /* XXX: increase range to 8 bits ? */
2868 else if (dx1
< -127)
2872 else if (dy1
< -127)
2874 b
= 0x08 | ((dx1
< 0) << 4) | ((dy1
< 0) << 5) | (s
->mouse_buttons
& 0x07);
2876 kbd_queue(s
, dx1
& 0xff, 1);
2877 kbd_queue(s
, dy1
& 0xff, 1);
2878 /* extra byte for IMPS/2 or IMEX */
2879 switch(s
->mouse_type
) {
2885 else if (dz1
< -127)
2887 kbd_queue(s
, dz1
& 0xff, 1);
2894 b
= (dz1
& 0x0f) | ((s
->mouse_buttons
& 0x18) << 1);
2905 void kbd_mouse_event(int dx
, int dy
, int dz
, int buttons_state
)
2907 KBDState
*s
= &kbd_state
;
2909 /* check if deltas are recorded when disabled */
2910 if (!(s
->mouse_status
& MOUSE_STATUS_ENABLED
))
2916 s
->mouse_buttons
= buttons_state
;
2918 if (!(s
->mouse_status
& MOUSE_STATUS_REMOTE
) &&
2919 (s
->queues
[1].count
< (KBD_QUEUE_SIZE
- 16))) {
2921 /* if not remote, send event. Multiple events are sent if
2923 kbd_mouse_send_packet(s
);
2924 if (s
->mouse_dx
== 0 && s
->mouse_dy
== 0 && s
->mouse_dz
== 0)
2930 static void kbd_write_mouse(KBDState
*s
, int val
)
2933 printf("kbd: write mouse 0x%02x\n", val
);
2935 switch(s
->mouse_write_cmd
) {
2939 if (s
->mouse_wrap
) {
2940 if (val
== AUX_RESET_WRAP
) {
2942 kbd_queue(s
, AUX_ACK
, 1);
2944 } else if (val
!= AUX_RESET
) {
2945 kbd_queue(s
, val
, 1);
2950 case AUX_SET_SCALE11
:
2951 s
->mouse_status
&= ~MOUSE_STATUS_SCALE21
;
2952 kbd_queue(s
, AUX_ACK
, 1);
2954 case AUX_SET_SCALE21
:
2955 s
->mouse_status
|= MOUSE_STATUS_SCALE21
;
2956 kbd_queue(s
, AUX_ACK
, 1);
2958 case AUX_SET_STREAM
:
2959 s
->mouse_status
&= ~MOUSE_STATUS_REMOTE
;
2960 kbd_queue(s
, AUX_ACK
, 1);
2964 kbd_queue(s
, AUX_ACK
, 1);
2966 case AUX_SET_REMOTE
:
2967 s
->mouse_status
|= MOUSE_STATUS_REMOTE
;
2968 kbd_queue(s
, AUX_ACK
, 1);
2971 kbd_queue(s
, AUX_ACK
, 1);
2972 kbd_queue(s
, s
->mouse_type
, 1);
2975 case AUX_SET_SAMPLE
:
2976 s
->mouse_write_cmd
= val
;
2977 kbd_queue(s
, AUX_ACK
, 1);
2980 kbd_queue(s
, AUX_ACK
, 1);
2981 kbd_queue(s
, s
->mouse_status
, 1);
2982 kbd_queue(s
, s
->mouse_resolution
, 1);
2983 kbd_queue(s
, s
->mouse_sample_rate
, 1);
2986 kbd_queue(s
, AUX_ACK
, 1);
2987 kbd_mouse_send_packet(s
);
2989 case AUX_ENABLE_DEV
:
2990 s
->mouse_status
|= MOUSE_STATUS_ENABLED
;
2991 kbd_queue(s
, AUX_ACK
, 1);
2993 case AUX_DISABLE_DEV
:
2994 s
->mouse_status
&= ~MOUSE_STATUS_ENABLED
;
2995 kbd_queue(s
, AUX_ACK
, 1);
2997 case AUX_SET_DEFAULT
:
2998 s
->mouse_sample_rate
= 100;
2999 s
->mouse_resolution
= 2;
3000 s
->mouse_status
= 0;
3001 kbd_queue(s
, AUX_ACK
, 1);
3004 s
->mouse_sample_rate
= 100;
3005 s
->mouse_resolution
= 2;
3006 s
->mouse_status
= 0;
3007 kbd_queue(s
, AUX_ACK
, 1);
3008 kbd_queue(s
, 0xaa, 1);
3009 kbd_queue(s
, s
->mouse_type
, 1);
3015 case AUX_SET_SAMPLE
:
3016 s
->mouse_sample_rate
= val
;
3018 /* detect IMPS/2 or IMEX */
3019 switch(s
->mouse_detect_state
) {
3023 s
->mouse_detect_state
= 1;
3027 s
->mouse_detect_state
= 2;
3028 else if (val
== 200)
3029 s
->mouse_detect_state
= 3;
3031 s
->mouse_detect_state
= 0;
3035 s
->mouse_type
= 3; /* IMPS/2 */
3036 s
->mouse_detect_state
= 0;
3040 s
->mouse_type
= 4; /* IMEX */
3041 s
->mouse_detect_state
= 0;
3045 kbd_queue(s
, AUX_ACK
, 1);
3046 s
->mouse_write_cmd
= -1;
3049 s
->mouse_resolution
= val
;
3050 kbd_queue(s
, AUX_ACK
, 1);
3051 s
->mouse_write_cmd
= -1;
3056 void kbd_write_data(CPUX86State
*env
, uint32_t addr
, uint32_t val
)
3058 KBDState
*s
= &kbd_state
;
3061 printf("kbd: write data=0x%02x\n", val
);
3064 switch(s
->write_cmd
) {
3066 kbd_write_keyboard(s
, val
);
3068 case KBD_CCMD_WRITE_MODE
:
3072 case KBD_CCMD_WRITE_OBUF
:
3073 kbd_queue(s
, val
, 0);
3075 case KBD_CCMD_WRITE_AUX_OBUF
:
3076 kbd_queue(s
, val
, 1);
3078 case KBD_CCMD_WRITE_OUTPORT
:
3079 a20_enabled
= (val
>> 1) & 1;
3081 reset_requested
= 1;
3082 cpu_x86_interrupt(global_env
, CPU_INTERRUPT_EXIT
);
3085 case KBD_CCMD_WRITE_MOUSE
:
3086 kbd_write_mouse(s
, val
);
3094 void kbd_reset(KBDState
*s
)
3099 s
->kbd_write_cmd
= -1;
3100 s
->mouse_write_cmd
= -1;
3101 s
->mode
= KBD_MODE_KBD_INT
| KBD_MODE_MOUSE_INT
;
3102 s
->status
= KBD_STAT_CMD
| KBD_STAT_UNLOCKED
;
3103 for(i
= 0; i
< 2; i
++) {
3113 kbd_reset(&kbd_state
);
3114 register_ioport_read(0x60, 1, kbd_read_data
, 1);
3115 register_ioport_write(0x60, 1, kbd_write_data
, 1);
3116 register_ioport_read(0x64, 1, kbd_read_status
, 1);
3117 register_ioport_write(0x64, 1, kbd_write_command
, 1);
3120 /***********************************************************/
3121 /* Bochs BIOS debug ports */
3123 void bochs_bios_write(CPUX86State
*env
, uint32_t addr
, uint32_t val
)
3126 /* Bochs BIOS messages */
3129 fprintf(stderr
, "BIOS panic at rombios.c, line %d\n", val
);
3134 fprintf(stderr
, "%c", val
);
3138 /* LGPL'ed VGA BIOS messages */
3141 fprintf(stderr
, "VGA BIOS panic, line %d\n", val
);
3146 fprintf(stderr
, "%c", val
);
3152 void bochs_bios_init(void)
3154 register_ioport_write(0x400, 1, bochs_bios_write
, 2);
3155 register_ioport_write(0x401, 1, bochs_bios_write
, 2);
3156 register_ioport_write(0x402, 1, bochs_bios_write
, 1);
3157 register_ioport_write(0x403, 1, bochs_bios_write
, 1);
3159 register_ioport_write(0x501, 1, bochs_bios_write
, 2);
3160 register_ioport_write(0x502, 1, bochs_bios_write
, 2);
3161 register_ioport_write(0x500, 1, bochs_bios_write
, 1);
3162 register_ioport_write(0x503, 1, bochs_bios_write
, 1);
3165 /***********************************************************/
3168 /* init terminal so that we can grab keys */
3169 static struct termios oldtty
;
3171 static void term_exit(void)
3173 tcsetattr (0, TCSANOW
, &oldtty
);
3176 static void term_init(void)
3180 tcgetattr (0, &tty
);
3183 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
3184 |INLCR
|IGNCR
|ICRNL
|IXON
);
3185 tty
.c_oflag
|= OPOST
;
3186 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
);
3187 /* if graphical mode, we allow Ctrl-C handling */
3189 tty
.c_lflag
&= ~ISIG
;
3190 tty
.c_cflag
&= ~(CSIZE
|PARENB
);
3193 tty
.c_cc
[VTIME
] = 0;
3195 tcsetattr (0, TCSANOW
, &tty
);
3199 fcntl(0, F_SETFL
, O_NONBLOCK
);
3202 static void dumb_update(DisplayState
*ds
, int x
, int y
, int w
, int h
)
3206 static void dumb_resize(DisplayState
*ds
, int w
, int h
)
3210 static void dumb_refresh(DisplayState
*ds
)
3212 vga_update_display();
3215 void dumb_display_init(DisplayState
*ds
)
3220 ds
->dpy_update
= dumb_update
;
3221 ds
->dpy_resize
= dumb_resize
;
3222 ds
->dpy_refresh
= dumb_refresh
;
3225 /***********************************************************/
3226 /* cpu signal handler */
3227 static void host_segv_handler(int host_signum
, siginfo_t
*info
,
3230 if (cpu_signal_handler(host_signum
, info
, puc
))
3236 static int timer_irq_pending
;
3237 static int timer_irq_count
;
3239 static int timer_ms
;
3240 static int gui_refresh_pending
, gui_refresh_count
;
3242 static void host_alarm_handler(int host_signum
, siginfo_t
*info
,
3245 /* NOTE: since usually the OS asks a 100 Hz clock, there can be
3246 some drift between cpu_get_ticks() and the interrupt time. So
3247 we queue some interrupts to avoid missing some */
3248 timer_irq_count
+= pit_get_out_edges(&pit_channels
[0]);
3249 if (timer_irq_count
) {
3250 if (timer_irq_count
> 2)
3251 timer_irq_count
= 2;
3253 timer_irq_pending
= 1;
3255 gui_refresh_count
+= timer_ms
;
3256 if (gui_refresh_count
>= GUI_REFRESH_INTERVAL
) {
3257 gui_refresh_count
= 0;
3258 gui_refresh_pending
= 1;
3261 if (gui_refresh_pending
|| timer_irq_pending
) {
3262 /* just exit from the cpu to have a chance to handle timers */
3263 cpu_x86_interrupt(global_env
, CPU_INTERRUPT_EXIT
);
3267 unsigned long mmap_addr
= PHYS_RAM_BASE
;
3269 void *get_mmap_addr(unsigned long size
)
3273 mmap_addr
+= ((size
+ 4095) & ~4095) + 4096;
3274 return (void *)addr
;
3277 /* main execution loop */
3279 CPUState
*cpu_gdbstub_get_env(void *opaque
)
3284 int main_loop(void *opaque
)
3286 struct pollfd ufds
[3], *pf
, *serial_ufd
, *net_ufd
, *gdb_ufd
;
3287 int ret
, n
, timeout
, serial_ok
;
3289 CPUState
*env
= global_env
;
3292 /* initialize terminal only there so that the user has a
3293 chance to stop QEMU with Ctrl-C before the gdb connection
3301 ret
= cpu_x86_exec(env
);
3302 if (reset_requested
)
3304 if (ret
== EXCP_DEBUG
)
3306 /* if hlt instruction, we wait until the next IRQ */
3307 if (ret
== EXCP_HLT
)
3311 /* poll any events */
3314 if (serial_ok
&& !(serial_ports
[0].lsr
& UART_LSR_DR
)) {
3317 pf
->events
= POLLIN
;
3321 if (net_fd
> 0 && ne2000_can_receive(&ne2000_state
)) {
3324 pf
->events
= POLLIN
;
3328 if (gdbstub_fd
> 0) {
3330 pf
->fd
= gdbstub_fd
;
3331 pf
->events
= POLLIN
;
3335 ret
= poll(ufds
, pf
- ufds
, timeout
);
3337 if (serial_ufd
&& (serial_ufd
->revents
& POLLIN
)) {
3338 n
= read(0, &ch
, 1);
3340 serial_received_byte(&serial_ports
[0], ch
);
3342 /* Closed, stop polling. */
3346 if (net_ufd
&& (net_ufd
->revents
& POLLIN
)) {
3347 uint8_t buf
[MAX_ETH_FRAME_SIZE
];
3349 n
= read(net_fd
, buf
, MAX_ETH_FRAME_SIZE
);
3352 memset(buf
+ n
, 0, 60 - n
);
3355 ne2000_receive(&ne2000_state
, buf
, n
);
3358 if (gdb_ufd
&& (gdb_ufd
->revents
& POLLIN
)) {
3360 /* stop emulation if requested by gdb */
3361 n
= read(gdbstub_fd
, buf
, 1);
3368 if (timer_irq_pending
) {
3371 timer_irq_pending
= 0;
3375 if (gui_refresh_pending
) {
3376 display_state
.dpy_refresh(&display_state
);
3377 gui_refresh_pending
= 0;
3380 return EXCP_INTERRUPT
;
3385 printf("QEMU PC emulator version " QEMU_VERSION
", Copyright (c) 2003 Fabrice Bellard\n"
3386 "usage: qemu [options] [disk_image]\n"
3388 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
3390 "Standard options:\n"
3391 "-hda file use 'file' as IDE hard disk 0 image\n"
3392 "-hdb file use 'file' as IDE hard disk 1 image\n"
3393 "-snapshot write to temporary files instead of disk image files\n"
3394 "-m megs set virtual RAM size to megs MB\n"
3395 "-n script set network init script [default=%s]\n"
3396 "-tun-fd fd this fd talks to tap/tun, use it.\n"
3397 "-nographic disable graphical output\n"
3399 "Linux boot specific (does not require PC BIOS):\n"
3400 "-kernel bzImage use 'bzImage' as kernel image\n"
3401 "-append cmdline use 'cmdline' as kernel command line\n"
3402 "-initrd file use 'file' as initial ram disk\n"
3404 "Debug/Expert options:\n"
3405 "-s wait gdb connection to port %d\n"
3406 "-p port change gdb connection port\n"
3407 "-d output log in /tmp/vl.log\n"
3408 "-hdachs c,h,s force hard disk 0 geometry (usually qemu can guess it)\n"
3409 "-L path set the directory for the BIOS and VGA BIOS\n"
3411 "During emulation, use C-a h to get terminal commands:\n",
3412 DEFAULT_NETWORK_SCRIPT
, DEFAULT_GDBSTUB_PORT
);
3417 struct option long_options
[] = {
3418 { "initrd", 1, NULL
, 0, },
3419 { "hda", 1, NULL
, 0, },
3420 { "hdb", 1, NULL
, 0, },
3421 { "snapshot", 0, NULL
, 0, },
3422 { "hdachs", 1, NULL
, 0, },
3423 { "nographic", 0, NULL
, 0, },
3424 { "kernel", 1, NULL
, 0, },
3425 { "append", 1, NULL
, 0, },
3426 { "tun-fd", 1, NULL
, 0, },
3427 { NULL
, 0, NULL
, 0 },
3431 /* SDL use the pthreads and they modify sigaction. We don't
3433 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 3)
3434 extern void __libc_sigaction();
3435 #define sigaction(sig, act, oact) __libc_sigaction(sig, act, oact)
3437 extern void __sigaction();
3438 #define sigaction(sig, act, oact) __sigaction(sig, act, oact)
3440 #endif /* CONFIG_SDL */
3442 int main(int argc
, char **argv
)
3444 int c
, ret
, initrd_size
, i
, use_gdbstub
, gdbstub_port
, long_index
;
3445 int snapshot
, linux_boot
, total_ram_size
;
3446 struct linux_params
*params
;
3447 struct sigaction act
;
3448 struct itimerval itv
;
3450 const char *tmpdir
, *initrd_filename
;
3451 const char *hd_filename
[MAX_DISKS
];
3452 const char *kernel_filename
, *kernel_cmdline
;
3453 DisplayState
*ds
= &display_state
;
3455 /* we never want that malloc() uses mmap() */
3456 mallopt(M_MMAP_THRESHOLD
, 4096 * 1024);
3457 initrd_filename
= NULL
;
3458 for(i
= 0; i
< MAX_DISKS
; i
++)
3459 hd_filename
[i
] = NULL
;
3460 phys_ram_size
= 32 * 1024 * 1024;
3461 vga_ram_size
= VGA_RAM_SIZE
;
3462 pstrcpy(network_script
, sizeof(network_script
), DEFAULT_NETWORK_SCRIPT
);
3464 gdbstub_port
= DEFAULT_GDBSTUB_PORT
;
3467 kernel_filename
= NULL
;
3468 kernel_cmdline
= "";
3470 c
= getopt_long_only(argc
, argv
, "hm:dn:sp:L:", long_options
, &long_index
);
3475 switch(long_index
) {
3477 initrd_filename
= optarg
;
3480 hd_filename
[0] = optarg
;
3483 hd_filename
[1] = optarg
;
3490 int cyls
, heads
, secs
;
3493 cyls
= strtol(p
, (char **)&p
, 0);
3497 heads
= strtol(p
, (char **)&p
, 0);
3501 secs
= strtol(p
, (char **)&p
, 0);
3504 ide_state
[0].cylinders
= cyls
;
3505 ide_state
[0].heads
= heads
;
3506 ide_state
[0].sectors
= secs
;
3514 kernel_filename
= optarg
;
3517 kernel_cmdline
= optarg
;
3520 net_fd
= atoi(optarg
);
3528 phys_ram_size
= atoi(optarg
) * 1024 * 1024;
3529 if (phys_ram_size
<= 0)
3531 if (phys_ram_size
> PHYS_RAM_MAX_SIZE
) {
3532 fprintf(stderr
, "vl: at most %d MB RAM can be simulated\n",
3533 PHYS_RAM_MAX_SIZE
/ (1024 * 1024));
3541 pstrcpy(network_script
, sizeof(network_script
), optarg
);
3547 gdbstub_port
= atoi(optarg
);
3550 interp_prefix
= optarg
;
3555 if (optind
< argc
) {
3556 hd_filename
[0] = argv
[optind
++];
3559 linux_boot
= (kernel_filename
!= NULL
);
3561 if (!linux_boot
&& hd_filename
[0] == '\0')
3565 setvbuf(stdout
, NULL
, _IOLBF
, 0);
3567 logfile
= fopen(DEBUG_LOGFILE
, "w");
3569 perror(DEBUG_LOGFILE
);
3572 setvbuf(logfile
, NULL
, _IOLBF
, 0);
3575 /* init network tun interface */
3579 /* init the memory */
3580 tmpdir
= getenv("QEMU_TMPDIR");
3583 snprintf(phys_ram_file
, sizeof(phys_ram_file
), "%s/vlXXXXXX", tmpdir
);
3584 if (mkstemp(phys_ram_file
) < 0) {
3585 fprintf(stderr
, "Could not create temporary memory file '%s'\n",
3589 phys_ram_fd
= open(phys_ram_file
, O_CREAT
| O_TRUNC
| O_RDWR
, 0600);
3590 if (phys_ram_fd
< 0) {
3591 fprintf(stderr
, "Could not open temporary memory file '%s'\n",
3595 total_ram_size
= phys_ram_size
+ vga_ram_size
;
3596 ftruncate(phys_ram_fd
, total_ram_size
);
3597 unlink(phys_ram_file
);
3598 phys_ram_base
= mmap(get_mmap_addr(total_ram_size
),
3600 PROT_WRITE
| PROT_READ
, MAP_SHARED
| MAP_FIXED
,
3602 if (phys_ram_base
== MAP_FAILED
) {
3603 fprintf(stderr
, "Could not map physical memory\n");
3607 /* open the virtual block devices */
3608 for(i
= 0; i
< MAX_DISKS
; i
++) {
3609 if (hd_filename
[i
]) {
3610 bs_table
[i
] = bdrv_open(hd_filename
[i
], snapshot
);
3612 fprintf(stderr
, "vl: could not open hard disk image '%s\n",
3619 /* init CPU state */
3622 cpu_single_env
= env
;
3627 cpu_register_physical_memory(0, phys_ram_size
, 0);
3630 /* now we can load the kernel */
3631 ret
= load_kernel(kernel_filename
, phys_ram_base
+ KERNEL_LOAD_ADDR
);
3633 fprintf(stderr
, "vl: could not load kernel '%s'\n",
3640 if (initrd_filename
) {
3641 initrd_size
= load_image(initrd_filename
, phys_ram_base
+ INITRD_LOAD_ADDR
);
3642 if (initrd_size
< 0) {
3643 fprintf(stderr
, "vl: could not load initial ram disk '%s'\n",
3649 /* init kernel params */
3650 params
= (void *)(phys_ram_base
+ KERNEL_PARAMS_ADDR
);
3651 memset(params
, 0, sizeof(struct linux_params
));
3652 params
->mount_root_rdonly
= 0;
3653 params
->cl_magic
= 0xA33F;
3654 params
->cl_offset
= params
->commandline
- (uint8_t *)params
;
3655 params
->alt_mem_k
= (phys_ram_size
/ 1024) - 1024;
3656 pstrcat(params
->commandline
, sizeof(params
->commandline
), kernel_cmdline
);
3657 params
->loader_type
= 0x01;
3658 if (initrd_size
> 0) {
3659 params
->initrd_start
= INITRD_LOAD_ADDR
;
3660 params
->initrd_size
= initrd_size
;
3662 params
->orig_video_lines
= 25;
3663 params
->orig_video_cols
= 80;
3665 /* setup basic memory access */
3666 env
->cr
[0] = 0x00000033;
3667 cpu_x86_init_mmu(env
);
3669 memset(params
->idt_table
, 0, sizeof(params
->idt_table
));
3671 params
->gdt_table
[2] = 0x00cf9a000000ffffLL
; /* KERNEL_CS */
3672 params
->gdt_table
[3] = 0x00cf92000000ffffLL
; /* KERNEL_DS */
3674 env
->idt
.base
= (void *)params
->idt_table
;
3675 env
->idt
.limit
= sizeof(params
->idt_table
) - 1;
3676 env
->gdt
.base
= (void *)params
->gdt_table
;
3677 env
->gdt
.limit
= sizeof(params
->gdt_table
) - 1;
3679 cpu_x86_load_seg_cache(env
, R_CS
, KERNEL_CS
, NULL
, 0xffffffff, 0x00cf9a00);
3680 cpu_x86_load_seg_cache(env
, R_DS
, KERNEL_DS
, NULL
, 0xffffffff, 0x00cf9200);
3681 cpu_x86_load_seg_cache(env
, R_ES
, KERNEL_DS
, NULL
, 0xffffffff, 0x00cf9200);
3682 cpu_x86_load_seg_cache(env
, R_SS
, KERNEL_DS
, NULL
, 0xffffffff, 0x00cf9200);
3683 cpu_x86_load_seg_cache(env
, R_FS
, KERNEL_DS
, NULL
, 0xffffffff, 0x00cf9200);
3684 cpu_x86_load_seg_cache(env
, R_GS
, KERNEL_DS
, NULL
, 0xffffffff, 0x00cf9200);
3686 env
->eip
= KERNEL_LOAD_ADDR
;
3687 env
->regs
[R_ESI
] = KERNEL_PARAMS_ADDR
;
3696 snprintf(buf
, sizeof(buf
), "%s/%s", interp_prefix
, BIOS_FILENAME
);
3697 ret
= load_image(buf
, phys_ram_base
+ 0x000f0000);
3698 if (ret
!= 0x10000) {
3699 fprintf(stderr
, "vl: could not load PC bios '%s'\n", BIOS_FILENAME
);
3704 snprintf(buf
, sizeof(buf
), "%s/%s", interp_prefix
, VGABIOS_FILENAME
);
3705 ret
= load_image(buf
, phys_ram_base
+ 0x000c0000);
3707 /* setup basic memory access */
3708 env
->cr
[0] = 0x60000010;
3709 cpu_x86_init_mmu(env
);
3711 env
->idt
.limit
= 0xffff;
3712 env
->gdt
.limit
= 0xffff;
3713 env
->ldt
.limit
= 0xffff;
3715 /* not correct (CS base=0xffff0000) */
3716 cpu_x86_load_seg_cache(env
, R_CS
, 0xf000, (uint8_t *)0x000f0000, 0xffff, 0);
3717 cpu_x86_load_seg_cache(env
, R_DS
, 0, NULL
, 0xffff, 0);
3718 cpu_x86_load_seg_cache(env
, R_ES
, 0, NULL
, 0xffff, 0);
3719 cpu_x86_load_seg_cache(env
, R_SS
, 0, NULL
, 0xffff, 0);
3720 cpu_x86_load_seg_cache(env
, R_FS
, 0, NULL
, 0xffff, 0);
3721 cpu_x86_load_seg_cache(env
, R_GS
, 0, NULL
, 0xffff, 0);
3724 env
->regs
[R_EDX
] = 0x600; /* indicate P6 processor */
3733 dumb_display_init(ds
);
3736 sdl_display_init(ds
);
3738 dumb_display_init(ds
);
3741 /* init basic PC hardware */
3742 register_ioport_write(0x80, 1, ioport80_write
, 1);
3744 vga_init(ds
, phys_ram_base
+ phys_ram_size
, phys_ram_size
,
3754 /* setup cpu signal handlers for MMU / self modifying code handling */
3755 sigfillset(&act
.sa_mask
);
3756 act
.sa_flags
= SA_SIGINFO
;
3757 act
.sa_sigaction
= host_segv_handler
;
3758 sigaction(SIGSEGV
, &act
, NULL
);
3759 sigaction(SIGBUS
, &act
, NULL
);
3761 act
.sa_sigaction
= host_alarm_handler
;
3762 sigaction(SIGALRM
, &act
, NULL
);
3764 itv
.it_interval
.tv_sec
= 0;
3765 itv
.it_interval
.tv_usec
= 1000;
3766 itv
.it_value
.tv_sec
= 0;
3767 itv
.it_value
.tv_usec
= 10 * 1000;
3768 setitimer(ITIMER_REAL
, &itv
, NULL
);
3769 /* we probe the tick duration of the kernel to inform the user if
3770 the emulated kernel requested a too high timer frequency */
3771 getitimer(ITIMER_REAL
, &itv
);
3772 timer_ms
= itv
.it_interval
.tv_usec
/ 1000;
3773 pit_min_timer_count
= ((uint64_t)itv
.it_interval
.tv_usec
* PIT_FREQ
) /
3777 cpu_gdbstub(NULL
, main_loop
, gdbstub_port
);