1 |
/* |
/* |
2 |
* Copyright (C) 2003-2006 Anders Gavare. All rights reserved. |
* Copyright (C) 2003-2007 Anders Gavare. All rights reserved. |
3 |
* |
* |
4 |
* Redistribution and use in source and binary forms, with or without |
* Redistribution and use in source and binary forms, with or without |
5 |
* modification, are permitted provided that the following conditions are met: |
* modification, are permitted provided that the following conditions are met: |
25 |
* SUCH DAMAGE. |
* SUCH DAMAGE. |
26 |
* |
* |
27 |
* |
* |
28 |
* $Id: emul.c,v 1.247 2006/02/04 12:27:12 debug Exp $ |
* $Id: emul.c,v 1.302 2007/08/29 20:36:49 debug Exp $ |
29 |
* |
* |
30 |
* Emulation startup and misc. routines. |
* Emulation startup and misc. routines. |
31 |
*/ |
*/ |
39 |
#include <unistd.h> |
#include <unistd.h> |
40 |
|
|
41 |
#include "arcbios.h" |
#include "arcbios.h" |
|
#include "bintrans.h" |
|
42 |
#include "cpu.h" |
#include "cpu.h" |
43 |
#include "emul.h" |
#include "emul.h" |
44 |
#include "console.h" |
#include "console.h" |
51 |
#include "mips_cpu_types.h" |
#include "mips_cpu_types.h" |
52 |
#include "misc.h" |
#include "misc.h" |
53 |
#include "net.h" |
#include "net.h" |
54 |
#include "sgi_arcbios.h" |
#include "settings.h" |
55 |
|
#include "timer.h" |
56 |
|
#include "useremul.h" |
57 |
#include "x11.h" |
#include "x11.h" |
58 |
|
|
59 |
|
|
|
extern int force_debugger_at_exit; |
|
|
|
|
60 |
extern int extra_argc; |
extern int extra_argc; |
61 |
extern char **extra_argv; |
extern char **extra_argv; |
62 |
|
|
63 |
extern int verbose; |
extern int verbose; |
64 |
extern int quiet_mode; |
extern int quiet_mode; |
65 |
|
extern int force_debugger_at_exit; |
66 |
extern struct emul *debugger_emul; |
extern int single_step; |
67 |
extern struct diskimage *diskimages[]; |
extern int old_show_trace_tree; |
68 |
|
extern int old_instruction_trace; |
69 |
static char *diskimage_types[] = DISKIMAGE_TYPES; |
extern int old_quiet_mode; |
70 |
|
extern int quiet_mode; |
|
|
|
|
static void print_separator(void) |
|
|
{ |
|
|
int i = 79; |
|
|
while (i-- > 0) |
|
|
debug("-"); |
|
|
debug("\n"); |
|
|
} |
|
71 |
|
|
72 |
|
|
73 |
/* |
/* |
74 |
* add_dump_points(): |
* add_breakpoints(): |
75 |
* |
* |
76 |
* Take the strings breakpoint_string[] and convert to addresses |
* Take the strings breakpoint_string[] and convert to addresses |
77 |
* (and store them in breakpoint_addr[]). |
* (and store them in breakpoint_addr[]). |
78 |
* |
* |
79 |
* TODO: This function should be moved elsewhere. |
* TODO: This function should be moved elsewhere. |
80 |
*/ |
*/ |
81 |
static void add_dump_points(struct machine *m) |
static void add_breakpoints(struct machine *m) |
82 |
{ |
{ |
83 |
int i; |
int i; |
84 |
int string_flag; |
int string_flag; |
85 |
uint64_t dp; |
uint64_t dp; |
86 |
|
|
87 |
for (i=0; i<m->n_breakpoints; i++) { |
for (i=0; i<m->breakpoints.n; i++) { |
88 |
string_flag = 0; |
string_flag = 0; |
89 |
dp = strtoull(m->breakpoint_string[i], NULL, 0); |
dp = strtoull(m->breakpoints.string[i], NULL, 0); |
90 |
|
|
91 |
/* |
/* |
92 |
* If conversion resulted in 0, then perhaps it is a |
* If conversion resulted in 0, then perhaps it is a |
95 |
if (dp == 0) { |
if (dp == 0) { |
96 |
uint64_t addr; |
uint64_t addr; |
97 |
int res = get_symbol_addr(&m->symbol_context, |
int res = get_symbol_addr(&m->symbol_context, |
98 |
m->breakpoint_string[i], &addr); |
m->breakpoints.string[i], &addr); |
99 |
if (!res) |
if (!res) { |
100 |
fprintf(stderr, |
fprintf(stderr, |
101 |
"WARNING! Breakpoint '%s' could not be" |
"ERROR! Breakpoint '%s' could not be" |
102 |
" parsed\n", |
" parsed\n", |
103 |
m->breakpoint_string[i]); |
m->breakpoints.string[i]); |
104 |
else { |
exit(1); |
105 |
|
} else { |
106 |
dp = addr; |
dp = addr; |
107 |
string_flag = 1; |
string_flag = 1; |
108 |
} |
} |
118 |
dp |= 0xffffffff00000000ULL; |
dp |= 0xffffffff00000000ULL; |
119 |
} |
} |
120 |
|
|
121 |
m->breakpoint_addr[i] = dp; |
m->breakpoints.addr[i] = dp; |
122 |
|
|
123 |
debug("breakpoint %i: 0x%llx", i, (long long)dp); |
debug("breakpoint %i: 0x%"PRIx64, i, dp); |
124 |
if (string_flag) |
if (string_flag) |
125 |
debug(" (%s)", m->breakpoint_string[i]); |
debug(" (%s)", m->breakpoints.string[i]); |
126 |
debug("\n"); |
debug("\n"); |
127 |
} |
} |
128 |
} |
} |
133 |
*/ |
*/ |
134 |
static void fix_console(void) |
static void fix_console(void) |
135 |
{ |
{ |
136 |
console_deinit(); |
console_deinit_main(); |
137 |
} |
} |
138 |
|
|
139 |
|
|
140 |
/* |
/* |
141 |
* iso_load_bootblock(): |
* emul_new(): |
|
* |
|
|
* Try to load a kernel from an ISO 9660 disk image. iso_type is 1 for |
|
|
* "CD001" (standard), 2 for "CDW01" (ECMA), and 3 for "CDROM" (Sierra). |
|
|
* |
|
|
* TODO: This function uses too many magic offsets and so on; it should be |
|
|
* cleaned up some day. |
|
142 |
* |
* |
143 |
* Returns 1 on success, 0 on failure. |
* Returns a reasonably initialized struct emul. |
144 |
*/ |
*/ |
145 |
static int iso_load_bootblock(struct machine *m, struct cpu *cpu, |
struct emul *emul_new(char *name) |
|
int disk_id, int disk_type, int iso_type, unsigned char *buf, |
|
|
int *n_loadp, char ***load_namesp) |
|
146 |
{ |
{ |
147 |
char str[35]; |
struct emul *e; |
|
int filenr, i, ofs, dirlen, res = 0, res2, iadd = DEBUG_INDENTATION; |
|
|
int found_dir; |
|
|
uint64_t dirofs; |
|
|
uint64_t fileofs, filelen; |
|
|
unsigned char *dirbuf = NULL, *dp; |
|
|
unsigned char *match_entry = NULL; |
|
|
char *p, *filename_orig; |
|
|
char *filename = strdup(cpu->machine->boot_kernel_filename); |
|
|
unsigned char *filebuf = NULL; |
|
|
char *tmpfname = NULL; |
|
|
char **new_array; |
|
|
int tmpfile_handle; |
|
|
|
|
|
if (filename == NULL) { |
|
|
fatal("out of memory\n"); |
|
|
exit(1); |
|
|
} |
|
|
filename_orig = filename; |
|
|
|
|
|
debug("ISO9660 boot:\n"); |
|
|
debug_indentation(iadd); |
|
|
|
|
|
/* Volume ID: */ |
|
|
ofs = iso_type == 3? 48 : 40; |
|
|
memcpy(str, buf + ofs, sizeof(str)); |
|
|
str[32] = '\0'; i = 31; |
|
|
while (i >= 0 && str[i]==' ') |
|
|
str[i--] = '\0'; |
|
|
if (str[0]) |
|
|
debug("\"%s\"", str); |
|
|
else { |
|
|
/* System ID: */ |
|
|
ofs = iso_type == 3? 16 : 8; |
|
|
memcpy(str, buf + ofs, sizeof(str)); |
|
|
str[32] = '\0'; i = 31; |
|
|
while (i >= 0 && str[i]==' ') |
|
|
str[i--] = '\0'; |
|
|
if (str[0]) |
|
|
debug("\"%s\"", str); |
|
|
else |
|
|
debug("(no ID)"); |
|
|
} |
|
|
|
|
|
debug(":%s\n", filename); |
|
|
|
|
|
|
|
|
/* |
|
|
* Traverse the directory structure to find the kernel. |
|
|
*/ |
|
|
|
|
|
dirlen = buf[0x84] + 256*buf[0x85] + 65536*buf[0x86]; |
|
|
if (dirlen != buf[0x8b] + 256*buf[0x8a] + 65536*buf[0x89]) |
|
|
fatal("WARNING: Root directory length mismatch?\n"); |
|
|
|
|
|
dirofs = (int64_t)(buf[0x8c] + (buf[0x8d] << 8) + (buf[0x8e] << 16) + |
|
|
((uint64_t)buf[0x8f] << 24)) * 2048; |
|
|
|
|
|
/* debug("root = %i bytes at 0x%llx\n", dirlen, (long long)dirofs); */ |
|
|
|
|
|
dirbuf = malloc(dirlen); |
|
|
if (dirbuf == NULL) { |
|
|
fatal("out of memory in iso_load_bootblock()\n"); |
|
|
exit(1); |
|
|
} |
|
|
|
|
|
res2 = diskimage_access(m, disk_id, disk_type, 0, dirofs, dirbuf, |
|
|
dirlen); |
|
|
if (!res2) { |
|
|
fatal("Couldn't read the disk image. Aborting.\n"); |
|
|
goto ret; |
|
|
} |
|
|
|
|
|
found_dir = 1; /* Assume root dir */ |
|
|
dp = dirbuf; filenr = 1; |
|
|
p = NULL; |
|
|
while (dp < dirbuf + dirlen) { |
|
|
size_t i, nlen = dp[0]; |
|
|
int x = dp[2] + (dp[3] << 8) + (dp[4] << 16) + |
|
|
((uint64_t)dp[5] << 24); |
|
|
int y = dp[6] + (dp[7] << 8); |
|
|
char direntry[65]; |
|
|
|
|
|
dp += 8; |
|
|
|
|
|
/* |
|
|
* As long as there is an \ or / in the filename, then we |
|
|
* have not yet found the directory. |
|
|
*/ |
|
|
p = strchr(filename, '/'); |
|
|
if (p == NULL) |
|
|
p = strchr(filename, '\\'); |
|
|
|
|
|
/* debug("%i%s: %i, %i, \"", filenr, filenr == found_dir? |
|
|
" [CURRENT]" : "", x, y); */ |
|
|
for (i=0; i<nlen && i<sizeof(direntry)-1; i++) |
|
|
if (dp[i]) { |
|
|
direntry[i] = dp[i]; |
|
|
/* debug("%c", dp[i]); */ |
|
|
} else |
|
|
break; |
|
|
/* debug("\"\n"); */ |
|
|
direntry[i] = '\0'; |
|
|
|
|
|
/* A directory name match? */ |
|
|
if (p != NULL && strncasecmp(filename, direntry, nlen) == 0 |
|
|
&& nlen == (size_t)p - (size_t)filename && found_dir == y) { |
|
|
found_dir = filenr; |
|
|
filename = p+1; |
|
|
dirofs = 2048 * (int64_t)x; |
|
|
} |
|
|
|
|
|
dp += nlen; |
|
|
|
|
|
/* 16-bit aligned lenght: */ |
|
|
if (nlen & 1) |
|
|
dp ++; |
|
|
|
|
|
filenr ++; |
|
|
} |
|
|
|
|
|
p = strchr(filename, '/'); |
|
|
if (p == NULL) |
|
|
p = strchr(filename, '\\'); |
|
|
|
|
|
if (p != NULL) { |
|
|
char *blah = filename_orig; |
|
|
|
|
|
fatal("could not find '%s' in /", filename); |
|
|
|
|
|
/* Print the first part of the filename: */ |
|
|
while (blah != filename) |
|
|
fatal("%c", *blah++); |
|
|
|
|
|
fatal("\n"); |
|
|
goto ret; |
|
|
} |
|
|
|
|
|
/* debug("dirofs = 0x%llx\n", (long long)dirofs); */ |
|
|
|
|
|
/* Free the old dirbuf, and allocate a new one: */ |
|
|
free(dirbuf); |
|
|
dirbuf = malloc(512); |
|
|
if (dirbuf == NULL) { |
|
|
fatal("out of memory in iso_load_bootblock()\n"); |
|
|
exit(1); |
|
|
} |
|
|
|
|
|
for (;;) { |
|
|
size_t len, i; |
|
|
|
|
|
/* Too close to another sector? Then realign. */ |
|
|
if ((dirofs & 2047) + 70 > 2047) { |
|
|
dirofs = (dirofs | 2047) + 1; |
|
|
/* debug("realign dirofs = 0x%llx\n", dirofs); */ |
|
|
} |
|
|
|
|
|
res2 = diskimage_access(m, disk_id, disk_type, 0, dirofs, |
|
|
dirbuf, 256); |
|
|
if (!res2) { |
|
|
fatal("Couldn't read the disk image. Aborting.\n"); |
|
|
goto ret; |
|
|
} |
|
|
|
|
|
dp = dirbuf; |
|
|
len = dp[0]; |
|
|
if (len < 2) |
|
|
break; |
|
|
|
|
|
/* |
|
|
* TODO: Actually parse the directory entry! |
|
|
* |
|
|
* Haha, this must be rewritten. |
|
|
*/ |
|
|
for (i=32; i<len; i++) { |
|
|
if (i < len - strlen(filename)) |
|
|
if (strncasecmp(filename, (char *)dp + i, |
|
|
strlen(filename)) == 0) { |
|
|
/* The filename was found somewhere |
|
|
in the directory entry. */ |
|
|
if (match_entry != NULL) { |
|
|
fatal("TODO: I'm too lazy to" |
|
|
" implement a correct " |
|
|
"directory parser right " |
|
|
"now... (BUG)\n"); |
|
|
exit(1); |
|
|
} |
|
|
match_entry = malloc(512); |
|
|
if (match_entry == NULL) { |
|
|
fatal("out of memory\n"); |
|
|
exit(1); |
|
|
} |
|
|
memcpy(match_entry, dp, 512); |
|
|
break; |
|
|
} |
|
|
} |
|
|
|
|
|
dirofs += len; |
|
|
} |
|
|
|
|
|
if (match_entry == NULL) { |
|
|
char *blah = filename_orig; |
|
|
|
|
|
fatal("could not find '%s' in /", filename); |
|
|
|
|
|
/* Print the first part of the filename: */ |
|
|
while (blah != filename) |
|
|
fatal("%c", *blah++); |
|
|
|
|
|
fatal("\n"); |
|
|
goto ret; |
|
|
} |
|
|
|
|
|
fileofs = match_entry[2] + (match_entry[3] << 8) + |
|
|
(match_entry[4] << 16) + ((uint64_t)match_entry[5] << 24); |
|
|
filelen = match_entry[10] + (match_entry[11] << 8) + |
|
|
(match_entry[12] << 16) + ((uint64_t)match_entry[13] << 24); |
|
|
fileofs *= 2048; |
|
|
|
|
|
/* debug("filelen=%llx fileofs=%llx\n", (long long)filelen, |
|
|
(long long)fileofs); */ |
|
|
|
|
|
filebuf = malloc(filelen); |
|
|
if (filebuf == NULL) { |
|
|
fatal("could not allocate %lli bytes to read the file" |
|
|
" from the disk image!\n", (long long)filelen); |
|
|
goto ret; |
|
|
} |
|
|
|
|
|
tmpfname = strdup("/tmp/gxemul.XXXXXXXXXXXX"); |
|
|
|
|
|
res2 = diskimage_access(m, disk_id, disk_type, 0, fileofs, filebuf, |
|
|
filelen); |
|
|
if (!res2) { |
|
|
fatal("could not read the file from the disk image!\n"); |
|
|
goto ret; |
|
|
} |
|
|
|
|
|
tmpfile_handle = mkstemp(tmpfname); |
|
|
if (tmpfile_handle < 0) { |
|
|
fatal("could not create %s\n", tmpfname); |
|
|
exit(1); |
|
|
} |
|
|
write(tmpfile_handle, filebuf, filelen); |
|
|
close(tmpfile_handle); |
|
|
|
|
|
debug("extracted %lli bytes into %s\n", (long long)filelen, tmpfname); |
|
|
|
|
|
/* Add the temporary filename to the load_namesp array: */ |
|
|
(*n_loadp)++; |
|
|
new_array = malloc(sizeof(char *) * (*n_loadp)); |
|
|
if (new_array == NULL) { |
|
|
fatal("out of memory\n"); |
|
|
exit(1); |
|
|
} |
|
|
memcpy(new_array, *load_namesp, sizeof(char *) * (*n_loadp)); |
|
|
*load_namesp = new_array; |
|
|
|
|
|
/* This adds a Backspace char in front of the filename; this |
|
|
is a special hack which causes the file to be removed once |
|
|
it has been loaded. */ |
|
|
tmpfname = realloc(tmpfname, strlen(tmpfname) + 2); |
|
|
memmove(tmpfname + 1, tmpfname, strlen(tmpfname) + 1); |
|
|
tmpfname[0] = 8; |
|
|
|
|
|
(*load_namesp)[*n_loadp - 1] = tmpfname; |
|
|
|
|
|
res = 1; |
|
|
|
|
|
ret: |
|
|
if (dirbuf != NULL) |
|
|
free(dirbuf); |
|
|
|
|
|
if (filebuf != NULL) |
|
|
free(filebuf); |
|
148 |
|
|
149 |
if (match_entry != NULL) |
CHECK_ALLOCATION(e = malloc(sizeof(struct emul))); |
150 |
free(match_entry); |
memset(e, 0, sizeof(struct emul)); |
151 |
|
|
152 |
free(filename_orig); |
e->settings = settings_new(); |
153 |
|
|
154 |
debug_indentation(-iadd); |
settings_add(e->settings, "n_machines", 0, |
155 |
return res; |
SETTINGS_TYPE_INT, SETTINGS_FORMAT_DECIMAL, |
156 |
} |
(void *) &e->n_machines); |
157 |
|
|
158 |
|
/* TODO: More settings? */ |
159 |
|
|
160 |
/* |
/* Sane default values: */ |
161 |
* apple_load_bootblock(): |
e->n_machines = 0; |
162 |
* |
e->next_serial_nr = 1; |
|
* 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 = ((uint64_t)buf[ofs + 8] << 24) + (buf[ofs + 9] << 16) + |
|
|
(buf[ofs + 10] << 8) + buf[ofs + 11]; |
|
|
length = ((uint64_t)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); |
|
163 |
|
|
164 |
if (n_hfs_partitions == 0) { |
if (name != NULL) { |
165 |
fatal("Error: No HFS partition found! TODO\n"); |
CHECK_ALLOCATION(e->name = strdup(name)); |
166 |
return 0; |
settings_add(e->settings, "name", 0, |
167 |
} |
SETTINGS_TYPE_STRING, SETTINGS_FORMAT_STRING, |
168 |
if (n_hfs_partitions >= 2) { |
(void *) &e->name); |
|
fatal("Error: Too many HFS partitions found! TODO\n"); |
|
|
return 0; |
|
169 |
} |
} |
170 |
|
|
171 |
return 0; |
return e; |
172 |
} |
} |
173 |
|
|
174 |
|
|
175 |
/* |
/* |
176 |
* load_bootblock(): |
* emul_destroy(): |
|
* |
|
|
* For some emulation modes, it is possible to boot from a harddisk image by |
|
|
* loading a bootblock from a specific disk offset into memory, and executing |
|
|
* that, instead of requiring a separate kernel file. It is then up to the |
|
|
* bootblock to load a kernel. |
|
177 |
* |
* |
178 |
* Returns 1 on success, 0 on failure. |
* Destroys a previously created emul object. |
179 |
*/ |
*/ |
180 |
static int load_bootblock(struct machine *m, struct cpu *cpu, |
void emul_destroy(struct emul *emul) |
|
int *n_loadp, char ***load_namesp) |
|
181 |
{ |
{ |
182 |
int boot_disk_id, boot_disk_type = 0, n_blocks, res, readofs, |
int i; |
|
iso_type, retval = 0; |
|
|
unsigned char minibuf[0x20]; |
|
|
unsigned char *bootblock_buf; |
|
|
uint64_t bootblock_offset; |
|
|
uint64_t bootblock_loadaddr, bootblock_pc; |
|
|
|
|
|
boot_disk_id = diskimage_bootdev(m, &boot_disk_type); |
|
|
if (boot_disk_id < 0) |
|
|
return 0; |
|
|
|
|
|
switch (m->machine_type) { |
|
|
case MACHINE_PMAX: |
|
|
/* |
|
|
* The first few bytes of a disk contains information about |
|
|
* where the bootblock(s) are located. (These are all 32-bit |
|
|
* little-endian words.) |
|
|
* |
|
|
* Offset 0x10 = load address |
|
|
* 0x14 = initial PC value |
|
|
* 0x18 = nr of 512-byte blocks to read |
|
|
* 0x1c = offset on disk to where the bootblocks |
|
|
* are (in 512-byte units) |
|
|
* 0x20 = nr of blocks to read... |
|
|
* 0x24 = offset... |
|
|
* |
|
|
* nr of blocks to read and offset are repeated until nr of |
|
|
* blocks to read is zero. |
|
|
*/ |
|
|
res = diskimage_access(m, boot_disk_id, boot_disk_type, 0, 0, |
|
|
minibuf, sizeof(minibuf)); |
|
|
|
|
|
bootblock_loadaddr = minibuf[0x10] + (minibuf[0x11] << 8) |
|
|
+ (minibuf[0x12] << 16) + ((uint64_t)minibuf[0x13] << 24); |
|
|
|
|
|
/* Convert loadaddr to uncached: */ |
|
|
if ((bootblock_loadaddr & 0xf0000000ULL) != 0x80000000 && |
|
|
(bootblock_loadaddr & 0xf0000000ULL) != 0xa0000000) |
|
|
fatal("\nWARNING! Weird load address 0x%08x.\n\n", |
|
|
(int)bootblock_loadaddr); |
|
|
bootblock_loadaddr &= 0x0fffffffULL; |
|
|
bootblock_loadaddr |= 0xffffffffa0000000ULL; |
|
|
|
|
|
bootblock_pc = minibuf[0x14] + (minibuf[0x15] << 8) |
|
|
+ (minibuf[0x16] << 16) + ((uint64_t)minibuf[0x17] << 24); |
|
|
|
|
|
bootblock_pc &= 0x0fffffffULL; |
|
|
bootblock_pc |= 0xffffffffa0000000ULL; |
|
|
cpu->pc = bootblock_pc; |
|
|
|
|
|
debug("DEC boot: loadaddr=0x%08x, pc=0x%08x", |
|
|
(int)bootblock_loadaddr, (int)bootblock_pc); |
|
|
|
|
|
readofs = 0x18; |
|
|
|
|
|
for (;;) { |
|
|
res = diskimage_access(m, boot_disk_id, boot_disk_type, |
|
|
0, readofs, minibuf, sizeof(minibuf)); |
|
|
if (!res) { |
|
|
fatal("Couldn't read the disk image. " |
|
|
"Aborting.\n"); |
|
|
return 0; |
|
|
} |
|
|
|
|
|
n_blocks = minibuf[0] + (minibuf[1] << 8) |
|
|
+ (minibuf[2] << 16) + ((uint64_t)minibuf[3] << 24); |
|
|
|
|
|
bootblock_offset = (minibuf[4] + (minibuf[5] << 8) + |
|
|
(minibuf[6]<<16) + ((uint64_t)minibuf[7]<<24)) * 512; |
|
|
|
|
|
if (n_blocks < 1) |
|
|
break; |
|
|
|
|
|
debug(readofs == 0x18? ": %i" : " + %i", n_blocks); |
|
|
|
|
|
if (n_blocks * 512 > 65536) |
|
|
fatal("\nWARNING! Unusually large bootblock " |
|
|
"(%i bytes)\n\n", n_blocks * 512); |
|
|
|
|
|
bootblock_buf = malloc(n_blocks * 512); |
|
|
if (bootblock_buf == NULL) { |
|
|
fprintf(stderr, "out of memory in " |
|
|
"load_bootblock()\n"); |
|
|
exit(1); |
|
|
} |
|
|
|
|
|
res = diskimage_access(m, boot_disk_id, boot_disk_type, |
|
|
0, bootblock_offset, bootblock_buf, n_blocks * 512); |
|
|
if (!res) { |
|
|
fatal("WARNING: could not load bootblocks from" |
|
|
" disk offset 0x%llx\n", |
|
|
(long long)bootblock_offset); |
|
|
} |
|
|
|
|
|
store_buf(cpu, bootblock_loadaddr, |
|
|
(char *)bootblock_buf, n_blocks * 512); |
|
|
|
|
|
bootblock_loadaddr += 512*n_blocks; |
|
|
free(bootblock_buf); |
|
|
readofs += 8; |
|
|
} |
|
|
|
|
|
debug(readofs == 0x18? ": no blocks?\n" : " blocks\n"); |
|
|
return 1; |
|
|
|
|
|
case MACHINE_X86: |
|
|
/* TODO: "El Torito" etc? */ |
|
|
if (diskimage_is_a_cdrom(cpu->machine, boot_disk_id, |
|
|
boot_disk_type)) |
|
|
break; |
|
|
|
|
|
bootblock_buf = malloc(512); |
|
|
if (bootblock_buf == NULL) { |
|
|
fprintf(stderr, "Out of memory.\n"); |
|
|
exit(1); |
|
|
} |
|
|
|
|
|
debug("loading PC bootsector from %s id %i\n", |
|
|
diskimage_types[boot_disk_type], boot_disk_id); |
|
|
|
|
|
res = diskimage_access(m, boot_disk_id, boot_disk_type, 0, 0, |
|
|
bootblock_buf, 512); |
|
|
if (!res) { |
|
|
fatal("Couldn't read the disk image. Aborting.\n"); |
|
|
return 0; |
|
|
} |
|
|
|
|
|
if (bootblock_buf[510] != 0x55 || bootblock_buf[511] != 0xaa) |
|
|
debug("WARNING! The 0x55,0xAA marker is missing! " |
|
|
"Booting anyway.\n"); |
|
|
store_buf(cpu, 0x7c00, (char *)bootblock_buf, 512); |
|
|
free(bootblock_buf); |
|
|
|
|
|
return 1; |
|
|
} |
|
|
|
|
|
|
|
|
/* |
|
|
* Try reading a kernel manually from the disk. The code here |
|
|
* does not rely on machine-dependent boot blocks etc. |
|
|
*/ |
|
|
/* ISO9660: (0x800 bytes at 0x8000) */ |
|
|
bootblock_buf = malloc(0x800); |
|
|
if (bootblock_buf == NULL) { |
|
|
fprintf(stderr, "Out of memory.\n"); |
|
|
exit(1); |
|
|
} |
|
|
|
|
|
res = diskimage_access(m, boot_disk_id, boot_disk_type, |
|
|
0, 0x8000, bootblock_buf, 0x800); |
|
|
if (!res) { |
|
|
fatal("Couldn't read the disk image. Aborting.\n"); |
|
|
return 0; |
|
|
} |
|
|
|
|
|
iso_type = 0; |
|
|
if (strncmp((char *)bootblock_buf+1, "CD001", 5) == 0) |
|
|
iso_type = 1; |
|
|
if (strncmp((char *)bootblock_buf+1, "CDW01", 5) == 0) |
|
|
iso_type = 2; |
|
|
if (strncmp((char *)bootblock_buf+1, "CDROM", 5) == 0) |
|
|
iso_type = 3; |
|
|
|
|
|
if (iso_type != 0) { |
|
|
/* 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("\nISO9660 filesystem, but no kernel " |
|
|
"specified? (Use the -j option.)\n"); |
|
|
else |
|
|
retval = iso_load_bootblock(m, cpu, boot_disk_id, |
|
|
boot_disk_type, iso_type, bootblock_buf, |
|
|
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; |
|
|
} |
|
|
|
|
183 |
|
|
184 |
/* |
if (emul->name != NULL) { |
185 |
* emul_new(): |
settings_remove(emul->settings, "name"); |
186 |
* |
free(emul->name); |
|
* Returns a reasonably initialized struct emul. |
|
|
*/ |
|
|
struct emul *emul_new(char *name) |
|
|
{ |
|
|
struct emul *e; |
|
|
e = malloc(sizeof(struct emul)); |
|
|
if (e == NULL) { |
|
|
fprintf(stderr, "out of memory in emul_new()\n"); |
|
|
exit(1); |
|
187 |
} |
} |
188 |
|
|
189 |
memset(e, 0, sizeof(struct emul)); |
for (i=0; i<emul->n_machines; i++) |
190 |
|
machine_destroy(emul->machines[i]); |
191 |
|
|
192 |
/* Sane default values: */ |
if (emul->machines != NULL) |
193 |
e->n_machines = 0; |
free(emul->machines); |
|
e->next_serial_nr = 1; |
|
194 |
|
|
195 |
if (name != NULL) { |
/* Remove any remaining level-1 settings: */ |
196 |
e->name = strdup(name); |
settings_remove_all(emul->settings); |
197 |
if (e->name == NULL) { |
settings_destroy(emul->settings); |
|
fprintf(stderr, "out of memory in emul_new()\n"); |
|
|
exit(1); |
|
|
} |
|
|
} |
|
198 |
|
|
199 |
return e; |
free(emul); |
200 |
} |
} |
201 |
|
|
202 |
|
|
211 |
struct machine *emul_add_machine(struct emul *e, char *name) |
struct machine *emul_add_machine(struct emul *e, char *name) |
212 |
{ |
{ |
213 |
struct machine *m; |
struct machine *m; |
214 |
|
char tmpstr[20]; |
215 |
|
int i; |
216 |
|
|
217 |
m = machine_new(name, e); |
m = machine_new(name, e, e->n_machines); |
218 |
m->serial_nr = (e->next_serial_nr ++); |
m->serial_nr = (e->next_serial_nr ++); |
219 |
|
|
220 |
e->n_machines ++; |
i = e->n_machines ++; |
221 |
e->machines = realloc(e->machines, |
|
222 |
sizeof(struct machine *) * e->n_machines); |
CHECK_ALLOCATION(e->machines = realloc(e->machines, |
223 |
if (e->machines == NULL) { |
sizeof(struct machine *) * e->n_machines)); |
224 |
fprintf(stderr, "emul_add_machine(): out of memory\n"); |
|
225 |
exit(1); |
e->machines[i] = m; |
226 |
} |
|
227 |
|
snprintf(tmpstr, sizeof(tmpstr), "machine[%i]", i); |
228 |
|
settings_add(e->settings, tmpstr, 1, SETTINGS_TYPE_SUBSETTINGS, 0, |
229 |
|
e->machines[i]->settings); |
230 |
|
|
|
e->machines[e->n_machines - 1] = m; |
|
231 |
return m; |
return m; |
232 |
} |
} |
233 |
|
|
361 |
uint64_t memory_amount, entrypoint = 0, gp = 0, toc = 0; |
uint64_t memory_amount, entrypoint = 0, gp = 0, toc = 0; |
362 |
int byte_order; |
int byte_order; |
363 |
|
|
364 |
debug("machine \"%s\":\n", m->name); |
if (m->name != NULL) |
365 |
|
debug("machine \"%s\":\n", m->name); |
366 |
|
else |
367 |
|
debug("machine:\n"); |
368 |
|
|
369 |
debug_indentation(iadd); |
debug_indentation(iadd); |
370 |
|
|
371 |
/* For userland-only, this decides which ARCH/cpu_name to use: */ |
/* For userland-only, this decides which ARCH/cpu_name to use: */ |
388 |
if (m->arch == ARCH_ALPHA) |
if (m->arch == ARCH_ALPHA) |
389 |
m->arch_pagesize = 8192; |
m->arch_pagesize = 8192; |
390 |
|
|
|
if (m->arch != ARCH_MIPS) |
|
|
m->bintrans_enable = 0; |
|
|
|
|
391 |
machine_memsize_fix(m); |
machine_memsize_fix(m); |
392 |
|
|
393 |
/* |
/* |
419 |
/* TODO: This should be moved elsewhere... */ |
/* TODO: This should be moved elsewhere... */ |
420 |
if (m->machine_type == MACHINE_BEBOX) |
if (m->machine_type == MACHINE_BEBOX) |
421 |
m->ncpus = 2; |
m->ncpus = 2; |
|
else if (m->machine_type == MACHINE_ARC && |
|
|
m->machine_subtype == MACHINE_ARC_NEC_R96) |
|
|
m->ncpus = 2; |
|
|
else if (m->machine_type == MACHINE_ARC && |
|
|
m->machine_subtype == MACHINE_ARC_NEC_R98) |
|
|
m->ncpus = 4; |
|
422 |
else |
else |
423 |
m->ncpus = 1; |
m->ncpus = 1; |
424 |
} |
} |
|
m->cpus = malloc(sizeof(struct cpu *) * m->ncpus); |
|
|
if (m->cpus == NULL) { |
|
|
fprintf(stderr, "out of memory\n"); |
|
|
exit(1); |
|
|
} |
|
|
memset(m->cpus, 0, sizeof(struct cpu *) * m->ncpus); |
|
425 |
|
|
426 |
/* Initialize dynamic binary translation, if available: */ |
CHECK_ALLOCATION(m->cpus = malloc(sizeof(struct cpu *) * m->ncpus)); |
427 |
if (m->bintrans_enable) |
memset(m->cpus, 0, sizeof(struct cpu *) * m->ncpus); |
|
bintrans_init(m, m->memory); |
|
428 |
|
|
429 |
debug("cpu0"); |
debug("cpu0"); |
430 |
if (m->ncpus > 1) |
if (m->ncpus > 1) |
432 |
debug(": "); |
debug(": "); |
433 |
for (i=0; i<m->ncpus; i++) { |
for (i=0; i<m->ncpus; i++) { |
434 |
m->cpus[i] = cpu_new(m->memory, m, i, m->cpu_name); |
m->cpus[i] = cpu_new(m->memory, m, i, m->cpu_name); |
435 |
if (m->bintrans_enable) |
if (m->cpus[i] == NULL) { |
436 |
bintrans_init_cpu(m->cpus[i]); |
fprintf(stderr, "Unable to create CPU object. " |
437 |
|
"Aborting."); |
438 |
|
exit(1); |
439 |
|
} |
440 |
} |
} |
441 |
debug("\n"); |
debug("\n"); |
442 |
|
|
|
#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 |
|
|
|
|
443 |
if (m->use_random_bootstrap_cpu) |
if (m->use_random_bootstrap_cpu) |
444 |
m->bootstrap_cpu = random() % m->ncpus; |
m->bootstrap_cpu = random() % m->ncpus; |
445 |
else |
else |
453 |
m->userland_emul, NULL, NULL, NULL); |
m->userland_emul, NULL, NULL, NULL); |
454 |
|
|
455 |
switch (m->arch) { |
switch (m->arch) { |
456 |
#ifdef ENABLE_ALPHA |
|
457 |
case ARCH_ALPHA: |
case ARCH_ALPHA: |
458 |
cpu->memory_rw = alpha_userland_memory_rw; |
cpu->memory_rw = alpha_userland_memory_rw; |
459 |
break; |
break; |
460 |
#endif |
|
461 |
default:cpu->memory_rw = userland_memory_rw; |
default: |
462 |
|
cpu->memory_rw = userland_memory_rw; |
463 |
} |
} |
464 |
} |
} |
465 |
|
|
466 |
if (m->use_x11) |
if (m->x11_md.in_use) |
467 |
x11_init(m); |
x11_init(m); |
468 |
|
|
469 |
/* Fill memory with random bytes: */ |
/* Fill memory with random bytes: */ |
532 |
fread(buf, 1, sizeof(buf), tmp_f); |
fread(buf, 1, sizeof(buf), tmp_f); |
533 |
if (buf[0]==0x1f && buf[1]==0x8b) { |
if (buf[0]==0x1f && buf[1]==0x8b) { |
534 |
size_t zzlen = strlen(name_to_load)*2 + 100; |
size_t zzlen = strlen(name_to_load)*2 + 100; |
535 |
char *zz = malloc(zzlen); |
char *zz; |
536 |
|
|
537 |
|
CHECK_ALLOCATION(zz = malloc(zzlen)); |
538 |
debug("gunziping %s\n", name_to_load); |
debug("gunziping %s\n", name_to_load); |
539 |
|
|
540 |
/* |
/* |
541 |
* gzip header found. If this was a file |
* gzip header found. If this was a file |
542 |
* extracted from, say, a CDROM image, then it |
* extracted from, say, a CDROM image, then it |
553 |
} else { |
} else { |
554 |
/* gunzip into new temp file: */ |
/* gunzip into new temp file: */ |
555 |
int tmpfile_handle; |
int tmpfile_handle; |
556 |
char *new_temp_name = |
char *new_temp_name; |
557 |
strdup("/tmp/gxemul.XXXXXXXXXXXX"); |
char *tmpdir = getenv("TMPDIR"); |
558 |
|
|
559 |
|
if (tmpdir == NULL) |
560 |
|
tmpdir = DEFAULT_TMP_DIR; |
561 |
|
|
562 |
|
CHECK_ALLOCATION(new_temp_name = |
563 |
|
malloc(300)); |
564 |
|
snprintf(new_temp_name, 300, |
565 |
|
"%s/gxemul.XXXXXXXXXXXX", tmpdir); |
566 |
|
|
567 |
tmpfile_handle = mkstemp(new_temp_name); |
tmpfile_handle = mkstemp(new_temp_name); |
568 |
close(tmpfile_handle); |
close(tmpfile_handle); |
569 |
snprintf(zz, zzlen, "gunzip -c '%s' > " |
snprintf(zz, zzlen, "gunzip -c '%s' > " |
577 |
fclose(tmp_f); |
fclose(tmp_f); |
578 |
} |
} |
579 |
|
|
|
/* |
|
|
* 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; j<filesize - 4; j++) |
|
|
if (memcmp(buf + j, ELFMAG, SELFMAG) == 0) { |
|
|
found = j; |
|
|
break; |
|
|
} |
|
|
if (found != 0) { |
|
|
int tmpfile_handle; |
|
|
char *new_temp_name = |
|
|
strdup("/tmp/gxemul.XXXXXXXXXXXX"); |
|
|
debug("extracting ELF from %s (offset 0x%x)\n", |
|
|
name_to_load, (int)found); |
|
|
tmpfile_handle = mkstemp(new_temp_name); |
|
|
write(tmpfile_handle, buf + found, |
|
|
filesize - found); |
|
|
close(tmpfile_handle); |
|
|
name_to_load = new_temp_name; |
|
|
remove_after_load = 1; |
|
|
} |
|
|
} |
|
|
|
|
|
/* Special things required _before_ loading the file: */ |
|
|
switch (m->arch) { |
|
|
case ARCH_X86: |
|
|
/* |
|
|
* X86 machines normally don't need to load any files, |
|
|
* they can boot from disk directly. Therefore, an x86 |
|
|
* machine usually boots up in 16-bit real mode. When |
|
|
* loading a 32-bit (or even 64-bit) ELF, that's not |
|
|
* very nice, hence this special case. |
|
|
*/ |
|
|
pc_bios_simple_pmode_setup(cpu); |
|
|
break; |
|
|
} |
|
|
|
|
580 |
byte_order = NO_BYTE_ORDER_OVERRIDE; |
byte_order = NO_BYTE_ORDER_OVERRIDE; |
581 |
|
|
582 |
/* |
/* |
610 |
cpu->pc &= 0xfffffffc; |
cpu->pc &= 0xfffffffc; |
611 |
break; |
break; |
612 |
|
|
613 |
case ARCH_AVR: |
case ARCH_M32R: |
614 |
cpu->pc &= 0xfffff; |
if (cpu->pc & 3) { |
615 |
if (cpu->pc & 1) { |
fatal("M32R: lowest bits of pc set: TODO\n"); |
|
fatal("AVR: lowest bit of pc set: TODO\n"); |
|
616 |
exit(1); |
exit(1); |
617 |
} |
} |
618 |
|
cpu->pc &= 0xfffffffc; |
619 |
break; |
break; |
620 |
|
|
621 |
case ARCH_HPPA: |
case ARCH_M88K: |
622 |
break; |
if (cpu->pc & 3) { |
623 |
|
fatal("M88K: lowest bits of pc set: TODO\n"); |
624 |
case ARCH_I960: |
exit(1); |
625 |
break; |
} |
626 |
|
cpu->pc &= 0xfffffffc; |
|
case ARCH_IA64: |
|
|
break; |
|
|
|
|
|
case ARCH_M68K: |
|
627 |
break; |
break; |
628 |
|
|
629 |
case ARCH_MIPS: |
case ARCH_MIPS: |
648 |
break; |
break; |
649 |
|
|
650 |
case ARCH_SH: |
case ARCH_SH: |
651 |
if (cpu->cd.sh.bits == 32) |
if (cpu->cd.sh.cpu_type.bits == 32) |
652 |
cpu->pc &= 0xffffffffULL; |
cpu->pc &= 0xffffffffULL; |
653 |
cpu->pc &= ~1; |
cpu->pc &= ~1; |
654 |
break; |
break; |
656 |
case ARCH_SPARC: |
case ARCH_SPARC: |
657 |
break; |
break; |
658 |
|
|
|
case ARCH_X86: |
|
|
/* |
|
|
* NOTE: The toc field is used to indicate an ELF32 |
|
|
* or ELF64 load. |
|
|
*/ |
|
|
switch (toc) { |
|
|
case 0: /* 16-bit? TODO */ |
|
|
cpu->pc &= 0xffffffffULL; |
|
|
break; |
|
|
case 1: /* 32-bit. */ |
|
|
cpu->pc &= 0xffffffffULL; |
|
|
break; |
|
|
case 2: /* 64-bit: TODO */ |
|
|
fatal("64-bit x86 load. TODO\n"); |
|
|
exit(1); |
|
|
} |
|
|
break; |
|
|
|
|
659 |
default: |
default: |
660 |
fatal("emul_machine_setup(): Internal error: " |
fatal("emul_machine_setup(): Internal error: " |
661 |
"Unimplemented arch %i\n", m->arch); |
"Unimplemented arch %i\n", m->arch); |
691 |
useremul_setup(cpu, n_load, load_names); |
useremul_setup(cpu, n_load, load_names); |
692 |
|
|
693 |
/* Startup the bootstrap CPU: */ |
/* Startup the bootstrap CPU: */ |
694 |
cpu->bootstrap_cpu_flag = 1; |
cpu->running = 1; |
|
cpu->running = 1; |
|
695 |
|
|
696 |
/* ... or pause all CPUs, if start_paused is set: */ |
/* ... or pause all CPUs, if start_paused is set: */ |
697 |
if (m->start_paused) { |
if (m->start_paused) { |
699 |
m->cpus[i]->running = 0; |
m->cpus[i]->running = 0; |
700 |
} |
} |
701 |
|
|
702 |
/* Add PC dump points: */ |
/* Parse and add breakpoints: */ |
703 |
add_dump_points(m); |
add_breakpoints(m); |
704 |
|
|
705 |
/* TODO: This is MIPS-specific! */ |
/* TODO: This is MIPS-specific! */ |
706 |
if (m->machine_type == MACHINE_PMAX && |
if (m->machine_type == MACHINE_PMAX && |
710 |
|
|
711 |
symbol_recalc_sizes(&m->symbol_context); |
symbol_recalc_sizes(&m->symbol_context); |
712 |
|
|
|
if (m->max_random_cycles_per_chunk > 0) |
|
|
debug("using random cycle chunks (1 to %i cycles)\n", |
|
|
m->max_random_cycles_per_chunk); |
|
|
|
|
713 |
/* Special hack for ARC/SGI emulation: */ |
/* Special hack for ARC/SGI emulation: */ |
714 |
if ((m->machine_type == MACHINE_ARC || |
if ((m->machine_type == MACHINE_ARC || |
715 |
m->machine_type == MACHINE_SGI) && m->prom_emulation) |
m->machine_type == MACHINE_SGI) && m->prom_emulation) |
716 |
add_arc_components(m); |
add_arc_components(m); |
717 |
|
|
718 |
debug("starting cpu%i at ", m->bootstrap_cpu); |
debug("cpu%i: starting at ", m->bootstrap_cpu); |
|
switch (m->arch) { |
|
|
|
|
|
case ARCH_ARM: |
|
|
/* ARM cpus aren't 64-bit: */ |
|
|
debug("0x%08x", (int)entrypoint); |
|
|
break; |
|
719 |
|
|
720 |
case ARCH_AVR: |
switch (m->arch) { |
|
/* Atmel AVR uses a 16-bit or 22-bit program counter: */ |
|
|
debug("0x%04x", (int)entrypoint); |
|
|
break; |
|
721 |
|
|
722 |
case ARCH_MIPS: |
case ARCH_MIPS: |
723 |
if (cpu->is_32bit) { |
if (cpu->is_32bit) { |
724 |
debug("0x%08x", (int)m->cpus[ |
debug("0x%08"PRIx32, (uint32_t) |
725 |
m->bootstrap_cpu]->pc); |
m->cpus[m->bootstrap_cpu]->pc); |
726 |
if (cpu->cd.mips.gpr[MIPS_GPR_GP] != 0) |
if (cpu->cd.mips.gpr[MIPS_GPR_GP] != 0) |
727 |
debug(" (gp=0x%08x)", (int)m->cpus[ |
debug(" (gp=0x%08"PRIx32")", (uint32_t) |
728 |
m->bootstrap_cpu]->cd.mips.gpr[ |
m->cpus[m->bootstrap_cpu]->cd.mips.gpr[ |
729 |
MIPS_GPR_GP]); |
MIPS_GPR_GP]); |
730 |
} else { |
} else { |
731 |
debug("0x%016llx", (long long)m->cpus[ |
debug("0x%016"PRIx64, (uint64_t) |
732 |
m->bootstrap_cpu]->pc); |
m->cpus[m->bootstrap_cpu]->pc); |
733 |
if (cpu->cd.mips.gpr[MIPS_GPR_GP] != 0) |
if (cpu->cd.mips.gpr[MIPS_GPR_GP] != 0) |
734 |
debug(" (gp=0x%016llx)", (long long) |
debug(" (gp=0x%016"PRIx64")", (uint64_t) |
735 |
cpu->cd.mips.gpr[MIPS_GPR_GP]); |
cpu->cd.mips.gpr[MIPS_GPR_GP]); |
736 |
} |
} |
737 |
break; |
break; |
738 |
|
|
|
case ARCH_PPC: |
|
|
if (cpu->cd.ppc.bits == 32) |
|
|
debug("0x%08x", (int)entrypoint); |
|
|
else |
|
|
debug("0x%016llx", (long long)entrypoint); |
|
|
break; |
|
|
|
|
|
case ARCH_X86: |
|
|
debug("0x%04x:0x%llx", cpu->cd.x86.s[X86_S_CS], |
|
|
(long long)cpu->pc); |
|
|
break; |
|
|
|
|
739 |
default: |
default: |
740 |
if (cpu->is_32bit) |
if (cpu->is_32bit) |
741 |
debug("0x%08x", (int)cpu->pc); |
debug("0x%08"PRIx32, (uint32_t) cpu->pc); |
742 |
else |
else |
743 |
debug("0x%016llx", (long long)cpu->pc); |
debug("0x%016"PRIx64, (uint64_t) cpu->pc); |
744 |
} |
} |
745 |
debug("\n"); |
debug("\n"); |
746 |
|
|
755 |
*/ |
*/ |
756 |
void emul_dumpinfo(struct emul *e) |
void emul_dumpinfo(struct emul *e) |
757 |
{ |
{ |
758 |
int j, nm, iadd = DEBUG_INDENTATION; |
int i; |
759 |
|
|
760 |
if (e->net != NULL) |
if (e->net != NULL) |
761 |
net_dumpinfo(e->net); |
net_dumpinfo(e->net); |
762 |
|
|
763 |
nm = e->n_machines; |
for (i = 0; i < e->n_machines; i++) { |
764 |
for (j=0; j<nm; j++) { |
if (e->n_machines > 1) |
765 |
debug("machine %i: \"%s\"\n", j, e->machines[j]->name); |
debug("machine %i: \"%s\"\n", i, e->machines[i]->name); |
766 |
debug_indentation(iadd); |
else |
767 |
machine_dumpinfo(e->machines[j]); |
debug("machine:\n"); |
768 |
debug_indentation(-iadd); |
|
769 |
|
debug_indentation(DEBUG_INDENTATION); |
770 |
|
|
771 |
|
machine_dumpinfo(e->machines[i]); |
772 |
|
|
773 |
|
debug_indentation(-DEBUG_INDENTATION); |
774 |
} |
} |
775 |
} |
} |
776 |
|
|
805 |
|
|
806 |
/* Create a simple network: */ |
/* Create a simple network: */ |
807 |
emul->net = net_init(emul, NET_INIT_FLAG_GATEWAY, |
emul->net = net_init(emul, NET_INIT_FLAG_GATEWAY, |
808 |
"10.0.0.0", 8, NULL, 0, 0); |
NET_DEFAULT_IPV4_MASK, |
809 |
|
NET_DEFAULT_IPV4_LEN, |
810 |
|
NULL, 0, 0, NULL); |
811 |
} else { |
} else { |
812 |
/* Userland pseudo-machine: */ |
/* Userland pseudo-machine: */ |
813 |
debug("Syscall emulation (userland-only) setup...\n"); |
debug("Syscall emulation (userland-only) setup...\n"); |
830 |
{ |
{ |
831 |
int iadd = DEBUG_INDENTATION; |
int iadd = DEBUG_INDENTATION; |
832 |
struct emul *e = emul_new(fname); |
struct emul *e = emul_new(fname); |
|
FILE *f; |
|
|
char buf[128]; |
|
|
size_t len; |
|
833 |
|
|
834 |
debug("Creating emulation from configfile \"%s\":\n", fname); |
debug("Creating emulation from configfile \"%s\":\n", fname); |
835 |
debug_indentation(iadd); |
debug_indentation(iadd); |
836 |
|
|
837 |
f = fopen(fname, "r"); |
emul_parse_config(e, fname); |
|
if (f == NULL) { |
|
|
perror(fname); |
|
|
exit(1); |
|
|
} |
|
|
|
|
|
/* Read header: (must be !!gxemul) */ |
|
|
len = fread(buf, 1, 8, f); |
|
|
if (len != 8 || strncmp(buf, "!!gxemul", 8) != 0) { |
|
|
fprintf(stderr, "%s: must start with '!!gxemul'\n", fname); |
|
|
exit(1); |
|
|
} |
|
|
|
|
|
/* Restart from beginning: */ |
|
|
rewind(f); |
|
|
|
|
|
emul_parse_config(e, f); |
|
838 |
|
|
|
fclose(f); |
|
839 |
debug_indentation(-iadd); |
debug_indentation(-iadd); |
840 |
return e; |
return e; |
841 |
} |
} |
844 |
/* |
/* |
845 |
* emul_run(): |
* emul_run(): |
846 |
* |
* |
847 |
* o) Set up things needed before running emulations. |
* o) Set up things needed before running an emulation. |
848 |
* |
* |
849 |
* o) Run emulations (one or more, in parallel). |
* o) Run instructions in all machines. |
850 |
* |
* |
851 |
* o) De-initialize things. |
* o) De-initialize things. |
852 |
*/ |
*/ |
853 |
void emul_run(struct emul **emuls, int n_emuls) |
void emul_run(struct emul *emul) |
854 |
{ |
{ |
|
struct emul *e; |
|
855 |
int i = 0, j, go = 1, n, anything; |
int i = 0, j, go = 1, n, anything; |
856 |
|
|
|
if (n_emuls < 1) { |
|
|
fprintf(stderr, "emul_run(): no thing to do\n"); |
|
|
return; |
|
|
} |
|
|
|
|
857 |
atexit(fix_console); |
atexit(fix_console); |
858 |
|
|
859 |
/* Initialize the interactive debugger: */ |
/* Initialize the interactive debugger: */ |
860 |
debugger_init(emuls, n_emuls); |
debugger_init(emul); |
861 |
|
|
862 |
/* Run any additional debugger commands before starting: */ |
/* Run any additional debugger commands before starting: */ |
863 |
for (i=0; i<n_emuls; i++) { |
if (emul->n_debugger_cmds > 0) { |
864 |
struct emul *emul = emuls[i]; |
int j; |
865 |
if (emul->n_debugger_cmds > 0) { |
if (i == 0) |
866 |
int j; |
print_separator_line(); |
867 |
if (i == 0) |
for (j = 0; j < emul->n_debugger_cmds; j ++) { |
868 |
print_separator(); |
debug("> %s\n", emul->debugger_cmds[j]); |
869 |
for (j = 0; j < emul->n_debugger_cmds; j ++) { |
debugger_execute_cmd(emul->debugger_cmds[j], |
870 |
debug("> %s\n", emul->debugger_cmds[j]); |
strlen(emul->debugger_cmds[j])); |
|
debugger_execute_cmd(emul->debugger_cmds[j], |
|
|
strlen(emul->debugger_cmds[j])); |
|
|
} |
|
871 |
} |
} |
872 |
} |
} |
873 |
|
|
874 |
print_separator(); |
print_separator_line(); |
875 |
debug("\n"); |
debug("\n"); |
876 |
|
|
877 |
|
|
885 |
* (or sends SIGSTOP) and then continues. It makes sure that the |
* (or sends SIGSTOP) and then continues. It makes sure that the |
886 |
* terminal is in an expected state. |
* terminal is in an expected state. |
887 |
*/ |
*/ |
888 |
console_init_main(emuls[0]); /* TODO: what is a good argument? */ |
console_init_main(emul); |
889 |
|
|
890 |
signal(SIGINT, debugger_activate); |
signal(SIGINT, debugger_activate); |
891 |
signal(SIGCONT, console_sigcont); |
signal(SIGCONT, console_sigcont); |
892 |
|
|
894 |
if (!verbose) |
if (!verbose) |
895 |
quiet_mode = 1; |
quiet_mode = 1; |
896 |
|
|
897 |
/* Initialize all CPUs in all machines in all emulations: */ |
|
898 |
for (i=0; i<n_emuls; i++) { |
/* Initialize all CPUs in all machines: */ |
899 |
e = emuls[i]; |
for (j=0; j<emul->n_machines; j++) |
900 |
if (e == NULL) |
cpu_run_init(emul->machines[j]); |
|
continue; |
|
|
for (j=0; j<e->n_machines; j++) |
|
|
cpu_run_init(e->machines[j]); |
|
|
} |
|
901 |
|
|
902 |
/* TODO: Generalize: */ |
/* TODO: Generalize: */ |
903 |
if (emuls[0]->machines[0]->show_trace_tree) |
if (emul->machines[0]->show_trace_tree) |
904 |
cpu_functioncall_trace(emuls[0]->machines[0]->cpus[0], |
cpu_functioncall_trace(emul->machines[0]->cpus[0], |
905 |
emuls[0]->machines[0]->cpus[0]->pc); |
emul->machines[0]->cpus[0]->pc); |
906 |
|
|
907 |
|
/* Start emulated clocks: */ |
908 |
|
timer_start(); |
909 |
|
|
910 |
|
|
911 |
/* |
/* |
912 |
* MAIN LOOP: |
* MAIN LOOP: |
913 |
* |
* |
914 |
* Run all emulations in parallel, running each machine in |
* Run all emulations in parallel, running instructions from each |
915 |
* each emulation. |
* cpu in each machine. |
916 |
*/ |
*/ |
917 |
while (go) { |
while (go) { |
918 |
|
struct cpu *bootcpu = emul->machines[0]->cpus[ |
919 |
|
emul->machines[0]->bootstrap_cpu]; |
920 |
|
|
921 |
go = 0; |
go = 0; |
922 |
|
|
923 |
x11_check_event(emuls, n_emuls); |
/* Flush X11 and serial console output every now and then: */ |
924 |
|
if (bootcpu->ninstrs > bootcpu->ninstrs_flush + (1<<19)) { |
925 |
|
x11_check_event(emul); |
926 |
|
console_flush(); |
927 |
|
bootcpu->ninstrs_flush = bootcpu->ninstrs; |
928 |
|
} |
929 |
|
|
930 |
for (i=0; i<n_emuls; i++) { |
if (bootcpu->ninstrs > bootcpu->ninstrs_show + (1<<25)) { |
931 |
e = emuls[i]; |
bootcpu->ninstrs_since_gettimeofday += |
932 |
if (e == NULL) |
(bootcpu->ninstrs - bootcpu->ninstrs_show); |
933 |
continue; |
cpu_show_cycles(emul->machines[0], 0); |
934 |
|
bootcpu->ninstrs_show = bootcpu->ninstrs; |
|
for (j=0; j<e->n_machines; j++) { |
|
|
/* TODO: cpu_run() is a strange name, since |
|
|
there can be multiple cpus in a machine */ |
|
|
anything = cpu_run(e, e->machines[j]); |
|
|
if (anything) |
|
|
go = 1; |
|
|
} |
|
935 |
} |
} |
|
} |
|
936 |
|
|
937 |
/* Deinitialize all CPUs in all machines in all emulations: */ |
if (single_step == ENTER_SINGLE_STEPPING) { |
938 |
for (i=0; i<n_emuls; i++) { |
/* TODO: Cleanup! */ |
939 |
e = emuls[i]; |
old_instruction_trace = |
940 |
if (e == NULL) |
emul->machines[0]->instruction_trace; |
941 |
continue; |
old_quiet_mode = quiet_mode; |
942 |
for (j=0; j<e->n_machines; j++) |
old_show_trace_tree = |
943 |
cpu_run_deinit(e->machines[j]); |
emul->machines[0]->show_trace_tree; |
944 |
|
emul->machines[0]->instruction_trace = 1; |
945 |
|
emul->machines[0]->show_trace_tree = 1; |
946 |
|
quiet_mode = 0; |
947 |
|
single_step = SINGLE_STEPPING; |
948 |
|
} |
949 |
|
|
950 |
|
if (single_step == SINGLE_STEPPING) |
951 |
|
debugger(); |
952 |
|
|
953 |
|
for (j=0; j<emul->n_machines; j++) { |
954 |
|
anything = machine_run(emul->machines[j]); |
955 |
|
if (anything) |
956 |
|
go = 1; |
957 |
|
} |
958 |
} |
} |
959 |
|
|
960 |
|
/* Stop any running timers: */ |
961 |
|
timer_stop(); |
962 |
|
|
963 |
|
/* Deinitialize all CPUs in all machines: */ |
964 |
|
for (j=0; j<emul->n_machines; j++) |
965 |
|
cpu_run_deinit(emul->machines[j]); |
966 |
|
|
967 |
/* force_debugger_at_exit flag set? Then enter the debugger: */ |
/* force_debugger_at_exit flag set? Then enter the debugger: */ |
968 |
if (force_debugger_at_exit) { |
if (force_debugger_at_exit) { |
969 |
quiet_mode = 0; |
quiet_mode = 0; |
971 |
debugger(); |
debugger(); |
972 |
} |
} |
973 |
|
|
974 |
/* Any machine using X11? Then we should wait before exiting: */ |
/* Any machine using X11? Then wait before exiting: */ |
975 |
n = 0; |
n = 0; |
976 |
for (i=0; i<n_emuls; i++) |
for (j=0; j<emul->n_machines; j++) |
977 |
for (j=0; j<emuls[i]->n_machines; j++) |
if (emul->machines[j]->x11_md.in_use) |
978 |
if (emuls[i]->machines[j]->use_x11) |
n++; |
979 |
n++; |
|
980 |
if (n > 0) { |
if (n > 0) { |
981 |
printf("Press enter to quit.\n"); |
printf("Press enter to quit.\n"); |
982 |
while (!console_charavail(MAIN_CONSOLE)) { |
while (!console_charavail(MAIN_CONSOLE)) { |
983 |
x11_check_event(emuls, n_emuls); |
x11_check_event(emul); |
984 |
usleep(1); |
usleep(10000); |
985 |
} |
} |
986 |
console_readchar(MAIN_CONSOLE); |
console_readchar(MAIN_CONSOLE); |
987 |
} |
} |
988 |
|
|
989 |
console_deinit(); |
console_deinit_main(); |
990 |
} |
} |
991 |
|
|