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.211 2005/06/26 11:36:28 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); |
834 |
|
|
835 |
m->cpu_family = cpu_family_ptr_by_number(m->arch); |
m->cpu_family = cpu_family_ptr_by_number(m->arch); |
836 |
|
|
837 |
|
if (m->arch != ARCH_MIPS) |
838 |
|
m->bintrans_enable = 0; |
839 |
|
|
840 |
machine_memsize_fix(m); |
machine_memsize_fix(m); |
841 |
|
|
842 |
/* |
/* |
899 |
} |
} |
900 |
debug("\n"); |
debug("\n"); |
901 |
|
|
902 |
|
#if 0 |
903 |
|
/* Special case: The Playstation Portable has an additional CPU: */ |
904 |
|
if (m->machine_type == MACHINE_PSP) { |
905 |
|
debug("cpu%i: ", m->ncpus); |
906 |
|
m->cpus[m->ncpus] = cpu_new(m->memory, m, |
907 |
|
0 /* use 0 here to show info with debug() */, |
908 |
|
"Allegrex" /* TODO */); |
909 |
|
if (m->bintrans_enable) |
910 |
|
bintrans_init_cpu(m->cpus[m->ncpus]); |
911 |
|
debug("\n"); |
912 |
|
m->ncpus ++; |
913 |
|
} |
914 |
|
#endif |
915 |
|
|
916 |
if (m->use_random_bootstrap_cpu) |
if (m->use_random_bootstrap_cpu) |
917 |
m->bootstrap_cpu = random() % m->ncpus; |
m->bootstrap_cpu = random() % m->ncpus; |
918 |
else |
else |
985 |
} |
} |
986 |
|
|
987 |
/* |
/* |
988 |
* Another special hack for temporary files; running gunzip |
* gzipped files are automagically gunzipped: |
989 |
* 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! |
|
990 |
*/ |
*/ |
991 |
tmp_f = fopen(name_to_load, "r"); |
tmp_f = fopen(name_to_load, "r"); |
992 |
if (tmp_f != NULL) { |
if (tmp_f != NULL) { |
994 |
memset(buf, 0, sizeof(buf)); |
memset(buf, 0, sizeof(buf)); |
995 |
fread(buf, 1, sizeof(buf), tmp_f); |
fread(buf, 1, sizeof(buf), tmp_f); |
996 |
if (buf[0]==0x1f && buf[1]==0x8b) { |
if (buf[0]==0x1f && buf[1]==0x8b) { |
997 |
char *zz = malloc(strlen(name_to_load)*2 + 100); |
size_t zzlen = strlen(name_to_load)*2 + 100; |
998 |
|
char *zz = malloc(zzlen); |
999 |
debug("gunziping %s\n", name_to_load); |
debug("gunziping %s\n", name_to_load); |
1000 |
sprintf(zz, "mv %s %s.gz", name_to_load, |
/* |
1001 |
name_to_load); |
* gzip header found. If this was a file |
1002 |
system(zz); |
* extracted from, say, a CDROM image, then it |
1003 |
sprintf(zz, "gunzip %s.gz", name_to_load); |
* already has a temporary name. Otherwise we |
1004 |
system(zz); |
* have to gunzip into a temporary file. |
1005 |
|
*/ |
1006 |
|
if (remove_after_load) { |
1007 |
|
snprintf(zz, zzlen, "mv %s %s.gz", |
1008 |
|
name_to_load, name_to_load); |
1009 |
|
system(zz); |
1010 |
|
snprintf(zz, zzlen, "gunzip %s.gz", |
1011 |
|
name_to_load); |
1012 |
|
system(zz); |
1013 |
|
} else { |
1014 |
|
/* gunzip into new temp file: */ |
1015 |
|
int tmpfile_handle; |
1016 |
|
char *new_temp_name = |
1017 |
|
strdup("/tmp/gxemul.XXXXXXXXXXXX"); |
1018 |
|
tmpfile_handle = mkstemp(new_temp_name); |
1019 |
|
close(tmpfile_handle); |
1020 |
|
snprintf(zz, zzlen, "gunzip -c '%s' > " |
1021 |
|
"%s", name_to_load, new_temp_name); |
1022 |
|
system(zz); |
1023 |
|
name_to_load = new_temp_name; |
1024 |
|
remove_after_load = 1; |
1025 |
|
} |
1026 |
free(zz); |
free(zz); |
1027 |
} |
} |
1028 |
fclose(tmp_f); |
fclose(tmp_f); |
1029 |
} |
} |
1030 |
|
|
1031 |
|
/* |
1032 |
|
* Ugly (but usable) hack for Playstation Portable: If the |
1033 |
|
* filename ends with ".pbp" and the file contains an ELF |
1034 |
|
* header, then extract the ELF file into a temporary file. |
1035 |
|
*/ |
1036 |
|
if (strlen(name_to_load) > 4 && strcasecmp(name_to_load + |
1037 |
|
strlen(name_to_load) - 4, ".pbp") == 0 && |
1038 |
|
(tmp_f = fopen(name_to_load, "r")) != NULL) { |
1039 |
|
off_t filesize, j, found=0; |
1040 |
|
unsigned char *buf; |
1041 |
|
fseek(tmp_f, 0, SEEK_END); |
1042 |
|
filesize = ftello(tmp_f); |
1043 |
|
fseek(tmp_f, 0, SEEK_SET); |
1044 |
|
buf = malloc(filesize); |
1045 |
|
if (buf == NULL) { |
1046 |
|
fprintf(stderr, "out of memory while trying" |
1047 |
|
" to read %s\n", name_to_load); |
1048 |
|
exit(1); |
1049 |
|
} |
1050 |
|
fread(buf, 1, filesize, tmp_f); |
1051 |
|
fclose(tmp_f); |
1052 |
|
/* Search for the ELF header, from offset 1 (!): */ |
1053 |
|
for (j=1; j<filesize - 4; j++) |
1054 |
|
if (memcmp(buf + j, ELFMAG, SELFMAG) == 0) { |
1055 |
|
found = j; |
1056 |
|
break; |
1057 |
|
} |
1058 |
|
if (found != 0) { |
1059 |
|
int tmpfile_handle; |
1060 |
|
char *new_temp_name = |
1061 |
|
strdup("/tmp/gxemul.XXXXXXXXXXXX"); |
1062 |
|
debug("extracting ELF from %s (offset 0x%x)\n", |
1063 |
|
name_to_load, (int)found); |
1064 |
|
tmpfile_handle = mkstemp(new_temp_name); |
1065 |
|
write(tmpfile_handle, buf + found, |
1066 |
|
filesize - found); |
1067 |
|
close(tmpfile_handle); |
1068 |
|
name_to_load = new_temp_name; |
1069 |
|
remove_after_load = 1; |
1070 |
|
} |
1071 |
|
} |
1072 |
|
|
1073 |
/* Special things required _before_ loading the file: */ |
/* Special things required _before_ loading the file: */ |
1074 |
switch (m->arch) { |
switch (m->arch) { |
1075 |
case ARCH_X86: |
case ARCH_X86: |
1130 |
break; |
break; |
1131 |
|
|
1132 |
case ARCH_ARM: |
case ARCH_ARM: |
1133 |
cpu->pc &= 0xffffffff; |
cpu->pc &= 0xfffffffc; |
1134 |
|
cpu->cd.arm.r[ARM_PC] = cpu->pc; |
1135 |
break; |
break; |
1136 |
|
|
1137 |
case ARCH_X86: |
case ARCH_X86: |
1263 |
cd.urisc.wordlen/8 - 1 - i]; |
cd.urisc.wordlen/8 - 1 - i]; |
1264 |
} |
} |
1265 |
|
|
1266 |
sprintf(tmps, "0x%%0%illx", cpu->cd.urisc.wordlen / 4); |
snprintf(tmps, sizeof(tmps), "0x%%0%illx", |
1267 |
|
cpu->cd.urisc.wordlen / 4); |
1268 |
debug(tmps, (long long)entrypoint); |
debug(tmps, (long long)entrypoint); |
1269 |
cpu->pc = entrypoint; |
cpu->pc = entrypoint; |
1270 |
} |
} |
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"); |