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_arm.c,v 1.44 2005/11/19 18:53:07 debug Exp $ |
* $Id: cpu_arm.c,v 1.59 2006/06/16 18:31:25 debug Exp $ |
29 |
* |
* |
30 |
* ARM CPU emulation. |
* ARM CPU emulation. |
31 |
* |
* |
45 |
#include "machine.h" |
#include "machine.h" |
46 |
#include "memory.h" |
#include "memory.h" |
47 |
#include "misc.h" |
#include "misc.h" |
48 |
|
#include "of.h" |
49 |
#include "symbol.h" |
#include "symbol.h" |
50 |
|
|
51 |
#define DYNTRANS_32 |
#define DYNTRANS_32 |
64 |
static int arm_exception_to_mode[N_ARM_EXCEPTIONS] = ARM_EXCEPTION_TO_MODE; |
static int arm_exception_to_mode[N_ARM_EXCEPTIONS] = ARM_EXCEPTION_TO_MODE; |
65 |
|
|
66 |
/* For quick_pc_to_pointers(): */ |
/* For quick_pc_to_pointers(): */ |
67 |
#include "arm_quick_pc_to_pointers.h" |
void arm_pc_to_pointers(struct cpu *cpu); |
68 |
|
#include "quick_pc_to_pointers.h" |
69 |
|
|
70 |
|
|
71 |
/* |
/* |
99 |
cpu->invalidate_code_translation = arm_invalidate_code_translation; |
cpu->invalidate_code_translation = arm_invalidate_code_translation; |
100 |
cpu->translate_address = arm_translate_address; |
cpu->translate_address = arm_translate_address; |
101 |
|
|
102 |
cpu->cd.arm.cpu_type = cpu_type_defs[found]; |
cpu->cd.arm.cpu_type = cpu_type_defs[found]; |
103 |
cpu->name = cpu->cd.arm.cpu_type.name; |
cpu->name = cpu->cd.arm.cpu_type.name; |
104 |
cpu->is_32bit = 1; |
cpu->is_32bit = 1; |
105 |
|
|
106 |
cpu->cd.arm.cpsr = ARM_FLAG_I | ARM_FLAG_F; |
cpu->cd.arm.cpsr = ARM_FLAG_I | ARM_FLAG_F; |
107 |
cpu->cd.arm.control = ARM_CONTROL_PROG32 | ARM_CONTROL_DATA32 |
cpu->cd.arm.control = ARM_CONTROL_PROG32 | ARM_CONTROL_DATA32 |
108 |
| ARM_CONTROL_CACHE | ARM_CONTROL_ICACHE | ARM_CONTROL_ALIGN; |
| ARM_CONTROL_CACHE | ARM_CONTROL_ICACHE | ARM_CONTROL_ALIGN; |
109 |
|
/* TODO: default auxctrl contents */ |
110 |
|
|
111 |
if (cpu->machine->prom_emulation) { |
if (cpu->machine->prom_emulation) { |
112 |
cpu->cd.arm.cpsr |= ARM_MODE_SVC32; |
cpu->cd.arm.cpsr |= ARM_MODE_SVC32; |
131 |
} |
} |
132 |
} |
} |
133 |
|
|
134 |
|
/* TODO: Some of these values (iway and dway) aren't used yet: */ |
135 |
|
cpu->cd.arm.cachetype = |
136 |
|
(5 << ARM_CACHETYPE_CLASS_SHIFT) |
137 |
|
| (1 << ARM_CACHETYPE_HARVARD_SHIFT) |
138 |
|
| ((cpu->cd.arm.cpu_type.dcache_shift - 9) << |
139 |
|
ARM_CACHETYPE_DSIZE_SHIFT) |
140 |
|
| (5 << ARM_CACHETYPE_DASSOC_SHIFT) /* 32-way */ |
141 |
|
| (2 << ARM_CACHETYPE_DLINE_SHIFT) /* 8 words/line */ |
142 |
|
| ((cpu->cd.arm.cpu_type.icache_shift - 9) << |
143 |
|
ARM_CACHETYPE_ISIZE_SHIFT) |
144 |
|
| (5 << ARM_CACHETYPE_IASSOC_SHIFT) /* 32-way */ |
145 |
|
| (2 << ARM_CACHETYPE_ILINE_SHIFT); /* 8 words/line */ |
146 |
|
|
147 |
/* Coprocessor 15 = the system control coprocessor. */ |
/* Coprocessor 15 = the system control coprocessor. */ |
148 |
cpu->cd.arm.coproc[15] = arm_coproc_15; |
cpu->cd.arm.coproc[15] = arm_coproc_15; |
149 |
|
|
150 |
|
/* Coprocessor 14 for XScale: */ |
151 |
|
if (cpu->cd.arm.cpu_type.flags & ARM_XSCALE) |
152 |
|
cpu->cd.arm.coproc[14] = arm_coproc_xscale_14; |
153 |
|
|
154 |
/* |
/* |
155 |
* NOTE/TODO: Ugly hack for OpenFirmware emulation: |
* NOTE/TODO: Ugly hack for OpenFirmware emulation: |
156 |
*/ |
*/ |
321 |
m->cpus[cpunr]->cd.arm.r[i] = *valuep; |
m->cpus[cpunr]->cd.arm.r[i] = *valuep; |
322 |
if (i == ARM_PC) |
if (i == ARM_PC) |
323 |
m->cpus[cpunr]->pc = *valuep; |
m->cpus[cpunr]->pc = *valuep; |
324 |
} else |
} else { |
325 |
*valuep = m->cpus[cpunr]->cd.arm.r[i]; |
*valuep = m->cpus[cpunr]->cd.arm.r[i]; |
326 |
|
if (i == ARM_PC) |
327 |
|
*valuep = m->cpus[cpunr]->pc; |
328 |
|
} |
329 |
*match_register = 1; |
*match_register = 1; |
330 |
} |
} |
331 |
} |
} |
401 |
} |
} |
402 |
|
|
403 |
if (m != ARM_MODE_USR32 && m != ARM_MODE_SYS32) { |
if (m != ARM_MODE_USR32 && m != ARM_MODE_SYS32) { |
404 |
debug("cpu%i: usr r8..r14 =", x); |
debug("cpu%i: usr r8-14:", x); |
405 |
for (i=0; i<7; i++) |
for (i=0; i<7; i++) |
406 |
debug(" %08x", cpu->cd.arm.default_r8_r14[i]); |
debug(" %08x", cpu->cd.arm.default_r8_r14[i]); |
407 |
debug("\n"); |
debug("\n"); |
408 |
} |
} |
409 |
|
|
410 |
if (m != ARM_MODE_FIQ32) { |
if (m != ARM_MODE_FIQ32) { |
411 |
debug("cpu%i: fiq r8..r14 =", x); |
debug("cpu%i: fiq r8-14:", x); |
412 |
for (i=0; i<7; i++) |
for (i=0; i<7; i++) |
413 |
debug(" %08x", cpu->cd.arm.fiq_r8_r14[i]); |
debug(" %08x", cpu->cd.arm.fiq_r8_r14[i]); |
414 |
debug("\n"); |
debug("\n"); |
415 |
} |
} |
416 |
|
|
417 |
if (m != ARM_MODE_IRQ32) { |
if (m != ARM_MODE_IRQ32) { |
418 |
debug("cpu%i: irq r13..r14 =", x); |
debug("cpu%i: irq r13-14:", x); |
419 |
for (i=0; i<2; i++) |
for (i=0; i<2; i++) |
420 |
debug(" %08x", cpu->cd.arm.irq_r13_r14[i]); |
debug(" %08x", cpu->cd.arm.irq_r13_r14[i]); |
421 |
debug("\n"); |
debug("\n"); |
422 |
} |
} |
423 |
|
|
424 |
if (m != ARM_MODE_SVC32) { |
if (m != ARM_MODE_SVC32) { |
425 |
debug("cpu%i: svc r13..r14 =", x); |
debug("cpu%i: svc r13-14:", x); |
426 |
for (i=0; i<2; i++) |
for (i=0; i<2; i++) |
427 |
debug(" %08x", cpu->cd.arm.svc_r13_r14[i]); |
debug(" %08x", cpu->cd.arm.svc_r13_r14[i]); |
428 |
debug("\n"); |
debug("\n"); |
429 |
} |
} |
430 |
|
|
431 |
if (m != ARM_MODE_ABT32) { |
if (m != ARM_MODE_ABT32) { |
432 |
debug("cpu%i: abt r13..r14 =", x); |
debug("cpu%i: abt r13-14:", x); |
433 |
for (i=0; i<2; i++) |
for (i=0; i<2; i++) |
434 |
debug(" %08x", cpu->cd.arm.abt_r13_r14[i]); |
debug(" %08x", cpu->cd.arm.abt_r13_r14[i]); |
435 |
debug("\n"); |
debug("\n"); |
436 |
} |
} |
437 |
|
|
438 |
if (m != ARM_MODE_UND32) { |
if (m != ARM_MODE_UND32) { |
439 |
debug("cpu%i: und r13..r14 =", x); |
debug("cpu%i: und r13-14:", x); |
440 |
for (i=0; i<2; i++) |
for (i=0; i<2; i++) |
441 |
debug(" %08x", cpu->cd.arm.und_r13_r14[i]); |
debug(" %08x", cpu->cd.arm.und_r13_r14[i]); |
442 |
debug("\n"); |
debug("\n"); |
473 |
cpu->cd.arm.control & |
cpu->cd.arm.control & |
474 |
ARM_CONTROL_V? "yes (0xffff0000)" : "no"); |
ARM_CONTROL_V? "yes (0xffff0000)" : "no"); |
475 |
|
|
476 |
|
/* TODO: auxctrl on which CPU types? */ |
477 |
|
if (cpu->cd.arm.cpu_type.flags & ARM_XSCALE) { |
478 |
|
debug("cpu%i: auxctrl = 0x%08x\n", x, |
479 |
|
cpu->cd.arm.auxctrl); |
480 |
|
debug("cpu%i: minidata cache attr = 0x%x\n", x, |
481 |
|
(cpu->cd.arm.auxctrl & ARM_AUXCTRL_MD) |
482 |
|
>> ARM_AUXCTRL_MD_SHIFT); |
483 |
|
debug("cpu%i: page table memory attr: %i\n", x, |
484 |
|
(cpu->cd.arm.auxctrl & ARM_AUXCTRL_P)? 1 : 0); |
485 |
|
debug("cpu%i: write buffer coalescing: %s\n", x, |
486 |
|
(cpu->cd.arm.auxctrl & ARM_AUXCTRL_K)? |
487 |
|
"disabled" : "enabled"); |
488 |
|
} |
489 |
|
|
490 |
debug("cpu%i: ttb = 0x%08x dacr = 0x%08x\n", x, |
debug("cpu%i: ttb = 0x%08x dacr = 0x%08x\n", x, |
491 |
cpu->cd.arm.ttb, cpu->cd.arm.dacr); |
cpu->cd.arm.ttb, cpu->cd.arm.dacr); |
492 |
debug("cpu%i: fsr = 0x%08x far = 0x%08x\n", x, |
debug("cpu%i: fsr = 0x%08x far = 0x%08x\n", x, |
689 |
"mode 0x%02x (pc=0x%x) ]\n", newmode, (int)cpu->pc); |
"mode 0x%02x (pc=0x%x) ]\n", newmode, (int)cpu->pc); |
690 |
/* exit(1); */ |
/* exit(1); */ |
691 |
} |
} |
692 |
#if 0 |
|
|
if (oldmode==0x10 && newmode ==0x17 && cpu->pc == 0x1644f0) |
|
|
single_step = 1; |
|
|
/* 00008554 */ |
|
|
#endif |
|
693 |
cpu->cd.arm.cpsr |= ARM_FLAG_I; |
cpu->cd.arm.cpsr |= ARM_FLAG_I; |
694 |
if (exception_nr == ARM_EXCEPTION_RESET || |
if (exception_nr == ARM_EXCEPTION_RESET || |
695 |
exception_nr == ARM_EXCEPTION_FIQ) |
exception_nr == ARM_EXCEPTION_FIQ) |
714 |
|
|
715 |
|
|
716 |
/* |
/* |
717 |
|
* arm_cpu_tlbdump(): |
718 |
|
* |
719 |
|
* Called from the debugger to dump the TLB in a readable format. |
720 |
|
* x is the cpu number to dump, or -1 to dump all CPUs. |
721 |
|
* |
722 |
|
* If rawflag is nonzero, then the TLB contents isn't formated nicely, |
723 |
|
* just dumped. |
724 |
|
*/ |
725 |
|
void arm_cpu_tlbdump(struct machine *m, int x, int rawflag) |
726 |
|
{ |
727 |
|
} |
728 |
|
|
729 |
|
|
730 |
|
static void add_response_word(struct cpu *cpu, char *r, uint32_t value, |
731 |
|
size_t maxlen) |
732 |
|
{ |
733 |
|
if (cpu->byte_order == EMUL_LITTLE_ENDIAN) { |
734 |
|
value = ((value & 0xff) << 24) + |
735 |
|
((value & 0xff00) << 8) + |
736 |
|
((value & 0xff0000) >> 8) + |
737 |
|
((value & 0xff000000) >> 24); |
738 |
|
} |
739 |
|
snprintf(r + strlen(r), maxlen - strlen(r), "%08"PRIx32, value); |
740 |
|
} |
741 |
|
|
742 |
|
|
743 |
|
/* |
744 |
|
* arm_cpu_gdb_stub(): |
745 |
|
* |
746 |
|
* Execute a "remote GDB" command. Returns a newly allocated response string |
747 |
|
* on success, NULL on failure. |
748 |
|
*/ |
749 |
|
char *arm_cpu_gdb_stub(struct cpu *cpu, char *cmd) |
750 |
|
{ |
751 |
|
if (strcmp(cmd, "g") == 0) { |
752 |
|
/* 15 gprs, pc, 8 fprs, fps, cpsr. */ |
753 |
|
int i; |
754 |
|
char *r; |
755 |
|
size_t len = 1 + 18 * sizeof(uint32_t); |
756 |
|
r = malloc(len); |
757 |
|
if (r == NULL) { |
758 |
|
fprintf(stderr, "out of memory\n"); |
759 |
|
exit(1); |
760 |
|
} |
761 |
|
r[0] = '\0'; |
762 |
|
for (i=0; i<15; i++) |
763 |
|
add_response_word(cpu, r, cpu->cd.arm.r[i], len); |
764 |
|
add_response_word(cpu, r, cpu->pc, len); |
765 |
|
/* TODO: fprs: */ |
766 |
|
for (i=0; i<8; i++) |
767 |
|
add_response_word(cpu, r, 0, len); |
768 |
|
/* TODO: fps */ |
769 |
|
add_response_word(cpu, r, 0, len); |
770 |
|
add_response_word(cpu, r, cpu->cd.arm.cpsr, len); |
771 |
|
return r; |
772 |
|
} |
773 |
|
|
774 |
|
if (cmd[0] == 'p') { |
775 |
|
int regnr = strtol(cmd + 1, NULL, 16); |
776 |
|
size_t len = 2 * sizeof(uint32_t) + 1; |
777 |
|
char *r = malloc(len); |
778 |
|
r[0] = '\0'; |
779 |
|
if (regnr == ARM_PC) { |
780 |
|
add_response_word(cpu, r, cpu->pc, len); |
781 |
|
} else if (regnr >= 0 && regnr < ARM_PC) { |
782 |
|
add_response_word(cpu, r, cpu->cd.arm.r[regnr], len); |
783 |
|
} else if (regnr >= 0x10 && regnr <= 0x17) { |
784 |
|
/* TODO: fprs */ |
785 |
|
add_response_word(cpu, r, 0, len); |
786 |
|
add_response_word(cpu, r, 0, len); |
787 |
|
add_response_word(cpu, r, 0, len); |
788 |
|
} else if (regnr == 0x18) { |
789 |
|
/* TODO: fps */ |
790 |
|
add_response_word(cpu, r, 0, len); |
791 |
|
} else if (regnr == 0x19) { |
792 |
|
add_response_word(cpu, r, cpu->cd.arm.cpsr, len); |
793 |
|
} |
794 |
|
return r; |
795 |
|
} |
796 |
|
|
797 |
|
fatal("arm_cpu_gdb_stub(): TODO\n"); |
798 |
|
return NULL; |
799 |
|
} |
800 |
|
|
801 |
|
|
802 |
|
/* |
803 |
* arm_cpu_interrupt(): |
* arm_cpu_interrupt(): |
804 |
* |
* |
805 |
* 0..31 are used as footbridge interrupt numbers, 32..47 = ISA, |
* 0..31 are used as footbridge interrupt numbers, 32..47 = ISA, |
815 |
cpu->machine->md_interrupt(cpu->machine, |
cpu->machine->md_interrupt(cpu->machine, |
816 |
cpu, irq_nr, 1); |
cpu, irq_nr, 1); |
817 |
else |
else |
818 |
fatal("arm_cpu_interrupt(): md_interrupt == NULL\n"); |
fatal("arm_cpu_interrupt(): irq_nr=%i md_interrupt ==" |
819 |
|
" NULL\n", (int)irq_nr); |
820 |
} else { |
} else { |
821 |
/* Assert ARM IRQs: */ |
/* Assert ARM IRQs: */ |
822 |
cpu->cd.arm.irq_asserted = 1; |
cpu->cd.arm.irq_asserted = 1; |
857 |
* cpu->pc for relative addresses. |
* cpu->pc for relative addresses. |
858 |
*/ |
*/ |
859 |
int arm_cpu_disassemble_instr(struct cpu *cpu, unsigned char *ib, |
int arm_cpu_disassemble_instr(struct cpu *cpu, unsigned char *ib, |
860 |
int running, uint64_t dumpaddr, int bintrans) |
int running, uint64_t dumpaddr) |
861 |
{ |
{ |
862 |
uint32_t iw, tmp; |
uint32_t iw, tmp; |
863 |
int main_opcode, secondary_opcode, s_bit, r16, r12, r8; |
int main_opcode, secondary_opcode, s_bit, r16, r12, r8; |
1349 |
* xxxx1100 0100nnnn ddddcccc oooommmm MCRR c,op,Rd,Rn,CRm |
* xxxx1100 0100nnnn ddddcccc oooommmm MCRR c,op,Rd,Rn,CRm |
1350 |
* xxxx1100 0101nnnn ddddcccc oooommmm MRRC c,op,Rd,Rn,CRm |
* xxxx1100 0101nnnn ddddcccc oooommmm MRRC c,op,Rd,Rn,CRm |
1351 |
*/ |
*/ |
1352 |
|
if ((iw & 0x0fe00fff) == 0x0c400000) { |
1353 |
|
debug("%s%s\t", iw & 0x100000? "mra" : "mar", |
1354 |
|
condition); |
1355 |
|
if (iw & 0x100000) |
1356 |
|
debug("%s,%s,acc0\n", |
1357 |
|
arm_regname[r12], arm_regname[r16]); |
1358 |
|
else |
1359 |
|
debug("acc0,%s,%s\n", |
1360 |
|
arm_regname[r12], arm_regname[r16]); |
1361 |
|
break; |
1362 |
|
} |
1363 |
if ((iw & 0x0fe00000) == 0x0c400000) { |
if ((iw & 0x0fe00000) == 0x0c400000) { |
1364 |
debug("%s%s\t", iw & 0x100000? "mrrc" : "mcrr", |
debug("%s%s\t", iw & 0x100000? "mrrc" : "mcrr", |
1365 |
condition); |
condition); |
1376 |
* xxxx1110 oooonnnn ddddpppp qqq0mmmm CDP |
* xxxx1110 oooonnnn ddddpppp qqq0mmmm CDP |
1377 |
* xxxx1110 oooLNNNN ddddpppp qqq1MMMM MRC/MCR |
* xxxx1110 oooLNNNN ddddpppp qqq1MMMM MRC/MCR |
1378 |
*/ |
*/ |
1379 |
|
if ((iw & 0x0ff00ff0) == 0x0e200010) { |
1380 |
|
/* Special case: mia* DSP instructions */ |
1381 |
|
switch ((iw >> 16) & 0xf) { |
1382 |
|
case 0: debug("mia"); break; |
1383 |
|
case 8: debug("miaph"); break; |
1384 |
|
case 12: debug("miaBB"); break; |
1385 |
|
case 13: debug("miaTB"); break; |
1386 |
|
case 14: debug("miaBT"); break; |
1387 |
|
case 15: debug("miaTT"); break; |
1388 |
|
default: debug("UNKNOWN mia vector instruction?"); |
1389 |
|
} |
1390 |
|
debug("%s\t", condition); |
1391 |
|
debug("acc%i,%s,%s\n", ((iw >> 5) & 7), |
1392 |
|
arm_regname[iw & 15], arm_regname[r12]); |
1393 |
|
break; |
1394 |
|
} |
1395 |
if (iw & 0x10) { |
if (iw & 0x10) { |
1396 |
debug("%s%s\t", |
debug("%s%s\t", |
1397 |
(iw & 0x00100000)? "mrc" : "mcr", condition); |
(iw & 0x00100000)? "mrc" : "mcr", condition); |