25 |
* SUCH DAMAGE. |
* SUCH DAMAGE. |
26 |
* |
* |
27 |
* |
* |
28 |
* $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 $ |
29 |
* |
* |
30 |
* Emulation startup and misc. routines. |
* Emulation startup and misc. routines. |
31 |
*/ |
*/ |
435 |
|
|
436 |
|
|
437 |
/* |
/* |
438 |
|
* apple_load_bootblock(): |
439 |
|
* |
440 |
|
* Try to load a kernel from a disk image with an Apple Partition Table. |
441 |
|
* |
442 |
|
* TODO: This function uses too many magic offsets and so on; it should be |
443 |
|
* cleaned up some day. See http://www.awprofessional.com/articles/ |
444 |
|
* article.asp?p=376123&seqNum=3&rl=1 for some info on the Apple |
445 |
|
* partition format. |
446 |
|
* |
447 |
|
* Returns 1 on success, 0 on failure. |
448 |
|
*/ |
449 |
|
static int apple_load_bootblock(struct machine *m, struct cpu *cpu, |
450 |
|
int disk_id, int disk_type, int *n_loadp, char ***load_namesp) |
451 |
|
{ |
452 |
|
unsigned char buf[0x8000]; |
453 |
|
int res, partnr, n_partitions = 0, n_hfs_partitions = 0; |
454 |
|
uint64_t hfs_start, hfs_length; |
455 |
|
|
456 |
|
res = diskimage_access(m, disk_id, disk_type, 0, 0x0, buf, sizeof(buf)); |
457 |
|
if (!res) { |
458 |
|
fatal("apple_load_bootblock: couldn't read the disk " |
459 |
|
"image. Aborting.\n"); |
460 |
|
return 0; |
461 |
|
} |
462 |
|
|
463 |
|
partnr = 0; |
464 |
|
do { |
465 |
|
int start, length; |
466 |
|
int ofs = 0x200 * (partnr + 1); |
467 |
|
if (partnr == 0) |
468 |
|
n_partitions = buf[ofs + 7]; |
469 |
|
start = (buf[ofs + 8] << 24) + (buf[ofs + 9] << 16) + |
470 |
|
(buf[ofs + 10] << 8) + buf[ofs + 11]; |
471 |
|
length = (buf[ofs + 12] << 24) + (buf[ofs + 13] << 16) + |
472 |
|
(buf[ofs + 14] << 8) + buf[ofs + 15]; |
473 |
|
|
474 |
|
debug("partition %i: '%s', type '%s', start %i, length %i\n", |
475 |
|
partnr, buf + ofs + 0x10, buf + ofs + 0x30, |
476 |
|
start, length); |
477 |
|
|
478 |
|
if (strcmp((char *)buf + ofs + 0x30, "Apple_HFS") == 0) { |
479 |
|
n_hfs_partitions ++; |
480 |
|
hfs_start = 512 * start; |
481 |
|
hfs_length = 512 * length; |
482 |
|
} |
483 |
|
|
484 |
|
/* Any more partitions? */ |
485 |
|
partnr ++; |
486 |
|
} while (partnr < n_partitions); |
487 |
|
|
488 |
|
if (n_hfs_partitions == 0) { |
489 |
|
fatal("Error: No HFS partition found! TODO\n"); |
490 |
|
return 0; |
491 |
|
} |
492 |
|
if (n_hfs_partitions >= 2) { |
493 |
|
fatal("Error: Too many HFS partitions found! TODO\n"); |
494 |
|
return 0; |
495 |
|
} |
496 |
|
|
497 |
|
return 0; |
498 |
|
} |
499 |
|
|
500 |
|
|
501 |
|
/* |
502 |
* load_bootblock(): |
* load_bootblock(): |
503 |
* |
* |
504 |
* For some emulation modes, it is possible to boot from a harddisk image by |
* For some emulation modes, it is possible to boot from a harddisk image by |
687 |
n_loadp, load_namesp); |
n_loadp, load_namesp); |
688 |
} |
} |
689 |
|
|
690 |
|
if (retval != 0) |
691 |
|
goto ret_ok; |
692 |
|
|
693 |
|
/* Apple parition table: */ |
694 |
|
res = diskimage_access(m, boot_disk_id, boot_disk_type, |
695 |
|
0, 0x0, bootblock_buf, 0x800); |
696 |
|
if (!res) { |
697 |
|
fatal("Couldn't read the disk image. Aborting.\n"); |
698 |
|
return 0; |
699 |
|
} |
700 |
|
if (bootblock_buf[0x000] == 'E' && bootblock_buf[0x001] == 'R' && |
701 |
|
bootblock_buf[0x200] == 'P' && bootblock_buf[0x201] == 'M') { |
702 |
|
/* We can't load a kernel if the name |
703 |
|
isn't specified. */ |
704 |
|
if (cpu->machine->boot_kernel_filename == NULL || |
705 |
|
cpu->machine->boot_kernel_filename[0] == '\0') |
706 |
|
fatal("\nApple partition table, but no kernel " |
707 |
|
"specified? (Use the -j option.)\n"); |
708 |
|
else |
709 |
|
retval = apple_load_bootblock(m, cpu, boot_disk_id, |
710 |
|
boot_disk_type, n_loadp, load_namesp); |
711 |
|
} |
712 |
|
|
713 |
|
ret_ok: |
714 |
free(bootblock_buf); |
free(bootblock_buf); |
715 |
return retval; |
return retval; |
716 |
} |
} |
797 |
|
|
798 |
len += 1048576 * m->memory_offset_in_mb; |
len += 1048576 * m->memory_offset_in_mb; |
799 |
|
|
800 |
/* NOTE/TODO: magic 12MB end of load program area */ |
/* |
801 |
|
* NOTE/TODO: magic 12MB end of load program area |
802 |
|
* |
803 |
|
* Hm. This breaks the old FreeBSD/MIPS snapshots... |
804 |
|
*/ |
805 |
|
#if 0 |
806 |
arcbios_add_memory_descriptor(cpu, |
arcbios_add_memory_descriptor(cpu, |
807 |
0x60000 + m->memory_offset_in_mb * 1048576, |
0x60000 + m->memory_offset_in_mb * 1048576, |
808 |
start-0x60000 - m->memory_offset_in_mb * 1048576, |
start-0x60000 - m->memory_offset_in_mb * 1048576, |
809 |
ARCBIOS_MEM_FreeMemory); |
ARCBIOS_MEM_FreeMemory); |
810 |
|
#endif |
811 |
arcbios_add_memory_descriptor(cpu, |
arcbios_add_memory_descriptor(cpu, |
812 |
start, len, ARCBIOS_MEM_LoadedProgram); |
start, len, ARCBIOS_MEM_LoadedProgram); |
813 |
|
|
928 |
|
|
929 |
m->cpu_family = cpu_family_ptr_by_number(m->arch); |
m->cpu_family = cpu_family_ptr_by_number(m->arch); |
930 |
|
|
931 |
|
if (m->arch == ARCH_ALPHA) |
932 |
|
m->arch_pagesize = 8192; |
933 |
|
|
934 |
if (m->arch != ARCH_MIPS) |
if (m->arch != ARCH_MIPS) |
935 |
m->bintrans_enable = 0; |
m->bintrans_enable = 0; |
936 |
|
|
954 |
debug(" (offset by %iMB)", m->memory_offset_in_mb); |
debug(" (offset by %iMB)", m->memory_offset_in_mb); |
955 |
memory_amount += 1048576 * m->memory_offset_in_mb; |
memory_amount += 1048576 * m->memory_offset_in_mb; |
956 |
} |
} |
957 |
m->memory = memory_new(memory_amount); |
m->memory = memory_new(memory_amount, m->arch); |
958 |
if (m->machine_type != MACHINE_USERLAND) |
if (m->machine_type != MACHINE_USERLAND) |
959 |
debug("\n"); |
debug("\n"); |
960 |
|
|
1021 |
if (m->userland_emul != NULL) { |
if (m->userland_emul != NULL) { |
1022 |
useremul_name_to_useremul(cpu, |
useremul_name_to_useremul(cpu, |
1023 |
m->userland_emul, NULL, NULL, NULL); |
m->userland_emul, NULL, NULL, NULL); |
1024 |
cpu->memory_rw = userland_memory_rw; |
|
1025 |
|
switch (m->arch) { |
1026 |
|
#ifdef ENABLE_ALPHA |
1027 |
|
case ARCH_ALPHA: |
1028 |
|
cpu->memory_rw = alpha_userland_memory_rw; |
1029 |
|
break; |
1030 |
|
#endif |
1031 |
|
default:cpu->memory_rw = userland_memory_rw; |
1032 |
|
} |
1033 |
} |
} |
1034 |
|
|
1035 |
if (m->use_x11) |
if (m->use_x11) |
1208 |
cpu->pc = entrypoint; |
cpu->pc = entrypoint; |
1209 |
|
|
1210 |
switch (m->arch) { |
switch (m->arch) { |
1211 |
|
|
1212 |
|
case ARCH_ALPHA: |
1213 |
|
/* For position-independant code: */ |
1214 |
|
cpu->cd.alpha.r[ALPHA_T12] = cpu->pc; |
1215 |
|
break; |
1216 |
|
|
1217 |
|
case ARCH_ARM: |
1218 |
|
cpu->pc &= 0xfffffffc; |
1219 |
|
cpu->cd.arm.r[ARM_PC] = cpu->pc; |
1220 |
|
break; |
1221 |
|
|
1222 |
|
case ARCH_AVR: |
1223 |
|
cpu->pc &= 0xfffff; |
1224 |
|
if (cpu->pc & 1) { |
1225 |
|
fatal("AVR: lowest bit of pc set: TODO\n"); |
1226 |
|
exit(1); |
1227 |
|
} |
1228 |
|
break; |
1229 |
|
|
1230 |
|
case ARCH_HPPA: |
1231 |
|
break; |
1232 |
|
|
1233 |
|
case ARCH_I960: |
1234 |
|
break; |
1235 |
|
|
1236 |
|
case ARCH_IA64: |
1237 |
|
break; |
1238 |
|
|
1239 |
|
case ARCH_M68K: |
1240 |
|
break; |
1241 |
|
|
1242 |
case ARCH_MIPS: |
case ARCH_MIPS: |
1243 |
if ((cpu->pc >> 32) == 0 |
if ((cpu->pc >> 32) == 0 |
1244 |
&& (cpu->pc & 0x80000000ULL)) |
&& (cpu->pc & 0x80000000ULL)) |
1257 |
spec/x458.html for more info. */ |
spec/x458.html for more info. */ |
1258 |
cpu->cd.ppc.gpr[2] = toc; |
cpu->cd.ppc.gpr[2] = toc; |
1259 |
/* TODO */ |
/* TODO */ |
1260 |
|
if (cpu->cd.ppc.bits == 32) |
1261 |
|
cpu->pc &= 0xffffffffULL; |
1262 |
break; |
break; |
1263 |
|
|
1264 |
case ARCH_ALPHA: |
case ARCH_SH: |
1265 |
case ARCH_HPPA: |
if (cpu->cd.sh.bits == 32) |
1266 |
case ARCH_SPARC: |
cpu->pc &= 0xffffffffULL; |
1267 |
case ARCH_URISC: |
cpu->pc &= ~1; |
1268 |
break; |
break; |
1269 |
|
|
1270 |
case ARCH_ARM: |
case ARCH_SPARC: |
|
cpu->pc &= 0xfffffffc; |
|
|
cpu->cd.arm.r[ARM_PC] = cpu->pc; |
|
1271 |
break; |
break; |
1272 |
|
|
1273 |
case ARCH_X86: |
case ARCH_X86: |
1339 |
if (m->machine_type == MACHINE_DEC && |
if (m->machine_type == MACHINE_DEC && |
1340 |
cpu->cd.mips.cpu_type.mmu_model == MMU3K) |
cpu->cd.mips.cpu_type.mmu_model == MMU3K) |
1341 |
add_symbol_name(&m->symbol_context, |
add_symbol_name(&m->symbol_context, |
1342 |
0x9fff0000, 0x10000, "r2k3k_cache", 0); |
0x9fff0000, 0x10000, "r2k3k_cache", 0, 0); |
1343 |
|
|
1344 |
symbol_recalc_sizes(&m->symbol_context); |
symbol_recalc_sizes(&m->symbol_context); |
1345 |
|
|
1354 |
|
|
1355 |
debug("starting cpu%i at ", m->bootstrap_cpu); |
debug("starting cpu%i at ", m->bootstrap_cpu); |
1356 |
switch (m->arch) { |
switch (m->arch) { |
1357 |
|
|
1358 |
|
case ARCH_ARM: |
1359 |
|
/* ARM cpus aren't 64-bit: */ |
1360 |
|
debug("0x%08x", (int)entrypoint); |
1361 |
|
break; |
1362 |
|
|
1363 |
|
case ARCH_AVR: |
1364 |
|
/* Atmel AVR uses a 16-bit or 22-bit program counter: */ |
1365 |
|
debug("0x%04x", (int)entrypoint); |
1366 |
|
break; |
1367 |
|
|
1368 |
case ARCH_MIPS: |
case ARCH_MIPS: |
1369 |
if (cpu->cd.mips.cpu_type.isa_level < 3 || |
if (cpu->is_32bit) { |
|
cpu->cd.mips.cpu_type.isa_level == 32) { |
|
1370 |
debug("0x%08x", (int)m->cpus[ |
debug("0x%08x", (int)m->cpus[ |
1371 |
m->bootstrap_cpu]->pc); |
m->bootstrap_cpu]->pc); |
1372 |
if (cpu->cd.mips.gpr[MIPS_GPR_GP] != 0) |
if (cpu->cd.mips.gpr[MIPS_GPR_GP] != 0) |
1381 |
cpu->cd.mips.gpr[MIPS_GPR_GP]); |
cpu->cd.mips.gpr[MIPS_GPR_GP]); |
1382 |
} |
} |
1383 |
break; |
break; |
1384 |
|
|
1385 |
case ARCH_PPC: |
case ARCH_PPC: |
1386 |
if (cpu->cd.ppc.bits == 32) |
if (cpu->cd.ppc.bits == 32) |
1387 |
debug("0x%08x", (int)entrypoint); |
debug("0x%08x", (int)entrypoint); |
1388 |
else |
else |
1389 |
debug("0x%016llx", (long long)entrypoint); |
debug("0x%016llx", (long long)entrypoint); |
1390 |
break; |
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; i<cpu->cd.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]; |
|
|
} |
|
1391 |
|
|
|
snprintf(tmps, sizeof(tmps), "0x%%0%illx", |
|
|
cpu->cd.urisc.wordlen / 4); |
|
|
debug(tmps, (long long)entrypoint); |
|
|
cpu->pc = entrypoint; |
|
|
} |
|
|
break; |
|
1392 |
case ARCH_X86: |
case ARCH_X86: |
1393 |
debug("0x%04x:0x%llx", cpu->cd.x86.s[X86_S_CS], |
debug("0x%04x:0x%llx", cpu->cd.x86.s[X86_S_CS], |
1394 |
(long long)cpu->pc); |
(long long)cpu->pc); |
1395 |
break; |
break; |
1396 |
|
|
1397 |
default: |
default: |
1398 |
debug("0x%016llx", (long long)cpu->pc); |
debug("0x%016llx", (long long)cpu->pc); |
1399 |
} |
} |
1562 |
if (e == NULL) |
if (e == NULL) |
1563 |
continue; |
continue; |
1564 |
for (j=0; j<e->n_machines; j++) |
for (j=0; j<e->n_machines; j++) |
1565 |
cpu_run_init(e, e->machines[j]); |
cpu_run_init(e->machines[j]); |
1566 |
} |
} |
1567 |
|
|
1568 |
|
/* TODO: Generalize: */ |
1569 |
|
if (emuls[0]->machines[0]->show_trace_tree) |
1570 |
|
cpu_functioncall_trace(emuls[0]->machines[0]->cpus[0], |
1571 |
|
emuls[0]->machines[0]->cpus[0]->pc); |
1572 |
|
|
1573 |
/* |
/* |
1574 |
* MAIN LOOP: |
* MAIN LOOP: |
1575 |
* |
* |
1602 |
if (e == NULL) |
if (e == NULL) |
1603 |
continue; |
continue; |
1604 |
for (j=0; j<e->n_machines; j++) |
for (j=0; j<e->n_machines; j++) |
1605 |
cpu_run_deinit(e, e->machines[j]); |
cpu_run_deinit(e->machines[j]); |
1606 |
} |
} |
1607 |
|
|
1608 |
/* force_debugger_at_exit flag set? Then enter the debugger: */ |
/* force_debugger_at_exit flag set? Then enter the debugger: */ |