vl.c 102 KB
Newer Older
1
/*
bellard's avatar
bellard committed
2
 * QEMU PC System Emulator
3
 * 
bellard's avatar
bellard committed
4
 * Copyright (c) 2003 Fabrice Bellard
5
 * 
bellard's avatar
bellard committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
23 24 25
 */
#include <stdlib.h>
#include <stdio.h>
bellard's avatar
bellard committed
26
#include <stdarg.h>
27
#include <string.h>
28
#include <ctype.h>
29 30 31 32 33 34 35 36 37 38 39 40
#include <getopt.h>
#include <inttypes.h>
#include <unistd.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <signal.h>
#include <time.h>
#include <sys/time.h>
#include <malloc.h>
#include <termios.h>
#include <sys/poll.h>
#include <errno.h>
bellard's avatar
bellard committed
41 42 43 44 45 46
#include <sys/wait.h>

#include <sys/ioctl.h>
#include <sys/socket.h>
#include <linux/if.h>
#include <linux/if_tun.h>
47

48
#include "cpu.h"
49
#include "disas.h"
bellard's avatar
bellard committed
50 51 52
#include "thunk.h"

#include "vl.h"
53

54
#define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup"
55 56
#define BIOS_FILENAME "bios.bin"
#define VGABIOS_FILENAME "vgabios.bin"
bellard's avatar
bellard committed
57

58
//#define DEBUG_UNUSED_IOPORT
59

60
//#define DEBUG_IRQ_LATENCY
61

62 63 64
/* output Bochs bios info messages */
//#define DEBUG_BIOS

65 66
//#define DEBUG_CMOS

67 68 69 70 71 72 73 74 75
/* debug PIC */
//#define DEBUG_PIC

/* debug NE2000 card */
//#define DEBUG_NE2000

/* debug PC keyboard */
//#define DEBUG_KBD

76 77 78
/* debug PC keyboard : only mouse */
//#define DEBUG_MOUSE

79 80
//#define DEBUG_SERIAL

bellard's avatar
bellard committed
81
#define PHYS_RAM_BASE     0xac000000
82
#if !defined(CONFIG_SOFTMMU)
bellard's avatar
bellard committed
83
#define PHYS_RAM_MAX_SIZE (256 * 1024 * 1024)
84 85 86
#else
#define PHYS_RAM_MAX_SIZE (2047 * 1024 * 1024)
#endif
bellard's avatar
bellard committed
87

88
#if defined (TARGET_I386)
89
#define KERNEL_LOAD_ADDR   0x00100000
90 91
#elif defined (TARGET_PPC)
//#define USE_OPEN_FIRMWARE
bellard's avatar
bellard committed
92
#if !defined (USE_OPEN_FIRMWARE)
93 94 95 96 97 98 99
#define KERNEL_LOAD_ADDR    0x01000000
#define KERNEL_STACK_ADDR   0x01200000
#else
#define KERNEL_LOAD_ADDR    0x00000000
#define KERNEL_STACK_ADDR   0x00400000
#endif
#endif
100 101 102
#define INITRD_LOAD_ADDR   0x00400000
#define KERNEL_PARAMS_ADDR 0x00090000

103 104
#define GUI_REFRESH_INTERVAL 30 

105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
/* from plex86 (BSD license) */
struct  __attribute__ ((packed)) linux_params {
  // For 0x00..0x3f, see 'struct screen_info' in linux/include/linux/tty.h.
  // I just padded out the VESA parts, rather than define them.

  /* 0x000 */ uint8_t   orig_x;
  /* 0x001 */ uint8_t   orig_y;
  /* 0x002 */ uint16_t  ext_mem_k;
  /* 0x004 */ uint16_t  orig_video_page;
  /* 0x006 */ uint8_t   orig_video_mode;
  /* 0x007 */ uint8_t   orig_video_cols;
  /* 0x008 */ uint16_t  unused1;
  /* 0x00a */ uint16_t  orig_video_ega_bx;
  /* 0x00c */ uint16_t  unused2;
  /* 0x00e */ uint8_t   orig_video_lines;
  /* 0x00f */ uint8_t   orig_video_isVGA;
  /* 0x010 */ uint16_t  orig_video_points;
  /* 0x012 */ uint8_t   pad0[0x20 - 0x12]; // VESA info.
  /* 0x020 */ uint16_t  cl_magic;  // Commandline magic number (0xA33F)
  /* 0x022 */ uint16_t  cl_offset; // Commandline offset.  Address of commandline
                                 // is calculated as 0x90000 + cl_offset, bu
                                 // only if cl_magic == 0xA33F.
  /* 0x024 */ uint8_t   pad1[0x40 - 0x24]; // VESA info.

  /* 0x040 */ uint8_t   apm_bios_info[20]; // struct apm_bios_info
  /* 0x054 */ uint8_t   pad2[0x80 - 0x54];

  // Following 2 from 'struct drive_info_struct' in drivers/block/cciss.h.
  // Might be truncated?
  /* 0x080 */ uint8_t   hd0_info[16]; // hd0-disk-parameter from intvector 0x41
  /* 0x090 */ uint8_t   hd1_info[16]; // hd1-disk-parameter from intvector 0x46

