--- trunk/src/emul.c 2007/10/08 16:18:00 4 +++ trunk/src/emul.c 2007/10/08 16:21:17 34 @@ -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.184 2005/04/20 04:43:52 debug Exp $ + * $Id: emul.c,v 1.278 2007/02/10 14:37:38 debug Exp $ * * Emulation startup and misc. routines. */ @@ -39,33 +39,53 @@ #include #include "arcbios.h" -#include "bintrans.h" #include "cpu.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; +/* #define ISO_DEBUG */ + 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) +{ + int i = 79; + while (i-- > 0) + debug("-"); + debug("\n"); +} + /* * add_dump_points(): @@ -93,12 +113,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; } @@ -109,11 +129,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"); @@ -126,7 +149,382 @@ */ 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. + * + * 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; + +#ifdef ISO_DEBUG + debug("root = %i bytes at 0x%llx\n", dirlen, (long long)dirofs); +#endif + + 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, '\\'); + +#ifdef ISO_DEBUG + debug("%i%s: %i, %i, \"", filenr, filenr == found_dir? + " [CURRENT]" : "", x, y); +#endif + 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; } @@ -137,22 +535,82 @@ * 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_offset, base_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; + + base_offset = diskimage_get_baseoffset(m, boot_disk_id, boot_disk_type); switch (m->machine_type) { - case MACHINE_DEC: + + case MACHINE_DREAMCAST: + if (!diskimage_is_a_cdrom(cpu->machine, boot_disk_id, + boot_disk_type)) { + fatal("The Dreamcast emulation mode can only boot" + " from CD images, not from other disk types.\n"); + exit(1); + } + + bootblock_buf = malloc(32768); + if (bootblock_buf == NULL) { + fprintf(stderr, "Out of memory.\n"); + exit(1); + } + + debug("loading Dreamcast IP.BIN from %s id %i\n", + diskimage_types[boot_disk_type], boot_disk_id); + + res = diskimage_access(m, boot_disk_id, boot_disk_type, + 0, base_offset, bootblock_buf, 0x8000); + if (!res) { + fatal("Couldn't read the disk image. Aborting.\n"); + return 0; + } + + if (strncmp((char *)bootblock_buf, "SEGA ", 5) != 0) { + fatal("This is not a Dreamcast IP.BIN header.\n"); + free(bootblock_buf); + return 0; + } + + /* Store IP.BIN at 0x8c008000, and set entry point. */ + store_buf(cpu, 0x8c008000, (char *)bootblock_buf, 32768); + cpu->pc = 0x8c008300; + + /* Remember the name of the file to boot (1ST_READ.BIN): */ + if (cpu->machine->boot_kernel_filename == NULL || + cpu->machine->boot_kernel_filename[0] == '\0') { + int i = 0x60; + while (i < 0x70) { + if (bootblock_buf[i] == ' ') + bootblock_buf[i] = 0; + i ++; + } + cpu->machine->boot_kernel_filename = strdup( + (char *)bootblock_buf + 0x60); + } + + debug("boot filename: %s\n", + cpu->machine->boot_kernel_filename); + + free(bootblock_buf); + + break; + + case MACHINE_PMAX: /* * The first few bytes of a disk contains information about * where the bootblock(s) are located. (These are all 32-bit @@ -169,22 +627,31 @@ * 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 && - (bootblock_loadaddr & 0xf0000000ULL) != 0xa0000000) - fatal("\nWARNING! Weird load address 0x%08x.\n\n", - (int)bootblock_loadaddr); + (bootblock_loadaddr & 0xf0000000ULL) != 0xa0000000) { + fatal("\nWARNING! Weird load address 0x%08"PRIx32 + " for SCSI id %i.\n\n", + (uint32_t)bootblock_loadaddr, boot_disk_id); + if (bootblock_loadaddr == 0) { + fatal("I'm assuming that this is _not_ a " + "DEC bootblock.\nAre you sure you are" + " booting from the correct disk?\n"); + exit(1); + } + } + bootblock_loadaddr &= 0x0fffffffULL; 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; @@ -196,18 +663,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; @@ -225,8 +693,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", @@ -242,38 +710,75 @@ } debug(readofs == 0x18? ": no blocks?\n" : " blocks\n"); - break; + return 1; + } - case MACHINE_X86: - cpu->cd.x86.mode = 16; - cpu->pc = 0x7c00; - bootblock_buf = malloc(512); - if (bootblock_buf == NULL) { - fprintf(stderr, "Out of memory.\n"); - exit(1); - } - - res = diskimage_access(m, boot_disk_id, 0, 0, - bootblock_buf, 512); - if (!res) { - printf("Couldn't read the disk image. Aborting.\n"); - exit(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 + base_offset) */ + bootblock_buf = malloc(0x800); + if (bootblock_buf == NULL) { + fprintf(stderr, "Out of memory.\n"); + exit(1); + } - debug("loading PC bootsector from disk %i\n", boot_disk_id); - 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); - break; + res = diskimage_access(m, boot_disk_id, boot_disk_type, + 0, base_offset + 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; - default: - fatal("Booting from disk without a separate kernel " - "doesn't work in this emulation mode.\n"); - exit(1); + if (iso_type != 0) { + /* + * If the user specified a kernel name, then load it from + * disk. + */ + 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') { + 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; } @@ -282,7 +787,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)); @@ -293,8 +798,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); @@ -302,6 +823,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; @@ -309,6 +834,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 @@ -319,10 +872,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); @@ -331,7 +888,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; } @@ -357,15 +919,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; @@ -413,25 +981,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); } } @@ -454,14 +1022,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); @@ -482,6 +1047,9 @@ m->cpu_family = cpu_family_ptr_by_number(m->arch); + if (m->arch == ARCH_ALPHA) + m->arch_pagesize = 8192; + machine_memsize_fix(m); /* @@ -502,7 +1070,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"); @@ -529,21 +1097,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 @@ -555,30 +1134,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 @@ -592,12 +1173,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"); @@ -606,20 +1193,152 @@ } 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_RCA180X: + cpu->pc &= 0xffff; + 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; @@ -631,24 +1350,25 @@ 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_SH: + if (cpu->cd.sh.cpu_type.bits == 32) + cpu->pc &= 0xffffffffULL; + cpu->pc &= ~1; break; - case ARCH_ALPHA: - case ARCH_HPPA: case ARCH_SPARC: - case ARCH_URISC: break; - case ARCH_X86: - /* - * NOTE: The toc field is used to indicate an ELF64 - * load, on AMD64! - */ - if (toc != 0) { - cpu->cd.x86.mode = 64; - } else - cpu->pc &= 0xffffffffULL; + case ARCH_TRANSPUTER: + cpu->pc &= 0xffffffffULL; break; default: @@ -686,8 +1406,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) { @@ -699,17 +1418,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) @@ -717,9 +1432,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) @@ -734,46 +1459,19 @@ 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; - } - break; - case ARCH_X86: - if (cpu->cd.x86.mode == 16) - debug("0x%04x:0x%04x", cpu->cd.x86.s[X86_S_CS], - (int)cpu->pc); - else if (cpu->cd.x86.mode == 32) + + default: + if (cpu->is_32bit) debug("0x%08x", (int)cpu->pc); else debug("0x%016llx", (long long)cpu->pc); - break; - default: - debug("0x%016llx", (long long)cpu->pc); } debug("\n"); @@ -788,7 +1486,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); @@ -817,7 +1515,7 @@ */ void emul_simple_init(struct emul *emul) { - int iadd=4; + int iadd = DEBUG_INDENTATION; struct machine *m; if (emul->n_machines != 1) { @@ -831,9 +1529,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"); @@ -852,36 +1552,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; } @@ -908,14 +1588,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. @@ -940,9 +1634,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: * @@ -952,30 +1654,66 @@ 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]); + if (e->machines[j]->gdb.port > 0) + debugger_gdb_check_incoming( + 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: */ @@ -985,7 +1723,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++) @@ -995,11 +1733,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(); }