1 |
/* |
/* |
2 |
* Copyright (C) 2003-2005 Anders Gavare. All rights reserved. |
* Copyright (C) 2003-2006 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.211 2005/06/26 11:36:28 debug Exp $ |
* $Id: emul.c,v 1.260 2006/07/26 23:21:47 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" |
55 |
#include "x11.h" |
#include "x11.h" |
56 |
|
|
57 |
|
|
|
extern int force_debugger_at_exit; |
|
|
|
|
58 |
extern int extra_argc; |
extern int extra_argc; |
59 |
extern char **extra_argv; |
extern char **extra_argv; |
60 |
|
|
61 |
extern int verbose; |
extern int verbose; |
62 |
extern int quiet_mode; |
extern int quiet_mode; |
63 |
|
extern int force_debugger_at_exit; |
64 |
|
extern int single_step; |
65 |
|
extern int old_show_trace_tree; |
66 |
|
extern int old_instruction_trace; |
67 |
|
extern int old_quiet_mode; |
68 |
|
extern int quiet_mode; |
69 |
|
|
70 |
extern struct emul *debugger_emul; |
extern struct emul *debugger_emul; |
71 |
extern struct diskimage *diskimages[]; |
extern struct diskimage *diskimages[]; |
73 |
static char *diskimage_types[] = DISKIMAGE_TYPES; |
static char *diskimage_types[] = DISKIMAGE_TYPES; |
74 |
|
|
75 |
|
|
76 |
|
static void print_separator(void) |
77 |
|
{ |
78 |
|
int i = 79; |
79 |
|
while (i-- > 0) |
80 |
|
debug("-"); |
81 |
|
debug("\n"); |
82 |
|
} |
83 |
|
|
84 |
|
|
85 |
/* |
/* |
86 |
* add_dump_points(): |
* add_dump_points(): |
87 |
* |
* |
108 |
uint64_t addr; |
uint64_t addr; |
109 |
int res = get_symbol_addr(&m->symbol_context, |
int res = get_symbol_addr(&m->symbol_context, |
110 |
m->breakpoint_string[i], &addr); |
m->breakpoint_string[i], &addr); |
111 |
if (!res) |
if (!res) { |
112 |
fprintf(stderr, |
fprintf(stderr, |
113 |
"WARNING! Breakpoint '%s' could not be" |
"ERROR! Breakpoint '%s' could not be" |
114 |
" parsed\n", |
" parsed\n", |
115 |
m->breakpoint_string[i]); |
m->breakpoint_string[i]); |
116 |
else { |
} else { |
117 |
dp = addr; |
dp = addr; |
118 |
string_flag = 1; |
string_flag = 1; |
119 |
} |
} |
124 |
* were automatically converted into the correct address. |
* were automatically converted into the correct address. |
125 |
*/ |
*/ |
126 |
|
|
127 |
if ((dp >> 32) == 0 && ((dp >> 31) & 1)) |
if (m->arch == ARCH_MIPS) { |
128 |
dp |= 0xffffffff00000000ULL; |
if ((dp >> 32) == 0 && ((dp >> 31) & 1)) |
129 |
|
dp |= 0xffffffff00000000ULL; |
130 |
|
} |
131 |
|
|
132 |
m->breakpoint_addr[i] = dp; |
m->breakpoint_addr[i] = dp; |
133 |
|
|
134 |
debug("breakpoint %i: 0x%016llx", i, (long long)dp); |
debug("breakpoint %i: 0x%llx", i, (long long)dp); |
135 |
if (string_flag) |
if (string_flag) |
136 |
debug(" (%s)", m->breakpoint_string[i]); |
debug(" (%s)", m->breakpoint_string[i]); |
137 |
debug("\n"); |
debug("\n"); |
164 |
int *n_loadp, char ***load_namesp) |
int *n_loadp, char ***load_namesp) |
165 |
{ |
{ |
166 |
char str[35]; |
char str[35]; |
167 |
int filenr, i, ofs, dirlen, res = 0, res2, iadd = 4; |
int filenr, i, ofs, dirlen, res = 0, res2, iadd = DEBUG_INDENTATION; |
168 |
int found_dir; |
int found_dir; |
169 |
uint64_t dirofs; |
uint64_t dirofs; |
170 |
uint64_t fileofs, filelen; |
uint64_t fileofs, filelen; |
173 |
char *p, *filename_orig; |
char *p, *filename_orig; |
174 |
char *filename = strdup(cpu->machine->boot_kernel_filename); |
char *filename = strdup(cpu->machine->boot_kernel_filename); |
175 |
unsigned char *filebuf = NULL; |
unsigned char *filebuf = NULL; |
176 |
char *tmpfilename = NULL; |
char *tmpfname = NULL; |
177 |
char **new_array; |
char **new_array; |
178 |
int tmpfile_handle; |
int tmpfile_handle; |
179 |
|
|
219 |
fatal("WARNING: Root directory length mismatch?\n"); |
fatal("WARNING: Root directory length mismatch?\n"); |
220 |
|
|
221 |
dirofs = (int64_t)(buf[0x8c] + (buf[0x8d] << 8) + (buf[0x8e] << 16) + |
dirofs = (int64_t)(buf[0x8c] + (buf[0x8d] << 8) + (buf[0x8e] << 16) + |
222 |
(buf[0x8f] << 24)) * 2048; |
((uint64_t)buf[0x8f] << 24)) * 2048; |
223 |
|
|
224 |
/* debug("root = %i bytes at 0x%llx\n", dirlen, (long long)dirofs); */ |
/* debug("root = %i bytes at 0x%llx\n", dirlen, (long long)dirofs); */ |
225 |
|
|
240 |
dp = dirbuf; filenr = 1; |
dp = dirbuf; filenr = 1; |
241 |
p = NULL; |
p = NULL; |
242 |
while (dp < dirbuf + dirlen) { |
while (dp < dirbuf + dirlen) { |
243 |
int i, nlen = dp[0]; |
size_t i, nlen = dp[0]; |
244 |
int x = dp[2] + (dp[3] << 8) + (dp[4] << 16) + (dp[5] << 24); |
int x = dp[2] + (dp[3] << 8) + (dp[4] << 16) + |
245 |
|
((uint64_t)dp[5] << 24); |
246 |
int y = dp[6] + (dp[7] << 8); |
int y = dp[6] + (dp[7] << 8); |
247 |
char direntry[65]; |
char direntry[65]; |
248 |
|
|
312 |
} |
} |
313 |
|
|
314 |
for (;;) { |
for (;;) { |
315 |
int len, i; |
size_t len, i; |
316 |
|
|
317 |
/* Too close to another sector? Then realign. */ |
/* Too close to another sector? Then realign. */ |
318 |
if ((dirofs & 2047) + 70 > 2047) { |
if ((dirofs & 2047) + 70 > 2047) { |
377 |
} |
} |
378 |
|
|
379 |
fileofs = match_entry[2] + (match_entry[3] << 8) + |
fileofs = match_entry[2] + (match_entry[3] << 8) + |
380 |
(match_entry[4] << 16) + (match_entry[5] << 24); |
(match_entry[4] << 16) + ((uint64_t)match_entry[5] << 24); |
381 |
filelen = match_entry[10] + (match_entry[11] << 8) + |
filelen = match_entry[10] + (match_entry[11] << 8) + |
382 |
(match_entry[12] << 16) + (match_entry[13] << 24); |
(match_entry[12] << 16) + ((uint64_t)match_entry[13] << 24); |
383 |
fileofs *= 2048; |
fileofs *= 2048; |
384 |
|
|
385 |
/* debug("filelen=%llx fileofs=%llx\n", (long long)filelen, |
/* debug("filelen=%llx fileofs=%llx\n", (long long)filelen, |
392 |
goto ret; |
goto ret; |
393 |
} |
} |
394 |
|
|
395 |
tmpfilename = strdup("/tmp/gxemul.XXXXXXXXXXXX"); |
tmpfname = strdup("/tmp/gxemul.XXXXXXXXXXXX"); |
|
|
|
|
debug("extracting %lli bytes into %s\n", |
|
|
(long long)filelen, tmpfilename); |
|
396 |
|
|
397 |
res2 = diskimage_access(m, disk_id, disk_type, 0, fileofs, filebuf, |
res2 = diskimage_access(m, disk_id, disk_type, 0, fileofs, filebuf, |
398 |
filelen); |
filelen); |
401 |
goto ret; |
goto ret; |
402 |
} |
} |
403 |
|
|
404 |
tmpfile_handle = mkstemp(tmpfilename); |
tmpfile_handle = mkstemp(tmpfname); |
405 |
if (tmpfile_handle < 0) { |
if (tmpfile_handle < 0) { |
406 |
fatal("could not create %s\n", tmpfilename); |
fatal("could not create %s\n", tmpfname); |
407 |
exit(1); |
exit(1); |
408 |
} |
} |
409 |
write(tmpfile_handle, filebuf, filelen); |
write(tmpfile_handle, filebuf, filelen); |
410 |
close(tmpfile_handle); |
close(tmpfile_handle); |
411 |
|
|
412 |
|
debug("extracted %lli bytes into %s\n", (long long)filelen, tmpfname); |
413 |
|
|
414 |
/* Add the temporary filename to the load_namesp array: */ |
/* Add the temporary filename to the load_namesp array: */ |
415 |
(*n_loadp)++; |
(*n_loadp)++; |
416 |
new_array = malloc(sizeof(char *) * (*n_loadp)); |
new_array = malloc(sizeof(char *) * (*n_loadp)); |
424 |
/* This adds a Backspace char in front of the filename; this |
/* This adds a Backspace char in front of the filename; this |
425 |
is a special hack which causes the file to be removed once |
is a special hack which causes the file to be removed once |
426 |
it has been loaded. */ |
it has been loaded. */ |
427 |
tmpfilename = realloc(tmpfilename, strlen(tmpfilename) + 2); |
tmpfname = realloc(tmpfname, strlen(tmpfname) + 2); |
428 |
memmove(tmpfilename + 1, tmpfilename, strlen(tmpfilename) + 1); |
memmove(tmpfname + 1, tmpfname, strlen(tmpfname) + 1); |
429 |
tmpfilename[0] = 8; |
tmpfname[0] = 8; |
430 |
|
|
431 |
(*load_namesp)[*n_loadp - 1] = tmpfilename; |
(*load_namesp)[*n_loadp - 1] = tmpfname; |
432 |
|
|
433 |
res = 1; |
res = 1; |
434 |
|
|
450 |
|
|
451 |
|
|
452 |
/* |
/* |
453 |
|
* apple_load_bootblock(): |
454 |
|
* |
455 |
|
* Try to load a kernel from a disk image with an Apple Partition Table. |
456 |
|
* |
457 |
|
* TODO: This function uses too many magic offsets and so on; it should be |
458 |
|
* cleaned up some day. See http://www.awprofessional.com/articles/ |
459 |
|
* article.asp?p=376123&seqNum=3&rl=1 for some info on the Apple |
460 |
|
* partition format. |
461 |
|
* |
462 |
|
* Returns 1 on success, 0 on failure. |
463 |
|
*/ |
464 |
|
static int apple_load_bootblock(struct machine *m, struct cpu *cpu, |
465 |
|
int disk_id, int disk_type, int *n_loadp, char ***load_namesp) |
466 |
|
{ |
467 |
|
unsigned char buf[0x8000]; |
468 |
|
int res, partnr, n_partitions = 0, n_hfs_partitions = 0; |
469 |
|
uint64_t hfs_start, hfs_length; |
470 |
|
|
471 |
|
res = diskimage_access(m, disk_id, disk_type, 0, 0x0, buf, sizeof(buf)); |
472 |
|
if (!res) { |
473 |
|
fatal("apple_load_bootblock: couldn't read the disk " |
474 |
|
"image. Aborting.\n"); |
475 |
|
return 0; |
476 |
|
} |
477 |
|
|
478 |
|
partnr = 0; |
479 |
|
do { |
480 |
|
int start, length; |
481 |
|
int ofs = 0x200 * (partnr + 1); |
482 |
|
if (partnr == 0) |
483 |
|
n_partitions = buf[ofs + 7]; |
484 |
|
start = ((uint64_t)buf[ofs + 8] << 24) + (buf[ofs + 9] << 16) + |
485 |
|
(buf[ofs + 10] << 8) + buf[ofs + 11]; |
486 |
|
length = ((uint64_t)buf[ofs+12] << 24) + (buf[ofs + 13] << 16) + |
487 |
|
(buf[ofs + 14] << 8) + buf[ofs + 15]; |
488 |
|
|
489 |
|
debug("partition %i: '%s', type '%s', start %i, length %i\n", |
490 |
|
partnr, buf + ofs + 0x10, buf + ofs + 0x30, |
491 |
|
start, length); |
492 |
|
|
493 |
|
if (strcmp((char *)buf + ofs + 0x30, "Apple_HFS") == 0) { |
494 |
|
n_hfs_partitions ++; |
495 |
|
hfs_start = 512 * start; |
496 |
|
hfs_length = 512 * length; |
497 |
|
} |
498 |
|
|
499 |
|
/* Any more partitions? */ |
500 |
|
partnr ++; |
501 |
|
} while (partnr < n_partitions); |
502 |
|
|
503 |
|
if (n_hfs_partitions == 0) { |
504 |
|
fatal("Error: No HFS partition found! TODO\n"); |
505 |
|
return 0; |
506 |
|
} |
507 |
|
if (n_hfs_partitions >= 2) { |
508 |
|
fatal("Error: Too many HFS partitions found! TODO\n"); |
509 |
|
return 0; |
510 |
|
} |
511 |
|
|
512 |
|
return 0; |
513 |
|
} |
514 |
|
|
515 |
|
|
516 |
|
/* |
517 |
* load_bootblock(): |
* load_bootblock(): |
518 |
* |
* |
519 |
* 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 |
538 |
return 0; |
return 0; |
539 |
|
|
540 |
switch (m->machine_type) { |
switch (m->machine_type) { |
541 |
case MACHINE_DEC: |
case MACHINE_PMAX: |
542 |
/* |
/* |
543 |
* The first few bytes of a disk contains information about |
* The first few bytes of a disk contains information about |
544 |
* where the bootblock(s) are located. (These are all 32-bit |
* where the bootblock(s) are located. (These are all 32-bit |
559 |
minibuf, sizeof(minibuf)); |
minibuf, sizeof(minibuf)); |
560 |
|
|
561 |
bootblock_loadaddr = minibuf[0x10] + (minibuf[0x11] << 8) |
bootblock_loadaddr = minibuf[0x10] + (minibuf[0x11] << 8) |
562 |
+ (minibuf[0x12] << 16) + (minibuf[0x13] << 24); |
+ (minibuf[0x12] << 16) + ((uint64_t)minibuf[0x13] << 24); |
563 |
|
|
564 |
/* Convert loadaddr to uncached: */ |
/* Convert loadaddr to uncached: */ |
565 |
if ((bootblock_loadaddr & 0xf0000000ULL) != 0x80000000 && |
if ((bootblock_loadaddr & 0xf0000000ULL) != 0x80000000 && |
570 |
bootblock_loadaddr |= 0xffffffffa0000000ULL; |
bootblock_loadaddr |= 0xffffffffa0000000ULL; |
571 |
|
|
572 |
bootblock_pc = minibuf[0x14] + (minibuf[0x15] << 8) |
bootblock_pc = minibuf[0x14] + (minibuf[0x15] << 8) |
573 |
+ (minibuf[0x16] << 16) + (minibuf[0x17] << 24); |
+ (minibuf[0x16] << 16) + ((uint64_t)minibuf[0x17] << 24); |
574 |
|
|
575 |
bootblock_pc &= 0x0fffffffULL; |
bootblock_pc &= 0x0fffffffULL; |
576 |
bootblock_pc |= 0xffffffffa0000000ULL; |
bootblock_pc |= 0xffffffffa0000000ULL; |
591 |
} |
} |
592 |
|
|
593 |
n_blocks = minibuf[0] + (minibuf[1] << 8) |
n_blocks = minibuf[0] + (minibuf[1] << 8) |
594 |
+ (minibuf[2] << 16) + (minibuf[3] << 24); |
+ (minibuf[2] << 16) + ((uint64_t)minibuf[3] << 24); |
595 |
|
|
596 |
bootblock_offset = (minibuf[4] + (minibuf[5] << 8) |
bootblock_offset = (minibuf[4] + (minibuf[5] << 8) + |
597 |
+ (minibuf[6] << 16) + (minibuf[7] << 24)) * 512; |
(minibuf[6]<<16) + ((uint64_t)minibuf[7]<<24)) * 512; |
598 |
|
|
599 |
if (n_blocks < 1) |
if (n_blocks < 1) |
600 |
break; |
break; |
665 |
|
|
666 |
/* |
/* |
667 |
* Try reading a kernel manually from the disk. The code here |
* Try reading a kernel manually from the disk. The code here |
668 |
* does not rely on machine-dependant boot blocks etc. |
* does not rely on machine-dependent boot blocks etc. |
669 |
*/ |
*/ |
670 |
/* ISO9660: (0x800 bytes at 0x8000) */ |
/* ISO9660: (0x800 bytes at 0x8000) */ |
671 |
bootblock_buf = malloc(0x800); |
bootblock_buf = malloc(0x800); |
702 |
n_loadp, load_namesp); |
n_loadp, load_namesp); |
703 |
} |
} |
704 |
|
|
705 |
|
if (retval != 0) |
706 |
|
goto ret_ok; |
707 |
|
|
708 |
|
/* Apple parition table: */ |
709 |
|
res = diskimage_access(m, boot_disk_id, boot_disk_type, |
710 |
|
0, 0x0, bootblock_buf, 0x800); |
711 |
|
if (!res) { |
712 |
|
fatal("Couldn't read the disk image. Aborting.\n"); |
713 |
|
return 0; |
714 |
|
} |
715 |
|
if (bootblock_buf[0x000] == 'E' && bootblock_buf[0x001] == 'R' && |
716 |
|
bootblock_buf[0x200] == 'P' && bootblock_buf[0x201] == 'M') { |
717 |
|
/* We can't load a kernel if the name |
718 |
|
isn't specified. */ |
719 |
|
if (cpu->machine->boot_kernel_filename == NULL || |
720 |
|
cpu->machine->boot_kernel_filename[0] == '\0') |
721 |
|
fatal("\nApple partition table, but no kernel " |
722 |
|
"specified? (Use the -j option.)\n"); |
723 |
|
else |
724 |
|
retval = apple_load_bootblock(m, cpu, boot_disk_id, |
725 |
|
boot_disk_type, n_loadp, load_namesp); |
726 |
|
} |
727 |
|
|
728 |
|
ret_ok: |
729 |
free(bootblock_buf); |
free(bootblock_buf); |
730 |
return retval; |
return retval; |
731 |
} |
} |
812 |
|
|
813 |
len += 1048576 * m->memory_offset_in_mb; |
len += 1048576 * m->memory_offset_in_mb; |
814 |
|
|
815 |
/* NOTE/TODO: magic 12MB end of load program area */ |
/* |
816 |
|
* NOTE/TODO: magic 12MB end of load program area |
817 |
|
* |
818 |
|
* Hm. This breaks the old FreeBSD/MIPS snapshots... |
819 |
|
*/ |
820 |
|
#if 0 |
821 |
arcbios_add_memory_descriptor(cpu, |
arcbios_add_memory_descriptor(cpu, |
822 |
0x60000 + m->memory_offset_in_mb * 1048576, |
0x60000 + m->memory_offset_in_mb * 1048576, |
823 |
start-0x60000 - m->memory_offset_in_mb * 1048576, |
start-0x60000 - m->memory_offset_in_mb * 1048576, |
824 |
ARCBIOS_MEM_FreeMemory); |
ARCBIOS_MEM_FreeMemory); |
825 |
|
#endif |
826 |
arcbios_add_memory_descriptor(cpu, |
arcbios_add_memory_descriptor(cpu, |
827 |
start, len, ARCBIOS_MEM_LoadedProgram); |
start, len, ARCBIOS_MEM_LoadedProgram); |
828 |
|
|
915 |
void emul_machine_setup(struct machine *m, int n_load, char **load_names, |
void emul_machine_setup(struct machine *m, int n_load, char **load_names, |
916 |
int n_devices, char **device_names) |
int n_devices, char **device_names) |
917 |
{ |
{ |
|
struct emul *emul; |
|
918 |
struct cpu *cpu; |
struct cpu *cpu; |
919 |
int i, iadd=4; |
int i, iadd = DEBUG_INDENTATION; |
920 |
uint64_t memory_amount, entrypoint = 0, gp = 0, toc = 0; |
uint64_t memory_amount, entrypoint = 0, gp = 0, toc = 0; |
921 |
int byte_order; |
int byte_order; |
922 |
|
|
|
emul = m->emul; |
|
|
|
|
923 |
debug("machine \"%s\":\n", m->name); |
debug("machine \"%s\":\n", m->name); |
924 |
debug_indentation(iadd); |
debug_indentation(iadd); |
925 |
|
|
940 |
|
|
941 |
m->cpu_family = cpu_family_ptr_by_number(m->arch); |
m->cpu_family = cpu_family_ptr_by_number(m->arch); |
942 |
|
|
943 |
if (m->arch != ARCH_MIPS) |
if (m->arch == ARCH_ALPHA) |
944 |
m->bintrans_enable = 0; |
m->arch_pagesize = 8192; |
945 |
|
|
946 |
machine_memsize_fix(m); |
machine_memsize_fix(m); |
947 |
|
|
963 |
debug(" (offset by %iMB)", m->memory_offset_in_mb); |
debug(" (offset by %iMB)", m->memory_offset_in_mb); |
964 |
memory_amount += 1048576 * m->memory_offset_in_mb; |
memory_amount += 1048576 * m->memory_offset_in_mb; |
965 |
} |
} |
966 |
m->memory = memory_new(memory_amount); |
m->memory = memory_new(memory_amount, m->arch); |
967 |
if (m->machine_type != MACHINE_USERLAND) |
if (m->machine_type != MACHINE_USERLAND) |
968 |
debug("\n"); |
debug("\n"); |
969 |
|
|
990 |
} |
} |
991 |
memset(m->cpus, 0, sizeof(struct cpu *) * m->ncpus); |
memset(m->cpus, 0, sizeof(struct cpu *) * m->ncpus); |
992 |
|
|
|
/* Initialize dynamic binary translation, if available: */ |
|
|
if (m->bintrans_enable) |
|
|
bintrans_init(m, m->memory); |
|
|
|
|
993 |
debug("cpu0"); |
debug("cpu0"); |
994 |
if (m->ncpus > 1) |
if (m->ncpus > 1) |
995 |
debug(" .. cpu%i", m->ncpus - 1); |
debug(" .. cpu%i", m->ncpus - 1); |
996 |
debug(": "); |
debug(": "); |
997 |
for (i=0; i<m->ncpus; i++) { |
for (i=0; i<m->ncpus; i++) { |
998 |
m->cpus[i] = cpu_new(m->memory, m, i, m->cpu_name); |
m->cpus[i] = cpu_new(m->memory, m, i, m->cpu_name); |
999 |
if (m->bintrans_enable) |
if (m->cpus[i] == NULL) { |
1000 |
bintrans_init_cpu(m->cpus[i]); |
fprintf(stderr, "Unable to create CPU object. " |
1001 |
|
"Aborting."); |
1002 |
|
exit(1); |
1003 |
|
} |
1004 |
} |
} |
1005 |
debug("\n"); |
debug("\n"); |
1006 |
|
|
1011 |
m->cpus[m->ncpus] = cpu_new(m->memory, m, |
m->cpus[m->ncpus] = cpu_new(m->memory, m, |
1012 |
0 /* use 0 here to show info with debug() */, |
0 /* use 0 here to show info with debug() */, |
1013 |
"Allegrex" /* TODO */); |
"Allegrex" /* TODO */); |
|
if (m->bintrans_enable) |
|
|
bintrans_init_cpu(m->cpus[m->ncpus]); |
|
1014 |
debug("\n"); |
debug("\n"); |
1015 |
m->ncpus ++; |
m->ncpus ++; |
1016 |
} |
} |
1027 |
if (m->userland_emul != NULL) { |
if (m->userland_emul != NULL) { |
1028 |
useremul_name_to_useremul(cpu, |
useremul_name_to_useremul(cpu, |
1029 |
m->userland_emul, NULL, NULL, NULL); |
m->userland_emul, NULL, NULL, NULL); |
1030 |
cpu->memory_rw = userland_memory_rw; |
|
1031 |
|
switch (m->arch) { |
1032 |
|
#ifdef ENABLE_ALPHA |
1033 |
|
case ARCH_ALPHA: |
1034 |
|
cpu->memory_rw = alpha_userland_memory_rw; |
1035 |
|
break; |
1036 |
|
#endif |
1037 |
|
default:cpu->memory_rw = userland_memory_rw; |
1038 |
|
} |
1039 |
} |
} |
1040 |
|
|
1041 |
if (m->use_x11) |
if (m->use_x11) |
1066 |
} |
} |
1067 |
|
|
1068 |
diskimage_dump_info(m); |
diskimage_dump_info(m); |
1069 |
|
console_debug_dump(m); |
1070 |
|
|
1071 |
/* Load files (ROM code, boot code, ...) into memory: */ |
/* Load files (ROM code, boot code, ...) into memory: */ |
1072 |
if (n_load == 0) { |
if (n_load == 0) { |
1215 |
cpu->pc = entrypoint; |
cpu->pc = entrypoint; |
1216 |
|
|
1217 |
switch (m->arch) { |
switch (m->arch) { |
1218 |
|
|
1219 |
|
case ARCH_ALPHA: |
1220 |
|
/* For position-independent code: */ |
1221 |
|
cpu->cd.alpha.r[ALPHA_T12] = cpu->pc; |
1222 |
|
break; |
1223 |
|
|
1224 |
|
case ARCH_ARM: |
1225 |
|
if (cpu->pc & 3) { |
1226 |
|
fatal("ARM: lowest bits of pc set: TODO\n"); |
1227 |
|
exit(1); |
1228 |
|
} |
1229 |
|
cpu->pc &= 0xfffffffc; |
1230 |
|
break; |
1231 |
|
|
1232 |
|
case ARCH_AVR: |
1233 |
|
cpu->pc &= 0xfffff; |
1234 |
|
if (cpu->pc & 1) { |
1235 |
|
fatal("AVR: lowest bit of pc set: TODO\n"); |
1236 |
|
exit(1); |
1237 |
|
} |
1238 |
|
break; |
1239 |
|
|
1240 |
|
case ARCH_HPPA: |
1241 |
|
break; |
1242 |
|
|
1243 |
|
case ARCH_I960: |
1244 |
|
break; |
1245 |
|
|
1246 |
|
case ARCH_IA64: |
1247 |
|
break; |
1248 |
|
|
1249 |
|
case ARCH_M68K: |
1250 |
|
break; |
1251 |
|
|
1252 |
case ARCH_MIPS: |
case ARCH_MIPS: |
1253 |
if ((cpu->pc >> 32) == 0 |
if ((cpu->pc >> 32) == 0 && (cpu->pc & 0x80000000ULL)) |
|
&& (cpu->pc & 0x80000000ULL)) |
|
1254 |
cpu->pc |= 0xffffffff00000000ULL; |
cpu->pc |= 0xffffffff00000000ULL; |
1255 |
|
|
1256 |
cpu->cd.mips.gpr[MIPS_GPR_GP] = gp; |
cpu->cd.mips.gpr[MIPS_GPR_GP] = gp; |
1266 |
spec/x458.html for more info. */ |
spec/x458.html for more info. */ |
1267 |
cpu->cd.ppc.gpr[2] = toc; |
cpu->cd.ppc.gpr[2] = toc; |
1268 |
/* TODO */ |
/* TODO */ |
1269 |
|
if (cpu->cd.ppc.bits == 32) |
1270 |
|
cpu->pc &= 0xffffffffULL; |
1271 |
|
break; |
1272 |
|
|
1273 |
|
case ARCH_SH: |
1274 |
|
if (cpu->cd.sh.cpu_type.bits == 32) |
1275 |
|
cpu->pc &= 0xffffffffULL; |
1276 |
|
cpu->pc &= ~1; |
1277 |
break; |
break; |
1278 |
|
|
|
case ARCH_ALPHA: |
|
|
case ARCH_HPPA: |
|
1279 |
case ARCH_SPARC: |
case ARCH_SPARC: |
|
case ARCH_URISC: |
|
1280 |
break; |
break; |
1281 |
|
|
1282 |
case ARCH_ARM: |
case ARCH_TRANSPUTER: |
1283 |
cpu->pc &= 0xfffffffc; |
cpu->pc &= 0xffffffffULL; |
|
cpu->cd.arm.r[ARM_PC] = cpu->pc; |
|
1284 |
break; |
break; |
1285 |
|
|
1286 |
case ARCH_X86: |
case ARCH_X86: |
1336 |
useremul_setup(cpu, n_load, load_names); |
useremul_setup(cpu, n_load, load_names); |
1337 |
|
|
1338 |
/* Startup the bootstrap CPU: */ |
/* Startup the bootstrap CPU: */ |
1339 |
cpu->bootstrap_cpu_flag = 1; |
cpu->running = 1; |
|
cpu->running = 1; |
|
1340 |
|
|
1341 |
/* ... or pause all CPUs, if start_paused is set: */ |
/* ... or pause all CPUs, if start_paused is set: */ |
1342 |
if (m->start_paused) { |
if (m->start_paused) { |
1348 |
add_dump_points(m); |
add_dump_points(m); |
1349 |
|
|
1350 |
/* TODO: This is MIPS-specific! */ |
/* TODO: This is MIPS-specific! */ |
1351 |
if (m->machine_type == MACHINE_DEC && |
if (m->machine_type == MACHINE_PMAX && |
1352 |
cpu->cd.mips.cpu_type.mmu_model == MMU3K) |
cpu->cd.mips.cpu_type.mmu_model == MMU3K) |
1353 |
add_symbol_name(&m->symbol_context, |
add_symbol_name(&m->symbol_context, |
1354 |
0x9fff0000, 0x10000, "r2k3k_cache", 0); |
0x9fff0000, 0x10000, "r2k3k_cache", 0, 0); |
1355 |
|
|
1356 |
symbol_recalc_sizes(&m->symbol_context); |
symbol_recalc_sizes(&m->symbol_context); |
1357 |
|
|
|
if (m->max_random_cycles_per_chunk > 0) |
|
|
debug("using random cycle chunks (1 to %i cycles)\n", |
|
|
m->max_random_cycles_per_chunk); |
|
|
|
|
1358 |
/* Special hack for ARC/SGI emulation: */ |
/* Special hack for ARC/SGI emulation: */ |
1359 |
if ((m->machine_type == MACHINE_ARC || |
if ((m->machine_type == MACHINE_ARC || |
1360 |
m->machine_type == MACHINE_SGI) && m->prom_emulation) |
m->machine_type == MACHINE_SGI) && m->prom_emulation) |
1362 |
|
|
1363 |
debug("starting cpu%i at ", m->bootstrap_cpu); |
debug("starting cpu%i at ", m->bootstrap_cpu); |
1364 |
switch (m->arch) { |
switch (m->arch) { |
1365 |
|
|
1366 |
|
case ARCH_ARM: |
1367 |
|
/* ARM cpus aren't 64-bit: */ |
1368 |
|
debug("0x%08x", (int)entrypoint); |
1369 |
|
break; |
1370 |
|
|
1371 |
|
case ARCH_AVR: |
1372 |
|
/* Atmel AVR uses a 16-bit or 22-bit program counter: */ |
1373 |
|
debug("0x%04x", (int)entrypoint); |
1374 |
|
break; |
1375 |
|
|
1376 |
case ARCH_MIPS: |
case ARCH_MIPS: |
1377 |
if (cpu->cd.mips.cpu_type.isa_level < 3 || |
if (cpu->is_32bit) { |
|
cpu->cd.mips.cpu_type.isa_level == 32) { |
|
1378 |
debug("0x%08x", (int)m->cpus[ |
debug("0x%08x", (int)m->cpus[ |
1379 |
m->bootstrap_cpu]->pc); |
m->bootstrap_cpu]->pc); |
1380 |
if (cpu->cd.mips.gpr[MIPS_GPR_GP] != 0) |
if (cpu->cd.mips.gpr[MIPS_GPR_GP] != 0) |
1389 |
cpu->cd.mips.gpr[MIPS_GPR_GP]); |
cpu->cd.mips.gpr[MIPS_GPR_GP]); |
1390 |
} |
} |
1391 |
break; |
break; |
1392 |
|
|
1393 |
case ARCH_PPC: |
case ARCH_PPC: |
1394 |
if (cpu->cd.ppc.bits == 32) |
if (cpu->cd.ppc.bits == 32) |
1395 |
debug("0x%08x", (int)entrypoint); |
debug("0x%08x", (int)entrypoint); |
1396 |
else |
else |
1397 |
debug("0x%016llx", (long long)entrypoint); |
debug("0x%016llx", (long long)entrypoint); |
1398 |
break; |
break; |
|
case ARCH_ARM: |
|
|
/* ARM cpus aren't 64-bit: */ |
|
|
debug("0x%08x", (int)entrypoint); |
|
|
break; |
|
|
case ARCH_URISC: |
|
|
{ |
|
|
char tmps[100]; |
|
|
unsigned char buf[sizeof(uint64_t)]; |
|
|
|
|
|
cpu->memory_rw(cpu, m->memory, 0, buf, sizeof(buf), |
|
|
MEM_READ, CACHE_NONE | NO_EXCEPTIONS); |
|
|
|
|
|
entrypoint = 0; |
|
|
for (i=0; i<cpu->cd.urisc.wordlen/8; i++) { |
|
|
entrypoint <<= 8; |
|
|
if (cpu->byte_order == EMUL_BIG_ENDIAN) |
|
|
entrypoint += buf[i]; |
|
|
else |
|
|
entrypoint += buf[cpu-> |
|
|
cd.urisc.wordlen/8 - 1 - i]; |
|
|
} |
|
1399 |
|
|
|
snprintf(tmps, sizeof(tmps), "0x%%0%illx", |
|
|
cpu->cd.urisc.wordlen / 4); |
|
|
debug(tmps, (long long)entrypoint); |
|
|
cpu->pc = entrypoint; |
|
|
} |
|
|
break; |
|
1400 |
case ARCH_X86: |
case ARCH_X86: |
1401 |
debug("0x%04x:0x%llx", cpu->cd.x86.s[X86_S_CS], |
debug("0x%04x:0x%llx", cpu->cd.x86.s[X86_S_CS], |
1402 |
(long long)cpu->pc); |
(long long)cpu->pc); |
1403 |
break; |
break; |
1404 |
|
|
1405 |
default: |
default: |
1406 |
debug("0x%016llx", (long long)cpu->pc); |
if (cpu->is_32bit) |
1407 |
|
debug("0x%08x", (int)cpu->pc); |
1408 |
|
else |
1409 |
|
debug("0x%016llx", (long long)cpu->pc); |
1410 |
} |
} |
1411 |
debug("\n"); |
debug("\n"); |
1412 |
|
|
1421 |
*/ |
*/ |
1422 |
void emul_dumpinfo(struct emul *e) |
void emul_dumpinfo(struct emul *e) |
1423 |
{ |
{ |
1424 |
int j, nm, iadd = 4; |
int j, nm, iadd = DEBUG_INDENTATION; |
1425 |
|
|
1426 |
if (e->net != NULL) |
if (e->net != NULL) |
1427 |
net_dumpinfo(e->net); |
net_dumpinfo(e->net); |
1450 |
*/ |
*/ |
1451 |
void emul_simple_init(struct emul *emul) |
void emul_simple_init(struct emul *emul) |
1452 |
{ |
{ |
1453 |
int iadd=4; |
int iadd = DEBUG_INDENTATION; |
1454 |
struct machine *m; |
struct machine *m; |
1455 |
|
|
1456 |
if (emul->n_machines != 1) { |
if (emul->n_machines != 1) { |
1487 |
*/ |
*/ |
1488 |
struct emul *emul_create_from_configfile(char *fname) |
struct emul *emul_create_from_configfile(char *fname) |
1489 |
{ |
{ |
1490 |
int iadd = 4; |
int iadd = DEBUG_INDENTATION; |
1491 |
struct emul *e = emul_new(fname); |
struct emul *e = emul_new(fname); |
|
FILE *f; |
|
|
char buf[128]; |
|
|
size_t len; |
|
1492 |
|
|
1493 |
debug("Creating emulation from configfile \"%s\":\n", fname); |
debug("Creating emulation from configfile \"%s\":\n", fname); |
1494 |
debug_indentation(iadd); |
debug_indentation(iadd); |
1495 |
|
|
1496 |
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); |
|
1497 |
|
|
|
fclose(f); |
|
1498 |
debug_indentation(-iadd); |
debug_indentation(-iadd); |
1499 |
return e; |
return e; |
1500 |
} |
} |
1521 |
|
|
1522 |
atexit(fix_console); |
atexit(fix_console); |
1523 |
|
|
|
i = 79; |
|
|
while (i-- > 0) |
|
|
debug("-"); |
|
|
debug("\n\n"); |
|
|
|
|
1524 |
/* Initialize the interactive debugger: */ |
/* Initialize the interactive debugger: */ |
1525 |
debugger_init(emuls, n_emuls); |
debugger_init(emuls, n_emuls); |
1526 |
|
|
1527 |
|
/* Run any additional debugger commands before starting: */ |
1528 |
|
for (i=0; i<n_emuls; i++) { |
1529 |
|
struct emul *emul = emuls[i]; |
1530 |
|
if (emul->n_debugger_cmds > 0) { |
1531 |
|
int j; |
1532 |
|
if (i == 0) |
1533 |
|
print_separator(); |
1534 |
|
for (j = 0; j < emul->n_debugger_cmds; j ++) { |
1535 |
|
debug("> %s\n", emul->debugger_cmds[j]); |
1536 |
|
debugger_execute_cmd(emul->debugger_cmds[j], |
1537 |
|
strlen(emul->debugger_cmds[j])); |
1538 |
|
} |
1539 |
|
} |
1540 |
|
} |
1541 |
|
|
1542 |
|
print_separator(); |
1543 |
|
debug("\n"); |
1544 |
|
|
1545 |
|
|
1546 |
/* |
/* |
1547 |
* console_init_main() makes sure that the terminal is in a |
* console_init_main() makes sure that the terminal is in a |
1548 |
* reasonable state. |
* reasonable state. |
1567 |
if (e == NULL) |
if (e == NULL) |
1568 |
continue; |
continue; |
1569 |
for (j=0; j<e->n_machines; j++) |
for (j=0; j<e->n_machines; j++) |
1570 |
cpu_run_init(e, e->machines[j]); |
cpu_run_init(e->machines[j]); |
1571 |
} |
} |
1572 |
|
|
1573 |
|
/* TODO: Generalize: */ |
1574 |
|
if (emuls[0]->machines[0]->show_trace_tree) |
1575 |
|
cpu_functioncall_trace(emuls[0]->machines[0]->cpus[0], |
1576 |
|
emuls[0]->machines[0]->cpus[0]->pc); |
1577 |
|
|
1578 |
/* |
/* |
1579 |
* MAIN LOOP: |
* MAIN LOOP: |
1580 |
* |
* |
1584 |
while (go) { |
while (go) { |
1585 |
go = 0; |
go = 0; |
1586 |
|
|
1587 |
x11_check_event(emuls, n_emuls); |
/* Flush X11 and serial console output every now and then: */ |
1588 |
|
if (emuls[0]->machines[0]->ninstrs > |
1589 |
for (i=0; i<n_emuls; i++) { |
emuls[0]->machines[0]->ninstrs_flush + (1<<19)) { |
1590 |
e = emuls[i]; |
x11_check_event(emuls, n_emuls); |
1591 |
if (e == NULL) |
console_flush(); |
1592 |
continue; |
emuls[0]->machines[0]->ninstrs_flush = |
1593 |
|
emuls[0]->machines[0]->ninstrs; |
1594 |
for (j=0; j<e->n_machines; j++) { |
} |
1595 |
/* TODO: cpu_run() is a strange name, since |
|
1596 |
there can be multiple cpus in a machine */ |
if (emuls[0]->machines[0]->ninstrs > |
1597 |
anything = cpu_run(e, e->machines[j]); |
emuls[0]->machines[0]->ninstrs_show + (1<<25)) { |
1598 |
if (anything) |
emuls[0]->machines[0]->ninstrs_since_gettimeofday += |
1599 |
go = 1; |
(emuls[0]->machines[0]->ninstrs - |
1600 |
} |
emuls[0]->machines[0]->ninstrs_show); |
1601 |
|
cpu_show_cycles(emuls[0]->machines[0], 0); |
1602 |
|
emuls[0]->machines[0]->ninstrs_show = |
1603 |
|
emuls[0]->machines[0]->ninstrs; |
1604 |
|
} |
1605 |
|
|
1606 |
|
if (single_step == ENTER_SINGLE_STEPPING) { |
1607 |
|
/* TODO: Cleanup! */ |
1608 |
|
old_instruction_trace = |
1609 |
|
emuls[0]->machines[0]->instruction_trace; |
1610 |
|
old_quiet_mode = quiet_mode; |
1611 |
|
old_show_trace_tree = |
1612 |
|
emuls[0]->machines[0]->show_trace_tree; |
1613 |
|
emuls[0]->machines[0]->instruction_trace = 1; |
1614 |
|
emuls[0]->machines[0]->show_trace_tree = 1; |
1615 |
|
quiet_mode = 0; |
1616 |
|
single_step = SINGLE_STEPPING; |
1617 |
|
} |
1618 |
|
|
1619 |
|
if (single_step == SINGLE_STEPPING) |
1620 |
|
debugger(); |
1621 |
|
|
1622 |
|
e = emuls[0]; /* Note: Only 1 emul supported now. */ |
1623 |
|
|
1624 |
|
for (j=0; j<e->n_machines; j++) { |
1625 |
|
if (e->machines[j]->gdb.port > 0) |
1626 |
|
debugger_gdb_check_incoming(e->machines[j]); |
1627 |
|
|
1628 |
|
anything = machine_run(e->machines[j]); |
1629 |
|
if (anything) |
1630 |
|
go = 1; |
1631 |
} |
} |
1632 |
} |
} |
1633 |
|
|
1637 |
if (e == NULL) |
if (e == NULL) |
1638 |
continue; |
continue; |
1639 |
for (j=0; j<e->n_machines; j++) |
for (j=0; j<e->n_machines; j++) |
1640 |
cpu_run_deinit(e, e->machines[j]); |
cpu_run_deinit(e->machines[j]); |
1641 |
} |
} |
1642 |
|
|
1643 |
/* force_debugger_at_exit flag set? Then enter the debugger: */ |
/* force_debugger_at_exit flag set? Then enter the debugger: */ |