  // System description table truncated to 16 bytes
  // From 'struct sys_desc_table_struct' in linux/arch/i386/kernel/setup.c.
  /* 0x0a0 */ uint16_t  sys_description_len;
  /* 0x0a2 */ uint8_t   sys_description_table[14];
                        // [0] machine id
                        // [1] machine submodel id
                        // [2] BIOS revision
                        // [3] bit1: MCA bus

  /* 0x0b0 */ uint8_t   pad3[0x1e0 - 0xb0];
  /* 0x1e0 */ uint32_t  alt_mem_k;
  /* 0x1e4 */ uint8_t   pad4[4];
  /* 0x1e8 */ uint8_t   e820map_entries;
  /* 0x1e9 */ uint8_t   eddbuf_entries; // EDD_NR
  /* 0x1ea */ uint8_t   pad5[0x1f1 - 0x1ea];
  /* 0x1f1 */ uint8_t   setup_sects; // size of setup.S, number of sectors
  /* 0x1f2 */ uint16_t  mount_root_rdonly; // MOUNT_ROOT_RDONLY (if !=0)
  /* 0x1f4 */ uint16_t  sys_size; // size of compressed kernel-part in the
                                // (b)zImage-file (in 16 byte units, rounded up)
  /* 0x1f6 */ uint16_t  swap_dev; // (unused AFAIK)
  /* 0x1f8 */ uint16_t  ramdisk_flags;
  /* 0x1fa */ uint16_t  vga_mode; // (old one)
  /* 0x1fc */ uint16_t  orig_root_dev; // (high=Major, low=minor)
  /* 0x1fe */ uint8_t   pad6[1];
  /* 0x1ff */ uint8_t   aux_device_info;
  /* 0x200 */ uint16_t  jump_setup; // Jump to start of setup code,
                                  // aka "reserved" field.
  /* 0x202 */ uint8_t   setup_signature[4]; // Signature for SETUP-header, ="HdrS"
  /* 0x206 */ uint16_t  header_format_version; // Version number of header format;
  /* 0x208 */ uint8_t   setup_S_temp0[8]; // Used by setup.S for communication with
                                        // boot loaders, look there.
  /* 0x210 */ uint8_t   loader_type;
                        // 0 for old one.
                        // else 0xTV:
                        //   T=0: LILO
                        //   T=1: Loadlin
                        //   T=2: bootsect-loader
                        //   T=3: SYSLINUX
                        //   T=4: ETHERBOOT
                        //   V=version
  /* 0x211 */ uint8_t   loadflags;
                        // bit0 = 1: kernel is loaded high (bzImage)
                        // bit7 = 1: Heap and pointer (see below) set by boot
                        //   loader.
  /* 0x212 */ uint16_t  setup_S_temp1;
  /* 0x214 */ uint32_t  kernel_start;
  /* 0x218 */ uint32_t  initrd_start;
  /* 0x21c */ uint32_t  initrd_size;
  /* 0x220 */ uint8_t   setup_S_temp2[4];
  /* 0x224 */ uint16_t  setup_S_heap_end_pointer;
  /* 0x226 */ uint8_t   pad7[0x2d0 - 0x226];

  /* 0x2d0 : Int 15, ax=e820 memory map. */
  // (linux/include/asm-i386/e820.h, 'struct e820entry')
#define E820MAX  32
#define E820_RAM  1
#define E820_RESERVED 2
#define E820_ACPI 3 /* usable as RAM once ACPI tables have been read */
#define E820_NVS  4
  struct {
    uint64_t addr;
    uint64_t size;
    uint32_t type;
    } e820map[E820MAX];

  /* 0x550 */ uint8_t   pad8[0x600 - 0x550];

  // BIOS Enhanced Disk Drive Services.
  // (From linux/include/asm-i386/edd.h, 'struct edd_info')
  // Each 'struct edd_info is 78 bytes, times a max of 6 structs in array.
  /* 0x600 */ uint8_t   eddbuf[0x7d4 - 0x600];

  /* 0x7d4 */ uint8_t   pad9[0x800 - 0x7d4];
  /* 0x800 */ uint8_t   commandline[0x800];

  /* 0x1000 */
  uint64_t gdt_table[256];
  uint64_t idt_table[48];
};

#define KERNEL_CS     0x10
#define KERNEL_DS     0x18

220 221
/* XXX: use a two level table to limit memory usage */
#define MAX_IOPORTS 65536
222

223
static const char *bios_dir = CONFIG_QEMU_SHAREDIR;
224
char phys_ram_file[1024];
225 226
CPUState *global_env;
CPUState *cpu_single_env;
bellard's avatar
bellard committed
227 228
IOPortReadFunc *ioport_read_table[3][MAX_IOPORTS];
IOPortWriteFunc *ioport_write_table[3][MAX_IOPORTS];
229
BlockDriverState *bs_table[MAX_DISKS], *fd_table[MAX_FD];
230 231
int vga_ram_size;
static DisplayState display_state;
232
int nographic;
233 234
int term_inited;
int64_t ticks_per_sec;
235
int boot_device = 'c';
236 237 238 239

/***********************************************************/
/* x86 io ports */

240
uint32_t default_ioport_readb(CPUState *env, uint32_t address)
241 242 243 244
{
#ifdef DEBUG_UNUSED_IOPORT
    fprintf(stderr, "inb: port=0x%04x\n", address);
#endif
bellard's avatar
bellard committed
245
    return 0xff;
246 247
}

