4 #include "host-utils.h"
5 #if !defined(CONFIG_USER_ONLY)
10 static uint32_t cortexa15_cp15_c0_c1[8] = {
11 0x00001131, 0x00011011, 0x02010555, 0x00000000,
12 0x10201105, 0x20000000, 0x01240000, 0x02102211
15 static uint32_t cortexa15_cp15_c0_c2[8] = {
16 0x02101110, 0x13112111, 0x21232041, 0x11112131, 0x10011142, 0, 0, 0
19 static uint32_t cortexa9_cp15_c0_c1[8] =
20 { 0x1031, 0x11, 0x000, 0, 0x00100103, 0x20000000, 0x01230000, 0x00002111 };
22 static uint32_t cortexa9_cp15_c0_c2[8] =
23 { 0x00101111, 0x13112111, 0x21232041, 0x11112131, 0x00111142, 0, 0, 0 };
25 static uint32_t cortexa8_cp15_c0_c1[8] =
26 { 0x1031, 0x11, 0x400, 0, 0x31100003, 0x20000000, 0x01202000, 0x11 };
28 static uint32_t cortexa8_cp15_c0_c2[8] =
29 { 0x00101111, 0x12112111, 0x21232031, 0x11112131, 0x00111142, 0, 0, 0 };
31 static uint32_t mpcore_cp15_c0_c1[8] =
32 { 0x111, 0x1, 0, 0x2, 0x01100103, 0x10020302, 0x01222000, 0 };
34 static uint32_t mpcore_cp15_c0_c2[8] =
35 { 0x00100011, 0x12002111, 0x11221011, 0x01102131, 0x141, 0, 0, 0 };
37 static uint32_t arm1136_cp15_c0_c1[8] =
38 { 0x111, 0x1, 0x2, 0x3, 0x01130003, 0x10030302, 0x01222110, 0 };
40 static uint32_t arm1136_cp15_c0_c2[8] =
41 { 0x00140011, 0x12002111, 0x11231111, 0x01102131, 0x141, 0, 0, 0 };
43 static uint32_t arm1176_cp15_c0_c1[8] =
44 { 0x111, 0x11, 0x33, 0, 0x01130003, 0x10030302, 0x01222100, 0 };
46 static uint32_t arm1176_cp15_c0_c2[8] =
47 { 0x0140011, 0x12002111, 0x11231121, 0x01102131, 0x01141, 0, 0, 0 };
49 static void cpu_reset_model_id(CPUARMState *env, uint32_t id)
52 case ARM_CPUID_ARM926:
54 case ARM_CPUID_ARM946:
56 case ARM_CPUID_ARM1026:
58 case ARM_CPUID_ARM1136:
59 /* This is the 1136 r1, which is a v6K core */
60 case ARM_CPUID_ARM1136_R2:
61 /* What qemu calls "arm1136_r2" is actually the 1136 r0p2, ie an
62 * older core than plain "arm1136". In particular this does not
63 * have the v6K features.
65 /* These ID register values are correct for 1136 but may be wrong
66 * for 1136_r2 (in particular r0p2 does not actually implement most
67 * of the ID registers).
69 memcpy(env->cp15.c0_c1, arm1136_cp15_c0_c1, 8 * sizeof(uint32_t));
70 memcpy(env->cp15.c0_c2, arm1136_cp15_c0_c2, 8 * sizeof(uint32_t));
72 case ARM_CPUID_ARM1176:
73 memcpy(env->cp15.c0_c1, arm1176_cp15_c0_c1, 8 * sizeof(uint32_t));
74 memcpy(env->cp15.c0_c2, arm1176_cp15_c0_c2, 8 * sizeof(uint32_t));
76 case ARM_CPUID_ARM11MPCORE:
77 memcpy(env->cp15.c0_c1, mpcore_cp15_c0_c1, 8 * sizeof(uint32_t));
78 memcpy(env->cp15.c0_c2, mpcore_cp15_c0_c2, 8 * sizeof(uint32_t));
80 case ARM_CPUID_CORTEXA8:
81 memcpy(env->cp15.c0_c1, cortexa8_cp15_c0_c1, 8 * sizeof(uint32_t));
82 memcpy(env->cp15.c0_c2, cortexa8_cp15_c0_c2, 8 * sizeof(uint32_t));
83 env->cp15.c0_clid = (1 << 27) | (2 << 24) | 3;
84 env->cp15.c0_ccsid[0] = 0xe007e01a; /* 16k L1 dcache. */
85 env->cp15.c0_ccsid[1] = 0x2007e01a; /* 16k L1 icache. */
86 env->cp15.c0_ccsid[2] = 0xf0000000; /* No L2 icache. */
88 case ARM_CPUID_CORTEXA9:
89 memcpy(env->cp15.c0_c1, cortexa9_cp15_c0_c1, 8 * sizeof(uint32_t));
90 memcpy(env->cp15.c0_c2, cortexa9_cp15_c0_c2, 8 * sizeof(uint32_t));
91 env->cp15.c0_clid = (1 << 27) | (1 << 24) | 3;
92 env->cp15.c0_ccsid[0] = 0xe00fe015; /* 16k L1 dcache. */
93 env->cp15.c0_ccsid[1] = 0x200fe015; /* 16k L1 icache. */
95 case ARM_CPUID_CORTEXA15:
96 memcpy(env->cp15.c0_c1, cortexa15_cp15_c0_c1, 8 * sizeof(uint32_t));
97 memcpy(env->cp15.c0_c2, cortexa15_cp15_c0_c2, 8 * sizeof(uint32_t));
98 env->cp15.c0_clid = 0x0a200023;
99 env->cp15.c0_ccsid[0] = 0x701fe00a; /* 32K L1 dcache */
100 env->cp15.c0_ccsid[1] = 0x201fe00a; /* 32K L1 icache */
101 env->cp15.c0_ccsid[2] = 0x711fe07a; /* 4096K L2 unified cache */
103 case ARM_CPUID_CORTEXM3:
105 case ARM_CPUID_ANY: /* For userspace emulation. */
107 case ARM_CPUID_TI915T:
108 case ARM_CPUID_TI925T:
109 env->cp15.c15_i_max = 0x000;
110 env->cp15.c15_i_min = 0xff0;
112 case ARM_CPUID_PXA250:
113 case ARM_CPUID_PXA255:
114 case ARM_CPUID_PXA260:
115 case ARM_CPUID_PXA261:
116 case ARM_CPUID_PXA262:
118 case ARM_CPUID_PXA270_A0:
119 case ARM_CPUID_PXA270_A1:
120 case ARM_CPUID_PXA270_B0:
121 case ARM_CPUID_PXA270_B1:
122 case ARM_CPUID_PXA270_C0:
123 case ARM_CPUID_PXA270_C5:
124 env->iwmmxt.cregs[ARM_IWMMXT_wCID] = 0x69051000 | 'Q';
126 case ARM_CPUID_SA1100:
127 case ARM_CPUID_SA1110:
130 cpu_abort(env, "Bad CPU ID: %x\n", id);
136 /* TODO Move contents into arm_cpu_reset() in cpu.c,
137 * once cpu_reset_model_id() is eliminated,
138 * and then forward to cpu_reset() here.
140 void cpu_state_reset(CPUARMState *env)
144 ARMCPU *cpu = arm_env_get_cpu(env);
146 if (qemu_loglevel_mask(CPU_LOG_RESET)) {
147 qemu_log("CPU Reset (CPU %d)\n", env->cpu_index);
148 log_cpu_state(env, 0);
152 tmp = env->cp15.c15_config_base_address;
153 memset(env, 0, offsetof(CPUARMState, breakpoints));
155 cpu_reset_model_id(env, id);
156 env->cp15.c15_config_base_address = tmp;
157 env->cp15.c0_cpuid = cpu->midr;
158 env->vfp.xregs[ARM_VFP_FPSID] = cpu->reset_fpsid;
159 env->vfp.xregs[ARM_VFP_MVFR0] = cpu->mvfr0;
160 env->vfp.xregs[ARM_VFP_MVFR1] = cpu->mvfr1;
161 env->cp15.c0_cachetype = cpu->ctr;
162 env->cp15.c1_sys = cpu->reset_sctlr;
164 #if defined (CONFIG_USER_ONLY)
165 env->uncached_cpsr = ARM_CPU_MODE_USR;
166 /* For user mode we must enable access to coprocessors */
167 env->vfp.xregs[ARM_VFP_FPEXC] = 1 << 30;
168 if (arm_feature(env, ARM_FEATURE_IWMMXT)) {
169 env->cp15.c15_cpar = 3;
170 } else if (arm_feature(env, ARM_FEATURE_XSCALE)) {
171 env->cp15.c15_cpar = 1;
174 /* SVC mode with interrupts disabled. */
175 env->uncached_cpsr = ARM_CPU_MODE_SVC | CPSR_A | CPSR_F | CPSR_I;
176 /* On ARMv7-M the CPSR_I is the value of the PRIMASK register, and is
177 clear at reset. Initial SP and PC are loaded from ROM. */
181 env->uncached_cpsr &= ~CPSR_I;
184 /* We should really use ldl_phys here, in case the guest
185 modified flash and reset itself. However images
186 loaded via -kernel have not been copied yet, so load the
187 values directly from there. */
188 env->regs[13] = ldl_p(rom);
191 env->regs[15] = pc & ~1;
194 env->vfp.xregs[ARM_VFP_FPEXC] = 0;
195 env->cp15.c2_base_mask = 0xffffc000u;
196 /* v7 performance monitor control register: same implementor
197 * field as main ID register, and we implement no event counters.
199 env->cp15.c9_pmcr = (id & 0xff000000);
201 set_flush_to_zero(1, &env->vfp.standard_fp_status);
202 set_flush_inputs_to_zero(1, &env->vfp.standard_fp_status);
203 set_default_nan_mode(1, &env->vfp.standard_fp_status);
204 set_float_detect_tininess(float_tininess_before_rounding,
205 &env->vfp.fp_status);
206 set_float_detect_tininess(float_tininess_before_rounding,
207 &env->vfp.standard_fp_status);
209 /* Reset is a state change for some CPUARMState fields which we
210 * bake assumptions about into translated code, so we need to
216 static int vfp_gdb_get_reg(CPUARMState *env, uint8_t *buf, int reg)
220 /* VFP data registers are always little-endian. */
221 nregs = arm_feature(env, ARM_FEATURE_VFP3) ? 32 : 16;
223 stfq_le_p(buf, env->vfp.regs[reg]);
226 if (arm_feature(env, ARM_FEATURE_NEON)) {
227 /* Aliases for Q regs. */
230 stfq_le_p(buf, env->vfp.regs[(reg - 32) * 2]);
231 stfq_le_p(buf + 8, env->vfp.regs[(reg - 32) * 2 + 1]);
235 switch (reg - nregs) {
236 case 0: stl_p(buf, env->vfp.xregs[ARM_VFP_FPSID]); return 4;
237 case 1: stl_p(buf, env->vfp.xregs[ARM_VFP_FPSCR]); return 4;
238 case 2: stl_p(buf, env->vfp.xregs[ARM_VFP_FPEXC]); return 4;
243 static int vfp_gdb_set_reg(CPUARMState *env, uint8_t *buf, int reg)
247 nregs = arm_feature(env, ARM_FEATURE_VFP3) ? 32 : 16;
249 env->vfp.regs[reg] = ldfq_le_p(buf);
252 if (arm_feature(env, ARM_FEATURE_NEON)) {
255 env->vfp.regs[(reg - 32) * 2] = ldfq_le_p(buf);
256 env->vfp.regs[(reg - 32) * 2 + 1] = ldfq_le_p(buf + 8);
260 switch (reg - nregs) {
261 case 0: env->vfp.xregs[ARM_VFP_FPSID] = ldl_p(buf); return 4;
262 case 1: env->vfp.xregs[ARM_VFP_FPSCR] = ldl_p(buf); return 4;
263 case 2: env->vfp.xregs[ARM_VFP_FPEXC] = ldl_p(buf) & (1 << 30); return 4;
268 CPUARMState *cpu_arm_init(const char *cpu_model)
272 static int inited = 0;
274 if (!object_class_by_name(cpu_model)) {
277 cpu = ARM_CPU(object_new(cpu_model));
279 env->cpu_model_str = cpu_model;
280 arm_cpu_realize(cpu);
282 if (tcg_enabled() && !inited) {
284 arm_translate_init();
287 cpu_state_reset(env);
288 if (arm_feature(env, ARM_FEATURE_NEON)) {
289 gdb_register_coprocessor(env, vfp_gdb_get_reg, vfp_gdb_set_reg,
290 51, "arm-neon.xml", 0);
291 } else if (arm_feature(env, ARM_FEATURE_VFP3)) {
292 gdb_register_coprocessor(env, vfp_gdb_get_reg, vfp_gdb_set_reg,
293 35, "arm-vfp3.xml", 0);
294 } else if (arm_feature(env, ARM_FEATURE_VFP)) {
295 gdb_register_coprocessor(env, vfp_gdb_get_reg, vfp_gdb_set_reg,
296 19, "arm-vfp.xml", 0);
302 typedef struct ARMCPUListState {
303 fprintf_function cpu_fprintf;
307 /* Sort alphabetically by type name, except for "any". */
308 static gint arm_cpu_list_compare(gconstpointer a, gconstpointer b)
310 ObjectClass *class_a = (ObjectClass *)a;
311 ObjectClass *class_b = (ObjectClass *)b;
312 const char *name_a, *name_b;
314 name_a = object_class_get_name(class_a);
315 name_b = object_class_get_name(class_b);
316 if (strcmp(name_a, "any") == 0) {
318 } else if (strcmp(name_b, "any") == 0) {
321 return strcmp(name_a, name_b);
325 static void arm_cpu_list_entry(gpointer data, gpointer user_data)
327 ObjectClass *oc = data;
328 ARMCPUListState *s = user_data;
330 (*s->cpu_fprintf)(s->file, " %s\n",
331 object_class_get_name(oc));
334 void arm_cpu_list(FILE *f, fprintf_function cpu_fprintf)
336 ARMCPUListState s = {
338 .cpu_fprintf = cpu_fprintf,
342 list = object_class_get_list(TYPE_ARM_CPU, false);
343 list = g_slist_sort(list, arm_cpu_list_compare);
344 (*cpu_fprintf)(f, "Available CPUs:\n");
345 g_slist_foreach(list, arm_cpu_list_entry, &s);
349 static int bad_mode_switch(CPUARMState *env, int mode)
351 /* Return true if it is not valid for us to switch to
352 * this CPU mode (ie all the UNPREDICTABLE cases in
353 * the ARM ARM CPSRWriteByInstr pseudocode).
356 case ARM_CPU_MODE_USR:
357 case ARM_CPU_MODE_SYS:
358 case ARM_CPU_MODE_SVC:
359 case ARM_CPU_MODE_ABT:
360 case ARM_CPU_MODE_UND:
361 case ARM_CPU_MODE_IRQ:
362 case ARM_CPU_MODE_FIQ:
369 uint32_t cpsr_read(CPUARMState *env)
373 return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) |
374 (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27)
375 | (env->thumb << 5) | ((env->condexec_bits & 3) << 25)
376 | ((env->condexec_bits & 0xfc) << 8)
380 void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask)
382 if (mask & CPSR_NZCV) {
383 env->ZF = (~val) & CPSR_Z;
385 env->CF = (val >> 29) & 1;
386 env->VF = (val << 3) & 0x80000000;
389 env->QF = ((val & CPSR_Q) != 0);
391 env->thumb = ((val & CPSR_T) != 0);
392 if (mask & CPSR_IT_0_1) {
393 env->condexec_bits &= ~3;
394 env->condexec_bits |= (val >> 25) & 3;
396 if (mask & CPSR_IT_2_7) {
397 env->condexec_bits &= 3;
398 env->condexec_bits |= (val >> 8) & 0xfc;
400 if (mask & CPSR_GE) {
401 env->GE = (val >> 16) & 0xf;
404 if ((env->uncached_cpsr ^ val) & mask & CPSR_M) {
405 if (bad_mode_switch(env, val & CPSR_M)) {
406 /* Attempt to switch to an invalid mode: this is UNPREDICTABLE.
407 * We choose to ignore the attempt and leave the CPSR M field
412 switch_mode(env, val & CPSR_M);
415 mask &= ~CACHED_CPSR_BITS;
416 env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask);
419 /* Sign/zero extend */
420 uint32_t HELPER(sxtb16)(uint32_t x)
423 res = (uint16_t)(int8_t)x;
424 res |= (uint32_t)(int8_t)(x >> 16) << 16;
428 uint32_t HELPER(uxtb16)(uint32_t x)
431 res = (uint16_t)(uint8_t)x;
432 res |= (uint32_t)(uint8_t)(x >> 16) << 16;
436 uint32_t HELPER(clz)(uint32_t x)
441 int32_t HELPER(sdiv)(int32_t num, int32_t den)
445 if (num == INT_MIN && den == -1)
450 uint32_t HELPER(udiv)(uint32_t num, uint32_t den)
457 uint32_t HELPER(rbit)(uint32_t x)
459 x = ((x & 0xff000000) >> 24)
460 | ((x & 0x00ff0000) >> 8)
461 | ((x & 0x0000ff00) << 8)
462 | ((x & 0x000000ff) << 24);
463 x = ((x & 0xf0f0f0f0) >> 4)
464 | ((x & 0x0f0f0f0f) << 4);
465 x = ((x & 0x88888888) >> 3)
466 | ((x & 0x44444444) >> 1)
467 | ((x & 0x22222222) << 1)
468 | ((x & 0x11111111) << 3);
472 uint32_t HELPER(abs)(uint32_t x)
474 return ((int32_t)x < 0) ? -x : x;
477 #if defined(CONFIG_USER_ONLY)
479 void do_interrupt (CPUARMState *env)
481 env->exception_index = -1;
484 int cpu_arm_handle_mmu_fault (CPUARMState *env, target_ulong address, int rw,
488 env->exception_index = EXCP_PREFETCH_ABORT;
489 env->cp15.c6_insn = address;
491 env->exception_index = EXCP_DATA_ABORT;
492 env->cp15.c6_data = address;
497 /* These should probably raise undefined insn exceptions. */
498 void HELPER(set_cp)(CPUARMState *env, uint32_t insn, uint32_t val)
500 int op1 = (insn >> 8) & 0xf;
501 cpu_abort(env, "cp%i insn %08x\n", op1, insn);
505 uint32_t HELPER(get_cp)(CPUARMState *env, uint32_t insn)
507 int op1 = (insn >> 8) & 0xf;
508 cpu_abort(env, "cp%i insn %08x\n", op1, insn);
512 void HELPER(set_cp15)(CPUARMState *env, uint32_t insn, uint32_t val)
514 cpu_abort(env, "cp15 insn %08x\n", insn);
517 uint32_t HELPER(get_cp15)(CPUARMState *env, uint32_t insn)
519 cpu_abort(env, "cp15 insn %08x\n", insn);
522 /* These should probably raise undefined insn exceptions. */
523 void HELPER(v7m_msr)(CPUARMState *env, uint32_t reg, uint32_t val)
525 cpu_abort(env, "v7m_mrs %d\n", reg);
528 uint32_t HELPER(v7m_mrs)(CPUARMState *env, uint32_t reg)
530 cpu_abort(env, "v7m_mrs %d\n", reg);
534 void switch_mode(CPUARMState *env, int mode)
536 if (mode != ARM_CPU_MODE_USR)
537 cpu_abort(env, "Tried to switch out of user mode\n");
540 void HELPER(set_r13_banked)(CPUARMState *env, uint32_t mode, uint32_t val)
542 cpu_abort(env, "banked r13 write\n");
545 uint32_t HELPER(get_r13_banked)(CPUARMState *env, uint32_t mode)
547 cpu_abort(env, "banked r13 read\n");
553 /* Map CPU modes onto saved register banks. */
554 static inline int bank_number(CPUARMState *env, int mode)
557 case ARM_CPU_MODE_USR:
558 case ARM_CPU_MODE_SYS:
560 case ARM_CPU_MODE_SVC:
562 case ARM_CPU_MODE_ABT:
564 case ARM_CPU_MODE_UND:
566 case ARM_CPU_MODE_IRQ:
568 case ARM_CPU_MODE_FIQ:
571 cpu_abort(env, "Bad mode %x\n", mode);
575 void switch_mode(CPUARMState *env, int mode)
580 old_mode = env->uncached_cpsr & CPSR_M;
581 if (mode == old_mode)
584 if (old_mode == ARM_CPU_MODE_FIQ) {
585 memcpy (env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t));
586 memcpy (env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t));
587 } else if (mode == ARM_CPU_MODE_FIQ) {
588 memcpy (env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t));
589 memcpy (env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t));
592 i = bank_number(env, old_mode);
593 env->banked_r13[i] = env->regs[13];
594 env->banked_r14[i] = env->regs[14];
595 env->banked_spsr[i] = env->spsr;
597 i = bank_number(env, mode);
598 env->regs[13] = env->banked_r13[i];
599 env->regs[14] = env->banked_r14[i];
600 env->spsr = env->banked_spsr[i];
603 static void v7m_push(CPUARMState *env, uint32_t val)
606 stl_phys(env->regs[13], val);
609 static uint32_t v7m_pop(CPUARMState *env)
612 val = ldl_phys(env->regs[13]);
617 /* Switch to V7M main or process stack pointer. */
618 static void switch_v7m_sp(CPUARMState *env, int process)
621 if (env->v7m.current_sp != process) {
622 tmp = env->v7m.other_sp;
623 env->v7m.other_sp = env->regs[13];
625 env->v7m.current_sp = process;
629 static void do_v7m_exception_exit(CPUARMState *env)
634 type = env->regs[15];
635 if (env->v7m.exception != 0)
636 armv7m_nvic_complete_irq(env->nvic, env->v7m.exception);
638 /* Switch to the target stack. */
639 switch_v7m_sp(env, (type & 4) != 0);
641 env->regs[0] = v7m_pop(env);
642 env->regs[1] = v7m_pop(env);
643 env->regs[2] = v7m_pop(env);
644 env->regs[3] = v7m_pop(env);
645 env->regs[12] = v7m_pop(env);
646 env->regs[14] = v7m_pop(env);
647 env->regs[15] = v7m_pop(env);
649 xpsr_write(env, xpsr, 0xfffffdff);
650 /* Undo stack alignment. */
653 /* ??? The exception return type specifies Thread/Handler mode. However
654 this is also implied by the xPSR value. Not sure what to do
655 if there is a mismatch. */
656 /* ??? Likewise for mismatches between the CONTROL register and the stack
660 static void do_interrupt_v7m(CPUARMState *env)
662 uint32_t xpsr = xpsr_read(env);
667 if (env->v7m.current_sp)
669 if (env->v7m.exception == 0)
672 /* For exceptions we just mark as pending on the NVIC, and let that
674 /* TODO: Need to escalate if the current priority is higher than the
675 one we're raising. */
676 switch (env->exception_index) {
678 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE);
682 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SVC);
684 case EXCP_PREFETCH_ABORT:
685 case EXCP_DATA_ABORT:
686 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_MEM);
689 if (semihosting_enabled) {
691 nr = arm_lduw_code(env->regs[15], env->bswap_code) & 0xff;
694 env->regs[0] = do_arm_semihosting(env);
698 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_DEBUG);
701 env->v7m.exception = armv7m_nvic_acknowledge_irq(env->nvic);
703 case EXCP_EXCEPTION_EXIT:
704 do_v7m_exception_exit(env);
707 cpu_abort(env, "Unhandled exception 0x%x\n", env->exception_index);
708 return; /* Never happens. Keep compiler happy. */
711 /* Align stack pointer. */
712 /* ??? Should only do this if Configuration Control Register
713 STACKALIGN bit is set. */
714 if (env->regs[13] & 4) {
718 /* Switch to the handler mode. */
720 v7m_push(env, env->regs[15]);
721 v7m_push(env, env->regs[14]);
722 v7m_push(env, env->regs[12]);
723 v7m_push(env, env->regs[3]);
724 v7m_push(env, env->regs[2]);
725 v7m_push(env, env->regs[1]);
726 v7m_push(env, env->regs[0]);
727 switch_v7m_sp(env, 0);
729 env->condexec_bits = 0;
731 addr = ldl_phys(env->v7m.vecbase + env->v7m.exception * 4);
732 env->regs[15] = addr & 0xfffffffe;
733 env->thumb = addr & 1;
736 /* Handle a CPU exception. */
737 void do_interrupt(CPUARMState *env)
745 do_interrupt_v7m(env);
748 /* TODO: Vectored interrupt controller. */
749 switch (env->exception_index) {
751 new_mode = ARM_CPU_MODE_UND;
760 if (semihosting_enabled) {
761 /* Check for semihosting interrupt. */
763 mask = arm_lduw_code(env->regs[15] - 2, env->bswap_code) & 0xff;
765 mask = arm_ldl_code(env->regs[15] - 4, env->bswap_code)
768 /* Only intercept calls from privileged modes, to provide some
769 semblance of security. */
770 if (((mask == 0x123456 && !env->thumb)
771 || (mask == 0xab && env->thumb))
772 && (env->uncached_cpsr & CPSR_M) != ARM_CPU_MODE_USR) {
773 env->regs[0] = do_arm_semihosting(env);
777 new_mode = ARM_CPU_MODE_SVC;
780 /* The PC already points to the next instruction. */
784 /* See if this is a semihosting syscall. */
785 if (env->thumb && semihosting_enabled) {
786 mask = arm_lduw_code(env->regs[15], env->bswap_code) & 0xff;
788 && (env->uncached_cpsr & CPSR_M) != ARM_CPU_MODE_USR) {
790 env->regs[0] = do_arm_semihosting(env);
794 env->cp15.c5_insn = 2;
795 /* Fall through to prefetch abort. */
796 case EXCP_PREFETCH_ABORT:
797 new_mode = ARM_CPU_MODE_ABT;
799 mask = CPSR_A | CPSR_I;
802 case EXCP_DATA_ABORT:
803 new_mode = ARM_CPU_MODE_ABT;
805 mask = CPSR_A | CPSR_I;
809 new_mode = ARM_CPU_MODE_IRQ;
811 /* Disable IRQ and imprecise data aborts. */
812 mask = CPSR_A | CPSR_I;
816 new_mode = ARM_CPU_MODE_FIQ;
818 /* Disable FIQ, IRQ and imprecise data aborts. */
819 mask = CPSR_A | CPSR_I | CPSR_F;
823 cpu_abort(env, "Unhandled exception 0x%x\n", env->exception_index);
824 return; /* Never happens. Keep compiler happy. */
827 if (env->cp15.c1_sys & (1 << 13)) {
830 switch_mode (env, new_mode);
831 env->spsr = cpsr_read(env);
833 env->condexec_bits = 0;
834 /* Switch to the new mode, and to the correct instruction set. */
835 env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode;
836 env->uncached_cpsr |= mask;
837 /* this is a lie, as the was no c1_sys on V4T/V5, but who cares
838 * and we should just guard the thumb mode on V4 */
839 if (arm_feature(env, ARM_FEATURE_V4T)) {
840 env->thumb = (env->cp15.c1_sys & (1 << 30)) != 0;
842 env->regs[14] = env->regs[15] + offset;
843 env->regs[15] = addr;
844 env->interrupt_request |= CPU_INTERRUPT_EXITTB;
847 /* Check section/page access permissions.
848 Returns the page protection flags, or zero if the access is not
850 static inline int check_ap(CPUARMState *env, int ap, int domain_prot,
851 int access_type, int is_user)
855 if (domain_prot == 3) {
856 return PAGE_READ | PAGE_WRITE;
859 if (access_type == 1)
866 if (access_type == 1)
868 switch ((env->cp15.c1_sys >> 8) & 3) {
870 return is_user ? 0 : PAGE_READ;
877 return is_user ? 0 : PAGE_READ | PAGE_WRITE;
882 return PAGE_READ | PAGE_WRITE;
884 return PAGE_READ | PAGE_WRITE;
885 case 4: /* Reserved. */
888 return is_user ? 0 : prot_ro;
892 if (!arm_feature (env, ARM_FEATURE_V6K))
900 static uint32_t get_level1_table_address(CPUARMState *env, uint32_t address)
904 if (address & env->cp15.c2_mask)
905 table = env->cp15.c2_base1 & 0xffffc000;
907 table = env->cp15.c2_base0 & env->cp15.c2_base_mask;
909 table |= (address >> 18) & 0x3ffc;
913 static int get_phys_addr_v5(CPUARMState *env, uint32_t address, int access_type,
914 int is_user, uint32_t *phys_ptr, int *prot,
915 target_ulong *page_size)
926 /* Pagetable walk. */
927 /* Lookup l1 descriptor. */
928 table = get_level1_table_address(env, address);
929 desc = ldl_phys(table);
931 domain = (desc >> 5) & 0x0f;
932 domain_prot = (env->cp15.c3 >> (domain * 2)) & 3;
934 /* Section translation fault. */
938 if (domain_prot == 0 || domain_prot == 2) {
940 code = 9; /* Section domain fault. */
942 code = 11; /* Page domain fault. */
947 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
948 ap = (desc >> 10) & 3;
950 *page_size = 1024 * 1024;
952 /* Lookup l2 entry. */
954 /* Coarse pagetable. */
955 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
957 /* Fine pagetable. */
958 table = (desc & 0xfffff000) | ((address >> 8) & 0xffc);
960 desc = ldl_phys(table);
962 case 0: /* Page translation fault. */
965 case 1: /* 64k page. */
966 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
967 ap = (desc >> (4 + ((address >> 13) & 6))) & 3;
968 *page_size = 0x10000;
970 case 2: /* 4k page. */
971 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
972 ap = (desc >> (4 + ((address >> 13) & 6))) & 3;
975 case 3: /* 1k page. */
977 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
978 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
980 /* Page translation fault. */
985 phys_addr = (desc & 0xfffffc00) | (address & 0x3ff);
987 ap = (desc >> 4) & 3;
991 /* Never happens, but compiler isn't smart enough to tell. */
996 *prot = check_ap(env, ap, domain_prot, access_type, is_user);
998 /* Access permission fault. */
1002 *phys_ptr = phys_addr;
1005 return code | (domain << 4);
1008 static int get_phys_addr_v6(CPUARMState *env, uint32_t address, int access_type,
1009 int is_user, uint32_t *phys_ptr, int *prot,
1010 target_ulong *page_size)
1022 /* Pagetable walk. */
1023 /* Lookup l1 descriptor. */
1024 table = get_level1_table_address(env, address);
1025 desc = ldl_phys(table);
1028 /* Section translation fault. */
1032 } else if (type == 2 && (desc & (1 << 18))) {
1036 /* Section or page. */
1037 domain = (desc >> 5) & 0x0f;
1039 domain_prot = (env->cp15.c3 >> (domain * 2)) & 3;
1040 if (domain_prot == 0 || domain_prot == 2) {
1042 code = 9; /* Section domain fault. */
1044 code = 11; /* Page domain fault. */
1048 if (desc & (1 << 18)) {
1050 phys_addr = (desc & 0xff000000) | (address & 0x00ffffff);
1051 *page_size = 0x1000000;
1054 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
1055 *page_size = 0x100000;
1057 ap = ((desc >> 10) & 3) | ((desc >> 13) & 4);
1058 xn = desc & (1 << 4);
1061 /* Lookup l2 entry. */
1062 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
1063 desc = ldl_phys(table);
1064 ap = ((desc >> 4) & 3) | ((desc >> 7) & 4);
1066 case 0: /* Page translation fault. */
1069 case 1: /* 64k page. */
1070 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
1071 xn = desc & (1 << 15);
1072 *page_size = 0x10000;
1074 case 2: case 3: /* 4k page. */
1075 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
1077 *page_size = 0x1000;
1080 /* Never happens, but compiler isn't smart enough to tell. */
1085 if (domain_prot == 3) {
1086 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
1088 if (xn && access_type == 2)
1091 /* The simplified model uses AP[0] as an access control bit. */
1092 if ((env->cp15.c1_sys & (1 << 29)) && (ap & 1) == 0) {
1093 /* Access flag fault. */
1094 code = (code == 15) ? 6 : 3;
1097 *prot = check_ap(env, ap, domain_prot, access_type, is_user);
1099 /* Access permission fault. */
1106 *phys_ptr = phys_addr;
1109 return code | (domain << 4);
1112 static int get_phys_addr_mpu(CPUARMState *env, uint32_t address, int access_type,
1113 int is_user, uint32_t *phys_ptr, int *prot)
1119 *phys_ptr = address;
1120 for (n = 7; n >= 0; n--) {
1121 base = env->cp15.c6_region[n];
1122 if ((base & 1) == 0)
1124 mask = 1 << ((base >> 1) & 0x1f);
1125 /* Keep this shift separate from the above to avoid an
1126 (undefined) << 32. */
1127 mask = (mask << 1) - 1;
1128 if (((base ^ address) & ~mask) == 0)
1134 if (access_type == 2) {
1135 mask = env->cp15.c5_insn;
1137 mask = env->cp15.c5_data;
1139 mask = (mask >> (n * 4)) & 0xf;
1146 *prot = PAGE_READ | PAGE_WRITE;
1151 *prot |= PAGE_WRITE;
1154 *prot = PAGE_READ | PAGE_WRITE;
1165 /* Bad permission. */
1172 static inline int get_phys_addr(CPUARMState *env, uint32_t address,
1173 int access_type, int is_user,
1174 uint32_t *phys_ptr, int *prot,
1175 target_ulong *page_size)
1177 /* Fast Context Switch Extension. */
1178 if (address < 0x02000000)
1179 address += env->cp15.c13_fcse;
1181 if ((env->cp15.c1_sys & 1) == 0) {
1182 /* MMU/MPU disabled. */
1183 *phys_ptr = address;
1184 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
1185 *page_size = TARGET_PAGE_SIZE;
1187 } else if (arm_feature(env, ARM_FEATURE_MPU)) {
1188 *page_size = TARGET_PAGE_SIZE;
1189 return get_phys_addr_mpu(env, address, access_type, is_user, phys_ptr,
1191 } else if (env->cp15.c1_sys & (1 << 23)) {
1192 return get_phys_addr_v6(env, address, access_type, is_user, phys_ptr,
1195 return get_phys_addr_v5(env, address, access_type, is_user, phys_ptr,
1200 int cpu_arm_handle_mmu_fault (CPUARMState *env, target_ulong address,
1201 int access_type, int mmu_idx)
1204 target_ulong page_size;
1208 is_user = mmu_idx == MMU_USER_IDX;
1209 ret = get_phys_addr(env, address, access_type, is_user, &phys_addr, &prot,
1212 /* Map a single [sub]page. */
1213 phys_addr &= ~(uint32_t)0x3ff;
1214 address &= ~(uint32_t)0x3ff;
1215 tlb_set_page (env, address, phys_addr, prot, mmu_idx, page_size);
1219 if (access_type == 2) {
1220 env->cp15.c5_insn = ret;
1221 env->cp15.c6_insn = address;
1222 env->exception_index = EXCP_PREFETCH_ABORT;
1224 env->cp15.c5_data = ret;
1225 if (access_type == 1 && arm_feature(env, ARM_FEATURE_V6))
1226 env->cp15.c5_data |= (1 << 11);
1227 env->cp15.c6_data = address;
1228 env->exception_index = EXCP_DATA_ABORT;
1233 target_phys_addr_t cpu_get_phys_page_debug(CPUARMState *env, target_ulong addr)
1236 target_ulong page_size;
1240 ret = get_phys_addr(env, addr, 0, 0, &phys_addr, &prot, &page_size);
1248 void HELPER(set_cp)(CPUARMState *env, uint32_t insn, uint32_t val)
1250 int cp_num = (insn >> 8) & 0xf;
1251 int cp_info = (insn >> 5) & 7;
1252 int src = (insn >> 16) & 0xf;
1253 int operand = insn & 0xf;
1255 if (env->cp[cp_num].cp_write)
1256 env->cp[cp_num].cp_write(env->cp[cp_num].opaque,
1257 cp_info, src, operand, val);
1260 uint32_t HELPER(get_cp)(CPUARMState *env, uint32_t insn)
1262 int cp_num = (insn >> 8) & 0xf;
1263 int cp_info = (insn >> 5) & 7;
1264 int dest = (insn >> 16) & 0xf;
1265 int operand = insn & 0xf;
1267 if (env->cp[cp_num].cp_read)
1268 return env->cp[cp_num].cp_read(env->cp[cp_num].opaque,
1269 cp_info, dest, operand);
1273 /* Return basic MPU access permission bits. */
1274 static uint32_t simple_mpu_ap_bits(uint32_t val)
1281 for (i = 0; i < 16; i += 2) {
1282 ret |= (val >> i) & mask;
1288 /* Pad basic MPU access permission bits to extended format. */
1289 static uint32_t extended_mpu_ap_bits(uint32_t val)
1296 for (i = 0; i < 16; i += 2) {
1297 ret |= (val & mask) << i;
1303 void HELPER(set_cp15)(CPUARMState *env, uint32_t insn, uint32_t val)
1309 op1 = (insn >> 21) & 7;
1310 op2 = (insn >> 5) & 7;
1312 switch ((insn >> 16) & 0xf) {
1315 if (arm_feature(env, ARM_FEATURE_XSCALE))
1317 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1319 if (arm_feature(env, ARM_FEATURE_V7)
1320 && op1 == 2 && crm == 0 && op2 == 0) {
1321 env->cp15.c0_cssel = val & 0xf;
1325 case 1: /* System configuration. */
1326 if (arm_feature(env, ARM_FEATURE_V7)
1327 && op1 == 0 && crm == 1 && op2 == 0) {
1328 env->cp15.c1_scr = val;
1331 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1335 if (!arm_feature(env, ARM_FEATURE_XSCALE) || crm == 0)
1336 env->cp15.c1_sys = val;
1337 /* ??? Lots of these bits are not implemented. */
1338 /* This may enable/disable the MMU, so do a TLB flush. */
1341 case 1: /* Auxiliary control register. */
1342 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
1343 env->cp15.c1_xscaleauxcr = val;
1346 /* Not implemented. */
1349 if (arm_feature(env, ARM_FEATURE_XSCALE))
1351 if (env->cp15.c1_coproc != val) {
1352 env->cp15.c1_coproc = val;
1353 /* ??? Is this safe when called from within a TB? */
1361 case 2: /* MMU Page table control / MPU cache control. */
1362 if (arm_feature(env, ARM_FEATURE_MPU)) {
1365 env->cp15.c2_data = val;
1368 env->cp15.c2_insn = val;
1376 env->cp15.c2_base0 = val;
1379 env->cp15.c2_base1 = val;
1383 env->cp15.c2_control = val;
1384 env->cp15.c2_mask = ~(((uint32_t)0xffffffffu) >> val);
1385 env->cp15.c2_base_mask = ~((uint32_t)0x3fffu >> val);
1392 case 3: /* MMU Domain access control / MPU write buffer control. */
1394 tlb_flush(env, 1); /* Flush TLB as domain not tracked in TLB */
1396 case 4: /* Reserved. */
1398 case 5: /* MMU Fault status / MPU access permission. */
1399 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1403 if (arm_feature(env, ARM_FEATURE_MPU))
1404 val = extended_mpu_ap_bits(val);
1405 env->cp15.c5_data = val;
1408 if (arm_feature(env, ARM_FEATURE_MPU))
1409 val = extended_mpu_ap_bits(val);
1410 env->cp15.c5_insn = val;
1413 if (!arm_feature(env, ARM_FEATURE_MPU))
1415 env->cp15.c5_data = val;
1418 if (!arm_feature(env, ARM_FEATURE_MPU))
1420 env->cp15.c5_insn = val;
1426 case 6: /* MMU Fault address / MPU base/size. */
1427 if (arm_feature(env, ARM_FEATURE_MPU)) {
1430 env->cp15.c6_region[crm] = val;
1432 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1436 env->cp15.c6_data = val;
1438 case 1: /* ??? This is WFAR on armv6 */
1440 env->cp15.c6_insn = val;
1447 case 7: /* Cache control. */
1448 env->cp15.c15_i_max = 0x000;
1449 env->cp15.c15_i_min = 0xff0;
1453 /* No cache, so nothing to do except VA->PA translations. */
1454 if (arm_feature(env, ARM_FEATURE_VAPA)) {
1457 if (arm_feature(env, ARM_FEATURE_V7)) {
1458 env->cp15.c7_par = val & 0xfffff6ff;
1460 env->cp15.c7_par = val & 0xfffff1ff;
1465 target_ulong page_size;
1467 int ret, is_user = op2 & 2;
1468 int access_type = op2 & 1;
1471 /* Other states are only available with TrustZone */
1474 ret = get_phys_addr(env, val, access_type, is_user,
1475 &phys_addr, &prot, &page_size);
1477 /* We do not set any attribute bits in the PAR */
1478 if (page_size == (1 << 24)
1479 && arm_feature(env, ARM_FEATURE_V7)) {
1480 env->cp15.c7_par = (phys_addr & 0xff000000) | 1 << 1;
1482 env->cp15.c7_par = phys_addr & 0xfffff000;
1485 env->cp15.c7_par = ((ret & (10 << 1)) >> 5) |
1486 ((ret & (12 << 1)) >> 6) |
1487 ((ret & 0xf) << 1) | 1;
1494 case 8: /* MMU TLB control. */
1496 case 0: /* Invalidate all (TLBIALL) */
1499 case 1: /* Invalidate single TLB entry by MVA and ASID (TLBIMVA) */
1500 tlb_flush_page(env, val & TARGET_PAGE_MASK);
1502 case 2: /* Invalidate by ASID (TLBIASID) */
1503 tlb_flush(env, val == 0);
1505 case 3: /* Invalidate single entry by MVA, all ASIDs (TLBIMVAA) */
1506 tlb_flush_page(env, val & TARGET_PAGE_MASK);
1513 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1515 if (arm_feature(env, ARM_FEATURE_STRONGARM))
1516 break; /* Ignore ReadBuffer access */
1518 case 0: /* Cache lockdown. */
1520 case 0: /* L1 cache. */
1523 env->cp15.c9_data = val;
1526 env->cp15.c9_insn = val;
1532 case 1: /* L2 cache. */
1533 /* Ignore writes to L2 lockdown/auxiliary registers. */
1539 case 1: /* TCM memory region registers. */
1540 /* Not implemented. */
1542 case 12: /* Performance monitor control */
1543 /* Performance monitors are implementation defined in v7,
1544 * but with an ARM recommended set of registers, which we
1545 * follow (although we don't actually implement any counters)
1547 if (!arm_feature(env, ARM_FEATURE_V7)) {
1551 case 0: /* performance monitor control register */
1552 /* only the DP, X, D and E bits are writable */
1553 env->cp15.c9_pmcr &= ~0x39;
1554 env->cp15.c9_pmcr |= (val & 0x39);
1556 case 1: /* Count enable set register */
1558 env->cp15.c9_pmcnten |= val;
1560 case 2: /* Count enable clear */
1562 env->cp15.c9_pmcnten &= ~val;
1564 case 3: /* Overflow flag status */
1565 env->cp15.c9_pmovsr &= ~val;
1567 case 4: /* Software increment */
1568 /* RAZ/WI since we don't implement the software-count event */
1570 case 5: /* Event counter selection register */
1571 /* Since we don't implement any events, writing to this register
1572 * is actually UNPREDICTABLE. So we choose to RAZ/WI.
1579 case 13: /* Performance counters */
1580 if (!arm_feature(env, ARM_FEATURE_V7)) {
1584 case 0: /* Cycle count register: not implemented, so RAZ/WI */
1586 case 1: /* Event type select */
1587 env->cp15.c9_pmxevtyper = val & 0xff;
1589 case 2: /* Event count register */
1590 /* Unimplemented (we have no events), RAZ/WI */
1596 case 14: /* Performance monitor control */
1597 if (!arm_feature(env, ARM_FEATURE_V7)) {
1601 case 0: /* user enable */
1602 env->cp15.c9_pmuserenr = val & 1;
1603 /* changes access rights for cp registers, so flush tbs */
1606 case 1: /* interrupt enable set */
1607 /* We have no event counters so only the C bit can be changed */
1609 env->cp15.c9_pminten |= val;
1611 case 2: /* interrupt enable clear */
1613 env->cp15.c9_pminten &= ~val;
1621 case 10: /* MMU TLB lockdown. */
1622 /* ??? TLB lockdown not implemented. */
1624 case 12: /* Reserved. */
1626 case 13: /* Process ID. */
1629 /* Unlike real hardware the qemu TLB uses virtual addresses,
1630 not modified virtual addresses, so this causes a TLB flush.
1632 if (env->cp15.c13_fcse != val)
1634 env->cp15.c13_fcse = val;
1637 /* This changes the ASID, so do a TLB flush. */
1638 if (env->cp15.c13_context != val
1639 && !arm_feature(env, ARM_FEATURE_MPU))
1641 env->cp15.c13_context = val;
1647 case 14: /* Generic timer */
1648 if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) {
1649 /* Dummy implementation: RAZ/WI for all */
1653 case 15: /* Implementation specific. */
1654 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
1655 if (op2 == 0 && crm == 1) {
1656 if (env->cp15.c15_cpar != (val & 0x3fff)) {
1657 /* Changes cp0 to cp13 behavior, so needs a TB flush. */
1659 env->cp15.c15_cpar = val & 0x3fff;
1665 if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
1669 case 1: /* Set TI925T configuration. */
1670 env->cp15.c15_ticonfig = val & 0xe7;
1671 env->cp15.c0_cpuid = (val & (1 << 5)) ? /* OS_TYPE bit */
1672 ARM_CPUID_TI915T : ARM_CPUID_TI925T;
1674 case 2: /* Set I_max. */
1675 env->cp15.c15_i_max = val;
1677 case 3: /* Set I_min. */
1678 env->cp15.c15_i_min = val;
1680 case 4: /* Set thread-ID. */
1681 env->cp15.c15_threadid = val & 0xffff;
1683 case 8: /* Wait-for-interrupt (deprecated). */
1684 cpu_interrupt(env, CPU_INTERRUPT_HALT);
1690 if (ARM_CPUID(env) == ARM_CPUID_CORTEXA9) {
1693 if ((op1 == 0) && (op2 == 0)) {
1694 env->cp15.c15_power_control = val;
1695 } else if ((op1 == 0) && (op2 == 1)) {
1696 env->cp15.c15_diagnostic = val;
1697 } else if ((op1 == 0) && (op2 == 2)) {
1698 env->cp15.c15_power_diagnostic = val;
1708 /* ??? For debugging only. Should raise illegal instruction exception. */
1709 cpu_abort(env, "Unimplemented cp15 register write (c%d, c%d, {%d, %d})\n",
1710 (insn >> 16) & 0xf, crm, op1, op2);
1713 uint32_t HELPER(get_cp15)(CPUARMState *env, uint32_t insn)
1719 op1 = (insn >> 21) & 7;
1720 op2 = (insn >> 5) & 7;
1722 switch ((insn >> 16) & 0xf) {
1723 case 0: /* ID codes. */
1729 case 0: /* Device ID. */
1730 return env->cp15.c0_cpuid;
1731 case 1: /* Cache Type. */
1732 return env->cp15.c0_cachetype;
1733 case 2: /* TCM status. */
1735 case 3: /* TLB type register. */
1736 return 0; /* No lockable TLB entries. */
1738 /* The MPIDR was standardised in v7; prior to
1739 * this it was implemented only in the 11MPCore.
1740 * For all other pre-v7 cores it does not exist.
1742 if (arm_feature(env, ARM_FEATURE_V7) ||
1743 ARM_CPUID(env) == ARM_CPUID_ARM11MPCORE) {
1744 int mpidr = env->cpu_index;
1745 /* We don't support setting cluster ID ([8..11])
1746 * so these bits always RAZ.
1748 if (arm_feature(env, ARM_FEATURE_V7MP)) {
1750 /* Cores which are uniprocessor (non-coherent)
1751 * but still implement the MP extensions set
1752 * bit 30. (For instance, A9UP.) However we do
1753 * not currently model any of those cores.
1758 /* otherwise fall through to the unimplemented-reg case */
1763 if (!arm_feature(env, ARM_FEATURE_V6))
1765 return env->cp15.c0_c1[op2];
1767 if (!arm_feature(env, ARM_FEATURE_V6))
1769 return env->cp15.c0_c2[op2];
1770 case 3: case 4: case 5: case 6: case 7:
1776 /* These registers aren't documented on arm11 cores. However
1777 Linux looks at them anyway. */
1778 if (!arm_feature(env, ARM_FEATURE_V6))
1782 if (!arm_feature(env, ARM_FEATURE_V7))
1787 return env->cp15.c0_ccsid[env->cp15.c0_cssel];
1789 return env->cp15.c0_clid;
1795 if (op2 != 0 || crm != 0)
1797 return env->cp15.c0_cssel;
1801 case 1: /* System configuration. */
1802 if (arm_feature(env, ARM_FEATURE_V7)
1803 && op1 == 0 && crm == 1 && op2 == 0) {
1804 return env->cp15.c1_scr;
1806 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1809 case 0: /* Control register. */
1810 return env->cp15.c1_sys;
1811 case 1: /* Auxiliary control register. */
1812 if (arm_feature(env, ARM_FEATURE_XSCALE))
1813 return env->cp15.c1_xscaleauxcr;
1814 if (!arm_feature(env, ARM_FEATURE_AUXCR))
1816 switch (ARM_CPUID(env)) {
1817 case ARM_CPUID_ARM1026:
1819 case ARM_CPUID_ARM1136:
1820 case ARM_CPUID_ARM1136_R2:
1821 case ARM_CPUID_ARM1176:
1823 case ARM_CPUID_ARM11MPCORE:
1825 case ARM_CPUID_CORTEXA8:
1827 case ARM_CPUID_CORTEXA9:
1828 case ARM_CPUID_CORTEXA15:
1833 case 2: /* Coprocessor access register. */
1834 if (arm_feature(env, ARM_FEATURE_XSCALE))
1836 return env->cp15.c1_coproc;
1840 case 2: /* MMU Page table control / MPU cache control. */
1841 if (arm_feature(env, ARM_FEATURE_MPU)) {
1844 return env->cp15.c2_data;
1847 return env->cp15.c2_insn;
1855 return env->cp15.c2_base0;
1857 return env->cp15.c2_base1;
1859 return env->cp15.c2_control;
1864 case 3: /* MMU Domain access control / MPU write buffer control. */
1865 return env->cp15.c3;
1866 case 4: /* Reserved. */
1868 case 5: /* MMU Fault status / MPU access permission. */
1869 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1873 if (arm_feature(env, ARM_FEATURE_MPU))
1874 return simple_mpu_ap_bits(env->cp15.c5_data);
1875 return env->cp15.c5_data;
1877 if (arm_feature(env, ARM_FEATURE_MPU))
1878 return simple_mpu_ap_bits(env->cp15.c5_insn);
1879 return env->cp15.c5_insn;
1881 if (!arm_feature(env, ARM_FEATURE_MPU))
1883 return env->cp15.c5_data;
1885 if (!arm_feature(env, ARM_FEATURE_MPU))
1887 return env->cp15.c5_insn;
1891 case 6: /* MMU Fault address. */
1892 if (arm_feature(env, ARM_FEATURE_MPU)) {
1895 return env->cp15.c6_region[crm];
1897 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1901 return env->cp15.c6_data;
1903 if (arm_feature(env, ARM_FEATURE_V6)) {
1904 /* Watchpoint Fault Adrress. */
1905 return 0; /* Not implemented. */
1907 /* Instruction Fault Adrress. */
1908 /* Arm9 doesn't have an IFAR, but implementing it anyway
1909 shouldn't do any harm. */
1910 return env->cp15.c6_insn;
1913 if (arm_feature(env, ARM_FEATURE_V6)) {
1914 /* Instruction Fault Adrress. */
1915 return env->cp15.c6_insn;
1923 case 7: /* Cache control. */
1924 if (crm == 4 && op1 == 0 && op2 == 0) {
1925 return env->cp15.c7_par;
1927 /* FIXME: Should only clear Z flag if destination is r15. */
1930 case 8: /* MMU TLB control. */
1934 case 0: /* Cache lockdown */
1936 case 0: /* L1 cache. */
1937 if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
1942 return env->cp15.c9_data;
1944 return env->cp15.c9_insn;
1948 case 1: /* L2 cache */
1949 /* L2 Lockdown and Auxiliary control. */
1952 /* L2 cache lockdown (A8 only) */
1955 /* L2 cache auxiliary control (A8) or control (A15) */
1956 if (ARM_CPUID(env) == ARM_CPUID_CORTEXA15) {
1957 /* Linux wants the number of processors from here.
1958 * Might as well set the interrupt-controller bit too.
1960 return ((smp_cpus - 1) << 24) | (1 << 23);
1964 /* L2 cache extended control (A15) */
1973 case 12: /* Performance monitor control */
1974 if (!arm_feature(env, ARM_FEATURE_V7)) {
1978 case 0: /* performance monitor control register */
1979 return env->cp15.c9_pmcr;
1980 case 1: /* count enable set */
1981 case 2: /* count enable clear */
1982 return env->cp15.c9_pmcnten;
1983 case 3: /* overflow flag status */
1984 return env->cp15.c9_pmovsr;
1985 case 4: /* software increment */
1986 case 5: /* event counter selection register */
1987 return 0; /* Unimplemented, RAZ/WI */
1991 case 13: /* Performance counters */
1992 if (!arm_feature(env, ARM_FEATURE_V7)) {
1996 case 1: /* Event type select */
1997 return env->cp15.c9_pmxevtyper;
1998 case 0: /* Cycle count register */
1999 case 2: /* Event count register */
2000 /* Unimplemented, so RAZ/WI */
2005 case 14: /* Performance monitor control */
2006 if (!arm_feature(env, ARM_FEATURE_V7)) {
2010 case 0: /* user enable */
2011 return env->cp15.c9_pmuserenr;
2012 case 1: /* interrupt enable set */
2013 case 2: /* interrupt enable clear */
2014 return env->cp15.c9_pminten;
2022 case 10: /* MMU TLB lockdown. */
2023 /* ??? TLB lockdown not implemented. */
2025 case 11: /* TCM DMA control. */
2026 case 12: /* Reserved. */
2028 case 13: /* Process ID. */
2031 return env->cp15.c13_fcse;
2033 return env->cp15.c13_context;
2037 case 14: /* Generic timer */
2038 if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) {
2039 /* Dummy implementation: RAZ/WI for all */
2043 case 15: /* Implementation specific. */
2044 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
2045 if (op2 == 0 && crm == 1)
2046 return env->cp15.c15_cpar;
2050 if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
2054 case 1: /* Read TI925T configuration. */
2055 return env->cp15.c15_ticonfig;
2056 case 2: /* Read I_max. */
2057 return env->cp15.c15_i_max;
2058 case 3: /* Read I_min. */
2059 return env->cp15.c15_i_min;
2060 case 4: /* Read thread-ID. */
2061 return env->cp15.c15_threadid;
2062 case 8: /* TI925T_status */
2065 /* TODO: Peripheral port remap register:
2066 * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt
2067 * controller base address at $rn & ~0xfff and map size of
2068 * 0x200 << ($rn & 0xfff), when MMU is off. */
2071 if (ARM_CPUID(env) == ARM_CPUID_CORTEXA9) {
2074 if ((op1 == 4) && (op2 == 0)) {
2075 /* The config_base_address should hold the value of
2076 * the peripheral base. ARM should get this from a CPU
2077 * object property, but that support isn't available in
2078 * December 2011. Default to 0 for now and board models
2079 * that care can set it by a private hook */
2080 return env->cp15.c15_config_base_address;
2081 } else if ((op1 == 0) && (op2 == 0)) {
2082 /* power_control should be set to maximum latency. Again,
2083 default to 0 and set by private hook */
2084 return env->cp15.c15_power_control;
2085 } else if ((op1 == 0) && (op2 == 1)) {
2086 return env->cp15.c15_diagnostic;
2087 } else if ((op1 == 0) && (op2 == 2)) {
2088 return env->cp15.c15_power_diagnostic;
2091 case 1: /* NEON Busy */
2093 case 5: /* tlb lockdown */
2096 if ((op1 == 5) && (op2 == 2)) {
2108 /* ??? For debugging only. Should raise illegal instruction exception. */
2109 cpu_abort(env, "Unimplemented cp15 register read (c%d, c%d, {%d, %d})\n",
2110 (insn >> 16) & 0xf, crm, op1, op2);
2114 void HELPER(set_r13_banked)(CPUARMState *env, uint32_t mode, uint32_t val)
2116 if ((env->uncached_cpsr & CPSR_M) == mode) {
2117 env->regs[13] = val;
2119 env->banked_r13[bank_number(env, mode)] = val;
2123 uint32_t HELPER(get_r13_banked)(CPUARMState *env, uint32_t mode)
2125 if ((env->uncached_cpsr & CPSR_M) == mode) {
2126 return env->regs[13];
2128 return env->banked_r13[bank_number(env, mode)];
2132 uint32_t HELPER(v7m_mrs)(CPUARMState *env, uint32_t reg)
2136 return xpsr_read(env) & 0xf8000000;
2138 return xpsr_read(env) & 0xf80001ff;
2140 return xpsr_read(env) & 0xff00fc00;
2142 return xpsr_read(env) & 0xff00fdff;
2144 return xpsr_read(env) & 0x000001ff;
2146 return xpsr_read(env) & 0x0700fc00;
2148 return xpsr_read(env) & 0x0700edff;
2150 return env->v7m.current_sp ? env->v7m.other_sp : env->regs[13];
2152 return env->v7m.current_sp ? env->regs[13] : env->v7m.other_sp;
2153 case 16: /* PRIMASK */
2154 return (env->uncached_cpsr & CPSR_I) != 0;
2155 case 17: /* BASEPRI */
2156 case 18: /* BASEPRI_MAX */
2157 return env->v7m.basepri;
2158 case 19: /* FAULTMASK */
2159 return (env->uncached_cpsr & CPSR_F) != 0;
2160 case 20: /* CONTROL */
2161 return env->v7m.control;
2163 /* ??? For debugging only. */
2164 cpu_abort(env, "Unimplemented system register read (%d)\n", reg);
2169 void HELPER(v7m_msr)(CPUARMState *env, uint32_t reg, uint32_t val)
2173 xpsr_write(env, val, 0xf8000000);
2176 xpsr_write(env, val, 0xf8000000);
2179 xpsr_write(env, val, 0xfe00fc00);
2182 xpsr_write(env, val, 0xfe00fc00);
2185 /* IPSR bits are readonly. */
2188 xpsr_write(env, val, 0x0600fc00);
2191 xpsr_write(env, val, 0x0600fc00);
2194 if (env->v7m.current_sp)
2195 env->v7m.other_sp = val;
2197 env->regs[13] = val;
2200 if (env->v7m.current_sp)
2201 env->regs[13] = val;
2203 env->v7m.other_sp = val;
2205 case 16: /* PRIMASK */
2207 env->uncached_cpsr |= CPSR_I;
2209 env->uncached_cpsr &= ~CPSR_I;
2211 case 17: /* BASEPRI */
2212 env->v7m.basepri = val & 0xff;
2214 case 18: /* BASEPRI_MAX */
2216 if (val != 0 && (val < env->v7m.basepri || env->v7m.basepri == 0))
2217 env->v7m.basepri = val;
2219 case 19: /* FAULTMASK */
2221 env->uncached_cpsr |= CPSR_F;
2223 env->uncached_cpsr &= ~CPSR_F;
2225 case 20: /* CONTROL */
2226 env->v7m.control = val & 3;
2227 switch_v7m_sp(env, (val & 2) != 0);
2230 /* ??? For debugging only. */
2231 cpu_abort(env, "Unimplemented system register write (%d)\n", reg);
2236 void cpu_arm_set_cp_io(CPUARMState *env, int cpnum,
2237 ARMReadCPFunc *cp_read, ARMWriteCPFunc *cp_write,
2240 if (cpnum < 0 || cpnum > 14) {
2241 cpu_abort(env, "Bad coprocessor number: %i\n", cpnum);
2245 env->cp[cpnum].cp_read = cp_read;
2246 env->cp[cpnum].cp_write = cp_write;
2247 env->cp[cpnum].opaque = opaque;
2252 /* Note that signed overflow is undefined in C. The following routines are
2253 careful to use unsigned types where modulo arithmetic is required.
2254 Failure to do so _will_ break on newer gcc. */
2256 /* Signed saturating arithmetic. */
2258 /* Perform 16-bit signed saturating addition. */
2259 static inline uint16_t add16_sat(uint16_t a, uint16_t b)
2264 if (((res ^ a) & 0x8000) && !((a ^ b) & 0x8000)) {
2273 /* Perform 8-bit signed saturating addition. */
2274 static inline uint8_t add8_sat(uint8_t a, uint8_t b)
2279 if (((res ^ a) & 0x80) && !((a ^ b) & 0x80)) {
2288 /* Perform 16-bit signed saturating subtraction. */
2289 static inline uint16_t sub16_sat(uint16_t a, uint16_t b)
2294 if (((res ^ a) & 0x8000) && ((a ^ b) & 0x8000)) {
2303 /* Perform 8-bit signed saturating subtraction. */
2304 static inline uint8_t sub8_sat(uint8_t a, uint8_t b)
2309 if (((res ^ a) & 0x80) && ((a ^ b) & 0x80)) {
2318 #define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16);
2319 #define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16);
2320 #define ADD8(a, b, n) RESULT(add8_sat(a, b), n, 8);
2321 #define SUB8(a, b, n) RESULT(sub8_sat(a, b), n, 8);
2324 #include "op_addsub.h"
2326 /* Unsigned saturating arithmetic. */
2327 static inline uint16_t add16_usat(uint16_t a, uint16_t b)
2336 static inline uint16_t sub16_usat(uint16_t a, uint16_t b)
2344 static inline uint8_t add8_usat(uint8_t a, uint8_t b)
2353 static inline uint8_t sub8_usat(uint8_t a, uint8_t b)
2361 #define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16);
2362 #define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16);
2363 #define ADD8(a, b, n) RESULT(add8_usat(a, b), n, 8);
2364 #define SUB8(a, b, n) RESULT(sub8_usat(a, b), n, 8);
2367 #include "op_addsub.h"
2369 /* Signed modulo arithmetic. */
2370 #define SARITH16(a, b, n, op) do { \
2372 sum = (int32_t)(int16_t)(a) op (int32_t)(int16_t)(b); \
2373 RESULT(sum, n, 16); \
2375 ge |= 3 << (n * 2); \
2378 #define SARITH8(a, b, n, op) do { \
2380 sum = (int32_t)(int8_t)(a) op (int32_t)(int8_t)(b); \
2381 RESULT(sum, n, 8); \
2387 #define ADD16(a, b, n) SARITH16(a, b, n, +)
2388 #define SUB16(a, b, n) SARITH16(a, b, n, -)
2389 #define ADD8(a, b, n) SARITH8(a, b, n, +)
2390 #define SUB8(a, b, n) SARITH8(a, b, n, -)
2394 #include "op_addsub.h"
2396 /* Unsigned modulo arithmetic. */
2397 #define ADD16(a, b, n) do { \
2399 sum = (uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b); \
2400 RESULT(sum, n, 16); \
2401 if ((sum >> 16) == 1) \
2402 ge |= 3 << (n * 2); \
2405 #define ADD8(a, b, n) do { \
2407 sum = (uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b); \
2408 RESULT(sum, n, 8); \
2409 if ((sum >> 8) == 1) \
2413 #define SUB16(a, b, n) do { \
2415 sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \
2416 RESULT(sum, n, 16); \
2417 if ((sum >> 16) == 0) \
2418 ge |= 3 << (n * 2); \
2421 #define SUB8(a, b, n) do { \
2423 sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \
2424 RESULT(sum, n, 8); \
2425 if ((sum >> 8) == 0) \
2432 #include "op_addsub.h"
2434 /* Halved signed arithmetic. */
2435 #define ADD16(a, b, n) \
2436 RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16)
2437 #define SUB16(a, b, n) \
2438 RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16)
2439 #define ADD8(a, b, n) \
2440 RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8)
2441 #define SUB8(a, b, n) \
2442 RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8)
2445 #include "op_addsub.h"
2447 /* Halved unsigned arithmetic. */
2448 #define ADD16(a, b, n) \
2449 RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16)
2450 #define SUB16(a, b, n) \
2451 RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16)
2452 #define ADD8(a, b, n) \
2453 RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8)
2454 #define SUB8(a, b, n) \
2455 RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8)
2458 #include "op_addsub.h"
2460 static inline uint8_t do_usad(uint8_t a, uint8_t b)
2468 /* Unsigned sum of absolute byte differences. */
2469 uint32_t HELPER(usad8)(uint32_t a, uint32_t b)
2472 sum = do_usad(a, b);
2473 sum += do_usad(a >> 8, b >> 8);
2474 sum += do_usad(a >> 16, b >>16);
2475 sum += do_usad(a >> 24, b >> 24);
2479 /* For ARMv6 SEL instruction. */
2480 uint32_t HELPER(sel_flags)(uint32_t flags, uint32_t a, uint32_t b)
2493 return (a & mask) | (b & ~mask);
2496 uint32_t HELPER(logicq_cc)(uint64_t val)
2498 return (val >> 32) | (val != 0);
2501 /* VFP support. We follow the convention used for VFP instrunctions:
2502 Single precition routines have a "s" suffix, double precision a
2505 /* Convert host exception flags to vfp form. */
2506 static inline int vfp_exceptbits_from_host(int host_bits)
2508 int target_bits = 0;
2510 if (host_bits & float_flag_invalid)
2512 if (host_bits & float_flag_divbyzero)
2514 if (host_bits & float_flag_overflow)
2516 if (host_bits & (float_flag_underflow | float_flag_output_denormal))
2518 if (host_bits & float_flag_inexact)
2519 target_bits |= 0x10;
2520 if (host_bits & float_flag_input_denormal)
2521 target_bits |= 0x80;
2525 uint32_t HELPER(vfp_get_fpscr)(CPUARMState *env)
2530 fpscr = (env->vfp.xregs[ARM_VFP_FPSCR] & 0xffc8ffff)
2531 | (env->vfp.vec_len << 16)
2532 | (env->vfp.vec_stride << 20);
2533 i = get_float_exception_flags(&env->vfp.fp_status);
2534 i |= get_float_exception_flags(&env->vfp.standard_fp_status);
2535 fpscr |= vfp_exceptbits_from_host(i);
2539 uint32_t vfp_get_fpscr(CPUARMState *env)
2541 return HELPER(vfp_get_fpscr)(env);
2544 /* Convert vfp exception flags to target form. */
2545 static inline int vfp_exceptbits_to_host(int target_bits)
2549 if (target_bits & 1)
2550 host_bits |= float_flag_invalid;
2551 if (target_bits & 2)
2552 host_bits |= float_flag_divbyzero;
2553 if (target_bits & 4)
2554 host_bits |= float_flag_overflow;
2555 if (target_bits & 8)
2556 host_bits |= float_flag_underflow;
2557 if (target_bits & 0x10)
2558 host_bits |= float_flag_inexact;
2559 if (target_bits & 0x80)
2560 host_bits |= float_flag_input_denormal;
2564 void HELPER(vfp_set_fpscr)(CPUARMState *env, uint32_t val)
2569 changed = env->vfp.xregs[ARM_VFP_FPSCR];
2570 env->vfp.xregs[ARM_VFP_FPSCR] = (val & 0xffc8ffff);
2571 env->vfp.vec_len = (val >> 16) & 7;
2572 env->vfp.vec_stride = (val >> 20) & 3;
2575 if (changed & (3 << 22)) {
2576 i = (val >> 22) & 3;
2579 i = float_round_nearest_even;
2585 i = float_round_down;
2588 i = float_round_to_zero;
2591 set_float_rounding_mode(i, &env->vfp.fp_status);
2593 if (changed & (1 << 24)) {
2594 set_flush_to_zero((val & (1 << 24)) != 0, &env->vfp.fp_status);
2595 set_flush_inputs_to_zero((val & (1 << 24)) != 0, &env->vfp.fp_status);
2597 if (changed & (1 << 25))
2598 set_default_nan_mode((val & (1 << 25)) != 0, &env->vfp.fp_status);
2600 i = vfp_exceptbits_to_host(val);
2601 set_float_exception_flags(i, &env->vfp.fp_status);
2602 set_float_exception_flags(0, &env->vfp.standard_fp_status);
2605 void vfp_set_fpscr(CPUARMState *env, uint32_t val)
2607 HELPER(vfp_set_fpscr)(env, val);
2610 #define VFP_HELPER(name, p) HELPER(glue(glue(vfp_,name),p))
2612 #define VFP_BINOP(name) \
2613 float32 VFP_HELPER(name, s)(float32 a, float32 b, void *fpstp) \
2615 float_status *fpst = fpstp; \
2616 return float32_ ## name(a, b, fpst); \
2618 float64 VFP_HELPER(name, d)(float64 a, float64 b, void *fpstp) \
2620 float_status *fpst = fpstp; \
2621 return float64_ ## name(a, b, fpst); \
2629 float32 VFP_HELPER(neg, s)(float32 a)
2631 return float32_chs(a);
2634 float64 VFP_HELPER(neg, d)(float64 a)
2636 return float64_chs(a);
2639 float32 VFP_HELPER(abs, s)(float32 a)
2641 return float32_abs(a);
2644 float64 VFP_HELPER(abs, d)(float64 a)
2646 return float64_abs(a);
2649 float32 VFP_HELPER(sqrt, s)(float32 a, CPUARMState *env)
2651 return float32_sqrt(a, &env->vfp.fp_status);
2654 float64 VFP_HELPER(sqrt, d)(float64 a, CPUARMState *env)
2656 return float64_sqrt(a, &env->vfp.fp_status);
2659 /* XXX: check quiet/signaling case */
2660 #define DO_VFP_cmp(p, type) \
2661 void VFP_HELPER(cmp, p)(type a, type b, CPUARMState *env) \
2664 switch(type ## _compare_quiet(a, b, &env->vfp.fp_status)) { \
2665 case 0: flags = 0x6; break; \
2666 case -1: flags = 0x8; break; \
2667 case 1: flags = 0x2; break; \
2668 default: case 2: flags = 0x3; break; \
2670 env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \
2671 | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \
2673 void VFP_HELPER(cmpe, p)(type a, type b, CPUARMState *env) \
2676 switch(type ## _compare(a, b, &env->vfp.fp_status)) { \
2677 case 0: flags = 0x6; break; \
2678 case -1: flags = 0x8; break; \
2679 case 1: flags = 0x2; break; \
2680 default: case 2: flags = 0x3; break; \
2682 env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \
2683 | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \
2685 DO_VFP_cmp(s, float32)
2686 DO_VFP_cmp(d, float64)
2689 /* Integer to float and float to integer conversions */
2691 #define CONV_ITOF(name, fsz, sign) \
2692 float##fsz HELPER(name)(uint32_t x, void *fpstp) \
2694 float_status *fpst = fpstp; \
2695 return sign##int32_to_##float##fsz((sign##int32_t)x, fpst); \
2698 #define CONV_FTOI(name, fsz, sign, round) \
2699 uint32_t HELPER(name)(float##fsz x, void *fpstp) \
2701 float_status *fpst = fpstp; \
2702 if (float##fsz##_is_any_nan(x)) { \
2703 float_raise(float_flag_invalid, fpst); \
2706 return float##fsz##_to_##sign##int32##round(x, fpst); \
2709 #define FLOAT_CONVS(name, p, fsz, sign) \
2710 CONV_ITOF(vfp_##name##to##p, fsz, sign) \
2711 CONV_FTOI(vfp_to##name##p, fsz, sign, ) \
2712 CONV_FTOI(vfp_to##name##z##p, fsz, sign, _round_to_zero)
2714 FLOAT_CONVS(si, s, 32, )
2715 FLOAT_CONVS(si, d, 64, )
2716 FLOAT_CONVS(ui, s, 32, u)
2717 FLOAT_CONVS(ui, d, 64, u)
2723 /* floating point conversion */
2724 float64 VFP_HELPER(fcvtd, s)(float32 x, CPUARMState *env)
2726 float64 r = float32_to_float64(x, &env->vfp.fp_status);
2727 /* ARM requires that S<->D conversion of any kind of NaN generates
2728 * a quiet NaN by forcing the most significant frac bit to 1.
2730 return float64_maybe_silence_nan(r);
2733 float32 VFP_HELPER(fcvts, d)(float64 x, CPUARMState *env)
2735 float32 r = float64_to_float32(x, &env->vfp.fp_status);
2736 /* ARM requires that S<->D conversion of any kind of NaN generates
2737 * a quiet NaN by forcing the most significant frac bit to 1.
2739 return float32_maybe_silence_nan(r);
2742 /* VFP3 fixed point conversion. */
2743 #define VFP_CONV_FIX(name, p, fsz, itype, sign) \
2744 float##fsz HELPER(vfp_##name##to##p)(uint##fsz##_t x, uint32_t shift, \
2747 float_status *fpst = fpstp; \
2749 tmp = sign##int32_to_##float##fsz((itype##_t)x, fpst); \
2750 return float##fsz##_scalbn(tmp, -(int)shift, fpst); \
2752 uint##fsz##_t HELPER(vfp_to##name##p)(float##fsz x, uint32_t shift, \
2755 float_status *fpst = fpstp; \
2757 if (float##fsz##_is_any_nan(x)) { \
2758 float_raise(float_flag_invalid, fpst); \
2761 tmp = float##fsz##_scalbn(x, shift, fpst); \
2762 return float##fsz##_to_##itype##_round_to_zero(tmp, fpst); \
2765 VFP_CONV_FIX(sh, d, 64, int16, )
2766 VFP_CONV_FIX(sl, d, 64, int32, )
2767 VFP_CONV_FIX(uh, d, 64, uint16, u)
2768 VFP_CONV_FIX(ul, d, 64, uint32, u)
2769 VFP_CONV_FIX(sh, s, 32, int16, )
2770 VFP_CONV_FIX(sl, s, 32, int32, )
2771 VFP_CONV_FIX(uh, s, 32, uint16, u)
2772 VFP_CONV_FIX(ul, s, 32, uint32, u)
2775 /* Half precision conversions. */
2776 static float32 do_fcvt_f16_to_f32(uint32_t a, CPUARMState *env, float_status *s)
2778 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
2779 float32 r = float16_to_float32(make_float16(a), ieee, s);
2781 return float32_maybe_silence_nan(r);
2786 static uint32_t do_fcvt_f32_to_f16(float32 a, CPUARMState *env, float_status *s)
2788 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
2789 float16 r = float32_to_float16(a, ieee, s);
2791 r = float16_maybe_silence_nan(r);
2793 return float16_val(r);
2796 float32 HELPER(neon_fcvt_f16_to_f32)(uint32_t a, CPUARMState *env)
2798 return do_fcvt_f16_to_f32(a, env, &env->vfp.standard_fp_status);
2801 uint32_t HELPER(neon_fcvt_f32_to_f16)(float32 a, CPUARMState *env)
2803 return do_fcvt_f32_to_f16(a, env, &env->vfp.standard_fp_status);
2806 float32 HELPER(vfp_fcvt_f16_to_f32)(uint32_t a, CPUARMState *env)
2808 return do_fcvt_f16_to_f32(a, env, &env->vfp.fp_status);
2811 uint32_t HELPER(vfp_fcvt_f32_to_f16)(float32 a, CPUARMState *env)
2813 return do_fcvt_f32_to_f16(a, env, &env->vfp.fp_status);
2816 #define float32_two make_float32(0x40000000)
2817 #define float32_three make_float32(0x40400000)
2818 #define float32_one_point_five make_float32(0x3fc00000)
2820 float32 HELPER(recps_f32)(float32 a, float32 b, CPUARMState *env)
2822 float_status *s = &env->vfp.standard_fp_status;
2823 if ((float32_is_infinity(a) && float32_is_zero_or_denormal(b)) ||
2824 (float32_is_infinity(b) && float32_is_zero_or_denormal(a))) {
2825 if (!(float32_is_zero(a) || float32_is_zero(b))) {
2826 float_raise(float_flag_input_denormal, s);
2830 return float32_sub(float32_two, float32_mul(a, b, s), s);
2833 float32 HELPER(rsqrts_f32)(float32 a, float32 b, CPUARMState *env)
2835 float_status *s = &env->vfp.standard_fp_status;
2837 if ((float32_is_infinity(a) && float32_is_zero_or_denormal(b)) ||
2838 (float32_is_infinity(b) && float32_is_zero_or_denormal(a))) {
2839 if (!(float32_is_zero(a) || float32_is_zero(b))) {
2840 float_raise(float_flag_input_denormal, s);
2842 return float32_one_point_five;
2844 product = float32_mul(a, b, s);
2845 return float32_div(float32_sub(float32_three, product, s), float32_two, s);
2850 /* Constants 256 and 512 are used in some helpers; we avoid relying on
2851 * int->float conversions at run-time. */
2852 #define float64_256 make_float64(0x4070000000000000LL)
2853 #define float64_512 make_float64(0x4080000000000000LL)
2855 /* The algorithm that must be used to calculate the estimate
2856 * is specified by the ARM ARM.
2858 static float64 recip_estimate(float64 a, CPUARMState *env)
2860 /* These calculations mustn't set any fp exception flags,
2861 * so we use a local copy of the fp_status.
2863 float_status dummy_status = env->vfp.standard_fp_status;
2864 float_status *s = &dummy_status;
2865 /* q = (int)(a * 512.0) */
2866 float64 q = float64_mul(float64_512, a, s);
2867 int64_t q_int = float64_to_int64_round_to_zero(q, s);
2869 /* r = 1.0 / (((double)q + 0.5) / 512.0) */
2870 q = int64_to_float64(q_int, s);
2871 q = float64_add(q, float64_half, s);
2872 q = float64_div(q, float64_512, s);
2873 q = float64_div(float64_one, q, s);
2875 /* s = (int)(256.0 * r + 0.5) */
2876 q = float64_mul(q, float64_256, s);
2877 q = float64_add(q, float64_half, s);
2878 q_int = float64_to_int64_round_to_zero(q, s);
2880 /* return (double)s / 256.0 */
2881 return float64_div(int64_to_float64(q_int, s), float64_256, s);
2884 float32 HELPER(recpe_f32)(float32 a, CPUARMState *env)
2886 float_status *s = &env->vfp.standard_fp_status;
2888 uint32_t val32 = float32_val(a);
2891 int a_exp = (val32 & 0x7f800000) >> 23;
2892 int sign = val32 & 0x80000000;
2894 if (float32_is_any_nan(a)) {
2895 if (float32_is_signaling_nan(a)) {
2896 float_raise(float_flag_invalid, s);
2898 return float32_default_nan;
2899 } else if (float32_is_infinity(a)) {
2900 return float32_set_sign(float32_zero, float32_is_neg(a));
2901 } else if (float32_is_zero_or_denormal(a)) {
2902 if (!float32_is_zero(a)) {
2903 float_raise(float_flag_input_denormal, s);
2905 float_raise(float_flag_divbyzero, s);
2906 return float32_set_sign(float32_infinity, float32_is_neg(a));
2907 } else if (a_exp >= 253) {
2908 float_raise(float_flag_underflow, s);
2909 return float32_set_sign(float32_zero, float32_is_neg(a));
2912 f64 = make_float64((0x3feULL << 52)
2913 | ((int64_t)(val32 & 0x7fffff) << 29));
2915 result_exp = 253 - a_exp;
2917 f64 = recip_estimate(f64, env);
2920 | ((result_exp & 0xff) << 23)
2921 | ((float64_val(f64) >> 29) & 0x7fffff);
2922 return make_float32(val32);
2925 /* The algorithm that must be used to calculate the estimate
2926 * is specified by the ARM ARM.
2928 static float64 recip_sqrt_estimate(float64 a, CPUARMState *env)
2930 /* These calculations mustn't set any fp exception flags,
2931 * so we use a local copy of the fp_status.
2933 float_status dummy_status = env->vfp.standard_fp_status;
2934 float_status *s = &dummy_status;
2938 if (float64_lt(a, float64_half, s)) {
2939 /* range 0.25 <= a < 0.5 */
2941 /* a in units of 1/512 rounded down */
2942 /* q0 = (int)(a * 512.0); */
2943 q = float64_mul(float64_512, a, s);
2944 q_int = float64_to_int64_round_to_zero(q, s);
2946 /* reciprocal root r */
2947 /* r = 1.0 / sqrt(((double)q0 + 0.5) / 512.0); */
2948 q = int64_to_float64(q_int, s);
2949 q = float64_add(q, float64_half, s);
2950 q = float64_div(q, float64_512, s);
2951 q = float64_sqrt(q, s);
2952 q = float64_div(float64_one, q, s);
2954 /* range 0.5 <= a < 1.0 */
2956 /* a in units of 1/256 rounded down */
2957 /* q1 = (int)(a * 256.0); */
2958 q = float64_mul(float64_256, a, s);
2959 int64_t q_int = float64_to_int64_round_to_zero(q, s);
2961 /* reciprocal root r */
2962 /* r = 1.0 /sqrt(((double)q1 + 0.5) / 256); */
2963 q = int64_to_float64(q_int, s);
2964 q = float64_add(q, float64_half, s);
2965 q = float64_div(q, float64_256, s);
2966 q = float64_sqrt(q, s);
2967 q = float64_div(float64_one, q, s);
2969 /* r in units of 1/256 rounded to nearest */
2970 /* s = (int)(256.0 * r + 0.5); */
2972 q = float64_mul(q, float64_256,s );
2973 q = float64_add(q, float64_half, s);
2974 q_int = float64_to_int64_round_to_zero(q, s);
2976 /* return (double)s / 256.0;*/
2977 return float64_div(int64_to_float64(q_int, s), float64_256, s);
2980 float32 HELPER(rsqrte_f32)(float32 a, CPUARMState *env)
2982 float_status *s = &env->vfp.standard_fp_status;
2988 val = float32_val(a);
2990 if (float32_is_any_nan(a)) {
2991 if (float32_is_signaling_nan(a)) {
2992 float_raise(float_flag_invalid, s);
2994 return float32_default_nan;
2995 } else if (float32_is_zero_or_denormal(a)) {
2996 if (!float32_is_zero(a)) {
2997 float_raise(float_flag_input_denormal, s);
2999 float_raise(float_flag_divbyzero, s);
3000 return float32_set_sign(float32_infinity, float32_is_neg(a));
3001 } else if (float32_is_neg(a)) {
3002 float_raise(float_flag_invalid, s);
3003 return float32_default_nan;
3004 } else if (float32_is_infinity(a)) {
3005 return float32_zero;
3008 /* Normalize to a double-precision value between 0.25 and 1.0,
3009 * preserving the parity of the exponent. */
3010 if ((val & 0x800000) == 0) {
3011 f64 = make_float64(((uint64_t)(val & 0x80000000) << 32)
3013 | ((uint64_t)(val & 0x7fffff) << 29));
3015 f64 = make_float64(((uint64_t)(val & 0x80000000) << 32)
3017 | ((uint64_t)(val & 0x7fffff) << 29));
3020 result_exp = (380 - ((val & 0x7f800000) >> 23)) / 2;
3022 f64 = recip_sqrt_estimate(f64, env);
3024 val64 = float64_val(f64);
3026 val = ((result_exp & 0xff) << 23)
3027 | ((val64 >> 29) & 0x7fffff);
3028 return make_float32(val);
3031 uint32_t HELPER(recpe_u32)(uint32_t a, CPUARMState *env)
3035 if ((a & 0x80000000) == 0) {
3039 f64 = make_float64((0x3feULL << 52)
3040 | ((int64_t)(a & 0x7fffffff) << 21));
3042 f64 = recip_estimate (f64, env);
3044 return 0x80000000 | ((float64_val(f64) >> 21) & 0x7fffffff);
3047 uint32_t HELPER(rsqrte_u32)(uint32_t a, CPUARMState *env)
3051 if ((a & 0xc0000000) == 0) {
3055 if (a & 0x80000000) {
3056 f64 = make_float64((0x3feULL << 52)
3057 | ((uint64_t)(a & 0x7fffffff) << 21));
3058 } else { /* bits 31-30 == '01' */
3059 f64 = make_float64((0x3fdULL << 52)
3060 | ((uint64_t)(a & 0x3fffffff) << 22));
3063 f64 = recip_sqrt_estimate(f64, env);
3065 return 0x80000000 | ((float64_val(f64) >> 21) & 0x7fffffff);
3068 /* VFPv4 fused multiply-accumulate */
3069 float32 VFP_HELPER(muladd, s)(float32 a, float32 b, float32 c, void *fpstp)
3071 float_status *fpst = fpstp;
3072 return float32_muladd(a, b, c, 0, fpst);
3075 float64 VFP_HELPER(muladd, d)(float64 a, float64 b, float64 c, void *fpstp)
3077 float_status *fpst = fpstp;
3078 return float64_muladd(a, b, c, 0, fpst);
3081 void HELPER(set_teecr)(CPUARMState *env, uint32_t val)
3084 if (env->teecr != val) {