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 static const ARMCPRegInfo cp_reginfo[] = {
60 /* DBGDIDR: just RAZ. In particular this means the "debug architecture
61 * version" bits will read as a reserved value, which should cause
62 * Linux to not try to use the debug hardware.
64 { .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0,
65 .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
69 static const ARMCPRegInfo not_v6_cp_reginfo[] = {
70 /* Not all pre-v6 cores implemented this WFI, so this is slightly
73 { .name = "WFI_v5", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = 2,
74 .access = PL1_W, .type = ARM_CP_WFI },
78 static const ARMCPRegInfo not_v7_cp_reginfo[] = {
79 /* Standard v6 WFI (also used in some pre-v6 cores); not in v7 (which
80 * is UNPREDICTABLE; we choose to NOP as most implementations do).
82 { .name = "WFI_v6", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
83 .access = PL1_W, .type = ARM_CP_WFI },
87 static const ARMCPRegInfo v6_cp_reginfo[] = {
88 /* prefetch by MVA in v6, NOP in v7 */
89 { .name = "MVA_prefetch",
90 .cp = 15, .crn = 7, .crm = 13, .opc1 = 0, .opc2 = 1,
91 .access = PL1_W, .type = ARM_CP_NOP },
92 { .name = "ISB", .cp = 15, .crn = 7, .crm = 5, .opc1 = 0, .opc2 = 4,
93 .access = PL0_W, .type = ARM_CP_NOP },
94 { .name = "ISB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 4,
95 .access = PL0_W, .type = ARM_CP_NOP },
96 { .name = "ISB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 5,
97 .access = PL0_W, .type = ARM_CP_NOP },
101 static const ARMCPRegInfo v7_cp_reginfo[] = {
102 /* DBGDRAR, DBGDSAR: always RAZ since we don't implement memory mapped
105 { .name = "DBGDRAR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 0,
106 .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
107 { .name = "DBGDRAR", .cp = 14, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
108 .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
109 /* the old v6 WFI, UNPREDICTABLE in v7 but we choose to NOP */
110 { .name = "NOP", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
111 .access = PL1_W, .type = ARM_CP_NOP },
115 static int teecr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
122 static int teehbr_read(CPUARMState *env, const ARMCPRegInfo *ri,
125 /* This is a helper function because the user access rights
126 * depend on the value of the TEECR.
128 if (arm_current_pl(env) == 0 && (env->teecr & 1)) {
131 *value = env->teehbr;
135 static int teehbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
138 if (arm_current_pl(env) == 0 && (env->teecr & 1)) {
145 static const ARMCPRegInfo t2ee_cp_reginfo[] = {
146 { .name = "TEECR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 6, .opc2 = 0,
147 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, teecr),
149 .writefn = teecr_write },
150 { .name = "TEEHBR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 6, .opc2 = 0,
151 .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, teehbr),
153 .readfn = teehbr_read, .writefn = teehbr_write },
157 void register_cp_regs_for_features(ARMCPU *cpu)
159 /* Register all the coprocessor registers based on feature bits */
160 CPUARMState *env = &cpu->env;
161 if (arm_feature(env, ARM_FEATURE_M)) {
162 /* M profile has no coprocessor registers */
166 define_arm_cp_regs(cpu, cp_reginfo);
167 if (arm_feature(env, ARM_FEATURE_V6)) {
168 define_arm_cp_regs(cpu, v6_cp_reginfo);
170 define_arm_cp_regs(cpu, not_v6_cp_reginfo);
172 if (arm_feature(env, ARM_FEATURE_V7)) {
173 define_arm_cp_regs(cpu, v7_cp_reginfo);
175 define_arm_cp_regs(cpu, not_v7_cp_reginfo);
177 if (arm_feature(env, ARM_FEATURE_THUMB2EE)) {
178 define_arm_cp_regs(cpu, t2ee_cp_reginfo);
182 ARMCPU *cpu_arm_init(const char *cpu_model)
186 static int inited = 0;
188 if (!object_class_by_name(cpu_model)) {
191 cpu = ARM_CPU(object_new(cpu_model));
193 env->cpu_model_str = cpu_model;
194 arm_cpu_realize(cpu);
196 if (tcg_enabled() && !inited) {
198 arm_translate_init();
202 if (arm_feature(env, ARM_FEATURE_NEON)) {
203 gdb_register_coprocessor(env, vfp_gdb_get_reg, vfp_gdb_set_reg,
204 51, "arm-neon.xml", 0);
205 } else if (arm_feature(env, ARM_FEATURE_VFP3)) {
206 gdb_register_coprocessor(env, vfp_gdb_get_reg, vfp_gdb_set_reg,
207 35, "arm-vfp3.xml", 0);
208 } else if (arm_feature(env, ARM_FEATURE_VFP)) {
209 gdb_register_coprocessor(env, vfp_gdb_get_reg, vfp_gdb_set_reg,
210 19, "arm-vfp.xml", 0);
216 typedef struct ARMCPUListState {
217 fprintf_function cpu_fprintf;
221 /* Sort alphabetically by type name, except for "any". */
222 static gint arm_cpu_list_compare(gconstpointer a, gconstpointer b)
224 ObjectClass *class_a = (ObjectClass *)a;
225 ObjectClass *class_b = (ObjectClass *)b;
226 const char *name_a, *name_b;
228 name_a = object_class_get_name(class_a);
229 name_b = object_class_get_name(class_b);
230 if (strcmp(name_a, "any") == 0) {
232 } else if (strcmp(name_b, "any") == 0) {
235 return strcmp(name_a, name_b);
239 static void arm_cpu_list_entry(gpointer data, gpointer user_data)
241 ObjectClass *oc = data;
242 ARMCPUListState *s = user_data;
244 (*s->cpu_fprintf)(s->file, " %s\n",
245 object_class_get_name(oc));
248 void arm_cpu_list(FILE *f, fprintf_function cpu_fprintf)
250 ARMCPUListState s = {
252 .cpu_fprintf = cpu_fprintf,
256 list = object_class_get_list(TYPE_ARM_CPU, false);
257 list = g_slist_sort(list, arm_cpu_list_compare);
258 (*cpu_fprintf)(f, "Available CPUs:\n");
259 g_slist_foreach(list, arm_cpu_list_entry, &s);
263 void define_one_arm_cp_reg_with_opaque(ARMCPU *cpu,
264 const ARMCPRegInfo *r, void *opaque)
266 /* Define implementations of coprocessor registers.
267 * We store these in a hashtable because typically
268 * there are less than 150 registers in a space which
269 * is 16*16*16*8*8 = 262144 in size.
270 * Wildcarding is supported for the crm, opc1 and opc2 fields.
271 * If a register is defined twice then the second definition is
272 * used, so this can be used to define some generic registers and
273 * then override them with implementation specific variations.
274 * At least one of the original and the second definition should
275 * include ARM_CP_OVERRIDE in its type bits -- this is just a guard
276 * against accidental use.
279 int crmmin = (r->crm == CP_ANY) ? 0 : r->crm;
280 int crmmax = (r->crm == CP_ANY) ? 15 : r->crm;
281 int opc1min = (r->opc1 == CP_ANY) ? 0 : r->opc1;
282 int opc1max = (r->opc1 == CP_ANY) ? 7 : r->opc1;
283 int opc2min = (r->opc2 == CP_ANY) ? 0 : r->opc2;
284 int opc2max = (r->opc2 == CP_ANY) ? 7 : r->opc2;
285 /* 64 bit registers have only CRm and Opc1 fields */
286 assert(!((r->type & ARM_CP_64BIT) && (r->opc2 || r->crn)));
287 /* Check that the register definition has enough info to handle
288 * reads and writes if they are permitted.
290 if (!(r->type & (ARM_CP_SPECIAL|ARM_CP_CONST))) {
291 if (r->access & PL3_R) {
292 assert(r->fieldoffset || r->readfn);
294 if (r->access & PL3_W) {
295 assert(r->fieldoffset || r->writefn);
298 /* Bad type field probably means missing sentinel at end of reg list */
299 assert(cptype_valid(r->type));
300 for (crm = crmmin; crm <= crmmax; crm++) {
301 for (opc1 = opc1min; opc1 <= opc1max; opc1++) {
302 for (opc2 = opc2min; opc2 <= opc2max; opc2++) {
303 uint32_t *key = g_new(uint32_t, 1);
304 ARMCPRegInfo *r2 = g_memdup(r, sizeof(ARMCPRegInfo));
305 int is64 = (r->type & ARM_CP_64BIT) ? 1 : 0;
306 *key = ENCODE_CP_REG(r->cp, is64, r->crn, crm, opc1, opc2);
308 /* Make sure reginfo passed to helpers for wildcarded regs
309 * has the correct crm/opc1/opc2 for this reg, not CP_ANY:
314 /* Overriding of an existing definition must be explicitly
317 if (!(r->type & ARM_CP_OVERRIDE)) {
318 ARMCPRegInfo *oldreg;
319 oldreg = g_hash_table_lookup(cpu->cp_regs, key);
320 if (oldreg && !(oldreg->type & ARM_CP_OVERRIDE)) {
321 fprintf(stderr, "Register redefined: cp=%d %d bit "
322 "crn=%d crm=%d opc1=%d opc2=%d, "
323 "was %s, now %s\n", r2->cp, 32 + 32 * is64,
324 r2->crn, r2->crm, r2->opc1, r2->opc2,
325 oldreg->name, r2->name);
329 g_hash_table_insert(cpu->cp_regs, key, r2);
335 void define_arm_cp_regs_with_opaque(ARMCPU *cpu,
336 const ARMCPRegInfo *regs, void *opaque)
338 /* Define a whole list of registers */
339 const ARMCPRegInfo *r;
340 for (r = regs; r->type != ARM_CP_SENTINEL; r++) {
341 define_one_arm_cp_reg_with_opaque(cpu, r, opaque);
345 const ARMCPRegInfo *get_arm_cp_reginfo(ARMCPU *cpu, uint32_t encoded_cp)
347 return g_hash_table_lookup(cpu->cp_regs, &encoded_cp);
350 int arm_cp_write_ignore(CPUARMState *env, const ARMCPRegInfo *ri,
353 /* Helper coprocessor write function for write-ignore registers */
357 int arm_cp_read_zero(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t *value)
359 /* Helper coprocessor write function for read-as-zero registers */
364 static int bad_mode_switch(CPUARMState *env, int mode)
366 /* Return true if it is not valid for us to switch to
367 * this CPU mode (ie all the UNPREDICTABLE cases in
368 * the ARM ARM CPSRWriteByInstr pseudocode).
371 case ARM_CPU_MODE_USR:
372 case ARM_CPU_MODE_SYS:
373 case ARM_CPU_MODE_SVC:
374 case ARM_CPU_MODE_ABT:
375 case ARM_CPU_MODE_UND:
376 case ARM_CPU_MODE_IRQ:
377 case ARM_CPU_MODE_FIQ:
384 uint32_t cpsr_read(CPUARMState *env)
388 return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) |
389 (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27)
390 | (env->thumb << 5) | ((env->condexec_bits & 3) << 25)
391 | ((env->condexec_bits & 0xfc) << 8)
395 void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask)
397 if (mask & CPSR_NZCV) {
398 env->ZF = (~val) & CPSR_Z;
400 env->CF = (val >> 29) & 1;
401 env->VF = (val << 3) & 0x80000000;
404 env->QF = ((val & CPSR_Q) != 0);
406 env->thumb = ((val & CPSR_T) != 0);
407 if (mask & CPSR_IT_0_1) {
408 env->condexec_bits &= ~3;
409 env->condexec_bits |= (val >> 25) & 3;
411 if (mask & CPSR_IT_2_7) {
412 env->condexec_bits &= 3;
413 env->condexec_bits |= (val >> 8) & 0xfc;
415 if (mask & CPSR_GE) {
416 env->GE = (val >> 16) & 0xf;
419 if ((env->uncached_cpsr ^ val) & mask & CPSR_M) {
420 if (bad_mode_switch(env, val & CPSR_M)) {
421 /* Attempt to switch to an invalid mode: this is UNPREDICTABLE.
422 * We choose to ignore the attempt and leave the CPSR M field
427 switch_mode(env, val & CPSR_M);
430 mask &= ~CACHED_CPSR_BITS;
431 env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask);
434 /* Sign/zero extend */
435 uint32_t HELPER(sxtb16)(uint32_t x)
438 res = (uint16_t)(int8_t)x;
439 res |= (uint32_t)(int8_t)(x >> 16) << 16;
443 uint32_t HELPER(uxtb16)(uint32_t x)
446 res = (uint16_t)(uint8_t)x;
447 res |= (uint32_t)(uint8_t)(x >> 16) << 16;
451 uint32_t HELPER(clz)(uint32_t x)
456 int32_t HELPER(sdiv)(int32_t num, int32_t den)
460 if (num == INT_MIN && den == -1)
465 uint32_t HELPER(udiv)(uint32_t num, uint32_t den)
472 uint32_t HELPER(rbit)(uint32_t x)
474 x = ((x & 0xff000000) >> 24)
475 | ((x & 0x00ff0000) >> 8)
476 | ((x & 0x0000ff00) << 8)
477 | ((x & 0x000000ff) << 24);
478 x = ((x & 0xf0f0f0f0) >> 4)
479 | ((x & 0x0f0f0f0f) << 4);
480 x = ((x & 0x88888888) >> 3)
481 | ((x & 0x44444444) >> 1)
482 | ((x & 0x22222222) << 1)
483 | ((x & 0x11111111) << 3);
487 uint32_t HELPER(abs)(uint32_t x)
489 return ((int32_t)x < 0) ? -x : x;
492 #if defined(CONFIG_USER_ONLY)
494 void do_interrupt (CPUARMState *env)
496 env->exception_index = -1;
499 int cpu_arm_handle_mmu_fault (CPUARMState *env, target_ulong address, int rw,
503 env->exception_index = EXCP_PREFETCH_ABORT;
504 env->cp15.c6_insn = address;
506 env->exception_index = EXCP_DATA_ABORT;
507 env->cp15.c6_data = address;
512 void HELPER(set_cp15)(CPUARMState *env, uint32_t insn, uint32_t val)
514 cpu_abort(env, "cp15 insn %08x\n", insn);
517 uint32_t HELPER(get_cp15)(CPUARMState *env, uint32_t insn)
519 cpu_abort(env, "cp15 insn %08x\n", insn);
522 /* These should probably raise undefined insn exceptions. */
523 void HELPER(v7m_msr)(CPUARMState *env, uint32_t reg, uint32_t val)
525 cpu_abort(env, "v7m_mrs %d\n", reg);
528 uint32_t HELPER(v7m_mrs)(CPUARMState *env, uint32_t reg)
530 cpu_abort(env, "v7m_mrs %d\n", reg);
534 void switch_mode(CPUARMState *env, int mode)
536 if (mode != ARM_CPU_MODE_USR)
537 cpu_abort(env, "Tried to switch out of user mode\n");
540 void HELPER(set_r13_banked)(CPUARMState *env, uint32_t mode, uint32_t val)
542 cpu_abort(env, "banked r13 write\n");
545 uint32_t HELPER(get_r13_banked)(CPUARMState *env, uint32_t mode)
547 cpu_abort(env, "banked r13 read\n");
553 /* Map CPU modes onto saved register banks. */
554 static inline int bank_number(CPUARMState *env, int mode)
557 case ARM_CPU_MODE_USR:
558 case ARM_CPU_MODE_SYS:
560 case ARM_CPU_MODE_SVC:
562 case ARM_CPU_MODE_ABT:
564 case ARM_CPU_MODE_UND:
566 case ARM_CPU_MODE_IRQ:
568 case ARM_CPU_MODE_FIQ:
571 cpu_abort(env, "Bad mode %x\n", mode);
575 void switch_mode(CPUARMState *env, int mode)
580 old_mode = env->uncached_cpsr & CPSR_M;
581 if (mode == old_mode)
584 if (old_mode == ARM_CPU_MODE_FIQ) {
585 memcpy (env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t));
586 memcpy (env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t));
587 } else if (mode == ARM_CPU_MODE_FIQ) {
588 memcpy (env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t));
589 memcpy (env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t));
592 i = bank_number(env, old_mode);
593 env->banked_r13[i] = env->regs[13];
594 env->banked_r14[i] = env->regs[14];
595 env->banked_spsr[i] = env->spsr;
597 i = bank_number(env, mode);
598 env->regs[13] = env->banked_r13[i];
599 env->regs[14] = env->banked_r14[i];
600 env->spsr = env->banked_spsr[i];
603 static void v7m_push(CPUARMState *env, uint32_t val)
606 stl_phys(env->regs[13], val);
609 static uint32_t v7m_pop(CPUARMState *env)
612 val = ldl_phys(env->regs[13]);
617 /* Switch to V7M main or process stack pointer. */
618 static void switch_v7m_sp(CPUARMState *env, int process)
621 if (env->v7m.current_sp != process) {
622 tmp = env->v7m.other_sp;
623 env->v7m.other_sp = env->regs[13];
625 env->v7m.current_sp = process;
629 static void do_v7m_exception_exit(CPUARMState *env)
634 type = env->regs[15];
635 if (env->v7m.exception != 0)
636 armv7m_nvic_complete_irq(env->nvic, env->v7m.exception);
638 /* Switch to the target stack. */
639 switch_v7m_sp(env, (type & 4) != 0);
641 env->regs[0] = v7m_pop(env);
642 env->regs[1] = v7m_pop(env);
643 env->regs[2] = v7m_pop(env);
644 env->regs[3] = v7m_pop(env);
645 env->regs[12] = v7m_pop(env);
646 env->regs[14] = v7m_pop(env);
647 env->regs[15] = v7m_pop(env);
649 xpsr_write(env, xpsr, 0xfffffdff);
650 /* Undo stack alignment. */
653 /* ??? The exception return type specifies Thread/Handler mode. However
654 this is also implied by the xPSR value. Not sure what to do
655 if there is a mismatch. */
656 /* ??? Likewise for mismatches between the CONTROL register and the stack
660 static void do_interrupt_v7m(CPUARMState *env)
662 uint32_t xpsr = xpsr_read(env);
667 if (env->v7m.current_sp)
669 if (env->v7m.exception == 0)
672 /* For exceptions we just mark as pending on the NVIC, and let that
674 /* TODO: Need to escalate if the current priority is higher than the
675 one we're raising. */
676 switch (env->exception_index) {
678 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE);
682 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SVC);
684 case EXCP_PREFETCH_ABORT:
685 case EXCP_DATA_ABORT:
686 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_MEM);
689 if (semihosting_enabled) {
691 nr = arm_lduw_code(env->regs[15], env->bswap_code) & 0xff;
694 env->regs[0] = do_arm_semihosting(env);
698 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_DEBUG);
701 env->v7m.exception = armv7m_nvic_acknowledge_irq(env->nvic);
703 case EXCP_EXCEPTION_EXIT:
704 do_v7m_exception_exit(env);
707 cpu_abort(env, "Unhandled exception 0x%x\n", env->exception_index);
708 return; /* Never happens. Keep compiler happy. */
711 /* Align stack pointer. */
712 /* ??? Should only do this if Configuration Control Register
713 STACKALIGN bit is set. */
714 if (env->regs[13] & 4) {
718 /* Switch to the handler mode. */
720 v7m_push(env, env->regs[15]);
721 v7m_push(env, env->regs[14]);
722 v7m_push(env, env->regs[12]);
723 v7m_push(env, env->regs[3]);
724 v7m_push(env, env->regs[2]);
725 v7m_push(env, env->regs[1]);
726 v7m_push(env, env->regs[0]);
727 switch_v7m_sp(env, 0);
729 env->condexec_bits = 0;
731 addr = ldl_phys(env->v7m.vecbase + env->v7m.exception * 4);
732 env->regs[15] = addr & 0xfffffffe;
733 env->thumb = addr & 1;
736 /* Handle a CPU exception. */
737 void do_interrupt(CPUARMState *env)
745 do_interrupt_v7m(env);
748 /* TODO: Vectored interrupt controller. */
749 switch (env->exception_index) {
751 new_mode = ARM_CPU_MODE_UND;
760 if (semihosting_enabled) {
761 /* Check for semihosting interrupt. */
763 mask = arm_lduw_code(env->regs[15] - 2, env->bswap_code) & 0xff;
765 mask = arm_ldl_code(env->regs[15] - 4, env->bswap_code)
768 /* Only intercept calls from privileged modes, to provide some
769 semblance of security. */
770 if (((mask == 0x123456 && !env->thumb)
771 || (mask == 0xab && env->thumb))
772 && (env->uncached_cpsr & CPSR_M) != ARM_CPU_MODE_USR) {
773 env->regs[0] = do_arm_semihosting(env);
777 new_mode = ARM_CPU_MODE_SVC;
780 /* The PC already points to the next instruction. */
784 /* See if this is a semihosting syscall. */
785 if (env->thumb && semihosting_enabled) {
786 mask = arm_lduw_code(env->regs[15], env->bswap_code) & 0xff;
788 && (env->uncached_cpsr & CPSR_M) != ARM_CPU_MODE_USR) {
790 env->regs[0] = do_arm_semihosting(env);
794 env->cp15.c5_insn = 2;
795 /* Fall through to prefetch abort. */
796 case EXCP_PREFETCH_ABORT:
797 new_mode = ARM_CPU_MODE_ABT;
799 mask = CPSR_A | CPSR_I;
802 case EXCP_DATA_ABORT:
803 new_mode = ARM_CPU_MODE_ABT;
805 mask = CPSR_A | CPSR_I;
809 new_mode = ARM_CPU_MODE_IRQ;
811 /* Disable IRQ and imprecise data aborts. */
812 mask = CPSR_A | CPSR_I;
816 new_mode = ARM_CPU_MODE_FIQ;
818 /* Disable FIQ, IRQ and imprecise data aborts. */
819 mask = CPSR_A | CPSR_I | CPSR_F;
823 cpu_abort(env, "Unhandled exception 0x%x\n", env->exception_index);
824 return; /* Never happens. Keep compiler happy. */
827 if (env->cp15.c1_sys & (1 << 13)) {
830 switch_mode (env, new_mode);
831 env->spsr = cpsr_read(env);
833 env->condexec_bits = 0;
834 /* Switch to the new mode, and to the correct instruction set. */
835 env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode;
836 env->uncached_cpsr |= mask;
837 /* this is a lie, as the was no c1_sys on V4T/V5, but who cares
838 * and we should just guard the thumb mode on V4 */
839 if (arm_feature(env, ARM_FEATURE_V4T)) {
840 env->thumb = (env->cp15.c1_sys & (1 << 30)) != 0;
842 env->regs[14] = env->regs[15] + offset;
843 env->regs[15] = addr;
844 env->interrupt_request |= CPU_INTERRUPT_EXITTB;
847 /* Check section/page access permissions.
848 Returns the page protection flags, or zero if the access is not
850 static inline int check_ap(CPUARMState *env, int ap, int domain_prot,
851 int access_type, int is_user)
855 if (domain_prot == 3) {
856 return PAGE_READ | PAGE_WRITE;
859 if (access_type == 1)
866 if (access_type == 1)
868 switch ((env->cp15.c1_sys >> 8) & 3) {
870 return is_user ? 0 : PAGE_READ;
877 return is_user ? 0 : PAGE_READ | PAGE_WRITE;
882 return PAGE_READ | PAGE_WRITE;
884 return PAGE_READ | PAGE_WRITE;
885 case 4: /* Reserved. */
888 return is_user ? 0 : prot_ro;
892 if (!arm_feature (env, ARM_FEATURE_V6K))
900 static uint32_t get_level1_table_address(CPUARMState *env, uint32_t address)
904 if (address & env->cp15.c2_mask)
905 table = env->cp15.c2_base1 & 0xffffc000;
907 table = env->cp15.c2_base0 & env->cp15.c2_base_mask;
909 table |= (address >> 18) & 0x3ffc;
913 static int get_phys_addr_v5(CPUARMState *env, uint32_t address, int access_type,
914 int is_user, uint32_t *phys_ptr, int *prot,
915 target_ulong *page_size)
926 /* Pagetable walk. */
927 /* Lookup l1 descriptor. */
928 table = get_level1_table_address(env, address);
929 desc = ldl_phys(table);
931 domain = (desc >> 5) & 0x0f;
932 domain_prot = (env->cp15.c3 >> (domain * 2)) & 3;
934 /* Section translation fault. */
938 if (domain_prot == 0 || domain_prot == 2) {
940 code = 9; /* Section domain fault. */
942 code = 11; /* Page domain fault. */
947 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
948 ap = (desc >> 10) & 3;
950 *page_size = 1024 * 1024;
952 /* Lookup l2 entry. */
954 /* Coarse pagetable. */
955 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
957 /* Fine pagetable. */
958 table = (desc & 0xfffff000) | ((address >> 8) & 0xffc);
960 desc = ldl_phys(table);
962 case 0: /* Page translation fault. */
965 case 1: /* 64k page. */
966 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
967 ap = (desc >> (4 + ((address >> 13) & 6))) & 3;
968 *page_size = 0x10000;
970 case 2: /* 4k page. */
971 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
972 ap = (desc >> (4 + ((address >> 13) & 6))) & 3;
975 case 3: /* 1k page. */
977 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
978 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
980 /* Page translation fault. */
985 phys_addr = (desc & 0xfffffc00) | (address & 0x3ff);
987 ap = (desc >> 4) & 3;
991 /* Never happens, but compiler isn't smart enough to tell. */
996 *prot = check_ap(env, ap, domain_prot, access_type, is_user);
998 /* Access permission fault. */
1002 *phys_ptr = phys_addr;
1005 return code | (domain << 4);
1008 static int get_phys_addr_v6(CPUARMState *env, uint32_t address, int access_type,
1009 int is_user, uint32_t *phys_ptr, int *prot,
1010 target_ulong *page_size)
1022 /* Pagetable walk. */
1023 /* Lookup l1 descriptor. */
1024 table = get_level1_table_address(env, address);
1025 desc = ldl_phys(table);
1028 /* Section translation fault. */
1032 } else if (type == 2 && (desc & (1 << 18))) {
1036 /* Section or page. */
1037 domain = (desc >> 5) & 0x0f;
1039 domain_prot = (env->cp15.c3 >> (domain * 2)) & 3;
1040 if (domain_prot == 0 || domain_prot == 2) {
1042 code = 9; /* Section domain fault. */
1044 code = 11; /* Page domain fault. */
1048 if (desc & (1 << 18)) {
1050 phys_addr = (desc & 0xff000000) | (address & 0x00ffffff);
1051 *page_size = 0x1000000;
1054 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
1055 *page_size = 0x100000;
1057 ap = ((desc >> 10) & 3) | ((desc >> 13) & 4);
1058 xn = desc & (1 << 4);
1061 /* Lookup l2 entry. */
1062 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
1063 desc = ldl_phys(table);
1064 ap = ((desc >> 4) & 3) | ((desc >> 7) & 4);
1066 case 0: /* Page translation fault. */
1069 case 1: /* 64k page. */
1070 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
1071 xn = desc & (1 << 15);
1072 *page_size = 0x10000;
1074 case 2: case 3: /* 4k page. */
1075 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
1077 *page_size = 0x1000;
1080 /* Never happens, but compiler isn't smart enough to tell. */
1085 if (domain_prot == 3) {
1086 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
1088 if (xn && access_type == 2)
1091 /* The simplified model uses AP[0] as an access control bit. */
1092 if ((env->cp15.c1_sys & (1 << 29)) && (ap & 1) == 0) {
1093 /* Access flag fault. */
1094 code = (code == 15) ? 6 : 3;
1097 *prot = check_ap(env, ap, domain_prot, access_type, is_user);
1099 /* Access permission fault. */
1106 *phys_ptr = phys_addr;
1109 return code | (domain << 4);
1112 static int get_phys_addr_mpu(CPUARMState *env, uint32_t address, int access_type,
1113 int is_user, uint32_t *phys_ptr, int *prot)
1119 *phys_ptr = address;
1120 for (n = 7; n >= 0; n--) {
1121 base = env->cp15.c6_region[n];
1122 if ((base & 1) == 0)
1124 mask = 1 << ((base >> 1) & 0x1f);
1125 /* Keep this shift separate from the above to avoid an
1126 (undefined) << 32. */
1127 mask = (mask << 1) - 1;
1128 if (((base ^ address) & ~mask) == 0)
1134 if (access_type == 2) {
1135 mask = env->cp15.c5_insn;
1137 mask = env->cp15.c5_data;
1139 mask = (mask >> (n * 4)) & 0xf;
1146 *prot = PAGE_READ | PAGE_WRITE;
1151 *prot |= PAGE_WRITE;
1154 *prot = PAGE_READ | PAGE_WRITE;
1165 /* Bad permission. */
1172 static inline int get_phys_addr(CPUARMState *env, uint32_t address,
1173 int access_type, int is_user,
1174 uint32_t *phys_ptr, int *prot,
1175 target_ulong *page_size)
1177 /* Fast Context Switch Extension. */
1178 if (address < 0x02000000)
1179 address += env->cp15.c13_fcse;
1181 if ((env->cp15.c1_sys & 1) == 0) {
1182 /* MMU/MPU disabled. */
1183 *phys_ptr = address;
1184 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
1185 *page_size = TARGET_PAGE_SIZE;
1187 } else if (arm_feature(env, ARM_FEATURE_MPU)) {
1188 *page_size = TARGET_PAGE_SIZE;
1189 return get_phys_addr_mpu(env, address, access_type, is_user, phys_ptr,
1191 } else if (env->cp15.c1_sys & (1 << 23)) {
1192 return get_phys_addr_v6(env, address, access_type, is_user, phys_ptr,
1195 return get_phys_addr_v5(env, address, access_type, is_user, phys_ptr,
1200 int cpu_arm_handle_mmu_fault (CPUARMState *env, target_ulong address,
1201 int access_type, int mmu_idx)
1204 target_ulong page_size;
1208 is_user = mmu_idx == MMU_USER_IDX;
1209 ret = get_phys_addr(env, address, access_type, is_user, &phys_addr, &prot,
1212 /* Map a single [sub]page. */
1213 phys_addr &= ~(uint32_t)0x3ff;
1214 address &= ~(uint32_t)0x3ff;
1215 tlb_set_page (env, address, phys_addr, prot, mmu_idx, page_size);
1219 if (access_type == 2) {
1220 env->cp15.c5_insn = ret;
1221 env->cp15.c6_insn = address;
1222 env->exception_index = EXCP_PREFETCH_ABORT;
1224 env->cp15.c5_data = ret;
1225 if (access_type == 1 && arm_feature(env, ARM_FEATURE_V6))
1226 env->cp15.c5_data |= (1 << 11);
1227 env->cp15.c6_data = address;
1228 env->exception_index = EXCP_DATA_ABORT;
1233 target_phys_addr_t cpu_get_phys_page_debug(CPUARMState *env, target_ulong addr)
1236 target_ulong page_size;
1240 ret = get_phys_addr(env, addr, 0, 0, &phys_addr, &prot, &page_size);
1248 /* Return basic MPU access permission bits. */
1249 static uint32_t simple_mpu_ap_bits(uint32_t val)
1256 for (i = 0; i < 16; i += 2) {
1257 ret |= (val >> i) & mask;
1263 /* Pad basic MPU access permission bits to extended format. */
1264 static uint32_t extended_mpu_ap_bits(uint32_t val)
1271 for (i = 0; i < 16; i += 2) {
1272 ret |= (val & mask) << i;
1278 void HELPER(set_cp15)(CPUARMState *env, uint32_t insn, uint32_t val)
1284 op1 = (insn >> 21) & 7;
1285 op2 = (insn >> 5) & 7;
1287 switch ((insn >> 16) & 0xf) {
1290 if (arm_feature(env, ARM_FEATURE_XSCALE))
1292 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1294 if (arm_feature(env, ARM_FEATURE_V7)
1295 && op1 == 2 && crm == 0 && op2 == 0) {
1296 env->cp15.c0_cssel = val & 0xf;
1300 case 1: /* System configuration. */
1301 if (arm_feature(env, ARM_FEATURE_V7)
1302 && op1 == 0 && crm == 1 && op2 == 0) {
1303 env->cp15.c1_scr = val;
1306 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1310 if (!arm_feature(env, ARM_FEATURE_XSCALE) || crm == 0)
1311 env->cp15.c1_sys = val;
1312 /* ??? Lots of these bits are not implemented. */
1313 /* This may enable/disable the MMU, so do a TLB flush. */
1316 case 1: /* Auxiliary control register. */
1317 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
1318 env->cp15.c1_xscaleauxcr = val;
1321 /* Not implemented. */
1324 if (arm_feature(env, ARM_FEATURE_XSCALE))
1326 if (env->cp15.c1_coproc != val) {
1327 env->cp15.c1_coproc = val;
1328 /* ??? Is this safe when called from within a TB? */
1336 case 2: /* MMU Page table control / MPU cache control. */
1337 if (arm_feature(env, ARM_FEATURE_MPU)) {
1340 env->cp15.c2_data = val;
1343 env->cp15.c2_insn = val;
1351 env->cp15.c2_base0 = val;
1354 env->cp15.c2_base1 = val;
1358 env->cp15.c2_control = val;
1359 env->cp15.c2_mask = ~(((uint32_t)0xffffffffu) >> val);
1360 env->cp15.c2_base_mask = ~((uint32_t)0x3fffu >> val);
1367 case 3: /* MMU Domain access control / MPU write buffer control. */
1369 tlb_flush(env, 1); /* Flush TLB as domain not tracked in TLB */
1371 case 4: /* Reserved. */
1373 case 5: /* MMU Fault status / MPU access permission. */
1374 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1378 if (arm_feature(env, ARM_FEATURE_MPU))
1379 val = extended_mpu_ap_bits(val);
1380 env->cp15.c5_data = val;
1383 if (arm_feature(env, ARM_FEATURE_MPU))
1384 val = extended_mpu_ap_bits(val);
1385 env->cp15.c5_insn = val;
1388 if (!arm_feature(env, ARM_FEATURE_MPU))
1390 env->cp15.c5_data = val;
1393 if (!arm_feature(env, ARM_FEATURE_MPU))
1395 env->cp15.c5_insn = val;
1401 case 6: /* MMU Fault address / MPU base/size. */
1402 if (arm_feature(env, ARM_FEATURE_MPU)) {
1405 env->cp15.c6_region[crm] = val;
1407 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1411 env->cp15.c6_data = val;
1413 case 1: /* ??? This is WFAR on armv6 */
1415 env->cp15.c6_insn = val;
1422 case 7: /* Cache control. */
1423 env->cp15.c15_i_max = 0x000;
1424 env->cp15.c15_i_min = 0xff0;
1428 /* No cache, so nothing to do except VA->PA translations. */
1429 if (arm_feature(env, ARM_FEATURE_VAPA)) {
1432 if (arm_feature(env, ARM_FEATURE_V7)) {
1433 env->cp15.c7_par = val & 0xfffff6ff;
1435 env->cp15.c7_par = val & 0xfffff1ff;
1440 target_ulong page_size;
1442 int ret, is_user = op2 & 2;
1443 int access_type = op2 & 1;
1446 /* Other states are only available with TrustZone */
1449 ret = get_phys_addr(env, val, access_type, is_user,
1450 &phys_addr, &prot, &page_size);
1452 /* We do not set any attribute bits in the PAR */
1453 if (page_size == (1 << 24)
1454 && arm_feature(env, ARM_FEATURE_V7)) {
1455 env->cp15.c7_par = (phys_addr & 0xff000000) | 1 << 1;
1457 env->cp15.c7_par = phys_addr & 0xfffff000;
1460 env->cp15.c7_par = ((ret & (10 << 1)) >> 5) |
1461 ((ret & (12 << 1)) >> 6) |
1462 ((ret & 0xf) << 1) | 1;
1469 case 8: /* MMU TLB control. */
1471 case 0: /* Invalidate all (TLBIALL) */
1474 case 1: /* Invalidate single TLB entry by MVA and ASID (TLBIMVA) */
1475 tlb_flush_page(env, val & TARGET_PAGE_MASK);
1477 case 2: /* Invalidate by ASID (TLBIASID) */
1478 tlb_flush(env, val == 0);
1480 case 3: /* Invalidate single entry by MVA, all ASIDs (TLBIMVAA) */
1481 tlb_flush_page(env, val & TARGET_PAGE_MASK);
1488 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1490 if (arm_feature(env, ARM_FEATURE_STRONGARM))
1491 break; /* Ignore ReadBuffer access */
1493 case 0: /* Cache lockdown. */
1495 case 0: /* L1 cache. */
1498 env->cp15.c9_data = val;
1501 env->cp15.c9_insn = val;
1507 case 1: /* L2 cache. */
1508 /* Ignore writes to L2 lockdown/auxiliary registers. */
1514 case 1: /* TCM memory region registers. */
1515 /* Not implemented. */
1517 case 12: /* Performance monitor control */
1518 /* Performance monitors are implementation defined in v7,
1519 * but with an ARM recommended set of registers, which we
1520 * follow (although we don't actually implement any counters)
1522 if (!arm_feature(env, ARM_FEATURE_V7)) {
1526 case 0: /* performance monitor control register */
1527 /* only the DP, X, D and E bits are writable */
1528 env->cp15.c9_pmcr &= ~0x39;
1529 env->cp15.c9_pmcr |= (val & 0x39);
1531 case 1: /* Count enable set register */
1533 env->cp15.c9_pmcnten |= val;
1535 case 2: /* Count enable clear */
1537 env->cp15.c9_pmcnten &= ~val;
1539 case 3: /* Overflow flag status */
1540 env->cp15.c9_pmovsr &= ~val;
1542 case 4: /* Software increment */
1543 /* RAZ/WI since we don't implement the software-count event */
1545 case 5: /* Event counter selection register */
1546 /* Since we don't implement any events, writing to this register
1547 * is actually UNPREDICTABLE. So we choose to RAZ/WI.
1554 case 13: /* Performance counters */
1555 if (!arm_feature(env, ARM_FEATURE_V7)) {
1559 case 0: /* Cycle count register: not implemented, so RAZ/WI */
1561 case 1: /* Event type select */
1562 env->cp15.c9_pmxevtyper = val & 0xff;
1564 case 2: /* Event count register */
1565 /* Unimplemented (we have no events), RAZ/WI */
1571 case 14: /* Performance monitor control */
1572 if (!arm_feature(env, ARM_FEATURE_V7)) {
1576 case 0: /* user enable */
1577 env->cp15.c9_pmuserenr = val & 1;
1578 /* changes access rights for cp registers, so flush tbs */
1581 case 1: /* interrupt enable set */
1582 /* We have no event counters so only the C bit can be changed */
1584 env->cp15.c9_pminten |= val;
1586 case 2: /* interrupt enable clear */
1588 env->cp15.c9_pminten &= ~val;
1596 case 10: /* MMU TLB lockdown. */
1597 /* ??? TLB lockdown not implemented. */
1599 case 12: /* Reserved. */
1601 case 13: /* Process ID. */
1604 /* Unlike real hardware the qemu TLB uses virtual addresses,
1605 not modified virtual addresses, so this causes a TLB flush.
1607 if (env->cp15.c13_fcse != val)
1609 env->cp15.c13_fcse = val;
1612 /* This changes the ASID, so do a TLB flush. */
1613 if (env->cp15.c13_context != val
1614 && !arm_feature(env, ARM_FEATURE_MPU))
1616 env->cp15.c13_context = val;
1622 case 14: /* Generic timer */
1623 if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) {
1624 /* Dummy implementation: RAZ/WI for all */
1628 case 15: /* Implementation specific. */
1629 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
1630 if (op2 == 0 && crm == 1) {
1631 if (env->cp15.c15_cpar != (val & 0x3fff)) {
1632 /* Changes cp0 to cp13 behavior, so needs a TB flush. */
1634 env->cp15.c15_cpar = val & 0x3fff;
1640 if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
1644 case 1: /* Set TI925T configuration. */
1645 env->cp15.c15_ticonfig = val & 0xe7;
1646 env->cp15.c0_cpuid = (val & (1 << 5)) ? /* OS_TYPE bit */
1647 ARM_CPUID_TI915T : ARM_CPUID_TI925T;
1649 case 2: /* Set I_max. */
1650 env->cp15.c15_i_max = val;
1652 case 3: /* Set I_min. */
1653 env->cp15.c15_i_min = val;
1655 case 4: /* Set thread-ID. */
1656 env->cp15.c15_threadid = val & 0xffff;
1658 case 8: /* Wait-for-interrupt (deprecated). */
1659 cpu_interrupt(env, CPU_INTERRUPT_HALT);
1665 if (ARM_CPUID(env) == ARM_CPUID_CORTEXA9) {
1668 if ((op1 == 0) && (op2 == 0)) {
1669 env->cp15.c15_power_control = val;
1670 } else if ((op1 == 0) && (op2 == 1)) {
1671 env->cp15.c15_diagnostic = val;
1672 } else if ((op1 == 0) && (op2 == 2)) {
1673 env->cp15.c15_power_diagnostic = val;
1683 /* ??? For debugging only. Should raise illegal instruction exception. */
1684 cpu_abort(env, "Unimplemented cp15 register write (c%d, c%d, {%d, %d})\n",
1685 (insn >> 16) & 0xf, crm, op1, op2);
1688 uint32_t HELPER(get_cp15)(CPUARMState *env, uint32_t insn)
1694 op1 = (insn >> 21) & 7;
1695 op2 = (insn >> 5) & 7;
1697 switch ((insn >> 16) & 0xf) {
1698 case 0: /* ID codes. */
1704 case 0: /* Device ID. */
1705 return env->cp15.c0_cpuid;
1706 case 1: /* Cache Type. */
1707 return env->cp15.c0_cachetype;
1708 case 2: /* TCM status. */
1710 case 3: /* TLB type register. */
1711 return 0; /* No lockable TLB entries. */
1713 /* The MPIDR was standardised in v7; prior to
1714 * this it was implemented only in the 11MPCore.
1715 * For all other pre-v7 cores it does not exist.
1717 if (arm_feature(env, ARM_FEATURE_V7) ||
1718 ARM_CPUID(env) == ARM_CPUID_ARM11MPCORE) {
1719 int mpidr = env->cpu_index;
1720 /* We don't support setting cluster ID ([8..11])
1721 * so these bits always RAZ.
1723 if (arm_feature(env, ARM_FEATURE_V7MP)) {
1725 /* Cores which are uniprocessor (non-coherent)
1726 * but still implement the MP extensions set
1727 * bit 30. (For instance, A9UP.) However we do
1728 * not currently model any of those cores.
1733 /* otherwise fall through to the unimplemented-reg case */
1738 if (!arm_feature(env, ARM_FEATURE_V6))
1740 return env->cp15.c0_c1[op2];
1742 if (!arm_feature(env, ARM_FEATURE_V6))
1744 return env->cp15.c0_c2[op2];
1745 case 3: case 4: case 5: case 6: case 7:
1751 /* These registers aren't documented on arm11 cores. However
1752 Linux looks at them anyway. */
1753 if (!arm_feature(env, ARM_FEATURE_V6))
1757 if (!arm_feature(env, ARM_FEATURE_V7))
1762 return env->cp15.c0_ccsid[env->cp15.c0_cssel];
1764 return env->cp15.c0_clid;
1770 if (op2 != 0 || crm != 0)
1772 return env->cp15.c0_cssel;
1776 case 1: /* System configuration. */
1777 if (arm_feature(env, ARM_FEATURE_V7)
1778 && op1 == 0 && crm == 1 && op2 == 0) {
1779 return env->cp15.c1_scr;
1781 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1784 case 0: /* Control register. */
1785 return env->cp15.c1_sys;
1786 case 1: /* Auxiliary control register. */
1787 if (arm_feature(env, ARM_FEATURE_XSCALE))
1788 return env->cp15.c1_xscaleauxcr;
1789 if (!arm_feature(env, ARM_FEATURE_AUXCR))
1791 switch (ARM_CPUID(env)) {
1792 case ARM_CPUID_ARM1026:
1794 case ARM_CPUID_ARM1136:
1795 case ARM_CPUID_ARM1136_R2:
1796 case ARM_CPUID_ARM1176:
1798 case ARM_CPUID_ARM11MPCORE:
1800 case ARM_CPUID_CORTEXA8:
1802 case ARM_CPUID_CORTEXA9:
1803 case ARM_CPUID_CORTEXA15:
1808 case 2: /* Coprocessor access register. */
1809 if (arm_feature(env, ARM_FEATURE_XSCALE))
1811 return env->cp15.c1_coproc;
1815 case 2: /* MMU Page table control / MPU cache control. */
1816 if (arm_feature(env, ARM_FEATURE_MPU)) {
1819 return env->cp15.c2_data;
1822 return env->cp15.c2_insn;
1830 return env->cp15.c2_base0;
1832 return env->cp15.c2_base1;
1834 return env->cp15.c2_control;
1839 case 3: /* MMU Domain access control / MPU write buffer control. */
1840 return env->cp15.c3;
1841 case 4: /* Reserved. */
1843 case 5: /* MMU Fault status / MPU access permission. */
1844 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1848 if (arm_feature(env, ARM_FEATURE_MPU))
1849 return simple_mpu_ap_bits(env->cp15.c5_data);
1850 return env->cp15.c5_data;
1852 if (arm_feature(env, ARM_FEATURE_MPU))
1853 return simple_mpu_ap_bits(env->cp15.c5_insn);
1854 return env->cp15.c5_insn;
1856 if (!arm_feature(env, ARM_FEATURE_MPU))
1858 return env->cp15.c5_data;
1860 if (!arm_feature(env, ARM_FEATURE_MPU))
1862 return env->cp15.c5_insn;
1866 case 6: /* MMU Fault address. */
1867 if (arm_feature(env, ARM_FEATURE_MPU)) {
1870 return env->cp15.c6_region[crm];
1872 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1876 return env->cp15.c6_data;
1878 if (arm_feature(env, ARM_FEATURE_V6)) {
1879 /* Watchpoint Fault Adrress. */
1880 return 0; /* Not implemented. */
1882 /* Instruction Fault Adrress. */
1883 /* Arm9 doesn't have an IFAR, but implementing it anyway
1884 shouldn't do any harm. */
1885 return env->cp15.c6_insn;
1888 if (arm_feature(env, ARM_FEATURE_V6)) {
1889 /* Instruction Fault Adrress. */
1890 return env->cp15.c6_insn;
1898 case 7: /* Cache control. */
1899 if (crm == 4 && op1 == 0 && op2 == 0) {
1900 return env->cp15.c7_par;
1902 /* FIXME: Should only clear Z flag if destination is r15. */
1905 case 8: /* MMU TLB control. */
1909 case 0: /* Cache lockdown */
1911 case 0: /* L1 cache. */
1912 if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
1917 return env->cp15.c9_data;
1919 return env->cp15.c9_insn;
1923 case 1: /* L2 cache */
1924 /* L2 Lockdown and Auxiliary control. */
1927 /* L2 cache lockdown (A8 only) */
1930 /* L2 cache auxiliary control (A8) or control (A15) */
1931 if (ARM_CPUID(env) == ARM_CPUID_CORTEXA15) {
1932 /* Linux wants the number of processors from here.
1933 * Might as well set the interrupt-controller bit too.
1935 return ((smp_cpus - 1) << 24) | (1 << 23);
1939 /* L2 cache extended control (A15) */
1948 case 12: /* Performance monitor control */
1949 if (!arm_feature(env, ARM_FEATURE_V7)) {
1953 case 0: /* performance monitor control register */
1954 return env->cp15.c9_pmcr;
1955 case 1: /* count enable set */
1956 case 2: /* count enable clear */
1957 return env->cp15.c9_pmcnten;
1958 case 3: /* overflow flag status */
1959 return env->cp15.c9_pmovsr;
1960 case 4: /* software increment */
1961 case 5: /* event counter selection register */
1962 return 0; /* Unimplemented, RAZ/WI */
1966 case 13: /* Performance counters */
1967 if (!arm_feature(env, ARM_FEATURE_V7)) {
1971 case 1: /* Event type select */
1972 return env->cp15.c9_pmxevtyper;
1973 case 0: /* Cycle count register */
1974 case 2: /* Event count register */
1975 /* Unimplemented, so RAZ/WI */
1980 case 14: /* Performance monitor control */
1981 if (!arm_feature(env, ARM_FEATURE_V7)) {
1985 case 0: /* user enable */
1986 return env->cp15.c9_pmuserenr;
1987 case 1: /* interrupt enable set */
1988 case 2: /* interrupt enable clear */
1989 return env->cp15.c9_pminten;
1997 case 10: /* MMU TLB lockdown. */
1998 /* ??? TLB lockdown not implemented. */
2000 case 11: /* TCM DMA control. */
2001 case 12: /* Reserved. */
2003 case 13: /* Process ID. */
2006 return env->cp15.c13_fcse;
2008 return env->cp15.c13_context;
2012 case 14: /* Generic timer */
2013 if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) {
2014 /* Dummy implementation: RAZ/WI for all */
2018 case 15: /* Implementation specific. */
2019 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
2020 if (op2 == 0 && crm == 1)
2021 return env->cp15.c15_cpar;
2025 if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
2029 case 1: /* Read TI925T configuration. */
2030 return env->cp15.c15_ticonfig;
2031 case 2: /* Read I_max. */
2032 return env->cp15.c15_i_max;
2033 case 3: /* Read I_min. */
2034 return env->cp15.c15_i_min;
2035 case 4: /* Read thread-ID. */
2036 return env->cp15.c15_threadid;
2037 case 8: /* TI925T_status */
2040 /* TODO: Peripheral port remap register:
2041 * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt
2042 * controller base address at $rn & ~0xfff and map size of
2043 * 0x200 << ($rn & 0xfff), when MMU is off. */
2046 if (ARM_CPUID(env) == ARM_CPUID_CORTEXA9) {
2049 if ((op1 == 4) && (op2 == 0)) {
2050 /* The config_base_address should hold the value of
2051 * the peripheral base. ARM should get this from a CPU
2052 * object property, but that support isn't available in
2053 * December 2011. Default to 0 for now and board models
2054 * that care can set it by a private hook */
2055 return env->cp15.c15_config_base_address;
2056 } else if ((op1 == 0) && (op2 == 0)) {
2057 /* power_control should be set to maximum latency. Again,
2058 default to 0 and set by private hook */
2059 return env->cp15.c15_power_control;
2060 } else if ((op1 == 0) && (op2 == 1)) {
2061 return env->cp15.c15_diagnostic;
2062 } else if ((op1 == 0) && (op2 == 2)) {
2063 return env->cp15.c15_power_diagnostic;
2066 case 1: /* NEON Busy */
2068 case 5: /* tlb lockdown */
2071 if ((op1 == 5) && (op2 == 2)) {
2083 /* ??? For debugging only. Should raise illegal instruction exception. */
2084 cpu_abort(env, "Unimplemented cp15 register read (c%d, c%d, {%d, %d})\n",
2085 (insn >> 16) & 0xf, crm, op1, op2);
2089 void HELPER(set_r13_banked)(CPUARMState *env, uint32_t mode, uint32_t val)
2091 if ((env->uncached_cpsr & CPSR_M) == mode) {
2092 env->regs[13] = val;
2094 env->banked_r13[bank_number(env, mode)] = val;
2098 uint32_t HELPER(get_r13_banked)(CPUARMState *env, uint32_t mode)
2100 if ((env->uncached_cpsr & CPSR_M) == mode) {
2101 return env->regs[13];
2103 return env->banked_r13[bank_number(env, mode)];
2107 uint32_t HELPER(v7m_mrs)(CPUARMState *env, uint32_t reg)
2111 return xpsr_read(env) & 0xf8000000;
2113 return xpsr_read(env) & 0xf80001ff;
2115 return xpsr_read(env) & 0xff00fc00;
2117 return xpsr_read(env) & 0xff00fdff;
2119 return xpsr_read(env) & 0x000001ff;
2121 return xpsr_read(env) & 0x0700fc00;
2123 return xpsr_read(env) & 0x0700edff;
2125 return env->v7m.current_sp ? env->v7m.other_sp : env->regs[13];
2127 return env->v7m.current_sp ? env->regs[13] : env->v7m.other_sp;
2128 case 16: /* PRIMASK */
2129 return (env->uncached_cpsr & CPSR_I) != 0;
2130 case 17: /* BASEPRI */
2131 case 18: /* BASEPRI_MAX */
2132 return env->v7m.basepri;
2133 case 19: /* FAULTMASK */
2134 return (env->uncached_cpsr & CPSR_F) != 0;
2135 case 20: /* CONTROL */
2136 return env->v7m.control;
2138 /* ??? For debugging only. */
2139 cpu_abort(env, "Unimplemented system register read (%d)\n", reg);
2144 void HELPER(v7m_msr)(CPUARMState *env, uint32_t reg, uint32_t val)
2148 xpsr_write(env, val, 0xf8000000);
2151 xpsr_write(env, val, 0xf8000000);
2154 xpsr_write(env, val, 0xfe00fc00);
2157 xpsr_write(env, val, 0xfe00fc00);
2160 /* IPSR bits are readonly. */
2163 xpsr_write(env, val, 0x0600fc00);
2166 xpsr_write(env, val, 0x0600fc00);
2169 if (env->v7m.current_sp)
2170 env->v7m.other_sp = val;
2172 env->regs[13] = val;
2175 if (env->v7m.current_sp)
2176 env->regs[13] = val;
2178 env->v7m.other_sp = val;
2180 case 16: /* PRIMASK */
2182 env->uncached_cpsr |= CPSR_I;
2184 env->uncached_cpsr &= ~CPSR_I;
2186 case 17: /* BASEPRI */
2187 env->v7m.basepri = val & 0xff;
2189 case 18: /* BASEPRI_MAX */
2191 if (val != 0 && (val < env->v7m.basepri || env->v7m.basepri == 0))
2192 env->v7m.basepri = val;
2194 case 19: /* FAULTMASK */
2196 env->uncached_cpsr |= CPSR_F;
2198 env->uncached_cpsr &= ~CPSR_F;
2200 case 20: /* CONTROL */
2201 env->v7m.control = val & 3;
2202 switch_v7m_sp(env, (val & 2) != 0);
2205 /* ??? For debugging only. */
2206 cpu_abort(env, "Unimplemented system register write (%d)\n", reg);
2213 /* Note that signed overflow is undefined in C. The following routines are
2214 careful to use unsigned types where modulo arithmetic is required.
2215 Failure to do so _will_ break on newer gcc. */
2217 /* Signed saturating arithmetic. */
2219 /* Perform 16-bit signed saturating addition. */
2220 static inline uint16_t add16_sat(uint16_t a, uint16_t b)
2225 if (((res ^ a) & 0x8000) && !((a ^ b) & 0x8000)) {
2234 /* Perform 8-bit signed saturating addition. */
2235 static inline uint8_t add8_sat(uint8_t a, uint8_t b)
2240 if (((res ^ a) & 0x80) && !((a ^ b) & 0x80)) {
2249 /* Perform 16-bit signed saturating subtraction. */
2250 static inline uint16_t sub16_sat(uint16_t a, uint16_t b)
2255 if (((res ^ a) & 0x8000) && ((a ^ b) & 0x8000)) {
2264 /* Perform 8-bit signed saturating subtraction. */
2265 static inline uint8_t sub8_sat(uint8_t a, uint8_t b)
2270 if (((res ^ a) & 0x80) && ((a ^ b) & 0x80)) {
2279 #define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16);
2280 #define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16);
2281 #define ADD8(a, b, n) RESULT(add8_sat(a, b), n, 8);
2282 #define SUB8(a, b, n) RESULT(sub8_sat(a, b), n, 8);
2285 #include "op_addsub.h"
2287 /* Unsigned saturating arithmetic. */
2288 static inline uint16_t add16_usat(uint16_t a, uint16_t b)
2297 static inline uint16_t sub16_usat(uint16_t a, uint16_t b)
2305 static inline uint8_t add8_usat(uint8_t a, uint8_t b)
2314 static inline uint8_t sub8_usat(uint8_t a, uint8_t b)
2322 #define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16);
2323 #define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16);
2324 #define ADD8(a, b, n) RESULT(add8_usat(a, b), n, 8);
2325 #define SUB8(a, b, n) RESULT(sub8_usat(a, b), n, 8);
2328 #include "op_addsub.h"
2330 /* Signed modulo arithmetic. */
2331 #define SARITH16(a, b, n, op) do { \
2333 sum = (int32_t)(int16_t)(a) op (int32_t)(int16_t)(b); \
2334 RESULT(sum, n, 16); \
2336 ge |= 3 << (n * 2); \
2339 #define SARITH8(a, b, n, op) do { \
2341 sum = (int32_t)(int8_t)(a) op (int32_t)(int8_t)(b); \
2342 RESULT(sum, n, 8); \
2348 #define ADD16(a, b, n) SARITH16(a, b, n, +)
2349 #define SUB16(a, b, n) SARITH16(a, b, n, -)
2350 #define ADD8(a, b, n) SARITH8(a, b, n, +)
2351 #define SUB8(a, b, n) SARITH8(a, b, n, -)
2355 #include "op_addsub.h"
2357 /* Unsigned modulo arithmetic. */
2358 #define ADD16(a, b, n) do { \
2360 sum = (uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b); \
2361 RESULT(sum, n, 16); \
2362 if ((sum >> 16) == 1) \
2363 ge |= 3 << (n * 2); \
2366 #define ADD8(a, b, n) do { \
2368 sum = (uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b); \
2369 RESULT(sum, n, 8); \
2370 if ((sum >> 8) == 1) \
2374 #define SUB16(a, b, n) do { \
2376 sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \
2377 RESULT(sum, n, 16); \
2378 if ((sum >> 16) == 0) \
2379 ge |= 3 << (n * 2); \
2382 #define SUB8(a, b, n) do { \
2384 sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \
2385 RESULT(sum, n, 8); \
2386 if ((sum >> 8) == 0) \
2393 #include "op_addsub.h"
2395 /* Halved signed arithmetic. */
2396 #define ADD16(a, b, n) \
2397 RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16)
2398 #define SUB16(a, b, n) \
2399 RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16)
2400 #define ADD8(a, b, n) \
2401 RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8)
2402 #define SUB8(a, b, n) \
2403 RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8)
2406 #include "op_addsub.h"
2408 /* Halved unsigned arithmetic. */
2409 #define ADD16(a, b, n) \
2410 RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16)
2411 #define SUB16(a, b, n) \
2412 RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16)
2413 #define ADD8(a, b, n) \
2414 RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8)
2415 #define SUB8(a, b, n) \
2416 RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8)
2419 #include "op_addsub.h"
2421 static inline uint8_t do_usad(uint8_t a, uint8_t b)
2429 /* Unsigned sum of absolute byte differences. */
2430 uint32_t HELPER(usad8)(uint32_t a, uint32_t b)
2433 sum = do_usad(a, b);
2434 sum += do_usad(a >> 8, b >> 8);
2435 sum += do_usad(a >> 16, b >>16);
2436 sum += do_usad(a >> 24, b >> 24);
2440 /* For ARMv6 SEL instruction. */
2441 uint32_t HELPER(sel_flags)(uint32_t flags, uint32_t a, uint32_t b)
2454 return (a & mask) | (b & ~mask);
2457 uint32_t HELPER(logicq_cc)(uint64_t val)
2459 return (val >> 32) | (val != 0);
2462 /* VFP support. We follow the convention used for VFP instrunctions:
2463 Single precition routines have a "s" suffix, double precision a
2466 /* Convert host exception flags to vfp form. */
2467 static inline int vfp_exceptbits_from_host(int host_bits)
2469 int target_bits = 0;
2471 if (host_bits & float_flag_invalid)
2473 if (host_bits & float_flag_divbyzero)
2475 if (host_bits & float_flag_overflow)
2477 if (host_bits & (float_flag_underflow | float_flag_output_denormal))
2479 if (host_bits & float_flag_inexact)
2480 target_bits |= 0x10;
2481 if (host_bits & float_flag_input_denormal)
2482 target_bits |= 0x80;
2486 uint32_t HELPER(vfp_get_fpscr)(CPUARMState *env)
2491 fpscr = (env->vfp.xregs[ARM_VFP_FPSCR] & 0xffc8ffff)
2492 | (env->vfp.vec_len << 16)
2493 | (env->vfp.vec_stride << 20);
2494 i = get_float_exception_flags(&env->vfp.fp_status);
2495 i |= get_float_exception_flags(&env->vfp.standard_fp_status);
2496 fpscr |= vfp_exceptbits_from_host(i);
2500 uint32_t vfp_get_fpscr(CPUARMState *env)
2502 return HELPER(vfp_get_fpscr)(env);
2505 /* Convert vfp exception flags to target form. */
2506 static inline int vfp_exceptbits_to_host(int target_bits)
2510 if (target_bits & 1)
2511 host_bits |= float_flag_invalid;
2512 if (target_bits & 2)
2513 host_bits |= float_flag_divbyzero;
2514 if (target_bits & 4)
2515 host_bits |= float_flag_overflow;
2516 if (target_bits & 8)
2517 host_bits |= float_flag_underflow;
2518 if (target_bits & 0x10)
2519 host_bits |= float_flag_inexact;
2520 if (target_bits & 0x80)
2521 host_bits |= float_flag_input_denormal;
2525 void HELPER(vfp_set_fpscr)(CPUARMState *env, uint32_t val)
2530 changed = env->vfp.xregs[ARM_VFP_FPSCR];
2531 env->vfp.xregs[ARM_VFP_FPSCR] = (val & 0xffc8ffff);
2532 env->vfp.vec_len = (val >> 16) & 7;
2533 env->vfp.vec_stride = (val >> 20) & 3;
2536 if (changed & (3 << 22)) {
2537 i = (val >> 22) & 3;
2540 i = float_round_nearest_even;
2546 i = float_round_down;
2549 i = float_round_to_zero;
2552 set_float_rounding_mode(i, &env->vfp.fp_status);
2554 if (changed & (1 << 24)) {
2555 set_flush_to_zero((val & (1 << 24)) != 0, &env->vfp.fp_status);
2556 set_flush_inputs_to_zero((val & (1 << 24)) != 0, &env->vfp.fp_status);
2558 if (changed & (1 << 25))
2559 set_default_nan_mode((val & (1 << 25)) != 0, &env->vfp.fp_status);
2561 i = vfp_exceptbits_to_host(val);
2562 set_float_exception_flags(i, &env->vfp.fp_status);
2563 set_float_exception_flags(0, &env->vfp.standard_fp_status);
2566 void vfp_set_fpscr(CPUARMState *env, uint32_t val)
2568 HELPER(vfp_set_fpscr)(env, val);
2571 #define VFP_HELPER(name, p) HELPER(glue(glue(vfp_,name),p))
2573 #define VFP_BINOP(name) \
2574 float32 VFP_HELPER(name, s)(float32 a, float32 b, void *fpstp) \
2576 float_status *fpst = fpstp; \
2577 return float32_ ## name(a, b, fpst); \
2579 float64 VFP_HELPER(name, d)(float64 a, float64 b, void *fpstp) \
2581 float_status *fpst = fpstp; \
2582 return float64_ ## name(a, b, fpst); \
2590 float32 VFP_HELPER(neg, s)(float32 a)
2592 return float32_chs(a);
2595 float64 VFP_HELPER(neg, d)(float64 a)
2597 return float64_chs(a);
2600 float32 VFP_HELPER(abs, s)(float32 a)
2602 return float32_abs(a);
2605 float64 VFP_HELPER(abs, d)(float64 a)
2607 return float64_abs(a);
2610 float32 VFP_HELPER(sqrt, s)(float32 a, CPUARMState *env)
2612 return float32_sqrt(a, &env->vfp.fp_status);
2615 float64 VFP_HELPER(sqrt, d)(float64 a, CPUARMState *env)
2617 return float64_sqrt(a, &env->vfp.fp_status);
2620 /* XXX: check quiet/signaling case */
2621 #define DO_VFP_cmp(p, type) \
2622 void VFP_HELPER(cmp, p)(type a, type b, CPUARMState *env) \
2625 switch(type ## _compare_quiet(a, b, &env->vfp.fp_status)) { \
2626 case 0: flags = 0x6; break; \
2627 case -1: flags = 0x8; break; \
2628 case 1: flags = 0x2; break; \
2629 default: case 2: flags = 0x3; break; \
2631 env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \
2632 | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \
2634 void VFP_HELPER(cmpe, p)(type a, type b, CPUARMState *env) \
2637 switch(type ## _compare(a, b, &env->vfp.fp_status)) { \
2638 case 0: flags = 0x6; break; \
2639 case -1: flags = 0x8; break; \
2640 case 1: flags = 0x2; break; \
2641 default: case 2: flags = 0x3; break; \
2643 env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \
2644 | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \
2646 DO_VFP_cmp(s, float32)
2647 DO_VFP_cmp(d, float64)
2650 /* Integer to float and float to integer conversions */
2652 #define CONV_ITOF(name, fsz, sign) \
2653 float##fsz HELPER(name)(uint32_t x, void *fpstp) \
2655 float_status *fpst = fpstp; \
2656 return sign##int32_to_##float##fsz((sign##int32_t)x, fpst); \
2659 #define CONV_FTOI(name, fsz, sign, round) \
2660 uint32_t HELPER(name)(float##fsz x, void *fpstp) \
2662 float_status *fpst = fpstp; \
2663 if (float##fsz##_is_any_nan(x)) { \
2664 float_raise(float_flag_invalid, fpst); \
2667 return float##fsz##_to_##sign##int32##round(x, fpst); \
2670 #define FLOAT_CONVS(name, p, fsz, sign) \
2671 CONV_ITOF(vfp_##name##to##p, fsz, sign) \
2672 CONV_FTOI(vfp_to##name##p, fsz, sign, ) \
2673 CONV_FTOI(vfp_to##name##z##p, fsz, sign, _round_to_zero)
2675 FLOAT_CONVS(si, s, 32, )
2676 FLOAT_CONVS(si, d, 64, )
2677 FLOAT_CONVS(ui, s, 32, u)
2678 FLOAT_CONVS(ui, d, 64, u)
2684 /* floating point conversion */
2685 float64 VFP_HELPER(fcvtd, s)(float32 x, CPUARMState *env)
2687 float64 r = float32_to_float64(x, &env->vfp.fp_status);
2688 /* ARM requires that S<->D conversion of any kind of NaN generates
2689 * a quiet NaN by forcing the most significant frac bit to 1.
2691 return float64_maybe_silence_nan(r);
2694 float32 VFP_HELPER(fcvts, d)(float64 x, CPUARMState *env)
2696 float32 r = float64_to_float32(x, &env->vfp.fp_status);
2697 /* ARM requires that S<->D conversion of any kind of NaN generates
2698 * a quiet NaN by forcing the most significant frac bit to 1.
2700 return float32_maybe_silence_nan(r);
2703 /* VFP3 fixed point conversion. */
2704 #define VFP_CONV_FIX(name, p, fsz, itype, sign) \
2705 float##fsz HELPER(vfp_##name##to##p)(uint##fsz##_t x, uint32_t shift, \
2708 float_status *fpst = fpstp; \
2710 tmp = sign##int32_to_##float##fsz((itype##_t)x, fpst); \
2711 return float##fsz##_scalbn(tmp, -(int)shift, fpst); \
2713 uint##fsz##_t HELPER(vfp_to##name##p)(float##fsz x, uint32_t shift, \
2716 float_status *fpst = fpstp; \
2718 if (float##fsz##_is_any_nan(x)) { \
2719 float_raise(float_flag_invalid, fpst); \
2722 tmp = float##fsz##_scalbn(x, shift, fpst); \
2723 return float##fsz##_to_##itype##_round_to_zero(tmp, fpst); \
2726 VFP_CONV_FIX(sh, d, 64, int16, )
2727 VFP_CONV_FIX(sl, d, 64, int32, )
2728 VFP_CONV_FIX(uh, d, 64, uint16, u)
2729 VFP_CONV_FIX(ul, d, 64, uint32, u)
2730 VFP_CONV_FIX(sh, s, 32, int16, )
2731 VFP_CONV_FIX(sl, s, 32, int32, )
2732 VFP_CONV_FIX(uh, s, 32, uint16, u)
2733 VFP_CONV_FIX(ul, s, 32, uint32, u)
2736 /* Half precision conversions. */
2737 static float32 do_fcvt_f16_to_f32(uint32_t a, CPUARMState *env, float_status *s)
2739 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
2740 float32 r = float16_to_float32(make_float16(a), ieee, s);
2742 return float32_maybe_silence_nan(r);
2747 static uint32_t do_fcvt_f32_to_f16(float32 a, CPUARMState *env, float_status *s)
2749 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
2750 float16 r = float32_to_float16(a, ieee, s);
2752 r = float16_maybe_silence_nan(r);
2754 return float16_val(r);
2757 float32 HELPER(neon_fcvt_f16_to_f32)(uint32_t a, CPUARMState *env)
2759 return do_fcvt_f16_to_f32(a, env, &env->vfp.standard_fp_status);
2762 uint32_t HELPER(neon_fcvt_f32_to_f16)(float32 a, CPUARMState *env)
2764 return do_fcvt_f32_to_f16(a, env, &env->vfp.standard_fp_status);
2767 float32 HELPER(vfp_fcvt_f16_to_f32)(uint32_t a, CPUARMState *env)
2769 return do_fcvt_f16_to_f32(a, env, &env->vfp.fp_status);
2772 uint32_t HELPER(vfp_fcvt_f32_to_f16)(float32 a, CPUARMState *env)
2774 return do_fcvt_f32_to_f16(a, env, &env->vfp.fp_status);
2777 #define float32_two make_float32(0x40000000)
2778 #define float32_three make_float32(0x40400000)
2779 #define float32_one_point_five make_float32(0x3fc00000)
2781 float32 HELPER(recps_f32)(float32 a, float32 b, CPUARMState *env)
2783 float_status *s = &env->vfp.standard_fp_status;
2784 if ((float32_is_infinity(a) && float32_is_zero_or_denormal(b)) ||
2785 (float32_is_infinity(b) && float32_is_zero_or_denormal(a))) {
2786 if (!(float32_is_zero(a) || float32_is_zero(b))) {
2787 float_raise(float_flag_input_denormal, s);
2791 return float32_sub(float32_two, float32_mul(a, b, s), s);
2794 float32 HELPER(rsqrts_f32)(float32 a, float32 b, CPUARMState *env)
2796 float_status *s = &env->vfp.standard_fp_status;
2798 if ((float32_is_infinity(a) && float32_is_zero_or_denormal(b)) ||
2799 (float32_is_infinity(b) && float32_is_zero_or_denormal(a))) {
2800 if (!(float32_is_zero(a) || float32_is_zero(b))) {
2801 float_raise(float_flag_input_denormal, s);
2803 return float32_one_point_five;
2805 product = float32_mul(a, b, s);
2806 return float32_div(float32_sub(float32_three, product, s), float32_two, s);
2811 /* Constants 256 and 512 are used in some helpers; we avoid relying on
2812 * int->float conversions at run-time. */
2813 #define float64_256 make_float64(0x4070000000000000LL)
2814 #define float64_512 make_float64(0x4080000000000000LL)
2816 /* The algorithm that must be used to calculate the estimate
2817 * is specified by the ARM ARM.
2819 static float64 recip_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;
2826 /* q = (int)(a * 512.0) */
2827 float64 q = float64_mul(float64_512, a, s);
2828 int64_t q_int = float64_to_int64_round_to_zero(q, s);
2830 /* r = 1.0 / (((double)q + 0.5) / 512.0) */
2831 q = int64_to_float64(q_int, s);
2832 q = float64_add(q, float64_half, s);
2833 q = float64_div(q, float64_512, s);
2834 q = float64_div(float64_one, q, s);
2836 /* s = (int)(256.0 * r + 0.5) */
2837 q = float64_mul(q, float64_256, s);
2838 q = float64_add(q, float64_half, s);
2839 q_int = float64_to_int64_round_to_zero(q, s);
2841 /* return (double)s / 256.0 */
2842 return float64_div(int64_to_float64(q_int, s), float64_256, s);
2845 float32 HELPER(recpe_f32)(float32 a, CPUARMState *env)
2847 float_status *s = &env->vfp.standard_fp_status;
2849 uint32_t val32 = float32_val(a);
2852 int a_exp = (val32 & 0x7f800000) >> 23;
2853 int sign = val32 & 0x80000000;
2855 if (float32_is_any_nan(a)) {
2856 if (float32_is_signaling_nan(a)) {
2857 float_raise(float_flag_invalid, s);
2859 return float32_default_nan;
2860 } else if (float32_is_infinity(a)) {
2861 return float32_set_sign(float32_zero, float32_is_neg(a));
2862 } else if (float32_is_zero_or_denormal(a)) {
2863 if (!float32_is_zero(a)) {
2864 float_raise(float_flag_input_denormal, s);
2866 float_raise(float_flag_divbyzero, s);
2867 return float32_set_sign(float32_infinity, float32_is_neg(a));
2868 } else if (a_exp >= 253) {
2869 float_raise(float_flag_underflow, s);
2870 return float32_set_sign(float32_zero, float32_is_neg(a));
2873 f64 = make_float64((0x3feULL << 52)
2874 | ((int64_t)(val32 & 0x7fffff) << 29));
2876 result_exp = 253 - a_exp;
2878 f64 = recip_estimate(f64, env);
2881 | ((result_exp & 0xff) << 23)
2882 | ((float64_val(f64) >> 29) & 0x7fffff);
2883 return make_float32(val32);
2886 /* The algorithm that must be used to calculate the estimate
2887 * is specified by the ARM ARM.
2889 static float64 recip_sqrt_estimate(float64 a, CPUARMState *env)
2891 /* These calculations mustn't set any fp exception flags,
2892 * so we use a local copy of the fp_status.
2894 float_status dummy_status = env->vfp.standard_fp_status;
2895 float_status *s = &dummy_status;
2899 if (float64_lt(a, float64_half, s)) {
2900 /* range 0.25 <= a < 0.5 */
2902 /* a in units of 1/512 rounded down */
2903 /* q0 = (int)(a * 512.0); */
2904 q = float64_mul(float64_512, a, s);
2905 q_int = float64_to_int64_round_to_zero(q, s);
2907 /* reciprocal root r */
2908 /* r = 1.0 / sqrt(((double)q0 + 0.5) / 512.0); */
2909 q = int64_to_float64(q_int, s);
2910 q = float64_add(q, float64_half, s);
2911 q = float64_div(q, float64_512, s);
2912 q = float64_sqrt(q, s);
2913 q = float64_div(float64_one, q, s);
2915 /* range 0.5 <= a < 1.0 */
2917 /* a in units of 1/256 rounded down */
2918 /* q1 = (int)(a * 256.0); */
2919 q = float64_mul(float64_256, a, s);
2920 int64_t q_int = float64_to_int64_round_to_zero(q, s);
2922 /* reciprocal root r */
2923 /* r = 1.0 /sqrt(((double)q1 + 0.5) / 256); */
2924 q = int64_to_float64(q_int, s);
2925 q = float64_add(q, float64_half, s);
2926 q = float64_div(q, float64_256, s);
2927 q = float64_sqrt(q, s);
2928 q = float64_div(float64_one, q, s);
2930 /* r in units of 1/256 rounded to nearest */
2931 /* s = (int)(256.0 * r + 0.5); */
2933 q = float64_mul(q, float64_256,s );
2934 q = float64_add(q, float64_half, s);
2935 q_int = float64_to_int64_round_to_zero(q, s);
2937 /* return (double)s / 256.0;*/
2938 return float64_div(int64_to_float64(q_int, s), float64_256, s);
2941 float32 HELPER(rsqrte_f32)(float32 a, CPUARMState *env)
2943 float_status *s = &env->vfp.standard_fp_status;
2949 val = float32_val(a);
2951 if (float32_is_any_nan(a)) {
2952 if (float32_is_signaling_nan(a)) {
2953 float_raise(float_flag_invalid, s);
2955 return float32_default_nan;
2956 } else if (float32_is_zero_or_denormal(a)) {
2957 if (!float32_is_zero(a)) {
2958 float_raise(float_flag_input_denormal, s);
2960 float_raise(float_flag_divbyzero, s);
2961 return float32_set_sign(float32_infinity, float32_is_neg(a));
2962 } else if (float32_is_neg(a)) {
2963 float_raise(float_flag_invalid, s);
2964 return float32_default_nan;
2965 } else if (float32_is_infinity(a)) {
2966 return float32_zero;
2969 /* Normalize to a double-precision value between 0.25 and 1.0,
2970 * preserving the parity of the exponent. */
2971 if ((val & 0x800000) == 0) {
2972 f64 = make_float64(((uint64_t)(val & 0x80000000) << 32)
2974 | ((uint64_t)(val & 0x7fffff) << 29));
2976 f64 = make_float64(((uint64_t)(val & 0x80000000) << 32)
2978 | ((uint64_t)(val & 0x7fffff) << 29));
2981 result_exp = (380 - ((val & 0x7f800000) >> 23)) / 2;
2983 f64 = recip_sqrt_estimate(f64, env);
2985 val64 = float64_val(f64);
2987 val = ((result_exp & 0xff) << 23)
2988 | ((val64 >> 29) & 0x7fffff);
2989 return make_float32(val);
2992 uint32_t HELPER(recpe_u32)(uint32_t a, CPUARMState *env)
2996 if ((a & 0x80000000) == 0) {
3000 f64 = make_float64((0x3feULL << 52)
3001 | ((int64_t)(a & 0x7fffffff) << 21));
3003 f64 = recip_estimate (f64, env);
3005 return 0x80000000 | ((float64_val(f64) >> 21) & 0x7fffffff);
3008 uint32_t HELPER(rsqrte_u32)(uint32_t a, CPUARMState *env)
3012 if ((a & 0xc0000000) == 0) {
3016 if (a & 0x80000000) {
3017 f64 = make_float64((0x3feULL << 52)
3018 | ((uint64_t)(a & 0x7fffffff) << 21));
3019 } else { /* bits 31-30 == '01' */
3020 f64 = make_float64((0x3fdULL << 52)
3021 | ((uint64_t)(a & 0x3fffffff) << 22));
3024 f64 = recip_sqrt_estimate(f64, env);
3026 return 0x80000000 | ((float64_val(f64) >> 21) & 0x7fffffff);
3029 /* VFPv4 fused multiply-accumulate */
3030 float32 VFP_HELPER(muladd, s)(float32 a, float32 b, float32 c, void *fpstp)
3032 float_status *fpst = fpstp;
3033 return float32_muladd(a, b, c, 0, fpst);
3036 float64 VFP_HELPER(muladd, d)(float64 a, float64 b, float64 c, void *fpstp)
3038 float_status *fpst = fpstp;
3039 return float64_muladd(a, b, c, 0, fpst);