248
void default_ioport_writeb(CPUState *env, uint32_t address, uint32_t data)
249 250 251 252 253 254 255
{
#ifdef DEBUG_UNUSED_IOPORT
    fprintf(stderr, "outb: port=0x%04x data=0x%02x\n", address, data);
#endif
}

/* default is to make two byte accesses */
256
uint32_t default_ioport_readw(CPUState *env, uint32_t address)
257 258
{
    uint32_t data;
259 260
    data = ioport_read_table[0][address & (MAX_IOPORTS - 1)](env, address);
    data |= ioport_read_table[0][(address + 1) & (MAX_IOPORTS - 1)](env, address + 1) << 8;
261 262 263
    return data;
}

264
void default_ioport_writew(CPUState *env, uint32_t address, uint32_t data)
265
{
266 267
    ioport_write_table[0][address & (MAX_IOPORTS - 1)](env, address, data & 0xff);
    ioport_write_table[0][(address + 1) & (MAX_IOPORTS - 1)](env, address + 1, (data >> 8) & 0xff);
268 269
}

270
uint32_t default_ioport_readl(CPUState *env, uint32_t address)
271
{
bellard's avatar
bellard committed
272 273 274 275
#ifdef DEBUG_UNUSED_IOPORT
    fprintf(stderr, "inl: port=0x%04x\n", address);
#endif
    return 0xffffffff;
276 277
}

278
void default_ioport_writel(CPUState *env, uint32_t address, uint32_t data)
279
{
bellard's avatar
bellard committed
280 281 282
#ifdef DEBUG_UNUSED_IOPORT
    fprintf(stderr, "outl: port=0x%04x data=0x%02x\n", address, data);
#endif
283 284
}

bellard's avatar
bellard committed
285
void init_ioports(void)
286 287 288
{
    int i;

bellard's avatar
bellard committed
289 290 291 292 293 294 295 296
    for(i = 0; i < MAX_IOPORTS; i++) {
        ioport_read_table[0][i] = default_ioport_readb;
        ioport_write_table[0][i] = default_ioport_writeb;
        ioport_read_table[1][i] = default_ioport_readw;
        ioport_write_table[1][i] = default_ioport_writew;
        ioport_read_table[2][i] = default_ioport_readl;
        ioport_write_table[2][i] = default_ioport_writel;
    }
297 298
}

bellard's avatar
bellard committed
299 300
/* size is the word size in byte */
int register_ioport_read(int start, int length, IOPortReadFunc *func, int size)
bellard's avatar
bellard committed
301
{
bellard's avatar
bellard committed
302
    int i, bsize;
bellard's avatar
bellard committed
303

bellard's avatar
bellard committed
304 305 306 307 308 309 310 311 312 313
    if (size == 1)
        bsize = 0;
    else if (size == 2)
        bsize = 1;
    else if (size == 4)
        bsize = 2;
    else
        return -1;
    for(i = start; i < start + length; i += size)
        ioport_read_table[bsize][i] = func;
bellard's avatar
bellard committed
314 315 316
    return 0;
}

bellard's avatar
bellard committed
317 318
/* size is the word size in byte */
int register_ioport_write(int start, int length, IOPortWriteFunc *func, int size)
bellard's avatar
bellard committed
319
{
bellard's avatar
bellard committed
320
    int i, bsize;
bellard's avatar
bellard committed
321

bellard's avatar
bellard committed
322 323 324 325 326 327 328 329 330 331
    if (size == 1)
        bsize = 0;
    else if (size == 2)
        bsize = 1;
    else if (size == 4)
        bsize = 2;
    else
        return -1;
    for(i = start; i < start + length; i += size)
        ioport_write_table[bsize][i] = func;
bellard's avatar
bellard committed
332 333 334
    return 0;
}

335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
void pstrcpy(char *buf, int buf_size, const char *str)
{
    int c;
    char *q = buf;

    if (buf_size <= 0)
        return;

    for(;;) {
        c = *str++;
        if (c == 0 || q >= buf + buf_size - 1)
            break;
        *q++ = c;
    }
    *q = '\0';
}

/* strcat and truncate. */
char *pstrcat(char *buf, int buf_size, const char *s)
{
    int len;
    len = strlen(buf);
    if (len < buf_size) 
        pstrcpy(buf + len, buf_size - len, s);
    return buf;
}

int load_kernel(const char *filename, uint8_t *addr)
{
364 365 366
    int fd, size;
#if defined (TARGET_I386)
    int setup_sects;
367
    uint8_t bootsect[512];
368
#endif
369

370 371
    printf("Load kernel at %p (0x%08x)\n", addr,
           (uint32_t)addr - (uint32_t)phys_ram_base);
372 373 374
    fd = open(filename, O_RDONLY);
    if (fd < 0)
        return -1;
375
#if defined (TARGET_I386)
376 377 378 379 380 381 382
    if (read(fd, bootsect, 512) != 512)
        goto fail;
    setup_sects = bootsect[0x1F1];
    if (!setup_sects)
        setup_sects = 4;
    /* skip 16 bit setup code */
    lseek(fd, (setup_sects + 1) * 512, SEEK_SET);
383
#endif
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
    size = read(fd, addr, 16 * 1024 * 1024);
    if (size < 0)
        goto fail;
    close(fd);
    return size;
 fail:
    close(fd);
    return -1;
}

