--- trunk/src/emul.c 2007/10/08 16:18:00 4 +++ trunk/src/emul.c 2007/10/08 16:18:27 10 @@ -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.211 2005/06/26 11:36:28 debug Exp $ * * Emulation startup and misc. routines. */ @@ -46,6 +46,7 @@ #include "debugger.h" #include "device.h" #include "diskimage.h" +#include "exec_elf.h" #include "machine.h" #include "memory.h" #include "mips_cpu_types.h" @@ -66,6 +67,8 @@ extern struct emul *debugger_emul; extern struct diskimage *diskimages[]; +static char *diskimage_types[] = DISKIMAGE_TYPES; + /* * add_dump_points(): @@ -131,25 +134,329 @@ /* + * 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 = 4; + 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 *tmpfilename = 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) + + (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) { + int i, nlen = dp[0]; + int x = dp[2] + (dp[3] << 8) + (dp[4] << 16) + (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; imachine_type) { case MACHINE_DEC: @@ -169,7 +476,7 @@ * 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) @@ -196,11 +503,12 @@ 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) @@ -225,8 +533,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,11 +550,13 @@ } debug(readofs == 0x18? ": no blocks?\n" : " blocks\n"); - break; + return 1; case MACHINE_X86: - cpu->cd.x86.mode = 16; - cpu->pc = 0x7c00; + /* 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) { @@ -254,26 +564,67 @@ exit(1); } - res = diskimage_access(m, boot_disk_id, 0, 0, + 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) { - printf("Couldn't read the disk image. Aborting.\n"); - exit(1); + fatal("Couldn't read the disk image. Aborting.\n"); + return 0; } - 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; - default: - fatal("Booting from disk without a separate kernel " - "doesn't work in this emulation mode.\n"); + return 1; + } + + + /* + * Try reading a kernel manually from the disk. The code here + * does not rely on machine-dependant 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); + } + + free(bootblock_buf); + return retval; } @@ -295,6 +646,7 @@ /* Sane default values: */ e->n_machines = 0; + e->next_serial_nr = 1; if (name != NULL) { e->name = strdup(name); @@ -365,7 +717,7 @@ 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 +765,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); } } @@ -457,7 +809,7 @@ struct emul *emul; struct cpu *cpu; int i, iadd=4; - uint64_t addr, memory_amount, entrypoint = 0, gp = 0, toc = 0; + uint64_t memory_amount, entrypoint = 0, gp = 0, toc = 0; int byte_order; emul = m->emul; @@ -482,6 +834,9 @@ m->cpu_family = cpu_family_ptr_by_number(m->arch); + if (m->arch != ARCH_MIPS) + m->bintrans_enable = 0; + machine_memsize_fix(m); /* @@ -544,6 +899,20 @@ } 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 */); + if (m->bintrans_enable) + bintrans_init_cpu(m->cpus[m->ncpus]); + debug("\n"); + m->ncpus ++; + } +#endif + if (m->use_random_bootstrap_cpu) m->bootstrap_cpu = random() % m->ncpus; else @@ -562,23 +931,17 @@ 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 @@ -595,9 +958,14 @@ /* 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,11 +974,129 @@ } 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; @@ -631,7 +1117,10 @@ 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 */ break; case ARCH_ALPHA: @@ -640,15 +1129,27 @@ case ARCH_URISC: 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 ELF64 - * load, on AMD64! + * NOTE: The toc field is used to indicate an ELF32 + * or ELF64 load. */ - if (toc != 0) { - cpu->cd.x86.mode = 64; - } else + 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: @@ -740,6 +1241,10 @@ 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_URISC: { char tmps[100]; @@ -758,19 +1263,15 @@ cd.urisc.wordlen/8 - 1 - i]; } - sprintf(tmps, "0x%%0%illx", cpu->cd.urisc.wordlen / 4); + snprintf(tmps, sizeof(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) - debug("0x%08x", (int)cpu->pc); - else - debug("0x%016llx", (long long)cpu->pc); + 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); @@ -831,9 +1332,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");