QMP: qmp-events.txt: alphabetical order fix and other minor changes
[qemu/ar7.git] / target-arm / helper.c
blob2a98be7436863220401439a1d4bc134ba5429e94
1 #include "cpu.h"
2 #include "exec/gdbstub.h"
3 #include "helper.h"
4 #include "qemu/host-utils.h"
5 #include "sysemu/arch_init.h"
6 #include "sysemu/sysemu.h"
7 #include "qemu/bitops.h"
9 #ifndef CONFIG_USER_ONLY
10 static inline int get_phys_addr(CPUARMState *env, uint32_t address,
11 int access_type, int is_user,
12 hwaddr *phys_ptr, int *prot,
13 target_ulong *page_size);
14 #endif
16 static int vfp_gdb_get_reg(CPUARMState *env, uint8_t *buf, int reg)
18 int nregs;
20 /* VFP data registers are always little-endian. */
21 nregs = arm_feature(env, ARM_FEATURE_VFP3) ? 32 : 16;
22 if (reg < nregs) {
23 stfq_le_p(buf, env->vfp.regs[reg]);
24 return 8;
26 if (arm_feature(env, ARM_FEATURE_NEON)) {
27 /* Aliases for Q regs. */
28 nregs += 16;
29 if (reg < nregs) {
30 stfq_le_p(buf, env->vfp.regs[(reg - 32) * 2]);
31 stfq_le_p(buf + 8, env->vfp.regs[(reg - 32) * 2 + 1]);
32 return 16;
35 switch (reg - nregs) {
36 case 0: stl_p(buf, env->vfp.xregs[ARM_VFP_FPSID]); return 4;
37 case 1: stl_p(buf, env->vfp.xregs[ARM_VFP_FPSCR]); return 4;
38 case 2: stl_p(buf, env->vfp.xregs[ARM_VFP_FPEXC]); return 4;
40 return 0;
43 static int vfp_gdb_set_reg(CPUARMState *env, uint8_t *buf, int reg)
45 int nregs;
47 nregs = arm_feature(env, ARM_FEATURE_VFP3) ? 32 : 16;
48 if (reg < nregs) {
49 env->vfp.regs[reg] = ldfq_le_p(buf);
50 return 8;
52 if (arm_feature(env, ARM_FEATURE_NEON)) {
53 nregs += 16;
54 if (reg < nregs) {
55 env->vfp.regs[(reg - 32) * 2] = ldfq_le_p(buf);
56 env->vfp.regs[(reg - 32) * 2 + 1] = ldfq_le_p(buf + 8);
57 return 16;
60 switch (reg - nregs) {
61 case 0: env->vfp.xregs[ARM_VFP_FPSID] = ldl_p(buf); return 4;
62 case 1: env->vfp.xregs[ARM_VFP_FPSCR] = ldl_p(buf); return 4;
63 case 2: env->vfp.xregs[ARM_VFP_FPEXC] = ldl_p(buf) & (1 << 30); return 4;
65 return 0;
68 static int raw_read(CPUARMState *env, const ARMCPRegInfo *ri,
69 uint64_t *value)
71 if (ri->type & ARM_CP_64BIT) {
72 *value = CPREG_FIELD64(env, ri);
73 } else {
74 *value = CPREG_FIELD32(env, ri);
76 return 0;
79 static int raw_write(CPUARMState *env, const ARMCPRegInfo *ri,
80 uint64_t value)
82 if (ri->type & ARM_CP_64BIT) {
83 CPREG_FIELD64(env, ri) = value;
84 } else {
85 CPREG_FIELD32(env, ri) = value;
87 return 0;
90 static bool read_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri,
91 uint64_t *v)
93 /* Raw read of a coprocessor register (as needed for migration, etc)
94 * return true on success, false if the read is impossible for some reason.
96 if (ri->type & ARM_CP_CONST) {
97 *v = ri->resetvalue;
98 } else if (ri->raw_readfn) {
99 return (ri->raw_readfn(env, ri, v) == 0);
100 } else if (ri->readfn) {
101 return (ri->readfn(env, ri, v) == 0);
102 } else {
103 if (ri->type & ARM_CP_64BIT) {
104 *v = CPREG_FIELD64(env, ri);
105 } else {
106 *v = CPREG_FIELD32(env, ri);
109 return true;
112 static bool write_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri,
113 int64_t v)
115 /* Raw write of a coprocessor register (as needed for migration, etc).
116 * Return true on success, false if the write is impossible for some reason.
117 * Note that constant registers are treated as write-ignored; the
118 * caller should check for success by whether a readback gives the
119 * value written.
121 if (ri->type & ARM_CP_CONST) {
122 return true;
123 } else if (ri->raw_writefn) {
124 return (ri->raw_writefn(env, ri, v) == 0);
125 } else if (ri->writefn) {
126 return (ri->writefn(env, ri, v) == 0);
127 } else {
128 if (ri->type & ARM_CP_64BIT) {
129 CPREG_FIELD64(env, ri) = v;
130 } else {
131 CPREG_FIELD32(env, ri) = v;
134 return true;
137 bool write_cpustate_to_list(ARMCPU *cpu)
139 /* Write the coprocessor state from cpu->env to the (index,value) list. */
140 int i;
141 bool ok = true;
143 for (i = 0; i < cpu->cpreg_array_len; i++) {
144 uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
145 const ARMCPRegInfo *ri;
146 uint64_t v;
147 ri = get_arm_cp_reginfo(cpu, regidx);
148 if (!ri) {
149 ok = false;
150 continue;
152 if (ri->type & ARM_CP_NO_MIGRATE) {
153 continue;
155 if (!read_raw_cp_reg(&cpu->env, ri, &v)) {
156 ok = false;
157 continue;
159 cpu->cpreg_values[i] = v;
161 return ok;
164 bool write_list_to_cpustate(ARMCPU *cpu)
166 int i;
167 bool ok = true;
169 for (i = 0; i < cpu->cpreg_array_len; i++) {
170 uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
171 uint64_t v = cpu->cpreg_values[i];
172 uint64_t readback;
173 const ARMCPRegInfo *ri;
175 ri = get_arm_cp_reginfo(cpu, regidx);
176 if (!ri) {
177 ok = false;
178 continue;
180 if (ri->type & ARM_CP_NO_MIGRATE) {
181 continue;
183 /* Write value and confirm it reads back as written
184 * (to catch read-only registers and partially read-only
185 * registers where the incoming migration value doesn't match)
187 if (!write_raw_cp_reg(&cpu->env, ri, v) ||
188 !read_raw_cp_reg(&cpu->env, ri, &readback) ||
189 readback != v) {
190 ok = false;
193 return ok;
196 static void add_cpreg_to_list(gpointer key, gpointer opaque)
198 ARMCPU *cpu = opaque;
199 uint64_t regidx;
200 const ARMCPRegInfo *ri;
202 regidx = *(uint32_t *)key;
203 ri = get_arm_cp_reginfo(cpu, regidx);
205 if (!(ri->type & ARM_CP_NO_MIGRATE)) {
206 cpu->cpreg_indexes[cpu->cpreg_array_len] = cpreg_to_kvm_id(regidx);
207 /* The value array need not be initialized at this point */
208 cpu->cpreg_array_len++;
212 static void count_cpreg(gpointer key, gpointer opaque)
214 ARMCPU *cpu = opaque;
215 uint64_t regidx;
216 const ARMCPRegInfo *ri;
218 regidx = *(uint32_t *)key;
219 ri = get_arm_cp_reginfo(cpu, regidx);
221 if (!(ri->type & ARM_CP_NO_MIGRATE)) {
222 cpu->cpreg_array_len++;
226 static gint cpreg_key_compare(gconstpointer a, gconstpointer b)
228 uint32_t aidx = *(uint32_t *)a;
229 uint32_t bidx = *(uint32_t *)b;
231 return aidx - bidx;
234 static void cpreg_make_keylist(gpointer key, gpointer value, gpointer udata)
236 GList **plist = udata;
238 *plist = g_list_prepend(*plist, key);
241 void init_cpreg_list(ARMCPU *cpu)
243 /* Initialise the cpreg_tuples[] array based on the cp_regs hash.
244 * Note that we require cpreg_tuples[] to be sorted by key ID.
246 GList *keys = NULL;
247 int arraylen;
249 g_hash_table_foreach(cpu->cp_regs, cpreg_make_keylist, &keys);
251 keys = g_list_sort(keys, cpreg_key_compare);
253 cpu->cpreg_array_len = 0;
255 g_list_foreach(keys, count_cpreg, cpu);
257 arraylen = cpu->cpreg_array_len;
258 cpu->cpreg_indexes = g_new(uint64_t, arraylen);
259 cpu->cpreg_values = g_new(uint64_t, arraylen);
260 cpu->cpreg_vmstate_indexes = g_new(uint64_t, arraylen);
261 cpu->cpreg_vmstate_values = g_new(uint64_t, arraylen);
262 cpu->cpreg_vmstate_array_len = cpu->cpreg_array_len;
263 cpu->cpreg_array_len = 0;
265 g_list_foreach(keys, add_cpreg_to_list, cpu);
267 assert(cpu->cpreg_array_len == arraylen);
269 g_list_free(keys);
272 static int dacr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
274 env->cp15.c3 = value;
275 tlb_flush(env, 1); /* Flush TLB as domain not tracked in TLB */
276 return 0;
279 static int fcse_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
281 if (env->cp15.c13_fcse != value) {
282 /* Unlike real hardware the qemu TLB uses virtual addresses,
283 * not modified virtual addresses, so this causes a TLB flush.
285 tlb_flush(env, 1);
286 env->cp15.c13_fcse = value;
288 return 0;
290 static int contextidr_write(CPUARMState *env, const ARMCPRegInfo *ri,
291 uint64_t value)
293 if (env->cp15.c13_context != value && !arm_feature(env, ARM_FEATURE_MPU)) {
294 /* For VMSA (when not using the LPAE long descriptor page table
295 * format) this register includes the ASID, so do a TLB flush.
296 * For PMSA it is purely a process ID and no action is needed.
298 tlb_flush(env, 1);
300 env->cp15.c13_context = value;
301 return 0;
304 static int tlbiall_write(CPUARMState *env, const ARMCPRegInfo *ri,
305 uint64_t value)
307 /* Invalidate all (TLBIALL) */
308 tlb_flush(env, 1);
309 return 0;
312 static int tlbimva_write(CPUARMState *env, const ARMCPRegInfo *ri,
313 uint64_t value)
315 /* Invalidate single TLB entry by MVA and ASID (TLBIMVA) */
316 tlb_flush_page(env, value & TARGET_PAGE_MASK);
317 return 0;
320 static int tlbiasid_write(CPUARMState *env, const ARMCPRegInfo *ri,
321 uint64_t value)
323 /* Invalidate by ASID (TLBIASID) */
324 tlb_flush(env, value == 0);
325 return 0;
328 static int tlbimvaa_write(CPUARMState *env, const ARMCPRegInfo *ri,
329 uint64_t value)
331 /* Invalidate single entry by MVA, all ASIDs (TLBIMVAA) */
332 tlb_flush_page(env, value & TARGET_PAGE_MASK);
333 return 0;
336 static const ARMCPRegInfo cp_reginfo[] = {
337 /* DBGDIDR: just RAZ. In particular this means the "debug architecture
338 * version" bits will read as a reserved value, which should cause
339 * Linux to not try to use the debug hardware.
341 { .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0,
342 .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
343 /* MMU Domain access control / MPU write buffer control */
344 { .name = "DACR", .cp = 15,
345 .crn = 3, .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
346 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c3),
347 .resetvalue = 0, .writefn = dacr_write, .raw_writefn = raw_write, },
348 { .name = "FCSEIDR", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 0,
349 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c13_fcse),
350 .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
351 { .name = "CONTEXTIDR", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 1,
352 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c13_fcse),
353 .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
354 /* ??? This covers not just the impdef TLB lockdown registers but also
355 * some v7VMSA registers relating to TEX remap, so it is overly broad.
357 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = CP_ANY,
358 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
359 /* MMU TLB control. Note that the wildcarding means we cover not just
360 * the unified TLB ops but also the dside/iside/inner-shareable variants.
362 { .name = "TLBIALL", .cp = 15, .crn = 8, .crm = CP_ANY,
363 .opc1 = CP_ANY, .opc2 = 0, .access = PL1_W, .writefn = tlbiall_write,
364 .type = ARM_CP_NO_MIGRATE },
365 { .name = "TLBIMVA", .cp = 15, .crn = 8, .crm = CP_ANY,
366 .opc1 = CP_ANY, .opc2 = 1, .access = PL1_W, .writefn = tlbimva_write,
367 .type = ARM_CP_NO_MIGRATE },
368 { .name = "TLBIASID", .cp = 15, .crn = 8, .crm = CP_ANY,
369 .opc1 = CP_ANY, .opc2 = 2, .access = PL1_W, .writefn = tlbiasid_write,
370 .type = ARM_CP_NO_MIGRATE },
371 { .name = "TLBIMVAA", .cp = 15, .crn = 8, .crm = CP_ANY,
372 .opc1 = CP_ANY, .opc2 = 3, .access = PL1_W, .writefn = tlbimvaa_write,
373 .type = ARM_CP_NO_MIGRATE },
374 /* Cache maintenance ops; some of this space may be overridden later. */
375 { .name = "CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
376 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
377 .type = ARM_CP_NOP | ARM_CP_OVERRIDE },
378 REGINFO_SENTINEL
381 static const ARMCPRegInfo not_v6_cp_reginfo[] = {
382 /* Not all pre-v6 cores implemented this WFI, so this is slightly
383 * over-broad.
385 { .name = "WFI_v5", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = 2,
386 .access = PL1_W, .type = ARM_CP_WFI },
387 REGINFO_SENTINEL
390 static const ARMCPRegInfo not_v7_cp_reginfo[] = {
391 /* Standard v6 WFI (also used in some pre-v6 cores); not in v7 (which
392 * is UNPREDICTABLE; we choose to NOP as most implementations do).
394 { .name = "WFI_v6", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
395 .access = PL1_W, .type = ARM_CP_WFI },
396 /* L1 cache lockdown. Not architectural in v6 and earlier but in practice
397 * implemented in 926, 946, 1026, 1136, 1176 and 11MPCore. StrongARM and
398 * OMAPCP will override this space.
400 { .name = "DLOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 0,
401 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_data),
402 .resetvalue = 0 },
403 { .name = "ILOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 1,
404 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_insn),
405 .resetvalue = 0 },
406 /* v6 doesn't have the cache ID registers but Linux reads them anyway */
407 { .name = "DUMMY", .cp = 15, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = CP_ANY,
408 .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_MIGRATE,
409 .resetvalue = 0 },
410 REGINFO_SENTINEL
413 static int cpacr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
415 if (env->cp15.c1_coproc != value) {
416 env->cp15.c1_coproc = value;
417 /* ??? Is this safe when called from within a TB? */
418 tb_flush(env);
420 return 0;
423 static const ARMCPRegInfo v6_cp_reginfo[] = {
424 /* prefetch by MVA in v6, NOP in v7 */
425 { .name = "MVA_prefetch",
426 .cp = 15, .crn = 7, .crm = 13, .opc1 = 0, .opc2 = 1,
427 .access = PL1_W, .type = ARM_CP_NOP },
428 { .name = "ISB", .cp = 15, .crn = 7, .crm = 5, .opc1 = 0, .opc2 = 4,
429 .access = PL0_W, .type = ARM_CP_NOP },
430 { .name = "DSB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 4,
431 .access = PL0_W, .type = ARM_CP_NOP },
432 { .name = "DMB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 5,
433 .access = PL0_W, .type = ARM_CP_NOP },
434 { .name = "IFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 2,
435 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c6_insn),
436 .resetvalue = 0, },
437 /* Watchpoint Fault Address Register : should actually only be present
438 * for 1136, 1176, 11MPCore.
440 { .name = "WFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 1,
441 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0, },
442 { .name = "CPACR", .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 2,
443 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c1_coproc),
444 .resetvalue = 0, .writefn = cpacr_write },
445 REGINFO_SENTINEL
449 static int pmreg_read(CPUARMState *env, const ARMCPRegInfo *ri,
450 uint64_t *value)
452 /* Generic performance monitor register read function for where
453 * user access may be allowed by PMUSERENR.
455 if (arm_current_pl(env) == 0 && !env->cp15.c9_pmuserenr) {
456 return EXCP_UDEF;
458 *value = CPREG_FIELD32(env, ri);
459 return 0;
462 static int pmcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
463 uint64_t value)
465 if (arm_current_pl(env) == 0 && !env->cp15.c9_pmuserenr) {
466 return EXCP_UDEF;
468 /* only the DP, X, D and E bits are writable */
469 env->cp15.c9_pmcr &= ~0x39;
470 env->cp15.c9_pmcr |= (value & 0x39);
471 return 0;
474 static int pmcntenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
475 uint64_t value)
477 if (arm_current_pl(env) == 0 && !env->cp15.c9_pmuserenr) {
478 return EXCP_UDEF;
480 value &= (1 << 31);
481 env->cp15.c9_pmcnten |= value;
482 return 0;
485 static int pmcntenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
486 uint64_t value)
488 if (arm_current_pl(env) == 0 && !env->cp15.c9_pmuserenr) {
489 return EXCP_UDEF;
491 value &= (1 << 31);
492 env->cp15.c9_pmcnten &= ~value;
493 return 0;
496 static int pmovsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
497 uint64_t value)
499 if (arm_current_pl(env) == 0 && !env->cp15.c9_pmuserenr) {
500 return EXCP_UDEF;
502 env->cp15.c9_pmovsr &= ~value;
503 return 0;
506 static int pmxevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
507 uint64_t value)
509 if (arm_current_pl(env) == 0 && !env->cp15.c9_pmuserenr) {
510 return EXCP_UDEF;
512 env->cp15.c9_pmxevtyper = value & 0xff;
513 return 0;
516 static int pmuserenr_write(CPUARMState *env, const ARMCPRegInfo *ri,
517 uint64_t value)
519 env->cp15.c9_pmuserenr = value & 1;
520 return 0;
523 static int pmintenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
524 uint64_t value)
526 /* We have no event counters so only the C bit can be changed */
527 value &= (1 << 31);
528 env->cp15.c9_pminten |= value;
529 return 0;
532 static int pmintenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
533 uint64_t value)
535 value &= (1 << 31);
536 env->cp15.c9_pminten &= ~value;
537 return 0;
540 static int ccsidr_read(CPUARMState *env, const ARMCPRegInfo *ri,
541 uint64_t *value)
543 ARMCPU *cpu = arm_env_get_cpu(env);
544 *value = cpu->ccsidr[env->cp15.c0_cssel];
545 return 0;
548 static int csselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
549 uint64_t value)
551 env->cp15.c0_cssel = value & 0xf;
552 return 0;
555 static const ARMCPRegInfo v7_cp_reginfo[] = {
556 /* DBGDRAR, DBGDSAR: always RAZ since we don't implement memory mapped
557 * debug components
559 { .name = "DBGDRAR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 0,
560 .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
561 { .name = "DBGDSAR", .cp = 14, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
562 .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
563 /* the old v6 WFI, UNPREDICTABLE in v7 but we choose to NOP */
564 { .name = "NOP", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
565 .access = PL1_W, .type = ARM_CP_NOP },
566 /* Performance monitors are implementation defined in v7,
567 * but with an ARM recommended set of registers, which we
568 * follow (although we don't actually implement any counters)
570 * Performance registers fall into three categories:
571 * (a) always UNDEF in PL0, RW in PL1 (PMINTENSET, PMINTENCLR)
572 * (b) RO in PL0 (ie UNDEF on write), RW in PL1 (PMUSERENR)
573 * (c) UNDEF in PL0 if PMUSERENR.EN==0, otherwise accessible (all others)
574 * For the cases controlled by PMUSERENR we must set .access to PL0_RW
575 * or PL0_RO as appropriate and then check PMUSERENR in the helper fn.
577 { .name = "PMCNTENSET", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 1,
578 .access = PL0_RW, .resetvalue = 0,
579 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten),
580 .readfn = pmreg_read, .writefn = pmcntenset_write,
581 .raw_readfn = raw_read, .raw_writefn = raw_write },
582 { .name = "PMCNTENCLR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 2,
583 .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten),
584 .readfn = pmreg_read, .writefn = pmcntenclr_write,
585 .type = ARM_CP_NO_MIGRATE },
586 { .name = "PMOVSR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 3,
587 .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
588 .readfn = pmreg_read, .writefn = pmovsr_write,
589 .raw_readfn = raw_read, .raw_writefn = raw_write },
590 /* Unimplemented so WI. Strictly speaking write accesses in PL0 should
591 * respect PMUSERENR.
593 { .name = "PMSWINC", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 4,
594 .access = PL0_W, .type = ARM_CP_NOP },
595 /* Since we don't implement any events, writing to PMSELR is UNPREDICTABLE.
596 * We choose to RAZ/WI. XXX should respect PMUSERENR.
598 { .name = "PMSELR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 5,
599 .access = PL0_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
600 /* Unimplemented, RAZ/WI. XXX PMUSERENR */
601 { .name = "PMCCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 0,
602 .access = PL0_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
603 { .name = "PMXEVTYPER", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 1,
604 .access = PL0_RW,
605 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmxevtyper),
606 .readfn = pmreg_read, .writefn = pmxevtyper_write,
607 .raw_readfn = raw_read, .raw_writefn = raw_write },
608 /* Unimplemented, RAZ/WI. XXX PMUSERENR */
609 { .name = "PMXEVCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 2,
610 .access = PL0_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
611 { .name = "PMUSERENR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 0,
612 .access = PL0_R | PL1_RW,
613 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmuserenr),
614 .resetvalue = 0,
615 .writefn = pmuserenr_write, .raw_writefn = raw_write },
616 { .name = "PMINTENSET", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 1,
617 .access = PL1_RW,
618 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
619 .resetvalue = 0,
620 .writefn = pmintenset_write, .raw_writefn = raw_write },
621 { .name = "PMINTENCLR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 2,
622 .access = PL1_RW, .type = ARM_CP_NO_MIGRATE,
623 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
624 .resetvalue = 0, .writefn = pmintenclr_write, },
625 { .name = "SCR", .cp = 15, .crn = 1, .crm = 1, .opc1 = 0, .opc2 = 0,
626 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c1_scr),
627 .resetvalue = 0, },
628 { .name = "CCSIDR", .cp = 15, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 0,
629 .access = PL1_R, .readfn = ccsidr_read, .type = ARM_CP_NO_MIGRATE },
630 { .name = "CSSELR", .cp = 15, .crn = 0, .crm = 0, .opc1 = 2, .opc2 = 0,
631 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c0_cssel),
632 .writefn = csselr_write, .resetvalue = 0 },
633 /* Auxiliary ID register: this actually has an IMPDEF value but for now
634 * just RAZ for all cores:
636 { .name = "AIDR", .cp = 15, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 7,
637 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
638 REGINFO_SENTINEL
641 static int teecr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
643 value &= 1;
644 env->teecr = value;
645 return 0;
648 static int teehbr_read(CPUARMState *env, const ARMCPRegInfo *ri,
649 uint64_t *value)
651 /* This is a helper function because the user access rights
652 * depend on the value of the TEECR.
654 if (arm_current_pl(env) == 0 && (env->teecr & 1)) {
655 return EXCP_UDEF;
657 *value = env->teehbr;
658 return 0;
661 static int teehbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
662 uint64_t value)
664 if (arm_current_pl(env) == 0 && (env->teecr & 1)) {
665 return EXCP_UDEF;
667 env->teehbr = value;
668 return 0;
671 static const ARMCPRegInfo t2ee_cp_reginfo[] = {
672 { .name = "TEECR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 6, .opc2 = 0,
673 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, teecr),
674 .resetvalue = 0,
675 .writefn = teecr_write },
676 { .name = "TEEHBR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 6, .opc2 = 0,
677 .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, teehbr),
678 .resetvalue = 0, .raw_readfn = raw_read, .raw_writefn = raw_write,
679 .readfn = teehbr_read, .writefn = teehbr_write },
680 REGINFO_SENTINEL
683 static const ARMCPRegInfo v6k_cp_reginfo[] = {
684 { .name = "TPIDRURW", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 2,
685 .access = PL0_RW,
686 .fieldoffset = offsetof(CPUARMState, cp15.c13_tls1),
687 .resetvalue = 0 },
688 { .name = "TPIDRURO", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 3,
689 .access = PL0_R|PL1_W,
690 .fieldoffset = offsetof(CPUARMState, cp15.c13_tls2),
691 .resetvalue = 0 },
692 { .name = "TPIDRPRW", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 4,
693 .access = PL1_RW,
694 .fieldoffset = offsetof(CPUARMState, cp15.c13_tls3),
695 .resetvalue = 0 },
696 REGINFO_SENTINEL
699 #ifndef CONFIG_USER_ONLY
701 static uint64_t gt_get_countervalue(CPUARMState *env)
703 return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) / GTIMER_SCALE;
706 static void gt_recalc_timer(ARMCPU *cpu, int timeridx)
708 ARMGenericTimer *gt = &cpu->env.cp15.c14_timer[timeridx];
710 if (gt->ctl & 1) {
711 /* Timer enabled: calculate and set current ISTATUS, irq, and
712 * reset timer to when ISTATUS next has to change
714 uint64_t count = gt_get_countervalue(&cpu->env);
715 /* Note that this must be unsigned 64 bit arithmetic: */
716 int istatus = count >= gt->cval;
717 uint64_t nexttick;
719 gt->ctl = deposit32(gt->ctl, 2, 1, istatus);
720 qemu_set_irq(cpu->gt_timer_outputs[timeridx],
721 (istatus && !(gt->ctl & 2)));
722 if (istatus) {
723 /* Next transition is when count rolls back over to zero */
724 nexttick = UINT64_MAX;
725 } else {
726 /* Next transition is when we hit cval */
727 nexttick = gt->cval;
729 /* Note that the desired next expiry time might be beyond the
730 * signed-64-bit range of a QEMUTimer -- in this case we just
731 * set the timer for as far in the future as possible. When the
732 * timer expires we will reset the timer for any remaining period.
734 if (nexttick > INT64_MAX / GTIMER_SCALE) {
735 nexttick = INT64_MAX / GTIMER_SCALE;
737 timer_mod(cpu->gt_timer[timeridx], nexttick);
738 } else {
739 /* Timer disabled: ISTATUS and timer output always clear */
740 gt->ctl &= ~4;
741 qemu_set_irq(cpu->gt_timer_outputs[timeridx], 0);
742 timer_del(cpu->gt_timer[timeridx]);
746 static int gt_cntfrq_read(CPUARMState *env, const ARMCPRegInfo *ri,
747 uint64_t *value)
749 /* Not visible from PL0 if both PL0PCTEN and PL0VCTEN are zero */
750 if (arm_current_pl(env) == 0 && !extract32(env->cp15.c14_cntkctl, 0, 2)) {
751 return EXCP_UDEF;
753 *value = env->cp15.c14_cntfrq;
754 return 0;
757 static void gt_cnt_reset(CPUARMState *env, const ARMCPRegInfo *ri)
759 ARMCPU *cpu = arm_env_get_cpu(env);
760 int timeridx = ri->opc1 & 1;
762 timer_del(cpu->gt_timer[timeridx]);
765 static int gt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri,
766 uint64_t *value)
768 int timeridx = ri->opc1 & 1;
770 if (arm_current_pl(env) == 0 &&
771 !extract32(env->cp15.c14_cntkctl, timeridx, 1)) {
772 return EXCP_UDEF;
774 *value = gt_get_countervalue(env);
775 return 0;
778 static int gt_cval_read(CPUARMState *env, const ARMCPRegInfo *ri,
779 uint64_t *value)
781 int timeridx = ri->opc1 & 1;
783 if (arm_current_pl(env) == 0 &&
784 !extract32(env->cp15.c14_cntkctl, 9 - timeridx, 1)) {
785 return EXCP_UDEF;
787 *value = env->cp15.c14_timer[timeridx].cval;
788 return 0;
791 static int gt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
792 uint64_t value)
794 int timeridx = ri->opc1 & 1;
796 env->cp15.c14_timer[timeridx].cval = value;
797 gt_recalc_timer(arm_env_get_cpu(env), timeridx);
798 return 0;
800 static int gt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri,
801 uint64_t *value)
803 int timeridx = ri->crm & 1;
805 if (arm_current_pl(env) == 0 &&
806 !extract32(env->cp15.c14_cntkctl, 9 - timeridx, 1)) {
807 return EXCP_UDEF;
809 *value = (uint32_t)(env->cp15.c14_timer[timeridx].cval -
810 gt_get_countervalue(env));
811 return 0;
814 static int gt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
815 uint64_t value)
817 int timeridx = ri->crm & 1;
819 env->cp15.c14_timer[timeridx].cval = gt_get_countervalue(env) +
820 + sextract64(value, 0, 32);
821 gt_recalc_timer(arm_env_get_cpu(env), timeridx);
822 return 0;
825 static int gt_ctl_read(CPUARMState *env, const ARMCPRegInfo *ri,
826 uint64_t *value)
828 int timeridx = ri->crm & 1;
830 if (arm_current_pl(env) == 0 &&
831 !extract32(env->cp15.c14_cntkctl, 9 - timeridx, 1)) {
832 return EXCP_UDEF;
834 *value = env->cp15.c14_timer[timeridx].ctl;
835 return 0;
838 static int gt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
839 uint64_t value)
841 ARMCPU *cpu = arm_env_get_cpu(env);
842 int timeridx = ri->crm & 1;
843 uint32_t oldval = env->cp15.c14_timer[timeridx].ctl;
845 env->cp15.c14_timer[timeridx].ctl = value & 3;
846 if ((oldval ^ value) & 1) {
847 /* Enable toggled */
848 gt_recalc_timer(cpu, timeridx);
849 } else if ((oldval & value) & 2) {
850 /* IMASK toggled: don't need to recalculate,
851 * just set the interrupt line based on ISTATUS
853 qemu_set_irq(cpu->gt_timer_outputs[timeridx],
854 (oldval & 4) && (value & 2));
856 return 0;
859 void arm_gt_ptimer_cb(void *opaque)
861 ARMCPU *cpu = opaque;
863 gt_recalc_timer(cpu, GTIMER_PHYS);
866 void arm_gt_vtimer_cb(void *opaque)
868 ARMCPU *cpu = opaque;
870 gt_recalc_timer(cpu, GTIMER_VIRT);
873 static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
874 /* Note that CNTFRQ is purely reads-as-written for the benefit
875 * of software; writing it doesn't actually change the timer frequency.
876 * Our reset value matches the fixed frequency we implement the timer at.
878 { .name = "CNTFRQ", .cp = 15, .crn = 14, .crm = 0, .opc1 = 0, .opc2 = 0,
879 .access = PL1_RW | PL0_R,
880 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
881 .resetvalue = (1000 * 1000 * 1000) / GTIMER_SCALE,
882 .readfn = gt_cntfrq_read, .raw_readfn = raw_read,
884 /* overall control: mostly access permissions */
885 { .name = "CNTKCTL", .cp = 15, .crn = 14, .crm = 1, .opc1 = 0, .opc2 = 0,
886 .access = PL1_RW,
887 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntkctl),
888 .resetvalue = 0,
890 /* per-timer control */
891 { .name = "CNTP_CTL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
892 .type = ARM_CP_IO, .access = PL1_RW | PL0_R,
893 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
894 .resetvalue = 0,
895 .readfn = gt_ctl_read, .writefn = gt_ctl_write,
896 .raw_readfn = raw_read, .raw_writefn = raw_write,
898 { .name = "CNTV_CTL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 1,
899 .type = ARM_CP_IO, .access = PL1_RW | PL0_R,
900 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
901 .resetvalue = 0,
902 .readfn = gt_ctl_read, .writefn = gt_ctl_write,
903 .raw_readfn = raw_read, .raw_writefn = raw_write,
905 /* TimerValue views: a 32 bit downcounting view of the underlying state */
906 { .name = "CNTP_TVAL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
907 .type = ARM_CP_NO_MIGRATE | ARM_CP_IO, .access = PL1_RW | PL0_R,
908 .readfn = gt_tval_read, .writefn = gt_tval_write,
910 { .name = "CNTV_TVAL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 0,
911 .type = ARM_CP_NO_MIGRATE | ARM_CP_IO, .access = PL1_RW | PL0_R,
912 .readfn = gt_tval_read, .writefn = gt_tval_write,
914 /* The counter itself */
915 { .name = "CNTPCT", .cp = 15, .crm = 14, .opc1 = 0,
916 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_MIGRATE | ARM_CP_IO,
917 .readfn = gt_cnt_read, .resetfn = gt_cnt_reset,
919 { .name = "CNTVCT", .cp = 15, .crm = 14, .opc1 = 1,
920 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_MIGRATE | ARM_CP_IO,
921 .readfn = gt_cnt_read, .resetfn = gt_cnt_reset,
923 /* Comparison value, indicating when the timer goes off */
924 { .name = "CNTP_CVAL", .cp = 15, .crm = 14, .opc1 = 2,
925 .access = PL1_RW | PL0_R,
926 .type = ARM_CP_64BIT | ARM_CP_IO,
927 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
928 .resetvalue = 0,
929 .readfn = gt_cval_read, .writefn = gt_cval_write,
930 .raw_readfn = raw_read, .raw_writefn = raw_write,
932 { .name = "CNTV_CVAL", .cp = 15, .crm = 14, .opc1 = 3,
933 .access = PL1_RW | PL0_R,
934 .type = ARM_CP_64BIT | ARM_CP_IO,
935 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
936 .resetvalue = 0,
937 .readfn = gt_cval_read, .writefn = gt_cval_write,
938 .raw_readfn = raw_read, .raw_writefn = raw_write,
940 REGINFO_SENTINEL
943 #else
944 /* In user-mode none of the generic timer registers are accessible,
945 * and their implementation depends on QEMU_CLOCK_VIRTUAL and qdev gpio outputs,
946 * so instead just don't register any of them.
948 static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
949 REGINFO_SENTINEL
952 #endif
954 static int par_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
956 if (arm_feature(env, ARM_FEATURE_LPAE)) {
957 env->cp15.c7_par = value;
958 } else if (arm_feature(env, ARM_FEATURE_V7)) {
959 env->cp15.c7_par = value & 0xfffff6ff;
960 } else {
961 env->cp15.c7_par = value & 0xfffff1ff;
963 return 0;
966 #ifndef CONFIG_USER_ONLY
967 /* get_phys_addr() isn't present for user-mode-only targets */
969 /* Return true if extended addresses are enabled, ie this is an
970 * LPAE implementation and we are using the long-descriptor translation
971 * table format because the TTBCR EAE bit is set.
973 static inline bool extended_addresses_enabled(CPUARMState *env)
975 return arm_feature(env, ARM_FEATURE_LPAE)
976 && (env->cp15.c2_control & (1U << 31));
979 static int ats_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
981 hwaddr phys_addr;
982 target_ulong page_size;
983 int prot;
984 int ret, is_user = ri->opc2 & 2;
985 int access_type = ri->opc2 & 1;
987 if (ri->opc2 & 4) {
988 /* Other states are only available with TrustZone */
989 return EXCP_UDEF;
991 ret = get_phys_addr(env, value, access_type, is_user,
992 &phys_addr, &prot, &page_size);
993 if (extended_addresses_enabled(env)) {
994 /* ret is a DFSR/IFSR value for the long descriptor
995 * translation table format, but with WnR always clear.
996 * Convert it to a 64-bit PAR.
998 uint64_t par64 = (1 << 11); /* LPAE bit always set */
999 if (ret == 0) {
1000 par64 |= phys_addr & ~0xfffULL;
1001 /* We don't set the ATTR or SH fields in the PAR. */
1002 } else {
1003 par64 |= 1; /* F */
1004 par64 |= (ret & 0x3f) << 1; /* FS */
1005 /* Note that S2WLK and FSTAGE are always zero, because we don't
1006 * implement virtualization and therefore there can't be a stage 2
1007 * fault.
1010 env->cp15.c7_par = par64;
1011 env->cp15.c7_par_hi = par64 >> 32;
1012 } else {
1013 /* ret is a DFSR/IFSR value for the short descriptor
1014 * translation table format (with WnR always clear).
1015 * Convert it to a 32-bit PAR.
1017 if (ret == 0) {
1018 /* We do not set any attribute bits in the PAR */
1019 if (page_size == (1 << 24)
1020 && arm_feature(env, ARM_FEATURE_V7)) {
1021 env->cp15.c7_par = (phys_addr & 0xff000000) | 1 << 1;
1022 } else {
1023 env->cp15.c7_par = phys_addr & 0xfffff000;
1025 } else {
1026 env->cp15.c7_par = ((ret & (10 << 1)) >> 5) |
1027 ((ret & (12 << 1)) >> 6) |
1028 ((ret & 0xf) << 1) | 1;
1030 env->cp15.c7_par_hi = 0;
1032 return 0;
1034 #endif
1036 static const ARMCPRegInfo vapa_cp_reginfo[] = {
1037 { .name = "PAR", .cp = 15, .crn = 7, .crm = 4, .opc1 = 0, .opc2 = 0,
1038 .access = PL1_RW, .resetvalue = 0,
1039 .fieldoffset = offsetof(CPUARMState, cp15.c7_par),
1040 .writefn = par_write },
1041 #ifndef CONFIG_USER_ONLY
1042 { .name = "ATS", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = CP_ANY,
1043 .access = PL1_W, .writefn = ats_write, .type = ARM_CP_NO_MIGRATE },
1044 #endif
1045 REGINFO_SENTINEL
1048 /* Return basic MPU access permission bits. */
1049 static uint32_t simple_mpu_ap_bits(uint32_t val)
1051 uint32_t ret;
1052 uint32_t mask;
1053 int i;
1054 ret = 0;
1055 mask = 3;
1056 for (i = 0; i < 16; i += 2) {
1057 ret |= (val >> i) & mask;
1058 mask <<= 2;
1060 return ret;
1063 /* Pad basic MPU access permission bits to extended format. */
1064 static uint32_t extended_mpu_ap_bits(uint32_t val)
1066 uint32_t ret;
1067 uint32_t mask;
1068 int i;
1069 ret = 0;
1070 mask = 3;
1071 for (i = 0; i < 16; i += 2) {
1072 ret |= (val & mask) << i;
1073 mask <<= 2;
1075 return ret;
1078 static int pmsav5_data_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
1079 uint64_t value)
1081 env->cp15.c5_data = extended_mpu_ap_bits(value);
1082 return 0;
1085 static int pmsav5_data_ap_read(CPUARMState *env, const ARMCPRegInfo *ri,
1086 uint64_t *value)
1088 *value = simple_mpu_ap_bits(env->cp15.c5_data);
1089 return 0;
1092 static int pmsav5_insn_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
1093 uint64_t value)
1095 env->cp15.c5_insn = extended_mpu_ap_bits(value);
1096 return 0;
1099 static int pmsav5_insn_ap_read(CPUARMState *env, const ARMCPRegInfo *ri,
1100 uint64_t *value)
1102 *value = simple_mpu_ap_bits(env->cp15.c5_insn);
1103 return 0;
1106 static int arm946_prbs_read(CPUARMState *env, const ARMCPRegInfo *ri,
1107 uint64_t *value)
1109 if (ri->crm >= 8) {
1110 return EXCP_UDEF;
1112 *value = env->cp15.c6_region[ri->crm];
1113 return 0;
1116 static int arm946_prbs_write(CPUARMState *env, const ARMCPRegInfo *ri,
1117 uint64_t value)
1119 if (ri->crm >= 8) {
1120 return EXCP_UDEF;
1122 env->cp15.c6_region[ri->crm] = value;
1123 return 0;
1126 static const ARMCPRegInfo pmsav5_cp_reginfo[] = {
1127 { .name = "DATA_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
1128 .access = PL1_RW, .type = ARM_CP_NO_MIGRATE,
1129 .fieldoffset = offsetof(CPUARMState, cp15.c5_data), .resetvalue = 0,
1130 .readfn = pmsav5_data_ap_read, .writefn = pmsav5_data_ap_write, },
1131 { .name = "INSN_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
1132 .access = PL1_RW, .type = ARM_CP_NO_MIGRATE,
1133 .fieldoffset = offsetof(CPUARMState, cp15.c5_insn), .resetvalue = 0,
1134 .readfn = pmsav5_insn_ap_read, .writefn = pmsav5_insn_ap_write, },
1135 { .name = "DATA_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 2,
1136 .access = PL1_RW,
1137 .fieldoffset = offsetof(CPUARMState, cp15.c5_data), .resetvalue = 0, },
1138 { .name = "INSN_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 3,
1139 .access = PL1_RW,
1140 .fieldoffset = offsetof(CPUARMState, cp15.c5_insn), .resetvalue = 0, },
1141 { .name = "DCACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
1142 .access = PL1_RW,
1143 .fieldoffset = offsetof(CPUARMState, cp15.c2_data), .resetvalue = 0, },
1144 { .name = "ICACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 1,
1145 .access = PL1_RW,
1146 .fieldoffset = offsetof(CPUARMState, cp15.c2_insn), .resetvalue = 0, },
1147 /* Protection region base and size registers */
1148 { .name = "946_PRBS", .cp = 15, .crn = 6, .crm = CP_ANY, .opc1 = 0,
1149 .opc2 = CP_ANY, .access = PL1_RW,
1150 .readfn = arm946_prbs_read, .writefn = arm946_prbs_write, },
1151 REGINFO_SENTINEL
1154 static int vmsa_ttbcr_raw_write(CPUARMState *env, const ARMCPRegInfo *ri,
1155 uint64_t value)
1157 int maskshift = extract32(value, 0, 3);
1159 if (arm_feature(env, ARM_FEATURE_LPAE)) {
1160 value &= ~((7 << 19) | (3 << 14) | (0xf << 3));
1161 } else {
1162 value &= 7;
1164 /* Note that we always calculate c2_mask and c2_base_mask, but
1165 * they are only used for short-descriptor tables (ie if EAE is 0);
1166 * for long-descriptor tables the TTBCR fields are used differently
1167 * and the c2_mask and c2_base_mask values are meaningless.
1169 env->cp15.c2_control = value;
1170 env->cp15.c2_mask = ~(((uint32_t)0xffffffffu) >> maskshift);
1171 env->cp15.c2_base_mask = ~((uint32_t)0x3fffu >> maskshift);
1172 return 0;
1175 static int vmsa_ttbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1176 uint64_t value)
1178 if (arm_feature(env, ARM_FEATURE_LPAE)) {
1179 /* With LPAE the TTBCR could result in a change of ASID
1180 * via the TTBCR.A1 bit, so do a TLB flush.
1182 tlb_flush(env, 1);
1184 return vmsa_ttbcr_raw_write(env, ri, value);
1187 static void vmsa_ttbcr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1189 env->cp15.c2_base_mask = 0xffffc000u;
1190 env->cp15.c2_control = 0;
1191 env->cp15.c2_mask = 0;
1194 static const ARMCPRegInfo vmsa_cp_reginfo[] = {
1195 { .name = "DFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
1196 .access = PL1_RW,
1197 .fieldoffset = offsetof(CPUARMState, cp15.c5_data), .resetvalue = 0, },
1198 { .name = "IFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
1199 .access = PL1_RW,
1200 .fieldoffset = offsetof(CPUARMState, cp15.c5_insn), .resetvalue = 0, },
1201 { .name = "TTBR0", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
1202 .access = PL1_RW,
1203 .fieldoffset = offsetof(CPUARMState, cp15.c2_base0), .resetvalue = 0, },
1204 { .name = "TTBR1", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 1,
1205 .access = PL1_RW,
1206 .fieldoffset = offsetof(CPUARMState, cp15.c2_base1), .resetvalue = 0, },
1207 { .name = "TTBCR", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
1208 .access = PL1_RW, .writefn = vmsa_ttbcr_write,
1209 .resetfn = vmsa_ttbcr_reset, .raw_writefn = vmsa_ttbcr_raw_write,
1210 .fieldoffset = offsetof(CPUARMState, cp15.c2_control) },
1211 { .name = "DFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 0,
1212 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c6_data),
1213 .resetvalue = 0, },
1214 REGINFO_SENTINEL
1217 static int omap_ticonfig_write(CPUARMState *env, const ARMCPRegInfo *ri,
1218 uint64_t value)
1220 env->cp15.c15_ticonfig = value & 0xe7;
1221 /* The OS_TYPE bit in this register changes the reported CPUID! */
1222 env->cp15.c0_cpuid = (value & (1 << 5)) ?
1223 ARM_CPUID_TI915T : ARM_CPUID_TI925T;
1224 return 0;
1227 static int omap_threadid_write(CPUARMState *env, const ARMCPRegInfo *ri,
1228 uint64_t value)
1230 env->cp15.c15_threadid = value & 0xffff;
1231 return 0;
1234 static int omap_wfi_write(CPUARMState *env, const ARMCPRegInfo *ri,
1235 uint64_t value)
1237 /* Wait-for-interrupt (deprecated) */
1238 cpu_interrupt(CPU(arm_env_get_cpu(env)), CPU_INTERRUPT_HALT);
1239 return 0;
1242 static int omap_cachemaint_write(CPUARMState *env, const ARMCPRegInfo *ri,
1243 uint64_t value)
1245 /* On OMAP there are registers indicating the max/min index of dcache lines
1246 * containing a dirty line; cache flush operations have to reset these.
1248 env->cp15.c15_i_max = 0x000;
1249 env->cp15.c15_i_min = 0xff0;
1250 return 0;
1253 static const ARMCPRegInfo omap_cp_reginfo[] = {
1254 { .name = "DFSR", .cp = 15, .crn = 5, .crm = CP_ANY,
1255 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_OVERRIDE,
1256 .fieldoffset = offsetof(CPUARMState, cp15.c5_data), .resetvalue = 0, },
1257 { .name = "", .cp = 15, .crn = 15, .crm = 0, .opc1 = 0, .opc2 = 0,
1258 .access = PL1_RW, .type = ARM_CP_NOP },
1259 { .name = "TICONFIG", .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0,
1260 .access = PL1_RW,
1261 .fieldoffset = offsetof(CPUARMState, cp15.c15_ticonfig), .resetvalue = 0,
1262 .writefn = omap_ticonfig_write },
1263 { .name = "IMAX", .cp = 15, .crn = 15, .crm = 2, .opc1 = 0, .opc2 = 0,
1264 .access = PL1_RW,
1265 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_max), .resetvalue = 0, },
1266 { .name = "IMIN", .cp = 15, .crn = 15, .crm = 3, .opc1 = 0, .opc2 = 0,
1267 .access = PL1_RW, .resetvalue = 0xff0,
1268 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_min) },
1269 { .name = "THREADID", .cp = 15, .crn = 15, .crm = 4, .opc1 = 0, .opc2 = 0,
1270 .access = PL1_RW,
1271 .fieldoffset = offsetof(CPUARMState, cp15.c15_threadid), .resetvalue = 0,
1272 .writefn = omap_threadid_write },
1273 { .name = "TI925T_STATUS", .cp = 15, .crn = 15,
1274 .crm = 8, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
1275 .type = ARM_CP_NO_MIGRATE,
1276 .readfn = arm_cp_read_zero, .writefn = omap_wfi_write, },
1277 /* TODO: Peripheral port remap register:
1278 * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt controller
1279 * base address at $rn & ~0xfff and map size of 0x200 << ($rn & 0xfff),
1280 * when MMU is off.
1282 { .name = "OMAP_CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
1283 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
1284 .type = ARM_CP_OVERRIDE | ARM_CP_NO_MIGRATE,
1285 .writefn = omap_cachemaint_write },
1286 { .name = "C9", .cp = 15, .crn = 9,
1287 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW,
1288 .type = ARM_CP_CONST | ARM_CP_OVERRIDE, .resetvalue = 0 },
1289 REGINFO_SENTINEL
1292 static int xscale_cpar_write(CPUARMState *env, const ARMCPRegInfo *ri,
1293 uint64_t value)
1295 value &= 0x3fff;
1296 if (env->cp15.c15_cpar != value) {
1297 /* Changes cp0 to cp13 behavior, so needs a TB flush. */
1298 tb_flush(env);
1299 env->cp15.c15_cpar = value;
1301 return 0;
1304 static const ARMCPRegInfo xscale_cp_reginfo[] = {
1305 { .name = "XSCALE_CPAR",
1306 .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
1307 .fieldoffset = offsetof(CPUARMState, cp15.c15_cpar), .resetvalue = 0,
1308 .writefn = xscale_cpar_write, },
1309 { .name = "XSCALE_AUXCR",
1310 .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 1, .access = PL1_RW,
1311 .fieldoffset = offsetof(CPUARMState, cp15.c1_xscaleauxcr),
1312 .resetvalue = 0, },
1313 REGINFO_SENTINEL
1316 static const ARMCPRegInfo dummy_c15_cp_reginfo[] = {
1317 /* RAZ/WI the whole crn=15 space, when we don't have a more specific
1318 * implementation of this implementation-defined space.
1319 * Ideally this should eventually disappear in favour of actually
1320 * implementing the correct behaviour for all cores.
1322 { .name = "C15_IMPDEF", .cp = 15, .crn = 15,
1323 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
1324 .access = PL1_RW, .type = ARM_CP_CONST | ARM_CP_NO_MIGRATE,
1325 .resetvalue = 0 },
1326 REGINFO_SENTINEL
1329 static const ARMCPRegInfo cache_dirty_status_cp_reginfo[] = {
1330 /* Cache status: RAZ because we have no cache so it's always clean */
1331 { .name = "CDSR", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 6,
1332 .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_MIGRATE,
1333 .resetvalue = 0 },
1334 REGINFO_SENTINEL
1337 static const ARMCPRegInfo cache_block_ops_cp_reginfo[] = {
1338 /* We never have a a block transfer operation in progress */
1339 { .name = "BXSR", .cp = 15, .crn = 7, .crm = 12, .opc1 = 0, .opc2 = 4,
1340 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_MIGRATE,
1341 .resetvalue = 0 },
1342 /* The cache ops themselves: these all NOP for QEMU */
1343 { .name = "IICR", .cp = 15, .crm = 5, .opc1 = 0,
1344 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
1345 { .name = "IDCR", .cp = 15, .crm = 6, .opc1 = 0,
1346 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
1347 { .name = "CDCR", .cp = 15, .crm = 12, .opc1 = 0,
1348 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
1349 { .name = "PIR", .cp = 15, .crm = 12, .opc1 = 1,
1350 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
1351 { .name = "PDR", .cp = 15, .crm = 12, .opc1 = 2,
1352 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
1353 { .name = "CIDCR", .cp = 15, .crm = 14, .opc1 = 0,
1354 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
1355 REGINFO_SENTINEL
1358 static const ARMCPRegInfo cache_test_clean_cp_reginfo[] = {
1359 /* The cache test-and-clean instructions always return (1 << 30)
1360 * to indicate that there are no dirty cache lines.
1362 { .name = "TC_DCACHE", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 3,
1363 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_MIGRATE,
1364 .resetvalue = (1 << 30) },
1365 { .name = "TCI_DCACHE", .cp = 15, .crn = 7, .crm = 14, .opc1 = 0, .opc2 = 3,
1366 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_MIGRATE,
1367 .resetvalue = (1 << 30) },
1368 REGINFO_SENTINEL
1371 static const ARMCPRegInfo strongarm_cp_reginfo[] = {
1372 /* Ignore ReadBuffer accesses */
1373 { .name = "C9_READBUFFER", .cp = 15, .crn = 9,
1374 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
1375 .access = PL1_RW, .resetvalue = 0,
1376 .type = ARM_CP_CONST | ARM_CP_OVERRIDE | ARM_CP_NO_MIGRATE },
1377 REGINFO_SENTINEL
1380 static int mpidr_read(CPUARMState *env, const ARMCPRegInfo *ri,
1381 uint64_t *value)
1383 CPUState *cs = CPU(arm_env_get_cpu(env));
1384 uint32_t mpidr = cs->cpu_index;
1385 /* We don't support setting cluster ID ([8..11])
1386 * so these bits always RAZ.
1388 if (arm_feature(env, ARM_FEATURE_V7MP)) {
1389 mpidr |= (1U << 31);
1390 /* Cores which are uniprocessor (non-coherent)
1391 * but still implement the MP extensions set
1392 * bit 30. (For instance, A9UP.) However we do
1393 * not currently model any of those cores.
1396 *value = mpidr;
1397 return 0;
1400 static const ARMCPRegInfo mpidr_cp_reginfo[] = {
1401 { .name = "MPIDR", .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 5,
1402 .access = PL1_R, .readfn = mpidr_read, .type = ARM_CP_NO_MIGRATE },
1403 REGINFO_SENTINEL
1406 static int par64_read(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t *value)
1408 *value = ((uint64_t)env->cp15.c7_par_hi << 32) | env->cp15.c7_par;
1409 return 0;
1412 static int par64_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
1414 env->cp15.c7_par_hi = value >> 32;
1415 env->cp15.c7_par = value;
1416 return 0;
1419 static void par64_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1421 env->cp15.c7_par_hi = 0;
1422 env->cp15.c7_par = 0;
1425 static int ttbr064_read(CPUARMState *env, const ARMCPRegInfo *ri,
1426 uint64_t *value)
1428 *value = ((uint64_t)env->cp15.c2_base0_hi << 32) | env->cp15.c2_base0;
1429 return 0;
1432 static int ttbr064_raw_write(CPUARMState *env, const ARMCPRegInfo *ri,
1433 uint64_t value)
1435 env->cp15.c2_base0_hi = value >> 32;
1436 env->cp15.c2_base0 = value;
1437 return 0;
1440 static int ttbr064_write(CPUARMState *env, const ARMCPRegInfo *ri,
1441 uint64_t value)
1443 /* Writes to the 64 bit format TTBRs may change the ASID */
1444 tlb_flush(env, 1);
1445 return ttbr064_raw_write(env, ri, value);
1448 static void ttbr064_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1450 env->cp15.c2_base0_hi = 0;
1451 env->cp15.c2_base0 = 0;
1454 static int ttbr164_read(CPUARMState *env, const ARMCPRegInfo *ri,
1455 uint64_t *value)
1457 *value = ((uint64_t)env->cp15.c2_base1_hi << 32) | env->cp15.c2_base1;
1458 return 0;
1461 static int ttbr164_write(CPUARMState *env, const ARMCPRegInfo *ri,
1462 uint64_t value)
1464 env->cp15.c2_base1_hi = value >> 32;
1465 env->cp15.c2_base1 = value;
1466 return 0;
1469 static void ttbr164_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1471 env->cp15.c2_base1_hi = 0;
1472 env->cp15.c2_base1 = 0;
1475 static const ARMCPRegInfo lpae_cp_reginfo[] = {
1476 /* NOP AMAIR0/1: the override is because these clash with the rather
1477 * broadly specified TLB_LOCKDOWN entry in the generic cp_reginfo.
1479 { .name = "AMAIR0", .cp = 15, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 0,
1480 .access = PL1_RW, .type = ARM_CP_CONST | ARM_CP_OVERRIDE,
1481 .resetvalue = 0 },
1482 { .name = "AMAIR1", .cp = 15, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 1,
1483 .access = PL1_RW, .type = ARM_CP_CONST | ARM_CP_OVERRIDE,
1484 .resetvalue = 0 },
1485 /* 64 bit access versions of the (dummy) debug registers */
1486 { .name = "DBGDRAR", .cp = 14, .crm = 1, .opc1 = 0,
1487 .access = PL0_R, .type = ARM_CP_CONST|ARM_CP_64BIT, .resetvalue = 0 },
1488 { .name = "DBGDSAR", .cp = 14, .crm = 2, .opc1 = 0,
1489 .access = PL0_R, .type = ARM_CP_CONST|ARM_CP_64BIT, .resetvalue = 0 },
1490 { .name = "PAR", .cp = 15, .crm = 7, .opc1 = 0,
1491 .access = PL1_RW, .type = ARM_CP_64BIT,
1492 .readfn = par64_read, .writefn = par64_write, .resetfn = par64_reset },
1493 { .name = "TTBR0", .cp = 15, .crm = 2, .opc1 = 0,
1494 .access = PL1_RW, .type = ARM_CP_64BIT, .readfn = ttbr064_read,
1495 .writefn = ttbr064_write, .raw_writefn = ttbr064_raw_write,
1496 .resetfn = ttbr064_reset },
1497 { .name = "TTBR1", .cp = 15, .crm = 2, .opc1 = 1,
1498 .access = PL1_RW, .type = ARM_CP_64BIT, .readfn = ttbr164_read,
1499 .writefn = ttbr164_write, .resetfn = ttbr164_reset },
1500 REGINFO_SENTINEL
1503 static int sctlr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
1505 env->cp15.c1_sys = value;
1506 /* ??? Lots of these bits are not implemented. */
1507 /* This may enable/disable the MMU, so do a TLB flush. */
1508 tlb_flush(env, 1);
1509 return 0;
1512 void register_cp_regs_for_features(ARMCPU *cpu)
1514 /* Register all the coprocessor registers based on feature bits */
1515 CPUARMState *env = &cpu->env;
1516 if (arm_feature(env, ARM_FEATURE_M)) {
1517 /* M profile has no coprocessor registers */
1518 return;
1521 define_arm_cp_regs(cpu, cp_reginfo);
1522 if (arm_feature(env, ARM_FEATURE_V6)) {
1523 /* The ID registers all have impdef reset values */
1524 ARMCPRegInfo v6_idregs[] = {
1525 { .name = "ID_PFR0", .cp = 15, .crn = 0, .crm = 1,
1526 .opc1 = 0, .opc2 = 0, .access = PL1_R, .type = ARM_CP_CONST,
1527 .resetvalue = cpu->id_pfr0 },
1528 { .name = "ID_PFR1", .cp = 15, .crn = 0, .crm = 1,
1529 .opc1 = 0, .opc2 = 1, .access = PL1_R, .type = ARM_CP_CONST,
1530 .resetvalue = cpu->id_pfr1 },
1531 { .name = "ID_DFR0", .cp = 15, .crn = 0, .crm = 1,
1532 .opc1 = 0, .opc2 = 2, .access = PL1_R, .type = ARM_CP_CONST,
1533 .resetvalue = cpu->id_dfr0 },
1534 { .name = "ID_AFR0", .cp = 15, .crn = 0, .crm = 1,
1535 .opc1 = 0, .opc2 = 3, .access = PL1_R, .type = ARM_CP_CONST,
1536 .resetvalue = cpu->id_afr0 },
1537 { .name = "ID_MMFR0", .cp = 15, .crn = 0, .crm = 1,
1538 .opc1 = 0, .opc2 = 4, .access = PL1_R, .type = ARM_CP_CONST,
1539 .resetvalue = cpu->id_mmfr0 },
1540 { .name = "ID_MMFR1", .cp = 15, .crn = 0, .crm = 1,
1541 .opc1 = 0, .opc2 = 5, .access = PL1_R, .type = ARM_CP_CONST,
1542 .resetvalue = cpu->id_mmfr1 },
1543 { .name = "ID_MMFR2", .cp = 15, .crn = 0, .crm = 1,
1544 .opc1 = 0, .opc2 = 6, .access = PL1_R, .type = ARM_CP_CONST,
1545 .resetvalue = cpu->id_mmfr2 },
1546 { .name = "ID_MMFR3", .cp = 15, .crn = 0, .crm = 1,
1547 .opc1 = 0, .opc2 = 7, .access = PL1_R, .type = ARM_CP_CONST,
1548 .resetvalue = cpu->id_mmfr3 },
1549 { .name = "ID_ISAR0", .cp = 15, .crn = 0, .crm = 2,
1550 .opc1 = 0, .opc2 = 0, .access = PL1_R, .type = ARM_CP_CONST,
1551 .resetvalue = cpu->id_isar0 },
1552 { .name = "ID_ISAR1", .cp = 15, .crn = 0, .crm = 2,
1553 .opc1 = 0, .opc2 = 1, .access = PL1_R, .type = ARM_CP_CONST,
1554 .resetvalue = cpu->id_isar1 },
1555 { .name = "ID_ISAR2", .cp = 15, .crn = 0, .crm = 2,
1556 .opc1 = 0, .opc2 = 2, .access = PL1_R, .type = ARM_CP_CONST,
1557 .resetvalue = cpu->id_isar2 },
1558 { .name = "ID_ISAR3", .cp = 15, .crn = 0, .crm = 2,
1559 .opc1 = 0, .opc2 = 3, .access = PL1_R, .type = ARM_CP_CONST,
1560 .resetvalue = cpu->id_isar3 },
1561 { .name = "ID_ISAR4", .cp = 15, .crn = 0, .crm = 2,
1562 .opc1 = 0, .opc2 = 4, .access = PL1_R, .type = ARM_CP_CONST,
1563 .resetvalue = cpu->id_isar4 },
1564 { .name = "ID_ISAR5", .cp = 15, .crn = 0, .crm = 2,
1565 .opc1 = 0, .opc2 = 5, .access = PL1_R, .type = ARM_CP_CONST,
1566 .resetvalue = cpu->id_isar5 },
1567 /* 6..7 are as yet unallocated and must RAZ */
1568 { .name = "ID_ISAR6", .cp = 15, .crn = 0, .crm = 2,
1569 .opc1 = 0, .opc2 = 6, .access = PL1_R, .type = ARM_CP_CONST,
1570 .resetvalue = 0 },
1571 { .name = "ID_ISAR7", .cp = 15, .crn = 0, .crm = 2,
1572 .opc1 = 0, .opc2 = 7, .access = PL1_R, .type = ARM_CP_CONST,
1573 .resetvalue = 0 },
1574 REGINFO_SENTINEL
1576 define_arm_cp_regs(cpu, v6_idregs);
1577 define_arm_cp_regs(cpu, v6_cp_reginfo);
1578 } else {
1579 define_arm_cp_regs(cpu, not_v6_cp_reginfo);
1581 if (arm_feature(env, ARM_FEATURE_V6K)) {
1582 define_arm_cp_regs(cpu, v6k_cp_reginfo);
1584 if (arm_feature(env, ARM_FEATURE_V7)) {
1585 /* v7 performance monitor control register: same implementor
1586 * field as main ID register, and we implement no event counters.
1588 ARMCPRegInfo pmcr = {
1589 .name = "PMCR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 0,
1590 .access = PL0_RW, .resetvalue = cpu->midr & 0xff000000,
1591 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcr),
1592 .readfn = pmreg_read, .writefn = pmcr_write,
1593 .raw_readfn = raw_read, .raw_writefn = raw_write,
1595 ARMCPRegInfo clidr = {
1596 .name = "CLIDR", .cp = 15, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 1,
1597 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->clidr
1599 define_one_arm_cp_reg(cpu, &pmcr);
1600 define_one_arm_cp_reg(cpu, &clidr);
1601 define_arm_cp_regs(cpu, v7_cp_reginfo);
1602 } else {
1603 define_arm_cp_regs(cpu, not_v7_cp_reginfo);
1605 if (arm_feature(env, ARM_FEATURE_MPU)) {
1606 /* These are the MPU registers prior to PMSAv6. Any new
1607 * PMSA core later than the ARM946 will require that we
1608 * implement the PMSAv6 or PMSAv7 registers, which are
1609 * completely different.
1611 assert(!arm_feature(env, ARM_FEATURE_V6));
1612 define_arm_cp_regs(cpu, pmsav5_cp_reginfo);
1613 } else {
1614 define_arm_cp_regs(cpu, vmsa_cp_reginfo);
1616 if (arm_feature(env, ARM_FEATURE_THUMB2EE)) {
1617 define_arm_cp_regs(cpu, t2ee_cp_reginfo);
1619 if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) {
1620 define_arm_cp_regs(cpu, generic_timer_cp_reginfo);
1622 if (arm_feature(env, ARM_FEATURE_VAPA)) {
1623 define_arm_cp_regs(cpu, vapa_cp_reginfo);
1625 if (arm_feature(env, ARM_FEATURE_CACHE_TEST_CLEAN)) {
1626 define_arm_cp_regs(cpu, cache_test_clean_cp_reginfo);
1628 if (arm_feature(env, ARM_FEATURE_CACHE_DIRTY_REG)) {
1629 define_arm_cp_regs(cpu, cache_dirty_status_cp_reginfo);
1631 if (arm_feature(env, ARM_FEATURE_CACHE_BLOCK_OPS)) {
1632 define_arm_cp_regs(cpu, cache_block_ops_cp_reginfo);
1634 if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
1635 define_arm_cp_regs(cpu, omap_cp_reginfo);
1637 if (arm_feature(env, ARM_FEATURE_STRONGARM)) {
1638 define_arm_cp_regs(cpu, strongarm_cp_reginfo);
1640 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
1641 define_arm_cp_regs(cpu, xscale_cp_reginfo);
1643 if (arm_feature(env, ARM_FEATURE_DUMMY_C15_REGS)) {
1644 define_arm_cp_regs(cpu, dummy_c15_cp_reginfo);
1646 if (arm_feature(env, ARM_FEATURE_LPAE)) {
1647 define_arm_cp_regs(cpu, lpae_cp_reginfo);
1649 /* Slightly awkwardly, the OMAP and StrongARM cores need all of
1650 * cp15 crn=0 to be writes-ignored, whereas for other cores they should
1651 * be read-only (ie write causes UNDEF exception).
1654 ARMCPRegInfo id_cp_reginfo[] = {
1655 /* Note that the MIDR isn't a simple constant register because
1656 * of the TI925 behaviour where writes to another register can
1657 * cause the MIDR value to change.
1659 * Unimplemented registers in the c15 0 0 0 space default to
1660 * MIDR. Define MIDR first as this entire space, then CTR, TCMTR
1661 * and friends override accordingly.
1663 { .name = "MIDR",
1664 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = CP_ANY,
1665 .access = PL1_R, .resetvalue = cpu->midr,
1666 .writefn = arm_cp_write_ignore, .raw_writefn = raw_write,
1667 .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
1668 .type = ARM_CP_OVERRIDE },
1669 { .name = "CTR",
1670 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 1,
1671 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
1672 { .name = "TCMTR",
1673 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 2,
1674 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
1675 { .name = "TLBTR",
1676 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 3,
1677 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
1678 /* crn = 0 op1 = 0 crm = 3..7 : currently unassigned; we RAZ. */
1679 { .name = "DUMMY",
1680 .cp = 15, .crn = 0, .crm = 3, .opc1 = 0, .opc2 = CP_ANY,
1681 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
1682 { .name = "DUMMY",
1683 .cp = 15, .crn = 0, .crm = 4, .opc1 = 0, .opc2 = CP_ANY,
1684 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
1685 { .name = "DUMMY",
1686 .cp = 15, .crn = 0, .crm = 5, .opc1 = 0, .opc2 = CP_ANY,
1687 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
1688 { .name = "DUMMY",
1689 .cp = 15, .crn = 0, .crm = 6, .opc1 = 0, .opc2 = CP_ANY,
1690 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
1691 { .name = "DUMMY",
1692 .cp = 15, .crn = 0, .crm = 7, .opc1 = 0, .opc2 = CP_ANY,
1693 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
1694 REGINFO_SENTINEL
1696 ARMCPRegInfo crn0_wi_reginfo = {
1697 .name = "CRN0_WI", .cp = 15, .crn = 0, .crm = CP_ANY,
1698 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_W,
1699 .type = ARM_CP_NOP | ARM_CP_OVERRIDE
1701 if (arm_feature(env, ARM_FEATURE_OMAPCP) ||
1702 arm_feature(env, ARM_FEATURE_STRONGARM)) {
1703 ARMCPRegInfo *r;
1704 /* Register the blanket "writes ignored" value first to cover the
1705 * whole space. Then update the specific ID registers to allow write
1706 * access, so that they ignore writes rather than causing them to
1707 * UNDEF.
1709 define_one_arm_cp_reg(cpu, &crn0_wi_reginfo);
1710 for (r = id_cp_reginfo; r->type != ARM_CP_SENTINEL; r++) {
1711 r->access = PL1_RW;
1714 define_arm_cp_regs(cpu, id_cp_reginfo);
1717 if (arm_feature(env, ARM_FEATURE_MPIDR)) {
1718 define_arm_cp_regs(cpu, mpidr_cp_reginfo);
1721 if (arm_feature(env, ARM_FEATURE_AUXCR)) {
1722 ARMCPRegInfo auxcr = {
1723 .name = "AUXCR", .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 1,
1724 .access = PL1_RW, .type = ARM_CP_CONST,
1725 .resetvalue = cpu->reset_auxcr
1727 define_one_arm_cp_reg(cpu, &auxcr);
1730 /* Generic registers whose values depend on the implementation */
1732 ARMCPRegInfo sctlr = {
1733 .name = "SCTLR", .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 0,
1734 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c1_sys),
1735 .writefn = sctlr_write, .resetvalue = cpu->reset_sctlr,
1736 .raw_writefn = raw_write,
1738 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
1739 /* Normally we would always end the TB on an SCTLR write, but Linux
1740 * arch/arm/mach-pxa/sleep.S expects two instructions following
1741 * an MMU enable to execute from cache. Imitate this behaviour.
1743 sctlr.type |= ARM_CP_SUPPRESS_TB_END;
1745 define_one_arm_cp_reg(cpu, &sctlr);
1749 ARMCPU *cpu_arm_init(const char *cpu_model)
1751 ARMCPU *cpu;
1752 CPUARMState *env;
1753 ObjectClass *oc;
1755 oc = cpu_class_by_name(TYPE_ARM_CPU, cpu_model);
1756 if (!oc) {
1757 return NULL;
1759 cpu = ARM_CPU(object_new(object_class_get_name(oc)));
1760 env = &cpu->env;
1761 env->cpu_model_str = cpu_model;
1763 /* TODO this should be set centrally, once possible */
1764 object_property_set_bool(OBJECT(cpu), true, "realized", NULL);
1766 return cpu;
1769 void arm_cpu_register_gdb_regs_for_features(ARMCPU *cpu)
1771 CPUState *cs = CPU(cpu);
1772 CPUARMState *env = &cpu->env;
1774 if (arm_feature(env, ARM_FEATURE_NEON)) {
1775 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
1776 51, "arm-neon.xml", 0);
1777 } else if (arm_feature(env, ARM_FEATURE_VFP3)) {
1778 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
1779 35, "arm-vfp3.xml", 0);
1780 } else if (arm_feature(env, ARM_FEATURE_VFP)) {
1781 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
1782 19, "arm-vfp.xml", 0);
1786 /* Sort alphabetically by type name, except for "any". */
1787 static gint arm_cpu_list_compare(gconstpointer a, gconstpointer b)
1789 ObjectClass *class_a = (ObjectClass *)a;
1790 ObjectClass *class_b = (ObjectClass *)b;
1791 const char *name_a, *name_b;
1793 name_a = object_class_get_name(class_a);
1794 name_b = object_class_get_name(class_b);
1795 if (strcmp(name_a, "any-" TYPE_ARM_CPU) == 0) {
1796 return 1;
1797 } else if (strcmp(name_b, "any-" TYPE_ARM_CPU) == 0) {
1798 return -1;
1799 } else {
1800 return strcmp(name_a, name_b);
1804 static void arm_cpu_list_entry(gpointer data, gpointer user_data)
1806 ObjectClass *oc = data;
1807 CPUListState *s = user_data;
1808 const char *typename;
1809 char *name;
1811 typename = object_class_get_name(oc);
1812 name = g_strndup(typename, strlen(typename) - strlen("-" TYPE_ARM_CPU));
1813 (*s->cpu_fprintf)(s->file, " %s\n",
1814 name);
1815 g_free(name);
1818 void arm_cpu_list(FILE *f, fprintf_function cpu_fprintf)
1820 CPUListState s = {
1821 .file = f,
1822 .cpu_fprintf = cpu_fprintf,
1824 GSList *list;
1826 list = object_class_get_list(TYPE_ARM_CPU, false);
1827 list = g_slist_sort(list, arm_cpu_list_compare);
1828 (*cpu_fprintf)(f, "Available CPUs:\n");
1829 g_slist_foreach(list, arm_cpu_list_entry, &s);
1830 g_slist_free(list);
1833 static void arm_cpu_add_definition(gpointer data, gpointer user_data)
1835 ObjectClass *oc = data;
1836 CpuDefinitionInfoList **cpu_list = user_data;
1837 CpuDefinitionInfoList *entry;
1838 CpuDefinitionInfo *info;
1839 const char *typename;
1841 typename = object_class_get_name(oc);
1842 info = g_malloc0(sizeof(*info));
1843 info->name = g_strndup(typename,
1844 strlen(typename) - strlen("-" TYPE_ARM_CPU));
1846 entry = g_malloc0(sizeof(*entry));
1847 entry->value = info;
1848 entry->next = *cpu_list;
1849 *cpu_list = entry;
1852 CpuDefinitionInfoList *arch_query_cpu_definitions(Error **errp)
1854 CpuDefinitionInfoList *cpu_list = NULL;
1855 GSList *list;
1857 list = object_class_get_list(TYPE_ARM_CPU, false);
1858 g_slist_foreach(list, arm_cpu_add_definition, &cpu_list);
1859 g_slist_free(list);
1861 return cpu_list;
1864 void define_one_arm_cp_reg_with_opaque(ARMCPU *cpu,
1865 const ARMCPRegInfo *r, void *opaque)
1867 /* Define implementations of coprocessor registers.
1868 * We store these in a hashtable because typically
1869 * there are less than 150 registers in a space which
1870 * is 16*16*16*8*8 = 262144 in size.
1871 * Wildcarding is supported for the crm, opc1 and opc2 fields.
1872 * If a register is defined twice then the second definition is
1873 * used, so this can be used to define some generic registers and
1874 * then override them with implementation specific variations.
1875 * At least one of the original and the second definition should
1876 * include ARM_CP_OVERRIDE in its type bits -- this is just a guard
1877 * against accidental use.
1879 int crm, opc1, opc2;
1880 int crmmin = (r->crm == CP_ANY) ? 0 : r->crm;
1881 int crmmax = (r->crm == CP_ANY) ? 15 : r->crm;
1882 int opc1min = (r->opc1 == CP_ANY) ? 0 : r->opc1;
1883 int opc1max = (r->opc1 == CP_ANY) ? 7 : r->opc1;
1884 int opc2min = (r->opc2 == CP_ANY) ? 0 : r->opc2;
1885 int opc2max = (r->opc2 == CP_ANY) ? 7 : r->opc2;
1886 /* 64 bit registers have only CRm and Opc1 fields */
1887 assert(!((r->type & ARM_CP_64BIT) && (r->opc2 || r->crn)));
1888 /* Check that the register definition has enough info to handle
1889 * reads and writes if they are permitted.
1891 if (!(r->type & (ARM_CP_SPECIAL|ARM_CP_CONST))) {
1892 if (r->access & PL3_R) {
1893 assert(r->fieldoffset || r->readfn);
1895 if (r->access & PL3_W) {
1896 assert(r->fieldoffset || r->writefn);
1899 /* Bad type field probably means missing sentinel at end of reg list */
1900 assert(cptype_valid(r->type));
1901 for (crm = crmmin; crm <= crmmax; crm++) {
1902 for (opc1 = opc1min; opc1 <= opc1max; opc1++) {
1903 for (opc2 = opc2min; opc2 <= opc2max; opc2++) {
1904 uint32_t *key = g_new(uint32_t, 1);
1905 ARMCPRegInfo *r2 = g_memdup(r, sizeof(ARMCPRegInfo));
1906 int is64 = (r->type & ARM_CP_64BIT) ? 1 : 0;
1907 *key = ENCODE_CP_REG(r->cp, is64, r->crn, crm, opc1, opc2);
1908 if (opaque) {
1909 r2->opaque = opaque;
1911 /* Make sure reginfo passed to helpers for wildcarded regs
1912 * has the correct crm/opc1/opc2 for this reg, not CP_ANY:
1914 r2->crm = crm;
1915 r2->opc1 = opc1;
1916 r2->opc2 = opc2;
1917 /* By convention, for wildcarded registers only the first
1918 * entry is used for migration; the others are marked as
1919 * NO_MIGRATE so we don't try to transfer the register
1920 * multiple times. Special registers (ie NOP/WFI) are
1921 * never migratable.
1923 if ((r->type & ARM_CP_SPECIAL) ||
1924 ((r->crm == CP_ANY) && crm != 0) ||
1925 ((r->opc1 == CP_ANY) && opc1 != 0) ||
1926 ((r->opc2 == CP_ANY) && opc2 != 0)) {
1927 r2->type |= ARM_CP_NO_MIGRATE;
1930 /* Overriding of an existing definition must be explicitly
1931 * requested.
1933 if (!(r->type & ARM_CP_OVERRIDE)) {
1934 ARMCPRegInfo *oldreg;
1935 oldreg = g_hash_table_lookup(cpu->cp_regs, key);
1936 if (oldreg && !(oldreg->type & ARM_CP_OVERRIDE)) {
1937 fprintf(stderr, "Register redefined: cp=%d %d bit "
1938 "crn=%d crm=%d opc1=%d opc2=%d, "
1939 "was %s, now %s\n", r2->cp, 32 + 32 * is64,
1940 r2->crn, r2->crm, r2->opc1, r2->opc2,
1941 oldreg->name, r2->name);
1942 g_assert_not_reached();
1945 g_hash_table_insert(cpu->cp_regs, key, r2);
1951 void define_arm_cp_regs_with_opaque(ARMCPU *cpu,
1952 const ARMCPRegInfo *regs, void *opaque)
1954 /* Define a whole list of registers */
1955 const ARMCPRegInfo *r;
1956 for (r = regs; r->type != ARM_CP_SENTINEL; r++) {
1957 define_one_arm_cp_reg_with_opaque(cpu, r, opaque);
1961 const ARMCPRegInfo *get_arm_cp_reginfo(ARMCPU *cpu, uint32_t encoded_cp)
1963 return g_hash_table_lookup(cpu->cp_regs, &encoded_cp);
1966 int arm_cp_write_ignore(CPUARMState *env, const ARMCPRegInfo *ri,
1967 uint64_t value)
1969 /* Helper coprocessor write function for write-ignore registers */
1970 return 0;
1973 int arm_cp_read_zero(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t *value)
1975 /* Helper coprocessor write function for read-as-zero registers */
1976 *value = 0;
1977 return 0;
1980 static int bad_mode_switch(CPUARMState *env, int mode)
1982 /* Return true if it is not valid for us to switch to
1983 * this CPU mode (ie all the UNPREDICTABLE cases in
1984 * the ARM ARM CPSRWriteByInstr pseudocode).
1986 switch (mode) {
1987 case ARM_CPU_MODE_USR:
1988 case ARM_CPU_MODE_SYS:
1989 case ARM_CPU_MODE_SVC:
1990 case ARM_CPU_MODE_ABT:
1991 case ARM_CPU_MODE_UND:
1992 case ARM_CPU_MODE_IRQ:
1993 case ARM_CPU_MODE_FIQ:
1994 return 0;
1995 default:
1996 return 1;
2000 uint32_t cpsr_read(CPUARMState *env)
2002 int ZF;
2003 ZF = (env->ZF == 0);
2004 return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) |
2005 (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27)
2006 | (env->thumb << 5) | ((env->condexec_bits & 3) << 25)
2007 | ((env->condexec_bits & 0xfc) << 8)
2008 | (env->GE << 16);
2011 void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask)
2013 if (mask & CPSR_NZCV) {
2014 env->ZF = (~val) & CPSR_Z;
2015 env->NF = val;
2016 env->CF = (val >> 29) & 1;
2017 env->VF = (val << 3) & 0x80000000;
2019 if (mask & CPSR_Q)
2020 env->QF = ((val & CPSR_Q) != 0);
2021 if (mask & CPSR_T)
2022 env->thumb = ((val & CPSR_T) != 0);
2023 if (mask & CPSR_IT_0_1) {
2024 env->condexec_bits &= ~3;
2025 env->condexec_bits |= (val >> 25) & 3;
2027 if (mask & CPSR_IT_2_7) {
2028 env->condexec_bits &= 3;
2029 env->condexec_bits |= (val >> 8) & 0xfc;
2031 if (mask & CPSR_GE) {
2032 env->GE = (val >> 16) & 0xf;
2035 if ((env->uncached_cpsr ^ val) & mask & CPSR_M) {
2036 if (bad_mode_switch(env, val & CPSR_M)) {
2037 /* Attempt to switch to an invalid mode: this is UNPREDICTABLE.
2038 * We choose to ignore the attempt and leave the CPSR M field
2039 * untouched.
2041 mask &= ~CPSR_M;
2042 } else {
2043 switch_mode(env, val & CPSR_M);
2046 mask &= ~CACHED_CPSR_BITS;
2047 env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask);
2050 /* Sign/zero extend */
2051 uint32_t HELPER(sxtb16)(uint32_t x)
2053 uint32_t res;
2054 res = (uint16_t)(int8_t)x;
2055 res |= (uint32_t)(int8_t)(x >> 16) << 16;
2056 return res;
2059 uint32_t HELPER(uxtb16)(uint32_t x)
2061 uint32_t res;
2062 res = (uint16_t)(uint8_t)x;
2063 res |= (uint32_t)(uint8_t)(x >> 16) << 16;
2064 return res;
2067 uint32_t HELPER(clz)(uint32_t x)
2069 return clz32(x);
2072 int32_t HELPER(sdiv)(int32_t num, int32_t den)
2074 if (den == 0)
2075 return 0;
2076 if (num == INT_MIN && den == -1)
2077 return INT_MIN;
2078 return num / den;
2081 uint32_t HELPER(udiv)(uint32_t num, uint32_t den)
2083 if (den == 0)
2084 return 0;
2085 return num / den;
2088 uint32_t HELPER(rbit)(uint32_t x)
2090 x = ((x & 0xff000000) >> 24)
2091 | ((x & 0x00ff0000) >> 8)
2092 | ((x & 0x0000ff00) << 8)
2093 | ((x & 0x000000ff) << 24);
2094 x = ((x & 0xf0f0f0f0) >> 4)
2095 | ((x & 0x0f0f0f0f) << 4);
2096 x = ((x & 0x88888888) >> 3)
2097 | ((x & 0x44444444) >> 1)
2098 | ((x & 0x22222222) << 1)
2099 | ((x & 0x11111111) << 3);
2100 return x;
2103 #if defined(CONFIG_USER_ONLY)
2105 void arm_cpu_do_interrupt(CPUState *cs)
2107 ARMCPU *cpu = ARM_CPU(cs);
2108 CPUARMState *env = &cpu->env;
2110 env->exception_index = -1;
2113 int cpu_arm_handle_mmu_fault (CPUARMState *env, target_ulong address, int rw,
2114 int mmu_idx)
2116 if (rw == 2) {
2117 env->exception_index = EXCP_PREFETCH_ABORT;
2118 env->cp15.c6_insn = address;
2119 } else {
2120 env->exception_index = EXCP_DATA_ABORT;
2121 env->cp15.c6_data = address;
2123 return 1;
2126 /* These should probably raise undefined insn exceptions. */
2127 void HELPER(v7m_msr)(CPUARMState *env, uint32_t reg, uint32_t val)
2129 cpu_abort(env, "v7m_mrs %d\n", reg);
2132 uint32_t HELPER(v7m_mrs)(CPUARMState *env, uint32_t reg)
2134 cpu_abort(env, "v7m_mrs %d\n", reg);
2135 return 0;
2138 void switch_mode(CPUARMState *env, int mode)
2140 if (mode != ARM_CPU_MODE_USR)
2141 cpu_abort(env, "Tried to switch out of user mode\n");
2144 void HELPER(set_r13_banked)(CPUARMState *env, uint32_t mode, uint32_t val)
2146 cpu_abort(env, "banked r13 write\n");
2149 uint32_t HELPER(get_r13_banked)(CPUARMState *env, uint32_t mode)
2151 cpu_abort(env, "banked r13 read\n");
2152 return 0;
2155 #else
2157 /* Map CPU modes onto saved register banks. */
2158 int bank_number(int mode)
2160 switch (mode) {
2161 case ARM_CPU_MODE_USR:
2162 case ARM_CPU_MODE_SYS:
2163 return 0;
2164 case ARM_CPU_MODE_SVC:
2165 return 1;
2166 case ARM_CPU_MODE_ABT:
2167 return 2;
2168 case ARM_CPU_MODE_UND:
2169 return 3;
2170 case ARM_CPU_MODE_IRQ:
2171 return 4;
2172 case ARM_CPU_MODE_FIQ:
2173 return 5;
2175 hw_error("bank number requested for bad CPSR mode value 0x%x\n", mode);
2178 void switch_mode(CPUARMState *env, int mode)
2180 int old_mode;
2181 int i;
2183 old_mode = env->uncached_cpsr & CPSR_M;
2184 if (mode == old_mode)
2185 return;
2187 if (old_mode == ARM_CPU_MODE_FIQ) {
2188 memcpy (env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t));
2189 memcpy (env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t));
2190 } else if (mode == ARM_CPU_MODE_FIQ) {
2191 memcpy (env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t));
2192 memcpy (env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t));
2195 i = bank_number(old_mode);
2196 env->banked_r13[i] = env->regs[13];
2197 env->banked_r14[i] = env->regs[14];
2198 env->banked_spsr[i] = env->spsr;
2200 i = bank_number(mode);
2201 env->regs[13] = env->banked_r13[i];
2202 env->regs[14] = env->banked_r14[i];
2203 env->spsr = env->banked_spsr[i];
2206 static void v7m_push(CPUARMState *env, uint32_t val)
2208 env->regs[13] -= 4;
2209 stl_phys(env->regs[13], val);
2212 static uint32_t v7m_pop(CPUARMState *env)
2214 uint32_t val;
2215 val = ldl_phys(env->regs[13]);
2216 env->regs[13] += 4;
2217 return val;
2220 /* Switch to V7M main or process stack pointer. */
2221 static void switch_v7m_sp(CPUARMState *env, int process)
2223 uint32_t tmp;
2224 if (env->v7m.current_sp != process) {
2225 tmp = env->v7m.other_sp;
2226 env->v7m.other_sp = env->regs[13];
2227 env->regs[13] = tmp;
2228 env->v7m.current_sp = process;
2232 static void do_v7m_exception_exit(CPUARMState *env)
2234 uint32_t type;
2235 uint32_t xpsr;
2237 type = env->regs[15];
2238 if (env->v7m.exception != 0)
2239 armv7m_nvic_complete_irq(env->nvic, env->v7m.exception);
2241 /* Switch to the target stack. */
2242 switch_v7m_sp(env, (type & 4) != 0);
2243 /* Pop registers. */
2244 env->regs[0] = v7m_pop(env);
2245 env->regs[1] = v7m_pop(env);
2246 env->regs[2] = v7m_pop(env);
2247 env->regs[3] = v7m_pop(env);
2248 env->regs[12] = v7m_pop(env);
2249 env->regs[14] = v7m_pop(env);
2250 env->regs[15] = v7m_pop(env);
2251 xpsr = v7m_pop(env);
2252 xpsr_write(env, xpsr, 0xfffffdff);
2253 /* Undo stack alignment. */
2254 if (xpsr & 0x200)
2255 env->regs[13] |= 4;
2256 /* ??? The exception return type specifies Thread/Handler mode. However
2257 this is also implied by the xPSR value. Not sure what to do
2258 if there is a mismatch. */
2259 /* ??? Likewise for mismatches between the CONTROL register and the stack
2260 pointer. */
2263 /* Exception names for debug logging; note that not all of these
2264 * precisely correspond to architectural exceptions.
2266 static const char * const excnames[] = {
2267 [EXCP_UDEF] = "Undefined Instruction",
2268 [EXCP_SWI] = "SVC",
2269 [EXCP_PREFETCH_ABORT] = "Prefetch Abort",
2270 [EXCP_DATA_ABORT] = "Data Abort",
2271 [EXCP_IRQ] = "IRQ",
2272 [EXCP_FIQ] = "FIQ",
2273 [EXCP_BKPT] = "Breakpoint",
2274 [EXCP_EXCEPTION_EXIT] = "QEMU v7M exception exit",
2275 [EXCP_KERNEL_TRAP] = "QEMU intercept of kernel commpage",
2276 [EXCP_STREX] = "QEMU intercept of STREX",
2279 static inline void arm_log_exception(int idx)
2281 if (qemu_loglevel_mask(CPU_LOG_INT)) {
2282 const char *exc = NULL;
2284 if (idx >= 0 && idx < ARRAY_SIZE(excnames)) {
2285 exc = excnames[idx];
2287 if (!exc) {
2288 exc = "unknown";
2290 qemu_log_mask(CPU_LOG_INT, "Taking exception %d [%s]\n", idx, exc);
2294 void arm_v7m_cpu_do_interrupt(CPUState *cs)
2296 ARMCPU *cpu = ARM_CPU(cs);
2297 CPUARMState *env = &cpu->env;
2298 uint32_t xpsr = xpsr_read(env);
2299 uint32_t lr;
2300 uint32_t addr;
2302 arm_log_exception(env->exception_index);
2304 lr = 0xfffffff1;
2305 if (env->v7m.current_sp)
2306 lr |= 4;
2307 if (env->v7m.exception == 0)
2308 lr |= 8;
2310 /* For exceptions we just mark as pending on the NVIC, and let that
2311 handle it. */
2312 /* TODO: Need to escalate if the current priority is higher than the
2313 one we're raising. */
2314 switch (env->exception_index) {
2315 case EXCP_UDEF:
2316 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE);
2317 return;
2318 case EXCP_SWI:
2319 /* The PC already points to the next instruction. */
2320 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SVC);
2321 return;
2322 case EXCP_PREFETCH_ABORT:
2323 case EXCP_DATA_ABORT:
2324 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_MEM);
2325 return;
2326 case EXCP_BKPT:
2327 if (semihosting_enabled) {
2328 int nr;
2329 nr = arm_lduw_code(env, env->regs[15], env->bswap_code) & 0xff;
2330 if (nr == 0xab) {
2331 env->regs[15] += 2;
2332 env->regs[0] = do_arm_semihosting(env);
2333 qemu_log_mask(CPU_LOG_INT, "...handled as semihosting call\n");
2334 return;
2337 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_DEBUG);
2338 return;
2339 case EXCP_IRQ:
2340 env->v7m.exception = armv7m_nvic_acknowledge_irq(env->nvic);
2341 break;
2342 case EXCP_EXCEPTION_EXIT:
2343 do_v7m_exception_exit(env);
2344 return;
2345 default:
2346 cpu_abort(env, "Unhandled exception 0x%x\n", env->exception_index);
2347 return; /* Never happens. Keep compiler happy. */
2350 /* Align stack pointer. */
2351 /* ??? Should only do this if Configuration Control Register
2352 STACKALIGN bit is set. */
2353 if (env->regs[13] & 4) {
2354 env->regs[13] -= 4;
2355 xpsr |= 0x200;
2357 /* Switch to the handler mode. */
2358 v7m_push(env, xpsr);
2359 v7m_push(env, env->regs[15]);
2360 v7m_push(env, env->regs[14]);
2361 v7m_push(env, env->regs[12]);
2362 v7m_push(env, env->regs[3]);
2363 v7m_push(env, env->regs[2]);
2364 v7m_push(env, env->regs[1]);
2365 v7m_push(env, env->regs[0]);
2366 switch_v7m_sp(env, 0);
2367 /* Clear IT bits */
2368 env->condexec_bits = 0;
2369 env->regs[14] = lr;
2370 addr = ldl_phys(env->v7m.vecbase + env->v7m.exception * 4);
2371 env->regs[15] = addr & 0xfffffffe;
2372 env->thumb = addr & 1;
2375 /* Handle a CPU exception. */
2376 void arm_cpu_do_interrupt(CPUState *cs)
2378 ARMCPU *cpu = ARM_CPU(cs);
2379 CPUARMState *env = &cpu->env;
2380 uint32_t addr;
2381 uint32_t mask;
2382 int new_mode;
2383 uint32_t offset;
2385 assert(!IS_M(env));
2387 arm_log_exception(env->exception_index);
2389 /* TODO: Vectored interrupt controller. */
2390 switch (env->exception_index) {
2391 case EXCP_UDEF:
2392 new_mode = ARM_CPU_MODE_UND;
2393 addr = 0x04;
2394 mask = CPSR_I;
2395 if (env->thumb)
2396 offset = 2;
2397 else
2398 offset = 4;
2399 break;
2400 case EXCP_SWI:
2401 if (semihosting_enabled) {
2402 /* Check for semihosting interrupt. */
2403 if (env->thumb) {
2404 mask = arm_lduw_code(env, env->regs[15] - 2, env->bswap_code)
2405 & 0xff;
2406 } else {
2407 mask = arm_ldl_code(env, env->regs[15] - 4, env->bswap_code)
2408 & 0xffffff;
2410 /* Only intercept calls from privileged modes, to provide some
2411 semblance of security. */
2412 if (((mask == 0x123456 && !env->thumb)
2413 || (mask == 0xab && env->thumb))
2414 && (env->uncached_cpsr & CPSR_M) != ARM_CPU_MODE_USR) {
2415 env->regs[0] = do_arm_semihosting(env);
2416 qemu_log_mask(CPU_LOG_INT, "...handled as semihosting call\n");
2417 return;
2420 new_mode = ARM_CPU_MODE_SVC;
2421 addr = 0x08;
2422 mask = CPSR_I;
2423 /* The PC already points to the next instruction. */
2424 offset = 0;
2425 break;
2426 case EXCP_BKPT:
2427 /* See if this is a semihosting syscall. */
2428 if (env->thumb && semihosting_enabled) {
2429 mask = arm_lduw_code(env, env->regs[15], env->bswap_code) & 0xff;
2430 if (mask == 0xab
2431 && (env->uncached_cpsr & CPSR_M) != ARM_CPU_MODE_USR) {
2432 env->regs[15] += 2;
2433 env->regs[0] = do_arm_semihosting(env);
2434 qemu_log_mask(CPU_LOG_INT, "...handled as semihosting call\n");
2435 return;
2438 env->cp15.c5_insn = 2;
2439 /* Fall through to prefetch abort. */
2440 case EXCP_PREFETCH_ABORT:
2441 qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x IFAR 0x%x\n",
2442 env->cp15.c5_insn, env->cp15.c6_insn);
2443 new_mode = ARM_CPU_MODE_ABT;
2444 addr = 0x0c;
2445 mask = CPSR_A | CPSR_I;
2446 offset = 4;
2447 break;
2448 case EXCP_DATA_ABORT:
2449 qemu_log_mask(CPU_LOG_INT, "...with DFSR 0x%x DFAR 0x%x\n",
2450 env->cp15.c5_data, env->cp15.c6_data);
2451 new_mode = ARM_CPU_MODE_ABT;
2452 addr = 0x10;
2453 mask = CPSR_A | CPSR_I;
2454 offset = 8;
2455 break;
2456 case EXCP_IRQ:
2457 new_mode = ARM_CPU_MODE_IRQ;
2458 addr = 0x18;
2459 /* Disable IRQ and imprecise data aborts. */
2460 mask = CPSR_A | CPSR_I;
2461 offset = 4;
2462 break;
2463 case EXCP_FIQ:
2464 new_mode = ARM_CPU_MODE_FIQ;
2465 addr = 0x1c;
2466 /* Disable FIQ, IRQ and imprecise data aborts. */
2467 mask = CPSR_A | CPSR_I | CPSR_F;
2468 offset = 4;
2469 break;
2470 default:
2471 cpu_abort(env, "Unhandled exception 0x%x\n", env->exception_index);
2472 return; /* Never happens. Keep compiler happy. */
2474 /* High vectors. */
2475 if (env->cp15.c1_sys & (1 << 13)) {
2476 addr += 0xffff0000;
2478 switch_mode (env, new_mode);
2479 env->spsr = cpsr_read(env);
2480 /* Clear IT bits. */
2481 env->condexec_bits = 0;
2482 /* Switch to the new mode, and to the correct instruction set. */
2483 env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode;
2484 env->uncached_cpsr |= mask;
2485 /* this is a lie, as the was no c1_sys on V4T/V5, but who cares
2486 * and we should just guard the thumb mode on V4 */
2487 if (arm_feature(env, ARM_FEATURE_V4T)) {
2488 env->thumb = (env->cp15.c1_sys & (1 << 30)) != 0;
2490 env->regs[14] = env->regs[15] + offset;
2491 env->regs[15] = addr;
2492 cs->interrupt_request |= CPU_INTERRUPT_EXITTB;
2495 /* Check section/page access permissions.
2496 Returns the page protection flags, or zero if the access is not
2497 permitted. */
2498 static inline int check_ap(CPUARMState *env, int ap, int domain_prot,
2499 int access_type, int is_user)
2501 int prot_ro;
2503 if (domain_prot == 3) {
2504 return PAGE_READ | PAGE_WRITE;
2507 if (access_type == 1)
2508 prot_ro = 0;
2509 else
2510 prot_ro = PAGE_READ;
2512 switch (ap) {
2513 case 0:
2514 if (access_type == 1)
2515 return 0;
2516 switch ((env->cp15.c1_sys >> 8) & 3) {
2517 case 1:
2518 return is_user ? 0 : PAGE_READ;
2519 case 2:
2520 return PAGE_READ;
2521 default:
2522 return 0;
2524 case 1:
2525 return is_user ? 0 : PAGE_READ | PAGE_WRITE;
2526 case 2:
2527 if (is_user)
2528 return prot_ro;
2529 else
2530 return PAGE_READ | PAGE_WRITE;
2531 case 3:
2532 return PAGE_READ | PAGE_WRITE;
2533 case 4: /* Reserved. */
2534 return 0;
2535 case 5:
2536 return is_user ? 0 : prot_ro;
2537 case 6:
2538 return prot_ro;
2539 case 7:
2540 if (!arm_feature (env, ARM_FEATURE_V6K))
2541 return 0;
2542 return prot_ro;
2543 default:
2544 abort();
2548 static uint32_t get_level1_table_address(CPUARMState *env, uint32_t address)
2550 uint32_t table;
2552 if (address & env->cp15.c2_mask)
2553 table = env->cp15.c2_base1 & 0xffffc000;
2554 else
2555 table = env->cp15.c2_base0 & env->cp15.c2_base_mask;
2557 table |= (address >> 18) & 0x3ffc;
2558 return table;
2561 static int get_phys_addr_v5(CPUARMState *env, uint32_t address, int access_type,
2562 int is_user, hwaddr *phys_ptr,
2563 int *prot, target_ulong *page_size)
2565 int code;
2566 uint32_t table;
2567 uint32_t desc;
2568 int type;
2569 int ap;
2570 int domain;
2571 int domain_prot;
2572 hwaddr phys_addr;
2574 /* Pagetable walk. */
2575 /* Lookup l1 descriptor. */
2576 table = get_level1_table_address(env, address);
2577 desc = ldl_phys(table);
2578 type = (desc & 3);
2579 domain = (desc >> 5) & 0x0f;
2580 domain_prot = (env->cp15.c3 >> (domain * 2)) & 3;
2581 if (type == 0) {
2582 /* Section translation fault. */
2583 code = 5;
2584 goto do_fault;
2586 if (domain_prot == 0 || domain_prot == 2) {
2587 if (type == 2)
2588 code = 9; /* Section domain fault. */
2589 else
2590 code = 11; /* Page domain fault. */
2591 goto do_fault;
2593 if (type == 2) {
2594 /* 1Mb section. */
2595 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
2596 ap = (desc >> 10) & 3;
2597 code = 13;
2598 *page_size = 1024 * 1024;
2599 } else {
2600 /* Lookup l2 entry. */
2601 if (type == 1) {
2602 /* Coarse pagetable. */
2603 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
2604 } else {
2605 /* Fine pagetable. */
2606 table = (desc & 0xfffff000) | ((address >> 8) & 0xffc);
2608 desc = ldl_phys(table);
2609 switch (desc & 3) {
2610 case 0: /* Page translation fault. */
2611 code = 7;
2612 goto do_fault;
2613 case 1: /* 64k page. */
2614 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
2615 ap = (desc >> (4 + ((address >> 13) & 6))) & 3;
2616 *page_size = 0x10000;
2617 break;
2618 case 2: /* 4k page. */
2619 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
2620 ap = (desc >> (4 + ((address >> 13) & 6))) & 3;
2621 *page_size = 0x1000;
2622 break;
2623 case 3: /* 1k page. */
2624 if (type == 1) {
2625 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
2626 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
2627 } else {
2628 /* Page translation fault. */
2629 code = 7;
2630 goto do_fault;
2632 } else {
2633 phys_addr = (desc & 0xfffffc00) | (address & 0x3ff);
2635 ap = (desc >> 4) & 3;
2636 *page_size = 0x400;
2637 break;
2638 default:
2639 /* Never happens, but compiler isn't smart enough to tell. */
2640 abort();
2642 code = 15;
2644 *prot = check_ap(env, ap, domain_prot, access_type, is_user);
2645 if (!*prot) {
2646 /* Access permission fault. */
2647 goto do_fault;
2649 *prot |= PAGE_EXEC;
2650 *phys_ptr = phys_addr;
2651 return 0;
2652 do_fault:
2653 return code | (domain << 4);
2656 static int get_phys_addr_v6(CPUARMState *env, uint32_t address, int access_type,
2657 int is_user, hwaddr *phys_ptr,
2658 int *prot, target_ulong *page_size)
2660 int code;
2661 uint32_t table;
2662 uint32_t desc;
2663 uint32_t xn;
2664 uint32_t pxn = 0;
2665 int type;
2666 int ap;
2667 int domain = 0;
2668 int domain_prot;
2669 hwaddr phys_addr;
2671 /* Pagetable walk. */
2672 /* Lookup l1 descriptor. */
2673 table = get_level1_table_address(env, address);
2674 desc = ldl_phys(table);
2675 type = (desc & 3);
2676 if (type == 0 || (type == 3 && !arm_feature(env, ARM_FEATURE_PXN))) {
2677 /* Section translation fault, or attempt to use the encoding
2678 * which is Reserved on implementations without PXN.
2680 code = 5;
2681 goto do_fault;
2683 if ((type == 1) || !(desc & (1 << 18))) {
2684 /* Page or Section. */
2685 domain = (desc >> 5) & 0x0f;
2687 domain_prot = (env->cp15.c3 >> (domain * 2)) & 3;
2688 if (domain_prot == 0 || domain_prot == 2) {
2689 if (type != 1) {
2690 code = 9; /* Section domain fault. */
2691 } else {
2692 code = 11; /* Page domain fault. */
2694 goto do_fault;
2696 if (type != 1) {
2697 if (desc & (1 << 18)) {
2698 /* Supersection. */
2699 phys_addr = (desc & 0xff000000) | (address & 0x00ffffff);
2700 *page_size = 0x1000000;
2701 } else {
2702 /* Section. */
2703 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
2704 *page_size = 0x100000;
2706 ap = ((desc >> 10) & 3) | ((desc >> 13) & 4);
2707 xn = desc & (1 << 4);
2708 pxn = desc & 1;
2709 code = 13;
2710 } else {
2711 if (arm_feature(env, ARM_FEATURE_PXN)) {
2712 pxn = (desc >> 2) & 1;
2714 /* Lookup l2 entry. */
2715 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
2716 desc = ldl_phys(table);
2717 ap = ((desc >> 4) & 3) | ((desc >> 7) & 4);
2718 switch (desc & 3) {
2719 case 0: /* Page translation fault. */
2720 code = 7;
2721 goto do_fault;
2722 case 1: /* 64k page. */
2723 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
2724 xn = desc & (1 << 15);
2725 *page_size = 0x10000;
2726 break;
2727 case 2: case 3: /* 4k page. */
2728 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
2729 xn = desc & 1;
2730 *page_size = 0x1000;
2731 break;
2732 default:
2733 /* Never happens, but compiler isn't smart enough to tell. */
2734 abort();
2736 code = 15;
2738 if (domain_prot == 3) {
2739 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
2740 } else {
2741 if (pxn && !is_user) {
2742 xn = 1;
2744 if (xn && access_type == 2)
2745 goto do_fault;
2747 /* The simplified model uses AP[0] as an access control bit. */
2748 if ((env->cp15.c1_sys & (1 << 29)) && (ap & 1) == 0) {
2749 /* Access flag fault. */
2750 code = (code == 15) ? 6 : 3;
2751 goto do_fault;
2753 *prot = check_ap(env, ap, domain_prot, access_type, is_user);
2754 if (!*prot) {
2755 /* Access permission fault. */
2756 goto do_fault;
2758 if (!xn) {
2759 *prot |= PAGE_EXEC;
2762 *phys_ptr = phys_addr;
2763 return 0;
2764 do_fault:
2765 return code | (domain << 4);
2768 /* Fault type for long-descriptor MMU fault reporting; this corresponds
2769 * to bits [5..2] in the STATUS field in long-format DFSR/IFSR.
2771 typedef enum {
2772 translation_fault = 1,
2773 access_fault = 2,
2774 permission_fault = 3,
2775 } MMUFaultType;
2777 static int get_phys_addr_lpae(CPUARMState *env, uint32_t address,
2778 int access_type, int is_user,
2779 hwaddr *phys_ptr, int *prot,
2780 target_ulong *page_size_ptr)
2782 /* Read an LPAE long-descriptor translation table. */
2783 MMUFaultType fault_type = translation_fault;
2784 uint32_t level = 1;
2785 uint32_t epd;
2786 uint32_t tsz;
2787 uint64_t ttbr;
2788 int ttbr_select;
2789 int n;
2790 hwaddr descaddr;
2791 uint32_t tableattrs;
2792 target_ulong page_size;
2793 uint32_t attrs;
2795 /* Determine whether this address is in the region controlled by
2796 * TTBR0 or TTBR1 (or if it is in neither region and should fault).
2797 * This is a Non-secure PL0/1 stage 1 translation, so controlled by
2798 * TTBCR/TTBR0/TTBR1 in accordance with ARM ARM DDI0406C table B-32:
2800 uint32_t t0sz = extract32(env->cp15.c2_control, 0, 3);
2801 uint32_t t1sz = extract32(env->cp15.c2_control, 16, 3);
2802 if (t0sz && !extract32(address, 32 - t0sz, t0sz)) {
2803 /* there is a ttbr0 region and we are in it (high bits all zero) */
2804 ttbr_select = 0;
2805 } else if (t1sz && !extract32(~address, 32 - t1sz, t1sz)) {
2806 /* there is a ttbr1 region and we are in it (high bits all one) */
2807 ttbr_select = 1;
2808 } else if (!t0sz) {
2809 /* ttbr0 region is "everything not in the ttbr1 region" */
2810 ttbr_select = 0;
2811 } else if (!t1sz) {
2812 /* ttbr1 region is "everything not in the ttbr0 region" */
2813 ttbr_select = 1;
2814 } else {
2815 /* in the gap between the two regions, this is a Translation fault */
2816 fault_type = translation_fault;
2817 goto do_fault;
2820 /* Note that QEMU ignores shareability and cacheability attributes,
2821 * so we don't need to do anything with the SH, ORGN, IRGN fields
2822 * in the TTBCR. Similarly, TTBCR:A1 selects whether we get the
2823 * ASID from TTBR0 or TTBR1, but QEMU's TLB doesn't currently
2824 * implement any ASID-like capability so we can ignore it (instead
2825 * we will always flush the TLB any time the ASID is changed).
2827 if (ttbr_select == 0) {
2828 ttbr = ((uint64_t)env->cp15.c2_base0_hi << 32) | env->cp15.c2_base0;
2829 epd = extract32(env->cp15.c2_control, 7, 1);
2830 tsz = t0sz;
2831 } else {
2832 ttbr = ((uint64_t)env->cp15.c2_base1_hi << 32) | env->cp15.c2_base1;
2833 epd = extract32(env->cp15.c2_control, 23, 1);
2834 tsz = t1sz;
2837 if (epd) {
2838 /* Translation table walk disabled => Translation fault on TLB miss */
2839 goto do_fault;
2842 /* If the region is small enough we will skip straight to a 2nd level
2843 * lookup. This affects the number of bits of the address used in
2844 * combination with the TTBR to find the first descriptor. ('n' here
2845 * matches the usage in the ARM ARM sB3.6.6, where bits [39..n] are
2846 * from the TTBR, [n-1..3] from the vaddr, and [2..0] always zero).
2848 if (tsz > 1) {
2849 level = 2;
2850 n = 14 - tsz;
2851 } else {
2852 n = 5 - tsz;
2855 /* Clear the vaddr bits which aren't part of the within-region address,
2856 * so that we don't have to special case things when calculating the
2857 * first descriptor address.
2859 address &= (0xffffffffU >> tsz);
2861 /* Now we can extract the actual base address from the TTBR */
2862 descaddr = extract64(ttbr, 0, 40);
2863 descaddr &= ~((1ULL << n) - 1);
2865 tableattrs = 0;
2866 for (;;) {
2867 uint64_t descriptor;
2869 descaddr |= ((address >> (9 * (4 - level))) & 0xff8);
2870 descriptor = ldq_phys(descaddr);
2871 if (!(descriptor & 1) ||
2872 (!(descriptor & 2) && (level == 3))) {
2873 /* Invalid, or the Reserved level 3 encoding */
2874 goto do_fault;
2876 descaddr = descriptor & 0xfffffff000ULL;
2878 if ((descriptor & 2) && (level < 3)) {
2879 /* Table entry. The top five bits are attributes which may
2880 * propagate down through lower levels of the table (and
2881 * which are all arranged so that 0 means "no effect", so
2882 * we can gather them up by ORing in the bits at each level).
2884 tableattrs |= extract64(descriptor, 59, 5);
2885 level++;
2886 continue;
2888 /* Block entry at level 1 or 2, or page entry at level 3.
2889 * These are basically the same thing, although the number
2890 * of bits we pull in from the vaddr varies.
2892 page_size = (1 << (39 - (9 * level)));
2893 descaddr |= (address & (page_size - 1));
2894 /* Extract attributes from the descriptor and merge with table attrs */
2895 attrs = extract64(descriptor, 2, 10)
2896 | (extract64(descriptor, 52, 12) << 10);
2897 attrs |= extract32(tableattrs, 0, 2) << 11; /* XN, PXN */
2898 attrs |= extract32(tableattrs, 3, 1) << 5; /* APTable[1] => AP[2] */
2899 /* The sense of AP[1] vs APTable[0] is reversed, as APTable[0] == 1
2900 * means "force PL1 access only", which means forcing AP[1] to 0.
2902 if (extract32(tableattrs, 2, 1)) {
2903 attrs &= ~(1 << 4);
2905 /* Since we're always in the Non-secure state, NSTable is ignored. */
2906 break;
2908 /* Here descaddr is the final physical address, and attributes
2909 * are all in attrs.
2911 fault_type = access_fault;
2912 if ((attrs & (1 << 8)) == 0) {
2913 /* Access flag */
2914 goto do_fault;
2916 fault_type = permission_fault;
2917 if (is_user && !(attrs & (1 << 4))) {
2918 /* Unprivileged access not enabled */
2919 goto do_fault;
2921 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
2922 if (attrs & (1 << 12) || (!is_user && (attrs & (1 << 11)))) {
2923 /* XN or PXN */
2924 if (access_type == 2) {
2925 goto do_fault;
2927 *prot &= ~PAGE_EXEC;
2929 if (attrs & (1 << 5)) {
2930 /* Write access forbidden */
2931 if (access_type == 1) {
2932 goto do_fault;
2934 *prot &= ~PAGE_WRITE;
2937 *phys_ptr = descaddr;
2938 *page_size_ptr = page_size;
2939 return 0;
2941 do_fault:
2942 /* Long-descriptor format IFSR/DFSR value */
2943 return (1 << 9) | (fault_type << 2) | level;
2946 static int get_phys_addr_mpu(CPUARMState *env, uint32_t address,
2947 int access_type, int is_user,
2948 hwaddr *phys_ptr, int *prot)
2950 int n;
2951 uint32_t mask;
2952 uint32_t base;
2954 *phys_ptr = address;
2955 for (n = 7; n >= 0; n--) {
2956 base = env->cp15.c6_region[n];
2957 if ((base & 1) == 0)
2958 continue;
2959 mask = 1 << ((base >> 1) & 0x1f);
2960 /* Keep this shift separate from the above to avoid an
2961 (undefined) << 32. */
2962 mask = (mask << 1) - 1;
2963 if (((base ^ address) & ~mask) == 0)
2964 break;
2966 if (n < 0)
2967 return 2;
2969 if (access_type == 2) {
2970 mask = env->cp15.c5_insn;
2971 } else {
2972 mask = env->cp15.c5_data;
2974 mask = (mask >> (n * 4)) & 0xf;
2975 switch (mask) {
2976 case 0:
2977 return 1;
2978 case 1:
2979 if (is_user)
2980 return 1;
2981 *prot = PAGE_READ | PAGE_WRITE;
2982 break;
2983 case 2:
2984 *prot = PAGE_READ;
2985 if (!is_user)
2986 *prot |= PAGE_WRITE;
2987 break;
2988 case 3:
2989 *prot = PAGE_READ | PAGE_WRITE;
2990 break;
2991 case 5:
2992 if (is_user)
2993 return 1;
2994 *prot = PAGE_READ;
2995 break;
2996 case 6:
2997 *prot = PAGE_READ;
2998 break;
2999 default:
3000 /* Bad permission. */
3001 return 1;
3003 *prot |= PAGE_EXEC;
3004 return 0;
3007 /* get_phys_addr - get the physical address for this virtual address
3009 * Find the physical address corresponding to the given virtual address,
3010 * by doing a translation table walk on MMU based systems or using the
3011 * MPU state on MPU based systems.
3013 * Returns 0 if the translation was successful. Otherwise, phys_ptr,
3014 * prot and page_size are not filled in, and the return value provides
3015 * information on why the translation aborted, in the format of a
3016 * DFSR/IFSR fault register, with the following caveats:
3017 * * we honour the short vs long DFSR format differences.
3018 * * the WnR bit is never set (the caller must do this).
3019 * * for MPU based systems we don't bother to return a full FSR format
3020 * value.
3022 * @env: CPUARMState
3023 * @address: virtual address to get physical address for
3024 * @access_type: 0 for read, 1 for write, 2 for execute
3025 * @is_user: 0 for privileged access, 1 for user
3026 * @phys_ptr: set to the physical address corresponding to the virtual address
3027 * @prot: set to the permissions for the page containing phys_ptr
3028 * @page_size: set to the size of the page containing phys_ptr
3030 static inline int get_phys_addr(CPUARMState *env, uint32_t address,
3031 int access_type, int is_user,
3032 hwaddr *phys_ptr, int *prot,
3033 target_ulong *page_size)
3035 /* Fast Context Switch Extension. */
3036 if (address < 0x02000000)
3037 address += env->cp15.c13_fcse;
3039 if ((env->cp15.c1_sys & 1) == 0) {
3040 /* MMU/MPU disabled. */
3041 *phys_ptr = address;
3042 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
3043 *page_size = TARGET_PAGE_SIZE;
3044 return 0;
3045 } else if (arm_feature(env, ARM_FEATURE_MPU)) {
3046 *page_size = TARGET_PAGE_SIZE;
3047 return get_phys_addr_mpu(env, address, access_type, is_user, phys_ptr,
3048 prot);
3049 } else if (extended_addresses_enabled(env)) {
3050 return get_phys_addr_lpae(env, address, access_type, is_user, phys_ptr,
3051 prot, page_size);
3052 } else if (env->cp15.c1_sys & (1 << 23)) {
3053 return get_phys_addr_v6(env, address, access_type, is_user, phys_ptr,
3054 prot, page_size);
3055 } else {
3056 return get_phys_addr_v5(env, address, access_type, is_user, phys_ptr,
3057 prot, page_size);
3061 int cpu_arm_handle_mmu_fault (CPUARMState *env, target_ulong address,
3062 int access_type, int mmu_idx)
3064 hwaddr phys_addr;
3065 target_ulong page_size;
3066 int prot;
3067 int ret, is_user;
3069 is_user = mmu_idx == MMU_USER_IDX;
3070 ret = get_phys_addr(env, address, access_type, is_user, &phys_addr, &prot,
3071 &page_size);
3072 if (ret == 0) {
3073 /* Map a single [sub]page. */
3074 phys_addr &= ~(hwaddr)0x3ff;
3075 address &= ~(uint32_t)0x3ff;
3076 tlb_set_page (env, address, phys_addr, prot, mmu_idx, page_size);
3077 return 0;
3080 if (access_type == 2) {
3081 env->cp15.c5_insn = ret;
3082 env->cp15.c6_insn = address;
3083 env->exception_index = EXCP_PREFETCH_ABORT;
3084 } else {
3085 env->cp15.c5_data = ret;
3086 if (access_type == 1 && arm_feature(env, ARM_FEATURE_V6))
3087 env->cp15.c5_data |= (1 << 11);
3088 env->cp15.c6_data = address;
3089 env->exception_index = EXCP_DATA_ABORT;
3091 return 1;
3094 hwaddr arm_cpu_get_phys_page_debug(CPUState *cs, vaddr addr)
3096 ARMCPU *cpu = ARM_CPU(cs);
3097 hwaddr phys_addr;
3098 target_ulong page_size;
3099 int prot;
3100 int ret;
3102 ret = get_phys_addr(&cpu->env, addr, 0, 0, &phys_addr, &prot, &page_size);
3104 if (ret != 0) {
3105 return -1;
3108 return phys_addr;
3111 void HELPER(set_r13_banked)(CPUARMState *env, uint32_t mode, uint32_t val)
3113 if ((env->uncached_cpsr & CPSR_M) == mode) {
3114 env->regs[13] = val;
3115 } else {
3116 env->banked_r13[bank_number(mode)] = val;
3120 uint32_t HELPER(get_r13_banked)(CPUARMState *env, uint32_t mode)
3122 if ((env->uncached_cpsr & CPSR_M) == mode) {
3123 return env->regs[13];
3124 } else {
3125 return env->banked_r13[bank_number(mode)];
3129 uint32_t HELPER(v7m_mrs)(CPUARMState *env, uint32_t reg)
3131 switch (reg) {
3132 case 0: /* APSR */
3133 return xpsr_read(env) & 0xf8000000;
3134 case 1: /* IAPSR */
3135 return xpsr_read(env) & 0xf80001ff;
3136 case 2: /* EAPSR */
3137 return xpsr_read(env) & 0xff00fc00;
3138 case 3: /* xPSR */
3139 return xpsr_read(env) & 0xff00fdff;
3140 case 5: /* IPSR */
3141 return xpsr_read(env) & 0x000001ff;
3142 case 6: /* EPSR */
3143 return xpsr_read(env) & 0x0700fc00;
3144 case 7: /* IEPSR */
3145 return xpsr_read(env) & 0x0700edff;
3146 case 8: /* MSP */
3147 return env->v7m.current_sp ? env->v7m.other_sp : env->regs[13];
3148 case 9: /* PSP */
3149 return env->v7m.current_sp ? env->regs[13] : env->v7m.other_sp;
3150 case 16: /* PRIMASK */
3151 return (env->uncached_cpsr & CPSR_I) != 0;
3152 case 17: /* BASEPRI */
3153 case 18: /* BASEPRI_MAX */
3154 return env->v7m.basepri;
3155 case 19: /* FAULTMASK */
3156 return (env->uncached_cpsr & CPSR_F) != 0;
3157 case 20: /* CONTROL */
3158 return env->v7m.control;
3159 default:
3160 /* ??? For debugging only. */
3161 cpu_abort(env, "Unimplemented system register read (%d)\n", reg);
3162 return 0;
3166 void HELPER(v7m_msr)(CPUARMState *env, uint32_t reg, uint32_t val)
3168 switch (reg) {
3169 case 0: /* APSR */
3170 xpsr_write(env, val, 0xf8000000);
3171 break;
3172 case 1: /* IAPSR */
3173 xpsr_write(env, val, 0xf8000000);
3174 break;
3175 case 2: /* EAPSR */
3176 xpsr_write(env, val, 0xfe00fc00);
3177 break;
3178 case 3: /* xPSR */
3179 xpsr_write(env, val, 0xfe00fc00);
3180 break;
3181 case 5: /* IPSR */
3182 /* IPSR bits are readonly. */
3183 break;
3184 case 6: /* EPSR */
3185 xpsr_write(env, val, 0x0600fc00);
3186 break;
3187 case 7: /* IEPSR */
3188 xpsr_write(env, val, 0x0600fc00);
3189 break;
3190 case 8: /* MSP */
3191 if (env->v7m.current_sp)
3192 env->v7m.other_sp = val;
3193 else
3194 env->regs[13] = val;
3195 break;
3196 case 9: /* PSP */
3197 if (env->v7m.current_sp)
3198 env->regs[13] = val;
3199 else
3200 env->v7m.other_sp = val;
3201 break;
3202 case 16: /* PRIMASK */
3203 if (val & 1)
3204 env->uncached_cpsr |= CPSR_I;
3205 else
3206 env->uncached_cpsr &= ~CPSR_I;
3207 break;
3208 case 17: /* BASEPRI */
3209 env->v7m.basepri = val & 0xff;
3210 break;
3211 case 18: /* BASEPRI_MAX */
3212 val &= 0xff;
3213 if (val != 0 && (val < env->v7m.basepri || env->v7m.basepri == 0))
3214 env->v7m.basepri = val;
3215 break;
3216 case 19: /* FAULTMASK */
3217 if (val & 1)
3218 env->uncached_cpsr |= CPSR_F;
3219 else
3220 env->uncached_cpsr &= ~CPSR_F;
3221 break;
3222 case 20: /* CONTROL */
3223 env->v7m.control = val & 3;
3224 switch_v7m_sp(env, (val & 2) != 0);
3225 break;
3226 default:
3227 /* ??? For debugging only. */
3228 cpu_abort(env, "Unimplemented system register write (%d)\n", reg);
3229 return;
3233 #endif
3235 /* Note that signed overflow is undefined in C. The following routines are
3236 careful to use unsigned types where modulo arithmetic is required.
3237 Failure to do so _will_ break on newer gcc. */
3239 /* Signed saturating arithmetic. */
3241 /* Perform 16-bit signed saturating addition. */
3242 static inline uint16_t add16_sat(uint16_t a, uint16_t b)
3244 uint16_t res;
3246 res = a + b;
3247 if (((res ^ a) & 0x8000) && !((a ^ b) & 0x8000)) {
3248 if (a & 0x8000)
3249 res = 0x8000;
3250 else
3251 res = 0x7fff;
3253 return res;
3256 /* Perform 8-bit signed saturating addition. */
3257 static inline uint8_t add8_sat(uint8_t a, uint8_t b)
3259 uint8_t res;
3261 res = a + b;
3262 if (((res ^ a) & 0x80) && !((a ^ b) & 0x80)) {
3263 if (a & 0x80)
3264 res = 0x80;
3265 else
3266 res = 0x7f;
3268 return res;
3271 /* Perform 16-bit signed saturating subtraction. */
3272 static inline uint16_t sub16_sat(uint16_t a, uint16_t b)
3274 uint16_t res;
3276 res = a - b;
3277 if (((res ^ a) & 0x8000) && ((a ^ b) & 0x8000)) {
3278 if (a & 0x8000)
3279 res = 0x8000;
3280 else
3281 res = 0x7fff;
3283 return res;
3286 /* Perform 8-bit signed saturating subtraction. */
3287 static inline uint8_t sub8_sat(uint8_t a, uint8_t b)
3289 uint8_t res;
3291 res = a - b;
3292 if (((res ^ a) & 0x80) && ((a ^ b) & 0x80)) {
3293 if (a & 0x80)
3294 res = 0x80;
3295 else
3296 res = 0x7f;
3298 return res;
3301 #define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16);
3302 #define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16);
3303 #define ADD8(a, b, n) RESULT(add8_sat(a, b), n, 8);
3304 #define SUB8(a, b, n) RESULT(sub8_sat(a, b), n, 8);
3305 #define PFX q
3307 #include "op_addsub.h"
3309 /* Unsigned saturating arithmetic. */
3310 static inline uint16_t add16_usat(uint16_t a, uint16_t b)
3312 uint16_t res;
3313 res = a + b;
3314 if (res < a)
3315 res = 0xffff;
3316 return res;
3319 static inline uint16_t sub16_usat(uint16_t a, uint16_t b)
3321 if (a > b)
3322 return a - b;
3323 else
3324 return 0;
3327 static inline uint8_t add8_usat(uint8_t a, uint8_t b)
3329 uint8_t res;
3330 res = a + b;
3331 if (res < a)
3332 res = 0xff;
3333 return res;
3336 static inline uint8_t sub8_usat(uint8_t a, uint8_t b)
3338 if (a > b)
3339 return a - b;
3340 else
3341 return 0;
3344 #define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16);
3345 #define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16);
3346 #define ADD8(a, b, n) RESULT(add8_usat(a, b), n, 8);
3347 #define SUB8(a, b, n) RESULT(sub8_usat(a, b), n, 8);
3348 #define PFX uq
3350 #include "op_addsub.h"
3352 /* Signed modulo arithmetic. */
3353 #define SARITH16(a, b, n, op) do { \
3354 int32_t sum; \
3355 sum = (int32_t)(int16_t)(a) op (int32_t)(int16_t)(b); \
3356 RESULT(sum, n, 16); \
3357 if (sum >= 0) \
3358 ge |= 3 << (n * 2); \
3359 } while(0)
3361 #define SARITH8(a, b, n, op) do { \
3362 int32_t sum; \
3363 sum = (int32_t)(int8_t)(a) op (int32_t)(int8_t)(b); \
3364 RESULT(sum, n, 8); \
3365 if (sum >= 0) \
3366 ge |= 1 << n; \
3367 } while(0)
3370 #define ADD16(a, b, n) SARITH16(a, b, n, +)
3371 #define SUB16(a, b, n) SARITH16(a, b, n, -)
3372 #define ADD8(a, b, n) SARITH8(a, b, n, +)
3373 #define SUB8(a, b, n) SARITH8(a, b, n, -)
3374 #define PFX s
3375 #define ARITH_GE
3377 #include "op_addsub.h"
3379 /* Unsigned modulo arithmetic. */
3380 #define ADD16(a, b, n) do { \
3381 uint32_t sum; \
3382 sum = (uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b); \
3383 RESULT(sum, n, 16); \
3384 if ((sum >> 16) == 1) \
3385 ge |= 3 << (n * 2); \
3386 } while(0)
3388 #define ADD8(a, b, n) do { \
3389 uint32_t sum; \
3390 sum = (uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b); \
3391 RESULT(sum, n, 8); \
3392 if ((sum >> 8) == 1) \
3393 ge |= 1 << n; \
3394 } while(0)
3396 #define SUB16(a, b, n) do { \
3397 uint32_t sum; \
3398 sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \
3399 RESULT(sum, n, 16); \
3400 if ((sum >> 16) == 0) \
3401 ge |= 3 << (n * 2); \
3402 } while(0)
3404 #define SUB8(a, b, n) do { \
3405 uint32_t sum; \
3406 sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \
3407 RESULT(sum, n, 8); \
3408 if ((sum >> 8) == 0) \
3409 ge |= 1 << n; \
3410 } while(0)
3412 #define PFX u
3413 #define ARITH_GE
3415 #include "op_addsub.h"
3417 /* Halved signed arithmetic. */
3418 #define ADD16(a, b, n) \
3419 RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16)
3420 #define SUB16(a, b, n) \
3421 RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16)
3422 #define ADD8(a, b, n) \
3423 RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8)
3424 #define SUB8(a, b, n) \
3425 RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8)
3426 #define PFX sh
3428 #include "op_addsub.h"
3430 /* Halved unsigned arithmetic. */
3431 #define ADD16(a, b, n) \
3432 RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16)
3433 #define SUB16(a, b, n) \
3434 RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16)
3435 #define ADD8(a, b, n) \
3436 RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8)
3437 #define SUB8(a, b, n) \
3438 RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8)
3439 #define PFX uh
3441 #include "op_addsub.h"
3443 static inline uint8_t do_usad(uint8_t a, uint8_t b)
3445 if (a > b)
3446 return a - b;
3447 else
3448 return b - a;
3451 /* Unsigned sum of absolute byte differences. */
3452 uint32_t HELPER(usad8)(uint32_t a, uint32_t b)
3454 uint32_t sum;
3455 sum = do_usad(a, b);
3456 sum += do_usad(a >> 8, b >> 8);
3457 sum += do_usad(a >> 16, b >>16);
3458 sum += do_usad(a >> 24, b >> 24);
3459 return sum;
3462 /* For ARMv6 SEL instruction. */
3463 uint32_t HELPER(sel_flags)(uint32_t flags, uint32_t a, uint32_t b)
3465 uint32_t mask;
3467 mask = 0;
3468 if (flags & 1)
3469 mask |= 0xff;
3470 if (flags & 2)
3471 mask |= 0xff00;
3472 if (flags & 4)
3473 mask |= 0xff0000;
3474 if (flags & 8)
3475 mask |= 0xff000000;
3476 return (a & mask) | (b & ~mask);
3479 /* VFP support. We follow the convention used for VFP instructions:
3480 Single precision routines have a "s" suffix, double precision a
3481 "d" suffix. */
3483 /* Convert host exception flags to vfp form. */
3484 static inline int vfp_exceptbits_from_host(int host_bits)
3486 int target_bits = 0;
3488 if (host_bits & float_flag_invalid)
3489 target_bits |= 1;
3490 if (host_bits & float_flag_divbyzero)
3491 target_bits |= 2;
3492 if (host_bits & float_flag_overflow)
3493 target_bits |= 4;
3494 if (host_bits & (float_flag_underflow | float_flag_output_denormal))
3495 target_bits |= 8;
3496 if (host_bits & float_flag_inexact)
3497 target_bits |= 0x10;
3498 if (host_bits & float_flag_input_denormal)
3499 target_bits |= 0x80;
3500 return target_bits;
3503 uint32_t HELPER(vfp_get_fpscr)(CPUARMState *env)
3505 int i;
3506 uint32_t fpscr;
3508 fpscr = (env->vfp.xregs[ARM_VFP_FPSCR] & 0xffc8ffff)
3509 | (env->vfp.vec_len << 16)
3510 | (env->vfp.vec_stride << 20);
3511 i = get_float_exception_flags(&env->vfp.fp_status);
3512 i |= get_float_exception_flags(&env->vfp.standard_fp_status);
3513 fpscr |= vfp_exceptbits_from_host(i);
3514 return fpscr;
3517 uint32_t vfp_get_fpscr(CPUARMState *env)
3519 return HELPER(vfp_get_fpscr)(env);
3522 /* Convert vfp exception flags to target form. */
3523 static inline int vfp_exceptbits_to_host(int target_bits)
3525 int host_bits = 0;
3527 if (target_bits & 1)
3528 host_bits |= float_flag_invalid;
3529 if (target_bits & 2)
3530 host_bits |= float_flag_divbyzero;
3531 if (target_bits & 4)
3532 host_bits |= float_flag_overflow;
3533 if (target_bits & 8)
3534 host_bits |= float_flag_underflow;
3535 if (target_bits & 0x10)
3536 host_bits |= float_flag_inexact;
3537 if (target_bits & 0x80)
3538 host_bits |= float_flag_input_denormal;
3539 return host_bits;
3542 void HELPER(vfp_set_fpscr)(CPUARMState *env, uint32_t val)
3544 int i;
3545 uint32_t changed;
3547 changed = env->vfp.xregs[ARM_VFP_FPSCR];
3548 env->vfp.xregs[ARM_VFP_FPSCR] = (val & 0xffc8ffff);
3549 env->vfp.vec_len = (val >> 16) & 7;
3550 env->vfp.vec_stride = (val >> 20) & 3;
3552 changed ^= val;
3553 if (changed & (3 << 22)) {
3554 i = (val >> 22) & 3;
3555 switch (i) {
3556 case 0:
3557 i = float_round_nearest_even;
3558 break;
3559 case 1:
3560 i = float_round_up;
3561 break;
3562 case 2:
3563 i = float_round_down;
3564 break;
3565 case 3:
3566 i = float_round_to_zero;
3567 break;
3569 set_float_rounding_mode(i, &env->vfp.fp_status);
3571 if (changed & (1 << 24)) {
3572 set_flush_to_zero((val & (1 << 24)) != 0, &env->vfp.fp_status);
3573 set_flush_inputs_to_zero((val & (1 << 24)) != 0, &env->vfp.fp_status);
3575 if (changed & (1 << 25))
3576 set_default_nan_mode((val & (1 << 25)) != 0, &env->vfp.fp_status);
3578 i = vfp_exceptbits_to_host(val);
3579 set_float_exception_flags(i, &env->vfp.fp_status);
3580 set_float_exception_flags(0, &env->vfp.standard_fp_status);
3583 void vfp_set_fpscr(CPUARMState *env, uint32_t val)
3585 HELPER(vfp_set_fpscr)(env, val);
3588 #define VFP_HELPER(name, p) HELPER(glue(glue(vfp_,name),p))
3590 #define VFP_BINOP(name) \
3591 float32 VFP_HELPER(name, s)(float32 a, float32 b, void *fpstp) \
3593 float_status *fpst = fpstp; \
3594 return float32_ ## name(a, b, fpst); \
3596 float64 VFP_HELPER(name, d)(float64 a, float64 b, void *fpstp) \
3598 float_status *fpst = fpstp; \
3599 return float64_ ## name(a, b, fpst); \
3601 VFP_BINOP(add)
3602 VFP_BINOP(sub)
3603 VFP_BINOP(mul)
3604 VFP_BINOP(div)
3605 #undef VFP_BINOP
3607 float32 VFP_HELPER(neg, s)(float32 a)
3609 return float32_chs(a);
3612 float64 VFP_HELPER(neg, d)(float64 a)
3614 return float64_chs(a);
3617 float32 VFP_HELPER(abs, s)(float32 a)
3619 return float32_abs(a);
3622 float64 VFP_HELPER(abs, d)(float64 a)
3624 return float64_abs(a);
3627 float32 VFP_HELPER(sqrt, s)(float32 a, CPUARMState *env)
3629 return float32_sqrt(a, &env->vfp.fp_status);
3632 float64 VFP_HELPER(sqrt, d)(float64 a, CPUARMState *env)
3634 return float64_sqrt(a, &env->vfp.fp_status);
3637 /* XXX: check quiet/signaling case */
3638 #define DO_VFP_cmp(p, type) \
3639 void VFP_HELPER(cmp, p)(type a, type b, CPUARMState *env) \
3641 uint32_t flags; \
3642 switch(type ## _compare_quiet(a, b, &env->vfp.fp_status)) { \
3643 case 0: flags = 0x6; break; \
3644 case -1: flags = 0x8; break; \
3645 case 1: flags = 0x2; break; \
3646 default: case 2: flags = 0x3; break; \
3648 env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \
3649 | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \
3651 void VFP_HELPER(cmpe, p)(type a, type b, CPUARMState *env) \
3653 uint32_t flags; \
3654 switch(type ## _compare(a, b, &env->vfp.fp_status)) { \
3655 case 0: flags = 0x6; break; \
3656 case -1: flags = 0x8; break; \
3657 case 1: flags = 0x2; break; \
3658 default: case 2: flags = 0x3; break; \
3660 env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \
3661 | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \
3663 DO_VFP_cmp(s, float32)
3664 DO_VFP_cmp(d, float64)
3665 #undef DO_VFP_cmp
3667 /* Integer to float and float to integer conversions */
3669 #define CONV_ITOF(name, fsz, sign) \
3670 float##fsz HELPER(name)(uint32_t x, void *fpstp) \
3672 float_status *fpst = fpstp; \
3673 return sign##int32_to_##float##fsz((sign##int32_t)x, fpst); \
3676 #define CONV_FTOI(name, fsz, sign, round) \
3677 uint32_t HELPER(name)(float##fsz x, void *fpstp) \
3679 float_status *fpst = fpstp; \
3680 if (float##fsz##_is_any_nan(x)) { \
3681 float_raise(float_flag_invalid, fpst); \
3682 return 0; \
3684 return float##fsz##_to_##sign##int32##round(x, fpst); \
3687 #define FLOAT_CONVS(name, p, fsz, sign) \
3688 CONV_ITOF(vfp_##name##to##p, fsz, sign) \
3689 CONV_FTOI(vfp_to##name##p, fsz, sign, ) \
3690 CONV_FTOI(vfp_to##name##z##p, fsz, sign, _round_to_zero)
3692 FLOAT_CONVS(si, s, 32, )
3693 FLOAT_CONVS(si, d, 64, )
3694 FLOAT_CONVS(ui, s, 32, u)
3695 FLOAT_CONVS(ui, d, 64, u)
3697 #undef CONV_ITOF
3698 #undef CONV_FTOI
3699 #undef FLOAT_CONVS
3701 /* floating point conversion */
3702 float64 VFP_HELPER(fcvtd, s)(float32 x, CPUARMState *env)
3704 float64 r = float32_to_float64(x, &env->vfp.fp_status);
3705 /* ARM requires that S<->D conversion of any kind of NaN generates
3706 * a quiet NaN by forcing the most significant frac bit to 1.
3708 return float64_maybe_silence_nan(r);
3711 float32 VFP_HELPER(fcvts, d)(float64 x, CPUARMState *env)
3713 float32 r = float64_to_float32(x, &env->vfp.fp_status);
3714 /* ARM requires that S<->D conversion of any kind of NaN generates
3715 * a quiet NaN by forcing the most significant frac bit to 1.
3717 return float32_maybe_silence_nan(r);
3720 /* VFP3 fixed point conversion. */
3721 #define VFP_CONV_FIX(name, p, fsz, itype, sign) \
3722 float##fsz HELPER(vfp_##name##to##p)(uint##fsz##_t x, uint32_t shift, \
3723 void *fpstp) \
3725 float_status *fpst = fpstp; \
3726 float##fsz tmp; \
3727 tmp = sign##int32_to_##float##fsz((itype##_t)x, fpst); \
3728 return float##fsz##_scalbn(tmp, -(int)shift, fpst); \
3730 uint##fsz##_t HELPER(vfp_to##name##p)(float##fsz x, uint32_t shift, \
3731 void *fpstp) \
3733 float_status *fpst = fpstp; \
3734 float##fsz tmp; \
3735 if (float##fsz##_is_any_nan(x)) { \
3736 float_raise(float_flag_invalid, fpst); \
3737 return 0; \
3739 tmp = float##fsz##_scalbn(x, shift, fpst); \
3740 return float##fsz##_to_##itype##_round_to_zero(tmp, fpst); \
3743 VFP_CONV_FIX(sh, d, 64, int16, )
3744 VFP_CONV_FIX(sl, d, 64, int32, )
3745 VFP_CONV_FIX(uh, d, 64, uint16, u)
3746 VFP_CONV_FIX(ul, d, 64, uint32, u)
3747 VFP_CONV_FIX(sh, s, 32, int16, )
3748 VFP_CONV_FIX(sl, s, 32, int32, )
3749 VFP_CONV_FIX(uh, s, 32, uint16, u)
3750 VFP_CONV_FIX(ul, s, 32, uint32, u)
3751 #undef VFP_CONV_FIX
3753 /* Half precision conversions. */
3754 static float32 do_fcvt_f16_to_f32(uint32_t a, CPUARMState *env, float_status *s)
3756 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
3757 float32 r = float16_to_float32(make_float16(a), ieee, s);
3758 if (ieee) {
3759 return float32_maybe_silence_nan(r);
3761 return r;
3764 static uint32_t do_fcvt_f32_to_f16(float32 a, CPUARMState *env, float_status *s)
3766 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
3767 float16 r = float32_to_float16(a, ieee, s);
3768 if (ieee) {
3769 r = float16_maybe_silence_nan(r);
3771 return float16_val(r);
3774 float32 HELPER(neon_fcvt_f16_to_f32)(uint32_t a, CPUARMState *env)
3776 return do_fcvt_f16_to_f32(a, env, &env->vfp.standard_fp_status);
3779 uint32_t HELPER(neon_fcvt_f32_to_f16)(float32 a, CPUARMState *env)
3781 return do_fcvt_f32_to_f16(a, env, &env->vfp.standard_fp_status);
3784 float32 HELPER(vfp_fcvt_f16_to_f32)(uint32_t a, CPUARMState *env)
3786 return do_fcvt_f16_to_f32(a, env, &env->vfp.fp_status);
3789 uint32_t HELPER(vfp_fcvt_f32_to_f16)(float32 a, CPUARMState *env)
3791 return do_fcvt_f32_to_f16(a, env, &env->vfp.fp_status);
3794 #define float32_two make_float32(0x40000000)
3795 #define float32_three make_float32(0x40400000)
3796 #define float32_one_point_five make_float32(0x3fc00000)
3798 float32 HELPER(recps_f32)(float32 a, float32 b, CPUARMState *env)
3800 float_status *s = &env->vfp.standard_fp_status;
3801 if ((float32_is_infinity(a) && float32_is_zero_or_denormal(b)) ||
3802 (float32_is_infinity(b) && float32_is_zero_or_denormal(a))) {
3803 if (!(float32_is_zero(a) || float32_is_zero(b))) {
3804 float_raise(float_flag_input_denormal, s);
3806 return float32_two;
3808 return float32_sub(float32_two, float32_mul(a, b, s), s);
3811 float32 HELPER(rsqrts_f32)(float32 a, float32 b, CPUARMState *env)
3813 float_status *s = &env->vfp.standard_fp_status;
3814 float32 product;
3815 if ((float32_is_infinity(a) && float32_is_zero_or_denormal(b)) ||
3816 (float32_is_infinity(b) && float32_is_zero_or_denormal(a))) {
3817 if (!(float32_is_zero(a) || float32_is_zero(b))) {
3818 float_raise(float_flag_input_denormal, s);
3820 return float32_one_point_five;
3822 product = float32_mul(a, b, s);
3823 return float32_div(float32_sub(float32_three, product, s), float32_two, s);
3826 /* NEON helpers. */
3828 /* Constants 256 and 512 are used in some helpers; we avoid relying on
3829 * int->float conversions at run-time. */
3830 #define float64_256 make_float64(0x4070000000000000LL)
3831 #define float64_512 make_float64(0x4080000000000000LL)
3833 /* The algorithm that must be used to calculate the estimate
3834 * is specified by the ARM ARM.
3836 static float64 recip_estimate(float64 a, CPUARMState *env)
3838 /* These calculations mustn't set any fp exception flags,
3839 * so we use a local copy of the fp_status.
3841 float_status dummy_status = env->vfp.standard_fp_status;
3842 float_status *s = &dummy_status;
3843 /* q = (int)(a * 512.0) */
3844 float64 q = float64_mul(float64_512, a, s);
3845 int64_t q_int = float64_to_int64_round_to_zero(q, s);
3847 /* r = 1.0 / (((double)q + 0.5) / 512.0) */
3848 q = int64_to_float64(q_int, s);
3849 q = float64_add(q, float64_half, s);
3850 q = float64_div(q, float64_512, s);
3851 q = float64_div(float64_one, q, s);
3853 /* s = (int)(256.0 * r + 0.5) */
3854 q = float64_mul(q, float64_256, s);
3855 q = float64_add(q, float64_half, s);
3856 q_int = float64_to_int64_round_to_zero(q, s);
3858 /* return (double)s / 256.0 */
3859 return float64_div(int64_to_float64(q_int, s), float64_256, s);
3862 float32 HELPER(recpe_f32)(float32 a, CPUARMState *env)
3864 float_status *s = &env->vfp.standard_fp_status;
3865 float64 f64;
3866 uint32_t val32 = float32_val(a);
3868 int result_exp;
3869 int a_exp = (val32 & 0x7f800000) >> 23;
3870 int sign = val32 & 0x80000000;
3872 if (float32_is_any_nan(a)) {
3873 if (float32_is_signaling_nan(a)) {
3874 float_raise(float_flag_invalid, s);
3876 return float32_default_nan;
3877 } else if (float32_is_infinity(a)) {
3878 return float32_set_sign(float32_zero, float32_is_neg(a));
3879 } else if (float32_is_zero_or_denormal(a)) {
3880 if (!float32_is_zero(a)) {
3881 float_raise(float_flag_input_denormal, s);
3883 float_raise(float_flag_divbyzero, s);
3884 return float32_set_sign(float32_infinity, float32_is_neg(a));
3885 } else if (a_exp >= 253) {
3886 float_raise(float_flag_underflow, s);
3887 return float32_set_sign(float32_zero, float32_is_neg(a));
3890 f64 = make_float64((0x3feULL << 52)
3891 | ((int64_t)(val32 & 0x7fffff) << 29));
3893 result_exp = 253 - a_exp;
3895 f64 = recip_estimate(f64, env);
3897 val32 = sign
3898 | ((result_exp & 0xff) << 23)
3899 | ((float64_val(f64) >> 29) & 0x7fffff);
3900 return make_float32(val32);
3903 /* The algorithm that must be used to calculate the estimate
3904 * is specified by the ARM ARM.
3906 static float64 recip_sqrt_estimate(float64 a, CPUARMState *env)
3908 /* These calculations mustn't set any fp exception flags,
3909 * so we use a local copy of the fp_status.
3911 float_status dummy_status = env->vfp.standard_fp_status;
3912 float_status *s = &dummy_status;
3913 float64 q;
3914 int64_t q_int;
3916 if (float64_lt(a, float64_half, s)) {
3917 /* range 0.25 <= a < 0.5 */
3919 /* a in units of 1/512 rounded down */
3920 /* q0 = (int)(a * 512.0); */
3921 q = float64_mul(float64_512, a, s);
3922 q_int = float64_to_int64_round_to_zero(q, s);
3924 /* reciprocal root r */
3925 /* r = 1.0 / sqrt(((double)q0 + 0.5) / 512.0); */
3926 q = int64_to_float64(q_int, s);
3927 q = float64_add(q, float64_half, s);
3928 q = float64_div(q, float64_512, s);
3929 q = float64_sqrt(q, s);
3930 q = float64_div(float64_one, q, s);
3931 } else {
3932 /* range 0.5 <= a < 1.0 */
3934 /* a in units of 1/256 rounded down */
3935 /* q1 = (int)(a * 256.0); */
3936 q = float64_mul(float64_256, a, s);
3937 int64_t q_int = float64_to_int64_round_to_zero(q, s);
3939 /* reciprocal root r */
3940 /* r = 1.0 /sqrt(((double)q1 + 0.5) / 256); */
3941 q = int64_to_float64(q_int, s);
3942 q = float64_add(q, float64_half, s);
3943 q = float64_div(q, float64_256, s);
3944 q = float64_sqrt(q, s);
3945 q = float64_div(float64_one, q, s);
3947 /* r in units of 1/256 rounded to nearest */
3948 /* s = (int)(256.0 * r + 0.5); */
3950 q = float64_mul(q, float64_256,s );
3951 q = float64_add(q, float64_half, s);
3952 q_int = float64_to_int64_round_to_zero(q, s);
3954 /* return (double)s / 256.0;*/
3955 return float64_div(int64_to_float64(q_int, s), float64_256, s);
3958 float32 HELPER(rsqrte_f32)(float32 a, CPUARMState *env)
3960 float_status *s = &env->vfp.standard_fp_status;
3961 int result_exp;
3962 float64 f64;
3963 uint32_t val;
3964 uint64_t val64;
3966 val = float32_val(a);
3968 if (float32_is_any_nan(a)) {
3969 if (float32_is_signaling_nan(a)) {
3970 float_raise(float_flag_invalid, s);
3972 return float32_default_nan;
3973 } else if (float32_is_zero_or_denormal(a)) {
3974 if (!float32_is_zero(a)) {
3975 float_raise(float_flag_input_denormal, s);
3977 float_raise(float_flag_divbyzero, s);
3978 return float32_set_sign(float32_infinity, float32_is_neg(a));
3979 } else if (float32_is_neg(a)) {
3980 float_raise(float_flag_invalid, s);
3981 return float32_default_nan;
3982 } else if (float32_is_infinity(a)) {
3983 return float32_zero;
3986 /* Normalize to a double-precision value between 0.25 and 1.0,
3987 * preserving the parity of the exponent. */
3988 if ((val & 0x800000) == 0) {
3989 f64 = make_float64(((uint64_t)(val & 0x80000000) << 32)
3990 | (0x3feULL << 52)
3991 | ((uint64_t)(val & 0x7fffff) << 29));
3992 } else {
3993 f64 = make_float64(((uint64_t)(val & 0x80000000) << 32)
3994 | (0x3fdULL << 52)
3995 | ((uint64_t)(val & 0x7fffff) << 29));
3998 result_exp = (380 - ((val & 0x7f800000) >> 23)) / 2;
4000 f64 = recip_sqrt_estimate(f64, env);
4002 val64 = float64_val(f64);
4004 val = ((result_exp & 0xff) << 23)
4005 | ((val64 >> 29) & 0x7fffff);
4006 return make_float32(val);
4009 uint32_t HELPER(recpe_u32)(uint32_t a, CPUARMState *env)
4011 float64 f64;
4013 if ((a & 0x80000000) == 0) {
4014 return 0xffffffff;
4017 f64 = make_float64((0x3feULL << 52)
4018 | ((int64_t)(a & 0x7fffffff) << 21));
4020 f64 = recip_estimate (f64, env);
4022 return 0x80000000 | ((float64_val(f64) >> 21) & 0x7fffffff);
4025 uint32_t HELPER(rsqrte_u32)(uint32_t a, CPUARMState *env)
4027 float64 f64;
4029 if ((a & 0xc0000000) == 0) {
4030 return 0xffffffff;
4033 if (a & 0x80000000) {
4034 f64 = make_float64((0x3feULL << 52)
4035 | ((uint64_t)(a & 0x7fffffff) << 21));
4036 } else { /* bits 31-30 == '01' */
4037 f64 = make_float64((0x3fdULL << 52)
4038 | ((uint64_t)(a & 0x3fffffff) << 22));
4041 f64 = recip_sqrt_estimate(f64, env);
4043 return 0x80000000 | ((float64_val(f64) >> 21) & 0x7fffffff);
4046 /* VFPv4 fused multiply-accumulate */
4047 float32 VFP_HELPER(muladd, s)(float32 a, float32 b, float32 c, void *fpstp)
4049 float_status *fpst = fpstp;
4050 return float32_muladd(a, b, c, 0, fpst);
4053 float64 VFP_HELPER(muladd, d)(float64 a, float64 b, float64 c, void *fpstp)
4055 float_status *fpst = fpstp;
4056 return float64_muladd(a, b, c, 0, fpst);