/* return the size or -1 if error */
int load_image(const char *filename, uint8_t *addr)
{
    int fd, size;
    fd = open(filename, O_RDONLY);
    if (fd < 0)
        return -1;
    size = lseek(fd, 0, SEEK_END);
    lseek(fd, 0, SEEK_SET);
    if (read(fd, addr, size) != size) {
        close(fd);
        return -1;
    }
    close(fd);
    return size;
}

411
void cpu_outb(CPUState *env, int addr, int val)
412
{
bellard's avatar
bellard committed
413
    ioport_write_table[0][addr & (MAX_IOPORTS - 1)](env, addr, val);
414 415
}

416
void cpu_outw(CPUState *env, int addr, int val)
417
{
bellard's avatar
bellard committed
418
    ioport_write_table[1][addr & (MAX_IOPORTS - 1)](env, addr, val);
419 420
}

421
void cpu_outl(CPUState *env, int addr, int val)
422
{
bellard's avatar
bellard committed
423
    ioport_write_table[2][addr & (MAX_IOPORTS - 1)](env, addr, val);
424 425
}

426
int cpu_inb(CPUState *env, int addr)
427
{
bellard's avatar
bellard committed
428
    return ioport_read_table[0][addr & (MAX_IOPORTS - 1)](env, addr);
429 430
}

431
int cpu_inw(CPUState *env, int addr)
432
{
bellard's avatar
bellard committed
433
    return ioport_read_table[1][addr & (MAX_IOPORTS - 1)](env, addr);
434 435
}

436
int cpu_inl(CPUState *env, int addr)
437
{
bellard's avatar
bellard committed
438
    return ioport_read_table[2][addr & (MAX_IOPORTS - 1)](env, addr);
439 440 441
}

/***********************************************************/
442
void ioport80_write(CPUState *env, uint32_t addr, uint32_t data)
443 444 445 446 447 448 449 450 451 452 453 454 455
{
}

void hw_error(const char *fmt, ...)
{
    va_list ap;

    va_start(ap, fmt);
    fprintf(stderr, "qemu: hardware error: ");
    vfprintf(stderr, fmt, ap);
    fprintf(stderr, "\n");
#ifdef TARGET_I386
    cpu_x86_dump_state(global_env, stderr, X86_DUMP_FPU | X86_DUMP_CCOP);
456 457
#else
    cpu_dump_state(global_env, stderr, 0);
458 459 460 461 462 463 464 465
#endif
    va_end(ap);
    abort();
}

/***********************************************************/
/* cmos emulation */

466
#if defined (TARGET_I386)
467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486
#define RTC_SECONDS             0
#define RTC_SECONDS_ALARM       1
#define RTC_MINUTES             2
#define RTC_MINUTES_ALARM       3
#define RTC_HOURS               4
#define RTC_HOURS_ALARM         5
#define RTC_ALARM_DONT_CARE    0xC0

#define RTC_DAY_OF_WEEK         6
#define RTC_DAY_OF_MONTH        7
#define RTC_MONTH               8
#define RTC_YEAR                9

#define RTC_REG_A               10
#define RTC_REG_B               11
#define RTC_REG_C               12
#define RTC_REG_D               13

/* PC cmos mappings */
#define REG_EQUIPMENT_BYTE          0x14
487
#define REG_IBM_CENTURY_BYTE        0x32
488 489 490 491

uint8_t cmos_data[128];
uint8_t cmos_index;

492
void cmos_ioport_write(CPUState *env, uint32_t addr, uint32_t data)
493 494 495
{
    if (addr == 0x70) {
        cmos_index = data & 0x7f;
496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528
    } else {
#ifdef DEBUG_CMOS
        printf("cmos: write index=0x%02x val=0x%02x\n",
               cmos_index, data);
#endif        
        switch(addr) {
        case RTC_SECONDS_ALARM:
        case RTC_MINUTES_ALARM:
        case RTC_HOURS_ALARM:
            /* XXX: not supported */
            cmos_data[cmos_index] = data;
            break;
        case RTC_SECONDS:
        case RTC_MINUTES:
        case RTC_HOURS:
        case RTC_DAY_OF_WEEK:
        case RTC_DAY_OF_MONTH:
        case RTC_MONTH:
        case RTC_YEAR:
            cmos_data[cmos_index] = data;
            break;
        case RTC_REG_A:
        case RTC_REG_B:
            cmos_data[cmos_index] = data;
            break;
        case RTC_REG_C:
        case RTC_REG_D:
            /* cannot write to them */
            break;
        default:
            cmos_data[cmos_index] = data;
            break;
        }
529 530 531
    }
}

532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553
static inline int to_bcd(int a)
{
    return ((a / 10) << 4) | (a % 10);
}

