9 #include "qemu-common.h"
11 static uint32_t cortexa8_cp15_c0_c1[8] =
12 { 0x1031, 0x11, 0x400, 0, 0x31100003, 0x20000000, 0x01202000, 0x11 };
14 static uint32_t cortexa8_cp15_c0_c2[8] =
15 { 0x00101111, 0x12112111, 0x21232031, 0x11112131, 0x00111142, 0, 0, 0 };
17 static uint32_t mpcore_cp15_c0_c1[8] =
18 { 0x111, 0x1, 0, 0x2, 0x01100103, 0x10020302, 0x01222000, 0 };
20 static uint32_t mpcore_cp15_c0_c2[8] =
21 { 0x00100011, 0x12002111, 0x11221011, 0x01102131, 0x141, 0, 0, 0 };
23 static uint32_t arm1136_cp15_c0_c1[8] =
24 { 0x111, 0x1, 0x2, 0x3, 0x01130003, 0x10030302, 0x01222110, 0 };
26 static uint32_t arm1136_cp15_c0_c2[8] =
27 { 0x00140011, 0x12002111, 0x11231111, 0x01102131, 0x141, 0, 0, 0 };
29 static uint32_t cpu_arm_find_by_name(const char *name);
31 static inline void set_feature(CPUARMState *env, int feature)
33 env->features |= 1u << feature;
36 static void cpu_reset_model_id(CPUARMState *env, uint32_t id)
38 env->cp15.c0_cpuid = id;
40 case ARM_CPUID_ARM926:
41 set_feature(env, ARM_FEATURE_VFP);
42 env->vfp.xregs[ARM_VFP_FPSID] = 0x41011090;
43 env->cp15.c0_cachetype = 0x1dd20d2;
44 env->cp15.c1_sys = 0x00090078;
46 case ARM_CPUID_ARM946:
47 set_feature(env, ARM_FEATURE_MPU);
48 env->cp15.c0_cachetype = 0x0f004006;
49 env->cp15.c1_sys = 0x00000078;
51 case ARM_CPUID_ARM1026:
52 set_feature(env, ARM_FEATURE_VFP);
53 set_feature(env, ARM_FEATURE_AUXCR);
54 env->vfp.xregs[ARM_VFP_FPSID] = 0x410110a0;
55 env->cp15.c0_cachetype = 0x1dd20d2;
56 env->cp15.c1_sys = 0x00090078;
58 case ARM_CPUID_ARM1136_R2:
59 case ARM_CPUID_ARM1136:
60 set_feature(env, ARM_FEATURE_V6);
61 set_feature(env, ARM_FEATURE_VFP);
62 set_feature(env, ARM_FEATURE_AUXCR);
63 env->vfp.xregs[ARM_VFP_FPSID] = 0x410120b4;
64 env->vfp.xregs[ARM_VFP_MVFR0] = 0x11111111;
65 env->vfp.xregs[ARM_VFP_MVFR1] = 0x00000000;
66 memcpy(env->cp15.c0_c1, arm1136_cp15_c0_c1, 8 * sizeof(uint32_t));
67 memcpy(env->cp15.c0_c1, arm1136_cp15_c0_c2, 8 * sizeof(uint32_t));
68 env->cp15.c0_cachetype = 0x1dd20d2;
70 case ARM_CPUID_ARM11MPCORE:
71 set_feature(env, ARM_FEATURE_V6);
72 set_feature(env, ARM_FEATURE_V6K);
73 set_feature(env, ARM_FEATURE_VFP);
74 set_feature(env, ARM_FEATURE_AUXCR);
75 env->vfp.xregs[ARM_VFP_FPSID] = 0x410120b4;
76 env->vfp.xregs[ARM_VFP_MVFR0] = 0x11111111;
77 env->vfp.xregs[ARM_VFP_MVFR1] = 0x00000000;
78 memcpy(env->cp15.c0_c1, mpcore_cp15_c0_c1, 8 * sizeof(uint32_t));
79 memcpy(env->cp15.c0_c1, mpcore_cp15_c0_c2, 8 * sizeof(uint32_t));
80 env->cp15.c0_cachetype = 0x1dd20d2;
82 case ARM_CPUID_CORTEXA8:
83 set_feature(env, ARM_FEATURE_V6);
84 set_feature(env, ARM_FEATURE_V6K);
85 set_feature(env, ARM_FEATURE_V7);
86 set_feature(env, ARM_FEATURE_AUXCR);
87 set_feature(env, ARM_FEATURE_THUMB2);
88 set_feature(env, ARM_FEATURE_VFP);
89 set_feature(env, ARM_FEATURE_VFP3);
90 set_feature(env, ARM_FEATURE_NEON);
91 env->vfp.xregs[ARM_VFP_FPSID] = 0x410330c0;
92 env->vfp.xregs[ARM_VFP_MVFR0] = 0x11110222;
93 env->vfp.xregs[ARM_VFP_MVFR1] = 0x00011100;
94 memcpy(env->cp15.c0_c1, cortexa8_cp15_c0_c1, 8 * sizeof(uint32_t));
95 memcpy(env->cp15.c0_c1, cortexa8_cp15_c0_c2, 8 * sizeof(uint32_t));
96 env->cp15.c0_cachetype = 0x1dd20d2;
98 case ARM_CPUID_CORTEXM3:
99 set_feature(env, ARM_FEATURE_V6);
100 set_feature(env, ARM_FEATURE_THUMB2);
101 set_feature(env, ARM_FEATURE_V7);
102 set_feature(env, ARM_FEATURE_M);
103 set_feature(env, ARM_FEATURE_DIV);
105 case ARM_CPUID_ANY: /* For userspace emulation. */
106 set_feature(env, ARM_FEATURE_V6);
107 set_feature(env, ARM_FEATURE_V6K);
108 set_feature(env, ARM_FEATURE_V7);
109 set_feature(env, ARM_FEATURE_THUMB2);
110 set_feature(env, ARM_FEATURE_VFP);
111 set_feature(env, ARM_FEATURE_VFP3);
112 set_feature(env, ARM_FEATURE_NEON);
113 set_feature(env, ARM_FEATURE_DIV);
115 case ARM_CPUID_TI915T:
116 case ARM_CPUID_TI925T:
117 set_feature(env, ARM_FEATURE_OMAPCP);
118 env->cp15.c0_cpuid = ARM_CPUID_TI925T; /* Depends on wiring. */
119 env->cp15.c0_cachetype = 0x5109149;
120 env->cp15.c1_sys = 0x00000070;
121 env->cp15.c15_i_max = 0x000;
122 env->cp15.c15_i_min = 0xff0;
124 case ARM_CPUID_PXA250:
125 case ARM_CPUID_PXA255:
126 case ARM_CPUID_PXA260:
127 case ARM_CPUID_PXA261:
128 case ARM_CPUID_PXA262:
129 set_feature(env, ARM_FEATURE_XSCALE);
130 /* JTAG_ID is ((id << 28) | 0x09265013) */
131 env->cp15.c0_cachetype = 0xd172172;
132 env->cp15.c1_sys = 0x00000078;
134 case ARM_CPUID_PXA270_A0:
135 case ARM_CPUID_PXA270_A1:
136 case ARM_CPUID_PXA270_B0:
137 case ARM_CPUID_PXA270_B1:
138 case ARM_CPUID_PXA270_C0:
139 case ARM_CPUID_PXA270_C5:
140 set_feature(env, ARM_FEATURE_XSCALE);
141 /* JTAG_ID is ((id << 28) | 0x09265013) */
142 set_feature(env, ARM_FEATURE_IWMMXT);
143 env->iwmmxt.cregs[ARM_IWMMXT_wCID] = 0x69051000 | 'Q';
144 env->cp15.c0_cachetype = 0xd172172;
145 env->cp15.c1_sys = 0x00000078;
148 cpu_abort(env, "Bad CPU ID: %x\n", id);
153 void cpu_reset(CPUARMState *env)
156 id = env->cp15.c0_cpuid;
157 memset(env, 0, offsetof(CPUARMState, breakpoints));
159 cpu_reset_model_id(env, id);
160 #if defined (CONFIG_USER_ONLY)
161 env->uncached_cpsr = ARM_CPU_MODE_USR;
162 env->vfp.xregs[ARM_VFP_FPEXC] = 1 << 30;
164 /* SVC mode with interrupts disabled. */
165 env->uncached_cpsr = ARM_CPU_MODE_SVC | CPSR_A | CPSR_F | CPSR_I;
166 /* On ARMv7-M the CPSR_I is the value of the PRIMASK register, and is
169 env->uncached_cpsr &= ~CPSR_I;
170 env->vfp.xregs[ARM_VFP_FPEXC] = 0;
176 CPUARMState *cpu_arm_init(const char *cpu_model)
180 static int inited = 0;
182 id = cpu_arm_find_by_name(cpu_model);
185 env = qemu_mallocz(sizeof(CPUARMState));
191 arm_translate_init();
194 env->cpu_model_str = cpu_model;
195 env->cp15.c0_cpuid = id;
205 static const struct arm_cpu_t arm_cpu_names[] = {
206 { ARM_CPUID_ARM926, "arm926"},
207 { ARM_CPUID_ARM946, "arm946"},
208 { ARM_CPUID_ARM1026, "arm1026"},
209 { ARM_CPUID_ARM1136, "arm1136"},
210 { ARM_CPUID_ARM1136_R2, "arm1136-r2"},
211 { ARM_CPUID_ARM11MPCORE, "arm11mpcore"},
212 { ARM_CPUID_CORTEXM3, "cortex-m3"},
213 { ARM_CPUID_CORTEXA8, "cortex-a8"},
214 { ARM_CPUID_TI925T, "ti925t" },
215 { ARM_CPUID_PXA250, "pxa250" },
216 { ARM_CPUID_PXA255, "pxa255" },
217 { ARM_CPUID_PXA260, "pxa260" },
218 { ARM_CPUID_PXA261, "pxa261" },
219 { ARM_CPUID_PXA262, "pxa262" },
220 { ARM_CPUID_PXA270, "pxa270" },
221 { ARM_CPUID_PXA270_A0, "pxa270-a0" },
222 { ARM_CPUID_PXA270_A1, "pxa270-a1" },
223 { ARM_CPUID_PXA270_B0, "pxa270-b0" },
224 { ARM_CPUID_PXA270_B1, "pxa270-b1" },
225 { ARM_CPUID_PXA270_C0, "pxa270-c0" },
226 { ARM_CPUID_PXA270_C5, "pxa270-c5" },
227 { ARM_CPUID_ANY, "any"},
231 void arm_cpu_list(FILE *f, int (*cpu_fprintf)(FILE *f, const char *fmt, ...))
235 (*cpu_fprintf)(f, "Available CPUs:\n");
236 for (i = 0; arm_cpu_names[i].name; i++) {
237 (*cpu_fprintf)(f, " %s\n", arm_cpu_names[i].name);
241 /* return 0 if not found */
242 static uint32_t cpu_arm_find_by_name(const char *name)
248 for (i = 0; arm_cpu_names[i].name; i++) {
249 if (strcmp(name, arm_cpu_names[i].name) == 0) {
250 id = arm_cpu_names[i].id;
257 void cpu_arm_close(CPUARMState *env)
262 uint32_t cpsr_read(CPUARMState *env)
266 return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) |
267 (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27)
268 | (env->thumb << 5) | ((env->condexec_bits & 3) << 25)
269 | ((env->condexec_bits & 0xfc) << 8)
273 void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask)
275 if (mask & CPSR_NZCV) {
276 env->ZF = (~val) & CPSR_Z;
278 env->CF = (val >> 29) & 1;
279 env->VF = (val << 3) & 0x80000000;
282 env->QF = ((val & CPSR_Q) != 0);
284 env->thumb = ((val & CPSR_T) != 0);
285 if (mask & CPSR_IT_0_1) {
286 env->condexec_bits &= ~3;
287 env->condexec_bits |= (val >> 25) & 3;
289 if (mask & CPSR_IT_2_7) {
290 env->condexec_bits &= 3;
291 env->condexec_bits |= (val >> 8) & 0xfc;
293 if (mask & CPSR_GE) {
294 env->GE = (val >> 16) & 0xf;
297 if ((env->uncached_cpsr ^ val) & mask & CPSR_M) {
298 switch_mode(env, val & CPSR_M);
300 mask &= ~CACHED_CPSR_BITS;
301 env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask);
304 /* Sign/zero extend */
305 uint32_t HELPER(sxtb16)(uint32_t x)
308 res = (uint16_t)(int8_t)x;
309 res |= (uint32_t)(int8_t)(x >> 16) << 16;
313 uint32_t HELPER(uxtb16)(uint32_t x)
316 res = (uint16_t)(uint8_t)x;
317 res |= (uint32_t)(uint8_t)(x >> 16) << 16;
321 uint32_t HELPER(clz)(uint32_t x)
324 for (count = 32; x; count--)
329 int32_t HELPER(sdiv)(int32_t num, int32_t den)
336 uint32_t HELPER(udiv)(uint32_t num, uint32_t den)
343 uint32_t HELPER(rbit)(uint32_t x)
345 x = ((x & 0xff000000) >> 24)
346 | ((x & 0x00ff0000) >> 8)
347 | ((x & 0x0000ff00) << 8)
348 | ((x & 0x000000ff) << 24);
349 x = ((x & 0xf0f0f0f0) >> 4)
350 | ((x & 0x0f0f0f0f) << 4);
351 x = ((x & 0x88888888) >> 3)
352 | ((x & 0x44444444) >> 1)
353 | ((x & 0x22222222) << 1)
354 | ((x & 0x11111111) << 3);
358 uint32_t HELPER(abs)(uint32_t x)
360 return ((int32_t)x < 0) ? -x : x;
363 #if defined(CONFIG_USER_ONLY)
365 void do_interrupt (CPUState *env)
367 env->exception_index = -1;
370 /* Structure used to record exclusive memory locations. */
371 typedef struct mmon_state {
372 struct mmon_state *next;
373 CPUARMState *cpu_env;
377 /* Chain of current locks. */
378 static mmon_state* mmon_head = NULL;
380 int cpu_arm_handle_mmu_fault (CPUState *env, target_ulong address, int rw,
381 int mmu_idx, int is_softmmu)
384 env->exception_index = EXCP_PREFETCH_ABORT;
385 env->cp15.c6_insn = address;
387 env->exception_index = EXCP_DATA_ABORT;
388 env->cp15.c6_data = address;
393 static void allocate_mmon_state(CPUState *env)
395 env->mmon_entry = malloc(sizeof (mmon_state));
396 if (!env->mmon_entry)
398 memset (env->mmon_entry, 0, sizeof (mmon_state));
399 env->mmon_entry->cpu_env = env;
400 mmon_head = env->mmon_entry;
403 /* Flush any monitor locks for the specified address. */
404 static void flush_mmon(uint32_t addr)
408 for (mon = mmon_head; mon; mon = mon->next)
410 if (mon->addr != addr)
418 /* Mark an address for exclusive access. */
419 void HELPER(mark_exclusive)(CPUState *env, uint32_t addr)
421 if (!env->mmon_entry)
422 allocate_mmon_state(env);
423 /* Clear any previous locks. */
425 env->mmon_entry->addr = addr;
428 /* Test if an exclusive address is still exclusive. Returns zero
429 if the address is still exclusive. */
430 uint32_t HELPER(test_exclusive)(CPUState *env, uint32_t addr)
434 if (!env->mmon_entry)
436 if (env->mmon_entry->addr == addr)
444 void HELPER(clrex)(CPUState *env)
446 if (!(env->mmon_entry && env->mmon_entry->addr))
448 flush_mmon(env->mmon_entry->addr);
451 target_phys_addr_t cpu_get_phys_page_debug(CPUState *env, target_ulong addr)
456 /* These should probably raise undefined insn exceptions. */
457 void HELPER(set_cp)(CPUState *env, uint32_t insn, uint32_t val)
459 int op1 = (insn >> 8) & 0xf;
460 cpu_abort(env, "cp%i insn %08x\n", op1, insn);
464 uint32_t HELPER(get_cp)(CPUState *env, uint32_t insn)
466 int op1 = (insn >> 8) & 0xf;
467 cpu_abort(env, "cp%i insn %08x\n", op1, insn);
471 void HELPER(set_cp15)(CPUState *env, uint32_t insn, uint32_t val)
473 cpu_abort(env, "cp15 insn %08x\n", insn);
476 uint32_t HELPER(get_cp15)(CPUState *env, uint32_t insn)
478 cpu_abort(env, "cp15 insn %08x\n", insn);
482 /* These should probably raise undefined insn exceptions. */
483 void HELPER(v7m_msr)(CPUState *env, uint32_t reg, uint32_t val)
485 cpu_abort(env, "v7m_mrs %d\n", reg);
488 uint32_t HELPER(v7m_mrs)(CPUState *env, uint32_t reg)
490 cpu_abort(env, "v7m_mrs %d\n", reg);
494 void switch_mode(CPUState *env, int mode)
496 if (mode != ARM_CPU_MODE_USR)
497 cpu_abort(env, "Tried to switch out of user mode\n");
500 void HELPER(set_r13_banked)(CPUState *env, uint32_t mode, uint32_t val)
502 cpu_abort(env, "banked r13 write\n");
505 uint32_t HELPER(get_r13_banked)(CPUState *env, uint32_t mode)
507 cpu_abort(env, "banked r13 read\n");
513 extern int semihosting_enabled;
515 /* Map CPU modes onto saved register banks. */
516 static inline int bank_number (int mode)
519 case ARM_CPU_MODE_USR:
520 case ARM_CPU_MODE_SYS:
522 case ARM_CPU_MODE_SVC:
524 case ARM_CPU_MODE_ABT:
526 case ARM_CPU_MODE_UND:
528 case ARM_CPU_MODE_IRQ:
530 case ARM_CPU_MODE_FIQ:
533 cpu_abort(cpu_single_env, "Bad mode %x\n", mode);
537 void switch_mode(CPUState *env, int mode)
542 old_mode = env->uncached_cpsr & CPSR_M;
543 if (mode == old_mode)
546 if (old_mode == ARM_CPU_MODE_FIQ) {
547 memcpy (env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t));
548 memcpy (env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t));
549 } else if (mode == ARM_CPU_MODE_FIQ) {
550 memcpy (env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t));
551 memcpy (env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t));
554 i = bank_number(old_mode);
555 env->banked_r13[i] = env->regs[13];
556 env->banked_r14[i] = env->regs[14];
557 env->banked_spsr[i] = env->spsr;
559 i = bank_number(mode);
560 env->regs[13] = env->banked_r13[i];
561 env->regs[14] = env->banked_r14[i];
562 env->spsr = env->banked_spsr[i];
565 static void v7m_push(CPUARMState *env, uint32_t val)
568 stl_phys(env->regs[13], val);
571 static uint32_t v7m_pop(CPUARMState *env)
574 val = ldl_phys(env->regs[13]);
579 /* Switch to V7M main or process stack pointer. */
580 static void switch_v7m_sp(CPUARMState *env, int process)
583 if (env->v7m.current_sp != process) {
584 tmp = env->v7m.other_sp;
585 env->v7m.other_sp = env->regs[13];
587 env->v7m.current_sp = process;
591 static void do_v7m_exception_exit(CPUARMState *env)
596 type = env->regs[15];
597 if (env->v7m.exception != 0)
598 armv7m_nvic_complete_irq(env->v7m.nvic, env->v7m.exception);
600 /* Switch to the target stack. */
601 switch_v7m_sp(env, (type & 4) != 0);
603 env->regs[0] = v7m_pop(env);
604 env->regs[1] = v7m_pop(env);
605 env->regs[2] = v7m_pop(env);
606 env->regs[3] = v7m_pop(env);
607 env->regs[12] = v7m_pop(env);
608 env->regs[14] = v7m_pop(env);
609 env->regs[15] = v7m_pop(env);
611 xpsr_write(env, xpsr, 0xfffffdff);
612 /* Undo stack alignment. */
615 /* ??? The exception return type specifies Thread/Handler mode. However
616 this is also implied by the xPSR value. Not sure what to do
617 if there is a mismatch. */
618 /* ??? Likewise for mismatches between the CONTROL register and the stack
622 void do_interrupt_v7m(CPUARMState *env)
624 uint32_t xpsr = xpsr_read(env);
629 if (env->v7m.current_sp)
631 if (env->v7m.exception == 0)
634 /* For exceptions we just mark as pending on the NVIC, and let that
636 /* TODO: Need to escalate if the current priority is higher than the
637 one we're raising. */
638 switch (env->exception_index) {
640 armv7m_nvic_set_pending(env->v7m.nvic, ARMV7M_EXCP_USAGE);
644 armv7m_nvic_set_pending(env->v7m.nvic, ARMV7M_EXCP_SVC);
646 case EXCP_PREFETCH_ABORT:
647 case EXCP_DATA_ABORT:
648 armv7m_nvic_set_pending(env->v7m.nvic, ARMV7M_EXCP_MEM);
651 if (semihosting_enabled) {
653 nr = lduw_code(env->regs[15]) & 0xff;
656 env->regs[0] = do_arm_semihosting(env);
660 armv7m_nvic_set_pending(env->v7m.nvic, ARMV7M_EXCP_DEBUG);
663 env->v7m.exception = armv7m_nvic_acknowledge_irq(env->v7m.nvic);
665 case EXCP_EXCEPTION_EXIT:
666 do_v7m_exception_exit(env);
669 cpu_abort(env, "Unhandled exception 0x%x\n", env->exception_index);
670 return; /* Never happens. Keep compiler happy. */
673 /* Align stack pointer. */
674 /* ??? Should only do this if Configuration Control Register
675 STACKALIGN bit is set. */
676 if (env->regs[13] & 4) {
680 /* Switch to the handler mode. */
682 v7m_push(env, env->regs[15]);
683 v7m_push(env, env->regs[14]);
684 v7m_push(env, env->regs[12]);
685 v7m_push(env, env->regs[3]);
686 v7m_push(env, env->regs[2]);
687 v7m_push(env, env->regs[1]);
688 v7m_push(env, env->regs[0]);
689 switch_v7m_sp(env, 0);
690 env->uncached_cpsr &= ~CPSR_IT;
692 addr = ldl_phys(env->v7m.vecbase + env->v7m.exception * 4);
693 env->regs[15] = addr & 0xfffffffe;
694 env->thumb = addr & 1;
697 /* Handle a CPU exception. */
698 void do_interrupt(CPUARMState *env)
706 do_interrupt_v7m(env);
709 /* TODO: Vectored interrupt controller. */
710 switch (env->exception_index) {
712 new_mode = ARM_CPU_MODE_UND;
721 if (semihosting_enabled) {
722 /* Check for semihosting interrupt. */
724 mask = lduw_code(env->regs[15] - 2) & 0xff;
726 mask = ldl_code(env->regs[15] - 4) & 0xffffff;
728 /* Only intercept calls from privileged modes, to provide some
729 semblance of security. */
730 if (((mask == 0x123456 && !env->thumb)
731 || (mask == 0xab && env->thumb))
732 && (env->uncached_cpsr & CPSR_M) != ARM_CPU_MODE_USR) {
733 env->regs[0] = do_arm_semihosting(env);
737 new_mode = ARM_CPU_MODE_SVC;
740 /* The PC already points to the next instruction. */
744 /* See if this is a semihosting syscall. */
745 if (env->thumb && semihosting_enabled) {
746 mask = lduw_code(env->regs[15]) & 0xff;
748 && (env->uncached_cpsr & CPSR_M) != ARM_CPU_MODE_USR) {
750 env->regs[0] = do_arm_semihosting(env);
754 /* Fall through to prefetch abort. */
755 case EXCP_PREFETCH_ABORT:
756 new_mode = ARM_CPU_MODE_ABT;
758 mask = CPSR_A | CPSR_I;
761 case EXCP_DATA_ABORT:
762 new_mode = ARM_CPU_MODE_ABT;
764 mask = CPSR_A | CPSR_I;
768 new_mode = ARM_CPU_MODE_IRQ;
770 /* Disable IRQ and imprecise data aborts. */
771 mask = CPSR_A | CPSR_I;
775 new_mode = ARM_CPU_MODE_FIQ;
777 /* Disable FIQ, IRQ and imprecise data aborts. */
778 mask = CPSR_A | CPSR_I | CPSR_F;
782 cpu_abort(env, "Unhandled exception 0x%x\n", env->exception_index);
783 return; /* Never happens. Keep compiler happy. */
786 if (env->cp15.c1_sys & (1 << 13)) {
789 switch_mode (env, new_mode);
790 env->spsr = cpsr_read(env);
792 env->condexec_bits = 0;
793 /* Switch to the new mode, and switch to Arm mode. */
794 /* ??? Thumb interrupt handlers not implemented. */
795 env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode;
796 env->uncached_cpsr |= mask;
798 env->regs[14] = env->regs[15] + offset;
799 env->regs[15] = addr;
800 env->interrupt_request |= CPU_INTERRUPT_EXITTB;
803 /* Check section/page access permissions.
804 Returns the page protection flags, or zero if the access is not
806 static inline int check_ap(CPUState *env, int ap, int domain, int access_type,
812 return PAGE_READ | PAGE_WRITE;
814 if (access_type == 1)
821 if (access_type == 1)
823 switch ((env->cp15.c1_sys >> 8) & 3) {
825 return is_user ? 0 : PAGE_READ;
832 return is_user ? 0 : PAGE_READ | PAGE_WRITE;
837 return PAGE_READ | PAGE_WRITE;
839 return PAGE_READ | PAGE_WRITE;
840 case 4: case 7: /* Reserved. */
843 return is_user ? 0 : prot_ro;
851 static int get_phys_addr_v5(CPUState *env, uint32_t address, int access_type,
852 int is_user, uint32_t *phys_ptr, int *prot)
862 /* Pagetable walk. */
863 /* Lookup l1 descriptor. */
864 if (address & env->cp15.c2_mask)
865 table = env->cp15.c2_base1;
867 table = env->cp15.c2_base0;
868 table = (table & 0xffffc000) | ((address >> 18) & 0x3ffc);
869 desc = ldl_phys(table);
871 domain = (env->cp15.c3 >> ((desc >> 4) & 0x1e)) & 3;
873 /* Section translation fault. */
877 if (domain == 0 || domain == 2) {
879 code = 9; /* Section domain fault. */
881 code = 11; /* Page domain fault. */
886 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
887 ap = (desc >> 10) & 3;
890 /* Lookup l2 entry. */
892 /* Coarse pagetable. */
893 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
895 /* Fine pagetable. */
896 table = (desc & 0xfffff000) | ((address >> 8) & 0xffc);
898 desc = ldl_phys(table);
900 case 0: /* Page translation fault. */
903 case 1: /* 64k page. */
904 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
905 ap = (desc >> (4 + ((address >> 13) & 6))) & 3;
907 case 2: /* 4k page. */
908 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
909 ap = (desc >> (4 + ((address >> 13) & 6))) & 3;
911 case 3: /* 1k page. */
913 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
914 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
916 /* Page translation fault. */
921 phys_addr = (desc & 0xfffffc00) | (address & 0x3ff);
923 ap = (desc >> 4) & 3;
926 /* Never happens, but compiler isn't smart enough to tell. */
931 *prot = check_ap(env, ap, domain, access_type, is_user);
933 /* Access permission fault. */
936 *phys_ptr = phys_addr;
939 return code | (domain << 4);
942 static int get_phys_addr_v6(CPUState *env, uint32_t address, int access_type,
943 int is_user, uint32_t *phys_ptr, int *prot)
954 /* Pagetable walk. */
955 /* Lookup l1 descriptor. */
956 if (address & env->cp15.c2_mask)
957 table = env->cp15.c2_base1;
959 table = env->cp15.c2_base0;
960 table = (table & 0xffffc000) | ((address >> 18) & 0x3ffc);
961 desc = ldl_phys(table);
964 /* Section translation fault. */
968 } else if (type == 2 && (desc & (1 << 18))) {
972 /* Section or page. */
973 domain = (desc >> 4) & 0x1e;
975 domain = (env->cp15.c3 >> domain) & 3;
976 if (domain == 0 || domain == 2) {
978 code = 9; /* Section domain fault. */
980 code = 11; /* Page domain fault. */
984 if (desc & (1 << 18)) {
986 phys_addr = (desc & 0xff000000) | (address & 0x00ffffff);
989 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
991 ap = ((desc >> 10) & 3) | ((desc >> 13) & 4);
992 xn = desc & (1 << 4);
995 /* Lookup l2 entry. */
996 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
997 desc = ldl_phys(table);
998 ap = ((desc >> 4) & 3) | ((desc >> 7) & 4);
1000 case 0: /* Page translation fault. */
1003 case 1: /* 64k page. */
1004 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
1005 xn = desc & (1 << 15);
1007 case 2: case 3: /* 4k page. */
1008 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
1012 /* Never happens, but compiler isn't smart enough to tell. */
1017 if (xn && access_type == 2)
1020 *prot = check_ap(env, ap, domain, access_type, is_user);
1022 /* Access permission fault. */
1025 *phys_ptr = phys_addr;
1028 return code | (domain << 4);
1031 static int get_phys_addr_mpu(CPUState *env, uint32_t address, int access_type,
1032 int is_user, uint32_t *phys_ptr, int *prot)
1038 *phys_ptr = address;
1039 for (n = 7; n >= 0; n--) {
1040 base = env->cp15.c6_region[n];
1041 if ((base & 1) == 0)
1043 mask = 1 << ((base >> 1) & 0x1f);
1044 /* Keep this shift separate from the above to avoid an
1045 (undefined) << 32. */
1046 mask = (mask << 1) - 1;
1047 if (((base ^ address) & ~mask) == 0)
1053 if (access_type == 2) {
1054 mask = env->cp15.c5_insn;
1056 mask = env->cp15.c5_data;
1058 mask = (mask >> (n * 4)) & 0xf;
1065 *prot = PAGE_READ | PAGE_WRITE;
1070 *prot |= PAGE_WRITE;
1073 *prot = PAGE_READ | PAGE_WRITE;
1084 /* Bad permission. */
1090 static inline int get_phys_addr(CPUState *env, uint32_t address,
1091 int access_type, int is_user,
1092 uint32_t *phys_ptr, int *prot)
1094 /* Fast Context Switch Extension. */
1095 if (address < 0x02000000)
1096 address += env->cp15.c13_fcse;
1098 if ((env->cp15.c1_sys & 1) == 0) {
1099 /* MMU/MPU disabled. */
1100 *phys_ptr = address;
1101 *prot = PAGE_READ | PAGE_WRITE;
1103 } else if (arm_feature(env, ARM_FEATURE_MPU)) {
1104 return get_phys_addr_mpu(env, address, access_type, is_user, phys_ptr,
1106 } else if (env->cp15.c1_sys & (1 << 23)) {
1107 return get_phys_addr_v6(env, address, access_type, is_user, phys_ptr,
1110 return get_phys_addr_v5(env, address, access_type, is_user, phys_ptr,
1115 int cpu_arm_handle_mmu_fault (CPUState *env, target_ulong address,
1116 int access_type, int mmu_idx, int is_softmmu)
1122 is_user = mmu_idx == MMU_USER_IDX;
1123 ret = get_phys_addr(env, address, access_type, is_user, &phys_addr, &prot);
1125 /* Map a single [sub]page. */
1126 phys_addr &= ~(uint32_t)0x3ff;
1127 address &= ~(uint32_t)0x3ff;
1128 return tlb_set_page (env, address, phys_addr, prot, mmu_idx,
1132 if (access_type == 2) {
1133 env->cp15.c5_insn = ret;
1134 env->cp15.c6_insn = address;
1135 env->exception_index = EXCP_PREFETCH_ABORT;
1137 env->cp15.c5_data = ret;
1138 if (access_type == 1 && arm_feature(env, ARM_FEATURE_V6))
1139 env->cp15.c5_data |= (1 << 11);
1140 env->cp15.c6_data = address;
1141 env->exception_index = EXCP_DATA_ABORT;
1146 target_phys_addr_t cpu_get_phys_page_debug(CPUState *env, target_ulong addr)
1152 ret = get_phys_addr(env, addr, 0, 0, &phys_addr, &prot);
1160 /* Not really implemented. Need to figure out a sane way of doing this.
1161 Maybe add generic watchpoint support and use that. */
1163 void HELPER(mark_exclusive)(CPUState *env, uint32_t addr)
1165 env->mmon_addr = addr;
1168 uint32_t HELPER(test_exclusive)(CPUState *env, uint32_t addr)
1170 return (env->mmon_addr != addr);
1173 void HELPER(clrex)(CPUState *env)
1175 env->mmon_addr = -1;
1178 void HELPER(set_cp)(CPUState *env, uint32_t insn, uint32_t val)
1180 int cp_num = (insn >> 8) & 0xf;
1181 int cp_info = (insn >> 5) & 7;
1182 int src = (insn >> 16) & 0xf;
1183 int operand = insn & 0xf;
1185 if (env->cp[cp_num].cp_write)
1186 env->cp[cp_num].cp_write(env->cp[cp_num].opaque,
1187 cp_info, src, operand, val);
1190 uint32_t HELPER(get_cp)(CPUState *env, uint32_t insn)
1192 int cp_num = (insn >> 8) & 0xf;
1193 int cp_info = (insn >> 5) & 7;
1194 int dest = (insn >> 16) & 0xf;
1195 int operand = insn & 0xf;
1197 if (env->cp[cp_num].cp_read)
1198 return env->cp[cp_num].cp_read(env->cp[cp_num].opaque,
1199 cp_info, dest, operand);
1203 /* Return basic MPU access permission bits. */
1204 static uint32_t simple_mpu_ap_bits(uint32_t val)
1211 for (i = 0; i < 16; i += 2) {
1212 ret |= (val >> i) & mask;
1218 /* Pad basic MPU access permission bits to extended format. */
1219 static uint32_t extended_mpu_ap_bits(uint32_t val)
1226 for (i = 0; i < 16; i += 2) {
1227 ret |= (val & mask) << i;
1233 void HELPER(set_cp15)(CPUState *env, uint32_t insn, uint32_t val)
1239 op1 = (insn >> 21) & 7;
1240 op2 = (insn >> 5) & 7;
1242 switch ((insn >> 16) & 0xf) {
1244 if (((insn >> 21) & 7) == 2) {
1245 /* ??? Select cache level. Ignore. */
1249 if (arm_feature(env, ARM_FEATURE_XSCALE))
1251 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1254 case 1: /* System configuration. */
1255 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1259 if (!arm_feature(env, ARM_FEATURE_XSCALE) || crm == 0)
1260 env->cp15.c1_sys = val;
1261 /* ??? Lots of these bits are not implemented. */
1262 /* This may enable/disable the MMU, so do a TLB flush. */
1265 case 1: /* Auxiliary cotrol register. */
1266 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
1267 env->cp15.c1_xscaleauxcr = val;
1270 /* Not implemented. */
1273 if (arm_feature(env, ARM_FEATURE_XSCALE))
1275 env->cp15.c1_coproc = val;
1276 /* ??? Is this safe when called from within a TB? */
1283 case 2: /* MMU Page table control / MPU cache control. */
1284 if (arm_feature(env, ARM_FEATURE_MPU)) {
1287 env->cp15.c2_data = val;
1290 env->cp15.c2_insn = val;
1298 env->cp15.c2_base0 = val;
1301 env->cp15.c2_base1 = val;
1304 env->cp15.c2_mask = ~(((uint32_t)0xffffffffu) >> val);
1311 case 3: /* MMU Domain access control / MPU write buffer control. */
1313 tlb_flush(env, 1); /* Flush TLB as domain not tracked in TLB */
1315 case 4: /* Reserved. */
1317 case 5: /* MMU Fault status / MPU access permission. */
1318 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1322 if (arm_feature(env, ARM_FEATURE_MPU))
1323 val = extended_mpu_ap_bits(val);
1324 env->cp15.c5_data = val;
1327 if (arm_feature(env, ARM_FEATURE_MPU))
1328 val = extended_mpu_ap_bits(val);
1329 env->cp15.c5_insn = val;
1332 if (!arm_feature(env, ARM_FEATURE_MPU))
1334 env->cp15.c5_data = val;
1337 if (!arm_feature(env, ARM_FEATURE_MPU))
1339 env->cp15.c5_insn = val;
1345 case 6: /* MMU Fault address / MPU base/size. */
1346 if (arm_feature(env, ARM_FEATURE_MPU)) {
1349 env->cp15.c6_region[crm] = val;
1351 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1355 env->cp15.c6_data = val;
1357 case 1: /* ??? This is WFAR on armv6 */
1359 env->cp15.c6_insn = val;
1366 case 7: /* Cache control. */
1367 env->cp15.c15_i_max = 0x000;
1368 env->cp15.c15_i_min = 0xff0;
1369 /* No cache, so nothing to do. */
1370 /* ??? MPCore has VA to PA translation functions. */
1372 case 8: /* MMU TLB control. */
1374 case 0: /* Invalidate all. */
1377 case 1: /* Invalidate single TLB entry. */
1379 /* ??? This is wrong for large pages and sections. */
1380 /* As an ugly hack to make linux work we always flush a 4K
1383 tlb_flush_page(env, val);
1384 tlb_flush_page(env, val + 0x400);
1385 tlb_flush_page(env, val + 0x800);
1386 tlb_flush_page(env, val + 0xc00);
1391 case 2: /* Invalidate on ASID. */
1392 tlb_flush(env, val == 0);
1394 case 3: /* Invalidate single entry on MVA. */
1395 /* ??? This is like case 1, but ignores ASID. */
1403 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1406 case 0: /* Cache lockdown. */
1408 case 0: /* L1 cache. */
1411 env->cp15.c9_data = val;
1414 env->cp15.c9_insn = val;
1420 case 1: /* L2 cache. */
1421 /* Ignore writes to L2 lockdown/auxiliary registers. */
1427 case 1: /* TCM memory region registers. */
1428 /* Not implemented. */
1434 case 10: /* MMU TLB lockdown. */
1435 /* ??? TLB lockdown not implemented. */
1437 case 12: /* Reserved. */
1439 case 13: /* Process ID. */
1442 /* Unlike real hardware the qemu TLB uses virtual addresses,
1443 not modified virtual addresses, so this causes a TLB flush.
1445 if (env->cp15.c13_fcse != val)
1447 env->cp15.c13_fcse = val;
1450 /* This changes the ASID, so do a TLB flush. */
1451 if (env->cp15.c13_context != val
1452 && !arm_feature(env, ARM_FEATURE_MPU))
1454 env->cp15.c13_context = val;
1457 env->cp15.c13_tls1 = val;
1460 env->cp15.c13_tls2 = val;
1463 env->cp15.c13_tls3 = val;
1469 case 14: /* Reserved. */
1471 case 15: /* Implementation specific. */
1472 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
1473 if (op2 == 0 && crm == 1) {
1474 if (env->cp15.c15_cpar != (val & 0x3fff)) {
1475 /* Changes cp0 to cp13 behavior, so needs a TB flush. */
1477 env->cp15.c15_cpar = val & 0x3fff;
1483 if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
1487 case 1: /* Set TI925T configuration. */
1488 env->cp15.c15_ticonfig = val & 0xe7;
1489 env->cp15.c0_cpuid = (val & (1 << 5)) ? /* OS_TYPE bit */
1490 ARM_CPUID_TI915T : ARM_CPUID_TI925T;
1492 case 2: /* Set I_max. */
1493 env->cp15.c15_i_max = val;
1495 case 3: /* Set I_min. */
1496 env->cp15.c15_i_min = val;
1498 case 4: /* Set thread-ID. */
1499 env->cp15.c15_threadid = val & 0xffff;
1501 case 8: /* Wait-for-interrupt (deprecated). */
1502 cpu_interrupt(env, CPU_INTERRUPT_HALT);
1512 /* ??? For debugging only. Should raise illegal instruction exception. */
1513 cpu_abort(env, "Unimplemented cp15 register write (c%d, c%d, {%d, %d})\n",
1514 (insn >> 16) & 0xf, crm, op1, op2);
1517 uint32_t HELPER(get_cp15)(CPUState *env, uint32_t insn)
1523 op1 = (insn >> 21) & 7;
1524 op2 = (insn >> 5) & 7;
1526 switch ((insn >> 16) & 0xf) {
1527 case 0: /* ID codes. */
1533 case 0: /* Device ID. */
1534 return env->cp15.c0_cpuid;
1535 case 1: /* Cache Type. */
1536 return env->cp15.c0_cachetype;
1537 case 2: /* TCM status. */
1539 case 3: /* TLB type register. */
1540 return 0; /* No lockable TLB entries. */
1541 case 5: /* CPU ID */
1542 return env->cpu_index;
1547 if (!arm_feature(env, ARM_FEATURE_V6))
1549 return env->cp15.c0_c1[op2];
1551 if (!arm_feature(env, ARM_FEATURE_V6))
1553 return env->cp15.c0_c2[op2];
1554 case 3: case 4: case 5: case 6: case 7:
1560 /* These registers aren't documented on arm11 cores. However
1561 Linux looks at them anyway. */
1562 if (!arm_feature(env, ARM_FEATURE_V6))
1566 if (arm_feature(env, ARM_FEATURE_XSCALE))
1572 case 1: /* System configuration. */
1573 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1576 case 0: /* Control register. */
1577 return env->cp15.c1_sys;
1578 case 1: /* Auxiliary control register. */
1579 if (arm_feature(env, ARM_FEATURE_XSCALE))
1580 return env->cp15.c1_xscaleauxcr;
1581 if (!arm_feature(env, ARM_FEATURE_AUXCR))
1583 switch (ARM_CPUID(env)) {
1584 case ARM_CPUID_ARM1026:
1586 case ARM_CPUID_ARM1136:
1587 case ARM_CPUID_ARM1136_R2:
1589 case ARM_CPUID_ARM11MPCORE:
1591 case ARM_CPUID_CORTEXA8:
1596 case 2: /* Coprocessor access register. */
1597 if (arm_feature(env, ARM_FEATURE_XSCALE))
1599 return env->cp15.c1_coproc;
1603 case 2: /* MMU Page table control / MPU cache control. */
1604 if (arm_feature(env, ARM_FEATURE_MPU)) {
1607 return env->cp15.c2_data;
1610 return env->cp15.c2_insn;
1618 return env->cp15.c2_base0;
1620 return env->cp15.c2_base1;
1626 mask = env->cp15.c2_mask;
1637 case 3: /* MMU Domain access control / MPU write buffer control. */
1638 return env->cp15.c3;
1639 case 4: /* Reserved. */
1641 case 5: /* MMU Fault status / MPU access permission. */
1642 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1646 if (arm_feature(env, ARM_FEATURE_MPU))
1647 return simple_mpu_ap_bits(env->cp15.c5_data);
1648 return env->cp15.c5_data;
1650 if (arm_feature(env, ARM_FEATURE_MPU))
1651 return simple_mpu_ap_bits(env->cp15.c5_data);
1652 return env->cp15.c5_insn;
1654 if (!arm_feature(env, ARM_FEATURE_MPU))
1656 return env->cp15.c5_data;
1658 if (!arm_feature(env, ARM_FEATURE_MPU))
1660 return env->cp15.c5_insn;
1664 case 6: /* MMU Fault address. */
1665 if (arm_feature(env, ARM_FEATURE_MPU)) {
1668 return env->cp15.c6_region[crm];
1670 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1674 return env->cp15.c6_data;
1676 if (arm_feature(env, ARM_FEATURE_V6)) {
1677 /* Watchpoint Fault Adrress. */
1678 return 0; /* Not implemented. */
1680 /* Instruction Fault Adrress. */
1681 /* Arm9 doesn't have an IFAR, but implementing it anyway
1682 shouldn't do any harm. */
1683 return env->cp15.c6_insn;
1686 if (arm_feature(env, ARM_FEATURE_V6)) {
1687 /* Instruction Fault Adrress. */
1688 return env->cp15.c6_insn;
1696 case 7: /* Cache control. */
1697 /* FIXME: Should only clear Z flag if destination is r15. */
1700 case 8: /* MMU TLB control. */
1702 case 9: /* Cache lockdown. */
1704 case 0: /* L1 cache. */
1705 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1709 return env->cp15.c9_data;
1711 return env->cp15.c9_insn;
1715 case 1: /* L2 cache */
1718 /* L2 Lockdown and Auxiliary control. */
1723 case 10: /* MMU TLB lockdown. */
1724 /* ??? TLB lockdown not implemented. */
1726 case 11: /* TCM DMA control. */
1727 case 12: /* Reserved. */
1729 case 13: /* Process ID. */
1732 return env->cp15.c13_fcse;
1734 return env->cp15.c13_context;
1736 return env->cp15.c13_tls1;
1738 return env->cp15.c13_tls2;
1740 return env->cp15.c13_tls3;
1744 case 14: /* Reserved. */
1746 case 15: /* Implementation specific. */
1747 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
1748 if (op2 == 0 && crm == 1)
1749 return env->cp15.c15_cpar;
1753 if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
1757 case 1: /* Read TI925T configuration. */
1758 return env->cp15.c15_ticonfig;
1759 case 2: /* Read I_max. */
1760 return env->cp15.c15_i_max;
1761 case 3: /* Read I_min. */
1762 return env->cp15.c15_i_min;
1763 case 4: /* Read thread-ID. */
1764 return env->cp15.c15_threadid;
1765 case 8: /* TI925T_status */
1768 /* TODO: Peripheral port remap register:
1769 * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt
1770 * controller base address at $rn & ~0xfff and map size of
1771 * 0x200 << ($rn & 0xfff), when MMU is off. */
1777 /* ??? For debugging only. Should raise illegal instruction exception. */
1778 cpu_abort(env, "Unimplemented cp15 register read (c%d, c%d, {%d, %d})\n",
1779 (insn >> 16) & 0xf, crm, op1, op2);
1783 void HELPER(set_r13_banked)(CPUState *env, uint32_t mode, uint32_t val)
1785 env->banked_r13[bank_number(mode)] = val;
1788 uint32_t HELPER(get_r13_banked)(CPUState *env, uint32_t mode)
1790 return env->banked_r13[bank_number(mode)];
1793 uint32_t HELPER(v7m_mrs)(CPUState *env, uint32_t reg)
1797 return xpsr_read(env) & 0xf8000000;
1799 return xpsr_read(env) & 0xf80001ff;
1801 return xpsr_read(env) & 0xff00fc00;
1803 return xpsr_read(env) & 0xff00fdff;
1805 return xpsr_read(env) & 0x000001ff;
1807 return xpsr_read(env) & 0x0700fc00;
1809 return xpsr_read(env) & 0x0700edff;
1811 return env->v7m.current_sp ? env->v7m.other_sp : env->regs[13];
1813 return env->v7m.current_sp ? env->regs[13] : env->v7m.other_sp;
1814 case 16: /* PRIMASK */
1815 return (env->uncached_cpsr & CPSR_I) != 0;
1816 case 17: /* FAULTMASK */
1817 return (env->uncached_cpsr & CPSR_F) != 0;
1818 case 18: /* BASEPRI */
1819 case 19: /* BASEPRI_MAX */
1820 return env->v7m.basepri;
1821 case 20: /* CONTROL */
1822 return env->v7m.control;
1824 /* ??? For debugging only. */
1825 cpu_abort(env, "Unimplemented system register read (%d)\n", reg);
1830 void HELPER(v7m_msr)(CPUState *env, uint32_t reg, uint32_t val)
1834 xpsr_write(env, val, 0xf8000000);
1837 xpsr_write(env, val, 0xf8000000);
1840 xpsr_write(env, val, 0xfe00fc00);
1843 xpsr_write(env, val, 0xfe00fc00);
1846 /* IPSR bits are readonly. */
1849 xpsr_write(env, val, 0x0600fc00);
1852 xpsr_write(env, val, 0x0600fc00);
1855 if (env->v7m.current_sp)
1856 env->v7m.other_sp = val;
1858 env->regs[13] = val;
1861 if (env->v7m.current_sp)
1862 env->regs[13] = val;
1864 env->v7m.other_sp = val;
1866 case 16: /* PRIMASK */
1868 env->uncached_cpsr |= CPSR_I;
1870 env->uncached_cpsr &= ~CPSR_I;
1872 case 17: /* FAULTMASK */
1874 env->uncached_cpsr |= CPSR_F;
1876 env->uncached_cpsr &= ~CPSR_F;
1878 case 18: /* BASEPRI */
1879 env->v7m.basepri = val & 0xff;
1881 case 19: /* BASEPRI_MAX */
1883 if (val != 0 && (val < env->v7m.basepri || env->v7m.basepri == 0))
1884 env->v7m.basepri = val;
1886 case 20: /* CONTROL */
1887 env->v7m.control = val & 3;
1888 switch_v7m_sp(env, (val & 2) != 0);
1891 /* ??? For debugging only. */
1892 cpu_abort(env, "Unimplemented system register write (%d)\n", reg);
1897 void cpu_arm_set_cp_io(CPUARMState *env, int cpnum,
1898 ARMReadCPFunc *cp_read, ARMWriteCPFunc *cp_write,
1901 if (cpnum < 0 || cpnum > 14) {
1902 cpu_abort(env, "Bad coprocessor number: %i\n", cpnum);
1906 env->cp[cpnum].cp_read = cp_read;
1907 env->cp[cpnum].cp_write = cp_write;
1908 env->cp[cpnum].opaque = opaque;
1913 /* Note that signed overflow is undefined in C. The following routines are
1914 careful to use unsigned types where modulo arithmetic is required.
1915 Failure to do so _will_ break on newer gcc. */
1917 /* Signed saturating arithmetic. */
1919 /* Perform 16-bit signed saturating addition. */
1920 static inline uint16_t add16_sat(uint16_t a, uint16_t b)
1925 if (((res ^ a) & 0x8000) && !((a ^ b) & 0x8000)) {
1934 /* Perform 8-bit signed saturating addition. */
1935 static inline uint8_t add8_sat(uint8_t a, uint8_t b)
1940 if (((res ^ a) & 0x80) && !((a ^ b) & 0x80)) {
1949 /* Perform 16-bit signed saturating subtraction. */
1950 static inline uint16_t sub16_sat(uint16_t a, uint16_t b)
1955 if (((res ^ a) & 0x8000) && ((a ^ b) & 0x8000)) {
1964 /* Perform 8-bit signed saturating subtraction. */
1965 static inline uint8_t sub8_sat(uint8_t a, uint8_t b)
1970 if (((res ^ a) & 0x80) && ((a ^ b) & 0x80)) {
1979 #define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16);
1980 #define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16);
1981 #define ADD8(a, b, n) RESULT(add8_sat(a, b), n, 8);
1982 #define SUB8(a, b, n) RESULT(sub8_sat(a, b), n, 8);
1985 #include "op_addsub.h"
1987 /* Unsigned saturating arithmetic. */
1988 static inline uint16_t add16_usat(uint16_t a, uint16_t b)
1997 static inline uint16_t sub16_usat(uint16_t a, uint16_t b)
2005 static inline uint8_t add8_usat(uint8_t a, uint8_t b)
2014 static inline uint8_t sub8_usat(uint8_t a, uint8_t b)
2022 #define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16);
2023 #define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16);
2024 #define ADD8(a, b, n) RESULT(add8_usat(a, b), n, 8);
2025 #define SUB8(a, b, n) RESULT(sub8_usat(a, b), n, 8);
2028 #include "op_addsub.h"
2030 /* Signed modulo arithmetic. */
2031 #define SARITH16(a, b, n, op) do { \
2033 sum = (int16_t)((uint16_t)(a) op (uint16_t)(b)); \
2034 RESULT(sum, n, 16); \
2036 ge |= 3 << (n * 2); \
2039 #define SARITH8(a, b, n, op) do { \
2041 sum = (int8_t)((uint8_t)(a) op (uint8_t)(b)); \
2042 RESULT(sum, n, 8); \
2048 #define ADD16(a, b, n) SARITH16(a, b, n, +)
2049 #define SUB16(a, b, n) SARITH16(a, b, n, -)
2050 #define ADD8(a, b, n) SARITH8(a, b, n, +)
2051 #define SUB8(a, b, n) SARITH8(a, b, n, -)
2055 #include "op_addsub.h"
2057 /* Unsigned modulo arithmetic. */
2058 #define ADD16(a, b, n) do { \
2060 sum = (uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b); \
2061 RESULT(sum, n, 16); \
2062 if ((sum >> 16) == 0) \
2063 ge |= 3 << (n * 2); \
2066 #define ADD8(a, b, n) do { \
2068 sum = (uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b); \
2069 RESULT(sum, n, 8); \
2070 if ((sum >> 8) == 0) \
2071 ge |= 3 << (n * 2); \
2074 #define SUB16(a, b, n) do { \
2076 sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \
2077 RESULT(sum, n, 16); \
2078 if ((sum >> 16) == 0) \
2079 ge |= 3 << (n * 2); \
2082 #define SUB8(a, b, n) do { \
2084 sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \
2085 RESULT(sum, n, 8); \
2086 if ((sum >> 8) == 0) \
2087 ge |= 3 << (n * 2); \
2093 #include "op_addsub.h"
2095 /* Halved signed arithmetic. */
2096 #define ADD16(a, b, n) \
2097 RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16)
2098 #define SUB16(a, b, n) \
2099 RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16)
2100 #define ADD8(a, b, n) \
2101 RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8)
2102 #define SUB8(a, b, n) \
2103 RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8)
2106 #include "op_addsub.h"
2108 /* Halved unsigned arithmetic. */
2109 #define ADD16(a, b, n) \
2110 RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16)
2111 #define SUB16(a, b, n) \
2112 RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16)
2113 #define ADD8(a, b, n) \
2114 RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8)
2115 #define SUB8(a, b, n) \
2116 RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8)
2119 #include "op_addsub.h"
2121 static inline uint8_t do_usad(uint8_t a, uint8_t b)
2129 /* Unsigned sum of absolute byte differences. */
2130 uint32_t HELPER(usad8)(uint32_t a, uint32_t b)
2133 sum = do_usad(a, b);
2134 sum += do_usad(a >> 8, b >> 8);
2135 sum += do_usad(a >> 16, b >>16);
2136 sum += do_usad(a >> 24, b >> 24);
2140 /* For ARMv6 SEL instruction. */
2141 uint32_t HELPER(sel_flags)(uint32_t flags, uint32_t a, uint32_t b)
2154 return (a & mask) | (b & ~mask);
2157 uint32_t HELPER(logicq_cc)(uint64_t val)
2159 return (val >> 32) | (val != 0);
2162 /* VFP support. We follow the convention used for VFP instrunctions:
2163 Single precition routines have a "s" suffix, double precision a
2166 /* Convert host exception flags to vfp form. */
2167 static inline int vfp_exceptbits_from_host(int host_bits)
2169 int target_bits = 0;
2171 if (host_bits & float_flag_invalid)
2173 if (host_bits & float_flag_divbyzero)
2175 if (host_bits & float_flag_overflow)
2177 if (host_bits & float_flag_underflow)
2179 if (host_bits & float_flag_inexact)
2180 target_bits |= 0x10;
2184 uint32_t HELPER(vfp_get_fpscr)(CPUState *env)
2189 fpscr = (env->vfp.xregs[ARM_VFP_FPSCR] & 0xffc8ffff)
2190 | (env->vfp.vec_len << 16)
2191 | (env->vfp.vec_stride << 20);
2192 i = get_float_exception_flags(&env->vfp.fp_status);
2193 fpscr |= vfp_exceptbits_from_host(i);
2197 /* Convert vfp exception flags to target form. */
2198 static inline int vfp_exceptbits_to_host(int target_bits)
2202 if (target_bits & 1)
2203 host_bits |= float_flag_invalid;
2204 if (target_bits & 2)
2205 host_bits |= float_flag_divbyzero;
2206 if (target_bits & 4)
2207 host_bits |= float_flag_overflow;
2208 if (target_bits & 8)
2209 host_bits |= float_flag_underflow;
2210 if (target_bits & 0x10)
2211 host_bits |= float_flag_inexact;
2215 void HELPER(vfp_set_fpscr)(CPUState *env, uint32_t val)
2220 changed = env->vfp.xregs[ARM_VFP_FPSCR];
2221 env->vfp.xregs[ARM_VFP_FPSCR] = (val & 0xffc8ffff);
2222 env->vfp.vec_len = (val >> 16) & 7;
2223 env->vfp.vec_stride = (val >> 20) & 3;
2226 if (changed & (3 << 22)) {
2227 i = (val >> 22) & 3;
2230 i = float_round_nearest_even;
2236 i = float_round_down;
2239 i = float_round_to_zero;
2242 set_float_rounding_mode(i, &env->vfp.fp_status);
2245 i = vfp_exceptbits_to_host((val >> 8) & 0x1f);
2246 set_float_exception_flags(i, &env->vfp.fp_status);
2247 /* XXX: FZ and DN are not implemented. */
2250 #define VFP_HELPER(name, p) HELPER(glue(glue(vfp_,name),p))
2252 #define VFP_BINOP(name) \
2253 float32 VFP_HELPER(name, s)(float32 a, float32 b, CPUState *env) \
2255 return float32_ ## name (a, b, &env->vfp.fp_status); \
2257 float64 VFP_HELPER(name, d)(float64 a, float64 b, CPUState *env) \
2259 return float64_ ## name (a, b, &env->vfp.fp_status); \
2267 float32 VFP_HELPER(neg, s)(float32 a)
2269 return float32_chs(a);
2272 float64 VFP_HELPER(neg, d)(float64 a)
2274 return float64_chs(a);
2277 float32 VFP_HELPER(abs, s)(float32 a)
2279 return float32_abs(a);
2282 float64 VFP_HELPER(abs, d)(float64 a)
2284 return float64_abs(a);
2287 float32 VFP_HELPER(sqrt, s)(float32 a, CPUState *env)
2289 return float32_sqrt(a, &env->vfp.fp_status);
2292 float64 VFP_HELPER(sqrt, d)(float64 a, CPUState *env)
2294 return float64_sqrt(a, &env->vfp.fp_status);
2297 /* XXX: check quiet/signaling case */
2298 #define DO_VFP_cmp(p, type) \
2299 void VFP_HELPER(cmp, p)(type a, type b, CPUState *env) \
2302 switch(type ## _compare_quiet(a, b, &env->vfp.fp_status)) { \
2303 case 0: flags = 0x6; break; \
2304 case -1: flags = 0x8; break; \
2305 case 1: flags = 0x2; break; \
2306 default: case 2: flags = 0x3; break; \
2308 env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \
2309 | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \
2311 void VFP_HELPER(cmpe, p)(type a, type b, CPUState *env) \
2314 switch(type ## _compare(a, b, &env->vfp.fp_status)) { \
2315 case 0: flags = 0x6; break; \
2316 case -1: flags = 0x8; break; \
2317 case 1: flags = 0x2; break; \
2318 default: case 2: flags = 0x3; break; \
2320 env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \
2321 | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \
2323 DO_VFP_cmp(s, float32)
2324 DO_VFP_cmp(d, float64)
2327 /* Helper routines to perform bitwise copies between float and int. */
2328 static inline float32 vfp_itos(uint32_t i)
2339 static inline uint32_t vfp_stoi(float32 s)
2350 static inline float64 vfp_itod(uint64_t i)
2361 static inline uint64_t vfp_dtoi(float64 d)
2372 /* Integer to float conversion. */
2373 float32 VFP_HELPER(uito, s)(float32 x, CPUState *env)
2375 return uint32_to_float32(vfp_stoi(x), &env->vfp.fp_status);
2378 float64 VFP_HELPER(uito, d)(float32 x, CPUState *env)
2380 return uint32_to_float64(vfp_stoi(x), &env->vfp.fp_status);
2383 float32 VFP_HELPER(sito, s)(float32 x, CPUState *env)
2385 return int32_to_float32(vfp_stoi(x), &env->vfp.fp_status);
2388 float64 VFP_HELPER(sito, d)(float32 x, CPUState *env)
2390 return int32_to_float64(vfp_stoi(x), &env->vfp.fp_status);
2393 /* Float to integer conversion. */
2394 float32 VFP_HELPER(toui, s)(float32 x, CPUState *env)
2396 return vfp_itos(float32_to_uint32(x, &env->vfp.fp_status));
2399 float32 VFP_HELPER(toui, d)(float64 x, CPUState *env)
2401 return vfp_itos(float64_to_uint32(x, &env->vfp.fp_status));
2404 float32 VFP_HELPER(tosi, s)(float32 x, CPUState *env)
2406 return vfp_itos(float32_to_int32(x, &env->vfp.fp_status));
2409 float32 VFP_HELPER(tosi, d)(float64 x, CPUState *env)
2411 return vfp_itos(float64_to_int32(x, &env->vfp.fp_status));
2414 float32 VFP_HELPER(touiz, s)(float32 x, CPUState *env)
2416 return vfp_itos(float32_to_uint32_round_to_zero(x, &env->vfp.fp_status));
2419 float32 VFP_HELPER(touiz, d)(float64 x, CPUState *env)
2421 return vfp_itos(float64_to_uint32_round_to_zero(x, &env->vfp.fp_status));
2424 float32 VFP_HELPER(tosiz, s)(float32 x, CPUState *env)
2426 return vfp_itos(float32_to_int32_round_to_zero(x, &env->vfp.fp_status));
2429 float32 VFP_HELPER(tosiz, d)(float64 x, CPUState *env)
2431 return vfp_itos(float64_to_int32_round_to_zero(x, &env->vfp.fp_status));
2434 /* floating point conversion */
2435 float64 VFP_HELPER(fcvtd, s)(float32 x, CPUState *env)
2437 return float32_to_float64(x, &env->vfp.fp_status);
2440 float32 VFP_HELPER(fcvts, d)(float64 x, CPUState *env)
2442 return float64_to_float32(x, &env->vfp.fp_status);
2445 /* VFP3 fixed point conversion. */
2446 #define VFP_CONV_FIX(name, p, ftype, itype, sign) \
2447 ftype VFP_HELPER(name##to, p)(ftype x, uint32_t shift, CPUState *env) \
2450 tmp = sign##int32_to_##ftype ((itype)vfp_##p##toi(x), \
2451 &env->vfp.fp_status); \
2452 return ftype##_scalbn(tmp, shift, &env->vfp.fp_status); \
2454 ftype VFP_HELPER(to##name, p)(ftype x, uint32_t shift, CPUState *env) \
2457 tmp = ftype##_scalbn(x, shift, &env->vfp.fp_status); \
2458 return vfp_ito##p((itype)ftype##_to_##sign##int32_round_to_zero(tmp, \
2459 &env->vfp.fp_status)); \
2462 VFP_CONV_FIX(sh, d, float64, int16, )
2463 VFP_CONV_FIX(sl, d, float64, int32, )
2464 VFP_CONV_FIX(uh, d, float64, uint16, u)
2465 VFP_CONV_FIX(ul, d, float64, uint32, u)
2466 VFP_CONV_FIX(sh, s, float32, int16, )
2467 VFP_CONV_FIX(sl, s, float32, int32, )
2468 VFP_CONV_FIX(uh, s, float32, uint16, u)
2469 VFP_CONV_FIX(ul, s, float32, uint32, u)
2472 float32 HELPER(recps_f32)(float32 a, float32 b, CPUState *env)
2474 float_status *s = &env->vfp.fp_status;
2475 float32 two = int32_to_float32(2, s);
2476 return float32_sub(two, float32_mul(a, b, s), s);
2479 float32 HELPER(rsqrts_f32)(float32 a, float32 b, CPUState *env)
2481 float_status *s = &env->vfp.fp_status;
2482 float32 three = int32_to_float32(3, s);
2483 return float32_sub(three, float32_mul(a, b, s), s);
2488 /* TODO: The architecture specifies the value that the estimate functions
2489 should return. We return the exact reciprocal/root instead. */
2490 float32 HELPER(recpe_f32)(float32 a, CPUState *env)
2492 float_status *s = &env->vfp.fp_status;
2493 float32 one = int32_to_float32(1, s);
2494 return float32_div(one, a, s);
2497 float32 HELPER(rsqrte_f32)(float32 a, CPUState *env)
2499 float_status *s = &env->vfp.fp_status;
2500 float32 one = int32_to_float32(1, s);
2501 return float32_div(one, float32_sqrt(a, s), s);
2504 uint32_t HELPER(recpe_u32)(uint32_t a, CPUState *env)
2506 float_status *s = &env->vfp.fp_status;
2508 tmp = int32_to_float32(a, s);
2509 tmp = float32_scalbn(tmp, -32, s);
2510 tmp = helper_recpe_f32(tmp, env);
2511 tmp = float32_scalbn(tmp, 31, s);
2512 return float32_to_int32(tmp, s);
2515 uint32_t HELPER(rsqrte_u32)(uint32_t a, CPUState *env)
2517 float_status *s = &env->vfp.fp_status;
2519 tmp = int32_to_float32(a, s);
2520 tmp = float32_scalbn(tmp, -32, s);
2521 tmp = helper_rsqrte_f32(tmp, env);
2522 tmp = float32_scalbn(tmp, 31, s);
2523 return float32_to_int32(tmp, s);