]> git.proxmox.com Git - mirror_qemu.git/blame - vl.c
added block.c
[mirror_qemu.git] / vl.c
CommitLineData
0824d6fc 1/*
1df912cf 2 * QEMU PC System Emulator
0824d6fc 3 *
1df912cf 4 * Copyright (c) 2003 Fabrice Bellard
0824d6fc 5 *
1df912cf
FB
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:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
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
22 * THE SOFTWARE.
0824d6fc
FB
23 */
24#include <stdlib.h>
25#include <stdio.h>
1df912cf 26#include <stdarg.h>
0824d6fc
FB
27#include <string.h>
28#include <getopt.h>
29#include <inttypes.h>
30#include <unistd.h>
31#include <sys/mman.h>
32#include <fcntl.h>
33#include <signal.h>
34#include <time.h>
35#include <sys/time.h>
36#include <malloc.h>
37#include <termios.h>
38#include <sys/poll.h>
39#include <errno.h>
f1510b2c
FB
40#include <sys/wait.h>
41
42#include <sys/ioctl.h>
43#include <sys/socket.h>
44#include <linux/if.h>
45#include <linux/if_tun.h>
0824d6fc
FB
46
47#include "cpu-i386.h"
48#include "disas.h"
fc01f7e7
FB
49#include "thunk.h"
50
51#include "vl.h"
0824d6fc
FB
52
53#define DEBUG_LOGFILE "/tmp/vl.log"
f1510b2c
FB
54#define DEFAULT_NETWORK_SCRIPT "/etc/vl-ifup"
55
0824d6fc
FB
56//#define DEBUG_UNUSED_IOPORT
57
58#define PHYS_RAM_BASE 0xa8000000
59#define KERNEL_LOAD_ADDR 0x00100000
60#define INITRD_LOAD_ADDR 0x00400000
61#define KERNEL_PARAMS_ADDR 0x00090000
62
63/* from plex86 (BSD license) */
64struct __attribute__ ((packed)) linux_params {
65 // For 0x00..0x3f, see 'struct screen_info' in linux/include/linux/tty.h.
66 // I just padded out the VESA parts, rather than define them.
67
68 /* 0x000 */ uint8_t orig_x;
69 /* 0x001 */ uint8_t orig_y;
70 /* 0x002 */ uint16_t ext_mem_k;
71 /* 0x004 */ uint16_t orig_video_page;
72 /* 0x006 */ uint8_t orig_video_mode;
73 /* 0x007 */ uint8_t orig_video_cols;
74 /* 0x008 */ uint16_t unused1;
75 /* 0x00a */ uint16_t orig_video_ega_bx;
76 /* 0x00c */ uint16_t unused2;
77 /* 0x00e */ uint8_t orig_video_lines;
78 /* 0x00f */ uint8_t orig_video_isVGA;
79 /* 0x010 */ uint16_t orig_video_points;
80 /* 0x012 */ uint8_t pad0[0x20 - 0x12]; // VESA info.
81 /* 0x020 */ uint16_t cl_magic; // Commandline magic number (0xA33F)
82 /* 0x022 */ uint16_t cl_offset; // Commandline offset. Address of commandline
83 // is calculated as 0x90000 + cl_offset, bu
84 // only if cl_magic == 0xA33F.
85 /* 0x024 */ uint8_t pad1[0x40 - 0x24]; // VESA info.
86
87 /* 0x040 */ uint8_t apm_bios_info[20]; // struct apm_bios_info
88 /* 0x054 */ uint8_t pad2[0x80 - 0x54];
89
90 // Following 2 from 'struct drive_info_struct' in drivers/block/cciss.h.
91 // Might be truncated?
92 /* 0x080 */ uint8_t hd0_info[16]; // hd0-disk-parameter from intvector 0x41
93 /* 0x090 */ uint8_t hd1_info[16]; // hd1-disk-parameter from intvector 0x46
94
95 // System description table truncated to 16 bytes
96 // From 'struct sys_desc_table_struct' in linux/arch/i386/kernel/setup.c.
97 /* 0x0a0 */ uint16_t sys_description_len;
98 /* 0x0a2 */ uint8_t sys_description_table[14];
99 // [0] machine id
100 // [1] machine submodel id
101 // [2] BIOS revision
102 // [3] bit1: MCA bus
103
104 /* 0x0b0 */ uint8_t pad3[0x1e0 - 0xb0];
105 /* 0x1e0 */ uint32_t alt_mem_k;
106 /* 0x1e4 */ uint8_t pad4[4];
107 /* 0x1e8 */ uint8_t e820map_entries;
108 /* 0x1e9 */ uint8_t eddbuf_entries; // EDD_NR
109 /* 0x1ea */ uint8_t pad5[0x1f1 - 0x1ea];
110 /* 0x1f1 */ uint8_t setup_sects; // size of setup.S, number of sectors
111 /* 0x1f2 */ uint16_t mount_root_rdonly; // MOUNT_ROOT_RDONLY (if !=0)
112 /* 0x1f4 */ uint16_t sys_size; // size of compressed kernel-part in the
113 // (b)zImage-file (in 16 byte units, rounded up)
114 /* 0x1f6 */ uint16_t swap_dev; // (unused AFAIK)
115 /* 0x1f8 */ uint16_t ramdisk_flags;
116 /* 0x1fa */ uint16_t vga_mode; // (old one)
117 /* 0x1fc */ uint16_t orig_root_dev; // (high=Major, low=minor)
118 /* 0x1fe */ uint8_t pad6[1];
119 /* 0x1ff */ uint8_t aux_device_info;
120 /* 0x200 */ uint16_t jump_setup; // Jump to start of setup code,
121 // aka "reserved" field.
122 /* 0x202 */ uint8_t setup_signature[4]; // Signature for SETUP-header, ="HdrS"
123 /* 0x206 */ uint16_t header_format_version; // Version number of header format;
124 /* 0x208 */ uint8_t setup_S_temp0[8]; // Used by setup.S for communication with
125 // boot loaders, look there.
126 /* 0x210 */ uint8_t loader_type;
127 // 0 for old one.
128 // else 0xTV:
129 // T=0: LILO
130 // T=1: Loadlin
131 // T=2: bootsect-loader
132 // T=3: SYSLINUX
133 // T=4: ETHERBOOT
134 // V=version
135 /* 0x211 */ uint8_t loadflags;
136 // bit0 = 1: kernel is loaded high (bzImage)
137 // bit7 = 1: Heap and pointer (see below) set by boot
138 // loader.
139 /* 0x212 */ uint16_t setup_S_temp1;
140 /* 0x214 */ uint32_t kernel_start;
141 /* 0x218 */ uint32_t initrd_start;
142 /* 0x21c */ uint32_t initrd_size;
143 /* 0x220 */ uint8_t setup_S_temp2[4];
144 /* 0x224 */ uint16_t setup_S_heap_end_pointer;
145 /* 0x226 */ uint8_t pad7[0x2d0 - 0x226];
146
147 /* 0x2d0 : Int 15, ax=e820 memory map. */
148 // (linux/include/asm-i386/e820.h, 'struct e820entry')
149#define E820MAX 32
150#define E820_RAM 1
151#define E820_RESERVED 2
152#define E820_ACPI 3 /* usable as RAM once ACPI tables have been read */
153#define E820_NVS 4
154 struct {
155 uint64_t addr;
156 uint64_t size;
157 uint32_t type;
158 } e820map[E820MAX];
159
160 /* 0x550 */ uint8_t pad8[0x600 - 0x550];
161
162 // BIOS Enhanced Disk Drive Services.
163 // (From linux/include/asm-i386/edd.h, 'struct edd_info')
164 // Each 'struct edd_info is 78 bytes, times a max of 6 structs in array.
165 /* 0x600 */ uint8_t eddbuf[0x7d4 - 0x600];
166
167 /* 0x7d4 */ uint8_t pad9[0x800 - 0x7d4];
168 /* 0x800 */ uint8_t commandline[0x800];
169
170 /* 0x1000 */
171 uint64_t gdt_table[256];
172 uint64_t idt_table[48];
173};
174
175#define KERNEL_CS 0x10
176#define KERNEL_DS 0x18
177
178typedef void (IOPortWriteFunc)(CPUX86State *env, uint32_t address, uint32_t data);
179typedef uint32_t (IOPortReadFunc)(CPUX86State *env, uint32_t address);
180
fc01f7e7 181#define MAX_IOPORTS 4096
0824d6fc
FB
182
183char phys_ram_file[1024];
184CPUX86State *global_env;
1df912cf 185CPUX86State *cpu_single_env;
0824d6fc
FB
186FILE *logfile = NULL;
187int loglevel;
fc01f7e7
FB
188IOPortReadFunc *ioport_read_table[3][MAX_IOPORTS];
189IOPortWriteFunc *ioport_write_table[3][MAX_IOPORTS];
0824d6fc
FB
190
191/***********************************************************/
192/* x86 io ports */
193
194uint32_t default_ioport_readb(CPUX86State *env, uint32_t address)
195{
196#ifdef DEBUG_UNUSED_IOPORT
197 fprintf(stderr, "inb: port=0x%04x\n", address);
198#endif
fc01f7e7 199 return 0xff;
0824d6fc
FB
200}
201
202void default_ioport_writeb(CPUX86State *env, uint32_t address, uint32_t data)
203{
204#ifdef DEBUG_UNUSED_IOPORT
205 fprintf(stderr, "outb: port=0x%04x data=0x%02x\n", address, data);
206#endif
207}
208
209/* default is to make two byte accesses */
210uint32_t default_ioport_readw(CPUX86State *env, uint32_t address)
211{
212 uint32_t data;
fc01f7e7
FB
213 data = ioport_read_table[0][address](env, address);
214 data |= ioport_read_table[0][address + 1](env, address + 1) << 8;
0824d6fc
FB
215 return data;
216}
217
218void default_ioport_writew(CPUX86State *env, uint32_t address, uint32_t data)
219{
fc01f7e7
FB
220 ioport_write_table[0][address](env, address, data & 0xff);
221 ioport_write_table[0][address + 1](env, address + 1, (data >> 8) & 0xff);
0824d6fc
FB
222}
223
fc01f7e7 224uint32_t default_ioport_readl(CPUX86State *env, uint32_t address)
0824d6fc 225{
fc01f7e7
FB
226#ifdef DEBUG_UNUSED_IOPORT
227 fprintf(stderr, "inl: port=0x%04x\n", address);
228#endif
229 return 0xffffffff;
0824d6fc
FB
230}
231
fc01f7e7 232void default_ioport_writel(CPUX86State *env, uint32_t address, uint32_t data)
0824d6fc 233{
fc01f7e7
FB
234#ifdef DEBUG_UNUSED_IOPORT
235 fprintf(stderr, "outl: port=0x%04x data=0x%02x\n", address, data);
236#endif
0824d6fc
FB
237}
238
fc01f7e7 239void init_ioports(void)
0824d6fc
FB
240{
241 int i;
242
fc01f7e7
FB
243 for(i = 0; i < MAX_IOPORTS; i++) {
244 ioport_read_table[0][i] = default_ioport_readb;
245 ioport_write_table[0][i] = default_ioport_writeb;
246 ioport_read_table[1][i] = default_ioport_readw;
247 ioport_write_table[1][i] = default_ioport_writew;
248 ioport_read_table[2][i] = default_ioport_readl;
249 ioport_write_table[2][i] = default_ioport_writel;
250 }
0824d6fc
FB
251}
252
fc01f7e7
FB
253/* size is the word size in byte */
254int register_ioport_read(int start, int length, IOPortReadFunc *func, int size)
f1510b2c 255{
fc01f7e7 256 int i, bsize;
f1510b2c 257
fc01f7e7
FB
258 if (size == 1)
259 bsize = 0;
260 else if (size == 2)
261 bsize = 1;
262 else if (size == 4)
263 bsize = 2;
264 else
265 return -1;
266 for(i = start; i < start + length; i += size)
267 ioport_read_table[bsize][i] = func;
f1510b2c
FB
268 return 0;
269}
270
fc01f7e7
FB
271/* size is the word size in byte */
272int register_ioport_write(int start, int length, IOPortWriteFunc *func, int size)
f1510b2c 273{
fc01f7e7 274 int i, bsize;
f1510b2c 275
fc01f7e7
FB
276 if (size == 1)
277 bsize = 0;
278 else if (size == 2)
279 bsize = 1;
280 else if (size == 4)
281 bsize = 2;
282 else
283 return -1;
284 for(i = start; i < start + length; i += size)
285 ioport_write_table[bsize][i] = func;
f1510b2c
FB
286 return 0;
287}
288
0824d6fc
FB
289void pstrcpy(char *buf, int buf_size, const char *str)
290{
291 int c;
292 char *q = buf;
293
294 if (buf_size <= 0)
295 return;
296
297 for(;;) {
298 c = *str++;
299 if (c == 0 || q >= buf + buf_size - 1)
300 break;
301 *q++ = c;
302 }
303 *q = '\0';
304}
305
306/* strcat and truncate. */
307char *pstrcat(char *buf, int buf_size, const char *s)
308{
309 int len;
310 len = strlen(buf);
311 if (len < buf_size)
312 pstrcpy(buf + len, buf_size - len, s);
313 return buf;
314}
315
316int load_kernel(const char *filename, uint8_t *addr)
317{
318 int fd, size, setup_sects;
319 uint8_t bootsect[512];
320
321 fd = open(filename, O_RDONLY);
322 if (fd < 0)
323 return -1;
324 if (read(fd, bootsect, 512) != 512)
325 goto fail;
326 setup_sects = bootsect[0x1F1];
327 if (!setup_sects)
328 setup_sects = 4;
329 /* skip 16 bit setup code */
330 lseek(fd, (setup_sects + 1) * 512, SEEK_SET);
331 size = read(fd, addr, 16 * 1024 * 1024);
332 if (size < 0)
333 goto fail;
334 close(fd);
335 return size;
336 fail:
337 close(fd);
338 return -1;
339}
340
341/* return the size or -1 if error */
342int load_image(const char *filename, uint8_t *addr)
343{
344 int fd, size;
345 fd = open(filename, O_RDONLY);
346 if (fd < 0)
347 return -1;
348 size = lseek(fd, 0, SEEK_END);
349 lseek(fd, 0, SEEK_SET);
350 if (read(fd, addr, size) != size) {
351 close(fd);
352 return -1;
353 }
354 close(fd);
355 return size;
356}
357
358void cpu_x86_outb(CPUX86State *env, int addr, int val)
359{
fc01f7e7 360 ioport_write_table[0][addr & (MAX_IOPORTS - 1)](env, addr, val);
0824d6fc
FB
361}
362
363void cpu_x86_outw(CPUX86State *env, int addr, int val)
364{
fc01f7e7 365 ioport_write_table[1][addr & (MAX_IOPORTS - 1)](env, addr, val);
0824d6fc
FB
366}
367
368void cpu_x86_outl(CPUX86State *env, int addr, int val)
369{
fc01f7e7 370 ioport_write_table[2][addr & (MAX_IOPORTS - 1)](env, addr, val);
0824d6fc
FB
371}
372
373int cpu_x86_inb(CPUX86State *env, int addr)
374{
fc01f7e7 375 return ioport_read_table[0][addr & (MAX_IOPORTS - 1)](env, addr);
0824d6fc
FB
376}
377
378int cpu_x86_inw(CPUX86State *env, int addr)
379{
fc01f7e7 380 return ioport_read_table[1][addr & (MAX_IOPORTS - 1)](env, addr);
0824d6fc
FB
381}
382
383int cpu_x86_inl(CPUX86State *env, int addr)
384{
fc01f7e7 385 return ioport_read_table[2][addr & (MAX_IOPORTS - 1)](env, addr);
0824d6fc
FB
386}
387
388/***********************************************************/
389void ioport80_write(CPUX86State *env, uint32_t addr, uint32_t data)
390{
391}
392
393void hw_error(const char *fmt, ...)
394{
395 va_list ap;
396
397 va_start(ap, fmt);
398 fprintf(stderr, "qemu: hardware error: ");
399 vfprintf(stderr, fmt, ap);
400 fprintf(stderr, "\n");
401#ifdef TARGET_I386
402 cpu_x86_dump_state(global_env, stderr, X86_DUMP_FPU | X86_DUMP_CCOP);
403#endif
404 va_end(ap);
405 abort();
406}
407
408/***********************************************************/
409/* vga emulation */
410static uint8_t vga_index;
411static uint8_t vga_regs[256];
412static int last_cursor_pos;
413
414void update_console_messages(void)
415{
416 int c, i, cursor_pos, eol;
417
418 cursor_pos = vga_regs[0x0f] | (vga_regs[0x0e] << 8);
419 eol = 0;
420 for(i = last_cursor_pos; i < cursor_pos; i++) {
421 c = phys_ram_base[0xb8000 + (i) * 2];
422 if (c >= ' ') {
423 putchar(c);
424 eol = 0;
425 } else {
426 if (!eol)
427 putchar('\n');
428 eol = 1;
429 }
430 }
431 fflush(stdout);
432 last_cursor_pos = cursor_pos;
433}
434
435/* just to see first Linux console messages, we intercept cursor position */
436void vga_ioport_write(CPUX86State *env, uint32_t addr, uint32_t data)
437{
438 switch(addr) {
439 case 0x3d4:
440 vga_index = data;
441 break;
442 case 0x3d5:
443 vga_regs[vga_index] = data;
444 if (vga_index == 0x0f)
445 update_console_messages();
446 break;
447 }
448
449}
450
451/***********************************************************/
452/* cmos emulation */
453
454#define RTC_SECONDS 0
455#define RTC_SECONDS_ALARM 1
456#define RTC_MINUTES 2
457#define RTC_MINUTES_ALARM 3
458#define RTC_HOURS 4
459#define RTC_HOURS_ALARM 5
460#define RTC_ALARM_DONT_CARE 0xC0
461
462#define RTC_DAY_OF_WEEK 6
463#define RTC_DAY_OF_MONTH 7
464#define RTC_MONTH 8
465#define RTC_YEAR 9
466
467#define RTC_REG_A 10
468#define RTC_REG_B 11
469#define RTC_REG_C 12
470#define RTC_REG_D 13
471
472/* PC cmos mappings */
473#define REG_EQUIPMENT_BYTE 0x14
474
475uint8_t cmos_data[128];
476uint8_t cmos_index;
477
478void cmos_ioport_write(CPUX86State *env, uint32_t addr, uint32_t data)
479{
480 if (addr == 0x70) {
481 cmos_index = data & 0x7f;
482 }
483}
484
485uint32_t cmos_ioport_read(CPUX86State *env, uint32_t addr)
486{
487 int ret;
488
489 if (addr == 0x70) {
490 return 0xff;
491 } else {
492 /* toggle update-in-progress bit for Linux (same hack as
493 plex86) */
494 ret = cmos_data[cmos_index];
495 if (cmos_index == RTC_REG_A)
496 cmos_data[RTC_REG_A] ^= 0x80;
497 else if (cmos_index == RTC_REG_C)
498 cmos_data[RTC_REG_C] = 0x00;
499 return ret;
500 }
501}
502
503
504static inline int to_bcd(int a)
505{
506 return ((a / 10) << 4) | (a % 10);
507}
508
509void cmos_init(void)
510{
511 struct tm *tm;
512 time_t ti;
513
514 ti = time(NULL);
515 tm = gmtime(&ti);
516 cmos_data[RTC_SECONDS] = to_bcd(tm->tm_sec);
517 cmos_data[RTC_MINUTES] = to_bcd(tm->tm_min);
518 cmos_data[RTC_HOURS] = to_bcd(tm->tm_hour);
519 cmos_data[RTC_DAY_OF_WEEK] = to_bcd(tm->tm_wday);
520 cmos_data[RTC_DAY_OF_MONTH] = to_bcd(tm->tm_mday);
521 cmos_data[RTC_MONTH] = to_bcd(tm->tm_mon);
522 cmos_data[RTC_YEAR] = to_bcd(tm->tm_year % 100);
523
524 cmos_data[RTC_REG_A] = 0x26;
525 cmos_data[RTC_REG_B] = 0x02;
526 cmos_data[RTC_REG_C] = 0x00;
527 cmos_data[RTC_REG_D] = 0x80;
528
529 cmos_data[REG_EQUIPMENT_BYTE] = 0x02; /* FPU is there */
530
fc01f7e7
FB
531 register_ioport_write(0x70, 2, cmos_ioport_write, 1);
532 register_ioport_read(0x70, 2, cmos_ioport_read, 1);
0824d6fc
FB
533}
534
535/***********************************************************/
536/* 8259 pic emulation */
537
538typedef struct PicState {
539 uint8_t last_irr; /* edge detection */
540 uint8_t irr; /* interrupt request register */
541 uint8_t imr; /* interrupt mask register */
542 uint8_t isr; /* interrupt service register */
543 uint8_t priority_add; /* used to compute irq priority */
544 uint8_t irq_base;
545 uint8_t read_reg_select;
546 uint8_t special_mask;
547 uint8_t init_state;
548 uint8_t auto_eoi;
549 uint8_t rotate_on_autoeoi;
550 uint8_t init4; /* true if 4 byte init */
551} PicState;
552
553/* 0 is master pic, 1 is slave pic */
554PicState pics[2];
555int pic_irq_requested;
556
557/* set irq level. If an edge is detected, then the IRR is set to 1 */
558static inline void pic_set_irq1(PicState *s, int irq, int level)
559{
560 int mask;
561 mask = 1 << irq;
562 if (level) {
563 if ((s->last_irr & mask) == 0)
564 s->irr |= mask;
565 s->last_irr |= mask;
566 } else {
567 s->last_irr &= ~mask;
568 }
569}
570
571static inline int get_priority(PicState *s, int mask)
572{
573 int priority;
574 if (mask == 0)
575 return -1;
576 priority = 7;
577 while ((mask & (1 << ((priority + s->priority_add) & 7))) == 0)
578 priority--;
579 return priority;
580}
581
582/* return the pic wanted interrupt. return -1 if none */
583static int pic_get_irq(PicState *s)
584{
585 int mask, cur_priority, priority;
586
587 mask = s->irr & ~s->imr;
588 priority = get_priority(s, mask);
589 if (priority < 0)
590 return -1;
591 /* compute current priority */
592 cur_priority = get_priority(s, s->isr);
593 if (priority > cur_priority) {
594 /* higher priority found: an irq should be generated */
595 return priority;
596 } else {
597 return -1;
598 }
599}
600
601void pic_set_irq(int irq, int level)
602{
603 pic_set_irq1(&pics[irq >> 3], irq & 7, level);
604}
605
606/* can be called at any time outside cpu_exec() to raise irqs if
607 necessary */
608void pic_handle_irq(void)
609{
610 int irq2, irq;
611
612 /* first look at slave pic */
613 irq2 = pic_get_irq(&pics[1]);
614 if (irq2 >= 0) {
615 /* if irq request by slave pic, signal master PIC */
616 pic_set_irq1(&pics[0], 2, 1);
617 pic_set_irq1(&pics[0], 2, 0);
618 }
619 /* look at requested irq */
620 irq = pic_get_irq(&pics[0]);
621 if (irq >= 0) {
622 if (irq == 2) {
623 /* from slave pic */
624 pic_irq_requested = 8 + irq2;
625 } else {
626 /* from master pic */
627 pic_irq_requested = irq;
628 }
629 global_env->hard_interrupt_request = 1;
630 }
631}
632
633int cpu_x86_get_pic_interrupt(CPUX86State *env)
634{
635 int irq, irq2, intno;
636
637 /* signal the pic that the irq was acked by the CPU */
638 irq = pic_irq_requested;
639 if (irq >= 8) {
640 irq2 = irq & 7;
641 pics[1].isr |= (1 << irq2);
642 pics[1].irr &= ~(1 << irq2);
643 irq = 2;
644 intno = pics[1].irq_base + irq2;
645 } else {
646 intno = pics[0].irq_base + irq;
647 }
648 pics[0].isr |= (1 << irq);
649 pics[0].irr &= ~(1 << irq);
650 return intno;
651}
652
653void pic_ioport_write(CPUX86State *env, uint32_t addr, uint32_t val)
654{
655 PicState *s;
656 int priority;
657
658 s = &pics[addr >> 7];
659 addr &= 1;
660 if (addr == 0) {
661 if (val & 0x10) {
662 /* init */
663 memset(s, 0, sizeof(PicState));
664 s->init_state = 1;
665 s->init4 = val & 1;
666 if (val & 0x02)
667 hw_error("single mode not supported");
668 if (val & 0x08)
669 hw_error("level sensitive irq not supported");
670 } else if (val & 0x08) {
671 if (val & 0x02)
672 s->read_reg_select = val & 1;
673 if (val & 0x40)
674 s->special_mask = (val >> 5) & 1;
675 } else {
676 switch(val) {
677 case 0x00:
678 case 0x80:
679 s->rotate_on_autoeoi = val >> 7;
680 break;
681 case 0x20: /* end of interrupt */
682 case 0xa0:
683 priority = get_priority(s, s->isr);
684 if (priority >= 0) {
685 s->isr &= ~(1 << ((priority + s->priority_add) & 7));
686 }
687 if (val == 0xa0)
688 s->priority_add = (s->priority_add + 1) & 7;
689 break;
690 case 0x60 ... 0x67:
691 priority = val & 7;
692 s->isr &= ~(1 << priority);
693 break;
694 case 0xc0 ... 0xc7:
695 s->priority_add = (val + 1) & 7;
696 break;
697 case 0xe0 ... 0xe7:
698 priority = val & 7;
699 s->isr &= ~(1 << priority);
700 s->priority_add = (priority + 1) & 7;
701 break;
702 }
703 }
704 } else {
705 switch(s->init_state) {
706 case 0:
707 /* normal mode */
708 s->imr = val;
709 break;
710 case 1:
711 s->irq_base = val & 0xf8;
712 s->init_state = 2;
713 break;
714 case 2:
715 if (s->init4) {
716 s->init_state = 3;
717 } else {
718 s->init_state = 0;
719 }
720 break;
721 case 3:
722 s->auto_eoi = (val >> 1) & 1;
723 s->init_state = 0;
724 break;
725 }
726 }
727}
728
729uint32_t pic_ioport_read(CPUX86State *env, uint32_t addr)
730{
731 PicState *s;
732 s = &pics[addr >> 7];
733 addr &= 1;
734 if (addr == 0) {
735 if (s->read_reg_select)
736 return s->isr;
737 else
738 return s->irr;
739 } else {
740 return s->imr;
741 }
742}
743
744void pic_init(void)
745{
fc01f7e7
FB
746 register_ioport_write(0x20, 2, pic_ioport_write, 1);
747 register_ioport_read(0x20, 2, pic_ioport_read, 1);
748 register_ioport_write(0xa0, 2, pic_ioport_write, 1);
749 register_ioport_read(0xa0, 2, pic_ioport_read, 1);
0824d6fc
FB
750}
751
752/***********************************************************/
753/* 8253 PIT emulation */
754
755#define PIT_FREQ 1193182
756
757#define RW_STATE_LSB 0
758#define RW_STATE_MSB 1
759#define RW_STATE_WORD0 2
760#define RW_STATE_WORD1 3
761#define RW_STATE_LATCHED_WORD0 4
762#define RW_STATE_LATCHED_WORD1 5
763
764typedef struct PITChannelState {
87858c89 765 int count; /* can be 65536 */
0824d6fc
FB
766 uint16_t latched_count;
767 uint8_t rw_state;
768 uint8_t mode;
769 uint8_t bcd; /* not supported */
770 uint8_t gate; /* timer start */
771 int64_t count_load_time;
87858c89 772 int64_t count_last_edge_check_time;
0824d6fc
FB
773} PITChannelState;
774
775PITChannelState pit_channels[3];
776int speaker_data_on;
87858c89 777int pit_min_timer_count = 0;
0824d6fc
FB
778
779int64_t ticks_per_sec;
780
781int64_t get_clock(void)
782{
783 struct timeval tv;
784 gettimeofday(&tv, NULL);
785 return tv.tv_sec * 1000000LL + tv.tv_usec;
786}
787
788int64_t cpu_get_ticks(void)
789{
790 int64_t val;
791 asm("rdtsc" : "=A" (val));
792 return val;
793}
794
795void cpu_calibrate_ticks(void)
796{
797 int64_t usec, ticks;
798
799 usec = get_clock();
800 ticks = cpu_get_ticks();
801 usleep(50 * 1000);
802 usec = get_clock() - usec;
803 ticks = cpu_get_ticks() - ticks;
804 ticks_per_sec = (ticks * 1000000LL + (usec >> 1)) / usec;
805}
806
87858c89
FB
807/* compute with 96 bit intermediate result: (a*b)/c */
808static uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
809{
810 union {
811 uint64_t ll;
812 struct {
813#ifdef WORDS_BIGENDIAN
814 uint32_t high, low;
815#else
816 uint32_t low, high;
817#endif
818 } l;
819 } u, res;
820 uint64_t rl, rh;
821
822 u.ll = a;
823 rl = (uint64_t)u.l.low * (uint64_t)b;
824 rh = (uint64_t)u.l.high * (uint64_t)b;
825 rh += (rl >> 32);
826 res.l.high = rh / c;
827 res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c;
828 return res.ll;
829}
830
0824d6fc
FB
831static int pit_get_count(PITChannelState *s)
832{
87858c89 833 uint64_t d;
0824d6fc
FB
834 int counter;
835
87858c89 836 d = muldiv64(cpu_get_ticks() - s->count_load_time, PIT_FREQ, ticks_per_sec);
0824d6fc
FB
837 switch(s->mode) {
838 case 0:
839 case 1:
840 case 4:
841 case 5:
842 counter = (s->count - d) & 0xffff;
843 break;
844 default:
845 counter = s->count - (d % s->count);
846 break;
847 }
848 return counter;
849}
850
851/* get pit output bit */
852static int pit_get_out(PITChannelState *s)
853{
87858c89 854 uint64_t d;
0824d6fc
FB
855 int out;
856
87858c89 857 d = muldiv64(cpu_get_ticks() - s->count_load_time, PIT_FREQ, ticks_per_sec);
0824d6fc
FB
858 switch(s->mode) {
859 default:
860 case 0:
861 out = (d >= s->count);
862 break;
863 case 1:
864 out = (d < s->count);
865 break;
866 case 2:
867 if ((d % s->count) == 0 && d != 0)
868 out = 1;
869 else
870 out = 0;
871 break;
872 case 3:
873 out = (d % s->count) < (s->count >> 1);
874 break;
875 case 4:
876 case 5:
877 out = (d == s->count);
878 break;
879 }
880 return out;
881}
882
87858c89
FB
883/* get the number of 0 to 1 transitions we had since we call this
884 function */
885/* XXX: maybe better to use ticks precision to avoid getting edges
886 twice if checks are done at very small intervals */
887static int pit_get_out_edges(PITChannelState *s)
888{
889 uint64_t d1, d2;
890 int64_t ticks;
891 int ret, v;
892
893 ticks = cpu_get_ticks();
894 d1 = muldiv64(s->count_last_edge_check_time - s->count_load_time,
895 PIT_FREQ, ticks_per_sec);
896 d2 = muldiv64(ticks - s->count_load_time,
897 PIT_FREQ, ticks_per_sec);
898 s->count_last_edge_check_time = ticks;
899 switch(s->mode) {
900 default:
901 case 0:
902 if (d1 < s->count && d2 >= s->count)
903 ret = 1;
904 else
905 ret = 0;
906 break;
907 case 1:
908 ret = 0;
909 break;
910 case 2:
911 d1 /= s->count;
912 d2 /= s->count;
913 ret = d2 - d1;
914 break;
915 case 3:
916 v = s->count - (s->count >> 1);
917 d1 = (d1 + v) / s->count;
918 d2 = (d2 + v) / s->count;
919 ret = d2 - d1;
920 break;
921 case 4:
922 case 5:
923 if (d1 < s->count && d2 >= s->count)
924 ret = 1;
925 else
926 ret = 0;
927 break;
928 }
929 return ret;
930}
931
932static inline void pit_load_count(PITChannelState *s, int val)
933{
934 if (val == 0)
935 val = 0x10000;
936 s->count_load_time = cpu_get_ticks();
937 s->count_last_edge_check_time = s->count_load_time;
938 s->count = val;
939 if (s == &pit_channels[0] && val <= pit_min_timer_count) {
940 fprintf(stderr,
941 "\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",
942 PIT_FREQ / pit_min_timer_count);
943 }
944}
945
0824d6fc
FB
946void pit_ioport_write(CPUX86State *env, uint32_t addr, uint32_t val)
947{
948 int channel, access;
949 PITChannelState *s;
87858c89 950
0824d6fc
FB
951 addr &= 3;
952 if (addr == 3) {
953 channel = val >> 6;
954 if (channel == 3)
955 return;
956 s = &pit_channels[channel];
957 access = (val >> 4) & 3;
958 switch(access) {
959 case 0:
960 s->latched_count = pit_get_count(s);
961 s->rw_state = RW_STATE_LATCHED_WORD0;
962 break;
963 default:
87858c89
FB
964 s->mode = (val >> 1) & 7;
965 s->bcd = val & 1;
0824d6fc
FB
966 s->rw_state = access - 1 + RW_STATE_LSB;
967 break;
968 }
0824d6fc
FB
969 } else {
970 s = &pit_channels[addr];
971 switch(s->rw_state) {
972 case RW_STATE_LSB:
87858c89 973 pit_load_count(s, val);
0824d6fc
FB
974 break;
975 case RW_STATE_MSB:
87858c89 976 pit_load_count(s, val << 8);
0824d6fc
FB
977 break;
978 case RW_STATE_WORD0:
979 case RW_STATE_WORD1:
980 if (s->rw_state & 1) {
87858c89 981 pit_load_count(s, (s->latched_count & 0xff) | (val << 8));
0824d6fc
FB
982 } else {
983 s->latched_count = val;
984 }
985 s->rw_state ^= 1;
986 break;
987 }
988 }
989}
990
991uint32_t pit_ioport_read(CPUX86State *env, uint32_t addr)
992{
993 int ret, count;
994 PITChannelState *s;
995
996 addr &= 3;
997 s = &pit_channels[addr];
998 switch(s->rw_state) {
999 case RW_STATE_LSB:
1000 case RW_STATE_MSB:
1001 case RW_STATE_WORD0:
1002 case RW_STATE_WORD1:
1003 count = pit_get_count(s);
1004 if (s->rw_state & 1)
1005 ret = (count >> 8) & 0xff;
1006 else
1007 ret = count & 0xff;
1008 if (s->rw_state & 2)
1009 s->rw_state ^= 1;
1010 break;
1011 default:
1012 case RW_STATE_LATCHED_WORD0:
1013 case RW_STATE_LATCHED_WORD1:
1014 if (s->rw_state & 1)
1015 ret = s->latched_count >> 8;
1016 else
1017 ret = s->latched_count & 0xff;
1018 s->rw_state ^= 1;
1019 break;
1020 }
1021 return ret;
1022}
1023
1024void speaker_ioport_write(CPUX86State *env, uint32_t addr, uint32_t val)
1025{
1026 speaker_data_on = (val >> 1) & 1;
1027 pit_channels[2].gate = val & 1;
1028}
1029
1030uint32_t speaker_ioport_read(CPUX86State *env, uint32_t addr)
1031{
1032 int out;
1033 out = pit_get_out(&pit_channels[2]);
1034 return (speaker_data_on << 1) | pit_channels[2].gate | (out << 5);
1035}
1036
1037void pit_init(void)
1038{
87858c89
FB
1039 PITChannelState *s;
1040 int i;
1041
1042 cpu_calibrate_ticks();
1043
1044 for(i = 0;i < 3; i++) {
1045 s = &pit_channels[i];
1046 s->mode = 3;
1047 s->gate = (i != 2);
1048 pit_load_count(s, 0);
1049 }
1050
fc01f7e7
FB
1051 register_ioport_write(0x40, 4, pit_ioport_write, 1);
1052 register_ioport_read(0x40, 3, pit_ioport_read, 1);
0824d6fc 1053
fc01f7e7
FB
1054 register_ioport_read(0x61, 1, speaker_ioport_read, 1);
1055 register_ioport_write(0x61, 1, speaker_ioport_write, 1);
0824d6fc
FB
1056}
1057
1058/***********************************************************/
1059/* serial port emulation */
1060
1061#define UART_IRQ 4
1062
1063#define UART_LCR_DLAB 0x80 /* Divisor latch access bit */
1064
1065#define UART_IER_MSI 0x08 /* Enable Modem status interrupt */
1066#define UART_IER_RLSI 0x04 /* Enable receiver line status interrupt */
1067#define UART_IER_THRI 0x02 /* Enable Transmitter holding register int. */
1068#define UART_IER_RDI 0x01 /* Enable receiver data interrupt */
1069
1070#define UART_IIR_NO_INT 0x01 /* No interrupts pending */
1071#define UART_IIR_ID 0x06 /* Mask for the interrupt ID */
1072
1073#define UART_IIR_MSI 0x00 /* Modem status interrupt */
1074#define UART_IIR_THRI 0x02 /* Transmitter holding register empty */
1075#define UART_IIR_RDI 0x04 /* Receiver data interrupt */
1076#define UART_IIR_RLSI 0x06 /* Receiver line status interrupt */
1077
1078#define UART_LSR_TEMT 0x40 /* Transmitter empty */
1079#define UART_LSR_THRE 0x20 /* Transmit-hold-register empty */
1080#define UART_LSR_BI 0x10 /* Break interrupt indicator */
1081#define UART_LSR_FE 0x08 /* Frame error indicator */
1082#define UART_LSR_PE 0x04 /* Parity error indicator */
1083#define UART_LSR_OE 0x02 /* Overrun error indicator */
1084#define UART_LSR_DR 0x01 /* Receiver data ready */
1085
1086typedef struct SerialState {
1087 uint8_t divider;
1088 uint8_t rbr; /* receive register */
1089 uint8_t ier;
1090 uint8_t iir; /* read only */
1091 uint8_t lcr;
1092 uint8_t mcr;
1093 uint8_t lsr; /* read only */
1094 uint8_t msr;
1095 uint8_t scr;
1096} SerialState;
1097
1098SerialState serial_ports[1];
1099
1100void serial_update_irq(void)
1101{
1102 SerialState *s = &serial_ports[0];
1103
1104 if ((s->lsr & UART_LSR_DR) && (s->ier & UART_IER_RDI)) {
1105 s->iir = UART_IIR_RDI;
1106 } else if ((s->lsr & UART_LSR_THRE) && (s->ier & UART_IER_THRI)) {
1107 s->iir = UART_IIR_THRI;
1108 } else {
1109 s->iir = UART_IIR_NO_INT;
1110 }
1111 if (s->iir != UART_IIR_NO_INT) {
1112 pic_set_irq(UART_IRQ, 1);
1113 } else {
1114 pic_set_irq(UART_IRQ, 0);
1115 }
1116}
1117
1118void serial_ioport_write(CPUX86State *env, uint32_t addr, uint32_t val)
1119{
1120 SerialState *s = &serial_ports[0];
1121 unsigned char ch;
1122 int ret;
1123
1124 addr &= 7;
1125 switch(addr) {
1126 default:
1127 case 0:
1128 if (s->lcr & UART_LCR_DLAB) {
1129 s->divider = (s->divider & 0xff00) | val;
1130 } else {
1131 s->lsr &= ~UART_LSR_THRE;
1132 serial_update_irq();
1133
1134 ch = val;
1135 do {
1136 ret = write(1, &ch, 1);
1137 } while (ret != 1);
1138 s->lsr |= UART_LSR_THRE;
1139 s->lsr |= UART_LSR_TEMT;
1140 serial_update_irq();
1141 }
1142 break;
1143 case 1:
1144 if (s->lcr & UART_LCR_DLAB) {
1145 s->divider = (s->divider & 0x00ff) | (val << 8);
1146 } else {
1147 s->ier = val;
1148 serial_update_irq();
1149 }
1150 break;
1151 case 2:
1152 break;
1153 case 3:
1154 s->lcr = val;
1155 break;
1156 case 4:
1157 s->mcr = val;
1158 break;
1159 case 5:
1160 break;
1161 case 6:
1162 s->msr = val;
1163 break;
1164 case 7:
1165 s->scr = val;
1166 break;
1167 }
1168}
1169
1170uint32_t serial_ioport_read(CPUX86State *env, uint32_t addr)
1171{
1172 SerialState *s = &serial_ports[0];
1173 uint32_t ret;
1174
1175 addr &= 7;
1176 switch(addr) {
1177 default:
1178 case 0:
1179 if (s->lcr & UART_LCR_DLAB) {
1180 ret = s->divider & 0xff;
1181 } else {
1182 ret = s->rbr;
1183 s->lsr &= ~(UART_LSR_DR | UART_LSR_BI);
1184 serial_update_irq();
1185 }
1186 break;
1187 case 1:
1188 if (s->lcr & UART_LCR_DLAB) {
1189 ret = (s->divider >> 8) & 0xff;
1190 } else {
1191 ret = s->ier;
1192 }
1193 break;
1194 case 2:
1195 ret = s->iir;
1196 break;
1197 case 3:
1198 ret = s->lcr;
1199 break;
1200 case 4:
1201 ret = s->mcr;
1202 break;
1203 case 5:
1204 ret = s->lsr;
1205 break;
1206 case 6:
1207 ret = s->msr;
1208 break;
1209 case 7:
1210 ret = s->scr;
1211 break;
1212 }
1213 return ret;
1214}
1215
1216#define TERM_ESCAPE 0x01 /* ctrl-a is used for escape */
1217static int term_got_escape;
1218
1219void term_print_help(void)
1220{
1221 printf("\n"
1222 "C-a h print this help\n"
1223 "C-a x exit emulatior\n"
1224 "C-a b send break (magic sysrq)\n"
1225 "C-a C-a send C-a\n"
1226 );
1227}
1228
1229/* called when a char is received */
1230void serial_received_byte(SerialState *s, int ch)
1231{
1232 if (term_got_escape) {
1233 term_got_escape = 0;
1234 switch(ch) {
1235 case 'h':
1236 term_print_help();
1237 break;
1238 case 'x':
1239 exit(0);
1240 break;
1241 case 'b':
1242 /* send break */
1243 s->rbr = 0;
1244 s->lsr |= UART_LSR_BI | UART_LSR_DR;
1245 serial_update_irq();
1246 break;
1247 case TERM_ESCAPE:
1248 goto send_char;
1249 }
1250 } else if (ch == TERM_ESCAPE) {
1251 term_got_escape = 1;
1252 } else {
1253 send_char:
1254 s->rbr = ch;
1255 s->lsr |= UART_LSR_DR;
1256 serial_update_irq();
1257 }
1258}
1259
1260/* init terminal so that we can grab keys */
1261static struct termios oldtty;
1262
1263static void term_exit(void)
1264{
1265 tcsetattr (0, TCSANOW, &oldtty);
1266}
1267
1268static void term_init(void)
1269{
1270 struct termios tty;
1271
1272 tcgetattr (0, &tty);
1273 oldtty = tty;
1274
1275 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
1276 |INLCR|IGNCR|ICRNL|IXON);
1277 tty.c_oflag |= OPOST;
1278 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN|ISIG);
1279 tty.c_cflag &= ~(CSIZE|PARENB);
1280 tty.c_cflag |= CS8;
1281 tty.c_cc[VMIN] = 1;
1282 tty.c_cc[VTIME] = 0;
1283
1284 tcsetattr (0, TCSANOW, &tty);
1285
1286 atexit(term_exit);
1287
1288 fcntl(0, F_SETFL, O_NONBLOCK);
1289}
1290
1291void serial_init(void)
1292{
1293 SerialState *s = &serial_ports[0];
1294
1295 s->lsr = UART_LSR_TEMT | UART_LSR_THRE;
1296
fc01f7e7
FB
1297 register_ioport_write(0x3f8, 8, serial_ioport_write, 1);
1298 register_ioport_read(0x3f8, 8, serial_ioport_read, 1);
0824d6fc
FB
1299
1300 term_init();
1301}
1302
f1510b2c
FB
1303/***********************************************************/
1304/* ne2000 emulation */
1305
1306//#define DEBUG_NE2000
1307
1308#define NE2000_IOPORT 0x300
1309#define NE2000_IRQ 9
1310
1311#define MAX_ETH_FRAME_SIZE 1514
1312
1313#define E8390_CMD 0x00 /* The command register (for all pages) */
1314/* Page 0 register offsets. */
1315#define EN0_CLDALO 0x01 /* Low byte of current local dma addr RD */
1316#define EN0_STARTPG 0x01 /* Starting page of ring bfr WR */
1317#define EN0_CLDAHI 0x02 /* High byte of current local dma addr RD */
1318#define EN0_STOPPG 0x02 /* Ending page +1 of ring bfr WR */
1319#define EN0_BOUNDARY 0x03 /* Boundary page of ring bfr RD WR */
1320#define EN0_TSR 0x04 /* Transmit status reg RD */
1321#define EN0_TPSR 0x04 /* Transmit starting page WR */
1322#define EN0_NCR 0x05 /* Number of collision reg RD */
1323#define EN0_TCNTLO 0x05 /* Low byte of tx byte count WR */
1324#define EN0_FIFO 0x06 /* FIFO RD */
1325#define EN0_TCNTHI 0x06 /* High byte of tx byte count WR */
1326#define EN0_ISR 0x07 /* Interrupt status reg RD WR */
1327#define EN0_CRDALO 0x08 /* low byte of current remote dma address RD */
1328#define EN0_RSARLO 0x08 /* Remote start address reg 0 */
1329#define EN0_CRDAHI 0x09 /* high byte, current remote dma address RD */
1330#define EN0_RSARHI 0x09 /* Remote start address reg 1 */
1331#define EN0_RCNTLO 0x0a /* Remote byte count reg WR */
1332#define EN0_RCNTHI 0x0b /* Remote byte count reg WR */
1333#define EN0_RSR 0x0c /* rx status reg RD */
1334#define EN0_RXCR 0x0c /* RX configuration reg WR */
1335#define EN0_TXCR 0x0d /* TX configuration reg WR */
1336#define EN0_COUNTER0 0x0d /* Rcv alignment error counter RD */
1337#define EN0_DCFG 0x0e /* Data configuration reg WR */
1338#define EN0_COUNTER1 0x0e /* Rcv CRC error counter RD */
1339#define EN0_IMR 0x0f /* Interrupt mask reg WR */
1340#define EN0_COUNTER2 0x0f /* Rcv missed frame error counter RD */
1341
1342#define EN1_PHYS 0x11
1343#define EN1_CURPAG 0x17
1344#define EN1_MULT 0x18
1345
1346/* Register accessed at EN_CMD, the 8390 base addr. */
1347#define E8390_STOP 0x01 /* Stop and reset the chip */
1348#define E8390_START 0x02 /* Start the chip, clear reset */
1349#define E8390_TRANS 0x04 /* Transmit a frame */
1350#define E8390_RREAD 0x08 /* Remote read */
1351#define E8390_RWRITE 0x10 /* Remote write */
1352#define E8390_NODMA 0x20 /* Remote DMA */
1353#define E8390_PAGE0 0x00 /* Select page chip registers */
1354#define E8390_PAGE1 0x40 /* using the two high-order bits */
1355#define E8390_PAGE2 0x80 /* Page 3 is invalid. */
1356
1357/* Bits in EN0_ISR - Interrupt status register */
1358#define ENISR_RX 0x01 /* Receiver, no error */
1359#define ENISR_TX 0x02 /* Transmitter, no error */
1360#define ENISR_RX_ERR 0x04 /* Receiver, with error */
1361#define ENISR_TX_ERR 0x08 /* Transmitter, with error */
1362#define ENISR_OVER 0x10 /* Receiver overwrote the ring */
1363#define ENISR_COUNTERS 0x20 /* Counters need emptying */
1364#define ENISR_RDC 0x40 /* remote dma complete */
1365#define ENISR_RESET 0x80 /* Reset completed */
1366#define ENISR_ALL 0x3f /* Interrupts we will enable */
1367
1368/* Bits in received packet status byte and EN0_RSR*/
1369#define ENRSR_RXOK 0x01 /* Received a good packet */
1370#define ENRSR_CRC 0x02 /* CRC error */
1371#define ENRSR_FAE 0x04 /* frame alignment error */
1372#define ENRSR_FO 0x08 /* FIFO overrun */
1373#define ENRSR_MPA 0x10 /* missed pkt */
1374#define ENRSR_PHY 0x20 /* physical/multicast address */
1375#define ENRSR_DIS 0x40 /* receiver disable. set in monitor mode */
1376#define ENRSR_DEF 0x80 /* deferring */
1377
1378/* Transmitted packet status, EN0_TSR. */
1379#define ENTSR_PTX 0x01 /* Packet transmitted without error */
1380#define ENTSR_ND 0x02 /* The transmit wasn't deferred. */
1381#define ENTSR_COL 0x04 /* The transmit collided at least once. */
1382#define ENTSR_ABT 0x08 /* The transmit collided 16 times, and was deferred. */
1383#define ENTSR_CRS 0x10 /* The carrier sense was lost. */
1384#define ENTSR_FU 0x20 /* A "FIFO underrun" occurred during transmit. */
1385#define ENTSR_CDH 0x40 /* The collision detect "heartbeat" signal was lost. */
1386#define ENTSR_OWC 0x80 /* There was an out-of-window collision. */
1387
1388#define NE2000_MEM_SIZE 32768
1389
1390typedef struct NE2000State {
1391 uint8_t cmd;
1392 uint32_t start;
1393 uint32_t stop;
1394 uint8_t boundary;
1395 uint8_t tsr;
1396 uint8_t tpsr;
1397 uint16_t tcnt;
1398 uint16_t rcnt;
1399 uint32_t rsar;
1400 uint8_t isr;
1401 uint8_t dcfg;
1402 uint8_t imr;
1403 uint8_t phys[6]; /* mac address */
1404 uint8_t curpag;
1405 uint8_t mult[8]; /* multicast mask array */
1406 uint8_t mem[NE2000_MEM_SIZE];
1407} NE2000State;
1408
1409NE2000State ne2000_state;
1410int net_fd = -1;
1411char network_script[1024];
1412
1413void ne2000_reset(void)
1414{
1415 NE2000State *s = &ne2000_state;
1416 int i;
1417
1418 s->isr = ENISR_RESET;
1419 s->mem[0] = 0x52;
1420 s->mem[1] = 0x54;
1421 s->mem[2] = 0x00;
1422 s->mem[3] = 0x12;
1423 s->mem[4] = 0x34;
1424 s->mem[5] = 0x56;
1425 s->mem[14] = 0x57;
1426 s->mem[15] = 0x57;
1427
1428 /* duplicate prom data */
1429 for(i = 15;i >= 0; i--) {
1430 s->mem[2 * i] = s->mem[i];
1431 s->mem[2 * i + 1] = s->mem[i];
1432 }
1433}
1434
1435void ne2000_update_irq(NE2000State *s)
1436{
1437 int isr;
1438 isr = s->isr & s->imr;
1439 if (isr)
1440 pic_set_irq(NE2000_IRQ, 1);
1441 else
1442 pic_set_irq(NE2000_IRQ, 0);
1443}
1444
1445int net_init(void)
1446{
1447 struct ifreq ifr;
1448 int fd, ret, pid, status;
1449
1450 fd = open("/dev/net/tun", O_RDWR);
1451 if (fd < 0) {
1452 fprintf(stderr, "warning: could not open /dev/net/tun: no virtual network emulation\n");
1453 return -1;
1454 }
1455 memset(&ifr, 0, sizeof(ifr));
1456 ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
1457 pstrcpy(ifr.ifr_name, IFNAMSIZ, "tun%d");
1458 ret = ioctl(fd, TUNSETIFF, (void *) &ifr);
1459 if (ret != 0) {
1460 fprintf(stderr, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
1461 close(fd);
1462 return -1;
1463 }
fc01f7e7 1464 printf("Connected to host network interface: %s\n", ifr.ifr_name);
f1510b2c
FB
1465 fcntl(fd, F_SETFL, O_NONBLOCK);
1466 net_fd = fd;
1467
1468 /* try to launch network init script */
1469 pid = fork();
1470 if (pid >= 0) {
1471 if (pid == 0) {
1472 execl(network_script, network_script, ifr.ifr_name, NULL);
1473 exit(1);
1474 }
1475 while (waitpid(pid, &status, 0) != pid);
1476 if (!WIFEXITED(status) ||
1477 WEXITSTATUS(status) != 0) {
1478 fprintf(stderr, "%s: could not launch network script for '%s'\n",
1479 network_script, ifr.ifr_name);
1480 }
1481 }
1482 return 0;
1483}
1484
1485void net_send_packet(NE2000State *s, const uint8_t *buf, int size)
1486{
1487#ifdef DEBUG_NE2000
1488 printf("NE2000: sending packet size=%d\n", size);
1489#endif
1490 write(net_fd, buf, size);
1491}
1492
1493/* return true if the NE2000 can receive more data */
1494int ne2000_can_receive(NE2000State *s)
1495{
1496 int avail, index, boundary;
1497
1498 if (s->cmd & E8390_STOP)
1499 return 0;
1500 index = s->curpag << 8;
1501 boundary = s->boundary << 8;
1502 if (index < boundary)
1503 avail = boundary - index;
1504 else
1505 avail = (s->stop - s->start) - (index - boundary);
1506 if (avail < (MAX_ETH_FRAME_SIZE + 4))
1507 return 0;
1508 return 1;
1509}
1510
1511void ne2000_receive(NE2000State *s, uint8_t *buf, int size)
1512{
1513 uint8_t *p;
1514 int total_len, next, avail, len, index;
1515
1516#if defined(DEBUG_NE2000)
1517 printf("NE2000: received len=%d\n", size);
1518#endif
1519
1520 index = s->curpag << 8;
1521 /* 4 bytes for header */
1522 total_len = size + 4;
1523 /* address for next packet (4 bytes for CRC) */
1524 next = index + ((total_len + 4 + 255) & ~0xff);
1525 if (next >= s->stop)
1526 next -= (s->stop - s->start);
1527 /* prepare packet header */
1528 p = s->mem + index;
1529 p[0] = ENRSR_RXOK; /* receive status */
1530 p[1] = next >> 8;
1531 p[2] = total_len;
1532 p[3] = total_len >> 8;
1533 index += 4;
1534
1535 /* write packet data */
1536 while (size > 0) {
1537 avail = s->stop - index;
1538 len = size;
1539 if (len > avail)
1540 len = avail;
1541 memcpy(s->mem + index, buf, len);
1542 buf += len;
1543 index += len;
1544 if (index == s->stop)
1545 index = s->start;
1546 size -= len;
1547 }
1548 s->curpag = next >> 8;
1549
1550 /* now we can signal we have receive something */
1551 s->isr |= ENISR_RX;
1552 ne2000_update_irq(s);
1553}
1554
1555void ne2000_ioport_write(CPUX86State *env, uint32_t addr, uint32_t val)
1556{
1557 NE2000State *s = &ne2000_state;
1558 int offset, page;
1559
1560 addr &= 0xf;
1561#ifdef DEBUG_NE2000
1562 printf("NE2000: write addr=0x%x val=0x%02x\n", addr, val);
1563#endif
1564 if (addr == E8390_CMD) {
1565 /* control register */
1566 s->cmd = val;
1567 if (val & E8390_START) {
1568 /* test specific case: zero length transfert */
1569 if ((val & (E8390_RREAD | E8390_RWRITE)) &&
1570 s->rcnt == 0) {
1571 s->isr |= ENISR_RDC;
1572 ne2000_update_irq(s);
87858c89
FB
1573 /* XXX: find a better solution for irqs */
1574 cpu_x86_interrupt(global_env);
f1510b2c
FB
1575 }
1576 if (val & E8390_TRANS) {
1577 net_send_packet(s, s->mem + (s->tpsr << 8), s->tcnt);
1578 /* signal end of transfert */
1579 s->tsr = ENTSR_PTX;
1580 s->isr |= ENISR_TX;
1581 ne2000_update_irq(s);
1582 }
1583 }
1584 } else {
1585 page = s->cmd >> 6;
1586 offset = addr | (page << 4);
1587 switch(offset) {
1588 case EN0_STARTPG:
1589 s->start = val << 8;
1590 break;
1591 case EN0_STOPPG:
1592 s->stop = val << 8;
1593 break;
1594 case EN0_BOUNDARY:
1595 s->boundary = val;
1596 break;
1597 case EN0_IMR:
1598 s->imr = val;
1599 ne2000_update_irq(s);
1600 break;
1601 case EN0_TPSR:
1602 s->tpsr = val;
1603 break;
1604 case EN0_TCNTLO:
1605 s->tcnt = (s->tcnt & 0xff00) | val;
1606 break;
1607 case EN0_TCNTHI:
1608 s->tcnt = (s->tcnt & 0x00ff) | (val << 8);
1609 break;
1610 case EN0_RSARLO:
1611 s->rsar = (s->rsar & 0xff00) | val;
1612 break;
1613 case EN0_RSARHI:
1614 s->rsar = (s->rsar & 0x00ff) | (val << 8);
1615 break;
1616 case EN0_RCNTLO:
1617 s->rcnt = (s->rcnt & 0xff00) | val;
1618 break;
1619 case EN0_RCNTHI:
1620 s->rcnt = (s->rcnt & 0x00ff) | (val << 8);
1621 break;
1622 case EN0_DCFG:
1623 s->dcfg = val;
1624 break;
1625 case EN0_ISR:
1626 s->isr &= ~val;
1627 ne2000_update_irq(s);
1628 break;
1629 case EN1_PHYS ... EN1_PHYS + 5:
1630 s->phys[offset - EN1_PHYS] = val;
1631 break;
1632 case EN1_CURPAG:
1633 s->curpag = val;
1634 break;
1635 case EN1_MULT ... EN1_MULT + 7:
1636 s->mult[offset - EN1_MULT] = val;
1637 break;
1638 }
1639 }
1640}
1641
1642uint32_t ne2000_ioport_read(CPUX86State *env, uint32_t addr)
1643{
1644 NE2000State *s = &ne2000_state;
1645 int offset, page, ret;
1646
1647 addr &= 0xf;
1648 if (addr == E8390_CMD) {
1649 ret = s->cmd;
1650 } else {
1651 page = s->cmd >> 6;
1652 offset = addr | (page << 4);
1653 switch(offset) {
1654 case EN0_TSR:
1655 ret = s->tsr;
1656 break;
1657 case EN0_BOUNDARY:
1658 ret = s->boundary;
1659 break;
1660 case EN0_ISR:
1661 ret = s->isr;
1662 break;
1663 case EN1_PHYS ... EN1_PHYS + 5:
1664 ret = s->phys[offset - EN1_PHYS];
1665 break;
1666 case EN1_CURPAG:
1667 ret = s->curpag;
1668 break;
1669 case EN1_MULT ... EN1_MULT + 7:
1670 ret = s->mult[offset - EN1_MULT];
1671 break;
1672 default:
1673 ret = 0x00;
1674 break;
1675 }
1676 }
1677#ifdef DEBUG_NE2000
1678 printf("NE2000: read addr=0x%x val=%02x\n", addr, ret);
1679#endif
1680 return ret;
1681}
1682
1683void ne2000_asic_ioport_write(CPUX86State *env, uint32_t addr, uint32_t val)
1684{
1685 NE2000State *s = &ne2000_state;
1686 uint8_t *p;
1687
1688#ifdef DEBUG_NE2000
1689 printf("NE2000: asic write val=0x%04x\n", val);
1690#endif
1691 p = s->mem + s->rsar;
1692 if (s->dcfg & 0x01) {
1693 /* 16 bit access */
1694 p[0] = val;
1695 p[1] = val >> 8;
1696 s->rsar += 2;
1697 s->rcnt -= 2;
1698 } else {
1699 /* 8 bit access */
1700 p[0] = val;
1701 s->rsar++;
1702 s->rcnt--;
1703 }
1704 /* wrap */
1705 if (s->rsar == s->stop)
1706 s->rsar = s->start;
1707 if (s->rcnt == 0) {
1708 /* signal end of transfert */
1709 s->isr |= ENISR_RDC;
1710 ne2000_update_irq(s);
1711 }
1712}
1713
1714uint32_t ne2000_asic_ioport_read(CPUX86State *env, uint32_t addr)
1715{
1716 NE2000State *s = &ne2000_state;
1717 uint8_t *p;
1718 int ret;
1719
1720 p = s->mem + s->rsar;
1721 if (s->dcfg & 0x01) {
1722 /* 16 bit access */
1723 ret = p[0] | (p[1] << 8);
1724 s->rsar += 2;
1725 s->rcnt -= 2;
1726 } else {
1727 /* 8 bit access */
1728 ret = p[0];
1729 s->rsar++;
1730 s->rcnt--;
1731 }
1732 /* wrap */
1733 if (s->rsar == s->stop)
1734 s->rsar = s->start;
1735 if (s->rcnt == 0) {
1736 /* signal end of transfert */
1737 s->isr |= ENISR_RDC;
1738 ne2000_update_irq(s);
1739 }
1740#ifdef DEBUG_NE2000
1741 printf("NE2000: asic read val=0x%04x\n", ret);
1742#endif
1743 return ret;
1744}
1745
1746void ne2000_reset_ioport_write(CPUX86State *env, uint32_t addr, uint32_t val)
1747{
1748 /* nothing to do (end of reset pulse) */
1749}
1750
1751uint32_t ne2000_reset_ioport_read(CPUX86State *env, uint32_t addr)
1752{
1753 ne2000_reset();
1754 return 0;
1755}
1756
1757void ne2000_init(void)
1758{
fc01f7e7
FB
1759 register_ioport_write(NE2000_IOPORT, 16, ne2000_ioport_write, 1);
1760 register_ioport_read(NE2000_IOPORT, 16, ne2000_ioport_read, 1);
f1510b2c 1761
fc01f7e7
FB
1762 register_ioport_write(NE2000_IOPORT + 0x10, 1, ne2000_asic_ioport_write, 1);
1763 register_ioport_read(NE2000_IOPORT + 0x10, 1, ne2000_asic_ioport_read, 1);
1764 register_ioport_write(NE2000_IOPORT + 0x10, 2, ne2000_asic_ioport_write, 2);
1765 register_ioport_read(NE2000_IOPORT + 0x10, 2, ne2000_asic_ioport_read, 2);
f1510b2c 1766
fc01f7e7
FB
1767 register_ioport_write(NE2000_IOPORT + 0x1f, 1, ne2000_reset_ioport_write, 1);
1768 register_ioport_read(NE2000_IOPORT + 0x1f, 1, ne2000_reset_ioport_read, 1);
f1510b2c
FB
1769 ne2000_reset();
1770}
1771
fc01f7e7
FB
1772/***********************************************************/
1773/* ide emulation */
1774
1775//#define DEBUG_IDE
1776
1777/* Bits of HD_STATUS */
1778#define ERR_STAT 0x01
1779#define INDEX_STAT 0x02
1780#define ECC_STAT 0x04 /* Corrected error */
1781#define DRQ_STAT 0x08
1782#define SEEK_STAT 0x10
1783#define SRV_STAT 0x10
1784#define WRERR_STAT 0x20
1785#define READY_STAT 0x40
1786#define BUSY_STAT 0x80
1787
1788/* Bits for HD_ERROR */
1789#define MARK_ERR 0x01 /* Bad address mark */
1790#define TRK0_ERR 0x02 /* couldn't find track 0 */
1791#define ABRT_ERR 0x04 /* Command aborted */
1792#define MCR_ERR 0x08 /* media change request */
1793#define ID_ERR 0x10 /* ID field not found */
1794#define MC_ERR 0x20 /* media changed */
1795#define ECC_ERR 0x40 /* Uncorrectable ECC error */
1796#define BBD_ERR 0x80 /* pre-EIDE meaning: block marked bad */
1797#define ICRC_ERR 0x80 /* new meaning: CRC error during transfer */
1798
1799/* Bits of HD_NSECTOR */
1800#define CD 0x01
1801#define IO 0x02
1802#define REL 0x04
1803#define TAG_MASK 0xf8
1804
1805#define IDE_CMD_RESET 0x04
1806#define IDE_CMD_DISABLE_IRQ 0x02
1807
1808/* ATA/ATAPI Commands pre T13 Spec */
1809#define WIN_NOP 0x00
1810/*
1811 * 0x01->0x02 Reserved
1812 */
1813#define CFA_REQ_EXT_ERROR_CODE 0x03 /* CFA Request Extended Error Code */
1814/*
1815 * 0x04->0x07 Reserved
1816 */
1817#define WIN_SRST 0x08 /* ATAPI soft reset command */
1818#define WIN_DEVICE_RESET 0x08
1819/*
1820 * 0x09->0x0F Reserved
1821 */
1822#define WIN_RECAL 0x10
1823#define WIN_RESTORE WIN_RECAL
1824/*
1825 * 0x10->0x1F Reserved
1826 */
1827#define WIN_READ 0x20 /* 28-Bit */
1828#define WIN_READ_ONCE 0x21 /* 28-Bit without retries */
1829#define WIN_READ_LONG 0x22 /* 28-Bit */
1830#define WIN_READ_LONG_ONCE 0x23 /* 28-Bit without retries */
1831#define WIN_READ_EXT 0x24 /* 48-Bit */
1832#define WIN_READDMA_EXT 0x25 /* 48-Bit */
1833#define WIN_READDMA_QUEUED_EXT 0x26 /* 48-Bit */
1834#define WIN_READ_NATIVE_MAX_EXT 0x27 /* 48-Bit */
1835/*
1836 * 0x28
1837 */
1838#define WIN_MULTREAD_EXT 0x29 /* 48-Bit */
1839/*
1840 * 0x2A->0x2F Reserved
1841 */
1842#define WIN_WRITE 0x30 /* 28-Bit */
1843#define WIN_WRITE_ONCE 0x31 /* 28-Bit without retries */
1844#define WIN_WRITE_LONG 0x32 /* 28-Bit */
1845#define WIN_WRITE_LONG_ONCE 0x33 /* 28-Bit without retries */
1846#define WIN_WRITE_EXT 0x34 /* 48-Bit */
1847#define WIN_WRITEDMA_EXT 0x35 /* 48-Bit */
1848#define WIN_WRITEDMA_QUEUED_EXT 0x36 /* 48-Bit */
1849#define WIN_SET_MAX_EXT 0x37 /* 48-Bit */
1850#define CFA_WRITE_SECT_WO_ERASE 0x38 /* CFA Write Sectors without erase */
1851#define WIN_MULTWRITE_EXT 0x39 /* 48-Bit */
1852/*
1853 * 0x3A->0x3B Reserved
1854 */
1855#define WIN_WRITE_VERIFY 0x3C /* 28-Bit */
1856/*
1857 * 0x3D->0x3F Reserved
1858 */
1859#define WIN_VERIFY 0x40 /* 28-Bit - Read Verify Sectors */
1860#define WIN_VERIFY_ONCE 0x41 /* 28-Bit - without retries */
1861#define WIN_VERIFY_EXT 0x42 /* 48-Bit */
1862/*
1863 * 0x43->0x4F Reserved
1864 */
1865#define WIN_FORMAT 0x50
1866/*
1867 * 0x51->0x5F Reserved
1868 */
1869#define WIN_INIT 0x60
1870/*
1871 * 0x61->0x5F Reserved
1872 */
1873#define WIN_SEEK 0x70 /* 0x70-0x7F Reserved */
1874#define CFA_TRANSLATE_SECTOR 0x87 /* CFA Translate Sector */
1875#define WIN_DIAGNOSE 0x90
1876#define WIN_SPECIFY 0x91 /* set drive geometry translation */
1877#define WIN_DOWNLOAD_MICROCODE 0x92
1878#define WIN_STANDBYNOW2 0x94
1879#define WIN_STANDBY2 0x96
1880#define WIN_SETIDLE2 0x97
1881#define WIN_CHECKPOWERMODE2 0x98
1882#define WIN_SLEEPNOW2 0x99
1883/*
1884 * 0x9A VENDOR
1885 */
1886#define WIN_PACKETCMD 0xA0 /* Send a packet command. */
1887#define WIN_PIDENTIFY 0xA1 /* identify ATAPI device */
1888#define WIN_QUEUED_SERVICE 0xA2
1889#define WIN_SMART 0xB0 /* self-monitoring and reporting */
1890#define CFA_ERASE_SECTORS 0xC0
1891#define WIN_MULTREAD 0xC4 /* read sectors using multiple mode*/
1892#define WIN_MULTWRITE 0xC5 /* write sectors using multiple mode */
1893#define WIN_SETMULT 0xC6 /* enable/disable multiple mode */
1894#define WIN_READDMA_QUEUED 0xC7 /* read sectors using Queued DMA transfers */
1895#define WIN_READDMA 0xC8 /* read sectors using DMA transfers */
1896#define WIN_READDMA_ONCE 0xC9 /* 28-Bit - without retries */
1897#define WIN_WRITEDMA 0xCA /* write sectors using DMA transfers */
1898#define WIN_WRITEDMA_ONCE 0xCB /* 28-Bit - without retries */
1899#define WIN_WRITEDMA_QUEUED 0xCC /* write sectors using Queued DMA transfers */
1900#define CFA_WRITE_MULTI_WO_ERASE 0xCD /* CFA Write multiple without erase */
1901#define WIN_GETMEDIASTATUS 0xDA
1902#define WIN_ACKMEDIACHANGE 0xDB /* ATA-1, ATA-2 vendor */
1903#define WIN_POSTBOOT 0xDC
1904#define WIN_PREBOOT 0xDD
1905#define WIN_DOORLOCK 0xDE /* lock door on removable drives */
1906#define WIN_DOORUNLOCK 0xDF /* unlock door on removable drives */
1907#define WIN_STANDBYNOW1 0xE0
1908#define WIN_IDLEIMMEDIATE 0xE1 /* force drive to become "ready" */
1909#define WIN_STANDBY 0xE2 /* Set device in Standby Mode */
1910#define WIN_SETIDLE1 0xE3
1911#define WIN_READ_BUFFER 0xE4 /* force read only 1 sector */
1912#define WIN_CHECKPOWERMODE1 0xE5
1913#define WIN_SLEEPNOW1 0xE6
1914#define WIN_FLUSH_CACHE 0xE7
1915#define WIN_WRITE_BUFFER 0xE8 /* force write only 1 sector */
1916#define WIN_WRITE_SAME 0xE9 /* read ata-2 to use */
1917 /* SET_FEATURES 0x22 or 0xDD */
1918#define WIN_FLUSH_CACHE_EXT 0xEA /* 48-Bit */
1919#define WIN_IDENTIFY 0xEC /* ask drive to identify itself */
1920#define WIN_MEDIAEJECT 0xED
1921#define WIN_IDENTIFY_DMA 0xEE /* same as WIN_IDENTIFY, but DMA */
1922#define WIN_SETFEATURES 0xEF /* set special drive features */
1923#define EXABYTE_ENABLE_NEST 0xF0
1924#define WIN_SECURITY_SET_PASS 0xF1
1925#define WIN_SECURITY_UNLOCK 0xF2
1926#define WIN_SECURITY_ERASE_PREPARE 0xF3
1927#define WIN_SECURITY_ERASE_UNIT 0xF4
1928#define WIN_SECURITY_FREEZE_LOCK 0xF5
1929#define WIN_SECURITY_DISABLE 0xF6
1930#define WIN_READ_NATIVE_MAX 0xF8 /* return the native maximum address */
1931#define WIN_SET_MAX 0xF9
1932#define DISABLE_SEAGATE 0xFB
1933
1934#define MAX_MULT_SECTORS 16
1935
1936#define MAX_DISKS 2
1937
1938struct IDEState;
1939
1940typedef void EndTransferFunc(struct IDEState *);
1941
1942typedef struct IDEState {
1943 /* ide config */
1944 int cylinders, heads, sectors;
1945 int64_t nb_sectors;
1946 int mult_sectors;
1947 int irq;
1948 /* ide regs */
1949 uint8_t feature;
1950 uint8_t error;
1951 uint8_t nsector;
1952 uint8_t sector;
1953 uint8_t lcyl;
1954 uint8_t hcyl;
1955 uint8_t select;
1956 uint8_t status;
1957 /* 0x3f6 command, only meaningful for drive 0 */
1958 uint8_t cmd;
1959 /* depends on bit 4 in select, only meaningful for drive 0 */
1960 struct IDEState *cur_drive;
1961 BlockDriverState *bs;
1962 EndTransferFunc *end_transfer_func;
1963 uint8_t *data_ptr;
1964 uint8_t *data_end;
1965 uint8_t io_buffer[MAX_MULT_SECTORS*512 + 4];
1966} IDEState;
1967
1968BlockDriverState *bs_table[MAX_DISKS];
1969IDEState ide_state[MAX_DISKS];
1970
1971static void padstr(char *str, const char *src, int len)
1972{
1973 int i, v;
1974 for(i = 0; i < len; i++) {
1975 if (*src)
1976 v = *src++;
1977 else
1978 v = ' ';
1979 *(char *)((long)str ^ 1) = v;
1980 str++;
1981 }
1982}
1983
1984static void ide_identify(IDEState *s)
1985{
1986 uint16_t *p;
1987 unsigned int oldsize;
1988
1989 memset(s->io_buffer, 0, 512);
1990 p = (uint16_t *)s->io_buffer;
1991 stw(p + 0, 0x0040);
1992 stw(p + 1, s->cylinders);
1993 stw(p + 3, s->heads);
1994 stw(p + 4, 512 * s->sectors); /* sectors */
1995 stw(p + 5, 512); /* sector size */
1996 stw(p + 6, s->sectors);
1997 stw(p + 20, 3); /* buffer type */
1998 stw(p + 21, 512); /* cache size in sectors */
1999 stw(p + 22, 4); /* ecc bytes */
2000 padstr((uint8_t *)(p + 27), "QEMU HARDDISK", 40);
2001 // stw(p + 47, MAX_MULT_SECTORS);
2002 stw(p + 48, 1); /* dword I/O */
2003 stw(p + 49, 1 << 9); /* LBA supported, no DMA */
2004 stw(p + 51, 0x200); /* PIO transfer cycle */
2005 stw(p + 52, 0x200); /* DMA transfer cycle */
2006 stw(p + 54, s->cylinders);
2007 stw(p + 55, s->heads);
2008 stw(p + 56, s->sectors);
2009 oldsize = s->cylinders * s->heads * s->sectors;
2010 stw(p + 57, oldsize);
2011 stw(p + 58, oldsize >> 16);
2012 if (s->mult_sectors)
2013 stw(p + 59, 0x100 | s->mult_sectors);
2014 stw(p + 60, s->nb_sectors);
2015 stw(p + 61, s->nb_sectors >> 16);
2016 stw(p + 80, (1 << 1) | (1 << 2));
2017 stw(p + 82, (1 << 14));
2018 stw(p + 83, (1 << 14));
2019 stw(p + 84, (1 << 14));
2020 stw(p + 85, (1 << 14));
2021 stw(p + 86, 0);
2022 stw(p + 87, (1 << 14));
2023}
2024
2025static inline void ide_abort_command(IDEState *s)
2026{
2027 s->status = READY_STAT | ERR_STAT;
2028 s->error = ABRT_ERR;
2029}
2030
2031static inline void ide_set_irq(IDEState *s)
2032{
2033 if (!(ide_state[0].cmd & IDE_CMD_DISABLE_IRQ)) {
2034 pic_set_irq(s->irq, 1);
2035 cpu_x86_interrupt(global_env);
2036 }
2037}
2038
2039/* prepare data transfer and tell what to do after */
2040static void ide_transfer_start(IDEState *s, int size,
2041 EndTransferFunc *end_transfer_func)
2042{
2043 s->end_transfer_func = end_transfer_func;
2044 s->data_ptr = s->io_buffer;
2045 s->data_end = s->io_buffer + size;
2046 s->status |= DRQ_STAT;
2047}
2048
2049static void ide_transfer_stop(IDEState *s)
2050{
2051 s->end_transfer_func = ide_transfer_stop;
2052 s->data_ptr = s->io_buffer;
2053 s->data_end = s->io_buffer;
2054 s->status &= ~DRQ_STAT;
2055}
2056
2057static int64_t ide_get_sector(IDEState *s)
2058{
2059 int64_t sector_num;
2060 if (s->select & 0x40) {
2061 /* lba */
2062 sector_num = ((s->select & 0x0f) << 24) | (s->hcyl << 16) |
2063 (s->lcyl << 8) | s->sector;
2064 } else {
2065 sector_num = ((s->hcyl << 8) | s->lcyl) * s->heads * s->sectors +
2066 (s->select & 0x0f) * s->sectors +
2067 (s->sector - 1);
2068 }
2069 return sector_num;
2070}
2071
2072static void ide_set_sector(IDEState *s, int64_t sector_num)
2073{
2074 unsigned int cyl, r;
2075 if (s->select & 0x40) {
2076 s->select = (s->select & 0xf0) | (sector_num >> 24);
2077 s->hcyl = (sector_num >> 16);
2078 s->lcyl = (sector_num >> 8);
2079 s->sector = (sector_num);
2080 } else {
2081 cyl = sector_num / (s->heads * s->sectors);
2082 r = sector_num % (s->heads * s->sectors);
2083 s->hcyl = cyl >> 8;
2084 s->lcyl = cyl;
2085 s->select = (s->select & 0xf0) | (r / s->sectors);
2086 s->sector = (r % s->sectors) + 1;
2087 }
2088}
2089
2090static void ide_sector_read(IDEState *s)
2091{
2092 int64_t sector_num;
2093 int ret;
2094
2095 s->status = READY_STAT | SEEK_STAT;
2096 sector_num = ide_get_sector(s);
2097 s->nsector--;
2098 if (s->nsector == 0xff) {
2099 /* no more sector to read from disk */
2100 ide_transfer_stop(s);
2101 } else {
2102#if defined(DEBUG_IDE)
2103 printf("read sector=%Ld\n", sector_num);
2104#endif
2105 ret = bdrv_read(s->bs, sector_num, s->io_buffer, 1);
2106 ide_transfer_start(s, 512, ide_sector_read);
2107 ide_set_irq(s);
2108 }
2109 ide_set_sector(s, sector_num + 1);
2110}
2111
2112static void ide_sector_write(IDEState *s)
2113{
2114 int64_t sector_num;
2115 int ret;
2116
2117 s->status = READY_STAT | SEEK_STAT;
2118 sector_num = ide_get_sector(s);
2119#if defined(DEBUG_IDE)
2120 printf("write sector=%Ld\n", sector_num);
2121#endif
2122 ret = bdrv_write(s->bs, sector_num, s->io_buffer, 1);
2123 s->nsector--;
2124 if (s->nsector == 0) {
2125 /* no more sector to write */
2126 ide_transfer_stop(s);
2127 } else {
2128 ide_transfer_start(s, 512, ide_sector_write);
2129 }
2130 ide_set_sector(s, sector_num + 1);
2131 ide_set_irq(s);
2132}
2133
2134void ide_ioport_write(CPUX86State *env, uint32_t addr, uint32_t val)
2135{
2136 IDEState *s = ide_state[0].cur_drive;
2137 int unit;
2138
2139 addr &= 7;
2140#ifdef DEBUG_IDE
2141 printf("IDE: write addr=0x%x val=0x%02x\n", addr, val);
2142#endif
2143 switch(addr) {
2144 case 0:
2145 break;
2146 case 1:
2147 s->feature = val;
2148 break;
2149 case 2:
2150 s->nsector = val;
2151 break;
2152 case 3:
2153 s->sector = val;
2154 break;
2155 case 4:
2156 s->lcyl = val;
2157 break;
2158 case 5:
2159 s->hcyl = val;
2160 break;
2161 case 6:
2162 /* select drive */
2163 unit = (val >> 4) & 1;
2164 s = &ide_state[unit];
2165 ide_state[0].cur_drive = s;
2166 s->select = val;
2167 break;
2168 default:
2169 case 7:
2170 /* command */
2171#if defined(DEBUG_IDE)
2172 printf("ide: CMD=%02x\n", val);
2173#endif
2174 switch(val) {
2175 case WIN_PIDENTIFY:
2176 case WIN_IDENTIFY:
2177 if (s->bs) {
2178 ide_identify(s);
2179 s->status = READY_STAT;
2180 ide_transfer_start(s, 512, ide_transfer_stop);
2181 } else {
2182 ide_abort_command(s);
2183 }
2184 ide_set_irq(s);
2185 break;
2186 case WIN_SPECIFY:
2187 case WIN_RECAL:
2188 s->status = READY_STAT;
2189 ide_set_irq(s);
2190 break;
2191 case WIN_SETMULT:
2192 if (s->nsector > MAX_MULT_SECTORS ||
2193 s->nsector == 0 ||
2194 (s->nsector & (s->nsector - 1)) != 0) {
2195 ide_abort_command(s);
2196 } else {
2197 s->mult_sectors = s->nsector;
2198 s->status = READY_STAT;
2199 }
2200 ide_set_irq(s);
2201 break;
2202 case WIN_READ:
2203 case WIN_READ_ONCE:
2204 ide_sector_read(s);
2205 break;
2206 case WIN_WRITE:
2207 case WIN_WRITE_ONCE:
2208 s->status = SEEK_STAT;
2209 ide_transfer_start(s, 512, ide_sector_write);
2210 break;
2211 default:
2212 ide_abort_command(s);
2213 ide_set_irq(s);
2214 break;
2215 }
2216 }
2217}
2218
2219uint32_t ide_ioport_read(CPUX86State *env, uint32_t addr)
2220{
2221 IDEState *s = ide_state[0].cur_drive;
2222 int ret;
2223
2224 addr &= 7;
2225 switch(addr) {
2226 case 0:
2227 ret = 0xff;
2228 break;
2229 case 1:
2230 ret = s->error;
2231 break;
2232 case 2:
2233 ret = s->nsector;
2234 break;
2235 case 3:
2236 ret = s->sector;
2237 break;
2238 case 4:
2239 ret = s->lcyl;
2240 break;
2241 case 5:
2242 ret = s->hcyl;
2243 break;
2244 case 6:
2245 ret = s->select;
2246 break;
2247 default:
2248 case 7:
2249 ret = s->status;
2250 pic_set_irq(s->irq, 0);
2251 break;
2252 }
2253#ifdef DEBUG_IDE
2254 printf("ide: read addr=0x%x val=%02x\n", addr, ret);
2255#endif
2256 return ret;
2257}
2258
2259uint32_t ide_status_read(CPUX86State *env, uint32_t addr)
2260{
2261 IDEState *s = ide_state[0].cur_drive;
2262 int ret;
2263 ret = s->status;
2264#ifdef DEBUG_IDE
2265 printf("ide: read addr=0x%x val=%02x\n", addr, ret);
2266#endif
2267 return ret;
2268}
2269
2270void ide_cmd_write(CPUX86State *env, uint32_t addr, uint32_t val)
2271{
2272 IDEState *s = &ide_state[0];
2273 /* common for both drives */
2274 s->cmd = val;
2275}
2276
2277void ide_data_writew(CPUX86State *env, uint32_t addr, uint32_t val)
2278{
2279 IDEState *s = ide_state[0].cur_drive;
2280 uint8_t *p;
2281
2282 p = s->data_ptr;
2283 *(uint16_t *)p = tswap16(val);
2284 p += 2;
2285 s->data_ptr = p;
2286 if (p >= s->data_end)
2287 s->end_transfer_func(s);
2288}
2289
2290uint32_t ide_data_readw(CPUX86State *env, uint32_t addr)
2291{
2292 IDEState *s = ide_state[0].cur_drive;
2293 uint8_t *p;
2294 int ret;
2295
2296 p = s->data_ptr;
2297 ret = tswap16(*(uint16_t *)p);
2298 p += 2;
2299 s->data_ptr = p;
2300 if (p >= s->data_end)
2301 s->end_transfer_func(s);
2302 return ret;
2303}
2304
2305void ide_data_writel(CPUX86State *env, uint32_t addr, uint32_t val)
2306{
2307 IDEState *s = ide_state[0].cur_drive;
2308 uint8_t *p;
2309
2310 p = s->data_ptr;
2311 *(uint32_t *)p = tswap32(val);
2312 p += 4;
2313 s->data_ptr = p;
2314 if (p >= s->data_end)
2315 s->end_transfer_func(s);
2316}
2317
2318uint32_t ide_data_readl(CPUX86State *env, uint32_t addr)
2319{
2320 IDEState *s = ide_state[0].cur_drive;
2321 uint8_t *p;
2322 int ret;
2323
2324 p = s->data_ptr;
2325 ret = tswap32(*(uint32_t *)p);
2326 p += 4;
2327 s->data_ptr = p;
2328 if (p >= s->data_end)
2329 s->end_transfer_func(s);
2330 return ret;
2331}
2332
2333void ide_reset(IDEState *s)
2334{
2335 s->mult_sectors = MAX_MULT_SECTORS;
2336 s->status = READY_STAT;
2337 s->cur_drive = s;
2338 s->select = 0xa0;
2339}
2340
2341void ide_init(void)
2342{
2343 IDEState *s;
2344 int i, cylinders;
2345 int64_t nb_sectors;
2346
2347 for(i = 0; i < MAX_DISKS; i++) {
2348 s = &ide_state[i];
2349 s->bs = bs_table[i];
2350 if (s->bs) {
2351 bdrv_get_geometry(s->bs, &nb_sectors);
2352 cylinders = nb_sectors / (16 * 63);
2353 if (cylinders > 16383)
2354 cylinders = 16383;
2355 else if (cylinders < 2)
2356 cylinders = 2;
2357 s->cylinders = cylinders;
2358 s->heads = 16;
2359 s->sectors = 63;
2360 s->nb_sectors = nb_sectors;
2361 }
2362 s->irq = 14;
2363 ide_reset(s);
2364 }
2365 register_ioport_write(0x1f0, 8, ide_ioport_write, 1);
2366 register_ioport_read(0x1f0, 8, ide_ioport_read, 1);
2367 register_ioport_read(0x3f6, 1, ide_status_read, 1);
2368 register_ioport_write(0x3f6, 1, ide_cmd_write, 1);
2369
2370 /* data ports */
2371 register_ioport_write(0x1f0, 2, ide_data_writew, 2);
2372 register_ioport_read(0x1f0, 2, ide_data_readw, 2);
2373 register_ioport_write(0x1f0, 4, ide_data_writel, 4);
2374 register_ioport_read(0x1f0, 4, ide_data_readl, 4);
2375}
2376
f1510b2c 2377/***********************************************************/
0824d6fc
FB
2378/* cpu signal handler */
2379static void host_segv_handler(int host_signum, siginfo_t *info,
2380 void *puc)
2381{
2382 if (cpu_signal_handler(host_signum, info, puc))
2383 return;
2384 term_exit();
2385 abort();
2386}
2387
2388static int timer_irq_pending;
87858c89 2389static int timer_irq_count;
0824d6fc
FB
2390
2391static void host_alarm_handler(int host_signum, siginfo_t *info,
2392 void *puc)
2393{
87858c89
FB
2394 /* NOTE: since usually the OS asks a 100 Hz clock, there can be
2395 some drift between cpu_get_ticks() and the interrupt time. So
2396 we queue some interrupts to avoid missing some */
2397 timer_irq_count += pit_get_out_edges(&pit_channels[0]);
2398 if (timer_irq_count) {
2399 if (timer_irq_count > 2)
2400 timer_irq_count = 2;
2401 timer_irq_count--;
2402 /* just exit from the cpu to have a chance to handle timers */
2403 cpu_x86_interrupt(global_env);
2404 timer_irq_pending = 1;
2405 }
0824d6fc
FB
2406}
2407
b4608c04
FB
2408/* main execution loop */
2409
2410CPUState *cpu_gdbstub_get_env(void *opaque)
2411{
2412 return global_env;
2413}
2414
2415void main_loop(void *opaque)
2416{
2417 struct pollfd ufds[2], *pf, *serial_ufd, *net_ufd, *gdb_ufd;
2418 int ret, n, timeout;
2419 uint8_t ch;
2420 CPUState *env = global_env;
2421
2422 for(;;) {
2423
2424 ret = cpu_x86_exec(env);
2425
2426 /* if hlt instruction, we wait until the next IRQ */
2427 if (ret == EXCP_HLT)
2428 timeout = 10;
2429 else
2430 timeout = 0;
2431 /* poll any events */
2432 serial_ufd = NULL;
2433 pf = ufds;
2434 if (!(serial_ports[0].lsr & UART_LSR_DR)) {
2435 serial_ufd = pf;
2436 pf->fd = 0;
2437 pf->events = POLLIN;
2438 pf++;
2439 }
2440 net_ufd = NULL;
2441 if (net_fd > 0 && ne2000_can_receive(&ne2000_state)) {
2442 net_ufd = pf;
2443 pf->fd = net_fd;
2444 pf->events = POLLIN;
2445 pf++;
2446 }
2447 gdb_ufd = NULL;
2448 if (gdbstub_fd > 0) {
2449 gdb_ufd = pf;
2450 pf->fd = gdbstub_fd;
2451 pf->events = POLLIN;
2452 pf++;
2453 }
2454
2455 ret = poll(ufds, pf - ufds, timeout);
2456 if (ret > 0) {
2457 if (serial_ufd && (serial_ufd->revents & POLLIN)) {
2458 n = read(0, &ch, 1);
2459 if (n == 1) {
2460 serial_received_byte(&serial_ports[0], ch);
2461 }
2462 }
2463 if (net_ufd && (net_ufd->revents & POLLIN)) {
2464 uint8_t buf[MAX_ETH_FRAME_SIZE];
2465
2466 n = read(net_fd, buf, MAX_ETH_FRAME_SIZE);
2467 if (n > 0) {
2468 if (n < 60) {
2469 memset(buf + n, 0, 60 - n);
2470 n = 60;
2471 }
2472 ne2000_receive(&ne2000_state, buf, n);
2473 }
2474 }
2475 if (gdb_ufd && (gdb_ufd->revents & POLLIN)) {
2476 uint8_t buf[1];
2477 /* stop emulation if requested by gdb */
2478 n = read(gdbstub_fd, buf, 1);
2479 if (n == 1)
2480 break;
2481 }
2482 }
2483
2484 /* timer IRQ */
2485 if (timer_irq_pending) {
2486 pic_set_irq(0, 1);
2487 pic_set_irq(0, 0);
2488 timer_irq_pending = 0;
2489 }
2490
2491 pic_handle_irq();
2492 }
2493}
2494
0824d6fc
FB
2495void help(void)
2496{
2497 printf("Virtual Linux version " QEMU_VERSION ", Copyright (c) 2003 Fabrice Bellard\n"
fc01f7e7 2498 "usage: vl [options] bzImage [kernel parameters...]\n"
0824d6fc
FB
2499 "\n"
2500 "'bzImage' is a Linux kernel image (PAGE_OFFSET must be defined\n"
2501 "to 0x90000000 in asm/page.h and arch/i386/vmlinux.lds)\n"
fc01f7e7
FB
2502 "\n"
2503 "General options:\n"
2504 "-initrd file use 'file' as initial ram disk\n"
2505 "-hda file use 'file' as hard disk 0 image\n"
2506 "-hdb file use 'file' as hard disk 1 image\n"
2507 "-m megs set virtual RAM size to megs MB\n"
2508 "-n script set network init script [default=%s]\n"
2509 "\n"
2510 "Debug options:\n"
2511 "-s wait gdb connection to port %d\n"
2512 "-p port change gdb connection port\n"
2513 "-d output log in /tmp/vl.log\n"
0824d6fc 2514 "\n"
f1510b2c 2515 "During emulation, use C-a h to get terminal commands:\n",
b4608c04 2516 DEFAULT_NETWORK_SCRIPT, DEFAULT_GDBSTUB_PORT);
0824d6fc
FB
2517 term_print_help();
2518 exit(1);
2519}
2520
fc01f7e7
FB
2521struct option long_options[] = {
2522 { "initrd", 1, NULL, 0, },
2523 { "hda", 1, NULL, 0, },
2524 { "hdb", 1, NULL, 0, },
2525 { NULL, 0, NULL, 0 },
2526};
2527
0824d6fc
FB
2528int main(int argc, char **argv)
2529{
fc01f7e7 2530 int c, ret, initrd_size, i, use_gdbstub, gdbstub_port, long_index;
0824d6fc
FB
2531 struct linux_params *params;
2532 struct sigaction act;
2533 struct itimerval itv;
2534 CPUX86State *env;
fc01f7e7
FB
2535 const char *tmpdir, *initrd_filename;
2536 const char *hd_filename[MAX_DISKS];
87858c89 2537
0824d6fc
FB
2538 /* we never want that malloc() uses mmap() */
2539 mallopt(M_MMAP_THRESHOLD, 4096 * 1024);
fc01f7e7
FB
2540 initrd_filename = NULL;
2541 for(i = 0; i < MAX_DISKS; i++)
2542 hd_filename[i] = NULL;
0824d6fc 2543 phys_ram_size = 32 * 1024 * 1024;
f1510b2c 2544 pstrcpy(network_script, sizeof(network_script), DEFAULT_NETWORK_SCRIPT);
b4608c04
FB
2545 use_gdbstub = 0;
2546 gdbstub_port = DEFAULT_GDBSTUB_PORT;
0824d6fc 2547 for(;;) {
fc01f7e7 2548 c = getopt_long_only(argc, argv, "hm:dn:sp:", long_options, &long_index);
0824d6fc
FB
2549 if (c == -1)
2550 break;
2551 switch(c) {
fc01f7e7
FB
2552 case 0:
2553 switch(long_index) {
2554 case 0:
2555 initrd_filename = optarg;
2556 break;
2557 case 1:
2558 hd_filename[0] = optarg;
2559 break;
2560 case 2:
2561 hd_filename[1] = optarg;
2562 break;
2563 }
2564 break;
0824d6fc
FB
2565 case 'h':
2566 help();
2567 break;
2568 case 'm':
2569 phys_ram_size = atoi(optarg) * 1024 * 1024;
2570 if (phys_ram_size <= 0)
2571 help();
2572 break;
2573 case 'd':
2574 loglevel = 1;
2575 break;
f1510b2c
FB
2576 case 'n':
2577 pstrcpy(network_script, sizeof(network_script), optarg);
2578 break;
b4608c04
FB
2579 case 's':
2580 use_gdbstub = 1;
2581 break;
2582 case 'p':
2583 gdbstub_port = atoi(optarg);
2584 break;
0824d6fc
FB
2585 }
2586 }
fc01f7e7 2587 if (optind >= argc)
0824d6fc
FB
2588 help();
2589
2590 /* init debug */
2591 if (loglevel) {
2592 logfile = fopen(DEBUG_LOGFILE, "w");
2593 if (!logfile) {
2594 perror(DEBUG_LOGFILE);
2595 _exit(1);
2596 }
2597 setvbuf(logfile, NULL, _IOLBF, 0);
2598 }
2599
fc01f7e7
FB
2600 /* open the virtual block devices */
2601 for(i = 0; i < MAX_DISKS; i++) {
2602 if (hd_filename[i]) {
2603 bs_table[i] = bdrv_open(hd_filename[i]);
2604 if (!bs_table[i]) {
2605 fprintf(stderr, "vl: could not open hard disk image '%s\n",
2606 hd_filename[i]);
2607 exit(1);
2608 }
2609 }
2610 }
2611
f1510b2c
FB
2612 /* init network tun interface */
2613 net_init();
2614
0824d6fc 2615 /* init the memory */
87858c89
FB
2616 tmpdir = getenv("VLTMPDIR");
2617 if (!tmpdir)
2618 tmpdir = "/tmp";
2619 snprintf(phys_ram_file, sizeof(phys_ram_file), "%s/vlXXXXXX", tmpdir);
0824d6fc 2620 if (mkstemp(phys_ram_file) < 0) {
87858c89
FB
2621 fprintf(stderr, "Could not create temporary memory file '%s'\n",
2622 phys_ram_file);
0824d6fc
FB
2623 exit(1);
2624 }
2625 phys_ram_fd = open(phys_ram_file, O_CREAT | O_TRUNC | O_RDWR, 0600);
2626 if (phys_ram_fd < 0) {
87858c89
FB
2627 fprintf(stderr, "Could not open temporary memory file '%s'\n",
2628 phys_ram_file);
0824d6fc
FB
2629 exit(1);
2630 }
2631 ftruncate(phys_ram_fd, phys_ram_size);
2632 unlink(phys_ram_file);
2633 phys_ram_base = mmap((void *)PHYS_RAM_BASE, phys_ram_size,
2634 PROT_WRITE | PROT_READ, MAP_SHARED | MAP_FIXED,
2635 phys_ram_fd, 0);
2636 if (phys_ram_base == MAP_FAILED) {
2637 fprintf(stderr, "Could not map physical memory\n");
2638 exit(1);
2639 }
2640
2641 /* now we can load the kernel */
2642 ret = load_kernel(argv[optind], phys_ram_base + KERNEL_LOAD_ADDR);
2643 if (ret < 0) {
fc01f7e7 2644 fprintf(stderr, "vl: could not load kernel '%s'\n", argv[optind]);
0824d6fc
FB
2645 exit(1);
2646 }
2647
2648 /* load initrd */
fc01f7e7
FB
2649 initrd_size = 0;
2650 if (initrd_filename) {
2651 initrd_size = load_image(initrd_filename, phys_ram_base + INITRD_LOAD_ADDR);
2652 if (initrd_size < 0) {
2653 fprintf(stderr, "vl: could not load initial ram disk '%s'\n",
2654 initrd_filename);
2655 exit(1);
2656 }
0824d6fc
FB
2657 }
2658
2659 /* init kernel params */
2660 params = (void *)(phys_ram_base + KERNEL_PARAMS_ADDR);
2661 memset(params, 0, sizeof(struct linux_params));
2662 params->mount_root_rdonly = 0;
2663 params->cl_magic = 0xA33F;
2664 params->cl_offset = params->commandline - (uint8_t *)params;
2665 params->ext_mem_k = (phys_ram_size / 1024) - 1024;
fc01f7e7
FB
2666 for(i = optind + 1; i < argc; i++) {
2667 if (i != optind + 1)
0824d6fc
FB
2668 pstrcat(params->commandline, sizeof(params->commandline), " ");
2669 pstrcat(params->commandline, sizeof(params->commandline), argv[i]);
2670 }
2671 params->loader_type = 0x01;
2672 if (initrd_size > 0) {
2673 params->initrd_start = INITRD_LOAD_ADDR;
2674 params->initrd_size = initrd_size;
2675 }
2676 params->orig_video_lines = 25;
2677 params->orig_video_cols = 80;
2678
2679 /* init basic PC hardware */
2680 init_ioports();
fc01f7e7 2681 register_ioport_write(0x80, 1, ioport80_write, 1);
0824d6fc 2682
fc01f7e7 2683 register_ioport_write(0x3d4, 2, vga_ioport_write, 1);
0824d6fc
FB
2684
2685 cmos_init();
2686 pic_init();
2687 pit_init();
2688 serial_init();
f1510b2c 2689 ne2000_init();
fc01f7e7 2690 ide_init();
0824d6fc
FB
2691
2692 /* setup cpu signal handlers for MMU / self modifying code handling */
2693 sigfillset(&act.sa_mask);
2694 act.sa_flags = SA_SIGINFO;
2695 act.sa_sigaction = host_segv_handler;
2696 sigaction(SIGSEGV, &act, NULL);
2697 sigaction(SIGBUS, &act, NULL);
2698
2699 act.sa_sigaction = host_alarm_handler;
2700 sigaction(SIGALRM, &act, NULL);
2701
2702 /* init CPU state */
2703 env = cpu_init();
2704 global_env = env;
1df912cf 2705 cpu_single_env = env;
0824d6fc
FB
2706
2707 /* setup basic memory access */
2708 env->cr[0] = 0x00000033;
2709 cpu_x86_init_mmu(env);
2710
2711 memset(params->idt_table, 0, sizeof(params->idt_table));
2712
2713 params->gdt_table[2] = 0x00cf9a000000ffffLL; /* KERNEL_CS */
2714 params->gdt_table[3] = 0x00cf92000000ffffLL; /* KERNEL_DS */
2715
2716 env->idt.base = (void *)params->idt_table;
2717 env->idt.limit = sizeof(params->idt_table) - 1;
2718 env->gdt.base = (void *)params->gdt_table;
2719 env->gdt.limit = sizeof(params->gdt_table) - 1;
2720
2721 cpu_x86_load_seg(env, R_CS, KERNEL_CS);
2722 cpu_x86_load_seg(env, R_DS, KERNEL_DS);
2723 cpu_x86_load_seg(env, R_ES, KERNEL_DS);
2724 cpu_x86_load_seg(env, R_SS, KERNEL_DS);
2725 cpu_x86_load_seg(env, R_FS, KERNEL_DS);
2726 cpu_x86_load_seg(env, R_GS, KERNEL_DS);
2727
2728 env->eip = KERNEL_LOAD_ADDR;
2729 env->regs[R_ESI] = KERNEL_PARAMS_ADDR;
2730 env->eflags = 0x2;
2731
2732 itv.it_interval.tv_sec = 0;
87858c89 2733 itv.it_interval.tv_usec = 1000;
0824d6fc
FB
2734 itv.it_value.tv_sec = 0;
2735 itv.it_value.tv_usec = 10 * 1000;
2736 setitimer(ITIMER_REAL, &itv, NULL);
87858c89
FB
2737 /* we probe the tick duration of the kernel to inform the user if
2738 the emulated kernel requested a too high timer frequency */
2739 getitimer(ITIMER_REAL, &itv);
2740 pit_min_timer_count = ((uint64_t)itv.it_interval.tv_usec * PIT_FREQ) /
2741 1000000;
b4608c04
FB
2742
2743 if (use_gdbstub) {
2744 cpu_gdbstub(NULL, main_loop, gdbstub_port);
2745 } else {
2746 main_loop(NULL);
0824d6fc 2747 }
0824d6fc
FB
2748 return 0;
2749}