static void cmos_update_time(void)
{
    struct tm *tm;
    time_t ti;

    ti = time(NULL);
    tm = gmtime(&ti);
    cmos_data[RTC_SECONDS] = to_bcd(tm->tm_sec);
    cmos_data[RTC_MINUTES] = to_bcd(tm->tm_min);
    cmos_data[RTC_HOURS] = to_bcd(tm->tm_hour);
    cmos_data[RTC_DAY_OF_WEEK] = to_bcd(tm->tm_wday);
    cmos_data[RTC_DAY_OF_MONTH] = to_bcd(tm->tm_mday);
    cmos_data[RTC_MONTH] = to_bcd(tm->tm_mon + 1);
    cmos_data[RTC_YEAR] = to_bcd(tm->tm_year % 100);
    cmos_data[REG_IBM_CENTURY_BYTE] = to_bcd((tm->tm_year / 100) + 19);
}

554
uint32_t cmos_ioport_read(CPUState *env, uint32_t addr)
555 556 557 558 559 560
{
    int ret;

    if (addr == 0x70) {
        return 0xff;
    } else {
561
        switch(cmos_index) {
562 563 564 565 566 567 568 569 570 571 572
        case RTC_SECONDS:
        case RTC_MINUTES:
        case RTC_HOURS:
        case RTC_DAY_OF_WEEK:
        case RTC_DAY_OF_MONTH:
        case RTC_MONTH:
        case RTC_YEAR:
        case REG_IBM_CENTURY_BYTE:
            cmos_update_time();
            ret = cmos_data[cmos_index];
            break;
573
        case RTC_REG_A:
574
            ret = cmos_data[cmos_index];
575 576
            /* toggle update-in-progress bit for Linux (same hack as
               plex86) */
577
            cmos_data[RTC_REG_A] ^= 0x80; 
578 579
            break;
        case RTC_REG_C:
580
            ret = cmos_data[cmos_index];
581
            pic_set_irq(8, 0);
582
            cmos_data[RTC_REG_C] = 0x00; 
583
            break;
584 585 586
        default:
            ret = cmos_data[cmos_index];
            break;
587 588 589 590 591
        }
#ifdef DEBUG_CMOS
        printf("cmos: read index=0x%02x val=0x%02x\n",
               cmos_index, ret);
#endif
592 593 594 595 596 597
        return ret;
    }
}

void cmos_init(void)
{
598
    int val;
599

600
    cmos_update_time();
601 602 603 604 605 606

    cmos_data[RTC_REG_A] = 0x26;
    cmos_data[RTC_REG_B] = 0x02;
    cmos_data[RTC_REG_C] = 0x00;
    cmos_data[RTC_REG_D] = 0x80;

607 608
    /* various important CMOS locations needed by PC/Bochs bios */

609
    cmos_data[REG_EQUIPMENT_BYTE] = 0x02; /* FPU is there */
610
    cmos_data[REG_EQUIPMENT_BYTE] |= 0x04; /* PS/2 mouse installed */
611

612 613 614 615 616 617 618 619 620 621 622 623 624 625 626
    /* memory size */
    val = (phys_ram_size / 1024) - 1024;
    if (val > 65535)
        val = 65535;
    cmos_data[0x17] = val;
    cmos_data[0x18] = val >> 8;
    cmos_data[0x30] = val;
    cmos_data[0x31] = val >> 8;

    val = (phys_ram_size / 65536) - ((16 * 1024 * 1024) / 65536);
    if (val > 65535)
        val = 65535;
    cmos_data[0x34] = val;
    cmos_data[0x35] = val >> 8;
    
627 628
    switch(boot_device) {
    case 'a':
629
    case 'b':
630 631 632 633 634 635 636 637 638 639 640
        cmos_data[0x3d] = 0x01; /* floppy boot */
        break;
    default:
    case 'c':
        cmos_data[0x3d] = 0x02; /* hard drive boot */
        break;
    case 'd':
        cmos_data[0x3d] = 0x03; /* CD-ROM boot */
        break;
    }

bellard's avatar
bellard committed
641 642
    register_ioport_write(0x70, 2, cmos_ioport_write, 1);
    register_ioport_read(0x70, 2, cmos_ioport_read, 1);
643 644
}

645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694
void cmos_register_fd (uint8_t fd0, uint8_t fd1)
{
    int nb = 0;

    cmos_data[0x10] = 0;
    switch (fd0) {
    case 0:
        /* 1.44 Mb 3"5 drive */
        cmos_data[0x10] |= 0x40;
        break;
    case 1:
        /* 2.88 Mb 3"5 drive */
        cmos_data[0x10] |= 0x60;
        break;
    case 2:
        /* 1.2 Mb 5"5 drive */
        cmos_data[0x10] |= 0x20;
        break;
    }
    switch (fd1) {
    case 0:
        /* 1.44 Mb 3"5 drive */
        cmos_data[0x10] |= 0x04;
        break;
    case 1:
        /* 2.88 Mb 3"5 drive */
        cmos_data[0x10] |= 0x06;
        break;
    case 2:
        /* 1.2 Mb 5"5 drive */
        cmos_data[0x10] |= 0x02;
        break;
    }
    if (fd0 < 3)
        nb++;
    if (fd1 < 3)
        nb++;
    switch (nb) {
    case 0:
        break;
    case 1:
        cmos_data[REG_EQUIPMENT_BYTE] |= 0x01; /* 1 drive, ready for boot */
        break;
    case 2:
        cmos_data[REG_EQUIPMENT_BYTE] |= 0x41; /* 2 drives, ready for boot */
        break;
    }
}
#endif /* TARGET_I386 */

695 696 697 698 699 700 701 702
/***********************************************************/
/* 8259 pic emulation */

