]> Git Repo - qemu.git/blame - target/arm/helper.c
arm/translate-a64: add FP16 FSQRT to simd_two_reg_misc_fp16
[qemu.git] / target / arm / helper.c
CommitLineData
74c21bd0 1#include "qemu/osdep.h"
194cbc49 2#include "trace.h"
b5ff1b31 3#include "cpu.h"
ccd38087 4#include "internals.h"
022c62cb 5#include "exec/gdbstub.h"
2ef6175a 6#include "exec/helper-proto.h"
1de7afc9 7#include "qemu/host-utils.h"
78027bb6 8#include "sysemu/arch_init.h"
9c17d615 9#include "sysemu/sysemu.h"
1de7afc9 10#include "qemu/bitops.h"
eb0ecd5a 11#include "qemu/crc32c.h"
63c91552 12#include "exec/exec-all.h"
f08b6170 13#include "exec/cpu_ldst.h"
1d854765 14#include "arm_ldst.h"
eb0ecd5a 15#include <zlib.h> /* For crc32 */
cfe67cef 16#include "exec/semihost.h"
f3a9b694 17#include "sysemu/kvm.h"
24f91e81 18#include "fpu/softfloat.h"
0b03bdfc 19
352c98e5
LV
20#define ARM_CPU_FREQ 1000000000 /* FIXME: 1 GHz, should be configurable */
21
4a501606 22#ifndef CONFIG_USER_ONLY
5b2d261d
AB
23/* Cacheability and shareability attributes for a memory access */
24typedef struct ARMCacheAttrs {
25 unsigned int attrs:8; /* as in the MAIR register encoding */
26 unsigned int shareability:2; /* as in the SH field of the VMSAv8-64 PTEs */
27} ARMCacheAttrs;
28
af51f566 29static bool get_phys_addr(CPUARMState *env, target_ulong address,
03ae85f8 30 MMUAccessType access_type, ARMMMUIdx mmu_idx,
af51f566 31 hwaddr *phys_ptr, MemTxAttrs *attrs, int *prot,
bc52bfeb 32 target_ulong *page_size,
5b2d261d 33 ARMMMUFaultInfo *fi, ARMCacheAttrs *cacheattrs);
7c2cb42b 34
37785977 35static bool get_phys_addr_lpae(CPUARMState *env, target_ulong address,
03ae85f8 36 MMUAccessType access_type, ARMMMUIdx mmu_idx,
37785977 37 hwaddr *phys_ptr, MemTxAttrs *txattrs, int *prot,
da909b2c 38 target_ulong *page_size_ptr,
5b2d261d 39 ARMMMUFaultInfo *fi, ARMCacheAttrs *cacheattrs);
37785977 40
35337cc3
PM
41/* Security attributes for an address, as returned by v8m_security_lookup. */
42typedef struct V8M_SAttributes {
43 bool ns;
44 bool nsc;
45 uint8_t sregion;
46 bool srvalid;
47 uint8_t iregion;
48 bool irvalid;
49} V8M_SAttributes;
50
333e10c5
PM
51static void v8m_security_lookup(CPUARMState *env, uint32_t address,
52 MMUAccessType access_type, ARMMMUIdx mmu_idx,
53 V8M_SAttributes *sattrs);
54
7c2cb42b
AF
55/* Definitions for the PMCCNTR and PMCR registers */
56#define PMCRD 0x8
57#define PMCRC 0x4
58#define PMCRE 0x1
4a501606
PM
59#endif
60
0ecb72a5 61static int vfp_gdb_get_reg(CPUARMState *env, uint8_t *buf, int reg)
56aebc89
PB
62{
63 int nregs;
64
65 /* VFP data registers are always little-endian. */
66 nregs = arm_feature(env, ARM_FEATURE_VFP3) ? 32 : 16;
67 if (reg < nregs) {
9a2b5256 68 stq_le_p(buf, *aa32_vfp_dreg(env, reg));
56aebc89
PB
69 return 8;
70 }
71 if (arm_feature(env, ARM_FEATURE_NEON)) {
72 /* Aliases for Q regs. */
73 nregs += 16;
74 if (reg < nregs) {
9a2b5256
RH
75 uint64_t *q = aa32_vfp_qreg(env, reg - 32);
76 stq_le_p(buf, q[0]);
77 stq_le_p(buf + 8, q[1]);
56aebc89
PB
78 return 16;
79 }
80 }
81 switch (reg - nregs) {
82 case 0: stl_p(buf, env->vfp.xregs[ARM_VFP_FPSID]); return 4;
83 case 1: stl_p(buf, env->vfp.xregs[ARM_VFP_FPSCR]); return 4;
84 case 2: stl_p(buf, env->vfp.xregs[ARM_VFP_FPEXC]); return 4;
85 }
86 return 0;
87}
88
0ecb72a5 89static int vfp_gdb_set_reg(CPUARMState *env, uint8_t *buf, int reg)
56aebc89
PB
90{
91 int nregs;
92
93 nregs = arm_feature(env, ARM_FEATURE_VFP3) ? 32 : 16;
94 if (reg < nregs) {
9a2b5256 95 *aa32_vfp_dreg(env, reg) = ldq_le_p(buf);
56aebc89
PB
96 return 8;
97 }
98 if (arm_feature(env, ARM_FEATURE_NEON)) {
99 nregs += 16;
100 if (reg < nregs) {
9a2b5256
RH
101 uint64_t *q = aa32_vfp_qreg(env, reg - 32);
102 q[0] = ldq_le_p(buf);
103 q[1] = ldq_le_p(buf + 8);
56aebc89
PB
104 return 16;
105 }
106 }
107 switch (reg - nregs) {
108 case 0: env->vfp.xregs[ARM_VFP_FPSID] = ldl_p(buf); return 4;
109 case 1: env->vfp.xregs[ARM_VFP_FPSCR] = ldl_p(buf); return 4;
71b3c3de 110 case 2: env->vfp.xregs[ARM_VFP_FPEXC] = ldl_p(buf) & (1 << 30); return 4;
56aebc89
PB
111 }
112 return 0;
113}
114
6a669427
PM
115static int aarch64_fpu_gdb_get_reg(CPUARMState *env, uint8_t *buf, int reg)
116{
117 switch (reg) {
118 case 0 ... 31:
119 /* 128 bit FP register */
9a2b5256
RH
120 {
121 uint64_t *q = aa64_vfp_qreg(env, reg);
122 stq_le_p(buf, q[0]);
123 stq_le_p(buf + 8, q[1]);
124 return 16;
125 }
6a669427
PM
126 case 32:
127 /* FPSR */
128 stl_p(buf, vfp_get_fpsr(env));
129 return 4;
130 case 33:
131 /* FPCR */
132 stl_p(buf, vfp_get_fpcr(env));
133 return 4;
134 default:
135 return 0;
136 }
137}
138
139static int aarch64_fpu_gdb_set_reg(CPUARMState *env, uint8_t *buf, int reg)
140{
141 switch (reg) {
142 case 0 ... 31:
143 /* 128 bit FP register */
9a2b5256
RH
144 {
145 uint64_t *q = aa64_vfp_qreg(env, reg);
146 q[0] = ldq_le_p(buf);
147 q[1] = ldq_le_p(buf + 8);
148 return 16;
149 }
6a669427
PM
150 case 32:
151 /* FPSR */
152 vfp_set_fpsr(env, ldl_p(buf));
153 return 4;
154 case 33:
155 /* FPCR */
156 vfp_set_fpcr(env, ldl_p(buf));
157 return 4;
158 default:
159 return 0;
160 }
161}
162
c4241c7d 163static uint64_t raw_read(CPUARMState *env, const ARMCPRegInfo *ri)
d4e6df63 164{
375421cc 165 assert(ri->fieldoffset);
67ed771d 166 if (cpreg_field_is_64bit(ri)) {
c4241c7d 167 return CPREG_FIELD64(env, ri);
22d9e1a9 168 } else {
c4241c7d 169 return CPREG_FIELD32(env, ri);
22d9e1a9 170 }
d4e6df63
PM
171}
172
c4241c7d
PM
173static void raw_write(CPUARMState *env, const ARMCPRegInfo *ri,
174 uint64_t value)
d4e6df63 175{
375421cc 176 assert(ri->fieldoffset);
67ed771d 177 if (cpreg_field_is_64bit(ri)) {
22d9e1a9
PM
178 CPREG_FIELD64(env, ri) = value;
179 } else {
180 CPREG_FIELD32(env, ri) = value;
181 }
d4e6df63
PM
182}
183
11f136ee
FA
184static void *raw_ptr(CPUARMState *env, const ARMCPRegInfo *ri)
185{
186 return (char *)env + ri->fieldoffset;
187}
188
49a66191 189uint64_t read_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri)
721fae12 190{
59a1c327 191 /* Raw read of a coprocessor register (as needed for migration, etc). */
721fae12 192 if (ri->type & ARM_CP_CONST) {
59a1c327 193 return ri->resetvalue;
721fae12 194 } else if (ri->raw_readfn) {
59a1c327 195 return ri->raw_readfn(env, ri);
721fae12 196 } else if (ri->readfn) {
59a1c327 197 return ri->readfn(env, ri);
721fae12 198 } else {
59a1c327 199 return raw_read(env, ri);
721fae12 200 }
721fae12
PM
201}
202
59a1c327 203static void write_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri,
7900e9f1 204 uint64_t v)
721fae12
PM
205{
206 /* Raw write of a coprocessor register (as needed for migration, etc).
721fae12
PM
207 * Note that constant registers are treated as write-ignored; the
208 * caller should check for success by whether a readback gives the
209 * value written.
210 */
211 if (ri->type & ARM_CP_CONST) {
59a1c327 212 return;
721fae12 213 } else if (ri->raw_writefn) {
c4241c7d 214 ri->raw_writefn(env, ri, v);
721fae12 215 } else if (ri->writefn) {
c4241c7d 216 ri->writefn(env, ri, v);
721fae12 217 } else {
afb2530f 218 raw_write(env, ri, v);
721fae12 219 }
721fae12
PM
220}
221
375421cc
PM
222static bool raw_accessors_invalid(const ARMCPRegInfo *ri)
223{
224 /* Return true if the regdef would cause an assertion if you called
225 * read_raw_cp_reg() or write_raw_cp_reg() on it (ie if it is a
226 * program bug for it not to have the NO_RAW flag).
227 * NB that returning false here doesn't necessarily mean that calling
228 * read/write_raw_cp_reg() is safe, because we can't distinguish "has
229 * read/write access functions which are safe for raw use" from "has
230 * read/write access functions which have side effects but has forgotten
231 * to provide raw access functions".
232 * The tests here line up with the conditions in read/write_raw_cp_reg()
233 * and assertions in raw_read()/raw_write().
234 */
235 if ((ri->type & ARM_CP_CONST) ||
236 ri->fieldoffset ||
237 ((ri->raw_writefn || ri->writefn) && (ri->raw_readfn || ri->readfn))) {
238 return false;
239 }
240 return true;
241}
242
721fae12
PM
243bool write_cpustate_to_list(ARMCPU *cpu)
244{
245 /* Write the coprocessor state from cpu->env to the (index,value) list. */
246 int i;
247 bool ok = true;
248
249 for (i = 0; i < cpu->cpreg_array_len; i++) {
250 uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
251 const ARMCPRegInfo *ri;
59a1c327 252
60322b39 253 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
721fae12
PM
254 if (!ri) {
255 ok = false;
256 continue;
257 }
7a0e58fa 258 if (ri->type & ARM_CP_NO_RAW) {
721fae12
PM
259 continue;
260 }
59a1c327 261 cpu->cpreg_values[i] = read_raw_cp_reg(&cpu->env, ri);
721fae12
PM
262 }
263 return ok;
264}
265
266bool write_list_to_cpustate(ARMCPU *cpu)
267{
268 int i;
269 bool ok = true;
270
271 for (i = 0; i < cpu->cpreg_array_len; i++) {
272 uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
273 uint64_t v = cpu->cpreg_values[i];
721fae12
PM
274 const ARMCPRegInfo *ri;
275
60322b39 276 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
721fae12
PM
277 if (!ri) {
278 ok = false;
279 continue;
280 }
7a0e58fa 281 if (ri->type & ARM_CP_NO_RAW) {
721fae12
PM
282 continue;
283 }
284 /* Write value and confirm it reads back as written
285 * (to catch read-only registers and partially read-only
286 * registers where the incoming migration value doesn't match)
287 */
59a1c327
PM
288 write_raw_cp_reg(&cpu->env, ri, v);
289 if (read_raw_cp_reg(&cpu->env, ri) != v) {
721fae12
PM
290 ok = false;
291 }
292 }
293 return ok;
294}
295
296static void add_cpreg_to_list(gpointer key, gpointer opaque)
297{
298 ARMCPU *cpu = opaque;
299 uint64_t regidx;
300 const ARMCPRegInfo *ri;
301
302 regidx = *(uint32_t *)key;
60322b39 303 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
721fae12 304
7a0e58fa 305 if (!(ri->type & (ARM_CP_NO_RAW|ARM_CP_ALIAS))) {
721fae12
PM
306 cpu->cpreg_indexes[cpu->cpreg_array_len] = cpreg_to_kvm_id(regidx);
307 /* The value array need not be initialized at this point */
308 cpu->cpreg_array_len++;
309 }
310}
311
312static void count_cpreg(gpointer key, gpointer opaque)
313{
314 ARMCPU *cpu = opaque;
315 uint64_t regidx;
316 const ARMCPRegInfo *ri;
317
318 regidx = *(uint32_t *)key;
60322b39 319 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
721fae12 320
7a0e58fa 321 if (!(ri->type & (ARM_CP_NO_RAW|ARM_CP_ALIAS))) {
721fae12
PM
322 cpu->cpreg_array_len++;
323 }
324}
325
326static gint cpreg_key_compare(gconstpointer a, gconstpointer b)
327{
cbf239b7
AR
328 uint64_t aidx = cpreg_to_kvm_id(*(uint32_t *)a);
329 uint64_t bidx = cpreg_to_kvm_id(*(uint32_t *)b);
721fae12 330
cbf239b7
AR
331 if (aidx > bidx) {
332 return 1;
333 }
334 if (aidx < bidx) {
335 return -1;
336 }
337 return 0;
721fae12
PM
338}
339
340void init_cpreg_list(ARMCPU *cpu)
341{
342 /* Initialise the cpreg_tuples[] array based on the cp_regs hash.
343 * Note that we require cpreg_tuples[] to be sorted by key ID.
344 */
57b6d95e 345 GList *keys;
721fae12
PM
346 int arraylen;
347
57b6d95e 348 keys = g_hash_table_get_keys(cpu->cp_regs);
721fae12
PM
349 keys = g_list_sort(keys, cpreg_key_compare);
350
351 cpu->cpreg_array_len = 0;
352
353 g_list_foreach(keys, count_cpreg, cpu);
354
355 arraylen = cpu->cpreg_array_len;
356 cpu->cpreg_indexes = g_new(uint64_t, arraylen);
357 cpu->cpreg_values = g_new(uint64_t, arraylen);
358 cpu->cpreg_vmstate_indexes = g_new(uint64_t, arraylen);
359 cpu->cpreg_vmstate_values = g_new(uint64_t, arraylen);
360 cpu->cpreg_vmstate_array_len = cpu->cpreg_array_len;
361 cpu->cpreg_array_len = 0;
362
363 g_list_foreach(keys, add_cpreg_to_list, cpu);
364
365 assert(cpu->cpreg_array_len == arraylen);
366
367 g_list_free(keys);
368}
369
68e9c2fe
EI
370/*
371 * Some registers are not accessible if EL3.NS=0 and EL3 is using AArch32 but
372 * they are accessible when EL3 is using AArch64 regardless of EL3.NS.
373 *
374 * access_el3_aa32ns: Used to check AArch32 register views.
375 * access_el3_aa32ns_aa64any: Used to check both AArch32/64 register views.
376 */
377static CPAccessResult access_el3_aa32ns(CPUARMState *env,
3f208fd7
PM
378 const ARMCPRegInfo *ri,
379 bool isread)
68e9c2fe
EI
380{
381 bool secure = arm_is_secure_below_el3(env);
382
383 assert(!arm_el_is_aa64(env, 3));
384 if (secure) {
385 return CP_ACCESS_TRAP_UNCATEGORIZED;
386 }
387 return CP_ACCESS_OK;
388}
389
390static CPAccessResult access_el3_aa32ns_aa64any(CPUARMState *env,
3f208fd7
PM
391 const ARMCPRegInfo *ri,
392 bool isread)
68e9c2fe
EI
393{
394 if (!arm_el_is_aa64(env, 3)) {
3f208fd7 395 return access_el3_aa32ns(env, ri, isread);
68e9c2fe
EI
396 }
397 return CP_ACCESS_OK;
398}
399
5513c3ab
PM
400/* Some secure-only AArch32 registers trap to EL3 if used from
401 * Secure EL1 (but are just ordinary UNDEF in other non-EL3 contexts).
402 * Note that an access from Secure EL1 can only happen if EL3 is AArch64.
403 * We assume that the .access field is set to PL1_RW.
404 */
405static CPAccessResult access_trap_aa32s_el1(CPUARMState *env,
3f208fd7
PM
406 const ARMCPRegInfo *ri,
407 bool isread)
5513c3ab
PM
408{
409 if (arm_current_el(env) == 3) {
410 return CP_ACCESS_OK;
411 }
412 if (arm_is_secure_below_el3(env)) {
413 return CP_ACCESS_TRAP_EL3;
414 }
415 /* This will be EL1 NS and EL2 NS, which just UNDEF */
416 return CP_ACCESS_TRAP_UNCATEGORIZED;
417}
418
187f678d
PM
419/* Check for traps to "powerdown debug" registers, which are controlled
420 * by MDCR.TDOSA
421 */
422static CPAccessResult access_tdosa(CPUARMState *env, const ARMCPRegInfo *ri,
423 bool isread)
424{
425 int el = arm_current_el(env);
426
427 if (el < 2 && (env->cp15.mdcr_el2 & MDCR_TDOSA)
428 && !arm_is_secure_below_el3(env)) {
429 return CP_ACCESS_TRAP_EL2;
430 }
431 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TDOSA)) {
432 return CP_ACCESS_TRAP_EL3;
433 }
434 return CP_ACCESS_OK;
435}
436
91b0a238
PM
437/* Check for traps to "debug ROM" registers, which are controlled
438 * by MDCR_EL2.TDRA for EL2 but by the more general MDCR_EL3.TDA for EL3.
439 */
440static CPAccessResult access_tdra(CPUARMState *env, const ARMCPRegInfo *ri,
441 bool isread)
442{
443 int el = arm_current_el(env);
444
445 if (el < 2 && (env->cp15.mdcr_el2 & MDCR_TDRA)
446 && !arm_is_secure_below_el3(env)) {
447 return CP_ACCESS_TRAP_EL2;
448 }
449 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TDA)) {
450 return CP_ACCESS_TRAP_EL3;
451 }
452 return CP_ACCESS_OK;
453}
454
d6c8cf81
PM
455/* Check for traps to general debug registers, which are controlled
456 * by MDCR_EL2.TDA for EL2 and MDCR_EL3.TDA for EL3.
457 */
458static CPAccessResult access_tda(CPUARMState *env, const ARMCPRegInfo *ri,
459 bool isread)
460{
461 int el = arm_current_el(env);
462
463 if (el < 2 && (env->cp15.mdcr_el2 & MDCR_TDA)
464 && !arm_is_secure_below_el3(env)) {
465 return CP_ACCESS_TRAP_EL2;
466 }
467 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TDA)) {
468 return CP_ACCESS_TRAP_EL3;
469 }
470 return CP_ACCESS_OK;
471}
472
1fce1ba9
PM
473/* Check for traps to performance monitor registers, which are controlled
474 * by MDCR_EL2.TPM for EL2 and MDCR_EL3.TPM for EL3.
475 */
476static CPAccessResult access_tpm(CPUARMState *env, const ARMCPRegInfo *ri,
477 bool isread)
478{
479 int el = arm_current_el(env);
480
481 if (el < 2 && (env->cp15.mdcr_el2 & MDCR_TPM)
482 && !arm_is_secure_below_el3(env)) {
483 return CP_ACCESS_TRAP_EL2;
484 }
485 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) {
486 return CP_ACCESS_TRAP_EL3;
487 }
488 return CP_ACCESS_OK;
489}
490
c4241c7d 491static void dacr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
c983fe6c 492{
00c8cb0a
AF
493 ARMCPU *cpu = arm_env_get_cpu(env);
494
8d5c773e 495 raw_write(env, ri, value);
d10eb08f 496 tlb_flush(CPU(cpu)); /* Flush TLB as domain not tracked in TLB */
c983fe6c
PM
497}
498
c4241c7d 499static void fcse_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
08de207b 500{
00c8cb0a
AF
501 ARMCPU *cpu = arm_env_get_cpu(env);
502
8d5c773e 503 if (raw_read(env, ri) != value) {
08de207b
PM
504 /* Unlike real hardware the qemu TLB uses virtual addresses,
505 * not modified virtual addresses, so this causes a TLB flush.
506 */
d10eb08f 507 tlb_flush(CPU(cpu));
8d5c773e 508 raw_write(env, ri, value);
08de207b 509 }
08de207b 510}
c4241c7d
PM
511
512static void contextidr_write(CPUARMState *env, const ARMCPRegInfo *ri,
513 uint64_t value)
08de207b 514{
00c8cb0a
AF
515 ARMCPU *cpu = arm_env_get_cpu(env);
516
452a0955 517 if (raw_read(env, ri) != value && !arm_feature(env, ARM_FEATURE_PMSA)
014406b5 518 && !extended_addresses_enabled(env)) {
08de207b
PM
519 /* For VMSA (when not using the LPAE long descriptor page table
520 * format) this register includes the ASID, so do a TLB flush.
521 * For PMSA it is purely a process ID and no action is needed.
522 */
d10eb08f 523 tlb_flush(CPU(cpu));
08de207b 524 }
8d5c773e 525 raw_write(env, ri, value);
08de207b
PM
526}
527
c4241c7d
PM
528static void tlbiall_write(CPUARMState *env, const ARMCPRegInfo *ri,
529 uint64_t value)
d929823f
PM
530{
531 /* Invalidate all (TLBIALL) */
00c8cb0a
AF
532 ARMCPU *cpu = arm_env_get_cpu(env);
533
d10eb08f 534 tlb_flush(CPU(cpu));
d929823f
PM
535}
536
c4241c7d
PM
537static void tlbimva_write(CPUARMState *env, const ARMCPRegInfo *ri,
538 uint64_t value)
d929823f
PM
539{
540 /* Invalidate single TLB entry by MVA and ASID (TLBIMVA) */
31b030d4
AF
541 ARMCPU *cpu = arm_env_get_cpu(env);
542
543 tlb_flush_page(CPU(cpu), value & TARGET_PAGE_MASK);
d929823f
PM
544}
545
c4241c7d
PM
546static void tlbiasid_write(CPUARMState *env, const ARMCPRegInfo *ri,
547 uint64_t value)
d929823f
PM
548{
549 /* Invalidate by ASID (TLBIASID) */
00c8cb0a
AF
550 ARMCPU *cpu = arm_env_get_cpu(env);
551
d10eb08f 552 tlb_flush(CPU(cpu));
d929823f
PM
553}
554
c4241c7d
PM
555static void tlbimvaa_write(CPUARMState *env, const ARMCPRegInfo *ri,
556 uint64_t value)
d929823f
PM
557{
558 /* Invalidate single entry by MVA, all ASIDs (TLBIMVAA) */
31b030d4
AF
559 ARMCPU *cpu = arm_env_get_cpu(env);
560
561 tlb_flush_page(CPU(cpu), value & TARGET_PAGE_MASK);
d929823f
PM
562}
563
fa439fc5
PM
564/* IS variants of TLB operations must affect all cores */
565static void tlbiall_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
566 uint64_t value)
567{
a67cf277 568 CPUState *cs = ENV_GET_CPU(env);
fa439fc5 569
a67cf277 570 tlb_flush_all_cpus_synced(cs);
fa439fc5
PM
571}
572
573static void tlbiasid_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
574 uint64_t value)
575{
a67cf277 576 CPUState *cs = ENV_GET_CPU(env);
fa439fc5 577
a67cf277 578 tlb_flush_all_cpus_synced(cs);
fa439fc5
PM
579}
580
581static void tlbimva_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
582 uint64_t value)
583{
a67cf277 584 CPUState *cs = ENV_GET_CPU(env);
fa439fc5 585
a67cf277 586 tlb_flush_page_all_cpus_synced(cs, value & TARGET_PAGE_MASK);
fa439fc5
PM
587}
588
589static void tlbimvaa_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
590 uint64_t value)
591{
a67cf277 592 CPUState *cs = ENV_GET_CPU(env);
fa439fc5 593
a67cf277 594 tlb_flush_page_all_cpus_synced(cs, value & TARGET_PAGE_MASK);
fa439fc5
PM
595}
596
541ef8c2
SS
597static void tlbiall_nsnh_write(CPUARMState *env, const ARMCPRegInfo *ri,
598 uint64_t value)
599{
600 CPUState *cs = ENV_GET_CPU(env);
601
0336cbf8 602 tlb_flush_by_mmuidx(cs,
8bd5c820
PM
603 ARMMMUIdxBit_S12NSE1 |
604 ARMMMUIdxBit_S12NSE0 |
605 ARMMMUIdxBit_S2NS);
541ef8c2
SS
606}
607
608static void tlbiall_nsnh_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
609 uint64_t value)
610{
a67cf277 611 CPUState *cs = ENV_GET_CPU(env);
541ef8c2 612
a67cf277 613 tlb_flush_by_mmuidx_all_cpus_synced(cs,
8bd5c820
PM
614 ARMMMUIdxBit_S12NSE1 |
615 ARMMMUIdxBit_S12NSE0 |
616 ARMMMUIdxBit_S2NS);
541ef8c2
SS
617}
618
619static void tlbiipas2_write(CPUARMState *env, const ARMCPRegInfo *ri,
620 uint64_t value)
621{
622 /* Invalidate by IPA. This has to invalidate any structures that
623 * contain only stage 2 translation information, but does not need
624 * to apply to structures that contain combined stage 1 and stage 2
625 * translation information.
626 * This must NOP if EL2 isn't implemented or SCR_EL3.NS is zero.
627 */
628 CPUState *cs = ENV_GET_CPU(env);
629 uint64_t pageaddr;
630
631 if (!arm_feature(env, ARM_FEATURE_EL2) || !(env->cp15.scr_el3 & SCR_NS)) {
632 return;
633 }
634
635 pageaddr = sextract64(value << 12, 0, 40);
636
8bd5c820 637 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_S2NS);
541ef8c2
SS
638}
639
640static void tlbiipas2_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
641 uint64_t value)
642{
a67cf277 643 CPUState *cs = ENV_GET_CPU(env);
541ef8c2
SS
644 uint64_t pageaddr;
645
646 if (!arm_feature(env, ARM_FEATURE_EL2) || !(env->cp15.scr_el3 & SCR_NS)) {
647 return;
648 }
649
650 pageaddr = sextract64(value << 12, 0, 40);
651
a67cf277 652 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
8bd5c820 653 ARMMMUIdxBit_S2NS);
541ef8c2
SS
654}
655
656static void tlbiall_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
657 uint64_t value)
658{
659 CPUState *cs = ENV_GET_CPU(env);
660
8bd5c820 661 tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_S1E2);
541ef8c2
SS
662}
663
664static void tlbiall_hyp_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
665 uint64_t value)
666{
a67cf277 667 CPUState *cs = ENV_GET_CPU(env);
541ef8c2 668
8bd5c820 669 tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_S1E2);
541ef8c2
SS
670}
671
672static void tlbimva_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
673 uint64_t value)
674{
675 CPUState *cs = ENV_GET_CPU(env);
676 uint64_t pageaddr = value & ~MAKE_64BIT_MASK(0, 12);
677
8bd5c820 678 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_S1E2);
541ef8c2
SS
679}
680
681static void tlbimva_hyp_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
682 uint64_t value)
683{
a67cf277 684 CPUState *cs = ENV_GET_CPU(env);
541ef8c2
SS
685 uint64_t pageaddr = value & ~MAKE_64BIT_MASK(0, 12);
686
a67cf277 687 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
8bd5c820 688 ARMMMUIdxBit_S1E2);
541ef8c2
SS
689}
690
e9aa6c21 691static const ARMCPRegInfo cp_reginfo[] = {
54bf36ed
FA
692 /* Define the secure and non-secure FCSE identifier CP registers
693 * separately because there is no secure bank in V8 (no _EL3). This allows
694 * the secure register to be properly reset and migrated. There is also no
695 * v8 EL1 version of the register so the non-secure instance stands alone.
696 */
697 { .name = "FCSEIDR(NS)",
698 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
699 .access = PL1_RW, .secure = ARM_CP_SECSTATE_NS,
700 .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_ns),
701 .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
702 { .name = "FCSEIDR(S)",
703 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
704 .access = PL1_RW, .secure = ARM_CP_SECSTATE_S,
705 .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_s),
d4e6df63 706 .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
54bf36ed
FA
707 /* Define the secure and non-secure context identifier CP registers
708 * separately because there is no secure bank in V8 (no _EL3). This allows
709 * the secure register to be properly reset and migrated. In the
710 * non-secure case, the 32-bit register will have reset and migration
711 * disabled during registration as it is handled by the 64-bit instance.
712 */
713 { .name = "CONTEXTIDR_EL1", .state = ARM_CP_STATE_BOTH,
014406b5 714 .opc0 = 3, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
54bf36ed
FA
715 .access = PL1_RW, .secure = ARM_CP_SECSTATE_NS,
716 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[1]),
717 .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
718 { .name = "CONTEXTIDR(S)", .state = ARM_CP_STATE_AA32,
719 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
720 .access = PL1_RW, .secure = ARM_CP_SECSTATE_S,
721 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_s),
d4e6df63 722 .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
9449fdf6
PM
723 REGINFO_SENTINEL
724};
725
726static const ARMCPRegInfo not_v8_cp_reginfo[] = {
727 /* NB: Some of these registers exist in v8 but with more precise
728 * definitions that don't use CP_ANY wildcards (mostly in v8_cp_reginfo[]).
729 */
730 /* MMU Domain access control / MPU write buffer control */
0c17d68c
FA
731 { .name = "DACR",
732 .cp = 15, .opc1 = CP_ANY, .crn = 3, .crm = CP_ANY, .opc2 = CP_ANY,
733 .access = PL1_RW, .resetvalue = 0,
734 .writefn = dacr_write, .raw_writefn = raw_write,
735 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
736 offsetoflow32(CPUARMState, cp15.dacr_ns) } },
a903c449
EI
737 /* ARMv7 allocates a range of implementation defined TLB LOCKDOWN regs.
738 * For v6 and v5, these mappings are overly broad.
4fdd17dd 739 */
a903c449
EI
740 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 0,
741 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
742 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 1,
743 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
744 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 4,
745 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
746 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 8,
4fdd17dd 747 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
c4804214
PM
748 /* Cache maintenance ops; some of this space may be overridden later. */
749 { .name = "CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
750 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
751 .type = ARM_CP_NOP | ARM_CP_OVERRIDE },
e9aa6c21
PM
752 REGINFO_SENTINEL
753};
754
7d57f408
PM
755static const ARMCPRegInfo not_v6_cp_reginfo[] = {
756 /* Not all pre-v6 cores implemented this WFI, so this is slightly
757 * over-broad.
758 */
759 { .name = "WFI_v5", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = 2,
760 .access = PL1_W, .type = ARM_CP_WFI },
761 REGINFO_SENTINEL
762};
763
764static const ARMCPRegInfo not_v7_cp_reginfo[] = {
765 /* Standard v6 WFI (also used in some pre-v6 cores); not in v7 (which
766 * is UNPREDICTABLE; we choose to NOP as most implementations do).
767 */
768 { .name = "WFI_v6", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
769 .access = PL1_W, .type = ARM_CP_WFI },
34f90529
PM
770 /* L1 cache lockdown. Not architectural in v6 and earlier but in practice
771 * implemented in 926, 946, 1026, 1136, 1176 and 11MPCore. StrongARM and
772 * OMAPCP will override this space.
773 */
774 { .name = "DLOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 0,
775 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_data),
776 .resetvalue = 0 },
777 { .name = "ILOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 1,
778 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_insn),
779 .resetvalue = 0 },
776d4e5c
PM
780 /* v6 doesn't have the cache ID registers but Linux reads them anyway */
781 { .name = "DUMMY", .cp = 15, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = CP_ANY,
7a0e58fa 782 .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 783 .resetvalue = 0 },
50300698
PM
784 /* We don't implement pre-v7 debug but most CPUs had at least a DBGDIDR;
785 * implementing it as RAZ means the "debug architecture version" bits
786 * will read as a reserved value, which should cause Linux to not try
787 * to use the debug hardware.
788 */
789 { .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0,
790 .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
995939a6
PM
791 /* MMU TLB control. Note that the wildcarding means we cover not just
792 * the unified TLB ops but also the dside/iside/inner-shareable variants.
793 */
794 { .name = "TLBIALL", .cp = 15, .crn = 8, .crm = CP_ANY,
795 .opc1 = CP_ANY, .opc2 = 0, .access = PL1_W, .writefn = tlbiall_write,
7a0e58fa 796 .type = ARM_CP_NO_RAW },
995939a6
PM
797 { .name = "TLBIMVA", .cp = 15, .crn = 8, .crm = CP_ANY,
798 .opc1 = CP_ANY, .opc2 = 1, .access = PL1_W, .writefn = tlbimva_write,
7a0e58fa 799 .type = ARM_CP_NO_RAW },
995939a6
PM
800 { .name = "TLBIASID", .cp = 15, .crn = 8, .crm = CP_ANY,
801 .opc1 = CP_ANY, .opc2 = 2, .access = PL1_W, .writefn = tlbiasid_write,
7a0e58fa 802 .type = ARM_CP_NO_RAW },
995939a6
PM
803 { .name = "TLBIMVAA", .cp = 15, .crn = 8, .crm = CP_ANY,
804 .opc1 = CP_ANY, .opc2 = 3, .access = PL1_W, .writefn = tlbimvaa_write,
7a0e58fa 805 .type = ARM_CP_NO_RAW },
a903c449
EI
806 { .name = "PRRR", .cp = 15, .crn = 10, .crm = 2,
807 .opc1 = 0, .opc2 = 0, .access = PL1_RW, .type = ARM_CP_NOP },
808 { .name = "NMRR", .cp = 15, .crn = 10, .crm = 2,
809 .opc1 = 0, .opc2 = 1, .access = PL1_RW, .type = ARM_CP_NOP },
7d57f408
PM
810 REGINFO_SENTINEL
811};
812
c4241c7d
PM
813static void cpacr_write(CPUARMState *env, const ARMCPRegInfo *ri,
814 uint64_t value)
2771db27 815{
f0aff255
FA
816 uint32_t mask = 0;
817
818 /* In ARMv8 most bits of CPACR_EL1 are RES0. */
819 if (!arm_feature(env, ARM_FEATURE_V8)) {
820 /* ARMv7 defines bits for unimplemented coprocessors as RAZ/WI.
821 * ASEDIS [31] and D32DIS [30] are both UNK/SBZP without VFP.
822 * TRCDIS [28] is RAZ/WI since we do not implement a trace macrocell.
823 */
824 if (arm_feature(env, ARM_FEATURE_VFP)) {
825 /* VFP coprocessor: cp10 & cp11 [23:20] */
826 mask |= (1 << 31) | (1 << 30) | (0xf << 20);
827
828 if (!arm_feature(env, ARM_FEATURE_NEON)) {
829 /* ASEDIS [31] bit is RAO/WI */
830 value |= (1 << 31);
831 }
832
833 /* VFPv3 and upwards with NEON implement 32 double precision
834 * registers (D0-D31).
835 */
836 if (!arm_feature(env, ARM_FEATURE_NEON) ||
837 !arm_feature(env, ARM_FEATURE_VFP3)) {
838 /* D32DIS [30] is RAO/WI if D16-31 are not implemented. */
839 value |= (1 << 30);
840 }
841 }
842 value &= mask;
2771db27 843 }
7ebd5f2e 844 env->cp15.cpacr_el1 = value;
2771db27
PM
845}
846
3f208fd7
PM
847static CPAccessResult cpacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
848 bool isread)
c6f19164
GB
849{
850 if (arm_feature(env, ARM_FEATURE_V8)) {
851 /* Check if CPACR accesses are to be trapped to EL2 */
852 if (arm_current_el(env) == 1 &&
853 (env->cp15.cptr_el[2] & CPTR_TCPAC) && !arm_is_secure(env)) {
854 return CP_ACCESS_TRAP_EL2;
855 /* Check if CPACR accesses are to be trapped to EL3 */
856 } else if (arm_current_el(env) < 3 &&
857 (env->cp15.cptr_el[3] & CPTR_TCPAC)) {
858 return CP_ACCESS_TRAP_EL3;
859 }
860 }
861
862 return CP_ACCESS_OK;
863}
864
3f208fd7
PM
865static CPAccessResult cptr_access(CPUARMState *env, const ARMCPRegInfo *ri,
866 bool isread)
c6f19164
GB
867{
868 /* Check if CPTR accesses are set to trap to EL3 */
869 if (arm_current_el(env) == 2 && (env->cp15.cptr_el[3] & CPTR_TCPAC)) {
870 return CP_ACCESS_TRAP_EL3;
871 }
872
873 return CP_ACCESS_OK;
874}
875
7d57f408
PM
876static const ARMCPRegInfo v6_cp_reginfo[] = {
877 /* prefetch by MVA in v6, NOP in v7 */
878 { .name = "MVA_prefetch",
879 .cp = 15, .crn = 7, .crm = 13, .opc1 = 0, .opc2 = 1,
880 .access = PL1_W, .type = ARM_CP_NOP },
6df99dec
SS
881 /* We need to break the TB after ISB to execute self-modifying code
882 * correctly and also to take any pending interrupts immediately.
883 * So use arm_cp_write_ignore() function instead of ARM_CP_NOP flag.
884 */
7d57f408 885 { .name = "ISB", .cp = 15, .crn = 7, .crm = 5, .opc1 = 0, .opc2 = 4,
6df99dec 886 .access = PL0_W, .type = ARM_CP_NO_RAW, .writefn = arm_cp_write_ignore },
091fd17c 887 { .name = "DSB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 4,
7d57f408 888 .access = PL0_W, .type = ARM_CP_NOP },
091fd17c 889 { .name = "DMB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 5,
7d57f408 890 .access = PL0_W, .type = ARM_CP_NOP },
06d76f31 891 { .name = "IFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 2,
6cd8a264 892 .access = PL1_RW,
b848ce2b
FA
893 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ifar_s),
894 offsetof(CPUARMState, cp15.ifar_ns) },
06d76f31
PM
895 .resetvalue = 0, },
896 /* Watchpoint Fault Address Register : should actually only be present
897 * for 1136, 1176, 11MPCore.
898 */
899 { .name = "WFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 1,
900 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0, },
34222fb8 901 { .name = "CPACR", .state = ARM_CP_STATE_BOTH, .opc0 = 3,
c6f19164 902 .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 2, .accessfn = cpacr_access,
7ebd5f2e 903 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.cpacr_el1),
2771db27 904 .resetvalue = 0, .writefn = cpacr_write },
7d57f408
PM
905 REGINFO_SENTINEL
906};
907
3f208fd7
PM
908static CPAccessResult pmreg_access(CPUARMState *env, const ARMCPRegInfo *ri,
909 bool isread)
200ac0ef 910{
3b163b01 911 /* Performance monitor registers user accessibility is controlled
1fce1ba9
PM
912 * by PMUSERENR. MDCR_EL2.TPM and MDCR_EL3.TPM allow configurable
913 * trapping to EL2 or EL3 for other accesses.
200ac0ef 914 */
1fce1ba9
PM
915 int el = arm_current_el(env);
916
6ecd0b6b 917 if (el == 0 && !(env->cp15.c9_pmuserenr & 1)) {
fcd25206 918 return CP_ACCESS_TRAP;
200ac0ef 919 }
1fce1ba9
PM
920 if (el < 2 && (env->cp15.mdcr_el2 & MDCR_TPM)
921 && !arm_is_secure_below_el3(env)) {
922 return CP_ACCESS_TRAP_EL2;
923 }
924 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) {
925 return CP_ACCESS_TRAP_EL3;
926 }
927
fcd25206 928 return CP_ACCESS_OK;
200ac0ef
PM
929}
930
6ecd0b6b
AB
931static CPAccessResult pmreg_access_xevcntr(CPUARMState *env,
932 const ARMCPRegInfo *ri,
933 bool isread)
934{
935 /* ER: event counter read trap control */
936 if (arm_feature(env, ARM_FEATURE_V8)
937 && arm_current_el(env) == 0
938 && (env->cp15.c9_pmuserenr & (1 << 3)) != 0
939 && isread) {
940 return CP_ACCESS_OK;
941 }
942
943 return pmreg_access(env, ri, isread);
944}
945
946static CPAccessResult pmreg_access_swinc(CPUARMState *env,
947 const ARMCPRegInfo *ri,
948 bool isread)
949{
950 /* SW: software increment write trap control */
951 if (arm_feature(env, ARM_FEATURE_V8)
952 && arm_current_el(env) == 0
953 && (env->cp15.c9_pmuserenr & (1 << 1)) != 0
954 && !isread) {
955 return CP_ACCESS_OK;
956 }
957
958 return pmreg_access(env, ri, isread);
959}
960
7c2cb42b 961#ifndef CONFIG_USER_ONLY
87124fde 962
6ecd0b6b
AB
963static CPAccessResult pmreg_access_selr(CPUARMState *env,
964 const ARMCPRegInfo *ri,
965 bool isread)
966{
967 /* ER: event counter read trap control */
968 if (arm_feature(env, ARM_FEATURE_V8)
969 && arm_current_el(env) == 0
970 && (env->cp15.c9_pmuserenr & (1 << 3)) != 0) {
971 return CP_ACCESS_OK;
972 }
973
974 return pmreg_access(env, ri, isread);
975}
976
977static CPAccessResult pmreg_access_ccntr(CPUARMState *env,
978 const ARMCPRegInfo *ri,
979 bool isread)
980{
981 /* CR: cycle counter read trap control */
982 if (arm_feature(env, ARM_FEATURE_V8)
983 && arm_current_el(env) == 0
984 && (env->cp15.c9_pmuserenr & (1 << 2)) != 0
985 && isread) {
986 return CP_ACCESS_OK;
987 }
988
989 return pmreg_access(env, ri, isread);
990}
991
87124fde
AF
992static inline bool arm_ccnt_enabled(CPUARMState *env)
993{
994 /* This does not support checking PMCCFILTR_EL0 register */
995
996 if (!(env->cp15.c9_pmcr & PMCRE)) {
997 return false;
998 }
999
1000 return true;
1001}
1002
ec7b4ce4
AF
1003void pmccntr_sync(CPUARMState *env)
1004{
1005 uint64_t temp_ticks;
1006
352c98e5
LV
1007 temp_ticks = muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
1008 ARM_CPU_FREQ, NANOSECONDS_PER_SECOND);
ec7b4ce4
AF
1009
1010 if (env->cp15.c9_pmcr & PMCRD) {
1011 /* Increment once every 64 processor clock cycles */
1012 temp_ticks /= 64;
1013 }
1014
1015 if (arm_ccnt_enabled(env)) {
1016 env->cp15.c15_ccnt = temp_ticks - env->cp15.c15_ccnt;
1017 }
1018}
1019
c4241c7d
PM
1020static void pmcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1021 uint64_t value)
200ac0ef 1022{
942a155b 1023 pmccntr_sync(env);
7c2cb42b
AF
1024
1025 if (value & PMCRC) {
1026 /* The counter has been reset */
1027 env->cp15.c15_ccnt = 0;
1028 }
1029
200ac0ef
PM
1030 /* only the DP, X, D and E bits are writable */
1031 env->cp15.c9_pmcr &= ~0x39;
1032 env->cp15.c9_pmcr |= (value & 0x39);
7c2cb42b 1033
942a155b 1034 pmccntr_sync(env);
7c2cb42b
AF
1035}
1036
1037static uint64_t pmccntr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1038{
c92c0687 1039 uint64_t total_ticks;
7c2cb42b 1040
942a155b 1041 if (!arm_ccnt_enabled(env)) {
7c2cb42b
AF
1042 /* Counter is disabled, do not change value */
1043 return env->cp15.c15_ccnt;
1044 }
1045
352c98e5
LV
1046 total_ticks = muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
1047 ARM_CPU_FREQ, NANOSECONDS_PER_SECOND);
7c2cb42b
AF
1048
1049 if (env->cp15.c9_pmcr & PMCRD) {
1050 /* Increment once every 64 processor clock cycles */
1051 total_ticks /= 64;
1052 }
1053 return total_ticks - env->cp15.c15_ccnt;
1054}
1055
6b040780
WH
1056static void pmselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1057 uint64_t value)
1058{
1059 /* The value of PMSELR.SEL affects the behavior of PMXEVTYPER and
1060 * PMXEVCNTR. We allow [0..31] to be written to PMSELR here; in the
1061 * meanwhile, we check PMSELR.SEL when PMXEVTYPER and PMXEVCNTR are
1062 * accessed.
1063 */
1064 env->cp15.c9_pmselr = value & 0x1f;
1065}
1066
7c2cb42b
AF
1067static void pmccntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1068 uint64_t value)
1069{
c92c0687 1070 uint64_t total_ticks;
7c2cb42b 1071
942a155b 1072 if (!arm_ccnt_enabled(env)) {
7c2cb42b
AF
1073 /* Counter is disabled, set the absolute value */
1074 env->cp15.c15_ccnt = value;
1075 return;
1076 }
1077
352c98e5
LV
1078 total_ticks = muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
1079 ARM_CPU_FREQ, NANOSECONDS_PER_SECOND);
7c2cb42b
AF
1080
1081 if (env->cp15.c9_pmcr & PMCRD) {
1082 /* Increment once every 64 processor clock cycles */
1083 total_ticks /= 64;
1084 }
1085 env->cp15.c15_ccnt = total_ticks - value;
200ac0ef 1086}
421c7ebd
PC
1087
1088static void pmccntr_write32(CPUARMState *env, const ARMCPRegInfo *ri,
1089 uint64_t value)
1090{
1091 uint64_t cur_val = pmccntr_read(env, NULL);
1092
1093 pmccntr_write(env, ri, deposit64(cur_val, 0, 32, value));
1094}
1095
ec7b4ce4
AF
1096#else /* CONFIG_USER_ONLY */
1097
1098void pmccntr_sync(CPUARMState *env)
1099{
1100}
1101
7c2cb42b 1102#endif
200ac0ef 1103
0614601c
AF
1104static void pmccfiltr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1105 uint64_t value)
1106{
1107 pmccntr_sync(env);
1108 env->cp15.pmccfiltr_el0 = value & 0x7E000000;
1109 pmccntr_sync(env);
1110}
1111
c4241c7d 1112static void pmcntenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
200ac0ef
PM
1113 uint64_t value)
1114{
200ac0ef
PM
1115 value &= (1 << 31);
1116 env->cp15.c9_pmcnten |= value;
200ac0ef
PM
1117}
1118
c4241c7d
PM
1119static void pmcntenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1120 uint64_t value)
200ac0ef 1121{
200ac0ef
PM
1122 value &= (1 << 31);
1123 env->cp15.c9_pmcnten &= ~value;
200ac0ef
PM
1124}
1125
c4241c7d
PM
1126static void pmovsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1127 uint64_t value)
200ac0ef 1128{
200ac0ef 1129 env->cp15.c9_pmovsr &= ~value;
200ac0ef
PM
1130}
1131
c4241c7d
PM
1132static void pmxevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
1133 uint64_t value)
200ac0ef 1134{
fdb86656
WH
1135 /* Attempts to access PMXEVTYPER are CONSTRAINED UNPREDICTABLE when
1136 * PMSELR value is equal to or greater than the number of implemented
1137 * counters, but not equal to 0x1f. We opt to behave as a RAZ/WI.
1138 */
1139 if (env->cp15.c9_pmselr == 0x1f) {
1140 pmccfiltr_write(env, ri, value);
1141 }
1142}
1143
1144static uint64_t pmxevtyper_read(CPUARMState *env, const ARMCPRegInfo *ri)
1145{
1146 /* We opt to behave as a RAZ/WI when attempts to access PMXEVTYPER
1147 * are CONSTRAINED UNPREDICTABLE. See comments in pmxevtyper_write().
1148 */
1149 if (env->cp15.c9_pmselr == 0x1f) {
1150 return env->cp15.pmccfiltr_el0;
1151 } else {
1152 return 0;
1153 }
200ac0ef
PM
1154}
1155
c4241c7d 1156static void pmuserenr_write(CPUARMState *env, const ARMCPRegInfo *ri,
200ac0ef
PM
1157 uint64_t value)
1158{
6ecd0b6b
AB
1159 if (arm_feature(env, ARM_FEATURE_V8)) {
1160 env->cp15.c9_pmuserenr = value & 0xf;
1161 } else {
1162 env->cp15.c9_pmuserenr = value & 1;
1163 }
200ac0ef
PM
1164}
1165
c4241c7d
PM
1166static void pmintenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1167 uint64_t value)
200ac0ef
PM
1168{
1169 /* We have no event counters so only the C bit can be changed */
1170 value &= (1 << 31);
1171 env->cp15.c9_pminten |= value;
200ac0ef
PM
1172}
1173
c4241c7d
PM
1174static void pmintenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1175 uint64_t value)
200ac0ef
PM
1176{
1177 value &= (1 << 31);
1178 env->cp15.c9_pminten &= ~value;
200ac0ef
PM
1179}
1180
c4241c7d
PM
1181static void vbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
1182 uint64_t value)
8641136c 1183{
a505d7fe
PM
1184 /* Note that even though the AArch64 view of this register has bits
1185 * [10:0] all RES0 we can only mask the bottom 5, to comply with the
1186 * architectural requirements for bits which are RES0 only in some
1187 * contexts. (ARMv8 would permit us to do no masking at all, but ARMv7
1188 * requires the bottom five bits to be RAZ/WI because they're UNK/SBZP.)
1189 */
855ea66d 1190 raw_write(env, ri, value & ~0x1FULL);
8641136c
NR
1191}
1192
64e0e2de
EI
1193static void scr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
1194{
1195 /* We only mask off bits that are RES0 both for AArch64 and AArch32.
1196 * For bits that vary between AArch32/64, code needs to check the
1197 * current execution mode before directly using the feature bit.
1198 */
1199 uint32_t valid_mask = SCR_AARCH64_MASK | SCR_AARCH32_MASK;
1200
1201 if (!arm_feature(env, ARM_FEATURE_EL2)) {
1202 valid_mask &= ~SCR_HCE;
1203
1204 /* On ARMv7, SMD (or SCD as it is called in v7) is only
1205 * supported if EL2 exists. The bit is UNK/SBZP when
1206 * EL2 is unavailable. In QEMU ARMv7, we force it to always zero
1207 * when EL2 is unavailable.
4eb27640 1208 * On ARMv8, this bit is always available.
64e0e2de 1209 */
4eb27640
GB
1210 if (arm_feature(env, ARM_FEATURE_V7) &&
1211 !arm_feature(env, ARM_FEATURE_V8)) {
64e0e2de
EI
1212 valid_mask &= ~SCR_SMD;
1213 }
1214 }
1215
1216 /* Clear all-context RES0 bits. */
1217 value &= valid_mask;
1218 raw_write(env, ri, value);
1219}
1220
c4241c7d 1221static uint64_t ccsidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
776d4e5c
PM
1222{
1223 ARMCPU *cpu = arm_env_get_cpu(env);
b85a1fd6
FA
1224
1225 /* Acquire the CSSELR index from the bank corresponding to the CCSIDR
1226 * bank
1227 */
1228 uint32_t index = A32_BANKED_REG_GET(env, csselr,
1229 ri->secure & ARM_CP_SECSTATE_S);
1230
1231 return cpu->ccsidr[index];
776d4e5c
PM
1232}
1233
c4241c7d
PM
1234static void csselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1235 uint64_t value)
776d4e5c 1236{
8d5c773e 1237 raw_write(env, ri, value & 0xf);
776d4e5c
PM
1238}
1239
1090b9c6
PM
1240static uint64_t isr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1241{
1242 CPUState *cs = ENV_GET_CPU(env);
1243 uint64_t ret = 0;
1244
1245 if (cs->interrupt_request & CPU_INTERRUPT_HARD) {
1246 ret |= CPSR_I;
1247 }
1248 if (cs->interrupt_request & CPU_INTERRUPT_FIQ) {
1249 ret |= CPSR_F;
1250 }
1251 /* External aborts are not possible in QEMU so A bit is always clear */
1252 return ret;
1253}
1254
e9aa6c21 1255static const ARMCPRegInfo v7_cp_reginfo[] = {
7d57f408
PM
1256 /* the old v6 WFI, UNPREDICTABLE in v7 but we choose to NOP */
1257 { .name = "NOP", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
1258 .access = PL1_W, .type = ARM_CP_NOP },
200ac0ef
PM
1259 /* Performance monitors are implementation defined in v7,
1260 * but with an ARM recommended set of registers, which we
1261 * follow (although we don't actually implement any counters)
1262 *
1263 * Performance registers fall into three categories:
1264 * (a) always UNDEF in PL0, RW in PL1 (PMINTENSET, PMINTENCLR)
1265 * (b) RO in PL0 (ie UNDEF on write), RW in PL1 (PMUSERENR)
1266 * (c) UNDEF in PL0 if PMUSERENR.EN==0, otherwise accessible (all others)
1267 * For the cases controlled by PMUSERENR we must set .access to PL0_RW
1268 * or PL0_RO as appropriate and then check PMUSERENR in the helper fn.
1269 */
1270 { .name = "PMCNTENSET", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 1,
7a0e58fa 1271 .access = PL0_RW, .type = ARM_CP_ALIAS,
8521466b 1272 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
fcd25206
PM
1273 .writefn = pmcntenset_write,
1274 .accessfn = pmreg_access,
1275 .raw_writefn = raw_write },
8521466b
AF
1276 { .name = "PMCNTENSET_EL0", .state = ARM_CP_STATE_AA64,
1277 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 1,
1278 .access = PL0_RW, .accessfn = pmreg_access,
1279 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten), .resetvalue = 0,
1280 .writefn = pmcntenset_write, .raw_writefn = raw_write },
200ac0ef 1281 { .name = "PMCNTENCLR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 2,
8521466b
AF
1282 .access = PL0_RW,
1283 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
fcd25206
PM
1284 .accessfn = pmreg_access,
1285 .writefn = pmcntenclr_write,
7a0e58fa 1286 .type = ARM_CP_ALIAS },
8521466b
AF
1287 { .name = "PMCNTENCLR_EL0", .state = ARM_CP_STATE_AA64,
1288 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 2,
1289 .access = PL0_RW, .accessfn = pmreg_access,
7a0e58fa 1290 .type = ARM_CP_ALIAS,
8521466b
AF
1291 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten),
1292 .writefn = pmcntenclr_write },
200ac0ef
PM
1293 { .name = "PMOVSR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 3,
1294 .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
fcd25206
PM
1295 .accessfn = pmreg_access,
1296 .writefn = pmovsr_write,
1297 .raw_writefn = raw_write },
978364f1
AF
1298 { .name = "PMOVSCLR_EL0", .state = ARM_CP_STATE_AA64,
1299 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 3,
1300 .access = PL0_RW, .accessfn = pmreg_access,
1301 .type = ARM_CP_ALIAS,
1302 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
1303 .writefn = pmovsr_write,
1304 .raw_writefn = raw_write },
fcd25206 1305 /* Unimplemented so WI. */
200ac0ef 1306 { .name = "PMSWINC", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 4,
6ecd0b6b 1307 .access = PL0_W, .accessfn = pmreg_access_swinc, .type = ARM_CP_NOP },
7c2cb42b 1308#ifndef CONFIG_USER_ONLY
6b040780
WH
1309 { .name = "PMSELR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 5,
1310 .access = PL0_RW, .type = ARM_CP_ALIAS,
1311 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmselr),
6ecd0b6b 1312 .accessfn = pmreg_access_selr, .writefn = pmselr_write,
6b040780
WH
1313 .raw_writefn = raw_write},
1314 { .name = "PMSELR_EL0", .state = ARM_CP_STATE_AA64,
1315 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 5,
6ecd0b6b 1316 .access = PL0_RW, .accessfn = pmreg_access_selr,
6b040780
WH
1317 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmselr),
1318 .writefn = pmselr_write, .raw_writefn = raw_write, },
200ac0ef 1319 { .name = "PMCCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 0,
7c2cb42b 1320 .access = PL0_RW, .resetvalue = 0, .type = ARM_CP_IO,
421c7ebd 1321 .readfn = pmccntr_read, .writefn = pmccntr_write32,
6ecd0b6b 1322 .accessfn = pmreg_access_ccntr },
8521466b
AF
1323 { .name = "PMCCNTR_EL0", .state = ARM_CP_STATE_AA64,
1324 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 0,
6ecd0b6b 1325 .access = PL0_RW, .accessfn = pmreg_access_ccntr,
8521466b
AF
1326 .type = ARM_CP_IO,
1327 .readfn = pmccntr_read, .writefn = pmccntr_write, },
7c2cb42b 1328#endif
8521466b
AF
1329 { .name = "PMCCFILTR_EL0", .state = ARM_CP_STATE_AA64,
1330 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 15, .opc2 = 7,
0614601c 1331 .writefn = pmccfiltr_write,
8521466b
AF
1332 .access = PL0_RW, .accessfn = pmreg_access,
1333 .type = ARM_CP_IO,
1334 .fieldoffset = offsetof(CPUARMState, cp15.pmccfiltr_el0),
1335 .resetvalue = 0, },
200ac0ef 1336 { .name = "PMXEVTYPER", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 1,
fdb86656
WH
1337 .access = PL0_RW, .type = ARM_CP_NO_RAW, .accessfn = pmreg_access,
1338 .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
1339 { .name = "PMXEVTYPER_EL0", .state = ARM_CP_STATE_AA64,
1340 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 1,
1341 .access = PL0_RW, .type = ARM_CP_NO_RAW, .accessfn = pmreg_access,
1342 .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
fcd25206 1343 /* Unimplemented, RAZ/WI. */
200ac0ef 1344 { .name = "PMXEVCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 2,
fcd25206 1345 .access = PL0_RW, .type = ARM_CP_CONST, .resetvalue = 0,
6ecd0b6b 1346 .accessfn = pmreg_access_xevcntr },
200ac0ef 1347 { .name = "PMUSERENR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 0,
1fce1ba9 1348 .access = PL0_R | PL1_RW, .accessfn = access_tpm,
200ac0ef
PM
1349 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmuserenr),
1350 .resetvalue = 0,
d4e6df63 1351 .writefn = pmuserenr_write, .raw_writefn = raw_write },
8a83ffc2
AF
1352 { .name = "PMUSERENR_EL0", .state = ARM_CP_STATE_AA64,
1353 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 0,
1fce1ba9 1354 .access = PL0_R | PL1_RW, .accessfn = access_tpm, .type = ARM_CP_ALIAS,
8a83ffc2
AF
1355 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmuserenr),
1356 .resetvalue = 0,
1357 .writefn = pmuserenr_write, .raw_writefn = raw_write },
200ac0ef 1358 { .name = "PMINTENSET", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 1,
1fce1ba9 1359 .access = PL1_RW, .accessfn = access_tpm,
e6ec5457
WH
1360 .type = ARM_CP_ALIAS,
1361 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pminten),
200ac0ef 1362 .resetvalue = 0,
d4e6df63 1363 .writefn = pmintenset_write, .raw_writefn = raw_write },
e6ec5457
WH
1364 { .name = "PMINTENSET_EL1", .state = ARM_CP_STATE_AA64,
1365 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 1,
1366 .access = PL1_RW, .accessfn = access_tpm,
1367 .type = ARM_CP_IO,
1368 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
1369 .writefn = pmintenset_write, .raw_writefn = raw_write,
1370 .resetvalue = 0x0 },
200ac0ef 1371 { .name = "PMINTENCLR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 2,
1fce1ba9 1372 .access = PL1_RW, .accessfn = access_tpm, .type = ARM_CP_ALIAS,
200ac0ef 1373 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
b061a82b 1374 .writefn = pmintenclr_write, },
978364f1
AF
1375 { .name = "PMINTENCLR_EL1", .state = ARM_CP_STATE_AA64,
1376 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 2,
1fce1ba9 1377 .access = PL1_RW, .accessfn = access_tpm, .type = ARM_CP_ALIAS,
978364f1
AF
1378 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
1379 .writefn = pmintenclr_write },
7da845b0
PM
1380 { .name = "CCSIDR", .state = ARM_CP_STATE_BOTH,
1381 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 0,
7a0e58fa 1382 .access = PL1_R, .readfn = ccsidr_read, .type = ARM_CP_NO_RAW },
7da845b0
PM
1383 { .name = "CSSELR", .state = ARM_CP_STATE_BOTH,
1384 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 2, .opc2 = 0,
b85a1fd6
FA
1385 .access = PL1_RW, .writefn = csselr_write, .resetvalue = 0,
1386 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.csselr_s),
1387 offsetof(CPUARMState, cp15.csselr_ns) } },
776d4e5c
PM
1388 /* Auxiliary ID register: this actually has an IMPDEF value but for now
1389 * just RAZ for all cores:
1390 */
0ff644a7
PM
1391 { .name = "AIDR", .state = ARM_CP_STATE_BOTH,
1392 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 7,
776d4e5c 1393 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
f32cdad5
PM
1394 /* Auxiliary fault status registers: these also are IMPDEF, and we
1395 * choose to RAZ/WI for all cores.
1396 */
1397 { .name = "AFSR0_EL1", .state = ARM_CP_STATE_BOTH,
1398 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 0,
1399 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
1400 { .name = "AFSR1_EL1", .state = ARM_CP_STATE_BOTH,
1401 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 1,
1402 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
b0fe2427
PM
1403 /* MAIR can just read-as-written because we don't implement caches
1404 * and so don't need to care about memory attributes.
1405 */
1406 { .name = "MAIR_EL1", .state = ARM_CP_STATE_AA64,
1407 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
be693c87 1408 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[1]),
b0fe2427 1409 .resetvalue = 0 },
4cfb8ad8
PM
1410 { .name = "MAIR_EL3", .state = ARM_CP_STATE_AA64,
1411 .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 2, .opc2 = 0,
1412 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[3]),
1413 .resetvalue = 0 },
b0fe2427
PM
1414 /* For non-long-descriptor page tables these are PRRR and NMRR;
1415 * regardless they still act as reads-as-written for QEMU.
b0fe2427 1416 */
1281f8e3 1417 /* MAIR0/1 are defined separately from their 64-bit counterpart which
be693c87
GB
1418 * allows them to assign the correct fieldoffset based on the endianness
1419 * handled in the field definitions.
1420 */
a903c449 1421 { .name = "MAIR0", .state = ARM_CP_STATE_AA32,
b0fe2427 1422 .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0, .access = PL1_RW,
be693c87
GB
1423 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair0_s),
1424 offsetof(CPUARMState, cp15.mair0_ns) },
b0fe2427 1425 .resetfn = arm_cp_reset_ignore },
a903c449 1426 { .name = "MAIR1", .state = ARM_CP_STATE_AA32,
b0fe2427 1427 .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 1, .access = PL1_RW,
be693c87
GB
1428 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair1_s),
1429 offsetof(CPUARMState, cp15.mair1_ns) },
b0fe2427 1430 .resetfn = arm_cp_reset_ignore },
1090b9c6
PM
1431 { .name = "ISR_EL1", .state = ARM_CP_STATE_BOTH,
1432 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 0,
7a0e58fa 1433 .type = ARM_CP_NO_RAW, .access = PL1_R, .readfn = isr_read },
995939a6
PM
1434 /* 32 bit ITLB invalidates */
1435 { .name = "ITLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 0,
7a0e58fa 1436 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_write },
995939a6 1437 { .name = "ITLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 1,
7a0e58fa 1438 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write },
995939a6 1439 { .name = "ITLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 2,
7a0e58fa 1440 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiasid_write },
995939a6
PM
1441 /* 32 bit DTLB invalidates */
1442 { .name = "DTLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 0,
7a0e58fa 1443 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_write },
995939a6 1444 { .name = "DTLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 1,
7a0e58fa 1445 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write },
995939a6 1446 { .name = "DTLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 2,
7a0e58fa 1447 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiasid_write },
995939a6
PM
1448 /* 32 bit TLB invalidates */
1449 { .name = "TLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
7a0e58fa 1450 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_write },
995939a6 1451 { .name = "TLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
7a0e58fa 1452 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write },
995939a6 1453 { .name = "TLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
7a0e58fa 1454 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiasid_write },
995939a6 1455 { .name = "TLBIMVAA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
7a0e58fa 1456 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimvaa_write },
995939a6
PM
1457 REGINFO_SENTINEL
1458};
1459
1460static const ARMCPRegInfo v7mp_cp_reginfo[] = {
1461 /* 32 bit TLB invalidates, Inner Shareable */
1462 { .name = "TLBIALLIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
7a0e58fa 1463 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_is_write },
995939a6 1464 { .name = "TLBIMVAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
7a0e58fa 1465 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_is_write },
995939a6 1466 { .name = "TLBIASIDIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
7a0e58fa 1467 .type = ARM_CP_NO_RAW, .access = PL1_W,
fa439fc5 1468 .writefn = tlbiasid_is_write },
995939a6 1469 { .name = "TLBIMVAAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
7a0e58fa 1470 .type = ARM_CP_NO_RAW, .access = PL1_W,
fa439fc5 1471 .writefn = tlbimvaa_is_write },
e9aa6c21
PM
1472 REGINFO_SENTINEL
1473};
1474
c4241c7d
PM
1475static void teecr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1476 uint64_t value)
c326b979
PM
1477{
1478 value &= 1;
1479 env->teecr = value;
c326b979
PM
1480}
1481
3f208fd7
PM
1482static CPAccessResult teehbr_access(CPUARMState *env, const ARMCPRegInfo *ri,
1483 bool isread)
c326b979 1484{
dcbff19b 1485 if (arm_current_el(env) == 0 && (env->teecr & 1)) {
92611c00 1486 return CP_ACCESS_TRAP;
c326b979 1487 }
92611c00 1488 return CP_ACCESS_OK;
c326b979
PM
1489}
1490
1491static const ARMCPRegInfo t2ee_cp_reginfo[] = {
1492 { .name = "TEECR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 6, .opc2 = 0,
1493 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, teecr),
1494 .resetvalue = 0,
1495 .writefn = teecr_write },
1496 { .name = "TEEHBR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 6, .opc2 = 0,
1497 .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, teehbr),
92611c00 1498 .accessfn = teehbr_access, .resetvalue = 0 },
c326b979
PM
1499 REGINFO_SENTINEL
1500};
1501
4d31c596 1502static const ARMCPRegInfo v6k_cp_reginfo[] = {
e4fe830b
PM
1503 { .name = "TPIDR_EL0", .state = ARM_CP_STATE_AA64,
1504 .opc0 = 3, .opc1 = 3, .opc2 = 2, .crn = 13, .crm = 0,
1505 .access = PL0_RW,
54bf36ed 1506 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[0]), .resetvalue = 0 },
4d31c596
PM
1507 { .name = "TPIDRURW", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 2,
1508 .access = PL0_RW,
54bf36ed
FA
1509 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrurw_s),
1510 offsetoflow32(CPUARMState, cp15.tpidrurw_ns) },
e4fe830b
PM
1511 .resetfn = arm_cp_reset_ignore },
1512 { .name = "TPIDRRO_EL0", .state = ARM_CP_STATE_AA64,
1513 .opc0 = 3, .opc1 = 3, .opc2 = 3, .crn = 13, .crm = 0,
1514 .access = PL0_R|PL1_W,
54bf36ed
FA
1515 .fieldoffset = offsetof(CPUARMState, cp15.tpidrro_el[0]),
1516 .resetvalue = 0},
4d31c596
PM
1517 { .name = "TPIDRURO", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 3,
1518 .access = PL0_R|PL1_W,
54bf36ed
FA
1519 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidruro_s),
1520 offsetoflow32(CPUARMState, cp15.tpidruro_ns) },
e4fe830b 1521 .resetfn = arm_cp_reset_ignore },
54bf36ed 1522 { .name = "TPIDR_EL1", .state = ARM_CP_STATE_AA64,
e4fe830b 1523 .opc0 = 3, .opc1 = 0, .opc2 = 4, .crn = 13, .crm = 0,
4d31c596 1524 .access = PL1_RW,
54bf36ed
FA
1525 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[1]), .resetvalue = 0 },
1526 { .name = "TPIDRPRW", .opc1 = 0, .cp = 15, .crn = 13, .crm = 0, .opc2 = 4,
1527 .access = PL1_RW,
1528 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrprw_s),
1529 offsetoflow32(CPUARMState, cp15.tpidrprw_ns) },
1530 .resetvalue = 0 },
4d31c596
PM
1531 REGINFO_SENTINEL
1532};
1533
55d284af
PM
1534#ifndef CONFIG_USER_ONLY
1535
3f208fd7
PM
1536static CPAccessResult gt_cntfrq_access(CPUARMState *env, const ARMCPRegInfo *ri,
1537 bool isread)
00108f2d 1538{
75502672
PM
1539 /* CNTFRQ: not visible from PL0 if both PL0PCTEN and PL0VCTEN are zero.
1540 * Writable only at the highest implemented exception level.
1541 */
1542 int el = arm_current_el(env);
1543
1544 switch (el) {
1545 case 0:
1546 if (!extract32(env->cp15.c14_cntkctl, 0, 2)) {
1547 return CP_ACCESS_TRAP;
1548 }
1549 break;
1550 case 1:
1551 if (!isread && ri->state == ARM_CP_STATE_AA32 &&
1552 arm_is_secure_below_el3(env)) {
1553 /* Accesses from 32-bit Secure EL1 UNDEF (*not* trap to EL3!) */
1554 return CP_ACCESS_TRAP_UNCATEGORIZED;
1555 }
1556 break;
1557 case 2:
1558 case 3:
1559 break;
00108f2d 1560 }
75502672
PM
1561
1562 if (!isread && el < arm_highest_el(env)) {
1563 return CP_ACCESS_TRAP_UNCATEGORIZED;
1564 }
1565
00108f2d
PM
1566 return CP_ACCESS_OK;
1567}
1568
3f208fd7
PM
1569static CPAccessResult gt_counter_access(CPUARMState *env, int timeridx,
1570 bool isread)
00108f2d 1571{
0b6440af
EI
1572 unsigned int cur_el = arm_current_el(env);
1573 bool secure = arm_is_secure(env);
1574
00108f2d 1575 /* CNT[PV]CT: not visible from PL0 if ELO[PV]CTEN is zero */
0b6440af 1576 if (cur_el == 0 &&
00108f2d
PM
1577 !extract32(env->cp15.c14_cntkctl, timeridx, 1)) {
1578 return CP_ACCESS_TRAP;
1579 }
0b6440af
EI
1580
1581 if (arm_feature(env, ARM_FEATURE_EL2) &&
1582 timeridx == GTIMER_PHYS && !secure && cur_el < 2 &&
1583 !extract32(env->cp15.cnthctl_el2, 0, 1)) {
1584 return CP_ACCESS_TRAP_EL2;
1585 }
00108f2d
PM
1586 return CP_ACCESS_OK;
1587}
1588
3f208fd7
PM
1589static CPAccessResult gt_timer_access(CPUARMState *env, int timeridx,
1590 bool isread)
00108f2d 1591{
0b6440af
EI
1592 unsigned int cur_el = arm_current_el(env);
1593 bool secure = arm_is_secure(env);
1594
00108f2d
PM
1595 /* CNT[PV]_CVAL, CNT[PV]_CTL, CNT[PV]_TVAL: not visible from PL0 if
1596 * EL0[PV]TEN is zero.
1597 */
0b6440af 1598 if (cur_el == 0 &&
00108f2d
PM
1599 !extract32(env->cp15.c14_cntkctl, 9 - timeridx, 1)) {
1600 return CP_ACCESS_TRAP;
1601 }
0b6440af
EI
1602
1603 if (arm_feature(env, ARM_FEATURE_EL2) &&
1604 timeridx == GTIMER_PHYS && !secure && cur_el < 2 &&
1605 !extract32(env->cp15.cnthctl_el2, 1, 1)) {
1606 return CP_ACCESS_TRAP_EL2;
1607 }
00108f2d
PM
1608 return CP_ACCESS_OK;
1609}
1610
1611static CPAccessResult gt_pct_access(CPUARMState *env,
3f208fd7
PM
1612 const ARMCPRegInfo *ri,
1613 bool isread)
00108f2d 1614{
3f208fd7 1615 return gt_counter_access(env, GTIMER_PHYS, isread);
00108f2d
PM
1616}
1617
1618static CPAccessResult gt_vct_access(CPUARMState *env,
3f208fd7
PM
1619 const ARMCPRegInfo *ri,
1620 bool isread)
00108f2d 1621{
3f208fd7 1622 return gt_counter_access(env, GTIMER_VIRT, isread);
00108f2d
PM
1623}
1624
3f208fd7
PM
1625static CPAccessResult gt_ptimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
1626 bool isread)
00108f2d 1627{
3f208fd7 1628 return gt_timer_access(env, GTIMER_PHYS, isread);
00108f2d
PM
1629}
1630
3f208fd7
PM
1631static CPAccessResult gt_vtimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
1632 bool isread)
00108f2d 1633{
3f208fd7 1634 return gt_timer_access(env, GTIMER_VIRT, isread);
00108f2d
PM
1635}
1636
b4d3978c 1637static CPAccessResult gt_stimer_access(CPUARMState *env,
3f208fd7
PM
1638 const ARMCPRegInfo *ri,
1639 bool isread)
b4d3978c
PM
1640{
1641 /* The AArch64 register view of the secure physical timer is
1642 * always accessible from EL3, and configurably accessible from
1643 * Secure EL1.
1644 */
1645 switch (arm_current_el(env)) {
1646 case 1:
1647 if (!arm_is_secure(env)) {
1648 return CP_ACCESS_TRAP;
1649 }
1650 if (!(env->cp15.scr_el3 & SCR_ST)) {
1651 return CP_ACCESS_TRAP_EL3;
1652 }
1653 return CP_ACCESS_OK;
1654 case 0:
1655 case 2:
1656 return CP_ACCESS_TRAP;
1657 case 3:
1658 return CP_ACCESS_OK;
1659 default:
1660 g_assert_not_reached();
1661 }
1662}
1663
55d284af
PM
1664static uint64_t gt_get_countervalue(CPUARMState *env)
1665{
bc72ad67 1666 return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) / GTIMER_SCALE;
55d284af
PM
1667}
1668
1669static void gt_recalc_timer(ARMCPU *cpu, int timeridx)
1670{
1671 ARMGenericTimer *gt = &cpu->env.cp15.c14_timer[timeridx];
1672
1673 if (gt->ctl & 1) {
1674 /* Timer enabled: calculate and set current ISTATUS, irq, and
1675 * reset timer to when ISTATUS next has to change
1676 */
edac4d8a
EI
1677 uint64_t offset = timeridx == GTIMER_VIRT ?
1678 cpu->env.cp15.cntvoff_el2 : 0;
55d284af
PM
1679 uint64_t count = gt_get_countervalue(&cpu->env);
1680 /* Note that this must be unsigned 64 bit arithmetic: */
edac4d8a 1681 int istatus = count - offset >= gt->cval;
55d284af 1682 uint64_t nexttick;
194cbc49 1683 int irqstate;
55d284af
PM
1684
1685 gt->ctl = deposit32(gt->ctl, 2, 1, istatus);
194cbc49
PM
1686
1687 irqstate = (istatus && !(gt->ctl & 2));
1688 qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate);
1689
55d284af
PM
1690 if (istatus) {
1691 /* Next transition is when count rolls back over to zero */
1692 nexttick = UINT64_MAX;
1693 } else {
1694 /* Next transition is when we hit cval */
edac4d8a 1695 nexttick = gt->cval + offset;
55d284af
PM
1696 }
1697 /* Note that the desired next expiry time might be beyond the
1698 * signed-64-bit range of a QEMUTimer -- in this case we just
1699 * set the timer for as far in the future as possible. When the
1700 * timer expires we will reset the timer for any remaining period.
1701 */
1702 if (nexttick > INT64_MAX / GTIMER_SCALE) {
1703 nexttick = INT64_MAX / GTIMER_SCALE;
1704 }
bc72ad67 1705 timer_mod(cpu->gt_timer[timeridx], nexttick);
194cbc49 1706 trace_arm_gt_recalc(timeridx, irqstate, nexttick);
55d284af
PM
1707 } else {
1708 /* Timer disabled: ISTATUS and timer output always clear */
1709 gt->ctl &= ~4;
1710 qemu_set_irq(cpu->gt_timer_outputs[timeridx], 0);
bc72ad67 1711 timer_del(cpu->gt_timer[timeridx]);
194cbc49 1712 trace_arm_gt_recalc_disabled(timeridx);
55d284af
PM
1713 }
1714}
1715
0e3eca4c
EI
1716static void gt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri,
1717 int timeridx)
55d284af
PM
1718{
1719 ARMCPU *cpu = arm_env_get_cpu(env);
55d284af 1720
bc72ad67 1721 timer_del(cpu->gt_timer[timeridx]);
55d284af
PM
1722}
1723
c4241c7d 1724static uint64_t gt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
55d284af 1725{
c4241c7d 1726 return gt_get_countervalue(env);
55d284af
PM
1727}
1728
edac4d8a
EI
1729static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
1730{
1731 return gt_get_countervalue(env) - env->cp15.cntvoff_el2;
1732}
1733
c4241c7d 1734static void gt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
0e3eca4c 1735 int timeridx,
c4241c7d 1736 uint64_t value)
55d284af 1737{
194cbc49 1738 trace_arm_gt_cval_write(timeridx, value);
55d284af
PM
1739 env->cp15.c14_timer[timeridx].cval = value;
1740 gt_recalc_timer(arm_env_get_cpu(env), timeridx);
55d284af 1741}
c4241c7d 1742
0e3eca4c
EI
1743static uint64_t gt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri,
1744 int timeridx)
55d284af 1745{
edac4d8a 1746 uint64_t offset = timeridx == GTIMER_VIRT ? env->cp15.cntvoff_el2 : 0;
55d284af 1747
c4241c7d 1748 return (uint32_t)(env->cp15.c14_timer[timeridx].cval -
edac4d8a 1749 (gt_get_countervalue(env) - offset));
55d284af
PM
1750}
1751
c4241c7d 1752static void gt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
0e3eca4c 1753 int timeridx,
c4241c7d 1754 uint64_t value)
55d284af 1755{
edac4d8a 1756 uint64_t offset = timeridx == GTIMER_VIRT ? env->cp15.cntvoff_el2 : 0;
55d284af 1757
194cbc49 1758 trace_arm_gt_tval_write(timeridx, value);
edac4d8a 1759 env->cp15.c14_timer[timeridx].cval = gt_get_countervalue(env) - offset +
18084b2f 1760 sextract64(value, 0, 32);
55d284af 1761 gt_recalc_timer(arm_env_get_cpu(env), timeridx);
55d284af
PM
1762}
1763
c4241c7d 1764static void gt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
0e3eca4c 1765 int timeridx,
c4241c7d 1766 uint64_t value)
55d284af
PM
1767{
1768 ARMCPU *cpu = arm_env_get_cpu(env);
55d284af
PM
1769 uint32_t oldval = env->cp15.c14_timer[timeridx].ctl;
1770
194cbc49 1771 trace_arm_gt_ctl_write(timeridx, value);
d3afacc7 1772 env->cp15.c14_timer[timeridx].ctl = deposit64(oldval, 0, 2, value);
55d284af
PM
1773 if ((oldval ^ value) & 1) {
1774 /* Enable toggled */
1775 gt_recalc_timer(cpu, timeridx);
d3afacc7 1776 } else if ((oldval ^ value) & 2) {
55d284af
PM
1777 /* IMASK toggled: don't need to recalculate,
1778 * just set the interrupt line based on ISTATUS
1779 */
194cbc49
PM
1780 int irqstate = (oldval & 4) && !(value & 2);
1781
1782 trace_arm_gt_imask_toggle(timeridx, irqstate);
1783 qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate);
55d284af 1784 }
55d284af
PM
1785}
1786
0e3eca4c
EI
1787static void gt_phys_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1788{
1789 gt_timer_reset(env, ri, GTIMER_PHYS);
1790}
1791
1792static void gt_phys_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1793 uint64_t value)
1794{
1795 gt_cval_write(env, ri, GTIMER_PHYS, value);
1796}
1797
1798static uint64_t gt_phys_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
1799{
1800 return gt_tval_read(env, ri, GTIMER_PHYS);
1801}
1802
1803static void gt_phys_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1804 uint64_t value)
1805{
1806 gt_tval_write(env, ri, GTIMER_PHYS, value);
1807}
1808
1809static void gt_phys_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
1810 uint64_t value)
1811{
1812 gt_ctl_write(env, ri, GTIMER_PHYS, value);
1813}
1814
1815static void gt_virt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1816{
1817 gt_timer_reset(env, ri, GTIMER_VIRT);
1818}
1819
1820static void gt_virt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1821 uint64_t value)
1822{
1823 gt_cval_write(env, ri, GTIMER_VIRT, value);
1824}
1825
1826static uint64_t gt_virt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
1827{
1828 return gt_tval_read(env, ri, GTIMER_VIRT);
1829}
1830
1831static void gt_virt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1832 uint64_t value)
1833{
1834 gt_tval_write(env, ri, GTIMER_VIRT, value);
1835}
1836
1837static void gt_virt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
1838 uint64_t value)
1839{
1840 gt_ctl_write(env, ri, GTIMER_VIRT, value);
1841}
1842
edac4d8a
EI
1843static void gt_cntvoff_write(CPUARMState *env, const ARMCPRegInfo *ri,
1844 uint64_t value)
1845{
1846 ARMCPU *cpu = arm_env_get_cpu(env);
1847
194cbc49 1848 trace_arm_gt_cntvoff_write(value);
edac4d8a
EI
1849 raw_write(env, ri, value);
1850 gt_recalc_timer(cpu, GTIMER_VIRT);
1851}
1852
b0e66d95
EI
1853static void gt_hyp_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1854{
1855 gt_timer_reset(env, ri, GTIMER_HYP);
1856}
1857
1858static void gt_hyp_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1859 uint64_t value)
1860{
1861 gt_cval_write(env, ri, GTIMER_HYP, value);
1862}
1863
1864static uint64_t gt_hyp_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
1865{
1866 return gt_tval_read(env, ri, GTIMER_HYP);
1867}
1868
1869static void gt_hyp_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1870 uint64_t value)
1871{
1872 gt_tval_write(env, ri, GTIMER_HYP, value);
1873}
1874
1875static void gt_hyp_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
1876 uint64_t value)
1877{
1878 gt_ctl_write(env, ri, GTIMER_HYP, value);
1879}
1880
b4d3978c
PM
1881static void gt_sec_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1882{
1883 gt_timer_reset(env, ri, GTIMER_SEC);
1884}
1885
1886static void gt_sec_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1887 uint64_t value)
1888{
1889 gt_cval_write(env, ri, GTIMER_SEC, value);
1890}
1891
1892static uint64_t gt_sec_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
1893{
1894 return gt_tval_read(env, ri, GTIMER_SEC);
1895}
1896
1897static void gt_sec_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1898 uint64_t value)
1899{
1900 gt_tval_write(env, ri, GTIMER_SEC, value);
1901}
1902
1903static void gt_sec_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
1904 uint64_t value)
1905{
1906 gt_ctl_write(env, ri, GTIMER_SEC, value);
1907}
1908
55d284af
PM
1909void arm_gt_ptimer_cb(void *opaque)
1910{
1911 ARMCPU *cpu = opaque;
1912
1913 gt_recalc_timer(cpu, GTIMER_PHYS);
1914}
1915
1916void arm_gt_vtimer_cb(void *opaque)
1917{
1918 ARMCPU *cpu = opaque;
1919
1920 gt_recalc_timer(cpu, GTIMER_VIRT);
1921}
1922
b0e66d95
EI
1923void arm_gt_htimer_cb(void *opaque)
1924{
1925 ARMCPU *cpu = opaque;
1926
1927 gt_recalc_timer(cpu, GTIMER_HYP);
1928}
1929
b4d3978c
PM
1930void arm_gt_stimer_cb(void *opaque)
1931{
1932 ARMCPU *cpu = opaque;
1933
1934 gt_recalc_timer(cpu, GTIMER_SEC);
1935}
1936
55d284af
PM
1937static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
1938 /* Note that CNTFRQ is purely reads-as-written for the benefit
1939 * of software; writing it doesn't actually change the timer frequency.
1940 * Our reset value matches the fixed frequency we implement the timer at.
1941 */
1942 { .name = "CNTFRQ", .cp = 15, .crn = 14, .crm = 0, .opc1 = 0, .opc2 = 0,
7a0e58fa 1943 .type = ARM_CP_ALIAS,
a7adc4b7
PM
1944 .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
1945 .fieldoffset = offsetoflow32(CPUARMState, cp15.c14_cntfrq),
a7adc4b7
PM
1946 },
1947 { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
1948 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
1949 .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
55d284af
PM
1950 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
1951 .resetvalue = (1000 * 1000 * 1000) / GTIMER_SCALE,
55d284af
PM
1952 },
1953 /* overall control: mostly access permissions */
a7adc4b7
PM
1954 { .name = "CNTKCTL", .state = ARM_CP_STATE_BOTH,
1955 .opc0 = 3, .opc1 = 0, .crn = 14, .crm = 1, .opc2 = 0,
55d284af
PM
1956 .access = PL1_RW,
1957 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntkctl),
1958 .resetvalue = 0,
1959 },
1960 /* per-timer control */
1961 { .name = "CNTP_CTL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
9ff9dd3c 1962 .secure = ARM_CP_SECSTATE_NS,
7a0e58fa 1963 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL1_RW | PL0_R,
a7adc4b7
PM
1964 .accessfn = gt_ptimer_access,
1965 .fieldoffset = offsetoflow32(CPUARMState,
1966 cp15.c14_timer[GTIMER_PHYS].ctl),
0e3eca4c 1967 .writefn = gt_phys_ctl_write, .raw_writefn = raw_write,
a7adc4b7 1968 },
9ff9dd3c
PM
1969 { .name = "CNTP_CTL(S)",
1970 .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
1971 .secure = ARM_CP_SECSTATE_S,
1972 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL1_RW | PL0_R,
1973 .accessfn = gt_ptimer_access,
1974 .fieldoffset = offsetoflow32(CPUARMState,
1975 cp15.c14_timer[GTIMER_SEC].ctl),
1976 .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
1977 },
a7adc4b7
PM
1978 { .name = "CNTP_CTL_EL0", .state = ARM_CP_STATE_AA64,
1979 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 1,
55d284af 1980 .type = ARM_CP_IO, .access = PL1_RW | PL0_R,
a7adc4b7 1981 .accessfn = gt_ptimer_access,
55d284af
PM
1982 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
1983 .resetvalue = 0,
0e3eca4c 1984 .writefn = gt_phys_ctl_write, .raw_writefn = raw_write,
55d284af
PM
1985 },
1986 { .name = "CNTV_CTL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 1,
7a0e58fa 1987 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL1_RW | PL0_R,
a7adc4b7
PM
1988 .accessfn = gt_vtimer_access,
1989 .fieldoffset = offsetoflow32(CPUARMState,
1990 cp15.c14_timer[GTIMER_VIRT].ctl),
0e3eca4c 1991 .writefn = gt_virt_ctl_write, .raw_writefn = raw_write,
a7adc4b7
PM
1992 },
1993 { .name = "CNTV_CTL_EL0", .state = ARM_CP_STATE_AA64,
1994 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 1,
55d284af 1995 .type = ARM_CP_IO, .access = PL1_RW | PL0_R,
a7adc4b7 1996 .accessfn = gt_vtimer_access,
55d284af
PM
1997 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
1998 .resetvalue = 0,
0e3eca4c 1999 .writefn = gt_virt_ctl_write, .raw_writefn = raw_write,
55d284af
PM
2000 },
2001 /* TimerValue views: a 32 bit downcounting view of the underlying state */
2002 { .name = "CNTP_TVAL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
9ff9dd3c 2003 .secure = ARM_CP_SECSTATE_NS,
7a0e58fa 2004 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW | PL0_R,
00108f2d 2005 .accessfn = gt_ptimer_access,
0e3eca4c 2006 .readfn = gt_phys_tval_read, .writefn = gt_phys_tval_write,
55d284af 2007 },
9ff9dd3c
PM
2008 { .name = "CNTP_TVAL(S)",
2009 .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
2010 .secure = ARM_CP_SECSTATE_S,
2011 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW | PL0_R,
2012 .accessfn = gt_ptimer_access,
2013 .readfn = gt_sec_tval_read, .writefn = gt_sec_tval_write,
2014 },
a7adc4b7
PM
2015 { .name = "CNTP_TVAL_EL0", .state = ARM_CP_STATE_AA64,
2016 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 0,
7a0e58fa 2017 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW | PL0_R,
0e3eca4c
EI
2018 .accessfn = gt_ptimer_access, .resetfn = gt_phys_timer_reset,
2019 .readfn = gt_phys_tval_read, .writefn = gt_phys_tval_write,
a7adc4b7 2020 },
55d284af 2021 { .name = "CNTV_TVAL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 0,
7a0e58fa 2022 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW | PL0_R,
00108f2d 2023 .accessfn = gt_vtimer_access,
0e3eca4c 2024 .readfn = gt_virt_tval_read, .writefn = gt_virt_tval_write,
55d284af 2025 },
a7adc4b7
PM
2026 { .name = "CNTV_TVAL_EL0", .state = ARM_CP_STATE_AA64,
2027 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 0,
7a0e58fa 2028 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW | PL0_R,
0e3eca4c
EI
2029 .accessfn = gt_vtimer_access, .resetfn = gt_virt_timer_reset,
2030 .readfn = gt_virt_tval_read, .writefn = gt_virt_tval_write,
a7adc4b7 2031 },
55d284af
PM
2032 /* The counter itself */
2033 { .name = "CNTPCT", .cp = 15, .crm = 14, .opc1 = 0,
7a0e58fa 2034 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
00108f2d 2035 .accessfn = gt_pct_access,
a7adc4b7
PM
2036 .readfn = gt_cnt_read, .resetfn = arm_cp_reset_ignore,
2037 },
2038 { .name = "CNTPCT_EL0", .state = ARM_CP_STATE_AA64,
2039 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 1,
7a0e58fa 2040 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
d57b9ee8 2041 .accessfn = gt_pct_access, .readfn = gt_cnt_read,
55d284af
PM
2042 },
2043 { .name = "CNTVCT", .cp = 15, .crm = 14, .opc1 = 1,
7a0e58fa 2044 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
00108f2d 2045 .accessfn = gt_vct_access,
edac4d8a 2046 .readfn = gt_virt_cnt_read, .resetfn = arm_cp_reset_ignore,
a7adc4b7
PM
2047 },
2048 { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
2049 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
7a0e58fa 2050 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
d57b9ee8 2051 .accessfn = gt_vct_access, .readfn = gt_virt_cnt_read,
55d284af
PM
2052 },
2053 /* Comparison value, indicating when the timer goes off */
2054 { .name = "CNTP_CVAL", .cp = 15, .crm = 14, .opc1 = 2,
9ff9dd3c 2055 .secure = ARM_CP_SECSTATE_NS,
55d284af 2056 .access = PL1_RW | PL0_R,
7a0e58fa 2057 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
55d284af 2058 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
b061a82b 2059 .accessfn = gt_ptimer_access,
0e3eca4c 2060 .writefn = gt_phys_cval_write, .raw_writefn = raw_write,
a7adc4b7 2061 },
9ff9dd3c
PM
2062 { .name = "CNTP_CVAL(S)", .cp = 15, .crm = 14, .opc1 = 2,
2063 .secure = ARM_CP_SECSTATE_S,
2064 .access = PL1_RW | PL0_R,
2065 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
2066 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
2067 .accessfn = gt_ptimer_access,
2068 .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
2069 },
a7adc4b7
PM
2070 { .name = "CNTP_CVAL_EL0", .state = ARM_CP_STATE_AA64,
2071 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 2,
2072 .access = PL1_RW | PL0_R,
2073 .type = ARM_CP_IO,
2074 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
12cde08a 2075 .resetvalue = 0, .accessfn = gt_ptimer_access,
0e3eca4c 2076 .writefn = gt_phys_cval_write, .raw_writefn = raw_write,
55d284af
PM
2077 },
2078 { .name = "CNTV_CVAL", .cp = 15, .crm = 14, .opc1 = 3,
2079 .access = PL1_RW | PL0_R,
7a0e58fa 2080 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
55d284af 2081 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
b061a82b 2082 .accessfn = gt_vtimer_access,
0e3eca4c 2083 .writefn = gt_virt_cval_write, .raw_writefn = raw_write,
a7adc4b7
PM
2084 },
2085 { .name = "CNTV_CVAL_EL0", .state = ARM_CP_STATE_AA64,
2086 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 2,
2087 .access = PL1_RW | PL0_R,
2088 .type = ARM_CP_IO,
2089 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
2090 .resetvalue = 0, .accessfn = gt_vtimer_access,
0e3eca4c 2091 .writefn = gt_virt_cval_write, .raw_writefn = raw_write,
55d284af 2092 },
b4d3978c
PM
2093 /* Secure timer -- this is actually restricted to only EL3
2094 * and configurably Secure-EL1 via the accessfn.
2095 */
2096 { .name = "CNTPS_TVAL_EL1", .state = ARM_CP_STATE_AA64,
2097 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 0,
2098 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW,
2099 .accessfn = gt_stimer_access,
2100 .readfn = gt_sec_tval_read,
2101 .writefn = gt_sec_tval_write,
2102 .resetfn = gt_sec_timer_reset,
2103 },
2104 { .name = "CNTPS_CTL_EL1", .state = ARM_CP_STATE_AA64,
2105 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 1,
2106 .type = ARM_CP_IO, .access = PL1_RW,
2107 .accessfn = gt_stimer_access,
2108 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].ctl),
2109 .resetvalue = 0,
2110 .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
2111 },
2112 { .name = "CNTPS_CVAL_EL1", .state = ARM_CP_STATE_AA64,
2113 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 2,
2114 .type = ARM_CP_IO, .access = PL1_RW,
2115 .accessfn = gt_stimer_access,
2116 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
2117 .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
2118 },
55d284af
PM
2119 REGINFO_SENTINEL
2120};
2121
2122#else
2123/* In user-mode none of the generic timer registers are accessible,
bc72ad67 2124 * and their implementation depends on QEMU_CLOCK_VIRTUAL and qdev gpio outputs,
55d284af
PM
2125 * so instead just don't register any of them.
2126 */
6cc7a3ae 2127static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
6cc7a3ae
PM
2128 REGINFO_SENTINEL
2129};
2130
55d284af
PM
2131#endif
2132
c4241c7d 2133static void par_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
4a501606 2134{
891a2fe7 2135 if (arm_feature(env, ARM_FEATURE_LPAE)) {
8d5c773e 2136 raw_write(env, ri, value);
891a2fe7 2137 } else if (arm_feature(env, ARM_FEATURE_V7)) {
8d5c773e 2138 raw_write(env, ri, value & 0xfffff6ff);
4a501606 2139 } else {
8d5c773e 2140 raw_write(env, ri, value & 0xfffff1ff);
4a501606 2141 }
4a501606
PM
2142}
2143
2144#ifndef CONFIG_USER_ONLY
2145/* get_phys_addr() isn't present for user-mode-only targets */
702a9357 2146
3f208fd7
PM
2147static CPAccessResult ats_access(CPUARMState *env, const ARMCPRegInfo *ri,
2148 bool isread)
92611c00
PM
2149{
2150 if (ri->opc2 & 4) {
87562e4f
PM
2151 /* The ATS12NSO* operations must trap to EL3 if executed in
2152 * Secure EL1 (which can only happen if EL3 is AArch64).
2153 * They are simply UNDEF if executed from NS EL1.
2154 * They function normally from EL2 or EL3.
92611c00 2155 */
87562e4f
PM
2156 if (arm_current_el(env) == 1) {
2157 if (arm_is_secure_below_el3(env)) {
2158 return CP_ACCESS_TRAP_UNCATEGORIZED_EL3;
2159 }
2160 return CP_ACCESS_TRAP_UNCATEGORIZED;
2161 }
92611c00
PM
2162 }
2163 return CP_ACCESS_OK;
2164}
2165
060e8a48 2166static uint64_t do_ats_write(CPUARMState *env, uint64_t value,
03ae85f8 2167 MMUAccessType access_type, ARMMMUIdx mmu_idx)
4a501606 2168{
a8170e5e 2169 hwaddr phys_addr;
4a501606
PM
2170 target_ulong page_size;
2171 int prot;
b7cc4e82 2172 bool ret;
01c097f7 2173 uint64_t par64;
1313e2d7 2174 bool format64 = false;
8bf5b6a9 2175 MemTxAttrs attrs = {};
e14b5a23 2176 ARMMMUFaultInfo fi = {};
5b2d261d 2177 ARMCacheAttrs cacheattrs = {};
4a501606 2178
5b2d261d 2179 ret = get_phys_addr(env, value, access_type, mmu_idx, &phys_addr, &attrs,
bc52bfeb 2180 &prot, &page_size, &fi, &cacheattrs);
1313e2d7
EI
2181
2182 if (is_a64(env)) {
2183 format64 = true;
2184 } else if (arm_feature(env, ARM_FEATURE_LPAE)) {
2185 /*
2186 * ATS1Cxx:
2187 * * TTBCR.EAE determines whether the result is returned using the
2188 * 32-bit or the 64-bit PAR format
2189 * * Instructions executed in Hyp mode always use the 64bit format
2190 *
2191 * ATS1S2NSOxx uses the 64bit format if any of the following is true:
2192 * * The Non-secure TTBCR.EAE bit is set to 1
2193 * * The implementation includes EL2, and the value of HCR.VM is 1
2194 *
2195 * ATS1Hx always uses the 64bit format (not supported yet).
2196 */
2197 format64 = arm_s1_regime_using_lpae_format(env, mmu_idx);
2198
2199 if (arm_feature(env, ARM_FEATURE_EL2)) {
2200 if (mmu_idx == ARMMMUIdx_S12NSE0 || mmu_idx == ARMMMUIdx_S12NSE1) {
2201 format64 |= env->cp15.hcr_el2 & HCR_VM;
2202 } else {
2203 format64 |= arm_current_el(env) == 2;
2204 }
2205 }
2206 }
2207
2208 if (format64) {
5efe9ed4 2209 /* Create a 64-bit PAR */
01c097f7 2210 par64 = (1 << 11); /* LPAE bit always set */
b7cc4e82 2211 if (!ret) {
702a9357 2212 par64 |= phys_addr & ~0xfffULL;
8bf5b6a9
PM
2213 if (!attrs.secure) {
2214 par64 |= (1 << 9); /* NS */
2215 }
5b2d261d
AB
2216 par64 |= (uint64_t)cacheattrs.attrs << 56; /* ATTR */
2217 par64 |= cacheattrs.shareability << 7; /* SH */
4a501606 2218 } else {
5efe9ed4
PM
2219 uint32_t fsr = arm_fi_to_lfsc(&fi);
2220
702a9357 2221 par64 |= 1; /* F */
b7cc4e82 2222 par64 |= (fsr & 0x3f) << 1; /* FS */
702a9357
PM
2223 /* Note that S2WLK and FSTAGE are always zero, because we don't
2224 * implement virtualization and therefore there can't be a stage 2
2225 * fault.
2226 */
4a501606
PM
2227 }
2228 } else {
b7cc4e82 2229 /* fsr is a DFSR/IFSR value for the short descriptor
702a9357
PM
2230 * translation table format (with WnR always clear).
2231 * Convert it to a 32-bit PAR.
2232 */
b7cc4e82 2233 if (!ret) {
702a9357
PM
2234 /* We do not set any attribute bits in the PAR */
2235 if (page_size == (1 << 24)
2236 && arm_feature(env, ARM_FEATURE_V7)) {
01c097f7 2237 par64 = (phys_addr & 0xff000000) | (1 << 1);
702a9357 2238 } else {
01c097f7 2239 par64 = phys_addr & 0xfffff000;
702a9357 2240 }
8bf5b6a9
PM
2241 if (!attrs.secure) {
2242 par64 |= (1 << 9); /* NS */
2243 }
702a9357 2244 } else {
5efe9ed4
PM
2245 uint32_t fsr = arm_fi_to_sfsc(&fi);
2246
b7cc4e82
PC
2247 par64 = ((fsr & (1 << 10)) >> 5) | ((fsr & (1 << 12)) >> 6) |
2248 ((fsr & 0xf) << 1) | 1;
702a9357 2249 }
4a501606 2250 }
060e8a48
PM
2251 return par64;
2252}
2253
2254static void ats_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
2255{
03ae85f8 2256 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
060e8a48 2257 uint64_t par64;
d3649702
PM
2258 ARMMMUIdx mmu_idx;
2259 int el = arm_current_el(env);
2260 bool secure = arm_is_secure_below_el3(env);
060e8a48 2261
d3649702
PM
2262 switch (ri->opc2 & 6) {
2263 case 0:
2264 /* stage 1 current state PL1: ATS1CPR, ATS1CPW */
2265 switch (el) {
2266 case 3:
2267 mmu_idx = ARMMMUIdx_S1E3;
2268 break;
2269 case 2:
2270 mmu_idx = ARMMMUIdx_S1NSE1;
2271 break;
2272 case 1:
2273 mmu_idx = secure ? ARMMMUIdx_S1SE1 : ARMMMUIdx_S1NSE1;
2274 break;
2275 default:
2276 g_assert_not_reached();
2277 }
2278 break;
2279 case 2:
2280 /* stage 1 current state PL0: ATS1CUR, ATS1CUW */
2281 switch (el) {
2282 case 3:
2283 mmu_idx = ARMMMUIdx_S1SE0;
2284 break;
2285 case 2:
2286 mmu_idx = ARMMMUIdx_S1NSE0;
2287 break;
2288 case 1:
2289 mmu_idx = secure ? ARMMMUIdx_S1SE0 : ARMMMUIdx_S1NSE0;
2290 break;
2291 default:
2292 g_assert_not_reached();
2293 }
2294 break;
2295 case 4:
2296 /* stage 1+2 NonSecure PL1: ATS12NSOPR, ATS12NSOPW */
2297 mmu_idx = ARMMMUIdx_S12NSE1;
2298 break;
2299 case 6:
2300 /* stage 1+2 NonSecure PL0: ATS12NSOUR, ATS12NSOUW */
2301 mmu_idx = ARMMMUIdx_S12NSE0;
2302 break;
2303 default:
2304 g_assert_not_reached();
2305 }
2306
2307 par64 = do_ats_write(env, value, access_type, mmu_idx);
01c097f7
FA
2308
2309 A32_BANKED_CURRENT_REG_SET(env, par, par64);
4a501606 2310}
060e8a48 2311
14db7fe0
PM
2312static void ats1h_write(CPUARMState *env, const ARMCPRegInfo *ri,
2313 uint64_t value)
2314{
03ae85f8 2315 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
14db7fe0
PM
2316 uint64_t par64;
2317
2318 par64 = do_ats_write(env, value, access_type, ARMMMUIdx_S2NS);
2319
2320 A32_BANKED_CURRENT_REG_SET(env, par, par64);
2321}
2322
3f208fd7
PM
2323static CPAccessResult at_s1e2_access(CPUARMState *env, const ARMCPRegInfo *ri,
2324 bool isread)
2a47df95
PM
2325{
2326 if (arm_current_el(env) == 3 && !(env->cp15.scr_el3 & SCR_NS)) {
2327 return CP_ACCESS_TRAP;
2328 }
2329 return CP_ACCESS_OK;
2330}
2331
060e8a48
PM
2332static void ats_write64(CPUARMState *env, const ARMCPRegInfo *ri,
2333 uint64_t value)
2334{
03ae85f8 2335 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
d3649702
PM
2336 ARMMMUIdx mmu_idx;
2337 int secure = arm_is_secure_below_el3(env);
2338
2339 switch (ri->opc2 & 6) {
2340 case 0:
2341 switch (ri->opc1) {
2342 case 0: /* AT S1E1R, AT S1E1W */
2343 mmu_idx = secure ? ARMMMUIdx_S1SE1 : ARMMMUIdx_S1NSE1;
2344 break;
2345 case 4: /* AT S1E2R, AT S1E2W */
2346 mmu_idx = ARMMMUIdx_S1E2;
2347 break;
2348 case 6: /* AT S1E3R, AT S1E3W */
2349 mmu_idx = ARMMMUIdx_S1E3;
2350 break;
2351 default:
2352 g_assert_not_reached();
2353 }
2354 break;
2355 case 2: /* AT S1E0R, AT S1E0W */
2356 mmu_idx = secure ? ARMMMUIdx_S1SE0 : ARMMMUIdx_S1NSE0;
2357 break;
2358 case 4: /* AT S12E1R, AT S12E1W */
2a47df95 2359 mmu_idx = secure ? ARMMMUIdx_S1SE1 : ARMMMUIdx_S12NSE1;
d3649702
PM
2360 break;
2361 case 6: /* AT S12E0R, AT S12E0W */
2a47df95 2362 mmu_idx = secure ? ARMMMUIdx_S1SE0 : ARMMMUIdx_S12NSE0;
d3649702
PM
2363 break;
2364 default:
2365 g_assert_not_reached();
2366 }
060e8a48 2367
d3649702 2368 env->cp15.par_el[1] = do_ats_write(env, value, access_type, mmu_idx);
060e8a48 2369}
4a501606
PM
2370#endif
2371
2372static const ARMCPRegInfo vapa_cp_reginfo[] = {
2373 { .name = "PAR", .cp = 15, .crn = 7, .crm = 4, .opc1 = 0, .opc2 = 0,
2374 .access = PL1_RW, .resetvalue = 0,
01c097f7
FA
2375 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.par_s),
2376 offsetoflow32(CPUARMState, cp15.par_ns) },
4a501606
PM
2377 .writefn = par_write },
2378#ifndef CONFIG_USER_ONLY
87562e4f 2379 /* This underdecoding is safe because the reginfo is NO_RAW. */
4a501606 2380 { .name = "ATS", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = CP_ANY,
92611c00 2381 .access = PL1_W, .accessfn = ats_access,
7a0e58fa 2382 .writefn = ats_write, .type = ARM_CP_NO_RAW },
4a501606
PM
2383#endif
2384 REGINFO_SENTINEL
2385};
2386
18032bec
PM
2387/* Return basic MPU access permission bits. */
2388static uint32_t simple_mpu_ap_bits(uint32_t val)
2389{
2390 uint32_t ret;
2391 uint32_t mask;
2392 int i;
2393 ret = 0;
2394 mask = 3;
2395 for (i = 0; i < 16; i += 2) {
2396 ret |= (val >> i) & mask;
2397 mask <<= 2;
2398 }
2399 return ret;
2400}
2401
2402/* Pad basic MPU access permission bits to extended format. */
2403static uint32_t extended_mpu_ap_bits(uint32_t val)
2404{
2405 uint32_t ret;
2406 uint32_t mask;
2407 int i;
2408 ret = 0;
2409 mask = 3;
2410 for (i = 0; i < 16; i += 2) {
2411 ret |= (val & mask) << i;
2412 mask <<= 2;
2413 }
2414 return ret;
2415}
2416
c4241c7d
PM
2417static void pmsav5_data_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
2418 uint64_t value)
18032bec 2419{
7e09797c 2420 env->cp15.pmsav5_data_ap = extended_mpu_ap_bits(value);
18032bec
PM
2421}
2422
c4241c7d 2423static uint64_t pmsav5_data_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
18032bec 2424{
7e09797c 2425 return simple_mpu_ap_bits(env->cp15.pmsav5_data_ap);
18032bec
PM
2426}
2427
c4241c7d
PM
2428static void pmsav5_insn_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
2429 uint64_t value)
18032bec 2430{
7e09797c 2431 env->cp15.pmsav5_insn_ap = extended_mpu_ap_bits(value);
18032bec
PM
2432}
2433
c4241c7d 2434static uint64_t pmsav5_insn_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
18032bec 2435{
7e09797c 2436 return simple_mpu_ap_bits(env->cp15.pmsav5_insn_ap);
18032bec
PM
2437}
2438
6cb0b013
PC
2439static uint64_t pmsav7_read(CPUARMState *env, const ARMCPRegInfo *ri)
2440{
2441 uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
2442
2443 if (!u32p) {
2444 return 0;
2445 }
2446
1bc04a88 2447 u32p += env->pmsav7.rnr[M_REG_NS];
6cb0b013
PC
2448 return *u32p;
2449}
2450
2451static void pmsav7_write(CPUARMState *env, const ARMCPRegInfo *ri,
2452 uint64_t value)
2453{
2454 ARMCPU *cpu = arm_env_get_cpu(env);
2455 uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
2456
2457 if (!u32p) {
2458 return;
2459 }
2460
1bc04a88 2461 u32p += env->pmsav7.rnr[M_REG_NS];
d10eb08f 2462 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
6cb0b013
PC
2463 *u32p = value;
2464}
2465
6cb0b013
PC
2466static void pmsav7_rgnr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2467 uint64_t value)
2468{
2469 ARMCPU *cpu = arm_env_get_cpu(env);
2470 uint32_t nrgs = cpu->pmsav7_dregion;
2471
2472 if (value >= nrgs) {
2473 qemu_log_mask(LOG_GUEST_ERROR,
2474 "PMSAv7 RGNR write >= # supported regions, %" PRIu32
2475 " > %" PRIu32 "\n", (uint32_t)value, nrgs);
2476 return;
2477 }
2478
2479 raw_write(env, ri, value);
2480}
2481
2482static const ARMCPRegInfo pmsav7_cp_reginfo[] = {
69ceea64
PM
2483 /* Reset for all these registers is handled in arm_cpu_reset(),
2484 * because the PMSAv7 is also used by M-profile CPUs, which do
2485 * not register cpregs but still need the state to be reset.
2486 */
6cb0b013
PC
2487 { .name = "DRBAR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 0,
2488 .access = PL1_RW, .type = ARM_CP_NO_RAW,
2489 .fieldoffset = offsetof(CPUARMState, pmsav7.drbar),
69ceea64
PM
2490 .readfn = pmsav7_read, .writefn = pmsav7_write,
2491 .resetfn = arm_cp_reset_ignore },
6cb0b013
PC
2492 { .name = "DRSR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 2,
2493 .access = PL1_RW, .type = ARM_CP_NO_RAW,
2494 .fieldoffset = offsetof(CPUARMState, pmsav7.drsr),
69ceea64
PM
2495 .readfn = pmsav7_read, .writefn = pmsav7_write,
2496 .resetfn = arm_cp_reset_ignore },
6cb0b013
PC
2497 { .name = "DRACR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 4,
2498 .access = PL1_RW, .type = ARM_CP_NO_RAW,
2499 .fieldoffset = offsetof(CPUARMState, pmsav7.dracr),
69ceea64
PM
2500 .readfn = pmsav7_read, .writefn = pmsav7_write,
2501 .resetfn = arm_cp_reset_ignore },
6cb0b013
PC
2502 { .name = "RGNR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 2, .opc2 = 0,
2503 .access = PL1_RW,
1bc04a88 2504 .fieldoffset = offsetof(CPUARMState, pmsav7.rnr[M_REG_NS]),
69ceea64
PM
2505 .writefn = pmsav7_rgnr_write,
2506 .resetfn = arm_cp_reset_ignore },
6cb0b013
PC
2507 REGINFO_SENTINEL
2508};
2509
18032bec
PM
2510static const ARMCPRegInfo pmsav5_cp_reginfo[] = {
2511 { .name = "DATA_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
7a0e58fa 2512 .access = PL1_RW, .type = ARM_CP_ALIAS,
7e09797c 2513 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
18032bec
PM
2514 .readfn = pmsav5_data_ap_read, .writefn = pmsav5_data_ap_write, },
2515 { .name = "INSN_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
7a0e58fa 2516 .access = PL1_RW, .type = ARM_CP_ALIAS,
7e09797c 2517 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
18032bec
PM
2518 .readfn = pmsav5_insn_ap_read, .writefn = pmsav5_insn_ap_write, },
2519 { .name = "DATA_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 2,
2520 .access = PL1_RW,
7e09797c
PM
2521 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
2522 .resetvalue = 0, },
18032bec
PM
2523 { .name = "INSN_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 3,
2524 .access = PL1_RW,
7e09797c
PM
2525 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
2526 .resetvalue = 0, },
ecce5c3c
PM
2527 { .name = "DCACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
2528 .access = PL1_RW,
2529 .fieldoffset = offsetof(CPUARMState, cp15.c2_data), .resetvalue = 0, },
2530 { .name = "ICACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 1,
2531 .access = PL1_RW,
2532 .fieldoffset = offsetof(CPUARMState, cp15.c2_insn), .resetvalue = 0, },
06d76f31 2533 /* Protection region base and size registers */
e508a92b
PM
2534 { .name = "946_PRBS0", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0,
2535 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2536 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[0]) },
2537 { .name = "946_PRBS1", .cp = 15, .crn = 6, .crm = 1, .opc1 = 0,
2538 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2539 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[1]) },
2540 { .name = "946_PRBS2", .cp = 15, .crn = 6, .crm = 2, .opc1 = 0,
2541 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2542 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[2]) },
2543 { .name = "946_PRBS3", .cp = 15, .crn = 6, .crm = 3, .opc1 = 0,
2544 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2545 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[3]) },
2546 { .name = "946_PRBS4", .cp = 15, .crn = 6, .crm = 4, .opc1 = 0,
2547 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2548 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[4]) },
2549 { .name = "946_PRBS5", .cp = 15, .crn = 6, .crm = 5, .opc1 = 0,
2550 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2551 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[5]) },
2552 { .name = "946_PRBS6", .cp = 15, .crn = 6, .crm = 6, .opc1 = 0,
2553 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2554 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[6]) },
2555 { .name = "946_PRBS7", .cp = 15, .crn = 6, .crm = 7, .opc1 = 0,
2556 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2557 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[7]) },
18032bec
PM
2558 REGINFO_SENTINEL
2559};
2560
c4241c7d
PM
2561static void vmsa_ttbcr_raw_write(CPUARMState *env, const ARMCPRegInfo *ri,
2562 uint64_t value)
ecce5c3c 2563{
11f136ee 2564 TCR *tcr = raw_ptr(env, ri);
2ebcebe2
PM
2565 int maskshift = extract32(value, 0, 3);
2566
e389be16
FA
2567 if (!arm_feature(env, ARM_FEATURE_V8)) {
2568 if (arm_feature(env, ARM_FEATURE_LPAE) && (value & TTBCR_EAE)) {
2569 /* Pre ARMv8 bits [21:19], [15:14] and [6:3] are UNK/SBZP when
2570 * using Long-desciptor translation table format */
2571 value &= ~((7 << 19) | (3 << 14) | (0xf << 3));
2572 } else if (arm_feature(env, ARM_FEATURE_EL3)) {
2573 /* In an implementation that includes the Security Extensions
2574 * TTBCR has additional fields PD0 [4] and PD1 [5] for
2575 * Short-descriptor translation table format.
2576 */
2577 value &= TTBCR_PD1 | TTBCR_PD0 | TTBCR_N;
2578 } else {
2579 value &= TTBCR_N;
2580 }
e42c4db3 2581 }
e389be16 2582
b6af0975 2583 /* Update the masks corresponding to the TCR bank being written
11f136ee 2584 * Note that we always calculate mask and base_mask, but
e42c4db3 2585 * they are only used for short-descriptor tables (ie if EAE is 0);
11f136ee
FA
2586 * for long-descriptor tables the TCR fields are used differently
2587 * and the mask and base_mask values are meaningless.
e42c4db3 2588 */
11f136ee
FA
2589 tcr->raw_tcr = value;
2590 tcr->mask = ~(((uint32_t)0xffffffffu) >> maskshift);
2591 tcr->base_mask = ~((uint32_t)0x3fffu >> maskshift);
ecce5c3c
PM
2592}
2593
c4241c7d
PM
2594static void vmsa_ttbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2595 uint64_t value)
d4e6df63 2596{
00c8cb0a
AF
2597 ARMCPU *cpu = arm_env_get_cpu(env);
2598
d4e6df63
PM
2599 if (arm_feature(env, ARM_FEATURE_LPAE)) {
2600 /* With LPAE the TTBCR could result in a change of ASID
2601 * via the TTBCR.A1 bit, so do a TLB flush.
2602 */
d10eb08f 2603 tlb_flush(CPU(cpu));
d4e6df63 2604 }
c4241c7d 2605 vmsa_ttbcr_raw_write(env, ri, value);
d4e6df63
PM
2606}
2607
ecce5c3c
PM
2608static void vmsa_ttbcr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2609{
11f136ee
FA
2610 TCR *tcr = raw_ptr(env, ri);
2611
2612 /* Reset both the TCR as well as the masks corresponding to the bank of
2613 * the TCR being reset.
2614 */
2615 tcr->raw_tcr = 0;
2616 tcr->mask = 0;
2617 tcr->base_mask = 0xffffc000u;
ecce5c3c
PM
2618}
2619
cb2e37df
PM
2620static void vmsa_tcr_el1_write(CPUARMState *env, const ARMCPRegInfo *ri,
2621 uint64_t value)
2622{
00c8cb0a 2623 ARMCPU *cpu = arm_env_get_cpu(env);
11f136ee 2624 TCR *tcr = raw_ptr(env, ri);
00c8cb0a 2625
cb2e37df 2626 /* For AArch64 the A1 bit could result in a change of ASID, so TLB flush. */
d10eb08f 2627 tlb_flush(CPU(cpu));
11f136ee 2628 tcr->raw_tcr = value;
cb2e37df
PM
2629}
2630
327ed10f
PM
2631static void vmsa_ttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2632 uint64_t value)
2633{
2634 /* 64 bit accesses to the TTBRs can change the ASID and so we
2635 * must flush the TLB.
2636 */
2637 if (cpreg_field_is_64bit(ri)) {
00c8cb0a
AF
2638 ARMCPU *cpu = arm_env_get_cpu(env);
2639
d10eb08f 2640 tlb_flush(CPU(cpu));
327ed10f
PM
2641 }
2642 raw_write(env, ri, value);
2643}
2644
b698e9cf
EI
2645static void vttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2646 uint64_t value)
2647{
2648 ARMCPU *cpu = arm_env_get_cpu(env);
2649 CPUState *cs = CPU(cpu);
2650
2651 /* Accesses to VTTBR may change the VMID so we must flush the TLB. */
2652 if (raw_read(env, ri) != value) {
0336cbf8 2653 tlb_flush_by_mmuidx(cs,
8bd5c820
PM
2654 ARMMMUIdxBit_S12NSE1 |
2655 ARMMMUIdxBit_S12NSE0 |
2656 ARMMMUIdxBit_S2NS);
b698e9cf
EI
2657 raw_write(env, ri, value);
2658 }
2659}
2660
8e5d75c9 2661static const ARMCPRegInfo vmsa_pmsa_cp_reginfo[] = {
18032bec 2662 { .name = "DFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
7a0e58fa 2663 .access = PL1_RW, .type = ARM_CP_ALIAS,
4a7e2d73 2664 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dfsr_s),
b061a82b 2665 offsetoflow32(CPUARMState, cp15.dfsr_ns) }, },
18032bec 2666 { .name = "IFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
88ca1c2d
FA
2667 .access = PL1_RW, .resetvalue = 0,
2668 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.ifsr_s),
2669 offsetoflow32(CPUARMState, cp15.ifsr_ns) } },
8e5d75c9
PC
2670 { .name = "DFAR", .cp = 15, .opc1 = 0, .crn = 6, .crm = 0, .opc2 = 0,
2671 .access = PL1_RW, .resetvalue = 0,
2672 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.dfar_s),
2673 offsetof(CPUARMState, cp15.dfar_ns) } },
2674 { .name = "FAR_EL1", .state = ARM_CP_STATE_AA64,
2675 .opc0 = 3, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 0,
2676 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[1]),
2677 .resetvalue = 0, },
2678 REGINFO_SENTINEL
2679};
2680
2681static const ARMCPRegInfo vmsa_cp_reginfo[] = {
6cd8a264
RH
2682 { .name = "ESR_EL1", .state = ARM_CP_STATE_AA64,
2683 .opc0 = 3, .crn = 5, .crm = 2, .opc1 = 0, .opc2 = 0,
2684 .access = PL1_RW,
d81c519c 2685 .fieldoffset = offsetof(CPUARMState, cp15.esr_el[1]), .resetvalue = 0, },
327ed10f 2686 { .name = "TTBR0_EL1", .state = ARM_CP_STATE_BOTH,
7dd8c9af
FA
2687 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 0,
2688 .access = PL1_RW, .writefn = vmsa_ttbr_write, .resetvalue = 0,
2689 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
2690 offsetof(CPUARMState, cp15.ttbr0_ns) } },
327ed10f 2691 { .name = "TTBR1_EL1", .state = ARM_CP_STATE_BOTH,
7dd8c9af
FA
2692 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 1,
2693 .access = PL1_RW, .writefn = vmsa_ttbr_write, .resetvalue = 0,
2694 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
2695 offsetof(CPUARMState, cp15.ttbr1_ns) } },
cb2e37df
PM
2696 { .name = "TCR_EL1", .state = ARM_CP_STATE_AA64,
2697 .opc0 = 3, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
2698 .access = PL1_RW, .writefn = vmsa_tcr_el1_write,
2699 .resetfn = vmsa_ttbcr_reset, .raw_writefn = raw_write,
11f136ee 2700 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[1]) },
cb2e37df 2701 { .name = "TTBCR", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
7a0e58fa 2702 .access = PL1_RW, .type = ARM_CP_ALIAS, .writefn = vmsa_ttbcr_write,
b061a82b 2703 .raw_writefn = vmsa_ttbcr_raw_write,
11f136ee
FA
2704 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tcr_el[3]),
2705 offsetoflow32(CPUARMState, cp15.tcr_el[1])} },
18032bec
PM
2706 REGINFO_SENTINEL
2707};
2708
c4241c7d
PM
2709static void omap_ticonfig_write(CPUARMState *env, const ARMCPRegInfo *ri,
2710 uint64_t value)
1047b9d7
PM
2711{
2712 env->cp15.c15_ticonfig = value & 0xe7;
2713 /* The OS_TYPE bit in this register changes the reported CPUID! */
2714 env->cp15.c0_cpuid = (value & (1 << 5)) ?
2715 ARM_CPUID_TI915T : ARM_CPUID_TI925T;
1047b9d7
PM
2716}
2717
c4241c7d
PM
2718static void omap_threadid_write(CPUARMState *env, const ARMCPRegInfo *ri,
2719 uint64_t value)
1047b9d7
PM
2720{
2721 env->cp15.c15_threadid = value & 0xffff;
1047b9d7
PM
2722}
2723
c4241c7d
PM
2724static void omap_wfi_write(CPUARMState *env, const ARMCPRegInfo *ri,
2725 uint64_t value)
1047b9d7
PM
2726{
2727 /* Wait-for-interrupt (deprecated) */
c3affe56 2728 cpu_interrupt(CPU(arm_env_get_cpu(env)), CPU_INTERRUPT_HALT);
1047b9d7
PM
2729}
2730
c4241c7d
PM
2731static void omap_cachemaint_write(CPUARMState *env, const ARMCPRegInfo *ri,
2732 uint64_t value)
c4804214
PM
2733{
2734 /* On OMAP there are registers indicating the max/min index of dcache lines
2735 * containing a dirty line; cache flush operations have to reset these.
2736 */
2737 env->cp15.c15_i_max = 0x000;
2738 env->cp15.c15_i_min = 0xff0;
c4804214
PM
2739}
2740
18032bec
PM
2741static const ARMCPRegInfo omap_cp_reginfo[] = {
2742 { .name = "DFSR", .cp = 15, .crn = 5, .crm = CP_ANY,
2743 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_OVERRIDE,
d81c519c 2744 .fieldoffset = offsetoflow32(CPUARMState, cp15.esr_el[1]),
6cd8a264 2745 .resetvalue = 0, },
1047b9d7
PM
2746 { .name = "", .cp = 15, .crn = 15, .crm = 0, .opc1 = 0, .opc2 = 0,
2747 .access = PL1_RW, .type = ARM_CP_NOP },
2748 { .name = "TICONFIG", .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0,
2749 .access = PL1_RW,
2750 .fieldoffset = offsetof(CPUARMState, cp15.c15_ticonfig), .resetvalue = 0,
2751 .writefn = omap_ticonfig_write },
2752 { .name = "IMAX", .cp = 15, .crn = 15, .crm = 2, .opc1 = 0, .opc2 = 0,
2753 .access = PL1_RW,
2754 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_max), .resetvalue = 0, },
2755 { .name = "IMIN", .cp = 15, .crn = 15, .crm = 3, .opc1 = 0, .opc2 = 0,
2756 .access = PL1_RW, .resetvalue = 0xff0,
2757 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_min) },
2758 { .name = "THREADID", .cp = 15, .crn = 15, .crm = 4, .opc1 = 0, .opc2 = 0,
2759 .access = PL1_RW,
2760 .fieldoffset = offsetof(CPUARMState, cp15.c15_threadid), .resetvalue = 0,
2761 .writefn = omap_threadid_write },
2762 { .name = "TI925T_STATUS", .cp = 15, .crn = 15,
2763 .crm = 8, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
7a0e58fa 2764 .type = ARM_CP_NO_RAW,
1047b9d7
PM
2765 .readfn = arm_cp_read_zero, .writefn = omap_wfi_write, },
2766 /* TODO: Peripheral port remap register:
2767 * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt controller
2768 * base address at $rn & ~0xfff and map size of 0x200 << ($rn & 0xfff),
2769 * when MMU is off.
2770 */
c4804214 2771 { .name = "OMAP_CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
d4e6df63 2772 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
7a0e58fa 2773 .type = ARM_CP_OVERRIDE | ARM_CP_NO_RAW,
c4804214 2774 .writefn = omap_cachemaint_write },
34f90529
PM
2775 { .name = "C9", .cp = 15, .crn = 9,
2776 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW,
2777 .type = ARM_CP_CONST | ARM_CP_OVERRIDE, .resetvalue = 0 },
1047b9d7
PM
2778 REGINFO_SENTINEL
2779};
2780
c4241c7d
PM
2781static void xscale_cpar_write(CPUARMState *env, const ARMCPRegInfo *ri,
2782 uint64_t value)
1047b9d7 2783{
c0f4af17 2784 env->cp15.c15_cpar = value & 0x3fff;
1047b9d7
PM
2785}
2786
2787static const ARMCPRegInfo xscale_cp_reginfo[] = {
2788 { .name = "XSCALE_CPAR",
2789 .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
2790 .fieldoffset = offsetof(CPUARMState, cp15.c15_cpar), .resetvalue = 0,
2791 .writefn = xscale_cpar_write, },
2771db27
PM
2792 { .name = "XSCALE_AUXCR",
2793 .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 1, .access = PL1_RW,
2794 .fieldoffset = offsetof(CPUARMState, cp15.c1_xscaleauxcr),
2795 .resetvalue = 0, },
3b771579
PM
2796 /* XScale specific cache-lockdown: since we have no cache we NOP these
2797 * and hope the guest does not really rely on cache behaviour.
2798 */
2799 { .name = "XSCALE_LOCK_ICACHE_LINE",
2800 .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 0,
2801 .access = PL1_W, .type = ARM_CP_NOP },
2802 { .name = "XSCALE_UNLOCK_ICACHE",
2803 .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 1,
2804 .access = PL1_W, .type = ARM_CP_NOP },
2805 { .name = "XSCALE_DCACHE_LOCK",
2806 .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 0,
2807 .access = PL1_RW, .type = ARM_CP_NOP },
2808 { .name = "XSCALE_UNLOCK_DCACHE",
2809 .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 1,
2810 .access = PL1_W, .type = ARM_CP_NOP },
1047b9d7
PM
2811 REGINFO_SENTINEL
2812};
2813
2814static const ARMCPRegInfo dummy_c15_cp_reginfo[] = {
2815 /* RAZ/WI the whole crn=15 space, when we don't have a more specific
2816 * implementation of this implementation-defined space.
2817 * Ideally this should eventually disappear in favour of actually
2818 * implementing the correct behaviour for all cores.
2819 */
2820 { .name = "C15_IMPDEF", .cp = 15, .crn = 15,
2821 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
3671cd87 2822 .access = PL1_RW,
7a0e58fa 2823 .type = ARM_CP_CONST | ARM_CP_NO_RAW | ARM_CP_OVERRIDE,
d4e6df63 2824 .resetvalue = 0 },
18032bec
PM
2825 REGINFO_SENTINEL
2826};
2827
c4804214
PM
2828static const ARMCPRegInfo cache_dirty_status_cp_reginfo[] = {
2829 /* Cache status: RAZ because we have no cache so it's always clean */
2830 { .name = "CDSR", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 6,
7a0e58fa 2831 .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 2832 .resetvalue = 0 },
c4804214
PM
2833 REGINFO_SENTINEL
2834};
2835
2836static const ARMCPRegInfo cache_block_ops_cp_reginfo[] = {
2837 /* We never have a a block transfer operation in progress */
2838 { .name = "BXSR", .cp = 15, .crn = 7, .crm = 12, .opc1 = 0, .opc2 = 4,
7a0e58fa 2839 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 2840 .resetvalue = 0 },
30b05bba
PM
2841 /* The cache ops themselves: these all NOP for QEMU */
2842 { .name = "IICR", .cp = 15, .crm = 5, .opc1 = 0,
2843 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
2844 { .name = "IDCR", .cp = 15, .crm = 6, .opc1 = 0,
2845 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
2846 { .name = "CDCR", .cp = 15, .crm = 12, .opc1 = 0,
2847 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
2848 { .name = "PIR", .cp = 15, .crm = 12, .opc1 = 1,
2849 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
2850 { .name = "PDR", .cp = 15, .crm = 12, .opc1 = 2,
2851 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
2852 { .name = "CIDCR", .cp = 15, .crm = 14, .opc1 = 0,
2853 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
c4804214
PM
2854 REGINFO_SENTINEL
2855};
2856
2857static const ARMCPRegInfo cache_test_clean_cp_reginfo[] = {
2858 /* The cache test-and-clean instructions always return (1 << 30)
2859 * to indicate that there are no dirty cache lines.
2860 */
2861 { .name = "TC_DCACHE", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 3,
7a0e58fa 2862 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 2863 .resetvalue = (1 << 30) },
c4804214 2864 { .name = "TCI_DCACHE", .cp = 15, .crn = 7, .crm = 14, .opc1 = 0, .opc2 = 3,
7a0e58fa 2865 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 2866 .resetvalue = (1 << 30) },
c4804214
PM
2867 REGINFO_SENTINEL
2868};
2869
34f90529
PM
2870static const ARMCPRegInfo strongarm_cp_reginfo[] = {
2871 /* Ignore ReadBuffer accesses */
2872 { .name = "C9_READBUFFER", .cp = 15, .crn = 9,
2873 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
d4e6df63 2874 .access = PL1_RW, .resetvalue = 0,
7a0e58fa 2875 .type = ARM_CP_CONST | ARM_CP_OVERRIDE | ARM_CP_NO_RAW },
34f90529
PM
2876 REGINFO_SENTINEL
2877};
2878
731de9e6
EI
2879static uint64_t midr_read(CPUARMState *env, const ARMCPRegInfo *ri)
2880{
2881 ARMCPU *cpu = arm_env_get_cpu(env);
2882 unsigned int cur_el = arm_current_el(env);
2883 bool secure = arm_is_secure(env);
2884
2885 if (arm_feature(&cpu->env, ARM_FEATURE_EL2) && !secure && cur_el == 1) {
2886 return env->cp15.vpidr_el2;
2887 }
2888 return raw_read(env, ri);
2889}
2890
06a7e647 2891static uint64_t mpidr_read_val(CPUARMState *env)
81bdde9d 2892{
eb5e1d3c
PF
2893 ARMCPU *cpu = ARM_CPU(arm_env_get_cpu(env));
2894 uint64_t mpidr = cpu->mp_affinity;
2895
81bdde9d 2896 if (arm_feature(env, ARM_FEATURE_V7MP)) {
78dbbbe4 2897 mpidr |= (1U << 31);
81bdde9d
PM
2898 /* Cores which are uniprocessor (non-coherent)
2899 * but still implement the MP extensions set
a8e81b31 2900 * bit 30. (For instance, Cortex-R5).
81bdde9d 2901 */
a8e81b31
PC
2902 if (cpu->mp_is_up) {
2903 mpidr |= (1u << 30);
2904 }
81bdde9d 2905 }
c4241c7d 2906 return mpidr;
81bdde9d
PM
2907}
2908
06a7e647
EI
2909static uint64_t mpidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
2910{
f0d574d6
EI
2911 unsigned int cur_el = arm_current_el(env);
2912 bool secure = arm_is_secure(env);
2913
2914 if (arm_feature(env, ARM_FEATURE_EL2) && !secure && cur_el == 1) {
2915 return env->cp15.vmpidr_el2;
2916 }
06a7e647
EI
2917 return mpidr_read_val(env);
2918}
2919
81bdde9d 2920static const ARMCPRegInfo mpidr_cp_reginfo[] = {
4b7fff2f
PM
2921 { .name = "MPIDR", .state = ARM_CP_STATE_BOTH,
2922 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 5,
7a0e58fa 2923 .access = PL1_R, .readfn = mpidr_read, .type = ARM_CP_NO_RAW },
81bdde9d
PM
2924 REGINFO_SENTINEL
2925};
2926
7ac681cf 2927static const ARMCPRegInfo lpae_cp_reginfo[] = {
a903c449 2928 /* NOP AMAIR0/1 */
b0fe2427
PM
2929 { .name = "AMAIR0", .state = ARM_CP_STATE_BOTH,
2930 .opc0 = 3, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 0,
a903c449 2931 .access = PL1_RW, .type = ARM_CP_CONST,
7ac681cf 2932 .resetvalue = 0 },
b0fe2427 2933 /* AMAIR1 is mapped to AMAIR_EL1[63:32] */
7ac681cf 2934 { .name = "AMAIR1", .cp = 15, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 1,
a903c449 2935 .access = PL1_RW, .type = ARM_CP_CONST,
7ac681cf 2936 .resetvalue = 0 },
891a2fe7 2937 { .name = "PAR", .cp = 15, .crm = 7, .opc1 = 0,
01c097f7
FA
2938 .access = PL1_RW, .type = ARM_CP_64BIT, .resetvalue = 0,
2939 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.par_s),
2940 offsetof(CPUARMState, cp15.par_ns)} },
891a2fe7 2941 { .name = "TTBR0", .cp = 15, .crm = 2, .opc1 = 0,
7a0e58fa 2942 .access = PL1_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
7dd8c9af
FA
2943 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
2944 offsetof(CPUARMState, cp15.ttbr0_ns) },
b061a82b 2945 .writefn = vmsa_ttbr_write, },
891a2fe7 2946 { .name = "TTBR1", .cp = 15, .crm = 2, .opc1 = 1,
7a0e58fa 2947 .access = PL1_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
7dd8c9af
FA
2948 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
2949 offsetof(CPUARMState, cp15.ttbr1_ns) },
b061a82b 2950 .writefn = vmsa_ttbr_write, },
7ac681cf
PM
2951 REGINFO_SENTINEL
2952};
2953
c4241c7d 2954static uint64_t aa64_fpcr_read(CPUARMState *env, const ARMCPRegInfo *ri)
b0d2b7d0 2955{
c4241c7d 2956 return vfp_get_fpcr(env);
b0d2b7d0
PM
2957}
2958
c4241c7d
PM
2959static void aa64_fpcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2960 uint64_t value)
b0d2b7d0
PM
2961{
2962 vfp_set_fpcr(env, value);
b0d2b7d0
PM
2963}
2964
c4241c7d 2965static uint64_t aa64_fpsr_read(CPUARMState *env, const ARMCPRegInfo *ri)
b0d2b7d0 2966{
c4241c7d 2967 return vfp_get_fpsr(env);
b0d2b7d0
PM
2968}
2969
c4241c7d
PM
2970static void aa64_fpsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2971 uint64_t value)
b0d2b7d0
PM
2972{
2973 vfp_set_fpsr(env, value);
b0d2b7d0
PM
2974}
2975
3f208fd7
PM
2976static CPAccessResult aa64_daif_access(CPUARMState *env, const ARMCPRegInfo *ri,
2977 bool isread)
c2b820fe 2978{
137feaa9 2979 if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_UMA)) {
c2b820fe
PM
2980 return CP_ACCESS_TRAP;
2981 }
2982 return CP_ACCESS_OK;
2983}
2984
2985static void aa64_daif_write(CPUARMState *env, const ARMCPRegInfo *ri,
2986 uint64_t value)
2987{
2988 env->daif = value & PSTATE_DAIF;
2989}
2990
8af35c37 2991static CPAccessResult aa64_cacheop_access(CPUARMState *env,
3f208fd7
PM
2992 const ARMCPRegInfo *ri,
2993 bool isread)
8af35c37
PM
2994{
2995 /* Cache invalidate/clean: NOP, but EL0 must UNDEF unless
2996 * SCTLR_EL1.UCI is set.
2997 */
137feaa9 2998 if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_UCI)) {
8af35c37
PM
2999 return CP_ACCESS_TRAP;
3000 }
3001 return CP_ACCESS_OK;
3002}
3003
dbb1fb27
AB
3004/* See: D4.7.2 TLB maintenance requirements and the TLB maintenance instructions
3005 * Page D4-1736 (DDI0487A.b)
3006 */
3007
fd3ed969
PM
3008static void tlbi_aa64_vmalle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
3009 uint64_t value)
168aa23b 3010{
a67cf277 3011 CPUState *cs = ENV_GET_CPU(env);
dbb1fb27 3012
fd3ed969 3013 if (arm_is_secure_below_el3(env)) {
0336cbf8 3014 tlb_flush_by_mmuidx(cs,
8bd5c820
PM
3015 ARMMMUIdxBit_S1SE1 |
3016 ARMMMUIdxBit_S1SE0);
fd3ed969 3017 } else {
0336cbf8 3018 tlb_flush_by_mmuidx(cs,
8bd5c820
PM
3019 ARMMMUIdxBit_S12NSE1 |
3020 ARMMMUIdxBit_S12NSE0);
fd3ed969 3021 }
168aa23b
PM
3022}
3023
fd3ed969
PM
3024static void tlbi_aa64_vmalle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
3025 uint64_t value)
168aa23b 3026{
a67cf277 3027 CPUState *cs = ENV_GET_CPU(env);
fd3ed969 3028 bool sec = arm_is_secure_below_el3(env);
dbb1fb27 3029
a67cf277
AB
3030 if (sec) {
3031 tlb_flush_by_mmuidx_all_cpus_synced(cs,
8bd5c820
PM
3032 ARMMMUIdxBit_S1SE1 |
3033 ARMMMUIdxBit_S1SE0);
a67cf277
AB
3034 } else {
3035 tlb_flush_by_mmuidx_all_cpus_synced(cs,
8bd5c820
PM
3036 ARMMMUIdxBit_S12NSE1 |
3037 ARMMMUIdxBit_S12NSE0);
fd3ed969 3038 }
168aa23b
PM
3039}
3040
fd3ed969
PM
3041static void tlbi_aa64_alle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
3042 uint64_t value)
168aa23b 3043{
fd3ed969
PM
3044 /* Note that the 'ALL' scope must invalidate both stage 1 and
3045 * stage 2 translations, whereas most other scopes only invalidate
3046 * stage 1 translations.
3047 */
00c8cb0a 3048 ARMCPU *cpu = arm_env_get_cpu(env);
fd3ed969
PM
3049 CPUState *cs = CPU(cpu);
3050
3051 if (arm_is_secure_below_el3(env)) {
0336cbf8 3052 tlb_flush_by_mmuidx(cs,
8bd5c820
PM
3053 ARMMMUIdxBit_S1SE1 |
3054 ARMMMUIdxBit_S1SE0);
fd3ed969
PM
3055 } else {
3056 if (arm_feature(env, ARM_FEATURE_EL2)) {
0336cbf8 3057 tlb_flush_by_mmuidx(cs,
8bd5c820
PM
3058 ARMMMUIdxBit_S12NSE1 |
3059 ARMMMUIdxBit_S12NSE0 |
3060 ARMMMUIdxBit_S2NS);
fd3ed969 3061 } else {
0336cbf8 3062 tlb_flush_by_mmuidx(cs,
8bd5c820
PM
3063 ARMMMUIdxBit_S12NSE1 |
3064 ARMMMUIdxBit_S12NSE0);
fd3ed969
PM
3065 }
3066 }
168aa23b
PM
3067}
3068
fd3ed969 3069static void tlbi_aa64_alle2_write(CPUARMState *env, const ARMCPRegInfo *ri,
fa439fc5
PM
3070 uint64_t value)
3071{
fd3ed969
PM
3072 ARMCPU *cpu = arm_env_get_cpu(env);
3073 CPUState *cs = CPU(cpu);
3074
8bd5c820 3075 tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_S1E2);
fd3ed969
PM
3076}
3077
43efaa33
PM
3078static void tlbi_aa64_alle3_write(CPUARMState *env, const ARMCPRegInfo *ri,
3079 uint64_t value)
3080{
3081 ARMCPU *cpu = arm_env_get_cpu(env);
3082 CPUState *cs = CPU(cpu);
3083
8bd5c820 3084 tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_S1E3);
43efaa33
PM
3085}
3086
fd3ed969
PM
3087static void tlbi_aa64_alle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
3088 uint64_t value)
3089{
3090 /* Note that the 'ALL' scope must invalidate both stage 1 and
3091 * stage 2 translations, whereas most other scopes only invalidate
3092 * stage 1 translations.
3093 */
a67cf277 3094 CPUState *cs = ENV_GET_CPU(env);
fd3ed969
PM
3095 bool sec = arm_is_secure_below_el3(env);
3096 bool has_el2 = arm_feature(env, ARM_FEATURE_EL2);
a67cf277
AB
3097
3098 if (sec) {
3099 tlb_flush_by_mmuidx_all_cpus_synced(cs,
8bd5c820
PM
3100 ARMMMUIdxBit_S1SE1 |
3101 ARMMMUIdxBit_S1SE0);
a67cf277
AB
3102 } else if (has_el2) {
3103 tlb_flush_by_mmuidx_all_cpus_synced(cs,
8bd5c820
PM
3104 ARMMMUIdxBit_S12NSE1 |
3105 ARMMMUIdxBit_S12NSE0 |
3106 ARMMMUIdxBit_S2NS);
a67cf277
AB
3107 } else {
3108 tlb_flush_by_mmuidx_all_cpus_synced(cs,
8bd5c820
PM
3109 ARMMMUIdxBit_S12NSE1 |
3110 ARMMMUIdxBit_S12NSE0);
fa439fc5
PM
3111 }
3112}
3113
2bfb9d75
PM
3114static void tlbi_aa64_alle2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
3115 uint64_t value)
3116{
a67cf277 3117 CPUState *cs = ENV_GET_CPU(env);
2bfb9d75 3118
8bd5c820 3119 tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_S1E2);
2bfb9d75
PM
3120}
3121
43efaa33
PM
3122static void tlbi_aa64_alle3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
3123 uint64_t value)
3124{
a67cf277 3125 CPUState *cs = ENV_GET_CPU(env);
43efaa33 3126
8bd5c820 3127 tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_S1E3);
43efaa33
PM
3128}
3129
fd3ed969
PM
3130static void tlbi_aa64_vae1_write(CPUARMState *env, const ARMCPRegInfo *ri,
3131 uint64_t value)
3132{
3133 /* Invalidate by VA, EL1&0 (AArch64 version).
3134 * Currently handles all of VAE1, VAAE1, VAALE1 and VALE1,
3135 * since we don't support flush-for-specific-ASID-only or
3136 * flush-last-level-only.
3137 */
3138 ARMCPU *cpu = arm_env_get_cpu(env);
3139 CPUState *cs = CPU(cpu);
3140 uint64_t pageaddr = sextract64(value << 12, 0, 56);
3141
3142 if (arm_is_secure_below_el3(env)) {
0336cbf8 3143 tlb_flush_page_by_mmuidx(cs, pageaddr,
8bd5c820
PM
3144 ARMMMUIdxBit_S1SE1 |
3145 ARMMMUIdxBit_S1SE0);
fd3ed969 3146 } else {
0336cbf8 3147 tlb_flush_page_by_mmuidx(cs, pageaddr,
8bd5c820
PM
3148 ARMMMUIdxBit_S12NSE1 |
3149 ARMMMUIdxBit_S12NSE0);
fd3ed969
PM
3150 }
3151}
3152
3153static void tlbi_aa64_vae2_write(CPUARMState *env, const ARMCPRegInfo *ri,
3154 uint64_t value)
fa439fc5 3155{
fd3ed969
PM
3156 /* Invalidate by VA, EL2
3157 * Currently handles both VAE2 and VALE2, since we don't support
3158 * flush-last-level-only.
3159 */
3160 ARMCPU *cpu = arm_env_get_cpu(env);
3161 CPUState *cs = CPU(cpu);
3162 uint64_t pageaddr = sextract64(value << 12, 0, 56);
3163
8bd5c820 3164 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_S1E2);
fd3ed969
PM
3165}
3166
43efaa33
PM
3167static void tlbi_aa64_vae3_write(CPUARMState *env, const ARMCPRegInfo *ri,
3168 uint64_t value)
3169{
3170 /* Invalidate by VA, EL3
3171 * Currently handles both VAE3 and VALE3, since we don't support
3172 * flush-last-level-only.
3173 */
3174 ARMCPU *cpu = arm_env_get_cpu(env);
3175 CPUState *cs = CPU(cpu);
3176 uint64_t pageaddr = sextract64(value << 12, 0, 56);
3177
8bd5c820 3178 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_S1E3);
43efaa33
PM
3179}
3180
fd3ed969
PM
3181static void tlbi_aa64_vae1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
3182 uint64_t value)
3183{
a67cf277
AB
3184 ARMCPU *cpu = arm_env_get_cpu(env);
3185 CPUState *cs = CPU(cpu);
fd3ed969 3186 bool sec = arm_is_secure_below_el3(env);
fa439fc5
PM
3187 uint64_t pageaddr = sextract64(value << 12, 0, 56);
3188
a67cf277
AB
3189 if (sec) {
3190 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
8bd5c820
PM
3191 ARMMMUIdxBit_S1SE1 |
3192 ARMMMUIdxBit_S1SE0);
a67cf277
AB
3193 } else {
3194 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
8bd5c820
PM
3195 ARMMMUIdxBit_S12NSE1 |
3196 ARMMMUIdxBit_S12NSE0);
fa439fc5
PM
3197 }
3198}
3199
fd3ed969
PM
3200static void tlbi_aa64_vae2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
3201 uint64_t value)
fa439fc5 3202{
a67cf277 3203 CPUState *cs = ENV_GET_CPU(env);
fd3ed969 3204 uint64_t pageaddr = sextract64(value << 12, 0, 56);
fa439fc5 3205
a67cf277 3206 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
8bd5c820 3207 ARMMMUIdxBit_S1E2);
fa439fc5
PM
3208}
3209
43efaa33
PM
3210static void tlbi_aa64_vae3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
3211 uint64_t value)
3212{
a67cf277 3213 CPUState *cs = ENV_GET_CPU(env);
43efaa33
PM
3214 uint64_t pageaddr = sextract64(value << 12, 0, 56);
3215
a67cf277 3216 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
8bd5c820 3217 ARMMMUIdxBit_S1E3);
43efaa33
PM
3218}
3219
cea66e91
PM
3220static void tlbi_aa64_ipas2e1_write(CPUARMState *env, const ARMCPRegInfo *ri,
3221 uint64_t value)
3222{
3223 /* Invalidate by IPA. This has to invalidate any structures that
3224 * contain only stage 2 translation information, but does not need
3225 * to apply to structures that contain combined stage 1 and stage 2
3226 * translation information.
3227 * This must NOP if EL2 isn't implemented or SCR_EL3.NS is zero.
3228 */
3229 ARMCPU *cpu = arm_env_get_cpu(env);
3230 CPUState *cs = CPU(cpu);
3231 uint64_t pageaddr;
3232
3233 if (!arm_feature(env, ARM_FEATURE_EL2) || !(env->cp15.scr_el3 & SCR_NS)) {
3234 return;
3235 }
3236
3237 pageaddr = sextract64(value << 12, 0, 48);
3238
8bd5c820 3239 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_S2NS);
cea66e91
PM
3240}
3241
3242static void tlbi_aa64_ipas2e1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
3243 uint64_t value)
3244{
a67cf277 3245 CPUState *cs = ENV_GET_CPU(env);
cea66e91
PM
3246 uint64_t pageaddr;
3247
3248 if (!arm_feature(env, ARM_FEATURE_EL2) || !(env->cp15.scr_el3 & SCR_NS)) {
3249 return;
3250 }
3251
3252 pageaddr = sextract64(value << 12, 0, 48);
3253
a67cf277 3254 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
8bd5c820 3255 ARMMMUIdxBit_S2NS);
cea66e91
PM
3256}
3257
3f208fd7
PM
3258static CPAccessResult aa64_zva_access(CPUARMState *env, const ARMCPRegInfo *ri,
3259 bool isread)
aca3f40b
PM
3260{
3261 /* We don't implement EL2, so the only control on DC ZVA is the
3262 * bit in the SCTLR which can prohibit access for EL0.
3263 */
137feaa9 3264 if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_DZE)) {
aca3f40b
PM
3265 return CP_ACCESS_TRAP;
3266 }
3267 return CP_ACCESS_OK;
3268}
3269
3270static uint64_t aa64_dczid_read(CPUARMState *env, const ARMCPRegInfo *ri)
3271{
3272 ARMCPU *cpu = arm_env_get_cpu(env);
3273 int dzp_bit = 1 << 4;
3274
3275 /* DZP indicates whether DC ZVA access is allowed */
3f208fd7 3276 if (aa64_zva_access(env, NULL, false) == CP_ACCESS_OK) {
aca3f40b
PM
3277 dzp_bit = 0;
3278 }
3279 return cpu->dcz_blocksize | dzp_bit;
3280}
3281
3f208fd7
PM
3282static CPAccessResult sp_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
3283 bool isread)
f502cfc2 3284{
cdcf1405 3285 if (!(env->pstate & PSTATE_SP)) {
f502cfc2
PM
3286 /* Access to SP_EL0 is undefined if it's being used as
3287 * the stack pointer.
3288 */
3289 return CP_ACCESS_TRAP_UNCATEGORIZED;
3290 }
3291 return CP_ACCESS_OK;
3292}
3293
3294static uint64_t spsel_read(CPUARMState *env, const ARMCPRegInfo *ri)
3295{
3296 return env->pstate & PSTATE_SP;
3297}
3298
3299static void spsel_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
3300{
3301 update_spsel(env, val);
3302}
3303
137feaa9
FA
3304static void sctlr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3305 uint64_t value)
3306{
3307 ARMCPU *cpu = arm_env_get_cpu(env);
3308
3309 if (raw_read(env, ri) == value) {
3310 /* Skip the TLB flush if nothing actually changed; Linux likes
3311 * to do a lot of pointless SCTLR writes.
3312 */
3313 return;
3314 }
3315
06312feb
PM
3316 if (arm_feature(env, ARM_FEATURE_PMSA) && !cpu->has_mpu) {
3317 /* M bit is RAZ/WI for PMSA with no MPU implemented */
3318 value &= ~SCTLR_M;
3319 }
3320
137feaa9
FA
3321 raw_write(env, ri, value);
3322 /* ??? Lots of these bits are not implemented. */
3323 /* This may enable/disable the MMU, so do a TLB flush. */
d10eb08f 3324 tlb_flush(CPU(cpu));
137feaa9
FA
3325}
3326
3f208fd7
PM
3327static CPAccessResult fpexc32_access(CPUARMState *env, const ARMCPRegInfo *ri,
3328 bool isread)
03fbf20f
PM
3329{
3330 if ((env->cp15.cptr_el[2] & CPTR_TFP) && arm_current_el(env) == 2) {
f2cae609 3331 return CP_ACCESS_TRAP_FP_EL2;
03fbf20f
PM
3332 }
3333 if (env->cp15.cptr_el[3] & CPTR_TFP) {
f2cae609 3334 return CP_ACCESS_TRAP_FP_EL3;
03fbf20f
PM
3335 }
3336 return CP_ACCESS_OK;
3337}
3338
a8d64e73
PM
3339static void sdcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3340 uint64_t value)
3341{
3342 env->cp15.mdcr_el3 = value & SDCR_VALID_MASK;
3343}
3344
b0d2b7d0
PM
3345static const ARMCPRegInfo v8_cp_reginfo[] = {
3346 /* Minimal set of EL0-visible registers. This will need to be expanded
3347 * significantly for system emulation of AArch64 CPUs.
3348 */
3349 { .name = "NZCV", .state = ARM_CP_STATE_AA64,
3350 .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 2,
3351 .access = PL0_RW, .type = ARM_CP_NZCV },
c2b820fe
PM
3352 { .name = "DAIF", .state = ARM_CP_STATE_AA64,
3353 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 2,
7a0e58fa 3354 .type = ARM_CP_NO_RAW,
c2b820fe
PM
3355 .access = PL0_RW, .accessfn = aa64_daif_access,
3356 .fieldoffset = offsetof(CPUARMState, daif),
3357 .writefn = aa64_daif_write, .resetfn = arm_cp_reset_ignore },
b0d2b7d0
PM
3358 { .name = "FPCR", .state = ARM_CP_STATE_AA64,
3359 .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 4,
b916c9c3 3360 .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END,
fe03d45f 3361 .readfn = aa64_fpcr_read, .writefn = aa64_fpcr_write },
b0d2b7d0
PM
3362 { .name = "FPSR", .state = ARM_CP_STATE_AA64,
3363 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 4,
b916c9c3 3364 .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END,
fe03d45f 3365 .readfn = aa64_fpsr_read, .writefn = aa64_fpsr_write },
b0d2b7d0
PM
3366 { .name = "DCZID_EL0", .state = ARM_CP_STATE_AA64,
3367 .opc0 = 3, .opc1 = 3, .opc2 = 7, .crn = 0, .crm = 0,
7a0e58fa 3368 .access = PL0_R, .type = ARM_CP_NO_RAW,
aca3f40b
PM
3369 .readfn = aa64_dczid_read },
3370 { .name = "DC_ZVA", .state = ARM_CP_STATE_AA64,
3371 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 1,
3372 .access = PL0_W, .type = ARM_CP_DC_ZVA,
3373#ifndef CONFIG_USER_ONLY
3374 /* Avoid overhead of an access check that always passes in user-mode */
3375 .accessfn = aa64_zva_access,
3376#endif
3377 },
0eef9d98
PM
3378 { .name = "CURRENTEL", .state = ARM_CP_STATE_AA64,
3379 .opc0 = 3, .opc1 = 0, .opc2 = 2, .crn = 4, .crm = 2,
3380 .access = PL1_R, .type = ARM_CP_CURRENTEL },
8af35c37
PM
3381 /* Cache ops: all NOPs since we don't emulate caches */
3382 { .name = "IC_IALLUIS", .state = ARM_CP_STATE_AA64,
3383 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
3384 .access = PL1_W, .type = ARM_CP_NOP },
3385 { .name = "IC_IALLU", .state = ARM_CP_STATE_AA64,
3386 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
3387 .access = PL1_W, .type = ARM_CP_NOP },
3388 { .name = "IC_IVAU", .state = ARM_CP_STATE_AA64,
3389 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 5, .opc2 = 1,
3390 .access = PL0_W, .type = ARM_CP_NOP,
3391 .accessfn = aa64_cacheop_access },
3392 { .name = "DC_IVAC", .state = ARM_CP_STATE_AA64,
3393 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
3394 .access = PL1_W, .type = ARM_CP_NOP },
3395 { .name = "DC_ISW", .state = ARM_CP_STATE_AA64,
3396 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
3397 .access = PL1_W, .type = ARM_CP_NOP },
3398 { .name = "DC_CVAC", .state = ARM_CP_STATE_AA64,
3399 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 1,
3400 .access = PL0_W, .type = ARM_CP_NOP,
3401 .accessfn = aa64_cacheop_access },
3402 { .name = "DC_CSW", .state = ARM_CP_STATE_AA64,
3403 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
3404 .access = PL1_W, .type = ARM_CP_NOP },
3405 { .name = "DC_CVAU", .state = ARM_CP_STATE_AA64,
3406 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 11, .opc2 = 1,
3407 .access = PL0_W, .type = ARM_CP_NOP,
3408 .accessfn = aa64_cacheop_access },
3409 { .name = "DC_CIVAC", .state = ARM_CP_STATE_AA64,
3410 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 1,
3411 .access = PL0_W, .type = ARM_CP_NOP,
3412 .accessfn = aa64_cacheop_access },
3413 { .name = "DC_CISW", .state = ARM_CP_STATE_AA64,
3414 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
3415 .access = PL1_W, .type = ARM_CP_NOP },
168aa23b
PM
3416 /* TLBI operations */
3417 { .name = "TLBI_VMALLE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 3418 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
7a0e58fa 3419 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 3420 .writefn = tlbi_aa64_vmalle1is_write },
168aa23b 3421 { .name = "TLBI_VAE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 3422 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
7a0e58fa 3423 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 3424 .writefn = tlbi_aa64_vae1is_write },
168aa23b 3425 { .name = "TLBI_ASIDE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 3426 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
7a0e58fa 3427 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 3428 .writefn = tlbi_aa64_vmalle1is_write },
168aa23b 3429 { .name = "TLBI_VAAE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 3430 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
7a0e58fa 3431 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 3432 .writefn = tlbi_aa64_vae1is_write },
168aa23b 3433 { .name = "TLBI_VALE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 3434 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
7a0e58fa 3435 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 3436 .writefn = tlbi_aa64_vae1is_write },
168aa23b 3437 { .name = "TLBI_VAALE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 3438 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
7a0e58fa 3439 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 3440 .writefn = tlbi_aa64_vae1is_write },
168aa23b 3441 { .name = "TLBI_VMALLE1", .state = ARM_CP_STATE_AA64,
6ab9f499 3442 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
7a0e58fa 3443 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 3444 .writefn = tlbi_aa64_vmalle1_write },
168aa23b 3445 { .name = "TLBI_VAE1", .state = ARM_CP_STATE_AA64,
6ab9f499 3446 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
7a0e58fa 3447 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 3448 .writefn = tlbi_aa64_vae1_write },
168aa23b 3449 { .name = "TLBI_ASIDE1", .state = ARM_CP_STATE_AA64,
6ab9f499 3450 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
7a0e58fa 3451 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 3452 .writefn = tlbi_aa64_vmalle1_write },
168aa23b 3453 { .name = "TLBI_VAAE1", .state = ARM_CP_STATE_AA64,
6ab9f499 3454 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
7a0e58fa 3455 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 3456 .writefn = tlbi_aa64_vae1_write },
168aa23b 3457 { .name = "TLBI_VALE1", .state = ARM_CP_STATE_AA64,
6ab9f499 3458 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
7a0e58fa 3459 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 3460 .writefn = tlbi_aa64_vae1_write },
168aa23b 3461 { .name = "TLBI_VAALE1", .state = ARM_CP_STATE_AA64,
6ab9f499 3462 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
7a0e58fa 3463 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 3464 .writefn = tlbi_aa64_vae1_write },
cea66e91
PM
3465 { .name = "TLBI_IPAS2E1IS", .state = ARM_CP_STATE_AA64,
3466 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1,
3467 .access = PL2_W, .type = ARM_CP_NO_RAW,
3468 .writefn = tlbi_aa64_ipas2e1is_write },
3469 { .name = "TLBI_IPAS2LE1IS", .state = ARM_CP_STATE_AA64,
3470 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5,
3471 .access = PL2_W, .type = ARM_CP_NO_RAW,
3472 .writefn = tlbi_aa64_ipas2e1is_write },
83ddf975
PM
3473 { .name = "TLBI_ALLE1IS", .state = ARM_CP_STATE_AA64,
3474 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
3475 .access = PL2_W, .type = ARM_CP_NO_RAW,
fd3ed969 3476 .writefn = tlbi_aa64_alle1is_write },
43efaa33
PM
3477 { .name = "TLBI_VMALLS12E1IS", .state = ARM_CP_STATE_AA64,
3478 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 6,
3479 .access = PL2_W, .type = ARM_CP_NO_RAW,
3480 .writefn = tlbi_aa64_alle1is_write },
cea66e91
PM
3481 { .name = "TLBI_IPAS2E1", .state = ARM_CP_STATE_AA64,
3482 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1,
3483 .access = PL2_W, .type = ARM_CP_NO_RAW,
3484 .writefn = tlbi_aa64_ipas2e1_write },
3485 { .name = "TLBI_IPAS2LE1", .state = ARM_CP_STATE_AA64,
3486 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5,
3487 .access = PL2_W, .type = ARM_CP_NO_RAW,
3488 .writefn = tlbi_aa64_ipas2e1_write },
83ddf975
PM
3489 { .name = "TLBI_ALLE1", .state = ARM_CP_STATE_AA64,
3490 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
3491 .access = PL2_W, .type = ARM_CP_NO_RAW,
fd3ed969 3492 .writefn = tlbi_aa64_alle1_write },
43efaa33
PM
3493 { .name = "TLBI_VMALLS12E1", .state = ARM_CP_STATE_AA64,
3494 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 6,
3495 .access = PL2_W, .type = ARM_CP_NO_RAW,
3496 .writefn = tlbi_aa64_alle1is_write },
19525524
PM
3497#ifndef CONFIG_USER_ONLY
3498 /* 64 bit address translation operations */
3499 { .name = "AT_S1E1R", .state = ARM_CP_STATE_AA64,
3500 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 0,
060e8a48 3501 .access = PL1_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
19525524
PM
3502 { .name = "AT_S1E1W", .state = ARM_CP_STATE_AA64,
3503 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 1,
060e8a48 3504 .access = PL1_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
19525524
PM
3505 { .name = "AT_S1E0R", .state = ARM_CP_STATE_AA64,
3506 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 2,
060e8a48 3507 .access = PL1_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
19525524
PM
3508 { .name = "AT_S1E0W", .state = ARM_CP_STATE_AA64,
3509 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 3,
060e8a48 3510 .access = PL1_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
2a47df95 3511 { .name = "AT_S12E1R", .state = ARM_CP_STATE_AA64,
7a379c7e 3512 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 4,
2a47df95
PM
3513 .access = PL2_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3514 { .name = "AT_S12E1W", .state = ARM_CP_STATE_AA64,
7a379c7e 3515 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 5,
2a47df95
PM
3516 .access = PL2_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3517 { .name = "AT_S12E0R", .state = ARM_CP_STATE_AA64,
7a379c7e 3518 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 6,
2a47df95
PM
3519 .access = PL2_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3520 { .name = "AT_S12E0W", .state = ARM_CP_STATE_AA64,
7a379c7e 3521 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 7,
2a47df95
PM
3522 .access = PL2_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3523 /* AT S1E2* are elsewhere as they UNDEF from EL3 if EL2 is not present */
3524 { .name = "AT_S1E3R", .state = ARM_CP_STATE_AA64,
3525 .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 0,
3526 .access = PL3_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3527 { .name = "AT_S1E3W", .state = ARM_CP_STATE_AA64,
3528 .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 1,
3529 .access = PL3_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
c96fc9b5
EI
3530 { .name = "PAR_EL1", .state = ARM_CP_STATE_AA64,
3531 .type = ARM_CP_ALIAS,
3532 .opc0 = 3, .opc1 = 0, .crn = 7, .crm = 4, .opc2 = 0,
3533 .access = PL1_RW, .resetvalue = 0,
3534 .fieldoffset = offsetof(CPUARMState, cp15.par_el[1]),
3535 .writefn = par_write },
19525524 3536#endif
995939a6 3537 /* TLB invalidate last level of translation table walk */
9449fdf6 3538 { .name = "TLBIMVALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
7a0e58fa 3539 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_is_write },
9449fdf6 3540 { .name = "TLBIMVAALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
7a0e58fa 3541 .type = ARM_CP_NO_RAW, .access = PL1_W,
fa439fc5 3542 .writefn = tlbimvaa_is_write },
9449fdf6 3543 { .name = "TLBIMVAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
7a0e58fa 3544 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write },
9449fdf6 3545 { .name = "TLBIMVAAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
7a0e58fa 3546 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimvaa_write },
541ef8c2
SS
3547 { .name = "TLBIMVALH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
3548 .type = ARM_CP_NO_RAW, .access = PL2_W,
3549 .writefn = tlbimva_hyp_write },
3550 { .name = "TLBIMVALHIS",
3551 .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
3552 .type = ARM_CP_NO_RAW, .access = PL2_W,
3553 .writefn = tlbimva_hyp_is_write },
3554 { .name = "TLBIIPAS2",
3555 .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1,
3556 .type = ARM_CP_NO_RAW, .access = PL2_W,
3557 .writefn = tlbiipas2_write },
3558 { .name = "TLBIIPAS2IS",
3559 .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1,
3560 .type = ARM_CP_NO_RAW, .access = PL2_W,
3561 .writefn = tlbiipas2_is_write },
3562 { .name = "TLBIIPAS2L",
3563 .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5,
3564 .type = ARM_CP_NO_RAW, .access = PL2_W,
3565 .writefn = tlbiipas2_write },
3566 { .name = "TLBIIPAS2LIS",
3567 .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5,
3568 .type = ARM_CP_NO_RAW, .access = PL2_W,
3569 .writefn = tlbiipas2_is_write },
9449fdf6
PM
3570 /* 32 bit cache operations */
3571 { .name = "ICIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
3572 .type = ARM_CP_NOP, .access = PL1_W },
3573 { .name = "BPIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 6,
3574 .type = ARM_CP_NOP, .access = PL1_W },
3575 { .name = "ICIALLU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
3576 .type = ARM_CP_NOP, .access = PL1_W },
3577 { .name = "ICIMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 1,
3578 .type = ARM_CP_NOP, .access = PL1_W },
3579 { .name = "BPIALL", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 6,
3580 .type = ARM_CP_NOP, .access = PL1_W },
3581 { .name = "BPIMVA", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 7,
3582 .type = ARM_CP_NOP, .access = PL1_W },
3583 { .name = "DCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
3584 .type = ARM_CP_NOP, .access = PL1_W },
3585 { .name = "DCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
3586 .type = ARM_CP_NOP, .access = PL1_W },
3587 { .name = "DCCMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 1,
3588 .type = ARM_CP_NOP, .access = PL1_W },
3589 { .name = "DCCSW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
3590 .type = ARM_CP_NOP, .access = PL1_W },
3591 { .name = "DCCMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 11, .opc2 = 1,
3592 .type = ARM_CP_NOP, .access = PL1_W },
3593 { .name = "DCCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 1,
3594 .type = ARM_CP_NOP, .access = PL1_W },
3595 { .name = "DCCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
3596 .type = ARM_CP_NOP, .access = PL1_W },
3597 /* MMU Domain access control / MPU write buffer control */
0c17d68c
FA
3598 { .name = "DACR", .cp = 15, .opc1 = 0, .crn = 3, .crm = 0, .opc2 = 0,
3599 .access = PL1_RW, .resetvalue = 0,
3600 .writefn = dacr_write, .raw_writefn = raw_write,
3601 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
3602 offsetoflow32(CPUARMState, cp15.dacr_ns) } },
a0618a19 3603 { .name = "ELR_EL1", .state = ARM_CP_STATE_AA64,
7a0e58fa 3604 .type = ARM_CP_ALIAS,
a0618a19 3605 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 1,
6947f059
EI
3606 .access = PL1_RW,
3607 .fieldoffset = offsetof(CPUARMState, elr_el[1]) },
a65f1de9 3608 { .name = "SPSR_EL1", .state = ARM_CP_STATE_AA64,
7a0e58fa 3609 .type = ARM_CP_ALIAS,
a65f1de9 3610 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 0,
99a99c1f
SB
3611 .access = PL1_RW,
3612 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_SVC]) },
f502cfc2
PM
3613 /* We rely on the access checks not allowing the guest to write to the
3614 * state field when SPSel indicates that it's being used as the stack
3615 * pointer.
3616 */
3617 { .name = "SP_EL0", .state = ARM_CP_STATE_AA64,
3618 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 1, .opc2 = 0,
3619 .access = PL1_RW, .accessfn = sp_el0_access,
7a0e58fa 3620 .type = ARM_CP_ALIAS,
f502cfc2 3621 .fieldoffset = offsetof(CPUARMState, sp_el[0]) },
884b4dee
GB
3622 { .name = "SP_EL1", .state = ARM_CP_STATE_AA64,
3623 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 1, .opc2 = 0,
7a0e58fa 3624 .access = PL2_RW, .type = ARM_CP_ALIAS,
884b4dee 3625 .fieldoffset = offsetof(CPUARMState, sp_el[1]) },
f502cfc2
PM
3626 { .name = "SPSel", .state = ARM_CP_STATE_AA64,
3627 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 0,
7a0e58fa 3628 .type = ARM_CP_NO_RAW,
f502cfc2 3629 .access = PL1_RW, .readfn = spsel_read, .writefn = spsel_write },
03fbf20f
PM
3630 { .name = "FPEXC32_EL2", .state = ARM_CP_STATE_AA64,
3631 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 3, .opc2 = 0,
3632 .type = ARM_CP_ALIAS,
3633 .fieldoffset = offsetof(CPUARMState, vfp.xregs[ARM_VFP_FPEXC]),
3634 .access = PL2_RW, .accessfn = fpexc32_access },
6a43e0b6
PM
3635 { .name = "DACR32_EL2", .state = ARM_CP_STATE_AA64,
3636 .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 0, .opc2 = 0,
3637 .access = PL2_RW, .resetvalue = 0,
3638 .writefn = dacr_write, .raw_writefn = raw_write,
3639 .fieldoffset = offsetof(CPUARMState, cp15.dacr32_el2) },
3640 { .name = "IFSR32_EL2", .state = ARM_CP_STATE_AA64,
3641 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 0, .opc2 = 1,
3642 .access = PL2_RW, .resetvalue = 0,
3643 .fieldoffset = offsetof(CPUARMState, cp15.ifsr32_el2) },
3644 { .name = "SPSR_IRQ", .state = ARM_CP_STATE_AA64,
3645 .type = ARM_CP_ALIAS,
3646 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 0,
3647 .access = PL2_RW,
3648 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_IRQ]) },
3649 { .name = "SPSR_ABT", .state = ARM_CP_STATE_AA64,
3650 .type = ARM_CP_ALIAS,
3651 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 1,
3652 .access = PL2_RW,
3653 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_ABT]) },
3654 { .name = "SPSR_UND", .state = ARM_CP_STATE_AA64,
3655 .type = ARM_CP_ALIAS,
3656 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 2,
3657 .access = PL2_RW,
3658 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_UND]) },
3659 { .name = "SPSR_FIQ", .state = ARM_CP_STATE_AA64,
3660 .type = ARM_CP_ALIAS,
3661 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 3,
3662 .access = PL2_RW,
3663 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_FIQ]) },
a8d64e73
PM
3664 { .name = "MDCR_EL3", .state = ARM_CP_STATE_AA64,
3665 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 3, .opc2 = 1,
3666 .resetvalue = 0,
3667 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el3) },
3668 { .name = "SDCR", .type = ARM_CP_ALIAS,
3669 .cp = 15, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 1,
3670 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
3671 .writefn = sdcr_write,
3672 .fieldoffset = offsetoflow32(CPUARMState, cp15.mdcr_el3) },
b0d2b7d0
PM
3673 REGINFO_SENTINEL
3674};
3675
d42e3c26 3676/* Used to describe the behaviour of EL2 regs when EL2 does not exist. */
4771cd01 3677static const ARMCPRegInfo el3_no_el2_cp_reginfo[] = {
d42e3c26
EI
3678 { .name = "VBAR_EL2", .state = ARM_CP_STATE_AA64,
3679 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0,
3680 .access = PL2_RW,
3681 .readfn = arm_cp_read_zero, .writefn = arm_cp_write_ignore },
f149e3e8 3682 { .name = "HCR_EL2", .state = ARM_CP_STATE_AA64,
7a0e58fa 3683 .type = ARM_CP_NO_RAW,
f149e3e8
EI
3684 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
3685 .access = PL2_RW,
3686 .readfn = arm_cp_read_zero, .writefn = arm_cp_write_ignore },
c6f19164
GB
3687 { .name = "CPTR_EL2", .state = ARM_CP_STATE_BOTH,
3688 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 2,
3689 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
95f949ac
EI
3690 { .name = "MAIR_EL2", .state = ARM_CP_STATE_BOTH,
3691 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 0,
3692 .access = PL2_RW, .type = ARM_CP_CONST,
3693 .resetvalue = 0 },
3694 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
3695 .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 1,
3696 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
2179ef95
PM
3697 { .name = "AMAIR_EL2", .state = ARM_CP_STATE_BOTH,
3698 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 0,
3699 .access = PL2_RW, .type = ARM_CP_CONST,
3700 .resetvalue = 0 },
3701 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
3702 .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 1,
3703 .access = PL2_RW, .type = ARM_CP_CONST,
3704 .resetvalue = 0 },
37cd6c24
PM
3705 { .name = "AFSR0_EL2", .state = ARM_CP_STATE_BOTH,
3706 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 0,
3707 .access = PL2_RW, .type = ARM_CP_CONST,
3708 .resetvalue = 0 },
3709 { .name = "AFSR1_EL2", .state = ARM_CP_STATE_BOTH,
3710 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 1,
3711 .access = PL2_RW, .type = ARM_CP_CONST,
3712 .resetvalue = 0 },
06ec4c8c
EI
3713 { .name = "TCR_EL2", .state = ARM_CP_STATE_BOTH,
3714 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 2,
3715 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
68e9c2fe
EI
3716 { .name = "VTCR_EL2", .state = ARM_CP_STATE_BOTH,
3717 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
3718 .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any,
3719 .type = ARM_CP_CONST, .resetvalue = 0 },
b698e9cf
EI
3720 { .name = "VTTBR", .state = ARM_CP_STATE_AA32,
3721 .cp = 15, .opc1 = 6, .crm = 2,
3722 .access = PL2_RW, .accessfn = access_el3_aa32ns,
3723 .type = ARM_CP_CONST | ARM_CP_64BIT, .resetvalue = 0 },
3724 { .name = "VTTBR_EL2", .state = ARM_CP_STATE_AA64,
3725 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 0,
3726 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
b9cb5323
EI
3727 { .name = "SCTLR_EL2", .state = ARM_CP_STATE_BOTH,
3728 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 0,
3729 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
ff05f37b
EI
3730 { .name = "TPIDR_EL2", .state = ARM_CP_STATE_BOTH,
3731 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 2,
3732 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
a57633c0
EI
3733 { .name = "TTBR0_EL2", .state = ARM_CP_STATE_AA64,
3734 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
3735 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3736 { .name = "HTTBR", .cp = 15, .opc1 = 4, .crm = 2,
3737 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_CONST,
3738 .resetvalue = 0 },
0b6440af
EI
3739 { .name = "CNTHCTL_EL2", .state = ARM_CP_STATE_BOTH,
3740 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 1, .opc2 = 0,
3741 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
edac4d8a
EI
3742 { .name = "CNTVOFF_EL2", .state = ARM_CP_STATE_AA64,
3743 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 3,
3744 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3745 { .name = "CNTVOFF", .cp = 15, .opc1 = 4, .crm = 14,
3746 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_CONST,
3747 .resetvalue = 0 },
b0e66d95
EI
3748 { .name = "CNTHP_CVAL_EL2", .state = ARM_CP_STATE_AA64,
3749 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 2,
3750 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3751 { .name = "CNTHP_CVAL", .cp = 15, .opc1 = 6, .crm = 14,
3752 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_CONST,
3753 .resetvalue = 0 },
3754 { .name = "CNTHP_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
3755 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 0,
3756 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3757 { .name = "CNTHP_CTL_EL2", .state = ARM_CP_STATE_BOTH,
3758 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 1,
3759 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
14cc7b54
SF
3760 { .name = "MDCR_EL2", .state = ARM_CP_STATE_BOTH,
3761 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 1,
d6c8cf81
PM
3762 .access = PL2_RW, .accessfn = access_tda,
3763 .type = ARM_CP_CONST, .resetvalue = 0 },
59e05530
EI
3764 { .name = "HPFAR_EL2", .state = ARM_CP_STATE_BOTH,
3765 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
3766 .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any,
3767 .type = ARM_CP_CONST, .resetvalue = 0 },
2a5a9abd
AF
3768 { .name = "HSTR_EL2", .state = ARM_CP_STATE_BOTH,
3769 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 3,
3770 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
d42e3c26
EI
3771 REGINFO_SENTINEL
3772};
3773
f149e3e8
EI
3774static void hcr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
3775{
3776 ARMCPU *cpu = arm_env_get_cpu(env);
3777 uint64_t valid_mask = HCR_MASK;
3778
3779 if (arm_feature(env, ARM_FEATURE_EL3)) {
3780 valid_mask &= ~HCR_HCD;
77077a83
JK
3781 } else if (cpu->psci_conduit != QEMU_PSCI_CONDUIT_SMC) {
3782 /* Architecturally HCR.TSC is RES0 if EL3 is not implemented.
3783 * However, if we're using the SMC PSCI conduit then QEMU is
3784 * effectively acting like EL3 firmware and so the guest at
3785 * EL2 should retain the ability to prevent EL1 from being
3786 * able to make SMC calls into the ersatz firmware, so in
3787 * that case HCR.TSC should be read/write.
3788 */
f149e3e8
EI
3789 valid_mask &= ~HCR_TSC;
3790 }
3791
3792 /* Clear RES0 bits. */
3793 value &= valid_mask;
3794
3795 /* These bits change the MMU setup:
3796 * HCR_VM enables stage 2 translation
3797 * HCR_PTW forbids certain page-table setups
3798 * HCR_DC Disables stage1 and enables stage2 translation
3799 */
3800 if ((raw_read(env, ri) ^ value) & (HCR_VM | HCR_PTW | HCR_DC)) {
d10eb08f 3801 tlb_flush(CPU(cpu));
f149e3e8
EI
3802 }
3803 raw_write(env, ri, value);
3804}
3805
4771cd01 3806static const ARMCPRegInfo el2_cp_reginfo[] = {
f149e3e8
EI
3807 { .name = "HCR_EL2", .state = ARM_CP_STATE_AA64,
3808 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
3809 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
3810 .writefn = hcr_write },
3b685ba7 3811 { .name = "ELR_EL2", .state = ARM_CP_STATE_AA64,
7a0e58fa 3812 .type = ARM_CP_ALIAS,
3b685ba7
EI
3813 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 1,
3814 .access = PL2_RW,
3815 .fieldoffset = offsetof(CPUARMState, elr_el[2]) },
f2c30f42 3816 { .name = "ESR_EL2", .state = ARM_CP_STATE_AA64,
f2c30f42
EI
3817 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 0,
3818 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[2]) },
63b60551
EI
3819 { .name = "FAR_EL2", .state = ARM_CP_STATE_AA64,
3820 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 0,
3821 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[2]) },
3b685ba7 3822 { .name = "SPSR_EL2", .state = ARM_CP_STATE_AA64,
7a0e58fa 3823 .type = ARM_CP_ALIAS,
3b685ba7 3824 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 0,
99a99c1f
SB
3825 .access = PL2_RW,
3826 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_HYP]) },
d42e3c26
EI
3827 { .name = "VBAR_EL2", .state = ARM_CP_STATE_AA64,
3828 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0,
3829 .access = PL2_RW, .writefn = vbar_write,
3830 .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[2]),
3831 .resetvalue = 0 },
884b4dee
GB
3832 { .name = "SP_EL2", .state = ARM_CP_STATE_AA64,
3833 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 1, .opc2 = 0,
7a0e58fa 3834 .access = PL3_RW, .type = ARM_CP_ALIAS,
884b4dee 3835 .fieldoffset = offsetof(CPUARMState, sp_el[2]) },
c6f19164
GB
3836 { .name = "CPTR_EL2", .state = ARM_CP_STATE_BOTH,
3837 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 2,
3838 .access = PL2_RW, .accessfn = cptr_access, .resetvalue = 0,
3839 .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[2]) },
95f949ac
EI
3840 { .name = "MAIR_EL2", .state = ARM_CP_STATE_BOTH,
3841 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 0,
3842 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[2]),
3843 .resetvalue = 0 },
3844 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
3845 .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 1,
3846 .access = PL2_RW, .type = ARM_CP_ALIAS,
3847 .fieldoffset = offsetofhigh32(CPUARMState, cp15.mair_el[2]) },
2179ef95
PM
3848 { .name = "AMAIR_EL2", .state = ARM_CP_STATE_BOTH,
3849 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 0,
3850 .access = PL2_RW, .type = ARM_CP_CONST,
3851 .resetvalue = 0 },
3852 /* HAMAIR1 is mapped to AMAIR_EL2[63:32] */
3853 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
3854 .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 1,
3855 .access = PL2_RW, .type = ARM_CP_CONST,
3856 .resetvalue = 0 },
37cd6c24
PM
3857 { .name = "AFSR0_EL2", .state = ARM_CP_STATE_BOTH,
3858 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 0,
3859 .access = PL2_RW, .type = ARM_CP_CONST,
3860 .resetvalue = 0 },
3861 { .name = "AFSR1_EL2", .state = ARM_CP_STATE_BOTH,
3862 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 1,
3863 .access = PL2_RW, .type = ARM_CP_CONST,
3864 .resetvalue = 0 },
06ec4c8c
EI
3865 { .name = "TCR_EL2", .state = ARM_CP_STATE_BOTH,
3866 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 2,
6459b94c
PM
3867 .access = PL2_RW,
3868 /* no .writefn needed as this can't cause an ASID change;
3869 * no .raw_writefn or .resetfn needed as we never use mask/base_mask
3870 */
06ec4c8c 3871 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[2]) },
68e9c2fe
EI
3872 { .name = "VTCR", .state = ARM_CP_STATE_AA32,
3873 .cp = 15, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
bf06c112 3874 .type = ARM_CP_ALIAS,
68e9c2fe
EI
3875 .access = PL2_RW, .accessfn = access_el3_aa32ns,
3876 .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) },
3877 { .name = "VTCR_EL2", .state = ARM_CP_STATE_AA64,
3878 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
bf06c112
PM
3879 .access = PL2_RW,
3880 /* no .writefn needed as this can't cause an ASID change;
3881 * no .raw_writefn or .resetfn needed as we never use mask/base_mask
3882 */
68e9c2fe 3883 .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) },
b698e9cf
EI
3884 { .name = "VTTBR", .state = ARM_CP_STATE_AA32,
3885 .cp = 15, .opc1 = 6, .crm = 2,
3886 .type = ARM_CP_64BIT | ARM_CP_ALIAS,
3887 .access = PL2_RW, .accessfn = access_el3_aa32ns,
3888 .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2),
3889 .writefn = vttbr_write },
3890 { .name = "VTTBR_EL2", .state = ARM_CP_STATE_AA64,
3891 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 0,
3892 .access = PL2_RW, .writefn = vttbr_write,
3893 .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2) },
b9cb5323
EI
3894 { .name = "SCTLR_EL2", .state = ARM_CP_STATE_BOTH,
3895 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 0,
3896 .access = PL2_RW, .raw_writefn = raw_write, .writefn = sctlr_write,
3897 .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[2]) },
ff05f37b
EI
3898 { .name = "TPIDR_EL2", .state = ARM_CP_STATE_BOTH,
3899 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 2,
3900 .access = PL2_RW, .resetvalue = 0,
3901 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[2]) },
a57633c0
EI
3902 { .name = "TTBR0_EL2", .state = ARM_CP_STATE_AA64,
3903 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
3904 .access = PL2_RW, .resetvalue = 0,
3905 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
3906 { .name = "HTTBR", .cp = 15, .opc1 = 4, .crm = 2,
3907 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
a57633c0 3908 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
541ef8c2
SS
3909 { .name = "TLBIALLNSNH",
3910 .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
3911 .type = ARM_CP_NO_RAW, .access = PL2_W,
3912 .writefn = tlbiall_nsnh_write },
3913 { .name = "TLBIALLNSNHIS",
3914 .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
3915 .type = ARM_CP_NO_RAW, .access = PL2_W,
3916 .writefn = tlbiall_nsnh_is_write },
3917 { .name = "TLBIALLH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
3918 .type = ARM_CP_NO_RAW, .access = PL2_W,
3919 .writefn = tlbiall_hyp_write },
3920 { .name = "TLBIALLHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
3921 .type = ARM_CP_NO_RAW, .access = PL2_W,
3922 .writefn = tlbiall_hyp_is_write },
3923 { .name = "TLBIMVAH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
3924 .type = ARM_CP_NO_RAW, .access = PL2_W,
3925 .writefn = tlbimva_hyp_write },
3926 { .name = "TLBIMVAHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
3927 .type = ARM_CP_NO_RAW, .access = PL2_W,
3928 .writefn = tlbimva_hyp_is_write },
51da9014
EI
3929 { .name = "TLBI_ALLE2", .state = ARM_CP_STATE_AA64,
3930 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
3931 .type = ARM_CP_NO_RAW, .access = PL2_W,
fd3ed969 3932 .writefn = tlbi_aa64_alle2_write },
8742d49d
EI
3933 { .name = "TLBI_VAE2", .state = ARM_CP_STATE_AA64,
3934 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
3935 .type = ARM_CP_NO_RAW, .access = PL2_W,
fd3ed969 3936 .writefn = tlbi_aa64_vae2_write },
2bfb9d75
PM
3937 { .name = "TLBI_VALE2", .state = ARM_CP_STATE_AA64,
3938 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
3939 .access = PL2_W, .type = ARM_CP_NO_RAW,
3940 .writefn = tlbi_aa64_vae2_write },
3941 { .name = "TLBI_ALLE2IS", .state = ARM_CP_STATE_AA64,
3942 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
3943 .access = PL2_W, .type = ARM_CP_NO_RAW,
3944 .writefn = tlbi_aa64_alle2is_write },
8742d49d
EI
3945 { .name = "TLBI_VAE2IS", .state = ARM_CP_STATE_AA64,
3946 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
3947 .type = ARM_CP_NO_RAW, .access = PL2_W,
fd3ed969 3948 .writefn = tlbi_aa64_vae2is_write },
2bfb9d75
PM
3949 { .name = "TLBI_VALE2IS", .state = ARM_CP_STATE_AA64,
3950 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
3951 .access = PL2_W, .type = ARM_CP_NO_RAW,
3952 .writefn = tlbi_aa64_vae2is_write },
edac4d8a 3953#ifndef CONFIG_USER_ONLY
2a47df95
PM
3954 /* Unlike the other EL2-related AT operations, these must
3955 * UNDEF from EL3 if EL2 is not implemented, which is why we
3956 * define them here rather than with the rest of the AT ops.
3957 */
3958 { .name = "AT_S1E2R", .state = ARM_CP_STATE_AA64,
3959 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
3960 .access = PL2_W, .accessfn = at_s1e2_access,
3961 .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3962 { .name = "AT_S1E2W", .state = ARM_CP_STATE_AA64,
3963 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
3964 .access = PL2_W, .accessfn = at_s1e2_access,
3965 .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
14db7fe0
PM
3966 /* The AArch32 ATS1H* operations are CONSTRAINED UNPREDICTABLE
3967 * if EL2 is not implemented; we choose to UNDEF. Behaviour at EL3
3968 * with SCR.NS == 0 outside Monitor mode is UNPREDICTABLE; we choose
3969 * to behave as if SCR.NS was 1.
3970 */
3971 { .name = "ATS1HR", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
3972 .access = PL2_W,
3973 .writefn = ats1h_write, .type = ARM_CP_NO_RAW },
3974 { .name = "ATS1HW", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
3975 .access = PL2_W,
3976 .writefn = ats1h_write, .type = ARM_CP_NO_RAW },
0b6440af
EI
3977 { .name = "CNTHCTL_EL2", .state = ARM_CP_STATE_BOTH,
3978 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 1, .opc2 = 0,
3979 /* ARMv7 requires bit 0 and 1 to reset to 1. ARMv8 defines the
3980 * reset values as IMPDEF. We choose to reset to 3 to comply with
3981 * both ARMv7 and ARMv8.
3982 */
3983 .access = PL2_RW, .resetvalue = 3,
3984 .fieldoffset = offsetof(CPUARMState, cp15.cnthctl_el2) },
edac4d8a
EI
3985 { .name = "CNTVOFF_EL2", .state = ARM_CP_STATE_AA64,
3986 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 3,
3987 .access = PL2_RW, .type = ARM_CP_IO, .resetvalue = 0,
3988 .writefn = gt_cntvoff_write,
3989 .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
3990 { .name = "CNTVOFF", .cp = 15, .opc1 = 4, .crm = 14,
3991 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS | ARM_CP_IO,
3992 .writefn = gt_cntvoff_write,
3993 .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
b0e66d95
EI
3994 { .name = "CNTHP_CVAL_EL2", .state = ARM_CP_STATE_AA64,
3995 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 2,
3996 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
3997 .type = ARM_CP_IO, .access = PL2_RW,
3998 .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
3999 { .name = "CNTHP_CVAL", .cp = 15, .opc1 = 6, .crm = 14,
4000 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
4001 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_IO,
4002 .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
4003 { .name = "CNTHP_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
4004 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 0,
d44ec156 4005 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
b0e66d95
EI
4006 .resetfn = gt_hyp_timer_reset,
4007 .readfn = gt_hyp_tval_read, .writefn = gt_hyp_tval_write },
4008 { .name = "CNTHP_CTL_EL2", .state = ARM_CP_STATE_BOTH,
4009 .type = ARM_CP_IO,
4010 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 1,
4011 .access = PL2_RW,
4012 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].ctl),
4013 .resetvalue = 0,
4014 .writefn = gt_hyp_ctl_write, .raw_writefn = raw_write },
edac4d8a 4015#endif
14cc7b54
SF
4016 /* The only field of MDCR_EL2 that has a defined architectural reset value
4017 * is MDCR_EL2.HPMN which should reset to the value of PMCR_EL0.N; but we
4018 * don't impelment any PMU event counters, so using zero as a reset
4019 * value for MDCR_EL2 is okay
4020 */
4021 { .name = "MDCR_EL2", .state = ARM_CP_STATE_BOTH,
4022 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 1,
4023 .access = PL2_RW, .resetvalue = 0,
4024 .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el2), },
59e05530
EI
4025 { .name = "HPFAR", .state = ARM_CP_STATE_AA32,
4026 .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
4027 .access = PL2_RW, .accessfn = access_el3_aa32ns,
4028 .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
4029 { .name = "HPFAR_EL2", .state = ARM_CP_STATE_AA64,
4030 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
4031 .access = PL2_RW,
4032 .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
2a5a9abd
AF
4033 { .name = "HSTR_EL2", .state = ARM_CP_STATE_BOTH,
4034 .cp = 15, .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 3,
4035 .access = PL2_RW,
4036 .fieldoffset = offsetof(CPUARMState, cp15.hstr_el2) },
3b685ba7
EI
4037 REGINFO_SENTINEL
4038};
4039
2f027fc5
PM
4040static CPAccessResult nsacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
4041 bool isread)
4042{
4043 /* The NSACR is RW at EL3, and RO for NS EL1 and NS EL2.
4044 * At Secure EL1 it traps to EL3.
4045 */
4046 if (arm_current_el(env) == 3) {
4047 return CP_ACCESS_OK;
4048 }
4049 if (arm_is_secure_below_el3(env)) {
4050 return CP_ACCESS_TRAP_EL3;
4051 }
4052 /* Accesses from EL1 NS and EL2 NS are UNDEF for write but allow reads. */
4053 if (isread) {
4054 return CP_ACCESS_OK;
4055 }
4056 return CP_ACCESS_TRAP_UNCATEGORIZED;
4057}
4058
60fb1a87
GB
4059static const ARMCPRegInfo el3_cp_reginfo[] = {
4060 { .name = "SCR_EL3", .state = ARM_CP_STATE_AA64,
4061 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 0,
4062 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.scr_el3),
4063 .resetvalue = 0, .writefn = scr_write },
7a0e58fa 4064 { .name = "SCR", .type = ARM_CP_ALIAS,
60fb1a87 4065 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 0,
efe4a274
PM
4066 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
4067 .fieldoffset = offsetoflow32(CPUARMState, cp15.scr_el3),
b061a82b 4068 .writefn = scr_write },
60fb1a87
GB
4069 { .name = "SDER32_EL3", .state = ARM_CP_STATE_AA64,
4070 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 1,
4071 .access = PL3_RW, .resetvalue = 0,
4072 .fieldoffset = offsetof(CPUARMState, cp15.sder) },
4073 { .name = "SDER",
4074 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 1,
4075 .access = PL3_RW, .resetvalue = 0,
4076 .fieldoffset = offsetoflow32(CPUARMState, cp15.sder) },
60fb1a87 4077 { .name = "MVBAR", .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
efe4a274
PM
4078 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
4079 .writefn = vbar_write, .resetvalue = 0,
60fb1a87 4080 .fieldoffset = offsetof(CPUARMState, cp15.mvbar) },
7dd8c9af
FA
4081 { .name = "TTBR0_EL3", .state = ARM_CP_STATE_AA64,
4082 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 0,
4083 .access = PL3_RW, .writefn = vmsa_ttbr_write, .resetvalue = 0,
4084 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[3]) },
11f136ee
FA
4085 { .name = "TCR_EL3", .state = ARM_CP_STATE_AA64,
4086 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 2,
6459b94c
PM
4087 .access = PL3_RW,
4088 /* no .writefn needed as this can't cause an ASID change;
811595a2
PM
4089 * we must provide a .raw_writefn and .resetfn because we handle
4090 * reset and migration for the AArch32 TTBCR(S), which might be
4091 * using mask and base_mask.
6459b94c 4092 */
811595a2 4093 .resetfn = vmsa_ttbcr_reset, .raw_writefn = vmsa_ttbcr_raw_write,
11f136ee 4094 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[3]) },
81547d66 4095 { .name = "ELR_EL3", .state = ARM_CP_STATE_AA64,
7a0e58fa 4096 .type = ARM_CP_ALIAS,
81547d66
EI
4097 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 1,
4098 .access = PL3_RW,
4099 .fieldoffset = offsetof(CPUARMState, elr_el[3]) },
f2c30f42 4100 { .name = "ESR_EL3", .state = ARM_CP_STATE_AA64,
f2c30f42
EI
4101 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 2, .opc2 = 0,
4102 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[3]) },
63b60551
EI
4103 { .name = "FAR_EL3", .state = ARM_CP_STATE_AA64,
4104 .opc0 = 3, .opc1 = 6, .crn = 6, .crm = 0, .opc2 = 0,
4105 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[3]) },
81547d66 4106 { .name = "SPSR_EL3", .state = ARM_CP_STATE_AA64,
7a0e58fa 4107 .type = ARM_CP_ALIAS,
81547d66 4108 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 0,
99a99c1f
SB
4109 .access = PL3_RW,
4110 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_MON]) },
a1ba125c
EI
4111 { .name = "VBAR_EL3", .state = ARM_CP_STATE_AA64,
4112 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 0,
4113 .access = PL3_RW, .writefn = vbar_write,
4114 .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[3]),
4115 .resetvalue = 0 },
c6f19164
GB
4116 { .name = "CPTR_EL3", .state = ARM_CP_STATE_AA64,
4117 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 2,
4118 .access = PL3_RW, .accessfn = cptr_access, .resetvalue = 0,
4119 .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[3]) },
4cfb8ad8
PM
4120 { .name = "TPIDR_EL3", .state = ARM_CP_STATE_AA64,
4121 .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 2,
4122 .access = PL3_RW, .resetvalue = 0,
4123 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[3]) },
2179ef95
PM
4124 { .name = "AMAIR_EL3", .state = ARM_CP_STATE_AA64,
4125 .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 3, .opc2 = 0,
4126 .access = PL3_RW, .type = ARM_CP_CONST,
4127 .resetvalue = 0 },
37cd6c24
PM
4128 { .name = "AFSR0_EL3", .state = ARM_CP_STATE_BOTH,
4129 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 0,
4130 .access = PL3_RW, .type = ARM_CP_CONST,
4131 .resetvalue = 0 },
4132 { .name = "AFSR1_EL3", .state = ARM_CP_STATE_BOTH,
4133 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 1,
4134 .access = PL3_RW, .type = ARM_CP_CONST,
4135 .resetvalue = 0 },
43efaa33
PM
4136 { .name = "TLBI_ALLE3IS", .state = ARM_CP_STATE_AA64,
4137 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 0,
4138 .access = PL3_W, .type = ARM_CP_NO_RAW,
4139 .writefn = tlbi_aa64_alle3is_write },
4140 { .name = "TLBI_VAE3IS", .state = ARM_CP_STATE_AA64,
4141 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 1,
4142 .access = PL3_W, .type = ARM_CP_NO_RAW,
4143 .writefn = tlbi_aa64_vae3is_write },
4144 { .name = "TLBI_VALE3IS", .state = ARM_CP_STATE_AA64,
4145 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 5,
4146 .access = PL3_W, .type = ARM_CP_NO_RAW,
4147 .writefn = tlbi_aa64_vae3is_write },
4148 { .name = "TLBI_ALLE3", .state = ARM_CP_STATE_AA64,
4149 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 0,
4150 .access = PL3_W, .type = ARM_CP_NO_RAW,
4151 .writefn = tlbi_aa64_alle3_write },
4152 { .name = "TLBI_VAE3", .state = ARM_CP_STATE_AA64,
4153 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 1,
4154 .access = PL3_W, .type = ARM_CP_NO_RAW,
4155 .writefn = tlbi_aa64_vae3_write },
4156 { .name = "TLBI_VALE3", .state = ARM_CP_STATE_AA64,
4157 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 5,
4158 .access = PL3_W, .type = ARM_CP_NO_RAW,
4159 .writefn = tlbi_aa64_vae3_write },
0f1a3b24
FA
4160 REGINFO_SENTINEL
4161};
4162
3f208fd7
PM
4163static CPAccessResult ctr_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
4164 bool isread)
7da845b0
PM
4165{
4166 /* Only accessible in EL0 if SCTLR.UCT is set (and only in AArch64,
4167 * but the AArch32 CTR has its own reginfo struct)
4168 */
137feaa9 4169 if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_UCT)) {
7da845b0
PM
4170 return CP_ACCESS_TRAP;
4171 }
4172 return CP_ACCESS_OK;
4173}
4174
1424ca8d
DM
4175static void oslar_write(CPUARMState *env, const ARMCPRegInfo *ri,
4176 uint64_t value)
4177{
4178 /* Writes to OSLAR_EL1 may update the OS lock status, which can be
4179 * read via a bit in OSLSR_EL1.
4180 */
4181 int oslock;
4182
4183 if (ri->state == ARM_CP_STATE_AA32) {
4184 oslock = (value == 0xC5ACCE55);
4185 } else {
4186 oslock = value & 1;
4187 }
4188
4189 env->cp15.oslsr_el1 = deposit32(env->cp15.oslsr_el1, 1, 1, oslock);
4190}
4191
50300698 4192static const ARMCPRegInfo debug_cp_reginfo[] = {
50300698 4193 /* DBGDRAR, DBGDSAR: always RAZ since we don't implement memory mapped
10aae104
PM
4194 * debug components. The AArch64 version of DBGDRAR is named MDRAR_EL1;
4195 * unlike DBGDRAR it is never accessible from EL0.
4196 * DBGDSAR is deprecated and must RAZ from v8 anyway, so it has no AArch64
4197 * accessor.
50300698
PM
4198 */
4199 { .name = "DBGDRAR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 0,
91b0a238
PM
4200 .access = PL0_R, .accessfn = access_tdra,
4201 .type = ARM_CP_CONST, .resetvalue = 0 },
10aae104
PM
4202 { .name = "MDRAR_EL1", .state = ARM_CP_STATE_AA64,
4203 .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
91b0a238
PM
4204 .access = PL1_R, .accessfn = access_tdra,
4205 .type = ARM_CP_CONST, .resetvalue = 0 },
50300698 4206 { .name = "DBGDSAR", .cp = 14, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
91b0a238
PM
4207 .access = PL0_R, .accessfn = access_tdra,
4208 .type = ARM_CP_CONST, .resetvalue = 0 },
17a9eb53 4209 /* Monitor debug system control register; the 32-bit alias is DBGDSCRext. */
10aae104
PM
4210 { .name = "MDSCR_EL1", .state = ARM_CP_STATE_BOTH,
4211 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
d6c8cf81 4212 .access = PL1_RW, .accessfn = access_tda,
0e5e8935
PM
4213 .fieldoffset = offsetof(CPUARMState, cp15.mdscr_el1),
4214 .resetvalue = 0 },
5e8b12ff
PM
4215 /* MDCCSR_EL0, aka DBGDSCRint. This is a read-only mirror of MDSCR_EL1.
4216 * We don't implement the configurable EL0 access.
4217 */
4218 { .name = "MDCCSR_EL0", .state = ARM_CP_STATE_BOTH,
4219 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
7a0e58fa 4220 .type = ARM_CP_ALIAS,
d6c8cf81 4221 .access = PL1_R, .accessfn = access_tda,
b061a82b 4222 .fieldoffset = offsetof(CPUARMState, cp15.mdscr_el1), },
10aae104
PM
4223 { .name = "OSLAR_EL1", .state = ARM_CP_STATE_BOTH,
4224 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 4,
1424ca8d 4225 .access = PL1_W, .type = ARM_CP_NO_RAW,
187f678d 4226 .accessfn = access_tdosa,
1424ca8d
DM
4227 .writefn = oslar_write },
4228 { .name = "OSLSR_EL1", .state = ARM_CP_STATE_BOTH,
4229 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 4,
4230 .access = PL1_R, .resetvalue = 10,
187f678d 4231 .accessfn = access_tdosa,
1424ca8d 4232 .fieldoffset = offsetof(CPUARMState, cp15.oslsr_el1) },
5e8b12ff
PM
4233 /* Dummy OSDLR_EL1: 32-bit Linux will read this */
4234 { .name = "OSDLR_EL1", .state = ARM_CP_STATE_BOTH,
4235 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 4,
187f678d
PM
4236 .access = PL1_RW, .accessfn = access_tdosa,
4237 .type = ARM_CP_NOP },
5e8b12ff
PM
4238 /* Dummy DBGVCR: Linux wants to clear this on startup, but we don't
4239 * implement vector catch debug events yet.
4240 */
4241 { .name = "DBGVCR",
4242 .cp = 14, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
d6c8cf81
PM
4243 .access = PL1_RW, .accessfn = access_tda,
4244 .type = ARM_CP_NOP },
4d2ec4da
PM
4245 /* Dummy DBGVCR32_EL2 (which is only for a 64-bit hypervisor
4246 * to save and restore a 32-bit guest's DBGVCR)
4247 */
4248 { .name = "DBGVCR32_EL2", .state = ARM_CP_STATE_AA64,
4249 .opc0 = 2, .opc1 = 4, .crn = 0, .crm = 7, .opc2 = 0,
4250 .access = PL2_RW, .accessfn = access_tda,
4251 .type = ARM_CP_NOP },
5dbdc434
PM
4252 /* Dummy MDCCINT_EL1, since we don't implement the Debug Communications
4253 * Channel but Linux may try to access this register. The 32-bit
4254 * alias is DBGDCCINT.
4255 */
4256 { .name = "MDCCINT_EL1", .state = ARM_CP_STATE_BOTH,
4257 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0,
4258 .access = PL1_RW, .accessfn = access_tda,
4259 .type = ARM_CP_NOP },
50300698
PM
4260 REGINFO_SENTINEL
4261};
4262
4263static const ARMCPRegInfo debug_lpae_cp_reginfo[] = {
4264 /* 64 bit access versions of the (dummy) debug registers */
4265 { .name = "DBGDRAR", .cp = 14, .crm = 1, .opc1 = 0,
4266 .access = PL0_R, .type = ARM_CP_CONST|ARM_CP_64BIT, .resetvalue = 0 },
4267 { .name = "DBGDSAR", .cp = 14, .crm = 2, .opc1 = 0,
4268 .access = PL0_R, .type = ARM_CP_CONST|ARM_CP_64BIT, .resetvalue = 0 },
4269 REGINFO_SENTINEL
4270};
4271
5be5e8ed
RH
4272/* Return the exception level to which SVE-disabled exceptions should
4273 * be taken, or 0 if SVE is enabled.
4274 */
4275static int sve_exception_el(CPUARMState *env)
4276{
4277#ifndef CONFIG_USER_ONLY
4278 unsigned current_el = arm_current_el(env);
4279
4280 /* The CPACR.ZEN controls traps to EL1:
4281 * 0, 2 : trap EL0 and EL1 accesses
4282 * 1 : trap only EL0 accesses
4283 * 3 : trap no accesses
4284 */
4285 switch (extract32(env->cp15.cpacr_el1, 16, 2)) {
4286 default:
4287 if (current_el <= 1) {
4288 /* Trap to PL1, which might be EL1 or EL3 */
4289 if (arm_is_secure(env) && !arm_el_is_aa64(env, 3)) {
4290 return 3;
4291 }
4292 return 1;
4293 }
4294 break;
4295 case 1:
4296 if (current_el == 0) {
4297 return 1;
4298 }
4299 break;
4300 case 3:
4301 break;
4302 }
4303
4304 /* Similarly for CPACR.FPEN, after having checked ZEN. */
4305 switch (extract32(env->cp15.cpacr_el1, 20, 2)) {
4306 default:
4307 if (current_el <= 1) {
4308 if (arm_is_secure(env) && !arm_el_is_aa64(env, 3)) {
4309 return 3;
4310 }
4311 return 1;
4312 }
4313 break;
4314 case 1:
4315 if (current_el == 0) {
4316 return 1;
4317 }
4318 break;
4319 case 3:
4320 break;
4321 }
4322
4323 /* CPTR_EL2. Check both TZ and TFP. */
4324 if (current_el <= 2
4325 && (env->cp15.cptr_el[2] & (CPTR_TFP | CPTR_TZ))
4326 && !arm_is_secure_below_el3(env)) {
4327 return 2;
4328 }
4329
4330 /* CPTR_EL3. Check both EZ and TFP. */
4331 if (!(env->cp15.cptr_el[3] & CPTR_EZ)
4332 || (env->cp15.cptr_el[3] & CPTR_TFP)) {
4333 return 3;
4334 }
4335#endif
4336 return 0;
4337}
4338
5be5e8ed
RH
4339static void zcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4340 uint64_t value)
4341{
4342 /* Bits other than [3:0] are RAZ/WI. */
4343 raw_write(env, ri, value & 0xf);
4344}
4345
4346static const ARMCPRegInfo zcr_el1_reginfo = {
4347 .name = "ZCR_EL1", .state = ARM_CP_STATE_AA64,
4348 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 0,
490aa7f1 4349 .access = PL1_RW, .type = ARM_CP_SVE | ARM_CP_FPU,
5be5e8ed
RH
4350 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[1]),
4351 .writefn = zcr_write, .raw_writefn = raw_write
4352};
4353
4354static const ARMCPRegInfo zcr_el2_reginfo = {
4355 .name = "ZCR_EL2", .state = ARM_CP_STATE_AA64,
4356 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 0,
490aa7f1 4357 .access = PL2_RW, .type = ARM_CP_SVE | ARM_CP_FPU,
5be5e8ed
RH
4358 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[2]),
4359 .writefn = zcr_write, .raw_writefn = raw_write
4360};
4361
4362static const ARMCPRegInfo zcr_no_el2_reginfo = {
4363 .name = "ZCR_EL2", .state = ARM_CP_STATE_AA64,
4364 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 0,
490aa7f1 4365 .access = PL2_RW, .type = ARM_CP_SVE | ARM_CP_FPU,
5be5e8ed
RH
4366 .readfn = arm_cp_read_zero, .writefn = arm_cp_write_ignore
4367};
4368
4369static const ARMCPRegInfo zcr_el3_reginfo = {
4370 .name = "ZCR_EL3", .state = ARM_CP_STATE_AA64,
4371 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 2, .opc2 = 0,
490aa7f1 4372 .access = PL3_RW, .type = ARM_CP_SVE | ARM_CP_FPU,
5be5e8ed
RH
4373 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[3]),
4374 .writefn = zcr_write, .raw_writefn = raw_write
4375};
4376
9ee98ce8
PM
4377void hw_watchpoint_update(ARMCPU *cpu, int n)
4378{
4379 CPUARMState *env = &cpu->env;
4380 vaddr len = 0;
4381 vaddr wvr = env->cp15.dbgwvr[n];
4382 uint64_t wcr = env->cp15.dbgwcr[n];
4383 int mask;
4384 int flags = BP_CPU | BP_STOP_BEFORE_ACCESS;
4385
4386 if (env->cpu_watchpoint[n]) {
4387 cpu_watchpoint_remove_by_ref(CPU(cpu), env->cpu_watchpoint[n]);
4388 env->cpu_watchpoint[n] = NULL;
4389 }
4390
4391 if (!extract64(wcr, 0, 1)) {
4392 /* E bit clear : watchpoint disabled */
4393 return;
4394 }
4395
4396 switch (extract64(wcr, 3, 2)) {
4397 case 0:
4398 /* LSC 00 is reserved and must behave as if the wp is disabled */
4399 return;
4400 case 1:
4401 flags |= BP_MEM_READ;
4402 break;
4403 case 2:
4404 flags |= BP_MEM_WRITE;
4405 break;
4406 case 3:
4407 flags |= BP_MEM_ACCESS;
4408 break;
4409 }
4410
4411 /* Attempts to use both MASK and BAS fields simultaneously are
4412 * CONSTRAINED UNPREDICTABLE; we opt to ignore BAS in this case,
4413 * thus generating a watchpoint for every byte in the masked region.
4414 */
4415 mask = extract64(wcr, 24, 4);
4416 if (mask == 1 || mask == 2) {
4417 /* Reserved values of MASK; we must act as if the mask value was
4418 * some non-reserved value, or as if the watchpoint were disabled.
4419 * We choose the latter.
4420 */
4421 return;
4422 } else if (mask) {
4423 /* Watchpoint covers an aligned area up to 2GB in size */
4424 len = 1ULL << mask;
4425 /* If masked bits in WVR are not zero it's CONSTRAINED UNPREDICTABLE
4426 * whether the watchpoint fires when the unmasked bits match; we opt
4427 * to generate the exceptions.
4428 */
4429 wvr &= ~(len - 1);
4430 } else {
4431 /* Watchpoint covers bytes defined by the byte address select bits */
4432 int bas = extract64(wcr, 5, 8);
4433 int basstart;
4434
4435 if (bas == 0) {
4436 /* This must act as if the watchpoint is disabled */
4437 return;
4438 }
4439
4440 if (extract64(wvr, 2, 1)) {
4441 /* Deprecated case of an only 4-aligned address. BAS[7:4] are
4442 * ignored, and BAS[3:0] define which bytes to watch.
4443 */
4444 bas &= 0xf;
4445 }
4446 /* The BAS bits are supposed to be programmed to indicate a contiguous
4447 * range of bytes. Otherwise it is CONSTRAINED UNPREDICTABLE whether
4448 * we fire for each byte in the word/doubleword addressed by the WVR.
4449 * We choose to ignore any non-zero bits after the first range of 1s.
4450 */
4451 basstart = ctz32(bas);
4452 len = cto32(bas >> basstart);
4453 wvr += basstart;
4454 }
4455
4456 cpu_watchpoint_insert(CPU(cpu), wvr, len, flags,
4457 &env->cpu_watchpoint[n]);
4458}
4459
4460void hw_watchpoint_update_all(ARMCPU *cpu)
4461{
4462 int i;
4463 CPUARMState *env = &cpu->env;
4464
4465 /* Completely clear out existing QEMU watchpoints and our array, to
4466 * avoid possible stale entries following migration load.
4467 */
4468 cpu_watchpoint_remove_all(CPU(cpu), BP_CPU);
4469 memset(env->cpu_watchpoint, 0, sizeof(env->cpu_watchpoint));
4470
4471 for (i = 0; i < ARRAY_SIZE(cpu->env.cpu_watchpoint); i++) {
4472 hw_watchpoint_update(cpu, i);
4473 }
4474}
4475
4476static void dbgwvr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4477 uint64_t value)
4478{
4479 ARMCPU *cpu = arm_env_get_cpu(env);
4480 int i = ri->crm;
4481
4482 /* Bits [63:49] are hardwired to the value of bit [48]; that is, the
4483 * register reads and behaves as if values written are sign extended.
4484 * Bits [1:0] are RES0.
4485 */
4486 value = sextract64(value, 0, 49) & ~3ULL;
4487
4488 raw_write(env, ri, value);
4489 hw_watchpoint_update(cpu, i);
4490}
4491
4492static void dbgwcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4493 uint64_t value)
4494{
4495 ARMCPU *cpu = arm_env_get_cpu(env);
4496 int i = ri->crm;
4497
4498 raw_write(env, ri, value);
4499 hw_watchpoint_update(cpu, i);
4500}
4501
46747d15
PM
4502void hw_breakpoint_update(ARMCPU *cpu, int n)
4503{
4504 CPUARMState *env = &cpu->env;
4505 uint64_t bvr = env->cp15.dbgbvr[n];
4506 uint64_t bcr = env->cp15.dbgbcr[n];
4507 vaddr addr;
4508 int bt;
4509 int flags = BP_CPU;
4510
4511 if (env->cpu_breakpoint[n]) {
4512 cpu_breakpoint_remove_by_ref(CPU(cpu), env->cpu_breakpoint[n]);
4513 env->cpu_breakpoint[n] = NULL;
4514 }
4515
4516 if (!extract64(bcr, 0, 1)) {
4517 /* E bit clear : watchpoint disabled */
4518 return;
4519 }
4520
4521 bt = extract64(bcr, 20, 4);
4522
4523 switch (bt) {
4524 case 4: /* unlinked address mismatch (reserved if AArch64) */
4525 case 5: /* linked address mismatch (reserved if AArch64) */
4526 qemu_log_mask(LOG_UNIMP,
4527 "arm: address mismatch breakpoint types not implemented");
4528 return;
4529 case 0: /* unlinked address match */
4530 case 1: /* linked address match */
4531 {
4532 /* Bits [63:49] are hardwired to the value of bit [48]; that is,
4533 * we behave as if the register was sign extended. Bits [1:0] are
4534 * RES0. The BAS field is used to allow setting breakpoints on 16
4535 * bit wide instructions; it is CONSTRAINED UNPREDICTABLE whether
4536 * a bp will fire if the addresses covered by the bp and the addresses
4537 * covered by the insn overlap but the insn doesn't start at the
4538 * start of the bp address range. We choose to require the insn and
4539 * the bp to have the same address. The constraints on writing to
4540 * BAS enforced in dbgbcr_write mean we have only four cases:
4541 * 0b0000 => no breakpoint
4542 * 0b0011 => breakpoint on addr
4543 * 0b1100 => breakpoint on addr + 2
4544 * 0b1111 => breakpoint on addr
4545 * See also figure D2-3 in the v8 ARM ARM (DDI0487A.c).
4546 */
4547 int bas = extract64(bcr, 5, 4);
4548 addr = sextract64(bvr, 0, 49) & ~3ULL;
4549 if (bas == 0) {
4550 return;
4551 }
4552 if (bas == 0xc) {
4553 addr += 2;
4554 }
4555 break;
4556 }
4557 case 2: /* unlinked context ID match */
4558 case 8: /* unlinked VMID match (reserved if no EL2) */
4559 case 10: /* unlinked context ID and VMID match (reserved if no EL2) */
4560 qemu_log_mask(LOG_UNIMP,
4561 "arm: unlinked context breakpoint types not implemented");
4562 return;
4563 case 9: /* linked VMID match (reserved if no EL2) */
4564 case 11: /* linked context ID and VMID match (reserved if no EL2) */
4565 case 3: /* linked context ID match */
4566 default:
4567 /* We must generate no events for Linked context matches (unless
4568 * they are linked to by some other bp/wp, which is handled in
4569 * updates for the linking bp/wp). We choose to also generate no events
4570 * for reserved values.
4571 */
4572 return;
4573 }
4574
4575 cpu_breakpoint_insert(CPU(cpu), addr, flags, &env->cpu_breakpoint[n]);
4576}
4577
4578void hw_breakpoint_update_all(ARMCPU *cpu)
4579{
4580 int i;
4581 CPUARMState *env = &cpu->env;
4582
4583 /* Completely clear out existing QEMU breakpoints and our array, to
4584 * avoid possible stale entries following migration load.
4585 */
4586 cpu_breakpoint_remove_all(CPU(cpu), BP_CPU);
4587 memset(env->cpu_breakpoint, 0, sizeof(env->cpu_breakpoint));
4588
4589 for (i = 0; i < ARRAY_SIZE(cpu->env.cpu_breakpoint); i++) {
4590 hw_breakpoint_update(cpu, i);
4591 }
4592}
4593
4594static void dbgbvr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4595 uint64_t value)
4596{
4597 ARMCPU *cpu = arm_env_get_cpu(env);
4598 int i = ri->crm;
4599
4600 raw_write(env, ri, value);
4601 hw_breakpoint_update(cpu, i);
4602}
4603
4604static void dbgbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4605 uint64_t value)
4606{
4607 ARMCPU *cpu = arm_env_get_cpu(env);
4608 int i = ri->crm;
4609
4610 /* BAS[3] is a read-only copy of BAS[2], and BAS[1] a read-only
4611 * copy of BAS[0].
4612 */
4613 value = deposit64(value, 6, 1, extract64(value, 5, 1));
4614 value = deposit64(value, 8, 1, extract64(value, 7, 1));
4615
4616 raw_write(env, ri, value);
4617 hw_breakpoint_update(cpu, i);
4618}
4619
50300698 4620static void define_debug_regs(ARMCPU *cpu)
0b45451e 4621{
50300698
PM
4622 /* Define v7 and v8 architectural debug registers.
4623 * These are just dummy implementations for now.
0b45451e
PM
4624 */
4625 int i;
3ff6fc91 4626 int wrps, brps, ctx_cmps;
48eb3ae6
PM
4627 ARMCPRegInfo dbgdidr = {
4628 .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0,
d6c8cf81
PM
4629 .access = PL0_R, .accessfn = access_tda,
4630 .type = ARM_CP_CONST, .resetvalue = cpu->dbgdidr,
48eb3ae6
PM
4631 };
4632
3ff6fc91 4633 /* Note that all these register fields hold "number of Xs minus 1". */
48eb3ae6
PM
4634 brps = extract32(cpu->dbgdidr, 24, 4);
4635 wrps = extract32(cpu->dbgdidr, 28, 4);
3ff6fc91
PM
4636 ctx_cmps = extract32(cpu->dbgdidr, 20, 4);
4637
4638 assert(ctx_cmps <= brps);
48eb3ae6
PM
4639
4640 /* The DBGDIDR and ID_AA64DFR0_EL1 define various properties
4641 * of the debug registers such as number of breakpoints;
4642 * check that if they both exist then they agree.
4643 */
4644 if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64)) {
4645 assert(extract32(cpu->id_aa64dfr0, 12, 4) == brps);
4646 assert(extract32(cpu->id_aa64dfr0, 20, 4) == wrps);
3ff6fc91 4647 assert(extract32(cpu->id_aa64dfr0, 28, 4) == ctx_cmps);
48eb3ae6 4648 }
0b45451e 4649
48eb3ae6 4650 define_one_arm_cp_reg(cpu, &dbgdidr);
50300698
PM
4651 define_arm_cp_regs(cpu, debug_cp_reginfo);
4652
4653 if (arm_feature(&cpu->env, ARM_FEATURE_LPAE)) {
4654 define_arm_cp_regs(cpu, debug_lpae_cp_reginfo);
4655 }
4656
48eb3ae6 4657 for (i = 0; i < brps + 1; i++) {
0b45451e 4658 ARMCPRegInfo dbgregs[] = {
10aae104
PM
4659 { .name = "DBGBVR", .state = ARM_CP_STATE_BOTH,
4660 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 4,
d6c8cf81 4661 .access = PL1_RW, .accessfn = access_tda,
46747d15
PM
4662 .fieldoffset = offsetof(CPUARMState, cp15.dbgbvr[i]),
4663 .writefn = dbgbvr_write, .raw_writefn = raw_write
4664 },
10aae104
PM
4665 { .name = "DBGBCR", .state = ARM_CP_STATE_BOTH,
4666 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 5,
d6c8cf81 4667 .access = PL1_RW, .accessfn = access_tda,
46747d15
PM
4668 .fieldoffset = offsetof(CPUARMState, cp15.dbgbcr[i]),
4669 .writefn = dbgbcr_write, .raw_writefn = raw_write
4670 },
48eb3ae6
PM
4671 REGINFO_SENTINEL
4672 };
4673 define_arm_cp_regs(cpu, dbgregs);
4674 }
4675
4676 for (i = 0; i < wrps + 1; i++) {
4677 ARMCPRegInfo dbgregs[] = {
10aae104
PM
4678 { .name = "DBGWVR", .state = ARM_CP_STATE_BOTH,
4679 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 6,
d6c8cf81 4680 .access = PL1_RW, .accessfn = access_tda,
9ee98ce8
PM
4681 .fieldoffset = offsetof(CPUARMState, cp15.dbgwvr[i]),
4682 .writefn = dbgwvr_write, .raw_writefn = raw_write
4683 },
10aae104
PM
4684 { .name = "DBGWCR", .state = ARM_CP_STATE_BOTH,
4685 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 7,
d6c8cf81 4686 .access = PL1_RW, .accessfn = access_tda,
9ee98ce8
PM
4687 .fieldoffset = offsetof(CPUARMState, cp15.dbgwcr[i]),
4688 .writefn = dbgwcr_write, .raw_writefn = raw_write
4689 },
4690 REGINFO_SENTINEL
0b45451e
PM
4691 };
4692 define_arm_cp_regs(cpu, dbgregs);
4693 }
4694}
4695
96a8b92e
PM
4696/* We don't know until after realize whether there's a GICv3
4697 * attached, and that is what registers the gicv3 sysregs.
4698 * So we have to fill in the GIC fields in ID_PFR/ID_PFR1_EL1/ID_AA64PFR0_EL1
4699 * at runtime.
4700 */
4701static uint64_t id_pfr1_read(CPUARMState *env, const ARMCPRegInfo *ri)
4702{
4703 ARMCPU *cpu = arm_env_get_cpu(env);
4704 uint64_t pfr1 = cpu->id_pfr1;
4705
4706 if (env->gicv3state) {
4707 pfr1 |= 1 << 28;
4708 }
4709 return pfr1;
4710}
4711
4712static uint64_t id_aa64pfr0_read(CPUARMState *env, const ARMCPRegInfo *ri)
4713{
4714 ARMCPU *cpu = arm_env_get_cpu(env);
4715 uint64_t pfr0 = cpu->id_aa64pfr0;
4716
4717 if (env->gicv3state) {
4718 pfr0 |= 1 << 24;
4719 }
4720 return pfr0;
4721}
4722
2ceb98c0
PM
4723void register_cp_regs_for_features(ARMCPU *cpu)
4724{
4725 /* Register all the coprocessor registers based on feature bits */
4726 CPUARMState *env = &cpu->env;
4727 if (arm_feature(env, ARM_FEATURE_M)) {
4728 /* M profile has no coprocessor registers */
4729 return;
4730 }
4731
e9aa6c21 4732 define_arm_cp_regs(cpu, cp_reginfo);
9449fdf6
PM
4733 if (!arm_feature(env, ARM_FEATURE_V8)) {
4734 /* Must go early as it is full of wildcards that may be
4735 * overridden by later definitions.
4736 */
4737 define_arm_cp_regs(cpu, not_v8_cp_reginfo);
4738 }
4739
7d57f408 4740 if (arm_feature(env, ARM_FEATURE_V6)) {
8515a092
PM
4741 /* The ID registers all have impdef reset values */
4742 ARMCPRegInfo v6_idregs[] = {
0ff644a7
PM
4743 { .name = "ID_PFR0", .state = ARM_CP_STATE_BOTH,
4744 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
4745 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 4746 .resetvalue = cpu->id_pfr0 },
96a8b92e
PM
4747 /* ID_PFR1 is not a plain ARM_CP_CONST because we don't know
4748 * the value of the GIC field until after we define these regs.
4749 */
0ff644a7
PM
4750 { .name = "ID_PFR1", .state = ARM_CP_STATE_BOTH,
4751 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 1,
96a8b92e
PM
4752 .access = PL1_R, .type = ARM_CP_NO_RAW,
4753 .readfn = id_pfr1_read,
4754 .writefn = arm_cp_write_ignore },
0ff644a7
PM
4755 { .name = "ID_DFR0", .state = ARM_CP_STATE_BOTH,
4756 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 2,
4757 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 4758 .resetvalue = cpu->id_dfr0 },
0ff644a7
PM
4759 { .name = "ID_AFR0", .state = ARM_CP_STATE_BOTH,
4760 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 3,
4761 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 4762 .resetvalue = cpu->id_afr0 },
0ff644a7
PM
4763 { .name = "ID_MMFR0", .state = ARM_CP_STATE_BOTH,
4764 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 4,
4765 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 4766 .resetvalue = cpu->id_mmfr0 },
0ff644a7
PM
4767 { .name = "ID_MMFR1", .state = ARM_CP_STATE_BOTH,
4768 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 5,
4769 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 4770 .resetvalue = cpu->id_mmfr1 },
0ff644a7
PM
4771 { .name = "ID_MMFR2", .state = ARM_CP_STATE_BOTH,
4772 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 6,
4773 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 4774 .resetvalue = cpu->id_mmfr2 },
0ff644a7
PM
4775 { .name = "ID_MMFR3", .state = ARM_CP_STATE_BOTH,
4776 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 7,
4777 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 4778 .resetvalue = cpu->id_mmfr3 },
0ff644a7
PM
4779 { .name = "ID_ISAR0", .state = ARM_CP_STATE_BOTH,
4780 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0,
4781 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 4782 .resetvalue = cpu->id_isar0 },
0ff644a7
PM
4783 { .name = "ID_ISAR1", .state = ARM_CP_STATE_BOTH,
4784 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 1,
4785 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 4786 .resetvalue = cpu->id_isar1 },
0ff644a7
PM
4787 { .name = "ID_ISAR2", .state = ARM_CP_STATE_BOTH,
4788 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
4789 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 4790 .resetvalue = cpu->id_isar2 },
0ff644a7
PM
4791 { .name = "ID_ISAR3", .state = ARM_CP_STATE_BOTH,
4792 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 3,
4793 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 4794 .resetvalue = cpu->id_isar3 },
0ff644a7
PM
4795 { .name = "ID_ISAR4", .state = ARM_CP_STATE_BOTH,
4796 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 4,
4797 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 4798 .resetvalue = cpu->id_isar4 },
0ff644a7
PM
4799 { .name = "ID_ISAR5", .state = ARM_CP_STATE_BOTH,
4800 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 5,
4801 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 4802 .resetvalue = cpu->id_isar5 },
e20d84c1
PM
4803 { .name = "ID_MMFR4", .state = ARM_CP_STATE_BOTH,
4804 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 6,
4805 .access = PL1_R, .type = ARM_CP_CONST,
4806 .resetvalue = cpu->id_mmfr4 },
4807 /* 7 is as yet unallocated and must RAZ */
4808 { .name = "ID_ISAR7_RESERVED", .state = ARM_CP_STATE_BOTH,
4809 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 7,
4810 .access = PL1_R, .type = ARM_CP_CONST,
8515a092
PM
4811 .resetvalue = 0 },
4812 REGINFO_SENTINEL
4813 };
4814 define_arm_cp_regs(cpu, v6_idregs);
7d57f408
PM
4815 define_arm_cp_regs(cpu, v6_cp_reginfo);
4816 } else {
4817 define_arm_cp_regs(cpu, not_v6_cp_reginfo);
4818 }
4d31c596
PM
4819 if (arm_feature(env, ARM_FEATURE_V6K)) {
4820 define_arm_cp_regs(cpu, v6k_cp_reginfo);
4821 }
5e5cf9e3 4822 if (arm_feature(env, ARM_FEATURE_V7MP) &&
452a0955 4823 !arm_feature(env, ARM_FEATURE_PMSA)) {
995939a6
PM
4824 define_arm_cp_regs(cpu, v7mp_cp_reginfo);
4825 }
e9aa6c21 4826 if (arm_feature(env, ARM_FEATURE_V7)) {
200ac0ef 4827 /* v7 performance monitor control register: same implementor
7c2cb42b
AF
4828 * field as main ID register, and we implement only the cycle
4829 * count register.
200ac0ef 4830 */
7c2cb42b 4831#ifndef CONFIG_USER_ONLY
200ac0ef
PM
4832 ARMCPRegInfo pmcr = {
4833 .name = "PMCR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 0,
8521466b 4834 .access = PL0_RW,
7a0e58fa 4835 .type = ARM_CP_IO | ARM_CP_ALIAS,
8521466b 4836 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcr),
fcd25206
PM
4837 .accessfn = pmreg_access, .writefn = pmcr_write,
4838 .raw_writefn = raw_write,
200ac0ef 4839 };
8521466b
AF
4840 ARMCPRegInfo pmcr64 = {
4841 .name = "PMCR_EL0", .state = ARM_CP_STATE_AA64,
4842 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 0,
4843 .access = PL0_RW, .accessfn = pmreg_access,
4844 .type = ARM_CP_IO,
4845 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcr),
4846 .resetvalue = cpu->midr & 0xff000000,
4847 .writefn = pmcr_write, .raw_writefn = raw_write,
4848 };
7c2cb42b 4849 define_one_arm_cp_reg(cpu, &pmcr);
8521466b 4850 define_one_arm_cp_reg(cpu, &pmcr64);
7c2cb42b 4851#endif
776d4e5c 4852 ARMCPRegInfo clidr = {
7da845b0
PM
4853 .name = "CLIDR", .state = ARM_CP_STATE_BOTH,
4854 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 1,
776d4e5c
PM
4855 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->clidr
4856 };
776d4e5c 4857 define_one_arm_cp_reg(cpu, &clidr);
e9aa6c21 4858 define_arm_cp_regs(cpu, v7_cp_reginfo);
50300698 4859 define_debug_regs(cpu);
7d57f408
PM
4860 } else {
4861 define_arm_cp_regs(cpu, not_v7_cp_reginfo);
e9aa6c21 4862 }
b0d2b7d0 4863 if (arm_feature(env, ARM_FEATURE_V8)) {
e20d84c1
PM
4864 /* AArch64 ID registers, which all have impdef reset values.
4865 * Note that within the ID register ranges the unused slots
4866 * must all RAZ, not UNDEF; future architecture versions may
4867 * define new registers here.
4868 */
e60cef86 4869 ARMCPRegInfo v8_idregs[] = {
96a8b92e
PM
4870 /* ID_AA64PFR0_EL1 is not a plain ARM_CP_CONST because we don't
4871 * know the right value for the GIC field until after we
4872 * define these regs.
4873 */
e60cef86
PM
4874 { .name = "ID_AA64PFR0_EL1", .state = ARM_CP_STATE_AA64,
4875 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 0,
96a8b92e
PM
4876 .access = PL1_R, .type = ARM_CP_NO_RAW,
4877 .readfn = id_aa64pfr0_read,
4878 .writefn = arm_cp_write_ignore },
e60cef86
PM
4879 { .name = "ID_AA64PFR1_EL1", .state = ARM_CP_STATE_AA64,
4880 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 1,
4881 .access = PL1_R, .type = ARM_CP_CONST,
4882 .resetvalue = cpu->id_aa64pfr1},
e20d84c1
PM
4883 { .name = "ID_AA64PFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4884 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 2,
4885 .access = PL1_R, .type = ARM_CP_CONST,
4886 .resetvalue = 0 },
4887 { .name = "ID_AA64PFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4888 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 3,
4889 .access = PL1_R, .type = ARM_CP_CONST,
4890 .resetvalue = 0 },
4891 { .name = "ID_AA64PFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4892 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 4,
4893 .access = PL1_R, .type = ARM_CP_CONST,
4894 .resetvalue = 0 },
4895 { .name = "ID_AA64PFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4896 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 5,
4897 .access = PL1_R, .type = ARM_CP_CONST,
4898 .resetvalue = 0 },
4899 { .name = "ID_AA64PFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4900 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 6,
4901 .access = PL1_R, .type = ARM_CP_CONST,
4902 .resetvalue = 0 },
4903 { .name = "ID_AA64PFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4904 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 7,
4905 .access = PL1_R, .type = ARM_CP_CONST,
4906 .resetvalue = 0 },
e60cef86
PM
4907 { .name = "ID_AA64DFR0_EL1", .state = ARM_CP_STATE_AA64,
4908 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 0,
4909 .access = PL1_R, .type = ARM_CP_CONST,
d6f02ce3 4910 .resetvalue = cpu->id_aa64dfr0 },
e60cef86
PM
4911 { .name = "ID_AA64DFR1_EL1", .state = ARM_CP_STATE_AA64,
4912 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 1,
4913 .access = PL1_R, .type = ARM_CP_CONST,
4914 .resetvalue = cpu->id_aa64dfr1 },
e20d84c1
PM
4915 { .name = "ID_AA64DFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4916 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 2,
4917 .access = PL1_R, .type = ARM_CP_CONST,
4918 .resetvalue = 0 },
4919 { .name = "ID_AA64DFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4920 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 3,
4921 .access = PL1_R, .type = ARM_CP_CONST,
4922 .resetvalue = 0 },
e60cef86
PM
4923 { .name = "ID_AA64AFR0_EL1", .state = ARM_CP_STATE_AA64,
4924 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 4,
4925 .access = PL1_R, .type = ARM_CP_CONST,
4926 .resetvalue = cpu->id_aa64afr0 },
4927 { .name = "ID_AA64AFR1_EL1", .state = ARM_CP_STATE_AA64,
4928 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 5,
4929 .access = PL1_R, .type = ARM_CP_CONST,
4930 .resetvalue = cpu->id_aa64afr1 },
e20d84c1
PM
4931 { .name = "ID_AA64AFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4932 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 6,
4933 .access = PL1_R, .type = ARM_CP_CONST,
4934 .resetvalue = 0 },
4935 { .name = "ID_AA64AFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4936 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 7,
4937 .access = PL1_R, .type = ARM_CP_CONST,
4938 .resetvalue = 0 },
e60cef86
PM
4939 { .name = "ID_AA64ISAR0_EL1", .state = ARM_CP_STATE_AA64,
4940 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 0,
4941 .access = PL1_R, .type = ARM_CP_CONST,
4942 .resetvalue = cpu->id_aa64isar0 },
4943 { .name = "ID_AA64ISAR1_EL1", .state = ARM_CP_STATE_AA64,
4944 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 1,
4945 .access = PL1_R, .type = ARM_CP_CONST,
4946 .resetvalue = cpu->id_aa64isar1 },
e20d84c1
PM
4947 { .name = "ID_AA64ISAR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4948 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 2,
4949 .access = PL1_R, .type = ARM_CP_CONST,
4950 .resetvalue = 0 },
4951 { .name = "ID_AA64ISAR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4952 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 3,
4953 .access = PL1_R, .type = ARM_CP_CONST,
4954 .resetvalue = 0 },
4955 { .name = "ID_AA64ISAR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4956 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 4,
4957 .access = PL1_R, .type = ARM_CP_CONST,
4958 .resetvalue = 0 },
4959 { .name = "ID_AA64ISAR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4960 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 5,
4961 .access = PL1_R, .type = ARM_CP_CONST,
4962 .resetvalue = 0 },
4963 { .name = "ID_AA64ISAR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4964 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 6,
4965 .access = PL1_R, .type = ARM_CP_CONST,
4966 .resetvalue = 0 },
4967 { .name = "ID_AA64ISAR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4968 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 7,
4969 .access = PL1_R, .type = ARM_CP_CONST,
4970 .resetvalue = 0 },
e60cef86
PM
4971 { .name = "ID_AA64MMFR0_EL1", .state = ARM_CP_STATE_AA64,
4972 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
4973 .access = PL1_R, .type = ARM_CP_CONST,
4974 .resetvalue = cpu->id_aa64mmfr0 },
4975 { .name = "ID_AA64MMFR1_EL1", .state = ARM_CP_STATE_AA64,
4976 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 1,
4977 .access = PL1_R, .type = ARM_CP_CONST,
4978 .resetvalue = cpu->id_aa64mmfr1 },
e20d84c1
PM
4979 { .name = "ID_AA64MMFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4980 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 2,
4981 .access = PL1_R, .type = ARM_CP_CONST,
4982 .resetvalue = 0 },
4983 { .name = "ID_AA64MMFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4984 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 3,
4985 .access = PL1_R, .type = ARM_CP_CONST,
4986 .resetvalue = 0 },
4987 { .name = "ID_AA64MMFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4988 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 4,
4989 .access = PL1_R, .type = ARM_CP_CONST,
4990 .resetvalue = 0 },
4991 { .name = "ID_AA64MMFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4992 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 5,
4993 .access = PL1_R, .type = ARM_CP_CONST,
4994 .resetvalue = 0 },
4995 { .name = "ID_AA64MMFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4996 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 6,
4997 .access = PL1_R, .type = ARM_CP_CONST,
4998 .resetvalue = 0 },
4999 { .name = "ID_AA64MMFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
5000 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 7,
5001 .access = PL1_R, .type = ARM_CP_CONST,
5002 .resetvalue = 0 },
a50c0f51
PM
5003 { .name = "MVFR0_EL1", .state = ARM_CP_STATE_AA64,
5004 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0,
5005 .access = PL1_R, .type = ARM_CP_CONST,
5006 .resetvalue = cpu->mvfr0 },
5007 { .name = "MVFR1_EL1", .state = ARM_CP_STATE_AA64,
5008 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1,
5009 .access = PL1_R, .type = ARM_CP_CONST,
5010 .resetvalue = cpu->mvfr1 },
5011 { .name = "MVFR2_EL1", .state = ARM_CP_STATE_AA64,
5012 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2,
5013 .access = PL1_R, .type = ARM_CP_CONST,
5014 .resetvalue = cpu->mvfr2 },
e20d84c1
PM
5015 { .name = "MVFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
5016 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 3,
5017 .access = PL1_R, .type = ARM_CP_CONST,
5018 .resetvalue = 0 },
5019 { .name = "MVFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
5020 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 4,
5021 .access = PL1_R, .type = ARM_CP_CONST,
5022 .resetvalue = 0 },
5023 { .name = "MVFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
5024 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 5,
5025 .access = PL1_R, .type = ARM_CP_CONST,
5026 .resetvalue = 0 },
5027 { .name = "MVFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
5028 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 6,
5029 .access = PL1_R, .type = ARM_CP_CONST,
5030 .resetvalue = 0 },
5031 { .name = "MVFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
5032 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 7,
5033 .access = PL1_R, .type = ARM_CP_CONST,
5034 .resetvalue = 0 },
4054bfa9
AF
5035 { .name = "PMCEID0", .state = ARM_CP_STATE_AA32,
5036 .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 6,
5037 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
5038 .resetvalue = cpu->pmceid0 },
5039 { .name = "PMCEID0_EL0", .state = ARM_CP_STATE_AA64,
5040 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 6,
5041 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
5042 .resetvalue = cpu->pmceid0 },
5043 { .name = "PMCEID1", .state = ARM_CP_STATE_AA32,
5044 .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 7,
5045 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
5046 .resetvalue = cpu->pmceid1 },
5047 { .name = "PMCEID1_EL0", .state = ARM_CP_STATE_AA64,
5048 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 7,
5049 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
5050 .resetvalue = cpu->pmceid1 },
e60cef86
PM
5051 REGINFO_SENTINEL
5052 };
be8e8128
GB
5053 /* RVBAR_EL1 is only implemented if EL1 is the highest EL */
5054 if (!arm_feature(env, ARM_FEATURE_EL3) &&
5055 !arm_feature(env, ARM_FEATURE_EL2)) {
5056 ARMCPRegInfo rvbar = {
5057 .name = "RVBAR_EL1", .state = ARM_CP_STATE_AA64,
5058 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
5059 .type = ARM_CP_CONST, .access = PL1_R, .resetvalue = cpu->rvbar
5060 };
5061 define_one_arm_cp_reg(cpu, &rvbar);
5062 }
e60cef86 5063 define_arm_cp_regs(cpu, v8_idregs);
b0d2b7d0
PM
5064 define_arm_cp_regs(cpu, v8_cp_reginfo);
5065 }
3b685ba7 5066 if (arm_feature(env, ARM_FEATURE_EL2)) {
f0d574d6 5067 uint64_t vmpidr_def = mpidr_read_val(env);
731de9e6
EI
5068 ARMCPRegInfo vpidr_regs[] = {
5069 { .name = "VPIDR", .state = ARM_CP_STATE_AA32,
5070 .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
5071 .access = PL2_RW, .accessfn = access_el3_aa32ns,
36476562
PM
5072 .resetvalue = cpu->midr, .type = ARM_CP_ALIAS,
5073 .fieldoffset = offsetoflow32(CPUARMState, cp15.vpidr_el2) },
731de9e6
EI
5074 { .name = "VPIDR_EL2", .state = ARM_CP_STATE_AA64,
5075 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
5076 .access = PL2_RW, .resetvalue = cpu->midr,
5077 .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) },
f0d574d6
EI
5078 { .name = "VMPIDR", .state = ARM_CP_STATE_AA32,
5079 .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
5080 .access = PL2_RW, .accessfn = access_el3_aa32ns,
36476562
PM
5081 .resetvalue = vmpidr_def, .type = ARM_CP_ALIAS,
5082 .fieldoffset = offsetoflow32(CPUARMState, cp15.vmpidr_el2) },
f0d574d6
EI
5083 { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_AA64,
5084 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
5085 .access = PL2_RW,
5086 .resetvalue = vmpidr_def,
5087 .fieldoffset = offsetof(CPUARMState, cp15.vmpidr_el2) },
731de9e6
EI
5088 REGINFO_SENTINEL
5089 };
5090 define_arm_cp_regs(cpu, vpidr_regs);
4771cd01 5091 define_arm_cp_regs(cpu, el2_cp_reginfo);
be8e8128
GB
5092 /* RVBAR_EL2 is only implemented if EL2 is the highest EL */
5093 if (!arm_feature(env, ARM_FEATURE_EL3)) {
5094 ARMCPRegInfo rvbar = {
5095 .name = "RVBAR_EL2", .state = ARM_CP_STATE_AA64,
5096 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 1,
5097 .type = ARM_CP_CONST, .access = PL2_R, .resetvalue = cpu->rvbar
5098 };
5099 define_one_arm_cp_reg(cpu, &rvbar);
5100 }
d42e3c26
EI
5101 } else {
5102 /* If EL2 is missing but higher ELs are enabled, we need to
5103 * register the no_el2 reginfos.
5104 */
5105 if (arm_feature(env, ARM_FEATURE_EL3)) {
f0d574d6
EI
5106 /* When EL3 exists but not EL2, VPIDR and VMPIDR take the value
5107 * of MIDR_EL1 and MPIDR_EL1.
731de9e6
EI
5108 */
5109 ARMCPRegInfo vpidr_regs[] = {
5110 { .name = "VPIDR_EL2", .state = ARM_CP_STATE_BOTH,
5111 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
5112 .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any,
5113 .type = ARM_CP_CONST, .resetvalue = cpu->midr,
5114 .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) },
f0d574d6
EI
5115 { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_BOTH,
5116 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
5117 .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any,
5118 .type = ARM_CP_NO_RAW,
5119 .writefn = arm_cp_write_ignore, .readfn = mpidr_read },
731de9e6
EI
5120 REGINFO_SENTINEL
5121 };
5122 define_arm_cp_regs(cpu, vpidr_regs);
4771cd01 5123 define_arm_cp_regs(cpu, el3_no_el2_cp_reginfo);
d42e3c26 5124 }
3b685ba7 5125 }
81547d66 5126 if (arm_feature(env, ARM_FEATURE_EL3)) {
0f1a3b24 5127 define_arm_cp_regs(cpu, el3_cp_reginfo);
e24fdd23
PM
5128 ARMCPRegInfo el3_regs[] = {
5129 { .name = "RVBAR_EL3", .state = ARM_CP_STATE_AA64,
5130 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 1,
5131 .type = ARM_CP_CONST, .access = PL3_R, .resetvalue = cpu->rvbar },
5132 { .name = "SCTLR_EL3", .state = ARM_CP_STATE_AA64,
5133 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 0,
5134 .access = PL3_RW,
5135 .raw_writefn = raw_write, .writefn = sctlr_write,
5136 .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[3]),
5137 .resetvalue = cpu->reset_sctlr },
5138 REGINFO_SENTINEL
be8e8128 5139 };
e24fdd23
PM
5140
5141 define_arm_cp_regs(cpu, el3_regs);
81547d66 5142 }
2f027fc5
PM
5143 /* The behaviour of NSACR is sufficiently various that we don't
5144 * try to describe it in a single reginfo:
5145 * if EL3 is 64 bit, then trap to EL3 from S EL1,
5146 * reads as constant 0xc00 from NS EL1 and NS EL2
5147 * if EL3 is 32 bit, then RW at EL3, RO at NS EL1 and NS EL2
5148 * if v7 without EL3, register doesn't exist
5149 * if v8 without EL3, reads as constant 0xc00 from NS EL1 and NS EL2
5150 */
5151 if (arm_feature(env, ARM_FEATURE_EL3)) {
5152 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
5153 ARMCPRegInfo nsacr = {
5154 .name = "NSACR", .type = ARM_CP_CONST,
5155 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
5156 .access = PL1_RW, .accessfn = nsacr_access,
5157 .resetvalue = 0xc00
5158 };
5159 define_one_arm_cp_reg(cpu, &nsacr);
5160 } else {
5161 ARMCPRegInfo nsacr = {
5162 .name = "NSACR",
5163 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
5164 .access = PL3_RW | PL1_R,
5165 .resetvalue = 0,
5166 .fieldoffset = offsetof(CPUARMState, cp15.nsacr)
5167 };
5168 define_one_arm_cp_reg(cpu, &nsacr);
5169 }
5170 } else {
5171 if (arm_feature(env, ARM_FEATURE_V8)) {
5172 ARMCPRegInfo nsacr = {
5173 .name = "NSACR", .type = ARM_CP_CONST,
5174 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
5175 .access = PL1_R,
5176 .resetvalue = 0xc00
5177 };
5178 define_one_arm_cp_reg(cpu, &nsacr);
5179 }
5180 }
5181
452a0955 5182 if (arm_feature(env, ARM_FEATURE_PMSA)) {
6cb0b013
PC
5183 if (arm_feature(env, ARM_FEATURE_V6)) {
5184 /* PMSAv6 not implemented */
5185 assert(arm_feature(env, ARM_FEATURE_V7));
5186 define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
5187 define_arm_cp_regs(cpu, pmsav7_cp_reginfo);
5188 } else {
5189 define_arm_cp_regs(cpu, pmsav5_cp_reginfo);
5190 }
18032bec 5191 } else {
8e5d75c9 5192 define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
18032bec
PM
5193 define_arm_cp_regs(cpu, vmsa_cp_reginfo);
5194 }
c326b979
PM
5195 if (arm_feature(env, ARM_FEATURE_THUMB2EE)) {
5196 define_arm_cp_regs(cpu, t2ee_cp_reginfo);
5197 }
6cc7a3ae
PM
5198 if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) {
5199 define_arm_cp_regs(cpu, generic_timer_cp_reginfo);
5200 }
4a501606
PM
5201 if (arm_feature(env, ARM_FEATURE_VAPA)) {
5202 define_arm_cp_regs(cpu, vapa_cp_reginfo);
5203 }
c4804214
PM
5204 if (arm_feature(env, ARM_FEATURE_CACHE_TEST_CLEAN)) {
5205 define_arm_cp_regs(cpu, cache_test_clean_cp_reginfo);
5206 }
5207 if (arm_feature(env, ARM_FEATURE_CACHE_DIRTY_REG)) {
5208 define_arm_cp_regs(cpu, cache_dirty_status_cp_reginfo);
5209 }
5210 if (arm_feature(env, ARM_FEATURE_CACHE_BLOCK_OPS)) {
5211 define_arm_cp_regs(cpu, cache_block_ops_cp_reginfo);
5212 }
18032bec
PM
5213 if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
5214 define_arm_cp_regs(cpu, omap_cp_reginfo);
5215 }
34f90529
PM
5216 if (arm_feature(env, ARM_FEATURE_STRONGARM)) {
5217 define_arm_cp_regs(cpu, strongarm_cp_reginfo);
5218 }
1047b9d7
PM
5219 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
5220 define_arm_cp_regs(cpu, xscale_cp_reginfo);
5221 }
5222 if (arm_feature(env, ARM_FEATURE_DUMMY_C15_REGS)) {
5223 define_arm_cp_regs(cpu, dummy_c15_cp_reginfo);
5224 }
7ac681cf
PM
5225 if (arm_feature(env, ARM_FEATURE_LPAE)) {
5226 define_arm_cp_regs(cpu, lpae_cp_reginfo);
5227 }
7884849c
PM
5228 /* Slightly awkwardly, the OMAP and StrongARM cores need all of
5229 * cp15 crn=0 to be writes-ignored, whereas for other cores they should
5230 * be read-only (ie write causes UNDEF exception).
5231 */
5232 {
00a29f3d
PM
5233 ARMCPRegInfo id_pre_v8_midr_cp_reginfo[] = {
5234 /* Pre-v8 MIDR space.
5235 * Note that the MIDR isn't a simple constant register because
7884849c
PM
5236 * of the TI925 behaviour where writes to another register can
5237 * cause the MIDR value to change.
97ce8d61
PC
5238 *
5239 * Unimplemented registers in the c15 0 0 0 space default to
5240 * MIDR. Define MIDR first as this entire space, then CTR, TCMTR
5241 * and friends override accordingly.
7884849c
PM
5242 */
5243 { .name = "MIDR",
97ce8d61 5244 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = CP_ANY,
7884849c 5245 .access = PL1_R, .resetvalue = cpu->midr,
d4e6df63 5246 .writefn = arm_cp_write_ignore, .raw_writefn = raw_write,
731de9e6 5247 .readfn = midr_read,
97ce8d61
PC
5248 .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
5249 .type = ARM_CP_OVERRIDE },
7884849c
PM
5250 /* crn = 0 op1 = 0 crm = 3..7 : currently unassigned; we RAZ. */
5251 { .name = "DUMMY",
5252 .cp = 15, .crn = 0, .crm = 3, .opc1 = 0, .opc2 = CP_ANY,
5253 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
5254 { .name = "DUMMY",
5255 .cp = 15, .crn = 0, .crm = 4, .opc1 = 0, .opc2 = CP_ANY,
5256 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
5257 { .name = "DUMMY",
5258 .cp = 15, .crn = 0, .crm = 5, .opc1 = 0, .opc2 = CP_ANY,
5259 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
5260 { .name = "DUMMY",
5261 .cp = 15, .crn = 0, .crm = 6, .opc1 = 0, .opc2 = CP_ANY,
5262 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
5263 { .name = "DUMMY",
5264 .cp = 15, .crn = 0, .crm = 7, .opc1 = 0, .opc2 = CP_ANY,
5265 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
5266 REGINFO_SENTINEL
5267 };
00a29f3d 5268 ARMCPRegInfo id_v8_midr_cp_reginfo[] = {
00a29f3d
PM
5269 { .name = "MIDR_EL1", .state = ARM_CP_STATE_BOTH,
5270 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 0,
731de9e6
EI
5271 .access = PL1_R, .type = ARM_CP_NO_RAW, .resetvalue = cpu->midr,
5272 .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
5273 .readfn = midr_read },
ac00c79f
SF
5274 /* crn = 0 op1 = 0 crm = 0 op2 = 4,7 : AArch32 aliases of MIDR */
5275 { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
5276 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
5277 .access = PL1_R, .resetvalue = cpu->midr },
5278 { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
5279 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 7,
5280 .access = PL1_R, .resetvalue = cpu->midr },
00a29f3d
PM
5281 { .name = "REVIDR_EL1", .state = ARM_CP_STATE_BOTH,
5282 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 6,
13b72b2b 5283 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->revidr },
00a29f3d
PM
5284 REGINFO_SENTINEL
5285 };
5286 ARMCPRegInfo id_cp_reginfo[] = {
5287 /* These are common to v8 and pre-v8 */
5288 { .name = "CTR",
5289 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 1,
5290 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
5291 { .name = "CTR_EL0", .state = ARM_CP_STATE_AA64,
5292 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 0, .crm = 0,
5293 .access = PL0_R, .accessfn = ctr_el0_access,
5294 .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
5295 /* TCMTR and TLBTR exist in v8 but have no 64-bit versions */
5296 { .name = "TCMTR",
5297 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 2,
5298 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
00a29f3d
PM
5299 REGINFO_SENTINEL
5300 };
8085ce63
PC
5301 /* TLBTR is specific to VMSA */
5302 ARMCPRegInfo id_tlbtr_reginfo = {
5303 .name = "TLBTR",
5304 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 3,
5305 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0,
5306 };
3281af81
PC
5307 /* MPUIR is specific to PMSA V6+ */
5308 ARMCPRegInfo id_mpuir_reginfo = {
5309 .name = "MPUIR",
5310 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
5311 .access = PL1_R, .type = ARM_CP_CONST,
5312 .resetvalue = cpu->pmsav7_dregion << 8
5313 };
7884849c
PM
5314 ARMCPRegInfo crn0_wi_reginfo = {
5315 .name = "CRN0_WI", .cp = 15, .crn = 0, .crm = CP_ANY,
5316 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_W,
5317 .type = ARM_CP_NOP | ARM_CP_OVERRIDE
5318 };
5319 if (arm_feature(env, ARM_FEATURE_OMAPCP) ||
5320 arm_feature(env, ARM_FEATURE_STRONGARM)) {
5321 ARMCPRegInfo *r;
5322 /* Register the blanket "writes ignored" value first to cover the
a703eda1
PC
5323 * whole space. Then update the specific ID registers to allow write
5324 * access, so that they ignore writes rather than causing them to
5325 * UNDEF.
7884849c
PM
5326 */
5327 define_one_arm_cp_reg(cpu, &crn0_wi_reginfo);
00a29f3d
PM
5328 for (r = id_pre_v8_midr_cp_reginfo;
5329 r->type != ARM_CP_SENTINEL; r++) {
5330 r->access = PL1_RW;
5331 }
7884849c
PM
5332 for (r = id_cp_reginfo; r->type != ARM_CP_SENTINEL; r++) {
5333 r->access = PL1_RW;
7884849c 5334 }
8085ce63 5335 id_tlbtr_reginfo.access = PL1_RW;
3281af81 5336 id_tlbtr_reginfo.access = PL1_RW;
7884849c 5337 }
00a29f3d
PM
5338 if (arm_feature(env, ARM_FEATURE_V8)) {
5339 define_arm_cp_regs(cpu, id_v8_midr_cp_reginfo);
5340 } else {
5341 define_arm_cp_regs(cpu, id_pre_v8_midr_cp_reginfo);
5342 }
a703eda1 5343 define_arm_cp_regs(cpu, id_cp_reginfo);
452a0955 5344 if (!arm_feature(env, ARM_FEATURE_PMSA)) {
8085ce63 5345 define_one_arm_cp_reg(cpu, &id_tlbtr_reginfo);
3281af81
PC
5346 } else if (arm_feature(env, ARM_FEATURE_V7)) {
5347 define_one_arm_cp_reg(cpu, &id_mpuir_reginfo);
8085ce63 5348 }
7884849c
PM
5349 }
5350
97ce8d61
PC
5351 if (arm_feature(env, ARM_FEATURE_MPIDR)) {
5352 define_arm_cp_regs(cpu, mpidr_cp_reginfo);
5353 }
5354
2771db27 5355 if (arm_feature(env, ARM_FEATURE_AUXCR)) {
834a6c69
PM
5356 ARMCPRegInfo auxcr_reginfo[] = {
5357 { .name = "ACTLR_EL1", .state = ARM_CP_STATE_BOTH,
5358 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 1,
5359 .access = PL1_RW, .type = ARM_CP_CONST,
5360 .resetvalue = cpu->reset_auxcr },
5361 { .name = "ACTLR_EL2", .state = ARM_CP_STATE_BOTH,
5362 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 1,
5363 .access = PL2_RW, .type = ARM_CP_CONST,
5364 .resetvalue = 0 },
5365 { .name = "ACTLR_EL3", .state = ARM_CP_STATE_AA64,
5366 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 1,
5367 .access = PL3_RW, .type = ARM_CP_CONST,
5368 .resetvalue = 0 },
5369 REGINFO_SENTINEL
2771db27 5370 };
834a6c69 5371 define_arm_cp_regs(cpu, auxcr_reginfo);
2771db27
PM
5372 }
5373
d8ba780b 5374 if (arm_feature(env, ARM_FEATURE_CBAR)) {
f318cec6
PM
5375 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
5376 /* 32 bit view is [31:18] 0...0 [43:32]. */
5377 uint32_t cbar32 = (extract64(cpu->reset_cbar, 18, 14) << 18)
5378 | extract64(cpu->reset_cbar, 32, 12);
5379 ARMCPRegInfo cbar_reginfo[] = {
5380 { .name = "CBAR",
5381 .type = ARM_CP_CONST,
5382 .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0,
5383 .access = PL1_R, .resetvalue = cpu->reset_cbar },
5384 { .name = "CBAR_EL1", .state = ARM_CP_STATE_AA64,
5385 .type = ARM_CP_CONST,
5386 .opc0 = 3, .opc1 = 1, .crn = 15, .crm = 3, .opc2 = 0,
5387 .access = PL1_R, .resetvalue = cbar32 },
5388 REGINFO_SENTINEL
5389 };
5390 /* We don't implement a r/w 64 bit CBAR currently */
5391 assert(arm_feature(env, ARM_FEATURE_CBAR_RO));
5392 define_arm_cp_regs(cpu, cbar_reginfo);
5393 } else {
5394 ARMCPRegInfo cbar = {
5395 .name = "CBAR",
5396 .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0,
5397 .access = PL1_R|PL3_W, .resetvalue = cpu->reset_cbar,
5398 .fieldoffset = offsetof(CPUARMState,
5399 cp15.c15_config_base_address)
5400 };
5401 if (arm_feature(env, ARM_FEATURE_CBAR_RO)) {
5402 cbar.access = PL1_R;
5403 cbar.fieldoffset = 0;
5404 cbar.type = ARM_CP_CONST;
5405 }
5406 define_one_arm_cp_reg(cpu, &cbar);
5407 }
d8ba780b
PC
5408 }
5409
91db4642
CLG
5410 if (arm_feature(env, ARM_FEATURE_VBAR)) {
5411 ARMCPRegInfo vbar_cp_reginfo[] = {
5412 { .name = "VBAR", .state = ARM_CP_STATE_BOTH,
5413 .opc0 = 3, .crn = 12, .crm = 0, .opc1 = 0, .opc2 = 0,
5414 .access = PL1_RW, .writefn = vbar_write,
5415 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.vbar_s),
5416 offsetof(CPUARMState, cp15.vbar_ns) },
5417 .resetvalue = 0 },
5418 REGINFO_SENTINEL
5419 };
5420 define_arm_cp_regs(cpu, vbar_cp_reginfo);
5421 }
5422
2771db27
PM
5423 /* Generic registers whose values depend on the implementation */
5424 {
5425 ARMCPRegInfo sctlr = {
5ebafdf3 5426 .name = "SCTLR", .state = ARM_CP_STATE_BOTH,
137feaa9
FA
5427 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
5428 .access = PL1_RW,
5429 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.sctlr_s),
5430 offsetof(CPUARMState, cp15.sctlr_ns) },
d4e6df63
PM
5431 .writefn = sctlr_write, .resetvalue = cpu->reset_sctlr,
5432 .raw_writefn = raw_write,
2771db27
PM
5433 };
5434 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
5435 /* Normally we would always end the TB on an SCTLR write, but Linux
5436 * arch/arm/mach-pxa/sleep.S expects two instructions following
5437 * an MMU enable to execute from cache. Imitate this behaviour.
5438 */
5439 sctlr.type |= ARM_CP_SUPPRESS_TB_END;
5440 }
5441 define_one_arm_cp_reg(cpu, &sctlr);
5442 }
5be5e8ed
RH
5443
5444 if (arm_feature(env, ARM_FEATURE_SVE)) {
5445 define_one_arm_cp_reg(cpu, &zcr_el1_reginfo);
5446 if (arm_feature(env, ARM_FEATURE_EL2)) {
5447 define_one_arm_cp_reg(cpu, &zcr_el2_reginfo);
5448 } else {
5449 define_one_arm_cp_reg(cpu, &zcr_no_el2_reginfo);
5450 }
5451 if (arm_feature(env, ARM_FEATURE_EL3)) {
5452 define_one_arm_cp_reg(cpu, &zcr_el3_reginfo);
5453 }
5454 }
2ceb98c0
PM
5455}
5456
14969266
AF
5457void arm_cpu_register_gdb_regs_for_features(ARMCPU *cpu)
5458{
22169d41 5459 CPUState *cs = CPU(cpu);
14969266
AF
5460 CPUARMState *env = &cpu->env;
5461
6a669427
PM
5462 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
5463 gdb_register_coprocessor(cs, aarch64_fpu_gdb_get_reg,
5464 aarch64_fpu_gdb_set_reg,
5465 34, "aarch64-fpu.xml", 0);
5466 } else if (arm_feature(env, ARM_FEATURE_NEON)) {
22169d41 5467 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
56aebc89
PB
5468 51, "arm-neon.xml", 0);
5469 } else if (arm_feature(env, ARM_FEATURE_VFP3)) {
22169d41 5470 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
56aebc89
PB
5471 35, "arm-vfp3.xml", 0);
5472 } else if (arm_feature(env, ARM_FEATURE_VFP)) {
22169d41 5473 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
56aebc89
PB
5474 19, "arm-vfp.xml", 0);
5475 }
40f137e1
PB
5476}
5477
777dc784
PM
5478/* Sort alphabetically by type name, except for "any". */
5479static gint arm_cpu_list_compare(gconstpointer a, gconstpointer b)
5adb4839 5480{
777dc784
PM
5481 ObjectClass *class_a = (ObjectClass *)a;
5482 ObjectClass *class_b = (ObjectClass *)b;
5483 const char *name_a, *name_b;
5adb4839 5484
777dc784
PM
5485 name_a = object_class_get_name(class_a);
5486 name_b = object_class_get_name(class_b);
51492fd1 5487 if (strcmp(name_a, "any-" TYPE_ARM_CPU) == 0) {
777dc784 5488 return 1;
51492fd1 5489 } else if (strcmp(name_b, "any-" TYPE_ARM_CPU) == 0) {
777dc784
PM
5490 return -1;
5491 } else {
5492 return strcmp(name_a, name_b);
5adb4839
PB
5493 }
5494}
5495
777dc784 5496static void arm_cpu_list_entry(gpointer data, gpointer user_data)
40f137e1 5497{
777dc784 5498 ObjectClass *oc = data;
92a31361 5499 CPUListState *s = user_data;
51492fd1
AF
5500 const char *typename;
5501 char *name;
3371d272 5502
51492fd1
AF
5503 typename = object_class_get_name(oc);
5504 name = g_strndup(typename, strlen(typename) - strlen("-" TYPE_ARM_CPU));
777dc784 5505 (*s->cpu_fprintf)(s->file, " %s\n",
51492fd1
AF
5506 name);
5507 g_free(name);
777dc784
PM
5508}
5509
5510void arm_cpu_list(FILE *f, fprintf_function cpu_fprintf)
5511{
92a31361 5512 CPUListState s = {
777dc784
PM
5513 .file = f,
5514 .cpu_fprintf = cpu_fprintf,
5515 };
5516 GSList *list;
5517
5518 list = object_class_get_list(TYPE_ARM_CPU, false);
5519 list = g_slist_sort(list, arm_cpu_list_compare);
5520 (*cpu_fprintf)(f, "Available CPUs:\n");
5521 g_slist_foreach(list, arm_cpu_list_entry, &s);
5522 g_slist_free(list);
a96c0514
PM
5523#ifdef CONFIG_KVM
5524 /* The 'host' CPU type is dynamically registered only if KVM is
5525 * enabled, so we have to special-case it here:
5526 */
5527 (*cpu_fprintf)(f, " host (only available in KVM mode)\n");
5528#endif
40f137e1
PB
5529}
5530
78027bb6
CR
5531static void arm_cpu_add_definition(gpointer data, gpointer user_data)
5532{
5533 ObjectClass *oc = data;
5534 CpuDefinitionInfoList **cpu_list = user_data;
5535 CpuDefinitionInfoList *entry;
5536 CpuDefinitionInfo *info;
5537 const char *typename;
5538
5539 typename = object_class_get_name(oc);
5540 info = g_malloc0(sizeof(*info));
5541 info->name = g_strndup(typename,
5542 strlen(typename) - strlen("-" TYPE_ARM_CPU));
8ed877b7 5543 info->q_typename = g_strdup(typename);
78027bb6
CR
5544
5545 entry = g_malloc0(sizeof(*entry));
5546 entry->value = info;
5547 entry->next = *cpu_list;
5548 *cpu_list = entry;
5549}
5550
5551CpuDefinitionInfoList *arch_query_cpu_definitions(Error **errp)
5552{
5553 CpuDefinitionInfoList *cpu_list = NULL;
5554 GSList *list;
5555
5556 list = object_class_get_list(TYPE_ARM_CPU, false);
5557 g_slist_foreach(list, arm_cpu_add_definition, &cpu_list);
5558 g_slist_free(list);
5559
5560 return cpu_list;
5561}
5562
6e6efd61 5563static void add_cpreg_to_hashtable(ARMCPU *cpu, const ARMCPRegInfo *r,
51a79b03 5564 void *opaque, int state, int secstate,
f5a0a5a5 5565 int crm, int opc1, int opc2)
6e6efd61
PM
5566{
5567 /* Private utility function for define_one_arm_cp_reg_with_opaque():
5568 * add a single reginfo struct to the hash table.
5569 */
5570 uint32_t *key = g_new(uint32_t, 1);
5571 ARMCPRegInfo *r2 = g_memdup(r, sizeof(ARMCPRegInfo));
5572 int is64 = (r->type & ARM_CP_64BIT) ? 1 : 0;
3f3c82a5
FA
5573 int ns = (secstate & ARM_CP_SECSTATE_NS) ? 1 : 0;
5574
5575 /* Reset the secure state to the specific incoming state. This is
5576 * necessary as the register may have been defined with both states.
5577 */
5578 r2->secure = secstate;
5579
5580 if (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1]) {
5581 /* Register is banked (using both entries in array).
5582 * Overwriting fieldoffset as the array is only used to define
5583 * banked registers but later only fieldoffset is used.
f5a0a5a5 5584 */
3f3c82a5
FA
5585 r2->fieldoffset = r->bank_fieldoffsets[ns];
5586 }
5587
5588 if (state == ARM_CP_STATE_AA32) {
5589 if (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1]) {
5590 /* If the register is banked then we don't need to migrate or
5591 * reset the 32-bit instance in certain cases:
5592 *
5593 * 1) If the register has both 32-bit and 64-bit instances then we
5594 * can count on the 64-bit instance taking care of the
5595 * non-secure bank.
5596 * 2) If ARMv8 is enabled then we can count on a 64-bit version
5597 * taking care of the secure bank. This requires that separate
5598 * 32 and 64-bit definitions are provided.
5599 */
5600 if ((r->state == ARM_CP_STATE_BOTH && ns) ||
5601 (arm_feature(&cpu->env, ARM_FEATURE_V8) && !ns)) {
7a0e58fa 5602 r2->type |= ARM_CP_ALIAS;
3f3c82a5
FA
5603 }
5604 } else if ((secstate != r->secure) && !ns) {
5605 /* The register is not banked so we only want to allow migration of
5606 * the non-secure instance.
5607 */
7a0e58fa 5608 r2->type |= ARM_CP_ALIAS;
58a1d8ce 5609 }
3f3c82a5
FA
5610
5611 if (r->state == ARM_CP_STATE_BOTH) {
5612 /* We assume it is a cp15 register if the .cp field is left unset.
5613 */
5614 if (r2->cp == 0) {
5615 r2->cp = 15;
5616 }
5617
f5a0a5a5 5618#ifdef HOST_WORDS_BIGENDIAN
3f3c82a5
FA
5619 if (r2->fieldoffset) {
5620 r2->fieldoffset += sizeof(uint32_t);
5621 }
f5a0a5a5 5622#endif
3f3c82a5 5623 }
f5a0a5a5
PM
5624 }
5625 if (state == ARM_CP_STATE_AA64) {
5626 /* To allow abbreviation of ARMCPRegInfo
5627 * definitions, we treat cp == 0 as equivalent to
5628 * the value for "standard guest-visible sysreg".
58a1d8ce
PM
5629 * STATE_BOTH definitions are also always "standard
5630 * sysreg" in their AArch64 view (the .cp value may
5631 * be non-zero for the benefit of the AArch32 view).
f5a0a5a5 5632 */
58a1d8ce 5633 if (r->cp == 0 || r->state == ARM_CP_STATE_BOTH) {
f5a0a5a5
PM
5634 r2->cp = CP_REG_ARM64_SYSREG_CP;
5635 }
5636 *key = ENCODE_AA64_CP_REG(r2->cp, r2->crn, crm,
5637 r2->opc0, opc1, opc2);
5638 } else {
51a79b03 5639 *key = ENCODE_CP_REG(r2->cp, is64, ns, r2->crn, crm, opc1, opc2);
f5a0a5a5 5640 }
6e6efd61
PM
5641 if (opaque) {
5642 r2->opaque = opaque;
5643 }
67ed771d
PM
5644 /* reginfo passed to helpers is correct for the actual access,
5645 * and is never ARM_CP_STATE_BOTH:
5646 */
5647 r2->state = state;
6e6efd61
PM
5648 /* Make sure reginfo passed to helpers for wildcarded regs
5649 * has the correct crm/opc1/opc2 for this reg, not CP_ANY:
5650 */
5651 r2->crm = crm;
5652 r2->opc1 = opc1;
5653 r2->opc2 = opc2;
5654 /* By convention, for wildcarded registers only the first
5655 * entry is used for migration; the others are marked as
7a0e58fa 5656 * ALIAS so we don't try to transfer the register
6e6efd61 5657 * multiple times. Special registers (ie NOP/WFI) are
7a0e58fa 5658 * never migratable and not even raw-accessible.
6e6efd61 5659 */
7a0e58fa
PM
5660 if ((r->type & ARM_CP_SPECIAL)) {
5661 r2->type |= ARM_CP_NO_RAW;
5662 }
5663 if (((r->crm == CP_ANY) && crm != 0) ||
6e6efd61
PM
5664 ((r->opc1 == CP_ANY) && opc1 != 0) ||
5665 ((r->opc2 == CP_ANY) && opc2 != 0)) {
7a0e58fa 5666 r2->type |= ARM_CP_ALIAS;
6e6efd61
PM
5667 }
5668
375421cc
PM
5669 /* Check that raw accesses are either forbidden or handled. Note that
5670 * we can't assert this earlier because the setup of fieldoffset for
5671 * banked registers has to be done first.
5672 */
5673 if (!(r2->type & ARM_CP_NO_RAW)) {
5674 assert(!raw_accessors_invalid(r2));
5675 }
5676
6e6efd61
PM
5677 /* Overriding of an existing definition must be explicitly
5678 * requested.
5679 */
5680 if (!(r->type & ARM_CP_OVERRIDE)) {
5681 ARMCPRegInfo *oldreg;
5682 oldreg = g_hash_table_lookup(cpu->cp_regs, key);
5683 if (oldreg && !(oldreg->type & ARM_CP_OVERRIDE)) {
5684 fprintf(stderr, "Register redefined: cp=%d %d bit "
5685 "crn=%d crm=%d opc1=%d opc2=%d, "
5686 "was %s, now %s\n", r2->cp, 32 + 32 * is64,
5687 r2->crn, r2->crm, r2->opc1, r2->opc2,
5688 oldreg->name, r2->name);
5689 g_assert_not_reached();
5690 }
5691 }
5692 g_hash_table_insert(cpu->cp_regs, key, r2);
5693}
5694
5695
4b6a83fb
PM
5696void define_one_arm_cp_reg_with_opaque(ARMCPU *cpu,
5697 const ARMCPRegInfo *r, void *opaque)
5698{
5699 /* Define implementations of coprocessor registers.
5700 * We store these in a hashtable because typically
5701 * there are less than 150 registers in a space which
5702 * is 16*16*16*8*8 = 262144 in size.
5703 * Wildcarding is supported for the crm, opc1 and opc2 fields.
5704 * If a register is defined twice then the second definition is
5705 * used, so this can be used to define some generic registers and
5706 * then override them with implementation specific variations.
5707 * At least one of the original and the second definition should
5708 * include ARM_CP_OVERRIDE in its type bits -- this is just a guard
5709 * against accidental use.
f5a0a5a5
PM
5710 *
5711 * The state field defines whether the register is to be
5712 * visible in the AArch32 or AArch64 execution state. If the
5713 * state is set to ARM_CP_STATE_BOTH then we synthesise a
5714 * reginfo structure for the AArch32 view, which sees the lower
5715 * 32 bits of the 64 bit register.
5716 *
5717 * Only registers visible in AArch64 may set r->opc0; opc0 cannot
5718 * be wildcarded. AArch64 registers are always considered to be 64
5719 * bits; the ARM_CP_64BIT* flag applies only to the AArch32 view of
5720 * the register, if any.
4b6a83fb 5721 */
f5a0a5a5 5722 int crm, opc1, opc2, state;
4b6a83fb
PM
5723 int crmmin = (r->crm == CP_ANY) ? 0 : r->crm;
5724 int crmmax = (r->crm == CP_ANY) ? 15 : r->crm;
5725 int opc1min = (r->opc1 == CP_ANY) ? 0 : r->opc1;
5726 int opc1max = (r->opc1 == CP_ANY) ? 7 : r->opc1;
5727 int opc2min = (r->opc2 == CP_ANY) ? 0 : r->opc2;
5728 int opc2max = (r->opc2 == CP_ANY) ? 7 : r->opc2;
5729 /* 64 bit registers have only CRm and Opc1 fields */
5730 assert(!((r->type & ARM_CP_64BIT) && (r->opc2 || r->crn)));
f5a0a5a5
PM
5731 /* op0 only exists in the AArch64 encodings */
5732 assert((r->state != ARM_CP_STATE_AA32) || (r->opc0 == 0));
5733 /* AArch64 regs are all 64 bit so ARM_CP_64BIT is meaningless */
5734 assert((r->state != ARM_CP_STATE_AA64) || !(r->type & ARM_CP_64BIT));
5735 /* The AArch64 pseudocode CheckSystemAccess() specifies that op1
5736 * encodes a minimum access level for the register. We roll this
5737 * runtime check into our general permission check code, so check
5738 * here that the reginfo's specified permissions are strict enough
5739 * to encompass the generic architectural permission check.
5740 */
5741 if (r->state != ARM_CP_STATE_AA32) {
5742 int mask = 0;
5743 switch (r->opc1) {
5744 case 0: case 1: case 2:
5745 /* min_EL EL1 */
5746 mask = PL1_RW;
5747 break;
5748 case 3:
5749 /* min_EL EL0 */
5750 mask = PL0_RW;
5751 break;
5752 case 4:
5753 /* min_EL EL2 */
5754 mask = PL2_RW;
5755 break;
5756 case 5:
5757 /* unallocated encoding, so not possible */
5758 assert(false);
5759 break;
5760 case 6:
5761 /* min_EL EL3 */
5762 mask = PL3_RW;
5763 break;
5764 case 7:
5765 /* min_EL EL1, secure mode only (we don't check the latter) */
5766 mask = PL1_RW;
5767 break;
5768 default:
5769 /* broken reginfo with out-of-range opc1 */
5770 assert(false);
5771 break;
5772 }
5773 /* assert our permissions are not too lax (stricter is fine) */
5774 assert((r->access & ~mask) == 0);
5775 }
5776
4b6a83fb
PM
5777 /* Check that the register definition has enough info to handle
5778 * reads and writes if they are permitted.
5779 */
5780 if (!(r->type & (ARM_CP_SPECIAL|ARM_CP_CONST))) {
5781 if (r->access & PL3_R) {
3f3c82a5
FA
5782 assert((r->fieldoffset ||
5783 (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
5784 r->readfn);
4b6a83fb
PM
5785 }
5786 if (r->access & PL3_W) {
3f3c82a5
FA
5787 assert((r->fieldoffset ||
5788 (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
5789 r->writefn);
4b6a83fb
PM
5790 }
5791 }
5792 /* Bad type field probably means missing sentinel at end of reg list */
5793 assert(cptype_valid(r->type));
5794 for (crm = crmmin; crm <= crmmax; crm++) {
5795 for (opc1 = opc1min; opc1 <= opc1max; opc1++) {
5796 for (opc2 = opc2min; opc2 <= opc2max; opc2++) {
f5a0a5a5
PM
5797 for (state = ARM_CP_STATE_AA32;
5798 state <= ARM_CP_STATE_AA64; state++) {
5799 if (r->state != state && r->state != ARM_CP_STATE_BOTH) {
5800 continue;
5801 }
3f3c82a5
FA
5802 if (state == ARM_CP_STATE_AA32) {
5803 /* Under AArch32 CP registers can be common
5804 * (same for secure and non-secure world) or banked.
5805 */
5806 switch (r->secure) {
5807 case ARM_CP_SECSTATE_S:
5808 case ARM_CP_SECSTATE_NS:
5809 add_cpreg_to_hashtable(cpu, r, opaque, state,
5810 r->secure, crm, opc1, opc2);
5811 break;
5812 default:
5813 add_cpreg_to_hashtable(cpu, r, opaque, state,
5814 ARM_CP_SECSTATE_S,
5815 crm, opc1, opc2);
5816 add_cpreg_to_hashtable(cpu, r, opaque, state,
5817 ARM_CP_SECSTATE_NS,
5818 crm, opc1, opc2);
5819 break;
5820 }
5821 } else {
5822 /* AArch64 registers get mapped to non-secure instance
5823 * of AArch32 */
5824 add_cpreg_to_hashtable(cpu, r, opaque, state,
5825 ARM_CP_SECSTATE_NS,
5826 crm, opc1, opc2);
5827 }
f5a0a5a5 5828 }
4b6a83fb
PM
5829 }
5830 }
5831 }
5832}
5833
5834void define_arm_cp_regs_with_opaque(ARMCPU *cpu,
5835 const ARMCPRegInfo *regs, void *opaque)
5836{
5837 /* Define a whole list of registers */
5838 const ARMCPRegInfo *r;
5839 for (r = regs; r->type != ARM_CP_SENTINEL; r++) {
5840 define_one_arm_cp_reg_with_opaque(cpu, r, opaque);
5841 }
5842}
5843
60322b39 5844const ARMCPRegInfo *get_arm_cp_reginfo(GHashTable *cpregs, uint32_t encoded_cp)
4b6a83fb 5845{
60322b39 5846 return g_hash_table_lookup(cpregs, &encoded_cp);
4b6a83fb
PM
5847}
5848
c4241c7d
PM
5849void arm_cp_write_ignore(CPUARMState *env, const ARMCPRegInfo *ri,
5850 uint64_t value)
4b6a83fb
PM
5851{
5852 /* Helper coprocessor write function for write-ignore registers */
4b6a83fb
PM
5853}
5854
c4241c7d 5855uint64_t arm_cp_read_zero(CPUARMState *env, const ARMCPRegInfo *ri)
4b6a83fb
PM
5856{
5857 /* Helper coprocessor write function for read-as-zero registers */
4b6a83fb
PM
5858 return 0;
5859}
5860
f5a0a5a5
PM
5861void arm_cp_reset_ignore(CPUARMState *env, const ARMCPRegInfo *opaque)
5862{
5863 /* Helper coprocessor reset function for do-nothing-on-reset registers */
5864}
5865
af393ffc 5866static int bad_mode_switch(CPUARMState *env, int mode, CPSRWriteType write_type)
37064a8b
PM
5867{
5868 /* Return true if it is not valid for us to switch to
5869 * this CPU mode (ie all the UNPREDICTABLE cases in
5870 * the ARM ARM CPSRWriteByInstr pseudocode).
5871 */
af393ffc
PM
5872
5873 /* Changes to or from Hyp via MSR and CPS are illegal. */
5874 if (write_type == CPSRWriteByInstr &&
5875 ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_HYP ||
5876 mode == ARM_CPU_MODE_HYP)) {
5877 return 1;
5878 }
5879
37064a8b
PM
5880 switch (mode) {
5881 case ARM_CPU_MODE_USR:
10eacda7 5882 return 0;
37064a8b
PM
5883 case ARM_CPU_MODE_SYS:
5884 case ARM_CPU_MODE_SVC:
5885 case ARM_CPU_MODE_ABT:
5886 case ARM_CPU_MODE_UND:
5887 case ARM_CPU_MODE_IRQ:
5888 case ARM_CPU_MODE_FIQ:
52ff951b
PM
5889 /* Note that we don't implement the IMPDEF NSACR.RFR which in v7
5890 * allows FIQ mode to be Secure-only. (In v8 this doesn't exist.)
5891 */
10eacda7
PM
5892 /* If HCR.TGE is set then changes from Monitor to NS PL1 via MSR
5893 * and CPS are treated as illegal mode changes.
5894 */
5895 if (write_type == CPSRWriteByInstr &&
5896 (env->cp15.hcr_el2 & HCR_TGE) &&
5897 (env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON &&
5898 !arm_is_secure_below_el3(env)) {
5899 return 1;
5900 }
37064a8b 5901 return 0;
e6c8fc07
PM
5902 case ARM_CPU_MODE_HYP:
5903 return !arm_feature(env, ARM_FEATURE_EL2)
5904 || arm_current_el(env) < 2 || arm_is_secure(env);
027fc527 5905 case ARM_CPU_MODE_MON:
58ae2d1f 5906 return arm_current_el(env) < 3;
37064a8b
PM
5907 default:
5908 return 1;
5909 }
5910}
5911
2f4a40e5
AZ
5912uint32_t cpsr_read(CPUARMState *env)
5913{
5914 int ZF;
6fbe23d5
PB
5915 ZF = (env->ZF == 0);
5916 return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) |
2f4a40e5
AZ
5917 (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27)
5918 | (env->thumb << 5) | ((env->condexec_bits & 3) << 25)
5919 | ((env->condexec_bits & 0xfc) << 8)
af519934 5920 | (env->GE << 16) | (env->daif & CPSR_AIF);
2f4a40e5
AZ
5921}
5922
50866ba5
PM
5923void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask,
5924 CPSRWriteType write_type)
2f4a40e5 5925{
6e8801f9
FA
5926 uint32_t changed_daif;
5927
2f4a40e5 5928 if (mask & CPSR_NZCV) {
6fbe23d5
PB
5929 env->ZF = (~val) & CPSR_Z;
5930 env->NF = val;
2f4a40e5
AZ
5931 env->CF = (val >> 29) & 1;
5932 env->VF = (val << 3) & 0x80000000;
5933 }
5934 if (mask & CPSR_Q)
5935 env->QF = ((val & CPSR_Q) != 0);
5936 if (mask & CPSR_T)
5937 env->thumb = ((val & CPSR_T) != 0);
5938 if (mask & CPSR_IT_0_1) {
5939 env->condexec_bits &= ~3;
5940 env->condexec_bits |= (val >> 25) & 3;
5941 }
5942 if (mask & CPSR_IT_2_7) {
5943 env->condexec_bits &= 3;
5944 env->condexec_bits |= (val >> 8) & 0xfc;
5945 }
5946 if (mask & CPSR_GE) {
5947 env->GE = (val >> 16) & 0xf;
5948 }
5949
6e8801f9
FA
5950 /* In a V7 implementation that includes the security extensions but does
5951 * not include Virtualization Extensions the SCR.FW and SCR.AW bits control
5952 * whether non-secure software is allowed to change the CPSR_F and CPSR_A
5953 * bits respectively.
5954 *
5955 * In a V8 implementation, it is permitted for privileged software to
5956 * change the CPSR A/F bits regardless of the SCR.AW/FW bits.
5957 */
f8c88bbc 5958 if (write_type != CPSRWriteRaw && !arm_feature(env, ARM_FEATURE_V8) &&
6e8801f9
FA
5959 arm_feature(env, ARM_FEATURE_EL3) &&
5960 !arm_feature(env, ARM_FEATURE_EL2) &&
5961 !arm_is_secure(env)) {
5962
5963 changed_daif = (env->daif ^ val) & mask;
5964
5965 if (changed_daif & CPSR_A) {
5966 /* Check to see if we are allowed to change the masking of async
5967 * abort exceptions from a non-secure state.
5968 */
5969 if (!(env->cp15.scr_el3 & SCR_AW)) {
5970 qemu_log_mask(LOG_GUEST_ERROR,
5971 "Ignoring attempt to switch CPSR_A flag from "
5972 "non-secure world with SCR.AW bit clear\n");
5973 mask &= ~CPSR_A;
5974 }
5975 }
5976
5977 if (changed_daif & CPSR_F) {
5978 /* Check to see if we are allowed to change the masking of FIQ
5979 * exceptions from a non-secure state.
5980 */
5981 if (!(env->cp15.scr_el3 & SCR_FW)) {
5982 qemu_log_mask(LOG_GUEST_ERROR,
5983 "Ignoring attempt to switch CPSR_F flag from "
5984 "non-secure world with SCR.FW bit clear\n");
5985 mask &= ~CPSR_F;
5986 }
5987
5988 /* Check whether non-maskable FIQ (NMFI) support is enabled.
5989 * If this bit is set software is not allowed to mask
5990 * FIQs, but is allowed to set CPSR_F to 0.
5991 */
5992 if ((A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_NMFI) &&
5993 (val & CPSR_F)) {
5994 qemu_log_mask(LOG_GUEST_ERROR,
5995 "Ignoring attempt to enable CPSR_F flag "
5996 "(non-maskable FIQ [NMFI] support enabled)\n");
5997 mask &= ~CPSR_F;
5998 }
5999 }
6000 }
6001
4cc35614
PM
6002 env->daif &= ~(CPSR_AIF & mask);
6003 env->daif |= val & CPSR_AIF & mask;
6004
f8c88bbc
PM
6005 if (write_type != CPSRWriteRaw &&
6006 ((env->uncached_cpsr ^ val) & mask & CPSR_M)) {
8c4f0eb9
PM
6007 if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_USR) {
6008 /* Note that we can only get here in USR mode if this is a
6009 * gdb stub write; for this case we follow the architectural
6010 * behaviour for guest writes in USR mode of ignoring an attempt
6011 * to switch mode. (Those are caught by translate.c for writes
6012 * triggered by guest instructions.)
6013 */
6014 mask &= ~CPSR_M;
6015 } else if (bad_mode_switch(env, val & CPSR_M, write_type)) {
81907a58
PM
6016 /* Attempt to switch to an invalid mode: this is UNPREDICTABLE in
6017 * v7, and has defined behaviour in v8:
6018 * + leave CPSR.M untouched
6019 * + allow changes to the other CPSR fields
6020 * + set PSTATE.IL
6021 * For user changes via the GDB stub, we don't set PSTATE.IL,
6022 * as this would be unnecessarily harsh for a user error.
37064a8b
PM
6023 */
6024 mask &= ~CPSR_M;
81907a58
PM
6025 if (write_type != CPSRWriteByGDBStub &&
6026 arm_feature(env, ARM_FEATURE_V8)) {
6027 mask |= CPSR_IL;
6028 val |= CPSR_IL;
6029 }
37064a8b
PM
6030 } else {
6031 switch_mode(env, val & CPSR_M);
6032 }
2f4a40e5
AZ
6033 }
6034 mask &= ~CACHED_CPSR_BITS;
6035 env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask);
6036}
6037
b26eefb6
PB
6038/* Sign/zero extend */
6039uint32_t HELPER(sxtb16)(uint32_t x)
6040{
6041 uint32_t res;
6042 res = (uint16_t)(int8_t)x;
6043 res |= (uint32_t)(int8_t)(x >> 16) << 16;
6044 return res;
6045}
6046
6047uint32_t HELPER(uxtb16)(uint32_t x)
6048{
6049 uint32_t res;
6050 res = (uint16_t)(uint8_t)x;
6051 res |= (uint32_t)(uint8_t)(x >> 16) << 16;
6052 return res;
6053}
6054
3670669c
PB
6055int32_t HELPER(sdiv)(int32_t num, int32_t den)
6056{
6057 if (den == 0)
6058 return 0;
686eeb93
AJ
6059 if (num == INT_MIN && den == -1)
6060 return INT_MIN;
3670669c
PB
6061 return num / den;
6062}
6063
6064uint32_t HELPER(udiv)(uint32_t num, uint32_t den)
6065{
6066 if (den == 0)
6067 return 0;
6068 return num / den;
6069}
6070
6071uint32_t HELPER(rbit)(uint32_t x)
6072{
42fedbca 6073 return revbit32(x);
3670669c
PB
6074}
6075
5fafdf24 6076#if defined(CONFIG_USER_ONLY)
b5ff1b31 6077
9ee6e8bb 6078/* These should probably raise undefined insn exceptions. */
0ecb72a5 6079void HELPER(v7m_msr)(CPUARMState *env, uint32_t reg, uint32_t val)
9ee6e8bb 6080{
a47dddd7
AF
6081 ARMCPU *cpu = arm_env_get_cpu(env);
6082
6083 cpu_abort(CPU(cpu), "v7m_msr %d\n", reg);
9ee6e8bb
PB
6084}
6085
0ecb72a5 6086uint32_t HELPER(v7m_mrs)(CPUARMState *env, uint32_t reg)
9ee6e8bb 6087{
a47dddd7
AF
6088 ARMCPU *cpu = arm_env_get_cpu(env);
6089
6090 cpu_abort(CPU(cpu), "v7m_mrs %d\n", reg);
9ee6e8bb
PB
6091 return 0;
6092}
6093
fb602cb7
PM
6094void HELPER(v7m_bxns)(CPUARMState *env, uint32_t dest)
6095{
6096 /* translate.c should never generate calls here in user-only mode */
6097 g_assert_not_reached();
6098}
6099
3e3fa230
PM
6100void HELPER(v7m_blxns)(CPUARMState *env, uint32_t dest)
6101{
6102 /* translate.c should never generate calls here in user-only mode */
6103 g_assert_not_reached();
6104}
6105
5158de24
PM
6106uint32_t HELPER(v7m_tt)(CPUARMState *env, uint32_t addr, uint32_t op)
6107{
6108 /* The TT instructions can be used by unprivileged code, but in
6109 * user-only emulation we don't have the MPU.
6110 * Luckily since we know we are NonSecure unprivileged (and that in
6111 * turn means that the A flag wasn't specified), all the bits in the
6112 * register must be zero:
6113 * IREGION: 0 because IRVALID is 0
6114 * IRVALID: 0 because NS
6115 * S: 0 because NS
6116 * NSRW: 0 because NS
6117 * NSR: 0 because NS
6118 * RW: 0 because unpriv and A flag not set
6119 * R: 0 because unpriv and A flag not set
6120 * SRVALID: 0 because NS
6121 * MRVALID: 0 because unpriv and A flag not set
6122 * SREGION: 0 becaus SRVALID is 0
6123 * MREGION: 0 because MRVALID is 0
6124 */
6125 return 0;
6126}
6127
0ecb72a5 6128void switch_mode(CPUARMState *env, int mode)
b5ff1b31 6129{
a47dddd7
AF
6130 ARMCPU *cpu = arm_env_get_cpu(env);
6131
6132 if (mode != ARM_CPU_MODE_USR) {
6133 cpu_abort(CPU(cpu), "Tried to switch out of user mode\n");
6134 }
b5ff1b31
FB
6135}
6136
012a906b
GB
6137uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
6138 uint32_t cur_el, bool secure)
9e729b57
EI
6139{
6140 return 1;
6141}
6142
ce02049d
GB
6143void aarch64_sync_64_to_32(CPUARMState *env)
6144{
6145 g_assert_not_reached();
6146}
6147
b5ff1b31
FB
6148#else
6149
0ecb72a5 6150void switch_mode(CPUARMState *env, int mode)
b5ff1b31
FB
6151{
6152 int old_mode;
6153 int i;
6154
6155 old_mode = env->uncached_cpsr & CPSR_M;
6156 if (mode == old_mode)
6157 return;
6158
6159 if (old_mode == ARM_CPU_MODE_FIQ) {
6160 memcpy (env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t));
8637c67f 6161 memcpy (env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t));
b5ff1b31
FB
6162 } else if (mode == ARM_CPU_MODE_FIQ) {
6163 memcpy (env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t));
8637c67f 6164 memcpy (env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t));
b5ff1b31
FB
6165 }
6166
f5206413 6167 i = bank_number(old_mode);
b5ff1b31
FB
6168 env->banked_r13[i] = env->regs[13];
6169 env->banked_r14[i] = env->regs[14];
6170 env->banked_spsr[i] = env->spsr;
6171
f5206413 6172 i = bank_number(mode);
b5ff1b31
FB
6173 env->regs[13] = env->banked_r13[i];
6174 env->regs[14] = env->banked_r14[i];
6175 env->spsr = env->banked_spsr[i];
6176}
6177
0eeb17d6
GB
6178/* Physical Interrupt Target EL Lookup Table
6179 *
6180 * [ From ARM ARM section G1.13.4 (Table G1-15) ]
6181 *
6182 * The below multi-dimensional table is used for looking up the target
6183 * exception level given numerous condition criteria. Specifically, the
6184 * target EL is based on SCR and HCR routing controls as well as the
6185 * currently executing EL and secure state.
6186 *
6187 * Dimensions:
6188 * target_el_table[2][2][2][2][2][4]
6189 * | | | | | +--- Current EL
6190 * | | | | +------ Non-secure(0)/Secure(1)
6191 * | | | +--------- HCR mask override
6192 * | | +------------ SCR exec state control
6193 * | +--------------- SCR mask override
6194 * +------------------ 32-bit(0)/64-bit(1) EL3
6195 *
6196 * The table values are as such:
6197 * 0-3 = EL0-EL3
6198 * -1 = Cannot occur
6199 *
6200 * The ARM ARM target EL table includes entries indicating that an "exception
6201 * is not taken". The two cases where this is applicable are:
6202 * 1) An exception is taken from EL3 but the SCR does not have the exception
6203 * routed to EL3.
6204 * 2) An exception is taken from EL2 but the HCR does not have the exception
6205 * routed to EL2.
6206 * In these two cases, the below table contain a target of EL1. This value is
6207 * returned as it is expected that the consumer of the table data will check
6208 * for "target EL >= current EL" to ensure the exception is not taken.
6209 *
6210 * SCR HCR
6211 * 64 EA AMO From
6212 * BIT IRQ IMO Non-secure Secure
6213 * EL3 FIQ RW FMO EL0 EL1 EL2 EL3 EL0 EL1 EL2 EL3
6214 */
82c39f6a 6215static const int8_t target_el_table[2][2][2][2][2][4] = {
0eeb17d6
GB
6216 {{{{/* 0 0 0 0 */{ 1, 1, 2, -1 },{ 3, -1, -1, 3 },},
6217 {/* 0 0 0 1 */{ 2, 2, 2, -1 },{ 3, -1, -1, 3 },},},
6218 {{/* 0 0 1 0 */{ 1, 1, 2, -1 },{ 3, -1, -1, 3 },},
6219 {/* 0 0 1 1 */{ 2, 2, 2, -1 },{ 3, -1, -1, 3 },},},},
6220 {{{/* 0 1 0 0 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},
6221 {/* 0 1 0 1 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},},
6222 {{/* 0 1 1 0 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},
6223 {/* 0 1 1 1 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},},},},
6224 {{{{/* 1 0 0 0 */{ 1, 1, 2, -1 },{ 1, 1, -1, 1 },},
6225 {/* 1 0 0 1 */{ 2, 2, 2, -1 },{ 1, 1, -1, 1 },},},
6226 {{/* 1 0 1 0 */{ 1, 1, 1, -1 },{ 1, 1, -1, 1 },},
6227 {/* 1 0 1 1 */{ 2, 2, 2, -1 },{ 1, 1, -1, 1 },},},},
6228 {{{/* 1 1 0 0 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},
6229 {/* 1 1 0 1 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},},
6230 {{/* 1 1 1 0 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},
6231 {/* 1 1 1 1 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},},},},
6232};
6233
6234/*
6235 * Determine the target EL for physical exceptions
6236 */
012a906b
GB
6237uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
6238 uint32_t cur_el, bool secure)
0eeb17d6
GB
6239{
6240 CPUARMState *env = cs->env_ptr;
2cde031f 6241 int rw;
0eeb17d6
GB
6242 int scr;
6243 int hcr;
6244 int target_el;
2cde031f
SS
6245 /* Is the highest EL AArch64? */
6246 int is64 = arm_feature(env, ARM_FEATURE_AARCH64);
6247
6248 if (arm_feature(env, ARM_FEATURE_EL3)) {
6249 rw = ((env->cp15.scr_el3 & SCR_RW) == SCR_RW);
6250 } else {
6251 /* Either EL2 is the highest EL (and so the EL2 register width
6252 * is given by is64); or there is no EL2 or EL3, in which case
6253 * the value of 'rw' does not affect the table lookup anyway.
6254 */
6255 rw = is64;
6256 }
0eeb17d6
GB
6257
6258 switch (excp_idx) {
6259 case EXCP_IRQ:
6260 scr = ((env->cp15.scr_el3 & SCR_IRQ) == SCR_IRQ);
6261 hcr = ((env->cp15.hcr_el2 & HCR_IMO) == HCR_IMO);
6262 break;
6263 case EXCP_FIQ:
6264 scr = ((env->cp15.scr_el3 & SCR_FIQ) == SCR_FIQ);
6265 hcr = ((env->cp15.hcr_el2 & HCR_FMO) == HCR_FMO);
6266 break;
6267 default:
6268 scr = ((env->cp15.scr_el3 & SCR_EA) == SCR_EA);
6269 hcr = ((env->cp15.hcr_el2 & HCR_AMO) == HCR_AMO);
6270 break;
6271 };
6272
6273 /* If HCR.TGE is set then HCR is treated as being 1 */
6274 hcr |= ((env->cp15.hcr_el2 & HCR_TGE) == HCR_TGE);
6275
6276 /* Perform a table-lookup for the target EL given the current state */
6277 target_el = target_el_table[is64][scr][rw][hcr][secure][cur_el];
6278
6279 assert(target_el > 0);
6280
6281 return target_el;
6282}
6283
fd592d89
PM
6284static bool v7m_stack_write(ARMCPU *cpu, uint32_t addr, uint32_t value,
6285 ARMMMUIdx mmu_idx, bool ignfault)
9ee6e8bb 6286{
fd592d89
PM
6287 CPUState *cs = CPU(cpu);
6288 CPUARMState *env = &cpu->env;
6289 MemTxAttrs attrs = {};
6290 MemTxResult txres;
6291 target_ulong page_size;
6292 hwaddr physaddr;
6293 int prot;
6294 ARMMMUFaultInfo fi;
6295 bool secure = mmu_idx & ARM_MMU_IDX_M_S;
6296 int exc;
6297 bool exc_secure;
6298
6299 if (get_phys_addr(env, addr, MMU_DATA_STORE, mmu_idx, &physaddr,
6300 &attrs, &prot, &page_size, &fi, NULL)) {
6301 /* MPU/SAU lookup failed */
6302 if (fi.type == ARMFault_QEMU_SFault) {
6303 qemu_log_mask(CPU_LOG_INT,
6304 "...SecureFault with SFSR.AUVIOL during stacking\n");
6305 env->v7m.sfsr |= R_V7M_SFSR_AUVIOL_MASK | R_V7M_SFSR_SFARVALID_MASK;
6306 env->v7m.sfar = addr;
6307 exc = ARMV7M_EXCP_SECURE;
6308 exc_secure = false;
6309 } else {
6310 qemu_log_mask(CPU_LOG_INT, "...MemManageFault with CFSR.MSTKERR\n");
6311 env->v7m.cfsr[secure] |= R_V7M_CFSR_MSTKERR_MASK;
6312 exc = ARMV7M_EXCP_MEM;
6313 exc_secure = secure;
6314 }
6315 goto pend_fault;
6316 }
6317 address_space_stl_le(arm_addressspace(cs, attrs), physaddr, value,
6318 attrs, &txres);
6319 if (txres != MEMTX_OK) {
6320 /* BusFault trying to write the data */
6321 qemu_log_mask(CPU_LOG_INT, "...BusFault with BFSR.STKERR\n");
6322 env->v7m.cfsr[M_REG_NS] |= R_V7M_CFSR_STKERR_MASK;
6323 exc = ARMV7M_EXCP_BUS;
6324 exc_secure = false;
6325 goto pend_fault;
6326 }
6327 return true;
70d74660 6328
fd592d89
PM
6329pend_fault:
6330 /* By pending the exception at this point we are making
6331 * the IMPDEF choice "overridden exceptions pended" (see the
6332 * MergeExcInfo() pseudocode). The other choice would be to not
6333 * pend them now and then make a choice about which to throw away
6334 * later if we have two derived exceptions.
6335 * The only case when we must not pend the exception but instead
6336 * throw it away is if we are doing the push of the callee registers
6337 * and we've already generated a derived exception. Even in this
6338 * case we will still update the fault status registers.
6339 */
6340 if (!ignfault) {
6341 armv7m_nvic_set_pending_derived(env->nvic, exc, exc_secure);
6342 }
6343 return false;
9ee6e8bb
PB
6344}
6345
95695eff
PM
6346static bool v7m_stack_read(ARMCPU *cpu, uint32_t *dest, uint32_t addr,
6347 ARMMMUIdx mmu_idx)
6348{
6349 CPUState *cs = CPU(cpu);
6350 CPUARMState *env = &cpu->env;
6351 MemTxAttrs attrs = {};
6352 MemTxResult txres;
6353 target_ulong page_size;
6354 hwaddr physaddr;
6355 int prot;
6356 ARMMMUFaultInfo fi;
6357 bool secure = mmu_idx & ARM_MMU_IDX_M_S;
6358 int exc;
6359 bool exc_secure;
6360 uint32_t value;
6361
6362 if (get_phys_addr(env, addr, MMU_DATA_LOAD, mmu_idx, &physaddr,
6363 &attrs, &prot, &page_size, &fi, NULL)) {
6364 /* MPU/SAU lookup failed */
6365 if (fi.type == ARMFault_QEMU_SFault) {
6366 qemu_log_mask(CPU_LOG_INT,
6367 "...SecureFault with SFSR.AUVIOL during unstack\n");
6368 env->v7m.sfsr |= R_V7M_SFSR_AUVIOL_MASK | R_V7M_SFSR_SFARVALID_MASK;
6369 env->v7m.sfar = addr;
6370 exc = ARMV7M_EXCP_SECURE;
6371 exc_secure = false;
6372 } else {
6373 qemu_log_mask(CPU_LOG_INT,
6374 "...MemManageFault with CFSR.MUNSTKERR\n");
6375 env->v7m.cfsr[secure] |= R_V7M_CFSR_MUNSTKERR_MASK;
6376 exc = ARMV7M_EXCP_MEM;
6377 exc_secure = secure;
6378 }
6379 goto pend_fault;
6380 }
6381
6382 value = address_space_ldl(arm_addressspace(cs, attrs), physaddr,
6383 attrs, &txres);
6384 if (txres != MEMTX_OK) {
6385 /* BusFault trying to read the data */
6386 qemu_log_mask(CPU_LOG_INT, "...BusFault with BFSR.UNSTKERR\n");
6387 env->v7m.cfsr[M_REG_NS] |= R_V7M_CFSR_UNSTKERR_MASK;
6388 exc = ARMV7M_EXCP_BUS;
6389 exc_secure = false;
6390 goto pend_fault;
6391 }
6392
6393 *dest = value;
6394 return true;
6395
6396pend_fault:
6397 /* By pending the exception at this point we are making
6398 * the IMPDEF choice "overridden exceptions pended" (see the
6399 * MergeExcInfo() pseudocode). The other choice would be to not
6400 * pend them now and then make a choice about which to throw away
6401 * later if we have two derived exceptions.
6402 */
6403 armv7m_nvic_set_pending(env->nvic, exc, exc_secure);
6404 return false;
6405}
6406
fb602cb7
PM
6407/* Return true if we're using the process stack pointer (not the MSP) */
6408static bool v7m_using_psp(CPUARMState *env)
6409{
6410 /* Handler mode always uses the main stack; for thread mode
6411 * the CONTROL.SPSEL bit determines the answer.
6412 * Note that in v7M it is not possible to be in Handler mode with
6413 * CONTROL.SPSEL non-zero, but in v8M it is, so we must check both.
6414 */
6415 return !arm_v7m_is_handler_mode(env) &&
6416 env->v7m.control[env->v7m.secure] & R_V7M_CONTROL_SPSEL_MASK;
6417}
6418
3f0cddee
PM
6419/* Write to v7M CONTROL.SPSEL bit for the specified security bank.
6420 * This may change the current stack pointer between Main and Process
6421 * stack pointers if it is done for the CONTROL register for the current
6422 * security state.
de2db7ec 6423 */
3f0cddee
PM
6424static void write_v7m_control_spsel_for_secstate(CPUARMState *env,
6425 bool new_spsel,
6426 bool secstate)
9ee6e8bb 6427{
3f0cddee 6428 bool old_is_psp = v7m_using_psp(env);
de2db7ec 6429
3f0cddee
PM
6430 env->v7m.control[secstate] =
6431 deposit32(env->v7m.control[secstate],
de2db7ec
PM
6432 R_V7M_CONTROL_SPSEL_SHIFT,
6433 R_V7M_CONTROL_SPSEL_LENGTH, new_spsel);
6434
3f0cddee
PM
6435 if (secstate == env->v7m.secure) {
6436 bool new_is_psp = v7m_using_psp(env);
6437 uint32_t tmp;
abc24d86 6438
3f0cddee
PM
6439 if (old_is_psp != new_is_psp) {
6440 tmp = env->v7m.other_sp;
6441 env->v7m.other_sp = env->regs[13];
6442 env->regs[13] = tmp;
6443 }
de2db7ec
PM
6444 }
6445}
6446
3f0cddee
PM
6447/* Write to v7M CONTROL.SPSEL bit. This may change the current
6448 * stack pointer between Main and Process stack pointers.
6449 */
6450static void write_v7m_control_spsel(CPUARMState *env, bool new_spsel)
6451{
6452 write_v7m_control_spsel_for_secstate(env, new_spsel, env->v7m.secure);
6453}
6454
de2db7ec
PM
6455void write_v7m_exception(CPUARMState *env, uint32_t new_exc)
6456{
6457 /* Write a new value to v7m.exception, thus transitioning into or out
6458 * of Handler mode; this may result in a change of active stack pointer.
6459 */
6460 bool new_is_psp, old_is_psp = v7m_using_psp(env);
6461 uint32_t tmp;
abc24d86 6462
de2db7ec
PM
6463 env->v7m.exception = new_exc;
6464
6465 new_is_psp = v7m_using_psp(env);
6466
6467 if (old_is_psp != new_is_psp) {
6468 tmp = env->v7m.other_sp;
6469 env->v7m.other_sp = env->regs[13];
6470 env->regs[13] = tmp;
9ee6e8bb
PB
6471 }
6472}
6473
fb602cb7
PM
6474/* Switch M profile security state between NS and S */
6475static void switch_v7m_security_state(CPUARMState *env, bool new_secstate)
6476{
6477 uint32_t new_ss_msp, new_ss_psp;
6478
6479 if (env->v7m.secure == new_secstate) {
6480 return;
6481 }
6482
6483 /* All the banked state is accessed by looking at env->v7m.secure
6484 * except for the stack pointer; rearrange the SP appropriately.
6485 */
6486 new_ss_msp = env->v7m.other_ss_msp;
6487 new_ss_psp = env->v7m.other_ss_psp;
6488
6489 if (v7m_using_psp(env)) {
6490 env->v7m.other_ss_psp = env->regs[13];
6491 env->v7m.other_ss_msp = env->v7m.other_sp;
6492 } else {
6493 env->v7m.other_ss_msp = env->regs[13];
6494 env->v7m.other_ss_psp = env->v7m.other_sp;
6495 }
6496
6497 env->v7m.secure = new_secstate;
6498
6499 if (v7m_using_psp(env)) {
6500 env->regs[13] = new_ss_psp;
6501 env->v7m.other_sp = new_ss_msp;
6502 } else {
6503 env->regs[13] = new_ss_msp;
6504 env->v7m.other_sp = new_ss_psp;
6505 }
6506}
6507
6508void HELPER(v7m_bxns)(CPUARMState *env, uint32_t dest)
6509{
6510 /* Handle v7M BXNS:
6511 * - if the return value is a magic value, do exception return (like BX)
6512 * - otherwise bit 0 of the return value is the target security state
6513 */
d02a8698
PM
6514 uint32_t min_magic;
6515
6516 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
6517 /* Covers FNC_RETURN and EXC_RETURN magic */
6518 min_magic = FNC_RETURN_MIN_MAGIC;
6519 } else {
6520 /* EXC_RETURN magic only */
6521 min_magic = EXC_RETURN_MIN_MAGIC;
6522 }
6523
6524 if (dest >= min_magic) {
fb602cb7
PM
6525 /* This is an exception return magic value; put it where
6526 * do_v7m_exception_exit() expects and raise EXCEPTION_EXIT.
6527 * Note that if we ever add gen_ss_advance() singlestep support to
6528 * M profile this should count as an "instruction execution complete"
6529 * event (compare gen_bx_excret_final_code()).
6530 */
6531 env->regs[15] = dest & ~1;
6532 env->thumb = dest & 1;
6533 HELPER(exception_internal)(env, EXCP_EXCEPTION_EXIT);
6534 /* notreached */
6535 }
6536
6537 /* translate.c should have made BXNS UNDEF unless we're secure */
6538 assert(env->v7m.secure);
6539
6540 switch_v7m_security_state(env, dest & 1);
6541 env->thumb = 1;
6542 env->regs[15] = dest & ~1;
6543}
6544
3e3fa230
PM
6545void HELPER(v7m_blxns)(CPUARMState *env, uint32_t dest)
6546{
6547 /* Handle v7M BLXNS:
6548 * - bit 0 of the destination address is the target security state
6549 */
6550
6551 /* At this point regs[15] is the address just after the BLXNS */
6552 uint32_t nextinst = env->regs[15] | 1;
6553 uint32_t sp = env->regs[13] - 8;
6554 uint32_t saved_psr;
6555
6556 /* translate.c will have made BLXNS UNDEF unless we're secure */
6557 assert(env->v7m.secure);
6558
6559 if (dest & 1) {
6560 /* target is Secure, so this is just a normal BLX,
6561 * except that the low bit doesn't indicate Thumb/not.
6562 */
6563 env->regs[14] = nextinst;
6564 env->thumb = 1;
6565 env->regs[15] = dest & ~1;
6566 return;
6567 }
6568
6569 /* Target is non-secure: first push a stack frame */
6570 if (!QEMU_IS_ALIGNED(sp, 8)) {
6571 qemu_log_mask(LOG_GUEST_ERROR,
6572 "BLXNS with misaligned SP is UNPREDICTABLE\n");
6573 }
6574
6575 saved_psr = env->v7m.exception;
6576 if (env->v7m.control[M_REG_S] & R_V7M_CONTROL_SFPA_MASK) {
6577 saved_psr |= XPSR_SFPA;
6578 }
6579
6580 /* Note that these stores can throw exceptions on MPU faults */
6581 cpu_stl_data(env, sp, nextinst);
6582 cpu_stl_data(env, sp + 4, saved_psr);
6583
6584 env->regs[13] = sp;
6585 env->regs[14] = 0xfeffffff;
6586 if (arm_v7m_is_handler_mode(env)) {
6587 /* Write a dummy value to IPSR, to avoid leaking the current secure
6588 * exception number to non-secure code. This is guaranteed not
6589 * to cause write_v7m_exception() to actually change stacks.
6590 */
6591 write_v7m_exception(env, 1);
6592 }
6593 switch_v7m_security_state(env, 0);
6594 env->thumb = 1;
6595 env->regs[15] = dest;
6596}
6597
5b522399
PM
6598static uint32_t *get_v7m_sp_ptr(CPUARMState *env, bool secure, bool threadmode,
6599 bool spsel)
6600{
6601 /* Return a pointer to the location where we currently store the
6602 * stack pointer for the requested security state and thread mode.
6603 * This pointer will become invalid if the CPU state is updated
6604 * such that the stack pointers are switched around (eg changing
6605 * the SPSEL control bit).
6606 * Compare the v8M ARM ARM pseudocode LookUpSP_with_security_mode().
6607 * Unlike that pseudocode, we require the caller to pass us in the
6608 * SPSEL control bit value; this is because we also use this
6609 * function in handling of pushing of the callee-saves registers
6610 * part of the v8M stack frame (pseudocode PushCalleeStack()),
6611 * and in the tailchain codepath the SPSEL bit comes from the exception
6612 * return magic LR value from the previous exception. The pseudocode
6613 * opencodes the stack-selection in PushCalleeStack(), but we prefer
6614 * to make this utility function generic enough to do the job.
6615 */
6616 bool want_psp = threadmode && spsel;
6617
6618 if (secure == env->v7m.secure) {
de2db7ec
PM
6619 if (want_psp == v7m_using_psp(env)) {
6620 return &env->regs[13];
6621 } else {
6622 return &env->v7m.other_sp;
6623 }
5b522399
PM
6624 } else {
6625 if (want_psp) {
6626 return &env->v7m.other_ss_psp;
6627 } else {
6628 return &env->v7m.other_ss_msp;
6629 }
6630 }
6631}
6632
600c33f2
PM
6633static bool arm_v7m_load_vector(ARMCPU *cpu, int exc, bool targets_secure,
6634 uint32_t *pvec)
39ae2474
PM
6635{
6636 CPUState *cs = CPU(cpu);
6637 CPUARMState *env = &cpu->env;
6638 MemTxResult result;
600c33f2
PM
6639 uint32_t addr = env->v7m.vecbase[targets_secure] + exc * 4;
6640 uint32_t vector_entry;
6641 MemTxAttrs attrs = {};
6642 ARMMMUIdx mmu_idx;
6643 bool exc_secure;
6644
6645 mmu_idx = arm_v7m_mmu_idx_for_secstate_and_priv(env, targets_secure, true);
39ae2474 6646
600c33f2
PM
6647 /* We don't do a get_phys_addr() here because the rules for vector
6648 * loads are special: they always use the default memory map, and
6649 * the default memory map permits reads from all addresses.
6650 * Since there's no easy way to pass through to pmsav8_mpu_lookup()
6651 * that we want this special case which would always say "yes",
6652 * we just do the SAU lookup here followed by a direct physical load.
6653 */
6654 attrs.secure = targets_secure;
6655 attrs.user = false;
6656
6657 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
6658 V8M_SAttributes sattrs = {};
6659
6660 v8m_security_lookup(env, addr, MMU_DATA_LOAD, mmu_idx, &sattrs);
6661 if (sattrs.ns) {
6662 attrs.secure = false;
6663 } else if (!targets_secure) {
6664 /* NS access to S memory */
6665 goto load_fail;
6666 }
6667 }
6668
6669 vector_entry = address_space_ldl(arm_addressspace(cs, attrs), addr,
6670 attrs, &result);
39ae2474 6671 if (result != MEMTX_OK) {
600c33f2 6672 goto load_fail;
39ae2474 6673 }
600c33f2
PM
6674 *pvec = vector_entry;
6675 return true;
6676
6677load_fail:
6678 /* All vector table fetch fails are reported as HardFault, with
6679 * HFSR.VECTTBL and .FORCED set. (FORCED is set because
6680 * technically the underlying exception is a MemManage or BusFault
6681 * that is escalated to HardFault.) This is a terminal exception,
6682 * so we will either take the HardFault immediately or else enter
6683 * lockup (the latter case is handled in armv7m_nvic_set_pending_derived()).
6684 */
6685 exc_secure = targets_secure ||
6686 !(cpu->env.v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK);
6687 env->v7m.hfsr |= R_V7M_HFSR_VECTTBL_MASK | R_V7M_HFSR_FORCED_MASK;
6688 armv7m_nvic_set_pending_derived(env->nvic, ARMV7M_EXCP_HARD, exc_secure);
6689 return false;
39ae2474
PM
6690}
6691
65b4234f 6692static bool v7m_push_callee_stack(ARMCPU *cpu, uint32_t lr, bool dotailchain,
0094ca70 6693 bool ignore_faults)
d3392718
PM
6694{
6695 /* For v8M, push the callee-saves register part of the stack frame.
6696 * Compare the v8M pseudocode PushCalleeStack().
6697 * In the tailchaining case this may not be the current stack.
6698 */
6699 CPUARMState *env = &cpu->env;
d3392718
PM
6700 uint32_t *frame_sp_p;
6701 uint32_t frameptr;
65b4234f
PM
6702 ARMMMUIdx mmu_idx;
6703 bool stacked_ok;
d3392718
PM
6704
6705 if (dotailchain) {
65b4234f
PM
6706 bool mode = lr & R_V7M_EXCRET_MODE_MASK;
6707 bool priv = !(env->v7m.control[M_REG_S] & R_V7M_CONTROL_NPRIV_MASK) ||
6708 !mode;
6709
6710 mmu_idx = arm_v7m_mmu_idx_for_secstate_and_priv(env, M_REG_S, priv);
6711 frame_sp_p = get_v7m_sp_ptr(env, M_REG_S, mode,
d3392718
PM
6712 lr & R_V7M_EXCRET_SPSEL_MASK);
6713 } else {
65b4234f 6714 mmu_idx = core_to_arm_mmu_idx(env, cpu_mmu_index(env, false));
d3392718
PM
6715 frame_sp_p = &env->regs[13];
6716 }
6717
6718 frameptr = *frame_sp_p - 0x28;
6719
65b4234f
PM
6720 /* Write as much of the stack frame as we can. A write failure may
6721 * cause us to pend a derived exception.
6722 */
6723 stacked_ok =
6724 v7m_stack_write(cpu, frameptr, 0xfefa125b, mmu_idx, ignore_faults) &&
6725 v7m_stack_write(cpu, frameptr + 0x8, env->regs[4], mmu_idx,
6726 ignore_faults) &&
6727 v7m_stack_write(cpu, frameptr + 0xc, env->regs[5], mmu_idx,
6728 ignore_faults) &&
6729 v7m_stack_write(cpu, frameptr + 0x10, env->regs[6], mmu_idx,
6730 ignore_faults) &&
6731 v7m_stack_write(cpu, frameptr + 0x14, env->regs[7], mmu_idx,
6732 ignore_faults) &&
6733 v7m_stack_write(cpu, frameptr + 0x18, env->regs[8], mmu_idx,
6734 ignore_faults) &&
6735 v7m_stack_write(cpu, frameptr + 0x1c, env->regs[9], mmu_idx,
6736 ignore_faults) &&
6737 v7m_stack_write(cpu, frameptr + 0x20, env->regs[10], mmu_idx,
6738 ignore_faults) &&
6739 v7m_stack_write(cpu, frameptr + 0x24, env->regs[11], mmu_idx,
6740 ignore_faults);
6741
6742 /* Update SP regardless of whether any of the stack accesses failed.
6743 * When we implement v8M stack limit checking then this attempt to
6744 * update SP might also fail and result in a derived exception.
6745 */
d3392718 6746 *frame_sp_p = frameptr;
65b4234f
PM
6747
6748 return !stacked_ok;
d3392718
PM
6749}
6750
0094ca70
PM
6751static void v7m_exception_taken(ARMCPU *cpu, uint32_t lr, bool dotailchain,
6752 bool ignore_stackfaults)
39ae2474
PM
6753{
6754 /* Do the "take the exception" parts of exception entry,
6755 * but not the pushing of state to the stack. This is
6756 * similar to the pseudocode ExceptionTaken() function.
6757 */
6758 CPUARMState *env = &cpu->env;
6759 uint32_t addr;
d3392718 6760 bool targets_secure;
6c948518 6761 int exc;
65b4234f 6762 bool push_failed = false;
d3392718 6763
6c948518 6764 armv7m_nvic_get_pending_irq_info(env->nvic, &exc, &targets_secure);
d3392718
PM
6765
6766 if (arm_feature(env, ARM_FEATURE_V8)) {
6767 if (arm_feature(env, ARM_FEATURE_M_SECURITY) &&
6768 (lr & R_V7M_EXCRET_S_MASK)) {
6769 /* The background code (the owner of the registers in the
6770 * exception frame) is Secure. This means it may either already
6771 * have or now needs to push callee-saves registers.
6772 */
6773 if (targets_secure) {
6774 if (dotailchain && !(lr & R_V7M_EXCRET_ES_MASK)) {
6775 /* We took an exception from Secure to NonSecure
6776 * (which means the callee-saved registers got stacked)
6777 * and are now tailchaining to a Secure exception.
6778 * Clear DCRS so eventual return from this Secure
6779 * exception unstacks the callee-saved registers.
6780 */
6781 lr &= ~R_V7M_EXCRET_DCRS_MASK;
6782 }
6783 } else {
6784 /* We're going to a non-secure exception; push the
6785 * callee-saves registers to the stack now, if they're
6786 * not already saved.
6787 */
6788 if (lr & R_V7M_EXCRET_DCRS_MASK &&
6789 !(dotailchain && (lr & R_V7M_EXCRET_ES_MASK))) {
65b4234f
PM
6790 push_failed = v7m_push_callee_stack(cpu, lr, dotailchain,
6791 ignore_stackfaults);
d3392718
PM
6792 }
6793 lr |= R_V7M_EXCRET_DCRS_MASK;
6794 }
6795 }
6796
6797 lr &= ~R_V7M_EXCRET_ES_MASK;
6798 if (targets_secure || !arm_feature(env, ARM_FEATURE_M_SECURITY)) {
6799 lr |= R_V7M_EXCRET_ES_MASK;
6800 }
6801 lr &= ~R_V7M_EXCRET_SPSEL_MASK;
6802 if (env->v7m.control[targets_secure] & R_V7M_CONTROL_SPSEL_MASK) {
6803 lr |= R_V7M_EXCRET_SPSEL_MASK;
6804 }
6805
6806 /* Clear registers if necessary to prevent non-secure exception
6807 * code being able to see register values from secure code.
6808 * Where register values become architecturally UNKNOWN we leave
6809 * them with their previous values.
6810 */
6811 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
6812 if (!targets_secure) {
6813 /* Always clear the caller-saved registers (they have been
6814 * pushed to the stack earlier in v7m_push_stack()).
6815 * Clear callee-saved registers if the background code is
6816 * Secure (in which case these regs were saved in
6817 * v7m_push_callee_stack()).
6818 */
6819 int i;
6820
6821 for (i = 0; i < 13; i++) {
6822 /* r4..r11 are callee-saves, zero only if EXCRET.S == 1 */
6823 if (i < 4 || i > 11 || (lr & R_V7M_EXCRET_S_MASK)) {
6824 env->regs[i] = 0;
6825 }
6826 }
6827 /* Clear EAPSR */
6828 xpsr_write(env, 0, XPSR_NZCV | XPSR_Q | XPSR_GE | XPSR_IT);
6829 }
6830 }
6831 }
39ae2474 6832
65b4234f
PM
6833 if (push_failed && !ignore_stackfaults) {
6834 /* Derived exception on callee-saves register stacking:
6835 * we might now want to take a different exception which
6836 * targets a different security state, so try again from the top.
6837 */
6838 v7m_exception_taken(cpu, lr, true, true);
6839 return;
6840 }
6841
600c33f2
PM
6842 if (!arm_v7m_load_vector(cpu, exc, targets_secure, &addr)) {
6843 /* Vector load failed: derived exception */
6844 v7m_exception_taken(cpu, lr, true, true);
6845 return;
6846 }
6c948518
PM
6847
6848 /* Now we've done everything that might cause a derived exception
6849 * we can go ahead and activate whichever exception we're going to
6850 * take (which might now be the derived exception).
6851 */
6852 armv7m_nvic_acknowledge_irq(env->nvic);
6853
d3392718
PM
6854 /* Switch to target security state -- must do this before writing SPSEL */
6855 switch_v7m_security_state(env, targets_secure);
de2db7ec 6856 write_v7m_control_spsel(env, 0);
dc3c4c14 6857 arm_clear_exclusive(env);
39ae2474
PM
6858 /* Clear IT bits */
6859 env->condexec_bits = 0;
6860 env->regs[14] = lr;
39ae2474
PM
6861 env->regs[15] = addr & 0xfffffffe;
6862 env->thumb = addr & 1;
6863}
6864
0094ca70 6865static bool v7m_push_stack(ARMCPU *cpu)
39ae2474
PM
6866{
6867 /* Do the "set up stack frame" part of exception entry,
6868 * similar to pseudocode PushStack().
0094ca70
PM
6869 * Return true if we generate a derived exception (and so
6870 * should ignore further stack faults trying to process
6871 * that derived exception.)
39ae2474 6872 */
fd592d89 6873 bool stacked_ok;
39ae2474
PM
6874 CPUARMState *env = &cpu->env;
6875 uint32_t xpsr = xpsr_read(env);
fd592d89
PM
6876 uint32_t frameptr = env->regs[13];
6877 ARMMMUIdx mmu_idx = core_to_arm_mmu_idx(env, cpu_mmu_index(env, false));
39ae2474
PM
6878
6879 /* Align stack pointer if the guest wants that */
fd592d89 6880 if ((frameptr & 4) &&
9d40cd8a 6881 (env->v7m.ccr[env->v7m.secure] & R_V7M_CCR_STKALIGN_MASK)) {
fd592d89 6882 frameptr -= 4;
987ab45e 6883 xpsr |= XPSR_SPREALIGN;
39ae2474 6884 }
0094ca70 6885
fd592d89
PM
6886 frameptr -= 0x20;
6887
6888 /* Write as much of the stack frame as we can. If we fail a stack
6889 * write this will result in a derived exception being pended
6890 * (which may be taken in preference to the one we started with
6891 * if it has higher priority).
6892 */
6893 stacked_ok =
6894 v7m_stack_write(cpu, frameptr, env->regs[0], mmu_idx, false) &&
6895 v7m_stack_write(cpu, frameptr + 4, env->regs[1], mmu_idx, false) &&
6896 v7m_stack_write(cpu, frameptr + 8, env->regs[2], mmu_idx, false) &&
6897 v7m_stack_write(cpu, frameptr + 12, env->regs[3], mmu_idx, false) &&
6898 v7m_stack_write(cpu, frameptr + 16, env->regs[12], mmu_idx, false) &&
6899 v7m_stack_write(cpu, frameptr + 20, env->regs[14], mmu_idx, false) &&
6900 v7m_stack_write(cpu, frameptr + 24, env->regs[15], mmu_idx, false) &&
6901 v7m_stack_write(cpu, frameptr + 28, xpsr, mmu_idx, false);
6902
6903 /* Update SP regardless of whether any of the stack accesses failed.
6904 * When we implement v8M stack limit checking then this attempt to
6905 * update SP might also fail and result in a derived exception.
6906 */
6907 env->regs[13] = frameptr;
6908
6909 return !stacked_ok;
39ae2474
PM
6910}
6911
aa488fe3 6912static void do_v7m_exception_exit(ARMCPU *cpu)
9ee6e8bb 6913{
aa488fe3 6914 CPUARMState *env = &cpu->env;
5b522399 6915 CPUState *cs = CPU(cpu);
351e527a 6916 uint32_t excret;
9ee6e8bb 6917 uint32_t xpsr;
aa488fe3 6918 bool ufault = false;
bfb2eb52
PM
6919 bool sfault = false;
6920 bool return_to_sp_process;
6921 bool return_to_handler;
aa488fe3 6922 bool rettobase = false;
5cb18069 6923 bool exc_secure = false;
5b522399 6924 bool return_to_secure;
aa488fe3 6925
d02a8698
PM
6926 /* If we're not in Handler mode then jumps to magic exception-exit
6927 * addresses don't have magic behaviour. However for the v8M
6928 * security extensions the magic secure-function-return has to
6929 * work in thread mode too, so to avoid doing an extra check in
6930 * the generated code we allow exception-exit magic to also cause the
6931 * internal exception and bring us here in thread mode. Correct code
6932 * will never try to do this (the following insn fetch will always
6933 * fault) so we the overhead of having taken an unnecessary exception
6934 * doesn't matter.
aa488fe3 6935 */
d02a8698
PM
6936 if (!arm_v7m_is_handler_mode(env)) {
6937 return;
6938 }
aa488fe3
PM
6939
6940 /* In the spec pseudocode ExceptionReturn() is called directly
6941 * from BXWritePC() and gets the full target PC value including
6942 * bit zero. In QEMU's implementation we treat it as a normal
6943 * jump-to-register (which is then caught later on), and so split
6944 * the target value up between env->regs[15] and env->thumb in
6945 * gen_bx(). Reconstitute it.
6946 */
351e527a 6947 excret = env->regs[15];
aa488fe3 6948 if (env->thumb) {
351e527a 6949 excret |= 1;
aa488fe3
PM
6950 }
6951
6952 qemu_log_mask(CPU_LOG_INT, "Exception return: magic PC %" PRIx32
6953 " previous exception %d\n",
351e527a 6954 excret, env->v7m.exception);
aa488fe3 6955
351e527a 6956 if ((excret & R_V7M_EXCRET_RES1_MASK) != R_V7M_EXCRET_RES1_MASK) {
aa488fe3 6957 qemu_log_mask(LOG_GUEST_ERROR, "M profile: zero high bits in exception "
351e527a
PM
6958 "exit PC value 0x%" PRIx32 " are UNPREDICTABLE\n",
6959 excret);
aa488fe3
PM
6960 }
6961
bfb2eb52
PM
6962 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
6963 /* EXC_RETURN.ES validation check (R_SMFL). We must do this before
6964 * we pick which FAULTMASK to clear.
6965 */
6966 if (!env->v7m.secure &&
6967 ((excret & R_V7M_EXCRET_ES_MASK) ||
6968 !(excret & R_V7M_EXCRET_DCRS_MASK))) {
6969 sfault = 1;
6970 /* For all other purposes, treat ES as 0 (R_HXSR) */
6971 excret &= ~R_V7M_EXCRET_ES_MASK;
6972 }
6973 }
6974
a20ee600 6975 if (env->v7m.exception != ARMV7M_EXCP_NMI) {
42a6686b
PM
6976 /* Auto-clear FAULTMASK on return from other than NMI.
6977 * If the security extension is implemented then this only
6978 * happens if the raw execution priority is >= 0; the
6979 * value of the ES bit in the exception return value indicates
6980 * which security state's faultmask to clear. (v8M ARM ARM R_KBNF.)
6981 */
6982 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
5cb18069 6983 exc_secure = excret & R_V7M_EXCRET_ES_MASK;
42a6686b 6984 if (armv7m_nvic_raw_execution_priority(env->nvic) >= 0) {
5cb18069 6985 env->v7m.faultmask[exc_secure] = 0;
42a6686b
PM
6986 }
6987 } else {
6988 env->v7m.faultmask[M_REG_NS] = 0;
6989 }
a20ee600 6990 }
aa488fe3 6991
5cb18069
PM
6992 switch (armv7m_nvic_complete_irq(env->nvic, env->v7m.exception,
6993 exc_secure)) {
aa488fe3
PM
6994 case -1:
6995 /* attempt to exit an exception that isn't active */
6996 ufault = true;
6997 break;
6998 case 0:
6999 /* still an irq active now */
7000 break;
7001 case 1:
7002 /* we returned to base exception level, no nesting.
7003 * (In the pseudocode this is written using "NestedActivation != 1"
7004 * where we have 'rettobase == false'.)
7005 */
7006 rettobase = true;
7007 break;
7008 default:
7009 g_assert_not_reached();
7010 }
7011
bfb2eb52
PM
7012 return_to_handler = !(excret & R_V7M_EXCRET_MODE_MASK);
7013 return_to_sp_process = excret & R_V7M_EXCRET_SPSEL_MASK;
5b522399
PM
7014 return_to_secure = arm_feature(env, ARM_FEATURE_M_SECURITY) &&
7015 (excret & R_V7M_EXCRET_S_MASK);
7016
bfb2eb52
PM
7017 if (arm_feature(env, ARM_FEATURE_V8)) {
7018 if (!arm_feature(env, ARM_FEATURE_M_SECURITY)) {
7019 /* UNPREDICTABLE if S == 1 or DCRS == 0 or ES == 1 (R_XLCP);
7020 * we choose to take the UsageFault.
7021 */
7022 if ((excret & R_V7M_EXCRET_S_MASK) ||
7023 (excret & R_V7M_EXCRET_ES_MASK) ||
7024 !(excret & R_V7M_EXCRET_DCRS_MASK)) {
7025 ufault = true;
7026 }
7027 }
7028 if (excret & R_V7M_EXCRET_RES0_MASK) {
aa488fe3
PM
7029 ufault = true;
7030 }
bfb2eb52
PM
7031 } else {
7032 /* For v7M we only recognize certain combinations of the low bits */
7033 switch (excret & 0xf) {
7034 case 1: /* Return to Handler */
7035 break;
7036 case 13: /* Return to Thread using Process stack */
7037 case 9: /* Return to Thread using Main stack */
7038 /* We only need to check NONBASETHRDENA for v7M, because in
7039 * v8M this bit does not exist (it is RES1).
7040 */
7041 if (!rettobase &&
7042 !(env->v7m.ccr[env->v7m.secure] &
7043 R_V7M_CCR_NONBASETHRDENA_MASK)) {
7044 ufault = true;
7045 }
7046 break;
7047 default:
7048 ufault = true;
7049 }
7050 }
7051
7052 if (sfault) {
7053 env->v7m.sfsr |= R_V7M_SFSR_INVER_MASK;
7054 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SECURE, false);
0094ca70 7055 v7m_exception_taken(cpu, excret, true, false);
bfb2eb52
PM
7056 qemu_log_mask(CPU_LOG_INT, "...taking SecureFault on existing "
7057 "stackframe: failed EXC_RETURN.ES validity check\n");
7058 return;
aa488fe3
PM
7059 }
7060
7061 if (ufault) {
7062 /* Bad exception return: instead of popping the exception
7063 * stack, directly take a usage fault on the current stack.
7064 */
334e8dad 7065 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_INVPC_MASK;
2fb50a33 7066 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, env->v7m.secure);
0094ca70 7067 v7m_exception_taken(cpu, excret, true, false);
aa488fe3
PM
7068 qemu_log_mask(CPU_LOG_INT, "...taking UsageFault on existing "
7069 "stackframe: failed exception return integrity check\n");
7070 return;
a20ee600 7071 }
9ee6e8bb 7072
de2db7ec
PM
7073 /* Set CONTROL.SPSEL from excret.SPSEL. Since we're still in
7074 * Handler mode (and will be until we write the new XPSR.Interrupt
7075 * field) this does not switch around the current stack pointer.
5b522399 7076 */
3f0cddee 7077 write_v7m_control_spsel_for_secstate(env, return_to_sp_process, exc_secure);
5b522399 7078
3919e60b
PM
7079 switch_v7m_security_state(env, return_to_secure);
7080
5b522399
PM
7081 {
7082 /* The stack pointer we should be reading the exception frame from
7083 * depends on bits in the magic exception return type value (and
7084 * for v8M isn't necessarily the stack pointer we will eventually
7085 * end up resuming execution with). Get a pointer to the location
7086 * in the CPU state struct where the SP we need is currently being
7087 * stored; we will use and modify it in place.
7088 * We use this limited C variable scope so we don't accidentally
7089 * use 'frame_sp_p' after we do something that makes it invalid.
fcf83ab1 7090 */
5b522399
PM
7091 uint32_t *frame_sp_p = get_v7m_sp_ptr(env,
7092 return_to_secure,
7093 !return_to_handler,
7094 return_to_sp_process);
7095 uint32_t frameptr = *frame_sp_p;
95695eff
PM
7096 bool pop_ok = true;
7097 ARMMMUIdx mmu_idx;
7098
7099 mmu_idx = arm_v7m_mmu_idx_for_secstate_and_priv(env, return_to_secure,
7100 !return_to_handler);
5b522399 7101
cb484f9a
PM
7102 if (!QEMU_IS_ALIGNED(frameptr, 8) &&
7103 arm_feature(env, ARM_FEATURE_V8)) {
7104 qemu_log_mask(LOG_GUEST_ERROR,
7105 "M profile exception return with non-8-aligned SP "
7106 "for destination state is UNPREDICTABLE\n");
7107 }
7108
907bedb3
PM
7109 /* Do we need to pop callee-saved registers? */
7110 if (return_to_secure &&
7111 ((excret & R_V7M_EXCRET_ES_MASK) == 0 ||
7112 (excret & R_V7M_EXCRET_DCRS_MASK) == 0)) {
7113 uint32_t expected_sig = 0xfefa125b;
7114 uint32_t actual_sig = ldl_phys(cs->as, frameptr);
7115
7116 if (expected_sig != actual_sig) {
7117 /* Take a SecureFault on the current stack */
7118 env->v7m.sfsr |= R_V7M_SFSR_INVIS_MASK;
7119 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SECURE, false);
0094ca70 7120 v7m_exception_taken(cpu, excret, true, false);
907bedb3
PM
7121 qemu_log_mask(CPU_LOG_INT, "...taking SecureFault on existing "
7122 "stackframe: failed exception return integrity "
7123 "signature check\n");
7124 return;
7125 }
7126
95695eff
PM
7127 pop_ok =
7128 v7m_stack_read(cpu, &env->regs[4], frameptr + 0x8, mmu_idx) &&
7129 v7m_stack_read(cpu, &env->regs[4], frameptr + 0x8, mmu_idx) &&
7130 v7m_stack_read(cpu, &env->regs[5], frameptr + 0xc, mmu_idx) &&
7131 v7m_stack_read(cpu, &env->regs[6], frameptr + 0x10, mmu_idx) &&
7132 v7m_stack_read(cpu, &env->regs[7], frameptr + 0x14, mmu_idx) &&
7133 v7m_stack_read(cpu, &env->regs[8], frameptr + 0x18, mmu_idx) &&
7134 v7m_stack_read(cpu, &env->regs[9], frameptr + 0x1c, mmu_idx) &&
7135 v7m_stack_read(cpu, &env->regs[10], frameptr + 0x20, mmu_idx) &&
7136 v7m_stack_read(cpu, &env->regs[11], frameptr + 0x24, mmu_idx);
907bedb3
PM
7137
7138 frameptr += 0x28;
7139 }
7140
95695eff
PM
7141 /* Pop registers */
7142 pop_ok = pop_ok &&
7143 v7m_stack_read(cpu, &env->regs[0], frameptr, mmu_idx) &&
7144 v7m_stack_read(cpu, &env->regs[1], frameptr + 0x4, mmu_idx) &&
7145 v7m_stack_read(cpu, &env->regs[2], frameptr + 0x8, mmu_idx) &&
7146 v7m_stack_read(cpu, &env->regs[3], frameptr + 0xc, mmu_idx) &&
7147 v7m_stack_read(cpu, &env->regs[12], frameptr + 0x10, mmu_idx) &&
7148 v7m_stack_read(cpu, &env->regs[14], frameptr + 0x14, mmu_idx) &&
7149 v7m_stack_read(cpu, &env->regs[15], frameptr + 0x18, mmu_idx) &&
7150 v7m_stack_read(cpu, &xpsr, frameptr + 0x1c, mmu_idx);
7151
7152 if (!pop_ok) {
7153 /* v7m_stack_read() pended a fault, so take it (as a tail
7154 * chained exception on the same stack frame)
7155 */
7156 v7m_exception_taken(cpu, excret, true, false);
7157 return;
7158 }
4e4259d3
PM
7159
7160 /* Returning from an exception with a PC with bit 0 set is defined
7161 * behaviour on v8M (bit 0 is ignored), but for v7M it was specified
7162 * to be UNPREDICTABLE. In practice actual v7M hardware seems to ignore
7163 * the lsbit, and there are several RTOSes out there which incorrectly
7164 * assume the r15 in the stack frame should be a Thumb-style "lsbit
7165 * indicates ARM/Thumb" value, so ignore the bit on v7M as well, but
7166 * complain about the badly behaved guest.
7167 */
5b522399 7168 if (env->regs[15] & 1) {
5b522399 7169 env->regs[15] &= ~1U;
4e4259d3
PM
7170 if (!arm_feature(env, ARM_FEATURE_V8)) {
7171 qemu_log_mask(LOG_GUEST_ERROR,
7172 "M profile return from interrupt with misaligned "
7173 "PC is UNPREDICTABLE on v7M\n");
7174 }
5b522399 7175 }
4e4259d3 7176
224e0c30
PM
7177 if (arm_feature(env, ARM_FEATURE_V8)) {
7178 /* For v8M we have to check whether the xPSR exception field
7179 * matches the EXCRET value for return to handler/thread
7180 * before we commit to changing the SP and xPSR.
7181 */
7182 bool will_be_handler = (xpsr & XPSR_EXCP) != 0;
7183 if (return_to_handler != will_be_handler) {
7184 /* Take an INVPC UsageFault on the current stack.
7185 * By this point we will have switched to the security state
7186 * for the background state, so this UsageFault will target
7187 * that state.
7188 */
7189 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE,
7190 env->v7m.secure);
7191 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_INVPC_MASK;
0094ca70 7192 v7m_exception_taken(cpu, excret, true, false);
224e0c30
PM
7193 qemu_log_mask(CPU_LOG_INT, "...taking UsageFault on existing "
7194 "stackframe: failed exception return integrity "
7195 "check\n");
7196 return;
7197 }
7198 }
7199
5b522399
PM
7200 /* Commit to consuming the stack frame */
7201 frameptr += 0x20;
7202 /* Undo stack alignment (the SPREALIGN bit indicates that the original
7203 * pre-exception SP was not 8-aligned and we added a padding word to
7204 * align it, so we undo this by ORing in the bit that increases it
7205 * from the current 8-aligned value to the 8-unaligned value. (Adding 4
7206 * would work too but a logical OR is how the pseudocode specifies it.)
7207 */
7208 if (xpsr & XPSR_SPREALIGN) {
7209 frameptr |= 4;
7210 }
7211 *frame_sp_p = frameptr;
fcf83ab1 7212 }
5b522399 7213 /* This xpsr_write() will invalidate frame_sp_p as it may switch stack */
987ab45e 7214 xpsr_write(env, xpsr, ~XPSR_SPREALIGN);
aa488fe3
PM
7215
7216 /* The restored xPSR exception field will be zero if we're
7217 * resuming in Thread mode. If that doesn't match what the
351e527a 7218 * exception return excret specified then this is a UsageFault.
224e0c30 7219 * v7M requires we make this check here; v8M did it earlier.
aa488fe3 7220 */
15b3f556 7221 if (return_to_handler != arm_v7m_is_handler_mode(env)) {
224e0c30
PM
7222 /* Take an INVPC UsageFault by pushing the stack again;
7223 * we know we're v7M so this is never a Secure UsageFault.
2fb50a33 7224 */
0094ca70
PM
7225 bool ignore_stackfaults;
7226
224e0c30 7227 assert(!arm_feature(env, ARM_FEATURE_V8));
2fb50a33 7228 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, false);
334e8dad 7229 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_INVPC_MASK;
0094ca70
PM
7230 ignore_stackfaults = v7m_push_stack(cpu);
7231 v7m_exception_taken(cpu, excret, false, ignore_stackfaults);
aa488fe3
PM
7232 qemu_log_mask(CPU_LOG_INT, "...taking UsageFault on new stackframe: "
7233 "failed exception return integrity check\n");
7234 return;
7235 }
7236
7237 /* Otherwise, we have a successful exception exit. */
dc3c4c14 7238 arm_clear_exclusive(env);
aa488fe3 7239 qemu_log_mask(CPU_LOG_INT, "...successful exception return\n");
9ee6e8bb
PB
7240}
7241
d02a8698
PM
7242static bool do_v7m_function_return(ARMCPU *cpu)
7243{
7244 /* v8M security extensions magic function return.
7245 * We may either:
7246 * (1) throw an exception (longjump)
7247 * (2) return true if we successfully handled the function return
7248 * (3) return false if we failed a consistency check and have
7249 * pended a UsageFault that needs to be taken now
7250 *
7251 * At this point the magic return value is split between env->regs[15]
7252 * and env->thumb. We don't bother to reconstitute it because we don't
7253 * need it (all values are handled the same way).
7254 */
7255 CPUARMState *env = &cpu->env;
7256 uint32_t newpc, newpsr, newpsr_exc;
7257
7258 qemu_log_mask(CPU_LOG_INT, "...really v7M secure function return\n");
7259
7260 {
7261 bool threadmode, spsel;
7262 TCGMemOpIdx oi;
7263 ARMMMUIdx mmu_idx;
7264 uint32_t *frame_sp_p;
7265 uint32_t frameptr;
7266
7267 /* Pull the return address and IPSR from the Secure stack */
7268 threadmode = !arm_v7m_is_handler_mode(env);
7269 spsel = env->v7m.control[M_REG_S] & R_V7M_CONTROL_SPSEL_MASK;
7270
7271 frame_sp_p = get_v7m_sp_ptr(env, true, threadmode, spsel);
7272 frameptr = *frame_sp_p;
7273
7274 /* These loads may throw an exception (for MPU faults). We want to
7275 * do them as secure, so work out what MMU index that is.
7276 */
7277 mmu_idx = arm_v7m_mmu_idx_for_secstate(env, true);
7278 oi = make_memop_idx(MO_LE, arm_to_core_mmu_idx(mmu_idx));
7279 newpc = helper_le_ldul_mmu(env, frameptr, oi, 0);
7280 newpsr = helper_le_ldul_mmu(env, frameptr + 4, oi, 0);
7281
7282 /* Consistency checks on new IPSR */
7283 newpsr_exc = newpsr & XPSR_EXCP;
7284 if (!((env->v7m.exception == 0 && newpsr_exc == 0) ||
7285 (env->v7m.exception == 1 && newpsr_exc != 0))) {
7286 /* Pend the fault and tell our caller to take it */
7287 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_INVPC_MASK;
7288 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE,
7289 env->v7m.secure);
7290 qemu_log_mask(CPU_LOG_INT,
7291 "...taking INVPC UsageFault: "
7292 "IPSR consistency check failed\n");
7293 return false;
7294 }
7295
7296 *frame_sp_p = frameptr + 8;
7297 }
7298
7299 /* This invalidates frame_sp_p */
7300 switch_v7m_security_state(env, true);
7301 env->v7m.exception = newpsr_exc;
7302 env->v7m.control[M_REG_S] &= ~R_V7M_CONTROL_SFPA_MASK;
7303 if (newpsr & XPSR_SFPA) {
7304 env->v7m.control[M_REG_S] |= R_V7M_CONTROL_SFPA_MASK;
7305 }
7306 xpsr_write(env, 0, XPSR_IT);
7307 env->thumb = newpc & 1;
7308 env->regs[15] = newpc & ~1;
7309
7310 qemu_log_mask(CPU_LOG_INT, "...function return successful\n");
7311 return true;
7312}
7313
27a7ea8a
PB
7314static void arm_log_exception(int idx)
7315{
7316 if (qemu_loglevel_mask(CPU_LOG_INT)) {
7317 const char *exc = NULL;
2c4a7cc5
PM
7318 static const char * const excnames[] = {
7319 [EXCP_UDEF] = "Undefined Instruction",
7320 [EXCP_SWI] = "SVC",
7321 [EXCP_PREFETCH_ABORT] = "Prefetch Abort",
7322 [EXCP_DATA_ABORT] = "Data Abort",
7323 [EXCP_IRQ] = "IRQ",
7324 [EXCP_FIQ] = "FIQ",
7325 [EXCP_BKPT] = "Breakpoint",
7326 [EXCP_EXCEPTION_EXIT] = "QEMU v7M exception exit",
7327 [EXCP_KERNEL_TRAP] = "QEMU intercept of kernel commpage",
7328 [EXCP_HVC] = "Hypervisor Call",
7329 [EXCP_HYP_TRAP] = "Hypervisor Trap",
7330 [EXCP_SMC] = "Secure Monitor Call",
7331 [EXCP_VIRQ] = "Virtual IRQ",
7332 [EXCP_VFIQ] = "Virtual FIQ",
7333 [EXCP_SEMIHOST] = "Semihosting call",
7334 [EXCP_NOCP] = "v7M NOCP UsageFault",
7335 [EXCP_INVSTATE] = "v7M INVSTATE UsageFault",
7336 };
27a7ea8a
PB
7337
7338 if (idx >= 0 && idx < ARRAY_SIZE(excnames)) {
7339 exc = excnames[idx];
7340 }
7341 if (!exc) {
7342 exc = "unknown";
7343 }
7344 qemu_log_mask(CPU_LOG_INT, "Taking exception %d [%s]\n", idx, exc);
7345 }
7346}
7347
333e10c5
PM
7348static bool v7m_read_half_insn(ARMCPU *cpu, ARMMMUIdx mmu_idx,
7349 uint32_t addr, uint16_t *insn)
7350{
7351 /* Load a 16-bit portion of a v7M instruction, returning true on success,
7352 * or false on failure (in which case we will have pended the appropriate
7353 * exception).
7354 * We need to do the instruction fetch's MPU and SAU checks
7355 * like this because there is no MMU index that would allow
7356 * doing the load with a single function call. Instead we must
7357 * first check that the security attributes permit the load
7358 * and that they don't mismatch on the two halves of the instruction,
7359 * and then we do the load as a secure load (ie using the security
7360 * attributes of the address, not the CPU, as architecturally required).
7361 */
7362 CPUState *cs = CPU(cpu);
7363 CPUARMState *env = &cpu->env;
7364 V8M_SAttributes sattrs = {};
7365 MemTxAttrs attrs = {};
7366 ARMMMUFaultInfo fi = {};
7367 MemTxResult txres;
7368 target_ulong page_size;
7369 hwaddr physaddr;
7370 int prot;
333e10c5
PM
7371
7372 v8m_security_lookup(env, addr, MMU_INST_FETCH, mmu_idx, &sattrs);
7373 if (!sattrs.nsc || sattrs.ns) {
7374 /* This must be the second half of the insn, and it straddles a
7375 * region boundary with the second half not being S&NSC.
7376 */
7377 env->v7m.sfsr |= R_V7M_SFSR_INVEP_MASK;
7378 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SECURE, false);
7379 qemu_log_mask(CPU_LOG_INT,
7380 "...really SecureFault with SFSR.INVEP\n");
7381 return false;
7382 }
7383 if (get_phys_addr(env, addr, MMU_INST_FETCH, mmu_idx,
bc52bfeb 7384 &physaddr, &attrs, &prot, &page_size, &fi, NULL)) {
333e10c5
PM
7385 /* the MPU lookup failed */
7386 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_IACCVIOL_MASK;
7387 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_MEM, env->v7m.secure);
7388 qemu_log_mask(CPU_LOG_INT, "...really MemManage with CFSR.IACCVIOL\n");
7389 return false;
7390 }
7391 *insn = address_space_lduw_le(arm_addressspace(cs, attrs), physaddr,
7392 attrs, &txres);
7393 if (txres != MEMTX_OK) {
7394 env->v7m.cfsr[M_REG_NS] |= R_V7M_CFSR_IBUSERR_MASK;
7395 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_BUS, false);
7396 qemu_log_mask(CPU_LOG_INT, "...really BusFault with CFSR.IBUSERR\n");
7397 return false;
7398 }
7399 return true;
7400}
7401
7402static bool v7m_handle_execute_nsc(ARMCPU *cpu)
7403{
7404 /* Check whether this attempt to execute code in a Secure & NS-Callable
7405 * memory region is for an SG instruction; if so, then emulate the
7406 * effect of the SG instruction and return true. Otherwise pend
7407 * the correct kind of exception and return false.
7408 */
7409 CPUARMState *env = &cpu->env;
7410 ARMMMUIdx mmu_idx;
7411 uint16_t insn;
7412
7413 /* We should never get here unless get_phys_addr_pmsav8() caused
7414 * an exception for NS executing in S&NSC memory.
7415 */
7416 assert(!env->v7m.secure);
7417 assert(arm_feature(env, ARM_FEATURE_M_SECURITY));
7418
7419 /* We want to do the MPU lookup as secure; work out what mmu_idx that is */
7420 mmu_idx = arm_v7m_mmu_idx_for_secstate(env, true);
7421
7422 if (!v7m_read_half_insn(cpu, mmu_idx, env->regs[15], &insn)) {
7423 return false;
7424 }
7425
7426 if (!env->thumb) {
7427 goto gen_invep;
7428 }
7429
7430 if (insn != 0xe97f) {
7431 /* Not an SG instruction first half (we choose the IMPDEF
7432 * early-SG-check option).
7433 */
7434 goto gen_invep;
7435 }
7436
7437 if (!v7m_read_half_insn(cpu, mmu_idx, env->regs[15] + 2, &insn)) {
7438 return false;
7439 }
7440
7441 if (insn != 0xe97f) {
7442 /* Not an SG instruction second half (yes, both halves of the SG
7443 * insn have the same hex value)
7444 */
7445 goto gen_invep;
7446 }
7447
7448 /* OK, we have confirmed that we really have an SG instruction.
7449 * We know we're NS in S memory so don't need to repeat those checks.
7450 */
7451 qemu_log_mask(CPU_LOG_INT, "...really an SG instruction at 0x%08" PRIx32
7452 ", executing it\n", env->regs[15]);
7453 env->regs[14] &= ~1;
7454 switch_v7m_security_state(env, true);
7455 xpsr_write(env, 0, XPSR_IT);
7456 env->regs[15] += 4;
7457 return true;
7458
7459gen_invep:
7460 env->v7m.sfsr |= R_V7M_SFSR_INVEP_MASK;
7461 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SECURE, false);
7462 qemu_log_mask(CPU_LOG_INT,
7463 "...really SecureFault with SFSR.INVEP\n");
7464 return false;
7465}
7466
e6f010cc 7467void arm_v7m_cpu_do_interrupt(CPUState *cs)
9ee6e8bb 7468{
e6f010cc
AF
7469 ARMCPU *cpu = ARM_CPU(cs);
7470 CPUARMState *env = &cpu->env;
9ee6e8bb 7471 uint32_t lr;
0094ca70 7472 bool ignore_stackfaults;
9ee6e8bb 7473
27103424 7474 arm_log_exception(cs->exception_index);
3f1beaca 7475
9ee6e8bb
PB
7476 /* For exceptions we just mark as pending on the NVIC, and let that
7477 handle it. */
27103424 7478 switch (cs->exception_index) {
9ee6e8bb 7479 case EXCP_UDEF:
2fb50a33 7480 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, env->v7m.secure);
334e8dad 7481 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_UNDEFINSTR_MASK;
a25dc805 7482 break;
7517748e 7483 case EXCP_NOCP:
2fb50a33 7484 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, env->v7m.secure);
334e8dad 7485 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_NOCP_MASK;
a25dc805 7486 break;
e13886e3 7487 case EXCP_INVSTATE:
2fb50a33 7488 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, env->v7m.secure);
334e8dad 7489 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_INVSTATE_MASK;
e13886e3 7490 break;
9ee6e8bb 7491 case EXCP_SWI:
314e2296 7492 /* The PC already points to the next instruction. */
2fb50a33 7493 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SVC, env->v7m.secure);
a25dc805 7494 break;
9ee6e8bb
PB
7495 case EXCP_PREFETCH_ABORT:
7496 case EXCP_DATA_ABORT:
5dd0641d
MD
7497 /* Note that for M profile we don't have a guest facing FSR, but
7498 * the env->exception.fsr will be populated by the code that
7499 * raises the fault, in the A profile short-descriptor format.
abf1172f 7500 */
5dd0641d 7501 switch (env->exception.fsr & 0xf) {
35337cc3
PM
7502 case M_FAKE_FSR_NSC_EXEC:
7503 /* Exception generated when we try to execute code at an address
7504 * which is marked as Secure & Non-Secure Callable and the CPU
7505 * is in the Non-Secure state. The only instruction which can
7506 * be executed like this is SG (and that only if both halves of
7507 * the SG instruction have the same security attributes.)
7508 * Everything else must generate an INVEP SecureFault, so we
7509 * emulate the SG instruction here.
35337cc3 7510 */
333e10c5
PM
7511 if (v7m_handle_execute_nsc(cpu)) {
7512 return;
7513 }
35337cc3
PM
7514 break;
7515 case M_FAKE_FSR_SFAULT:
7516 /* Various flavours of SecureFault for attempts to execute or
7517 * access data in the wrong security state.
7518 */
7519 switch (cs->exception_index) {
7520 case EXCP_PREFETCH_ABORT:
7521 if (env->v7m.secure) {
7522 env->v7m.sfsr |= R_V7M_SFSR_INVTRAN_MASK;
7523 qemu_log_mask(CPU_LOG_INT,
7524 "...really SecureFault with SFSR.INVTRAN\n");
7525 } else {
7526 env->v7m.sfsr |= R_V7M_SFSR_INVEP_MASK;
7527 qemu_log_mask(CPU_LOG_INT,
7528 "...really SecureFault with SFSR.INVEP\n");
7529 }
7530 break;
7531 case EXCP_DATA_ABORT:
7532 /* This must be an NS access to S memory */
7533 env->v7m.sfsr |= R_V7M_SFSR_AUVIOL_MASK;
7534 qemu_log_mask(CPU_LOG_INT,
7535 "...really SecureFault with SFSR.AUVIOL\n");
7536 break;
7537 }
7538 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SECURE, false);
7539 break;
5dd0641d
MD
7540 case 0x8: /* External Abort */
7541 switch (cs->exception_index) {
7542 case EXCP_PREFETCH_ABORT:
c6158878
PM
7543 env->v7m.cfsr[M_REG_NS] |= R_V7M_CFSR_IBUSERR_MASK;
7544 qemu_log_mask(CPU_LOG_INT, "...with CFSR.IBUSERR\n");
5dd0641d
MD
7545 break;
7546 case EXCP_DATA_ABORT:
334e8dad 7547 env->v7m.cfsr[M_REG_NS] |=
c6158878 7548 (R_V7M_CFSR_PRECISERR_MASK | R_V7M_CFSR_BFARVALID_MASK);
5dd0641d
MD
7549 env->v7m.bfar = env->exception.vaddress;
7550 qemu_log_mask(CPU_LOG_INT,
c6158878 7551 "...with CFSR.PRECISERR and BFAR 0x%x\n",
5dd0641d
MD
7552 env->v7m.bfar);
7553 break;
7554 }
2fb50a33 7555 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_BUS, false);
5dd0641d
MD
7556 break;
7557 default:
7558 /* All other FSR values are either MPU faults or "can't happen
7559 * for M profile" cases.
7560 */
7561 switch (cs->exception_index) {
7562 case EXCP_PREFETCH_ABORT:
334e8dad 7563 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_IACCVIOL_MASK;
5dd0641d
MD
7564 qemu_log_mask(CPU_LOG_INT, "...with CFSR.IACCVIOL\n");
7565 break;
7566 case EXCP_DATA_ABORT:
334e8dad 7567 env->v7m.cfsr[env->v7m.secure] |=
5dd0641d 7568 (R_V7M_CFSR_DACCVIOL_MASK | R_V7M_CFSR_MMARVALID_MASK);
c51a5cfc 7569 env->v7m.mmfar[env->v7m.secure] = env->exception.vaddress;
5dd0641d
MD
7570 qemu_log_mask(CPU_LOG_INT,
7571 "...with CFSR.DACCVIOL and MMFAR 0x%x\n",
c51a5cfc 7572 env->v7m.mmfar[env->v7m.secure]);
5dd0641d
MD
7573 break;
7574 }
2fb50a33
PM
7575 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_MEM,
7576 env->v7m.secure);
5dd0641d
MD
7577 break;
7578 }
a25dc805 7579 break;
9ee6e8bb 7580 case EXCP_BKPT:
cfe67cef 7581 if (semihosting_enabled()) {
2ad207d4 7582 int nr;
f9fd40eb 7583 nr = arm_lduw_code(env, env->regs[15], arm_sctlr_b(env)) & 0xff;
2ad207d4
PB
7584 if (nr == 0xab) {
7585 env->regs[15] += 2;
205ace55
CC
7586 qemu_log_mask(CPU_LOG_INT,
7587 "...handling as semihosting call 0x%x\n",
7588 env->regs[0]);
2ad207d4
PB
7589 env->regs[0] = do_arm_semihosting(env);
7590 return;
7591 }
7592 }
2fb50a33 7593 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_DEBUG, false);
a25dc805 7594 break;
9ee6e8bb 7595 case EXCP_IRQ:
9ee6e8bb
PB
7596 break;
7597 case EXCP_EXCEPTION_EXIT:
d02a8698
PM
7598 if (env->regs[15] < EXC_RETURN_MIN_MAGIC) {
7599 /* Must be v8M security extension function return */
7600 assert(env->regs[15] >= FNC_RETURN_MIN_MAGIC);
7601 assert(arm_feature(env, ARM_FEATURE_M_SECURITY));
7602 if (do_v7m_function_return(cpu)) {
7603 return;
7604 }
7605 } else {
7606 do_v7m_exception_exit(cpu);
7607 return;
7608 }
7609 break;
9ee6e8bb 7610 default:
a47dddd7 7611 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
9ee6e8bb
PB
7612 return; /* Never happens. Keep compiler happy. */
7613 }
7614
d3392718
PM
7615 if (arm_feature(env, ARM_FEATURE_V8)) {
7616 lr = R_V7M_EXCRET_RES1_MASK |
7617 R_V7M_EXCRET_DCRS_MASK |
7618 R_V7M_EXCRET_FTYPE_MASK;
7619 /* The S bit indicates whether we should return to Secure
7620 * or NonSecure (ie our current state).
7621 * The ES bit indicates whether we're taking this exception
7622 * to Secure or NonSecure (ie our target state). We set it
7623 * later, in v7m_exception_taken().
7624 * The SPSEL bit is also set in v7m_exception_taken() for v8M.
7625 * This corresponds to the ARM ARM pseudocode for v8M setting
7626 * some LR bits in PushStack() and some in ExceptionTaken();
7627 * the distinction matters for the tailchain cases where we
7628 * can take an exception without pushing the stack.
7629 */
7630 if (env->v7m.secure) {
7631 lr |= R_V7M_EXCRET_S_MASK;
7632 }
7633 } else {
7634 lr = R_V7M_EXCRET_RES1_MASK |
7635 R_V7M_EXCRET_S_MASK |
7636 R_V7M_EXCRET_DCRS_MASK |
7637 R_V7M_EXCRET_FTYPE_MASK |
7638 R_V7M_EXCRET_ES_MASK;
7639 if (env->v7m.control[M_REG_NS] & R_V7M_CONTROL_SPSEL_MASK) {
7640 lr |= R_V7M_EXCRET_SPSEL_MASK;
7641 }
bd70b29b 7642 }
15b3f556 7643 if (!arm_v7m_is_handler_mode(env)) {
4d1e7a47 7644 lr |= R_V7M_EXCRET_MODE_MASK;
bd70b29b
PM
7645 }
7646
0094ca70
PM
7647 ignore_stackfaults = v7m_push_stack(cpu);
7648 v7m_exception_taken(cpu, lr, false, ignore_stackfaults);
a25dc805 7649 qemu_log_mask(CPU_LOG_INT, "... as %d\n", env->v7m.exception);
9ee6e8bb
PB
7650}
7651
ce02049d
GB
7652/* Function used to synchronize QEMU's AArch64 register set with AArch32
7653 * register set. This is necessary when switching between AArch32 and AArch64
7654 * execution state.
7655 */
7656void aarch64_sync_32_to_64(CPUARMState *env)
7657{
7658 int i;
7659 uint32_t mode = env->uncached_cpsr & CPSR_M;
7660
7661 /* We can blanket copy R[0:7] to X[0:7] */
7662 for (i = 0; i < 8; i++) {
7663 env->xregs[i] = env->regs[i];
7664 }
7665
7666 /* Unless we are in FIQ mode, x8-x12 come from the user registers r8-r12.
7667 * Otherwise, they come from the banked user regs.
7668 */
7669 if (mode == ARM_CPU_MODE_FIQ) {
7670 for (i = 8; i < 13; i++) {
7671 env->xregs[i] = env->usr_regs[i - 8];
7672 }
7673 } else {
7674 for (i = 8; i < 13; i++) {
7675 env->xregs[i] = env->regs[i];
7676 }
7677 }
7678
7679 /* Registers x13-x23 are the various mode SP and FP registers. Registers
7680 * r13 and r14 are only copied if we are in that mode, otherwise we copy
7681 * from the mode banked register.
7682 */
7683 if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
7684 env->xregs[13] = env->regs[13];
7685 env->xregs[14] = env->regs[14];
7686 } else {
7687 env->xregs[13] = env->banked_r13[bank_number(ARM_CPU_MODE_USR)];
7688 /* HYP is an exception in that it is copied from r14 */
7689 if (mode == ARM_CPU_MODE_HYP) {
7690 env->xregs[14] = env->regs[14];
7691 } else {
7692 env->xregs[14] = env->banked_r14[bank_number(ARM_CPU_MODE_USR)];
7693 }
7694 }
7695
7696 if (mode == ARM_CPU_MODE_HYP) {
7697 env->xregs[15] = env->regs[13];
7698 } else {
7699 env->xregs[15] = env->banked_r13[bank_number(ARM_CPU_MODE_HYP)];
7700 }
7701
7702 if (mode == ARM_CPU_MODE_IRQ) {
3a9148d0
SS
7703 env->xregs[16] = env->regs[14];
7704 env->xregs[17] = env->regs[13];
ce02049d 7705 } else {
3a9148d0
SS
7706 env->xregs[16] = env->banked_r14[bank_number(ARM_CPU_MODE_IRQ)];
7707 env->xregs[17] = env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)];
ce02049d
GB
7708 }
7709
7710 if (mode == ARM_CPU_MODE_SVC) {
3a9148d0
SS
7711 env->xregs[18] = env->regs[14];
7712 env->xregs[19] = env->regs[13];
ce02049d 7713 } else {
3a9148d0
SS
7714 env->xregs[18] = env->banked_r14[bank_number(ARM_CPU_MODE_SVC)];
7715 env->xregs[19] = env->banked_r13[bank_number(ARM_CPU_MODE_SVC)];
ce02049d
GB
7716 }
7717
7718 if (mode == ARM_CPU_MODE_ABT) {
3a9148d0
SS
7719 env->xregs[20] = env->regs[14];
7720 env->xregs[21] = env->regs[13];
ce02049d 7721 } else {
3a9148d0
SS
7722 env->xregs[20] = env->banked_r14[bank_number(ARM_CPU_MODE_ABT)];
7723 env->xregs[21] = env->banked_r13[bank_number(ARM_CPU_MODE_ABT)];
ce02049d
GB
7724 }
7725
7726 if (mode == ARM_CPU_MODE_UND) {
3a9148d0
SS
7727 env->xregs[22] = env->regs[14];
7728 env->xregs[23] = env->regs[13];
ce02049d 7729 } else {
3a9148d0
SS
7730 env->xregs[22] = env->banked_r14[bank_number(ARM_CPU_MODE_UND)];
7731 env->xregs[23] = env->banked_r13[bank_number(ARM_CPU_MODE_UND)];
ce02049d
GB
7732 }
7733
7734 /* Registers x24-x30 are mapped to r8-r14 in FIQ mode. If we are in FIQ
7735 * mode, then we can copy from r8-r14. Otherwise, we copy from the
7736 * FIQ bank for r8-r14.
7737 */
7738 if (mode == ARM_CPU_MODE_FIQ) {
7739 for (i = 24; i < 31; i++) {
7740 env->xregs[i] = env->regs[i - 16]; /* X[24:30] <- R[8:14] */
7741 }
7742 } else {
7743 for (i = 24; i < 29; i++) {
7744 env->xregs[i] = env->fiq_regs[i - 24];
7745 }
7746 env->xregs[29] = env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)];
7747 env->xregs[30] = env->banked_r14[bank_number(ARM_CPU_MODE_FIQ)];
7748 }
7749
7750 env->pc = env->regs[15];
7751}
7752
7753/* Function used to synchronize QEMU's AArch32 register set with AArch64
7754 * register set. This is necessary when switching between AArch32 and AArch64
7755 * execution state.
7756 */
7757void aarch64_sync_64_to_32(CPUARMState *env)
7758{
7759 int i;
7760 uint32_t mode = env->uncached_cpsr & CPSR_M;
7761
7762 /* We can blanket copy X[0:7] to R[0:7] */
7763 for (i = 0; i < 8; i++) {
7764 env->regs[i] = env->xregs[i];
7765 }
7766
7767 /* Unless we are in FIQ mode, r8-r12 come from the user registers x8-x12.
7768 * Otherwise, we copy x8-x12 into the banked user regs.
7769 */
7770 if (mode == ARM_CPU_MODE_FIQ) {
7771 for (i = 8; i < 13; i++) {
7772 env->usr_regs[i - 8] = env->xregs[i];
7773 }
7774 } else {
7775 for (i = 8; i < 13; i++) {
7776 env->regs[i] = env->xregs[i];
7777 }
7778 }
7779
7780 /* Registers r13 & r14 depend on the current mode.
7781 * If we are in a given mode, we copy the corresponding x registers to r13
7782 * and r14. Otherwise, we copy the x register to the banked r13 and r14
7783 * for the mode.
7784 */
7785 if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
7786 env->regs[13] = env->xregs[13];
7787 env->regs[14] = env->xregs[14];
7788 } else {
7789 env->banked_r13[bank_number(ARM_CPU_MODE_USR)] = env->xregs[13];
7790
7791 /* HYP is an exception in that it does not have its own banked r14 but
7792 * shares the USR r14
7793 */
7794 if (mode == ARM_CPU_MODE_HYP) {
7795 env->regs[14] = env->xregs[14];
7796 } else {
7797 env->banked_r14[bank_number(ARM_CPU_MODE_USR)] = env->xregs[14];
7798 }
7799 }
7800
7801 if (mode == ARM_CPU_MODE_HYP) {
7802 env->regs[13] = env->xregs[15];
7803 } else {
7804 env->banked_r13[bank_number(ARM_CPU_MODE_HYP)] = env->xregs[15];
7805 }
7806
7807 if (mode == ARM_CPU_MODE_IRQ) {
3a9148d0
SS
7808 env->regs[14] = env->xregs[16];
7809 env->regs[13] = env->xregs[17];
ce02049d 7810 } else {
3a9148d0
SS
7811 env->banked_r14[bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[16];
7812 env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[17];
ce02049d
GB
7813 }
7814
7815 if (mode == ARM_CPU_MODE_SVC) {
3a9148d0
SS
7816 env->regs[14] = env->xregs[18];
7817 env->regs[13] = env->xregs[19];
ce02049d 7818 } else {
3a9148d0
SS
7819 env->banked_r14[bank_number(ARM_CPU_MODE_SVC)] = env->xregs[18];
7820 env->banked_r13[bank_number(ARM_CPU_MODE_SVC)] = env->xregs[19];
ce02049d
GB
7821 }
7822
7823 if (mode == ARM_CPU_MODE_ABT) {
3a9148d0
SS
7824 env->regs[14] = env->xregs[20];
7825 env->regs[13] = env->xregs[21];
ce02049d 7826 } else {
3a9148d0
SS
7827 env->banked_r14[bank_number(ARM_CPU_MODE_ABT)] = env->xregs[20];
7828 env->banked_r13[bank_number(ARM_CPU_MODE_ABT)] = env->xregs[21];
ce02049d
GB
7829 }
7830
7831 if (mode == ARM_CPU_MODE_UND) {
3a9148d0
SS
7832 env->regs[14] = env->xregs[22];
7833 env->regs[13] = env->xregs[23];
ce02049d 7834 } else {
3a9148d0
SS
7835 env->banked_r14[bank_number(ARM_CPU_MODE_UND)] = env->xregs[22];
7836 env->banked_r13[bank_number(ARM_CPU_MODE_UND)] = env->xregs[23];
ce02049d
GB
7837 }
7838
7839 /* Registers x24-x30 are mapped to r8-r14 in FIQ mode. If we are in FIQ
7840 * mode, then we can copy to r8-r14. Otherwise, we copy to the
7841 * FIQ bank for r8-r14.
7842 */
7843 if (mode == ARM_CPU_MODE_FIQ) {
7844 for (i = 24; i < 31; i++) {
7845 env->regs[i - 16] = env->xregs[i]; /* X[24:30] -> R[8:14] */
7846 }
7847 } else {
7848 for (i = 24; i < 29; i++) {
7849 env->fiq_regs[i - 24] = env->xregs[i];
7850 }
7851 env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[29];
7852 env->banked_r14[bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[30];
7853 }
7854
7855 env->regs[15] = env->pc;
7856}
7857
966f758c 7858static void arm_cpu_do_interrupt_aarch32(CPUState *cs)
b5ff1b31 7859{
97a8ea5a
AF
7860 ARMCPU *cpu = ARM_CPU(cs);
7861 CPUARMState *env = &cpu->env;
b5ff1b31
FB
7862 uint32_t addr;
7863 uint32_t mask;
7864 int new_mode;
7865 uint32_t offset;
16a906fd 7866 uint32_t moe;
b5ff1b31 7867
16a906fd
PM
7868 /* If this is a debug exception we must update the DBGDSCR.MOE bits */
7869 switch (env->exception.syndrome >> ARM_EL_EC_SHIFT) {
7870 case EC_BREAKPOINT:
7871 case EC_BREAKPOINT_SAME_EL:
7872 moe = 1;
7873 break;
7874 case EC_WATCHPOINT:
7875 case EC_WATCHPOINT_SAME_EL:
7876 moe = 10;
7877 break;
7878 case EC_AA32_BKPT:
7879 moe = 3;
7880 break;
7881 case EC_VECTORCATCH:
7882 moe = 5;
7883 break;
7884 default:
7885 moe = 0;
7886 break;
7887 }
7888
7889 if (moe) {
7890 env->cp15.mdscr_el1 = deposit64(env->cp15.mdscr_el1, 2, 4, moe);
7891 }
7892
b5ff1b31 7893 /* TODO: Vectored interrupt controller. */
27103424 7894 switch (cs->exception_index) {
b5ff1b31
FB
7895 case EXCP_UDEF:
7896 new_mode = ARM_CPU_MODE_UND;
7897 addr = 0x04;
7898 mask = CPSR_I;
7899 if (env->thumb)
7900 offset = 2;
7901 else
7902 offset = 4;
7903 break;
7904 case EXCP_SWI:
7905 new_mode = ARM_CPU_MODE_SVC;
7906 addr = 0x08;
7907 mask = CPSR_I;
601d70b9 7908 /* The PC already points to the next instruction. */
b5ff1b31
FB
7909 offset = 0;
7910 break;
06c949e6 7911 case EXCP_BKPT:
abf1172f 7912 env->exception.fsr = 2;
9ee6e8bb
PB
7913 /* Fall through to prefetch abort. */
7914 case EXCP_PREFETCH_ABORT:
88ca1c2d 7915 A32_BANKED_CURRENT_REG_SET(env, ifsr, env->exception.fsr);
b848ce2b 7916 A32_BANKED_CURRENT_REG_SET(env, ifar, env->exception.vaddress);
3f1beaca 7917 qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x IFAR 0x%x\n",
88ca1c2d 7918 env->exception.fsr, (uint32_t)env->exception.vaddress);
b5ff1b31
FB
7919 new_mode = ARM_CPU_MODE_ABT;
7920 addr = 0x0c;
7921 mask = CPSR_A | CPSR_I;
7922 offset = 4;
7923 break;
7924 case EXCP_DATA_ABORT:
4a7e2d73 7925 A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr);
b848ce2b 7926 A32_BANKED_CURRENT_REG_SET(env, dfar, env->exception.vaddress);
3f1beaca 7927 qemu_log_mask(CPU_LOG_INT, "...with DFSR 0x%x DFAR 0x%x\n",
4a7e2d73 7928 env->exception.fsr,
6cd8a264 7929 (uint32_t)env->exception.vaddress);
b5ff1b31
FB
7930 new_mode = ARM_CPU_MODE_ABT;
7931 addr = 0x10;
7932 mask = CPSR_A | CPSR_I;
7933 offset = 8;
7934 break;
7935 case EXCP_IRQ:
7936 new_mode = ARM_CPU_MODE_IRQ;
7937 addr = 0x18;
7938 /* Disable IRQ and imprecise data aborts. */
7939 mask = CPSR_A | CPSR_I;
7940 offset = 4;
de38d23b
FA
7941 if (env->cp15.scr_el3 & SCR_IRQ) {
7942 /* IRQ routed to monitor mode */
7943 new_mode = ARM_CPU_MODE_MON;
7944 mask |= CPSR_F;
7945 }
b5ff1b31
FB
7946 break;
7947 case EXCP_FIQ:
7948 new_mode = ARM_CPU_MODE_FIQ;
7949 addr = 0x1c;
7950 /* Disable FIQ, IRQ and imprecise data aborts. */
7951 mask = CPSR_A | CPSR_I | CPSR_F;
de38d23b
FA
7952 if (env->cp15.scr_el3 & SCR_FIQ) {
7953 /* FIQ routed to monitor mode */
7954 new_mode = ARM_CPU_MODE_MON;
7955 }
b5ff1b31
FB
7956 offset = 4;
7957 break;
87a4b270
PM
7958 case EXCP_VIRQ:
7959 new_mode = ARM_CPU_MODE_IRQ;
7960 addr = 0x18;
7961 /* Disable IRQ and imprecise data aborts. */
7962 mask = CPSR_A | CPSR_I;
7963 offset = 4;
7964 break;
7965 case EXCP_VFIQ:
7966 new_mode = ARM_CPU_MODE_FIQ;
7967 addr = 0x1c;
7968 /* Disable FIQ, IRQ and imprecise data aborts. */
7969 mask = CPSR_A | CPSR_I | CPSR_F;
7970 offset = 4;
7971 break;
dbe9d163
FA
7972 case EXCP_SMC:
7973 new_mode = ARM_CPU_MODE_MON;
7974 addr = 0x08;
7975 mask = CPSR_A | CPSR_I | CPSR_F;
7976 offset = 0;
7977 break;
b5ff1b31 7978 default:
a47dddd7 7979 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
b5ff1b31
FB
7980 return; /* Never happens. Keep compiler happy. */
7981 }
e89e51a1
FA
7982
7983 if (new_mode == ARM_CPU_MODE_MON) {
7984 addr += env->cp15.mvbar;
137feaa9 7985 } else if (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_V) {
e89e51a1 7986 /* High vectors. When enabled, base address cannot be remapped. */
b5ff1b31 7987 addr += 0xffff0000;
8641136c
NR
7988 } else {
7989 /* ARM v7 architectures provide a vector base address register to remap
7990 * the interrupt vector table.
e89e51a1 7991 * This register is only followed in non-monitor mode, and is banked.
8641136c
NR
7992 * Note: only bits 31:5 are valid.
7993 */
fb6c91ba 7994 addr += A32_BANKED_CURRENT_REG_GET(env, vbar);
b5ff1b31 7995 }
dbe9d163
FA
7996
7997 if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON) {
7998 env->cp15.scr_el3 &= ~SCR_NS;
7999 }
8000
b5ff1b31 8001 switch_mode (env, new_mode);
662cefb7
PM
8002 /* For exceptions taken to AArch32 we must clear the SS bit in both
8003 * PSTATE and in the old-state value we save to SPSR_<mode>, so zero it now.
8004 */
8005 env->uncached_cpsr &= ~PSTATE_SS;
b5ff1b31 8006 env->spsr = cpsr_read(env);
9ee6e8bb
PB
8007 /* Clear IT bits. */
8008 env->condexec_bits = 0;
30a8cac1 8009 /* Switch to the new mode, and to the correct instruction set. */
6d7e6326 8010 env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode;
73462ddd
PC
8011 /* Set new mode endianness */
8012 env->uncached_cpsr &= ~CPSR_E;
8013 if (env->cp15.sctlr_el[arm_current_el(env)] & SCTLR_EE) {
3823b9db 8014 env->uncached_cpsr |= CPSR_E;
73462ddd 8015 }
4cc35614 8016 env->daif |= mask;
be5e7a76
DES
8017 /* this is a lie, as the was no c1_sys on V4T/V5, but who cares
8018 * and we should just guard the thumb mode on V4 */
8019 if (arm_feature(env, ARM_FEATURE_V4T)) {
137feaa9 8020 env->thumb = (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_TE) != 0;
be5e7a76 8021 }
b5ff1b31
FB
8022 env->regs[14] = env->regs[15] + offset;
8023 env->regs[15] = addr;
b5ff1b31
FB
8024}
8025
966f758c
PM
8026/* Handle exception entry to a target EL which is using AArch64 */
8027static void arm_cpu_do_interrupt_aarch64(CPUState *cs)
f3a9b694
PM
8028{
8029 ARMCPU *cpu = ARM_CPU(cs);
8030 CPUARMState *env = &cpu->env;
8031 unsigned int new_el = env->exception.target_el;
8032 target_ulong addr = env->cp15.vbar_el[new_el];
8033 unsigned int new_mode = aarch64_pstate_mode(new_el, true);
8034
8035 if (arm_current_el(env) < new_el) {
3d6f7617
PM
8036 /* Entry vector offset depends on whether the implemented EL
8037 * immediately lower than the target level is using AArch32 or AArch64
8038 */
8039 bool is_aa64;
8040
8041 switch (new_el) {
8042 case 3:
8043 is_aa64 = (env->cp15.scr_el3 & SCR_RW) != 0;
8044 break;
8045 case 2:
8046 is_aa64 = (env->cp15.hcr_el2 & HCR_RW) != 0;
8047 break;
8048 case 1:
8049 is_aa64 = is_a64(env);
8050 break;
8051 default:
8052 g_assert_not_reached();
8053 }
8054
8055 if (is_aa64) {
f3a9b694
PM
8056 addr += 0x400;
8057 } else {
8058 addr += 0x600;
8059 }
8060 } else if (pstate_read(env) & PSTATE_SP) {
8061 addr += 0x200;
8062 }
8063
f3a9b694
PM
8064 switch (cs->exception_index) {
8065 case EXCP_PREFETCH_ABORT:
8066 case EXCP_DATA_ABORT:
8067 env->cp15.far_el[new_el] = env->exception.vaddress;
8068 qemu_log_mask(CPU_LOG_INT, "...with FAR 0x%" PRIx64 "\n",
8069 env->cp15.far_el[new_el]);
8070 /* fall through */
8071 case EXCP_BKPT:
8072 case EXCP_UDEF:
8073 case EXCP_SWI:
8074 case EXCP_HVC:
8075 case EXCP_HYP_TRAP:
8076 case EXCP_SMC:
8077 env->cp15.esr_el[new_el] = env->exception.syndrome;
8078 break;
8079 case EXCP_IRQ:
8080 case EXCP_VIRQ:
8081 addr += 0x80;
8082 break;
8083 case EXCP_FIQ:
8084 case EXCP_VFIQ:
8085 addr += 0x100;
8086 break;
8087 case EXCP_SEMIHOST:
8088 qemu_log_mask(CPU_LOG_INT,
8089 "...handling as semihosting call 0x%" PRIx64 "\n",
8090 env->xregs[0]);
8091 env->xregs[0] = do_arm_semihosting(env);
8092 return;
8093 default:
8094 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
8095 }
8096
8097 if (is_a64(env)) {
8098 env->banked_spsr[aarch64_banked_spsr_index(new_el)] = pstate_read(env);
8099 aarch64_save_sp(env, arm_current_el(env));
8100 env->elr_el[new_el] = env->pc;
8101 } else {
8102 env->banked_spsr[aarch64_banked_spsr_index(new_el)] = cpsr_read(env);
f3a9b694
PM
8103 env->elr_el[new_el] = env->regs[15];
8104
8105 aarch64_sync_32_to_64(env);
8106
8107 env->condexec_bits = 0;
8108 }
8109 qemu_log_mask(CPU_LOG_INT, "...with ELR 0x%" PRIx64 "\n",
8110 env->elr_el[new_el]);
8111
8112 pstate_write(env, PSTATE_DAIF | new_mode);
8113 env->aarch64 = 1;
8114 aarch64_restore_sp(env, new_el);
8115
8116 env->pc = addr;
8117
8118 qemu_log_mask(CPU_LOG_INT, "...to EL%d PC 0x%" PRIx64 " PSTATE 0x%x\n",
8119 new_el, env->pc, pstate_read(env));
966f758c
PM
8120}
8121
904c04de
PM
8122static inline bool check_for_semihosting(CPUState *cs)
8123{
8124 /* Check whether this exception is a semihosting call; if so
8125 * then handle it and return true; otherwise return false.
8126 */
8127 ARMCPU *cpu = ARM_CPU(cs);
8128 CPUARMState *env = &cpu->env;
8129
8130 if (is_a64(env)) {
8131 if (cs->exception_index == EXCP_SEMIHOST) {
8132 /* This is always the 64-bit semihosting exception.
8133 * The "is this usermode" and "is semihosting enabled"
8134 * checks have been done at translate time.
8135 */
8136 qemu_log_mask(CPU_LOG_INT,
8137 "...handling as semihosting call 0x%" PRIx64 "\n",
8138 env->xregs[0]);
8139 env->xregs[0] = do_arm_semihosting(env);
8140 return true;
8141 }
8142 return false;
8143 } else {
8144 uint32_t imm;
8145
8146 /* Only intercept calls from privileged modes, to provide some
8147 * semblance of security.
8148 */
19a6e31c
PM
8149 if (cs->exception_index != EXCP_SEMIHOST &&
8150 (!semihosting_enabled() ||
8151 ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_USR))) {
904c04de
PM
8152 return false;
8153 }
8154
8155 switch (cs->exception_index) {
19a6e31c
PM
8156 case EXCP_SEMIHOST:
8157 /* This is always a semihosting call; the "is this usermode"
8158 * and "is semihosting enabled" checks have been done at
8159 * translate time.
8160 */
8161 break;
904c04de
PM
8162 case EXCP_SWI:
8163 /* Check for semihosting interrupt. */
8164 if (env->thumb) {
f9fd40eb 8165 imm = arm_lduw_code(env, env->regs[15] - 2, arm_sctlr_b(env))
904c04de
PM
8166 & 0xff;
8167 if (imm == 0xab) {
8168 break;
8169 }
8170 } else {
f9fd40eb 8171 imm = arm_ldl_code(env, env->regs[15] - 4, arm_sctlr_b(env))
904c04de
PM
8172 & 0xffffff;
8173 if (imm == 0x123456) {
8174 break;
8175 }
8176 }
8177 return false;
8178 case EXCP_BKPT:
8179 /* See if this is a semihosting syscall. */
8180 if (env->thumb) {
f9fd40eb 8181 imm = arm_lduw_code(env, env->regs[15], arm_sctlr_b(env))
904c04de
PM
8182 & 0xff;
8183 if (imm == 0xab) {
8184 env->regs[15] += 2;
8185 break;
8186 }
8187 }
8188 return false;
8189 default:
8190 return false;
8191 }
8192
8193 qemu_log_mask(CPU_LOG_INT,
8194 "...handling as semihosting call 0x%x\n",
8195 env->regs[0]);
8196 env->regs[0] = do_arm_semihosting(env);
8197 return true;
8198 }
8199}
8200
966f758c
PM
8201/* Handle a CPU exception for A and R profile CPUs.
8202 * Do any appropriate logging, handle PSCI calls, and then hand off
8203 * to the AArch64-entry or AArch32-entry function depending on the
8204 * target exception level's register width.
8205 */
8206void arm_cpu_do_interrupt(CPUState *cs)
8207{
8208 ARMCPU *cpu = ARM_CPU(cs);
8209 CPUARMState *env = &cpu->env;
8210 unsigned int new_el = env->exception.target_el;
8211
531c60a9 8212 assert(!arm_feature(env, ARM_FEATURE_M));
966f758c
PM
8213
8214 arm_log_exception(cs->exception_index);
8215 qemu_log_mask(CPU_LOG_INT, "...from EL%d to EL%d\n", arm_current_el(env),
8216 new_el);
8217 if (qemu_loglevel_mask(CPU_LOG_INT)
8218 && !excp_is_internal(cs->exception_index)) {
6568da45 8219 qemu_log_mask(CPU_LOG_INT, "...with ESR 0x%x/0x%" PRIx32 "\n",
966f758c
PM
8220 env->exception.syndrome >> ARM_EL_EC_SHIFT,
8221 env->exception.syndrome);
8222 }
8223
8224 if (arm_is_psci_call(cpu, cs->exception_index)) {
8225 arm_handle_psci_call(cpu);
8226 qemu_log_mask(CPU_LOG_INT, "...handled as PSCI call\n");
8227 return;
8228 }
8229
904c04de
PM
8230 /* Semihosting semantics depend on the register width of the
8231 * code that caused the exception, not the target exception level,
8232 * so must be handled here.
966f758c 8233 */
904c04de
PM
8234 if (check_for_semihosting(cs)) {
8235 return;
8236 }
8237
8238 assert(!excp_is_internal(cs->exception_index));
8239 if (arm_el_is_aa64(env, new_el)) {
966f758c
PM
8240 arm_cpu_do_interrupt_aarch64(cs);
8241 } else {
8242 arm_cpu_do_interrupt_aarch32(cs);
8243 }
f3a9b694 8244
8d04fb55
JK
8245 /* Hooks may change global state so BQL should be held, also the
8246 * BQL needs to be held for any modification of
8247 * cs->interrupt_request.
8248 */
8249 g_assert(qemu_mutex_iothread_locked());
8250
bd7d00fc
PM
8251 arm_call_el_change_hook(cpu);
8252
f3a9b694
PM
8253 if (!kvm_enabled()) {
8254 cs->interrupt_request |= CPU_INTERRUPT_EXITTB;
8255 }
8256}
0480f69a
PM
8257
8258/* Return the exception level which controls this address translation regime */
8259static inline uint32_t regime_el(CPUARMState *env, ARMMMUIdx mmu_idx)
8260{
8261 switch (mmu_idx) {
8262 case ARMMMUIdx_S2NS:
8263 case ARMMMUIdx_S1E2:
8264 return 2;
8265 case ARMMMUIdx_S1E3:
8266 return 3;
8267 case ARMMMUIdx_S1SE0:
8268 return arm_el_is_aa64(env, 3) ? 1 : 3;
8269 case ARMMMUIdx_S1SE1:
8270 case ARMMMUIdx_S1NSE0:
8271 case ARMMMUIdx_S1NSE1:
62593718
PM
8272 case ARMMMUIdx_MPrivNegPri:
8273 case ARMMMUIdx_MUserNegPri:
e7b921c2
PM
8274 case ARMMMUIdx_MPriv:
8275 case ARMMMUIdx_MUser:
62593718
PM
8276 case ARMMMUIdx_MSPrivNegPri:
8277 case ARMMMUIdx_MSUserNegPri:
66787c78 8278 case ARMMMUIdx_MSPriv:
66787c78 8279 case ARMMMUIdx_MSUser:
0480f69a
PM
8280 return 1;
8281 default:
8282 g_assert_not_reached();
8283 }
8284}
8285
8286/* Return the SCTLR value which controls this address translation regime */
8287static inline uint32_t regime_sctlr(CPUARMState *env, ARMMMUIdx mmu_idx)
8288{
8289 return env->cp15.sctlr_el[regime_el(env, mmu_idx)];
8290}
8291
8292/* Return true if the specified stage of address translation is disabled */
8293static inline bool regime_translation_disabled(CPUARMState *env,
8294 ARMMMUIdx mmu_idx)
8295{
29c483a5 8296 if (arm_feature(env, ARM_FEATURE_M)) {
ecf5e8ea 8297 switch (env->v7m.mpu_ctrl[regime_is_secure(env, mmu_idx)] &
3bef7012
PM
8298 (R_V7M_MPU_CTRL_ENABLE_MASK | R_V7M_MPU_CTRL_HFNMIENA_MASK)) {
8299 case R_V7M_MPU_CTRL_ENABLE_MASK:
8300 /* Enabled, but not for HardFault and NMI */
62593718 8301 return mmu_idx & ARM_MMU_IDX_M_NEGPRI;
3bef7012
PM
8302 case R_V7M_MPU_CTRL_ENABLE_MASK | R_V7M_MPU_CTRL_HFNMIENA_MASK:
8303 /* Enabled for all cases */
8304 return false;
8305 case 0:
8306 default:
8307 /* HFNMIENA set and ENABLE clear is UNPREDICTABLE, but
8308 * we warned about that in armv7m_nvic.c when the guest set it.
8309 */
8310 return true;
8311 }
29c483a5
MD
8312 }
8313
0480f69a
PM
8314 if (mmu_idx == ARMMMUIdx_S2NS) {
8315 return (env->cp15.hcr_el2 & HCR_VM) == 0;
8316 }
8317 return (regime_sctlr(env, mmu_idx) & SCTLR_M) == 0;
8318}
8319
73462ddd
PC
8320static inline bool regime_translation_big_endian(CPUARMState *env,
8321 ARMMMUIdx mmu_idx)
8322{
8323 return (regime_sctlr(env, mmu_idx) & SCTLR_EE) != 0;
8324}
8325
0480f69a
PM
8326/* Return the TCR controlling this translation regime */
8327static inline TCR *regime_tcr(CPUARMState *env, ARMMMUIdx mmu_idx)
8328{
8329 if (mmu_idx == ARMMMUIdx_S2NS) {
68e9c2fe 8330 return &env->cp15.vtcr_el2;
0480f69a
PM
8331 }
8332 return &env->cp15.tcr_el[regime_el(env, mmu_idx)];
8333}
8334
8bd5c820
PM
8335/* Convert a possible stage1+2 MMU index into the appropriate
8336 * stage 1 MMU index
8337 */
8338static inline ARMMMUIdx stage_1_mmu_idx(ARMMMUIdx mmu_idx)
8339{
8340 if (mmu_idx == ARMMMUIdx_S12NSE0 || mmu_idx == ARMMMUIdx_S12NSE1) {
8341 mmu_idx += (ARMMMUIdx_S1NSE0 - ARMMMUIdx_S12NSE0);
8342 }
8343 return mmu_idx;
8344}
8345
86fb3fa4
TH
8346/* Returns TBI0 value for current regime el */
8347uint32_t arm_regime_tbi0(CPUARMState *env, ARMMMUIdx mmu_idx)
8348{
8349 TCR *tcr;
8350 uint32_t el;
8351
8352 /* For EL0 and EL1, TBI is controlled by stage 1's TCR, so convert
8bd5c820
PM
8353 * a stage 1+2 mmu index into the appropriate stage 1 mmu index.
8354 */
8355 mmu_idx = stage_1_mmu_idx(mmu_idx);
86fb3fa4
TH
8356
8357 tcr = regime_tcr(env, mmu_idx);
8358 el = regime_el(env, mmu_idx);
8359
8360 if (el > 1) {
8361 return extract64(tcr->raw_tcr, 20, 1);
8362 } else {
8363 return extract64(tcr->raw_tcr, 37, 1);
8364 }
8365}
8366
8367/* Returns TBI1 value for current regime el */
8368uint32_t arm_regime_tbi1(CPUARMState *env, ARMMMUIdx mmu_idx)
8369{
8370 TCR *tcr;
8371 uint32_t el;
8372
8373 /* For EL0 and EL1, TBI is controlled by stage 1's TCR, so convert
8bd5c820
PM
8374 * a stage 1+2 mmu index into the appropriate stage 1 mmu index.
8375 */
8376 mmu_idx = stage_1_mmu_idx(mmu_idx);
86fb3fa4
TH
8377
8378 tcr = regime_tcr(env, mmu_idx);
8379 el = regime_el(env, mmu_idx);
8380
8381 if (el > 1) {
8382 return 0;
8383 } else {
8384 return extract64(tcr->raw_tcr, 38, 1);
8385 }
8386}
8387
aef878be
GB
8388/* Return the TTBR associated with this translation regime */
8389static inline uint64_t regime_ttbr(CPUARMState *env, ARMMMUIdx mmu_idx,
8390 int ttbrn)
8391{
8392 if (mmu_idx == ARMMMUIdx_S2NS) {
b698e9cf 8393 return env->cp15.vttbr_el2;
aef878be
GB
8394 }
8395 if (ttbrn == 0) {
8396 return env->cp15.ttbr0_el[regime_el(env, mmu_idx)];
8397 } else {
8398 return env->cp15.ttbr1_el[regime_el(env, mmu_idx)];
8399 }
8400}
8401
0480f69a
PM
8402/* Return true if the translation regime is using LPAE format page tables */
8403static inline bool regime_using_lpae_format(CPUARMState *env,
8404 ARMMMUIdx mmu_idx)
8405{
8406 int el = regime_el(env, mmu_idx);
8407 if (el == 2 || arm_el_is_aa64(env, el)) {
8408 return true;
8409 }
8410 if (arm_feature(env, ARM_FEATURE_LPAE)
8411 && (regime_tcr(env, mmu_idx)->raw_tcr & TTBCR_EAE)) {
8412 return true;
8413 }
8414 return false;
8415}
8416
deb2db99
AR
8417/* Returns true if the stage 1 translation regime is using LPAE format page
8418 * tables. Used when raising alignment exceptions, whose FSR changes depending
8419 * on whether the long or short descriptor format is in use. */
8420bool arm_s1_regime_using_lpae_format(CPUARMState *env, ARMMMUIdx mmu_idx)
30901475 8421{
8bd5c820 8422 mmu_idx = stage_1_mmu_idx(mmu_idx);
deb2db99 8423
30901475
AB
8424 return regime_using_lpae_format(env, mmu_idx);
8425}
8426
0480f69a
PM
8427static inline bool regime_is_user(CPUARMState *env, ARMMMUIdx mmu_idx)
8428{
8429 switch (mmu_idx) {
8430 case ARMMMUIdx_S1SE0:
8431 case ARMMMUIdx_S1NSE0:
e7b921c2 8432 case ARMMMUIdx_MUser:
871bec7c 8433 case ARMMMUIdx_MSUser:
62593718
PM
8434 case ARMMMUIdx_MUserNegPri:
8435 case ARMMMUIdx_MSUserNegPri:
0480f69a
PM
8436 return true;
8437 default:
8438 return false;
8439 case ARMMMUIdx_S12NSE0:
8440 case ARMMMUIdx_S12NSE1:
8441 g_assert_not_reached();
8442 }
8443}
8444
0fbf5238
AJ
8445/* Translate section/page access permissions to page
8446 * R/W protection flags
d76951b6
AJ
8447 *
8448 * @env: CPUARMState
8449 * @mmu_idx: MMU index indicating required translation regime
8450 * @ap: The 3-bit access permissions (AP[2:0])
8451 * @domain_prot: The 2-bit domain access permissions
0fbf5238
AJ
8452 */
8453static inline int ap_to_rw_prot(CPUARMState *env, ARMMMUIdx mmu_idx,
8454 int ap, int domain_prot)
8455{
554b0b09
PM
8456 bool is_user = regime_is_user(env, mmu_idx);
8457
8458 if (domain_prot == 3) {
8459 return PAGE_READ | PAGE_WRITE;
8460 }
8461
554b0b09
PM
8462 switch (ap) {
8463 case 0:
8464 if (arm_feature(env, ARM_FEATURE_V7)) {
8465 return 0;
8466 }
554b0b09
PM
8467 switch (regime_sctlr(env, mmu_idx) & (SCTLR_S | SCTLR_R)) {
8468 case SCTLR_S:
8469 return is_user ? 0 : PAGE_READ;
8470 case SCTLR_R:
8471 return PAGE_READ;
8472 default:
8473 return 0;
8474 }
8475 case 1:
8476 return is_user ? 0 : PAGE_READ | PAGE_WRITE;
8477 case 2:
87c3d486 8478 if (is_user) {
0fbf5238 8479 return PAGE_READ;
87c3d486 8480 } else {
554b0b09 8481 return PAGE_READ | PAGE_WRITE;
87c3d486 8482 }
554b0b09
PM
8483 case 3:
8484 return PAGE_READ | PAGE_WRITE;
8485 case 4: /* Reserved. */
8486 return 0;
8487 case 5:
0fbf5238 8488 return is_user ? 0 : PAGE_READ;
554b0b09 8489 case 6:
0fbf5238 8490 return PAGE_READ;
554b0b09 8491 case 7:
87c3d486 8492 if (!arm_feature(env, ARM_FEATURE_V6K)) {
554b0b09 8493 return 0;
87c3d486 8494 }
0fbf5238 8495 return PAGE_READ;
554b0b09 8496 default:
0fbf5238 8497 g_assert_not_reached();
554b0b09 8498 }
b5ff1b31
FB
8499}
8500
d76951b6
AJ
8501/* Translate section/page access permissions to page
8502 * R/W protection flags.
8503 *
d76951b6 8504 * @ap: The 2-bit simple AP (AP[2:1])
d8e052b3 8505 * @is_user: TRUE if accessing from PL0
d76951b6 8506 */
d8e052b3 8507static inline int simple_ap_to_rw_prot_is_user(int ap, bool is_user)
d76951b6 8508{
d76951b6
AJ
8509 switch (ap) {
8510 case 0:
8511 return is_user ? 0 : PAGE_READ | PAGE_WRITE;
8512 case 1:
8513 return PAGE_READ | PAGE_WRITE;
8514 case 2:
8515 return is_user ? 0 : PAGE_READ;
8516 case 3:
8517 return PAGE_READ;
8518 default:
8519 g_assert_not_reached();
8520 }
8521}
8522
d8e052b3
AJ
8523static inline int
8524simple_ap_to_rw_prot(CPUARMState *env, ARMMMUIdx mmu_idx, int ap)
8525{
8526 return simple_ap_to_rw_prot_is_user(ap, regime_is_user(env, mmu_idx));
8527}
8528
6ab1a5ee
EI
8529/* Translate S2 section/page access permissions to protection flags
8530 *
8531 * @env: CPUARMState
8532 * @s2ap: The 2-bit stage2 access permissions (S2AP)
8533 * @xn: XN (execute-never) bit
8534 */
8535static int get_S2prot(CPUARMState *env, int s2ap, int xn)
8536{
8537 int prot = 0;
8538
8539 if (s2ap & 1) {
8540 prot |= PAGE_READ;
8541 }
8542 if (s2ap & 2) {
8543 prot |= PAGE_WRITE;
8544 }
8545 if (!xn) {
dfda6837
SS
8546 if (arm_el_is_aa64(env, 2) || prot & PAGE_READ) {
8547 prot |= PAGE_EXEC;
8548 }
6ab1a5ee
EI
8549 }
8550 return prot;
8551}
8552
d8e052b3
AJ
8553/* Translate section/page access permissions to protection flags
8554 *
8555 * @env: CPUARMState
8556 * @mmu_idx: MMU index indicating required translation regime
8557 * @is_aa64: TRUE if AArch64
8558 * @ap: The 2-bit simple AP (AP[2:1])
8559 * @ns: NS (non-secure) bit
8560 * @xn: XN (execute-never) bit
8561 * @pxn: PXN (privileged execute-never) bit
8562 */
8563static int get_S1prot(CPUARMState *env, ARMMMUIdx mmu_idx, bool is_aa64,
8564 int ap, int ns, int xn, int pxn)
8565{
8566 bool is_user = regime_is_user(env, mmu_idx);
8567 int prot_rw, user_rw;
8568 bool have_wxn;
8569 int wxn = 0;
8570
8571 assert(mmu_idx != ARMMMUIdx_S2NS);
8572
8573 user_rw = simple_ap_to_rw_prot_is_user(ap, true);
8574 if (is_user) {
8575 prot_rw = user_rw;
8576 } else {
8577 prot_rw = simple_ap_to_rw_prot_is_user(ap, false);
8578 }
8579
8580 if (ns && arm_is_secure(env) && (env->cp15.scr_el3 & SCR_SIF)) {
8581 return prot_rw;
8582 }
8583
8584 /* TODO have_wxn should be replaced with
8585 * ARM_FEATURE_V8 || (ARM_FEATURE_V7 && ARM_FEATURE_EL2)
8586 * when ARM_FEATURE_EL2 starts getting set. For now we assume all LPAE
8587 * compatible processors have EL2, which is required for [U]WXN.
8588 */
8589 have_wxn = arm_feature(env, ARM_FEATURE_LPAE);
8590
8591 if (have_wxn) {
8592 wxn = regime_sctlr(env, mmu_idx) & SCTLR_WXN;
8593 }
8594
8595 if (is_aa64) {
8596 switch (regime_el(env, mmu_idx)) {
8597 case 1:
8598 if (!is_user) {
8599 xn = pxn || (user_rw & PAGE_WRITE);
8600 }
8601 break;
8602 case 2:
8603 case 3:
8604 break;
8605 }
8606 } else if (arm_feature(env, ARM_FEATURE_V7)) {
8607 switch (regime_el(env, mmu_idx)) {
8608 case 1:
8609 case 3:
8610 if (is_user) {
8611 xn = xn || !(user_rw & PAGE_READ);
8612 } else {
8613 int uwxn = 0;
8614 if (have_wxn) {
8615 uwxn = regime_sctlr(env, mmu_idx) & SCTLR_UWXN;
8616 }
8617 xn = xn || !(prot_rw & PAGE_READ) || pxn ||
8618 (uwxn && (user_rw & PAGE_WRITE));
8619 }
8620 break;
8621 case 2:
8622 break;
8623 }
8624 } else {
8625 xn = wxn = 0;
8626 }
8627
8628 if (xn || (wxn && (prot_rw & PAGE_WRITE))) {
8629 return prot_rw;
8630 }
8631 return prot_rw | PAGE_EXEC;
8632}
8633
0480f69a
PM
8634static bool get_level1_table_address(CPUARMState *env, ARMMMUIdx mmu_idx,
8635 uint32_t *table, uint32_t address)
b2fa1797 8636{
0480f69a 8637 /* Note that we can only get here for an AArch32 PL0/PL1 lookup */
0480f69a 8638 TCR *tcr = regime_tcr(env, mmu_idx);
11f136ee 8639
11f136ee
FA
8640 if (address & tcr->mask) {
8641 if (tcr->raw_tcr & TTBCR_PD1) {
e389be16
FA
8642 /* Translation table walk disabled for TTBR1 */
8643 return false;
8644 }
aef878be 8645 *table = regime_ttbr(env, mmu_idx, 1) & 0xffffc000;
e389be16 8646 } else {
11f136ee 8647 if (tcr->raw_tcr & TTBCR_PD0) {
e389be16
FA
8648 /* Translation table walk disabled for TTBR0 */
8649 return false;
8650 }
aef878be 8651 *table = regime_ttbr(env, mmu_idx, 0) & tcr->base_mask;
e389be16
FA
8652 }
8653 *table |= (address >> 18) & 0x3ffc;
8654 return true;
b2fa1797
PB
8655}
8656
37785977
EI
8657/* Translate a S1 pagetable walk through S2 if needed. */
8658static hwaddr S1_ptw_translate(CPUARMState *env, ARMMMUIdx mmu_idx,
8659 hwaddr addr, MemTxAttrs txattrs,
37785977
EI
8660 ARMMMUFaultInfo *fi)
8661{
8662 if ((mmu_idx == ARMMMUIdx_S1NSE0 || mmu_idx == ARMMMUIdx_S1NSE1) &&
8663 !regime_translation_disabled(env, ARMMMUIdx_S2NS)) {
8664 target_ulong s2size;
8665 hwaddr s2pa;
8666 int s2prot;
8667 int ret;
8668
8669 ret = get_phys_addr_lpae(env, addr, 0, ARMMMUIdx_S2NS, &s2pa,
da909b2c 8670 &txattrs, &s2prot, &s2size, fi, NULL);
37785977 8671 if (ret) {
3b39d734 8672 assert(fi->type != ARMFault_None);
37785977
EI
8673 fi->s2addr = addr;
8674 fi->stage2 = true;
8675 fi->s1ptw = true;
8676 return ~0;
8677 }
8678 addr = s2pa;
8679 }
8680 return addr;
8681}
8682
ebca90e4
PM
8683/* All loads done in the course of a page table walk go through here.
8684 * TODO: rather than ignoring errors from physical memory reads (which
8685 * are external aborts in ARM terminology) we should propagate this
8686 * error out so that we can turn it into a Data Abort if this walk
8687 * was being done for a CPU load/store or an address translation instruction
8688 * (but not if it was for a debug access).
8689 */
a614e698 8690static uint32_t arm_ldl_ptw(CPUState *cs, hwaddr addr, bool is_secure,
3795a6de 8691 ARMMMUIdx mmu_idx, ARMMMUFaultInfo *fi)
ebca90e4 8692{
a614e698
EI
8693 ARMCPU *cpu = ARM_CPU(cs);
8694 CPUARMState *env = &cpu->env;
ebca90e4 8695 MemTxAttrs attrs = {};
3b39d734 8696 MemTxResult result = MEMTX_OK;
5ce4ff65 8697 AddressSpace *as;
3b39d734 8698 uint32_t data;
ebca90e4
PM
8699
8700 attrs.secure = is_secure;
5ce4ff65 8701 as = arm_addressspace(cs, attrs);
3795a6de 8702 addr = S1_ptw_translate(env, mmu_idx, addr, attrs, fi);
a614e698
EI
8703 if (fi->s1ptw) {
8704 return 0;
8705 }
73462ddd 8706 if (regime_translation_big_endian(env, mmu_idx)) {
3b39d734 8707 data = address_space_ldl_be(as, addr, attrs, &result);
73462ddd 8708 } else {
3b39d734 8709 data = address_space_ldl_le(as, addr, attrs, &result);
73462ddd 8710 }
3b39d734
PM
8711 if (result == MEMTX_OK) {
8712 return data;
8713 }
8714 fi->type = ARMFault_SyncExternalOnWalk;
8715 fi->ea = arm_extabort_type(result);
8716 return 0;
ebca90e4
PM
8717}
8718
37785977 8719static uint64_t arm_ldq_ptw(CPUState *cs, hwaddr addr, bool is_secure,
3795a6de 8720 ARMMMUIdx mmu_idx, ARMMMUFaultInfo *fi)
ebca90e4 8721{
37785977
EI
8722 ARMCPU *cpu = ARM_CPU(cs);
8723 CPUARMState *env = &cpu->env;
ebca90e4 8724 MemTxAttrs attrs = {};
3b39d734 8725 MemTxResult result = MEMTX_OK;
5ce4ff65 8726 AddressSpace *as;
9aea1ea3 8727 uint64_t data;
ebca90e4
PM
8728
8729 attrs.secure = is_secure;
5ce4ff65 8730 as = arm_addressspace(cs, attrs);
3795a6de 8731 addr = S1_ptw_translate(env, mmu_idx, addr, attrs, fi);
37785977
EI
8732 if (fi->s1ptw) {
8733 return 0;
8734 }
73462ddd 8735 if (regime_translation_big_endian(env, mmu_idx)) {
3b39d734 8736 data = address_space_ldq_be(as, addr, attrs, &result);
73462ddd 8737 } else {
3b39d734
PM
8738 data = address_space_ldq_le(as, addr, attrs, &result);
8739 }
8740 if (result == MEMTX_OK) {
8741 return data;
73462ddd 8742 }
3b39d734
PM
8743 fi->type = ARMFault_SyncExternalOnWalk;
8744 fi->ea = arm_extabort_type(result);
8745 return 0;
ebca90e4
PM
8746}
8747
b7cc4e82 8748static bool get_phys_addr_v5(CPUARMState *env, uint32_t address,
03ae85f8 8749 MMUAccessType access_type, ARMMMUIdx mmu_idx,
b7cc4e82 8750 hwaddr *phys_ptr, int *prot,
f989983e 8751 target_ulong *page_size,
e14b5a23 8752 ARMMMUFaultInfo *fi)
b5ff1b31 8753{
70d74660 8754 CPUState *cs = CPU(arm_env_get_cpu(env));
f989983e 8755 int level = 1;
b5ff1b31
FB
8756 uint32_t table;
8757 uint32_t desc;
8758 int type;
8759 int ap;
e389be16 8760 int domain = 0;
dd4ebc2e 8761 int domain_prot;
a8170e5e 8762 hwaddr phys_addr;
0480f69a 8763 uint32_t dacr;
b5ff1b31 8764
9ee6e8bb
PB
8765 /* Pagetable walk. */
8766 /* Lookup l1 descriptor. */
0480f69a 8767 if (!get_level1_table_address(env, mmu_idx, &table, address)) {
e389be16 8768 /* Section translation fault if page walk is disabled by PD0 or PD1 */
f989983e 8769 fi->type = ARMFault_Translation;
e389be16
FA
8770 goto do_fault;
8771 }
a614e698 8772 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
3795a6de 8773 mmu_idx, fi);
3b39d734
PM
8774 if (fi->type != ARMFault_None) {
8775 goto do_fault;
8776 }
9ee6e8bb 8777 type = (desc & 3);
dd4ebc2e 8778 domain = (desc >> 5) & 0x0f;
0480f69a
PM
8779 if (regime_el(env, mmu_idx) == 1) {
8780 dacr = env->cp15.dacr_ns;
8781 } else {
8782 dacr = env->cp15.dacr_s;
8783 }
8784 domain_prot = (dacr >> (domain * 2)) & 3;
9ee6e8bb 8785 if (type == 0) {
601d70b9 8786 /* Section translation fault. */
f989983e 8787 fi->type = ARMFault_Translation;
9ee6e8bb
PB
8788 goto do_fault;
8789 }
f989983e
PM
8790 if (type != 2) {
8791 level = 2;
8792 }
dd4ebc2e 8793 if (domain_prot == 0 || domain_prot == 2) {
f989983e 8794 fi->type = ARMFault_Domain;
9ee6e8bb
PB
8795 goto do_fault;
8796 }
8797 if (type == 2) {
8798 /* 1Mb section. */
8799 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
8800 ap = (desc >> 10) & 3;
d4c430a8 8801 *page_size = 1024 * 1024;
9ee6e8bb
PB
8802 } else {
8803 /* Lookup l2 entry. */
554b0b09
PM
8804 if (type == 1) {
8805 /* Coarse pagetable. */
8806 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
8807 } else {
8808 /* Fine pagetable. */
8809 table = (desc & 0xfffff000) | ((address >> 8) & 0xffc);
8810 }
a614e698 8811 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
3795a6de 8812 mmu_idx, fi);
3b39d734
PM
8813 if (fi->type != ARMFault_None) {
8814 goto do_fault;
8815 }
9ee6e8bb
PB
8816 switch (desc & 3) {
8817 case 0: /* Page translation fault. */
f989983e 8818 fi->type = ARMFault_Translation;
9ee6e8bb
PB
8819 goto do_fault;
8820 case 1: /* 64k page. */
8821 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
8822 ap = (desc >> (4 + ((address >> 13) & 6))) & 3;
d4c430a8 8823 *page_size = 0x10000;
ce819861 8824 break;
9ee6e8bb
PB
8825 case 2: /* 4k page. */
8826 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
c10f7fc3 8827 ap = (desc >> (4 + ((address >> 9) & 6))) & 3;
d4c430a8 8828 *page_size = 0x1000;
ce819861 8829 break;
fc1891c7 8830 case 3: /* 1k page, or ARMv6/XScale "extended small (4k) page" */
554b0b09 8831 if (type == 1) {
fc1891c7
PM
8832 /* ARMv6/XScale extended small page format */
8833 if (arm_feature(env, ARM_FEATURE_XSCALE)
8834 || arm_feature(env, ARM_FEATURE_V6)) {
554b0b09 8835 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
fc1891c7 8836 *page_size = 0x1000;
554b0b09 8837 } else {
fc1891c7
PM
8838 /* UNPREDICTABLE in ARMv5; we choose to take a
8839 * page translation fault.
8840 */
f989983e 8841 fi->type = ARMFault_Translation;
554b0b09
PM
8842 goto do_fault;
8843 }
8844 } else {
8845 phys_addr = (desc & 0xfffffc00) | (address & 0x3ff);
fc1891c7 8846 *page_size = 0x400;
554b0b09 8847 }
9ee6e8bb 8848 ap = (desc >> 4) & 3;
ce819861
PB
8849 break;
8850 default:
9ee6e8bb
PB
8851 /* Never happens, but compiler isn't smart enough to tell. */
8852 abort();
ce819861 8853 }
9ee6e8bb 8854 }
0fbf5238
AJ
8855 *prot = ap_to_rw_prot(env, mmu_idx, ap, domain_prot);
8856 *prot |= *prot ? PAGE_EXEC : 0;
8857 if (!(*prot & (1 << access_type))) {
9ee6e8bb 8858 /* Access permission fault. */
f989983e 8859 fi->type = ARMFault_Permission;
9ee6e8bb
PB
8860 goto do_fault;
8861 }
8862 *phys_ptr = phys_addr;
b7cc4e82 8863 return false;
9ee6e8bb 8864do_fault:
f989983e
PM
8865 fi->domain = domain;
8866 fi->level = level;
b7cc4e82 8867 return true;
9ee6e8bb
PB
8868}
8869
b7cc4e82 8870static bool get_phys_addr_v6(CPUARMState *env, uint32_t address,
03ae85f8 8871 MMUAccessType access_type, ARMMMUIdx mmu_idx,
b7cc4e82 8872 hwaddr *phys_ptr, MemTxAttrs *attrs, int *prot,
f06cf243 8873 target_ulong *page_size, ARMMMUFaultInfo *fi)
9ee6e8bb 8874{
70d74660 8875 CPUState *cs = CPU(arm_env_get_cpu(env));
f06cf243 8876 int level = 1;
9ee6e8bb
PB
8877 uint32_t table;
8878 uint32_t desc;
8879 uint32_t xn;
de9b05b8 8880 uint32_t pxn = 0;
9ee6e8bb
PB
8881 int type;
8882 int ap;
de9b05b8 8883 int domain = 0;
dd4ebc2e 8884 int domain_prot;
a8170e5e 8885 hwaddr phys_addr;
0480f69a 8886 uint32_t dacr;
8bf5b6a9 8887 bool ns;
9ee6e8bb
PB
8888
8889 /* Pagetable walk. */
8890 /* Lookup l1 descriptor. */
0480f69a 8891 if (!get_level1_table_address(env, mmu_idx, &table, address)) {
e389be16 8892 /* Section translation fault if page walk is disabled by PD0 or PD1 */
f06cf243 8893 fi->type = ARMFault_Translation;
e389be16
FA
8894 goto do_fault;
8895 }
a614e698 8896 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
3795a6de 8897 mmu_idx, fi);
3b39d734
PM
8898 if (fi->type != ARMFault_None) {
8899 goto do_fault;
8900 }
9ee6e8bb 8901 type = (desc & 3);
de9b05b8
PM
8902 if (type == 0 || (type == 3 && !arm_feature(env, ARM_FEATURE_PXN))) {
8903 /* Section translation fault, or attempt to use the encoding
8904 * which is Reserved on implementations without PXN.
8905 */
f06cf243 8906 fi->type = ARMFault_Translation;
9ee6e8bb 8907 goto do_fault;
de9b05b8
PM
8908 }
8909 if ((type == 1) || !(desc & (1 << 18))) {
8910 /* Page or Section. */
dd4ebc2e 8911 domain = (desc >> 5) & 0x0f;
9ee6e8bb 8912 }
0480f69a
PM
8913 if (regime_el(env, mmu_idx) == 1) {
8914 dacr = env->cp15.dacr_ns;
8915 } else {
8916 dacr = env->cp15.dacr_s;
8917 }
f06cf243
PM
8918 if (type == 1) {
8919 level = 2;
8920 }
0480f69a 8921 domain_prot = (dacr >> (domain * 2)) & 3;
dd4ebc2e 8922 if (domain_prot == 0 || domain_prot == 2) {
f06cf243
PM
8923 /* Section or Page domain fault */
8924 fi->type = ARMFault_Domain;
9ee6e8bb
PB
8925 goto do_fault;
8926 }
de9b05b8 8927 if (type != 1) {
9ee6e8bb
PB
8928 if (desc & (1 << 18)) {
8929 /* Supersection. */
8930 phys_addr = (desc & 0xff000000) | (address & 0x00ffffff);
4e42a6ca
SF
8931 phys_addr |= (uint64_t)extract32(desc, 20, 4) << 32;
8932 phys_addr |= (uint64_t)extract32(desc, 5, 4) << 36;
d4c430a8 8933 *page_size = 0x1000000;
b5ff1b31 8934 } else {
9ee6e8bb
PB
8935 /* Section. */
8936 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
d4c430a8 8937 *page_size = 0x100000;
b5ff1b31 8938 }
9ee6e8bb
PB
8939 ap = ((desc >> 10) & 3) | ((desc >> 13) & 4);
8940 xn = desc & (1 << 4);
de9b05b8 8941 pxn = desc & 1;
8bf5b6a9 8942 ns = extract32(desc, 19, 1);
9ee6e8bb 8943 } else {
de9b05b8
PM
8944 if (arm_feature(env, ARM_FEATURE_PXN)) {
8945 pxn = (desc >> 2) & 1;
8946 }
8bf5b6a9 8947 ns = extract32(desc, 3, 1);
9ee6e8bb
PB
8948 /* Lookup l2 entry. */
8949 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
a614e698 8950 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
3795a6de 8951 mmu_idx, fi);
3b39d734
PM
8952 if (fi->type != ARMFault_None) {
8953 goto do_fault;
8954 }
9ee6e8bb
PB
8955 ap = ((desc >> 4) & 3) | ((desc >> 7) & 4);
8956 switch (desc & 3) {
8957 case 0: /* Page translation fault. */
f06cf243 8958 fi->type = ARMFault_Translation;
b5ff1b31 8959 goto do_fault;
9ee6e8bb
PB
8960 case 1: /* 64k page. */
8961 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
8962 xn = desc & (1 << 15);
d4c430a8 8963 *page_size = 0x10000;
9ee6e8bb
PB
8964 break;
8965 case 2: case 3: /* 4k page. */
8966 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
8967 xn = desc & 1;
d4c430a8 8968 *page_size = 0x1000;
9ee6e8bb
PB
8969 break;
8970 default:
8971 /* Never happens, but compiler isn't smart enough to tell. */
8972 abort();
b5ff1b31 8973 }
9ee6e8bb 8974 }
dd4ebc2e 8975 if (domain_prot == 3) {
c0034328
JR
8976 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
8977 } else {
0480f69a 8978 if (pxn && !regime_is_user(env, mmu_idx)) {
de9b05b8
PM
8979 xn = 1;
8980 }
f06cf243
PM
8981 if (xn && access_type == MMU_INST_FETCH) {
8982 fi->type = ARMFault_Permission;
c0034328 8983 goto do_fault;
f06cf243 8984 }
9ee6e8bb 8985
d76951b6
AJ
8986 if (arm_feature(env, ARM_FEATURE_V6K) &&
8987 (regime_sctlr(env, mmu_idx) & SCTLR_AFE)) {
8988 /* The simplified model uses AP[0] as an access control bit. */
8989 if ((ap & 1) == 0) {
8990 /* Access flag fault. */
f06cf243 8991 fi->type = ARMFault_AccessFlag;
d76951b6
AJ
8992 goto do_fault;
8993 }
8994 *prot = simple_ap_to_rw_prot(env, mmu_idx, ap >> 1);
8995 } else {
8996 *prot = ap_to_rw_prot(env, mmu_idx, ap, domain_prot);
c0034328 8997 }
0fbf5238
AJ
8998 if (*prot && !xn) {
8999 *prot |= PAGE_EXEC;
9000 }
9001 if (!(*prot & (1 << access_type))) {
c0034328 9002 /* Access permission fault. */
f06cf243 9003 fi->type = ARMFault_Permission;
c0034328
JR
9004 goto do_fault;
9005 }
3ad493fc 9006 }
8bf5b6a9
PM
9007 if (ns) {
9008 /* The NS bit will (as required by the architecture) have no effect if
9009 * the CPU doesn't support TZ or this is a non-secure translation
9010 * regime, because the attribute will already be non-secure.
9011 */
9012 attrs->secure = false;
9013 }
9ee6e8bb 9014 *phys_ptr = phys_addr;
b7cc4e82 9015 return false;
b5ff1b31 9016do_fault:
f06cf243
PM
9017 fi->domain = domain;
9018 fi->level = level;
b7cc4e82 9019 return true;
b5ff1b31
FB
9020}
9021
1853d5a9 9022/*
a0e966c9 9023 * check_s2_mmu_setup
1853d5a9
EI
9024 * @cpu: ARMCPU
9025 * @is_aa64: True if the translation regime is in AArch64 state
9026 * @startlevel: Suggested starting level
9027 * @inputsize: Bitsize of IPAs
9028 * @stride: Page-table stride (See the ARM ARM)
9029 *
a0e966c9
EI
9030 * Returns true if the suggested S2 translation parameters are OK and
9031 * false otherwise.
1853d5a9 9032 */
a0e966c9
EI
9033static bool check_s2_mmu_setup(ARMCPU *cpu, bool is_aa64, int level,
9034 int inputsize, int stride)
1853d5a9 9035{
98d68ec2
EI
9036 const int grainsize = stride + 3;
9037 int startsizecheck;
9038
1853d5a9
EI
9039 /* Negative levels are never allowed. */
9040 if (level < 0) {
9041 return false;
9042 }
9043
98d68ec2
EI
9044 startsizecheck = inputsize - ((3 - level) * stride + grainsize);
9045 if (startsizecheck < 1 || startsizecheck > stride + 4) {
9046 return false;
9047 }
9048
1853d5a9 9049 if (is_aa64) {
3526423e 9050 CPUARMState *env = &cpu->env;
1853d5a9
EI
9051 unsigned int pamax = arm_pamax(cpu);
9052
9053 switch (stride) {
9054 case 13: /* 64KB Pages. */
9055 if (level == 0 || (level == 1 && pamax <= 42)) {
9056 return false;
9057 }
9058 break;
9059 case 11: /* 16KB Pages. */
9060 if (level == 0 || (level == 1 && pamax <= 40)) {
9061 return false;
9062 }
9063 break;
9064 case 9: /* 4KB Pages. */
9065 if (level == 0 && pamax <= 42) {
9066 return false;
9067 }
9068 break;
9069 default:
9070 g_assert_not_reached();
9071 }
3526423e
EI
9072
9073 /* Inputsize checks. */
9074 if (inputsize > pamax &&
9075 (arm_el_is_aa64(env, 1) || inputsize > 40)) {
9076 /* This is CONSTRAINED UNPREDICTABLE and we choose to fault. */
9077 return false;
9078 }
1853d5a9 9079 } else {
1853d5a9
EI
9080 /* AArch32 only supports 4KB pages. Assert on that. */
9081 assert(stride == 9);
9082
9083 if (level == 0) {
9084 return false;
9085 }
1853d5a9
EI
9086 }
9087 return true;
9088}
9089
5b2d261d
AB
9090/* Translate from the 4-bit stage 2 representation of
9091 * memory attributes (without cache-allocation hints) to
9092 * the 8-bit representation of the stage 1 MAIR registers
9093 * (which includes allocation hints).
9094 *
9095 * ref: shared/translation/attrs/S2AttrDecode()
9096 * .../S2ConvertAttrsHints()
9097 */
9098static uint8_t convert_stage2_attrs(CPUARMState *env, uint8_t s2attrs)
9099{
9100 uint8_t hiattr = extract32(s2attrs, 2, 2);
9101 uint8_t loattr = extract32(s2attrs, 0, 2);
9102 uint8_t hihint = 0, lohint = 0;
9103
9104 if (hiattr != 0) { /* normal memory */
9105 if ((env->cp15.hcr_el2 & HCR_CD) != 0) { /* cache disabled */
9106 hiattr = loattr = 1; /* non-cacheable */
9107 } else {
9108 if (hiattr != 1) { /* Write-through or write-back */
9109 hihint = 3; /* RW allocate */
9110 }
9111 if (loattr != 1) { /* Write-through or write-back */
9112 lohint = 3; /* RW allocate */
9113 }
9114 }
9115 }
9116
9117 return (hiattr << 6) | (hihint << 4) | (loattr << 2) | lohint;
9118}
9119
b7cc4e82 9120static bool get_phys_addr_lpae(CPUARMState *env, target_ulong address,
03ae85f8 9121 MMUAccessType access_type, ARMMMUIdx mmu_idx,
b7cc4e82 9122 hwaddr *phys_ptr, MemTxAttrs *txattrs, int *prot,
da909b2c 9123 target_ulong *page_size_ptr,
5b2d261d 9124 ARMMMUFaultInfo *fi, ARMCacheAttrs *cacheattrs)
3dde962f 9125{
1853d5a9
EI
9126 ARMCPU *cpu = arm_env_get_cpu(env);
9127 CPUState *cs = CPU(cpu);
3dde962f 9128 /* Read an LPAE long-descriptor translation table. */
da909b2c 9129 ARMFaultType fault_type = ARMFault_Translation;
1b4093ea 9130 uint32_t level;
0c5fbf3b 9131 uint32_t epd = 0;
1f4c8c18 9132 int32_t t0sz, t1sz;
2c8dd318 9133 uint32_t tg;
3dde962f
PM
9134 uint64_t ttbr;
9135 int ttbr_select;
dddb5223 9136 hwaddr descaddr, indexmask, indexmask_grainsize;
3dde962f
PM
9137 uint32_t tableattrs;
9138 target_ulong page_size;
9139 uint32_t attrs;
973a5434 9140 int32_t stride = 9;
6e99f762 9141 int32_t addrsize;
4ca6a051 9142 int inputsize;
2c8dd318 9143 int32_t tbi = 0;
0480f69a 9144 TCR *tcr = regime_tcr(env, mmu_idx);
d8e052b3 9145 int ap, ns, xn, pxn;
88e8add8
GB
9146 uint32_t el = regime_el(env, mmu_idx);
9147 bool ttbr1_valid = true;
6109769a 9148 uint64_t descaddrmask;
6e99f762 9149 bool aarch64 = arm_el_is_aa64(env, el);
0480f69a
PM
9150
9151 /* TODO:
88e8add8
GB
9152 * This code does not handle the different format TCR for VTCR_EL2.
9153 * This code also does not support shareability levels.
9154 * Attribute and permission bit handling should also be checked when adding
9155 * support for those page table walks.
0480f69a 9156 */
6e99f762 9157 if (aarch64) {
1b4093ea 9158 level = 0;
6e99f762 9159 addrsize = 64;
88e8add8 9160 if (el > 1) {
1edee470
EI
9161 if (mmu_idx != ARMMMUIdx_S2NS) {
9162 tbi = extract64(tcr->raw_tcr, 20, 1);
9163 }
88e8add8
GB
9164 } else {
9165 if (extract64(address, 55, 1)) {
9166 tbi = extract64(tcr->raw_tcr, 38, 1);
9167 } else {
9168 tbi = extract64(tcr->raw_tcr, 37, 1);
9169 }
9170 }
2c8dd318 9171 tbi *= 8;
88e8add8
GB
9172
9173 /* If we are in 64-bit EL2 or EL3 then there is no TTBR1, so mark it
9174 * invalid.
9175 */
9176 if (el > 1) {
9177 ttbr1_valid = false;
9178 }
d0a2cbce 9179 } else {
1b4093ea 9180 level = 1;
6e99f762 9181 addrsize = 32;
d0a2cbce
PM
9182 /* There is no TTBR1 for EL2 */
9183 if (el == 2) {
9184 ttbr1_valid = false;
9185 }
2c8dd318 9186 }
3dde962f
PM
9187
9188 /* Determine whether this address is in the region controlled by
9189 * TTBR0 or TTBR1 (or if it is in neither region and should fault).
9190 * This is a Non-secure PL0/1 stage 1 translation, so controlled by
9191 * TTBCR/TTBR0/TTBR1 in accordance with ARM ARM DDI0406C table B-32:
9192 */
6e99f762 9193 if (aarch64) {
4ee38098
EI
9194 /* AArch64 translation. */
9195 t0sz = extract32(tcr->raw_tcr, 0, 6);
2c8dd318
RH
9196 t0sz = MIN(t0sz, 39);
9197 t0sz = MAX(t0sz, 16);
4ee38098
EI
9198 } else if (mmu_idx != ARMMMUIdx_S2NS) {
9199 /* AArch32 stage 1 translation. */
9200 t0sz = extract32(tcr->raw_tcr, 0, 3);
9201 } else {
9202 /* AArch32 stage 2 translation. */
9203 bool sext = extract32(tcr->raw_tcr, 4, 1);
9204 bool sign = extract32(tcr->raw_tcr, 3, 1);
6e99f762
SS
9205 /* Address size is 40-bit for a stage 2 translation,
9206 * and t0sz can be negative (from -8 to 7),
9207 * so we need to adjust it to use the TTBR selecting logic below.
9208 */
9209 addrsize = 40;
9210 t0sz = sextract32(tcr->raw_tcr, 0, 4) + 8;
4ee38098
EI
9211
9212 /* If the sign-extend bit is not the same as t0sz[3], the result
9213 * is unpredictable. Flag this as a guest error. */
9214 if (sign != sext) {
9215 qemu_log_mask(LOG_GUEST_ERROR,
39cba610 9216 "AArch32: VTCR.S / VTCR.T0SZ[3] mismatch\n");
4ee38098 9217 }
2c8dd318 9218 }
1f4c8c18 9219 t1sz = extract32(tcr->raw_tcr, 16, 6);
6e99f762 9220 if (aarch64) {
2c8dd318
RH
9221 t1sz = MIN(t1sz, 39);
9222 t1sz = MAX(t1sz, 16);
9223 }
6e99f762 9224 if (t0sz && !extract64(address, addrsize - t0sz, t0sz - tbi)) {
3dde962f
PM
9225 /* there is a ttbr0 region and we are in it (high bits all zero) */
9226 ttbr_select = 0;
88e8add8 9227 } else if (ttbr1_valid && t1sz &&
6e99f762 9228 !extract64(~address, addrsize - t1sz, t1sz - tbi)) {
3dde962f
PM
9229 /* there is a ttbr1 region and we are in it (high bits all one) */
9230 ttbr_select = 1;
9231 } else if (!t0sz) {
9232 /* ttbr0 region is "everything not in the ttbr1 region" */
9233 ttbr_select = 0;
88e8add8 9234 } else if (!t1sz && ttbr1_valid) {
3dde962f
PM
9235 /* ttbr1 region is "everything not in the ttbr0 region" */
9236 ttbr_select = 1;
9237 } else {
9238 /* in the gap between the two regions, this is a Translation fault */
da909b2c 9239 fault_type = ARMFault_Translation;
3dde962f
PM
9240 goto do_fault;
9241 }
9242
9243 /* Note that QEMU ignores shareability and cacheability attributes,
9244 * so we don't need to do anything with the SH, ORGN, IRGN fields
9245 * in the TTBCR. Similarly, TTBCR:A1 selects whether we get the
9246 * ASID from TTBR0 or TTBR1, but QEMU's TLB doesn't currently
9247 * implement any ASID-like capability so we can ignore it (instead
9248 * we will always flush the TLB any time the ASID is changed).
9249 */
9250 if (ttbr_select == 0) {
aef878be 9251 ttbr = regime_ttbr(env, mmu_idx, 0);
0c5fbf3b
EI
9252 if (el < 2) {
9253 epd = extract32(tcr->raw_tcr, 7, 1);
9254 }
6e99f762 9255 inputsize = addrsize - t0sz;
2c8dd318 9256
11f136ee 9257 tg = extract32(tcr->raw_tcr, 14, 2);
2c8dd318 9258 if (tg == 1) { /* 64KB pages */
973a5434 9259 stride = 13;
2c8dd318
RH
9260 }
9261 if (tg == 2) { /* 16KB pages */
973a5434 9262 stride = 11;
2c8dd318 9263 }
3dde962f 9264 } else {
88e8add8
GB
9265 /* We should only be here if TTBR1 is valid */
9266 assert(ttbr1_valid);
9267
aef878be 9268 ttbr = regime_ttbr(env, mmu_idx, 1);
11f136ee 9269 epd = extract32(tcr->raw_tcr, 23, 1);
6e99f762 9270 inputsize = addrsize - t1sz;
2c8dd318 9271
11f136ee 9272 tg = extract32(tcr->raw_tcr, 30, 2);
2c8dd318 9273 if (tg == 3) { /* 64KB pages */
973a5434 9274 stride = 13;
2c8dd318
RH
9275 }
9276 if (tg == 1) { /* 16KB pages */
973a5434 9277 stride = 11;
2c8dd318 9278 }
3dde962f
PM
9279 }
9280
0480f69a 9281 /* Here we should have set up all the parameters for the translation:
6e99f762 9282 * inputsize, ttbr, epd, stride, tbi
0480f69a
PM
9283 */
9284
3dde962f 9285 if (epd) {
88e8add8
GB
9286 /* Translation table walk disabled => Translation fault on TLB miss
9287 * Note: This is always 0 on 64-bit EL2 and EL3.
9288 */
3dde962f
PM
9289 goto do_fault;
9290 }
9291
1853d5a9
EI
9292 if (mmu_idx != ARMMMUIdx_S2NS) {
9293 /* The starting level depends on the virtual address size (which can
9294 * be up to 48 bits) and the translation granule size. It indicates
9295 * the number of strides (stride bits at a time) needed to
9296 * consume the bits of the input address. In the pseudocode this is:
9297 * level = 4 - RoundUp((inputsize - grainsize) / stride)
9298 * where their 'inputsize' is our 'inputsize', 'grainsize' is
9299 * our 'stride + 3' and 'stride' is our 'stride'.
9300 * Applying the usual "rounded up m/n is (m+n-1)/n" and simplifying:
9301 * = 4 - (inputsize - stride - 3 + stride - 1) / stride
9302 * = 4 - (inputsize - 4) / stride;
9303 */
9304 level = 4 - (inputsize - 4) / stride;
9305 } else {
9306 /* For stage 2 translations the starting level is specified by the
9307 * VTCR_EL2.SL0 field (whose interpretation depends on the page size)
9308 */
1b4093ea
SS
9309 uint32_t sl0 = extract32(tcr->raw_tcr, 6, 2);
9310 uint32_t startlevel;
1853d5a9
EI
9311 bool ok;
9312
6e99f762 9313 if (!aarch64 || stride == 9) {
1853d5a9 9314 /* AArch32 or 4KB pages */
1b4093ea 9315 startlevel = 2 - sl0;
1853d5a9
EI
9316 } else {
9317 /* 16KB or 64KB pages */
1b4093ea 9318 startlevel = 3 - sl0;
1853d5a9
EI
9319 }
9320
9321 /* Check that the starting level is valid. */
6e99f762 9322 ok = check_s2_mmu_setup(cpu, aarch64, startlevel,
1b4093ea 9323 inputsize, stride);
1853d5a9 9324 if (!ok) {
da909b2c 9325 fault_type = ARMFault_Translation;
1853d5a9
EI
9326 goto do_fault;
9327 }
1b4093ea 9328 level = startlevel;
1853d5a9 9329 }
3dde962f 9330
dddb5223
SS
9331 indexmask_grainsize = (1ULL << (stride + 3)) - 1;
9332 indexmask = (1ULL << (inputsize - (stride * (4 - level)))) - 1;
3dde962f
PM
9333
9334 /* Now we can extract the actual base address from the TTBR */
2c8dd318 9335 descaddr = extract64(ttbr, 0, 48);
dddb5223 9336 descaddr &= ~indexmask;
3dde962f 9337
6109769a 9338 /* The address field in the descriptor goes up to bit 39 for ARMv7
dddb5223
SS
9339 * but up to bit 47 for ARMv8, but we use the descaddrmask
9340 * up to bit 39 for AArch32, because we don't need other bits in that case
9341 * to construct next descriptor address (anyway they should be all zeroes).
6109769a 9342 */
6e99f762 9343 descaddrmask = ((1ull << (aarch64 ? 48 : 40)) - 1) &
dddb5223 9344 ~indexmask_grainsize;
6109769a 9345
ebca90e4
PM
9346 /* Secure accesses start with the page table in secure memory and
9347 * can be downgraded to non-secure at any step. Non-secure accesses
9348 * remain non-secure. We implement this by just ORing in the NSTable/NS
9349 * bits at each step.
9350 */
9351 tableattrs = regime_is_secure(env, mmu_idx) ? 0 : (1 << 4);
3dde962f
PM
9352 for (;;) {
9353 uint64_t descriptor;
ebca90e4 9354 bool nstable;
3dde962f 9355
dddb5223 9356 descaddr |= (address >> (stride * (4 - level))) & indexmask;
2c8dd318 9357 descaddr &= ~7ULL;
ebca90e4 9358 nstable = extract32(tableattrs, 4, 1);
3795a6de 9359 descriptor = arm_ldq_ptw(cs, descaddr, !nstable, mmu_idx, fi);
3b39d734 9360 if (fi->type != ARMFault_None) {
37785977
EI
9361 goto do_fault;
9362 }
9363
3dde962f
PM
9364 if (!(descriptor & 1) ||
9365 (!(descriptor & 2) && (level == 3))) {
9366 /* Invalid, or the Reserved level 3 encoding */
9367 goto do_fault;
9368 }
6109769a 9369 descaddr = descriptor & descaddrmask;
3dde962f
PM
9370
9371 if ((descriptor & 2) && (level < 3)) {
9372 /* Table entry. The top five bits are attributes which may
9373 * propagate down through lower levels of the table (and
9374 * which are all arranged so that 0 means "no effect", so
9375 * we can gather them up by ORing in the bits at each level).
9376 */
9377 tableattrs |= extract64(descriptor, 59, 5);
9378 level++;
dddb5223 9379 indexmask = indexmask_grainsize;
3dde962f
PM
9380 continue;
9381 }
9382 /* Block entry at level 1 or 2, or page entry at level 3.
9383 * These are basically the same thing, although the number
9384 * of bits we pull in from the vaddr varies.
9385 */
973a5434 9386 page_size = (1ULL << ((stride * (4 - level)) + 3));
3dde962f 9387 descaddr |= (address & (page_size - 1));
6ab1a5ee 9388 /* Extract attributes from the descriptor */
d615efac
IC
9389 attrs = extract64(descriptor, 2, 10)
9390 | (extract64(descriptor, 52, 12) << 10);
6ab1a5ee
EI
9391
9392 if (mmu_idx == ARMMMUIdx_S2NS) {
9393 /* Stage 2 table descriptors do not include any attribute fields */
9394 break;
9395 }
9396 /* Merge in attributes from table descriptors */
3dde962f
PM
9397 attrs |= extract32(tableattrs, 0, 2) << 11; /* XN, PXN */
9398 attrs |= extract32(tableattrs, 3, 1) << 5; /* APTable[1] => AP[2] */
9399 /* The sense of AP[1] vs APTable[0] is reversed, as APTable[0] == 1
9400 * means "force PL1 access only", which means forcing AP[1] to 0.
9401 */
9402 if (extract32(tableattrs, 2, 1)) {
9403 attrs &= ~(1 << 4);
9404 }
ebca90e4 9405 attrs |= nstable << 3; /* NS */
3dde962f
PM
9406 break;
9407 }
9408 /* Here descaddr is the final physical address, and attributes
9409 * are all in attrs.
9410 */
da909b2c 9411 fault_type = ARMFault_AccessFlag;
3dde962f
PM
9412 if ((attrs & (1 << 8)) == 0) {
9413 /* Access flag */
9414 goto do_fault;
9415 }
d8e052b3
AJ
9416
9417 ap = extract32(attrs, 4, 2);
d8e052b3 9418 xn = extract32(attrs, 12, 1);
d8e052b3 9419
6ab1a5ee
EI
9420 if (mmu_idx == ARMMMUIdx_S2NS) {
9421 ns = true;
9422 *prot = get_S2prot(env, ap, xn);
9423 } else {
9424 ns = extract32(attrs, 3, 1);
9425 pxn = extract32(attrs, 11, 1);
6e99f762 9426 *prot = get_S1prot(env, mmu_idx, aarch64, ap, ns, xn, pxn);
6ab1a5ee 9427 }
d8e052b3 9428
da909b2c 9429 fault_type = ARMFault_Permission;
d8e052b3 9430 if (!(*prot & (1 << access_type))) {
3dde962f
PM
9431 goto do_fault;
9432 }
3dde962f 9433
8bf5b6a9
PM
9434 if (ns) {
9435 /* The NS bit will (as required by the architecture) have no effect if
9436 * the CPU doesn't support TZ or this is a non-secure translation
9437 * regime, because the attribute will already be non-secure.
9438 */
9439 txattrs->secure = false;
9440 }
5b2d261d
AB
9441
9442 if (cacheattrs != NULL) {
9443 if (mmu_idx == ARMMMUIdx_S2NS) {
9444 cacheattrs->attrs = convert_stage2_attrs(env,
9445 extract32(attrs, 0, 4));
9446 } else {
9447 /* Index into MAIR registers for cache attributes */
9448 uint8_t attrindx = extract32(attrs, 0, 3);
9449 uint64_t mair = env->cp15.mair_el[regime_el(env, mmu_idx)];
9450 assert(attrindx <= 7);
9451 cacheattrs->attrs = extract64(mair, attrindx * 8, 8);
9452 }
9453 cacheattrs->shareability = extract32(attrs, 6, 2);
9454 }
9455
3dde962f
PM
9456 *phys_ptr = descaddr;
9457 *page_size_ptr = page_size;
b7cc4e82 9458 return false;
3dde962f
PM
9459
9460do_fault:
da909b2c
PM
9461 fi->type = fault_type;
9462 fi->level = level;
37785977
EI
9463 /* Tag the error as S2 for failed S1 PTW at S2 or ordinary S2. */
9464 fi->stage2 = fi->s1ptw || (mmu_idx == ARMMMUIdx_S2NS);
b7cc4e82 9465 return true;
3dde962f
PM
9466}
9467
f6bda88f
PC
9468static inline void get_phys_addr_pmsav7_default(CPUARMState *env,
9469 ARMMMUIdx mmu_idx,
9470 int32_t address, int *prot)
9471{
3a00d560
MD
9472 if (!arm_feature(env, ARM_FEATURE_M)) {
9473 *prot = PAGE_READ | PAGE_WRITE;
9474 switch (address) {
9475 case 0xF0000000 ... 0xFFFFFFFF:
9476 if (regime_sctlr(env, mmu_idx) & SCTLR_V) {
9477 /* hivecs execing is ok */
9478 *prot |= PAGE_EXEC;
9479 }
9480 break;
9481 case 0x00000000 ... 0x7FFFFFFF:
f6bda88f 9482 *prot |= PAGE_EXEC;
3a00d560
MD
9483 break;
9484 }
9485 } else {
9486 /* Default system address map for M profile cores.
9487 * The architecture specifies which regions are execute-never;
9488 * at the MPU level no other checks are defined.
9489 */
9490 switch (address) {
9491 case 0x00000000 ... 0x1fffffff: /* ROM */
9492 case 0x20000000 ... 0x3fffffff: /* SRAM */
9493 case 0x60000000 ... 0x7fffffff: /* RAM */
9494 case 0x80000000 ... 0x9fffffff: /* RAM */
9495 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
9496 break;
9497 case 0x40000000 ... 0x5fffffff: /* Peripheral */
9498 case 0xa0000000 ... 0xbfffffff: /* Device */
9499 case 0xc0000000 ... 0xdfffffff: /* Device */
9500 case 0xe0000000 ... 0xffffffff: /* System */
9501 *prot = PAGE_READ | PAGE_WRITE;
9502 break;
9503 default:
9504 g_assert_not_reached();
f6bda88f 9505 }
f6bda88f 9506 }
f6bda88f
PC
9507}
9508
29c483a5
MD
9509static bool pmsav7_use_background_region(ARMCPU *cpu,
9510 ARMMMUIdx mmu_idx, bool is_user)
9511{
9512 /* Return true if we should use the default memory map as a
9513 * "background" region if there are no hits against any MPU regions.
9514 */
9515 CPUARMState *env = &cpu->env;
9516
9517 if (is_user) {
9518 return false;
9519 }
9520
9521 if (arm_feature(env, ARM_FEATURE_M)) {
ecf5e8ea
PM
9522 return env->v7m.mpu_ctrl[regime_is_secure(env, mmu_idx)]
9523 & R_V7M_MPU_CTRL_PRIVDEFENA_MASK;
29c483a5
MD
9524 } else {
9525 return regime_sctlr(env, mmu_idx) & SCTLR_BR;
9526 }
9527}
9528
38aaa60c
PM
9529static inline bool m_is_ppb_region(CPUARMState *env, uint32_t address)
9530{
9531 /* True if address is in the M profile PPB region 0xe0000000 - 0xe00fffff */
9532 return arm_feature(env, ARM_FEATURE_M) &&
9533 extract32(address, 20, 12) == 0xe00;
9534}
9535
bf446a11
PM
9536static inline bool m_is_system_region(CPUARMState *env, uint32_t address)
9537{
9538 /* True if address is in the M profile system region
9539 * 0xe0000000 - 0xffffffff
9540 */
9541 return arm_feature(env, ARM_FEATURE_M) && extract32(address, 29, 3) == 0x7;
9542}
9543
f6bda88f 9544static bool get_phys_addr_pmsav7(CPUARMState *env, uint32_t address,
03ae85f8 9545 MMUAccessType access_type, ARMMMUIdx mmu_idx,
9375ad15
PM
9546 hwaddr *phys_ptr, int *prot,
9547 ARMMMUFaultInfo *fi)
f6bda88f
PC
9548{
9549 ARMCPU *cpu = arm_env_get_cpu(env);
9550 int n;
9551 bool is_user = regime_is_user(env, mmu_idx);
9552
9553 *phys_ptr = address;
9554 *prot = 0;
9555
38aaa60c
PM
9556 if (regime_translation_disabled(env, mmu_idx) ||
9557 m_is_ppb_region(env, address)) {
9558 /* MPU disabled or M profile PPB access: use default memory map.
9559 * The other case which uses the default memory map in the
9560 * v7M ARM ARM pseudocode is exception vector reads from the vector
9561 * table. In QEMU those accesses are done in arm_v7m_load_vector(),
9562 * which always does a direct read using address_space_ldl(), rather
9563 * than going via this function, so we don't need to check that here.
9564 */
f6bda88f
PC
9565 get_phys_addr_pmsav7_default(env, mmu_idx, address, prot);
9566 } else { /* MPU enabled */
9567 for (n = (int)cpu->pmsav7_dregion - 1; n >= 0; n--) {
9568 /* region search */
9569 uint32_t base = env->pmsav7.drbar[n];
9570 uint32_t rsize = extract32(env->pmsav7.drsr[n], 1, 5);
9571 uint32_t rmask;
9572 bool srdis = false;
9573
9574 if (!(env->pmsav7.drsr[n] & 0x1)) {
9575 continue;
9576 }
9577
9578 if (!rsize) {
c9f9f124
MD
9579 qemu_log_mask(LOG_GUEST_ERROR,
9580 "DRSR[%d]: Rsize field cannot be 0\n", n);
f6bda88f
PC
9581 continue;
9582 }
9583 rsize++;
9584 rmask = (1ull << rsize) - 1;
9585
9586 if (base & rmask) {
c9f9f124
MD
9587 qemu_log_mask(LOG_GUEST_ERROR,
9588 "DRBAR[%d]: 0x%" PRIx32 " misaligned "
9589 "to DRSR region size, mask = 0x%" PRIx32 "\n",
9590 n, base, rmask);
f6bda88f
PC
9591 continue;
9592 }
9593
9594 if (address < base || address > base + rmask) {
9595 continue;
9596 }
9597
9598 /* Region matched */
9599
9600 if (rsize >= 8) { /* no subregions for regions < 256 bytes */
9601 int i, snd;
9602 uint32_t srdis_mask;
9603
9604 rsize -= 3; /* sub region size (power of 2) */
9605 snd = ((address - base) >> rsize) & 0x7;
9606 srdis = extract32(env->pmsav7.drsr[n], snd + 8, 1);
9607
9608 srdis_mask = srdis ? 0x3 : 0x0;
9609 for (i = 2; i <= 8 && rsize < TARGET_PAGE_BITS; i *= 2) {
9610 /* This will check in groups of 2, 4 and then 8, whether
9611 * the subregion bits are consistent. rsize is incremented
9612 * back up to give the region size, considering consistent
9613 * adjacent subregions as one region. Stop testing if rsize
9614 * is already big enough for an entire QEMU page.
9615 */
9616 int snd_rounded = snd & ~(i - 1);
9617 uint32_t srdis_multi = extract32(env->pmsav7.drsr[n],
9618 snd_rounded + 8, i);
9619 if (srdis_mask ^ srdis_multi) {
9620 break;
9621 }
9622 srdis_mask = (srdis_mask << i) | srdis_mask;
9623 rsize++;
9624 }
9625 }
9626 if (rsize < TARGET_PAGE_BITS) {
c9f9f124
MD
9627 qemu_log_mask(LOG_UNIMP,
9628 "DRSR[%d]: No support for MPU (sub)region "
f6bda88f 9629 "alignment of %" PRIu32 " bits. Minimum is %d\n",
c9f9f124 9630 n, rsize, TARGET_PAGE_BITS);
f6bda88f
PC
9631 continue;
9632 }
9633 if (srdis) {
9634 continue;
9635 }
9636 break;
9637 }
9638
9639 if (n == -1) { /* no hits */
29c483a5 9640 if (!pmsav7_use_background_region(cpu, mmu_idx, is_user)) {
f6bda88f 9641 /* background fault */
9375ad15 9642 fi->type = ARMFault_Background;
f6bda88f
PC
9643 return true;
9644 }
9645 get_phys_addr_pmsav7_default(env, mmu_idx, address, prot);
9646 } else { /* a MPU hit! */
9647 uint32_t ap = extract32(env->pmsav7.dracr[n], 8, 3);
bf446a11
PM
9648 uint32_t xn = extract32(env->pmsav7.dracr[n], 12, 1);
9649
9650 if (m_is_system_region(env, address)) {
9651 /* System space is always execute never */
9652 xn = 1;
9653 }
f6bda88f
PC
9654
9655 if (is_user) { /* User mode AP bit decoding */
9656 switch (ap) {
9657 case 0:
9658 case 1:
9659 case 5:
9660 break; /* no access */
9661 case 3:
9662 *prot |= PAGE_WRITE;
9663 /* fall through */
9664 case 2:
9665 case 6:
9666 *prot |= PAGE_READ | PAGE_EXEC;
9667 break;
8638f1ad
PM
9668 case 7:
9669 /* for v7M, same as 6; for R profile a reserved value */
9670 if (arm_feature(env, ARM_FEATURE_M)) {
9671 *prot |= PAGE_READ | PAGE_EXEC;
9672 break;
9673 }
9674 /* fall through */
f6bda88f
PC
9675 default:
9676 qemu_log_mask(LOG_GUEST_ERROR,
c9f9f124
MD
9677 "DRACR[%d]: Bad value for AP bits: 0x%"
9678 PRIx32 "\n", n, ap);
f6bda88f
PC
9679 }
9680 } else { /* Priv. mode AP bits decoding */
9681 switch (ap) {
9682 case 0:
9683 break; /* no access */
9684 case 1:
9685 case 2:
9686 case 3:
9687 *prot |= PAGE_WRITE;
9688 /* fall through */
9689 case 5:
9690 case 6:
9691 *prot |= PAGE_READ | PAGE_EXEC;
9692 break;
8638f1ad
PM
9693 case 7:
9694 /* for v7M, same as 6; for R profile a reserved value */
9695 if (arm_feature(env, ARM_FEATURE_M)) {
9696 *prot |= PAGE_READ | PAGE_EXEC;
9697 break;
9698 }
9699 /* fall through */
f6bda88f
PC
9700 default:
9701 qemu_log_mask(LOG_GUEST_ERROR,
c9f9f124
MD
9702 "DRACR[%d]: Bad value for AP bits: 0x%"
9703 PRIx32 "\n", n, ap);
f6bda88f
PC
9704 }
9705 }
9706
9707 /* execute never */
bf446a11 9708 if (xn) {
f6bda88f
PC
9709 *prot &= ~PAGE_EXEC;
9710 }
9711 }
9712 }
9713
9375ad15
PM
9714 fi->type = ARMFault_Permission;
9715 fi->level = 1;
f6bda88f
PC
9716 return !(*prot & (1 << access_type));
9717}
9718
35337cc3
PM
9719static bool v8m_is_sau_exempt(CPUARMState *env,
9720 uint32_t address, MMUAccessType access_type)
9721{
9722 /* The architecture specifies that certain address ranges are
9723 * exempt from v8M SAU/IDAU checks.
9724 */
9725 return
9726 (access_type == MMU_INST_FETCH && m_is_system_region(env, address)) ||
9727 (address >= 0xe0000000 && address <= 0xe0002fff) ||
9728 (address >= 0xe000e000 && address <= 0xe000efff) ||
9729 (address >= 0xe002e000 && address <= 0xe002efff) ||
9730 (address >= 0xe0040000 && address <= 0xe0041fff) ||
9731 (address >= 0xe00ff000 && address <= 0xe00fffff);
9732}
9733
9734static void v8m_security_lookup(CPUARMState *env, uint32_t address,
9735 MMUAccessType access_type, ARMMMUIdx mmu_idx,
9736 V8M_SAttributes *sattrs)
9737{
9738 /* Look up the security attributes for this address. Compare the
9739 * pseudocode SecurityCheck() function.
9740 * We assume the caller has zero-initialized *sattrs.
9741 */
9742 ARMCPU *cpu = arm_env_get_cpu(env);
9743 int r;
9744
9745 /* TODO: implement IDAU */
9746
9747 if (access_type == MMU_INST_FETCH && extract32(address, 28, 4) == 0xf) {
9748 /* 0xf0000000..0xffffffff is always S for insn fetches */
9749 return;
9750 }
9751
9752 if (v8m_is_sau_exempt(env, address, access_type)) {
9753 sattrs->ns = !regime_is_secure(env, mmu_idx);
9754 return;
9755 }
9756
9757 switch (env->sau.ctrl & 3) {
9758 case 0: /* SAU.ENABLE == 0, SAU.ALLNS == 0 */
9759 break;
9760 case 2: /* SAU.ENABLE == 0, SAU.ALLNS == 1 */
9761 sattrs->ns = true;
9762 break;
9763 default: /* SAU.ENABLE == 1 */
9764 for (r = 0; r < cpu->sau_sregion; r++) {
9765 if (env->sau.rlar[r] & 1) {
9766 uint32_t base = env->sau.rbar[r] & ~0x1f;
9767 uint32_t limit = env->sau.rlar[r] | 0x1f;
9768
9769 if (base <= address && limit >= address) {
9770 if (sattrs->srvalid) {
9771 /* If we hit in more than one region then we must report
9772 * as Secure, not NS-Callable, with no valid region
9773 * number info.
9774 */
9775 sattrs->ns = false;
9776 sattrs->nsc = false;
9777 sattrs->sregion = 0;
9778 sattrs->srvalid = false;
9779 break;
9780 } else {
9781 if (env->sau.rlar[r] & 2) {
9782 sattrs->nsc = true;
9783 } else {
9784 sattrs->ns = true;
9785 }
9786 sattrs->srvalid = true;
9787 sattrs->sregion = r;
9788 }
9789 }
9790 }
9791 }
9792
9793 /* TODO when we support the IDAU then it may override the result here */
9794 break;
9795 }
9796}
9797
54317c0f
PM
9798static bool pmsav8_mpu_lookup(CPUARMState *env, uint32_t address,
9799 MMUAccessType access_type, ARMMMUIdx mmu_idx,
9800 hwaddr *phys_ptr, MemTxAttrs *txattrs,
3f551b5b 9801 int *prot, ARMMMUFaultInfo *fi, uint32_t *mregion)
54317c0f
PM
9802{
9803 /* Perform a PMSAv8 MPU lookup (without also doing the SAU check
9804 * that a full phys-to-virt translation does).
9805 * mregion is (if not NULL) set to the region number which matched,
9806 * or -1 if no region number is returned (MPU off, address did not
9807 * hit a region, address hit in multiple regions).
9808 */
504e3cc3
PM
9809 ARMCPU *cpu = arm_env_get_cpu(env);
9810 bool is_user = regime_is_user(env, mmu_idx);
62c58ee0 9811 uint32_t secure = regime_is_secure(env, mmu_idx);
504e3cc3
PM
9812 int n;
9813 int matchregion = -1;
9814 bool hit = false;
9815
9816 *phys_ptr = address;
9817 *prot = 0;
54317c0f
PM
9818 if (mregion) {
9819 *mregion = -1;
35337cc3
PM
9820 }
9821
504e3cc3
PM
9822 /* Unlike the ARM ARM pseudocode, we don't need to check whether this
9823 * was an exception vector read from the vector table (which is always
9824 * done using the default system address map), because those accesses
9825 * are done in arm_v7m_load_vector(), which always does a direct
9826 * read using address_space_ldl(), rather than going via this function.
9827 */
9828 if (regime_translation_disabled(env, mmu_idx)) { /* MPU disabled */
9829 hit = true;
9830 } else if (m_is_ppb_region(env, address)) {
9831 hit = true;
9832 } else if (pmsav7_use_background_region(cpu, mmu_idx, is_user)) {
9833 hit = true;
9834 } else {
9835 for (n = (int)cpu->pmsav7_dregion - 1; n >= 0; n--) {
9836 /* region search */
9837 /* Note that the base address is bits [31:5] from the register
9838 * with bits [4:0] all zeroes, but the limit address is bits
9839 * [31:5] from the register with bits [4:0] all ones.
9840 */
62c58ee0
PM
9841 uint32_t base = env->pmsav8.rbar[secure][n] & ~0x1f;
9842 uint32_t limit = env->pmsav8.rlar[secure][n] | 0x1f;
504e3cc3 9843
62c58ee0 9844 if (!(env->pmsav8.rlar[secure][n] & 0x1)) {
504e3cc3
PM
9845 /* Region disabled */
9846 continue;
9847 }
9848
9849 if (address < base || address > limit) {
9850 continue;
9851 }
9852
9853 if (hit) {
9854 /* Multiple regions match -- always a failure (unlike
9855 * PMSAv7 where highest-numbered-region wins)
9856 */
3f551b5b
PM
9857 fi->type = ARMFault_Permission;
9858 fi->level = 1;
504e3cc3
PM
9859 return true;
9860 }
9861
9862 matchregion = n;
9863 hit = true;
9864
9865 if (base & ~TARGET_PAGE_MASK) {
9866 qemu_log_mask(LOG_UNIMP,
9867 "MPU_RBAR[%d]: No support for MPU region base"
9868 "address of 0x%" PRIx32 ". Minimum alignment is "
9869 "%d\n",
9870 n, base, TARGET_PAGE_BITS);
9871 continue;
9872 }
9873 if ((limit + 1) & ~TARGET_PAGE_MASK) {
9874 qemu_log_mask(LOG_UNIMP,
9875 "MPU_RBAR[%d]: No support for MPU region limit"
9876 "address of 0x%" PRIx32 ". Minimum alignment is "
9877 "%d\n",
9878 n, limit, TARGET_PAGE_BITS);
9879 continue;
9880 }
9881 }
9882 }
9883
9884 if (!hit) {
9885 /* background fault */
3f551b5b 9886 fi->type = ARMFault_Background;
504e3cc3
PM
9887 return true;
9888 }
9889
9890 if (matchregion == -1) {
9891 /* hit using the background region */
9892 get_phys_addr_pmsav7_default(env, mmu_idx, address, prot);
9893 } else {
62c58ee0
PM
9894 uint32_t ap = extract32(env->pmsav8.rbar[secure][matchregion], 1, 2);
9895 uint32_t xn = extract32(env->pmsav8.rbar[secure][matchregion], 0, 1);
504e3cc3
PM
9896
9897 if (m_is_system_region(env, address)) {
9898 /* System space is always execute never */
9899 xn = 1;
9900 }
9901
9902 *prot = simple_ap_to_rw_prot(env, mmu_idx, ap);
9903 if (*prot && !xn) {
9904 *prot |= PAGE_EXEC;
9905 }
9906 /* We don't need to look the attribute up in the MAIR0/MAIR1
9907 * registers because that only tells us about cacheability.
9908 */
54317c0f
PM
9909 if (mregion) {
9910 *mregion = matchregion;
9911 }
504e3cc3
PM
9912 }
9913
3f551b5b
PM
9914 fi->type = ARMFault_Permission;
9915 fi->level = 1;
504e3cc3
PM
9916 return !(*prot & (1 << access_type));
9917}
9918
54317c0f
PM
9919
9920static bool get_phys_addr_pmsav8(CPUARMState *env, uint32_t address,
9921 MMUAccessType access_type, ARMMMUIdx mmu_idx,
9922 hwaddr *phys_ptr, MemTxAttrs *txattrs,
3f551b5b 9923 int *prot, ARMMMUFaultInfo *fi)
54317c0f
PM
9924{
9925 uint32_t secure = regime_is_secure(env, mmu_idx);
9926 V8M_SAttributes sattrs = {};
9927
9928 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
9929 v8m_security_lookup(env, address, access_type, mmu_idx, &sattrs);
9930 if (access_type == MMU_INST_FETCH) {
9931 /* Instruction fetches always use the MMU bank and the
9932 * transaction attribute determined by the fetch address,
9933 * regardless of CPU state. This is painful for QEMU
9934 * to handle, because it would mean we need to encode
9935 * into the mmu_idx not just the (user, negpri) information
9936 * for the current security state but also that for the
9937 * other security state, which would balloon the number
9938 * of mmu_idx values needed alarmingly.
9939 * Fortunately we can avoid this because it's not actually
9940 * possible to arbitrarily execute code from memory with
9941 * the wrong security attribute: it will always generate
9942 * an exception of some kind or another, apart from the
9943 * special case of an NS CPU executing an SG instruction
9944 * in S&NSC memory. So we always just fail the translation
9945 * here and sort things out in the exception handler
9946 * (including possibly emulating an SG instruction).
9947 */
9948 if (sattrs.ns != !secure) {
3f551b5b
PM
9949 if (sattrs.nsc) {
9950 fi->type = ARMFault_QEMU_NSCExec;
9951 } else {
9952 fi->type = ARMFault_QEMU_SFault;
9953 }
54317c0f
PM
9954 *phys_ptr = address;
9955 *prot = 0;
9956 return true;
9957 }
9958 } else {
9959 /* For data accesses we always use the MMU bank indicated
9960 * by the current CPU state, but the security attributes
9961 * might downgrade a secure access to nonsecure.
9962 */
9963 if (sattrs.ns) {
9964 txattrs->secure = false;
9965 } else if (!secure) {
9966 /* NS access to S memory must fault.
9967 * Architecturally we should first check whether the
9968 * MPU information for this address indicates that we
9969 * are doing an unaligned access to Device memory, which
9970 * should generate a UsageFault instead. QEMU does not
9971 * currently check for that kind of unaligned access though.
9972 * If we added it we would need to do so as a special case
9973 * for M_FAKE_FSR_SFAULT in arm_v7m_cpu_do_interrupt().
9974 */
3f551b5b 9975 fi->type = ARMFault_QEMU_SFault;
54317c0f
PM
9976 *phys_ptr = address;
9977 *prot = 0;
9978 return true;
9979 }
9980 }
9981 }
9982
9983 return pmsav8_mpu_lookup(env, address, access_type, mmu_idx, phys_ptr,
3f551b5b 9984 txattrs, prot, fi, NULL);
54317c0f
PM
9985}
9986
13689d43 9987static bool get_phys_addr_pmsav5(CPUARMState *env, uint32_t address,
03ae85f8 9988 MMUAccessType access_type, ARMMMUIdx mmu_idx,
53a4e5c5
PM
9989 hwaddr *phys_ptr, int *prot,
9990 ARMMMUFaultInfo *fi)
9ee6e8bb
PB
9991{
9992 int n;
9993 uint32_t mask;
9994 uint32_t base;
0480f69a 9995 bool is_user = regime_is_user(env, mmu_idx);
9ee6e8bb 9996
3279adb9
PM
9997 if (regime_translation_disabled(env, mmu_idx)) {
9998 /* MPU disabled. */
9999 *phys_ptr = address;
10000 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
10001 return false;
10002 }
10003
9ee6e8bb
PB
10004 *phys_ptr = address;
10005 for (n = 7; n >= 0; n--) {
554b0b09 10006 base = env->cp15.c6_region[n];
87c3d486 10007 if ((base & 1) == 0) {
554b0b09 10008 continue;
87c3d486 10009 }
554b0b09
PM
10010 mask = 1 << ((base >> 1) & 0x1f);
10011 /* Keep this shift separate from the above to avoid an
10012 (undefined) << 32. */
10013 mask = (mask << 1) - 1;
87c3d486 10014 if (((base ^ address) & ~mask) == 0) {
554b0b09 10015 break;
87c3d486 10016 }
9ee6e8bb 10017 }
87c3d486 10018 if (n < 0) {
53a4e5c5 10019 fi->type = ARMFault_Background;
b7cc4e82 10020 return true;
87c3d486 10021 }
9ee6e8bb 10022
03ae85f8 10023 if (access_type == MMU_INST_FETCH) {
7e09797c 10024 mask = env->cp15.pmsav5_insn_ap;
9ee6e8bb 10025 } else {
7e09797c 10026 mask = env->cp15.pmsav5_data_ap;
9ee6e8bb
PB
10027 }
10028 mask = (mask >> (n * 4)) & 0xf;
10029 switch (mask) {
10030 case 0:
53a4e5c5
PM
10031 fi->type = ARMFault_Permission;
10032 fi->level = 1;
b7cc4e82 10033 return true;
9ee6e8bb 10034 case 1:
87c3d486 10035 if (is_user) {
53a4e5c5
PM
10036 fi->type = ARMFault_Permission;
10037 fi->level = 1;
b7cc4e82 10038 return true;
87c3d486 10039 }
554b0b09
PM
10040 *prot = PAGE_READ | PAGE_WRITE;
10041 break;
9ee6e8bb 10042 case 2:
554b0b09 10043 *prot = PAGE_READ;
87c3d486 10044 if (!is_user) {
554b0b09 10045 *prot |= PAGE_WRITE;
87c3d486 10046 }
554b0b09 10047 break;
9ee6e8bb 10048 case 3:
554b0b09
PM
10049 *prot = PAGE_READ | PAGE_WRITE;
10050 break;
9ee6e8bb 10051 case 5:
87c3d486 10052 if (is_user) {
53a4e5c5
PM
10053 fi->type = ARMFault_Permission;
10054 fi->level = 1;
b7cc4e82 10055 return true;
87c3d486 10056 }
554b0b09
PM
10057 *prot = PAGE_READ;
10058 break;
9ee6e8bb 10059 case 6:
554b0b09
PM
10060 *prot = PAGE_READ;
10061 break;
9ee6e8bb 10062 default:
554b0b09 10063 /* Bad permission. */
53a4e5c5
PM
10064 fi->type = ARMFault_Permission;
10065 fi->level = 1;
b7cc4e82 10066 return true;
9ee6e8bb 10067 }
3ad493fc 10068 *prot |= PAGE_EXEC;
b7cc4e82 10069 return false;
9ee6e8bb
PB
10070}
10071
5b2d261d
AB
10072/* Combine either inner or outer cacheability attributes for normal
10073 * memory, according to table D4-42 and pseudocode procedure
10074 * CombineS1S2AttrHints() of ARM DDI 0487B.b (the ARMv8 ARM).
10075 *
10076 * NB: only stage 1 includes allocation hints (RW bits), leading to
10077 * some asymmetry.
10078 */
10079static uint8_t combine_cacheattr_nibble(uint8_t s1, uint8_t s2)
10080{
10081 if (s1 == 4 || s2 == 4) {
10082 /* non-cacheable has precedence */
10083 return 4;
10084 } else if (extract32(s1, 2, 2) == 0 || extract32(s1, 2, 2) == 2) {
10085 /* stage 1 write-through takes precedence */
10086 return s1;
10087 } else if (extract32(s2, 2, 2) == 2) {
10088 /* stage 2 write-through takes precedence, but the allocation hint
10089 * is still taken from stage 1
10090 */
10091 return (2 << 2) | extract32(s1, 0, 2);
10092 } else { /* write-back */
10093 return s1;
10094 }
10095}
10096
10097/* Combine S1 and S2 cacheability/shareability attributes, per D4.5.4
10098 * and CombineS1S2Desc()
10099 *
10100 * @s1: Attributes from stage 1 walk
10101 * @s2: Attributes from stage 2 walk
10102 */
10103static ARMCacheAttrs combine_cacheattrs(ARMCacheAttrs s1, ARMCacheAttrs s2)
10104{
10105 uint8_t s1lo = extract32(s1.attrs, 0, 4), s2lo = extract32(s2.attrs, 0, 4);
10106 uint8_t s1hi = extract32(s1.attrs, 4, 4), s2hi = extract32(s2.attrs, 4, 4);
10107 ARMCacheAttrs ret;
10108
10109 /* Combine shareability attributes (table D4-43) */
10110 if (s1.shareability == 2 || s2.shareability == 2) {
10111 /* if either are outer-shareable, the result is outer-shareable */
10112 ret.shareability = 2;
10113 } else if (s1.shareability == 3 || s2.shareability == 3) {
10114 /* if either are inner-shareable, the result is inner-shareable */
10115 ret.shareability = 3;
10116 } else {
10117 /* both non-shareable */
10118 ret.shareability = 0;
10119 }
10120
10121 /* Combine memory type and cacheability attributes */
10122 if (s1hi == 0 || s2hi == 0) {
10123 /* Device has precedence over normal */
10124 if (s1lo == 0 || s2lo == 0) {
10125 /* nGnRnE has precedence over anything */
10126 ret.attrs = 0;
10127 } else if (s1lo == 4 || s2lo == 4) {
10128 /* non-Reordering has precedence over Reordering */
10129 ret.attrs = 4; /* nGnRE */
10130 } else if (s1lo == 8 || s2lo == 8) {
10131 /* non-Gathering has precedence over Gathering */
10132 ret.attrs = 8; /* nGRE */
10133 } else {
10134 ret.attrs = 0xc; /* GRE */
10135 }
10136
10137 /* Any location for which the resultant memory type is any
10138 * type of Device memory is always treated as Outer Shareable.
10139 */
10140 ret.shareability = 2;
10141 } else { /* Normal memory */
10142 /* Outer/inner cacheability combine independently */
10143 ret.attrs = combine_cacheattr_nibble(s1hi, s2hi) << 4
10144 | combine_cacheattr_nibble(s1lo, s2lo);
10145
10146 if (ret.attrs == 0x44) {
10147 /* Any location for which the resultant memory type is Normal
10148 * Inner Non-cacheable, Outer Non-cacheable is always treated
10149 * as Outer Shareable.
10150 */
10151 ret.shareability = 2;
10152 }
10153 }
10154
10155 return ret;
10156}
10157
10158
702a9357
PM
10159/* get_phys_addr - get the physical address for this virtual address
10160 *
10161 * Find the physical address corresponding to the given virtual address,
10162 * by doing a translation table walk on MMU based systems or using the
10163 * MPU state on MPU based systems.
10164 *
b7cc4e82
PC
10165 * Returns false if the translation was successful. Otherwise, phys_ptr, attrs,
10166 * prot and page_size may not be filled in, and the populated fsr value provides
702a9357
PM
10167 * information on why the translation aborted, in the format of a
10168 * DFSR/IFSR fault register, with the following caveats:
10169 * * we honour the short vs long DFSR format differences.
10170 * * the WnR bit is never set (the caller must do this).
f6bda88f 10171 * * for PSMAv5 based systems we don't bother to return a full FSR format
702a9357
PM
10172 * value.
10173 *
10174 * @env: CPUARMState
10175 * @address: virtual address to get physical address for
10176 * @access_type: 0 for read, 1 for write, 2 for execute
d3649702 10177 * @mmu_idx: MMU index indicating required translation regime
702a9357 10178 * @phys_ptr: set to the physical address corresponding to the virtual address
8bf5b6a9 10179 * @attrs: set to the memory transaction attributes to use
702a9357
PM
10180 * @prot: set to the permissions for the page containing phys_ptr
10181 * @page_size: set to the size of the page containing phys_ptr
5b2d261d
AB
10182 * @fi: set to fault info if the translation fails
10183 * @cacheattrs: (if non-NULL) set to the cacheability/shareability attributes
702a9357 10184 */
af51f566 10185static bool get_phys_addr(CPUARMState *env, target_ulong address,
03ae85f8 10186 MMUAccessType access_type, ARMMMUIdx mmu_idx,
af51f566 10187 hwaddr *phys_ptr, MemTxAttrs *attrs, int *prot,
bc52bfeb 10188 target_ulong *page_size,
5b2d261d 10189 ARMMMUFaultInfo *fi, ARMCacheAttrs *cacheattrs)
9ee6e8bb 10190{
0480f69a 10191 if (mmu_idx == ARMMMUIdx_S12NSE0 || mmu_idx == ARMMMUIdx_S12NSE1) {
9b539263
EI
10192 /* Call ourselves recursively to do the stage 1 and then stage 2
10193 * translations.
0480f69a 10194 */
9b539263
EI
10195 if (arm_feature(env, ARM_FEATURE_EL2)) {
10196 hwaddr ipa;
10197 int s2_prot;
10198 int ret;
5b2d261d 10199 ARMCacheAttrs cacheattrs2 = {};
9b539263
EI
10200
10201 ret = get_phys_addr(env, address, access_type,
8bd5c820 10202 stage_1_mmu_idx(mmu_idx), &ipa, attrs,
bc52bfeb 10203 prot, page_size, fi, cacheattrs);
9b539263
EI
10204
10205 /* If S1 fails or S2 is disabled, return early. */
10206 if (ret || regime_translation_disabled(env, ARMMMUIdx_S2NS)) {
10207 *phys_ptr = ipa;
10208 return ret;
10209 }
10210
10211 /* S1 is done. Now do S2 translation. */
10212 ret = get_phys_addr_lpae(env, ipa, access_type, ARMMMUIdx_S2NS,
10213 phys_ptr, attrs, &s2_prot,
da909b2c 10214 page_size, fi,
5b2d261d 10215 cacheattrs != NULL ? &cacheattrs2 : NULL);
9b539263
EI
10216 fi->s2addr = ipa;
10217 /* Combine the S1 and S2 perms. */
10218 *prot &= s2_prot;
5b2d261d
AB
10219
10220 /* Combine the S1 and S2 cache attributes, if needed */
10221 if (!ret && cacheattrs != NULL) {
10222 *cacheattrs = combine_cacheattrs(*cacheattrs, cacheattrs2);
10223 }
10224
9b539263
EI
10225 return ret;
10226 } else {
10227 /*
10228 * For non-EL2 CPUs a stage1+stage2 translation is just stage 1.
10229 */
8bd5c820 10230 mmu_idx = stage_1_mmu_idx(mmu_idx);
9b539263 10231 }
0480f69a 10232 }
d3649702 10233
8bf5b6a9
PM
10234 /* The page table entries may downgrade secure to non-secure, but
10235 * cannot upgrade an non-secure translation regime's attributes
10236 * to secure.
10237 */
10238 attrs->secure = regime_is_secure(env, mmu_idx);
0995bf8c 10239 attrs->user = regime_is_user(env, mmu_idx);
8bf5b6a9 10240
0480f69a
PM
10241 /* Fast Context Switch Extension. This doesn't exist at all in v8.
10242 * In v7 and earlier it affects all stage 1 translations.
10243 */
10244 if (address < 0x02000000 && mmu_idx != ARMMMUIdx_S2NS
10245 && !arm_feature(env, ARM_FEATURE_V8)) {
10246 if (regime_el(env, mmu_idx) == 3) {
10247 address += env->cp15.fcseidr_s;
10248 } else {
10249 address += env->cp15.fcseidr_ns;
10250 }
54bf36ed 10251 }
9ee6e8bb 10252
3279adb9 10253 if (arm_feature(env, ARM_FEATURE_PMSA)) {
c9f9f124 10254 bool ret;
f6bda88f 10255 *page_size = TARGET_PAGE_SIZE;
3279adb9 10256
504e3cc3
PM
10257 if (arm_feature(env, ARM_FEATURE_V8)) {
10258 /* PMSAv8 */
10259 ret = get_phys_addr_pmsav8(env, address, access_type, mmu_idx,
3f551b5b 10260 phys_ptr, attrs, prot, fi);
504e3cc3 10261 } else if (arm_feature(env, ARM_FEATURE_V7)) {
3279adb9
PM
10262 /* PMSAv7 */
10263 ret = get_phys_addr_pmsav7(env, address, access_type, mmu_idx,
9375ad15 10264 phys_ptr, prot, fi);
3279adb9
PM
10265 } else {
10266 /* Pre-v7 MPU */
10267 ret = get_phys_addr_pmsav5(env, address, access_type, mmu_idx,
53a4e5c5 10268 phys_ptr, prot, fi);
3279adb9
PM
10269 }
10270 qemu_log_mask(CPU_LOG_MMU, "PMSA MPU lookup for %s at 0x%08" PRIx32
c9f9f124 10271 " mmu_idx %u -> %s (prot %c%c%c)\n",
709e4407
PM
10272 access_type == MMU_DATA_LOAD ? "reading" :
10273 (access_type == MMU_DATA_STORE ? "writing" : "execute"),
c9f9f124
MD
10274 (uint32_t)address, mmu_idx,
10275 ret ? "Miss" : "Hit",
10276 *prot & PAGE_READ ? 'r' : '-',
10277 *prot & PAGE_WRITE ? 'w' : '-',
10278 *prot & PAGE_EXEC ? 'x' : '-');
10279
10280 return ret;
f6bda88f
PC
10281 }
10282
3279adb9
PM
10283 /* Definitely a real MMU, not an MPU */
10284
0480f69a 10285 if (regime_translation_disabled(env, mmu_idx)) {
3279adb9 10286 /* MMU disabled. */
9ee6e8bb 10287 *phys_ptr = address;
3ad493fc 10288 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
d4c430a8 10289 *page_size = TARGET_PAGE_SIZE;
9ee6e8bb 10290 return 0;
0480f69a
PM
10291 }
10292
0480f69a 10293 if (regime_using_lpae_format(env, mmu_idx)) {
bc52bfeb
PM
10294 return get_phys_addr_lpae(env, address, access_type, mmu_idx,
10295 phys_ptr, attrs, prot, page_size,
10296 fi, cacheattrs);
0480f69a 10297 } else if (regime_sctlr(env, mmu_idx) & SCTLR_XP) {
bc52bfeb
PM
10298 return get_phys_addr_v6(env, address, access_type, mmu_idx,
10299 phys_ptr, attrs, prot, page_size, fi);
9ee6e8bb 10300 } else {
bc52bfeb 10301 return get_phys_addr_v5(env, address, access_type, mmu_idx,
f989983e 10302 phys_ptr, prot, page_size, fi);
9ee6e8bb
PB
10303 }
10304}
10305
8c6084bf 10306/* Walk the page table and (if the mapping exists) add the page
b7cc4e82
PC
10307 * to the TLB. Return false on success, or true on failure. Populate
10308 * fsr with ARM DFSR/IFSR fault register format value on failure.
8c6084bf 10309 */
b7cc4e82 10310bool arm_tlb_fill(CPUState *cs, vaddr address,
bc52bfeb 10311 MMUAccessType access_type, int mmu_idx,
e14b5a23 10312 ARMMMUFaultInfo *fi)
b5ff1b31 10313{
7510454e
AF
10314 ARMCPU *cpu = ARM_CPU(cs);
10315 CPUARMState *env = &cpu->env;
a8170e5e 10316 hwaddr phys_addr;
d4c430a8 10317 target_ulong page_size;
b5ff1b31 10318 int prot;
d3649702 10319 int ret;
8bf5b6a9 10320 MemTxAttrs attrs = {};
b5ff1b31 10321
8bd5c820
PM
10322 ret = get_phys_addr(env, address, access_type,
10323 core_to_arm_mmu_idx(env, mmu_idx), &phys_addr,
bc52bfeb 10324 &attrs, &prot, &page_size, fi, NULL);
b7cc4e82 10325 if (!ret) {
b5ff1b31 10326 /* Map a single [sub]page. */
dcd82c11
AB
10327 phys_addr &= TARGET_PAGE_MASK;
10328 address &= TARGET_PAGE_MASK;
8bf5b6a9
PM
10329 tlb_set_page_with_attrs(cs, address, phys_addr, attrs,
10330 prot, mmu_idx, page_size);
d4c430a8 10331 return 0;
b5ff1b31
FB
10332 }
10333
8c6084bf 10334 return ret;
b5ff1b31
FB
10335}
10336
0faea0c7
PM
10337hwaddr arm_cpu_get_phys_page_attrs_debug(CPUState *cs, vaddr addr,
10338 MemTxAttrs *attrs)
b5ff1b31 10339{
00b941e5 10340 ARMCPU *cpu = ARM_CPU(cs);
d3649702 10341 CPUARMState *env = &cpu->env;
a8170e5e 10342 hwaddr phys_addr;
d4c430a8 10343 target_ulong page_size;
b5ff1b31 10344 int prot;
b7cc4e82 10345 bool ret;
e14b5a23 10346 ARMMMUFaultInfo fi = {};
8bd5c820 10347 ARMMMUIdx mmu_idx = core_to_arm_mmu_idx(env, cpu_mmu_index(env, false));
b5ff1b31 10348
0faea0c7
PM
10349 *attrs = (MemTxAttrs) {};
10350
8bd5c820 10351 ret = get_phys_addr(env, addr, 0, mmu_idx, &phys_addr,
bc52bfeb 10352 attrs, &prot, &page_size, &fi, NULL);
b5ff1b31 10353
b7cc4e82 10354 if (ret) {
b5ff1b31 10355 return -1;
00b941e5 10356 }
b5ff1b31
FB
10357 return phys_addr;
10358}
10359
0ecb72a5 10360uint32_t HELPER(v7m_mrs)(CPUARMState *env, uint32_t reg)
9ee6e8bb 10361{
58117c9b
MD
10362 uint32_t mask;
10363 unsigned el = arm_current_el(env);
10364
10365 /* First handle registers which unprivileged can read */
10366
10367 switch (reg) {
10368 case 0 ... 7: /* xPSR sub-fields */
10369 mask = 0;
10370 if ((reg & 1) && el) {
987ab45e 10371 mask |= XPSR_EXCP; /* IPSR (unpriv. reads as zero) */
58117c9b
MD
10372 }
10373 if (!(reg & 4)) {
987ab45e 10374 mask |= XPSR_NZCV | XPSR_Q; /* APSR */
58117c9b
MD
10375 }
10376 /* EPSR reads as zero */
10377 return xpsr_read(env) & mask;
10378 break;
10379 case 20: /* CONTROL */
8bfc26ea 10380 return env->v7m.control[env->v7m.secure];
50f11062
PM
10381 case 0x94: /* CONTROL_NS */
10382 /* We have to handle this here because unprivileged Secure code
10383 * can read the NS CONTROL register.
10384 */
10385 if (!env->v7m.secure) {
10386 return 0;
10387 }
10388 return env->v7m.control[M_REG_NS];
58117c9b
MD
10389 }
10390
10391 if (el == 0) {
10392 return 0; /* unprivileged reads others as zero */
10393 }
a47dddd7 10394
50f11062
PM
10395 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
10396 switch (reg) {
10397 case 0x88: /* MSP_NS */
10398 if (!env->v7m.secure) {
10399 return 0;
10400 }
10401 return env->v7m.other_ss_msp;
10402 case 0x89: /* PSP_NS */
10403 if (!env->v7m.secure) {
10404 return 0;
10405 }
10406 return env->v7m.other_ss_psp;
57bb3156
PM
10407 case 0x8a: /* MSPLIM_NS */
10408 if (!env->v7m.secure) {
10409 return 0;
10410 }
10411 return env->v7m.msplim[M_REG_NS];
10412 case 0x8b: /* PSPLIM_NS */
10413 if (!env->v7m.secure) {
10414 return 0;
10415 }
10416 return env->v7m.psplim[M_REG_NS];
50f11062
PM
10417 case 0x90: /* PRIMASK_NS */
10418 if (!env->v7m.secure) {
10419 return 0;
10420 }
10421 return env->v7m.primask[M_REG_NS];
10422 case 0x91: /* BASEPRI_NS */
10423 if (!env->v7m.secure) {
10424 return 0;
10425 }
10426 return env->v7m.basepri[M_REG_NS];
10427 case 0x93: /* FAULTMASK_NS */
10428 if (!env->v7m.secure) {
10429 return 0;
10430 }
10431 return env->v7m.faultmask[M_REG_NS];
10432 case 0x98: /* SP_NS */
10433 {
10434 /* This gives the non-secure SP selected based on whether we're
10435 * currently in handler mode or not, using the NS CONTROL.SPSEL.
10436 */
10437 bool spsel = env->v7m.control[M_REG_NS] & R_V7M_CONTROL_SPSEL_MASK;
10438
10439 if (!env->v7m.secure) {
10440 return 0;
10441 }
10442 if (!arm_v7m_is_handler_mode(env) && spsel) {
10443 return env->v7m.other_ss_psp;
10444 } else {
10445 return env->v7m.other_ss_msp;
10446 }
10447 }
10448 default:
10449 break;
10450 }
10451 }
10452
9ee6e8bb 10453 switch (reg) {
9ee6e8bb 10454 case 8: /* MSP */
1169d3aa 10455 return v7m_using_psp(env) ? env->v7m.other_sp : env->regs[13];
9ee6e8bb 10456 case 9: /* PSP */
1169d3aa 10457 return v7m_using_psp(env) ? env->regs[13] : env->v7m.other_sp;
57bb3156
PM
10458 case 10: /* MSPLIM */
10459 if (!arm_feature(env, ARM_FEATURE_V8)) {
10460 goto bad_reg;
10461 }
10462 return env->v7m.msplim[env->v7m.secure];
10463 case 11: /* PSPLIM */
10464 if (!arm_feature(env, ARM_FEATURE_V8)) {
10465 goto bad_reg;
10466 }
10467 return env->v7m.psplim[env->v7m.secure];
9ee6e8bb 10468 case 16: /* PRIMASK */
6d804834 10469 return env->v7m.primask[env->v7m.secure];
82845826
SH
10470 case 17: /* BASEPRI */
10471 case 18: /* BASEPRI_MAX */
acf94941 10472 return env->v7m.basepri[env->v7m.secure];
82845826 10473 case 19: /* FAULTMASK */
42a6686b 10474 return env->v7m.faultmask[env->v7m.secure];
9ee6e8bb 10475 default:
57bb3156 10476 bad_reg:
58117c9b
MD
10477 qemu_log_mask(LOG_GUEST_ERROR, "Attempt to read unknown special"
10478 " register %d\n", reg);
9ee6e8bb
PB
10479 return 0;
10480 }
10481}
10482
b28b3377
PM
10483void HELPER(v7m_msr)(CPUARMState *env, uint32_t maskreg, uint32_t val)
10484{
10485 /* We're passed bits [11..0] of the instruction; extract
10486 * SYSm and the mask bits.
10487 * Invalid combinations of SYSm and mask are UNPREDICTABLE;
10488 * we choose to treat them as if the mask bits were valid.
10489 * NB that the pseudocode 'mask' variable is bits [11..10],
10490 * whereas ours is [11..8].
10491 */
10492 uint32_t mask = extract32(maskreg, 8, 4);
10493 uint32_t reg = extract32(maskreg, 0, 8);
10494
58117c9b
MD
10495 if (arm_current_el(env) == 0 && reg > 7) {
10496 /* only xPSR sub-fields may be written by unprivileged */
10497 return;
10498 }
a47dddd7 10499
50f11062
PM
10500 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
10501 switch (reg) {
10502 case 0x88: /* MSP_NS */
10503 if (!env->v7m.secure) {
10504 return;
10505 }
10506 env->v7m.other_ss_msp = val;
10507 return;
10508 case 0x89: /* PSP_NS */
10509 if (!env->v7m.secure) {
10510 return;
10511 }
10512 env->v7m.other_ss_psp = val;
10513 return;
57bb3156
PM
10514 case 0x8a: /* MSPLIM_NS */
10515 if (!env->v7m.secure) {
10516 return;
10517 }
10518 env->v7m.msplim[M_REG_NS] = val & ~7;
10519 return;
10520 case 0x8b: /* PSPLIM_NS */
10521 if (!env->v7m.secure) {
10522 return;
10523 }
10524 env->v7m.psplim[M_REG_NS] = val & ~7;
10525 return;
50f11062
PM
10526 case 0x90: /* PRIMASK_NS */
10527 if (!env->v7m.secure) {
10528 return;
10529 }
10530 env->v7m.primask[M_REG_NS] = val & 1;
10531 return;
10532 case 0x91: /* BASEPRI_NS */
10533 if (!env->v7m.secure) {
10534 return;
10535 }
10536 env->v7m.basepri[M_REG_NS] = val & 0xff;
10537 return;
10538 case 0x93: /* FAULTMASK_NS */
10539 if (!env->v7m.secure) {
10540 return;
10541 }
10542 env->v7m.faultmask[M_REG_NS] = val & 1;
10543 return;
6eb3a64e
PM
10544 case 0x94: /* CONTROL_NS */
10545 if (!env->v7m.secure) {
10546 return;
10547 }
10548 write_v7m_control_spsel_for_secstate(env,
10549 val & R_V7M_CONTROL_SPSEL_MASK,
10550 M_REG_NS);
10551 env->v7m.control[M_REG_NS] &= ~R_V7M_CONTROL_NPRIV_MASK;
10552 env->v7m.control[M_REG_NS] |= val & R_V7M_CONTROL_NPRIV_MASK;
10553 return;
50f11062
PM
10554 case 0x98: /* SP_NS */
10555 {
10556 /* This gives the non-secure SP selected based on whether we're
10557 * currently in handler mode or not, using the NS CONTROL.SPSEL.
10558 */
10559 bool spsel = env->v7m.control[M_REG_NS] & R_V7M_CONTROL_SPSEL_MASK;
10560
10561 if (!env->v7m.secure) {
10562 return;
10563 }
10564 if (!arm_v7m_is_handler_mode(env) && spsel) {
10565 env->v7m.other_ss_psp = val;
10566 } else {
10567 env->v7m.other_ss_msp = val;
10568 }
10569 return;
10570 }
10571 default:
10572 break;
10573 }
10574 }
10575
9ee6e8bb 10576 switch (reg) {
58117c9b
MD
10577 case 0 ... 7: /* xPSR sub-fields */
10578 /* only APSR is actually writable */
b28b3377
PM
10579 if (!(reg & 4)) {
10580 uint32_t apsrmask = 0;
10581
10582 if (mask & 8) {
987ab45e 10583 apsrmask |= XPSR_NZCV | XPSR_Q;
b28b3377
PM
10584 }
10585 if ((mask & 4) && arm_feature(env, ARM_FEATURE_THUMB_DSP)) {
987ab45e 10586 apsrmask |= XPSR_GE;
b28b3377
PM
10587 }
10588 xpsr_write(env, val, apsrmask);
58117c9b 10589 }
9ee6e8bb
PB
10590 break;
10591 case 8: /* MSP */
1169d3aa 10592 if (v7m_using_psp(env)) {
9ee6e8bb 10593 env->v7m.other_sp = val;
abc24d86 10594 } else {
9ee6e8bb 10595 env->regs[13] = val;
abc24d86 10596 }
9ee6e8bb
PB
10597 break;
10598 case 9: /* PSP */
1169d3aa 10599 if (v7m_using_psp(env)) {
9ee6e8bb 10600 env->regs[13] = val;
abc24d86 10601 } else {
9ee6e8bb 10602 env->v7m.other_sp = val;
abc24d86 10603 }
9ee6e8bb 10604 break;
57bb3156
PM
10605 case 10: /* MSPLIM */
10606 if (!arm_feature(env, ARM_FEATURE_V8)) {
10607 goto bad_reg;
10608 }
10609 env->v7m.msplim[env->v7m.secure] = val & ~7;
10610 break;
10611 case 11: /* PSPLIM */
10612 if (!arm_feature(env, ARM_FEATURE_V8)) {
10613 goto bad_reg;
10614 }
10615 env->v7m.psplim[env->v7m.secure] = val & ~7;
10616 break;
9ee6e8bb 10617 case 16: /* PRIMASK */
6d804834 10618 env->v7m.primask[env->v7m.secure] = val & 1;
9ee6e8bb 10619 break;
82845826 10620 case 17: /* BASEPRI */
acf94941 10621 env->v7m.basepri[env->v7m.secure] = val & 0xff;
9ee6e8bb 10622 break;
82845826 10623 case 18: /* BASEPRI_MAX */
9ee6e8bb 10624 val &= 0xff;
acf94941
PM
10625 if (val != 0 && (val < env->v7m.basepri[env->v7m.secure]
10626 || env->v7m.basepri[env->v7m.secure] == 0)) {
10627 env->v7m.basepri[env->v7m.secure] = val;
10628 }
9ee6e8bb 10629 break;
82845826 10630 case 19: /* FAULTMASK */
42a6686b 10631 env->v7m.faultmask[env->v7m.secure] = val & 1;
82845826 10632 break;
9ee6e8bb 10633 case 20: /* CONTROL */
792dac30
PM
10634 /* Writing to the SPSEL bit only has an effect if we are in
10635 * thread mode; other bits can be updated by any privileged code.
de2db7ec 10636 * write_v7m_control_spsel() deals with updating the SPSEL bit in
792dac30 10637 * env->v7m.control, so we only need update the others.
83d7f86d
PM
10638 * For v7M, we must just ignore explicit writes to SPSEL in handler
10639 * mode; for v8M the write is permitted but will have no effect.
792dac30 10640 */
83d7f86d
PM
10641 if (arm_feature(env, ARM_FEATURE_V8) ||
10642 !arm_v7m_is_handler_mode(env)) {
de2db7ec 10643 write_v7m_control_spsel(env, (val & R_V7M_CONTROL_SPSEL_MASK) != 0);
792dac30 10644 }
8bfc26ea
PM
10645 env->v7m.control[env->v7m.secure] &= ~R_V7M_CONTROL_NPRIV_MASK;
10646 env->v7m.control[env->v7m.secure] |= val & R_V7M_CONTROL_NPRIV_MASK;
9ee6e8bb
PB
10647 break;
10648 default:
57bb3156 10649 bad_reg:
58117c9b
MD
10650 qemu_log_mask(LOG_GUEST_ERROR, "Attempt to write unknown special"
10651 " register %d\n", reg);
9ee6e8bb
PB
10652 return;
10653 }
10654}
10655
5158de24
PM
10656uint32_t HELPER(v7m_tt)(CPUARMState *env, uint32_t addr, uint32_t op)
10657{
10658 /* Implement the TT instruction. op is bits [7:6] of the insn. */
10659 bool forceunpriv = op & 1;
10660 bool alt = op & 2;
10661 V8M_SAttributes sattrs = {};
10662 uint32_t tt_resp;
10663 bool r, rw, nsr, nsrw, mrvalid;
10664 int prot;
3f551b5b 10665 ARMMMUFaultInfo fi = {};
5158de24
PM
10666 MemTxAttrs attrs = {};
10667 hwaddr phys_addr;
5158de24
PM
10668 ARMMMUIdx mmu_idx;
10669 uint32_t mregion;
10670 bool targetpriv;
10671 bool targetsec = env->v7m.secure;
10672
10673 /* Work out what the security state and privilege level we're
10674 * interested in is...
10675 */
10676 if (alt) {
10677 targetsec = !targetsec;
10678 }
10679
10680 if (forceunpriv) {
10681 targetpriv = false;
10682 } else {
10683 targetpriv = arm_v7m_is_handler_mode(env) ||
10684 !(env->v7m.control[targetsec] & R_V7M_CONTROL_NPRIV_MASK);
10685 }
10686
10687 /* ...and then figure out which MMU index this is */
10688 mmu_idx = arm_v7m_mmu_idx_for_secstate_and_priv(env, targetsec, targetpriv);
10689
10690 /* We know that the MPU and SAU don't care about the access type
10691 * for our purposes beyond that we don't want to claim to be
10692 * an insn fetch, so we arbitrarily call this a read.
10693 */
10694
10695 /* MPU region info only available for privileged or if
10696 * inspecting the other MPU state.
10697 */
10698 if (arm_current_el(env) != 0 || alt) {
10699 /* We can ignore the return value as prot is always set */
10700 pmsav8_mpu_lookup(env, addr, MMU_DATA_LOAD, mmu_idx,
3f551b5b 10701 &phys_addr, &attrs, &prot, &fi, &mregion);
5158de24
PM
10702 if (mregion == -1) {
10703 mrvalid = false;
10704 mregion = 0;
10705 } else {
10706 mrvalid = true;
10707 }
10708 r = prot & PAGE_READ;
10709 rw = prot & PAGE_WRITE;
10710 } else {
10711 r = false;
10712 rw = false;
10713 mrvalid = false;
10714 mregion = 0;
10715 }
10716
10717 if (env->v7m.secure) {
10718 v8m_security_lookup(env, addr, MMU_DATA_LOAD, mmu_idx, &sattrs);
10719 nsr = sattrs.ns && r;
10720 nsrw = sattrs.ns && rw;
10721 } else {
10722 sattrs.ns = true;
10723 nsr = false;
10724 nsrw = false;
10725 }
10726
10727 tt_resp = (sattrs.iregion << 24) |
10728 (sattrs.irvalid << 23) |
10729 ((!sattrs.ns) << 22) |
10730 (nsrw << 21) |
10731 (nsr << 20) |
10732 (rw << 19) |
10733 (r << 18) |
10734 (sattrs.srvalid << 17) |
10735 (mrvalid << 16) |
10736 (sattrs.sregion << 8) |
10737 mregion;
10738
10739 return tt_resp;
10740}
10741
b5ff1b31 10742#endif
6ddbc6e4 10743
aca3f40b
PM
10744void HELPER(dc_zva)(CPUARMState *env, uint64_t vaddr_in)
10745{
10746 /* Implement DC ZVA, which zeroes a fixed-length block of memory.
10747 * Note that we do not implement the (architecturally mandated)
10748 * alignment fault for attempts to use this on Device memory
10749 * (which matches the usual QEMU behaviour of not implementing either
10750 * alignment faults or any memory attribute handling).
10751 */
10752
10753 ARMCPU *cpu = arm_env_get_cpu(env);
10754 uint64_t blocklen = 4 << cpu->dcz_blocksize;
10755 uint64_t vaddr = vaddr_in & ~(blocklen - 1);
10756
10757#ifndef CONFIG_USER_ONLY
10758 {
10759 /* Slightly awkwardly, QEMU's TARGET_PAGE_SIZE may be less than
10760 * the block size so we might have to do more than one TLB lookup.
10761 * We know that in fact for any v8 CPU the page size is at least 4K
10762 * and the block size must be 2K or less, but TARGET_PAGE_SIZE is only
10763 * 1K as an artefact of legacy v5 subpage support being present in the
10764 * same QEMU executable.
10765 */
10766 int maxidx = DIV_ROUND_UP(blocklen, TARGET_PAGE_SIZE);
10767 void *hostaddr[maxidx];
10768 int try, i;
97ed5ccd 10769 unsigned mmu_idx = cpu_mmu_index(env, false);
3972ef6f 10770 TCGMemOpIdx oi = make_memop_idx(MO_UB, mmu_idx);
aca3f40b
PM
10771
10772 for (try = 0; try < 2; try++) {
10773
10774 for (i = 0; i < maxidx; i++) {
10775 hostaddr[i] = tlb_vaddr_to_host(env,
10776 vaddr + TARGET_PAGE_SIZE * i,
3972ef6f 10777 1, mmu_idx);
aca3f40b
PM
10778 if (!hostaddr[i]) {
10779 break;
10780 }
10781 }
10782 if (i == maxidx) {
10783 /* If it's all in the TLB it's fair game for just writing to;
10784 * we know we don't need to update dirty status, etc.
10785 */
10786 for (i = 0; i < maxidx - 1; i++) {
10787 memset(hostaddr[i], 0, TARGET_PAGE_SIZE);
10788 }
10789 memset(hostaddr[i], 0, blocklen - (i * TARGET_PAGE_SIZE));
10790 return;
10791 }
10792 /* OK, try a store and see if we can populate the tlb. This
10793 * might cause an exception if the memory isn't writable,
10794 * in which case we will longjmp out of here. We must for
10795 * this purpose use the actual register value passed to us
10796 * so that we get the fault address right.
10797 */
01ecaf43 10798 helper_ret_stb_mmu(env, vaddr_in, 0, oi, GETPC());
aca3f40b
PM
10799 /* Now we can populate the other TLB entries, if any */
10800 for (i = 0; i < maxidx; i++) {
10801 uint64_t va = vaddr + TARGET_PAGE_SIZE * i;
10802 if (va != (vaddr_in & TARGET_PAGE_MASK)) {
01ecaf43 10803 helper_ret_stb_mmu(env, va, 0, oi, GETPC());
aca3f40b
PM
10804 }
10805 }
10806 }
10807
10808 /* Slow path (probably attempt to do this to an I/O device or
10809 * similar, or clearing of a block of code we have translations
10810 * cached for). Just do a series of byte writes as the architecture
10811 * demands. It's not worth trying to use a cpu_physical_memory_map(),
10812 * memset(), unmap() sequence here because:
10813 * + we'd need to account for the blocksize being larger than a page
10814 * + the direct-RAM access case is almost always going to be dealt
10815 * with in the fastpath code above, so there's no speed benefit
10816 * + we would have to deal with the map returning NULL because the
10817 * bounce buffer was in use
10818 */
10819 for (i = 0; i < blocklen; i++) {
01ecaf43 10820 helper_ret_stb_mmu(env, vaddr + i, 0, oi, GETPC());
aca3f40b
PM
10821 }
10822 }
10823#else
10824 memset(g2h(vaddr), 0, blocklen);
10825#endif
10826}
10827
6ddbc6e4
PB
10828/* Note that signed overflow is undefined in C. The following routines are
10829 careful to use unsigned types where modulo arithmetic is required.
10830 Failure to do so _will_ break on newer gcc. */
10831
10832/* Signed saturating arithmetic. */
10833
1654b2d6 10834/* Perform 16-bit signed saturating addition. */
6ddbc6e4
PB
10835static inline uint16_t add16_sat(uint16_t a, uint16_t b)
10836{
10837 uint16_t res;
10838
10839 res = a + b;
10840 if (((res ^ a) & 0x8000) && !((a ^ b) & 0x8000)) {
10841 if (a & 0x8000)
10842 res = 0x8000;
10843 else
10844 res = 0x7fff;
10845 }
10846 return res;
10847}
10848
1654b2d6 10849/* Perform 8-bit signed saturating addition. */
6ddbc6e4
PB
10850static inline uint8_t add8_sat(uint8_t a, uint8_t b)
10851{
10852 uint8_t res;
10853
10854 res = a + b;
10855 if (((res ^ a) & 0x80) && !((a ^ b) & 0x80)) {
10856 if (a & 0x80)
10857 res = 0x80;
10858 else
10859 res = 0x7f;
10860 }
10861 return res;
10862}
10863
1654b2d6 10864/* Perform 16-bit signed saturating subtraction. */
6ddbc6e4
PB
10865static inline uint16_t sub16_sat(uint16_t a, uint16_t b)
10866{
10867 uint16_t res;
10868
10869 res = a - b;
10870 if (((res ^ a) & 0x8000) && ((a ^ b) & 0x8000)) {
10871 if (a & 0x8000)
10872 res = 0x8000;
10873 else
10874 res = 0x7fff;
10875 }
10876 return res;
10877}
10878
1654b2d6 10879/* Perform 8-bit signed saturating subtraction. */
6ddbc6e4
PB
10880static inline uint8_t sub8_sat(uint8_t a, uint8_t b)
10881{
10882 uint8_t res;
10883
10884 res = a - b;
10885 if (((res ^ a) & 0x80) && ((a ^ b) & 0x80)) {
10886 if (a & 0x80)
10887 res = 0x80;
10888 else
10889 res = 0x7f;
10890 }
10891 return res;
10892}
10893
10894#define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16);
10895#define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16);
10896#define ADD8(a, b, n) RESULT(add8_sat(a, b), n, 8);
10897#define SUB8(a, b, n) RESULT(sub8_sat(a, b), n, 8);
10898#define PFX q
10899
10900#include "op_addsub.h"
10901
10902/* Unsigned saturating arithmetic. */
460a09c1 10903static inline uint16_t add16_usat(uint16_t a, uint16_t b)
6ddbc6e4
PB
10904{
10905 uint16_t res;
10906 res = a + b;
10907 if (res < a)
10908 res = 0xffff;
10909 return res;
10910}
10911
460a09c1 10912static inline uint16_t sub16_usat(uint16_t a, uint16_t b)
6ddbc6e4 10913{
4c4fd3f8 10914 if (a > b)
6ddbc6e4
PB
10915 return a - b;
10916 else
10917 return 0;
10918}
10919
10920static inline uint8_t add8_usat(uint8_t a, uint8_t b)
10921{
10922 uint8_t res;
10923 res = a + b;
10924 if (res < a)
10925 res = 0xff;
10926 return res;
10927}
10928
10929static inline uint8_t sub8_usat(uint8_t a, uint8_t b)
10930{
4c4fd3f8 10931 if (a > b)
6ddbc6e4
PB
10932 return a - b;
10933 else
10934 return 0;
10935}
10936
10937#define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16);
10938#define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16);
10939#define ADD8(a, b, n) RESULT(add8_usat(a, b), n, 8);
10940#define SUB8(a, b, n) RESULT(sub8_usat(a, b), n, 8);
10941#define PFX uq
10942
10943#include "op_addsub.h"
10944
10945/* Signed modulo arithmetic. */
10946#define SARITH16(a, b, n, op) do { \
10947 int32_t sum; \
db6e2e65 10948 sum = (int32_t)(int16_t)(a) op (int32_t)(int16_t)(b); \
6ddbc6e4
PB
10949 RESULT(sum, n, 16); \
10950 if (sum >= 0) \
10951 ge |= 3 << (n * 2); \
10952 } while(0)
10953
10954#define SARITH8(a, b, n, op) do { \
10955 int32_t sum; \
db6e2e65 10956 sum = (int32_t)(int8_t)(a) op (int32_t)(int8_t)(b); \
6ddbc6e4
PB
10957 RESULT(sum, n, 8); \
10958 if (sum >= 0) \
10959 ge |= 1 << n; \
10960 } while(0)
10961
10962
10963#define ADD16(a, b, n) SARITH16(a, b, n, +)
10964#define SUB16(a, b, n) SARITH16(a, b, n, -)
10965#define ADD8(a, b, n) SARITH8(a, b, n, +)
10966#define SUB8(a, b, n) SARITH8(a, b, n, -)
10967#define PFX s
10968#define ARITH_GE
10969
10970#include "op_addsub.h"
10971
10972/* Unsigned modulo arithmetic. */
10973#define ADD16(a, b, n) do { \
10974 uint32_t sum; \
10975 sum = (uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b); \
10976 RESULT(sum, n, 16); \
a87aa10b 10977 if ((sum >> 16) == 1) \
6ddbc6e4
PB
10978 ge |= 3 << (n * 2); \
10979 } while(0)
10980
10981#define ADD8(a, b, n) do { \
10982 uint32_t sum; \
10983 sum = (uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b); \
10984 RESULT(sum, n, 8); \
a87aa10b
AZ
10985 if ((sum >> 8) == 1) \
10986 ge |= 1 << n; \
6ddbc6e4
PB
10987 } while(0)
10988
10989#define SUB16(a, b, n) do { \
10990 uint32_t sum; \
10991 sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \
10992 RESULT(sum, n, 16); \
10993 if ((sum >> 16) == 0) \
10994 ge |= 3 << (n * 2); \
10995 } while(0)
10996
10997#define SUB8(a, b, n) do { \
10998 uint32_t sum; \
10999 sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \
11000 RESULT(sum, n, 8); \
11001 if ((sum >> 8) == 0) \
a87aa10b 11002 ge |= 1 << n; \
6ddbc6e4
PB
11003 } while(0)
11004
11005#define PFX u
11006#define ARITH_GE
11007
11008#include "op_addsub.h"
11009
11010/* Halved signed arithmetic. */
11011#define ADD16(a, b, n) \
11012 RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16)
11013#define SUB16(a, b, n) \
11014 RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16)
11015#define ADD8(a, b, n) \
11016 RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8)
11017#define SUB8(a, b, n) \
11018 RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8)
11019#define PFX sh
11020
11021#include "op_addsub.h"
11022
11023/* Halved unsigned arithmetic. */
11024#define ADD16(a, b, n) \
11025 RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16)
11026#define SUB16(a, b, n) \
11027 RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16)
11028#define ADD8(a, b, n) \
11029 RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8)
11030#define SUB8(a, b, n) \
11031 RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8)
11032#define PFX uh
11033
11034#include "op_addsub.h"
11035
11036static inline uint8_t do_usad(uint8_t a, uint8_t b)
11037{
11038 if (a > b)
11039 return a - b;
11040 else
11041 return b - a;
11042}
11043
11044/* Unsigned sum of absolute byte differences. */
11045uint32_t HELPER(usad8)(uint32_t a, uint32_t b)
11046{
11047 uint32_t sum;
11048 sum = do_usad(a, b);
11049 sum += do_usad(a >> 8, b >> 8);
11050 sum += do_usad(a >> 16, b >>16);
11051 sum += do_usad(a >> 24, b >> 24);
11052 return sum;
11053}
11054
11055/* For ARMv6 SEL instruction. */
11056uint32_t HELPER(sel_flags)(uint32_t flags, uint32_t a, uint32_t b)
11057{
11058 uint32_t mask;
11059
11060 mask = 0;
11061 if (flags & 1)
11062 mask |= 0xff;
11063 if (flags & 2)
11064 mask |= 0xff00;
11065 if (flags & 4)
11066 mask |= 0xff0000;
11067 if (flags & 8)
11068 mask |= 0xff000000;
11069 return (a & mask) | (b & ~mask);
11070}
11071
b90372ad
PM
11072/* VFP support. We follow the convention used for VFP instructions:
11073 Single precision routines have a "s" suffix, double precision a
4373f3ce
PB
11074 "d" suffix. */
11075
11076/* Convert host exception flags to vfp form. */
11077static inline int vfp_exceptbits_from_host(int host_bits)
11078{
11079 int target_bits = 0;
11080
11081 if (host_bits & float_flag_invalid)
11082 target_bits |= 1;
11083 if (host_bits & float_flag_divbyzero)
11084 target_bits |= 2;
11085 if (host_bits & float_flag_overflow)
11086 target_bits |= 4;
36802b6b 11087 if (host_bits & (float_flag_underflow | float_flag_output_denormal))
4373f3ce
PB
11088 target_bits |= 8;
11089 if (host_bits & float_flag_inexact)
11090 target_bits |= 0x10;
cecd8504
PM
11091 if (host_bits & float_flag_input_denormal)
11092 target_bits |= 0x80;
4373f3ce
PB
11093 return target_bits;
11094}
11095
0ecb72a5 11096uint32_t HELPER(vfp_get_fpscr)(CPUARMState *env)
4373f3ce
PB
11097{
11098 int i;
11099 uint32_t fpscr;
11100
11101 fpscr = (env->vfp.xregs[ARM_VFP_FPSCR] & 0xffc8ffff)
11102 | (env->vfp.vec_len << 16)
11103 | (env->vfp.vec_stride << 20);
11104 i = get_float_exception_flags(&env->vfp.fp_status);
3a492f3a 11105 i |= get_float_exception_flags(&env->vfp.standard_fp_status);
d81ce0ef 11106 i |= get_float_exception_flags(&env->vfp.fp_status_f16);
4373f3ce
PB
11107 fpscr |= vfp_exceptbits_from_host(i);
11108 return fpscr;
11109}
11110
0ecb72a5 11111uint32_t vfp_get_fpscr(CPUARMState *env)
01653295
PM
11112{
11113 return HELPER(vfp_get_fpscr)(env);
11114}
11115
4373f3ce
PB
11116/* Convert vfp exception flags to target form. */
11117static inline int vfp_exceptbits_to_host(int target_bits)
11118{
11119 int host_bits = 0;
11120
11121 if (target_bits & 1)
11122 host_bits |= float_flag_invalid;
11123 if (target_bits & 2)
11124 host_bits |= float_flag_divbyzero;
11125 if (target_bits & 4)
11126 host_bits |= float_flag_overflow;
11127 if (target_bits & 8)
11128 host_bits |= float_flag_underflow;
11129 if (target_bits & 0x10)
11130 host_bits |= float_flag_inexact;
cecd8504
PM
11131 if (target_bits & 0x80)
11132 host_bits |= float_flag_input_denormal;
4373f3ce
PB
11133 return host_bits;
11134}
11135
0ecb72a5 11136void HELPER(vfp_set_fpscr)(CPUARMState *env, uint32_t val)
4373f3ce
PB
11137{
11138 int i;
11139 uint32_t changed;
11140
11141 changed = env->vfp.xregs[ARM_VFP_FPSCR];
11142 env->vfp.xregs[ARM_VFP_FPSCR] = (val & 0xffc8ffff);
11143 env->vfp.vec_len = (val >> 16) & 7;
11144 env->vfp.vec_stride = (val >> 20) & 3;
11145
11146 changed ^= val;
11147 if (changed & (3 << 22)) {
11148 i = (val >> 22) & 3;
11149 switch (i) {
4d3da0f3 11150 case FPROUNDING_TIEEVEN:
4373f3ce
PB
11151 i = float_round_nearest_even;
11152 break;
4d3da0f3 11153 case FPROUNDING_POSINF:
4373f3ce
PB
11154 i = float_round_up;
11155 break;
4d3da0f3 11156 case FPROUNDING_NEGINF:
4373f3ce
PB
11157 i = float_round_down;
11158 break;
4d3da0f3 11159 case FPROUNDING_ZERO:
4373f3ce
PB
11160 i = float_round_to_zero;
11161 break;
11162 }
11163 set_float_rounding_mode(i, &env->vfp.fp_status);
d81ce0ef 11164 set_float_rounding_mode(i, &env->vfp.fp_status_f16);
4373f3ce 11165 }
d81ce0ef
AB
11166 if (changed & FPCR_FZ16) {
11167 bool ftz_enabled = val & FPCR_FZ16;
11168 set_flush_to_zero(ftz_enabled, &env->vfp.fp_status_f16);
11169 set_flush_inputs_to_zero(ftz_enabled, &env->vfp.fp_status_f16);
11170 }
11171 if (changed & FPCR_FZ) {
11172 bool ftz_enabled = val & FPCR_FZ;
11173 set_flush_to_zero(ftz_enabled, &env->vfp.fp_status);
11174 set_flush_inputs_to_zero(ftz_enabled, &env->vfp.fp_status);
11175 }
11176 if (changed & FPCR_DN) {
11177 bool dnan_enabled = val & FPCR_DN;
11178 set_default_nan_mode(dnan_enabled, &env->vfp.fp_status);
11179 set_default_nan_mode(dnan_enabled, &env->vfp.fp_status_f16);
cecd8504 11180 }
4373f3ce 11181
d81ce0ef
AB
11182 /* The exception flags are ORed together when we read fpscr so we
11183 * only need to preserve the current state in one of our
11184 * float_status values.
11185 */
b12c390b 11186 i = vfp_exceptbits_to_host(val);
4373f3ce 11187 set_float_exception_flags(i, &env->vfp.fp_status);
d81ce0ef 11188 set_float_exception_flags(0, &env->vfp.fp_status_f16);
3a492f3a 11189 set_float_exception_flags(0, &env->vfp.standard_fp_status);
4373f3ce
PB
11190}
11191
0ecb72a5 11192void vfp_set_fpscr(CPUARMState *env, uint32_t val)
01653295
PM
11193{
11194 HELPER(vfp_set_fpscr)(env, val);
11195}
11196
4373f3ce
PB
11197#define VFP_HELPER(name, p) HELPER(glue(glue(vfp_,name),p))
11198
11199#define VFP_BINOP(name) \
ae1857ec 11200float32 VFP_HELPER(name, s)(float32 a, float32 b, void *fpstp) \
4373f3ce 11201{ \
ae1857ec
PM
11202 float_status *fpst = fpstp; \
11203 return float32_ ## name(a, b, fpst); \
4373f3ce 11204} \
ae1857ec 11205float64 VFP_HELPER(name, d)(float64 a, float64 b, void *fpstp) \
4373f3ce 11206{ \
ae1857ec
PM
11207 float_status *fpst = fpstp; \
11208 return float64_ ## name(a, b, fpst); \
4373f3ce
PB
11209}
11210VFP_BINOP(add)
11211VFP_BINOP(sub)
11212VFP_BINOP(mul)
11213VFP_BINOP(div)
f71a2ae5
PM
11214VFP_BINOP(min)
11215VFP_BINOP(max)
11216VFP_BINOP(minnum)
11217VFP_BINOP(maxnum)
4373f3ce
PB
11218#undef VFP_BINOP
11219
11220float32 VFP_HELPER(neg, s)(float32 a)
11221{
11222 return float32_chs(a);
11223}
11224
11225float64 VFP_HELPER(neg, d)(float64 a)
11226{
66230e0d 11227 return float64_chs(a);
4373f3ce
PB
11228}
11229
11230float32 VFP_HELPER(abs, s)(float32 a)
11231{
11232 return float32_abs(a);
11233}
11234
11235float64 VFP_HELPER(abs, d)(float64 a)
11236{
66230e0d 11237 return float64_abs(a);
4373f3ce
PB
11238}
11239
0ecb72a5 11240float32 VFP_HELPER(sqrt, s)(float32 a, CPUARMState *env)
4373f3ce
PB
11241{
11242 return float32_sqrt(a, &env->vfp.fp_status);
11243}
11244
0ecb72a5 11245float64 VFP_HELPER(sqrt, d)(float64 a, CPUARMState *env)
4373f3ce
PB
11246{
11247 return float64_sqrt(a, &env->vfp.fp_status);
11248}
11249
11250/* XXX: check quiet/signaling case */
11251#define DO_VFP_cmp(p, type) \
0ecb72a5 11252void VFP_HELPER(cmp, p)(type a, type b, CPUARMState *env) \
4373f3ce
PB
11253{ \
11254 uint32_t flags; \
11255 switch(type ## _compare_quiet(a, b, &env->vfp.fp_status)) { \
11256 case 0: flags = 0x6; break; \
11257 case -1: flags = 0x8; break; \
11258 case 1: flags = 0x2; break; \
11259 default: case 2: flags = 0x3; break; \
11260 } \
11261 env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \
11262 | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \
11263} \
0ecb72a5 11264void VFP_HELPER(cmpe, p)(type a, type b, CPUARMState *env) \
4373f3ce
PB
11265{ \
11266 uint32_t flags; \
11267 switch(type ## _compare(a, b, &env->vfp.fp_status)) { \
11268 case 0: flags = 0x6; break; \
11269 case -1: flags = 0x8; break; \
11270 case 1: flags = 0x2; break; \
11271 default: case 2: flags = 0x3; break; \
11272 } \
11273 env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \
11274 | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \
11275}
11276DO_VFP_cmp(s, float32)
11277DO_VFP_cmp(d, float64)
11278#undef DO_VFP_cmp
11279
5500b06c 11280/* Integer to float and float to integer conversions */
4373f3ce 11281
5500b06c
PM
11282#define CONV_ITOF(name, fsz, sign) \
11283 float##fsz HELPER(name)(uint32_t x, void *fpstp) \
11284{ \
11285 float_status *fpst = fpstp; \
85836979 11286 return sign##int32_to_##float##fsz((sign##int32_t)x, fpst); \
4373f3ce
PB
11287}
11288
5500b06c
PM
11289#define CONV_FTOI(name, fsz, sign, round) \
11290uint32_t HELPER(name)(float##fsz x, void *fpstp) \
11291{ \
11292 float_status *fpst = fpstp; \
11293 if (float##fsz##_is_any_nan(x)) { \
11294 float_raise(float_flag_invalid, fpst); \
11295 return 0; \
11296 } \
11297 return float##fsz##_to_##sign##int32##round(x, fpst); \
4373f3ce
PB
11298}
11299
5500b06c
PM
11300#define FLOAT_CONVS(name, p, fsz, sign) \
11301CONV_ITOF(vfp_##name##to##p, fsz, sign) \
11302CONV_FTOI(vfp_to##name##p, fsz, sign, ) \
11303CONV_FTOI(vfp_to##name##z##p, fsz, sign, _round_to_zero)
4373f3ce 11304
93193190 11305FLOAT_CONVS(si, h, 16, )
5500b06c
PM
11306FLOAT_CONVS(si, s, 32, )
11307FLOAT_CONVS(si, d, 64, )
93193190 11308FLOAT_CONVS(ui, h, 16, u)
5500b06c
PM
11309FLOAT_CONVS(ui, s, 32, u)
11310FLOAT_CONVS(ui, d, 64, u)
4373f3ce 11311
5500b06c
PM
11312#undef CONV_ITOF
11313#undef CONV_FTOI
11314#undef FLOAT_CONVS
4373f3ce
PB
11315
11316/* floating point conversion */
0ecb72a5 11317float64 VFP_HELPER(fcvtd, s)(float32 x, CPUARMState *env)
4373f3ce 11318{
2d627737
PM
11319 float64 r = float32_to_float64(x, &env->vfp.fp_status);
11320 /* ARM requires that S<->D conversion of any kind of NaN generates
11321 * a quiet NaN by forcing the most significant frac bit to 1.
11322 */
af39bc8c 11323 return float64_maybe_silence_nan(r, &env->vfp.fp_status);
4373f3ce
PB
11324}
11325
0ecb72a5 11326float32 VFP_HELPER(fcvts, d)(float64 x, CPUARMState *env)
4373f3ce 11327{
2d627737
PM
11328 float32 r = float64_to_float32(x, &env->vfp.fp_status);
11329 /* ARM requires that S<->D conversion of any kind of NaN generates
11330 * a quiet NaN by forcing the most significant frac bit to 1.
11331 */
af39bc8c 11332 return float32_maybe_silence_nan(r, &env->vfp.fp_status);
4373f3ce
PB
11333}
11334
11335/* VFP3 fixed point conversion. */
16d5b3ca 11336#define VFP_CONV_FIX_FLOAT(name, p, fsz, isz, itype) \
8ed697e8
WN
11337float##fsz HELPER(vfp_##name##to##p)(uint##isz##_t x, uint32_t shift, \
11338 void *fpstp) \
4373f3ce 11339{ \
5500b06c 11340 float_status *fpst = fpstp; \
622465e1 11341 float##fsz tmp; \
8ed697e8 11342 tmp = itype##_to_##float##fsz(x, fpst); \
5500b06c 11343 return float##fsz##_scalbn(tmp, -(int)shift, fpst); \
16d5b3ca
WN
11344}
11345
abe66f70
PM
11346/* Notice that we want only input-denormal exception flags from the
11347 * scalbn operation: the other possible flags (overflow+inexact if
11348 * we overflow to infinity, output-denormal) aren't correct for the
11349 * complete scale-and-convert operation.
11350 */
16d5b3ca
WN
11351#define VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, round) \
11352uint##isz##_t HELPER(vfp_to##name##p##round)(float##fsz x, \
11353 uint32_t shift, \
11354 void *fpstp) \
4373f3ce 11355{ \
5500b06c 11356 float_status *fpst = fpstp; \
abe66f70 11357 int old_exc_flags = get_float_exception_flags(fpst); \
622465e1
PM
11358 float##fsz tmp; \
11359 if (float##fsz##_is_any_nan(x)) { \
5500b06c 11360 float_raise(float_flag_invalid, fpst); \
622465e1 11361 return 0; \
09d9487f 11362 } \
5500b06c 11363 tmp = float##fsz##_scalbn(x, shift, fpst); \
abe66f70
PM
11364 old_exc_flags |= get_float_exception_flags(fpst) \
11365 & float_flag_input_denormal; \
11366 set_float_exception_flags(old_exc_flags, fpst); \
16d5b3ca 11367 return float##fsz##_to_##itype##round(tmp, fpst); \
622465e1
PM
11368}
11369
16d5b3ca
WN
11370#define VFP_CONV_FIX(name, p, fsz, isz, itype) \
11371VFP_CONV_FIX_FLOAT(name, p, fsz, isz, itype) \
3c6a074a
WN
11372VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, _round_to_zero) \
11373VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, )
11374
11375#define VFP_CONV_FIX_A64(name, p, fsz, isz, itype) \
11376VFP_CONV_FIX_FLOAT(name, p, fsz, isz, itype) \
11377VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, )
16d5b3ca 11378
8ed697e8
WN
11379VFP_CONV_FIX(sh, d, 64, 64, int16)
11380VFP_CONV_FIX(sl, d, 64, 64, int32)
3c6a074a 11381VFP_CONV_FIX_A64(sq, d, 64, 64, int64)
8ed697e8
WN
11382VFP_CONV_FIX(uh, d, 64, 64, uint16)
11383VFP_CONV_FIX(ul, d, 64, 64, uint32)
3c6a074a 11384VFP_CONV_FIX_A64(uq, d, 64, 64, uint64)
8ed697e8
WN
11385VFP_CONV_FIX(sh, s, 32, 32, int16)
11386VFP_CONV_FIX(sl, s, 32, 32, int32)
3c6a074a 11387VFP_CONV_FIX_A64(sq, s, 32, 64, int64)
8ed697e8
WN
11388VFP_CONV_FIX(uh, s, 32, 32, uint16)
11389VFP_CONV_FIX(ul, s, 32, 32, uint32)
3c6a074a 11390VFP_CONV_FIX_A64(uq, s, 32, 64, uint64)
93193190
AB
11391VFP_CONV_FIX_A64(sl, h, 16, 32, int32)
11392VFP_CONV_FIX_A64(ul, h, 16, 32, uint32)
4373f3ce 11393#undef VFP_CONV_FIX
16d5b3ca
WN
11394#undef VFP_CONV_FIX_FLOAT
11395#undef VFP_CONV_FLOAT_FIX_ROUND
4373f3ce 11396
52a1f6a3
AG
11397/* Set the current fp rounding mode and return the old one.
11398 * The argument is a softfloat float_round_ value.
11399 */
9b049916 11400uint32_t HELPER(set_rmode)(uint32_t rmode, void *fpstp)
52a1f6a3 11401{
9b049916 11402 float_status *fp_status = fpstp;
52a1f6a3
AG
11403
11404 uint32_t prev_rmode = get_float_rounding_mode(fp_status);
11405 set_float_rounding_mode(rmode, fp_status);
11406
11407 return prev_rmode;
11408}
11409
43630e58
WN
11410/* Set the current fp rounding mode in the standard fp status and return
11411 * the old one. This is for NEON instructions that need to change the
11412 * rounding mode but wish to use the standard FPSCR values for everything
11413 * else. Always set the rounding mode back to the correct value after
11414 * modifying it.
11415 * The argument is a softfloat float_round_ value.
11416 */
11417uint32_t HELPER(set_neon_rmode)(uint32_t rmode, CPUARMState *env)
11418{
11419 float_status *fp_status = &env->vfp.standard_fp_status;
11420
11421 uint32_t prev_rmode = get_float_rounding_mode(fp_status);
11422 set_float_rounding_mode(rmode, fp_status);
11423
11424 return prev_rmode;
11425}
11426
60011498 11427/* Half precision conversions. */
0ecb72a5 11428static float32 do_fcvt_f16_to_f32(uint32_t a, CPUARMState *env, float_status *s)
60011498 11429{
60011498 11430 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
fb91678d
PM
11431 float32 r = float16_to_float32(make_float16(a), ieee, s);
11432 if (ieee) {
af39bc8c 11433 return float32_maybe_silence_nan(r, s);
fb91678d
PM
11434 }
11435 return r;
60011498
PB
11436}
11437
0ecb72a5 11438static uint32_t do_fcvt_f32_to_f16(float32 a, CPUARMState *env, float_status *s)
60011498 11439{
60011498 11440 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
fb91678d
PM
11441 float16 r = float32_to_float16(a, ieee, s);
11442 if (ieee) {
af39bc8c 11443 r = float16_maybe_silence_nan(r, s);
fb91678d
PM
11444 }
11445 return float16_val(r);
60011498
PB
11446}
11447
0ecb72a5 11448float32 HELPER(neon_fcvt_f16_to_f32)(uint32_t a, CPUARMState *env)
2d981da7
PM
11449{
11450 return do_fcvt_f16_to_f32(a, env, &env->vfp.standard_fp_status);
11451}
11452
0ecb72a5 11453uint32_t HELPER(neon_fcvt_f32_to_f16)(float32 a, CPUARMState *env)
2d981da7
PM
11454{
11455 return do_fcvt_f32_to_f16(a, env, &env->vfp.standard_fp_status);
11456}
11457
0ecb72a5 11458float32 HELPER(vfp_fcvt_f16_to_f32)(uint32_t a, CPUARMState *env)
2d981da7
PM
11459{
11460 return do_fcvt_f16_to_f32(a, env, &env->vfp.fp_status);
11461}
11462
0ecb72a5 11463uint32_t HELPER(vfp_fcvt_f32_to_f16)(float32 a, CPUARMState *env)
2d981da7
PM
11464{
11465 return do_fcvt_f32_to_f16(a, env, &env->vfp.fp_status);
11466}
11467
8900aad2
PM
11468float64 HELPER(vfp_fcvt_f16_to_f64)(uint32_t a, CPUARMState *env)
11469{
11470 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
11471 float64 r = float16_to_float64(make_float16(a), ieee, &env->vfp.fp_status);
11472 if (ieee) {
af39bc8c 11473 return float64_maybe_silence_nan(r, &env->vfp.fp_status);
8900aad2
PM
11474 }
11475 return r;
11476}
11477
11478uint32_t HELPER(vfp_fcvt_f64_to_f16)(float64 a, CPUARMState *env)
11479{
11480 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
11481 float16 r = float64_to_float16(a, ieee, &env->vfp.fp_status);
11482 if (ieee) {
af39bc8c 11483 r = float16_maybe_silence_nan(r, &env->vfp.fp_status);
8900aad2
PM
11484 }
11485 return float16_val(r);
11486}
11487
dda3ec49 11488#define float32_two make_float32(0x40000000)
6aae3df1
PM
11489#define float32_three make_float32(0x40400000)
11490#define float32_one_point_five make_float32(0x3fc00000)
dda3ec49 11491
0ecb72a5 11492float32 HELPER(recps_f32)(float32 a, float32 b, CPUARMState *env)
4373f3ce 11493{
dda3ec49
PM
11494 float_status *s = &env->vfp.standard_fp_status;
11495 if ((float32_is_infinity(a) && float32_is_zero_or_denormal(b)) ||
11496 (float32_is_infinity(b) && float32_is_zero_or_denormal(a))) {
43fe9bdb
PM
11497 if (!(float32_is_zero(a) || float32_is_zero(b))) {
11498 float_raise(float_flag_input_denormal, s);
11499 }
dda3ec49
PM
11500 return float32_two;
11501 }
11502 return float32_sub(float32_two, float32_mul(a, b, s), s);
4373f3ce
PB
11503}
11504
0ecb72a5 11505float32 HELPER(rsqrts_f32)(float32 a, float32 b, CPUARMState *env)
4373f3ce 11506{
71826966 11507 float_status *s = &env->vfp.standard_fp_status;
9ea62f57
PM
11508 float32 product;
11509 if ((float32_is_infinity(a) && float32_is_zero_or_denormal(b)) ||
11510 (float32_is_infinity(b) && float32_is_zero_or_denormal(a))) {
43fe9bdb
PM
11511 if (!(float32_is_zero(a) || float32_is_zero(b))) {
11512 float_raise(float_flag_input_denormal, s);
11513 }
6aae3df1 11514 return float32_one_point_five;
9ea62f57 11515 }
6aae3df1
PM
11516 product = float32_mul(a, b, s);
11517 return float32_div(float32_sub(float32_three, product, s), float32_two, s);
4373f3ce
PB
11518}
11519
8f8e3aa4
PB
11520/* NEON helpers. */
11521
56bf4fe2
CL
11522/* Constants 256 and 512 are used in some helpers; we avoid relying on
11523 * int->float conversions at run-time. */
11524#define float64_256 make_float64(0x4070000000000000LL)
11525#define float64_512 make_float64(0x4080000000000000LL)
5eb70735 11526#define float16_maxnorm make_float16(0x7bff)
b6d4443a
AB
11527#define float32_maxnorm make_float32(0x7f7fffff)
11528#define float64_maxnorm make_float64(0x7fefffffffffffffLL)
56bf4fe2 11529
b6d4443a
AB
11530/* Reciprocal functions
11531 *
11532 * The algorithm that must be used to calculate the estimate
5eb70735 11533 * is specified by the ARM ARM, see FPRecipEstimate()/RecipEstimate
fe0e4872 11534 */
b6d4443a 11535
5eb70735
AB
11536/* See RecipEstimate()
11537 *
11538 * input is a 9 bit fixed point number
11539 * input range 256 .. 511 for a number from 0.5 <= x < 1.0.
11540 * result range 256 .. 511 for a number from 1.0 to 511/256.
11541 */
fe0e4872 11542
5eb70735
AB
11543static int recip_estimate(int input)
11544{
11545 int a, b, r;
11546 assert(256 <= input && input < 512);
11547 a = (input * 2) + 1;
11548 b = (1 << 19) / a;
11549 r = (b + 1) >> 1;
11550 assert(256 <= r && r < 512);
11551 return r;
fe0e4872
CL
11552}
11553
5eb70735
AB
11554/*
11555 * Common wrapper to call recip_estimate
11556 *
11557 * The parameters are exponent and 64 bit fraction (without implicit
11558 * bit) where the binary point is nominally at bit 52. Returns a
11559 * float64 which can then be rounded to the appropriate size by the
11560 * callee.
11561 */
11562
11563static uint64_t call_recip_estimate(int *exp, int exp_off, uint64_t frac)
4373f3ce 11564{
5eb70735
AB
11565 uint32_t scaled, estimate;
11566 uint64_t result_frac;
11567 int result_exp;
fe0e4872 11568
5eb70735
AB
11569 /* Handle sub-normals */
11570 if (*exp == 0) {
b6d4443a 11571 if (extract64(frac, 51, 1) == 0) {
5eb70735
AB
11572 *exp = -1;
11573 frac <<= 2;
b6d4443a 11574 } else {
5eb70735 11575 frac <<= 1;
b6d4443a
AB
11576 }
11577 }
fe0e4872 11578
5eb70735
AB
11579 /* scaled = UInt('1':fraction<51:44>) */
11580 scaled = deposit32(1 << 8, 0, 8, extract64(frac, 44, 8));
11581 estimate = recip_estimate(scaled);
b6d4443a 11582
5eb70735
AB
11583 result_exp = exp_off - *exp;
11584 result_frac = deposit64(0, 44, 8, estimate);
11585 if (result_exp == 0) {
11586 result_frac = deposit64(result_frac >> 1, 51, 1, 1);
11587 } else if (result_exp == -1) {
11588 result_frac = deposit64(result_frac >> 2, 50, 2, 1);
11589 result_exp = 0;
b6d4443a
AB
11590 }
11591
5eb70735
AB
11592 *exp = result_exp;
11593
11594 return result_frac;
b6d4443a
AB
11595}
11596
11597static bool round_to_inf(float_status *fpst, bool sign_bit)
11598{
11599 switch (fpst->float_rounding_mode) {
11600 case float_round_nearest_even: /* Round to Nearest */
11601 return true;
11602 case float_round_up: /* Round to +Inf */
11603 return !sign_bit;
11604 case float_round_down: /* Round to -Inf */
11605 return sign_bit;
11606 case float_round_to_zero: /* Round to Zero */
11607 return false;
11608 }
11609
11610 g_assert_not_reached();
11611}
11612
5eb70735
AB
11613float16 HELPER(recpe_f16)(float16 input, void *fpstp)
11614{
11615 float_status *fpst = fpstp;
11616 float16 f16 = float16_squash_input_denormal(input, fpst);
11617 uint32_t f16_val = float16_val(f16);
11618 uint32_t f16_sign = float16_is_neg(f16);
11619 int f16_exp = extract32(f16_val, 10, 5);
11620 uint32_t f16_frac = extract32(f16_val, 0, 10);
11621 uint64_t f64_frac;
11622
11623 if (float16_is_any_nan(f16)) {
11624 float16 nan = f16;
11625 if (float16_is_signaling_nan(f16, fpst)) {
11626 float_raise(float_flag_invalid, fpst);
11627 nan = float16_maybe_silence_nan(f16, fpst);
11628 }
11629 if (fpst->default_nan_mode) {
11630 nan = float16_default_nan(fpst);
11631 }
11632 return nan;
11633 } else if (float16_is_infinity(f16)) {
11634 return float16_set_sign(float16_zero, float16_is_neg(f16));
11635 } else if (float16_is_zero(f16)) {
11636 float_raise(float_flag_divbyzero, fpst);
11637 return float16_set_sign(float16_infinity, float16_is_neg(f16));
11638 } else if (float16_abs(f16) < (1 << 8)) {
11639 /* Abs(value) < 2.0^-16 */
11640 float_raise(float_flag_overflow | float_flag_inexact, fpst);
11641 if (round_to_inf(fpst, f16_sign)) {
11642 return float16_set_sign(float16_infinity, f16_sign);
11643 } else {
11644 return float16_set_sign(float16_maxnorm, f16_sign);
11645 }
11646 } else if (f16_exp >= 29 && fpst->flush_to_zero) {
11647 float_raise(float_flag_underflow, fpst);
11648 return float16_set_sign(float16_zero, float16_is_neg(f16));
11649 }
11650
11651 f64_frac = call_recip_estimate(&f16_exp, 29,
11652 ((uint64_t) f16_frac) << (52 - 10));
11653
11654 /* result = sign : result_exp<4:0> : fraction<51:42> */
11655 f16_val = deposit32(0, 15, 1, f16_sign);
11656 f16_val = deposit32(f16_val, 10, 5, f16_exp);
11657 f16_val = deposit32(f16_val, 0, 10, extract64(f64_frac, 52 - 10, 10));
11658 return make_float16(f16_val);
11659}
11660
b6d4443a
AB
11661float32 HELPER(recpe_f32)(float32 input, void *fpstp)
11662{
11663 float_status *fpst = fpstp;
11664 float32 f32 = float32_squash_input_denormal(input, fpst);
11665 uint32_t f32_val = float32_val(f32);
5eb70735
AB
11666 bool f32_sign = float32_is_neg(f32);
11667 int f32_exp = extract32(f32_val, 23, 8);
b6d4443a 11668 uint32_t f32_frac = extract32(f32_val, 0, 23);
5eb70735 11669 uint64_t f64_frac;
b6d4443a
AB
11670
11671 if (float32_is_any_nan(f32)) {
11672 float32 nan = f32;
af39bc8c 11673 if (float32_is_signaling_nan(f32, fpst)) {
b6d4443a 11674 float_raise(float_flag_invalid, fpst);
af39bc8c 11675 nan = float32_maybe_silence_nan(f32, fpst);
fe0e4872 11676 }
b6d4443a 11677 if (fpst->default_nan_mode) {
af39bc8c 11678 nan = float32_default_nan(fpst);
43fe9bdb 11679 }
b6d4443a
AB
11680 return nan;
11681 } else if (float32_is_infinity(f32)) {
11682 return float32_set_sign(float32_zero, float32_is_neg(f32));
11683 } else if (float32_is_zero(f32)) {
11684 float_raise(float_flag_divbyzero, fpst);
11685 return float32_set_sign(float32_infinity, float32_is_neg(f32));
5eb70735 11686 } else if (float32_abs(f32) < (1ULL << 21)) {
b6d4443a
AB
11687 /* Abs(value) < 2.0^-128 */
11688 float_raise(float_flag_overflow | float_flag_inexact, fpst);
5eb70735
AB
11689 if (round_to_inf(fpst, f32_sign)) {
11690 return float32_set_sign(float32_infinity, f32_sign);
b6d4443a 11691 } else {
5eb70735 11692 return float32_set_sign(float32_maxnorm, f32_sign);
b6d4443a
AB
11693 }
11694 } else if (f32_exp >= 253 && fpst->flush_to_zero) {
11695 float_raise(float_flag_underflow, fpst);
11696 return float32_set_sign(float32_zero, float32_is_neg(f32));
fe0e4872
CL
11697 }
11698
5eb70735
AB
11699 f64_frac = call_recip_estimate(&f32_exp, 253,
11700 ((uint64_t) f32_frac) << (52 - 23));
fe0e4872 11701
5eb70735
AB
11702 /* result = sign : result_exp<7:0> : fraction<51:29> */
11703 f32_val = deposit32(0, 31, 1, f32_sign);
11704 f32_val = deposit32(f32_val, 23, 8, f32_exp);
11705 f32_val = deposit32(f32_val, 0, 23, extract64(f64_frac, 52 - 23, 23));
11706 return make_float32(f32_val);
b6d4443a
AB
11707}
11708
11709float64 HELPER(recpe_f64)(float64 input, void *fpstp)
11710{
11711 float_status *fpst = fpstp;
11712 float64 f64 = float64_squash_input_denormal(input, fpst);
11713 uint64_t f64_val = float64_val(f64);
5eb70735
AB
11714 bool f64_sign = float64_is_neg(f64);
11715 int f64_exp = extract64(f64_val, 52, 11);
11716 uint64_t f64_frac = extract64(f64_val, 0, 52);
b6d4443a
AB
11717
11718 /* Deal with any special cases */
11719 if (float64_is_any_nan(f64)) {
11720 float64 nan = f64;
af39bc8c 11721 if (float64_is_signaling_nan(f64, fpst)) {
b6d4443a 11722 float_raise(float_flag_invalid, fpst);
af39bc8c 11723 nan = float64_maybe_silence_nan(f64, fpst);
b6d4443a
AB
11724 }
11725 if (fpst->default_nan_mode) {
af39bc8c 11726 nan = float64_default_nan(fpst);
b6d4443a
AB
11727 }
11728 return nan;
11729 } else if (float64_is_infinity(f64)) {
11730 return float64_set_sign(float64_zero, float64_is_neg(f64));
11731 } else if (float64_is_zero(f64)) {
11732 float_raise(float_flag_divbyzero, fpst);
11733 return float64_set_sign(float64_infinity, float64_is_neg(f64));
11734 } else if ((f64_val & ~(1ULL << 63)) < (1ULL << 50)) {
11735 /* Abs(value) < 2.0^-1024 */
11736 float_raise(float_flag_overflow | float_flag_inexact, fpst);
5eb70735
AB
11737 if (round_to_inf(fpst, f64_sign)) {
11738 return float64_set_sign(float64_infinity, f64_sign);
b6d4443a 11739 } else {
5eb70735 11740 return float64_set_sign(float64_maxnorm, f64_sign);
b6d4443a 11741 }
fc1792e9 11742 } else if (f64_exp >= 2045 && fpst->flush_to_zero) {
b6d4443a
AB
11743 float_raise(float_flag_underflow, fpst);
11744 return float64_set_sign(float64_zero, float64_is_neg(f64));
11745 }
fe0e4872 11746
5eb70735 11747 f64_frac = call_recip_estimate(&f64_exp, 2045, f64_frac);
fe0e4872 11748
5eb70735
AB
11749 /* result = sign : result_exp<10:0> : fraction<51:0>; */
11750 f64_val = deposit64(0, 63, 1, f64_sign);
11751 f64_val = deposit64(f64_val, 52, 11, f64_exp);
11752 f64_val = deposit64(f64_val, 0, 52, f64_frac);
11753 return make_float64(f64_val);
4373f3ce
PB
11754}
11755
e07be5d2
CL
11756/* The algorithm that must be used to calculate the estimate
11757 * is specified by the ARM ARM.
11758 */
c2fb418e 11759static float64 recip_sqrt_estimate(float64 a, float_status *real_fp_status)
e07be5d2 11760{
1146a817
PM
11761 /* These calculations mustn't set any fp exception flags,
11762 * so we use a local copy of the fp_status.
11763 */
c2fb418e 11764 float_status dummy_status = *real_fp_status;
1146a817 11765 float_status *s = &dummy_status;
e07be5d2
CL
11766 float64 q;
11767 int64_t q_int;
11768
11769 if (float64_lt(a, float64_half, s)) {
11770 /* range 0.25 <= a < 0.5 */
11771
11772 /* a in units of 1/512 rounded down */
11773 /* q0 = (int)(a * 512.0); */
11774 q = float64_mul(float64_512, a, s);
11775 q_int = float64_to_int64_round_to_zero(q, s);
11776
11777 /* reciprocal root r */
11778 /* r = 1.0 / sqrt(((double)q0 + 0.5) / 512.0); */
11779 q = int64_to_float64(q_int, s);
11780 q = float64_add(q, float64_half, s);
11781 q = float64_div(q, float64_512, s);
11782 q = float64_sqrt(q, s);
11783 q = float64_div(float64_one, q, s);
11784 } else {
11785 /* range 0.5 <= a < 1.0 */
11786
11787 /* a in units of 1/256 rounded down */
11788 /* q1 = (int)(a * 256.0); */
11789 q = float64_mul(float64_256, a, s);
11790 int64_t q_int = float64_to_int64_round_to_zero(q, s);
11791
11792 /* reciprocal root r */
11793 /* r = 1.0 /sqrt(((double)q1 + 0.5) / 256); */
11794 q = int64_to_float64(q_int, s);
11795 q = float64_add(q, float64_half, s);
11796 q = float64_div(q, float64_256, s);
11797 q = float64_sqrt(q, s);
11798 q = float64_div(float64_one, q, s);
11799 }
11800 /* r in units of 1/256 rounded to nearest */
11801 /* s = (int)(256.0 * r + 0.5); */
11802
11803 q = float64_mul(q, float64_256,s );
11804 q = float64_add(q, float64_half, s);
11805 q_int = float64_to_int64_round_to_zero(q, s);
11806
11807 /* return (double)s / 256.0;*/
11808 return float64_div(int64_to_float64(q_int, s), float64_256, s);
11809}
11810
c2fb418e 11811float32 HELPER(rsqrte_f32)(float32 input, void *fpstp)
4373f3ce 11812{
c2fb418e
AB
11813 float_status *s = fpstp;
11814 float32 f32 = float32_squash_input_denormal(input, s);
11815 uint32_t val = float32_val(f32);
11816 uint32_t f32_sbit = 0x80000000 & val;
11817 int32_t f32_exp = extract32(val, 23, 8);
11818 uint32_t f32_frac = extract32(val, 0, 23);
11819 uint64_t f64_frac;
11820 uint64_t val64;
e07be5d2
CL
11821 int result_exp;
11822 float64 f64;
e07be5d2 11823
c2fb418e
AB
11824 if (float32_is_any_nan(f32)) {
11825 float32 nan = f32;
af39bc8c 11826 if (float32_is_signaling_nan(f32, s)) {
e07be5d2 11827 float_raise(float_flag_invalid, s);
af39bc8c 11828 nan = float32_maybe_silence_nan(f32, s);
e07be5d2 11829 }
c2fb418e 11830 if (s->default_nan_mode) {
af39bc8c 11831 nan = float32_default_nan(s);
43fe9bdb 11832 }
c2fb418e
AB
11833 return nan;
11834 } else if (float32_is_zero(f32)) {
e07be5d2 11835 float_raise(float_flag_divbyzero, s);
c2fb418e
AB
11836 return float32_set_sign(float32_infinity, float32_is_neg(f32));
11837 } else if (float32_is_neg(f32)) {
e07be5d2 11838 float_raise(float_flag_invalid, s);
af39bc8c 11839 return float32_default_nan(s);
c2fb418e 11840 } else if (float32_is_infinity(f32)) {
e07be5d2
CL
11841 return float32_zero;
11842 }
11843
c2fb418e 11844 /* Scale and normalize to a double-precision value between 0.25 and 1.0,
e07be5d2 11845 * preserving the parity of the exponent. */
c2fb418e
AB
11846
11847 f64_frac = ((uint64_t) f32_frac) << 29;
11848 if (f32_exp == 0) {
11849 while (extract64(f64_frac, 51, 1) == 0) {
11850 f64_frac = f64_frac << 1;
11851 f32_exp = f32_exp-1;
11852 }
11853 f64_frac = extract64(f64_frac, 0, 51) << 1;
11854 }
11855
11856 if (extract64(f32_exp, 0, 1) == 0) {
11857 f64 = make_float64(((uint64_t) f32_sbit) << 32
e07be5d2 11858 | (0x3feULL << 52)
c2fb418e 11859 | f64_frac);
e07be5d2 11860 } else {
c2fb418e 11861 f64 = make_float64(((uint64_t) f32_sbit) << 32
e07be5d2 11862 | (0x3fdULL << 52)
c2fb418e 11863 | f64_frac);
e07be5d2
CL
11864 }
11865
c2fb418e 11866 result_exp = (380 - f32_exp) / 2;
e07be5d2 11867
c2fb418e 11868 f64 = recip_sqrt_estimate(f64, s);
e07be5d2
CL
11869
11870 val64 = float64_val(f64);
11871
26cc6abf 11872 val = ((result_exp & 0xff) << 23)
e07be5d2
CL
11873 | ((val64 >> 29) & 0x7fffff);
11874 return make_float32(val);
4373f3ce
PB
11875}
11876
c2fb418e
AB
11877float64 HELPER(rsqrte_f64)(float64 input, void *fpstp)
11878{
11879 float_status *s = fpstp;
11880 float64 f64 = float64_squash_input_denormal(input, s);
11881 uint64_t val = float64_val(f64);
11882 uint64_t f64_sbit = 0x8000000000000000ULL & val;
11883 int64_t f64_exp = extract64(val, 52, 11);
11884 uint64_t f64_frac = extract64(val, 0, 52);
11885 int64_t result_exp;
11886 uint64_t result_frac;
11887
11888 if (float64_is_any_nan(f64)) {
11889 float64 nan = f64;
af39bc8c 11890 if (float64_is_signaling_nan(f64, s)) {
c2fb418e 11891 float_raise(float_flag_invalid, s);
af39bc8c 11892 nan = float64_maybe_silence_nan(f64, s);
c2fb418e
AB
11893 }
11894 if (s->default_nan_mode) {
af39bc8c 11895 nan = float64_default_nan(s);
c2fb418e
AB
11896 }
11897 return nan;
11898 } else if (float64_is_zero(f64)) {
11899 float_raise(float_flag_divbyzero, s);
11900 return float64_set_sign(float64_infinity, float64_is_neg(f64));
11901 } else if (float64_is_neg(f64)) {
11902 float_raise(float_flag_invalid, s);
af39bc8c 11903 return float64_default_nan(s);
c2fb418e
AB
11904 } else if (float64_is_infinity(f64)) {
11905 return float64_zero;
11906 }
11907
11908 /* Scale and normalize to a double-precision value between 0.25 and 1.0,
11909 * preserving the parity of the exponent. */
11910
11911 if (f64_exp == 0) {
11912 while (extract64(f64_frac, 51, 1) == 0) {
11913 f64_frac = f64_frac << 1;
11914 f64_exp = f64_exp - 1;
11915 }
11916 f64_frac = extract64(f64_frac, 0, 51) << 1;
11917 }
11918
11919 if (extract64(f64_exp, 0, 1) == 0) {
11920 f64 = make_float64(f64_sbit
11921 | (0x3feULL << 52)
11922 | f64_frac);
11923 } else {
11924 f64 = make_float64(f64_sbit
11925 | (0x3fdULL << 52)
11926 | f64_frac);
11927 }
11928
11929 result_exp = (3068 - f64_exp) / 2;
11930
11931 f64 = recip_sqrt_estimate(f64, s);
11932
11933 result_frac = extract64(float64_val(f64), 0, 52);
11934
11935 return make_float64(f64_sbit |
11936 ((result_exp & 0x7ff) << 52) |
11937 result_frac);
11938}
11939
b6d4443a 11940uint32_t HELPER(recpe_u32)(uint32_t a, void *fpstp)
4373f3ce 11941{
5eb70735
AB
11942 /* float_status *s = fpstp; */
11943 int input, estimate;
fe0e4872
CL
11944
11945 if ((a & 0x80000000) == 0) {
11946 return 0xffffffff;
11947 }
11948
5eb70735
AB
11949 input = extract32(a, 23, 9);
11950 estimate = recip_estimate(input);
fe0e4872 11951
5eb70735 11952 return deposit32(0, (32 - 9), 9, estimate);
4373f3ce
PB
11953}
11954
c2fb418e 11955uint32_t HELPER(rsqrte_u32)(uint32_t a, void *fpstp)
4373f3ce 11956{
c2fb418e 11957 float_status *fpst = fpstp;
e07be5d2
CL
11958 float64 f64;
11959
11960 if ((a & 0xc0000000) == 0) {
11961 return 0xffffffff;
11962 }
11963
11964 if (a & 0x80000000) {
11965 f64 = make_float64((0x3feULL << 52)
11966 | ((uint64_t)(a & 0x7fffffff) << 21));
11967 } else { /* bits 31-30 == '01' */
11968 f64 = make_float64((0x3fdULL << 52)
11969 | ((uint64_t)(a & 0x3fffffff) << 22));
11970 }
11971
c2fb418e 11972 f64 = recip_sqrt_estimate(f64, fpst);
e07be5d2
CL
11973
11974 return 0x80000000 | ((float64_val(f64) >> 21) & 0x7fffffff);
4373f3ce 11975}
fe1479c3 11976
da97f52c
PM
11977/* VFPv4 fused multiply-accumulate */
11978float32 VFP_HELPER(muladd, s)(float32 a, float32 b, float32 c, void *fpstp)
11979{
11980 float_status *fpst = fpstp;
11981 return float32_muladd(a, b, c, 0, fpst);
11982}
11983
11984float64 VFP_HELPER(muladd, d)(float64 a, float64 b, float64 c, void *fpstp)
11985{
11986 float_status *fpst = fpstp;
11987 return float64_muladd(a, b, c, 0, fpst);
11988}
d9b0848d
PM
11989
11990/* ARMv8 round to integral */
11991float32 HELPER(rints_exact)(float32 x, void *fp_status)
11992{
11993 return float32_round_to_int(x, fp_status);
11994}
11995
11996float64 HELPER(rintd_exact)(float64 x, void *fp_status)
11997{
11998 return float64_round_to_int(x, fp_status);
11999}
12000
12001float32 HELPER(rints)(float32 x, void *fp_status)
12002{
12003 int old_flags = get_float_exception_flags(fp_status), new_flags;
12004 float32 ret;
12005
12006 ret = float32_round_to_int(x, fp_status);
12007
12008 /* Suppress any inexact exceptions the conversion produced */
12009 if (!(old_flags & float_flag_inexact)) {
12010 new_flags = get_float_exception_flags(fp_status);
12011 set_float_exception_flags(new_flags & ~float_flag_inexact, fp_status);
12012 }
12013
12014 return ret;
12015}
12016
12017float64 HELPER(rintd)(float64 x, void *fp_status)
12018{
12019 int old_flags = get_float_exception_flags(fp_status), new_flags;
12020 float64 ret;
12021
12022 ret = float64_round_to_int(x, fp_status);
12023
12024 new_flags = get_float_exception_flags(fp_status);
12025
12026 /* Suppress any inexact exceptions the conversion produced */
12027 if (!(old_flags & float_flag_inexact)) {
12028 new_flags = get_float_exception_flags(fp_status);
12029 set_float_exception_flags(new_flags & ~float_flag_inexact, fp_status);
12030 }
12031
12032 return ret;
12033}
9972da66
WN
12034
12035/* Convert ARM rounding mode to softfloat */
12036int arm_rmode_to_sf(int rmode)
12037{
12038 switch (rmode) {
12039 case FPROUNDING_TIEAWAY:
12040 rmode = float_round_ties_away;
12041 break;
12042 case FPROUNDING_ODD:
12043 /* FIXME: add support for TIEAWAY and ODD */
12044 qemu_log_mask(LOG_UNIMP, "arm: unimplemented rounding mode: %d\n",
12045 rmode);
12046 case FPROUNDING_TIEEVEN:
12047 default:
12048 rmode = float_round_nearest_even;
12049 break;
12050 case FPROUNDING_POSINF:
12051 rmode = float_round_up;
12052 break;
12053 case FPROUNDING_NEGINF:
12054 rmode = float_round_down;
12055 break;
12056 case FPROUNDING_ZERO:
12057 rmode = float_round_to_zero;
12058 break;
12059 }
12060 return rmode;
12061}
eb0ecd5a 12062
aa633469
PM
12063/* CRC helpers.
12064 * The upper bytes of val (above the number specified by 'bytes') must have
12065 * been zeroed out by the caller.
12066 */
eb0ecd5a
WN
12067uint32_t HELPER(crc32)(uint32_t acc, uint32_t val, uint32_t bytes)
12068{
12069 uint8_t buf[4];
12070
aa633469 12071 stl_le_p(buf, val);
eb0ecd5a
WN
12072
12073 /* zlib crc32 converts the accumulator and output to one's complement. */
12074 return crc32(acc ^ 0xffffffff, buf, bytes) ^ 0xffffffff;
12075}
12076
12077uint32_t HELPER(crc32c)(uint32_t acc, uint32_t val, uint32_t bytes)
12078{
12079 uint8_t buf[4];
12080
aa633469 12081 stl_le_p(buf, val);
eb0ecd5a
WN
12082
12083 /* Linux crc32c converts the output to one's complement. */
12084 return crc32c(acc, buf, bytes) ^ 0xffffffff;
12085}
a9e01311
RH
12086
12087/* Return the exception level to which FP-disabled exceptions should
12088 * be taken, or 0 if FP is enabled.
12089 */
12090static inline int fp_exception_el(CPUARMState *env)
12091{
55faa212 12092#ifndef CONFIG_USER_ONLY
a9e01311
RH
12093 int fpen;
12094 int cur_el = arm_current_el(env);
12095
12096 /* CPACR and the CPTR registers don't exist before v6, so FP is
12097 * always accessible
12098 */
12099 if (!arm_feature(env, ARM_FEATURE_V6)) {
12100 return 0;
12101 }
12102
12103 /* The CPACR controls traps to EL1, or PL1 if we're 32 bit:
12104 * 0, 2 : trap EL0 and EL1/PL1 accesses
12105 * 1 : trap only EL0 accesses
12106 * 3 : trap no accesses
12107 */
12108 fpen = extract32(env->cp15.cpacr_el1, 20, 2);
12109 switch (fpen) {
12110 case 0:
12111 case 2:
12112 if (cur_el == 0 || cur_el == 1) {
12113 /* Trap to PL1, which might be EL1 or EL3 */
12114 if (arm_is_secure(env) && !arm_el_is_aa64(env, 3)) {
12115 return 3;
12116 }
12117 return 1;
12118 }
12119 if (cur_el == 3 && !is_a64(env)) {
12120 /* Secure PL1 running at EL3 */
12121 return 3;
12122 }
12123 break;
12124 case 1:
12125 if (cur_el == 0) {
12126 return 1;
12127 }
12128 break;
12129 case 3:
12130 break;
12131 }
12132
12133 /* For the CPTR registers we don't need to guard with an ARM_FEATURE
12134 * check because zero bits in the registers mean "don't trap".
12135 */
12136
12137 /* CPTR_EL2 : present in v7VE or v8 */
12138 if (cur_el <= 2 && extract32(env->cp15.cptr_el[2], 10, 1)
12139 && !arm_is_secure_below_el3(env)) {
12140 /* Trap FP ops at EL2, NS-EL1 or NS-EL0 to EL2 */
12141 return 2;
12142 }
12143
12144 /* CPTR_EL3 : present in v8 */
12145 if (extract32(env->cp15.cptr_el[3], 10, 1)) {
12146 /* Trap all FP ops to EL3 */
12147 return 3;
12148 }
55faa212 12149#endif
a9e01311
RH
12150 return 0;
12151}
12152
12153void cpu_get_tb_cpu_state(CPUARMState *env, target_ulong *pc,
b9adaa70 12154 target_ulong *cs_base, uint32_t *pflags)
a9e01311
RH
12155{
12156 ARMMMUIdx mmu_idx = core_to_arm_mmu_idx(env, cpu_mmu_index(env, false));
1db5e96c 12157 int fp_el = fp_exception_el(env);
b9adaa70
RH
12158 uint32_t flags;
12159
a9e01311 12160 if (is_a64(env)) {
1db5e96c
RH
12161 int sve_el = sve_exception_el(env);
12162 uint32_t zcr_len;
12163
a9e01311 12164 *pc = env->pc;
b9adaa70 12165 flags = ARM_TBFLAG_AARCH64_STATE_MASK;
a9e01311 12166 /* Get control bits for tagged addresses */
b9adaa70
RH
12167 flags |= (arm_regime_tbi0(env, mmu_idx) << ARM_TBFLAG_TBI0_SHIFT);
12168 flags |= (arm_regime_tbi1(env, mmu_idx) << ARM_TBFLAG_TBI1_SHIFT);
1db5e96c
RH
12169 flags |= sve_el << ARM_TBFLAG_SVEEXC_EL_SHIFT;
12170
12171 /* If SVE is disabled, but FP is enabled,
12172 then the effective len is 0. */
12173 if (sve_el != 0 && fp_el == 0) {
12174 zcr_len = 0;
12175 } else {
12176 int current_el = arm_current_el(env);
12177
12178 zcr_len = env->vfp.zcr_el[current_el <= 1 ? 1 : current_el];
12179 zcr_len &= 0xf;
12180 if (current_el < 2 && arm_feature(env, ARM_FEATURE_EL2)) {
12181 zcr_len = MIN(zcr_len, 0xf & (uint32_t)env->vfp.zcr_el[2]);
12182 }
12183 if (current_el < 3 && arm_feature(env, ARM_FEATURE_EL3)) {
12184 zcr_len = MIN(zcr_len, 0xf & (uint32_t)env->vfp.zcr_el[3]);
12185 }
12186 }
12187 flags |= zcr_len << ARM_TBFLAG_ZCR_LEN_SHIFT;
a9e01311
RH
12188 } else {
12189 *pc = env->regs[15];
b9adaa70 12190 flags = (env->thumb << ARM_TBFLAG_THUMB_SHIFT)
a9e01311
RH
12191 | (env->vfp.vec_len << ARM_TBFLAG_VECLEN_SHIFT)
12192 | (env->vfp.vec_stride << ARM_TBFLAG_VECSTRIDE_SHIFT)
12193 | (env->condexec_bits << ARM_TBFLAG_CONDEXEC_SHIFT)
12194 | (arm_sctlr_b(env) << ARM_TBFLAG_SCTLR_B_SHIFT);
12195 if (!(access_secure_reg(env))) {
b9adaa70 12196 flags |= ARM_TBFLAG_NS_MASK;
a9e01311
RH
12197 }
12198 if (env->vfp.xregs[ARM_VFP_FPEXC] & (1 << 30)
12199 || arm_el_is_aa64(env, 1)) {
b9adaa70 12200 flags |= ARM_TBFLAG_VFPEN_MASK;
a9e01311 12201 }
b9adaa70
RH
12202 flags |= (extract32(env->cp15.c15_cpar, 0, 2)
12203 << ARM_TBFLAG_XSCALE_CPAR_SHIFT);
a9e01311
RH
12204 }
12205
b9adaa70 12206 flags |= (arm_to_core_mmu_idx(mmu_idx) << ARM_TBFLAG_MMUIDX_SHIFT);
a9e01311
RH
12207
12208 /* The SS_ACTIVE and PSTATE_SS bits correspond to the state machine
12209 * states defined in the ARM ARM for software singlestep:
12210 * SS_ACTIVE PSTATE.SS State
12211 * 0 x Inactive (the TB flag for SS is always 0)
12212 * 1 0 Active-pending
12213 * 1 1 Active-not-pending
12214 */
12215 if (arm_singlestep_active(env)) {
b9adaa70 12216 flags |= ARM_TBFLAG_SS_ACTIVE_MASK;
a9e01311
RH
12217 if (is_a64(env)) {
12218 if (env->pstate & PSTATE_SS) {
b9adaa70 12219 flags |= ARM_TBFLAG_PSTATE_SS_MASK;
a9e01311
RH
12220 }
12221 } else {
12222 if (env->uncached_cpsr & PSTATE_SS) {
b9adaa70 12223 flags |= ARM_TBFLAG_PSTATE_SS_MASK;
a9e01311
RH
12224 }
12225 }
12226 }
12227 if (arm_cpu_data_is_big_endian(env)) {
b9adaa70 12228 flags |= ARM_TBFLAG_BE_DATA_MASK;
a9e01311 12229 }
1db5e96c 12230 flags |= fp_el << ARM_TBFLAG_FPEXC_EL_SHIFT;
a9e01311
RH
12231
12232 if (arm_v7m_is_handler_mode(env)) {
b9adaa70 12233 flags |= ARM_TBFLAG_HANDLER_MASK;
a9e01311
RH
12234 }
12235
b9adaa70 12236 *pflags = flags;
a9e01311
RH
12237 *cs_base = 0;
12238}
This page took 3.183293 seconds and 4 git commands to generate.