--- trunk/src/emul.c 2007/10/08 16:18:27 10 +++ trunk/src/emul.c 2007/10/08 16:18:51 14 @@ -25,7 +25,7 @@ * SUCH DAMAGE. * * - * $Id: emul.c,v 1.211 2005/06/26 11:36:28 debug Exp $ + * $Id: emul.c,v 1.234 2005/09/17 21:55:19 debug Exp $ * * Emulation startup and misc. routines. */ @@ -435,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 @@ -623,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; } @@ -709,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); @@ -834,6 +928,9 @@ 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; @@ -857,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"); @@ -924,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) @@ -1103,6 +1208,37 @@ cpu->pc = entrypoint; switch (m->arch) { + + case ARCH_ALPHA: + /* For position-independant 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)) @@ -1121,17 +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 &= 0xfffffffc; - cpu->cd.arm.r[ARM_PC] = cpu->pc; + case ARCH_SPARC: break; case ARCH_X86: @@ -1203,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); @@ -1218,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) @@ -1235,44 +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]; - } - snprintf(tmps, sizeof(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); } @@ -1441,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: * @@ -1476,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: */