typedef struct PicState {
    uint8_t last_irr; /* edge detection */
    uint8_t irr; /* interrupt request register */
    uint8_t imr; /* interrupt mask register */
    uint8_t isr; /* interrupt service register */
703
    uint8_t priority_add; /* highest irq priority */
704 705
    uint8_t irq_base;
    uint8_t read_reg_select;
706
    uint8_t poll;
707 708 709
    uint8_t special_mask;
    uint8_t init_state;
    uint8_t auto_eoi;
710 711
    uint8_t rotate_on_auto_eoi;
    uint8_t special_fully_nested_mode;
712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732
    uint8_t init4; /* true if 4 byte init */
} PicState;

/* 0 is master pic, 1 is slave pic */
PicState pics[2];
int pic_irq_requested;

/* set irq level. If an edge is detected, then the IRR is set to 1 */
static inline void pic_set_irq1(PicState *s, int irq, int level)
{
    int mask;
    mask = 1 << irq;
    if (level) {
        if ((s->last_irr & mask) == 0)
            s->irr |= mask;
        s->last_irr |= mask;
    } else {
        s->last_irr &= ~mask;
    }
}

733 734
/* return the highest priority found in mask (highest = smallest
   number). Return 8 if no irq */
735 736 737 738
static inline int get_priority(PicState *s, int mask)
{
    int priority;
    if (mask == 0)
739 740
        return 8;
    priority = 0;
741
    while ((mask & (1 << ((priority + s->priority_add) & 7))) == 0)
742
        priority++;
743 744 745 746 747 748 749 750 751 752
    return priority;
}

/* return the pic wanted interrupt. return -1 if none */
static int pic_get_irq(PicState *s)
{
    int mask, cur_priority, priority;

    mask = s->irr & ~s->imr;
    priority = get_priority(s, mask);
753
    if (priority == 8)
754
        return -1;
755 756 757 758 759 760 761 762
    /* compute current priority. If special fully nested mode on the
       master, the IRQ coming from the slave is not taken into account
       for the priority computation. */
    mask = s->isr;
    if (s->special_fully_nested_mode && s == &pics[0])
        mask &= ~(1 << 2);
    cur_priority = get_priority(s, mask);
    if (priority < cur_priority) {
763
        /* higher priority found: an irq should be generated */
764
        return (priority + s->priority_add) & 7;
765 766 767 768 769
    } else {
        return -1;
    }
}

770 771
/* raise irq to CPU if necessary. must be called every time the active
   irq may change */
772
void pic_update_irq(void)
773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792
{
    int irq2, irq;

    /* first look at slave pic */
    irq2 = pic_get_irq(&pics[1]);
    if (irq2 >= 0) {
        /* if irq request by slave pic, signal master PIC */
        pic_set_irq1(&pics[0], 2, 1);
        pic_set_irq1(&pics[0], 2, 0);
    }
    /* look at requested irq */
    irq = pic_get_irq(&pics[0]);
    if (irq >= 0) {
        if (irq == 2) {
            /* from slave pic */
            pic_irq_requested = 8 + irq2;
        } else {
            /* from master pic */
            pic_irq_requested = irq;
        }
793 794 795 796 797 798 799 800 801 802 803
#if defined(DEBUG_PIC)
        {
            int i;
            for(i = 0; i < 2; i++) {
                printf("pic%d: imr=%x irr=%x padd=%d\n", 
                       i, pics[i].imr, pics[i].irr, pics[i].priority_add);
                
            }
        }
        printf("pic: cpu_interrupt req=%d\n", pic_irq_requested);
#endif
804
        cpu_interrupt(global_env, CPU_INTERRUPT_HARD);
805 806 807
    }
}

808 809 810 811
#ifdef DEBUG_IRQ_LATENCY
int64_t irq_time[16];
int64_t cpu_get_ticks(void);
#endif
812
#if defined(DEBUG_PIC)
bellard's avatar
bellard committed
813 814
int irq_level[16];
#endif
815 816 817

void pic_set_irq(int irq, int level)
{
818
#if defined(DEBUG_PIC)
bellard's avatar
bellard committed
819 820 821 822 823
    if (level != irq_level[irq]) {
        printf("pic_set_irq: irq=%d level=%d\n", irq, level);
        irq_level[irq] = level;
    }
#endif
824 825 826 827 828 829 830 831 832
#ifdef DEBUG_IRQ_LATENCY
    if (level) {
        irq_time[irq] = cpu_get_ticks();
    }
#endif
    pic_set_irq1(&pics[irq >> 3], irq & 7, level);
    pic_update_irq();
}

833 834 835 836 837 838 839 840 841 842 843 844
/* acknowledge interrupt 'irq' */
static inline void pic_intack(PicState *s, int irq)
{
    if (s->auto_eoi) {
        if (s->rotate_on_auto_eoi)
            s->priority_add = (irq + 1) & 7;
    } else {
        s->isr |= (1 << irq);
    }
    s->irr &= ~(1 << irq);
}

