--- trunk/src/emul.c 2007/10/08 16:17:48 2 +++ trunk/src/emul.c 2007/10/08 16:21:53 38 @@ -1,5 +1,5 @@ /* - * Copyright (C) 2003-2005 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.179 2005/03/14 19:14:04 debug Exp $ + * $Id: emul.c,v 1.282 2007/04/11 15:15:31 debug Exp $ * * Emulation startup and misc. routines. */ @@ -39,35 +39,49 @@ #include #include "arcbios.h" -#include "bintrans.h" #include "cpu.h" -#include "cpu_mips.h" #include "emul.h" #include "console.h" #include "debugger.h" #include "device.h" #include "diskimage.h" +#include "exec_elf.h" #include "machine.h" #include "memory.h" #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 void print_separator(void) +{ + int i = 79; + while (i-- > 0) + debug("-"); + debug("\n"); +} + + /* * add_dump_points(): * @@ -94,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; } @@ -110,11 +124,14 @@ * were automatically converted into the correct address. */ - if ((dp >> 32) == 0 && ((dp >> 31) & 1)) - dp |= 0xffffffff00000000ULL; + if (m->arch == ARCH_MIPS) { + if ((dp >> 32) == 0 && ((dp >> 31) & 1)) + dp |= 0xffffffff00000000ULL; + } + m->breakpoint_addr[i] = dp; - debug("breakpoint %i: 0x%016llx", i, (long long)dp); + debug("breakpoint %i: 0x%llx", i, (long long)dp); if (string_flag) debug(" (%s)", m->breakpoint_string[i]); debug("\n"); @@ -127,128 +144,7 @@ */ static void fix_console(void) { - console_deinit(); -} - - -/* - * 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. - */ -static void load_bootblock(struct machine *m, struct cpu *cpu) -{ - int boot_disk_id; - unsigned char minibuf[0x20]; - unsigned char *bootblock_buf; - uint64_t bootblock_offset; - uint64_t bootblock_loadaddr, bootblock_pc; - int n_blocks, res, readofs; - - boot_disk_id = diskimage_bootdev(m); - if (boot_disk_id < 0) - return; - - switch (m->machine_type) { - case MACHINE_DEC: - /* - * 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, 0, 0, - minibuf, sizeof(minibuf)); - - bootblock_loadaddr = minibuf[0x10] + (minibuf[0x11] << 8) - + (minibuf[0x12] << 16) + (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) + (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, 0, readofs, - minibuf, sizeof(minibuf)); - if (!res) { - printf("couldn't read disk?\n"); - exit(1); - } - - n_blocks = minibuf[0] + (minibuf[1] << 8) - + (minibuf[2] << 16) + (minibuf[3] << 24); - - bootblock_offset = (minibuf[4] + (minibuf[5] << 8) - + (minibuf[6] << 16) + (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, 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"); - break; - default: - fatal("Booting from disk without a separate kernel " - "doesn't work in this emulation mode.\n"); - exit(1); - } + console_deinit_main(); } @@ -257,7 +153,7 @@ * * Returns a reasonably initialized struct emul. */ -struct emul *emul_new(char *name) +struct emul *emul_new(char *name, int id) { struct emul *e; e = malloc(sizeof(struct emul)); @@ -268,8 +164,24 @@ memset(e, 0, sizeof(struct emul)); + e->path = malloc(15); + if (e->path == NULL) { + fprintf(stderr, "out of memory\n"); + exit(1); + } + snprintf(e->path, 15, "emul[%i]", id); + + e->settings = settings_new(); + + settings_add(e->settings, "n_machines", 0, + SETTINGS_TYPE_INT, SETTINGS_FORMAT_DECIMAL, + (void *) &e->n_machines); + + /* TODO: More settings? */ + /* Sane default values: */ e->n_machines = 0; + e->next_serial_nr = 1; if (name != NULL) { e->name = strdup(name); @@ -277,6 +189,10 @@ fprintf(stderr, "out of memory in emul_new()\n"); exit(1); } + + settings_add(e->settings, "name", 0, + SETTINGS_TYPE_STRING, SETTINGS_FORMAT_STRING, + (void *) &e->name); } return e; @@ -284,6 +200,34 @@ /* + * emul_destroy(): + * + * Destroys a previously created emul object. + */ +void emul_destroy(struct emul *emul) +{ + int i; + + if (emul->name != NULL) { + settings_remove(emul->settings, "name"); + free(emul->name); + } + + for (i=0; in_machines; i++) + machine_destroy(emul->machines[i]); + + if (emul->machines != NULL) + free(emul->machines); + + /* Remove any remaining level-1 settings: */ + settings_remove_all(emul->settings); + settings_destroy(emul->settings); + + free(emul); +} + + +/* * emul_add_machine(): * * Calls machine_new(), adds the new machine into the emul struct, and @@ -294,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); @@ -306,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; } @@ -332,15 +285,21 @@ len += 1048576 * m->memory_offset_in_mb; - /* NOTE/TODO: magic 12MB end of load program area */ + /* + * NOTE/TODO: magic 12MB end of load program area + * + * Hm. This breaks the old FreeBSD/MIPS snapshots... + */ +#if 0 arcbios_add_memory_descriptor(cpu, 0x60000 + m->memory_offset_in_mb * 1048576, start-0x60000 - m->memory_offset_in_mb * 1048576, ARCBIOS_MEM_FreeMemory); +#endif arcbios_add_memory_descriptor(cpu, start, len, ARCBIOS_MEM_LoadedProgram); - scsicontroller = arcbios_get_scsicontroller(); + scsicontroller = arcbios_get_scsicontroller(m); if (scsicontroller == 0) return; @@ -388,25 +347,25 @@ snprintf(component_string, sizeof(component_string), "scsi(0)cdrom(%i)", d->id); - arcbios_add_string_to_component( + arcbios_add_string_to_component(m, component_string, scsidevice); snprintf(component_string, sizeof(component_string), "scsi(0)cdrom(%i)fdisk(0)", d->id); - arcbios_add_string_to_component( + arcbios_add_string_to_component(m, component_string, scsidisk); } else { snprintf(component_string, sizeof(component_string), "scsi(0)disk(%i)", d->id); - arcbios_add_string_to_component( + arcbios_add_string_to_component(m, component_string, scsidevice); snprintf(component_string, sizeof(component_string), "scsi(0)disk(%i)rdisk(0)", d->id); - arcbios_add_string_to_component( + arcbios_add_string_to_component(m, component_string, scsidisk); } } @@ -429,14 +388,11 @@ void emul_machine_setup(struct machine *m, int n_load, char **load_names, int n_devices, char **device_names) { - struct emul *emul; struct cpu *cpu; - int i, iadd=4; - uint64_t addr, memory_amount, entrypoint = 0, gp = 0, toc = 0; + int i, iadd = DEBUG_INDENTATION; + uint64_t memory_amount, entrypoint = 0, gp = 0, toc = 0; int byte_order; - emul = m->emul; - debug("machine \"%s\":\n", m->name); debug_indentation(iadd); @@ -457,6 +413,9 @@ m->cpu_family = cpu_family_ptr_by_number(m->arch); + if (m->arch == ARCH_ALPHA) + m->arch_pagesize = 8192; + machine_memsize_fix(m); /* @@ -477,7 +436,7 @@ debug(" (offset by %iMB)", m->memory_offset_in_mb); memory_amount += 1048576 * m->memory_offset_in_mb; } - m->memory = memory_new(memory_amount); + m->memory = memory_new(memory_amount, m->arch); if (m->machine_type != MACHINE_USERLAND) debug("\n"); @@ -504,21 +463,32 @@ } 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"); +#if 0 + /* Special case: The Playstation Portable has an additional CPU: */ + if (m->machine_type == MACHINE_PSP) { + debug("cpu%i: ", m->ncpus); + m->cpus[m->ncpus] = cpu_new(m->memory, m, + 0 /* use 0 here to show info with debug() */, + "Allegrex" /* TODO */); + debug("\n"); + m->ncpus ++; + } +#endif + if (m->use_random_bootstrap_cpu) m->bootstrap_cpu = random() % m->ncpus; else @@ -530,30 +500,32 @@ if (m->userland_emul != NULL) { useremul_name_to_useremul(cpu, m->userland_emul, NULL, NULL, NULL); - cpu->memory_rw = userland_memory_rw; + + switch (m->arch) { +#ifdef ENABLE_ALPHA + case ARCH_ALPHA: + cpu->memory_rw = alpha_userland_memory_rw; + break; +#endif + default:cpu->memory_rw = userland_memory_rw; + } } if (m->use_x11) x11_init(m); /* Fill memory with random bytes: */ - /* TODO: This is MIPS-specific! */ if (m->random_mem_contents) { for (i=0; iphysical_ram_in_mb * 1048576; i+=256) { unsigned char data[256]; unsigned int j; for (j=0; jmemory_rw(cpu, m->memory, addr, data, sizeof(data), - MEM_WRITE, CACHE_NONE | NO_EXCEPTIONS); + cpu->memory_rw(cpu, m->memory, i, data, sizeof(data), + MEM_WRITE, CACHE_NONE | NO_EXCEPTIONS | PHYSICAL); } } - if ((m->machine_type == MACHINE_ARC || - m->machine_type == MACHINE_SGI) && m->prom_emulation) - arcbios_init(); - if (m->userland_emul != NULL) { /* * For userland-only emulation, no machine emulation @@ -567,12 +539,18 @@ } diskimage_dump_info(m); + console_debug_dump(m); /* Load files (ROM code, boot code, ...) into memory: */ if (n_load == 0) { - if (m->first_diskimage != NULL) - load_bootblock(m, cpu); - else { + if (m->first_diskimage != NULL) { + if (!load_bootblock(m, cpu, &n_load, &load_names)) { + fprintf(stderr, "\nNo executable files were" + " specified, and booting directly from disk" + " failed.\n"); + exit(1); + } + } else { fprintf(stderr, "No executable file(s) loaded, and " "we are not booting directly from a disk image." "\nAborting.\n"); @@ -581,20 +559,148 @@ } while (n_load > 0) { + FILE *tmp_f; + char *name_to_load = *load_names; + int remove_after_load = 0; + + /* Special hack for removing temporary files: */ + if (name_to_load[0] == 8) { + name_to_load ++; + remove_after_load = 1; + } + + /* + * gzipped files are automagically gunzipped: + * NOTE/TODO: This isn't secure. system() is used. + */ + tmp_f = fopen(name_to_load, "r"); + if (tmp_f != NULL) { + unsigned char buf[2]; /* gzip header */ + memset(buf, 0, sizeof(buf)); + fread(buf, 1, sizeof(buf), tmp_f); + if (buf[0]==0x1f && buf[1]==0x8b) { + size_t zzlen = strlen(name_to_load)*2 + 100; + char *zz = malloc(zzlen); + debug("gunziping %s\n", name_to_load); + /* + * gzip header found. If this was a file + * extracted from, say, a CDROM image, then it + * already has a temporary name. Otherwise we + * have to gunzip into a temporary file. + */ + if (remove_after_load) { + snprintf(zz, zzlen, "mv %s %s.gz", + name_to_load, name_to_load); + system(zz); + snprintf(zz, zzlen, "gunzip %s.gz", + name_to_load); + system(zz); + } else { + /* gunzip into new temp file: */ + int tmpfile_handle; + char *new_temp_name = + strdup("/tmp/gxemul.XXXXXXXXXXXX"); + tmpfile_handle = mkstemp(new_temp_name); + close(tmpfile_handle); + snprintf(zz, zzlen, "gunzip -c '%s' > " + "%s", name_to_load, new_temp_name); + system(zz); + name_to_load = new_temp_name; + remove_after_load = 1; + } + free(zz); + } + fclose(tmp_f); + } + + /* + * Ugly (but usable) hack for Playstation Portable: If the + * filename ends with ".pbp" and the file contains an ELF + * header, then extract the ELF file into a temporary file. + */ + if (strlen(name_to_load) > 4 && strcasecmp(name_to_load + + strlen(name_to_load) - 4, ".pbp") == 0 && + (tmp_f = fopen(name_to_load, "r")) != NULL) { + off_t filesize, j, found=0; + unsigned char *buf; + fseek(tmp_f, 0, SEEK_END); + filesize = ftello(tmp_f); + fseek(tmp_f, 0, SEEK_SET); + buf = malloc(filesize); + if (buf == NULL) { + fprintf(stderr, "out of memory while trying" + " to read %s\n", name_to_load); + exit(1); + } + fread(buf, 1, filesize, tmp_f); + fclose(tmp_f); + /* Search for the ELF header, from offset 1 (!): */ + for (j=1; jmemory, *load_names, &entrypoint, + /* + * Load the file: :-) + */ + file_load(m, m->memory, name_to_load, &entrypoint, m->arch, &gp, &byte_order, &toc); + if (remove_after_load) { + debug("removing %s\n", name_to_load); + unlink(name_to_load); + } + if (byte_order != NO_BYTE_ORDER_OVERRIDE) cpu->byte_order = byte_order; cpu->pc = entrypoint; switch (m->arch) { + + case ARCH_ALPHA: + /* For position-independent code: */ + cpu->cd.alpha.r[ALPHA_T12] = cpu->pc; + break; + + case ARCH_ARM: + if (cpu->pc & 3) { + fatal("ARM: lowest bits of pc set: TODO\n"); + exit(1); + } + cpu->pc &= 0xfffffffc; + break; + + case ARCH_AVR: + cpu->pc &= 0xfffff; + if (cpu->pc & 1) { + fatal("AVR: lowest bit of pc set: TODO\n"); + exit(1); + } + break; + + case ARCH_M68K: + break; + case ARCH_MIPS: - if ((cpu->pc >> 32) == 0 - && (cpu->pc & 0x80000000ULL)) + if ((cpu->pc >> 32) == 0 && (cpu->pc & 0x80000000ULL)) cpu->pc |= 0xffffffff00000000ULL; cpu->cd.mips.gpr[MIPS_GPR_GP] = gp; @@ -604,17 +710,25 @@ cpu->cd.mips.gpr[MIPS_GPR_GP] |= 0xffffffff00000000ULL; break; + case ARCH_PPC: + /* See http://www.linuxbase.org/spec/ELF/ppc64/ + spec/x458.html for more info. */ cpu->cd.ppc.gpr[2] = toc; + /* TODO */ + if (cpu->cd.ppc.bits == 32) + cpu->pc &= 0xffffffffULL; break; - case ARCH_SPARC: - break; - case ARCH_URISC: - break; - case ARCH_HPPA: + + case ARCH_SH: + if (cpu->cd.sh.cpu_type.bits == 32) + cpu->pc &= 0xffffffffULL; + cpu->pc &= ~1; break; - case ARCH_ALPHA: + + case ARCH_SPARC: break; + default: fatal("emul_machine_setup(): Internal error: " "Unimplemented arch %i\n", m->arch); @@ -650,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) { @@ -663,17 +776,13 @@ add_dump_points(m); /* TODO: This is MIPS-specific! */ - if (m->machine_type == MACHINE_DEC && + if (m->machine_type == MACHINE_PMAX && cpu->cd.mips.cpu_type.mmu_model == MMU3K) add_symbol_name(&m->symbol_context, - 0x9fff0000, 0x10000, "r2k3k_cache", 0); + 0x9fff0000, 0x10000, "r2k3k_cache", 0, 0); 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) @@ -681,54 +790,46 @@ debug("starting cpu%i at ", m->bootstrap_cpu); switch (m->arch) { + + case ARCH_ARM: + /* ARM cpus aren't 64-bit: */ + 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); + break; + case ARCH_MIPS: - if (cpu->cd.mips.cpu_type.isa_level < 3 || - cpu->cd.mips.cpu_type.isa_level == 32) { - debug("0x%08x", (int)m->cpus[ - m->bootstrap_cpu]->pc); + if (cpu->is_32bit) { + 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_URISC: - { - char tmps[100]; - unsigned char buf[sizeof(uint64_t)]; - - cpu->memory_rw(cpu, m->memory, 0, buf, sizeof(buf), - MEM_READ, CACHE_NONE | NO_EXCEPTIONS); - - entrypoint = 0; - for (i=0; icd.urisc.wordlen/8; i++) { - entrypoint <<= 8; - if (cpu->byte_order == EMUL_BIG_ENDIAN) - entrypoint += buf[i]; - else - entrypoint += buf[cpu-> - cd.urisc.wordlen/8 - 1 - i]; - } - - sprintf(tmps, "0x%%0%illx", cpu->cd.urisc.wordlen / 4); - debug(tmps, (long long)entrypoint); - cpu->pc = entrypoint; - } + debug("0x%016"PRIx64, (uint64_t) entrypoint); break; + default: - debug("0x%016llx", (long long)entrypoint); + if (cpu->is_32bit) + debug("0x%08"PRIx32, (uint32_t) cpu->pc); + else + debug("0x%016"PRIx64, (uint64_t) cpu->pc); } debug("\n"); @@ -743,7 +844,7 @@ */ void emul_dumpinfo(struct emul *e) { - int j, nm, iadd = 4; + int j, nm, iadd = DEBUG_INDENTATION; if (e->net != NULL) net_dumpinfo(e->net); @@ -772,7 +873,7 @@ */ void emul_simple_init(struct emul *emul) { - int iadd=4; + int iadd = DEBUG_INDENTATION; struct machine *m; if (emul->n_machines != 1) { @@ -786,9 +887,11 @@ debug("Simple setup...\n"); debug_indentation(iadd); - /* Create a network: */ + /* Create a simple network: */ emul->net = net_init(emul, NET_INIT_FLAG_GATEWAY, - "10.0.0.0", 8); + NET_DEFAULT_IPV4_MASK, + NET_DEFAULT_IPV4_LEN, + NULL, 0, 0, NULL); } else { /* Userland pseudo-machine: */ debug("Syscall emulation (userland-only) setup...\n"); @@ -807,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 = 4; - struct emul *e = emul_new(fname); - FILE *f; - char buf[128]; - size_t len; + int iadd = DEBUG_INDENTATION; + 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; } @@ -863,14 +946,28 @@ atexit(fix_console); - i = 79; - while (i-- > 0) - debug("-"); - debug("\n\n"); - /* Initialize the interactive debugger: */ debugger_init(emuls, n_emuls); + /* Run any additional debugger commands before starting: */ + for (i=0; in_debugger_cmds > 0) { + int j; + if (i == 0) + print_separator(); + for (j = 0; j < emul->n_debugger_cmds; j ++) { + debug("> %s\n", emul->debugger_cmds[j]); + debugger_execute_cmd(emul->debugger_cmds[j], + strlen(emul->debugger_cmds[j])); + } + } + } + + print_separator(); + debug("\n"); + + /* * console_init_main() makes sure that the terminal is in a * reasonable state. @@ -895,9 +992,17 @@ if (e == NULL) continue; for (j=0; jn_machines; j++) - cpu_run_init(e, e->machines[j]); + cpu_run_init(e->machines[j]); } + /* TODO: Generalize: */ + if (emuls[0]->machines[0]->show_trace_tree) + cpu_functioncall_trace(emuls[0]->machines[0]->cpus[0], + emuls[0]->machines[0]->cpus[0]->pc); + + /* Start emulated clocks: */ + timer_start(); + /* * MAIN LOOP: * @@ -907,30 +1012,62 @@ 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++) - cpu_run_deinit(e, e->machines[j]); + cpu_run_deinit(e->machines[j]); } /* force_debugger_at_exit flag set? Then enter the debugger: */ @@ -940,7 +1077,7 @@ debugger(); } - /* Any machine using X11? Then we should wait before exiting: */ + /* Any machine using X11? Then wait before exiting: */ n = 0; for (i=0; in_machines; j++) @@ -950,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(); }