]> Git Repo - qemu.git/blame - target/arm/helper.c
target/arm: Treat PMCCNTR as alias of PMCCNTR_EL0
[qemu.git] / target / arm / helper.c
CommitLineData
74c21bd0 1#include "qemu/osdep.h"
181962fd 2#include "target/arm/idau.h"
194cbc49 3#include "trace.h"
b5ff1b31 4#include "cpu.h"
ccd38087 5#include "internals.h"
022c62cb 6#include "exec/gdbstub.h"
2ef6175a 7#include "exec/helper-proto.h"
1de7afc9 8#include "qemu/host-utils.h"
78027bb6 9#include "sysemu/arch_init.h"
9c17d615 10#include "sysemu/sysemu.h"
1de7afc9 11#include "qemu/bitops.h"
eb0ecd5a 12#include "qemu/crc32c.h"
63c91552 13#include "exec/exec-all.h"
f08b6170 14#include "exec/cpu_ldst.h"
1d854765 15#include "arm_ldst.h"
eb0ecd5a 16#include <zlib.h> /* For crc32 */
cfe67cef 17#include "exec/semihost.h"
f3a9b694 18#include "sysemu/kvm.h"
24f91e81 19#include "fpu/softfloat.h"
0b03bdfc 20
352c98e5
LV
21#define ARM_CPU_FREQ 1000000000 /* FIXME: 1 GHz, should be configurable */
22
4a501606 23#ifndef CONFIG_USER_ONLY
5b2d261d
AB
24/* Cacheability and shareability attributes for a memory access */
25typedef struct ARMCacheAttrs {
26 unsigned int attrs:8; /* as in the MAIR register encoding */
27 unsigned int shareability:2; /* as in the SH field of the VMSAv8-64 PTEs */
28} ARMCacheAttrs;
29
af51f566 30static bool get_phys_addr(CPUARMState *env, target_ulong address,
03ae85f8 31 MMUAccessType access_type, ARMMMUIdx mmu_idx,
af51f566 32 hwaddr *phys_ptr, MemTxAttrs *attrs, int *prot,
bc52bfeb 33 target_ulong *page_size,
5b2d261d 34 ARMMMUFaultInfo *fi, ARMCacheAttrs *cacheattrs);
7c2cb42b 35
37785977 36static bool get_phys_addr_lpae(CPUARMState *env, target_ulong address,
03ae85f8 37 MMUAccessType access_type, ARMMMUIdx mmu_idx,
37785977 38 hwaddr *phys_ptr, MemTxAttrs *txattrs, int *prot,
da909b2c 39 target_ulong *page_size_ptr,
5b2d261d 40 ARMMMUFaultInfo *fi, ARMCacheAttrs *cacheattrs);
37785977 41
35337cc3
PM
42/* Security attributes for an address, as returned by v8m_security_lookup. */
43typedef struct V8M_SAttributes {
44 bool ns;
45 bool nsc;
46 uint8_t sregion;
47 bool srvalid;
48 uint8_t iregion;
49 bool irvalid;
50} V8M_SAttributes;
51
333e10c5
PM
52static void v8m_security_lookup(CPUARMState *env, uint32_t address,
53 MMUAccessType access_type, ARMMMUIdx mmu_idx,
54 V8M_SAttributes *sattrs);
55
7c2cb42b
AF
56/* Definitions for the PMCCNTR and PMCR registers */
57#define PMCRD 0x8
58#define PMCRC 0x4
59#define PMCRE 0x1
4a501606
PM
60#endif
61
0ecb72a5 62static int vfp_gdb_get_reg(CPUARMState *env, uint8_t *buf, int reg)
56aebc89
PB
63{
64 int nregs;
65
66 /* VFP data registers are always little-endian. */
67 nregs = arm_feature(env, ARM_FEATURE_VFP3) ? 32 : 16;
68 if (reg < nregs) {
9a2b5256 69 stq_le_p(buf, *aa32_vfp_dreg(env, reg));
56aebc89
PB
70 return 8;
71 }
72 if (arm_feature(env, ARM_FEATURE_NEON)) {
73 /* Aliases for Q regs. */
74 nregs += 16;
75 if (reg < nregs) {
9a2b5256
RH
76 uint64_t *q = aa32_vfp_qreg(env, reg - 32);
77 stq_le_p(buf, q[0]);
78 stq_le_p(buf + 8, q[1]);
56aebc89
PB
79 return 16;
80 }
81 }
82 switch (reg - nregs) {
83 case 0: stl_p(buf, env->vfp.xregs[ARM_VFP_FPSID]); return 4;
84 case 1: stl_p(buf, env->vfp.xregs[ARM_VFP_FPSCR]); return 4;
85 case 2: stl_p(buf, env->vfp.xregs[ARM_VFP_FPEXC]); return 4;
86 }
87 return 0;
88}
89
0ecb72a5 90static int vfp_gdb_set_reg(CPUARMState *env, uint8_t *buf, int reg)
56aebc89
PB
91{
92 int nregs;
93
94 nregs = arm_feature(env, ARM_FEATURE_VFP3) ? 32 : 16;
95 if (reg < nregs) {
9a2b5256 96 *aa32_vfp_dreg(env, reg) = ldq_le_p(buf);
56aebc89
PB
97 return 8;
98 }
99 if (arm_feature(env, ARM_FEATURE_NEON)) {
100 nregs += 16;
101 if (reg < nregs) {
9a2b5256
RH
102 uint64_t *q = aa32_vfp_qreg(env, reg - 32);
103 q[0] = ldq_le_p(buf);
104 q[1] = ldq_le_p(buf + 8);
56aebc89
PB
105 return 16;
106 }
107 }
108 switch (reg - nregs) {
109 case 0: env->vfp.xregs[ARM_VFP_FPSID] = ldl_p(buf); return 4;
110 case 1: env->vfp.xregs[ARM_VFP_FPSCR] = ldl_p(buf); return 4;
71b3c3de 111 case 2: env->vfp.xregs[ARM_VFP_FPEXC] = ldl_p(buf) & (1 << 30); return 4;
56aebc89
PB
112 }
113 return 0;
114}
115
6a669427
PM
116static int aarch64_fpu_gdb_get_reg(CPUARMState *env, uint8_t *buf, int reg)
117{
118 switch (reg) {
119 case 0 ... 31:
120 /* 128 bit FP register */
9a2b5256
RH
121 {
122 uint64_t *q = aa64_vfp_qreg(env, reg);
123 stq_le_p(buf, q[0]);
124 stq_le_p(buf + 8, q[1]);
125 return 16;
126 }
6a669427
PM
127 case 32:
128 /* FPSR */
129 stl_p(buf, vfp_get_fpsr(env));
130 return 4;
131 case 33:
132 /* FPCR */
133 stl_p(buf, vfp_get_fpcr(env));
134 return 4;
135 default:
136 return 0;
137 }
138}
139
140static int aarch64_fpu_gdb_set_reg(CPUARMState *env, uint8_t *buf, int reg)
141{
142 switch (reg) {
143 case 0 ... 31:
144 /* 128 bit FP register */
9a2b5256
RH
145 {
146 uint64_t *q = aa64_vfp_qreg(env, reg);
147 q[0] = ldq_le_p(buf);
148 q[1] = ldq_le_p(buf + 8);
149 return 16;
150 }
6a669427
PM
151 case 32:
152 /* FPSR */
153 vfp_set_fpsr(env, ldl_p(buf));
154 return 4;
155 case 33:
156 /* FPCR */
157 vfp_set_fpcr(env, ldl_p(buf));
158 return 4;
159 default:
160 return 0;
161 }
162}
163
c4241c7d 164static uint64_t raw_read(CPUARMState *env, const ARMCPRegInfo *ri)
d4e6df63 165{
375421cc 166 assert(ri->fieldoffset);
67ed771d 167 if (cpreg_field_is_64bit(ri)) {
c4241c7d 168 return CPREG_FIELD64(env, ri);
22d9e1a9 169 } else {
c4241c7d 170 return CPREG_FIELD32(env, ri);
22d9e1a9 171 }
d4e6df63
PM
172}
173
c4241c7d
PM
174static void raw_write(CPUARMState *env, const ARMCPRegInfo *ri,
175 uint64_t value)
d4e6df63 176{
375421cc 177 assert(ri->fieldoffset);
67ed771d 178 if (cpreg_field_is_64bit(ri)) {
22d9e1a9
PM
179 CPREG_FIELD64(env, ri) = value;
180 } else {
181 CPREG_FIELD32(env, ri) = value;
182 }
d4e6df63
PM
183}
184
11f136ee
FA
185static void *raw_ptr(CPUARMState *env, const ARMCPRegInfo *ri)
186{
187 return (char *)env + ri->fieldoffset;
188}
189
49a66191 190uint64_t read_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri)
721fae12 191{
59a1c327 192 /* Raw read of a coprocessor register (as needed for migration, etc). */
721fae12 193 if (ri->type & ARM_CP_CONST) {
59a1c327 194 return ri->resetvalue;
721fae12 195 } else if (ri->raw_readfn) {
59a1c327 196 return ri->raw_readfn(env, ri);
721fae12 197 } else if (ri->readfn) {
59a1c327 198 return ri->readfn(env, ri);
721fae12 199 } else {
59a1c327 200 return raw_read(env, ri);
721fae12 201 }
721fae12
PM
202}
203
59a1c327 204static void write_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri,
7900e9f1 205 uint64_t v)
721fae12
PM
206{
207 /* Raw write of a coprocessor register (as needed for migration, etc).
721fae12
PM
208 * Note that constant registers are treated as write-ignored; the
209 * caller should check for success by whether a readback gives the
210 * value written.
211 */
212 if (ri->type & ARM_CP_CONST) {
59a1c327 213 return;
721fae12 214 } else if (ri->raw_writefn) {
c4241c7d 215 ri->raw_writefn(env, ri, v);
721fae12 216 } else if (ri->writefn) {
c4241c7d 217 ri->writefn(env, ri, v);
721fae12 218 } else {
afb2530f 219 raw_write(env, ri, v);
721fae12 220 }
721fae12
PM
221}
222
375421cc
PM
223static bool raw_accessors_invalid(const ARMCPRegInfo *ri)
224{
225 /* Return true if the regdef would cause an assertion if you called
226 * read_raw_cp_reg() or write_raw_cp_reg() on it (ie if it is a
227 * program bug for it not to have the NO_RAW flag).
228 * NB that returning false here doesn't necessarily mean that calling
229 * read/write_raw_cp_reg() is safe, because we can't distinguish "has
230 * read/write access functions which are safe for raw use" from "has
231 * read/write access functions which have side effects but has forgotten
232 * to provide raw access functions".
233 * The tests here line up with the conditions in read/write_raw_cp_reg()
234 * and assertions in raw_read()/raw_write().
235 */
236 if ((ri->type & ARM_CP_CONST) ||
237 ri->fieldoffset ||
238 ((ri->raw_writefn || ri->writefn) && (ri->raw_readfn || ri->readfn))) {
239 return false;
240 }
241 return true;
242}
243
721fae12
PM
244bool write_cpustate_to_list(ARMCPU *cpu)
245{
246 /* Write the coprocessor state from cpu->env to the (index,value) list. */
247 int i;
248 bool ok = true;
249
250 for (i = 0; i < cpu->cpreg_array_len; i++) {
251 uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
252 const ARMCPRegInfo *ri;
59a1c327 253
60322b39 254 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
721fae12
PM
255 if (!ri) {
256 ok = false;
257 continue;
258 }
7a0e58fa 259 if (ri->type & ARM_CP_NO_RAW) {
721fae12
PM
260 continue;
261 }
59a1c327 262 cpu->cpreg_values[i] = read_raw_cp_reg(&cpu->env, ri);
721fae12
PM
263 }
264 return ok;
265}
266
267bool write_list_to_cpustate(ARMCPU *cpu)
268{
269 int i;
270 bool ok = true;
271
272 for (i = 0; i < cpu->cpreg_array_len; i++) {
273 uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
274 uint64_t v = cpu->cpreg_values[i];
721fae12
PM
275 const ARMCPRegInfo *ri;
276
60322b39 277 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
721fae12
PM
278 if (!ri) {
279 ok = false;
280 continue;
281 }
7a0e58fa 282 if (ri->type & ARM_CP_NO_RAW) {
721fae12
PM
283 continue;
284 }
285 /* Write value and confirm it reads back as written
286 * (to catch read-only registers and partially read-only
287 * registers where the incoming migration value doesn't match)
288 */
59a1c327
PM
289 write_raw_cp_reg(&cpu->env, ri, v);
290 if (read_raw_cp_reg(&cpu->env, ri) != v) {
721fae12
PM
291 ok = false;
292 }
293 }
294 return ok;
295}
296
297static void add_cpreg_to_list(gpointer key, gpointer opaque)
298{
299 ARMCPU *cpu = opaque;
300 uint64_t regidx;
301 const ARMCPRegInfo *ri;
302
303 regidx = *(uint32_t *)key;
60322b39 304 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
721fae12 305
7a0e58fa 306 if (!(ri->type & (ARM_CP_NO_RAW|ARM_CP_ALIAS))) {
721fae12
PM
307 cpu->cpreg_indexes[cpu->cpreg_array_len] = cpreg_to_kvm_id(regidx);
308 /* The value array need not be initialized at this point */
309 cpu->cpreg_array_len++;
310 }
311}
312
313static void count_cpreg(gpointer key, gpointer opaque)
314{
315 ARMCPU *cpu = opaque;
316 uint64_t regidx;
317 const ARMCPRegInfo *ri;
318
319 regidx = *(uint32_t *)key;
60322b39 320 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
721fae12 321
7a0e58fa 322 if (!(ri->type & (ARM_CP_NO_RAW|ARM_CP_ALIAS))) {
721fae12
PM
323 cpu->cpreg_array_len++;
324 }
325}
326
327static gint cpreg_key_compare(gconstpointer a, gconstpointer b)
328{
cbf239b7
AR
329 uint64_t aidx = cpreg_to_kvm_id(*(uint32_t *)a);
330 uint64_t bidx = cpreg_to_kvm_id(*(uint32_t *)b);
721fae12 331
cbf239b7
AR
332 if (aidx > bidx) {
333 return 1;
334 }
335 if (aidx < bidx) {
336 return -1;
337 }
338 return 0;
721fae12
PM
339}
340
341void init_cpreg_list(ARMCPU *cpu)
342{
343 /* Initialise the cpreg_tuples[] array based on the cp_regs hash.
344 * Note that we require cpreg_tuples[] to be sorted by key ID.
345 */
57b6d95e 346 GList *keys;
721fae12
PM
347 int arraylen;
348
57b6d95e 349 keys = g_hash_table_get_keys(cpu->cp_regs);
721fae12
PM
350 keys = g_list_sort(keys, cpreg_key_compare);
351
352 cpu->cpreg_array_len = 0;
353
354 g_list_foreach(keys, count_cpreg, cpu);
355
356 arraylen = cpu->cpreg_array_len;
357 cpu->cpreg_indexes = g_new(uint64_t, arraylen);
358 cpu->cpreg_values = g_new(uint64_t, arraylen);
359 cpu->cpreg_vmstate_indexes = g_new(uint64_t, arraylen);
360 cpu->cpreg_vmstate_values = g_new(uint64_t, arraylen);
361 cpu->cpreg_vmstate_array_len = cpu->cpreg_array_len;
362 cpu->cpreg_array_len = 0;
363
364 g_list_foreach(keys, add_cpreg_to_list, cpu);
365
366 assert(cpu->cpreg_array_len == arraylen);
367
368 g_list_free(keys);
369}
370
68e9c2fe
EI
371/*
372 * Some registers are not accessible if EL3.NS=0 and EL3 is using AArch32 but
373 * they are accessible when EL3 is using AArch64 regardless of EL3.NS.
374 *
375 * access_el3_aa32ns: Used to check AArch32 register views.
376 * access_el3_aa32ns_aa64any: Used to check both AArch32/64 register views.
377 */
378static CPAccessResult access_el3_aa32ns(CPUARMState *env,
3f208fd7
PM
379 const ARMCPRegInfo *ri,
380 bool isread)
68e9c2fe
EI
381{
382 bool secure = arm_is_secure_below_el3(env);
383
384 assert(!arm_el_is_aa64(env, 3));
385 if (secure) {
386 return CP_ACCESS_TRAP_UNCATEGORIZED;
387 }
388 return CP_ACCESS_OK;
389}
390
391static CPAccessResult access_el3_aa32ns_aa64any(CPUARMState *env,
3f208fd7
PM
392 const ARMCPRegInfo *ri,
393 bool isread)
68e9c2fe
EI
394{
395 if (!arm_el_is_aa64(env, 3)) {
3f208fd7 396 return access_el3_aa32ns(env, ri, isread);
68e9c2fe
EI
397 }
398 return CP_ACCESS_OK;
399}
400
5513c3ab
PM
401/* Some secure-only AArch32 registers trap to EL3 if used from
402 * Secure EL1 (but are just ordinary UNDEF in other non-EL3 contexts).
403 * Note that an access from Secure EL1 can only happen if EL3 is AArch64.
404 * We assume that the .access field is set to PL1_RW.
405 */
406static CPAccessResult access_trap_aa32s_el1(CPUARMState *env,
3f208fd7
PM
407 const ARMCPRegInfo *ri,
408 bool isread)
5513c3ab
PM
409{
410 if (arm_current_el(env) == 3) {
411 return CP_ACCESS_OK;
412 }
413 if (arm_is_secure_below_el3(env)) {
414 return CP_ACCESS_TRAP_EL3;
415 }
416 /* This will be EL1 NS and EL2 NS, which just UNDEF */
417 return CP_ACCESS_TRAP_UNCATEGORIZED;
418}
419
187f678d
PM
420/* Check for traps to "powerdown debug" registers, which are controlled
421 * by MDCR.TDOSA
422 */
423static CPAccessResult access_tdosa(CPUARMState *env, const ARMCPRegInfo *ri,
424 bool isread)
425{
426 int el = arm_current_el(env);
427
428 if (el < 2 && (env->cp15.mdcr_el2 & MDCR_TDOSA)
429 && !arm_is_secure_below_el3(env)) {
430 return CP_ACCESS_TRAP_EL2;
431 }
432 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TDOSA)) {
433 return CP_ACCESS_TRAP_EL3;
434 }
435 return CP_ACCESS_OK;
436}
437
91b0a238
PM
438/* Check for traps to "debug ROM" registers, which are controlled
439 * by MDCR_EL2.TDRA for EL2 but by the more general MDCR_EL3.TDA for EL3.
440 */
441static CPAccessResult access_tdra(CPUARMState *env, const ARMCPRegInfo *ri,
442 bool isread)
443{
444 int el = arm_current_el(env);
445
446 if (el < 2 && (env->cp15.mdcr_el2 & MDCR_TDRA)
447 && !arm_is_secure_below_el3(env)) {
448 return CP_ACCESS_TRAP_EL2;
449 }
450 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TDA)) {
451 return CP_ACCESS_TRAP_EL3;
452 }
453 return CP_ACCESS_OK;
454}
455
d6c8cf81
PM
456/* Check for traps to general debug registers, which are controlled
457 * by MDCR_EL2.TDA for EL2 and MDCR_EL3.TDA for EL3.
458 */
459static CPAccessResult access_tda(CPUARMState *env, const ARMCPRegInfo *ri,
460 bool isread)
461{
462 int el = arm_current_el(env);
463
464 if (el < 2 && (env->cp15.mdcr_el2 & MDCR_TDA)
465 && !arm_is_secure_below_el3(env)) {
466 return CP_ACCESS_TRAP_EL2;
467 }
468 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TDA)) {
469 return CP_ACCESS_TRAP_EL3;
470 }
471 return CP_ACCESS_OK;
472}
473
1fce1ba9
PM
474/* Check for traps to performance monitor registers, which are controlled
475 * by MDCR_EL2.TPM for EL2 and MDCR_EL3.TPM for EL3.
476 */
477static CPAccessResult access_tpm(CPUARMState *env, const ARMCPRegInfo *ri,
478 bool isread)
479{
480 int el = arm_current_el(env);
481
482 if (el < 2 && (env->cp15.mdcr_el2 & MDCR_TPM)
483 && !arm_is_secure_below_el3(env)) {
484 return CP_ACCESS_TRAP_EL2;
485 }
486 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) {
487 return CP_ACCESS_TRAP_EL3;
488 }
489 return CP_ACCESS_OK;
490}
491
c4241c7d 492static void dacr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
c983fe6c 493{
00c8cb0a
AF
494 ARMCPU *cpu = arm_env_get_cpu(env);
495
8d5c773e 496 raw_write(env, ri, value);
d10eb08f 497 tlb_flush(CPU(cpu)); /* Flush TLB as domain not tracked in TLB */
c983fe6c
PM
498}
499
c4241c7d 500static void fcse_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
08de207b 501{
00c8cb0a
AF
502 ARMCPU *cpu = arm_env_get_cpu(env);
503
8d5c773e 504 if (raw_read(env, ri) != value) {
08de207b
PM
505 /* Unlike real hardware the qemu TLB uses virtual addresses,
506 * not modified virtual addresses, so this causes a TLB flush.
507 */
d10eb08f 508 tlb_flush(CPU(cpu));
8d5c773e 509 raw_write(env, ri, value);
08de207b 510 }
08de207b 511}
c4241c7d
PM
512
513static void contextidr_write(CPUARMState *env, const ARMCPRegInfo *ri,
514 uint64_t value)
08de207b 515{
00c8cb0a
AF
516 ARMCPU *cpu = arm_env_get_cpu(env);
517
452a0955 518 if (raw_read(env, ri) != value && !arm_feature(env, ARM_FEATURE_PMSA)
014406b5 519 && !extended_addresses_enabled(env)) {
08de207b
PM
520 /* For VMSA (when not using the LPAE long descriptor page table
521 * format) this register includes the ASID, so do a TLB flush.
522 * For PMSA it is purely a process ID and no action is needed.
523 */
d10eb08f 524 tlb_flush(CPU(cpu));
08de207b 525 }
8d5c773e 526 raw_write(env, ri, value);
08de207b
PM
527}
528
c4241c7d
PM
529static void tlbiall_write(CPUARMState *env, const ARMCPRegInfo *ri,
530 uint64_t value)
d929823f
PM
531{
532 /* Invalidate all (TLBIALL) */
00c8cb0a
AF
533 ARMCPU *cpu = arm_env_get_cpu(env);
534
d10eb08f 535 tlb_flush(CPU(cpu));
d929823f
PM
536}
537
c4241c7d
PM
538static void tlbimva_write(CPUARMState *env, const ARMCPRegInfo *ri,
539 uint64_t value)
d929823f
PM
540{
541 /* Invalidate single TLB entry by MVA and ASID (TLBIMVA) */
31b030d4
AF
542 ARMCPU *cpu = arm_env_get_cpu(env);
543
544 tlb_flush_page(CPU(cpu), value & TARGET_PAGE_MASK);
d929823f
PM
545}
546
c4241c7d
PM
547static void tlbiasid_write(CPUARMState *env, const ARMCPRegInfo *ri,
548 uint64_t value)
d929823f
PM
549{
550 /* Invalidate by ASID (TLBIASID) */
00c8cb0a
AF
551 ARMCPU *cpu = arm_env_get_cpu(env);
552
d10eb08f 553 tlb_flush(CPU(cpu));
d929823f
PM
554}
555
c4241c7d
PM
556static void tlbimvaa_write(CPUARMState *env, const ARMCPRegInfo *ri,
557 uint64_t value)
d929823f
PM
558{
559 /* Invalidate single entry by MVA, all ASIDs (TLBIMVAA) */
31b030d4
AF
560 ARMCPU *cpu = arm_env_get_cpu(env);
561
562 tlb_flush_page(CPU(cpu), value & TARGET_PAGE_MASK);
d929823f
PM
563}
564
fa439fc5
PM
565/* IS variants of TLB operations must affect all cores */
566static void tlbiall_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
567 uint64_t value)
568{
a67cf277 569 CPUState *cs = ENV_GET_CPU(env);
fa439fc5 570
a67cf277 571 tlb_flush_all_cpus_synced(cs);
fa439fc5
PM
572}
573
574static void tlbiasid_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
575 uint64_t value)
576{
a67cf277 577 CPUState *cs = ENV_GET_CPU(env);
fa439fc5 578
a67cf277 579 tlb_flush_all_cpus_synced(cs);
fa439fc5
PM
580}
581
582static void tlbimva_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
583 uint64_t value)
584{
a67cf277 585 CPUState *cs = ENV_GET_CPU(env);
fa439fc5 586
a67cf277 587 tlb_flush_page_all_cpus_synced(cs, value & TARGET_PAGE_MASK);
fa439fc5
PM
588}
589
590static void tlbimvaa_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
591 uint64_t value)
592{
a67cf277 593 CPUState *cs = ENV_GET_CPU(env);
fa439fc5 594
a67cf277 595 tlb_flush_page_all_cpus_synced(cs, value & TARGET_PAGE_MASK);
fa439fc5
PM
596}
597
541ef8c2
SS
598static void tlbiall_nsnh_write(CPUARMState *env, const ARMCPRegInfo *ri,
599 uint64_t value)
600{
601 CPUState *cs = ENV_GET_CPU(env);
602
0336cbf8 603 tlb_flush_by_mmuidx(cs,
8bd5c820
PM
604 ARMMMUIdxBit_S12NSE1 |
605 ARMMMUIdxBit_S12NSE0 |
606 ARMMMUIdxBit_S2NS);
541ef8c2
SS
607}
608
609static void tlbiall_nsnh_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
610 uint64_t value)
611{
a67cf277 612 CPUState *cs = ENV_GET_CPU(env);
541ef8c2 613
a67cf277 614 tlb_flush_by_mmuidx_all_cpus_synced(cs,
8bd5c820
PM
615 ARMMMUIdxBit_S12NSE1 |
616 ARMMMUIdxBit_S12NSE0 |
617 ARMMMUIdxBit_S2NS);
541ef8c2
SS
618}
619
620static void tlbiipas2_write(CPUARMState *env, const ARMCPRegInfo *ri,
621 uint64_t value)
622{
623 /* Invalidate by IPA. This has to invalidate any structures that
624 * contain only stage 2 translation information, but does not need
625 * to apply to structures that contain combined stage 1 and stage 2
626 * translation information.
627 * This must NOP if EL2 isn't implemented or SCR_EL3.NS is zero.
628 */
629 CPUState *cs = ENV_GET_CPU(env);
630 uint64_t pageaddr;
631
632 if (!arm_feature(env, ARM_FEATURE_EL2) || !(env->cp15.scr_el3 & SCR_NS)) {
633 return;
634 }
635
636 pageaddr = sextract64(value << 12, 0, 40);
637
8bd5c820 638 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_S2NS);
541ef8c2
SS
639}
640
641static void tlbiipas2_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
642 uint64_t value)
643{
a67cf277 644 CPUState *cs = ENV_GET_CPU(env);
541ef8c2
SS
645 uint64_t pageaddr;
646
647 if (!arm_feature(env, ARM_FEATURE_EL2) || !(env->cp15.scr_el3 & SCR_NS)) {
648 return;
649 }
650
651 pageaddr = sextract64(value << 12, 0, 40);
652
a67cf277 653 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
8bd5c820 654 ARMMMUIdxBit_S2NS);
541ef8c2
SS
655}
656
657static void tlbiall_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
658 uint64_t value)
659{
660 CPUState *cs = ENV_GET_CPU(env);
661
8bd5c820 662 tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_S1E2);
541ef8c2
SS
663}
664
665static void tlbiall_hyp_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
666 uint64_t value)
667{
a67cf277 668 CPUState *cs = ENV_GET_CPU(env);
541ef8c2 669
8bd5c820 670 tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_S1E2);
541ef8c2
SS
671}
672
673static void tlbimva_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
674 uint64_t value)
675{
676 CPUState *cs = ENV_GET_CPU(env);
677 uint64_t pageaddr = value & ~MAKE_64BIT_MASK(0, 12);
678
8bd5c820 679 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_S1E2);
541ef8c2
SS
680}
681
682static void tlbimva_hyp_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
683 uint64_t value)
684{
a67cf277 685 CPUState *cs = ENV_GET_CPU(env);
541ef8c2
SS
686 uint64_t pageaddr = value & ~MAKE_64BIT_MASK(0, 12);
687
a67cf277 688 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
8bd5c820 689 ARMMMUIdxBit_S1E2);
541ef8c2
SS
690}
691
e9aa6c21 692static const ARMCPRegInfo cp_reginfo[] = {
54bf36ed
FA
693 /* Define the secure and non-secure FCSE identifier CP registers
694 * separately because there is no secure bank in V8 (no _EL3). This allows
695 * the secure register to be properly reset and migrated. There is also no
696 * v8 EL1 version of the register so the non-secure instance stands alone.
697 */
698 { .name = "FCSEIDR(NS)",
699 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
700 .access = PL1_RW, .secure = ARM_CP_SECSTATE_NS,
701 .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_ns),
702 .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
703 { .name = "FCSEIDR(S)",
704 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
705 .access = PL1_RW, .secure = ARM_CP_SECSTATE_S,
706 .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_s),
d4e6df63 707 .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
54bf36ed
FA
708 /* Define the secure and non-secure context identifier CP registers
709 * separately because there is no secure bank in V8 (no _EL3). This allows
710 * the secure register to be properly reset and migrated. In the
711 * non-secure case, the 32-bit register will have reset and migration
712 * disabled during registration as it is handled by the 64-bit instance.
713 */
714 { .name = "CONTEXTIDR_EL1", .state = ARM_CP_STATE_BOTH,
014406b5 715 .opc0 = 3, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
54bf36ed
FA
716 .access = PL1_RW, .secure = ARM_CP_SECSTATE_NS,
717 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[1]),
718 .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
719 { .name = "CONTEXTIDR(S)", .state = ARM_CP_STATE_AA32,
720 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
721 .access = PL1_RW, .secure = ARM_CP_SECSTATE_S,
722 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_s),
d4e6df63 723 .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
9449fdf6
PM
724 REGINFO_SENTINEL
725};
726
727static const ARMCPRegInfo not_v8_cp_reginfo[] = {
728 /* NB: Some of these registers exist in v8 but with more precise
729 * definitions that don't use CP_ANY wildcards (mostly in v8_cp_reginfo[]).
730 */
731 /* MMU Domain access control / MPU write buffer control */
0c17d68c
FA
732 { .name = "DACR",
733 .cp = 15, .opc1 = CP_ANY, .crn = 3, .crm = CP_ANY, .opc2 = CP_ANY,
734 .access = PL1_RW, .resetvalue = 0,
735 .writefn = dacr_write, .raw_writefn = raw_write,
736 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
737 offsetoflow32(CPUARMState, cp15.dacr_ns) } },
a903c449
EI
738 /* ARMv7 allocates a range of implementation defined TLB LOCKDOWN regs.
739 * For v6 and v5, these mappings are overly broad.
4fdd17dd 740 */
a903c449
EI
741 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 0,
742 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
743 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 1,
744 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
745 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 4,
746 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
747 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 8,
4fdd17dd 748 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
c4804214
PM
749 /* Cache maintenance ops; some of this space may be overridden later. */
750 { .name = "CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
751 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
752 .type = ARM_CP_NOP | ARM_CP_OVERRIDE },
e9aa6c21
PM
753 REGINFO_SENTINEL
754};
755
7d57f408
PM
756static const ARMCPRegInfo not_v6_cp_reginfo[] = {
757 /* Not all pre-v6 cores implemented this WFI, so this is slightly
758 * over-broad.
759 */
760 { .name = "WFI_v5", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = 2,
761 .access = PL1_W, .type = ARM_CP_WFI },
762 REGINFO_SENTINEL
763};
764
765static const ARMCPRegInfo not_v7_cp_reginfo[] = {
766 /* Standard v6 WFI (also used in some pre-v6 cores); not in v7 (which
767 * is UNPREDICTABLE; we choose to NOP as most implementations do).
768 */
769 { .name = "WFI_v6", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
770 .access = PL1_W, .type = ARM_CP_WFI },
34f90529
PM
771 /* L1 cache lockdown. Not architectural in v6 and earlier but in practice
772 * implemented in 926, 946, 1026, 1136, 1176 and 11MPCore. StrongARM and
773 * OMAPCP will override this space.
774 */
775 { .name = "DLOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 0,
776 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_data),
777 .resetvalue = 0 },
778 { .name = "ILOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 1,
779 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_insn),
780 .resetvalue = 0 },
776d4e5c
PM
781 /* v6 doesn't have the cache ID registers but Linux reads them anyway */
782 { .name = "DUMMY", .cp = 15, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = CP_ANY,
7a0e58fa 783 .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 784 .resetvalue = 0 },
50300698
PM
785 /* We don't implement pre-v7 debug but most CPUs had at least a DBGDIDR;
786 * implementing it as RAZ means the "debug architecture version" bits
787 * will read as a reserved value, which should cause Linux to not try
788 * to use the debug hardware.
789 */
790 { .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0,
791 .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
995939a6
PM
792 /* MMU TLB control. Note that the wildcarding means we cover not just
793 * the unified TLB ops but also the dside/iside/inner-shareable variants.
794 */
795 { .name = "TLBIALL", .cp = 15, .crn = 8, .crm = CP_ANY,
796 .opc1 = CP_ANY, .opc2 = 0, .access = PL1_W, .writefn = tlbiall_write,
7a0e58fa 797 .type = ARM_CP_NO_RAW },
995939a6
PM
798 { .name = "TLBIMVA", .cp = 15, .crn = 8, .crm = CP_ANY,
799 .opc1 = CP_ANY, .opc2 = 1, .access = PL1_W, .writefn = tlbimva_write,
7a0e58fa 800 .type = ARM_CP_NO_RAW },
995939a6
PM
801 { .name = "TLBIASID", .cp = 15, .crn = 8, .crm = CP_ANY,
802 .opc1 = CP_ANY, .opc2 = 2, .access = PL1_W, .writefn = tlbiasid_write,
7a0e58fa 803 .type = ARM_CP_NO_RAW },
995939a6
PM
804 { .name = "TLBIMVAA", .cp = 15, .crn = 8, .crm = CP_ANY,
805 .opc1 = CP_ANY, .opc2 = 3, .access = PL1_W, .writefn = tlbimvaa_write,
7a0e58fa 806 .type = ARM_CP_NO_RAW },
a903c449
EI
807 { .name = "PRRR", .cp = 15, .crn = 10, .crm = 2,
808 .opc1 = 0, .opc2 = 0, .access = PL1_RW, .type = ARM_CP_NOP },
809 { .name = "NMRR", .cp = 15, .crn = 10, .crm = 2,
810 .opc1 = 0, .opc2 = 1, .access = PL1_RW, .type = ARM_CP_NOP },
7d57f408
PM
811 REGINFO_SENTINEL
812};
813
c4241c7d
PM
814static void cpacr_write(CPUARMState *env, const ARMCPRegInfo *ri,
815 uint64_t value)
2771db27 816{
f0aff255
FA
817 uint32_t mask = 0;
818
819 /* In ARMv8 most bits of CPACR_EL1 are RES0. */
820 if (!arm_feature(env, ARM_FEATURE_V8)) {
821 /* ARMv7 defines bits for unimplemented coprocessors as RAZ/WI.
822 * ASEDIS [31] and D32DIS [30] are both UNK/SBZP without VFP.
823 * TRCDIS [28] is RAZ/WI since we do not implement a trace macrocell.
824 */
825 if (arm_feature(env, ARM_FEATURE_VFP)) {
826 /* VFP coprocessor: cp10 & cp11 [23:20] */
827 mask |= (1 << 31) | (1 << 30) | (0xf << 20);
828
829 if (!arm_feature(env, ARM_FEATURE_NEON)) {
830 /* ASEDIS [31] bit is RAO/WI */
831 value |= (1 << 31);
832 }
833
834 /* VFPv3 and upwards with NEON implement 32 double precision
835 * registers (D0-D31).
836 */
837 if (!arm_feature(env, ARM_FEATURE_NEON) ||
838 !arm_feature(env, ARM_FEATURE_VFP3)) {
839 /* D32DIS [30] is RAO/WI if D16-31 are not implemented. */
840 value |= (1 << 30);
841 }
842 }
843 value &= mask;
2771db27 844 }
7ebd5f2e 845 env->cp15.cpacr_el1 = value;
2771db27
PM
846}
847
3f208fd7
PM
848static CPAccessResult cpacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
849 bool isread)
c6f19164
GB
850{
851 if (arm_feature(env, ARM_FEATURE_V8)) {
852 /* Check if CPACR accesses are to be trapped to EL2 */
853 if (arm_current_el(env) == 1 &&
854 (env->cp15.cptr_el[2] & CPTR_TCPAC) && !arm_is_secure(env)) {
855 return CP_ACCESS_TRAP_EL2;
856 /* Check if CPACR accesses are to be trapped to EL3 */
857 } else if (arm_current_el(env) < 3 &&
858 (env->cp15.cptr_el[3] & CPTR_TCPAC)) {
859 return CP_ACCESS_TRAP_EL3;
860 }
861 }
862
863 return CP_ACCESS_OK;
864}
865
3f208fd7
PM
866static CPAccessResult cptr_access(CPUARMState *env, const ARMCPRegInfo *ri,
867 bool isread)
c6f19164
GB
868{
869 /* Check if CPTR accesses are set to trap to EL3 */
870 if (arm_current_el(env) == 2 && (env->cp15.cptr_el[3] & CPTR_TCPAC)) {
871 return CP_ACCESS_TRAP_EL3;
872 }
873
874 return CP_ACCESS_OK;
875}
876
7d57f408
PM
877static const ARMCPRegInfo v6_cp_reginfo[] = {
878 /* prefetch by MVA in v6, NOP in v7 */
879 { .name = "MVA_prefetch",
880 .cp = 15, .crn = 7, .crm = 13, .opc1 = 0, .opc2 = 1,
881 .access = PL1_W, .type = ARM_CP_NOP },
6df99dec
SS
882 /* We need to break the TB after ISB to execute self-modifying code
883 * correctly and also to take any pending interrupts immediately.
884 * So use arm_cp_write_ignore() function instead of ARM_CP_NOP flag.
885 */
7d57f408 886 { .name = "ISB", .cp = 15, .crn = 7, .crm = 5, .opc1 = 0, .opc2 = 4,
6df99dec 887 .access = PL0_W, .type = ARM_CP_NO_RAW, .writefn = arm_cp_write_ignore },
091fd17c 888 { .name = "DSB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 4,
7d57f408 889 .access = PL0_W, .type = ARM_CP_NOP },
091fd17c 890 { .name = "DMB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 5,
7d57f408 891 .access = PL0_W, .type = ARM_CP_NOP },
06d76f31 892 { .name = "IFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 2,
6cd8a264 893 .access = PL1_RW,
b848ce2b
FA
894 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ifar_s),
895 offsetof(CPUARMState, cp15.ifar_ns) },
06d76f31
PM
896 .resetvalue = 0, },
897 /* Watchpoint Fault Address Register : should actually only be present
898 * for 1136, 1176, 11MPCore.
899 */
900 { .name = "WFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 1,
901 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0, },
34222fb8 902 { .name = "CPACR", .state = ARM_CP_STATE_BOTH, .opc0 = 3,
c6f19164 903 .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 2, .accessfn = cpacr_access,
7ebd5f2e 904 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.cpacr_el1),
2771db27 905 .resetvalue = 0, .writefn = cpacr_write },
7d57f408
PM
906 REGINFO_SENTINEL
907};
908
3f208fd7
PM
909static CPAccessResult pmreg_access(CPUARMState *env, const ARMCPRegInfo *ri,
910 bool isread)
200ac0ef 911{
3b163b01 912 /* Performance monitor registers user accessibility is controlled
1fce1ba9
PM
913 * by PMUSERENR. MDCR_EL2.TPM and MDCR_EL3.TPM allow configurable
914 * trapping to EL2 or EL3 for other accesses.
200ac0ef 915 */
1fce1ba9
PM
916 int el = arm_current_el(env);
917
6ecd0b6b 918 if (el == 0 && !(env->cp15.c9_pmuserenr & 1)) {
fcd25206 919 return CP_ACCESS_TRAP;
200ac0ef 920 }
1fce1ba9
PM
921 if (el < 2 && (env->cp15.mdcr_el2 & MDCR_TPM)
922 && !arm_is_secure_below_el3(env)) {
923 return CP_ACCESS_TRAP_EL2;
924 }
925 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) {
926 return CP_ACCESS_TRAP_EL3;
927 }
928
fcd25206 929 return CP_ACCESS_OK;
200ac0ef
PM
930}
931
6ecd0b6b
AB
932static CPAccessResult pmreg_access_xevcntr(CPUARMState *env,
933 const ARMCPRegInfo *ri,
934 bool isread)
935{
936 /* ER: event counter read trap control */
937 if (arm_feature(env, ARM_FEATURE_V8)
938 && arm_current_el(env) == 0
939 && (env->cp15.c9_pmuserenr & (1 << 3)) != 0
940 && isread) {
941 return CP_ACCESS_OK;
942 }
943
944 return pmreg_access(env, ri, isread);
945}
946
947static CPAccessResult pmreg_access_swinc(CPUARMState *env,
948 const ARMCPRegInfo *ri,
949 bool isread)
950{
951 /* SW: software increment write trap control */
952 if (arm_feature(env, ARM_FEATURE_V8)
953 && arm_current_el(env) == 0
954 && (env->cp15.c9_pmuserenr & (1 << 1)) != 0
955 && !isread) {
956 return CP_ACCESS_OK;
957 }
958
959 return pmreg_access(env, ri, isread);
960}
961
7c2cb42b 962#ifndef CONFIG_USER_ONLY
87124fde 963
6ecd0b6b
AB
964static CPAccessResult pmreg_access_selr(CPUARMState *env,
965 const ARMCPRegInfo *ri,
966 bool isread)
967{
968 /* ER: event counter read trap control */
969 if (arm_feature(env, ARM_FEATURE_V8)
970 && arm_current_el(env) == 0
971 && (env->cp15.c9_pmuserenr & (1 << 3)) != 0) {
972 return CP_ACCESS_OK;
973 }
974
975 return pmreg_access(env, ri, isread);
976}
977
978static CPAccessResult pmreg_access_ccntr(CPUARMState *env,
979 const ARMCPRegInfo *ri,
980 bool isread)
981{
982 /* CR: cycle counter read trap control */
983 if (arm_feature(env, ARM_FEATURE_V8)
984 && arm_current_el(env) == 0
985 && (env->cp15.c9_pmuserenr & (1 << 2)) != 0
986 && isread) {
987 return CP_ACCESS_OK;
988 }
989
990 return pmreg_access(env, ri, isread);
991}
992
87124fde
AF
993static inline bool arm_ccnt_enabled(CPUARMState *env)
994{
995 /* This does not support checking PMCCFILTR_EL0 register */
996
ccbc0e33 997 if (!(env->cp15.c9_pmcr & PMCRE) || !(env->cp15.c9_pmcnten & (1 << 31))) {
87124fde
AF
998 return false;
999 }
1000
1001 return true;
1002}
1003
ec7b4ce4
AF
1004void pmccntr_sync(CPUARMState *env)
1005{
1006 uint64_t temp_ticks;
1007
352c98e5
LV
1008 temp_ticks = muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
1009 ARM_CPU_FREQ, NANOSECONDS_PER_SECOND);
ec7b4ce4
AF
1010
1011 if (env->cp15.c9_pmcr & PMCRD) {
1012 /* Increment once every 64 processor clock cycles */
1013 temp_ticks /= 64;
1014 }
1015
1016 if (arm_ccnt_enabled(env)) {
1017 env->cp15.c15_ccnt = temp_ticks - env->cp15.c15_ccnt;
1018 }
1019}
1020
c4241c7d
PM
1021static void pmcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1022 uint64_t value)
200ac0ef 1023{
942a155b 1024 pmccntr_sync(env);
7c2cb42b
AF
1025
1026 if (value & PMCRC) {
1027 /* The counter has been reset */
1028 env->cp15.c15_ccnt = 0;
1029 }
1030
200ac0ef
PM
1031 /* only the DP, X, D and E bits are writable */
1032 env->cp15.c9_pmcr &= ~0x39;
1033 env->cp15.c9_pmcr |= (value & 0x39);
7c2cb42b 1034
942a155b 1035 pmccntr_sync(env);
7c2cb42b
AF
1036}
1037
1038static uint64_t pmccntr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1039{
c92c0687 1040 uint64_t total_ticks;
7c2cb42b 1041
942a155b 1042 if (!arm_ccnt_enabled(env)) {
7c2cb42b
AF
1043 /* Counter is disabled, do not change value */
1044 return env->cp15.c15_ccnt;
1045 }
1046
352c98e5
LV
1047 total_ticks = muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
1048 ARM_CPU_FREQ, NANOSECONDS_PER_SECOND);
7c2cb42b
AF
1049
1050 if (env->cp15.c9_pmcr & PMCRD) {
1051 /* Increment once every 64 processor clock cycles */
1052 total_ticks /= 64;
1053 }
1054 return total_ticks - env->cp15.c15_ccnt;
1055}
1056
6b040780
WH
1057static void pmselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1058 uint64_t value)
1059{
1060 /* The value of PMSELR.SEL affects the behavior of PMXEVTYPER and
1061 * PMXEVCNTR. We allow [0..31] to be written to PMSELR here; in the
1062 * meanwhile, we check PMSELR.SEL when PMXEVTYPER and PMXEVCNTR are
1063 * accessed.
1064 */
1065 env->cp15.c9_pmselr = value & 0x1f;
1066}
1067
7c2cb42b
AF
1068static void pmccntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1069 uint64_t value)
1070{
c92c0687 1071 uint64_t total_ticks;
7c2cb42b 1072
942a155b 1073 if (!arm_ccnt_enabled(env)) {
7c2cb42b
AF
1074 /* Counter is disabled, set the absolute value */
1075 env->cp15.c15_ccnt = value;
1076 return;
1077 }
1078
352c98e5
LV
1079 total_ticks = muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
1080 ARM_CPU_FREQ, NANOSECONDS_PER_SECOND);
7c2cb42b
AF
1081
1082 if (env->cp15.c9_pmcr & PMCRD) {
1083 /* Increment once every 64 processor clock cycles */
1084 total_ticks /= 64;
1085 }
1086 env->cp15.c15_ccnt = total_ticks - value;
200ac0ef 1087}
421c7ebd
PC
1088
1089static void pmccntr_write32(CPUARMState *env, const ARMCPRegInfo *ri,
1090 uint64_t value)
1091{
1092 uint64_t cur_val = pmccntr_read(env, NULL);
1093
1094 pmccntr_write(env, ri, deposit64(cur_val, 0, 32, value));
1095}
1096
ec7b4ce4
AF
1097#else /* CONFIG_USER_ONLY */
1098
1099void pmccntr_sync(CPUARMState *env)
1100{
1101}
1102
7c2cb42b 1103#endif
200ac0ef 1104
0614601c
AF
1105static void pmccfiltr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1106 uint64_t value)
1107{
1108 pmccntr_sync(env);
1109 env->cp15.pmccfiltr_el0 = value & 0x7E000000;
1110 pmccntr_sync(env);
1111}
1112
c4241c7d 1113static void pmcntenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
200ac0ef
PM
1114 uint64_t value)
1115{
200ac0ef
PM
1116 value &= (1 << 31);
1117 env->cp15.c9_pmcnten |= value;
200ac0ef
PM
1118}
1119
c4241c7d
PM
1120static void pmcntenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1121 uint64_t value)
200ac0ef 1122{
200ac0ef
PM
1123 value &= (1 << 31);
1124 env->cp15.c9_pmcnten &= ~value;
200ac0ef
PM
1125}
1126
c4241c7d
PM
1127static void pmovsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1128 uint64_t value)
200ac0ef 1129{
200ac0ef 1130 env->cp15.c9_pmovsr &= ~value;
200ac0ef
PM
1131}
1132
c4241c7d
PM
1133static void pmxevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
1134 uint64_t value)
200ac0ef 1135{
fdb86656
WH
1136 /* Attempts to access PMXEVTYPER are CONSTRAINED UNPREDICTABLE when
1137 * PMSELR value is equal to or greater than the number of implemented
1138 * counters, but not equal to 0x1f. We opt to behave as a RAZ/WI.
1139 */
1140 if (env->cp15.c9_pmselr == 0x1f) {
1141 pmccfiltr_write(env, ri, value);
1142 }
1143}
1144
1145static uint64_t pmxevtyper_read(CPUARMState *env, const ARMCPRegInfo *ri)
1146{
1147 /* We opt to behave as a RAZ/WI when attempts to access PMXEVTYPER
1148 * are CONSTRAINED UNPREDICTABLE. See comments in pmxevtyper_write().
1149 */
1150 if (env->cp15.c9_pmselr == 0x1f) {
1151 return env->cp15.pmccfiltr_el0;
1152 } else {
1153 return 0;
1154 }
200ac0ef
PM
1155}
1156
c4241c7d 1157static void pmuserenr_write(CPUARMState *env, const ARMCPRegInfo *ri,
200ac0ef
PM
1158 uint64_t value)
1159{
6ecd0b6b
AB
1160 if (arm_feature(env, ARM_FEATURE_V8)) {
1161 env->cp15.c9_pmuserenr = value & 0xf;
1162 } else {
1163 env->cp15.c9_pmuserenr = value & 1;
1164 }
200ac0ef
PM
1165}
1166
c4241c7d
PM
1167static void pmintenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1168 uint64_t value)
200ac0ef
PM
1169{
1170 /* We have no event counters so only the C bit can be changed */
1171 value &= (1 << 31);
1172 env->cp15.c9_pminten |= value;
200ac0ef
PM
1173}
1174
c4241c7d
PM
1175static void pmintenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1176 uint64_t value)
200ac0ef
PM
1177{
1178 value &= (1 << 31);
1179 env->cp15.c9_pminten &= ~value;
200ac0ef
PM
1180}
1181
c4241c7d
PM
1182static void vbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
1183 uint64_t value)
8641136c 1184{
a505d7fe
PM
1185 /* Note that even though the AArch64 view of this register has bits
1186 * [10:0] all RES0 we can only mask the bottom 5, to comply with the
1187 * architectural requirements for bits which are RES0 only in some
1188 * contexts. (ARMv8 would permit us to do no masking at all, but ARMv7
1189 * requires the bottom five bits to be RAZ/WI because they're UNK/SBZP.)
1190 */
855ea66d 1191 raw_write(env, ri, value & ~0x1FULL);
8641136c
NR
1192}
1193
64e0e2de
EI
1194static void scr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
1195{
1196 /* We only mask off bits that are RES0 both for AArch64 and AArch32.
1197 * For bits that vary between AArch32/64, code needs to check the
1198 * current execution mode before directly using the feature bit.
1199 */
1200 uint32_t valid_mask = SCR_AARCH64_MASK | SCR_AARCH32_MASK;
1201
1202 if (!arm_feature(env, ARM_FEATURE_EL2)) {
1203 valid_mask &= ~SCR_HCE;
1204
1205 /* On ARMv7, SMD (or SCD as it is called in v7) is only
1206 * supported if EL2 exists. The bit is UNK/SBZP when
1207 * EL2 is unavailable. In QEMU ARMv7, we force it to always zero
1208 * when EL2 is unavailable.
4eb27640 1209 * On ARMv8, this bit is always available.
64e0e2de 1210 */
4eb27640
GB
1211 if (arm_feature(env, ARM_FEATURE_V7) &&
1212 !arm_feature(env, ARM_FEATURE_V8)) {
64e0e2de
EI
1213 valid_mask &= ~SCR_SMD;
1214 }
1215 }
1216
1217 /* Clear all-context RES0 bits. */
1218 value &= valid_mask;
1219 raw_write(env, ri, value);
1220}
1221
c4241c7d 1222static uint64_t ccsidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
776d4e5c
PM
1223{
1224 ARMCPU *cpu = arm_env_get_cpu(env);
b85a1fd6
FA
1225
1226 /* Acquire the CSSELR index from the bank corresponding to the CCSIDR
1227 * bank
1228 */
1229 uint32_t index = A32_BANKED_REG_GET(env, csselr,
1230 ri->secure & ARM_CP_SECSTATE_S);
1231
1232 return cpu->ccsidr[index];
776d4e5c
PM
1233}
1234
c4241c7d
PM
1235static void csselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1236 uint64_t value)
776d4e5c 1237{
8d5c773e 1238 raw_write(env, ri, value & 0xf);
776d4e5c
PM
1239}
1240
1090b9c6
PM
1241static uint64_t isr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1242{
1243 CPUState *cs = ENV_GET_CPU(env);
1244 uint64_t ret = 0;
1245
1246 if (cs->interrupt_request & CPU_INTERRUPT_HARD) {
1247 ret |= CPSR_I;
1248 }
1249 if (cs->interrupt_request & CPU_INTERRUPT_FIQ) {
1250 ret |= CPSR_F;
1251 }
1252 /* External aborts are not possible in QEMU so A bit is always clear */
1253 return ret;
1254}
1255
e9aa6c21 1256static const ARMCPRegInfo v7_cp_reginfo[] = {
7d57f408
PM
1257 /* the old v6 WFI, UNPREDICTABLE in v7 but we choose to NOP */
1258 { .name = "NOP", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
1259 .access = PL1_W, .type = ARM_CP_NOP },
200ac0ef
PM
1260 /* Performance monitors are implementation defined in v7,
1261 * but with an ARM recommended set of registers, which we
1262 * follow (although we don't actually implement any counters)
1263 *
1264 * Performance registers fall into three categories:
1265 * (a) always UNDEF in PL0, RW in PL1 (PMINTENSET, PMINTENCLR)
1266 * (b) RO in PL0 (ie UNDEF on write), RW in PL1 (PMUSERENR)
1267 * (c) UNDEF in PL0 if PMUSERENR.EN==0, otherwise accessible (all others)
1268 * For the cases controlled by PMUSERENR we must set .access to PL0_RW
1269 * or PL0_RO as appropriate and then check PMUSERENR in the helper fn.
1270 */
1271 { .name = "PMCNTENSET", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 1,
7a0e58fa 1272 .access = PL0_RW, .type = ARM_CP_ALIAS,
8521466b 1273 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
fcd25206
PM
1274 .writefn = pmcntenset_write,
1275 .accessfn = pmreg_access,
1276 .raw_writefn = raw_write },
8521466b
AF
1277 { .name = "PMCNTENSET_EL0", .state = ARM_CP_STATE_AA64,
1278 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 1,
1279 .access = PL0_RW, .accessfn = pmreg_access,
1280 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten), .resetvalue = 0,
1281 .writefn = pmcntenset_write, .raw_writefn = raw_write },
200ac0ef 1282 { .name = "PMCNTENCLR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 2,
8521466b
AF
1283 .access = PL0_RW,
1284 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
fcd25206
PM
1285 .accessfn = pmreg_access,
1286 .writefn = pmcntenclr_write,
7a0e58fa 1287 .type = ARM_CP_ALIAS },
8521466b
AF
1288 { .name = "PMCNTENCLR_EL0", .state = ARM_CP_STATE_AA64,
1289 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 2,
1290 .access = PL0_RW, .accessfn = pmreg_access,
7a0e58fa 1291 .type = ARM_CP_ALIAS,
8521466b
AF
1292 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten),
1293 .writefn = pmcntenclr_write },
200ac0ef
PM
1294 { .name = "PMOVSR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 3,
1295 .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
fcd25206
PM
1296 .accessfn = pmreg_access,
1297 .writefn = pmovsr_write,
1298 .raw_writefn = raw_write },
978364f1
AF
1299 { .name = "PMOVSCLR_EL0", .state = ARM_CP_STATE_AA64,
1300 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 3,
1301 .access = PL0_RW, .accessfn = pmreg_access,
1302 .type = ARM_CP_ALIAS,
1303 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
1304 .writefn = pmovsr_write,
1305 .raw_writefn = raw_write },
fcd25206 1306 /* Unimplemented so WI. */
200ac0ef 1307 { .name = "PMSWINC", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 4,
6ecd0b6b 1308 .access = PL0_W, .accessfn = pmreg_access_swinc, .type = ARM_CP_NOP },
7c2cb42b 1309#ifndef CONFIG_USER_ONLY
6b040780
WH
1310 { .name = "PMSELR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 5,
1311 .access = PL0_RW, .type = ARM_CP_ALIAS,
1312 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmselr),
6ecd0b6b 1313 .accessfn = pmreg_access_selr, .writefn = pmselr_write,
6b040780
WH
1314 .raw_writefn = raw_write},
1315 { .name = "PMSELR_EL0", .state = ARM_CP_STATE_AA64,
1316 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 5,
6ecd0b6b 1317 .access = PL0_RW, .accessfn = pmreg_access_selr,
6b040780
WH
1318 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmselr),
1319 .writefn = pmselr_write, .raw_writefn = raw_write, },
200ac0ef 1320 { .name = "PMCCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 0,
169c8938 1321 .access = PL0_RW, .resetvalue = 0, .type = ARM_CP_ALIAS | ARM_CP_IO,
421c7ebd 1322 .readfn = pmccntr_read, .writefn = pmccntr_write32,
6ecd0b6b 1323 .accessfn = pmreg_access_ccntr },
8521466b
AF
1324 { .name = "PMCCNTR_EL0", .state = ARM_CP_STATE_AA64,
1325 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 0,
6ecd0b6b 1326 .access = PL0_RW, .accessfn = pmreg_access_ccntr,
8521466b
AF
1327 .type = ARM_CP_IO,
1328 .readfn = pmccntr_read, .writefn = pmccntr_write, },
7c2cb42b 1329#endif
8521466b
AF
1330 { .name = "PMCCFILTR_EL0", .state = ARM_CP_STATE_AA64,
1331 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 15, .opc2 = 7,
0614601c 1332 .writefn = pmccfiltr_write,
8521466b
AF
1333 .access = PL0_RW, .accessfn = pmreg_access,
1334 .type = ARM_CP_IO,
1335 .fieldoffset = offsetof(CPUARMState, cp15.pmccfiltr_el0),
1336 .resetvalue = 0, },
200ac0ef 1337 { .name = "PMXEVTYPER", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 1,
fdb86656
WH
1338 .access = PL0_RW, .type = ARM_CP_NO_RAW, .accessfn = pmreg_access,
1339 .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
1340 { .name = "PMXEVTYPER_EL0", .state = ARM_CP_STATE_AA64,
1341 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 1,
1342 .access = PL0_RW, .type = ARM_CP_NO_RAW, .accessfn = pmreg_access,
1343 .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
fcd25206 1344 /* Unimplemented, RAZ/WI. */
200ac0ef 1345 { .name = "PMXEVCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 2,
fcd25206 1346 .access = PL0_RW, .type = ARM_CP_CONST, .resetvalue = 0,
6ecd0b6b 1347 .accessfn = pmreg_access_xevcntr },
200ac0ef 1348 { .name = "PMUSERENR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 0,
1fce1ba9 1349 .access = PL0_R | PL1_RW, .accessfn = access_tpm,
200ac0ef
PM
1350 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmuserenr),
1351 .resetvalue = 0,
d4e6df63 1352 .writefn = pmuserenr_write, .raw_writefn = raw_write },
8a83ffc2
AF
1353 { .name = "PMUSERENR_EL0", .state = ARM_CP_STATE_AA64,
1354 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 0,
1fce1ba9 1355 .access = PL0_R | PL1_RW, .accessfn = access_tpm, .type = ARM_CP_ALIAS,
8a83ffc2
AF
1356 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmuserenr),
1357 .resetvalue = 0,
1358 .writefn = pmuserenr_write, .raw_writefn = raw_write },
200ac0ef 1359 { .name = "PMINTENSET", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 1,
1fce1ba9 1360 .access = PL1_RW, .accessfn = access_tpm,
e6ec5457
WH
1361 .type = ARM_CP_ALIAS,
1362 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pminten),
200ac0ef 1363 .resetvalue = 0,
d4e6df63 1364 .writefn = pmintenset_write, .raw_writefn = raw_write },
e6ec5457
WH
1365 { .name = "PMINTENSET_EL1", .state = ARM_CP_STATE_AA64,
1366 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 1,
1367 .access = PL1_RW, .accessfn = access_tpm,
1368 .type = ARM_CP_IO,
1369 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
1370 .writefn = pmintenset_write, .raw_writefn = raw_write,
1371 .resetvalue = 0x0 },
200ac0ef 1372 { .name = "PMINTENCLR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 2,
1fce1ba9 1373 .access = PL1_RW, .accessfn = access_tpm, .type = ARM_CP_ALIAS,
200ac0ef 1374 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
b061a82b 1375 .writefn = pmintenclr_write, },
978364f1
AF
1376 { .name = "PMINTENCLR_EL1", .state = ARM_CP_STATE_AA64,
1377 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 2,
1fce1ba9 1378 .access = PL1_RW, .accessfn = access_tpm, .type = ARM_CP_ALIAS,
978364f1
AF
1379 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
1380 .writefn = pmintenclr_write },
7da845b0
PM
1381 { .name = "CCSIDR", .state = ARM_CP_STATE_BOTH,
1382 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 0,
7a0e58fa 1383 .access = PL1_R, .readfn = ccsidr_read, .type = ARM_CP_NO_RAW },
7da845b0
PM
1384 { .name = "CSSELR", .state = ARM_CP_STATE_BOTH,
1385 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 2, .opc2 = 0,
b85a1fd6
FA
1386 .access = PL1_RW, .writefn = csselr_write, .resetvalue = 0,
1387 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.csselr_s),
1388 offsetof(CPUARMState, cp15.csselr_ns) } },
776d4e5c
PM
1389 /* Auxiliary ID register: this actually has an IMPDEF value but for now
1390 * just RAZ for all cores:
1391 */
0ff644a7
PM
1392 { .name = "AIDR", .state = ARM_CP_STATE_BOTH,
1393 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 7,
776d4e5c 1394 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
f32cdad5
PM
1395 /* Auxiliary fault status registers: these also are IMPDEF, and we
1396 * choose to RAZ/WI for all cores.
1397 */
1398 { .name = "AFSR0_EL1", .state = ARM_CP_STATE_BOTH,
1399 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 0,
1400 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
1401 { .name = "AFSR1_EL1", .state = ARM_CP_STATE_BOTH,
1402 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 1,
1403 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
b0fe2427
PM
1404 /* MAIR can just read-as-written because we don't implement caches
1405 * and so don't need to care about memory attributes.
1406 */
1407 { .name = "MAIR_EL1", .state = ARM_CP_STATE_AA64,
1408 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
be693c87 1409 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[1]),
b0fe2427 1410 .resetvalue = 0 },
4cfb8ad8
PM
1411 { .name = "MAIR_EL3", .state = ARM_CP_STATE_AA64,
1412 .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 2, .opc2 = 0,
1413 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[3]),
1414 .resetvalue = 0 },
b0fe2427
PM
1415 /* For non-long-descriptor page tables these are PRRR and NMRR;
1416 * regardless they still act as reads-as-written for QEMU.
b0fe2427 1417 */
1281f8e3 1418 /* MAIR0/1 are defined separately from their 64-bit counterpart which
be693c87
GB
1419 * allows them to assign the correct fieldoffset based on the endianness
1420 * handled in the field definitions.
1421 */
a903c449 1422 { .name = "MAIR0", .state = ARM_CP_STATE_AA32,
b0fe2427 1423 .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0, .access = PL1_RW,
be693c87
GB
1424 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair0_s),
1425 offsetof(CPUARMState, cp15.mair0_ns) },
b0fe2427 1426 .resetfn = arm_cp_reset_ignore },
a903c449 1427 { .name = "MAIR1", .state = ARM_CP_STATE_AA32,
b0fe2427 1428 .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 1, .access = PL1_RW,
be693c87
GB
1429 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair1_s),
1430 offsetof(CPUARMState, cp15.mair1_ns) },
b0fe2427 1431 .resetfn = arm_cp_reset_ignore },
1090b9c6
PM
1432 { .name = "ISR_EL1", .state = ARM_CP_STATE_BOTH,
1433 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 0,
7a0e58fa 1434 .type = ARM_CP_NO_RAW, .access = PL1_R, .readfn = isr_read },
995939a6
PM
1435 /* 32 bit ITLB invalidates */
1436 { .name = "ITLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 0,
7a0e58fa 1437 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_write },
995939a6 1438 { .name = "ITLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 1,
7a0e58fa 1439 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write },
995939a6 1440 { .name = "ITLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 2,
7a0e58fa 1441 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiasid_write },
995939a6
PM
1442 /* 32 bit DTLB invalidates */
1443 { .name = "DTLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 0,
7a0e58fa 1444 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_write },
995939a6 1445 { .name = "DTLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 1,
7a0e58fa 1446 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write },
995939a6 1447 { .name = "DTLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 2,
7a0e58fa 1448 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiasid_write },
995939a6
PM
1449 /* 32 bit TLB invalidates */
1450 { .name = "TLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
7a0e58fa 1451 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_write },
995939a6 1452 { .name = "TLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
7a0e58fa 1453 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write },
995939a6 1454 { .name = "TLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
7a0e58fa 1455 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiasid_write },
995939a6 1456 { .name = "TLBIMVAA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
7a0e58fa 1457 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimvaa_write },
995939a6
PM
1458 REGINFO_SENTINEL
1459};
1460
1461static const ARMCPRegInfo v7mp_cp_reginfo[] = {
1462 /* 32 bit TLB invalidates, Inner Shareable */
1463 { .name = "TLBIALLIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
7a0e58fa 1464 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_is_write },
995939a6 1465 { .name = "TLBIMVAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
7a0e58fa 1466 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_is_write },
995939a6 1467 { .name = "TLBIASIDIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
7a0e58fa 1468 .type = ARM_CP_NO_RAW, .access = PL1_W,
fa439fc5 1469 .writefn = tlbiasid_is_write },
995939a6 1470 { .name = "TLBIMVAAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
7a0e58fa 1471 .type = ARM_CP_NO_RAW, .access = PL1_W,
fa439fc5 1472 .writefn = tlbimvaa_is_write },
e9aa6c21
PM
1473 REGINFO_SENTINEL
1474};
1475
c4241c7d
PM
1476static void teecr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1477 uint64_t value)
c326b979
PM
1478{
1479 value &= 1;
1480 env->teecr = value;
c326b979
PM
1481}
1482
3f208fd7
PM
1483static CPAccessResult teehbr_access(CPUARMState *env, const ARMCPRegInfo *ri,
1484 bool isread)
c326b979 1485{
dcbff19b 1486 if (arm_current_el(env) == 0 && (env->teecr & 1)) {
92611c00 1487 return CP_ACCESS_TRAP;
c326b979 1488 }
92611c00 1489 return CP_ACCESS_OK;
c326b979
PM
1490}
1491
1492static const ARMCPRegInfo t2ee_cp_reginfo[] = {
1493 { .name = "TEECR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 6, .opc2 = 0,
1494 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, teecr),
1495 .resetvalue = 0,
1496 .writefn = teecr_write },
1497 { .name = "TEEHBR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 6, .opc2 = 0,
1498 .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, teehbr),
92611c00 1499 .accessfn = teehbr_access, .resetvalue = 0 },
c326b979
PM
1500 REGINFO_SENTINEL
1501};
1502
4d31c596 1503static const ARMCPRegInfo v6k_cp_reginfo[] = {
e4fe830b
PM
1504 { .name = "TPIDR_EL0", .state = ARM_CP_STATE_AA64,
1505 .opc0 = 3, .opc1 = 3, .opc2 = 2, .crn = 13, .crm = 0,
1506 .access = PL0_RW,
54bf36ed 1507 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[0]), .resetvalue = 0 },
4d31c596
PM
1508 { .name = "TPIDRURW", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 2,
1509 .access = PL0_RW,
54bf36ed
FA
1510 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrurw_s),
1511 offsetoflow32(CPUARMState, cp15.tpidrurw_ns) },
e4fe830b
PM
1512 .resetfn = arm_cp_reset_ignore },
1513 { .name = "TPIDRRO_EL0", .state = ARM_CP_STATE_AA64,
1514 .opc0 = 3, .opc1 = 3, .opc2 = 3, .crn = 13, .crm = 0,
1515 .access = PL0_R|PL1_W,
54bf36ed
FA
1516 .fieldoffset = offsetof(CPUARMState, cp15.tpidrro_el[0]),
1517 .resetvalue = 0},
4d31c596
PM
1518 { .name = "TPIDRURO", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 3,
1519 .access = PL0_R|PL1_W,
54bf36ed
FA
1520 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidruro_s),
1521 offsetoflow32(CPUARMState, cp15.tpidruro_ns) },
e4fe830b 1522 .resetfn = arm_cp_reset_ignore },
54bf36ed 1523 { .name = "TPIDR_EL1", .state = ARM_CP_STATE_AA64,
e4fe830b 1524 .opc0 = 3, .opc1 = 0, .opc2 = 4, .crn = 13, .crm = 0,
4d31c596 1525 .access = PL1_RW,
54bf36ed
FA
1526 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[1]), .resetvalue = 0 },
1527 { .name = "TPIDRPRW", .opc1 = 0, .cp = 15, .crn = 13, .crm = 0, .opc2 = 4,
1528 .access = PL1_RW,
1529 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrprw_s),
1530 offsetoflow32(CPUARMState, cp15.tpidrprw_ns) },
1531 .resetvalue = 0 },
4d31c596
PM
1532 REGINFO_SENTINEL
1533};
1534
55d284af
PM
1535#ifndef CONFIG_USER_ONLY
1536
3f208fd7
PM
1537static CPAccessResult gt_cntfrq_access(CPUARMState *env, const ARMCPRegInfo *ri,
1538 bool isread)
00108f2d 1539{
75502672
PM
1540 /* CNTFRQ: not visible from PL0 if both PL0PCTEN and PL0VCTEN are zero.
1541 * Writable only at the highest implemented exception level.
1542 */
1543 int el = arm_current_el(env);
1544
1545 switch (el) {
1546 case 0:
1547 if (!extract32(env->cp15.c14_cntkctl, 0, 2)) {
1548 return CP_ACCESS_TRAP;
1549 }
1550 break;
1551 case 1:
1552 if (!isread && ri->state == ARM_CP_STATE_AA32 &&
1553 arm_is_secure_below_el3(env)) {
1554 /* Accesses from 32-bit Secure EL1 UNDEF (*not* trap to EL3!) */
1555 return CP_ACCESS_TRAP_UNCATEGORIZED;
1556 }
1557 break;
1558 case 2:
1559 case 3:
1560 break;
00108f2d 1561 }
75502672
PM
1562
1563 if (!isread && el < arm_highest_el(env)) {
1564 return CP_ACCESS_TRAP_UNCATEGORIZED;
1565 }
1566
00108f2d
PM
1567 return CP_ACCESS_OK;
1568}
1569
3f208fd7
PM
1570static CPAccessResult gt_counter_access(CPUARMState *env, int timeridx,
1571 bool isread)
00108f2d 1572{
0b6440af
EI
1573 unsigned int cur_el = arm_current_el(env);
1574 bool secure = arm_is_secure(env);
1575
00108f2d 1576 /* CNT[PV]CT: not visible from PL0 if ELO[PV]CTEN is zero */
0b6440af 1577 if (cur_el == 0 &&
00108f2d
PM
1578 !extract32(env->cp15.c14_cntkctl, timeridx, 1)) {
1579 return CP_ACCESS_TRAP;
1580 }
0b6440af
EI
1581
1582 if (arm_feature(env, ARM_FEATURE_EL2) &&
1583 timeridx == GTIMER_PHYS && !secure && cur_el < 2 &&
1584 !extract32(env->cp15.cnthctl_el2, 0, 1)) {
1585 return CP_ACCESS_TRAP_EL2;
1586 }
00108f2d
PM
1587 return CP_ACCESS_OK;
1588}
1589
3f208fd7
PM
1590static CPAccessResult gt_timer_access(CPUARMState *env, int timeridx,
1591 bool isread)
00108f2d 1592{
0b6440af
EI
1593 unsigned int cur_el = arm_current_el(env);
1594 bool secure = arm_is_secure(env);
1595
00108f2d
PM
1596 /* CNT[PV]_CVAL, CNT[PV]_CTL, CNT[PV]_TVAL: not visible from PL0 if
1597 * EL0[PV]TEN is zero.
1598 */
0b6440af 1599 if (cur_el == 0 &&
00108f2d
PM
1600 !extract32(env->cp15.c14_cntkctl, 9 - timeridx, 1)) {
1601 return CP_ACCESS_TRAP;
1602 }
0b6440af
EI
1603
1604 if (arm_feature(env, ARM_FEATURE_EL2) &&
1605 timeridx == GTIMER_PHYS && !secure && cur_el < 2 &&
1606 !extract32(env->cp15.cnthctl_el2, 1, 1)) {
1607 return CP_ACCESS_TRAP_EL2;
1608 }
00108f2d
PM
1609 return CP_ACCESS_OK;
1610}
1611
1612static CPAccessResult gt_pct_access(CPUARMState *env,
3f208fd7
PM
1613 const ARMCPRegInfo *ri,
1614 bool isread)
00108f2d 1615{
3f208fd7 1616 return gt_counter_access(env, GTIMER_PHYS, isread);
00108f2d
PM
1617}
1618
1619static CPAccessResult gt_vct_access(CPUARMState *env,
3f208fd7
PM
1620 const ARMCPRegInfo *ri,
1621 bool isread)
00108f2d 1622{
3f208fd7 1623 return gt_counter_access(env, GTIMER_VIRT, isread);
00108f2d
PM
1624}
1625
3f208fd7
PM
1626static CPAccessResult gt_ptimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
1627 bool isread)
00108f2d 1628{
3f208fd7 1629 return gt_timer_access(env, GTIMER_PHYS, isread);
00108f2d
PM
1630}
1631
3f208fd7
PM
1632static CPAccessResult gt_vtimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
1633 bool isread)
00108f2d 1634{
3f208fd7 1635 return gt_timer_access(env, GTIMER_VIRT, isread);
00108f2d
PM
1636}
1637
b4d3978c 1638static CPAccessResult gt_stimer_access(CPUARMState *env,
3f208fd7
PM
1639 const ARMCPRegInfo *ri,
1640 bool isread)
b4d3978c
PM
1641{
1642 /* The AArch64 register view of the secure physical timer is
1643 * always accessible from EL3, and configurably accessible from
1644 * Secure EL1.
1645 */
1646 switch (arm_current_el(env)) {
1647 case 1:
1648 if (!arm_is_secure(env)) {
1649 return CP_ACCESS_TRAP;
1650 }
1651 if (!(env->cp15.scr_el3 & SCR_ST)) {
1652 return CP_ACCESS_TRAP_EL3;
1653 }
1654 return CP_ACCESS_OK;
1655 case 0:
1656 case 2:
1657 return CP_ACCESS_TRAP;
1658 case 3:
1659 return CP_ACCESS_OK;
1660 default:
1661 g_assert_not_reached();
1662 }
1663}
1664
55d284af
PM
1665static uint64_t gt_get_countervalue(CPUARMState *env)
1666{
bc72ad67 1667 return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) / GTIMER_SCALE;
55d284af
PM
1668}
1669
1670static void gt_recalc_timer(ARMCPU *cpu, int timeridx)
1671{
1672 ARMGenericTimer *gt = &cpu->env.cp15.c14_timer[timeridx];
1673
1674 if (gt->ctl & 1) {
1675 /* Timer enabled: calculate and set current ISTATUS, irq, and
1676 * reset timer to when ISTATUS next has to change
1677 */
edac4d8a
EI
1678 uint64_t offset = timeridx == GTIMER_VIRT ?
1679 cpu->env.cp15.cntvoff_el2 : 0;
55d284af
PM
1680 uint64_t count = gt_get_countervalue(&cpu->env);
1681 /* Note that this must be unsigned 64 bit arithmetic: */
edac4d8a 1682 int istatus = count - offset >= gt->cval;
55d284af 1683 uint64_t nexttick;
194cbc49 1684 int irqstate;
55d284af
PM
1685
1686 gt->ctl = deposit32(gt->ctl, 2, 1, istatus);
194cbc49
PM
1687
1688 irqstate = (istatus && !(gt->ctl & 2));
1689 qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate);
1690
55d284af
PM
1691 if (istatus) {
1692 /* Next transition is when count rolls back over to zero */
1693 nexttick = UINT64_MAX;
1694 } else {
1695 /* Next transition is when we hit cval */
edac4d8a 1696 nexttick = gt->cval + offset;
55d284af
PM
1697 }
1698 /* Note that the desired next expiry time might be beyond the
1699 * signed-64-bit range of a QEMUTimer -- in this case we just
1700 * set the timer for as far in the future as possible. When the
1701 * timer expires we will reset the timer for any remaining period.
1702 */
1703 if (nexttick > INT64_MAX / GTIMER_SCALE) {
1704 nexttick = INT64_MAX / GTIMER_SCALE;
1705 }
bc72ad67 1706 timer_mod(cpu->gt_timer[timeridx], nexttick);
194cbc49 1707 trace_arm_gt_recalc(timeridx, irqstate, nexttick);
55d284af
PM
1708 } else {
1709 /* Timer disabled: ISTATUS and timer output always clear */
1710 gt->ctl &= ~4;
1711 qemu_set_irq(cpu->gt_timer_outputs[timeridx], 0);
bc72ad67 1712 timer_del(cpu->gt_timer[timeridx]);
194cbc49 1713 trace_arm_gt_recalc_disabled(timeridx);
55d284af
PM
1714 }
1715}
1716
0e3eca4c
EI
1717static void gt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri,
1718 int timeridx)
55d284af
PM
1719{
1720 ARMCPU *cpu = arm_env_get_cpu(env);
55d284af 1721
bc72ad67 1722 timer_del(cpu->gt_timer[timeridx]);
55d284af
PM
1723}
1724
c4241c7d 1725static uint64_t gt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
55d284af 1726{
c4241c7d 1727 return gt_get_countervalue(env);
55d284af
PM
1728}
1729
edac4d8a
EI
1730static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
1731{
1732 return gt_get_countervalue(env) - env->cp15.cntvoff_el2;
1733}
1734
c4241c7d 1735static void gt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
0e3eca4c 1736 int timeridx,
c4241c7d 1737 uint64_t value)
55d284af 1738{
194cbc49 1739 trace_arm_gt_cval_write(timeridx, value);
55d284af
PM
1740 env->cp15.c14_timer[timeridx].cval = value;
1741 gt_recalc_timer(arm_env_get_cpu(env), timeridx);
55d284af 1742}
c4241c7d 1743
0e3eca4c
EI
1744static uint64_t gt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri,
1745 int timeridx)
55d284af 1746{
edac4d8a 1747 uint64_t offset = timeridx == GTIMER_VIRT ? env->cp15.cntvoff_el2 : 0;
55d284af 1748
c4241c7d 1749 return (uint32_t)(env->cp15.c14_timer[timeridx].cval -
edac4d8a 1750 (gt_get_countervalue(env) - offset));
55d284af
PM
1751}
1752
c4241c7d 1753static void gt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
0e3eca4c 1754 int timeridx,
c4241c7d 1755 uint64_t value)
55d284af 1756{
edac4d8a 1757 uint64_t offset = timeridx == GTIMER_VIRT ? env->cp15.cntvoff_el2 : 0;
55d284af 1758
194cbc49 1759 trace_arm_gt_tval_write(timeridx, value);
edac4d8a 1760 env->cp15.c14_timer[timeridx].cval = gt_get_countervalue(env) - offset +
18084b2f 1761 sextract64(value, 0, 32);
55d284af 1762 gt_recalc_timer(arm_env_get_cpu(env), timeridx);
55d284af
PM
1763}
1764
c4241c7d 1765static void gt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
0e3eca4c 1766 int timeridx,
c4241c7d 1767 uint64_t value)
55d284af
PM
1768{
1769 ARMCPU *cpu = arm_env_get_cpu(env);
55d284af
PM
1770 uint32_t oldval = env->cp15.c14_timer[timeridx].ctl;
1771
194cbc49 1772 trace_arm_gt_ctl_write(timeridx, value);
d3afacc7 1773 env->cp15.c14_timer[timeridx].ctl = deposit64(oldval, 0, 2, value);
55d284af
PM
1774 if ((oldval ^ value) & 1) {
1775 /* Enable toggled */
1776 gt_recalc_timer(cpu, timeridx);
d3afacc7 1777 } else if ((oldval ^ value) & 2) {
55d284af
PM
1778 /* IMASK toggled: don't need to recalculate,
1779 * just set the interrupt line based on ISTATUS
1780 */
194cbc49
PM
1781 int irqstate = (oldval & 4) && !(value & 2);
1782
1783 trace_arm_gt_imask_toggle(timeridx, irqstate);
1784 qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate);
55d284af 1785 }
55d284af
PM
1786}
1787
0e3eca4c
EI
1788static void gt_phys_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1789{
1790 gt_timer_reset(env, ri, GTIMER_PHYS);
1791}
1792
1793static void gt_phys_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1794 uint64_t value)
1795{
1796 gt_cval_write(env, ri, GTIMER_PHYS, value);
1797}
1798
1799static uint64_t gt_phys_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
1800{
1801 return gt_tval_read(env, ri, GTIMER_PHYS);
1802}
1803
1804static void gt_phys_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1805 uint64_t value)
1806{
1807 gt_tval_write(env, ri, GTIMER_PHYS, value);
1808}
1809
1810static void gt_phys_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
1811 uint64_t value)
1812{
1813 gt_ctl_write(env, ri, GTIMER_PHYS, value);
1814}
1815
1816static void gt_virt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1817{
1818 gt_timer_reset(env, ri, GTIMER_VIRT);
1819}
1820
1821static void gt_virt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1822 uint64_t value)
1823{
1824 gt_cval_write(env, ri, GTIMER_VIRT, value);
1825}
1826
1827static uint64_t gt_virt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
1828{
1829 return gt_tval_read(env, ri, GTIMER_VIRT);
1830}
1831
1832static void gt_virt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1833 uint64_t value)
1834{
1835 gt_tval_write(env, ri, GTIMER_VIRT, value);
1836}
1837
1838static void gt_virt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
1839 uint64_t value)
1840{
1841 gt_ctl_write(env, ri, GTIMER_VIRT, value);
1842}
1843
edac4d8a
EI
1844static void gt_cntvoff_write(CPUARMState *env, const ARMCPRegInfo *ri,
1845 uint64_t value)
1846{
1847 ARMCPU *cpu = arm_env_get_cpu(env);
1848
194cbc49 1849 trace_arm_gt_cntvoff_write(value);
edac4d8a
EI
1850 raw_write(env, ri, value);
1851 gt_recalc_timer(cpu, GTIMER_VIRT);
1852}
1853
b0e66d95
EI
1854static void gt_hyp_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1855{
1856 gt_timer_reset(env, ri, GTIMER_HYP);
1857}
1858
1859static void gt_hyp_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1860 uint64_t value)
1861{
1862 gt_cval_write(env, ri, GTIMER_HYP, value);
1863}
1864
1865static uint64_t gt_hyp_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
1866{
1867 return gt_tval_read(env, ri, GTIMER_HYP);
1868}
1869
1870static void gt_hyp_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1871 uint64_t value)
1872{
1873 gt_tval_write(env, ri, GTIMER_HYP, value);
1874}
1875
1876static void gt_hyp_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
1877 uint64_t value)
1878{
1879 gt_ctl_write(env, ri, GTIMER_HYP, value);
1880}
1881
b4d3978c
PM
1882static void gt_sec_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1883{
1884 gt_timer_reset(env, ri, GTIMER_SEC);
1885}
1886
1887static void gt_sec_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1888 uint64_t value)
1889{
1890 gt_cval_write(env, ri, GTIMER_SEC, value);
1891}
1892
1893static uint64_t gt_sec_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
1894{
1895 return gt_tval_read(env, ri, GTIMER_SEC);
1896}
1897
1898static void gt_sec_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1899 uint64_t value)
1900{
1901 gt_tval_write(env, ri, GTIMER_SEC, value);
1902}
1903
1904static void gt_sec_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
1905 uint64_t value)
1906{
1907 gt_ctl_write(env, ri, GTIMER_SEC, value);
1908}
1909
55d284af
PM
1910void arm_gt_ptimer_cb(void *opaque)
1911{
1912 ARMCPU *cpu = opaque;
1913
1914 gt_recalc_timer(cpu, GTIMER_PHYS);
1915}
1916
1917void arm_gt_vtimer_cb(void *opaque)
1918{
1919 ARMCPU *cpu = opaque;
1920
1921 gt_recalc_timer(cpu, GTIMER_VIRT);
1922}
1923
b0e66d95
EI
1924void arm_gt_htimer_cb(void *opaque)
1925{
1926 ARMCPU *cpu = opaque;
1927
1928 gt_recalc_timer(cpu, GTIMER_HYP);
1929}
1930
b4d3978c
PM
1931void arm_gt_stimer_cb(void *opaque)
1932{
1933 ARMCPU *cpu = opaque;
1934
1935 gt_recalc_timer(cpu, GTIMER_SEC);
1936}
1937
55d284af
PM
1938static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
1939 /* Note that CNTFRQ is purely reads-as-written for the benefit
1940 * of software; writing it doesn't actually change the timer frequency.
1941 * Our reset value matches the fixed frequency we implement the timer at.
1942 */
1943 { .name = "CNTFRQ", .cp = 15, .crn = 14, .crm = 0, .opc1 = 0, .opc2 = 0,
7a0e58fa 1944 .type = ARM_CP_ALIAS,
a7adc4b7
PM
1945 .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
1946 .fieldoffset = offsetoflow32(CPUARMState, cp15.c14_cntfrq),
a7adc4b7
PM
1947 },
1948 { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
1949 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
1950 .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
55d284af
PM
1951 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
1952 .resetvalue = (1000 * 1000 * 1000) / GTIMER_SCALE,
55d284af
PM
1953 },
1954 /* overall control: mostly access permissions */
a7adc4b7
PM
1955 { .name = "CNTKCTL", .state = ARM_CP_STATE_BOTH,
1956 .opc0 = 3, .opc1 = 0, .crn = 14, .crm = 1, .opc2 = 0,
55d284af
PM
1957 .access = PL1_RW,
1958 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntkctl),
1959 .resetvalue = 0,
1960 },
1961 /* per-timer control */
1962 { .name = "CNTP_CTL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
9ff9dd3c 1963 .secure = ARM_CP_SECSTATE_NS,
7a0e58fa 1964 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL1_RW | PL0_R,
a7adc4b7
PM
1965 .accessfn = gt_ptimer_access,
1966 .fieldoffset = offsetoflow32(CPUARMState,
1967 cp15.c14_timer[GTIMER_PHYS].ctl),
0e3eca4c 1968 .writefn = gt_phys_ctl_write, .raw_writefn = raw_write,
a7adc4b7 1969 },
9ff9dd3c
PM
1970 { .name = "CNTP_CTL(S)",
1971 .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
1972 .secure = ARM_CP_SECSTATE_S,
1973 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL1_RW | PL0_R,
1974 .accessfn = gt_ptimer_access,
1975 .fieldoffset = offsetoflow32(CPUARMState,
1976 cp15.c14_timer[GTIMER_SEC].ctl),
1977 .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
1978 },
a7adc4b7
PM
1979 { .name = "CNTP_CTL_EL0", .state = ARM_CP_STATE_AA64,
1980 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 1,
55d284af 1981 .type = ARM_CP_IO, .access = PL1_RW | PL0_R,
a7adc4b7 1982 .accessfn = gt_ptimer_access,
55d284af
PM
1983 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
1984 .resetvalue = 0,
0e3eca4c 1985 .writefn = gt_phys_ctl_write, .raw_writefn = raw_write,
55d284af
PM
1986 },
1987 { .name = "CNTV_CTL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 1,
7a0e58fa 1988 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL1_RW | PL0_R,
a7adc4b7
PM
1989 .accessfn = gt_vtimer_access,
1990 .fieldoffset = offsetoflow32(CPUARMState,
1991 cp15.c14_timer[GTIMER_VIRT].ctl),
0e3eca4c 1992 .writefn = gt_virt_ctl_write, .raw_writefn = raw_write,
a7adc4b7
PM
1993 },
1994 { .name = "CNTV_CTL_EL0", .state = ARM_CP_STATE_AA64,
1995 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 1,
55d284af 1996 .type = ARM_CP_IO, .access = PL1_RW | PL0_R,
a7adc4b7 1997 .accessfn = gt_vtimer_access,
55d284af
PM
1998 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
1999 .resetvalue = 0,
0e3eca4c 2000 .writefn = gt_virt_ctl_write, .raw_writefn = raw_write,
55d284af
PM
2001 },
2002 /* TimerValue views: a 32 bit downcounting view of the underlying state */
2003 { .name = "CNTP_TVAL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
9ff9dd3c 2004 .secure = ARM_CP_SECSTATE_NS,
7a0e58fa 2005 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW | PL0_R,
00108f2d 2006 .accessfn = gt_ptimer_access,
0e3eca4c 2007 .readfn = gt_phys_tval_read, .writefn = gt_phys_tval_write,
55d284af 2008 },
9ff9dd3c
PM
2009 { .name = "CNTP_TVAL(S)",
2010 .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
2011 .secure = ARM_CP_SECSTATE_S,
2012 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW | PL0_R,
2013 .accessfn = gt_ptimer_access,
2014 .readfn = gt_sec_tval_read, .writefn = gt_sec_tval_write,
2015 },
a7adc4b7
PM
2016 { .name = "CNTP_TVAL_EL0", .state = ARM_CP_STATE_AA64,
2017 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 0,
7a0e58fa 2018 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW | PL0_R,
0e3eca4c
EI
2019 .accessfn = gt_ptimer_access, .resetfn = gt_phys_timer_reset,
2020 .readfn = gt_phys_tval_read, .writefn = gt_phys_tval_write,
a7adc4b7 2021 },
55d284af 2022 { .name = "CNTV_TVAL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 0,
7a0e58fa 2023 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW | PL0_R,
00108f2d 2024 .accessfn = gt_vtimer_access,
0e3eca4c 2025 .readfn = gt_virt_tval_read, .writefn = gt_virt_tval_write,
55d284af 2026 },
a7adc4b7
PM
2027 { .name = "CNTV_TVAL_EL0", .state = ARM_CP_STATE_AA64,
2028 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 0,
7a0e58fa 2029 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW | PL0_R,
0e3eca4c
EI
2030 .accessfn = gt_vtimer_access, .resetfn = gt_virt_timer_reset,
2031 .readfn = gt_virt_tval_read, .writefn = gt_virt_tval_write,
a7adc4b7 2032 },
55d284af
PM
2033 /* The counter itself */
2034 { .name = "CNTPCT", .cp = 15, .crm = 14, .opc1 = 0,
7a0e58fa 2035 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
00108f2d 2036 .accessfn = gt_pct_access,
a7adc4b7
PM
2037 .readfn = gt_cnt_read, .resetfn = arm_cp_reset_ignore,
2038 },
2039 { .name = "CNTPCT_EL0", .state = ARM_CP_STATE_AA64,
2040 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 1,
7a0e58fa 2041 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
d57b9ee8 2042 .accessfn = gt_pct_access, .readfn = gt_cnt_read,
55d284af
PM
2043 },
2044 { .name = "CNTVCT", .cp = 15, .crm = 14, .opc1 = 1,
7a0e58fa 2045 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
00108f2d 2046 .accessfn = gt_vct_access,
edac4d8a 2047 .readfn = gt_virt_cnt_read, .resetfn = arm_cp_reset_ignore,
a7adc4b7
PM
2048 },
2049 { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
2050 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
7a0e58fa 2051 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
d57b9ee8 2052 .accessfn = gt_vct_access, .readfn = gt_virt_cnt_read,
55d284af
PM
2053 },
2054 /* Comparison value, indicating when the timer goes off */
2055 { .name = "CNTP_CVAL", .cp = 15, .crm = 14, .opc1 = 2,
9ff9dd3c 2056 .secure = ARM_CP_SECSTATE_NS,
55d284af 2057 .access = PL1_RW | PL0_R,
7a0e58fa 2058 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
55d284af 2059 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
b061a82b 2060 .accessfn = gt_ptimer_access,
0e3eca4c 2061 .writefn = gt_phys_cval_write, .raw_writefn = raw_write,
a7adc4b7 2062 },
9ff9dd3c
PM
2063 { .name = "CNTP_CVAL(S)", .cp = 15, .crm = 14, .opc1 = 2,
2064 .secure = ARM_CP_SECSTATE_S,
2065 .access = PL1_RW | PL0_R,
2066 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
2067 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
2068 .accessfn = gt_ptimer_access,
2069 .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
2070 },
a7adc4b7
PM
2071 { .name = "CNTP_CVAL_EL0", .state = ARM_CP_STATE_AA64,
2072 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 2,
2073 .access = PL1_RW | PL0_R,
2074 .type = ARM_CP_IO,
2075 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
12cde08a 2076 .resetvalue = 0, .accessfn = gt_ptimer_access,
0e3eca4c 2077 .writefn = gt_phys_cval_write, .raw_writefn = raw_write,
55d284af
PM
2078 },
2079 { .name = "CNTV_CVAL", .cp = 15, .crm = 14, .opc1 = 3,
2080 .access = PL1_RW | PL0_R,
7a0e58fa 2081 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
55d284af 2082 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
b061a82b 2083 .accessfn = gt_vtimer_access,
0e3eca4c 2084 .writefn = gt_virt_cval_write, .raw_writefn = raw_write,
a7adc4b7
PM
2085 },
2086 { .name = "CNTV_CVAL_EL0", .state = ARM_CP_STATE_AA64,
2087 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 2,
2088 .access = PL1_RW | PL0_R,
2089 .type = ARM_CP_IO,
2090 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
2091 .resetvalue = 0, .accessfn = gt_vtimer_access,
0e3eca4c 2092 .writefn = gt_virt_cval_write, .raw_writefn = raw_write,
55d284af 2093 },
b4d3978c
PM
2094 /* Secure timer -- this is actually restricted to only EL3
2095 * and configurably Secure-EL1 via the accessfn.
2096 */
2097 { .name = "CNTPS_TVAL_EL1", .state = ARM_CP_STATE_AA64,
2098 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 0,
2099 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW,
2100 .accessfn = gt_stimer_access,
2101 .readfn = gt_sec_tval_read,
2102 .writefn = gt_sec_tval_write,
2103 .resetfn = gt_sec_timer_reset,
2104 },
2105 { .name = "CNTPS_CTL_EL1", .state = ARM_CP_STATE_AA64,
2106 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 1,
2107 .type = ARM_CP_IO, .access = PL1_RW,
2108 .accessfn = gt_stimer_access,
2109 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].ctl),
2110 .resetvalue = 0,
2111 .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
2112 },
2113 { .name = "CNTPS_CVAL_EL1", .state = ARM_CP_STATE_AA64,
2114 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 2,
2115 .type = ARM_CP_IO, .access = PL1_RW,
2116 .accessfn = gt_stimer_access,
2117 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
2118 .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
2119 },
55d284af
PM
2120 REGINFO_SENTINEL
2121};
2122
2123#else
2124/* In user-mode none of the generic timer registers are accessible,
bc72ad67 2125 * and their implementation depends on QEMU_CLOCK_VIRTUAL and qdev gpio outputs,
55d284af
PM
2126 * so instead just don't register any of them.
2127 */
6cc7a3ae 2128static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
6cc7a3ae
PM
2129 REGINFO_SENTINEL
2130};
2131
55d284af
PM
2132#endif
2133
c4241c7d 2134static void par_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
4a501606 2135{
891a2fe7 2136 if (arm_feature(env, ARM_FEATURE_LPAE)) {
8d5c773e 2137 raw_write(env, ri, value);
891a2fe7 2138 } else if (arm_feature(env, ARM_FEATURE_V7)) {
8d5c773e 2139 raw_write(env, ri, value & 0xfffff6ff);
4a501606 2140 } else {
8d5c773e 2141 raw_write(env, ri, value & 0xfffff1ff);
4a501606 2142 }
4a501606
PM
2143}
2144
2145#ifndef CONFIG_USER_ONLY
2146/* get_phys_addr() isn't present for user-mode-only targets */
702a9357 2147
3f208fd7
PM
2148static CPAccessResult ats_access(CPUARMState *env, const ARMCPRegInfo *ri,
2149 bool isread)
92611c00
PM
2150{
2151 if (ri->opc2 & 4) {
87562e4f
PM
2152 /* The ATS12NSO* operations must trap to EL3 if executed in
2153 * Secure EL1 (which can only happen if EL3 is AArch64).
2154 * They are simply UNDEF if executed from NS EL1.
2155 * They function normally from EL2 or EL3.
92611c00 2156 */
87562e4f
PM
2157 if (arm_current_el(env) == 1) {
2158 if (arm_is_secure_below_el3(env)) {
2159 return CP_ACCESS_TRAP_UNCATEGORIZED_EL3;
2160 }
2161 return CP_ACCESS_TRAP_UNCATEGORIZED;
2162 }
92611c00
PM
2163 }
2164 return CP_ACCESS_OK;
2165}
2166
060e8a48 2167static uint64_t do_ats_write(CPUARMState *env, uint64_t value,
03ae85f8 2168 MMUAccessType access_type, ARMMMUIdx mmu_idx)
4a501606 2169{
a8170e5e 2170 hwaddr phys_addr;
4a501606
PM
2171 target_ulong page_size;
2172 int prot;
b7cc4e82 2173 bool ret;
01c097f7 2174 uint64_t par64;
1313e2d7 2175 bool format64 = false;
8bf5b6a9 2176 MemTxAttrs attrs = {};
e14b5a23 2177 ARMMMUFaultInfo fi = {};
5b2d261d 2178 ARMCacheAttrs cacheattrs = {};
4a501606 2179
5b2d261d 2180 ret = get_phys_addr(env, value, access_type, mmu_idx, &phys_addr, &attrs,
bc52bfeb 2181 &prot, &page_size, &fi, &cacheattrs);
1313e2d7
EI
2182
2183 if (is_a64(env)) {
2184 format64 = true;
2185 } else if (arm_feature(env, ARM_FEATURE_LPAE)) {
2186 /*
2187 * ATS1Cxx:
2188 * * TTBCR.EAE determines whether the result is returned using the
2189 * 32-bit or the 64-bit PAR format
2190 * * Instructions executed in Hyp mode always use the 64bit format
2191 *
2192 * ATS1S2NSOxx uses the 64bit format if any of the following is true:
2193 * * The Non-secure TTBCR.EAE bit is set to 1
2194 * * The implementation includes EL2, and the value of HCR.VM is 1
2195 *
2196 * ATS1Hx always uses the 64bit format (not supported yet).
2197 */
2198 format64 = arm_s1_regime_using_lpae_format(env, mmu_idx);
2199
2200 if (arm_feature(env, ARM_FEATURE_EL2)) {
2201 if (mmu_idx == ARMMMUIdx_S12NSE0 || mmu_idx == ARMMMUIdx_S12NSE1) {
2202 format64 |= env->cp15.hcr_el2 & HCR_VM;
2203 } else {
2204 format64 |= arm_current_el(env) == 2;
2205 }
2206 }
2207 }
2208
2209 if (format64) {
5efe9ed4 2210 /* Create a 64-bit PAR */
01c097f7 2211 par64 = (1 << 11); /* LPAE bit always set */
b7cc4e82 2212 if (!ret) {
702a9357 2213 par64 |= phys_addr & ~0xfffULL;
8bf5b6a9
PM
2214 if (!attrs.secure) {
2215 par64 |= (1 << 9); /* NS */
2216 }
5b2d261d
AB
2217 par64 |= (uint64_t)cacheattrs.attrs << 56; /* ATTR */
2218 par64 |= cacheattrs.shareability << 7; /* SH */
4a501606 2219 } else {
5efe9ed4
PM
2220 uint32_t fsr = arm_fi_to_lfsc(&fi);
2221
702a9357 2222 par64 |= 1; /* F */
b7cc4e82 2223 par64 |= (fsr & 0x3f) << 1; /* FS */
702a9357
PM
2224 /* Note that S2WLK and FSTAGE are always zero, because we don't
2225 * implement virtualization and therefore there can't be a stage 2
2226 * fault.
2227 */
4a501606
PM
2228 }
2229 } else {
b7cc4e82 2230 /* fsr is a DFSR/IFSR value for the short descriptor
702a9357
PM
2231 * translation table format (with WnR always clear).
2232 * Convert it to a 32-bit PAR.
2233 */
b7cc4e82 2234 if (!ret) {
702a9357
PM
2235 /* We do not set any attribute bits in the PAR */
2236 if (page_size == (1 << 24)
2237 && arm_feature(env, ARM_FEATURE_V7)) {
01c097f7 2238 par64 = (phys_addr & 0xff000000) | (1 << 1);
702a9357 2239 } else {
01c097f7 2240 par64 = phys_addr & 0xfffff000;
702a9357 2241 }
8bf5b6a9
PM
2242 if (!attrs.secure) {
2243 par64 |= (1 << 9); /* NS */
2244 }
702a9357 2245 } else {
5efe9ed4
PM
2246 uint32_t fsr = arm_fi_to_sfsc(&fi);
2247
b7cc4e82
PC
2248 par64 = ((fsr & (1 << 10)) >> 5) | ((fsr & (1 << 12)) >> 6) |
2249 ((fsr & 0xf) << 1) | 1;
702a9357 2250 }
4a501606 2251 }
060e8a48
PM
2252 return par64;
2253}
2254
2255static void ats_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
2256{
03ae85f8 2257 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
060e8a48 2258 uint64_t par64;
d3649702
PM
2259 ARMMMUIdx mmu_idx;
2260 int el = arm_current_el(env);
2261 bool secure = arm_is_secure_below_el3(env);
060e8a48 2262
d3649702
PM
2263 switch (ri->opc2 & 6) {
2264 case 0:
2265 /* stage 1 current state PL1: ATS1CPR, ATS1CPW */
2266 switch (el) {
2267 case 3:
2268 mmu_idx = ARMMMUIdx_S1E3;
2269 break;
2270 case 2:
2271 mmu_idx = ARMMMUIdx_S1NSE1;
2272 break;
2273 case 1:
2274 mmu_idx = secure ? ARMMMUIdx_S1SE1 : ARMMMUIdx_S1NSE1;
2275 break;
2276 default:
2277 g_assert_not_reached();
2278 }
2279 break;
2280 case 2:
2281 /* stage 1 current state PL0: ATS1CUR, ATS1CUW */
2282 switch (el) {
2283 case 3:
2284 mmu_idx = ARMMMUIdx_S1SE0;
2285 break;
2286 case 2:
2287 mmu_idx = ARMMMUIdx_S1NSE0;
2288 break;
2289 case 1:
2290 mmu_idx = secure ? ARMMMUIdx_S1SE0 : ARMMMUIdx_S1NSE0;
2291 break;
2292 default:
2293 g_assert_not_reached();
2294 }
2295 break;
2296 case 4:
2297 /* stage 1+2 NonSecure PL1: ATS12NSOPR, ATS12NSOPW */
2298 mmu_idx = ARMMMUIdx_S12NSE1;
2299 break;
2300 case 6:
2301 /* stage 1+2 NonSecure PL0: ATS12NSOUR, ATS12NSOUW */
2302 mmu_idx = ARMMMUIdx_S12NSE0;
2303 break;
2304 default:
2305 g_assert_not_reached();
2306 }
2307
2308 par64 = do_ats_write(env, value, access_type, mmu_idx);
01c097f7
FA
2309
2310 A32_BANKED_CURRENT_REG_SET(env, par, par64);
4a501606 2311}
060e8a48 2312
14db7fe0
PM
2313static void ats1h_write(CPUARMState *env, const ARMCPRegInfo *ri,
2314 uint64_t value)
2315{
03ae85f8 2316 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
14db7fe0
PM
2317 uint64_t par64;
2318
2319 par64 = do_ats_write(env, value, access_type, ARMMMUIdx_S2NS);
2320
2321 A32_BANKED_CURRENT_REG_SET(env, par, par64);
2322}
2323
3f208fd7
PM
2324static CPAccessResult at_s1e2_access(CPUARMState *env, const ARMCPRegInfo *ri,
2325 bool isread)
2a47df95
PM
2326{
2327 if (arm_current_el(env) == 3 && !(env->cp15.scr_el3 & SCR_NS)) {
2328 return CP_ACCESS_TRAP;
2329 }
2330 return CP_ACCESS_OK;
2331}
2332
060e8a48
PM
2333static void ats_write64(CPUARMState *env, const ARMCPRegInfo *ri,
2334 uint64_t value)
2335{
03ae85f8 2336 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
d3649702
PM
2337 ARMMMUIdx mmu_idx;
2338 int secure = arm_is_secure_below_el3(env);
2339
2340 switch (ri->opc2 & 6) {
2341 case 0:
2342 switch (ri->opc1) {
2343 case 0: /* AT S1E1R, AT S1E1W */
2344 mmu_idx = secure ? ARMMMUIdx_S1SE1 : ARMMMUIdx_S1NSE1;
2345 break;
2346 case 4: /* AT S1E2R, AT S1E2W */
2347 mmu_idx = ARMMMUIdx_S1E2;
2348 break;
2349 case 6: /* AT S1E3R, AT S1E3W */
2350 mmu_idx = ARMMMUIdx_S1E3;
2351 break;
2352 default:
2353 g_assert_not_reached();
2354 }
2355 break;
2356 case 2: /* AT S1E0R, AT S1E0W */
2357 mmu_idx = secure ? ARMMMUIdx_S1SE0 : ARMMMUIdx_S1NSE0;
2358 break;
2359 case 4: /* AT S12E1R, AT S12E1W */
2a47df95 2360 mmu_idx = secure ? ARMMMUIdx_S1SE1 : ARMMMUIdx_S12NSE1;
d3649702
PM
2361 break;
2362 case 6: /* AT S12E0R, AT S12E0W */
2a47df95 2363 mmu_idx = secure ? ARMMMUIdx_S1SE0 : ARMMMUIdx_S12NSE0;
d3649702
PM
2364 break;
2365 default:
2366 g_assert_not_reached();
2367 }
060e8a48 2368
d3649702 2369 env->cp15.par_el[1] = do_ats_write(env, value, access_type, mmu_idx);
060e8a48 2370}
4a501606
PM
2371#endif
2372
2373static const ARMCPRegInfo vapa_cp_reginfo[] = {
2374 { .name = "PAR", .cp = 15, .crn = 7, .crm = 4, .opc1 = 0, .opc2 = 0,
2375 .access = PL1_RW, .resetvalue = 0,
01c097f7
FA
2376 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.par_s),
2377 offsetoflow32(CPUARMState, cp15.par_ns) },
4a501606
PM
2378 .writefn = par_write },
2379#ifndef CONFIG_USER_ONLY
87562e4f 2380 /* This underdecoding is safe because the reginfo is NO_RAW. */
4a501606 2381 { .name = "ATS", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = CP_ANY,
92611c00 2382 .access = PL1_W, .accessfn = ats_access,
7a0e58fa 2383 .writefn = ats_write, .type = ARM_CP_NO_RAW },
4a501606
PM
2384#endif
2385 REGINFO_SENTINEL
2386};
2387
18032bec
PM
2388/* Return basic MPU access permission bits. */
2389static uint32_t simple_mpu_ap_bits(uint32_t val)
2390{
2391 uint32_t ret;
2392 uint32_t mask;
2393 int i;
2394 ret = 0;
2395 mask = 3;
2396 for (i = 0; i < 16; i += 2) {
2397 ret |= (val >> i) & mask;
2398 mask <<= 2;
2399 }
2400 return ret;
2401}
2402
2403/* Pad basic MPU access permission bits to extended format. */
2404static uint32_t extended_mpu_ap_bits(uint32_t val)
2405{
2406 uint32_t ret;
2407 uint32_t mask;
2408 int i;
2409 ret = 0;
2410 mask = 3;
2411 for (i = 0; i < 16; i += 2) {
2412 ret |= (val & mask) << i;
2413 mask <<= 2;
2414 }
2415 return ret;
2416}
2417
c4241c7d
PM
2418static void pmsav5_data_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
2419 uint64_t value)
18032bec 2420{
7e09797c 2421 env->cp15.pmsav5_data_ap = extended_mpu_ap_bits(value);
18032bec
PM
2422}
2423
c4241c7d 2424static uint64_t pmsav5_data_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
18032bec 2425{
7e09797c 2426 return simple_mpu_ap_bits(env->cp15.pmsav5_data_ap);
18032bec
PM
2427}
2428
c4241c7d
PM
2429static void pmsav5_insn_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
2430 uint64_t value)
18032bec 2431{
7e09797c 2432 env->cp15.pmsav5_insn_ap = extended_mpu_ap_bits(value);
18032bec
PM
2433}
2434
c4241c7d 2435static uint64_t pmsav5_insn_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
18032bec 2436{
7e09797c 2437 return simple_mpu_ap_bits(env->cp15.pmsav5_insn_ap);
18032bec
PM
2438}
2439
6cb0b013
PC
2440static uint64_t pmsav7_read(CPUARMState *env, const ARMCPRegInfo *ri)
2441{
2442 uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
2443
2444 if (!u32p) {
2445 return 0;
2446 }
2447
1bc04a88 2448 u32p += env->pmsav7.rnr[M_REG_NS];
6cb0b013
PC
2449 return *u32p;
2450}
2451
2452static void pmsav7_write(CPUARMState *env, const ARMCPRegInfo *ri,
2453 uint64_t value)
2454{
2455 ARMCPU *cpu = arm_env_get_cpu(env);
2456 uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
2457
2458 if (!u32p) {
2459 return;
2460 }
2461
1bc04a88 2462 u32p += env->pmsav7.rnr[M_REG_NS];
d10eb08f 2463 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
6cb0b013
PC
2464 *u32p = value;
2465}
2466
6cb0b013
PC
2467static void pmsav7_rgnr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2468 uint64_t value)
2469{
2470 ARMCPU *cpu = arm_env_get_cpu(env);
2471 uint32_t nrgs = cpu->pmsav7_dregion;
2472
2473 if (value >= nrgs) {
2474 qemu_log_mask(LOG_GUEST_ERROR,
2475 "PMSAv7 RGNR write >= # supported regions, %" PRIu32
2476 " > %" PRIu32 "\n", (uint32_t)value, nrgs);
2477 return;
2478 }
2479
2480 raw_write(env, ri, value);
2481}
2482
2483static const ARMCPRegInfo pmsav7_cp_reginfo[] = {
69ceea64
PM
2484 /* Reset for all these registers is handled in arm_cpu_reset(),
2485 * because the PMSAv7 is also used by M-profile CPUs, which do
2486 * not register cpregs but still need the state to be reset.
2487 */
6cb0b013
PC
2488 { .name = "DRBAR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 0,
2489 .access = PL1_RW, .type = ARM_CP_NO_RAW,
2490 .fieldoffset = offsetof(CPUARMState, pmsav7.drbar),
69ceea64
PM
2491 .readfn = pmsav7_read, .writefn = pmsav7_write,
2492 .resetfn = arm_cp_reset_ignore },
6cb0b013
PC
2493 { .name = "DRSR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 2,
2494 .access = PL1_RW, .type = ARM_CP_NO_RAW,
2495 .fieldoffset = offsetof(CPUARMState, pmsav7.drsr),
69ceea64
PM
2496 .readfn = pmsav7_read, .writefn = pmsav7_write,
2497 .resetfn = arm_cp_reset_ignore },
6cb0b013
PC
2498 { .name = "DRACR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 4,
2499 .access = PL1_RW, .type = ARM_CP_NO_RAW,
2500 .fieldoffset = offsetof(CPUARMState, pmsav7.dracr),
69ceea64
PM
2501 .readfn = pmsav7_read, .writefn = pmsav7_write,
2502 .resetfn = arm_cp_reset_ignore },
6cb0b013
PC
2503 { .name = "RGNR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 2, .opc2 = 0,
2504 .access = PL1_RW,
1bc04a88 2505 .fieldoffset = offsetof(CPUARMState, pmsav7.rnr[M_REG_NS]),
69ceea64
PM
2506 .writefn = pmsav7_rgnr_write,
2507 .resetfn = arm_cp_reset_ignore },
6cb0b013
PC
2508 REGINFO_SENTINEL
2509};
2510
18032bec
PM
2511static const ARMCPRegInfo pmsav5_cp_reginfo[] = {
2512 { .name = "DATA_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
7a0e58fa 2513 .access = PL1_RW, .type = ARM_CP_ALIAS,
7e09797c 2514 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
18032bec
PM
2515 .readfn = pmsav5_data_ap_read, .writefn = pmsav5_data_ap_write, },
2516 { .name = "INSN_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
7a0e58fa 2517 .access = PL1_RW, .type = ARM_CP_ALIAS,
7e09797c 2518 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
18032bec
PM
2519 .readfn = pmsav5_insn_ap_read, .writefn = pmsav5_insn_ap_write, },
2520 { .name = "DATA_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 2,
2521 .access = PL1_RW,
7e09797c
PM
2522 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
2523 .resetvalue = 0, },
18032bec
PM
2524 { .name = "INSN_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 3,
2525 .access = PL1_RW,
7e09797c
PM
2526 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
2527 .resetvalue = 0, },
ecce5c3c
PM
2528 { .name = "DCACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
2529 .access = PL1_RW,
2530 .fieldoffset = offsetof(CPUARMState, cp15.c2_data), .resetvalue = 0, },
2531 { .name = "ICACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 1,
2532 .access = PL1_RW,
2533 .fieldoffset = offsetof(CPUARMState, cp15.c2_insn), .resetvalue = 0, },
06d76f31 2534 /* Protection region base and size registers */
e508a92b
PM
2535 { .name = "946_PRBS0", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0,
2536 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2537 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[0]) },
2538 { .name = "946_PRBS1", .cp = 15, .crn = 6, .crm = 1, .opc1 = 0,
2539 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2540 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[1]) },
2541 { .name = "946_PRBS2", .cp = 15, .crn = 6, .crm = 2, .opc1 = 0,
2542 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2543 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[2]) },
2544 { .name = "946_PRBS3", .cp = 15, .crn = 6, .crm = 3, .opc1 = 0,
2545 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2546 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[3]) },
2547 { .name = "946_PRBS4", .cp = 15, .crn = 6, .crm = 4, .opc1 = 0,
2548 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2549 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[4]) },
2550 { .name = "946_PRBS5", .cp = 15, .crn = 6, .crm = 5, .opc1 = 0,
2551 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2552 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[5]) },
2553 { .name = "946_PRBS6", .cp = 15, .crn = 6, .crm = 6, .opc1 = 0,
2554 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2555 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[6]) },
2556 { .name = "946_PRBS7", .cp = 15, .crn = 6, .crm = 7, .opc1 = 0,
2557 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2558 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[7]) },
18032bec
PM
2559 REGINFO_SENTINEL
2560};
2561
c4241c7d
PM
2562static void vmsa_ttbcr_raw_write(CPUARMState *env, const ARMCPRegInfo *ri,
2563 uint64_t value)
ecce5c3c 2564{
11f136ee 2565 TCR *tcr = raw_ptr(env, ri);
2ebcebe2
PM
2566 int maskshift = extract32(value, 0, 3);
2567
e389be16
FA
2568 if (!arm_feature(env, ARM_FEATURE_V8)) {
2569 if (arm_feature(env, ARM_FEATURE_LPAE) && (value & TTBCR_EAE)) {
2570 /* Pre ARMv8 bits [21:19], [15:14] and [6:3] are UNK/SBZP when
2571 * using Long-desciptor translation table format */
2572 value &= ~((7 << 19) | (3 << 14) | (0xf << 3));
2573 } else if (arm_feature(env, ARM_FEATURE_EL3)) {
2574 /* In an implementation that includes the Security Extensions
2575 * TTBCR has additional fields PD0 [4] and PD1 [5] for
2576 * Short-descriptor translation table format.
2577 */
2578 value &= TTBCR_PD1 | TTBCR_PD0 | TTBCR_N;
2579 } else {
2580 value &= TTBCR_N;
2581 }
e42c4db3 2582 }
e389be16 2583
b6af0975 2584 /* Update the masks corresponding to the TCR bank being written
11f136ee 2585 * Note that we always calculate mask and base_mask, but
e42c4db3 2586 * they are only used for short-descriptor tables (ie if EAE is 0);
11f136ee
FA
2587 * for long-descriptor tables the TCR fields are used differently
2588 * and the mask and base_mask values are meaningless.
e42c4db3 2589 */
11f136ee
FA
2590 tcr->raw_tcr = value;
2591 tcr->mask = ~(((uint32_t)0xffffffffu) >> maskshift);
2592 tcr->base_mask = ~((uint32_t)0x3fffu >> maskshift);
ecce5c3c
PM
2593}
2594
c4241c7d
PM
2595static void vmsa_ttbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2596 uint64_t value)
d4e6df63 2597{
00c8cb0a
AF
2598 ARMCPU *cpu = arm_env_get_cpu(env);
2599
d4e6df63
PM
2600 if (arm_feature(env, ARM_FEATURE_LPAE)) {
2601 /* With LPAE the TTBCR could result in a change of ASID
2602 * via the TTBCR.A1 bit, so do a TLB flush.
2603 */
d10eb08f 2604 tlb_flush(CPU(cpu));
d4e6df63 2605 }
c4241c7d 2606 vmsa_ttbcr_raw_write(env, ri, value);
d4e6df63
PM
2607}
2608
ecce5c3c
PM
2609static void vmsa_ttbcr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2610{
11f136ee
FA
2611 TCR *tcr = raw_ptr(env, ri);
2612
2613 /* Reset both the TCR as well as the masks corresponding to the bank of
2614 * the TCR being reset.
2615 */
2616 tcr->raw_tcr = 0;
2617 tcr->mask = 0;
2618 tcr->base_mask = 0xffffc000u;
ecce5c3c
PM
2619}
2620
cb2e37df
PM
2621static void vmsa_tcr_el1_write(CPUARMState *env, const ARMCPRegInfo *ri,
2622 uint64_t value)
2623{
00c8cb0a 2624 ARMCPU *cpu = arm_env_get_cpu(env);
11f136ee 2625 TCR *tcr = raw_ptr(env, ri);
00c8cb0a 2626
cb2e37df 2627 /* For AArch64 the A1 bit could result in a change of ASID, so TLB flush. */
d10eb08f 2628 tlb_flush(CPU(cpu));
11f136ee 2629 tcr->raw_tcr = value;
cb2e37df
PM
2630}
2631
327ed10f
PM
2632static void vmsa_ttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2633 uint64_t value)
2634{
2635 /* 64 bit accesses to the TTBRs can change the ASID and so we
2636 * must flush the TLB.
2637 */
2638 if (cpreg_field_is_64bit(ri)) {
00c8cb0a
AF
2639 ARMCPU *cpu = arm_env_get_cpu(env);
2640
d10eb08f 2641 tlb_flush(CPU(cpu));
327ed10f
PM
2642 }
2643 raw_write(env, ri, value);
2644}
2645
b698e9cf
EI
2646static void vttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2647 uint64_t value)
2648{
2649 ARMCPU *cpu = arm_env_get_cpu(env);
2650 CPUState *cs = CPU(cpu);
2651
2652 /* Accesses to VTTBR may change the VMID so we must flush the TLB. */
2653 if (raw_read(env, ri) != value) {
0336cbf8 2654 tlb_flush_by_mmuidx(cs,
8bd5c820
PM
2655 ARMMMUIdxBit_S12NSE1 |
2656 ARMMMUIdxBit_S12NSE0 |
2657 ARMMMUIdxBit_S2NS);
b698e9cf
EI
2658 raw_write(env, ri, value);
2659 }
2660}
2661
8e5d75c9 2662static const ARMCPRegInfo vmsa_pmsa_cp_reginfo[] = {
18032bec 2663 { .name = "DFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
7a0e58fa 2664 .access = PL1_RW, .type = ARM_CP_ALIAS,
4a7e2d73 2665 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dfsr_s),
b061a82b 2666 offsetoflow32(CPUARMState, cp15.dfsr_ns) }, },
18032bec 2667 { .name = "IFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
88ca1c2d
FA
2668 .access = PL1_RW, .resetvalue = 0,
2669 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.ifsr_s),
2670 offsetoflow32(CPUARMState, cp15.ifsr_ns) } },
8e5d75c9
PC
2671 { .name = "DFAR", .cp = 15, .opc1 = 0, .crn = 6, .crm = 0, .opc2 = 0,
2672 .access = PL1_RW, .resetvalue = 0,
2673 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.dfar_s),
2674 offsetof(CPUARMState, cp15.dfar_ns) } },
2675 { .name = "FAR_EL1", .state = ARM_CP_STATE_AA64,
2676 .opc0 = 3, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 0,
2677 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[1]),
2678 .resetvalue = 0, },
2679 REGINFO_SENTINEL
2680};
2681
2682static const ARMCPRegInfo vmsa_cp_reginfo[] = {
6cd8a264
RH
2683 { .name = "ESR_EL1", .state = ARM_CP_STATE_AA64,
2684 .opc0 = 3, .crn = 5, .crm = 2, .opc1 = 0, .opc2 = 0,
2685 .access = PL1_RW,
d81c519c 2686 .fieldoffset = offsetof(CPUARMState, cp15.esr_el[1]), .resetvalue = 0, },
327ed10f 2687 { .name = "TTBR0_EL1", .state = ARM_CP_STATE_BOTH,
7dd8c9af
FA
2688 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 0,
2689 .access = PL1_RW, .writefn = vmsa_ttbr_write, .resetvalue = 0,
2690 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
2691 offsetof(CPUARMState, cp15.ttbr0_ns) } },
327ed10f 2692 { .name = "TTBR1_EL1", .state = ARM_CP_STATE_BOTH,
7dd8c9af
FA
2693 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 1,
2694 .access = PL1_RW, .writefn = vmsa_ttbr_write, .resetvalue = 0,
2695 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
2696 offsetof(CPUARMState, cp15.ttbr1_ns) } },
cb2e37df
PM
2697 { .name = "TCR_EL1", .state = ARM_CP_STATE_AA64,
2698 .opc0 = 3, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
2699 .access = PL1_RW, .writefn = vmsa_tcr_el1_write,
2700 .resetfn = vmsa_ttbcr_reset, .raw_writefn = raw_write,
11f136ee 2701 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[1]) },
cb2e37df 2702 { .name = "TTBCR", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
7a0e58fa 2703 .access = PL1_RW, .type = ARM_CP_ALIAS, .writefn = vmsa_ttbcr_write,
b061a82b 2704 .raw_writefn = vmsa_ttbcr_raw_write,
11f136ee
FA
2705 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tcr_el[3]),
2706 offsetoflow32(CPUARMState, cp15.tcr_el[1])} },
18032bec
PM
2707 REGINFO_SENTINEL
2708};
2709
c4241c7d
PM
2710static void omap_ticonfig_write(CPUARMState *env, const ARMCPRegInfo *ri,
2711 uint64_t value)
1047b9d7
PM
2712{
2713 env->cp15.c15_ticonfig = value & 0xe7;
2714 /* The OS_TYPE bit in this register changes the reported CPUID! */
2715 env->cp15.c0_cpuid = (value & (1 << 5)) ?
2716 ARM_CPUID_TI915T : ARM_CPUID_TI925T;
1047b9d7
PM
2717}
2718
c4241c7d
PM
2719static void omap_threadid_write(CPUARMState *env, const ARMCPRegInfo *ri,
2720 uint64_t value)
1047b9d7
PM
2721{
2722 env->cp15.c15_threadid = value & 0xffff;
1047b9d7
PM
2723}
2724
c4241c7d
PM
2725static void omap_wfi_write(CPUARMState *env, const ARMCPRegInfo *ri,
2726 uint64_t value)
1047b9d7
PM
2727{
2728 /* Wait-for-interrupt (deprecated) */
c3affe56 2729 cpu_interrupt(CPU(arm_env_get_cpu(env)), CPU_INTERRUPT_HALT);
1047b9d7
PM
2730}
2731
c4241c7d
PM
2732static void omap_cachemaint_write(CPUARMState *env, const ARMCPRegInfo *ri,
2733 uint64_t value)
c4804214
PM
2734{
2735 /* On OMAP there are registers indicating the max/min index of dcache lines
2736 * containing a dirty line; cache flush operations have to reset these.
2737 */
2738 env->cp15.c15_i_max = 0x000;
2739 env->cp15.c15_i_min = 0xff0;
c4804214
PM
2740}
2741
18032bec
PM
2742static const ARMCPRegInfo omap_cp_reginfo[] = {
2743 { .name = "DFSR", .cp = 15, .crn = 5, .crm = CP_ANY,
2744 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_OVERRIDE,
d81c519c 2745 .fieldoffset = offsetoflow32(CPUARMState, cp15.esr_el[1]),
6cd8a264 2746 .resetvalue = 0, },
1047b9d7
PM
2747 { .name = "", .cp = 15, .crn = 15, .crm = 0, .opc1 = 0, .opc2 = 0,
2748 .access = PL1_RW, .type = ARM_CP_NOP },
2749 { .name = "TICONFIG", .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0,
2750 .access = PL1_RW,
2751 .fieldoffset = offsetof(CPUARMState, cp15.c15_ticonfig), .resetvalue = 0,
2752 .writefn = omap_ticonfig_write },
2753 { .name = "IMAX", .cp = 15, .crn = 15, .crm = 2, .opc1 = 0, .opc2 = 0,
2754 .access = PL1_RW,
2755 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_max), .resetvalue = 0, },
2756 { .name = "IMIN", .cp = 15, .crn = 15, .crm = 3, .opc1 = 0, .opc2 = 0,
2757 .access = PL1_RW, .resetvalue = 0xff0,
2758 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_min) },
2759 { .name = "THREADID", .cp = 15, .crn = 15, .crm = 4, .opc1 = 0, .opc2 = 0,
2760 .access = PL1_RW,
2761 .fieldoffset = offsetof(CPUARMState, cp15.c15_threadid), .resetvalue = 0,
2762 .writefn = omap_threadid_write },
2763 { .name = "TI925T_STATUS", .cp = 15, .crn = 15,
2764 .crm = 8, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
7a0e58fa 2765 .type = ARM_CP_NO_RAW,
1047b9d7
PM
2766 .readfn = arm_cp_read_zero, .writefn = omap_wfi_write, },
2767 /* TODO: Peripheral port remap register:
2768 * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt controller
2769 * base address at $rn & ~0xfff and map size of 0x200 << ($rn & 0xfff),
2770 * when MMU is off.
2771 */
c4804214 2772 { .name = "OMAP_CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
d4e6df63 2773 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
7a0e58fa 2774 .type = ARM_CP_OVERRIDE | ARM_CP_NO_RAW,
c4804214 2775 .writefn = omap_cachemaint_write },
34f90529
PM
2776 { .name = "C9", .cp = 15, .crn = 9,
2777 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW,
2778 .type = ARM_CP_CONST | ARM_CP_OVERRIDE, .resetvalue = 0 },
1047b9d7
PM
2779 REGINFO_SENTINEL
2780};
2781
c4241c7d
PM
2782static void xscale_cpar_write(CPUARMState *env, const ARMCPRegInfo *ri,
2783 uint64_t value)
1047b9d7 2784{
c0f4af17 2785 env->cp15.c15_cpar = value & 0x3fff;
1047b9d7
PM
2786}
2787
2788static const ARMCPRegInfo xscale_cp_reginfo[] = {
2789 { .name = "XSCALE_CPAR",
2790 .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
2791 .fieldoffset = offsetof(CPUARMState, cp15.c15_cpar), .resetvalue = 0,
2792 .writefn = xscale_cpar_write, },
2771db27
PM
2793 { .name = "XSCALE_AUXCR",
2794 .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 1, .access = PL1_RW,
2795 .fieldoffset = offsetof(CPUARMState, cp15.c1_xscaleauxcr),
2796 .resetvalue = 0, },
3b771579
PM
2797 /* XScale specific cache-lockdown: since we have no cache we NOP these
2798 * and hope the guest does not really rely on cache behaviour.
2799 */
2800 { .name = "XSCALE_LOCK_ICACHE_LINE",
2801 .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 0,
2802 .access = PL1_W, .type = ARM_CP_NOP },
2803 { .name = "XSCALE_UNLOCK_ICACHE",
2804 .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 1,
2805 .access = PL1_W, .type = ARM_CP_NOP },
2806 { .name = "XSCALE_DCACHE_LOCK",
2807 .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 0,
2808 .access = PL1_RW, .type = ARM_CP_NOP },
2809 { .name = "XSCALE_UNLOCK_DCACHE",
2810 .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 1,
2811 .access = PL1_W, .type = ARM_CP_NOP },
1047b9d7
PM
2812 REGINFO_SENTINEL
2813};
2814
2815static const ARMCPRegInfo dummy_c15_cp_reginfo[] = {
2816 /* RAZ/WI the whole crn=15 space, when we don't have a more specific
2817 * implementation of this implementation-defined space.
2818 * Ideally this should eventually disappear in favour of actually
2819 * implementing the correct behaviour for all cores.
2820 */
2821 { .name = "C15_IMPDEF", .cp = 15, .crn = 15,
2822 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
3671cd87 2823 .access = PL1_RW,
7a0e58fa 2824 .type = ARM_CP_CONST | ARM_CP_NO_RAW | ARM_CP_OVERRIDE,
d4e6df63 2825 .resetvalue = 0 },
18032bec
PM
2826 REGINFO_SENTINEL
2827};
2828
c4804214
PM
2829static const ARMCPRegInfo cache_dirty_status_cp_reginfo[] = {
2830 /* Cache status: RAZ because we have no cache so it's always clean */
2831 { .name = "CDSR", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 6,
7a0e58fa 2832 .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 2833 .resetvalue = 0 },
c4804214
PM
2834 REGINFO_SENTINEL
2835};
2836
2837static const ARMCPRegInfo cache_block_ops_cp_reginfo[] = {
2838 /* We never have a a block transfer operation in progress */
2839 { .name = "BXSR", .cp = 15, .crn = 7, .crm = 12, .opc1 = 0, .opc2 = 4,
7a0e58fa 2840 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 2841 .resetvalue = 0 },
30b05bba
PM
2842 /* The cache ops themselves: these all NOP for QEMU */
2843 { .name = "IICR", .cp = 15, .crm = 5, .opc1 = 0,
2844 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
2845 { .name = "IDCR", .cp = 15, .crm = 6, .opc1 = 0,
2846 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
2847 { .name = "CDCR", .cp = 15, .crm = 12, .opc1 = 0,
2848 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
2849 { .name = "PIR", .cp = 15, .crm = 12, .opc1 = 1,
2850 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
2851 { .name = "PDR", .cp = 15, .crm = 12, .opc1 = 2,
2852 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
2853 { .name = "CIDCR", .cp = 15, .crm = 14, .opc1 = 0,
2854 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
c4804214
PM
2855 REGINFO_SENTINEL
2856};
2857
2858static const ARMCPRegInfo cache_test_clean_cp_reginfo[] = {
2859 /* The cache test-and-clean instructions always return (1 << 30)
2860 * to indicate that there are no dirty cache lines.
2861 */
2862 { .name = "TC_DCACHE", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 3,
7a0e58fa 2863 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 2864 .resetvalue = (1 << 30) },
c4804214 2865 { .name = "TCI_DCACHE", .cp = 15, .crn = 7, .crm = 14, .opc1 = 0, .opc2 = 3,
7a0e58fa 2866 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 2867 .resetvalue = (1 << 30) },
c4804214
PM
2868 REGINFO_SENTINEL
2869};
2870
34f90529
PM
2871static const ARMCPRegInfo strongarm_cp_reginfo[] = {
2872 /* Ignore ReadBuffer accesses */
2873 { .name = "C9_READBUFFER", .cp = 15, .crn = 9,
2874 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
d4e6df63 2875 .access = PL1_RW, .resetvalue = 0,
7a0e58fa 2876 .type = ARM_CP_CONST | ARM_CP_OVERRIDE | ARM_CP_NO_RAW },
34f90529
PM
2877 REGINFO_SENTINEL
2878};
2879
731de9e6
EI
2880static uint64_t midr_read(CPUARMState *env, const ARMCPRegInfo *ri)
2881{
2882 ARMCPU *cpu = arm_env_get_cpu(env);
2883 unsigned int cur_el = arm_current_el(env);
2884 bool secure = arm_is_secure(env);
2885
2886 if (arm_feature(&cpu->env, ARM_FEATURE_EL2) && !secure && cur_el == 1) {
2887 return env->cp15.vpidr_el2;
2888 }
2889 return raw_read(env, ri);
2890}
2891
06a7e647 2892static uint64_t mpidr_read_val(CPUARMState *env)
81bdde9d 2893{
eb5e1d3c
PF
2894 ARMCPU *cpu = ARM_CPU(arm_env_get_cpu(env));
2895 uint64_t mpidr = cpu->mp_affinity;
2896
81bdde9d 2897 if (arm_feature(env, ARM_FEATURE_V7MP)) {
78dbbbe4 2898 mpidr |= (1U << 31);
81bdde9d
PM
2899 /* Cores which are uniprocessor (non-coherent)
2900 * but still implement the MP extensions set
a8e81b31 2901 * bit 30. (For instance, Cortex-R5).
81bdde9d 2902 */
a8e81b31
PC
2903 if (cpu->mp_is_up) {
2904 mpidr |= (1u << 30);
2905 }
81bdde9d 2906 }
c4241c7d 2907 return mpidr;
81bdde9d
PM
2908}
2909
06a7e647
EI
2910static uint64_t mpidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
2911{
f0d574d6
EI
2912 unsigned int cur_el = arm_current_el(env);
2913 bool secure = arm_is_secure(env);
2914
2915 if (arm_feature(env, ARM_FEATURE_EL2) && !secure && cur_el == 1) {
2916 return env->cp15.vmpidr_el2;
2917 }
06a7e647
EI
2918 return mpidr_read_val(env);
2919}
2920
81bdde9d 2921static const ARMCPRegInfo mpidr_cp_reginfo[] = {
4b7fff2f
PM
2922 { .name = "MPIDR", .state = ARM_CP_STATE_BOTH,
2923 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 5,
7a0e58fa 2924 .access = PL1_R, .readfn = mpidr_read, .type = ARM_CP_NO_RAW },
81bdde9d
PM
2925 REGINFO_SENTINEL
2926};
2927
7ac681cf 2928static const ARMCPRegInfo lpae_cp_reginfo[] = {
a903c449 2929 /* NOP AMAIR0/1 */
b0fe2427
PM
2930 { .name = "AMAIR0", .state = ARM_CP_STATE_BOTH,
2931 .opc0 = 3, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 0,
a903c449 2932 .access = PL1_RW, .type = ARM_CP_CONST,
7ac681cf 2933 .resetvalue = 0 },
b0fe2427 2934 /* AMAIR1 is mapped to AMAIR_EL1[63:32] */
7ac681cf 2935 { .name = "AMAIR1", .cp = 15, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 1,
a903c449 2936 .access = PL1_RW, .type = ARM_CP_CONST,
7ac681cf 2937 .resetvalue = 0 },
891a2fe7 2938 { .name = "PAR", .cp = 15, .crm = 7, .opc1 = 0,
01c097f7
FA
2939 .access = PL1_RW, .type = ARM_CP_64BIT, .resetvalue = 0,
2940 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.par_s),
2941 offsetof(CPUARMState, cp15.par_ns)} },
891a2fe7 2942 { .name = "TTBR0", .cp = 15, .crm = 2, .opc1 = 0,
7a0e58fa 2943 .access = PL1_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
7dd8c9af
FA
2944 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
2945 offsetof(CPUARMState, cp15.ttbr0_ns) },
b061a82b 2946 .writefn = vmsa_ttbr_write, },
891a2fe7 2947 { .name = "TTBR1", .cp = 15, .crm = 2, .opc1 = 1,
7a0e58fa 2948 .access = PL1_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
7dd8c9af
FA
2949 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
2950 offsetof(CPUARMState, cp15.ttbr1_ns) },
b061a82b 2951 .writefn = vmsa_ttbr_write, },
7ac681cf
PM
2952 REGINFO_SENTINEL
2953};
2954
c4241c7d 2955static uint64_t aa64_fpcr_read(CPUARMState *env, const ARMCPRegInfo *ri)
b0d2b7d0 2956{
c4241c7d 2957 return vfp_get_fpcr(env);
b0d2b7d0
PM
2958}
2959
c4241c7d
PM
2960static void aa64_fpcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2961 uint64_t value)
b0d2b7d0
PM
2962{
2963 vfp_set_fpcr(env, value);
b0d2b7d0
PM
2964}
2965
c4241c7d 2966static uint64_t aa64_fpsr_read(CPUARMState *env, const ARMCPRegInfo *ri)
b0d2b7d0 2967{
c4241c7d 2968 return vfp_get_fpsr(env);
b0d2b7d0
PM
2969}
2970
c4241c7d
PM
2971static void aa64_fpsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2972 uint64_t value)
b0d2b7d0
PM
2973{
2974 vfp_set_fpsr(env, value);
b0d2b7d0
PM
2975}
2976
3f208fd7
PM
2977static CPAccessResult aa64_daif_access(CPUARMState *env, const ARMCPRegInfo *ri,
2978 bool isread)
c2b820fe 2979{
137feaa9 2980 if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_UMA)) {
c2b820fe
PM
2981 return CP_ACCESS_TRAP;
2982 }
2983 return CP_ACCESS_OK;
2984}
2985
2986static void aa64_daif_write(CPUARMState *env, const ARMCPRegInfo *ri,
2987 uint64_t value)
2988{
2989 env->daif = value & PSTATE_DAIF;
2990}
2991
8af35c37 2992static CPAccessResult aa64_cacheop_access(CPUARMState *env,
3f208fd7
PM
2993 const ARMCPRegInfo *ri,
2994 bool isread)
8af35c37
PM
2995{
2996 /* Cache invalidate/clean: NOP, but EL0 must UNDEF unless
2997 * SCTLR_EL1.UCI is set.
2998 */
137feaa9 2999 if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_UCI)) {
8af35c37
PM
3000 return CP_ACCESS_TRAP;
3001 }
3002 return CP_ACCESS_OK;
3003}
3004
dbb1fb27
AB
3005/* See: D4.7.2 TLB maintenance requirements and the TLB maintenance instructions
3006 * Page D4-1736 (DDI0487A.b)
3007 */
3008
fd3ed969
PM
3009static void tlbi_aa64_vmalle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
3010 uint64_t value)
168aa23b 3011{
a67cf277 3012 CPUState *cs = ENV_GET_CPU(env);
dbb1fb27 3013
fd3ed969 3014 if (arm_is_secure_below_el3(env)) {
0336cbf8 3015 tlb_flush_by_mmuidx(cs,
8bd5c820
PM
3016 ARMMMUIdxBit_S1SE1 |
3017 ARMMMUIdxBit_S1SE0);
fd3ed969 3018 } else {
0336cbf8 3019 tlb_flush_by_mmuidx(cs,
8bd5c820
PM
3020 ARMMMUIdxBit_S12NSE1 |
3021 ARMMMUIdxBit_S12NSE0);
fd3ed969 3022 }
168aa23b
PM
3023}
3024
fd3ed969
PM
3025static void tlbi_aa64_vmalle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
3026 uint64_t value)
168aa23b 3027{
a67cf277 3028 CPUState *cs = ENV_GET_CPU(env);
fd3ed969 3029 bool sec = arm_is_secure_below_el3(env);
dbb1fb27 3030
a67cf277
AB
3031 if (sec) {
3032 tlb_flush_by_mmuidx_all_cpus_synced(cs,
8bd5c820
PM
3033 ARMMMUIdxBit_S1SE1 |
3034 ARMMMUIdxBit_S1SE0);
a67cf277
AB
3035 } else {
3036 tlb_flush_by_mmuidx_all_cpus_synced(cs,
8bd5c820
PM
3037 ARMMMUIdxBit_S12NSE1 |
3038 ARMMMUIdxBit_S12NSE0);
fd3ed969 3039 }
168aa23b
PM
3040}
3041
fd3ed969
PM
3042static void tlbi_aa64_alle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
3043 uint64_t value)
168aa23b 3044{
fd3ed969
PM
3045 /* Note that the 'ALL' scope must invalidate both stage 1 and
3046 * stage 2 translations, whereas most other scopes only invalidate
3047 * stage 1 translations.
3048 */
00c8cb0a 3049 ARMCPU *cpu = arm_env_get_cpu(env);
fd3ed969
PM
3050 CPUState *cs = CPU(cpu);
3051
3052 if (arm_is_secure_below_el3(env)) {
0336cbf8 3053 tlb_flush_by_mmuidx(cs,
8bd5c820
PM
3054 ARMMMUIdxBit_S1SE1 |
3055 ARMMMUIdxBit_S1SE0);
fd3ed969
PM
3056 } else {
3057 if (arm_feature(env, ARM_FEATURE_EL2)) {
0336cbf8 3058 tlb_flush_by_mmuidx(cs,
8bd5c820
PM
3059 ARMMMUIdxBit_S12NSE1 |
3060 ARMMMUIdxBit_S12NSE0 |
3061 ARMMMUIdxBit_S2NS);
fd3ed969 3062 } else {
0336cbf8 3063 tlb_flush_by_mmuidx(cs,
8bd5c820
PM
3064 ARMMMUIdxBit_S12NSE1 |
3065 ARMMMUIdxBit_S12NSE0);
fd3ed969
PM
3066 }
3067 }
168aa23b
PM
3068}
3069
fd3ed969 3070static void tlbi_aa64_alle2_write(CPUARMState *env, const ARMCPRegInfo *ri,
fa439fc5
PM
3071 uint64_t value)
3072{
fd3ed969
PM
3073 ARMCPU *cpu = arm_env_get_cpu(env);
3074 CPUState *cs = CPU(cpu);
3075
8bd5c820 3076 tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_S1E2);
fd3ed969
PM
3077}
3078
43efaa33
PM
3079static void tlbi_aa64_alle3_write(CPUARMState *env, const ARMCPRegInfo *ri,
3080 uint64_t value)
3081{
3082 ARMCPU *cpu = arm_env_get_cpu(env);
3083 CPUState *cs = CPU(cpu);
3084
8bd5c820 3085 tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_S1E3);
43efaa33
PM
3086}
3087
fd3ed969
PM
3088static void tlbi_aa64_alle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
3089 uint64_t value)
3090{
3091 /* Note that the 'ALL' scope must invalidate both stage 1 and
3092 * stage 2 translations, whereas most other scopes only invalidate
3093 * stage 1 translations.
3094 */
a67cf277 3095 CPUState *cs = ENV_GET_CPU(env);
fd3ed969
PM
3096 bool sec = arm_is_secure_below_el3(env);
3097 bool has_el2 = arm_feature(env, ARM_FEATURE_EL2);
a67cf277
AB
3098
3099 if (sec) {
3100 tlb_flush_by_mmuidx_all_cpus_synced(cs,
8bd5c820
PM
3101 ARMMMUIdxBit_S1SE1 |
3102 ARMMMUIdxBit_S1SE0);
a67cf277
AB
3103 } else if (has_el2) {
3104 tlb_flush_by_mmuidx_all_cpus_synced(cs,
8bd5c820
PM
3105 ARMMMUIdxBit_S12NSE1 |
3106 ARMMMUIdxBit_S12NSE0 |
3107 ARMMMUIdxBit_S2NS);
a67cf277
AB
3108 } else {
3109 tlb_flush_by_mmuidx_all_cpus_synced(cs,
8bd5c820
PM
3110 ARMMMUIdxBit_S12NSE1 |
3111 ARMMMUIdxBit_S12NSE0);
fa439fc5
PM
3112 }
3113}
3114
2bfb9d75
PM
3115static void tlbi_aa64_alle2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
3116 uint64_t value)
3117{
a67cf277 3118 CPUState *cs = ENV_GET_CPU(env);
2bfb9d75 3119
8bd5c820 3120 tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_S1E2);
2bfb9d75
PM
3121}
3122
43efaa33
PM
3123static void tlbi_aa64_alle3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
3124 uint64_t value)
3125{
a67cf277 3126 CPUState *cs = ENV_GET_CPU(env);
43efaa33 3127
8bd5c820 3128 tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_S1E3);
43efaa33
PM
3129}
3130
fd3ed969
PM
3131static void tlbi_aa64_vae1_write(CPUARMState *env, const ARMCPRegInfo *ri,
3132 uint64_t value)
3133{
3134 /* Invalidate by VA, EL1&0 (AArch64 version).
3135 * Currently handles all of VAE1, VAAE1, VAALE1 and VALE1,
3136 * since we don't support flush-for-specific-ASID-only or
3137 * flush-last-level-only.
3138 */
3139 ARMCPU *cpu = arm_env_get_cpu(env);
3140 CPUState *cs = CPU(cpu);
3141 uint64_t pageaddr = sextract64(value << 12, 0, 56);
3142
3143 if (arm_is_secure_below_el3(env)) {
0336cbf8 3144 tlb_flush_page_by_mmuidx(cs, pageaddr,
8bd5c820
PM
3145 ARMMMUIdxBit_S1SE1 |
3146 ARMMMUIdxBit_S1SE0);
fd3ed969 3147 } else {
0336cbf8 3148 tlb_flush_page_by_mmuidx(cs, pageaddr,
8bd5c820
PM
3149 ARMMMUIdxBit_S12NSE1 |
3150 ARMMMUIdxBit_S12NSE0);
fd3ed969
PM
3151 }
3152}
3153
3154static void tlbi_aa64_vae2_write(CPUARMState *env, const ARMCPRegInfo *ri,
3155 uint64_t value)
fa439fc5 3156{
fd3ed969
PM
3157 /* Invalidate by VA, EL2
3158 * Currently handles both VAE2 and VALE2, since we don't support
3159 * flush-last-level-only.
3160 */
3161 ARMCPU *cpu = arm_env_get_cpu(env);
3162 CPUState *cs = CPU(cpu);
3163 uint64_t pageaddr = sextract64(value << 12, 0, 56);
3164
8bd5c820 3165 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_S1E2);
fd3ed969
PM
3166}
3167
43efaa33
PM
3168static void tlbi_aa64_vae3_write(CPUARMState *env, const ARMCPRegInfo *ri,
3169 uint64_t value)
3170{
3171 /* Invalidate by VA, EL3
3172 * Currently handles both VAE3 and VALE3, since we don't support
3173 * flush-last-level-only.
3174 */
3175 ARMCPU *cpu = arm_env_get_cpu(env);
3176 CPUState *cs = CPU(cpu);
3177 uint64_t pageaddr = sextract64(value << 12, 0, 56);
3178
8bd5c820 3179 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_S1E3);
43efaa33
PM
3180}
3181
fd3ed969
PM
3182static void tlbi_aa64_vae1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
3183 uint64_t value)
3184{
a67cf277
AB
3185 ARMCPU *cpu = arm_env_get_cpu(env);
3186 CPUState *cs = CPU(cpu);
fd3ed969 3187 bool sec = arm_is_secure_below_el3(env);
fa439fc5
PM
3188 uint64_t pageaddr = sextract64(value << 12, 0, 56);
3189
a67cf277
AB
3190 if (sec) {
3191 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
8bd5c820
PM
3192 ARMMMUIdxBit_S1SE1 |
3193 ARMMMUIdxBit_S1SE0);
a67cf277
AB
3194 } else {
3195 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
8bd5c820
PM
3196 ARMMMUIdxBit_S12NSE1 |
3197 ARMMMUIdxBit_S12NSE0);
fa439fc5
PM
3198 }
3199}
3200
fd3ed969
PM
3201static void tlbi_aa64_vae2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
3202 uint64_t value)
fa439fc5 3203{
a67cf277 3204 CPUState *cs = ENV_GET_CPU(env);
fd3ed969 3205 uint64_t pageaddr = sextract64(value << 12, 0, 56);
fa439fc5 3206
a67cf277 3207 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
8bd5c820 3208 ARMMMUIdxBit_S1E2);
fa439fc5
PM
3209}
3210
43efaa33
PM
3211static void tlbi_aa64_vae3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
3212 uint64_t value)
3213{
a67cf277 3214 CPUState *cs = ENV_GET_CPU(env);
43efaa33
PM
3215 uint64_t pageaddr = sextract64(value << 12, 0, 56);
3216
a67cf277 3217 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
8bd5c820 3218 ARMMMUIdxBit_S1E3);
43efaa33
PM
3219}
3220
cea66e91
PM
3221static void tlbi_aa64_ipas2e1_write(CPUARMState *env, const ARMCPRegInfo *ri,
3222 uint64_t value)
3223{
3224 /* Invalidate by IPA. This has to invalidate any structures that
3225 * contain only stage 2 translation information, but does not need
3226 * to apply to structures that contain combined stage 1 and stage 2
3227 * translation information.
3228 * This must NOP if EL2 isn't implemented or SCR_EL3.NS is zero.
3229 */
3230 ARMCPU *cpu = arm_env_get_cpu(env);
3231 CPUState *cs = CPU(cpu);
3232 uint64_t pageaddr;
3233
3234 if (!arm_feature(env, ARM_FEATURE_EL2) || !(env->cp15.scr_el3 & SCR_NS)) {
3235 return;
3236 }
3237
3238 pageaddr = sextract64(value << 12, 0, 48);
3239
8bd5c820 3240 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_S2NS);
cea66e91
PM
3241}
3242
3243static void tlbi_aa64_ipas2e1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
3244 uint64_t value)
3245{
a67cf277 3246 CPUState *cs = ENV_GET_CPU(env);
cea66e91
PM
3247 uint64_t pageaddr;
3248
3249 if (!arm_feature(env, ARM_FEATURE_EL2) || !(env->cp15.scr_el3 & SCR_NS)) {
3250 return;
3251 }
3252
3253 pageaddr = sextract64(value << 12, 0, 48);
3254
a67cf277 3255 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
8bd5c820 3256 ARMMMUIdxBit_S2NS);
cea66e91
PM
3257}
3258
3f208fd7
PM
3259static CPAccessResult aa64_zva_access(CPUARMState *env, const ARMCPRegInfo *ri,
3260 bool isread)
aca3f40b
PM
3261{
3262 /* We don't implement EL2, so the only control on DC ZVA is the
3263 * bit in the SCTLR which can prohibit access for EL0.
3264 */
137feaa9 3265 if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_DZE)) {
aca3f40b
PM
3266 return CP_ACCESS_TRAP;
3267 }
3268 return CP_ACCESS_OK;
3269}
3270
3271static uint64_t aa64_dczid_read(CPUARMState *env, const ARMCPRegInfo *ri)
3272{
3273 ARMCPU *cpu = arm_env_get_cpu(env);
3274 int dzp_bit = 1 << 4;
3275
3276 /* DZP indicates whether DC ZVA access is allowed */
3f208fd7 3277 if (aa64_zva_access(env, NULL, false) == CP_ACCESS_OK) {
aca3f40b
PM
3278 dzp_bit = 0;
3279 }
3280 return cpu->dcz_blocksize | dzp_bit;
3281}
3282
3f208fd7
PM
3283static CPAccessResult sp_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
3284 bool isread)
f502cfc2 3285{
cdcf1405 3286 if (!(env->pstate & PSTATE_SP)) {
f502cfc2
PM
3287 /* Access to SP_EL0 is undefined if it's being used as
3288 * the stack pointer.
3289 */
3290 return CP_ACCESS_TRAP_UNCATEGORIZED;
3291 }
3292 return CP_ACCESS_OK;
3293}
3294
3295static uint64_t spsel_read(CPUARMState *env, const ARMCPRegInfo *ri)
3296{
3297 return env->pstate & PSTATE_SP;
3298}
3299
3300static void spsel_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
3301{
3302 update_spsel(env, val);
3303}
3304
137feaa9
FA
3305static void sctlr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3306 uint64_t value)
3307{
3308 ARMCPU *cpu = arm_env_get_cpu(env);
3309
3310 if (raw_read(env, ri) == value) {
3311 /* Skip the TLB flush if nothing actually changed; Linux likes
3312 * to do a lot of pointless SCTLR writes.
3313 */
3314 return;
3315 }
3316
06312feb
PM
3317 if (arm_feature(env, ARM_FEATURE_PMSA) && !cpu->has_mpu) {
3318 /* M bit is RAZ/WI for PMSA with no MPU implemented */
3319 value &= ~SCTLR_M;
3320 }
3321
137feaa9
FA
3322 raw_write(env, ri, value);
3323 /* ??? Lots of these bits are not implemented. */
3324 /* This may enable/disable the MMU, so do a TLB flush. */
d10eb08f 3325 tlb_flush(CPU(cpu));
137feaa9
FA
3326}
3327
3f208fd7
PM
3328static CPAccessResult fpexc32_access(CPUARMState *env, const ARMCPRegInfo *ri,
3329 bool isread)
03fbf20f
PM
3330{
3331 if ((env->cp15.cptr_el[2] & CPTR_TFP) && arm_current_el(env) == 2) {
f2cae609 3332 return CP_ACCESS_TRAP_FP_EL2;
03fbf20f
PM
3333 }
3334 if (env->cp15.cptr_el[3] & CPTR_TFP) {
f2cae609 3335 return CP_ACCESS_TRAP_FP_EL3;
03fbf20f
PM
3336 }
3337 return CP_ACCESS_OK;
3338}
3339
a8d64e73
PM
3340static void sdcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3341 uint64_t value)
3342{
3343 env->cp15.mdcr_el3 = value & SDCR_VALID_MASK;
3344}
3345
b0d2b7d0
PM
3346static const ARMCPRegInfo v8_cp_reginfo[] = {
3347 /* Minimal set of EL0-visible registers. This will need to be expanded
3348 * significantly for system emulation of AArch64 CPUs.
3349 */
3350 { .name = "NZCV", .state = ARM_CP_STATE_AA64,
3351 .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 2,
3352 .access = PL0_RW, .type = ARM_CP_NZCV },
c2b820fe
PM
3353 { .name = "DAIF", .state = ARM_CP_STATE_AA64,
3354 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 2,
7a0e58fa 3355 .type = ARM_CP_NO_RAW,
c2b820fe
PM
3356 .access = PL0_RW, .accessfn = aa64_daif_access,
3357 .fieldoffset = offsetof(CPUARMState, daif),
3358 .writefn = aa64_daif_write, .resetfn = arm_cp_reset_ignore },
b0d2b7d0
PM
3359 { .name = "FPCR", .state = ARM_CP_STATE_AA64,
3360 .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 4,
b916c9c3 3361 .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END,
fe03d45f 3362 .readfn = aa64_fpcr_read, .writefn = aa64_fpcr_write },
b0d2b7d0
PM
3363 { .name = "FPSR", .state = ARM_CP_STATE_AA64,
3364 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 4,
b916c9c3 3365 .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END,
fe03d45f 3366 .readfn = aa64_fpsr_read, .writefn = aa64_fpsr_write },
b0d2b7d0
PM
3367 { .name = "DCZID_EL0", .state = ARM_CP_STATE_AA64,
3368 .opc0 = 3, .opc1 = 3, .opc2 = 7, .crn = 0, .crm = 0,
7a0e58fa 3369 .access = PL0_R, .type = ARM_CP_NO_RAW,
aca3f40b
PM
3370 .readfn = aa64_dczid_read },
3371 { .name = "DC_ZVA", .state = ARM_CP_STATE_AA64,
3372 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 1,
3373 .access = PL0_W, .type = ARM_CP_DC_ZVA,
3374#ifndef CONFIG_USER_ONLY
3375 /* Avoid overhead of an access check that always passes in user-mode */
3376 .accessfn = aa64_zva_access,
3377#endif
3378 },
0eef9d98
PM
3379 { .name = "CURRENTEL", .state = ARM_CP_STATE_AA64,
3380 .opc0 = 3, .opc1 = 0, .opc2 = 2, .crn = 4, .crm = 2,
3381 .access = PL1_R, .type = ARM_CP_CURRENTEL },
8af35c37
PM
3382 /* Cache ops: all NOPs since we don't emulate caches */
3383 { .name = "IC_IALLUIS", .state = ARM_CP_STATE_AA64,
3384 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
3385 .access = PL1_W, .type = ARM_CP_NOP },
3386 { .name = "IC_IALLU", .state = ARM_CP_STATE_AA64,
3387 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
3388 .access = PL1_W, .type = ARM_CP_NOP },
3389 { .name = "IC_IVAU", .state = ARM_CP_STATE_AA64,
3390 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 5, .opc2 = 1,
3391 .access = PL0_W, .type = ARM_CP_NOP,
3392 .accessfn = aa64_cacheop_access },
3393 { .name = "DC_IVAC", .state = ARM_CP_STATE_AA64,
3394 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
3395 .access = PL1_W, .type = ARM_CP_NOP },
3396 { .name = "DC_ISW", .state = ARM_CP_STATE_AA64,
3397 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
3398 .access = PL1_W, .type = ARM_CP_NOP },
3399 { .name = "DC_CVAC", .state = ARM_CP_STATE_AA64,
3400 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 1,
3401 .access = PL0_W, .type = ARM_CP_NOP,
3402 .accessfn = aa64_cacheop_access },
3403 { .name = "DC_CSW", .state = ARM_CP_STATE_AA64,
3404 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
3405 .access = PL1_W, .type = ARM_CP_NOP },
3406 { .name = "DC_CVAU", .state = ARM_CP_STATE_AA64,
3407 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 11, .opc2 = 1,
3408 .access = PL0_W, .type = ARM_CP_NOP,
3409 .accessfn = aa64_cacheop_access },
3410 { .name = "DC_CIVAC", .state = ARM_CP_STATE_AA64,
3411 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 1,
3412 .access = PL0_W, .type = ARM_CP_NOP,
3413 .accessfn = aa64_cacheop_access },
3414 { .name = "DC_CISW", .state = ARM_CP_STATE_AA64,
3415 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
3416 .access = PL1_W, .type = ARM_CP_NOP },
168aa23b
PM
3417 /* TLBI operations */
3418 { .name = "TLBI_VMALLE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 3419 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
7a0e58fa 3420 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 3421 .writefn = tlbi_aa64_vmalle1is_write },
168aa23b 3422 { .name = "TLBI_VAE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 3423 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
7a0e58fa 3424 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 3425 .writefn = tlbi_aa64_vae1is_write },
168aa23b 3426 { .name = "TLBI_ASIDE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 3427 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
7a0e58fa 3428 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 3429 .writefn = tlbi_aa64_vmalle1is_write },
168aa23b 3430 { .name = "TLBI_VAAE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 3431 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
7a0e58fa 3432 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 3433 .writefn = tlbi_aa64_vae1is_write },
168aa23b 3434 { .name = "TLBI_VALE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 3435 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
7a0e58fa 3436 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 3437 .writefn = tlbi_aa64_vae1is_write },
168aa23b 3438 { .name = "TLBI_VAALE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 3439 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
7a0e58fa 3440 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 3441 .writefn = tlbi_aa64_vae1is_write },
168aa23b 3442 { .name = "TLBI_VMALLE1", .state = ARM_CP_STATE_AA64,
6ab9f499 3443 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
7a0e58fa 3444 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 3445 .writefn = tlbi_aa64_vmalle1_write },
168aa23b 3446 { .name = "TLBI_VAE1", .state = ARM_CP_STATE_AA64,
6ab9f499 3447 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
7a0e58fa 3448 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 3449 .writefn = tlbi_aa64_vae1_write },
168aa23b 3450 { .name = "TLBI_ASIDE1", .state = ARM_CP_STATE_AA64,
6ab9f499 3451 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
7a0e58fa 3452 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 3453 .writefn = tlbi_aa64_vmalle1_write },
168aa23b 3454 { .name = "TLBI_VAAE1", .state = ARM_CP_STATE_AA64,
6ab9f499 3455 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
7a0e58fa 3456 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 3457 .writefn = tlbi_aa64_vae1_write },
168aa23b 3458 { .name = "TLBI_VALE1", .state = ARM_CP_STATE_AA64,
6ab9f499 3459 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
7a0e58fa 3460 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 3461 .writefn = tlbi_aa64_vae1_write },
168aa23b 3462 { .name = "TLBI_VAALE1", .state = ARM_CP_STATE_AA64,
6ab9f499 3463 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
7a0e58fa 3464 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 3465 .writefn = tlbi_aa64_vae1_write },
cea66e91
PM
3466 { .name = "TLBI_IPAS2E1IS", .state = ARM_CP_STATE_AA64,
3467 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1,
3468 .access = PL2_W, .type = ARM_CP_NO_RAW,
3469 .writefn = tlbi_aa64_ipas2e1is_write },
3470 { .name = "TLBI_IPAS2LE1IS", .state = ARM_CP_STATE_AA64,
3471 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5,
3472 .access = PL2_W, .type = ARM_CP_NO_RAW,
3473 .writefn = tlbi_aa64_ipas2e1is_write },
83ddf975
PM
3474 { .name = "TLBI_ALLE1IS", .state = ARM_CP_STATE_AA64,
3475 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
3476 .access = PL2_W, .type = ARM_CP_NO_RAW,
fd3ed969 3477 .writefn = tlbi_aa64_alle1is_write },
43efaa33
PM
3478 { .name = "TLBI_VMALLS12E1IS", .state = ARM_CP_STATE_AA64,
3479 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 6,
3480 .access = PL2_W, .type = ARM_CP_NO_RAW,
3481 .writefn = tlbi_aa64_alle1is_write },
cea66e91
PM
3482 { .name = "TLBI_IPAS2E1", .state = ARM_CP_STATE_AA64,
3483 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1,
3484 .access = PL2_W, .type = ARM_CP_NO_RAW,
3485 .writefn = tlbi_aa64_ipas2e1_write },
3486 { .name = "TLBI_IPAS2LE1", .state = ARM_CP_STATE_AA64,
3487 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5,
3488 .access = PL2_W, .type = ARM_CP_NO_RAW,
3489 .writefn = tlbi_aa64_ipas2e1_write },
83ddf975
PM
3490 { .name = "TLBI_ALLE1", .state = ARM_CP_STATE_AA64,
3491 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
3492 .access = PL2_W, .type = ARM_CP_NO_RAW,
fd3ed969 3493 .writefn = tlbi_aa64_alle1_write },
43efaa33
PM
3494 { .name = "TLBI_VMALLS12E1", .state = ARM_CP_STATE_AA64,
3495 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 6,
3496 .access = PL2_W, .type = ARM_CP_NO_RAW,
3497 .writefn = tlbi_aa64_alle1is_write },
19525524
PM
3498#ifndef CONFIG_USER_ONLY
3499 /* 64 bit address translation operations */
3500 { .name = "AT_S1E1R", .state = ARM_CP_STATE_AA64,
3501 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 0,
060e8a48 3502 .access = PL1_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
19525524
PM
3503 { .name = "AT_S1E1W", .state = ARM_CP_STATE_AA64,
3504 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 1,
060e8a48 3505 .access = PL1_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
19525524
PM
3506 { .name = "AT_S1E0R", .state = ARM_CP_STATE_AA64,
3507 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 2,
060e8a48 3508 .access = PL1_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
19525524
PM
3509 { .name = "AT_S1E0W", .state = ARM_CP_STATE_AA64,
3510 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 3,
060e8a48 3511 .access = PL1_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
2a47df95 3512 { .name = "AT_S12E1R", .state = ARM_CP_STATE_AA64,
7a379c7e 3513 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 4,
2a47df95
PM
3514 .access = PL2_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3515 { .name = "AT_S12E1W", .state = ARM_CP_STATE_AA64,
7a379c7e 3516 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 5,
2a47df95
PM
3517 .access = PL2_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3518 { .name = "AT_S12E0R", .state = ARM_CP_STATE_AA64,
7a379c7e 3519 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 6,
2a47df95
PM
3520 .access = PL2_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3521 { .name = "AT_S12E0W", .state = ARM_CP_STATE_AA64,
7a379c7e 3522 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 7,
2a47df95
PM
3523 .access = PL2_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3524 /* AT S1E2* are elsewhere as they UNDEF from EL3 if EL2 is not present */
3525 { .name = "AT_S1E3R", .state = ARM_CP_STATE_AA64,
3526 .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 0,
3527 .access = PL3_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3528 { .name = "AT_S1E3W", .state = ARM_CP_STATE_AA64,
3529 .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 1,
3530 .access = PL3_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
c96fc9b5
EI
3531 { .name = "PAR_EL1", .state = ARM_CP_STATE_AA64,
3532 .type = ARM_CP_ALIAS,
3533 .opc0 = 3, .opc1 = 0, .crn = 7, .crm = 4, .opc2 = 0,
3534 .access = PL1_RW, .resetvalue = 0,
3535 .fieldoffset = offsetof(CPUARMState, cp15.par_el[1]),
3536 .writefn = par_write },
19525524 3537#endif
995939a6 3538 /* TLB invalidate last level of translation table walk */
9449fdf6 3539 { .name = "TLBIMVALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
7a0e58fa 3540 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_is_write },
9449fdf6 3541 { .name = "TLBIMVAALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
7a0e58fa 3542 .type = ARM_CP_NO_RAW, .access = PL1_W,
fa439fc5 3543 .writefn = tlbimvaa_is_write },
9449fdf6 3544 { .name = "TLBIMVAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
7a0e58fa 3545 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write },
9449fdf6 3546 { .name = "TLBIMVAAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
7a0e58fa 3547 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimvaa_write },
541ef8c2
SS
3548 { .name = "TLBIMVALH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
3549 .type = ARM_CP_NO_RAW, .access = PL2_W,
3550 .writefn = tlbimva_hyp_write },
3551 { .name = "TLBIMVALHIS",
3552 .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
3553 .type = ARM_CP_NO_RAW, .access = PL2_W,
3554 .writefn = tlbimva_hyp_is_write },
3555 { .name = "TLBIIPAS2",
3556 .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1,
3557 .type = ARM_CP_NO_RAW, .access = PL2_W,
3558 .writefn = tlbiipas2_write },
3559 { .name = "TLBIIPAS2IS",
3560 .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1,
3561 .type = ARM_CP_NO_RAW, .access = PL2_W,
3562 .writefn = tlbiipas2_is_write },
3563 { .name = "TLBIIPAS2L",
3564 .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5,
3565 .type = ARM_CP_NO_RAW, .access = PL2_W,
3566 .writefn = tlbiipas2_write },
3567 { .name = "TLBIIPAS2LIS",
3568 .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5,
3569 .type = ARM_CP_NO_RAW, .access = PL2_W,
3570 .writefn = tlbiipas2_is_write },
9449fdf6
PM
3571 /* 32 bit cache operations */
3572 { .name = "ICIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
3573 .type = ARM_CP_NOP, .access = PL1_W },
3574 { .name = "BPIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 6,
3575 .type = ARM_CP_NOP, .access = PL1_W },
3576 { .name = "ICIALLU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
3577 .type = ARM_CP_NOP, .access = PL1_W },
3578 { .name = "ICIMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 1,
3579 .type = ARM_CP_NOP, .access = PL1_W },
3580 { .name = "BPIALL", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 6,
3581 .type = ARM_CP_NOP, .access = PL1_W },
3582 { .name = "BPIMVA", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 7,
3583 .type = ARM_CP_NOP, .access = PL1_W },
3584 { .name = "DCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
3585 .type = ARM_CP_NOP, .access = PL1_W },
3586 { .name = "DCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
3587 .type = ARM_CP_NOP, .access = PL1_W },
3588 { .name = "DCCMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 1,
3589 .type = ARM_CP_NOP, .access = PL1_W },
3590 { .name = "DCCSW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
3591 .type = ARM_CP_NOP, .access = PL1_W },
3592 { .name = "DCCMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 11, .opc2 = 1,
3593 .type = ARM_CP_NOP, .access = PL1_W },
3594 { .name = "DCCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 1,
3595 .type = ARM_CP_NOP, .access = PL1_W },
3596 { .name = "DCCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
3597 .type = ARM_CP_NOP, .access = PL1_W },
3598 /* MMU Domain access control / MPU write buffer control */
0c17d68c
FA
3599 { .name = "DACR", .cp = 15, .opc1 = 0, .crn = 3, .crm = 0, .opc2 = 0,
3600 .access = PL1_RW, .resetvalue = 0,
3601 .writefn = dacr_write, .raw_writefn = raw_write,
3602 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
3603 offsetoflow32(CPUARMState, cp15.dacr_ns) } },
a0618a19 3604 { .name = "ELR_EL1", .state = ARM_CP_STATE_AA64,
7a0e58fa 3605 .type = ARM_CP_ALIAS,
a0618a19 3606 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 1,
6947f059
EI
3607 .access = PL1_RW,
3608 .fieldoffset = offsetof(CPUARMState, elr_el[1]) },
a65f1de9 3609 { .name = "SPSR_EL1", .state = ARM_CP_STATE_AA64,
7a0e58fa 3610 .type = ARM_CP_ALIAS,
a65f1de9 3611 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 0,
99a99c1f
SB
3612 .access = PL1_RW,
3613 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_SVC]) },
f502cfc2
PM
3614 /* We rely on the access checks not allowing the guest to write to the
3615 * state field when SPSel indicates that it's being used as the stack
3616 * pointer.
3617 */
3618 { .name = "SP_EL0", .state = ARM_CP_STATE_AA64,
3619 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 1, .opc2 = 0,
3620 .access = PL1_RW, .accessfn = sp_el0_access,
7a0e58fa 3621 .type = ARM_CP_ALIAS,
f502cfc2 3622 .fieldoffset = offsetof(CPUARMState, sp_el[0]) },
884b4dee
GB
3623 { .name = "SP_EL1", .state = ARM_CP_STATE_AA64,
3624 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 1, .opc2 = 0,
7a0e58fa 3625 .access = PL2_RW, .type = ARM_CP_ALIAS,
884b4dee 3626 .fieldoffset = offsetof(CPUARMState, sp_el[1]) },
f502cfc2
PM
3627 { .name = "SPSel", .state = ARM_CP_STATE_AA64,
3628 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 0,
7a0e58fa 3629 .type = ARM_CP_NO_RAW,
f502cfc2 3630 .access = PL1_RW, .readfn = spsel_read, .writefn = spsel_write },
03fbf20f
PM
3631 { .name = "FPEXC32_EL2", .state = ARM_CP_STATE_AA64,
3632 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 3, .opc2 = 0,
3633 .type = ARM_CP_ALIAS,
3634 .fieldoffset = offsetof(CPUARMState, vfp.xregs[ARM_VFP_FPEXC]),
3635 .access = PL2_RW, .accessfn = fpexc32_access },
6a43e0b6
PM
3636 { .name = "DACR32_EL2", .state = ARM_CP_STATE_AA64,
3637 .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 0, .opc2 = 0,
3638 .access = PL2_RW, .resetvalue = 0,
3639 .writefn = dacr_write, .raw_writefn = raw_write,
3640 .fieldoffset = offsetof(CPUARMState, cp15.dacr32_el2) },
3641 { .name = "IFSR32_EL2", .state = ARM_CP_STATE_AA64,
3642 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 0, .opc2 = 1,
3643 .access = PL2_RW, .resetvalue = 0,
3644 .fieldoffset = offsetof(CPUARMState, cp15.ifsr32_el2) },
3645 { .name = "SPSR_IRQ", .state = ARM_CP_STATE_AA64,
3646 .type = ARM_CP_ALIAS,
3647 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 0,
3648 .access = PL2_RW,
3649 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_IRQ]) },
3650 { .name = "SPSR_ABT", .state = ARM_CP_STATE_AA64,
3651 .type = ARM_CP_ALIAS,
3652 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 1,
3653 .access = PL2_RW,
3654 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_ABT]) },
3655 { .name = "SPSR_UND", .state = ARM_CP_STATE_AA64,
3656 .type = ARM_CP_ALIAS,
3657 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 2,
3658 .access = PL2_RW,
3659 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_UND]) },
3660 { .name = "SPSR_FIQ", .state = ARM_CP_STATE_AA64,
3661 .type = ARM_CP_ALIAS,
3662 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 3,
3663 .access = PL2_RW,
3664 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_FIQ]) },
a8d64e73
PM
3665 { .name = "MDCR_EL3", .state = ARM_CP_STATE_AA64,
3666 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 3, .opc2 = 1,
3667 .resetvalue = 0,
3668 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el3) },
3669 { .name = "SDCR", .type = ARM_CP_ALIAS,
3670 .cp = 15, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 1,
3671 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
3672 .writefn = sdcr_write,
3673 .fieldoffset = offsetoflow32(CPUARMState, cp15.mdcr_el3) },
b0d2b7d0
PM
3674 REGINFO_SENTINEL
3675};
3676
d42e3c26 3677/* Used to describe the behaviour of EL2 regs when EL2 does not exist. */
4771cd01 3678static const ARMCPRegInfo el3_no_el2_cp_reginfo[] = {
d42e3c26
EI
3679 { .name = "VBAR_EL2", .state = ARM_CP_STATE_AA64,
3680 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0,
3681 .access = PL2_RW,
3682 .readfn = arm_cp_read_zero, .writefn = arm_cp_write_ignore },
f149e3e8 3683 { .name = "HCR_EL2", .state = ARM_CP_STATE_AA64,
7a0e58fa 3684 .type = ARM_CP_NO_RAW,
f149e3e8
EI
3685 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
3686 .access = PL2_RW,
3687 .readfn = arm_cp_read_zero, .writefn = arm_cp_write_ignore },
c6f19164
GB
3688 { .name = "CPTR_EL2", .state = ARM_CP_STATE_BOTH,
3689 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 2,
3690 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
95f949ac
EI
3691 { .name = "MAIR_EL2", .state = ARM_CP_STATE_BOTH,
3692 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 0,
3693 .access = PL2_RW, .type = ARM_CP_CONST,
3694 .resetvalue = 0 },
3695 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
3696 .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 1,
3697 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
2179ef95
PM
3698 { .name = "AMAIR_EL2", .state = ARM_CP_STATE_BOTH,
3699 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 0,
3700 .access = PL2_RW, .type = ARM_CP_CONST,
3701 .resetvalue = 0 },
3702 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
3703 .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 1,
3704 .access = PL2_RW, .type = ARM_CP_CONST,
3705 .resetvalue = 0 },
37cd6c24
PM
3706 { .name = "AFSR0_EL2", .state = ARM_CP_STATE_BOTH,
3707 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 0,
3708 .access = PL2_RW, .type = ARM_CP_CONST,
3709 .resetvalue = 0 },
3710 { .name = "AFSR1_EL2", .state = ARM_CP_STATE_BOTH,
3711 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 1,
3712 .access = PL2_RW, .type = ARM_CP_CONST,
3713 .resetvalue = 0 },
06ec4c8c
EI
3714 { .name = "TCR_EL2", .state = ARM_CP_STATE_BOTH,
3715 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 2,
3716 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
68e9c2fe
EI
3717 { .name = "VTCR_EL2", .state = ARM_CP_STATE_BOTH,
3718 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
3719 .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any,
3720 .type = ARM_CP_CONST, .resetvalue = 0 },
b698e9cf
EI
3721 { .name = "VTTBR", .state = ARM_CP_STATE_AA32,
3722 .cp = 15, .opc1 = 6, .crm = 2,
3723 .access = PL2_RW, .accessfn = access_el3_aa32ns,
3724 .type = ARM_CP_CONST | ARM_CP_64BIT, .resetvalue = 0 },
3725 { .name = "VTTBR_EL2", .state = ARM_CP_STATE_AA64,
3726 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 0,
3727 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
b9cb5323
EI
3728 { .name = "SCTLR_EL2", .state = ARM_CP_STATE_BOTH,
3729 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 0,
3730 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
ff05f37b
EI
3731 { .name = "TPIDR_EL2", .state = ARM_CP_STATE_BOTH,
3732 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 2,
3733 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
a57633c0
EI
3734 { .name = "TTBR0_EL2", .state = ARM_CP_STATE_AA64,
3735 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
3736 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3737 { .name = "HTTBR", .cp = 15, .opc1 = 4, .crm = 2,
3738 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_CONST,
3739 .resetvalue = 0 },
0b6440af
EI
3740 { .name = "CNTHCTL_EL2", .state = ARM_CP_STATE_BOTH,
3741 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 1, .opc2 = 0,
3742 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
edac4d8a
EI
3743 { .name = "CNTVOFF_EL2", .state = ARM_CP_STATE_AA64,
3744 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 3,
3745 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3746 { .name = "CNTVOFF", .cp = 15, .opc1 = 4, .crm = 14,
3747 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_CONST,
3748 .resetvalue = 0 },
b0e66d95
EI
3749 { .name = "CNTHP_CVAL_EL2", .state = ARM_CP_STATE_AA64,
3750 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 2,
3751 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3752 { .name = "CNTHP_CVAL", .cp = 15, .opc1 = 6, .crm = 14,
3753 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_CONST,
3754 .resetvalue = 0 },
3755 { .name = "CNTHP_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
3756 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 0,
3757 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3758 { .name = "CNTHP_CTL_EL2", .state = ARM_CP_STATE_BOTH,
3759 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 1,
3760 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
14cc7b54
SF
3761 { .name = "MDCR_EL2", .state = ARM_CP_STATE_BOTH,
3762 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 1,
d6c8cf81
PM
3763 .access = PL2_RW, .accessfn = access_tda,
3764 .type = ARM_CP_CONST, .resetvalue = 0 },
59e05530
EI
3765 { .name = "HPFAR_EL2", .state = ARM_CP_STATE_BOTH,
3766 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
3767 .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any,
3768 .type = ARM_CP_CONST, .resetvalue = 0 },
2a5a9abd
AF
3769 { .name = "HSTR_EL2", .state = ARM_CP_STATE_BOTH,
3770 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 3,
3771 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
d42e3c26
EI
3772 REGINFO_SENTINEL
3773};
3774
f149e3e8
EI
3775static void hcr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
3776{
3777 ARMCPU *cpu = arm_env_get_cpu(env);
3778 uint64_t valid_mask = HCR_MASK;
3779
3780 if (arm_feature(env, ARM_FEATURE_EL3)) {
3781 valid_mask &= ~HCR_HCD;
77077a83
JK
3782 } else if (cpu->psci_conduit != QEMU_PSCI_CONDUIT_SMC) {
3783 /* Architecturally HCR.TSC is RES0 if EL3 is not implemented.
3784 * However, if we're using the SMC PSCI conduit then QEMU is
3785 * effectively acting like EL3 firmware and so the guest at
3786 * EL2 should retain the ability to prevent EL1 from being
3787 * able to make SMC calls into the ersatz firmware, so in
3788 * that case HCR.TSC should be read/write.
3789 */
f149e3e8
EI
3790 valid_mask &= ~HCR_TSC;
3791 }
3792
3793 /* Clear RES0 bits. */
3794 value &= valid_mask;
3795
3796 /* These bits change the MMU setup:
3797 * HCR_VM enables stage 2 translation
3798 * HCR_PTW forbids certain page-table setups
3799 * HCR_DC Disables stage1 and enables stage2 translation
3800 */
3801 if ((raw_read(env, ri) ^ value) & (HCR_VM | HCR_PTW | HCR_DC)) {
d10eb08f 3802 tlb_flush(CPU(cpu));
f149e3e8
EI
3803 }
3804 raw_write(env, ri, value);
3805}
3806
4771cd01 3807static const ARMCPRegInfo el2_cp_reginfo[] = {
f149e3e8
EI
3808 { .name = "HCR_EL2", .state = ARM_CP_STATE_AA64,
3809 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
3810 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
3811 .writefn = hcr_write },
3b685ba7 3812 { .name = "ELR_EL2", .state = ARM_CP_STATE_AA64,
7a0e58fa 3813 .type = ARM_CP_ALIAS,
3b685ba7
EI
3814 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 1,
3815 .access = PL2_RW,
3816 .fieldoffset = offsetof(CPUARMState, elr_el[2]) },
f2c30f42 3817 { .name = "ESR_EL2", .state = ARM_CP_STATE_AA64,
f2c30f42
EI
3818 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 0,
3819 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[2]) },
63b60551
EI
3820 { .name = "FAR_EL2", .state = ARM_CP_STATE_AA64,
3821 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 0,
3822 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[2]) },
3b685ba7 3823 { .name = "SPSR_EL2", .state = ARM_CP_STATE_AA64,
7a0e58fa 3824 .type = ARM_CP_ALIAS,
3b685ba7 3825 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 0,
99a99c1f
SB
3826 .access = PL2_RW,
3827 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_HYP]) },
d42e3c26
EI
3828 { .name = "VBAR_EL2", .state = ARM_CP_STATE_AA64,
3829 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0,
3830 .access = PL2_RW, .writefn = vbar_write,
3831 .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[2]),
3832 .resetvalue = 0 },
884b4dee
GB
3833 { .name = "SP_EL2", .state = ARM_CP_STATE_AA64,
3834 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 1, .opc2 = 0,
7a0e58fa 3835 .access = PL3_RW, .type = ARM_CP_ALIAS,
884b4dee 3836 .fieldoffset = offsetof(CPUARMState, sp_el[2]) },
c6f19164
GB
3837 { .name = "CPTR_EL2", .state = ARM_CP_STATE_BOTH,
3838 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 2,
3839 .access = PL2_RW, .accessfn = cptr_access, .resetvalue = 0,
3840 .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[2]) },
95f949ac
EI
3841 { .name = "MAIR_EL2", .state = ARM_CP_STATE_BOTH,
3842 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 0,
3843 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[2]),
3844 .resetvalue = 0 },
3845 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
3846 .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 1,
3847 .access = PL2_RW, .type = ARM_CP_ALIAS,
3848 .fieldoffset = offsetofhigh32(CPUARMState, cp15.mair_el[2]) },
2179ef95
PM
3849 { .name = "AMAIR_EL2", .state = ARM_CP_STATE_BOTH,
3850 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 0,
3851 .access = PL2_RW, .type = ARM_CP_CONST,
3852 .resetvalue = 0 },
3853 /* HAMAIR1 is mapped to AMAIR_EL2[63:32] */
3854 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
3855 .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 1,
3856 .access = PL2_RW, .type = ARM_CP_CONST,
3857 .resetvalue = 0 },
37cd6c24
PM
3858 { .name = "AFSR0_EL2", .state = ARM_CP_STATE_BOTH,
3859 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 0,
3860 .access = PL2_RW, .type = ARM_CP_CONST,
3861 .resetvalue = 0 },
3862 { .name = "AFSR1_EL2", .state = ARM_CP_STATE_BOTH,
3863 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 1,
3864 .access = PL2_RW, .type = ARM_CP_CONST,
3865 .resetvalue = 0 },
06ec4c8c
EI
3866 { .name = "TCR_EL2", .state = ARM_CP_STATE_BOTH,
3867 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 2,
6459b94c
PM
3868 .access = PL2_RW,
3869 /* no .writefn needed as this can't cause an ASID change;
3870 * no .raw_writefn or .resetfn needed as we never use mask/base_mask
3871 */
06ec4c8c 3872 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[2]) },
68e9c2fe
EI
3873 { .name = "VTCR", .state = ARM_CP_STATE_AA32,
3874 .cp = 15, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
bf06c112 3875 .type = ARM_CP_ALIAS,
68e9c2fe
EI
3876 .access = PL2_RW, .accessfn = access_el3_aa32ns,
3877 .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) },
3878 { .name = "VTCR_EL2", .state = ARM_CP_STATE_AA64,
3879 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
bf06c112
PM
3880 .access = PL2_RW,
3881 /* no .writefn needed as this can't cause an ASID change;
3882 * no .raw_writefn or .resetfn needed as we never use mask/base_mask
3883 */
68e9c2fe 3884 .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) },
b698e9cf
EI
3885 { .name = "VTTBR", .state = ARM_CP_STATE_AA32,
3886 .cp = 15, .opc1 = 6, .crm = 2,
3887 .type = ARM_CP_64BIT | ARM_CP_ALIAS,
3888 .access = PL2_RW, .accessfn = access_el3_aa32ns,
3889 .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2),
3890 .writefn = vttbr_write },
3891 { .name = "VTTBR_EL2", .state = ARM_CP_STATE_AA64,
3892 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 0,
3893 .access = PL2_RW, .writefn = vttbr_write,
3894 .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2) },
b9cb5323
EI
3895 { .name = "SCTLR_EL2", .state = ARM_CP_STATE_BOTH,
3896 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 0,
3897 .access = PL2_RW, .raw_writefn = raw_write, .writefn = sctlr_write,
3898 .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[2]) },
ff05f37b
EI
3899 { .name = "TPIDR_EL2", .state = ARM_CP_STATE_BOTH,
3900 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 2,
3901 .access = PL2_RW, .resetvalue = 0,
3902 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[2]) },
a57633c0
EI
3903 { .name = "TTBR0_EL2", .state = ARM_CP_STATE_AA64,
3904 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
3905 .access = PL2_RW, .resetvalue = 0,
3906 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
3907 { .name = "HTTBR", .cp = 15, .opc1 = 4, .crm = 2,
3908 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
a57633c0 3909 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
541ef8c2
SS
3910 { .name = "TLBIALLNSNH",
3911 .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
3912 .type = ARM_CP_NO_RAW, .access = PL2_W,
3913 .writefn = tlbiall_nsnh_write },
3914 { .name = "TLBIALLNSNHIS",
3915 .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
3916 .type = ARM_CP_NO_RAW, .access = PL2_W,
3917 .writefn = tlbiall_nsnh_is_write },
3918 { .name = "TLBIALLH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
3919 .type = ARM_CP_NO_RAW, .access = PL2_W,
3920 .writefn = tlbiall_hyp_write },
3921 { .name = "TLBIALLHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
3922 .type = ARM_CP_NO_RAW, .access = PL2_W,
3923 .writefn = tlbiall_hyp_is_write },
3924 { .name = "TLBIMVAH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
3925 .type = ARM_CP_NO_RAW, .access = PL2_W,
3926 .writefn = tlbimva_hyp_write },
3927 { .name = "TLBIMVAHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
3928 .type = ARM_CP_NO_RAW, .access = PL2_W,
3929 .writefn = tlbimva_hyp_is_write },
51da9014
EI
3930 { .name = "TLBI_ALLE2", .state = ARM_CP_STATE_AA64,
3931 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
3932 .type = ARM_CP_NO_RAW, .access = PL2_W,
fd3ed969 3933 .writefn = tlbi_aa64_alle2_write },
8742d49d
EI
3934 { .name = "TLBI_VAE2", .state = ARM_CP_STATE_AA64,
3935 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
3936 .type = ARM_CP_NO_RAW, .access = PL2_W,
fd3ed969 3937 .writefn = tlbi_aa64_vae2_write },
2bfb9d75
PM
3938 { .name = "TLBI_VALE2", .state = ARM_CP_STATE_AA64,
3939 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
3940 .access = PL2_W, .type = ARM_CP_NO_RAW,
3941 .writefn = tlbi_aa64_vae2_write },
3942 { .name = "TLBI_ALLE2IS", .state = ARM_CP_STATE_AA64,
3943 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
3944 .access = PL2_W, .type = ARM_CP_NO_RAW,
3945 .writefn = tlbi_aa64_alle2is_write },
8742d49d
EI
3946 { .name = "TLBI_VAE2IS", .state = ARM_CP_STATE_AA64,
3947 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
3948 .type = ARM_CP_NO_RAW, .access = PL2_W,
fd3ed969 3949 .writefn = tlbi_aa64_vae2is_write },
2bfb9d75
PM
3950 { .name = "TLBI_VALE2IS", .state = ARM_CP_STATE_AA64,
3951 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
3952 .access = PL2_W, .type = ARM_CP_NO_RAW,
3953 .writefn = tlbi_aa64_vae2is_write },
edac4d8a 3954#ifndef CONFIG_USER_ONLY
2a47df95
PM
3955 /* Unlike the other EL2-related AT operations, these must
3956 * UNDEF from EL3 if EL2 is not implemented, which is why we
3957 * define them here rather than with the rest of the AT ops.
3958 */
3959 { .name = "AT_S1E2R", .state = ARM_CP_STATE_AA64,
3960 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
3961 .access = PL2_W, .accessfn = at_s1e2_access,
3962 .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3963 { .name = "AT_S1E2W", .state = ARM_CP_STATE_AA64,
3964 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
3965 .access = PL2_W, .accessfn = at_s1e2_access,
3966 .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
14db7fe0
PM
3967 /* The AArch32 ATS1H* operations are CONSTRAINED UNPREDICTABLE
3968 * if EL2 is not implemented; we choose to UNDEF. Behaviour at EL3
3969 * with SCR.NS == 0 outside Monitor mode is UNPREDICTABLE; we choose
3970 * to behave as if SCR.NS was 1.
3971 */
3972 { .name = "ATS1HR", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
3973 .access = PL2_W,
3974 .writefn = ats1h_write, .type = ARM_CP_NO_RAW },
3975 { .name = "ATS1HW", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
3976 .access = PL2_W,
3977 .writefn = ats1h_write, .type = ARM_CP_NO_RAW },
0b6440af
EI
3978 { .name = "CNTHCTL_EL2", .state = ARM_CP_STATE_BOTH,
3979 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 1, .opc2 = 0,
3980 /* ARMv7 requires bit 0 and 1 to reset to 1. ARMv8 defines the
3981 * reset values as IMPDEF. We choose to reset to 3 to comply with
3982 * both ARMv7 and ARMv8.
3983 */
3984 .access = PL2_RW, .resetvalue = 3,
3985 .fieldoffset = offsetof(CPUARMState, cp15.cnthctl_el2) },
edac4d8a
EI
3986 { .name = "CNTVOFF_EL2", .state = ARM_CP_STATE_AA64,
3987 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 3,
3988 .access = PL2_RW, .type = ARM_CP_IO, .resetvalue = 0,
3989 .writefn = gt_cntvoff_write,
3990 .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
3991 { .name = "CNTVOFF", .cp = 15, .opc1 = 4, .crm = 14,
3992 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS | ARM_CP_IO,
3993 .writefn = gt_cntvoff_write,
3994 .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
b0e66d95
EI
3995 { .name = "CNTHP_CVAL_EL2", .state = ARM_CP_STATE_AA64,
3996 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 2,
3997 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
3998 .type = ARM_CP_IO, .access = PL2_RW,
3999 .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
4000 { .name = "CNTHP_CVAL", .cp = 15, .opc1 = 6, .crm = 14,
4001 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
4002 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_IO,
4003 .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
4004 { .name = "CNTHP_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
4005 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 0,
d44ec156 4006 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
b0e66d95
EI
4007 .resetfn = gt_hyp_timer_reset,
4008 .readfn = gt_hyp_tval_read, .writefn = gt_hyp_tval_write },
4009 { .name = "CNTHP_CTL_EL2", .state = ARM_CP_STATE_BOTH,
4010 .type = ARM_CP_IO,
4011 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 1,
4012 .access = PL2_RW,
4013 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].ctl),
4014 .resetvalue = 0,
4015 .writefn = gt_hyp_ctl_write, .raw_writefn = raw_write },
edac4d8a 4016#endif
14cc7b54
SF
4017 /* The only field of MDCR_EL2 that has a defined architectural reset value
4018 * is MDCR_EL2.HPMN which should reset to the value of PMCR_EL0.N; but we
4019 * don't impelment any PMU event counters, so using zero as a reset
4020 * value for MDCR_EL2 is okay
4021 */
4022 { .name = "MDCR_EL2", .state = ARM_CP_STATE_BOTH,
4023 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 1,
4024 .access = PL2_RW, .resetvalue = 0,
4025 .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el2), },
59e05530
EI
4026 { .name = "HPFAR", .state = ARM_CP_STATE_AA32,
4027 .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
4028 .access = PL2_RW, .accessfn = access_el3_aa32ns,
4029 .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
4030 { .name = "HPFAR_EL2", .state = ARM_CP_STATE_AA64,
4031 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
4032 .access = PL2_RW,
4033 .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
2a5a9abd
AF
4034 { .name = "HSTR_EL2", .state = ARM_CP_STATE_BOTH,
4035 .cp = 15, .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 3,
4036 .access = PL2_RW,
4037 .fieldoffset = offsetof(CPUARMState, cp15.hstr_el2) },
3b685ba7
EI
4038 REGINFO_SENTINEL
4039};
4040
2f027fc5
PM
4041static CPAccessResult nsacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
4042 bool isread)
4043{
4044 /* The NSACR is RW at EL3, and RO for NS EL1 and NS EL2.
4045 * At Secure EL1 it traps to EL3.
4046 */
4047 if (arm_current_el(env) == 3) {
4048 return CP_ACCESS_OK;
4049 }
4050 if (arm_is_secure_below_el3(env)) {
4051 return CP_ACCESS_TRAP_EL3;
4052 }
4053 /* Accesses from EL1 NS and EL2 NS are UNDEF for write but allow reads. */
4054 if (isread) {
4055 return CP_ACCESS_OK;
4056 }
4057 return CP_ACCESS_TRAP_UNCATEGORIZED;
4058}
4059
60fb1a87
GB
4060static const ARMCPRegInfo el3_cp_reginfo[] = {
4061 { .name = "SCR_EL3", .state = ARM_CP_STATE_AA64,
4062 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 0,
4063 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.scr_el3),
4064 .resetvalue = 0, .writefn = scr_write },
7a0e58fa 4065 { .name = "SCR", .type = ARM_CP_ALIAS,
60fb1a87 4066 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 0,
efe4a274
PM
4067 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
4068 .fieldoffset = offsetoflow32(CPUARMState, cp15.scr_el3),
b061a82b 4069 .writefn = scr_write },
60fb1a87
GB
4070 { .name = "SDER32_EL3", .state = ARM_CP_STATE_AA64,
4071 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 1,
4072 .access = PL3_RW, .resetvalue = 0,
4073 .fieldoffset = offsetof(CPUARMState, cp15.sder) },
4074 { .name = "SDER",
4075 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 1,
4076 .access = PL3_RW, .resetvalue = 0,
4077 .fieldoffset = offsetoflow32(CPUARMState, cp15.sder) },
60fb1a87 4078 { .name = "MVBAR", .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
efe4a274
PM
4079 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
4080 .writefn = vbar_write, .resetvalue = 0,
60fb1a87 4081 .fieldoffset = offsetof(CPUARMState, cp15.mvbar) },
7dd8c9af
FA
4082 { .name = "TTBR0_EL3", .state = ARM_CP_STATE_AA64,
4083 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 0,
4084 .access = PL3_RW, .writefn = vmsa_ttbr_write, .resetvalue = 0,
4085 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[3]) },
11f136ee
FA
4086 { .name = "TCR_EL3", .state = ARM_CP_STATE_AA64,
4087 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 2,
6459b94c
PM
4088 .access = PL3_RW,
4089 /* no .writefn needed as this can't cause an ASID change;
811595a2
PM
4090 * we must provide a .raw_writefn and .resetfn because we handle
4091 * reset and migration for the AArch32 TTBCR(S), which might be
4092 * using mask and base_mask.
6459b94c 4093 */
811595a2 4094 .resetfn = vmsa_ttbcr_reset, .raw_writefn = vmsa_ttbcr_raw_write,
11f136ee 4095 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[3]) },
81547d66 4096 { .name = "ELR_EL3", .state = ARM_CP_STATE_AA64,
7a0e58fa 4097 .type = ARM_CP_ALIAS,
81547d66
EI
4098 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 1,
4099 .access = PL3_RW,
4100 .fieldoffset = offsetof(CPUARMState, elr_el[3]) },
f2c30f42 4101 { .name = "ESR_EL3", .state = ARM_CP_STATE_AA64,
f2c30f42
EI
4102 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 2, .opc2 = 0,
4103 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[3]) },
63b60551
EI
4104 { .name = "FAR_EL3", .state = ARM_CP_STATE_AA64,
4105 .opc0 = 3, .opc1 = 6, .crn = 6, .crm = 0, .opc2 = 0,
4106 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[3]) },
81547d66 4107 { .name = "SPSR_EL3", .state = ARM_CP_STATE_AA64,
7a0e58fa 4108 .type = ARM_CP_ALIAS,
81547d66 4109 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 0,
99a99c1f
SB
4110 .access = PL3_RW,
4111 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_MON]) },
a1ba125c
EI
4112 { .name = "VBAR_EL3", .state = ARM_CP_STATE_AA64,
4113 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 0,
4114 .access = PL3_RW, .writefn = vbar_write,
4115 .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[3]),
4116 .resetvalue = 0 },
c6f19164
GB
4117 { .name = "CPTR_EL3", .state = ARM_CP_STATE_AA64,
4118 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 2,
4119 .access = PL3_RW, .accessfn = cptr_access, .resetvalue = 0,
4120 .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[3]) },
4cfb8ad8
PM
4121 { .name = "TPIDR_EL3", .state = ARM_CP_STATE_AA64,
4122 .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 2,
4123 .access = PL3_RW, .resetvalue = 0,
4124 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[3]) },
2179ef95
PM
4125 { .name = "AMAIR_EL3", .state = ARM_CP_STATE_AA64,
4126 .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 3, .opc2 = 0,
4127 .access = PL3_RW, .type = ARM_CP_CONST,
4128 .resetvalue = 0 },
37cd6c24
PM
4129 { .name = "AFSR0_EL3", .state = ARM_CP_STATE_BOTH,
4130 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 0,
4131 .access = PL3_RW, .type = ARM_CP_CONST,
4132 .resetvalue = 0 },
4133 { .name = "AFSR1_EL3", .state = ARM_CP_STATE_BOTH,
4134 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 1,
4135 .access = PL3_RW, .type = ARM_CP_CONST,
4136 .resetvalue = 0 },
43efaa33
PM
4137 { .name = "TLBI_ALLE3IS", .state = ARM_CP_STATE_AA64,
4138 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 0,
4139 .access = PL3_W, .type = ARM_CP_NO_RAW,
4140 .writefn = tlbi_aa64_alle3is_write },
4141 { .name = "TLBI_VAE3IS", .state = ARM_CP_STATE_AA64,
4142 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 1,
4143 .access = PL3_W, .type = ARM_CP_NO_RAW,
4144 .writefn = tlbi_aa64_vae3is_write },
4145 { .name = "TLBI_VALE3IS", .state = ARM_CP_STATE_AA64,
4146 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 5,
4147 .access = PL3_W, .type = ARM_CP_NO_RAW,
4148 .writefn = tlbi_aa64_vae3is_write },
4149 { .name = "TLBI_ALLE3", .state = ARM_CP_STATE_AA64,
4150 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 0,
4151 .access = PL3_W, .type = ARM_CP_NO_RAW,
4152 .writefn = tlbi_aa64_alle3_write },
4153 { .name = "TLBI_VAE3", .state = ARM_CP_STATE_AA64,
4154 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 1,
4155 .access = PL3_W, .type = ARM_CP_NO_RAW,
4156 .writefn = tlbi_aa64_vae3_write },
4157 { .name = "TLBI_VALE3", .state = ARM_CP_STATE_AA64,
4158 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 5,
4159 .access = PL3_W, .type = ARM_CP_NO_RAW,
4160 .writefn = tlbi_aa64_vae3_write },
0f1a3b24
FA
4161 REGINFO_SENTINEL
4162};
4163
3f208fd7
PM
4164static CPAccessResult ctr_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
4165 bool isread)
7da845b0
PM
4166{
4167 /* Only accessible in EL0 if SCTLR.UCT is set (and only in AArch64,
4168 * but the AArch32 CTR has its own reginfo struct)
4169 */
137feaa9 4170 if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_UCT)) {
7da845b0
PM
4171 return CP_ACCESS_TRAP;
4172 }
4173 return CP_ACCESS_OK;
4174}
4175
1424ca8d
DM
4176static void oslar_write(CPUARMState *env, const ARMCPRegInfo *ri,
4177 uint64_t value)
4178{
4179 /* Writes to OSLAR_EL1 may update the OS lock status, which can be
4180 * read via a bit in OSLSR_EL1.
4181 */
4182 int oslock;
4183
4184 if (ri->state == ARM_CP_STATE_AA32) {
4185 oslock = (value == 0xC5ACCE55);
4186 } else {
4187 oslock = value & 1;
4188 }
4189
4190 env->cp15.oslsr_el1 = deposit32(env->cp15.oslsr_el1, 1, 1, oslock);
4191}
4192
50300698 4193static const ARMCPRegInfo debug_cp_reginfo[] = {
50300698 4194 /* DBGDRAR, DBGDSAR: always RAZ since we don't implement memory mapped
10aae104
PM
4195 * debug components. The AArch64 version of DBGDRAR is named MDRAR_EL1;
4196 * unlike DBGDRAR it is never accessible from EL0.
4197 * DBGDSAR is deprecated and must RAZ from v8 anyway, so it has no AArch64
4198 * accessor.
50300698
PM
4199 */
4200 { .name = "DBGDRAR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 0,
91b0a238
PM
4201 .access = PL0_R, .accessfn = access_tdra,
4202 .type = ARM_CP_CONST, .resetvalue = 0 },
10aae104
PM
4203 { .name = "MDRAR_EL1", .state = ARM_CP_STATE_AA64,
4204 .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
91b0a238
PM
4205 .access = PL1_R, .accessfn = access_tdra,
4206 .type = ARM_CP_CONST, .resetvalue = 0 },
50300698 4207 { .name = "DBGDSAR", .cp = 14, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
91b0a238
PM
4208 .access = PL0_R, .accessfn = access_tdra,
4209 .type = ARM_CP_CONST, .resetvalue = 0 },
17a9eb53 4210 /* Monitor debug system control register; the 32-bit alias is DBGDSCRext. */
10aae104
PM
4211 { .name = "MDSCR_EL1", .state = ARM_CP_STATE_BOTH,
4212 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
d6c8cf81 4213 .access = PL1_RW, .accessfn = access_tda,
0e5e8935
PM
4214 .fieldoffset = offsetof(CPUARMState, cp15.mdscr_el1),
4215 .resetvalue = 0 },
5e8b12ff
PM
4216 /* MDCCSR_EL0, aka DBGDSCRint. This is a read-only mirror of MDSCR_EL1.
4217 * We don't implement the configurable EL0 access.
4218 */
4219 { .name = "MDCCSR_EL0", .state = ARM_CP_STATE_BOTH,
4220 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
7a0e58fa 4221 .type = ARM_CP_ALIAS,
d6c8cf81 4222 .access = PL1_R, .accessfn = access_tda,
b061a82b 4223 .fieldoffset = offsetof(CPUARMState, cp15.mdscr_el1), },
10aae104
PM
4224 { .name = "OSLAR_EL1", .state = ARM_CP_STATE_BOTH,
4225 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 4,
1424ca8d 4226 .access = PL1_W, .type = ARM_CP_NO_RAW,
187f678d 4227 .accessfn = access_tdosa,
1424ca8d
DM
4228 .writefn = oslar_write },
4229 { .name = "OSLSR_EL1", .state = ARM_CP_STATE_BOTH,
4230 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 4,
4231 .access = PL1_R, .resetvalue = 10,
187f678d 4232 .accessfn = access_tdosa,
1424ca8d 4233 .fieldoffset = offsetof(CPUARMState, cp15.oslsr_el1) },
5e8b12ff
PM
4234 /* Dummy OSDLR_EL1: 32-bit Linux will read this */
4235 { .name = "OSDLR_EL1", .state = ARM_CP_STATE_BOTH,
4236 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 4,
187f678d
PM
4237 .access = PL1_RW, .accessfn = access_tdosa,
4238 .type = ARM_CP_NOP },
5e8b12ff
PM
4239 /* Dummy DBGVCR: Linux wants to clear this on startup, but we don't
4240 * implement vector catch debug events yet.
4241 */
4242 { .name = "DBGVCR",
4243 .cp = 14, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
d6c8cf81
PM
4244 .access = PL1_RW, .accessfn = access_tda,
4245 .type = ARM_CP_NOP },
4d2ec4da
PM
4246 /* Dummy DBGVCR32_EL2 (which is only for a 64-bit hypervisor
4247 * to save and restore a 32-bit guest's DBGVCR)
4248 */
4249 { .name = "DBGVCR32_EL2", .state = ARM_CP_STATE_AA64,
4250 .opc0 = 2, .opc1 = 4, .crn = 0, .crm = 7, .opc2 = 0,
4251 .access = PL2_RW, .accessfn = access_tda,
4252 .type = ARM_CP_NOP },
5dbdc434
PM
4253 /* Dummy MDCCINT_EL1, since we don't implement the Debug Communications
4254 * Channel but Linux may try to access this register. The 32-bit
4255 * alias is DBGDCCINT.
4256 */
4257 { .name = "MDCCINT_EL1", .state = ARM_CP_STATE_BOTH,
4258 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0,
4259 .access = PL1_RW, .accessfn = access_tda,
4260 .type = ARM_CP_NOP },
50300698
PM
4261 REGINFO_SENTINEL
4262};
4263
4264static const ARMCPRegInfo debug_lpae_cp_reginfo[] = {
4265 /* 64 bit access versions of the (dummy) debug registers */
4266 { .name = "DBGDRAR", .cp = 14, .crm = 1, .opc1 = 0,
4267 .access = PL0_R, .type = ARM_CP_CONST|ARM_CP_64BIT, .resetvalue = 0 },
4268 { .name = "DBGDSAR", .cp = 14, .crm = 2, .opc1 = 0,
4269 .access = PL0_R, .type = ARM_CP_CONST|ARM_CP_64BIT, .resetvalue = 0 },
4270 REGINFO_SENTINEL
4271};
4272
5be5e8ed
RH
4273/* Return the exception level to which SVE-disabled exceptions should
4274 * be taken, or 0 if SVE is enabled.
4275 */
4276static int sve_exception_el(CPUARMState *env)
4277{
4278#ifndef CONFIG_USER_ONLY
4279 unsigned current_el = arm_current_el(env);
4280
4281 /* The CPACR.ZEN controls traps to EL1:
4282 * 0, 2 : trap EL0 and EL1 accesses
4283 * 1 : trap only EL0 accesses
4284 * 3 : trap no accesses
4285 */
4286 switch (extract32(env->cp15.cpacr_el1, 16, 2)) {
4287 default:
4288 if (current_el <= 1) {
4289 /* Trap to PL1, which might be EL1 or EL3 */
4290 if (arm_is_secure(env) && !arm_el_is_aa64(env, 3)) {
4291 return 3;
4292 }
4293 return 1;
4294 }
4295 break;
4296 case 1:
4297 if (current_el == 0) {
4298 return 1;
4299 }
4300 break;
4301 case 3:
4302 break;
4303 }
4304
4305 /* Similarly for CPACR.FPEN, after having checked ZEN. */
4306 switch (extract32(env->cp15.cpacr_el1, 20, 2)) {
4307 default:
4308 if (current_el <= 1) {
4309 if (arm_is_secure(env) && !arm_el_is_aa64(env, 3)) {
4310 return 3;
4311 }
4312 return 1;
4313 }
4314 break;
4315 case 1:
4316 if (current_el == 0) {
4317 return 1;
4318 }
4319 break;
4320 case 3:
4321 break;
4322 }
4323
4324 /* CPTR_EL2. Check both TZ and TFP. */
4325 if (current_el <= 2
4326 && (env->cp15.cptr_el[2] & (CPTR_TFP | CPTR_TZ))
4327 && !arm_is_secure_below_el3(env)) {
4328 return 2;
4329 }
4330
4331 /* CPTR_EL3. Check both EZ and TFP. */
4332 if (!(env->cp15.cptr_el[3] & CPTR_EZ)
4333 || (env->cp15.cptr_el[3] & CPTR_TFP)) {
4334 return 3;
4335 }
4336#endif
4337 return 0;
4338}
4339
5be5e8ed
RH
4340static void zcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4341 uint64_t value)
4342{
4343 /* Bits other than [3:0] are RAZ/WI. */
4344 raw_write(env, ri, value & 0xf);
4345}
4346
4347static const ARMCPRegInfo zcr_el1_reginfo = {
4348 .name = "ZCR_EL1", .state = ARM_CP_STATE_AA64,
4349 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 0,
490aa7f1 4350 .access = PL1_RW, .type = ARM_CP_SVE | ARM_CP_FPU,
5be5e8ed
RH
4351 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[1]),
4352 .writefn = zcr_write, .raw_writefn = raw_write
4353};
4354
4355static const ARMCPRegInfo zcr_el2_reginfo = {
4356 .name = "ZCR_EL2", .state = ARM_CP_STATE_AA64,
4357 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 0,
490aa7f1 4358 .access = PL2_RW, .type = ARM_CP_SVE | ARM_CP_FPU,
5be5e8ed
RH
4359 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[2]),
4360 .writefn = zcr_write, .raw_writefn = raw_write
4361};
4362
4363static const ARMCPRegInfo zcr_no_el2_reginfo = {
4364 .name = "ZCR_EL2", .state = ARM_CP_STATE_AA64,
4365 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 0,
490aa7f1 4366 .access = PL2_RW, .type = ARM_CP_SVE | ARM_CP_FPU,
5be5e8ed
RH
4367 .readfn = arm_cp_read_zero, .writefn = arm_cp_write_ignore
4368};
4369
4370static const ARMCPRegInfo zcr_el3_reginfo = {
4371 .name = "ZCR_EL3", .state = ARM_CP_STATE_AA64,
4372 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 2, .opc2 = 0,
490aa7f1 4373 .access = PL3_RW, .type = ARM_CP_SVE | ARM_CP_FPU,
5be5e8ed
RH
4374 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[3]),
4375 .writefn = zcr_write, .raw_writefn = raw_write
4376};
4377
9ee98ce8
PM
4378void hw_watchpoint_update(ARMCPU *cpu, int n)
4379{
4380 CPUARMState *env = &cpu->env;
4381 vaddr len = 0;
4382 vaddr wvr = env->cp15.dbgwvr[n];
4383 uint64_t wcr = env->cp15.dbgwcr[n];
4384 int mask;
4385 int flags = BP_CPU | BP_STOP_BEFORE_ACCESS;
4386
4387 if (env->cpu_watchpoint[n]) {
4388 cpu_watchpoint_remove_by_ref(CPU(cpu), env->cpu_watchpoint[n]);
4389 env->cpu_watchpoint[n] = NULL;
4390 }
4391
4392 if (!extract64(wcr, 0, 1)) {
4393 /* E bit clear : watchpoint disabled */
4394 return;
4395 }
4396
4397 switch (extract64(wcr, 3, 2)) {
4398 case 0:
4399 /* LSC 00 is reserved and must behave as if the wp is disabled */
4400 return;
4401 case 1:
4402 flags |= BP_MEM_READ;
4403 break;
4404 case 2:
4405 flags |= BP_MEM_WRITE;
4406 break;
4407 case 3:
4408 flags |= BP_MEM_ACCESS;
4409 break;
4410 }
4411
4412 /* Attempts to use both MASK and BAS fields simultaneously are
4413 * CONSTRAINED UNPREDICTABLE; we opt to ignore BAS in this case,
4414 * thus generating a watchpoint for every byte in the masked region.
4415 */
4416 mask = extract64(wcr, 24, 4);
4417 if (mask == 1 || mask == 2) {
4418 /* Reserved values of MASK; we must act as if the mask value was
4419 * some non-reserved value, or as if the watchpoint were disabled.
4420 * We choose the latter.
4421 */
4422 return;
4423 } else if (mask) {
4424 /* Watchpoint covers an aligned area up to 2GB in size */
4425 len = 1ULL << mask;
4426 /* If masked bits in WVR are not zero it's CONSTRAINED UNPREDICTABLE
4427 * whether the watchpoint fires when the unmasked bits match; we opt
4428 * to generate the exceptions.
4429 */
4430 wvr &= ~(len - 1);
4431 } else {
4432 /* Watchpoint covers bytes defined by the byte address select bits */
4433 int bas = extract64(wcr, 5, 8);
4434 int basstart;
4435
4436 if (bas == 0) {
4437 /* This must act as if the watchpoint is disabled */
4438 return;
4439 }
4440
4441 if (extract64(wvr, 2, 1)) {
4442 /* Deprecated case of an only 4-aligned address. BAS[7:4] are
4443 * ignored, and BAS[3:0] define which bytes to watch.
4444 */
4445 bas &= 0xf;
4446 }
4447 /* The BAS bits are supposed to be programmed to indicate a contiguous
4448 * range of bytes. Otherwise it is CONSTRAINED UNPREDICTABLE whether
4449 * we fire for each byte in the word/doubleword addressed by the WVR.
4450 * We choose to ignore any non-zero bits after the first range of 1s.
4451 */
4452 basstart = ctz32(bas);
4453 len = cto32(bas >> basstart);
4454 wvr += basstart;
4455 }
4456
4457 cpu_watchpoint_insert(CPU(cpu), wvr, len, flags,
4458 &env->cpu_watchpoint[n]);
4459}
4460
4461void hw_watchpoint_update_all(ARMCPU *cpu)
4462{
4463 int i;
4464 CPUARMState *env = &cpu->env;
4465
4466 /* Completely clear out existing QEMU watchpoints and our array, to
4467 * avoid possible stale entries following migration load.
4468 */
4469 cpu_watchpoint_remove_all(CPU(cpu), BP_CPU);
4470 memset(env->cpu_watchpoint, 0, sizeof(env->cpu_watchpoint));
4471
4472 for (i = 0; i < ARRAY_SIZE(cpu->env.cpu_watchpoint); i++) {
4473 hw_watchpoint_update(cpu, i);
4474 }
4475}
4476
4477static void dbgwvr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4478 uint64_t value)
4479{
4480 ARMCPU *cpu = arm_env_get_cpu(env);
4481 int i = ri->crm;
4482
4483 /* Bits [63:49] are hardwired to the value of bit [48]; that is, the
4484 * register reads and behaves as if values written are sign extended.
4485 * Bits [1:0] are RES0.
4486 */
4487 value = sextract64(value, 0, 49) & ~3ULL;
4488
4489 raw_write(env, ri, value);
4490 hw_watchpoint_update(cpu, i);
4491}
4492
4493static void dbgwcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4494 uint64_t value)
4495{
4496 ARMCPU *cpu = arm_env_get_cpu(env);
4497 int i = ri->crm;
4498
4499 raw_write(env, ri, value);
4500 hw_watchpoint_update(cpu, i);
4501}
4502
46747d15
PM
4503void hw_breakpoint_update(ARMCPU *cpu, int n)
4504{
4505 CPUARMState *env = &cpu->env;
4506 uint64_t bvr = env->cp15.dbgbvr[n];
4507 uint64_t bcr = env->cp15.dbgbcr[n];
4508 vaddr addr;
4509 int bt;
4510 int flags = BP_CPU;
4511
4512 if (env->cpu_breakpoint[n]) {
4513 cpu_breakpoint_remove_by_ref(CPU(cpu), env->cpu_breakpoint[n]);
4514 env->cpu_breakpoint[n] = NULL;
4515 }
4516
4517 if (!extract64(bcr, 0, 1)) {
4518 /* E bit clear : watchpoint disabled */
4519 return;
4520 }
4521
4522 bt = extract64(bcr, 20, 4);
4523
4524 switch (bt) {
4525 case 4: /* unlinked address mismatch (reserved if AArch64) */
4526 case 5: /* linked address mismatch (reserved if AArch64) */
4527 qemu_log_mask(LOG_UNIMP,
4528 "arm: address mismatch breakpoint types not implemented");
4529 return;
4530 case 0: /* unlinked address match */
4531 case 1: /* linked address match */
4532 {
4533 /* Bits [63:49] are hardwired to the value of bit [48]; that is,
4534 * we behave as if the register was sign extended. Bits [1:0] are
4535 * RES0. The BAS field is used to allow setting breakpoints on 16
4536 * bit wide instructions; it is CONSTRAINED UNPREDICTABLE whether
4537 * a bp will fire if the addresses covered by the bp and the addresses
4538 * covered by the insn overlap but the insn doesn't start at the
4539 * start of the bp address range. We choose to require the insn and
4540 * the bp to have the same address. The constraints on writing to
4541 * BAS enforced in dbgbcr_write mean we have only four cases:
4542 * 0b0000 => no breakpoint
4543 * 0b0011 => breakpoint on addr
4544 * 0b1100 => breakpoint on addr + 2
4545 * 0b1111 => breakpoint on addr
4546 * See also figure D2-3 in the v8 ARM ARM (DDI0487A.c).
4547 */
4548 int bas = extract64(bcr, 5, 4);
4549 addr = sextract64(bvr, 0, 49) & ~3ULL;
4550 if (bas == 0) {
4551 return;
4552 }
4553 if (bas == 0xc) {
4554 addr += 2;
4555 }
4556 break;
4557 }
4558 case 2: /* unlinked context ID match */
4559 case 8: /* unlinked VMID match (reserved if no EL2) */
4560 case 10: /* unlinked context ID and VMID match (reserved if no EL2) */
4561 qemu_log_mask(LOG_UNIMP,
4562 "arm: unlinked context breakpoint types not implemented");
4563 return;
4564 case 9: /* linked VMID match (reserved if no EL2) */
4565 case 11: /* linked context ID and VMID match (reserved if no EL2) */
4566 case 3: /* linked context ID match */
4567 default:
4568 /* We must generate no events for Linked context matches (unless
4569 * they are linked to by some other bp/wp, which is handled in
4570 * updates for the linking bp/wp). We choose to also generate no events
4571 * for reserved values.
4572 */
4573 return;
4574 }
4575
4576 cpu_breakpoint_insert(CPU(cpu), addr, flags, &env->cpu_breakpoint[n]);
4577}
4578
4579void hw_breakpoint_update_all(ARMCPU *cpu)
4580{
4581 int i;
4582 CPUARMState *env = &cpu->env;
4583
4584 /* Completely clear out existing QEMU breakpoints and our array, to
4585 * avoid possible stale entries following migration load.
4586 */
4587 cpu_breakpoint_remove_all(CPU(cpu), BP_CPU);
4588 memset(env->cpu_breakpoint, 0, sizeof(env->cpu_breakpoint));
4589
4590 for (i = 0; i < ARRAY_SIZE(cpu->env.cpu_breakpoint); i++) {
4591 hw_breakpoint_update(cpu, i);
4592 }
4593}
4594
4595static void dbgbvr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4596 uint64_t value)
4597{
4598 ARMCPU *cpu = arm_env_get_cpu(env);
4599 int i = ri->crm;
4600
4601 raw_write(env, ri, value);
4602 hw_breakpoint_update(cpu, i);
4603}
4604
4605static void dbgbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4606 uint64_t value)
4607{
4608 ARMCPU *cpu = arm_env_get_cpu(env);
4609 int i = ri->crm;
4610
4611 /* BAS[3] is a read-only copy of BAS[2], and BAS[1] a read-only
4612 * copy of BAS[0].
4613 */
4614 value = deposit64(value, 6, 1, extract64(value, 5, 1));
4615 value = deposit64(value, 8, 1, extract64(value, 7, 1));
4616
4617 raw_write(env, ri, value);
4618 hw_breakpoint_update(cpu, i);
4619}
4620
50300698 4621static void define_debug_regs(ARMCPU *cpu)
0b45451e 4622{
50300698
PM
4623 /* Define v7 and v8 architectural debug registers.
4624 * These are just dummy implementations for now.
0b45451e
PM
4625 */
4626 int i;
3ff6fc91 4627 int wrps, brps, ctx_cmps;
48eb3ae6
PM
4628 ARMCPRegInfo dbgdidr = {
4629 .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0,
d6c8cf81
PM
4630 .access = PL0_R, .accessfn = access_tda,
4631 .type = ARM_CP_CONST, .resetvalue = cpu->dbgdidr,
48eb3ae6
PM
4632 };
4633
3ff6fc91 4634 /* Note that all these register fields hold "number of Xs minus 1". */
48eb3ae6
PM
4635 brps = extract32(cpu->dbgdidr, 24, 4);
4636 wrps = extract32(cpu->dbgdidr, 28, 4);
3ff6fc91
PM
4637 ctx_cmps = extract32(cpu->dbgdidr, 20, 4);
4638
4639 assert(ctx_cmps <= brps);
48eb3ae6
PM
4640
4641 /* The DBGDIDR and ID_AA64DFR0_EL1 define various properties
4642 * of the debug registers such as number of breakpoints;
4643 * check that if they both exist then they agree.
4644 */
4645 if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64)) {
4646 assert(extract32(cpu->id_aa64dfr0, 12, 4) == brps);
4647 assert(extract32(cpu->id_aa64dfr0, 20, 4) == wrps);
3ff6fc91 4648 assert(extract32(cpu->id_aa64dfr0, 28, 4) == ctx_cmps);
48eb3ae6 4649 }
0b45451e 4650
48eb3ae6 4651 define_one_arm_cp_reg(cpu, &dbgdidr);
50300698
PM
4652 define_arm_cp_regs(cpu, debug_cp_reginfo);
4653
4654 if (arm_feature(&cpu->env, ARM_FEATURE_LPAE)) {
4655 define_arm_cp_regs(cpu, debug_lpae_cp_reginfo);
4656 }
4657
48eb3ae6 4658 for (i = 0; i < brps + 1; i++) {
0b45451e 4659 ARMCPRegInfo dbgregs[] = {
10aae104
PM
4660 { .name = "DBGBVR", .state = ARM_CP_STATE_BOTH,
4661 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 4,
d6c8cf81 4662 .access = PL1_RW, .accessfn = access_tda,
46747d15
PM
4663 .fieldoffset = offsetof(CPUARMState, cp15.dbgbvr[i]),
4664 .writefn = dbgbvr_write, .raw_writefn = raw_write
4665 },
10aae104
PM
4666 { .name = "DBGBCR", .state = ARM_CP_STATE_BOTH,
4667 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 5,
d6c8cf81 4668 .access = PL1_RW, .accessfn = access_tda,
46747d15
PM
4669 .fieldoffset = offsetof(CPUARMState, cp15.dbgbcr[i]),
4670 .writefn = dbgbcr_write, .raw_writefn = raw_write
4671 },
48eb3ae6
PM
4672 REGINFO_SENTINEL
4673 };
4674 define_arm_cp_regs(cpu, dbgregs);
4675 }
4676
4677 for (i = 0; i < wrps + 1; i++) {
4678 ARMCPRegInfo dbgregs[] = {
10aae104
PM
4679 { .name = "DBGWVR", .state = ARM_CP_STATE_BOTH,
4680 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 6,
d6c8cf81 4681 .access = PL1_RW, .accessfn = access_tda,
9ee98ce8
PM
4682 .fieldoffset = offsetof(CPUARMState, cp15.dbgwvr[i]),
4683 .writefn = dbgwvr_write, .raw_writefn = raw_write
4684 },
10aae104
PM
4685 { .name = "DBGWCR", .state = ARM_CP_STATE_BOTH,
4686 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 7,
d6c8cf81 4687 .access = PL1_RW, .accessfn = access_tda,
9ee98ce8
PM
4688 .fieldoffset = offsetof(CPUARMState, cp15.dbgwcr[i]),
4689 .writefn = dbgwcr_write, .raw_writefn = raw_write
4690 },
4691 REGINFO_SENTINEL
0b45451e
PM
4692 };
4693 define_arm_cp_regs(cpu, dbgregs);
4694 }
4695}
4696
96a8b92e
PM
4697/* We don't know until after realize whether there's a GICv3
4698 * attached, and that is what registers the gicv3 sysregs.
4699 * So we have to fill in the GIC fields in ID_PFR/ID_PFR1_EL1/ID_AA64PFR0_EL1
4700 * at runtime.
4701 */
4702static uint64_t id_pfr1_read(CPUARMState *env, const ARMCPRegInfo *ri)
4703{
4704 ARMCPU *cpu = arm_env_get_cpu(env);
4705 uint64_t pfr1 = cpu->id_pfr1;
4706
4707 if (env->gicv3state) {
4708 pfr1 |= 1 << 28;
4709 }
4710 return pfr1;
4711}
4712
4713static uint64_t id_aa64pfr0_read(CPUARMState *env, const ARMCPRegInfo *ri)
4714{
4715 ARMCPU *cpu = arm_env_get_cpu(env);
4716 uint64_t pfr0 = cpu->id_aa64pfr0;
4717
4718 if (env->gicv3state) {
4719 pfr0 |= 1 << 24;
4720 }
4721 return pfr0;
4722}
4723
2ceb98c0
PM
4724void register_cp_regs_for_features(ARMCPU *cpu)
4725{
4726 /* Register all the coprocessor registers based on feature bits */
4727 CPUARMState *env = &cpu->env;
4728 if (arm_feature(env, ARM_FEATURE_M)) {
4729 /* M profile has no coprocessor registers */
4730 return;
4731 }
4732
e9aa6c21 4733 define_arm_cp_regs(cpu, cp_reginfo);
9449fdf6
PM
4734 if (!arm_feature(env, ARM_FEATURE_V8)) {
4735 /* Must go early as it is full of wildcards that may be
4736 * overridden by later definitions.
4737 */
4738 define_arm_cp_regs(cpu, not_v8_cp_reginfo);
4739 }
4740
7d57f408 4741 if (arm_feature(env, ARM_FEATURE_V6)) {
8515a092
PM
4742 /* The ID registers all have impdef reset values */
4743 ARMCPRegInfo v6_idregs[] = {
0ff644a7
PM
4744 { .name = "ID_PFR0", .state = ARM_CP_STATE_BOTH,
4745 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
4746 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 4747 .resetvalue = cpu->id_pfr0 },
96a8b92e
PM
4748 /* ID_PFR1 is not a plain ARM_CP_CONST because we don't know
4749 * the value of the GIC field until after we define these regs.
4750 */
0ff644a7
PM
4751 { .name = "ID_PFR1", .state = ARM_CP_STATE_BOTH,
4752 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 1,
96a8b92e
PM
4753 .access = PL1_R, .type = ARM_CP_NO_RAW,
4754 .readfn = id_pfr1_read,
4755 .writefn = arm_cp_write_ignore },
0ff644a7
PM
4756 { .name = "ID_DFR0", .state = ARM_CP_STATE_BOTH,
4757 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 2,
4758 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 4759 .resetvalue = cpu->id_dfr0 },
0ff644a7
PM
4760 { .name = "ID_AFR0", .state = ARM_CP_STATE_BOTH,
4761 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 3,
4762 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 4763 .resetvalue = cpu->id_afr0 },
0ff644a7
PM
4764 { .name = "ID_MMFR0", .state = ARM_CP_STATE_BOTH,
4765 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 4,
4766 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 4767 .resetvalue = cpu->id_mmfr0 },
0ff644a7
PM
4768 { .name = "ID_MMFR1", .state = ARM_CP_STATE_BOTH,
4769 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 5,
4770 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 4771 .resetvalue = cpu->id_mmfr1 },
0ff644a7
PM
4772 { .name = "ID_MMFR2", .state = ARM_CP_STATE_BOTH,
4773 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 6,
4774 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 4775 .resetvalue = cpu->id_mmfr2 },
0ff644a7
PM
4776 { .name = "ID_MMFR3", .state = ARM_CP_STATE_BOTH,
4777 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 7,
4778 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 4779 .resetvalue = cpu->id_mmfr3 },
0ff644a7
PM
4780 { .name = "ID_ISAR0", .state = ARM_CP_STATE_BOTH,
4781 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0,
4782 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 4783 .resetvalue = cpu->id_isar0 },
0ff644a7
PM
4784 { .name = "ID_ISAR1", .state = ARM_CP_STATE_BOTH,
4785 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 1,
4786 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 4787 .resetvalue = cpu->id_isar1 },
0ff644a7
PM
4788 { .name = "ID_ISAR2", .state = ARM_CP_STATE_BOTH,
4789 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
4790 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 4791 .resetvalue = cpu->id_isar2 },
0ff644a7
PM
4792 { .name = "ID_ISAR3", .state = ARM_CP_STATE_BOTH,
4793 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 3,
4794 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 4795 .resetvalue = cpu->id_isar3 },
0ff644a7
PM
4796 { .name = "ID_ISAR4", .state = ARM_CP_STATE_BOTH,
4797 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 4,
4798 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 4799 .resetvalue = cpu->id_isar4 },
0ff644a7
PM
4800 { .name = "ID_ISAR5", .state = ARM_CP_STATE_BOTH,
4801 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 5,
4802 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 4803 .resetvalue = cpu->id_isar5 },
e20d84c1
PM
4804 { .name = "ID_MMFR4", .state = ARM_CP_STATE_BOTH,
4805 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 6,
4806 .access = PL1_R, .type = ARM_CP_CONST,
4807 .resetvalue = cpu->id_mmfr4 },
4808 /* 7 is as yet unallocated and must RAZ */
4809 { .name = "ID_ISAR7_RESERVED", .state = ARM_CP_STATE_BOTH,
4810 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 7,
4811 .access = PL1_R, .type = ARM_CP_CONST,
8515a092
PM
4812 .resetvalue = 0 },
4813 REGINFO_SENTINEL
4814 };
4815 define_arm_cp_regs(cpu, v6_idregs);
7d57f408
PM
4816 define_arm_cp_regs(cpu, v6_cp_reginfo);
4817 } else {
4818 define_arm_cp_regs(cpu, not_v6_cp_reginfo);
4819 }
4d31c596
PM
4820 if (arm_feature(env, ARM_FEATURE_V6K)) {
4821 define_arm_cp_regs(cpu, v6k_cp_reginfo);
4822 }
5e5cf9e3 4823 if (arm_feature(env, ARM_FEATURE_V7MP) &&
452a0955 4824 !arm_feature(env, ARM_FEATURE_PMSA)) {
995939a6
PM
4825 define_arm_cp_regs(cpu, v7mp_cp_reginfo);
4826 }
e9aa6c21 4827 if (arm_feature(env, ARM_FEATURE_V7)) {
200ac0ef 4828 /* v7 performance monitor control register: same implementor
7c2cb42b
AF
4829 * field as main ID register, and we implement only the cycle
4830 * count register.
200ac0ef 4831 */
7c2cb42b 4832#ifndef CONFIG_USER_ONLY
200ac0ef
PM
4833 ARMCPRegInfo pmcr = {
4834 .name = "PMCR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 0,
8521466b 4835 .access = PL0_RW,
7a0e58fa 4836 .type = ARM_CP_IO | ARM_CP_ALIAS,
8521466b 4837 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcr),
fcd25206
PM
4838 .accessfn = pmreg_access, .writefn = pmcr_write,
4839 .raw_writefn = raw_write,
200ac0ef 4840 };
8521466b
AF
4841 ARMCPRegInfo pmcr64 = {
4842 .name = "PMCR_EL0", .state = ARM_CP_STATE_AA64,
4843 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 0,
4844 .access = PL0_RW, .accessfn = pmreg_access,
4845 .type = ARM_CP_IO,
4846 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcr),
4847 .resetvalue = cpu->midr & 0xff000000,
4848 .writefn = pmcr_write, .raw_writefn = raw_write,
4849 };
7c2cb42b 4850 define_one_arm_cp_reg(cpu, &pmcr);
8521466b 4851 define_one_arm_cp_reg(cpu, &pmcr64);
7c2cb42b 4852#endif
776d4e5c 4853 ARMCPRegInfo clidr = {
7da845b0
PM
4854 .name = "CLIDR", .state = ARM_CP_STATE_BOTH,
4855 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 1,
776d4e5c
PM
4856 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->clidr
4857 };
776d4e5c 4858 define_one_arm_cp_reg(cpu, &clidr);
e9aa6c21 4859 define_arm_cp_regs(cpu, v7_cp_reginfo);
50300698 4860 define_debug_regs(cpu);
7d57f408
PM
4861 } else {
4862 define_arm_cp_regs(cpu, not_v7_cp_reginfo);
e9aa6c21 4863 }
b0d2b7d0 4864 if (arm_feature(env, ARM_FEATURE_V8)) {
e20d84c1
PM
4865 /* AArch64 ID registers, which all have impdef reset values.
4866 * Note that within the ID register ranges the unused slots
4867 * must all RAZ, not UNDEF; future architecture versions may
4868 * define new registers here.
4869 */
e60cef86 4870 ARMCPRegInfo v8_idregs[] = {
96a8b92e
PM
4871 /* ID_AA64PFR0_EL1 is not a plain ARM_CP_CONST because we don't
4872 * know the right value for the GIC field until after we
4873 * define these regs.
4874 */
e60cef86
PM
4875 { .name = "ID_AA64PFR0_EL1", .state = ARM_CP_STATE_AA64,
4876 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 0,
96a8b92e
PM
4877 .access = PL1_R, .type = ARM_CP_NO_RAW,
4878 .readfn = id_aa64pfr0_read,
4879 .writefn = arm_cp_write_ignore },
e60cef86
PM
4880 { .name = "ID_AA64PFR1_EL1", .state = ARM_CP_STATE_AA64,
4881 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 1,
4882 .access = PL1_R, .type = ARM_CP_CONST,
4883 .resetvalue = cpu->id_aa64pfr1},
e20d84c1
PM
4884 { .name = "ID_AA64PFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4885 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 2,
4886 .access = PL1_R, .type = ARM_CP_CONST,
4887 .resetvalue = 0 },
4888 { .name = "ID_AA64PFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4889 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 3,
4890 .access = PL1_R, .type = ARM_CP_CONST,
4891 .resetvalue = 0 },
4892 { .name = "ID_AA64PFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4893 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 4,
4894 .access = PL1_R, .type = ARM_CP_CONST,
4895 .resetvalue = 0 },
4896 { .name = "ID_AA64PFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4897 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 5,
4898 .access = PL1_R, .type = ARM_CP_CONST,
4899 .resetvalue = 0 },
4900 { .name = "ID_AA64PFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4901 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 6,
4902 .access = PL1_R, .type = ARM_CP_CONST,
4903 .resetvalue = 0 },
4904 { .name = "ID_AA64PFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4905 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 7,
4906 .access = PL1_R, .type = ARM_CP_CONST,
4907 .resetvalue = 0 },
e60cef86
PM
4908 { .name = "ID_AA64DFR0_EL1", .state = ARM_CP_STATE_AA64,
4909 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 0,
4910 .access = PL1_R, .type = ARM_CP_CONST,
d6f02ce3 4911 .resetvalue = cpu->id_aa64dfr0 },
e60cef86
PM
4912 { .name = "ID_AA64DFR1_EL1", .state = ARM_CP_STATE_AA64,
4913 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 1,
4914 .access = PL1_R, .type = ARM_CP_CONST,
4915 .resetvalue = cpu->id_aa64dfr1 },
e20d84c1
PM
4916 { .name = "ID_AA64DFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4917 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 2,
4918 .access = PL1_R, .type = ARM_CP_CONST,
4919 .resetvalue = 0 },
4920 { .name = "ID_AA64DFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4921 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 3,
4922 .access = PL1_R, .type = ARM_CP_CONST,
4923 .resetvalue = 0 },
e60cef86
PM
4924 { .name = "ID_AA64AFR0_EL1", .state = ARM_CP_STATE_AA64,
4925 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 4,
4926 .access = PL1_R, .type = ARM_CP_CONST,
4927 .resetvalue = cpu->id_aa64afr0 },
4928 { .name = "ID_AA64AFR1_EL1", .state = ARM_CP_STATE_AA64,
4929 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 5,
4930 .access = PL1_R, .type = ARM_CP_CONST,
4931 .resetvalue = cpu->id_aa64afr1 },
e20d84c1
PM
4932 { .name = "ID_AA64AFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4933 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 6,
4934 .access = PL1_R, .type = ARM_CP_CONST,
4935 .resetvalue = 0 },
4936 { .name = "ID_AA64AFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4937 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 7,
4938 .access = PL1_R, .type = ARM_CP_CONST,
4939 .resetvalue = 0 },
e60cef86
PM
4940 { .name = "ID_AA64ISAR0_EL1", .state = ARM_CP_STATE_AA64,
4941 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 0,
4942 .access = PL1_R, .type = ARM_CP_CONST,
4943 .resetvalue = cpu->id_aa64isar0 },
4944 { .name = "ID_AA64ISAR1_EL1", .state = ARM_CP_STATE_AA64,
4945 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 1,
4946 .access = PL1_R, .type = ARM_CP_CONST,
4947 .resetvalue = cpu->id_aa64isar1 },
e20d84c1
PM
4948 { .name = "ID_AA64ISAR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4949 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 2,
4950 .access = PL1_R, .type = ARM_CP_CONST,
4951 .resetvalue = 0 },
4952 { .name = "ID_AA64ISAR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4953 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 3,
4954 .access = PL1_R, .type = ARM_CP_CONST,
4955 .resetvalue = 0 },
4956 { .name = "ID_AA64ISAR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4957 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 4,
4958 .access = PL1_R, .type = ARM_CP_CONST,
4959 .resetvalue = 0 },
4960 { .name = "ID_AA64ISAR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4961 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 5,
4962 .access = PL1_R, .type = ARM_CP_CONST,
4963 .resetvalue = 0 },
4964 { .name = "ID_AA64ISAR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4965 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 6,
4966 .access = PL1_R, .type = ARM_CP_CONST,
4967 .resetvalue = 0 },
4968 { .name = "ID_AA64ISAR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4969 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 7,
4970 .access = PL1_R, .type = ARM_CP_CONST,
4971 .resetvalue = 0 },
e60cef86
PM
4972 { .name = "ID_AA64MMFR0_EL1", .state = ARM_CP_STATE_AA64,
4973 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
4974 .access = PL1_R, .type = ARM_CP_CONST,
4975 .resetvalue = cpu->id_aa64mmfr0 },
4976 { .name = "ID_AA64MMFR1_EL1", .state = ARM_CP_STATE_AA64,
4977 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 1,
4978 .access = PL1_R, .type = ARM_CP_CONST,
4979 .resetvalue = cpu->id_aa64mmfr1 },
e20d84c1
PM
4980 { .name = "ID_AA64MMFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4981 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 2,
4982 .access = PL1_R, .type = ARM_CP_CONST,
4983 .resetvalue = 0 },
4984 { .name = "ID_AA64MMFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4985 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 3,
4986 .access = PL1_R, .type = ARM_CP_CONST,
4987 .resetvalue = 0 },
4988 { .name = "ID_AA64MMFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4989 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 4,
4990 .access = PL1_R, .type = ARM_CP_CONST,
4991 .resetvalue = 0 },
4992 { .name = "ID_AA64MMFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4993 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 5,
4994 .access = PL1_R, .type = ARM_CP_CONST,
4995 .resetvalue = 0 },
4996 { .name = "ID_AA64MMFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4997 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 6,
4998 .access = PL1_R, .type = ARM_CP_CONST,
4999 .resetvalue = 0 },
5000 { .name = "ID_AA64MMFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
5001 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 7,
5002 .access = PL1_R, .type = ARM_CP_CONST,
5003 .resetvalue = 0 },
a50c0f51
PM
5004 { .name = "MVFR0_EL1", .state = ARM_CP_STATE_AA64,
5005 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0,
5006 .access = PL1_R, .type = ARM_CP_CONST,
5007 .resetvalue = cpu->mvfr0 },
5008 { .name = "MVFR1_EL1", .state = ARM_CP_STATE_AA64,
5009 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1,
5010 .access = PL1_R, .type = ARM_CP_CONST,
5011 .resetvalue = cpu->mvfr1 },
5012 { .name = "MVFR2_EL1", .state = ARM_CP_STATE_AA64,
5013 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2,
5014 .access = PL1_R, .type = ARM_CP_CONST,
5015 .resetvalue = cpu->mvfr2 },
e20d84c1
PM
5016 { .name = "MVFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
5017 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 3,
5018 .access = PL1_R, .type = ARM_CP_CONST,
5019 .resetvalue = 0 },
5020 { .name = "MVFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
5021 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 4,
5022 .access = PL1_R, .type = ARM_CP_CONST,
5023 .resetvalue = 0 },
5024 { .name = "MVFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
5025 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 5,
5026 .access = PL1_R, .type = ARM_CP_CONST,
5027 .resetvalue = 0 },
5028 { .name = "MVFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
5029 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 6,
5030 .access = PL1_R, .type = ARM_CP_CONST,
5031 .resetvalue = 0 },
5032 { .name = "MVFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
5033 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 7,
5034 .access = PL1_R, .type = ARM_CP_CONST,
5035 .resetvalue = 0 },
4054bfa9
AF
5036 { .name = "PMCEID0", .state = ARM_CP_STATE_AA32,
5037 .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 6,
5038 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
5039 .resetvalue = cpu->pmceid0 },
5040 { .name = "PMCEID0_EL0", .state = ARM_CP_STATE_AA64,
5041 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 6,
5042 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
5043 .resetvalue = cpu->pmceid0 },
5044 { .name = "PMCEID1", .state = ARM_CP_STATE_AA32,
5045 .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 7,
5046 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
5047 .resetvalue = cpu->pmceid1 },
5048 { .name = "PMCEID1_EL0", .state = ARM_CP_STATE_AA64,
5049 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 7,
5050 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
5051 .resetvalue = cpu->pmceid1 },
e60cef86
PM
5052 REGINFO_SENTINEL
5053 };
be8e8128
GB
5054 /* RVBAR_EL1 is only implemented if EL1 is the highest EL */
5055 if (!arm_feature(env, ARM_FEATURE_EL3) &&
5056 !arm_feature(env, ARM_FEATURE_EL2)) {
5057 ARMCPRegInfo rvbar = {
5058 .name = "RVBAR_EL1", .state = ARM_CP_STATE_AA64,
5059 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
5060 .type = ARM_CP_CONST, .access = PL1_R, .resetvalue = cpu->rvbar
5061 };
5062 define_one_arm_cp_reg(cpu, &rvbar);
5063 }
e60cef86 5064 define_arm_cp_regs(cpu, v8_idregs);
b0d2b7d0
PM
5065 define_arm_cp_regs(cpu, v8_cp_reginfo);
5066 }
3b685ba7 5067 if (arm_feature(env, ARM_FEATURE_EL2)) {
f0d574d6 5068 uint64_t vmpidr_def = mpidr_read_val(env);
731de9e6
EI
5069 ARMCPRegInfo vpidr_regs[] = {
5070 { .name = "VPIDR", .state = ARM_CP_STATE_AA32,
5071 .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
5072 .access = PL2_RW, .accessfn = access_el3_aa32ns,
36476562
PM
5073 .resetvalue = cpu->midr, .type = ARM_CP_ALIAS,
5074 .fieldoffset = offsetoflow32(CPUARMState, cp15.vpidr_el2) },
731de9e6
EI
5075 { .name = "VPIDR_EL2", .state = ARM_CP_STATE_AA64,
5076 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
5077 .access = PL2_RW, .resetvalue = cpu->midr,
5078 .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) },
f0d574d6
EI
5079 { .name = "VMPIDR", .state = ARM_CP_STATE_AA32,
5080 .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
5081 .access = PL2_RW, .accessfn = access_el3_aa32ns,
36476562
PM
5082 .resetvalue = vmpidr_def, .type = ARM_CP_ALIAS,
5083 .fieldoffset = offsetoflow32(CPUARMState, cp15.vmpidr_el2) },
f0d574d6
EI
5084 { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_AA64,
5085 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
5086 .access = PL2_RW,
5087 .resetvalue = vmpidr_def,
5088 .fieldoffset = offsetof(CPUARMState, cp15.vmpidr_el2) },
731de9e6
EI
5089 REGINFO_SENTINEL
5090 };
5091 define_arm_cp_regs(cpu, vpidr_regs);
4771cd01 5092 define_arm_cp_regs(cpu, el2_cp_reginfo);
be8e8128
GB
5093 /* RVBAR_EL2 is only implemented if EL2 is the highest EL */
5094 if (!arm_feature(env, ARM_FEATURE_EL3)) {
5095 ARMCPRegInfo rvbar = {
5096 .name = "RVBAR_EL2", .state = ARM_CP_STATE_AA64,
5097 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 1,
5098 .type = ARM_CP_CONST, .access = PL2_R, .resetvalue = cpu->rvbar
5099 };
5100 define_one_arm_cp_reg(cpu, &rvbar);
5101 }
d42e3c26
EI
5102 } else {
5103 /* If EL2 is missing but higher ELs are enabled, we need to
5104 * register the no_el2 reginfos.
5105 */
5106 if (arm_feature(env, ARM_FEATURE_EL3)) {
f0d574d6
EI
5107 /* When EL3 exists but not EL2, VPIDR and VMPIDR take the value
5108 * of MIDR_EL1 and MPIDR_EL1.
731de9e6
EI
5109 */
5110 ARMCPRegInfo vpidr_regs[] = {
5111 { .name = "VPIDR_EL2", .state = ARM_CP_STATE_BOTH,
5112 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
5113 .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any,
5114 .type = ARM_CP_CONST, .resetvalue = cpu->midr,
5115 .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) },
f0d574d6
EI
5116 { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_BOTH,
5117 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
5118 .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any,
5119 .type = ARM_CP_NO_RAW,
5120 .writefn = arm_cp_write_ignore, .readfn = mpidr_read },
731de9e6
EI
5121 REGINFO_SENTINEL
5122 };
5123 define_arm_cp_regs(cpu, vpidr_regs);
4771cd01 5124 define_arm_cp_regs(cpu, el3_no_el2_cp_reginfo);
d42e3c26 5125 }
3b685ba7 5126 }
81547d66 5127 if (arm_feature(env, ARM_FEATURE_EL3)) {
0f1a3b24 5128 define_arm_cp_regs(cpu, el3_cp_reginfo);
e24fdd23
PM
5129 ARMCPRegInfo el3_regs[] = {
5130 { .name = "RVBAR_EL3", .state = ARM_CP_STATE_AA64,
5131 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 1,
5132 .type = ARM_CP_CONST, .access = PL3_R, .resetvalue = cpu->rvbar },
5133 { .name = "SCTLR_EL3", .state = ARM_CP_STATE_AA64,
5134 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 0,
5135 .access = PL3_RW,
5136 .raw_writefn = raw_write, .writefn = sctlr_write,
5137 .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[3]),
5138 .resetvalue = cpu->reset_sctlr },
5139 REGINFO_SENTINEL
be8e8128 5140 };
e24fdd23
PM
5141
5142 define_arm_cp_regs(cpu, el3_regs);
81547d66 5143 }
2f027fc5
PM
5144 /* The behaviour of NSACR is sufficiently various that we don't
5145 * try to describe it in a single reginfo:
5146 * if EL3 is 64 bit, then trap to EL3 from S EL1,
5147 * reads as constant 0xc00 from NS EL1 and NS EL2
5148 * if EL3 is 32 bit, then RW at EL3, RO at NS EL1 and NS EL2
5149 * if v7 without EL3, register doesn't exist
5150 * if v8 without EL3, reads as constant 0xc00 from NS EL1 and NS EL2
5151 */
5152 if (arm_feature(env, ARM_FEATURE_EL3)) {
5153 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
5154 ARMCPRegInfo nsacr = {
5155 .name = "NSACR", .type = ARM_CP_CONST,
5156 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
5157 .access = PL1_RW, .accessfn = nsacr_access,
5158 .resetvalue = 0xc00
5159 };
5160 define_one_arm_cp_reg(cpu, &nsacr);
5161 } else {
5162 ARMCPRegInfo nsacr = {
5163 .name = "NSACR",
5164 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
5165 .access = PL3_RW | PL1_R,
5166 .resetvalue = 0,
5167 .fieldoffset = offsetof(CPUARMState, cp15.nsacr)
5168 };
5169 define_one_arm_cp_reg(cpu, &nsacr);
5170 }
5171 } else {
5172 if (arm_feature(env, ARM_FEATURE_V8)) {
5173 ARMCPRegInfo nsacr = {
5174 .name = "NSACR", .type = ARM_CP_CONST,
5175 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
5176 .access = PL1_R,
5177 .resetvalue = 0xc00
5178 };
5179 define_one_arm_cp_reg(cpu, &nsacr);
5180 }
5181 }
5182
452a0955 5183 if (arm_feature(env, ARM_FEATURE_PMSA)) {
6cb0b013
PC
5184 if (arm_feature(env, ARM_FEATURE_V6)) {
5185 /* PMSAv6 not implemented */
5186 assert(arm_feature(env, ARM_FEATURE_V7));
5187 define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
5188 define_arm_cp_regs(cpu, pmsav7_cp_reginfo);
5189 } else {
5190 define_arm_cp_regs(cpu, pmsav5_cp_reginfo);
5191 }
18032bec 5192 } else {
8e5d75c9 5193 define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
18032bec
PM
5194 define_arm_cp_regs(cpu, vmsa_cp_reginfo);
5195 }
c326b979
PM
5196 if (arm_feature(env, ARM_FEATURE_THUMB2EE)) {
5197 define_arm_cp_regs(cpu, t2ee_cp_reginfo);
5198 }
6cc7a3ae
PM
5199 if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) {
5200 define_arm_cp_regs(cpu, generic_timer_cp_reginfo);
5201 }
4a501606
PM
5202 if (arm_feature(env, ARM_FEATURE_VAPA)) {
5203 define_arm_cp_regs(cpu, vapa_cp_reginfo);
5204 }
c4804214
PM
5205 if (arm_feature(env, ARM_FEATURE_CACHE_TEST_CLEAN)) {
5206 define_arm_cp_regs(cpu, cache_test_clean_cp_reginfo);
5207 }
5208 if (arm_feature(env, ARM_FEATURE_CACHE_DIRTY_REG)) {
5209 define_arm_cp_regs(cpu, cache_dirty_status_cp_reginfo);
5210 }
5211 if (arm_feature(env, ARM_FEATURE_CACHE_BLOCK_OPS)) {
5212 define_arm_cp_regs(cpu, cache_block_ops_cp_reginfo);
5213 }
18032bec
PM
5214 if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
5215 define_arm_cp_regs(cpu, omap_cp_reginfo);
5216 }
34f90529
PM
5217 if (arm_feature(env, ARM_FEATURE_STRONGARM)) {
5218 define_arm_cp_regs(cpu, strongarm_cp_reginfo);
5219 }
1047b9d7
PM
5220 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
5221 define_arm_cp_regs(cpu, xscale_cp_reginfo);
5222 }
5223 if (arm_feature(env, ARM_FEATURE_DUMMY_C15_REGS)) {
5224 define_arm_cp_regs(cpu, dummy_c15_cp_reginfo);
5225 }
7ac681cf
PM
5226 if (arm_feature(env, ARM_FEATURE_LPAE)) {
5227 define_arm_cp_regs(cpu, lpae_cp_reginfo);
5228 }
7884849c
PM
5229 /* Slightly awkwardly, the OMAP and StrongARM cores need all of
5230 * cp15 crn=0 to be writes-ignored, whereas for other cores they should
5231 * be read-only (ie write causes UNDEF exception).
5232 */
5233 {
00a29f3d
PM
5234 ARMCPRegInfo id_pre_v8_midr_cp_reginfo[] = {
5235 /* Pre-v8 MIDR space.
5236 * Note that the MIDR isn't a simple constant register because
7884849c
PM
5237 * of the TI925 behaviour where writes to another register can
5238 * cause the MIDR value to change.
97ce8d61
PC
5239 *
5240 * Unimplemented registers in the c15 0 0 0 space default to
5241 * MIDR. Define MIDR first as this entire space, then CTR, TCMTR
5242 * and friends override accordingly.
7884849c
PM
5243 */
5244 { .name = "MIDR",
97ce8d61 5245 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = CP_ANY,
7884849c 5246 .access = PL1_R, .resetvalue = cpu->midr,
d4e6df63 5247 .writefn = arm_cp_write_ignore, .raw_writefn = raw_write,
731de9e6 5248 .readfn = midr_read,
97ce8d61
PC
5249 .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
5250 .type = ARM_CP_OVERRIDE },
7884849c
PM
5251 /* crn = 0 op1 = 0 crm = 3..7 : currently unassigned; we RAZ. */
5252 { .name = "DUMMY",
5253 .cp = 15, .crn = 0, .crm = 3, .opc1 = 0, .opc2 = CP_ANY,
5254 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
5255 { .name = "DUMMY",
5256 .cp = 15, .crn = 0, .crm = 4, .opc1 = 0, .opc2 = CP_ANY,
5257 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
5258 { .name = "DUMMY",
5259 .cp = 15, .crn = 0, .crm = 5, .opc1 = 0, .opc2 = CP_ANY,
5260 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
5261 { .name = "DUMMY",
5262 .cp = 15, .crn = 0, .crm = 6, .opc1 = 0, .opc2 = CP_ANY,
5263 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
5264 { .name = "DUMMY",
5265 .cp = 15, .crn = 0, .crm = 7, .opc1 = 0, .opc2 = CP_ANY,
5266 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
5267 REGINFO_SENTINEL
5268 };
00a29f3d 5269 ARMCPRegInfo id_v8_midr_cp_reginfo[] = {
00a29f3d
PM
5270 { .name = "MIDR_EL1", .state = ARM_CP_STATE_BOTH,
5271 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 0,
731de9e6
EI
5272 .access = PL1_R, .type = ARM_CP_NO_RAW, .resetvalue = cpu->midr,
5273 .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
5274 .readfn = midr_read },
ac00c79f
SF
5275 /* crn = 0 op1 = 0 crm = 0 op2 = 4,7 : AArch32 aliases of MIDR */
5276 { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
5277 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
5278 .access = PL1_R, .resetvalue = cpu->midr },
5279 { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
5280 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 7,
5281 .access = PL1_R, .resetvalue = cpu->midr },
00a29f3d
PM
5282 { .name = "REVIDR_EL1", .state = ARM_CP_STATE_BOTH,
5283 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 6,
13b72b2b 5284 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->revidr },
00a29f3d
PM
5285 REGINFO_SENTINEL
5286 };
5287 ARMCPRegInfo id_cp_reginfo[] = {
5288 /* These are common to v8 and pre-v8 */
5289 { .name = "CTR",
5290 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 1,
5291 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
5292 { .name = "CTR_EL0", .state = ARM_CP_STATE_AA64,
5293 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 0, .crm = 0,
5294 .access = PL0_R, .accessfn = ctr_el0_access,
5295 .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
5296 /* TCMTR and TLBTR exist in v8 but have no 64-bit versions */
5297 { .name = "TCMTR",
5298 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 2,
5299 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
00a29f3d
PM
5300 REGINFO_SENTINEL
5301 };
8085ce63
PC
5302 /* TLBTR is specific to VMSA */
5303 ARMCPRegInfo id_tlbtr_reginfo = {
5304 .name = "TLBTR",
5305 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 3,
5306 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0,
5307 };
3281af81
PC
5308 /* MPUIR is specific to PMSA V6+ */
5309 ARMCPRegInfo id_mpuir_reginfo = {
5310 .name = "MPUIR",
5311 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
5312 .access = PL1_R, .type = ARM_CP_CONST,
5313 .resetvalue = cpu->pmsav7_dregion << 8
5314 };
7884849c
PM
5315 ARMCPRegInfo crn0_wi_reginfo = {
5316 .name = "CRN0_WI", .cp = 15, .crn = 0, .crm = CP_ANY,
5317 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_W,
5318 .type = ARM_CP_NOP | ARM_CP_OVERRIDE
5319 };
5320 if (arm_feature(env, ARM_FEATURE_OMAPCP) ||
5321 arm_feature(env, ARM_FEATURE_STRONGARM)) {
5322 ARMCPRegInfo *r;
5323 /* Register the blanket "writes ignored" value first to cover the
a703eda1
PC
5324 * whole space. Then update the specific ID registers to allow write
5325 * access, so that they ignore writes rather than causing them to
5326 * UNDEF.
7884849c
PM
5327 */
5328 define_one_arm_cp_reg(cpu, &crn0_wi_reginfo);
00a29f3d
PM
5329 for (r = id_pre_v8_midr_cp_reginfo;
5330 r->type != ARM_CP_SENTINEL; r++) {
5331 r->access = PL1_RW;
5332 }
7884849c
PM
5333 for (r = id_cp_reginfo; r->type != ARM_CP_SENTINEL; r++) {
5334 r->access = PL1_RW;
7884849c 5335 }
8085ce63 5336 id_tlbtr_reginfo.access = PL1_RW;
3281af81 5337 id_tlbtr_reginfo.access = PL1_RW;
7884849c 5338 }
00a29f3d
PM
5339 if (arm_feature(env, ARM_FEATURE_V8)) {
5340 define_arm_cp_regs(cpu, id_v8_midr_cp_reginfo);
5341 } else {
5342 define_arm_cp_regs(cpu, id_pre_v8_midr_cp_reginfo);
5343 }
a703eda1 5344 define_arm_cp_regs(cpu, id_cp_reginfo);
452a0955 5345 if (!arm_feature(env, ARM_FEATURE_PMSA)) {
8085ce63 5346 define_one_arm_cp_reg(cpu, &id_tlbtr_reginfo);
3281af81
PC
5347 } else if (arm_feature(env, ARM_FEATURE_V7)) {
5348 define_one_arm_cp_reg(cpu, &id_mpuir_reginfo);
8085ce63 5349 }
7884849c
PM
5350 }
5351
97ce8d61
PC
5352 if (arm_feature(env, ARM_FEATURE_MPIDR)) {
5353 define_arm_cp_regs(cpu, mpidr_cp_reginfo);
5354 }
5355
2771db27 5356 if (arm_feature(env, ARM_FEATURE_AUXCR)) {
834a6c69
PM
5357 ARMCPRegInfo auxcr_reginfo[] = {
5358 { .name = "ACTLR_EL1", .state = ARM_CP_STATE_BOTH,
5359 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 1,
5360 .access = PL1_RW, .type = ARM_CP_CONST,
5361 .resetvalue = cpu->reset_auxcr },
5362 { .name = "ACTLR_EL2", .state = ARM_CP_STATE_BOTH,
5363 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 1,
5364 .access = PL2_RW, .type = ARM_CP_CONST,
5365 .resetvalue = 0 },
5366 { .name = "ACTLR_EL3", .state = ARM_CP_STATE_AA64,
5367 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 1,
5368 .access = PL3_RW, .type = ARM_CP_CONST,
5369 .resetvalue = 0 },
5370 REGINFO_SENTINEL
2771db27 5371 };
834a6c69 5372 define_arm_cp_regs(cpu, auxcr_reginfo);
2771db27
PM
5373 }
5374
d8ba780b 5375 if (arm_feature(env, ARM_FEATURE_CBAR)) {
f318cec6
PM
5376 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
5377 /* 32 bit view is [31:18] 0...0 [43:32]. */
5378 uint32_t cbar32 = (extract64(cpu->reset_cbar, 18, 14) << 18)
5379 | extract64(cpu->reset_cbar, 32, 12);
5380 ARMCPRegInfo cbar_reginfo[] = {
5381 { .name = "CBAR",
5382 .type = ARM_CP_CONST,
5383 .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0,
5384 .access = PL1_R, .resetvalue = cpu->reset_cbar },
5385 { .name = "CBAR_EL1", .state = ARM_CP_STATE_AA64,
5386 .type = ARM_CP_CONST,
5387 .opc0 = 3, .opc1 = 1, .crn = 15, .crm = 3, .opc2 = 0,
5388 .access = PL1_R, .resetvalue = cbar32 },
5389 REGINFO_SENTINEL
5390 };
5391 /* We don't implement a r/w 64 bit CBAR currently */
5392 assert(arm_feature(env, ARM_FEATURE_CBAR_RO));
5393 define_arm_cp_regs(cpu, cbar_reginfo);
5394 } else {
5395 ARMCPRegInfo cbar = {
5396 .name = "CBAR",
5397 .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0,
5398 .access = PL1_R|PL3_W, .resetvalue = cpu->reset_cbar,
5399 .fieldoffset = offsetof(CPUARMState,
5400 cp15.c15_config_base_address)
5401 };
5402 if (arm_feature(env, ARM_FEATURE_CBAR_RO)) {
5403 cbar.access = PL1_R;
5404 cbar.fieldoffset = 0;
5405 cbar.type = ARM_CP_CONST;
5406 }
5407 define_one_arm_cp_reg(cpu, &cbar);
5408 }
d8ba780b
PC
5409 }
5410
91db4642
CLG
5411 if (arm_feature(env, ARM_FEATURE_VBAR)) {
5412 ARMCPRegInfo vbar_cp_reginfo[] = {
5413 { .name = "VBAR", .state = ARM_CP_STATE_BOTH,
5414 .opc0 = 3, .crn = 12, .crm = 0, .opc1 = 0, .opc2 = 0,
5415 .access = PL1_RW, .writefn = vbar_write,
5416 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.vbar_s),
5417 offsetof(CPUARMState, cp15.vbar_ns) },
5418 .resetvalue = 0 },
5419 REGINFO_SENTINEL
5420 };
5421 define_arm_cp_regs(cpu, vbar_cp_reginfo);
5422 }
5423
2771db27
PM
5424 /* Generic registers whose values depend on the implementation */
5425 {
5426 ARMCPRegInfo sctlr = {
5ebafdf3 5427 .name = "SCTLR", .state = ARM_CP_STATE_BOTH,
137feaa9
FA
5428 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
5429 .access = PL1_RW,
5430 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.sctlr_s),
5431 offsetof(CPUARMState, cp15.sctlr_ns) },
d4e6df63
PM
5432 .writefn = sctlr_write, .resetvalue = cpu->reset_sctlr,
5433 .raw_writefn = raw_write,
2771db27
PM
5434 };
5435 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
5436 /* Normally we would always end the TB on an SCTLR write, but Linux
5437 * arch/arm/mach-pxa/sleep.S expects two instructions following
5438 * an MMU enable to execute from cache. Imitate this behaviour.
5439 */
5440 sctlr.type |= ARM_CP_SUPPRESS_TB_END;
5441 }
5442 define_one_arm_cp_reg(cpu, &sctlr);
5443 }
5be5e8ed
RH
5444
5445 if (arm_feature(env, ARM_FEATURE_SVE)) {
5446 define_one_arm_cp_reg(cpu, &zcr_el1_reginfo);
5447 if (arm_feature(env, ARM_FEATURE_EL2)) {
5448 define_one_arm_cp_reg(cpu, &zcr_el2_reginfo);
5449 } else {
5450 define_one_arm_cp_reg(cpu, &zcr_no_el2_reginfo);
5451 }
5452 if (arm_feature(env, ARM_FEATURE_EL3)) {
5453 define_one_arm_cp_reg(cpu, &zcr_el3_reginfo);
5454 }
5455 }
2ceb98c0
PM
5456}
5457
14969266
AF
5458void arm_cpu_register_gdb_regs_for_features(ARMCPU *cpu)
5459{
22169d41 5460 CPUState *cs = CPU(cpu);
14969266
AF
5461 CPUARMState *env = &cpu->env;
5462
6a669427
PM
5463 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
5464 gdb_register_coprocessor(cs, aarch64_fpu_gdb_get_reg,
5465 aarch64_fpu_gdb_set_reg,
5466 34, "aarch64-fpu.xml", 0);
5467 } else if (arm_feature(env, ARM_FEATURE_NEON)) {
22169d41 5468 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
56aebc89
PB
5469 51, "arm-neon.xml", 0);
5470 } else if (arm_feature(env, ARM_FEATURE_VFP3)) {
22169d41 5471 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
56aebc89
PB
5472 35, "arm-vfp3.xml", 0);
5473 } else if (arm_feature(env, ARM_FEATURE_VFP)) {
22169d41 5474 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
56aebc89
PB
5475 19, "arm-vfp.xml", 0);
5476 }
40f137e1
PB
5477}
5478
777dc784
PM
5479/* Sort alphabetically by type name, except for "any". */
5480static gint arm_cpu_list_compare(gconstpointer a, gconstpointer b)
5adb4839 5481{
777dc784
PM
5482 ObjectClass *class_a = (ObjectClass *)a;
5483 ObjectClass *class_b = (ObjectClass *)b;
5484 const char *name_a, *name_b;
5adb4839 5485
777dc784
PM
5486 name_a = object_class_get_name(class_a);
5487 name_b = object_class_get_name(class_b);
51492fd1 5488 if (strcmp(name_a, "any-" TYPE_ARM_CPU) == 0) {
777dc784 5489 return 1;
51492fd1 5490 } else if (strcmp(name_b, "any-" TYPE_ARM_CPU) == 0) {
777dc784
PM
5491 return -1;
5492 } else {
5493 return strcmp(name_a, name_b);
5adb4839
PB
5494 }
5495}
5496
777dc784 5497static void arm_cpu_list_entry(gpointer data, gpointer user_data)
40f137e1 5498{
777dc784 5499 ObjectClass *oc = data;
92a31361 5500 CPUListState *s = user_data;
51492fd1
AF
5501 const char *typename;
5502 char *name;
3371d272 5503
51492fd1
AF
5504 typename = object_class_get_name(oc);
5505 name = g_strndup(typename, strlen(typename) - strlen("-" TYPE_ARM_CPU));
777dc784 5506 (*s->cpu_fprintf)(s->file, " %s\n",
51492fd1
AF
5507 name);
5508 g_free(name);
777dc784
PM
5509}
5510
5511void arm_cpu_list(FILE *f, fprintf_function cpu_fprintf)
5512{
92a31361 5513 CPUListState s = {
777dc784
PM
5514 .file = f,
5515 .cpu_fprintf = cpu_fprintf,
5516 };
5517 GSList *list;
5518
5519 list = object_class_get_list(TYPE_ARM_CPU, false);
5520 list = g_slist_sort(list, arm_cpu_list_compare);
5521 (*cpu_fprintf)(f, "Available CPUs:\n");
5522 g_slist_foreach(list, arm_cpu_list_entry, &s);
5523 g_slist_free(list);
a96c0514
PM
5524#ifdef CONFIG_KVM
5525 /* The 'host' CPU type is dynamically registered only if KVM is
5526 * enabled, so we have to special-case it here:
5527 */
5528 (*cpu_fprintf)(f, " host (only available in KVM mode)\n");
5529#endif
40f137e1
PB
5530}
5531
78027bb6
CR
5532static void arm_cpu_add_definition(gpointer data, gpointer user_data)
5533{
5534 ObjectClass *oc = data;
5535 CpuDefinitionInfoList **cpu_list = user_data;
5536 CpuDefinitionInfoList *entry;
5537 CpuDefinitionInfo *info;
5538 const char *typename;
5539
5540 typename = object_class_get_name(oc);
5541 info = g_malloc0(sizeof(*info));
5542 info->name = g_strndup(typename,
5543 strlen(typename) - strlen("-" TYPE_ARM_CPU));
8ed877b7 5544 info->q_typename = g_strdup(typename);
78027bb6
CR
5545
5546 entry = g_malloc0(sizeof(*entry));
5547 entry->value = info;
5548 entry->next = *cpu_list;
5549 *cpu_list = entry;
5550}
5551
5552CpuDefinitionInfoList *arch_query_cpu_definitions(Error **errp)
5553{
5554 CpuDefinitionInfoList *cpu_list = NULL;
5555 GSList *list;
5556
5557 list = object_class_get_list(TYPE_ARM_CPU, false);
5558 g_slist_foreach(list, arm_cpu_add_definition, &cpu_list);
5559 g_slist_free(list);
5560
5561 return cpu_list;
5562}
5563
6e6efd61 5564static void add_cpreg_to_hashtable(ARMCPU *cpu, const ARMCPRegInfo *r,
51a79b03 5565 void *opaque, int state, int secstate,
f5a0a5a5 5566 int crm, int opc1, int opc2)
6e6efd61
PM
5567{
5568 /* Private utility function for define_one_arm_cp_reg_with_opaque():
5569 * add a single reginfo struct to the hash table.
5570 */
5571 uint32_t *key = g_new(uint32_t, 1);
5572 ARMCPRegInfo *r2 = g_memdup(r, sizeof(ARMCPRegInfo));
5573 int is64 = (r->type & ARM_CP_64BIT) ? 1 : 0;
3f3c82a5
FA
5574 int ns = (secstate & ARM_CP_SECSTATE_NS) ? 1 : 0;
5575
5576 /* Reset the secure state to the specific incoming state. This is
5577 * necessary as the register may have been defined with both states.
5578 */
5579 r2->secure = secstate;
5580
5581 if (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1]) {
5582 /* Register is banked (using both entries in array).
5583 * Overwriting fieldoffset as the array is only used to define
5584 * banked registers but later only fieldoffset is used.
f5a0a5a5 5585 */
3f3c82a5
FA
5586 r2->fieldoffset = r->bank_fieldoffsets[ns];
5587 }
5588
5589 if (state == ARM_CP_STATE_AA32) {
5590 if (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1]) {
5591 /* If the register is banked then we don't need to migrate or
5592 * reset the 32-bit instance in certain cases:
5593 *
5594 * 1) If the register has both 32-bit and 64-bit instances then we
5595 * can count on the 64-bit instance taking care of the
5596 * non-secure bank.
5597 * 2) If ARMv8 is enabled then we can count on a 64-bit version
5598 * taking care of the secure bank. This requires that separate
5599 * 32 and 64-bit definitions are provided.
5600 */
5601 if ((r->state == ARM_CP_STATE_BOTH && ns) ||
5602 (arm_feature(&cpu->env, ARM_FEATURE_V8) && !ns)) {
7a0e58fa 5603 r2->type |= ARM_CP_ALIAS;
3f3c82a5
FA
5604 }
5605 } else if ((secstate != r->secure) && !ns) {
5606 /* The register is not banked so we only want to allow migration of
5607 * the non-secure instance.
5608 */
7a0e58fa 5609 r2->type |= ARM_CP_ALIAS;
58a1d8ce 5610 }
3f3c82a5
FA
5611
5612 if (r->state == ARM_CP_STATE_BOTH) {
5613 /* We assume it is a cp15 register if the .cp field is left unset.
5614 */
5615 if (r2->cp == 0) {
5616 r2->cp = 15;
5617 }
5618
f5a0a5a5 5619#ifdef HOST_WORDS_BIGENDIAN
3f3c82a5
FA
5620 if (r2->fieldoffset) {
5621 r2->fieldoffset += sizeof(uint32_t);
5622 }
f5a0a5a5 5623#endif
3f3c82a5 5624 }
f5a0a5a5
PM
5625 }
5626 if (state == ARM_CP_STATE_AA64) {
5627 /* To allow abbreviation of ARMCPRegInfo
5628 * definitions, we treat cp == 0 as equivalent to
5629 * the value for "standard guest-visible sysreg".
58a1d8ce
PM
5630 * STATE_BOTH definitions are also always "standard
5631 * sysreg" in their AArch64 view (the .cp value may
5632 * be non-zero for the benefit of the AArch32 view).
f5a0a5a5 5633 */
58a1d8ce 5634 if (r->cp == 0 || r->state == ARM_CP_STATE_BOTH) {
f5a0a5a5
PM
5635 r2->cp = CP_REG_ARM64_SYSREG_CP;
5636 }
5637 *key = ENCODE_AA64_CP_REG(r2->cp, r2->crn, crm,
5638 r2->opc0, opc1, opc2);
5639 } else {
51a79b03 5640 *key = ENCODE_CP_REG(r2->cp, is64, ns, r2->crn, crm, opc1, opc2);
f5a0a5a5 5641 }
6e6efd61
PM
5642 if (opaque) {
5643 r2->opaque = opaque;
5644 }
67ed771d
PM
5645 /* reginfo passed to helpers is correct for the actual access,
5646 * and is never ARM_CP_STATE_BOTH:
5647 */
5648 r2->state = state;
6e6efd61
PM
5649 /* Make sure reginfo passed to helpers for wildcarded regs
5650 * has the correct crm/opc1/opc2 for this reg, not CP_ANY:
5651 */
5652 r2->crm = crm;
5653 r2->opc1 = opc1;
5654 r2->opc2 = opc2;
5655 /* By convention, for wildcarded registers only the first
5656 * entry is used for migration; the others are marked as
7a0e58fa 5657 * ALIAS so we don't try to transfer the register
6e6efd61 5658 * multiple times. Special registers (ie NOP/WFI) are
7a0e58fa 5659 * never migratable and not even raw-accessible.
6e6efd61 5660 */
7a0e58fa
PM
5661 if ((r->type & ARM_CP_SPECIAL)) {
5662 r2->type |= ARM_CP_NO_RAW;
5663 }
5664 if (((r->crm == CP_ANY) && crm != 0) ||
6e6efd61
PM
5665 ((r->opc1 == CP_ANY) && opc1 != 0) ||
5666 ((r->opc2 == CP_ANY) && opc2 != 0)) {
7a0e58fa 5667 r2->type |= ARM_CP_ALIAS;
6e6efd61
PM
5668 }
5669
375421cc
PM
5670 /* Check that raw accesses are either forbidden or handled. Note that
5671 * we can't assert this earlier because the setup of fieldoffset for
5672 * banked registers has to be done first.
5673 */
5674 if (!(r2->type & ARM_CP_NO_RAW)) {
5675 assert(!raw_accessors_invalid(r2));
5676 }
5677
6e6efd61
PM
5678 /* Overriding of an existing definition must be explicitly
5679 * requested.
5680 */
5681 if (!(r->type & ARM_CP_OVERRIDE)) {
5682 ARMCPRegInfo *oldreg;
5683 oldreg = g_hash_table_lookup(cpu->cp_regs, key);
5684 if (oldreg && !(oldreg->type & ARM_CP_OVERRIDE)) {
5685 fprintf(stderr, "Register redefined: cp=%d %d bit "
5686 "crn=%d crm=%d opc1=%d opc2=%d, "
5687 "was %s, now %s\n", r2->cp, 32 + 32 * is64,
5688 r2->crn, r2->crm, r2->opc1, r2->opc2,
5689 oldreg->name, r2->name);
5690 g_assert_not_reached();
5691 }
5692 }
5693 g_hash_table_insert(cpu->cp_regs, key, r2);
5694}
5695
5696
4b6a83fb
PM
5697void define_one_arm_cp_reg_with_opaque(ARMCPU *cpu,
5698 const ARMCPRegInfo *r, void *opaque)
5699{
5700 /* Define implementations of coprocessor registers.
5701 * We store these in a hashtable because typically
5702 * there are less than 150 registers in a space which
5703 * is 16*16*16*8*8 = 262144 in size.
5704 * Wildcarding is supported for the crm, opc1 and opc2 fields.
5705 * If a register is defined twice then the second definition is
5706 * used, so this can be used to define some generic registers and
5707 * then override them with implementation specific variations.
5708 * At least one of the original and the second definition should
5709 * include ARM_CP_OVERRIDE in its type bits -- this is just a guard
5710 * against accidental use.
f5a0a5a5
PM
5711 *
5712 * The state field defines whether the register is to be
5713 * visible in the AArch32 or AArch64 execution state. If the
5714 * state is set to ARM_CP_STATE_BOTH then we synthesise a
5715 * reginfo structure for the AArch32 view, which sees the lower
5716 * 32 bits of the 64 bit register.
5717 *
5718 * Only registers visible in AArch64 may set r->opc0; opc0 cannot
5719 * be wildcarded. AArch64 registers are always considered to be 64
5720 * bits; the ARM_CP_64BIT* flag applies only to the AArch32 view of
5721 * the register, if any.
4b6a83fb 5722 */
f5a0a5a5 5723 int crm, opc1, opc2, state;
4b6a83fb
PM
5724 int crmmin = (r->crm == CP_ANY) ? 0 : r->crm;
5725 int crmmax = (r->crm == CP_ANY) ? 15 : r->crm;
5726 int opc1min = (r->opc1 == CP_ANY) ? 0 : r->opc1;
5727 int opc1max = (r->opc1 == CP_ANY) ? 7 : r->opc1;
5728 int opc2min = (r->opc2 == CP_ANY) ? 0 : r->opc2;
5729 int opc2max = (r->opc2 == CP_ANY) ? 7 : r->opc2;
5730 /* 64 bit registers have only CRm and Opc1 fields */
5731 assert(!((r->type & ARM_CP_64BIT) && (r->opc2 || r->crn)));
f5a0a5a5
PM
5732 /* op0 only exists in the AArch64 encodings */
5733 assert((r->state != ARM_CP_STATE_AA32) || (r->opc0 == 0));
5734 /* AArch64 regs are all 64 bit so ARM_CP_64BIT is meaningless */
5735 assert((r->state != ARM_CP_STATE_AA64) || !(r->type & ARM_CP_64BIT));
5736 /* The AArch64 pseudocode CheckSystemAccess() specifies that op1
5737 * encodes a minimum access level for the register. We roll this
5738 * runtime check into our general permission check code, so check
5739 * here that the reginfo's specified permissions are strict enough
5740 * to encompass the generic architectural permission check.
5741 */
5742 if (r->state != ARM_CP_STATE_AA32) {
5743 int mask = 0;
5744 switch (r->opc1) {
5745 case 0: case 1: case 2:
5746 /* min_EL EL1 */
5747 mask = PL1_RW;
5748 break;
5749 case 3:
5750 /* min_EL EL0 */
5751 mask = PL0_RW;
5752 break;
5753 case 4:
5754 /* min_EL EL2 */
5755 mask = PL2_RW;
5756 break;
5757 case 5:
5758 /* unallocated encoding, so not possible */
5759 assert(false);
5760 break;
5761 case 6:
5762 /* min_EL EL3 */
5763 mask = PL3_RW;
5764 break;
5765 case 7:
5766 /* min_EL EL1, secure mode only (we don't check the latter) */
5767 mask = PL1_RW;
5768 break;
5769 default:
5770 /* broken reginfo with out-of-range opc1 */
5771 assert(false);
5772 break;
5773 }
5774 /* assert our permissions are not too lax (stricter is fine) */
5775 assert((r->access & ~mask) == 0);
5776 }
5777
4b6a83fb
PM
5778 /* Check that the register definition has enough info to handle
5779 * reads and writes if they are permitted.
5780 */
5781 if (!(r->type & (ARM_CP_SPECIAL|ARM_CP_CONST))) {
5782 if (r->access & PL3_R) {
3f3c82a5
FA
5783 assert((r->fieldoffset ||
5784 (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
5785 r->readfn);
4b6a83fb
PM
5786 }
5787 if (r->access & PL3_W) {
3f3c82a5
FA
5788 assert((r->fieldoffset ||
5789 (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
5790 r->writefn);
4b6a83fb
PM
5791 }
5792 }
5793 /* Bad type field probably means missing sentinel at end of reg list */
5794 assert(cptype_valid(r->type));
5795 for (crm = crmmin; crm <= crmmax; crm++) {
5796 for (opc1 = opc1min; opc1 <= opc1max; opc1++) {
5797 for (opc2 = opc2min; opc2 <= opc2max; opc2++) {
f5a0a5a5
PM
5798 for (state = ARM_CP_STATE_AA32;
5799 state <= ARM_CP_STATE_AA64; state++) {
5800 if (r->state != state && r->state != ARM_CP_STATE_BOTH) {
5801 continue;
5802 }
3f3c82a5
FA
5803 if (state == ARM_CP_STATE_AA32) {
5804 /* Under AArch32 CP registers can be common
5805 * (same for secure and non-secure world) or banked.
5806 */
5807 switch (r->secure) {
5808 case ARM_CP_SECSTATE_S:
5809 case ARM_CP_SECSTATE_NS:
5810 add_cpreg_to_hashtable(cpu, r, opaque, state,
5811 r->secure, crm, opc1, opc2);
5812 break;
5813 default:
5814 add_cpreg_to_hashtable(cpu, r, opaque, state,
5815 ARM_CP_SECSTATE_S,
5816 crm, opc1, opc2);
5817 add_cpreg_to_hashtable(cpu, r, opaque, state,
5818 ARM_CP_SECSTATE_NS,
5819 crm, opc1, opc2);
5820 break;
5821 }
5822 } else {
5823 /* AArch64 registers get mapped to non-secure instance
5824 * of AArch32 */
5825 add_cpreg_to_hashtable(cpu, r, opaque, state,
5826 ARM_CP_SECSTATE_NS,
5827 crm, opc1, opc2);
5828 }
f5a0a5a5 5829 }
4b6a83fb
PM
5830 }
5831 }
5832 }
5833}
5834
5835void define_arm_cp_regs_with_opaque(ARMCPU *cpu,
5836 const ARMCPRegInfo *regs, void *opaque)
5837{
5838 /* Define a whole list of registers */
5839 const ARMCPRegInfo *r;
5840 for (r = regs; r->type != ARM_CP_SENTINEL; r++) {
5841 define_one_arm_cp_reg_with_opaque(cpu, r, opaque);
5842 }
5843}
5844
60322b39 5845const ARMCPRegInfo *get_arm_cp_reginfo(GHashTable *cpregs, uint32_t encoded_cp)
4b6a83fb 5846{
60322b39 5847 return g_hash_table_lookup(cpregs, &encoded_cp);
4b6a83fb
PM
5848}
5849
c4241c7d
PM
5850void arm_cp_write_ignore(CPUARMState *env, const ARMCPRegInfo *ri,
5851 uint64_t value)
4b6a83fb
PM
5852{
5853 /* Helper coprocessor write function for write-ignore registers */
4b6a83fb
PM
5854}
5855
c4241c7d 5856uint64_t arm_cp_read_zero(CPUARMState *env, const ARMCPRegInfo *ri)
4b6a83fb
PM
5857{
5858 /* Helper coprocessor write function for read-as-zero registers */
4b6a83fb
PM
5859 return 0;
5860}
5861
f5a0a5a5
PM
5862void arm_cp_reset_ignore(CPUARMState *env, const ARMCPRegInfo *opaque)
5863{
5864 /* Helper coprocessor reset function for do-nothing-on-reset registers */
5865}
5866
af393ffc 5867static int bad_mode_switch(CPUARMState *env, int mode, CPSRWriteType write_type)
37064a8b
PM
5868{
5869 /* Return true if it is not valid for us to switch to
5870 * this CPU mode (ie all the UNPREDICTABLE cases in
5871 * the ARM ARM CPSRWriteByInstr pseudocode).
5872 */
af393ffc
PM
5873
5874 /* Changes to or from Hyp via MSR and CPS are illegal. */
5875 if (write_type == CPSRWriteByInstr &&
5876 ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_HYP ||
5877 mode == ARM_CPU_MODE_HYP)) {
5878 return 1;
5879 }
5880
37064a8b
PM
5881 switch (mode) {
5882 case ARM_CPU_MODE_USR:
10eacda7 5883 return 0;
37064a8b
PM
5884 case ARM_CPU_MODE_SYS:
5885 case ARM_CPU_MODE_SVC:
5886 case ARM_CPU_MODE_ABT:
5887 case ARM_CPU_MODE_UND:
5888 case ARM_CPU_MODE_IRQ:
5889 case ARM_CPU_MODE_FIQ:
52ff951b
PM
5890 /* Note that we don't implement the IMPDEF NSACR.RFR which in v7
5891 * allows FIQ mode to be Secure-only. (In v8 this doesn't exist.)
5892 */
10eacda7
PM
5893 /* If HCR.TGE is set then changes from Monitor to NS PL1 via MSR
5894 * and CPS are treated as illegal mode changes.
5895 */
5896 if (write_type == CPSRWriteByInstr &&
5897 (env->cp15.hcr_el2 & HCR_TGE) &&
5898 (env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON &&
5899 !arm_is_secure_below_el3(env)) {
5900 return 1;
5901 }
37064a8b 5902 return 0;
e6c8fc07
PM
5903 case ARM_CPU_MODE_HYP:
5904 return !arm_feature(env, ARM_FEATURE_EL2)
5905 || arm_current_el(env) < 2 || arm_is_secure(env);
027fc527 5906 case ARM_CPU_MODE_MON:
58ae2d1f 5907 return arm_current_el(env) < 3;
37064a8b
PM
5908 default:
5909 return 1;
5910 }
5911}
5912
2f4a40e5
AZ
5913uint32_t cpsr_read(CPUARMState *env)
5914{
5915 int ZF;
6fbe23d5
PB
5916 ZF = (env->ZF == 0);
5917 return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) |
2f4a40e5
AZ
5918 (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27)
5919 | (env->thumb << 5) | ((env->condexec_bits & 3) << 25)
5920 | ((env->condexec_bits & 0xfc) << 8)
af519934 5921 | (env->GE << 16) | (env->daif & CPSR_AIF);
2f4a40e5
AZ
5922}
5923
50866ba5
PM
5924void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask,
5925 CPSRWriteType write_type)
2f4a40e5 5926{
6e8801f9
FA
5927 uint32_t changed_daif;
5928
2f4a40e5 5929 if (mask & CPSR_NZCV) {
6fbe23d5
PB
5930 env->ZF = (~val) & CPSR_Z;
5931 env->NF = val;
2f4a40e5
AZ
5932 env->CF = (val >> 29) & 1;
5933 env->VF = (val << 3) & 0x80000000;
5934 }
5935 if (mask & CPSR_Q)
5936 env->QF = ((val & CPSR_Q) != 0);
5937 if (mask & CPSR_T)
5938 env->thumb = ((val & CPSR_T) != 0);
5939 if (mask & CPSR_IT_0_1) {
5940 env->condexec_bits &= ~3;
5941 env->condexec_bits |= (val >> 25) & 3;
5942 }
5943 if (mask & CPSR_IT_2_7) {
5944 env->condexec_bits &= 3;
5945 env->condexec_bits |= (val >> 8) & 0xfc;
5946 }
5947 if (mask & CPSR_GE) {
5948 env->GE = (val >> 16) & 0xf;
5949 }
5950
6e8801f9
FA
5951 /* In a V7 implementation that includes the security extensions but does
5952 * not include Virtualization Extensions the SCR.FW and SCR.AW bits control
5953 * whether non-secure software is allowed to change the CPSR_F and CPSR_A
5954 * bits respectively.
5955 *
5956 * In a V8 implementation, it is permitted for privileged software to
5957 * change the CPSR A/F bits regardless of the SCR.AW/FW bits.
5958 */
f8c88bbc 5959 if (write_type != CPSRWriteRaw && !arm_feature(env, ARM_FEATURE_V8) &&
6e8801f9
FA
5960 arm_feature(env, ARM_FEATURE_EL3) &&
5961 !arm_feature(env, ARM_FEATURE_EL2) &&
5962 !arm_is_secure(env)) {
5963
5964 changed_daif = (env->daif ^ val) & mask;
5965
5966 if (changed_daif & CPSR_A) {
5967 /* Check to see if we are allowed to change the masking of async
5968 * abort exceptions from a non-secure state.
5969 */
5970 if (!(env->cp15.scr_el3 & SCR_AW)) {
5971 qemu_log_mask(LOG_GUEST_ERROR,
5972 "Ignoring attempt to switch CPSR_A flag from "
5973 "non-secure world with SCR.AW bit clear\n");
5974 mask &= ~CPSR_A;
5975 }
5976 }
5977
5978 if (changed_daif & CPSR_F) {
5979 /* Check to see if we are allowed to change the masking of FIQ
5980 * exceptions from a non-secure state.
5981 */
5982 if (!(env->cp15.scr_el3 & SCR_FW)) {
5983 qemu_log_mask(LOG_GUEST_ERROR,
5984 "Ignoring attempt to switch CPSR_F flag from "
5985 "non-secure world with SCR.FW bit clear\n");
5986 mask &= ~CPSR_F;
5987 }
5988
5989 /* Check whether non-maskable FIQ (NMFI) support is enabled.
5990 * If this bit is set software is not allowed to mask
5991 * FIQs, but is allowed to set CPSR_F to 0.
5992 */
5993 if ((A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_NMFI) &&
5994 (val & CPSR_F)) {
5995 qemu_log_mask(LOG_GUEST_ERROR,
5996 "Ignoring attempt to enable CPSR_F flag "
5997 "(non-maskable FIQ [NMFI] support enabled)\n");
5998 mask &= ~CPSR_F;
5999 }
6000 }
6001 }
6002
4cc35614
PM
6003 env->daif &= ~(CPSR_AIF & mask);
6004 env->daif |= val & CPSR_AIF & mask;
6005
f8c88bbc
PM
6006 if (write_type != CPSRWriteRaw &&
6007 ((env->uncached_cpsr ^ val) & mask & CPSR_M)) {
8c4f0eb9
PM
6008 if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_USR) {
6009 /* Note that we can only get here in USR mode if this is a
6010 * gdb stub write; for this case we follow the architectural
6011 * behaviour for guest writes in USR mode of ignoring an attempt
6012 * to switch mode. (Those are caught by translate.c for writes
6013 * triggered by guest instructions.)
6014 */
6015 mask &= ~CPSR_M;
6016 } else if (bad_mode_switch(env, val & CPSR_M, write_type)) {
81907a58
PM
6017 /* Attempt to switch to an invalid mode: this is UNPREDICTABLE in
6018 * v7, and has defined behaviour in v8:
6019 * + leave CPSR.M untouched
6020 * + allow changes to the other CPSR fields
6021 * + set PSTATE.IL
6022 * For user changes via the GDB stub, we don't set PSTATE.IL,
6023 * as this would be unnecessarily harsh for a user error.
37064a8b
PM
6024 */
6025 mask &= ~CPSR_M;
81907a58
PM
6026 if (write_type != CPSRWriteByGDBStub &&
6027 arm_feature(env, ARM_FEATURE_V8)) {
6028 mask |= CPSR_IL;
6029 val |= CPSR_IL;
6030 }
37064a8b
PM
6031 } else {
6032 switch_mode(env, val & CPSR_M);
6033 }
2f4a40e5
AZ
6034 }
6035 mask &= ~CACHED_CPSR_BITS;
6036 env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask);
6037}
6038
b26eefb6
PB
6039/* Sign/zero extend */
6040uint32_t HELPER(sxtb16)(uint32_t x)
6041{
6042 uint32_t res;
6043 res = (uint16_t)(int8_t)x;
6044 res |= (uint32_t)(int8_t)(x >> 16) << 16;
6045 return res;
6046}
6047
6048uint32_t HELPER(uxtb16)(uint32_t x)
6049{
6050 uint32_t res;
6051 res = (uint16_t)(uint8_t)x;
6052 res |= (uint32_t)(uint8_t)(x >> 16) << 16;
6053 return res;
6054}
6055
3670669c
PB
6056int32_t HELPER(sdiv)(int32_t num, int32_t den)
6057{
6058 if (den == 0)
6059 return 0;
686eeb93
AJ
6060 if (num == INT_MIN && den == -1)
6061 return INT_MIN;
3670669c
PB
6062 return num / den;
6063}
6064
6065uint32_t HELPER(udiv)(uint32_t num, uint32_t den)
6066{
6067 if (den == 0)
6068 return 0;
6069 return num / den;
6070}
6071
6072uint32_t HELPER(rbit)(uint32_t x)
6073{
42fedbca 6074 return revbit32(x);
3670669c
PB
6075}
6076
5fafdf24 6077#if defined(CONFIG_USER_ONLY)
b5ff1b31 6078
9ee6e8bb 6079/* These should probably raise undefined insn exceptions. */
0ecb72a5 6080void HELPER(v7m_msr)(CPUARMState *env, uint32_t reg, uint32_t val)
9ee6e8bb 6081{
a47dddd7
AF
6082 ARMCPU *cpu = arm_env_get_cpu(env);
6083
6084 cpu_abort(CPU(cpu), "v7m_msr %d\n", reg);
9ee6e8bb
PB
6085}
6086
0ecb72a5 6087uint32_t HELPER(v7m_mrs)(CPUARMState *env, uint32_t reg)
9ee6e8bb 6088{
a47dddd7
AF
6089 ARMCPU *cpu = arm_env_get_cpu(env);
6090
6091 cpu_abort(CPU(cpu), "v7m_mrs %d\n", reg);
9ee6e8bb
PB
6092 return 0;
6093}
6094
fb602cb7
PM
6095void HELPER(v7m_bxns)(CPUARMState *env, uint32_t dest)
6096{
6097 /* translate.c should never generate calls here in user-only mode */
6098 g_assert_not_reached();
6099}
6100
3e3fa230
PM
6101void HELPER(v7m_blxns)(CPUARMState *env, uint32_t dest)
6102{
6103 /* translate.c should never generate calls here in user-only mode */
6104 g_assert_not_reached();
6105}
6106
5158de24
PM
6107uint32_t HELPER(v7m_tt)(CPUARMState *env, uint32_t addr, uint32_t op)
6108{
6109 /* The TT instructions can be used by unprivileged code, but in
6110 * user-only emulation we don't have the MPU.
6111 * Luckily since we know we are NonSecure unprivileged (and that in
6112 * turn means that the A flag wasn't specified), all the bits in the
6113 * register must be zero:
6114 * IREGION: 0 because IRVALID is 0
6115 * IRVALID: 0 because NS
6116 * S: 0 because NS
6117 * NSRW: 0 because NS
6118 * NSR: 0 because NS
6119 * RW: 0 because unpriv and A flag not set
6120 * R: 0 because unpriv and A flag not set
6121 * SRVALID: 0 because NS
6122 * MRVALID: 0 because unpriv and A flag not set
6123 * SREGION: 0 becaus SRVALID is 0
6124 * MREGION: 0 because MRVALID is 0
6125 */
6126 return 0;
6127}
6128
0ecb72a5 6129void switch_mode(CPUARMState *env, int mode)
b5ff1b31 6130{
a47dddd7
AF
6131 ARMCPU *cpu = arm_env_get_cpu(env);
6132
6133 if (mode != ARM_CPU_MODE_USR) {
6134 cpu_abort(CPU(cpu), "Tried to switch out of user mode\n");
6135 }
b5ff1b31
FB
6136}
6137
012a906b
GB
6138uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
6139 uint32_t cur_el, bool secure)
9e729b57
EI
6140{
6141 return 1;
6142}
6143
ce02049d
GB
6144void aarch64_sync_64_to_32(CPUARMState *env)
6145{
6146 g_assert_not_reached();
6147}
6148
b5ff1b31
FB
6149#else
6150
0ecb72a5 6151void switch_mode(CPUARMState *env, int mode)
b5ff1b31
FB
6152{
6153 int old_mode;
6154 int i;
6155
6156 old_mode = env->uncached_cpsr & CPSR_M;
6157 if (mode == old_mode)
6158 return;
6159
6160 if (old_mode == ARM_CPU_MODE_FIQ) {
6161 memcpy (env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t));
8637c67f 6162 memcpy (env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t));
b5ff1b31
FB
6163 } else if (mode == ARM_CPU_MODE_FIQ) {
6164 memcpy (env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t));
8637c67f 6165 memcpy (env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t));
b5ff1b31
FB
6166 }
6167
f5206413 6168 i = bank_number(old_mode);
b5ff1b31
FB
6169 env->banked_r13[i] = env->regs[13];
6170 env->banked_r14[i] = env->regs[14];
6171 env->banked_spsr[i] = env->spsr;
6172
f5206413 6173 i = bank_number(mode);
b5ff1b31
FB
6174 env->regs[13] = env->banked_r13[i];
6175 env->regs[14] = env->banked_r14[i];
6176 env->spsr = env->banked_spsr[i];
6177}
6178
0eeb17d6
GB
6179/* Physical Interrupt Target EL Lookup Table
6180 *
6181 * [ From ARM ARM section G1.13.4 (Table G1-15) ]
6182 *
6183 * The below multi-dimensional table is used for looking up the target
6184 * exception level given numerous condition criteria. Specifically, the
6185 * target EL is based on SCR and HCR routing controls as well as the
6186 * currently executing EL and secure state.
6187 *
6188 * Dimensions:
6189 * target_el_table[2][2][2][2][2][4]
6190 * | | | | | +--- Current EL
6191 * | | | | +------ Non-secure(0)/Secure(1)
6192 * | | | +--------- HCR mask override
6193 * | | +------------ SCR exec state control
6194 * | +--------------- SCR mask override
6195 * +------------------ 32-bit(0)/64-bit(1) EL3
6196 *
6197 * The table values are as such:
6198 * 0-3 = EL0-EL3
6199 * -1 = Cannot occur
6200 *
6201 * The ARM ARM target EL table includes entries indicating that an "exception
6202 * is not taken". The two cases where this is applicable are:
6203 * 1) An exception is taken from EL3 but the SCR does not have the exception
6204 * routed to EL3.
6205 * 2) An exception is taken from EL2 but the HCR does not have the exception
6206 * routed to EL2.
6207 * In these two cases, the below table contain a target of EL1. This value is
6208 * returned as it is expected that the consumer of the table data will check
6209 * for "target EL >= current EL" to ensure the exception is not taken.
6210 *
6211 * SCR HCR
6212 * 64 EA AMO From
6213 * BIT IRQ IMO Non-secure Secure
6214 * EL3 FIQ RW FMO EL0 EL1 EL2 EL3 EL0 EL1 EL2 EL3
6215 */
82c39f6a 6216static const int8_t target_el_table[2][2][2][2][2][4] = {
0eeb17d6
GB
6217 {{{{/* 0 0 0 0 */{ 1, 1, 2, -1 },{ 3, -1, -1, 3 },},
6218 {/* 0 0 0 1 */{ 2, 2, 2, -1 },{ 3, -1, -1, 3 },},},
6219 {{/* 0 0 1 0 */{ 1, 1, 2, -1 },{ 3, -1, -1, 3 },},
6220 {/* 0 0 1 1 */{ 2, 2, 2, -1 },{ 3, -1, -1, 3 },},},},
6221 {{{/* 0 1 0 0 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},
6222 {/* 0 1 0 1 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},},
6223 {{/* 0 1 1 0 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},
6224 {/* 0 1 1 1 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},},},},
6225 {{{{/* 1 0 0 0 */{ 1, 1, 2, -1 },{ 1, 1, -1, 1 },},
6226 {/* 1 0 0 1 */{ 2, 2, 2, -1 },{ 1, 1, -1, 1 },},},
6227 {{/* 1 0 1 0 */{ 1, 1, 1, -1 },{ 1, 1, -1, 1 },},
6228 {/* 1 0 1 1 */{ 2, 2, 2, -1 },{ 1, 1, -1, 1 },},},},
6229 {{{/* 1 1 0 0 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},
6230 {/* 1 1 0 1 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},},
6231 {{/* 1 1 1 0 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},
6232 {/* 1 1 1 1 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},},},},
6233};
6234
6235/*
6236 * Determine the target EL for physical exceptions
6237 */
012a906b
GB
6238uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
6239 uint32_t cur_el, bool secure)
0eeb17d6
GB
6240{
6241 CPUARMState *env = cs->env_ptr;
2cde031f 6242 int rw;
0eeb17d6
GB
6243 int scr;
6244 int hcr;
6245 int target_el;
2cde031f
SS
6246 /* Is the highest EL AArch64? */
6247 int is64 = arm_feature(env, ARM_FEATURE_AARCH64);
6248
6249 if (arm_feature(env, ARM_FEATURE_EL3)) {
6250 rw = ((env->cp15.scr_el3 & SCR_RW) == SCR_RW);
6251 } else {
6252 /* Either EL2 is the highest EL (and so the EL2 register width
6253 * is given by is64); or there is no EL2 or EL3, in which case
6254 * the value of 'rw' does not affect the table lookup anyway.
6255 */
6256 rw = is64;
6257 }
0eeb17d6
GB
6258
6259 switch (excp_idx) {
6260 case EXCP_IRQ:
6261 scr = ((env->cp15.scr_el3 & SCR_IRQ) == SCR_IRQ);
6262 hcr = ((env->cp15.hcr_el2 & HCR_IMO) == HCR_IMO);
6263 break;
6264 case EXCP_FIQ:
6265 scr = ((env->cp15.scr_el3 & SCR_FIQ) == SCR_FIQ);
6266 hcr = ((env->cp15.hcr_el2 & HCR_FMO) == HCR_FMO);
6267 break;
6268 default:
6269 scr = ((env->cp15.scr_el3 & SCR_EA) == SCR_EA);
6270 hcr = ((env->cp15.hcr_el2 & HCR_AMO) == HCR_AMO);
6271 break;
6272 };
6273
6274 /* If HCR.TGE is set then HCR is treated as being 1 */
6275 hcr |= ((env->cp15.hcr_el2 & HCR_TGE) == HCR_TGE);
6276
6277 /* Perform a table-lookup for the target EL given the current state */
6278 target_el = target_el_table[is64][scr][rw][hcr][secure][cur_el];
6279
6280 assert(target_el > 0);
6281
6282 return target_el;
6283}
6284
fd592d89
PM
6285static bool v7m_stack_write(ARMCPU *cpu, uint32_t addr, uint32_t value,
6286 ARMMMUIdx mmu_idx, bool ignfault)
9ee6e8bb 6287{
fd592d89
PM
6288 CPUState *cs = CPU(cpu);
6289 CPUARMState *env = &cpu->env;
6290 MemTxAttrs attrs = {};
6291 MemTxResult txres;
6292 target_ulong page_size;
6293 hwaddr physaddr;
6294 int prot;
6295 ARMMMUFaultInfo fi;
6296 bool secure = mmu_idx & ARM_MMU_IDX_M_S;
6297 int exc;
6298 bool exc_secure;
6299
6300 if (get_phys_addr(env, addr, MMU_DATA_STORE, mmu_idx, &physaddr,
6301 &attrs, &prot, &page_size, &fi, NULL)) {
6302 /* MPU/SAU lookup failed */
6303 if (fi.type == ARMFault_QEMU_SFault) {
6304 qemu_log_mask(CPU_LOG_INT,
6305 "...SecureFault with SFSR.AUVIOL during stacking\n");
6306 env->v7m.sfsr |= R_V7M_SFSR_AUVIOL_MASK | R_V7M_SFSR_SFARVALID_MASK;
6307 env->v7m.sfar = addr;
6308 exc = ARMV7M_EXCP_SECURE;
6309 exc_secure = false;
6310 } else {
6311 qemu_log_mask(CPU_LOG_INT, "...MemManageFault with CFSR.MSTKERR\n");
6312 env->v7m.cfsr[secure] |= R_V7M_CFSR_MSTKERR_MASK;
6313 exc = ARMV7M_EXCP_MEM;
6314 exc_secure = secure;
6315 }
6316 goto pend_fault;
6317 }
6318 address_space_stl_le(arm_addressspace(cs, attrs), physaddr, value,
6319 attrs, &txres);
6320 if (txres != MEMTX_OK) {
6321 /* BusFault trying to write the data */
6322 qemu_log_mask(CPU_LOG_INT, "...BusFault with BFSR.STKERR\n");
6323 env->v7m.cfsr[M_REG_NS] |= R_V7M_CFSR_STKERR_MASK;
6324 exc = ARMV7M_EXCP_BUS;
6325 exc_secure = false;
6326 goto pend_fault;
6327 }
6328 return true;
70d74660 6329
fd592d89
PM
6330pend_fault:
6331 /* By pending the exception at this point we are making
6332 * the IMPDEF choice "overridden exceptions pended" (see the
6333 * MergeExcInfo() pseudocode). The other choice would be to not
6334 * pend them now and then make a choice about which to throw away
6335 * later if we have two derived exceptions.
6336 * The only case when we must not pend the exception but instead
6337 * throw it away is if we are doing the push of the callee registers
6338 * and we've already generated a derived exception. Even in this
6339 * case we will still update the fault status registers.
6340 */
6341 if (!ignfault) {
6342 armv7m_nvic_set_pending_derived(env->nvic, exc, exc_secure);
6343 }
6344 return false;
9ee6e8bb
PB
6345}
6346
95695eff
PM
6347static bool v7m_stack_read(ARMCPU *cpu, uint32_t *dest, uint32_t addr,
6348 ARMMMUIdx mmu_idx)
6349{
6350 CPUState *cs = CPU(cpu);
6351 CPUARMState *env = &cpu->env;
6352 MemTxAttrs attrs = {};
6353 MemTxResult txres;
6354 target_ulong page_size;
6355 hwaddr physaddr;
6356 int prot;
6357 ARMMMUFaultInfo fi;
6358 bool secure = mmu_idx & ARM_MMU_IDX_M_S;
6359 int exc;
6360 bool exc_secure;
6361 uint32_t value;
6362
6363 if (get_phys_addr(env, addr, MMU_DATA_LOAD, mmu_idx, &physaddr,
6364 &attrs, &prot, &page_size, &fi, NULL)) {
6365 /* MPU/SAU lookup failed */
6366 if (fi.type == ARMFault_QEMU_SFault) {
6367 qemu_log_mask(CPU_LOG_INT,
6368 "...SecureFault with SFSR.AUVIOL during unstack\n");
6369 env->v7m.sfsr |= R_V7M_SFSR_AUVIOL_MASK | R_V7M_SFSR_SFARVALID_MASK;
6370 env->v7m.sfar = addr;
6371 exc = ARMV7M_EXCP_SECURE;
6372 exc_secure = false;
6373 } else {
6374 qemu_log_mask(CPU_LOG_INT,
6375 "...MemManageFault with CFSR.MUNSTKERR\n");
6376 env->v7m.cfsr[secure] |= R_V7M_CFSR_MUNSTKERR_MASK;
6377 exc = ARMV7M_EXCP_MEM;
6378 exc_secure = secure;
6379 }
6380 goto pend_fault;
6381 }
6382
6383 value = address_space_ldl(arm_addressspace(cs, attrs), physaddr,
6384 attrs, &txres);
6385 if (txres != MEMTX_OK) {
6386 /* BusFault trying to read the data */
6387 qemu_log_mask(CPU_LOG_INT, "...BusFault with BFSR.UNSTKERR\n");
6388 env->v7m.cfsr[M_REG_NS] |= R_V7M_CFSR_UNSTKERR_MASK;
6389 exc = ARMV7M_EXCP_BUS;
6390 exc_secure = false;
6391 goto pend_fault;
6392 }
6393
6394 *dest = value;
6395 return true;
6396
6397pend_fault:
6398 /* By pending the exception at this point we are making
6399 * the IMPDEF choice "overridden exceptions pended" (see the
6400 * MergeExcInfo() pseudocode). The other choice would be to not
6401 * pend them now and then make a choice about which to throw away
6402 * later if we have two derived exceptions.
6403 */
6404 armv7m_nvic_set_pending(env->nvic, exc, exc_secure);
6405 return false;
6406}
6407
fb602cb7
PM
6408/* Return true if we're using the process stack pointer (not the MSP) */
6409static bool v7m_using_psp(CPUARMState *env)
6410{
6411 /* Handler mode always uses the main stack; for thread mode
6412 * the CONTROL.SPSEL bit determines the answer.
6413 * Note that in v7M it is not possible to be in Handler mode with
6414 * CONTROL.SPSEL non-zero, but in v8M it is, so we must check both.
6415 */
6416 return !arm_v7m_is_handler_mode(env) &&
6417 env->v7m.control[env->v7m.secure] & R_V7M_CONTROL_SPSEL_MASK;
6418}
6419
3f0cddee
PM
6420/* Write to v7M CONTROL.SPSEL bit for the specified security bank.
6421 * This may change the current stack pointer between Main and Process
6422 * stack pointers if it is done for the CONTROL register for the current
6423 * security state.
de2db7ec 6424 */
3f0cddee
PM
6425static void write_v7m_control_spsel_for_secstate(CPUARMState *env,
6426 bool new_spsel,
6427 bool secstate)
9ee6e8bb 6428{
3f0cddee 6429 bool old_is_psp = v7m_using_psp(env);
de2db7ec 6430
3f0cddee
PM
6431 env->v7m.control[secstate] =
6432 deposit32(env->v7m.control[secstate],
de2db7ec
PM
6433 R_V7M_CONTROL_SPSEL_SHIFT,
6434 R_V7M_CONTROL_SPSEL_LENGTH, new_spsel);
6435
3f0cddee
PM
6436 if (secstate == env->v7m.secure) {
6437 bool new_is_psp = v7m_using_psp(env);
6438 uint32_t tmp;
abc24d86 6439
3f0cddee
PM
6440 if (old_is_psp != new_is_psp) {
6441 tmp = env->v7m.other_sp;
6442 env->v7m.other_sp = env->regs[13];
6443 env->regs[13] = tmp;
6444 }
de2db7ec
PM
6445 }
6446}
6447
3f0cddee
PM
6448/* Write to v7M CONTROL.SPSEL bit. This may change the current
6449 * stack pointer between Main and Process stack pointers.
6450 */
6451static void write_v7m_control_spsel(CPUARMState *env, bool new_spsel)
6452{
6453 write_v7m_control_spsel_for_secstate(env, new_spsel, env->v7m.secure);
6454}
6455
de2db7ec
PM
6456void write_v7m_exception(CPUARMState *env, uint32_t new_exc)
6457{
6458 /* Write a new value to v7m.exception, thus transitioning into or out
6459 * of Handler mode; this may result in a change of active stack pointer.
6460 */
6461 bool new_is_psp, old_is_psp = v7m_using_psp(env);
6462 uint32_t tmp;
abc24d86 6463
de2db7ec
PM
6464 env->v7m.exception = new_exc;
6465
6466 new_is_psp = v7m_using_psp(env);
6467
6468 if (old_is_psp != new_is_psp) {
6469 tmp = env->v7m.other_sp;
6470 env->v7m.other_sp = env->regs[13];
6471 env->regs[13] = tmp;
9ee6e8bb
PB
6472 }
6473}
6474
fb602cb7
PM
6475/* Switch M profile security state between NS and S */
6476static void switch_v7m_security_state(CPUARMState *env, bool new_secstate)
6477{
6478 uint32_t new_ss_msp, new_ss_psp;
6479
6480 if (env->v7m.secure == new_secstate) {
6481 return;
6482 }
6483
6484 /* All the banked state is accessed by looking at env->v7m.secure
6485 * except for the stack pointer; rearrange the SP appropriately.
6486 */
6487 new_ss_msp = env->v7m.other_ss_msp;
6488 new_ss_psp = env->v7m.other_ss_psp;
6489
6490 if (v7m_using_psp(env)) {
6491 env->v7m.other_ss_psp = env->regs[13];
6492 env->v7m.other_ss_msp = env->v7m.other_sp;
6493 } else {
6494 env->v7m.other_ss_msp = env->regs[13];
6495 env->v7m.other_ss_psp = env->v7m.other_sp;
6496 }
6497
6498 env->v7m.secure = new_secstate;
6499
6500 if (v7m_using_psp(env)) {
6501 env->regs[13] = new_ss_psp;
6502 env->v7m.other_sp = new_ss_msp;
6503 } else {
6504 env->regs[13] = new_ss_msp;
6505 env->v7m.other_sp = new_ss_psp;
6506 }
6507}
6508
6509void HELPER(v7m_bxns)(CPUARMState *env, uint32_t dest)
6510{
6511 /* Handle v7M BXNS:
6512 * - if the return value is a magic value, do exception return (like BX)
6513 * - otherwise bit 0 of the return value is the target security state
6514 */
d02a8698
PM
6515 uint32_t min_magic;
6516
6517 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
6518 /* Covers FNC_RETURN and EXC_RETURN magic */
6519 min_magic = FNC_RETURN_MIN_MAGIC;
6520 } else {
6521 /* EXC_RETURN magic only */
6522 min_magic = EXC_RETURN_MIN_MAGIC;
6523 }
6524
6525 if (dest >= min_magic) {
fb602cb7
PM
6526 /* This is an exception return magic value; put it where
6527 * do_v7m_exception_exit() expects and raise EXCEPTION_EXIT.
6528 * Note that if we ever add gen_ss_advance() singlestep support to
6529 * M profile this should count as an "instruction execution complete"
6530 * event (compare gen_bx_excret_final_code()).
6531 */
6532 env->regs[15] = dest & ~1;
6533 env->thumb = dest & 1;
6534 HELPER(exception_internal)(env, EXCP_EXCEPTION_EXIT);
6535 /* notreached */
6536 }
6537
6538 /* translate.c should have made BXNS UNDEF unless we're secure */
6539 assert(env->v7m.secure);
6540
6541 switch_v7m_security_state(env, dest & 1);
6542 env->thumb = 1;
6543 env->regs[15] = dest & ~1;
6544}
6545
3e3fa230
PM
6546void HELPER(v7m_blxns)(CPUARMState *env, uint32_t dest)
6547{
6548 /* Handle v7M BLXNS:
6549 * - bit 0 of the destination address is the target security state
6550 */
6551
6552 /* At this point regs[15] is the address just after the BLXNS */
6553 uint32_t nextinst = env->regs[15] | 1;
6554 uint32_t sp = env->regs[13] - 8;
6555 uint32_t saved_psr;
6556
6557 /* translate.c will have made BLXNS UNDEF unless we're secure */
6558 assert(env->v7m.secure);
6559
6560 if (dest & 1) {
6561 /* target is Secure, so this is just a normal BLX,
6562 * except that the low bit doesn't indicate Thumb/not.
6563 */
6564 env->regs[14] = nextinst;
6565 env->thumb = 1;
6566 env->regs[15] = dest & ~1;
6567 return;
6568 }
6569
6570 /* Target is non-secure: first push a stack frame */
6571 if (!QEMU_IS_ALIGNED(sp, 8)) {
6572 qemu_log_mask(LOG_GUEST_ERROR,
6573 "BLXNS with misaligned SP is UNPREDICTABLE\n");
6574 }
6575
6576 saved_psr = env->v7m.exception;
6577 if (env->v7m.control[M_REG_S] & R_V7M_CONTROL_SFPA_MASK) {
6578 saved_psr |= XPSR_SFPA;
6579 }
6580
6581 /* Note that these stores can throw exceptions on MPU faults */
6582 cpu_stl_data(env, sp, nextinst);
6583 cpu_stl_data(env, sp + 4, saved_psr);
6584
6585 env->regs[13] = sp;
6586 env->regs[14] = 0xfeffffff;
6587 if (arm_v7m_is_handler_mode(env)) {
6588 /* Write a dummy value to IPSR, to avoid leaking the current secure
6589 * exception number to non-secure code. This is guaranteed not
6590 * to cause write_v7m_exception() to actually change stacks.
6591 */
6592 write_v7m_exception(env, 1);
6593 }
6594 switch_v7m_security_state(env, 0);
6595 env->thumb = 1;
6596 env->regs[15] = dest;
6597}
6598
5b522399
PM
6599static uint32_t *get_v7m_sp_ptr(CPUARMState *env, bool secure, bool threadmode,
6600 bool spsel)
6601{
6602 /* Return a pointer to the location where we currently store the
6603 * stack pointer for the requested security state and thread mode.
6604 * This pointer will become invalid if the CPU state is updated
6605 * such that the stack pointers are switched around (eg changing
6606 * the SPSEL control bit).
6607 * Compare the v8M ARM ARM pseudocode LookUpSP_with_security_mode().
6608 * Unlike that pseudocode, we require the caller to pass us in the
6609 * SPSEL control bit value; this is because we also use this
6610 * function in handling of pushing of the callee-saves registers
6611 * part of the v8M stack frame (pseudocode PushCalleeStack()),
6612 * and in the tailchain codepath the SPSEL bit comes from the exception
6613 * return magic LR value from the previous exception. The pseudocode
6614 * opencodes the stack-selection in PushCalleeStack(), but we prefer
6615 * to make this utility function generic enough to do the job.
6616 */
6617 bool want_psp = threadmode && spsel;
6618
6619 if (secure == env->v7m.secure) {
de2db7ec
PM
6620 if (want_psp == v7m_using_psp(env)) {
6621 return &env->regs[13];
6622 } else {
6623 return &env->v7m.other_sp;
6624 }
5b522399
PM
6625 } else {
6626 if (want_psp) {
6627 return &env->v7m.other_ss_psp;
6628 } else {
6629 return &env->v7m.other_ss_msp;
6630 }
6631 }
6632}
6633
600c33f2
PM
6634static bool arm_v7m_load_vector(ARMCPU *cpu, int exc, bool targets_secure,
6635 uint32_t *pvec)
39ae2474
PM
6636{
6637 CPUState *cs = CPU(cpu);
6638 CPUARMState *env = &cpu->env;
6639 MemTxResult result;
600c33f2
PM
6640 uint32_t addr = env->v7m.vecbase[targets_secure] + exc * 4;
6641 uint32_t vector_entry;
6642 MemTxAttrs attrs = {};
6643 ARMMMUIdx mmu_idx;
6644 bool exc_secure;
6645
6646 mmu_idx = arm_v7m_mmu_idx_for_secstate_and_priv(env, targets_secure, true);
39ae2474 6647
600c33f2
PM
6648 /* We don't do a get_phys_addr() here because the rules for vector
6649 * loads are special: they always use the default memory map, and
6650 * the default memory map permits reads from all addresses.
6651 * Since there's no easy way to pass through to pmsav8_mpu_lookup()
6652 * that we want this special case which would always say "yes",
6653 * we just do the SAU lookup here followed by a direct physical load.
6654 */
6655 attrs.secure = targets_secure;
6656 attrs.user = false;
6657
6658 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
6659 V8M_SAttributes sattrs = {};
6660
6661 v8m_security_lookup(env, addr, MMU_DATA_LOAD, mmu_idx, &sattrs);
6662 if (sattrs.ns) {
6663 attrs.secure = false;
6664 } else if (!targets_secure) {
6665 /* NS access to S memory */
6666 goto load_fail;
6667 }
6668 }
6669
6670 vector_entry = address_space_ldl(arm_addressspace(cs, attrs), addr,
6671 attrs, &result);
39ae2474 6672 if (result != MEMTX_OK) {
600c33f2 6673 goto load_fail;
39ae2474 6674 }
600c33f2
PM
6675 *pvec = vector_entry;
6676 return true;
6677
6678load_fail:
6679 /* All vector table fetch fails are reported as HardFault, with
6680 * HFSR.VECTTBL and .FORCED set. (FORCED is set because
6681 * technically the underlying exception is a MemManage or BusFault
6682 * that is escalated to HardFault.) This is a terminal exception,
6683 * so we will either take the HardFault immediately or else enter
6684 * lockup (the latter case is handled in armv7m_nvic_set_pending_derived()).
6685 */
6686 exc_secure = targets_secure ||
6687 !(cpu->env.v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK);
6688 env->v7m.hfsr |= R_V7M_HFSR_VECTTBL_MASK | R_V7M_HFSR_FORCED_MASK;
6689 armv7m_nvic_set_pending_derived(env->nvic, ARMV7M_EXCP_HARD, exc_secure);
6690 return false;
39ae2474
PM
6691}
6692
65b4234f 6693static bool v7m_push_callee_stack(ARMCPU *cpu, uint32_t lr, bool dotailchain,
0094ca70 6694 bool ignore_faults)
d3392718
PM
6695{
6696 /* For v8M, push the callee-saves register part of the stack frame.
6697 * Compare the v8M pseudocode PushCalleeStack().
6698 * In the tailchaining case this may not be the current stack.
6699 */
6700 CPUARMState *env = &cpu->env;
d3392718
PM
6701 uint32_t *frame_sp_p;
6702 uint32_t frameptr;
65b4234f
PM
6703 ARMMMUIdx mmu_idx;
6704 bool stacked_ok;
d3392718
PM
6705
6706 if (dotailchain) {
65b4234f
PM
6707 bool mode = lr & R_V7M_EXCRET_MODE_MASK;
6708 bool priv = !(env->v7m.control[M_REG_S] & R_V7M_CONTROL_NPRIV_MASK) ||
6709 !mode;
6710
6711 mmu_idx = arm_v7m_mmu_idx_for_secstate_and_priv(env, M_REG_S, priv);
6712 frame_sp_p = get_v7m_sp_ptr(env, M_REG_S, mode,
d3392718
PM
6713 lr & R_V7M_EXCRET_SPSEL_MASK);
6714 } else {
65b4234f 6715 mmu_idx = core_to_arm_mmu_idx(env, cpu_mmu_index(env, false));
d3392718
PM
6716 frame_sp_p = &env->regs[13];
6717 }
6718
6719 frameptr = *frame_sp_p - 0x28;
6720
65b4234f
PM
6721 /* Write as much of the stack frame as we can. A write failure may
6722 * cause us to pend a derived exception.
6723 */
6724 stacked_ok =
6725 v7m_stack_write(cpu, frameptr, 0xfefa125b, mmu_idx, ignore_faults) &&
6726 v7m_stack_write(cpu, frameptr + 0x8, env->regs[4], mmu_idx,
6727 ignore_faults) &&
6728 v7m_stack_write(cpu, frameptr + 0xc, env->regs[5], mmu_idx,
6729 ignore_faults) &&
6730 v7m_stack_write(cpu, frameptr + 0x10, env->regs[6], mmu_idx,
6731 ignore_faults) &&
6732 v7m_stack_write(cpu, frameptr + 0x14, env->regs[7], mmu_idx,
6733 ignore_faults) &&
6734 v7m_stack_write(cpu, frameptr + 0x18, env->regs[8], mmu_idx,
6735 ignore_faults) &&
6736 v7m_stack_write(cpu, frameptr + 0x1c, env->regs[9], mmu_idx,
6737 ignore_faults) &&
6738 v7m_stack_write(cpu, frameptr + 0x20, env->regs[10], mmu_idx,
6739 ignore_faults) &&
6740 v7m_stack_write(cpu, frameptr + 0x24, env->regs[11], mmu_idx,
6741 ignore_faults);
6742
6743 /* Update SP regardless of whether any of the stack accesses failed.
6744 * When we implement v8M stack limit checking then this attempt to
6745 * update SP might also fail and result in a derived exception.
6746 */
d3392718 6747 *frame_sp_p = frameptr;
65b4234f
PM
6748
6749 return !stacked_ok;
d3392718
PM
6750}
6751
0094ca70
PM
6752static void v7m_exception_taken(ARMCPU *cpu, uint32_t lr, bool dotailchain,
6753 bool ignore_stackfaults)
39ae2474
PM
6754{
6755 /* Do the "take the exception" parts of exception entry,
6756 * but not the pushing of state to the stack. This is
6757 * similar to the pseudocode ExceptionTaken() function.
6758 */
6759 CPUARMState *env = &cpu->env;
6760 uint32_t addr;
d3392718 6761 bool targets_secure;
6c948518 6762 int exc;
65b4234f 6763 bool push_failed = false;
d3392718 6764
6c948518 6765 armv7m_nvic_get_pending_irq_info(env->nvic, &exc, &targets_secure);
d3392718
PM
6766
6767 if (arm_feature(env, ARM_FEATURE_V8)) {
6768 if (arm_feature(env, ARM_FEATURE_M_SECURITY) &&
6769 (lr & R_V7M_EXCRET_S_MASK)) {
6770 /* The background code (the owner of the registers in the
6771 * exception frame) is Secure. This means it may either already
6772 * have or now needs to push callee-saves registers.
6773 */
6774 if (targets_secure) {
6775 if (dotailchain && !(lr & R_V7M_EXCRET_ES_MASK)) {
6776 /* We took an exception from Secure to NonSecure
6777 * (which means the callee-saved registers got stacked)
6778 * and are now tailchaining to a Secure exception.
6779 * Clear DCRS so eventual return from this Secure
6780 * exception unstacks the callee-saved registers.
6781 */
6782 lr &= ~R_V7M_EXCRET_DCRS_MASK;
6783 }
6784 } else {
6785 /* We're going to a non-secure exception; push the
6786 * callee-saves registers to the stack now, if they're
6787 * not already saved.
6788 */
6789 if (lr & R_V7M_EXCRET_DCRS_MASK &&
6790 !(dotailchain && (lr & R_V7M_EXCRET_ES_MASK))) {
65b4234f
PM
6791 push_failed = v7m_push_callee_stack(cpu, lr, dotailchain,
6792 ignore_stackfaults);
d3392718
PM
6793 }
6794 lr |= R_V7M_EXCRET_DCRS_MASK;
6795 }
6796 }
6797
6798 lr &= ~R_V7M_EXCRET_ES_MASK;
6799 if (targets_secure || !arm_feature(env, ARM_FEATURE_M_SECURITY)) {
6800 lr |= R_V7M_EXCRET_ES_MASK;
6801 }
6802 lr &= ~R_V7M_EXCRET_SPSEL_MASK;
6803 if (env->v7m.control[targets_secure] & R_V7M_CONTROL_SPSEL_MASK) {
6804 lr |= R_V7M_EXCRET_SPSEL_MASK;
6805 }
6806
6807 /* Clear registers if necessary to prevent non-secure exception
6808 * code being able to see register values from secure code.
6809 * Where register values become architecturally UNKNOWN we leave
6810 * them with their previous values.
6811 */
6812 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
6813 if (!targets_secure) {
6814 /* Always clear the caller-saved registers (they have been
6815 * pushed to the stack earlier in v7m_push_stack()).
6816 * Clear callee-saved registers if the background code is
6817 * Secure (in which case these regs were saved in
6818 * v7m_push_callee_stack()).
6819 */
6820 int i;
6821
6822 for (i = 0; i < 13; i++) {
6823 /* r4..r11 are callee-saves, zero only if EXCRET.S == 1 */
6824 if (i < 4 || i > 11 || (lr & R_V7M_EXCRET_S_MASK)) {
6825 env->regs[i] = 0;
6826 }
6827 }
6828 /* Clear EAPSR */
6829 xpsr_write(env, 0, XPSR_NZCV | XPSR_Q | XPSR_GE | XPSR_IT);
6830 }
6831 }
6832 }
39ae2474 6833
65b4234f
PM
6834 if (push_failed && !ignore_stackfaults) {
6835 /* Derived exception on callee-saves register stacking:
6836 * we might now want to take a different exception which
6837 * targets a different security state, so try again from the top.
6838 */
6839 v7m_exception_taken(cpu, lr, true, true);
6840 return;
6841 }
6842
600c33f2
PM
6843 if (!arm_v7m_load_vector(cpu, exc, targets_secure, &addr)) {
6844 /* Vector load failed: derived exception */
6845 v7m_exception_taken(cpu, lr, true, true);
6846 return;
6847 }
6c948518
PM
6848
6849 /* Now we've done everything that might cause a derived exception
6850 * we can go ahead and activate whichever exception we're going to
6851 * take (which might now be the derived exception).
6852 */
6853 armv7m_nvic_acknowledge_irq(env->nvic);
6854
d3392718
PM
6855 /* Switch to target security state -- must do this before writing SPSEL */
6856 switch_v7m_security_state(env, targets_secure);
de2db7ec 6857 write_v7m_control_spsel(env, 0);
dc3c4c14 6858 arm_clear_exclusive(env);
39ae2474
PM
6859 /* Clear IT bits */
6860 env->condexec_bits = 0;
6861 env->regs[14] = lr;
39ae2474
PM
6862 env->regs[15] = addr & 0xfffffffe;
6863 env->thumb = addr & 1;
6864}
6865
0094ca70 6866static bool v7m_push_stack(ARMCPU *cpu)
39ae2474
PM
6867{
6868 /* Do the "set up stack frame" part of exception entry,
6869 * similar to pseudocode PushStack().
0094ca70
PM
6870 * Return true if we generate a derived exception (and so
6871 * should ignore further stack faults trying to process
6872 * that derived exception.)
39ae2474 6873 */
fd592d89 6874 bool stacked_ok;
39ae2474
PM
6875 CPUARMState *env = &cpu->env;
6876 uint32_t xpsr = xpsr_read(env);
fd592d89
PM
6877 uint32_t frameptr = env->regs[13];
6878 ARMMMUIdx mmu_idx = core_to_arm_mmu_idx(env, cpu_mmu_index(env, false));
39ae2474
PM
6879
6880 /* Align stack pointer if the guest wants that */
fd592d89 6881 if ((frameptr & 4) &&
9d40cd8a 6882 (env->v7m.ccr[env->v7m.secure] & R_V7M_CCR_STKALIGN_MASK)) {
fd592d89 6883 frameptr -= 4;
987ab45e 6884 xpsr |= XPSR_SPREALIGN;
39ae2474 6885 }
0094ca70 6886
fd592d89
PM
6887 frameptr -= 0x20;
6888
6889 /* Write as much of the stack frame as we can. If we fail a stack
6890 * write this will result in a derived exception being pended
6891 * (which may be taken in preference to the one we started with
6892 * if it has higher priority).
6893 */
6894 stacked_ok =
6895 v7m_stack_write(cpu, frameptr, env->regs[0], mmu_idx, false) &&
6896 v7m_stack_write(cpu, frameptr + 4, env->regs[1], mmu_idx, false) &&
6897 v7m_stack_write(cpu, frameptr + 8, env->regs[2], mmu_idx, false) &&
6898 v7m_stack_write(cpu, frameptr + 12, env->regs[3], mmu_idx, false) &&
6899 v7m_stack_write(cpu, frameptr + 16, env->regs[12], mmu_idx, false) &&
6900 v7m_stack_write(cpu, frameptr + 20, env->regs[14], mmu_idx, false) &&
6901 v7m_stack_write(cpu, frameptr + 24, env->regs[15], mmu_idx, false) &&
6902 v7m_stack_write(cpu, frameptr + 28, xpsr, mmu_idx, false);
6903
6904 /* Update SP regardless of whether any of the stack accesses failed.
6905 * When we implement v8M stack limit checking then this attempt to
6906 * update SP might also fail and result in a derived exception.
6907 */
6908 env->regs[13] = frameptr;
6909
6910 return !stacked_ok;
39ae2474
PM
6911}
6912
aa488fe3 6913static void do_v7m_exception_exit(ARMCPU *cpu)
9ee6e8bb 6914{
aa488fe3 6915 CPUARMState *env = &cpu->env;
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;
4818bad9
PM
7114 uint32_t actual_sig;
7115
7116 pop_ok = v7m_stack_read(cpu, &actual_sig, frameptr, mmu_idx);
907bedb3 7117
4818bad9 7118 if (pop_ok && expected_sig != actual_sig) {
907bedb3
PM
7119 /* Take a SecureFault on the current stack */
7120 env->v7m.sfsr |= R_V7M_SFSR_INVIS_MASK;
7121 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SECURE, false);
0094ca70 7122 v7m_exception_taken(cpu, excret, true, false);
907bedb3
PM
7123 qemu_log_mask(CPU_LOG_INT, "...taking SecureFault on existing "
7124 "stackframe: failed exception return integrity "
7125 "signature check\n");
7126 return;
7127 }
7128
4818bad9 7129 pop_ok = pop_ok &&
95695eff
PM
7130 v7m_stack_read(cpu, &env->regs[4], frameptr + 0x8, mmu_idx) &&
7131 v7m_stack_read(cpu, &env->regs[4], frameptr + 0x8, mmu_idx) &&
7132 v7m_stack_read(cpu, &env->regs[5], frameptr + 0xc, mmu_idx) &&
7133 v7m_stack_read(cpu, &env->regs[6], frameptr + 0x10, mmu_idx) &&
7134 v7m_stack_read(cpu, &env->regs[7], frameptr + 0x14, mmu_idx) &&
7135 v7m_stack_read(cpu, &env->regs[8], frameptr + 0x18, mmu_idx) &&
7136 v7m_stack_read(cpu, &env->regs[9], frameptr + 0x1c, mmu_idx) &&
7137 v7m_stack_read(cpu, &env->regs[10], frameptr + 0x20, mmu_idx) &&
7138 v7m_stack_read(cpu, &env->regs[11], frameptr + 0x24, mmu_idx);
907bedb3
PM
7139
7140 frameptr += 0x28;
7141 }
7142
95695eff
PM
7143 /* Pop registers */
7144 pop_ok = pop_ok &&
7145 v7m_stack_read(cpu, &env->regs[0], frameptr, mmu_idx) &&
7146 v7m_stack_read(cpu, &env->regs[1], frameptr + 0x4, mmu_idx) &&
7147 v7m_stack_read(cpu, &env->regs[2], frameptr + 0x8, mmu_idx) &&
7148 v7m_stack_read(cpu, &env->regs[3], frameptr + 0xc, mmu_idx) &&
7149 v7m_stack_read(cpu, &env->regs[12], frameptr + 0x10, mmu_idx) &&
7150 v7m_stack_read(cpu, &env->regs[14], frameptr + 0x14, mmu_idx) &&
7151 v7m_stack_read(cpu, &env->regs[15], frameptr + 0x18, mmu_idx) &&
7152 v7m_stack_read(cpu, &xpsr, frameptr + 0x1c, mmu_idx);
7153
7154 if (!pop_ok) {
7155 /* v7m_stack_read() pended a fault, so take it (as a tail
7156 * chained exception on the same stack frame)
7157 */
7158 v7m_exception_taken(cpu, excret, true, false);
7159 return;
7160 }
4e4259d3
PM
7161
7162 /* Returning from an exception with a PC with bit 0 set is defined
7163 * behaviour on v8M (bit 0 is ignored), but for v7M it was specified
7164 * to be UNPREDICTABLE. In practice actual v7M hardware seems to ignore
7165 * the lsbit, and there are several RTOSes out there which incorrectly
7166 * assume the r15 in the stack frame should be a Thumb-style "lsbit
7167 * indicates ARM/Thumb" value, so ignore the bit on v7M as well, but
7168 * complain about the badly behaved guest.
7169 */
5b522399 7170 if (env->regs[15] & 1) {
5b522399 7171 env->regs[15] &= ~1U;
4e4259d3
PM
7172 if (!arm_feature(env, ARM_FEATURE_V8)) {
7173 qemu_log_mask(LOG_GUEST_ERROR,
7174 "M profile return from interrupt with misaligned "
7175 "PC is UNPREDICTABLE on v7M\n");
7176 }
5b522399 7177 }
4e4259d3 7178
224e0c30
PM
7179 if (arm_feature(env, ARM_FEATURE_V8)) {
7180 /* For v8M we have to check whether the xPSR exception field
7181 * matches the EXCRET value for return to handler/thread
7182 * before we commit to changing the SP and xPSR.
7183 */
7184 bool will_be_handler = (xpsr & XPSR_EXCP) != 0;
7185 if (return_to_handler != will_be_handler) {
7186 /* Take an INVPC UsageFault on the current stack.
7187 * By this point we will have switched to the security state
7188 * for the background state, so this UsageFault will target
7189 * that state.
7190 */
7191 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE,
7192 env->v7m.secure);
7193 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_INVPC_MASK;
0094ca70 7194 v7m_exception_taken(cpu, excret, true, false);
224e0c30
PM
7195 qemu_log_mask(CPU_LOG_INT, "...taking UsageFault on existing "
7196 "stackframe: failed exception return integrity "
7197 "check\n");
7198 return;
7199 }
7200 }
7201
5b522399
PM
7202 /* Commit to consuming the stack frame */
7203 frameptr += 0x20;
7204 /* Undo stack alignment (the SPREALIGN bit indicates that the original
7205 * pre-exception SP was not 8-aligned and we added a padding word to
7206 * align it, so we undo this by ORing in the bit that increases it
7207 * from the current 8-aligned value to the 8-unaligned value. (Adding 4
7208 * would work too but a logical OR is how the pseudocode specifies it.)
7209 */
7210 if (xpsr & XPSR_SPREALIGN) {
7211 frameptr |= 4;
7212 }
7213 *frame_sp_p = frameptr;
fcf83ab1 7214 }
5b522399 7215 /* This xpsr_write() will invalidate frame_sp_p as it may switch stack */
987ab45e 7216 xpsr_write(env, xpsr, ~XPSR_SPREALIGN);
aa488fe3
PM
7217
7218 /* The restored xPSR exception field will be zero if we're
7219 * resuming in Thread mode. If that doesn't match what the
351e527a 7220 * exception return excret specified then this is a UsageFault.
224e0c30 7221 * v7M requires we make this check here; v8M did it earlier.
aa488fe3 7222 */
15b3f556 7223 if (return_to_handler != arm_v7m_is_handler_mode(env)) {
224e0c30
PM
7224 /* Take an INVPC UsageFault by pushing the stack again;
7225 * we know we're v7M so this is never a Secure UsageFault.
2fb50a33 7226 */
0094ca70
PM
7227 bool ignore_stackfaults;
7228
224e0c30 7229 assert(!arm_feature(env, ARM_FEATURE_V8));
2fb50a33 7230 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, false);
334e8dad 7231 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_INVPC_MASK;
0094ca70
PM
7232 ignore_stackfaults = v7m_push_stack(cpu);
7233 v7m_exception_taken(cpu, excret, false, ignore_stackfaults);
aa488fe3
PM
7234 qemu_log_mask(CPU_LOG_INT, "...taking UsageFault on new stackframe: "
7235 "failed exception return integrity check\n");
7236 return;
7237 }
7238
7239 /* Otherwise, we have a successful exception exit. */
dc3c4c14 7240 arm_clear_exclusive(env);
aa488fe3 7241 qemu_log_mask(CPU_LOG_INT, "...successful exception return\n");
9ee6e8bb
PB
7242}
7243
d02a8698
PM
7244static bool do_v7m_function_return(ARMCPU *cpu)
7245{
7246 /* v8M security extensions magic function return.
7247 * We may either:
7248 * (1) throw an exception (longjump)
7249 * (2) return true if we successfully handled the function return
7250 * (3) return false if we failed a consistency check and have
7251 * pended a UsageFault that needs to be taken now
7252 *
7253 * At this point the magic return value is split between env->regs[15]
7254 * and env->thumb. We don't bother to reconstitute it because we don't
7255 * need it (all values are handled the same way).
7256 */
7257 CPUARMState *env = &cpu->env;
7258 uint32_t newpc, newpsr, newpsr_exc;
7259
7260 qemu_log_mask(CPU_LOG_INT, "...really v7M secure function return\n");
7261
7262 {
7263 bool threadmode, spsel;
7264 TCGMemOpIdx oi;
7265 ARMMMUIdx mmu_idx;
7266 uint32_t *frame_sp_p;
7267 uint32_t frameptr;
7268
7269 /* Pull the return address and IPSR from the Secure stack */
7270 threadmode = !arm_v7m_is_handler_mode(env);
7271 spsel = env->v7m.control[M_REG_S] & R_V7M_CONTROL_SPSEL_MASK;
7272
7273 frame_sp_p = get_v7m_sp_ptr(env, true, threadmode, spsel);
7274 frameptr = *frame_sp_p;
7275
7276 /* These loads may throw an exception (for MPU faults). We want to
7277 * do them as secure, so work out what MMU index that is.
7278 */
7279 mmu_idx = arm_v7m_mmu_idx_for_secstate(env, true);
7280 oi = make_memop_idx(MO_LE, arm_to_core_mmu_idx(mmu_idx));
7281 newpc = helper_le_ldul_mmu(env, frameptr, oi, 0);
7282 newpsr = helper_le_ldul_mmu(env, frameptr + 4, oi, 0);
7283
7284 /* Consistency checks on new IPSR */
7285 newpsr_exc = newpsr & XPSR_EXCP;
7286 if (!((env->v7m.exception == 0 && newpsr_exc == 0) ||
7287 (env->v7m.exception == 1 && newpsr_exc != 0))) {
7288 /* Pend the fault and tell our caller to take it */
7289 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_INVPC_MASK;
7290 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE,
7291 env->v7m.secure);
7292 qemu_log_mask(CPU_LOG_INT,
7293 "...taking INVPC UsageFault: "
7294 "IPSR consistency check failed\n");
7295 return false;
7296 }
7297
7298 *frame_sp_p = frameptr + 8;
7299 }
7300
7301 /* This invalidates frame_sp_p */
7302 switch_v7m_security_state(env, true);
7303 env->v7m.exception = newpsr_exc;
7304 env->v7m.control[M_REG_S] &= ~R_V7M_CONTROL_SFPA_MASK;
7305 if (newpsr & XPSR_SFPA) {
7306 env->v7m.control[M_REG_S] |= R_V7M_CONTROL_SFPA_MASK;
7307 }
7308 xpsr_write(env, 0, XPSR_IT);
7309 env->thumb = newpc & 1;
7310 env->regs[15] = newpc & ~1;
7311
7312 qemu_log_mask(CPU_LOG_INT, "...function return successful\n");
7313 return true;
7314}
7315
27a7ea8a
PB
7316static void arm_log_exception(int idx)
7317{
7318 if (qemu_loglevel_mask(CPU_LOG_INT)) {
7319 const char *exc = NULL;
2c4a7cc5
PM
7320 static const char * const excnames[] = {
7321 [EXCP_UDEF] = "Undefined Instruction",
7322 [EXCP_SWI] = "SVC",
7323 [EXCP_PREFETCH_ABORT] = "Prefetch Abort",
7324 [EXCP_DATA_ABORT] = "Data Abort",
7325 [EXCP_IRQ] = "IRQ",
7326 [EXCP_FIQ] = "FIQ",
7327 [EXCP_BKPT] = "Breakpoint",
7328 [EXCP_EXCEPTION_EXIT] = "QEMU v7M exception exit",
7329 [EXCP_KERNEL_TRAP] = "QEMU intercept of kernel commpage",
7330 [EXCP_HVC] = "Hypervisor Call",
7331 [EXCP_HYP_TRAP] = "Hypervisor Trap",
7332 [EXCP_SMC] = "Secure Monitor Call",
7333 [EXCP_VIRQ] = "Virtual IRQ",
7334 [EXCP_VFIQ] = "Virtual FIQ",
7335 [EXCP_SEMIHOST] = "Semihosting call",
7336 [EXCP_NOCP] = "v7M NOCP UsageFault",
7337 [EXCP_INVSTATE] = "v7M INVSTATE UsageFault",
7338 };
27a7ea8a
PB
7339
7340 if (idx >= 0 && idx < ARRAY_SIZE(excnames)) {
7341 exc = excnames[idx];
7342 }
7343 if (!exc) {
7344 exc = "unknown";
7345 }
7346 qemu_log_mask(CPU_LOG_INT, "Taking exception %d [%s]\n", idx, exc);
7347 }
7348}
7349
333e10c5
PM
7350static bool v7m_read_half_insn(ARMCPU *cpu, ARMMMUIdx mmu_idx,
7351 uint32_t addr, uint16_t *insn)
7352{
7353 /* Load a 16-bit portion of a v7M instruction, returning true on success,
7354 * or false on failure (in which case we will have pended the appropriate
7355 * exception).
7356 * We need to do the instruction fetch's MPU and SAU checks
7357 * like this because there is no MMU index that would allow
7358 * doing the load with a single function call. Instead we must
7359 * first check that the security attributes permit the load
7360 * and that they don't mismatch on the two halves of the instruction,
7361 * and then we do the load as a secure load (ie using the security
7362 * attributes of the address, not the CPU, as architecturally required).
7363 */
7364 CPUState *cs = CPU(cpu);
7365 CPUARMState *env = &cpu->env;
7366 V8M_SAttributes sattrs = {};
7367 MemTxAttrs attrs = {};
7368 ARMMMUFaultInfo fi = {};
7369 MemTxResult txres;
7370 target_ulong page_size;
7371 hwaddr physaddr;
7372 int prot;
333e10c5
PM
7373
7374 v8m_security_lookup(env, addr, MMU_INST_FETCH, mmu_idx, &sattrs);
7375 if (!sattrs.nsc || sattrs.ns) {
7376 /* This must be the second half of the insn, and it straddles a
7377 * region boundary with the second half not being S&NSC.
7378 */
7379 env->v7m.sfsr |= R_V7M_SFSR_INVEP_MASK;
7380 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SECURE, false);
7381 qemu_log_mask(CPU_LOG_INT,
7382 "...really SecureFault with SFSR.INVEP\n");
7383 return false;
7384 }
7385 if (get_phys_addr(env, addr, MMU_INST_FETCH, mmu_idx,
bc52bfeb 7386 &physaddr, &attrs, &prot, &page_size, &fi, NULL)) {
333e10c5
PM
7387 /* the MPU lookup failed */
7388 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_IACCVIOL_MASK;
7389 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_MEM, env->v7m.secure);
7390 qemu_log_mask(CPU_LOG_INT, "...really MemManage with CFSR.IACCVIOL\n");
7391 return false;
7392 }
7393 *insn = address_space_lduw_le(arm_addressspace(cs, attrs), physaddr,
7394 attrs, &txres);
7395 if (txres != MEMTX_OK) {
7396 env->v7m.cfsr[M_REG_NS] |= R_V7M_CFSR_IBUSERR_MASK;
7397 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_BUS, false);
7398 qemu_log_mask(CPU_LOG_INT, "...really BusFault with CFSR.IBUSERR\n");
7399 return false;
7400 }
7401 return true;
7402}
7403
7404static bool v7m_handle_execute_nsc(ARMCPU *cpu)
7405{
7406 /* Check whether this attempt to execute code in a Secure & NS-Callable
7407 * memory region is for an SG instruction; if so, then emulate the
7408 * effect of the SG instruction and return true. Otherwise pend
7409 * the correct kind of exception and return false.
7410 */
7411 CPUARMState *env = &cpu->env;
7412 ARMMMUIdx mmu_idx;
7413 uint16_t insn;
7414
7415 /* We should never get here unless get_phys_addr_pmsav8() caused
7416 * an exception for NS executing in S&NSC memory.
7417 */
7418 assert(!env->v7m.secure);
7419 assert(arm_feature(env, ARM_FEATURE_M_SECURITY));
7420
7421 /* We want to do the MPU lookup as secure; work out what mmu_idx that is */
7422 mmu_idx = arm_v7m_mmu_idx_for_secstate(env, true);
7423
7424 if (!v7m_read_half_insn(cpu, mmu_idx, env->regs[15], &insn)) {
7425 return false;
7426 }
7427
7428 if (!env->thumb) {
7429 goto gen_invep;
7430 }
7431
7432 if (insn != 0xe97f) {
7433 /* Not an SG instruction first half (we choose the IMPDEF
7434 * early-SG-check option).
7435 */
7436 goto gen_invep;
7437 }
7438
7439 if (!v7m_read_half_insn(cpu, mmu_idx, env->regs[15] + 2, &insn)) {
7440 return false;
7441 }
7442
7443 if (insn != 0xe97f) {
7444 /* Not an SG instruction second half (yes, both halves of the SG
7445 * insn have the same hex value)
7446 */
7447 goto gen_invep;
7448 }
7449
7450 /* OK, we have confirmed that we really have an SG instruction.
7451 * We know we're NS in S memory so don't need to repeat those checks.
7452 */
7453 qemu_log_mask(CPU_LOG_INT, "...really an SG instruction at 0x%08" PRIx32
7454 ", executing it\n", env->regs[15]);
7455 env->regs[14] &= ~1;
7456 switch_v7m_security_state(env, true);
7457 xpsr_write(env, 0, XPSR_IT);
7458 env->regs[15] += 4;
7459 return true;
7460
7461gen_invep:
7462 env->v7m.sfsr |= R_V7M_SFSR_INVEP_MASK;
7463 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SECURE, false);
7464 qemu_log_mask(CPU_LOG_INT,
7465 "...really SecureFault with SFSR.INVEP\n");
7466 return false;
7467}
7468
e6f010cc 7469void arm_v7m_cpu_do_interrupt(CPUState *cs)
9ee6e8bb 7470{
e6f010cc
AF
7471 ARMCPU *cpu = ARM_CPU(cs);
7472 CPUARMState *env = &cpu->env;
9ee6e8bb 7473 uint32_t lr;
0094ca70 7474 bool ignore_stackfaults;
9ee6e8bb 7475
27103424 7476 arm_log_exception(cs->exception_index);
3f1beaca 7477
9ee6e8bb
PB
7478 /* For exceptions we just mark as pending on the NVIC, and let that
7479 handle it. */
27103424 7480 switch (cs->exception_index) {
9ee6e8bb 7481 case EXCP_UDEF:
2fb50a33 7482 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, env->v7m.secure);
334e8dad 7483 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_UNDEFINSTR_MASK;
a25dc805 7484 break;
7517748e 7485 case EXCP_NOCP:
2fb50a33 7486 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, env->v7m.secure);
334e8dad 7487 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_NOCP_MASK;
a25dc805 7488 break;
e13886e3 7489 case EXCP_INVSTATE:
2fb50a33 7490 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, env->v7m.secure);
334e8dad 7491 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_INVSTATE_MASK;
e13886e3 7492 break;
9ee6e8bb 7493 case EXCP_SWI:
314e2296 7494 /* The PC already points to the next instruction. */
2fb50a33 7495 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SVC, env->v7m.secure);
a25dc805 7496 break;
9ee6e8bb
PB
7497 case EXCP_PREFETCH_ABORT:
7498 case EXCP_DATA_ABORT:
5dd0641d
MD
7499 /* Note that for M profile we don't have a guest facing FSR, but
7500 * the env->exception.fsr will be populated by the code that
7501 * raises the fault, in the A profile short-descriptor format.
abf1172f 7502 */
5dd0641d 7503 switch (env->exception.fsr & 0xf) {
35337cc3
PM
7504 case M_FAKE_FSR_NSC_EXEC:
7505 /* Exception generated when we try to execute code at an address
7506 * which is marked as Secure & Non-Secure Callable and the CPU
7507 * is in the Non-Secure state. The only instruction which can
7508 * be executed like this is SG (and that only if both halves of
7509 * the SG instruction have the same security attributes.)
7510 * Everything else must generate an INVEP SecureFault, so we
7511 * emulate the SG instruction here.
35337cc3 7512 */
333e10c5
PM
7513 if (v7m_handle_execute_nsc(cpu)) {
7514 return;
7515 }
35337cc3
PM
7516 break;
7517 case M_FAKE_FSR_SFAULT:
7518 /* Various flavours of SecureFault for attempts to execute or
7519 * access data in the wrong security state.
7520 */
7521 switch (cs->exception_index) {
7522 case EXCP_PREFETCH_ABORT:
7523 if (env->v7m.secure) {
7524 env->v7m.sfsr |= R_V7M_SFSR_INVTRAN_MASK;
7525 qemu_log_mask(CPU_LOG_INT,
7526 "...really SecureFault with SFSR.INVTRAN\n");
7527 } else {
7528 env->v7m.sfsr |= R_V7M_SFSR_INVEP_MASK;
7529 qemu_log_mask(CPU_LOG_INT,
7530 "...really SecureFault with SFSR.INVEP\n");
7531 }
7532 break;
7533 case EXCP_DATA_ABORT:
7534 /* This must be an NS access to S memory */
7535 env->v7m.sfsr |= R_V7M_SFSR_AUVIOL_MASK;
7536 qemu_log_mask(CPU_LOG_INT,
7537 "...really SecureFault with SFSR.AUVIOL\n");
7538 break;
7539 }
7540 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SECURE, false);
7541 break;
5dd0641d
MD
7542 case 0x8: /* External Abort */
7543 switch (cs->exception_index) {
7544 case EXCP_PREFETCH_ABORT:
c6158878
PM
7545 env->v7m.cfsr[M_REG_NS] |= R_V7M_CFSR_IBUSERR_MASK;
7546 qemu_log_mask(CPU_LOG_INT, "...with CFSR.IBUSERR\n");
5dd0641d
MD
7547 break;
7548 case EXCP_DATA_ABORT:
334e8dad 7549 env->v7m.cfsr[M_REG_NS] |=
c6158878 7550 (R_V7M_CFSR_PRECISERR_MASK | R_V7M_CFSR_BFARVALID_MASK);
5dd0641d
MD
7551 env->v7m.bfar = env->exception.vaddress;
7552 qemu_log_mask(CPU_LOG_INT,
c6158878 7553 "...with CFSR.PRECISERR and BFAR 0x%x\n",
5dd0641d
MD
7554 env->v7m.bfar);
7555 break;
7556 }
2fb50a33 7557 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_BUS, false);
5dd0641d
MD
7558 break;
7559 default:
7560 /* All other FSR values are either MPU faults or "can't happen
7561 * for M profile" cases.
7562 */
7563 switch (cs->exception_index) {
7564 case EXCP_PREFETCH_ABORT:
334e8dad 7565 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_IACCVIOL_MASK;
5dd0641d
MD
7566 qemu_log_mask(CPU_LOG_INT, "...with CFSR.IACCVIOL\n");
7567 break;
7568 case EXCP_DATA_ABORT:
334e8dad 7569 env->v7m.cfsr[env->v7m.secure] |=
5dd0641d 7570 (R_V7M_CFSR_DACCVIOL_MASK | R_V7M_CFSR_MMARVALID_MASK);
c51a5cfc 7571 env->v7m.mmfar[env->v7m.secure] = env->exception.vaddress;
5dd0641d
MD
7572 qemu_log_mask(CPU_LOG_INT,
7573 "...with CFSR.DACCVIOL and MMFAR 0x%x\n",
c51a5cfc 7574 env->v7m.mmfar[env->v7m.secure]);
5dd0641d
MD
7575 break;
7576 }
2fb50a33
PM
7577 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_MEM,
7578 env->v7m.secure);
5dd0641d
MD
7579 break;
7580 }
a25dc805 7581 break;
9ee6e8bb 7582 case EXCP_BKPT:
cfe67cef 7583 if (semihosting_enabled()) {
2ad207d4 7584 int nr;
f9fd40eb 7585 nr = arm_lduw_code(env, env->regs[15], arm_sctlr_b(env)) & 0xff;
2ad207d4
PB
7586 if (nr == 0xab) {
7587 env->regs[15] += 2;
205ace55
CC
7588 qemu_log_mask(CPU_LOG_INT,
7589 "...handling as semihosting call 0x%x\n",
7590 env->regs[0]);
2ad207d4
PB
7591 env->regs[0] = do_arm_semihosting(env);
7592 return;
7593 }
7594 }
2fb50a33 7595 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_DEBUG, false);
a25dc805 7596 break;
9ee6e8bb 7597 case EXCP_IRQ:
9ee6e8bb
PB
7598 break;
7599 case EXCP_EXCEPTION_EXIT:
d02a8698
PM
7600 if (env->regs[15] < EXC_RETURN_MIN_MAGIC) {
7601 /* Must be v8M security extension function return */
7602 assert(env->regs[15] >= FNC_RETURN_MIN_MAGIC);
7603 assert(arm_feature(env, ARM_FEATURE_M_SECURITY));
7604 if (do_v7m_function_return(cpu)) {
7605 return;
7606 }
7607 } else {
7608 do_v7m_exception_exit(cpu);
7609 return;
7610 }
7611 break;
9ee6e8bb 7612 default:
a47dddd7 7613 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
9ee6e8bb
PB
7614 return; /* Never happens. Keep compiler happy. */
7615 }
7616
d3392718
PM
7617 if (arm_feature(env, ARM_FEATURE_V8)) {
7618 lr = R_V7M_EXCRET_RES1_MASK |
7619 R_V7M_EXCRET_DCRS_MASK |
7620 R_V7M_EXCRET_FTYPE_MASK;
7621 /* The S bit indicates whether we should return to Secure
7622 * or NonSecure (ie our current state).
7623 * The ES bit indicates whether we're taking this exception
7624 * to Secure or NonSecure (ie our target state). We set it
7625 * later, in v7m_exception_taken().
7626 * The SPSEL bit is also set in v7m_exception_taken() for v8M.
7627 * This corresponds to the ARM ARM pseudocode for v8M setting
7628 * some LR bits in PushStack() and some in ExceptionTaken();
7629 * the distinction matters for the tailchain cases where we
7630 * can take an exception without pushing the stack.
7631 */
7632 if (env->v7m.secure) {
7633 lr |= R_V7M_EXCRET_S_MASK;
7634 }
7635 } else {
7636 lr = R_V7M_EXCRET_RES1_MASK |
7637 R_V7M_EXCRET_S_MASK |
7638 R_V7M_EXCRET_DCRS_MASK |
7639 R_V7M_EXCRET_FTYPE_MASK |
7640 R_V7M_EXCRET_ES_MASK;
7641 if (env->v7m.control[M_REG_NS] & R_V7M_CONTROL_SPSEL_MASK) {
7642 lr |= R_V7M_EXCRET_SPSEL_MASK;
7643 }
bd70b29b 7644 }
15b3f556 7645 if (!arm_v7m_is_handler_mode(env)) {
4d1e7a47 7646 lr |= R_V7M_EXCRET_MODE_MASK;
bd70b29b
PM
7647 }
7648
0094ca70
PM
7649 ignore_stackfaults = v7m_push_stack(cpu);
7650 v7m_exception_taken(cpu, lr, false, ignore_stackfaults);
a25dc805 7651 qemu_log_mask(CPU_LOG_INT, "... as %d\n", env->v7m.exception);
9ee6e8bb
PB
7652}
7653
ce02049d
GB
7654/* Function used to synchronize QEMU's AArch64 register set with AArch32
7655 * register set. This is necessary when switching between AArch32 and AArch64
7656 * execution state.
7657 */
7658void aarch64_sync_32_to_64(CPUARMState *env)
7659{
7660 int i;
7661 uint32_t mode = env->uncached_cpsr & CPSR_M;
7662
7663 /* We can blanket copy R[0:7] to X[0:7] */
7664 for (i = 0; i < 8; i++) {
7665 env->xregs[i] = env->regs[i];
7666 }
7667
7668 /* Unless we are in FIQ mode, x8-x12 come from the user registers r8-r12.
7669 * Otherwise, they come from the banked user regs.
7670 */
7671 if (mode == ARM_CPU_MODE_FIQ) {
7672 for (i = 8; i < 13; i++) {
7673 env->xregs[i] = env->usr_regs[i - 8];
7674 }
7675 } else {
7676 for (i = 8; i < 13; i++) {
7677 env->xregs[i] = env->regs[i];
7678 }
7679 }
7680
7681 /* Registers x13-x23 are the various mode SP and FP registers. Registers
7682 * r13 and r14 are only copied if we are in that mode, otherwise we copy
7683 * from the mode banked register.
7684 */
7685 if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
7686 env->xregs[13] = env->regs[13];
7687 env->xregs[14] = env->regs[14];
7688 } else {
7689 env->xregs[13] = env->banked_r13[bank_number(ARM_CPU_MODE_USR)];
7690 /* HYP is an exception in that it is copied from r14 */
7691 if (mode == ARM_CPU_MODE_HYP) {
7692 env->xregs[14] = env->regs[14];
7693 } else {
7694 env->xregs[14] = env->banked_r14[bank_number(ARM_CPU_MODE_USR)];
7695 }
7696 }
7697
7698 if (mode == ARM_CPU_MODE_HYP) {
7699 env->xregs[15] = env->regs[13];
7700 } else {
7701 env->xregs[15] = env->banked_r13[bank_number(ARM_CPU_MODE_HYP)];
7702 }
7703
7704 if (mode == ARM_CPU_MODE_IRQ) {
3a9148d0
SS
7705 env->xregs[16] = env->regs[14];
7706 env->xregs[17] = env->regs[13];
ce02049d 7707 } else {
3a9148d0
SS
7708 env->xregs[16] = env->banked_r14[bank_number(ARM_CPU_MODE_IRQ)];
7709 env->xregs[17] = env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)];
ce02049d
GB
7710 }
7711
7712 if (mode == ARM_CPU_MODE_SVC) {
3a9148d0
SS
7713 env->xregs[18] = env->regs[14];
7714 env->xregs[19] = env->regs[13];
ce02049d 7715 } else {
3a9148d0
SS
7716 env->xregs[18] = env->banked_r14[bank_number(ARM_CPU_MODE_SVC)];
7717 env->xregs[19] = env->banked_r13[bank_number(ARM_CPU_MODE_SVC)];
ce02049d
GB
7718 }
7719
7720 if (mode == ARM_CPU_MODE_ABT) {
3a9148d0
SS
7721 env->xregs[20] = env->regs[14];
7722 env->xregs[21] = env->regs[13];
ce02049d 7723 } else {
3a9148d0
SS
7724 env->xregs[20] = env->banked_r14[bank_number(ARM_CPU_MODE_ABT)];
7725 env->xregs[21] = env->banked_r13[bank_number(ARM_CPU_MODE_ABT)];
ce02049d
GB
7726 }
7727
7728 if (mode == ARM_CPU_MODE_UND) {
3a9148d0
SS
7729 env->xregs[22] = env->regs[14];
7730 env->xregs[23] = env->regs[13];
ce02049d 7731 } else {
3a9148d0
SS
7732 env->xregs[22] = env->banked_r14[bank_number(ARM_CPU_MODE_UND)];
7733 env->xregs[23] = env->banked_r13[bank_number(ARM_CPU_MODE_UND)];
ce02049d
GB
7734 }
7735
7736 /* Registers x24-x30 are mapped to r8-r14 in FIQ mode. If we are in FIQ
7737 * mode, then we can copy from r8-r14. Otherwise, we copy from the
7738 * FIQ bank for r8-r14.
7739 */
7740 if (mode == ARM_CPU_MODE_FIQ) {
7741 for (i = 24; i < 31; i++) {
7742 env->xregs[i] = env->regs[i - 16]; /* X[24:30] <- R[8:14] */
7743 }
7744 } else {
7745 for (i = 24; i < 29; i++) {
7746 env->xregs[i] = env->fiq_regs[i - 24];
7747 }
7748 env->xregs[29] = env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)];
7749 env->xregs[30] = env->banked_r14[bank_number(ARM_CPU_MODE_FIQ)];
7750 }
7751
7752 env->pc = env->regs[15];
7753}
7754
7755/* Function used to synchronize QEMU's AArch32 register set with AArch64
7756 * register set. This is necessary when switching between AArch32 and AArch64
7757 * execution state.
7758 */
7759void aarch64_sync_64_to_32(CPUARMState *env)
7760{
7761 int i;
7762 uint32_t mode = env->uncached_cpsr & CPSR_M;
7763
7764 /* We can blanket copy X[0:7] to R[0:7] */
7765 for (i = 0; i < 8; i++) {
7766 env->regs[i] = env->xregs[i];
7767 }
7768
7769 /* Unless we are in FIQ mode, r8-r12 come from the user registers x8-x12.
7770 * Otherwise, we copy x8-x12 into the banked user regs.
7771 */
7772 if (mode == ARM_CPU_MODE_FIQ) {
7773 for (i = 8; i < 13; i++) {
7774 env->usr_regs[i - 8] = env->xregs[i];
7775 }
7776 } else {
7777 for (i = 8; i < 13; i++) {
7778 env->regs[i] = env->xregs[i];
7779 }
7780 }
7781
7782 /* Registers r13 & r14 depend on the current mode.
7783 * If we are in a given mode, we copy the corresponding x registers to r13
7784 * and r14. Otherwise, we copy the x register to the banked r13 and r14
7785 * for the mode.
7786 */
7787 if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
7788 env->regs[13] = env->xregs[13];
7789 env->regs[14] = env->xregs[14];
7790 } else {
7791 env->banked_r13[bank_number(ARM_CPU_MODE_USR)] = env->xregs[13];
7792
7793 /* HYP is an exception in that it does not have its own banked r14 but
7794 * shares the USR r14
7795 */
7796 if (mode == ARM_CPU_MODE_HYP) {
7797 env->regs[14] = env->xregs[14];
7798 } else {
7799 env->banked_r14[bank_number(ARM_CPU_MODE_USR)] = env->xregs[14];
7800 }
7801 }
7802
7803 if (mode == ARM_CPU_MODE_HYP) {
7804 env->regs[13] = env->xregs[15];
7805 } else {
7806 env->banked_r13[bank_number(ARM_CPU_MODE_HYP)] = env->xregs[15];
7807 }
7808
7809 if (mode == ARM_CPU_MODE_IRQ) {
3a9148d0
SS
7810 env->regs[14] = env->xregs[16];
7811 env->regs[13] = env->xregs[17];
ce02049d 7812 } else {
3a9148d0
SS
7813 env->banked_r14[bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[16];
7814 env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[17];
ce02049d
GB
7815 }
7816
7817 if (mode == ARM_CPU_MODE_SVC) {
3a9148d0
SS
7818 env->regs[14] = env->xregs[18];
7819 env->regs[13] = env->xregs[19];
ce02049d 7820 } else {
3a9148d0
SS
7821 env->banked_r14[bank_number(ARM_CPU_MODE_SVC)] = env->xregs[18];
7822 env->banked_r13[bank_number(ARM_CPU_MODE_SVC)] = env->xregs[19];
ce02049d
GB
7823 }
7824
7825 if (mode == ARM_CPU_MODE_ABT) {
3a9148d0
SS
7826 env->regs[14] = env->xregs[20];
7827 env->regs[13] = env->xregs[21];
ce02049d 7828 } else {
3a9148d0
SS
7829 env->banked_r14[bank_number(ARM_CPU_MODE_ABT)] = env->xregs[20];
7830 env->banked_r13[bank_number(ARM_CPU_MODE_ABT)] = env->xregs[21];
ce02049d
GB
7831 }
7832
7833 if (mode == ARM_CPU_MODE_UND) {
3a9148d0
SS
7834 env->regs[14] = env->xregs[22];
7835 env->regs[13] = env->xregs[23];
ce02049d 7836 } else {
3a9148d0
SS
7837 env->banked_r14[bank_number(ARM_CPU_MODE_UND)] = env->xregs[22];
7838 env->banked_r13[bank_number(ARM_CPU_MODE_UND)] = env->xregs[23];
ce02049d
GB
7839 }
7840
7841 /* Registers x24-x30 are mapped to r8-r14 in FIQ mode. If we are in FIQ
7842 * mode, then we can copy to r8-r14. Otherwise, we copy to the
7843 * FIQ bank for r8-r14.
7844 */
7845 if (mode == ARM_CPU_MODE_FIQ) {
7846 for (i = 24; i < 31; i++) {
7847 env->regs[i - 16] = env->xregs[i]; /* X[24:30] -> R[8:14] */
7848 }
7849 } else {
7850 for (i = 24; i < 29; i++) {
7851 env->fiq_regs[i - 24] = env->xregs[i];
7852 }
7853 env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[29];
7854 env->banked_r14[bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[30];
7855 }
7856
7857 env->regs[15] = env->pc;
7858}
7859
966f758c 7860static void arm_cpu_do_interrupt_aarch32(CPUState *cs)
b5ff1b31 7861{
97a8ea5a
AF
7862 ARMCPU *cpu = ARM_CPU(cs);
7863 CPUARMState *env = &cpu->env;
b5ff1b31
FB
7864 uint32_t addr;
7865 uint32_t mask;
7866 int new_mode;
7867 uint32_t offset;
16a906fd 7868 uint32_t moe;
b5ff1b31 7869
16a906fd
PM
7870 /* If this is a debug exception we must update the DBGDSCR.MOE bits */
7871 switch (env->exception.syndrome >> ARM_EL_EC_SHIFT) {
7872 case EC_BREAKPOINT:
7873 case EC_BREAKPOINT_SAME_EL:
7874 moe = 1;
7875 break;
7876 case EC_WATCHPOINT:
7877 case EC_WATCHPOINT_SAME_EL:
7878 moe = 10;
7879 break;
7880 case EC_AA32_BKPT:
7881 moe = 3;
7882 break;
7883 case EC_VECTORCATCH:
7884 moe = 5;
7885 break;
7886 default:
7887 moe = 0;
7888 break;
7889 }
7890
7891 if (moe) {
7892 env->cp15.mdscr_el1 = deposit64(env->cp15.mdscr_el1, 2, 4, moe);
7893 }
7894
b5ff1b31 7895 /* TODO: Vectored interrupt controller. */
27103424 7896 switch (cs->exception_index) {
b5ff1b31
FB
7897 case EXCP_UDEF:
7898 new_mode = ARM_CPU_MODE_UND;
7899 addr = 0x04;
7900 mask = CPSR_I;
7901 if (env->thumb)
7902 offset = 2;
7903 else
7904 offset = 4;
7905 break;
7906 case EXCP_SWI:
7907 new_mode = ARM_CPU_MODE_SVC;
7908 addr = 0x08;
7909 mask = CPSR_I;
601d70b9 7910 /* The PC already points to the next instruction. */
b5ff1b31
FB
7911 offset = 0;
7912 break;
06c949e6 7913 case EXCP_BKPT:
9ee6e8bb
PB
7914 /* Fall through to prefetch abort. */
7915 case EXCP_PREFETCH_ABORT:
88ca1c2d 7916 A32_BANKED_CURRENT_REG_SET(env, ifsr, env->exception.fsr);
b848ce2b 7917 A32_BANKED_CURRENT_REG_SET(env, ifar, env->exception.vaddress);
3f1beaca 7918 qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x IFAR 0x%x\n",
88ca1c2d 7919 env->exception.fsr, (uint32_t)env->exception.vaddress);
b5ff1b31
FB
7920 new_mode = ARM_CPU_MODE_ABT;
7921 addr = 0x0c;
7922 mask = CPSR_A | CPSR_I;
7923 offset = 4;
7924 break;
7925 case EXCP_DATA_ABORT:
4a7e2d73 7926 A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr);
b848ce2b 7927 A32_BANKED_CURRENT_REG_SET(env, dfar, env->exception.vaddress);
3f1beaca 7928 qemu_log_mask(CPU_LOG_INT, "...with DFSR 0x%x DFAR 0x%x\n",
4a7e2d73 7929 env->exception.fsr,
6cd8a264 7930 (uint32_t)env->exception.vaddress);
b5ff1b31
FB
7931 new_mode = ARM_CPU_MODE_ABT;
7932 addr = 0x10;
7933 mask = CPSR_A | CPSR_I;
7934 offset = 8;
7935 break;
7936 case EXCP_IRQ:
7937 new_mode = ARM_CPU_MODE_IRQ;
7938 addr = 0x18;
7939 /* Disable IRQ and imprecise data aborts. */
7940 mask = CPSR_A | CPSR_I;
7941 offset = 4;
de38d23b
FA
7942 if (env->cp15.scr_el3 & SCR_IRQ) {
7943 /* IRQ routed to monitor mode */
7944 new_mode = ARM_CPU_MODE_MON;
7945 mask |= CPSR_F;
7946 }
b5ff1b31
FB
7947 break;
7948 case EXCP_FIQ:
7949 new_mode = ARM_CPU_MODE_FIQ;
7950 addr = 0x1c;
7951 /* Disable FIQ, IRQ and imprecise data aborts. */
7952 mask = CPSR_A | CPSR_I | CPSR_F;
de38d23b
FA
7953 if (env->cp15.scr_el3 & SCR_FIQ) {
7954 /* FIQ routed to monitor mode */
7955 new_mode = ARM_CPU_MODE_MON;
7956 }
b5ff1b31
FB
7957 offset = 4;
7958 break;
87a4b270
PM
7959 case EXCP_VIRQ:
7960 new_mode = ARM_CPU_MODE_IRQ;
7961 addr = 0x18;
7962 /* Disable IRQ and imprecise data aborts. */
7963 mask = CPSR_A | CPSR_I;
7964 offset = 4;
7965 break;
7966 case EXCP_VFIQ:
7967 new_mode = ARM_CPU_MODE_FIQ;
7968 addr = 0x1c;
7969 /* Disable FIQ, IRQ and imprecise data aborts. */
7970 mask = CPSR_A | CPSR_I | CPSR_F;
7971 offset = 4;
7972 break;
dbe9d163
FA
7973 case EXCP_SMC:
7974 new_mode = ARM_CPU_MODE_MON;
7975 addr = 0x08;
7976 mask = CPSR_A | CPSR_I | CPSR_F;
7977 offset = 0;
7978 break;
b5ff1b31 7979 default:
a47dddd7 7980 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
b5ff1b31
FB
7981 return; /* Never happens. Keep compiler happy. */
7982 }
e89e51a1
FA
7983
7984 if (new_mode == ARM_CPU_MODE_MON) {
7985 addr += env->cp15.mvbar;
137feaa9 7986 } else if (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_V) {
e89e51a1 7987 /* High vectors. When enabled, base address cannot be remapped. */
b5ff1b31 7988 addr += 0xffff0000;
8641136c
NR
7989 } else {
7990 /* ARM v7 architectures provide a vector base address register to remap
7991 * the interrupt vector table.
e89e51a1 7992 * This register is only followed in non-monitor mode, and is banked.
8641136c
NR
7993 * Note: only bits 31:5 are valid.
7994 */
fb6c91ba 7995 addr += A32_BANKED_CURRENT_REG_GET(env, vbar);
b5ff1b31 7996 }
dbe9d163
FA
7997
7998 if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON) {
7999 env->cp15.scr_el3 &= ~SCR_NS;
8000 }
8001
b5ff1b31 8002 switch_mode (env, new_mode);
662cefb7
PM
8003 /* For exceptions taken to AArch32 we must clear the SS bit in both
8004 * PSTATE and in the old-state value we save to SPSR_<mode>, so zero it now.
8005 */
8006 env->uncached_cpsr &= ~PSTATE_SS;
b5ff1b31 8007 env->spsr = cpsr_read(env);
9ee6e8bb
PB
8008 /* Clear IT bits. */
8009 env->condexec_bits = 0;
30a8cac1 8010 /* Switch to the new mode, and to the correct instruction set. */
6d7e6326 8011 env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode;
73462ddd
PC
8012 /* Set new mode endianness */
8013 env->uncached_cpsr &= ~CPSR_E;
8014 if (env->cp15.sctlr_el[arm_current_el(env)] & SCTLR_EE) {
3823b9db 8015 env->uncached_cpsr |= CPSR_E;
73462ddd 8016 }
4cc35614 8017 env->daif |= mask;
be5e7a76
DES
8018 /* this is a lie, as the was no c1_sys on V4T/V5, but who cares
8019 * and we should just guard the thumb mode on V4 */
8020 if (arm_feature(env, ARM_FEATURE_V4T)) {
137feaa9 8021 env->thumb = (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_TE) != 0;
be5e7a76 8022 }
b5ff1b31
FB
8023 env->regs[14] = env->regs[15] + offset;
8024 env->regs[15] = addr;
b5ff1b31
FB
8025}
8026
966f758c
PM
8027/* Handle exception entry to a target EL which is using AArch64 */
8028static void arm_cpu_do_interrupt_aarch64(CPUState *cs)
f3a9b694
PM
8029{
8030 ARMCPU *cpu = ARM_CPU(cs);
8031 CPUARMState *env = &cpu->env;
8032 unsigned int new_el = env->exception.target_el;
8033 target_ulong addr = env->cp15.vbar_el[new_el];
8034 unsigned int new_mode = aarch64_pstate_mode(new_el, true);
8035
8036 if (arm_current_el(env) < new_el) {
3d6f7617
PM
8037 /* Entry vector offset depends on whether the implemented EL
8038 * immediately lower than the target level is using AArch32 or AArch64
8039 */
8040 bool is_aa64;
8041
8042 switch (new_el) {
8043 case 3:
8044 is_aa64 = (env->cp15.scr_el3 & SCR_RW) != 0;
8045 break;
8046 case 2:
8047 is_aa64 = (env->cp15.hcr_el2 & HCR_RW) != 0;
8048 break;
8049 case 1:
8050 is_aa64 = is_a64(env);
8051 break;
8052 default:
8053 g_assert_not_reached();
8054 }
8055
8056 if (is_aa64) {
f3a9b694
PM
8057 addr += 0x400;
8058 } else {
8059 addr += 0x600;
8060 }
8061 } else if (pstate_read(env) & PSTATE_SP) {
8062 addr += 0x200;
8063 }
8064
f3a9b694
PM
8065 switch (cs->exception_index) {
8066 case EXCP_PREFETCH_ABORT:
8067 case EXCP_DATA_ABORT:
8068 env->cp15.far_el[new_el] = env->exception.vaddress;
8069 qemu_log_mask(CPU_LOG_INT, "...with FAR 0x%" PRIx64 "\n",
8070 env->cp15.far_el[new_el]);
8071 /* fall through */
8072 case EXCP_BKPT:
8073 case EXCP_UDEF:
8074 case EXCP_SWI:
8075 case EXCP_HVC:
8076 case EXCP_HYP_TRAP:
8077 case EXCP_SMC:
8078 env->cp15.esr_el[new_el] = env->exception.syndrome;
8079 break;
8080 case EXCP_IRQ:
8081 case EXCP_VIRQ:
8082 addr += 0x80;
8083 break;
8084 case EXCP_FIQ:
8085 case EXCP_VFIQ:
8086 addr += 0x100;
8087 break;
8088 case EXCP_SEMIHOST:
8089 qemu_log_mask(CPU_LOG_INT,
8090 "...handling as semihosting call 0x%" PRIx64 "\n",
8091 env->xregs[0]);
8092 env->xregs[0] = do_arm_semihosting(env);
8093 return;
8094 default:
8095 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
8096 }
8097
8098 if (is_a64(env)) {
8099 env->banked_spsr[aarch64_banked_spsr_index(new_el)] = pstate_read(env);
8100 aarch64_save_sp(env, arm_current_el(env));
8101 env->elr_el[new_el] = env->pc;
8102 } else {
8103 env->banked_spsr[aarch64_banked_spsr_index(new_el)] = cpsr_read(env);
f3a9b694
PM
8104 env->elr_el[new_el] = env->regs[15];
8105
8106 aarch64_sync_32_to_64(env);
8107
8108 env->condexec_bits = 0;
8109 }
8110 qemu_log_mask(CPU_LOG_INT, "...with ELR 0x%" PRIx64 "\n",
8111 env->elr_el[new_el]);
8112
8113 pstate_write(env, PSTATE_DAIF | new_mode);
8114 env->aarch64 = 1;
8115 aarch64_restore_sp(env, new_el);
8116
8117 env->pc = addr;
8118
8119 qemu_log_mask(CPU_LOG_INT, "...to EL%d PC 0x%" PRIx64 " PSTATE 0x%x\n",
8120 new_el, env->pc, pstate_read(env));
966f758c
PM
8121}
8122
904c04de
PM
8123static inline bool check_for_semihosting(CPUState *cs)
8124{
8125 /* Check whether this exception is a semihosting call; if so
8126 * then handle it and return true; otherwise return false.
8127 */
8128 ARMCPU *cpu = ARM_CPU(cs);
8129 CPUARMState *env = &cpu->env;
8130
8131 if (is_a64(env)) {
8132 if (cs->exception_index == EXCP_SEMIHOST) {
8133 /* This is always the 64-bit semihosting exception.
8134 * The "is this usermode" and "is semihosting enabled"
8135 * checks have been done at translate time.
8136 */
8137 qemu_log_mask(CPU_LOG_INT,
8138 "...handling as semihosting call 0x%" PRIx64 "\n",
8139 env->xregs[0]);
8140 env->xregs[0] = do_arm_semihosting(env);
8141 return true;
8142 }
8143 return false;
8144 } else {
8145 uint32_t imm;
8146
8147 /* Only intercept calls from privileged modes, to provide some
8148 * semblance of security.
8149 */
19a6e31c
PM
8150 if (cs->exception_index != EXCP_SEMIHOST &&
8151 (!semihosting_enabled() ||
8152 ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_USR))) {
904c04de
PM
8153 return false;
8154 }
8155
8156 switch (cs->exception_index) {
19a6e31c
PM
8157 case EXCP_SEMIHOST:
8158 /* This is always a semihosting call; the "is this usermode"
8159 * and "is semihosting enabled" checks have been done at
8160 * translate time.
8161 */
8162 break;
904c04de
PM
8163 case EXCP_SWI:
8164 /* Check for semihosting interrupt. */
8165 if (env->thumb) {
f9fd40eb 8166 imm = arm_lduw_code(env, env->regs[15] - 2, arm_sctlr_b(env))
904c04de
PM
8167 & 0xff;
8168 if (imm == 0xab) {
8169 break;
8170 }
8171 } else {
f9fd40eb 8172 imm = arm_ldl_code(env, env->regs[15] - 4, arm_sctlr_b(env))
904c04de
PM
8173 & 0xffffff;
8174 if (imm == 0x123456) {
8175 break;
8176 }
8177 }
8178 return false;
8179 case EXCP_BKPT:
8180 /* See if this is a semihosting syscall. */
8181 if (env->thumb) {
f9fd40eb 8182 imm = arm_lduw_code(env, env->regs[15], arm_sctlr_b(env))
904c04de
PM
8183 & 0xff;
8184 if (imm == 0xab) {
8185 env->regs[15] += 2;
8186 break;
8187 }
8188 }
8189 return false;
8190 default:
8191 return false;
8192 }
8193
8194 qemu_log_mask(CPU_LOG_INT,
8195 "...handling as semihosting call 0x%x\n",
8196 env->regs[0]);
8197 env->regs[0] = do_arm_semihosting(env);
8198 return true;
8199 }
8200}
8201
966f758c
PM
8202/* Handle a CPU exception for A and R profile CPUs.
8203 * Do any appropriate logging, handle PSCI calls, and then hand off
8204 * to the AArch64-entry or AArch32-entry function depending on the
8205 * target exception level's register width.
8206 */
8207void arm_cpu_do_interrupt(CPUState *cs)
8208{
8209 ARMCPU *cpu = ARM_CPU(cs);
8210 CPUARMState *env = &cpu->env;
8211 unsigned int new_el = env->exception.target_el;
8212
531c60a9 8213 assert(!arm_feature(env, ARM_FEATURE_M));
966f758c
PM
8214
8215 arm_log_exception(cs->exception_index);
8216 qemu_log_mask(CPU_LOG_INT, "...from EL%d to EL%d\n", arm_current_el(env),
8217 new_el);
8218 if (qemu_loglevel_mask(CPU_LOG_INT)
8219 && !excp_is_internal(cs->exception_index)) {
6568da45 8220 qemu_log_mask(CPU_LOG_INT, "...with ESR 0x%x/0x%" PRIx32 "\n",
966f758c
PM
8221 env->exception.syndrome >> ARM_EL_EC_SHIFT,
8222 env->exception.syndrome);
8223 }
8224
8225 if (arm_is_psci_call(cpu, cs->exception_index)) {
8226 arm_handle_psci_call(cpu);
8227 qemu_log_mask(CPU_LOG_INT, "...handled as PSCI call\n");
8228 return;
8229 }
8230
904c04de
PM
8231 /* Semihosting semantics depend on the register width of the
8232 * code that caused the exception, not the target exception level,
8233 * so must be handled here.
966f758c 8234 */
904c04de
PM
8235 if (check_for_semihosting(cs)) {
8236 return;
8237 }
8238
8239 assert(!excp_is_internal(cs->exception_index));
8240 if (arm_el_is_aa64(env, new_el)) {
966f758c
PM
8241 arm_cpu_do_interrupt_aarch64(cs);
8242 } else {
8243 arm_cpu_do_interrupt_aarch32(cs);
8244 }
f3a9b694 8245
8d04fb55
JK
8246 /* Hooks may change global state so BQL should be held, also the
8247 * BQL needs to be held for any modification of
8248 * cs->interrupt_request.
8249 */
8250 g_assert(qemu_mutex_iothread_locked());
8251
bd7d00fc
PM
8252 arm_call_el_change_hook(cpu);
8253
f3a9b694
PM
8254 if (!kvm_enabled()) {
8255 cs->interrupt_request |= CPU_INTERRUPT_EXITTB;
8256 }
8257}
0480f69a
PM
8258
8259/* Return the exception level which controls this address translation regime */
8260static inline uint32_t regime_el(CPUARMState *env, ARMMMUIdx mmu_idx)
8261{
8262 switch (mmu_idx) {
8263 case ARMMMUIdx_S2NS:
8264 case ARMMMUIdx_S1E2:
8265 return 2;
8266 case ARMMMUIdx_S1E3:
8267 return 3;
8268 case ARMMMUIdx_S1SE0:
8269 return arm_el_is_aa64(env, 3) ? 1 : 3;
8270 case ARMMMUIdx_S1SE1:
8271 case ARMMMUIdx_S1NSE0:
8272 case ARMMMUIdx_S1NSE1:
62593718
PM
8273 case ARMMMUIdx_MPrivNegPri:
8274 case ARMMMUIdx_MUserNegPri:
e7b921c2
PM
8275 case ARMMMUIdx_MPriv:
8276 case ARMMMUIdx_MUser:
62593718
PM
8277 case ARMMMUIdx_MSPrivNegPri:
8278 case ARMMMUIdx_MSUserNegPri:
66787c78 8279 case ARMMMUIdx_MSPriv:
66787c78 8280 case ARMMMUIdx_MSUser:
0480f69a
PM
8281 return 1;
8282 default:
8283 g_assert_not_reached();
8284 }
8285}
8286
8287/* Return the SCTLR value which controls this address translation regime */
8288static inline uint32_t regime_sctlr(CPUARMState *env, ARMMMUIdx mmu_idx)
8289{
8290 return env->cp15.sctlr_el[regime_el(env, mmu_idx)];
8291}
8292
8293/* Return true if the specified stage of address translation is disabled */
8294static inline bool regime_translation_disabled(CPUARMState *env,
8295 ARMMMUIdx mmu_idx)
8296{
29c483a5 8297 if (arm_feature(env, ARM_FEATURE_M)) {
ecf5e8ea 8298 switch (env->v7m.mpu_ctrl[regime_is_secure(env, mmu_idx)] &
3bef7012
PM
8299 (R_V7M_MPU_CTRL_ENABLE_MASK | R_V7M_MPU_CTRL_HFNMIENA_MASK)) {
8300 case R_V7M_MPU_CTRL_ENABLE_MASK:
8301 /* Enabled, but not for HardFault and NMI */
62593718 8302 return mmu_idx & ARM_MMU_IDX_M_NEGPRI;
3bef7012
PM
8303 case R_V7M_MPU_CTRL_ENABLE_MASK | R_V7M_MPU_CTRL_HFNMIENA_MASK:
8304 /* Enabled for all cases */
8305 return false;
8306 case 0:
8307 default:
8308 /* HFNMIENA set and ENABLE clear is UNPREDICTABLE, but
8309 * we warned about that in armv7m_nvic.c when the guest set it.
8310 */
8311 return true;
8312 }
29c483a5
MD
8313 }
8314
0480f69a
PM
8315 if (mmu_idx == ARMMMUIdx_S2NS) {
8316 return (env->cp15.hcr_el2 & HCR_VM) == 0;
8317 }
8318 return (regime_sctlr(env, mmu_idx) & SCTLR_M) == 0;
8319}
8320
73462ddd
PC
8321static inline bool regime_translation_big_endian(CPUARMState *env,
8322 ARMMMUIdx mmu_idx)
8323{
8324 return (regime_sctlr(env, mmu_idx) & SCTLR_EE) != 0;
8325}
8326
0480f69a
PM
8327/* Return the TCR controlling this translation regime */
8328static inline TCR *regime_tcr(CPUARMState *env, ARMMMUIdx mmu_idx)
8329{
8330 if (mmu_idx == ARMMMUIdx_S2NS) {
68e9c2fe 8331 return &env->cp15.vtcr_el2;
0480f69a
PM
8332 }
8333 return &env->cp15.tcr_el[regime_el(env, mmu_idx)];
8334}
8335
8bd5c820
PM
8336/* Convert a possible stage1+2 MMU index into the appropriate
8337 * stage 1 MMU index
8338 */
8339static inline ARMMMUIdx stage_1_mmu_idx(ARMMMUIdx mmu_idx)
8340{
8341 if (mmu_idx == ARMMMUIdx_S12NSE0 || mmu_idx == ARMMMUIdx_S12NSE1) {
8342 mmu_idx += (ARMMMUIdx_S1NSE0 - ARMMMUIdx_S12NSE0);
8343 }
8344 return mmu_idx;
8345}
8346
86fb3fa4
TH
8347/* Returns TBI0 value for current regime el */
8348uint32_t arm_regime_tbi0(CPUARMState *env, ARMMMUIdx mmu_idx)
8349{
8350 TCR *tcr;
8351 uint32_t el;
8352
8353 /* For EL0 and EL1, TBI is controlled by stage 1's TCR, so convert
8bd5c820
PM
8354 * a stage 1+2 mmu index into the appropriate stage 1 mmu index.
8355 */
8356 mmu_idx = stage_1_mmu_idx(mmu_idx);
86fb3fa4
TH
8357
8358 tcr = regime_tcr(env, mmu_idx);
8359 el = regime_el(env, mmu_idx);
8360
8361 if (el > 1) {
8362 return extract64(tcr->raw_tcr, 20, 1);
8363 } else {
8364 return extract64(tcr->raw_tcr, 37, 1);
8365 }
8366}
8367
8368/* Returns TBI1 value for current regime el */
8369uint32_t arm_regime_tbi1(CPUARMState *env, ARMMMUIdx mmu_idx)
8370{
8371 TCR *tcr;
8372 uint32_t el;
8373
8374 /* For EL0 and EL1, TBI is controlled by stage 1's TCR, so convert
8bd5c820
PM
8375 * a stage 1+2 mmu index into the appropriate stage 1 mmu index.
8376 */
8377 mmu_idx = stage_1_mmu_idx(mmu_idx);
86fb3fa4
TH
8378
8379 tcr = regime_tcr(env, mmu_idx);
8380 el = regime_el(env, mmu_idx);
8381
8382 if (el > 1) {
8383 return 0;
8384 } else {
8385 return extract64(tcr->raw_tcr, 38, 1);
8386 }
8387}
8388
aef878be
GB
8389/* Return the TTBR associated with this translation regime */
8390static inline uint64_t regime_ttbr(CPUARMState *env, ARMMMUIdx mmu_idx,
8391 int ttbrn)
8392{
8393 if (mmu_idx == ARMMMUIdx_S2NS) {
b698e9cf 8394 return env->cp15.vttbr_el2;
aef878be
GB
8395 }
8396 if (ttbrn == 0) {
8397 return env->cp15.ttbr0_el[regime_el(env, mmu_idx)];
8398 } else {
8399 return env->cp15.ttbr1_el[regime_el(env, mmu_idx)];
8400 }
8401}
8402
0480f69a
PM
8403/* Return true if the translation regime is using LPAE format page tables */
8404static inline bool regime_using_lpae_format(CPUARMState *env,
8405 ARMMMUIdx mmu_idx)
8406{
8407 int el = regime_el(env, mmu_idx);
8408 if (el == 2 || arm_el_is_aa64(env, el)) {
8409 return true;
8410 }
8411 if (arm_feature(env, ARM_FEATURE_LPAE)
8412 && (regime_tcr(env, mmu_idx)->raw_tcr & TTBCR_EAE)) {
8413 return true;
8414 }
8415 return false;
8416}
8417
deb2db99
AR
8418/* Returns true if the stage 1 translation regime is using LPAE format page
8419 * tables. Used when raising alignment exceptions, whose FSR changes depending
8420 * on whether the long or short descriptor format is in use. */
8421bool arm_s1_regime_using_lpae_format(CPUARMState *env, ARMMMUIdx mmu_idx)
30901475 8422{
8bd5c820 8423 mmu_idx = stage_1_mmu_idx(mmu_idx);
deb2db99 8424
30901475
AB
8425 return regime_using_lpae_format(env, mmu_idx);
8426}
8427
0480f69a
PM
8428static inline bool regime_is_user(CPUARMState *env, ARMMMUIdx mmu_idx)
8429{
8430 switch (mmu_idx) {
8431 case ARMMMUIdx_S1SE0:
8432 case ARMMMUIdx_S1NSE0:
e7b921c2 8433 case ARMMMUIdx_MUser:
871bec7c 8434 case ARMMMUIdx_MSUser:
62593718
PM
8435 case ARMMMUIdx_MUserNegPri:
8436 case ARMMMUIdx_MSUserNegPri:
0480f69a
PM
8437 return true;
8438 default:
8439 return false;
8440 case ARMMMUIdx_S12NSE0:
8441 case ARMMMUIdx_S12NSE1:
8442 g_assert_not_reached();
8443 }
8444}
8445
0fbf5238
AJ
8446/* Translate section/page access permissions to page
8447 * R/W protection flags
d76951b6
AJ
8448 *
8449 * @env: CPUARMState
8450 * @mmu_idx: MMU index indicating required translation regime
8451 * @ap: The 3-bit access permissions (AP[2:0])
8452 * @domain_prot: The 2-bit domain access permissions
0fbf5238
AJ
8453 */
8454static inline int ap_to_rw_prot(CPUARMState *env, ARMMMUIdx mmu_idx,
8455 int ap, int domain_prot)
8456{
554b0b09
PM
8457 bool is_user = regime_is_user(env, mmu_idx);
8458
8459 if (domain_prot == 3) {
8460 return PAGE_READ | PAGE_WRITE;
8461 }
8462
554b0b09
PM
8463 switch (ap) {
8464 case 0:
8465 if (arm_feature(env, ARM_FEATURE_V7)) {
8466 return 0;
8467 }
554b0b09
PM
8468 switch (regime_sctlr(env, mmu_idx) & (SCTLR_S | SCTLR_R)) {
8469 case SCTLR_S:
8470 return is_user ? 0 : PAGE_READ;
8471 case SCTLR_R:
8472 return PAGE_READ;
8473 default:
8474 return 0;
8475 }
8476 case 1:
8477 return is_user ? 0 : PAGE_READ | PAGE_WRITE;
8478 case 2:
87c3d486 8479 if (is_user) {
0fbf5238 8480 return PAGE_READ;
87c3d486 8481 } else {
554b0b09 8482 return PAGE_READ | PAGE_WRITE;
87c3d486 8483 }
554b0b09
PM
8484 case 3:
8485 return PAGE_READ | PAGE_WRITE;
8486 case 4: /* Reserved. */
8487 return 0;
8488 case 5:
0fbf5238 8489 return is_user ? 0 : PAGE_READ;
554b0b09 8490 case 6:
0fbf5238 8491 return PAGE_READ;
554b0b09 8492 case 7:
87c3d486 8493 if (!arm_feature(env, ARM_FEATURE_V6K)) {
554b0b09 8494 return 0;
87c3d486 8495 }
0fbf5238 8496 return PAGE_READ;
554b0b09 8497 default:
0fbf5238 8498 g_assert_not_reached();
554b0b09 8499 }
b5ff1b31
FB
8500}
8501
d76951b6
AJ
8502/* Translate section/page access permissions to page
8503 * R/W protection flags.
8504 *
d76951b6 8505 * @ap: The 2-bit simple AP (AP[2:1])
d8e052b3 8506 * @is_user: TRUE if accessing from PL0
d76951b6 8507 */
d8e052b3 8508static inline int simple_ap_to_rw_prot_is_user(int ap, bool is_user)
d76951b6 8509{
d76951b6
AJ
8510 switch (ap) {
8511 case 0:
8512 return is_user ? 0 : PAGE_READ | PAGE_WRITE;
8513 case 1:
8514 return PAGE_READ | PAGE_WRITE;
8515 case 2:
8516 return is_user ? 0 : PAGE_READ;
8517 case 3:
8518 return PAGE_READ;
8519 default:
8520 g_assert_not_reached();
8521 }
8522}
8523
d8e052b3
AJ
8524static inline int
8525simple_ap_to_rw_prot(CPUARMState *env, ARMMMUIdx mmu_idx, int ap)
8526{
8527 return simple_ap_to_rw_prot_is_user(ap, regime_is_user(env, mmu_idx));
8528}
8529
6ab1a5ee
EI
8530/* Translate S2 section/page access permissions to protection flags
8531 *
8532 * @env: CPUARMState
8533 * @s2ap: The 2-bit stage2 access permissions (S2AP)
8534 * @xn: XN (execute-never) bit
8535 */
8536static int get_S2prot(CPUARMState *env, int s2ap, int xn)
8537{
8538 int prot = 0;
8539
8540 if (s2ap & 1) {
8541 prot |= PAGE_READ;
8542 }
8543 if (s2ap & 2) {
8544 prot |= PAGE_WRITE;
8545 }
8546 if (!xn) {
dfda6837
SS
8547 if (arm_el_is_aa64(env, 2) || prot & PAGE_READ) {
8548 prot |= PAGE_EXEC;
8549 }
6ab1a5ee
EI
8550 }
8551 return prot;
8552}
8553
d8e052b3
AJ
8554/* Translate section/page access permissions to protection flags
8555 *
8556 * @env: CPUARMState
8557 * @mmu_idx: MMU index indicating required translation regime
8558 * @is_aa64: TRUE if AArch64
8559 * @ap: The 2-bit simple AP (AP[2:1])
8560 * @ns: NS (non-secure) bit
8561 * @xn: XN (execute-never) bit
8562 * @pxn: PXN (privileged execute-never) bit
8563 */
8564static int get_S1prot(CPUARMState *env, ARMMMUIdx mmu_idx, bool is_aa64,
8565 int ap, int ns, int xn, int pxn)
8566{
8567 bool is_user = regime_is_user(env, mmu_idx);
8568 int prot_rw, user_rw;
8569 bool have_wxn;
8570 int wxn = 0;
8571
8572 assert(mmu_idx != ARMMMUIdx_S2NS);
8573
8574 user_rw = simple_ap_to_rw_prot_is_user(ap, true);
8575 if (is_user) {
8576 prot_rw = user_rw;
8577 } else {
8578 prot_rw = simple_ap_to_rw_prot_is_user(ap, false);
8579 }
8580
8581 if (ns && arm_is_secure(env) && (env->cp15.scr_el3 & SCR_SIF)) {
8582 return prot_rw;
8583 }
8584
8585 /* TODO have_wxn should be replaced with
8586 * ARM_FEATURE_V8 || (ARM_FEATURE_V7 && ARM_FEATURE_EL2)
8587 * when ARM_FEATURE_EL2 starts getting set. For now we assume all LPAE
8588 * compatible processors have EL2, which is required for [U]WXN.
8589 */
8590 have_wxn = arm_feature(env, ARM_FEATURE_LPAE);
8591
8592 if (have_wxn) {
8593 wxn = regime_sctlr(env, mmu_idx) & SCTLR_WXN;
8594 }
8595
8596 if (is_aa64) {
8597 switch (regime_el(env, mmu_idx)) {
8598 case 1:
8599 if (!is_user) {
8600 xn = pxn || (user_rw & PAGE_WRITE);
8601 }
8602 break;
8603 case 2:
8604 case 3:
8605 break;
8606 }
8607 } else if (arm_feature(env, ARM_FEATURE_V7)) {
8608 switch (regime_el(env, mmu_idx)) {
8609 case 1:
8610 case 3:
8611 if (is_user) {
8612 xn = xn || !(user_rw & PAGE_READ);
8613 } else {
8614 int uwxn = 0;
8615 if (have_wxn) {
8616 uwxn = regime_sctlr(env, mmu_idx) & SCTLR_UWXN;
8617 }
8618 xn = xn || !(prot_rw & PAGE_READ) || pxn ||
8619 (uwxn && (user_rw & PAGE_WRITE));
8620 }
8621 break;
8622 case 2:
8623 break;
8624 }
8625 } else {
8626 xn = wxn = 0;
8627 }
8628
8629 if (xn || (wxn && (prot_rw & PAGE_WRITE))) {
8630 return prot_rw;
8631 }
8632 return prot_rw | PAGE_EXEC;
8633}
8634
0480f69a
PM
8635static bool get_level1_table_address(CPUARMState *env, ARMMMUIdx mmu_idx,
8636 uint32_t *table, uint32_t address)
b2fa1797 8637{
0480f69a 8638 /* Note that we can only get here for an AArch32 PL0/PL1 lookup */
0480f69a 8639 TCR *tcr = regime_tcr(env, mmu_idx);
11f136ee 8640
11f136ee
FA
8641 if (address & tcr->mask) {
8642 if (tcr->raw_tcr & TTBCR_PD1) {
e389be16
FA
8643 /* Translation table walk disabled for TTBR1 */
8644 return false;
8645 }
aef878be 8646 *table = regime_ttbr(env, mmu_idx, 1) & 0xffffc000;
e389be16 8647 } else {
11f136ee 8648 if (tcr->raw_tcr & TTBCR_PD0) {
e389be16
FA
8649 /* Translation table walk disabled for TTBR0 */
8650 return false;
8651 }
aef878be 8652 *table = regime_ttbr(env, mmu_idx, 0) & tcr->base_mask;
e389be16
FA
8653 }
8654 *table |= (address >> 18) & 0x3ffc;
8655 return true;
b2fa1797
PB
8656}
8657
37785977
EI
8658/* Translate a S1 pagetable walk through S2 if needed. */
8659static hwaddr S1_ptw_translate(CPUARMState *env, ARMMMUIdx mmu_idx,
8660 hwaddr addr, MemTxAttrs txattrs,
37785977
EI
8661 ARMMMUFaultInfo *fi)
8662{
8663 if ((mmu_idx == ARMMMUIdx_S1NSE0 || mmu_idx == ARMMMUIdx_S1NSE1) &&
8664 !regime_translation_disabled(env, ARMMMUIdx_S2NS)) {
8665 target_ulong s2size;
8666 hwaddr s2pa;
8667 int s2prot;
8668 int ret;
8669
8670 ret = get_phys_addr_lpae(env, addr, 0, ARMMMUIdx_S2NS, &s2pa,
da909b2c 8671 &txattrs, &s2prot, &s2size, fi, NULL);
37785977 8672 if (ret) {
3b39d734 8673 assert(fi->type != ARMFault_None);
37785977
EI
8674 fi->s2addr = addr;
8675 fi->stage2 = true;
8676 fi->s1ptw = true;
8677 return ~0;
8678 }
8679 addr = s2pa;
8680 }
8681 return addr;
8682}
8683
14577270 8684/* All loads done in the course of a page table walk go through here. */
a614e698 8685static uint32_t arm_ldl_ptw(CPUState *cs, hwaddr addr, bool is_secure,
3795a6de 8686 ARMMMUIdx mmu_idx, ARMMMUFaultInfo *fi)
ebca90e4 8687{
a614e698
EI
8688 ARMCPU *cpu = ARM_CPU(cs);
8689 CPUARMState *env = &cpu->env;
ebca90e4 8690 MemTxAttrs attrs = {};
3b39d734 8691 MemTxResult result = MEMTX_OK;
5ce4ff65 8692 AddressSpace *as;
3b39d734 8693 uint32_t data;
ebca90e4
PM
8694
8695 attrs.secure = is_secure;
5ce4ff65 8696 as = arm_addressspace(cs, attrs);
3795a6de 8697 addr = S1_ptw_translate(env, mmu_idx, addr, attrs, fi);
a614e698
EI
8698 if (fi->s1ptw) {
8699 return 0;
8700 }
73462ddd 8701 if (regime_translation_big_endian(env, mmu_idx)) {
3b39d734 8702 data = address_space_ldl_be(as, addr, attrs, &result);
73462ddd 8703 } else {
3b39d734 8704 data = address_space_ldl_le(as, addr, attrs, &result);
73462ddd 8705 }
3b39d734
PM
8706 if (result == MEMTX_OK) {
8707 return data;
8708 }
8709 fi->type = ARMFault_SyncExternalOnWalk;
8710 fi->ea = arm_extabort_type(result);
8711 return 0;
ebca90e4
PM
8712}
8713
37785977 8714static uint64_t arm_ldq_ptw(CPUState *cs, hwaddr addr, bool is_secure,
3795a6de 8715 ARMMMUIdx mmu_idx, ARMMMUFaultInfo *fi)
ebca90e4 8716{
37785977
EI
8717 ARMCPU *cpu = ARM_CPU(cs);
8718 CPUARMState *env = &cpu->env;
ebca90e4 8719 MemTxAttrs attrs = {};
3b39d734 8720 MemTxResult result = MEMTX_OK;
5ce4ff65 8721 AddressSpace *as;
9aea1ea3 8722 uint64_t data;
ebca90e4
PM
8723
8724 attrs.secure = is_secure;
5ce4ff65 8725 as = arm_addressspace(cs, attrs);
3795a6de 8726 addr = S1_ptw_translate(env, mmu_idx, addr, attrs, fi);
37785977
EI
8727 if (fi->s1ptw) {
8728 return 0;
8729 }
73462ddd 8730 if (regime_translation_big_endian(env, mmu_idx)) {
3b39d734 8731 data = address_space_ldq_be(as, addr, attrs, &result);
73462ddd 8732 } else {
3b39d734
PM
8733 data = address_space_ldq_le(as, addr, attrs, &result);
8734 }
8735 if (result == MEMTX_OK) {
8736 return data;
73462ddd 8737 }
3b39d734
PM
8738 fi->type = ARMFault_SyncExternalOnWalk;
8739 fi->ea = arm_extabort_type(result);
8740 return 0;
ebca90e4
PM
8741}
8742
b7cc4e82 8743static bool get_phys_addr_v5(CPUARMState *env, uint32_t address,
03ae85f8 8744 MMUAccessType access_type, ARMMMUIdx mmu_idx,
b7cc4e82 8745 hwaddr *phys_ptr, int *prot,
f989983e 8746 target_ulong *page_size,
e14b5a23 8747 ARMMMUFaultInfo *fi)
b5ff1b31 8748{
70d74660 8749 CPUState *cs = CPU(arm_env_get_cpu(env));
f989983e 8750 int level = 1;
b5ff1b31
FB
8751 uint32_t table;
8752 uint32_t desc;
8753 int type;
8754 int ap;
e389be16 8755 int domain = 0;
dd4ebc2e 8756 int domain_prot;
a8170e5e 8757 hwaddr phys_addr;
0480f69a 8758 uint32_t dacr;
b5ff1b31 8759
9ee6e8bb
PB
8760 /* Pagetable walk. */
8761 /* Lookup l1 descriptor. */
0480f69a 8762 if (!get_level1_table_address(env, mmu_idx, &table, address)) {
e389be16 8763 /* Section translation fault if page walk is disabled by PD0 or PD1 */
f989983e 8764 fi->type = ARMFault_Translation;
e389be16
FA
8765 goto do_fault;
8766 }
a614e698 8767 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
3795a6de 8768 mmu_idx, fi);
3b39d734
PM
8769 if (fi->type != ARMFault_None) {
8770 goto do_fault;
8771 }
9ee6e8bb 8772 type = (desc & 3);
dd4ebc2e 8773 domain = (desc >> 5) & 0x0f;
0480f69a
PM
8774 if (regime_el(env, mmu_idx) == 1) {
8775 dacr = env->cp15.dacr_ns;
8776 } else {
8777 dacr = env->cp15.dacr_s;
8778 }
8779 domain_prot = (dacr >> (domain * 2)) & 3;
9ee6e8bb 8780 if (type == 0) {
601d70b9 8781 /* Section translation fault. */
f989983e 8782 fi->type = ARMFault_Translation;
9ee6e8bb
PB
8783 goto do_fault;
8784 }
f989983e
PM
8785 if (type != 2) {
8786 level = 2;
8787 }
dd4ebc2e 8788 if (domain_prot == 0 || domain_prot == 2) {
f989983e 8789 fi->type = ARMFault_Domain;
9ee6e8bb
PB
8790 goto do_fault;
8791 }
8792 if (type == 2) {
8793 /* 1Mb section. */
8794 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
8795 ap = (desc >> 10) & 3;
d4c430a8 8796 *page_size = 1024 * 1024;
9ee6e8bb
PB
8797 } else {
8798 /* Lookup l2 entry. */
554b0b09
PM
8799 if (type == 1) {
8800 /* Coarse pagetable. */
8801 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
8802 } else {
8803 /* Fine pagetable. */
8804 table = (desc & 0xfffff000) | ((address >> 8) & 0xffc);
8805 }
a614e698 8806 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
3795a6de 8807 mmu_idx, fi);
3b39d734
PM
8808 if (fi->type != ARMFault_None) {
8809 goto do_fault;
8810 }
9ee6e8bb
PB
8811 switch (desc & 3) {
8812 case 0: /* Page translation fault. */
f989983e 8813 fi->type = ARMFault_Translation;
9ee6e8bb
PB
8814 goto do_fault;
8815 case 1: /* 64k page. */
8816 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
8817 ap = (desc >> (4 + ((address >> 13) & 6))) & 3;
d4c430a8 8818 *page_size = 0x10000;
ce819861 8819 break;
9ee6e8bb
PB
8820 case 2: /* 4k page. */
8821 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
c10f7fc3 8822 ap = (desc >> (4 + ((address >> 9) & 6))) & 3;
d4c430a8 8823 *page_size = 0x1000;
ce819861 8824 break;
fc1891c7 8825 case 3: /* 1k page, or ARMv6/XScale "extended small (4k) page" */
554b0b09 8826 if (type == 1) {
fc1891c7
PM
8827 /* ARMv6/XScale extended small page format */
8828 if (arm_feature(env, ARM_FEATURE_XSCALE)
8829 || arm_feature(env, ARM_FEATURE_V6)) {
554b0b09 8830 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
fc1891c7 8831 *page_size = 0x1000;
554b0b09 8832 } else {
fc1891c7
PM
8833 /* UNPREDICTABLE in ARMv5; we choose to take a
8834 * page translation fault.
8835 */
f989983e 8836 fi->type = ARMFault_Translation;
554b0b09
PM
8837 goto do_fault;
8838 }
8839 } else {
8840 phys_addr = (desc & 0xfffffc00) | (address & 0x3ff);
fc1891c7 8841 *page_size = 0x400;
554b0b09 8842 }
9ee6e8bb 8843 ap = (desc >> 4) & 3;
ce819861
PB
8844 break;
8845 default:
9ee6e8bb
PB
8846 /* Never happens, but compiler isn't smart enough to tell. */
8847 abort();
ce819861 8848 }
9ee6e8bb 8849 }
0fbf5238
AJ
8850 *prot = ap_to_rw_prot(env, mmu_idx, ap, domain_prot);
8851 *prot |= *prot ? PAGE_EXEC : 0;
8852 if (!(*prot & (1 << access_type))) {
9ee6e8bb 8853 /* Access permission fault. */
f989983e 8854 fi->type = ARMFault_Permission;
9ee6e8bb
PB
8855 goto do_fault;
8856 }
8857 *phys_ptr = phys_addr;
b7cc4e82 8858 return false;
9ee6e8bb 8859do_fault:
f989983e
PM
8860 fi->domain = domain;
8861 fi->level = level;
b7cc4e82 8862 return true;
9ee6e8bb
PB
8863}
8864
b7cc4e82 8865static bool get_phys_addr_v6(CPUARMState *env, uint32_t address,
03ae85f8 8866 MMUAccessType access_type, ARMMMUIdx mmu_idx,
b7cc4e82 8867 hwaddr *phys_ptr, MemTxAttrs *attrs, int *prot,
f06cf243 8868 target_ulong *page_size, ARMMMUFaultInfo *fi)
9ee6e8bb 8869{
70d74660 8870 CPUState *cs = CPU(arm_env_get_cpu(env));
f06cf243 8871 int level = 1;
9ee6e8bb
PB
8872 uint32_t table;
8873 uint32_t desc;
8874 uint32_t xn;
de9b05b8 8875 uint32_t pxn = 0;
9ee6e8bb
PB
8876 int type;
8877 int ap;
de9b05b8 8878 int domain = 0;
dd4ebc2e 8879 int domain_prot;
a8170e5e 8880 hwaddr phys_addr;
0480f69a 8881 uint32_t dacr;
8bf5b6a9 8882 bool ns;
9ee6e8bb
PB
8883
8884 /* Pagetable walk. */
8885 /* Lookup l1 descriptor. */
0480f69a 8886 if (!get_level1_table_address(env, mmu_idx, &table, address)) {
e389be16 8887 /* Section translation fault if page walk is disabled by PD0 or PD1 */
f06cf243 8888 fi->type = ARMFault_Translation;
e389be16
FA
8889 goto do_fault;
8890 }
a614e698 8891 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
3795a6de 8892 mmu_idx, fi);
3b39d734
PM
8893 if (fi->type != ARMFault_None) {
8894 goto do_fault;
8895 }
9ee6e8bb 8896 type = (desc & 3);
de9b05b8
PM
8897 if (type == 0 || (type == 3 && !arm_feature(env, ARM_FEATURE_PXN))) {
8898 /* Section translation fault, or attempt to use the encoding
8899 * which is Reserved on implementations without PXN.
8900 */
f06cf243 8901 fi->type = ARMFault_Translation;
9ee6e8bb 8902 goto do_fault;
de9b05b8
PM
8903 }
8904 if ((type == 1) || !(desc & (1 << 18))) {
8905 /* Page or Section. */
dd4ebc2e 8906 domain = (desc >> 5) & 0x0f;
9ee6e8bb 8907 }
0480f69a
PM
8908 if (regime_el(env, mmu_idx) == 1) {
8909 dacr = env->cp15.dacr_ns;
8910 } else {
8911 dacr = env->cp15.dacr_s;
8912 }
f06cf243
PM
8913 if (type == 1) {
8914 level = 2;
8915 }
0480f69a 8916 domain_prot = (dacr >> (domain * 2)) & 3;
dd4ebc2e 8917 if (domain_prot == 0 || domain_prot == 2) {
f06cf243
PM
8918 /* Section or Page domain fault */
8919 fi->type = ARMFault_Domain;
9ee6e8bb
PB
8920 goto do_fault;
8921 }
de9b05b8 8922 if (type != 1) {
9ee6e8bb
PB
8923 if (desc & (1 << 18)) {
8924 /* Supersection. */
8925 phys_addr = (desc & 0xff000000) | (address & 0x00ffffff);
4e42a6ca
SF
8926 phys_addr |= (uint64_t)extract32(desc, 20, 4) << 32;
8927 phys_addr |= (uint64_t)extract32(desc, 5, 4) << 36;
d4c430a8 8928 *page_size = 0x1000000;
b5ff1b31 8929 } else {
9ee6e8bb
PB
8930 /* Section. */
8931 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
d4c430a8 8932 *page_size = 0x100000;
b5ff1b31 8933 }
9ee6e8bb
PB
8934 ap = ((desc >> 10) & 3) | ((desc >> 13) & 4);
8935 xn = desc & (1 << 4);
de9b05b8 8936 pxn = desc & 1;
8bf5b6a9 8937 ns = extract32(desc, 19, 1);
9ee6e8bb 8938 } else {
de9b05b8
PM
8939 if (arm_feature(env, ARM_FEATURE_PXN)) {
8940 pxn = (desc >> 2) & 1;
8941 }
8bf5b6a9 8942 ns = extract32(desc, 3, 1);
9ee6e8bb
PB
8943 /* Lookup l2 entry. */
8944 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
a614e698 8945 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
3795a6de 8946 mmu_idx, fi);
3b39d734
PM
8947 if (fi->type != ARMFault_None) {
8948 goto do_fault;
8949 }
9ee6e8bb
PB
8950 ap = ((desc >> 4) & 3) | ((desc >> 7) & 4);
8951 switch (desc & 3) {
8952 case 0: /* Page translation fault. */
f06cf243 8953 fi->type = ARMFault_Translation;
b5ff1b31 8954 goto do_fault;
9ee6e8bb
PB
8955 case 1: /* 64k page. */
8956 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
8957 xn = desc & (1 << 15);
d4c430a8 8958 *page_size = 0x10000;
9ee6e8bb
PB
8959 break;
8960 case 2: case 3: /* 4k page. */
8961 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
8962 xn = desc & 1;
d4c430a8 8963 *page_size = 0x1000;
9ee6e8bb
PB
8964 break;
8965 default:
8966 /* Never happens, but compiler isn't smart enough to tell. */
8967 abort();
b5ff1b31 8968 }
9ee6e8bb 8969 }
dd4ebc2e 8970 if (domain_prot == 3) {
c0034328
JR
8971 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
8972 } else {
0480f69a 8973 if (pxn && !regime_is_user(env, mmu_idx)) {
de9b05b8
PM
8974 xn = 1;
8975 }
f06cf243
PM
8976 if (xn && access_type == MMU_INST_FETCH) {
8977 fi->type = ARMFault_Permission;
c0034328 8978 goto do_fault;
f06cf243 8979 }
9ee6e8bb 8980
d76951b6
AJ
8981 if (arm_feature(env, ARM_FEATURE_V6K) &&
8982 (regime_sctlr(env, mmu_idx) & SCTLR_AFE)) {
8983 /* The simplified model uses AP[0] as an access control bit. */
8984 if ((ap & 1) == 0) {
8985 /* Access flag fault. */
f06cf243 8986 fi->type = ARMFault_AccessFlag;
d76951b6
AJ
8987 goto do_fault;
8988 }
8989 *prot = simple_ap_to_rw_prot(env, mmu_idx, ap >> 1);
8990 } else {
8991 *prot = ap_to_rw_prot(env, mmu_idx, ap, domain_prot);
c0034328 8992 }
0fbf5238
AJ
8993 if (*prot && !xn) {
8994 *prot |= PAGE_EXEC;
8995 }
8996 if (!(*prot & (1 << access_type))) {
c0034328 8997 /* Access permission fault. */
f06cf243 8998 fi->type = ARMFault_Permission;
c0034328
JR
8999 goto do_fault;
9000 }
3ad493fc 9001 }
8bf5b6a9
PM
9002 if (ns) {
9003 /* The NS bit will (as required by the architecture) have no effect if
9004 * the CPU doesn't support TZ or this is a non-secure translation
9005 * regime, because the attribute will already be non-secure.
9006 */
9007 attrs->secure = false;
9008 }
9ee6e8bb 9009 *phys_ptr = phys_addr;
b7cc4e82 9010 return false;
b5ff1b31 9011do_fault:
f06cf243
PM
9012 fi->domain = domain;
9013 fi->level = level;
b7cc4e82 9014 return true;
b5ff1b31
FB
9015}
9016
1853d5a9 9017/*
a0e966c9 9018 * check_s2_mmu_setup
1853d5a9
EI
9019 * @cpu: ARMCPU
9020 * @is_aa64: True if the translation regime is in AArch64 state
9021 * @startlevel: Suggested starting level
9022 * @inputsize: Bitsize of IPAs
9023 * @stride: Page-table stride (See the ARM ARM)
9024 *
a0e966c9
EI
9025 * Returns true if the suggested S2 translation parameters are OK and
9026 * false otherwise.
1853d5a9 9027 */
a0e966c9
EI
9028static bool check_s2_mmu_setup(ARMCPU *cpu, bool is_aa64, int level,
9029 int inputsize, int stride)
1853d5a9 9030{
98d68ec2
EI
9031 const int grainsize = stride + 3;
9032 int startsizecheck;
9033
1853d5a9
EI
9034 /* Negative levels are never allowed. */
9035 if (level < 0) {
9036 return false;
9037 }
9038
98d68ec2
EI
9039 startsizecheck = inputsize - ((3 - level) * stride + grainsize);
9040 if (startsizecheck < 1 || startsizecheck > stride + 4) {
9041 return false;
9042 }
9043
1853d5a9 9044 if (is_aa64) {
3526423e 9045 CPUARMState *env = &cpu->env;
1853d5a9
EI
9046 unsigned int pamax = arm_pamax(cpu);
9047
9048 switch (stride) {
9049 case 13: /* 64KB Pages. */
9050 if (level == 0 || (level == 1 && pamax <= 42)) {
9051 return false;
9052 }
9053 break;
9054 case 11: /* 16KB Pages. */
9055 if (level == 0 || (level == 1 && pamax <= 40)) {
9056 return false;
9057 }
9058 break;
9059 case 9: /* 4KB Pages. */
9060 if (level == 0 && pamax <= 42) {
9061 return false;
9062 }
9063 break;
9064 default:
9065 g_assert_not_reached();
9066 }
3526423e
EI
9067
9068 /* Inputsize checks. */
9069 if (inputsize > pamax &&
9070 (arm_el_is_aa64(env, 1) || inputsize > 40)) {
9071 /* This is CONSTRAINED UNPREDICTABLE and we choose to fault. */
9072 return false;
9073 }
1853d5a9 9074 } else {
1853d5a9
EI
9075 /* AArch32 only supports 4KB pages. Assert on that. */
9076 assert(stride == 9);
9077
9078 if (level == 0) {
9079 return false;
9080 }
1853d5a9
EI
9081 }
9082 return true;
9083}
9084
5b2d261d
AB
9085/* Translate from the 4-bit stage 2 representation of
9086 * memory attributes (without cache-allocation hints) to
9087 * the 8-bit representation of the stage 1 MAIR registers
9088 * (which includes allocation hints).
9089 *
9090 * ref: shared/translation/attrs/S2AttrDecode()
9091 * .../S2ConvertAttrsHints()
9092 */
9093static uint8_t convert_stage2_attrs(CPUARMState *env, uint8_t s2attrs)
9094{
9095 uint8_t hiattr = extract32(s2attrs, 2, 2);
9096 uint8_t loattr = extract32(s2attrs, 0, 2);
9097 uint8_t hihint = 0, lohint = 0;
9098
9099 if (hiattr != 0) { /* normal memory */
9100 if ((env->cp15.hcr_el2 & HCR_CD) != 0) { /* cache disabled */
9101 hiattr = loattr = 1; /* non-cacheable */
9102 } else {
9103 if (hiattr != 1) { /* Write-through or write-back */
9104 hihint = 3; /* RW allocate */
9105 }
9106 if (loattr != 1) { /* Write-through or write-back */
9107 lohint = 3; /* RW allocate */
9108 }
9109 }
9110 }
9111
9112 return (hiattr << 6) | (hihint << 4) | (loattr << 2) | lohint;
9113}
9114
b7cc4e82 9115static bool get_phys_addr_lpae(CPUARMState *env, target_ulong address,
03ae85f8 9116 MMUAccessType access_type, ARMMMUIdx mmu_idx,
b7cc4e82 9117 hwaddr *phys_ptr, MemTxAttrs *txattrs, int *prot,
da909b2c 9118 target_ulong *page_size_ptr,
5b2d261d 9119 ARMMMUFaultInfo *fi, ARMCacheAttrs *cacheattrs)
3dde962f 9120{
1853d5a9
EI
9121 ARMCPU *cpu = arm_env_get_cpu(env);
9122 CPUState *cs = CPU(cpu);
3dde962f 9123 /* Read an LPAE long-descriptor translation table. */
da909b2c 9124 ARMFaultType fault_type = ARMFault_Translation;
1b4093ea 9125 uint32_t level;
0c5fbf3b 9126 uint32_t epd = 0;
1f4c8c18 9127 int32_t t0sz, t1sz;
2c8dd318 9128 uint32_t tg;
3dde962f
PM
9129 uint64_t ttbr;
9130 int ttbr_select;
dddb5223 9131 hwaddr descaddr, indexmask, indexmask_grainsize;
3dde962f
PM
9132 uint32_t tableattrs;
9133 target_ulong page_size;
9134 uint32_t attrs;
973a5434 9135 int32_t stride = 9;
6e99f762 9136 int32_t addrsize;
4ca6a051 9137 int inputsize;
2c8dd318 9138 int32_t tbi = 0;
0480f69a 9139 TCR *tcr = regime_tcr(env, mmu_idx);
d8e052b3 9140 int ap, ns, xn, pxn;
88e8add8
GB
9141 uint32_t el = regime_el(env, mmu_idx);
9142 bool ttbr1_valid = true;
6109769a 9143 uint64_t descaddrmask;
6e99f762 9144 bool aarch64 = arm_el_is_aa64(env, el);
0480f69a
PM
9145
9146 /* TODO:
88e8add8
GB
9147 * This code does not handle the different format TCR for VTCR_EL2.
9148 * This code also does not support shareability levels.
9149 * Attribute and permission bit handling should also be checked when adding
9150 * support for those page table walks.
0480f69a 9151 */
6e99f762 9152 if (aarch64) {
1b4093ea 9153 level = 0;
6e99f762 9154 addrsize = 64;
88e8add8 9155 if (el > 1) {
1edee470
EI
9156 if (mmu_idx != ARMMMUIdx_S2NS) {
9157 tbi = extract64(tcr->raw_tcr, 20, 1);
9158 }
88e8add8
GB
9159 } else {
9160 if (extract64(address, 55, 1)) {
9161 tbi = extract64(tcr->raw_tcr, 38, 1);
9162 } else {
9163 tbi = extract64(tcr->raw_tcr, 37, 1);
9164 }
9165 }
2c8dd318 9166 tbi *= 8;
88e8add8
GB
9167
9168 /* If we are in 64-bit EL2 or EL3 then there is no TTBR1, so mark it
9169 * invalid.
9170 */
9171 if (el > 1) {
9172 ttbr1_valid = false;
9173 }
d0a2cbce 9174 } else {
1b4093ea 9175 level = 1;
6e99f762 9176 addrsize = 32;
d0a2cbce
PM
9177 /* There is no TTBR1 for EL2 */
9178 if (el == 2) {
9179 ttbr1_valid = false;
9180 }
2c8dd318 9181 }
3dde962f
PM
9182
9183 /* Determine whether this address is in the region controlled by
9184 * TTBR0 or TTBR1 (or if it is in neither region and should fault).
9185 * This is a Non-secure PL0/1 stage 1 translation, so controlled by
9186 * TTBCR/TTBR0/TTBR1 in accordance with ARM ARM DDI0406C table B-32:
9187 */
6e99f762 9188 if (aarch64) {
4ee38098
EI
9189 /* AArch64 translation. */
9190 t0sz = extract32(tcr->raw_tcr, 0, 6);
2c8dd318
RH
9191 t0sz = MIN(t0sz, 39);
9192 t0sz = MAX(t0sz, 16);
4ee38098
EI
9193 } else if (mmu_idx != ARMMMUIdx_S2NS) {
9194 /* AArch32 stage 1 translation. */
9195 t0sz = extract32(tcr->raw_tcr, 0, 3);
9196 } else {
9197 /* AArch32 stage 2 translation. */
9198 bool sext = extract32(tcr->raw_tcr, 4, 1);
9199 bool sign = extract32(tcr->raw_tcr, 3, 1);
6e99f762
SS
9200 /* Address size is 40-bit for a stage 2 translation,
9201 * and t0sz can be negative (from -8 to 7),
9202 * so we need to adjust it to use the TTBR selecting logic below.
9203 */
9204 addrsize = 40;
9205 t0sz = sextract32(tcr->raw_tcr, 0, 4) + 8;
4ee38098
EI
9206
9207 /* If the sign-extend bit is not the same as t0sz[3], the result
9208 * is unpredictable. Flag this as a guest error. */
9209 if (sign != sext) {
9210 qemu_log_mask(LOG_GUEST_ERROR,
39cba610 9211 "AArch32: VTCR.S / VTCR.T0SZ[3] mismatch\n");
4ee38098 9212 }
2c8dd318 9213 }
1f4c8c18 9214 t1sz = extract32(tcr->raw_tcr, 16, 6);
6e99f762 9215 if (aarch64) {
2c8dd318
RH
9216 t1sz = MIN(t1sz, 39);
9217 t1sz = MAX(t1sz, 16);
9218 }
6e99f762 9219 if (t0sz && !extract64(address, addrsize - t0sz, t0sz - tbi)) {
3dde962f
PM
9220 /* there is a ttbr0 region and we are in it (high bits all zero) */
9221 ttbr_select = 0;
88e8add8 9222 } else if (ttbr1_valid && t1sz &&
6e99f762 9223 !extract64(~address, addrsize - t1sz, t1sz - tbi)) {
3dde962f
PM
9224 /* there is a ttbr1 region and we are in it (high bits all one) */
9225 ttbr_select = 1;
9226 } else if (!t0sz) {
9227 /* ttbr0 region is "everything not in the ttbr1 region" */
9228 ttbr_select = 0;
88e8add8 9229 } else if (!t1sz && ttbr1_valid) {
3dde962f
PM
9230 /* ttbr1 region is "everything not in the ttbr0 region" */
9231 ttbr_select = 1;
9232 } else {
9233 /* in the gap between the two regions, this is a Translation fault */
da909b2c 9234 fault_type = ARMFault_Translation;
3dde962f
PM
9235 goto do_fault;
9236 }
9237
9238 /* Note that QEMU ignores shareability and cacheability attributes,
9239 * so we don't need to do anything with the SH, ORGN, IRGN fields
9240 * in the TTBCR. Similarly, TTBCR:A1 selects whether we get the
9241 * ASID from TTBR0 or TTBR1, but QEMU's TLB doesn't currently
9242 * implement any ASID-like capability so we can ignore it (instead
9243 * we will always flush the TLB any time the ASID is changed).
9244 */
9245 if (ttbr_select == 0) {
aef878be 9246 ttbr = regime_ttbr(env, mmu_idx, 0);
0c5fbf3b
EI
9247 if (el < 2) {
9248 epd = extract32(tcr->raw_tcr, 7, 1);
9249 }
6e99f762 9250 inputsize = addrsize - t0sz;
2c8dd318 9251
11f136ee 9252 tg = extract32(tcr->raw_tcr, 14, 2);
2c8dd318 9253 if (tg == 1) { /* 64KB pages */
973a5434 9254 stride = 13;
2c8dd318
RH
9255 }
9256 if (tg == 2) { /* 16KB pages */
973a5434 9257 stride = 11;
2c8dd318 9258 }
3dde962f 9259 } else {
88e8add8
GB
9260 /* We should only be here if TTBR1 is valid */
9261 assert(ttbr1_valid);
9262
aef878be 9263 ttbr = regime_ttbr(env, mmu_idx, 1);
11f136ee 9264 epd = extract32(tcr->raw_tcr, 23, 1);
6e99f762 9265 inputsize = addrsize - t1sz;
2c8dd318 9266
11f136ee 9267 tg = extract32(tcr->raw_tcr, 30, 2);
2c8dd318 9268 if (tg == 3) { /* 64KB pages */
973a5434 9269 stride = 13;
2c8dd318
RH
9270 }
9271 if (tg == 1) { /* 16KB pages */
973a5434 9272 stride = 11;
2c8dd318 9273 }
3dde962f
PM
9274 }
9275
0480f69a 9276 /* Here we should have set up all the parameters for the translation:
6e99f762 9277 * inputsize, ttbr, epd, stride, tbi
0480f69a
PM
9278 */
9279
3dde962f 9280 if (epd) {
88e8add8
GB
9281 /* Translation table walk disabled => Translation fault on TLB miss
9282 * Note: This is always 0 on 64-bit EL2 and EL3.
9283 */
3dde962f
PM
9284 goto do_fault;
9285 }
9286
1853d5a9
EI
9287 if (mmu_idx != ARMMMUIdx_S2NS) {
9288 /* The starting level depends on the virtual address size (which can
9289 * be up to 48 bits) and the translation granule size. It indicates
9290 * the number of strides (stride bits at a time) needed to
9291 * consume the bits of the input address. In the pseudocode this is:
9292 * level = 4 - RoundUp((inputsize - grainsize) / stride)
9293 * where their 'inputsize' is our 'inputsize', 'grainsize' is
9294 * our 'stride + 3' and 'stride' is our 'stride'.
9295 * Applying the usual "rounded up m/n is (m+n-1)/n" and simplifying:
9296 * = 4 - (inputsize - stride - 3 + stride - 1) / stride
9297 * = 4 - (inputsize - 4) / stride;
9298 */
9299 level = 4 - (inputsize - 4) / stride;
9300 } else {
9301 /* For stage 2 translations the starting level is specified by the
9302 * VTCR_EL2.SL0 field (whose interpretation depends on the page size)
9303 */
1b4093ea
SS
9304 uint32_t sl0 = extract32(tcr->raw_tcr, 6, 2);
9305 uint32_t startlevel;
1853d5a9
EI
9306 bool ok;
9307
6e99f762 9308 if (!aarch64 || stride == 9) {
1853d5a9 9309 /* AArch32 or 4KB pages */
1b4093ea 9310 startlevel = 2 - sl0;
1853d5a9
EI
9311 } else {
9312 /* 16KB or 64KB pages */
1b4093ea 9313 startlevel = 3 - sl0;
1853d5a9
EI
9314 }
9315
9316 /* Check that the starting level is valid. */
6e99f762 9317 ok = check_s2_mmu_setup(cpu, aarch64, startlevel,
1b4093ea 9318 inputsize, stride);
1853d5a9 9319 if (!ok) {
da909b2c 9320 fault_type = ARMFault_Translation;
1853d5a9
EI
9321 goto do_fault;
9322 }
1b4093ea 9323 level = startlevel;
1853d5a9 9324 }
3dde962f 9325
dddb5223
SS
9326 indexmask_grainsize = (1ULL << (stride + 3)) - 1;
9327 indexmask = (1ULL << (inputsize - (stride * (4 - level)))) - 1;
3dde962f
PM
9328
9329 /* Now we can extract the actual base address from the TTBR */
2c8dd318 9330 descaddr = extract64(ttbr, 0, 48);
dddb5223 9331 descaddr &= ~indexmask;
3dde962f 9332
6109769a 9333 /* The address field in the descriptor goes up to bit 39 for ARMv7
dddb5223
SS
9334 * but up to bit 47 for ARMv8, but we use the descaddrmask
9335 * up to bit 39 for AArch32, because we don't need other bits in that case
9336 * to construct next descriptor address (anyway they should be all zeroes).
6109769a 9337 */
6e99f762 9338 descaddrmask = ((1ull << (aarch64 ? 48 : 40)) - 1) &
dddb5223 9339 ~indexmask_grainsize;
6109769a 9340
ebca90e4
PM
9341 /* Secure accesses start with the page table in secure memory and
9342 * can be downgraded to non-secure at any step. Non-secure accesses
9343 * remain non-secure. We implement this by just ORing in the NSTable/NS
9344 * bits at each step.
9345 */
9346 tableattrs = regime_is_secure(env, mmu_idx) ? 0 : (1 << 4);
3dde962f
PM
9347 for (;;) {
9348 uint64_t descriptor;
ebca90e4 9349 bool nstable;
3dde962f 9350
dddb5223 9351 descaddr |= (address >> (stride * (4 - level))) & indexmask;
2c8dd318 9352 descaddr &= ~7ULL;
ebca90e4 9353 nstable = extract32(tableattrs, 4, 1);
3795a6de 9354 descriptor = arm_ldq_ptw(cs, descaddr, !nstable, mmu_idx, fi);
3b39d734 9355 if (fi->type != ARMFault_None) {
37785977
EI
9356 goto do_fault;
9357 }
9358
3dde962f
PM
9359 if (!(descriptor & 1) ||
9360 (!(descriptor & 2) && (level == 3))) {
9361 /* Invalid, or the Reserved level 3 encoding */
9362 goto do_fault;
9363 }
6109769a 9364 descaddr = descriptor & descaddrmask;
3dde962f
PM
9365
9366 if ((descriptor & 2) && (level < 3)) {
9367 /* Table entry. The top five bits are attributes which may
9368 * propagate down through lower levels of the table (and
9369 * which are all arranged so that 0 means "no effect", so
9370 * we can gather them up by ORing in the bits at each level).
9371 */
9372 tableattrs |= extract64(descriptor, 59, 5);
9373 level++;
dddb5223 9374 indexmask = indexmask_grainsize;
3dde962f
PM
9375 continue;
9376 }
9377 /* Block entry at level 1 or 2, or page entry at level 3.
9378 * These are basically the same thing, although the number
9379 * of bits we pull in from the vaddr varies.
9380 */
973a5434 9381 page_size = (1ULL << ((stride * (4 - level)) + 3));
3dde962f 9382 descaddr |= (address & (page_size - 1));
6ab1a5ee 9383 /* Extract attributes from the descriptor */
d615efac
IC
9384 attrs = extract64(descriptor, 2, 10)
9385 | (extract64(descriptor, 52, 12) << 10);
6ab1a5ee
EI
9386
9387 if (mmu_idx == ARMMMUIdx_S2NS) {
9388 /* Stage 2 table descriptors do not include any attribute fields */
9389 break;
9390 }
9391 /* Merge in attributes from table descriptors */
3dde962f
PM
9392 attrs |= extract32(tableattrs, 0, 2) << 11; /* XN, PXN */
9393 attrs |= extract32(tableattrs, 3, 1) << 5; /* APTable[1] => AP[2] */
9394 /* The sense of AP[1] vs APTable[0] is reversed, as APTable[0] == 1
9395 * means "force PL1 access only", which means forcing AP[1] to 0.
9396 */
9397 if (extract32(tableattrs, 2, 1)) {
9398 attrs &= ~(1 << 4);
9399 }
ebca90e4 9400 attrs |= nstable << 3; /* NS */
3dde962f
PM
9401 break;
9402 }
9403 /* Here descaddr is the final physical address, and attributes
9404 * are all in attrs.
9405 */
da909b2c 9406 fault_type = ARMFault_AccessFlag;
3dde962f
PM
9407 if ((attrs & (1 << 8)) == 0) {
9408 /* Access flag */
9409 goto do_fault;
9410 }
d8e052b3
AJ
9411
9412 ap = extract32(attrs, 4, 2);
d8e052b3 9413 xn = extract32(attrs, 12, 1);
d8e052b3 9414
6ab1a5ee
EI
9415 if (mmu_idx == ARMMMUIdx_S2NS) {
9416 ns = true;
9417 *prot = get_S2prot(env, ap, xn);
9418 } else {
9419 ns = extract32(attrs, 3, 1);
9420 pxn = extract32(attrs, 11, 1);
6e99f762 9421 *prot = get_S1prot(env, mmu_idx, aarch64, ap, ns, xn, pxn);
6ab1a5ee 9422 }
d8e052b3 9423
da909b2c 9424 fault_type = ARMFault_Permission;
d8e052b3 9425 if (!(*prot & (1 << access_type))) {
3dde962f
PM
9426 goto do_fault;
9427 }
3dde962f 9428
8bf5b6a9
PM
9429 if (ns) {
9430 /* The NS bit will (as required by the architecture) have no effect if
9431 * the CPU doesn't support TZ or this is a non-secure translation
9432 * regime, because the attribute will already be non-secure.
9433 */
9434 txattrs->secure = false;
9435 }
5b2d261d
AB
9436
9437 if (cacheattrs != NULL) {
9438 if (mmu_idx == ARMMMUIdx_S2NS) {
9439 cacheattrs->attrs = convert_stage2_attrs(env,
9440 extract32(attrs, 0, 4));
9441 } else {
9442 /* Index into MAIR registers for cache attributes */
9443 uint8_t attrindx = extract32(attrs, 0, 3);
9444 uint64_t mair = env->cp15.mair_el[regime_el(env, mmu_idx)];
9445 assert(attrindx <= 7);
9446 cacheattrs->attrs = extract64(mair, attrindx * 8, 8);
9447 }
9448 cacheattrs->shareability = extract32(attrs, 6, 2);
9449 }
9450
3dde962f
PM
9451 *phys_ptr = descaddr;
9452 *page_size_ptr = page_size;
b7cc4e82 9453 return false;
3dde962f
PM
9454
9455do_fault:
da909b2c
PM
9456 fi->type = fault_type;
9457 fi->level = level;
37785977
EI
9458 /* Tag the error as S2 for failed S1 PTW at S2 or ordinary S2. */
9459 fi->stage2 = fi->s1ptw || (mmu_idx == ARMMMUIdx_S2NS);
b7cc4e82 9460 return true;
3dde962f
PM
9461}
9462
f6bda88f
PC
9463static inline void get_phys_addr_pmsav7_default(CPUARMState *env,
9464 ARMMMUIdx mmu_idx,
9465 int32_t address, int *prot)
9466{
3a00d560
MD
9467 if (!arm_feature(env, ARM_FEATURE_M)) {
9468 *prot = PAGE_READ | PAGE_WRITE;
9469 switch (address) {
9470 case 0xF0000000 ... 0xFFFFFFFF:
9471 if (regime_sctlr(env, mmu_idx) & SCTLR_V) {
9472 /* hivecs execing is ok */
9473 *prot |= PAGE_EXEC;
9474 }
9475 break;
9476 case 0x00000000 ... 0x7FFFFFFF:
f6bda88f 9477 *prot |= PAGE_EXEC;
3a00d560
MD
9478 break;
9479 }
9480 } else {
9481 /* Default system address map for M profile cores.
9482 * The architecture specifies which regions are execute-never;
9483 * at the MPU level no other checks are defined.
9484 */
9485 switch (address) {
9486 case 0x00000000 ... 0x1fffffff: /* ROM */
9487 case 0x20000000 ... 0x3fffffff: /* SRAM */
9488 case 0x60000000 ... 0x7fffffff: /* RAM */
9489 case 0x80000000 ... 0x9fffffff: /* RAM */
9490 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
9491 break;
9492 case 0x40000000 ... 0x5fffffff: /* Peripheral */
9493 case 0xa0000000 ... 0xbfffffff: /* Device */
9494 case 0xc0000000 ... 0xdfffffff: /* Device */
9495 case 0xe0000000 ... 0xffffffff: /* System */
9496 *prot = PAGE_READ | PAGE_WRITE;
9497 break;
9498 default:
9499 g_assert_not_reached();
f6bda88f 9500 }
f6bda88f 9501 }
f6bda88f
PC
9502}
9503
29c483a5
MD
9504static bool pmsav7_use_background_region(ARMCPU *cpu,
9505 ARMMMUIdx mmu_idx, bool is_user)
9506{
9507 /* Return true if we should use the default memory map as a
9508 * "background" region if there are no hits against any MPU regions.
9509 */
9510 CPUARMState *env = &cpu->env;
9511
9512 if (is_user) {
9513 return false;
9514 }
9515
9516 if (arm_feature(env, ARM_FEATURE_M)) {
ecf5e8ea
PM
9517 return env->v7m.mpu_ctrl[regime_is_secure(env, mmu_idx)]
9518 & R_V7M_MPU_CTRL_PRIVDEFENA_MASK;
29c483a5
MD
9519 } else {
9520 return regime_sctlr(env, mmu_idx) & SCTLR_BR;
9521 }
9522}
9523
38aaa60c
PM
9524static inline bool m_is_ppb_region(CPUARMState *env, uint32_t address)
9525{
9526 /* True if address is in the M profile PPB region 0xe0000000 - 0xe00fffff */
9527 return arm_feature(env, ARM_FEATURE_M) &&
9528 extract32(address, 20, 12) == 0xe00;
9529}
9530
bf446a11
PM
9531static inline bool m_is_system_region(CPUARMState *env, uint32_t address)
9532{
9533 /* True if address is in the M profile system region
9534 * 0xe0000000 - 0xffffffff
9535 */
9536 return arm_feature(env, ARM_FEATURE_M) && extract32(address, 29, 3) == 0x7;
9537}
9538
f6bda88f 9539static bool get_phys_addr_pmsav7(CPUARMState *env, uint32_t address,
03ae85f8 9540 MMUAccessType access_type, ARMMMUIdx mmu_idx,
9375ad15
PM
9541 hwaddr *phys_ptr, int *prot,
9542 ARMMMUFaultInfo *fi)
f6bda88f
PC
9543{
9544 ARMCPU *cpu = arm_env_get_cpu(env);
9545 int n;
9546 bool is_user = regime_is_user(env, mmu_idx);
9547
9548 *phys_ptr = address;
9549 *prot = 0;
9550
38aaa60c
PM
9551 if (regime_translation_disabled(env, mmu_idx) ||
9552 m_is_ppb_region(env, address)) {
9553 /* MPU disabled or M profile PPB access: use default memory map.
9554 * The other case which uses the default memory map in the
9555 * v7M ARM ARM pseudocode is exception vector reads from the vector
9556 * table. In QEMU those accesses are done in arm_v7m_load_vector(),
9557 * which always does a direct read using address_space_ldl(), rather
9558 * than going via this function, so we don't need to check that here.
9559 */
f6bda88f
PC
9560 get_phys_addr_pmsav7_default(env, mmu_idx, address, prot);
9561 } else { /* MPU enabled */
9562 for (n = (int)cpu->pmsav7_dregion - 1; n >= 0; n--) {
9563 /* region search */
9564 uint32_t base = env->pmsav7.drbar[n];
9565 uint32_t rsize = extract32(env->pmsav7.drsr[n], 1, 5);
9566 uint32_t rmask;
9567 bool srdis = false;
9568
9569 if (!(env->pmsav7.drsr[n] & 0x1)) {
9570 continue;
9571 }
9572
9573 if (!rsize) {
c9f9f124
MD
9574 qemu_log_mask(LOG_GUEST_ERROR,
9575 "DRSR[%d]: Rsize field cannot be 0\n", n);
f6bda88f
PC
9576 continue;
9577 }
9578 rsize++;
9579 rmask = (1ull << rsize) - 1;
9580
9581 if (base & rmask) {
c9f9f124
MD
9582 qemu_log_mask(LOG_GUEST_ERROR,
9583 "DRBAR[%d]: 0x%" PRIx32 " misaligned "
9584 "to DRSR region size, mask = 0x%" PRIx32 "\n",
9585 n, base, rmask);
f6bda88f
PC
9586 continue;
9587 }
9588
9589 if (address < base || address > base + rmask) {
9590 continue;
9591 }
9592
9593 /* Region matched */
9594
9595 if (rsize >= 8) { /* no subregions for regions < 256 bytes */
9596 int i, snd;
9597 uint32_t srdis_mask;
9598
9599 rsize -= 3; /* sub region size (power of 2) */
9600 snd = ((address - base) >> rsize) & 0x7;
9601 srdis = extract32(env->pmsav7.drsr[n], snd + 8, 1);
9602
9603 srdis_mask = srdis ? 0x3 : 0x0;
9604 for (i = 2; i <= 8 && rsize < TARGET_PAGE_BITS; i *= 2) {
9605 /* This will check in groups of 2, 4 and then 8, whether
9606 * the subregion bits are consistent. rsize is incremented
9607 * back up to give the region size, considering consistent
9608 * adjacent subregions as one region. Stop testing if rsize
9609 * is already big enough for an entire QEMU page.
9610 */
9611 int snd_rounded = snd & ~(i - 1);
9612 uint32_t srdis_multi = extract32(env->pmsav7.drsr[n],
9613 snd_rounded + 8, i);
9614 if (srdis_mask ^ srdis_multi) {
9615 break;
9616 }
9617 srdis_mask = (srdis_mask << i) | srdis_mask;
9618 rsize++;
9619 }
9620 }
9621 if (rsize < TARGET_PAGE_BITS) {
c9f9f124 9622 qemu_log_mask(LOG_UNIMP,
8aec759b
PM
9623 "DRSR[%d]: No support for MPU (sub)region size of"
9624 " %" PRIu32 " bytes. Minimum is %d.\n",
9625 n, (1 << rsize), TARGET_PAGE_SIZE);
f6bda88f
PC
9626 continue;
9627 }
9628 if (srdis) {
9629 continue;
9630 }
9631 break;
9632 }
9633
9634 if (n == -1) { /* no hits */
29c483a5 9635 if (!pmsav7_use_background_region(cpu, mmu_idx, is_user)) {
f6bda88f 9636 /* background fault */
9375ad15 9637 fi->type = ARMFault_Background;
f6bda88f
PC
9638 return true;
9639 }
9640 get_phys_addr_pmsav7_default(env, mmu_idx, address, prot);
9641 } else { /* a MPU hit! */
9642 uint32_t ap = extract32(env->pmsav7.dracr[n], 8, 3);
bf446a11
PM
9643 uint32_t xn = extract32(env->pmsav7.dracr[n], 12, 1);
9644
9645 if (m_is_system_region(env, address)) {
9646 /* System space is always execute never */
9647 xn = 1;
9648 }
f6bda88f
PC
9649
9650 if (is_user) { /* User mode AP bit decoding */
9651 switch (ap) {
9652 case 0:
9653 case 1:
9654 case 5:
9655 break; /* no access */
9656 case 3:
9657 *prot |= PAGE_WRITE;
9658 /* fall through */
9659 case 2:
9660 case 6:
9661 *prot |= PAGE_READ | PAGE_EXEC;
9662 break;
8638f1ad
PM
9663 case 7:
9664 /* for v7M, same as 6; for R profile a reserved value */
9665 if (arm_feature(env, ARM_FEATURE_M)) {
9666 *prot |= PAGE_READ | PAGE_EXEC;
9667 break;
9668 }
9669 /* fall through */
f6bda88f
PC
9670 default:
9671 qemu_log_mask(LOG_GUEST_ERROR,
c9f9f124
MD
9672 "DRACR[%d]: Bad value for AP bits: 0x%"
9673 PRIx32 "\n", n, ap);
f6bda88f
PC
9674 }
9675 } else { /* Priv. mode AP bits decoding */
9676 switch (ap) {
9677 case 0:
9678 break; /* no access */
9679 case 1:
9680 case 2:
9681 case 3:
9682 *prot |= PAGE_WRITE;
9683 /* fall through */
9684 case 5:
9685 case 6:
9686 *prot |= PAGE_READ | PAGE_EXEC;
9687 break;
8638f1ad
PM
9688 case 7:
9689 /* for v7M, same as 6; for R profile a reserved value */
9690 if (arm_feature(env, ARM_FEATURE_M)) {
9691 *prot |= PAGE_READ | PAGE_EXEC;
9692 break;
9693 }
9694 /* fall through */
f6bda88f
PC
9695 default:
9696 qemu_log_mask(LOG_GUEST_ERROR,
c9f9f124
MD
9697 "DRACR[%d]: Bad value for AP bits: 0x%"
9698 PRIx32 "\n", n, ap);
f6bda88f
PC
9699 }
9700 }
9701
9702 /* execute never */
bf446a11 9703 if (xn) {
f6bda88f
PC
9704 *prot &= ~PAGE_EXEC;
9705 }
9706 }
9707 }
9708
9375ad15
PM
9709 fi->type = ARMFault_Permission;
9710 fi->level = 1;
f6bda88f
PC
9711 return !(*prot & (1 << access_type));
9712}
9713
35337cc3
PM
9714static bool v8m_is_sau_exempt(CPUARMState *env,
9715 uint32_t address, MMUAccessType access_type)
9716{
9717 /* The architecture specifies that certain address ranges are
9718 * exempt from v8M SAU/IDAU checks.
9719 */
9720 return
9721 (access_type == MMU_INST_FETCH && m_is_system_region(env, address)) ||
9722 (address >= 0xe0000000 && address <= 0xe0002fff) ||
9723 (address >= 0xe000e000 && address <= 0xe000efff) ||
9724 (address >= 0xe002e000 && address <= 0xe002efff) ||
9725 (address >= 0xe0040000 && address <= 0xe0041fff) ||
9726 (address >= 0xe00ff000 && address <= 0xe00fffff);
9727}
9728
9729static void v8m_security_lookup(CPUARMState *env, uint32_t address,
9730 MMUAccessType access_type, ARMMMUIdx mmu_idx,
9731 V8M_SAttributes *sattrs)
9732{
9733 /* Look up the security attributes for this address. Compare the
9734 * pseudocode SecurityCheck() function.
9735 * We assume the caller has zero-initialized *sattrs.
9736 */
9737 ARMCPU *cpu = arm_env_get_cpu(env);
9738 int r;
181962fd
PM
9739 bool idau_exempt = false, idau_ns = true, idau_nsc = true;
9740 int idau_region = IREGION_NOTVALID;
35337cc3 9741
181962fd
PM
9742 if (cpu->idau) {
9743 IDAUInterfaceClass *iic = IDAU_INTERFACE_GET_CLASS(cpu->idau);
9744 IDAUInterface *ii = IDAU_INTERFACE(cpu->idau);
9745
9746 iic->check(ii, address, &idau_region, &idau_exempt, &idau_ns,
9747 &idau_nsc);
9748 }
35337cc3
PM
9749
9750 if (access_type == MMU_INST_FETCH && extract32(address, 28, 4) == 0xf) {
9751 /* 0xf0000000..0xffffffff is always S for insn fetches */
9752 return;
9753 }
9754
181962fd 9755 if (idau_exempt || v8m_is_sau_exempt(env, address, access_type)) {
35337cc3
PM
9756 sattrs->ns = !regime_is_secure(env, mmu_idx);
9757 return;
9758 }
9759
181962fd
PM
9760 if (idau_region != IREGION_NOTVALID) {
9761 sattrs->irvalid = true;
9762 sattrs->iregion = idau_region;
9763 }
9764
35337cc3
PM
9765 switch (env->sau.ctrl & 3) {
9766 case 0: /* SAU.ENABLE == 0, SAU.ALLNS == 0 */
9767 break;
9768 case 2: /* SAU.ENABLE == 0, SAU.ALLNS == 1 */
9769 sattrs->ns = true;
9770 break;
9771 default: /* SAU.ENABLE == 1 */
9772 for (r = 0; r < cpu->sau_sregion; r++) {
9773 if (env->sau.rlar[r] & 1) {
9774 uint32_t base = env->sau.rbar[r] & ~0x1f;
9775 uint32_t limit = env->sau.rlar[r] | 0x1f;
9776
9777 if (base <= address && limit >= address) {
9778 if (sattrs->srvalid) {
9779 /* If we hit in more than one region then we must report
9780 * as Secure, not NS-Callable, with no valid region
9781 * number info.
9782 */
9783 sattrs->ns = false;
9784 sattrs->nsc = false;
9785 sattrs->sregion = 0;
9786 sattrs->srvalid = false;
9787 break;
9788 } else {
9789 if (env->sau.rlar[r] & 2) {
9790 sattrs->nsc = true;
9791 } else {
9792 sattrs->ns = true;
9793 }
9794 sattrs->srvalid = true;
9795 sattrs->sregion = r;
9796 }
9797 }
9798 }
9799 }
9800
181962fd
PM
9801 /* The IDAU will override the SAU lookup results if it specifies
9802 * higher security than the SAU does.
9803 */
9804 if (!idau_ns) {
9805 if (sattrs->ns || (!idau_nsc && sattrs->nsc)) {
9806 sattrs->ns = false;
9807 sattrs->nsc = idau_nsc;
9808 }
9809 }
35337cc3
PM
9810 break;
9811 }
9812}
9813
54317c0f
PM
9814static bool pmsav8_mpu_lookup(CPUARMState *env, uint32_t address,
9815 MMUAccessType access_type, ARMMMUIdx mmu_idx,
9816 hwaddr *phys_ptr, MemTxAttrs *txattrs,
3f551b5b 9817 int *prot, ARMMMUFaultInfo *fi, uint32_t *mregion)
54317c0f
PM
9818{
9819 /* Perform a PMSAv8 MPU lookup (without also doing the SAU check
9820 * that a full phys-to-virt translation does).
9821 * mregion is (if not NULL) set to the region number which matched,
9822 * or -1 if no region number is returned (MPU off, address did not
9823 * hit a region, address hit in multiple regions).
9824 */
504e3cc3
PM
9825 ARMCPU *cpu = arm_env_get_cpu(env);
9826 bool is_user = regime_is_user(env, mmu_idx);
62c58ee0 9827 uint32_t secure = regime_is_secure(env, mmu_idx);
504e3cc3
PM
9828 int n;
9829 int matchregion = -1;
9830 bool hit = false;
9831
9832 *phys_ptr = address;
9833 *prot = 0;
54317c0f
PM
9834 if (mregion) {
9835 *mregion = -1;
35337cc3
PM
9836 }
9837
504e3cc3
PM
9838 /* Unlike the ARM ARM pseudocode, we don't need to check whether this
9839 * was an exception vector read from the vector table (which is always
9840 * done using the default system address map), because those accesses
9841 * are done in arm_v7m_load_vector(), which always does a direct
9842 * read using address_space_ldl(), rather than going via this function.
9843 */
9844 if (regime_translation_disabled(env, mmu_idx)) { /* MPU disabled */
9845 hit = true;
9846 } else if (m_is_ppb_region(env, address)) {
9847 hit = true;
9848 } else if (pmsav7_use_background_region(cpu, mmu_idx, is_user)) {
9849 hit = true;
9850 } else {
9851 for (n = (int)cpu->pmsav7_dregion - 1; n >= 0; n--) {
9852 /* region search */
9853 /* Note that the base address is bits [31:5] from the register
9854 * with bits [4:0] all zeroes, but the limit address is bits
9855 * [31:5] from the register with bits [4:0] all ones.
9856 */
62c58ee0
PM
9857 uint32_t base = env->pmsav8.rbar[secure][n] & ~0x1f;
9858 uint32_t limit = env->pmsav8.rlar[secure][n] | 0x1f;
504e3cc3 9859
62c58ee0 9860 if (!(env->pmsav8.rlar[secure][n] & 0x1)) {
504e3cc3
PM
9861 /* Region disabled */
9862 continue;
9863 }
9864
9865 if (address < base || address > limit) {
9866 continue;
9867 }
9868
9869 if (hit) {
9870 /* Multiple regions match -- always a failure (unlike
9871 * PMSAv7 where highest-numbered-region wins)
9872 */
3f551b5b
PM
9873 fi->type = ARMFault_Permission;
9874 fi->level = 1;
504e3cc3
PM
9875 return true;
9876 }
9877
9878 matchregion = n;
9879 hit = true;
9880
9881 if (base & ~TARGET_PAGE_MASK) {
9882 qemu_log_mask(LOG_UNIMP,
9883 "MPU_RBAR[%d]: No support for MPU region base"
9884 "address of 0x%" PRIx32 ". Minimum alignment is "
9885 "%d\n",
9886 n, base, TARGET_PAGE_BITS);
9887 continue;
9888 }
9889 if ((limit + 1) & ~TARGET_PAGE_MASK) {
9890 qemu_log_mask(LOG_UNIMP,
9891 "MPU_RBAR[%d]: No support for MPU region limit"
9892 "address of 0x%" PRIx32 ". Minimum alignment is "
9893 "%d\n",
9894 n, limit, TARGET_PAGE_BITS);
9895 continue;
9896 }
9897 }
9898 }
9899
9900 if (!hit) {
9901 /* background fault */
3f551b5b 9902 fi->type = ARMFault_Background;
504e3cc3
PM
9903 return true;
9904 }
9905
9906 if (matchregion == -1) {
9907 /* hit using the background region */
9908 get_phys_addr_pmsav7_default(env, mmu_idx, address, prot);
9909 } else {
62c58ee0
PM
9910 uint32_t ap = extract32(env->pmsav8.rbar[secure][matchregion], 1, 2);
9911 uint32_t xn = extract32(env->pmsav8.rbar[secure][matchregion], 0, 1);
504e3cc3
PM
9912
9913 if (m_is_system_region(env, address)) {
9914 /* System space is always execute never */
9915 xn = 1;
9916 }
9917
9918 *prot = simple_ap_to_rw_prot(env, mmu_idx, ap);
9919 if (*prot && !xn) {
9920 *prot |= PAGE_EXEC;
9921 }
9922 /* We don't need to look the attribute up in the MAIR0/MAIR1
9923 * registers because that only tells us about cacheability.
9924 */
54317c0f
PM
9925 if (mregion) {
9926 *mregion = matchregion;
9927 }
504e3cc3
PM
9928 }
9929
3f551b5b
PM
9930 fi->type = ARMFault_Permission;
9931 fi->level = 1;
504e3cc3
PM
9932 return !(*prot & (1 << access_type));
9933}
9934
54317c0f
PM
9935
9936static bool get_phys_addr_pmsav8(CPUARMState *env, uint32_t address,
9937 MMUAccessType access_type, ARMMMUIdx mmu_idx,
9938 hwaddr *phys_ptr, MemTxAttrs *txattrs,
3f551b5b 9939 int *prot, ARMMMUFaultInfo *fi)
54317c0f
PM
9940{
9941 uint32_t secure = regime_is_secure(env, mmu_idx);
9942 V8M_SAttributes sattrs = {};
9943
9944 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
9945 v8m_security_lookup(env, address, access_type, mmu_idx, &sattrs);
9946 if (access_type == MMU_INST_FETCH) {
9947 /* Instruction fetches always use the MMU bank and the
9948 * transaction attribute determined by the fetch address,
9949 * regardless of CPU state. This is painful for QEMU
9950 * to handle, because it would mean we need to encode
9951 * into the mmu_idx not just the (user, negpri) information
9952 * for the current security state but also that for the
9953 * other security state, which would balloon the number
9954 * of mmu_idx values needed alarmingly.
9955 * Fortunately we can avoid this because it's not actually
9956 * possible to arbitrarily execute code from memory with
9957 * the wrong security attribute: it will always generate
9958 * an exception of some kind or another, apart from the
9959 * special case of an NS CPU executing an SG instruction
9960 * in S&NSC memory. So we always just fail the translation
9961 * here and sort things out in the exception handler
9962 * (including possibly emulating an SG instruction).
9963 */
9964 if (sattrs.ns != !secure) {
3f551b5b
PM
9965 if (sattrs.nsc) {
9966 fi->type = ARMFault_QEMU_NSCExec;
9967 } else {
9968 fi->type = ARMFault_QEMU_SFault;
9969 }
54317c0f
PM
9970 *phys_ptr = address;
9971 *prot = 0;
9972 return true;
9973 }
9974 } else {
9975 /* For data accesses we always use the MMU bank indicated
9976 * by the current CPU state, but the security attributes
9977 * might downgrade a secure access to nonsecure.
9978 */
9979 if (sattrs.ns) {
9980 txattrs->secure = false;
9981 } else if (!secure) {
9982 /* NS access to S memory must fault.
9983 * Architecturally we should first check whether the
9984 * MPU information for this address indicates that we
9985 * are doing an unaligned access to Device memory, which
9986 * should generate a UsageFault instead. QEMU does not
9987 * currently check for that kind of unaligned access though.
9988 * If we added it we would need to do so as a special case
9989 * for M_FAKE_FSR_SFAULT in arm_v7m_cpu_do_interrupt().
9990 */
3f551b5b 9991 fi->type = ARMFault_QEMU_SFault;
54317c0f
PM
9992 *phys_ptr = address;
9993 *prot = 0;
9994 return true;
9995 }
9996 }
9997 }
9998
9999 return pmsav8_mpu_lookup(env, address, access_type, mmu_idx, phys_ptr,
3f551b5b 10000 txattrs, prot, fi, NULL);
54317c0f
PM
10001}
10002
13689d43 10003static bool get_phys_addr_pmsav5(CPUARMState *env, uint32_t address,
03ae85f8 10004 MMUAccessType access_type, ARMMMUIdx mmu_idx,
53a4e5c5
PM
10005 hwaddr *phys_ptr, int *prot,
10006 ARMMMUFaultInfo *fi)
9ee6e8bb
PB
10007{
10008 int n;
10009 uint32_t mask;
10010 uint32_t base;
0480f69a 10011 bool is_user = regime_is_user(env, mmu_idx);
9ee6e8bb 10012
3279adb9
PM
10013 if (regime_translation_disabled(env, mmu_idx)) {
10014 /* MPU disabled. */
10015 *phys_ptr = address;
10016 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
10017 return false;
10018 }
10019
9ee6e8bb
PB
10020 *phys_ptr = address;
10021 for (n = 7; n >= 0; n--) {
554b0b09 10022 base = env->cp15.c6_region[n];
87c3d486 10023 if ((base & 1) == 0) {
554b0b09 10024 continue;
87c3d486 10025 }
554b0b09
PM
10026 mask = 1 << ((base >> 1) & 0x1f);
10027 /* Keep this shift separate from the above to avoid an
10028 (undefined) << 32. */
10029 mask = (mask << 1) - 1;
87c3d486 10030 if (((base ^ address) & ~mask) == 0) {
554b0b09 10031 break;
87c3d486 10032 }
9ee6e8bb 10033 }
87c3d486 10034 if (n < 0) {
53a4e5c5 10035 fi->type = ARMFault_Background;
b7cc4e82 10036 return true;
87c3d486 10037 }
9ee6e8bb 10038
03ae85f8 10039 if (access_type == MMU_INST_FETCH) {
7e09797c 10040 mask = env->cp15.pmsav5_insn_ap;
9ee6e8bb 10041 } else {
7e09797c 10042 mask = env->cp15.pmsav5_data_ap;
9ee6e8bb
PB
10043 }
10044 mask = (mask >> (n * 4)) & 0xf;
10045 switch (mask) {
10046 case 0:
53a4e5c5
PM
10047 fi->type = ARMFault_Permission;
10048 fi->level = 1;
b7cc4e82 10049 return true;
9ee6e8bb 10050 case 1:
87c3d486 10051 if (is_user) {
53a4e5c5
PM
10052 fi->type = ARMFault_Permission;
10053 fi->level = 1;
b7cc4e82 10054 return true;
87c3d486 10055 }
554b0b09
PM
10056 *prot = PAGE_READ | PAGE_WRITE;
10057 break;
9ee6e8bb 10058 case 2:
554b0b09 10059 *prot = PAGE_READ;
87c3d486 10060 if (!is_user) {
554b0b09 10061 *prot |= PAGE_WRITE;
87c3d486 10062 }
554b0b09 10063 break;
9ee6e8bb 10064 case 3:
554b0b09
PM
10065 *prot = PAGE_READ | PAGE_WRITE;
10066 break;
9ee6e8bb 10067 case 5:
87c3d486 10068 if (is_user) {
53a4e5c5
PM
10069 fi->type = ARMFault_Permission;
10070 fi->level = 1;
b7cc4e82 10071 return true;
87c3d486 10072 }
554b0b09
PM
10073 *prot = PAGE_READ;
10074 break;
9ee6e8bb 10075 case 6:
554b0b09
PM
10076 *prot = PAGE_READ;
10077 break;
9ee6e8bb 10078 default:
554b0b09 10079 /* Bad permission. */
53a4e5c5
PM
10080 fi->type = ARMFault_Permission;
10081 fi->level = 1;
b7cc4e82 10082 return true;
9ee6e8bb 10083 }
3ad493fc 10084 *prot |= PAGE_EXEC;
b7cc4e82 10085 return false;
9ee6e8bb
PB
10086}
10087
5b2d261d
AB
10088/* Combine either inner or outer cacheability attributes for normal
10089 * memory, according to table D4-42 and pseudocode procedure
10090 * CombineS1S2AttrHints() of ARM DDI 0487B.b (the ARMv8 ARM).
10091 *
10092 * NB: only stage 1 includes allocation hints (RW bits), leading to
10093 * some asymmetry.
10094 */
10095static uint8_t combine_cacheattr_nibble(uint8_t s1, uint8_t s2)
10096{
10097 if (s1 == 4 || s2 == 4) {
10098 /* non-cacheable has precedence */
10099 return 4;
10100 } else if (extract32(s1, 2, 2) == 0 || extract32(s1, 2, 2) == 2) {
10101 /* stage 1 write-through takes precedence */
10102 return s1;
10103 } else if (extract32(s2, 2, 2) == 2) {
10104 /* stage 2 write-through takes precedence, but the allocation hint
10105 * is still taken from stage 1
10106 */
10107 return (2 << 2) | extract32(s1, 0, 2);
10108 } else { /* write-back */
10109 return s1;
10110 }
10111}
10112
10113/* Combine S1 and S2 cacheability/shareability attributes, per D4.5.4
10114 * and CombineS1S2Desc()
10115 *
10116 * @s1: Attributes from stage 1 walk
10117 * @s2: Attributes from stage 2 walk
10118 */
10119static ARMCacheAttrs combine_cacheattrs(ARMCacheAttrs s1, ARMCacheAttrs s2)
10120{
10121 uint8_t s1lo = extract32(s1.attrs, 0, 4), s2lo = extract32(s2.attrs, 0, 4);
10122 uint8_t s1hi = extract32(s1.attrs, 4, 4), s2hi = extract32(s2.attrs, 4, 4);
10123 ARMCacheAttrs ret;
10124
10125 /* Combine shareability attributes (table D4-43) */
10126 if (s1.shareability == 2 || s2.shareability == 2) {
10127 /* if either are outer-shareable, the result is outer-shareable */
10128 ret.shareability = 2;
10129 } else if (s1.shareability == 3 || s2.shareability == 3) {
10130 /* if either are inner-shareable, the result is inner-shareable */
10131 ret.shareability = 3;
10132 } else {
10133 /* both non-shareable */
10134 ret.shareability = 0;
10135 }
10136
10137 /* Combine memory type and cacheability attributes */
10138 if (s1hi == 0 || s2hi == 0) {
10139 /* Device has precedence over normal */
10140 if (s1lo == 0 || s2lo == 0) {
10141 /* nGnRnE has precedence over anything */
10142 ret.attrs = 0;
10143 } else if (s1lo == 4 || s2lo == 4) {
10144 /* non-Reordering has precedence over Reordering */
10145 ret.attrs = 4; /* nGnRE */
10146 } else if (s1lo == 8 || s2lo == 8) {
10147 /* non-Gathering has precedence over Gathering */
10148 ret.attrs = 8; /* nGRE */
10149 } else {
10150 ret.attrs = 0xc; /* GRE */
10151 }
10152
10153 /* Any location for which the resultant memory type is any
10154 * type of Device memory is always treated as Outer Shareable.
10155 */
10156 ret.shareability = 2;
10157 } else { /* Normal memory */
10158 /* Outer/inner cacheability combine independently */
10159 ret.attrs = combine_cacheattr_nibble(s1hi, s2hi) << 4
10160 | combine_cacheattr_nibble(s1lo, s2lo);
10161
10162 if (ret.attrs == 0x44) {
10163 /* Any location for which the resultant memory type is Normal
10164 * Inner Non-cacheable, Outer Non-cacheable is always treated
10165 * as Outer Shareable.
10166 */
10167 ret.shareability = 2;
10168 }
10169 }
10170
10171 return ret;
10172}
10173
10174
702a9357
PM
10175/* get_phys_addr - get the physical address for this virtual address
10176 *
10177 * Find the physical address corresponding to the given virtual address,
10178 * by doing a translation table walk on MMU based systems or using the
10179 * MPU state on MPU based systems.
10180 *
b7cc4e82
PC
10181 * Returns false if the translation was successful. Otherwise, phys_ptr, attrs,
10182 * prot and page_size may not be filled in, and the populated fsr value provides
702a9357
PM
10183 * information on why the translation aborted, in the format of a
10184 * DFSR/IFSR fault register, with the following caveats:
10185 * * we honour the short vs long DFSR format differences.
10186 * * the WnR bit is never set (the caller must do this).
f6bda88f 10187 * * for PSMAv5 based systems we don't bother to return a full FSR format
702a9357
PM
10188 * value.
10189 *
10190 * @env: CPUARMState
10191 * @address: virtual address to get physical address for
10192 * @access_type: 0 for read, 1 for write, 2 for execute
d3649702 10193 * @mmu_idx: MMU index indicating required translation regime
702a9357 10194 * @phys_ptr: set to the physical address corresponding to the virtual address
8bf5b6a9 10195 * @attrs: set to the memory transaction attributes to use
702a9357
PM
10196 * @prot: set to the permissions for the page containing phys_ptr
10197 * @page_size: set to the size of the page containing phys_ptr
5b2d261d
AB
10198 * @fi: set to fault info if the translation fails
10199 * @cacheattrs: (if non-NULL) set to the cacheability/shareability attributes
702a9357 10200 */
af51f566 10201static bool get_phys_addr(CPUARMState *env, target_ulong address,
03ae85f8 10202 MMUAccessType access_type, ARMMMUIdx mmu_idx,
af51f566 10203 hwaddr *phys_ptr, MemTxAttrs *attrs, int *prot,
bc52bfeb 10204 target_ulong *page_size,
5b2d261d 10205 ARMMMUFaultInfo *fi, ARMCacheAttrs *cacheattrs)
9ee6e8bb 10206{
0480f69a 10207 if (mmu_idx == ARMMMUIdx_S12NSE0 || mmu_idx == ARMMMUIdx_S12NSE1) {
9b539263
EI
10208 /* Call ourselves recursively to do the stage 1 and then stage 2
10209 * translations.
0480f69a 10210 */
9b539263
EI
10211 if (arm_feature(env, ARM_FEATURE_EL2)) {
10212 hwaddr ipa;
10213 int s2_prot;
10214 int ret;
5b2d261d 10215 ARMCacheAttrs cacheattrs2 = {};
9b539263
EI
10216
10217 ret = get_phys_addr(env, address, access_type,
8bd5c820 10218 stage_1_mmu_idx(mmu_idx), &ipa, attrs,
bc52bfeb 10219 prot, page_size, fi, cacheattrs);
9b539263
EI
10220
10221 /* If S1 fails or S2 is disabled, return early. */
10222 if (ret || regime_translation_disabled(env, ARMMMUIdx_S2NS)) {
10223 *phys_ptr = ipa;
10224 return ret;
10225 }
10226
10227 /* S1 is done. Now do S2 translation. */
10228 ret = get_phys_addr_lpae(env, ipa, access_type, ARMMMUIdx_S2NS,
10229 phys_ptr, attrs, &s2_prot,
da909b2c 10230 page_size, fi,
5b2d261d 10231 cacheattrs != NULL ? &cacheattrs2 : NULL);
9b539263
EI
10232 fi->s2addr = ipa;
10233 /* Combine the S1 and S2 perms. */
10234 *prot &= s2_prot;
5b2d261d
AB
10235
10236 /* Combine the S1 and S2 cache attributes, if needed */
10237 if (!ret && cacheattrs != NULL) {
10238 *cacheattrs = combine_cacheattrs(*cacheattrs, cacheattrs2);
10239 }
10240
9b539263
EI
10241 return ret;
10242 } else {
10243 /*
10244 * For non-EL2 CPUs a stage1+stage2 translation is just stage 1.
10245 */
8bd5c820 10246 mmu_idx = stage_1_mmu_idx(mmu_idx);
9b539263 10247 }
0480f69a 10248 }
d3649702 10249
8bf5b6a9
PM
10250 /* The page table entries may downgrade secure to non-secure, but
10251 * cannot upgrade an non-secure translation regime's attributes
10252 * to secure.
10253 */
10254 attrs->secure = regime_is_secure(env, mmu_idx);
0995bf8c 10255 attrs->user = regime_is_user(env, mmu_idx);
8bf5b6a9 10256
0480f69a
PM
10257 /* Fast Context Switch Extension. This doesn't exist at all in v8.
10258 * In v7 and earlier it affects all stage 1 translations.
10259 */
10260 if (address < 0x02000000 && mmu_idx != ARMMMUIdx_S2NS
10261 && !arm_feature(env, ARM_FEATURE_V8)) {
10262 if (regime_el(env, mmu_idx) == 3) {
10263 address += env->cp15.fcseidr_s;
10264 } else {
10265 address += env->cp15.fcseidr_ns;
10266 }
54bf36ed 10267 }
9ee6e8bb 10268
3279adb9 10269 if (arm_feature(env, ARM_FEATURE_PMSA)) {
c9f9f124 10270 bool ret;
f6bda88f 10271 *page_size = TARGET_PAGE_SIZE;
3279adb9 10272
504e3cc3
PM
10273 if (arm_feature(env, ARM_FEATURE_V8)) {
10274 /* PMSAv8 */
10275 ret = get_phys_addr_pmsav8(env, address, access_type, mmu_idx,
3f551b5b 10276 phys_ptr, attrs, prot, fi);
504e3cc3 10277 } else if (arm_feature(env, ARM_FEATURE_V7)) {
3279adb9
PM
10278 /* PMSAv7 */
10279 ret = get_phys_addr_pmsav7(env, address, access_type, mmu_idx,
9375ad15 10280 phys_ptr, prot, fi);
3279adb9
PM
10281 } else {
10282 /* Pre-v7 MPU */
10283 ret = get_phys_addr_pmsav5(env, address, access_type, mmu_idx,
53a4e5c5 10284 phys_ptr, prot, fi);
3279adb9
PM
10285 }
10286 qemu_log_mask(CPU_LOG_MMU, "PMSA MPU lookup for %s at 0x%08" PRIx32
c9f9f124 10287 " mmu_idx %u -> %s (prot %c%c%c)\n",
709e4407
PM
10288 access_type == MMU_DATA_LOAD ? "reading" :
10289 (access_type == MMU_DATA_STORE ? "writing" : "execute"),
c9f9f124
MD
10290 (uint32_t)address, mmu_idx,
10291 ret ? "Miss" : "Hit",
10292 *prot & PAGE_READ ? 'r' : '-',
10293 *prot & PAGE_WRITE ? 'w' : '-',
10294 *prot & PAGE_EXEC ? 'x' : '-');
10295
10296 return ret;
f6bda88f
PC
10297 }
10298
3279adb9
PM
10299 /* Definitely a real MMU, not an MPU */
10300
0480f69a 10301 if (regime_translation_disabled(env, mmu_idx)) {
3279adb9 10302 /* MMU disabled. */
9ee6e8bb 10303 *phys_ptr = address;
3ad493fc 10304 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
d4c430a8 10305 *page_size = TARGET_PAGE_SIZE;
9ee6e8bb 10306 return 0;
0480f69a
PM
10307 }
10308
0480f69a 10309 if (regime_using_lpae_format(env, mmu_idx)) {
bc52bfeb
PM
10310 return get_phys_addr_lpae(env, address, access_type, mmu_idx,
10311 phys_ptr, attrs, prot, page_size,
10312 fi, cacheattrs);
0480f69a 10313 } else if (regime_sctlr(env, mmu_idx) & SCTLR_XP) {
bc52bfeb
PM
10314 return get_phys_addr_v6(env, address, access_type, mmu_idx,
10315 phys_ptr, attrs, prot, page_size, fi);
9ee6e8bb 10316 } else {
bc52bfeb 10317 return get_phys_addr_v5(env, address, access_type, mmu_idx,
f989983e 10318 phys_ptr, prot, page_size, fi);
9ee6e8bb
PB
10319 }
10320}
10321
8c6084bf 10322/* Walk the page table and (if the mapping exists) add the page
b7cc4e82
PC
10323 * to the TLB. Return false on success, or true on failure. Populate
10324 * fsr with ARM DFSR/IFSR fault register format value on failure.
8c6084bf 10325 */
b7cc4e82 10326bool arm_tlb_fill(CPUState *cs, vaddr address,
bc52bfeb 10327 MMUAccessType access_type, int mmu_idx,
e14b5a23 10328 ARMMMUFaultInfo *fi)
b5ff1b31 10329{
7510454e
AF
10330 ARMCPU *cpu = ARM_CPU(cs);
10331 CPUARMState *env = &cpu->env;
a8170e5e 10332 hwaddr phys_addr;
d4c430a8 10333 target_ulong page_size;
b5ff1b31 10334 int prot;
d3649702 10335 int ret;
8bf5b6a9 10336 MemTxAttrs attrs = {};
b5ff1b31 10337
8bd5c820
PM
10338 ret = get_phys_addr(env, address, access_type,
10339 core_to_arm_mmu_idx(env, mmu_idx), &phys_addr,
bc52bfeb 10340 &attrs, &prot, &page_size, fi, NULL);
b7cc4e82 10341 if (!ret) {
b5ff1b31 10342 /* Map a single [sub]page. */
dcd82c11
AB
10343 phys_addr &= TARGET_PAGE_MASK;
10344 address &= TARGET_PAGE_MASK;
8bf5b6a9
PM
10345 tlb_set_page_with_attrs(cs, address, phys_addr, attrs,
10346 prot, mmu_idx, page_size);
d4c430a8 10347 return 0;
b5ff1b31
FB
10348 }
10349
8c6084bf 10350 return ret;
b5ff1b31
FB
10351}
10352
0faea0c7
PM
10353hwaddr arm_cpu_get_phys_page_attrs_debug(CPUState *cs, vaddr addr,
10354 MemTxAttrs *attrs)
b5ff1b31 10355{
00b941e5 10356 ARMCPU *cpu = ARM_CPU(cs);
d3649702 10357 CPUARMState *env = &cpu->env;
a8170e5e 10358 hwaddr phys_addr;
d4c430a8 10359 target_ulong page_size;
b5ff1b31 10360 int prot;
b7cc4e82 10361 bool ret;
e14b5a23 10362 ARMMMUFaultInfo fi = {};
8bd5c820 10363 ARMMMUIdx mmu_idx = core_to_arm_mmu_idx(env, cpu_mmu_index(env, false));
b5ff1b31 10364
0faea0c7
PM
10365 *attrs = (MemTxAttrs) {};
10366
8bd5c820 10367 ret = get_phys_addr(env, addr, 0, mmu_idx, &phys_addr,
bc52bfeb 10368 attrs, &prot, &page_size, &fi, NULL);
b5ff1b31 10369
b7cc4e82 10370 if (ret) {
b5ff1b31 10371 return -1;
00b941e5 10372 }
b5ff1b31
FB
10373 return phys_addr;
10374}
10375
0ecb72a5 10376uint32_t HELPER(v7m_mrs)(CPUARMState *env, uint32_t reg)
9ee6e8bb 10377{
58117c9b
MD
10378 uint32_t mask;
10379 unsigned el = arm_current_el(env);
10380
10381 /* First handle registers which unprivileged can read */
10382
10383 switch (reg) {
10384 case 0 ... 7: /* xPSR sub-fields */
10385 mask = 0;
10386 if ((reg & 1) && el) {
987ab45e 10387 mask |= XPSR_EXCP; /* IPSR (unpriv. reads as zero) */
58117c9b
MD
10388 }
10389 if (!(reg & 4)) {
987ab45e 10390 mask |= XPSR_NZCV | XPSR_Q; /* APSR */
58117c9b
MD
10391 }
10392 /* EPSR reads as zero */
10393 return xpsr_read(env) & mask;
10394 break;
10395 case 20: /* CONTROL */
8bfc26ea 10396 return env->v7m.control[env->v7m.secure];
50f11062
PM
10397 case 0x94: /* CONTROL_NS */
10398 /* We have to handle this here because unprivileged Secure code
10399 * can read the NS CONTROL register.
10400 */
10401 if (!env->v7m.secure) {
10402 return 0;
10403 }
10404 return env->v7m.control[M_REG_NS];
58117c9b
MD
10405 }
10406
10407 if (el == 0) {
10408 return 0; /* unprivileged reads others as zero */
10409 }
a47dddd7 10410
50f11062
PM
10411 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
10412 switch (reg) {
10413 case 0x88: /* MSP_NS */
10414 if (!env->v7m.secure) {
10415 return 0;
10416 }
10417 return env->v7m.other_ss_msp;
10418 case 0x89: /* PSP_NS */
10419 if (!env->v7m.secure) {
10420 return 0;
10421 }
10422 return env->v7m.other_ss_psp;
57bb3156
PM
10423 case 0x8a: /* MSPLIM_NS */
10424 if (!env->v7m.secure) {
10425 return 0;
10426 }
10427 return env->v7m.msplim[M_REG_NS];
10428 case 0x8b: /* PSPLIM_NS */
10429 if (!env->v7m.secure) {
10430 return 0;
10431 }
10432 return env->v7m.psplim[M_REG_NS];
50f11062
PM
10433 case 0x90: /* PRIMASK_NS */
10434 if (!env->v7m.secure) {
10435 return 0;
10436 }
10437 return env->v7m.primask[M_REG_NS];
10438 case 0x91: /* BASEPRI_NS */
10439 if (!env->v7m.secure) {
10440 return 0;
10441 }
10442 return env->v7m.basepri[M_REG_NS];
10443 case 0x93: /* FAULTMASK_NS */
10444 if (!env->v7m.secure) {
10445 return 0;
10446 }
10447 return env->v7m.faultmask[M_REG_NS];
10448 case 0x98: /* SP_NS */
10449 {
10450 /* This gives the non-secure SP selected based on whether we're
10451 * currently in handler mode or not, using the NS CONTROL.SPSEL.
10452 */
10453 bool spsel = env->v7m.control[M_REG_NS] & R_V7M_CONTROL_SPSEL_MASK;
10454
10455 if (!env->v7m.secure) {
10456 return 0;
10457 }
10458 if (!arm_v7m_is_handler_mode(env) && spsel) {
10459 return env->v7m.other_ss_psp;
10460 } else {
10461 return env->v7m.other_ss_msp;
10462 }
10463 }
10464 default:
10465 break;
10466 }
10467 }
10468
9ee6e8bb 10469 switch (reg) {
9ee6e8bb 10470 case 8: /* MSP */
1169d3aa 10471 return v7m_using_psp(env) ? env->v7m.other_sp : env->regs[13];
9ee6e8bb 10472 case 9: /* PSP */
1169d3aa 10473 return v7m_using_psp(env) ? env->regs[13] : env->v7m.other_sp;
57bb3156
PM
10474 case 10: /* MSPLIM */
10475 if (!arm_feature(env, ARM_FEATURE_V8)) {
10476 goto bad_reg;
10477 }
10478 return env->v7m.msplim[env->v7m.secure];
10479 case 11: /* PSPLIM */
10480 if (!arm_feature(env, ARM_FEATURE_V8)) {
10481 goto bad_reg;
10482 }
10483 return env->v7m.psplim[env->v7m.secure];
9ee6e8bb 10484 case 16: /* PRIMASK */
6d804834 10485 return env->v7m.primask[env->v7m.secure];
82845826
SH
10486 case 17: /* BASEPRI */
10487 case 18: /* BASEPRI_MAX */
acf94941 10488 return env->v7m.basepri[env->v7m.secure];
82845826 10489 case 19: /* FAULTMASK */
42a6686b 10490 return env->v7m.faultmask[env->v7m.secure];
9ee6e8bb 10491 default:
57bb3156 10492 bad_reg:
58117c9b
MD
10493 qemu_log_mask(LOG_GUEST_ERROR, "Attempt to read unknown special"
10494 " register %d\n", reg);
9ee6e8bb
PB
10495 return 0;
10496 }
10497}
10498
b28b3377
PM
10499void HELPER(v7m_msr)(CPUARMState *env, uint32_t maskreg, uint32_t val)
10500{
10501 /* We're passed bits [11..0] of the instruction; extract
10502 * SYSm and the mask bits.
10503 * Invalid combinations of SYSm and mask are UNPREDICTABLE;
10504 * we choose to treat them as if the mask bits were valid.
10505 * NB that the pseudocode 'mask' variable is bits [11..10],
10506 * whereas ours is [11..8].
10507 */
10508 uint32_t mask = extract32(maskreg, 8, 4);
10509 uint32_t reg = extract32(maskreg, 0, 8);
10510
58117c9b
MD
10511 if (arm_current_el(env) == 0 && reg > 7) {
10512 /* only xPSR sub-fields may be written by unprivileged */
10513 return;
10514 }
a47dddd7 10515
50f11062
PM
10516 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
10517 switch (reg) {
10518 case 0x88: /* MSP_NS */
10519 if (!env->v7m.secure) {
10520 return;
10521 }
10522 env->v7m.other_ss_msp = val;
10523 return;
10524 case 0x89: /* PSP_NS */
10525 if (!env->v7m.secure) {
10526 return;
10527 }
10528 env->v7m.other_ss_psp = val;
10529 return;
57bb3156
PM
10530 case 0x8a: /* MSPLIM_NS */
10531 if (!env->v7m.secure) {
10532 return;
10533 }
10534 env->v7m.msplim[M_REG_NS] = val & ~7;
10535 return;
10536 case 0x8b: /* PSPLIM_NS */
10537 if (!env->v7m.secure) {
10538 return;
10539 }
10540 env->v7m.psplim[M_REG_NS] = val & ~7;
10541 return;
50f11062
PM
10542 case 0x90: /* PRIMASK_NS */
10543 if (!env->v7m.secure) {
10544 return;
10545 }
10546 env->v7m.primask[M_REG_NS] = val & 1;
10547 return;
10548 case 0x91: /* BASEPRI_NS */
10549 if (!env->v7m.secure) {
10550 return;
10551 }
10552 env->v7m.basepri[M_REG_NS] = val & 0xff;
10553 return;
10554 case 0x93: /* FAULTMASK_NS */
10555 if (!env->v7m.secure) {
10556 return;
10557 }
10558 env->v7m.faultmask[M_REG_NS] = val & 1;
10559 return;
6eb3a64e
PM
10560 case 0x94: /* CONTROL_NS */
10561 if (!env->v7m.secure) {
10562 return;
10563 }
10564 write_v7m_control_spsel_for_secstate(env,
10565 val & R_V7M_CONTROL_SPSEL_MASK,
10566 M_REG_NS);
10567 env->v7m.control[M_REG_NS] &= ~R_V7M_CONTROL_NPRIV_MASK;
10568 env->v7m.control[M_REG_NS] |= val & R_V7M_CONTROL_NPRIV_MASK;
10569 return;
50f11062
PM
10570 case 0x98: /* SP_NS */
10571 {
10572 /* This gives the non-secure SP selected based on whether we're
10573 * currently in handler mode or not, using the NS CONTROL.SPSEL.
10574 */
10575 bool spsel = env->v7m.control[M_REG_NS] & R_V7M_CONTROL_SPSEL_MASK;
10576
10577 if (!env->v7m.secure) {
10578 return;
10579 }
10580 if (!arm_v7m_is_handler_mode(env) && spsel) {
10581 env->v7m.other_ss_psp = val;
10582 } else {
10583 env->v7m.other_ss_msp = val;
10584 }
10585 return;
10586 }
10587 default:
10588 break;
10589 }
10590 }
10591
9ee6e8bb 10592 switch (reg) {
58117c9b
MD
10593 case 0 ... 7: /* xPSR sub-fields */
10594 /* only APSR is actually writable */
b28b3377
PM
10595 if (!(reg & 4)) {
10596 uint32_t apsrmask = 0;
10597
10598 if (mask & 8) {
987ab45e 10599 apsrmask |= XPSR_NZCV | XPSR_Q;
b28b3377
PM
10600 }
10601 if ((mask & 4) && arm_feature(env, ARM_FEATURE_THUMB_DSP)) {
987ab45e 10602 apsrmask |= XPSR_GE;
b28b3377
PM
10603 }
10604 xpsr_write(env, val, apsrmask);
58117c9b 10605 }
9ee6e8bb
PB
10606 break;
10607 case 8: /* MSP */
1169d3aa 10608 if (v7m_using_psp(env)) {
9ee6e8bb 10609 env->v7m.other_sp = val;
abc24d86 10610 } else {
9ee6e8bb 10611 env->regs[13] = val;
abc24d86 10612 }
9ee6e8bb
PB
10613 break;
10614 case 9: /* PSP */
1169d3aa 10615 if (v7m_using_psp(env)) {
9ee6e8bb 10616 env->regs[13] = val;
abc24d86 10617 } else {
9ee6e8bb 10618 env->v7m.other_sp = val;
abc24d86 10619 }
9ee6e8bb 10620 break;
57bb3156
PM
10621 case 10: /* MSPLIM */
10622 if (!arm_feature(env, ARM_FEATURE_V8)) {
10623 goto bad_reg;
10624 }
10625 env->v7m.msplim[env->v7m.secure] = val & ~7;
10626 break;
10627 case 11: /* PSPLIM */
10628 if (!arm_feature(env, ARM_FEATURE_V8)) {
10629 goto bad_reg;
10630 }
10631 env->v7m.psplim[env->v7m.secure] = val & ~7;
10632 break;
9ee6e8bb 10633 case 16: /* PRIMASK */
6d804834 10634 env->v7m.primask[env->v7m.secure] = val & 1;
9ee6e8bb 10635 break;
82845826 10636 case 17: /* BASEPRI */
acf94941 10637 env->v7m.basepri[env->v7m.secure] = val & 0xff;
9ee6e8bb 10638 break;
82845826 10639 case 18: /* BASEPRI_MAX */
9ee6e8bb 10640 val &= 0xff;
acf94941
PM
10641 if (val != 0 && (val < env->v7m.basepri[env->v7m.secure]
10642 || env->v7m.basepri[env->v7m.secure] == 0)) {
10643 env->v7m.basepri[env->v7m.secure] = val;
10644 }
9ee6e8bb 10645 break;
82845826 10646 case 19: /* FAULTMASK */
42a6686b 10647 env->v7m.faultmask[env->v7m.secure] = val & 1;
82845826 10648 break;
9ee6e8bb 10649 case 20: /* CONTROL */
792dac30
PM
10650 /* Writing to the SPSEL bit only has an effect if we are in
10651 * thread mode; other bits can be updated by any privileged code.
de2db7ec 10652 * write_v7m_control_spsel() deals with updating the SPSEL bit in
792dac30 10653 * env->v7m.control, so we only need update the others.
83d7f86d
PM
10654 * For v7M, we must just ignore explicit writes to SPSEL in handler
10655 * mode; for v8M the write is permitted but will have no effect.
792dac30 10656 */
83d7f86d
PM
10657 if (arm_feature(env, ARM_FEATURE_V8) ||
10658 !arm_v7m_is_handler_mode(env)) {
de2db7ec 10659 write_v7m_control_spsel(env, (val & R_V7M_CONTROL_SPSEL_MASK) != 0);
792dac30 10660 }
8bfc26ea
PM
10661 env->v7m.control[env->v7m.secure] &= ~R_V7M_CONTROL_NPRIV_MASK;
10662 env->v7m.control[env->v7m.secure] |= val & R_V7M_CONTROL_NPRIV_MASK;
9ee6e8bb
PB
10663 break;
10664 default:
57bb3156 10665 bad_reg:
58117c9b
MD
10666 qemu_log_mask(LOG_GUEST_ERROR, "Attempt to write unknown special"
10667 " register %d\n", reg);
9ee6e8bb
PB
10668 return;
10669 }
10670}
10671
5158de24
PM
10672uint32_t HELPER(v7m_tt)(CPUARMState *env, uint32_t addr, uint32_t op)
10673{
10674 /* Implement the TT instruction. op is bits [7:6] of the insn. */
10675 bool forceunpriv = op & 1;
10676 bool alt = op & 2;
10677 V8M_SAttributes sattrs = {};
10678 uint32_t tt_resp;
10679 bool r, rw, nsr, nsrw, mrvalid;
10680 int prot;
3f551b5b 10681 ARMMMUFaultInfo fi = {};
5158de24
PM
10682 MemTxAttrs attrs = {};
10683 hwaddr phys_addr;
5158de24
PM
10684 ARMMMUIdx mmu_idx;
10685 uint32_t mregion;
10686 bool targetpriv;
10687 bool targetsec = env->v7m.secure;
10688
10689 /* Work out what the security state and privilege level we're
10690 * interested in is...
10691 */
10692 if (alt) {
10693 targetsec = !targetsec;
10694 }
10695
10696 if (forceunpriv) {
10697 targetpriv = false;
10698 } else {
10699 targetpriv = arm_v7m_is_handler_mode(env) ||
10700 !(env->v7m.control[targetsec] & R_V7M_CONTROL_NPRIV_MASK);
10701 }
10702
10703 /* ...and then figure out which MMU index this is */
10704 mmu_idx = arm_v7m_mmu_idx_for_secstate_and_priv(env, targetsec, targetpriv);
10705
10706 /* We know that the MPU and SAU don't care about the access type
10707 * for our purposes beyond that we don't want to claim to be
10708 * an insn fetch, so we arbitrarily call this a read.
10709 */
10710
10711 /* MPU region info only available for privileged or if
10712 * inspecting the other MPU state.
10713 */
10714 if (arm_current_el(env) != 0 || alt) {
10715 /* We can ignore the return value as prot is always set */
10716 pmsav8_mpu_lookup(env, addr, MMU_DATA_LOAD, mmu_idx,
3f551b5b 10717 &phys_addr, &attrs, &prot, &fi, &mregion);
5158de24
PM
10718 if (mregion == -1) {
10719 mrvalid = false;
10720 mregion = 0;
10721 } else {
10722 mrvalid = true;
10723 }
10724 r = prot & PAGE_READ;
10725 rw = prot & PAGE_WRITE;
10726 } else {
10727 r = false;
10728 rw = false;
10729 mrvalid = false;
10730 mregion = 0;
10731 }
10732
10733 if (env->v7m.secure) {
10734 v8m_security_lookup(env, addr, MMU_DATA_LOAD, mmu_idx, &sattrs);
10735 nsr = sattrs.ns && r;
10736 nsrw = sattrs.ns && rw;
10737 } else {
10738 sattrs.ns = true;
10739 nsr = false;
10740 nsrw = false;
10741 }
10742
10743 tt_resp = (sattrs.iregion << 24) |
10744 (sattrs.irvalid << 23) |
10745 ((!sattrs.ns) << 22) |
10746 (nsrw << 21) |
10747 (nsr << 20) |
10748 (rw << 19) |
10749 (r << 18) |
10750 (sattrs.srvalid << 17) |
10751 (mrvalid << 16) |
10752 (sattrs.sregion << 8) |
10753 mregion;
10754
10755 return tt_resp;
10756}
10757
b5ff1b31 10758#endif
6ddbc6e4 10759
aca3f40b
PM
10760void HELPER(dc_zva)(CPUARMState *env, uint64_t vaddr_in)
10761{
10762 /* Implement DC ZVA, which zeroes a fixed-length block of memory.
10763 * Note that we do not implement the (architecturally mandated)
10764 * alignment fault for attempts to use this on Device memory
10765 * (which matches the usual QEMU behaviour of not implementing either
10766 * alignment faults or any memory attribute handling).
10767 */
10768
10769 ARMCPU *cpu = arm_env_get_cpu(env);
10770 uint64_t blocklen = 4 << cpu->dcz_blocksize;
10771 uint64_t vaddr = vaddr_in & ~(blocklen - 1);
10772
10773#ifndef CONFIG_USER_ONLY
10774 {
10775 /* Slightly awkwardly, QEMU's TARGET_PAGE_SIZE may be less than
10776 * the block size so we might have to do more than one TLB lookup.
10777 * We know that in fact for any v8 CPU the page size is at least 4K
10778 * and the block size must be 2K or less, but TARGET_PAGE_SIZE is only
10779 * 1K as an artefact of legacy v5 subpage support being present in the
10780 * same QEMU executable.
10781 */
10782 int maxidx = DIV_ROUND_UP(blocklen, TARGET_PAGE_SIZE);
10783 void *hostaddr[maxidx];
10784 int try, i;
97ed5ccd 10785 unsigned mmu_idx = cpu_mmu_index(env, false);
3972ef6f 10786 TCGMemOpIdx oi = make_memop_idx(MO_UB, mmu_idx);
aca3f40b
PM
10787
10788 for (try = 0; try < 2; try++) {
10789
10790 for (i = 0; i < maxidx; i++) {
10791 hostaddr[i] = tlb_vaddr_to_host(env,
10792 vaddr + TARGET_PAGE_SIZE * i,
3972ef6f 10793 1, mmu_idx);
aca3f40b
PM
10794 if (!hostaddr[i]) {
10795 break;
10796 }
10797 }
10798 if (i == maxidx) {
10799 /* If it's all in the TLB it's fair game for just writing to;
10800 * we know we don't need to update dirty status, etc.
10801 */
10802 for (i = 0; i < maxidx - 1; i++) {
10803 memset(hostaddr[i], 0, TARGET_PAGE_SIZE);
10804 }
10805 memset(hostaddr[i], 0, blocklen - (i * TARGET_PAGE_SIZE));
10806 return;
10807 }
10808 /* OK, try a store and see if we can populate the tlb. This
10809 * might cause an exception if the memory isn't writable,
10810 * in which case we will longjmp out of here. We must for
10811 * this purpose use the actual register value passed to us
10812 * so that we get the fault address right.
10813 */
01ecaf43 10814 helper_ret_stb_mmu(env, vaddr_in, 0, oi, GETPC());
aca3f40b
PM
10815 /* Now we can populate the other TLB entries, if any */
10816 for (i = 0; i < maxidx; i++) {
10817 uint64_t va = vaddr + TARGET_PAGE_SIZE * i;
10818 if (va != (vaddr_in & TARGET_PAGE_MASK)) {
01ecaf43 10819 helper_ret_stb_mmu(env, va, 0, oi, GETPC());
aca3f40b
PM
10820 }
10821 }
10822 }
10823
10824 /* Slow path (probably attempt to do this to an I/O device or
10825 * similar, or clearing of a block of code we have translations
10826 * cached for). Just do a series of byte writes as the architecture
10827 * demands. It's not worth trying to use a cpu_physical_memory_map(),
10828 * memset(), unmap() sequence here because:
10829 * + we'd need to account for the blocksize being larger than a page
10830 * + the direct-RAM access case is almost always going to be dealt
10831 * with in the fastpath code above, so there's no speed benefit
10832 * + we would have to deal with the map returning NULL because the
10833 * bounce buffer was in use
10834 */
10835 for (i = 0; i < blocklen; i++) {
01ecaf43 10836 helper_ret_stb_mmu(env, vaddr + i, 0, oi, GETPC());
aca3f40b
PM
10837 }
10838 }
10839#else
10840 memset(g2h(vaddr), 0, blocklen);
10841#endif
10842}
10843
6ddbc6e4
PB
10844/* Note that signed overflow is undefined in C. The following routines are
10845 careful to use unsigned types where modulo arithmetic is required.
10846 Failure to do so _will_ break on newer gcc. */
10847
10848/* Signed saturating arithmetic. */
10849
1654b2d6 10850/* Perform 16-bit signed saturating addition. */
6ddbc6e4
PB
10851static inline uint16_t add16_sat(uint16_t a, uint16_t b)
10852{
10853 uint16_t res;
10854
10855 res = a + b;
10856 if (((res ^ a) & 0x8000) && !((a ^ b) & 0x8000)) {
10857 if (a & 0x8000)
10858 res = 0x8000;
10859 else
10860 res = 0x7fff;
10861 }
10862 return res;
10863}
10864
1654b2d6 10865/* Perform 8-bit signed saturating addition. */
6ddbc6e4
PB
10866static inline uint8_t add8_sat(uint8_t a, uint8_t b)
10867{
10868 uint8_t res;
10869
10870 res = a + b;
10871 if (((res ^ a) & 0x80) && !((a ^ b) & 0x80)) {
10872 if (a & 0x80)
10873 res = 0x80;
10874 else
10875 res = 0x7f;
10876 }
10877 return res;
10878}
10879
1654b2d6 10880/* Perform 16-bit signed saturating subtraction. */
6ddbc6e4
PB
10881static inline uint16_t sub16_sat(uint16_t a, uint16_t b)
10882{
10883 uint16_t res;
10884
10885 res = a - b;
10886 if (((res ^ a) & 0x8000) && ((a ^ b) & 0x8000)) {
10887 if (a & 0x8000)
10888 res = 0x8000;
10889 else
10890 res = 0x7fff;
10891 }
10892 return res;
10893}
10894
1654b2d6 10895/* Perform 8-bit signed saturating subtraction. */
6ddbc6e4
PB
10896static inline uint8_t sub8_sat(uint8_t a, uint8_t b)
10897{
10898 uint8_t res;
10899
10900 res = a - b;
10901 if (((res ^ a) & 0x80) && ((a ^ b) & 0x80)) {
10902 if (a & 0x80)
10903 res = 0x80;
10904 else
10905 res = 0x7f;
10906 }
10907 return res;
10908}
10909
10910#define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16);
10911#define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16);
10912#define ADD8(a, b, n) RESULT(add8_sat(a, b), n, 8);
10913#define SUB8(a, b, n) RESULT(sub8_sat(a, b), n, 8);
10914#define PFX q
10915
10916#include "op_addsub.h"
10917
10918/* Unsigned saturating arithmetic. */
460a09c1 10919static inline uint16_t add16_usat(uint16_t a, uint16_t b)
6ddbc6e4
PB
10920{
10921 uint16_t res;
10922 res = a + b;
10923 if (res < a)
10924 res = 0xffff;
10925 return res;
10926}
10927
460a09c1 10928static inline uint16_t sub16_usat(uint16_t a, uint16_t b)
6ddbc6e4 10929{
4c4fd3f8 10930 if (a > b)
6ddbc6e4
PB
10931 return a - b;
10932 else
10933 return 0;
10934}
10935
10936static inline uint8_t add8_usat(uint8_t a, uint8_t b)
10937{
10938 uint8_t res;
10939 res = a + b;
10940 if (res < a)
10941 res = 0xff;
10942 return res;
10943}
10944
10945static inline uint8_t sub8_usat(uint8_t a, uint8_t b)
10946{
4c4fd3f8 10947 if (a > b)
6ddbc6e4
PB
10948 return a - b;
10949 else
10950 return 0;
10951}
10952
10953#define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16);
10954#define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16);
10955#define ADD8(a, b, n) RESULT(add8_usat(a, b), n, 8);
10956#define SUB8(a, b, n) RESULT(sub8_usat(a, b), n, 8);
10957#define PFX uq
10958
10959#include "op_addsub.h"
10960
10961/* Signed modulo arithmetic. */
10962#define SARITH16(a, b, n, op) do { \
10963 int32_t sum; \
db6e2e65 10964 sum = (int32_t)(int16_t)(a) op (int32_t)(int16_t)(b); \
6ddbc6e4
PB
10965 RESULT(sum, n, 16); \
10966 if (sum >= 0) \
10967 ge |= 3 << (n * 2); \
10968 } while(0)
10969
10970#define SARITH8(a, b, n, op) do { \
10971 int32_t sum; \
db6e2e65 10972 sum = (int32_t)(int8_t)(a) op (int32_t)(int8_t)(b); \
6ddbc6e4
PB
10973 RESULT(sum, n, 8); \
10974 if (sum >= 0) \
10975 ge |= 1 << n; \
10976 } while(0)
10977
10978
10979#define ADD16(a, b, n) SARITH16(a, b, n, +)
10980#define SUB16(a, b, n) SARITH16(a, b, n, -)
10981#define ADD8(a, b, n) SARITH8(a, b, n, +)
10982#define SUB8(a, b, n) SARITH8(a, b, n, -)
10983#define PFX s
10984#define ARITH_GE
10985
10986#include "op_addsub.h"
10987
10988/* Unsigned modulo arithmetic. */
10989#define ADD16(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); \
a87aa10b 10993 if ((sum >> 16) == 1) \
6ddbc6e4
PB
10994 ge |= 3 << (n * 2); \
10995 } while(0)
10996
10997#define ADD8(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); \
a87aa10b
AZ
11001 if ((sum >> 8) == 1) \
11002 ge |= 1 << n; \
6ddbc6e4
PB
11003 } while(0)
11004
11005#define SUB16(a, b, n) do { \
11006 uint32_t sum; \
11007 sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \
11008 RESULT(sum, n, 16); \
11009 if ((sum >> 16) == 0) \
11010 ge |= 3 << (n * 2); \
11011 } while(0)
11012
11013#define SUB8(a, b, n) do { \
11014 uint32_t sum; \
11015 sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \
11016 RESULT(sum, n, 8); \
11017 if ((sum >> 8) == 0) \
a87aa10b 11018 ge |= 1 << n; \
6ddbc6e4
PB
11019 } while(0)
11020
11021#define PFX u
11022#define ARITH_GE
11023
11024#include "op_addsub.h"
11025
11026/* Halved signed arithmetic. */
11027#define ADD16(a, b, n) \
11028 RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16)
11029#define SUB16(a, b, n) \
11030 RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16)
11031#define ADD8(a, b, n) \
11032 RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8)
11033#define SUB8(a, b, n) \
11034 RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8)
11035#define PFX sh
11036
11037#include "op_addsub.h"
11038
11039/* Halved unsigned arithmetic. */
11040#define ADD16(a, b, n) \
11041 RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16)
11042#define SUB16(a, b, n) \
11043 RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16)
11044#define ADD8(a, b, n) \
11045 RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8)
11046#define SUB8(a, b, n) \
11047 RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8)
11048#define PFX uh
11049
11050#include "op_addsub.h"
11051
11052static inline uint8_t do_usad(uint8_t a, uint8_t b)
11053{
11054 if (a > b)
11055 return a - b;
11056 else
11057 return b - a;
11058}
11059
11060/* Unsigned sum of absolute byte differences. */
11061uint32_t HELPER(usad8)(uint32_t a, uint32_t b)
11062{
11063 uint32_t sum;
11064 sum = do_usad(a, b);
11065 sum += do_usad(a >> 8, b >> 8);
11066 sum += do_usad(a >> 16, b >>16);
11067 sum += do_usad(a >> 24, b >> 24);
11068 return sum;
11069}
11070
11071/* For ARMv6 SEL instruction. */
11072uint32_t HELPER(sel_flags)(uint32_t flags, uint32_t a, uint32_t b)
11073{
11074 uint32_t mask;
11075
11076 mask = 0;
11077 if (flags & 1)
11078 mask |= 0xff;
11079 if (flags & 2)
11080 mask |= 0xff00;
11081 if (flags & 4)
11082 mask |= 0xff0000;
11083 if (flags & 8)
11084 mask |= 0xff000000;
11085 return (a & mask) | (b & ~mask);
11086}
11087
b90372ad
PM
11088/* VFP support. We follow the convention used for VFP instructions:
11089 Single precision routines have a "s" suffix, double precision a
4373f3ce
PB
11090 "d" suffix. */
11091
11092/* Convert host exception flags to vfp form. */
11093static inline int vfp_exceptbits_from_host(int host_bits)
11094{
11095 int target_bits = 0;
11096
11097 if (host_bits & float_flag_invalid)
11098 target_bits |= 1;
11099 if (host_bits & float_flag_divbyzero)
11100 target_bits |= 2;
11101 if (host_bits & float_flag_overflow)
11102 target_bits |= 4;
36802b6b 11103 if (host_bits & (float_flag_underflow | float_flag_output_denormal))
4373f3ce
PB
11104 target_bits |= 8;
11105 if (host_bits & float_flag_inexact)
11106 target_bits |= 0x10;
cecd8504
PM
11107 if (host_bits & float_flag_input_denormal)
11108 target_bits |= 0x80;
4373f3ce
PB
11109 return target_bits;
11110}
11111
0ecb72a5 11112uint32_t HELPER(vfp_get_fpscr)(CPUARMState *env)
4373f3ce
PB
11113{
11114 int i;
11115 uint32_t fpscr;
11116
11117 fpscr = (env->vfp.xregs[ARM_VFP_FPSCR] & 0xffc8ffff)
11118 | (env->vfp.vec_len << 16)
11119 | (env->vfp.vec_stride << 20);
11120 i = get_float_exception_flags(&env->vfp.fp_status);
3a492f3a 11121 i |= get_float_exception_flags(&env->vfp.standard_fp_status);
d81ce0ef 11122 i |= get_float_exception_flags(&env->vfp.fp_status_f16);
4373f3ce
PB
11123 fpscr |= vfp_exceptbits_from_host(i);
11124 return fpscr;
11125}
11126
0ecb72a5 11127uint32_t vfp_get_fpscr(CPUARMState *env)
01653295
PM
11128{
11129 return HELPER(vfp_get_fpscr)(env);
11130}
11131
4373f3ce
PB
11132/* Convert vfp exception flags to target form. */
11133static inline int vfp_exceptbits_to_host(int target_bits)
11134{
11135 int host_bits = 0;
11136
11137 if (target_bits & 1)
11138 host_bits |= float_flag_invalid;
11139 if (target_bits & 2)
11140 host_bits |= float_flag_divbyzero;
11141 if (target_bits & 4)
11142 host_bits |= float_flag_overflow;
11143 if (target_bits & 8)
11144 host_bits |= float_flag_underflow;
11145 if (target_bits & 0x10)
11146 host_bits |= float_flag_inexact;
cecd8504
PM
11147 if (target_bits & 0x80)
11148 host_bits |= float_flag_input_denormal;
4373f3ce
PB
11149 return host_bits;
11150}
11151
0ecb72a5 11152void HELPER(vfp_set_fpscr)(CPUARMState *env, uint32_t val)
4373f3ce
PB
11153{
11154 int i;
11155 uint32_t changed;
11156
11157 changed = env->vfp.xregs[ARM_VFP_FPSCR];
11158 env->vfp.xregs[ARM_VFP_FPSCR] = (val & 0xffc8ffff);
11159 env->vfp.vec_len = (val >> 16) & 7;
11160 env->vfp.vec_stride = (val >> 20) & 3;
11161
11162 changed ^= val;
11163 if (changed & (3 << 22)) {
11164 i = (val >> 22) & 3;
11165 switch (i) {
4d3da0f3 11166 case FPROUNDING_TIEEVEN:
4373f3ce
PB
11167 i = float_round_nearest_even;
11168 break;
4d3da0f3 11169 case FPROUNDING_POSINF:
4373f3ce
PB
11170 i = float_round_up;
11171 break;
4d3da0f3 11172 case FPROUNDING_NEGINF:
4373f3ce
PB
11173 i = float_round_down;
11174 break;
4d3da0f3 11175 case FPROUNDING_ZERO:
4373f3ce
PB
11176 i = float_round_to_zero;
11177 break;
11178 }
11179 set_float_rounding_mode(i, &env->vfp.fp_status);
d81ce0ef 11180 set_float_rounding_mode(i, &env->vfp.fp_status_f16);
4373f3ce 11181 }
d81ce0ef
AB
11182 if (changed & FPCR_FZ16) {
11183 bool ftz_enabled = val & FPCR_FZ16;
11184 set_flush_to_zero(ftz_enabled, &env->vfp.fp_status_f16);
11185 set_flush_inputs_to_zero(ftz_enabled, &env->vfp.fp_status_f16);
11186 }
11187 if (changed & FPCR_FZ) {
11188 bool ftz_enabled = val & FPCR_FZ;
11189 set_flush_to_zero(ftz_enabled, &env->vfp.fp_status);
11190 set_flush_inputs_to_zero(ftz_enabled, &env->vfp.fp_status);
11191 }
11192 if (changed & FPCR_DN) {
11193 bool dnan_enabled = val & FPCR_DN;
11194 set_default_nan_mode(dnan_enabled, &env->vfp.fp_status);
11195 set_default_nan_mode(dnan_enabled, &env->vfp.fp_status_f16);
cecd8504 11196 }
4373f3ce 11197
d81ce0ef
AB
11198 /* The exception flags are ORed together when we read fpscr so we
11199 * only need to preserve the current state in one of our
11200 * float_status values.
11201 */
b12c390b 11202 i = vfp_exceptbits_to_host(val);
4373f3ce 11203 set_float_exception_flags(i, &env->vfp.fp_status);
d81ce0ef 11204 set_float_exception_flags(0, &env->vfp.fp_status_f16);
3a492f3a 11205 set_float_exception_flags(0, &env->vfp.standard_fp_status);
4373f3ce
PB
11206}
11207
0ecb72a5 11208void vfp_set_fpscr(CPUARMState *env, uint32_t val)
01653295
PM
11209{
11210 HELPER(vfp_set_fpscr)(env, val);
11211}
11212
4373f3ce
PB
11213#define VFP_HELPER(name, p) HELPER(glue(glue(vfp_,name),p))
11214
11215#define VFP_BINOP(name) \
ae1857ec 11216float32 VFP_HELPER(name, s)(float32 a, float32 b, void *fpstp) \
4373f3ce 11217{ \
ae1857ec
PM
11218 float_status *fpst = fpstp; \
11219 return float32_ ## name(a, b, fpst); \
4373f3ce 11220} \
ae1857ec 11221float64 VFP_HELPER(name, d)(float64 a, float64 b, void *fpstp) \
4373f3ce 11222{ \
ae1857ec
PM
11223 float_status *fpst = fpstp; \
11224 return float64_ ## name(a, b, fpst); \
4373f3ce
PB
11225}
11226VFP_BINOP(add)
11227VFP_BINOP(sub)
11228VFP_BINOP(mul)
11229VFP_BINOP(div)
f71a2ae5
PM
11230VFP_BINOP(min)
11231VFP_BINOP(max)
11232VFP_BINOP(minnum)
11233VFP_BINOP(maxnum)
4373f3ce
PB
11234#undef VFP_BINOP
11235
11236float32 VFP_HELPER(neg, s)(float32 a)
11237{
11238 return float32_chs(a);
11239}
11240
11241float64 VFP_HELPER(neg, d)(float64 a)
11242{
66230e0d 11243 return float64_chs(a);
4373f3ce
PB
11244}
11245
11246float32 VFP_HELPER(abs, s)(float32 a)
11247{
11248 return float32_abs(a);
11249}
11250
11251float64 VFP_HELPER(abs, d)(float64 a)
11252{
66230e0d 11253 return float64_abs(a);
4373f3ce
PB
11254}
11255
0ecb72a5 11256float32 VFP_HELPER(sqrt, s)(float32 a, CPUARMState *env)
4373f3ce
PB
11257{
11258 return float32_sqrt(a, &env->vfp.fp_status);
11259}
11260
0ecb72a5 11261float64 VFP_HELPER(sqrt, d)(float64 a, CPUARMState *env)
4373f3ce
PB
11262{
11263 return float64_sqrt(a, &env->vfp.fp_status);
11264}
11265
11266/* XXX: check quiet/signaling case */
11267#define DO_VFP_cmp(p, type) \
0ecb72a5 11268void VFP_HELPER(cmp, p)(type a, type b, CPUARMState *env) \
4373f3ce
PB
11269{ \
11270 uint32_t flags; \
11271 switch(type ## _compare_quiet(a, b, &env->vfp.fp_status)) { \
11272 case 0: flags = 0x6; break; \
11273 case -1: flags = 0x8; break; \
11274 case 1: flags = 0x2; break; \
11275 default: case 2: flags = 0x3; break; \
11276 } \
11277 env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \
11278 | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \
11279} \
0ecb72a5 11280void VFP_HELPER(cmpe, p)(type a, type b, CPUARMState *env) \
4373f3ce
PB
11281{ \
11282 uint32_t flags; \
11283 switch(type ## _compare(a, b, &env->vfp.fp_status)) { \
11284 case 0: flags = 0x6; break; \
11285 case -1: flags = 0x8; break; \
11286 case 1: flags = 0x2; break; \
11287 default: case 2: flags = 0x3; break; \
11288 } \
11289 env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \
11290 | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \
11291}
11292DO_VFP_cmp(s, float32)
11293DO_VFP_cmp(d, float64)
11294#undef DO_VFP_cmp
11295
5500b06c 11296/* Integer to float and float to integer conversions */
4373f3ce 11297
5500b06c
PM
11298#define CONV_ITOF(name, fsz, sign) \
11299 float##fsz HELPER(name)(uint32_t x, void *fpstp) \
11300{ \
11301 float_status *fpst = fpstp; \
85836979 11302 return sign##int32_to_##float##fsz((sign##int32_t)x, fpst); \
4373f3ce
PB
11303}
11304
5500b06c
PM
11305#define CONV_FTOI(name, fsz, sign, round) \
11306uint32_t HELPER(name)(float##fsz x, void *fpstp) \
11307{ \
11308 float_status *fpst = fpstp; \
11309 if (float##fsz##_is_any_nan(x)) { \
11310 float_raise(float_flag_invalid, fpst); \
11311 return 0; \
11312 } \
11313 return float##fsz##_to_##sign##int32##round(x, fpst); \
4373f3ce
PB
11314}
11315
5500b06c
PM
11316#define FLOAT_CONVS(name, p, fsz, sign) \
11317CONV_ITOF(vfp_##name##to##p, fsz, sign) \
11318CONV_FTOI(vfp_to##name##p, fsz, sign, ) \
11319CONV_FTOI(vfp_to##name##z##p, fsz, sign, _round_to_zero)
4373f3ce 11320
93193190 11321FLOAT_CONVS(si, h, 16, )
5500b06c
PM
11322FLOAT_CONVS(si, s, 32, )
11323FLOAT_CONVS(si, d, 64, )
93193190 11324FLOAT_CONVS(ui, h, 16, u)
5500b06c
PM
11325FLOAT_CONVS(ui, s, 32, u)
11326FLOAT_CONVS(ui, d, 64, u)
4373f3ce 11327
5500b06c
PM
11328#undef CONV_ITOF
11329#undef CONV_FTOI
11330#undef FLOAT_CONVS
4373f3ce
PB
11331
11332/* floating point conversion */
0ecb72a5 11333float64 VFP_HELPER(fcvtd, s)(float32 x, CPUARMState *env)
4373f3ce 11334{
2d627737
PM
11335 float64 r = float32_to_float64(x, &env->vfp.fp_status);
11336 /* ARM requires that S<->D conversion of any kind of NaN generates
11337 * a quiet NaN by forcing the most significant frac bit to 1.
11338 */
af39bc8c 11339 return float64_maybe_silence_nan(r, &env->vfp.fp_status);
4373f3ce
PB
11340}
11341
0ecb72a5 11342float32 VFP_HELPER(fcvts, d)(float64 x, CPUARMState *env)
4373f3ce 11343{
2d627737
PM
11344 float32 r = float64_to_float32(x, &env->vfp.fp_status);
11345 /* ARM requires that S<->D conversion of any kind of NaN generates
11346 * a quiet NaN by forcing the most significant frac bit to 1.
11347 */
af39bc8c 11348 return float32_maybe_silence_nan(r, &env->vfp.fp_status);
4373f3ce
PB
11349}
11350
11351/* VFP3 fixed point conversion. */
16d5b3ca 11352#define VFP_CONV_FIX_FLOAT(name, p, fsz, isz, itype) \
8ed697e8
WN
11353float##fsz HELPER(vfp_##name##to##p)(uint##isz##_t x, uint32_t shift, \
11354 void *fpstp) \
4373f3ce 11355{ \
5500b06c 11356 float_status *fpst = fpstp; \
622465e1 11357 float##fsz tmp; \
8ed697e8 11358 tmp = itype##_to_##float##fsz(x, fpst); \
5500b06c 11359 return float##fsz##_scalbn(tmp, -(int)shift, fpst); \
16d5b3ca
WN
11360}
11361
abe66f70
PM
11362/* Notice that we want only input-denormal exception flags from the
11363 * scalbn operation: the other possible flags (overflow+inexact if
11364 * we overflow to infinity, output-denormal) aren't correct for the
11365 * complete scale-and-convert operation.
11366 */
16d5b3ca
WN
11367#define VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, round) \
11368uint##isz##_t HELPER(vfp_to##name##p##round)(float##fsz x, \
11369 uint32_t shift, \
11370 void *fpstp) \
4373f3ce 11371{ \
5500b06c 11372 float_status *fpst = fpstp; \
abe66f70 11373 int old_exc_flags = get_float_exception_flags(fpst); \
622465e1
PM
11374 float##fsz tmp; \
11375 if (float##fsz##_is_any_nan(x)) { \
5500b06c 11376 float_raise(float_flag_invalid, fpst); \
622465e1 11377 return 0; \
09d9487f 11378 } \
5500b06c 11379 tmp = float##fsz##_scalbn(x, shift, fpst); \
abe66f70
PM
11380 old_exc_flags |= get_float_exception_flags(fpst) \
11381 & float_flag_input_denormal; \
11382 set_float_exception_flags(old_exc_flags, fpst); \
16d5b3ca 11383 return float##fsz##_to_##itype##round(tmp, fpst); \
622465e1
PM
11384}
11385
16d5b3ca
WN
11386#define VFP_CONV_FIX(name, p, fsz, isz, itype) \
11387VFP_CONV_FIX_FLOAT(name, p, fsz, isz, itype) \
3c6a074a
WN
11388VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, _round_to_zero) \
11389VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, )
11390
11391#define VFP_CONV_FIX_A64(name, p, fsz, isz, itype) \
11392VFP_CONV_FIX_FLOAT(name, p, fsz, isz, itype) \
11393VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, )
16d5b3ca 11394
8ed697e8
WN
11395VFP_CONV_FIX(sh, d, 64, 64, int16)
11396VFP_CONV_FIX(sl, d, 64, 64, int32)
3c6a074a 11397VFP_CONV_FIX_A64(sq, d, 64, 64, int64)
8ed697e8
WN
11398VFP_CONV_FIX(uh, d, 64, 64, uint16)
11399VFP_CONV_FIX(ul, d, 64, 64, uint32)
3c6a074a 11400VFP_CONV_FIX_A64(uq, d, 64, 64, uint64)
8ed697e8
WN
11401VFP_CONV_FIX(sh, s, 32, 32, int16)
11402VFP_CONV_FIX(sl, s, 32, 32, int32)
3c6a074a 11403VFP_CONV_FIX_A64(sq, s, 32, 64, int64)
8ed697e8
WN
11404VFP_CONV_FIX(uh, s, 32, 32, uint16)
11405VFP_CONV_FIX(ul, s, 32, 32, uint32)
3c6a074a 11406VFP_CONV_FIX_A64(uq, s, 32, 64, uint64)
93193190
AB
11407VFP_CONV_FIX_A64(sl, h, 16, 32, int32)
11408VFP_CONV_FIX_A64(ul, h, 16, 32, uint32)
4373f3ce 11409#undef VFP_CONV_FIX
16d5b3ca
WN
11410#undef VFP_CONV_FIX_FLOAT
11411#undef VFP_CONV_FLOAT_FIX_ROUND
4373f3ce 11412
52a1f6a3
AG
11413/* Set the current fp rounding mode and return the old one.
11414 * The argument is a softfloat float_round_ value.
11415 */
9b049916 11416uint32_t HELPER(set_rmode)(uint32_t rmode, void *fpstp)
52a1f6a3 11417{
9b049916 11418 float_status *fp_status = fpstp;
52a1f6a3
AG
11419
11420 uint32_t prev_rmode = get_float_rounding_mode(fp_status);
11421 set_float_rounding_mode(rmode, fp_status);
11422
11423 return prev_rmode;
11424}
11425
43630e58
WN
11426/* Set the current fp rounding mode in the standard fp status and return
11427 * the old one. This is for NEON instructions that need to change the
11428 * rounding mode but wish to use the standard FPSCR values for everything
11429 * else. Always set the rounding mode back to the correct value after
11430 * modifying it.
11431 * The argument is a softfloat float_round_ value.
11432 */
11433uint32_t HELPER(set_neon_rmode)(uint32_t rmode, CPUARMState *env)
11434{
11435 float_status *fp_status = &env->vfp.standard_fp_status;
11436
11437 uint32_t prev_rmode = get_float_rounding_mode(fp_status);
11438 set_float_rounding_mode(rmode, fp_status);
11439
11440 return prev_rmode;
11441}
11442
60011498 11443/* Half precision conversions. */
0ecb72a5 11444static float32 do_fcvt_f16_to_f32(uint32_t a, CPUARMState *env, float_status *s)
60011498 11445{
60011498 11446 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
fb91678d
PM
11447 float32 r = float16_to_float32(make_float16(a), ieee, s);
11448 if (ieee) {
af39bc8c 11449 return float32_maybe_silence_nan(r, s);
fb91678d
PM
11450 }
11451 return r;
60011498
PB
11452}
11453
0ecb72a5 11454static uint32_t do_fcvt_f32_to_f16(float32 a, CPUARMState *env, float_status *s)
60011498 11455{
60011498 11456 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
fb91678d
PM
11457 float16 r = float32_to_float16(a, ieee, s);
11458 if (ieee) {
af39bc8c 11459 r = float16_maybe_silence_nan(r, s);
fb91678d
PM
11460 }
11461 return float16_val(r);
60011498
PB
11462}
11463
0ecb72a5 11464float32 HELPER(neon_fcvt_f16_to_f32)(uint32_t a, CPUARMState *env)
2d981da7
PM
11465{
11466 return do_fcvt_f16_to_f32(a, env, &env->vfp.standard_fp_status);
11467}
11468
0ecb72a5 11469uint32_t HELPER(neon_fcvt_f32_to_f16)(float32 a, CPUARMState *env)
2d981da7
PM
11470{
11471 return do_fcvt_f32_to_f16(a, env, &env->vfp.standard_fp_status);
11472}
11473
0ecb72a5 11474float32 HELPER(vfp_fcvt_f16_to_f32)(uint32_t a, CPUARMState *env)
2d981da7
PM
11475{
11476 return do_fcvt_f16_to_f32(a, env, &env->vfp.fp_status);
11477}
11478
0ecb72a5 11479uint32_t HELPER(vfp_fcvt_f32_to_f16)(float32 a, CPUARMState *env)
2d981da7
PM
11480{
11481 return do_fcvt_f32_to_f16(a, env, &env->vfp.fp_status);
11482}
11483
8900aad2
PM
11484float64 HELPER(vfp_fcvt_f16_to_f64)(uint32_t a, CPUARMState *env)
11485{
11486 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
11487 float64 r = float16_to_float64(make_float16(a), ieee, &env->vfp.fp_status);
11488 if (ieee) {
af39bc8c 11489 return float64_maybe_silence_nan(r, &env->vfp.fp_status);
8900aad2
PM
11490 }
11491 return r;
11492}
11493
11494uint32_t HELPER(vfp_fcvt_f64_to_f16)(float64 a, CPUARMState *env)
11495{
11496 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
11497 float16 r = float64_to_float16(a, ieee, &env->vfp.fp_status);
11498 if (ieee) {
af39bc8c 11499 r = float16_maybe_silence_nan(r, &env->vfp.fp_status);
8900aad2
PM
11500 }
11501 return float16_val(r);
11502}
11503
dda3ec49 11504#define float32_two make_float32(0x40000000)
6aae3df1
PM
11505#define float32_three make_float32(0x40400000)
11506#define float32_one_point_five make_float32(0x3fc00000)
dda3ec49 11507
0ecb72a5 11508float32 HELPER(recps_f32)(float32 a, float32 b, CPUARMState *env)
4373f3ce 11509{
dda3ec49
PM
11510 float_status *s = &env->vfp.standard_fp_status;
11511 if ((float32_is_infinity(a) && float32_is_zero_or_denormal(b)) ||
11512 (float32_is_infinity(b) && float32_is_zero_or_denormal(a))) {
43fe9bdb
PM
11513 if (!(float32_is_zero(a) || float32_is_zero(b))) {
11514 float_raise(float_flag_input_denormal, s);
11515 }
dda3ec49
PM
11516 return float32_two;
11517 }
11518 return float32_sub(float32_two, float32_mul(a, b, s), s);
4373f3ce
PB
11519}
11520
0ecb72a5 11521float32 HELPER(rsqrts_f32)(float32 a, float32 b, CPUARMState *env)
4373f3ce 11522{
71826966 11523 float_status *s = &env->vfp.standard_fp_status;
9ea62f57
PM
11524 float32 product;
11525 if ((float32_is_infinity(a) && float32_is_zero_or_denormal(b)) ||
11526 (float32_is_infinity(b) && float32_is_zero_or_denormal(a))) {
43fe9bdb
PM
11527 if (!(float32_is_zero(a) || float32_is_zero(b))) {
11528 float_raise(float_flag_input_denormal, s);
11529 }
6aae3df1 11530 return float32_one_point_five;
9ea62f57 11531 }
6aae3df1
PM
11532 product = float32_mul(a, b, s);
11533 return float32_div(float32_sub(float32_three, product, s), float32_two, s);
4373f3ce
PB
11534}
11535
8f8e3aa4
PB
11536/* NEON helpers. */
11537
56bf4fe2
CL
11538/* Constants 256 and 512 are used in some helpers; we avoid relying on
11539 * int->float conversions at run-time. */
11540#define float64_256 make_float64(0x4070000000000000LL)
11541#define float64_512 make_float64(0x4080000000000000LL)
5eb70735 11542#define float16_maxnorm make_float16(0x7bff)
b6d4443a
AB
11543#define float32_maxnorm make_float32(0x7f7fffff)
11544#define float64_maxnorm make_float64(0x7fefffffffffffffLL)
56bf4fe2 11545
b6d4443a
AB
11546/* Reciprocal functions
11547 *
11548 * The algorithm that must be used to calculate the estimate
5eb70735 11549 * is specified by the ARM ARM, see FPRecipEstimate()/RecipEstimate
fe0e4872 11550 */
b6d4443a 11551
5eb70735
AB
11552/* See RecipEstimate()
11553 *
11554 * input is a 9 bit fixed point number
11555 * input range 256 .. 511 for a number from 0.5 <= x < 1.0.
11556 * result range 256 .. 511 for a number from 1.0 to 511/256.
11557 */
fe0e4872 11558
5eb70735
AB
11559static int recip_estimate(int input)
11560{
11561 int a, b, r;
11562 assert(256 <= input && input < 512);
11563 a = (input * 2) + 1;
11564 b = (1 << 19) / a;
11565 r = (b + 1) >> 1;
11566 assert(256 <= r && r < 512);
11567 return r;
fe0e4872
CL
11568}
11569
5eb70735
AB
11570/*
11571 * Common wrapper to call recip_estimate
11572 *
11573 * The parameters are exponent and 64 bit fraction (without implicit
11574 * bit) where the binary point is nominally at bit 52. Returns a
11575 * float64 which can then be rounded to the appropriate size by the
11576 * callee.
11577 */
11578
11579static uint64_t call_recip_estimate(int *exp, int exp_off, uint64_t frac)
4373f3ce 11580{
5eb70735
AB
11581 uint32_t scaled, estimate;
11582 uint64_t result_frac;
11583 int result_exp;
fe0e4872 11584
5eb70735
AB
11585 /* Handle sub-normals */
11586 if (*exp == 0) {
b6d4443a 11587 if (extract64(frac, 51, 1) == 0) {
5eb70735
AB
11588 *exp = -1;
11589 frac <<= 2;
b6d4443a 11590 } else {
5eb70735 11591 frac <<= 1;
b6d4443a
AB
11592 }
11593 }
fe0e4872 11594
5eb70735
AB
11595 /* scaled = UInt('1':fraction<51:44>) */
11596 scaled = deposit32(1 << 8, 0, 8, extract64(frac, 44, 8));
11597 estimate = recip_estimate(scaled);
b6d4443a 11598
5eb70735
AB
11599 result_exp = exp_off - *exp;
11600 result_frac = deposit64(0, 44, 8, estimate);
11601 if (result_exp == 0) {
11602 result_frac = deposit64(result_frac >> 1, 51, 1, 1);
11603 } else if (result_exp == -1) {
11604 result_frac = deposit64(result_frac >> 2, 50, 2, 1);
11605 result_exp = 0;
b6d4443a
AB
11606 }
11607
5eb70735
AB
11608 *exp = result_exp;
11609
11610 return result_frac;
b6d4443a
AB
11611}
11612
11613static bool round_to_inf(float_status *fpst, bool sign_bit)
11614{
11615 switch (fpst->float_rounding_mode) {
11616 case float_round_nearest_even: /* Round to Nearest */
11617 return true;
11618 case float_round_up: /* Round to +Inf */
11619 return !sign_bit;
11620 case float_round_down: /* Round to -Inf */
11621 return sign_bit;
11622 case float_round_to_zero: /* Round to Zero */
11623 return false;
11624 }
11625
11626 g_assert_not_reached();
11627}
11628
5eb70735
AB
11629float16 HELPER(recpe_f16)(float16 input, void *fpstp)
11630{
11631 float_status *fpst = fpstp;
11632 float16 f16 = float16_squash_input_denormal(input, fpst);
11633 uint32_t f16_val = float16_val(f16);
11634 uint32_t f16_sign = float16_is_neg(f16);
11635 int f16_exp = extract32(f16_val, 10, 5);
11636 uint32_t f16_frac = extract32(f16_val, 0, 10);
11637 uint64_t f64_frac;
11638
11639 if (float16_is_any_nan(f16)) {
11640 float16 nan = f16;
11641 if (float16_is_signaling_nan(f16, fpst)) {
11642 float_raise(float_flag_invalid, fpst);
11643 nan = float16_maybe_silence_nan(f16, fpst);
11644 }
11645 if (fpst->default_nan_mode) {
11646 nan = float16_default_nan(fpst);
11647 }
11648 return nan;
11649 } else if (float16_is_infinity(f16)) {
11650 return float16_set_sign(float16_zero, float16_is_neg(f16));
11651 } else if (float16_is_zero(f16)) {
11652 float_raise(float_flag_divbyzero, fpst);
11653 return float16_set_sign(float16_infinity, float16_is_neg(f16));
11654 } else if (float16_abs(f16) < (1 << 8)) {
11655 /* Abs(value) < 2.0^-16 */
11656 float_raise(float_flag_overflow | float_flag_inexact, fpst);
11657 if (round_to_inf(fpst, f16_sign)) {
11658 return float16_set_sign(float16_infinity, f16_sign);
11659 } else {
11660 return float16_set_sign(float16_maxnorm, f16_sign);
11661 }
11662 } else if (f16_exp >= 29 && fpst->flush_to_zero) {
11663 float_raise(float_flag_underflow, fpst);
11664 return float16_set_sign(float16_zero, float16_is_neg(f16));
11665 }
11666
11667 f64_frac = call_recip_estimate(&f16_exp, 29,
11668 ((uint64_t) f16_frac) << (52 - 10));
11669
11670 /* result = sign : result_exp<4:0> : fraction<51:42> */
11671 f16_val = deposit32(0, 15, 1, f16_sign);
11672 f16_val = deposit32(f16_val, 10, 5, f16_exp);
11673 f16_val = deposit32(f16_val, 0, 10, extract64(f64_frac, 52 - 10, 10));
11674 return make_float16(f16_val);
11675}
11676
b6d4443a
AB
11677float32 HELPER(recpe_f32)(float32 input, void *fpstp)
11678{
11679 float_status *fpst = fpstp;
11680 float32 f32 = float32_squash_input_denormal(input, fpst);
11681 uint32_t f32_val = float32_val(f32);
5eb70735
AB
11682 bool f32_sign = float32_is_neg(f32);
11683 int f32_exp = extract32(f32_val, 23, 8);
b6d4443a 11684 uint32_t f32_frac = extract32(f32_val, 0, 23);
5eb70735 11685 uint64_t f64_frac;
b6d4443a
AB
11686
11687 if (float32_is_any_nan(f32)) {
11688 float32 nan = f32;
af39bc8c 11689 if (float32_is_signaling_nan(f32, fpst)) {
b6d4443a 11690 float_raise(float_flag_invalid, fpst);
af39bc8c 11691 nan = float32_maybe_silence_nan(f32, fpst);
fe0e4872 11692 }
b6d4443a 11693 if (fpst->default_nan_mode) {
af39bc8c 11694 nan = float32_default_nan(fpst);
43fe9bdb 11695 }
b6d4443a
AB
11696 return nan;
11697 } else if (float32_is_infinity(f32)) {
11698 return float32_set_sign(float32_zero, float32_is_neg(f32));
11699 } else if (float32_is_zero(f32)) {
11700 float_raise(float_flag_divbyzero, fpst);
11701 return float32_set_sign(float32_infinity, float32_is_neg(f32));
5eb70735 11702 } else if (float32_abs(f32) < (1ULL << 21)) {
b6d4443a
AB
11703 /* Abs(value) < 2.0^-128 */
11704 float_raise(float_flag_overflow | float_flag_inexact, fpst);
5eb70735
AB
11705 if (round_to_inf(fpst, f32_sign)) {
11706 return float32_set_sign(float32_infinity, f32_sign);
b6d4443a 11707 } else {
5eb70735 11708 return float32_set_sign(float32_maxnorm, f32_sign);
b6d4443a
AB
11709 }
11710 } else if (f32_exp >= 253 && fpst->flush_to_zero) {
11711 float_raise(float_flag_underflow, fpst);
11712 return float32_set_sign(float32_zero, float32_is_neg(f32));
fe0e4872
CL
11713 }
11714
5eb70735
AB
11715 f64_frac = call_recip_estimate(&f32_exp, 253,
11716 ((uint64_t) f32_frac) << (52 - 23));
fe0e4872 11717
5eb70735
AB
11718 /* result = sign : result_exp<7:0> : fraction<51:29> */
11719 f32_val = deposit32(0, 31, 1, f32_sign);
11720 f32_val = deposit32(f32_val, 23, 8, f32_exp);
11721 f32_val = deposit32(f32_val, 0, 23, extract64(f64_frac, 52 - 23, 23));
11722 return make_float32(f32_val);
b6d4443a
AB
11723}
11724
11725float64 HELPER(recpe_f64)(float64 input, void *fpstp)
11726{
11727 float_status *fpst = fpstp;
11728 float64 f64 = float64_squash_input_denormal(input, fpst);
11729 uint64_t f64_val = float64_val(f64);
5eb70735
AB
11730 bool f64_sign = float64_is_neg(f64);
11731 int f64_exp = extract64(f64_val, 52, 11);
11732 uint64_t f64_frac = extract64(f64_val, 0, 52);
b6d4443a
AB
11733
11734 /* Deal with any special cases */
11735 if (float64_is_any_nan(f64)) {
11736 float64 nan = f64;
af39bc8c 11737 if (float64_is_signaling_nan(f64, fpst)) {
b6d4443a 11738 float_raise(float_flag_invalid, fpst);
af39bc8c 11739 nan = float64_maybe_silence_nan(f64, fpst);
b6d4443a
AB
11740 }
11741 if (fpst->default_nan_mode) {
af39bc8c 11742 nan = float64_default_nan(fpst);
b6d4443a
AB
11743 }
11744 return nan;
11745 } else if (float64_is_infinity(f64)) {
11746 return float64_set_sign(float64_zero, float64_is_neg(f64));
11747 } else if (float64_is_zero(f64)) {
11748 float_raise(float_flag_divbyzero, fpst);
11749 return float64_set_sign(float64_infinity, float64_is_neg(f64));
11750 } else if ((f64_val & ~(1ULL << 63)) < (1ULL << 50)) {
11751 /* Abs(value) < 2.0^-1024 */
11752 float_raise(float_flag_overflow | float_flag_inexact, fpst);
5eb70735
AB
11753 if (round_to_inf(fpst, f64_sign)) {
11754 return float64_set_sign(float64_infinity, f64_sign);
b6d4443a 11755 } else {
5eb70735 11756 return float64_set_sign(float64_maxnorm, f64_sign);
b6d4443a 11757 }
fc1792e9 11758 } else if (f64_exp >= 2045 && fpst->flush_to_zero) {
b6d4443a
AB
11759 float_raise(float_flag_underflow, fpst);
11760 return float64_set_sign(float64_zero, float64_is_neg(f64));
11761 }
fe0e4872 11762
5eb70735 11763 f64_frac = call_recip_estimate(&f64_exp, 2045, f64_frac);
fe0e4872 11764
5eb70735
AB
11765 /* result = sign : result_exp<10:0> : fraction<51:0>; */
11766 f64_val = deposit64(0, 63, 1, f64_sign);
11767 f64_val = deposit64(f64_val, 52, 11, f64_exp);
11768 f64_val = deposit64(f64_val, 0, 52, f64_frac);
11769 return make_float64(f64_val);
4373f3ce
PB
11770}
11771
e07be5d2
CL
11772/* The algorithm that must be used to calculate the estimate
11773 * is specified by the ARM ARM.
11774 */
d719cbc7
AB
11775
11776static int do_recip_sqrt_estimate(int a)
11777{
11778 int b, estimate;
11779
11780 assert(128 <= a && a < 512);
11781 if (a < 256) {
11782 a = a * 2 + 1;
e07be5d2 11783 } else {
d719cbc7
AB
11784 a = (a >> 1) << 1;
11785 a = (a + 1) * 2;
11786 }
11787 b = 512;
11788 while (a * (b + 1) * (b + 1) < (1 << 28)) {
11789 b += 1;
11790 }
11791 estimate = (b + 1) / 2;
11792 assert(256 <= estimate && estimate < 512);
11793
11794 return estimate;
11795}
11796
e07be5d2 11797
d719cbc7
AB
11798static uint64_t recip_sqrt_estimate(int *exp , int exp_off, uint64_t frac)
11799{
11800 int estimate;
11801 uint32_t scaled;
e07be5d2 11802
d719cbc7
AB
11803 if (*exp == 0) {
11804 while (extract64(frac, 51, 1) == 0) {
11805 frac = frac << 1;
11806 *exp -= 1;
11807 }
11808 frac = extract64(frac, 0, 51) << 1;
e07be5d2 11809 }
e07be5d2 11810
d719cbc7
AB
11811 if (*exp & 1) {
11812 /* scaled = UInt('01':fraction<51:45>) */
11813 scaled = deposit32(1 << 7, 0, 7, extract64(frac, 45, 7));
11814 } else {
11815 /* scaled = UInt('1':fraction<51:44>) */
11816 scaled = deposit32(1 << 8, 0, 8, extract64(frac, 44, 8));
11817 }
11818 estimate = do_recip_sqrt_estimate(scaled);
e07be5d2 11819
d719cbc7
AB
11820 *exp = (exp_off - *exp) / 2;
11821 return extract64(estimate, 0, 8) << 44;
11822}
11823
11824float16 HELPER(rsqrte_f16)(float16 input, void *fpstp)
11825{
11826 float_status *s = fpstp;
11827 float16 f16 = float16_squash_input_denormal(input, s);
11828 uint16_t val = float16_val(f16);
11829 bool f16_sign = float16_is_neg(f16);
11830 int f16_exp = extract32(val, 10, 5);
11831 uint16_t f16_frac = extract32(val, 0, 10);
11832 uint64_t f64_frac;
11833
11834 if (float16_is_any_nan(f16)) {
11835 float16 nan = f16;
11836 if (float16_is_signaling_nan(f16, s)) {
11837 float_raise(float_flag_invalid, s);
11838 nan = float16_maybe_silence_nan(f16, s);
11839 }
11840 if (s->default_nan_mode) {
11841 nan = float16_default_nan(s);
11842 }
11843 return nan;
11844 } else if (float16_is_zero(f16)) {
11845 float_raise(float_flag_divbyzero, s);
11846 return float16_set_sign(float16_infinity, f16_sign);
11847 } else if (f16_sign) {
11848 float_raise(float_flag_invalid, s);
11849 return float16_default_nan(s);
11850 } else if (float16_is_infinity(f16)) {
11851 return float16_zero;
11852 }
11853
11854 /* Scale and normalize to a double-precision value between 0.25 and 1.0,
11855 * preserving the parity of the exponent. */
11856
11857 f64_frac = ((uint64_t) f16_frac) << (52 - 10);
11858
11859 f64_frac = recip_sqrt_estimate(&f16_exp, 44, f64_frac);
11860
11861 /* result = sign : result_exp<4:0> : estimate<7:0> : Zeros(2) */
11862 val = deposit32(0, 15, 1, f16_sign);
11863 val = deposit32(val, 10, 5, f16_exp);
11864 val = deposit32(val, 2, 8, extract64(f64_frac, 52 - 8, 8));
11865 return make_float16(val);
e07be5d2
CL
11866}
11867
c2fb418e 11868float32 HELPER(rsqrte_f32)(float32 input, void *fpstp)
4373f3ce 11869{
c2fb418e
AB
11870 float_status *s = fpstp;
11871 float32 f32 = float32_squash_input_denormal(input, s);
11872 uint32_t val = float32_val(f32);
d719cbc7
AB
11873 uint32_t f32_sign = float32_is_neg(f32);
11874 int f32_exp = extract32(val, 23, 8);
c2fb418e
AB
11875 uint32_t f32_frac = extract32(val, 0, 23);
11876 uint64_t f64_frac;
e07be5d2 11877
c2fb418e
AB
11878 if (float32_is_any_nan(f32)) {
11879 float32 nan = f32;
af39bc8c 11880 if (float32_is_signaling_nan(f32, s)) {
e07be5d2 11881 float_raise(float_flag_invalid, s);
af39bc8c 11882 nan = float32_maybe_silence_nan(f32, s);
e07be5d2 11883 }
c2fb418e 11884 if (s->default_nan_mode) {
af39bc8c 11885 nan = float32_default_nan(s);
43fe9bdb 11886 }
c2fb418e
AB
11887 return nan;
11888 } else if (float32_is_zero(f32)) {
e07be5d2 11889 float_raise(float_flag_divbyzero, s);
c2fb418e
AB
11890 return float32_set_sign(float32_infinity, float32_is_neg(f32));
11891 } else if (float32_is_neg(f32)) {
e07be5d2 11892 float_raise(float_flag_invalid, s);
af39bc8c 11893 return float32_default_nan(s);
c2fb418e 11894 } else if (float32_is_infinity(f32)) {
e07be5d2
CL
11895 return float32_zero;
11896 }
11897
c2fb418e 11898 /* Scale and normalize to a double-precision value between 0.25 and 1.0,
e07be5d2 11899 * preserving the parity of the exponent. */
c2fb418e
AB
11900
11901 f64_frac = ((uint64_t) f32_frac) << 29;
e07be5d2 11902
d719cbc7 11903 f64_frac = recip_sqrt_estimate(&f32_exp, 380, f64_frac);
e07be5d2 11904
d719cbc7
AB
11905 /* result = sign : result_exp<4:0> : estimate<7:0> : Zeros(15) */
11906 val = deposit32(0, 31, 1, f32_sign);
11907 val = deposit32(val, 23, 8, f32_exp);
11908 val = deposit32(val, 15, 8, extract64(f64_frac, 52 - 8, 8));
e07be5d2 11909 return make_float32(val);
4373f3ce
PB
11910}
11911
c2fb418e
AB
11912float64 HELPER(rsqrte_f64)(float64 input, void *fpstp)
11913{
11914 float_status *s = fpstp;
11915 float64 f64 = float64_squash_input_denormal(input, s);
11916 uint64_t val = float64_val(f64);
d719cbc7
AB
11917 bool f64_sign = float64_is_neg(f64);
11918 int f64_exp = extract64(val, 52, 11);
c2fb418e 11919 uint64_t f64_frac = extract64(val, 0, 52);
c2fb418e
AB
11920
11921 if (float64_is_any_nan(f64)) {
11922 float64 nan = f64;
af39bc8c 11923 if (float64_is_signaling_nan(f64, s)) {
c2fb418e 11924 float_raise(float_flag_invalid, s);
af39bc8c 11925 nan = float64_maybe_silence_nan(f64, s);
c2fb418e
AB
11926 }
11927 if (s->default_nan_mode) {
af39bc8c 11928 nan = float64_default_nan(s);
c2fb418e
AB
11929 }
11930 return nan;
11931 } else if (float64_is_zero(f64)) {
11932 float_raise(float_flag_divbyzero, s);
11933 return float64_set_sign(float64_infinity, float64_is_neg(f64));
11934 } else if (float64_is_neg(f64)) {
11935 float_raise(float_flag_invalid, s);
af39bc8c 11936 return float64_default_nan(s);
c2fb418e
AB
11937 } else if (float64_is_infinity(f64)) {
11938 return float64_zero;
11939 }
11940
d719cbc7 11941 f64_frac = recip_sqrt_estimate(&f64_exp, 3068, f64_frac);
c2fb418e 11942
d719cbc7
AB
11943 /* result = sign : result_exp<4:0> : estimate<7:0> : Zeros(44) */
11944 val = deposit64(0, 61, 1, f64_sign);
11945 val = deposit64(val, 52, 11, f64_exp);
11946 val = deposit64(val, 44, 8, extract64(f64_frac, 52 - 8, 8));
11947 return make_float64(val);
c2fb418e
AB
11948}
11949
b6d4443a 11950uint32_t HELPER(recpe_u32)(uint32_t a, void *fpstp)
4373f3ce 11951{
5eb70735
AB
11952 /* float_status *s = fpstp; */
11953 int input, estimate;
fe0e4872
CL
11954
11955 if ((a & 0x80000000) == 0) {
11956 return 0xffffffff;
11957 }
11958
5eb70735
AB
11959 input = extract32(a, 23, 9);
11960 estimate = recip_estimate(input);
fe0e4872 11961
5eb70735 11962 return deposit32(0, (32 - 9), 9, estimate);
4373f3ce
PB
11963}
11964
c2fb418e 11965uint32_t HELPER(rsqrte_u32)(uint32_t a, void *fpstp)
4373f3ce 11966{
d719cbc7 11967 int estimate;
e07be5d2
CL
11968
11969 if ((a & 0xc0000000) == 0) {
11970 return 0xffffffff;
11971 }
11972
d719cbc7 11973 estimate = do_recip_sqrt_estimate(extract32(a, 23, 9));
e07be5d2 11974
d719cbc7 11975 return deposit32(0, 23, 9, estimate);
4373f3ce 11976}
fe1479c3 11977
da97f52c
PM
11978/* VFPv4 fused multiply-accumulate */
11979float32 VFP_HELPER(muladd, s)(float32 a, float32 b, float32 c, void *fpstp)
11980{
11981 float_status *fpst = fpstp;
11982 return float32_muladd(a, b, c, 0, fpst);
11983}
11984
11985float64 VFP_HELPER(muladd, d)(float64 a, float64 b, float64 c, void *fpstp)
11986{
11987 float_status *fpst = fpstp;
11988 return float64_muladd(a, b, c, 0, fpst);
11989}
d9b0848d
PM
11990
11991/* ARMv8 round to integral */
11992float32 HELPER(rints_exact)(float32 x, void *fp_status)
11993{
11994 return float32_round_to_int(x, fp_status);
11995}
11996
11997float64 HELPER(rintd_exact)(float64 x, void *fp_status)
11998{
11999 return float64_round_to_int(x, fp_status);
12000}
12001
12002float32 HELPER(rints)(float32 x, void *fp_status)
12003{
12004 int old_flags = get_float_exception_flags(fp_status), new_flags;
12005 float32 ret;
12006
12007 ret = float32_round_to_int(x, fp_status);
12008
12009 /* Suppress any inexact exceptions the conversion produced */
12010 if (!(old_flags & float_flag_inexact)) {
12011 new_flags = get_float_exception_flags(fp_status);
12012 set_float_exception_flags(new_flags & ~float_flag_inexact, fp_status);
12013 }
12014
12015 return ret;
12016}
12017
12018float64 HELPER(rintd)(float64 x, void *fp_status)
12019{
12020 int old_flags = get_float_exception_flags(fp_status), new_flags;
12021 float64 ret;
12022
12023 ret = float64_round_to_int(x, fp_status);
12024
12025 new_flags = get_float_exception_flags(fp_status);
12026
12027 /* Suppress any inexact exceptions the conversion produced */
12028 if (!(old_flags & float_flag_inexact)) {
12029 new_flags = get_float_exception_flags(fp_status);
12030 set_float_exception_flags(new_flags & ~float_flag_inexact, fp_status);
12031 }
12032
12033 return ret;
12034}
9972da66
WN
12035
12036/* Convert ARM rounding mode to softfloat */
12037int arm_rmode_to_sf(int rmode)
12038{
12039 switch (rmode) {
12040 case FPROUNDING_TIEAWAY:
12041 rmode = float_round_ties_away;
12042 break;
12043 case FPROUNDING_ODD:
12044 /* FIXME: add support for TIEAWAY and ODD */
12045 qemu_log_mask(LOG_UNIMP, "arm: unimplemented rounding mode: %d\n",
12046 rmode);
12047 case FPROUNDING_TIEEVEN:
12048 default:
12049 rmode = float_round_nearest_even;
12050 break;
12051 case FPROUNDING_POSINF:
12052 rmode = float_round_up;
12053 break;
12054 case FPROUNDING_NEGINF:
12055 rmode = float_round_down;
12056 break;
12057 case FPROUNDING_ZERO:
12058 rmode = float_round_to_zero;
12059 break;
12060 }
12061 return rmode;
12062}
eb0ecd5a 12063
aa633469
PM
12064/* CRC helpers.
12065 * The upper bytes of val (above the number specified by 'bytes') must have
12066 * been zeroed out by the caller.
12067 */
eb0ecd5a
WN
12068uint32_t HELPER(crc32)(uint32_t acc, uint32_t val, uint32_t bytes)
12069{
12070 uint8_t buf[4];
12071
aa633469 12072 stl_le_p(buf, val);
eb0ecd5a
WN
12073
12074 /* zlib crc32 converts the accumulator and output to one's complement. */
12075 return crc32(acc ^ 0xffffffff, buf, bytes) ^ 0xffffffff;
12076}
12077
12078uint32_t HELPER(crc32c)(uint32_t acc, uint32_t val, uint32_t bytes)
12079{
12080 uint8_t buf[4];
12081
aa633469 12082 stl_le_p(buf, val);
eb0ecd5a
WN
12083
12084 /* Linux crc32c converts the output to one's complement. */
12085 return crc32c(acc, buf, bytes) ^ 0xffffffff;
12086}
a9e01311
RH
12087
12088/* Return the exception level to which FP-disabled exceptions should
12089 * be taken, or 0 if FP is enabled.
12090 */
12091static inline int fp_exception_el(CPUARMState *env)
12092{
55faa212 12093#ifndef CONFIG_USER_ONLY
a9e01311
RH
12094 int fpen;
12095 int cur_el = arm_current_el(env);
12096
12097 /* CPACR and the CPTR registers don't exist before v6, so FP is
12098 * always accessible
12099 */
12100 if (!arm_feature(env, ARM_FEATURE_V6)) {
12101 return 0;
12102 }
12103
12104 /* The CPACR controls traps to EL1, or PL1 if we're 32 bit:
12105 * 0, 2 : trap EL0 and EL1/PL1 accesses
12106 * 1 : trap only EL0 accesses
12107 * 3 : trap no accesses
12108 */
12109 fpen = extract32(env->cp15.cpacr_el1, 20, 2);
12110 switch (fpen) {
12111 case 0:
12112 case 2:
12113 if (cur_el == 0 || cur_el == 1) {
12114 /* Trap to PL1, which might be EL1 or EL3 */
12115 if (arm_is_secure(env) && !arm_el_is_aa64(env, 3)) {
12116 return 3;
12117 }
12118 return 1;
12119 }
12120 if (cur_el == 3 && !is_a64(env)) {
12121 /* Secure PL1 running at EL3 */
12122 return 3;
12123 }
12124 break;
12125 case 1:
12126 if (cur_el == 0) {
12127 return 1;
12128 }
12129 break;
12130 case 3:
12131 break;
12132 }
12133
12134 /* For the CPTR registers we don't need to guard with an ARM_FEATURE
12135 * check because zero bits in the registers mean "don't trap".
12136 */
12137
12138 /* CPTR_EL2 : present in v7VE or v8 */
12139 if (cur_el <= 2 && extract32(env->cp15.cptr_el[2], 10, 1)
12140 && !arm_is_secure_below_el3(env)) {
12141 /* Trap FP ops at EL2, NS-EL1 or NS-EL0 to EL2 */
12142 return 2;
12143 }
12144
12145 /* CPTR_EL3 : present in v8 */
12146 if (extract32(env->cp15.cptr_el[3], 10, 1)) {
12147 /* Trap all FP ops to EL3 */
12148 return 3;
12149 }
55faa212 12150#endif
a9e01311
RH
12151 return 0;
12152}
12153
12154void cpu_get_tb_cpu_state(CPUARMState *env, target_ulong *pc,
b9adaa70 12155 target_ulong *cs_base, uint32_t *pflags)
a9e01311
RH
12156{
12157 ARMMMUIdx mmu_idx = core_to_arm_mmu_idx(env, cpu_mmu_index(env, false));
1db5e96c 12158 int fp_el = fp_exception_el(env);
b9adaa70
RH
12159 uint32_t flags;
12160
a9e01311 12161 if (is_a64(env)) {
1db5e96c
RH
12162 int sve_el = sve_exception_el(env);
12163 uint32_t zcr_len;
12164
a9e01311 12165 *pc = env->pc;
b9adaa70 12166 flags = ARM_TBFLAG_AARCH64_STATE_MASK;
a9e01311 12167 /* Get control bits for tagged addresses */
b9adaa70
RH
12168 flags |= (arm_regime_tbi0(env, mmu_idx) << ARM_TBFLAG_TBI0_SHIFT);
12169 flags |= (arm_regime_tbi1(env, mmu_idx) << ARM_TBFLAG_TBI1_SHIFT);
1db5e96c
RH
12170 flags |= sve_el << ARM_TBFLAG_SVEEXC_EL_SHIFT;
12171
12172 /* If SVE is disabled, but FP is enabled,
12173 then the effective len is 0. */
12174 if (sve_el != 0 && fp_el == 0) {
12175 zcr_len = 0;
12176 } else {
12177 int current_el = arm_current_el(env);
12178
12179 zcr_len = env->vfp.zcr_el[current_el <= 1 ? 1 : current_el];
12180 zcr_len &= 0xf;
12181 if (current_el < 2 && arm_feature(env, ARM_FEATURE_EL2)) {
12182 zcr_len = MIN(zcr_len, 0xf & (uint32_t)env->vfp.zcr_el[2]);
12183 }
12184 if (current_el < 3 && arm_feature(env, ARM_FEATURE_EL3)) {
12185 zcr_len = MIN(zcr_len, 0xf & (uint32_t)env->vfp.zcr_el[3]);
12186 }
12187 }
12188 flags |= zcr_len << ARM_TBFLAG_ZCR_LEN_SHIFT;
a9e01311
RH
12189 } else {
12190 *pc = env->regs[15];
b9adaa70 12191 flags = (env->thumb << ARM_TBFLAG_THUMB_SHIFT)
a9e01311
RH
12192 | (env->vfp.vec_len << ARM_TBFLAG_VECLEN_SHIFT)
12193 | (env->vfp.vec_stride << ARM_TBFLAG_VECSTRIDE_SHIFT)
12194 | (env->condexec_bits << ARM_TBFLAG_CONDEXEC_SHIFT)
12195 | (arm_sctlr_b(env) << ARM_TBFLAG_SCTLR_B_SHIFT);
12196 if (!(access_secure_reg(env))) {
b9adaa70 12197 flags |= ARM_TBFLAG_NS_MASK;
a9e01311
RH
12198 }
12199 if (env->vfp.xregs[ARM_VFP_FPEXC] & (1 << 30)
12200 || arm_el_is_aa64(env, 1)) {
b9adaa70 12201 flags |= ARM_TBFLAG_VFPEN_MASK;
a9e01311 12202 }
b9adaa70
RH
12203 flags |= (extract32(env->cp15.c15_cpar, 0, 2)
12204 << ARM_TBFLAG_XSCALE_CPAR_SHIFT);
a9e01311
RH
12205 }
12206
b9adaa70 12207 flags |= (arm_to_core_mmu_idx(mmu_idx) << ARM_TBFLAG_MMUIDX_SHIFT);
a9e01311
RH
12208
12209 /* The SS_ACTIVE and PSTATE_SS bits correspond to the state machine
12210 * states defined in the ARM ARM for software singlestep:
12211 * SS_ACTIVE PSTATE.SS State
12212 * 0 x Inactive (the TB flag for SS is always 0)
12213 * 1 0 Active-pending
12214 * 1 1 Active-not-pending
12215 */
12216 if (arm_singlestep_active(env)) {
b9adaa70 12217 flags |= ARM_TBFLAG_SS_ACTIVE_MASK;
a9e01311
RH
12218 if (is_a64(env)) {
12219 if (env->pstate & PSTATE_SS) {
b9adaa70 12220 flags |= ARM_TBFLAG_PSTATE_SS_MASK;
a9e01311
RH
12221 }
12222 } else {
12223 if (env->uncached_cpsr & PSTATE_SS) {
b9adaa70 12224 flags |= ARM_TBFLAG_PSTATE_SS_MASK;
a9e01311
RH
12225 }
12226 }
12227 }
12228 if (arm_cpu_data_is_big_endian(env)) {
b9adaa70 12229 flags |= ARM_TBFLAG_BE_DATA_MASK;
a9e01311 12230 }
1db5e96c 12231 flags |= fp_el << ARM_TBFLAG_FPEXC_EL_SHIFT;
a9e01311
RH
12232
12233 if (arm_v7m_is_handler_mode(env)) {
b9adaa70 12234 flags |= ARM_TBFLAG_HANDLER_MASK;
a9e01311
RH
12235 }
12236
b9adaa70 12237 *pflags = flags;
a9e01311
RH
12238 *cs_base = 0;
12239}
This page took 3.142434 seconds and 4 git commands to generate.