845
int cpu_x86_get_pic_interrupt(CPUState *env)
846 847 848 849 850
{
    int irq, irq2, intno;

    /* signal the pic that the irq was acked by the CPU */
    irq = pic_irq_requested;
851
#ifdef DEBUG_IRQ_LATENCY
852 853 854
    printf("IRQ%d latency=%0.3fus\n", 
           irq, 
           (double)(cpu_get_ticks() - irq_time[irq]) * 1000000.0 / ticks_per_sec);
855
#endif
856
#if defined(DEBUG_PIC)
bellard's avatar
bellard committed
857 858
    printf("pic_interrupt: irq=%d\n", irq);
#endif
859

860 861
    if (irq >= 8) {
        irq2 = irq & 7;
862
        pic_intack(&pics[1], irq2);
863 864 865 866 867
        irq = 2;
        intno = pics[1].irq_base + irq2;
    } else {
        intno = pics[0].irq_base + irq;
    }
868
    pic_intack(&pics[0], irq);
869 870 871
    return intno;
}

872
void pic_ioport_write(CPUState *env, uint32_t addr, uint32_t val)
873 874
{
    PicState *s;
875
    int priority, cmd, irq;
876

bellard's avatar
bellard committed
877 878 879
#ifdef DEBUG_PIC
    printf("pic_write: addr=0x%02x val=0x%02x\n", addr, val);
#endif
880 881 882 883 884 885 886 887 888 889 890 891 892
    s = &pics[addr >> 7];
    addr &= 1;
    if (addr == 0) {
        if (val & 0x10) {
            /* init */
            memset(s, 0, sizeof(PicState));
            s->init_state = 1;
            s->init4 = val & 1;
            if (val & 0x02)
                hw_error("single mode not supported");
            if (val & 0x08)
                hw_error("level sensitive irq not supported");
        } else if (val & 0x08) {
893
            if (val & 0x04)
894
                s->poll = 1;
895 896 897 898 899
            if (val & 0x02)
                s->read_reg_select = val & 1;
            if (val & 0x40)
                s->special_mask = (val >> 5) & 1;
        } else {
900 901 902 903 904
            cmd = val >> 5;
            switch(cmd) {
            case 0:
            case 4:
                s->rotate_on_auto_eoi = cmd >> 2;
905
                break;
906 907
            case 1: /* end of interrupt */
            case 5:
908
                priority = get_priority(s, s->isr);
909 910 911 912 913 914
                if (priority != 8) {
                    irq = (priority + s->priority_add) & 7;
                    s->isr &= ~(1 << irq);
                    if (cmd == 5)
                        s->priority_add = (irq + 1) & 7;
                    pic_update_irq();
915 916
                }
                break;
917 918 919
            case 3:
                irq = val & 7;
                s->isr &= ~(1 << irq);
920
                pic_update_irq();
921
                break;
922
            case 6:
923
                s->priority_add = (val + 1) & 7;
924
                pic_update_irq();
925
                break;
926 927 928 929
            case 7:
                irq = val & 7;
                s->isr &= ~(1 << irq);
                s->priority_add = (irq + 1) & 7;
930
                pic_update_irq();
931
                break;
932 933 934
            default:
                /* no operation */
                break;
935 936 937 938 939 940 941
            }
        }
    } else {
        switch(s->init_state) {
        case 0:
            /* normal mode */
            s->imr = val;
942
            pic_update_irq();
943 944 945 946 947 948 949 950 951 952 953 954 955
            break;
        case 1:
            s->irq_base = val & 0xf8;
            s->init_state = 2;
            break;
        case 2:
            if (s->init4) {
                s->init_state = 3;
            } else {
                s->init_state = 0;
            }
            break;
        case 3:
956
            s->special_fully_nested_mode = (val >> 4) & 1;
957 958 959 960 961 962 963
            s->auto_eoi = (val >> 1) & 1;
            s->init_state = 0;
            break;
        }
    }
}

964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986
static uint32_t pic_poll_read (PicState *s, uint32_t addr1)
{
    int ret;

    ret = pic_get_irq(s);
    if (ret >= 0) {
        if (addr1 >> 7) {
            pics[0].isr &= ~(1 << 2);
            pics[0].irr &= ~(1 << 2);
        }
        s->irr &= ~(1 << ret);
        s->isr &= ~(1 << ret);
        if (addr1 >> 7 || ret != 2)
            pic_update_irq();
    } else {
        ret = 0x07;
        pic_update_irq();
    }

    return ret;
}

uint32_t pic_ioport_read(CPUState *env, uint32_t addr1)
987 988
{
    PicState *s;
bellard's avatar
bellard committed
989 990 991 992
    unsigned int addr;
    int ret;

    addr = addr1;
993 994
    s = &pics[addr >> 7];
    addr &= 1;
995
    if (s->poll) {
996 997 998
        ret = pic_poll_read(s, addr1);
        s->poll = 0;
    } else {
999 1000 1001 1002 1003 1004 1005 1006
        if (addr == 0) {
            if (s->read_reg_select)
                ret = s->isr;
            else
                ret = s->irr;
        } else {
            ret = s->imr;
        }
1007
    }
bellard's avatar
bellard committed
1008 1009 1010 1011
#ifdef DEBUG_PIC
    printf("pic_read: addr=0x%02x val=0x%02x\n", addr1, ret);
#endif
    return ret;
1012 1013
}

1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027
/* memory mapped interrupt status */
uint32_t pic_intack_read(CPUState *env)
{
    int ret;

    ret = pic_poll_read(&pics[0], 0x00);
    if (ret == 2)
        ret = pic_poll_read(&pics[1], 0x80) + 8;
    /* Prepare for ISR read */
    pics[0].read_reg_select = 1;
    
    return ret;
}

