--- trunk/src/emul.c 2007/10/08 16:18:11 6 +++ trunk/src/emul.c 2007/10/08 16:19:11 18 @@ -25,7 +25,7 @@ * SUCH DAMAGE. * * - * $Id: emul.c,v 1.203 2005/06/03 07:39:27 debug Exp $ + * $Id: emul.c,v 1.235 2005/10/26 14:37:02 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" @@ -434,6 +435,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 = (buf[ofs + 8] << 24) + (buf[ofs + 9] << 16) + + (buf[ofs + 10] << 8) + buf[ofs + 11]; + length = (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 @@ -585,7 +650,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); @@ -622,6 +687,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; } @@ -645,6 +734,7 @@ /* Sane default values: */ e->n_machines = 0; + e->next_serial_nr = 1; if (name != NULL) { e->name = strdup(name); @@ -707,11 +797,17 @@ 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); @@ -832,6 +928,12 @@ m->cpu_family = cpu_family_ptr_by_number(m->arch); + if (m->arch == ARCH_ALPHA) + m->arch_pagesize = 8192; + + if (m->arch != ARCH_MIPS) + m->bintrans_enable = 0; + machine_memsize_fix(m); /* @@ -852,7 +954,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"); @@ -894,6 +996,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 @@ -905,7 +1021,15 @@ 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) @@ -966,9 +1090,8 @@ } /* - * Another special hack for temporary files; running gunzip - * on them, if they have a gzip header. TODO: Change this - * into some kind of generic support for gzipped files! + * 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) { @@ -976,18 +1099,82 @@ memset(buf, 0, sizeof(buf)); fread(buf, 1, sizeof(buf), tmp_f); if (buf[0]==0x1f && buf[1]==0x8b) { - char *zz = malloc(strlen(name_to_load)*2 + 100); + size_t zzlen = strlen(name_to_load)*2 + 100; + char *zz = malloc(zzlen); debug("gunziping %s\n", name_to_load); - sprintf(zz, "mv %s %s.gz", name_to_load, - name_to_load); - system(zz); - sprintf(zz, "gunzip %s.gz", name_to_load); - system(zz); + /* + * 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: @@ -1021,6 +1208,37 @@ 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: + cpu->pc &= 0xfffffffc; + cpu->cd.arm.r[ARM_PC] = cpu->pc; + 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)) @@ -1039,16 +1257,17 @@ 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: - case ARCH_HPPA: - case ARCH_SPARC: - case ARCH_URISC: + case ARCH_SH: + if (cpu->cd.sh.bits == 32) + cpu->pc &= 0xffffffffULL; + cpu->pc &= ~1; break; - case ARCH_ARM: - cpu->pc &= 0xffffffff; + case ARCH_SPARC: break; case ARCH_X86: @@ -1120,7 +1339,7 @@ if (m->machine_type == MACHINE_DEC && 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); @@ -1135,9 +1354,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) @@ -1152,43 +1381,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_ARM: - /* ARM cpus aren't 64-bit: */ - debug("0x%08x", (int)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: 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); } @@ -1248,9 +1453,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"); @@ -1357,9 +1562,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: * @@ -1392,7 +1602,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: */