1 |
/* |
/* |
2 |
* Copyright (C) 2005 Anders Gavare. All rights reserved. |
* Copyright (C) 2005-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: cpu.c,v 1.316 2005/08/16 05:37:09 debug Exp $ |
* $Id: cpu.c,v 1.344 2006/06/24 21:47:22 debug Exp $ |
29 |
* |
* |
30 |
* Common routines for CPU emulation. (Not specific to any CPU type.) |
* Common routines for CPU emulation. (Not specific to any CPU type.) |
31 |
*/ |
*/ |
33 |
#include <stdio.h> |
#include <stdio.h> |
34 |
#include <stdlib.h> |
#include <stdlib.h> |
35 |
#include <sys/types.h> |
#include <sys/types.h> |
36 |
|
#include <sys/mman.h> |
37 |
#include <string.h> |
#include <string.h> |
38 |
|
|
39 |
#include "cpu.h" |
#include "cpu.h" |
43 |
|
|
44 |
|
|
45 |
extern int quiet_mode; |
extern int quiet_mode; |
|
extern int show_opcode_statistics; |
|
|
|
|
46 |
|
|
47 |
static struct cpu_family *first_cpu_family = NULL; |
static struct cpu_family *first_cpu_family = NULL; |
48 |
|
|
96 |
"NULL\n"); |
"NULL\n"); |
97 |
exit(1); |
exit(1); |
98 |
} |
} |
99 |
return cpu; |
break; |
100 |
} |
} |
101 |
} |
} |
102 |
|
|
103 |
fp = fp->next; |
fp = fp->next; |
104 |
} |
} |
105 |
|
|
106 |
fatal("\ncpu_new(): unknown cpu type '%s'\n", cpu_type_name); |
if (fp == NULL) { |
107 |
exit(1); |
fatal("\ncpu_new(): unknown cpu type '%s'\n", cpu_type_name); |
108 |
} |
return NULL; |
109 |
|
} |
110 |
|
|
111 |
|
fp->init_tables(cpu); |
112 |
|
|
113 |
/* |
return cpu; |
|
* cpu_show_full_statistics(): |
|
|
* |
|
|
* Show detailed statistics on opcode usage on each cpu. |
|
|
*/ |
|
|
void cpu_show_full_statistics(struct machine *m) |
|
|
{ |
|
|
if (m->cpu_family == NULL || |
|
|
m->cpu_family->show_full_statistics == NULL) |
|
|
fatal("cpu_show_full_statistics(): NULL\n"); |
|
|
else |
|
|
m->cpu_family->show_full_statistics(m); |
|
114 |
} |
} |
115 |
|
|
116 |
|
|
155 |
* tracing. |
* tracing. |
156 |
*/ |
*/ |
157 |
int cpu_disassemble_instr(struct machine *m, struct cpu *cpu, |
int cpu_disassemble_instr(struct machine *m, struct cpu *cpu, |
158 |
unsigned char *instr, int running, uint64_t addr, int bintrans) |
unsigned char *instr, int running, uint64_t addr) |
159 |
{ |
{ |
160 |
if (m->cpu_family == NULL || m->cpu_family->disassemble_instr == NULL) { |
if (m->cpu_family == NULL || m->cpu_family->disassemble_instr == NULL) { |
161 |
fatal("cpu_disassemble_instr(): NULL\n"); |
fatal("cpu_disassemble_instr(): NULL\n"); |
162 |
return 0; |
return 0; |
163 |
} else |
} else |
164 |
return m->cpu_family->disassemble_instr(cpu, instr, |
return m->cpu_family->disassemble_instr(cpu, instr, |
165 |
running, addr, bintrans); |
running, addr); |
166 |
} |
} |
167 |
|
|
168 |
|
|
171 |
* |
* |
172 |
* Dump cpu registers in a relatively readable format. |
* Dump cpu registers in a relatively readable format. |
173 |
* |
* |
174 |
* gprs: set to non-zero to dump GPRs. (CPU dependant.) |
* gprs: set to non-zero to dump GPRs. (CPU dependent.) |
175 |
* coprocs: set bit 0..x to dump registers in coproc 0..x. (CPU dependant.) |
* coprocs: set bit 0..x to dump registers in coproc 0..x. (CPU dependent.) |
176 |
*/ |
*/ |
177 |
void cpu_register_dump(struct machine *m, struct cpu *cpu, |
void cpu_register_dump(struct machine *m, struct cpu *cpu, |
178 |
int gprs, int coprocs) |
int gprs, int coprocs) |
185 |
|
|
186 |
|
|
187 |
/* |
/* |
188 |
|
* cpu_gdb_stub(): |
189 |
|
* |
190 |
|
* Execute a "remote GDB" command. Return value is a pointer to a newly |
191 |
|
* allocated response string, if the command was successfully executed. If |
192 |
|
* there was an error, NULL is returned. |
193 |
|
*/ |
194 |
|
char *cpu_gdb_stub(struct cpu *cpu, char *cmd) |
195 |
|
{ |
196 |
|
if (cpu->machine->cpu_family == NULL || |
197 |
|
cpu->machine->cpu_family->gdb_stub == NULL) { |
198 |
|
fatal("cpu_gdb_stub(): NULL\n"); |
199 |
|
return NULL; |
200 |
|
} else |
201 |
|
return cpu->machine->cpu_family->gdb_stub(cpu, cmd); |
202 |
|
} |
203 |
|
|
204 |
|
|
205 |
|
/* |
206 |
* cpu_interrupt(): |
* cpu_interrupt(): |
207 |
* |
* |
208 |
* Assert an interrupt. |
* Assert an interrupt. |
252 |
fatal("cpu%i:\t", cpu->cpu_id); |
fatal("cpu%i:\t", cpu->cpu_id); |
253 |
|
|
254 |
cpu->trace_tree_depth ++; |
cpu->trace_tree_depth ++; |
255 |
|
if (cpu->trace_tree_depth > 100) |
256 |
|
cpu->trace_tree_depth = 100; |
257 |
for (i=0; i<cpu->trace_tree_depth; i++) |
for (i=0; i<cpu->trace_tree_depth; i++) |
258 |
fatal(" "); |
fatal(" "); |
259 |
|
|
264 |
fatal("%s", symbol); |
fatal("%s", symbol); |
265 |
else { |
else { |
266 |
if (cpu->is_32bit) |
if (cpu->is_32bit) |
267 |
fatal("0x%08x", (int)f); |
fatal("0x%"PRIx32, (uint32_t) f); |
268 |
else |
else |
269 |
fatal("0x%llx", (long long)f); |
fatal("0x%"PRIx64, (uint64_t) f); |
270 |
} |
} |
271 |
fatal("("); |
fatal("("); |
272 |
|
|
274 |
cpu->machine->cpu_family->functioncall_trace(cpu, f, n_args); |
cpu->machine->cpu_family->functioncall_trace(cpu, f, n_args); |
275 |
|
|
276 |
fatal(")>\n"); |
fatal(")>\n"); |
277 |
|
|
278 |
|
#ifdef PRINT_MEMORY_CHECKSUM |
279 |
|
/* Temporary hack for finding bugs: */ |
280 |
|
fatal("call chksum=%016"PRIx64"\n", memory_checksum(cpu->mem)); |
281 |
|
#endif |
282 |
} |
} |
283 |
|
|
284 |
|
|
307 |
*/ |
*/ |
308 |
void cpu_create_or_reset_tc(struct cpu *cpu) |
void cpu_create_or_reset_tc(struct cpu *cpu) |
309 |
{ |
{ |
310 |
|
size_t s = DYNTRANS_CACHE_SIZE + DYNTRANS_CACHE_MARGIN; |
311 |
|
|
312 |
if (cpu->translation_cache == NULL) |
if (cpu->translation_cache == NULL) |
313 |
cpu->translation_cache = zeroed_alloc(DYNTRANS_CACHE_SIZE + |
cpu->translation_cache = zeroed_alloc(s); |
|
DYNTRANS_CACHE_MARGIN); |
|
314 |
|
|
315 |
/* Create an empty table at the beginning of the translation cache: */ |
/* Create an empty table at the beginning of the translation cache: */ |
316 |
memset(cpu->translation_cache, 0, sizeof(uint32_t) |
memset(cpu->translation_cache, 0, sizeof(uint32_t) |
323 |
* There might be other translation pointers that still point to |
* There might be other translation pointers that still point to |
324 |
* within the translation_cache region. Let's invalidate those too: |
* within the translation_cache region. Let's invalidate those too: |
325 |
*/ |
*/ |
326 |
if (cpu->invalidate_code_translation_caches != NULL) |
if (cpu->invalidate_code_translation != NULL) |
327 |
cpu->invalidate_code_translation_caches(cpu); |
cpu->invalidate_code_translation(cpu, 0, INVALIDATE_ALL); |
|
} |
|
|
|
|
|
|
|
|
/* |
|
|
* cpu_run(): |
|
|
* |
|
|
* Run instructions on all CPUs in this machine, for a "medium duration" |
|
|
* (or until all CPUs have halted). |
|
|
* |
|
|
* Return value is 1 if anything happened, 0 if all CPUs are stopped. |
|
|
*/ |
|
|
int cpu_run(struct emul *emul, struct machine *m) |
|
|
{ |
|
|
if (m->cpu_family == NULL || m->cpu_family->run == NULL) { |
|
|
fatal("cpu_run(): NULL\n"); |
|
|
return 0; |
|
|
} else |
|
|
return m->cpu_family->run(emul, m); |
|
328 |
} |
} |
329 |
|
|
330 |
|
|
332 |
* cpu_dumpinfo(): |
* cpu_dumpinfo(): |
333 |
* |
* |
334 |
* Dumps info about a CPU using debug(). "cpu0: CPUNAME, running" (or similar) |
* Dumps info about a CPU using debug(). "cpu0: CPUNAME, running" (or similar) |
335 |
* is outputed, and it is up to CPU dependant code to complete the line. |
* is outputed, and it is up to CPU dependent code to complete the line. |
336 |
*/ |
*/ |
337 |
void cpu_dumpinfo(struct machine *m, struct cpu *cpu) |
void cpu_dumpinfo(struct machine *m, struct cpu *cpu) |
338 |
{ |
{ |
354 |
void cpu_list_available_types(void) |
void cpu_list_available_types(void) |
355 |
{ |
{ |
356 |
struct cpu_family *fp; |
struct cpu_family *fp; |
357 |
int iadd = 4; |
int iadd = DEBUG_INDENTATION; |
358 |
|
|
359 |
fp = first_cpu_family; |
fp = first_cpu_family; |
360 |
|
|
405 |
if (machine->show_nr_of_instructions || !quiet_mode) |
if (machine->show_nr_of_instructions || !quiet_mode) |
406 |
cpu_show_cycles(machine, 1); |
cpu_show_cycles(machine, 1); |
407 |
|
|
|
if (show_opcode_statistics) |
|
|
cpu_show_full_statistics(machine); |
|
|
|
|
408 |
fflush(stdout); |
fflush(stdout); |
409 |
} |
} |
410 |
|
|
423 |
char *symbol; |
char *symbol; |
424 |
int64_t mseconds, ninstrs, is, avg; |
int64_t mseconds, ninstrs, is, avg; |
425 |
struct timeval tv; |
struct timeval tv; |
426 |
int h, m, s, ms, d, instrs_per_cycle = 1; |
int h, m, s, ms, d; |
427 |
|
|
428 |
static int64_t mseconds_last = 0; |
static int64_t mseconds_last = 0; |
429 |
static int64_t ninstrs_last = -1; |
static int64_t ninstrs_last = -1; |
430 |
|
|
|
switch (machine->arch) { |
|
|
case ARCH_MIPS: |
|
|
instrs_per_cycle = machine->cpus[machine->bootstrap_cpu]-> |
|
|
cd.mips.cpu_type.instrs_per_cycle; |
|
|
break; |
|
|
} |
|
|
|
|
431 |
pc = machine->cpus[machine->bootstrap_cpu]->pc; |
pc = machine->cpus[machine->bootstrap_cpu]->pc; |
432 |
|
|
433 |
gettimeofday(&tv, NULL); |
gettimeofday(&tv, NULL); |
440 |
if (mseconds - mseconds_last == 0) |
if (mseconds - mseconds_last == 0) |
441 |
mseconds ++; |
mseconds ++; |
442 |
|
|
443 |
ninstrs = machine->ncycles_since_gettimeofday * instrs_per_cycle; |
ninstrs = machine->ncycles_since_gettimeofday; |
444 |
|
|
445 |
if (machine->automatic_clock_adjustment) { |
if (machine->automatic_clock_adjustment) { |
446 |
static int first_adjustment = 1; |
static int first_adjustment = 1; |
447 |
|
|
448 |
/* Current nr of cycles per second: */ |
/* Current nr of cycles per second: */ |
449 |
int64_t cur_cycles_per_second = 1000 * |
int64_t cur_cycles_per_second = 1000 * |
450 |
(ninstrs-ninstrs_last) / (mseconds-mseconds_last) |
(ninstrs-ninstrs_last) / (mseconds-mseconds_last); |
451 |
/ instrs_per_cycle; |
|
452 |
|
/* fatal("[ CYCLES PER SECOND = %"PRIi64" ]\n", |
453 |
|
cur_cycles_per_second); */ |
454 |
|
|
455 |
if (cur_cycles_per_second < 1000000) |
if (cur_cycles_per_second < 1000000) |
456 |
cur_cycles_per_second = 1000000; |
cur_cycles_per_second = 1000000; |
463 |
cur_cycles_per_second) / 16; |
cur_cycles_per_second) / 16; |
464 |
} |
} |
465 |
|
|
466 |
/* debug("[ updating emulated_hz to %lli Hz ]\n", |
/* fatal("[ updating emulated_hz to %"PRIi64" Hz ]\n", |
467 |
(long long)machine->emulated_hz); */ |
machine->emulated_hz); */ |
468 |
} |
} |
469 |
|
|
470 |
|
|
472 |
if (!machine->show_nr_of_instructions && !forced) |
if (!machine->show_nr_of_instructions && !forced) |
473 |
goto do_return; |
goto do_return; |
474 |
|
|
475 |
printf("[ %lli instrs", |
printf("[ %"PRIi64" instrs", (int64_t)machine->ncycles); |
|
(long long)(machine->ncycles * instrs_per_cycle)); |
|
476 |
|
|
477 |
if (!machine->automatic_clock_adjustment) { |
if (!machine->automatic_clock_adjustment) { |
478 |
d = machine->emulated_hz / 1000; |
d = machine->emulated_hz / 1000; |
486 |
s = ms / 1000; |
s = ms / 1000; |
487 |
ms -= 1000 * s; |
ms -= 1000 * s; |
488 |
|
|
489 |
printf("emulated time = %02i:%02i:%02i.%03i; ", h, m, s, ms); |
printf(", emulated time = %02i:%02i:%02i.%03i; ", h, m, s, ms); |
490 |
} |
} |
491 |
|
|
492 |
/* Instructions per second, and average so far: */ |
/* Instructions per second, and average so far: */ |
496 |
is = 0; |
is = 0; |
497 |
if (avg < 0) |
if (avg < 0) |
498 |
avg = 0; |
avg = 0; |
499 |
printf("; i/s=%lli avg=%lli", (long long)is, (long long)avg); |
printf("; i/s=%"PRIi64" avg=%"PRIi64, is, avg); |
500 |
|
|
501 |
symbol = get_symbol_name(&machine->symbol_context, pc, &offset); |
symbol = get_symbol_name(&machine->symbol_context, pc, &offset); |
502 |
|
|
503 |
if (machine->ncpus == 1) { |
if (machine->ncpus == 1) { |
504 |
if (machine->cpus[machine->bootstrap_cpu]->is_32bit) |
if (machine->cpus[machine->bootstrap_cpu]->is_32bit) |
505 |
printf("; pc=0x%08x", (int)pc); |
printf("; pc=0x%08"PRIx32, (uint32_t) pc); |
506 |
else |
else |
507 |
printf("; pc=0x%016llx", (long long)pc); |
printf("; pc=0x%016"PRIx64, (uint64_t) pc); |
508 |
} |
} |
509 |
|
|
510 |
if (symbol != NULL) |
if (symbol != NULL) |
548 |
if (machine->a_few_cycles < 1) |
if (machine->a_few_cycles < 1) |
549 |
machine->a_few_cycles = 1; |
machine->a_few_cycles = 1; |
550 |
|
|
551 |
if (ncpus > 1 && machine->max_random_cycles_per_chunk == 0) |
if (ncpus > 1) |
552 |
machine->a_few_cycles = 1; |
machine->a_few_cycles = 1; |
553 |
|
|
554 |
/* debug("cpu_run_init(): a_few_cycles = %i\n", |
/* debug("cpu_run_init(): a_few_cycles = %i\n", |
642 |
add_cpu_family(arm_cpu_family_init, ARCH_ARM); |
add_cpu_family(arm_cpu_family_init, ARCH_ARM); |
643 |
#endif |
#endif |
644 |
|
|
645 |
|
#ifdef ENABLE_AVR |
646 |
|
add_cpu_family(avr_cpu_family_init, ARCH_AVR); |
647 |
|
#endif |
648 |
|
|
649 |
|
#ifdef ENABLE_HPPA |
650 |
|
add_cpu_family(hppa_cpu_family_init, ARCH_HPPA); |
651 |
|
#endif |
652 |
|
|
653 |
|
#ifdef ENABLE_I960 |
654 |
|
add_cpu_family(i960_cpu_family_init, ARCH_I960); |
655 |
|
#endif |
656 |
|
|
657 |
#ifdef ENABLE_IA64 |
#ifdef ENABLE_IA64 |
658 |
add_cpu_family(ia64_cpu_family_init, ARCH_IA64); |
add_cpu_family(ia64_cpu_family_init, ARCH_IA64); |
659 |
#endif |
#endif |
670 |
add_cpu_family(ppc_cpu_family_init, ARCH_PPC); |
add_cpu_family(ppc_cpu_family_init, ARCH_PPC); |
671 |
#endif |
#endif |
672 |
|
|
673 |
|
#ifdef ENABLE_SH |
674 |
|
add_cpu_family(sh_cpu_family_init, ARCH_SH); |
675 |
|
#endif |
676 |
|
|
677 |
#ifdef ENABLE_SPARC |
#ifdef ENABLE_SPARC |
678 |
add_cpu_family(sparc_cpu_family_init, ARCH_SPARC); |
add_cpu_family(sparc_cpu_family_init, ARCH_SPARC); |
679 |
#endif |
#endif |