--- trunk/src/emul.c 2007/10/08 16:19:11 18 +++ trunk/src/emul.c 2007/10/08 16:19:56 24 @@ -1,5 +1,5 @@ /* - * Copyright (C) 2003-2005 Anders Gavare. All rights reserved. + * Copyright (C) 2003-2006 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.235 2005/10/26 14:37:02 debug Exp $ + * $Id: emul.c,v 1.254 2006/06/22 13:22:40 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" @@ -70,6 +69,15 @@ static char *diskimage_types[] = DISKIMAGE_TYPES; +static void print_separator(void) +{ + int i = 79; + while (i-- > 0) + debug("-"); + debug("\n"); +} + + /* * add_dump_points(): * @@ -96,12 +104,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; } @@ -112,11 +120,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"); @@ -149,7 +160,7 @@ int *n_loadp, char ***load_namesp) { char str[35]; - int filenr, i, ofs, dirlen, res = 0, res2, iadd = 4; + int filenr, i, ofs, dirlen, res = 0, res2, iadd = DEBUG_INDENTATION; int found_dir; uint64_t dirofs; uint64_t fileofs, filelen; @@ -158,7 +169,7 @@ char *p, *filename_orig; char *filename = strdup(cpu->machine->boot_kernel_filename); unsigned char *filebuf = NULL; - char *tmpfilename = NULL; + char *tmpfname = NULL; char **new_array; int tmpfile_handle; @@ -204,7 +215,7 @@ fatal("WARNING: Root directory length mismatch?\n"); dirofs = (int64_t)(buf[0x8c] + (buf[0x8d] << 8) + (buf[0x8e] << 16) + - (buf[0x8f] << 24)) * 2048; + ((uint64_t)buf[0x8f] << 24)) * 2048; /* debug("root = %i bytes at 0x%llx\n", dirlen, (long long)dirofs); */ @@ -225,8 +236,9 @@ dp = dirbuf; filenr = 1; p = NULL; while (dp < dirbuf + dirlen) { - int i, nlen = dp[0]; - int x = dp[2] + (dp[3] << 8) + (dp[4] << 16) + (dp[5] << 24); + 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]; @@ -296,7 +308,7 @@ } for (;;) { - int len, i; + size_t len, i; /* Too close to another sector? Then realign. */ if ((dirofs & 2047) + 70 > 2047) { @@ -361,9 +373,9 @@ } fileofs = match_entry[2] + (match_entry[3] << 8) + - (match_entry[4] << 16) + (match_entry[5] << 24); + (match_entry[4] << 16) + ((uint64_t)match_entry[5] << 24); filelen = match_entry[10] + (match_entry[11] << 8) + - (match_entry[12] << 16) + (match_entry[13] << 24); + (match_entry[12] << 16) + ((uint64_t)match_entry[13] << 24); fileofs *= 2048; /* debug("filelen=%llx fileofs=%llx\n", (long long)filelen, @@ -376,10 +388,7 @@ goto ret; } - tmpfilename = strdup("/tmp/gxemul.XXXXXXXXXXXX"); - - debug("extracting %lli bytes into %s\n", - (long long)filelen, tmpfilename); + tmpfname = strdup("/tmp/gxemul.XXXXXXXXXXXX"); res2 = diskimage_access(m, disk_id, disk_type, 0, fileofs, filebuf, filelen); @@ -388,14 +397,16 @@ goto ret; } - tmpfile_handle = mkstemp(tmpfilename); + tmpfile_handle = mkstemp(tmpfname); if (tmpfile_handle < 0) { - fatal("could not create %s\n", tmpfilename); + fatal("could not create %s\n", tmpfname); exit(1); } write(tmpfile_handle, filebuf, filelen); close(tmpfile_handle); + debug("extracted %lli bytes into %s\n", (long long)filelen, tmpfname); + /* Add the temporary filename to the load_namesp array: */ (*n_loadp)++; new_array = malloc(sizeof(char *) * (*n_loadp)); @@ -409,11 +420,11 @@ /* 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. */ - tmpfilename = realloc(tmpfilename, strlen(tmpfilename) + 2); - memmove(tmpfilename + 1, tmpfilename, strlen(tmpfilename) + 1); - tmpfilename[0] = 8; + tmpfname = realloc(tmpfname, strlen(tmpfname) + 2); + memmove(tmpfname + 1, tmpfname, strlen(tmpfname) + 1); + tmpfname[0] = 8; - (*load_namesp)[*n_loadp - 1] = tmpfilename; + (*load_namesp)[*n_loadp - 1] = tmpfname; res = 1; @@ -466,9 +477,9 @@ int ofs = 0x200 * (partnr + 1); if (partnr == 0) n_partitions = buf[ofs + 7]; - start = (buf[ofs + 8] << 24) + (buf[ofs + 9] << 16) + + start = ((uint64_t)buf[ofs + 8] << 24) + (buf[ofs + 9] << 16) + (buf[ofs + 10] << 8) + buf[ofs + 11]; - length = (buf[ofs + 12] << 24) + (buf[ofs + 13] << 16) + + 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", @@ -523,7 +534,7 @@ return 0; switch (m->machine_type) { - case MACHINE_DEC: + case MACHINE_PMAX: /* * The first few bytes of a disk contains information about * where the bootblock(s) are located. (These are all 32-bit @@ -544,7 +555,7 @@ minibuf, sizeof(minibuf)); bootblock_loadaddr = minibuf[0x10] + (minibuf[0x11] << 8) - + (minibuf[0x12] << 16) + (minibuf[0x13] << 24); + + (minibuf[0x12] << 16) + ((uint64_t)minibuf[0x13] << 24); /* Convert loadaddr to uncached: */ if ((bootblock_loadaddr & 0xf0000000ULL) != 0x80000000 && @@ -555,7 +566,7 @@ bootblock_loadaddr |= 0xffffffffa0000000ULL; bootblock_pc = minibuf[0x14] + (minibuf[0x15] << 8) - + (minibuf[0x16] << 16) + (minibuf[0x17] << 24); + + (minibuf[0x16] << 16) + ((uint64_t)minibuf[0x17] << 24); bootblock_pc &= 0x0fffffffULL; bootblock_pc |= 0xffffffffa0000000ULL; @@ -576,10 +587,10 @@ } n_blocks = minibuf[0] + (minibuf[1] << 8) - + (minibuf[2] << 16) + (minibuf[3] << 24); + + (minibuf[2] << 16) + ((uint64_t)minibuf[3] << 24); - bootblock_offset = (minibuf[4] + (minibuf[5] << 8) - + (minibuf[6] << 16) + (minibuf[7] << 24)) * 512; + bootblock_offset = (minibuf[4] + (minibuf[5] << 8) + + (minibuf[6]<<16) + ((uint64_t)minibuf[7]<<24)) * 512; if (n_blocks < 1) break; @@ -900,14 +911,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; + 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); @@ -931,9 +939,6 @@ if (m->arch == ARCH_ALPHA) m->arch_pagesize = 8192; - if (m->arch != ARCH_MIPS) - m->bintrans_enable = 0; - machine_memsize_fix(m); /* @@ -981,18 +986,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"); @@ -1003,8 +1007,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 ++; } @@ -1060,6 +1062,7 @@ } diskimage_dump_info(m); + console_debug_dump(m); /* Load files (ROM code, boot code, ...) into memory: */ if (n_load == 0) { @@ -1215,8 +1218,11 @@ break; case ARCH_ARM: + if (cpu->pc & 3) { + fatal("ARM: lowest bits of pc set: TODO\n"); + exit(1); + } cpu->pc &= 0xfffffffc; - cpu->cd.arm.r[ARM_PC] = cpu->pc; break; case ARCH_AVR: @@ -1240,8 +1246,7 @@ 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; @@ -1336,17 +1341,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, 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) @@ -1395,7 +1396,10 @@ break; default: - debug("0x%016llx", (long long)cpu->pc); + if (cpu->is_32bit) + debug("0x%08x", (int)cpu->pc); + else + debug("0x%016llx", (long long)cpu->pc); } debug("\n"); @@ -1410,7 +1414,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); @@ -1439,7 +1443,7 @@ */ void emul_simple_init(struct emul *emul) { - int iadd=4; + int iadd = DEBUG_INDENTATION; struct machine *m; if (emul->n_machines != 1) { @@ -1476,34 +1480,14 @@ */ struct emul *emul_create_from_configfile(char *fname) { - int iadd = 4; + int iadd = DEBUG_INDENTATION; struct emul *e = emul_new(fname); - FILE *f; - char buf[128]; - size_t len; 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, f); + emul_parse_config(e, fname); - fclose(f); debug_indentation(-iadd); return e; } @@ -1530,14 +1514,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. @@ -1587,6 +1585,10 @@ continue; for (j=0; jn_machines; j++) { + if (e->machines[j]->gdb.port > 0) + debugger_gdb_check_incoming( + e->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]);