--- trunk/src/emul.c 2007/10/08 16:19:37 22 +++ trunk/src/emul.c 2007/10/08 16:22:11 40 @@ -1,5 +1,5 @@ /* - * Copyright (C) 2003-2006 Anders Gavare. All rights reserved. + * Copyright (C) 2003-2007 Anders Gavare. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: @@ -25,7 +25,7 @@ * SUCH DAMAGE. * * - * $Id: emul.c,v 1.247 2006/02/04 12:27:12 debug Exp $ + * $Id: emul.c,v 1.284 2007/04/19 15:18:15 debug Exp $ * * Emulation startup and misc. routines. */ @@ -39,7 +39,6 @@ #include #include "arcbios.h" -#include "bintrans.h" #include "cpu.h" #include "emul.h" #include "console.h" @@ -52,23 +51,27 @@ #include "mips_cpu_types.h" #include "misc.h" #include "net.h" +#include "settings.h" #include "sgi_arcbios.h" +#include "timer.h" #include "x11.h" -extern int force_debugger_at_exit; - extern int extra_argc; extern char **extra_argv; extern int verbose; extern int quiet_mode; +extern int force_debugger_at_exit; +extern int single_step; +extern int old_show_trace_tree; +extern int old_instruction_trace; +extern int old_quiet_mode; +extern int quiet_mode; extern struct emul *debugger_emul; extern struct diskimage *diskimages[]; -static char *diskimage_types[] = DISKIMAGE_TYPES; - static void print_separator(void) { @@ -105,12 +108,12 @@ uint64_t addr; int res = get_symbol_addr(&m->symbol_context, m->breakpoint_string[i], &addr); - if (!res) + if (!res) { fprintf(stderr, - "WARNING! Breakpoint '%s' could not be" + "ERROR! Breakpoint '%s' could not be" " parsed\n", m->breakpoint_string[i]); - else { + } else { dp = addr; string_flag = 1; } @@ -141,622 +144,86 @@ */ static void fix_console(void) { - console_deinit(); + console_deinit_main(); } /* - * iso_load_bootblock(): - * - * Try to load a kernel from an ISO 9660 disk image. iso_type is 1 for - * "CD001" (standard), 2 for "CDW01" (ECMA), and 3 for "CDROM" (Sierra). - * - * TODO: This function uses too many magic offsets and so on; it should be - * cleaned up some day. + * emul_new(): * - * Returns 1 on success, 0 on failure. + * Returns a reasonably initialized struct emul. */ -static int iso_load_bootblock(struct machine *m, struct cpu *cpu, - int disk_id, int disk_type, int iso_type, unsigned char *buf, - int *n_loadp, char ***load_namesp) +struct emul *emul_new(char *name, int id) { - char str[35]; - int filenr, i, ofs, dirlen, res = 0, res2, iadd = DEBUG_INDENTATION; - int found_dir; - uint64_t dirofs; - uint64_t fileofs, filelen; - unsigned char *dirbuf = NULL, *dp; - unsigned char *match_entry = NULL; - char *p, *filename_orig; - char *filename = strdup(cpu->machine->boot_kernel_filename); - unsigned char *filebuf = NULL; - char *tmpfname = NULL; - char **new_array; - int tmpfile_handle; - - if (filename == NULL) { - fatal("out of memory\n"); - exit(1); - } - filename_orig = filename; - - debug("ISO9660 boot:\n"); - debug_indentation(iadd); - - /* Volume ID: */ - ofs = iso_type == 3? 48 : 40; - memcpy(str, buf + ofs, sizeof(str)); - str[32] = '\0'; i = 31; - while (i >= 0 && str[i]==' ') - str[i--] = '\0'; - if (str[0]) - debug("\"%s\"", str); - else { - /* System ID: */ - ofs = iso_type == 3? 16 : 8; - memcpy(str, buf + ofs, sizeof(str)); - str[32] = '\0'; i = 31; - while (i >= 0 && str[i]==' ') - str[i--] = '\0'; - if (str[0]) - debug("\"%s\"", str); - else - debug("(no ID)"); - } - - debug(":%s\n", filename); - - - /* - * Traverse the directory structure to find the kernel. - */ - - dirlen = buf[0x84] + 256*buf[0x85] + 65536*buf[0x86]; - if (dirlen != buf[0x8b] + 256*buf[0x8a] + 65536*buf[0x89]) - fatal("WARNING: Root directory length mismatch?\n"); - - dirofs = (int64_t)(buf[0x8c] + (buf[0x8d] << 8) + (buf[0x8e] << 16) + - ((uint64_t)buf[0x8f] << 24)) * 2048; - - /* debug("root = %i bytes at 0x%llx\n", dirlen, (long long)dirofs); */ - - dirbuf = malloc(dirlen); - if (dirbuf == NULL) { - fatal("out of memory in iso_load_bootblock()\n"); - exit(1); - } - - res2 = diskimage_access(m, disk_id, disk_type, 0, dirofs, dirbuf, - dirlen); - if (!res2) { - fatal("Couldn't read the disk image. Aborting.\n"); - goto ret; - } - - found_dir = 1; /* Assume root dir */ - dp = dirbuf; filenr = 1; - p = NULL; - while (dp < dirbuf + dirlen) { - size_t i, nlen = dp[0]; - int x = dp[2] + (dp[3] << 8) + (dp[4] << 16) + - ((uint64_t)dp[5] << 24); - int y = dp[6] + (dp[7] << 8); - char direntry[65]; - - dp += 8; - - /* - * As long as there is an \ or / in the filename, then we - * have not yet found the directory. - */ - p = strchr(filename, '/'); - if (p == NULL) - p = strchr(filename, '\\'); - - /* debug("%i%s: %i, %i, \"", filenr, filenr == found_dir? - " [CURRENT]" : "", x, y); */ - for (i=0; i 2047) { - dirofs = (dirofs | 2047) + 1; - /* debug("realign dirofs = 0x%llx\n", dirofs); */ - } - - res2 = diskimage_access(m, disk_id, disk_type, 0, dirofs, - dirbuf, 256); - if (!res2) { - fatal("Couldn't read the disk image. Aborting.\n"); - goto ret; - } - - dp = dirbuf; - len = dp[0]; - if (len < 2) - break; - - /* - * TODO: Actually parse the directory entry! - * - * Haha, this must be rewritten. - */ - for (i=32; ipath = malloc(15); + if (e->path == NULL) { + fprintf(stderr, "out of memory\n"); exit(1); } - memcpy(new_array, *load_namesp, sizeof(char *) * (*n_loadp)); - *load_namesp = new_array; - - /* This adds a Backspace char in front of the filename; this - is a special hack which causes the file to be removed once - it has been loaded. */ - tmpfname = realloc(tmpfname, strlen(tmpfname) + 2); - memmove(tmpfname + 1, tmpfname, strlen(tmpfname) + 1); - tmpfname[0] = 8; - - (*load_namesp)[*n_loadp - 1] = tmpfname; - - res = 1; - -ret: - if (dirbuf != NULL) - free(dirbuf); - - if (filebuf != NULL) - free(filebuf); - - if (match_entry != NULL) - free(match_entry); - - free(filename_orig); - - debug_indentation(-iadd); - return res; -} - - -/* - * apple_load_bootblock(): - * - * Try to load a kernel from a disk image with an Apple Partition Table. - * - * TODO: This function uses too many magic offsets and so on; it should be - * cleaned up some day. See http://www.awprofessional.com/articles/ - * article.asp?p=376123&seqNum=3&rl=1 for some info on the Apple - * partition format. - * - * Returns 1 on success, 0 on failure. - */ -static int apple_load_bootblock(struct machine *m, struct cpu *cpu, - int disk_id, int disk_type, int *n_loadp, char ***load_namesp) -{ - unsigned char buf[0x8000]; - int res, partnr, n_partitions = 0, n_hfs_partitions = 0; - uint64_t hfs_start, hfs_length; - - res = diskimage_access(m, disk_id, disk_type, 0, 0x0, buf, sizeof(buf)); - if (!res) { - fatal("apple_load_bootblock: couldn't read the disk " - "image. Aborting.\n"); - return 0; - } - - partnr = 0; - do { - int start, length; - int ofs = 0x200 * (partnr + 1); - if (partnr == 0) - n_partitions = buf[ofs + 7]; - start = ((uint64_t)buf[ofs + 8] << 24) + (buf[ofs + 9] << 16) + - (buf[ofs + 10] << 8) + buf[ofs + 11]; - length = ((uint64_t)buf[ofs+12] << 24) + (buf[ofs + 13] << 16) + - (buf[ofs + 14] << 8) + buf[ofs + 15]; - - debug("partition %i: '%s', type '%s', start %i, length %i\n", - partnr, buf + ofs + 0x10, buf + ofs + 0x30, - start, length); - - if (strcmp((char *)buf + ofs + 0x30, "Apple_HFS") == 0) { - n_hfs_partitions ++; - hfs_start = 512 * start; - hfs_length = 512 * length; - } - - /* Any more partitions? */ - partnr ++; - } while (partnr < n_partitions); - - if (n_hfs_partitions == 0) { - fatal("Error: No HFS partition found! TODO\n"); - return 0; - } - if (n_hfs_partitions >= 2) { - fatal("Error: Too many HFS partitions found! TODO\n"); - return 0; - } - - return 0; -} + snprintf(e->path, 15, "emul[%i]", id); + e->settings = settings_new(); -/* - * load_bootblock(): - * - * For some emulation modes, it is possible to boot from a harddisk image by - * loading a bootblock from a specific disk offset into memory, and executing - * that, instead of requiring a separate kernel file. It is then up to the - * bootblock to load a kernel. - * - * Returns 1 on success, 0 on failure. - */ -static int load_bootblock(struct machine *m, struct cpu *cpu, - int *n_loadp, char ***load_namesp) -{ - int boot_disk_id, boot_disk_type = 0, n_blocks, res, readofs, - iso_type, retval = 0; - unsigned char minibuf[0x20]; - unsigned char *bootblock_buf; - uint64_t bootblock_offset; - uint64_t bootblock_loadaddr, bootblock_pc; - - boot_disk_id = diskimage_bootdev(m, &boot_disk_type); - if (boot_disk_id < 0) - return 0; - - switch (m->machine_type) { - case MACHINE_PMAX: - /* - * The first few bytes of a disk contains information about - * where the bootblock(s) are located. (These are all 32-bit - * little-endian words.) - * - * Offset 0x10 = load address - * 0x14 = initial PC value - * 0x18 = nr of 512-byte blocks to read - * 0x1c = offset on disk to where the bootblocks - * are (in 512-byte units) - * 0x20 = nr of blocks to read... - * 0x24 = offset... - * - * nr of blocks to read and offset are repeated until nr of - * blocks to read is zero. - */ - res = diskimage_access(m, boot_disk_id, boot_disk_type, 0, 0, - minibuf, sizeof(minibuf)); - - bootblock_loadaddr = minibuf[0x10] + (minibuf[0x11] << 8) - + (minibuf[0x12] << 16) + ((uint64_t)minibuf[0x13] << 24); - - /* Convert loadaddr to uncached: */ - if ((bootblock_loadaddr & 0xf0000000ULL) != 0x80000000 && - (bootblock_loadaddr & 0xf0000000ULL) != 0xa0000000) - fatal("\nWARNING! Weird load address 0x%08x.\n\n", - (int)bootblock_loadaddr); - bootblock_loadaddr &= 0x0fffffffULL; - bootblock_loadaddr |= 0xffffffffa0000000ULL; - - bootblock_pc = minibuf[0x14] + (minibuf[0x15] << 8) - + (minibuf[0x16] << 16) + ((uint64_t)minibuf[0x17] << 24); - - bootblock_pc &= 0x0fffffffULL; - bootblock_pc |= 0xffffffffa0000000ULL; - cpu->pc = bootblock_pc; - - debug("DEC boot: loadaddr=0x%08x, pc=0x%08x", - (int)bootblock_loadaddr, (int)bootblock_pc); - - readofs = 0x18; - - for (;;) { - res = diskimage_access(m, boot_disk_id, boot_disk_type, - 0, readofs, minibuf, sizeof(minibuf)); - if (!res) { - fatal("Couldn't read the disk image. " - "Aborting.\n"); - return 0; - } + settings_add(e->settings, "n_machines", 0, + SETTINGS_TYPE_INT, SETTINGS_FORMAT_DECIMAL, + (void *) &e->n_machines); - n_blocks = minibuf[0] + (minibuf[1] << 8) - + (minibuf[2] << 16) + ((uint64_t)minibuf[3] << 24); + /* TODO: More settings? */ - bootblock_offset = (minibuf[4] + (minibuf[5] << 8) + - (minibuf[6]<<16) + ((uint64_t)minibuf[7]<<24)) * 512; - - if (n_blocks < 1) - break; - - debug(readofs == 0x18? ": %i" : " + %i", n_blocks); - - if (n_blocks * 512 > 65536) - fatal("\nWARNING! Unusually large bootblock " - "(%i bytes)\n\n", n_blocks * 512); - - bootblock_buf = malloc(n_blocks * 512); - if (bootblock_buf == NULL) { - fprintf(stderr, "out of memory in " - "load_bootblock()\n"); - exit(1); - } - - res = diskimage_access(m, boot_disk_id, boot_disk_type, - 0, bootblock_offset, bootblock_buf, n_blocks * 512); - if (!res) { - fatal("WARNING: could not load bootblocks from" - " disk offset 0x%llx\n", - (long long)bootblock_offset); - } - - store_buf(cpu, bootblock_loadaddr, - (char *)bootblock_buf, n_blocks * 512); - - bootblock_loadaddr += 512*n_blocks; - free(bootblock_buf); - readofs += 8; - } - - debug(readofs == 0x18? ": no blocks?\n" : " blocks\n"); - return 1; - - case MACHINE_X86: - /* TODO: "El Torito" etc? */ - if (diskimage_is_a_cdrom(cpu->machine, boot_disk_id, - boot_disk_type)) - break; + /* Sane default values: */ + e->n_machines = 0; + e->next_serial_nr = 1; - bootblock_buf = malloc(512); - if (bootblock_buf == NULL) { - fprintf(stderr, "Out of memory.\n"); + if (name != NULL) { + e->name = strdup(name); + if (e->name == NULL) { + fprintf(stderr, "out of memory in emul_new()\n"); exit(1); } - debug("loading PC bootsector from %s id %i\n", - diskimage_types[boot_disk_type], boot_disk_id); - - res = diskimage_access(m, boot_disk_id, boot_disk_type, 0, 0, - bootblock_buf, 512); - if (!res) { - fatal("Couldn't read the disk image. Aborting.\n"); - return 0; - } - - if (bootblock_buf[510] != 0x55 || bootblock_buf[511] != 0xaa) - debug("WARNING! The 0x55,0xAA marker is missing! " - "Booting anyway.\n"); - store_buf(cpu, 0x7c00, (char *)bootblock_buf, 512); - free(bootblock_buf); - - return 1; - } - - - /* - * Try reading a kernel manually from the disk. The code here - * does not rely on machine-dependent boot blocks etc. - */ - /* ISO9660: (0x800 bytes at 0x8000) */ - bootblock_buf = malloc(0x800); - if (bootblock_buf == NULL) { - fprintf(stderr, "Out of memory.\n"); - exit(1); + settings_add(e->settings, "name", 0, + SETTINGS_TYPE_STRING, SETTINGS_FORMAT_STRING, + (void *) &e->name); } - res = diskimage_access(m, boot_disk_id, boot_disk_type, - 0, 0x8000, bootblock_buf, 0x800); - if (!res) { - fatal("Couldn't read the disk image. Aborting.\n"); - return 0; - } - - iso_type = 0; - if (strncmp((char *)bootblock_buf+1, "CD001", 5) == 0) - iso_type = 1; - if (strncmp((char *)bootblock_buf+1, "CDW01", 5) == 0) - iso_type = 2; - if (strncmp((char *)bootblock_buf+1, "CDROM", 5) == 0) - iso_type = 3; - - if (iso_type != 0) { - /* We can't load a kernel if the name - isn't specified. */ - if (cpu->machine->boot_kernel_filename == NULL || - cpu->machine->boot_kernel_filename[0] == '\0') - fatal("\nISO9660 filesystem, but no kernel " - "specified? (Use the -j option.)\n"); - else - retval = iso_load_bootblock(m, cpu, boot_disk_id, - boot_disk_type, iso_type, bootblock_buf, - n_loadp, load_namesp); - } - - if (retval != 0) - goto ret_ok; - - /* Apple parition table: */ - res = diskimage_access(m, boot_disk_id, boot_disk_type, - 0, 0x0, bootblock_buf, 0x800); - if (!res) { - fatal("Couldn't read the disk image. Aborting.\n"); - return 0; - } - if (bootblock_buf[0x000] == 'E' && bootblock_buf[0x001] == 'R' && - bootblock_buf[0x200] == 'P' && bootblock_buf[0x201] == 'M') { - /* We can't load a kernel if the name - isn't specified. */ - if (cpu->machine->boot_kernel_filename == NULL || - cpu->machine->boot_kernel_filename[0] == '\0') - fatal("\nApple partition table, but no kernel " - "specified? (Use the -j option.)\n"); - else - retval = apple_load_bootblock(m, cpu, boot_disk_id, - boot_disk_type, n_loadp, load_namesp); - } - -ret_ok: - free(bootblock_buf); - return retval; + return e; } /* - * emul_new(): + * emul_destroy(): * - * Returns a reasonably initialized struct emul. + * Destroys a previously created emul object. */ -struct emul *emul_new(char *name) +void emul_destroy(struct emul *emul) { - struct emul *e; - e = malloc(sizeof(struct emul)); - if (e == NULL) { - fprintf(stderr, "out of memory in emul_new()\n"); - exit(1); + int i; + + if (emul->name != NULL) { + settings_remove(emul->settings, "name"); + free(emul->name); } - memset(e, 0, sizeof(struct emul)); + for (i=0; in_machines; i++) + machine_destroy(emul->machines[i]); - /* Sane default values: */ - e->n_machines = 0; - e->next_serial_nr = 1; + if (emul->machines != NULL) + free(emul->machines); - if (name != NULL) { - e->name = strdup(name); - if (e->name == NULL) { - fprintf(stderr, "out of memory in emul_new()\n"); - exit(1); - } - } + /* Remove any remaining level-1 settings: */ + settings_remove_all(emul->settings); + settings_destroy(emul->settings); - return e; + free(emul); } @@ -771,10 +238,14 @@ struct machine *emul_add_machine(struct emul *e, char *name) { struct machine *m; + char tmpstr[20]; + int i; - m = machine_new(name, e); + m = machine_new(name, e, e->n_machines); m->serial_nr = (e->next_serial_nr ++); + i = e->n_machines; + e->n_machines ++; e->machines = realloc(e->machines, sizeof(struct machine *) * e->n_machines); @@ -783,7 +254,12 @@ exit(1); } - e->machines[e->n_machines - 1] = m; + e->machines[i] = m; + + snprintf(tmpstr, sizeof(tmpstr), "machine[%i]", i); + settings_add(e->settings, tmpstr, 1, SETTINGS_TYPE_SUBSETTINGS, 0, + e->machines[i]->settings); + return m; } @@ -940,9 +416,6 @@ if (m->arch == ARCH_ALPHA) m->arch_pagesize = 8192; - if (m->arch != ARCH_MIPS) - m->bintrans_enable = 0; - machine_memsize_fix(m); /* @@ -990,18 +463,17 @@ } memset(m->cpus, 0, sizeof(struct cpu *) * m->ncpus); - /* Initialize dynamic binary translation, if available: */ - if (m->bintrans_enable) - bintrans_init(m, m->memory); - debug("cpu0"); if (m->ncpus > 1) debug(" .. cpu%i", m->ncpus - 1); debug(": "); for (i=0; incpus; i++) { m->cpus[i] = cpu_new(m->memory, m, i, m->cpu_name); - if (m->bintrans_enable) - bintrans_init_cpu(m->cpus[i]); + if (m->cpus[i] == NULL) { + fprintf(stderr, "Unable to create CPU object. " + "Aborting."); + exit(1); + } } debug("\n"); @@ -1012,8 +484,6 @@ m->cpus[m->ncpus] = cpu_new(m->memory, m, 0 /* use 0 here to show info with debug() */, "Allegrex" /* TODO */); - if (m->bintrans_enable) - bintrans_init_cpu(m->cpus[m->ncpus]); debug("\n"); m->ncpus ++; } @@ -1185,20 +655,6 @@ } } - /* Special things required _before_ loading the file: */ - switch (m->arch) { - case ARCH_X86: - /* - * X86 machines normally don't need to load any files, - * they can boot from disk directly. Therefore, an x86 - * machine usually boots up in 16-bit real mode. When - * loading a 32-bit (or even 64-bit) ELF, that's not - * very nice, hence this special case. - */ - pc_bios_simple_pmode_setup(cpu); - break; - } - byte_order = NO_BYTE_ORDER_OVERRIDE; /* @@ -1240,16 +696,7 @@ } break; - case ARCH_HPPA: - break; - - case ARCH_I960: - break; - - case ARCH_IA64: - break; - - case ARCH_M68K: + case ARCH_M88K: break; case ARCH_MIPS: @@ -1274,7 +721,7 @@ break; case ARCH_SH: - if (cpu->cd.sh.bits == 32) + if (cpu->cd.sh.cpu_type.bits == 32) cpu->pc &= 0xffffffffULL; cpu->pc &= ~1; break; @@ -1282,24 +729,6 @@ case ARCH_SPARC: break; - case ARCH_X86: - /* - * NOTE: The toc field is used to indicate an ELF32 - * or ELF64 load. - */ - switch (toc) { - case 0: /* 16-bit? TODO */ - cpu->pc &= 0xffffffffULL; - break; - case 1: /* 32-bit. */ - cpu->pc &= 0xffffffffULL; - break; - case 2: /* 64-bit: TODO */ - fatal("64-bit x86 load. TODO\n"); - exit(1); - } - break; - default: fatal("emul_machine_setup(): Internal error: " "Unimplemented arch %i\n", m->arch); @@ -1335,8 +764,7 @@ useremul_setup(cpu, n_load, load_names); /* Startup the bootstrap CPU: */ - cpu->bootstrap_cpu_flag = 1; - cpu->running = 1; + cpu->running = 1; /* ... or pause all CPUs, if start_paused is set: */ if (m->start_paused) { @@ -1355,10 +783,6 @@ symbol_recalc_sizes(&m->symbol_context); - if (m->max_random_cycles_per_chunk > 0) - debug("using random cycle chunks (1 to %i cycles)\n", - m->max_random_cycles_per_chunk); - /* Special hack for ARC/SGI emulation: */ if ((m->machine_type == MACHINE_ARC || m->machine_type == MACHINE_SGI) && m->prom_emulation) @@ -1369,48 +793,43 @@ case ARCH_ARM: /* ARM cpus aren't 64-bit: */ - debug("0x%08x", (int)entrypoint); + debug("0x%08"PRIx32, (uint32_t) entrypoint); break; case ARCH_AVR: /* Atmel AVR uses a 16-bit or 22-bit program counter: */ - debug("0x%04x", (int)entrypoint); + debug("0x%04x", (int) entrypoint); break; case ARCH_MIPS: if (cpu->is_32bit) { - debug("0x%08x", (int)m->cpus[ - m->bootstrap_cpu]->pc); + debug("0x%08"PRIx32, (uint32_t) + m->cpus[m->bootstrap_cpu]->pc); if (cpu->cd.mips.gpr[MIPS_GPR_GP] != 0) - debug(" (gp=0x%08x)", (int)m->cpus[ - m->bootstrap_cpu]->cd.mips.gpr[ + debug(" (gp=0x%08"PRIx32")", (uint32_t) + m->cpus[m->bootstrap_cpu]->cd.mips.gpr[ MIPS_GPR_GP]); } else { - debug("0x%016llx", (long long)m->cpus[ - m->bootstrap_cpu]->pc); + debug("0x%016"PRIx64, (uint64_t) + m->cpus[m->bootstrap_cpu]->pc); if (cpu->cd.mips.gpr[MIPS_GPR_GP] != 0) - debug(" (gp=0x%016llx)", (long long) + debug(" (gp=0x%016"PRIx64")", (uint64_t) cpu->cd.mips.gpr[MIPS_GPR_GP]); } break; case ARCH_PPC: if (cpu->cd.ppc.bits == 32) - debug("0x%08x", (int)entrypoint); + debug("0x%08"PRIx32, (uint32_t) entrypoint); else - debug("0x%016llx", (long long)entrypoint); - break; - - case ARCH_X86: - debug("0x%04x:0x%llx", cpu->cd.x86.s[X86_S_CS], - (long long)cpu->pc); + debug("0x%016"PRIx64, (uint64_t) entrypoint); break; default: if (cpu->is_32bit) - debug("0x%08x", (int)cpu->pc); + debug("0x%08"PRIx32, (uint32_t) cpu->pc); else - debug("0x%016llx", (long long)cpu->pc); + debug("0x%016"PRIx64, (uint64_t) cpu->pc); } debug("\n"); @@ -1470,7 +889,9 @@ /* Create a simple network: */ emul->net = net_init(emul, NET_INIT_FLAG_GATEWAY, - "10.0.0.0", 8, NULL, 0, 0); + NET_DEFAULT_IPV4_MASK, + NET_DEFAULT_IPV4_LEN, + NULL, 0, 0, NULL); } else { /* Userland pseudo-machine: */ debug("Syscall emulation (userland-only) setup...\n"); @@ -1489,36 +910,16 @@ * * Create an emul struct by reading settings from a configuration file. */ -struct emul *emul_create_from_configfile(char *fname) +struct emul *emul_create_from_configfile(char *fname, int id) { int iadd = DEBUG_INDENTATION; - struct emul *e = emul_new(fname); - FILE *f; - char buf[128]; - size_t len; + struct emul *e = emul_new(fname, id); debug("Creating emulation from configfile \"%s\":\n", fname); debug_indentation(iadd); - f = fopen(fname, "r"); - if (f == NULL) { - perror(fname); - exit(1); - } - - /* Read header: (must be !!gxemul) */ - len = fread(buf, 1, 8, f); - if (len != 8 || strncmp(buf, "!!gxemul", 8) != 0) { - fprintf(stderr, "%s: must start with '!!gxemul'\n", fname); - exit(1); - } - - /* Restart from beginning: */ - rewind(f); + emul_parse_config(e, fname); - emul_parse_config(e, f); - - fclose(f); debug_indentation(-iadd); return e; } @@ -1599,6 +1000,9 @@ cpu_functioncall_trace(emuls[0]->machines[0]->cpus[0], emuls[0]->machines[0]->cpus[0]->pc); + /* Start emulated clocks: */ + timer_start(); + /* * MAIN LOOP: * @@ -1608,23 +1012,55 @@ while (go) { go = 0; - x11_check_event(emuls, n_emuls); + /* Flush X11 and serial console output every now and then: */ + if (emuls[0]->machines[0]->ninstrs > + emuls[0]->machines[0]->ninstrs_flush + (1<<19)) { + x11_check_event(emuls, n_emuls); + console_flush(); + emuls[0]->machines[0]->ninstrs_flush = + emuls[0]->machines[0]->ninstrs; + } + + if (emuls[0]->machines[0]->ninstrs > + emuls[0]->machines[0]->ninstrs_show + (1<<25)) { + emuls[0]->machines[0]->ninstrs_since_gettimeofday += + (emuls[0]->machines[0]->ninstrs - + emuls[0]->machines[0]->ninstrs_show); + cpu_show_cycles(emuls[0]->machines[0], 0); + emuls[0]->machines[0]->ninstrs_show = + emuls[0]->machines[0]->ninstrs; + } + + if (single_step == ENTER_SINGLE_STEPPING) { + /* TODO: Cleanup! */ + old_instruction_trace = + emuls[0]->machines[0]->instruction_trace; + old_quiet_mode = quiet_mode; + old_show_trace_tree = + emuls[0]->machines[0]->show_trace_tree; + emuls[0]->machines[0]->instruction_trace = 1; + emuls[0]->machines[0]->show_trace_tree = 1; + quiet_mode = 0; + single_step = SINGLE_STEPPING; + } + + if (single_step == SINGLE_STEPPING) + debugger(); for (i=0; in_machines; j++) { - /* TODO: cpu_run() is a strange name, since - there can be multiple cpus in a machine */ - anything = cpu_run(e, e->machines[j]); + anything = machine_run(e->machines[j]); if (anything) go = 1; } } } + /* Stop any running timers: */ + timer_stop(); + /* Deinitialize all CPUs in all machines in all emulations: */ for (i=0; in_machines; j++) @@ -1651,11 +1087,11 @@ printf("Press enter to quit.\n"); while (!console_charavail(MAIN_CONSOLE)) { x11_check_event(emuls, n_emuls); - usleep(1); + usleep(10000); } console_readchar(MAIN_CONSOLE); } - console_deinit(); + console_deinit_main(); }