25 |
* SUCH DAMAGE. |
* SUCH DAMAGE. |
26 |
* |
* |
27 |
* |
* |
28 |
* $Id: emul.c,v 1.203 2005/06/03 07:39:27 debug Exp $ |
* $Id: emul.c,v 1.225 2005/08/14 19:35:54 debug Exp $ |
29 |
* |
* |
30 |
* Emulation startup and misc. routines. |
* Emulation startup and misc. routines. |
31 |
*/ |
*/ |
46 |
#include "debugger.h" |
#include "debugger.h" |
47 |
#include "device.h" |
#include "device.h" |
48 |
#include "diskimage.h" |
#include "diskimage.h" |
49 |
|
#include "exec_elf.h" |
50 |
#include "machine.h" |
#include "machine.h" |
51 |
#include "memory.h" |
#include "memory.h" |
52 |
#include "mips_cpu_types.h" |
#include "mips_cpu_types.h" |
646 |
|
|
647 |
/* Sane default values: */ |
/* Sane default values: */ |
648 |
e->n_machines = 0; |
e->n_machines = 0; |
649 |
|
e->next_serial_nr = 1; |
650 |
|
|
651 |
if (name != NULL) { |
if (name != NULL) { |
652 |
e->name = strdup(name); |
e->name = strdup(name); |
709 |
|
|
710 |
len += 1048576 * m->memory_offset_in_mb; |
len += 1048576 * m->memory_offset_in_mb; |
711 |
|
|
712 |
/* NOTE/TODO: magic 12MB end of load program area */ |
/* |
713 |
|
* NOTE/TODO: magic 12MB end of load program area |
714 |
|
* |
715 |
|
* Hm. This breaks the old FreeBSD/MIPS snapshots... |
716 |
|
*/ |
717 |
|
#if 0 |
718 |
arcbios_add_memory_descriptor(cpu, |
arcbios_add_memory_descriptor(cpu, |
719 |
0x60000 + m->memory_offset_in_mb * 1048576, |
0x60000 + m->memory_offset_in_mb * 1048576, |
720 |
start-0x60000 - m->memory_offset_in_mb * 1048576, |
start-0x60000 - m->memory_offset_in_mb * 1048576, |
721 |
ARCBIOS_MEM_FreeMemory); |
ARCBIOS_MEM_FreeMemory); |
722 |
|
#endif |
723 |
arcbios_add_memory_descriptor(cpu, |
arcbios_add_memory_descriptor(cpu, |
724 |
start, len, ARCBIOS_MEM_LoadedProgram); |
start, len, ARCBIOS_MEM_LoadedProgram); |
725 |
|
|
840 |
|
|
841 |
m->cpu_family = cpu_family_ptr_by_number(m->arch); |
m->cpu_family = cpu_family_ptr_by_number(m->arch); |
842 |
|
|
843 |
|
if (m->arch == ARCH_ALPHA) |
844 |
|
m->arch_pagesize = 8192; |
845 |
|
|
846 |
|
if (m->arch != ARCH_MIPS) |
847 |
|
m->bintrans_enable = 0; |
848 |
|
|
849 |
machine_memsize_fix(m); |
machine_memsize_fix(m); |
850 |
|
|
851 |
/* |
/* |
866 |
debug(" (offset by %iMB)", m->memory_offset_in_mb); |
debug(" (offset by %iMB)", m->memory_offset_in_mb); |
867 |
memory_amount += 1048576 * m->memory_offset_in_mb; |
memory_amount += 1048576 * m->memory_offset_in_mb; |
868 |
} |
} |
869 |
m->memory = memory_new(memory_amount); |
m->memory = memory_new(memory_amount, m->arch); |
870 |
if (m->machine_type != MACHINE_USERLAND) |
if (m->machine_type != MACHINE_USERLAND) |
871 |
debug("\n"); |
debug("\n"); |
872 |
|
|
908 |
} |
} |
909 |
debug("\n"); |
debug("\n"); |
910 |
|
|
911 |
|
#if 0 |
912 |
|
/* Special case: The Playstation Portable has an additional CPU: */ |
913 |
|
if (m->machine_type == MACHINE_PSP) { |
914 |
|
debug("cpu%i: ", m->ncpus); |
915 |
|
m->cpus[m->ncpus] = cpu_new(m->memory, m, |
916 |
|
0 /* use 0 here to show info with debug() */, |
917 |
|
"Allegrex" /* TODO */); |
918 |
|
if (m->bintrans_enable) |
919 |
|
bintrans_init_cpu(m->cpus[m->ncpus]); |
920 |
|
debug("\n"); |
921 |
|
m->ncpus ++; |
922 |
|
} |
923 |
|
#endif |
924 |
|
|
925 |
if (m->use_random_bootstrap_cpu) |
if (m->use_random_bootstrap_cpu) |
926 |
m->bootstrap_cpu = random() % m->ncpus; |
m->bootstrap_cpu = random() % m->ncpus; |
927 |
else |
else |
933 |
if (m->userland_emul != NULL) { |
if (m->userland_emul != NULL) { |
934 |
useremul_name_to_useremul(cpu, |
useremul_name_to_useremul(cpu, |
935 |
m->userland_emul, NULL, NULL, NULL); |
m->userland_emul, NULL, NULL, NULL); |
936 |
cpu->memory_rw = userland_memory_rw; |
|
937 |
|
switch (m->arch) { |
938 |
|
#ifdef ENABLE_ALPHA |
939 |
|
case ARCH_ALPHA: |
940 |
|
cpu->memory_rw = alpha_userland_memory_rw; |
941 |
|
break; |
942 |
|
#endif |
943 |
|
default:cpu->memory_rw = userland_memory_rw; |
944 |
|
} |
945 |
} |
} |
946 |
|
|
947 |
if (m->use_x11) |
if (m->use_x11) |
1002 |
} |
} |
1003 |
|
|
1004 |
/* |
/* |
1005 |
* Another special hack for temporary files; running gunzip |
* gzipped files are automagically gunzipped: |
1006 |
* on them, if they have a gzip header. TODO: Change this |
* NOTE/TODO: This isn't secure. system() is used. |
|
* into some kind of generic support for gzipped files! |
|
1007 |
*/ |
*/ |
1008 |
tmp_f = fopen(name_to_load, "r"); |
tmp_f = fopen(name_to_load, "r"); |
1009 |
if (tmp_f != NULL) { |
if (tmp_f != NULL) { |
1011 |
memset(buf, 0, sizeof(buf)); |
memset(buf, 0, sizeof(buf)); |
1012 |
fread(buf, 1, sizeof(buf), tmp_f); |
fread(buf, 1, sizeof(buf), tmp_f); |
1013 |
if (buf[0]==0x1f && buf[1]==0x8b) { |
if (buf[0]==0x1f && buf[1]==0x8b) { |
1014 |
char *zz = malloc(strlen(name_to_load)*2 + 100); |
size_t zzlen = strlen(name_to_load)*2 + 100; |
1015 |
|
char *zz = malloc(zzlen); |
1016 |
debug("gunziping %s\n", name_to_load); |
debug("gunziping %s\n", name_to_load); |
1017 |
sprintf(zz, "mv %s %s.gz", name_to_load, |
/* |
1018 |
name_to_load); |
* gzip header found. If this was a file |
1019 |
system(zz); |
* extracted from, say, a CDROM image, then it |
1020 |
sprintf(zz, "gunzip %s.gz", name_to_load); |
* already has a temporary name. Otherwise we |
1021 |
system(zz); |
* have to gunzip into a temporary file. |
1022 |
|
*/ |
1023 |
|
if (remove_after_load) { |
1024 |
|
snprintf(zz, zzlen, "mv %s %s.gz", |
1025 |
|
name_to_load, name_to_load); |
1026 |
|
system(zz); |
1027 |
|
snprintf(zz, zzlen, "gunzip %s.gz", |
1028 |
|
name_to_load); |
1029 |
|
system(zz); |
1030 |
|
} else { |
1031 |
|
/* gunzip into new temp file: */ |
1032 |
|
int tmpfile_handle; |
1033 |
|
char *new_temp_name = |
1034 |
|
strdup("/tmp/gxemul.XXXXXXXXXXXX"); |
1035 |
|
tmpfile_handle = mkstemp(new_temp_name); |
1036 |
|
close(tmpfile_handle); |
1037 |
|
snprintf(zz, zzlen, "gunzip -c '%s' > " |
1038 |
|
"%s", name_to_load, new_temp_name); |
1039 |
|
system(zz); |
1040 |
|
name_to_load = new_temp_name; |
1041 |
|
remove_after_load = 1; |
1042 |
|
} |
1043 |
free(zz); |
free(zz); |
1044 |
} |
} |
1045 |
fclose(tmp_f); |
fclose(tmp_f); |
1046 |
} |
} |
1047 |
|
|
1048 |
|
/* |
1049 |
|
* Ugly (but usable) hack for Playstation Portable: If the |
1050 |
|
* filename ends with ".pbp" and the file contains an ELF |
1051 |
|
* header, then extract the ELF file into a temporary file. |
1052 |
|
*/ |
1053 |
|
if (strlen(name_to_load) > 4 && strcasecmp(name_to_load + |
1054 |
|
strlen(name_to_load) - 4, ".pbp") == 0 && |
1055 |
|
(tmp_f = fopen(name_to_load, "r")) != NULL) { |
1056 |
|
off_t filesize, j, found=0; |
1057 |
|
unsigned char *buf; |
1058 |
|
fseek(tmp_f, 0, SEEK_END); |
1059 |
|
filesize = ftello(tmp_f); |
1060 |
|
fseek(tmp_f, 0, SEEK_SET); |
1061 |
|
buf = malloc(filesize); |
1062 |
|
if (buf == NULL) { |
1063 |
|
fprintf(stderr, "out of memory while trying" |
1064 |
|
" to read %s\n", name_to_load); |
1065 |
|
exit(1); |
1066 |
|
} |
1067 |
|
fread(buf, 1, filesize, tmp_f); |
1068 |
|
fclose(tmp_f); |
1069 |
|
/* Search for the ELF header, from offset 1 (!): */ |
1070 |
|
for (j=1; j<filesize - 4; j++) |
1071 |
|
if (memcmp(buf + j, ELFMAG, SELFMAG) == 0) { |
1072 |
|
found = j; |
1073 |
|
break; |
1074 |
|
} |
1075 |
|
if (found != 0) { |
1076 |
|
int tmpfile_handle; |
1077 |
|
char *new_temp_name = |
1078 |
|
strdup("/tmp/gxemul.XXXXXXXXXXXX"); |
1079 |
|
debug("extracting ELF from %s (offset 0x%x)\n", |
1080 |
|
name_to_load, (int)found); |
1081 |
|
tmpfile_handle = mkstemp(new_temp_name); |
1082 |
|
write(tmpfile_handle, buf + found, |
1083 |
|
filesize - found); |
1084 |
|
close(tmpfile_handle); |
1085 |
|
name_to_load = new_temp_name; |
1086 |
|
remove_after_load = 1; |
1087 |
|
} |
1088 |
|
} |
1089 |
|
|
1090 |
/* Special things required _before_ loading the file: */ |
/* Special things required _before_ loading the file: */ |
1091 |
switch (m->arch) { |
switch (m->arch) { |
1092 |
case ARCH_X86: |
case ARCH_X86: |
1141 |
break; |
break; |
1142 |
|
|
1143 |
case ARCH_ALPHA: |
case ARCH_ALPHA: |
1144 |
case ARCH_HPPA: |
/* For position-independant code: */ |
1145 |
|
cpu->cd.alpha.r[ALPHA_T12] = cpu->pc; |
1146 |
|
break; |
1147 |
|
|
1148 |
case ARCH_SPARC: |
case ARCH_SPARC: |
1149 |
case ARCH_URISC: |
break; |
1150 |
|
|
1151 |
|
case ARCH_IA64: |
1152 |
|
break; |
1153 |
|
|
1154 |
|
case ARCH_M68K: |
1155 |
break; |
break; |
1156 |
|
|
1157 |
case ARCH_ARM: |
case ARCH_ARM: |
1158 |
cpu->pc &= 0xffffffff; |
cpu->pc &= 0xfffffffc; |
1159 |
|
cpu->cd.arm.r[ARM_PC] = cpu->pc; |
1160 |
break; |
break; |
1161 |
|
|
1162 |
case ARCH_X86: |
case ARCH_X86: |
1228 |
if (m->machine_type == MACHINE_DEC && |
if (m->machine_type == MACHINE_DEC && |
1229 |
cpu->cd.mips.cpu_type.mmu_model == MMU3K) |
cpu->cd.mips.cpu_type.mmu_model == MMU3K) |
1230 |
add_symbol_name(&m->symbol_context, |
add_symbol_name(&m->symbol_context, |
1231 |
0x9fff0000, 0x10000, "r2k3k_cache", 0); |
0x9fff0000, 0x10000, "r2k3k_cache", 0, 0); |
1232 |
|
|
1233 |
symbol_recalc_sizes(&m->symbol_context); |
symbol_recalc_sizes(&m->symbol_context); |
1234 |
|
|
1244 |
debug("starting cpu%i at ", m->bootstrap_cpu); |
debug("starting cpu%i at ", m->bootstrap_cpu); |
1245 |
switch (m->arch) { |
switch (m->arch) { |
1246 |
case ARCH_MIPS: |
case ARCH_MIPS: |
1247 |
if (cpu->cd.mips.cpu_type.isa_level < 3 || |
if (cpu->is_32bit) { |
|
cpu->cd.mips.cpu_type.isa_level == 32) { |
|
1248 |
debug("0x%08x", (int)m->cpus[ |
debug("0x%08x", (int)m->cpus[ |
1249 |
m->bootstrap_cpu]->pc); |
m->bootstrap_cpu]->pc); |
1250 |
if (cpu->cd.mips.gpr[MIPS_GPR_GP] != 0) |
if (cpu->cd.mips.gpr[MIPS_GPR_GP] != 0) |
1269 |
/* ARM cpus aren't 64-bit: */ |
/* ARM cpus aren't 64-bit: */ |
1270 |
debug("0x%08x", (int)entrypoint); |
debug("0x%08x", (int)entrypoint); |
1271 |
break; |
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]; |
|
|
} |
|
|
|
|
|
sprintf(tmps, "0x%%0%illx", cpu->cd.urisc.wordlen / 4); |
|
|
debug(tmps, (long long)entrypoint); |
|
|
cpu->pc = entrypoint; |
|
|
} |
|
|
break; |
|
1272 |
case ARCH_X86: |
case ARCH_X86: |
1273 |
debug("0x%04x:0x%llx", cpu->cd.x86.s[X86_S_CS], |
debug("0x%04x:0x%llx", cpu->cd.x86.s[X86_S_CS], |
1274 |
(long long)cpu->pc); |
(long long)cpu->pc); |
1332 |
debug("Simple setup...\n"); |
debug("Simple setup...\n"); |
1333 |
debug_indentation(iadd); |
debug_indentation(iadd); |
1334 |
|
|
1335 |
/* Create a network: */ |
/* Create a simple network: */ |
1336 |
emul->net = net_init(emul, NET_INIT_FLAG_GATEWAY, |
emul->net = net_init(emul, NET_INIT_FLAG_GATEWAY, |
1337 |
"10.0.0.0", 8); |
"10.0.0.0", 8, NULL, 0, 0); |
1338 |
} else { |
} else { |
1339 |
/* Userland pseudo-machine: */ |
/* Userland pseudo-machine: */ |
1340 |
debug("Syscall emulation (userland-only) setup...\n"); |
debug("Syscall emulation (userland-only) setup...\n"); |
1441 |
if (e == NULL) |
if (e == NULL) |
1442 |
continue; |
continue; |
1443 |
for (j=0; j<e->n_machines; j++) |
for (j=0; j<e->n_machines; j++) |
1444 |
cpu_run_init(e, e->machines[j]); |
cpu_run_init(e->machines[j]); |
1445 |
} |
} |
1446 |
|
|
1447 |
|
/* TODO: Generalize: */ |
1448 |
|
if (emuls[0]->machines[0]->show_trace_tree) |
1449 |
|
cpu_functioncall_trace(emuls[0]->machines[0]->cpus[0], |
1450 |
|
emuls[0]->machines[0]->cpus[0]->pc); |
1451 |
|
|
1452 |
/* |
/* |
1453 |
* MAIN LOOP: |
* MAIN LOOP: |
1454 |
* |
* |
1481 |
if (e == NULL) |
if (e == NULL) |
1482 |
continue; |
continue; |
1483 |
for (j=0; j<e->n_machines; j++) |
for (j=0; j<e->n_machines; j++) |
1484 |
cpu_run_deinit(e, e->machines[j]); |
cpu_run_deinit(e->machines[j]); |
1485 |
} |
} |
1486 |
|
|
1487 |
/* force_debugger_at_exit flag set? Then enter the debugger: */ |
/* force_debugger_at_exit flag set? Then enter the debugger: */ |