]> Git Repo - qemu.git/blame - target/arm/helper.c
target/arm: Enforce FP access to FPCR/FPSR
[qemu.git] / target / arm / helper.c
CommitLineData
74c21bd0 1#include "qemu/osdep.h"
194cbc49 2#include "trace.h"
b5ff1b31 3#include "cpu.h"
ccd38087 4#include "internals.h"
022c62cb 5#include "exec/gdbstub.h"
2ef6175a 6#include "exec/helper-proto.h"
1de7afc9 7#include "qemu/host-utils.h"
78027bb6 8#include "sysemu/arch_init.h"
9c17d615 9#include "sysemu/sysemu.h"
1de7afc9 10#include "qemu/bitops.h"
eb0ecd5a 11#include "qemu/crc32c.h"
63c91552 12#include "exec/exec-all.h"
f08b6170 13#include "exec/cpu_ldst.h"
1d854765 14#include "arm_ldst.h"
eb0ecd5a 15#include <zlib.h> /* For crc32 */
cfe67cef 16#include "exec/semihost.h"
f3a9b694 17#include "sysemu/kvm.h"
0b03bdfc 18
352c98e5
LV
19#define ARM_CPU_FREQ 1000000000 /* FIXME: 1 GHz, should be configurable */
20
4a501606 21#ifndef CONFIG_USER_ONLY
5b2d261d
AB
22/* Cacheability and shareability attributes for a memory access */
23typedef struct ARMCacheAttrs {
24 unsigned int attrs:8; /* as in the MAIR register encoding */
25 unsigned int shareability:2; /* as in the SH field of the VMSAv8-64 PTEs */
26} ARMCacheAttrs;
27
af51f566 28static bool get_phys_addr(CPUARMState *env, target_ulong address,
03ae85f8 29 MMUAccessType access_type, ARMMMUIdx mmu_idx,
af51f566 30 hwaddr *phys_ptr, MemTxAttrs *attrs, int *prot,
bc52bfeb 31 target_ulong *page_size,
5b2d261d 32 ARMMMUFaultInfo *fi, ARMCacheAttrs *cacheattrs);
7c2cb42b 33
37785977 34static bool get_phys_addr_lpae(CPUARMState *env, target_ulong address,
03ae85f8 35 MMUAccessType access_type, ARMMMUIdx mmu_idx,
37785977 36 hwaddr *phys_ptr, MemTxAttrs *txattrs, int *prot,
da909b2c 37 target_ulong *page_size_ptr,
5b2d261d 38 ARMMMUFaultInfo *fi, ARMCacheAttrs *cacheattrs);
37785977 39
35337cc3
PM
40/* Security attributes for an address, as returned by v8m_security_lookup. */
41typedef struct V8M_SAttributes {
42 bool ns;
43 bool nsc;
44 uint8_t sregion;
45 bool srvalid;
46 uint8_t iregion;
47 bool irvalid;
48} V8M_SAttributes;
49
333e10c5
PM
50static void v8m_security_lookup(CPUARMState *env, uint32_t address,
51 MMUAccessType access_type, ARMMMUIdx mmu_idx,
52 V8M_SAttributes *sattrs);
53
7c2cb42b
AF
54/* Definitions for the PMCCNTR and PMCR registers */
55#define PMCRD 0x8
56#define PMCRC 0x4
57#define PMCRE 0x1
4a501606
PM
58#endif
59
0ecb72a5 60static int vfp_gdb_get_reg(CPUARMState *env, uint8_t *buf, int reg)
56aebc89
PB
61{
62 int nregs;
63
64 /* VFP data registers are always little-endian. */
65 nregs = arm_feature(env, ARM_FEATURE_VFP3) ? 32 : 16;
66 if (reg < nregs) {
9a2b5256 67 stq_le_p(buf, *aa32_vfp_dreg(env, reg));
56aebc89
PB
68 return 8;
69 }
70 if (arm_feature(env, ARM_FEATURE_NEON)) {
71 /* Aliases for Q regs. */
72 nregs += 16;
73 if (reg < nregs) {
9a2b5256
RH
74 uint64_t *q = aa32_vfp_qreg(env, reg - 32);
75 stq_le_p(buf, q[0]);
76 stq_le_p(buf + 8, q[1]);
56aebc89
PB
77 return 16;
78 }
79 }
80 switch (reg - nregs) {
81 case 0: stl_p(buf, env->vfp.xregs[ARM_VFP_FPSID]); return 4;
82 case 1: stl_p(buf, env->vfp.xregs[ARM_VFP_FPSCR]); return 4;
83 case 2: stl_p(buf, env->vfp.xregs[ARM_VFP_FPEXC]); return 4;
84 }
85 return 0;
86}
87
0ecb72a5 88static int vfp_gdb_set_reg(CPUARMState *env, uint8_t *buf, int reg)
56aebc89
PB
89{
90 int nregs;
91
92 nregs = arm_feature(env, ARM_FEATURE_VFP3) ? 32 : 16;
93 if (reg < nregs) {
9a2b5256 94 *aa32_vfp_dreg(env, reg) = ldq_le_p(buf);
56aebc89
PB
95 return 8;
96 }
97 if (arm_feature(env, ARM_FEATURE_NEON)) {
98 nregs += 16;
99 if (reg < nregs) {
9a2b5256
RH
100 uint64_t *q = aa32_vfp_qreg(env, reg - 32);
101 q[0] = ldq_le_p(buf);
102 q[1] = ldq_le_p(buf + 8);
56aebc89
PB
103 return 16;
104 }
105 }
106 switch (reg - nregs) {
107 case 0: env->vfp.xregs[ARM_VFP_FPSID] = ldl_p(buf); return 4;
108 case 1: env->vfp.xregs[ARM_VFP_FPSCR] = ldl_p(buf); return 4;
71b3c3de 109 case 2: env->vfp.xregs[ARM_VFP_FPEXC] = ldl_p(buf) & (1 << 30); return 4;
56aebc89
PB
110 }
111 return 0;
112}
113
6a669427
PM
114static int aarch64_fpu_gdb_get_reg(CPUARMState *env, uint8_t *buf, int reg)
115{
116 switch (reg) {
117 case 0 ... 31:
118 /* 128 bit FP register */
9a2b5256
RH
119 {
120 uint64_t *q = aa64_vfp_qreg(env, reg);
121 stq_le_p(buf, q[0]);
122 stq_le_p(buf + 8, q[1]);
123 return 16;
124 }
6a669427
PM
125 case 32:
126 /* FPSR */
127 stl_p(buf, vfp_get_fpsr(env));
128 return 4;
129 case 33:
130 /* FPCR */
131 stl_p(buf, vfp_get_fpcr(env));
132 return 4;
133 default:
134 return 0;
135 }
136}
137
138static int aarch64_fpu_gdb_set_reg(CPUARMState *env, uint8_t *buf, int reg)
139{
140 switch (reg) {
141 case 0 ... 31:
142 /* 128 bit FP register */
9a2b5256
RH
143 {
144 uint64_t *q = aa64_vfp_qreg(env, reg);
145 q[0] = ldq_le_p(buf);
146 q[1] = ldq_le_p(buf + 8);
147 return 16;
148 }
6a669427
PM
149 case 32:
150 /* FPSR */
151 vfp_set_fpsr(env, ldl_p(buf));
152 return 4;
153 case 33:
154 /* FPCR */
155 vfp_set_fpcr(env, ldl_p(buf));
156 return 4;
157 default:
158 return 0;
159 }
160}
161
c4241c7d 162static uint64_t raw_read(CPUARMState *env, const ARMCPRegInfo *ri)
d4e6df63 163{
375421cc 164 assert(ri->fieldoffset);
67ed771d 165 if (cpreg_field_is_64bit(ri)) {
c4241c7d 166 return CPREG_FIELD64(env, ri);
22d9e1a9 167 } else {
c4241c7d 168 return CPREG_FIELD32(env, ri);
22d9e1a9 169 }
d4e6df63
PM
170}
171
c4241c7d
PM
172static void raw_write(CPUARMState *env, const ARMCPRegInfo *ri,
173 uint64_t value)
d4e6df63 174{
375421cc 175 assert(ri->fieldoffset);
67ed771d 176 if (cpreg_field_is_64bit(ri)) {
22d9e1a9
PM
177 CPREG_FIELD64(env, ri) = value;
178 } else {
179 CPREG_FIELD32(env, ri) = value;
180 }
d4e6df63
PM
181}
182
11f136ee
FA
183static void *raw_ptr(CPUARMState *env, const ARMCPRegInfo *ri)
184{
185 return (char *)env + ri->fieldoffset;
186}
187
49a66191 188uint64_t read_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri)
721fae12 189{
59a1c327 190 /* Raw read of a coprocessor register (as needed for migration, etc). */
721fae12 191 if (ri->type & ARM_CP_CONST) {
59a1c327 192 return ri->resetvalue;
721fae12 193 } else if (ri->raw_readfn) {
59a1c327 194 return ri->raw_readfn(env, ri);
721fae12 195 } else if (ri->readfn) {
59a1c327 196 return ri->readfn(env, ri);
721fae12 197 } else {
59a1c327 198 return raw_read(env, ri);
721fae12 199 }
721fae12
PM
200}
201
59a1c327 202static void write_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri,
7900e9f1 203 uint64_t v)
721fae12
PM
204{
205 /* Raw write of a coprocessor register (as needed for migration, etc).
721fae12
PM
206 * Note that constant registers are treated as write-ignored; the
207 * caller should check for success by whether a readback gives the
208 * value written.
209 */
210 if (ri->type & ARM_CP_CONST) {
59a1c327 211 return;
721fae12 212 } else if (ri->raw_writefn) {
c4241c7d 213 ri->raw_writefn(env, ri, v);
721fae12 214 } else if (ri->writefn) {
c4241c7d 215 ri->writefn(env, ri, v);
721fae12 216 } else {
afb2530f 217 raw_write(env, ri, v);
721fae12 218 }
721fae12
PM
219}
220
375421cc
PM
221static bool raw_accessors_invalid(const ARMCPRegInfo *ri)
222{
223 /* Return true if the regdef would cause an assertion if you called
224 * read_raw_cp_reg() or write_raw_cp_reg() on it (ie if it is a
225 * program bug for it not to have the NO_RAW flag).
226 * NB that returning false here doesn't necessarily mean that calling
227 * read/write_raw_cp_reg() is safe, because we can't distinguish "has
228 * read/write access functions which are safe for raw use" from "has
229 * read/write access functions which have side effects but has forgotten
230 * to provide raw access functions".
231 * The tests here line up with the conditions in read/write_raw_cp_reg()
232 * and assertions in raw_read()/raw_write().
233 */
234 if ((ri->type & ARM_CP_CONST) ||
235 ri->fieldoffset ||
236 ((ri->raw_writefn || ri->writefn) && (ri->raw_readfn || ri->readfn))) {
237 return false;
238 }
239 return true;
240}
241
721fae12
PM
242bool write_cpustate_to_list(ARMCPU *cpu)
243{
244 /* Write the coprocessor state from cpu->env to the (index,value) list. */
245 int i;
246 bool ok = true;
247
248 for (i = 0; i < cpu->cpreg_array_len; i++) {
249 uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
250 const ARMCPRegInfo *ri;
59a1c327 251
60322b39 252 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
721fae12
PM
253 if (!ri) {
254 ok = false;
255 continue;
256 }
7a0e58fa 257 if (ri->type & ARM_CP_NO_RAW) {
721fae12
PM
258 continue;
259 }
59a1c327 260 cpu->cpreg_values[i] = read_raw_cp_reg(&cpu->env, ri);
721fae12
PM
261 }
262 return ok;
263}
264
265bool write_list_to_cpustate(ARMCPU *cpu)
266{
267 int i;
268 bool ok = true;
269
270 for (i = 0; i < cpu->cpreg_array_len; i++) {
271 uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
272 uint64_t v = cpu->cpreg_values[i];
721fae12
PM
273 const ARMCPRegInfo *ri;
274
60322b39 275 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
721fae12
PM
276 if (!ri) {
277 ok = false;
278 continue;
279 }
7a0e58fa 280 if (ri->type & ARM_CP_NO_RAW) {
721fae12
PM
281 continue;
282 }
283 /* Write value and confirm it reads back as written
284 * (to catch read-only registers and partially read-only
285 * registers where the incoming migration value doesn't match)
286 */
59a1c327
PM
287 write_raw_cp_reg(&cpu->env, ri, v);
288 if (read_raw_cp_reg(&cpu->env, ri) != v) {
721fae12
PM
289 ok = false;
290 }
291 }
292 return ok;
293}
294
295static void add_cpreg_to_list(gpointer key, gpointer opaque)
296{
297 ARMCPU *cpu = opaque;
298 uint64_t regidx;
299 const ARMCPRegInfo *ri;
300
301 regidx = *(uint32_t *)key;
60322b39 302 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
721fae12 303
7a0e58fa 304 if (!(ri->type & (ARM_CP_NO_RAW|ARM_CP_ALIAS))) {
721fae12
PM
305 cpu->cpreg_indexes[cpu->cpreg_array_len] = cpreg_to_kvm_id(regidx);
306 /* The value array need not be initialized at this point */
307 cpu->cpreg_array_len++;
308 }
309}
310
311static void count_cpreg(gpointer key, gpointer opaque)
312{
313 ARMCPU *cpu = opaque;
314 uint64_t regidx;
315 const ARMCPRegInfo *ri;
316
317 regidx = *(uint32_t *)key;
60322b39 318 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
721fae12 319
7a0e58fa 320 if (!(ri->type & (ARM_CP_NO_RAW|ARM_CP_ALIAS))) {
721fae12
PM
321 cpu->cpreg_array_len++;
322 }
323}
324
325static gint cpreg_key_compare(gconstpointer a, gconstpointer b)
326{
cbf239b7
AR
327 uint64_t aidx = cpreg_to_kvm_id(*(uint32_t *)a);
328 uint64_t bidx = cpreg_to_kvm_id(*(uint32_t *)b);
721fae12 329
cbf239b7
AR
330 if (aidx > bidx) {
331 return 1;
332 }
333 if (aidx < bidx) {
334 return -1;
335 }
336 return 0;
721fae12
PM
337}
338
339void init_cpreg_list(ARMCPU *cpu)
340{
341 /* Initialise the cpreg_tuples[] array based on the cp_regs hash.
342 * Note that we require cpreg_tuples[] to be sorted by key ID.
343 */
57b6d95e 344 GList *keys;
721fae12
PM
345 int arraylen;
346
57b6d95e 347 keys = g_hash_table_get_keys(cpu->cp_regs);
721fae12
PM
348 keys = g_list_sort(keys, cpreg_key_compare);
349
350 cpu->cpreg_array_len = 0;
351
352 g_list_foreach(keys, count_cpreg, cpu);
353
354 arraylen = cpu->cpreg_array_len;
355 cpu->cpreg_indexes = g_new(uint64_t, arraylen);
356 cpu->cpreg_values = g_new(uint64_t, arraylen);
357 cpu->cpreg_vmstate_indexes = g_new(uint64_t, arraylen);
358 cpu->cpreg_vmstate_values = g_new(uint64_t, arraylen);
359 cpu->cpreg_vmstate_array_len = cpu->cpreg_array_len;
360 cpu->cpreg_array_len = 0;
361
362 g_list_foreach(keys, add_cpreg_to_list, cpu);
363
364 assert(cpu->cpreg_array_len == arraylen);
365
366 g_list_free(keys);
367}
368
68e9c2fe
EI
369/*
370 * Some registers are not accessible if EL3.NS=0 and EL3 is using AArch32 but
371 * they are accessible when EL3 is using AArch64 regardless of EL3.NS.
372 *
373 * access_el3_aa32ns: Used to check AArch32 register views.
374 * access_el3_aa32ns_aa64any: Used to check both AArch32/64 register views.
375 */
376static CPAccessResult access_el3_aa32ns(CPUARMState *env,
3f208fd7
PM
377 const ARMCPRegInfo *ri,
378 bool isread)
68e9c2fe
EI
379{
380 bool secure = arm_is_secure_below_el3(env);
381
382 assert(!arm_el_is_aa64(env, 3));
383 if (secure) {
384 return CP_ACCESS_TRAP_UNCATEGORIZED;
385 }
386 return CP_ACCESS_OK;
387}
388
389static CPAccessResult access_el3_aa32ns_aa64any(CPUARMState *env,
3f208fd7
PM
390 const ARMCPRegInfo *ri,
391 bool isread)
68e9c2fe
EI
392{
393 if (!arm_el_is_aa64(env, 3)) {
3f208fd7 394 return access_el3_aa32ns(env, ri, isread);
68e9c2fe
EI
395 }
396 return CP_ACCESS_OK;
397}
398
5513c3ab
PM
399/* Some secure-only AArch32 registers trap to EL3 if used from
400 * Secure EL1 (but are just ordinary UNDEF in other non-EL3 contexts).
401 * Note that an access from Secure EL1 can only happen if EL3 is AArch64.
402 * We assume that the .access field is set to PL1_RW.
403 */
404static CPAccessResult access_trap_aa32s_el1(CPUARMState *env,
3f208fd7
PM
405 const ARMCPRegInfo *ri,
406 bool isread)
5513c3ab
PM
407{
408 if (arm_current_el(env) == 3) {
409 return CP_ACCESS_OK;
410 }
411 if (arm_is_secure_below_el3(env)) {
412 return CP_ACCESS_TRAP_EL3;
413 }
414 /* This will be EL1 NS and EL2 NS, which just UNDEF */
415 return CP_ACCESS_TRAP_UNCATEGORIZED;
416}
417
187f678d
PM
418/* Check for traps to "powerdown debug" registers, which are controlled
419 * by MDCR.TDOSA
420 */
421static CPAccessResult access_tdosa(CPUARMState *env, const ARMCPRegInfo *ri,
422 bool isread)
423{
424 int el = arm_current_el(env);
425
426 if (el < 2 && (env->cp15.mdcr_el2 & MDCR_TDOSA)
427 && !arm_is_secure_below_el3(env)) {
428 return CP_ACCESS_TRAP_EL2;
429 }
430 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TDOSA)) {
431 return CP_ACCESS_TRAP_EL3;
432 }
433 return CP_ACCESS_OK;
434}
435
91b0a238
PM
436/* Check for traps to "debug ROM" registers, which are controlled
437 * by MDCR_EL2.TDRA for EL2 but by the more general MDCR_EL3.TDA for EL3.
438 */
439static CPAccessResult access_tdra(CPUARMState *env, const ARMCPRegInfo *ri,
440 bool isread)
441{
442 int el = arm_current_el(env);
443
444 if (el < 2 && (env->cp15.mdcr_el2 & MDCR_TDRA)
445 && !arm_is_secure_below_el3(env)) {
446 return CP_ACCESS_TRAP_EL2;
447 }
448 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TDA)) {
449 return CP_ACCESS_TRAP_EL3;
450 }
451 return CP_ACCESS_OK;
452}
453
d6c8cf81
PM
454/* Check for traps to general debug registers, which are controlled
455 * by MDCR_EL2.TDA for EL2 and MDCR_EL3.TDA for EL3.
456 */
457static CPAccessResult access_tda(CPUARMState *env, const ARMCPRegInfo *ri,
458 bool isread)
459{
460 int el = arm_current_el(env);
461
462 if (el < 2 && (env->cp15.mdcr_el2 & MDCR_TDA)
463 && !arm_is_secure_below_el3(env)) {
464 return CP_ACCESS_TRAP_EL2;
465 }
466 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TDA)) {
467 return CP_ACCESS_TRAP_EL3;
468 }
469 return CP_ACCESS_OK;
470}
471
1fce1ba9
PM
472/* Check for traps to performance monitor registers, which are controlled
473 * by MDCR_EL2.TPM for EL2 and MDCR_EL3.TPM for EL3.
474 */
475static CPAccessResult access_tpm(CPUARMState *env, const ARMCPRegInfo *ri,
476 bool isread)
477{
478 int el = arm_current_el(env);
479
480 if (el < 2 && (env->cp15.mdcr_el2 & MDCR_TPM)
481 && !arm_is_secure_below_el3(env)) {
482 return CP_ACCESS_TRAP_EL2;
483 }
484 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) {
485 return CP_ACCESS_TRAP_EL3;
486 }
487 return CP_ACCESS_OK;
488}
489
c4241c7d 490static void dacr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
c983fe6c 491{
00c8cb0a
AF
492 ARMCPU *cpu = arm_env_get_cpu(env);
493
8d5c773e 494 raw_write(env, ri, value);
d10eb08f 495 tlb_flush(CPU(cpu)); /* Flush TLB as domain not tracked in TLB */
c983fe6c
PM
496}
497
c4241c7d 498static void fcse_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
08de207b 499{
00c8cb0a
AF
500 ARMCPU *cpu = arm_env_get_cpu(env);
501
8d5c773e 502 if (raw_read(env, ri) != value) {
08de207b
PM
503 /* Unlike real hardware the qemu TLB uses virtual addresses,
504 * not modified virtual addresses, so this causes a TLB flush.
505 */
d10eb08f 506 tlb_flush(CPU(cpu));
8d5c773e 507 raw_write(env, ri, value);
08de207b 508 }
08de207b 509}
c4241c7d
PM
510
511static void contextidr_write(CPUARMState *env, const ARMCPRegInfo *ri,
512 uint64_t value)
08de207b 513{
00c8cb0a
AF
514 ARMCPU *cpu = arm_env_get_cpu(env);
515
452a0955 516 if (raw_read(env, ri) != value && !arm_feature(env, ARM_FEATURE_PMSA)
014406b5 517 && !extended_addresses_enabled(env)) {
08de207b
PM
518 /* For VMSA (when not using the LPAE long descriptor page table
519 * format) this register includes the ASID, so do a TLB flush.
520 * For PMSA it is purely a process ID and no action is needed.
521 */
d10eb08f 522 tlb_flush(CPU(cpu));
08de207b 523 }
8d5c773e 524 raw_write(env, ri, value);
08de207b
PM
525}
526
c4241c7d
PM
527static void tlbiall_write(CPUARMState *env, const ARMCPRegInfo *ri,
528 uint64_t value)
d929823f
PM
529{
530 /* Invalidate all (TLBIALL) */
00c8cb0a
AF
531 ARMCPU *cpu = arm_env_get_cpu(env);
532
d10eb08f 533 tlb_flush(CPU(cpu));
d929823f
PM
534}
535
c4241c7d
PM
536static void tlbimva_write(CPUARMState *env, const ARMCPRegInfo *ri,
537 uint64_t value)
d929823f
PM
538{
539 /* Invalidate single TLB entry by MVA and ASID (TLBIMVA) */
31b030d4
AF
540 ARMCPU *cpu = arm_env_get_cpu(env);
541
542 tlb_flush_page(CPU(cpu), value & TARGET_PAGE_MASK);
d929823f
PM
543}
544
c4241c7d
PM
545static void tlbiasid_write(CPUARMState *env, const ARMCPRegInfo *ri,
546 uint64_t value)
d929823f
PM
547{
548 /* Invalidate by ASID (TLBIASID) */
00c8cb0a
AF
549 ARMCPU *cpu = arm_env_get_cpu(env);
550
d10eb08f 551 tlb_flush(CPU(cpu));
d929823f
PM
552}
553
c4241c7d
PM
554static void tlbimvaa_write(CPUARMState *env, const ARMCPRegInfo *ri,
555 uint64_t value)
d929823f
PM
556{
557 /* Invalidate single entry by MVA, all ASIDs (TLBIMVAA) */
31b030d4
AF
558 ARMCPU *cpu = arm_env_get_cpu(env);
559
560 tlb_flush_page(CPU(cpu), value & TARGET_PAGE_MASK);
d929823f
PM
561}
562
fa439fc5
PM
563/* IS variants of TLB operations must affect all cores */
564static void tlbiall_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
565 uint64_t value)
566{
a67cf277 567 CPUState *cs = ENV_GET_CPU(env);
fa439fc5 568
a67cf277 569 tlb_flush_all_cpus_synced(cs);
fa439fc5
PM
570}
571
572static void tlbiasid_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
573 uint64_t value)
574{
a67cf277 575 CPUState *cs = ENV_GET_CPU(env);
fa439fc5 576
a67cf277 577 tlb_flush_all_cpus_synced(cs);
fa439fc5
PM
578}
579
580static void tlbimva_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
581 uint64_t value)
582{
a67cf277 583 CPUState *cs = ENV_GET_CPU(env);
fa439fc5 584
a67cf277 585 tlb_flush_page_all_cpus_synced(cs, value & TARGET_PAGE_MASK);
fa439fc5
PM
586}
587
588static void tlbimvaa_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
589 uint64_t value)
590{
a67cf277 591 CPUState *cs = ENV_GET_CPU(env);
fa439fc5 592
a67cf277 593 tlb_flush_page_all_cpus_synced(cs, value & TARGET_PAGE_MASK);
fa439fc5
PM
594}
595
541ef8c2
SS
596static void tlbiall_nsnh_write(CPUARMState *env, const ARMCPRegInfo *ri,
597 uint64_t value)
598{
599 CPUState *cs = ENV_GET_CPU(env);
600
0336cbf8 601 tlb_flush_by_mmuidx(cs,
8bd5c820
PM
602 ARMMMUIdxBit_S12NSE1 |
603 ARMMMUIdxBit_S12NSE0 |
604 ARMMMUIdxBit_S2NS);
541ef8c2
SS
605}
606
607static void tlbiall_nsnh_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
608 uint64_t value)
609{
a67cf277 610 CPUState *cs = ENV_GET_CPU(env);
541ef8c2 611
a67cf277 612 tlb_flush_by_mmuidx_all_cpus_synced(cs,
8bd5c820
PM
613 ARMMMUIdxBit_S12NSE1 |
614 ARMMMUIdxBit_S12NSE0 |
615 ARMMMUIdxBit_S2NS);
541ef8c2
SS
616}
617
618static void tlbiipas2_write(CPUARMState *env, const ARMCPRegInfo *ri,
619 uint64_t value)
620{
621 /* Invalidate by IPA. This has to invalidate any structures that
622 * contain only stage 2 translation information, but does not need
623 * to apply to structures that contain combined stage 1 and stage 2
624 * translation information.
625 * This must NOP if EL2 isn't implemented or SCR_EL3.NS is zero.
626 */
627 CPUState *cs = ENV_GET_CPU(env);
628 uint64_t pageaddr;
629
630 if (!arm_feature(env, ARM_FEATURE_EL2) || !(env->cp15.scr_el3 & SCR_NS)) {
631 return;
632 }
633
634 pageaddr = sextract64(value << 12, 0, 40);
635
8bd5c820 636 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_S2NS);
541ef8c2
SS
637}
638
639static void tlbiipas2_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
640 uint64_t value)
641{
a67cf277 642 CPUState *cs = ENV_GET_CPU(env);
541ef8c2
SS
643 uint64_t pageaddr;
644
645 if (!arm_feature(env, ARM_FEATURE_EL2) || !(env->cp15.scr_el3 & SCR_NS)) {
646 return;
647 }
648
649 pageaddr = sextract64(value << 12, 0, 40);
650
a67cf277 651 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
8bd5c820 652 ARMMMUIdxBit_S2NS);
541ef8c2
SS
653}
654
655static void tlbiall_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
656 uint64_t value)
657{
658 CPUState *cs = ENV_GET_CPU(env);
659
8bd5c820 660 tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_S1E2);
541ef8c2
SS
661}
662
663static void tlbiall_hyp_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
664 uint64_t value)
665{
a67cf277 666 CPUState *cs = ENV_GET_CPU(env);
541ef8c2 667
8bd5c820 668 tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_S1E2);
541ef8c2
SS
669}
670
671static void tlbimva_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
672 uint64_t value)
673{
674 CPUState *cs = ENV_GET_CPU(env);
675 uint64_t pageaddr = value & ~MAKE_64BIT_MASK(0, 12);
676
8bd5c820 677 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_S1E2);
541ef8c2
SS
678}
679
680static void tlbimva_hyp_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
681 uint64_t value)
682{
a67cf277 683 CPUState *cs = ENV_GET_CPU(env);
541ef8c2
SS
684 uint64_t pageaddr = value & ~MAKE_64BIT_MASK(0, 12);
685
a67cf277 686 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
8bd5c820 687 ARMMMUIdxBit_S1E2);
541ef8c2
SS
688}
689
e9aa6c21 690static const ARMCPRegInfo cp_reginfo[] = {
54bf36ed
FA
691 /* Define the secure and non-secure FCSE identifier CP registers
692 * separately because there is no secure bank in V8 (no _EL3). This allows
693 * the secure register to be properly reset and migrated. There is also no
694 * v8 EL1 version of the register so the non-secure instance stands alone.
695 */
696 { .name = "FCSEIDR(NS)",
697 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
698 .access = PL1_RW, .secure = ARM_CP_SECSTATE_NS,
699 .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_ns),
700 .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
701 { .name = "FCSEIDR(S)",
702 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
703 .access = PL1_RW, .secure = ARM_CP_SECSTATE_S,
704 .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_s),
d4e6df63 705 .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
54bf36ed
FA
706 /* Define the secure and non-secure context identifier CP registers
707 * separately because there is no secure bank in V8 (no _EL3). This allows
708 * the secure register to be properly reset and migrated. In the
709 * non-secure case, the 32-bit register will have reset and migration
710 * disabled during registration as it is handled by the 64-bit instance.
711 */
712 { .name = "CONTEXTIDR_EL1", .state = ARM_CP_STATE_BOTH,
014406b5 713 .opc0 = 3, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
54bf36ed
FA
714 .access = PL1_RW, .secure = ARM_CP_SECSTATE_NS,
715 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[1]),
716 .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
717 { .name = "CONTEXTIDR(S)", .state = ARM_CP_STATE_AA32,
718 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
719 .access = PL1_RW, .secure = ARM_CP_SECSTATE_S,
720 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_s),
d4e6df63 721 .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
9449fdf6
PM
722 REGINFO_SENTINEL
723};
724
725static const ARMCPRegInfo not_v8_cp_reginfo[] = {
726 /* NB: Some of these registers exist in v8 but with more precise
727 * definitions that don't use CP_ANY wildcards (mostly in v8_cp_reginfo[]).
728 */
729 /* MMU Domain access control / MPU write buffer control */
0c17d68c
FA
730 { .name = "DACR",
731 .cp = 15, .opc1 = CP_ANY, .crn = 3, .crm = CP_ANY, .opc2 = CP_ANY,
732 .access = PL1_RW, .resetvalue = 0,
733 .writefn = dacr_write, .raw_writefn = raw_write,
734 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
735 offsetoflow32(CPUARMState, cp15.dacr_ns) } },
a903c449
EI
736 /* ARMv7 allocates a range of implementation defined TLB LOCKDOWN regs.
737 * For v6 and v5, these mappings are overly broad.
4fdd17dd 738 */
a903c449
EI
739 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 0,
740 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
741 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 1,
742 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
743 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 4,
744 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
745 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 8,
4fdd17dd 746 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
c4804214
PM
747 /* Cache maintenance ops; some of this space may be overridden later. */
748 { .name = "CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
749 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
750 .type = ARM_CP_NOP | ARM_CP_OVERRIDE },
e9aa6c21
PM
751 REGINFO_SENTINEL
752};
753
7d57f408
PM
754static const ARMCPRegInfo not_v6_cp_reginfo[] = {
755 /* Not all pre-v6 cores implemented this WFI, so this is slightly
756 * over-broad.
757 */
758 { .name = "WFI_v5", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = 2,
759 .access = PL1_W, .type = ARM_CP_WFI },
760 REGINFO_SENTINEL
761};
762
763static const ARMCPRegInfo not_v7_cp_reginfo[] = {
764 /* Standard v6 WFI (also used in some pre-v6 cores); not in v7 (which
765 * is UNPREDICTABLE; we choose to NOP as most implementations do).
766 */
767 { .name = "WFI_v6", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
768 .access = PL1_W, .type = ARM_CP_WFI },
34f90529
PM
769 /* L1 cache lockdown. Not architectural in v6 and earlier but in practice
770 * implemented in 926, 946, 1026, 1136, 1176 and 11MPCore. StrongARM and
771 * OMAPCP will override this space.
772 */
773 { .name = "DLOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 0,
774 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_data),
775 .resetvalue = 0 },
776 { .name = "ILOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 1,
777 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_insn),
778 .resetvalue = 0 },
776d4e5c
PM
779 /* v6 doesn't have the cache ID registers but Linux reads them anyway */
780 { .name = "DUMMY", .cp = 15, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = CP_ANY,
7a0e58fa 781 .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 782 .resetvalue = 0 },
50300698
PM
783 /* We don't implement pre-v7 debug but most CPUs had at least a DBGDIDR;
784 * implementing it as RAZ means the "debug architecture version" bits
785 * will read as a reserved value, which should cause Linux to not try
786 * to use the debug hardware.
787 */
788 { .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0,
789 .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
995939a6
PM
790 /* MMU TLB control. Note that the wildcarding means we cover not just
791 * the unified TLB ops but also the dside/iside/inner-shareable variants.
792 */
793 { .name = "TLBIALL", .cp = 15, .crn = 8, .crm = CP_ANY,
794 .opc1 = CP_ANY, .opc2 = 0, .access = PL1_W, .writefn = tlbiall_write,
7a0e58fa 795 .type = ARM_CP_NO_RAW },
995939a6
PM
796 { .name = "TLBIMVA", .cp = 15, .crn = 8, .crm = CP_ANY,
797 .opc1 = CP_ANY, .opc2 = 1, .access = PL1_W, .writefn = tlbimva_write,
7a0e58fa 798 .type = ARM_CP_NO_RAW },
995939a6
PM
799 { .name = "TLBIASID", .cp = 15, .crn = 8, .crm = CP_ANY,
800 .opc1 = CP_ANY, .opc2 = 2, .access = PL1_W, .writefn = tlbiasid_write,
7a0e58fa 801 .type = ARM_CP_NO_RAW },
995939a6
PM
802 { .name = "TLBIMVAA", .cp = 15, .crn = 8, .crm = CP_ANY,
803 .opc1 = CP_ANY, .opc2 = 3, .access = PL1_W, .writefn = tlbimvaa_write,
7a0e58fa 804 .type = ARM_CP_NO_RAW },
a903c449
EI
805 { .name = "PRRR", .cp = 15, .crn = 10, .crm = 2,
806 .opc1 = 0, .opc2 = 0, .access = PL1_RW, .type = ARM_CP_NOP },
807 { .name = "NMRR", .cp = 15, .crn = 10, .crm = 2,
808 .opc1 = 0, .opc2 = 1, .access = PL1_RW, .type = ARM_CP_NOP },
7d57f408
PM
809 REGINFO_SENTINEL
810};
811
c4241c7d
PM
812static void cpacr_write(CPUARMState *env, const ARMCPRegInfo *ri,
813 uint64_t value)
2771db27 814{
f0aff255
FA
815 uint32_t mask = 0;
816
817 /* In ARMv8 most bits of CPACR_EL1 are RES0. */
818 if (!arm_feature(env, ARM_FEATURE_V8)) {
819 /* ARMv7 defines bits for unimplemented coprocessors as RAZ/WI.
820 * ASEDIS [31] and D32DIS [30] are both UNK/SBZP without VFP.
821 * TRCDIS [28] is RAZ/WI since we do not implement a trace macrocell.
822 */
823 if (arm_feature(env, ARM_FEATURE_VFP)) {
824 /* VFP coprocessor: cp10 & cp11 [23:20] */
825 mask |= (1 << 31) | (1 << 30) | (0xf << 20);
826
827 if (!arm_feature(env, ARM_FEATURE_NEON)) {
828 /* ASEDIS [31] bit is RAO/WI */
829 value |= (1 << 31);
830 }
831
832 /* VFPv3 and upwards with NEON implement 32 double precision
833 * registers (D0-D31).
834 */
835 if (!arm_feature(env, ARM_FEATURE_NEON) ||
836 !arm_feature(env, ARM_FEATURE_VFP3)) {
837 /* D32DIS [30] is RAO/WI if D16-31 are not implemented. */
838 value |= (1 << 30);
839 }
840 }
841 value &= mask;
2771db27 842 }
7ebd5f2e 843 env->cp15.cpacr_el1 = value;
2771db27
PM
844}
845
3f208fd7
PM
846static CPAccessResult cpacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
847 bool isread)
c6f19164
GB
848{
849 if (arm_feature(env, ARM_FEATURE_V8)) {
850 /* Check if CPACR accesses are to be trapped to EL2 */
851 if (arm_current_el(env) == 1 &&
852 (env->cp15.cptr_el[2] & CPTR_TCPAC) && !arm_is_secure(env)) {
853 return CP_ACCESS_TRAP_EL2;
854 /* Check if CPACR accesses are to be trapped to EL3 */
855 } else if (arm_current_el(env) < 3 &&
856 (env->cp15.cptr_el[3] & CPTR_TCPAC)) {
857 return CP_ACCESS_TRAP_EL3;
858 }
859 }
860
861 return CP_ACCESS_OK;
862}
863
3f208fd7
PM
864static CPAccessResult cptr_access(CPUARMState *env, const ARMCPRegInfo *ri,
865 bool isread)
c6f19164
GB
866{
867 /* Check if CPTR accesses are set to trap to EL3 */
868 if (arm_current_el(env) == 2 && (env->cp15.cptr_el[3] & CPTR_TCPAC)) {
869 return CP_ACCESS_TRAP_EL3;
870 }
871
872 return CP_ACCESS_OK;
873}
874
7d57f408
PM
875static const ARMCPRegInfo v6_cp_reginfo[] = {
876 /* prefetch by MVA in v6, NOP in v7 */
877 { .name = "MVA_prefetch",
878 .cp = 15, .crn = 7, .crm = 13, .opc1 = 0, .opc2 = 1,
879 .access = PL1_W, .type = ARM_CP_NOP },
6df99dec
SS
880 /* We need to break the TB after ISB to execute self-modifying code
881 * correctly and also to take any pending interrupts immediately.
882 * So use arm_cp_write_ignore() function instead of ARM_CP_NOP flag.
883 */
7d57f408 884 { .name = "ISB", .cp = 15, .crn = 7, .crm = 5, .opc1 = 0, .opc2 = 4,
6df99dec 885 .access = PL0_W, .type = ARM_CP_NO_RAW, .writefn = arm_cp_write_ignore },
091fd17c 886 { .name = "DSB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 4,
7d57f408 887 .access = PL0_W, .type = ARM_CP_NOP },
091fd17c 888 { .name = "DMB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 5,
7d57f408 889 .access = PL0_W, .type = ARM_CP_NOP },
06d76f31 890 { .name = "IFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 2,
6cd8a264 891 .access = PL1_RW,
b848ce2b
FA
892 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ifar_s),
893 offsetof(CPUARMState, cp15.ifar_ns) },
06d76f31
PM
894 .resetvalue = 0, },
895 /* Watchpoint Fault Address Register : should actually only be present
896 * for 1136, 1176, 11MPCore.
897 */
898 { .name = "WFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 1,
899 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0, },
34222fb8 900 { .name = "CPACR", .state = ARM_CP_STATE_BOTH, .opc0 = 3,
c6f19164 901 .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 2, .accessfn = cpacr_access,
7ebd5f2e 902 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.cpacr_el1),
2771db27 903 .resetvalue = 0, .writefn = cpacr_write },
7d57f408
PM
904 REGINFO_SENTINEL
905};
906
3f208fd7
PM
907static CPAccessResult pmreg_access(CPUARMState *env, const ARMCPRegInfo *ri,
908 bool isread)
200ac0ef 909{
3b163b01 910 /* Performance monitor registers user accessibility is controlled
1fce1ba9
PM
911 * by PMUSERENR. MDCR_EL2.TPM and MDCR_EL3.TPM allow configurable
912 * trapping to EL2 or EL3 for other accesses.
200ac0ef 913 */
1fce1ba9
PM
914 int el = arm_current_el(env);
915
6ecd0b6b 916 if (el == 0 && !(env->cp15.c9_pmuserenr & 1)) {
fcd25206 917 return CP_ACCESS_TRAP;
200ac0ef 918 }
1fce1ba9
PM
919 if (el < 2 && (env->cp15.mdcr_el2 & MDCR_TPM)
920 && !arm_is_secure_below_el3(env)) {
921 return CP_ACCESS_TRAP_EL2;
922 }
923 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) {
924 return CP_ACCESS_TRAP_EL3;
925 }
926
fcd25206 927 return CP_ACCESS_OK;
200ac0ef
PM
928}
929
6ecd0b6b
AB
930static CPAccessResult pmreg_access_xevcntr(CPUARMState *env,
931 const ARMCPRegInfo *ri,
932 bool isread)
933{
934 /* ER: event counter read trap control */
935 if (arm_feature(env, ARM_FEATURE_V8)
936 && arm_current_el(env) == 0
937 && (env->cp15.c9_pmuserenr & (1 << 3)) != 0
938 && isread) {
939 return CP_ACCESS_OK;
940 }
941
942 return pmreg_access(env, ri, isread);
943}
944
945static CPAccessResult pmreg_access_swinc(CPUARMState *env,
946 const ARMCPRegInfo *ri,
947 bool isread)
948{
949 /* SW: software increment write trap control */
950 if (arm_feature(env, ARM_FEATURE_V8)
951 && arm_current_el(env) == 0
952 && (env->cp15.c9_pmuserenr & (1 << 1)) != 0
953 && !isread) {
954 return CP_ACCESS_OK;
955 }
956
957 return pmreg_access(env, ri, isread);
958}
959
7c2cb42b 960#ifndef CONFIG_USER_ONLY
87124fde 961
6ecd0b6b
AB
962static CPAccessResult pmreg_access_selr(CPUARMState *env,
963 const ARMCPRegInfo *ri,
964 bool isread)
965{
966 /* ER: event counter read trap control */
967 if (arm_feature(env, ARM_FEATURE_V8)
968 && arm_current_el(env) == 0
969 && (env->cp15.c9_pmuserenr & (1 << 3)) != 0) {
970 return CP_ACCESS_OK;
971 }
972
973 return pmreg_access(env, ri, isread);
974}
975
976static CPAccessResult pmreg_access_ccntr(CPUARMState *env,
977 const ARMCPRegInfo *ri,
978 bool isread)
979{
980 /* CR: cycle counter read trap control */
981 if (arm_feature(env, ARM_FEATURE_V8)
982 && arm_current_el(env) == 0
983 && (env->cp15.c9_pmuserenr & (1 << 2)) != 0
984 && isread) {
985 return CP_ACCESS_OK;
986 }
987
988 return pmreg_access(env, ri, isread);
989}
990
87124fde
AF
991static inline bool arm_ccnt_enabled(CPUARMState *env)
992{
993 /* This does not support checking PMCCFILTR_EL0 register */
994
995 if (!(env->cp15.c9_pmcr & PMCRE)) {
996 return false;
997 }
998
999 return true;
1000}
1001
ec7b4ce4
AF
1002void pmccntr_sync(CPUARMState *env)
1003{
1004 uint64_t temp_ticks;
1005
352c98e5
LV
1006 temp_ticks = muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
1007 ARM_CPU_FREQ, NANOSECONDS_PER_SECOND);
ec7b4ce4
AF
1008
1009 if (env->cp15.c9_pmcr & PMCRD) {
1010 /* Increment once every 64 processor clock cycles */
1011 temp_ticks /= 64;
1012 }
1013
1014 if (arm_ccnt_enabled(env)) {
1015 env->cp15.c15_ccnt = temp_ticks - env->cp15.c15_ccnt;
1016 }
1017}
1018
c4241c7d
PM
1019static void pmcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1020 uint64_t value)
200ac0ef 1021{
942a155b 1022 pmccntr_sync(env);
7c2cb42b
AF
1023
1024 if (value & PMCRC) {
1025 /* The counter has been reset */
1026 env->cp15.c15_ccnt = 0;
1027 }
1028
200ac0ef
PM
1029 /* only the DP, X, D and E bits are writable */
1030 env->cp15.c9_pmcr &= ~0x39;
1031 env->cp15.c9_pmcr |= (value & 0x39);
7c2cb42b 1032
942a155b 1033 pmccntr_sync(env);
7c2cb42b
AF
1034}
1035
1036static uint64_t pmccntr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1037{
c92c0687 1038 uint64_t total_ticks;
7c2cb42b 1039
942a155b 1040 if (!arm_ccnt_enabled(env)) {
7c2cb42b
AF
1041 /* Counter is disabled, do not change value */
1042 return env->cp15.c15_ccnt;
1043 }
1044
352c98e5
LV
1045 total_ticks = muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
1046 ARM_CPU_FREQ, NANOSECONDS_PER_SECOND);
7c2cb42b
AF
1047
1048 if (env->cp15.c9_pmcr & PMCRD) {
1049 /* Increment once every 64 processor clock cycles */
1050 total_ticks /= 64;
1051 }
1052 return total_ticks - env->cp15.c15_ccnt;
1053}
1054
6b040780
WH
1055static void pmselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1056 uint64_t value)
1057{
1058 /* The value of PMSELR.SEL affects the behavior of PMXEVTYPER and
1059 * PMXEVCNTR. We allow [0..31] to be written to PMSELR here; in the
1060 * meanwhile, we check PMSELR.SEL when PMXEVTYPER and PMXEVCNTR are
1061 * accessed.
1062 */
1063 env->cp15.c9_pmselr = value & 0x1f;
1064}
1065
7c2cb42b
AF
1066static void pmccntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1067 uint64_t value)
1068{
c92c0687 1069 uint64_t total_ticks;
7c2cb42b 1070
942a155b 1071 if (!arm_ccnt_enabled(env)) {
7c2cb42b
AF
1072 /* Counter is disabled, set the absolute value */
1073 env->cp15.c15_ccnt = value;
1074 return;
1075 }
1076
352c98e5
LV
1077 total_ticks = muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
1078 ARM_CPU_FREQ, NANOSECONDS_PER_SECOND);
7c2cb42b
AF
1079
1080 if (env->cp15.c9_pmcr & PMCRD) {
1081 /* Increment once every 64 processor clock cycles */
1082 total_ticks /= 64;
1083 }
1084 env->cp15.c15_ccnt = total_ticks - value;
200ac0ef 1085}
421c7ebd
PC
1086
1087static void pmccntr_write32(CPUARMState *env, const ARMCPRegInfo *ri,
1088 uint64_t value)
1089{
1090 uint64_t cur_val = pmccntr_read(env, NULL);
1091
1092 pmccntr_write(env, ri, deposit64(cur_val, 0, 32, value));
1093}
1094
ec7b4ce4
AF
1095#else /* CONFIG_USER_ONLY */
1096
1097void pmccntr_sync(CPUARMState *env)
1098{
1099}
1100
7c2cb42b 1101#endif
200ac0ef 1102
0614601c
AF
1103static void pmccfiltr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1104 uint64_t value)
1105{
1106 pmccntr_sync(env);
1107 env->cp15.pmccfiltr_el0 = value & 0x7E000000;
1108 pmccntr_sync(env);
1109}
1110
c4241c7d 1111static void pmcntenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
200ac0ef
PM
1112 uint64_t value)
1113{
200ac0ef
PM
1114 value &= (1 << 31);
1115 env->cp15.c9_pmcnten |= value;
200ac0ef
PM
1116}
1117
c4241c7d
PM
1118static void pmcntenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1119 uint64_t value)
200ac0ef 1120{
200ac0ef
PM
1121 value &= (1 << 31);
1122 env->cp15.c9_pmcnten &= ~value;
200ac0ef
PM
1123}
1124
c4241c7d
PM
1125static void pmovsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1126 uint64_t value)
200ac0ef 1127{
200ac0ef 1128 env->cp15.c9_pmovsr &= ~value;
200ac0ef
PM
1129}
1130
c4241c7d
PM
1131static void pmxevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
1132 uint64_t value)
200ac0ef 1133{
fdb86656
WH
1134 /* Attempts to access PMXEVTYPER are CONSTRAINED UNPREDICTABLE when
1135 * PMSELR value is equal to or greater than the number of implemented
1136 * counters, but not equal to 0x1f. We opt to behave as a RAZ/WI.
1137 */
1138 if (env->cp15.c9_pmselr == 0x1f) {
1139 pmccfiltr_write(env, ri, value);
1140 }
1141}
1142
1143static uint64_t pmxevtyper_read(CPUARMState *env, const ARMCPRegInfo *ri)
1144{
1145 /* We opt to behave as a RAZ/WI when attempts to access PMXEVTYPER
1146 * are CONSTRAINED UNPREDICTABLE. See comments in pmxevtyper_write().
1147 */
1148 if (env->cp15.c9_pmselr == 0x1f) {
1149 return env->cp15.pmccfiltr_el0;
1150 } else {
1151 return 0;
1152 }
200ac0ef
PM
1153}
1154
c4241c7d 1155static void pmuserenr_write(CPUARMState *env, const ARMCPRegInfo *ri,
200ac0ef
PM
1156 uint64_t value)
1157{
6ecd0b6b
AB
1158 if (arm_feature(env, ARM_FEATURE_V8)) {
1159 env->cp15.c9_pmuserenr = value & 0xf;
1160 } else {
1161 env->cp15.c9_pmuserenr = value & 1;
1162 }
200ac0ef
PM
1163}
1164
c4241c7d
PM
1165static void pmintenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1166 uint64_t value)
200ac0ef
PM
1167{
1168 /* We have no event counters so only the C bit can be changed */
1169 value &= (1 << 31);
1170 env->cp15.c9_pminten |= value;
200ac0ef
PM
1171}
1172
c4241c7d
PM
1173static void pmintenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1174 uint64_t value)
200ac0ef
PM
1175{
1176 value &= (1 << 31);
1177 env->cp15.c9_pminten &= ~value;
200ac0ef
PM
1178}
1179
c4241c7d
PM
1180static void vbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
1181 uint64_t value)
8641136c 1182{
a505d7fe
PM
1183 /* Note that even though the AArch64 view of this register has bits
1184 * [10:0] all RES0 we can only mask the bottom 5, to comply with the
1185 * architectural requirements for bits which are RES0 only in some
1186 * contexts. (ARMv8 would permit us to do no masking at all, but ARMv7
1187 * requires the bottom five bits to be RAZ/WI because they're UNK/SBZP.)
1188 */
855ea66d 1189 raw_write(env, ri, value & ~0x1FULL);
8641136c
NR
1190}
1191
64e0e2de
EI
1192static void scr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
1193{
1194 /* We only mask off bits that are RES0 both for AArch64 and AArch32.
1195 * For bits that vary between AArch32/64, code needs to check the
1196 * current execution mode before directly using the feature bit.
1197 */
1198 uint32_t valid_mask = SCR_AARCH64_MASK | SCR_AARCH32_MASK;
1199
1200 if (!arm_feature(env, ARM_FEATURE_EL2)) {
1201 valid_mask &= ~SCR_HCE;
1202
1203 /* On ARMv7, SMD (or SCD as it is called in v7) is only
1204 * supported if EL2 exists. The bit is UNK/SBZP when
1205 * EL2 is unavailable. In QEMU ARMv7, we force it to always zero
1206 * when EL2 is unavailable.
4eb27640 1207 * On ARMv8, this bit is always available.
64e0e2de 1208 */
4eb27640
GB
1209 if (arm_feature(env, ARM_FEATURE_V7) &&
1210 !arm_feature(env, ARM_FEATURE_V8)) {
64e0e2de
EI
1211 valid_mask &= ~SCR_SMD;
1212 }
1213 }
1214
1215 /* Clear all-context RES0 bits. */
1216 value &= valid_mask;
1217 raw_write(env, ri, value);
1218}
1219
c4241c7d 1220static uint64_t ccsidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
776d4e5c
PM
1221{
1222 ARMCPU *cpu = arm_env_get_cpu(env);
b85a1fd6
FA
1223
1224 /* Acquire the CSSELR index from the bank corresponding to the CCSIDR
1225 * bank
1226 */
1227 uint32_t index = A32_BANKED_REG_GET(env, csselr,
1228 ri->secure & ARM_CP_SECSTATE_S);
1229
1230 return cpu->ccsidr[index];
776d4e5c
PM
1231}
1232
c4241c7d
PM
1233static void csselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1234 uint64_t value)
776d4e5c 1235{
8d5c773e 1236 raw_write(env, ri, value & 0xf);
776d4e5c
PM
1237}
1238
1090b9c6
PM
1239static uint64_t isr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1240{
1241 CPUState *cs = ENV_GET_CPU(env);
1242 uint64_t ret = 0;
1243
1244 if (cs->interrupt_request & CPU_INTERRUPT_HARD) {
1245 ret |= CPSR_I;
1246 }
1247 if (cs->interrupt_request & CPU_INTERRUPT_FIQ) {
1248 ret |= CPSR_F;
1249 }
1250 /* External aborts are not possible in QEMU so A bit is always clear */
1251 return ret;
1252}
1253
e9aa6c21 1254static const ARMCPRegInfo v7_cp_reginfo[] = {
7d57f408
PM
1255 /* the old v6 WFI, UNPREDICTABLE in v7 but we choose to NOP */
1256 { .name = "NOP", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
1257 .access = PL1_W, .type = ARM_CP_NOP },
200ac0ef
PM
1258 /* Performance monitors are implementation defined in v7,
1259 * but with an ARM recommended set of registers, which we
1260 * follow (although we don't actually implement any counters)
1261 *
1262 * Performance registers fall into three categories:
1263 * (a) always UNDEF in PL0, RW in PL1 (PMINTENSET, PMINTENCLR)
1264 * (b) RO in PL0 (ie UNDEF on write), RW in PL1 (PMUSERENR)
1265 * (c) UNDEF in PL0 if PMUSERENR.EN==0, otherwise accessible (all others)
1266 * For the cases controlled by PMUSERENR we must set .access to PL0_RW
1267 * or PL0_RO as appropriate and then check PMUSERENR in the helper fn.
1268 */
1269 { .name = "PMCNTENSET", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 1,
7a0e58fa 1270 .access = PL0_RW, .type = ARM_CP_ALIAS,
8521466b 1271 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
fcd25206
PM
1272 .writefn = pmcntenset_write,
1273 .accessfn = pmreg_access,
1274 .raw_writefn = raw_write },
8521466b
AF
1275 { .name = "PMCNTENSET_EL0", .state = ARM_CP_STATE_AA64,
1276 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 1,
1277 .access = PL0_RW, .accessfn = pmreg_access,
1278 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten), .resetvalue = 0,
1279 .writefn = pmcntenset_write, .raw_writefn = raw_write },
200ac0ef 1280 { .name = "PMCNTENCLR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 2,
8521466b
AF
1281 .access = PL0_RW,
1282 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
fcd25206
PM
1283 .accessfn = pmreg_access,
1284 .writefn = pmcntenclr_write,
7a0e58fa 1285 .type = ARM_CP_ALIAS },
8521466b
AF
1286 { .name = "PMCNTENCLR_EL0", .state = ARM_CP_STATE_AA64,
1287 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 2,
1288 .access = PL0_RW, .accessfn = pmreg_access,
7a0e58fa 1289 .type = ARM_CP_ALIAS,
8521466b
AF
1290 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten),
1291 .writefn = pmcntenclr_write },
200ac0ef
PM
1292 { .name = "PMOVSR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 3,
1293 .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
fcd25206
PM
1294 .accessfn = pmreg_access,
1295 .writefn = pmovsr_write,
1296 .raw_writefn = raw_write },
978364f1
AF
1297 { .name = "PMOVSCLR_EL0", .state = ARM_CP_STATE_AA64,
1298 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 3,
1299 .access = PL0_RW, .accessfn = pmreg_access,
1300 .type = ARM_CP_ALIAS,
1301 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
1302 .writefn = pmovsr_write,
1303 .raw_writefn = raw_write },
fcd25206 1304 /* Unimplemented so WI. */
200ac0ef 1305 { .name = "PMSWINC", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 4,
6ecd0b6b 1306 .access = PL0_W, .accessfn = pmreg_access_swinc, .type = ARM_CP_NOP },
7c2cb42b 1307#ifndef CONFIG_USER_ONLY
6b040780
WH
1308 { .name = "PMSELR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 5,
1309 .access = PL0_RW, .type = ARM_CP_ALIAS,
1310 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmselr),
6ecd0b6b 1311 .accessfn = pmreg_access_selr, .writefn = pmselr_write,
6b040780
WH
1312 .raw_writefn = raw_write},
1313 { .name = "PMSELR_EL0", .state = ARM_CP_STATE_AA64,
1314 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 5,
6ecd0b6b 1315 .access = PL0_RW, .accessfn = pmreg_access_selr,
6b040780
WH
1316 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmselr),
1317 .writefn = pmselr_write, .raw_writefn = raw_write, },
200ac0ef 1318 { .name = "PMCCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 0,
7c2cb42b 1319 .access = PL0_RW, .resetvalue = 0, .type = ARM_CP_IO,
421c7ebd 1320 .readfn = pmccntr_read, .writefn = pmccntr_write32,
6ecd0b6b 1321 .accessfn = pmreg_access_ccntr },
8521466b
AF
1322 { .name = "PMCCNTR_EL0", .state = ARM_CP_STATE_AA64,
1323 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 0,
6ecd0b6b 1324 .access = PL0_RW, .accessfn = pmreg_access_ccntr,
8521466b
AF
1325 .type = ARM_CP_IO,
1326 .readfn = pmccntr_read, .writefn = pmccntr_write, },
7c2cb42b 1327#endif
8521466b
AF
1328 { .name = "PMCCFILTR_EL0", .state = ARM_CP_STATE_AA64,
1329 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 15, .opc2 = 7,
0614601c 1330 .writefn = pmccfiltr_write,
8521466b
AF
1331 .access = PL0_RW, .accessfn = pmreg_access,
1332 .type = ARM_CP_IO,
1333 .fieldoffset = offsetof(CPUARMState, cp15.pmccfiltr_el0),
1334 .resetvalue = 0, },
200ac0ef 1335 { .name = "PMXEVTYPER", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 1,
fdb86656
WH
1336 .access = PL0_RW, .type = ARM_CP_NO_RAW, .accessfn = pmreg_access,
1337 .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
1338 { .name = "PMXEVTYPER_EL0", .state = ARM_CP_STATE_AA64,
1339 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 1,
1340 .access = PL0_RW, .type = ARM_CP_NO_RAW, .accessfn = pmreg_access,
1341 .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
fcd25206 1342 /* Unimplemented, RAZ/WI. */
200ac0ef 1343 { .name = "PMXEVCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 2,
fcd25206 1344 .access = PL0_RW, .type = ARM_CP_CONST, .resetvalue = 0,
6ecd0b6b 1345 .accessfn = pmreg_access_xevcntr },
200ac0ef 1346 { .name = "PMUSERENR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 0,
1fce1ba9 1347 .access = PL0_R | PL1_RW, .accessfn = access_tpm,
200ac0ef
PM
1348 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmuserenr),
1349 .resetvalue = 0,
d4e6df63 1350 .writefn = pmuserenr_write, .raw_writefn = raw_write },
8a83ffc2
AF
1351 { .name = "PMUSERENR_EL0", .state = ARM_CP_STATE_AA64,
1352 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 0,
1fce1ba9 1353 .access = PL0_R | PL1_RW, .accessfn = access_tpm, .type = ARM_CP_ALIAS,
8a83ffc2
AF
1354 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmuserenr),
1355 .resetvalue = 0,
1356 .writefn = pmuserenr_write, .raw_writefn = raw_write },
200ac0ef 1357 { .name = "PMINTENSET", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 1,
1fce1ba9 1358 .access = PL1_RW, .accessfn = access_tpm,
e6ec5457
WH
1359 .type = ARM_CP_ALIAS,
1360 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pminten),
200ac0ef 1361 .resetvalue = 0,
d4e6df63 1362 .writefn = pmintenset_write, .raw_writefn = raw_write },
e6ec5457
WH
1363 { .name = "PMINTENSET_EL1", .state = ARM_CP_STATE_AA64,
1364 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 1,
1365 .access = PL1_RW, .accessfn = access_tpm,
1366 .type = ARM_CP_IO,
1367 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
1368 .writefn = pmintenset_write, .raw_writefn = raw_write,
1369 .resetvalue = 0x0 },
200ac0ef 1370 { .name = "PMINTENCLR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 2,
1fce1ba9 1371 .access = PL1_RW, .accessfn = access_tpm, .type = ARM_CP_ALIAS,
200ac0ef 1372 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
b061a82b 1373 .writefn = pmintenclr_write, },
978364f1
AF
1374 { .name = "PMINTENCLR_EL1", .state = ARM_CP_STATE_AA64,
1375 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 2,
1fce1ba9 1376 .access = PL1_RW, .accessfn = access_tpm, .type = ARM_CP_ALIAS,
978364f1
AF
1377 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
1378 .writefn = pmintenclr_write },
7da845b0
PM
1379 { .name = "CCSIDR", .state = ARM_CP_STATE_BOTH,
1380 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 0,
7a0e58fa 1381 .access = PL1_R, .readfn = ccsidr_read, .type = ARM_CP_NO_RAW },
7da845b0
PM
1382 { .name = "CSSELR", .state = ARM_CP_STATE_BOTH,
1383 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 2, .opc2 = 0,
b85a1fd6
FA
1384 .access = PL1_RW, .writefn = csselr_write, .resetvalue = 0,
1385 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.csselr_s),
1386 offsetof(CPUARMState, cp15.csselr_ns) } },
776d4e5c
PM
1387 /* Auxiliary ID register: this actually has an IMPDEF value but for now
1388 * just RAZ for all cores:
1389 */
0ff644a7
PM
1390 { .name = "AIDR", .state = ARM_CP_STATE_BOTH,
1391 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 7,
776d4e5c 1392 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
f32cdad5
PM
1393 /* Auxiliary fault status registers: these also are IMPDEF, and we
1394 * choose to RAZ/WI for all cores.
1395 */
1396 { .name = "AFSR0_EL1", .state = ARM_CP_STATE_BOTH,
1397 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 0,
1398 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
1399 { .name = "AFSR1_EL1", .state = ARM_CP_STATE_BOTH,
1400 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 1,
1401 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
b0fe2427
PM
1402 /* MAIR can just read-as-written because we don't implement caches
1403 * and so don't need to care about memory attributes.
1404 */
1405 { .name = "MAIR_EL1", .state = ARM_CP_STATE_AA64,
1406 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
be693c87 1407 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[1]),
b0fe2427 1408 .resetvalue = 0 },
4cfb8ad8
PM
1409 { .name = "MAIR_EL3", .state = ARM_CP_STATE_AA64,
1410 .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 2, .opc2 = 0,
1411 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[3]),
1412 .resetvalue = 0 },
b0fe2427
PM
1413 /* For non-long-descriptor page tables these are PRRR and NMRR;
1414 * regardless they still act as reads-as-written for QEMU.
b0fe2427 1415 */
1281f8e3 1416 /* MAIR0/1 are defined separately from their 64-bit counterpart which
be693c87
GB
1417 * allows them to assign the correct fieldoffset based on the endianness
1418 * handled in the field definitions.
1419 */
a903c449 1420 { .name = "MAIR0", .state = ARM_CP_STATE_AA32,
b0fe2427 1421 .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0, .access = PL1_RW,
be693c87
GB
1422 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair0_s),
1423 offsetof(CPUARMState, cp15.mair0_ns) },
b0fe2427 1424 .resetfn = arm_cp_reset_ignore },
a903c449 1425 { .name = "MAIR1", .state = ARM_CP_STATE_AA32,
b0fe2427 1426 .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 1, .access = PL1_RW,
be693c87
GB
1427 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair1_s),
1428 offsetof(CPUARMState, cp15.mair1_ns) },
b0fe2427 1429 .resetfn = arm_cp_reset_ignore },
1090b9c6
PM
1430 { .name = "ISR_EL1", .state = ARM_CP_STATE_BOTH,
1431 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 0,
7a0e58fa 1432 .type = ARM_CP_NO_RAW, .access = PL1_R, .readfn = isr_read },
995939a6
PM
1433 /* 32 bit ITLB invalidates */
1434 { .name = "ITLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 0,
7a0e58fa 1435 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_write },
995939a6 1436 { .name = "ITLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 1,
7a0e58fa 1437 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write },
995939a6 1438 { .name = "ITLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 2,
7a0e58fa 1439 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiasid_write },
995939a6
PM
1440 /* 32 bit DTLB invalidates */
1441 { .name = "DTLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 0,
7a0e58fa 1442 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_write },
995939a6 1443 { .name = "DTLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 1,
7a0e58fa 1444 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write },
995939a6 1445 { .name = "DTLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 2,
7a0e58fa 1446 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiasid_write },
995939a6
PM
1447 /* 32 bit TLB invalidates */
1448 { .name = "TLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
7a0e58fa 1449 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_write },
995939a6 1450 { .name = "TLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
7a0e58fa 1451 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write },
995939a6 1452 { .name = "TLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
7a0e58fa 1453 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiasid_write },
995939a6 1454 { .name = "TLBIMVAA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
7a0e58fa 1455 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimvaa_write },
995939a6
PM
1456 REGINFO_SENTINEL
1457};
1458
1459static const ARMCPRegInfo v7mp_cp_reginfo[] = {
1460 /* 32 bit TLB invalidates, Inner Shareable */
1461 { .name = "TLBIALLIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
7a0e58fa 1462 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_is_write },
995939a6 1463 { .name = "TLBIMVAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
7a0e58fa 1464 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_is_write },
995939a6 1465 { .name = "TLBIASIDIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
7a0e58fa 1466 .type = ARM_CP_NO_RAW, .access = PL1_W,
fa439fc5 1467 .writefn = tlbiasid_is_write },
995939a6 1468 { .name = "TLBIMVAAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
7a0e58fa 1469 .type = ARM_CP_NO_RAW, .access = PL1_W,
fa439fc5 1470 .writefn = tlbimvaa_is_write },
e9aa6c21
PM
1471 REGINFO_SENTINEL
1472};
1473
c4241c7d
PM
1474static void teecr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1475 uint64_t value)
c326b979
PM
1476{
1477 value &= 1;
1478 env->teecr = value;
c326b979
PM
1479}
1480
3f208fd7
PM
1481static CPAccessResult teehbr_access(CPUARMState *env, const ARMCPRegInfo *ri,
1482 bool isread)
c326b979 1483{
dcbff19b 1484 if (arm_current_el(env) == 0 && (env->teecr & 1)) {
92611c00 1485 return CP_ACCESS_TRAP;
c326b979 1486 }
92611c00 1487 return CP_ACCESS_OK;
c326b979
PM
1488}
1489
1490static const ARMCPRegInfo t2ee_cp_reginfo[] = {
1491 { .name = "TEECR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 6, .opc2 = 0,
1492 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, teecr),
1493 .resetvalue = 0,
1494 .writefn = teecr_write },
1495 { .name = "TEEHBR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 6, .opc2 = 0,
1496 .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, teehbr),
92611c00 1497 .accessfn = teehbr_access, .resetvalue = 0 },
c326b979
PM
1498 REGINFO_SENTINEL
1499};
1500
4d31c596 1501static const ARMCPRegInfo v6k_cp_reginfo[] = {
e4fe830b
PM
1502 { .name = "TPIDR_EL0", .state = ARM_CP_STATE_AA64,
1503 .opc0 = 3, .opc1 = 3, .opc2 = 2, .crn = 13, .crm = 0,
1504 .access = PL0_RW,
54bf36ed 1505 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[0]), .resetvalue = 0 },
4d31c596
PM
1506 { .name = "TPIDRURW", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 2,
1507 .access = PL0_RW,
54bf36ed
FA
1508 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrurw_s),
1509 offsetoflow32(CPUARMState, cp15.tpidrurw_ns) },
e4fe830b
PM
1510 .resetfn = arm_cp_reset_ignore },
1511 { .name = "TPIDRRO_EL0", .state = ARM_CP_STATE_AA64,
1512 .opc0 = 3, .opc1 = 3, .opc2 = 3, .crn = 13, .crm = 0,
1513 .access = PL0_R|PL1_W,
54bf36ed
FA
1514 .fieldoffset = offsetof(CPUARMState, cp15.tpidrro_el[0]),
1515 .resetvalue = 0},
4d31c596
PM
1516 { .name = "TPIDRURO", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 3,
1517 .access = PL0_R|PL1_W,
54bf36ed
FA
1518 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidruro_s),
1519 offsetoflow32(CPUARMState, cp15.tpidruro_ns) },
e4fe830b 1520 .resetfn = arm_cp_reset_ignore },
54bf36ed 1521 { .name = "TPIDR_EL1", .state = ARM_CP_STATE_AA64,
e4fe830b 1522 .opc0 = 3, .opc1 = 0, .opc2 = 4, .crn = 13, .crm = 0,
4d31c596 1523 .access = PL1_RW,
54bf36ed
FA
1524 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[1]), .resetvalue = 0 },
1525 { .name = "TPIDRPRW", .opc1 = 0, .cp = 15, .crn = 13, .crm = 0, .opc2 = 4,
1526 .access = PL1_RW,
1527 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrprw_s),
1528 offsetoflow32(CPUARMState, cp15.tpidrprw_ns) },
1529 .resetvalue = 0 },
4d31c596
PM
1530 REGINFO_SENTINEL
1531};
1532
55d284af
PM
1533#ifndef CONFIG_USER_ONLY
1534
3f208fd7
PM
1535static CPAccessResult gt_cntfrq_access(CPUARMState *env, const ARMCPRegInfo *ri,
1536 bool isread)
00108f2d 1537{
75502672
PM
1538 /* CNTFRQ: not visible from PL0 if both PL0PCTEN and PL0VCTEN are zero.
1539 * Writable only at the highest implemented exception level.
1540 */
1541 int el = arm_current_el(env);
1542
1543 switch (el) {
1544 case 0:
1545 if (!extract32(env->cp15.c14_cntkctl, 0, 2)) {
1546 return CP_ACCESS_TRAP;
1547 }
1548 break;
1549 case 1:
1550 if (!isread && ri->state == ARM_CP_STATE_AA32 &&
1551 arm_is_secure_below_el3(env)) {
1552 /* Accesses from 32-bit Secure EL1 UNDEF (*not* trap to EL3!) */
1553 return CP_ACCESS_TRAP_UNCATEGORIZED;
1554 }
1555 break;
1556 case 2:
1557 case 3:
1558 break;
00108f2d 1559 }
75502672
PM
1560
1561 if (!isread && el < arm_highest_el(env)) {
1562 return CP_ACCESS_TRAP_UNCATEGORIZED;
1563 }
1564
00108f2d
PM
1565 return CP_ACCESS_OK;
1566}
1567
3f208fd7
PM
1568static CPAccessResult gt_counter_access(CPUARMState *env, int timeridx,
1569 bool isread)
00108f2d 1570{
0b6440af
EI
1571 unsigned int cur_el = arm_current_el(env);
1572 bool secure = arm_is_secure(env);
1573
00108f2d 1574 /* CNT[PV]CT: not visible from PL0 if ELO[PV]CTEN is zero */
0b6440af 1575 if (cur_el == 0 &&
00108f2d
PM
1576 !extract32(env->cp15.c14_cntkctl, timeridx, 1)) {
1577 return CP_ACCESS_TRAP;
1578 }
0b6440af
EI
1579
1580 if (arm_feature(env, ARM_FEATURE_EL2) &&
1581 timeridx == GTIMER_PHYS && !secure && cur_el < 2 &&
1582 !extract32(env->cp15.cnthctl_el2, 0, 1)) {
1583 return CP_ACCESS_TRAP_EL2;
1584 }
00108f2d
PM
1585 return CP_ACCESS_OK;
1586}
1587
3f208fd7
PM
1588static CPAccessResult gt_timer_access(CPUARMState *env, int timeridx,
1589 bool isread)
00108f2d 1590{
0b6440af
EI
1591 unsigned int cur_el = arm_current_el(env);
1592 bool secure = arm_is_secure(env);
1593
00108f2d
PM
1594 /* CNT[PV]_CVAL, CNT[PV]_CTL, CNT[PV]_TVAL: not visible from PL0 if
1595 * EL0[PV]TEN is zero.
1596 */
0b6440af 1597 if (cur_el == 0 &&
00108f2d
PM
1598 !extract32(env->cp15.c14_cntkctl, 9 - timeridx, 1)) {
1599 return CP_ACCESS_TRAP;
1600 }
0b6440af
EI
1601
1602 if (arm_feature(env, ARM_FEATURE_EL2) &&
1603 timeridx == GTIMER_PHYS && !secure && cur_el < 2 &&
1604 !extract32(env->cp15.cnthctl_el2, 1, 1)) {
1605 return CP_ACCESS_TRAP_EL2;
1606 }
00108f2d
PM
1607 return CP_ACCESS_OK;
1608}
1609
1610static CPAccessResult gt_pct_access(CPUARMState *env,
3f208fd7
PM
1611 const ARMCPRegInfo *ri,
1612 bool isread)
00108f2d 1613{
3f208fd7 1614 return gt_counter_access(env, GTIMER_PHYS, isread);
00108f2d
PM
1615}
1616
1617static CPAccessResult gt_vct_access(CPUARMState *env,
3f208fd7
PM
1618 const ARMCPRegInfo *ri,
1619 bool isread)
00108f2d 1620{
3f208fd7 1621 return gt_counter_access(env, GTIMER_VIRT, isread);
00108f2d
PM
1622}
1623
3f208fd7
PM
1624static CPAccessResult gt_ptimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
1625 bool isread)
00108f2d 1626{
3f208fd7 1627 return gt_timer_access(env, GTIMER_PHYS, isread);
00108f2d
PM
1628}
1629
3f208fd7
PM
1630static CPAccessResult gt_vtimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
1631 bool isread)
00108f2d 1632{
3f208fd7 1633 return gt_timer_access(env, GTIMER_VIRT, isread);
00108f2d
PM
1634}
1635
b4d3978c 1636static CPAccessResult gt_stimer_access(CPUARMState *env,
3f208fd7
PM
1637 const ARMCPRegInfo *ri,
1638 bool isread)
b4d3978c
PM
1639{
1640 /* The AArch64 register view of the secure physical timer is
1641 * always accessible from EL3, and configurably accessible from
1642 * Secure EL1.
1643 */
1644 switch (arm_current_el(env)) {
1645 case 1:
1646 if (!arm_is_secure(env)) {
1647 return CP_ACCESS_TRAP;
1648 }
1649 if (!(env->cp15.scr_el3 & SCR_ST)) {
1650 return CP_ACCESS_TRAP_EL3;
1651 }
1652 return CP_ACCESS_OK;
1653 case 0:
1654 case 2:
1655 return CP_ACCESS_TRAP;
1656 case 3:
1657 return CP_ACCESS_OK;
1658 default:
1659 g_assert_not_reached();
1660 }
1661}
1662
55d284af
PM
1663static uint64_t gt_get_countervalue(CPUARMState *env)
1664{
bc72ad67 1665 return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) / GTIMER_SCALE;
55d284af
PM
1666}
1667
1668static void gt_recalc_timer(ARMCPU *cpu, int timeridx)
1669{
1670 ARMGenericTimer *gt = &cpu->env.cp15.c14_timer[timeridx];
1671
1672 if (gt->ctl & 1) {
1673 /* Timer enabled: calculate and set current ISTATUS, irq, and
1674 * reset timer to when ISTATUS next has to change
1675 */
edac4d8a
EI
1676 uint64_t offset = timeridx == GTIMER_VIRT ?
1677 cpu->env.cp15.cntvoff_el2 : 0;
55d284af
PM
1678 uint64_t count = gt_get_countervalue(&cpu->env);
1679 /* Note that this must be unsigned 64 bit arithmetic: */
edac4d8a 1680 int istatus = count - offset >= gt->cval;
55d284af 1681 uint64_t nexttick;
194cbc49 1682 int irqstate;
55d284af
PM
1683
1684 gt->ctl = deposit32(gt->ctl, 2, 1, istatus);
194cbc49
PM
1685
1686 irqstate = (istatus && !(gt->ctl & 2));
1687 qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate);
1688
55d284af
PM
1689 if (istatus) {
1690 /* Next transition is when count rolls back over to zero */
1691 nexttick = UINT64_MAX;
1692 } else {
1693 /* Next transition is when we hit cval */
edac4d8a 1694 nexttick = gt->cval + offset;
55d284af
PM
1695 }
1696 /* Note that the desired next expiry time might be beyond the
1697 * signed-64-bit range of a QEMUTimer -- in this case we just
1698 * set the timer for as far in the future as possible. When the
1699 * timer expires we will reset the timer for any remaining period.
1700 */
1701 if (nexttick > INT64_MAX / GTIMER_SCALE) {
1702 nexttick = INT64_MAX / GTIMER_SCALE;
1703 }
bc72ad67 1704 timer_mod(cpu->gt_timer[timeridx], nexttick);
194cbc49 1705 trace_arm_gt_recalc(timeridx, irqstate, nexttick);
55d284af
PM
1706 } else {
1707 /* Timer disabled: ISTATUS and timer output always clear */
1708 gt->ctl &= ~4;
1709 qemu_set_irq(cpu->gt_timer_outputs[timeridx], 0);
bc72ad67 1710 timer_del(cpu->gt_timer[timeridx]);
194cbc49 1711 trace_arm_gt_recalc_disabled(timeridx);
55d284af
PM
1712 }
1713}
1714
0e3eca4c
EI
1715static void gt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri,
1716 int timeridx)
55d284af
PM
1717{
1718 ARMCPU *cpu = arm_env_get_cpu(env);
55d284af 1719
bc72ad67 1720 timer_del(cpu->gt_timer[timeridx]);
55d284af
PM
1721}
1722
c4241c7d 1723static uint64_t gt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
55d284af 1724{
c4241c7d 1725 return gt_get_countervalue(env);
55d284af
PM
1726}
1727
edac4d8a
EI
1728static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
1729{
1730 return gt_get_countervalue(env) - env->cp15.cntvoff_el2;
1731}
1732
c4241c7d 1733static void gt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
0e3eca4c 1734 int timeridx,
c4241c7d 1735 uint64_t value)
55d284af 1736{
194cbc49 1737 trace_arm_gt_cval_write(timeridx, value);
55d284af
PM
1738 env->cp15.c14_timer[timeridx].cval = value;
1739 gt_recalc_timer(arm_env_get_cpu(env), timeridx);
55d284af 1740}
c4241c7d 1741
0e3eca4c
EI
1742static uint64_t gt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri,
1743 int timeridx)
55d284af 1744{
edac4d8a 1745 uint64_t offset = timeridx == GTIMER_VIRT ? env->cp15.cntvoff_el2 : 0;
55d284af 1746
c4241c7d 1747 return (uint32_t)(env->cp15.c14_timer[timeridx].cval -
edac4d8a 1748 (gt_get_countervalue(env) - offset));
55d284af
PM
1749}
1750
c4241c7d 1751static void gt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
0e3eca4c 1752 int timeridx,
c4241c7d 1753 uint64_t value)
55d284af 1754{
edac4d8a 1755 uint64_t offset = timeridx == GTIMER_VIRT ? env->cp15.cntvoff_el2 : 0;
55d284af 1756
194cbc49 1757 trace_arm_gt_tval_write(timeridx, value);
edac4d8a 1758 env->cp15.c14_timer[timeridx].cval = gt_get_countervalue(env) - offset +
18084b2f 1759 sextract64(value, 0, 32);
55d284af 1760 gt_recalc_timer(arm_env_get_cpu(env), timeridx);
55d284af
PM
1761}
1762
c4241c7d 1763static void gt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
0e3eca4c 1764 int timeridx,
c4241c7d 1765 uint64_t value)
55d284af
PM
1766{
1767 ARMCPU *cpu = arm_env_get_cpu(env);
55d284af
PM
1768 uint32_t oldval = env->cp15.c14_timer[timeridx].ctl;
1769
194cbc49 1770 trace_arm_gt_ctl_write(timeridx, value);
d3afacc7 1771 env->cp15.c14_timer[timeridx].ctl = deposit64(oldval, 0, 2, value);
55d284af
PM
1772 if ((oldval ^ value) & 1) {
1773 /* Enable toggled */
1774 gt_recalc_timer(cpu, timeridx);
d3afacc7 1775 } else if ((oldval ^ value) & 2) {
55d284af
PM
1776 /* IMASK toggled: don't need to recalculate,
1777 * just set the interrupt line based on ISTATUS
1778 */
194cbc49
PM
1779 int irqstate = (oldval & 4) && !(value & 2);
1780
1781 trace_arm_gt_imask_toggle(timeridx, irqstate);
1782 qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate);
55d284af 1783 }
55d284af
PM
1784}
1785
0e3eca4c
EI
1786static void gt_phys_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1787{
1788 gt_timer_reset(env, ri, GTIMER_PHYS);
1789}
1790
1791static void gt_phys_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1792 uint64_t value)
1793{
1794 gt_cval_write(env, ri, GTIMER_PHYS, value);
1795}
1796
1797static uint64_t gt_phys_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
1798{
1799 return gt_tval_read(env, ri, GTIMER_PHYS);
1800}
1801
1802static void gt_phys_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1803 uint64_t value)
1804{
1805 gt_tval_write(env, ri, GTIMER_PHYS, value);
1806}
1807
1808static void gt_phys_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
1809 uint64_t value)
1810{
1811 gt_ctl_write(env, ri, GTIMER_PHYS, value);
1812}
1813
1814static void gt_virt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1815{
1816 gt_timer_reset(env, ri, GTIMER_VIRT);
1817}
1818
1819static void gt_virt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1820 uint64_t value)
1821{
1822 gt_cval_write(env, ri, GTIMER_VIRT, value);
1823}
1824
1825static uint64_t gt_virt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
1826{
1827 return gt_tval_read(env, ri, GTIMER_VIRT);
1828}
1829
1830static void gt_virt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1831 uint64_t value)
1832{
1833 gt_tval_write(env, ri, GTIMER_VIRT, value);
1834}
1835
1836static void gt_virt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
1837 uint64_t value)
1838{
1839 gt_ctl_write(env, ri, GTIMER_VIRT, value);
1840}
1841
edac4d8a
EI
1842static void gt_cntvoff_write(CPUARMState *env, const ARMCPRegInfo *ri,
1843 uint64_t value)
1844{
1845 ARMCPU *cpu = arm_env_get_cpu(env);
1846
194cbc49 1847 trace_arm_gt_cntvoff_write(value);
edac4d8a
EI
1848 raw_write(env, ri, value);
1849 gt_recalc_timer(cpu, GTIMER_VIRT);
1850}
1851
b0e66d95
EI
1852static void gt_hyp_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1853{
1854 gt_timer_reset(env, ri, GTIMER_HYP);
1855}
1856
1857static void gt_hyp_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1858 uint64_t value)
1859{
1860 gt_cval_write(env, ri, GTIMER_HYP, value);
1861}
1862
1863static uint64_t gt_hyp_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
1864{
1865 return gt_tval_read(env, ri, GTIMER_HYP);
1866}
1867
1868static void gt_hyp_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1869 uint64_t value)
1870{
1871 gt_tval_write(env, ri, GTIMER_HYP, value);
1872}
1873
1874static void gt_hyp_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
1875 uint64_t value)
1876{
1877 gt_ctl_write(env, ri, GTIMER_HYP, value);
1878}
1879
b4d3978c
PM
1880static void gt_sec_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1881{
1882 gt_timer_reset(env, ri, GTIMER_SEC);
1883}
1884
1885static void gt_sec_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1886 uint64_t value)
1887{
1888 gt_cval_write(env, ri, GTIMER_SEC, value);
1889}
1890
1891static uint64_t gt_sec_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
1892{
1893 return gt_tval_read(env, ri, GTIMER_SEC);
1894}
1895
1896static void gt_sec_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1897 uint64_t value)
1898{
1899 gt_tval_write(env, ri, GTIMER_SEC, value);
1900}
1901
1902static void gt_sec_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
1903 uint64_t value)
1904{
1905 gt_ctl_write(env, ri, GTIMER_SEC, value);
1906}
1907
55d284af
PM
1908void arm_gt_ptimer_cb(void *opaque)
1909{
1910 ARMCPU *cpu = opaque;
1911
1912 gt_recalc_timer(cpu, GTIMER_PHYS);
1913}
1914
1915void arm_gt_vtimer_cb(void *opaque)
1916{
1917 ARMCPU *cpu = opaque;
1918
1919 gt_recalc_timer(cpu, GTIMER_VIRT);
1920}
1921
b0e66d95
EI
1922void arm_gt_htimer_cb(void *opaque)
1923{
1924 ARMCPU *cpu = opaque;
1925
1926 gt_recalc_timer(cpu, GTIMER_HYP);
1927}
1928
b4d3978c
PM
1929void arm_gt_stimer_cb(void *opaque)
1930{
1931 ARMCPU *cpu = opaque;
1932
1933 gt_recalc_timer(cpu, GTIMER_SEC);
1934}
1935
55d284af
PM
1936static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
1937 /* Note that CNTFRQ is purely reads-as-written for the benefit
1938 * of software; writing it doesn't actually change the timer frequency.
1939 * Our reset value matches the fixed frequency we implement the timer at.
1940 */
1941 { .name = "CNTFRQ", .cp = 15, .crn = 14, .crm = 0, .opc1 = 0, .opc2 = 0,
7a0e58fa 1942 .type = ARM_CP_ALIAS,
a7adc4b7
PM
1943 .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
1944 .fieldoffset = offsetoflow32(CPUARMState, cp15.c14_cntfrq),
a7adc4b7
PM
1945 },
1946 { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
1947 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
1948 .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
55d284af
PM
1949 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
1950 .resetvalue = (1000 * 1000 * 1000) / GTIMER_SCALE,
55d284af
PM
1951 },
1952 /* overall control: mostly access permissions */
a7adc4b7
PM
1953 { .name = "CNTKCTL", .state = ARM_CP_STATE_BOTH,
1954 .opc0 = 3, .opc1 = 0, .crn = 14, .crm = 1, .opc2 = 0,
55d284af
PM
1955 .access = PL1_RW,
1956 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntkctl),
1957 .resetvalue = 0,
1958 },
1959 /* per-timer control */
1960 { .name = "CNTP_CTL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
9ff9dd3c 1961 .secure = ARM_CP_SECSTATE_NS,
7a0e58fa 1962 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL1_RW | PL0_R,
a7adc4b7
PM
1963 .accessfn = gt_ptimer_access,
1964 .fieldoffset = offsetoflow32(CPUARMState,
1965 cp15.c14_timer[GTIMER_PHYS].ctl),
0e3eca4c 1966 .writefn = gt_phys_ctl_write, .raw_writefn = raw_write,
a7adc4b7 1967 },
9ff9dd3c
PM
1968 { .name = "CNTP_CTL(S)",
1969 .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
1970 .secure = ARM_CP_SECSTATE_S,
1971 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL1_RW | PL0_R,
1972 .accessfn = gt_ptimer_access,
1973 .fieldoffset = offsetoflow32(CPUARMState,
1974 cp15.c14_timer[GTIMER_SEC].ctl),
1975 .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
1976 },
a7adc4b7
PM
1977 { .name = "CNTP_CTL_EL0", .state = ARM_CP_STATE_AA64,
1978 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 1,
55d284af 1979 .type = ARM_CP_IO, .access = PL1_RW | PL0_R,
a7adc4b7 1980 .accessfn = gt_ptimer_access,
55d284af
PM
1981 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
1982 .resetvalue = 0,
0e3eca4c 1983 .writefn = gt_phys_ctl_write, .raw_writefn = raw_write,
55d284af
PM
1984 },
1985 { .name = "CNTV_CTL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 1,
7a0e58fa 1986 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL1_RW | PL0_R,
a7adc4b7
PM
1987 .accessfn = gt_vtimer_access,
1988 .fieldoffset = offsetoflow32(CPUARMState,
1989 cp15.c14_timer[GTIMER_VIRT].ctl),
0e3eca4c 1990 .writefn = gt_virt_ctl_write, .raw_writefn = raw_write,
a7adc4b7
PM
1991 },
1992 { .name = "CNTV_CTL_EL0", .state = ARM_CP_STATE_AA64,
1993 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 1,
55d284af 1994 .type = ARM_CP_IO, .access = PL1_RW | PL0_R,
a7adc4b7 1995 .accessfn = gt_vtimer_access,
55d284af
PM
1996 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
1997 .resetvalue = 0,
0e3eca4c 1998 .writefn = gt_virt_ctl_write, .raw_writefn = raw_write,
55d284af
PM
1999 },
2000 /* TimerValue views: a 32 bit downcounting view of the underlying state */
2001 { .name = "CNTP_TVAL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
9ff9dd3c 2002 .secure = ARM_CP_SECSTATE_NS,
7a0e58fa 2003 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW | PL0_R,
00108f2d 2004 .accessfn = gt_ptimer_access,
0e3eca4c 2005 .readfn = gt_phys_tval_read, .writefn = gt_phys_tval_write,
55d284af 2006 },
9ff9dd3c
PM
2007 { .name = "CNTP_TVAL(S)",
2008 .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
2009 .secure = ARM_CP_SECSTATE_S,
2010 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW | PL0_R,
2011 .accessfn = gt_ptimer_access,
2012 .readfn = gt_sec_tval_read, .writefn = gt_sec_tval_write,
2013 },
a7adc4b7
PM
2014 { .name = "CNTP_TVAL_EL0", .state = ARM_CP_STATE_AA64,
2015 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 0,
7a0e58fa 2016 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW | PL0_R,
0e3eca4c
EI
2017 .accessfn = gt_ptimer_access, .resetfn = gt_phys_timer_reset,
2018 .readfn = gt_phys_tval_read, .writefn = gt_phys_tval_write,
a7adc4b7 2019 },
55d284af 2020 { .name = "CNTV_TVAL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 0,
7a0e58fa 2021 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW | PL0_R,
00108f2d 2022 .accessfn = gt_vtimer_access,
0e3eca4c 2023 .readfn = gt_virt_tval_read, .writefn = gt_virt_tval_write,
55d284af 2024 },
a7adc4b7
PM
2025 { .name = "CNTV_TVAL_EL0", .state = ARM_CP_STATE_AA64,
2026 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 0,
7a0e58fa 2027 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW | PL0_R,
0e3eca4c
EI
2028 .accessfn = gt_vtimer_access, .resetfn = gt_virt_timer_reset,
2029 .readfn = gt_virt_tval_read, .writefn = gt_virt_tval_write,
a7adc4b7 2030 },
55d284af
PM
2031 /* The counter itself */
2032 { .name = "CNTPCT", .cp = 15, .crm = 14, .opc1 = 0,
7a0e58fa 2033 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
00108f2d 2034 .accessfn = gt_pct_access,
a7adc4b7
PM
2035 .readfn = gt_cnt_read, .resetfn = arm_cp_reset_ignore,
2036 },
2037 { .name = "CNTPCT_EL0", .state = ARM_CP_STATE_AA64,
2038 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 1,
7a0e58fa 2039 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
d57b9ee8 2040 .accessfn = gt_pct_access, .readfn = gt_cnt_read,
55d284af
PM
2041 },
2042 { .name = "CNTVCT", .cp = 15, .crm = 14, .opc1 = 1,
7a0e58fa 2043 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
00108f2d 2044 .accessfn = gt_vct_access,
edac4d8a 2045 .readfn = gt_virt_cnt_read, .resetfn = arm_cp_reset_ignore,
a7adc4b7
PM
2046 },
2047 { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
2048 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
7a0e58fa 2049 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
d57b9ee8 2050 .accessfn = gt_vct_access, .readfn = gt_virt_cnt_read,
55d284af
PM
2051 },
2052 /* Comparison value, indicating when the timer goes off */
2053 { .name = "CNTP_CVAL", .cp = 15, .crm = 14, .opc1 = 2,
9ff9dd3c 2054 .secure = ARM_CP_SECSTATE_NS,
55d284af 2055 .access = PL1_RW | PL0_R,
7a0e58fa 2056 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
55d284af 2057 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
b061a82b 2058 .accessfn = gt_ptimer_access,
0e3eca4c 2059 .writefn = gt_phys_cval_write, .raw_writefn = raw_write,
a7adc4b7 2060 },
9ff9dd3c
PM
2061 { .name = "CNTP_CVAL(S)", .cp = 15, .crm = 14, .opc1 = 2,
2062 .secure = ARM_CP_SECSTATE_S,
2063 .access = PL1_RW | PL0_R,
2064 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
2065 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
2066 .accessfn = gt_ptimer_access,
2067 .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
2068 },
a7adc4b7
PM
2069 { .name = "CNTP_CVAL_EL0", .state = ARM_CP_STATE_AA64,
2070 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 2,
2071 .access = PL1_RW | PL0_R,
2072 .type = ARM_CP_IO,
2073 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
12cde08a 2074 .resetvalue = 0, .accessfn = gt_ptimer_access,
0e3eca4c 2075 .writefn = gt_phys_cval_write, .raw_writefn = raw_write,
55d284af
PM
2076 },
2077 { .name = "CNTV_CVAL", .cp = 15, .crm = 14, .opc1 = 3,
2078 .access = PL1_RW | PL0_R,
7a0e58fa 2079 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
55d284af 2080 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
b061a82b 2081 .accessfn = gt_vtimer_access,
0e3eca4c 2082 .writefn = gt_virt_cval_write, .raw_writefn = raw_write,
a7adc4b7
PM
2083 },
2084 { .name = "CNTV_CVAL_EL0", .state = ARM_CP_STATE_AA64,
2085 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 2,
2086 .access = PL1_RW | PL0_R,
2087 .type = ARM_CP_IO,
2088 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
2089 .resetvalue = 0, .accessfn = gt_vtimer_access,
0e3eca4c 2090 .writefn = gt_virt_cval_write, .raw_writefn = raw_write,
55d284af 2091 },
b4d3978c
PM
2092 /* Secure timer -- this is actually restricted to only EL3
2093 * and configurably Secure-EL1 via the accessfn.
2094 */
2095 { .name = "CNTPS_TVAL_EL1", .state = ARM_CP_STATE_AA64,
2096 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 0,
2097 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW,
2098 .accessfn = gt_stimer_access,
2099 .readfn = gt_sec_tval_read,
2100 .writefn = gt_sec_tval_write,
2101 .resetfn = gt_sec_timer_reset,
2102 },
2103 { .name = "CNTPS_CTL_EL1", .state = ARM_CP_STATE_AA64,
2104 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 1,
2105 .type = ARM_CP_IO, .access = PL1_RW,
2106 .accessfn = gt_stimer_access,
2107 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].ctl),
2108 .resetvalue = 0,
2109 .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
2110 },
2111 { .name = "CNTPS_CVAL_EL1", .state = ARM_CP_STATE_AA64,
2112 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 2,
2113 .type = ARM_CP_IO, .access = PL1_RW,
2114 .accessfn = gt_stimer_access,
2115 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
2116 .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
2117 },
55d284af
PM
2118 REGINFO_SENTINEL
2119};
2120
2121#else
2122/* In user-mode none of the generic timer registers are accessible,
bc72ad67 2123 * and their implementation depends on QEMU_CLOCK_VIRTUAL and qdev gpio outputs,
55d284af
PM
2124 * so instead just don't register any of them.
2125 */
6cc7a3ae 2126static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
6cc7a3ae
PM
2127 REGINFO_SENTINEL
2128};
2129
55d284af
PM
2130#endif
2131
c4241c7d 2132static void par_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
4a501606 2133{
891a2fe7 2134 if (arm_feature(env, ARM_FEATURE_LPAE)) {
8d5c773e 2135 raw_write(env, ri, value);
891a2fe7 2136 } else if (arm_feature(env, ARM_FEATURE_V7)) {
8d5c773e 2137 raw_write(env, ri, value & 0xfffff6ff);
4a501606 2138 } else {
8d5c773e 2139 raw_write(env, ri, value & 0xfffff1ff);
4a501606 2140 }
4a501606
PM
2141}
2142
2143#ifndef CONFIG_USER_ONLY
2144/* get_phys_addr() isn't present for user-mode-only targets */
702a9357 2145
3f208fd7
PM
2146static CPAccessResult ats_access(CPUARMState *env, const ARMCPRegInfo *ri,
2147 bool isread)
92611c00
PM
2148{
2149 if (ri->opc2 & 4) {
87562e4f
PM
2150 /* The ATS12NSO* operations must trap to EL3 if executed in
2151 * Secure EL1 (which can only happen if EL3 is AArch64).
2152 * They are simply UNDEF if executed from NS EL1.
2153 * They function normally from EL2 or EL3.
92611c00 2154 */
87562e4f
PM
2155 if (arm_current_el(env) == 1) {
2156 if (arm_is_secure_below_el3(env)) {
2157 return CP_ACCESS_TRAP_UNCATEGORIZED_EL3;
2158 }
2159 return CP_ACCESS_TRAP_UNCATEGORIZED;
2160 }
92611c00
PM
2161 }
2162 return CP_ACCESS_OK;
2163}
2164
060e8a48 2165static uint64_t do_ats_write(CPUARMState *env, uint64_t value,
03ae85f8 2166 MMUAccessType access_type, ARMMMUIdx mmu_idx)
4a501606 2167{
a8170e5e 2168 hwaddr phys_addr;
4a501606
PM
2169 target_ulong page_size;
2170 int prot;
b7cc4e82 2171 bool ret;
01c097f7 2172 uint64_t par64;
1313e2d7 2173 bool format64 = false;
8bf5b6a9 2174 MemTxAttrs attrs = {};
e14b5a23 2175 ARMMMUFaultInfo fi = {};
5b2d261d 2176 ARMCacheAttrs cacheattrs = {};
4a501606 2177
5b2d261d 2178 ret = get_phys_addr(env, value, access_type, mmu_idx, &phys_addr, &attrs,
bc52bfeb 2179 &prot, &page_size, &fi, &cacheattrs);
1313e2d7
EI
2180
2181 if (is_a64(env)) {
2182 format64 = true;
2183 } else if (arm_feature(env, ARM_FEATURE_LPAE)) {
2184 /*
2185 * ATS1Cxx:
2186 * * TTBCR.EAE determines whether the result is returned using the
2187 * 32-bit or the 64-bit PAR format
2188 * * Instructions executed in Hyp mode always use the 64bit format
2189 *
2190 * ATS1S2NSOxx uses the 64bit format if any of the following is true:
2191 * * The Non-secure TTBCR.EAE bit is set to 1
2192 * * The implementation includes EL2, and the value of HCR.VM is 1
2193 *
2194 * ATS1Hx always uses the 64bit format (not supported yet).
2195 */
2196 format64 = arm_s1_regime_using_lpae_format(env, mmu_idx);
2197
2198 if (arm_feature(env, ARM_FEATURE_EL2)) {
2199 if (mmu_idx == ARMMMUIdx_S12NSE0 || mmu_idx == ARMMMUIdx_S12NSE1) {
2200 format64 |= env->cp15.hcr_el2 & HCR_VM;
2201 } else {
2202 format64 |= arm_current_el(env) == 2;
2203 }
2204 }
2205 }
2206
2207 if (format64) {
5efe9ed4 2208 /* Create a 64-bit PAR */
01c097f7 2209 par64 = (1 << 11); /* LPAE bit always set */
b7cc4e82 2210 if (!ret) {
702a9357 2211 par64 |= phys_addr & ~0xfffULL;
8bf5b6a9
PM
2212 if (!attrs.secure) {
2213 par64 |= (1 << 9); /* NS */
2214 }
5b2d261d
AB
2215 par64 |= (uint64_t)cacheattrs.attrs << 56; /* ATTR */
2216 par64 |= cacheattrs.shareability << 7; /* SH */
4a501606 2217 } else {
5efe9ed4
PM
2218 uint32_t fsr = arm_fi_to_lfsc(&fi);
2219
702a9357 2220 par64 |= 1; /* F */
b7cc4e82 2221 par64 |= (fsr & 0x3f) << 1; /* FS */
702a9357
PM
2222 /* Note that S2WLK and FSTAGE are always zero, because we don't
2223 * implement virtualization and therefore there can't be a stage 2
2224 * fault.
2225 */
4a501606
PM
2226 }
2227 } else {
b7cc4e82 2228 /* fsr is a DFSR/IFSR value for the short descriptor
702a9357
PM
2229 * translation table format (with WnR always clear).
2230 * Convert it to a 32-bit PAR.
2231 */
b7cc4e82 2232 if (!ret) {
702a9357
PM
2233 /* We do not set any attribute bits in the PAR */
2234 if (page_size == (1 << 24)
2235 && arm_feature(env, ARM_FEATURE_V7)) {
01c097f7 2236 par64 = (phys_addr & 0xff000000) | (1 << 1);
702a9357 2237 } else {
01c097f7 2238 par64 = phys_addr & 0xfffff000;
702a9357 2239 }
8bf5b6a9
PM
2240 if (!attrs.secure) {
2241 par64 |= (1 << 9); /* NS */
2242 }
702a9357 2243 } else {
5efe9ed4
PM
2244 uint32_t fsr = arm_fi_to_sfsc(&fi);
2245
b7cc4e82
PC
2246 par64 = ((fsr & (1 << 10)) >> 5) | ((fsr & (1 << 12)) >> 6) |
2247 ((fsr & 0xf) << 1) | 1;
702a9357 2248 }
4a501606 2249 }
060e8a48
PM
2250 return par64;
2251}
2252
2253static void ats_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
2254{
03ae85f8 2255 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
060e8a48 2256 uint64_t par64;
d3649702
PM
2257 ARMMMUIdx mmu_idx;
2258 int el = arm_current_el(env);
2259 bool secure = arm_is_secure_below_el3(env);
060e8a48 2260
d3649702
PM
2261 switch (ri->opc2 & 6) {
2262 case 0:
2263 /* stage 1 current state PL1: ATS1CPR, ATS1CPW */
2264 switch (el) {
2265 case 3:
2266 mmu_idx = ARMMMUIdx_S1E3;
2267 break;
2268 case 2:
2269 mmu_idx = ARMMMUIdx_S1NSE1;
2270 break;
2271 case 1:
2272 mmu_idx = secure ? ARMMMUIdx_S1SE1 : ARMMMUIdx_S1NSE1;
2273 break;
2274 default:
2275 g_assert_not_reached();
2276 }
2277 break;
2278 case 2:
2279 /* stage 1 current state PL0: ATS1CUR, ATS1CUW */
2280 switch (el) {
2281 case 3:
2282 mmu_idx = ARMMMUIdx_S1SE0;
2283 break;
2284 case 2:
2285 mmu_idx = ARMMMUIdx_S1NSE0;
2286 break;
2287 case 1:
2288 mmu_idx = secure ? ARMMMUIdx_S1SE0 : ARMMMUIdx_S1NSE0;
2289 break;
2290 default:
2291 g_assert_not_reached();
2292 }
2293 break;
2294 case 4:
2295 /* stage 1+2 NonSecure PL1: ATS12NSOPR, ATS12NSOPW */
2296 mmu_idx = ARMMMUIdx_S12NSE1;
2297 break;
2298 case 6:
2299 /* stage 1+2 NonSecure PL0: ATS12NSOUR, ATS12NSOUW */
2300 mmu_idx = ARMMMUIdx_S12NSE0;
2301 break;
2302 default:
2303 g_assert_not_reached();
2304 }
2305
2306 par64 = do_ats_write(env, value, access_type, mmu_idx);
01c097f7
FA
2307
2308 A32_BANKED_CURRENT_REG_SET(env, par, par64);
4a501606 2309}
060e8a48 2310
14db7fe0
PM
2311static void ats1h_write(CPUARMState *env, const ARMCPRegInfo *ri,
2312 uint64_t value)
2313{
03ae85f8 2314 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
14db7fe0
PM
2315 uint64_t par64;
2316
2317 par64 = do_ats_write(env, value, access_type, ARMMMUIdx_S2NS);
2318
2319 A32_BANKED_CURRENT_REG_SET(env, par, par64);
2320}
2321
3f208fd7
PM
2322static CPAccessResult at_s1e2_access(CPUARMState *env, const ARMCPRegInfo *ri,
2323 bool isread)
2a47df95
PM
2324{
2325 if (arm_current_el(env) == 3 && !(env->cp15.scr_el3 & SCR_NS)) {
2326 return CP_ACCESS_TRAP;
2327 }
2328 return CP_ACCESS_OK;
2329}
2330
060e8a48
PM
2331static void ats_write64(CPUARMState *env, const ARMCPRegInfo *ri,
2332 uint64_t value)
2333{
03ae85f8 2334 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
d3649702
PM
2335 ARMMMUIdx mmu_idx;
2336 int secure = arm_is_secure_below_el3(env);
2337
2338 switch (ri->opc2 & 6) {
2339 case 0:
2340 switch (ri->opc1) {
2341 case 0: /* AT S1E1R, AT S1E1W */
2342 mmu_idx = secure ? ARMMMUIdx_S1SE1 : ARMMMUIdx_S1NSE1;
2343 break;
2344 case 4: /* AT S1E2R, AT S1E2W */
2345 mmu_idx = ARMMMUIdx_S1E2;
2346 break;
2347 case 6: /* AT S1E3R, AT S1E3W */
2348 mmu_idx = ARMMMUIdx_S1E3;
2349 break;
2350 default:
2351 g_assert_not_reached();
2352 }
2353 break;
2354 case 2: /* AT S1E0R, AT S1E0W */
2355 mmu_idx = secure ? ARMMMUIdx_S1SE0 : ARMMMUIdx_S1NSE0;
2356 break;
2357 case 4: /* AT S12E1R, AT S12E1W */
2a47df95 2358 mmu_idx = secure ? ARMMMUIdx_S1SE1 : ARMMMUIdx_S12NSE1;
d3649702
PM
2359 break;
2360 case 6: /* AT S12E0R, AT S12E0W */
2a47df95 2361 mmu_idx = secure ? ARMMMUIdx_S1SE0 : ARMMMUIdx_S12NSE0;
d3649702
PM
2362 break;
2363 default:
2364 g_assert_not_reached();
2365 }
060e8a48 2366
d3649702 2367 env->cp15.par_el[1] = do_ats_write(env, value, access_type, mmu_idx);
060e8a48 2368}
4a501606
PM
2369#endif
2370
2371static const ARMCPRegInfo vapa_cp_reginfo[] = {
2372 { .name = "PAR", .cp = 15, .crn = 7, .crm = 4, .opc1 = 0, .opc2 = 0,
2373 .access = PL1_RW, .resetvalue = 0,
01c097f7
FA
2374 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.par_s),
2375 offsetoflow32(CPUARMState, cp15.par_ns) },
4a501606
PM
2376 .writefn = par_write },
2377#ifndef CONFIG_USER_ONLY
87562e4f 2378 /* This underdecoding is safe because the reginfo is NO_RAW. */
4a501606 2379 { .name = "ATS", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = CP_ANY,
92611c00 2380 .access = PL1_W, .accessfn = ats_access,
7a0e58fa 2381 .writefn = ats_write, .type = ARM_CP_NO_RAW },
4a501606
PM
2382#endif
2383 REGINFO_SENTINEL
2384};
2385
18032bec
PM
2386/* Return basic MPU access permission bits. */
2387static uint32_t simple_mpu_ap_bits(uint32_t val)
2388{
2389 uint32_t ret;
2390 uint32_t mask;
2391 int i;
2392 ret = 0;
2393 mask = 3;
2394 for (i = 0; i < 16; i += 2) {
2395 ret |= (val >> i) & mask;
2396 mask <<= 2;
2397 }
2398 return ret;
2399}
2400
2401/* Pad basic MPU access permission bits to extended format. */
2402static uint32_t extended_mpu_ap_bits(uint32_t val)
2403{
2404 uint32_t ret;
2405 uint32_t mask;
2406 int i;
2407 ret = 0;
2408 mask = 3;
2409 for (i = 0; i < 16; i += 2) {
2410 ret |= (val & mask) << i;
2411 mask <<= 2;
2412 }
2413 return ret;
2414}
2415
c4241c7d
PM
2416static void pmsav5_data_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
2417 uint64_t value)
18032bec 2418{
7e09797c 2419 env->cp15.pmsav5_data_ap = extended_mpu_ap_bits(value);
18032bec
PM
2420}
2421
c4241c7d 2422static uint64_t pmsav5_data_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
18032bec 2423{
7e09797c 2424 return simple_mpu_ap_bits(env->cp15.pmsav5_data_ap);
18032bec
PM
2425}
2426
c4241c7d
PM
2427static void pmsav5_insn_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
2428 uint64_t value)
18032bec 2429{
7e09797c 2430 env->cp15.pmsav5_insn_ap = extended_mpu_ap_bits(value);
18032bec
PM
2431}
2432
c4241c7d 2433static uint64_t pmsav5_insn_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
18032bec 2434{
7e09797c 2435 return simple_mpu_ap_bits(env->cp15.pmsav5_insn_ap);
18032bec
PM
2436}
2437
6cb0b013
PC
2438static uint64_t pmsav7_read(CPUARMState *env, const ARMCPRegInfo *ri)
2439{
2440 uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
2441
2442 if (!u32p) {
2443 return 0;
2444 }
2445
1bc04a88 2446 u32p += env->pmsav7.rnr[M_REG_NS];
6cb0b013
PC
2447 return *u32p;
2448}
2449
2450static void pmsav7_write(CPUARMState *env, const ARMCPRegInfo *ri,
2451 uint64_t value)
2452{
2453 ARMCPU *cpu = arm_env_get_cpu(env);
2454 uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
2455
2456 if (!u32p) {
2457 return;
2458 }
2459
1bc04a88 2460 u32p += env->pmsav7.rnr[M_REG_NS];
d10eb08f 2461 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
6cb0b013
PC
2462 *u32p = value;
2463}
2464
6cb0b013
PC
2465static void pmsav7_rgnr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2466 uint64_t value)
2467{
2468 ARMCPU *cpu = arm_env_get_cpu(env);
2469 uint32_t nrgs = cpu->pmsav7_dregion;
2470
2471 if (value >= nrgs) {
2472 qemu_log_mask(LOG_GUEST_ERROR,
2473 "PMSAv7 RGNR write >= # supported regions, %" PRIu32
2474 " > %" PRIu32 "\n", (uint32_t)value, nrgs);
2475 return;
2476 }
2477
2478 raw_write(env, ri, value);
2479}
2480
2481static const ARMCPRegInfo pmsav7_cp_reginfo[] = {
69ceea64
PM
2482 /* Reset for all these registers is handled in arm_cpu_reset(),
2483 * because the PMSAv7 is also used by M-profile CPUs, which do
2484 * not register cpregs but still need the state to be reset.
2485 */
6cb0b013
PC
2486 { .name = "DRBAR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 0,
2487 .access = PL1_RW, .type = ARM_CP_NO_RAW,
2488 .fieldoffset = offsetof(CPUARMState, pmsav7.drbar),
69ceea64
PM
2489 .readfn = pmsav7_read, .writefn = pmsav7_write,
2490 .resetfn = arm_cp_reset_ignore },
6cb0b013
PC
2491 { .name = "DRSR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 2,
2492 .access = PL1_RW, .type = ARM_CP_NO_RAW,
2493 .fieldoffset = offsetof(CPUARMState, pmsav7.drsr),
69ceea64
PM
2494 .readfn = pmsav7_read, .writefn = pmsav7_write,
2495 .resetfn = arm_cp_reset_ignore },
6cb0b013
PC
2496 { .name = "DRACR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 4,
2497 .access = PL1_RW, .type = ARM_CP_NO_RAW,
2498 .fieldoffset = offsetof(CPUARMState, pmsav7.dracr),
69ceea64
PM
2499 .readfn = pmsav7_read, .writefn = pmsav7_write,
2500 .resetfn = arm_cp_reset_ignore },
6cb0b013
PC
2501 { .name = "RGNR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 2, .opc2 = 0,
2502 .access = PL1_RW,
1bc04a88 2503 .fieldoffset = offsetof(CPUARMState, pmsav7.rnr[M_REG_NS]),
69ceea64
PM
2504 .writefn = pmsav7_rgnr_write,
2505 .resetfn = arm_cp_reset_ignore },
6cb0b013
PC
2506 REGINFO_SENTINEL
2507};
2508
18032bec
PM
2509static const ARMCPRegInfo pmsav5_cp_reginfo[] = {
2510 { .name = "DATA_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
7a0e58fa 2511 .access = PL1_RW, .type = ARM_CP_ALIAS,
7e09797c 2512 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
18032bec
PM
2513 .readfn = pmsav5_data_ap_read, .writefn = pmsav5_data_ap_write, },
2514 { .name = "INSN_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
7a0e58fa 2515 .access = PL1_RW, .type = ARM_CP_ALIAS,
7e09797c 2516 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
18032bec
PM
2517 .readfn = pmsav5_insn_ap_read, .writefn = pmsav5_insn_ap_write, },
2518 { .name = "DATA_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 2,
2519 .access = PL1_RW,
7e09797c
PM
2520 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
2521 .resetvalue = 0, },
18032bec
PM
2522 { .name = "INSN_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 3,
2523 .access = PL1_RW,
7e09797c
PM
2524 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
2525 .resetvalue = 0, },
ecce5c3c
PM
2526 { .name = "DCACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
2527 .access = PL1_RW,
2528 .fieldoffset = offsetof(CPUARMState, cp15.c2_data), .resetvalue = 0, },
2529 { .name = "ICACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 1,
2530 .access = PL1_RW,
2531 .fieldoffset = offsetof(CPUARMState, cp15.c2_insn), .resetvalue = 0, },
06d76f31 2532 /* Protection region base and size registers */
e508a92b
PM
2533 { .name = "946_PRBS0", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0,
2534 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2535 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[0]) },
2536 { .name = "946_PRBS1", .cp = 15, .crn = 6, .crm = 1, .opc1 = 0,
2537 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2538 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[1]) },
2539 { .name = "946_PRBS2", .cp = 15, .crn = 6, .crm = 2, .opc1 = 0,
2540 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2541 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[2]) },
2542 { .name = "946_PRBS3", .cp = 15, .crn = 6, .crm = 3, .opc1 = 0,
2543 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2544 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[3]) },
2545 { .name = "946_PRBS4", .cp = 15, .crn = 6, .crm = 4, .opc1 = 0,
2546 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2547 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[4]) },
2548 { .name = "946_PRBS5", .cp = 15, .crn = 6, .crm = 5, .opc1 = 0,
2549 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2550 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[5]) },
2551 { .name = "946_PRBS6", .cp = 15, .crn = 6, .crm = 6, .opc1 = 0,
2552 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2553 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[6]) },
2554 { .name = "946_PRBS7", .cp = 15, .crn = 6, .crm = 7, .opc1 = 0,
2555 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2556 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[7]) },
18032bec
PM
2557 REGINFO_SENTINEL
2558};
2559
c4241c7d
PM
2560static void vmsa_ttbcr_raw_write(CPUARMState *env, const ARMCPRegInfo *ri,
2561 uint64_t value)
ecce5c3c 2562{
11f136ee 2563 TCR *tcr = raw_ptr(env, ri);
2ebcebe2
PM
2564 int maskshift = extract32(value, 0, 3);
2565
e389be16
FA
2566 if (!arm_feature(env, ARM_FEATURE_V8)) {
2567 if (arm_feature(env, ARM_FEATURE_LPAE) && (value & TTBCR_EAE)) {
2568 /* Pre ARMv8 bits [21:19], [15:14] and [6:3] are UNK/SBZP when
2569 * using Long-desciptor translation table format */
2570 value &= ~((7 << 19) | (3 << 14) | (0xf << 3));
2571 } else if (arm_feature(env, ARM_FEATURE_EL3)) {
2572 /* In an implementation that includes the Security Extensions
2573 * TTBCR has additional fields PD0 [4] and PD1 [5] for
2574 * Short-descriptor translation table format.
2575 */
2576 value &= TTBCR_PD1 | TTBCR_PD0 | TTBCR_N;
2577 } else {
2578 value &= TTBCR_N;
2579 }
e42c4db3 2580 }
e389be16 2581
b6af0975 2582 /* Update the masks corresponding to the TCR bank being written
11f136ee 2583 * Note that we always calculate mask and base_mask, but
e42c4db3 2584 * they are only used for short-descriptor tables (ie if EAE is 0);
11f136ee
FA
2585 * for long-descriptor tables the TCR fields are used differently
2586 * and the mask and base_mask values are meaningless.
e42c4db3 2587 */
11f136ee
FA
2588 tcr->raw_tcr = value;
2589 tcr->mask = ~(((uint32_t)0xffffffffu) >> maskshift);
2590 tcr->base_mask = ~((uint32_t)0x3fffu >> maskshift);
ecce5c3c
PM
2591}
2592
c4241c7d
PM
2593static void vmsa_ttbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2594 uint64_t value)
d4e6df63 2595{
00c8cb0a
AF
2596 ARMCPU *cpu = arm_env_get_cpu(env);
2597
d4e6df63
PM
2598 if (arm_feature(env, ARM_FEATURE_LPAE)) {
2599 /* With LPAE the TTBCR could result in a change of ASID
2600 * via the TTBCR.A1 bit, so do a TLB flush.
2601 */
d10eb08f 2602 tlb_flush(CPU(cpu));
d4e6df63 2603 }
c4241c7d 2604 vmsa_ttbcr_raw_write(env, ri, value);
d4e6df63
PM
2605}
2606
ecce5c3c
PM
2607static void vmsa_ttbcr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2608{
11f136ee
FA
2609 TCR *tcr = raw_ptr(env, ri);
2610
2611 /* Reset both the TCR as well as the masks corresponding to the bank of
2612 * the TCR being reset.
2613 */
2614 tcr->raw_tcr = 0;
2615 tcr->mask = 0;
2616 tcr->base_mask = 0xffffc000u;
ecce5c3c
PM
2617}
2618
cb2e37df
PM
2619static void vmsa_tcr_el1_write(CPUARMState *env, const ARMCPRegInfo *ri,
2620 uint64_t value)
2621{
00c8cb0a 2622 ARMCPU *cpu = arm_env_get_cpu(env);
11f136ee 2623 TCR *tcr = raw_ptr(env, ri);
00c8cb0a 2624
cb2e37df 2625 /* For AArch64 the A1 bit could result in a change of ASID, so TLB flush. */
d10eb08f 2626 tlb_flush(CPU(cpu));
11f136ee 2627 tcr->raw_tcr = value;
cb2e37df
PM
2628}
2629
327ed10f
PM
2630static void vmsa_ttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2631 uint64_t value)
2632{
2633 /* 64 bit accesses to the TTBRs can change the ASID and so we
2634 * must flush the TLB.
2635 */
2636 if (cpreg_field_is_64bit(ri)) {
00c8cb0a
AF
2637 ARMCPU *cpu = arm_env_get_cpu(env);
2638
d10eb08f 2639 tlb_flush(CPU(cpu));
327ed10f
PM
2640 }
2641 raw_write(env, ri, value);
2642}
2643
b698e9cf
EI
2644static void vttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2645 uint64_t value)
2646{
2647 ARMCPU *cpu = arm_env_get_cpu(env);
2648 CPUState *cs = CPU(cpu);
2649
2650 /* Accesses to VTTBR may change the VMID so we must flush the TLB. */
2651 if (raw_read(env, ri) != value) {
0336cbf8 2652 tlb_flush_by_mmuidx(cs,
8bd5c820
PM
2653 ARMMMUIdxBit_S12NSE1 |
2654 ARMMMUIdxBit_S12NSE0 |
2655 ARMMMUIdxBit_S2NS);
b698e9cf
EI
2656 raw_write(env, ri, value);
2657 }
2658}
2659
8e5d75c9 2660static const ARMCPRegInfo vmsa_pmsa_cp_reginfo[] = {
18032bec 2661 { .name = "DFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
7a0e58fa 2662 .access = PL1_RW, .type = ARM_CP_ALIAS,
4a7e2d73 2663 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dfsr_s),
b061a82b 2664 offsetoflow32(CPUARMState, cp15.dfsr_ns) }, },
18032bec 2665 { .name = "IFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
88ca1c2d
FA
2666 .access = PL1_RW, .resetvalue = 0,
2667 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.ifsr_s),
2668 offsetoflow32(CPUARMState, cp15.ifsr_ns) } },
8e5d75c9
PC
2669 { .name = "DFAR", .cp = 15, .opc1 = 0, .crn = 6, .crm = 0, .opc2 = 0,
2670 .access = PL1_RW, .resetvalue = 0,
2671 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.dfar_s),
2672 offsetof(CPUARMState, cp15.dfar_ns) } },
2673 { .name = "FAR_EL1", .state = ARM_CP_STATE_AA64,
2674 .opc0 = 3, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 0,
2675 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[1]),
2676 .resetvalue = 0, },
2677 REGINFO_SENTINEL
2678};
2679
2680static const ARMCPRegInfo vmsa_cp_reginfo[] = {
6cd8a264
RH
2681 { .name = "ESR_EL1", .state = ARM_CP_STATE_AA64,
2682 .opc0 = 3, .crn = 5, .crm = 2, .opc1 = 0, .opc2 = 0,
2683 .access = PL1_RW,
d81c519c 2684 .fieldoffset = offsetof(CPUARMState, cp15.esr_el[1]), .resetvalue = 0, },
327ed10f 2685 { .name = "TTBR0_EL1", .state = ARM_CP_STATE_BOTH,
7dd8c9af
FA
2686 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 0,
2687 .access = PL1_RW, .writefn = vmsa_ttbr_write, .resetvalue = 0,
2688 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
2689 offsetof(CPUARMState, cp15.ttbr0_ns) } },
327ed10f 2690 { .name = "TTBR1_EL1", .state = ARM_CP_STATE_BOTH,
7dd8c9af
FA
2691 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 1,
2692 .access = PL1_RW, .writefn = vmsa_ttbr_write, .resetvalue = 0,
2693 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
2694 offsetof(CPUARMState, cp15.ttbr1_ns) } },
cb2e37df
PM
2695 { .name = "TCR_EL1", .state = ARM_CP_STATE_AA64,
2696 .opc0 = 3, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
2697 .access = PL1_RW, .writefn = vmsa_tcr_el1_write,
2698 .resetfn = vmsa_ttbcr_reset, .raw_writefn = raw_write,
11f136ee 2699 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[1]) },
cb2e37df 2700 { .name = "TTBCR", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
7a0e58fa 2701 .access = PL1_RW, .type = ARM_CP_ALIAS, .writefn = vmsa_ttbcr_write,
b061a82b 2702 .raw_writefn = vmsa_ttbcr_raw_write,
11f136ee
FA
2703 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tcr_el[3]),
2704 offsetoflow32(CPUARMState, cp15.tcr_el[1])} },
18032bec
PM
2705 REGINFO_SENTINEL
2706};
2707
c4241c7d
PM
2708static void omap_ticonfig_write(CPUARMState *env, const ARMCPRegInfo *ri,
2709 uint64_t value)
1047b9d7
PM
2710{
2711 env->cp15.c15_ticonfig = value & 0xe7;
2712 /* The OS_TYPE bit in this register changes the reported CPUID! */
2713 env->cp15.c0_cpuid = (value & (1 << 5)) ?
2714 ARM_CPUID_TI915T : ARM_CPUID_TI925T;
1047b9d7
PM
2715}
2716
c4241c7d
PM
2717static void omap_threadid_write(CPUARMState *env, const ARMCPRegInfo *ri,
2718 uint64_t value)
1047b9d7
PM
2719{
2720 env->cp15.c15_threadid = value & 0xffff;
1047b9d7
PM
2721}
2722
c4241c7d
PM
2723static void omap_wfi_write(CPUARMState *env, const ARMCPRegInfo *ri,
2724 uint64_t value)
1047b9d7
PM
2725{
2726 /* Wait-for-interrupt (deprecated) */
c3affe56 2727 cpu_interrupt(CPU(arm_env_get_cpu(env)), CPU_INTERRUPT_HALT);
1047b9d7
PM
2728}
2729
c4241c7d
PM
2730static void omap_cachemaint_write(CPUARMState *env, const ARMCPRegInfo *ri,
2731 uint64_t value)
c4804214
PM
2732{
2733 /* On OMAP there are registers indicating the max/min index of dcache lines
2734 * containing a dirty line; cache flush operations have to reset these.
2735 */
2736 env->cp15.c15_i_max = 0x000;
2737 env->cp15.c15_i_min = 0xff0;
c4804214
PM
2738}
2739
18032bec
PM
2740static const ARMCPRegInfo omap_cp_reginfo[] = {
2741 { .name = "DFSR", .cp = 15, .crn = 5, .crm = CP_ANY,
2742 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_OVERRIDE,
d81c519c 2743 .fieldoffset = offsetoflow32(CPUARMState, cp15.esr_el[1]),
6cd8a264 2744 .resetvalue = 0, },
1047b9d7
PM
2745 { .name = "", .cp = 15, .crn = 15, .crm = 0, .opc1 = 0, .opc2 = 0,
2746 .access = PL1_RW, .type = ARM_CP_NOP },
2747 { .name = "TICONFIG", .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0,
2748 .access = PL1_RW,
2749 .fieldoffset = offsetof(CPUARMState, cp15.c15_ticonfig), .resetvalue = 0,
2750 .writefn = omap_ticonfig_write },
2751 { .name = "IMAX", .cp = 15, .crn = 15, .crm = 2, .opc1 = 0, .opc2 = 0,
2752 .access = PL1_RW,
2753 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_max), .resetvalue = 0, },
2754 { .name = "IMIN", .cp = 15, .crn = 15, .crm = 3, .opc1 = 0, .opc2 = 0,
2755 .access = PL1_RW, .resetvalue = 0xff0,
2756 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_min) },
2757 { .name = "THREADID", .cp = 15, .crn = 15, .crm = 4, .opc1 = 0, .opc2 = 0,
2758 .access = PL1_RW,
2759 .fieldoffset = offsetof(CPUARMState, cp15.c15_threadid), .resetvalue = 0,
2760 .writefn = omap_threadid_write },
2761 { .name = "TI925T_STATUS", .cp = 15, .crn = 15,
2762 .crm = 8, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
7a0e58fa 2763 .type = ARM_CP_NO_RAW,
1047b9d7
PM
2764 .readfn = arm_cp_read_zero, .writefn = omap_wfi_write, },
2765 /* TODO: Peripheral port remap register:
2766 * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt controller
2767 * base address at $rn & ~0xfff and map size of 0x200 << ($rn & 0xfff),
2768 * when MMU is off.
2769 */
c4804214 2770 { .name = "OMAP_CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
d4e6df63 2771 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
7a0e58fa 2772 .type = ARM_CP_OVERRIDE | ARM_CP_NO_RAW,
c4804214 2773 .writefn = omap_cachemaint_write },
34f90529
PM
2774 { .name = "C9", .cp = 15, .crn = 9,
2775 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW,
2776 .type = ARM_CP_CONST | ARM_CP_OVERRIDE, .resetvalue = 0 },
1047b9d7
PM
2777 REGINFO_SENTINEL
2778};
2779
c4241c7d
PM
2780static void xscale_cpar_write(CPUARMState *env, const ARMCPRegInfo *ri,
2781 uint64_t value)
1047b9d7 2782{
c0f4af17 2783 env->cp15.c15_cpar = value & 0x3fff;
1047b9d7
PM
2784}
2785
2786static const ARMCPRegInfo xscale_cp_reginfo[] = {
2787 { .name = "XSCALE_CPAR",
2788 .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
2789 .fieldoffset = offsetof(CPUARMState, cp15.c15_cpar), .resetvalue = 0,
2790 .writefn = xscale_cpar_write, },
2771db27
PM
2791 { .name = "XSCALE_AUXCR",
2792 .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 1, .access = PL1_RW,
2793 .fieldoffset = offsetof(CPUARMState, cp15.c1_xscaleauxcr),
2794 .resetvalue = 0, },
3b771579
PM
2795 /* XScale specific cache-lockdown: since we have no cache we NOP these
2796 * and hope the guest does not really rely on cache behaviour.
2797 */
2798 { .name = "XSCALE_LOCK_ICACHE_LINE",
2799 .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 0,
2800 .access = PL1_W, .type = ARM_CP_NOP },
2801 { .name = "XSCALE_UNLOCK_ICACHE",
2802 .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 1,
2803 .access = PL1_W, .type = ARM_CP_NOP },
2804 { .name = "XSCALE_DCACHE_LOCK",
2805 .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 0,
2806 .access = PL1_RW, .type = ARM_CP_NOP },
2807 { .name = "XSCALE_UNLOCK_DCACHE",
2808 .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 1,
2809 .access = PL1_W, .type = ARM_CP_NOP },
1047b9d7
PM
2810 REGINFO_SENTINEL
2811};
2812
2813static const ARMCPRegInfo dummy_c15_cp_reginfo[] = {
2814 /* RAZ/WI the whole crn=15 space, when we don't have a more specific
2815 * implementation of this implementation-defined space.
2816 * Ideally this should eventually disappear in favour of actually
2817 * implementing the correct behaviour for all cores.
2818 */
2819 { .name = "C15_IMPDEF", .cp = 15, .crn = 15,
2820 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
3671cd87 2821 .access = PL1_RW,
7a0e58fa 2822 .type = ARM_CP_CONST | ARM_CP_NO_RAW | ARM_CP_OVERRIDE,
d4e6df63 2823 .resetvalue = 0 },
18032bec
PM
2824 REGINFO_SENTINEL
2825};
2826
c4804214
PM
2827static const ARMCPRegInfo cache_dirty_status_cp_reginfo[] = {
2828 /* Cache status: RAZ because we have no cache so it's always clean */
2829 { .name = "CDSR", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 6,
7a0e58fa 2830 .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 2831 .resetvalue = 0 },
c4804214
PM
2832 REGINFO_SENTINEL
2833};
2834
2835static const ARMCPRegInfo cache_block_ops_cp_reginfo[] = {
2836 /* We never have a a block transfer operation in progress */
2837 { .name = "BXSR", .cp = 15, .crn = 7, .crm = 12, .opc1 = 0, .opc2 = 4,
7a0e58fa 2838 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 2839 .resetvalue = 0 },
30b05bba
PM
2840 /* The cache ops themselves: these all NOP for QEMU */
2841 { .name = "IICR", .cp = 15, .crm = 5, .opc1 = 0,
2842 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
2843 { .name = "IDCR", .cp = 15, .crm = 6, .opc1 = 0,
2844 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
2845 { .name = "CDCR", .cp = 15, .crm = 12, .opc1 = 0,
2846 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
2847 { .name = "PIR", .cp = 15, .crm = 12, .opc1 = 1,
2848 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
2849 { .name = "PDR", .cp = 15, .crm = 12, .opc1 = 2,
2850 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
2851 { .name = "CIDCR", .cp = 15, .crm = 14, .opc1 = 0,
2852 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
c4804214
PM
2853 REGINFO_SENTINEL
2854};
2855
2856static const ARMCPRegInfo cache_test_clean_cp_reginfo[] = {
2857 /* The cache test-and-clean instructions always return (1 << 30)
2858 * to indicate that there are no dirty cache lines.
2859 */
2860 { .name = "TC_DCACHE", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 3,
7a0e58fa 2861 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 2862 .resetvalue = (1 << 30) },
c4804214 2863 { .name = "TCI_DCACHE", .cp = 15, .crn = 7, .crm = 14, .opc1 = 0, .opc2 = 3,
7a0e58fa 2864 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 2865 .resetvalue = (1 << 30) },
c4804214
PM
2866 REGINFO_SENTINEL
2867};
2868
34f90529
PM
2869static const ARMCPRegInfo strongarm_cp_reginfo[] = {
2870 /* Ignore ReadBuffer accesses */
2871 { .name = "C9_READBUFFER", .cp = 15, .crn = 9,
2872 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
d4e6df63 2873 .access = PL1_RW, .resetvalue = 0,
7a0e58fa 2874 .type = ARM_CP_CONST | ARM_CP_OVERRIDE | ARM_CP_NO_RAW },
34f90529
PM
2875 REGINFO_SENTINEL
2876};
2877
731de9e6
EI
2878static uint64_t midr_read(CPUARMState *env, const ARMCPRegInfo *ri)
2879{
2880 ARMCPU *cpu = arm_env_get_cpu(env);
2881 unsigned int cur_el = arm_current_el(env);
2882 bool secure = arm_is_secure(env);
2883
2884 if (arm_feature(&cpu->env, ARM_FEATURE_EL2) && !secure && cur_el == 1) {
2885 return env->cp15.vpidr_el2;
2886 }
2887 return raw_read(env, ri);
2888}
2889
06a7e647 2890static uint64_t mpidr_read_val(CPUARMState *env)
81bdde9d 2891{
eb5e1d3c
PF
2892 ARMCPU *cpu = ARM_CPU(arm_env_get_cpu(env));
2893 uint64_t mpidr = cpu->mp_affinity;
2894
81bdde9d 2895 if (arm_feature(env, ARM_FEATURE_V7MP)) {
78dbbbe4 2896 mpidr |= (1U << 31);
81bdde9d
PM
2897 /* Cores which are uniprocessor (non-coherent)
2898 * but still implement the MP extensions set
a8e81b31 2899 * bit 30. (For instance, Cortex-R5).
81bdde9d 2900 */
a8e81b31
PC
2901 if (cpu->mp_is_up) {
2902 mpidr |= (1u << 30);
2903 }
81bdde9d 2904 }
c4241c7d 2905 return mpidr;
81bdde9d
PM
2906}
2907
06a7e647
EI
2908static uint64_t mpidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
2909{
f0d574d6
EI
2910 unsigned int cur_el = arm_current_el(env);
2911 bool secure = arm_is_secure(env);
2912
2913 if (arm_feature(env, ARM_FEATURE_EL2) && !secure && cur_el == 1) {
2914 return env->cp15.vmpidr_el2;
2915 }
06a7e647
EI
2916 return mpidr_read_val(env);
2917}
2918
81bdde9d 2919static const ARMCPRegInfo mpidr_cp_reginfo[] = {
4b7fff2f
PM
2920 { .name = "MPIDR", .state = ARM_CP_STATE_BOTH,
2921 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 5,
7a0e58fa 2922 .access = PL1_R, .readfn = mpidr_read, .type = ARM_CP_NO_RAW },
81bdde9d
PM
2923 REGINFO_SENTINEL
2924};
2925
7ac681cf 2926static const ARMCPRegInfo lpae_cp_reginfo[] = {
a903c449 2927 /* NOP AMAIR0/1 */
b0fe2427
PM
2928 { .name = "AMAIR0", .state = ARM_CP_STATE_BOTH,
2929 .opc0 = 3, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 0,
a903c449 2930 .access = PL1_RW, .type = ARM_CP_CONST,
7ac681cf 2931 .resetvalue = 0 },
b0fe2427 2932 /* AMAIR1 is mapped to AMAIR_EL1[63:32] */
7ac681cf 2933 { .name = "AMAIR1", .cp = 15, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 1,
a903c449 2934 .access = PL1_RW, .type = ARM_CP_CONST,
7ac681cf 2935 .resetvalue = 0 },
891a2fe7 2936 { .name = "PAR", .cp = 15, .crm = 7, .opc1 = 0,
01c097f7
FA
2937 .access = PL1_RW, .type = ARM_CP_64BIT, .resetvalue = 0,
2938 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.par_s),
2939 offsetof(CPUARMState, cp15.par_ns)} },
891a2fe7 2940 { .name = "TTBR0", .cp = 15, .crm = 2, .opc1 = 0,
7a0e58fa 2941 .access = PL1_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
7dd8c9af
FA
2942 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
2943 offsetof(CPUARMState, cp15.ttbr0_ns) },
b061a82b 2944 .writefn = vmsa_ttbr_write, },
891a2fe7 2945 { .name = "TTBR1", .cp = 15, .crm = 2, .opc1 = 1,
7a0e58fa 2946 .access = PL1_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
7dd8c9af
FA
2947 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
2948 offsetof(CPUARMState, cp15.ttbr1_ns) },
b061a82b 2949 .writefn = vmsa_ttbr_write, },
7ac681cf
PM
2950 REGINFO_SENTINEL
2951};
2952
c4241c7d 2953static uint64_t aa64_fpcr_read(CPUARMState *env, const ARMCPRegInfo *ri)
b0d2b7d0 2954{
c4241c7d 2955 return vfp_get_fpcr(env);
b0d2b7d0
PM
2956}
2957
c4241c7d
PM
2958static void aa64_fpcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2959 uint64_t value)
b0d2b7d0
PM
2960{
2961 vfp_set_fpcr(env, value);
b0d2b7d0
PM
2962}
2963
c4241c7d 2964static uint64_t aa64_fpsr_read(CPUARMState *env, const ARMCPRegInfo *ri)
b0d2b7d0 2965{
c4241c7d 2966 return vfp_get_fpsr(env);
b0d2b7d0
PM
2967}
2968
c4241c7d
PM
2969static void aa64_fpsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2970 uint64_t value)
b0d2b7d0
PM
2971{
2972 vfp_set_fpsr(env, value);
b0d2b7d0
PM
2973}
2974
3f208fd7
PM
2975static CPAccessResult aa64_daif_access(CPUARMState *env, const ARMCPRegInfo *ri,
2976 bool isread)
c2b820fe 2977{
137feaa9 2978 if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_UMA)) {
c2b820fe
PM
2979 return CP_ACCESS_TRAP;
2980 }
2981 return CP_ACCESS_OK;
2982}
2983
2984static void aa64_daif_write(CPUARMState *env, const ARMCPRegInfo *ri,
2985 uint64_t value)
2986{
2987 env->daif = value & PSTATE_DAIF;
2988}
2989
8af35c37 2990static CPAccessResult aa64_cacheop_access(CPUARMState *env,
3f208fd7
PM
2991 const ARMCPRegInfo *ri,
2992 bool isread)
8af35c37
PM
2993{
2994 /* Cache invalidate/clean: NOP, but EL0 must UNDEF unless
2995 * SCTLR_EL1.UCI is set.
2996 */
137feaa9 2997 if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_UCI)) {
8af35c37
PM
2998 return CP_ACCESS_TRAP;
2999 }
3000 return CP_ACCESS_OK;
3001}
3002
dbb1fb27
AB
3003/* See: D4.7.2 TLB maintenance requirements and the TLB maintenance instructions
3004 * Page D4-1736 (DDI0487A.b)
3005 */
3006
fd3ed969
PM
3007static void tlbi_aa64_vmalle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
3008 uint64_t value)
168aa23b 3009{
a67cf277 3010 CPUState *cs = ENV_GET_CPU(env);
dbb1fb27 3011
fd3ed969 3012 if (arm_is_secure_below_el3(env)) {
0336cbf8 3013 tlb_flush_by_mmuidx(cs,
8bd5c820
PM
3014 ARMMMUIdxBit_S1SE1 |
3015 ARMMMUIdxBit_S1SE0);
fd3ed969 3016 } else {
0336cbf8 3017 tlb_flush_by_mmuidx(cs,
8bd5c820
PM
3018 ARMMMUIdxBit_S12NSE1 |
3019 ARMMMUIdxBit_S12NSE0);
fd3ed969 3020 }
168aa23b
PM
3021}
3022
fd3ed969
PM
3023static void tlbi_aa64_vmalle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
3024 uint64_t value)
168aa23b 3025{
a67cf277 3026 CPUState *cs = ENV_GET_CPU(env);
fd3ed969 3027 bool sec = arm_is_secure_below_el3(env);
dbb1fb27 3028
a67cf277
AB
3029 if (sec) {
3030 tlb_flush_by_mmuidx_all_cpus_synced(cs,
8bd5c820
PM
3031 ARMMMUIdxBit_S1SE1 |
3032 ARMMMUIdxBit_S1SE0);
a67cf277
AB
3033 } else {
3034 tlb_flush_by_mmuidx_all_cpus_synced(cs,
8bd5c820
PM
3035 ARMMMUIdxBit_S12NSE1 |
3036 ARMMMUIdxBit_S12NSE0);
fd3ed969 3037 }
168aa23b
PM
3038}
3039
fd3ed969
PM
3040static void tlbi_aa64_alle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
3041 uint64_t value)
168aa23b 3042{
fd3ed969
PM
3043 /* Note that the 'ALL' scope must invalidate both stage 1 and
3044 * stage 2 translations, whereas most other scopes only invalidate
3045 * stage 1 translations.
3046 */
00c8cb0a 3047 ARMCPU *cpu = arm_env_get_cpu(env);
fd3ed969
PM
3048 CPUState *cs = CPU(cpu);
3049
3050 if (arm_is_secure_below_el3(env)) {
0336cbf8 3051 tlb_flush_by_mmuidx(cs,
8bd5c820
PM
3052 ARMMMUIdxBit_S1SE1 |
3053 ARMMMUIdxBit_S1SE0);
fd3ed969
PM
3054 } else {
3055 if (arm_feature(env, ARM_FEATURE_EL2)) {
0336cbf8 3056 tlb_flush_by_mmuidx(cs,
8bd5c820
PM
3057 ARMMMUIdxBit_S12NSE1 |
3058 ARMMMUIdxBit_S12NSE0 |
3059 ARMMMUIdxBit_S2NS);
fd3ed969 3060 } else {
0336cbf8 3061 tlb_flush_by_mmuidx(cs,
8bd5c820
PM
3062 ARMMMUIdxBit_S12NSE1 |
3063 ARMMMUIdxBit_S12NSE0);
fd3ed969
PM
3064 }
3065 }
168aa23b
PM
3066}
3067
fd3ed969 3068static void tlbi_aa64_alle2_write(CPUARMState *env, const ARMCPRegInfo *ri,
fa439fc5
PM
3069 uint64_t value)
3070{
fd3ed969
PM
3071 ARMCPU *cpu = arm_env_get_cpu(env);
3072 CPUState *cs = CPU(cpu);
3073
8bd5c820 3074 tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_S1E2);
fd3ed969
PM
3075}
3076
43efaa33
PM
3077static void tlbi_aa64_alle3_write(CPUARMState *env, const ARMCPRegInfo *ri,
3078 uint64_t value)
3079{
3080 ARMCPU *cpu = arm_env_get_cpu(env);
3081 CPUState *cs = CPU(cpu);
3082
8bd5c820 3083 tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_S1E3);
43efaa33
PM
3084}
3085
fd3ed969
PM
3086static void tlbi_aa64_alle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
3087 uint64_t value)
3088{
3089 /* Note that the 'ALL' scope must invalidate both stage 1 and
3090 * stage 2 translations, whereas most other scopes only invalidate
3091 * stage 1 translations.
3092 */
a67cf277 3093 CPUState *cs = ENV_GET_CPU(env);
fd3ed969
PM
3094 bool sec = arm_is_secure_below_el3(env);
3095 bool has_el2 = arm_feature(env, ARM_FEATURE_EL2);
a67cf277
AB
3096
3097 if (sec) {
3098 tlb_flush_by_mmuidx_all_cpus_synced(cs,
8bd5c820
PM
3099 ARMMMUIdxBit_S1SE1 |
3100 ARMMMUIdxBit_S1SE0);
a67cf277
AB
3101 } else if (has_el2) {
3102 tlb_flush_by_mmuidx_all_cpus_synced(cs,
8bd5c820
PM
3103 ARMMMUIdxBit_S12NSE1 |
3104 ARMMMUIdxBit_S12NSE0 |
3105 ARMMMUIdxBit_S2NS);
a67cf277
AB
3106 } else {
3107 tlb_flush_by_mmuidx_all_cpus_synced(cs,
8bd5c820
PM
3108 ARMMMUIdxBit_S12NSE1 |
3109 ARMMMUIdxBit_S12NSE0);
fa439fc5
PM
3110 }
3111}
3112
2bfb9d75
PM
3113static void tlbi_aa64_alle2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
3114 uint64_t value)
3115{
a67cf277 3116 CPUState *cs = ENV_GET_CPU(env);
2bfb9d75 3117
8bd5c820 3118 tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_S1E2);
2bfb9d75
PM
3119}
3120
43efaa33
PM
3121static void tlbi_aa64_alle3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
3122 uint64_t value)
3123{
a67cf277 3124 CPUState *cs = ENV_GET_CPU(env);
43efaa33 3125
8bd5c820 3126 tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_S1E3);
43efaa33
PM
3127}
3128
fd3ed969
PM
3129static void tlbi_aa64_vae1_write(CPUARMState *env, const ARMCPRegInfo *ri,
3130 uint64_t value)
3131{
3132 /* Invalidate by VA, EL1&0 (AArch64 version).
3133 * Currently handles all of VAE1, VAAE1, VAALE1 and VALE1,
3134 * since we don't support flush-for-specific-ASID-only or
3135 * flush-last-level-only.
3136 */
3137 ARMCPU *cpu = arm_env_get_cpu(env);
3138 CPUState *cs = CPU(cpu);
3139 uint64_t pageaddr = sextract64(value << 12, 0, 56);
3140
3141 if (arm_is_secure_below_el3(env)) {
0336cbf8 3142 tlb_flush_page_by_mmuidx(cs, pageaddr,
8bd5c820
PM
3143 ARMMMUIdxBit_S1SE1 |
3144 ARMMMUIdxBit_S1SE0);
fd3ed969 3145 } else {
0336cbf8 3146 tlb_flush_page_by_mmuidx(cs, pageaddr,
8bd5c820
PM
3147 ARMMMUIdxBit_S12NSE1 |
3148 ARMMMUIdxBit_S12NSE0);
fd3ed969
PM
3149 }
3150}
3151
3152static void tlbi_aa64_vae2_write(CPUARMState *env, const ARMCPRegInfo *ri,
3153 uint64_t value)
fa439fc5 3154{
fd3ed969
PM
3155 /* Invalidate by VA, EL2
3156 * Currently handles both VAE2 and VALE2, since we don't support
3157 * flush-last-level-only.
3158 */
3159 ARMCPU *cpu = arm_env_get_cpu(env);
3160 CPUState *cs = CPU(cpu);
3161 uint64_t pageaddr = sextract64(value << 12, 0, 56);
3162
8bd5c820 3163 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_S1E2);
fd3ed969
PM
3164}
3165
43efaa33
PM
3166static void tlbi_aa64_vae3_write(CPUARMState *env, const ARMCPRegInfo *ri,
3167 uint64_t value)
3168{
3169 /* Invalidate by VA, EL3
3170 * Currently handles both VAE3 and VALE3, since we don't support
3171 * flush-last-level-only.
3172 */
3173 ARMCPU *cpu = arm_env_get_cpu(env);
3174 CPUState *cs = CPU(cpu);
3175 uint64_t pageaddr = sextract64(value << 12, 0, 56);
3176
8bd5c820 3177 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_S1E3);
43efaa33
PM
3178}
3179
fd3ed969
PM
3180static void tlbi_aa64_vae1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
3181 uint64_t value)
3182{
a67cf277
AB
3183 ARMCPU *cpu = arm_env_get_cpu(env);
3184 CPUState *cs = CPU(cpu);
fd3ed969 3185 bool sec = arm_is_secure_below_el3(env);
fa439fc5
PM
3186 uint64_t pageaddr = sextract64(value << 12, 0, 56);
3187
a67cf277
AB
3188 if (sec) {
3189 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
8bd5c820
PM
3190 ARMMMUIdxBit_S1SE1 |
3191 ARMMMUIdxBit_S1SE0);
a67cf277
AB
3192 } else {
3193 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
8bd5c820
PM
3194 ARMMMUIdxBit_S12NSE1 |
3195 ARMMMUIdxBit_S12NSE0);
fa439fc5
PM
3196 }
3197}
3198
fd3ed969
PM
3199static void tlbi_aa64_vae2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
3200 uint64_t value)
fa439fc5 3201{
a67cf277 3202 CPUState *cs = ENV_GET_CPU(env);
fd3ed969 3203 uint64_t pageaddr = sextract64(value << 12, 0, 56);
fa439fc5 3204
a67cf277 3205 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
8bd5c820 3206 ARMMMUIdxBit_S1E2);
fa439fc5
PM
3207}
3208
43efaa33
PM
3209static void tlbi_aa64_vae3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
3210 uint64_t value)
3211{
a67cf277 3212 CPUState *cs = ENV_GET_CPU(env);
43efaa33
PM
3213 uint64_t pageaddr = sextract64(value << 12, 0, 56);
3214
a67cf277 3215 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
8bd5c820 3216 ARMMMUIdxBit_S1E3);
43efaa33
PM
3217}
3218
cea66e91
PM
3219static void tlbi_aa64_ipas2e1_write(CPUARMState *env, const ARMCPRegInfo *ri,
3220 uint64_t value)
3221{
3222 /* Invalidate by IPA. This has to invalidate any structures that
3223 * contain only stage 2 translation information, but does not need
3224 * to apply to structures that contain combined stage 1 and stage 2
3225 * translation information.
3226 * This must NOP if EL2 isn't implemented or SCR_EL3.NS is zero.
3227 */
3228 ARMCPU *cpu = arm_env_get_cpu(env);
3229 CPUState *cs = CPU(cpu);
3230 uint64_t pageaddr;
3231
3232 if (!arm_feature(env, ARM_FEATURE_EL2) || !(env->cp15.scr_el3 & SCR_NS)) {
3233 return;
3234 }
3235
3236 pageaddr = sextract64(value << 12, 0, 48);
3237
8bd5c820 3238 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_S2NS);
cea66e91
PM
3239}
3240
3241static void tlbi_aa64_ipas2e1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
3242 uint64_t value)
3243{
a67cf277 3244 CPUState *cs = ENV_GET_CPU(env);
cea66e91
PM
3245 uint64_t pageaddr;
3246
3247 if (!arm_feature(env, ARM_FEATURE_EL2) || !(env->cp15.scr_el3 & SCR_NS)) {
3248 return;
3249 }
3250
3251 pageaddr = sextract64(value << 12, 0, 48);
3252
a67cf277 3253 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
8bd5c820 3254 ARMMMUIdxBit_S2NS);
cea66e91
PM
3255}
3256
3f208fd7
PM
3257static CPAccessResult aa64_zva_access(CPUARMState *env, const ARMCPRegInfo *ri,
3258 bool isread)
aca3f40b
PM
3259{
3260 /* We don't implement EL2, so the only control on DC ZVA is the
3261 * bit in the SCTLR which can prohibit access for EL0.
3262 */
137feaa9 3263 if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_DZE)) {
aca3f40b
PM
3264 return CP_ACCESS_TRAP;
3265 }
3266 return CP_ACCESS_OK;
3267}
3268
3269static uint64_t aa64_dczid_read(CPUARMState *env, const ARMCPRegInfo *ri)
3270{
3271 ARMCPU *cpu = arm_env_get_cpu(env);
3272 int dzp_bit = 1 << 4;
3273
3274 /* DZP indicates whether DC ZVA access is allowed */
3f208fd7 3275 if (aa64_zva_access(env, NULL, false) == CP_ACCESS_OK) {
aca3f40b
PM
3276 dzp_bit = 0;
3277 }
3278 return cpu->dcz_blocksize | dzp_bit;
3279}
3280
3f208fd7
PM
3281static CPAccessResult sp_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
3282 bool isread)
f502cfc2 3283{
cdcf1405 3284 if (!(env->pstate & PSTATE_SP)) {
f502cfc2
PM
3285 /* Access to SP_EL0 is undefined if it's being used as
3286 * the stack pointer.
3287 */
3288 return CP_ACCESS_TRAP_UNCATEGORIZED;
3289 }
3290 return CP_ACCESS_OK;
3291}
3292
3293static uint64_t spsel_read(CPUARMState *env, const ARMCPRegInfo *ri)
3294{
3295 return env->pstate & PSTATE_SP;
3296}
3297
3298static void spsel_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
3299{
3300 update_spsel(env, val);
3301}
3302
137feaa9
FA
3303static void sctlr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3304 uint64_t value)
3305{
3306 ARMCPU *cpu = arm_env_get_cpu(env);
3307
3308 if (raw_read(env, ri) == value) {
3309 /* Skip the TLB flush if nothing actually changed; Linux likes
3310 * to do a lot of pointless SCTLR writes.
3311 */
3312 return;
3313 }
3314
06312feb
PM
3315 if (arm_feature(env, ARM_FEATURE_PMSA) && !cpu->has_mpu) {
3316 /* M bit is RAZ/WI for PMSA with no MPU implemented */
3317 value &= ~SCTLR_M;
3318 }
3319
137feaa9
FA
3320 raw_write(env, ri, value);
3321 /* ??? Lots of these bits are not implemented. */
3322 /* This may enable/disable the MMU, so do a TLB flush. */
d10eb08f 3323 tlb_flush(CPU(cpu));
137feaa9
FA
3324}
3325
3f208fd7
PM
3326static CPAccessResult fpexc32_access(CPUARMState *env, const ARMCPRegInfo *ri,
3327 bool isread)
03fbf20f
PM
3328{
3329 if ((env->cp15.cptr_el[2] & CPTR_TFP) && arm_current_el(env) == 2) {
f2cae609 3330 return CP_ACCESS_TRAP_FP_EL2;
03fbf20f
PM
3331 }
3332 if (env->cp15.cptr_el[3] & CPTR_TFP) {
f2cae609 3333 return CP_ACCESS_TRAP_FP_EL3;
03fbf20f
PM
3334 }
3335 return CP_ACCESS_OK;
3336}
3337
a8d64e73
PM
3338static void sdcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3339 uint64_t value)
3340{
3341 env->cp15.mdcr_el3 = value & SDCR_VALID_MASK;
3342}
3343
b0d2b7d0
PM
3344static const ARMCPRegInfo v8_cp_reginfo[] = {
3345 /* Minimal set of EL0-visible registers. This will need to be expanded
3346 * significantly for system emulation of AArch64 CPUs.
3347 */
3348 { .name = "NZCV", .state = ARM_CP_STATE_AA64,
3349 .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 2,
3350 .access = PL0_RW, .type = ARM_CP_NZCV },
c2b820fe
PM
3351 { .name = "DAIF", .state = ARM_CP_STATE_AA64,
3352 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 2,
7a0e58fa 3353 .type = ARM_CP_NO_RAW,
c2b820fe
PM
3354 .access = PL0_RW, .accessfn = aa64_daif_access,
3355 .fieldoffset = offsetof(CPUARMState, daif),
3356 .writefn = aa64_daif_write, .resetfn = arm_cp_reset_ignore },
b0d2b7d0
PM
3357 { .name = "FPCR", .state = ARM_CP_STATE_AA64,
3358 .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 4,
fe03d45f
RH
3359 .access = PL0_RW, .type = ARM_CP_FPU,
3360 .readfn = aa64_fpcr_read, .writefn = aa64_fpcr_write },
b0d2b7d0
PM
3361 { .name = "FPSR", .state = ARM_CP_STATE_AA64,
3362 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 4,
fe03d45f
RH
3363 .access = PL0_RW, .type = ARM_CP_FPU,
3364 .readfn = aa64_fpsr_read, .writefn = aa64_fpsr_write },
b0d2b7d0
PM
3365 { .name = "DCZID_EL0", .state = ARM_CP_STATE_AA64,
3366 .opc0 = 3, .opc1 = 3, .opc2 = 7, .crn = 0, .crm = 0,
7a0e58fa 3367 .access = PL0_R, .type = ARM_CP_NO_RAW,
aca3f40b
PM
3368 .readfn = aa64_dczid_read },
3369 { .name = "DC_ZVA", .state = ARM_CP_STATE_AA64,
3370 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 1,
3371 .access = PL0_W, .type = ARM_CP_DC_ZVA,
3372#ifndef CONFIG_USER_ONLY
3373 /* Avoid overhead of an access check that always passes in user-mode */
3374 .accessfn = aa64_zva_access,
3375#endif
3376 },
0eef9d98
PM
3377 { .name = "CURRENTEL", .state = ARM_CP_STATE_AA64,
3378 .opc0 = 3, .opc1 = 0, .opc2 = 2, .crn = 4, .crm = 2,
3379 .access = PL1_R, .type = ARM_CP_CURRENTEL },
8af35c37
PM
3380 /* Cache ops: all NOPs since we don't emulate caches */
3381 { .name = "IC_IALLUIS", .state = ARM_CP_STATE_AA64,
3382 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
3383 .access = PL1_W, .type = ARM_CP_NOP },
3384 { .name = "IC_IALLU", .state = ARM_CP_STATE_AA64,
3385 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
3386 .access = PL1_W, .type = ARM_CP_NOP },
3387 { .name = "IC_IVAU", .state = ARM_CP_STATE_AA64,
3388 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 5, .opc2 = 1,
3389 .access = PL0_W, .type = ARM_CP_NOP,
3390 .accessfn = aa64_cacheop_access },
3391 { .name = "DC_IVAC", .state = ARM_CP_STATE_AA64,
3392 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
3393 .access = PL1_W, .type = ARM_CP_NOP },
3394 { .name = "DC_ISW", .state = ARM_CP_STATE_AA64,
3395 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
3396 .access = PL1_W, .type = ARM_CP_NOP },
3397 { .name = "DC_CVAC", .state = ARM_CP_STATE_AA64,
3398 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 1,
3399 .access = PL0_W, .type = ARM_CP_NOP,
3400 .accessfn = aa64_cacheop_access },
3401 { .name = "DC_CSW", .state = ARM_CP_STATE_AA64,
3402 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
3403 .access = PL1_W, .type = ARM_CP_NOP },
3404 { .name = "DC_CVAU", .state = ARM_CP_STATE_AA64,
3405 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 11, .opc2 = 1,
3406 .access = PL0_W, .type = ARM_CP_NOP,
3407 .accessfn = aa64_cacheop_access },
3408 { .name = "DC_CIVAC", .state = ARM_CP_STATE_AA64,
3409 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 1,
3410 .access = PL0_W, .type = ARM_CP_NOP,
3411 .accessfn = aa64_cacheop_access },
3412 { .name = "DC_CISW", .state = ARM_CP_STATE_AA64,
3413 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
3414 .access = PL1_W, .type = ARM_CP_NOP },
168aa23b
PM
3415 /* TLBI operations */
3416 { .name = "TLBI_VMALLE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 3417 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
7a0e58fa 3418 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 3419 .writefn = tlbi_aa64_vmalle1is_write },
168aa23b 3420 { .name = "TLBI_VAE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 3421 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
7a0e58fa 3422 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 3423 .writefn = tlbi_aa64_vae1is_write },
168aa23b 3424 { .name = "TLBI_ASIDE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 3425 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
7a0e58fa 3426 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 3427 .writefn = tlbi_aa64_vmalle1is_write },
168aa23b 3428 { .name = "TLBI_VAAE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 3429 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
7a0e58fa 3430 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 3431 .writefn = tlbi_aa64_vae1is_write },
168aa23b 3432 { .name = "TLBI_VALE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 3433 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
7a0e58fa 3434 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 3435 .writefn = tlbi_aa64_vae1is_write },
168aa23b 3436 { .name = "TLBI_VAALE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 3437 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
7a0e58fa 3438 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 3439 .writefn = tlbi_aa64_vae1is_write },
168aa23b 3440 { .name = "TLBI_VMALLE1", .state = ARM_CP_STATE_AA64,
6ab9f499 3441 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
7a0e58fa 3442 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 3443 .writefn = tlbi_aa64_vmalle1_write },
168aa23b 3444 { .name = "TLBI_VAE1", .state = ARM_CP_STATE_AA64,
6ab9f499 3445 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
7a0e58fa 3446 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 3447 .writefn = tlbi_aa64_vae1_write },
168aa23b 3448 { .name = "TLBI_ASIDE1", .state = ARM_CP_STATE_AA64,
6ab9f499 3449 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
7a0e58fa 3450 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 3451 .writefn = tlbi_aa64_vmalle1_write },
168aa23b 3452 { .name = "TLBI_VAAE1", .state = ARM_CP_STATE_AA64,
6ab9f499 3453 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
7a0e58fa 3454 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 3455 .writefn = tlbi_aa64_vae1_write },
168aa23b 3456 { .name = "TLBI_VALE1", .state = ARM_CP_STATE_AA64,
6ab9f499 3457 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
7a0e58fa 3458 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 3459 .writefn = tlbi_aa64_vae1_write },
168aa23b 3460 { .name = "TLBI_VAALE1", .state = ARM_CP_STATE_AA64,
6ab9f499 3461 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
7a0e58fa 3462 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 3463 .writefn = tlbi_aa64_vae1_write },
cea66e91
PM
3464 { .name = "TLBI_IPAS2E1IS", .state = ARM_CP_STATE_AA64,
3465 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1,
3466 .access = PL2_W, .type = ARM_CP_NO_RAW,
3467 .writefn = tlbi_aa64_ipas2e1is_write },
3468 { .name = "TLBI_IPAS2LE1IS", .state = ARM_CP_STATE_AA64,
3469 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5,
3470 .access = PL2_W, .type = ARM_CP_NO_RAW,
3471 .writefn = tlbi_aa64_ipas2e1is_write },
83ddf975
PM
3472 { .name = "TLBI_ALLE1IS", .state = ARM_CP_STATE_AA64,
3473 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
3474 .access = PL2_W, .type = ARM_CP_NO_RAW,
fd3ed969 3475 .writefn = tlbi_aa64_alle1is_write },
43efaa33
PM
3476 { .name = "TLBI_VMALLS12E1IS", .state = ARM_CP_STATE_AA64,
3477 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 6,
3478 .access = PL2_W, .type = ARM_CP_NO_RAW,
3479 .writefn = tlbi_aa64_alle1is_write },
cea66e91
PM
3480 { .name = "TLBI_IPAS2E1", .state = ARM_CP_STATE_AA64,
3481 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1,
3482 .access = PL2_W, .type = ARM_CP_NO_RAW,
3483 .writefn = tlbi_aa64_ipas2e1_write },
3484 { .name = "TLBI_IPAS2LE1", .state = ARM_CP_STATE_AA64,
3485 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5,
3486 .access = PL2_W, .type = ARM_CP_NO_RAW,
3487 .writefn = tlbi_aa64_ipas2e1_write },
83ddf975
PM
3488 { .name = "TLBI_ALLE1", .state = ARM_CP_STATE_AA64,
3489 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
3490 .access = PL2_W, .type = ARM_CP_NO_RAW,
fd3ed969 3491 .writefn = tlbi_aa64_alle1_write },
43efaa33
PM
3492 { .name = "TLBI_VMALLS12E1", .state = ARM_CP_STATE_AA64,
3493 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 6,
3494 .access = PL2_W, .type = ARM_CP_NO_RAW,
3495 .writefn = tlbi_aa64_alle1is_write },
19525524
PM
3496#ifndef CONFIG_USER_ONLY
3497 /* 64 bit address translation operations */
3498 { .name = "AT_S1E1R", .state = ARM_CP_STATE_AA64,
3499 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 0,
060e8a48 3500 .access = PL1_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
19525524
PM
3501 { .name = "AT_S1E1W", .state = ARM_CP_STATE_AA64,
3502 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 1,
060e8a48 3503 .access = PL1_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
19525524
PM
3504 { .name = "AT_S1E0R", .state = ARM_CP_STATE_AA64,
3505 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 2,
060e8a48 3506 .access = PL1_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
19525524
PM
3507 { .name = "AT_S1E0W", .state = ARM_CP_STATE_AA64,
3508 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 3,
060e8a48 3509 .access = PL1_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
2a47df95 3510 { .name = "AT_S12E1R", .state = ARM_CP_STATE_AA64,
7a379c7e 3511 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 4,
2a47df95
PM
3512 .access = PL2_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3513 { .name = "AT_S12E1W", .state = ARM_CP_STATE_AA64,
7a379c7e 3514 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 5,
2a47df95
PM
3515 .access = PL2_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3516 { .name = "AT_S12E0R", .state = ARM_CP_STATE_AA64,
7a379c7e 3517 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 6,
2a47df95
PM
3518 .access = PL2_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3519 { .name = "AT_S12E0W", .state = ARM_CP_STATE_AA64,
7a379c7e 3520 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 7,
2a47df95
PM
3521 .access = PL2_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3522 /* AT S1E2* are elsewhere as they UNDEF from EL3 if EL2 is not present */
3523 { .name = "AT_S1E3R", .state = ARM_CP_STATE_AA64,
3524 .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 0,
3525 .access = PL3_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3526 { .name = "AT_S1E3W", .state = ARM_CP_STATE_AA64,
3527 .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 1,
3528 .access = PL3_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
c96fc9b5
EI
3529 { .name = "PAR_EL1", .state = ARM_CP_STATE_AA64,
3530 .type = ARM_CP_ALIAS,
3531 .opc0 = 3, .opc1 = 0, .crn = 7, .crm = 4, .opc2 = 0,
3532 .access = PL1_RW, .resetvalue = 0,
3533 .fieldoffset = offsetof(CPUARMState, cp15.par_el[1]),
3534 .writefn = par_write },
19525524 3535#endif
995939a6 3536 /* TLB invalidate last level of translation table walk */
9449fdf6 3537 { .name = "TLBIMVALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
7a0e58fa 3538 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_is_write },
9449fdf6 3539 { .name = "TLBIMVAALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
7a0e58fa 3540 .type = ARM_CP_NO_RAW, .access = PL1_W,
fa439fc5 3541 .writefn = tlbimvaa_is_write },
9449fdf6 3542 { .name = "TLBIMVAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
7a0e58fa 3543 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write },
9449fdf6 3544 { .name = "TLBIMVAAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
7a0e58fa 3545 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimvaa_write },
541ef8c2
SS
3546 { .name = "TLBIMVALH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
3547 .type = ARM_CP_NO_RAW, .access = PL2_W,
3548 .writefn = tlbimva_hyp_write },
3549 { .name = "TLBIMVALHIS",
3550 .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
3551 .type = ARM_CP_NO_RAW, .access = PL2_W,
3552 .writefn = tlbimva_hyp_is_write },
3553 { .name = "TLBIIPAS2",
3554 .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1,
3555 .type = ARM_CP_NO_RAW, .access = PL2_W,
3556 .writefn = tlbiipas2_write },
3557 { .name = "TLBIIPAS2IS",
3558 .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1,
3559 .type = ARM_CP_NO_RAW, .access = PL2_W,
3560 .writefn = tlbiipas2_is_write },
3561 { .name = "TLBIIPAS2L",
3562 .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5,
3563 .type = ARM_CP_NO_RAW, .access = PL2_W,
3564 .writefn = tlbiipas2_write },
3565 { .name = "TLBIIPAS2LIS",
3566 .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5,
3567 .type = ARM_CP_NO_RAW, .access = PL2_W,
3568 .writefn = tlbiipas2_is_write },
9449fdf6
PM
3569 /* 32 bit cache operations */
3570 { .name = "ICIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
3571 .type = ARM_CP_NOP, .access = PL1_W },
3572 { .name = "BPIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 6,
3573 .type = ARM_CP_NOP, .access = PL1_W },
3574 { .name = "ICIALLU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
3575 .type = ARM_CP_NOP, .access = PL1_W },
3576 { .name = "ICIMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 1,
3577 .type = ARM_CP_NOP, .access = PL1_W },
3578 { .name = "BPIALL", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 6,
3579 .type = ARM_CP_NOP, .access = PL1_W },
3580 { .name = "BPIMVA", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 7,
3581 .type = ARM_CP_NOP, .access = PL1_W },
3582 { .name = "DCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
3583 .type = ARM_CP_NOP, .access = PL1_W },
3584 { .name = "DCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
3585 .type = ARM_CP_NOP, .access = PL1_W },
3586 { .name = "DCCMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 1,
3587 .type = ARM_CP_NOP, .access = PL1_W },
3588 { .name = "DCCSW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
3589 .type = ARM_CP_NOP, .access = PL1_W },
3590 { .name = "DCCMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 11, .opc2 = 1,
3591 .type = ARM_CP_NOP, .access = PL1_W },
3592 { .name = "DCCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 1,
3593 .type = ARM_CP_NOP, .access = PL1_W },
3594 { .name = "DCCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
3595 .type = ARM_CP_NOP, .access = PL1_W },
3596 /* MMU Domain access control / MPU write buffer control */
0c17d68c
FA
3597 { .name = "DACR", .cp = 15, .opc1 = 0, .crn = 3, .crm = 0, .opc2 = 0,
3598 .access = PL1_RW, .resetvalue = 0,
3599 .writefn = dacr_write, .raw_writefn = raw_write,
3600 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
3601 offsetoflow32(CPUARMState, cp15.dacr_ns) } },
a0618a19 3602 { .name = "ELR_EL1", .state = ARM_CP_STATE_AA64,
7a0e58fa 3603 .type = ARM_CP_ALIAS,
a0618a19 3604 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 1,
6947f059
EI
3605 .access = PL1_RW,
3606 .fieldoffset = offsetof(CPUARMState, elr_el[1]) },
a65f1de9 3607 { .name = "SPSR_EL1", .state = ARM_CP_STATE_AA64,
7a0e58fa 3608 .type = ARM_CP_ALIAS,
a65f1de9 3609 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 0,
99a99c1f
SB
3610 .access = PL1_RW,
3611 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_SVC]) },
f502cfc2
PM
3612 /* We rely on the access checks not allowing the guest to write to the
3613 * state field when SPSel indicates that it's being used as the stack
3614 * pointer.
3615 */
3616 { .name = "SP_EL0", .state = ARM_CP_STATE_AA64,
3617 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 1, .opc2 = 0,
3618 .access = PL1_RW, .accessfn = sp_el0_access,
7a0e58fa 3619 .type = ARM_CP_ALIAS,
f502cfc2 3620 .fieldoffset = offsetof(CPUARMState, sp_el[0]) },
884b4dee
GB
3621 { .name = "SP_EL1", .state = ARM_CP_STATE_AA64,
3622 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 1, .opc2 = 0,
7a0e58fa 3623 .access = PL2_RW, .type = ARM_CP_ALIAS,
884b4dee 3624 .fieldoffset = offsetof(CPUARMState, sp_el[1]) },
f502cfc2
PM
3625 { .name = "SPSel", .state = ARM_CP_STATE_AA64,
3626 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 0,
7a0e58fa 3627 .type = ARM_CP_NO_RAW,
f502cfc2 3628 .access = PL1_RW, .readfn = spsel_read, .writefn = spsel_write },
03fbf20f
PM
3629 { .name = "FPEXC32_EL2", .state = ARM_CP_STATE_AA64,
3630 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 3, .opc2 = 0,
3631 .type = ARM_CP_ALIAS,
3632 .fieldoffset = offsetof(CPUARMState, vfp.xregs[ARM_VFP_FPEXC]),
3633 .access = PL2_RW, .accessfn = fpexc32_access },
6a43e0b6
PM
3634 { .name = "DACR32_EL2", .state = ARM_CP_STATE_AA64,
3635 .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 0, .opc2 = 0,
3636 .access = PL2_RW, .resetvalue = 0,
3637 .writefn = dacr_write, .raw_writefn = raw_write,
3638 .fieldoffset = offsetof(CPUARMState, cp15.dacr32_el2) },
3639 { .name = "IFSR32_EL2", .state = ARM_CP_STATE_AA64,
3640 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 0, .opc2 = 1,
3641 .access = PL2_RW, .resetvalue = 0,
3642 .fieldoffset = offsetof(CPUARMState, cp15.ifsr32_el2) },
3643 { .name = "SPSR_IRQ", .state = ARM_CP_STATE_AA64,
3644 .type = ARM_CP_ALIAS,
3645 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 0,
3646 .access = PL2_RW,
3647 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_IRQ]) },
3648 { .name = "SPSR_ABT", .state = ARM_CP_STATE_AA64,
3649 .type = ARM_CP_ALIAS,
3650 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 1,
3651 .access = PL2_RW,
3652 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_ABT]) },
3653 { .name = "SPSR_UND", .state = ARM_CP_STATE_AA64,
3654 .type = ARM_CP_ALIAS,
3655 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 2,
3656 .access = PL2_RW,
3657 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_UND]) },
3658 { .name = "SPSR_FIQ", .state = ARM_CP_STATE_AA64,
3659 .type = ARM_CP_ALIAS,
3660 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 3,
3661 .access = PL2_RW,
3662 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_FIQ]) },
a8d64e73
PM
3663 { .name = "MDCR_EL3", .state = ARM_CP_STATE_AA64,
3664 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 3, .opc2 = 1,
3665 .resetvalue = 0,
3666 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el3) },
3667 { .name = "SDCR", .type = ARM_CP_ALIAS,
3668 .cp = 15, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 1,
3669 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
3670 .writefn = sdcr_write,
3671 .fieldoffset = offsetoflow32(CPUARMState, cp15.mdcr_el3) },
b0d2b7d0
PM
3672 REGINFO_SENTINEL
3673};
3674
d42e3c26 3675/* Used to describe the behaviour of EL2 regs when EL2 does not exist. */
4771cd01 3676static const ARMCPRegInfo el3_no_el2_cp_reginfo[] = {
d42e3c26
EI
3677 { .name = "VBAR_EL2", .state = ARM_CP_STATE_AA64,
3678 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0,
3679 .access = PL2_RW,
3680 .readfn = arm_cp_read_zero, .writefn = arm_cp_write_ignore },
f149e3e8 3681 { .name = "HCR_EL2", .state = ARM_CP_STATE_AA64,
7a0e58fa 3682 .type = ARM_CP_NO_RAW,
f149e3e8
EI
3683 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
3684 .access = PL2_RW,
3685 .readfn = arm_cp_read_zero, .writefn = arm_cp_write_ignore },
c6f19164
GB
3686 { .name = "CPTR_EL2", .state = ARM_CP_STATE_BOTH,
3687 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 2,
3688 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
95f949ac
EI
3689 { .name = "MAIR_EL2", .state = ARM_CP_STATE_BOTH,
3690 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 0,
3691 .access = PL2_RW, .type = ARM_CP_CONST,
3692 .resetvalue = 0 },
3693 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
3694 .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 1,
3695 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
2179ef95
PM
3696 { .name = "AMAIR_EL2", .state = ARM_CP_STATE_BOTH,
3697 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 0,
3698 .access = PL2_RW, .type = ARM_CP_CONST,
3699 .resetvalue = 0 },
3700 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
3701 .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 1,
3702 .access = PL2_RW, .type = ARM_CP_CONST,
3703 .resetvalue = 0 },
37cd6c24
PM
3704 { .name = "AFSR0_EL2", .state = ARM_CP_STATE_BOTH,
3705 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 0,
3706 .access = PL2_RW, .type = ARM_CP_CONST,
3707 .resetvalue = 0 },
3708 { .name = "AFSR1_EL2", .state = ARM_CP_STATE_BOTH,
3709 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 1,
3710 .access = PL2_RW, .type = ARM_CP_CONST,
3711 .resetvalue = 0 },
06ec4c8c
EI
3712 { .name = "TCR_EL2", .state = ARM_CP_STATE_BOTH,
3713 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 2,
3714 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
68e9c2fe
EI
3715 { .name = "VTCR_EL2", .state = ARM_CP_STATE_BOTH,
3716 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
3717 .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any,
3718 .type = ARM_CP_CONST, .resetvalue = 0 },
b698e9cf
EI
3719 { .name = "VTTBR", .state = ARM_CP_STATE_AA32,
3720 .cp = 15, .opc1 = 6, .crm = 2,
3721 .access = PL2_RW, .accessfn = access_el3_aa32ns,
3722 .type = ARM_CP_CONST | ARM_CP_64BIT, .resetvalue = 0 },
3723 { .name = "VTTBR_EL2", .state = ARM_CP_STATE_AA64,
3724 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 0,
3725 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
b9cb5323
EI
3726 { .name = "SCTLR_EL2", .state = ARM_CP_STATE_BOTH,
3727 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 0,
3728 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
ff05f37b
EI
3729 { .name = "TPIDR_EL2", .state = ARM_CP_STATE_BOTH,
3730 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 2,
3731 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
a57633c0
EI
3732 { .name = "TTBR0_EL2", .state = ARM_CP_STATE_AA64,
3733 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
3734 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3735 { .name = "HTTBR", .cp = 15, .opc1 = 4, .crm = 2,
3736 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_CONST,
3737 .resetvalue = 0 },
0b6440af
EI
3738 { .name = "CNTHCTL_EL2", .state = ARM_CP_STATE_BOTH,
3739 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 1, .opc2 = 0,
3740 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
edac4d8a
EI
3741 { .name = "CNTVOFF_EL2", .state = ARM_CP_STATE_AA64,
3742 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 3,
3743 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3744 { .name = "CNTVOFF", .cp = 15, .opc1 = 4, .crm = 14,
3745 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_CONST,
3746 .resetvalue = 0 },
b0e66d95
EI
3747 { .name = "CNTHP_CVAL_EL2", .state = ARM_CP_STATE_AA64,
3748 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 2,
3749 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3750 { .name = "CNTHP_CVAL", .cp = 15, .opc1 = 6, .crm = 14,
3751 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_CONST,
3752 .resetvalue = 0 },
3753 { .name = "CNTHP_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
3754 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 0,
3755 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3756 { .name = "CNTHP_CTL_EL2", .state = ARM_CP_STATE_BOTH,
3757 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 1,
3758 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
14cc7b54
SF
3759 { .name = "MDCR_EL2", .state = ARM_CP_STATE_BOTH,
3760 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 1,
d6c8cf81
PM
3761 .access = PL2_RW, .accessfn = access_tda,
3762 .type = ARM_CP_CONST, .resetvalue = 0 },
59e05530
EI
3763 { .name = "HPFAR_EL2", .state = ARM_CP_STATE_BOTH,
3764 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
3765 .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any,
3766 .type = ARM_CP_CONST, .resetvalue = 0 },
2a5a9abd
AF
3767 { .name = "HSTR_EL2", .state = ARM_CP_STATE_BOTH,
3768 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 3,
3769 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
d42e3c26
EI
3770 REGINFO_SENTINEL
3771};
3772
f149e3e8
EI
3773static void hcr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
3774{
3775 ARMCPU *cpu = arm_env_get_cpu(env);
3776 uint64_t valid_mask = HCR_MASK;
3777
3778 if (arm_feature(env, ARM_FEATURE_EL3)) {
3779 valid_mask &= ~HCR_HCD;
77077a83
JK
3780 } else if (cpu->psci_conduit != QEMU_PSCI_CONDUIT_SMC) {
3781 /* Architecturally HCR.TSC is RES0 if EL3 is not implemented.
3782 * However, if we're using the SMC PSCI conduit then QEMU is
3783 * effectively acting like EL3 firmware and so the guest at
3784 * EL2 should retain the ability to prevent EL1 from being
3785 * able to make SMC calls into the ersatz firmware, so in
3786 * that case HCR.TSC should be read/write.
3787 */
f149e3e8
EI
3788 valid_mask &= ~HCR_TSC;
3789 }
3790
3791 /* Clear RES0 bits. */
3792 value &= valid_mask;
3793
3794 /* These bits change the MMU setup:
3795 * HCR_VM enables stage 2 translation
3796 * HCR_PTW forbids certain page-table setups
3797 * HCR_DC Disables stage1 and enables stage2 translation
3798 */
3799 if ((raw_read(env, ri) ^ value) & (HCR_VM | HCR_PTW | HCR_DC)) {
d10eb08f 3800 tlb_flush(CPU(cpu));
f149e3e8
EI
3801 }
3802 raw_write(env, ri, value);
3803}
3804
4771cd01 3805static const ARMCPRegInfo el2_cp_reginfo[] = {
f149e3e8
EI
3806 { .name = "HCR_EL2", .state = ARM_CP_STATE_AA64,
3807 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
3808 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
3809 .writefn = hcr_write },
3b685ba7 3810 { .name = "ELR_EL2", .state = ARM_CP_STATE_AA64,
7a0e58fa 3811 .type = ARM_CP_ALIAS,
3b685ba7
EI
3812 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 1,
3813 .access = PL2_RW,
3814 .fieldoffset = offsetof(CPUARMState, elr_el[2]) },
f2c30f42 3815 { .name = "ESR_EL2", .state = ARM_CP_STATE_AA64,
f2c30f42
EI
3816 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 0,
3817 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[2]) },
63b60551
EI
3818 { .name = "FAR_EL2", .state = ARM_CP_STATE_AA64,
3819 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 0,
3820 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[2]) },
3b685ba7 3821 { .name = "SPSR_EL2", .state = ARM_CP_STATE_AA64,
7a0e58fa 3822 .type = ARM_CP_ALIAS,
3b685ba7 3823 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 0,
99a99c1f
SB
3824 .access = PL2_RW,
3825 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_HYP]) },
d42e3c26
EI
3826 { .name = "VBAR_EL2", .state = ARM_CP_STATE_AA64,
3827 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0,
3828 .access = PL2_RW, .writefn = vbar_write,
3829 .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[2]),
3830 .resetvalue = 0 },
884b4dee
GB
3831 { .name = "SP_EL2", .state = ARM_CP_STATE_AA64,
3832 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 1, .opc2 = 0,
7a0e58fa 3833 .access = PL3_RW, .type = ARM_CP_ALIAS,
884b4dee 3834 .fieldoffset = offsetof(CPUARMState, sp_el[2]) },
c6f19164
GB
3835 { .name = "CPTR_EL2", .state = ARM_CP_STATE_BOTH,
3836 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 2,
3837 .access = PL2_RW, .accessfn = cptr_access, .resetvalue = 0,
3838 .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[2]) },
95f949ac
EI
3839 { .name = "MAIR_EL2", .state = ARM_CP_STATE_BOTH,
3840 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 0,
3841 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[2]),
3842 .resetvalue = 0 },
3843 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
3844 .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 1,
3845 .access = PL2_RW, .type = ARM_CP_ALIAS,
3846 .fieldoffset = offsetofhigh32(CPUARMState, cp15.mair_el[2]) },
2179ef95
PM
3847 { .name = "AMAIR_EL2", .state = ARM_CP_STATE_BOTH,
3848 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 0,
3849 .access = PL2_RW, .type = ARM_CP_CONST,
3850 .resetvalue = 0 },
3851 /* HAMAIR1 is mapped to AMAIR_EL2[63:32] */
3852 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
3853 .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 1,
3854 .access = PL2_RW, .type = ARM_CP_CONST,
3855 .resetvalue = 0 },
37cd6c24
PM
3856 { .name = "AFSR0_EL2", .state = ARM_CP_STATE_BOTH,
3857 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 0,
3858 .access = PL2_RW, .type = ARM_CP_CONST,
3859 .resetvalue = 0 },
3860 { .name = "AFSR1_EL2", .state = ARM_CP_STATE_BOTH,
3861 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 1,
3862 .access = PL2_RW, .type = ARM_CP_CONST,
3863 .resetvalue = 0 },
06ec4c8c
EI
3864 { .name = "TCR_EL2", .state = ARM_CP_STATE_BOTH,
3865 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 2,
6459b94c
PM
3866 .access = PL2_RW,
3867 /* no .writefn needed as this can't cause an ASID change;
3868 * no .raw_writefn or .resetfn needed as we never use mask/base_mask
3869 */
06ec4c8c 3870 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[2]) },
68e9c2fe
EI
3871 { .name = "VTCR", .state = ARM_CP_STATE_AA32,
3872 .cp = 15, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
bf06c112 3873 .type = ARM_CP_ALIAS,
68e9c2fe
EI
3874 .access = PL2_RW, .accessfn = access_el3_aa32ns,
3875 .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) },
3876 { .name = "VTCR_EL2", .state = ARM_CP_STATE_AA64,
3877 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
bf06c112
PM
3878 .access = PL2_RW,
3879 /* no .writefn needed as this can't cause an ASID change;
3880 * no .raw_writefn or .resetfn needed as we never use mask/base_mask
3881 */
68e9c2fe 3882 .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) },
b698e9cf
EI
3883 { .name = "VTTBR", .state = ARM_CP_STATE_AA32,
3884 .cp = 15, .opc1 = 6, .crm = 2,
3885 .type = ARM_CP_64BIT | ARM_CP_ALIAS,
3886 .access = PL2_RW, .accessfn = access_el3_aa32ns,
3887 .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2),
3888 .writefn = vttbr_write },
3889 { .name = "VTTBR_EL2", .state = ARM_CP_STATE_AA64,
3890 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 0,
3891 .access = PL2_RW, .writefn = vttbr_write,
3892 .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2) },
b9cb5323
EI
3893 { .name = "SCTLR_EL2", .state = ARM_CP_STATE_BOTH,
3894 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 0,
3895 .access = PL2_RW, .raw_writefn = raw_write, .writefn = sctlr_write,
3896 .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[2]) },
ff05f37b
EI
3897 { .name = "TPIDR_EL2", .state = ARM_CP_STATE_BOTH,
3898 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 2,
3899 .access = PL2_RW, .resetvalue = 0,
3900 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[2]) },
a57633c0
EI
3901 { .name = "TTBR0_EL2", .state = ARM_CP_STATE_AA64,
3902 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
3903 .access = PL2_RW, .resetvalue = 0,
3904 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
3905 { .name = "HTTBR", .cp = 15, .opc1 = 4, .crm = 2,
3906 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
a57633c0 3907 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
541ef8c2
SS
3908 { .name = "TLBIALLNSNH",
3909 .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
3910 .type = ARM_CP_NO_RAW, .access = PL2_W,
3911 .writefn = tlbiall_nsnh_write },
3912 { .name = "TLBIALLNSNHIS",
3913 .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
3914 .type = ARM_CP_NO_RAW, .access = PL2_W,
3915 .writefn = tlbiall_nsnh_is_write },
3916 { .name = "TLBIALLH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
3917 .type = ARM_CP_NO_RAW, .access = PL2_W,
3918 .writefn = tlbiall_hyp_write },
3919 { .name = "TLBIALLHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
3920 .type = ARM_CP_NO_RAW, .access = PL2_W,
3921 .writefn = tlbiall_hyp_is_write },
3922 { .name = "TLBIMVAH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
3923 .type = ARM_CP_NO_RAW, .access = PL2_W,
3924 .writefn = tlbimva_hyp_write },
3925 { .name = "TLBIMVAHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
3926 .type = ARM_CP_NO_RAW, .access = PL2_W,
3927 .writefn = tlbimva_hyp_is_write },
51da9014
EI
3928 { .name = "TLBI_ALLE2", .state = ARM_CP_STATE_AA64,
3929 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
3930 .type = ARM_CP_NO_RAW, .access = PL2_W,
fd3ed969 3931 .writefn = tlbi_aa64_alle2_write },
8742d49d
EI
3932 { .name = "TLBI_VAE2", .state = ARM_CP_STATE_AA64,
3933 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
3934 .type = ARM_CP_NO_RAW, .access = PL2_W,
fd3ed969 3935 .writefn = tlbi_aa64_vae2_write },
2bfb9d75
PM
3936 { .name = "TLBI_VALE2", .state = ARM_CP_STATE_AA64,
3937 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
3938 .access = PL2_W, .type = ARM_CP_NO_RAW,
3939 .writefn = tlbi_aa64_vae2_write },
3940 { .name = "TLBI_ALLE2IS", .state = ARM_CP_STATE_AA64,
3941 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
3942 .access = PL2_W, .type = ARM_CP_NO_RAW,
3943 .writefn = tlbi_aa64_alle2is_write },
8742d49d
EI
3944 { .name = "TLBI_VAE2IS", .state = ARM_CP_STATE_AA64,
3945 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
3946 .type = ARM_CP_NO_RAW, .access = PL2_W,
fd3ed969 3947 .writefn = tlbi_aa64_vae2is_write },
2bfb9d75
PM
3948 { .name = "TLBI_VALE2IS", .state = ARM_CP_STATE_AA64,
3949 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
3950 .access = PL2_W, .type = ARM_CP_NO_RAW,
3951 .writefn = tlbi_aa64_vae2is_write },
edac4d8a 3952#ifndef CONFIG_USER_ONLY
2a47df95
PM
3953 /* Unlike the other EL2-related AT operations, these must
3954 * UNDEF from EL3 if EL2 is not implemented, which is why we
3955 * define them here rather than with the rest of the AT ops.
3956 */
3957 { .name = "AT_S1E2R", .state = ARM_CP_STATE_AA64,
3958 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
3959 .access = PL2_W, .accessfn = at_s1e2_access,
3960 .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3961 { .name = "AT_S1E2W", .state = ARM_CP_STATE_AA64,
3962 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
3963 .access = PL2_W, .accessfn = at_s1e2_access,
3964 .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
14db7fe0
PM
3965 /* The AArch32 ATS1H* operations are CONSTRAINED UNPREDICTABLE
3966 * if EL2 is not implemented; we choose to UNDEF. Behaviour at EL3
3967 * with SCR.NS == 0 outside Monitor mode is UNPREDICTABLE; we choose
3968 * to behave as if SCR.NS was 1.
3969 */
3970 { .name = "ATS1HR", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
3971 .access = PL2_W,
3972 .writefn = ats1h_write, .type = ARM_CP_NO_RAW },
3973 { .name = "ATS1HW", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
3974 .access = PL2_W,
3975 .writefn = ats1h_write, .type = ARM_CP_NO_RAW },
0b6440af
EI
3976 { .name = "CNTHCTL_EL2", .state = ARM_CP_STATE_BOTH,
3977 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 1, .opc2 = 0,
3978 /* ARMv7 requires bit 0 and 1 to reset to 1. ARMv8 defines the
3979 * reset values as IMPDEF. We choose to reset to 3 to comply with
3980 * both ARMv7 and ARMv8.
3981 */
3982 .access = PL2_RW, .resetvalue = 3,
3983 .fieldoffset = offsetof(CPUARMState, cp15.cnthctl_el2) },
edac4d8a
EI
3984 { .name = "CNTVOFF_EL2", .state = ARM_CP_STATE_AA64,
3985 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 3,
3986 .access = PL2_RW, .type = ARM_CP_IO, .resetvalue = 0,
3987 .writefn = gt_cntvoff_write,
3988 .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
3989 { .name = "CNTVOFF", .cp = 15, .opc1 = 4, .crm = 14,
3990 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS | ARM_CP_IO,
3991 .writefn = gt_cntvoff_write,
3992 .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
b0e66d95
EI
3993 { .name = "CNTHP_CVAL_EL2", .state = ARM_CP_STATE_AA64,
3994 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 2,
3995 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
3996 .type = ARM_CP_IO, .access = PL2_RW,
3997 .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
3998 { .name = "CNTHP_CVAL", .cp = 15, .opc1 = 6, .crm = 14,
3999 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
4000 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_IO,
4001 .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
4002 { .name = "CNTHP_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
4003 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 0,
d44ec156 4004 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
b0e66d95
EI
4005 .resetfn = gt_hyp_timer_reset,
4006 .readfn = gt_hyp_tval_read, .writefn = gt_hyp_tval_write },
4007 { .name = "CNTHP_CTL_EL2", .state = ARM_CP_STATE_BOTH,
4008 .type = ARM_CP_IO,
4009 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 1,
4010 .access = PL2_RW,
4011 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].ctl),
4012 .resetvalue = 0,
4013 .writefn = gt_hyp_ctl_write, .raw_writefn = raw_write },
edac4d8a 4014#endif
14cc7b54
SF
4015 /* The only field of MDCR_EL2 that has a defined architectural reset value
4016 * is MDCR_EL2.HPMN which should reset to the value of PMCR_EL0.N; but we
4017 * don't impelment any PMU event counters, so using zero as a reset
4018 * value for MDCR_EL2 is okay
4019 */
4020 { .name = "MDCR_EL2", .state = ARM_CP_STATE_BOTH,
4021 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 1,
4022 .access = PL2_RW, .resetvalue = 0,
4023 .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el2), },
59e05530
EI
4024 { .name = "HPFAR", .state = ARM_CP_STATE_AA32,
4025 .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
4026 .access = PL2_RW, .accessfn = access_el3_aa32ns,
4027 .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
4028 { .name = "HPFAR_EL2", .state = ARM_CP_STATE_AA64,
4029 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
4030 .access = PL2_RW,
4031 .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
2a5a9abd
AF
4032 { .name = "HSTR_EL2", .state = ARM_CP_STATE_BOTH,
4033 .cp = 15, .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 3,
4034 .access = PL2_RW,
4035 .fieldoffset = offsetof(CPUARMState, cp15.hstr_el2) },
3b685ba7
EI
4036 REGINFO_SENTINEL
4037};
4038
2f027fc5
PM
4039static CPAccessResult nsacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
4040 bool isread)
4041{
4042 /* The NSACR is RW at EL3, and RO for NS EL1 and NS EL2.
4043 * At Secure EL1 it traps to EL3.
4044 */
4045 if (arm_current_el(env) == 3) {
4046 return CP_ACCESS_OK;
4047 }
4048 if (arm_is_secure_below_el3(env)) {
4049 return CP_ACCESS_TRAP_EL3;
4050 }
4051 /* Accesses from EL1 NS and EL2 NS are UNDEF for write but allow reads. */
4052 if (isread) {
4053 return CP_ACCESS_OK;
4054 }
4055 return CP_ACCESS_TRAP_UNCATEGORIZED;
4056}
4057
60fb1a87
GB
4058static const ARMCPRegInfo el3_cp_reginfo[] = {
4059 { .name = "SCR_EL3", .state = ARM_CP_STATE_AA64,
4060 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 0,
4061 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.scr_el3),
4062 .resetvalue = 0, .writefn = scr_write },
7a0e58fa 4063 { .name = "SCR", .type = ARM_CP_ALIAS,
60fb1a87 4064 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 0,
efe4a274
PM
4065 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
4066 .fieldoffset = offsetoflow32(CPUARMState, cp15.scr_el3),
b061a82b 4067 .writefn = scr_write },
60fb1a87
GB
4068 { .name = "SDER32_EL3", .state = ARM_CP_STATE_AA64,
4069 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 1,
4070 .access = PL3_RW, .resetvalue = 0,
4071 .fieldoffset = offsetof(CPUARMState, cp15.sder) },
4072 { .name = "SDER",
4073 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 1,
4074 .access = PL3_RW, .resetvalue = 0,
4075 .fieldoffset = offsetoflow32(CPUARMState, cp15.sder) },
60fb1a87 4076 { .name = "MVBAR", .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
efe4a274
PM
4077 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
4078 .writefn = vbar_write, .resetvalue = 0,
60fb1a87 4079 .fieldoffset = offsetof(CPUARMState, cp15.mvbar) },
7dd8c9af
FA
4080 { .name = "TTBR0_EL3", .state = ARM_CP_STATE_AA64,
4081 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 0,
4082 .access = PL3_RW, .writefn = vmsa_ttbr_write, .resetvalue = 0,
4083 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[3]) },
11f136ee
FA
4084 { .name = "TCR_EL3", .state = ARM_CP_STATE_AA64,
4085 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 2,
6459b94c
PM
4086 .access = PL3_RW,
4087 /* no .writefn needed as this can't cause an ASID change;
811595a2
PM
4088 * we must provide a .raw_writefn and .resetfn because we handle
4089 * reset and migration for the AArch32 TTBCR(S), which might be
4090 * using mask and base_mask.
6459b94c 4091 */
811595a2 4092 .resetfn = vmsa_ttbcr_reset, .raw_writefn = vmsa_ttbcr_raw_write,
11f136ee 4093 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[3]) },
81547d66 4094 { .name = "ELR_EL3", .state = ARM_CP_STATE_AA64,
7a0e58fa 4095 .type = ARM_CP_ALIAS,
81547d66
EI
4096 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 1,
4097 .access = PL3_RW,
4098 .fieldoffset = offsetof(CPUARMState, elr_el[3]) },
f2c30f42 4099 { .name = "ESR_EL3", .state = ARM_CP_STATE_AA64,
f2c30f42
EI
4100 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 2, .opc2 = 0,
4101 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[3]) },
63b60551
EI
4102 { .name = "FAR_EL3", .state = ARM_CP_STATE_AA64,
4103 .opc0 = 3, .opc1 = 6, .crn = 6, .crm = 0, .opc2 = 0,
4104 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[3]) },
81547d66 4105 { .name = "SPSR_EL3", .state = ARM_CP_STATE_AA64,
7a0e58fa 4106 .type = ARM_CP_ALIAS,
81547d66 4107 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 0,
99a99c1f
SB
4108 .access = PL3_RW,
4109 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_MON]) },
a1ba125c
EI
4110 { .name = "VBAR_EL3", .state = ARM_CP_STATE_AA64,
4111 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 0,
4112 .access = PL3_RW, .writefn = vbar_write,
4113 .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[3]),
4114 .resetvalue = 0 },
c6f19164
GB
4115 { .name = "CPTR_EL3", .state = ARM_CP_STATE_AA64,
4116 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 2,
4117 .access = PL3_RW, .accessfn = cptr_access, .resetvalue = 0,
4118 .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[3]) },
4cfb8ad8
PM
4119 { .name = "TPIDR_EL3", .state = ARM_CP_STATE_AA64,
4120 .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 2,
4121 .access = PL3_RW, .resetvalue = 0,
4122 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[3]) },
2179ef95
PM
4123 { .name = "AMAIR_EL3", .state = ARM_CP_STATE_AA64,
4124 .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 3, .opc2 = 0,
4125 .access = PL3_RW, .type = ARM_CP_CONST,
4126 .resetvalue = 0 },
37cd6c24
PM
4127 { .name = "AFSR0_EL3", .state = ARM_CP_STATE_BOTH,
4128 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 0,
4129 .access = PL3_RW, .type = ARM_CP_CONST,
4130 .resetvalue = 0 },
4131 { .name = "AFSR1_EL3", .state = ARM_CP_STATE_BOTH,
4132 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 1,
4133 .access = PL3_RW, .type = ARM_CP_CONST,
4134 .resetvalue = 0 },
43efaa33
PM
4135 { .name = "TLBI_ALLE3IS", .state = ARM_CP_STATE_AA64,
4136 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 0,
4137 .access = PL3_W, .type = ARM_CP_NO_RAW,
4138 .writefn = tlbi_aa64_alle3is_write },
4139 { .name = "TLBI_VAE3IS", .state = ARM_CP_STATE_AA64,
4140 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 1,
4141 .access = PL3_W, .type = ARM_CP_NO_RAW,
4142 .writefn = tlbi_aa64_vae3is_write },
4143 { .name = "TLBI_VALE3IS", .state = ARM_CP_STATE_AA64,
4144 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 5,
4145 .access = PL3_W, .type = ARM_CP_NO_RAW,
4146 .writefn = tlbi_aa64_vae3is_write },
4147 { .name = "TLBI_ALLE3", .state = ARM_CP_STATE_AA64,
4148 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 0,
4149 .access = PL3_W, .type = ARM_CP_NO_RAW,
4150 .writefn = tlbi_aa64_alle3_write },
4151 { .name = "TLBI_VAE3", .state = ARM_CP_STATE_AA64,
4152 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 1,
4153 .access = PL3_W, .type = ARM_CP_NO_RAW,
4154 .writefn = tlbi_aa64_vae3_write },
4155 { .name = "TLBI_VALE3", .state = ARM_CP_STATE_AA64,
4156 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 5,
4157 .access = PL3_W, .type = ARM_CP_NO_RAW,
4158 .writefn = tlbi_aa64_vae3_write },
0f1a3b24
FA
4159 REGINFO_SENTINEL
4160};
4161
3f208fd7
PM
4162static CPAccessResult ctr_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
4163 bool isread)
7da845b0
PM
4164{
4165 /* Only accessible in EL0 if SCTLR.UCT is set (and only in AArch64,
4166 * but the AArch32 CTR has its own reginfo struct)
4167 */
137feaa9 4168 if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_UCT)) {
7da845b0
PM
4169 return CP_ACCESS_TRAP;
4170 }
4171 return CP_ACCESS_OK;
4172}
4173
1424ca8d
DM
4174static void oslar_write(CPUARMState *env, const ARMCPRegInfo *ri,
4175 uint64_t value)
4176{
4177 /* Writes to OSLAR_EL1 may update the OS lock status, which can be
4178 * read via a bit in OSLSR_EL1.
4179 */
4180 int oslock;
4181
4182 if (ri->state == ARM_CP_STATE_AA32) {
4183 oslock = (value == 0xC5ACCE55);
4184 } else {
4185 oslock = value & 1;
4186 }
4187
4188 env->cp15.oslsr_el1 = deposit32(env->cp15.oslsr_el1, 1, 1, oslock);
4189}
4190
50300698 4191static const ARMCPRegInfo debug_cp_reginfo[] = {
50300698 4192 /* DBGDRAR, DBGDSAR: always RAZ since we don't implement memory mapped
10aae104
PM
4193 * debug components. The AArch64 version of DBGDRAR is named MDRAR_EL1;
4194 * unlike DBGDRAR it is never accessible from EL0.
4195 * DBGDSAR is deprecated and must RAZ from v8 anyway, so it has no AArch64
4196 * accessor.
50300698
PM
4197 */
4198 { .name = "DBGDRAR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 0,
91b0a238
PM
4199 .access = PL0_R, .accessfn = access_tdra,
4200 .type = ARM_CP_CONST, .resetvalue = 0 },
10aae104
PM
4201 { .name = "MDRAR_EL1", .state = ARM_CP_STATE_AA64,
4202 .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
91b0a238
PM
4203 .access = PL1_R, .accessfn = access_tdra,
4204 .type = ARM_CP_CONST, .resetvalue = 0 },
50300698 4205 { .name = "DBGDSAR", .cp = 14, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
91b0a238
PM
4206 .access = PL0_R, .accessfn = access_tdra,
4207 .type = ARM_CP_CONST, .resetvalue = 0 },
17a9eb53 4208 /* Monitor debug system control register; the 32-bit alias is DBGDSCRext. */
10aae104
PM
4209 { .name = "MDSCR_EL1", .state = ARM_CP_STATE_BOTH,
4210 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
d6c8cf81 4211 .access = PL1_RW, .accessfn = access_tda,
0e5e8935
PM
4212 .fieldoffset = offsetof(CPUARMState, cp15.mdscr_el1),
4213 .resetvalue = 0 },
5e8b12ff
PM
4214 /* MDCCSR_EL0, aka DBGDSCRint. This is a read-only mirror of MDSCR_EL1.
4215 * We don't implement the configurable EL0 access.
4216 */
4217 { .name = "MDCCSR_EL0", .state = ARM_CP_STATE_BOTH,
4218 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
7a0e58fa 4219 .type = ARM_CP_ALIAS,
d6c8cf81 4220 .access = PL1_R, .accessfn = access_tda,
b061a82b 4221 .fieldoffset = offsetof(CPUARMState, cp15.mdscr_el1), },
10aae104
PM
4222 { .name = "OSLAR_EL1", .state = ARM_CP_STATE_BOTH,
4223 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 4,
1424ca8d 4224 .access = PL1_W, .type = ARM_CP_NO_RAW,
187f678d 4225 .accessfn = access_tdosa,
1424ca8d
DM
4226 .writefn = oslar_write },
4227 { .name = "OSLSR_EL1", .state = ARM_CP_STATE_BOTH,
4228 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 4,
4229 .access = PL1_R, .resetvalue = 10,
187f678d 4230 .accessfn = access_tdosa,
1424ca8d 4231 .fieldoffset = offsetof(CPUARMState, cp15.oslsr_el1) },
5e8b12ff
PM
4232 /* Dummy OSDLR_EL1: 32-bit Linux will read this */
4233 { .name = "OSDLR_EL1", .state = ARM_CP_STATE_BOTH,
4234 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 4,
187f678d
PM
4235 .access = PL1_RW, .accessfn = access_tdosa,
4236 .type = ARM_CP_NOP },
5e8b12ff
PM
4237 /* Dummy DBGVCR: Linux wants to clear this on startup, but we don't
4238 * implement vector catch debug events yet.
4239 */
4240 { .name = "DBGVCR",
4241 .cp = 14, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
d6c8cf81
PM
4242 .access = PL1_RW, .accessfn = access_tda,
4243 .type = ARM_CP_NOP },
4d2ec4da
PM
4244 /* Dummy DBGVCR32_EL2 (which is only for a 64-bit hypervisor
4245 * to save and restore a 32-bit guest's DBGVCR)
4246 */
4247 { .name = "DBGVCR32_EL2", .state = ARM_CP_STATE_AA64,
4248 .opc0 = 2, .opc1 = 4, .crn = 0, .crm = 7, .opc2 = 0,
4249 .access = PL2_RW, .accessfn = access_tda,
4250 .type = ARM_CP_NOP },
5dbdc434
PM
4251 /* Dummy MDCCINT_EL1, since we don't implement the Debug Communications
4252 * Channel but Linux may try to access this register. The 32-bit
4253 * alias is DBGDCCINT.
4254 */
4255 { .name = "MDCCINT_EL1", .state = ARM_CP_STATE_BOTH,
4256 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0,
4257 .access = PL1_RW, .accessfn = access_tda,
4258 .type = ARM_CP_NOP },
50300698
PM
4259 REGINFO_SENTINEL
4260};
4261
4262static const ARMCPRegInfo debug_lpae_cp_reginfo[] = {
4263 /* 64 bit access versions of the (dummy) debug registers */
4264 { .name = "DBGDRAR", .cp = 14, .crm = 1, .opc1 = 0,
4265 .access = PL0_R, .type = ARM_CP_CONST|ARM_CP_64BIT, .resetvalue = 0 },
4266 { .name = "DBGDSAR", .cp = 14, .crm = 2, .opc1 = 0,
4267 .access = PL0_R, .type = ARM_CP_CONST|ARM_CP_64BIT, .resetvalue = 0 },
4268 REGINFO_SENTINEL
4269};
4270
5be5e8ed
RH
4271/* Return the exception level to which SVE-disabled exceptions should
4272 * be taken, or 0 if SVE is enabled.
4273 */
4274static int sve_exception_el(CPUARMState *env)
4275{
4276#ifndef CONFIG_USER_ONLY
4277 unsigned current_el = arm_current_el(env);
4278
4279 /* The CPACR.ZEN controls traps to EL1:
4280 * 0, 2 : trap EL0 and EL1 accesses
4281 * 1 : trap only EL0 accesses
4282 * 3 : trap no accesses
4283 */
4284 switch (extract32(env->cp15.cpacr_el1, 16, 2)) {
4285 default:
4286 if (current_el <= 1) {
4287 /* Trap to PL1, which might be EL1 or EL3 */
4288 if (arm_is_secure(env) && !arm_el_is_aa64(env, 3)) {
4289 return 3;
4290 }
4291 return 1;
4292 }
4293 break;
4294 case 1:
4295 if (current_el == 0) {
4296 return 1;
4297 }
4298 break;
4299 case 3:
4300 break;
4301 }
4302
4303 /* Similarly for CPACR.FPEN, after having checked ZEN. */
4304 switch (extract32(env->cp15.cpacr_el1, 20, 2)) {
4305 default:
4306 if (current_el <= 1) {
4307 if (arm_is_secure(env) && !arm_el_is_aa64(env, 3)) {
4308 return 3;
4309 }
4310 return 1;
4311 }
4312 break;
4313 case 1:
4314 if (current_el == 0) {
4315 return 1;
4316 }
4317 break;
4318 case 3:
4319 break;
4320 }
4321
4322 /* CPTR_EL2. Check both TZ and TFP. */
4323 if (current_el <= 2
4324 && (env->cp15.cptr_el[2] & (CPTR_TFP | CPTR_TZ))
4325 && !arm_is_secure_below_el3(env)) {
4326 return 2;
4327 }
4328
4329 /* CPTR_EL3. Check both EZ and TFP. */
4330 if (!(env->cp15.cptr_el[3] & CPTR_EZ)
4331 || (env->cp15.cptr_el[3] & CPTR_TFP)) {
4332 return 3;
4333 }
4334#endif
4335 return 0;
4336}
4337
4338static CPAccessResult zcr_access(CPUARMState *env, const ARMCPRegInfo *ri,
4339 bool isread)
4340{
4341 switch (sve_exception_el(env)) {
4342 case 3:
4343 return CP_ACCESS_TRAP_EL3;
4344 case 2:
4345 return CP_ACCESS_TRAP_EL2;
4346 case 1:
4347 return CP_ACCESS_TRAP;
4348 }
4349 return CP_ACCESS_OK;
4350}
4351
4352static void zcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4353 uint64_t value)
4354{
4355 /* Bits other than [3:0] are RAZ/WI. */
4356 raw_write(env, ri, value & 0xf);
4357}
4358
4359static const ARMCPRegInfo zcr_el1_reginfo = {
4360 .name = "ZCR_EL1", .state = ARM_CP_STATE_AA64,
4361 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 0,
5d1e6999 4362 .access = PL1_RW, .accessfn = zcr_access,
5be5e8ed
RH
4363 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[1]),
4364 .writefn = zcr_write, .raw_writefn = raw_write
4365};
4366
4367static const ARMCPRegInfo zcr_el2_reginfo = {
4368 .name = "ZCR_EL2", .state = ARM_CP_STATE_AA64,
4369 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 0,
5d1e6999 4370 .access = PL2_RW, .accessfn = zcr_access,
5be5e8ed
RH
4371 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[2]),
4372 .writefn = zcr_write, .raw_writefn = raw_write
4373};
4374
4375static const ARMCPRegInfo zcr_no_el2_reginfo = {
4376 .name = "ZCR_EL2", .state = ARM_CP_STATE_AA64,
4377 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 0,
5d1e6999 4378 .access = PL2_RW,
5be5e8ed
RH
4379 .readfn = arm_cp_read_zero, .writefn = arm_cp_write_ignore
4380};
4381
4382static const ARMCPRegInfo zcr_el3_reginfo = {
4383 .name = "ZCR_EL3", .state = ARM_CP_STATE_AA64,
4384 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 2, .opc2 = 0,
5d1e6999 4385 .access = PL3_RW, .accessfn = zcr_access,
5be5e8ed
RH
4386 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[3]),
4387 .writefn = zcr_write, .raw_writefn = raw_write
4388};
4389
9ee98ce8
PM
4390void hw_watchpoint_update(ARMCPU *cpu, int n)
4391{
4392 CPUARMState *env = &cpu->env;
4393 vaddr len = 0;
4394 vaddr wvr = env->cp15.dbgwvr[n];
4395 uint64_t wcr = env->cp15.dbgwcr[n];
4396 int mask;
4397 int flags = BP_CPU | BP_STOP_BEFORE_ACCESS;
4398
4399 if (env->cpu_watchpoint[n]) {
4400 cpu_watchpoint_remove_by_ref(CPU(cpu), env->cpu_watchpoint[n]);
4401 env->cpu_watchpoint[n] = NULL;
4402 }
4403
4404 if (!extract64(wcr, 0, 1)) {
4405 /* E bit clear : watchpoint disabled */
4406 return;
4407 }
4408
4409 switch (extract64(wcr, 3, 2)) {
4410 case 0:
4411 /* LSC 00 is reserved and must behave as if the wp is disabled */
4412 return;
4413 case 1:
4414 flags |= BP_MEM_READ;
4415 break;
4416 case 2:
4417 flags |= BP_MEM_WRITE;
4418 break;
4419 case 3:
4420 flags |= BP_MEM_ACCESS;
4421 break;
4422 }
4423
4424 /* Attempts to use both MASK and BAS fields simultaneously are
4425 * CONSTRAINED UNPREDICTABLE; we opt to ignore BAS in this case,
4426 * thus generating a watchpoint for every byte in the masked region.
4427 */
4428 mask = extract64(wcr, 24, 4);
4429 if (mask == 1 || mask == 2) {
4430 /* Reserved values of MASK; we must act as if the mask value was
4431 * some non-reserved value, or as if the watchpoint were disabled.
4432 * We choose the latter.
4433 */
4434 return;
4435 } else if (mask) {
4436 /* Watchpoint covers an aligned area up to 2GB in size */
4437 len = 1ULL << mask;
4438 /* If masked bits in WVR are not zero it's CONSTRAINED UNPREDICTABLE
4439 * whether the watchpoint fires when the unmasked bits match; we opt
4440 * to generate the exceptions.
4441 */
4442 wvr &= ~(len - 1);
4443 } else {
4444 /* Watchpoint covers bytes defined by the byte address select bits */
4445 int bas = extract64(wcr, 5, 8);
4446 int basstart;
4447
4448 if (bas == 0) {
4449 /* This must act as if the watchpoint is disabled */
4450 return;
4451 }
4452
4453 if (extract64(wvr, 2, 1)) {
4454 /* Deprecated case of an only 4-aligned address. BAS[7:4] are
4455 * ignored, and BAS[3:0] define which bytes to watch.
4456 */
4457 bas &= 0xf;
4458 }
4459 /* The BAS bits are supposed to be programmed to indicate a contiguous
4460 * range of bytes. Otherwise it is CONSTRAINED UNPREDICTABLE whether
4461 * we fire for each byte in the word/doubleword addressed by the WVR.
4462 * We choose to ignore any non-zero bits after the first range of 1s.
4463 */
4464 basstart = ctz32(bas);
4465 len = cto32(bas >> basstart);
4466 wvr += basstart;
4467 }
4468
4469 cpu_watchpoint_insert(CPU(cpu), wvr, len, flags,
4470 &env->cpu_watchpoint[n]);
4471}
4472
4473void hw_watchpoint_update_all(ARMCPU *cpu)
4474{
4475 int i;
4476 CPUARMState *env = &cpu->env;
4477
4478 /* Completely clear out existing QEMU watchpoints and our array, to
4479 * avoid possible stale entries following migration load.
4480 */
4481 cpu_watchpoint_remove_all(CPU(cpu), BP_CPU);
4482 memset(env->cpu_watchpoint, 0, sizeof(env->cpu_watchpoint));
4483
4484 for (i = 0; i < ARRAY_SIZE(cpu->env.cpu_watchpoint); i++) {
4485 hw_watchpoint_update(cpu, i);
4486 }
4487}
4488
4489static void dbgwvr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4490 uint64_t value)
4491{
4492 ARMCPU *cpu = arm_env_get_cpu(env);
4493 int i = ri->crm;
4494
4495 /* Bits [63:49] are hardwired to the value of bit [48]; that is, the
4496 * register reads and behaves as if values written are sign extended.
4497 * Bits [1:0] are RES0.
4498 */
4499 value = sextract64(value, 0, 49) & ~3ULL;
4500
4501 raw_write(env, ri, value);
4502 hw_watchpoint_update(cpu, i);
4503}
4504
4505static void dbgwcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4506 uint64_t value)
4507{
4508 ARMCPU *cpu = arm_env_get_cpu(env);
4509 int i = ri->crm;
4510
4511 raw_write(env, ri, value);
4512 hw_watchpoint_update(cpu, i);
4513}
4514
46747d15
PM
4515void hw_breakpoint_update(ARMCPU *cpu, int n)
4516{
4517 CPUARMState *env = &cpu->env;
4518 uint64_t bvr = env->cp15.dbgbvr[n];
4519 uint64_t bcr = env->cp15.dbgbcr[n];
4520 vaddr addr;
4521 int bt;
4522 int flags = BP_CPU;
4523
4524 if (env->cpu_breakpoint[n]) {
4525 cpu_breakpoint_remove_by_ref(CPU(cpu), env->cpu_breakpoint[n]);
4526 env->cpu_breakpoint[n] = NULL;
4527 }
4528
4529 if (!extract64(bcr, 0, 1)) {
4530 /* E bit clear : watchpoint disabled */
4531 return;
4532 }
4533
4534 bt = extract64(bcr, 20, 4);
4535
4536 switch (bt) {
4537 case 4: /* unlinked address mismatch (reserved if AArch64) */
4538 case 5: /* linked address mismatch (reserved if AArch64) */
4539 qemu_log_mask(LOG_UNIMP,
4540 "arm: address mismatch breakpoint types not implemented");
4541 return;
4542 case 0: /* unlinked address match */
4543 case 1: /* linked address match */
4544 {
4545 /* Bits [63:49] are hardwired to the value of bit [48]; that is,
4546 * we behave as if the register was sign extended. Bits [1:0] are
4547 * RES0. The BAS field is used to allow setting breakpoints on 16
4548 * bit wide instructions; it is CONSTRAINED UNPREDICTABLE whether
4549 * a bp will fire if the addresses covered by the bp and the addresses
4550 * covered by the insn overlap but the insn doesn't start at the
4551 * start of the bp address range. We choose to require the insn and
4552 * the bp to have the same address. The constraints on writing to
4553 * BAS enforced in dbgbcr_write mean we have only four cases:
4554 * 0b0000 => no breakpoint
4555 * 0b0011 => breakpoint on addr
4556 * 0b1100 => breakpoint on addr + 2
4557 * 0b1111 => breakpoint on addr
4558 * See also figure D2-3 in the v8 ARM ARM (DDI0487A.c).
4559 */
4560 int bas = extract64(bcr, 5, 4);
4561 addr = sextract64(bvr, 0, 49) & ~3ULL;
4562 if (bas == 0) {
4563 return;
4564 }
4565 if (bas == 0xc) {
4566 addr += 2;
4567 }
4568 break;
4569 }
4570 case 2: /* unlinked context ID match */
4571 case 8: /* unlinked VMID match (reserved if no EL2) */
4572 case 10: /* unlinked context ID and VMID match (reserved if no EL2) */
4573 qemu_log_mask(LOG_UNIMP,
4574 "arm: unlinked context breakpoint types not implemented");
4575 return;
4576 case 9: /* linked VMID match (reserved if no EL2) */
4577 case 11: /* linked context ID and VMID match (reserved if no EL2) */
4578 case 3: /* linked context ID match */
4579 default:
4580 /* We must generate no events for Linked context matches (unless
4581 * they are linked to by some other bp/wp, which is handled in
4582 * updates for the linking bp/wp). We choose to also generate no events
4583 * for reserved values.
4584 */
4585 return;
4586 }
4587
4588 cpu_breakpoint_insert(CPU(cpu), addr, flags, &env->cpu_breakpoint[n]);
4589}
4590
4591void hw_breakpoint_update_all(ARMCPU *cpu)
4592{
4593 int i;
4594 CPUARMState *env = &cpu->env;
4595
4596 /* Completely clear out existing QEMU breakpoints and our array, to
4597 * avoid possible stale entries following migration load.
4598 */
4599 cpu_breakpoint_remove_all(CPU(cpu), BP_CPU);
4600 memset(env->cpu_breakpoint, 0, sizeof(env->cpu_breakpoint));
4601
4602 for (i = 0; i < ARRAY_SIZE(cpu->env.cpu_breakpoint); i++) {
4603 hw_breakpoint_update(cpu, i);
4604 }
4605}
4606
4607static void dbgbvr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4608 uint64_t value)
4609{
4610 ARMCPU *cpu = arm_env_get_cpu(env);
4611 int i = ri->crm;
4612
4613 raw_write(env, ri, value);
4614 hw_breakpoint_update(cpu, i);
4615}
4616
4617static void dbgbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4618 uint64_t value)
4619{
4620 ARMCPU *cpu = arm_env_get_cpu(env);
4621 int i = ri->crm;
4622
4623 /* BAS[3] is a read-only copy of BAS[2], and BAS[1] a read-only
4624 * copy of BAS[0].
4625 */
4626 value = deposit64(value, 6, 1, extract64(value, 5, 1));
4627 value = deposit64(value, 8, 1, extract64(value, 7, 1));
4628
4629 raw_write(env, ri, value);
4630 hw_breakpoint_update(cpu, i);
4631}
4632
50300698 4633static void define_debug_regs(ARMCPU *cpu)
0b45451e 4634{
50300698
PM
4635 /* Define v7 and v8 architectural debug registers.
4636 * These are just dummy implementations for now.
0b45451e
PM
4637 */
4638 int i;
3ff6fc91 4639 int wrps, brps, ctx_cmps;
48eb3ae6
PM
4640 ARMCPRegInfo dbgdidr = {
4641 .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0,
d6c8cf81
PM
4642 .access = PL0_R, .accessfn = access_tda,
4643 .type = ARM_CP_CONST, .resetvalue = cpu->dbgdidr,
48eb3ae6
PM
4644 };
4645
3ff6fc91 4646 /* Note that all these register fields hold "number of Xs minus 1". */
48eb3ae6
PM
4647 brps = extract32(cpu->dbgdidr, 24, 4);
4648 wrps = extract32(cpu->dbgdidr, 28, 4);
3ff6fc91
PM
4649 ctx_cmps = extract32(cpu->dbgdidr, 20, 4);
4650
4651 assert(ctx_cmps <= brps);
48eb3ae6
PM
4652
4653 /* The DBGDIDR and ID_AA64DFR0_EL1 define various properties
4654 * of the debug registers such as number of breakpoints;
4655 * check that if they both exist then they agree.
4656 */
4657 if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64)) {
4658 assert(extract32(cpu->id_aa64dfr0, 12, 4) == brps);
4659 assert(extract32(cpu->id_aa64dfr0, 20, 4) == wrps);
3ff6fc91 4660 assert(extract32(cpu->id_aa64dfr0, 28, 4) == ctx_cmps);
48eb3ae6 4661 }
0b45451e 4662
48eb3ae6 4663 define_one_arm_cp_reg(cpu, &dbgdidr);
50300698
PM
4664 define_arm_cp_regs(cpu, debug_cp_reginfo);
4665
4666 if (arm_feature(&cpu->env, ARM_FEATURE_LPAE)) {
4667 define_arm_cp_regs(cpu, debug_lpae_cp_reginfo);
4668 }
4669
48eb3ae6 4670 for (i = 0; i < brps + 1; i++) {
0b45451e 4671 ARMCPRegInfo dbgregs[] = {
10aae104
PM
4672 { .name = "DBGBVR", .state = ARM_CP_STATE_BOTH,
4673 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 4,
d6c8cf81 4674 .access = PL1_RW, .accessfn = access_tda,
46747d15
PM
4675 .fieldoffset = offsetof(CPUARMState, cp15.dbgbvr[i]),
4676 .writefn = dbgbvr_write, .raw_writefn = raw_write
4677 },
10aae104
PM
4678 { .name = "DBGBCR", .state = ARM_CP_STATE_BOTH,
4679 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 5,
d6c8cf81 4680 .access = PL1_RW, .accessfn = access_tda,
46747d15
PM
4681 .fieldoffset = offsetof(CPUARMState, cp15.dbgbcr[i]),
4682 .writefn = dbgbcr_write, .raw_writefn = raw_write
4683 },
48eb3ae6
PM
4684 REGINFO_SENTINEL
4685 };
4686 define_arm_cp_regs(cpu, dbgregs);
4687 }
4688
4689 for (i = 0; i < wrps + 1; i++) {
4690 ARMCPRegInfo dbgregs[] = {
10aae104
PM
4691 { .name = "DBGWVR", .state = ARM_CP_STATE_BOTH,
4692 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 6,
d6c8cf81 4693 .access = PL1_RW, .accessfn = access_tda,
9ee98ce8
PM
4694 .fieldoffset = offsetof(CPUARMState, cp15.dbgwvr[i]),
4695 .writefn = dbgwvr_write, .raw_writefn = raw_write
4696 },
10aae104
PM
4697 { .name = "DBGWCR", .state = ARM_CP_STATE_BOTH,
4698 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 7,
d6c8cf81 4699 .access = PL1_RW, .accessfn = access_tda,
9ee98ce8
PM
4700 .fieldoffset = offsetof(CPUARMState, cp15.dbgwcr[i]),
4701 .writefn = dbgwcr_write, .raw_writefn = raw_write
4702 },
4703 REGINFO_SENTINEL
0b45451e
PM
4704 };
4705 define_arm_cp_regs(cpu, dbgregs);
4706 }
4707}
4708
96a8b92e
PM
4709/* We don't know until after realize whether there's a GICv3
4710 * attached, and that is what registers the gicv3 sysregs.
4711 * So we have to fill in the GIC fields in ID_PFR/ID_PFR1_EL1/ID_AA64PFR0_EL1
4712 * at runtime.
4713 */
4714static uint64_t id_pfr1_read(CPUARMState *env, const ARMCPRegInfo *ri)
4715{
4716 ARMCPU *cpu = arm_env_get_cpu(env);
4717 uint64_t pfr1 = cpu->id_pfr1;
4718
4719 if (env->gicv3state) {
4720 pfr1 |= 1 << 28;
4721 }
4722 return pfr1;
4723}
4724
4725static uint64_t id_aa64pfr0_read(CPUARMState *env, const ARMCPRegInfo *ri)
4726{
4727 ARMCPU *cpu = arm_env_get_cpu(env);
4728 uint64_t pfr0 = cpu->id_aa64pfr0;
4729
4730 if (env->gicv3state) {
4731 pfr0 |= 1 << 24;
4732 }
4733 return pfr0;
4734}
4735
2ceb98c0
PM
4736void register_cp_regs_for_features(ARMCPU *cpu)
4737{
4738 /* Register all the coprocessor registers based on feature bits */
4739 CPUARMState *env = &cpu->env;
4740 if (arm_feature(env, ARM_FEATURE_M)) {
4741 /* M profile has no coprocessor registers */
4742 return;
4743 }
4744
e9aa6c21 4745 define_arm_cp_regs(cpu, cp_reginfo);
9449fdf6
PM
4746 if (!arm_feature(env, ARM_FEATURE_V8)) {
4747 /* Must go early as it is full of wildcards that may be
4748 * overridden by later definitions.
4749 */
4750 define_arm_cp_regs(cpu, not_v8_cp_reginfo);
4751 }
4752
7d57f408 4753 if (arm_feature(env, ARM_FEATURE_V6)) {
8515a092
PM
4754 /* The ID registers all have impdef reset values */
4755 ARMCPRegInfo v6_idregs[] = {
0ff644a7
PM
4756 { .name = "ID_PFR0", .state = ARM_CP_STATE_BOTH,
4757 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
4758 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 4759 .resetvalue = cpu->id_pfr0 },
96a8b92e
PM
4760 /* ID_PFR1 is not a plain ARM_CP_CONST because we don't know
4761 * the value of the GIC field until after we define these regs.
4762 */
0ff644a7
PM
4763 { .name = "ID_PFR1", .state = ARM_CP_STATE_BOTH,
4764 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 1,
96a8b92e
PM
4765 .access = PL1_R, .type = ARM_CP_NO_RAW,
4766 .readfn = id_pfr1_read,
4767 .writefn = arm_cp_write_ignore },
0ff644a7
PM
4768 { .name = "ID_DFR0", .state = ARM_CP_STATE_BOTH,
4769 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 2,
4770 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 4771 .resetvalue = cpu->id_dfr0 },
0ff644a7
PM
4772 { .name = "ID_AFR0", .state = ARM_CP_STATE_BOTH,
4773 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 3,
4774 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 4775 .resetvalue = cpu->id_afr0 },
0ff644a7
PM
4776 { .name = "ID_MMFR0", .state = ARM_CP_STATE_BOTH,
4777 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 4,
4778 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 4779 .resetvalue = cpu->id_mmfr0 },
0ff644a7
PM
4780 { .name = "ID_MMFR1", .state = ARM_CP_STATE_BOTH,
4781 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 5,
4782 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 4783 .resetvalue = cpu->id_mmfr1 },
0ff644a7
PM
4784 { .name = "ID_MMFR2", .state = ARM_CP_STATE_BOTH,
4785 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 6,
4786 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 4787 .resetvalue = cpu->id_mmfr2 },
0ff644a7
PM
4788 { .name = "ID_MMFR3", .state = ARM_CP_STATE_BOTH,
4789 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 7,
4790 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 4791 .resetvalue = cpu->id_mmfr3 },
0ff644a7
PM
4792 { .name = "ID_ISAR0", .state = ARM_CP_STATE_BOTH,
4793 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0,
4794 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 4795 .resetvalue = cpu->id_isar0 },
0ff644a7
PM
4796 { .name = "ID_ISAR1", .state = ARM_CP_STATE_BOTH,
4797 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 1,
4798 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 4799 .resetvalue = cpu->id_isar1 },
0ff644a7
PM
4800 { .name = "ID_ISAR2", .state = ARM_CP_STATE_BOTH,
4801 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
4802 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 4803 .resetvalue = cpu->id_isar2 },
0ff644a7
PM
4804 { .name = "ID_ISAR3", .state = ARM_CP_STATE_BOTH,
4805 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 3,
4806 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 4807 .resetvalue = cpu->id_isar3 },
0ff644a7
PM
4808 { .name = "ID_ISAR4", .state = ARM_CP_STATE_BOTH,
4809 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 4,
4810 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 4811 .resetvalue = cpu->id_isar4 },
0ff644a7
PM
4812 { .name = "ID_ISAR5", .state = ARM_CP_STATE_BOTH,
4813 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 5,
4814 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 4815 .resetvalue = cpu->id_isar5 },
e20d84c1
PM
4816 { .name = "ID_MMFR4", .state = ARM_CP_STATE_BOTH,
4817 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 6,
4818 .access = PL1_R, .type = ARM_CP_CONST,
4819 .resetvalue = cpu->id_mmfr4 },
4820 /* 7 is as yet unallocated and must RAZ */
4821 { .name = "ID_ISAR7_RESERVED", .state = ARM_CP_STATE_BOTH,
4822 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 7,
4823 .access = PL1_R, .type = ARM_CP_CONST,
8515a092
PM
4824 .resetvalue = 0 },
4825 REGINFO_SENTINEL
4826 };
4827 define_arm_cp_regs(cpu, v6_idregs);
7d57f408
PM
4828 define_arm_cp_regs(cpu, v6_cp_reginfo);
4829 } else {
4830 define_arm_cp_regs(cpu, not_v6_cp_reginfo);
4831 }
4d31c596
PM
4832 if (arm_feature(env, ARM_FEATURE_V6K)) {
4833 define_arm_cp_regs(cpu, v6k_cp_reginfo);
4834 }
5e5cf9e3 4835 if (arm_feature(env, ARM_FEATURE_V7MP) &&
452a0955 4836 !arm_feature(env, ARM_FEATURE_PMSA)) {
995939a6
PM
4837 define_arm_cp_regs(cpu, v7mp_cp_reginfo);
4838 }
e9aa6c21 4839 if (arm_feature(env, ARM_FEATURE_V7)) {
200ac0ef 4840 /* v7 performance monitor control register: same implementor
7c2cb42b
AF
4841 * field as main ID register, and we implement only the cycle
4842 * count register.
200ac0ef 4843 */
7c2cb42b 4844#ifndef CONFIG_USER_ONLY
200ac0ef
PM
4845 ARMCPRegInfo pmcr = {
4846 .name = "PMCR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 0,
8521466b 4847 .access = PL0_RW,
7a0e58fa 4848 .type = ARM_CP_IO | ARM_CP_ALIAS,
8521466b 4849 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcr),
fcd25206
PM
4850 .accessfn = pmreg_access, .writefn = pmcr_write,
4851 .raw_writefn = raw_write,
200ac0ef 4852 };
8521466b
AF
4853 ARMCPRegInfo pmcr64 = {
4854 .name = "PMCR_EL0", .state = ARM_CP_STATE_AA64,
4855 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 0,
4856 .access = PL0_RW, .accessfn = pmreg_access,
4857 .type = ARM_CP_IO,
4858 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcr),
4859 .resetvalue = cpu->midr & 0xff000000,
4860 .writefn = pmcr_write, .raw_writefn = raw_write,
4861 };
7c2cb42b 4862 define_one_arm_cp_reg(cpu, &pmcr);
8521466b 4863 define_one_arm_cp_reg(cpu, &pmcr64);
7c2cb42b 4864#endif
776d4e5c 4865 ARMCPRegInfo clidr = {
7da845b0
PM
4866 .name = "CLIDR", .state = ARM_CP_STATE_BOTH,
4867 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 1,
776d4e5c
PM
4868 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->clidr
4869 };
776d4e5c 4870 define_one_arm_cp_reg(cpu, &clidr);
e9aa6c21 4871 define_arm_cp_regs(cpu, v7_cp_reginfo);
50300698 4872 define_debug_regs(cpu);
7d57f408
PM
4873 } else {
4874 define_arm_cp_regs(cpu, not_v7_cp_reginfo);
e9aa6c21 4875 }
b0d2b7d0 4876 if (arm_feature(env, ARM_FEATURE_V8)) {
e20d84c1
PM
4877 /* AArch64 ID registers, which all have impdef reset values.
4878 * Note that within the ID register ranges the unused slots
4879 * must all RAZ, not UNDEF; future architecture versions may
4880 * define new registers here.
4881 */
e60cef86 4882 ARMCPRegInfo v8_idregs[] = {
96a8b92e
PM
4883 /* ID_AA64PFR0_EL1 is not a plain ARM_CP_CONST because we don't
4884 * know the right value for the GIC field until after we
4885 * define these regs.
4886 */
e60cef86
PM
4887 { .name = "ID_AA64PFR0_EL1", .state = ARM_CP_STATE_AA64,
4888 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 0,
96a8b92e
PM
4889 .access = PL1_R, .type = ARM_CP_NO_RAW,
4890 .readfn = id_aa64pfr0_read,
4891 .writefn = arm_cp_write_ignore },
e60cef86
PM
4892 { .name = "ID_AA64PFR1_EL1", .state = ARM_CP_STATE_AA64,
4893 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 1,
4894 .access = PL1_R, .type = ARM_CP_CONST,
4895 .resetvalue = cpu->id_aa64pfr1},
e20d84c1
PM
4896 { .name = "ID_AA64PFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4897 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 2,
4898 .access = PL1_R, .type = ARM_CP_CONST,
4899 .resetvalue = 0 },
4900 { .name = "ID_AA64PFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4901 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 3,
4902 .access = PL1_R, .type = ARM_CP_CONST,
4903 .resetvalue = 0 },
4904 { .name = "ID_AA64PFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4905 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 4,
4906 .access = PL1_R, .type = ARM_CP_CONST,
4907 .resetvalue = 0 },
4908 { .name = "ID_AA64PFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4909 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 5,
4910 .access = PL1_R, .type = ARM_CP_CONST,
4911 .resetvalue = 0 },
4912 { .name = "ID_AA64PFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4913 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 6,
4914 .access = PL1_R, .type = ARM_CP_CONST,
4915 .resetvalue = 0 },
4916 { .name = "ID_AA64PFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4917 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 7,
4918 .access = PL1_R, .type = ARM_CP_CONST,
4919 .resetvalue = 0 },
e60cef86
PM
4920 { .name = "ID_AA64DFR0_EL1", .state = ARM_CP_STATE_AA64,
4921 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 0,
4922 .access = PL1_R, .type = ARM_CP_CONST,
d6f02ce3 4923 .resetvalue = cpu->id_aa64dfr0 },
e60cef86
PM
4924 { .name = "ID_AA64DFR1_EL1", .state = ARM_CP_STATE_AA64,
4925 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 1,
4926 .access = PL1_R, .type = ARM_CP_CONST,
4927 .resetvalue = cpu->id_aa64dfr1 },
e20d84c1
PM
4928 { .name = "ID_AA64DFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4929 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 2,
4930 .access = PL1_R, .type = ARM_CP_CONST,
4931 .resetvalue = 0 },
4932 { .name = "ID_AA64DFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4933 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 3,
4934 .access = PL1_R, .type = ARM_CP_CONST,
4935 .resetvalue = 0 },
e60cef86
PM
4936 { .name = "ID_AA64AFR0_EL1", .state = ARM_CP_STATE_AA64,
4937 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 4,
4938 .access = PL1_R, .type = ARM_CP_CONST,
4939 .resetvalue = cpu->id_aa64afr0 },
4940 { .name = "ID_AA64AFR1_EL1", .state = ARM_CP_STATE_AA64,
4941 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 5,
4942 .access = PL1_R, .type = ARM_CP_CONST,
4943 .resetvalue = cpu->id_aa64afr1 },
e20d84c1
PM
4944 { .name = "ID_AA64AFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4945 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 6,
4946 .access = PL1_R, .type = ARM_CP_CONST,
4947 .resetvalue = 0 },
4948 { .name = "ID_AA64AFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4949 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 7,
4950 .access = PL1_R, .type = ARM_CP_CONST,
4951 .resetvalue = 0 },
e60cef86
PM
4952 { .name = "ID_AA64ISAR0_EL1", .state = ARM_CP_STATE_AA64,
4953 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 0,
4954 .access = PL1_R, .type = ARM_CP_CONST,
4955 .resetvalue = cpu->id_aa64isar0 },
4956 { .name = "ID_AA64ISAR1_EL1", .state = ARM_CP_STATE_AA64,
4957 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 1,
4958 .access = PL1_R, .type = ARM_CP_CONST,
4959 .resetvalue = cpu->id_aa64isar1 },
e20d84c1
PM
4960 { .name = "ID_AA64ISAR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4961 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 2,
4962 .access = PL1_R, .type = ARM_CP_CONST,
4963 .resetvalue = 0 },
4964 { .name = "ID_AA64ISAR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4965 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 3,
4966 .access = PL1_R, .type = ARM_CP_CONST,
4967 .resetvalue = 0 },
4968 { .name = "ID_AA64ISAR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4969 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 4,
4970 .access = PL1_R, .type = ARM_CP_CONST,
4971 .resetvalue = 0 },
4972 { .name = "ID_AA64ISAR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4973 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 5,
4974 .access = PL1_R, .type = ARM_CP_CONST,
4975 .resetvalue = 0 },
4976 { .name = "ID_AA64ISAR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4977 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 6,
4978 .access = PL1_R, .type = ARM_CP_CONST,
4979 .resetvalue = 0 },
4980 { .name = "ID_AA64ISAR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4981 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 7,
4982 .access = PL1_R, .type = ARM_CP_CONST,
4983 .resetvalue = 0 },
e60cef86
PM
4984 { .name = "ID_AA64MMFR0_EL1", .state = ARM_CP_STATE_AA64,
4985 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
4986 .access = PL1_R, .type = ARM_CP_CONST,
4987 .resetvalue = cpu->id_aa64mmfr0 },
4988 { .name = "ID_AA64MMFR1_EL1", .state = ARM_CP_STATE_AA64,
4989 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 1,
4990 .access = PL1_R, .type = ARM_CP_CONST,
4991 .resetvalue = cpu->id_aa64mmfr1 },
e20d84c1
PM
4992 { .name = "ID_AA64MMFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4993 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 2,
4994 .access = PL1_R, .type = ARM_CP_CONST,
4995 .resetvalue = 0 },
4996 { .name = "ID_AA64MMFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4997 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 3,
4998 .access = PL1_R, .type = ARM_CP_CONST,
4999 .resetvalue = 0 },
5000 { .name = "ID_AA64MMFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
5001 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 4,
5002 .access = PL1_R, .type = ARM_CP_CONST,
5003 .resetvalue = 0 },
5004 { .name = "ID_AA64MMFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
5005 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 5,
5006 .access = PL1_R, .type = ARM_CP_CONST,
5007 .resetvalue = 0 },
5008 { .name = "ID_AA64MMFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
5009 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 6,
5010 .access = PL1_R, .type = ARM_CP_CONST,
5011 .resetvalue = 0 },
5012 { .name = "ID_AA64MMFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
5013 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 7,
5014 .access = PL1_R, .type = ARM_CP_CONST,
5015 .resetvalue = 0 },
a50c0f51
PM
5016 { .name = "MVFR0_EL1", .state = ARM_CP_STATE_AA64,
5017 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0,
5018 .access = PL1_R, .type = ARM_CP_CONST,
5019 .resetvalue = cpu->mvfr0 },
5020 { .name = "MVFR1_EL1", .state = ARM_CP_STATE_AA64,
5021 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1,
5022 .access = PL1_R, .type = ARM_CP_CONST,
5023 .resetvalue = cpu->mvfr1 },
5024 { .name = "MVFR2_EL1", .state = ARM_CP_STATE_AA64,
5025 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2,
5026 .access = PL1_R, .type = ARM_CP_CONST,
5027 .resetvalue = cpu->mvfr2 },
e20d84c1
PM
5028 { .name = "MVFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
5029 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 3,
5030 .access = PL1_R, .type = ARM_CP_CONST,
5031 .resetvalue = 0 },
5032 { .name = "MVFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
5033 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 4,
5034 .access = PL1_R, .type = ARM_CP_CONST,
5035 .resetvalue = 0 },
5036 { .name = "MVFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
5037 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 5,
5038 .access = PL1_R, .type = ARM_CP_CONST,
5039 .resetvalue = 0 },
5040 { .name = "MVFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
5041 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 6,
5042 .access = PL1_R, .type = ARM_CP_CONST,
5043 .resetvalue = 0 },
5044 { .name = "MVFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
5045 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 7,
5046 .access = PL1_R, .type = ARM_CP_CONST,
5047 .resetvalue = 0 },
4054bfa9
AF
5048 { .name = "PMCEID0", .state = ARM_CP_STATE_AA32,
5049 .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 6,
5050 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
5051 .resetvalue = cpu->pmceid0 },
5052 { .name = "PMCEID0_EL0", .state = ARM_CP_STATE_AA64,
5053 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 6,
5054 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
5055 .resetvalue = cpu->pmceid0 },
5056 { .name = "PMCEID1", .state = ARM_CP_STATE_AA32,
5057 .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 7,
5058 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
5059 .resetvalue = cpu->pmceid1 },
5060 { .name = "PMCEID1_EL0", .state = ARM_CP_STATE_AA64,
5061 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 7,
5062 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
5063 .resetvalue = cpu->pmceid1 },
e60cef86
PM
5064 REGINFO_SENTINEL
5065 };
be8e8128
GB
5066 /* RVBAR_EL1 is only implemented if EL1 is the highest EL */
5067 if (!arm_feature(env, ARM_FEATURE_EL3) &&
5068 !arm_feature(env, ARM_FEATURE_EL2)) {
5069 ARMCPRegInfo rvbar = {
5070 .name = "RVBAR_EL1", .state = ARM_CP_STATE_AA64,
5071 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
5072 .type = ARM_CP_CONST, .access = PL1_R, .resetvalue = cpu->rvbar
5073 };
5074 define_one_arm_cp_reg(cpu, &rvbar);
5075 }
e60cef86 5076 define_arm_cp_regs(cpu, v8_idregs);
b0d2b7d0
PM
5077 define_arm_cp_regs(cpu, v8_cp_reginfo);
5078 }
3b685ba7 5079 if (arm_feature(env, ARM_FEATURE_EL2)) {
f0d574d6 5080 uint64_t vmpidr_def = mpidr_read_val(env);
731de9e6
EI
5081 ARMCPRegInfo vpidr_regs[] = {
5082 { .name = "VPIDR", .state = ARM_CP_STATE_AA32,
5083 .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
5084 .access = PL2_RW, .accessfn = access_el3_aa32ns,
5085 .resetvalue = cpu->midr,
5086 .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) },
5087 { .name = "VPIDR_EL2", .state = ARM_CP_STATE_AA64,
5088 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
5089 .access = PL2_RW, .resetvalue = cpu->midr,
5090 .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) },
f0d574d6
EI
5091 { .name = "VMPIDR", .state = ARM_CP_STATE_AA32,
5092 .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
5093 .access = PL2_RW, .accessfn = access_el3_aa32ns,
5094 .resetvalue = vmpidr_def,
5095 .fieldoffset = offsetof(CPUARMState, cp15.vmpidr_el2) },
5096 { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_AA64,
5097 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
5098 .access = PL2_RW,
5099 .resetvalue = vmpidr_def,
5100 .fieldoffset = offsetof(CPUARMState, cp15.vmpidr_el2) },
731de9e6
EI
5101 REGINFO_SENTINEL
5102 };
5103 define_arm_cp_regs(cpu, vpidr_regs);
4771cd01 5104 define_arm_cp_regs(cpu, el2_cp_reginfo);
be8e8128
GB
5105 /* RVBAR_EL2 is only implemented if EL2 is the highest EL */
5106 if (!arm_feature(env, ARM_FEATURE_EL3)) {
5107 ARMCPRegInfo rvbar = {
5108 .name = "RVBAR_EL2", .state = ARM_CP_STATE_AA64,
5109 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 1,
5110 .type = ARM_CP_CONST, .access = PL2_R, .resetvalue = cpu->rvbar
5111 };
5112 define_one_arm_cp_reg(cpu, &rvbar);
5113 }
d42e3c26
EI
5114 } else {
5115 /* If EL2 is missing but higher ELs are enabled, we need to
5116 * register the no_el2 reginfos.
5117 */
5118 if (arm_feature(env, ARM_FEATURE_EL3)) {
f0d574d6
EI
5119 /* When EL3 exists but not EL2, VPIDR and VMPIDR take the value
5120 * of MIDR_EL1 and MPIDR_EL1.
731de9e6
EI
5121 */
5122 ARMCPRegInfo vpidr_regs[] = {
5123 { .name = "VPIDR_EL2", .state = ARM_CP_STATE_BOTH,
5124 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
5125 .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any,
5126 .type = ARM_CP_CONST, .resetvalue = cpu->midr,
5127 .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) },
f0d574d6
EI
5128 { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_BOTH,
5129 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
5130 .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any,
5131 .type = ARM_CP_NO_RAW,
5132 .writefn = arm_cp_write_ignore, .readfn = mpidr_read },
731de9e6
EI
5133 REGINFO_SENTINEL
5134 };
5135 define_arm_cp_regs(cpu, vpidr_regs);
4771cd01 5136 define_arm_cp_regs(cpu, el3_no_el2_cp_reginfo);
d42e3c26 5137 }
3b685ba7 5138 }
81547d66 5139 if (arm_feature(env, ARM_FEATURE_EL3)) {
0f1a3b24 5140 define_arm_cp_regs(cpu, el3_cp_reginfo);
e24fdd23
PM
5141 ARMCPRegInfo el3_regs[] = {
5142 { .name = "RVBAR_EL3", .state = ARM_CP_STATE_AA64,
5143 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 1,
5144 .type = ARM_CP_CONST, .access = PL3_R, .resetvalue = cpu->rvbar },
5145 { .name = "SCTLR_EL3", .state = ARM_CP_STATE_AA64,
5146 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 0,
5147 .access = PL3_RW,
5148 .raw_writefn = raw_write, .writefn = sctlr_write,
5149 .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[3]),
5150 .resetvalue = cpu->reset_sctlr },
5151 REGINFO_SENTINEL
be8e8128 5152 };
e24fdd23
PM
5153
5154 define_arm_cp_regs(cpu, el3_regs);
81547d66 5155 }
2f027fc5
PM
5156 /* The behaviour of NSACR is sufficiently various that we don't
5157 * try to describe it in a single reginfo:
5158 * if EL3 is 64 bit, then trap to EL3 from S EL1,
5159 * reads as constant 0xc00 from NS EL1 and NS EL2
5160 * if EL3 is 32 bit, then RW at EL3, RO at NS EL1 and NS EL2
5161 * if v7 without EL3, register doesn't exist
5162 * if v8 without EL3, reads as constant 0xc00 from NS EL1 and NS EL2
5163 */
5164 if (arm_feature(env, ARM_FEATURE_EL3)) {
5165 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
5166 ARMCPRegInfo nsacr = {
5167 .name = "NSACR", .type = ARM_CP_CONST,
5168 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
5169 .access = PL1_RW, .accessfn = nsacr_access,
5170 .resetvalue = 0xc00
5171 };
5172 define_one_arm_cp_reg(cpu, &nsacr);
5173 } else {
5174 ARMCPRegInfo nsacr = {
5175 .name = "NSACR",
5176 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
5177 .access = PL3_RW | PL1_R,
5178 .resetvalue = 0,
5179 .fieldoffset = offsetof(CPUARMState, cp15.nsacr)
5180 };
5181 define_one_arm_cp_reg(cpu, &nsacr);
5182 }
5183 } else {
5184 if (arm_feature(env, ARM_FEATURE_V8)) {
5185 ARMCPRegInfo nsacr = {
5186 .name = "NSACR", .type = ARM_CP_CONST,
5187 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
5188 .access = PL1_R,
5189 .resetvalue = 0xc00
5190 };
5191 define_one_arm_cp_reg(cpu, &nsacr);
5192 }
5193 }
5194
452a0955 5195 if (arm_feature(env, ARM_FEATURE_PMSA)) {
6cb0b013
PC
5196 if (arm_feature(env, ARM_FEATURE_V6)) {
5197 /* PMSAv6 not implemented */
5198 assert(arm_feature(env, ARM_FEATURE_V7));
5199 define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
5200 define_arm_cp_regs(cpu, pmsav7_cp_reginfo);
5201 } else {
5202 define_arm_cp_regs(cpu, pmsav5_cp_reginfo);
5203 }
18032bec 5204 } else {
8e5d75c9 5205 define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
18032bec
PM
5206 define_arm_cp_regs(cpu, vmsa_cp_reginfo);
5207 }
c326b979
PM
5208 if (arm_feature(env, ARM_FEATURE_THUMB2EE)) {
5209 define_arm_cp_regs(cpu, t2ee_cp_reginfo);
5210 }
6cc7a3ae
PM
5211 if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) {
5212 define_arm_cp_regs(cpu, generic_timer_cp_reginfo);
5213 }
4a501606
PM
5214 if (arm_feature(env, ARM_FEATURE_VAPA)) {
5215 define_arm_cp_regs(cpu, vapa_cp_reginfo);
5216 }
c4804214
PM
5217 if (arm_feature(env, ARM_FEATURE_CACHE_TEST_CLEAN)) {
5218 define_arm_cp_regs(cpu, cache_test_clean_cp_reginfo);
5219 }
5220 if (arm_feature(env, ARM_FEATURE_CACHE_DIRTY_REG)) {
5221 define_arm_cp_regs(cpu, cache_dirty_status_cp_reginfo);
5222 }
5223 if (arm_feature(env, ARM_FEATURE_CACHE_BLOCK_OPS)) {
5224 define_arm_cp_regs(cpu, cache_block_ops_cp_reginfo);
5225 }
18032bec
PM
5226 if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
5227 define_arm_cp_regs(cpu, omap_cp_reginfo);
5228 }
34f90529
PM
5229 if (arm_feature(env, ARM_FEATURE_STRONGARM)) {
5230 define_arm_cp_regs(cpu, strongarm_cp_reginfo);
5231 }
1047b9d7
PM
5232 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
5233 define_arm_cp_regs(cpu, xscale_cp_reginfo);
5234 }
5235 if (arm_feature(env, ARM_FEATURE_DUMMY_C15_REGS)) {
5236 define_arm_cp_regs(cpu, dummy_c15_cp_reginfo);
5237 }
7ac681cf
PM
5238 if (arm_feature(env, ARM_FEATURE_LPAE)) {
5239 define_arm_cp_regs(cpu, lpae_cp_reginfo);
5240 }
7884849c
PM
5241 /* Slightly awkwardly, the OMAP and StrongARM cores need all of
5242 * cp15 crn=0 to be writes-ignored, whereas for other cores they should
5243 * be read-only (ie write causes UNDEF exception).
5244 */
5245 {
00a29f3d
PM
5246 ARMCPRegInfo id_pre_v8_midr_cp_reginfo[] = {
5247 /* Pre-v8 MIDR space.
5248 * Note that the MIDR isn't a simple constant register because
7884849c
PM
5249 * of the TI925 behaviour where writes to another register can
5250 * cause the MIDR value to change.
97ce8d61
PC
5251 *
5252 * Unimplemented registers in the c15 0 0 0 space default to
5253 * MIDR. Define MIDR first as this entire space, then CTR, TCMTR
5254 * and friends override accordingly.
7884849c
PM
5255 */
5256 { .name = "MIDR",
97ce8d61 5257 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = CP_ANY,
7884849c 5258 .access = PL1_R, .resetvalue = cpu->midr,
d4e6df63 5259 .writefn = arm_cp_write_ignore, .raw_writefn = raw_write,
731de9e6 5260 .readfn = midr_read,
97ce8d61
PC
5261 .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
5262 .type = ARM_CP_OVERRIDE },
7884849c
PM
5263 /* crn = 0 op1 = 0 crm = 3..7 : currently unassigned; we RAZ. */
5264 { .name = "DUMMY",
5265 .cp = 15, .crn = 0, .crm = 3, .opc1 = 0, .opc2 = CP_ANY,
5266 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
5267 { .name = "DUMMY",
5268 .cp = 15, .crn = 0, .crm = 4, .opc1 = 0, .opc2 = CP_ANY,
5269 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
5270 { .name = "DUMMY",
5271 .cp = 15, .crn = 0, .crm = 5, .opc1 = 0, .opc2 = CP_ANY,
5272 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
5273 { .name = "DUMMY",
5274 .cp = 15, .crn = 0, .crm = 6, .opc1 = 0, .opc2 = CP_ANY,
5275 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
5276 { .name = "DUMMY",
5277 .cp = 15, .crn = 0, .crm = 7, .opc1 = 0, .opc2 = CP_ANY,
5278 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
5279 REGINFO_SENTINEL
5280 };
00a29f3d 5281 ARMCPRegInfo id_v8_midr_cp_reginfo[] = {
00a29f3d
PM
5282 { .name = "MIDR_EL1", .state = ARM_CP_STATE_BOTH,
5283 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 0,
731de9e6
EI
5284 .access = PL1_R, .type = ARM_CP_NO_RAW, .resetvalue = cpu->midr,
5285 .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
5286 .readfn = midr_read },
ac00c79f
SF
5287 /* crn = 0 op1 = 0 crm = 0 op2 = 4,7 : AArch32 aliases of MIDR */
5288 { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
5289 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
5290 .access = PL1_R, .resetvalue = cpu->midr },
5291 { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
5292 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 7,
5293 .access = PL1_R, .resetvalue = cpu->midr },
00a29f3d
PM
5294 { .name = "REVIDR_EL1", .state = ARM_CP_STATE_BOTH,
5295 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 6,
13b72b2b 5296 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->revidr },
00a29f3d
PM
5297 REGINFO_SENTINEL
5298 };
5299 ARMCPRegInfo id_cp_reginfo[] = {
5300 /* These are common to v8 and pre-v8 */
5301 { .name = "CTR",
5302 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 1,
5303 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
5304 { .name = "CTR_EL0", .state = ARM_CP_STATE_AA64,
5305 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 0, .crm = 0,
5306 .access = PL0_R, .accessfn = ctr_el0_access,
5307 .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
5308 /* TCMTR and TLBTR exist in v8 but have no 64-bit versions */
5309 { .name = "TCMTR",
5310 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 2,
5311 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
00a29f3d
PM
5312 REGINFO_SENTINEL
5313 };
8085ce63
PC
5314 /* TLBTR is specific to VMSA */
5315 ARMCPRegInfo id_tlbtr_reginfo = {
5316 .name = "TLBTR",
5317 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 3,
5318 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0,
5319 };
3281af81
PC
5320 /* MPUIR is specific to PMSA V6+ */
5321 ARMCPRegInfo id_mpuir_reginfo = {
5322 .name = "MPUIR",
5323 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
5324 .access = PL1_R, .type = ARM_CP_CONST,
5325 .resetvalue = cpu->pmsav7_dregion << 8
5326 };
7884849c
PM
5327 ARMCPRegInfo crn0_wi_reginfo = {
5328 .name = "CRN0_WI", .cp = 15, .crn = 0, .crm = CP_ANY,
5329 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_W,
5330 .type = ARM_CP_NOP | ARM_CP_OVERRIDE
5331 };
5332 if (arm_feature(env, ARM_FEATURE_OMAPCP) ||
5333 arm_feature(env, ARM_FEATURE_STRONGARM)) {
5334 ARMCPRegInfo *r;
5335 /* Register the blanket "writes ignored" value first to cover the
a703eda1
PC
5336 * whole space. Then update the specific ID registers to allow write
5337 * access, so that they ignore writes rather than causing them to
5338 * UNDEF.
7884849c
PM
5339 */
5340 define_one_arm_cp_reg(cpu, &crn0_wi_reginfo);
00a29f3d
PM
5341 for (r = id_pre_v8_midr_cp_reginfo;
5342 r->type != ARM_CP_SENTINEL; r++) {
5343 r->access = PL1_RW;
5344 }
7884849c
PM
5345 for (r = id_cp_reginfo; r->type != ARM_CP_SENTINEL; r++) {
5346 r->access = PL1_RW;
7884849c 5347 }
8085ce63 5348 id_tlbtr_reginfo.access = PL1_RW;
3281af81 5349 id_tlbtr_reginfo.access = PL1_RW;
7884849c 5350 }
00a29f3d
PM
5351 if (arm_feature(env, ARM_FEATURE_V8)) {
5352 define_arm_cp_regs(cpu, id_v8_midr_cp_reginfo);
5353 } else {
5354 define_arm_cp_regs(cpu, id_pre_v8_midr_cp_reginfo);
5355 }
a703eda1 5356 define_arm_cp_regs(cpu, id_cp_reginfo);
452a0955 5357 if (!arm_feature(env, ARM_FEATURE_PMSA)) {
8085ce63 5358 define_one_arm_cp_reg(cpu, &id_tlbtr_reginfo);
3281af81
PC
5359 } else if (arm_feature(env, ARM_FEATURE_V7)) {
5360 define_one_arm_cp_reg(cpu, &id_mpuir_reginfo);
8085ce63 5361 }
7884849c
PM
5362 }
5363
97ce8d61
PC
5364 if (arm_feature(env, ARM_FEATURE_MPIDR)) {
5365 define_arm_cp_regs(cpu, mpidr_cp_reginfo);
5366 }
5367
2771db27 5368 if (arm_feature(env, ARM_FEATURE_AUXCR)) {
834a6c69
PM
5369 ARMCPRegInfo auxcr_reginfo[] = {
5370 { .name = "ACTLR_EL1", .state = ARM_CP_STATE_BOTH,
5371 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 1,
5372 .access = PL1_RW, .type = ARM_CP_CONST,
5373 .resetvalue = cpu->reset_auxcr },
5374 { .name = "ACTLR_EL2", .state = ARM_CP_STATE_BOTH,
5375 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 1,
5376 .access = PL2_RW, .type = ARM_CP_CONST,
5377 .resetvalue = 0 },
5378 { .name = "ACTLR_EL3", .state = ARM_CP_STATE_AA64,
5379 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 1,
5380 .access = PL3_RW, .type = ARM_CP_CONST,
5381 .resetvalue = 0 },
5382 REGINFO_SENTINEL
2771db27 5383 };
834a6c69 5384 define_arm_cp_regs(cpu, auxcr_reginfo);
2771db27
PM
5385 }
5386
d8ba780b 5387 if (arm_feature(env, ARM_FEATURE_CBAR)) {
f318cec6
PM
5388 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
5389 /* 32 bit view is [31:18] 0...0 [43:32]. */
5390 uint32_t cbar32 = (extract64(cpu->reset_cbar, 18, 14) << 18)
5391 | extract64(cpu->reset_cbar, 32, 12);
5392 ARMCPRegInfo cbar_reginfo[] = {
5393 { .name = "CBAR",
5394 .type = ARM_CP_CONST,
5395 .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0,
5396 .access = PL1_R, .resetvalue = cpu->reset_cbar },
5397 { .name = "CBAR_EL1", .state = ARM_CP_STATE_AA64,
5398 .type = ARM_CP_CONST,
5399 .opc0 = 3, .opc1 = 1, .crn = 15, .crm = 3, .opc2 = 0,
5400 .access = PL1_R, .resetvalue = cbar32 },
5401 REGINFO_SENTINEL
5402 };
5403 /* We don't implement a r/w 64 bit CBAR currently */
5404 assert(arm_feature(env, ARM_FEATURE_CBAR_RO));
5405 define_arm_cp_regs(cpu, cbar_reginfo);
5406 } else {
5407 ARMCPRegInfo cbar = {
5408 .name = "CBAR",
5409 .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0,
5410 .access = PL1_R|PL3_W, .resetvalue = cpu->reset_cbar,
5411 .fieldoffset = offsetof(CPUARMState,
5412 cp15.c15_config_base_address)
5413 };
5414 if (arm_feature(env, ARM_FEATURE_CBAR_RO)) {
5415 cbar.access = PL1_R;
5416 cbar.fieldoffset = 0;
5417 cbar.type = ARM_CP_CONST;
5418 }
5419 define_one_arm_cp_reg(cpu, &cbar);
5420 }
d8ba780b
PC
5421 }
5422
91db4642
CLG
5423 if (arm_feature(env, ARM_FEATURE_VBAR)) {
5424 ARMCPRegInfo vbar_cp_reginfo[] = {
5425 { .name = "VBAR", .state = ARM_CP_STATE_BOTH,
5426 .opc0 = 3, .crn = 12, .crm = 0, .opc1 = 0, .opc2 = 0,
5427 .access = PL1_RW, .writefn = vbar_write,
5428 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.vbar_s),
5429 offsetof(CPUARMState, cp15.vbar_ns) },
5430 .resetvalue = 0 },
5431 REGINFO_SENTINEL
5432 };
5433 define_arm_cp_regs(cpu, vbar_cp_reginfo);
5434 }
5435
2771db27
PM
5436 /* Generic registers whose values depend on the implementation */
5437 {
5438 ARMCPRegInfo sctlr = {
5ebafdf3 5439 .name = "SCTLR", .state = ARM_CP_STATE_BOTH,
137feaa9
FA
5440 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
5441 .access = PL1_RW,
5442 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.sctlr_s),
5443 offsetof(CPUARMState, cp15.sctlr_ns) },
d4e6df63
PM
5444 .writefn = sctlr_write, .resetvalue = cpu->reset_sctlr,
5445 .raw_writefn = raw_write,
2771db27
PM
5446 };
5447 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
5448 /* Normally we would always end the TB on an SCTLR write, but Linux
5449 * arch/arm/mach-pxa/sleep.S expects two instructions following
5450 * an MMU enable to execute from cache. Imitate this behaviour.
5451 */
5452 sctlr.type |= ARM_CP_SUPPRESS_TB_END;
5453 }
5454 define_one_arm_cp_reg(cpu, &sctlr);
5455 }
5be5e8ed
RH
5456
5457 if (arm_feature(env, ARM_FEATURE_SVE)) {
5458 define_one_arm_cp_reg(cpu, &zcr_el1_reginfo);
5459 if (arm_feature(env, ARM_FEATURE_EL2)) {
5460 define_one_arm_cp_reg(cpu, &zcr_el2_reginfo);
5461 } else {
5462 define_one_arm_cp_reg(cpu, &zcr_no_el2_reginfo);
5463 }
5464 if (arm_feature(env, ARM_FEATURE_EL3)) {
5465 define_one_arm_cp_reg(cpu, &zcr_el3_reginfo);
5466 }
5467 }
2ceb98c0
PM
5468}
5469
14969266
AF
5470void arm_cpu_register_gdb_regs_for_features(ARMCPU *cpu)
5471{
22169d41 5472 CPUState *cs = CPU(cpu);
14969266
AF
5473 CPUARMState *env = &cpu->env;
5474
6a669427
PM
5475 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
5476 gdb_register_coprocessor(cs, aarch64_fpu_gdb_get_reg,
5477 aarch64_fpu_gdb_set_reg,
5478 34, "aarch64-fpu.xml", 0);
5479 } else if (arm_feature(env, ARM_FEATURE_NEON)) {
22169d41 5480 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
56aebc89
PB
5481 51, "arm-neon.xml", 0);
5482 } else if (arm_feature(env, ARM_FEATURE_VFP3)) {
22169d41 5483 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
56aebc89
PB
5484 35, "arm-vfp3.xml", 0);
5485 } else if (arm_feature(env, ARM_FEATURE_VFP)) {
22169d41 5486 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
56aebc89
PB
5487 19, "arm-vfp.xml", 0);
5488 }
40f137e1
PB
5489}
5490
777dc784
PM
5491/* Sort alphabetically by type name, except for "any". */
5492static gint arm_cpu_list_compare(gconstpointer a, gconstpointer b)
5adb4839 5493{
777dc784
PM
5494 ObjectClass *class_a = (ObjectClass *)a;
5495 ObjectClass *class_b = (ObjectClass *)b;
5496 const char *name_a, *name_b;
5adb4839 5497
777dc784
PM
5498 name_a = object_class_get_name(class_a);
5499 name_b = object_class_get_name(class_b);
51492fd1 5500 if (strcmp(name_a, "any-" TYPE_ARM_CPU) == 0) {
777dc784 5501 return 1;
51492fd1 5502 } else if (strcmp(name_b, "any-" TYPE_ARM_CPU) == 0) {
777dc784
PM
5503 return -1;
5504 } else {
5505 return strcmp(name_a, name_b);
5adb4839
PB
5506 }
5507}
5508
777dc784 5509static void arm_cpu_list_entry(gpointer data, gpointer user_data)
40f137e1 5510{
777dc784 5511 ObjectClass *oc = data;
92a31361 5512 CPUListState *s = user_data;
51492fd1
AF
5513 const char *typename;
5514 char *name;
3371d272 5515
51492fd1
AF
5516 typename = object_class_get_name(oc);
5517 name = g_strndup(typename, strlen(typename) - strlen("-" TYPE_ARM_CPU));
777dc784 5518 (*s->cpu_fprintf)(s->file, " %s\n",
51492fd1
AF
5519 name);
5520 g_free(name);
777dc784
PM
5521}
5522
5523void arm_cpu_list(FILE *f, fprintf_function cpu_fprintf)
5524{
92a31361 5525 CPUListState s = {
777dc784
PM
5526 .file = f,
5527 .cpu_fprintf = cpu_fprintf,
5528 };
5529 GSList *list;
5530
5531 list = object_class_get_list(TYPE_ARM_CPU, false);
5532 list = g_slist_sort(list, arm_cpu_list_compare);
5533 (*cpu_fprintf)(f, "Available CPUs:\n");
5534 g_slist_foreach(list, arm_cpu_list_entry, &s);
5535 g_slist_free(list);
a96c0514
PM
5536#ifdef CONFIG_KVM
5537 /* The 'host' CPU type is dynamically registered only if KVM is
5538 * enabled, so we have to special-case it here:
5539 */
5540 (*cpu_fprintf)(f, " host (only available in KVM mode)\n");
5541#endif
40f137e1
PB
5542}
5543
78027bb6
CR
5544static void arm_cpu_add_definition(gpointer data, gpointer user_data)
5545{
5546 ObjectClass *oc = data;
5547 CpuDefinitionInfoList **cpu_list = user_data;
5548 CpuDefinitionInfoList *entry;
5549 CpuDefinitionInfo *info;
5550 const char *typename;
5551
5552 typename = object_class_get_name(oc);
5553 info = g_malloc0(sizeof(*info));
5554 info->name = g_strndup(typename,
5555 strlen(typename) - strlen("-" TYPE_ARM_CPU));
8ed877b7 5556 info->q_typename = g_strdup(typename);
78027bb6
CR
5557
5558 entry = g_malloc0(sizeof(*entry));
5559 entry->value = info;
5560 entry->next = *cpu_list;
5561 *cpu_list = entry;
5562}
5563
5564CpuDefinitionInfoList *arch_query_cpu_definitions(Error **errp)
5565{
5566 CpuDefinitionInfoList *cpu_list = NULL;
5567 GSList *list;
5568
5569 list = object_class_get_list(TYPE_ARM_CPU, false);
5570 g_slist_foreach(list, arm_cpu_add_definition, &cpu_list);
5571 g_slist_free(list);
5572
5573 return cpu_list;
5574}
5575
6e6efd61 5576static void add_cpreg_to_hashtable(ARMCPU *cpu, const ARMCPRegInfo *r,
51a79b03 5577 void *opaque, int state, int secstate,
f5a0a5a5 5578 int crm, int opc1, int opc2)
6e6efd61
PM
5579{
5580 /* Private utility function for define_one_arm_cp_reg_with_opaque():
5581 * add a single reginfo struct to the hash table.
5582 */
5583 uint32_t *key = g_new(uint32_t, 1);
5584 ARMCPRegInfo *r2 = g_memdup(r, sizeof(ARMCPRegInfo));
5585 int is64 = (r->type & ARM_CP_64BIT) ? 1 : 0;
3f3c82a5
FA
5586 int ns = (secstate & ARM_CP_SECSTATE_NS) ? 1 : 0;
5587
5588 /* Reset the secure state to the specific incoming state. This is
5589 * necessary as the register may have been defined with both states.
5590 */
5591 r2->secure = secstate;
5592
5593 if (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1]) {
5594 /* Register is banked (using both entries in array).
5595 * Overwriting fieldoffset as the array is only used to define
5596 * banked registers but later only fieldoffset is used.
f5a0a5a5 5597 */
3f3c82a5
FA
5598 r2->fieldoffset = r->bank_fieldoffsets[ns];
5599 }
5600
5601 if (state == ARM_CP_STATE_AA32) {
5602 if (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1]) {
5603 /* If the register is banked then we don't need to migrate or
5604 * reset the 32-bit instance in certain cases:
5605 *
5606 * 1) If the register has both 32-bit and 64-bit instances then we
5607 * can count on the 64-bit instance taking care of the
5608 * non-secure bank.
5609 * 2) If ARMv8 is enabled then we can count on a 64-bit version
5610 * taking care of the secure bank. This requires that separate
5611 * 32 and 64-bit definitions are provided.
5612 */
5613 if ((r->state == ARM_CP_STATE_BOTH && ns) ||
5614 (arm_feature(&cpu->env, ARM_FEATURE_V8) && !ns)) {
7a0e58fa 5615 r2->type |= ARM_CP_ALIAS;
3f3c82a5
FA
5616 }
5617 } else if ((secstate != r->secure) && !ns) {
5618 /* The register is not banked so we only want to allow migration of
5619 * the non-secure instance.
5620 */
7a0e58fa 5621 r2->type |= ARM_CP_ALIAS;
58a1d8ce 5622 }
3f3c82a5
FA
5623
5624 if (r->state == ARM_CP_STATE_BOTH) {
5625 /* We assume it is a cp15 register if the .cp field is left unset.
5626 */
5627 if (r2->cp == 0) {
5628 r2->cp = 15;
5629 }
5630
f5a0a5a5 5631#ifdef HOST_WORDS_BIGENDIAN
3f3c82a5
FA
5632 if (r2->fieldoffset) {
5633 r2->fieldoffset += sizeof(uint32_t);
5634 }
f5a0a5a5 5635#endif
3f3c82a5 5636 }
f5a0a5a5
PM
5637 }
5638 if (state == ARM_CP_STATE_AA64) {
5639 /* To allow abbreviation of ARMCPRegInfo
5640 * definitions, we treat cp == 0 as equivalent to
5641 * the value for "standard guest-visible sysreg".
58a1d8ce
PM
5642 * STATE_BOTH definitions are also always "standard
5643 * sysreg" in their AArch64 view (the .cp value may
5644 * be non-zero for the benefit of the AArch32 view).
f5a0a5a5 5645 */
58a1d8ce 5646 if (r->cp == 0 || r->state == ARM_CP_STATE_BOTH) {
f5a0a5a5
PM
5647 r2->cp = CP_REG_ARM64_SYSREG_CP;
5648 }
5649 *key = ENCODE_AA64_CP_REG(r2->cp, r2->crn, crm,
5650 r2->opc0, opc1, opc2);
5651 } else {
51a79b03 5652 *key = ENCODE_CP_REG(r2->cp, is64, ns, r2->crn, crm, opc1, opc2);
f5a0a5a5 5653 }
6e6efd61
PM
5654 if (opaque) {
5655 r2->opaque = opaque;
5656 }
67ed771d
PM
5657 /* reginfo passed to helpers is correct for the actual access,
5658 * and is never ARM_CP_STATE_BOTH:
5659 */
5660 r2->state = state;
6e6efd61
PM
5661 /* Make sure reginfo passed to helpers for wildcarded regs
5662 * has the correct crm/opc1/opc2 for this reg, not CP_ANY:
5663 */
5664 r2->crm = crm;
5665 r2->opc1 = opc1;
5666 r2->opc2 = opc2;
5667 /* By convention, for wildcarded registers only the first
5668 * entry is used for migration; the others are marked as
7a0e58fa 5669 * ALIAS so we don't try to transfer the register
6e6efd61 5670 * multiple times. Special registers (ie NOP/WFI) are
7a0e58fa 5671 * never migratable and not even raw-accessible.
6e6efd61 5672 */
7a0e58fa
PM
5673 if ((r->type & ARM_CP_SPECIAL)) {
5674 r2->type |= ARM_CP_NO_RAW;
5675 }
5676 if (((r->crm == CP_ANY) && crm != 0) ||
6e6efd61
PM
5677 ((r->opc1 == CP_ANY) && opc1 != 0) ||
5678 ((r->opc2 == CP_ANY) && opc2 != 0)) {
7a0e58fa 5679 r2->type |= ARM_CP_ALIAS;
6e6efd61
PM
5680 }
5681
375421cc
PM
5682 /* Check that raw accesses are either forbidden or handled. Note that
5683 * we can't assert this earlier because the setup of fieldoffset for
5684 * banked registers has to be done first.
5685 */
5686 if (!(r2->type & ARM_CP_NO_RAW)) {
5687 assert(!raw_accessors_invalid(r2));
5688 }
5689
6e6efd61
PM
5690 /* Overriding of an existing definition must be explicitly
5691 * requested.
5692 */
5693 if (!(r->type & ARM_CP_OVERRIDE)) {
5694 ARMCPRegInfo *oldreg;
5695 oldreg = g_hash_table_lookup(cpu->cp_regs, key);
5696 if (oldreg && !(oldreg->type & ARM_CP_OVERRIDE)) {
5697 fprintf(stderr, "Register redefined: cp=%d %d bit "
5698 "crn=%d crm=%d opc1=%d opc2=%d, "
5699 "was %s, now %s\n", r2->cp, 32 + 32 * is64,
5700 r2->crn, r2->crm, r2->opc1, r2->opc2,
5701 oldreg->name, r2->name);
5702 g_assert_not_reached();
5703 }
5704 }
5705 g_hash_table_insert(cpu->cp_regs, key, r2);
5706}
5707
5708
4b6a83fb
PM
5709void define_one_arm_cp_reg_with_opaque(ARMCPU *cpu,
5710 const ARMCPRegInfo *r, void *opaque)
5711{
5712 /* Define implementations of coprocessor registers.
5713 * We store these in a hashtable because typically
5714 * there are less than 150 registers in a space which
5715 * is 16*16*16*8*8 = 262144 in size.
5716 * Wildcarding is supported for the crm, opc1 and opc2 fields.
5717 * If a register is defined twice then the second definition is
5718 * used, so this can be used to define some generic registers and
5719 * then override them with implementation specific variations.
5720 * At least one of the original and the second definition should
5721 * include ARM_CP_OVERRIDE in its type bits -- this is just a guard
5722 * against accidental use.
f5a0a5a5
PM
5723 *
5724 * The state field defines whether the register is to be
5725 * visible in the AArch32 or AArch64 execution state. If the
5726 * state is set to ARM_CP_STATE_BOTH then we synthesise a
5727 * reginfo structure for the AArch32 view, which sees the lower
5728 * 32 bits of the 64 bit register.
5729 *
5730 * Only registers visible in AArch64 may set r->opc0; opc0 cannot
5731 * be wildcarded. AArch64 registers are always considered to be 64
5732 * bits; the ARM_CP_64BIT* flag applies only to the AArch32 view of
5733 * the register, if any.
4b6a83fb 5734 */
f5a0a5a5 5735 int crm, opc1, opc2, state;
4b6a83fb
PM
5736 int crmmin = (r->crm == CP_ANY) ? 0 : r->crm;
5737 int crmmax = (r->crm == CP_ANY) ? 15 : r->crm;
5738 int opc1min = (r->opc1 == CP_ANY) ? 0 : r->opc1;
5739 int opc1max = (r->opc1 == CP_ANY) ? 7 : r->opc1;
5740 int opc2min = (r->opc2 == CP_ANY) ? 0 : r->opc2;
5741 int opc2max = (r->opc2 == CP_ANY) ? 7 : r->opc2;
5742 /* 64 bit registers have only CRm and Opc1 fields */
5743 assert(!((r->type & ARM_CP_64BIT) && (r->opc2 || r->crn)));
f5a0a5a5
PM
5744 /* op0 only exists in the AArch64 encodings */
5745 assert((r->state != ARM_CP_STATE_AA32) || (r->opc0 == 0));
5746 /* AArch64 regs are all 64 bit so ARM_CP_64BIT is meaningless */
5747 assert((r->state != ARM_CP_STATE_AA64) || !(r->type & ARM_CP_64BIT));
5748 /* The AArch64 pseudocode CheckSystemAccess() specifies that op1
5749 * encodes a minimum access level for the register. We roll this
5750 * runtime check into our general permission check code, so check
5751 * here that the reginfo's specified permissions are strict enough
5752 * to encompass the generic architectural permission check.
5753 */
5754 if (r->state != ARM_CP_STATE_AA32) {
5755 int mask = 0;
5756 switch (r->opc1) {
5757 case 0: case 1: case 2:
5758 /* min_EL EL1 */
5759 mask = PL1_RW;
5760 break;
5761 case 3:
5762 /* min_EL EL0 */
5763 mask = PL0_RW;
5764 break;
5765 case 4:
5766 /* min_EL EL2 */
5767 mask = PL2_RW;
5768 break;
5769 case 5:
5770 /* unallocated encoding, so not possible */
5771 assert(false);
5772 break;
5773 case 6:
5774 /* min_EL EL3 */
5775 mask = PL3_RW;
5776 break;
5777 case 7:
5778 /* min_EL EL1, secure mode only (we don't check the latter) */
5779 mask = PL1_RW;
5780 break;
5781 default:
5782 /* broken reginfo with out-of-range opc1 */
5783 assert(false);
5784 break;
5785 }
5786 /* assert our permissions are not too lax (stricter is fine) */
5787 assert((r->access & ~mask) == 0);
5788 }
5789
4b6a83fb
PM
5790 /* Check that the register definition has enough info to handle
5791 * reads and writes if they are permitted.
5792 */
5793 if (!(r->type & (ARM_CP_SPECIAL|ARM_CP_CONST))) {
5794 if (r->access & PL3_R) {
3f3c82a5
FA
5795 assert((r->fieldoffset ||
5796 (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
5797 r->readfn);
4b6a83fb
PM
5798 }
5799 if (r->access & PL3_W) {
3f3c82a5
FA
5800 assert((r->fieldoffset ||
5801 (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
5802 r->writefn);
4b6a83fb
PM
5803 }
5804 }
5805 /* Bad type field probably means missing sentinel at end of reg list */
5806 assert(cptype_valid(r->type));
5807 for (crm = crmmin; crm <= crmmax; crm++) {
5808 for (opc1 = opc1min; opc1 <= opc1max; opc1++) {
5809 for (opc2 = opc2min; opc2 <= opc2max; opc2++) {
f5a0a5a5
PM
5810 for (state = ARM_CP_STATE_AA32;
5811 state <= ARM_CP_STATE_AA64; state++) {
5812 if (r->state != state && r->state != ARM_CP_STATE_BOTH) {
5813 continue;
5814 }
3f3c82a5
FA
5815 if (state == ARM_CP_STATE_AA32) {
5816 /* Under AArch32 CP registers can be common
5817 * (same for secure and non-secure world) or banked.
5818 */
5819 switch (r->secure) {
5820 case ARM_CP_SECSTATE_S:
5821 case ARM_CP_SECSTATE_NS:
5822 add_cpreg_to_hashtable(cpu, r, opaque, state,
5823 r->secure, crm, opc1, opc2);
5824 break;
5825 default:
5826 add_cpreg_to_hashtable(cpu, r, opaque, state,
5827 ARM_CP_SECSTATE_S,
5828 crm, opc1, opc2);
5829 add_cpreg_to_hashtable(cpu, r, opaque, state,
5830 ARM_CP_SECSTATE_NS,
5831 crm, opc1, opc2);
5832 break;
5833 }
5834 } else {
5835 /* AArch64 registers get mapped to non-secure instance
5836 * of AArch32 */
5837 add_cpreg_to_hashtable(cpu, r, opaque, state,
5838 ARM_CP_SECSTATE_NS,
5839 crm, opc1, opc2);
5840 }
f5a0a5a5 5841 }
4b6a83fb
PM
5842 }
5843 }
5844 }
5845}
5846
5847void define_arm_cp_regs_with_opaque(ARMCPU *cpu,
5848 const ARMCPRegInfo *regs, void *opaque)
5849{
5850 /* Define a whole list of registers */
5851 const ARMCPRegInfo *r;
5852 for (r = regs; r->type != ARM_CP_SENTINEL; r++) {
5853 define_one_arm_cp_reg_with_opaque(cpu, r, opaque);
5854 }
5855}
5856
60322b39 5857const ARMCPRegInfo *get_arm_cp_reginfo(GHashTable *cpregs, uint32_t encoded_cp)
4b6a83fb 5858{
60322b39 5859 return g_hash_table_lookup(cpregs, &encoded_cp);
4b6a83fb
PM
5860}
5861
c4241c7d
PM
5862void arm_cp_write_ignore(CPUARMState *env, const ARMCPRegInfo *ri,
5863 uint64_t value)
4b6a83fb
PM
5864{
5865 /* Helper coprocessor write function for write-ignore registers */
4b6a83fb
PM
5866}
5867
c4241c7d 5868uint64_t arm_cp_read_zero(CPUARMState *env, const ARMCPRegInfo *ri)
4b6a83fb
PM
5869{
5870 /* Helper coprocessor write function for read-as-zero registers */
4b6a83fb
PM
5871 return 0;
5872}
5873
f5a0a5a5
PM
5874void arm_cp_reset_ignore(CPUARMState *env, const ARMCPRegInfo *opaque)
5875{
5876 /* Helper coprocessor reset function for do-nothing-on-reset registers */
5877}
5878
af393ffc 5879static int bad_mode_switch(CPUARMState *env, int mode, CPSRWriteType write_type)
37064a8b
PM
5880{
5881 /* Return true if it is not valid for us to switch to
5882 * this CPU mode (ie all the UNPREDICTABLE cases in
5883 * the ARM ARM CPSRWriteByInstr pseudocode).
5884 */
af393ffc
PM
5885
5886 /* Changes to or from Hyp via MSR and CPS are illegal. */
5887 if (write_type == CPSRWriteByInstr &&
5888 ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_HYP ||
5889 mode == ARM_CPU_MODE_HYP)) {
5890 return 1;
5891 }
5892
37064a8b
PM
5893 switch (mode) {
5894 case ARM_CPU_MODE_USR:
10eacda7 5895 return 0;
37064a8b
PM
5896 case ARM_CPU_MODE_SYS:
5897 case ARM_CPU_MODE_SVC:
5898 case ARM_CPU_MODE_ABT:
5899 case ARM_CPU_MODE_UND:
5900 case ARM_CPU_MODE_IRQ:
5901 case ARM_CPU_MODE_FIQ:
52ff951b
PM
5902 /* Note that we don't implement the IMPDEF NSACR.RFR which in v7
5903 * allows FIQ mode to be Secure-only. (In v8 this doesn't exist.)
5904 */
10eacda7
PM
5905 /* If HCR.TGE is set then changes from Monitor to NS PL1 via MSR
5906 * and CPS are treated as illegal mode changes.
5907 */
5908 if (write_type == CPSRWriteByInstr &&
5909 (env->cp15.hcr_el2 & HCR_TGE) &&
5910 (env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON &&
5911 !arm_is_secure_below_el3(env)) {
5912 return 1;
5913 }
37064a8b 5914 return 0;
e6c8fc07
PM
5915 case ARM_CPU_MODE_HYP:
5916 return !arm_feature(env, ARM_FEATURE_EL2)
5917 || arm_current_el(env) < 2 || arm_is_secure(env);
027fc527 5918 case ARM_CPU_MODE_MON:
58ae2d1f 5919 return arm_current_el(env) < 3;
37064a8b
PM
5920 default:
5921 return 1;
5922 }
5923}
5924
2f4a40e5
AZ
5925uint32_t cpsr_read(CPUARMState *env)
5926{
5927 int ZF;
6fbe23d5
PB
5928 ZF = (env->ZF == 0);
5929 return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) |
2f4a40e5
AZ
5930 (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27)
5931 | (env->thumb << 5) | ((env->condexec_bits & 3) << 25)
5932 | ((env->condexec_bits & 0xfc) << 8)
af519934 5933 | (env->GE << 16) | (env->daif & CPSR_AIF);
2f4a40e5
AZ
5934}
5935
50866ba5
PM
5936void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask,
5937 CPSRWriteType write_type)
2f4a40e5 5938{
6e8801f9
FA
5939 uint32_t changed_daif;
5940
2f4a40e5 5941 if (mask & CPSR_NZCV) {
6fbe23d5
PB
5942 env->ZF = (~val) & CPSR_Z;
5943 env->NF = val;
2f4a40e5
AZ
5944 env->CF = (val >> 29) & 1;
5945 env->VF = (val << 3) & 0x80000000;
5946 }
5947 if (mask & CPSR_Q)
5948 env->QF = ((val & CPSR_Q) != 0);
5949 if (mask & CPSR_T)
5950 env->thumb = ((val & CPSR_T) != 0);
5951 if (mask & CPSR_IT_0_1) {
5952 env->condexec_bits &= ~3;
5953 env->condexec_bits |= (val >> 25) & 3;
5954 }
5955 if (mask & CPSR_IT_2_7) {
5956 env->condexec_bits &= 3;
5957 env->condexec_bits |= (val >> 8) & 0xfc;
5958 }
5959 if (mask & CPSR_GE) {
5960 env->GE = (val >> 16) & 0xf;
5961 }
5962
6e8801f9
FA
5963 /* In a V7 implementation that includes the security extensions but does
5964 * not include Virtualization Extensions the SCR.FW and SCR.AW bits control
5965 * whether non-secure software is allowed to change the CPSR_F and CPSR_A
5966 * bits respectively.
5967 *
5968 * In a V8 implementation, it is permitted for privileged software to
5969 * change the CPSR A/F bits regardless of the SCR.AW/FW bits.
5970 */
f8c88bbc 5971 if (write_type != CPSRWriteRaw && !arm_feature(env, ARM_FEATURE_V8) &&
6e8801f9
FA
5972 arm_feature(env, ARM_FEATURE_EL3) &&
5973 !arm_feature(env, ARM_FEATURE_EL2) &&
5974 !arm_is_secure(env)) {
5975
5976 changed_daif = (env->daif ^ val) & mask;
5977
5978 if (changed_daif & CPSR_A) {
5979 /* Check to see if we are allowed to change the masking of async
5980 * abort exceptions from a non-secure state.
5981 */
5982 if (!(env->cp15.scr_el3 & SCR_AW)) {
5983 qemu_log_mask(LOG_GUEST_ERROR,
5984 "Ignoring attempt to switch CPSR_A flag from "
5985 "non-secure world with SCR.AW bit clear\n");
5986 mask &= ~CPSR_A;
5987 }
5988 }
5989
5990 if (changed_daif & CPSR_F) {
5991 /* Check to see if we are allowed to change the masking of FIQ
5992 * exceptions from a non-secure state.
5993 */
5994 if (!(env->cp15.scr_el3 & SCR_FW)) {
5995 qemu_log_mask(LOG_GUEST_ERROR,
5996 "Ignoring attempt to switch CPSR_F flag from "
5997 "non-secure world with SCR.FW bit clear\n");
5998 mask &= ~CPSR_F;
5999 }
6000
6001 /* Check whether non-maskable FIQ (NMFI) support is enabled.
6002 * If this bit is set software is not allowed to mask
6003 * FIQs, but is allowed to set CPSR_F to 0.
6004 */
6005 if ((A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_NMFI) &&
6006 (val & CPSR_F)) {
6007 qemu_log_mask(LOG_GUEST_ERROR,
6008 "Ignoring attempt to enable CPSR_F flag "
6009 "(non-maskable FIQ [NMFI] support enabled)\n");
6010 mask &= ~CPSR_F;
6011 }
6012 }
6013 }
6014
4cc35614
PM
6015 env->daif &= ~(CPSR_AIF & mask);
6016 env->daif |= val & CPSR_AIF & mask;
6017
f8c88bbc
PM
6018 if (write_type != CPSRWriteRaw &&
6019 ((env->uncached_cpsr ^ val) & mask & CPSR_M)) {
8c4f0eb9
PM
6020 if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_USR) {
6021 /* Note that we can only get here in USR mode if this is a
6022 * gdb stub write; for this case we follow the architectural
6023 * behaviour for guest writes in USR mode of ignoring an attempt
6024 * to switch mode. (Those are caught by translate.c for writes
6025 * triggered by guest instructions.)
6026 */
6027 mask &= ~CPSR_M;
6028 } else if (bad_mode_switch(env, val & CPSR_M, write_type)) {
81907a58
PM
6029 /* Attempt to switch to an invalid mode: this is UNPREDICTABLE in
6030 * v7, and has defined behaviour in v8:
6031 * + leave CPSR.M untouched
6032 * + allow changes to the other CPSR fields
6033 * + set PSTATE.IL
6034 * For user changes via the GDB stub, we don't set PSTATE.IL,
6035 * as this would be unnecessarily harsh for a user error.
37064a8b
PM
6036 */
6037 mask &= ~CPSR_M;
81907a58
PM
6038 if (write_type != CPSRWriteByGDBStub &&
6039 arm_feature(env, ARM_FEATURE_V8)) {
6040 mask |= CPSR_IL;
6041 val |= CPSR_IL;
6042 }
37064a8b
PM
6043 } else {
6044 switch_mode(env, val & CPSR_M);
6045 }
2f4a40e5
AZ
6046 }
6047 mask &= ~CACHED_CPSR_BITS;
6048 env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask);
6049}
6050
b26eefb6
PB
6051/* Sign/zero extend */
6052uint32_t HELPER(sxtb16)(uint32_t x)
6053{
6054 uint32_t res;
6055 res = (uint16_t)(int8_t)x;
6056 res |= (uint32_t)(int8_t)(x >> 16) << 16;
6057 return res;
6058}
6059
6060uint32_t HELPER(uxtb16)(uint32_t x)
6061{
6062 uint32_t res;
6063 res = (uint16_t)(uint8_t)x;
6064 res |= (uint32_t)(uint8_t)(x >> 16) << 16;
6065 return res;
6066}
6067
3670669c
PB
6068int32_t HELPER(sdiv)(int32_t num, int32_t den)
6069{
6070 if (den == 0)
6071 return 0;
686eeb93
AJ
6072 if (num == INT_MIN && den == -1)
6073 return INT_MIN;
3670669c
PB
6074 return num / den;
6075}
6076
6077uint32_t HELPER(udiv)(uint32_t num, uint32_t den)
6078{
6079 if (den == 0)
6080 return 0;
6081 return num / den;
6082}
6083
6084uint32_t HELPER(rbit)(uint32_t x)
6085{
42fedbca 6086 return revbit32(x);
3670669c
PB
6087}
6088
5fafdf24 6089#if defined(CONFIG_USER_ONLY)
b5ff1b31 6090
9ee6e8bb 6091/* These should probably raise undefined insn exceptions. */
0ecb72a5 6092void HELPER(v7m_msr)(CPUARMState *env, uint32_t reg, uint32_t val)
9ee6e8bb 6093{
a47dddd7
AF
6094 ARMCPU *cpu = arm_env_get_cpu(env);
6095
6096 cpu_abort(CPU(cpu), "v7m_msr %d\n", reg);
9ee6e8bb
PB
6097}
6098
0ecb72a5 6099uint32_t HELPER(v7m_mrs)(CPUARMState *env, uint32_t reg)
9ee6e8bb 6100{
a47dddd7
AF
6101 ARMCPU *cpu = arm_env_get_cpu(env);
6102
6103 cpu_abort(CPU(cpu), "v7m_mrs %d\n", reg);
9ee6e8bb
PB
6104 return 0;
6105}
6106
fb602cb7
PM
6107void HELPER(v7m_bxns)(CPUARMState *env, uint32_t dest)
6108{
6109 /* translate.c should never generate calls here in user-only mode */
6110 g_assert_not_reached();
6111}
6112
3e3fa230
PM
6113void HELPER(v7m_blxns)(CPUARMState *env, uint32_t dest)
6114{
6115 /* translate.c should never generate calls here in user-only mode */
6116 g_assert_not_reached();
6117}
6118
5158de24
PM
6119uint32_t HELPER(v7m_tt)(CPUARMState *env, uint32_t addr, uint32_t op)
6120{
6121 /* The TT instructions can be used by unprivileged code, but in
6122 * user-only emulation we don't have the MPU.
6123 * Luckily since we know we are NonSecure unprivileged (and that in
6124 * turn means that the A flag wasn't specified), all the bits in the
6125 * register must be zero:
6126 * IREGION: 0 because IRVALID is 0
6127 * IRVALID: 0 because NS
6128 * S: 0 because NS
6129 * NSRW: 0 because NS
6130 * NSR: 0 because NS
6131 * RW: 0 because unpriv and A flag not set
6132 * R: 0 because unpriv and A flag not set
6133 * SRVALID: 0 because NS
6134 * MRVALID: 0 because unpriv and A flag not set
6135 * SREGION: 0 becaus SRVALID is 0
6136 * MREGION: 0 because MRVALID is 0
6137 */
6138 return 0;
6139}
6140
0ecb72a5 6141void switch_mode(CPUARMState *env, int mode)
b5ff1b31 6142{
a47dddd7
AF
6143 ARMCPU *cpu = arm_env_get_cpu(env);
6144
6145 if (mode != ARM_CPU_MODE_USR) {
6146 cpu_abort(CPU(cpu), "Tried to switch out of user mode\n");
6147 }
b5ff1b31
FB
6148}
6149
012a906b
GB
6150uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
6151 uint32_t cur_el, bool secure)
9e729b57
EI
6152{
6153 return 1;
6154}
6155
ce02049d
GB
6156void aarch64_sync_64_to_32(CPUARMState *env)
6157{
6158 g_assert_not_reached();
6159}
6160
b5ff1b31
FB
6161#else
6162
0ecb72a5 6163void switch_mode(CPUARMState *env, int mode)
b5ff1b31
FB
6164{
6165 int old_mode;
6166 int i;
6167
6168 old_mode = env->uncached_cpsr & CPSR_M;
6169 if (mode == old_mode)
6170 return;
6171
6172 if (old_mode == ARM_CPU_MODE_FIQ) {
6173 memcpy (env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t));
8637c67f 6174 memcpy (env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t));
b5ff1b31
FB
6175 } else if (mode == ARM_CPU_MODE_FIQ) {
6176 memcpy (env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t));
8637c67f 6177 memcpy (env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t));
b5ff1b31
FB
6178 }
6179
f5206413 6180 i = bank_number(old_mode);
b5ff1b31
FB
6181 env->banked_r13[i] = env->regs[13];
6182 env->banked_r14[i] = env->regs[14];
6183 env->banked_spsr[i] = env->spsr;
6184
f5206413 6185 i = bank_number(mode);
b5ff1b31
FB
6186 env->regs[13] = env->banked_r13[i];
6187 env->regs[14] = env->banked_r14[i];
6188 env->spsr = env->banked_spsr[i];
6189}
6190
0eeb17d6
GB
6191/* Physical Interrupt Target EL Lookup Table
6192 *
6193 * [ From ARM ARM section G1.13.4 (Table G1-15) ]
6194 *
6195 * The below multi-dimensional table is used for looking up the target
6196 * exception level given numerous condition criteria. Specifically, the
6197 * target EL is based on SCR and HCR routing controls as well as the
6198 * currently executing EL and secure state.
6199 *
6200 * Dimensions:
6201 * target_el_table[2][2][2][2][2][4]
6202 * | | | | | +--- Current EL
6203 * | | | | +------ Non-secure(0)/Secure(1)
6204 * | | | +--------- HCR mask override
6205 * | | +------------ SCR exec state control
6206 * | +--------------- SCR mask override
6207 * +------------------ 32-bit(0)/64-bit(1) EL3
6208 *
6209 * The table values are as such:
6210 * 0-3 = EL0-EL3
6211 * -1 = Cannot occur
6212 *
6213 * The ARM ARM target EL table includes entries indicating that an "exception
6214 * is not taken". The two cases where this is applicable are:
6215 * 1) An exception is taken from EL3 but the SCR does not have the exception
6216 * routed to EL3.
6217 * 2) An exception is taken from EL2 but the HCR does not have the exception
6218 * routed to EL2.
6219 * In these two cases, the below table contain a target of EL1. This value is
6220 * returned as it is expected that the consumer of the table data will check
6221 * for "target EL >= current EL" to ensure the exception is not taken.
6222 *
6223 * SCR HCR
6224 * 64 EA AMO From
6225 * BIT IRQ IMO Non-secure Secure
6226 * EL3 FIQ RW FMO EL0 EL1 EL2 EL3 EL0 EL1 EL2 EL3
6227 */
82c39f6a 6228static const int8_t target_el_table[2][2][2][2][2][4] = {
0eeb17d6
GB
6229 {{{{/* 0 0 0 0 */{ 1, 1, 2, -1 },{ 3, -1, -1, 3 },},
6230 {/* 0 0 0 1 */{ 2, 2, 2, -1 },{ 3, -1, -1, 3 },},},
6231 {{/* 0 0 1 0 */{ 1, 1, 2, -1 },{ 3, -1, -1, 3 },},
6232 {/* 0 0 1 1 */{ 2, 2, 2, -1 },{ 3, -1, -1, 3 },},},},
6233 {{{/* 0 1 0 0 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},
6234 {/* 0 1 0 1 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},},
6235 {{/* 0 1 1 0 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},
6236 {/* 0 1 1 1 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},},},},
6237 {{{{/* 1 0 0 0 */{ 1, 1, 2, -1 },{ 1, 1, -1, 1 },},
6238 {/* 1 0 0 1 */{ 2, 2, 2, -1 },{ 1, 1, -1, 1 },},},
6239 {{/* 1 0 1 0 */{ 1, 1, 1, -1 },{ 1, 1, -1, 1 },},
6240 {/* 1 0 1 1 */{ 2, 2, 2, -1 },{ 1, 1, -1, 1 },},},},
6241 {{{/* 1 1 0 0 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},
6242 {/* 1 1 0 1 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},},
6243 {{/* 1 1 1 0 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},
6244 {/* 1 1 1 1 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},},},},
6245};
6246
6247/*
6248 * Determine the target EL for physical exceptions
6249 */
012a906b
GB
6250uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
6251 uint32_t cur_el, bool secure)
0eeb17d6
GB
6252{
6253 CPUARMState *env = cs->env_ptr;
2cde031f 6254 int rw;
0eeb17d6
GB
6255 int scr;
6256 int hcr;
6257 int target_el;
2cde031f
SS
6258 /* Is the highest EL AArch64? */
6259 int is64 = arm_feature(env, ARM_FEATURE_AARCH64);
6260
6261 if (arm_feature(env, ARM_FEATURE_EL3)) {
6262 rw = ((env->cp15.scr_el3 & SCR_RW) == SCR_RW);
6263 } else {
6264 /* Either EL2 is the highest EL (and so the EL2 register width
6265 * is given by is64); or there is no EL2 or EL3, in which case
6266 * the value of 'rw' does not affect the table lookup anyway.
6267 */
6268 rw = is64;
6269 }
0eeb17d6
GB
6270
6271 switch (excp_idx) {
6272 case EXCP_IRQ:
6273 scr = ((env->cp15.scr_el3 & SCR_IRQ) == SCR_IRQ);
6274 hcr = ((env->cp15.hcr_el2 & HCR_IMO) == HCR_IMO);
6275 break;
6276 case EXCP_FIQ:
6277 scr = ((env->cp15.scr_el3 & SCR_FIQ) == SCR_FIQ);
6278 hcr = ((env->cp15.hcr_el2 & HCR_FMO) == HCR_FMO);
6279 break;
6280 default:
6281 scr = ((env->cp15.scr_el3 & SCR_EA) == SCR_EA);
6282 hcr = ((env->cp15.hcr_el2 & HCR_AMO) == HCR_AMO);
6283 break;
6284 };
6285
6286 /* If HCR.TGE is set then HCR is treated as being 1 */
6287 hcr |= ((env->cp15.hcr_el2 & HCR_TGE) == HCR_TGE);
6288
6289 /* Perform a table-lookup for the target EL given the current state */
6290 target_el = target_el_table[is64][scr][rw][hcr][secure][cur_el];
6291
6292 assert(target_el > 0);
6293
6294 return target_el;
6295}
6296
fd592d89
PM
6297static bool v7m_stack_write(ARMCPU *cpu, uint32_t addr, uint32_t value,
6298 ARMMMUIdx mmu_idx, bool ignfault)
9ee6e8bb 6299{
fd592d89
PM
6300 CPUState *cs = CPU(cpu);
6301 CPUARMState *env = &cpu->env;
6302 MemTxAttrs attrs = {};
6303 MemTxResult txres;
6304 target_ulong page_size;
6305 hwaddr physaddr;
6306 int prot;
6307 ARMMMUFaultInfo fi;
6308 bool secure = mmu_idx & ARM_MMU_IDX_M_S;
6309 int exc;
6310 bool exc_secure;
6311
6312 if (get_phys_addr(env, addr, MMU_DATA_STORE, mmu_idx, &physaddr,
6313 &attrs, &prot, &page_size, &fi, NULL)) {
6314 /* MPU/SAU lookup failed */
6315 if (fi.type == ARMFault_QEMU_SFault) {
6316 qemu_log_mask(CPU_LOG_INT,
6317 "...SecureFault with SFSR.AUVIOL during stacking\n");
6318 env->v7m.sfsr |= R_V7M_SFSR_AUVIOL_MASK | R_V7M_SFSR_SFARVALID_MASK;
6319 env->v7m.sfar = addr;
6320 exc = ARMV7M_EXCP_SECURE;
6321 exc_secure = false;
6322 } else {
6323 qemu_log_mask(CPU_LOG_INT, "...MemManageFault with CFSR.MSTKERR\n");
6324 env->v7m.cfsr[secure] |= R_V7M_CFSR_MSTKERR_MASK;
6325 exc = ARMV7M_EXCP_MEM;
6326 exc_secure = secure;
6327 }
6328 goto pend_fault;
6329 }
6330 address_space_stl_le(arm_addressspace(cs, attrs), physaddr, value,
6331 attrs, &txres);
6332 if (txres != MEMTX_OK) {
6333 /* BusFault trying to write the data */
6334 qemu_log_mask(CPU_LOG_INT, "...BusFault with BFSR.STKERR\n");
6335 env->v7m.cfsr[M_REG_NS] |= R_V7M_CFSR_STKERR_MASK;
6336 exc = ARMV7M_EXCP_BUS;
6337 exc_secure = false;
6338 goto pend_fault;
6339 }
6340 return true;
70d74660 6341
fd592d89
PM
6342pend_fault:
6343 /* By pending the exception at this point we are making
6344 * the IMPDEF choice "overridden exceptions pended" (see the
6345 * MergeExcInfo() pseudocode). The other choice would be to not
6346 * pend them now and then make a choice about which to throw away
6347 * later if we have two derived exceptions.
6348 * The only case when we must not pend the exception but instead
6349 * throw it away is if we are doing the push of the callee registers
6350 * and we've already generated a derived exception. Even in this
6351 * case we will still update the fault status registers.
6352 */
6353 if (!ignfault) {
6354 armv7m_nvic_set_pending_derived(env->nvic, exc, exc_secure);
6355 }
6356 return false;
9ee6e8bb
PB
6357}
6358
95695eff
PM
6359static bool v7m_stack_read(ARMCPU *cpu, uint32_t *dest, uint32_t addr,
6360 ARMMMUIdx mmu_idx)
6361{
6362 CPUState *cs = CPU(cpu);
6363 CPUARMState *env = &cpu->env;
6364 MemTxAttrs attrs = {};
6365 MemTxResult txres;
6366 target_ulong page_size;
6367 hwaddr physaddr;
6368 int prot;
6369 ARMMMUFaultInfo fi;
6370 bool secure = mmu_idx & ARM_MMU_IDX_M_S;
6371 int exc;
6372 bool exc_secure;
6373 uint32_t value;
6374
6375 if (get_phys_addr(env, addr, MMU_DATA_LOAD, mmu_idx, &physaddr,
6376 &attrs, &prot, &page_size, &fi, NULL)) {
6377 /* MPU/SAU lookup failed */
6378 if (fi.type == ARMFault_QEMU_SFault) {
6379 qemu_log_mask(CPU_LOG_INT,
6380 "...SecureFault with SFSR.AUVIOL during unstack\n");
6381 env->v7m.sfsr |= R_V7M_SFSR_AUVIOL_MASK | R_V7M_SFSR_SFARVALID_MASK;
6382 env->v7m.sfar = addr;
6383 exc = ARMV7M_EXCP_SECURE;
6384 exc_secure = false;
6385 } else {
6386 qemu_log_mask(CPU_LOG_INT,
6387 "...MemManageFault with CFSR.MUNSTKERR\n");
6388 env->v7m.cfsr[secure] |= R_V7M_CFSR_MUNSTKERR_MASK;
6389 exc = ARMV7M_EXCP_MEM;
6390 exc_secure = secure;
6391 }
6392 goto pend_fault;
6393 }
6394
6395 value = address_space_ldl(arm_addressspace(cs, attrs), physaddr,
6396 attrs, &txres);
6397 if (txres != MEMTX_OK) {
6398 /* BusFault trying to read the data */
6399 qemu_log_mask(CPU_LOG_INT, "...BusFault with BFSR.UNSTKERR\n");
6400 env->v7m.cfsr[M_REG_NS] |= R_V7M_CFSR_UNSTKERR_MASK;
6401 exc = ARMV7M_EXCP_BUS;
6402 exc_secure = false;
6403 goto pend_fault;
6404 }
6405
6406 *dest = value;
6407 return true;
6408
6409pend_fault:
6410 /* By pending the exception at this point we are making
6411 * the IMPDEF choice "overridden exceptions pended" (see the
6412 * MergeExcInfo() pseudocode). The other choice would be to not
6413 * pend them now and then make a choice about which to throw away
6414 * later if we have two derived exceptions.
6415 */
6416 armv7m_nvic_set_pending(env->nvic, exc, exc_secure);
6417 return false;
6418}
6419
fb602cb7
PM
6420/* Return true if we're using the process stack pointer (not the MSP) */
6421static bool v7m_using_psp(CPUARMState *env)
6422{
6423 /* Handler mode always uses the main stack; for thread mode
6424 * the CONTROL.SPSEL bit determines the answer.
6425 * Note that in v7M it is not possible to be in Handler mode with
6426 * CONTROL.SPSEL non-zero, but in v8M it is, so we must check both.
6427 */
6428 return !arm_v7m_is_handler_mode(env) &&
6429 env->v7m.control[env->v7m.secure] & R_V7M_CONTROL_SPSEL_MASK;
6430}
6431
3f0cddee
PM
6432/* Write to v7M CONTROL.SPSEL bit for the specified security bank.
6433 * This may change the current stack pointer between Main and Process
6434 * stack pointers if it is done for the CONTROL register for the current
6435 * security state.
de2db7ec 6436 */
3f0cddee
PM
6437static void write_v7m_control_spsel_for_secstate(CPUARMState *env,
6438 bool new_spsel,
6439 bool secstate)
9ee6e8bb 6440{
3f0cddee 6441 bool old_is_psp = v7m_using_psp(env);
de2db7ec 6442
3f0cddee
PM
6443 env->v7m.control[secstate] =
6444 deposit32(env->v7m.control[secstate],
de2db7ec
PM
6445 R_V7M_CONTROL_SPSEL_SHIFT,
6446 R_V7M_CONTROL_SPSEL_LENGTH, new_spsel);
6447
3f0cddee
PM
6448 if (secstate == env->v7m.secure) {
6449 bool new_is_psp = v7m_using_psp(env);
6450 uint32_t tmp;
abc24d86 6451
3f0cddee
PM
6452 if (old_is_psp != new_is_psp) {
6453 tmp = env->v7m.other_sp;
6454 env->v7m.other_sp = env->regs[13];
6455 env->regs[13] = tmp;
6456 }
de2db7ec
PM
6457 }
6458}
6459
3f0cddee
PM
6460/* Write to v7M CONTROL.SPSEL bit. This may change the current
6461 * stack pointer between Main and Process stack pointers.
6462 */
6463static void write_v7m_control_spsel(CPUARMState *env, bool new_spsel)
6464{
6465 write_v7m_control_spsel_for_secstate(env, new_spsel, env->v7m.secure);
6466}
6467
de2db7ec
PM
6468void write_v7m_exception(CPUARMState *env, uint32_t new_exc)
6469{
6470 /* Write a new value to v7m.exception, thus transitioning into or out
6471 * of Handler mode; this may result in a change of active stack pointer.
6472 */
6473 bool new_is_psp, old_is_psp = v7m_using_psp(env);
6474 uint32_t tmp;
abc24d86 6475
de2db7ec
PM
6476 env->v7m.exception = new_exc;
6477
6478 new_is_psp = v7m_using_psp(env);
6479
6480 if (old_is_psp != new_is_psp) {
6481 tmp = env->v7m.other_sp;
6482 env->v7m.other_sp = env->regs[13];
6483 env->regs[13] = tmp;
9ee6e8bb
PB
6484 }
6485}
6486
fb602cb7
PM
6487/* Switch M profile security state between NS and S */
6488static void switch_v7m_security_state(CPUARMState *env, bool new_secstate)
6489{
6490 uint32_t new_ss_msp, new_ss_psp;
6491
6492 if (env->v7m.secure == new_secstate) {
6493 return;
6494 }
6495
6496 /* All the banked state is accessed by looking at env->v7m.secure
6497 * except for the stack pointer; rearrange the SP appropriately.
6498 */
6499 new_ss_msp = env->v7m.other_ss_msp;
6500 new_ss_psp = env->v7m.other_ss_psp;
6501
6502 if (v7m_using_psp(env)) {
6503 env->v7m.other_ss_psp = env->regs[13];
6504 env->v7m.other_ss_msp = env->v7m.other_sp;
6505 } else {
6506 env->v7m.other_ss_msp = env->regs[13];
6507 env->v7m.other_ss_psp = env->v7m.other_sp;
6508 }
6509
6510 env->v7m.secure = new_secstate;
6511
6512 if (v7m_using_psp(env)) {
6513 env->regs[13] = new_ss_psp;
6514 env->v7m.other_sp = new_ss_msp;
6515 } else {
6516 env->regs[13] = new_ss_msp;
6517 env->v7m.other_sp = new_ss_psp;
6518 }
6519}
6520
6521void HELPER(v7m_bxns)(CPUARMState *env, uint32_t dest)
6522{
6523 /* Handle v7M BXNS:
6524 * - if the return value is a magic value, do exception return (like BX)
6525 * - otherwise bit 0 of the return value is the target security state
6526 */
d02a8698
PM
6527 uint32_t min_magic;
6528
6529 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
6530 /* Covers FNC_RETURN and EXC_RETURN magic */
6531 min_magic = FNC_RETURN_MIN_MAGIC;
6532 } else {
6533 /* EXC_RETURN magic only */
6534 min_magic = EXC_RETURN_MIN_MAGIC;
6535 }
6536
6537 if (dest >= min_magic) {
fb602cb7
PM
6538 /* This is an exception return magic value; put it where
6539 * do_v7m_exception_exit() expects and raise EXCEPTION_EXIT.
6540 * Note that if we ever add gen_ss_advance() singlestep support to
6541 * M profile this should count as an "instruction execution complete"
6542 * event (compare gen_bx_excret_final_code()).
6543 */
6544 env->regs[15] = dest & ~1;
6545 env->thumb = dest & 1;
6546 HELPER(exception_internal)(env, EXCP_EXCEPTION_EXIT);
6547 /* notreached */
6548 }
6549
6550 /* translate.c should have made BXNS UNDEF unless we're secure */
6551 assert(env->v7m.secure);
6552
6553 switch_v7m_security_state(env, dest & 1);
6554 env->thumb = 1;
6555 env->regs[15] = dest & ~1;
6556}
6557
3e3fa230
PM
6558void HELPER(v7m_blxns)(CPUARMState *env, uint32_t dest)
6559{
6560 /* Handle v7M BLXNS:
6561 * - bit 0 of the destination address is the target security state
6562 */
6563
6564 /* At this point regs[15] is the address just after the BLXNS */
6565 uint32_t nextinst = env->regs[15] | 1;
6566 uint32_t sp = env->regs[13] - 8;
6567 uint32_t saved_psr;
6568
6569 /* translate.c will have made BLXNS UNDEF unless we're secure */
6570 assert(env->v7m.secure);
6571
6572 if (dest & 1) {
6573 /* target is Secure, so this is just a normal BLX,
6574 * except that the low bit doesn't indicate Thumb/not.
6575 */
6576 env->regs[14] = nextinst;
6577 env->thumb = 1;
6578 env->regs[15] = dest & ~1;
6579 return;
6580 }
6581
6582 /* Target is non-secure: first push a stack frame */
6583 if (!QEMU_IS_ALIGNED(sp, 8)) {
6584 qemu_log_mask(LOG_GUEST_ERROR,
6585 "BLXNS with misaligned SP is UNPREDICTABLE\n");
6586 }
6587
6588 saved_psr = env->v7m.exception;
6589 if (env->v7m.control[M_REG_S] & R_V7M_CONTROL_SFPA_MASK) {
6590 saved_psr |= XPSR_SFPA;
6591 }
6592
6593 /* Note that these stores can throw exceptions on MPU faults */
6594 cpu_stl_data(env, sp, nextinst);
6595 cpu_stl_data(env, sp + 4, saved_psr);
6596
6597 env->regs[13] = sp;
6598 env->regs[14] = 0xfeffffff;
6599 if (arm_v7m_is_handler_mode(env)) {
6600 /* Write a dummy value to IPSR, to avoid leaking the current secure
6601 * exception number to non-secure code. This is guaranteed not
6602 * to cause write_v7m_exception() to actually change stacks.
6603 */
6604 write_v7m_exception(env, 1);
6605 }
6606 switch_v7m_security_state(env, 0);
6607 env->thumb = 1;
6608 env->regs[15] = dest;
6609}
6610
5b522399
PM
6611static uint32_t *get_v7m_sp_ptr(CPUARMState *env, bool secure, bool threadmode,
6612 bool spsel)
6613{
6614 /* Return a pointer to the location where we currently store the
6615 * stack pointer for the requested security state and thread mode.
6616 * This pointer will become invalid if the CPU state is updated
6617 * such that the stack pointers are switched around (eg changing
6618 * the SPSEL control bit).
6619 * Compare the v8M ARM ARM pseudocode LookUpSP_with_security_mode().
6620 * Unlike that pseudocode, we require the caller to pass us in the
6621 * SPSEL control bit value; this is because we also use this
6622 * function in handling of pushing of the callee-saves registers
6623 * part of the v8M stack frame (pseudocode PushCalleeStack()),
6624 * and in the tailchain codepath the SPSEL bit comes from the exception
6625 * return magic LR value from the previous exception. The pseudocode
6626 * opencodes the stack-selection in PushCalleeStack(), but we prefer
6627 * to make this utility function generic enough to do the job.
6628 */
6629 bool want_psp = threadmode && spsel;
6630
6631 if (secure == env->v7m.secure) {
de2db7ec
PM
6632 if (want_psp == v7m_using_psp(env)) {
6633 return &env->regs[13];
6634 } else {
6635 return &env->v7m.other_sp;
6636 }
5b522399
PM
6637 } else {
6638 if (want_psp) {
6639 return &env->v7m.other_ss_psp;
6640 } else {
6641 return &env->v7m.other_ss_msp;
6642 }
6643 }
6644}
6645
600c33f2
PM
6646static bool arm_v7m_load_vector(ARMCPU *cpu, int exc, bool targets_secure,
6647 uint32_t *pvec)
39ae2474
PM
6648{
6649 CPUState *cs = CPU(cpu);
6650 CPUARMState *env = &cpu->env;
6651 MemTxResult result;
600c33f2
PM
6652 uint32_t addr = env->v7m.vecbase[targets_secure] + exc * 4;
6653 uint32_t vector_entry;
6654 MemTxAttrs attrs = {};
6655 ARMMMUIdx mmu_idx;
6656 bool exc_secure;
6657
6658 mmu_idx = arm_v7m_mmu_idx_for_secstate_and_priv(env, targets_secure, true);
39ae2474 6659
600c33f2
PM
6660 /* We don't do a get_phys_addr() here because the rules for vector
6661 * loads are special: they always use the default memory map, and
6662 * the default memory map permits reads from all addresses.
6663 * Since there's no easy way to pass through to pmsav8_mpu_lookup()
6664 * that we want this special case which would always say "yes",
6665 * we just do the SAU lookup here followed by a direct physical load.
6666 */
6667 attrs.secure = targets_secure;
6668 attrs.user = false;
6669
6670 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
6671 V8M_SAttributes sattrs = {};
6672
6673 v8m_security_lookup(env, addr, MMU_DATA_LOAD, mmu_idx, &sattrs);
6674 if (sattrs.ns) {
6675 attrs.secure = false;
6676 } else if (!targets_secure) {
6677 /* NS access to S memory */
6678 goto load_fail;
6679 }
6680 }
6681
6682 vector_entry = address_space_ldl(arm_addressspace(cs, attrs), addr,
6683 attrs, &result);
39ae2474 6684 if (result != MEMTX_OK) {
600c33f2 6685 goto load_fail;
39ae2474 6686 }
600c33f2
PM
6687 *pvec = vector_entry;
6688 return true;
6689
6690load_fail:
6691 /* All vector table fetch fails are reported as HardFault, with
6692 * HFSR.VECTTBL and .FORCED set. (FORCED is set because
6693 * technically the underlying exception is a MemManage or BusFault
6694 * that is escalated to HardFault.) This is a terminal exception,
6695 * so we will either take the HardFault immediately or else enter
6696 * lockup (the latter case is handled in armv7m_nvic_set_pending_derived()).
6697 */
6698 exc_secure = targets_secure ||
6699 !(cpu->env.v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK);
6700 env->v7m.hfsr |= R_V7M_HFSR_VECTTBL_MASK | R_V7M_HFSR_FORCED_MASK;
6701 armv7m_nvic_set_pending_derived(env->nvic, ARMV7M_EXCP_HARD, exc_secure);
6702 return false;
39ae2474
PM
6703}
6704
65b4234f 6705static bool v7m_push_callee_stack(ARMCPU *cpu, uint32_t lr, bool dotailchain,
0094ca70 6706 bool ignore_faults)
d3392718
PM
6707{
6708 /* For v8M, push the callee-saves register part of the stack frame.
6709 * Compare the v8M pseudocode PushCalleeStack().
6710 * In the tailchaining case this may not be the current stack.
6711 */
6712 CPUARMState *env = &cpu->env;
d3392718
PM
6713 uint32_t *frame_sp_p;
6714 uint32_t frameptr;
65b4234f
PM
6715 ARMMMUIdx mmu_idx;
6716 bool stacked_ok;
d3392718
PM
6717
6718 if (dotailchain) {
65b4234f
PM
6719 bool mode = lr & R_V7M_EXCRET_MODE_MASK;
6720 bool priv = !(env->v7m.control[M_REG_S] & R_V7M_CONTROL_NPRIV_MASK) ||
6721 !mode;
6722
6723 mmu_idx = arm_v7m_mmu_idx_for_secstate_and_priv(env, M_REG_S, priv);
6724 frame_sp_p = get_v7m_sp_ptr(env, M_REG_S, mode,
d3392718
PM
6725 lr & R_V7M_EXCRET_SPSEL_MASK);
6726 } else {
65b4234f 6727 mmu_idx = core_to_arm_mmu_idx(env, cpu_mmu_index(env, false));
d3392718
PM
6728 frame_sp_p = &env->regs[13];
6729 }
6730
6731 frameptr = *frame_sp_p - 0x28;
6732
65b4234f
PM
6733 /* Write as much of the stack frame as we can. A write failure may
6734 * cause us to pend a derived exception.
6735 */
6736 stacked_ok =
6737 v7m_stack_write(cpu, frameptr, 0xfefa125b, mmu_idx, ignore_faults) &&
6738 v7m_stack_write(cpu, frameptr + 0x8, env->regs[4], mmu_idx,
6739 ignore_faults) &&
6740 v7m_stack_write(cpu, frameptr + 0xc, env->regs[5], mmu_idx,
6741 ignore_faults) &&
6742 v7m_stack_write(cpu, frameptr + 0x10, env->regs[6], mmu_idx,
6743 ignore_faults) &&
6744 v7m_stack_write(cpu, frameptr + 0x14, env->regs[7], mmu_idx,
6745 ignore_faults) &&
6746 v7m_stack_write(cpu, frameptr + 0x18, env->regs[8], mmu_idx,
6747 ignore_faults) &&
6748 v7m_stack_write(cpu, frameptr + 0x1c, env->regs[9], mmu_idx,
6749 ignore_faults) &&
6750 v7m_stack_write(cpu, frameptr + 0x20, env->regs[10], mmu_idx,
6751 ignore_faults) &&
6752 v7m_stack_write(cpu, frameptr + 0x24, env->regs[11], mmu_idx,
6753 ignore_faults);
6754
6755 /* Update SP regardless of whether any of the stack accesses failed.
6756 * When we implement v8M stack limit checking then this attempt to
6757 * update SP might also fail and result in a derived exception.
6758 */
d3392718 6759 *frame_sp_p = frameptr;
65b4234f
PM
6760
6761 return !stacked_ok;
d3392718
PM
6762}
6763
0094ca70
PM
6764static void v7m_exception_taken(ARMCPU *cpu, uint32_t lr, bool dotailchain,
6765 bool ignore_stackfaults)
39ae2474
PM
6766{
6767 /* Do the "take the exception" parts of exception entry,
6768 * but not the pushing of state to the stack. This is
6769 * similar to the pseudocode ExceptionTaken() function.
6770 */
6771 CPUARMState *env = &cpu->env;
6772 uint32_t addr;
d3392718 6773 bool targets_secure;
6c948518 6774 int exc;
65b4234f 6775 bool push_failed = false;
d3392718 6776
6c948518 6777 armv7m_nvic_get_pending_irq_info(env->nvic, &exc, &targets_secure);
d3392718
PM
6778
6779 if (arm_feature(env, ARM_FEATURE_V8)) {
6780 if (arm_feature(env, ARM_FEATURE_M_SECURITY) &&
6781 (lr & R_V7M_EXCRET_S_MASK)) {
6782 /* The background code (the owner of the registers in the
6783 * exception frame) is Secure. This means it may either already
6784 * have or now needs to push callee-saves registers.
6785 */
6786 if (targets_secure) {
6787 if (dotailchain && !(lr & R_V7M_EXCRET_ES_MASK)) {
6788 /* We took an exception from Secure to NonSecure
6789 * (which means the callee-saved registers got stacked)
6790 * and are now tailchaining to a Secure exception.
6791 * Clear DCRS so eventual return from this Secure
6792 * exception unstacks the callee-saved registers.
6793 */
6794 lr &= ~R_V7M_EXCRET_DCRS_MASK;
6795 }
6796 } else {
6797 /* We're going to a non-secure exception; push the
6798 * callee-saves registers to the stack now, if they're
6799 * not already saved.
6800 */
6801 if (lr & R_V7M_EXCRET_DCRS_MASK &&
6802 !(dotailchain && (lr & R_V7M_EXCRET_ES_MASK))) {
65b4234f
PM
6803 push_failed = v7m_push_callee_stack(cpu, lr, dotailchain,
6804 ignore_stackfaults);
d3392718
PM
6805 }
6806 lr |= R_V7M_EXCRET_DCRS_MASK;
6807 }
6808 }
6809
6810 lr &= ~R_V7M_EXCRET_ES_MASK;
6811 if (targets_secure || !arm_feature(env, ARM_FEATURE_M_SECURITY)) {
6812 lr |= R_V7M_EXCRET_ES_MASK;
6813 }
6814 lr &= ~R_V7M_EXCRET_SPSEL_MASK;
6815 if (env->v7m.control[targets_secure] & R_V7M_CONTROL_SPSEL_MASK) {
6816 lr |= R_V7M_EXCRET_SPSEL_MASK;
6817 }
6818
6819 /* Clear registers if necessary to prevent non-secure exception
6820 * code being able to see register values from secure code.
6821 * Where register values become architecturally UNKNOWN we leave
6822 * them with their previous values.
6823 */
6824 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
6825 if (!targets_secure) {
6826 /* Always clear the caller-saved registers (they have been
6827 * pushed to the stack earlier in v7m_push_stack()).
6828 * Clear callee-saved registers if the background code is
6829 * Secure (in which case these regs were saved in
6830 * v7m_push_callee_stack()).
6831 */
6832 int i;
6833
6834 for (i = 0; i < 13; i++) {
6835 /* r4..r11 are callee-saves, zero only if EXCRET.S == 1 */
6836 if (i < 4 || i > 11 || (lr & R_V7M_EXCRET_S_MASK)) {
6837 env->regs[i] = 0;
6838 }
6839 }
6840 /* Clear EAPSR */
6841 xpsr_write(env, 0, XPSR_NZCV | XPSR_Q | XPSR_GE | XPSR_IT);
6842 }
6843 }
6844 }
39ae2474 6845
65b4234f
PM
6846 if (push_failed && !ignore_stackfaults) {
6847 /* Derived exception on callee-saves register stacking:
6848 * we might now want to take a different exception which
6849 * targets a different security state, so try again from the top.
6850 */
6851 v7m_exception_taken(cpu, lr, true, true);
6852 return;
6853 }
6854
600c33f2
PM
6855 if (!arm_v7m_load_vector(cpu, exc, targets_secure, &addr)) {
6856 /* Vector load failed: derived exception */
6857 v7m_exception_taken(cpu, lr, true, true);
6858 return;
6859 }
6c948518
PM
6860
6861 /* Now we've done everything that might cause a derived exception
6862 * we can go ahead and activate whichever exception we're going to
6863 * take (which might now be the derived exception).
6864 */
6865 armv7m_nvic_acknowledge_irq(env->nvic);
6866
d3392718
PM
6867 /* Switch to target security state -- must do this before writing SPSEL */
6868 switch_v7m_security_state(env, targets_secure);
de2db7ec 6869 write_v7m_control_spsel(env, 0);
dc3c4c14 6870 arm_clear_exclusive(env);
39ae2474
PM
6871 /* Clear IT bits */
6872 env->condexec_bits = 0;
6873 env->regs[14] = lr;
39ae2474
PM
6874 env->regs[15] = addr & 0xfffffffe;
6875 env->thumb = addr & 1;
6876}
6877
0094ca70 6878static bool v7m_push_stack(ARMCPU *cpu)
39ae2474
PM
6879{
6880 /* Do the "set up stack frame" part of exception entry,
6881 * similar to pseudocode PushStack().
0094ca70
PM
6882 * Return true if we generate a derived exception (and so
6883 * should ignore further stack faults trying to process
6884 * that derived exception.)
39ae2474 6885 */
fd592d89 6886 bool stacked_ok;
39ae2474
PM
6887 CPUARMState *env = &cpu->env;
6888 uint32_t xpsr = xpsr_read(env);
fd592d89
PM
6889 uint32_t frameptr = env->regs[13];
6890 ARMMMUIdx mmu_idx = core_to_arm_mmu_idx(env, cpu_mmu_index(env, false));
39ae2474
PM
6891
6892 /* Align stack pointer if the guest wants that */
fd592d89 6893 if ((frameptr & 4) &&
9d40cd8a 6894 (env->v7m.ccr[env->v7m.secure] & R_V7M_CCR_STKALIGN_MASK)) {
fd592d89 6895 frameptr -= 4;
987ab45e 6896 xpsr |= XPSR_SPREALIGN;
39ae2474 6897 }
0094ca70 6898
fd592d89
PM
6899 frameptr -= 0x20;
6900
6901 /* Write as much of the stack frame as we can. If we fail a stack
6902 * write this will result in a derived exception being pended
6903 * (which may be taken in preference to the one we started with
6904 * if it has higher priority).
6905 */
6906 stacked_ok =
6907 v7m_stack_write(cpu, frameptr, env->regs[0], mmu_idx, false) &&
6908 v7m_stack_write(cpu, frameptr + 4, env->regs[1], mmu_idx, false) &&
6909 v7m_stack_write(cpu, frameptr + 8, env->regs[2], mmu_idx, false) &&
6910 v7m_stack_write(cpu, frameptr + 12, env->regs[3], mmu_idx, false) &&
6911 v7m_stack_write(cpu, frameptr + 16, env->regs[12], mmu_idx, false) &&
6912 v7m_stack_write(cpu, frameptr + 20, env->regs[14], mmu_idx, false) &&
6913 v7m_stack_write(cpu, frameptr + 24, env->regs[15], mmu_idx, false) &&
6914 v7m_stack_write(cpu, frameptr + 28, xpsr, mmu_idx, false);
6915
6916 /* Update SP regardless of whether any of the stack accesses failed.
6917 * When we implement v8M stack limit checking then this attempt to
6918 * update SP might also fail and result in a derived exception.
6919 */
6920 env->regs[13] = frameptr;
6921
6922 return !stacked_ok;
39ae2474
PM
6923}
6924
aa488fe3 6925static void do_v7m_exception_exit(ARMCPU *cpu)
9ee6e8bb 6926{
aa488fe3 6927 CPUARMState *env = &cpu->env;
5b522399 6928 CPUState *cs = CPU(cpu);
351e527a 6929 uint32_t excret;
9ee6e8bb 6930 uint32_t xpsr;
aa488fe3 6931 bool ufault = false;
bfb2eb52
PM
6932 bool sfault = false;
6933 bool return_to_sp_process;
6934 bool return_to_handler;
aa488fe3 6935 bool rettobase = false;
5cb18069 6936 bool exc_secure = false;
5b522399 6937 bool return_to_secure;
aa488fe3 6938
d02a8698
PM
6939 /* If we're not in Handler mode then jumps to magic exception-exit
6940 * addresses don't have magic behaviour. However for the v8M
6941 * security extensions the magic secure-function-return has to
6942 * work in thread mode too, so to avoid doing an extra check in
6943 * the generated code we allow exception-exit magic to also cause the
6944 * internal exception and bring us here in thread mode. Correct code
6945 * will never try to do this (the following insn fetch will always
6946 * fault) so we the overhead of having taken an unnecessary exception
6947 * doesn't matter.
aa488fe3 6948 */
d02a8698
PM
6949 if (!arm_v7m_is_handler_mode(env)) {
6950 return;
6951 }
aa488fe3
PM
6952
6953 /* In the spec pseudocode ExceptionReturn() is called directly
6954 * from BXWritePC() and gets the full target PC value including
6955 * bit zero. In QEMU's implementation we treat it as a normal
6956 * jump-to-register (which is then caught later on), and so split
6957 * the target value up between env->regs[15] and env->thumb in
6958 * gen_bx(). Reconstitute it.
6959 */
351e527a 6960 excret = env->regs[15];
aa488fe3 6961 if (env->thumb) {
351e527a 6962 excret |= 1;
aa488fe3
PM
6963 }
6964
6965 qemu_log_mask(CPU_LOG_INT, "Exception return: magic PC %" PRIx32
6966 " previous exception %d\n",
351e527a 6967 excret, env->v7m.exception);
aa488fe3 6968
351e527a 6969 if ((excret & R_V7M_EXCRET_RES1_MASK) != R_V7M_EXCRET_RES1_MASK) {
aa488fe3 6970 qemu_log_mask(LOG_GUEST_ERROR, "M profile: zero high bits in exception "
351e527a
PM
6971 "exit PC value 0x%" PRIx32 " are UNPREDICTABLE\n",
6972 excret);
aa488fe3
PM
6973 }
6974
bfb2eb52
PM
6975 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
6976 /* EXC_RETURN.ES validation check (R_SMFL). We must do this before
6977 * we pick which FAULTMASK to clear.
6978 */
6979 if (!env->v7m.secure &&
6980 ((excret & R_V7M_EXCRET_ES_MASK) ||
6981 !(excret & R_V7M_EXCRET_DCRS_MASK))) {
6982 sfault = 1;
6983 /* For all other purposes, treat ES as 0 (R_HXSR) */
6984 excret &= ~R_V7M_EXCRET_ES_MASK;
6985 }
6986 }
6987
a20ee600 6988 if (env->v7m.exception != ARMV7M_EXCP_NMI) {
42a6686b
PM
6989 /* Auto-clear FAULTMASK on return from other than NMI.
6990 * If the security extension is implemented then this only
6991 * happens if the raw execution priority is >= 0; the
6992 * value of the ES bit in the exception return value indicates
6993 * which security state's faultmask to clear. (v8M ARM ARM R_KBNF.)
6994 */
6995 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
5cb18069 6996 exc_secure = excret & R_V7M_EXCRET_ES_MASK;
42a6686b 6997 if (armv7m_nvic_raw_execution_priority(env->nvic) >= 0) {
5cb18069 6998 env->v7m.faultmask[exc_secure] = 0;
42a6686b
PM
6999 }
7000 } else {
7001 env->v7m.faultmask[M_REG_NS] = 0;
7002 }
a20ee600 7003 }
aa488fe3 7004
5cb18069
PM
7005 switch (armv7m_nvic_complete_irq(env->nvic, env->v7m.exception,
7006 exc_secure)) {
aa488fe3
PM
7007 case -1:
7008 /* attempt to exit an exception that isn't active */
7009 ufault = true;
7010 break;
7011 case 0:
7012 /* still an irq active now */
7013 break;
7014 case 1:
7015 /* we returned to base exception level, no nesting.
7016 * (In the pseudocode this is written using "NestedActivation != 1"
7017 * where we have 'rettobase == false'.)
7018 */
7019 rettobase = true;
7020 break;
7021 default:
7022 g_assert_not_reached();
7023 }
7024
bfb2eb52
PM
7025 return_to_handler = !(excret & R_V7M_EXCRET_MODE_MASK);
7026 return_to_sp_process = excret & R_V7M_EXCRET_SPSEL_MASK;
5b522399
PM
7027 return_to_secure = arm_feature(env, ARM_FEATURE_M_SECURITY) &&
7028 (excret & R_V7M_EXCRET_S_MASK);
7029
bfb2eb52
PM
7030 if (arm_feature(env, ARM_FEATURE_V8)) {
7031 if (!arm_feature(env, ARM_FEATURE_M_SECURITY)) {
7032 /* UNPREDICTABLE if S == 1 or DCRS == 0 or ES == 1 (R_XLCP);
7033 * we choose to take the UsageFault.
7034 */
7035 if ((excret & R_V7M_EXCRET_S_MASK) ||
7036 (excret & R_V7M_EXCRET_ES_MASK) ||
7037 !(excret & R_V7M_EXCRET_DCRS_MASK)) {
7038 ufault = true;
7039 }
7040 }
7041 if (excret & R_V7M_EXCRET_RES0_MASK) {
aa488fe3
PM
7042 ufault = true;
7043 }
bfb2eb52
PM
7044 } else {
7045 /* For v7M we only recognize certain combinations of the low bits */
7046 switch (excret & 0xf) {
7047 case 1: /* Return to Handler */
7048 break;
7049 case 13: /* Return to Thread using Process stack */
7050 case 9: /* Return to Thread using Main stack */
7051 /* We only need to check NONBASETHRDENA for v7M, because in
7052 * v8M this bit does not exist (it is RES1).
7053 */
7054 if (!rettobase &&
7055 !(env->v7m.ccr[env->v7m.secure] &
7056 R_V7M_CCR_NONBASETHRDENA_MASK)) {
7057 ufault = true;
7058 }
7059 break;
7060 default:
7061 ufault = true;
7062 }
7063 }
7064
7065 if (sfault) {
7066 env->v7m.sfsr |= R_V7M_SFSR_INVER_MASK;
7067 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SECURE, false);
0094ca70 7068 v7m_exception_taken(cpu, excret, true, false);
bfb2eb52
PM
7069 qemu_log_mask(CPU_LOG_INT, "...taking SecureFault on existing "
7070 "stackframe: failed EXC_RETURN.ES validity check\n");
7071 return;
aa488fe3
PM
7072 }
7073
7074 if (ufault) {
7075 /* Bad exception return: instead of popping the exception
7076 * stack, directly take a usage fault on the current stack.
7077 */
334e8dad 7078 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_INVPC_MASK;
2fb50a33 7079 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, env->v7m.secure);
0094ca70 7080 v7m_exception_taken(cpu, excret, true, false);
aa488fe3
PM
7081 qemu_log_mask(CPU_LOG_INT, "...taking UsageFault on existing "
7082 "stackframe: failed exception return integrity check\n");
7083 return;
a20ee600 7084 }
9ee6e8bb 7085
de2db7ec
PM
7086 /* Set CONTROL.SPSEL from excret.SPSEL. Since we're still in
7087 * Handler mode (and will be until we write the new XPSR.Interrupt
7088 * field) this does not switch around the current stack pointer.
5b522399 7089 */
3f0cddee 7090 write_v7m_control_spsel_for_secstate(env, return_to_sp_process, exc_secure);
5b522399 7091
3919e60b
PM
7092 switch_v7m_security_state(env, return_to_secure);
7093
5b522399
PM
7094 {
7095 /* The stack pointer we should be reading the exception frame from
7096 * depends on bits in the magic exception return type value (and
7097 * for v8M isn't necessarily the stack pointer we will eventually
7098 * end up resuming execution with). Get a pointer to the location
7099 * in the CPU state struct where the SP we need is currently being
7100 * stored; we will use and modify it in place.
7101 * We use this limited C variable scope so we don't accidentally
7102 * use 'frame_sp_p' after we do something that makes it invalid.
fcf83ab1 7103 */
5b522399
PM
7104 uint32_t *frame_sp_p = get_v7m_sp_ptr(env,
7105 return_to_secure,
7106 !return_to_handler,
7107 return_to_sp_process);
7108 uint32_t frameptr = *frame_sp_p;
95695eff
PM
7109 bool pop_ok = true;
7110 ARMMMUIdx mmu_idx;
7111
7112 mmu_idx = arm_v7m_mmu_idx_for_secstate_and_priv(env, return_to_secure,
7113 !return_to_handler);
5b522399 7114
cb484f9a
PM
7115 if (!QEMU_IS_ALIGNED(frameptr, 8) &&
7116 arm_feature(env, ARM_FEATURE_V8)) {
7117 qemu_log_mask(LOG_GUEST_ERROR,
7118 "M profile exception return with non-8-aligned SP "
7119 "for destination state is UNPREDICTABLE\n");
7120 }
7121
907bedb3
PM
7122 /* Do we need to pop callee-saved registers? */
7123 if (return_to_secure &&
7124 ((excret & R_V7M_EXCRET_ES_MASK) == 0 ||
7125 (excret & R_V7M_EXCRET_DCRS_MASK) == 0)) {
7126 uint32_t expected_sig = 0xfefa125b;
7127 uint32_t actual_sig = ldl_phys(cs->as, frameptr);
7128
7129 if (expected_sig != actual_sig) {
7130 /* Take a SecureFault on the current stack */
7131 env->v7m.sfsr |= R_V7M_SFSR_INVIS_MASK;
7132 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SECURE, false);
0094ca70 7133 v7m_exception_taken(cpu, excret, true, false);
907bedb3
PM
7134 qemu_log_mask(CPU_LOG_INT, "...taking SecureFault on existing "
7135 "stackframe: failed exception return integrity "
7136 "signature check\n");
7137 return;
7138 }
7139
95695eff
PM
7140 pop_ok =
7141 v7m_stack_read(cpu, &env->regs[4], frameptr + 0x8, mmu_idx) &&
7142 v7m_stack_read(cpu, &env->regs[4], frameptr + 0x8, mmu_idx) &&
7143 v7m_stack_read(cpu, &env->regs[5], frameptr + 0xc, mmu_idx) &&
7144 v7m_stack_read(cpu, &env->regs[6], frameptr + 0x10, mmu_idx) &&
7145 v7m_stack_read(cpu, &env->regs[7], frameptr + 0x14, mmu_idx) &&
7146 v7m_stack_read(cpu, &env->regs[8], frameptr + 0x18, mmu_idx) &&
7147 v7m_stack_read(cpu, &env->regs[9], frameptr + 0x1c, mmu_idx) &&
7148 v7m_stack_read(cpu, &env->regs[10], frameptr + 0x20, mmu_idx) &&
7149 v7m_stack_read(cpu, &env->regs[11], frameptr + 0x24, mmu_idx);
907bedb3
PM
7150
7151 frameptr += 0x28;
7152 }
7153
95695eff
PM
7154 /* Pop registers */
7155 pop_ok = pop_ok &&
7156 v7m_stack_read(cpu, &env->regs[0], frameptr, mmu_idx) &&
7157 v7m_stack_read(cpu, &env->regs[1], frameptr + 0x4, mmu_idx) &&
7158 v7m_stack_read(cpu, &env->regs[2], frameptr + 0x8, mmu_idx) &&
7159 v7m_stack_read(cpu, &env->regs[3], frameptr + 0xc, mmu_idx) &&
7160 v7m_stack_read(cpu, &env->regs[12], frameptr + 0x10, mmu_idx) &&
7161 v7m_stack_read(cpu, &env->regs[14], frameptr + 0x14, mmu_idx) &&
7162 v7m_stack_read(cpu, &env->regs[15], frameptr + 0x18, mmu_idx) &&
7163 v7m_stack_read(cpu, &xpsr, frameptr + 0x1c, mmu_idx);
7164
7165 if (!pop_ok) {
7166 /* v7m_stack_read() pended a fault, so take it (as a tail
7167 * chained exception on the same stack frame)
7168 */
7169 v7m_exception_taken(cpu, excret, true, false);
7170 return;
7171 }
4e4259d3
PM
7172
7173 /* Returning from an exception with a PC with bit 0 set is defined
7174 * behaviour on v8M (bit 0 is ignored), but for v7M it was specified
7175 * to be UNPREDICTABLE. In practice actual v7M hardware seems to ignore
7176 * the lsbit, and there are several RTOSes out there which incorrectly
7177 * assume the r15 in the stack frame should be a Thumb-style "lsbit
7178 * indicates ARM/Thumb" value, so ignore the bit on v7M as well, but
7179 * complain about the badly behaved guest.
7180 */
5b522399 7181 if (env->regs[15] & 1) {
5b522399 7182 env->regs[15] &= ~1U;
4e4259d3
PM
7183 if (!arm_feature(env, ARM_FEATURE_V8)) {
7184 qemu_log_mask(LOG_GUEST_ERROR,
7185 "M profile return from interrupt with misaligned "
7186 "PC is UNPREDICTABLE on v7M\n");
7187 }
5b522399 7188 }
4e4259d3 7189
224e0c30
PM
7190 if (arm_feature(env, ARM_FEATURE_V8)) {
7191 /* For v8M we have to check whether the xPSR exception field
7192 * matches the EXCRET value for return to handler/thread
7193 * before we commit to changing the SP and xPSR.
7194 */
7195 bool will_be_handler = (xpsr & XPSR_EXCP) != 0;
7196 if (return_to_handler != will_be_handler) {
7197 /* Take an INVPC UsageFault on the current stack.
7198 * By this point we will have switched to the security state
7199 * for the background state, so this UsageFault will target
7200 * that state.
7201 */
7202 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE,
7203 env->v7m.secure);
7204 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_INVPC_MASK;
0094ca70 7205 v7m_exception_taken(cpu, excret, true, false);
224e0c30
PM
7206 qemu_log_mask(CPU_LOG_INT, "...taking UsageFault on existing "
7207 "stackframe: failed exception return integrity "
7208 "check\n");
7209 return;
7210 }
7211 }
7212
5b522399
PM
7213 /* Commit to consuming the stack frame */
7214 frameptr += 0x20;
7215 /* Undo stack alignment (the SPREALIGN bit indicates that the original
7216 * pre-exception SP was not 8-aligned and we added a padding word to
7217 * align it, so we undo this by ORing in the bit that increases it
7218 * from the current 8-aligned value to the 8-unaligned value. (Adding 4
7219 * would work too but a logical OR is how the pseudocode specifies it.)
7220 */
7221 if (xpsr & XPSR_SPREALIGN) {
7222 frameptr |= 4;
7223 }
7224 *frame_sp_p = frameptr;
fcf83ab1 7225 }
5b522399 7226 /* This xpsr_write() will invalidate frame_sp_p as it may switch stack */
987ab45e 7227 xpsr_write(env, xpsr, ~XPSR_SPREALIGN);
aa488fe3
PM
7228
7229 /* The restored xPSR exception field will be zero if we're
7230 * resuming in Thread mode. If that doesn't match what the
351e527a 7231 * exception return excret specified then this is a UsageFault.
224e0c30 7232 * v7M requires we make this check here; v8M did it earlier.
aa488fe3 7233 */
15b3f556 7234 if (return_to_handler != arm_v7m_is_handler_mode(env)) {
224e0c30
PM
7235 /* Take an INVPC UsageFault by pushing the stack again;
7236 * we know we're v7M so this is never a Secure UsageFault.
2fb50a33 7237 */
0094ca70
PM
7238 bool ignore_stackfaults;
7239
224e0c30 7240 assert(!arm_feature(env, ARM_FEATURE_V8));
2fb50a33 7241 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, false);
334e8dad 7242 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_INVPC_MASK;
0094ca70
PM
7243 ignore_stackfaults = v7m_push_stack(cpu);
7244 v7m_exception_taken(cpu, excret, false, ignore_stackfaults);
aa488fe3
PM
7245 qemu_log_mask(CPU_LOG_INT, "...taking UsageFault on new stackframe: "
7246 "failed exception return integrity check\n");
7247 return;
7248 }
7249
7250 /* Otherwise, we have a successful exception exit. */
dc3c4c14 7251 arm_clear_exclusive(env);
aa488fe3 7252 qemu_log_mask(CPU_LOG_INT, "...successful exception return\n");
9ee6e8bb
PB
7253}
7254
d02a8698
PM
7255static bool do_v7m_function_return(ARMCPU *cpu)
7256{
7257 /* v8M security extensions magic function return.
7258 * We may either:
7259 * (1) throw an exception (longjump)
7260 * (2) return true if we successfully handled the function return
7261 * (3) return false if we failed a consistency check and have
7262 * pended a UsageFault that needs to be taken now
7263 *
7264 * At this point the magic return value is split between env->regs[15]
7265 * and env->thumb. We don't bother to reconstitute it because we don't
7266 * need it (all values are handled the same way).
7267 */
7268 CPUARMState *env = &cpu->env;
7269 uint32_t newpc, newpsr, newpsr_exc;
7270
7271 qemu_log_mask(CPU_LOG_INT, "...really v7M secure function return\n");
7272
7273 {
7274 bool threadmode, spsel;
7275 TCGMemOpIdx oi;
7276 ARMMMUIdx mmu_idx;
7277 uint32_t *frame_sp_p;
7278 uint32_t frameptr;
7279
7280 /* Pull the return address and IPSR from the Secure stack */
7281 threadmode = !arm_v7m_is_handler_mode(env);
7282 spsel = env->v7m.control[M_REG_S] & R_V7M_CONTROL_SPSEL_MASK;
7283
7284 frame_sp_p = get_v7m_sp_ptr(env, true, threadmode, spsel);
7285 frameptr = *frame_sp_p;
7286
7287 /* These loads may throw an exception (for MPU faults). We want to
7288 * do them as secure, so work out what MMU index that is.
7289 */
7290 mmu_idx = arm_v7m_mmu_idx_for_secstate(env, true);
7291 oi = make_memop_idx(MO_LE, arm_to_core_mmu_idx(mmu_idx));
7292 newpc = helper_le_ldul_mmu(env, frameptr, oi, 0);
7293 newpsr = helper_le_ldul_mmu(env, frameptr + 4, oi, 0);
7294
7295 /* Consistency checks on new IPSR */
7296 newpsr_exc = newpsr & XPSR_EXCP;
7297 if (!((env->v7m.exception == 0 && newpsr_exc == 0) ||
7298 (env->v7m.exception == 1 && newpsr_exc != 0))) {
7299 /* Pend the fault and tell our caller to take it */
7300 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_INVPC_MASK;
7301 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE,
7302 env->v7m.secure);
7303 qemu_log_mask(CPU_LOG_INT,
7304 "...taking INVPC UsageFault: "
7305 "IPSR consistency check failed\n");
7306 return false;
7307 }
7308
7309 *frame_sp_p = frameptr + 8;
7310 }
7311
7312 /* This invalidates frame_sp_p */
7313 switch_v7m_security_state(env, true);
7314 env->v7m.exception = newpsr_exc;
7315 env->v7m.control[M_REG_S] &= ~R_V7M_CONTROL_SFPA_MASK;
7316 if (newpsr & XPSR_SFPA) {
7317 env->v7m.control[M_REG_S] |= R_V7M_CONTROL_SFPA_MASK;
7318 }
7319 xpsr_write(env, 0, XPSR_IT);
7320 env->thumb = newpc & 1;
7321 env->regs[15] = newpc & ~1;
7322
7323 qemu_log_mask(CPU_LOG_INT, "...function return successful\n");
7324 return true;
7325}
7326
27a7ea8a
PB
7327static void arm_log_exception(int idx)
7328{
7329 if (qemu_loglevel_mask(CPU_LOG_INT)) {
7330 const char *exc = NULL;
2c4a7cc5
PM
7331 static const char * const excnames[] = {
7332 [EXCP_UDEF] = "Undefined Instruction",
7333 [EXCP_SWI] = "SVC",
7334 [EXCP_PREFETCH_ABORT] = "Prefetch Abort",
7335 [EXCP_DATA_ABORT] = "Data Abort",
7336 [EXCP_IRQ] = "IRQ",
7337 [EXCP_FIQ] = "FIQ",
7338 [EXCP_BKPT] = "Breakpoint",
7339 [EXCP_EXCEPTION_EXIT] = "QEMU v7M exception exit",
7340 [EXCP_KERNEL_TRAP] = "QEMU intercept of kernel commpage",
7341 [EXCP_HVC] = "Hypervisor Call",
7342 [EXCP_HYP_TRAP] = "Hypervisor Trap",
7343 [EXCP_SMC] = "Secure Monitor Call",
7344 [EXCP_VIRQ] = "Virtual IRQ",
7345 [EXCP_VFIQ] = "Virtual FIQ",
7346 [EXCP_SEMIHOST] = "Semihosting call",
7347 [EXCP_NOCP] = "v7M NOCP UsageFault",
7348 [EXCP_INVSTATE] = "v7M INVSTATE UsageFault",
7349 };
27a7ea8a
PB
7350
7351 if (idx >= 0 && idx < ARRAY_SIZE(excnames)) {
7352 exc = excnames[idx];
7353 }
7354 if (!exc) {
7355 exc = "unknown";
7356 }
7357 qemu_log_mask(CPU_LOG_INT, "Taking exception %d [%s]\n", idx, exc);
7358 }
7359}
7360
333e10c5
PM
7361static bool v7m_read_half_insn(ARMCPU *cpu, ARMMMUIdx mmu_idx,
7362 uint32_t addr, uint16_t *insn)
7363{
7364 /* Load a 16-bit portion of a v7M instruction, returning true on success,
7365 * or false on failure (in which case we will have pended the appropriate
7366 * exception).
7367 * We need to do the instruction fetch's MPU and SAU checks
7368 * like this because there is no MMU index that would allow
7369 * doing the load with a single function call. Instead we must
7370 * first check that the security attributes permit the load
7371 * and that they don't mismatch on the two halves of the instruction,
7372 * and then we do the load as a secure load (ie using the security
7373 * attributes of the address, not the CPU, as architecturally required).
7374 */
7375 CPUState *cs = CPU(cpu);
7376 CPUARMState *env = &cpu->env;
7377 V8M_SAttributes sattrs = {};
7378 MemTxAttrs attrs = {};
7379 ARMMMUFaultInfo fi = {};
7380 MemTxResult txres;
7381 target_ulong page_size;
7382 hwaddr physaddr;
7383 int prot;
333e10c5
PM
7384
7385 v8m_security_lookup(env, addr, MMU_INST_FETCH, mmu_idx, &sattrs);
7386 if (!sattrs.nsc || sattrs.ns) {
7387 /* This must be the second half of the insn, and it straddles a
7388 * region boundary with the second half not being S&NSC.
7389 */
7390 env->v7m.sfsr |= R_V7M_SFSR_INVEP_MASK;
7391 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SECURE, false);
7392 qemu_log_mask(CPU_LOG_INT,
7393 "...really SecureFault with SFSR.INVEP\n");
7394 return false;
7395 }
7396 if (get_phys_addr(env, addr, MMU_INST_FETCH, mmu_idx,
bc52bfeb 7397 &physaddr, &attrs, &prot, &page_size, &fi, NULL)) {
333e10c5
PM
7398 /* the MPU lookup failed */
7399 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_IACCVIOL_MASK;
7400 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_MEM, env->v7m.secure);
7401 qemu_log_mask(CPU_LOG_INT, "...really MemManage with CFSR.IACCVIOL\n");
7402 return false;
7403 }
7404 *insn = address_space_lduw_le(arm_addressspace(cs, attrs), physaddr,
7405 attrs, &txres);
7406 if (txres != MEMTX_OK) {
7407 env->v7m.cfsr[M_REG_NS] |= R_V7M_CFSR_IBUSERR_MASK;
7408 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_BUS, false);
7409 qemu_log_mask(CPU_LOG_INT, "...really BusFault with CFSR.IBUSERR\n");
7410 return false;
7411 }
7412 return true;
7413}
7414
7415static bool v7m_handle_execute_nsc(ARMCPU *cpu)
7416{
7417 /* Check whether this attempt to execute code in a Secure & NS-Callable
7418 * memory region is for an SG instruction; if so, then emulate the
7419 * effect of the SG instruction and return true. Otherwise pend
7420 * the correct kind of exception and return false.
7421 */
7422 CPUARMState *env = &cpu->env;
7423 ARMMMUIdx mmu_idx;
7424 uint16_t insn;
7425
7426 /* We should never get here unless get_phys_addr_pmsav8() caused
7427 * an exception for NS executing in S&NSC memory.
7428 */
7429 assert(!env->v7m.secure);
7430 assert(arm_feature(env, ARM_FEATURE_M_SECURITY));
7431
7432 /* We want to do the MPU lookup as secure; work out what mmu_idx that is */
7433 mmu_idx = arm_v7m_mmu_idx_for_secstate(env, true);
7434
7435 if (!v7m_read_half_insn(cpu, mmu_idx, env->regs[15], &insn)) {
7436 return false;
7437 }
7438
7439 if (!env->thumb) {
7440 goto gen_invep;
7441 }
7442
7443 if (insn != 0xe97f) {
7444 /* Not an SG instruction first half (we choose the IMPDEF
7445 * early-SG-check option).
7446 */
7447 goto gen_invep;
7448 }
7449
7450 if (!v7m_read_half_insn(cpu, mmu_idx, env->regs[15] + 2, &insn)) {
7451 return false;
7452 }
7453
7454 if (insn != 0xe97f) {
7455 /* Not an SG instruction second half (yes, both halves of the SG
7456 * insn have the same hex value)
7457 */
7458 goto gen_invep;
7459 }
7460
7461 /* OK, we have confirmed that we really have an SG instruction.
7462 * We know we're NS in S memory so don't need to repeat those checks.
7463 */
7464 qemu_log_mask(CPU_LOG_INT, "...really an SG instruction at 0x%08" PRIx32
7465 ", executing it\n", env->regs[15]);
7466 env->regs[14] &= ~1;
7467 switch_v7m_security_state(env, true);
7468 xpsr_write(env, 0, XPSR_IT);
7469 env->regs[15] += 4;
7470 return true;
7471
7472gen_invep:
7473 env->v7m.sfsr |= R_V7M_SFSR_INVEP_MASK;
7474 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SECURE, false);
7475 qemu_log_mask(CPU_LOG_INT,
7476 "...really SecureFault with SFSR.INVEP\n");
7477 return false;
7478}
7479
e6f010cc 7480void arm_v7m_cpu_do_interrupt(CPUState *cs)
9ee6e8bb 7481{
e6f010cc
AF
7482 ARMCPU *cpu = ARM_CPU(cs);
7483 CPUARMState *env = &cpu->env;
9ee6e8bb 7484 uint32_t lr;
0094ca70 7485 bool ignore_stackfaults;
9ee6e8bb 7486
27103424 7487 arm_log_exception(cs->exception_index);
3f1beaca 7488
9ee6e8bb
PB
7489 /* For exceptions we just mark as pending on the NVIC, and let that
7490 handle it. */
27103424 7491 switch (cs->exception_index) {
9ee6e8bb 7492 case EXCP_UDEF:
2fb50a33 7493 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, env->v7m.secure);
334e8dad 7494 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_UNDEFINSTR_MASK;
a25dc805 7495 break;
7517748e 7496 case EXCP_NOCP:
2fb50a33 7497 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, env->v7m.secure);
334e8dad 7498 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_NOCP_MASK;
a25dc805 7499 break;
e13886e3 7500 case EXCP_INVSTATE:
2fb50a33 7501 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, env->v7m.secure);
334e8dad 7502 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_INVSTATE_MASK;
e13886e3 7503 break;
9ee6e8bb 7504 case EXCP_SWI:
314e2296 7505 /* The PC already points to the next instruction. */
2fb50a33 7506 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SVC, env->v7m.secure);
a25dc805 7507 break;
9ee6e8bb
PB
7508 case EXCP_PREFETCH_ABORT:
7509 case EXCP_DATA_ABORT:
5dd0641d
MD
7510 /* Note that for M profile we don't have a guest facing FSR, but
7511 * the env->exception.fsr will be populated by the code that
7512 * raises the fault, in the A profile short-descriptor format.
abf1172f 7513 */
5dd0641d 7514 switch (env->exception.fsr & 0xf) {
35337cc3
PM
7515 case M_FAKE_FSR_NSC_EXEC:
7516 /* Exception generated when we try to execute code at an address
7517 * which is marked as Secure & Non-Secure Callable and the CPU
7518 * is in the Non-Secure state. The only instruction which can
7519 * be executed like this is SG (and that only if both halves of
7520 * the SG instruction have the same security attributes.)
7521 * Everything else must generate an INVEP SecureFault, so we
7522 * emulate the SG instruction here.
35337cc3 7523 */
333e10c5
PM
7524 if (v7m_handle_execute_nsc(cpu)) {
7525 return;
7526 }
35337cc3
PM
7527 break;
7528 case M_FAKE_FSR_SFAULT:
7529 /* Various flavours of SecureFault for attempts to execute or
7530 * access data in the wrong security state.
7531 */
7532 switch (cs->exception_index) {
7533 case EXCP_PREFETCH_ABORT:
7534 if (env->v7m.secure) {
7535 env->v7m.sfsr |= R_V7M_SFSR_INVTRAN_MASK;
7536 qemu_log_mask(CPU_LOG_INT,
7537 "...really SecureFault with SFSR.INVTRAN\n");
7538 } else {
7539 env->v7m.sfsr |= R_V7M_SFSR_INVEP_MASK;
7540 qemu_log_mask(CPU_LOG_INT,
7541 "...really SecureFault with SFSR.INVEP\n");
7542 }
7543 break;
7544 case EXCP_DATA_ABORT:
7545 /* This must be an NS access to S memory */
7546 env->v7m.sfsr |= R_V7M_SFSR_AUVIOL_MASK;
7547 qemu_log_mask(CPU_LOG_INT,
7548 "...really SecureFault with SFSR.AUVIOL\n");
7549 break;
7550 }
7551 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SECURE, false);
7552 break;
5dd0641d
MD
7553 case 0x8: /* External Abort */
7554 switch (cs->exception_index) {
7555 case EXCP_PREFETCH_ABORT:
c6158878
PM
7556 env->v7m.cfsr[M_REG_NS] |= R_V7M_CFSR_IBUSERR_MASK;
7557 qemu_log_mask(CPU_LOG_INT, "...with CFSR.IBUSERR\n");
5dd0641d
MD
7558 break;
7559 case EXCP_DATA_ABORT:
334e8dad 7560 env->v7m.cfsr[M_REG_NS] |=
c6158878 7561 (R_V7M_CFSR_PRECISERR_MASK | R_V7M_CFSR_BFARVALID_MASK);
5dd0641d
MD
7562 env->v7m.bfar = env->exception.vaddress;
7563 qemu_log_mask(CPU_LOG_INT,
c6158878 7564 "...with CFSR.PRECISERR and BFAR 0x%x\n",
5dd0641d
MD
7565 env->v7m.bfar);
7566 break;
7567 }
2fb50a33 7568 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_BUS, false);
5dd0641d
MD
7569 break;
7570 default:
7571 /* All other FSR values are either MPU faults or "can't happen
7572 * for M profile" cases.
7573 */
7574 switch (cs->exception_index) {
7575 case EXCP_PREFETCH_ABORT:
334e8dad 7576 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_IACCVIOL_MASK;
5dd0641d
MD
7577 qemu_log_mask(CPU_LOG_INT, "...with CFSR.IACCVIOL\n");
7578 break;
7579 case EXCP_DATA_ABORT:
334e8dad 7580 env->v7m.cfsr[env->v7m.secure] |=
5dd0641d 7581 (R_V7M_CFSR_DACCVIOL_MASK | R_V7M_CFSR_MMARVALID_MASK);
c51a5cfc 7582 env->v7m.mmfar[env->v7m.secure] = env->exception.vaddress;
5dd0641d
MD
7583 qemu_log_mask(CPU_LOG_INT,
7584 "...with CFSR.DACCVIOL and MMFAR 0x%x\n",
c51a5cfc 7585 env->v7m.mmfar[env->v7m.secure]);
5dd0641d
MD
7586 break;
7587 }
2fb50a33
PM
7588 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_MEM,
7589 env->v7m.secure);
5dd0641d
MD
7590 break;
7591 }
a25dc805 7592 break;
9ee6e8bb 7593 case EXCP_BKPT:
cfe67cef 7594 if (semihosting_enabled()) {
2ad207d4 7595 int nr;
f9fd40eb 7596 nr = arm_lduw_code(env, env->regs[15], arm_sctlr_b(env)) & 0xff;
2ad207d4
PB
7597 if (nr == 0xab) {
7598 env->regs[15] += 2;
205ace55
CC
7599 qemu_log_mask(CPU_LOG_INT,
7600 "...handling as semihosting call 0x%x\n",
7601 env->regs[0]);
2ad207d4
PB
7602 env->regs[0] = do_arm_semihosting(env);
7603 return;
7604 }
7605 }
2fb50a33 7606 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_DEBUG, false);
a25dc805 7607 break;
9ee6e8bb 7608 case EXCP_IRQ:
9ee6e8bb
PB
7609 break;
7610 case EXCP_EXCEPTION_EXIT:
d02a8698
PM
7611 if (env->regs[15] < EXC_RETURN_MIN_MAGIC) {
7612 /* Must be v8M security extension function return */
7613 assert(env->regs[15] >= FNC_RETURN_MIN_MAGIC);
7614 assert(arm_feature(env, ARM_FEATURE_M_SECURITY));
7615 if (do_v7m_function_return(cpu)) {
7616 return;
7617 }
7618 } else {
7619 do_v7m_exception_exit(cpu);
7620 return;
7621 }
7622 break;
9ee6e8bb 7623 default:
a47dddd7 7624 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
9ee6e8bb
PB
7625 return; /* Never happens. Keep compiler happy. */
7626 }
7627
d3392718
PM
7628 if (arm_feature(env, ARM_FEATURE_V8)) {
7629 lr = R_V7M_EXCRET_RES1_MASK |
7630 R_V7M_EXCRET_DCRS_MASK |
7631 R_V7M_EXCRET_FTYPE_MASK;
7632 /* The S bit indicates whether we should return to Secure
7633 * or NonSecure (ie our current state).
7634 * The ES bit indicates whether we're taking this exception
7635 * to Secure or NonSecure (ie our target state). We set it
7636 * later, in v7m_exception_taken().
7637 * The SPSEL bit is also set in v7m_exception_taken() for v8M.
7638 * This corresponds to the ARM ARM pseudocode for v8M setting
7639 * some LR bits in PushStack() and some in ExceptionTaken();
7640 * the distinction matters for the tailchain cases where we
7641 * can take an exception without pushing the stack.
7642 */
7643 if (env->v7m.secure) {
7644 lr |= R_V7M_EXCRET_S_MASK;
7645 }
7646 } else {
7647 lr = R_V7M_EXCRET_RES1_MASK |
7648 R_V7M_EXCRET_S_MASK |
7649 R_V7M_EXCRET_DCRS_MASK |
7650 R_V7M_EXCRET_FTYPE_MASK |
7651 R_V7M_EXCRET_ES_MASK;
7652 if (env->v7m.control[M_REG_NS] & R_V7M_CONTROL_SPSEL_MASK) {
7653 lr |= R_V7M_EXCRET_SPSEL_MASK;
7654 }
bd70b29b 7655 }
15b3f556 7656 if (!arm_v7m_is_handler_mode(env)) {
4d1e7a47 7657 lr |= R_V7M_EXCRET_MODE_MASK;
bd70b29b
PM
7658 }
7659
0094ca70
PM
7660 ignore_stackfaults = v7m_push_stack(cpu);
7661 v7m_exception_taken(cpu, lr, false, ignore_stackfaults);
a25dc805 7662 qemu_log_mask(CPU_LOG_INT, "... as %d\n", env->v7m.exception);
9ee6e8bb
PB
7663}
7664
ce02049d
GB
7665/* Function used to synchronize QEMU's AArch64 register set with AArch32
7666 * register set. This is necessary when switching between AArch32 and AArch64
7667 * execution state.
7668 */
7669void aarch64_sync_32_to_64(CPUARMState *env)
7670{
7671 int i;
7672 uint32_t mode = env->uncached_cpsr & CPSR_M;
7673
7674 /* We can blanket copy R[0:7] to X[0:7] */
7675 for (i = 0; i < 8; i++) {
7676 env->xregs[i] = env->regs[i];
7677 }
7678
7679 /* Unless we are in FIQ mode, x8-x12 come from the user registers r8-r12.
7680 * Otherwise, they come from the banked user regs.
7681 */
7682 if (mode == ARM_CPU_MODE_FIQ) {
7683 for (i = 8; i < 13; i++) {
7684 env->xregs[i] = env->usr_regs[i - 8];
7685 }
7686 } else {
7687 for (i = 8; i < 13; i++) {
7688 env->xregs[i] = env->regs[i];
7689 }
7690 }
7691
7692 /* Registers x13-x23 are the various mode SP and FP registers. Registers
7693 * r13 and r14 are only copied if we are in that mode, otherwise we copy
7694 * from the mode banked register.
7695 */
7696 if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
7697 env->xregs[13] = env->regs[13];
7698 env->xregs[14] = env->regs[14];
7699 } else {
7700 env->xregs[13] = env->banked_r13[bank_number(ARM_CPU_MODE_USR)];
7701 /* HYP is an exception in that it is copied from r14 */
7702 if (mode == ARM_CPU_MODE_HYP) {
7703 env->xregs[14] = env->regs[14];
7704 } else {
7705 env->xregs[14] = env->banked_r14[bank_number(ARM_CPU_MODE_USR)];
7706 }
7707 }
7708
7709 if (mode == ARM_CPU_MODE_HYP) {
7710 env->xregs[15] = env->regs[13];
7711 } else {
7712 env->xregs[15] = env->banked_r13[bank_number(ARM_CPU_MODE_HYP)];
7713 }
7714
7715 if (mode == ARM_CPU_MODE_IRQ) {
3a9148d0
SS
7716 env->xregs[16] = env->regs[14];
7717 env->xregs[17] = env->regs[13];
ce02049d 7718 } else {
3a9148d0
SS
7719 env->xregs[16] = env->banked_r14[bank_number(ARM_CPU_MODE_IRQ)];
7720 env->xregs[17] = env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)];
ce02049d
GB
7721 }
7722
7723 if (mode == ARM_CPU_MODE_SVC) {
3a9148d0
SS
7724 env->xregs[18] = env->regs[14];
7725 env->xregs[19] = env->regs[13];
ce02049d 7726 } else {
3a9148d0
SS
7727 env->xregs[18] = env->banked_r14[bank_number(ARM_CPU_MODE_SVC)];
7728 env->xregs[19] = env->banked_r13[bank_number(ARM_CPU_MODE_SVC)];
ce02049d
GB
7729 }
7730
7731 if (mode == ARM_CPU_MODE_ABT) {
3a9148d0
SS
7732 env->xregs[20] = env->regs[14];
7733 env->xregs[21] = env->regs[13];
ce02049d 7734 } else {
3a9148d0
SS
7735 env->xregs[20] = env->banked_r14[bank_number(ARM_CPU_MODE_ABT)];
7736 env->xregs[21] = env->banked_r13[bank_number(ARM_CPU_MODE_ABT)];
ce02049d
GB
7737 }
7738
7739 if (mode == ARM_CPU_MODE_UND) {
3a9148d0
SS
7740 env->xregs[22] = env->regs[14];
7741 env->xregs[23] = env->regs[13];
ce02049d 7742 } else {
3a9148d0
SS
7743 env->xregs[22] = env->banked_r14[bank_number(ARM_CPU_MODE_UND)];
7744 env->xregs[23] = env->banked_r13[bank_number(ARM_CPU_MODE_UND)];
ce02049d
GB
7745 }
7746
7747 /* Registers x24-x30 are mapped to r8-r14 in FIQ mode. If we are in FIQ
7748 * mode, then we can copy from r8-r14. Otherwise, we copy from the
7749 * FIQ bank for r8-r14.
7750 */
7751 if (mode == ARM_CPU_MODE_FIQ) {
7752 for (i = 24; i < 31; i++) {
7753 env->xregs[i] = env->regs[i - 16]; /* X[24:30] <- R[8:14] */
7754 }
7755 } else {
7756 for (i = 24; i < 29; i++) {
7757 env->xregs[i] = env->fiq_regs[i - 24];
7758 }
7759 env->xregs[29] = env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)];
7760 env->xregs[30] = env->banked_r14[bank_number(ARM_CPU_MODE_FIQ)];
7761 }
7762
7763 env->pc = env->regs[15];
7764}
7765
7766/* Function used to synchronize QEMU's AArch32 register set with AArch64
7767 * register set. This is necessary when switching between AArch32 and AArch64
7768 * execution state.
7769 */
7770void aarch64_sync_64_to_32(CPUARMState *env)
7771{
7772 int i;
7773 uint32_t mode = env->uncached_cpsr & CPSR_M;
7774
7775 /* We can blanket copy X[0:7] to R[0:7] */
7776 for (i = 0; i < 8; i++) {
7777 env->regs[i] = env->xregs[i];
7778 }
7779
7780 /* Unless we are in FIQ mode, r8-r12 come from the user registers x8-x12.
7781 * Otherwise, we copy x8-x12 into the banked user regs.
7782 */
7783 if (mode == ARM_CPU_MODE_FIQ) {
7784 for (i = 8; i < 13; i++) {
7785 env->usr_regs[i - 8] = env->xregs[i];
7786 }
7787 } else {
7788 for (i = 8; i < 13; i++) {
7789 env->regs[i] = env->xregs[i];
7790 }
7791 }
7792
7793 /* Registers r13 & r14 depend on the current mode.
7794 * If we are in a given mode, we copy the corresponding x registers to r13
7795 * and r14. Otherwise, we copy the x register to the banked r13 and r14
7796 * for the mode.
7797 */
7798 if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
7799 env->regs[13] = env->xregs[13];
7800 env->regs[14] = env->xregs[14];
7801 } else {
7802 env->banked_r13[bank_number(ARM_CPU_MODE_USR)] = env->xregs[13];
7803
7804 /* HYP is an exception in that it does not have its own banked r14 but
7805 * shares the USR r14
7806 */
7807 if (mode == ARM_CPU_MODE_HYP) {
7808 env->regs[14] = env->xregs[14];
7809 } else {
7810 env->banked_r14[bank_number(ARM_CPU_MODE_USR)] = env->xregs[14];
7811 }
7812 }
7813
7814 if (mode == ARM_CPU_MODE_HYP) {
7815 env->regs[13] = env->xregs[15];
7816 } else {
7817 env->banked_r13[bank_number(ARM_CPU_MODE_HYP)] = env->xregs[15];
7818 }
7819
7820 if (mode == ARM_CPU_MODE_IRQ) {
3a9148d0
SS
7821 env->regs[14] = env->xregs[16];
7822 env->regs[13] = env->xregs[17];
ce02049d 7823 } else {
3a9148d0
SS
7824 env->banked_r14[bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[16];
7825 env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[17];
ce02049d
GB
7826 }
7827
7828 if (mode == ARM_CPU_MODE_SVC) {
3a9148d0
SS
7829 env->regs[14] = env->xregs[18];
7830 env->regs[13] = env->xregs[19];
ce02049d 7831 } else {
3a9148d0
SS
7832 env->banked_r14[bank_number(ARM_CPU_MODE_SVC)] = env->xregs[18];
7833 env->banked_r13[bank_number(ARM_CPU_MODE_SVC)] = env->xregs[19];
ce02049d
GB
7834 }
7835
7836 if (mode == ARM_CPU_MODE_ABT) {
3a9148d0
SS
7837 env->regs[14] = env->xregs[20];
7838 env->regs[13] = env->xregs[21];
ce02049d 7839 } else {
3a9148d0
SS
7840 env->banked_r14[bank_number(ARM_CPU_MODE_ABT)] = env->xregs[20];
7841 env->banked_r13[bank_number(ARM_CPU_MODE_ABT)] = env->xregs[21];
ce02049d
GB
7842 }
7843
7844 if (mode == ARM_CPU_MODE_UND) {
3a9148d0
SS
7845 env->regs[14] = env->xregs[22];
7846 env->regs[13] = env->xregs[23];
ce02049d 7847 } else {
3a9148d0
SS
7848 env->banked_r14[bank_number(ARM_CPU_MODE_UND)] = env->xregs[22];
7849 env->banked_r13[bank_number(ARM_CPU_MODE_UND)] = env->xregs[23];
ce02049d
GB
7850 }
7851
7852 /* Registers x24-x30 are mapped to r8-r14 in FIQ mode. If we are in FIQ
7853 * mode, then we can copy to r8-r14. Otherwise, we copy to the
7854 * FIQ bank for r8-r14.
7855 */
7856 if (mode == ARM_CPU_MODE_FIQ) {
7857 for (i = 24; i < 31; i++) {
7858 env->regs[i - 16] = env->xregs[i]; /* X[24:30] -> R[8:14] */
7859 }
7860 } else {
7861 for (i = 24; i < 29; i++) {
7862 env->fiq_regs[i - 24] = env->xregs[i];
7863 }
7864 env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[29];
7865 env->banked_r14[bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[30];
7866 }
7867
7868 env->regs[15] = env->pc;
7869}
7870
966f758c 7871static void arm_cpu_do_interrupt_aarch32(CPUState *cs)
b5ff1b31 7872{
97a8ea5a
AF
7873 ARMCPU *cpu = ARM_CPU(cs);
7874 CPUARMState *env = &cpu->env;
b5ff1b31
FB
7875 uint32_t addr;
7876 uint32_t mask;
7877 int new_mode;
7878 uint32_t offset;
16a906fd 7879 uint32_t moe;
b5ff1b31 7880
16a906fd
PM
7881 /* If this is a debug exception we must update the DBGDSCR.MOE bits */
7882 switch (env->exception.syndrome >> ARM_EL_EC_SHIFT) {
7883 case EC_BREAKPOINT:
7884 case EC_BREAKPOINT_SAME_EL:
7885 moe = 1;
7886 break;
7887 case EC_WATCHPOINT:
7888 case EC_WATCHPOINT_SAME_EL:
7889 moe = 10;
7890 break;
7891 case EC_AA32_BKPT:
7892 moe = 3;
7893 break;
7894 case EC_VECTORCATCH:
7895 moe = 5;
7896 break;
7897 default:
7898 moe = 0;
7899 break;
7900 }
7901
7902 if (moe) {
7903 env->cp15.mdscr_el1 = deposit64(env->cp15.mdscr_el1, 2, 4, moe);
7904 }
7905
b5ff1b31 7906 /* TODO: Vectored interrupt controller. */
27103424 7907 switch (cs->exception_index) {
b5ff1b31
FB
7908 case EXCP_UDEF:
7909 new_mode = ARM_CPU_MODE_UND;
7910 addr = 0x04;
7911 mask = CPSR_I;
7912 if (env->thumb)
7913 offset = 2;
7914 else
7915 offset = 4;
7916 break;
7917 case EXCP_SWI:
7918 new_mode = ARM_CPU_MODE_SVC;
7919 addr = 0x08;
7920 mask = CPSR_I;
601d70b9 7921 /* The PC already points to the next instruction. */
b5ff1b31
FB
7922 offset = 0;
7923 break;
06c949e6 7924 case EXCP_BKPT:
abf1172f 7925 env->exception.fsr = 2;
9ee6e8bb
PB
7926 /* Fall through to prefetch abort. */
7927 case EXCP_PREFETCH_ABORT:
88ca1c2d 7928 A32_BANKED_CURRENT_REG_SET(env, ifsr, env->exception.fsr);
b848ce2b 7929 A32_BANKED_CURRENT_REG_SET(env, ifar, env->exception.vaddress);
3f1beaca 7930 qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x IFAR 0x%x\n",
88ca1c2d 7931 env->exception.fsr, (uint32_t)env->exception.vaddress);
b5ff1b31
FB
7932 new_mode = ARM_CPU_MODE_ABT;
7933 addr = 0x0c;
7934 mask = CPSR_A | CPSR_I;
7935 offset = 4;
7936 break;
7937 case EXCP_DATA_ABORT:
4a7e2d73 7938 A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr);
b848ce2b 7939 A32_BANKED_CURRENT_REG_SET(env, dfar, env->exception.vaddress);
3f1beaca 7940 qemu_log_mask(CPU_LOG_INT, "...with DFSR 0x%x DFAR 0x%x\n",
4a7e2d73 7941 env->exception.fsr,
6cd8a264 7942 (uint32_t)env->exception.vaddress);
b5ff1b31
FB
7943 new_mode = ARM_CPU_MODE_ABT;
7944 addr = 0x10;
7945 mask = CPSR_A | CPSR_I;
7946 offset = 8;
7947 break;
7948 case EXCP_IRQ:
7949 new_mode = ARM_CPU_MODE_IRQ;
7950 addr = 0x18;
7951 /* Disable IRQ and imprecise data aborts. */
7952 mask = CPSR_A | CPSR_I;
7953 offset = 4;
de38d23b
FA
7954 if (env->cp15.scr_el3 & SCR_IRQ) {
7955 /* IRQ routed to monitor mode */
7956 new_mode = ARM_CPU_MODE_MON;
7957 mask |= CPSR_F;
7958 }
b5ff1b31
FB
7959 break;
7960 case EXCP_FIQ:
7961 new_mode = ARM_CPU_MODE_FIQ;
7962 addr = 0x1c;
7963 /* Disable FIQ, IRQ and imprecise data aborts. */
7964 mask = CPSR_A | CPSR_I | CPSR_F;
de38d23b
FA
7965 if (env->cp15.scr_el3 & SCR_FIQ) {
7966 /* FIQ routed to monitor mode */
7967 new_mode = ARM_CPU_MODE_MON;
7968 }
b5ff1b31
FB
7969 offset = 4;
7970 break;
87a4b270
PM
7971 case EXCP_VIRQ:
7972 new_mode = ARM_CPU_MODE_IRQ;
7973 addr = 0x18;
7974 /* Disable IRQ and imprecise data aborts. */
7975 mask = CPSR_A | CPSR_I;
7976 offset = 4;
7977 break;
7978 case EXCP_VFIQ:
7979 new_mode = ARM_CPU_MODE_FIQ;
7980 addr = 0x1c;
7981 /* Disable FIQ, IRQ and imprecise data aborts. */
7982 mask = CPSR_A | CPSR_I | CPSR_F;
7983 offset = 4;
7984 break;
dbe9d163
FA
7985 case EXCP_SMC:
7986 new_mode = ARM_CPU_MODE_MON;
7987 addr = 0x08;
7988 mask = CPSR_A | CPSR_I | CPSR_F;
7989 offset = 0;
7990 break;
b5ff1b31 7991 default:
a47dddd7 7992 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
b5ff1b31
FB
7993 return; /* Never happens. Keep compiler happy. */
7994 }
e89e51a1
FA
7995
7996 if (new_mode == ARM_CPU_MODE_MON) {
7997 addr += env->cp15.mvbar;
137feaa9 7998 } else if (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_V) {
e89e51a1 7999 /* High vectors. When enabled, base address cannot be remapped. */
b5ff1b31 8000 addr += 0xffff0000;
8641136c
NR
8001 } else {
8002 /* ARM v7 architectures provide a vector base address register to remap
8003 * the interrupt vector table.
e89e51a1 8004 * This register is only followed in non-monitor mode, and is banked.
8641136c
NR
8005 * Note: only bits 31:5 are valid.
8006 */
fb6c91ba 8007 addr += A32_BANKED_CURRENT_REG_GET(env, vbar);
b5ff1b31 8008 }
dbe9d163
FA
8009
8010 if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON) {
8011 env->cp15.scr_el3 &= ~SCR_NS;
8012 }
8013
b5ff1b31 8014 switch_mode (env, new_mode);
662cefb7
PM
8015 /* For exceptions taken to AArch32 we must clear the SS bit in both
8016 * PSTATE and in the old-state value we save to SPSR_<mode>, so zero it now.
8017 */
8018 env->uncached_cpsr &= ~PSTATE_SS;
b5ff1b31 8019 env->spsr = cpsr_read(env);
9ee6e8bb
PB
8020 /* Clear IT bits. */
8021 env->condexec_bits = 0;
30a8cac1 8022 /* Switch to the new mode, and to the correct instruction set. */
6d7e6326 8023 env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode;
73462ddd
PC
8024 /* Set new mode endianness */
8025 env->uncached_cpsr &= ~CPSR_E;
8026 if (env->cp15.sctlr_el[arm_current_el(env)] & SCTLR_EE) {
3823b9db 8027 env->uncached_cpsr |= CPSR_E;
73462ddd 8028 }
4cc35614 8029 env->daif |= mask;
be5e7a76
DES
8030 /* this is a lie, as the was no c1_sys on V4T/V5, but who cares
8031 * and we should just guard the thumb mode on V4 */
8032 if (arm_feature(env, ARM_FEATURE_V4T)) {
137feaa9 8033 env->thumb = (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_TE) != 0;
be5e7a76 8034 }
b5ff1b31
FB
8035 env->regs[14] = env->regs[15] + offset;
8036 env->regs[15] = addr;
b5ff1b31
FB
8037}
8038
966f758c
PM
8039/* Handle exception entry to a target EL which is using AArch64 */
8040static void arm_cpu_do_interrupt_aarch64(CPUState *cs)
f3a9b694
PM
8041{
8042 ARMCPU *cpu = ARM_CPU(cs);
8043 CPUARMState *env = &cpu->env;
8044 unsigned int new_el = env->exception.target_el;
8045 target_ulong addr = env->cp15.vbar_el[new_el];
8046 unsigned int new_mode = aarch64_pstate_mode(new_el, true);
8047
8048 if (arm_current_el(env) < new_el) {
3d6f7617
PM
8049 /* Entry vector offset depends on whether the implemented EL
8050 * immediately lower than the target level is using AArch32 or AArch64
8051 */
8052 bool is_aa64;
8053
8054 switch (new_el) {
8055 case 3:
8056 is_aa64 = (env->cp15.scr_el3 & SCR_RW) != 0;
8057 break;
8058 case 2:
8059 is_aa64 = (env->cp15.hcr_el2 & HCR_RW) != 0;
8060 break;
8061 case 1:
8062 is_aa64 = is_a64(env);
8063 break;
8064 default:
8065 g_assert_not_reached();
8066 }
8067
8068 if (is_aa64) {
f3a9b694
PM
8069 addr += 0x400;
8070 } else {
8071 addr += 0x600;
8072 }
8073 } else if (pstate_read(env) & PSTATE_SP) {
8074 addr += 0x200;
8075 }
8076
f3a9b694
PM
8077 switch (cs->exception_index) {
8078 case EXCP_PREFETCH_ABORT:
8079 case EXCP_DATA_ABORT:
8080 env->cp15.far_el[new_el] = env->exception.vaddress;
8081 qemu_log_mask(CPU_LOG_INT, "...with FAR 0x%" PRIx64 "\n",
8082 env->cp15.far_el[new_el]);
8083 /* fall through */
8084 case EXCP_BKPT:
8085 case EXCP_UDEF:
8086 case EXCP_SWI:
8087 case EXCP_HVC:
8088 case EXCP_HYP_TRAP:
8089 case EXCP_SMC:
8090 env->cp15.esr_el[new_el] = env->exception.syndrome;
8091 break;
8092 case EXCP_IRQ:
8093 case EXCP_VIRQ:
8094 addr += 0x80;
8095 break;
8096 case EXCP_FIQ:
8097 case EXCP_VFIQ:
8098 addr += 0x100;
8099 break;
8100 case EXCP_SEMIHOST:
8101 qemu_log_mask(CPU_LOG_INT,
8102 "...handling as semihosting call 0x%" PRIx64 "\n",
8103 env->xregs[0]);
8104 env->xregs[0] = do_arm_semihosting(env);
8105 return;
8106 default:
8107 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
8108 }
8109
8110 if (is_a64(env)) {
8111 env->banked_spsr[aarch64_banked_spsr_index(new_el)] = pstate_read(env);
8112 aarch64_save_sp(env, arm_current_el(env));
8113 env->elr_el[new_el] = env->pc;
8114 } else {
8115 env->banked_spsr[aarch64_banked_spsr_index(new_el)] = cpsr_read(env);
f3a9b694
PM
8116 env->elr_el[new_el] = env->regs[15];
8117
8118 aarch64_sync_32_to_64(env);
8119
8120 env->condexec_bits = 0;
8121 }
8122 qemu_log_mask(CPU_LOG_INT, "...with ELR 0x%" PRIx64 "\n",
8123 env->elr_el[new_el]);
8124
8125 pstate_write(env, PSTATE_DAIF | new_mode);
8126 env->aarch64 = 1;
8127 aarch64_restore_sp(env, new_el);
8128
8129 env->pc = addr;
8130
8131 qemu_log_mask(CPU_LOG_INT, "...to EL%d PC 0x%" PRIx64 " PSTATE 0x%x\n",
8132 new_el, env->pc, pstate_read(env));
966f758c
PM
8133}
8134
904c04de
PM
8135static inline bool check_for_semihosting(CPUState *cs)
8136{
8137 /* Check whether this exception is a semihosting call; if so
8138 * then handle it and return true; otherwise return false.
8139 */
8140 ARMCPU *cpu = ARM_CPU(cs);
8141 CPUARMState *env = &cpu->env;
8142
8143 if (is_a64(env)) {
8144 if (cs->exception_index == EXCP_SEMIHOST) {
8145 /* This is always the 64-bit semihosting exception.
8146 * The "is this usermode" and "is semihosting enabled"
8147 * checks have been done at translate time.
8148 */
8149 qemu_log_mask(CPU_LOG_INT,
8150 "...handling as semihosting call 0x%" PRIx64 "\n",
8151 env->xregs[0]);
8152 env->xregs[0] = do_arm_semihosting(env);
8153 return true;
8154 }
8155 return false;
8156 } else {
8157 uint32_t imm;
8158
8159 /* Only intercept calls from privileged modes, to provide some
8160 * semblance of security.
8161 */
19a6e31c
PM
8162 if (cs->exception_index != EXCP_SEMIHOST &&
8163 (!semihosting_enabled() ||
8164 ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_USR))) {
904c04de
PM
8165 return false;
8166 }
8167
8168 switch (cs->exception_index) {
19a6e31c
PM
8169 case EXCP_SEMIHOST:
8170 /* This is always a semihosting call; the "is this usermode"
8171 * and "is semihosting enabled" checks have been done at
8172 * translate time.
8173 */
8174 break;
904c04de
PM
8175 case EXCP_SWI:
8176 /* Check for semihosting interrupt. */
8177 if (env->thumb) {
f9fd40eb 8178 imm = arm_lduw_code(env, env->regs[15] - 2, arm_sctlr_b(env))
904c04de
PM
8179 & 0xff;
8180 if (imm == 0xab) {
8181 break;
8182 }
8183 } else {
f9fd40eb 8184 imm = arm_ldl_code(env, env->regs[15] - 4, arm_sctlr_b(env))
904c04de
PM
8185 & 0xffffff;
8186 if (imm == 0x123456) {
8187 break;
8188 }
8189 }
8190 return false;
8191 case EXCP_BKPT:
8192 /* See if this is a semihosting syscall. */
8193 if (env->thumb) {
f9fd40eb 8194 imm = arm_lduw_code(env, env->regs[15], arm_sctlr_b(env))
904c04de
PM
8195 & 0xff;
8196 if (imm == 0xab) {
8197 env->regs[15] += 2;
8198 break;
8199 }
8200 }
8201 return false;
8202 default:
8203 return false;
8204 }
8205
8206 qemu_log_mask(CPU_LOG_INT,
8207 "...handling as semihosting call 0x%x\n",
8208 env->regs[0]);
8209 env->regs[0] = do_arm_semihosting(env);
8210 return true;
8211 }
8212}
8213
966f758c
PM
8214/* Handle a CPU exception for A and R profile CPUs.
8215 * Do any appropriate logging, handle PSCI calls, and then hand off
8216 * to the AArch64-entry or AArch32-entry function depending on the
8217 * target exception level's register width.
8218 */
8219void arm_cpu_do_interrupt(CPUState *cs)
8220{
8221 ARMCPU *cpu = ARM_CPU(cs);
8222 CPUARMState *env = &cpu->env;
8223 unsigned int new_el = env->exception.target_el;
8224
531c60a9 8225 assert(!arm_feature(env, ARM_FEATURE_M));
966f758c
PM
8226
8227 arm_log_exception(cs->exception_index);
8228 qemu_log_mask(CPU_LOG_INT, "...from EL%d to EL%d\n", arm_current_el(env),
8229 new_el);
8230 if (qemu_loglevel_mask(CPU_LOG_INT)
8231 && !excp_is_internal(cs->exception_index)) {
6568da45 8232 qemu_log_mask(CPU_LOG_INT, "...with ESR 0x%x/0x%" PRIx32 "\n",
966f758c
PM
8233 env->exception.syndrome >> ARM_EL_EC_SHIFT,
8234 env->exception.syndrome);
8235 }
8236
8237 if (arm_is_psci_call(cpu, cs->exception_index)) {
8238 arm_handle_psci_call(cpu);
8239 qemu_log_mask(CPU_LOG_INT, "...handled as PSCI call\n");
8240 return;
8241 }
8242
904c04de
PM
8243 /* Semihosting semantics depend on the register width of the
8244 * code that caused the exception, not the target exception level,
8245 * so must be handled here.
966f758c 8246 */
904c04de
PM
8247 if (check_for_semihosting(cs)) {
8248 return;
8249 }
8250
8251 assert(!excp_is_internal(cs->exception_index));
8252 if (arm_el_is_aa64(env, new_el)) {
966f758c
PM
8253 arm_cpu_do_interrupt_aarch64(cs);
8254 } else {
8255 arm_cpu_do_interrupt_aarch32(cs);
8256 }
f3a9b694 8257
8d04fb55
JK
8258 /* Hooks may change global state so BQL should be held, also the
8259 * BQL needs to be held for any modification of
8260 * cs->interrupt_request.
8261 */
8262 g_assert(qemu_mutex_iothread_locked());
8263
bd7d00fc
PM
8264 arm_call_el_change_hook(cpu);
8265
f3a9b694
PM
8266 if (!kvm_enabled()) {
8267 cs->interrupt_request |= CPU_INTERRUPT_EXITTB;
8268 }
8269}
0480f69a
PM
8270
8271/* Return the exception level which controls this address translation regime */
8272static inline uint32_t regime_el(CPUARMState *env, ARMMMUIdx mmu_idx)
8273{
8274 switch (mmu_idx) {
8275 case ARMMMUIdx_S2NS:
8276 case ARMMMUIdx_S1E2:
8277 return 2;
8278 case ARMMMUIdx_S1E3:
8279 return 3;
8280 case ARMMMUIdx_S1SE0:
8281 return arm_el_is_aa64(env, 3) ? 1 : 3;
8282 case ARMMMUIdx_S1SE1:
8283 case ARMMMUIdx_S1NSE0:
8284 case ARMMMUIdx_S1NSE1:
62593718
PM
8285 case ARMMMUIdx_MPrivNegPri:
8286 case ARMMMUIdx_MUserNegPri:
e7b921c2
PM
8287 case ARMMMUIdx_MPriv:
8288 case ARMMMUIdx_MUser:
62593718
PM
8289 case ARMMMUIdx_MSPrivNegPri:
8290 case ARMMMUIdx_MSUserNegPri:
66787c78 8291 case ARMMMUIdx_MSPriv:
66787c78 8292 case ARMMMUIdx_MSUser:
0480f69a
PM
8293 return 1;
8294 default:
8295 g_assert_not_reached();
8296 }
8297}
8298
8299/* Return the SCTLR value which controls this address translation regime */
8300static inline uint32_t regime_sctlr(CPUARMState *env, ARMMMUIdx mmu_idx)
8301{
8302 return env->cp15.sctlr_el[regime_el(env, mmu_idx)];
8303}
8304
8305/* Return true if the specified stage of address translation is disabled */
8306static inline bool regime_translation_disabled(CPUARMState *env,
8307 ARMMMUIdx mmu_idx)
8308{
29c483a5 8309 if (arm_feature(env, ARM_FEATURE_M)) {
ecf5e8ea 8310 switch (env->v7m.mpu_ctrl[regime_is_secure(env, mmu_idx)] &
3bef7012
PM
8311 (R_V7M_MPU_CTRL_ENABLE_MASK | R_V7M_MPU_CTRL_HFNMIENA_MASK)) {
8312 case R_V7M_MPU_CTRL_ENABLE_MASK:
8313 /* Enabled, but not for HardFault and NMI */
62593718 8314 return mmu_idx & ARM_MMU_IDX_M_NEGPRI;
3bef7012
PM
8315 case R_V7M_MPU_CTRL_ENABLE_MASK | R_V7M_MPU_CTRL_HFNMIENA_MASK:
8316 /* Enabled for all cases */
8317 return false;
8318 case 0:
8319 default:
8320 /* HFNMIENA set and ENABLE clear is UNPREDICTABLE, but
8321 * we warned about that in armv7m_nvic.c when the guest set it.
8322 */
8323 return true;
8324 }
29c483a5
MD
8325 }
8326
0480f69a
PM
8327 if (mmu_idx == ARMMMUIdx_S2NS) {
8328 return (env->cp15.hcr_el2 & HCR_VM) == 0;
8329 }
8330 return (regime_sctlr(env, mmu_idx) & SCTLR_M) == 0;
8331}
8332
73462ddd
PC
8333static inline bool regime_translation_big_endian(CPUARMState *env,
8334 ARMMMUIdx mmu_idx)
8335{
8336 return (regime_sctlr(env, mmu_idx) & SCTLR_EE) != 0;
8337}
8338
0480f69a
PM
8339/* Return the TCR controlling this translation regime */
8340static inline TCR *regime_tcr(CPUARMState *env, ARMMMUIdx mmu_idx)
8341{
8342 if (mmu_idx == ARMMMUIdx_S2NS) {
68e9c2fe 8343 return &env->cp15.vtcr_el2;
0480f69a
PM
8344 }
8345 return &env->cp15.tcr_el[regime_el(env, mmu_idx)];
8346}
8347
8bd5c820
PM
8348/* Convert a possible stage1+2 MMU index into the appropriate
8349 * stage 1 MMU index
8350 */
8351static inline ARMMMUIdx stage_1_mmu_idx(ARMMMUIdx mmu_idx)
8352{
8353 if (mmu_idx == ARMMMUIdx_S12NSE0 || mmu_idx == ARMMMUIdx_S12NSE1) {
8354 mmu_idx += (ARMMMUIdx_S1NSE0 - ARMMMUIdx_S12NSE0);
8355 }
8356 return mmu_idx;
8357}
8358
86fb3fa4
TH
8359/* Returns TBI0 value for current regime el */
8360uint32_t arm_regime_tbi0(CPUARMState *env, ARMMMUIdx mmu_idx)
8361{
8362 TCR *tcr;
8363 uint32_t el;
8364
8365 /* For EL0 and EL1, TBI is controlled by stage 1's TCR, so convert
8bd5c820
PM
8366 * a stage 1+2 mmu index into the appropriate stage 1 mmu index.
8367 */
8368 mmu_idx = stage_1_mmu_idx(mmu_idx);
86fb3fa4
TH
8369
8370 tcr = regime_tcr(env, mmu_idx);
8371 el = regime_el(env, mmu_idx);
8372
8373 if (el > 1) {
8374 return extract64(tcr->raw_tcr, 20, 1);
8375 } else {
8376 return extract64(tcr->raw_tcr, 37, 1);
8377 }
8378}
8379
8380/* Returns TBI1 value for current regime el */
8381uint32_t arm_regime_tbi1(CPUARMState *env, ARMMMUIdx mmu_idx)
8382{
8383 TCR *tcr;
8384 uint32_t el;
8385
8386 /* For EL0 and EL1, TBI is controlled by stage 1's TCR, so convert
8bd5c820
PM
8387 * a stage 1+2 mmu index into the appropriate stage 1 mmu index.
8388 */
8389 mmu_idx = stage_1_mmu_idx(mmu_idx);
86fb3fa4
TH
8390
8391 tcr = regime_tcr(env, mmu_idx);
8392 el = regime_el(env, mmu_idx);
8393
8394 if (el > 1) {
8395 return 0;
8396 } else {
8397 return extract64(tcr->raw_tcr, 38, 1);
8398 }
8399}
8400
aef878be
GB
8401/* Return the TTBR associated with this translation regime */
8402static inline uint64_t regime_ttbr(CPUARMState *env, ARMMMUIdx mmu_idx,
8403 int ttbrn)
8404{
8405 if (mmu_idx == ARMMMUIdx_S2NS) {
b698e9cf 8406 return env->cp15.vttbr_el2;
aef878be
GB
8407 }
8408 if (ttbrn == 0) {
8409 return env->cp15.ttbr0_el[regime_el(env, mmu_idx)];
8410 } else {
8411 return env->cp15.ttbr1_el[regime_el(env, mmu_idx)];
8412 }
8413}
8414
0480f69a
PM
8415/* Return true if the translation regime is using LPAE format page tables */
8416static inline bool regime_using_lpae_format(CPUARMState *env,
8417 ARMMMUIdx mmu_idx)
8418{
8419 int el = regime_el(env, mmu_idx);
8420 if (el == 2 || arm_el_is_aa64(env, el)) {
8421 return true;
8422 }
8423 if (arm_feature(env, ARM_FEATURE_LPAE)
8424 && (regime_tcr(env, mmu_idx)->raw_tcr & TTBCR_EAE)) {
8425 return true;
8426 }
8427 return false;
8428}
8429
deb2db99
AR
8430/* Returns true if the stage 1 translation regime is using LPAE format page
8431 * tables. Used when raising alignment exceptions, whose FSR changes depending
8432 * on whether the long or short descriptor format is in use. */
8433bool arm_s1_regime_using_lpae_format(CPUARMState *env, ARMMMUIdx mmu_idx)
30901475 8434{
8bd5c820 8435 mmu_idx = stage_1_mmu_idx(mmu_idx);
deb2db99 8436
30901475
AB
8437 return regime_using_lpae_format(env, mmu_idx);
8438}
8439
0480f69a
PM
8440static inline bool regime_is_user(CPUARMState *env, ARMMMUIdx mmu_idx)
8441{
8442 switch (mmu_idx) {
8443 case ARMMMUIdx_S1SE0:
8444 case ARMMMUIdx_S1NSE0:
e7b921c2 8445 case ARMMMUIdx_MUser:
871bec7c 8446 case ARMMMUIdx_MSUser:
62593718
PM
8447 case ARMMMUIdx_MUserNegPri:
8448 case ARMMMUIdx_MSUserNegPri:
0480f69a
PM
8449 return true;
8450 default:
8451 return false;
8452 case ARMMMUIdx_S12NSE0:
8453 case ARMMMUIdx_S12NSE1:
8454 g_assert_not_reached();
8455 }
8456}
8457
0fbf5238
AJ
8458/* Translate section/page access permissions to page
8459 * R/W protection flags
d76951b6
AJ
8460 *
8461 * @env: CPUARMState
8462 * @mmu_idx: MMU index indicating required translation regime
8463 * @ap: The 3-bit access permissions (AP[2:0])
8464 * @domain_prot: The 2-bit domain access permissions
0fbf5238
AJ
8465 */
8466static inline int ap_to_rw_prot(CPUARMState *env, ARMMMUIdx mmu_idx,
8467 int ap, int domain_prot)
8468{
554b0b09
PM
8469 bool is_user = regime_is_user(env, mmu_idx);
8470
8471 if (domain_prot == 3) {
8472 return PAGE_READ | PAGE_WRITE;
8473 }
8474
554b0b09
PM
8475 switch (ap) {
8476 case 0:
8477 if (arm_feature(env, ARM_FEATURE_V7)) {
8478 return 0;
8479 }
554b0b09
PM
8480 switch (regime_sctlr(env, mmu_idx) & (SCTLR_S | SCTLR_R)) {
8481 case SCTLR_S:
8482 return is_user ? 0 : PAGE_READ;
8483 case SCTLR_R:
8484 return PAGE_READ;
8485 default:
8486 return 0;
8487 }
8488 case 1:
8489 return is_user ? 0 : PAGE_READ | PAGE_WRITE;
8490 case 2:
87c3d486 8491 if (is_user) {
0fbf5238 8492 return PAGE_READ;
87c3d486 8493 } else {
554b0b09 8494 return PAGE_READ | PAGE_WRITE;
87c3d486 8495 }
554b0b09
PM
8496 case 3:
8497 return PAGE_READ | PAGE_WRITE;
8498 case 4: /* Reserved. */
8499 return 0;
8500 case 5:
0fbf5238 8501 return is_user ? 0 : PAGE_READ;
554b0b09 8502 case 6:
0fbf5238 8503 return PAGE_READ;
554b0b09 8504 case 7:
87c3d486 8505 if (!arm_feature(env, ARM_FEATURE_V6K)) {
554b0b09 8506 return 0;
87c3d486 8507 }
0fbf5238 8508 return PAGE_READ;
554b0b09 8509 default:
0fbf5238 8510 g_assert_not_reached();
554b0b09 8511 }
b5ff1b31
FB
8512}
8513
d76951b6
AJ
8514/* Translate section/page access permissions to page
8515 * R/W protection flags.
8516 *
d76951b6 8517 * @ap: The 2-bit simple AP (AP[2:1])
d8e052b3 8518 * @is_user: TRUE if accessing from PL0
d76951b6 8519 */
d8e052b3 8520static inline int simple_ap_to_rw_prot_is_user(int ap, bool is_user)
d76951b6 8521{
d76951b6
AJ
8522 switch (ap) {
8523 case 0:
8524 return is_user ? 0 : PAGE_READ | PAGE_WRITE;
8525 case 1:
8526 return PAGE_READ | PAGE_WRITE;
8527 case 2:
8528 return is_user ? 0 : PAGE_READ;
8529 case 3:
8530 return PAGE_READ;
8531 default:
8532 g_assert_not_reached();
8533 }
8534}
8535
d8e052b3
AJ
8536static inline int
8537simple_ap_to_rw_prot(CPUARMState *env, ARMMMUIdx mmu_idx, int ap)
8538{
8539 return simple_ap_to_rw_prot_is_user(ap, regime_is_user(env, mmu_idx));
8540}
8541
6ab1a5ee
EI
8542/* Translate S2 section/page access permissions to protection flags
8543 *
8544 * @env: CPUARMState
8545 * @s2ap: The 2-bit stage2 access permissions (S2AP)
8546 * @xn: XN (execute-never) bit
8547 */
8548static int get_S2prot(CPUARMState *env, int s2ap, int xn)
8549{
8550 int prot = 0;
8551
8552 if (s2ap & 1) {
8553 prot |= PAGE_READ;
8554 }
8555 if (s2ap & 2) {
8556 prot |= PAGE_WRITE;
8557 }
8558 if (!xn) {
dfda6837
SS
8559 if (arm_el_is_aa64(env, 2) || prot & PAGE_READ) {
8560 prot |= PAGE_EXEC;
8561 }
6ab1a5ee
EI
8562 }
8563 return prot;
8564}
8565
d8e052b3
AJ
8566/* Translate section/page access permissions to protection flags
8567 *
8568 * @env: CPUARMState
8569 * @mmu_idx: MMU index indicating required translation regime
8570 * @is_aa64: TRUE if AArch64
8571 * @ap: The 2-bit simple AP (AP[2:1])
8572 * @ns: NS (non-secure) bit
8573 * @xn: XN (execute-never) bit
8574 * @pxn: PXN (privileged execute-never) bit
8575 */
8576static int get_S1prot(CPUARMState *env, ARMMMUIdx mmu_idx, bool is_aa64,
8577 int ap, int ns, int xn, int pxn)
8578{
8579 bool is_user = regime_is_user(env, mmu_idx);
8580 int prot_rw, user_rw;
8581 bool have_wxn;
8582 int wxn = 0;
8583
8584 assert(mmu_idx != ARMMMUIdx_S2NS);
8585
8586 user_rw = simple_ap_to_rw_prot_is_user(ap, true);
8587 if (is_user) {
8588 prot_rw = user_rw;
8589 } else {
8590 prot_rw = simple_ap_to_rw_prot_is_user(ap, false);
8591 }
8592
8593 if (ns && arm_is_secure(env) && (env->cp15.scr_el3 & SCR_SIF)) {
8594 return prot_rw;
8595 }
8596
8597 /* TODO have_wxn should be replaced with
8598 * ARM_FEATURE_V8 || (ARM_FEATURE_V7 && ARM_FEATURE_EL2)
8599 * when ARM_FEATURE_EL2 starts getting set. For now we assume all LPAE
8600 * compatible processors have EL2, which is required for [U]WXN.
8601 */
8602 have_wxn = arm_feature(env, ARM_FEATURE_LPAE);
8603
8604 if (have_wxn) {
8605 wxn = regime_sctlr(env, mmu_idx) & SCTLR_WXN;
8606 }
8607
8608 if (is_aa64) {
8609 switch (regime_el(env, mmu_idx)) {
8610 case 1:
8611 if (!is_user) {
8612 xn = pxn || (user_rw & PAGE_WRITE);
8613 }
8614 break;
8615 case 2:
8616 case 3:
8617 break;
8618 }
8619 } else if (arm_feature(env, ARM_FEATURE_V7)) {
8620 switch (regime_el(env, mmu_idx)) {
8621 case 1:
8622 case 3:
8623 if (is_user) {
8624 xn = xn || !(user_rw & PAGE_READ);
8625 } else {
8626 int uwxn = 0;
8627 if (have_wxn) {
8628 uwxn = regime_sctlr(env, mmu_idx) & SCTLR_UWXN;
8629 }
8630 xn = xn || !(prot_rw & PAGE_READ) || pxn ||
8631 (uwxn && (user_rw & PAGE_WRITE));
8632 }
8633 break;
8634 case 2:
8635 break;
8636 }
8637 } else {
8638 xn = wxn = 0;
8639 }
8640
8641 if (xn || (wxn && (prot_rw & PAGE_WRITE))) {
8642 return prot_rw;
8643 }
8644 return prot_rw | PAGE_EXEC;
8645}
8646
0480f69a
PM
8647static bool get_level1_table_address(CPUARMState *env, ARMMMUIdx mmu_idx,
8648 uint32_t *table, uint32_t address)
b2fa1797 8649{
0480f69a 8650 /* Note that we can only get here for an AArch32 PL0/PL1 lookup */
0480f69a 8651 TCR *tcr = regime_tcr(env, mmu_idx);
11f136ee 8652
11f136ee
FA
8653 if (address & tcr->mask) {
8654 if (tcr->raw_tcr & TTBCR_PD1) {
e389be16
FA
8655 /* Translation table walk disabled for TTBR1 */
8656 return false;
8657 }
aef878be 8658 *table = regime_ttbr(env, mmu_idx, 1) & 0xffffc000;
e389be16 8659 } else {
11f136ee 8660 if (tcr->raw_tcr & TTBCR_PD0) {
e389be16
FA
8661 /* Translation table walk disabled for TTBR0 */
8662 return false;
8663 }
aef878be 8664 *table = regime_ttbr(env, mmu_idx, 0) & tcr->base_mask;
e389be16
FA
8665 }
8666 *table |= (address >> 18) & 0x3ffc;
8667 return true;
b2fa1797
PB
8668}
8669
37785977
EI
8670/* Translate a S1 pagetable walk through S2 if needed. */
8671static hwaddr S1_ptw_translate(CPUARMState *env, ARMMMUIdx mmu_idx,
8672 hwaddr addr, MemTxAttrs txattrs,
37785977
EI
8673 ARMMMUFaultInfo *fi)
8674{
8675 if ((mmu_idx == ARMMMUIdx_S1NSE0 || mmu_idx == ARMMMUIdx_S1NSE1) &&
8676 !regime_translation_disabled(env, ARMMMUIdx_S2NS)) {
8677 target_ulong s2size;
8678 hwaddr s2pa;
8679 int s2prot;
8680 int ret;
8681
8682 ret = get_phys_addr_lpae(env, addr, 0, ARMMMUIdx_S2NS, &s2pa,
da909b2c 8683 &txattrs, &s2prot, &s2size, fi, NULL);
37785977 8684 if (ret) {
3b39d734 8685 assert(fi->type != ARMFault_None);
37785977
EI
8686 fi->s2addr = addr;
8687 fi->stage2 = true;
8688 fi->s1ptw = true;
8689 return ~0;
8690 }
8691 addr = s2pa;
8692 }
8693 return addr;
8694}
8695
ebca90e4
PM
8696/* All loads done in the course of a page table walk go through here.
8697 * TODO: rather than ignoring errors from physical memory reads (which
8698 * are external aborts in ARM terminology) we should propagate this
8699 * error out so that we can turn it into a Data Abort if this walk
8700 * was being done for a CPU load/store or an address translation instruction
8701 * (but not if it was for a debug access).
8702 */
a614e698 8703static uint32_t arm_ldl_ptw(CPUState *cs, hwaddr addr, bool is_secure,
3795a6de 8704 ARMMMUIdx mmu_idx, ARMMMUFaultInfo *fi)
ebca90e4 8705{
a614e698
EI
8706 ARMCPU *cpu = ARM_CPU(cs);
8707 CPUARMState *env = &cpu->env;
ebca90e4 8708 MemTxAttrs attrs = {};
3b39d734 8709 MemTxResult result = MEMTX_OK;
5ce4ff65 8710 AddressSpace *as;
3b39d734 8711 uint32_t data;
ebca90e4
PM
8712
8713 attrs.secure = is_secure;
5ce4ff65 8714 as = arm_addressspace(cs, attrs);
3795a6de 8715 addr = S1_ptw_translate(env, mmu_idx, addr, attrs, fi);
a614e698
EI
8716 if (fi->s1ptw) {
8717 return 0;
8718 }
73462ddd 8719 if (regime_translation_big_endian(env, mmu_idx)) {
3b39d734 8720 data = address_space_ldl_be(as, addr, attrs, &result);
73462ddd 8721 } else {
3b39d734 8722 data = address_space_ldl_le(as, addr, attrs, &result);
73462ddd 8723 }
3b39d734
PM
8724 if (result == MEMTX_OK) {
8725 return data;
8726 }
8727 fi->type = ARMFault_SyncExternalOnWalk;
8728 fi->ea = arm_extabort_type(result);
8729 return 0;
ebca90e4
PM
8730}
8731
37785977 8732static uint64_t arm_ldq_ptw(CPUState *cs, hwaddr addr, bool is_secure,
3795a6de 8733 ARMMMUIdx mmu_idx, ARMMMUFaultInfo *fi)
ebca90e4 8734{
37785977
EI
8735 ARMCPU *cpu = ARM_CPU(cs);
8736 CPUARMState *env = &cpu->env;
ebca90e4 8737 MemTxAttrs attrs = {};
3b39d734 8738 MemTxResult result = MEMTX_OK;
5ce4ff65 8739 AddressSpace *as;
9aea1ea3 8740 uint64_t data;
ebca90e4
PM
8741
8742 attrs.secure = is_secure;
5ce4ff65 8743 as = arm_addressspace(cs, attrs);
3795a6de 8744 addr = S1_ptw_translate(env, mmu_idx, addr, attrs, fi);
37785977
EI
8745 if (fi->s1ptw) {
8746 return 0;
8747 }
73462ddd 8748 if (regime_translation_big_endian(env, mmu_idx)) {
3b39d734 8749 data = address_space_ldq_be(as, addr, attrs, &result);
73462ddd 8750 } else {
3b39d734
PM
8751 data = address_space_ldq_le(as, addr, attrs, &result);
8752 }
8753 if (result == MEMTX_OK) {
8754 return data;
73462ddd 8755 }
3b39d734
PM
8756 fi->type = ARMFault_SyncExternalOnWalk;
8757 fi->ea = arm_extabort_type(result);
8758 return 0;
ebca90e4
PM
8759}
8760
b7cc4e82 8761static bool get_phys_addr_v5(CPUARMState *env, uint32_t address,
03ae85f8 8762 MMUAccessType access_type, ARMMMUIdx mmu_idx,
b7cc4e82 8763 hwaddr *phys_ptr, int *prot,
f989983e 8764 target_ulong *page_size,
e14b5a23 8765 ARMMMUFaultInfo *fi)
b5ff1b31 8766{
70d74660 8767 CPUState *cs = CPU(arm_env_get_cpu(env));
f989983e 8768 int level = 1;
b5ff1b31
FB
8769 uint32_t table;
8770 uint32_t desc;
8771 int type;
8772 int ap;
e389be16 8773 int domain = 0;
dd4ebc2e 8774 int domain_prot;
a8170e5e 8775 hwaddr phys_addr;
0480f69a 8776 uint32_t dacr;
b5ff1b31 8777
9ee6e8bb
PB
8778 /* Pagetable walk. */
8779 /* Lookup l1 descriptor. */
0480f69a 8780 if (!get_level1_table_address(env, mmu_idx, &table, address)) {
e389be16 8781 /* Section translation fault if page walk is disabled by PD0 or PD1 */
f989983e 8782 fi->type = ARMFault_Translation;
e389be16
FA
8783 goto do_fault;
8784 }
a614e698 8785 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
3795a6de 8786 mmu_idx, fi);
3b39d734
PM
8787 if (fi->type != ARMFault_None) {
8788 goto do_fault;
8789 }
9ee6e8bb 8790 type = (desc & 3);
dd4ebc2e 8791 domain = (desc >> 5) & 0x0f;
0480f69a
PM
8792 if (regime_el(env, mmu_idx) == 1) {
8793 dacr = env->cp15.dacr_ns;
8794 } else {
8795 dacr = env->cp15.dacr_s;
8796 }
8797 domain_prot = (dacr >> (domain * 2)) & 3;
9ee6e8bb 8798 if (type == 0) {
601d70b9 8799 /* Section translation fault. */
f989983e 8800 fi->type = ARMFault_Translation;
9ee6e8bb
PB
8801 goto do_fault;
8802 }
f989983e
PM
8803 if (type != 2) {
8804 level = 2;
8805 }
dd4ebc2e 8806 if (domain_prot == 0 || domain_prot == 2) {
f989983e 8807 fi->type = ARMFault_Domain;
9ee6e8bb
PB
8808 goto do_fault;
8809 }
8810 if (type == 2) {
8811 /* 1Mb section. */
8812 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
8813 ap = (desc >> 10) & 3;
d4c430a8 8814 *page_size = 1024 * 1024;
9ee6e8bb
PB
8815 } else {
8816 /* Lookup l2 entry. */
554b0b09
PM
8817 if (type == 1) {
8818 /* Coarse pagetable. */
8819 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
8820 } else {
8821 /* Fine pagetable. */
8822 table = (desc & 0xfffff000) | ((address >> 8) & 0xffc);
8823 }
a614e698 8824 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
3795a6de 8825 mmu_idx, fi);
3b39d734
PM
8826 if (fi->type != ARMFault_None) {
8827 goto do_fault;
8828 }
9ee6e8bb
PB
8829 switch (desc & 3) {
8830 case 0: /* Page translation fault. */
f989983e 8831 fi->type = ARMFault_Translation;
9ee6e8bb
PB
8832 goto do_fault;
8833 case 1: /* 64k page. */
8834 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
8835 ap = (desc >> (4 + ((address >> 13) & 6))) & 3;
d4c430a8 8836 *page_size = 0x10000;
ce819861 8837 break;
9ee6e8bb
PB
8838 case 2: /* 4k page. */
8839 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
c10f7fc3 8840 ap = (desc >> (4 + ((address >> 9) & 6))) & 3;
d4c430a8 8841 *page_size = 0x1000;
ce819861 8842 break;
fc1891c7 8843 case 3: /* 1k page, or ARMv6/XScale "extended small (4k) page" */
554b0b09 8844 if (type == 1) {
fc1891c7
PM
8845 /* ARMv6/XScale extended small page format */
8846 if (arm_feature(env, ARM_FEATURE_XSCALE)
8847 || arm_feature(env, ARM_FEATURE_V6)) {
554b0b09 8848 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
fc1891c7 8849 *page_size = 0x1000;
554b0b09 8850 } else {
fc1891c7
PM
8851 /* UNPREDICTABLE in ARMv5; we choose to take a
8852 * page translation fault.
8853 */
f989983e 8854 fi->type = ARMFault_Translation;
554b0b09
PM
8855 goto do_fault;
8856 }
8857 } else {
8858 phys_addr = (desc & 0xfffffc00) | (address & 0x3ff);
fc1891c7 8859 *page_size = 0x400;
554b0b09 8860 }
9ee6e8bb 8861 ap = (desc >> 4) & 3;
ce819861
PB
8862 break;
8863 default:
9ee6e8bb
PB
8864 /* Never happens, but compiler isn't smart enough to tell. */
8865 abort();
ce819861 8866 }
9ee6e8bb 8867 }
0fbf5238
AJ
8868 *prot = ap_to_rw_prot(env, mmu_idx, ap, domain_prot);
8869 *prot |= *prot ? PAGE_EXEC : 0;
8870 if (!(*prot & (1 << access_type))) {
9ee6e8bb 8871 /* Access permission fault. */
f989983e 8872 fi->type = ARMFault_Permission;
9ee6e8bb
PB
8873 goto do_fault;
8874 }
8875 *phys_ptr = phys_addr;
b7cc4e82 8876 return false;
9ee6e8bb 8877do_fault:
f989983e
PM
8878 fi->domain = domain;
8879 fi->level = level;
b7cc4e82 8880 return true;
9ee6e8bb
PB
8881}
8882
b7cc4e82 8883static bool get_phys_addr_v6(CPUARMState *env, uint32_t address,
03ae85f8 8884 MMUAccessType access_type, ARMMMUIdx mmu_idx,
b7cc4e82 8885 hwaddr *phys_ptr, MemTxAttrs *attrs, int *prot,
f06cf243 8886 target_ulong *page_size, ARMMMUFaultInfo *fi)
9ee6e8bb 8887{
70d74660 8888 CPUState *cs = CPU(arm_env_get_cpu(env));
f06cf243 8889 int level = 1;
9ee6e8bb
PB
8890 uint32_t table;
8891 uint32_t desc;
8892 uint32_t xn;
de9b05b8 8893 uint32_t pxn = 0;
9ee6e8bb
PB
8894 int type;
8895 int ap;
de9b05b8 8896 int domain = 0;
dd4ebc2e 8897 int domain_prot;
a8170e5e 8898 hwaddr phys_addr;
0480f69a 8899 uint32_t dacr;
8bf5b6a9 8900 bool ns;
9ee6e8bb
PB
8901
8902 /* Pagetable walk. */
8903 /* Lookup l1 descriptor. */
0480f69a 8904 if (!get_level1_table_address(env, mmu_idx, &table, address)) {
e389be16 8905 /* Section translation fault if page walk is disabled by PD0 or PD1 */
f06cf243 8906 fi->type = ARMFault_Translation;
e389be16
FA
8907 goto do_fault;
8908 }
a614e698 8909 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
3795a6de 8910 mmu_idx, fi);
3b39d734
PM
8911 if (fi->type != ARMFault_None) {
8912 goto do_fault;
8913 }
9ee6e8bb 8914 type = (desc & 3);
de9b05b8
PM
8915 if (type == 0 || (type == 3 && !arm_feature(env, ARM_FEATURE_PXN))) {
8916 /* Section translation fault, or attempt to use the encoding
8917 * which is Reserved on implementations without PXN.
8918 */
f06cf243 8919 fi->type = ARMFault_Translation;
9ee6e8bb 8920 goto do_fault;
de9b05b8
PM
8921 }
8922 if ((type == 1) || !(desc & (1 << 18))) {
8923 /* Page or Section. */
dd4ebc2e 8924 domain = (desc >> 5) & 0x0f;
9ee6e8bb 8925 }
0480f69a
PM
8926 if (regime_el(env, mmu_idx) == 1) {
8927 dacr = env->cp15.dacr_ns;
8928 } else {
8929 dacr = env->cp15.dacr_s;
8930 }
f06cf243
PM
8931 if (type == 1) {
8932 level = 2;
8933 }
0480f69a 8934 domain_prot = (dacr >> (domain * 2)) & 3;
dd4ebc2e 8935 if (domain_prot == 0 || domain_prot == 2) {
f06cf243
PM
8936 /* Section or Page domain fault */
8937 fi->type = ARMFault_Domain;
9ee6e8bb
PB
8938 goto do_fault;
8939 }
de9b05b8 8940 if (type != 1) {
9ee6e8bb
PB
8941 if (desc & (1 << 18)) {
8942 /* Supersection. */
8943 phys_addr = (desc & 0xff000000) | (address & 0x00ffffff);
4e42a6ca
SF
8944 phys_addr |= (uint64_t)extract32(desc, 20, 4) << 32;
8945 phys_addr |= (uint64_t)extract32(desc, 5, 4) << 36;
d4c430a8 8946 *page_size = 0x1000000;
b5ff1b31 8947 } else {
9ee6e8bb
PB
8948 /* Section. */
8949 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
d4c430a8 8950 *page_size = 0x100000;
b5ff1b31 8951 }
9ee6e8bb
PB
8952 ap = ((desc >> 10) & 3) | ((desc >> 13) & 4);
8953 xn = desc & (1 << 4);
de9b05b8 8954 pxn = desc & 1;
8bf5b6a9 8955 ns = extract32(desc, 19, 1);
9ee6e8bb 8956 } else {
de9b05b8
PM
8957 if (arm_feature(env, ARM_FEATURE_PXN)) {
8958 pxn = (desc >> 2) & 1;
8959 }
8bf5b6a9 8960 ns = extract32(desc, 3, 1);
9ee6e8bb
PB
8961 /* Lookup l2 entry. */
8962 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
a614e698 8963 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
3795a6de 8964 mmu_idx, fi);
3b39d734
PM
8965 if (fi->type != ARMFault_None) {
8966 goto do_fault;
8967 }
9ee6e8bb
PB
8968 ap = ((desc >> 4) & 3) | ((desc >> 7) & 4);
8969 switch (desc & 3) {
8970 case 0: /* Page translation fault. */
f06cf243 8971 fi->type = ARMFault_Translation;
b5ff1b31 8972 goto do_fault;
9ee6e8bb
PB
8973 case 1: /* 64k page. */
8974 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
8975 xn = desc & (1 << 15);
d4c430a8 8976 *page_size = 0x10000;
9ee6e8bb
PB
8977 break;
8978 case 2: case 3: /* 4k page. */
8979 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
8980 xn = desc & 1;
d4c430a8 8981 *page_size = 0x1000;
9ee6e8bb
PB
8982 break;
8983 default:
8984 /* Never happens, but compiler isn't smart enough to tell. */
8985 abort();
b5ff1b31 8986 }
9ee6e8bb 8987 }
dd4ebc2e 8988 if (domain_prot == 3) {
c0034328
JR
8989 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
8990 } else {
0480f69a 8991 if (pxn && !regime_is_user(env, mmu_idx)) {
de9b05b8
PM
8992 xn = 1;
8993 }
f06cf243
PM
8994 if (xn && access_type == MMU_INST_FETCH) {
8995 fi->type = ARMFault_Permission;
c0034328 8996 goto do_fault;
f06cf243 8997 }
9ee6e8bb 8998
d76951b6
AJ
8999 if (arm_feature(env, ARM_FEATURE_V6K) &&
9000 (regime_sctlr(env, mmu_idx) & SCTLR_AFE)) {
9001 /* The simplified model uses AP[0] as an access control bit. */
9002 if ((ap & 1) == 0) {
9003 /* Access flag fault. */
f06cf243 9004 fi->type = ARMFault_AccessFlag;
d76951b6
AJ
9005 goto do_fault;
9006 }
9007 *prot = simple_ap_to_rw_prot(env, mmu_idx, ap >> 1);
9008 } else {
9009 *prot = ap_to_rw_prot(env, mmu_idx, ap, domain_prot);
c0034328 9010 }
0fbf5238
AJ
9011 if (*prot && !xn) {
9012 *prot |= PAGE_EXEC;
9013 }
9014 if (!(*prot & (1 << access_type))) {
c0034328 9015 /* Access permission fault. */
f06cf243 9016 fi->type = ARMFault_Permission;
c0034328
JR
9017 goto do_fault;
9018 }
3ad493fc 9019 }
8bf5b6a9
PM
9020 if (ns) {
9021 /* The NS bit will (as required by the architecture) have no effect if
9022 * the CPU doesn't support TZ or this is a non-secure translation
9023 * regime, because the attribute will already be non-secure.
9024 */
9025 attrs->secure = false;
9026 }
9ee6e8bb 9027 *phys_ptr = phys_addr;
b7cc4e82 9028 return false;
b5ff1b31 9029do_fault:
f06cf243
PM
9030 fi->domain = domain;
9031 fi->level = level;
b7cc4e82 9032 return true;
b5ff1b31
FB
9033}
9034
1853d5a9 9035/*
a0e966c9 9036 * check_s2_mmu_setup
1853d5a9
EI
9037 * @cpu: ARMCPU
9038 * @is_aa64: True if the translation regime is in AArch64 state
9039 * @startlevel: Suggested starting level
9040 * @inputsize: Bitsize of IPAs
9041 * @stride: Page-table stride (See the ARM ARM)
9042 *
a0e966c9
EI
9043 * Returns true if the suggested S2 translation parameters are OK and
9044 * false otherwise.
1853d5a9 9045 */
a0e966c9
EI
9046static bool check_s2_mmu_setup(ARMCPU *cpu, bool is_aa64, int level,
9047 int inputsize, int stride)
1853d5a9 9048{
98d68ec2
EI
9049 const int grainsize = stride + 3;
9050 int startsizecheck;
9051
1853d5a9
EI
9052 /* Negative levels are never allowed. */
9053 if (level < 0) {
9054 return false;
9055 }
9056
98d68ec2
EI
9057 startsizecheck = inputsize - ((3 - level) * stride + grainsize);
9058 if (startsizecheck < 1 || startsizecheck > stride + 4) {
9059 return false;
9060 }
9061
1853d5a9 9062 if (is_aa64) {
3526423e 9063 CPUARMState *env = &cpu->env;
1853d5a9
EI
9064 unsigned int pamax = arm_pamax(cpu);
9065
9066 switch (stride) {
9067 case 13: /* 64KB Pages. */
9068 if (level == 0 || (level == 1 && pamax <= 42)) {
9069 return false;
9070 }
9071 break;
9072 case 11: /* 16KB Pages. */
9073 if (level == 0 || (level == 1 && pamax <= 40)) {
9074 return false;
9075 }
9076 break;
9077 case 9: /* 4KB Pages. */
9078 if (level == 0 && pamax <= 42) {
9079 return false;
9080 }
9081 break;
9082 default:
9083 g_assert_not_reached();
9084 }
3526423e
EI
9085
9086 /* Inputsize checks. */
9087 if (inputsize > pamax &&
9088 (arm_el_is_aa64(env, 1) || inputsize > 40)) {
9089 /* This is CONSTRAINED UNPREDICTABLE and we choose to fault. */
9090 return false;
9091 }
1853d5a9 9092 } else {
1853d5a9
EI
9093 /* AArch32 only supports 4KB pages. Assert on that. */
9094 assert(stride == 9);
9095
9096 if (level == 0) {
9097 return false;
9098 }
1853d5a9
EI
9099 }
9100 return true;
9101}
9102
5b2d261d
AB
9103/* Translate from the 4-bit stage 2 representation of
9104 * memory attributes (without cache-allocation hints) to
9105 * the 8-bit representation of the stage 1 MAIR registers
9106 * (which includes allocation hints).
9107 *
9108 * ref: shared/translation/attrs/S2AttrDecode()
9109 * .../S2ConvertAttrsHints()
9110 */
9111static uint8_t convert_stage2_attrs(CPUARMState *env, uint8_t s2attrs)
9112{
9113 uint8_t hiattr = extract32(s2attrs, 2, 2);
9114 uint8_t loattr = extract32(s2attrs, 0, 2);
9115 uint8_t hihint = 0, lohint = 0;
9116
9117 if (hiattr != 0) { /* normal memory */
9118 if ((env->cp15.hcr_el2 & HCR_CD) != 0) { /* cache disabled */
9119 hiattr = loattr = 1; /* non-cacheable */
9120 } else {
9121 if (hiattr != 1) { /* Write-through or write-back */
9122 hihint = 3; /* RW allocate */
9123 }
9124 if (loattr != 1) { /* Write-through or write-back */
9125 lohint = 3; /* RW allocate */
9126 }
9127 }
9128 }
9129
9130 return (hiattr << 6) | (hihint << 4) | (loattr << 2) | lohint;
9131}
9132
b7cc4e82 9133static bool get_phys_addr_lpae(CPUARMState *env, target_ulong address,
03ae85f8 9134 MMUAccessType access_type, ARMMMUIdx mmu_idx,
b7cc4e82 9135 hwaddr *phys_ptr, MemTxAttrs *txattrs, int *prot,
da909b2c 9136 target_ulong *page_size_ptr,
5b2d261d 9137 ARMMMUFaultInfo *fi, ARMCacheAttrs *cacheattrs)
3dde962f 9138{
1853d5a9
EI
9139 ARMCPU *cpu = arm_env_get_cpu(env);
9140 CPUState *cs = CPU(cpu);
3dde962f 9141 /* Read an LPAE long-descriptor translation table. */
da909b2c 9142 ARMFaultType fault_type = ARMFault_Translation;
1b4093ea 9143 uint32_t level;
0c5fbf3b 9144 uint32_t epd = 0;
1f4c8c18 9145 int32_t t0sz, t1sz;
2c8dd318 9146 uint32_t tg;
3dde962f
PM
9147 uint64_t ttbr;
9148 int ttbr_select;
dddb5223 9149 hwaddr descaddr, indexmask, indexmask_grainsize;
3dde962f
PM
9150 uint32_t tableattrs;
9151 target_ulong page_size;
9152 uint32_t attrs;
973a5434 9153 int32_t stride = 9;
6e99f762 9154 int32_t addrsize;
4ca6a051 9155 int inputsize;
2c8dd318 9156 int32_t tbi = 0;
0480f69a 9157 TCR *tcr = regime_tcr(env, mmu_idx);
d8e052b3 9158 int ap, ns, xn, pxn;
88e8add8
GB
9159 uint32_t el = regime_el(env, mmu_idx);
9160 bool ttbr1_valid = true;
6109769a 9161 uint64_t descaddrmask;
6e99f762 9162 bool aarch64 = arm_el_is_aa64(env, el);
0480f69a
PM
9163
9164 /* TODO:
88e8add8
GB
9165 * This code does not handle the different format TCR for VTCR_EL2.
9166 * This code also does not support shareability levels.
9167 * Attribute and permission bit handling should also be checked when adding
9168 * support for those page table walks.
0480f69a 9169 */
6e99f762 9170 if (aarch64) {
1b4093ea 9171 level = 0;
6e99f762 9172 addrsize = 64;
88e8add8 9173 if (el > 1) {
1edee470
EI
9174 if (mmu_idx != ARMMMUIdx_S2NS) {
9175 tbi = extract64(tcr->raw_tcr, 20, 1);
9176 }
88e8add8
GB
9177 } else {
9178 if (extract64(address, 55, 1)) {
9179 tbi = extract64(tcr->raw_tcr, 38, 1);
9180 } else {
9181 tbi = extract64(tcr->raw_tcr, 37, 1);
9182 }
9183 }
2c8dd318 9184 tbi *= 8;
88e8add8
GB
9185
9186 /* If we are in 64-bit EL2 or EL3 then there is no TTBR1, so mark it
9187 * invalid.
9188 */
9189 if (el > 1) {
9190 ttbr1_valid = false;
9191 }
d0a2cbce 9192 } else {
1b4093ea 9193 level = 1;
6e99f762 9194 addrsize = 32;
d0a2cbce
PM
9195 /* There is no TTBR1 for EL2 */
9196 if (el == 2) {
9197 ttbr1_valid = false;
9198 }
2c8dd318 9199 }
3dde962f
PM
9200
9201 /* Determine whether this address is in the region controlled by
9202 * TTBR0 or TTBR1 (or if it is in neither region and should fault).
9203 * This is a Non-secure PL0/1 stage 1 translation, so controlled by
9204 * TTBCR/TTBR0/TTBR1 in accordance with ARM ARM DDI0406C table B-32:
9205 */
6e99f762 9206 if (aarch64) {
4ee38098
EI
9207 /* AArch64 translation. */
9208 t0sz = extract32(tcr->raw_tcr, 0, 6);
2c8dd318
RH
9209 t0sz = MIN(t0sz, 39);
9210 t0sz = MAX(t0sz, 16);
4ee38098
EI
9211 } else if (mmu_idx != ARMMMUIdx_S2NS) {
9212 /* AArch32 stage 1 translation. */
9213 t0sz = extract32(tcr->raw_tcr, 0, 3);
9214 } else {
9215 /* AArch32 stage 2 translation. */
9216 bool sext = extract32(tcr->raw_tcr, 4, 1);
9217 bool sign = extract32(tcr->raw_tcr, 3, 1);
6e99f762
SS
9218 /* Address size is 40-bit for a stage 2 translation,
9219 * and t0sz can be negative (from -8 to 7),
9220 * so we need to adjust it to use the TTBR selecting logic below.
9221 */
9222 addrsize = 40;
9223 t0sz = sextract32(tcr->raw_tcr, 0, 4) + 8;
4ee38098
EI
9224
9225 /* If the sign-extend bit is not the same as t0sz[3], the result
9226 * is unpredictable. Flag this as a guest error. */
9227 if (sign != sext) {
9228 qemu_log_mask(LOG_GUEST_ERROR,
39cba610 9229 "AArch32: VTCR.S / VTCR.T0SZ[3] mismatch\n");
4ee38098 9230 }
2c8dd318 9231 }
1f4c8c18 9232 t1sz = extract32(tcr->raw_tcr, 16, 6);
6e99f762 9233 if (aarch64) {
2c8dd318
RH
9234 t1sz = MIN(t1sz, 39);
9235 t1sz = MAX(t1sz, 16);
9236 }
6e99f762 9237 if (t0sz && !extract64(address, addrsize - t0sz, t0sz - tbi)) {
3dde962f
PM
9238 /* there is a ttbr0 region and we are in it (high bits all zero) */
9239 ttbr_select = 0;
88e8add8 9240 } else if (ttbr1_valid && t1sz &&
6e99f762 9241 !extract64(~address, addrsize - t1sz, t1sz - tbi)) {
3dde962f
PM
9242 /* there is a ttbr1 region and we are in it (high bits all one) */
9243 ttbr_select = 1;
9244 } else if (!t0sz) {
9245 /* ttbr0 region is "everything not in the ttbr1 region" */
9246 ttbr_select = 0;
88e8add8 9247 } else if (!t1sz && ttbr1_valid) {
3dde962f
PM
9248 /* ttbr1 region is "everything not in the ttbr0 region" */
9249 ttbr_select = 1;
9250 } else {
9251 /* in the gap between the two regions, this is a Translation fault */
da909b2c 9252 fault_type = ARMFault_Translation;
3dde962f
PM
9253 goto do_fault;
9254 }
9255
9256 /* Note that QEMU ignores shareability and cacheability attributes,
9257 * so we don't need to do anything with the SH, ORGN, IRGN fields
9258 * in the TTBCR. Similarly, TTBCR:A1 selects whether we get the
9259 * ASID from TTBR0 or TTBR1, but QEMU's TLB doesn't currently
9260 * implement any ASID-like capability so we can ignore it (instead
9261 * we will always flush the TLB any time the ASID is changed).
9262 */
9263 if (ttbr_select == 0) {
aef878be 9264 ttbr = regime_ttbr(env, mmu_idx, 0);
0c5fbf3b
EI
9265 if (el < 2) {
9266 epd = extract32(tcr->raw_tcr, 7, 1);
9267 }
6e99f762 9268 inputsize = addrsize - t0sz;
2c8dd318 9269
11f136ee 9270 tg = extract32(tcr->raw_tcr, 14, 2);
2c8dd318 9271 if (tg == 1) { /* 64KB pages */
973a5434 9272 stride = 13;
2c8dd318
RH
9273 }
9274 if (tg == 2) { /* 16KB pages */
973a5434 9275 stride = 11;
2c8dd318 9276 }
3dde962f 9277 } else {
88e8add8
GB
9278 /* We should only be here if TTBR1 is valid */
9279 assert(ttbr1_valid);
9280
aef878be 9281 ttbr = regime_ttbr(env, mmu_idx, 1);
11f136ee 9282 epd = extract32(tcr->raw_tcr, 23, 1);
6e99f762 9283 inputsize = addrsize - t1sz;
2c8dd318 9284
11f136ee 9285 tg = extract32(tcr->raw_tcr, 30, 2);
2c8dd318 9286 if (tg == 3) { /* 64KB pages */
973a5434 9287 stride = 13;
2c8dd318
RH
9288 }
9289 if (tg == 1) { /* 16KB pages */
973a5434 9290 stride = 11;
2c8dd318 9291 }
3dde962f
PM
9292 }
9293
0480f69a 9294 /* Here we should have set up all the parameters for the translation:
6e99f762 9295 * inputsize, ttbr, epd, stride, tbi
0480f69a
PM
9296 */
9297
3dde962f 9298 if (epd) {
88e8add8
GB
9299 /* Translation table walk disabled => Translation fault on TLB miss
9300 * Note: This is always 0 on 64-bit EL2 and EL3.
9301 */
3dde962f
PM
9302 goto do_fault;
9303 }
9304
1853d5a9
EI
9305 if (mmu_idx != ARMMMUIdx_S2NS) {
9306 /* The starting level depends on the virtual address size (which can
9307 * be up to 48 bits) and the translation granule size. It indicates
9308 * the number of strides (stride bits at a time) needed to
9309 * consume the bits of the input address. In the pseudocode this is:
9310 * level = 4 - RoundUp((inputsize - grainsize) / stride)
9311 * where their 'inputsize' is our 'inputsize', 'grainsize' is
9312 * our 'stride + 3' and 'stride' is our 'stride'.
9313 * Applying the usual "rounded up m/n is (m+n-1)/n" and simplifying:
9314 * = 4 - (inputsize - stride - 3 + stride - 1) / stride
9315 * = 4 - (inputsize - 4) / stride;
9316 */
9317 level = 4 - (inputsize - 4) / stride;
9318 } else {
9319 /* For stage 2 translations the starting level is specified by the
9320 * VTCR_EL2.SL0 field (whose interpretation depends on the page size)
9321 */
1b4093ea
SS
9322 uint32_t sl0 = extract32(tcr->raw_tcr, 6, 2);
9323 uint32_t startlevel;
1853d5a9
EI
9324 bool ok;
9325
6e99f762 9326 if (!aarch64 || stride == 9) {
1853d5a9 9327 /* AArch32 or 4KB pages */
1b4093ea 9328 startlevel = 2 - sl0;
1853d5a9
EI
9329 } else {
9330 /* 16KB or 64KB pages */
1b4093ea 9331 startlevel = 3 - sl0;
1853d5a9
EI
9332 }
9333
9334 /* Check that the starting level is valid. */
6e99f762 9335 ok = check_s2_mmu_setup(cpu, aarch64, startlevel,
1b4093ea 9336 inputsize, stride);
1853d5a9 9337 if (!ok) {
da909b2c 9338 fault_type = ARMFault_Translation;
1853d5a9
EI
9339 goto do_fault;
9340 }
1b4093ea 9341 level = startlevel;
1853d5a9 9342 }
3dde962f 9343
dddb5223
SS
9344 indexmask_grainsize = (1ULL << (stride + 3)) - 1;
9345 indexmask = (1ULL << (inputsize - (stride * (4 - level)))) - 1;
3dde962f
PM
9346
9347 /* Now we can extract the actual base address from the TTBR */
2c8dd318 9348 descaddr = extract64(ttbr, 0, 48);
dddb5223 9349 descaddr &= ~indexmask;
3dde962f 9350
6109769a 9351 /* The address field in the descriptor goes up to bit 39 for ARMv7
dddb5223
SS
9352 * but up to bit 47 for ARMv8, but we use the descaddrmask
9353 * up to bit 39 for AArch32, because we don't need other bits in that case
9354 * to construct next descriptor address (anyway they should be all zeroes).
6109769a 9355 */
6e99f762 9356 descaddrmask = ((1ull << (aarch64 ? 48 : 40)) - 1) &
dddb5223 9357 ~indexmask_grainsize;
6109769a 9358
ebca90e4
PM
9359 /* Secure accesses start with the page table in secure memory and
9360 * can be downgraded to non-secure at any step. Non-secure accesses
9361 * remain non-secure. We implement this by just ORing in the NSTable/NS
9362 * bits at each step.
9363 */
9364 tableattrs = regime_is_secure(env, mmu_idx) ? 0 : (1 << 4);
3dde962f
PM
9365 for (;;) {
9366 uint64_t descriptor;
ebca90e4 9367 bool nstable;
3dde962f 9368
dddb5223 9369 descaddr |= (address >> (stride * (4 - level))) & indexmask;
2c8dd318 9370 descaddr &= ~7ULL;
ebca90e4 9371 nstable = extract32(tableattrs, 4, 1);
3795a6de 9372 descriptor = arm_ldq_ptw(cs, descaddr, !nstable, mmu_idx, fi);
3b39d734 9373 if (fi->type != ARMFault_None) {
37785977
EI
9374 goto do_fault;
9375 }
9376
3dde962f
PM
9377 if (!(descriptor & 1) ||
9378 (!(descriptor & 2) && (level == 3))) {
9379 /* Invalid, or the Reserved level 3 encoding */
9380 goto do_fault;
9381 }
6109769a 9382 descaddr = descriptor & descaddrmask;
3dde962f
PM
9383
9384 if ((descriptor & 2) && (level < 3)) {
9385 /* Table entry. The top five bits are attributes which may
9386 * propagate down through lower levels of the table (and
9387 * which are all arranged so that 0 means "no effect", so
9388 * we can gather them up by ORing in the bits at each level).
9389 */
9390 tableattrs |= extract64(descriptor, 59, 5);
9391 level++;
dddb5223 9392 indexmask = indexmask_grainsize;
3dde962f
PM
9393 continue;
9394 }
9395 /* Block entry at level 1 or 2, or page entry at level 3.
9396 * These are basically the same thing, although the number
9397 * of bits we pull in from the vaddr varies.
9398 */
973a5434 9399 page_size = (1ULL << ((stride * (4 - level)) + 3));
3dde962f 9400 descaddr |= (address & (page_size - 1));
6ab1a5ee 9401 /* Extract attributes from the descriptor */
d615efac
IC
9402 attrs = extract64(descriptor, 2, 10)
9403 | (extract64(descriptor, 52, 12) << 10);
6ab1a5ee
EI
9404
9405 if (mmu_idx == ARMMMUIdx_S2NS) {
9406 /* Stage 2 table descriptors do not include any attribute fields */
9407 break;
9408 }
9409 /* Merge in attributes from table descriptors */
3dde962f
PM
9410 attrs |= extract32(tableattrs, 0, 2) << 11; /* XN, PXN */
9411 attrs |= extract32(tableattrs, 3, 1) << 5; /* APTable[1] => AP[2] */
9412 /* The sense of AP[1] vs APTable[0] is reversed, as APTable[0] == 1
9413 * means "force PL1 access only", which means forcing AP[1] to 0.
9414 */
9415 if (extract32(tableattrs, 2, 1)) {
9416 attrs &= ~(1 << 4);
9417 }
ebca90e4 9418 attrs |= nstable << 3; /* NS */
3dde962f
PM
9419 break;
9420 }
9421 /* Here descaddr is the final physical address, and attributes
9422 * are all in attrs.
9423 */
da909b2c 9424 fault_type = ARMFault_AccessFlag;
3dde962f
PM
9425 if ((attrs & (1 << 8)) == 0) {
9426 /* Access flag */
9427 goto do_fault;
9428 }
d8e052b3
AJ
9429
9430 ap = extract32(attrs, 4, 2);
d8e052b3 9431 xn = extract32(attrs, 12, 1);
d8e052b3 9432
6ab1a5ee
EI
9433 if (mmu_idx == ARMMMUIdx_S2NS) {
9434 ns = true;
9435 *prot = get_S2prot(env, ap, xn);
9436 } else {
9437 ns = extract32(attrs, 3, 1);
9438 pxn = extract32(attrs, 11, 1);
6e99f762 9439 *prot = get_S1prot(env, mmu_idx, aarch64, ap, ns, xn, pxn);
6ab1a5ee 9440 }
d8e052b3 9441
da909b2c 9442 fault_type = ARMFault_Permission;
d8e052b3 9443 if (!(*prot & (1 << access_type))) {
3dde962f
PM
9444 goto do_fault;
9445 }
3dde962f 9446
8bf5b6a9
PM
9447 if (ns) {
9448 /* The NS bit will (as required by the architecture) have no effect if
9449 * the CPU doesn't support TZ or this is a non-secure translation
9450 * regime, because the attribute will already be non-secure.
9451 */
9452 txattrs->secure = false;
9453 }
5b2d261d
AB
9454
9455 if (cacheattrs != NULL) {
9456 if (mmu_idx == ARMMMUIdx_S2NS) {
9457 cacheattrs->attrs = convert_stage2_attrs(env,
9458 extract32(attrs, 0, 4));
9459 } else {
9460 /* Index into MAIR registers for cache attributes */
9461 uint8_t attrindx = extract32(attrs, 0, 3);
9462 uint64_t mair = env->cp15.mair_el[regime_el(env, mmu_idx)];
9463 assert(attrindx <= 7);
9464 cacheattrs->attrs = extract64(mair, attrindx * 8, 8);
9465 }
9466 cacheattrs->shareability = extract32(attrs, 6, 2);
9467 }
9468
3dde962f
PM
9469 *phys_ptr = descaddr;
9470 *page_size_ptr = page_size;
b7cc4e82 9471 return false;
3dde962f
PM
9472
9473do_fault:
da909b2c
PM
9474 fi->type = fault_type;
9475 fi->level = level;
37785977
EI
9476 /* Tag the error as S2 for failed S1 PTW at S2 or ordinary S2. */
9477 fi->stage2 = fi->s1ptw || (mmu_idx == ARMMMUIdx_S2NS);
b7cc4e82 9478 return true;
3dde962f
PM
9479}
9480
f6bda88f
PC
9481static inline void get_phys_addr_pmsav7_default(CPUARMState *env,
9482 ARMMMUIdx mmu_idx,
9483 int32_t address, int *prot)
9484{
3a00d560
MD
9485 if (!arm_feature(env, ARM_FEATURE_M)) {
9486 *prot = PAGE_READ | PAGE_WRITE;
9487 switch (address) {
9488 case 0xF0000000 ... 0xFFFFFFFF:
9489 if (regime_sctlr(env, mmu_idx) & SCTLR_V) {
9490 /* hivecs execing is ok */
9491 *prot |= PAGE_EXEC;
9492 }
9493 break;
9494 case 0x00000000 ... 0x7FFFFFFF:
f6bda88f 9495 *prot |= PAGE_EXEC;
3a00d560
MD
9496 break;
9497 }
9498 } else {
9499 /* Default system address map for M profile cores.
9500 * The architecture specifies which regions are execute-never;
9501 * at the MPU level no other checks are defined.
9502 */
9503 switch (address) {
9504 case 0x00000000 ... 0x1fffffff: /* ROM */
9505 case 0x20000000 ... 0x3fffffff: /* SRAM */
9506 case 0x60000000 ... 0x7fffffff: /* RAM */
9507 case 0x80000000 ... 0x9fffffff: /* RAM */
9508 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
9509 break;
9510 case 0x40000000 ... 0x5fffffff: /* Peripheral */
9511 case 0xa0000000 ... 0xbfffffff: /* Device */
9512 case 0xc0000000 ... 0xdfffffff: /* Device */
9513 case 0xe0000000 ... 0xffffffff: /* System */
9514 *prot = PAGE_READ | PAGE_WRITE;
9515 break;
9516 default:
9517 g_assert_not_reached();
f6bda88f 9518 }
f6bda88f 9519 }
f6bda88f
PC
9520}
9521
29c483a5
MD
9522static bool pmsav7_use_background_region(ARMCPU *cpu,
9523 ARMMMUIdx mmu_idx, bool is_user)
9524{
9525 /* Return true if we should use the default memory map as a
9526 * "background" region if there are no hits against any MPU regions.
9527 */
9528 CPUARMState *env = &cpu->env;
9529
9530 if (is_user) {
9531 return false;
9532 }
9533
9534 if (arm_feature(env, ARM_FEATURE_M)) {
ecf5e8ea
PM
9535 return env->v7m.mpu_ctrl[regime_is_secure(env, mmu_idx)]
9536 & R_V7M_MPU_CTRL_PRIVDEFENA_MASK;
29c483a5
MD
9537 } else {
9538 return regime_sctlr(env, mmu_idx) & SCTLR_BR;
9539 }
9540}
9541
38aaa60c
PM
9542static inline bool m_is_ppb_region(CPUARMState *env, uint32_t address)
9543{
9544 /* True if address is in the M profile PPB region 0xe0000000 - 0xe00fffff */
9545 return arm_feature(env, ARM_FEATURE_M) &&
9546 extract32(address, 20, 12) == 0xe00;
9547}
9548
bf446a11
PM
9549static inline bool m_is_system_region(CPUARMState *env, uint32_t address)
9550{
9551 /* True if address is in the M profile system region
9552 * 0xe0000000 - 0xffffffff
9553 */
9554 return arm_feature(env, ARM_FEATURE_M) && extract32(address, 29, 3) == 0x7;
9555}
9556
f6bda88f 9557static bool get_phys_addr_pmsav7(CPUARMState *env, uint32_t address,
03ae85f8 9558 MMUAccessType access_type, ARMMMUIdx mmu_idx,
9375ad15
PM
9559 hwaddr *phys_ptr, int *prot,
9560 ARMMMUFaultInfo *fi)
f6bda88f
PC
9561{
9562 ARMCPU *cpu = arm_env_get_cpu(env);
9563 int n;
9564 bool is_user = regime_is_user(env, mmu_idx);
9565
9566 *phys_ptr = address;
9567 *prot = 0;
9568
38aaa60c
PM
9569 if (regime_translation_disabled(env, mmu_idx) ||
9570 m_is_ppb_region(env, address)) {
9571 /* MPU disabled or M profile PPB access: use default memory map.
9572 * The other case which uses the default memory map in the
9573 * v7M ARM ARM pseudocode is exception vector reads from the vector
9574 * table. In QEMU those accesses are done in arm_v7m_load_vector(),
9575 * which always does a direct read using address_space_ldl(), rather
9576 * than going via this function, so we don't need to check that here.
9577 */
f6bda88f
PC
9578 get_phys_addr_pmsav7_default(env, mmu_idx, address, prot);
9579 } else { /* MPU enabled */
9580 for (n = (int)cpu->pmsav7_dregion - 1; n >= 0; n--) {
9581 /* region search */
9582 uint32_t base = env->pmsav7.drbar[n];
9583 uint32_t rsize = extract32(env->pmsav7.drsr[n], 1, 5);
9584 uint32_t rmask;
9585 bool srdis = false;
9586
9587 if (!(env->pmsav7.drsr[n] & 0x1)) {
9588 continue;
9589 }
9590
9591 if (!rsize) {
c9f9f124
MD
9592 qemu_log_mask(LOG_GUEST_ERROR,
9593 "DRSR[%d]: Rsize field cannot be 0\n", n);
f6bda88f
PC
9594 continue;
9595 }
9596 rsize++;
9597 rmask = (1ull << rsize) - 1;
9598
9599 if (base & rmask) {
c9f9f124
MD
9600 qemu_log_mask(LOG_GUEST_ERROR,
9601 "DRBAR[%d]: 0x%" PRIx32 " misaligned "
9602 "to DRSR region size, mask = 0x%" PRIx32 "\n",
9603 n, base, rmask);
f6bda88f
PC
9604 continue;
9605 }
9606
9607 if (address < base || address > base + rmask) {
9608 continue;
9609 }
9610
9611 /* Region matched */
9612
9613 if (rsize >= 8) { /* no subregions for regions < 256 bytes */
9614 int i, snd;
9615 uint32_t srdis_mask;
9616
9617 rsize -= 3; /* sub region size (power of 2) */
9618 snd = ((address - base) >> rsize) & 0x7;
9619 srdis = extract32(env->pmsav7.drsr[n], snd + 8, 1);
9620
9621 srdis_mask = srdis ? 0x3 : 0x0;
9622 for (i = 2; i <= 8 && rsize < TARGET_PAGE_BITS; i *= 2) {
9623 /* This will check in groups of 2, 4 and then 8, whether
9624 * the subregion bits are consistent. rsize is incremented
9625 * back up to give the region size, considering consistent
9626 * adjacent subregions as one region. Stop testing if rsize
9627 * is already big enough for an entire QEMU page.
9628 */
9629 int snd_rounded = snd & ~(i - 1);
9630 uint32_t srdis_multi = extract32(env->pmsav7.drsr[n],
9631 snd_rounded + 8, i);
9632 if (srdis_mask ^ srdis_multi) {
9633 break;
9634 }
9635 srdis_mask = (srdis_mask << i) | srdis_mask;
9636 rsize++;
9637 }
9638 }
9639 if (rsize < TARGET_PAGE_BITS) {
c9f9f124
MD
9640 qemu_log_mask(LOG_UNIMP,
9641 "DRSR[%d]: No support for MPU (sub)region "
f6bda88f 9642 "alignment of %" PRIu32 " bits. Minimum is %d\n",
c9f9f124 9643 n, rsize, TARGET_PAGE_BITS);
f6bda88f
PC
9644 continue;
9645 }
9646 if (srdis) {
9647 continue;
9648 }
9649 break;
9650 }
9651
9652 if (n == -1) { /* no hits */
29c483a5 9653 if (!pmsav7_use_background_region(cpu, mmu_idx, is_user)) {
f6bda88f 9654 /* background fault */
9375ad15 9655 fi->type = ARMFault_Background;
f6bda88f
PC
9656 return true;
9657 }
9658 get_phys_addr_pmsav7_default(env, mmu_idx, address, prot);
9659 } else { /* a MPU hit! */
9660 uint32_t ap = extract32(env->pmsav7.dracr[n], 8, 3);
bf446a11
PM
9661 uint32_t xn = extract32(env->pmsav7.dracr[n], 12, 1);
9662
9663 if (m_is_system_region(env, address)) {
9664 /* System space is always execute never */
9665 xn = 1;
9666 }
f6bda88f
PC
9667
9668 if (is_user) { /* User mode AP bit decoding */
9669 switch (ap) {
9670 case 0:
9671 case 1:
9672 case 5:
9673 break; /* no access */
9674 case 3:
9675 *prot |= PAGE_WRITE;
9676 /* fall through */
9677 case 2:
9678 case 6:
9679 *prot |= PAGE_READ | PAGE_EXEC;
9680 break;
8638f1ad
PM
9681 case 7:
9682 /* for v7M, same as 6; for R profile a reserved value */
9683 if (arm_feature(env, ARM_FEATURE_M)) {
9684 *prot |= PAGE_READ | PAGE_EXEC;
9685 break;
9686 }
9687 /* fall through */
f6bda88f
PC
9688 default:
9689 qemu_log_mask(LOG_GUEST_ERROR,
c9f9f124
MD
9690 "DRACR[%d]: Bad value for AP bits: 0x%"
9691 PRIx32 "\n", n, ap);
f6bda88f
PC
9692 }
9693 } else { /* Priv. mode AP bits decoding */
9694 switch (ap) {
9695 case 0:
9696 break; /* no access */
9697 case 1:
9698 case 2:
9699 case 3:
9700 *prot |= PAGE_WRITE;
9701 /* fall through */
9702 case 5:
9703 case 6:
9704 *prot |= PAGE_READ | PAGE_EXEC;
9705 break;
8638f1ad
PM
9706 case 7:
9707 /* for v7M, same as 6; for R profile a reserved value */
9708 if (arm_feature(env, ARM_FEATURE_M)) {
9709 *prot |= PAGE_READ | PAGE_EXEC;
9710 break;
9711 }
9712 /* fall through */
f6bda88f
PC
9713 default:
9714 qemu_log_mask(LOG_GUEST_ERROR,
c9f9f124
MD
9715 "DRACR[%d]: Bad value for AP bits: 0x%"
9716 PRIx32 "\n", n, ap);
f6bda88f
PC
9717 }
9718 }
9719
9720 /* execute never */
bf446a11 9721 if (xn) {
f6bda88f
PC
9722 *prot &= ~PAGE_EXEC;
9723 }
9724 }
9725 }
9726
9375ad15
PM
9727 fi->type = ARMFault_Permission;
9728 fi->level = 1;
f6bda88f
PC
9729 return !(*prot & (1 << access_type));
9730}
9731
35337cc3
PM
9732static bool v8m_is_sau_exempt(CPUARMState *env,
9733 uint32_t address, MMUAccessType access_type)
9734{
9735 /* The architecture specifies that certain address ranges are
9736 * exempt from v8M SAU/IDAU checks.
9737 */
9738 return
9739 (access_type == MMU_INST_FETCH && m_is_system_region(env, address)) ||
9740 (address >= 0xe0000000 && address <= 0xe0002fff) ||
9741 (address >= 0xe000e000 && address <= 0xe000efff) ||
9742 (address >= 0xe002e000 && address <= 0xe002efff) ||
9743 (address >= 0xe0040000 && address <= 0xe0041fff) ||
9744 (address >= 0xe00ff000 && address <= 0xe00fffff);
9745}
9746
9747static void v8m_security_lookup(CPUARMState *env, uint32_t address,
9748 MMUAccessType access_type, ARMMMUIdx mmu_idx,
9749 V8M_SAttributes *sattrs)
9750{
9751 /* Look up the security attributes for this address. Compare the
9752 * pseudocode SecurityCheck() function.
9753 * We assume the caller has zero-initialized *sattrs.
9754 */
9755 ARMCPU *cpu = arm_env_get_cpu(env);
9756 int r;
9757
9758 /* TODO: implement IDAU */
9759
9760 if (access_type == MMU_INST_FETCH && extract32(address, 28, 4) == 0xf) {
9761 /* 0xf0000000..0xffffffff is always S for insn fetches */
9762 return;
9763 }
9764
9765 if (v8m_is_sau_exempt(env, address, access_type)) {
9766 sattrs->ns = !regime_is_secure(env, mmu_idx);
9767 return;
9768 }
9769
9770 switch (env->sau.ctrl & 3) {
9771 case 0: /* SAU.ENABLE == 0, SAU.ALLNS == 0 */
9772 break;
9773 case 2: /* SAU.ENABLE == 0, SAU.ALLNS == 1 */
9774 sattrs->ns = true;
9775 break;
9776 default: /* SAU.ENABLE == 1 */
9777 for (r = 0; r < cpu->sau_sregion; r++) {
9778 if (env->sau.rlar[r] & 1) {
9779 uint32_t base = env->sau.rbar[r] & ~0x1f;
9780 uint32_t limit = env->sau.rlar[r] | 0x1f;
9781
9782 if (base <= address && limit >= address) {
9783 if (sattrs->srvalid) {
9784 /* If we hit in more than one region then we must report
9785 * as Secure, not NS-Callable, with no valid region
9786 * number info.
9787 */
9788 sattrs->ns = false;
9789 sattrs->nsc = false;
9790 sattrs->sregion = 0;
9791 sattrs->srvalid = false;
9792 break;
9793 } else {
9794 if (env->sau.rlar[r] & 2) {
9795 sattrs->nsc = true;
9796 } else {
9797 sattrs->ns = true;
9798 }
9799 sattrs->srvalid = true;
9800 sattrs->sregion = r;
9801 }
9802 }
9803 }
9804 }
9805
9806 /* TODO when we support the IDAU then it may override the result here */
9807 break;
9808 }
9809}
9810
54317c0f
PM
9811static bool pmsav8_mpu_lookup(CPUARMState *env, uint32_t address,
9812 MMUAccessType access_type, ARMMMUIdx mmu_idx,
9813 hwaddr *phys_ptr, MemTxAttrs *txattrs,
3f551b5b 9814 int *prot, ARMMMUFaultInfo *fi, uint32_t *mregion)
54317c0f
PM
9815{
9816 /* Perform a PMSAv8 MPU lookup (without also doing the SAU check
9817 * that a full phys-to-virt translation does).
9818 * mregion is (if not NULL) set to the region number which matched,
9819 * or -1 if no region number is returned (MPU off, address did not
9820 * hit a region, address hit in multiple regions).
9821 */
504e3cc3
PM
9822 ARMCPU *cpu = arm_env_get_cpu(env);
9823 bool is_user = regime_is_user(env, mmu_idx);
62c58ee0 9824 uint32_t secure = regime_is_secure(env, mmu_idx);
504e3cc3
PM
9825 int n;
9826 int matchregion = -1;
9827 bool hit = false;
9828
9829 *phys_ptr = address;
9830 *prot = 0;
54317c0f
PM
9831 if (mregion) {
9832 *mregion = -1;
35337cc3
PM
9833 }
9834
504e3cc3
PM
9835 /* Unlike the ARM ARM pseudocode, we don't need to check whether this
9836 * was an exception vector read from the vector table (which is always
9837 * done using the default system address map), because those accesses
9838 * are done in arm_v7m_load_vector(), which always does a direct
9839 * read using address_space_ldl(), rather than going via this function.
9840 */
9841 if (regime_translation_disabled(env, mmu_idx)) { /* MPU disabled */
9842 hit = true;
9843 } else if (m_is_ppb_region(env, address)) {
9844 hit = true;
9845 } else if (pmsav7_use_background_region(cpu, mmu_idx, is_user)) {
9846 hit = true;
9847 } else {
9848 for (n = (int)cpu->pmsav7_dregion - 1; n >= 0; n--) {
9849 /* region search */
9850 /* Note that the base address is bits [31:5] from the register
9851 * with bits [4:0] all zeroes, but the limit address is bits
9852 * [31:5] from the register with bits [4:0] all ones.
9853 */
62c58ee0
PM
9854 uint32_t base = env->pmsav8.rbar[secure][n] & ~0x1f;
9855 uint32_t limit = env->pmsav8.rlar[secure][n] | 0x1f;
504e3cc3 9856
62c58ee0 9857 if (!(env->pmsav8.rlar[secure][n] & 0x1)) {
504e3cc3
PM
9858 /* Region disabled */
9859 continue;
9860 }
9861
9862 if (address < base || address > limit) {
9863 continue;
9864 }
9865
9866 if (hit) {
9867 /* Multiple regions match -- always a failure (unlike
9868 * PMSAv7 where highest-numbered-region wins)
9869 */
3f551b5b
PM
9870 fi->type = ARMFault_Permission;
9871 fi->level = 1;
504e3cc3
PM
9872 return true;
9873 }
9874
9875 matchregion = n;
9876 hit = true;
9877
9878 if (base & ~TARGET_PAGE_MASK) {
9879 qemu_log_mask(LOG_UNIMP,
9880 "MPU_RBAR[%d]: No support for MPU region base"
9881 "address of 0x%" PRIx32 ". Minimum alignment is "
9882 "%d\n",
9883 n, base, TARGET_PAGE_BITS);
9884 continue;
9885 }
9886 if ((limit + 1) & ~TARGET_PAGE_MASK) {
9887 qemu_log_mask(LOG_UNIMP,
9888 "MPU_RBAR[%d]: No support for MPU region limit"
9889 "address of 0x%" PRIx32 ". Minimum alignment is "
9890 "%d\n",
9891 n, limit, TARGET_PAGE_BITS);
9892 continue;
9893 }
9894 }
9895 }
9896
9897 if (!hit) {
9898 /* background fault */
3f551b5b 9899 fi->type = ARMFault_Background;
504e3cc3
PM
9900 return true;
9901 }
9902
9903 if (matchregion == -1) {
9904 /* hit using the background region */
9905 get_phys_addr_pmsav7_default(env, mmu_idx, address, prot);
9906 } else {
62c58ee0
PM
9907 uint32_t ap = extract32(env->pmsav8.rbar[secure][matchregion], 1, 2);
9908 uint32_t xn = extract32(env->pmsav8.rbar[secure][matchregion], 0, 1);
504e3cc3
PM
9909
9910 if (m_is_system_region(env, address)) {
9911 /* System space is always execute never */
9912 xn = 1;
9913 }
9914
9915 *prot = simple_ap_to_rw_prot(env, mmu_idx, ap);
9916 if (*prot && !xn) {
9917 *prot |= PAGE_EXEC;
9918 }
9919 /* We don't need to look the attribute up in the MAIR0/MAIR1
9920 * registers because that only tells us about cacheability.
9921 */
54317c0f
PM
9922 if (mregion) {
9923 *mregion = matchregion;
9924 }
504e3cc3
PM
9925 }
9926
3f551b5b
PM
9927 fi->type = ARMFault_Permission;
9928 fi->level = 1;
504e3cc3
PM
9929 return !(*prot & (1 << access_type));
9930}
9931
54317c0f
PM
9932
9933static bool get_phys_addr_pmsav8(CPUARMState *env, uint32_t address,
9934 MMUAccessType access_type, ARMMMUIdx mmu_idx,
9935 hwaddr *phys_ptr, MemTxAttrs *txattrs,
3f551b5b 9936 int *prot, ARMMMUFaultInfo *fi)
54317c0f
PM
9937{
9938 uint32_t secure = regime_is_secure(env, mmu_idx);
9939 V8M_SAttributes sattrs = {};
9940
9941 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
9942 v8m_security_lookup(env, address, access_type, mmu_idx, &sattrs);
9943 if (access_type == MMU_INST_FETCH) {
9944 /* Instruction fetches always use the MMU bank and the
9945 * transaction attribute determined by the fetch address,
9946 * regardless of CPU state. This is painful for QEMU
9947 * to handle, because it would mean we need to encode
9948 * into the mmu_idx not just the (user, negpri) information
9949 * for the current security state but also that for the
9950 * other security state, which would balloon the number
9951 * of mmu_idx values needed alarmingly.
9952 * Fortunately we can avoid this because it's not actually
9953 * possible to arbitrarily execute code from memory with
9954 * the wrong security attribute: it will always generate
9955 * an exception of some kind or another, apart from the
9956 * special case of an NS CPU executing an SG instruction
9957 * in S&NSC memory. So we always just fail the translation
9958 * here and sort things out in the exception handler
9959 * (including possibly emulating an SG instruction).
9960 */
9961 if (sattrs.ns != !secure) {
3f551b5b
PM
9962 if (sattrs.nsc) {
9963 fi->type = ARMFault_QEMU_NSCExec;
9964 } else {
9965 fi->type = ARMFault_QEMU_SFault;
9966 }
54317c0f
PM
9967 *phys_ptr = address;
9968 *prot = 0;
9969 return true;
9970 }
9971 } else {
9972 /* For data accesses we always use the MMU bank indicated
9973 * by the current CPU state, but the security attributes
9974 * might downgrade a secure access to nonsecure.
9975 */
9976 if (sattrs.ns) {
9977 txattrs->secure = false;
9978 } else if (!secure) {
9979 /* NS access to S memory must fault.
9980 * Architecturally we should first check whether the
9981 * MPU information for this address indicates that we
9982 * are doing an unaligned access to Device memory, which
9983 * should generate a UsageFault instead. QEMU does not
9984 * currently check for that kind of unaligned access though.
9985 * If we added it we would need to do so as a special case
9986 * for M_FAKE_FSR_SFAULT in arm_v7m_cpu_do_interrupt().
9987 */
3f551b5b 9988 fi->type = ARMFault_QEMU_SFault;
54317c0f
PM
9989 *phys_ptr = address;
9990 *prot = 0;
9991 return true;
9992 }
9993 }
9994 }
9995
9996 return pmsav8_mpu_lookup(env, address, access_type, mmu_idx, phys_ptr,
3f551b5b 9997 txattrs, prot, fi, NULL);
54317c0f
PM
9998}
9999
13689d43 10000static bool get_phys_addr_pmsav5(CPUARMState *env, uint32_t address,
03ae85f8 10001 MMUAccessType access_type, ARMMMUIdx mmu_idx,
53a4e5c5
PM
10002 hwaddr *phys_ptr, int *prot,
10003 ARMMMUFaultInfo *fi)
9ee6e8bb
PB
10004{
10005 int n;
10006 uint32_t mask;
10007 uint32_t base;
0480f69a 10008 bool is_user = regime_is_user(env, mmu_idx);
9ee6e8bb 10009
3279adb9
PM
10010 if (regime_translation_disabled(env, mmu_idx)) {
10011 /* MPU disabled. */
10012 *phys_ptr = address;
10013 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
10014 return false;
10015 }
10016
9ee6e8bb
PB
10017 *phys_ptr = address;
10018 for (n = 7; n >= 0; n--) {
554b0b09 10019 base = env->cp15.c6_region[n];
87c3d486 10020 if ((base & 1) == 0) {
554b0b09 10021 continue;
87c3d486 10022 }
554b0b09
PM
10023 mask = 1 << ((base >> 1) & 0x1f);
10024 /* Keep this shift separate from the above to avoid an
10025 (undefined) << 32. */
10026 mask = (mask << 1) - 1;
87c3d486 10027 if (((base ^ address) & ~mask) == 0) {
554b0b09 10028 break;
87c3d486 10029 }
9ee6e8bb 10030 }
87c3d486 10031 if (n < 0) {
53a4e5c5 10032 fi->type = ARMFault_Background;
b7cc4e82 10033 return true;
87c3d486 10034 }
9ee6e8bb 10035
03ae85f8 10036 if (access_type == MMU_INST_FETCH) {
7e09797c 10037 mask = env->cp15.pmsav5_insn_ap;
9ee6e8bb 10038 } else {
7e09797c 10039 mask = env->cp15.pmsav5_data_ap;
9ee6e8bb
PB
10040 }
10041 mask = (mask >> (n * 4)) & 0xf;
10042 switch (mask) {
10043 case 0:
53a4e5c5
PM
10044 fi->type = ARMFault_Permission;
10045 fi->level = 1;
b7cc4e82 10046 return true;
9ee6e8bb 10047 case 1:
87c3d486 10048 if (is_user) {
53a4e5c5
PM
10049 fi->type = ARMFault_Permission;
10050 fi->level = 1;
b7cc4e82 10051 return true;
87c3d486 10052 }
554b0b09
PM
10053 *prot = PAGE_READ | PAGE_WRITE;
10054 break;
9ee6e8bb 10055 case 2:
554b0b09 10056 *prot = PAGE_READ;
87c3d486 10057 if (!is_user) {
554b0b09 10058 *prot |= PAGE_WRITE;
87c3d486 10059 }
554b0b09 10060 break;
9ee6e8bb 10061 case 3:
554b0b09
PM
10062 *prot = PAGE_READ | PAGE_WRITE;
10063 break;
9ee6e8bb 10064 case 5:
87c3d486 10065 if (is_user) {
53a4e5c5
PM
10066 fi->type = ARMFault_Permission;
10067 fi->level = 1;
b7cc4e82 10068 return true;
87c3d486 10069 }
554b0b09
PM
10070 *prot = PAGE_READ;
10071 break;
9ee6e8bb 10072 case 6:
554b0b09
PM
10073 *prot = PAGE_READ;
10074 break;
9ee6e8bb 10075 default:
554b0b09 10076 /* Bad permission. */
53a4e5c5
PM
10077 fi->type = ARMFault_Permission;
10078 fi->level = 1;
b7cc4e82 10079 return true;
9ee6e8bb 10080 }
3ad493fc 10081 *prot |= PAGE_EXEC;
b7cc4e82 10082 return false;
9ee6e8bb
PB
10083}
10084
5b2d261d
AB
10085/* Combine either inner or outer cacheability attributes for normal
10086 * memory, according to table D4-42 and pseudocode procedure
10087 * CombineS1S2AttrHints() of ARM DDI 0487B.b (the ARMv8 ARM).
10088 *
10089 * NB: only stage 1 includes allocation hints (RW bits), leading to
10090 * some asymmetry.
10091 */
10092static uint8_t combine_cacheattr_nibble(uint8_t s1, uint8_t s2)
10093{
10094 if (s1 == 4 || s2 == 4) {
10095 /* non-cacheable has precedence */
10096 return 4;
10097 } else if (extract32(s1, 2, 2) == 0 || extract32(s1, 2, 2) == 2) {
10098 /* stage 1 write-through takes precedence */
10099 return s1;
10100 } else if (extract32(s2, 2, 2) == 2) {
10101 /* stage 2 write-through takes precedence, but the allocation hint
10102 * is still taken from stage 1
10103 */
10104 return (2 << 2) | extract32(s1, 0, 2);
10105 } else { /* write-back */
10106 return s1;
10107 }
10108}
10109
10110/* Combine S1 and S2 cacheability/shareability attributes, per D4.5.4
10111 * and CombineS1S2Desc()
10112 *
10113 * @s1: Attributes from stage 1 walk
10114 * @s2: Attributes from stage 2 walk
10115 */
10116static ARMCacheAttrs combine_cacheattrs(ARMCacheAttrs s1, ARMCacheAttrs s2)
10117{
10118 uint8_t s1lo = extract32(s1.attrs, 0, 4), s2lo = extract32(s2.attrs, 0, 4);
10119 uint8_t s1hi = extract32(s1.attrs, 4, 4), s2hi = extract32(s2.attrs, 4, 4);
10120 ARMCacheAttrs ret;
10121
10122 /* Combine shareability attributes (table D4-43) */
10123 if (s1.shareability == 2 || s2.shareability == 2) {
10124 /* if either are outer-shareable, the result is outer-shareable */
10125 ret.shareability = 2;
10126 } else if (s1.shareability == 3 || s2.shareability == 3) {
10127 /* if either are inner-shareable, the result is inner-shareable */
10128 ret.shareability = 3;
10129 } else {
10130 /* both non-shareable */
10131 ret.shareability = 0;
10132 }
10133
10134 /* Combine memory type and cacheability attributes */
10135 if (s1hi == 0 || s2hi == 0) {
10136 /* Device has precedence over normal */
10137 if (s1lo == 0 || s2lo == 0) {
10138 /* nGnRnE has precedence over anything */
10139 ret.attrs = 0;
10140 } else if (s1lo == 4 || s2lo == 4) {
10141 /* non-Reordering has precedence over Reordering */
10142 ret.attrs = 4; /* nGnRE */
10143 } else if (s1lo == 8 || s2lo == 8) {
10144 /* non-Gathering has precedence over Gathering */
10145 ret.attrs = 8; /* nGRE */
10146 } else {
10147 ret.attrs = 0xc; /* GRE */
10148 }
10149
10150 /* Any location for which the resultant memory type is any
10151 * type of Device memory is always treated as Outer Shareable.
10152 */
10153 ret.shareability = 2;
10154 } else { /* Normal memory */
10155 /* Outer/inner cacheability combine independently */
10156 ret.attrs = combine_cacheattr_nibble(s1hi, s2hi) << 4
10157 | combine_cacheattr_nibble(s1lo, s2lo);
10158
10159 if (ret.attrs == 0x44) {
10160 /* Any location for which the resultant memory type is Normal
10161 * Inner Non-cacheable, Outer Non-cacheable is always treated
10162 * as Outer Shareable.
10163 */
10164 ret.shareability = 2;
10165 }
10166 }
10167
10168 return ret;
10169}
10170
10171
702a9357
PM
10172/* get_phys_addr - get the physical address for this virtual address
10173 *
10174 * Find the physical address corresponding to the given virtual address,
10175 * by doing a translation table walk on MMU based systems or using the
10176 * MPU state on MPU based systems.
10177 *
b7cc4e82
PC
10178 * Returns false if the translation was successful. Otherwise, phys_ptr, attrs,
10179 * prot and page_size may not be filled in, and the populated fsr value provides
702a9357
PM
10180 * information on why the translation aborted, in the format of a
10181 * DFSR/IFSR fault register, with the following caveats:
10182 * * we honour the short vs long DFSR format differences.
10183 * * the WnR bit is never set (the caller must do this).
f6bda88f 10184 * * for PSMAv5 based systems we don't bother to return a full FSR format
702a9357
PM
10185 * value.
10186 *
10187 * @env: CPUARMState
10188 * @address: virtual address to get physical address for
10189 * @access_type: 0 for read, 1 for write, 2 for execute
d3649702 10190 * @mmu_idx: MMU index indicating required translation regime
702a9357 10191 * @phys_ptr: set to the physical address corresponding to the virtual address
8bf5b6a9 10192 * @attrs: set to the memory transaction attributes to use
702a9357
PM
10193 * @prot: set to the permissions for the page containing phys_ptr
10194 * @page_size: set to the size of the page containing phys_ptr
5b2d261d
AB
10195 * @fi: set to fault info if the translation fails
10196 * @cacheattrs: (if non-NULL) set to the cacheability/shareability attributes
702a9357 10197 */
af51f566 10198static bool get_phys_addr(CPUARMState *env, target_ulong address,
03ae85f8 10199 MMUAccessType access_type, ARMMMUIdx mmu_idx,
af51f566 10200 hwaddr *phys_ptr, MemTxAttrs *attrs, int *prot,
bc52bfeb 10201 target_ulong *page_size,
5b2d261d 10202 ARMMMUFaultInfo *fi, ARMCacheAttrs *cacheattrs)
9ee6e8bb 10203{
0480f69a 10204 if (mmu_idx == ARMMMUIdx_S12NSE0 || mmu_idx == ARMMMUIdx_S12NSE1) {
9b539263
EI
10205 /* Call ourselves recursively to do the stage 1 and then stage 2
10206 * translations.
0480f69a 10207 */
9b539263
EI
10208 if (arm_feature(env, ARM_FEATURE_EL2)) {
10209 hwaddr ipa;
10210 int s2_prot;
10211 int ret;
5b2d261d 10212 ARMCacheAttrs cacheattrs2 = {};
9b539263
EI
10213
10214 ret = get_phys_addr(env, address, access_type,
8bd5c820 10215 stage_1_mmu_idx(mmu_idx), &ipa, attrs,
bc52bfeb 10216 prot, page_size, fi, cacheattrs);
9b539263
EI
10217
10218 /* If S1 fails or S2 is disabled, return early. */
10219 if (ret || regime_translation_disabled(env, ARMMMUIdx_S2NS)) {
10220 *phys_ptr = ipa;
10221 return ret;
10222 }
10223
10224 /* S1 is done. Now do S2 translation. */
10225 ret = get_phys_addr_lpae(env, ipa, access_type, ARMMMUIdx_S2NS,
10226 phys_ptr, attrs, &s2_prot,
da909b2c 10227 page_size, fi,
5b2d261d 10228 cacheattrs != NULL ? &cacheattrs2 : NULL);
9b539263
EI
10229 fi->s2addr = ipa;
10230 /* Combine the S1 and S2 perms. */
10231 *prot &= s2_prot;
5b2d261d
AB
10232
10233 /* Combine the S1 and S2 cache attributes, if needed */
10234 if (!ret && cacheattrs != NULL) {
10235 *cacheattrs = combine_cacheattrs(*cacheattrs, cacheattrs2);
10236 }
10237
9b539263
EI
10238 return ret;
10239 } else {
10240 /*
10241 * For non-EL2 CPUs a stage1+stage2 translation is just stage 1.
10242 */
8bd5c820 10243 mmu_idx = stage_1_mmu_idx(mmu_idx);
9b539263 10244 }
0480f69a 10245 }
d3649702 10246
8bf5b6a9
PM
10247 /* The page table entries may downgrade secure to non-secure, but
10248 * cannot upgrade an non-secure translation regime's attributes
10249 * to secure.
10250 */
10251 attrs->secure = regime_is_secure(env, mmu_idx);
0995bf8c 10252 attrs->user = regime_is_user(env, mmu_idx);
8bf5b6a9 10253
0480f69a
PM
10254 /* Fast Context Switch Extension. This doesn't exist at all in v8.
10255 * In v7 and earlier it affects all stage 1 translations.
10256 */
10257 if (address < 0x02000000 && mmu_idx != ARMMMUIdx_S2NS
10258 && !arm_feature(env, ARM_FEATURE_V8)) {
10259 if (regime_el(env, mmu_idx) == 3) {
10260 address += env->cp15.fcseidr_s;
10261 } else {
10262 address += env->cp15.fcseidr_ns;
10263 }
54bf36ed 10264 }
9ee6e8bb 10265
3279adb9 10266 if (arm_feature(env, ARM_FEATURE_PMSA)) {
c9f9f124 10267 bool ret;
f6bda88f 10268 *page_size = TARGET_PAGE_SIZE;
3279adb9 10269
504e3cc3
PM
10270 if (arm_feature(env, ARM_FEATURE_V8)) {
10271 /* PMSAv8 */
10272 ret = get_phys_addr_pmsav8(env, address, access_type, mmu_idx,
3f551b5b 10273 phys_ptr, attrs, prot, fi);
504e3cc3 10274 } else if (arm_feature(env, ARM_FEATURE_V7)) {
3279adb9
PM
10275 /* PMSAv7 */
10276 ret = get_phys_addr_pmsav7(env, address, access_type, mmu_idx,
9375ad15 10277 phys_ptr, prot, fi);
3279adb9
PM
10278 } else {
10279 /* Pre-v7 MPU */
10280 ret = get_phys_addr_pmsav5(env, address, access_type, mmu_idx,
53a4e5c5 10281 phys_ptr, prot, fi);
3279adb9
PM
10282 }
10283 qemu_log_mask(CPU_LOG_MMU, "PMSA MPU lookup for %s at 0x%08" PRIx32
c9f9f124 10284 " mmu_idx %u -> %s (prot %c%c%c)\n",
709e4407
PM
10285 access_type == MMU_DATA_LOAD ? "reading" :
10286 (access_type == MMU_DATA_STORE ? "writing" : "execute"),
c9f9f124
MD
10287 (uint32_t)address, mmu_idx,
10288 ret ? "Miss" : "Hit",
10289 *prot & PAGE_READ ? 'r' : '-',
10290 *prot & PAGE_WRITE ? 'w' : '-',
10291 *prot & PAGE_EXEC ? 'x' : '-');
10292
10293 return ret;
f6bda88f
PC
10294 }
10295
3279adb9
PM
10296 /* Definitely a real MMU, not an MPU */
10297
0480f69a 10298 if (regime_translation_disabled(env, mmu_idx)) {
3279adb9 10299 /* MMU disabled. */
9ee6e8bb 10300 *phys_ptr = address;
3ad493fc 10301 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
d4c430a8 10302 *page_size = TARGET_PAGE_SIZE;
9ee6e8bb 10303 return 0;
0480f69a
PM
10304 }
10305
0480f69a 10306 if (regime_using_lpae_format(env, mmu_idx)) {
bc52bfeb
PM
10307 return get_phys_addr_lpae(env, address, access_type, mmu_idx,
10308 phys_ptr, attrs, prot, page_size,
10309 fi, cacheattrs);
0480f69a 10310 } else if (regime_sctlr(env, mmu_idx) & SCTLR_XP) {
bc52bfeb
PM
10311 return get_phys_addr_v6(env, address, access_type, mmu_idx,
10312 phys_ptr, attrs, prot, page_size, fi);
9ee6e8bb 10313 } else {
bc52bfeb 10314 return get_phys_addr_v5(env, address, access_type, mmu_idx,
f989983e 10315 phys_ptr, prot, page_size, fi);
9ee6e8bb
PB
10316 }
10317}
10318
8c6084bf 10319/* Walk the page table and (if the mapping exists) add the page
b7cc4e82
PC
10320 * to the TLB. Return false on success, or true on failure. Populate
10321 * fsr with ARM DFSR/IFSR fault register format value on failure.
8c6084bf 10322 */
b7cc4e82 10323bool arm_tlb_fill(CPUState *cs, vaddr address,
bc52bfeb 10324 MMUAccessType access_type, int mmu_idx,
e14b5a23 10325 ARMMMUFaultInfo *fi)
b5ff1b31 10326{
7510454e
AF
10327 ARMCPU *cpu = ARM_CPU(cs);
10328 CPUARMState *env = &cpu->env;
a8170e5e 10329 hwaddr phys_addr;
d4c430a8 10330 target_ulong page_size;
b5ff1b31 10331 int prot;
d3649702 10332 int ret;
8bf5b6a9 10333 MemTxAttrs attrs = {};
b5ff1b31 10334
8bd5c820
PM
10335 ret = get_phys_addr(env, address, access_type,
10336 core_to_arm_mmu_idx(env, mmu_idx), &phys_addr,
bc52bfeb 10337 &attrs, &prot, &page_size, fi, NULL);
b7cc4e82 10338 if (!ret) {
b5ff1b31 10339 /* Map a single [sub]page. */
dcd82c11
AB
10340 phys_addr &= TARGET_PAGE_MASK;
10341 address &= TARGET_PAGE_MASK;
8bf5b6a9
PM
10342 tlb_set_page_with_attrs(cs, address, phys_addr, attrs,
10343 prot, mmu_idx, page_size);
d4c430a8 10344 return 0;
b5ff1b31
FB
10345 }
10346
8c6084bf 10347 return ret;
b5ff1b31
FB
10348}
10349
0faea0c7
PM
10350hwaddr arm_cpu_get_phys_page_attrs_debug(CPUState *cs, vaddr addr,
10351 MemTxAttrs *attrs)
b5ff1b31 10352{
00b941e5 10353 ARMCPU *cpu = ARM_CPU(cs);
d3649702 10354 CPUARMState *env = &cpu->env;
a8170e5e 10355 hwaddr phys_addr;
d4c430a8 10356 target_ulong page_size;
b5ff1b31 10357 int prot;
b7cc4e82 10358 bool ret;
e14b5a23 10359 ARMMMUFaultInfo fi = {};
8bd5c820 10360 ARMMMUIdx mmu_idx = core_to_arm_mmu_idx(env, cpu_mmu_index(env, false));
b5ff1b31 10361
0faea0c7
PM
10362 *attrs = (MemTxAttrs) {};
10363
8bd5c820 10364 ret = get_phys_addr(env, addr, 0, mmu_idx, &phys_addr,
bc52bfeb 10365 attrs, &prot, &page_size, &fi, NULL);
b5ff1b31 10366
b7cc4e82 10367 if (ret) {
b5ff1b31 10368 return -1;
00b941e5 10369 }
b5ff1b31
FB
10370 return phys_addr;
10371}
10372
0ecb72a5 10373uint32_t HELPER(v7m_mrs)(CPUARMState *env, uint32_t reg)
9ee6e8bb 10374{
58117c9b
MD
10375 uint32_t mask;
10376 unsigned el = arm_current_el(env);
10377
10378 /* First handle registers which unprivileged can read */
10379
10380 switch (reg) {
10381 case 0 ... 7: /* xPSR sub-fields */
10382 mask = 0;
10383 if ((reg & 1) && el) {
987ab45e 10384 mask |= XPSR_EXCP; /* IPSR (unpriv. reads as zero) */
58117c9b
MD
10385 }
10386 if (!(reg & 4)) {
987ab45e 10387 mask |= XPSR_NZCV | XPSR_Q; /* APSR */
58117c9b
MD
10388 }
10389 /* EPSR reads as zero */
10390 return xpsr_read(env) & mask;
10391 break;
10392 case 20: /* CONTROL */
8bfc26ea 10393 return env->v7m.control[env->v7m.secure];
50f11062
PM
10394 case 0x94: /* CONTROL_NS */
10395 /* We have to handle this here because unprivileged Secure code
10396 * can read the NS CONTROL register.
10397 */
10398 if (!env->v7m.secure) {
10399 return 0;
10400 }
10401 return env->v7m.control[M_REG_NS];
58117c9b
MD
10402 }
10403
10404 if (el == 0) {
10405 return 0; /* unprivileged reads others as zero */
10406 }
a47dddd7 10407
50f11062
PM
10408 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
10409 switch (reg) {
10410 case 0x88: /* MSP_NS */
10411 if (!env->v7m.secure) {
10412 return 0;
10413 }
10414 return env->v7m.other_ss_msp;
10415 case 0x89: /* PSP_NS */
10416 if (!env->v7m.secure) {
10417 return 0;
10418 }
10419 return env->v7m.other_ss_psp;
10420 case 0x90: /* PRIMASK_NS */
10421 if (!env->v7m.secure) {
10422 return 0;
10423 }
10424 return env->v7m.primask[M_REG_NS];
10425 case 0x91: /* BASEPRI_NS */
10426 if (!env->v7m.secure) {
10427 return 0;
10428 }
10429 return env->v7m.basepri[M_REG_NS];
10430 case 0x93: /* FAULTMASK_NS */
10431 if (!env->v7m.secure) {
10432 return 0;
10433 }
10434 return env->v7m.faultmask[M_REG_NS];
10435 case 0x98: /* SP_NS */
10436 {
10437 /* This gives the non-secure SP selected based on whether we're
10438 * currently in handler mode or not, using the NS CONTROL.SPSEL.
10439 */
10440 bool spsel = env->v7m.control[M_REG_NS] & R_V7M_CONTROL_SPSEL_MASK;
10441
10442 if (!env->v7m.secure) {
10443 return 0;
10444 }
10445 if (!arm_v7m_is_handler_mode(env) && spsel) {
10446 return env->v7m.other_ss_psp;
10447 } else {
10448 return env->v7m.other_ss_msp;
10449 }
10450 }
10451 default:
10452 break;
10453 }
10454 }
10455
9ee6e8bb 10456 switch (reg) {
9ee6e8bb 10457 case 8: /* MSP */
1169d3aa 10458 return v7m_using_psp(env) ? env->v7m.other_sp : env->regs[13];
9ee6e8bb 10459 case 9: /* PSP */
1169d3aa 10460 return v7m_using_psp(env) ? env->regs[13] : env->v7m.other_sp;
9ee6e8bb 10461 case 16: /* PRIMASK */
6d804834 10462 return env->v7m.primask[env->v7m.secure];
82845826
SH
10463 case 17: /* BASEPRI */
10464 case 18: /* BASEPRI_MAX */
acf94941 10465 return env->v7m.basepri[env->v7m.secure];
82845826 10466 case 19: /* FAULTMASK */
42a6686b 10467 return env->v7m.faultmask[env->v7m.secure];
9ee6e8bb 10468 default:
58117c9b
MD
10469 qemu_log_mask(LOG_GUEST_ERROR, "Attempt to read unknown special"
10470 " register %d\n", reg);
9ee6e8bb
PB
10471 return 0;
10472 }
10473}
10474
b28b3377
PM
10475void HELPER(v7m_msr)(CPUARMState *env, uint32_t maskreg, uint32_t val)
10476{
10477 /* We're passed bits [11..0] of the instruction; extract
10478 * SYSm and the mask bits.
10479 * Invalid combinations of SYSm and mask are UNPREDICTABLE;
10480 * we choose to treat them as if the mask bits were valid.
10481 * NB that the pseudocode 'mask' variable is bits [11..10],
10482 * whereas ours is [11..8].
10483 */
10484 uint32_t mask = extract32(maskreg, 8, 4);
10485 uint32_t reg = extract32(maskreg, 0, 8);
10486
58117c9b
MD
10487 if (arm_current_el(env) == 0 && reg > 7) {
10488 /* only xPSR sub-fields may be written by unprivileged */
10489 return;
10490 }
a47dddd7 10491
50f11062
PM
10492 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
10493 switch (reg) {
10494 case 0x88: /* MSP_NS */
10495 if (!env->v7m.secure) {
10496 return;
10497 }
10498 env->v7m.other_ss_msp = val;
10499 return;
10500 case 0x89: /* PSP_NS */
10501 if (!env->v7m.secure) {
10502 return;
10503 }
10504 env->v7m.other_ss_psp = val;
10505 return;
10506 case 0x90: /* PRIMASK_NS */
10507 if (!env->v7m.secure) {
10508 return;
10509 }
10510 env->v7m.primask[M_REG_NS] = val & 1;
10511 return;
10512 case 0x91: /* BASEPRI_NS */
10513 if (!env->v7m.secure) {
10514 return;
10515 }
10516 env->v7m.basepri[M_REG_NS] = val & 0xff;
10517 return;
10518 case 0x93: /* FAULTMASK_NS */
10519 if (!env->v7m.secure) {
10520 return;
10521 }
10522 env->v7m.faultmask[M_REG_NS] = val & 1;
10523 return;
10524 case 0x98: /* SP_NS */
10525 {
10526 /* This gives the non-secure SP selected based on whether we're
10527 * currently in handler mode or not, using the NS CONTROL.SPSEL.
10528 */
10529 bool spsel = env->v7m.control[M_REG_NS] & R_V7M_CONTROL_SPSEL_MASK;
10530
10531 if (!env->v7m.secure) {
10532 return;
10533 }
10534 if (!arm_v7m_is_handler_mode(env) && spsel) {
10535 env->v7m.other_ss_psp = val;
10536 } else {
10537 env->v7m.other_ss_msp = val;
10538 }
10539 return;
10540 }
10541 default:
10542 break;
10543 }
10544 }
10545
9ee6e8bb 10546 switch (reg) {
58117c9b
MD
10547 case 0 ... 7: /* xPSR sub-fields */
10548 /* only APSR is actually writable */
b28b3377
PM
10549 if (!(reg & 4)) {
10550 uint32_t apsrmask = 0;
10551
10552 if (mask & 8) {
987ab45e 10553 apsrmask |= XPSR_NZCV | XPSR_Q;
b28b3377
PM
10554 }
10555 if ((mask & 4) && arm_feature(env, ARM_FEATURE_THUMB_DSP)) {
987ab45e 10556 apsrmask |= XPSR_GE;
b28b3377
PM
10557 }
10558 xpsr_write(env, val, apsrmask);
58117c9b 10559 }
9ee6e8bb
PB
10560 break;
10561 case 8: /* MSP */
1169d3aa 10562 if (v7m_using_psp(env)) {
9ee6e8bb 10563 env->v7m.other_sp = val;
abc24d86 10564 } else {
9ee6e8bb 10565 env->regs[13] = val;
abc24d86 10566 }
9ee6e8bb
PB
10567 break;
10568 case 9: /* PSP */
1169d3aa 10569 if (v7m_using_psp(env)) {
9ee6e8bb 10570 env->regs[13] = val;
abc24d86 10571 } else {
9ee6e8bb 10572 env->v7m.other_sp = val;
abc24d86 10573 }
9ee6e8bb
PB
10574 break;
10575 case 16: /* PRIMASK */
6d804834 10576 env->v7m.primask[env->v7m.secure] = val & 1;
9ee6e8bb 10577 break;
82845826 10578 case 17: /* BASEPRI */
acf94941 10579 env->v7m.basepri[env->v7m.secure] = val & 0xff;
9ee6e8bb 10580 break;
82845826 10581 case 18: /* BASEPRI_MAX */
9ee6e8bb 10582 val &= 0xff;
acf94941
PM
10583 if (val != 0 && (val < env->v7m.basepri[env->v7m.secure]
10584 || env->v7m.basepri[env->v7m.secure] == 0)) {
10585 env->v7m.basepri[env->v7m.secure] = val;
10586 }
9ee6e8bb 10587 break;
82845826 10588 case 19: /* FAULTMASK */
42a6686b 10589 env->v7m.faultmask[env->v7m.secure] = val & 1;
82845826 10590 break;
9ee6e8bb 10591 case 20: /* CONTROL */
792dac30
PM
10592 /* Writing to the SPSEL bit only has an effect if we are in
10593 * thread mode; other bits can be updated by any privileged code.
de2db7ec 10594 * write_v7m_control_spsel() deals with updating the SPSEL bit in
792dac30 10595 * env->v7m.control, so we only need update the others.
83d7f86d
PM
10596 * For v7M, we must just ignore explicit writes to SPSEL in handler
10597 * mode; for v8M the write is permitted but will have no effect.
792dac30 10598 */
83d7f86d
PM
10599 if (arm_feature(env, ARM_FEATURE_V8) ||
10600 !arm_v7m_is_handler_mode(env)) {
de2db7ec 10601 write_v7m_control_spsel(env, (val & R_V7M_CONTROL_SPSEL_MASK) != 0);
792dac30 10602 }
8bfc26ea
PM
10603 env->v7m.control[env->v7m.secure] &= ~R_V7M_CONTROL_NPRIV_MASK;
10604 env->v7m.control[env->v7m.secure] |= val & R_V7M_CONTROL_NPRIV_MASK;
9ee6e8bb
PB
10605 break;
10606 default:
58117c9b
MD
10607 qemu_log_mask(LOG_GUEST_ERROR, "Attempt to write unknown special"
10608 " register %d\n", reg);
9ee6e8bb
PB
10609 return;
10610 }
10611}
10612
5158de24
PM
10613uint32_t HELPER(v7m_tt)(CPUARMState *env, uint32_t addr, uint32_t op)
10614{
10615 /* Implement the TT instruction. op is bits [7:6] of the insn. */
10616 bool forceunpriv = op & 1;
10617 bool alt = op & 2;
10618 V8M_SAttributes sattrs = {};
10619 uint32_t tt_resp;
10620 bool r, rw, nsr, nsrw, mrvalid;
10621 int prot;
3f551b5b 10622 ARMMMUFaultInfo fi = {};
5158de24
PM
10623 MemTxAttrs attrs = {};
10624 hwaddr phys_addr;
5158de24
PM
10625 ARMMMUIdx mmu_idx;
10626 uint32_t mregion;
10627 bool targetpriv;
10628 bool targetsec = env->v7m.secure;
10629
10630 /* Work out what the security state and privilege level we're
10631 * interested in is...
10632 */
10633 if (alt) {
10634 targetsec = !targetsec;
10635 }
10636
10637 if (forceunpriv) {
10638 targetpriv = false;
10639 } else {
10640 targetpriv = arm_v7m_is_handler_mode(env) ||
10641 !(env->v7m.control[targetsec] & R_V7M_CONTROL_NPRIV_MASK);
10642 }
10643
10644 /* ...and then figure out which MMU index this is */
10645 mmu_idx = arm_v7m_mmu_idx_for_secstate_and_priv(env, targetsec, targetpriv);
10646
10647 /* We know that the MPU and SAU don't care about the access type
10648 * for our purposes beyond that we don't want to claim to be
10649 * an insn fetch, so we arbitrarily call this a read.
10650 */
10651
10652 /* MPU region info only available for privileged or if
10653 * inspecting the other MPU state.
10654 */
10655 if (arm_current_el(env) != 0 || alt) {
10656 /* We can ignore the return value as prot is always set */
10657 pmsav8_mpu_lookup(env, addr, MMU_DATA_LOAD, mmu_idx,
3f551b5b 10658 &phys_addr, &attrs, &prot, &fi, &mregion);
5158de24
PM
10659 if (mregion == -1) {
10660 mrvalid = false;
10661 mregion = 0;
10662 } else {
10663 mrvalid = true;
10664 }
10665 r = prot & PAGE_READ;
10666 rw = prot & PAGE_WRITE;
10667 } else {
10668 r = false;
10669 rw = false;
10670 mrvalid = false;
10671 mregion = 0;
10672 }
10673
10674 if (env->v7m.secure) {
10675 v8m_security_lookup(env, addr, MMU_DATA_LOAD, mmu_idx, &sattrs);
10676 nsr = sattrs.ns && r;
10677 nsrw = sattrs.ns && rw;
10678 } else {
10679 sattrs.ns = true;
10680 nsr = false;
10681 nsrw = false;
10682 }
10683
10684 tt_resp = (sattrs.iregion << 24) |
10685 (sattrs.irvalid << 23) |
10686 ((!sattrs.ns) << 22) |
10687 (nsrw << 21) |
10688 (nsr << 20) |
10689 (rw << 19) |
10690 (r << 18) |
10691 (sattrs.srvalid << 17) |
10692 (mrvalid << 16) |
10693 (sattrs.sregion << 8) |
10694 mregion;
10695
10696 return tt_resp;
10697}
10698
b5ff1b31 10699#endif
6ddbc6e4 10700
aca3f40b
PM
10701void HELPER(dc_zva)(CPUARMState *env, uint64_t vaddr_in)
10702{
10703 /* Implement DC ZVA, which zeroes a fixed-length block of memory.
10704 * Note that we do not implement the (architecturally mandated)
10705 * alignment fault for attempts to use this on Device memory
10706 * (which matches the usual QEMU behaviour of not implementing either
10707 * alignment faults or any memory attribute handling).
10708 */
10709
10710 ARMCPU *cpu = arm_env_get_cpu(env);
10711 uint64_t blocklen = 4 << cpu->dcz_blocksize;
10712 uint64_t vaddr = vaddr_in & ~(blocklen - 1);
10713
10714#ifndef CONFIG_USER_ONLY
10715 {
10716 /* Slightly awkwardly, QEMU's TARGET_PAGE_SIZE may be less than
10717 * the block size so we might have to do more than one TLB lookup.
10718 * We know that in fact for any v8 CPU the page size is at least 4K
10719 * and the block size must be 2K or less, but TARGET_PAGE_SIZE is only
10720 * 1K as an artefact of legacy v5 subpage support being present in the
10721 * same QEMU executable.
10722 */
10723 int maxidx = DIV_ROUND_UP(blocklen, TARGET_PAGE_SIZE);
10724 void *hostaddr[maxidx];
10725 int try, i;
97ed5ccd 10726 unsigned mmu_idx = cpu_mmu_index(env, false);
3972ef6f 10727 TCGMemOpIdx oi = make_memop_idx(MO_UB, mmu_idx);
aca3f40b
PM
10728
10729 for (try = 0; try < 2; try++) {
10730
10731 for (i = 0; i < maxidx; i++) {
10732 hostaddr[i] = tlb_vaddr_to_host(env,
10733 vaddr + TARGET_PAGE_SIZE * i,
3972ef6f 10734 1, mmu_idx);
aca3f40b
PM
10735 if (!hostaddr[i]) {
10736 break;
10737 }
10738 }
10739 if (i == maxidx) {
10740 /* If it's all in the TLB it's fair game for just writing to;
10741 * we know we don't need to update dirty status, etc.
10742 */
10743 for (i = 0; i < maxidx - 1; i++) {
10744 memset(hostaddr[i], 0, TARGET_PAGE_SIZE);
10745 }
10746 memset(hostaddr[i], 0, blocklen - (i * TARGET_PAGE_SIZE));
10747 return;
10748 }
10749 /* OK, try a store and see if we can populate the tlb. This
10750 * might cause an exception if the memory isn't writable,
10751 * in which case we will longjmp out of here. We must for
10752 * this purpose use the actual register value passed to us
10753 * so that we get the fault address right.
10754 */
01ecaf43 10755 helper_ret_stb_mmu(env, vaddr_in, 0, oi, GETPC());
aca3f40b
PM
10756 /* Now we can populate the other TLB entries, if any */
10757 for (i = 0; i < maxidx; i++) {
10758 uint64_t va = vaddr + TARGET_PAGE_SIZE * i;
10759 if (va != (vaddr_in & TARGET_PAGE_MASK)) {
01ecaf43 10760 helper_ret_stb_mmu(env, va, 0, oi, GETPC());
aca3f40b
PM
10761 }
10762 }
10763 }
10764
10765 /* Slow path (probably attempt to do this to an I/O device or
10766 * similar, or clearing of a block of code we have translations
10767 * cached for). Just do a series of byte writes as the architecture
10768 * demands. It's not worth trying to use a cpu_physical_memory_map(),
10769 * memset(), unmap() sequence here because:
10770 * + we'd need to account for the blocksize being larger than a page
10771 * + the direct-RAM access case is almost always going to be dealt
10772 * with in the fastpath code above, so there's no speed benefit
10773 * + we would have to deal with the map returning NULL because the
10774 * bounce buffer was in use
10775 */
10776 for (i = 0; i < blocklen; i++) {
01ecaf43 10777 helper_ret_stb_mmu(env, vaddr + i, 0, oi, GETPC());
aca3f40b
PM
10778 }
10779 }
10780#else
10781 memset(g2h(vaddr), 0, blocklen);
10782#endif
10783}
10784
6ddbc6e4
PB
10785/* Note that signed overflow is undefined in C. The following routines are
10786 careful to use unsigned types where modulo arithmetic is required.
10787 Failure to do so _will_ break on newer gcc. */
10788
10789/* Signed saturating arithmetic. */
10790
1654b2d6 10791/* Perform 16-bit signed saturating addition. */
6ddbc6e4
PB
10792static inline uint16_t add16_sat(uint16_t a, uint16_t b)
10793{
10794 uint16_t res;
10795
10796 res = a + b;
10797 if (((res ^ a) & 0x8000) && !((a ^ b) & 0x8000)) {
10798 if (a & 0x8000)
10799 res = 0x8000;
10800 else
10801 res = 0x7fff;
10802 }
10803 return res;
10804}
10805
1654b2d6 10806/* Perform 8-bit signed saturating addition. */
6ddbc6e4
PB
10807static inline uint8_t add8_sat(uint8_t a, uint8_t b)
10808{
10809 uint8_t res;
10810
10811 res = a + b;
10812 if (((res ^ a) & 0x80) && !((a ^ b) & 0x80)) {
10813 if (a & 0x80)
10814 res = 0x80;
10815 else
10816 res = 0x7f;
10817 }
10818 return res;
10819}
10820
1654b2d6 10821/* Perform 16-bit signed saturating subtraction. */
6ddbc6e4
PB
10822static inline uint16_t sub16_sat(uint16_t a, uint16_t b)
10823{
10824 uint16_t res;
10825
10826 res = a - b;
10827 if (((res ^ a) & 0x8000) && ((a ^ b) & 0x8000)) {
10828 if (a & 0x8000)
10829 res = 0x8000;
10830 else
10831 res = 0x7fff;
10832 }
10833 return res;
10834}
10835
1654b2d6 10836/* Perform 8-bit signed saturating subtraction. */
6ddbc6e4
PB
10837static inline uint8_t sub8_sat(uint8_t a, uint8_t b)
10838{
10839 uint8_t res;
10840
10841 res = a - b;
10842 if (((res ^ a) & 0x80) && ((a ^ b) & 0x80)) {
10843 if (a & 0x80)
10844 res = 0x80;
10845 else
10846 res = 0x7f;
10847 }
10848 return res;
10849}
10850
10851#define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16);
10852#define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16);
10853#define ADD8(a, b, n) RESULT(add8_sat(a, b), n, 8);
10854#define SUB8(a, b, n) RESULT(sub8_sat(a, b), n, 8);
10855#define PFX q
10856
10857#include "op_addsub.h"
10858
10859/* Unsigned saturating arithmetic. */
460a09c1 10860static inline uint16_t add16_usat(uint16_t a, uint16_t b)
6ddbc6e4
PB
10861{
10862 uint16_t res;
10863 res = a + b;
10864 if (res < a)
10865 res = 0xffff;
10866 return res;
10867}
10868
460a09c1 10869static inline uint16_t sub16_usat(uint16_t a, uint16_t b)
6ddbc6e4 10870{
4c4fd3f8 10871 if (a > b)
6ddbc6e4
PB
10872 return a - b;
10873 else
10874 return 0;
10875}
10876
10877static inline uint8_t add8_usat(uint8_t a, uint8_t b)
10878{
10879 uint8_t res;
10880 res = a + b;
10881 if (res < a)
10882 res = 0xff;
10883 return res;
10884}
10885
10886static inline uint8_t sub8_usat(uint8_t a, uint8_t b)
10887{
4c4fd3f8 10888 if (a > b)
6ddbc6e4
PB
10889 return a - b;
10890 else
10891 return 0;
10892}
10893
10894#define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16);
10895#define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16);
10896#define ADD8(a, b, n) RESULT(add8_usat(a, b), n, 8);
10897#define SUB8(a, b, n) RESULT(sub8_usat(a, b), n, 8);
10898#define PFX uq
10899
10900#include "op_addsub.h"
10901
10902/* Signed modulo arithmetic. */
10903#define SARITH16(a, b, n, op) do { \
10904 int32_t sum; \
db6e2e65 10905 sum = (int32_t)(int16_t)(a) op (int32_t)(int16_t)(b); \
6ddbc6e4
PB
10906 RESULT(sum, n, 16); \
10907 if (sum >= 0) \
10908 ge |= 3 << (n * 2); \
10909 } while(0)
10910
10911#define SARITH8(a, b, n, op) do { \
10912 int32_t sum; \
db6e2e65 10913 sum = (int32_t)(int8_t)(a) op (int32_t)(int8_t)(b); \
6ddbc6e4
PB
10914 RESULT(sum, n, 8); \
10915 if (sum >= 0) \
10916 ge |= 1 << n; \
10917 } while(0)
10918
10919
10920#define ADD16(a, b, n) SARITH16(a, b, n, +)
10921#define SUB16(a, b, n) SARITH16(a, b, n, -)
10922#define ADD8(a, b, n) SARITH8(a, b, n, +)
10923#define SUB8(a, b, n) SARITH8(a, b, n, -)
10924#define PFX s
10925#define ARITH_GE
10926
10927#include "op_addsub.h"
10928
10929/* Unsigned modulo arithmetic. */
10930#define ADD16(a, b, n) do { \
10931 uint32_t sum; \
10932 sum = (uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b); \
10933 RESULT(sum, n, 16); \
a87aa10b 10934 if ((sum >> 16) == 1) \
6ddbc6e4
PB
10935 ge |= 3 << (n * 2); \
10936 } while(0)
10937
10938#define ADD8(a, b, n) do { \
10939 uint32_t sum; \
10940 sum = (uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b); \
10941 RESULT(sum, n, 8); \
a87aa10b
AZ
10942 if ((sum >> 8) == 1) \
10943 ge |= 1 << n; \
6ddbc6e4
PB
10944 } while(0)
10945
10946#define SUB16(a, b, n) do { \
10947 uint32_t sum; \
10948 sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \
10949 RESULT(sum, n, 16); \
10950 if ((sum >> 16) == 0) \
10951 ge |= 3 << (n * 2); \
10952 } while(0)
10953
10954#define SUB8(a, b, n) do { \
10955 uint32_t sum; \
10956 sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \
10957 RESULT(sum, n, 8); \
10958 if ((sum >> 8) == 0) \
a87aa10b 10959 ge |= 1 << n; \
6ddbc6e4
PB
10960 } while(0)
10961
10962#define PFX u
10963#define ARITH_GE
10964
10965#include "op_addsub.h"
10966
10967/* Halved signed arithmetic. */
10968#define ADD16(a, b, n) \
10969 RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16)
10970#define SUB16(a, b, n) \
10971 RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16)
10972#define ADD8(a, b, n) \
10973 RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8)
10974#define SUB8(a, b, n) \
10975 RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8)
10976#define PFX sh
10977
10978#include "op_addsub.h"
10979
10980/* Halved unsigned arithmetic. */
10981#define ADD16(a, b, n) \
10982 RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16)
10983#define SUB16(a, b, n) \
10984 RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16)
10985#define ADD8(a, b, n) \
10986 RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8)
10987#define SUB8(a, b, n) \
10988 RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8)
10989#define PFX uh
10990
10991#include "op_addsub.h"
10992
10993static inline uint8_t do_usad(uint8_t a, uint8_t b)
10994{
10995 if (a > b)
10996 return a - b;
10997 else
10998 return b - a;
10999}
11000
11001/* Unsigned sum of absolute byte differences. */
11002uint32_t HELPER(usad8)(uint32_t a, uint32_t b)
11003{
11004 uint32_t sum;
11005 sum = do_usad(a, b);
11006 sum += do_usad(a >> 8, b >> 8);
11007 sum += do_usad(a >> 16, b >>16);
11008 sum += do_usad(a >> 24, b >> 24);
11009 return sum;
11010}
11011
11012/* For ARMv6 SEL instruction. */
11013uint32_t HELPER(sel_flags)(uint32_t flags, uint32_t a, uint32_t b)
11014{
11015 uint32_t mask;
11016
11017 mask = 0;
11018 if (flags & 1)
11019 mask |= 0xff;
11020 if (flags & 2)
11021 mask |= 0xff00;
11022 if (flags & 4)
11023 mask |= 0xff0000;
11024 if (flags & 8)
11025 mask |= 0xff000000;
11026 return (a & mask) | (b & ~mask);
11027}
11028
b90372ad
PM
11029/* VFP support. We follow the convention used for VFP instructions:
11030 Single precision routines have a "s" suffix, double precision a
4373f3ce
PB
11031 "d" suffix. */
11032
11033/* Convert host exception flags to vfp form. */
11034static inline int vfp_exceptbits_from_host(int host_bits)
11035{
11036 int target_bits = 0;
11037
11038 if (host_bits & float_flag_invalid)
11039 target_bits |= 1;
11040 if (host_bits & float_flag_divbyzero)
11041 target_bits |= 2;
11042 if (host_bits & float_flag_overflow)
11043 target_bits |= 4;
36802b6b 11044 if (host_bits & (float_flag_underflow | float_flag_output_denormal))
4373f3ce
PB
11045 target_bits |= 8;
11046 if (host_bits & float_flag_inexact)
11047 target_bits |= 0x10;
cecd8504
PM
11048 if (host_bits & float_flag_input_denormal)
11049 target_bits |= 0x80;
4373f3ce
PB
11050 return target_bits;
11051}
11052
0ecb72a5 11053uint32_t HELPER(vfp_get_fpscr)(CPUARMState *env)
4373f3ce
PB
11054{
11055 int i;
11056 uint32_t fpscr;
11057
11058 fpscr = (env->vfp.xregs[ARM_VFP_FPSCR] & 0xffc8ffff)
11059 | (env->vfp.vec_len << 16)
11060 | (env->vfp.vec_stride << 20);
11061 i = get_float_exception_flags(&env->vfp.fp_status);
3a492f3a 11062 i |= get_float_exception_flags(&env->vfp.standard_fp_status);
4373f3ce
PB
11063 fpscr |= vfp_exceptbits_from_host(i);
11064 return fpscr;
11065}
11066
0ecb72a5 11067uint32_t vfp_get_fpscr(CPUARMState *env)
01653295
PM
11068{
11069 return HELPER(vfp_get_fpscr)(env);
11070}
11071
4373f3ce
PB
11072/* Convert vfp exception flags to target form. */
11073static inline int vfp_exceptbits_to_host(int target_bits)
11074{
11075 int host_bits = 0;
11076
11077 if (target_bits & 1)
11078 host_bits |= float_flag_invalid;
11079 if (target_bits & 2)
11080 host_bits |= float_flag_divbyzero;
11081 if (target_bits & 4)
11082 host_bits |= float_flag_overflow;
11083 if (target_bits & 8)
11084 host_bits |= float_flag_underflow;
11085 if (target_bits & 0x10)
11086 host_bits |= float_flag_inexact;
cecd8504
PM
11087 if (target_bits & 0x80)
11088 host_bits |= float_flag_input_denormal;
4373f3ce
PB
11089 return host_bits;
11090}
11091
0ecb72a5 11092void HELPER(vfp_set_fpscr)(CPUARMState *env, uint32_t val)
4373f3ce
PB
11093{
11094 int i;
11095 uint32_t changed;
11096
11097 changed = env->vfp.xregs[ARM_VFP_FPSCR];
11098 env->vfp.xregs[ARM_VFP_FPSCR] = (val & 0xffc8ffff);
11099 env->vfp.vec_len = (val >> 16) & 7;
11100 env->vfp.vec_stride = (val >> 20) & 3;
11101
11102 changed ^= val;
11103 if (changed & (3 << 22)) {
11104 i = (val >> 22) & 3;
11105 switch (i) {
4d3da0f3 11106 case FPROUNDING_TIEEVEN:
4373f3ce
PB
11107 i = float_round_nearest_even;
11108 break;
4d3da0f3 11109 case FPROUNDING_POSINF:
4373f3ce
PB
11110 i = float_round_up;
11111 break;
4d3da0f3 11112 case FPROUNDING_NEGINF:
4373f3ce
PB
11113 i = float_round_down;
11114 break;
4d3da0f3 11115 case FPROUNDING_ZERO:
4373f3ce
PB
11116 i = float_round_to_zero;
11117 break;
11118 }
11119 set_float_rounding_mode(i, &env->vfp.fp_status);
11120 }
cecd8504 11121 if (changed & (1 << 24)) {
fe76d976 11122 set_flush_to_zero((val & (1 << 24)) != 0, &env->vfp.fp_status);
cecd8504
PM
11123 set_flush_inputs_to_zero((val & (1 << 24)) != 0, &env->vfp.fp_status);
11124 }
5c7908ed
PB
11125 if (changed & (1 << 25))
11126 set_default_nan_mode((val & (1 << 25)) != 0, &env->vfp.fp_status);
4373f3ce 11127
b12c390b 11128 i = vfp_exceptbits_to_host(val);
4373f3ce 11129 set_float_exception_flags(i, &env->vfp.fp_status);
3a492f3a 11130 set_float_exception_flags(0, &env->vfp.standard_fp_status);
4373f3ce
PB
11131}
11132
0ecb72a5 11133void vfp_set_fpscr(CPUARMState *env, uint32_t val)
01653295
PM
11134{
11135 HELPER(vfp_set_fpscr)(env, val);
11136}
11137
4373f3ce
PB
11138#define VFP_HELPER(name, p) HELPER(glue(glue(vfp_,name),p))
11139
11140#define VFP_BINOP(name) \
ae1857ec 11141float32 VFP_HELPER(name, s)(float32 a, float32 b, void *fpstp) \
4373f3ce 11142{ \
ae1857ec
PM
11143 float_status *fpst = fpstp; \
11144 return float32_ ## name(a, b, fpst); \
4373f3ce 11145} \
ae1857ec 11146float64 VFP_HELPER(name, d)(float64 a, float64 b, void *fpstp) \
4373f3ce 11147{ \
ae1857ec
PM
11148 float_status *fpst = fpstp; \
11149 return float64_ ## name(a, b, fpst); \
4373f3ce
PB
11150}
11151VFP_BINOP(add)
11152VFP_BINOP(sub)
11153VFP_BINOP(mul)
11154VFP_BINOP(div)
f71a2ae5
PM
11155VFP_BINOP(min)
11156VFP_BINOP(max)
11157VFP_BINOP(minnum)
11158VFP_BINOP(maxnum)
4373f3ce
PB
11159#undef VFP_BINOP
11160
11161float32 VFP_HELPER(neg, s)(float32 a)
11162{
11163 return float32_chs(a);
11164}
11165
11166float64 VFP_HELPER(neg, d)(float64 a)
11167{
66230e0d 11168 return float64_chs(a);
4373f3ce
PB
11169}
11170
11171float32 VFP_HELPER(abs, s)(float32 a)
11172{
11173 return float32_abs(a);
11174}
11175
11176float64 VFP_HELPER(abs, d)(float64 a)
11177{
66230e0d 11178 return float64_abs(a);
4373f3ce
PB
11179}
11180
0ecb72a5 11181float32 VFP_HELPER(sqrt, s)(float32 a, CPUARMState *env)
4373f3ce
PB
11182{
11183 return float32_sqrt(a, &env->vfp.fp_status);
11184}
11185
0ecb72a5 11186float64 VFP_HELPER(sqrt, d)(float64 a, CPUARMState *env)
4373f3ce
PB
11187{
11188 return float64_sqrt(a, &env->vfp.fp_status);
11189}
11190
11191/* XXX: check quiet/signaling case */
11192#define DO_VFP_cmp(p, type) \
0ecb72a5 11193void VFP_HELPER(cmp, p)(type a, type b, CPUARMState *env) \
4373f3ce
PB
11194{ \
11195 uint32_t flags; \
11196 switch(type ## _compare_quiet(a, b, &env->vfp.fp_status)) { \
11197 case 0: flags = 0x6; break; \
11198 case -1: flags = 0x8; break; \
11199 case 1: flags = 0x2; break; \
11200 default: case 2: flags = 0x3; break; \
11201 } \
11202 env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \
11203 | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \
11204} \
0ecb72a5 11205void VFP_HELPER(cmpe, p)(type a, type b, CPUARMState *env) \
4373f3ce
PB
11206{ \
11207 uint32_t flags; \
11208 switch(type ## _compare(a, b, &env->vfp.fp_status)) { \
11209 case 0: flags = 0x6; break; \
11210 case -1: flags = 0x8; break; \
11211 case 1: flags = 0x2; break; \
11212 default: case 2: flags = 0x3; break; \
11213 } \
11214 env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \
11215 | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \
11216}
11217DO_VFP_cmp(s, float32)
11218DO_VFP_cmp(d, float64)
11219#undef DO_VFP_cmp
11220
5500b06c 11221/* Integer to float and float to integer conversions */
4373f3ce 11222
5500b06c
PM
11223#define CONV_ITOF(name, fsz, sign) \
11224 float##fsz HELPER(name)(uint32_t x, void *fpstp) \
11225{ \
11226 float_status *fpst = fpstp; \
85836979 11227 return sign##int32_to_##float##fsz((sign##int32_t)x, fpst); \
4373f3ce
PB
11228}
11229
5500b06c
PM
11230#define CONV_FTOI(name, fsz, sign, round) \
11231uint32_t HELPER(name)(float##fsz x, void *fpstp) \
11232{ \
11233 float_status *fpst = fpstp; \
11234 if (float##fsz##_is_any_nan(x)) { \
11235 float_raise(float_flag_invalid, fpst); \
11236 return 0; \
11237 } \
11238 return float##fsz##_to_##sign##int32##round(x, fpst); \
4373f3ce
PB
11239}
11240
5500b06c
PM
11241#define FLOAT_CONVS(name, p, fsz, sign) \
11242CONV_ITOF(vfp_##name##to##p, fsz, sign) \
11243CONV_FTOI(vfp_to##name##p, fsz, sign, ) \
11244CONV_FTOI(vfp_to##name##z##p, fsz, sign, _round_to_zero)
4373f3ce 11245
5500b06c
PM
11246FLOAT_CONVS(si, s, 32, )
11247FLOAT_CONVS(si, d, 64, )
11248FLOAT_CONVS(ui, s, 32, u)
11249FLOAT_CONVS(ui, d, 64, u)
4373f3ce 11250
5500b06c
PM
11251#undef CONV_ITOF
11252#undef CONV_FTOI
11253#undef FLOAT_CONVS
4373f3ce
PB
11254
11255/* floating point conversion */
0ecb72a5 11256float64 VFP_HELPER(fcvtd, s)(float32 x, CPUARMState *env)
4373f3ce 11257{
2d627737
PM
11258 float64 r = float32_to_float64(x, &env->vfp.fp_status);
11259 /* ARM requires that S<->D conversion of any kind of NaN generates
11260 * a quiet NaN by forcing the most significant frac bit to 1.
11261 */
af39bc8c 11262 return float64_maybe_silence_nan(r, &env->vfp.fp_status);
4373f3ce
PB
11263}
11264
0ecb72a5 11265float32 VFP_HELPER(fcvts, d)(float64 x, CPUARMState *env)
4373f3ce 11266{
2d627737
PM
11267 float32 r = float64_to_float32(x, &env->vfp.fp_status);
11268 /* ARM requires that S<->D conversion of any kind of NaN generates
11269 * a quiet NaN by forcing the most significant frac bit to 1.
11270 */
af39bc8c 11271 return float32_maybe_silence_nan(r, &env->vfp.fp_status);
4373f3ce
PB
11272}
11273
11274/* VFP3 fixed point conversion. */
16d5b3ca 11275#define VFP_CONV_FIX_FLOAT(name, p, fsz, isz, itype) \
8ed697e8
WN
11276float##fsz HELPER(vfp_##name##to##p)(uint##isz##_t x, uint32_t shift, \
11277 void *fpstp) \
4373f3ce 11278{ \
5500b06c 11279 float_status *fpst = fpstp; \
622465e1 11280 float##fsz tmp; \
8ed697e8 11281 tmp = itype##_to_##float##fsz(x, fpst); \
5500b06c 11282 return float##fsz##_scalbn(tmp, -(int)shift, fpst); \
16d5b3ca
WN
11283}
11284
abe66f70
PM
11285/* Notice that we want only input-denormal exception flags from the
11286 * scalbn operation: the other possible flags (overflow+inexact if
11287 * we overflow to infinity, output-denormal) aren't correct for the
11288 * complete scale-and-convert operation.
11289 */
16d5b3ca
WN
11290#define VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, round) \
11291uint##isz##_t HELPER(vfp_to##name##p##round)(float##fsz x, \
11292 uint32_t shift, \
11293 void *fpstp) \
4373f3ce 11294{ \
5500b06c 11295 float_status *fpst = fpstp; \
abe66f70 11296 int old_exc_flags = get_float_exception_flags(fpst); \
622465e1
PM
11297 float##fsz tmp; \
11298 if (float##fsz##_is_any_nan(x)) { \
5500b06c 11299 float_raise(float_flag_invalid, fpst); \
622465e1 11300 return 0; \
09d9487f 11301 } \
5500b06c 11302 tmp = float##fsz##_scalbn(x, shift, fpst); \
abe66f70
PM
11303 old_exc_flags |= get_float_exception_flags(fpst) \
11304 & float_flag_input_denormal; \
11305 set_float_exception_flags(old_exc_flags, fpst); \
16d5b3ca 11306 return float##fsz##_to_##itype##round(tmp, fpst); \
622465e1
PM
11307}
11308
16d5b3ca
WN
11309#define VFP_CONV_FIX(name, p, fsz, isz, itype) \
11310VFP_CONV_FIX_FLOAT(name, p, fsz, isz, itype) \
3c6a074a
WN
11311VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, _round_to_zero) \
11312VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, )
11313
11314#define VFP_CONV_FIX_A64(name, p, fsz, isz, itype) \
11315VFP_CONV_FIX_FLOAT(name, p, fsz, isz, itype) \
11316VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, )
16d5b3ca 11317
8ed697e8
WN
11318VFP_CONV_FIX(sh, d, 64, 64, int16)
11319VFP_CONV_FIX(sl, d, 64, 64, int32)
3c6a074a 11320VFP_CONV_FIX_A64(sq, d, 64, 64, int64)
8ed697e8
WN
11321VFP_CONV_FIX(uh, d, 64, 64, uint16)
11322VFP_CONV_FIX(ul, d, 64, 64, uint32)
3c6a074a 11323VFP_CONV_FIX_A64(uq, d, 64, 64, uint64)
8ed697e8
WN
11324VFP_CONV_FIX(sh, s, 32, 32, int16)
11325VFP_CONV_FIX(sl, s, 32, 32, int32)
3c6a074a 11326VFP_CONV_FIX_A64(sq, s, 32, 64, int64)
8ed697e8
WN
11327VFP_CONV_FIX(uh, s, 32, 32, uint16)
11328VFP_CONV_FIX(ul, s, 32, 32, uint32)
3c6a074a 11329VFP_CONV_FIX_A64(uq, s, 32, 64, uint64)
4373f3ce 11330#undef VFP_CONV_FIX
16d5b3ca
WN
11331#undef VFP_CONV_FIX_FLOAT
11332#undef VFP_CONV_FLOAT_FIX_ROUND
4373f3ce 11333
52a1f6a3
AG
11334/* Set the current fp rounding mode and return the old one.
11335 * The argument is a softfloat float_round_ value.
11336 */
11337uint32_t HELPER(set_rmode)(uint32_t rmode, CPUARMState *env)
11338{
11339 float_status *fp_status = &env->vfp.fp_status;
11340
11341 uint32_t prev_rmode = get_float_rounding_mode(fp_status);
11342 set_float_rounding_mode(rmode, fp_status);
11343
11344 return prev_rmode;
11345}
11346
43630e58
WN
11347/* Set the current fp rounding mode in the standard fp status and return
11348 * the old one. This is for NEON instructions that need to change the
11349 * rounding mode but wish to use the standard FPSCR values for everything
11350 * else. Always set the rounding mode back to the correct value after
11351 * modifying it.
11352 * The argument is a softfloat float_round_ value.
11353 */
11354uint32_t HELPER(set_neon_rmode)(uint32_t rmode, CPUARMState *env)
11355{
11356 float_status *fp_status = &env->vfp.standard_fp_status;
11357
11358 uint32_t prev_rmode = get_float_rounding_mode(fp_status);
11359 set_float_rounding_mode(rmode, fp_status);
11360
11361 return prev_rmode;
11362}
11363
60011498 11364/* Half precision conversions. */
0ecb72a5 11365static float32 do_fcvt_f16_to_f32(uint32_t a, CPUARMState *env, float_status *s)
60011498 11366{
60011498 11367 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
fb91678d
PM
11368 float32 r = float16_to_float32(make_float16(a), ieee, s);
11369 if (ieee) {
af39bc8c 11370 return float32_maybe_silence_nan(r, s);
fb91678d
PM
11371 }
11372 return r;
60011498
PB
11373}
11374
0ecb72a5 11375static uint32_t do_fcvt_f32_to_f16(float32 a, CPUARMState *env, float_status *s)
60011498 11376{
60011498 11377 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
fb91678d
PM
11378 float16 r = float32_to_float16(a, ieee, s);
11379 if (ieee) {
af39bc8c 11380 r = float16_maybe_silence_nan(r, s);
fb91678d
PM
11381 }
11382 return float16_val(r);
60011498
PB
11383}
11384
0ecb72a5 11385float32 HELPER(neon_fcvt_f16_to_f32)(uint32_t a, CPUARMState *env)
2d981da7
PM
11386{
11387 return do_fcvt_f16_to_f32(a, env, &env->vfp.standard_fp_status);
11388}
11389
0ecb72a5 11390uint32_t HELPER(neon_fcvt_f32_to_f16)(float32 a, CPUARMState *env)
2d981da7
PM
11391{
11392 return do_fcvt_f32_to_f16(a, env, &env->vfp.standard_fp_status);
11393}
11394
0ecb72a5 11395float32 HELPER(vfp_fcvt_f16_to_f32)(uint32_t a, CPUARMState *env)
2d981da7
PM
11396{
11397 return do_fcvt_f16_to_f32(a, env, &env->vfp.fp_status);
11398}
11399
0ecb72a5 11400uint32_t HELPER(vfp_fcvt_f32_to_f16)(float32 a, CPUARMState *env)
2d981da7
PM
11401{
11402 return do_fcvt_f32_to_f16(a, env, &env->vfp.fp_status);
11403}
11404
8900aad2
PM
11405float64 HELPER(vfp_fcvt_f16_to_f64)(uint32_t a, CPUARMState *env)
11406{
11407 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
11408 float64 r = float16_to_float64(make_float16(a), ieee, &env->vfp.fp_status);
11409 if (ieee) {
af39bc8c 11410 return float64_maybe_silence_nan(r, &env->vfp.fp_status);
8900aad2
PM
11411 }
11412 return r;
11413}
11414
11415uint32_t HELPER(vfp_fcvt_f64_to_f16)(float64 a, CPUARMState *env)
11416{
11417 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
11418 float16 r = float64_to_float16(a, ieee, &env->vfp.fp_status);
11419 if (ieee) {
af39bc8c 11420 r = float16_maybe_silence_nan(r, &env->vfp.fp_status);
8900aad2
PM
11421 }
11422 return float16_val(r);
11423}
11424
dda3ec49 11425#define float32_two make_float32(0x40000000)
6aae3df1
PM
11426#define float32_three make_float32(0x40400000)
11427#define float32_one_point_five make_float32(0x3fc00000)
dda3ec49 11428
0ecb72a5 11429float32 HELPER(recps_f32)(float32 a, float32 b, CPUARMState *env)
4373f3ce 11430{
dda3ec49
PM
11431 float_status *s = &env->vfp.standard_fp_status;
11432 if ((float32_is_infinity(a) && float32_is_zero_or_denormal(b)) ||
11433 (float32_is_infinity(b) && float32_is_zero_or_denormal(a))) {
43fe9bdb
PM
11434 if (!(float32_is_zero(a) || float32_is_zero(b))) {
11435 float_raise(float_flag_input_denormal, s);
11436 }
dda3ec49
PM
11437 return float32_two;
11438 }
11439 return float32_sub(float32_two, float32_mul(a, b, s), s);
4373f3ce
PB
11440}
11441
0ecb72a5 11442float32 HELPER(rsqrts_f32)(float32 a, float32 b, CPUARMState *env)
4373f3ce 11443{
71826966 11444 float_status *s = &env->vfp.standard_fp_status;
9ea62f57
PM
11445 float32 product;
11446 if ((float32_is_infinity(a) && float32_is_zero_or_denormal(b)) ||
11447 (float32_is_infinity(b) && float32_is_zero_or_denormal(a))) {
43fe9bdb
PM
11448 if (!(float32_is_zero(a) || float32_is_zero(b))) {
11449 float_raise(float_flag_input_denormal, s);
11450 }
6aae3df1 11451 return float32_one_point_five;
9ea62f57 11452 }
6aae3df1
PM
11453 product = float32_mul(a, b, s);
11454 return float32_div(float32_sub(float32_three, product, s), float32_two, s);
4373f3ce
PB
11455}
11456
8f8e3aa4
PB
11457/* NEON helpers. */
11458
56bf4fe2
CL
11459/* Constants 256 and 512 are used in some helpers; we avoid relying on
11460 * int->float conversions at run-time. */
11461#define float64_256 make_float64(0x4070000000000000LL)
11462#define float64_512 make_float64(0x4080000000000000LL)
b6d4443a
AB
11463#define float32_maxnorm make_float32(0x7f7fffff)
11464#define float64_maxnorm make_float64(0x7fefffffffffffffLL)
56bf4fe2 11465
b6d4443a
AB
11466/* Reciprocal functions
11467 *
11468 * The algorithm that must be used to calculate the estimate
11469 * is specified by the ARM ARM, see FPRecipEstimate()
fe0e4872 11470 */
b6d4443a
AB
11471
11472static float64 recip_estimate(float64 a, float_status *real_fp_status)
fe0e4872 11473{
1146a817
PM
11474 /* These calculations mustn't set any fp exception flags,
11475 * so we use a local copy of the fp_status.
11476 */
b6d4443a 11477 float_status dummy_status = *real_fp_status;
1146a817 11478 float_status *s = &dummy_status;
fe0e4872
CL
11479 /* q = (int)(a * 512.0) */
11480 float64 q = float64_mul(float64_512, a, s);
11481 int64_t q_int = float64_to_int64_round_to_zero(q, s);
11482
11483 /* r = 1.0 / (((double)q + 0.5) / 512.0) */
11484 q = int64_to_float64(q_int, s);
11485 q = float64_add(q, float64_half, s);
11486 q = float64_div(q, float64_512, s);
11487 q = float64_div(float64_one, q, s);
11488
11489 /* s = (int)(256.0 * r + 0.5) */
11490 q = float64_mul(q, float64_256, s);
11491 q = float64_add(q, float64_half, s);
11492 q_int = float64_to_int64_round_to_zero(q, s);
11493
11494 /* return (double)s / 256.0 */
11495 return float64_div(int64_to_float64(q_int, s), float64_256, s);
11496}
11497
b6d4443a
AB
11498/* Common wrapper to call recip_estimate */
11499static float64 call_recip_estimate(float64 num, int off, float_status *fpst)
4373f3ce 11500{
b6d4443a
AB
11501 uint64_t val64 = float64_val(num);
11502 uint64_t frac = extract64(val64, 0, 52);
11503 int64_t exp = extract64(val64, 52, 11);
11504 uint64_t sbit;
11505 float64 scaled, estimate;
fe0e4872 11506
b6d4443a
AB
11507 /* Generate the scaled number for the estimate function */
11508 if (exp == 0) {
11509 if (extract64(frac, 51, 1) == 0) {
11510 exp = -1;
11511 frac = extract64(frac, 0, 50) << 2;
11512 } else {
11513 frac = extract64(frac, 0, 51) << 1;
11514 }
11515 }
fe0e4872 11516
b6d4443a
AB
11517 /* scaled = '0' : '01111111110' : fraction<51:44> : Zeros(44); */
11518 scaled = make_float64((0x3feULL << 52)
11519 | extract64(frac, 44, 8) << 44);
11520
11521 estimate = recip_estimate(scaled, fpst);
11522
11523 /* Build new result */
11524 val64 = float64_val(estimate);
11525 sbit = 0x8000000000000000ULL & val64;
11526 exp = off - exp;
11527 frac = extract64(val64, 0, 52);
11528
11529 if (exp == 0) {
11530 frac = 1ULL << 51 | extract64(frac, 1, 51);
11531 } else if (exp == -1) {
11532 frac = 1ULL << 50 | extract64(frac, 2, 50);
11533 exp = 0;
11534 }
11535
11536 return make_float64(sbit | (exp << 52) | frac);
11537}
11538
11539static bool round_to_inf(float_status *fpst, bool sign_bit)
11540{
11541 switch (fpst->float_rounding_mode) {
11542 case float_round_nearest_even: /* Round to Nearest */
11543 return true;
11544 case float_round_up: /* Round to +Inf */
11545 return !sign_bit;
11546 case float_round_down: /* Round to -Inf */
11547 return sign_bit;
11548 case float_round_to_zero: /* Round to Zero */
11549 return false;
11550 }
11551
11552 g_assert_not_reached();
11553}
11554
11555float32 HELPER(recpe_f32)(float32 input, void *fpstp)
11556{
11557 float_status *fpst = fpstp;
11558 float32 f32 = float32_squash_input_denormal(input, fpst);
11559 uint32_t f32_val = float32_val(f32);
11560 uint32_t f32_sbit = 0x80000000ULL & f32_val;
11561 int32_t f32_exp = extract32(f32_val, 23, 8);
11562 uint32_t f32_frac = extract32(f32_val, 0, 23);
11563 float64 f64, r64;
11564 uint64_t r64_val;
11565 int64_t r64_exp;
11566 uint64_t r64_frac;
11567
11568 if (float32_is_any_nan(f32)) {
11569 float32 nan = f32;
af39bc8c 11570 if (float32_is_signaling_nan(f32, fpst)) {
b6d4443a 11571 float_raise(float_flag_invalid, fpst);
af39bc8c 11572 nan = float32_maybe_silence_nan(f32, fpst);
fe0e4872 11573 }
b6d4443a 11574 if (fpst->default_nan_mode) {
af39bc8c 11575 nan = float32_default_nan(fpst);
43fe9bdb 11576 }
b6d4443a
AB
11577 return nan;
11578 } else if (float32_is_infinity(f32)) {
11579 return float32_set_sign(float32_zero, float32_is_neg(f32));
11580 } else if (float32_is_zero(f32)) {
11581 float_raise(float_flag_divbyzero, fpst);
11582 return float32_set_sign(float32_infinity, float32_is_neg(f32));
11583 } else if ((f32_val & ~(1ULL << 31)) < (1ULL << 21)) {
11584 /* Abs(value) < 2.0^-128 */
11585 float_raise(float_flag_overflow | float_flag_inexact, fpst);
11586 if (round_to_inf(fpst, f32_sbit)) {
11587 return float32_set_sign(float32_infinity, float32_is_neg(f32));
11588 } else {
11589 return float32_set_sign(float32_maxnorm, float32_is_neg(f32));
11590 }
11591 } else if (f32_exp >= 253 && fpst->flush_to_zero) {
11592 float_raise(float_flag_underflow, fpst);
11593 return float32_set_sign(float32_zero, float32_is_neg(f32));
fe0e4872
CL
11594 }
11595
fe0e4872 11596
b6d4443a
AB
11597 f64 = make_float64(((int64_t)(f32_exp) << 52) | (int64_t)(f32_frac) << 29);
11598 r64 = call_recip_estimate(f64, 253, fpst);
11599 r64_val = float64_val(r64);
11600 r64_exp = extract64(r64_val, 52, 11);
11601 r64_frac = extract64(r64_val, 0, 52);
11602
11603 /* result = sign : result_exp<7:0> : fraction<51:29>; */
11604 return make_float32(f32_sbit |
11605 (r64_exp & 0xff) << 23 |
11606 extract64(r64_frac, 29, 24));
11607}
11608
11609float64 HELPER(recpe_f64)(float64 input, void *fpstp)
11610{
11611 float_status *fpst = fpstp;
11612 float64 f64 = float64_squash_input_denormal(input, fpst);
11613 uint64_t f64_val = float64_val(f64);
11614 uint64_t f64_sbit = 0x8000000000000000ULL & f64_val;
11615 int64_t f64_exp = extract64(f64_val, 52, 11);
11616 float64 r64;
11617 uint64_t r64_val;
11618 int64_t r64_exp;
11619 uint64_t r64_frac;
11620
11621 /* Deal with any special cases */
11622 if (float64_is_any_nan(f64)) {
11623 float64 nan = f64;
af39bc8c 11624 if (float64_is_signaling_nan(f64, fpst)) {
b6d4443a 11625 float_raise(float_flag_invalid, fpst);
af39bc8c 11626 nan = float64_maybe_silence_nan(f64, fpst);
b6d4443a
AB
11627 }
11628 if (fpst->default_nan_mode) {
af39bc8c 11629 nan = float64_default_nan(fpst);
b6d4443a
AB
11630 }
11631 return nan;
11632 } else if (float64_is_infinity(f64)) {
11633 return float64_set_sign(float64_zero, float64_is_neg(f64));
11634 } else if (float64_is_zero(f64)) {
11635 float_raise(float_flag_divbyzero, fpst);
11636 return float64_set_sign(float64_infinity, float64_is_neg(f64));
11637 } else if ((f64_val & ~(1ULL << 63)) < (1ULL << 50)) {
11638 /* Abs(value) < 2.0^-1024 */
11639 float_raise(float_flag_overflow | float_flag_inexact, fpst);
11640 if (round_to_inf(fpst, f64_sbit)) {
11641 return float64_set_sign(float64_infinity, float64_is_neg(f64));
11642 } else {
11643 return float64_set_sign(float64_maxnorm, float64_is_neg(f64));
11644 }
fc1792e9 11645 } else if (f64_exp >= 2045 && fpst->flush_to_zero) {
b6d4443a
AB
11646 float_raise(float_flag_underflow, fpst);
11647 return float64_set_sign(float64_zero, float64_is_neg(f64));
11648 }
fe0e4872 11649
b6d4443a
AB
11650 r64 = call_recip_estimate(f64, 2045, fpst);
11651 r64_val = float64_val(r64);
11652 r64_exp = extract64(r64_val, 52, 11);
11653 r64_frac = extract64(r64_val, 0, 52);
fe0e4872 11654
b6d4443a
AB
11655 /* result = sign : result_exp<10:0> : fraction<51:0> */
11656 return make_float64(f64_sbit |
11657 ((r64_exp & 0x7ff) << 52) |
11658 r64_frac);
4373f3ce
PB
11659}
11660
e07be5d2
CL
11661/* The algorithm that must be used to calculate the estimate
11662 * is specified by the ARM ARM.
11663 */
c2fb418e 11664static float64 recip_sqrt_estimate(float64 a, float_status *real_fp_status)
e07be5d2 11665{
1146a817
PM
11666 /* These calculations mustn't set any fp exception flags,
11667 * so we use a local copy of the fp_status.
11668 */
c2fb418e 11669 float_status dummy_status = *real_fp_status;
1146a817 11670 float_status *s = &dummy_status;
e07be5d2
CL
11671 float64 q;
11672 int64_t q_int;
11673
11674 if (float64_lt(a, float64_half, s)) {
11675 /* range 0.25 <= a < 0.5 */
11676
11677 /* a in units of 1/512 rounded down */
11678 /* q0 = (int)(a * 512.0); */
11679 q = float64_mul(float64_512, a, s);
11680 q_int = float64_to_int64_round_to_zero(q, s);
11681
11682 /* reciprocal root r */
11683 /* r = 1.0 / sqrt(((double)q0 + 0.5) / 512.0); */
11684 q = int64_to_float64(q_int, s);
11685 q = float64_add(q, float64_half, s);
11686 q = float64_div(q, float64_512, s);
11687 q = float64_sqrt(q, s);
11688 q = float64_div(float64_one, q, s);
11689 } else {
11690 /* range 0.5 <= a < 1.0 */
11691
11692 /* a in units of 1/256 rounded down */
11693 /* q1 = (int)(a * 256.0); */
11694 q = float64_mul(float64_256, a, s);
11695 int64_t q_int = float64_to_int64_round_to_zero(q, s);
11696
11697 /* reciprocal root r */
11698 /* r = 1.0 /sqrt(((double)q1 + 0.5) / 256); */
11699 q = int64_to_float64(q_int, s);
11700 q = float64_add(q, float64_half, s);
11701 q = float64_div(q, float64_256, s);
11702 q = float64_sqrt(q, s);
11703 q = float64_div(float64_one, q, s);
11704 }
11705 /* r in units of 1/256 rounded to nearest */
11706 /* s = (int)(256.0 * r + 0.5); */
11707
11708 q = float64_mul(q, float64_256,s );
11709 q = float64_add(q, float64_half, s);
11710 q_int = float64_to_int64_round_to_zero(q, s);
11711
11712 /* return (double)s / 256.0;*/
11713 return float64_div(int64_to_float64(q_int, s), float64_256, s);
11714}
11715
c2fb418e 11716float32 HELPER(rsqrte_f32)(float32 input, void *fpstp)
4373f3ce 11717{
c2fb418e
AB
11718 float_status *s = fpstp;
11719 float32 f32 = float32_squash_input_denormal(input, s);
11720 uint32_t val = float32_val(f32);
11721 uint32_t f32_sbit = 0x80000000 & val;
11722 int32_t f32_exp = extract32(val, 23, 8);
11723 uint32_t f32_frac = extract32(val, 0, 23);
11724 uint64_t f64_frac;
11725 uint64_t val64;
e07be5d2
CL
11726 int result_exp;
11727 float64 f64;
e07be5d2 11728
c2fb418e
AB
11729 if (float32_is_any_nan(f32)) {
11730 float32 nan = f32;
af39bc8c 11731 if (float32_is_signaling_nan(f32, s)) {
e07be5d2 11732 float_raise(float_flag_invalid, s);
af39bc8c 11733 nan = float32_maybe_silence_nan(f32, s);
e07be5d2 11734 }
c2fb418e 11735 if (s->default_nan_mode) {
af39bc8c 11736 nan = float32_default_nan(s);
43fe9bdb 11737 }
c2fb418e
AB
11738 return nan;
11739 } else if (float32_is_zero(f32)) {
e07be5d2 11740 float_raise(float_flag_divbyzero, s);
c2fb418e
AB
11741 return float32_set_sign(float32_infinity, float32_is_neg(f32));
11742 } else if (float32_is_neg(f32)) {
e07be5d2 11743 float_raise(float_flag_invalid, s);
af39bc8c 11744 return float32_default_nan(s);
c2fb418e 11745 } else if (float32_is_infinity(f32)) {
e07be5d2
CL
11746 return float32_zero;
11747 }
11748
c2fb418e 11749 /* Scale and normalize to a double-precision value between 0.25 and 1.0,
e07be5d2 11750 * preserving the parity of the exponent. */
c2fb418e
AB
11751
11752 f64_frac = ((uint64_t) f32_frac) << 29;
11753 if (f32_exp == 0) {
11754 while (extract64(f64_frac, 51, 1) == 0) {
11755 f64_frac = f64_frac << 1;
11756 f32_exp = f32_exp-1;
11757 }
11758 f64_frac = extract64(f64_frac, 0, 51) << 1;
11759 }
11760
11761 if (extract64(f32_exp, 0, 1) == 0) {
11762 f64 = make_float64(((uint64_t) f32_sbit) << 32
e07be5d2 11763 | (0x3feULL << 52)
c2fb418e 11764 | f64_frac);
e07be5d2 11765 } else {
c2fb418e 11766 f64 = make_float64(((uint64_t) f32_sbit) << 32
e07be5d2 11767 | (0x3fdULL << 52)
c2fb418e 11768 | f64_frac);
e07be5d2
CL
11769 }
11770
c2fb418e 11771 result_exp = (380 - f32_exp) / 2;
e07be5d2 11772
c2fb418e 11773 f64 = recip_sqrt_estimate(f64, s);
e07be5d2
CL
11774
11775 val64 = float64_val(f64);
11776
26cc6abf 11777 val = ((result_exp & 0xff) << 23)
e07be5d2
CL
11778 | ((val64 >> 29) & 0x7fffff);
11779 return make_float32(val);
4373f3ce
PB
11780}
11781
c2fb418e
AB
11782float64 HELPER(rsqrte_f64)(float64 input, void *fpstp)
11783{
11784 float_status *s = fpstp;
11785 float64 f64 = float64_squash_input_denormal(input, s);
11786 uint64_t val = float64_val(f64);
11787 uint64_t f64_sbit = 0x8000000000000000ULL & val;
11788 int64_t f64_exp = extract64(val, 52, 11);
11789 uint64_t f64_frac = extract64(val, 0, 52);
11790 int64_t result_exp;
11791 uint64_t result_frac;
11792
11793 if (float64_is_any_nan(f64)) {
11794 float64 nan = f64;
af39bc8c 11795 if (float64_is_signaling_nan(f64, s)) {
c2fb418e 11796 float_raise(float_flag_invalid, s);
af39bc8c 11797 nan = float64_maybe_silence_nan(f64, s);
c2fb418e
AB
11798 }
11799 if (s->default_nan_mode) {
af39bc8c 11800 nan = float64_default_nan(s);
c2fb418e
AB
11801 }
11802 return nan;
11803 } else if (float64_is_zero(f64)) {
11804 float_raise(float_flag_divbyzero, s);
11805 return float64_set_sign(float64_infinity, float64_is_neg(f64));
11806 } else if (float64_is_neg(f64)) {
11807 float_raise(float_flag_invalid, s);
af39bc8c 11808 return float64_default_nan(s);
c2fb418e
AB
11809 } else if (float64_is_infinity(f64)) {
11810 return float64_zero;
11811 }
11812
11813 /* Scale and normalize to a double-precision value between 0.25 and 1.0,
11814 * preserving the parity of the exponent. */
11815
11816 if (f64_exp == 0) {
11817 while (extract64(f64_frac, 51, 1) == 0) {
11818 f64_frac = f64_frac << 1;
11819 f64_exp = f64_exp - 1;
11820 }
11821 f64_frac = extract64(f64_frac, 0, 51) << 1;
11822 }
11823
11824 if (extract64(f64_exp, 0, 1) == 0) {
11825 f64 = make_float64(f64_sbit
11826 | (0x3feULL << 52)
11827 | f64_frac);
11828 } else {
11829 f64 = make_float64(f64_sbit
11830 | (0x3fdULL << 52)
11831 | f64_frac);
11832 }
11833
11834 result_exp = (3068 - f64_exp) / 2;
11835
11836 f64 = recip_sqrt_estimate(f64, s);
11837
11838 result_frac = extract64(float64_val(f64), 0, 52);
11839
11840 return make_float64(f64_sbit |
11841 ((result_exp & 0x7ff) << 52) |
11842 result_frac);
11843}
11844
b6d4443a 11845uint32_t HELPER(recpe_u32)(uint32_t a, void *fpstp)
4373f3ce 11846{
b6d4443a 11847 float_status *s = fpstp;
fe0e4872
CL
11848 float64 f64;
11849
11850 if ((a & 0x80000000) == 0) {
11851 return 0xffffffff;
11852 }
11853
11854 f64 = make_float64((0x3feULL << 52)
11855 | ((int64_t)(a & 0x7fffffff) << 21));
11856
b6d4443a 11857 f64 = recip_estimate(f64, s);
fe0e4872
CL
11858
11859 return 0x80000000 | ((float64_val(f64) >> 21) & 0x7fffffff);
4373f3ce
PB
11860}
11861
c2fb418e 11862uint32_t HELPER(rsqrte_u32)(uint32_t a, void *fpstp)
4373f3ce 11863{
c2fb418e 11864 float_status *fpst = fpstp;
e07be5d2
CL
11865 float64 f64;
11866
11867 if ((a & 0xc0000000) == 0) {
11868 return 0xffffffff;
11869 }
11870
11871 if (a & 0x80000000) {
11872 f64 = make_float64((0x3feULL << 52)
11873 | ((uint64_t)(a & 0x7fffffff) << 21));
11874 } else { /* bits 31-30 == '01' */
11875 f64 = make_float64((0x3fdULL << 52)
11876 | ((uint64_t)(a & 0x3fffffff) << 22));
11877 }
11878
c2fb418e 11879 f64 = recip_sqrt_estimate(f64, fpst);
e07be5d2
CL
11880
11881 return 0x80000000 | ((float64_val(f64) >> 21) & 0x7fffffff);
4373f3ce 11882}
fe1479c3 11883
da97f52c
PM
11884/* VFPv4 fused multiply-accumulate */
11885float32 VFP_HELPER(muladd, s)(float32 a, float32 b, float32 c, void *fpstp)
11886{
11887 float_status *fpst = fpstp;
11888 return float32_muladd(a, b, c, 0, fpst);
11889}
11890
11891float64 VFP_HELPER(muladd, d)(float64 a, float64 b, float64 c, void *fpstp)
11892{
11893 float_status *fpst = fpstp;
11894 return float64_muladd(a, b, c, 0, fpst);
11895}
d9b0848d
PM
11896
11897/* ARMv8 round to integral */
11898float32 HELPER(rints_exact)(float32 x, void *fp_status)
11899{
11900 return float32_round_to_int(x, fp_status);
11901}
11902
11903float64 HELPER(rintd_exact)(float64 x, void *fp_status)
11904{
11905 return float64_round_to_int(x, fp_status);
11906}
11907
11908float32 HELPER(rints)(float32 x, void *fp_status)
11909{
11910 int old_flags = get_float_exception_flags(fp_status), new_flags;
11911 float32 ret;
11912
11913 ret = float32_round_to_int(x, fp_status);
11914
11915 /* Suppress any inexact exceptions the conversion produced */
11916 if (!(old_flags & float_flag_inexact)) {
11917 new_flags = get_float_exception_flags(fp_status);
11918 set_float_exception_flags(new_flags & ~float_flag_inexact, fp_status);
11919 }
11920
11921 return ret;
11922}
11923
11924float64 HELPER(rintd)(float64 x, void *fp_status)
11925{
11926 int old_flags = get_float_exception_flags(fp_status), new_flags;
11927 float64 ret;
11928
11929 ret = float64_round_to_int(x, fp_status);
11930
11931 new_flags = get_float_exception_flags(fp_status);
11932
11933 /* Suppress any inexact exceptions the conversion produced */
11934 if (!(old_flags & float_flag_inexact)) {
11935 new_flags = get_float_exception_flags(fp_status);
11936 set_float_exception_flags(new_flags & ~float_flag_inexact, fp_status);
11937 }
11938
11939 return ret;
11940}
9972da66
WN
11941
11942/* Convert ARM rounding mode to softfloat */
11943int arm_rmode_to_sf(int rmode)
11944{
11945 switch (rmode) {
11946 case FPROUNDING_TIEAWAY:
11947 rmode = float_round_ties_away;
11948 break;
11949 case FPROUNDING_ODD:
11950 /* FIXME: add support for TIEAWAY and ODD */
11951 qemu_log_mask(LOG_UNIMP, "arm: unimplemented rounding mode: %d\n",
11952 rmode);
11953 case FPROUNDING_TIEEVEN:
11954 default:
11955 rmode = float_round_nearest_even;
11956 break;
11957 case FPROUNDING_POSINF:
11958 rmode = float_round_up;
11959 break;
11960 case FPROUNDING_NEGINF:
11961 rmode = float_round_down;
11962 break;
11963 case FPROUNDING_ZERO:
11964 rmode = float_round_to_zero;
11965 break;
11966 }
11967 return rmode;
11968}
eb0ecd5a 11969
aa633469
PM
11970/* CRC helpers.
11971 * The upper bytes of val (above the number specified by 'bytes') must have
11972 * been zeroed out by the caller.
11973 */
eb0ecd5a
WN
11974uint32_t HELPER(crc32)(uint32_t acc, uint32_t val, uint32_t bytes)
11975{
11976 uint8_t buf[4];
11977
aa633469 11978 stl_le_p(buf, val);
eb0ecd5a
WN
11979
11980 /* zlib crc32 converts the accumulator and output to one's complement. */
11981 return crc32(acc ^ 0xffffffff, buf, bytes) ^ 0xffffffff;
11982}
11983
11984uint32_t HELPER(crc32c)(uint32_t acc, uint32_t val, uint32_t bytes)
11985{
11986 uint8_t buf[4];
11987
aa633469 11988 stl_le_p(buf, val);
eb0ecd5a
WN
11989
11990 /* Linux crc32c converts the output to one's complement. */
11991 return crc32c(acc, buf, bytes) ^ 0xffffffff;
11992}
a9e01311
RH
11993
11994/* Return the exception level to which FP-disabled exceptions should
11995 * be taken, or 0 if FP is enabled.
11996 */
11997static inline int fp_exception_el(CPUARMState *env)
11998{
55faa212 11999#ifndef CONFIG_USER_ONLY
a9e01311
RH
12000 int fpen;
12001 int cur_el = arm_current_el(env);
12002
12003 /* CPACR and the CPTR registers don't exist before v6, so FP is
12004 * always accessible
12005 */
12006 if (!arm_feature(env, ARM_FEATURE_V6)) {
12007 return 0;
12008 }
12009
12010 /* The CPACR controls traps to EL1, or PL1 if we're 32 bit:
12011 * 0, 2 : trap EL0 and EL1/PL1 accesses
12012 * 1 : trap only EL0 accesses
12013 * 3 : trap no accesses
12014 */
12015 fpen = extract32(env->cp15.cpacr_el1, 20, 2);
12016 switch (fpen) {
12017 case 0:
12018 case 2:
12019 if (cur_el == 0 || cur_el == 1) {
12020 /* Trap to PL1, which might be EL1 or EL3 */
12021 if (arm_is_secure(env) && !arm_el_is_aa64(env, 3)) {
12022 return 3;
12023 }
12024 return 1;
12025 }
12026 if (cur_el == 3 && !is_a64(env)) {
12027 /* Secure PL1 running at EL3 */
12028 return 3;
12029 }
12030 break;
12031 case 1:
12032 if (cur_el == 0) {
12033 return 1;
12034 }
12035 break;
12036 case 3:
12037 break;
12038 }
12039
12040 /* For the CPTR registers we don't need to guard with an ARM_FEATURE
12041 * check because zero bits in the registers mean "don't trap".
12042 */
12043
12044 /* CPTR_EL2 : present in v7VE or v8 */
12045 if (cur_el <= 2 && extract32(env->cp15.cptr_el[2], 10, 1)
12046 && !arm_is_secure_below_el3(env)) {
12047 /* Trap FP ops at EL2, NS-EL1 or NS-EL0 to EL2 */
12048 return 2;
12049 }
12050
12051 /* CPTR_EL3 : present in v8 */
12052 if (extract32(env->cp15.cptr_el[3], 10, 1)) {
12053 /* Trap all FP ops to EL3 */
12054 return 3;
12055 }
55faa212 12056#endif
a9e01311
RH
12057 return 0;
12058}
12059
12060void cpu_get_tb_cpu_state(CPUARMState *env, target_ulong *pc,
b9adaa70 12061 target_ulong *cs_base, uint32_t *pflags)
a9e01311
RH
12062{
12063 ARMMMUIdx mmu_idx = core_to_arm_mmu_idx(env, cpu_mmu_index(env, false));
1db5e96c 12064 int fp_el = fp_exception_el(env);
b9adaa70
RH
12065 uint32_t flags;
12066
a9e01311 12067 if (is_a64(env)) {
1db5e96c
RH
12068 int sve_el = sve_exception_el(env);
12069 uint32_t zcr_len;
12070
a9e01311 12071 *pc = env->pc;
b9adaa70 12072 flags = ARM_TBFLAG_AARCH64_STATE_MASK;
a9e01311 12073 /* Get control bits for tagged addresses */
b9adaa70
RH
12074 flags |= (arm_regime_tbi0(env, mmu_idx) << ARM_TBFLAG_TBI0_SHIFT);
12075 flags |= (arm_regime_tbi1(env, mmu_idx) << ARM_TBFLAG_TBI1_SHIFT);
1db5e96c
RH
12076 flags |= sve_el << ARM_TBFLAG_SVEEXC_EL_SHIFT;
12077
12078 /* If SVE is disabled, but FP is enabled,
12079 then the effective len is 0. */
12080 if (sve_el != 0 && fp_el == 0) {
12081 zcr_len = 0;
12082 } else {
12083 int current_el = arm_current_el(env);
12084
12085 zcr_len = env->vfp.zcr_el[current_el <= 1 ? 1 : current_el];
12086 zcr_len &= 0xf;
12087 if (current_el < 2 && arm_feature(env, ARM_FEATURE_EL2)) {
12088 zcr_len = MIN(zcr_len, 0xf & (uint32_t)env->vfp.zcr_el[2]);
12089 }
12090 if (current_el < 3 && arm_feature(env, ARM_FEATURE_EL3)) {
12091 zcr_len = MIN(zcr_len, 0xf & (uint32_t)env->vfp.zcr_el[3]);
12092 }
12093 }
12094 flags |= zcr_len << ARM_TBFLAG_ZCR_LEN_SHIFT;
a9e01311
RH
12095 } else {
12096 *pc = env->regs[15];
b9adaa70 12097 flags = (env->thumb << ARM_TBFLAG_THUMB_SHIFT)
a9e01311
RH
12098 | (env->vfp.vec_len << ARM_TBFLAG_VECLEN_SHIFT)
12099 | (env->vfp.vec_stride << ARM_TBFLAG_VECSTRIDE_SHIFT)
12100 | (env->condexec_bits << ARM_TBFLAG_CONDEXEC_SHIFT)
12101 | (arm_sctlr_b(env) << ARM_TBFLAG_SCTLR_B_SHIFT);
12102 if (!(access_secure_reg(env))) {
b9adaa70 12103 flags |= ARM_TBFLAG_NS_MASK;
a9e01311
RH
12104 }
12105 if (env->vfp.xregs[ARM_VFP_FPEXC] & (1 << 30)
12106 || arm_el_is_aa64(env, 1)) {
b9adaa70 12107 flags |= ARM_TBFLAG_VFPEN_MASK;
a9e01311 12108 }
b9adaa70
RH
12109 flags |= (extract32(env->cp15.c15_cpar, 0, 2)
12110 << ARM_TBFLAG_XSCALE_CPAR_SHIFT);
a9e01311
RH
12111 }
12112
b9adaa70 12113 flags |= (arm_to_core_mmu_idx(mmu_idx) << ARM_TBFLAG_MMUIDX_SHIFT);
a9e01311
RH
12114
12115 /* The SS_ACTIVE and PSTATE_SS bits correspond to the state machine
12116 * states defined in the ARM ARM for software singlestep:
12117 * SS_ACTIVE PSTATE.SS State
12118 * 0 x Inactive (the TB flag for SS is always 0)
12119 * 1 0 Active-pending
12120 * 1 1 Active-not-pending
12121 */
12122 if (arm_singlestep_active(env)) {
b9adaa70 12123 flags |= ARM_TBFLAG_SS_ACTIVE_MASK;
a9e01311
RH
12124 if (is_a64(env)) {
12125 if (env->pstate & PSTATE_SS) {
b9adaa70 12126 flags |= ARM_TBFLAG_PSTATE_SS_MASK;
a9e01311
RH
12127 }
12128 } else {
12129 if (env->uncached_cpsr & PSTATE_SS) {
b9adaa70 12130 flags |= ARM_TBFLAG_PSTATE_SS_MASK;
a9e01311
RH
12131 }
12132 }
12133 }
12134 if (arm_cpu_data_is_big_endian(env)) {
b9adaa70 12135 flags |= ARM_TBFLAG_BE_DATA_MASK;
a9e01311 12136 }
1db5e96c 12137 flags |= fp_el << ARM_TBFLAG_FPEXC_EL_SHIFT;
a9e01311
RH
12138
12139 if (arm_v7m_is_handler_mode(env)) {
b9adaa70 12140 flags |= ARM_TBFLAG_HANDLER_MASK;
a9e01311
RH
12141 }
12142
b9adaa70 12143 *pflags = flags;
a9e01311
RH
12144 *cs_base = 0;
12145}
This page took 2.993965 seconds and 4 git commands to generate.