--- trunk/src/emul.c 2007/10/08 16:17:48 2 +++ 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.179 2005/03/14 19:14:04 debug Exp $ + * $Id: emul.c,v 1.254 2006/06/22 13:22:40 debug Exp $ * * Emulation startup and misc. routines. */ @@ -39,14 +39,13 @@ #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" @@ -67,6 +66,17 @@ extern struct emul *debugger_emul; extern struct diskimage *diskimages[]; +static char *diskimage_types[] = DISKIMAGE_TYPES; + + +static void print_separator(void) +{ + int i = 79; + while (i-- > 0) + debug("-"); + debug("\n"); +} + /* * add_dump_points(): @@ -94,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; } @@ -110,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"); @@ -132,28 +145,396 @@ /* + * 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. + * + * Returns 1 on success, 0 on failure. + */ +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) +{ + 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; i= 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 * 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 void load_bootblock(struct machine *m, struct cpu *cpu) +static int load_bootblock(struct machine *m, struct cpu *cpu, + int *n_loadp, char ***load_namesp) { - int boot_disk_id; + 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; - int n_blocks, res, readofs; - boot_disk_id = diskimage_bootdev(m); + boot_disk_id = diskimage_bootdev(m, &boot_disk_type); if (boot_disk_id < 0) - return; + 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 @@ -170,11 +551,11 @@ * 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, + 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) + (minibuf[0x13] << 24); + + (minibuf[0x12] << 16) + ((uint64_t)minibuf[0x13] << 24); /* Convert loadaddr to uncached: */ if ((bootblock_loadaddr & 0xf0000000ULL) != 0x80000000 && @@ -185,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; @@ -197,18 +578,19 @@ readofs = 0x18; for (;;) { - res = diskimage_access(m, boot_disk_id, 0, readofs, - minibuf, sizeof(minibuf)); + res = diskimage_access(m, boot_disk_id, boot_disk_type, + 0, readofs, minibuf, sizeof(minibuf)); if (!res) { - printf("couldn't read disk?\n"); - exit(1); + fatal("Couldn't read the disk image. " + "Aborting.\n"); + return 0; } 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; @@ -226,8 +608,8 @@ exit(1); } - res = diskimage_access(m, boot_disk_id, 0, - bootblock_offset, bootblock_buf, n_blocks * 512); + 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", @@ -243,12 +625,105 @@ } 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"); + return 1; + + case MACHINE_X86: + /* TODO: "El Torito" etc? */ + if (diskimage_is_a_cdrom(cpu->machine, boot_disk_id, + boot_disk_type)) + break; + + bootblock_buf = malloc(512); + if (bootblock_buf == NULL) { + fprintf(stderr, "Out of memory.\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); } + + 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; } @@ -270,6 +745,7 @@ /* Sane default values: */ e->n_machines = 0; + e->next_serial_nr = 1; if (name != NULL) { e->name = strdup(name); @@ -332,15 +808,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 +870,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 +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; - 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 +936,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 +959,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 +986,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 +1023,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 +1062,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 +1082,171 @@ } 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; jarch) { + 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; - file_load(m, m->memory, *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_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; @@ -604,17 +1256,43 @@ 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: + + case ARCH_SH: + if (cpu->cd.sh.bits == 32) + cpu->pc &= 0xffffffffULL; + cpu->pc &= ~1; break; - case ARCH_HPPA: + + case ARCH_SPARC: break; - case ARCH_ALPHA: + + 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); @@ -663,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); + 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,9 +1355,19 @@ 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->cd.mips.cpu_type.isa_level < 3 || - cpu->cd.mips.cpu_type.isa_level == 32) { + if (cpu->is_32bit) { debug("0x%08x", (int)m->cpus[ m->bootstrap_cpu]->pc); if (cpu->cd.mips.gpr[MIPS_GPR_GP] != 0) @@ -698,37 +1382,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_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; - } + + 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)entrypoint); + if (cpu->is_32bit) + debug("0x%08x", (int)cpu->pc); + else + debug("0x%016llx", (long long)cpu->pc); } debug("\n"); @@ -743,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); @@ -772,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) { @@ -786,9 +1457,9 @@ 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); + "10.0.0.0", 8, NULL, 0, 0); } else { /* Userland pseudo-machine: */ debug("Syscall emulation (userland-only) setup...\n"); @@ -809,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); - } + emul_parse_config(e, fname); - /* Restart from beginning: */ - rewind(f); - - emul_parse_config(e, f); - - fclose(f); debug_indentation(-iadd); return e; } @@ -863,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. @@ -895,9 +1560,14 @@ 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); + /* * MAIN LOOP: * @@ -915,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]); @@ -930,7 +1604,7 @@ if (e == NULL) continue; for (j=0; jn_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: */