--- trunk/src/cpus/cpu_arm.c 2007/10/08 16:19:23 20 +++ trunk/src/cpus/cpu_arm.c 2007/10/08 16:21:17 34 @@ -1,5 +1,5 @@ /* - * Copyright (C) 2005 Anders Gavare. All rights reserved. + * Copyright (C) 2005-2007 Anders Gavare. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: @@ -25,7 +25,7 @@ * SUCH DAMAGE. * * - * $Id: cpu_arm.c,v 1.44 2005/11/19 18:53:07 debug Exp $ + * $Id: cpu_arm.c,v 1.67 2006/12/30 13:30:53 debug Exp $ * * ARM CPU emulation. * @@ -42,9 +42,12 @@ #include "arm_cpu_types.h" #include "cpu.h" +#include "interrupt.h" #include "machine.h" #include "memory.h" #include "misc.h" +#include "of.h" +#include "settings.h" #include "symbol.h" #define DYNTRANS_32 @@ -63,7 +66,11 @@ static int arm_exception_to_mode[N_ARM_EXCEPTIONS] = ARM_EXCEPTION_TO_MODE; /* For quick_pc_to_pointers(): */ -#include "arm_quick_pc_to_pointers.h" +void arm_pc_to_pointers(struct cpu *cpu); +#include "quick_pc_to_pointers.h" + +void arm_irq_interrupt_assert(struct interrupt *interrupt); +void arm_irq_interrupt_deassert(struct interrupt *interrupt); /* @@ -75,7 +82,7 @@ int arm_cpu_new(struct cpu *cpu, struct memory *mem, struct machine *machine, int cpu_id, char *cpu_type_name) { - int any_cache = 0, i, found; + int i, found; struct arm_cpu_type_def cpu_type_defs[] = ARM_CPU_TYPE_DEFS; /* Scan the list for this cpu type: */ @@ -90,20 +97,23 @@ if (found == -1) return 0; + cpu->run_instr = arm_run_instr; cpu->memory_rw = arm_memory_rw; cpu->update_translation_table = arm_update_translation_table; cpu->invalidate_translation_caches = arm_invalidate_translation_caches; cpu->invalidate_code_translation = arm_invalidate_code_translation; - cpu->translate_address = arm_translate_address; + cpu->translate_v2p = arm_translate_v2p; - cpu->cd.arm.cpu_type = cpu_type_defs[found]; - cpu->name = cpu->cd.arm.cpu_type.name; - cpu->is_32bit = 1; + cpu->cd.arm.cpu_type = cpu_type_defs[found]; + cpu->name = cpu->cd.arm.cpu_type.name; + cpu->is_32bit = 1; + cpu->byte_order = EMUL_LITTLE_ENDIAN; cpu->cd.arm.cpsr = ARM_FLAG_I | ARM_FLAG_F; cpu->cd.arm.control = ARM_CONTROL_PROG32 | ARM_CONTROL_DATA32 | ARM_CONTROL_CACHE | ARM_CONTROL_ICACHE | ARM_CONTROL_ALIGN; + /* TODO: default auxctrl contents */ if (cpu->machine->prom_emulation) { cpu->cd.arm.cpsr |= ARM_MODE_SVC32; @@ -116,21 +126,38 @@ /* Only show name and caches etc for CPU nr 0: */ if (cpu_id == 0) { debug("%s", cpu->name); - if (cpu->cd.arm.cpu_type.icache_shift != 0) - any_cache = 1; - if (cpu->cd.arm.cpu_type.dcache_shift != 0) - any_cache = 1; - if (any_cache) { - debug(" (I+D = %i+%i KB", - (int)(1 << (cpu->cd.arm.cpu_type.icache_shift-10)), - (int)(1 << (cpu->cd.arm.cpu_type.dcache_shift-10))); - debug(")"); + if (cpu->cd.arm.cpu_type.icache_shift != 0 || + cpu->cd.arm.cpu_type.dcache_shift != 0) { + int isize = cpu->cd.arm.cpu_type.icache_shift; + int dsize = cpu->cd.arm.cpu_type.dcache_shift; + if (isize != 0) + isize = 1 << (isize - 10); + if (dsize != 0) + dsize = 1 << (dsize - 10); + debug(" (I+D = %i+%i KB)", isize, dsize); } } + /* TODO: Some of these values (iway and dway) aren't used yet: */ + cpu->cd.arm.cachetype = + (5 << ARM_CACHETYPE_CLASS_SHIFT) + | (1 << ARM_CACHETYPE_HARVARD_SHIFT) + | ((cpu->cd.arm.cpu_type.dcache_shift - 9) << + ARM_CACHETYPE_DSIZE_SHIFT) + | (5 << ARM_CACHETYPE_DASSOC_SHIFT) /* 32-way */ + | (2 << ARM_CACHETYPE_DLINE_SHIFT) /* 8 words/line */ + | ((cpu->cd.arm.cpu_type.icache_shift - 9) << + ARM_CACHETYPE_ISIZE_SHIFT) + | (5 << ARM_CACHETYPE_IASSOC_SHIFT) /* 32-way */ + | (2 << ARM_CACHETYPE_ILINE_SHIFT); /* 8 words/line */ + /* Coprocessor 15 = the system control coprocessor. */ cpu->cd.arm.coproc[15] = arm_coproc_15; + /* Coprocessor 14 for XScale: */ + if (cpu->cd.arm.cpu_type.flags & ARM_XSCALE) + cpu->cd.arm.coproc[14] = arm_coproc_xscale_14; + /* * NOTE/TODO: Ugly hack for OpenFirmware emulation: */ @@ -142,6 +169,28 @@ cpu->cd.arm.flags = cpu->cd.arm.cpsr >> 28; + CPU_SETTINGS_ADD_REGISTER64("pc", cpu->pc); + for (i=0; icd.arm.r[i]); + + /* Register the CPU's "IRQ" and "FIQ" interrupts: */ + { + struct interrupt template; + char name[50]; + snprintf(name, sizeof(name), "%s.irq", cpu->path); + + memset(&template, 0, sizeof(template)); + template.line = 0; + template.name = name; + template.extra = cpu; + template.interrupt_assert = arm_irq_interrupt_assert; + template.interrupt_deassert = arm_irq_interrupt_deassert; + interrupt_handler_register(&template); + + /* FIQ: TODO */ + } + + return 1; } @@ -165,7 +214,7 @@ } cpu->cd.arm.control |= ARM_CONTROL_MMU; - cpu->translate_address = arm_translate_address_mmu; + cpu->translate_v2p = arm_translate_v2p_mmu; cpu->cd.arm.dacr |= 0x00000003; cpu->cd.arm.ttb = ttb_addr; @@ -283,33 +332,6 @@ /* - * arm_cpu_register_match(): - */ -void arm_cpu_register_match(struct machine *m, char *name, - int writeflag, uint64_t *valuep, int *match_register) -{ - int i, cpunr = 0; - - /* CPU number: */ - - /* TODO */ - - /* Register names: */ - for (i=0; icpus[cpunr]->cd.arm.r[i] = *valuep; - if (i == ARM_PC) - m->cpus[cpunr]->pc = *valuep; - } else - *valuep = m->cpus[cpunr]->cd.arm.r[i]; - *match_register = 1; - } - } -} - - -/* * arm_cpu_register_dump(): * * Dump cpu registers in a relatively readable format. @@ -378,42 +400,42 @@ } if (m != ARM_MODE_USR32 && m != ARM_MODE_SYS32) { - debug("cpu%i: usr r8..r14 =", x); + debug("cpu%i: usr r8-14:", x); for (i=0; i<7; i++) debug(" %08x", cpu->cd.arm.default_r8_r14[i]); debug("\n"); } if (m != ARM_MODE_FIQ32) { - debug("cpu%i: fiq r8..r14 =", x); + debug("cpu%i: fiq r8-14:", x); for (i=0; i<7; i++) debug(" %08x", cpu->cd.arm.fiq_r8_r14[i]); debug("\n"); } if (m != ARM_MODE_IRQ32) { - debug("cpu%i: irq r13..r14 =", x); + debug("cpu%i: irq r13-14:", x); for (i=0; i<2; i++) debug(" %08x", cpu->cd.arm.irq_r13_r14[i]); debug("\n"); } if (m != ARM_MODE_SVC32) { - debug("cpu%i: svc r13..r14 =", x); + debug("cpu%i: svc r13-14:", x); for (i=0; i<2; i++) debug(" %08x", cpu->cd.arm.svc_r13_r14[i]); debug("\n"); } if (m != ARM_MODE_ABT32) { - debug("cpu%i: abt r13..r14 =", x); + debug("cpu%i: abt r13-14:", x); for (i=0; i<2; i++) debug(" %08x", cpu->cd.arm.abt_r13_r14[i]); debug("\n"); } if (m != ARM_MODE_UND32) { - debug("cpu%i: und r13..r14 =", x); + debug("cpu%i: und r13-14:", x); for (i=0; i<2; i++) debug(" %08x", cpu->cd.arm.und_r13_r14[i]); debug("\n"); @@ -450,6 +472,20 @@ cpu->cd.arm.control & ARM_CONTROL_V? "yes (0xffff0000)" : "no"); + /* TODO: auxctrl on which CPU types? */ + if (cpu->cd.arm.cpu_type.flags & ARM_XSCALE) { + debug("cpu%i: auxctrl = 0x%08x\n", x, + cpu->cd.arm.auxctrl); + debug("cpu%i: minidata cache attr = 0x%x\n", x, + (cpu->cd.arm.auxctrl & ARM_AUXCTRL_MD) + >> ARM_AUXCTRL_MD_SHIFT); + debug("cpu%i: page table memory attr: %i\n", x, + (cpu->cd.arm.auxctrl & ARM_AUXCTRL_P)? 1 : 0); + debug("cpu%i: write buffer coalescing: %s\n", x, + (cpu->cd.arm.auxctrl & ARM_AUXCTRL_K)? + "disabled" : "enabled"); + } + debug("cpu%i: ttb = 0x%08x dacr = 0x%08x\n", x, cpu->cd.arm.ttb, cpu->cd.arm.dacr); debug("cpu%i: fsr = 0x%08x far = 0x%08x\n", x, @@ -652,11 +688,7 @@ "mode 0x%02x (pc=0x%x) ]\n", newmode, (int)cpu->pc); /* exit(1); */ } -#if 0 -if (oldmode==0x10 && newmode ==0x17 && cpu->pc == 0x1644f0) -single_step = 1; -/* 00008554 */ -#endif + cpu->cd.arm.cpsr |= ARM_FLAG_I; if (exception_nr == ARM_EXCEPTION_RESET || exception_nr == ARM_EXCEPTION_FIQ) @@ -681,46 +713,108 @@ /* - * arm_cpu_interrupt(): + * arm_cpu_tlbdump(): * - * 0..31 are used as footbridge interrupt numbers, 32..47 = ISA, - * 64 is used as a "re-assert" signal to cpu->machine->md_interrupt(). + * 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. * - * TODO: don't hardcode to footbridge! + * If rawflag is nonzero, then the TLB contents isn't formated nicely, + * just dumped. */ -int arm_cpu_interrupt(struct cpu *cpu, uint64_t irq_nr) +void arm_cpu_tlbdump(struct machine *m, int x, int rawflag) { - /* fatal("arm_cpu_interrupt(): 0x%x\n", (int)irq_nr); */ - if (irq_nr <= 64) { - if (cpu->machine->md_interrupt != NULL) - cpu->machine->md_interrupt(cpu->machine, - cpu, irq_nr, 1); - else - fatal("arm_cpu_interrupt(): md_interrupt == NULL\n"); - } else { - /* Assert ARM IRQs: */ - cpu->cd.arm.irq_asserted = 1; - } +} - return 1; + +static void add_response_word(struct cpu *cpu, char *r, uint32_t value, + size_t maxlen) +{ + if (cpu->byte_order == EMUL_LITTLE_ENDIAN) { + value = ((value & 0xff) << 24) + + ((value & 0xff00) << 8) + + ((value & 0xff0000) >> 8) + + ((value & 0xff000000) >> 24); + } + snprintf(r + strlen(r), maxlen - strlen(r), "%08"PRIx32, value); } /* - * arm_cpu_interrupt_ack(): + * arm_cpu_gdb_stub(): + * + * Execute a "remote GDB" command. Returns a newly allocated response string + * on success, NULL on failure. */ -int arm_cpu_interrupt_ack(struct cpu *cpu, uint64_t irq_nr) +char *arm_cpu_gdb_stub(struct cpu *cpu, char *cmd) { - if (irq_nr <= 64) { - if (cpu->machine->md_interrupt != NULL) - cpu->machine->md_interrupt(cpu->machine, - cpu, irq_nr, 0); - } else { - /* De-assert ARM IRQs: */ - cpu->cd.arm.irq_asserted = 0; + if (strcmp(cmd, "g") == 0) { + /* 15 gprs, pc, 8 fprs, fps, cpsr. */ + int i; + char *r; + size_t len = 1 + 18 * sizeof(uint32_t); + r = malloc(len); + if (r == NULL) { + fprintf(stderr, "out of memory\n"); + exit(1); + } + r[0] = '\0'; + for (i=0; i<15; i++) + add_response_word(cpu, r, cpu->cd.arm.r[i], len); + add_response_word(cpu, r, cpu->pc, len); + /* TODO: fprs: */ + for (i=0; i<8; i++) + add_response_word(cpu, r, 0, len); + /* TODO: fps */ + add_response_word(cpu, r, 0, len); + add_response_word(cpu, r, cpu->cd.arm.cpsr, len); + return r; } - return 1; + if (cmd[0] == 'p') { + int regnr = strtol(cmd + 1, NULL, 16); + size_t len = 2 * sizeof(uint32_t) + 1; + char *r = malloc(len); + r[0] = '\0'; + if (regnr == ARM_PC) { + add_response_word(cpu, r, cpu->pc, len); + } else if (regnr >= 0 && regnr < ARM_PC) { + add_response_word(cpu, r, cpu->cd.arm.r[regnr], len); + } else if (regnr >= 0x10 && regnr <= 0x17) { + /* TODO: fprs */ + add_response_word(cpu, r, 0, len); + add_response_word(cpu, r, 0, len); + add_response_word(cpu, r, 0, len); + } else if (regnr == 0x18) { + /* TODO: fps */ + add_response_word(cpu, r, 0, len); + } else if (regnr == 0x19) { + add_response_word(cpu, r, cpu->cd.arm.cpsr, len); + } + return r; + } + + fatal("arm_cpu_gdb_stub(): TODO\n"); + return NULL; +} + + +/* + * arm_irq_interrupt_assert(): + */ +void arm_irq_interrupt_assert(struct interrupt *interrupt) +{ + struct cpu *cpu = (struct cpu *) interrupt->extra; + cpu->cd.arm.irq_asserted = 1; +} + + +/* + * arm_irq_interrupt_deassert(): + */ +void arm_irq_interrupt_deassert(struct interrupt *interrupt) +{ + struct cpu *cpu = (struct cpu *) interrupt->extra; + cpu->cd.arm.irq_asserted = 0; } @@ -737,7 +831,7 @@ * cpu->pc for relative addresses. */ int arm_cpu_disassemble_instr(struct cpu *cpu, unsigned char *ib, - int running, uint64_t dumpaddr, int bintrans) + int running, uint64_t dumpaddr) { uint32_t iw, tmp; int main_opcode, secondary_opcode, s_bit, r16, r12, r8; @@ -1229,6 +1323,17 @@ * xxxx1100 0100nnnn ddddcccc oooommmm MCRR c,op,Rd,Rn,CRm * xxxx1100 0101nnnn ddddcccc oooommmm MRRC c,op,Rd,Rn,CRm */ + if ((iw & 0x0fe00fff) == 0x0c400000) { + debug("%s%s\t", iw & 0x100000? "mra" : "mar", + condition); + if (iw & 0x100000) + debug("%s,%s,acc0\n", + arm_regname[r12], arm_regname[r16]); + else + debug("acc0,%s,%s\n", + arm_regname[r12], arm_regname[r16]); + break; + } if ((iw & 0x0fe00000) == 0x0c400000) { debug("%s%s\t", iw & 0x100000? "mrrc" : "mcrr", condition); @@ -1245,6 +1350,22 @@ * xxxx1110 oooonnnn ddddpppp qqq0mmmm CDP * xxxx1110 oooLNNNN ddddpppp qqq1MMMM MRC/MCR */ + if ((iw & 0x0ff00ff0) == 0x0e200010) { + /* Special case: mia* DSP instructions */ + switch ((iw >> 16) & 0xf) { + case 0: debug("mia"); break; + case 8: debug("miaph"); break; + case 12: debug("miaBB"); break; + case 13: debug("miaTB"); break; + case 14: debug("miaBT"); break; + case 15: debug("miaTT"); break; + default: debug("UNKNOWN mia vector instruction?"); + } + debug("%s\t", condition); + debug("acc%i,%s,%s\n", ((iw >> 5) & 7), + arm_regname[iw & 15], arm_regname[r12]); + break; + } if (iw & 0x10) { debug("%s%s\t", (iw & 0x00100000)? "mrc" : "mcr", condition);