4 #include "host-utils.h"
7 static int vfp_gdb_get_reg(CPUARMState *env, uint8_t *buf, int reg)
11 /* VFP data registers are always little-endian. */
12 nregs = arm_feature(env, ARM_FEATURE_VFP3) ? 32 : 16;
14 stfq_le_p(buf, env->vfp.regs[reg]);
17 if (arm_feature(env, ARM_FEATURE_NEON)) {
18 /* Aliases for Q regs. */
21 stfq_le_p(buf, env->vfp.regs[(reg - 32) * 2]);
22 stfq_le_p(buf + 8, env->vfp.regs[(reg - 32) * 2 + 1]);
26 switch (reg - nregs) {
27 case 0: stl_p(buf, env->vfp.xregs[ARM_VFP_FPSID]); return 4;
28 case 1: stl_p(buf, env->vfp.xregs[ARM_VFP_FPSCR]); return 4;
29 case 2: stl_p(buf, env->vfp.xregs[ARM_VFP_FPEXC]); return 4;
34 static int vfp_gdb_set_reg(CPUARMState *env, uint8_t *buf, int reg)
38 nregs = arm_feature(env, ARM_FEATURE_VFP3) ? 32 : 16;
40 env->vfp.regs[reg] = ldfq_le_p(buf);
43 if (arm_feature(env, ARM_FEATURE_NEON)) {
46 env->vfp.regs[(reg - 32) * 2] = ldfq_le_p(buf);
47 env->vfp.regs[(reg - 32) * 2 + 1] = ldfq_le_p(buf + 8);
51 switch (reg - nregs) {
52 case 0: env->vfp.xregs[ARM_VFP_FPSID] = ldl_p(buf); return 4;
53 case 1: env->vfp.xregs[ARM_VFP_FPSCR] = ldl_p(buf); return 4;
54 case 2: env->vfp.xregs[ARM_VFP_FPEXC] = ldl_p(buf) & (1 << 30); return 4;
59 void register_cp_regs_for_features(ARMCPU *cpu)
61 /* Register all the coprocessor registers based on feature bits */
62 CPUARMState *env = &cpu->env;
63 if (arm_feature(env, ARM_FEATURE_M)) {
64 /* M profile has no coprocessor registers */
70 ARMCPU *cpu_arm_init(const char *cpu_model)
74 static int inited = 0;
76 if (!object_class_by_name(cpu_model)) {
79 cpu = ARM_CPU(object_new(cpu_model));
81 env->cpu_model_str = cpu_model;
84 if (tcg_enabled() && !inited) {
90 if (arm_feature(env, ARM_FEATURE_NEON)) {
91 gdb_register_coprocessor(env, vfp_gdb_get_reg, vfp_gdb_set_reg,
92 51, "arm-neon.xml", 0);
93 } else if (arm_feature(env, ARM_FEATURE_VFP3)) {
94 gdb_register_coprocessor(env, vfp_gdb_get_reg, vfp_gdb_set_reg,
95 35, "arm-vfp3.xml", 0);
96 } else if (arm_feature(env, ARM_FEATURE_VFP)) {
97 gdb_register_coprocessor(env, vfp_gdb_get_reg, vfp_gdb_set_reg,
98 19, "arm-vfp.xml", 0);
104 typedef struct ARMCPUListState {
105 fprintf_function cpu_fprintf;
109 /* Sort alphabetically by type name, except for "any". */
110 static gint arm_cpu_list_compare(gconstpointer a, gconstpointer b)
112 ObjectClass *class_a = (ObjectClass *)a;
113 ObjectClass *class_b = (ObjectClass *)b;
114 const char *name_a, *name_b;
116 name_a = object_class_get_name(class_a);
117 name_b = object_class_get_name(class_b);
118 if (strcmp(name_a, "any") == 0) {
120 } else if (strcmp(name_b, "any") == 0) {
123 return strcmp(name_a, name_b);
127 static void arm_cpu_list_entry(gpointer data, gpointer user_data)
129 ObjectClass *oc = data;
130 ARMCPUListState *s = user_data;
132 (*s->cpu_fprintf)(s->file, " %s\n",
133 object_class_get_name(oc));
136 void arm_cpu_list(FILE *f, fprintf_function cpu_fprintf)
138 ARMCPUListState s = {
140 .cpu_fprintf = cpu_fprintf,
144 list = object_class_get_list(TYPE_ARM_CPU, false);
145 list = g_slist_sort(list, arm_cpu_list_compare);
146 (*cpu_fprintf)(f, "Available CPUs:\n");
147 g_slist_foreach(list, arm_cpu_list_entry, &s);
151 void define_one_arm_cp_reg_with_opaque(ARMCPU *cpu,
152 const ARMCPRegInfo *r, void *opaque)
154 /* Define implementations of coprocessor registers.
155 * We store these in a hashtable because typically
156 * there are less than 150 registers in a space which
157 * is 16*16*16*8*8 = 262144 in size.
158 * Wildcarding is supported for the crm, opc1 and opc2 fields.
159 * If a register is defined twice then the second definition is
160 * used, so this can be used to define some generic registers and
161 * then override them with implementation specific variations.
162 * At least one of the original and the second definition should
163 * include ARM_CP_OVERRIDE in its type bits -- this is just a guard
164 * against accidental use.
167 int crmmin = (r->crm == CP_ANY) ? 0 : r->crm;
168 int crmmax = (r->crm == CP_ANY) ? 15 : r->crm;
169 int opc1min = (r->opc1 == CP_ANY) ? 0 : r->opc1;
170 int opc1max = (r->opc1 == CP_ANY) ? 7 : r->opc1;
171 int opc2min = (r->opc2 == CP_ANY) ? 0 : r->opc2;
172 int opc2max = (r->opc2 == CP_ANY) ? 7 : r->opc2;
173 /* 64 bit registers have only CRm and Opc1 fields */
174 assert(!((r->type & ARM_CP_64BIT) && (r->opc2 || r->crn)));
175 /* Check that the register definition has enough info to handle
176 * reads and writes if they are permitted.
178 if (!(r->type & (ARM_CP_SPECIAL|ARM_CP_CONST))) {
179 if (r->access & PL3_R) {
180 assert(r->fieldoffset || r->readfn);
182 if (r->access & PL3_W) {
183 assert(r->fieldoffset || r->writefn);
186 /* Bad type field probably means missing sentinel at end of reg list */
187 assert(cptype_valid(r->type));
188 for (crm = crmmin; crm <= crmmax; crm++) {
189 for (opc1 = opc1min; opc1 <= opc1max; opc1++) {
190 for (opc2 = opc2min; opc2 <= opc2max; opc2++) {
191 uint32_t *key = g_new(uint32_t, 1);
192 ARMCPRegInfo *r2 = g_memdup(r, sizeof(ARMCPRegInfo));
193 int is64 = (r->type & ARM_CP_64BIT) ? 1 : 0;
194 *key = ENCODE_CP_REG(r->cp, is64, r->crn, crm, opc1, opc2);
196 /* Make sure reginfo passed to helpers for wildcarded regs
197 * has the correct crm/opc1/opc2 for this reg, not CP_ANY:
202 /* Overriding of an existing definition must be explicitly
205 if (!(r->type & ARM_CP_OVERRIDE)) {
206 ARMCPRegInfo *oldreg;
207 oldreg = g_hash_table_lookup(cpu->cp_regs, key);
208 if (oldreg && !(oldreg->type & ARM_CP_OVERRIDE)) {
209 fprintf(stderr, "Register redefined: cp=%d %d bit "
210 "crn=%d crm=%d opc1=%d opc2=%d, "
211 "was %s, now %s\n", r2->cp, 32 + 32 * is64,
212 r2->crn, r2->crm, r2->opc1, r2->opc2,
213 oldreg->name, r2->name);
217 g_hash_table_insert(cpu->cp_regs, key, r2);
223 void define_arm_cp_regs_with_opaque(ARMCPU *cpu,
224 const ARMCPRegInfo *regs, void *opaque)
226 /* Define a whole list of registers */
227 const ARMCPRegInfo *r;
228 for (r = regs; r->type != ARM_CP_SENTINEL; r++) {
229 define_one_arm_cp_reg_with_opaque(cpu, r, opaque);
233 const ARMCPRegInfo *get_arm_cp_reginfo(ARMCPU *cpu, uint32_t encoded_cp)
235 return g_hash_table_lookup(cpu->cp_regs, &encoded_cp);
238 int arm_cp_write_ignore(CPUARMState *env, const ARMCPRegInfo *ri,
241 /* Helper coprocessor write function for write-ignore registers */
245 int arm_cp_read_zero(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t *value)
247 /* Helper coprocessor write function for read-as-zero registers */
252 static int bad_mode_switch(CPUARMState *env, int mode)
254 /* Return true if it is not valid for us to switch to
255 * this CPU mode (ie all the UNPREDICTABLE cases in
256 * the ARM ARM CPSRWriteByInstr pseudocode).
259 case ARM_CPU_MODE_USR:
260 case ARM_CPU_MODE_SYS:
261 case ARM_CPU_MODE_SVC:
262 case ARM_CPU_MODE_ABT:
263 case ARM_CPU_MODE_UND:
264 case ARM_CPU_MODE_IRQ:
265 case ARM_CPU_MODE_FIQ:
272 uint32_t cpsr_read(CPUARMState *env)
276 return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) |
277 (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27)
278 | (env->thumb << 5) | ((env->condexec_bits & 3) << 25)
279 | ((env->condexec_bits & 0xfc) << 8)
283 void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask)
285 if (mask & CPSR_NZCV) {
286 env->ZF = (~val) & CPSR_Z;
288 env->CF = (val >> 29) & 1;
289 env->VF = (val << 3) & 0x80000000;
292 env->QF = ((val & CPSR_Q) != 0);
294 env->thumb = ((val & CPSR_T) != 0);
295 if (mask & CPSR_IT_0_1) {
296 env->condexec_bits &= ~3;
297 env->condexec_bits |= (val >> 25) & 3;
299 if (mask & CPSR_IT_2_7) {
300 env->condexec_bits &= 3;
301 env->condexec_bits |= (val >> 8) & 0xfc;
303 if (mask & CPSR_GE) {
304 env->GE = (val >> 16) & 0xf;
307 if ((env->uncached_cpsr ^ val) & mask & CPSR_M) {
308 if (bad_mode_switch(env, val & CPSR_M)) {
309 /* Attempt to switch to an invalid mode: this is UNPREDICTABLE.
310 * We choose to ignore the attempt and leave the CPSR M field
315 switch_mode(env, val & CPSR_M);
318 mask &= ~CACHED_CPSR_BITS;
319 env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask);
322 /* Sign/zero extend */
323 uint32_t HELPER(sxtb16)(uint32_t x)
326 res = (uint16_t)(int8_t)x;
327 res |= (uint32_t)(int8_t)(x >> 16) << 16;
331 uint32_t HELPER(uxtb16)(uint32_t x)
334 res = (uint16_t)(uint8_t)x;
335 res |= (uint32_t)(uint8_t)(x >> 16) << 16;
339 uint32_t HELPER(clz)(uint32_t x)
344 int32_t HELPER(sdiv)(int32_t num, int32_t den)
348 if (num == INT_MIN && den == -1)
353 uint32_t HELPER(udiv)(uint32_t num, uint32_t den)
360 uint32_t HELPER(rbit)(uint32_t x)
362 x = ((x & 0xff000000) >> 24)
363 | ((x & 0x00ff0000) >> 8)
364 | ((x & 0x0000ff00) << 8)
365 | ((x & 0x000000ff) << 24);
366 x = ((x & 0xf0f0f0f0) >> 4)
367 | ((x & 0x0f0f0f0f) << 4);
368 x = ((x & 0x88888888) >> 3)
369 | ((x & 0x44444444) >> 1)
370 | ((x & 0x22222222) << 1)
371 | ((x & 0x11111111) << 3);
375 uint32_t HELPER(abs)(uint32_t x)
377 return ((int32_t)x < 0) ? -x : x;
380 #if defined(CONFIG_USER_ONLY)
382 void do_interrupt (CPUARMState *env)
384 env->exception_index = -1;
387 int cpu_arm_handle_mmu_fault (CPUARMState *env, target_ulong address, int rw,
391 env->exception_index = EXCP_PREFETCH_ABORT;
392 env->cp15.c6_insn = address;
394 env->exception_index = EXCP_DATA_ABORT;
395 env->cp15.c6_data = address;
400 void HELPER(set_cp15)(CPUARMState *env, uint32_t insn, uint32_t val)
402 cpu_abort(env, "cp15 insn %08x\n", insn);
405 uint32_t HELPER(get_cp15)(CPUARMState *env, uint32_t insn)
407 cpu_abort(env, "cp15 insn %08x\n", insn);
410 /* These should probably raise undefined insn exceptions. */
411 void HELPER(v7m_msr)(CPUARMState *env, uint32_t reg, uint32_t val)
413 cpu_abort(env, "v7m_mrs %d\n", reg);
416 uint32_t HELPER(v7m_mrs)(CPUARMState *env, uint32_t reg)
418 cpu_abort(env, "v7m_mrs %d\n", reg);
422 void switch_mode(CPUARMState *env, int mode)
424 if (mode != ARM_CPU_MODE_USR)
425 cpu_abort(env, "Tried to switch out of user mode\n");
428 void HELPER(set_r13_banked)(CPUARMState *env, uint32_t mode, uint32_t val)
430 cpu_abort(env, "banked r13 write\n");
433 uint32_t HELPER(get_r13_banked)(CPUARMState *env, uint32_t mode)
435 cpu_abort(env, "banked r13 read\n");
441 /* Map CPU modes onto saved register banks. */
442 static inline int bank_number(CPUARMState *env, int mode)
445 case ARM_CPU_MODE_USR:
446 case ARM_CPU_MODE_SYS:
448 case ARM_CPU_MODE_SVC:
450 case ARM_CPU_MODE_ABT:
452 case ARM_CPU_MODE_UND:
454 case ARM_CPU_MODE_IRQ:
456 case ARM_CPU_MODE_FIQ:
459 cpu_abort(env, "Bad mode %x\n", mode);
463 void switch_mode(CPUARMState *env, int mode)
468 old_mode = env->uncached_cpsr & CPSR_M;
469 if (mode == old_mode)
472 if (old_mode == ARM_CPU_MODE_FIQ) {
473 memcpy (env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t));
474 memcpy (env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t));
475 } else if (mode == ARM_CPU_MODE_FIQ) {
476 memcpy (env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t));
477 memcpy (env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t));
480 i = bank_number(env, old_mode);
481 env->banked_r13[i] = env->regs[13];
482 env->banked_r14[i] = env->regs[14];
483 env->banked_spsr[i] = env->spsr;
485 i = bank_number(env, mode);
486 env->regs[13] = env->banked_r13[i];
487 env->regs[14] = env->banked_r14[i];
488 env->spsr = env->banked_spsr[i];
491 static void v7m_push(CPUARMState *env, uint32_t val)
494 stl_phys(env->regs[13], val);
497 static uint32_t v7m_pop(CPUARMState *env)
500 val = ldl_phys(env->regs[13]);
505 /* Switch to V7M main or process stack pointer. */
506 static void switch_v7m_sp(CPUARMState *env, int process)
509 if (env->v7m.current_sp != process) {
510 tmp = env->v7m.other_sp;
511 env->v7m.other_sp = env->regs[13];
513 env->v7m.current_sp = process;
517 static void do_v7m_exception_exit(CPUARMState *env)
522 type = env->regs[15];
523 if (env->v7m.exception != 0)
524 armv7m_nvic_complete_irq(env->nvic, env->v7m.exception);
526 /* Switch to the target stack. */
527 switch_v7m_sp(env, (type & 4) != 0);
529 env->regs[0] = v7m_pop(env);
530 env->regs[1] = v7m_pop(env);
531 env->regs[2] = v7m_pop(env);
532 env->regs[3] = v7m_pop(env);
533 env->regs[12] = v7m_pop(env);
534 env->regs[14] = v7m_pop(env);
535 env->regs[15] = v7m_pop(env);
537 xpsr_write(env, xpsr, 0xfffffdff);
538 /* Undo stack alignment. */
541 /* ??? The exception return type specifies Thread/Handler mode. However
542 this is also implied by the xPSR value. Not sure what to do
543 if there is a mismatch. */
544 /* ??? Likewise for mismatches between the CONTROL register and the stack
548 static void do_interrupt_v7m(CPUARMState *env)
550 uint32_t xpsr = xpsr_read(env);
555 if (env->v7m.current_sp)
557 if (env->v7m.exception == 0)
560 /* For exceptions we just mark as pending on the NVIC, and let that
562 /* TODO: Need to escalate if the current priority is higher than the
563 one we're raising. */
564 switch (env->exception_index) {
566 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE);
570 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SVC);
572 case EXCP_PREFETCH_ABORT:
573 case EXCP_DATA_ABORT:
574 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_MEM);
577 if (semihosting_enabled) {
579 nr = arm_lduw_code(env->regs[15], env->bswap_code) & 0xff;
582 env->regs[0] = do_arm_semihosting(env);
586 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_DEBUG);
589 env->v7m.exception = armv7m_nvic_acknowledge_irq(env->nvic);
591 case EXCP_EXCEPTION_EXIT:
592 do_v7m_exception_exit(env);
595 cpu_abort(env, "Unhandled exception 0x%x\n", env->exception_index);
596 return; /* Never happens. Keep compiler happy. */
599 /* Align stack pointer. */
600 /* ??? Should only do this if Configuration Control Register
601 STACKALIGN bit is set. */
602 if (env->regs[13] & 4) {
606 /* Switch to the handler mode. */
608 v7m_push(env, env->regs[15]);
609 v7m_push(env, env->regs[14]);
610 v7m_push(env, env->regs[12]);
611 v7m_push(env, env->regs[3]);
612 v7m_push(env, env->regs[2]);
613 v7m_push(env, env->regs[1]);
614 v7m_push(env, env->regs[0]);
615 switch_v7m_sp(env, 0);
617 env->condexec_bits = 0;
619 addr = ldl_phys(env->v7m.vecbase + env->v7m.exception * 4);
620 env->regs[15] = addr & 0xfffffffe;
621 env->thumb = addr & 1;
624 /* Handle a CPU exception. */
625 void do_interrupt(CPUARMState *env)
633 do_interrupt_v7m(env);
636 /* TODO: Vectored interrupt controller. */
637 switch (env->exception_index) {
639 new_mode = ARM_CPU_MODE_UND;
648 if (semihosting_enabled) {
649 /* Check for semihosting interrupt. */
651 mask = arm_lduw_code(env->regs[15] - 2, env->bswap_code) & 0xff;
653 mask = arm_ldl_code(env->regs[15] - 4, env->bswap_code)
656 /* Only intercept calls from privileged modes, to provide some
657 semblance of security. */
658 if (((mask == 0x123456 && !env->thumb)
659 || (mask == 0xab && env->thumb))
660 && (env->uncached_cpsr & CPSR_M) != ARM_CPU_MODE_USR) {
661 env->regs[0] = do_arm_semihosting(env);
665 new_mode = ARM_CPU_MODE_SVC;
668 /* The PC already points to the next instruction. */
672 /* See if this is a semihosting syscall. */
673 if (env->thumb && semihosting_enabled) {
674 mask = arm_lduw_code(env->regs[15], env->bswap_code) & 0xff;
676 && (env->uncached_cpsr & CPSR_M) != ARM_CPU_MODE_USR) {
678 env->regs[0] = do_arm_semihosting(env);
682 env->cp15.c5_insn = 2;
683 /* Fall through to prefetch abort. */
684 case EXCP_PREFETCH_ABORT:
685 new_mode = ARM_CPU_MODE_ABT;
687 mask = CPSR_A | CPSR_I;
690 case EXCP_DATA_ABORT:
691 new_mode = ARM_CPU_MODE_ABT;
693 mask = CPSR_A | CPSR_I;
697 new_mode = ARM_CPU_MODE_IRQ;
699 /* Disable IRQ and imprecise data aborts. */
700 mask = CPSR_A | CPSR_I;
704 new_mode = ARM_CPU_MODE_FIQ;
706 /* Disable FIQ, IRQ and imprecise data aborts. */
707 mask = CPSR_A | CPSR_I | CPSR_F;
711 cpu_abort(env, "Unhandled exception 0x%x\n", env->exception_index);
712 return; /* Never happens. Keep compiler happy. */
715 if (env->cp15.c1_sys & (1 << 13)) {
718 switch_mode (env, new_mode);
719 env->spsr = cpsr_read(env);
721 env->condexec_bits = 0;
722 /* Switch to the new mode, and to the correct instruction set. */
723 env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode;
724 env->uncached_cpsr |= mask;
725 /* this is a lie, as the was no c1_sys on V4T/V5, but who cares
726 * and we should just guard the thumb mode on V4 */
727 if (arm_feature(env, ARM_FEATURE_V4T)) {
728 env->thumb = (env->cp15.c1_sys & (1 << 30)) != 0;
730 env->regs[14] = env->regs[15] + offset;
731 env->regs[15] = addr;
732 env->interrupt_request |= CPU_INTERRUPT_EXITTB;
735 /* Check section/page access permissions.
736 Returns the page protection flags, or zero if the access is not
738 static inline int check_ap(CPUARMState *env, int ap, int domain_prot,
739 int access_type, int is_user)
743 if (domain_prot == 3) {
744 return PAGE_READ | PAGE_WRITE;
747 if (access_type == 1)
754 if (access_type == 1)
756 switch ((env->cp15.c1_sys >> 8) & 3) {
758 return is_user ? 0 : PAGE_READ;
765 return is_user ? 0 : PAGE_READ | PAGE_WRITE;
770 return PAGE_READ | PAGE_WRITE;
772 return PAGE_READ | PAGE_WRITE;
773 case 4: /* Reserved. */
776 return is_user ? 0 : prot_ro;
780 if (!arm_feature (env, ARM_FEATURE_V6K))
788 static uint32_t get_level1_table_address(CPUARMState *env, uint32_t address)
792 if (address & env->cp15.c2_mask)
793 table = env->cp15.c2_base1 & 0xffffc000;
795 table = env->cp15.c2_base0 & env->cp15.c2_base_mask;
797 table |= (address >> 18) & 0x3ffc;
801 static int get_phys_addr_v5(CPUARMState *env, uint32_t address, int access_type,
802 int is_user, uint32_t *phys_ptr, int *prot,
803 target_ulong *page_size)
814 /* Pagetable walk. */
815 /* Lookup l1 descriptor. */
816 table = get_level1_table_address(env, address);
817 desc = ldl_phys(table);
819 domain = (desc >> 5) & 0x0f;
820 domain_prot = (env->cp15.c3 >> (domain * 2)) & 3;
822 /* Section translation fault. */
826 if (domain_prot == 0 || domain_prot == 2) {
828 code = 9; /* Section domain fault. */
830 code = 11; /* Page domain fault. */
835 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
836 ap = (desc >> 10) & 3;
838 *page_size = 1024 * 1024;
840 /* Lookup l2 entry. */
842 /* Coarse pagetable. */
843 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
845 /* Fine pagetable. */
846 table = (desc & 0xfffff000) | ((address >> 8) & 0xffc);
848 desc = ldl_phys(table);
850 case 0: /* Page translation fault. */
853 case 1: /* 64k page. */
854 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
855 ap = (desc >> (4 + ((address >> 13) & 6))) & 3;
856 *page_size = 0x10000;
858 case 2: /* 4k page. */
859 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
860 ap = (desc >> (4 + ((address >> 13) & 6))) & 3;
863 case 3: /* 1k page. */
865 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
866 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
868 /* Page translation fault. */
873 phys_addr = (desc & 0xfffffc00) | (address & 0x3ff);
875 ap = (desc >> 4) & 3;
879 /* Never happens, but compiler isn't smart enough to tell. */
884 *prot = check_ap(env, ap, domain_prot, access_type, is_user);
886 /* Access permission fault. */
890 *phys_ptr = phys_addr;
893 return code | (domain << 4);
896 static int get_phys_addr_v6(CPUARMState *env, uint32_t address, int access_type,
897 int is_user, uint32_t *phys_ptr, int *prot,
898 target_ulong *page_size)
910 /* Pagetable walk. */
911 /* Lookup l1 descriptor. */
912 table = get_level1_table_address(env, address);
913 desc = ldl_phys(table);
916 /* Section translation fault. */
920 } else if (type == 2 && (desc & (1 << 18))) {
924 /* Section or page. */
925 domain = (desc >> 5) & 0x0f;
927 domain_prot = (env->cp15.c3 >> (domain * 2)) & 3;
928 if (domain_prot == 0 || domain_prot == 2) {
930 code = 9; /* Section domain fault. */
932 code = 11; /* Page domain fault. */
936 if (desc & (1 << 18)) {
938 phys_addr = (desc & 0xff000000) | (address & 0x00ffffff);
939 *page_size = 0x1000000;
942 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
943 *page_size = 0x100000;
945 ap = ((desc >> 10) & 3) | ((desc >> 13) & 4);
946 xn = desc & (1 << 4);
949 /* Lookup l2 entry. */
950 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
951 desc = ldl_phys(table);
952 ap = ((desc >> 4) & 3) | ((desc >> 7) & 4);
954 case 0: /* Page translation fault. */
957 case 1: /* 64k page. */
958 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
959 xn = desc & (1 << 15);
960 *page_size = 0x10000;
962 case 2: case 3: /* 4k page. */
963 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
968 /* Never happens, but compiler isn't smart enough to tell. */
973 if (domain_prot == 3) {
974 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
976 if (xn && access_type == 2)
979 /* The simplified model uses AP[0] as an access control bit. */
980 if ((env->cp15.c1_sys & (1 << 29)) && (ap & 1) == 0) {
981 /* Access flag fault. */
982 code = (code == 15) ? 6 : 3;
985 *prot = check_ap(env, ap, domain_prot, access_type, is_user);
987 /* Access permission fault. */
994 *phys_ptr = phys_addr;
997 return code | (domain << 4);
1000 static int get_phys_addr_mpu(CPUARMState *env, uint32_t address, int access_type,
1001 int is_user, uint32_t *phys_ptr, int *prot)
1007 *phys_ptr = address;
1008 for (n = 7; n >= 0; n--) {
1009 base = env->cp15.c6_region[n];
1010 if ((base & 1) == 0)
1012 mask = 1 << ((base >> 1) & 0x1f);
1013 /* Keep this shift separate from the above to avoid an
1014 (undefined) << 32. */
1015 mask = (mask << 1) - 1;
1016 if (((base ^ address) & ~mask) == 0)
1022 if (access_type == 2) {
1023 mask = env->cp15.c5_insn;
1025 mask = env->cp15.c5_data;
1027 mask = (mask >> (n * 4)) & 0xf;
1034 *prot = PAGE_READ | PAGE_WRITE;
1039 *prot |= PAGE_WRITE;
1042 *prot = PAGE_READ | PAGE_WRITE;
1053 /* Bad permission. */
1060 static inline int get_phys_addr(CPUARMState *env, uint32_t address,
1061 int access_type, int is_user,
1062 uint32_t *phys_ptr, int *prot,
1063 target_ulong *page_size)
1065 /* Fast Context Switch Extension. */
1066 if (address < 0x02000000)
1067 address += env->cp15.c13_fcse;
1069 if ((env->cp15.c1_sys & 1) == 0) {
1070 /* MMU/MPU disabled. */
1071 *phys_ptr = address;
1072 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
1073 *page_size = TARGET_PAGE_SIZE;
1075 } else if (arm_feature(env, ARM_FEATURE_MPU)) {
1076 *page_size = TARGET_PAGE_SIZE;
1077 return get_phys_addr_mpu(env, address, access_type, is_user, phys_ptr,
1079 } else if (env->cp15.c1_sys & (1 << 23)) {
1080 return get_phys_addr_v6(env, address, access_type, is_user, phys_ptr,
1083 return get_phys_addr_v5(env, address, access_type, is_user, phys_ptr,
1088 int cpu_arm_handle_mmu_fault (CPUARMState *env, target_ulong address,
1089 int access_type, int mmu_idx)
1092 target_ulong page_size;
1096 is_user = mmu_idx == MMU_USER_IDX;
1097 ret = get_phys_addr(env, address, access_type, is_user, &phys_addr, &prot,
1100 /* Map a single [sub]page. */
1101 phys_addr &= ~(uint32_t)0x3ff;
1102 address &= ~(uint32_t)0x3ff;
1103 tlb_set_page (env, address, phys_addr, prot, mmu_idx, page_size);
1107 if (access_type == 2) {
1108 env->cp15.c5_insn = ret;
1109 env->cp15.c6_insn = address;
1110 env->exception_index = EXCP_PREFETCH_ABORT;
1112 env->cp15.c5_data = ret;
1113 if (access_type == 1 && arm_feature(env, ARM_FEATURE_V6))
1114 env->cp15.c5_data |= (1 << 11);
1115 env->cp15.c6_data = address;
1116 env->exception_index = EXCP_DATA_ABORT;
1121 target_phys_addr_t cpu_get_phys_page_debug(CPUARMState *env, target_ulong addr)
1124 target_ulong page_size;
1128 ret = get_phys_addr(env, addr, 0, 0, &phys_addr, &prot, &page_size);
1136 /* Return basic MPU access permission bits. */
1137 static uint32_t simple_mpu_ap_bits(uint32_t val)
1144 for (i = 0; i < 16; i += 2) {
1145 ret |= (val >> i) & mask;
1151 /* Pad basic MPU access permission bits to extended format. */
1152 static uint32_t extended_mpu_ap_bits(uint32_t val)
1159 for (i = 0; i < 16; i += 2) {
1160 ret |= (val & mask) << i;
1166 void HELPER(set_cp15)(CPUARMState *env, uint32_t insn, uint32_t val)
1172 op1 = (insn >> 21) & 7;
1173 op2 = (insn >> 5) & 7;
1175 switch ((insn >> 16) & 0xf) {
1178 if (arm_feature(env, ARM_FEATURE_XSCALE))
1180 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1182 if (arm_feature(env, ARM_FEATURE_V7)
1183 && op1 == 2 && crm == 0 && op2 == 0) {
1184 env->cp15.c0_cssel = val & 0xf;
1188 case 1: /* System configuration. */
1189 if (arm_feature(env, ARM_FEATURE_V7)
1190 && op1 == 0 && crm == 1 && op2 == 0) {
1191 env->cp15.c1_scr = val;
1194 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1198 if (!arm_feature(env, ARM_FEATURE_XSCALE) || crm == 0)
1199 env->cp15.c1_sys = val;
1200 /* ??? Lots of these bits are not implemented. */
1201 /* This may enable/disable the MMU, so do a TLB flush. */
1204 case 1: /* Auxiliary control register. */
1205 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
1206 env->cp15.c1_xscaleauxcr = val;
1209 /* Not implemented. */
1212 if (arm_feature(env, ARM_FEATURE_XSCALE))
1214 if (env->cp15.c1_coproc != val) {
1215 env->cp15.c1_coproc = val;
1216 /* ??? Is this safe when called from within a TB? */
1224 case 2: /* MMU Page table control / MPU cache control. */
1225 if (arm_feature(env, ARM_FEATURE_MPU)) {
1228 env->cp15.c2_data = val;
1231 env->cp15.c2_insn = val;
1239 env->cp15.c2_base0 = val;
1242 env->cp15.c2_base1 = val;
1246 env->cp15.c2_control = val;
1247 env->cp15.c2_mask = ~(((uint32_t)0xffffffffu) >> val);
1248 env->cp15.c2_base_mask = ~((uint32_t)0x3fffu >> val);
1255 case 3: /* MMU Domain access control / MPU write buffer control. */
1257 tlb_flush(env, 1); /* Flush TLB as domain not tracked in TLB */
1259 case 4: /* Reserved. */
1261 case 5: /* MMU Fault status / MPU access permission. */
1262 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1266 if (arm_feature(env, ARM_FEATURE_MPU))
1267 val = extended_mpu_ap_bits(val);
1268 env->cp15.c5_data = val;
1271 if (arm_feature(env, ARM_FEATURE_MPU))
1272 val = extended_mpu_ap_bits(val);
1273 env->cp15.c5_insn = val;
1276 if (!arm_feature(env, ARM_FEATURE_MPU))
1278 env->cp15.c5_data = val;
1281 if (!arm_feature(env, ARM_FEATURE_MPU))
1283 env->cp15.c5_insn = val;
1289 case 6: /* MMU Fault address / MPU base/size. */
1290 if (arm_feature(env, ARM_FEATURE_MPU)) {
1293 env->cp15.c6_region[crm] = val;
1295 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1299 env->cp15.c6_data = val;
1301 case 1: /* ??? This is WFAR on armv6 */
1303 env->cp15.c6_insn = val;
1310 case 7: /* Cache control. */
1311 env->cp15.c15_i_max = 0x000;
1312 env->cp15.c15_i_min = 0xff0;
1316 /* No cache, so nothing to do except VA->PA translations. */
1317 if (arm_feature(env, ARM_FEATURE_VAPA)) {
1320 if (arm_feature(env, ARM_FEATURE_V7)) {
1321 env->cp15.c7_par = val & 0xfffff6ff;
1323 env->cp15.c7_par = val & 0xfffff1ff;
1328 target_ulong page_size;
1330 int ret, is_user = op2 & 2;
1331 int access_type = op2 & 1;
1334 /* Other states are only available with TrustZone */
1337 ret = get_phys_addr(env, val, access_type, is_user,
1338 &phys_addr, &prot, &page_size);
1340 /* We do not set any attribute bits in the PAR */
1341 if (page_size == (1 << 24)
1342 && arm_feature(env, ARM_FEATURE_V7)) {
1343 env->cp15.c7_par = (phys_addr & 0xff000000) | 1 << 1;
1345 env->cp15.c7_par = phys_addr & 0xfffff000;
1348 env->cp15.c7_par = ((ret & (10 << 1)) >> 5) |
1349 ((ret & (12 << 1)) >> 6) |
1350 ((ret & 0xf) << 1) | 1;
1357 case 8: /* MMU TLB control. */
1359 case 0: /* Invalidate all (TLBIALL) */
1362 case 1: /* Invalidate single TLB entry by MVA and ASID (TLBIMVA) */
1363 tlb_flush_page(env, val & TARGET_PAGE_MASK);
1365 case 2: /* Invalidate by ASID (TLBIASID) */
1366 tlb_flush(env, val == 0);
1368 case 3: /* Invalidate single entry by MVA, all ASIDs (TLBIMVAA) */
1369 tlb_flush_page(env, val & TARGET_PAGE_MASK);
1376 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1378 if (arm_feature(env, ARM_FEATURE_STRONGARM))
1379 break; /* Ignore ReadBuffer access */
1381 case 0: /* Cache lockdown. */
1383 case 0: /* L1 cache. */
1386 env->cp15.c9_data = val;
1389 env->cp15.c9_insn = val;
1395 case 1: /* L2 cache. */
1396 /* Ignore writes to L2 lockdown/auxiliary registers. */
1402 case 1: /* TCM memory region registers. */
1403 /* Not implemented. */
1405 case 12: /* Performance monitor control */
1406 /* Performance monitors are implementation defined in v7,
1407 * but with an ARM recommended set of registers, which we
1408 * follow (although we don't actually implement any counters)
1410 if (!arm_feature(env, ARM_FEATURE_V7)) {
1414 case 0: /* performance monitor control register */
1415 /* only the DP, X, D and E bits are writable */
1416 env->cp15.c9_pmcr &= ~0x39;
1417 env->cp15.c9_pmcr |= (val & 0x39);
1419 case 1: /* Count enable set register */
1421 env->cp15.c9_pmcnten |= val;
1423 case 2: /* Count enable clear */
1425 env->cp15.c9_pmcnten &= ~val;
1427 case 3: /* Overflow flag status */
1428 env->cp15.c9_pmovsr &= ~val;
1430 case 4: /* Software increment */
1431 /* RAZ/WI since we don't implement the software-count event */
1433 case 5: /* Event counter selection register */
1434 /* Since we don't implement any events, writing to this register
1435 * is actually UNPREDICTABLE. So we choose to RAZ/WI.
1442 case 13: /* Performance counters */
1443 if (!arm_feature(env, ARM_FEATURE_V7)) {
1447 case 0: /* Cycle count register: not implemented, so RAZ/WI */
1449 case 1: /* Event type select */
1450 env->cp15.c9_pmxevtyper = val & 0xff;
1452 case 2: /* Event count register */
1453 /* Unimplemented (we have no events), RAZ/WI */
1459 case 14: /* Performance monitor control */
1460 if (!arm_feature(env, ARM_FEATURE_V7)) {
1464 case 0: /* user enable */
1465 env->cp15.c9_pmuserenr = val & 1;
1466 /* changes access rights for cp registers, so flush tbs */
1469 case 1: /* interrupt enable set */
1470 /* We have no event counters so only the C bit can be changed */
1472 env->cp15.c9_pminten |= val;
1474 case 2: /* interrupt enable clear */
1476 env->cp15.c9_pminten &= ~val;
1484 case 10: /* MMU TLB lockdown. */
1485 /* ??? TLB lockdown not implemented. */
1487 case 12: /* Reserved. */
1489 case 13: /* Process ID. */
1492 /* Unlike real hardware the qemu TLB uses virtual addresses,
1493 not modified virtual addresses, so this causes a TLB flush.
1495 if (env->cp15.c13_fcse != val)
1497 env->cp15.c13_fcse = val;
1500 /* This changes the ASID, so do a TLB flush. */
1501 if (env->cp15.c13_context != val
1502 && !arm_feature(env, ARM_FEATURE_MPU))
1504 env->cp15.c13_context = val;
1510 case 14: /* Generic timer */
1511 if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) {
1512 /* Dummy implementation: RAZ/WI for all */
1516 case 15: /* Implementation specific. */
1517 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
1518 if (op2 == 0 && crm == 1) {
1519 if (env->cp15.c15_cpar != (val & 0x3fff)) {
1520 /* Changes cp0 to cp13 behavior, so needs a TB flush. */
1522 env->cp15.c15_cpar = val & 0x3fff;
1528 if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
1532 case 1: /* Set TI925T configuration. */
1533 env->cp15.c15_ticonfig = val & 0xe7;
1534 env->cp15.c0_cpuid = (val & (1 << 5)) ? /* OS_TYPE bit */
1535 ARM_CPUID_TI915T : ARM_CPUID_TI925T;
1537 case 2: /* Set I_max. */
1538 env->cp15.c15_i_max = val;
1540 case 3: /* Set I_min. */
1541 env->cp15.c15_i_min = val;
1543 case 4: /* Set thread-ID. */
1544 env->cp15.c15_threadid = val & 0xffff;
1546 case 8: /* Wait-for-interrupt (deprecated). */
1547 cpu_interrupt(env, CPU_INTERRUPT_HALT);
1553 if (ARM_CPUID(env) == ARM_CPUID_CORTEXA9) {
1556 if ((op1 == 0) && (op2 == 0)) {
1557 env->cp15.c15_power_control = val;
1558 } else if ((op1 == 0) && (op2 == 1)) {
1559 env->cp15.c15_diagnostic = val;
1560 } else if ((op1 == 0) && (op2 == 2)) {
1561 env->cp15.c15_power_diagnostic = val;
1571 /* ??? For debugging only. Should raise illegal instruction exception. */
1572 cpu_abort(env, "Unimplemented cp15 register write (c%d, c%d, {%d, %d})\n",
1573 (insn >> 16) & 0xf, crm, op1, op2);
1576 uint32_t HELPER(get_cp15)(CPUARMState *env, uint32_t insn)
1582 op1 = (insn >> 21) & 7;
1583 op2 = (insn >> 5) & 7;
1585 switch ((insn >> 16) & 0xf) {
1586 case 0: /* ID codes. */
1592 case 0: /* Device ID. */
1593 return env->cp15.c0_cpuid;
1594 case 1: /* Cache Type. */
1595 return env->cp15.c0_cachetype;
1596 case 2: /* TCM status. */
1598 case 3: /* TLB type register. */
1599 return 0; /* No lockable TLB entries. */
1601 /* The MPIDR was standardised in v7; prior to
1602 * this it was implemented only in the 11MPCore.
1603 * For all other pre-v7 cores it does not exist.
1605 if (arm_feature(env, ARM_FEATURE_V7) ||
1606 ARM_CPUID(env) == ARM_CPUID_ARM11MPCORE) {
1607 int mpidr = env->cpu_index;
1608 /* We don't support setting cluster ID ([8..11])
1609 * so these bits always RAZ.
1611 if (arm_feature(env, ARM_FEATURE_V7MP)) {
1613 /* Cores which are uniprocessor (non-coherent)
1614 * but still implement the MP extensions set
1615 * bit 30. (For instance, A9UP.) However we do
1616 * not currently model any of those cores.
1621 /* otherwise fall through to the unimplemented-reg case */
1626 if (!arm_feature(env, ARM_FEATURE_V6))
1628 return env->cp15.c0_c1[op2];
1630 if (!arm_feature(env, ARM_FEATURE_V6))
1632 return env->cp15.c0_c2[op2];
1633 case 3: case 4: case 5: case 6: case 7:
1639 /* These registers aren't documented on arm11 cores. However
1640 Linux looks at them anyway. */
1641 if (!arm_feature(env, ARM_FEATURE_V6))
1645 if (!arm_feature(env, ARM_FEATURE_V7))
1650 return env->cp15.c0_ccsid[env->cp15.c0_cssel];
1652 return env->cp15.c0_clid;
1658 if (op2 != 0 || crm != 0)
1660 return env->cp15.c0_cssel;
1664 case 1: /* System configuration. */
1665 if (arm_feature(env, ARM_FEATURE_V7)
1666 && op1 == 0 && crm == 1 && op2 == 0) {
1667 return env->cp15.c1_scr;
1669 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1672 case 0: /* Control register. */
1673 return env->cp15.c1_sys;
1674 case 1: /* Auxiliary control register. */
1675 if (arm_feature(env, ARM_FEATURE_XSCALE))
1676 return env->cp15.c1_xscaleauxcr;
1677 if (!arm_feature(env, ARM_FEATURE_AUXCR))
1679 switch (ARM_CPUID(env)) {
1680 case ARM_CPUID_ARM1026:
1682 case ARM_CPUID_ARM1136:
1683 case ARM_CPUID_ARM1136_R2:
1684 case ARM_CPUID_ARM1176:
1686 case ARM_CPUID_ARM11MPCORE:
1688 case ARM_CPUID_CORTEXA8:
1690 case ARM_CPUID_CORTEXA9:
1691 case ARM_CPUID_CORTEXA15:
1696 case 2: /* Coprocessor access register. */
1697 if (arm_feature(env, ARM_FEATURE_XSCALE))
1699 return env->cp15.c1_coproc;
1703 case 2: /* MMU Page table control / MPU cache control. */
1704 if (arm_feature(env, ARM_FEATURE_MPU)) {
1707 return env->cp15.c2_data;
1710 return env->cp15.c2_insn;
1718 return env->cp15.c2_base0;
1720 return env->cp15.c2_base1;
1722 return env->cp15.c2_control;
1727 case 3: /* MMU Domain access control / MPU write buffer control. */
1728 return env->cp15.c3;
1729 case 4: /* Reserved. */
1731 case 5: /* MMU Fault status / MPU access permission. */
1732 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1736 if (arm_feature(env, ARM_FEATURE_MPU))
1737 return simple_mpu_ap_bits(env->cp15.c5_data);
1738 return env->cp15.c5_data;
1740 if (arm_feature(env, ARM_FEATURE_MPU))
1741 return simple_mpu_ap_bits(env->cp15.c5_insn);
1742 return env->cp15.c5_insn;
1744 if (!arm_feature(env, ARM_FEATURE_MPU))
1746 return env->cp15.c5_data;
1748 if (!arm_feature(env, ARM_FEATURE_MPU))
1750 return env->cp15.c5_insn;
1754 case 6: /* MMU Fault address. */
1755 if (arm_feature(env, ARM_FEATURE_MPU)) {
1758 return env->cp15.c6_region[crm];
1760 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1764 return env->cp15.c6_data;
1766 if (arm_feature(env, ARM_FEATURE_V6)) {
1767 /* Watchpoint Fault Adrress. */
1768 return 0; /* Not implemented. */
1770 /* Instruction Fault Adrress. */
1771 /* Arm9 doesn't have an IFAR, but implementing it anyway
1772 shouldn't do any harm. */
1773 return env->cp15.c6_insn;
1776 if (arm_feature(env, ARM_FEATURE_V6)) {
1777 /* Instruction Fault Adrress. */
1778 return env->cp15.c6_insn;
1786 case 7: /* Cache control. */
1787 if (crm == 4 && op1 == 0 && op2 == 0) {
1788 return env->cp15.c7_par;
1790 /* FIXME: Should only clear Z flag if destination is r15. */
1793 case 8: /* MMU TLB control. */
1797 case 0: /* Cache lockdown */
1799 case 0: /* L1 cache. */
1800 if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
1805 return env->cp15.c9_data;
1807 return env->cp15.c9_insn;
1811 case 1: /* L2 cache */
1812 /* L2 Lockdown and Auxiliary control. */
1815 /* L2 cache lockdown (A8 only) */
1818 /* L2 cache auxiliary control (A8) or control (A15) */
1819 if (ARM_CPUID(env) == ARM_CPUID_CORTEXA15) {
1820 /* Linux wants the number of processors from here.
1821 * Might as well set the interrupt-controller bit too.
1823 return ((smp_cpus - 1) << 24) | (1 << 23);
1827 /* L2 cache extended control (A15) */
1836 case 12: /* Performance monitor control */
1837 if (!arm_feature(env, ARM_FEATURE_V7)) {
1841 case 0: /* performance monitor control register */
1842 return env->cp15.c9_pmcr;
1843 case 1: /* count enable set */
1844 case 2: /* count enable clear */
1845 return env->cp15.c9_pmcnten;
1846 case 3: /* overflow flag status */
1847 return env->cp15.c9_pmovsr;
1848 case 4: /* software increment */
1849 case 5: /* event counter selection register */
1850 return 0; /* Unimplemented, RAZ/WI */
1854 case 13: /* Performance counters */
1855 if (!arm_feature(env, ARM_FEATURE_V7)) {
1859 case 1: /* Event type select */
1860 return env->cp15.c9_pmxevtyper;
1861 case 0: /* Cycle count register */
1862 case 2: /* Event count register */
1863 /* Unimplemented, so RAZ/WI */
1868 case 14: /* Performance monitor control */
1869 if (!arm_feature(env, ARM_FEATURE_V7)) {
1873 case 0: /* user enable */
1874 return env->cp15.c9_pmuserenr;
1875 case 1: /* interrupt enable set */
1876 case 2: /* interrupt enable clear */
1877 return env->cp15.c9_pminten;
1885 case 10: /* MMU TLB lockdown. */
1886 /* ??? TLB lockdown not implemented. */
1888 case 11: /* TCM DMA control. */
1889 case 12: /* Reserved. */
1891 case 13: /* Process ID. */
1894 return env->cp15.c13_fcse;
1896 return env->cp15.c13_context;
1900 case 14: /* Generic timer */
1901 if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) {
1902 /* Dummy implementation: RAZ/WI for all */
1906 case 15: /* Implementation specific. */
1907 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
1908 if (op2 == 0 && crm == 1)
1909 return env->cp15.c15_cpar;
1913 if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
1917 case 1: /* Read TI925T configuration. */
1918 return env->cp15.c15_ticonfig;
1919 case 2: /* Read I_max. */
1920 return env->cp15.c15_i_max;
1921 case 3: /* Read I_min. */
1922 return env->cp15.c15_i_min;
1923 case 4: /* Read thread-ID. */
1924 return env->cp15.c15_threadid;
1925 case 8: /* TI925T_status */
1928 /* TODO: Peripheral port remap register:
1929 * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt
1930 * controller base address at $rn & ~0xfff and map size of
1931 * 0x200 << ($rn & 0xfff), when MMU is off. */
1934 if (ARM_CPUID(env) == ARM_CPUID_CORTEXA9) {
1937 if ((op1 == 4) && (op2 == 0)) {
1938 /* The config_base_address should hold the value of
1939 * the peripheral base. ARM should get this from a CPU
1940 * object property, but that support isn't available in
1941 * December 2011. Default to 0 for now and board models
1942 * that care can set it by a private hook */
1943 return env->cp15.c15_config_base_address;
1944 } else if ((op1 == 0) && (op2 == 0)) {
1945 /* power_control should be set to maximum latency. Again,
1946 default to 0 and set by private hook */
1947 return env->cp15.c15_power_control;
1948 } else if ((op1 == 0) && (op2 == 1)) {
1949 return env->cp15.c15_diagnostic;
1950 } else if ((op1 == 0) && (op2 == 2)) {
1951 return env->cp15.c15_power_diagnostic;
1954 case 1: /* NEON Busy */
1956 case 5: /* tlb lockdown */
1959 if ((op1 == 5) && (op2 == 2)) {
1971 /* ??? For debugging only. Should raise illegal instruction exception. */
1972 cpu_abort(env, "Unimplemented cp15 register read (c%d, c%d, {%d, %d})\n",
1973 (insn >> 16) & 0xf, crm, op1, op2);
1977 void HELPER(set_r13_banked)(CPUARMState *env, uint32_t mode, uint32_t val)
1979 if ((env->uncached_cpsr & CPSR_M) == mode) {
1980 env->regs[13] = val;
1982 env->banked_r13[bank_number(env, mode)] = val;
1986 uint32_t HELPER(get_r13_banked)(CPUARMState *env, uint32_t mode)
1988 if ((env->uncached_cpsr & CPSR_M) == mode) {
1989 return env->regs[13];
1991 return env->banked_r13[bank_number(env, mode)];
1995 uint32_t HELPER(v7m_mrs)(CPUARMState *env, uint32_t reg)
1999 return xpsr_read(env) & 0xf8000000;
2001 return xpsr_read(env) & 0xf80001ff;
2003 return xpsr_read(env) & 0xff00fc00;
2005 return xpsr_read(env) & 0xff00fdff;
2007 return xpsr_read(env) & 0x000001ff;
2009 return xpsr_read(env) & 0x0700fc00;
2011 return xpsr_read(env) & 0x0700edff;
2013 return env->v7m.current_sp ? env->v7m.other_sp : env->regs[13];
2015 return env->v7m.current_sp ? env->regs[13] : env->v7m.other_sp;
2016 case 16: /* PRIMASK */
2017 return (env->uncached_cpsr & CPSR_I) != 0;
2018 case 17: /* BASEPRI */
2019 case 18: /* BASEPRI_MAX */
2020 return env->v7m.basepri;
2021 case 19: /* FAULTMASK */
2022 return (env->uncached_cpsr & CPSR_F) != 0;
2023 case 20: /* CONTROL */
2024 return env->v7m.control;
2026 /* ??? For debugging only. */
2027 cpu_abort(env, "Unimplemented system register read (%d)\n", reg);
2032 void HELPER(v7m_msr)(CPUARMState *env, uint32_t reg, uint32_t val)
2036 xpsr_write(env, val, 0xf8000000);
2039 xpsr_write(env, val, 0xf8000000);
2042 xpsr_write(env, val, 0xfe00fc00);
2045 xpsr_write(env, val, 0xfe00fc00);
2048 /* IPSR bits are readonly. */
2051 xpsr_write(env, val, 0x0600fc00);
2054 xpsr_write(env, val, 0x0600fc00);
2057 if (env->v7m.current_sp)
2058 env->v7m.other_sp = val;
2060 env->regs[13] = val;
2063 if (env->v7m.current_sp)
2064 env->regs[13] = val;
2066 env->v7m.other_sp = val;
2068 case 16: /* PRIMASK */
2070 env->uncached_cpsr |= CPSR_I;
2072 env->uncached_cpsr &= ~CPSR_I;
2074 case 17: /* BASEPRI */
2075 env->v7m.basepri = val & 0xff;
2077 case 18: /* BASEPRI_MAX */
2079 if (val != 0 && (val < env->v7m.basepri || env->v7m.basepri == 0))
2080 env->v7m.basepri = val;
2082 case 19: /* FAULTMASK */
2084 env->uncached_cpsr |= CPSR_F;
2086 env->uncached_cpsr &= ~CPSR_F;
2088 case 20: /* CONTROL */
2089 env->v7m.control = val & 3;
2090 switch_v7m_sp(env, (val & 2) != 0);
2093 /* ??? For debugging only. */
2094 cpu_abort(env, "Unimplemented system register write (%d)\n", reg);
2101 /* Note that signed overflow is undefined in C. The following routines are
2102 careful to use unsigned types where modulo arithmetic is required.
2103 Failure to do so _will_ break on newer gcc. */
2105 /* Signed saturating arithmetic. */
2107 /* Perform 16-bit signed saturating addition. */
2108 static inline uint16_t add16_sat(uint16_t a, uint16_t b)
2113 if (((res ^ a) & 0x8000) && !((a ^ b) & 0x8000)) {
2122 /* Perform 8-bit signed saturating addition. */
2123 static inline uint8_t add8_sat(uint8_t a, uint8_t b)
2128 if (((res ^ a) & 0x80) && !((a ^ b) & 0x80)) {
2137 /* Perform 16-bit signed saturating subtraction. */
2138 static inline uint16_t sub16_sat(uint16_t a, uint16_t b)
2143 if (((res ^ a) & 0x8000) && ((a ^ b) & 0x8000)) {
2152 /* Perform 8-bit signed saturating subtraction. */
2153 static inline uint8_t sub8_sat(uint8_t a, uint8_t b)
2158 if (((res ^ a) & 0x80) && ((a ^ b) & 0x80)) {
2167 #define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16);
2168 #define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16);
2169 #define ADD8(a, b, n) RESULT(add8_sat(a, b), n, 8);
2170 #define SUB8(a, b, n) RESULT(sub8_sat(a, b), n, 8);
2173 #include "op_addsub.h"
2175 /* Unsigned saturating arithmetic. */
2176 static inline uint16_t add16_usat(uint16_t a, uint16_t b)
2185 static inline uint16_t sub16_usat(uint16_t a, uint16_t b)
2193 static inline uint8_t add8_usat(uint8_t a, uint8_t b)
2202 static inline uint8_t sub8_usat(uint8_t a, uint8_t b)
2210 #define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16);
2211 #define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16);
2212 #define ADD8(a, b, n) RESULT(add8_usat(a, b), n, 8);
2213 #define SUB8(a, b, n) RESULT(sub8_usat(a, b), n, 8);
2216 #include "op_addsub.h"
2218 /* Signed modulo arithmetic. */
2219 #define SARITH16(a, b, n, op) do { \
2221 sum = (int32_t)(int16_t)(a) op (int32_t)(int16_t)(b); \
2222 RESULT(sum, n, 16); \
2224 ge |= 3 << (n * 2); \
2227 #define SARITH8(a, b, n, op) do { \
2229 sum = (int32_t)(int8_t)(a) op (int32_t)(int8_t)(b); \
2230 RESULT(sum, n, 8); \
2236 #define ADD16(a, b, n) SARITH16(a, b, n, +)
2237 #define SUB16(a, b, n) SARITH16(a, b, n, -)
2238 #define ADD8(a, b, n) SARITH8(a, b, n, +)
2239 #define SUB8(a, b, n) SARITH8(a, b, n, -)
2243 #include "op_addsub.h"
2245 /* Unsigned modulo arithmetic. */
2246 #define ADD16(a, b, n) do { \
2248 sum = (uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b); \
2249 RESULT(sum, n, 16); \
2250 if ((sum >> 16) == 1) \
2251 ge |= 3 << (n * 2); \
2254 #define ADD8(a, b, n) do { \
2256 sum = (uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b); \
2257 RESULT(sum, n, 8); \
2258 if ((sum >> 8) == 1) \
2262 #define SUB16(a, b, n) do { \
2264 sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \
2265 RESULT(sum, n, 16); \
2266 if ((sum >> 16) == 0) \
2267 ge |= 3 << (n * 2); \
2270 #define SUB8(a, b, n) do { \
2272 sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \
2273 RESULT(sum, n, 8); \
2274 if ((sum >> 8) == 0) \
2281 #include "op_addsub.h"
2283 /* Halved signed arithmetic. */
2284 #define ADD16(a, b, n) \
2285 RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16)
2286 #define SUB16(a, b, n) \
2287 RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16)
2288 #define ADD8(a, b, n) \
2289 RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8)
2290 #define SUB8(a, b, n) \
2291 RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8)
2294 #include "op_addsub.h"
2296 /* Halved unsigned arithmetic. */
2297 #define ADD16(a, b, n) \
2298 RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16)
2299 #define SUB16(a, b, n) \
2300 RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16)
2301 #define ADD8(a, b, n) \
2302 RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8)
2303 #define SUB8(a, b, n) \
2304 RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8)
2307 #include "op_addsub.h"
2309 static inline uint8_t do_usad(uint8_t a, uint8_t b)
2317 /* Unsigned sum of absolute byte differences. */
2318 uint32_t HELPER(usad8)(uint32_t a, uint32_t b)
2321 sum = do_usad(a, b);
2322 sum += do_usad(a >> 8, b >> 8);
2323 sum += do_usad(a >> 16, b >>16);
2324 sum += do_usad(a >> 24, b >> 24);
2328 /* For ARMv6 SEL instruction. */
2329 uint32_t HELPER(sel_flags)(uint32_t flags, uint32_t a, uint32_t b)
2342 return (a & mask) | (b & ~mask);
2345 uint32_t HELPER(logicq_cc)(uint64_t val)
2347 return (val >> 32) | (val != 0);
2350 /* VFP support. We follow the convention used for VFP instrunctions:
2351 Single precition routines have a "s" suffix, double precision a
2354 /* Convert host exception flags to vfp form. */
2355 static inline int vfp_exceptbits_from_host(int host_bits)
2357 int target_bits = 0;
2359 if (host_bits & float_flag_invalid)
2361 if (host_bits & float_flag_divbyzero)
2363 if (host_bits & float_flag_overflow)
2365 if (host_bits & (float_flag_underflow | float_flag_output_denormal))
2367 if (host_bits & float_flag_inexact)
2368 target_bits |= 0x10;
2369 if (host_bits & float_flag_input_denormal)
2370 target_bits |= 0x80;
2374 uint32_t HELPER(vfp_get_fpscr)(CPUARMState *env)
2379 fpscr = (env->vfp.xregs[ARM_VFP_FPSCR] & 0xffc8ffff)
2380 | (env->vfp.vec_len << 16)
2381 | (env->vfp.vec_stride << 20);
2382 i = get_float_exception_flags(&env->vfp.fp_status);
2383 i |= get_float_exception_flags(&env->vfp.standard_fp_status);
2384 fpscr |= vfp_exceptbits_from_host(i);
2388 uint32_t vfp_get_fpscr(CPUARMState *env)
2390 return HELPER(vfp_get_fpscr)(env);
2393 /* Convert vfp exception flags to target form. */
2394 static inline int vfp_exceptbits_to_host(int target_bits)
2398 if (target_bits & 1)
2399 host_bits |= float_flag_invalid;
2400 if (target_bits & 2)
2401 host_bits |= float_flag_divbyzero;
2402 if (target_bits & 4)
2403 host_bits |= float_flag_overflow;
2404 if (target_bits & 8)
2405 host_bits |= float_flag_underflow;
2406 if (target_bits & 0x10)
2407 host_bits |= float_flag_inexact;
2408 if (target_bits & 0x80)
2409 host_bits |= float_flag_input_denormal;
2413 void HELPER(vfp_set_fpscr)(CPUARMState *env, uint32_t val)
2418 changed = env->vfp.xregs[ARM_VFP_FPSCR];
2419 env->vfp.xregs[ARM_VFP_FPSCR] = (val & 0xffc8ffff);
2420 env->vfp.vec_len = (val >> 16) & 7;
2421 env->vfp.vec_stride = (val >> 20) & 3;
2424 if (changed & (3 << 22)) {
2425 i = (val >> 22) & 3;
2428 i = float_round_nearest_even;
2434 i = float_round_down;
2437 i = float_round_to_zero;
2440 set_float_rounding_mode(i, &env->vfp.fp_status);
2442 if (changed & (1 << 24)) {
2443 set_flush_to_zero((val & (1 << 24)) != 0, &env->vfp.fp_status);
2444 set_flush_inputs_to_zero((val & (1 << 24)) != 0, &env->vfp.fp_status);
2446 if (changed & (1 << 25))
2447 set_default_nan_mode((val & (1 << 25)) != 0, &env->vfp.fp_status);
2449 i = vfp_exceptbits_to_host(val);
2450 set_float_exception_flags(i, &env->vfp.fp_status);
2451 set_float_exception_flags(0, &env->vfp.standard_fp_status);
2454 void vfp_set_fpscr(CPUARMState *env, uint32_t val)
2456 HELPER(vfp_set_fpscr)(env, val);
2459 #define VFP_HELPER(name, p) HELPER(glue(glue(vfp_,name),p))
2461 #define VFP_BINOP(name) \
2462 float32 VFP_HELPER(name, s)(float32 a, float32 b, void *fpstp) \
2464 float_status *fpst = fpstp; \
2465 return float32_ ## name(a, b, fpst); \
2467 float64 VFP_HELPER(name, d)(float64 a, float64 b, void *fpstp) \
2469 float_status *fpst = fpstp; \
2470 return float64_ ## name(a, b, fpst); \
2478 float32 VFP_HELPER(neg, s)(float32 a)
2480 return float32_chs(a);
2483 float64 VFP_HELPER(neg, d)(float64 a)
2485 return float64_chs(a);
2488 float32 VFP_HELPER(abs, s)(float32 a)
2490 return float32_abs(a);
2493 float64 VFP_HELPER(abs, d)(float64 a)
2495 return float64_abs(a);
2498 float32 VFP_HELPER(sqrt, s)(float32 a, CPUARMState *env)
2500 return float32_sqrt(a, &env->vfp.fp_status);
2503 float64 VFP_HELPER(sqrt, d)(float64 a, CPUARMState *env)
2505 return float64_sqrt(a, &env->vfp.fp_status);
2508 /* XXX: check quiet/signaling case */
2509 #define DO_VFP_cmp(p, type) \
2510 void VFP_HELPER(cmp, p)(type a, type b, CPUARMState *env) \
2513 switch(type ## _compare_quiet(a, b, &env->vfp.fp_status)) { \
2514 case 0: flags = 0x6; break; \
2515 case -1: flags = 0x8; break; \
2516 case 1: flags = 0x2; break; \
2517 default: case 2: flags = 0x3; break; \
2519 env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \
2520 | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \
2522 void VFP_HELPER(cmpe, p)(type a, type b, CPUARMState *env) \
2525 switch(type ## _compare(a, b, &env->vfp.fp_status)) { \
2526 case 0: flags = 0x6; break; \
2527 case -1: flags = 0x8; break; \
2528 case 1: flags = 0x2; break; \
2529 default: case 2: flags = 0x3; break; \
2531 env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \
2532 | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \
2534 DO_VFP_cmp(s, float32)
2535 DO_VFP_cmp(d, float64)
2538 /* Integer to float and float to integer conversions */
2540 #define CONV_ITOF(name, fsz, sign) \
2541 float##fsz HELPER(name)(uint32_t x, void *fpstp) \
2543 float_status *fpst = fpstp; \
2544 return sign##int32_to_##float##fsz((sign##int32_t)x, fpst); \
2547 #define CONV_FTOI(name, fsz, sign, round) \
2548 uint32_t HELPER(name)(float##fsz x, void *fpstp) \
2550 float_status *fpst = fpstp; \
2551 if (float##fsz##_is_any_nan(x)) { \
2552 float_raise(float_flag_invalid, fpst); \
2555 return float##fsz##_to_##sign##int32##round(x, fpst); \
2558 #define FLOAT_CONVS(name, p, fsz, sign) \
2559 CONV_ITOF(vfp_##name##to##p, fsz, sign) \
2560 CONV_FTOI(vfp_to##name##p, fsz, sign, ) \
2561 CONV_FTOI(vfp_to##name##z##p, fsz, sign, _round_to_zero)
2563 FLOAT_CONVS(si, s, 32, )
2564 FLOAT_CONVS(si, d, 64, )
2565 FLOAT_CONVS(ui, s, 32, u)
2566 FLOAT_CONVS(ui, d, 64, u)
2572 /* floating point conversion */
2573 float64 VFP_HELPER(fcvtd, s)(float32 x, CPUARMState *env)
2575 float64 r = float32_to_float64(x, &env->vfp.fp_status);
2576 /* ARM requires that S<->D conversion of any kind of NaN generates
2577 * a quiet NaN by forcing the most significant frac bit to 1.
2579 return float64_maybe_silence_nan(r);
2582 float32 VFP_HELPER(fcvts, d)(float64 x, CPUARMState *env)
2584 float32 r = float64_to_float32(x, &env->vfp.fp_status);
2585 /* ARM requires that S<->D conversion of any kind of NaN generates
2586 * a quiet NaN by forcing the most significant frac bit to 1.
2588 return float32_maybe_silence_nan(r);
2591 /* VFP3 fixed point conversion. */
2592 #define VFP_CONV_FIX(name, p, fsz, itype, sign) \
2593 float##fsz HELPER(vfp_##name##to##p)(uint##fsz##_t x, uint32_t shift, \
2596 float_status *fpst = fpstp; \
2598 tmp = sign##int32_to_##float##fsz((itype##_t)x, fpst); \
2599 return float##fsz##_scalbn(tmp, -(int)shift, fpst); \
2601 uint##fsz##_t HELPER(vfp_to##name##p)(float##fsz x, uint32_t shift, \
2604 float_status *fpst = fpstp; \
2606 if (float##fsz##_is_any_nan(x)) { \
2607 float_raise(float_flag_invalid, fpst); \
2610 tmp = float##fsz##_scalbn(x, shift, fpst); \
2611 return float##fsz##_to_##itype##_round_to_zero(tmp, fpst); \
2614 VFP_CONV_FIX(sh, d, 64, int16, )
2615 VFP_CONV_FIX(sl, d, 64, int32, )
2616 VFP_CONV_FIX(uh, d, 64, uint16, u)
2617 VFP_CONV_FIX(ul, d, 64, uint32, u)
2618 VFP_CONV_FIX(sh, s, 32, int16, )
2619 VFP_CONV_FIX(sl, s, 32, int32, )
2620 VFP_CONV_FIX(uh, s, 32, uint16, u)
2621 VFP_CONV_FIX(ul, s, 32, uint32, u)
2624 /* Half precision conversions. */
2625 static float32 do_fcvt_f16_to_f32(uint32_t a, CPUARMState *env, float_status *s)
2627 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
2628 float32 r = float16_to_float32(make_float16(a), ieee, s);
2630 return float32_maybe_silence_nan(r);
2635 static uint32_t do_fcvt_f32_to_f16(float32 a, CPUARMState *env, float_status *s)
2637 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
2638 float16 r = float32_to_float16(a, ieee, s);
2640 r = float16_maybe_silence_nan(r);
2642 return float16_val(r);
2645 float32 HELPER(neon_fcvt_f16_to_f32)(uint32_t a, CPUARMState *env)
2647 return do_fcvt_f16_to_f32(a, env, &env->vfp.standard_fp_status);
2650 uint32_t HELPER(neon_fcvt_f32_to_f16)(float32 a, CPUARMState *env)
2652 return do_fcvt_f32_to_f16(a, env, &env->vfp.standard_fp_status);
2655 float32 HELPER(vfp_fcvt_f16_to_f32)(uint32_t a, CPUARMState *env)
2657 return do_fcvt_f16_to_f32(a, env, &env->vfp.fp_status);
2660 uint32_t HELPER(vfp_fcvt_f32_to_f16)(float32 a, CPUARMState *env)
2662 return do_fcvt_f32_to_f16(a, env, &env->vfp.fp_status);
2665 #define float32_two make_float32(0x40000000)
2666 #define float32_three make_float32(0x40400000)
2667 #define float32_one_point_five make_float32(0x3fc00000)
2669 float32 HELPER(recps_f32)(float32 a, float32 b, CPUARMState *env)
2671 float_status *s = &env->vfp.standard_fp_status;
2672 if ((float32_is_infinity(a) && float32_is_zero_or_denormal(b)) ||
2673 (float32_is_infinity(b) && float32_is_zero_or_denormal(a))) {
2674 if (!(float32_is_zero(a) || float32_is_zero(b))) {
2675 float_raise(float_flag_input_denormal, s);
2679 return float32_sub(float32_two, float32_mul(a, b, s), s);
2682 float32 HELPER(rsqrts_f32)(float32 a, float32 b, CPUARMState *env)
2684 float_status *s = &env->vfp.standard_fp_status;
2686 if ((float32_is_infinity(a) && float32_is_zero_or_denormal(b)) ||
2687 (float32_is_infinity(b) && float32_is_zero_or_denormal(a))) {
2688 if (!(float32_is_zero(a) || float32_is_zero(b))) {
2689 float_raise(float_flag_input_denormal, s);
2691 return float32_one_point_five;
2693 product = float32_mul(a, b, s);
2694 return float32_div(float32_sub(float32_three, product, s), float32_two, s);
2699 /* Constants 256 and 512 are used in some helpers; we avoid relying on
2700 * int->float conversions at run-time. */
2701 #define float64_256 make_float64(0x4070000000000000LL)
2702 #define float64_512 make_float64(0x4080000000000000LL)
2704 /* The algorithm that must be used to calculate the estimate
2705 * is specified by the ARM ARM.
2707 static float64 recip_estimate(float64 a, CPUARMState *env)
2709 /* These calculations mustn't set any fp exception flags,
2710 * so we use a local copy of the fp_status.
2712 float_status dummy_status = env->vfp.standard_fp_status;
2713 float_status *s = &dummy_status;
2714 /* q = (int)(a * 512.0) */
2715 float64 q = float64_mul(float64_512, a, s);
2716 int64_t q_int = float64_to_int64_round_to_zero(q, s);
2718 /* r = 1.0 / (((double)q + 0.5) / 512.0) */
2719 q = int64_to_float64(q_int, s);
2720 q = float64_add(q, float64_half, s);
2721 q = float64_div(q, float64_512, s);
2722 q = float64_div(float64_one, q, s);
2724 /* s = (int)(256.0 * r + 0.5) */
2725 q = float64_mul(q, float64_256, s);
2726 q = float64_add(q, float64_half, s);
2727 q_int = float64_to_int64_round_to_zero(q, s);
2729 /* return (double)s / 256.0 */
2730 return float64_div(int64_to_float64(q_int, s), float64_256, s);
2733 float32 HELPER(recpe_f32)(float32 a, CPUARMState *env)
2735 float_status *s = &env->vfp.standard_fp_status;
2737 uint32_t val32 = float32_val(a);
2740 int a_exp = (val32 & 0x7f800000) >> 23;
2741 int sign = val32 & 0x80000000;
2743 if (float32_is_any_nan(a)) {
2744 if (float32_is_signaling_nan(a)) {
2745 float_raise(float_flag_invalid, s);
2747 return float32_default_nan;
2748 } else if (float32_is_infinity(a)) {
2749 return float32_set_sign(float32_zero, float32_is_neg(a));
2750 } else if (float32_is_zero_or_denormal(a)) {
2751 if (!float32_is_zero(a)) {
2752 float_raise(float_flag_input_denormal, s);
2754 float_raise(float_flag_divbyzero, s);
2755 return float32_set_sign(float32_infinity, float32_is_neg(a));
2756 } else if (a_exp >= 253) {
2757 float_raise(float_flag_underflow, s);
2758 return float32_set_sign(float32_zero, float32_is_neg(a));
2761 f64 = make_float64((0x3feULL << 52)
2762 | ((int64_t)(val32 & 0x7fffff) << 29));
2764 result_exp = 253 - a_exp;
2766 f64 = recip_estimate(f64, env);
2769 | ((result_exp & 0xff) << 23)
2770 | ((float64_val(f64) >> 29) & 0x7fffff);
2771 return make_float32(val32);
2774 /* The algorithm that must be used to calculate the estimate
2775 * is specified by the ARM ARM.
2777 static float64 recip_sqrt_estimate(float64 a, CPUARMState *env)
2779 /* These calculations mustn't set any fp exception flags,
2780 * so we use a local copy of the fp_status.
2782 float_status dummy_status = env->vfp.standard_fp_status;
2783 float_status *s = &dummy_status;
2787 if (float64_lt(a, float64_half, s)) {
2788 /* range 0.25 <= a < 0.5 */
2790 /* a in units of 1/512 rounded down */
2791 /* q0 = (int)(a * 512.0); */
2792 q = float64_mul(float64_512, a, s);
2793 q_int = float64_to_int64_round_to_zero(q, s);
2795 /* reciprocal root r */
2796 /* r = 1.0 / sqrt(((double)q0 + 0.5) / 512.0); */
2797 q = int64_to_float64(q_int, s);
2798 q = float64_add(q, float64_half, s);
2799 q = float64_div(q, float64_512, s);
2800 q = float64_sqrt(q, s);
2801 q = float64_div(float64_one, q, s);
2803 /* range 0.5 <= a < 1.0 */
2805 /* a in units of 1/256 rounded down */
2806 /* q1 = (int)(a * 256.0); */
2807 q = float64_mul(float64_256, a, s);
2808 int64_t q_int = float64_to_int64_round_to_zero(q, s);
2810 /* reciprocal root r */
2811 /* r = 1.0 /sqrt(((double)q1 + 0.5) / 256); */
2812 q = int64_to_float64(q_int, s);
2813 q = float64_add(q, float64_half, s);
2814 q = float64_div(q, float64_256, s);
2815 q = float64_sqrt(q, s);
2816 q = float64_div(float64_one, q, s);
2818 /* r in units of 1/256 rounded to nearest */
2819 /* s = (int)(256.0 * r + 0.5); */
2821 q = float64_mul(q, float64_256,s );
2822 q = float64_add(q, float64_half, s);
2823 q_int = float64_to_int64_round_to_zero(q, s);
2825 /* return (double)s / 256.0;*/
2826 return float64_div(int64_to_float64(q_int, s), float64_256, s);
2829 float32 HELPER(rsqrte_f32)(float32 a, CPUARMState *env)
2831 float_status *s = &env->vfp.standard_fp_status;
2837 val = float32_val(a);
2839 if (float32_is_any_nan(a)) {
2840 if (float32_is_signaling_nan(a)) {
2841 float_raise(float_flag_invalid, s);
2843 return float32_default_nan;
2844 } else if (float32_is_zero_or_denormal(a)) {
2845 if (!float32_is_zero(a)) {
2846 float_raise(float_flag_input_denormal, s);
2848 float_raise(float_flag_divbyzero, s);
2849 return float32_set_sign(float32_infinity, float32_is_neg(a));
2850 } else if (float32_is_neg(a)) {
2851 float_raise(float_flag_invalid, s);
2852 return float32_default_nan;
2853 } else if (float32_is_infinity(a)) {
2854 return float32_zero;
2857 /* Normalize to a double-precision value between 0.25 and 1.0,
2858 * preserving the parity of the exponent. */
2859 if ((val & 0x800000) == 0) {
2860 f64 = make_float64(((uint64_t)(val & 0x80000000) << 32)
2862 | ((uint64_t)(val & 0x7fffff) << 29));
2864 f64 = make_float64(((uint64_t)(val & 0x80000000) << 32)
2866 | ((uint64_t)(val & 0x7fffff) << 29));
2869 result_exp = (380 - ((val & 0x7f800000) >> 23)) / 2;
2871 f64 = recip_sqrt_estimate(f64, env);
2873 val64 = float64_val(f64);
2875 val = ((result_exp & 0xff) << 23)
2876 | ((val64 >> 29) & 0x7fffff);
2877 return make_float32(val);
2880 uint32_t HELPER(recpe_u32)(uint32_t a, CPUARMState *env)
2884 if ((a & 0x80000000) == 0) {
2888 f64 = make_float64((0x3feULL << 52)
2889 | ((int64_t)(a & 0x7fffffff) << 21));
2891 f64 = recip_estimate (f64, env);
2893 return 0x80000000 | ((float64_val(f64) >> 21) & 0x7fffffff);
2896 uint32_t HELPER(rsqrte_u32)(uint32_t a, CPUARMState *env)
2900 if ((a & 0xc0000000) == 0) {
2904 if (a & 0x80000000) {
2905 f64 = make_float64((0x3feULL << 52)
2906 | ((uint64_t)(a & 0x7fffffff) << 21));
2907 } else { /* bits 31-30 == '01' */
2908 f64 = make_float64((0x3fdULL << 52)
2909 | ((uint64_t)(a & 0x3fffffff) << 22));
2912 f64 = recip_sqrt_estimate(f64, env);
2914 return 0x80000000 | ((float64_val(f64) >> 21) & 0x7fffffff);
2917 /* VFPv4 fused multiply-accumulate */
2918 float32 VFP_HELPER(muladd, s)(float32 a, float32 b, float32 c, void *fpstp)
2920 float_status *fpst = fpstp;
2921 return float32_muladd(a, b, c, 0, fpst);
2924 float64 VFP_HELPER(muladd, d)(float64 a, float64 b, float64 c, void *fpstp)
2926 float_status *fpst = fpstp;
2927 return float64_muladd(a, b, c, 0, fpst);
2930 void HELPER(set_teecr)(CPUARMState *env, uint32_t val)
2933 if (env->teecr != val) {