4 #include "host-utils.h"
5 #if !defined(CONFIG_USER_ONLY)
10 /* TODO Move contents into arm_cpu_reset() in cpu.c,
11 * once cpu_reset_model_id() is eliminated,
12 * and then forward to cpu_reset() here.
14 void cpu_state_reset(CPUARMState *env)
17 ARMCPU *cpu = arm_env_get_cpu(env);
19 if (qemu_loglevel_mask(CPU_LOG_RESET)) {
20 qemu_log("CPU Reset (CPU %d)\n", env->cpu_index);
21 log_cpu_state(env, 0);
24 tmp = env->cp15.c15_config_base_address;
25 memset(env, 0, offsetof(CPUARMState, breakpoints));
26 env->cp15.c15_config_base_address = tmp;
27 env->cp15.c0_cpuid = cpu->midr;
28 env->vfp.xregs[ARM_VFP_FPSID] = cpu->reset_fpsid;
29 env->vfp.xregs[ARM_VFP_MVFR0] = cpu->mvfr0;
30 env->vfp.xregs[ARM_VFP_MVFR1] = cpu->mvfr1;
31 env->cp15.c0_cachetype = cpu->ctr;
32 env->cp15.c1_sys = cpu->reset_sctlr;
33 env->cp15.c0_c1[0] = cpu->id_pfr0;
34 env->cp15.c0_c1[1] = cpu->id_pfr1;
35 env->cp15.c0_c1[2] = cpu->id_dfr0;
36 env->cp15.c0_c1[3] = cpu->id_afr0;
37 env->cp15.c0_c1[4] = cpu->id_mmfr0;
38 env->cp15.c0_c1[5] = cpu->id_mmfr1;
39 env->cp15.c0_c1[6] = cpu->id_mmfr2;
40 env->cp15.c0_c1[7] = cpu->id_mmfr3;
41 env->cp15.c0_c2[0] = cpu->id_isar0;
42 env->cp15.c0_c2[1] = cpu->id_isar1;
43 env->cp15.c0_c2[2] = cpu->id_isar2;
44 env->cp15.c0_c2[3] = cpu->id_isar3;
45 env->cp15.c0_c2[4] = cpu->id_isar4;
46 env->cp15.c0_c2[5] = cpu->id_isar5;
47 env->cp15.c15_i_min = 0xff0;
48 env->cp15.c0_clid = cpu->clidr;
49 memcpy(env->cp15.c0_ccsid, cpu->ccsidr, ARRAY_SIZE(cpu->ccsidr));
51 if (arm_feature(env, ARM_FEATURE_IWMMXT)) {
52 env->iwmmxt.cregs[ARM_IWMMXT_wCID] = 0x69051000 | 'Q';
55 #if defined (CONFIG_USER_ONLY)
56 env->uncached_cpsr = ARM_CPU_MODE_USR;
57 /* For user mode we must enable access to coprocessors */
58 env->vfp.xregs[ARM_VFP_FPEXC] = 1 << 30;
59 if (arm_feature(env, ARM_FEATURE_IWMMXT)) {
60 env->cp15.c15_cpar = 3;
61 } else if (arm_feature(env, ARM_FEATURE_XSCALE)) {
62 env->cp15.c15_cpar = 1;
65 /* SVC mode with interrupts disabled. */
66 env->uncached_cpsr = ARM_CPU_MODE_SVC | CPSR_A | CPSR_F | CPSR_I;
67 /* On ARMv7-M the CPSR_I is the value of the PRIMASK register, and is
68 clear at reset. Initial SP and PC are loaded from ROM. */
72 env->uncached_cpsr &= ~CPSR_I;
75 /* We should really use ldl_phys here, in case the guest
76 modified flash and reset itself. However images
77 loaded via -kernel have not been copied yet, so load the
78 values directly from there. */
79 env->regs[13] = ldl_p(rom);
82 env->regs[15] = pc & ~1;
85 env->vfp.xregs[ARM_VFP_FPEXC] = 0;
86 env->cp15.c2_base_mask = 0xffffc000u;
87 /* v7 performance monitor control register: same implementor
88 * field as main ID register, and we implement no event counters.
90 env->cp15.c9_pmcr = (cpu->midr & 0xff000000);
92 set_flush_to_zero(1, &env->vfp.standard_fp_status);
93 set_flush_inputs_to_zero(1, &env->vfp.standard_fp_status);
94 set_default_nan_mode(1, &env->vfp.standard_fp_status);
95 set_float_detect_tininess(float_tininess_before_rounding,
97 set_float_detect_tininess(float_tininess_before_rounding,
98 &env->vfp.standard_fp_status);
100 /* Reset is a state change for some CPUARMState fields which we
101 * bake assumptions about into translated code, so we need to
107 static int vfp_gdb_get_reg(CPUARMState *env, uint8_t *buf, int reg)
111 /* VFP data registers are always little-endian. */
112 nregs = arm_feature(env, ARM_FEATURE_VFP3) ? 32 : 16;
114 stfq_le_p(buf, env->vfp.regs[reg]);
117 if (arm_feature(env, ARM_FEATURE_NEON)) {
118 /* Aliases for Q regs. */
121 stfq_le_p(buf, env->vfp.regs[(reg - 32) * 2]);
122 stfq_le_p(buf + 8, env->vfp.regs[(reg - 32) * 2 + 1]);
126 switch (reg - nregs) {
127 case 0: stl_p(buf, env->vfp.xregs[ARM_VFP_FPSID]); return 4;
128 case 1: stl_p(buf, env->vfp.xregs[ARM_VFP_FPSCR]); return 4;
129 case 2: stl_p(buf, env->vfp.xregs[ARM_VFP_FPEXC]); return 4;
134 static int vfp_gdb_set_reg(CPUARMState *env, uint8_t *buf, int reg)
138 nregs = arm_feature(env, ARM_FEATURE_VFP3) ? 32 : 16;
140 env->vfp.regs[reg] = ldfq_le_p(buf);
143 if (arm_feature(env, ARM_FEATURE_NEON)) {
146 env->vfp.regs[(reg - 32) * 2] = ldfq_le_p(buf);
147 env->vfp.regs[(reg - 32) * 2 + 1] = ldfq_le_p(buf + 8);
151 switch (reg - nregs) {
152 case 0: env->vfp.xregs[ARM_VFP_FPSID] = ldl_p(buf); return 4;
153 case 1: env->vfp.xregs[ARM_VFP_FPSCR] = ldl_p(buf); return 4;
154 case 2: env->vfp.xregs[ARM_VFP_FPEXC] = ldl_p(buf) & (1 << 30); return 4;
159 CPUARMState *cpu_arm_init(const char *cpu_model)
163 static int inited = 0;
165 if (!object_class_by_name(cpu_model)) {
168 cpu = ARM_CPU(object_new(cpu_model));
170 env->cpu_model_str = cpu_model;
171 arm_cpu_realize(cpu);
173 if (tcg_enabled() && !inited) {
175 arm_translate_init();
178 cpu_state_reset(env);
179 if (arm_feature(env, ARM_FEATURE_NEON)) {
180 gdb_register_coprocessor(env, vfp_gdb_get_reg, vfp_gdb_set_reg,
181 51, "arm-neon.xml", 0);
182 } else if (arm_feature(env, ARM_FEATURE_VFP3)) {
183 gdb_register_coprocessor(env, vfp_gdb_get_reg, vfp_gdb_set_reg,
184 35, "arm-vfp3.xml", 0);
185 } else if (arm_feature(env, ARM_FEATURE_VFP)) {
186 gdb_register_coprocessor(env, vfp_gdb_get_reg, vfp_gdb_set_reg,
187 19, "arm-vfp.xml", 0);
193 typedef struct ARMCPUListState {
194 fprintf_function cpu_fprintf;
198 /* Sort alphabetically by type name, except for "any". */
199 static gint arm_cpu_list_compare(gconstpointer a, gconstpointer b)
201 ObjectClass *class_a = (ObjectClass *)a;
202 ObjectClass *class_b = (ObjectClass *)b;
203 const char *name_a, *name_b;
205 name_a = object_class_get_name(class_a);
206 name_b = object_class_get_name(class_b);
207 if (strcmp(name_a, "any") == 0) {
209 } else if (strcmp(name_b, "any") == 0) {
212 return strcmp(name_a, name_b);
216 static void arm_cpu_list_entry(gpointer data, gpointer user_data)
218 ObjectClass *oc = data;
219 ARMCPUListState *s = user_data;
221 (*s->cpu_fprintf)(s->file, " %s\n",
222 object_class_get_name(oc));
225 void arm_cpu_list(FILE *f, fprintf_function cpu_fprintf)
227 ARMCPUListState s = {
229 .cpu_fprintf = cpu_fprintf,
233 list = object_class_get_list(TYPE_ARM_CPU, false);
234 list = g_slist_sort(list, arm_cpu_list_compare);
235 (*cpu_fprintf)(f, "Available CPUs:\n");
236 g_slist_foreach(list, arm_cpu_list_entry, &s);
240 static int bad_mode_switch(CPUARMState *env, int mode)
242 /* Return true if it is not valid for us to switch to
243 * this CPU mode (ie all the UNPREDICTABLE cases in
244 * the ARM ARM CPSRWriteByInstr pseudocode).
247 case ARM_CPU_MODE_USR:
248 case ARM_CPU_MODE_SYS:
249 case ARM_CPU_MODE_SVC:
250 case ARM_CPU_MODE_ABT:
251 case ARM_CPU_MODE_UND:
252 case ARM_CPU_MODE_IRQ:
253 case ARM_CPU_MODE_FIQ:
260 uint32_t cpsr_read(CPUARMState *env)
264 return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) |
265 (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27)
266 | (env->thumb << 5) | ((env->condexec_bits & 3) << 25)
267 | ((env->condexec_bits & 0xfc) << 8)
271 void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask)
273 if (mask & CPSR_NZCV) {
274 env->ZF = (~val) & CPSR_Z;
276 env->CF = (val >> 29) & 1;
277 env->VF = (val << 3) & 0x80000000;
280 env->QF = ((val & CPSR_Q) != 0);
282 env->thumb = ((val & CPSR_T) != 0);
283 if (mask & CPSR_IT_0_1) {
284 env->condexec_bits &= ~3;
285 env->condexec_bits |= (val >> 25) & 3;
287 if (mask & CPSR_IT_2_7) {
288 env->condexec_bits &= 3;
289 env->condexec_bits |= (val >> 8) & 0xfc;
291 if (mask & CPSR_GE) {
292 env->GE = (val >> 16) & 0xf;
295 if ((env->uncached_cpsr ^ val) & mask & CPSR_M) {
296 if (bad_mode_switch(env, val & CPSR_M)) {
297 /* Attempt to switch to an invalid mode: this is UNPREDICTABLE.
298 * We choose to ignore the attempt and leave the CPSR M field
303 switch_mode(env, val & CPSR_M);
306 mask &= ~CACHED_CPSR_BITS;
307 env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask);
310 /* Sign/zero extend */
311 uint32_t HELPER(sxtb16)(uint32_t x)
314 res = (uint16_t)(int8_t)x;
315 res |= (uint32_t)(int8_t)(x >> 16) << 16;
319 uint32_t HELPER(uxtb16)(uint32_t x)
322 res = (uint16_t)(uint8_t)x;
323 res |= (uint32_t)(uint8_t)(x >> 16) << 16;
327 uint32_t HELPER(clz)(uint32_t x)
332 int32_t HELPER(sdiv)(int32_t num, int32_t den)
336 if (num == INT_MIN && den == -1)
341 uint32_t HELPER(udiv)(uint32_t num, uint32_t den)
348 uint32_t HELPER(rbit)(uint32_t x)
350 x = ((x & 0xff000000) >> 24)
351 | ((x & 0x00ff0000) >> 8)
352 | ((x & 0x0000ff00) << 8)
353 | ((x & 0x000000ff) << 24);
354 x = ((x & 0xf0f0f0f0) >> 4)
355 | ((x & 0x0f0f0f0f) << 4);
356 x = ((x & 0x88888888) >> 3)
357 | ((x & 0x44444444) >> 1)
358 | ((x & 0x22222222) << 1)
359 | ((x & 0x11111111) << 3);
363 uint32_t HELPER(abs)(uint32_t x)
365 return ((int32_t)x < 0) ? -x : x;
368 #if defined(CONFIG_USER_ONLY)
370 void do_interrupt (CPUARMState *env)
372 env->exception_index = -1;
375 int cpu_arm_handle_mmu_fault (CPUARMState *env, target_ulong address, int rw,
379 env->exception_index = EXCP_PREFETCH_ABORT;
380 env->cp15.c6_insn = address;
382 env->exception_index = EXCP_DATA_ABORT;
383 env->cp15.c6_data = address;
388 /* These should probably raise undefined insn exceptions. */
389 void HELPER(set_cp)(CPUARMState *env, uint32_t insn, uint32_t val)
391 int op1 = (insn >> 8) & 0xf;
392 cpu_abort(env, "cp%i insn %08x\n", op1, insn);
396 uint32_t HELPER(get_cp)(CPUARMState *env, uint32_t insn)
398 int op1 = (insn >> 8) & 0xf;
399 cpu_abort(env, "cp%i insn %08x\n", op1, insn);
403 void HELPER(set_cp15)(CPUARMState *env, uint32_t insn, uint32_t val)
405 cpu_abort(env, "cp15 insn %08x\n", insn);
408 uint32_t HELPER(get_cp15)(CPUARMState *env, uint32_t insn)
410 cpu_abort(env, "cp15 insn %08x\n", insn);
413 /* These should probably raise undefined insn exceptions. */
414 void HELPER(v7m_msr)(CPUARMState *env, uint32_t reg, uint32_t val)
416 cpu_abort(env, "v7m_mrs %d\n", reg);
419 uint32_t HELPER(v7m_mrs)(CPUARMState *env, uint32_t reg)
421 cpu_abort(env, "v7m_mrs %d\n", reg);
425 void switch_mode(CPUARMState *env, int mode)
427 if (mode != ARM_CPU_MODE_USR)
428 cpu_abort(env, "Tried to switch out of user mode\n");
431 void HELPER(set_r13_banked)(CPUARMState *env, uint32_t mode, uint32_t val)
433 cpu_abort(env, "banked r13 write\n");
436 uint32_t HELPER(get_r13_banked)(CPUARMState *env, uint32_t mode)
438 cpu_abort(env, "banked r13 read\n");
444 /* Map CPU modes onto saved register banks. */
445 static inline int bank_number(CPUARMState *env, int mode)
448 case ARM_CPU_MODE_USR:
449 case ARM_CPU_MODE_SYS:
451 case ARM_CPU_MODE_SVC:
453 case ARM_CPU_MODE_ABT:
455 case ARM_CPU_MODE_UND:
457 case ARM_CPU_MODE_IRQ:
459 case ARM_CPU_MODE_FIQ:
462 cpu_abort(env, "Bad mode %x\n", mode);
466 void switch_mode(CPUARMState *env, int mode)
471 old_mode = env->uncached_cpsr & CPSR_M;
472 if (mode == old_mode)
475 if (old_mode == ARM_CPU_MODE_FIQ) {
476 memcpy (env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t));
477 memcpy (env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t));
478 } else if (mode == ARM_CPU_MODE_FIQ) {
479 memcpy (env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t));
480 memcpy (env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t));
483 i = bank_number(env, old_mode);
484 env->banked_r13[i] = env->regs[13];
485 env->banked_r14[i] = env->regs[14];
486 env->banked_spsr[i] = env->spsr;
488 i = bank_number(env, mode);
489 env->regs[13] = env->banked_r13[i];
490 env->regs[14] = env->banked_r14[i];
491 env->spsr = env->banked_spsr[i];
494 static void v7m_push(CPUARMState *env, uint32_t val)
497 stl_phys(env->regs[13], val);
500 static uint32_t v7m_pop(CPUARMState *env)
503 val = ldl_phys(env->regs[13]);
508 /* Switch to V7M main or process stack pointer. */
509 static void switch_v7m_sp(CPUARMState *env, int process)
512 if (env->v7m.current_sp != process) {
513 tmp = env->v7m.other_sp;
514 env->v7m.other_sp = env->regs[13];
516 env->v7m.current_sp = process;
520 static void do_v7m_exception_exit(CPUARMState *env)
525 type = env->regs[15];
526 if (env->v7m.exception != 0)
527 armv7m_nvic_complete_irq(env->nvic, env->v7m.exception);
529 /* Switch to the target stack. */
530 switch_v7m_sp(env, (type & 4) != 0);
532 env->regs[0] = v7m_pop(env);
533 env->regs[1] = v7m_pop(env);
534 env->regs[2] = v7m_pop(env);
535 env->regs[3] = v7m_pop(env);
536 env->regs[12] = v7m_pop(env);
537 env->regs[14] = v7m_pop(env);
538 env->regs[15] = v7m_pop(env);
540 xpsr_write(env, xpsr, 0xfffffdff);
541 /* Undo stack alignment. */
544 /* ??? The exception return type specifies Thread/Handler mode. However
545 this is also implied by the xPSR value. Not sure what to do
546 if there is a mismatch. */
547 /* ??? Likewise for mismatches between the CONTROL register and the stack
551 static void do_interrupt_v7m(CPUARMState *env)
553 uint32_t xpsr = xpsr_read(env);
558 if (env->v7m.current_sp)
560 if (env->v7m.exception == 0)
563 /* For exceptions we just mark as pending on the NVIC, and let that
565 /* TODO: Need to escalate if the current priority is higher than the
566 one we're raising. */
567 switch (env->exception_index) {
569 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE);
573 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SVC);
575 case EXCP_PREFETCH_ABORT:
576 case EXCP_DATA_ABORT:
577 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_MEM);
580 if (semihosting_enabled) {
582 nr = arm_lduw_code(env->regs[15], env->bswap_code) & 0xff;
585 env->regs[0] = do_arm_semihosting(env);
589 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_DEBUG);
592 env->v7m.exception = armv7m_nvic_acknowledge_irq(env->nvic);
594 case EXCP_EXCEPTION_EXIT:
595 do_v7m_exception_exit(env);
598 cpu_abort(env, "Unhandled exception 0x%x\n", env->exception_index);
599 return; /* Never happens. Keep compiler happy. */
602 /* Align stack pointer. */
603 /* ??? Should only do this if Configuration Control Register
604 STACKALIGN bit is set. */
605 if (env->regs[13] & 4) {
609 /* Switch to the handler mode. */
611 v7m_push(env, env->regs[15]);
612 v7m_push(env, env->regs[14]);
613 v7m_push(env, env->regs[12]);
614 v7m_push(env, env->regs[3]);
615 v7m_push(env, env->regs[2]);
616 v7m_push(env, env->regs[1]);
617 v7m_push(env, env->regs[0]);
618 switch_v7m_sp(env, 0);
620 env->condexec_bits = 0;
622 addr = ldl_phys(env->v7m.vecbase + env->v7m.exception * 4);
623 env->regs[15] = addr & 0xfffffffe;
624 env->thumb = addr & 1;
627 /* Handle a CPU exception. */
628 void do_interrupt(CPUARMState *env)
636 do_interrupt_v7m(env);
639 /* TODO: Vectored interrupt controller. */
640 switch (env->exception_index) {
642 new_mode = ARM_CPU_MODE_UND;
651 if (semihosting_enabled) {
652 /* Check for semihosting interrupt. */
654 mask = arm_lduw_code(env->regs[15] - 2, env->bswap_code) & 0xff;
656 mask = arm_ldl_code(env->regs[15] - 4, env->bswap_code)
659 /* Only intercept calls from privileged modes, to provide some
660 semblance of security. */
661 if (((mask == 0x123456 && !env->thumb)
662 || (mask == 0xab && env->thumb))
663 && (env->uncached_cpsr & CPSR_M) != ARM_CPU_MODE_USR) {
664 env->regs[0] = do_arm_semihosting(env);
668 new_mode = ARM_CPU_MODE_SVC;
671 /* The PC already points to the next instruction. */
675 /* See if this is a semihosting syscall. */
676 if (env->thumb && semihosting_enabled) {
677 mask = arm_lduw_code(env->regs[15], env->bswap_code) & 0xff;
679 && (env->uncached_cpsr & CPSR_M) != ARM_CPU_MODE_USR) {
681 env->regs[0] = do_arm_semihosting(env);
685 env->cp15.c5_insn = 2;
686 /* Fall through to prefetch abort. */
687 case EXCP_PREFETCH_ABORT:
688 new_mode = ARM_CPU_MODE_ABT;
690 mask = CPSR_A | CPSR_I;
693 case EXCP_DATA_ABORT:
694 new_mode = ARM_CPU_MODE_ABT;
696 mask = CPSR_A | CPSR_I;
700 new_mode = ARM_CPU_MODE_IRQ;
702 /* Disable IRQ and imprecise data aborts. */
703 mask = CPSR_A | CPSR_I;
707 new_mode = ARM_CPU_MODE_FIQ;
709 /* Disable FIQ, IRQ and imprecise data aborts. */
710 mask = CPSR_A | CPSR_I | CPSR_F;
714 cpu_abort(env, "Unhandled exception 0x%x\n", env->exception_index);
715 return; /* Never happens. Keep compiler happy. */
718 if (env->cp15.c1_sys & (1 << 13)) {
721 switch_mode (env, new_mode);
722 env->spsr = cpsr_read(env);
724 env->condexec_bits = 0;
725 /* Switch to the new mode, and to the correct instruction set. */
726 env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode;
727 env->uncached_cpsr |= mask;
728 /* this is a lie, as the was no c1_sys on V4T/V5, but who cares
729 * and we should just guard the thumb mode on V4 */
730 if (arm_feature(env, ARM_FEATURE_V4T)) {
731 env->thumb = (env->cp15.c1_sys & (1 << 30)) != 0;
733 env->regs[14] = env->regs[15] + offset;
734 env->regs[15] = addr;
735 env->interrupt_request |= CPU_INTERRUPT_EXITTB;
738 /* Check section/page access permissions.
739 Returns the page protection flags, or zero if the access is not
741 static inline int check_ap(CPUARMState *env, int ap, int domain_prot,
742 int access_type, int is_user)
746 if (domain_prot == 3) {
747 return PAGE_READ | PAGE_WRITE;
750 if (access_type == 1)
757 if (access_type == 1)
759 switch ((env->cp15.c1_sys >> 8) & 3) {
761 return is_user ? 0 : PAGE_READ;
768 return is_user ? 0 : PAGE_READ | PAGE_WRITE;
773 return PAGE_READ | PAGE_WRITE;
775 return PAGE_READ | PAGE_WRITE;
776 case 4: /* Reserved. */
779 return is_user ? 0 : prot_ro;
783 if (!arm_feature (env, ARM_FEATURE_V6K))
791 static uint32_t get_level1_table_address(CPUARMState *env, uint32_t address)
795 if (address & env->cp15.c2_mask)
796 table = env->cp15.c2_base1 & 0xffffc000;
798 table = env->cp15.c2_base0 & env->cp15.c2_base_mask;
800 table |= (address >> 18) & 0x3ffc;
804 static int get_phys_addr_v5(CPUARMState *env, uint32_t address, int access_type,
805 int is_user, uint32_t *phys_ptr, int *prot,
806 target_ulong *page_size)
817 /* Pagetable walk. */
818 /* Lookup l1 descriptor. */
819 table = get_level1_table_address(env, address);
820 desc = ldl_phys(table);
822 domain = (desc >> 5) & 0x0f;
823 domain_prot = (env->cp15.c3 >> (domain * 2)) & 3;
825 /* Section translation fault. */
829 if (domain_prot == 0 || domain_prot == 2) {
831 code = 9; /* Section domain fault. */
833 code = 11; /* Page domain fault. */
838 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
839 ap = (desc >> 10) & 3;
841 *page_size = 1024 * 1024;
843 /* Lookup l2 entry. */
845 /* Coarse pagetable. */
846 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
848 /* Fine pagetable. */
849 table = (desc & 0xfffff000) | ((address >> 8) & 0xffc);
851 desc = ldl_phys(table);
853 case 0: /* Page translation fault. */
856 case 1: /* 64k page. */
857 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
858 ap = (desc >> (4 + ((address >> 13) & 6))) & 3;
859 *page_size = 0x10000;
861 case 2: /* 4k page. */
862 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
863 ap = (desc >> (4 + ((address >> 13) & 6))) & 3;
866 case 3: /* 1k page. */
868 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
869 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
871 /* Page translation fault. */
876 phys_addr = (desc & 0xfffffc00) | (address & 0x3ff);
878 ap = (desc >> 4) & 3;
882 /* Never happens, but compiler isn't smart enough to tell. */
887 *prot = check_ap(env, ap, domain_prot, access_type, is_user);
889 /* Access permission fault. */
893 *phys_ptr = phys_addr;
896 return code | (domain << 4);
899 static int get_phys_addr_v6(CPUARMState *env, uint32_t address, int access_type,
900 int is_user, uint32_t *phys_ptr, int *prot,
901 target_ulong *page_size)
913 /* Pagetable walk. */
914 /* Lookup l1 descriptor. */
915 table = get_level1_table_address(env, address);
916 desc = ldl_phys(table);
919 /* Section translation fault. */
923 } else if (type == 2 && (desc & (1 << 18))) {
927 /* Section or page. */
928 domain = (desc >> 5) & 0x0f;
930 domain_prot = (env->cp15.c3 >> (domain * 2)) & 3;
931 if (domain_prot == 0 || domain_prot == 2) {
933 code = 9; /* Section domain fault. */
935 code = 11; /* Page domain fault. */
939 if (desc & (1 << 18)) {
941 phys_addr = (desc & 0xff000000) | (address & 0x00ffffff);
942 *page_size = 0x1000000;
945 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
946 *page_size = 0x100000;
948 ap = ((desc >> 10) & 3) | ((desc >> 13) & 4);
949 xn = desc & (1 << 4);
952 /* Lookup l2 entry. */
953 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
954 desc = ldl_phys(table);
955 ap = ((desc >> 4) & 3) | ((desc >> 7) & 4);
957 case 0: /* Page translation fault. */
960 case 1: /* 64k page. */
961 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
962 xn = desc & (1 << 15);
963 *page_size = 0x10000;
965 case 2: case 3: /* 4k page. */
966 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
971 /* Never happens, but compiler isn't smart enough to tell. */
976 if (domain_prot == 3) {
977 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
979 if (xn && access_type == 2)
982 /* The simplified model uses AP[0] as an access control bit. */
983 if ((env->cp15.c1_sys & (1 << 29)) && (ap & 1) == 0) {
984 /* Access flag fault. */
985 code = (code == 15) ? 6 : 3;
988 *prot = check_ap(env, ap, domain_prot, access_type, is_user);
990 /* Access permission fault. */
997 *phys_ptr = phys_addr;
1000 return code | (domain << 4);
1003 static int get_phys_addr_mpu(CPUARMState *env, uint32_t address, int access_type,
1004 int is_user, uint32_t *phys_ptr, int *prot)
1010 *phys_ptr = address;
1011 for (n = 7; n >= 0; n--) {
1012 base = env->cp15.c6_region[n];
1013 if ((base & 1) == 0)
1015 mask = 1 << ((base >> 1) & 0x1f);
1016 /* Keep this shift separate from the above to avoid an
1017 (undefined) << 32. */
1018 mask = (mask << 1) - 1;
1019 if (((base ^ address) & ~mask) == 0)
1025 if (access_type == 2) {
1026 mask = env->cp15.c5_insn;
1028 mask = env->cp15.c5_data;
1030 mask = (mask >> (n * 4)) & 0xf;
1037 *prot = PAGE_READ | PAGE_WRITE;
1042 *prot |= PAGE_WRITE;
1045 *prot = PAGE_READ | PAGE_WRITE;
1056 /* Bad permission. */
1063 static inline int get_phys_addr(CPUARMState *env, uint32_t address,
1064 int access_type, int is_user,
1065 uint32_t *phys_ptr, int *prot,
1066 target_ulong *page_size)
1068 /* Fast Context Switch Extension. */
1069 if (address < 0x02000000)
1070 address += env->cp15.c13_fcse;
1072 if ((env->cp15.c1_sys & 1) == 0) {
1073 /* MMU/MPU disabled. */
1074 *phys_ptr = address;
1075 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
1076 *page_size = TARGET_PAGE_SIZE;
1078 } else if (arm_feature(env, ARM_FEATURE_MPU)) {
1079 *page_size = TARGET_PAGE_SIZE;
1080 return get_phys_addr_mpu(env, address, access_type, is_user, phys_ptr,
1082 } else if (env->cp15.c1_sys & (1 << 23)) {
1083 return get_phys_addr_v6(env, address, access_type, is_user, phys_ptr,
1086 return get_phys_addr_v5(env, address, access_type, is_user, phys_ptr,
1091 int cpu_arm_handle_mmu_fault (CPUARMState *env, target_ulong address,
1092 int access_type, int mmu_idx)
1095 target_ulong page_size;
1099 is_user = mmu_idx == MMU_USER_IDX;
1100 ret = get_phys_addr(env, address, access_type, is_user, &phys_addr, &prot,
1103 /* Map a single [sub]page. */
1104 phys_addr &= ~(uint32_t)0x3ff;
1105 address &= ~(uint32_t)0x3ff;
1106 tlb_set_page (env, address, phys_addr, prot, mmu_idx, page_size);
1110 if (access_type == 2) {
1111 env->cp15.c5_insn = ret;
1112 env->cp15.c6_insn = address;
1113 env->exception_index = EXCP_PREFETCH_ABORT;
1115 env->cp15.c5_data = ret;
1116 if (access_type == 1 && arm_feature(env, ARM_FEATURE_V6))
1117 env->cp15.c5_data |= (1 << 11);
1118 env->cp15.c6_data = address;
1119 env->exception_index = EXCP_DATA_ABORT;
1124 target_phys_addr_t cpu_get_phys_page_debug(CPUARMState *env, target_ulong addr)
1127 target_ulong page_size;
1131 ret = get_phys_addr(env, addr, 0, 0, &phys_addr, &prot, &page_size);
1139 void HELPER(set_cp)(CPUARMState *env, uint32_t insn, uint32_t val)
1141 int cp_num = (insn >> 8) & 0xf;
1142 int cp_info = (insn >> 5) & 7;
1143 int src = (insn >> 16) & 0xf;
1144 int operand = insn & 0xf;
1146 if (env->cp[cp_num].cp_write)
1147 env->cp[cp_num].cp_write(env->cp[cp_num].opaque,
1148 cp_info, src, operand, val);
1151 uint32_t HELPER(get_cp)(CPUARMState *env, uint32_t insn)
1153 int cp_num = (insn >> 8) & 0xf;
1154 int cp_info = (insn >> 5) & 7;
1155 int dest = (insn >> 16) & 0xf;
1156 int operand = insn & 0xf;
1158 if (env->cp[cp_num].cp_read)
1159 return env->cp[cp_num].cp_read(env->cp[cp_num].opaque,
1160 cp_info, dest, operand);
1164 /* Return basic MPU access permission bits. */
1165 static uint32_t simple_mpu_ap_bits(uint32_t val)
1172 for (i = 0; i < 16; i += 2) {
1173 ret |= (val >> i) & mask;
1179 /* Pad basic MPU access permission bits to extended format. */
1180 static uint32_t extended_mpu_ap_bits(uint32_t val)
1187 for (i = 0; i < 16; i += 2) {
1188 ret |= (val & mask) << i;
1194 void HELPER(set_cp15)(CPUARMState *env, uint32_t insn, uint32_t val)
1200 op1 = (insn >> 21) & 7;
1201 op2 = (insn >> 5) & 7;
1203 switch ((insn >> 16) & 0xf) {
1206 if (arm_feature(env, ARM_FEATURE_XSCALE))
1208 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1210 if (arm_feature(env, ARM_FEATURE_V7)
1211 && op1 == 2 && crm == 0 && op2 == 0) {
1212 env->cp15.c0_cssel = val & 0xf;
1216 case 1: /* System configuration. */
1217 if (arm_feature(env, ARM_FEATURE_V7)
1218 && op1 == 0 && crm == 1 && op2 == 0) {
1219 env->cp15.c1_scr = val;
1222 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1226 if (!arm_feature(env, ARM_FEATURE_XSCALE) || crm == 0)
1227 env->cp15.c1_sys = val;
1228 /* ??? Lots of these bits are not implemented. */
1229 /* This may enable/disable the MMU, so do a TLB flush. */
1232 case 1: /* Auxiliary control register. */
1233 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
1234 env->cp15.c1_xscaleauxcr = val;
1237 /* Not implemented. */
1240 if (arm_feature(env, ARM_FEATURE_XSCALE))
1242 if (env->cp15.c1_coproc != val) {
1243 env->cp15.c1_coproc = val;
1244 /* ??? Is this safe when called from within a TB? */
1252 case 2: /* MMU Page table control / MPU cache control. */
1253 if (arm_feature(env, ARM_FEATURE_MPU)) {
1256 env->cp15.c2_data = val;
1259 env->cp15.c2_insn = val;
1267 env->cp15.c2_base0 = val;
1270 env->cp15.c2_base1 = val;
1274 env->cp15.c2_control = val;
1275 env->cp15.c2_mask = ~(((uint32_t)0xffffffffu) >> val);
1276 env->cp15.c2_base_mask = ~((uint32_t)0x3fffu >> val);
1283 case 3: /* MMU Domain access control / MPU write buffer control. */
1285 tlb_flush(env, 1); /* Flush TLB as domain not tracked in TLB */
1287 case 4: /* Reserved. */
1289 case 5: /* MMU Fault status / MPU access permission. */
1290 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1294 if (arm_feature(env, ARM_FEATURE_MPU))
1295 val = extended_mpu_ap_bits(val);
1296 env->cp15.c5_data = val;
1299 if (arm_feature(env, ARM_FEATURE_MPU))
1300 val = extended_mpu_ap_bits(val);
1301 env->cp15.c5_insn = val;
1304 if (!arm_feature(env, ARM_FEATURE_MPU))
1306 env->cp15.c5_data = val;
1309 if (!arm_feature(env, ARM_FEATURE_MPU))
1311 env->cp15.c5_insn = val;
1317 case 6: /* MMU Fault address / MPU base/size. */
1318 if (arm_feature(env, ARM_FEATURE_MPU)) {
1321 env->cp15.c6_region[crm] = val;
1323 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1327 env->cp15.c6_data = val;
1329 case 1: /* ??? This is WFAR on armv6 */
1331 env->cp15.c6_insn = val;
1338 case 7: /* Cache control. */
1339 env->cp15.c15_i_max = 0x000;
1340 env->cp15.c15_i_min = 0xff0;
1344 /* No cache, so nothing to do except VA->PA translations. */
1345 if (arm_feature(env, ARM_FEATURE_VAPA)) {
1348 if (arm_feature(env, ARM_FEATURE_V7)) {
1349 env->cp15.c7_par = val & 0xfffff6ff;
1351 env->cp15.c7_par = val & 0xfffff1ff;
1356 target_ulong page_size;
1358 int ret, is_user = op2 & 2;
1359 int access_type = op2 & 1;
1362 /* Other states are only available with TrustZone */
1365 ret = get_phys_addr(env, val, access_type, is_user,
1366 &phys_addr, &prot, &page_size);
1368 /* We do not set any attribute bits in the PAR */
1369 if (page_size == (1 << 24)
1370 && arm_feature(env, ARM_FEATURE_V7)) {
1371 env->cp15.c7_par = (phys_addr & 0xff000000) | 1 << 1;
1373 env->cp15.c7_par = phys_addr & 0xfffff000;
1376 env->cp15.c7_par = ((ret & (10 << 1)) >> 5) |
1377 ((ret & (12 << 1)) >> 6) |
1378 ((ret & 0xf) << 1) | 1;
1385 case 8: /* MMU TLB control. */
1387 case 0: /* Invalidate all (TLBIALL) */
1390 case 1: /* Invalidate single TLB entry by MVA and ASID (TLBIMVA) */
1391 tlb_flush_page(env, val & TARGET_PAGE_MASK);
1393 case 2: /* Invalidate by ASID (TLBIASID) */
1394 tlb_flush(env, val == 0);
1396 case 3: /* Invalidate single entry by MVA, all ASIDs (TLBIMVAA) */
1397 tlb_flush_page(env, val & TARGET_PAGE_MASK);
1404 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1406 if (arm_feature(env, ARM_FEATURE_STRONGARM))
1407 break; /* Ignore ReadBuffer access */
1409 case 0: /* Cache lockdown. */
1411 case 0: /* L1 cache. */
1414 env->cp15.c9_data = val;
1417 env->cp15.c9_insn = val;
1423 case 1: /* L2 cache. */
1424 /* Ignore writes to L2 lockdown/auxiliary registers. */
1430 case 1: /* TCM memory region registers. */
1431 /* Not implemented. */
1433 case 12: /* Performance monitor control */
1434 /* Performance monitors are implementation defined in v7,
1435 * but with an ARM recommended set of registers, which we
1436 * follow (although we don't actually implement any counters)
1438 if (!arm_feature(env, ARM_FEATURE_V7)) {
1442 case 0: /* performance monitor control register */
1443 /* only the DP, X, D and E bits are writable */
1444 env->cp15.c9_pmcr &= ~0x39;
1445 env->cp15.c9_pmcr |= (val & 0x39);
1447 case 1: /* Count enable set register */
1449 env->cp15.c9_pmcnten |= val;
1451 case 2: /* Count enable clear */
1453 env->cp15.c9_pmcnten &= ~val;
1455 case 3: /* Overflow flag status */
1456 env->cp15.c9_pmovsr &= ~val;
1458 case 4: /* Software increment */
1459 /* RAZ/WI since we don't implement the software-count event */
1461 case 5: /* Event counter selection register */
1462 /* Since we don't implement any events, writing to this register
1463 * is actually UNPREDICTABLE. So we choose to RAZ/WI.
1470 case 13: /* Performance counters */
1471 if (!arm_feature(env, ARM_FEATURE_V7)) {
1475 case 0: /* Cycle count register: not implemented, so RAZ/WI */
1477 case 1: /* Event type select */
1478 env->cp15.c9_pmxevtyper = val & 0xff;
1480 case 2: /* Event count register */
1481 /* Unimplemented (we have no events), RAZ/WI */
1487 case 14: /* Performance monitor control */
1488 if (!arm_feature(env, ARM_FEATURE_V7)) {
1492 case 0: /* user enable */
1493 env->cp15.c9_pmuserenr = val & 1;
1494 /* changes access rights for cp registers, so flush tbs */
1497 case 1: /* interrupt enable set */
1498 /* We have no event counters so only the C bit can be changed */
1500 env->cp15.c9_pminten |= val;
1502 case 2: /* interrupt enable clear */
1504 env->cp15.c9_pminten &= ~val;
1512 case 10: /* MMU TLB lockdown. */
1513 /* ??? TLB lockdown not implemented. */
1515 case 12: /* Reserved. */
1517 case 13: /* Process ID. */
1520 /* Unlike real hardware the qemu TLB uses virtual addresses,
1521 not modified virtual addresses, so this causes a TLB flush.
1523 if (env->cp15.c13_fcse != val)
1525 env->cp15.c13_fcse = val;
1528 /* This changes the ASID, so do a TLB flush. */
1529 if (env->cp15.c13_context != val
1530 && !arm_feature(env, ARM_FEATURE_MPU))
1532 env->cp15.c13_context = val;
1538 case 14: /* Generic timer */
1539 if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) {
1540 /* Dummy implementation: RAZ/WI for all */
1544 case 15: /* Implementation specific. */
1545 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
1546 if (op2 == 0 && crm == 1) {
1547 if (env->cp15.c15_cpar != (val & 0x3fff)) {
1548 /* Changes cp0 to cp13 behavior, so needs a TB flush. */
1550 env->cp15.c15_cpar = val & 0x3fff;
1556 if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
1560 case 1: /* Set TI925T configuration. */
1561 env->cp15.c15_ticonfig = val & 0xe7;
1562 env->cp15.c0_cpuid = (val & (1 << 5)) ? /* OS_TYPE bit */
1563 ARM_CPUID_TI915T : ARM_CPUID_TI925T;
1565 case 2: /* Set I_max. */
1566 env->cp15.c15_i_max = val;
1568 case 3: /* Set I_min. */
1569 env->cp15.c15_i_min = val;
1571 case 4: /* Set thread-ID. */
1572 env->cp15.c15_threadid = val & 0xffff;
1574 case 8: /* Wait-for-interrupt (deprecated). */
1575 cpu_interrupt(env, CPU_INTERRUPT_HALT);
1581 if (ARM_CPUID(env) == ARM_CPUID_CORTEXA9) {
1584 if ((op1 == 0) && (op2 == 0)) {
1585 env->cp15.c15_power_control = val;
1586 } else if ((op1 == 0) && (op2 == 1)) {
1587 env->cp15.c15_diagnostic = val;
1588 } else if ((op1 == 0) && (op2 == 2)) {
1589 env->cp15.c15_power_diagnostic = val;
1599 /* ??? For debugging only. Should raise illegal instruction exception. */
1600 cpu_abort(env, "Unimplemented cp15 register write (c%d, c%d, {%d, %d})\n",
1601 (insn >> 16) & 0xf, crm, op1, op2);
1604 uint32_t HELPER(get_cp15)(CPUARMState *env, uint32_t insn)
1610 op1 = (insn >> 21) & 7;
1611 op2 = (insn >> 5) & 7;
1613 switch ((insn >> 16) & 0xf) {
1614 case 0: /* ID codes. */
1620 case 0: /* Device ID. */
1621 return env->cp15.c0_cpuid;
1622 case 1: /* Cache Type. */
1623 return env->cp15.c0_cachetype;
1624 case 2: /* TCM status. */
1626 case 3: /* TLB type register. */
1627 return 0; /* No lockable TLB entries. */
1629 /* The MPIDR was standardised in v7; prior to
1630 * this it was implemented only in the 11MPCore.
1631 * For all other pre-v7 cores it does not exist.
1633 if (arm_feature(env, ARM_FEATURE_V7) ||
1634 ARM_CPUID(env) == ARM_CPUID_ARM11MPCORE) {
1635 int mpidr = env->cpu_index;
1636 /* We don't support setting cluster ID ([8..11])
1637 * so these bits always RAZ.
1639 if (arm_feature(env, ARM_FEATURE_V7MP)) {
1641 /* Cores which are uniprocessor (non-coherent)
1642 * but still implement the MP extensions set
1643 * bit 30. (For instance, A9UP.) However we do
1644 * not currently model any of those cores.
1649 /* otherwise fall through to the unimplemented-reg case */
1654 if (!arm_feature(env, ARM_FEATURE_V6))
1656 return env->cp15.c0_c1[op2];
1658 if (!arm_feature(env, ARM_FEATURE_V6))
1660 return env->cp15.c0_c2[op2];
1661 case 3: case 4: case 5: case 6: case 7:
1667 /* These registers aren't documented on arm11 cores. However
1668 Linux looks at them anyway. */
1669 if (!arm_feature(env, ARM_FEATURE_V6))
1673 if (!arm_feature(env, ARM_FEATURE_V7))
1678 return env->cp15.c0_ccsid[env->cp15.c0_cssel];
1680 return env->cp15.c0_clid;
1686 if (op2 != 0 || crm != 0)
1688 return env->cp15.c0_cssel;
1692 case 1: /* System configuration. */
1693 if (arm_feature(env, ARM_FEATURE_V7)
1694 && op1 == 0 && crm == 1 && op2 == 0) {
1695 return env->cp15.c1_scr;
1697 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1700 case 0: /* Control register. */
1701 return env->cp15.c1_sys;
1702 case 1: /* Auxiliary control register. */
1703 if (arm_feature(env, ARM_FEATURE_XSCALE))
1704 return env->cp15.c1_xscaleauxcr;
1705 if (!arm_feature(env, ARM_FEATURE_AUXCR))
1707 switch (ARM_CPUID(env)) {
1708 case ARM_CPUID_ARM1026:
1710 case ARM_CPUID_ARM1136:
1711 case ARM_CPUID_ARM1136_R2:
1712 case ARM_CPUID_ARM1176:
1714 case ARM_CPUID_ARM11MPCORE:
1716 case ARM_CPUID_CORTEXA8:
1718 case ARM_CPUID_CORTEXA9:
1719 case ARM_CPUID_CORTEXA15:
1724 case 2: /* Coprocessor access register. */
1725 if (arm_feature(env, ARM_FEATURE_XSCALE))
1727 return env->cp15.c1_coproc;
1731 case 2: /* MMU Page table control / MPU cache control. */
1732 if (arm_feature(env, ARM_FEATURE_MPU)) {
1735 return env->cp15.c2_data;
1738 return env->cp15.c2_insn;
1746 return env->cp15.c2_base0;
1748 return env->cp15.c2_base1;
1750 return env->cp15.c2_control;
1755 case 3: /* MMU Domain access control / MPU write buffer control. */
1756 return env->cp15.c3;
1757 case 4: /* Reserved. */
1759 case 5: /* MMU Fault status / MPU access permission. */
1760 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1764 if (arm_feature(env, ARM_FEATURE_MPU))
1765 return simple_mpu_ap_bits(env->cp15.c5_data);
1766 return env->cp15.c5_data;
1768 if (arm_feature(env, ARM_FEATURE_MPU))
1769 return simple_mpu_ap_bits(env->cp15.c5_insn);
1770 return env->cp15.c5_insn;
1772 if (!arm_feature(env, ARM_FEATURE_MPU))
1774 return env->cp15.c5_data;
1776 if (!arm_feature(env, ARM_FEATURE_MPU))
1778 return env->cp15.c5_insn;
1782 case 6: /* MMU Fault address. */
1783 if (arm_feature(env, ARM_FEATURE_MPU)) {
1786 return env->cp15.c6_region[crm];
1788 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1792 return env->cp15.c6_data;
1794 if (arm_feature(env, ARM_FEATURE_V6)) {
1795 /* Watchpoint Fault Adrress. */
1796 return 0; /* Not implemented. */
1798 /* Instruction Fault Adrress. */
1799 /* Arm9 doesn't have an IFAR, but implementing it anyway
1800 shouldn't do any harm. */
1801 return env->cp15.c6_insn;
1804 if (arm_feature(env, ARM_FEATURE_V6)) {
1805 /* Instruction Fault Adrress. */
1806 return env->cp15.c6_insn;
1814 case 7: /* Cache control. */
1815 if (crm == 4 && op1 == 0 && op2 == 0) {
1816 return env->cp15.c7_par;
1818 /* FIXME: Should only clear Z flag if destination is r15. */
1821 case 8: /* MMU TLB control. */
1825 case 0: /* Cache lockdown */
1827 case 0: /* L1 cache. */
1828 if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
1833 return env->cp15.c9_data;
1835 return env->cp15.c9_insn;
1839 case 1: /* L2 cache */
1840 /* L2 Lockdown and Auxiliary control. */
1843 /* L2 cache lockdown (A8 only) */
1846 /* L2 cache auxiliary control (A8) or control (A15) */
1847 if (ARM_CPUID(env) == ARM_CPUID_CORTEXA15) {
1848 /* Linux wants the number of processors from here.
1849 * Might as well set the interrupt-controller bit too.
1851 return ((smp_cpus - 1) << 24) | (1 << 23);
1855 /* L2 cache extended control (A15) */
1864 case 12: /* Performance monitor control */
1865 if (!arm_feature(env, ARM_FEATURE_V7)) {
1869 case 0: /* performance monitor control register */
1870 return env->cp15.c9_pmcr;
1871 case 1: /* count enable set */
1872 case 2: /* count enable clear */
1873 return env->cp15.c9_pmcnten;
1874 case 3: /* overflow flag status */
1875 return env->cp15.c9_pmovsr;
1876 case 4: /* software increment */
1877 case 5: /* event counter selection register */
1878 return 0; /* Unimplemented, RAZ/WI */
1882 case 13: /* Performance counters */
1883 if (!arm_feature(env, ARM_FEATURE_V7)) {
1887 case 1: /* Event type select */
1888 return env->cp15.c9_pmxevtyper;
1889 case 0: /* Cycle count register */
1890 case 2: /* Event count register */
1891 /* Unimplemented, so RAZ/WI */
1896 case 14: /* Performance monitor control */
1897 if (!arm_feature(env, ARM_FEATURE_V7)) {
1901 case 0: /* user enable */
1902 return env->cp15.c9_pmuserenr;
1903 case 1: /* interrupt enable set */
1904 case 2: /* interrupt enable clear */
1905 return env->cp15.c9_pminten;
1913 case 10: /* MMU TLB lockdown. */
1914 /* ??? TLB lockdown not implemented. */
1916 case 11: /* TCM DMA control. */
1917 case 12: /* Reserved. */
1919 case 13: /* Process ID. */
1922 return env->cp15.c13_fcse;
1924 return env->cp15.c13_context;
1928 case 14: /* Generic timer */
1929 if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) {
1930 /* Dummy implementation: RAZ/WI for all */
1934 case 15: /* Implementation specific. */
1935 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
1936 if (op2 == 0 && crm == 1)
1937 return env->cp15.c15_cpar;
1941 if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
1945 case 1: /* Read TI925T configuration. */
1946 return env->cp15.c15_ticonfig;
1947 case 2: /* Read I_max. */
1948 return env->cp15.c15_i_max;
1949 case 3: /* Read I_min. */
1950 return env->cp15.c15_i_min;
1951 case 4: /* Read thread-ID. */
1952 return env->cp15.c15_threadid;
1953 case 8: /* TI925T_status */
1956 /* TODO: Peripheral port remap register:
1957 * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt
1958 * controller base address at $rn & ~0xfff and map size of
1959 * 0x200 << ($rn & 0xfff), when MMU is off. */
1962 if (ARM_CPUID(env) == ARM_CPUID_CORTEXA9) {
1965 if ((op1 == 4) && (op2 == 0)) {
1966 /* The config_base_address should hold the value of
1967 * the peripheral base. ARM should get this from a CPU
1968 * object property, but that support isn't available in
1969 * December 2011. Default to 0 for now and board models
1970 * that care can set it by a private hook */
1971 return env->cp15.c15_config_base_address;
1972 } else if ((op1 == 0) && (op2 == 0)) {
1973 /* power_control should be set to maximum latency. Again,
1974 default to 0 and set by private hook */
1975 return env->cp15.c15_power_control;
1976 } else if ((op1 == 0) && (op2 == 1)) {
1977 return env->cp15.c15_diagnostic;
1978 } else if ((op1 == 0) && (op2 == 2)) {
1979 return env->cp15.c15_power_diagnostic;
1982 case 1: /* NEON Busy */
1984 case 5: /* tlb lockdown */
1987 if ((op1 == 5) && (op2 == 2)) {
1999 /* ??? For debugging only. Should raise illegal instruction exception. */
2000 cpu_abort(env, "Unimplemented cp15 register read (c%d, c%d, {%d, %d})\n",
2001 (insn >> 16) & 0xf, crm, op1, op2);
2005 void HELPER(set_r13_banked)(CPUARMState *env, uint32_t mode, uint32_t val)
2007 if ((env->uncached_cpsr & CPSR_M) == mode) {
2008 env->regs[13] = val;
2010 env->banked_r13[bank_number(env, mode)] = val;
2014 uint32_t HELPER(get_r13_banked)(CPUARMState *env, uint32_t mode)
2016 if ((env->uncached_cpsr & CPSR_M) == mode) {
2017 return env->regs[13];
2019 return env->banked_r13[bank_number(env, mode)];
2023 uint32_t HELPER(v7m_mrs)(CPUARMState *env, uint32_t reg)
2027 return xpsr_read(env) & 0xf8000000;
2029 return xpsr_read(env) & 0xf80001ff;
2031 return xpsr_read(env) & 0xff00fc00;
2033 return xpsr_read(env) & 0xff00fdff;
2035 return xpsr_read(env) & 0x000001ff;
2037 return xpsr_read(env) & 0x0700fc00;
2039 return xpsr_read(env) & 0x0700edff;
2041 return env->v7m.current_sp ? env->v7m.other_sp : env->regs[13];
2043 return env->v7m.current_sp ? env->regs[13] : env->v7m.other_sp;
2044 case 16: /* PRIMASK */
2045 return (env->uncached_cpsr & CPSR_I) != 0;
2046 case 17: /* BASEPRI */
2047 case 18: /* BASEPRI_MAX */
2048 return env->v7m.basepri;
2049 case 19: /* FAULTMASK */
2050 return (env->uncached_cpsr & CPSR_F) != 0;
2051 case 20: /* CONTROL */
2052 return env->v7m.control;
2054 /* ??? For debugging only. */
2055 cpu_abort(env, "Unimplemented system register read (%d)\n", reg);
2060 void HELPER(v7m_msr)(CPUARMState *env, uint32_t reg, uint32_t val)
2064 xpsr_write(env, val, 0xf8000000);
2067 xpsr_write(env, val, 0xf8000000);
2070 xpsr_write(env, val, 0xfe00fc00);
2073 xpsr_write(env, val, 0xfe00fc00);
2076 /* IPSR bits are readonly. */
2079 xpsr_write(env, val, 0x0600fc00);
2082 xpsr_write(env, val, 0x0600fc00);
2085 if (env->v7m.current_sp)
2086 env->v7m.other_sp = val;
2088 env->regs[13] = val;
2091 if (env->v7m.current_sp)
2092 env->regs[13] = val;
2094 env->v7m.other_sp = val;
2096 case 16: /* PRIMASK */
2098 env->uncached_cpsr |= CPSR_I;
2100 env->uncached_cpsr &= ~CPSR_I;
2102 case 17: /* BASEPRI */
2103 env->v7m.basepri = val & 0xff;
2105 case 18: /* BASEPRI_MAX */
2107 if (val != 0 && (val < env->v7m.basepri || env->v7m.basepri == 0))
2108 env->v7m.basepri = val;
2110 case 19: /* FAULTMASK */
2112 env->uncached_cpsr |= CPSR_F;
2114 env->uncached_cpsr &= ~CPSR_F;
2116 case 20: /* CONTROL */
2117 env->v7m.control = val & 3;
2118 switch_v7m_sp(env, (val & 2) != 0);
2121 /* ??? For debugging only. */
2122 cpu_abort(env, "Unimplemented system register write (%d)\n", reg);
2127 void cpu_arm_set_cp_io(CPUARMState *env, int cpnum,
2128 ARMReadCPFunc *cp_read, ARMWriteCPFunc *cp_write,
2131 if (cpnum < 0 || cpnum > 14) {
2132 cpu_abort(env, "Bad coprocessor number: %i\n", cpnum);
2136 env->cp[cpnum].cp_read = cp_read;
2137 env->cp[cpnum].cp_write = cp_write;
2138 env->cp[cpnum].opaque = opaque;
2143 /* Note that signed overflow is undefined in C. The following routines are
2144 careful to use unsigned types where modulo arithmetic is required.
2145 Failure to do so _will_ break on newer gcc. */
2147 /* Signed saturating arithmetic. */
2149 /* Perform 16-bit signed saturating addition. */
2150 static inline uint16_t add16_sat(uint16_t a, uint16_t b)
2155 if (((res ^ a) & 0x8000) && !((a ^ b) & 0x8000)) {
2164 /* Perform 8-bit signed saturating addition. */
2165 static inline uint8_t add8_sat(uint8_t a, uint8_t b)
2170 if (((res ^ a) & 0x80) && !((a ^ b) & 0x80)) {
2179 /* Perform 16-bit signed saturating subtraction. */
2180 static inline uint16_t sub16_sat(uint16_t a, uint16_t b)
2185 if (((res ^ a) & 0x8000) && ((a ^ b) & 0x8000)) {
2194 /* Perform 8-bit signed saturating subtraction. */
2195 static inline uint8_t sub8_sat(uint8_t a, uint8_t b)
2200 if (((res ^ a) & 0x80) && ((a ^ b) & 0x80)) {
2209 #define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16);
2210 #define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16);
2211 #define ADD8(a, b, n) RESULT(add8_sat(a, b), n, 8);
2212 #define SUB8(a, b, n) RESULT(sub8_sat(a, b), n, 8);
2215 #include "op_addsub.h"
2217 /* Unsigned saturating arithmetic. */
2218 static inline uint16_t add16_usat(uint16_t a, uint16_t b)
2227 static inline uint16_t sub16_usat(uint16_t a, uint16_t b)
2235 static inline uint8_t add8_usat(uint8_t a, uint8_t b)
2244 static inline uint8_t sub8_usat(uint8_t a, uint8_t b)
2252 #define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16);
2253 #define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16);
2254 #define ADD8(a, b, n) RESULT(add8_usat(a, b), n, 8);
2255 #define SUB8(a, b, n) RESULT(sub8_usat(a, b), n, 8);
2258 #include "op_addsub.h"
2260 /* Signed modulo arithmetic. */
2261 #define SARITH16(a, b, n, op) do { \
2263 sum = (int32_t)(int16_t)(a) op (int32_t)(int16_t)(b); \
2264 RESULT(sum, n, 16); \
2266 ge |= 3 << (n * 2); \
2269 #define SARITH8(a, b, n, op) do { \
2271 sum = (int32_t)(int8_t)(a) op (int32_t)(int8_t)(b); \
2272 RESULT(sum, n, 8); \
2278 #define ADD16(a, b, n) SARITH16(a, b, n, +)
2279 #define SUB16(a, b, n) SARITH16(a, b, n, -)
2280 #define ADD8(a, b, n) SARITH8(a, b, n, +)
2281 #define SUB8(a, b, n) SARITH8(a, b, n, -)
2285 #include "op_addsub.h"
2287 /* Unsigned modulo arithmetic. */
2288 #define ADD16(a, b, n) do { \
2290 sum = (uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b); \
2291 RESULT(sum, n, 16); \
2292 if ((sum >> 16) == 1) \
2293 ge |= 3 << (n * 2); \
2296 #define ADD8(a, b, n) do { \
2298 sum = (uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b); \
2299 RESULT(sum, n, 8); \
2300 if ((sum >> 8) == 1) \
2304 #define SUB16(a, b, n) do { \
2306 sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \
2307 RESULT(sum, n, 16); \
2308 if ((sum >> 16) == 0) \
2309 ge |= 3 << (n * 2); \
2312 #define SUB8(a, b, n) do { \
2314 sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \
2315 RESULT(sum, n, 8); \
2316 if ((sum >> 8) == 0) \
2323 #include "op_addsub.h"
2325 /* Halved signed arithmetic. */
2326 #define ADD16(a, b, n) \
2327 RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16)
2328 #define SUB16(a, b, n) \
2329 RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16)
2330 #define ADD8(a, b, n) \
2331 RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8)
2332 #define SUB8(a, b, n) \
2333 RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8)
2336 #include "op_addsub.h"
2338 /* Halved unsigned arithmetic. */
2339 #define ADD16(a, b, n) \
2340 RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16)
2341 #define SUB16(a, b, n) \
2342 RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16)
2343 #define ADD8(a, b, n) \
2344 RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8)
2345 #define SUB8(a, b, n) \
2346 RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8)
2349 #include "op_addsub.h"
2351 static inline uint8_t do_usad(uint8_t a, uint8_t b)
2359 /* Unsigned sum of absolute byte differences. */
2360 uint32_t HELPER(usad8)(uint32_t a, uint32_t b)
2363 sum = do_usad(a, b);
2364 sum += do_usad(a >> 8, b >> 8);
2365 sum += do_usad(a >> 16, b >>16);
2366 sum += do_usad(a >> 24, b >> 24);
2370 /* For ARMv6 SEL instruction. */
2371 uint32_t HELPER(sel_flags)(uint32_t flags, uint32_t a, uint32_t b)
2384 return (a & mask) | (b & ~mask);
2387 uint32_t HELPER(logicq_cc)(uint64_t val)
2389 return (val >> 32) | (val != 0);
2392 /* VFP support. We follow the convention used for VFP instrunctions:
2393 Single precition routines have a "s" suffix, double precision a
2396 /* Convert host exception flags to vfp form. */
2397 static inline int vfp_exceptbits_from_host(int host_bits)
2399 int target_bits = 0;
2401 if (host_bits & float_flag_invalid)
2403 if (host_bits & float_flag_divbyzero)
2405 if (host_bits & float_flag_overflow)
2407 if (host_bits & (float_flag_underflow | float_flag_output_denormal))
2409 if (host_bits & float_flag_inexact)
2410 target_bits |= 0x10;
2411 if (host_bits & float_flag_input_denormal)
2412 target_bits |= 0x80;
2416 uint32_t HELPER(vfp_get_fpscr)(CPUARMState *env)
2421 fpscr = (env->vfp.xregs[ARM_VFP_FPSCR] & 0xffc8ffff)
2422 | (env->vfp.vec_len << 16)
2423 | (env->vfp.vec_stride << 20);
2424 i = get_float_exception_flags(&env->vfp.fp_status);
2425 i |= get_float_exception_flags(&env->vfp.standard_fp_status);
2426 fpscr |= vfp_exceptbits_from_host(i);
2430 uint32_t vfp_get_fpscr(CPUARMState *env)
2432 return HELPER(vfp_get_fpscr)(env);
2435 /* Convert vfp exception flags to target form. */
2436 static inline int vfp_exceptbits_to_host(int target_bits)
2440 if (target_bits & 1)
2441 host_bits |= float_flag_invalid;
2442 if (target_bits & 2)
2443 host_bits |= float_flag_divbyzero;
2444 if (target_bits & 4)
2445 host_bits |= float_flag_overflow;
2446 if (target_bits & 8)
2447 host_bits |= float_flag_underflow;
2448 if (target_bits & 0x10)
2449 host_bits |= float_flag_inexact;
2450 if (target_bits & 0x80)
2451 host_bits |= float_flag_input_denormal;
2455 void HELPER(vfp_set_fpscr)(CPUARMState *env, uint32_t val)
2460 changed = env->vfp.xregs[ARM_VFP_FPSCR];
2461 env->vfp.xregs[ARM_VFP_FPSCR] = (val & 0xffc8ffff);
2462 env->vfp.vec_len = (val >> 16) & 7;
2463 env->vfp.vec_stride = (val >> 20) & 3;
2466 if (changed & (3 << 22)) {
2467 i = (val >> 22) & 3;
2470 i = float_round_nearest_even;
2476 i = float_round_down;
2479 i = float_round_to_zero;
2482 set_float_rounding_mode(i, &env->vfp.fp_status);
2484 if (changed & (1 << 24)) {
2485 set_flush_to_zero((val & (1 << 24)) != 0, &env->vfp.fp_status);
2486 set_flush_inputs_to_zero((val & (1 << 24)) != 0, &env->vfp.fp_status);
2488 if (changed & (1 << 25))
2489 set_default_nan_mode((val & (1 << 25)) != 0, &env->vfp.fp_status);
2491 i = vfp_exceptbits_to_host(val);
2492 set_float_exception_flags(i, &env->vfp.fp_status);
2493 set_float_exception_flags(0, &env->vfp.standard_fp_status);
2496 void vfp_set_fpscr(CPUARMState *env, uint32_t val)
2498 HELPER(vfp_set_fpscr)(env, val);
2501 #define VFP_HELPER(name, p) HELPER(glue(glue(vfp_,name),p))
2503 #define VFP_BINOP(name) \
2504 float32 VFP_HELPER(name, s)(float32 a, float32 b, void *fpstp) \
2506 float_status *fpst = fpstp; \
2507 return float32_ ## name(a, b, fpst); \
2509 float64 VFP_HELPER(name, d)(float64 a, float64 b, void *fpstp) \
2511 float_status *fpst = fpstp; \
2512 return float64_ ## name(a, b, fpst); \
2520 float32 VFP_HELPER(neg, s)(float32 a)
2522 return float32_chs(a);
2525 float64 VFP_HELPER(neg, d)(float64 a)
2527 return float64_chs(a);
2530 float32 VFP_HELPER(abs, s)(float32 a)
2532 return float32_abs(a);
2535 float64 VFP_HELPER(abs, d)(float64 a)
2537 return float64_abs(a);
2540 float32 VFP_HELPER(sqrt, s)(float32 a, CPUARMState *env)
2542 return float32_sqrt(a, &env->vfp.fp_status);
2545 float64 VFP_HELPER(sqrt, d)(float64 a, CPUARMState *env)
2547 return float64_sqrt(a, &env->vfp.fp_status);
2550 /* XXX: check quiet/signaling case */
2551 #define DO_VFP_cmp(p, type) \
2552 void VFP_HELPER(cmp, p)(type a, type b, CPUARMState *env) \
2555 switch(type ## _compare_quiet(a, b, &env->vfp.fp_status)) { \
2556 case 0: flags = 0x6; break; \
2557 case -1: flags = 0x8; break; \
2558 case 1: flags = 0x2; break; \
2559 default: case 2: flags = 0x3; break; \
2561 env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \
2562 | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \
2564 void VFP_HELPER(cmpe, p)(type a, type b, CPUARMState *env) \
2567 switch(type ## _compare(a, b, &env->vfp.fp_status)) { \
2568 case 0: flags = 0x6; break; \
2569 case -1: flags = 0x8; break; \
2570 case 1: flags = 0x2; break; \
2571 default: case 2: flags = 0x3; break; \
2573 env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \
2574 | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \
2576 DO_VFP_cmp(s, float32)
2577 DO_VFP_cmp(d, float64)
2580 /* Integer to float and float to integer conversions */
2582 #define CONV_ITOF(name, fsz, sign) \
2583 float##fsz HELPER(name)(uint32_t x, void *fpstp) \
2585 float_status *fpst = fpstp; \
2586 return sign##int32_to_##float##fsz((sign##int32_t)x, fpst); \
2589 #define CONV_FTOI(name, fsz, sign, round) \
2590 uint32_t HELPER(name)(float##fsz x, void *fpstp) \
2592 float_status *fpst = fpstp; \
2593 if (float##fsz##_is_any_nan(x)) { \
2594 float_raise(float_flag_invalid, fpst); \
2597 return float##fsz##_to_##sign##int32##round(x, fpst); \
2600 #define FLOAT_CONVS(name, p, fsz, sign) \
2601 CONV_ITOF(vfp_##name##to##p, fsz, sign) \
2602 CONV_FTOI(vfp_to##name##p, fsz, sign, ) \
2603 CONV_FTOI(vfp_to##name##z##p, fsz, sign, _round_to_zero)
2605 FLOAT_CONVS(si, s, 32, )
2606 FLOAT_CONVS(si, d, 64, )
2607 FLOAT_CONVS(ui, s, 32, u)
2608 FLOAT_CONVS(ui, d, 64, u)
2614 /* floating point conversion */
2615 float64 VFP_HELPER(fcvtd, s)(float32 x, CPUARMState *env)
2617 float64 r = float32_to_float64(x, &env->vfp.fp_status);
2618 /* ARM requires that S<->D conversion of any kind of NaN generates
2619 * a quiet NaN by forcing the most significant frac bit to 1.
2621 return float64_maybe_silence_nan(r);
2624 float32 VFP_HELPER(fcvts, d)(float64 x, CPUARMState *env)
2626 float32 r = float64_to_float32(x, &env->vfp.fp_status);
2627 /* ARM requires that S<->D conversion of any kind of NaN generates
2628 * a quiet NaN by forcing the most significant frac bit to 1.
2630 return float32_maybe_silence_nan(r);
2633 /* VFP3 fixed point conversion. */
2634 #define VFP_CONV_FIX(name, p, fsz, itype, sign) \
2635 float##fsz HELPER(vfp_##name##to##p)(uint##fsz##_t x, uint32_t shift, \
2638 float_status *fpst = fpstp; \
2640 tmp = sign##int32_to_##float##fsz((itype##_t)x, fpst); \
2641 return float##fsz##_scalbn(tmp, -(int)shift, fpst); \
2643 uint##fsz##_t HELPER(vfp_to##name##p)(float##fsz x, uint32_t shift, \
2646 float_status *fpst = fpstp; \
2648 if (float##fsz##_is_any_nan(x)) { \
2649 float_raise(float_flag_invalid, fpst); \
2652 tmp = float##fsz##_scalbn(x, shift, fpst); \
2653 return float##fsz##_to_##itype##_round_to_zero(tmp, fpst); \
2656 VFP_CONV_FIX(sh, d, 64, int16, )
2657 VFP_CONV_FIX(sl, d, 64, int32, )
2658 VFP_CONV_FIX(uh, d, 64, uint16, u)
2659 VFP_CONV_FIX(ul, d, 64, uint32, u)
2660 VFP_CONV_FIX(sh, s, 32, int16, )
2661 VFP_CONV_FIX(sl, s, 32, int32, )
2662 VFP_CONV_FIX(uh, s, 32, uint16, u)
2663 VFP_CONV_FIX(ul, s, 32, uint32, u)
2666 /* Half precision conversions. */
2667 static float32 do_fcvt_f16_to_f32(uint32_t a, CPUARMState *env, float_status *s)
2669 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
2670 float32 r = float16_to_float32(make_float16(a), ieee, s);
2672 return float32_maybe_silence_nan(r);
2677 static uint32_t do_fcvt_f32_to_f16(float32 a, CPUARMState *env, float_status *s)
2679 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
2680 float16 r = float32_to_float16(a, ieee, s);
2682 r = float16_maybe_silence_nan(r);
2684 return float16_val(r);
2687 float32 HELPER(neon_fcvt_f16_to_f32)(uint32_t a, CPUARMState *env)
2689 return do_fcvt_f16_to_f32(a, env, &env->vfp.standard_fp_status);
2692 uint32_t HELPER(neon_fcvt_f32_to_f16)(float32 a, CPUARMState *env)
2694 return do_fcvt_f32_to_f16(a, env, &env->vfp.standard_fp_status);
2697 float32 HELPER(vfp_fcvt_f16_to_f32)(uint32_t a, CPUARMState *env)
2699 return do_fcvt_f16_to_f32(a, env, &env->vfp.fp_status);
2702 uint32_t HELPER(vfp_fcvt_f32_to_f16)(float32 a, CPUARMState *env)
2704 return do_fcvt_f32_to_f16(a, env, &env->vfp.fp_status);
2707 #define float32_two make_float32(0x40000000)
2708 #define float32_three make_float32(0x40400000)
2709 #define float32_one_point_five make_float32(0x3fc00000)
2711 float32 HELPER(recps_f32)(float32 a, float32 b, CPUARMState *env)
2713 float_status *s = &env->vfp.standard_fp_status;
2714 if ((float32_is_infinity(a) && float32_is_zero_or_denormal(b)) ||
2715 (float32_is_infinity(b) && float32_is_zero_or_denormal(a))) {
2716 if (!(float32_is_zero(a) || float32_is_zero(b))) {
2717 float_raise(float_flag_input_denormal, s);
2721 return float32_sub(float32_two, float32_mul(a, b, s), s);
2724 float32 HELPER(rsqrts_f32)(float32 a, float32 b, CPUARMState *env)
2726 float_status *s = &env->vfp.standard_fp_status;
2728 if ((float32_is_infinity(a) && float32_is_zero_or_denormal(b)) ||
2729 (float32_is_infinity(b) && float32_is_zero_or_denormal(a))) {
2730 if (!(float32_is_zero(a) || float32_is_zero(b))) {
2731 float_raise(float_flag_input_denormal, s);
2733 return float32_one_point_five;
2735 product = float32_mul(a, b, s);
2736 return float32_div(float32_sub(float32_three, product, s), float32_two, s);
2741 /* Constants 256 and 512 are used in some helpers; we avoid relying on
2742 * int->float conversions at run-time. */
2743 #define float64_256 make_float64(0x4070000000000000LL)
2744 #define float64_512 make_float64(0x4080000000000000LL)
2746 /* The algorithm that must be used to calculate the estimate
2747 * is specified by the ARM ARM.
2749 static float64 recip_estimate(float64 a, CPUARMState *env)
2751 /* These calculations mustn't set any fp exception flags,
2752 * so we use a local copy of the fp_status.
2754 float_status dummy_status = env->vfp.standard_fp_status;
2755 float_status *s = &dummy_status;
2756 /* q = (int)(a * 512.0) */
2757 float64 q = float64_mul(float64_512, a, s);
2758 int64_t q_int = float64_to_int64_round_to_zero(q, s);
2760 /* r = 1.0 / (((double)q + 0.5) / 512.0) */
2761 q = int64_to_float64(q_int, s);
2762 q = float64_add(q, float64_half, s);
2763 q = float64_div(q, float64_512, s);
2764 q = float64_div(float64_one, q, s);
2766 /* s = (int)(256.0 * r + 0.5) */
2767 q = float64_mul(q, float64_256, s);
2768 q = float64_add(q, float64_half, s);
2769 q_int = float64_to_int64_round_to_zero(q, s);
2771 /* return (double)s / 256.0 */
2772 return float64_div(int64_to_float64(q_int, s), float64_256, s);
2775 float32 HELPER(recpe_f32)(float32 a, CPUARMState *env)
2777 float_status *s = &env->vfp.standard_fp_status;
2779 uint32_t val32 = float32_val(a);
2782 int a_exp = (val32 & 0x7f800000) >> 23;
2783 int sign = val32 & 0x80000000;
2785 if (float32_is_any_nan(a)) {
2786 if (float32_is_signaling_nan(a)) {
2787 float_raise(float_flag_invalid, s);
2789 return float32_default_nan;
2790 } else if (float32_is_infinity(a)) {
2791 return float32_set_sign(float32_zero, float32_is_neg(a));
2792 } else if (float32_is_zero_or_denormal(a)) {
2793 if (!float32_is_zero(a)) {
2794 float_raise(float_flag_input_denormal, s);
2796 float_raise(float_flag_divbyzero, s);
2797 return float32_set_sign(float32_infinity, float32_is_neg(a));
2798 } else if (a_exp >= 253) {
2799 float_raise(float_flag_underflow, s);
2800 return float32_set_sign(float32_zero, float32_is_neg(a));
2803 f64 = make_float64((0x3feULL << 52)
2804 | ((int64_t)(val32 & 0x7fffff) << 29));
2806 result_exp = 253 - a_exp;
2808 f64 = recip_estimate(f64, env);
2811 | ((result_exp & 0xff) << 23)
2812 | ((float64_val(f64) >> 29) & 0x7fffff);
2813 return make_float32(val32);
2816 /* The algorithm that must be used to calculate the estimate
2817 * is specified by the ARM ARM.
2819 static float64 recip_sqrt_estimate(float64 a, CPUARMState *env)
2821 /* These calculations mustn't set any fp exception flags,
2822 * so we use a local copy of the fp_status.
2824 float_status dummy_status = env->vfp.standard_fp_status;
2825 float_status *s = &dummy_status;
2829 if (float64_lt(a, float64_half, s)) {
2830 /* range 0.25 <= a < 0.5 */
2832 /* a in units of 1/512 rounded down */
2833 /* q0 = (int)(a * 512.0); */
2834 q = float64_mul(float64_512, a, s);
2835 q_int = float64_to_int64_round_to_zero(q, s);
2837 /* reciprocal root r */
2838 /* r = 1.0 / sqrt(((double)q0 + 0.5) / 512.0); */
2839 q = int64_to_float64(q_int, s);
2840 q = float64_add(q, float64_half, s);
2841 q = float64_div(q, float64_512, s);
2842 q = float64_sqrt(q, s);
2843 q = float64_div(float64_one, q, s);
2845 /* range 0.5 <= a < 1.0 */
2847 /* a in units of 1/256 rounded down */
2848 /* q1 = (int)(a * 256.0); */
2849 q = float64_mul(float64_256, a, s);
2850 int64_t q_int = float64_to_int64_round_to_zero(q, s);
2852 /* reciprocal root r */
2853 /* r = 1.0 /sqrt(((double)q1 + 0.5) / 256); */
2854 q = int64_to_float64(q_int, s);
2855 q = float64_add(q, float64_half, s);
2856 q = float64_div(q, float64_256, s);
2857 q = float64_sqrt(q, s);
2858 q = float64_div(float64_one, q, s);
2860 /* r in units of 1/256 rounded to nearest */
2861 /* s = (int)(256.0 * r + 0.5); */
2863 q = float64_mul(q, float64_256,s );
2864 q = float64_add(q, float64_half, s);
2865 q_int = float64_to_int64_round_to_zero(q, s);
2867 /* return (double)s / 256.0;*/
2868 return float64_div(int64_to_float64(q_int, s), float64_256, s);
2871 float32 HELPER(rsqrte_f32)(float32 a, CPUARMState *env)
2873 float_status *s = &env->vfp.standard_fp_status;
2879 val = float32_val(a);
2881 if (float32_is_any_nan(a)) {
2882 if (float32_is_signaling_nan(a)) {
2883 float_raise(float_flag_invalid, s);
2885 return float32_default_nan;
2886 } else if (float32_is_zero_or_denormal(a)) {
2887 if (!float32_is_zero(a)) {
2888 float_raise(float_flag_input_denormal, s);
2890 float_raise(float_flag_divbyzero, s);
2891 return float32_set_sign(float32_infinity, float32_is_neg(a));
2892 } else if (float32_is_neg(a)) {
2893 float_raise(float_flag_invalid, s);
2894 return float32_default_nan;
2895 } else if (float32_is_infinity(a)) {
2896 return float32_zero;
2899 /* Normalize to a double-precision value between 0.25 and 1.0,
2900 * preserving the parity of the exponent. */
2901 if ((val & 0x800000) == 0) {
2902 f64 = make_float64(((uint64_t)(val & 0x80000000) << 32)
2904 | ((uint64_t)(val & 0x7fffff) << 29));
2906 f64 = make_float64(((uint64_t)(val & 0x80000000) << 32)
2908 | ((uint64_t)(val & 0x7fffff) << 29));
2911 result_exp = (380 - ((val & 0x7f800000) >> 23)) / 2;
2913 f64 = recip_sqrt_estimate(f64, env);
2915 val64 = float64_val(f64);
2917 val = ((result_exp & 0xff) << 23)
2918 | ((val64 >> 29) & 0x7fffff);
2919 return make_float32(val);
2922 uint32_t HELPER(recpe_u32)(uint32_t a, CPUARMState *env)
2926 if ((a & 0x80000000) == 0) {
2930 f64 = make_float64((0x3feULL << 52)
2931 | ((int64_t)(a & 0x7fffffff) << 21));
2933 f64 = recip_estimate (f64, env);
2935 return 0x80000000 | ((float64_val(f64) >> 21) & 0x7fffffff);
2938 uint32_t HELPER(rsqrte_u32)(uint32_t a, CPUARMState *env)
2942 if ((a & 0xc0000000) == 0) {
2946 if (a & 0x80000000) {
2947 f64 = make_float64((0x3feULL << 52)
2948 | ((uint64_t)(a & 0x7fffffff) << 21));
2949 } else { /* bits 31-30 == '01' */
2950 f64 = make_float64((0x3fdULL << 52)
2951 | ((uint64_t)(a & 0x3fffffff) << 22));
2954 f64 = recip_sqrt_estimate(f64, env);
2956 return 0x80000000 | ((float64_val(f64) >> 21) & 0x7fffffff);
2959 /* VFPv4 fused multiply-accumulate */
2960 float32 VFP_HELPER(muladd, s)(float32 a, float32 b, float32 c, void *fpstp)
2962 float_status *fpst = fpstp;
2963 return float32_muladd(a, b, c, 0, fpst);
2966 float64 VFP_HELPER(muladd, d)(float64 a, float64 b, float64 c, void *fpstp)
2968 float_status *fpst = fpstp;
2969 return float64_muladd(a, b, c, 0, fpst);
2972 void HELPER(set_teecr)(CPUARMState *env, uint32_t val)
2975 if (env->teecr != val) {