--- trunk/src/emul.c 2007/10/08 16:18:38 12 +++ trunk/src/emul.c 2007/10/08 16:19:37 22 @@ -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.225 2005/08/14 19:35:54 debug Exp $ + * $Id: emul.c,v 1.247 2006/02/04 12:27:12 debug Exp $ * * Emulation startup and misc. routines. */ @@ -70,6 +70,15 @@ static char *diskimage_types[] = DISKIMAGE_TYPES; +static void print_separator(void) +{ + int i = 79; + while (i-- > 0) + debug("-"); + debug("\n"); +} + + /* * add_dump_points(): * @@ -112,11 +121,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 +161,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 +170,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 +216,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 +237,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 +309,7 @@ } for (;;) { - int len, i; + size_t len, i; /* Too close to another sector? Then realign. */ if ((dirofs & 2047) + 70 > 2047) { @@ -361,9 +374,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 +389,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 +398,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 +421,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; @@ -435,6 +447,70 @@ /* + * 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; +} + + +/* * load_bootblock(): * * For some emulation modes, it is possible to boot from a harddisk image by @@ -459,7 +535,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 @@ -480,7 +556,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 && @@ -491,7 +567,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; @@ -512,10 +588,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; @@ -586,7 +662,7 @@ /* * Try reading a kernel manually from the disk. The code here - * does not rely on machine-dependant boot blocks etc. + * does not rely on machine-dependent boot blocks etc. */ /* ISO9660: (0x800 bytes at 0x8000) */ bootblock_buf = malloc(0x800); @@ -623,6 +699,30 @@ 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; } @@ -812,14 +912,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); @@ -972,6 +1069,7 @@ } diskimage_dump_info(m); + console_debug_dump(m); /* Load files (ROM code, boot code, ...) into memory: */ if (n_load == 0) { @@ -1120,9 +1218,42 @@ 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_HPPA: + break; + + case ARCH_I960: + break; + + case ARCH_IA64: + 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; @@ -1138,27 +1269,19 @@ 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_ALPHA: - /* For position-independant code: */ - cpu->cd.alpha.r[ALPHA_T12] = cpu->pc; + case ARCH_SH: + if (cpu->cd.sh.bits == 32) + cpu->pc &= 0xffffffffULL; + cpu->pc &= ~1; break; case ARCH_SPARC: break; - case ARCH_IA64: - break; - - case ARCH_M68K: - break; - - case ARCH_ARM: - cpu->pc &= 0xfffffffc; - cpu->cd.arm.r[ARM_PC] = cpu->pc; - break; - case ARCH_X86: /* * NOTE: The toc field is used to indicate an ELF32 @@ -1225,7 +1348,7 @@ 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); @@ -1243,6 +1366,17 @@ debug("starting cpu%i at ", m->bootstrap_cpu); switch (m->arch) { + + case ARCH_ARM: + /* ARM cpus aren't 64-bit: */ + debug("0x%08x", (int)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->is_32bit) { debug("0x%08x", (int)m->cpus[ @@ -1259,22 +1393,24 @@ cpu->cd.mips.gpr[MIPS_GPR_GP]); } break; + case ARCH_PPC: if (cpu->cd.ppc.bits == 32) debug("0x%08x", (int)entrypoint); else debug("0x%016llx", (long long)entrypoint); break; - case ARCH_ARM: - /* ARM cpus aren't 64-bit: */ - debug("0x%08x", (int)entrypoint); - break; + case ARCH_X86: debug("0x%04x:0x%llx", cpu->cd.x86.s[X86_S_CS], (long long)cpu->pc); 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"); @@ -1289,7 +1425,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); @@ -1318,7 +1454,7 @@ */ void emul_simple_init(struct emul *emul) { - int iadd=4; + int iadd = DEBUG_INDENTATION; struct machine *m; if (emul->n_machines != 1) { @@ -1355,7 +1491,7 @@ */ 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]; @@ -1409,14 +1545,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.