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.28 2005/10/07 10:26:03 debug Exp $ |
* $Id: cpu_arm.c,v 1.54 2006/02/09 20:02:58 debug Exp $ |
29 |
* |
* |
30 |
* ARM CPU emulation. |
* ARM CPU emulation. |
31 |
* |
* |
32 |
|
* |
33 |
* A good source of quick info on ARM instruction encoding: |
* A good source of quick info on ARM instruction encoding: |
34 |
* |
* |
35 |
* http://www.pinknoise.demon.co.uk/ARMinstrs/ARMinstrs.html |
* http://www.pinknoise.demon.co.uk/ARMinstrs/ARMinstrs.html |
|
* |
|
|
* (Most "xxxx0101..." and similar strings in this file are from that URL, |
|
|
* or from the ARM manual.) |
|
36 |
*/ |
*/ |
37 |
|
|
38 |
#include <stdio.h> |
#include <stdio.h> |
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 |
61 |
static int arm_dpi_uses_d[16] = { 1,1,1,1,1,1,1,1,0,0,0,0,1,1,1,1 }; |
static int arm_dpi_uses_d[16] = { 1,1,1,1,1,1,1,1,0,0,0,0,1,1,1,1 }; |
62 |
static int arm_dpi_uses_n[16] = { 1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,0 }; |
static int arm_dpi_uses_n[16] = { 1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,0 }; |
63 |
|
|
64 |
/* Forward reference: */ |
static int arm_exception_to_mode[N_ARM_EXCEPTIONS] = ARM_EXCEPTION_TO_MODE; |
65 |
|
|
66 |
|
/* For quick_pc_to_pointers(): */ |
67 |
void arm_pc_to_pointers(struct cpu *cpu); |
void arm_pc_to_pointers(struct cpu *cpu); |
68 |
|
#include "quick_pc_to_pointers.h" |
69 |
|
|
70 |
|
|
71 |
/* |
/* |
94 |
|
|
95 |
cpu->memory_rw = arm_memory_rw; |
cpu->memory_rw = arm_memory_rw; |
96 |
cpu->update_translation_table = arm_update_translation_table; |
cpu->update_translation_table = arm_update_translation_table; |
97 |
cpu->invalidate_translation_caches_paddr = |
cpu->invalidate_translation_caches = |
98 |
arm_invalidate_translation_caches_paddr; |
arm_invalidate_translation_caches; |
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; |
113 |
cpu->cd.arm.control |= ARM_CONTROL_S; |
cpu->cd.arm.control |= ARM_CONTROL_S; |
114 |
} else { |
} else { |
115 |
cpu->cd.arm.cpsr |= ARM_MODE_USR32; |
cpu->cd.arm.cpsr |= ARM_MODE_SVC32; |
116 |
cpu->cd.arm.control |= ARM_CONTROL_S | ARM_CONTROL_R; |
cpu->cd.arm.control |= ARM_CONTROL_R; |
117 |
} |
} |
118 |
|
|
119 |
/* Only show name and caches etc for CPU nr 0: */ |
/* Only show name and caches etc for CPU nr 0: */ |
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 |
*/ |
*/ |
160 |
store_32bit_word(cpu, cpu->cd.arm.of_emul_addr, 0xef8c64be); |
store_32bit_word(cpu, cpu->cd.arm.of_emul_addr, 0xef8c64be); |
161 |
} |
} |
162 |
|
|
163 |
|
cpu->cd.arm.flags = cpu->cd.arm.cpsr >> 28; |
164 |
|
|
165 |
return 1; |
return 1; |
166 |
} |
} |
167 |
|
|
185 |
} |
} |
186 |
|
|
187 |
cpu->cd.arm.control |= ARM_CONTROL_MMU; |
cpu->cd.arm.control |= ARM_CONTROL_MMU; |
188 |
|
cpu->translate_address = arm_translate_address_mmu; |
189 |
cpu->cd.arm.dacr |= 0x00000003; |
cpu->cd.arm.dacr |= 0x00000003; |
190 |
cpu->cd.arm.ttb = ttb_addr; |
cpu->cd.arm.ttb = ttb_addr; |
191 |
|
|
198 |
uint32_t addr = cpu->cd.arm.ttb + |
uint32_t addr = cpu->cd.arm.ttb + |
199 |
(((j << 28) + (i << 20)) >> 18); |
(((j << 28) + (i << 20)) >> 18); |
200 |
uint32_t d = (1048576*i) | 0xc02; |
uint32_t d = (1048576*i) | 0xc02; |
201 |
|
|
202 |
|
if (cpu->byte_order == EMUL_LITTLE_ENDIAN) { |
203 |
|
descr[0] = d; descr[1] = d >> 8; |
204 |
|
descr[2] = d >> 16; descr[3] = d >> 24; |
205 |
|
} else { |
206 |
|
descr[3] = d; descr[2] = d >> 8; |
207 |
|
descr[1] = d >> 16; descr[0] = d >> 24; |
208 |
|
} |
209 |
|
cpu->memory_rw(cpu, cpu->mem, addr, &descr[0], |
210 |
|
sizeof(descr), MEM_WRITE, PHYSICAL | NO_EXCEPTIONS); |
211 |
|
} |
212 |
|
} |
213 |
|
|
214 |
|
|
215 |
/* |
/* |
216 |
d = (1048576 * (i + (j==12? 10 : j)*256)) | 2; |
* arm_translation_table_set_l1(): |
217 |
*/ |
*/ |
218 |
|
void arm_translation_table_set_l1(struct cpu *cpu, uint32_t vaddr, |
219 |
|
uint32_t paddr) |
220 |
|
{ |
221 |
|
unsigned int i, j, vhigh = vaddr >> 28, phigh = paddr >> 28; |
222 |
|
|
223 |
|
for (i=0; i<256; i++) |
224 |
|
for (j=vhigh; j<=vhigh; j++) { |
225 |
|
unsigned char descr[4]; |
226 |
|
uint32_t addr = cpu->cd.arm.ttb + |
227 |
|
(((j << 28) + (i << 20)) >> 18); |
228 |
|
uint32_t d = ((phigh << 28) + 1048576*i) | 0xc02; |
229 |
|
|
230 |
|
if (cpu->byte_order == EMUL_LITTLE_ENDIAN) { |
231 |
|
descr[0] = d; descr[1] = d >> 8; |
232 |
|
descr[2] = d >> 16; descr[3] = d >> 24; |
233 |
|
} else { |
234 |
|
descr[3] = d; descr[2] = d >> 8; |
235 |
|
descr[1] = d >> 16; descr[0] = d >> 24; |
236 |
|
} |
237 |
|
cpu->memory_rw(cpu, cpu->mem, addr, &descr[0], |
238 |
|
sizeof(descr), MEM_WRITE, PHYSICAL | NO_EXCEPTIONS); |
239 |
|
} |
240 |
|
} |
241 |
|
|
242 |
|
|
243 |
|
/* |
244 |
|
* arm_translation_table_set_l1_b(): |
245 |
|
*/ |
246 |
|
void arm_translation_table_set_l1_b(struct cpu *cpu, uint32_t vaddr, |
247 |
|
uint32_t paddr) |
248 |
|
{ |
249 |
|
unsigned int i, j, vhigh = vaddr >> 24, phigh = paddr >> 24; |
250 |
|
|
251 |
|
for (i=0; i<16; i++) |
252 |
|
for (j=vhigh; j<=vhigh; j++) { |
253 |
|
unsigned char descr[4]; |
254 |
|
uint32_t addr = cpu->cd.arm.ttb + |
255 |
|
(((j << 24) + (i << 20)) >> 18); |
256 |
|
uint32_t d = ((phigh << 24) + 1048576*i) | 0xc02; |
257 |
|
|
258 |
if (cpu->byte_order == EMUL_LITTLE_ENDIAN) { |
if (cpu->byte_order == EMUL_LITTLE_ENDIAN) { |
259 |
descr[0] = d; descr[1] = d >> 8; |
descr[0] = d; descr[1] = d >> 8; |
260 |
descr[2] = d >> 16; descr[3] = d >> 24; |
descr[2] = d >> 16; descr[3] = d >> 24; |
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 |
} |
} |
347 |
int mode = cpu->cd.arm.cpsr & ARM_FLAG_MODE; |
int mode = cpu->cd.arm.cpsr & ARM_FLAG_MODE; |
348 |
int i, x = cpu->cpu_id; |
int i, x = cpu->cpu_id; |
349 |
|
|
350 |
|
cpu->cd.arm.cpsr &= 0x0fffffff; |
351 |
|
cpu->cd.arm.cpsr |= (cpu->cd.arm.flags << 28); |
352 |
|
|
353 |
if (gprs) { |
if (gprs) { |
354 |
symbol = get_symbol_name(&cpu->machine->symbol_context, |
symbol = get_symbol_name(&cpu->machine->symbol_context, |
355 |
cpu->cd.arm.r[ARM_PC], &offset); |
cpu->pc, &offset); |
356 |
debug("cpu%i: cpsr = ", x); |
debug("cpu%i: cpsr = ", x); |
357 |
debug("%s%s%s%s%s%s", |
debug("%s%s%s%s%s%s", |
358 |
(cpu->cd.arm.cpsr & ARM_FLAG_N)? "N" : "n", |
(cpu->cd.arm.cpsr & ARM_FLAG_N)? "N" : "n", |
362 |
(cpu->cd.arm.cpsr & ARM_FLAG_I)? "I" : "i", |
(cpu->cd.arm.cpsr & ARM_FLAG_I)? "I" : "i", |
363 |
(cpu->cd.arm.cpsr & ARM_FLAG_F)? "F" : "f"); |
(cpu->cd.arm.cpsr & ARM_FLAG_F)? "F" : "f"); |
364 |
if (mode < ARM_MODE_USR32) |
if (mode < ARM_MODE_USR32) |
365 |
debug(" pc = 0x%07x", |
debug(" pc = 0x%07x", (int)(cpu->pc & 0x03ffffff)); |
|
(int)(cpu->cd.arm.r[ARM_PC] & 0x03ffffff)); |
|
366 |
else |
else |
367 |
debug(" pc = 0x%08x", (int)cpu->cd.arm.r[ARM_PC]); |
debug(" pc = 0x%08x", (int)cpu->pc); |
368 |
|
|
369 |
debug(" <%s>\n", symbol != NULL? symbol : " no symbol "); |
debug(" <%s>\n", symbol != NULL? symbol : " no symbol "); |
370 |
|
|
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, |
496 |
|
|
497 |
|
|
498 |
/* |
/* |
|
* arm_cpu_show_full_statistics(): |
|
|
* |
|
|
* Show detailed statistics on opcode usage on each cpu. |
|
|
*/ |
|
|
void arm_cpu_show_full_statistics(struct machine *m) |
|
|
{ |
|
|
fatal("arm_cpu_show_full_statistics(): TODO\n"); |
|
|
} |
|
|
|
|
|
|
|
|
/* |
|
|
* arm_cpu_tlbdump(): |
|
|
* |
|
|
* Called from the debugger to dump the TLB in a readable format. |
|
|
* x is the cpu number to dump, or -1 to dump all CPUs. |
|
|
* |
|
|
* If rawflag is nonzero, then the TLB contents isn't formated nicely, |
|
|
* just dumped. |
|
|
*/ |
|
|
void arm_cpu_tlbdump(struct machine *m, int x, int rawflag) |
|
|
{ |
|
|
fatal("arm_cpu_tlbdump(): TODO\n"); |
|
|
} |
|
|
|
|
|
|
|
|
/* |
|
499 |
* arm_save_register_bank(): |
* arm_save_register_bank(): |
500 |
*/ |
*/ |
501 |
void arm_save_register_bank(struct cpu *cpu) |
void arm_save_register_bank(struct cpu *cpu) |
512 |
&cpu->cd.arm.r[8], sizeof(uint32_t) * 7); |
&cpu->cd.arm.r[8], sizeof(uint32_t) * 7); |
513 |
break; |
break; |
514 |
case ARM_MODE_IRQ32: |
case ARM_MODE_IRQ32: |
515 |
|
memcpy(cpu->cd.arm.default_r8_r14, |
516 |
|
&cpu->cd.arm.r[8], sizeof(uint32_t) * 5); |
517 |
cpu->cd.arm.irq_r13_r14[0] = cpu->cd.arm.r[13]; |
cpu->cd.arm.irq_r13_r14[0] = cpu->cd.arm.r[13]; |
518 |
cpu->cd.arm.irq_r13_r14[1] = cpu->cd.arm.r[14]; |
cpu->cd.arm.irq_r13_r14[1] = cpu->cd.arm.r[14]; |
519 |
break; |
break; |
520 |
case ARM_MODE_SVC32: |
case ARM_MODE_SVC32: |
521 |
if ((cpu->cd.arm.r[13] & 0xffff0000) == 0xffff0000) { |
memcpy(cpu->cd.arm.default_r8_r14, |
522 |
fatal("NEJ! pc=0x%08x\n", (int)cpu->pc); |
&cpu->cd.arm.r[8], sizeof(uint32_t) * 5); |
|
exit(1); |
|
|
} |
|
523 |
cpu->cd.arm.svc_r13_r14[0] = cpu->cd.arm.r[13]; |
cpu->cd.arm.svc_r13_r14[0] = cpu->cd.arm.r[13]; |
524 |
cpu->cd.arm.svc_r13_r14[1] = cpu->cd.arm.r[14]; |
cpu->cd.arm.svc_r13_r14[1] = cpu->cd.arm.r[14]; |
525 |
break; |
break; |
526 |
case ARM_MODE_ABT32: |
case ARM_MODE_ABT32: |
527 |
|
memcpy(cpu->cd.arm.default_r8_r14, |
528 |
|
&cpu->cd.arm.r[8], sizeof(uint32_t) * 5); |
529 |
cpu->cd.arm.abt_r13_r14[0] = cpu->cd.arm.r[13]; |
cpu->cd.arm.abt_r13_r14[0] = cpu->cd.arm.r[13]; |
530 |
cpu->cd.arm.abt_r13_r14[1] = cpu->cd.arm.r[14]; |
cpu->cd.arm.abt_r13_r14[1] = cpu->cd.arm.r[14]; |
531 |
break; |
break; |
532 |
case ARM_MODE_UND32: |
case ARM_MODE_UND32: |
533 |
|
memcpy(cpu->cd.arm.default_r8_r14, |
534 |
|
&cpu->cd.arm.r[8], sizeof(uint32_t) * 5); |
535 |
cpu->cd.arm.und_r13_r14[0] = cpu->cd.arm.r[13]; |
cpu->cd.arm.und_r13_r14[0] = cpu->cd.arm.r[13]; |
536 |
cpu->cd.arm.und_r13_r14[1] = cpu->cd.arm.r[14]; |
cpu->cd.arm.und_r13_r14[1] = cpu->cd.arm.r[14]; |
537 |
break; |
break; |
559 |
sizeof(uint32_t) * 7); |
sizeof(uint32_t) * 7); |
560 |
break; |
break; |
561 |
case ARM_MODE_IRQ32: |
case ARM_MODE_IRQ32: |
562 |
|
memcpy(&cpu->cd.arm.r[8], |
563 |
|
cpu->cd.arm.default_r8_r14, sizeof(uint32_t) * 5); |
564 |
cpu->cd.arm.r[13] = cpu->cd.arm.irq_r13_r14[0]; |
cpu->cd.arm.r[13] = cpu->cd.arm.irq_r13_r14[0]; |
565 |
cpu->cd.arm.r[14] = cpu->cd.arm.irq_r13_r14[1]; |
cpu->cd.arm.r[14] = cpu->cd.arm.irq_r13_r14[1]; |
566 |
break; |
break; |
567 |
case ARM_MODE_SVC32: |
case ARM_MODE_SVC32: |
568 |
|
memcpy(&cpu->cd.arm.r[8], |
569 |
|
cpu->cd.arm.default_r8_r14, sizeof(uint32_t) * 5); |
570 |
cpu->cd.arm.r[13] = cpu->cd.arm.svc_r13_r14[0]; |
cpu->cd.arm.r[13] = cpu->cd.arm.svc_r13_r14[0]; |
571 |
cpu->cd.arm.r[14] = cpu->cd.arm.svc_r13_r14[1]; |
cpu->cd.arm.r[14] = cpu->cd.arm.svc_r13_r14[1]; |
572 |
break; |
break; |
573 |
case ARM_MODE_ABT32: |
case ARM_MODE_ABT32: |
574 |
|
memcpy(&cpu->cd.arm.r[8], |
575 |
|
cpu->cd.arm.default_r8_r14, sizeof(uint32_t) * 5); |
576 |
cpu->cd.arm.r[13] = cpu->cd.arm.abt_r13_r14[0]; |
cpu->cd.arm.r[13] = cpu->cd.arm.abt_r13_r14[0]; |
577 |
cpu->cd.arm.r[14] = cpu->cd.arm.abt_r13_r14[1]; |
cpu->cd.arm.r[14] = cpu->cd.arm.abt_r13_r14[1]; |
578 |
break; |
break; |
579 |
case ARM_MODE_UND32: |
case ARM_MODE_UND32: |
580 |
|
memcpy(&cpu->cd.arm.r[8], |
581 |
|
cpu->cd.arm.default_r8_r14, sizeof(uint32_t) * 5); |
582 |
cpu->cd.arm.r[13] = cpu->cd.arm.und_r13_r14[0]; |
cpu->cd.arm.r[13] = cpu->cd.arm.und_r13_r14[0]; |
583 |
cpu->cd.arm.r[14] = cpu->cd.arm.und_r13_r14[1]; |
cpu->cd.arm.r[14] = cpu->cd.arm.und_r13_r14[1]; |
584 |
break; |
break; |
594 |
*/ |
*/ |
595 |
void arm_exception(struct cpu *cpu, int exception_nr) |
void arm_exception(struct cpu *cpu, int exception_nr) |
596 |
{ |
{ |
|
int arm_exception_to_mode[N_ARM_EXCEPTIONS] = ARM_EXCEPTION_TO_MODE; |
|
597 |
int oldmode, newmode; |
int oldmode, newmode; |
598 |
uint32_t retaddr; |
uint32_t retaddr; |
599 |
|
|
604 |
|
|
605 |
retaddr = cpu->pc; |
retaddr = cpu->pc; |
606 |
|
|
607 |
debug("[ arm_exception(): "); |
if (!quiet_mode) { |
608 |
|
debug("[ arm_exception(): "); |
609 |
|
switch (exception_nr) { |
610 |
|
case ARM_EXCEPTION_RESET: |
611 |
|
fatal("RESET: TODO"); |
612 |
|
break; |
613 |
|
case ARM_EXCEPTION_UND: |
614 |
|
debug("UNDEFINED"); |
615 |
|
break; |
616 |
|
case ARM_EXCEPTION_SWI: |
617 |
|
debug("SWI"); |
618 |
|
break; |
619 |
|
case ARM_EXCEPTION_PREF_ABT: |
620 |
|
debug("PREFETCH ABORT"); |
621 |
|
break; |
622 |
|
case ARM_EXCEPTION_IRQ: |
623 |
|
debug("IRQ"); |
624 |
|
break; |
625 |
|
case ARM_EXCEPTION_FIQ: |
626 |
|
debug("FIQ"); |
627 |
|
break; |
628 |
|
case ARM_EXCEPTION_DATA_ABT: |
629 |
|
debug("DATA ABORT, far=0x%08x fsr=0x%02x", |
630 |
|
cpu->cd.arm.far, cpu->cd.arm.fsr); |
631 |
|
break; |
632 |
|
} |
633 |
|
debug(" ]\n"); |
634 |
|
} |
635 |
|
|
636 |
switch (exception_nr) { |
switch (exception_nr) { |
637 |
case ARM_EXCEPTION_RESET: |
case ARM_EXCEPTION_RESET: |
638 |
cpu->running = 0; |
cpu->running = 0; |
639 |
fatal("RESET: TODO"); |
fatal("ARM RESET: TODO"); |
640 |
exit(1); |
exit(1); |
|
case ARM_EXCEPTION_UND: |
|
|
debug("UNDEFINED"); |
|
|
retaddr += 4; |
|
|
break; |
|
|
case ARM_EXCEPTION_SWI: |
|
|
debug("SWI"); |
|
|
retaddr += 4; |
|
|
break; |
|
|
case ARM_EXCEPTION_PREF_ABT: |
|
|
debug("PREFETCH ABORT"); |
|
|
retaddr += 4; |
|
|
break; |
|
|
case ARM_EXCEPTION_IRQ: |
|
|
debug("IRQ"); |
|
|
retaddr += 4; |
|
|
break; |
|
|
case ARM_EXCEPTION_FIQ: |
|
|
debug("FIQ"); |
|
|
retaddr += 4; |
|
|
break; |
|
641 |
case ARM_EXCEPTION_DATA_ABT: |
case ARM_EXCEPTION_DATA_ABT: |
642 |
debug("DATA ABORT, far=0x%08x fsr=0x%02x", |
retaddr += 4; |
|
cpu->cd.arm.far, cpu->cd.arm.fsr); |
|
|
retaddr += 8; |
|
643 |
break; |
break; |
644 |
} |
} |
645 |
|
|
646 |
debug(" ]\n"); |
retaddr += 4; |
647 |
|
|
648 |
arm_save_register_bank(cpu); |
arm_save_register_bank(cpu); |
649 |
|
|
650 |
|
cpu->cd.arm.cpsr &= 0x0fffffff; |
651 |
|
cpu->cd.arm.cpsr |= (cpu->cd.arm.flags << 28); |
652 |
|
|
653 |
switch (arm_exception_to_mode[exception_nr]) { |
switch (arm_exception_to_mode[exception_nr]) { |
654 |
case ARM_MODE_SVC32: |
case ARM_MODE_SVC32: |
655 |
cpu->cd.arm.spsr_svc = cpu->cd.arm.cpsr; break; |
cpu->cd.arm.spsr_svc = cpu->cd.arm.cpsr; break; |
676 |
cpu->cd.arm.cpsr &= ~ARM_FLAG_MODE; |
cpu->cd.arm.cpsr &= ~ARM_FLAG_MODE; |
677 |
cpu->cd.arm.cpsr |= arm_exception_to_mode[exception_nr]; |
cpu->cd.arm.cpsr |= arm_exception_to_mode[exception_nr]; |
678 |
|
|
679 |
|
/* |
680 |
|
* Usually, an exception should change modes (so that saved status |
681 |
|
* bits don't get lost). However, Linux on ARM seems to use floating |
682 |
|
* point instructions in the kernel (!), and it emulates those using |
683 |
|
* its own fp emulation code. This leads to a situation where we |
684 |
|
* sometimes change from SVC32 to SVC32. |
685 |
|
*/ |
686 |
newmode = cpu->cd.arm.cpsr & ARM_FLAG_MODE; |
newmode = cpu->cd.arm.cpsr & ARM_FLAG_MODE; |
687 |
if (oldmode == newmode) { |
if (oldmode == newmode && oldmode != ARM_MODE_SVC32) { |
688 |
fatal("Exception caused no mode change? TODO\n"); |
fatal("[ WARNING! Exception caused no mode change? " |
689 |
exit(1); |
"mode 0x%02x (pc=0x%x) ]\n", newmode, (int)cpu->pc); |
690 |
|
/* exit(1); */ |
691 |
} |
} |
692 |
|
|
693 |
cpu->cd.arm.cpsr |= ARM_FLAG_I; |
cpu->cd.arm.cpsr |= ARM_FLAG_I; |
698 |
/* Load the new register bank, if we switched: */ |
/* Load the new register bank, if we switched: */ |
699 |
arm_load_register_bank(cpu); |
arm_load_register_bank(cpu); |
700 |
|
|
701 |
/* Set the return address and new PC: */ |
/* |
702 |
|
* Set the return address and new PC. |
703 |
|
* |
704 |
|
* NOTE: r[ARM_PC] is also set; see cpu_arm_instr_loadstore.c for |
705 |
|
* details. (If an exception occurs during a load into the pc |
706 |
|
* register, the code in that file assumes that the r[ARM_PC] |
707 |
|
* was changed to the address of the exception handler.) |
708 |
|
*/ |
709 |
cpu->cd.arm.r[ARM_LR] = retaddr; |
cpu->cd.arm.r[ARM_LR] = retaddr; |
|
|
|
710 |
cpu->pc = cpu->cd.arm.r[ARM_PC] = exception_nr * 4 + |
cpu->pc = cpu->cd.arm.r[ARM_PC] = exception_nr * 4 + |
711 |
((cpu->cd.arm.control & ARM_CONTROL_V)? 0xffff0000 : 0); |
((cpu->cd.arm.control & ARM_CONTROL_V)? 0xffff0000 : 0); |
712 |
arm_pc_to_pointers(cpu); |
quick_pc_to_pointers(cpu); |
713 |
} |
} |
714 |
|
|
715 |
|
|
723 |
*/ |
*/ |
724 |
int arm_cpu_interrupt(struct cpu *cpu, uint64_t irq_nr) |
int arm_cpu_interrupt(struct cpu *cpu, uint64_t irq_nr) |
725 |
{ |
{ |
726 |
/* fatal("arm_cpu_interrupt(): 0x%llx\n", (int)irq_nr); */ |
/* fatal("arm_cpu_interrupt(): 0x%x\n", (int)irq_nr); */ |
727 |
if (irq_nr <= 64) { |
if (irq_nr <= 64) { |
728 |
if (cpu->machine->md_interrupt != NULL) |
if (cpu->machine->md_interrupt != NULL) |
729 |
cpu->machine->md_interrupt(cpu->machine, |
cpu->machine->md_interrupt(cpu->machine, |
730 |
cpu, irq_nr, 1); |
cpu, irq_nr, 1); |
731 |
else |
else |
732 |
fatal("arm_cpu_interrupt(): md_interrupt == NULL\n"); |
fatal("arm_cpu_interrupt(): irq_nr=%i md_interrupt ==" |
733 |
|
" NULL\n", (int)irq_nr); |
734 |
} else { |
} else { |
735 |
/* Assert ARM IRQs: */ |
/* Assert ARM IRQs: */ |
736 |
cpu->cd.arm.irq_asserted = 1; |
cpu->cd.arm.irq_asserted = 1; |
844 |
int a_bit = (iw >> 21) & 1; |
int a_bit = (iw >> 21) & 1; |
845 |
debug("%s%sl%s%s\t", u_bit? "s" : "u", |
debug("%s%sl%s%s\t", u_bit? "s" : "u", |
846 |
a_bit? "mla" : "mul", condition, s_bit? "s" : ""); |
a_bit? "mla" : "mul", condition, s_bit? "s" : ""); |
847 |
debug("%s,", arm_regname[r12]); |
debug("%s,%s,", arm_regname[r12], arm_regname[r16]); |
848 |
debug("%s,", arm_regname[r16]); |
debug("%s,%s\n", arm_regname[iw&15], arm_regname[r8]); |
849 |
debug("%s,", arm_regname[iw & 15]); |
break; |
850 |
debug("%s\n", arm_regname[r8]); |
} |
851 |
|
|
852 |
|
/* |
853 |
|
* xxxx0001 0000nnnn dddd0000 0101mmmm qadd Rd,Rm,Rn |
854 |
|
* xxxx0001 0010nnnn dddd0000 0101mmmm qsub Rd,Rm,Rn |
855 |
|
* xxxx0001 0100nnnn dddd0000 0101mmmm qdadd Rd,Rm,Rn |
856 |
|
* xxxx0001 0110nnnn dddd0000 0101mmmm qdsub Rd,Rm,Rn |
857 |
|
*/ |
858 |
|
if ((iw & 0x0f900ff0) == 0x01000050) { |
859 |
|
debug("q%s%s%s\t", iw & 0x400000? "d" : "", |
860 |
|
iw & 0x200000? "sub" : "add", condition); |
861 |
|
debug("%s,%s,%s\n", arm_regname[r12], |
862 |
|
arm_regname[iw&15], arm_regname[r16]); |
863 |
break; |
break; |
864 |
} |
} |
865 |
|
|
917 |
} |
} |
918 |
|
|
919 |
/* |
/* |
920 |
|
* xxxx0001 01101111 dddd1111 0001mmmm CLZ Rd,Rm |
921 |
|
*/ |
922 |
|
if ((iw & 0x0fff0ff0) == 0x016f0f10) { |
923 |
|
debug("clz%s\t", condition); |
924 |
|
debug("%s,%s\n", arm_regname[r12], arm_regname[iw&15]); |
925 |
|
break; |
926 |
|
} |
927 |
|
|
928 |
|
/* |
929 |
|
* xxxx0001 0000dddd nnnnssss 1yx0mmmm SMLAxy Rd,Rm,Rs,Rn |
930 |
|
* xxxx0001 0100dddd DDDDssss 1yx0mmmm SMLALxy RdL,RdH,Rm,Rs |
931 |
|
* xxxx0001 0010dddd nnnnssss 1y00mmmm SMLAWy Rd,Rm,Rs,Rn |
932 |
|
* xxxx0001 0110dddd 0000ssss 1yx0mmmm SMULxy Rd,Rm,Rs |
933 |
|
* xxxx0001 0010dddd 0000ssss 1y10mmmm SMULWy Rd,Rm,Rs |
934 |
|
*/ |
935 |
|
if ((iw & 0x0ff00090) == 0x01000080) { |
936 |
|
debug("smla%s%s%s\t", |
937 |
|
iw & 0x20? "t" : "b", iw & 0x40? "t" : "b", |
938 |
|
condition); |
939 |
|
debug("%s,%s,%s,%s\n", arm_regname[r16], |
940 |
|
arm_regname[iw&15], arm_regname[r8], |
941 |
|
arm_regname[r12]); |
942 |
|
break; |
943 |
|
} |
944 |
|
if ((iw & 0x0ff00090) == 0x01400080) { |
945 |
|
debug("smlal%s%s%s\t", |
946 |
|
iw & 0x20? "t" : "b", iw & 0x40? "t" : "b", |
947 |
|
condition); |
948 |
|
debug("%s,%s,%s,%s\n", arm_regname[r12], |
949 |
|
arm_regname[r16], arm_regname[iw&15], |
950 |
|
arm_regname[r8]); |
951 |
|
break; |
952 |
|
} |
953 |
|
if ((iw & 0x0ff000b0) == 0x01200080) { |
954 |
|
debug("smlaw%s%s\t", iw & 0x40? "t" : "b", |
955 |
|
condition); |
956 |
|
debug("%s,%s,%s,%s\n", arm_regname[r16], |
957 |
|
arm_regname[iw&15], arm_regname[r8], |
958 |
|
arm_regname[r12]); |
959 |
|
break; |
960 |
|
} |
961 |
|
if ((iw & 0x0ff0f090) == 0x01600080) { |
962 |
|
debug("smul%s%s%s\t", |
963 |
|
iw & 0x20? "t" : "b", iw & 0x40? "t" : "b", |
964 |
|
condition); |
965 |
|
debug("%s,%s,%s\n", arm_regname[r16], |
966 |
|
arm_regname[iw&15], arm_regname[r8]); |
967 |
|
break; |
968 |
|
} |
969 |
|
if ((iw & 0x0ff0f0b0) == 0x012000a0) { |
970 |
|
debug("smulw%s%s\t", iw & 0x40? "t" : "b", |
971 |
|
condition); |
972 |
|
debug("%s,%s,%s\n", arm_regname[r16], |
973 |
|
arm_regname[iw&15], arm_regname[r8]); |
974 |
|
break; |
975 |
|
} |
976 |
|
|
977 |
|
/* |
978 |
* xxxx000P U1WLnnnn ddddHHHH 1SH1LLLL load/store rd,imm(rn) |
* xxxx000P U1WLnnnn ddddHHHH 1SH1LLLL load/store rd,imm(rn) |
979 |
*/ |
*/ |
980 |
if ((iw & 0x0e000090) == 0x00000090) { |
if ((iw & 0x0e000090) == 0x00000090) { |
981 |
|
char *op = "st"; |
982 |
int imm = ((iw >> 4) & 0xf0) | (iw & 0xf); |
int imm = ((iw >> 4) & 0xf0) | (iw & 0xf); |
983 |
int regform = !(iw & 0x00400000); |
int regform = !(iw & 0x00400000); |
984 |
p_bit = main_opcode & 1; |
p_bit = main_opcode & 1; |
991 |
break; |
break; |
992 |
} |
} |
993 |
/* Semi-generic case: */ |
/* Semi-generic case: */ |
994 |
debug("%sr%s", iw & 0x00100000? "ld" : "st", |
if (iw & 0x00100000) |
995 |
condition); |
op = "ld"; |
996 |
|
if (!l_bit && (iw & 0xd0) == 0xd0) |
997 |
|
op = iw & 0x20? "st" : "ld"; |
998 |
|
debug("%sr%s", op, condition); |
999 |
if (!l_bit && (iw & 0xd0) == 0xd0) { |
if (!l_bit && (iw & 0xd0) == 0xd0) { |
1000 |
debug("d"); /* Double-register */ |
debug("d"); /* Double-register */ |
1001 |
} else { |
} else { |
1195 |
} else { |
} else { |
1196 |
tmpw[0] = addr = cpu->cd.arm.r[r12]; |
tmpw[0] = addr = cpu->cd.arm.r[r12]; |
1197 |
if (r12 == ARM_PC) |
if (r12 == ARM_PC) |
1198 |
addr += 8; |
addr = cpu->pc + 8; |
1199 |
} |
} |
1200 |
debug(": "); |
debug(": "); |
1201 |
if (b_bit) |
if (b_bit) |
1259 |
break; |
break; |
1260 |
case 0xc: /* Coprocessor */ |
case 0xc: /* Coprocessor */ |
1261 |
case 0xd: /* LDC/STC */ |
case 0xd: /* LDC/STC */ |
1262 |
|
/* |
1263 |
|
* xxxx1100 0100nnnn ddddcccc oooommmm MCRR c,op,Rd,Rn,CRm |
1264 |
|
* xxxx1100 0101nnnn ddddcccc oooommmm MRRC c,op,Rd,Rn,CRm |
1265 |
|
*/ |
1266 |
|
if ((iw & 0x0fe00fff) == 0x0c400000) { |
1267 |
|
debug("%s%s\t", iw & 0x100000? "mra" : "mar", |
1268 |
|
condition); |
1269 |
|
if (iw & 0x100000) |
1270 |
|
debug("%s,%s,acc0\n", |
1271 |
|
arm_regname[r12], arm_regname[r16]); |
1272 |
|
else |
1273 |
|
debug("acc0,%s,%s\n", |
1274 |
|
arm_regname[r12], arm_regname[r16]); |
1275 |
|
break; |
1276 |
|
} |
1277 |
|
if ((iw & 0x0fe00000) == 0x0c400000) { |
1278 |
|
debug("%s%s\t", iw & 0x100000? "mrrc" : "mcrr", |
1279 |
|
condition); |
1280 |
|
debug("%i,%i,%s,%s,cr%i\n", r8, (iw >> 4) & 15, |
1281 |
|
arm_regname[r12], arm_regname[r16], iw & 15); |
1282 |
|
break; |
1283 |
|
} |
1284 |
|
|
1285 |
/* xxxx110P UNWLnnnn DDDDpppp oooooooo LDC/STC */ |
/* xxxx110P UNWLnnnn DDDDpppp oooooooo LDC/STC */ |
1286 |
debug("TODO: coprocessor LDC/STC\n"); |
debug("TODO: coprocessor LDC/STC\n"); |
1287 |
break; |
break; |
1290 |
* xxxx1110 oooonnnn ddddpppp qqq0mmmm CDP |
* xxxx1110 oooonnnn ddddpppp qqq0mmmm CDP |
1291 |
* xxxx1110 oooLNNNN ddddpppp qqq1MMMM MRC/MCR |
* xxxx1110 oooLNNNN ddddpppp qqq1MMMM MRC/MCR |
1292 |
*/ |
*/ |
1293 |
|
if ((iw & 0x0ff00ff0) == 0x0e200010) { |
1294 |
|
/* Special case: mia* DSP instructions */ |
1295 |
|
switch ((iw >> 16) & 0xf) { |
1296 |
|
case 0: debug("mia"); break; |
1297 |
|
case 8: debug("miaph"); break; |
1298 |
|
case 12: debug("miaBB"); break; |
1299 |
|
case 13: debug("miaTB"); break; |
1300 |
|
case 14: debug("miaBT"); break; |
1301 |
|
case 15: debug("miaTT"); break; |
1302 |
|
default: debug("UNKNOWN mia vector instruction?"); |
1303 |
|
} |
1304 |
|
debug("%s\t", condition); |
1305 |
|
debug("acc%i,%s,%s\n", ((iw >> 5) & 7), |
1306 |
|
arm_regname[iw & 15], arm_regname[r12]); |
1307 |
|
break; |
1308 |
|
} |
1309 |
if (iw & 0x10) { |
if (iw & 0x10) { |
1310 |
debug("%s%s\t", |
debug("%s%s\t", |
1311 |
(iw & 0x00100000)? "mrc" : "mcr", condition); |
(iw & 0x00100000)? "mrc" : "mcr", condition); |
1362 |
cpu->cd.arm.coproc[cp_num](cpu, opcode1, opcode2, l_bit, |
cpu->cd.arm.coproc[cp_num](cpu, opcode1, opcode2, l_bit, |
1363 |
crn, crm, rd); |
crn, crm, rd); |
1364 |
else { |
else { |
1365 |
fatal("arm_mcr_mrc: pc=0x%08x, iword=0x%08x: " |
fatal("[ arm_mcr_mrc: pc=0x%08x, iword=0x%08x: " |
1366 |
"cp_num=%i\n", (int)cpu->pc, iword, cp_num); |
"cp_num=%i ]\n", (int)cpu->pc, iword, cp_num); |
1367 |
exit(1); |
arm_exception(cpu, ARM_EXCEPTION_UND); |
1368 |
|
/* exit(1); */ |
1369 |
} |
} |
1370 |
} |
} |
1371 |
|
|
1380 |
*/ |
*/ |
1381 |
void arm_cdp(struct cpu *cpu, uint32_t iword) |
void arm_cdp(struct cpu *cpu, uint32_t iword) |
1382 |
{ |
{ |
1383 |
fatal("arm_cdp: pc=0x%08x, iword=0x%08x\n", (int)cpu->pc, iword); |
fatal("[ arm_cdp: pc=0x%08x, iword=0x%08x ]\n", (int)cpu->pc, iword); |
1384 |
exit(1); |
arm_exception(cpu, ARM_EXCEPTION_UND); |
1385 |
|
/* exit(1); */ |
1386 |
} |
} |
1387 |
|
|
1388 |
|
|