#define DEFAULT_NETWORK_SCRIPT "/etc/vl-ifup"
//#define DEBUG_UNUSED_IOPORT
+//#define DEBUG_IRQ_LATENCY
+
+#define PHYS_RAM_BASE 0xac000000
+#define PHYS_RAM_MAX_SIZE (256 * 1024 * 1024)
-#define PHYS_RAM_BASE 0xa8000000
#define KERNEL_LOAD_ADDR 0x00100000
#define INITRD_LOAD_ADDR 0x00400000
#define KERNEL_PARAMS_ADDR 0x00090000
+#define MAX_DISKS 2
+
/* from plex86 (BSD license) */
struct __attribute__ ((packed)) linux_params {
// For 0x00..0x3f, see 'struct screen_info' in linux/include/linux/tty.h.
int loglevel;
IOPortReadFunc *ioport_read_table[3][MAX_IOPORTS];
IOPortWriteFunc *ioport_write_table[3][MAX_IOPORTS];
+BlockDriverState *bs_table[MAX_DISKS];
/***********************************************************/
/* x86 io ports */
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);
+ cmos_data[RTC_MONTH] = to_bcd(tm->tm_mon + 1);
cmos_data[RTC_YEAR] = to_bcd(tm->tm_year % 100);
cmos_data[RTC_REG_A] = 0x26;
/***********************************************************/
/* 8259 pic emulation */
+//#define DEBUG_PIC
+
typedef struct PicState {
uint8_t last_irr; /* edge detection */
uint8_t irr; /* interrupt request register */
}
}
-void pic_set_irq(int irq, int level)
-{
- pic_set_irq1(&pics[irq >> 3], irq & 7, level);
-}
-
-/* can be called at any time outside cpu_exec() to raise irqs if
- necessary */
-void pic_handle_irq(void)
+/* raise irq to CPU if necessary. must be called every time the active
+ irq may change */
+static void pic_update_irq(void)
{
int irq2, irq;
/* from master pic */
pic_irq_requested = irq;
}
- global_env->hard_interrupt_request = 1;
+ cpu_x86_interrupt(global_env, CPU_INTERRUPT_HARD);
+ }
+}
+
+#ifdef DEBUG_IRQ_LATENCY
+int64_t irq_time[16];
+int64_t cpu_get_ticks(void);
+#endif
+#ifdef DEBUG_PIC
+int irq_level[16];
+#endif
+
+void pic_set_irq(int irq, int level)
+{
+#ifdef DEBUG_PIC
+ if (level != irq_level[irq]) {
+ printf("pic_set_irq: irq=%d level=%d\n", irq, level);
+ irq_level[irq] = level;
}
+#endif
+#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();
}
int cpu_x86_get_pic_interrupt(CPUX86State *env)
/* signal the pic that the irq was acked by the CPU */
irq = pic_irq_requested;
+#ifdef DEBUG_IRQ_LATENCY
+ printf("IRQ%d latency=%Ld\n", irq, cpu_get_ticks() - irq_time[irq]);
+#endif
+#ifdef DEBUG_PIC
+ printf("pic_interrupt: irq=%d\n", irq);
+#endif
+
if (irq >= 8) {
irq2 = irq & 7;
pics[1].isr |= (1 << irq2);
PicState *s;
int priority;
+#ifdef DEBUG_PIC
+ printf("pic_write: addr=0x%02x val=0x%02x\n", addr, val);
+#endif
s = &pics[addr >> 7];
addr &= 1;
if (addr == 0) {
case 0:
/* normal mode */
s->imr = val;
+ pic_update_irq();
break;
case 1:
s->irq_base = val & 0xf8;
}
}
-uint32_t pic_ioport_read(CPUX86State *env, uint32_t addr)
+uint32_t pic_ioport_read(CPUX86State *env, uint32_t addr1)
{
PicState *s;
+ unsigned int addr;
+ int ret;
+
+ addr = addr1;
s = &pics[addr >> 7];
addr &= 1;
if (addr == 0) {
if (s->read_reg_select)
- return s->isr;
+ ret = s->isr;
else
- return s->irr;
+ ret = s->irr;
} else {
- return s->imr;
+ ret = s->imr;
}
+#ifdef DEBUG_PIC
+ printf("pic_read: addr=0x%02x val=0x%02x\n", addr1, ret);
+#endif
+ return ret;
}
void pic_init(void)
printf("\n"
"C-a h print this help\n"
"C-a x exit emulatior\n"
+ "C-a s save disk data back to file (if -snapshot)\n"
"C-a b send break (magic sysrq)\n"
"C-a C-a send C-a\n"
);
case 'x':
exit(0);
break;
+ case 's':
+ {
+ int i;
+ for (i = 0; i < MAX_DISKS; i++) {
+ if (bs_table[i])
+ bdrv_commit(bs_table[i]);
+ }
+ }
+ break;
case 'b':
/* send break */
s->rbr = 0;
s->rcnt == 0) {
s->isr |= ENISR_RDC;
ne2000_update_irq(s);
- /* XXX: find a better solution for irqs */
- cpu_x86_interrupt(global_env);
}
if (val & E8390_TRANS) {
net_send_packet(s, s->mem + (s->tpsr << 8), s->tcnt);
#define WIN_SET_MAX 0xF9
#define DISABLE_SEAGATE 0xFB
-#define MAX_MULT_SECTORS 16
-
-#define MAX_DISKS 2
+/* set to 1 set disable mult support */
+#define MAX_MULT_SECTORS 8
struct IDEState;
/* ide regs */
uint8_t feature;
uint8_t error;
- uint8_t nsector;
+ uint16_t nsector; /* 0 is 256 to ease computations */
uint8_t sector;
uint8_t lcyl;
uint8_t hcyl;
/* depends on bit 4 in select, only meaningful for drive 0 */
struct IDEState *cur_drive;
BlockDriverState *bs;
+ int req_nb_sectors; /* number of sectors per interrupt */
EndTransferFunc *end_transfer_func;
uint8_t *data_ptr;
uint8_t *data_end;
uint8_t io_buffer[MAX_MULT_SECTORS*512 + 4];
} IDEState;
-BlockDriverState *bs_table[MAX_DISKS];
IDEState ide_state[MAX_DISKS];
static void padstr(char *str, const char *src, int len)
stw(p + 21, 512); /* cache size in sectors */
stw(p + 22, 4); /* ecc bytes */
padstr((uint8_t *)(p + 27), "QEMU HARDDISK", 40);
- // stw(p + 47, MAX_MULT_SECTORS);
+#if MAX_MULT_SECTORS > 1
+ stw(p + 47, MAX_MULT_SECTORS);
+#endif
stw(p + 48, 1); /* dword I/O */
stw(p + 49, 1 << 9); /* LBA supported, no DMA */
stw(p + 51, 0x200); /* PIO transfer cycle */
{
if (!(ide_state[0].cmd & IDE_CMD_DISABLE_IRQ)) {
pic_set_irq(s->irq, 1);
- cpu_x86_interrupt(global_env);
}
}
static void ide_sector_read(IDEState *s)
{
int64_t sector_num;
- int ret;
+ int ret, n;
s->status = READY_STAT | SEEK_STAT;
sector_num = ide_get_sector(s);
- s->nsector--;
- if (s->nsector == 0xff) {
+ n = s->nsector;
+ if (n == 0) {
/* no more sector to read from disk */
ide_transfer_stop(s);
} else {
#if defined(DEBUG_IDE)
printf("read sector=%Ld\n", sector_num);
#endif
- ret = bdrv_read(s->bs, sector_num, s->io_buffer, 1);
- ide_transfer_start(s, 512, ide_sector_read);
+ if (n > s->req_nb_sectors)
+ n = s->req_nb_sectors;
+ ret = bdrv_read(s->bs, sector_num, s->io_buffer, n);
+ ide_transfer_start(s, 512 * n, ide_sector_read);
ide_set_irq(s);
+ ide_set_sector(s, sector_num + n);
+ s->nsector -= n;
}
- ide_set_sector(s, sector_num + 1);
}
static void ide_sector_write(IDEState *s)
{
int64_t sector_num;
- int ret;
+ int ret, n, n1;
s->status = READY_STAT | SEEK_STAT;
sector_num = ide_get_sector(s);
#if defined(DEBUG_IDE)
printf("write sector=%Ld\n", sector_num);
#endif
- ret = bdrv_write(s->bs, sector_num, s->io_buffer, 1);
- s->nsector--;
+ n = s->nsector;
+ if (n > s->req_nb_sectors)
+ n = s->req_nb_sectors;
+ ret = bdrv_write(s->bs, sector_num, s->io_buffer, n);
+ s->nsector -= n;
if (s->nsector == 0) {
/* no more sector to write */
ide_transfer_stop(s);
} else {
- ide_transfer_start(s, 512, ide_sector_write);
+ n1 = s->nsector;
+ if (n1 > s->req_nb_sectors)
+ n1 = s->req_nb_sectors;
+ ide_transfer_start(s, 512 * n1, ide_sector_write);
}
- ide_set_sector(s, sector_num + 1);
+ ide_set_sector(s, sector_num + n);
ide_set_irq(s);
}
void ide_ioport_write(CPUX86State *env, uint32_t addr, uint32_t val)
{
IDEState *s = ide_state[0].cur_drive;
- int unit;
+ int unit, n;
addr &= 7;
#ifdef DEBUG_IDE
s->feature = val;
break;
case 2:
+ if (val == 0)
+ val = 256;
s->nsector = val;
break;
case 3:
break;
case WIN_READ:
case WIN_READ_ONCE:
+ s->req_nb_sectors = 1;
ide_sector_read(s);
break;
case WIN_WRITE:
case WIN_WRITE_ONCE:
s->status = SEEK_STAT;
+ s->req_nb_sectors = 1;
ide_transfer_start(s, 512, ide_sector_write);
break;
+ case WIN_MULTREAD:
+ if (!s->mult_sectors)
+ goto abort_cmd;
+ s->req_nb_sectors = s->mult_sectors;
+ ide_sector_read(s);
+ break;
+ case WIN_MULTWRITE:
+ if (!s->mult_sectors)
+ goto abort_cmd;
+ s->status = SEEK_STAT;
+ s->req_nb_sectors = s->mult_sectors;
+ n = s->nsector;
+ if (n > s->req_nb_sectors)
+ n = s->req_nb_sectors;
+ ide_transfer_start(s, 512 * n, ide_sector_write);
+ break;
+ case WIN_READ_NATIVE_MAX:
+ ide_set_sector(s, s->nb_sectors - 1);
+ s->status = READY_STAT;
+ ide_set_irq(s);
+ break;
default:
+ abort_cmd:
ide_abort_command(s);
ide_set_irq(s);
break;
ret = s->error;
break;
case 2:
- ret = s->nsector;
+ ret = s->nsector & 0xff;
break;
case 3:
ret = s->sector;
}
/***********************************************************/
+/* simulate reset (stop qemu) */
+
+int reset_requested;
+
+uint32_t kbd_read_status(CPUX86State *env, uint32_t addr)
+{
+ return 0;
+}
+
+void kbd_write_command(CPUX86State *env, uint32_t addr, uint32_t val)
+{
+ switch(val) {
+ case 0xfe:
+ reset_requested = 1;
+ cpu_x86_interrupt(global_env, CPU_INTERRUPT_EXIT);
+ break;
+ default:
+ break;
+ }
+}
+
+void kbd_init(void)
+{
+ register_ioport_read(0x64, 1, kbd_read_status, 1);
+ register_ioport_write(0x64, 1, kbd_write_command, 1);
+}
+
+/***********************************************************/
/* cpu signal handler */
static void host_segv_handler(int host_signum, siginfo_t *info,
void *puc)
timer_irq_count = 2;
timer_irq_count--;
/* just exit from the cpu to have a chance to handle timers */
- cpu_x86_interrupt(global_env);
+ cpu_x86_interrupt(global_env, CPU_INTERRUPT_EXIT);
timer_irq_pending = 1;
}
}
+unsigned long mmap_addr = PHYS_RAM_BASE;
+
+void *get_mmap_addr(unsigned long size)
+{
+ unsigned long addr;
+ addr = mmap_addr;
+ mmap_addr += ((size + 4095) & ~4095) + 4096;
+ return (void *)addr;
+}
+
/* main execution loop */
CPUState *cpu_gdbstub_get_env(void *opaque)
for(;;) {
ret = cpu_x86_exec(env);
-
+ if (reset_requested)
+ break;
+
/* if hlt instruction, we wait until the next IRQ */
if (ret == EXCP_HLT)
timeout = 10;
pic_set_irq(0, 0);
timer_irq_pending = 0;
}
-
- pic_handle_irq();
}
}
"-initrd file use 'file' as initial ram disk\n"
"-hda file use 'file' as hard disk 0 image\n"
"-hdb file use 'file' as hard disk 1 image\n"
+ "-snapshot write to temporary files instead of disk image files\n"
"-m megs set virtual RAM size to megs MB\n"
"-n script set network init script [default=%s]\n"
"\n"
{ "initrd", 1, NULL, 0, },
{ "hda", 1, NULL, 0, },
{ "hdb", 1, NULL, 0, },
+ { "snapshot", 0, NULL, 0, },
{ NULL, 0, NULL, 0 },
};
int main(int argc, char **argv)
{
int c, ret, initrd_size, i, use_gdbstub, gdbstub_port, long_index;
+ int snapshot;
struct linux_params *params;
struct sigaction act;
struct itimerval itv;
pstrcpy(network_script, sizeof(network_script), DEFAULT_NETWORK_SCRIPT);
use_gdbstub = 0;
gdbstub_port = DEFAULT_GDBSTUB_PORT;
+ snapshot = 0;
for(;;) {
c = getopt_long_only(argc, argv, "hm:dn:sp:", long_options, &long_index);
if (c == -1)
case 2:
hd_filename[1] = optarg;
break;
+ case 3:
+ snapshot = 1;
+ break;
}
break;
case 'h':
phys_ram_size = atoi(optarg) * 1024 * 1024;
if (phys_ram_size <= 0)
help();
+ if (phys_ram_size > PHYS_RAM_MAX_SIZE) {
+ fprintf(stderr, "vl: at most %d MB RAM can be simulated\n",
+ PHYS_RAM_MAX_SIZE / (1024 * 1024));
+ exit(1);
+ }
break;
case 'd':
loglevel = 1;
help();
/* init debug */
+ setvbuf(stdout, NULL, _IOLBF, 0);
if (loglevel) {
logfile = fopen(DEBUG_LOGFILE, "w");
if (!logfile) {
setvbuf(logfile, NULL, _IOLBF, 0);
}
- /* open the virtual block devices */
- for(i = 0; i < MAX_DISKS; i++) {
- if (hd_filename[i]) {
- bs_table[i] = bdrv_open(hd_filename[i]);
- if (!bs_table[i]) {
- fprintf(stderr, "vl: could not open hard disk image '%s\n",
- hd_filename[i]);
- exit(1);
- }
- }
- }
-
/* init network tun interface */
net_init();
}
ftruncate(phys_ram_fd, phys_ram_size);
unlink(phys_ram_file);
- phys_ram_base = mmap((void *)PHYS_RAM_BASE, phys_ram_size,
+ phys_ram_base = mmap(get_mmap_addr(phys_ram_size), phys_ram_size,
PROT_WRITE | PROT_READ, MAP_SHARED | MAP_FIXED,
phys_ram_fd, 0);
if (phys_ram_base == MAP_FAILED) {
exit(1);
}
+ /* open the virtual block devices */
+ for(i = 0; i < MAX_DISKS; i++) {
+ if (hd_filename[i]) {
+ bs_table[i] = bdrv_open(hd_filename[i], snapshot);
+ if (!bs_table[i]) {
+ fprintf(stderr, "vl: could not open hard disk image '%s\n",
+ hd_filename[i]);
+ exit(1);
+ }
+ }
+ }
+
/* now we can load the kernel */
ret = load_kernel(argv[optind], phys_ram_base + KERNEL_LOAD_ADDR);
if (ret < 0) {
params->mount_root_rdonly = 0;
params->cl_magic = 0xA33F;
params->cl_offset = params->commandline - (uint8_t *)params;
- params->ext_mem_k = (phys_ram_size / 1024) - 1024;
+ params->alt_mem_k = (phys_ram_size / 1024) - 1024;
for(i = optind + 1; i < argc; i++) {
if (i != optind + 1)
pstrcat(params->commandline, sizeof(params->commandline), " ");
serial_init();
ne2000_init();
ide_init();
+ kbd_init();
/* setup cpu signal handlers for MMU / self modifying code handling */
sigfillset(&act.sa_mask);