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 int pmreg_read(CPUARMState *env, const ARMCPRegInfo *ri,
104 /* Generic performance monitor register read function for where
105 * user access may be allowed by PMUSERENR.
107 if (arm_current_pl(env) == 0 && !env->cp15.c9_pmuserenr) {
110 *value = CPREG_FIELD32(env, ri);
114 static int pmcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
117 if (arm_current_pl(env) == 0 && !env->cp15.c9_pmuserenr) {
120 /* only the DP, X, D and E bits are writable */
121 env->cp15.c9_pmcr &= ~0x39;
122 env->cp15.c9_pmcr |= (value & 0x39);
126 static int pmcntenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
129 if (arm_current_pl(env) == 0 && !env->cp15.c9_pmuserenr) {
133 env->cp15.c9_pmcnten |= value;
137 static int pmcntenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
140 if (arm_current_pl(env) == 0 && !env->cp15.c9_pmuserenr) {
144 env->cp15.c9_pmcnten &= ~value;
148 static int pmovsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
151 if (arm_current_pl(env) == 0 && !env->cp15.c9_pmuserenr) {
154 env->cp15.c9_pmovsr &= ~value;
158 static int pmxevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
161 if (arm_current_pl(env) == 0 && !env->cp15.c9_pmuserenr) {
164 env->cp15.c9_pmxevtyper = value & 0xff;
168 static int pmuserenr_write(CPUARMState *env, const ARMCPRegInfo *ri,
171 env->cp15.c9_pmuserenr = value & 1;
175 static int pmintenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
178 /* We have no event counters so only the C bit can be changed */
180 env->cp15.c9_pminten |= value;
184 static int pmintenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
188 env->cp15.c9_pminten &= ~value;
192 static const ARMCPRegInfo v7_cp_reginfo[] = {
193 /* DBGDRAR, DBGDSAR: always RAZ since we don't implement memory mapped
196 { .name = "DBGDRAR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 0,
197 .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
198 { .name = "DBGDRAR", .cp = 14, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
199 .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
200 /* the old v6 WFI, UNPREDICTABLE in v7 but we choose to NOP */
201 { .name = "NOP", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
202 .access = PL1_W, .type = ARM_CP_NOP },
203 /* Performance monitors are implementation defined in v7,
204 * but with an ARM recommended set of registers, which we
205 * follow (although we don't actually implement any counters)
207 * Performance registers fall into three categories:
208 * (a) always UNDEF in PL0, RW in PL1 (PMINTENSET, PMINTENCLR)
209 * (b) RO in PL0 (ie UNDEF on write), RW in PL1 (PMUSERENR)
210 * (c) UNDEF in PL0 if PMUSERENR.EN==0, otherwise accessible (all others)
211 * For the cases controlled by PMUSERENR we must set .access to PL0_RW
212 * or PL0_RO as appropriate and then check PMUSERENR in the helper fn.
214 { .name = "PMCNTENSET", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 1,
215 .access = PL0_RW, .resetvalue = 0,
216 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten),
217 .readfn = pmreg_read, .writefn = pmcntenset_write },
218 { .name = "PMCNTENCLR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 2,
219 .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten),
220 .readfn = pmreg_read, .writefn = pmcntenclr_write },
221 { .name = "PMOVSR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 3,
222 .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
223 .readfn = pmreg_read, .writefn = pmovsr_write },
224 /* Unimplemented so WI. Strictly speaking write accesses in PL0 should
227 { .name = "PMSWINC", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 4,
228 .access = PL0_W, .type = ARM_CP_NOP },
229 /* Since we don't implement any events, writing to PMSELR is UNPREDICTABLE.
230 * We choose to RAZ/WI. XXX should respect PMUSERENR.
232 { .name = "PMSELR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 5,
233 .access = PL0_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
234 /* Unimplemented, RAZ/WI. XXX PMUSERENR */
235 { .name = "PMCCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 0,
236 .access = PL0_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
237 { .name = "PMXEVTYPER", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 1,
239 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmxevtyper),
240 .readfn = pmreg_read, .writefn = pmxevtyper_write },
241 /* Unimplemented, RAZ/WI. XXX PMUSERENR */
242 { .name = "PMXEVCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 2,
243 .access = PL0_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
244 { .name = "PMUSERENR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 0,
245 .access = PL0_R | PL1_RW,
246 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmuserenr),
248 .writefn = pmuserenr_write },
249 { .name = "PMINTENSET", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 1,
251 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
253 .writefn = pmintenset_write },
254 { .name = "PMINTENCLR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 2,
256 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
258 .writefn = pmintenclr_write },
262 static int teecr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
269 static int teehbr_read(CPUARMState *env, const ARMCPRegInfo *ri,
272 /* This is a helper function because the user access rights
273 * depend on the value of the TEECR.
275 if (arm_current_pl(env) == 0 && (env->teecr & 1)) {
278 *value = env->teehbr;
282 static int teehbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
285 if (arm_current_pl(env) == 0 && (env->teecr & 1)) {
292 static const ARMCPRegInfo t2ee_cp_reginfo[] = {
293 { .name = "TEECR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 6, .opc2 = 0,
294 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, teecr),
296 .writefn = teecr_write },
297 { .name = "TEEHBR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 6, .opc2 = 0,
298 .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, teehbr),
300 .readfn = teehbr_read, .writefn = teehbr_write },
304 static const ARMCPRegInfo v6k_cp_reginfo[] = {
305 { .name = "TPIDRURW", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 2,
307 .fieldoffset = offsetof(CPUARMState, cp15.c13_tls1),
309 { .name = "TPIDRURO", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 3,
310 .access = PL0_R|PL1_W,
311 .fieldoffset = offsetof(CPUARMState, cp15.c13_tls2),
313 { .name = "TPIDRPRW", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 4,
315 .fieldoffset = offsetof(CPUARMState, cp15.c13_tls3),
320 static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
321 /* Dummy implementation: RAZ/WI the whole crn=14 space */
322 { .name = "GENERIC_TIMER", .cp = 15, .crn = 14,
323 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
324 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
328 void register_cp_regs_for_features(ARMCPU *cpu)
330 /* Register all the coprocessor registers based on feature bits */
331 CPUARMState *env = &cpu->env;
332 if (arm_feature(env, ARM_FEATURE_M)) {
333 /* M profile has no coprocessor registers */
337 define_arm_cp_regs(cpu, cp_reginfo);
338 if (arm_feature(env, ARM_FEATURE_V6)) {
339 define_arm_cp_regs(cpu, v6_cp_reginfo);
341 define_arm_cp_regs(cpu, not_v6_cp_reginfo);
343 if (arm_feature(env, ARM_FEATURE_V6K)) {
344 define_arm_cp_regs(cpu, v6k_cp_reginfo);
346 if (arm_feature(env, ARM_FEATURE_V7)) {
347 /* v7 performance monitor control register: same implementor
348 * field as main ID register, and we implement no event counters.
350 ARMCPRegInfo pmcr = {
351 .name = "PMCR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 0,
352 .access = PL0_RW, .resetvalue = cpu->midr & 0xff000000,
353 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcr),
354 .readfn = pmreg_read, .writefn = pmcr_write
356 define_one_arm_cp_reg(cpu, &pmcr);
357 define_arm_cp_regs(cpu, v7_cp_reginfo);
359 define_arm_cp_regs(cpu, not_v7_cp_reginfo);
361 if (arm_feature(env, ARM_FEATURE_THUMB2EE)) {
362 define_arm_cp_regs(cpu, t2ee_cp_reginfo);
364 if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) {
365 define_arm_cp_regs(cpu, generic_timer_cp_reginfo);
369 ARMCPU *cpu_arm_init(const char *cpu_model)
373 static int inited = 0;
375 if (!object_class_by_name(cpu_model)) {
378 cpu = ARM_CPU(object_new(cpu_model));
380 env->cpu_model_str = cpu_model;
381 arm_cpu_realize(cpu);
383 if (tcg_enabled() && !inited) {
385 arm_translate_init();
389 if (arm_feature(env, ARM_FEATURE_NEON)) {
390 gdb_register_coprocessor(env, vfp_gdb_get_reg, vfp_gdb_set_reg,
391 51, "arm-neon.xml", 0);
392 } else if (arm_feature(env, ARM_FEATURE_VFP3)) {
393 gdb_register_coprocessor(env, vfp_gdb_get_reg, vfp_gdb_set_reg,
394 35, "arm-vfp3.xml", 0);
395 } else if (arm_feature(env, ARM_FEATURE_VFP)) {
396 gdb_register_coprocessor(env, vfp_gdb_get_reg, vfp_gdb_set_reg,
397 19, "arm-vfp.xml", 0);
403 typedef struct ARMCPUListState {
404 fprintf_function cpu_fprintf;
408 /* Sort alphabetically by type name, except for "any". */
409 static gint arm_cpu_list_compare(gconstpointer a, gconstpointer b)
411 ObjectClass *class_a = (ObjectClass *)a;
412 ObjectClass *class_b = (ObjectClass *)b;
413 const char *name_a, *name_b;
415 name_a = object_class_get_name(class_a);
416 name_b = object_class_get_name(class_b);
417 if (strcmp(name_a, "any") == 0) {
419 } else if (strcmp(name_b, "any") == 0) {
422 return strcmp(name_a, name_b);
426 static void arm_cpu_list_entry(gpointer data, gpointer user_data)
428 ObjectClass *oc = data;
429 ARMCPUListState *s = user_data;
431 (*s->cpu_fprintf)(s->file, " %s\n",
432 object_class_get_name(oc));
435 void arm_cpu_list(FILE *f, fprintf_function cpu_fprintf)
437 ARMCPUListState s = {
439 .cpu_fprintf = cpu_fprintf,
443 list = object_class_get_list(TYPE_ARM_CPU, false);
444 list = g_slist_sort(list, arm_cpu_list_compare);
445 (*cpu_fprintf)(f, "Available CPUs:\n");
446 g_slist_foreach(list, arm_cpu_list_entry, &s);
450 void define_one_arm_cp_reg_with_opaque(ARMCPU *cpu,
451 const ARMCPRegInfo *r, void *opaque)
453 /* Define implementations of coprocessor registers.
454 * We store these in a hashtable because typically
455 * there are less than 150 registers in a space which
456 * is 16*16*16*8*8 = 262144 in size.
457 * Wildcarding is supported for the crm, opc1 and opc2 fields.
458 * If a register is defined twice then the second definition is
459 * used, so this can be used to define some generic registers and
460 * then override them with implementation specific variations.
461 * At least one of the original and the second definition should
462 * include ARM_CP_OVERRIDE in its type bits -- this is just a guard
463 * against accidental use.
466 int crmmin = (r->crm == CP_ANY) ? 0 : r->crm;
467 int crmmax = (r->crm == CP_ANY) ? 15 : r->crm;
468 int opc1min = (r->opc1 == CP_ANY) ? 0 : r->opc1;
469 int opc1max = (r->opc1 == CP_ANY) ? 7 : r->opc1;
470 int opc2min = (r->opc2 == CP_ANY) ? 0 : r->opc2;
471 int opc2max = (r->opc2 == CP_ANY) ? 7 : r->opc2;
472 /* 64 bit registers have only CRm and Opc1 fields */
473 assert(!((r->type & ARM_CP_64BIT) && (r->opc2 || r->crn)));
474 /* Check that the register definition has enough info to handle
475 * reads and writes if they are permitted.
477 if (!(r->type & (ARM_CP_SPECIAL|ARM_CP_CONST))) {
478 if (r->access & PL3_R) {
479 assert(r->fieldoffset || r->readfn);
481 if (r->access & PL3_W) {
482 assert(r->fieldoffset || r->writefn);
485 /* Bad type field probably means missing sentinel at end of reg list */
486 assert(cptype_valid(r->type));
487 for (crm = crmmin; crm <= crmmax; crm++) {
488 for (opc1 = opc1min; opc1 <= opc1max; opc1++) {
489 for (opc2 = opc2min; opc2 <= opc2max; opc2++) {
490 uint32_t *key = g_new(uint32_t, 1);
491 ARMCPRegInfo *r2 = g_memdup(r, sizeof(ARMCPRegInfo));
492 int is64 = (r->type & ARM_CP_64BIT) ? 1 : 0;
493 *key = ENCODE_CP_REG(r->cp, is64, r->crn, crm, opc1, opc2);
495 /* Make sure reginfo passed to helpers for wildcarded regs
496 * has the correct crm/opc1/opc2 for this reg, not CP_ANY:
501 /* Overriding of an existing definition must be explicitly
504 if (!(r->type & ARM_CP_OVERRIDE)) {
505 ARMCPRegInfo *oldreg;
506 oldreg = g_hash_table_lookup(cpu->cp_regs, key);
507 if (oldreg && !(oldreg->type & ARM_CP_OVERRIDE)) {
508 fprintf(stderr, "Register redefined: cp=%d %d bit "
509 "crn=%d crm=%d opc1=%d opc2=%d, "
510 "was %s, now %s\n", r2->cp, 32 + 32 * is64,
511 r2->crn, r2->crm, r2->opc1, r2->opc2,
512 oldreg->name, r2->name);
516 g_hash_table_insert(cpu->cp_regs, key, r2);
522 void define_arm_cp_regs_with_opaque(ARMCPU *cpu,
523 const ARMCPRegInfo *regs, void *opaque)
525 /* Define a whole list of registers */
526 const ARMCPRegInfo *r;
527 for (r = regs; r->type != ARM_CP_SENTINEL; r++) {
528 define_one_arm_cp_reg_with_opaque(cpu, r, opaque);
532 const ARMCPRegInfo *get_arm_cp_reginfo(ARMCPU *cpu, uint32_t encoded_cp)
534 return g_hash_table_lookup(cpu->cp_regs, &encoded_cp);
537 int arm_cp_write_ignore(CPUARMState *env, const ARMCPRegInfo *ri,
540 /* Helper coprocessor write function for write-ignore registers */
544 int arm_cp_read_zero(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t *value)
546 /* Helper coprocessor write function for read-as-zero registers */
551 static int bad_mode_switch(CPUARMState *env, int mode)
553 /* Return true if it is not valid for us to switch to
554 * this CPU mode (ie all the UNPREDICTABLE cases in
555 * the ARM ARM CPSRWriteByInstr pseudocode).
558 case ARM_CPU_MODE_USR:
559 case ARM_CPU_MODE_SYS:
560 case ARM_CPU_MODE_SVC:
561 case ARM_CPU_MODE_ABT:
562 case ARM_CPU_MODE_UND:
563 case ARM_CPU_MODE_IRQ:
564 case ARM_CPU_MODE_FIQ:
571 uint32_t cpsr_read(CPUARMState *env)
575 return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) |
576 (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27)
577 | (env->thumb << 5) | ((env->condexec_bits & 3) << 25)
578 | ((env->condexec_bits & 0xfc) << 8)
582 void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask)
584 if (mask & CPSR_NZCV) {
585 env->ZF = (~val) & CPSR_Z;
587 env->CF = (val >> 29) & 1;
588 env->VF = (val << 3) & 0x80000000;
591 env->QF = ((val & CPSR_Q) != 0);
593 env->thumb = ((val & CPSR_T) != 0);
594 if (mask & CPSR_IT_0_1) {
595 env->condexec_bits &= ~3;
596 env->condexec_bits |= (val >> 25) & 3;
598 if (mask & CPSR_IT_2_7) {
599 env->condexec_bits &= 3;
600 env->condexec_bits |= (val >> 8) & 0xfc;
602 if (mask & CPSR_GE) {
603 env->GE = (val >> 16) & 0xf;
606 if ((env->uncached_cpsr ^ val) & mask & CPSR_M) {
607 if (bad_mode_switch(env, val & CPSR_M)) {
608 /* Attempt to switch to an invalid mode: this is UNPREDICTABLE.
609 * We choose to ignore the attempt and leave the CPSR M field
614 switch_mode(env, val & CPSR_M);
617 mask &= ~CACHED_CPSR_BITS;
618 env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask);
621 /* Sign/zero extend */
622 uint32_t HELPER(sxtb16)(uint32_t x)
625 res = (uint16_t)(int8_t)x;
626 res |= (uint32_t)(int8_t)(x >> 16) << 16;
630 uint32_t HELPER(uxtb16)(uint32_t x)
633 res = (uint16_t)(uint8_t)x;
634 res |= (uint32_t)(uint8_t)(x >> 16) << 16;
638 uint32_t HELPER(clz)(uint32_t x)
643 int32_t HELPER(sdiv)(int32_t num, int32_t den)
647 if (num == INT_MIN && den == -1)
652 uint32_t HELPER(udiv)(uint32_t num, uint32_t den)
659 uint32_t HELPER(rbit)(uint32_t x)
661 x = ((x & 0xff000000) >> 24)
662 | ((x & 0x00ff0000) >> 8)
663 | ((x & 0x0000ff00) << 8)
664 | ((x & 0x000000ff) << 24);
665 x = ((x & 0xf0f0f0f0) >> 4)
666 | ((x & 0x0f0f0f0f) << 4);
667 x = ((x & 0x88888888) >> 3)
668 | ((x & 0x44444444) >> 1)
669 | ((x & 0x22222222) << 1)
670 | ((x & 0x11111111) << 3);
674 uint32_t HELPER(abs)(uint32_t x)
676 return ((int32_t)x < 0) ? -x : x;
679 #if defined(CONFIG_USER_ONLY)
681 void do_interrupt (CPUARMState *env)
683 env->exception_index = -1;
686 int cpu_arm_handle_mmu_fault (CPUARMState *env, target_ulong address, int rw,
690 env->exception_index = EXCP_PREFETCH_ABORT;
691 env->cp15.c6_insn = address;
693 env->exception_index = EXCP_DATA_ABORT;
694 env->cp15.c6_data = address;
699 void HELPER(set_cp15)(CPUARMState *env, uint32_t insn, uint32_t val)
701 cpu_abort(env, "cp15 insn %08x\n", insn);
704 uint32_t HELPER(get_cp15)(CPUARMState *env, uint32_t insn)
706 cpu_abort(env, "cp15 insn %08x\n", insn);
709 /* These should probably raise undefined insn exceptions. */
710 void HELPER(v7m_msr)(CPUARMState *env, uint32_t reg, uint32_t val)
712 cpu_abort(env, "v7m_mrs %d\n", reg);
715 uint32_t HELPER(v7m_mrs)(CPUARMState *env, uint32_t reg)
717 cpu_abort(env, "v7m_mrs %d\n", reg);
721 void switch_mode(CPUARMState *env, int mode)
723 if (mode != ARM_CPU_MODE_USR)
724 cpu_abort(env, "Tried to switch out of user mode\n");
727 void HELPER(set_r13_banked)(CPUARMState *env, uint32_t mode, uint32_t val)
729 cpu_abort(env, "banked r13 write\n");
732 uint32_t HELPER(get_r13_banked)(CPUARMState *env, uint32_t mode)
734 cpu_abort(env, "banked r13 read\n");
740 /* Map CPU modes onto saved register banks. */
741 static inline int bank_number(CPUARMState *env, int mode)
744 case ARM_CPU_MODE_USR:
745 case ARM_CPU_MODE_SYS:
747 case ARM_CPU_MODE_SVC:
749 case ARM_CPU_MODE_ABT:
751 case ARM_CPU_MODE_UND:
753 case ARM_CPU_MODE_IRQ:
755 case ARM_CPU_MODE_FIQ:
758 cpu_abort(env, "Bad mode %x\n", mode);
762 void switch_mode(CPUARMState *env, int mode)
767 old_mode = env->uncached_cpsr & CPSR_M;
768 if (mode == old_mode)
771 if (old_mode == ARM_CPU_MODE_FIQ) {
772 memcpy (env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t));
773 memcpy (env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t));
774 } else if (mode == ARM_CPU_MODE_FIQ) {
775 memcpy (env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t));
776 memcpy (env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t));
779 i = bank_number(env, old_mode);
780 env->banked_r13[i] = env->regs[13];
781 env->banked_r14[i] = env->regs[14];
782 env->banked_spsr[i] = env->spsr;
784 i = bank_number(env, mode);
785 env->regs[13] = env->banked_r13[i];
786 env->regs[14] = env->banked_r14[i];
787 env->spsr = env->banked_spsr[i];
790 static void v7m_push(CPUARMState *env, uint32_t val)
793 stl_phys(env->regs[13], val);
796 static uint32_t v7m_pop(CPUARMState *env)
799 val = ldl_phys(env->regs[13]);
804 /* Switch to V7M main or process stack pointer. */
805 static void switch_v7m_sp(CPUARMState *env, int process)
808 if (env->v7m.current_sp != process) {
809 tmp = env->v7m.other_sp;
810 env->v7m.other_sp = env->regs[13];
812 env->v7m.current_sp = process;
816 static void do_v7m_exception_exit(CPUARMState *env)
821 type = env->regs[15];
822 if (env->v7m.exception != 0)
823 armv7m_nvic_complete_irq(env->nvic, env->v7m.exception);
825 /* Switch to the target stack. */
826 switch_v7m_sp(env, (type & 4) != 0);
828 env->regs[0] = v7m_pop(env);
829 env->regs[1] = v7m_pop(env);
830 env->regs[2] = v7m_pop(env);
831 env->regs[3] = v7m_pop(env);
832 env->regs[12] = v7m_pop(env);
833 env->regs[14] = v7m_pop(env);
834 env->regs[15] = v7m_pop(env);
836 xpsr_write(env, xpsr, 0xfffffdff);
837 /* Undo stack alignment. */
840 /* ??? The exception return type specifies Thread/Handler mode. However
841 this is also implied by the xPSR value. Not sure what to do
842 if there is a mismatch. */
843 /* ??? Likewise for mismatches between the CONTROL register and the stack
847 static void do_interrupt_v7m(CPUARMState *env)
849 uint32_t xpsr = xpsr_read(env);
854 if (env->v7m.current_sp)
856 if (env->v7m.exception == 0)
859 /* For exceptions we just mark as pending on the NVIC, and let that
861 /* TODO: Need to escalate if the current priority is higher than the
862 one we're raising. */
863 switch (env->exception_index) {
865 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE);
869 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SVC);
871 case EXCP_PREFETCH_ABORT:
872 case EXCP_DATA_ABORT:
873 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_MEM);
876 if (semihosting_enabled) {
878 nr = arm_lduw_code(env->regs[15], env->bswap_code) & 0xff;
881 env->regs[0] = do_arm_semihosting(env);
885 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_DEBUG);
888 env->v7m.exception = armv7m_nvic_acknowledge_irq(env->nvic);
890 case EXCP_EXCEPTION_EXIT:
891 do_v7m_exception_exit(env);
894 cpu_abort(env, "Unhandled exception 0x%x\n", env->exception_index);
895 return; /* Never happens. Keep compiler happy. */
898 /* Align stack pointer. */
899 /* ??? Should only do this if Configuration Control Register
900 STACKALIGN bit is set. */
901 if (env->regs[13] & 4) {
905 /* Switch to the handler mode. */
907 v7m_push(env, env->regs[15]);
908 v7m_push(env, env->regs[14]);
909 v7m_push(env, env->regs[12]);
910 v7m_push(env, env->regs[3]);
911 v7m_push(env, env->regs[2]);
912 v7m_push(env, env->regs[1]);
913 v7m_push(env, env->regs[0]);
914 switch_v7m_sp(env, 0);
916 env->condexec_bits = 0;
918 addr = ldl_phys(env->v7m.vecbase + env->v7m.exception * 4);
919 env->regs[15] = addr & 0xfffffffe;
920 env->thumb = addr & 1;
923 /* Handle a CPU exception. */
924 void do_interrupt(CPUARMState *env)
932 do_interrupt_v7m(env);
935 /* TODO: Vectored interrupt controller. */
936 switch (env->exception_index) {
938 new_mode = ARM_CPU_MODE_UND;
947 if (semihosting_enabled) {
948 /* Check for semihosting interrupt. */
950 mask = arm_lduw_code(env->regs[15] - 2, env->bswap_code) & 0xff;
952 mask = arm_ldl_code(env->regs[15] - 4, env->bswap_code)
955 /* Only intercept calls from privileged modes, to provide some
956 semblance of security. */
957 if (((mask == 0x123456 && !env->thumb)
958 || (mask == 0xab && env->thumb))
959 && (env->uncached_cpsr & CPSR_M) != ARM_CPU_MODE_USR) {
960 env->regs[0] = do_arm_semihosting(env);
964 new_mode = ARM_CPU_MODE_SVC;
967 /* The PC already points to the next instruction. */
971 /* See if this is a semihosting syscall. */
972 if (env->thumb && semihosting_enabled) {
973 mask = arm_lduw_code(env->regs[15], env->bswap_code) & 0xff;
975 && (env->uncached_cpsr & CPSR_M) != ARM_CPU_MODE_USR) {
977 env->regs[0] = do_arm_semihosting(env);
981 env->cp15.c5_insn = 2;
982 /* Fall through to prefetch abort. */
983 case EXCP_PREFETCH_ABORT:
984 new_mode = ARM_CPU_MODE_ABT;
986 mask = CPSR_A | CPSR_I;
989 case EXCP_DATA_ABORT:
990 new_mode = ARM_CPU_MODE_ABT;
992 mask = CPSR_A | CPSR_I;
996 new_mode = ARM_CPU_MODE_IRQ;
998 /* Disable IRQ and imprecise data aborts. */
999 mask = CPSR_A | CPSR_I;
1003 new_mode = ARM_CPU_MODE_FIQ;
1005 /* Disable FIQ, IRQ and imprecise data aborts. */
1006 mask = CPSR_A | CPSR_I | CPSR_F;
1010 cpu_abort(env, "Unhandled exception 0x%x\n", env->exception_index);
1011 return; /* Never happens. Keep compiler happy. */
1014 if (env->cp15.c1_sys & (1 << 13)) {
1017 switch_mode (env, new_mode);
1018 env->spsr = cpsr_read(env);
1019 /* Clear IT bits. */
1020 env->condexec_bits = 0;
1021 /* Switch to the new mode, and to the correct instruction set. */
1022 env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode;
1023 env->uncached_cpsr |= mask;
1024 /* this is a lie, as the was no c1_sys on V4T/V5, but who cares
1025 * and we should just guard the thumb mode on V4 */
1026 if (arm_feature(env, ARM_FEATURE_V4T)) {
1027 env->thumb = (env->cp15.c1_sys & (1 << 30)) != 0;
1029 env->regs[14] = env->regs[15] + offset;
1030 env->regs[15] = addr;
1031 env->interrupt_request |= CPU_INTERRUPT_EXITTB;
1034 /* Check section/page access permissions.
1035 Returns the page protection flags, or zero if the access is not
1037 static inline int check_ap(CPUARMState *env, int ap, int domain_prot,
1038 int access_type, int is_user)
1042 if (domain_prot == 3) {
1043 return PAGE_READ | PAGE_WRITE;
1046 if (access_type == 1)
1049 prot_ro = PAGE_READ;
1053 if (access_type == 1)
1055 switch ((env->cp15.c1_sys >> 8) & 3) {
1057 return is_user ? 0 : PAGE_READ;
1064 return is_user ? 0 : PAGE_READ | PAGE_WRITE;
1069 return PAGE_READ | PAGE_WRITE;
1071 return PAGE_READ | PAGE_WRITE;
1072 case 4: /* Reserved. */
1075 return is_user ? 0 : prot_ro;
1079 if (!arm_feature (env, ARM_FEATURE_V6K))
1087 static uint32_t get_level1_table_address(CPUARMState *env, uint32_t address)
1091 if (address & env->cp15.c2_mask)
1092 table = env->cp15.c2_base1 & 0xffffc000;
1094 table = env->cp15.c2_base0 & env->cp15.c2_base_mask;
1096 table |= (address >> 18) & 0x3ffc;
1100 static int get_phys_addr_v5(CPUARMState *env, uint32_t address, int access_type,
1101 int is_user, uint32_t *phys_ptr, int *prot,
1102 target_ulong *page_size)
1113 /* Pagetable walk. */
1114 /* Lookup l1 descriptor. */
1115 table = get_level1_table_address(env, address);
1116 desc = ldl_phys(table);
1118 domain = (desc >> 5) & 0x0f;
1119 domain_prot = (env->cp15.c3 >> (domain * 2)) & 3;
1121 /* Section translation fault. */
1125 if (domain_prot == 0 || domain_prot == 2) {
1127 code = 9; /* Section domain fault. */
1129 code = 11; /* Page domain fault. */
1134 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
1135 ap = (desc >> 10) & 3;
1137 *page_size = 1024 * 1024;
1139 /* Lookup l2 entry. */
1141 /* Coarse pagetable. */
1142 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
1144 /* Fine pagetable. */
1145 table = (desc & 0xfffff000) | ((address >> 8) & 0xffc);
1147 desc = ldl_phys(table);
1149 case 0: /* Page translation fault. */
1152 case 1: /* 64k page. */
1153 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
1154 ap = (desc >> (4 + ((address >> 13) & 6))) & 3;
1155 *page_size = 0x10000;
1157 case 2: /* 4k page. */
1158 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
1159 ap = (desc >> (4 + ((address >> 13) & 6))) & 3;
1160 *page_size = 0x1000;
1162 case 3: /* 1k page. */
1164 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
1165 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
1167 /* Page translation fault. */
1172 phys_addr = (desc & 0xfffffc00) | (address & 0x3ff);
1174 ap = (desc >> 4) & 3;
1178 /* Never happens, but compiler isn't smart enough to tell. */
1183 *prot = check_ap(env, ap, domain_prot, access_type, is_user);
1185 /* Access permission fault. */
1189 *phys_ptr = phys_addr;
1192 return code | (domain << 4);
1195 static int get_phys_addr_v6(CPUARMState *env, uint32_t address, int access_type,
1196 int is_user, uint32_t *phys_ptr, int *prot,
1197 target_ulong *page_size)
1209 /* Pagetable walk. */
1210 /* Lookup l1 descriptor. */
1211 table = get_level1_table_address(env, address);
1212 desc = ldl_phys(table);
1215 /* Section translation fault. */
1219 } else if (type == 2 && (desc & (1 << 18))) {
1223 /* Section or page. */
1224 domain = (desc >> 5) & 0x0f;
1226 domain_prot = (env->cp15.c3 >> (domain * 2)) & 3;
1227 if (domain_prot == 0 || domain_prot == 2) {
1229 code = 9; /* Section domain fault. */
1231 code = 11; /* Page domain fault. */
1235 if (desc & (1 << 18)) {
1237 phys_addr = (desc & 0xff000000) | (address & 0x00ffffff);
1238 *page_size = 0x1000000;
1241 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
1242 *page_size = 0x100000;
1244 ap = ((desc >> 10) & 3) | ((desc >> 13) & 4);
1245 xn = desc & (1 << 4);
1248 /* Lookup l2 entry. */
1249 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
1250 desc = ldl_phys(table);
1251 ap = ((desc >> 4) & 3) | ((desc >> 7) & 4);
1253 case 0: /* Page translation fault. */
1256 case 1: /* 64k page. */
1257 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
1258 xn = desc & (1 << 15);
1259 *page_size = 0x10000;
1261 case 2: case 3: /* 4k page. */
1262 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
1264 *page_size = 0x1000;
1267 /* Never happens, but compiler isn't smart enough to tell. */
1272 if (domain_prot == 3) {
1273 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
1275 if (xn && access_type == 2)
1278 /* The simplified model uses AP[0] as an access control bit. */
1279 if ((env->cp15.c1_sys & (1 << 29)) && (ap & 1) == 0) {
1280 /* Access flag fault. */
1281 code = (code == 15) ? 6 : 3;
1284 *prot = check_ap(env, ap, domain_prot, access_type, is_user);
1286 /* Access permission fault. */
1293 *phys_ptr = phys_addr;
1296 return code | (domain << 4);
1299 static int get_phys_addr_mpu(CPUARMState *env, uint32_t address, int access_type,
1300 int is_user, uint32_t *phys_ptr, int *prot)
1306 *phys_ptr = address;
1307 for (n = 7; n >= 0; n--) {
1308 base = env->cp15.c6_region[n];
1309 if ((base & 1) == 0)
1311 mask = 1 << ((base >> 1) & 0x1f);
1312 /* Keep this shift separate from the above to avoid an
1313 (undefined) << 32. */
1314 mask = (mask << 1) - 1;
1315 if (((base ^ address) & ~mask) == 0)
1321 if (access_type == 2) {
1322 mask = env->cp15.c5_insn;
1324 mask = env->cp15.c5_data;
1326 mask = (mask >> (n * 4)) & 0xf;
1333 *prot = PAGE_READ | PAGE_WRITE;
1338 *prot |= PAGE_WRITE;
1341 *prot = PAGE_READ | PAGE_WRITE;
1352 /* Bad permission. */
1359 static inline int get_phys_addr(CPUARMState *env, uint32_t address,
1360 int access_type, int is_user,
1361 uint32_t *phys_ptr, int *prot,
1362 target_ulong *page_size)
1364 /* Fast Context Switch Extension. */
1365 if (address < 0x02000000)
1366 address += env->cp15.c13_fcse;
1368 if ((env->cp15.c1_sys & 1) == 0) {
1369 /* MMU/MPU disabled. */
1370 *phys_ptr = address;
1371 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
1372 *page_size = TARGET_PAGE_SIZE;
1374 } else if (arm_feature(env, ARM_FEATURE_MPU)) {
1375 *page_size = TARGET_PAGE_SIZE;
1376 return get_phys_addr_mpu(env, address, access_type, is_user, phys_ptr,
1378 } else if (env->cp15.c1_sys & (1 << 23)) {
1379 return get_phys_addr_v6(env, address, access_type, is_user, phys_ptr,
1382 return get_phys_addr_v5(env, address, access_type, is_user, phys_ptr,
1387 int cpu_arm_handle_mmu_fault (CPUARMState *env, target_ulong address,
1388 int access_type, int mmu_idx)
1391 target_ulong page_size;
1395 is_user = mmu_idx == MMU_USER_IDX;
1396 ret = get_phys_addr(env, address, access_type, is_user, &phys_addr, &prot,
1399 /* Map a single [sub]page. */
1400 phys_addr &= ~(uint32_t)0x3ff;
1401 address &= ~(uint32_t)0x3ff;
1402 tlb_set_page (env, address, phys_addr, prot, mmu_idx, page_size);
1406 if (access_type == 2) {
1407 env->cp15.c5_insn = ret;
1408 env->cp15.c6_insn = address;
1409 env->exception_index = EXCP_PREFETCH_ABORT;
1411 env->cp15.c5_data = ret;
1412 if (access_type == 1 && arm_feature(env, ARM_FEATURE_V6))
1413 env->cp15.c5_data |= (1 << 11);
1414 env->cp15.c6_data = address;
1415 env->exception_index = EXCP_DATA_ABORT;
1420 target_phys_addr_t cpu_get_phys_page_debug(CPUARMState *env, target_ulong addr)
1423 target_ulong page_size;
1427 ret = get_phys_addr(env, addr, 0, 0, &phys_addr, &prot, &page_size);
1435 /* Return basic MPU access permission bits. */
1436 static uint32_t simple_mpu_ap_bits(uint32_t val)
1443 for (i = 0; i < 16; i += 2) {
1444 ret |= (val >> i) & mask;
1450 /* Pad basic MPU access permission bits to extended format. */
1451 static uint32_t extended_mpu_ap_bits(uint32_t val)
1458 for (i = 0; i < 16; i += 2) {
1459 ret |= (val & mask) << i;
1465 void HELPER(set_cp15)(CPUARMState *env, uint32_t insn, uint32_t val)
1471 op1 = (insn >> 21) & 7;
1472 op2 = (insn >> 5) & 7;
1474 switch ((insn >> 16) & 0xf) {
1477 if (arm_feature(env, ARM_FEATURE_XSCALE))
1479 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1481 if (arm_feature(env, ARM_FEATURE_V7)
1482 && op1 == 2 && crm == 0 && op2 == 0) {
1483 env->cp15.c0_cssel = val & 0xf;
1487 case 1: /* System configuration. */
1488 if (arm_feature(env, ARM_FEATURE_V7)
1489 && op1 == 0 && crm == 1 && op2 == 0) {
1490 env->cp15.c1_scr = val;
1493 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1497 if (!arm_feature(env, ARM_FEATURE_XSCALE) || crm == 0)
1498 env->cp15.c1_sys = val;
1499 /* ??? Lots of these bits are not implemented. */
1500 /* This may enable/disable the MMU, so do a TLB flush. */
1503 case 1: /* Auxiliary control register. */
1504 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
1505 env->cp15.c1_xscaleauxcr = val;
1508 /* Not implemented. */
1511 if (arm_feature(env, ARM_FEATURE_XSCALE))
1513 if (env->cp15.c1_coproc != val) {
1514 env->cp15.c1_coproc = val;
1515 /* ??? Is this safe when called from within a TB? */
1523 case 2: /* MMU Page table control / MPU cache control. */
1524 if (arm_feature(env, ARM_FEATURE_MPU)) {
1527 env->cp15.c2_data = val;
1530 env->cp15.c2_insn = val;
1538 env->cp15.c2_base0 = val;
1541 env->cp15.c2_base1 = val;
1545 env->cp15.c2_control = val;
1546 env->cp15.c2_mask = ~(((uint32_t)0xffffffffu) >> val);
1547 env->cp15.c2_base_mask = ~((uint32_t)0x3fffu >> val);
1554 case 3: /* MMU Domain access control / MPU write buffer control. */
1556 tlb_flush(env, 1); /* Flush TLB as domain not tracked in TLB */
1558 case 4: /* Reserved. */
1560 case 5: /* MMU Fault status / MPU access permission. */
1561 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1565 if (arm_feature(env, ARM_FEATURE_MPU))
1566 val = extended_mpu_ap_bits(val);
1567 env->cp15.c5_data = val;
1570 if (arm_feature(env, ARM_FEATURE_MPU))
1571 val = extended_mpu_ap_bits(val);
1572 env->cp15.c5_insn = val;
1575 if (!arm_feature(env, ARM_FEATURE_MPU))
1577 env->cp15.c5_data = val;
1580 if (!arm_feature(env, ARM_FEATURE_MPU))
1582 env->cp15.c5_insn = val;
1588 case 6: /* MMU Fault address / MPU base/size. */
1589 if (arm_feature(env, ARM_FEATURE_MPU)) {
1592 env->cp15.c6_region[crm] = val;
1594 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1598 env->cp15.c6_data = val;
1600 case 1: /* ??? This is WFAR on armv6 */
1602 env->cp15.c6_insn = val;
1609 case 7: /* Cache control. */
1610 env->cp15.c15_i_max = 0x000;
1611 env->cp15.c15_i_min = 0xff0;
1615 /* No cache, so nothing to do except VA->PA translations. */
1616 if (arm_feature(env, ARM_FEATURE_VAPA)) {
1619 if (arm_feature(env, ARM_FEATURE_V7)) {
1620 env->cp15.c7_par = val & 0xfffff6ff;
1622 env->cp15.c7_par = val & 0xfffff1ff;
1627 target_ulong page_size;
1629 int ret, is_user = op2 & 2;
1630 int access_type = op2 & 1;
1633 /* Other states are only available with TrustZone */
1636 ret = get_phys_addr(env, val, access_type, is_user,
1637 &phys_addr, &prot, &page_size);
1639 /* We do not set any attribute bits in the PAR */
1640 if (page_size == (1 << 24)
1641 && arm_feature(env, ARM_FEATURE_V7)) {
1642 env->cp15.c7_par = (phys_addr & 0xff000000) | 1 << 1;
1644 env->cp15.c7_par = phys_addr & 0xfffff000;
1647 env->cp15.c7_par = ((ret & (10 << 1)) >> 5) |
1648 ((ret & (12 << 1)) >> 6) |
1649 ((ret & 0xf) << 1) | 1;
1656 case 8: /* MMU TLB control. */
1658 case 0: /* Invalidate all (TLBIALL) */
1661 case 1: /* Invalidate single TLB entry by MVA and ASID (TLBIMVA) */
1662 tlb_flush_page(env, val & TARGET_PAGE_MASK);
1664 case 2: /* Invalidate by ASID (TLBIASID) */
1665 tlb_flush(env, val == 0);
1667 case 3: /* Invalidate single entry by MVA, all ASIDs (TLBIMVAA) */
1668 tlb_flush_page(env, val & TARGET_PAGE_MASK);
1675 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1677 if (arm_feature(env, ARM_FEATURE_STRONGARM))
1678 break; /* Ignore ReadBuffer access */
1680 case 0: /* Cache lockdown. */
1682 case 0: /* L1 cache. */
1685 env->cp15.c9_data = val;
1688 env->cp15.c9_insn = val;
1694 case 1: /* L2 cache. */
1695 /* Ignore writes to L2 lockdown/auxiliary registers. */
1701 case 1: /* TCM memory region registers. */
1702 /* Not implemented. */
1708 case 10: /* MMU TLB lockdown. */
1709 /* ??? TLB lockdown not implemented. */
1711 case 12: /* Reserved. */
1713 case 13: /* Process ID. */
1716 /* Unlike real hardware the qemu TLB uses virtual addresses,
1717 not modified virtual addresses, so this causes a TLB flush.
1719 if (env->cp15.c13_fcse != val)
1721 env->cp15.c13_fcse = val;
1724 /* This changes the ASID, so do a TLB flush. */
1725 if (env->cp15.c13_context != val
1726 && !arm_feature(env, ARM_FEATURE_MPU))
1728 env->cp15.c13_context = val;
1734 case 15: /* Implementation specific. */
1735 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
1736 if (op2 == 0 && crm == 1) {
1737 if (env->cp15.c15_cpar != (val & 0x3fff)) {
1738 /* Changes cp0 to cp13 behavior, so needs a TB flush. */
1740 env->cp15.c15_cpar = val & 0x3fff;
1746 if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
1750 case 1: /* Set TI925T configuration. */
1751 env->cp15.c15_ticonfig = val & 0xe7;
1752 env->cp15.c0_cpuid = (val & (1 << 5)) ? /* OS_TYPE bit */
1753 ARM_CPUID_TI915T : ARM_CPUID_TI925T;
1755 case 2: /* Set I_max. */
1756 env->cp15.c15_i_max = val;
1758 case 3: /* Set I_min. */
1759 env->cp15.c15_i_min = val;
1761 case 4: /* Set thread-ID. */
1762 env->cp15.c15_threadid = val & 0xffff;
1764 case 8: /* Wait-for-interrupt (deprecated). */
1765 cpu_interrupt(env, CPU_INTERRUPT_HALT);
1771 if (ARM_CPUID(env) == ARM_CPUID_CORTEXA9) {
1774 if ((op1 == 0) && (op2 == 0)) {
1775 env->cp15.c15_power_control = val;
1776 } else if ((op1 == 0) && (op2 == 1)) {
1777 env->cp15.c15_diagnostic = val;
1778 } else if ((op1 == 0) && (op2 == 2)) {
1779 env->cp15.c15_power_diagnostic = val;
1789 /* ??? For debugging only. Should raise illegal instruction exception. */
1790 cpu_abort(env, "Unimplemented cp15 register write (c%d, c%d, {%d, %d})\n",
1791 (insn >> 16) & 0xf, crm, op1, op2);
1794 uint32_t HELPER(get_cp15)(CPUARMState *env, uint32_t insn)
1800 op1 = (insn >> 21) & 7;
1801 op2 = (insn >> 5) & 7;
1803 switch ((insn >> 16) & 0xf) {
1804 case 0: /* ID codes. */
1810 case 0: /* Device ID. */
1811 return env->cp15.c0_cpuid;
1812 case 1: /* Cache Type. */
1813 return env->cp15.c0_cachetype;
1814 case 2: /* TCM status. */
1816 case 3: /* TLB type register. */
1817 return 0; /* No lockable TLB entries. */
1819 /* The MPIDR was standardised in v7; prior to
1820 * this it was implemented only in the 11MPCore.
1821 * For all other pre-v7 cores it does not exist.
1823 if (arm_feature(env, ARM_FEATURE_V7) ||
1824 ARM_CPUID(env) == ARM_CPUID_ARM11MPCORE) {
1825 int mpidr = env->cpu_index;
1826 /* We don't support setting cluster ID ([8..11])
1827 * so these bits always RAZ.
1829 if (arm_feature(env, ARM_FEATURE_V7MP)) {
1831 /* Cores which are uniprocessor (non-coherent)
1832 * but still implement the MP extensions set
1833 * bit 30. (For instance, A9UP.) However we do
1834 * not currently model any of those cores.
1839 /* otherwise fall through to the unimplemented-reg case */
1844 if (!arm_feature(env, ARM_FEATURE_V6))
1846 return env->cp15.c0_c1[op2];
1848 if (!arm_feature(env, ARM_FEATURE_V6))
1850 return env->cp15.c0_c2[op2];
1851 case 3: case 4: case 5: case 6: case 7:
1857 /* These registers aren't documented on arm11 cores. However
1858 Linux looks at them anyway. */
1859 if (!arm_feature(env, ARM_FEATURE_V6))
1863 if (!arm_feature(env, ARM_FEATURE_V7))
1868 return env->cp15.c0_ccsid[env->cp15.c0_cssel];
1870 return env->cp15.c0_clid;
1876 if (op2 != 0 || crm != 0)
1878 return env->cp15.c0_cssel;
1882 case 1: /* System configuration. */
1883 if (arm_feature(env, ARM_FEATURE_V7)
1884 && op1 == 0 && crm == 1 && op2 == 0) {
1885 return env->cp15.c1_scr;
1887 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1890 case 0: /* Control register. */
1891 return env->cp15.c1_sys;
1892 case 1: /* Auxiliary control register. */
1893 if (arm_feature(env, ARM_FEATURE_XSCALE))
1894 return env->cp15.c1_xscaleauxcr;
1895 if (!arm_feature(env, ARM_FEATURE_AUXCR))
1897 switch (ARM_CPUID(env)) {
1898 case ARM_CPUID_ARM1026:
1900 case ARM_CPUID_ARM1136:
1901 case ARM_CPUID_ARM1136_R2:
1902 case ARM_CPUID_ARM1176:
1904 case ARM_CPUID_ARM11MPCORE:
1906 case ARM_CPUID_CORTEXA8:
1908 case ARM_CPUID_CORTEXA9:
1909 case ARM_CPUID_CORTEXA15:
1914 case 2: /* Coprocessor access register. */
1915 if (arm_feature(env, ARM_FEATURE_XSCALE))
1917 return env->cp15.c1_coproc;
1921 case 2: /* MMU Page table control / MPU cache control. */
1922 if (arm_feature(env, ARM_FEATURE_MPU)) {
1925 return env->cp15.c2_data;
1928 return env->cp15.c2_insn;
1936 return env->cp15.c2_base0;
1938 return env->cp15.c2_base1;
1940 return env->cp15.c2_control;
1945 case 3: /* MMU Domain access control / MPU write buffer control. */
1946 return env->cp15.c3;
1947 case 4: /* Reserved. */
1949 case 5: /* MMU Fault status / MPU access permission. */
1950 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1954 if (arm_feature(env, ARM_FEATURE_MPU))
1955 return simple_mpu_ap_bits(env->cp15.c5_data);
1956 return env->cp15.c5_data;
1958 if (arm_feature(env, ARM_FEATURE_MPU))
1959 return simple_mpu_ap_bits(env->cp15.c5_insn);
1960 return env->cp15.c5_insn;
1962 if (!arm_feature(env, ARM_FEATURE_MPU))
1964 return env->cp15.c5_data;
1966 if (!arm_feature(env, ARM_FEATURE_MPU))
1968 return env->cp15.c5_insn;
1972 case 6: /* MMU Fault address. */
1973 if (arm_feature(env, ARM_FEATURE_MPU)) {
1976 return env->cp15.c6_region[crm];
1978 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1982 return env->cp15.c6_data;
1984 if (arm_feature(env, ARM_FEATURE_V6)) {
1985 /* Watchpoint Fault Adrress. */
1986 return 0; /* Not implemented. */
1988 /* Instruction Fault Adrress. */
1989 /* Arm9 doesn't have an IFAR, but implementing it anyway
1990 shouldn't do any harm. */
1991 return env->cp15.c6_insn;
1994 if (arm_feature(env, ARM_FEATURE_V6)) {
1995 /* Instruction Fault Adrress. */
1996 return env->cp15.c6_insn;
2004 case 7: /* Cache control. */
2005 if (crm == 4 && op1 == 0 && op2 == 0) {
2006 return env->cp15.c7_par;
2008 /* FIXME: Should only clear Z flag if destination is r15. */
2011 case 8: /* MMU TLB control. */
2015 case 0: /* Cache lockdown */
2017 case 0: /* L1 cache. */
2018 if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
2023 return env->cp15.c9_data;
2025 return env->cp15.c9_insn;
2029 case 1: /* L2 cache */
2030 /* L2 Lockdown and Auxiliary control. */
2033 /* L2 cache lockdown (A8 only) */
2036 /* L2 cache auxiliary control (A8) or control (A15) */
2037 if (ARM_CPUID(env) == ARM_CPUID_CORTEXA15) {
2038 /* Linux wants the number of processors from here.
2039 * Might as well set the interrupt-controller bit too.
2041 return ((smp_cpus - 1) << 24) | (1 << 23);
2045 /* L2 cache extended control (A15) */
2058 case 10: /* MMU TLB lockdown. */
2059 /* ??? TLB lockdown not implemented. */
2061 case 11: /* TCM DMA control. */
2062 case 12: /* Reserved. */
2064 case 13: /* Process ID. */
2067 return env->cp15.c13_fcse;
2069 return env->cp15.c13_context;
2073 case 15: /* Implementation specific. */
2074 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
2075 if (op2 == 0 && crm == 1)
2076 return env->cp15.c15_cpar;
2080 if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
2084 case 1: /* Read TI925T configuration. */
2085 return env->cp15.c15_ticonfig;
2086 case 2: /* Read I_max. */
2087 return env->cp15.c15_i_max;
2088 case 3: /* Read I_min. */
2089 return env->cp15.c15_i_min;
2090 case 4: /* Read thread-ID. */
2091 return env->cp15.c15_threadid;
2092 case 8: /* TI925T_status */
2095 /* TODO: Peripheral port remap register:
2096 * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt
2097 * controller base address at $rn & ~0xfff and map size of
2098 * 0x200 << ($rn & 0xfff), when MMU is off. */
2101 if (ARM_CPUID(env) == ARM_CPUID_CORTEXA9) {
2104 if ((op1 == 4) && (op2 == 0)) {
2105 /* The config_base_address should hold the value of
2106 * the peripheral base. ARM should get this from a CPU
2107 * object property, but that support isn't available in
2108 * December 2011. Default to 0 for now and board models
2109 * that care can set it by a private hook */
2110 return env->cp15.c15_config_base_address;
2111 } else if ((op1 == 0) && (op2 == 0)) {
2112 /* power_control should be set to maximum latency. Again,
2113 default to 0 and set by private hook */
2114 return env->cp15.c15_power_control;
2115 } else if ((op1 == 0) && (op2 == 1)) {
2116 return env->cp15.c15_diagnostic;
2117 } else if ((op1 == 0) && (op2 == 2)) {
2118 return env->cp15.c15_power_diagnostic;
2121 case 1: /* NEON Busy */
2123 case 5: /* tlb lockdown */
2126 if ((op1 == 5) && (op2 == 2)) {
2138 /* ??? For debugging only. Should raise illegal instruction exception. */
2139 cpu_abort(env, "Unimplemented cp15 register read (c%d, c%d, {%d, %d})\n",
2140 (insn >> 16) & 0xf, crm, op1, op2);
2144 void HELPER(set_r13_banked)(CPUARMState *env, uint32_t mode, uint32_t val)
2146 if ((env->uncached_cpsr & CPSR_M) == mode) {
2147 env->regs[13] = val;
2149 env->banked_r13[bank_number(env, mode)] = val;
2153 uint32_t HELPER(get_r13_banked)(CPUARMState *env, uint32_t mode)
2155 if ((env->uncached_cpsr & CPSR_M) == mode) {
2156 return env->regs[13];
2158 return env->banked_r13[bank_number(env, mode)];
2162 uint32_t HELPER(v7m_mrs)(CPUARMState *env, uint32_t reg)
2166 return xpsr_read(env) & 0xf8000000;
2168 return xpsr_read(env) & 0xf80001ff;
2170 return xpsr_read(env) & 0xff00fc00;
2172 return xpsr_read(env) & 0xff00fdff;
2174 return xpsr_read(env) & 0x000001ff;
2176 return xpsr_read(env) & 0x0700fc00;
2178 return xpsr_read(env) & 0x0700edff;
2180 return env->v7m.current_sp ? env->v7m.other_sp : env->regs[13];
2182 return env->v7m.current_sp ? env->regs[13] : env->v7m.other_sp;
2183 case 16: /* PRIMASK */
2184 return (env->uncached_cpsr & CPSR_I) != 0;
2185 case 17: /* BASEPRI */
2186 case 18: /* BASEPRI_MAX */
2187 return env->v7m.basepri;
2188 case 19: /* FAULTMASK */
2189 return (env->uncached_cpsr & CPSR_F) != 0;
2190 case 20: /* CONTROL */
2191 return env->v7m.control;
2193 /* ??? For debugging only. */
2194 cpu_abort(env, "Unimplemented system register read (%d)\n", reg);
2199 void HELPER(v7m_msr)(CPUARMState *env, uint32_t reg, uint32_t val)
2203 xpsr_write(env, val, 0xf8000000);
2206 xpsr_write(env, val, 0xf8000000);
2209 xpsr_write(env, val, 0xfe00fc00);
2212 xpsr_write(env, val, 0xfe00fc00);
2215 /* IPSR bits are readonly. */
2218 xpsr_write(env, val, 0x0600fc00);
2221 xpsr_write(env, val, 0x0600fc00);
2224 if (env->v7m.current_sp)
2225 env->v7m.other_sp = val;
2227 env->regs[13] = val;
2230 if (env->v7m.current_sp)
2231 env->regs[13] = val;
2233 env->v7m.other_sp = val;
2235 case 16: /* PRIMASK */
2237 env->uncached_cpsr |= CPSR_I;
2239 env->uncached_cpsr &= ~CPSR_I;
2241 case 17: /* BASEPRI */
2242 env->v7m.basepri = val & 0xff;
2244 case 18: /* BASEPRI_MAX */
2246 if (val != 0 && (val < env->v7m.basepri || env->v7m.basepri == 0))
2247 env->v7m.basepri = val;
2249 case 19: /* FAULTMASK */
2251 env->uncached_cpsr |= CPSR_F;
2253 env->uncached_cpsr &= ~CPSR_F;
2255 case 20: /* CONTROL */
2256 env->v7m.control = val & 3;
2257 switch_v7m_sp(env, (val & 2) != 0);
2260 /* ??? For debugging only. */
2261 cpu_abort(env, "Unimplemented system register write (%d)\n", reg);
2268 /* Note that signed overflow is undefined in C. The following routines are
2269 careful to use unsigned types where modulo arithmetic is required.
2270 Failure to do so _will_ break on newer gcc. */
2272 /* Signed saturating arithmetic. */
2274 /* Perform 16-bit signed saturating addition. */
2275 static inline uint16_t add16_sat(uint16_t a, uint16_t b)
2280 if (((res ^ a) & 0x8000) && !((a ^ b) & 0x8000)) {
2289 /* Perform 8-bit signed saturating addition. */
2290 static inline uint8_t add8_sat(uint8_t a, uint8_t b)
2295 if (((res ^ a) & 0x80) && !((a ^ b) & 0x80)) {
2304 /* Perform 16-bit signed saturating subtraction. */
2305 static inline uint16_t sub16_sat(uint16_t a, uint16_t b)
2310 if (((res ^ a) & 0x8000) && ((a ^ b) & 0x8000)) {
2319 /* Perform 8-bit signed saturating subtraction. */
2320 static inline uint8_t sub8_sat(uint8_t a, uint8_t b)
2325 if (((res ^ a) & 0x80) && ((a ^ b) & 0x80)) {
2334 #define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16);
2335 #define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16);
2336 #define ADD8(a, b, n) RESULT(add8_sat(a, b), n, 8);
2337 #define SUB8(a, b, n) RESULT(sub8_sat(a, b), n, 8);
2340 #include "op_addsub.h"
2342 /* Unsigned saturating arithmetic. */
2343 static inline uint16_t add16_usat(uint16_t a, uint16_t b)
2352 static inline uint16_t sub16_usat(uint16_t a, uint16_t b)
2360 static inline uint8_t add8_usat(uint8_t a, uint8_t b)
2369 static inline uint8_t sub8_usat(uint8_t a, uint8_t b)
2377 #define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16);
2378 #define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16);
2379 #define ADD8(a, b, n) RESULT(add8_usat(a, b), n, 8);
2380 #define SUB8(a, b, n) RESULT(sub8_usat(a, b), n, 8);
2383 #include "op_addsub.h"
2385 /* Signed modulo arithmetic. */
2386 #define SARITH16(a, b, n, op) do { \
2388 sum = (int32_t)(int16_t)(a) op (int32_t)(int16_t)(b); \
2389 RESULT(sum, n, 16); \
2391 ge |= 3 << (n * 2); \
2394 #define SARITH8(a, b, n, op) do { \
2396 sum = (int32_t)(int8_t)(a) op (int32_t)(int8_t)(b); \
2397 RESULT(sum, n, 8); \
2403 #define ADD16(a, b, n) SARITH16(a, b, n, +)
2404 #define SUB16(a, b, n) SARITH16(a, b, n, -)
2405 #define ADD8(a, b, n) SARITH8(a, b, n, +)
2406 #define SUB8(a, b, n) SARITH8(a, b, n, -)
2410 #include "op_addsub.h"
2412 /* Unsigned modulo arithmetic. */
2413 #define ADD16(a, b, n) do { \
2415 sum = (uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b); \
2416 RESULT(sum, n, 16); \
2417 if ((sum >> 16) == 1) \
2418 ge |= 3 << (n * 2); \
2421 #define ADD8(a, b, n) do { \
2423 sum = (uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b); \
2424 RESULT(sum, n, 8); \
2425 if ((sum >> 8) == 1) \
2429 #define SUB16(a, b, n) do { \
2431 sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \
2432 RESULT(sum, n, 16); \
2433 if ((sum >> 16) == 0) \
2434 ge |= 3 << (n * 2); \
2437 #define SUB8(a, b, n) do { \
2439 sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \
2440 RESULT(sum, n, 8); \
2441 if ((sum >> 8) == 0) \
2448 #include "op_addsub.h"
2450 /* Halved signed arithmetic. */
2451 #define ADD16(a, b, n) \
2452 RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16)
2453 #define SUB16(a, b, n) \
2454 RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16)
2455 #define ADD8(a, b, n) \
2456 RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8)
2457 #define SUB8(a, b, n) \
2458 RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8)
2461 #include "op_addsub.h"
2463 /* Halved unsigned arithmetic. */
2464 #define ADD16(a, b, n) \
2465 RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16)
2466 #define SUB16(a, b, n) \
2467 RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16)
2468 #define ADD8(a, b, n) \
2469 RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8)
2470 #define SUB8(a, b, n) \
2471 RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8)
2474 #include "op_addsub.h"
2476 static inline uint8_t do_usad(uint8_t a, uint8_t b)
2484 /* Unsigned sum of absolute byte differences. */
2485 uint32_t HELPER(usad8)(uint32_t a, uint32_t b)
2488 sum = do_usad(a, b);
2489 sum += do_usad(a >> 8, b >> 8);
2490 sum += do_usad(a >> 16, b >>16);
2491 sum += do_usad(a >> 24, b >> 24);
2495 /* For ARMv6 SEL instruction. */
2496 uint32_t HELPER(sel_flags)(uint32_t flags, uint32_t a, uint32_t b)
2509 return (a & mask) | (b & ~mask);
2512 uint32_t HELPER(logicq_cc)(uint64_t val)
2514 return (val >> 32) | (val != 0);
2517 /* VFP support. We follow the convention used for VFP instrunctions:
2518 Single precition routines have a "s" suffix, double precision a
2521 /* Convert host exception flags to vfp form. */
2522 static inline int vfp_exceptbits_from_host(int host_bits)
2524 int target_bits = 0;
2526 if (host_bits & float_flag_invalid)
2528 if (host_bits & float_flag_divbyzero)
2530 if (host_bits & float_flag_overflow)
2532 if (host_bits & (float_flag_underflow | float_flag_output_denormal))
2534 if (host_bits & float_flag_inexact)
2535 target_bits |= 0x10;
2536 if (host_bits & float_flag_input_denormal)
2537 target_bits |= 0x80;
2541 uint32_t HELPER(vfp_get_fpscr)(CPUARMState *env)
2546 fpscr = (env->vfp.xregs[ARM_VFP_FPSCR] & 0xffc8ffff)
2547 | (env->vfp.vec_len << 16)
2548 | (env->vfp.vec_stride << 20);
2549 i = get_float_exception_flags(&env->vfp.fp_status);
2550 i |= get_float_exception_flags(&env->vfp.standard_fp_status);
2551 fpscr |= vfp_exceptbits_from_host(i);
2555 uint32_t vfp_get_fpscr(CPUARMState *env)
2557 return HELPER(vfp_get_fpscr)(env);
2560 /* Convert vfp exception flags to target form. */
2561 static inline int vfp_exceptbits_to_host(int target_bits)
2565 if (target_bits & 1)
2566 host_bits |= float_flag_invalid;
2567 if (target_bits & 2)
2568 host_bits |= float_flag_divbyzero;
2569 if (target_bits & 4)
2570 host_bits |= float_flag_overflow;
2571 if (target_bits & 8)
2572 host_bits |= float_flag_underflow;
2573 if (target_bits & 0x10)
2574 host_bits |= float_flag_inexact;
2575 if (target_bits & 0x80)
2576 host_bits |= float_flag_input_denormal;
2580 void HELPER(vfp_set_fpscr)(CPUARMState *env, uint32_t val)
2585 changed = env->vfp.xregs[ARM_VFP_FPSCR];
2586 env->vfp.xregs[ARM_VFP_FPSCR] = (val & 0xffc8ffff);
2587 env->vfp.vec_len = (val >> 16) & 7;
2588 env->vfp.vec_stride = (val >> 20) & 3;
2591 if (changed & (3 << 22)) {
2592 i = (val >> 22) & 3;
2595 i = float_round_nearest_even;
2601 i = float_round_down;
2604 i = float_round_to_zero;
2607 set_float_rounding_mode(i, &env->vfp.fp_status);
2609 if (changed & (1 << 24)) {
2610 set_flush_to_zero((val & (1 << 24)) != 0, &env->vfp.fp_status);
2611 set_flush_inputs_to_zero((val & (1 << 24)) != 0, &env->vfp.fp_status);
2613 if (changed & (1 << 25))
2614 set_default_nan_mode((val & (1 << 25)) != 0, &env->vfp.fp_status);
2616 i = vfp_exceptbits_to_host(val);
2617 set_float_exception_flags(i, &env->vfp.fp_status);
2618 set_float_exception_flags(0, &env->vfp.standard_fp_status);
2621 void vfp_set_fpscr(CPUARMState *env, uint32_t val)
2623 HELPER(vfp_set_fpscr)(env, val);
2626 #define VFP_HELPER(name, p) HELPER(glue(glue(vfp_,name),p))
2628 #define VFP_BINOP(name) \
2629 float32 VFP_HELPER(name, s)(float32 a, float32 b, void *fpstp) \
2631 float_status *fpst = fpstp; \
2632 return float32_ ## name(a, b, fpst); \
2634 float64 VFP_HELPER(name, d)(float64 a, float64 b, void *fpstp) \
2636 float_status *fpst = fpstp; \
2637 return float64_ ## name(a, b, fpst); \
2645 float32 VFP_HELPER(neg, s)(float32 a)
2647 return float32_chs(a);
2650 float64 VFP_HELPER(neg, d)(float64 a)
2652 return float64_chs(a);
2655 float32 VFP_HELPER(abs, s)(float32 a)
2657 return float32_abs(a);
2660 float64 VFP_HELPER(abs, d)(float64 a)
2662 return float64_abs(a);
2665 float32 VFP_HELPER(sqrt, s)(float32 a, CPUARMState *env)
2667 return float32_sqrt(a, &env->vfp.fp_status);
2670 float64 VFP_HELPER(sqrt, d)(float64 a, CPUARMState *env)
2672 return float64_sqrt(a, &env->vfp.fp_status);
2675 /* XXX: check quiet/signaling case */
2676 #define DO_VFP_cmp(p, type) \
2677 void VFP_HELPER(cmp, p)(type a, type b, CPUARMState *env) \
2680 switch(type ## _compare_quiet(a, b, &env->vfp.fp_status)) { \
2681 case 0: flags = 0x6; break; \
2682 case -1: flags = 0x8; break; \
2683 case 1: flags = 0x2; break; \
2684 default: case 2: flags = 0x3; break; \
2686 env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \
2687 | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \
2689 void VFP_HELPER(cmpe, p)(type a, type b, CPUARMState *env) \
2692 switch(type ## _compare(a, b, &env->vfp.fp_status)) { \
2693 case 0: flags = 0x6; break; \
2694 case -1: flags = 0x8; break; \
2695 case 1: flags = 0x2; break; \
2696 default: case 2: flags = 0x3; break; \
2698 env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \
2699 | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \
2701 DO_VFP_cmp(s, float32)
2702 DO_VFP_cmp(d, float64)
2705 /* Integer to float and float to integer conversions */
2707 #define CONV_ITOF(name, fsz, sign) \
2708 float##fsz HELPER(name)(uint32_t x, void *fpstp) \
2710 float_status *fpst = fpstp; \
2711 return sign##int32_to_##float##fsz((sign##int32_t)x, fpst); \
2714 #define CONV_FTOI(name, fsz, sign, round) \
2715 uint32_t HELPER(name)(float##fsz x, void *fpstp) \
2717 float_status *fpst = fpstp; \
2718 if (float##fsz##_is_any_nan(x)) { \
2719 float_raise(float_flag_invalid, fpst); \
2722 return float##fsz##_to_##sign##int32##round(x, fpst); \
2725 #define FLOAT_CONVS(name, p, fsz, sign) \
2726 CONV_ITOF(vfp_##name##to##p, fsz, sign) \
2727 CONV_FTOI(vfp_to##name##p, fsz, sign, ) \
2728 CONV_FTOI(vfp_to##name##z##p, fsz, sign, _round_to_zero)
2730 FLOAT_CONVS(si, s, 32, )
2731 FLOAT_CONVS(si, d, 64, )
2732 FLOAT_CONVS(ui, s, 32, u)
2733 FLOAT_CONVS(ui, d, 64, u)
2739 /* floating point conversion */
2740 float64 VFP_HELPER(fcvtd, s)(float32 x, CPUARMState *env)
2742 float64 r = float32_to_float64(x, &env->vfp.fp_status);
2743 /* ARM requires that S<->D conversion of any kind of NaN generates
2744 * a quiet NaN by forcing the most significant frac bit to 1.
2746 return float64_maybe_silence_nan(r);
2749 float32 VFP_HELPER(fcvts, d)(float64 x, CPUARMState *env)
2751 float32 r = float64_to_float32(x, &env->vfp.fp_status);
2752 /* ARM requires that S<->D conversion of any kind of NaN generates
2753 * a quiet NaN by forcing the most significant frac bit to 1.
2755 return float32_maybe_silence_nan(r);
2758 /* VFP3 fixed point conversion. */
2759 #define VFP_CONV_FIX(name, p, fsz, itype, sign) \
2760 float##fsz HELPER(vfp_##name##to##p)(uint##fsz##_t x, uint32_t shift, \
2763 float_status *fpst = fpstp; \
2765 tmp = sign##int32_to_##float##fsz((itype##_t)x, fpst); \
2766 return float##fsz##_scalbn(tmp, -(int)shift, fpst); \
2768 uint##fsz##_t HELPER(vfp_to##name##p)(float##fsz x, uint32_t shift, \
2771 float_status *fpst = fpstp; \
2773 if (float##fsz##_is_any_nan(x)) { \
2774 float_raise(float_flag_invalid, fpst); \
2777 tmp = float##fsz##_scalbn(x, shift, fpst); \
2778 return float##fsz##_to_##itype##_round_to_zero(tmp, fpst); \
2781 VFP_CONV_FIX(sh, d, 64, int16, )
2782 VFP_CONV_FIX(sl, d, 64, int32, )
2783 VFP_CONV_FIX(uh, d, 64, uint16, u)
2784 VFP_CONV_FIX(ul, d, 64, uint32, u)
2785 VFP_CONV_FIX(sh, s, 32, int16, )
2786 VFP_CONV_FIX(sl, s, 32, int32, )
2787 VFP_CONV_FIX(uh, s, 32, uint16, u)
2788 VFP_CONV_FIX(ul, s, 32, uint32, u)
2791 /* Half precision conversions. */
2792 static float32 do_fcvt_f16_to_f32(uint32_t a, CPUARMState *env, float_status *s)
2794 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
2795 float32 r = float16_to_float32(make_float16(a), ieee, s);
2797 return float32_maybe_silence_nan(r);
2802 static uint32_t do_fcvt_f32_to_f16(float32 a, CPUARMState *env, float_status *s)
2804 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
2805 float16 r = float32_to_float16(a, ieee, s);
2807 r = float16_maybe_silence_nan(r);
2809 return float16_val(r);
2812 float32 HELPER(neon_fcvt_f16_to_f32)(uint32_t a, CPUARMState *env)
2814 return do_fcvt_f16_to_f32(a, env, &env->vfp.standard_fp_status);
2817 uint32_t HELPER(neon_fcvt_f32_to_f16)(float32 a, CPUARMState *env)
2819 return do_fcvt_f32_to_f16(a, env, &env->vfp.standard_fp_status);
2822 float32 HELPER(vfp_fcvt_f16_to_f32)(uint32_t a, CPUARMState *env)
2824 return do_fcvt_f16_to_f32(a, env, &env->vfp.fp_status);
2827 uint32_t HELPER(vfp_fcvt_f32_to_f16)(float32 a, CPUARMState *env)
2829 return do_fcvt_f32_to_f16(a, env, &env->vfp.fp_status);
2832 #define float32_two make_float32(0x40000000)
2833 #define float32_three make_float32(0x40400000)
2834 #define float32_one_point_five make_float32(0x3fc00000)
2836 float32 HELPER(recps_f32)(float32 a, float32 b, CPUARMState *env)
2838 float_status *s = &env->vfp.standard_fp_status;
2839 if ((float32_is_infinity(a) && float32_is_zero_or_denormal(b)) ||
2840 (float32_is_infinity(b) && float32_is_zero_or_denormal(a))) {
2841 if (!(float32_is_zero(a) || float32_is_zero(b))) {
2842 float_raise(float_flag_input_denormal, s);
2846 return float32_sub(float32_two, float32_mul(a, b, s), s);
2849 float32 HELPER(rsqrts_f32)(float32 a, float32 b, CPUARMState *env)
2851 float_status *s = &env->vfp.standard_fp_status;
2853 if ((float32_is_infinity(a) && float32_is_zero_or_denormal(b)) ||
2854 (float32_is_infinity(b) && float32_is_zero_or_denormal(a))) {
2855 if (!(float32_is_zero(a) || float32_is_zero(b))) {
2856 float_raise(float_flag_input_denormal, s);
2858 return float32_one_point_five;
2860 product = float32_mul(a, b, s);
2861 return float32_div(float32_sub(float32_three, product, s), float32_two, s);
2866 /* Constants 256 and 512 are used in some helpers; we avoid relying on
2867 * int->float conversions at run-time. */
2868 #define float64_256 make_float64(0x4070000000000000LL)
2869 #define float64_512 make_float64(0x4080000000000000LL)
2871 /* The algorithm that must be used to calculate the estimate
2872 * is specified by the ARM ARM.
2874 static float64 recip_estimate(float64 a, CPUARMState *env)
2876 /* These calculations mustn't set any fp exception flags,
2877 * so we use a local copy of the fp_status.
2879 float_status dummy_status = env->vfp.standard_fp_status;
2880 float_status *s = &dummy_status;
2881 /* q = (int)(a * 512.0) */
2882 float64 q = float64_mul(float64_512, a, s);
2883 int64_t q_int = float64_to_int64_round_to_zero(q, s);
2885 /* r = 1.0 / (((double)q + 0.5) / 512.0) */
2886 q = int64_to_float64(q_int, s);
2887 q = float64_add(q, float64_half, s);
2888 q = float64_div(q, float64_512, s);
2889 q = float64_div(float64_one, q, s);
2891 /* s = (int)(256.0 * r + 0.5) */
2892 q = float64_mul(q, float64_256, s);
2893 q = float64_add(q, float64_half, s);
2894 q_int = float64_to_int64_round_to_zero(q, s);
2896 /* return (double)s / 256.0 */
2897 return float64_div(int64_to_float64(q_int, s), float64_256, s);
2900 float32 HELPER(recpe_f32)(float32 a, CPUARMState *env)
2902 float_status *s = &env->vfp.standard_fp_status;
2904 uint32_t val32 = float32_val(a);
2907 int a_exp = (val32 & 0x7f800000) >> 23;
2908 int sign = val32 & 0x80000000;
2910 if (float32_is_any_nan(a)) {
2911 if (float32_is_signaling_nan(a)) {
2912 float_raise(float_flag_invalid, s);
2914 return float32_default_nan;
2915 } else if (float32_is_infinity(a)) {
2916 return float32_set_sign(float32_zero, float32_is_neg(a));
2917 } else if (float32_is_zero_or_denormal(a)) {
2918 if (!float32_is_zero(a)) {
2919 float_raise(float_flag_input_denormal, s);
2921 float_raise(float_flag_divbyzero, s);
2922 return float32_set_sign(float32_infinity, float32_is_neg(a));
2923 } else if (a_exp >= 253) {
2924 float_raise(float_flag_underflow, s);
2925 return float32_set_sign(float32_zero, float32_is_neg(a));
2928 f64 = make_float64((0x3feULL << 52)
2929 | ((int64_t)(val32 & 0x7fffff) << 29));
2931 result_exp = 253 - a_exp;
2933 f64 = recip_estimate(f64, env);
2936 | ((result_exp & 0xff) << 23)
2937 | ((float64_val(f64) >> 29) & 0x7fffff);
2938 return make_float32(val32);
2941 /* The algorithm that must be used to calculate the estimate
2942 * is specified by the ARM ARM.
2944 static float64 recip_sqrt_estimate(float64 a, CPUARMState *env)
2946 /* These calculations mustn't set any fp exception flags,
2947 * so we use a local copy of the fp_status.
2949 float_status dummy_status = env->vfp.standard_fp_status;
2950 float_status *s = &dummy_status;
2954 if (float64_lt(a, float64_half, s)) {
2955 /* range 0.25 <= a < 0.5 */
2957 /* a in units of 1/512 rounded down */
2958 /* q0 = (int)(a * 512.0); */
2959 q = float64_mul(float64_512, a, s);
2960 q_int = float64_to_int64_round_to_zero(q, s);
2962 /* reciprocal root r */
2963 /* r = 1.0 / sqrt(((double)q0 + 0.5) / 512.0); */
2964 q = int64_to_float64(q_int, s);
2965 q = float64_add(q, float64_half, s);
2966 q = float64_div(q, float64_512, s);
2967 q = float64_sqrt(q, s);
2968 q = float64_div(float64_one, q, s);
2970 /* range 0.5 <= a < 1.0 */
2972 /* a in units of 1/256 rounded down */
2973 /* q1 = (int)(a * 256.0); */
2974 q = float64_mul(float64_256, a, s);
2975 int64_t q_int = float64_to_int64_round_to_zero(q, s);
2977 /* reciprocal root r */
2978 /* r = 1.0 /sqrt(((double)q1 + 0.5) / 256); */
2979 q = int64_to_float64(q_int, s);
2980 q = float64_add(q, float64_half, s);
2981 q = float64_div(q, float64_256, s);
2982 q = float64_sqrt(q, s);
2983 q = float64_div(float64_one, q, s);
2985 /* r in units of 1/256 rounded to nearest */
2986 /* s = (int)(256.0 * r + 0.5); */
2988 q = float64_mul(q, float64_256,s );
2989 q = float64_add(q, float64_half, s);
2990 q_int = float64_to_int64_round_to_zero(q, s);
2992 /* return (double)s / 256.0;*/
2993 return float64_div(int64_to_float64(q_int, s), float64_256, s);
2996 float32 HELPER(rsqrte_f32)(float32 a, CPUARMState *env)
2998 float_status *s = &env->vfp.standard_fp_status;
3004 val = float32_val(a);
3006 if (float32_is_any_nan(a)) {
3007 if (float32_is_signaling_nan(a)) {
3008 float_raise(float_flag_invalid, s);
3010 return float32_default_nan;
3011 } else if (float32_is_zero_or_denormal(a)) {
3012 if (!float32_is_zero(a)) {
3013 float_raise(float_flag_input_denormal, s);
3015 float_raise(float_flag_divbyzero, s);
3016 return float32_set_sign(float32_infinity, float32_is_neg(a));
3017 } else if (float32_is_neg(a)) {
3018 float_raise(float_flag_invalid, s);
3019 return float32_default_nan;
3020 } else if (float32_is_infinity(a)) {
3021 return float32_zero;
3024 /* Normalize to a double-precision value between 0.25 and 1.0,
3025 * preserving the parity of the exponent. */
3026 if ((val & 0x800000) == 0) {
3027 f64 = make_float64(((uint64_t)(val & 0x80000000) << 32)
3029 | ((uint64_t)(val & 0x7fffff) << 29));
3031 f64 = make_float64(((uint64_t)(val & 0x80000000) << 32)
3033 | ((uint64_t)(val & 0x7fffff) << 29));
3036 result_exp = (380 - ((val & 0x7f800000) >> 23)) / 2;
3038 f64 = recip_sqrt_estimate(f64, env);
3040 val64 = float64_val(f64);
3042 val = ((result_exp & 0xff) << 23)
3043 | ((val64 >> 29) & 0x7fffff);
3044 return make_float32(val);
3047 uint32_t HELPER(recpe_u32)(uint32_t a, CPUARMState *env)
3051 if ((a & 0x80000000) == 0) {
3055 f64 = make_float64((0x3feULL << 52)
3056 | ((int64_t)(a & 0x7fffffff) << 21));
3058 f64 = recip_estimate (f64, env);
3060 return 0x80000000 | ((float64_val(f64) >> 21) & 0x7fffffff);
3063 uint32_t HELPER(rsqrte_u32)(uint32_t a, CPUARMState *env)
3067 if ((a & 0xc0000000) == 0) {
3071 if (a & 0x80000000) {
3072 f64 = make_float64((0x3feULL << 52)
3073 | ((uint64_t)(a & 0x7fffffff) << 21));
3074 } else { /* bits 31-30 == '01' */
3075 f64 = make_float64((0x3fdULL << 52)
3076 | ((uint64_t)(a & 0x3fffffff) << 22));
3079 f64 = recip_sqrt_estimate(f64, env);
3081 return 0x80000000 | ((float64_val(f64) >> 21) & 0x7fffffff);
3084 /* VFPv4 fused multiply-accumulate */
3085 float32 VFP_HELPER(muladd, s)(float32 a, float32 b, float32 c, void *fpstp)
3087 float_status *fpst = fpstp;
3088 return float32_muladd(a, b, c, 0, fpst);
3091 float64 VFP_HELPER(muladd, d)(float64 a, float64 b, float64 c, void *fpstp)
3093 float_status *fpst = fpstp;
3094 return float64_muladd(a, b, c, 0, fpst);