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>
50 #define DEBUG_LOGFILE "/tmp/vl.log"
51 #define DEFAULT_NETWORK_SCRIPT "/etc/vl-ifup"
53 //#define DEBUG_UNUSED_IOPORT
55 #define PHYS_RAM_BASE 0xa8000000
56 #define KERNEL_LOAD_ADDR 0x00100000
57 #define INITRD_LOAD_ADDR 0x00400000
58 #define KERNEL_PARAMS_ADDR 0x00090000
60 /* from plex86 (BSD license) */
61 struct __attribute__ ((packed
)) linux_params
{
62 // For 0x00..0x3f, see 'struct screen_info' in linux/include/linux/tty.h.
63 // I just padded out the VESA parts, rather than define them.
65 /* 0x000 */ uint8_t orig_x
;
66 /* 0x001 */ uint8_t orig_y
;
67 /* 0x002 */ uint16_t ext_mem_k
;
68 /* 0x004 */ uint16_t orig_video_page
;
69 /* 0x006 */ uint8_t orig_video_mode
;
70 /* 0x007 */ uint8_t orig_video_cols
;
71 /* 0x008 */ uint16_t unused1
;
72 /* 0x00a */ uint16_t orig_video_ega_bx
;
73 /* 0x00c */ uint16_t unused2
;
74 /* 0x00e */ uint8_t orig_video_lines
;
75 /* 0x00f */ uint8_t orig_video_isVGA
;
76 /* 0x010 */ uint16_t orig_video_points
;
77 /* 0x012 */ uint8_t pad0
[0x20 - 0x12]; // VESA info.
78 /* 0x020 */ uint16_t cl_magic
; // Commandline magic number (0xA33F)
79 /* 0x022 */ uint16_t cl_offset
; // Commandline offset. Address of commandline
80 // is calculated as 0x90000 + cl_offset, bu
81 // only if cl_magic == 0xA33F.
82 /* 0x024 */ uint8_t pad1
[0x40 - 0x24]; // VESA info.
84 /* 0x040 */ uint8_t apm_bios_info
[20]; // struct apm_bios_info
85 /* 0x054 */ uint8_t pad2
[0x80 - 0x54];
87 // Following 2 from 'struct drive_info_struct' in drivers/block/cciss.h.
88 // Might be truncated?
89 /* 0x080 */ uint8_t hd0_info
[16]; // hd0-disk-parameter from intvector 0x41
90 /* 0x090 */ uint8_t hd1_info
[16]; // hd1-disk-parameter from intvector 0x46
92 // System description table truncated to 16 bytes
93 // From 'struct sys_desc_table_struct' in linux/arch/i386/kernel/setup.c.
94 /* 0x0a0 */ uint16_t sys_description_len
;
95 /* 0x0a2 */ uint8_t sys_description_table
[14];
97 // [1] machine submodel id
101 /* 0x0b0 */ uint8_t pad3
[0x1e0 - 0xb0];
102 /* 0x1e0 */ uint32_t alt_mem_k
;
103 /* 0x1e4 */ uint8_t pad4
[4];
104 /* 0x1e8 */ uint8_t e820map_entries
;
105 /* 0x1e9 */ uint8_t eddbuf_entries
; // EDD_NR
106 /* 0x1ea */ uint8_t pad5
[0x1f1 - 0x1ea];
107 /* 0x1f1 */ uint8_t setup_sects
; // size of setup.S, number of sectors
108 /* 0x1f2 */ uint16_t mount_root_rdonly
; // MOUNT_ROOT_RDONLY (if !=0)
109 /* 0x1f4 */ uint16_t sys_size
; // size of compressed kernel-part in the
110 // (b)zImage-file (in 16 byte units, rounded up)
111 /* 0x1f6 */ uint16_t swap_dev
; // (unused AFAIK)
112 /* 0x1f8 */ uint16_t ramdisk_flags
;
113 /* 0x1fa */ uint16_t vga_mode
; // (old one)
114 /* 0x1fc */ uint16_t orig_root_dev
; // (high=Major, low=minor)
115 /* 0x1fe */ uint8_t pad6
[1];
116 /* 0x1ff */ uint8_t aux_device_info
;
117 /* 0x200 */ uint16_t jump_setup
; // Jump to start of setup code,
118 // aka "reserved" field.
119 /* 0x202 */ uint8_t setup_signature
[4]; // Signature for SETUP-header, ="HdrS"
120 /* 0x206 */ uint16_t header_format_version
; // Version number of header format;
121 /* 0x208 */ uint8_t setup_S_temp0
[8]; // Used by setup.S for communication with
122 // boot loaders, look there.
123 /* 0x210 */ uint8_t loader_type
;
128 // T=2: bootsect-loader
132 /* 0x211 */ uint8_t loadflags
;
133 // bit0 = 1: kernel is loaded high (bzImage)
134 // bit7 = 1: Heap and pointer (see below) set by boot
136 /* 0x212 */ uint16_t setup_S_temp1
;
137 /* 0x214 */ uint32_t kernel_start
;
138 /* 0x218 */ uint32_t initrd_start
;
139 /* 0x21c */ uint32_t initrd_size
;
140 /* 0x220 */ uint8_t setup_S_temp2
[4];
141 /* 0x224 */ uint16_t setup_S_heap_end_pointer
;
142 /* 0x226 */ uint8_t pad7
[0x2d0 - 0x226];
144 /* 0x2d0 : Int 15, ax=e820 memory map. */
145 // (linux/include/asm-i386/e820.h, 'struct e820entry')
148 #define E820_RESERVED 2
149 #define E820_ACPI 3 /* usable as RAM once ACPI tables have been read */
157 /* 0x550 */ uint8_t pad8
[0x600 - 0x550];
159 // BIOS Enhanced Disk Drive Services.
160 // (From linux/include/asm-i386/edd.h, 'struct edd_info')
161 // Each 'struct edd_info is 78 bytes, times a max of 6 structs in array.
162 /* 0x600 */ uint8_t eddbuf
[0x7d4 - 0x600];
164 /* 0x7d4 */ uint8_t pad9
[0x800 - 0x7d4];
165 /* 0x800 */ uint8_t commandline
[0x800];
168 uint64_t gdt_table
[256];
169 uint64_t idt_table
[48];
172 #define KERNEL_CS 0x10
173 #define KERNEL_DS 0x18
175 typedef void (IOPortWriteFunc
)(CPUX86State
*env
, uint32_t address
, uint32_t data
);
176 typedef uint32_t (IOPortReadFunc
)(CPUX86State
*env
, uint32_t address
);
178 #define MAX_IOPORTS 1024
180 char phys_ram_file
[1024];
181 CPUX86State
*global_env
;
182 CPUX86State
*cpu_single_env
;
183 FILE *logfile
= NULL
;
185 IOPortReadFunc
*ioport_readb_table
[MAX_IOPORTS
];
186 IOPortWriteFunc
*ioport_writeb_table
[MAX_IOPORTS
];
187 IOPortReadFunc
*ioport_readw_table
[MAX_IOPORTS
];
188 IOPortWriteFunc
*ioport_writew_table
[MAX_IOPORTS
];
190 /***********************************************************/
193 uint32_t default_ioport_readb(CPUX86State
*env
, uint32_t address
)
195 #ifdef DEBUG_UNUSED_IOPORT
196 fprintf(stderr
, "inb: port=0x%04x\n", address
);
201 void default_ioport_writeb(CPUX86State
*env
, uint32_t address
, uint32_t data
)
203 #ifdef DEBUG_UNUSED_IOPORT
204 fprintf(stderr
, "outb: port=0x%04x data=0x%02x\n", address
, data
);
208 /* default is to make two byte accesses */
209 uint32_t default_ioport_readw(CPUX86State
*env
, uint32_t address
)
212 data
= ioport_readb_table
[address
](env
, address
);
213 data
|= ioport_readb_table
[address
+ 1](env
, address
+ 1) << 8;
217 void default_ioport_writew(CPUX86State
*env
, uint32_t address
, uint32_t data
)
219 ioport_writeb_table
[address
](env
, address
, data
& 0xff);
220 ioport_writeb_table
[address
+ 1](env
, address
+ 1, (data
>> 8) & 0xff);
223 void init_ioports(void)
227 for(i
= 0; i
< MAX_IOPORTS
; i
++) {
228 ioport_readb_table
[i
] = default_ioport_readb
;
229 ioport_writeb_table
[i
] = default_ioport_writeb
;
230 ioport_readw_table
[i
] = default_ioport_readw
;
231 ioport_writew_table
[i
] = default_ioport_writew
;
235 int register_ioport_readb(int start
, int length
, IOPortReadFunc
*func
)
239 for(i
= start
; i
< start
+ length
; i
++)
240 ioport_readb_table
[i
] = func
;
244 int register_ioport_writeb(int start
, int length
, IOPortWriteFunc
*func
)
248 for(i
= start
; i
< start
+ length
; i
++)
249 ioport_writeb_table
[i
] = func
;
253 int register_ioport_readw(int start
, int length
, IOPortReadFunc
*func
)
257 for(i
= start
; i
< start
+ length
; i
+= 2)
258 ioport_readw_table
[i
] = func
;
262 int register_ioport_writew(int start
, int length
, IOPortWriteFunc
*func
)
266 for(i
= start
; i
< start
+ length
; i
+= 2)
267 ioport_writew_table
[i
] = func
;
271 void pstrcpy(char *buf
, int buf_size
, const char *str
)
281 if (c
== 0 || q
>= buf
+ buf_size
- 1)
288 /* strcat and truncate. */
289 char *pstrcat(char *buf
, int buf_size
, const char *s
)
294 pstrcpy(buf
+ len
, buf_size
- len
, s
);
298 int load_kernel(const char *filename
, uint8_t *addr
)
300 int fd
, size
, setup_sects
;
301 uint8_t bootsect
[512];
303 fd
= open(filename
, O_RDONLY
);
306 if (read(fd
, bootsect
, 512) != 512)
308 setup_sects
= bootsect
[0x1F1];
311 /* skip 16 bit setup code */
312 lseek(fd
, (setup_sects
+ 1) * 512, SEEK_SET
);
313 size
= read(fd
, addr
, 16 * 1024 * 1024);
323 /* return the size or -1 if error */
324 int load_image(const char *filename
, uint8_t *addr
)
327 fd
= open(filename
, O_RDONLY
);
330 size
= lseek(fd
, 0, SEEK_END
);
331 lseek(fd
, 0, SEEK_SET
);
332 if (read(fd
, addr
, size
) != size
) {
340 void cpu_x86_outb(CPUX86State
*env
, int addr
, int val
)
342 ioport_writeb_table
[addr
& (MAX_IOPORTS
- 1)](env
, addr
, val
);
345 void cpu_x86_outw(CPUX86State
*env
, int addr
, int val
)
347 ioport_writew_table
[addr
& (MAX_IOPORTS
- 1)](env
, addr
, val
);
350 void cpu_x86_outl(CPUX86State
*env
, int addr
, int val
)
352 fprintf(stderr
, "outl: port=0x%04x, data=%08x\n", addr
, val
);
355 int cpu_x86_inb(CPUX86State
*env
, int addr
)
357 return ioport_readb_table
[addr
& (MAX_IOPORTS
- 1)](env
, addr
);
360 int cpu_x86_inw(CPUX86State
*env
, int addr
)
362 return ioport_readw_table
[addr
& (MAX_IOPORTS
- 1)](env
, addr
);
365 int cpu_x86_inl(CPUX86State
*env
, int addr
)
367 fprintf(stderr
, "inl: port=0x%04x\n", addr
);
371 /***********************************************************/
372 void ioport80_write(CPUX86State
*env
, uint32_t addr
, uint32_t data
)
376 void hw_error(const char *fmt
, ...)
381 fprintf(stderr
, "qemu: hardware error: ");
382 vfprintf(stderr
, fmt
, ap
);
383 fprintf(stderr
, "\n");
385 cpu_x86_dump_state(global_env
, stderr
, X86_DUMP_FPU
| X86_DUMP_CCOP
);
391 /***********************************************************/
393 static uint8_t vga_index
;
394 static uint8_t vga_regs
[256];
395 static int last_cursor_pos
;
397 void update_console_messages(void)
399 int c
, i
, cursor_pos
, eol
;
401 cursor_pos
= vga_regs
[0x0f] | (vga_regs
[0x0e] << 8);
403 for(i
= last_cursor_pos
; i
< cursor_pos
; i
++) {
404 c
= phys_ram_base
[0xb8000 + (i
) * 2];
415 last_cursor_pos
= cursor_pos
;
418 /* just to see first Linux console messages, we intercept cursor position */
419 void vga_ioport_write(CPUX86State
*env
, uint32_t addr
, uint32_t data
)
426 vga_regs
[vga_index
] = data
;
427 if (vga_index
== 0x0f)
428 update_console_messages();
434 /***********************************************************/
437 #define RTC_SECONDS 0
438 #define RTC_SECONDS_ALARM 1
439 #define RTC_MINUTES 2
440 #define RTC_MINUTES_ALARM 3
442 #define RTC_HOURS_ALARM 5
443 #define RTC_ALARM_DONT_CARE 0xC0
445 #define RTC_DAY_OF_WEEK 6
446 #define RTC_DAY_OF_MONTH 7
455 /* PC cmos mappings */
456 #define REG_EQUIPMENT_BYTE 0x14
458 uint8_t cmos_data
[128];
461 void cmos_ioport_write(CPUX86State
*env
, uint32_t addr
, uint32_t data
)
464 cmos_index
= data
& 0x7f;
468 uint32_t cmos_ioport_read(CPUX86State
*env
, uint32_t addr
)
475 /* toggle update-in-progress bit for Linux (same hack as
477 ret
= cmos_data
[cmos_index
];
478 if (cmos_index
== RTC_REG_A
)
479 cmos_data
[RTC_REG_A
] ^= 0x80;
480 else if (cmos_index
== RTC_REG_C
)
481 cmos_data
[RTC_REG_C
] = 0x00;
487 static inline int to_bcd(int a
)
489 return ((a
/ 10) << 4) | (a
% 10);
499 cmos_data
[RTC_SECONDS
] = to_bcd(tm
->tm_sec
);
500 cmos_data
[RTC_MINUTES
] = to_bcd(tm
->tm_min
);
501 cmos_data
[RTC_HOURS
] = to_bcd(tm
->tm_hour
);
502 cmos_data
[RTC_DAY_OF_WEEK
] = to_bcd(tm
->tm_wday
);
503 cmos_data
[RTC_DAY_OF_MONTH
] = to_bcd(tm
->tm_mday
);
504 cmos_data
[RTC_MONTH
] = to_bcd(tm
->tm_mon
);
505 cmos_data
[RTC_YEAR
] = to_bcd(tm
->tm_year
% 100);
507 cmos_data
[RTC_REG_A
] = 0x26;
508 cmos_data
[RTC_REG_B
] = 0x02;
509 cmos_data
[RTC_REG_C
] = 0x00;
510 cmos_data
[RTC_REG_D
] = 0x80;
512 cmos_data
[REG_EQUIPMENT_BYTE
] = 0x02; /* FPU is there */
514 register_ioport_writeb(0x70, 2, cmos_ioport_write
);
515 register_ioport_readb(0x70, 2, cmos_ioport_read
);
518 /***********************************************************/
519 /* 8259 pic emulation */
521 typedef struct PicState
{
522 uint8_t last_irr
; /* edge detection */
523 uint8_t irr
; /* interrupt request register */
524 uint8_t imr
; /* interrupt mask register */
525 uint8_t isr
; /* interrupt service register */
526 uint8_t priority_add
; /* used to compute irq priority */
528 uint8_t read_reg_select
;
529 uint8_t special_mask
;
532 uint8_t rotate_on_autoeoi
;
533 uint8_t init4
; /* true if 4 byte init */
536 /* 0 is master pic, 1 is slave pic */
538 int pic_irq_requested
;
540 /* set irq level. If an edge is detected, then the IRR is set to 1 */
541 static inline void pic_set_irq1(PicState
*s
, int irq
, int level
)
546 if ((s
->last_irr
& mask
) == 0)
550 s
->last_irr
&= ~mask
;
554 static inline int get_priority(PicState
*s
, int mask
)
560 while ((mask
& (1 << ((priority
+ s
->priority_add
) & 7))) == 0)
565 /* return the pic wanted interrupt. return -1 if none */
566 static int pic_get_irq(PicState
*s
)
568 int mask
, cur_priority
, priority
;
570 mask
= s
->irr
& ~s
->imr
;
571 priority
= get_priority(s
, mask
);
574 /* compute current priority */
575 cur_priority
= get_priority(s
, s
->isr
);
576 if (priority
> cur_priority
) {
577 /* higher priority found: an irq should be generated */
584 void pic_set_irq(int irq
, int level
)
586 pic_set_irq1(&pics
[irq
>> 3], irq
& 7, level
);
589 /* can be called at any time outside cpu_exec() to raise irqs if
591 void pic_handle_irq(void)
595 /* first look at slave pic */
596 irq2
= pic_get_irq(&pics
[1]);
598 /* if irq request by slave pic, signal master PIC */
599 pic_set_irq1(&pics
[0], 2, 1);
600 pic_set_irq1(&pics
[0], 2, 0);
602 /* look at requested irq */
603 irq
= pic_get_irq(&pics
[0]);
607 pic_irq_requested
= 8 + irq2
;
609 /* from master pic */
610 pic_irq_requested
= irq
;
612 global_env
->hard_interrupt_request
= 1;
616 int cpu_x86_get_pic_interrupt(CPUX86State
*env
)
618 int irq
, irq2
, intno
;
620 /* signal the pic that the irq was acked by the CPU */
621 irq
= pic_irq_requested
;
624 pics
[1].isr
|= (1 << irq2
);
625 pics
[1].irr
&= ~(1 << irq2
);
627 intno
= pics
[1].irq_base
+ irq2
;
629 intno
= pics
[0].irq_base
+ irq
;
631 pics
[0].isr
|= (1 << irq
);
632 pics
[0].irr
&= ~(1 << irq
);
636 void pic_ioport_write(CPUX86State
*env
, uint32_t addr
, uint32_t val
)
641 s
= &pics
[addr
>> 7];
646 memset(s
, 0, sizeof(PicState
));
650 hw_error("single mode not supported");
652 hw_error("level sensitive irq not supported");
653 } else if (val
& 0x08) {
655 s
->read_reg_select
= val
& 1;
657 s
->special_mask
= (val
>> 5) & 1;
662 s
->rotate_on_autoeoi
= val
>> 7;
664 case 0x20: /* end of interrupt */
666 priority
= get_priority(s
, s
->isr
);
668 s
->isr
&= ~(1 << ((priority
+ s
->priority_add
) & 7));
671 s
->priority_add
= (s
->priority_add
+ 1) & 7;
675 s
->isr
&= ~(1 << priority
);
678 s
->priority_add
= (val
+ 1) & 7;
682 s
->isr
&= ~(1 << priority
);
683 s
->priority_add
= (priority
+ 1) & 7;
688 switch(s
->init_state
) {
694 s
->irq_base
= val
& 0xf8;
705 s
->auto_eoi
= (val
>> 1) & 1;
712 uint32_t pic_ioport_read(CPUX86State
*env
, uint32_t addr
)
715 s
= &pics
[addr
>> 7];
718 if (s
->read_reg_select
)
729 register_ioport_writeb(0x20, 2, pic_ioport_write
);
730 register_ioport_readb(0x20, 2, pic_ioport_read
);
731 register_ioport_writeb(0xa0, 2, pic_ioport_write
);
732 register_ioport_readb(0xa0, 2, pic_ioport_read
);
735 /***********************************************************/
736 /* 8253 PIT emulation */
738 #define PIT_FREQ 1193182
740 #define RW_STATE_LSB 0
741 #define RW_STATE_MSB 1
742 #define RW_STATE_WORD0 2
743 #define RW_STATE_WORD1 3
744 #define RW_STATE_LATCHED_WORD0 4
745 #define RW_STATE_LATCHED_WORD1 5
747 typedef struct PITChannelState
{
749 uint16_t latched_count
;
752 uint8_t bcd
; /* not supported */
753 uint8_t gate
; /* timer start */
754 int64_t count_load_time
;
757 PITChannelState pit_channels
[3];
760 int64_t ticks_per_sec
;
762 int64_t get_clock(void)
765 gettimeofday(&tv
, NULL
);
766 return tv
.tv_sec
* 1000000LL + tv
.tv_usec
;
769 int64_t cpu_get_ticks(void)
772 asm("rdtsc" : "=A" (val
));
776 void cpu_calibrate_ticks(void)
781 ticks
= cpu_get_ticks();
783 usec
= get_clock() - usec
;
784 ticks
= cpu_get_ticks() - ticks
;
785 ticks_per_sec
= (ticks
* 1000000LL + (usec
>> 1)) / usec
;
788 static int pit_get_count(PITChannelState
*s
)
793 d
= ((cpu_get_ticks() - s
->count_load_time
) * PIT_FREQ
) /
800 counter
= (s
->count
- d
) & 0xffff;
803 counter
= s
->count
- (d
% s
->count
);
809 /* get pit output bit */
810 static int pit_get_out(PITChannelState
*s
)
815 d
= ((cpu_get_ticks() - s
->count_load_time
) * PIT_FREQ
) /
820 out
= (d
>= s
->count
);
823 out
= (d
< s
->count
);
826 if ((d
% s
->count
) == 0 && d
!= 0)
832 out
= (d
% s
->count
) < (s
->count
>> 1);
836 out
= (d
== s
->count
);
842 void pit_ioport_write(CPUX86State
*env
, uint32_t addr
, uint32_t val
)
852 s
= &pit_channels
[channel
];
853 access
= (val
>> 4) & 3;
856 s
->latched_count
= pit_get_count(s
);
857 s
->rw_state
= RW_STATE_LATCHED_WORD0
;
860 s
->rw_state
= access
- 1 + RW_STATE_LSB
;
863 s
->mode
= (val
>> 1) & 7;
866 s
= &pit_channels
[addr
];
867 switch(s
->rw_state
) {
869 s
->count_load_time
= cpu_get_ticks();
873 s
->count_load_time
= cpu_get_ticks();
874 s
->count
= (val
<< 8);
878 if (s
->rw_state
& 1) {
879 s
->count_load_time
= cpu_get_ticks();
880 s
->count
= (s
->latched_count
& 0xff) | (val
<< 8);
882 s
->latched_count
= val
;
890 uint32_t pit_ioport_read(CPUX86State
*env
, uint32_t addr
)
896 s
= &pit_channels
[addr
];
897 switch(s
->rw_state
) {
902 count
= pit_get_count(s
);
904 ret
= (count
>> 8) & 0xff;
911 case RW_STATE_LATCHED_WORD0
:
912 case RW_STATE_LATCHED_WORD1
:
914 ret
= s
->latched_count
>> 8;
916 ret
= s
->latched_count
& 0xff;
923 void speaker_ioport_write(CPUX86State
*env
, uint32_t addr
, uint32_t val
)
925 speaker_data_on
= (val
>> 1) & 1;
926 pit_channels
[2].gate
= val
& 1;
929 uint32_t speaker_ioport_read(CPUX86State
*env
, uint32_t addr
)
932 out
= pit_get_out(&pit_channels
[2]);
933 return (speaker_data_on
<< 1) | pit_channels
[2].gate
| (out
<< 5);
938 pit_channels
[0].gate
= 1;
939 pit_channels
[1].gate
= 1;
940 pit_channels
[2].gate
= 0;
942 register_ioport_writeb(0x40, 4, pit_ioport_write
);
943 register_ioport_readb(0x40, 3, pit_ioport_read
);
945 register_ioport_readb(0x61, 1, speaker_ioport_read
);
946 register_ioport_writeb(0x61, 1, speaker_ioport_write
);
947 cpu_calibrate_ticks();
950 /***********************************************************/
951 /* serial port emulation */
955 #define UART_LCR_DLAB 0x80 /* Divisor latch access bit */
957 #define UART_IER_MSI 0x08 /* Enable Modem status interrupt */
958 #define UART_IER_RLSI 0x04 /* Enable receiver line status interrupt */
959 #define UART_IER_THRI 0x02 /* Enable Transmitter holding register int. */
960 #define UART_IER_RDI 0x01 /* Enable receiver data interrupt */
962 #define UART_IIR_NO_INT 0x01 /* No interrupts pending */
963 #define UART_IIR_ID 0x06 /* Mask for the interrupt ID */
965 #define UART_IIR_MSI 0x00 /* Modem status interrupt */
966 #define UART_IIR_THRI 0x02 /* Transmitter holding register empty */
967 #define UART_IIR_RDI 0x04 /* Receiver data interrupt */
968 #define UART_IIR_RLSI 0x06 /* Receiver line status interrupt */
970 #define UART_LSR_TEMT 0x40 /* Transmitter empty */
971 #define UART_LSR_THRE 0x20 /* Transmit-hold-register empty */
972 #define UART_LSR_BI 0x10 /* Break interrupt indicator */
973 #define UART_LSR_FE 0x08 /* Frame error indicator */
974 #define UART_LSR_PE 0x04 /* Parity error indicator */
975 #define UART_LSR_OE 0x02 /* Overrun error indicator */
976 #define UART_LSR_DR 0x01 /* Receiver data ready */
978 typedef struct SerialState
{
980 uint8_t rbr
; /* receive register */
982 uint8_t iir
; /* read only */
985 uint8_t lsr
; /* read only */
990 SerialState serial_ports
[1];
992 void serial_update_irq(void)
994 SerialState
*s
= &serial_ports
[0];
996 if ((s
->lsr
& UART_LSR_DR
) && (s
->ier
& UART_IER_RDI
)) {
997 s
->iir
= UART_IIR_RDI
;
998 } else if ((s
->lsr
& UART_LSR_THRE
) && (s
->ier
& UART_IER_THRI
)) {
999 s
->iir
= UART_IIR_THRI
;
1001 s
->iir
= UART_IIR_NO_INT
;
1003 if (s
->iir
!= UART_IIR_NO_INT
) {
1004 pic_set_irq(UART_IRQ
, 1);
1006 pic_set_irq(UART_IRQ
, 0);
1010 void serial_ioport_write(CPUX86State
*env
, uint32_t addr
, uint32_t val
)
1012 SerialState
*s
= &serial_ports
[0];
1020 if (s
->lcr
& UART_LCR_DLAB
) {
1021 s
->divider
= (s
->divider
& 0xff00) | val
;
1023 s
->lsr
&= ~UART_LSR_THRE
;
1024 serial_update_irq();
1028 ret
= write(1, &ch
, 1);
1030 s
->lsr
|= UART_LSR_THRE
;
1031 s
->lsr
|= UART_LSR_TEMT
;
1032 serial_update_irq();
1036 if (s
->lcr
& UART_LCR_DLAB
) {
1037 s
->divider
= (s
->divider
& 0x00ff) | (val
<< 8);
1040 serial_update_irq();
1062 uint32_t serial_ioport_read(CPUX86State
*env
, uint32_t addr
)
1064 SerialState
*s
= &serial_ports
[0];
1071 if (s
->lcr
& UART_LCR_DLAB
) {
1072 ret
= s
->divider
& 0xff;
1075 s
->lsr
&= ~(UART_LSR_DR
| UART_LSR_BI
);
1076 serial_update_irq();
1080 if (s
->lcr
& UART_LCR_DLAB
) {
1081 ret
= (s
->divider
>> 8) & 0xff;
1108 #define TERM_ESCAPE 0x01 /* ctrl-a is used for escape */
1109 static int term_got_escape
;
1111 void term_print_help(void)
1114 "C-a h print this help\n"
1115 "C-a x exit emulatior\n"
1116 "C-a b send break (magic sysrq)\n"
1117 "C-a C-a send C-a\n"
1121 /* called when a char is received */
1122 void serial_received_byte(SerialState
*s
, int ch
)
1124 if (term_got_escape
) {
1125 term_got_escape
= 0;
1136 s
->lsr
|= UART_LSR_BI
| UART_LSR_DR
;
1137 serial_update_irq();
1142 } else if (ch
== TERM_ESCAPE
) {
1143 term_got_escape
= 1;
1147 s
->lsr
|= UART_LSR_DR
;
1148 serial_update_irq();
1152 /* init terminal so that we can grab keys */
1153 static struct termios oldtty
;
1155 static void term_exit(void)
1157 tcsetattr (0, TCSANOW
, &oldtty
);
1160 static void term_init(void)
1164 tcgetattr (0, &tty
);
1167 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
1168 |INLCR
|IGNCR
|ICRNL
|IXON
);
1169 tty
.c_oflag
|= OPOST
;
1170 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
|ISIG
);
1171 tty
.c_cflag
&= ~(CSIZE
|PARENB
);
1174 tty
.c_cc
[VTIME
] = 0;
1176 tcsetattr (0, TCSANOW
, &tty
);
1180 fcntl(0, F_SETFL
, O_NONBLOCK
);
1183 void serial_init(void)
1185 SerialState
*s
= &serial_ports
[0];
1187 s
->lsr
= UART_LSR_TEMT
| UART_LSR_THRE
;
1189 register_ioport_writeb(0x3f8, 8, serial_ioport_write
);
1190 register_ioport_readb(0x3f8, 8, serial_ioport_read
);
1195 /***********************************************************/
1196 /* ne2000 emulation */
1198 //#define DEBUG_NE2000
1200 #define NE2000_IOPORT 0x300
1201 #define NE2000_IRQ 9
1203 #define MAX_ETH_FRAME_SIZE 1514
1205 #define E8390_CMD 0x00 /* The command register (for all pages) */
1206 /* Page 0 register offsets. */
1207 #define EN0_CLDALO 0x01 /* Low byte of current local dma addr RD */
1208 #define EN0_STARTPG 0x01 /* Starting page of ring bfr WR */
1209 #define EN0_CLDAHI 0x02 /* High byte of current local dma addr RD */
1210 #define EN0_STOPPG 0x02 /* Ending page +1 of ring bfr WR */
1211 #define EN0_BOUNDARY 0x03 /* Boundary page of ring bfr RD WR */
1212 #define EN0_TSR 0x04 /* Transmit status reg RD */
1213 #define EN0_TPSR 0x04 /* Transmit starting page WR */
1214 #define EN0_NCR 0x05 /* Number of collision reg RD */
1215 #define EN0_TCNTLO 0x05 /* Low byte of tx byte count WR */
1216 #define EN0_FIFO 0x06 /* FIFO RD */
1217 #define EN0_TCNTHI 0x06 /* High byte of tx byte count WR */
1218 #define EN0_ISR 0x07 /* Interrupt status reg RD WR */
1219 #define EN0_CRDALO 0x08 /* low byte of current remote dma address RD */
1220 #define EN0_RSARLO 0x08 /* Remote start address reg 0 */
1221 #define EN0_CRDAHI 0x09 /* high byte, current remote dma address RD */
1222 #define EN0_RSARHI 0x09 /* Remote start address reg 1 */
1223 #define EN0_RCNTLO 0x0a /* Remote byte count reg WR */
1224 #define EN0_RCNTHI 0x0b /* Remote byte count reg WR */
1225 #define EN0_RSR 0x0c /* rx status reg RD */
1226 #define EN0_RXCR 0x0c /* RX configuration reg WR */
1227 #define EN0_TXCR 0x0d /* TX configuration reg WR */
1228 #define EN0_COUNTER0 0x0d /* Rcv alignment error counter RD */
1229 #define EN0_DCFG 0x0e /* Data configuration reg WR */
1230 #define EN0_COUNTER1 0x0e /* Rcv CRC error counter RD */
1231 #define EN0_IMR 0x0f /* Interrupt mask reg WR */
1232 #define EN0_COUNTER2 0x0f /* Rcv missed frame error counter RD */
1234 #define EN1_PHYS 0x11
1235 #define EN1_CURPAG 0x17
1236 #define EN1_MULT 0x18
1238 /* Register accessed at EN_CMD, the 8390 base addr. */
1239 #define E8390_STOP 0x01 /* Stop and reset the chip */
1240 #define E8390_START 0x02 /* Start the chip, clear reset */
1241 #define E8390_TRANS 0x04 /* Transmit a frame */
1242 #define E8390_RREAD 0x08 /* Remote read */
1243 #define E8390_RWRITE 0x10 /* Remote write */
1244 #define E8390_NODMA 0x20 /* Remote DMA */
1245 #define E8390_PAGE0 0x00 /* Select page chip registers */
1246 #define E8390_PAGE1 0x40 /* using the two high-order bits */
1247 #define E8390_PAGE2 0x80 /* Page 3 is invalid. */
1249 /* Bits in EN0_ISR - Interrupt status register */
1250 #define ENISR_RX 0x01 /* Receiver, no error */
1251 #define ENISR_TX 0x02 /* Transmitter, no error */
1252 #define ENISR_RX_ERR 0x04 /* Receiver, with error */
1253 #define ENISR_TX_ERR 0x08 /* Transmitter, with error */
1254 #define ENISR_OVER 0x10 /* Receiver overwrote the ring */
1255 #define ENISR_COUNTERS 0x20 /* Counters need emptying */
1256 #define ENISR_RDC 0x40 /* remote dma complete */
1257 #define ENISR_RESET 0x80 /* Reset completed */
1258 #define ENISR_ALL 0x3f /* Interrupts we will enable */
1260 /* Bits in received packet status byte and EN0_RSR*/
1261 #define ENRSR_RXOK 0x01 /* Received a good packet */
1262 #define ENRSR_CRC 0x02 /* CRC error */
1263 #define ENRSR_FAE 0x04 /* frame alignment error */
1264 #define ENRSR_FO 0x08 /* FIFO overrun */
1265 #define ENRSR_MPA 0x10 /* missed pkt */
1266 #define ENRSR_PHY 0x20 /* physical/multicast address */
1267 #define ENRSR_DIS 0x40 /* receiver disable. set in monitor mode */
1268 #define ENRSR_DEF 0x80 /* deferring */
1270 /* Transmitted packet status, EN0_TSR. */
1271 #define ENTSR_PTX 0x01 /* Packet transmitted without error */
1272 #define ENTSR_ND 0x02 /* The transmit wasn't deferred. */
1273 #define ENTSR_COL 0x04 /* The transmit collided at least once. */
1274 #define ENTSR_ABT 0x08 /* The transmit collided 16 times, and was deferred. */
1275 #define ENTSR_CRS 0x10 /* The carrier sense was lost. */
1276 #define ENTSR_FU 0x20 /* A "FIFO underrun" occurred during transmit. */
1277 #define ENTSR_CDH 0x40 /* The collision detect "heartbeat" signal was lost. */
1278 #define ENTSR_OWC 0x80 /* There was an out-of-window collision. */
1280 #define NE2000_MEM_SIZE 32768
1282 typedef struct NE2000State
{
1295 uint8_t phys
[6]; /* mac address */
1297 uint8_t mult
[8]; /* multicast mask array */
1298 uint8_t mem
[NE2000_MEM_SIZE
];
1301 NE2000State ne2000_state
;
1303 char network_script
[1024];
1305 void ne2000_reset(void)
1307 NE2000State
*s
= &ne2000_state
;
1310 s
->isr
= ENISR_RESET
;
1320 /* duplicate prom data */
1321 for(i
= 15;i
>= 0; i
--) {
1322 s
->mem
[2 * i
] = s
->mem
[i
];
1323 s
->mem
[2 * i
+ 1] = s
->mem
[i
];
1327 void ne2000_update_irq(NE2000State
*s
)
1330 isr
= s
->isr
& s
->imr
;
1332 pic_set_irq(NE2000_IRQ
, 1);
1334 pic_set_irq(NE2000_IRQ
, 0);
1340 int fd
, ret
, pid
, status
;
1342 fd
= open("/dev/net/tun", O_RDWR
);
1344 fprintf(stderr
, "warning: could not open /dev/net/tun: no virtual network emulation\n");
1347 memset(&ifr
, 0, sizeof(ifr
));
1348 ifr
.ifr_flags
= IFF_TAP
| IFF_NO_PI
;
1349 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, "tun%d");
1350 ret
= ioctl(fd
, TUNSETIFF
, (void *) &ifr
);
1352 fprintf(stderr
, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
1356 printf("connected to host network interface: %s\n", ifr
.ifr_name
);
1357 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1360 /* try to launch network init script */
1364 execl(network_script
, network_script
, ifr
.ifr_name
, NULL
);
1367 while (waitpid(pid
, &status
, 0) != pid
);
1368 if (!WIFEXITED(status
) ||
1369 WEXITSTATUS(status
) != 0) {
1370 fprintf(stderr
, "%s: could not launch network script for '%s'\n",
1371 network_script
, ifr
.ifr_name
);
1377 void net_send_packet(NE2000State
*s
, const uint8_t *buf
, int size
)
1380 printf("NE2000: sending packet size=%d\n", size
);
1382 write(net_fd
, buf
, size
);
1385 /* return true if the NE2000 can receive more data */
1386 int ne2000_can_receive(NE2000State
*s
)
1388 int avail
, index
, boundary
;
1390 if (s
->cmd
& E8390_STOP
)
1392 index
= s
->curpag
<< 8;
1393 boundary
= s
->boundary
<< 8;
1394 if (index
< boundary
)
1395 avail
= boundary
- index
;
1397 avail
= (s
->stop
- s
->start
) - (index
- boundary
);
1398 if (avail
< (MAX_ETH_FRAME_SIZE
+ 4))
1403 void ne2000_receive(NE2000State
*s
, uint8_t *buf
, int size
)
1406 int total_len
, next
, avail
, len
, index
;
1408 #if defined(DEBUG_NE2000)
1409 printf("NE2000: received len=%d\n", size
);
1412 index
= s
->curpag
<< 8;
1413 /* 4 bytes for header */
1414 total_len
= size
+ 4;
1415 /* address for next packet (4 bytes for CRC) */
1416 next
= index
+ ((total_len
+ 4 + 255) & ~0xff);
1417 if (next
>= s
->stop
)
1418 next
-= (s
->stop
- s
->start
);
1419 /* prepare packet header */
1421 p
[0] = ENRSR_RXOK
; /* receive status */
1424 p
[3] = total_len
>> 8;
1427 /* write packet data */
1429 avail
= s
->stop
- index
;
1433 memcpy(s
->mem
+ index
, buf
, len
);
1436 if (index
== s
->stop
)
1440 s
->curpag
= next
>> 8;
1442 /* now we can signal we have receive something */
1444 ne2000_update_irq(s
);
1447 void ne2000_ioport_write(CPUX86State
*env
, uint32_t addr
, uint32_t val
)
1449 NE2000State
*s
= &ne2000_state
;
1454 printf("NE2000: write addr=0x%x val=0x%02x\n", addr
, val
);
1456 if (addr
== E8390_CMD
) {
1457 /* control register */
1459 if (val
& E8390_START
) {
1460 /* test specific case: zero length transfert */
1461 if ((val
& (E8390_RREAD
| E8390_RWRITE
)) &&
1463 s
->isr
|= ENISR_RDC
;
1464 ne2000_update_irq(s
);
1466 if (val
& E8390_TRANS
) {
1467 net_send_packet(s
, s
->mem
+ (s
->tpsr
<< 8), s
->tcnt
);
1468 /* signal end of transfert */
1471 ne2000_update_irq(s
);
1476 offset
= addr
| (page
<< 4);
1479 s
->start
= val
<< 8;
1489 ne2000_update_irq(s
);
1495 s
->tcnt
= (s
->tcnt
& 0xff00) | val
;
1498 s
->tcnt
= (s
->tcnt
& 0x00ff) | (val
<< 8);
1501 s
->rsar
= (s
->rsar
& 0xff00) | val
;
1504 s
->rsar
= (s
->rsar
& 0x00ff) | (val
<< 8);
1507 s
->rcnt
= (s
->rcnt
& 0xff00) | val
;
1510 s
->rcnt
= (s
->rcnt
& 0x00ff) | (val
<< 8);
1517 ne2000_update_irq(s
);
1519 case EN1_PHYS
... EN1_PHYS
+ 5:
1520 s
->phys
[offset
- EN1_PHYS
] = val
;
1525 case EN1_MULT
... EN1_MULT
+ 7:
1526 s
->mult
[offset
- EN1_MULT
] = val
;
1532 uint32_t ne2000_ioport_read(CPUX86State
*env
, uint32_t addr
)
1534 NE2000State
*s
= &ne2000_state
;
1535 int offset
, page
, ret
;
1538 if (addr
== E8390_CMD
) {
1542 offset
= addr
| (page
<< 4);
1553 case EN1_PHYS
... EN1_PHYS
+ 5:
1554 ret
= s
->phys
[offset
- EN1_PHYS
];
1559 case EN1_MULT
... EN1_MULT
+ 7:
1560 ret
= s
->mult
[offset
- EN1_MULT
];
1568 printf("NE2000: read addr=0x%x val=%02x\n", addr
, ret
);
1573 void ne2000_asic_ioport_write(CPUX86State
*env
, uint32_t addr
, uint32_t val
)
1575 NE2000State
*s
= &ne2000_state
;
1579 printf("NE2000: asic write val=0x%04x\n", val
);
1581 p
= s
->mem
+ s
->rsar
;
1582 if (s
->dcfg
& 0x01) {
1595 if (s
->rsar
== s
->stop
)
1598 /* signal end of transfert */
1599 s
->isr
|= ENISR_RDC
;
1600 ne2000_update_irq(s
);
1604 uint32_t ne2000_asic_ioport_read(CPUX86State
*env
, uint32_t addr
)
1606 NE2000State
*s
= &ne2000_state
;
1610 p
= s
->mem
+ s
->rsar
;
1611 if (s
->dcfg
& 0x01) {
1613 ret
= p
[0] | (p
[1] << 8);
1623 if (s
->rsar
== s
->stop
)
1626 /* signal end of transfert */
1627 s
->isr
|= ENISR_RDC
;
1628 ne2000_update_irq(s
);
1631 printf("NE2000: asic read val=0x%04x\n", ret
);
1636 void ne2000_reset_ioport_write(CPUX86State
*env
, uint32_t addr
, uint32_t val
)
1638 /* nothing to do (end of reset pulse) */
1641 uint32_t ne2000_reset_ioport_read(CPUX86State
*env
, uint32_t addr
)
1647 void ne2000_init(void)
1649 register_ioport_writeb(NE2000_IOPORT
, 16, ne2000_ioport_write
);
1650 register_ioport_readb(NE2000_IOPORT
, 16, ne2000_ioport_read
);
1652 register_ioport_writeb(NE2000_IOPORT
+ 0x10, 1, ne2000_asic_ioport_write
);
1653 register_ioport_readb(NE2000_IOPORT
+ 0x10, 1, ne2000_asic_ioport_read
);
1654 register_ioport_writew(NE2000_IOPORT
+ 0x10, 2, ne2000_asic_ioport_write
);
1655 register_ioport_readw(NE2000_IOPORT
+ 0x10, 2, ne2000_asic_ioport_read
);
1657 register_ioport_writeb(NE2000_IOPORT
+ 0x1f, 1, ne2000_reset_ioport_write
);
1658 register_ioport_readb(NE2000_IOPORT
+ 0x1f, 1, ne2000_reset_ioport_read
);
1662 /***********************************************************/
1663 /* cpu signal handler */
1664 static void host_segv_handler(int host_signum
, siginfo_t
*info
,
1667 if (cpu_signal_handler(host_signum
, info
, puc
))
1673 static int timer_irq_pending
;
1675 static void host_alarm_handler(int host_signum
, siginfo_t
*info
,
1678 /* just exit from the cpu to have a chance to handle timers */
1679 cpu_x86_interrupt(global_env
);
1680 timer_irq_pending
= 1;
1685 printf("Virtual Linux version " QEMU_VERSION
", Copyright (c) 2003 Fabrice Bellard\n"
1686 "usage: vl [-h] bzImage initrd [kernel parameters...]\n"
1688 "'bzImage' is a Linux kernel image (PAGE_OFFSET must be defined\n"
1689 "to 0x90000000 in asm/page.h and arch/i386/vmlinux.lds)\n"
1690 "'initrd' is an initrd image\n"
1691 "-m megs set virtual RAM size to megs MB\n"
1692 "-n script set network init script [default=%s]\n"
1693 "-d output log in /tmp/vl.log\n"
1695 "During emulation, use C-a h to get terminal commands:\n",
1696 DEFAULT_NETWORK_SCRIPT
);
1701 int main(int argc
, char **argv
)
1703 int c
, ret
, initrd_size
, i
;
1704 struct linux_params
*params
;
1705 struct sigaction act
;
1706 struct itimerval itv
;
1709 /* we never want that malloc() uses mmap() */
1710 mallopt(M_MMAP_THRESHOLD
, 4096 * 1024);
1712 phys_ram_size
= 32 * 1024 * 1024;
1713 pstrcpy(network_script
, sizeof(network_script
), DEFAULT_NETWORK_SCRIPT
);
1715 c
= getopt(argc
, argv
, "hm:dn:");
1723 phys_ram_size
= atoi(optarg
) * 1024 * 1024;
1724 if (phys_ram_size
<= 0)
1731 pstrcpy(network_script
, sizeof(network_script
), optarg
);
1735 if (optind
+ 1 >= argc
)
1740 logfile
= fopen(DEBUG_LOGFILE
, "w");
1742 perror(DEBUG_LOGFILE
);
1745 setvbuf(logfile
, NULL
, _IOLBF
, 0);
1748 /* init network tun interface */
1751 /* init the memory */
1752 strcpy(phys_ram_file
, "/tmp/vlXXXXXX");
1753 if (mkstemp(phys_ram_file
) < 0) {
1754 fprintf(stderr
, "Could not create temporary memory file\n");
1757 phys_ram_fd
= open(phys_ram_file
, O_CREAT
| O_TRUNC
| O_RDWR
, 0600);
1758 if (phys_ram_fd
< 0) {
1759 fprintf(stderr
, "Could not open temporary memory file\n");
1762 ftruncate(phys_ram_fd
, phys_ram_size
);
1763 unlink(phys_ram_file
);
1764 phys_ram_base
= mmap((void *)PHYS_RAM_BASE
, phys_ram_size
,
1765 PROT_WRITE
| PROT_READ
, MAP_SHARED
| MAP_FIXED
,
1767 if (phys_ram_base
== MAP_FAILED
) {
1768 fprintf(stderr
, "Could not map physical memory\n");
1772 /* now we can load the kernel */
1773 ret
= load_kernel(argv
[optind
], phys_ram_base
+ KERNEL_LOAD_ADDR
);
1775 fprintf(stderr
, "%s: could not load kernel\n", argv
[optind
]);
1780 initrd_size
= load_image(argv
[optind
+ 1], phys_ram_base
+ INITRD_LOAD_ADDR
);
1781 if (initrd_size
< 0) {
1782 fprintf(stderr
, "%s: could not load initrd\n", argv
[optind
+ 1]);
1786 /* init kernel params */
1787 params
= (void *)(phys_ram_base
+ KERNEL_PARAMS_ADDR
);
1788 memset(params
, 0, sizeof(struct linux_params
));
1789 params
->mount_root_rdonly
= 0;
1790 params
->cl_magic
= 0xA33F;
1791 params
->cl_offset
= params
->commandline
- (uint8_t *)params
;
1792 params
->ext_mem_k
= (phys_ram_size
/ 1024) - 1024;
1793 for(i
= optind
+ 2; i
< argc
; i
++) {
1794 if (i
!= optind
+ 2)
1795 pstrcat(params
->commandline
, sizeof(params
->commandline
), " ");
1796 pstrcat(params
->commandline
, sizeof(params
->commandline
), argv
[i
]);
1798 params
->loader_type
= 0x01;
1799 if (initrd_size
> 0) {
1800 params
->initrd_start
= INITRD_LOAD_ADDR
;
1801 params
->initrd_size
= initrd_size
;
1803 params
->orig_video_lines
= 25;
1804 params
->orig_video_cols
= 80;
1806 /* init basic PC hardware */
1808 register_ioport_writeb(0x80, 1, ioport80_write
);
1810 register_ioport_writeb(0x3d4, 2, vga_ioport_write
);
1818 /* setup cpu signal handlers for MMU / self modifying code handling */
1819 sigfillset(&act
.sa_mask
);
1820 act
.sa_flags
= SA_SIGINFO
;
1821 act
.sa_sigaction
= host_segv_handler
;
1822 sigaction(SIGSEGV
, &act
, NULL
);
1823 sigaction(SIGBUS
, &act
, NULL
);
1825 act
.sa_sigaction
= host_alarm_handler
;
1826 sigaction(SIGALRM
, &act
, NULL
);
1828 /* init CPU state */
1831 cpu_single_env
= env
;
1833 /* setup basic memory access */
1834 env
->cr
[0] = 0x00000033;
1835 cpu_x86_init_mmu(env
);
1837 memset(params
->idt_table
, 0, sizeof(params
->idt_table
));
1839 params
->gdt_table
[2] = 0x00cf9a000000ffffLL
; /* KERNEL_CS */
1840 params
->gdt_table
[3] = 0x00cf92000000ffffLL
; /* KERNEL_DS */
1842 env
->idt
.base
= (void *)params
->idt_table
;
1843 env
->idt
.limit
= sizeof(params
->idt_table
) - 1;
1844 env
->gdt
.base
= (void *)params
->gdt_table
;
1845 env
->gdt
.limit
= sizeof(params
->gdt_table
) - 1;
1847 cpu_x86_load_seg(env
, R_CS
, KERNEL_CS
);
1848 cpu_x86_load_seg(env
, R_DS
, KERNEL_DS
);
1849 cpu_x86_load_seg(env
, R_ES
, KERNEL_DS
);
1850 cpu_x86_load_seg(env
, R_SS
, KERNEL_DS
);
1851 cpu_x86_load_seg(env
, R_FS
, KERNEL_DS
);
1852 cpu_x86_load_seg(env
, R_GS
, KERNEL_DS
);
1854 env
->eip
= KERNEL_LOAD_ADDR
;
1855 env
->regs
[R_ESI
] = KERNEL_PARAMS_ADDR
;
1858 itv
.it_interval
.tv_sec
= 0;
1859 itv
.it_interval
.tv_usec
= 10 * 1000;
1860 itv
.it_value
.tv_sec
= 0;
1861 itv
.it_value
.tv_usec
= 10 * 1000;
1862 setitimer(ITIMER_REAL
, &itv
, NULL
);
1865 struct pollfd ufds
[2], *pf
, *serial_ufd
, *net_ufd
;
1866 int ret
, n
, timeout
;
1869 ret
= cpu_x86_exec(env
);
1871 /* if hlt instruction, we wait until the next IRQ */
1872 if (ret
== EXCP_HLT
)
1876 /* poll any events */
1880 if (!(serial_ports
[0].lsr
& UART_LSR_DR
)) {
1883 pf
->events
= POLLIN
;
1886 if (net_fd
> 0 && ne2000_can_receive(&ne2000_state
)) {
1889 pf
->events
= POLLIN
;
1892 ret
= poll(ufds
, pf
- ufds
, timeout
);
1894 if (serial_ufd
&& (serial_ufd
->revents
& POLLIN
)) {
1895 n
= read(0, &ch
, 1);
1897 serial_received_byte(&serial_ports
[0], ch
);
1900 if (net_ufd
&& (net_ufd
->revents
& POLLIN
)) {
1901 uint8_t buf
[MAX_ETH_FRAME_SIZE
];
1903 n
= read(net_fd
, buf
, MAX_ETH_FRAME_SIZE
);
1906 memset(buf
+ n
, 0, 60 - n
);
1909 ne2000_receive(&ne2000_state
, buf
, n
);
1915 if (timer_irq_pending
) {
1918 timer_irq_pending
= 0;