1028 1029
void pic_init(void)
{
1030
#if defined (TARGET_I386) || defined (TARGET_PPC)
bellard's avatar
bellard committed
1031 1032 1033 1034
    register_ioport_write(0x20, 2, pic_ioport_write, 1);
    register_ioport_read(0x20, 2, pic_ioport_read, 1);
    register_ioport_write(0xa0, 2, pic_ioport_write, 1);
    register_ioport_read(0xa0, 2, pic_ioport_read, 1);
1035
#endif
1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
}

/***********************************************************/
/* 8253 PIT emulation */

#define PIT_FREQ 1193182

#define RW_STATE_LSB 0
#define RW_STATE_MSB 1
#define RW_STATE_WORD0 2
#define RW_STATE_WORD1 3
#define RW_STATE_LATCHED_WORD0 4
#define RW_STATE_LATCHED_WORD1 5

typedef struct PITChannelState {
1051
    int count; /* can be 65536 */
1052 1053 1054 1055 1056 1057
    uint16_t latched_count;
    uint8_t rw_state;
    uint8_t mode;
    uint8_t bcd; /* not supported */
    uint8_t gate; /* timer start */
    int64_t count_load_time;
1058
    int64_t count_last_edge_check_time;
1059 1060 1061 1062
} PITChannelState;

PITChannelState pit_channels[3];
int speaker_data_on;
1063
int dummy_refresh_clock;
1064
int pit_min_timer_count = 0;
1065

1066 1067 1068 1069

#if defined(__powerpc__)

static inline uint32_t get_tbl(void) 
1070
{
1071 1072 1073
    uint32_t tbl;
    asm volatile("mftb %0" : "=r" (tbl));
    return tbl;
1074 1075
}

1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097
static inline uint32_t get_tbu(void) 
{
	uint32_t tbl;
	asm volatile("mftbu %0" : "=r" (tbl));
	return tbl;
}

int64_t cpu_get_real_ticks(void)
{
    uint32_t l, h, h1;
    /* NOTE: we test if wrapping has occurred */
    do {
        h = get_tbu();
        l = get_tbl();
        h1 = get_tbu();
    } while (h != h1);
    return ((int64_t)h << 32) | l;
}

#elif defined(__i386__)

int64_t cpu_get_real_ticks(void)
1098 1099 1100 1101 1102 1103
{
    int64_t val;
    asm("rdtsc" : "=A" (val));
    return val;
}

1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135
#else
#error unsupported CPU
#endif

static int64_t cpu_ticks_offset;
static int64_t cpu_ticks_last;

int64_t cpu_get_ticks(void)
{
    return cpu_get_real_ticks() + cpu_ticks_offset;
}

/* enable cpu_get_ticks() */
void cpu_enable_ticks(void)
{
    cpu_ticks_offset = cpu_ticks_last - cpu_get_real_ticks();
}

/* disable cpu_get_ticks() : the clock is stopped. You must not call
   cpu_get_ticks() after that.  */
void cpu_disable_ticks(void)
{
    cpu_ticks_last = cpu_get_ticks();
}

int64_t get_clock(void)
{
    struct timeval tv;
    gettimeofday(&tv, NULL);
    return tv.tv_sec * 1000000LL + tv.tv_usec;
}

1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147
void cpu_calibrate_ticks(void)
{
    int64_t usec, ticks;

    usec = get_clock();
    ticks = cpu_get_ticks();
    usleep(50 * 1000);
    usec = get_clock() - usec;
    ticks = cpu_get_ticks() - ticks;
    ticks_per_sec = (ticks * 1000000LL + (usec >> 1)) / usec;
}

1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171
/* compute with 96 bit intermediate result: (a*b)/c */
static uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
{
    union {
        uint64_t ll;
        struct {
#ifdef WORDS_BIGENDIAN
            uint32_t high, low;
#else
            uint32_t low, high;
#endif            
        } l;
    } u, res;
    uint64_t rl, rh;

    u.ll = a;
    rl = (uint64_t)u.l.low * (uint64_t)b;
    rh = (uint64_t)u.l.high * (uint64_t)b;
    rh += (rl >> 32);
    res.l.high = rh / c;
    res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c;
    return res.ll;
}

1172 1173
static int pit_get_count(PITChannelState *s)
{
1174
    uint64_t d;
1175 1176
    int counter;

1177
    d = muldiv64(cpu_get_ticks() - s->count_load_time, PIT_FREQ, ticks_per_sec);
1178 1179 1180 1181 1182 1183 1184
    switch(s->mode) {
    case 0:
    case 1:
    case 4:
    case 5:
        counter = (s->count - d) & 0xffff;
        break;
bellard's avatar
bellard committed
1185 1186 1187 1188
    case 3:
        /* XXX: may be incorrect for odd counts */
        counter = s->count - ((2 * d) % s->count);
        break;
1189 1190 1191 1192 1193 1194 1195 1196 1197 1198
    default:
        counter = s->count - (d % s->count);
        break;
    }
    return counter;
}

/* get pit output bit */
static int pit_get_out(PITChannelState *s)
{
1199
    uint64_t d;
1200 1201
    int out;