25 |
* SUCH DAMAGE. |
* SUCH DAMAGE. |
26 |
* |
* |
27 |
* |
* |
28 |
* $Id: emul.c,v 1.184 2005/04/20 04:43:52 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" |
67 |
extern struct emul *debugger_emul; |
extern struct emul *debugger_emul; |
68 |
extern struct diskimage *diskimages[]; |
extern struct diskimage *diskimages[]; |
69 |
|
|
70 |
|
static char *diskimage_types[] = DISKIMAGE_TYPES; |
71 |
|
|
72 |
|
|
73 |
/* |
/* |
74 |
* add_dump_points(): |
* add_dump_points(): |
134 |
|
|
135 |
|
|
136 |
/* |
/* |
137 |
|
* iso_load_bootblock(): |
138 |
|
* |
139 |
|
* Try to load a kernel from an ISO 9660 disk image. iso_type is 1 for |
140 |
|
* "CD001" (standard), 2 for "CDW01" (ECMA), and 3 for "CDROM" (Sierra). |
141 |
|
* |
142 |
|
* TODO: This function uses too many magic offsets and so on; it should be |
143 |
|
* cleaned up some day. |
144 |
|
* |
145 |
|
* Returns 1 on success, 0 on failure. |
146 |
|
*/ |
147 |
|
static int iso_load_bootblock(struct machine *m, struct cpu *cpu, |
148 |
|
int disk_id, int disk_type, int iso_type, unsigned char *buf, |
149 |
|
int *n_loadp, char ***load_namesp) |
150 |
|
{ |
151 |
|
char str[35]; |
152 |
|
int filenr, i, ofs, dirlen, res = 0, res2, iadd = 4; |
153 |
|
int found_dir; |
154 |
|
uint64_t dirofs; |
155 |
|
uint64_t fileofs, filelen; |
156 |
|
unsigned char *dirbuf = NULL, *dp; |
157 |
|
unsigned char *match_entry = NULL; |
158 |
|
char *p, *filename_orig; |
159 |
|
char *filename = strdup(cpu->machine->boot_kernel_filename); |
160 |
|
unsigned char *filebuf = NULL; |
161 |
|
char *tmpfilename = NULL; |
162 |
|
char **new_array; |
163 |
|
int tmpfile_handle; |
164 |
|
|
165 |
|
if (filename == NULL) { |
166 |
|
fatal("out of memory\n"); |
167 |
|
exit(1); |
168 |
|
} |
169 |
|
filename_orig = filename; |
170 |
|
|
171 |
|
debug("ISO9660 boot:\n"); |
172 |
|
debug_indentation(iadd); |
173 |
|
|
174 |
|
/* Volume ID: */ |
175 |
|
ofs = iso_type == 3? 48 : 40; |
176 |
|
memcpy(str, buf + ofs, sizeof(str)); |
177 |
|
str[32] = '\0'; i = 31; |
178 |
|
while (i >= 0 && str[i]==' ') |
179 |
|
str[i--] = '\0'; |
180 |
|
if (str[0]) |
181 |
|
debug("\"%s\"", str); |
182 |
|
else { |
183 |
|
/* System ID: */ |
184 |
|
ofs = iso_type == 3? 16 : 8; |
185 |
|
memcpy(str, buf + ofs, sizeof(str)); |
186 |
|
str[32] = '\0'; i = 31; |
187 |
|
while (i >= 0 && str[i]==' ') |
188 |
|
str[i--] = '\0'; |
189 |
|
if (str[0]) |
190 |
|
debug("\"%s\"", str); |
191 |
|
else |
192 |
|
debug("(no ID)"); |
193 |
|
} |
194 |
|
|
195 |
|
debug(":%s\n", filename); |
196 |
|
|
197 |
|
|
198 |
|
/* |
199 |
|
* Traverse the directory structure to find the kernel. |
200 |
|
*/ |
201 |
|
|
202 |
|
dirlen = buf[0x84] + 256*buf[0x85] + 65536*buf[0x86]; |
203 |
|
if (dirlen != buf[0x8b] + 256*buf[0x8a] + 65536*buf[0x89]) |
204 |
|
fatal("WARNING: Root directory length mismatch?\n"); |
205 |
|
|
206 |
|
dirofs = (int64_t)(buf[0x8c] + (buf[0x8d] << 8) + (buf[0x8e] << 16) + |
207 |
|
(buf[0x8f] << 24)) * 2048; |
208 |
|
|
209 |
|
/* debug("root = %i bytes at 0x%llx\n", dirlen, (long long)dirofs); */ |
210 |
|
|
211 |
|
dirbuf = malloc(dirlen); |
212 |
|
if (dirbuf == NULL) { |
213 |
|
fatal("out of memory in iso_load_bootblock()\n"); |
214 |
|
exit(1); |
215 |
|
} |
216 |
|
|
217 |
|
res2 = diskimage_access(m, disk_id, disk_type, 0, dirofs, dirbuf, |
218 |
|
dirlen); |
219 |
|
if (!res2) { |
220 |
|
fatal("Couldn't read the disk image. Aborting.\n"); |
221 |
|
goto ret; |
222 |
|
} |
223 |
|
|
224 |
|
found_dir = 1; /* Assume root dir */ |
225 |
|
dp = dirbuf; filenr = 1; |
226 |
|
p = NULL; |
227 |
|
while (dp < dirbuf + dirlen) { |
228 |
|
int i, nlen = dp[0]; |
229 |
|
int x = dp[2] + (dp[3] << 8) + (dp[4] << 16) + (dp[5] << 24); |
230 |
|
int y = dp[6] + (dp[7] << 8); |
231 |
|
char direntry[65]; |
232 |
|
|
233 |
|
dp += 8; |
234 |
|
|
235 |
|
/* |
236 |
|
* As long as there is an \ or / in the filename, then we |
237 |
|
* have not yet found the directory. |
238 |
|
*/ |
239 |
|
p = strchr(filename, '/'); |
240 |
|
if (p == NULL) |
241 |
|
p = strchr(filename, '\\'); |
242 |
|
|
243 |
|
/* debug("%i%s: %i, %i, \"", filenr, filenr == found_dir? |
244 |
|
" [CURRENT]" : "", x, y); */ |
245 |
|
for (i=0; i<nlen && i<sizeof(direntry)-1; i++) |
246 |
|
if (dp[i]) { |
247 |
|
direntry[i] = dp[i]; |
248 |
|
/* debug("%c", dp[i]); */ |
249 |
|
} else |
250 |
|
break; |
251 |
|
/* debug("\"\n"); */ |
252 |
|
direntry[i] = '\0'; |
253 |
|
|
254 |
|
/* A directory name match? */ |
255 |
|
if (p != NULL && strncasecmp(filename, direntry, nlen) == 0 |
256 |
|
&& nlen == (size_t)p - (size_t)filename && found_dir == y) { |
257 |
|
found_dir = filenr; |
258 |
|
filename = p+1; |
259 |
|
dirofs = 2048 * (int64_t)x; |
260 |
|
} |
261 |
|
|
262 |
|
dp += nlen; |
263 |
|
|
264 |
|
/* 16-bit aligned lenght: */ |
265 |
|
if (nlen & 1) |
266 |
|
dp ++; |
267 |
|
|
268 |
|
filenr ++; |
269 |
|
} |
270 |
|
|
271 |
|
p = strchr(filename, '/'); |
272 |
|
if (p == NULL) |
273 |
|
p = strchr(filename, '\\'); |
274 |
|
|
275 |
|
if (p != NULL) { |
276 |
|
char *blah = filename_orig; |
277 |
|
|
278 |
|
fatal("could not find '%s' in /", filename); |
279 |
|
|
280 |
|
/* Print the first part of the filename: */ |
281 |
|
while (blah != filename) |
282 |
|
fatal("%c", *blah++); |
283 |
|
|
284 |
|
fatal("\n"); |
285 |
|
goto ret; |
286 |
|
} |
287 |
|
|
288 |
|
/* debug("dirofs = 0x%llx\n", (long long)dirofs); */ |
289 |
|
|
290 |
|
/* Free the old dirbuf, and allocate a new one: */ |
291 |
|
free(dirbuf); |
292 |
|
dirbuf = malloc(512); |
293 |
|
if (dirbuf == NULL) { |
294 |
|
fatal("out of memory in iso_load_bootblock()\n"); |
295 |
|
exit(1); |
296 |
|
} |
297 |
|
|
298 |
|
for (;;) { |
299 |
|
int len, i; |
300 |
|
|
301 |
|
/* Too close to another sector? Then realign. */ |
302 |
|
if ((dirofs & 2047) + 70 > 2047) { |
303 |
|
dirofs = (dirofs | 2047) + 1; |
304 |
|
/* debug("realign dirofs = 0x%llx\n", dirofs); */ |
305 |
|
} |
306 |
|
|
307 |
|
res2 = diskimage_access(m, disk_id, disk_type, 0, dirofs, |
308 |
|
dirbuf, 256); |
309 |
|
if (!res2) { |
310 |
|
fatal("Couldn't read the disk image. Aborting.\n"); |
311 |
|
goto ret; |
312 |
|
} |
313 |
|
|
314 |
|
dp = dirbuf; |
315 |
|
len = dp[0]; |
316 |
|
if (len < 2) |
317 |
|
break; |
318 |
|
|
319 |
|
/* |
320 |
|
* TODO: Actually parse the directory entry! |
321 |
|
* |
322 |
|
* Haha, this must be rewritten. |
323 |
|
*/ |
324 |
|
for (i=32; i<len; i++) { |
325 |
|
if (i < len - strlen(filename)) |
326 |
|
if (strncasecmp(filename, (char *)dp + i, |
327 |
|
strlen(filename)) == 0) { |
328 |
|
/* The filename was found somewhere |
329 |
|
in the directory entry. */ |
330 |
|
if (match_entry != NULL) { |
331 |
|
fatal("TODO: I'm too lazy to" |
332 |
|
" implement a correct " |
333 |
|
"directory parser right " |
334 |
|
"now... (BUG)\n"); |
335 |
|
exit(1); |
336 |
|
} |
337 |
|
match_entry = malloc(512); |
338 |
|
if (match_entry == NULL) { |
339 |
|
fatal("out of memory\n"); |
340 |
|
exit(1); |
341 |
|
} |
342 |
|
memcpy(match_entry, dp, 512); |
343 |
|
break; |
344 |
|
} |
345 |
|
} |
346 |
|
|
347 |
|
dirofs += len; |
348 |
|
} |
349 |
|
|
350 |
|
if (match_entry == NULL) { |
351 |
|
char *blah = filename_orig; |
352 |
|
|
353 |
|
fatal("could not find '%s' in /", filename); |
354 |
|
|
355 |
|
/* Print the first part of the filename: */ |
356 |
|
while (blah != filename) |
357 |
|
fatal("%c", *blah++); |
358 |
|
|
359 |
|
fatal("\n"); |
360 |
|
goto ret; |
361 |
|
} |
362 |
|
|
363 |
|
fileofs = match_entry[2] + (match_entry[3] << 8) + |
364 |
|
(match_entry[4] << 16) + (match_entry[5] << 24); |
365 |
|
filelen = match_entry[10] + (match_entry[11] << 8) + |
366 |
|
(match_entry[12] << 16) + (match_entry[13] << 24); |
367 |
|
fileofs *= 2048; |
368 |
|
|
369 |
|
/* debug("filelen=%llx fileofs=%llx\n", (long long)filelen, |
370 |
|
(long long)fileofs); */ |
371 |
|
|
372 |
|
filebuf = malloc(filelen); |
373 |
|
if (filebuf == NULL) { |
374 |
|
fatal("could not allocate %lli bytes to read the file" |
375 |
|
" from the disk image!\n", (long long)filelen); |
376 |
|
goto ret; |
377 |
|
} |
378 |
|
|
379 |
|
tmpfilename = strdup("/tmp/gxemul.XXXXXXXXXXXX"); |
380 |
|
|
381 |
|
debug("extracting %lli bytes into %s\n", |
382 |
|
(long long)filelen, tmpfilename); |
383 |
|
|
384 |
|
res2 = diskimage_access(m, disk_id, disk_type, 0, fileofs, filebuf, |
385 |
|
filelen); |
386 |
|
if (!res2) { |
387 |
|
fatal("could not read the file from the disk image!\n"); |
388 |
|
goto ret; |
389 |
|
} |
390 |
|
|
391 |
|
tmpfile_handle = mkstemp(tmpfilename); |
392 |
|
if (tmpfile_handle < 0) { |
393 |
|
fatal("could not create %s\n", tmpfilename); |
394 |
|
exit(1); |
395 |
|
} |
396 |
|
write(tmpfile_handle, filebuf, filelen); |
397 |
|
close(tmpfile_handle); |
398 |
|
|
399 |
|
/* Add the temporary filename to the load_namesp array: */ |
400 |
|
(*n_loadp)++; |
401 |
|
new_array = malloc(sizeof(char *) * (*n_loadp)); |
402 |
|
if (new_array == NULL) { |
403 |
|
fatal("out of memory\n"); |
404 |
|
exit(1); |
405 |
|
} |
406 |
|
memcpy(new_array, *load_namesp, sizeof(char *) * (*n_loadp)); |
407 |
|
*load_namesp = new_array; |
408 |
|
|
409 |
|
/* This adds a Backspace char in front of the filename; this |
410 |
|
is a special hack which causes the file to be removed once |
411 |
|
it has been loaded. */ |
412 |
|
tmpfilename = realloc(tmpfilename, strlen(tmpfilename) + 2); |
413 |
|
memmove(tmpfilename + 1, tmpfilename, strlen(tmpfilename) + 1); |
414 |
|
tmpfilename[0] = 8; |
415 |
|
|
416 |
|
(*load_namesp)[*n_loadp - 1] = tmpfilename; |
417 |
|
|
418 |
|
res = 1; |
419 |
|
|
420 |
|
ret: |
421 |
|
if (dirbuf != NULL) |
422 |
|
free(dirbuf); |
423 |
|
|
424 |
|
if (filebuf != NULL) |
425 |
|
free(filebuf); |
426 |
|
|
427 |
|
if (match_entry != NULL) |
428 |
|
free(match_entry); |
429 |
|
|
430 |
|
free(filename_orig); |
431 |
|
|
432 |
|
debug_indentation(-iadd); |
433 |
|
return res; |
434 |
|
} |
435 |
|
|
436 |
|
|
437 |
|
/* |
438 |
* load_bootblock(): |
* load_bootblock(): |
439 |
* |
* |
440 |
* 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 |
441 |
* loading a bootblock from a specific disk offset into memory, and executing |
* loading a bootblock from a specific disk offset into memory, and executing |
442 |
* that, instead of requiring a separate kernel file. It is then up to the |
* that, instead of requiring a separate kernel file. It is then up to the |
443 |
* bootblock to load a kernel. |
* bootblock to load a kernel. |
444 |
|
* |
445 |
|
* Returns 1 on success, 0 on failure. |
446 |
*/ |
*/ |
447 |
static void load_bootblock(struct machine *m, struct cpu *cpu) |
static int load_bootblock(struct machine *m, struct cpu *cpu, |
448 |
|
int *n_loadp, char ***load_namesp) |
449 |
{ |
{ |
450 |
int boot_disk_id; |
int boot_disk_id, boot_disk_type = 0, n_blocks, res, readofs, |
451 |
|
iso_type, retval = 0; |
452 |
unsigned char minibuf[0x20]; |
unsigned char minibuf[0x20]; |
453 |
unsigned char *bootblock_buf; |
unsigned char *bootblock_buf; |
454 |
uint64_t bootblock_offset; |
uint64_t bootblock_offset; |
455 |
uint64_t bootblock_loadaddr, bootblock_pc; |
uint64_t bootblock_loadaddr, bootblock_pc; |
|
int n_blocks, res, readofs; |
|
456 |
|
|
457 |
boot_disk_id = diskimage_bootdev(m); |
boot_disk_id = diskimage_bootdev(m, &boot_disk_type); |
458 |
if (boot_disk_id < 0) |
if (boot_disk_id < 0) |
459 |
return; |
return 0; |
460 |
|
|
461 |
switch (m->machine_type) { |
switch (m->machine_type) { |
462 |
case MACHINE_DEC: |
case MACHINE_DEC: |
476 |
* nr of blocks to read and offset are repeated until nr of |
* nr of blocks to read and offset are repeated until nr of |
477 |
* blocks to read is zero. |
* blocks to read is zero. |
478 |
*/ |
*/ |
479 |
res = diskimage_access(m, boot_disk_id, 0, 0, |
res = diskimage_access(m, boot_disk_id, boot_disk_type, 0, 0, |
480 |
minibuf, sizeof(minibuf)); |
minibuf, sizeof(minibuf)); |
481 |
|
|
482 |
bootblock_loadaddr = minibuf[0x10] + (minibuf[0x11] << 8) |
bootblock_loadaddr = minibuf[0x10] + (minibuf[0x11] << 8) |
503 |
readofs = 0x18; |
readofs = 0x18; |
504 |
|
|
505 |
for (;;) { |
for (;;) { |
506 |
res = diskimage_access(m, boot_disk_id, 0, readofs, |
res = diskimage_access(m, boot_disk_id, boot_disk_type, |
507 |
minibuf, sizeof(minibuf)); |
0, readofs, minibuf, sizeof(minibuf)); |
508 |
if (!res) { |
if (!res) { |
509 |
printf("couldn't read disk?\n"); |
fatal("Couldn't read the disk image. " |
510 |
exit(1); |
"Aborting.\n"); |
511 |
|
return 0; |
512 |
} |
} |
513 |
|
|
514 |
n_blocks = minibuf[0] + (minibuf[1] << 8) |
n_blocks = minibuf[0] + (minibuf[1] << 8) |
533 |
exit(1); |
exit(1); |
534 |
} |
} |
535 |
|
|
536 |
res = diskimage_access(m, boot_disk_id, 0, |
res = diskimage_access(m, boot_disk_id, boot_disk_type, |
537 |
bootblock_offset, bootblock_buf, n_blocks * 512); |
0, bootblock_offset, bootblock_buf, n_blocks * 512); |
538 |
if (!res) { |
if (!res) { |
539 |
fatal("WARNING: could not load bootblocks from" |
fatal("WARNING: could not load bootblocks from" |
540 |
" disk offset 0x%llx\n", |
" disk offset 0x%llx\n", |
550 |
} |
} |
551 |
|
|
552 |
debug(readofs == 0x18? ": no blocks?\n" : " blocks\n"); |
debug(readofs == 0x18? ": no blocks?\n" : " blocks\n"); |
553 |
break; |
return 1; |
554 |
|
|
555 |
case MACHINE_X86: |
case MACHINE_X86: |
556 |
cpu->cd.x86.mode = 16; |
/* TODO: "El Torito" etc? */ |
557 |
cpu->pc = 0x7c00; |
if (diskimage_is_a_cdrom(cpu->machine, boot_disk_id, |
558 |
|
boot_disk_type)) |
559 |
|
break; |
560 |
|
|
561 |
bootblock_buf = malloc(512); |
bootblock_buf = malloc(512); |
562 |
if (bootblock_buf == NULL) { |
if (bootblock_buf == NULL) { |
564 |
exit(1); |
exit(1); |
565 |
} |
} |
566 |
|
|
567 |
res = diskimage_access(m, boot_disk_id, 0, 0, |
debug("loading PC bootsector from %s id %i\n", |
568 |
|
diskimage_types[boot_disk_type], boot_disk_id); |
569 |
|
|
570 |
|
res = diskimage_access(m, boot_disk_id, boot_disk_type, 0, 0, |
571 |
bootblock_buf, 512); |
bootblock_buf, 512); |
572 |
if (!res) { |
if (!res) { |
573 |
printf("Couldn't read the disk image. Aborting.\n"); |
fatal("Couldn't read the disk image. Aborting.\n"); |
574 |
exit(1); |
return 0; |
575 |
} |
} |
576 |
|
|
|
debug("loading PC bootsector from disk %i\n", boot_disk_id); |
|
577 |
if (bootblock_buf[510] != 0x55 || bootblock_buf[511] != 0xaa) |
if (bootblock_buf[510] != 0x55 || bootblock_buf[511] != 0xaa) |
578 |
debug("WARNING! The 0x55,0xAA marker is missing! " |
debug("WARNING! The 0x55,0xAA marker is missing! " |
579 |
"Booting anyway.\n"); |
"Booting anyway.\n"); |
580 |
store_buf(cpu, 0x7c00, (char *)bootblock_buf, 512); |
store_buf(cpu, 0x7c00, (char *)bootblock_buf, 512); |
581 |
free(bootblock_buf); |
free(bootblock_buf); |
|
break; |
|
582 |
|
|
583 |
default: |
return 1; |
584 |
fatal("Booting from disk without a separate kernel " |
} |
585 |
"doesn't work in this emulation mode.\n"); |
|
586 |
|
|
587 |
|
/* |
588 |
|
* Try reading a kernel manually from the disk. The code here |
589 |
|
* does not rely on machine-dependant boot blocks etc. |
590 |
|
*/ |
591 |
|
/* ISO9660: (0x800 bytes at 0x8000) */ |
592 |
|
bootblock_buf = malloc(0x800); |
593 |
|
if (bootblock_buf == NULL) { |
594 |
|
fprintf(stderr, "Out of memory.\n"); |
595 |
exit(1); |
exit(1); |
596 |
} |
} |
597 |
|
|
598 |
|
res = diskimage_access(m, boot_disk_id, boot_disk_type, |
599 |
|
0, 0x8000, bootblock_buf, 0x800); |
600 |
|
if (!res) { |
601 |
|
fatal("Couldn't read the disk image. Aborting.\n"); |
602 |
|
return 0; |
603 |
|
} |
604 |
|
|
605 |
|
iso_type = 0; |
606 |
|
if (strncmp((char *)bootblock_buf+1, "CD001", 5) == 0) |
607 |
|
iso_type = 1; |
608 |
|
if (strncmp((char *)bootblock_buf+1, "CDW01", 5) == 0) |
609 |
|
iso_type = 2; |
610 |
|
if (strncmp((char *)bootblock_buf+1, "CDROM", 5) == 0) |
611 |
|
iso_type = 3; |
612 |
|
|
613 |
|
if (iso_type != 0) { |
614 |
|
/* We can't load a kernel if the name |
615 |
|
isn't specified. */ |
616 |
|
if (cpu->machine->boot_kernel_filename == NULL || |
617 |
|
cpu->machine->boot_kernel_filename[0] == '\0') |
618 |
|
fatal("\nISO9660 filesystem, but no kernel " |
619 |
|
"specified? (Use the -j option.)\n"); |
620 |
|
else |
621 |
|
retval = iso_load_bootblock(m, cpu, boot_disk_id, |
622 |
|
boot_disk_type, iso_type, bootblock_buf, |
623 |
|
n_loadp, load_namesp); |
624 |
|
} |
625 |
|
|
626 |
|
free(bootblock_buf); |
627 |
|
return retval; |
628 |
} |
} |
629 |
|
|
630 |
|
|
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); |
717 |
arcbios_add_memory_descriptor(cpu, |
arcbios_add_memory_descriptor(cpu, |
718 |
start, len, ARCBIOS_MEM_LoadedProgram); |
start, len, ARCBIOS_MEM_LoadedProgram); |
719 |
|
|
720 |
scsicontroller = arcbios_get_scsicontroller(); |
scsicontroller = arcbios_get_scsicontroller(m); |
721 |
if (scsicontroller == 0) |
if (scsicontroller == 0) |
722 |
return; |
return; |
723 |
|
|
765 |
snprintf(component_string, |
snprintf(component_string, |
766 |
sizeof(component_string), |
sizeof(component_string), |
767 |
"scsi(0)cdrom(%i)", d->id); |
"scsi(0)cdrom(%i)", d->id); |
768 |
arcbios_add_string_to_component( |
arcbios_add_string_to_component(m, |
769 |
component_string, scsidevice); |
component_string, scsidevice); |
770 |
|
|
771 |
snprintf(component_string, |
snprintf(component_string, |
772 |
sizeof(component_string), |
sizeof(component_string), |
773 |
"scsi(0)cdrom(%i)fdisk(0)", d->id); |
"scsi(0)cdrom(%i)fdisk(0)", d->id); |
774 |
arcbios_add_string_to_component( |
arcbios_add_string_to_component(m, |
775 |
component_string, scsidisk); |
component_string, scsidisk); |
776 |
} else { |
} else { |
777 |
snprintf(component_string, |
snprintf(component_string, |
778 |
sizeof(component_string), |
sizeof(component_string), |
779 |
"scsi(0)disk(%i)", d->id); |
"scsi(0)disk(%i)", d->id); |
780 |
arcbios_add_string_to_component( |
arcbios_add_string_to_component(m, |
781 |
component_string, scsidevice); |
component_string, scsidevice); |
782 |
|
|
783 |
snprintf(component_string, |
snprintf(component_string, |
784 |
sizeof(component_string), |
sizeof(component_string), |
785 |
"scsi(0)disk(%i)rdisk(0)", d->id); |
"scsi(0)disk(%i)rdisk(0)", d->id); |
786 |
arcbios_add_string_to_component( |
arcbios_add_string_to_component(m, |
787 |
component_string, scsidisk); |
component_string, scsidisk); |
788 |
} |
} |
789 |
} |
} |
809 |
struct emul *emul; |
struct emul *emul; |
810 |
struct cpu *cpu; |
struct cpu *cpu; |
811 |
int i, iadd=4; |
int i, iadd=4; |
812 |
uint64_t addr, memory_amount, entrypoint = 0, gp = 0, toc = 0; |
uint64_t memory_amount, entrypoint = 0, gp = 0, toc = 0; |
813 |
int byte_order; |
int byte_order; |
814 |
|
|
815 |
emul = m->emul; |
emul = m->emul; |
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 |
931 |
x11_init(m); |
x11_init(m); |
932 |
|
|
933 |
/* Fill memory with random bytes: */ |
/* Fill memory with random bytes: */ |
|
/* TODO: This is MIPS-specific! */ |
|
934 |
if (m->random_mem_contents) { |
if (m->random_mem_contents) { |
935 |
for (i=0; i<m->physical_ram_in_mb * 1048576; i+=256) { |
for (i=0; i<m->physical_ram_in_mb * 1048576; i+=256) { |
936 |
unsigned char data[256]; |
unsigned char data[256]; |
937 |
unsigned int j; |
unsigned int j; |
938 |
for (j=0; j<sizeof(data); j++) |
for (j=0; j<sizeof(data); j++) |
939 |
data[j] = random() & 255; |
data[j] = random() & 255; |
940 |
addr = 0xffffffff80000000ULL + i; |
cpu->memory_rw(cpu, m->memory, i, data, sizeof(data), |
941 |
cpu->memory_rw(cpu, m->memory, addr, data, sizeof(data), |
MEM_WRITE, CACHE_NONE | NO_EXCEPTIONS | PHYSICAL); |
|
MEM_WRITE, CACHE_NONE | NO_EXCEPTIONS); |
|
942 |
} |
} |
943 |
} |
} |
944 |
|
|
|
if ((m->machine_type == MACHINE_ARC || |
|
|
m->machine_type == MACHINE_SGI) && m->prom_emulation) |
|
|
arcbios_init(); |
|
|
|
|
945 |
if (m->userland_emul != NULL) { |
if (m->userland_emul != NULL) { |
946 |
/* |
/* |
947 |
* For userland-only emulation, no machine emulation |
* For userland-only emulation, no machine emulation |
958 |
|
|
959 |
/* Load files (ROM code, boot code, ...) into memory: */ |
/* Load files (ROM code, boot code, ...) into memory: */ |
960 |
if (n_load == 0) { |
if (n_load == 0) { |
961 |
if (m->first_diskimage != NULL) |
if (m->first_diskimage != NULL) { |
962 |
load_bootblock(m, cpu); |
if (!load_bootblock(m, cpu, &n_load, &load_names)) { |
963 |
else { |
fprintf(stderr, "\nNo executable files were" |
964 |
|
" specified, and booting directly from disk" |
965 |
|
" failed.\n"); |
966 |
|
exit(1); |
967 |
|
} |
968 |
|
} else { |
969 |
fprintf(stderr, "No executable file(s) loaded, and " |
fprintf(stderr, "No executable file(s) loaded, and " |
970 |
"we are not booting directly from a disk image." |
"we are not booting directly from a disk image." |
971 |
"\nAborting.\n"); |
"\nAborting.\n"); |
974 |
} |
} |
975 |
|
|
976 |
while (n_load > 0) { |
while (n_load > 0) { |
977 |
|
FILE *tmp_f; |
978 |
|
char *name_to_load = *load_names; |
979 |
|
int remove_after_load = 0; |
980 |
|
|
981 |
|
/* Special hack for removing temporary files: */ |
982 |
|
if (name_to_load[0] == 8) { |
983 |
|
name_to_load ++; |
984 |
|
remove_after_load = 1; |
985 |
|
} |
986 |
|
|
987 |
|
/* |
988 |
|
* gzipped files are automagically gunzipped: |
989 |
|
* NOTE/TODO: This isn't secure. system() is used. |
990 |
|
*/ |
991 |
|
tmp_f = fopen(name_to_load, "r"); |
992 |
|
if (tmp_f != NULL) { |
993 |
|
unsigned char buf[2]; /* gzip header */ |
994 |
|
memset(buf, 0, sizeof(buf)); |
995 |
|
fread(buf, 1, sizeof(buf), tmp_f); |
996 |
|
if (buf[0]==0x1f && buf[1]==0x8b) { |
997 |
|
size_t zzlen = strlen(name_to_load)*2 + 100; |
998 |
|
char *zz = malloc(zzlen); |
999 |
|
debug("gunziping %s\n", name_to_load); |
1000 |
|
/* |
1001 |
|
* gzip header found. If this was a file |
1002 |
|
* extracted from, say, a CDROM image, then it |
1003 |
|
* already has a temporary name. Otherwise we |
1004 |
|
* 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); |
1027 |
|
} |
1028 |
|
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: */ |
1074 |
|
switch (m->arch) { |
1075 |
|
case ARCH_X86: |
1076 |
|
/* |
1077 |
|
* X86 machines normally don't need to load any files, |
1078 |
|
* they can boot from disk directly. Therefore, an x86 |
1079 |
|
* machine usually boots up in 16-bit real mode. When |
1080 |
|
* loading a 32-bit (or even 64-bit) ELF, that's not |
1081 |
|
* very nice, hence this special case. |
1082 |
|
*/ |
1083 |
|
pc_bios_simple_pmode_setup(cpu); |
1084 |
|
break; |
1085 |
|
} |
1086 |
|
|
1087 |
byte_order = NO_BYTE_ORDER_OVERRIDE; |
byte_order = NO_BYTE_ORDER_OVERRIDE; |
1088 |
|
|
1089 |
file_load(m, m->memory, *load_names, &entrypoint, |
/* |
1090 |
|
* Load the file: :-) |
1091 |
|
*/ |
1092 |
|
file_load(m, m->memory, name_to_load, &entrypoint, |
1093 |
m->arch, &gp, &byte_order, &toc); |
m->arch, &gp, &byte_order, &toc); |
1094 |
|
|
1095 |
|
if (remove_after_load) { |
1096 |
|
debug("removing %s\n", name_to_load); |
1097 |
|
unlink(name_to_load); |
1098 |
|
} |
1099 |
|
|
1100 |
if (byte_order != NO_BYTE_ORDER_OVERRIDE) |
if (byte_order != NO_BYTE_ORDER_OVERRIDE) |
1101 |
cpu->byte_order = byte_order; |
cpu->byte_order = byte_order; |
1102 |
|
|
1117 |
break; |
break; |
1118 |
|
|
1119 |
case ARCH_PPC: |
case ARCH_PPC: |
1120 |
|
/* See http://www.linuxbase.org/spec/ELF/ppc64/ |
1121 |
|
spec/x458.html for more info. */ |
1122 |
cpu->cd.ppc.gpr[2] = toc; |
cpu->cd.ppc.gpr[2] = toc; |
1123 |
|
/* TODO */ |
1124 |
break; |
break; |
1125 |
|
|
1126 |
case ARCH_ALPHA: |
case ARCH_ALPHA: |
1129 |
case ARCH_URISC: |
case ARCH_URISC: |
1130 |
break; |
break; |
1131 |
|
|
1132 |
|
case ARCH_ARM: |
1133 |
|
cpu->pc &= 0xfffffffc; |
1134 |
|
cpu->cd.arm.r[ARM_PC] = cpu->pc; |
1135 |
|
break; |
1136 |
|
|
1137 |
case ARCH_X86: |
case ARCH_X86: |
1138 |
/* |
/* |
1139 |
* NOTE: The toc field is used to indicate an ELF64 |
* NOTE: The toc field is used to indicate an ELF32 |
1140 |
* load, on AMD64! |
* or ELF64 load. |
1141 |
*/ |
*/ |
1142 |
if (toc != 0) { |
switch (toc) { |
1143 |
cpu->cd.x86.mode = 64; |
case 0: /* 16-bit? TODO */ |
|
} else |
|
1144 |
cpu->pc &= 0xffffffffULL; |
cpu->pc &= 0xffffffffULL; |
1145 |
|
break; |
1146 |
|
case 1: /* 32-bit. */ |
1147 |
|
cpu->pc &= 0xffffffffULL; |
1148 |
|
break; |
1149 |
|
case 2: /* 64-bit: TODO */ |
1150 |
|
fatal("64-bit x86 load. TODO\n"); |
1151 |
|
exit(1); |
1152 |
|
} |
1153 |
break; |
break; |
1154 |
|
|
1155 |
default: |
default: |
1241 |
else |
else |
1242 |
debug("0x%016llx", (long long)entrypoint); |
debug("0x%016llx", (long long)entrypoint); |
1243 |
break; |
break; |
1244 |
|
case ARCH_ARM: |
1245 |
|
/* ARM cpus aren't 64-bit: */ |
1246 |
|
debug("0x%08x", (int)entrypoint); |
1247 |
|
break; |
1248 |
case ARCH_URISC: |
case ARCH_URISC: |
1249 |
{ |
{ |
1250 |
char tmps[100]; |
char tmps[100]; |
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 |
} |
} |
1271 |
break; |
break; |
1272 |
case ARCH_X86: |
case ARCH_X86: |
1273 |
if (cpu->cd.x86.mode == 16) |
debug("0x%04x:0x%llx", cpu->cd.x86.s[X86_S_CS], |
1274 |
debug("0x%04x:0x%04x", cpu->cd.x86.s[X86_S_CS], |
(long long)cpu->pc); |
|
(int)cpu->pc); |
|
|
else if (cpu->cd.x86.mode == 32) |
|
|
debug("0x%08x", (int)cpu->pc); |
|
|
else |
|
|
debug("0x%016llx", (long long)cpu->pc); |
|
1275 |
break; |
break; |
1276 |
default: |
default: |
1277 |
debug("0x%016llx", (long long)cpu->pc); |
debug("0x%016llx", (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"); |