]> git.proxmox.com Git - mirror_qemu.git/blame - target/arm/helper.c
target/arm: Add the XML dynamic generation
[mirror_qemu.git] / target / arm / helper.c
CommitLineData
74c21bd0 1#include "qemu/osdep.h"
181962fd 2#include "target/arm/idau.h"
194cbc49 3#include "trace.h"
b5ff1b31 4#include "cpu.h"
ccd38087 5#include "internals.h"
022c62cb 6#include "exec/gdbstub.h"
2ef6175a 7#include "exec/helper-proto.h"
1de7afc9 8#include "qemu/host-utils.h"
78027bb6 9#include "sysemu/arch_init.h"
9c17d615 10#include "sysemu/sysemu.h"
1de7afc9 11#include "qemu/bitops.h"
eb0ecd5a 12#include "qemu/crc32c.h"
63c91552 13#include "exec/exec-all.h"
f08b6170 14#include "exec/cpu_ldst.h"
1d854765 15#include "arm_ldst.h"
eb0ecd5a 16#include <zlib.h> /* For crc32 */
cfe67cef 17#include "exec/semihost.h"
f3a9b694 18#include "sysemu/kvm.h"
24f91e81 19#include "fpu/softfloat.h"
0b03bdfc 20
352c98e5
LV
21#define ARM_CPU_FREQ 1000000000 /* FIXME: 1 GHz, should be configurable */
22
4a501606 23#ifndef CONFIG_USER_ONLY
5b2d261d
AB
24/* Cacheability and shareability attributes for a memory access */
25typedef struct ARMCacheAttrs {
26 unsigned int attrs:8; /* as in the MAIR register encoding */
27 unsigned int shareability:2; /* as in the SH field of the VMSAv8-64 PTEs */
28} ARMCacheAttrs;
29
af51f566 30static bool get_phys_addr(CPUARMState *env, target_ulong address,
03ae85f8 31 MMUAccessType access_type, ARMMMUIdx mmu_idx,
af51f566 32 hwaddr *phys_ptr, MemTxAttrs *attrs, int *prot,
bc52bfeb 33 target_ulong *page_size,
5b2d261d 34 ARMMMUFaultInfo *fi, ARMCacheAttrs *cacheattrs);
7c2cb42b 35
37785977 36static bool get_phys_addr_lpae(CPUARMState *env, target_ulong address,
03ae85f8 37 MMUAccessType access_type, ARMMMUIdx mmu_idx,
37785977 38 hwaddr *phys_ptr, MemTxAttrs *txattrs, int *prot,
da909b2c 39 target_ulong *page_size_ptr,
5b2d261d 40 ARMMMUFaultInfo *fi, ARMCacheAttrs *cacheattrs);
37785977 41
35337cc3
PM
42/* Security attributes for an address, as returned by v8m_security_lookup. */
43typedef struct V8M_SAttributes {
44 bool ns;
45 bool nsc;
46 uint8_t sregion;
47 bool srvalid;
48 uint8_t iregion;
49 bool irvalid;
50} V8M_SAttributes;
51
333e10c5
PM
52static void v8m_security_lookup(CPUARMState *env, uint32_t address,
53 MMUAccessType access_type, ARMMMUIdx mmu_idx,
54 V8M_SAttributes *sattrs);
4a501606
PM
55#endif
56
0ecb72a5 57static int vfp_gdb_get_reg(CPUARMState *env, uint8_t *buf, int reg)
56aebc89
PB
58{
59 int nregs;
60
61 /* VFP data registers are always little-endian. */
62 nregs = arm_feature(env, ARM_FEATURE_VFP3) ? 32 : 16;
63 if (reg < nregs) {
9a2b5256 64 stq_le_p(buf, *aa32_vfp_dreg(env, reg));
56aebc89
PB
65 return 8;
66 }
67 if (arm_feature(env, ARM_FEATURE_NEON)) {
68 /* Aliases for Q regs. */
69 nregs += 16;
70 if (reg < nregs) {
9a2b5256
RH
71 uint64_t *q = aa32_vfp_qreg(env, reg - 32);
72 stq_le_p(buf, q[0]);
73 stq_le_p(buf + 8, q[1]);
56aebc89
PB
74 return 16;
75 }
76 }
77 switch (reg - nregs) {
78 case 0: stl_p(buf, env->vfp.xregs[ARM_VFP_FPSID]); return 4;
79 case 1: stl_p(buf, env->vfp.xregs[ARM_VFP_FPSCR]); return 4;
80 case 2: stl_p(buf, env->vfp.xregs[ARM_VFP_FPEXC]); return 4;
81 }
82 return 0;
83}
84
0ecb72a5 85static int vfp_gdb_set_reg(CPUARMState *env, uint8_t *buf, int reg)
56aebc89
PB
86{
87 int nregs;
88
89 nregs = arm_feature(env, ARM_FEATURE_VFP3) ? 32 : 16;
90 if (reg < nregs) {
9a2b5256 91 *aa32_vfp_dreg(env, reg) = ldq_le_p(buf);
56aebc89
PB
92 return 8;
93 }
94 if (arm_feature(env, ARM_FEATURE_NEON)) {
95 nregs += 16;
96 if (reg < nregs) {
9a2b5256
RH
97 uint64_t *q = aa32_vfp_qreg(env, reg - 32);
98 q[0] = ldq_le_p(buf);
99 q[1] = ldq_le_p(buf + 8);
56aebc89
PB
100 return 16;
101 }
102 }
103 switch (reg - nregs) {
104 case 0: env->vfp.xregs[ARM_VFP_FPSID] = ldl_p(buf); return 4;
105 case 1: env->vfp.xregs[ARM_VFP_FPSCR] = ldl_p(buf); return 4;
71b3c3de 106 case 2: env->vfp.xregs[ARM_VFP_FPEXC] = ldl_p(buf) & (1 << 30); return 4;
56aebc89
PB
107 }
108 return 0;
109}
110
6a669427
PM
111static int aarch64_fpu_gdb_get_reg(CPUARMState *env, uint8_t *buf, int reg)
112{
113 switch (reg) {
114 case 0 ... 31:
115 /* 128 bit FP register */
9a2b5256
RH
116 {
117 uint64_t *q = aa64_vfp_qreg(env, reg);
118 stq_le_p(buf, q[0]);
119 stq_le_p(buf + 8, q[1]);
120 return 16;
121 }
6a669427
PM
122 case 32:
123 /* FPSR */
124 stl_p(buf, vfp_get_fpsr(env));
125 return 4;
126 case 33:
127 /* FPCR */
128 stl_p(buf, vfp_get_fpcr(env));
129 return 4;
130 default:
131 return 0;
132 }
133}
134
135static int aarch64_fpu_gdb_set_reg(CPUARMState *env, uint8_t *buf, int reg)
136{
137 switch (reg) {
138 case 0 ... 31:
139 /* 128 bit FP register */
9a2b5256
RH
140 {
141 uint64_t *q = aa64_vfp_qreg(env, reg);
142 q[0] = ldq_le_p(buf);
143 q[1] = ldq_le_p(buf + 8);
144 return 16;
145 }
6a669427
PM
146 case 32:
147 /* FPSR */
148 vfp_set_fpsr(env, ldl_p(buf));
149 return 4;
150 case 33:
151 /* FPCR */
152 vfp_set_fpcr(env, ldl_p(buf));
153 return 4;
154 default:
155 return 0;
156 }
157}
158
c4241c7d 159static uint64_t raw_read(CPUARMState *env, const ARMCPRegInfo *ri)
d4e6df63 160{
375421cc 161 assert(ri->fieldoffset);
67ed771d 162 if (cpreg_field_is_64bit(ri)) {
c4241c7d 163 return CPREG_FIELD64(env, ri);
22d9e1a9 164 } else {
c4241c7d 165 return CPREG_FIELD32(env, ri);
22d9e1a9 166 }
d4e6df63
PM
167}
168
c4241c7d
PM
169static void raw_write(CPUARMState *env, const ARMCPRegInfo *ri,
170 uint64_t value)
d4e6df63 171{
375421cc 172 assert(ri->fieldoffset);
67ed771d 173 if (cpreg_field_is_64bit(ri)) {
22d9e1a9
PM
174 CPREG_FIELD64(env, ri) = value;
175 } else {
176 CPREG_FIELD32(env, ri) = value;
177 }
d4e6df63
PM
178}
179
11f136ee
FA
180static void *raw_ptr(CPUARMState *env, const ARMCPRegInfo *ri)
181{
182 return (char *)env + ri->fieldoffset;
183}
184
49a66191 185uint64_t read_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri)
721fae12 186{
59a1c327 187 /* Raw read of a coprocessor register (as needed for migration, etc). */
721fae12 188 if (ri->type & ARM_CP_CONST) {
59a1c327 189 return ri->resetvalue;
721fae12 190 } else if (ri->raw_readfn) {
59a1c327 191 return ri->raw_readfn(env, ri);
721fae12 192 } else if (ri->readfn) {
59a1c327 193 return ri->readfn(env, ri);
721fae12 194 } else {
59a1c327 195 return raw_read(env, ri);
721fae12 196 }
721fae12
PM
197}
198
59a1c327 199static void write_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri,
7900e9f1 200 uint64_t v)
721fae12
PM
201{
202 /* Raw write of a coprocessor register (as needed for migration, etc).
721fae12
PM
203 * Note that constant registers are treated as write-ignored; the
204 * caller should check for success by whether a readback gives the
205 * value written.
206 */
207 if (ri->type & ARM_CP_CONST) {
59a1c327 208 return;
721fae12 209 } else if (ri->raw_writefn) {
c4241c7d 210 ri->raw_writefn(env, ri, v);
721fae12 211 } else if (ri->writefn) {
c4241c7d 212 ri->writefn(env, ri, v);
721fae12 213 } else {
afb2530f 214 raw_write(env, ri, v);
721fae12 215 }
721fae12
PM
216}
217
200bf5b7
AB
218static int arm_gdb_get_sysreg(CPUARMState *env, uint8_t *buf, int reg)
219{
220 ARMCPU *cpu = arm_env_get_cpu(env);
221 const ARMCPRegInfo *ri;
222 uint32_t key;
223
224 key = cpu->dyn_xml.cpregs_keys[reg];
225 ri = get_arm_cp_reginfo(cpu->cp_regs, key);
226 if (ri) {
227 if (cpreg_field_is_64bit(ri)) {
228 return gdb_get_reg64(buf, (uint64_t)read_raw_cp_reg(env, ri));
229 } else {
230 return gdb_get_reg32(buf, (uint32_t)read_raw_cp_reg(env, ri));
231 }
232 }
233 return 0;
234}
235
236static int arm_gdb_set_sysreg(CPUARMState *env, uint8_t *buf, int reg)
237{
238 return 0;
239}
240
375421cc
PM
241static bool raw_accessors_invalid(const ARMCPRegInfo *ri)
242{
243 /* Return true if the regdef would cause an assertion if you called
244 * read_raw_cp_reg() or write_raw_cp_reg() on it (ie if it is a
245 * program bug for it not to have the NO_RAW flag).
246 * NB that returning false here doesn't necessarily mean that calling
247 * read/write_raw_cp_reg() is safe, because we can't distinguish "has
248 * read/write access functions which are safe for raw use" from "has
249 * read/write access functions which have side effects but has forgotten
250 * to provide raw access functions".
251 * The tests here line up with the conditions in read/write_raw_cp_reg()
252 * and assertions in raw_read()/raw_write().
253 */
254 if ((ri->type & ARM_CP_CONST) ||
255 ri->fieldoffset ||
256 ((ri->raw_writefn || ri->writefn) && (ri->raw_readfn || ri->readfn))) {
257 return false;
258 }
259 return true;
260}
261
721fae12
PM
262bool write_cpustate_to_list(ARMCPU *cpu)
263{
264 /* Write the coprocessor state from cpu->env to the (index,value) list. */
265 int i;
266 bool ok = true;
267
268 for (i = 0; i < cpu->cpreg_array_len; i++) {
269 uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
270 const ARMCPRegInfo *ri;
59a1c327 271
60322b39 272 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
721fae12
PM
273 if (!ri) {
274 ok = false;
275 continue;
276 }
7a0e58fa 277 if (ri->type & ARM_CP_NO_RAW) {
721fae12
PM
278 continue;
279 }
59a1c327 280 cpu->cpreg_values[i] = read_raw_cp_reg(&cpu->env, ri);
721fae12
PM
281 }
282 return ok;
283}
284
285bool write_list_to_cpustate(ARMCPU *cpu)
286{
287 int i;
288 bool ok = true;
289
290 for (i = 0; i < cpu->cpreg_array_len; i++) {
291 uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
292 uint64_t v = cpu->cpreg_values[i];
721fae12
PM
293 const ARMCPRegInfo *ri;
294
60322b39 295 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
721fae12
PM
296 if (!ri) {
297 ok = false;
298 continue;
299 }
7a0e58fa 300 if (ri->type & ARM_CP_NO_RAW) {
721fae12
PM
301 continue;
302 }
303 /* Write value and confirm it reads back as written
304 * (to catch read-only registers and partially read-only
305 * registers where the incoming migration value doesn't match)
306 */
59a1c327
PM
307 write_raw_cp_reg(&cpu->env, ri, v);
308 if (read_raw_cp_reg(&cpu->env, ri) != v) {
721fae12
PM
309 ok = false;
310 }
311 }
312 return ok;
313}
314
315static void add_cpreg_to_list(gpointer key, gpointer opaque)
316{
317 ARMCPU *cpu = opaque;
318 uint64_t regidx;
319 const ARMCPRegInfo *ri;
320
321 regidx = *(uint32_t *)key;
60322b39 322 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
721fae12 323
7a0e58fa 324 if (!(ri->type & (ARM_CP_NO_RAW|ARM_CP_ALIAS))) {
721fae12
PM
325 cpu->cpreg_indexes[cpu->cpreg_array_len] = cpreg_to_kvm_id(regidx);
326 /* The value array need not be initialized at this point */
327 cpu->cpreg_array_len++;
328 }
329}
330
331static void count_cpreg(gpointer key, gpointer opaque)
332{
333 ARMCPU *cpu = opaque;
334 uint64_t regidx;
335 const ARMCPRegInfo *ri;
336
337 regidx = *(uint32_t *)key;
60322b39 338 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
721fae12 339
7a0e58fa 340 if (!(ri->type & (ARM_CP_NO_RAW|ARM_CP_ALIAS))) {
721fae12
PM
341 cpu->cpreg_array_len++;
342 }
343}
344
345static gint cpreg_key_compare(gconstpointer a, gconstpointer b)
346{
cbf239b7
AR
347 uint64_t aidx = cpreg_to_kvm_id(*(uint32_t *)a);
348 uint64_t bidx = cpreg_to_kvm_id(*(uint32_t *)b);
721fae12 349
cbf239b7
AR
350 if (aidx > bidx) {
351 return 1;
352 }
353 if (aidx < bidx) {
354 return -1;
355 }
356 return 0;
721fae12
PM
357}
358
359void init_cpreg_list(ARMCPU *cpu)
360{
361 /* Initialise the cpreg_tuples[] array based on the cp_regs hash.
362 * Note that we require cpreg_tuples[] to be sorted by key ID.
363 */
57b6d95e 364 GList *keys;
721fae12
PM
365 int arraylen;
366
57b6d95e 367 keys = g_hash_table_get_keys(cpu->cp_regs);
721fae12
PM
368 keys = g_list_sort(keys, cpreg_key_compare);
369
370 cpu->cpreg_array_len = 0;
371
372 g_list_foreach(keys, count_cpreg, cpu);
373
374 arraylen = cpu->cpreg_array_len;
375 cpu->cpreg_indexes = g_new(uint64_t, arraylen);
376 cpu->cpreg_values = g_new(uint64_t, arraylen);
377 cpu->cpreg_vmstate_indexes = g_new(uint64_t, arraylen);
378 cpu->cpreg_vmstate_values = g_new(uint64_t, arraylen);
379 cpu->cpreg_vmstate_array_len = cpu->cpreg_array_len;
380 cpu->cpreg_array_len = 0;
381
382 g_list_foreach(keys, add_cpreg_to_list, cpu);
383
384 assert(cpu->cpreg_array_len == arraylen);
385
386 g_list_free(keys);
387}
388
68e9c2fe
EI
389/*
390 * Some registers are not accessible if EL3.NS=0 and EL3 is using AArch32 but
391 * they are accessible when EL3 is using AArch64 regardless of EL3.NS.
392 *
393 * access_el3_aa32ns: Used to check AArch32 register views.
394 * access_el3_aa32ns_aa64any: Used to check both AArch32/64 register views.
395 */
396static CPAccessResult access_el3_aa32ns(CPUARMState *env,
3f208fd7
PM
397 const ARMCPRegInfo *ri,
398 bool isread)
68e9c2fe
EI
399{
400 bool secure = arm_is_secure_below_el3(env);
401
402 assert(!arm_el_is_aa64(env, 3));
403 if (secure) {
404 return CP_ACCESS_TRAP_UNCATEGORIZED;
405 }
406 return CP_ACCESS_OK;
407}
408
409static CPAccessResult access_el3_aa32ns_aa64any(CPUARMState *env,
3f208fd7
PM
410 const ARMCPRegInfo *ri,
411 bool isread)
68e9c2fe
EI
412{
413 if (!arm_el_is_aa64(env, 3)) {
3f208fd7 414 return access_el3_aa32ns(env, ri, isread);
68e9c2fe
EI
415 }
416 return CP_ACCESS_OK;
417}
418
5513c3ab
PM
419/* Some secure-only AArch32 registers trap to EL3 if used from
420 * Secure EL1 (but are just ordinary UNDEF in other non-EL3 contexts).
421 * Note that an access from Secure EL1 can only happen if EL3 is AArch64.
422 * We assume that the .access field is set to PL1_RW.
423 */
424static CPAccessResult access_trap_aa32s_el1(CPUARMState *env,
3f208fd7
PM
425 const ARMCPRegInfo *ri,
426 bool isread)
5513c3ab
PM
427{
428 if (arm_current_el(env) == 3) {
429 return CP_ACCESS_OK;
430 }
431 if (arm_is_secure_below_el3(env)) {
432 return CP_ACCESS_TRAP_EL3;
433 }
434 /* This will be EL1 NS and EL2 NS, which just UNDEF */
435 return CP_ACCESS_TRAP_UNCATEGORIZED;
436}
437
187f678d
PM
438/* Check for traps to "powerdown debug" registers, which are controlled
439 * by MDCR.TDOSA
440 */
441static CPAccessResult access_tdosa(CPUARMState *env, const ARMCPRegInfo *ri,
442 bool isread)
443{
444 int el = arm_current_el(env);
445
446 if (el < 2 && (env->cp15.mdcr_el2 & MDCR_TDOSA)
447 && !arm_is_secure_below_el3(env)) {
448 return CP_ACCESS_TRAP_EL2;
449 }
450 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TDOSA)) {
451 return CP_ACCESS_TRAP_EL3;
452 }
453 return CP_ACCESS_OK;
454}
455
91b0a238
PM
456/* Check for traps to "debug ROM" registers, which are controlled
457 * by MDCR_EL2.TDRA for EL2 but by the more general MDCR_EL3.TDA for EL3.
458 */
459static CPAccessResult access_tdra(CPUARMState *env, const ARMCPRegInfo *ri,
460 bool isread)
461{
462 int el = arm_current_el(env);
463
464 if (el < 2 && (env->cp15.mdcr_el2 & MDCR_TDRA)
465 && !arm_is_secure_below_el3(env)) {
466 return CP_ACCESS_TRAP_EL2;
467 }
468 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TDA)) {
469 return CP_ACCESS_TRAP_EL3;
470 }
471 return CP_ACCESS_OK;
472}
473
d6c8cf81
PM
474/* Check for traps to general debug registers, which are controlled
475 * by MDCR_EL2.TDA for EL2 and MDCR_EL3.TDA for EL3.
476 */
477static CPAccessResult access_tda(CPUARMState *env, const ARMCPRegInfo *ri,
478 bool isread)
479{
480 int el = arm_current_el(env);
481
482 if (el < 2 && (env->cp15.mdcr_el2 & MDCR_TDA)
483 && !arm_is_secure_below_el3(env)) {
484 return CP_ACCESS_TRAP_EL2;
485 }
486 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TDA)) {
487 return CP_ACCESS_TRAP_EL3;
488 }
489 return CP_ACCESS_OK;
490}
491
1fce1ba9
PM
492/* Check for traps to performance monitor registers, which are controlled
493 * by MDCR_EL2.TPM for EL2 and MDCR_EL3.TPM for EL3.
494 */
495static CPAccessResult access_tpm(CPUARMState *env, const ARMCPRegInfo *ri,
496 bool isread)
497{
498 int el = arm_current_el(env);
499
500 if (el < 2 && (env->cp15.mdcr_el2 & MDCR_TPM)
501 && !arm_is_secure_below_el3(env)) {
502 return CP_ACCESS_TRAP_EL2;
503 }
504 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) {
505 return CP_ACCESS_TRAP_EL3;
506 }
507 return CP_ACCESS_OK;
508}
509
c4241c7d 510static void dacr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
c983fe6c 511{
00c8cb0a
AF
512 ARMCPU *cpu = arm_env_get_cpu(env);
513
8d5c773e 514 raw_write(env, ri, value);
d10eb08f 515 tlb_flush(CPU(cpu)); /* Flush TLB as domain not tracked in TLB */
c983fe6c
PM
516}
517
c4241c7d 518static void fcse_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
08de207b 519{
00c8cb0a
AF
520 ARMCPU *cpu = arm_env_get_cpu(env);
521
8d5c773e 522 if (raw_read(env, ri) != value) {
08de207b
PM
523 /* Unlike real hardware the qemu TLB uses virtual addresses,
524 * not modified virtual addresses, so this causes a TLB flush.
525 */
d10eb08f 526 tlb_flush(CPU(cpu));
8d5c773e 527 raw_write(env, ri, value);
08de207b 528 }
08de207b 529}
c4241c7d
PM
530
531static void contextidr_write(CPUARMState *env, const ARMCPRegInfo *ri,
532 uint64_t value)
08de207b 533{
00c8cb0a
AF
534 ARMCPU *cpu = arm_env_get_cpu(env);
535
452a0955 536 if (raw_read(env, ri) != value && !arm_feature(env, ARM_FEATURE_PMSA)
014406b5 537 && !extended_addresses_enabled(env)) {
08de207b
PM
538 /* For VMSA (when not using the LPAE long descriptor page table
539 * format) this register includes the ASID, so do a TLB flush.
540 * For PMSA it is purely a process ID and no action is needed.
541 */
d10eb08f 542 tlb_flush(CPU(cpu));
08de207b 543 }
8d5c773e 544 raw_write(env, ri, value);
08de207b
PM
545}
546
c4241c7d
PM
547static void tlbiall_write(CPUARMState *env, const ARMCPRegInfo *ri,
548 uint64_t value)
d929823f
PM
549{
550 /* Invalidate all (TLBIALL) */
00c8cb0a
AF
551 ARMCPU *cpu = arm_env_get_cpu(env);
552
d10eb08f 553 tlb_flush(CPU(cpu));
d929823f
PM
554}
555
c4241c7d
PM
556static void tlbimva_write(CPUARMState *env, const ARMCPRegInfo *ri,
557 uint64_t value)
d929823f
PM
558{
559 /* Invalidate single TLB entry by MVA and ASID (TLBIMVA) */
31b030d4
AF
560 ARMCPU *cpu = arm_env_get_cpu(env);
561
562 tlb_flush_page(CPU(cpu), value & TARGET_PAGE_MASK);
d929823f
PM
563}
564
c4241c7d
PM
565static void tlbiasid_write(CPUARMState *env, const ARMCPRegInfo *ri,
566 uint64_t value)
d929823f
PM
567{
568 /* Invalidate by ASID (TLBIASID) */
00c8cb0a
AF
569 ARMCPU *cpu = arm_env_get_cpu(env);
570
d10eb08f 571 tlb_flush(CPU(cpu));
d929823f
PM
572}
573
c4241c7d
PM
574static void tlbimvaa_write(CPUARMState *env, const ARMCPRegInfo *ri,
575 uint64_t value)
d929823f
PM
576{
577 /* Invalidate single entry by MVA, all ASIDs (TLBIMVAA) */
31b030d4
AF
578 ARMCPU *cpu = arm_env_get_cpu(env);
579
580 tlb_flush_page(CPU(cpu), value & TARGET_PAGE_MASK);
d929823f
PM
581}
582
fa439fc5
PM
583/* IS variants of TLB operations must affect all cores */
584static void tlbiall_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
585 uint64_t value)
586{
a67cf277 587 CPUState *cs = ENV_GET_CPU(env);
fa439fc5 588
a67cf277 589 tlb_flush_all_cpus_synced(cs);
fa439fc5
PM
590}
591
592static void tlbiasid_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
593 uint64_t value)
594{
a67cf277 595 CPUState *cs = ENV_GET_CPU(env);
fa439fc5 596
a67cf277 597 tlb_flush_all_cpus_synced(cs);
fa439fc5
PM
598}
599
600static void tlbimva_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
601 uint64_t value)
602{
a67cf277 603 CPUState *cs = ENV_GET_CPU(env);
fa439fc5 604
a67cf277 605 tlb_flush_page_all_cpus_synced(cs, value & TARGET_PAGE_MASK);
fa439fc5
PM
606}
607
608static void tlbimvaa_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
609 uint64_t value)
610{
a67cf277 611 CPUState *cs = ENV_GET_CPU(env);
fa439fc5 612
a67cf277 613 tlb_flush_page_all_cpus_synced(cs, value & TARGET_PAGE_MASK);
fa439fc5
PM
614}
615
541ef8c2
SS
616static void tlbiall_nsnh_write(CPUARMState *env, const ARMCPRegInfo *ri,
617 uint64_t value)
618{
619 CPUState *cs = ENV_GET_CPU(env);
620
0336cbf8 621 tlb_flush_by_mmuidx(cs,
8bd5c820
PM
622 ARMMMUIdxBit_S12NSE1 |
623 ARMMMUIdxBit_S12NSE0 |
624 ARMMMUIdxBit_S2NS);
541ef8c2
SS
625}
626
627static void tlbiall_nsnh_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
628 uint64_t value)
629{
a67cf277 630 CPUState *cs = ENV_GET_CPU(env);
541ef8c2 631
a67cf277 632 tlb_flush_by_mmuidx_all_cpus_synced(cs,
8bd5c820
PM
633 ARMMMUIdxBit_S12NSE1 |
634 ARMMMUIdxBit_S12NSE0 |
635 ARMMMUIdxBit_S2NS);
541ef8c2
SS
636}
637
638static void tlbiipas2_write(CPUARMState *env, const ARMCPRegInfo *ri,
639 uint64_t value)
640{
641 /* Invalidate by IPA. This has to invalidate any structures that
642 * contain only stage 2 translation information, but does not need
643 * to apply to structures that contain combined stage 1 and stage 2
644 * translation information.
645 * This must NOP if EL2 isn't implemented or SCR_EL3.NS is zero.
646 */
647 CPUState *cs = ENV_GET_CPU(env);
648 uint64_t pageaddr;
649
650 if (!arm_feature(env, ARM_FEATURE_EL2) || !(env->cp15.scr_el3 & SCR_NS)) {
651 return;
652 }
653
654 pageaddr = sextract64(value << 12, 0, 40);
655
8bd5c820 656 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_S2NS);
541ef8c2
SS
657}
658
659static void tlbiipas2_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
660 uint64_t value)
661{
a67cf277 662 CPUState *cs = ENV_GET_CPU(env);
541ef8c2
SS
663 uint64_t pageaddr;
664
665 if (!arm_feature(env, ARM_FEATURE_EL2) || !(env->cp15.scr_el3 & SCR_NS)) {
666 return;
667 }
668
669 pageaddr = sextract64(value << 12, 0, 40);
670
a67cf277 671 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
8bd5c820 672 ARMMMUIdxBit_S2NS);
541ef8c2
SS
673}
674
675static void tlbiall_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
676 uint64_t value)
677{
678 CPUState *cs = ENV_GET_CPU(env);
679
8bd5c820 680 tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_S1E2);
541ef8c2
SS
681}
682
683static void tlbiall_hyp_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
684 uint64_t value)
685{
a67cf277 686 CPUState *cs = ENV_GET_CPU(env);
541ef8c2 687
8bd5c820 688 tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_S1E2);
541ef8c2
SS
689}
690
691static void tlbimva_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
692 uint64_t value)
693{
694 CPUState *cs = ENV_GET_CPU(env);
695 uint64_t pageaddr = value & ~MAKE_64BIT_MASK(0, 12);
696
8bd5c820 697 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_S1E2);
541ef8c2
SS
698}
699
700static void tlbimva_hyp_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
701 uint64_t value)
702{
a67cf277 703 CPUState *cs = ENV_GET_CPU(env);
541ef8c2
SS
704 uint64_t pageaddr = value & ~MAKE_64BIT_MASK(0, 12);
705
a67cf277 706 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
8bd5c820 707 ARMMMUIdxBit_S1E2);
541ef8c2
SS
708}
709
e9aa6c21 710static const ARMCPRegInfo cp_reginfo[] = {
54bf36ed
FA
711 /* Define the secure and non-secure FCSE identifier CP registers
712 * separately because there is no secure bank in V8 (no _EL3). This allows
713 * the secure register to be properly reset and migrated. There is also no
714 * v8 EL1 version of the register so the non-secure instance stands alone.
715 */
9c513e78 716 { .name = "FCSEIDR",
54bf36ed
FA
717 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
718 .access = PL1_RW, .secure = ARM_CP_SECSTATE_NS,
719 .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_ns),
720 .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
9c513e78 721 { .name = "FCSEIDR_S",
54bf36ed
FA
722 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
723 .access = PL1_RW, .secure = ARM_CP_SECSTATE_S,
724 .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_s),
d4e6df63 725 .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
54bf36ed
FA
726 /* Define the secure and non-secure context identifier CP registers
727 * separately because there is no secure bank in V8 (no _EL3). This allows
728 * the secure register to be properly reset and migrated. In the
729 * non-secure case, the 32-bit register will have reset and migration
730 * disabled during registration as it is handled by the 64-bit instance.
731 */
732 { .name = "CONTEXTIDR_EL1", .state = ARM_CP_STATE_BOTH,
014406b5 733 .opc0 = 3, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
54bf36ed
FA
734 .access = PL1_RW, .secure = ARM_CP_SECSTATE_NS,
735 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[1]),
736 .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
9c513e78 737 { .name = "CONTEXTIDR_S", .state = ARM_CP_STATE_AA32,
54bf36ed
FA
738 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
739 .access = PL1_RW, .secure = ARM_CP_SECSTATE_S,
740 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_s),
d4e6df63 741 .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
9449fdf6
PM
742 REGINFO_SENTINEL
743};
744
745static const ARMCPRegInfo not_v8_cp_reginfo[] = {
746 /* NB: Some of these registers exist in v8 but with more precise
747 * definitions that don't use CP_ANY wildcards (mostly in v8_cp_reginfo[]).
748 */
749 /* MMU Domain access control / MPU write buffer control */
0c17d68c
FA
750 { .name = "DACR",
751 .cp = 15, .opc1 = CP_ANY, .crn = 3, .crm = CP_ANY, .opc2 = CP_ANY,
752 .access = PL1_RW, .resetvalue = 0,
753 .writefn = dacr_write, .raw_writefn = raw_write,
754 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
755 offsetoflow32(CPUARMState, cp15.dacr_ns) } },
a903c449
EI
756 /* ARMv7 allocates a range of implementation defined TLB LOCKDOWN regs.
757 * For v6 and v5, these mappings are overly broad.
4fdd17dd 758 */
a903c449
EI
759 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 0,
760 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
761 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 1,
762 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
763 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 4,
764 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
765 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 8,
4fdd17dd 766 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
c4804214
PM
767 /* Cache maintenance ops; some of this space may be overridden later. */
768 { .name = "CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
769 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
770 .type = ARM_CP_NOP | ARM_CP_OVERRIDE },
e9aa6c21
PM
771 REGINFO_SENTINEL
772};
773
7d57f408
PM
774static const ARMCPRegInfo not_v6_cp_reginfo[] = {
775 /* Not all pre-v6 cores implemented this WFI, so this is slightly
776 * over-broad.
777 */
778 { .name = "WFI_v5", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = 2,
779 .access = PL1_W, .type = ARM_CP_WFI },
780 REGINFO_SENTINEL
781};
782
783static const ARMCPRegInfo not_v7_cp_reginfo[] = {
784 /* Standard v6 WFI (also used in some pre-v6 cores); not in v7 (which
785 * is UNPREDICTABLE; we choose to NOP as most implementations do).
786 */
787 { .name = "WFI_v6", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
788 .access = PL1_W, .type = ARM_CP_WFI },
34f90529
PM
789 /* L1 cache lockdown. Not architectural in v6 and earlier but in practice
790 * implemented in 926, 946, 1026, 1136, 1176 and 11MPCore. StrongARM and
791 * OMAPCP will override this space.
792 */
793 { .name = "DLOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 0,
794 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_data),
795 .resetvalue = 0 },
796 { .name = "ILOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 1,
797 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_insn),
798 .resetvalue = 0 },
776d4e5c
PM
799 /* v6 doesn't have the cache ID registers but Linux reads them anyway */
800 { .name = "DUMMY", .cp = 15, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = CP_ANY,
7a0e58fa 801 .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 802 .resetvalue = 0 },
50300698
PM
803 /* We don't implement pre-v7 debug but most CPUs had at least a DBGDIDR;
804 * implementing it as RAZ means the "debug architecture version" bits
805 * will read as a reserved value, which should cause Linux to not try
806 * to use the debug hardware.
807 */
808 { .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0,
809 .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
995939a6
PM
810 /* MMU TLB control. Note that the wildcarding means we cover not just
811 * the unified TLB ops but also the dside/iside/inner-shareable variants.
812 */
813 { .name = "TLBIALL", .cp = 15, .crn = 8, .crm = CP_ANY,
814 .opc1 = CP_ANY, .opc2 = 0, .access = PL1_W, .writefn = tlbiall_write,
7a0e58fa 815 .type = ARM_CP_NO_RAW },
995939a6
PM
816 { .name = "TLBIMVA", .cp = 15, .crn = 8, .crm = CP_ANY,
817 .opc1 = CP_ANY, .opc2 = 1, .access = PL1_W, .writefn = tlbimva_write,
7a0e58fa 818 .type = ARM_CP_NO_RAW },
995939a6
PM
819 { .name = "TLBIASID", .cp = 15, .crn = 8, .crm = CP_ANY,
820 .opc1 = CP_ANY, .opc2 = 2, .access = PL1_W, .writefn = tlbiasid_write,
7a0e58fa 821 .type = ARM_CP_NO_RAW },
995939a6
PM
822 { .name = "TLBIMVAA", .cp = 15, .crn = 8, .crm = CP_ANY,
823 .opc1 = CP_ANY, .opc2 = 3, .access = PL1_W, .writefn = tlbimvaa_write,
7a0e58fa 824 .type = ARM_CP_NO_RAW },
a903c449
EI
825 { .name = "PRRR", .cp = 15, .crn = 10, .crm = 2,
826 .opc1 = 0, .opc2 = 0, .access = PL1_RW, .type = ARM_CP_NOP },
827 { .name = "NMRR", .cp = 15, .crn = 10, .crm = 2,
828 .opc1 = 0, .opc2 = 1, .access = PL1_RW, .type = ARM_CP_NOP },
7d57f408
PM
829 REGINFO_SENTINEL
830};
831
c4241c7d
PM
832static void cpacr_write(CPUARMState *env, const ARMCPRegInfo *ri,
833 uint64_t value)
2771db27 834{
f0aff255
FA
835 uint32_t mask = 0;
836
837 /* In ARMv8 most bits of CPACR_EL1 are RES0. */
838 if (!arm_feature(env, ARM_FEATURE_V8)) {
839 /* ARMv7 defines bits for unimplemented coprocessors as RAZ/WI.
840 * ASEDIS [31] and D32DIS [30] are both UNK/SBZP without VFP.
841 * TRCDIS [28] is RAZ/WI since we do not implement a trace macrocell.
842 */
843 if (arm_feature(env, ARM_FEATURE_VFP)) {
844 /* VFP coprocessor: cp10 & cp11 [23:20] */
845 mask |= (1 << 31) | (1 << 30) | (0xf << 20);
846
847 if (!arm_feature(env, ARM_FEATURE_NEON)) {
848 /* ASEDIS [31] bit is RAO/WI */
849 value |= (1 << 31);
850 }
851
852 /* VFPv3 and upwards with NEON implement 32 double precision
853 * registers (D0-D31).
854 */
855 if (!arm_feature(env, ARM_FEATURE_NEON) ||
856 !arm_feature(env, ARM_FEATURE_VFP3)) {
857 /* D32DIS [30] is RAO/WI if D16-31 are not implemented. */
858 value |= (1 << 30);
859 }
860 }
861 value &= mask;
2771db27 862 }
7ebd5f2e 863 env->cp15.cpacr_el1 = value;
2771db27
PM
864}
865
3f208fd7
PM
866static CPAccessResult cpacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
867 bool isread)
c6f19164
GB
868{
869 if (arm_feature(env, ARM_FEATURE_V8)) {
870 /* Check if CPACR accesses are to be trapped to EL2 */
871 if (arm_current_el(env) == 1 &&
872 (env->cp15.cptr_el[2] & CPTR_TCPAC) && !arm_is_secure(env)) {
873 return CP_ACCESS_TRAP_EL2;
874 /* Check if CPACR accesses are to be trapped to EL3 */
875 } else if (arm_current_el(env) < 3 &&
876 (env->cp15.cptr_el[3] & CPTR_TCPAC)) {
877 return CP_ACCESS_TRAP_EL3;
878 }
879 }
880
881 return CP_ACCESS_OK;
882}
883
3f208fd7
PM
884static CPAccessResult cptr_access(CPUARMState *env, const ARMCPRegInfo *ri,
885 bool isread)
c6f19164
GB
886{
887 /* Check if CPTR accesses are set to trap to EL3 */
888 if (arm_current_el(env) == 2 && (env->cp15.cptr_el[3] & CPTR_TCPAC)) {
889 return CP_ACCESS_TRAP_EL3;
890 }
891
892 return CP_ACCESS_OK;
893}
894
7d57f408
PM
895static const ARMCPRegInfo v6_cp_reginfo[] = {
896 /* prefetch by MVA in v6, NOP in v7 */
897 { .name = "MVA_prefetch",
898 .cp = 15, .crn = 7, .crm = 13, .opc1 = 0, .opc2 = 1,
899 .access = PL1_W, .type = ARM_CP_NOP },
6df99dec
SS
900 /* We need to break the TB after ISB to execute self-modifying code
901 * correctly and also to take any pending interrupts immediately.
902 * So use arm_cp_write_ignore() function instead of ARM_CP_NOP flag.
903 */
7d57f408 904 { .name = "ISB", .cp = 15, .crn = 7, .crm = 5, .opc1 = 0, .opc2 = 4,
6df99dec 905 .access = PL0_W, .type = ARM_CP_NO_RAW, .writefn = arm_cp_write_ignore },
091fd17c 906 { .name = "DSB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 4,
7d57f408 907 .access = PL0_W, .type = ARM_CP_NOP },
091fd17c 908 { .name = "DMB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 5,
7d57f408 909 .access = PL0_W, .type = ARM_CP_NOP },
06d76f31 910 { .name = "IFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 2,
6cd8a264 911 .access = PL1_RW,
b848ce2b
FA
912 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ifar_s),
913 offsetof(CPUARMState, cp15.ifar_ns) },
06d76f31
PM
914 .resetvalue = 0, },
915 /* Watchpoint Fault Address Register : should actually only be present
916 * for 1136, 1176, 11MPCore.
917 */
918 { .name = "WFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 1,
919 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0, },
34222fb8 920 { .name = "CPACR", .state = ARM_CP_STATE_BOTH, .opc0 = 3,
c6f19164 921 .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 2, .accessfn = cpacr_access,
7ebd5f2e 922 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.cpacr_el1),
2771db27 923 .resetvalue = 0, .writefn = cpacr_write },
7d57f408
PM
924 REGINFO_SENTINEL
925};
926
7ece99b1
AL
927/* Definitions for the PMU registers */
928#define PMCRN_MASK 0xf800
929#define PMCRN_SHIFT 11
930#define PMCRD 0x8
931#define PMCRC 0x4
932#define PMCRE 0x1
933
934static inline uint32_t pmu_num_counters(CPUARMState *env)
935{
936 return (env->cp15.c9_pmcr & PMCRN_MASK) >> PMCRN_SHIFT;
937}
938
939/* Bits allowed to be set/cleared for PMCNTEN* and PMINTEN* */
940static inline uint64_t pmu_counter_mask(CPUARMState *env)
941{
942 return (1 << 31) | ((1 << pmu_num_counters(env)) - 1);
943}
944
3f208fd7
PM
945static CPAccessResult pmreg_access(CPUARMState *env, const ARMCPRegInfo *ri,
946 bool isread)
200ac0ef 947{
3b163b01 948 /* Performance monitor registers user accessibility is controlled
1fce1ba9
PM
949 * by PMUSERENR. MDCR_EL2.TPM and MDCR_EL3.TPM allow configurable
950 * trapping to EL2 or EL3 for other accesses.
200ac0ef 951 */
1fce1ba9
PM
952 int el = arm_current_el(env);
953
6ecd0b6b 954 if (el == 0 && !(env->cp15.c9_pmuserenr & 1)) {
fcd25206 955 return CP_ACCESS_TRAP;
200ac0ef 956 }
1fce1ba9
PM
957 if (el < 2 && (env->cp15.mdcr_el2 & MDCR_TPM)
958 && !arm_is_secure_below_el3(env)) {
959 return CP_ACCESS_TRAP_EL2;
960 }
961 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) {
962 return CP_ACCESS_TRAP_EL3;
963 }
964
fcd25206 965 return CP_ACCESS_OK;
200ac0ef
PM
966}
967
6ecd0b6b
AB
968static CPAccessResult pmreg_access_xevcntr(CPUARMState *env,
969 const ARMCPRegInfo *ri,
970 bool isread)
971{
972 /* ER: event counter read trap control */
973 if (arm_feature(env, ARM_FEATURE_V8)
974 && arm_current_el(env) == 0
975 && (env->cp15.c9_pmuserenr & (1 << 3)) != 0
976 && isread) {
977 return CP_ACCESS_OK;
978 }
979
980 return pmreg_access(env, ri, isread);
981}
982
983static CPAccessResult pmreg_access_swinc(CPUARMState *env,
984 const ARMCPRegInfo *ri,
985 bool isread)
986{
987 /* SW: software increment write trap control */
988 if (arm_feature(env, ARM_FEATURE_V8)
989 && arm_current_el(env) == 0
990 && (env->cp15.c9_pmuserenr & (1 << 1)) != 0
991 && !isread) {
992 return CP_ACCESS_OK;
993 }
994
995 return pmreg_access(env, ri, isread);
996}
997
7c2cb42b 998#ifndef CONFIG_USER_ONLY
87124fde 999
6ecd0b6b
AB
1000static CPAccessResult pmreg_access_selr(CPUARMState *env,
1001 const ARMCPRegInfo *ri,
1002 bool isread)
1003{
1004 /* ER: event counter read trap control */
1005 if (arm_feature(env, ARM_FEATURE_V8)
1006 && arm_current_el(env) == 0
1007 && (env->cp15.c9_pmuserenr & (1 << 3)) != 0) {
1008 return CP_ACCESS_OK;
1009 }
1010
1011 return pmreg_access(env, ri, isread);
1012}
1013
1014static CPAccessResult pmreg_access_ccntr(CPUARMState *env,
1015 const ARMCPRegInfo *ri,
1016 bool isread)
1017{
1018 /* CR: cycle counter read trap control */
1019 if (arm_feature(env, ARM_FEATURE_V8)
1020 && arm_current_el(env) == 0
1021 && (env->cp15.c9_pmuserenr & (1 << 2)) != 0
1022 && isread) {
1023 return CP_ACCESS_OK;
1024 }
1025
1026 return pmreg_access(env, ri, isread);
1027}
1028
87124fde
AF
1029static inline bool arm_ccnt_enabled(CPUARMState *env)
1030{
1031 /* This does not support checking PMCCFILTR_EL0 register */
1032
ccbc0e33 1033 if (!(env->cp15.c9_pmcr & PMCRE) || !(env->cp15.c9_pmcnten & (1 << 31))) {
87124fde
AF
1034 return false;
1035 }
1036
1037 return true;
1038}
1039
ec7b4ce4
AF
1040void pmccntr_sync(CPUARMState *env)
1041{
1042 uint64_t temp_ticks;
1043
352c98e5
LV
1044 temp_ticks = muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
1045 ARM_CPU_FREQ, NANOSECONDS_PER_SECOND);
ec7b4ce4
AF
1046
1047 if (env->cp15.c9_pmcr & PMCRD) {
1048 /* Increment once every 64 processor clock cycles */
1049 temp_ticks /= 64;
1050 }
1051
1052 if (arm_ccnt_enabled(env)) {
1053 env->cp15.c15_ccnt = temp_ticks - env->cp15.c15_ccnt;
1054 }
1055}
1056
c4241c7d
PM
1057static void pmcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1058 uint64_t value)
200ac0ef 1059{
942a155b 1060 pmccntr_sync(env);
7c2cb42b
AF
1061
1062 if (value & PMCRC) {
1063 /* The counter has been reset */
1064 env->cp15.c15_ccnt = 0;
1065 }
1066
200ac0ef
PM
1067 /* only the DP, X, D and E bits are writable */
1068 env->cp15.c9_pmcr &= ~0x39;
1069 env->cp15.c9_pmcr |= (value & 0x39);
7c2cb42b 1070
942a155b 1071 pmccntr_sync(env);
7c2cb42b
AF
1072}
1073
1074static uint64_t pmccntr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1075{
c92c0687 1076 uint64_t total_ticks;
7c2cb42b 1077
942a155b 1078 if (!arm_ccnt_enabled(env)) {
7c2cb42b
AF
1079 /* Counter is disabled, do not change value */
1080 return env->cp15.c15_ccnt;
1081 }
1082
352c98e5
LV
1083 total_ticks = muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
1084 ARM_CPU_FREQ, NANOSECONDS_PER_SECOND);
7c2cb42b
AF
1085
1086 if (env->cp15.c9_pmcr & PMCRD) {
1087 /* Increment once every 64 processor clock cycles */
1088 total_ticks /= 64;
1089 }
1090 return total_ticks - env->cp15.c15_ccnt;
1091}
1092
6b040780
WH
1093static void pmselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1094 uint64_t value)
1095{
1096 /* The value of PMSELR.SEL affects the behavior of PMXEVTYPER and
1097 * PMXEVCNTR. We allow [0..31] to be written to PMSELR here; in the
1098 * meanwhile, we check PMSELR.SEL when PMXEVTYPER and PMXEVCNTR are
1099 * accessed.
1100 */
1101 env->cp15.c9_pmselr = value & 0x1f;
1102}
1103
7c2cb42b
AF
1104static void pmccntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1105 uint64_t value)
1106{
c92c0687 1107 uint64_t total_ticks;
7c2cb42b 1108
942a155b 1109 if (!arm_ccnt_enabled(env)) {
7c2cb42b
AF
1110 /* Counter is disabled, set the absolute value */
1111 env->cp15.c15_ccnt = value;
1112 return;
1113 }
1114
352c98e5
LV
1115 total_ticks = muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
1116 ARM_CPU_FREQ, NANOSECONDS_PER_SECOND);
7c2cb42b
AF
1117
1118 if (env->cp15.c9_pmcr & PMCRD) {
1119 /* Increment once every 64 processor clock cycles */
1120 total_ticks /= 64;
1121 }
1122 env->cp15.c15_ccnt = total_ticks - value;
200ac0ef 1123}
421c7ebd
PC
1124
1125static void pmccntr_write32(CPUARMState *env, const ARMCPRegInfo *ri,
1126 uint64_t value)
1127{
1128 uint64_t cur_val = pmccntr_read(env, NULL);
1129
1130 pmccntr_write(env, ri, deposit64(cur_val, 0, 32, value));
1131}
1132
ec7b4ce4
AF
1133#else /* CONFIG_USER_ONLY */
1134
1135void pmccntr_sync(CPUARMState *env)
1136{
1137}
1138
7c2cb42b 1139#endif
200ac0ef 1140
0614601c
AF
1141static void pmccfiltr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1142 uint64_t value)
1143{
1144 pmccntr_sync(env);
ac57fd24 1145 env->cp15.pmccfiltr_el0 = value & 0xfc000000;
0614601c
AF
1146 pmccntr_sync(env);
1147}
1148
c4241c7d 1149static void pmcntenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
200ac0ef
PM
1150 uint64_t value)
1151{
7ece99b1 1152 value &= pmu_counter_mask(env);
200ac0ef 1153 env->cp15.c9_pmcnten |= value;
200ac0ef
PM
1154}
1155
c4241c7d
PM
1156static void pmcntenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1157 uint64_t value)
200ac0ef 1158{
7ece99b1 1159 value &= pmu_counter_mask(env);
200ac0ef 1160 env->cp15.c9_pmcnten &= ~value;
200ac0ef
PM
1161}
1162
c4241c7d
PM
1163static void pmovsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1164 uint64_t value)
200ac0ef 1165{
200ac0ef 1166 env->cp15.c9_pmovsr &= ~value;
200ac0ef
PM
1167}
1168
c4241c7d
PM
1169static void pmxevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
1170 uint64_t value)
200ac0ef 1171{
fdb86656
WH
1172 /* Attempts to access PMXEVTYPER are CONSTRAINED UNPREDICTABLE when
1173 * PMSELR value is equal to or greater than the number of implemented
1174 * counters, but not equal to 0x1f. We opt to behave as a RAZ/WI.
1175 */
1176 if (env->cp15.c9_pmselr == 0x1f) {
1177 pmccfiltr_write(env, ri, value);
1178 }
1179}
1180
1181static uint64_t pmxevtyper_read(CPUARMState *env, const ARMCPRegInfo *ri)
1182{
1183 /* We opt to behave as a RAZ/WI when attempts to access PMXEVTYPER
1184 * are CONSTRAINED UNPREDICTABLE. See comments in pmxevtyper_write().
1185 */
1186 if (env->cp15.c9_pmselr == 0x1f) {
1187 return env->cp15.pmccfiltr_el0;
1188 } else {
1189 return 0;
1190 }
200ac0ef
PM
1191}
1192
c4241c7d 1193static void pmuserenr_write(CPUARMState *env, const ARMCPRegInfo *ri,
200ac0ef
PM
1194 uint64_t value)
1195{
6ecd0b6b
AB
1196 if (arm_feature(env, ARM_FEATURE_V8)) {
1197 env->cp15.c9_pmuserenr = value & 0xf;
1198 } else {
1199 env->cp15.c9_pmuserenr = value & 1;
1200 }
200ac0ef
PM
1201}
1202
c4241c7d
PM
1203static void pmintenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1204 uint64_t value)
200ac0ef
PM
1205{
1206 /* We have no event counters so only the C bit can be changed */
7ece99b1 1207 value &= pmu_counter_mask(env);
200ac0ef 1208 env->cp15.c9_pminten |= value;
200ac0ef
PM
1209}
1210
c4241c7d
PM
1211static void pmintenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1212 uint64_t value)
200ac0ef 1213{
7ece99b1 1214 value &= pmu_counter_mask(env);
200ac0ef 1215 env->cp15.c9_pminten &= ~value;
200ac0ef
PM
1216}
1217
c4241c7d
PM
1218static void vbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
1219 uint64_t value)
8641136c 1220{
a505d7fe
PM
1221 /* Note that even though the AArch64 view of this register has bits
1222 * [10:0] all RES0 we can only mask the bottom 5, to comply with the
1223 * architectural requirements for bits which are RES0 only in some
1224 * contexts. (ARMv8 would permit us to do no masking at all, but ARMv7
1225 * requires the bottom five bits to be RAZ/WI because they're UNK/SBZP.)
1226 */
855ea66d 1227 raw_write(env, ri, value & ~0x1FULL);
8641136c
NR
1228}
1229
64e0e2de
EI
1230static void scr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
1231{
1232 /* We only mask off bits that are RES0 both for AArch64 and AArch32.
1233 * For bits that vary between AArch32/64, code needs to check the
1234 * current execution mode before directly using the feature bit.
1235 */
1236 uint32_t valid_mask = SCR_AARCH64_MASK | SCR_AARCH32_MASK;
1237
1238 if (!arm_feature(env, ARM_FEATURE_EL2)) {
1239 valid_mask &= ~SCR_HCE;
1240
1241 /* On ARMv7, SMD (or SCD as it is called in v7) is only
1242 * supported if EL2 exists. The bit is UNK/SBZP when
1243 * EL2 is unavailable. In QEMU ARMv7, we force it to always zero
1244 * when EL2 is unavailable.
4eb27640 1245 * On ARMv8, this bit is always available.
64e0e2de 1246 */
4eb27640
GB
1247 if (arm_feature(env, ARM_FEATURE_V7) &&
1248 !arm_feature(env, ARM_FEATURE_V8)) {
64e0e2de
EI
1249 valid_mask &= ~SCR_SMD;
1250 }
1251 }
1252
1253 /* Clear all-context RES0 bits. */
1254 value &= valid_mask;
1255 raw_write(env, ri, value);
1256}
1257
c4241c7d 1258static uint64_t ccsidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
776d4e5c
PM
1259{
1260 ARMCPU *cpu = arm_env_get_cpu(env);
b85a1fd6
FA
1261
1262 /* Acquire the CSSELR index from the bank corresponding to the CCSIDR
1263 * bank
1264 */
1265 uint32_t index = A32_BANKED_REG_GET(env, csselr,
1266 ri->secure & ARM_CP_SECSTATE_S);
1267
1268 return cpu->ccsidr[index];
776d4e5c
PM
1269}
1270
c4241c7d
PM
1271static void csselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1272 uint64_t value)
776d4e5c 1273{
8d5c773e 1274 raw_write(env, ri, value & 0xf);
776d4e5c
PM
1275}
1276
1090b9c6
PM
1277static uint64_t isr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1278{
1279 CPUState *cs = ENV_GET_CPU(env);
1280 uint64_t ret = 0;
1281
1282 if (cs->interrupt_request & CPU_INTERRUPT_HARD) {
1283 ret |= CPSR_I;
1284 }
1285 if (cs->interrupt_request & CPU_INTERRUPT_FIQ) {
1286 ret |= CPSR_F;
1287 }
1288 /* External aborts are not possible in QEMU so A bit is always clear */
1289 return ret;
1290}
1291
e9aa6c21 1292static const ARMCPRegInfo v7_cp_reginfo[] = {
7d57f408
PM
1293 /* the old v6 WFI, UNPREDICTABLE in v7 but we choose to NOP */
1294 { .name = "NOP", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
1295 .access = PL1_W, .type = ARM_CP_NOP },
200ac0ef
PM
1296 /* Performance monitors are implementation defined in v7,
1297 * but with an ARM recommended set of registers, which we
1298 * follow (although we don't actually implement any counters)
1299 *
1300 * Performance registers fall into three categories:
1301 * (a) always UNDEF in PL0, RW in PL1 (PMINTENSET, PMINTENCLR)
1302 * (b) RO in PL0 (ie UNDEF on write), RW in PL1 (PMUSERENR)
1303 * (c) UNDEF in PL0 if PMUSERENR.EN==0, otherwise accessible (all others)
1304 * For the cases controlled by PMUSERENR we must set .access to PL0_RW
1305 * or PL0_RO as appropriate and then check PMUSERENR in the helper fn.
1306 */
1307 { .name = "PMCNTENSET", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 1,
7a0e58fa 1308 .access = PL0_RW, .type = ARM_CP_ALIAS,
8521466b 1309 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
fcd25206
PM
1310 .writefn = pmcntenset_write,
1311 .accessfn = pmreg_access,
1312 .raw_writefn = raw_write },
8521466b
AF
1313 { .name = "PMCNTENSET_EL0", .state = ARM_CP_STATE_AA64,
1314 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 1,
1315 .access = PL0_RW, .accessfn = pmreg_access,
1316 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten), .resetvalue = 0,
1317 .writefn = pmcntenset_write, .raw_writefn = raw_write },
200ac0ef 1318 { .name = "PMCNTENCLR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 2,
8521466b
AF
1319 .access = PL0_RW,
1320 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
fcd25206
PM
1321 .accessfn = pmreg_access,
1322 .writefn = pmcntenclr_write,
7a0e58fa 1323 .type = ARM_CP_ALIAS },
8521466b
AF
1324 { .name = "PMCNTENCLR_EL0", .state = ARM_CP_STATE_AA64,
1325 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 2,
1326 .access = PL0_RW, .accessfn = pmreg_access,
7a0e58fa 1327 .type = ARM_CP_ALIAS,
8521466b
AF
1328 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten),
1329 .writefn = pmcntenclr_write },
200ac0ef 1330 { .name = "PMOVSR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 3,
e4e91a21
AL
1331 .access = PL0_RW,
1332 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr),
fcd25206
PM
1333 .accessfn = pmreg_access,
1334 .writefn = pmovsr_write,
1335 .raw_writefn = raw_write },
978364f1
AF
1336 { .name = "PMOVSCLR_EL0", .state = ARM_CP_STATE_AA64,
1337 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 3,
1338 .access = PL0_RW, .accessfn = pmreg_access,
1339 .type = ARM_CP_ALIAS,
1340 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
1341 .writefn = pmovsr_write,
1342 .raw_writefn = raw_write },
fcd25206 1343 /* Unimplemented so WI. */
200ac0ef 1344 { .name = "PMSWINC", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 4,
6ecd0b6b 1345 .access = PL0_W, .accessfn = pmreg_access_swinc, .type = ARM_CP_NOP },
7c2cb42b 1346#ifndef CONFIG_USER_ONLY
6b040780
WH
1347 { .name = "PMSELR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 5,
1348 .access = PL0_RW, .type = ARM_CP_ALIAS,
1349 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmselr),
6ecd0b6b 1350 .accessfn = pmreg_access_selr, .writefn = pmselr_write,
6b040780
WH
1351 .raw_writefn = raw_write},
1352 { .name = "PMSELR_EL0", .state = ARM_CP_STATE_AA64,
1353 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 5,
6ecd0b6b 1354 .access = PL0_RW, .accessfn = pmreg_access_selr,
6b040780
WH
1355 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmselr),
1356 .writefn = pmselr_write, .raw_writefn = raw_write, },
200ac0ef 1357 { .name = "PMCCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 0,
169c8938 1358 .access = PL0_RW, .resetvalue = 0, .type = ARM_CP_ALIAS | ARM_CP_IO,
421c7ebd 1359 .readfn = pmccntr_read, .writefn = pmccntr_write32,
6ecd0b6b 1360 .accessfn = pmreg_access_ccntr },
8521466b
AF
1361 { .name = "PMCCNTR_EL0", .state = ARM_CP_STATE_AA64,
1362 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 0,
6ecd0b6b 1363 .access = PL0_RW, .accessfn = pmreg_access_ccntr,
8521466b
AF
1364 .type = ARM_CP_IO,
1365 .readfn = pmccntr_read, .writefn = pmccntr_write, },
7c2cb42b 1366#endif
8521466b
AF
1367 { .name = "PMCCFILTR_EL0", .state = ARM_CP_STATE_AA64,
1368 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 15, .opc2 = 7,
0614601c 1369 .writefn = pmccfiltr_write,
8521466b
AF
1370 .access = PL0_RW, .accessfn = pmreg_access,
1371 .type = ARM_CP_IO,
1372 .fieldoffset = offsetof(CPUARMState, cp15.pmccfiltr_el0),
1373 .resetvalue = 0, },
200ac0ef 1374 { .name = "PMXEVTYPER", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 1,
fdb86656
WH
1375 .access = PL0_RW, .type = ARM_CP_NO_RAW, .accessfn = pmreg_access,
1376 .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
1377 { .name = "PMXEVTYPER_EL0", .state = ARM_CP_STATE_AA64,
1378 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 1,
1379 .access = PL0_RW, .type = ARM_CP_NO_RAW, .accessfn = pmreg_access,
1380 .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
fcd25206 1381 /* Unimplemented, RAZ/WI. */
200ac0ef 1382 { .name = "PMXEVCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 2,
fcd25206 1383 .access = PL0_RW, .type = ARM_CP_CONST, .resetvalue = 0,
6ecd0b6b 1384 .accessfn = pmreg_access_xevcntr },
200ac0ef 1385 { .name = "PMUSERENR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 0,
1fce1ba9 1386 .access = PL0_R | PL1_RW, .accessfn = access_tpm,
e4e91a21 1387 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmuserenr),
200ac0ef 1388 .resetvalue = 0,
d4e6df63 1389 .writefn = pmuserenr_write, .raw_writefn = raw_write },
8a83ffc2
AF
1390 { .name = "PMUSERENR_EL0", .state = ARM_CP_STATE_AA64,
1391 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 0,
1fce1ba9 1392 .access = PL0_R | PL1_RW, .accessfn = access_tpm, .type = ARM_CP_ALIAS,
8a83ffc2
AF
1393 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmuserenr),
1394 .resetvalue = 0,
1395 .writefn = pmuserenr_write, .raw_writefn = raw_write },
200ac0ef 1396 { .name = "PMINTENSET", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 1,
1fce1ba9 1397 .access = PL1_RW, .accessfn = access_tpm,
e6ec5457
WH
1398 .type = ARM_CP_ALIAS,
1399 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pminten),
200ac0ef 1400 .resetvalue = 0,
d4e6df63 1401 .writefn = pmintenset_write, .raw_writefn = raw_write },
e6ec5457
WH
1402 { .name = "PMINTENSET_EL1", .state = ARM_CP_STATE_AA64,
1403 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 1,
1404 .access = PL1_RW, .accessfn = access_tpm,
1405 .type = ARM_CP_IO,
1406 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
1407 .writefn = pmintenset_write, .raw_writefn = raw_write,
1408 .resetvalue = 0x0 },
200ac0ef 1409 { .name = "PMINTENCLR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 2,
1fce1ba9 1410 .access = PL1_RW, .accessfn = access_tpm, .type = ARM_CP_ALIAS,
200ac0ef 1411 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
b061a82b 1412 .writefn = pmintenclr_write, },
978364f1
AF
1413 { .name = "PMINTENCLR_EL1", .state = ARM_CP_STATE_AA64,
1414 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 2,
1fce1ba9 1415 .access = PL1_RW, .accessfn = access_tpm, .type = ARM_CP_ALIAS,
978364f1
AF
1416 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
1417 .writefn = pmintenclr_write },
7da845b0
PM
1418 { .name = "CCSIDR", .state = ARM_CP_STATE_BOTH,
1419 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 0,
7a0e58fa 1420 .access = PL1_R, .readfn = ccsidr_read, .type = ARM_CP_NO_RAW },
7da845b0
PM
1421 { .name = "CSSELR", .state = ARM_CP_STATE_BOTH,
1422 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 2, .opc2 = 0,
b85a1fd6
FA
1423 .access = PL1_RW, .writefn = csselr_write, .resetvalue = 0,
1424 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.csselr_s),
1425 offsetof(CPUARMState, cp15.csselr_ns) } },
776d4e5c
PM
1426 /* Auxiliary ID register: this actually has an IMPDEF value but for now
1427 * just RAZ for all cores:
1428 */
0ff644a7
PM
1429 { .name = "AIDR", .state = ARM_CP_STATE_BOTH,
1430 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 7,
776d4e5c 1431 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
f32cdad5
PM
1432 /* Auxiliary fault status registers: these also are IMPDEF, and we
1433 * choose to RAZ/WI for all cores.
1434 */
1435 { .name = "AFSR0_EL1", .state = ARM_CP_STATE_BOTH,
1436 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 0,
1437 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
1438 { .name = "AFSR1_EL1", .state = ARM_CP_STATE_BOTH,
1439 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 1,
1440 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
b0fe2427
PM
1441 /* MAIR can just read-as-written because we don't implement caches
1442 * and so don't need to care about memory attributes.
1443 */
1444 { .name = "MAIR_EL1", .state = ARM_CP_STATE_AA64,
1445 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
be693c87 1446 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[1]),
b0fe2427 1447 .resetvalue = 0 },
4cfb8ad8
PM
1448 { .name = "MAIR_EL3", .state = ARM_CP_STATE_AA64,
1449 .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 2, .opc2 = 0,
1450 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[3]),
1451 .resetvalue = 0 },
b0fe2427
PM
1452 /* For non-long-descriptor page tables these are PRRR and NMRR;
1453 * regardless they still act as reads-as-written for QEMU.
b0fe2427 1454 */
1281f8e3 1455 /* MAIR0/1 are defined separately from their 64-bit counterpart which
be693c87
GB
1456 * allows them to assign the correct fieldoffset based on the endianness
1457 * handled in the field definitions.
1458 */
a903c449 1459 { .name = "MAIR0", .state = ARM_CP_STATE_AA32,
b0fe2427 1460 .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0, .access = PL1_RW,
be693c87
GB
1461 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair0_s),
1462 offsetof(CPUARMState, cp15.mair0_ns) },
b0fe2427 1463 .resetfn = arm_cp_reset_ignore },
a903c449 1464 { .name = "MAIR1", .state = ARM_CP_STATE_AA32,
b0fe2427 1465 .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 1, .access = PL1_RW,
be693c87
GB
1466 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair1_s),
1467 offsetof(CPUARMState, cp15.mair1_ns) },
b0fe2427 1468 .resetfn = arm_cp_reset_ignore },
1090b9c6
PM
1469 { .name = "ISR_EL1", .state = ARM_CP_STATE_BOTH,
1470 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 0,
7a0e58fa 1471 .type = ARM_CP_NO_RAW, .access = PL1_R, .readfn = isr_read },
995939a6
PM
1472 /* 32 bit ITLB invalidates */
1473 { .name = "ITLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 0,
7a0e58fa 1474 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_write },
995939a6 1475 { .name = "ITLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 1,
7a0e58fa 1476 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write },
995939a6 1477 { .name = "ITLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 2,
7a0e58fa 1478 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiasid_write },
995939a6
PM
1479 /* 32 bit DTLB invalidates */
1480 { .name = "DTLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 0,
7a0e58fa 1481 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_write },
995939a6 1482 { .name = "DTLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 1,
7a0e58fa 1483 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write },
995939a6 1484 { .name = "DTLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 2,
7a0e58fa 1485 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiasid_write },
995939a6
PM
1486 /* 32 bit TLB invalidates */
1487 { .name = "TLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
7a0e58fa 1488 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_write },
995939a6 1489 { .name = "TLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
7a0e58fa 1490 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write },
995939a6 1491 { .name = "TLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
7a0e58fa 1492 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiasid_write },
995939a6 1493 { .name = "TLBIMVAA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
7a0e58fa 1494 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimvaa_write },
995939a6
PM
1495 REGINFO_SENTINEL
1496};
1497
1498static const ARMCPRegInfo v7mp_cp_reginfo[] = {
1499 /* 32 bit TLB invalidates, Inner Shareable */
1500 { .name = "TLBIALLIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
7a0e58fa 1501 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_is_write },
995939a6 1502 { .name = "TLBIMVAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
7a0e58fa 1503 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_is_write },
995939a6 1504 { .name = "TLBIASIDIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
7a0e58fa 1505 .type = ARM_CP_NO_RAW, .access = PL1_W,
fa439fc5 1506 .writefn = tlbiasid_is_write },
995939a6 1507 { .name = "TLBIMVAAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
7a0e58fa 1508 .type = ARM_CP_NO_RAW, .access = PL1_W,
fa439fc5 1509 .writefn = tlbimvaa_is_write },
e9aa6c21
PM
1510 REGINFO_SENTINEL
1511};
1512
c4241c7d
PM
1513static void teecr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1514 uint64_t value)
c326b979
PM
1515{
1516 value &= 1;
1517 env->teecr = value;
c326b979
PM
1518}
1519
3f208fd7
PM
1520static CPAccessResult teehbr_access(CPUARMState *env, const ARMCPRegInfo *ri,
1521 bool isread)
c326b979 1522{
dcbff19b 1523 if (arm_current_el(env) == 0 && (env->teecr & 1)) {
92611c00 1524 return CP_ACCESS_TRAP;
c326b979 1525 }
92611c00 1526 return CP_ACCESS_OK;
c326b979
PM
1527}
1528
1529static const ARMCPRegInfo t2ee_cp_reginfo[] = {
1530 { .name = "TEECR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 6, .opc2 = 0,
1531 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, teecr),
1532 .resetvalue = 0,
1533 .writefn = teecr_write },
1534 { .name = "TEEHBR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 6, .opc2 = 0,
1535 .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, teehbr),
92611c00 1536 .accessfn = teehbr_access, .resetvalue = 0 },
c326b979
PM
1537 REGINFO_SENTINEL
1538};
1539
4d31c596 1540static const ARMCPRegInfo v6k_cp_reginfo[] = {
e4fe830b
PM
1541 { .name = "TPIDR_EL0", .state = ARM_CP_STATE_AA64,
1542 .opc0 = 3, .opc1 = 3, .opc2 = 2, .crn = 13, .crm = 0,
1543 .access = PL0_RW,
54bf36ed 1544 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[0]), .resetvalue = 0 },
4d31c596
PM
1545 { .name = "TPIDRURW", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 2,
1546 .access = PL0_RW,
54bf36ed
FA
1547 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrurw_s),
1548 offsetoflow32(CPUARMState, cp15.tpidrurw_ns) },
e4fe830b
PM
1549 .resetfn = arm_cp_reset_ignore },
1550 { .name = "TPIDRRO_EL0", .state = ARM_CP_STATE_AA64,
1551 .opc0 = 3, .opc1 = 3, .opc2 = 3, .crn = 13, .crm = 0,
1552 .access = PL0_R|PL1_W,
54bf36ed
FA
1553 .fieldoffset = offsetof(CPUARMState, cp15.tpidrro_el[0]),
1554 .resetvalue = 0},
4d31c596
PM
1555 { .name = "TPIDRURO", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 3,
1556 .access = PL0_R|PL1_W,
54bf36ed
FA
1557 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidruro_s),
1558 offsetoflow32(CPUARMState, cp15.tpidruro_ns) },
e4fe830b 1559 .resetfn = arm_cp_reset_ignore },
54bf36ed 1560 { .name = "TPIDR_EL1", .state = ARM_CP_STATE_AA64,
e4fe830b 1561 .opc0 = 3, .opc1 = 0, .opc2 = 4, .crn = 13, .crm = 0,
4d31c596 1562 .access = PL1_RW,
54bf36ed
FA
1563 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[1]), .resetvalue = 0 },
1564 { .name = "TPIDRPRW", .opc1 = 0, .cp = 15, .crn = 13, .crm = 0, .opc2 = 4,
1565 .access = PL1_RW,
1566 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrprw_s),
1567 offsetoflow32(CPUARMState, cp15.tpidrprw_ns) },
1568 .resetvalue = 0 },
4d31c596
PM
1569 REGINFO_SENTINEL
1570};
1571
55d284af
PM
1572#ifndef CONFIG_USER_ONLY
1573
3f208fd7
PM
1574static CPAccessResult gt_cntfrq_access(CPUARMState *env, const ARMCPRegInfo *ri,
1575 bool isread)
00108f2d 1576{
75502672
PM
1577 /* CNTFRQ: not visible from PL0 if both PL0PCTEN and PL0VCTEN are zero.
1578 * Writable only at the highest implemented exception level.
1579 */
1580 int el = arm_current_el(env);
1581
1582 switch (el) {
1583 case 0:
1584 if (!extract32(env->cp15.c14_cntkctl, 0, 2)) {
1585 return CP_ACCESS_TRAP;
1586 }
1587 break;
1588 case 1:
1589 if (!isread && ri->state == ARM_CP_STATE_AA32 &&
1590 arm_is_secure_below_el3(env)) {
1591 /* Accesses from 32-bit Secure EL1 UNDEF (*not* trap to EL3!) */
1592 return CP_ACCESS_TRAP_UNCATEGORIZED;
1593 }
1594 break;
1595 case 2:
1596 case 3:
1597 break;
00108f2d 1598 }
75502672
PM
1599
1600 if (!isread && el < arm_highest_el(env)) {
1601 return CP_ACCESS_TRAP_UNCATEGORIZED;
1602 }
1603
00108f2d
PM
1604 return CP_ACCESS_OK;
1605}
1606
3f208fd7
PM
1607static CPAccessResult gt_counter_access(CPUARMState *env, int timeridx,
1608 bool isread)
00108f2d 1609{
0b6440af
EI
1610 unsigned int cur_el = arm_current_el(env);
1611 bool secure = arm_is_secure(env);
1612
00108f2d 1613 /* CNT[PV]CT: not visible from PL0 if ELO[PV]CTEN is zero */
0b6440af 1614 if (cur_el == 0 &&
00108f2d
PM
1615 !extract32(env->cp15.c14_cntkctl, timeridx, 1)) {
1616 return CP_ACCESS_TRAP;
1617 }
0b6440af
EI
1618
1619 if (arm_feature(env, ARM_FEATURE_EL2) &&
1620 timeridx == GTIMER_PHYS && !secure && cur_el < 2 &&
1621 !extract32(env->cp15.cnthctl_el2, 0, 1)) {
1622 return CP_ACCESS_TRAP_EL2;
1623 }
00108f2d
PM
1624 return CP_ACCESS_OK;
1625}
1626
3f208fd7
PM
1627static CPAccessResult gt_timer_access(CPUARMState *env, int timeridx,
1628 bool isread)
00108f2d 1629{
0b6440af
EI
1630 unsigned int cur_el = arm_current_el(env);
1631 bool secure = arm_is_secure(env);
1632
00108f2d
PM
1633 /* CNT[PV]_CVAL, CNT[PV]_CTL, CNT[PV]_TVAL: not visible from PL0 if
1634 * EL0[PV]TEN is zero.
1635 */
0b6440af 1636 if (cur_el == 0 &&
00108f2d
PM
1637 !extract32(env->cp15.c14_cntkctl, 9 - timeridx, 1)) {
1638 return CP_ACCESS_TRAP;
1639 }
0b6440af
EI
1640
1641 if (arm_feature(env, ARM_FEATURE_EL2) &&
1642 timeridx == GTIMER_PHYS && !secure && cur_el < 2 &&
1643 !extract32(env->cp15.cnthctl_el2, 1, 1)) {
1644 return CP_ACCESS_TRAP_EL2;
1645 }
00108f2d
PM
1646 return CP_ACCESS_OK;
1647}
1648
1649static CPAccessResult gt_pct_access(CPUARMState *env,
3f208fd7
PM
1650 const ARMCPRegInfo *ri,
1651 bool isread)
00108f2d 1652{
3f208fd7 1653 return gt_counter_access(env, GTIMER_PHYS, isread);
00108f2d
PM
1654}
1655
1656static CPAccessResult gt_vct_access(CPUARMState *env,
3f208fd7
PM
1657 const ARMCPRegInfo *ri,
1658 bool isread)
00108f2d 1659{
3f208fd7 1660 return gt_counter_access(env, GTIMER_VIRT, isread);
00108f2d
PM
1661}
1662
3f208fd7
PM
1663static CPAccessResult gt_ptimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
1664 bool isread)
00108f2d 1665{
3f208fd7 1666 return gt_timer_access(env, GTIMER_PHYS, isread);
00108f2d
PM
1667}
1668
3f208fd7
PM
1669static CPAccessResult gt_vtimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
1670 bool isread)
00108f2d 1671{
3f208fd7 1672 return gt_timer_access(env, GTIMER_VIRT, isread);
00108f2d
PM
1673}
1674
b4d3978c 1675static CPAccessResult gt_stimer_access(CPUARMState *env,
3f208fd7
PM
1676 const ARMCPRegInfo *ri,
1677 bool isread)
b4d3978c
PM
1678{
1679 /* The AArch64 register view of the secure physical timer is
1680 * always accessible from EL3, and configurably accessible from
1681 * Secure EL1.
1682 */
1683 switch (arm_current_el(env)) {
1684 case 1:
1685 if (!arm_is_secure(env)) {
1686 return CP_ACCESS_TRAP;
1687 }
1688 if (!(env->cp15.scr_el3 & SCR_ST)) {
1689 return CP_ACCESS_TRAP_EL3;
1690 }
1691 return CP_ACCESS_OK;
1692 case 0:
1693 case 2:
1694 return CP_ACCESS_TRAP;
1695 case 3:
1696 return CP_ACCESS_OK;
1697 default:
1698 g_assert_not_reached();
1699 }
1700}
1701
55d284af
PM
1702static uint64_t gt_get_countervalue(CPUARMState *env)
1703{
bc72ad67 1704 return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) / GTIMER_SCALE;
55d284af
PM
1705}
1706
1707static void gt_recalc_timer(ARMCPU *cpu, int timeridx)
1708{
1709 ARMGenericTimer *gt = &cpu->env.cp15.c14_timer[timeridx];
1710
1711 if (gt->ctl & 1) {
1712 /* Timer enabled: calculate and set current ISTATUS, irq, and
1713 * reset timer to when ISTATUS next has to change
1714 */
edac4d8a
EI
1715 uint64_t offset = timeridx == GTIMER_VIRT ?
1716 cpu->env.cp15.cntvoff_el2 : 0;
55d284af
PM
1717 uint64_t count = gt_get_countervalue(&cpu->env);
1718 /* Note that this must be unsigned 64 bit arithmetic: */
edac4d8a 1719 int istatus = count - offset >= gt->cval;
55d284af 1720 uint64_t nexttick;
194cbc49 1721 int irqstate;
55d284af
PM
1722
1723 gt->ctl = deposit32(gt->ctl, 2, 1, istatus);
194cbc49
PM
1724
1725 irqstate = (istatus && !(gt->ctl & 2));
1726 qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate);
1727
55d284af
PM
1728 if (istatus) {
1729 /* Next transition is when count rolls back over to zero */
1730 nexttick = UINT64_MAX;
1731 } else {
1732 /* Next transition is when we hit cval */
edac4d8a 1733 nexttick = gt->cval + offset;
55d284af
PM
1734 }
1735 /* Note that the desired next expiry time might be beyond the
1736 * signed-64-bit range of a QEMUTimer -- in this case we just
1737 * set the timer for as far in the future as possible. When the
1738 * timer expires we will reset the timer for any remaining period.
1739 */
1740 if (nexttick > INT64_MAX / GTIMER_SCALE) {
1741 nexttick = INT64_MAX / GTIMER_SCALE;
1742 }
bc72ad67 1743 timer_mod(cpu->gt_timer[timeridx], nexttick);
194cbc49 1744 trace_arm_gt_recalc(timeridx, irqstate, nexttick);
55d284af
PM
1745 } else {
1746 /* Timer disabled: ISTATUS and timer output always clear */
1747 gt->ctl &= ~4;
1748 qemu_set_irq(cpu->gt_timer_outputs[timeridx], 0);
bc72ad67 1749 timer_del(cpu->gt_timer[timeridx]);
194cbc49 1750 trace_arm_gt_recalc_disabled(timeridx);
55d284af
PM
1751 }
1752}
1753
0e3eca4c
EI
1754static void gt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri,
1755 int timeridx)
55d284af
PM
1756{
1757 ARMCPU *cpu = arm_env_get_cpu(env);
55d284af 1758
bc72ad67 1759 timer_del(cpu->gt_timer[timeridx]);
55d284af
PM
1760}
1761
c4241c7d 1762static uint64_t gt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
55d284af 1763{
c4241c7d 1764 return gt_get_countervalue(env);
55d284af
PM
1765}
1766
edac4d8a
EI
1767static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
1768{
1769 return gt_get_countervalue(env) - env->cp15.cntvoff_el2;
1770}
1771
c4241c7d 1772static void gt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
0e3eca4c 1773 int timeridx,
c4241c7d 1774 uint64_t value)
55d284af 1775{
194cbc49 1776 trace_arm_gt_cval_write(timeridx, value);
55d284af
PM
1777 env->cp15.c14_timer[timeridx].cval = value;
1778 gt_recalc_timer(arm_env_get_cpu(env), timeridx);
55d284af 1779}
c4241c7d 1780
0e3eca4c
EI
1781static uint64_t gt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri,
1782 int timeridx)
55d284af 1783{
edac4d8a 1784 uint64_t offset = timeridx == GTIMER_VIRT ? env->cp15.cntvoff_el2 : 0;
55d284af 1785
c4241c7d 1786 return (uint32_t)(env->cp15.c14_timer[timeridx].cval -
edac4d8a 1787 (gt_get_countervalue(env) - offset));
55d284af
PM
1788}
1789
c4241c7d 1790static void gt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
0e3eca4c 1791 int timeridx,
c4241c7d 1792 uint64_t value)
55d284af 1793{
edac4d8a 1794 uint64_t offset = timeridx == GTIMER_VIRT ? env->cp15.cntvoff_el2 : 0;
55d284af 1795
194cbc49 1796 trace_arm_gt_tval_write(timeridx, value);
edac4d8a 1797 env->cp15.c14_timer[timeridx].cval = gt_get_countervalue(env) - offset +
18084b2f 1798 sextract64(value, 0, 32);
55d284af 1799 gt_recalc_timer(arm_env_get_cpu(env), timeridx);
55d284af
PM
1800}
1801
c4241c7d 1802static void gt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
0e3eca4c 1803 int timeridx,
c4241c7d 1804 uint64_t value)
55d284af
PM
1805{
1806 ARMCPU *cpu = arm_env_get_cpu(env);
55d284af
PM
1807 uint32_t oldval = env->cp15.c14_timer[timeridx].ctl;
1808
194cbc49 1809 trace_arm_gt_ctl_write(timeridx, value);
d3afacc7 1810 env->cp15.c14_timer[timeridx].ctl = deposit64(oldval, 0, 2, value);
55d284af
PM
1811 if ((oldval ^ value) & 1) {
1812 /* Enable toggled */
1813 gt_recalc_timer(cpu, timeridx);
d3afacc7 1814 } else if ((oldval ^ value) & 2) {
55d284af
PM
1815 /* IMASK toggled: don't need to recalculate,
1816 * just set the interrupt line based on ISTATUS
1817 */
194cbc49
PM
1818 int irqstate = (oldval & 4) && !(value & 2);
1819
1820 trace_arm_gt_imask_toggle(timeridx, irqstate);
1821 qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate);
55d284af 1822 }
55d284af
PM
1823}
1824
0e3eca4c
EI
1825static void gt_phys_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1826{
1827 gt_timer_reset(env, ri, GTIMER_PHYS);
1828}
1829
1830static void gt_phys_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1831 uint64_t value)
1832{
1833 gt_cval_write(env, ri, GTIMER_PHYS, value);
1834}
1835
1836static uint64_t gt_phys_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
1837{
1838 return gt_tval_read(env, ri, GTIMER_PHYS);
1839}
1840
1841static void gt_phys_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1842 uint64_t value)
1843{
1844 gt_tval_write(env, ri, GTIMER_PHYS, value);
1845}
1846
1847static void gt_phys_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
1848 uint64_t value)
1849{
1850 gt_ctl_write(env, ri, GTIMER_PHYS, value);
1851}
1852
1853static void gt_virt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1854{
1855 gt_timer_reset(env, ri, GTIMER_VIRT);
1856}
1857
1858static void gt_virt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1859 uint64_t value)
1860{
1861 gt_cval_write(env, ri, GTIMER_VIRT, value);
1862}
1863
1864static uint64_t gt_virt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
1865{
1866 return gt_tval_read(env, ri, GTIMER_VIRT);
1867}
1868
1869static void gt_virt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1870 uint64_t value)
1871{
1872 gt_tval_write(env, ri, GTIMER_VIRT, value);
1873}
1874
1875static void gt_virt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
1876 uint64_t value)
1877{
1878 gt_ctl_write(env, ri, GTIMER_VIRT, value);
1879}
1880
edac4d8a
EI
1881static void gt_cntvoff_write(CPUARMState *env, const ARMCPRegInfo *ri,
1882 uint64_t value)
1883{
1884 ARMCPU *cpu = arm_env_get_cpu(env);
1885
194cbc49 1886 trace_arm_gt_cntvoff_write(value);
edac4d8a
EI
1887 raw_write(env, ri, value);
1888 gt_recalc_timer(cpu, GTIMER_VIRT);
1889}
1890
b0e66d95
EI
1891static void gt_hyp_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1892{
1893 gt_timer_reset(env, ri, GTIMER_HYP);
1894}
1895
1896static void gt_hyp_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1897 uint64_t value)
1898{
1899 gt_cval_write(env, ri, GTIMER_HYP, value);
1900}
1901
1902static uint64_t gt_hyp_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
1903{
1904 return gt_tval_read(env, ri, GTIMER_HYP);
1905}
1906
1907static void gt_hyp_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1908 uint64_t value)
1909{
1910 gt_tval_write(env, ri, GTIMER_HYP, value);
1911}
1912
1913static void gt_hyp_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
1914 uint64_t value)
1915{
1916 gt_ctl_write(env, ri, GTIMER_HYP, value);
1917}
1918
b4d3978c
PM
1919static void gt_sec_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1920{
1921 gt_timer_reset(env, ri, GTIMER_SEC);
1922}
1923
1924static void gt_sec_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1925 uint64_t value)
1926{
1927 gt_cval_write(env, ri, GTIMER_SEC, value);
1928}
1929
1930static uint64_t gt_sec_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
1931{
1932 return gt_tval_read(env, ri, GTIMER_SEC);
1933}
1934
1935static void gt_sec_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1936 uint64_t value)
1937{
1938 gt_tval_write(env, ri, GTIMER_SEC, value);
1939}
1940
1941static void gt_sec_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
1942 uint64_t value)
1943{
1944 gt_ctl_write(env, ri, GTIMER_SEC, value);
1945}
1946
55d284af
PM
1947void arm_gt_ptimer_cb(void *opaque)
1948{
1949 ARMCPU *cpu = opaque;
1950
1951 gt_recalc_timer(cpu, GTIMER_PHYS);
1952}
1953
1954void arm_gt_vtimer_cb(void *opaque)
1955{
1956 ARMCPU *cpu = opaque;
1957
1958 gt_recalc_timer(cpu, GTIMER_VIRT);
1959}
1960
b0e66d95
EI
1961void arm_gt_htimer_cb(void *opaque)
1962{
1963 ARMCPU *cpu = opaque;
1964
1965 gt_recalc_timer(cpu, GTIMER_HYP);
1966}
1967
b4d3978c
PM
1968void arm_gt_stimer_cb(void *opaque)
1969{
1970 ARMCPU *cpu = opaque;
1971
1972 gt_recalc_timer(cpu, GTIMER_SEC);
1973}
1974
55d284af
PM
1975static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
1976 /* Note that CNTFRQ is purely reads-as-written for the benefit
1977 * of software; writing it doesn't actually change the timer frequency.
1978 * Our reset value matches the fixed frequency we implement the timer at.
1979 */
1980 { .name = "CNTFRQ", .cp = 15, .crn = 14, .crm = 0, .opc1 = 0, .opc2 = 0,
7a0e58fa 1981 .type = ARM_CP_ALIAS,
a7adc4b7
PM
1982 .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
1983 .fieldoffset = offsetoflow32(CPUARMState, cp15.c14_cntfrq),
a7adc4b7
PM
1984 },
1985 { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
1986 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
1987 .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
55d284af
PM
1988 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
1989 .resetvalue = (1000 * 1000 * 1000) / GTIMER_SCALE,
55d284af
PM
1990 },
1991 /* overall control: mostly access permissions */
a7adc4b7
PM
1992 { .name = "CNTKCTL", .state = ARM_CP_STATE_BOTH,
1993 .opc0 = 3, .opc1 = 0, .crn = 14, .crm = 1, .opc2 = 0,
55d284af
PM
1994 .access = PL1_RW,
1995 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntkctl),
1996 .resetvalue = 0,
1997 },
1998 /* per-timer control */
1999 { .name = "CNTP_CTL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
9ff9dd3c 2000 .secure = ARM_CP_SECSTATE_NS,
7a0e58fa 2001 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL1_RW | PL0_R,
a7adc4b7
PM
2002 .accessfn = gt_ptimer_access,
2003 .fieldoffset = offsetoflow32(CPUARMState,
2004 cp15.c14_timer[GTIMER_PHYS].ctl),
0e3eca4c 2005 .writefn = gt_phys_ctl_write, .raw_writefn = raw_write,
a7adc4b7 2006 },
9c513e78 2007 { .name = "CNTP_CTL_S",
9ff9dd3c
PM
2008 .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
2009 .secure = ARM_CP_SECSTATE_S,
2010 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL1_RW | PL0_R,
2011 .accessfn = gt_ptimer_access,
2012 .fieldoffset = offsetoflow32(CPUARMState,
2013 cp15.c14_timer[GTIMER_SEC].ctl),
2014 .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
2015 },
a7adc4b7
PM
2016 { .name = "CNTP_CTL_EL0", .state = ARM_CP_STATE_AA64,
2017 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 1,
55d284af 2018 .type = ARM_CP_IO, .access = PL1_RW | PL0_R,
a7adc4b7 2019 .accessfn = gt_ptimer_access,
55d284af
PM
2020 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
2021 .resetvalue = 0,
0e3eca4c 2022 .writefn = gt_phys_ctl_write, .raw_writefn = raw_write,
55d284af
PM
2023 },
2024 { .name = "CNTV_CTL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 1,
7a0e58fa 2025 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL1_RW | PL0_R,
a7adc4b7
PM
2026 .accessfn = gt_vtimer_access,
2027 .fieldoffset = offsetoflow32(CPUARMState,
2028 cp15.c14_timer[GTIMER_VIRT].ctl),
0e3eca4c 2029 .writefn = gt_virt_ctl_write, .raw_writefn = raw_write,
a7adc4b7
PM
2030 },
2031 { .name = "CNTV_CTL_EL0", .state = ARM_CP_STATE_AA64,
2032 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 1,
55d284af 2033 .type = ARM_CP_IO, .access = PL1_RW | PL0_R,
a7adc4b7 2034 .accessfn = gt_vtimer_access,
55d284af
PM
2035 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
2036 .resetvalue = 0,
0e3eca4c 2037 .writefn = gt_virt_ctl_write, .raw_writefn = raw_write,
55d284af
PM
2038 },
2039 /* TimerValue views: a 32 bit downcounting view of the underlying state */
2040 { .name = "CNTP_TVAL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
9ff9dd3c 2041 .secure = ARM_CP_SECSTATE_NS,
7a0e58fa 2042 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW | PL0_R,
00108f2d 2043 .accessfn = gt_ptimer_access,
0e3eca4c 2044 .readfn = gt_phys_tval_read, .writefn = gt_phys_tval_write,
55d284af 2045 },
9c513e78 2046 { .name = "CNTP_TVAL_S",
9ff9dd3c
PM
2047 .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
2048 .secure = ARM_CP_SECSTATE_S,
2049 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW | PL0_R,
2050 .accessfn = gt_ptimer_access,
2051 .readfn = gt_sec_tval_read, .writefn = gt_sec_tval_write,
2052 },
a7adc4b7
PM
2053 { .name = "CNTP_TVAL_EL0", .state = ARM_CP_STATE_AA64,
2054 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 0,
7a0e58fa 2055 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW | PL0_R,
0e3eca4c
EI
2056 .accessfn = gt_ptimer_access, .resetfn = gt_phys_timer_reset,
2057 .readfn = gt_phys_tval_read, .writefn = gt_phys_tval_write,
a7adc4b7 2058 },
55d284af 2059 { .name = "CNTV_TVAL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 0,
7a0e58fa 2060 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW | PL0_R,
00108f2d 2061 .accessfn = gt_vtimer_access,
0e3eca4c 2062 .readfn = gt_virt_tval_read, .writefn = gt_virt_tval_write,
55d284af 2063 },
a7adc4b7
PM
2064 { .name = "CNTV_TVAL_EL0", .state = ARM_CP_STATE_AA64,
2065 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 0,
7a0e58fa 2066 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW | PL0_R,
0e3eca4c
EI
2067 .accessfn = gt_vtimer_access, .resetfn = gt_virt_timer_reset,
2068 .readfn = gt_virt_tval_read, .writefn = gt_virt_tval_write,
a7adc4b7 2069 },
55d284af
PM
2070 /* The counter itself */
2071 { .name = "CNTPCT", .cp = 15, .crm = 14, .opc1 = 0,
7a0e58fa 2072 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
00108f2d 2073 .accessfn = gt_pct_access,
a7adc4b7
PM
2074 .readfn = gt_cnt_read, .resetfn = arm_cp_reset_ignore,
2075 },
2076 { .name = "CNTPCT_EL0", .state = ARM_CP_STATE_AA64,
2077 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 1,
7a0e58fa 2078 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
d57b9ee8 2079 .accessfn = gt_pct_access, .readfn = gt_cnt_read,
55d284af
PM
2080 },
2081 { .name = "CNTVCT", .cp = 15, .crm = 14, .opc1 = 1,
7a0e58fa 2082 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
00108f2d 2083 .accessfn = gt_vct_access,
edac4d8a 2084 .readfn = gt_virt_cnt_read, .resetfn = arm_cp_reset_ignore,
a7adc4b7
PM
2085 },
2086 { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
2087 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
7a0e58fa 2088 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
d57b9ee8 2089 .accessfn = gt_vct_access, .readfn = gt_virt_cnt_read,
55d284af
PM
2090 },
2091 /* Comparison value, indicating when the timer goes off */
2092 { .name = "CNTP_CVAL", .cp = 15, .crm = 14, .opc1 = 2,
9ff9dd3c 2093 .secure = ARM_CP_SECSTATE_NS,
55d284af 2094 .access = PL1_RW | PL0_R,
7a0e58fa 2095 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
55d284af 2096 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
b061a82b 2097 .accessfn = gt_ptimer_access,
0e3eca4c 2098 .writefn = gt_phys_cval_write, .raw_writefn = raw_write,
a7adc4b7 2099 },
9c513e78 2100 { .name = "CNTP_CVAL_S", .cp = 15, .crm = 14, .opc1 = 2,
9ff9dd3c
PM
2101 .secure = ARM_CP_SECSTATE_S,
2102 .access = PL1_RW | PL0_R,
2103 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
2104 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
2105 .accessfn = gt_ptimer_access,
2106 .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
2107 },
a7adc4b7
PM
2108 { .name = "CNTP_CVAL_EL0", .state = ARM_CP_STATE_AA64,
2109 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 2,
2110 .access = PL1_RW | PL0_R,
2111 .type = ARM_CP_IO,
2112 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
12cde08a 2113 .resetvalue = 0, .accessfn = gt_ptimer_access,
0e3eca4c 2114 .writefn = gt_phys_cval_write, .raw_writefn = raw_write,
55d284af
PM
2115 },
2116 { .name = "CNTV_CVAL", .cp = 15, .crm = 14, .opc1 = 3,
2117 .access = PL1_RW | PL0_R,
7a0e58fa 2118 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
55d284af 2119 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
b061a82b 2120 .accessfn = gt_vtimer_access,
0e3eca4c 2121 .writefn = gt_virt_cval_write, .raw_writefn = raw_write,
a7adc4b7
PM
2122 },
2123 { .name = "CNTV_CVAL_EL0", .state = ARM_CP_STATE_AA64,
2124 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 2,
2125 .access = PL1_RW | PL0_R,
2126 .type = ARM_CP_IO,
2127 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
2128 .resetvalue = 0, .accessfn = gt_vtimer_access,
0e3eca4c 2129 .writefn = gt_virt_cval_write, .raw_writefn = raw_write,
55d284af 2130 },
b4d3978c
PM
2131 /* Secure timer -- this is actually restricted to only EL3
2132 * and configurably Secure-EL1 via the accessfn.
2133 */
2134 { .name = "CNTPS_TVAL_EL1", .state = ARM_CP_STATE_AA64,
2135 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 0,
2136 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW,
2137 .accessfn = gt_stimer_access,
2138 .readfn = gt_sec_tval_read,
2139 .writefn = gt_sec_tval_write,
2140 .resetfn = gt_sec_timer_reset,
2141 },
2142 { .name = "CNTPS_CTL_EL1", .state = ARM_CP_STATE_AA64,
2143 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 1,
2144 .type = ARM_CP_IO, .access = PL1_RW,
2145 .accessfn = gt_stimer_access,
2146 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].ctl),
2147 .resetvalue = 0,
2148 .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
2149 },
2150 { .name = "CNTPS_CVAL_EL1", .state = ARM_CP_STATE_AA64,
2151 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 2,
2152 .type = ARM_CP_IO, .access = PL1_RW,
2153 .accessfn = gt_stimer_access,
2154 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
2155 .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
2156 },
55d284af
PM
2157 REGINFO_SENTINEL
2158};
2159
2160#else
2161/* In user-mode none of the generic timer registers are accessible,
bc72ad67 2162 * and their implementation depends on QEMU_CLOCK_VIRTUAL and qdev gpio outputs,
55d284af
PM
2163 * so instead just don't register any of them.
2164 */
6cc7a3ae 2165static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
6cc7a3ae
PM
2166 REGINFO_SENTINEL
2167};
2168
55d284af
PM
2169#endif
2170
c4241c7d 2171static void par_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
4a501606 2172{
891a2fe7 2173 if (arm_feature(env, ARM_FEATURE_LPAE)) {
8d5c773e 2174 raw_write(env, ri, value);
891a2fe7 2175 } else if (arm_feature(env, ARM_FEATURE_V7)) {
8d5c773e 2176 raw_write(env, ri, value & 0xfffff6ff);
4a501606 2177 } else {
8d5c773e 2178 raw_write(env, ri, value & 0xfffff1ff);
4a501606 2179 }
4a501606
PM
2180}
2181
2182#ifndef CONFIG_USER_ONLY
2183/* get_phys_addr() isn't present for user-mode-only targets */
702a9357 2184
3f208fd7
PM
2185static CPAccessResult ats_access(CPUARMState *env, const ARMCPRegInfo *ri,
2186 bool isread)
92611c00
PM
2187{
2188 if (ri->opc2 & 4) {
87562e4f
PM
2189 /* The ATS12NSO* operations must trap to EL3 if executed in
2190 * Secure EL1 (which can only happen if EL3 is AArch64).
2191 * They are simply UNDEF if executed from NS EL1.
2192 * They function normally from EL2 or EL3.
92611c00 2193 */
87562e4f
PM
2194 if (arm_current_el(env) == 1) {
2195 if (arm_is_secure_below_el3(env)) {
2196 return CP_ACCESS_TRAP_UNCATEGORIZED_EL3;
2197 }
2198 return CP_ACCESS_TRAP_UNCATEGORIZED;
2199 }
92611c00
PM
2200 }
2201 return CP_ACCESS_OK;
2202}
2203
060e8a48 2204static uint64_t do_ats_write(CPUARMState *env, uint64_t value,
03ae85f8 2205 MMUAccessType access_type, ARMMMUIdx mmu_idx)
4a501606 2206{
a8170e5e 2207 hwaddr phys_addr;
4a501606
PM
2208 target_ulong page_size;
2209 int prot;
b7cc4e82 2210 bool ret;
01c097f7 2211 uint64_t par64;
1313e2d7 2212 bool format64 = false;
8bf5b6a9 2213 MemTxAttrs attrs = {};
e14b5a23 2214 ARMMMUFaultInfo fi = {};
5b2d261d 2215 ARMCacheAttrs cacheattrs = {};
4a501606 2216
5b2d261d 2217 ret = get_phys_addr(env, value, access_type, mmu_idx, &phys_addr, &attrs,
bc52bfeb 2218 &prot, &page_size, &fi, &cacheattrs);
1313e2d7
EI
2219
2220 if (is_a64(env)) {
2221 format64 = true;
2222 } else if (arm_feature(env, ARM_FEATURE_LPAE)) {
2223 /*
2224 * ATS1Cxx:
2225 * * TTBCR.EAE determines whether the result is returned using the
2226 * 32-bit or the 64-bit PAR format
2227 * * Instructions executed in Hyp mode always use the 64bit format
2228 *
2229 * ATS1S2NSOxx uses the 64bit format if any of the following is true:
2230 * * The Non-secure TTBCR.EAE bit is set to 1
2231 * * The implementation includes EL2, and the value of HCR.VM is 1
2232 *
2233 * ATS1Hx always uses the 64bit format (not supported yet).
2234 */
2235 format64 = arm_s1_regime_using_lpae_format(env, mmu_idx);
2236
2237 if (arm_feature(env, ARM_FEATURE_EL2)) {
2238 if (mmu_idx == ARMMMUIdx_S12NSE0 || mmu_idx == ARMMMUIdx_S12NSE1) {
2239 format64 |= env->cp15.hcr_el2 & HCR_VM;
2240 } else {
2241 format64 |= arm_current_el(env) == 2;
2242 }
2243 }
2244 }
2245
2246 if (format64) {
5efe9ed4 2247 /* Create a 64-bit PAR */
01c097f7 2248 par64 = (1 << 11); /* LPAE bit always set */
b7cc4e82 2249 if (!ret) {
702a9357 2250 par64 |= phys_addr & ~0xfffULL;
8bf5b6a9
PM
2251 if (!attrs.secure) {
2252 par64 |= (1 << 9); /* NS */
2253 }
5b2d261d
AB
2254 par64 |= (uint64_t)cacheattrs.attrs << 56; /* ATTR */
2255 par64 |= cacheattrs.shareability << 7; /* SH */
4a501606 2256 } else {
5efe9ed4
PM
2257 uint32_t fsr = arm_fi_to_lfsc(&fi);
2258
702a9357 2259 par64 |= 1; /* F */
b7cc4e82 2260 par64 |= (fsr & 0x3f) << 1; /* FS */
702a9357
PM
2261 /* Note that S2WLK and FSTAGE are always zero, because we don't
2262 * implement virtualization and therefore there can't be a stage 2
2263 * fault.
2264 */
4a501606
PM
2265 }
2266 } else {
b7cc4e82 2267 /* fsr is a DFSR/IFSR value for the short descriptor
702a9357
PM
2268 * translation table format (with WnR always clear).
2269 * Convert it to a 32-bit PAR.
2270 */
b7cc4e82 2271 if (!ret) {
702a9357
PM
2272 /* We do not set any attribute bits in the PAR */
2273 if (page_size == (1 << 24)
2274 && arm_feature(env, ARM_FEATURE_V7)) {
01c097f7 2275 par64 = (phys_addr & 0xff000000) | (1 << 1);
702a9357 2276 } else {
01c097f7 2277 par64 = phys_addr & 0xfffff000;
702a9357 2278 }
8bf5b6a9
PM
2279 if (!attrs.secure) {
2280 par64 |= (1 << 9); /* NS */
2281 }
702a9357 2282 } else {
5efe9ed4
PM
2283 uint32_t fsr = arm_fi_to_sfsc(&fi);
2284
b7cc4e82
PC
2285 par64 = ((fsr & (1 << 10)) >> 5) | ((fsr & (1 << 12)) >> 6) |
2286 ((fsr & 0xf) << 1) | 1;
702a9357 2287 }
4a501606 2288 }
060e8a48
PM
2289 return par64;
2290}
2291
2292static void ats_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
2293{
03ae85f8 2294 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
060e8a48 2295 uint64_t par64;
d3649702
PM
2296 ARMMMUIdx mmu_idx;
2297 int el = arm_current_el(env);
2298 bool secure = arm_is_secure_below_el3(env);
060e8a48 2299
d3649702
PM
2300 switch (ri->opc2 & 6) {
2301 case 0:
2302 /* stage 1 current state PL1: ATS1CPR, ATS1CPW */
2303 switch (el) {
2304 case 3:
2305 mmu_idx = ARMMMUIdx_S1E3;
2306 break;
2307 case 2:
2308 mmu_idx = ARMMMUIdx_S1NSE1;
2309 break;
2310 case 1:
2311 mmu_idx = secure ? ARMMMUIdx_S1SE1 : ARMMMUIdx_S1NSE1;
2312 break;
2313 default:
2314 g_assert_not_reached();
2315 }
2316 break;
2317 case 2:
2318 /* stage 1 current state PL0: ATS1CUR, ATS1CUW */
2319 switch (el) {
2320 case 3:
2321 mmu_idx = ARMMMUIdx_S1SE0;
2322 break;
2323 case 2:
2324 mmu_idx = ARMMMUIdx_S1NSE0;
2325 break;
2326 case 1:
2327 mmu_idx = secure ? ARMMMUIdx_S1SE0 : ARMMMUIdx_S1NSE0;
2328 break;
2329 default:
2330 g_assert_not_reached();
2331 }
2332 break;
2333 case 4:
2334 /* stage 1+2 NonSecure PL1: ATS12NSOPR, ATS12NSOPW */
2335 mmu_idx = ARMMMUIdx_S12NSE1;
2336 break;
2337 case 6:
2338 /* stage 1+2 NonSecure PL0: ATS12NSOUR, ATS12NSOUW */
2339 mmu_idx = ARMMMUIdx_S12NSE0;
2340 break;
2341 default:
2342 g_assert_not_reached();
2343 }
2344
2345 par64 = do_ats_write(env, value, access_type, mmu_idx);
01c097f7
FA
2346
2347 A32_BANKED_CURRENT_REG_SET(env, par, par64);
4a501606 2348}
060e8a48 2349
14db7fe0
PM
2350static void ats1h_write(CPUARMState *env, const ARMCPRegInfo *ri,
2351 uint64_t value)
2352{
03ae85f8 2353 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
14db7fe0
PM
2354 uint64_t par64;
2355
2356 par64 = do_ats_write(env, value, access_type, ARMMMUIdx_S2NS);
2357
2358 A32_BANKED_CURRENT_REG_SET(env, par, par64);
2359}
2360
3f208fd7
PM
2361static CPAccessResult at_s1e2_access(CPUARMState *env, const ARMCPRegInfo *ri,
2362 bool isread)
2a47df95
PM
2363{
2364 if (arm_current_el(env) == 3 && !(env->cp15.scr_el3 & SCR_NS)) {
2365 return CP_ACCESS_TRAP;
2366 }
2367 return CP_ACCESS_OK;
2368}
2369
060e8a48
PM
2370static void ats_write64(CPUARMState *env, const ARMCPRegInfo *ri,
2371 uint64_t value)
2372{
03ae85f8 2373 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
d3649702
PM
2374 ARMMMUIdx mmu_idx;
2375 int secure = arm_is_secure_below_el3(env);
2376
2377 switch (ri->opc2 & 6) {
2378 case 0:
2379 switch (ri->opc1) {
2380 case 0: /* AT S1E1R, AT S1E1W */
2381 mmu_idx = secure ? ARMMMUIdx_S1SE1 : ARMMMUIdx_S1NSE1;
2382 break;
2383 case 4: /* AT S1E2R, AT S1E2W */
2384 mmu_idx = ARMMMUIdx_S1E2;
2385 break;
2386 case 6: /* AT S1E3R, AT S1E3W */
2387 mmu_idx = ARMMMUIdx_S1E3;
2388 break;
2389 default:
2390 g_assert_not_reached();
2391 }
2392 break;
2393 case 2: /* AT S1E0R, AT S1E0W */
2394 mmu_idx = secure ? ARMMMUIdx_S1SE0 : ARMMMUIdx_S1NSE0;
2395 break;
2396 case 4: /* AT S12E1R, AT S12E1W */
2a47df95 2397 mmu_idx = secure ? ARMMMUIdx_S1SE1 : ARMMMUIdx_S12NSE1;
d3649702
PM
2398 break;
2399 case 6: /* AT S12E0R, AT S12E0W */
2a47df95 2400 mmu_idx = secure ? ARMMMUIdx_S1SE0 : ARMMMUIdx_S12NSE0;
d3649702
PM
2401 break;
2402 default:
2403 g_assert_not_reached();
2404 }
060e8a48 2405
d3649702 2406 env->cp15.par_el[1] = do_ats_write(env, value, access_type, mmu_idx);
060e8a48 2407}
4a501606
PM
2408#endif
2409
2410static const ARMCPRegInfo vapa_cp_reginfo[] = {
2411 { .name = "PAR", .cp = 15, .crn = 7, .crm = 4, .opc1 = 0, .opc2 = 0,
2412 .access = PL1_RW, .resetvalue = 0,
01c097f7
FA
2413 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.par_s),
2414 offsetoflow32(CPUARMState, cp15.par_ns) },
4a501606
PM
2415 .writefn = par_write },
2416#ifndef CONFIG_USER_ONLY
87562e4f 2417 /* This underdecoding is safe because the reginfo is NO_RAW. */
4a501606 2418 { .name = "ATS", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = CP_ANY,
92611c00 2419 .access = PL1_W, .accessfn = ats_access,
7a0e58fa 2420 .writefn = ats_write, .type = ARM_CP_NO_RAW },
4a501606
PM
2421#endif
2422 REGINFO_SENTINEL
2423};
2424
18032bec
PM
2425/* Return basic MPU access permission bits. */
2426static uint32_t simple_mpu_ap_bits(uint32_t val)
2427{
2428 uint32_t ret;
2429 uint32_t mask;
2430 int i;
2431 ret = 0;
2432 mask = 3;
2433 for (i = 0; i < 16; i += 2) {
2434 ret |= (val >> i) & mask;
2435 mask <<= 2;
2436 }
2437 return ret;
2438}
2439
2440/* Pad basic MPU access permission bits to extended format. */
2441static uint32_t extended_mpu_ap_bits(uint32_t val)
2442{
2443 uint32_t ret;
2444 uint32_t mask;
2445 int i;
2446 ret = 0;
2447 mask = 3;
2448 for (i = 0; i < 16; i += 2) {
2449 ret |= (val & mask) << i;
2450 mask <<= 2;
2451 }
2452 return ret;
2453}
2454
c4241c7d
PM
2455static void pmsav5_data_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
2456 uint64_t value)
18032bec 2457{
7e09797c 2458 env->cp15.pmsav5_data_ap = extended_mpu_ap_bits(value);
18032bec
PM
2459}
2460
c4241c7d 2461static uint64_t pmsav5_data_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
18032bec 2462{
7e09797c 2463 return simple_mpu_ap_bits(env->cp15.pmsav5_data_ap);
18032bec
PM
2464}
2465
c4241c7d
PM
2466static void pmsav5_insn_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
2467 uint64_t value)
18032bec 2468{
7e09797c 2469 env->cp15.pmsav5_insn_ap = extended_mpu_ap_bits(value);
18032bec
PM
2470}
2471
c4241c7d 2472static uint64_t pmsav5_insn_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
18032bec 2473{
7e09797c 2474 return simple_mpu_ap_bits(env->cp15.pmsav5_insn_ap);
18032bec
PM
2475}
2476
6cb0b013
PC
2477static uint64_t pmsav7_read(CPUARMState *env, const ARMCPRegInfo *ri)
2478{
2479 uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
2480
2481 if (!u32p) {
2482 return 0;
2483 }
2484
1bc04a88 2485 u32p += env->pmsav7.rnr[M_REG_NS];
6cb0b013
PC
2486 return *u32p;
2487}
2488
2489static void pmsav7_write(CPUARMState *env, const ARMCPRegInfo *ri,
2490 uint64_t value)
2491{
2492 ARMCPU *cpu = arm_env_get_cpu(env);
2493 uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
2494
2495 if (!u32p) {
2496 return;
2497 }
2498
1bc04a88 2499 u32p += env->pmsav7.rnr[M_REG_NS];
d10eb08f 2500 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
6cb0b013
PC
2501 *u32p = value;
2502}
2503
6cb0b013
PC
2504static void pmsav7_rgnr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2505 uint64_t value)
2506{
2507 ARMCPU *cpu = arm_env_get_cpu(env);
2508 uint32_t nrgs = cpu->pmsav7_dregion;
2509
2510 if (value >= nrgs) {
2511 qemu_log_mask(LOG_GUEST_ERROR,
2512 "PMSAv7 RGNR write >= # supported regions, %" PRIu32
2513 " > %" PRIu32 "\n", (uint32_t)value, nrgs);
2514 return;
2515 }
2516
2517 raw_write(env, ri, value);
2518}
2519
2520static const ARMCPRegInfo pmsav7_cp_reginfo[] = {
69ceea64
PM
2521 /* Reset for all these registers is handled in arm_cpu_reset(),
2522 * because the PMSAv7 is also used by M-profile CPUs, which do
2523 * not register cpregs but still need the state to be reset.
2524 */
6cb0b013
PC
2525 { .name = "DRBAR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 0,
2526 .access = PL1_RW, .type = ARM_CP_NO_RAW,
2527 .fieldoffset = offsetof(CPUARMState, pmsav7.drbar),
69ceea64
PM
2528 .readfn = pmsav7_read, .writefn = pmsav7_write,
2529 .resetfn = arm_cp_reset_ignore },
6cb0b013
PC
2530 { .name = "DRSR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 2,
2531 .access = PL1_RW, .type = ARM_CP_NO_RAW,
2532 .fieldoffset = offsetof(CPUARMState, pmsav7.drsr),
69ceea64
PM
2533 .readfn = pmsav7_read, .writefn = pmsav7_write,
2534 .resetfn = arm_cp_reset_ignore },
6cb0b013
PC
2535 { .name = "DRACR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 4,
2536 .access = PL1_RW, .type = ARM_CP_NO_RAW,
2537 .fieldoffset = offsetof(CPUARMState, pmsav7.dracr),
69ceea64
PM
2538 .readfn = pmsav7_read, .writefn = pmsav7_write,
2539 .resetfn = arm_cp_reset_ignore },
6cb0b013
PC
2540 { .name = "RGNR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 2, .opc2 = 0,
2541 .access = PL1_RW,
1bc04a88 2542 .fieldoffset = offsetof(CPUARMState, pmsav7.rnr[M_REG_NS]),
69ceea64
PM
2543 .writefn = pmsav7_rgnr_write,
2544 .resetfn = arm_cp_reset_ignore },
6cb0b013
PC
2545 REGINFO_SENTINEL
2546};
2547
18032bec
PM
2548static const ARMCPRegInfo pmsav5_cp_reginfo[] = {
2549 { .name = "DATA_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
7a0e58fa 2550 .access = PL1_RW, .type = ARM_CP_ALIAS,
7e09797c 2551 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
18032bec
PM
2552 .readfn = pmsav5_data_ap_read, .writefn = pmsav5_data_ap_write, },
2553 { .name = "INSN_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
7a0e58fa 2554 .access = PL1_RW, .type = ARM_CP_ALIAS,
7e09797c 2555 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
18032bec
PM
2556 .readfn = pmsav5_insn_ap_read, .writefn = pmsav5_insn_ap_write, },
2557 { .name = "DATA_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 2,
2558 .access = PL1_RW,
7e09797c
PM
2559 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
2560 .resetvalue = 0, },
18032bec
PM
2561 { .name = "INSN_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 3,
2562 .access = PL1_RW,
7e09797c
PM
2563 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
2564 .resetvalue = 0, },
ecce5c3c
PM
2565 { .name = "DCACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
2566 .access = PL1_RW,
2567 .fieldoffset = offsetof(CPUARMState, cp15.c2_data), .resetvalue = 0, },
2568 { .name = "ICACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 1,
2569 .access = PL1_RW,
2570 .fieldoffset = offsetof(CPUARMState, cp15.c2_insn), .resetvalue = 0, },
06d76f31 2571 /* Protection region base and size registers */
e508a92b
PM
2572 { .name = "946_PRBS0", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0,
2573 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2574 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[0]) },
2575 { .name = "946_PRBS1", .cp = 15, .crn = 6, .crm = 1, .opc1 = 0,
2576 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2577 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[1]) },
2578 { .name = "946_PRBS2", .cp = 15, .crn = 6, .crm = 2, .opc1 = 0,
2579 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2580 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[2]) },
2581 { .name = "946_PRBS3", .cp = 15, .crn = 6, .crm = 3, .opc1 = 0,
2582 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2583 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[3]) },
2584 { .name = "946_PRBS4", .cp = 15, .crn = 6, .crm = 4, .opc1 = 0,
2585 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2586 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[4]) },
2587 { .name = "946_PRBS5", .cp = 15, .crn = 6, .crm = 5, .opc1 = 0,
2588 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2589 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[5]) },
2590 { .name = "946_PRBS6", .cp = 15, .crn = 6, .crm = 6, .opc1 = 0,
2591 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2592 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[6]) },
2593 { .name = "946_PRBS7", .cp = 15, .crn = 6, .crm = 7, .opc1 = 0,
2594 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2595 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[7]) },
18032bec
PM
2596 REGINFO_SENTINEL
2597};
2598
c4241c7d
PM
2599static void vmsa_ttbcr_raw_write(CPUARMState *env, const ARMCPRegInfo *ri,
2600 uint64_t value)
ecce5c3c 2601{
11f136ee 2602 TCR *tcr = raw_ptr(env, ri);
2ebcebe2
PM
2603 int maskshift = extract32(value, 0, 3);
2604
e389be16
FA
2605 if (!arm_feature(env, ARM_FEATURE_V8)) {
2606 if (arm_feature(env, ARM_FEATURE_LPAE) && (value & TTBCR_EAE)) {
2607 /* Pre ARMv8 bits [21:19], [15:14] and [6:3] are UNK/SBZP when
2608 * using Long-desciptor translation table format */
2609 value &= ~((7 << 19) | (3 << 14) | (0xf << 3));
2610 } else if (arm_feature(env, ARM_FEATURE_EL3)) {
2611 /* In an implementation that includes the Security Extensions
2612 * TTBCR has additional fields PD0 [4] and PD1 [5] for
2613 * Short-descriptor translation table format.
2614 */
2615 value &= TTBCR_PD1 | TTBCR_PD0 | TTBCR_N;
2616 } else {
2617 value &= TTBCR_N;
2618 }
e42c4db3 2619 }
e389be16 2620
b6af0975 2621 /* Update the masks corresponding to the TCR bank being written
11f136ee 2622 * Note that we always calculate mask and base_mask, but
e42c4db3 2623 * they are only used for short-descriptor tables (ie if EAE is 0);
11f136ee
FA
2624 * for long-descriptor tables the TCR fields are used differently
2625 * and the mask and base_mask values are meaningless.
e42c4db3 2626 */
11f136ee
FA
2627 tcr->raw_tcr = value;
2628 tcr->mask = ~(((uint32_t)0xffffffffu) >> maskshift);
2629 tcr->base_mask = ~((uint32_t)0x3fffu >> maskshift);
ecce5c3c
PM
2630}
2631
c4241c7d
PM
2632static void vmsa_ttbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2633 uint64_t value)
d4e6df63 2634{
00c8cb0a
AF
2635 ARMCPU *cpu = arm_env_get_cpu(env);
2636
d4e6df63
PM
2637 if (arm_feature(env, ARM_FEATURE_LPAE)) {
2638 /* With LPAE the TTBCR could result in a change of ASID
2639 * via the TTBCR.A1 bit, so do a TLB flush.
2640 */
d10eb08f 2641 tlb_flush(CPU(cpu));
d4e6df63 2642 }
c4241c7d 2643 vmsa_ttbcr_raw_write(env, ri, value);
d4e6df63
PM
2644}
2645
ecce5c3c
PM
2646static void vmsa_ttbcr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2647{
11f136ee
FA
2648 TCR *tcr = raw_ptr(env, ri);
2649
2650 /* Reset both the TCR as well as the masks corresponding to the bank of
2651 * the TCR being reset.
2652 */
2653 tcr->raw_tcr = 0;
2654 tcr->mask = 0;
2655 tcr->base_mask = 0xffffc000u;
ecce5c3c
PM
2656}
2657
cb2e37df
PM
2658static void vmsa_tcr_el1_write(CPUARMState *env, const ARMCPRegInfo *ri,
2659 uint64_t value)
2660{
00c8cb0a 2661 ARMCPU *cpu = arm_env_get_cpu(env);
11f136ee 2662 TCR *tcr = raw_ptr(env, ri);
00c8cb0a 2663
cb2e37df 2664 /* For AArch64 the A1 bit could result in a change of ASID, so TLB flush. */
d10eb08f 2665 tlb_flush(CPU(cpu));
11f136ee 2666 tcr->raw_tcr = value;
cb2e37df
PM
2667}
2668
327ed10f
PM
2669static void vmsa_ttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2670 uint64_t value)
2671{
2672 /* 64 bit accesses to the TTBRs can change the ASID and so we
2673 * must flush the TLB.
2674 */
2675 if (cpreg_field_is_64bit(ri)) {
00c8cb0a
AF
2676 ARMCPU *cpu = arm_env_get_cpu(env);
2677
d10eb08f 2678 tlb_flush(CPU(cpu));
327ed10f
PM
2679 }
2680 raw_write(env, ri, value);
2681}
2682
b698e9cf
EI
2683static void vttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2684 uint64_t value)
2685{
2686 ARMCPU *cpu = arm_env_get_cpu(env);
2687 CPUState *cs = CPU(cpu);
2688
2689 /* Accesses to VTTBR may change the VMID so we must flush the TLB. */
2690 if (raw_read(env, ri) != value) {
0336cbf8 2691 tlb_flush_by_mmuidx(cs,
8bd5c820
PM
2692 ARMMMUIdxBit_S12NSE1 |
2693 ARMMMUIdxBit_S12NSE0 |
2694 ARMMMUIdxBit_S2NS);
b698e9cf
EI
2695 raw_write(env, ri, value);
2696 }
2697}
2698
8e5d75c9 2699static const ARMCPRegInfo vmsa_pmsa_cp_reginfo[] = {
18032bec 2700 { .name = "DFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
7a0e58fa 2701 .access = PL1_RW, .type = ARM_CP_ALIAS,
4a7e2d73 2702 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dfsr_s),
b061a82b 2703 offsetoflow32(CPUARMState, cp15.dfsr_ns) }, },
18032bec 2704 { .name = "IFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
88ca1c2d
FA
2705 .access = PL1_RW, .resetvalue = 0,
2706 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.ifsr_s),
2707 offsetoflow32(CPUARMState, cp15.ifsr_ns) } },
8e5d75c9
PC
2708 { .name = "DFAR", .cp = 15, .opc1 = 0, .crn = 6, .crm = 0, .opc2 = 0,
2709 .access = PL1_RW, .resetvalue = 0,
2710 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.dfar_s),
2711 offsetof(CPUARMState, cp15.dfar_ns) } },
2712 { .name = "FAR_EL1", .state = ARM_CP_STATE_AA64,
2713 .opc0 = 3, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 0,
2714 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[1]),
2715 .resetvalue = 0, },
2716 REGINFO_SENTINEL
2717};
2718
2719static const ARMCPRegInfo vmsa_cp_reginfo[] = {
6cd8a264
RH
2720 { .name = "ESR_EL1", .state = ARM_CP_STATE_AA64,
2721 .opc0 = 3, .crn = 5, .crm = 2, .opc1 = 0, .opc2 = 0,
2722 .access = PL1_RW,
d81c519c 2723 .fieldoffset = offsetof(CPUARMState, cp15.esr_el[1]), .resetvalue = 0, },
327ed10f 2724 { .name = "TTBR0_EL1", .state = ARM_CP_STATE_BOTH,
7dd8c9af
FA
2725 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 0,
2726 .access = PL1_RW, .writefn = vmsa_ttbr_write, .resetvalue = 0,
2727 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
2728 offsetof(CPUARMState, cp15.ttbr0_ns) } },
327ed10f 2729 { .name = "TTBR1_EL1", .state = ARM_CP_STATE_BOTH,
7dd8c9af
FA
2730 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 1,
2731 .access = PL1_RW, .writefn = vmsa_ttbr_write, .resetvalue = 0,
2732 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
2733 offsetof(CPUARMState, cp15.ttbr1_ns) } },
cb2e37df
PM
2734 { .name = "TCR_EL1", .state = ARM_CP_STATE_AA64,
2735 .opc0 = 3, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
2736 .access = PL1_RW, .writefn = vmsa_tcr_el1_write,
2737 .resetfn = vmsa_ttbcr_reset, .raw_writefn = raw_write,
11f136ee 2738 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[1]) },
cb2e37df 2739 { .name = "TTBCR", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
7a0e58fa 2740 .access = PL1_RW, .type = ARM_CP_ALIAS, .writefn = vmsa_ttbcr_write,
b061a82b 2741 .raw_writefn = vmsa_ttbcr_raw_write,
11f136ee
FA
2742 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tcr_el[3]),
2743 offsetoflow32(CPUARMState, cp15.tcr_el[1])} },
18032bec
PM
2744 REGINFO_SENTINEL
2745};
2746
c4241c7d
PM
2747static void omap_ticonfig_write(CPUARMState *env, const ARMCPRegInfo *ri,
2748 uint64_t value)
1047b9d7
PM
2749{
2750 env->cp15.c15_ticonfig = value & 0xe7;
2751 /* The OS_TYPE bit in this register changes the reported CPUID! */
2752 env->cp15.c0_cpuid = (value & (1 << 5)) ?
2753 ARM_CPUID_TI915T : ARM_CPUID_TI925T;
1047b9d7
PM
2754}
2755
c4241c7d
PM
2756static void omap_threadid_write(CPUARMState *env, const ARMCPRegInfo *ri,
2757 uint64_t value)
1047b9d7
PM
2758{
2759 env->cp15.c15_threadid = value & 0xffff;
1047b9d7
PM
2760}
2761
c4241c7d
PM
2762static void omap_wfi_write(CPUARMState *env, const ARMCPRegInfo *ri,
2763 uint64_t value)
1047b9d7
PM
2764{
2765 /* Wait-for-interrupt (deprecated) */
c3affe56 2766 cpu_interrupt(CPU(arm_env_get_cpu(env)), CPU_INTERRUPT_HALT);
1047b9d7
PM
2767}
2768
c4241c7d
PM
2769static void omap_cachemaint_write(CPUARMState *env, const ARMCPRegInfo *ri,
2770 uint64_t value)
c4804214
PM
2771{
2772 /* On OMAP there are registers indicating the max/min index of dcache lines
2773 * containing a dirty line; cache flush operations have to reset these.
2774 */
2775 env->cp15.c15_i_max = 0x000;
2776 env->cp15.c15_i_min = 0xff0;
c4804214
PM
2777}
2778
18032bec
PM
2779static const ARMCPRegInfo omap_cp_reginfo[] = {
2780 { .name = "DFSR", .cp = 15, .crn = 5, .crm = CP_ANY,
2781 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_OVERRIDE,
d81c519c 2782 .fieldoffset = offsetoflow32(CPUARMState, cp15.esr_el[1]),
6cd8a264 2783 .resetvalue = 0, },
1047b9d7
PM
2784 { .name = "", .cp = 15, .crn = 15, .crm = 0, .opc1 = 0, .opc2 = 0,
2785 .access = PL1_RW, .type = ARM_CP_NOP },
2786 { .name = "TICONFIG", .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0,
2787 .access = PL1_RW,
2788 .fieldoffset = offsetof(CPUARMState, cp15.c15_ticonfig), .resetvalue = 0,
2789 .writefn = omap_ticonfig_write },
2790 { .name = "IMAX", .cp = 15, .crn = 15, .crm = 2, .opc1 = 0, .opc2 = 0,
2791 .access = PL1_RW,
2792 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_max), .resetvalue = 0, },
2793 { .name = "IMIN", .cp = 15, .crn = 15, .crm = 3, .opc1 = 0, .opc2 = 0,
2794 .access = PL1_RW, .resetvalue = 0xff0,
2795 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_min) },
2796 { .name = "THREADID", .cp = 15, .crn = 15, .crm = 4, .opc1 = 0, .opc2 = 0,
2797 .access = PL1_RW,
2798 .fieldoffset = offsetof(CPUARMState, cp15.c15_threadid), .resetvalue = 0,
2799 .writefn = omap_threadid_write },
2800 { .name = "TI925T_STATUS", .cp = 15, .crn = 15,
2801 .crm = 8, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
7a0e58fa 2802 .type = ARM_CP_NO_RAW,
1047b9d7
PM
2803 .readfn = arm_cp_read_zero, .writefn = omap_wfi_write, },
2804 /* TODO: Peripheral port remap register:
2805 * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt controller
2806 * base address at $rn & ~0xfff and map size of 0x200 << ($rn & 0xfff),
2807 * when MMU is off.
2808 */
c4804214 2809 { .name = "OMAP_CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
d4e6df63 2810 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
7a0e58fa 2811 .type = ARM_CP_OVERRIDE | ARM_CP_NO_RAW,
c4804214 2812 .writefn = omap_cachemaint_write },
34f90529
PM
2813 { .name = "C9", .cp = 15, .crn = 9,
2814 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW,
2815 .type = ARM_CP_CONST | ARM_CP_OVERRIDE, .resetvalue = 0 },
1047b9d7
PM
2816 REGINFO_SENTINEL
2817};
2818
c4241c7d
PM
2819static void xscale_cpar_write(CPUARMState *env, const ARMCPRegInfo *ri,
2820 uint64_t value)
1047b9d7 2821{
c0f4af17 2822 env->cp15.c15_cpar = value & 0x3fff;
1047b9d7
PM
2823}
2824
2825static const ARMCPRegInfo xscale_cp_reginfo[] = {
2826 { .name = "XSCALE_CPAR",
2827 .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
2828 .fieldoffset = offsetof(CPUARMState, cp15.c15_cpar), .resetvalue = 0,
2829 .writefn = xscale_cpar_write, },
2771db27
PM
2830 { .name = "XSCALE_AUXCR",
2831 .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 1, .access = PL1_RW,
2832 .fieldoffset = offsetof(CPUARMState, cp15.c1_xscaleauxcr),
2833 .resetvalue = 0, },
3b771579
PM
2834 /* XScale specific cache-lockdown: since we have no cache we NOP these
2835 * and hope the guest does not really rely on cache behaviour.
2836 */
2837 { .name = "XSCALE_LOCK_ICACHE_LINE",
2838 .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 0,
2839 .access = PL1_W, .type = ARM_CP_NOP },
2840 { .name = "XSCALE_UNLOCK_ICACHE",
2841 .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 1,
2842 .access = PL1_W, .type = ARM_CP_NOP },
2843 { .name = "XSCALE_DCACHE_LOCK",
2844 .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 0,
2845 .access = PL1_RW, .type = ARM_CP_NOP },
2846 { .name = "XSCALE_UNLOCK_DCACHE",
2847 .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 1,
2848 .access = PL1_W, .type = ARM_CP_NOP },
1047b9d7
PM
2849 REGINFO_SENTINEL
2850};
2851
2852static const ARMCPRegInfo dummy_c15_cp_reginfo[] = {
2853 /* RAZ/WI the whole crn=15 space, when we don't have a more specific
2854 * implementation of this implementation-defined space.
2855 * Ideally this should eventually disappear in favour of actually
2856 * implementing the correct behaviour for all cores.
2857 */
2858 { .name = "C15_IMPDEF", .cp = 15, .crn = 15,
2859 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
3671cd87 2860 .access = PL1_RW,
7a0e58fa 2861 .type = ARM_CP_CONST | ARM_CP_NO_RAW | ARM_CP_OVERRIDE,
d4e6df63 2862 .resetvalue = 0 },
18032bec
PM
2863 REGINFO_SENTINEL
2864};
2865
c4804214
PM
2866static const ARMCPRegInfo cache_dirty_status_cp_reginfo[] = {
2867 /* Cache status: RAZ because we have no cache so it's always clean */
2868 { .name = "CDSR", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 6,
7a0e58fa 2869 .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 2870 .resetvalue = 0 },
c4804214
PM
2871 REGINFO_SENTINEL
2872};
2873
2874static const ARMCPRegInfo cache_block_ops_cp_reginfo[] = {
2875 /* We never have a a block transfer operation in progress */
2876 { .name = "BXSR", .cp = 15, .crn = 7, .crm = 12, .opc1 = 0, .opc2 = 4,
7a0e58fa 2877 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 2878 .resetvalue = 0 },
30b05bba
PM
2879 /* The cache ops themselves: these all NOP for QEMU */
2880 { .name = "IICR", .cp = 15, .crm = 5, .opc1 = 0,
2881 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
2882 { .name = "IDCR", .cp = 15, .crm = 6, .opc1 = 0,
2883 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
2884 { .name = "CDCR", .cp = 15, .crm = 12, .opc1 = 0,
2885 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
2886 { .name = "PIR", .cp = 15, .crm = 12, .opc1 = 1,
2887 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
2888 { .name = "PDR", .cp = 15, .crm = 12, .opc1 = 2,
2889 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
2890 { .name = "CIDCR", .cp = 15, .crm = 14, .opc1 = 0,
2891 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
c4804214
PM
2892 REGINFO_SENTINEL
2893};
2894
2895static const ARMCPRegInfo cache_test_clean_cp_reginfo[] = {
2896 /* The cache test-and-clean instructions always return (1 << 30)
2897 * to indicate that there are no dirty cache lines.
2898 */
2899 { .name = "TC_DCACHE", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 3,
7a0e58fa 2900 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 2901 .resetvalue = (1 << 30) },
c4804214 2902 { .name = "TCI_DCACHE", .cp = 15, .crn = 7, .crm = 14, .opc1 = 0, .opc2 = 3,
7a0e58fa 2903 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 2904 .resetvalue = (1 << 30) },
c4804214
PM
2905 REGINFO_SENTINEL
2906};
2907
34f90529
PM
2908static const ARMCPRegInfo strongarm_cp_reginfo[] = {
2909 /* Ignore ReadBuffer accesses */
2910 { .name = "C9_READBUFFER", .cp = 15, .crn = 9,
2911 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
d4e6df63 2912 .access = PL1_RW, .resetvalue = 0,
7a0e58fa 2913 .type = ARM_CP_CONST | ARM_CP_OVERRIDE | ARM_CP_NO_RAW },
34f90529
PM
2914 REGINFO_SENTINEL
2915};
2916
731de9e6
EI
2917static uint64_t midr_read(CPUARMState *env, const ARMCPRegInfo *ri)
2918{
2919 ARMCPU *cpu = arm_env_get_cpu(env);
2920 unsigned int cur_el = arm_current_el(env);
2921 bool secure = arm_is_secure(env);
2922
2923 if (arm_feature(&cpu->env, ARM_FEATURE_EL2) && !secure && cur_el == 1) {
2924 return env->cp15.vpidr_el2;
2925 }
2926 return raw_read(env, ri);
2927}
2928
06a7e647 2929static uint64_t mpidr_read_val(CPUARMState *env)
81bdde9d 2930{
eb5e1d3c
PF
2931 ARMCPU *cpu = ARM_CPU(arm_env_get_cpu(env));
2932 uint64_t mpidr = cpu->mp_affinity;
2933
81bdde9d 2934 if (arm_feature(env, ARM_FEATURE_V7MP)) {
78dbbbe4 2935 mpidr |= (1U << 31);
81bdde9d
PM
2936 /* Cores which are uniprocessor (non-coherent)
2937 * but still implement the MP extensions set
a8e81b31 2938 * bit 30. (For instance, Cortex-R5).
81bdde9d 2939 */
a8e81b31
PC
2940 if (cpu->mp_is_up) {
2941 mpidr |= (1u << 30);
2942 }
81bdde9d 2943 }
c4241c7d 2944 return mpidr;
81bdde9d
PM
2945}
2946
06a7e647
EI
2947static uint64_t mpidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
2948{
f0d574d6
EI
2949 unsigned int cur_el = arm_current_el(env);
2950 bool secure = arm_is_secure(env);
2951
2952 if (arm_feature(env, ARM_FEATURE_EL2) && !secure && cur_el == 1) {
2953 return env->cp15.vmpidr_el2;
2954 }
06a7e647
EI
2955 return mpidr_read_val(env);
2956}
2957
81bdde9d 2958static const ARMCPRegInfo mpidr_cp_reginfo[] = {
4b7fff2f
PM
2959 { .name = "MPIDR", .state = ARM_CP_STATE_BOTH,
2960 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 5,
7a0e58fa 2961 .access = PL1_R, .readfn = mpidr_read, .type = ARM_CP_NO_RAW },
81bdde9d
PM
2962 REGINFO_SENTINEL
2963};
2964
7ac681cf 2965static const ARMCPRegInfo lpae_cp_reginfo[] = {
a903c449 2966 /* NOP AMAIR0/1 */
b0fe2427
PM
2967 { .name = "AMAIR0", .state = ARM_CP_STATE_BOTH,
2968 .opc0 = 3, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 0,
a903c449 2969 .access = PL1_RW, .type = ARM_CP_CONST,
7ac681cf 2970 .resetvalue = 0 },
b0fe2427 2971 /* AMAIR1 is mapped to AMAIR_EL1[63:32] */
7ac681cf 2972 { .name = "AMAIR1", .cp = 15, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 1,
a903c449 2973 .access = PL1_RW, .type = ARM_CP_CONST,
7ac681cf 2974 .resetvalue = 0 },
891a2fe7 2975 { .name = "PAR", .cp = 15, .crm = 7, .opc1 = 0,
01c097f7
FA
2976 .access = PL1_RW, .type = ARM_CP_64BIT, .resetvalue = 0,
2977 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.par_s),
2978 offsetof(CPUARMState, cp15.par_ns)} },
891a2fe7 2979 { .name = "TTBR0", .cp = 15, .crm = 2, .opc1 = 0,
7a0e58fa 2980 .access = PL1_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
7dd8c9af
FA
2981 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
2982 offsetof(CPUARMState, cp15.ttbr0_ns) },
b061a82b 2983 .writefn = vmsa_ttbr_write, },
891a2fe7 2984 { .name = "TTBR1", .cp = 15, .crm = 2, .opc1 = 1,
7a0e58fa 2985 .access = PL1_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
7dd8c9af
FA
2986 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
2987 offsetof(CPUARMState, cp15.ttbr1_ns) },
b061a82b 2988 .writefn = vmsa_ttbr_write, },
7ac681cf
PM
2989 REGINFO_SENTINEL
2990};
2991
c4241c7d 2992static uint64_t aa64_fpcr_read(CPUARMState *env, const ARMCPRegInfo *ri)
b0d2b7d0 2993{
c4241c7d 2994 return vfp_get_fpcr(env);
b0d2b7d0
PM
2995}
2996
c4241c7d
PM
2997static void aa64_fpcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2998 uint64_t value)
b0d2b7d0
PM
2999{
3000 vfp_set_fpcr(env, value);
b0d2b7d0
PM
3001}
3002
c4241c7d 3003static uint64_t aa64_fpsr_read(CPUARMState *env, const ARMCPRegInfo *ri)
b0d2b7d0 3004{
c4241c7d 3005 return vfp_get_fpsr(env);
b0d2b7d0
PM
3006}
3007
c4241c7d
PM
3008static void aa64_fpsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3009 uint64_t value)
b0d2b7d0
PM
3010{
3011 vfp_set_fpsr(env, value);
b0d2b7d0
PM
3012}
3013
3f208fd7
PM
3014static CPAccessResult aa64_daif_access(CPUARMState *env, const ARMCPRegInfo *ri,
3015 bool isread)
c2b820fe 3016{
137feaa9 3017 if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_UMA)) {
c2b820fe
PM
3018 return CP_ACCESS_TRAP;
3019 }
3020 return CP_ACCESS_OK;
3021}
3022
3023static void aa64_daif_write(CPUARMState *env, const ARMCPRegInfo *ri,
3024 uint64_t value)
3025{
3026 env->daif = value & PSTATE_DAIF;
3027}
3028
8af35c37 3029static CPAccessResult aa64_cacheop_access(CPUARMState *env,
3f208fd7
PM
3030 const ARMCPRegInfo *ri,
3031 bool isread)
8af35c37
PM
3032{
3033 /* Cache invalidate/clean: NOP, but EL0 must UNDEF unless
3034 * SCTLR_EL1.UCI is set.
3035 */
137feaa9 3036 if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_UCI)) {
8af35c37
PM
3037 return CP_ACCESS_TRAP;
3038 }
3039 return CP_ACCESS_OK;
3040}
3041
dbb1fb27
AB
3042/* See: D4.7.2 TLB maintenance requirements and the TLB maintenance instructions
3043 * Page D4-1736 (DDI0487A.b)
3044 */
3045
fd3ed969
PM
3046static void tlbi_aa64_vmalle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
3047 uint64_t value)
168aa23b 3048{
a67cf277 3049 CPUState *cs = ENV_GET_CPU(env);
dbb1fb27 3050
fd3ed969 3051 if (arm_is_secure_below_el3(env)) {
0336cbf8 3052 tlb_flush_by_mmuidx(cs,
8bd5c820
PM
3053 ARMMMUIdxBit_S1SE1 |
3054 ARMMMUIdxBit_S1SE0);
fd3ed969 3055 } else {
0336cbf8 3056 tlb_flush_by_mmuidx(cs,
8bd5c820
PM
3057 ARMMMUIdxBit_S12NSE1 |
3058 ARMMMUIdxBit_S12NSE0);
fd3ed969 3059 }
168aa23b
PM
3060}
3061
fd3ed969
PM
3062static void tlbi_aa64_vmalle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
3063 uint64_t value)
168aa23b 3064{
a67cf277 3065 CPUState *cs = ENV_GET_CPU(env);
fd3ed969 3066 bool sec = arm_is_secure_below_el3(env);
dbb1fb27 3067
a67cf277
AB
3068 if (sec) {
3069 tlb_flush_by_mmuidx_all_cpus_synced(cs,
8bd5c820
PM
3070 ARMMMUIdxBit_S1SE1 |
3071 ARMMMUIdxBit_S1SE0);
a67cf277
AB
3072 } else {
3073 tlb_flush_by_mmuidx_all_cpus_synced(cs,
8bd5c820
PM
3074 ARMMMUIdxBit_S12NSE1 |
3075 ARMMMUIdxBit_S12NSE0);
fd3ed969 3076 }
168aa23b
PM
3077}
3078
fd3ed969
PM
3079static void tlbi_aa64_alle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
3080 uint64_t value)
168aa23b 3081{
fd3ed969
PM
3082 /* Note that the 'ALL' scope must invalidate both stage 1 and
3083 * stage 2 translations, whereas most other scopes only invalidate
3084 * stage 1 translations.
3085 */
00c8cb0a 3086 ARMCPU *cpu = arm_env_get_cpu(env);
fd3ed969
PM
3087 CPUState *cs = CPU(cpu);
3088
3089 if (arm_is_secure_below_el3(env)) {
0336cbf8 3090 tlb_flush_by_mmuidx(cs,
8bd5c820
PM
3091 ARMMMUIdxBit_S1SE1 |
3092 ARMMMUIdxBit_S1SE0);
fd3ed969
PM
3093 } else {
3094 if (arm_feature(env, ARM_FEATURE_EL2)) {
0336cbf8 3095 tlb_flush_by_mmuidx(cs,
8bd5c820
PM
3096 ARMMMUIdxBit_S12NSE1 |
3097 ARMMMUIdxBit_S12NSE0 |
3098 ARMMMUIdxBit_S2NS);
fd3ed969 3099 } else {
0336cbf8 3100 tlb_flush_by_mmuidx(cs,
8bd5c820
PM
3101 ARMMMUIdxBit_S12NSE1 |
3102 ARMMMUIdxBit_S12NSE0);
fd3ed969
PM
3103 }
3104 }
168aa23b
PM
3105}
3106
fd3ed969 3107static void tlbi_aa64_alle2_write(CPUARMState *env, const ARMCPRegInfo *ri,
fa439fc5
PM
3108 uint64_t value)
3109{
fd3ed969
PM
3110 ARMCPU *cpu = arm_env_get_cpu(env);
3111 CPUState *cs = CPU(cpu);
3112
8bd5c820 3113 tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_S1E2);
fd3ed969
PM
3114}
3115
43efaa33
PM
3116static void tlbi_aa64_alle3_write(CPUARMState *env, const ARMCPRegInfo *ri,
3117 uint64_t value)
3118{
3119 ARMCPU *cpu = arm_env_get_cpu(env);
3120 CPUState *cs = CPU(cpu);
3121
8bd5c820 3122 tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_S1E3);
43efaa33
PM
3123}
3124
fd3ed969
PM
3125static void tlbi_aa64_alle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
3126 uint64_t value)
3127{
3128 /* Note that the 'ALL' scope must invalidate both stage 1 and
3129 * stage 2 translations, whereas most other scopes only invalidate
3130 * stage 1 translations.
3131 */
a67cf277 3132 CPUState *cs = ENV_GET_CPU(env);
fd3ed969
PM
3133 bool sec = arm_is_secure_below_el3(env);
3134 bool has_el2 = arm_feature(env, ARM_FEATURE_EL2);
a67cf277
AB
3135
3136 if (sec) {
3137 tlb_flush_by_mmuidx_all_cpus_synced(cs,
8bd5c820
PM
3138 ARMMMUIdxBit_S1SE1 |
3139 ARMMMUIdxBit_S1SE0);
a67cf277
AB
3140 } else if (has_el2) {
3141 tlb_flush_by_mmuidx_all_cpus_synced(cs,
8bd5c820
PM
3142 ARMMMUIdxBit_S12NSE1 |
3143 ARMMMUIdxBit_S12NSE0 |
3144 ARMMMUIdxBit_S2NS);
a67cf277
AB
3145 } else {
3146 tlb_flush_by_mmuidx_all_cpus_synced(cs,
8bd5c820
PM
3147 ARMMMUIdxBit_S12NSE1 |
3148 ARMMMUIdxBit_S12NSE0);
fa439fc5
PM
3149 }
3150}
3151
2bfb9d75
PM
3152static void tlbi_aa64_alle2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
3153 uint64_t value)
3154{
a67cf277 3155 CPUState *cs = ENV_GET_CPU(env);
2bfb9d75 3156
8bd5c820 3157 tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_S1E2);
2bfb9d75
PM
3158}
3159
43efaa33
PM
3160static void tlbi_aa64_alle3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
3161 uint64_t value)
3162{
a67cf277 3163 CPUState *cs = ENV_GET_CPU(env);
43efaa33 3164
8bd5c820 3165 tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_S1E3);
43efaa33
PM
3166}
3167
fd3ed969
PM
3168static void tlbi_aa64_vae1_write(CPUARMState *env, const ARMCPRegInfo *ri,
3169 uint64_t value)
3170{
3171 /* Invalidate by VA, EL1&0 (AArch64 version).
3172 * Currently handles all of VAE1, VAAE1, VAALE1 and VALE1,
3173 * since we don't support flush-for-specific-ASID-only or
3174 * flush-last-level-only.
3175 */
3176 ARMCPU *cpu = arm_env_get_cpu(env);
3177 CPUState *cs = CPU(cpu);
3178 uint64_t pageaddr = sextract64(value << 12, 0, 56);
3179
3180 if (arm_is_secure_below_el3(env)) {
0336cbf8 3181 tlb_flush_page_by_mmuidx(cs, pageaddr,
8bd5c820
PM
3182 ARMMMUIdxBit_S1SE1 |
3183 ARMMMUIdxBit_S1SE0);
fd3ed969 3184 } else {
0336cbf8 3185 tlb_flush_page_by_mmuidx(cs, pageaddr,
8bd5c820
PM
3186 ARMMMUIdxBit_S12NSE1 |
3187 ARMMMUIdxBit_S12NSE0);
fd3ed969
PM
3188 }
3189}
3190
3191static void tlbi_aa64_vae2_write(CPUARMState *env, const ARMCPRegInfo *ri,
3192 uint64_t value)
fa439fc5 3193{
fd3ed969
PM
3194 /* Invalidate by VA, EL2
3195 * Currently handles both VAE2 and VALE2, since we don't support
3196 * flush-last-level-only.
3197 */
3198 ARMCPU *cpu = arm_env_get_cpu(env);
3199 CPUState *cs = CPU(cpu);
3200 uint64_t pageaddr = sextract64(value << 12, 0, 56);
3201
8bd5c820 3202 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_S1E2);
fd3ed969
PM
3203}
3204
43efaa33
PM
3205static void tlbi_aa64_vae3_write(CPUARMState *env, const ARMCPRegInfo *ri,
3206 uint64_t value)
3207{
3208 /* Invalidate by VA, EL3
3209 * Currently handles both VAE3 and VALE3, since we don't support
3210 * flush-last-level-only.
3211 */
3212 ARMCPU *cpu = arm_env_get_cpu(env);
3213 CPUState *cs = CPU(cpu);
3214 uint64_t pageaddr = sextract64(value << 12, 0, 56);
3215
8bd5c820 3216 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_S1E3);
43efaa33
PM
3217}
3218
fd3ed969
PM
3219static void tlbi_aa64_vae1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
3220 uint64_t value)
3221{
a67cf277
AB
3222 ARMCPU *cpu = arm_env_get_cpu(env);
3223 CPUState *cs = CPU(cpu);
fd3ed969 3224 bool sec = arm_is_secure_below_el3(env);
fa439fc5
PM
3225 uint64_t pageaddr = sextract64(value << 12, 0, 56);
3226
a67cf277
AB
3227 if (sec) {
3228 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
8bd5c820
PM
3229 ARMMMUIdxBit_S1SE1 |
3230 ARMMMUIdxBit_S1SE0);
a67cf277
AB
3231 } else {
3232 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
8bd5c820
PM
3233 ARMMMUIdxBit_S12NSE1 |
3234 ARMMMUIdxBit_S12NSE0);
fa439fc5
PM
3235 }
3236}
3237
fd3ed969
PM
3238static void tlbi_aa64_vae2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
3239 uint64_t value)
fa439fc5 3240{
a67cf277 3241 CPUState *cs = ENV_GET_CPU(env);
fd3ed969 3242 uint64_t pageaddr = sextract64(value << 12, 0, 56);
fa439fc5 3243
a67cf277 3244 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
8bd5c820 3245 ARMMMUIdxBit_S1E2);
fa439fc5
PM
3246}
3247
43efaa33
PM
3248static void tlbi_aa64_vae3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
3249 uint64_t value)
3250{
a67cf277 3251 CPUState *cs = ENV_GET_CPU(env);
43efaa33
PM
3252 uint64_t pageaddr = sextract64(value << 12, 0, 56);
3253
a67cf277 3254 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
8bd5c820 3255 ARMMMUIdxBit_S1E3);
43efaa33
PM
3256}
3257
cea66e91
PM
3258static void tlbi_aa64_ipas2e1_write(CPUARMState *env, const ARMCPRegInfo *ri,
3259 uint64_t value)
3260{
3261 /* Invalidate by IPA. This has to invalidate any structures that
3262 * contain only stage 2 translation information, but does not need
3263 * to apply to structures that contain combined stage 1 and stage 2
3264 * translation information.
3265 * This must NOP if EL2 isn't implemented or SCR_EL3.NS is zero.
3266 */
3267 ARMCPU *cpu = arm_env_get_cpu(env);
3268 CPUState *cs = CPU(cpu);
3269 uint64_t pageaddr;
3270
3271 if (!arm_feature(env, ARM_FEATURE_EL2) || !(env->cp15.scr_el3 & SCR_NS)) {
3272 return;
3273 }
3274
3275 pageaddr = sextract64(value << 12, 0, 48);
3276
8bd5c820 3277 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_S2NS);
cea66e91
PM
3278}
3279
3280static void tlbi_aa64_ipas2e1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
3281 uint64_t value)
3282{
a67cf277 3283 CPUState *cs = ENV_GET_CPU(env);
cea66e91
PM
3284 uint64_t pageaddr;
3285
3286 if (!arm_feature(env, ARM_FEATURE_EL2) || !(env->cp15.scr_el3 & SCR_NS)) {
3287 return;
3288 }
3289
3290 pageaddr = sextract64(value << 12, 0, 48);
3291
a67cf277 3292 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
8bd5c820 3293 ARMMMUIdxBit_S2NS);
cea66e91
PM
3294}
3295
3f208fd7
PM
3296static CPAccessResult aa64_zva_access(CPUARMState *env, const ARMCPRegInfo *ri,
3297 bool isread)
aca3f40b
PM
3298{
3299 /* We don't implement EL2, so the only control on DC ZVA is the
3300 * bit in the SCTLR which can prohibit access for EL0.
3301 */
137feaa9 3302 if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_DZE)) {
aca3f40b
PM
3303 return CP_ACCESS_TRAP;
3304 }
3305 return CP_ACCESS_OK;
3306}
3307
3308static uint64_t aa64_dczid_read(CPUARMState *env, const ARMCPRegInfo *ri)
3309{
3310 ARMCPU *cpu = arm_env_get_cpu(env);
3311 int dzp_bit = 1 << 4;
3312
3313 /* DZP indicates whether DC ZVA access is allowed */
3f208fd7 3314 if (aa64_zva_access(env, NULL, false) == CP_ACCESS_OK) {
aca3f40b
PM
3315 dzp_bit = 0;
3316 }
3317 return cpu->dcz_blocksize | dzp_bit;
3318}
3319
3f208fd7
PM
3320static CPAccessResult sp_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
3321 bool isread)
f502cfc2 3322{
cdcf1405 3323 if (!(env->pstate & PSTATE_SP)) {
f502cfc2
PM
3324 /* Access to SP_EL0 is undefined if it's being used as
3325 * the stack pointer.
3326 */
3327 return CP_ACCESS_TRAP_UNCATEGORIZED;
3328 }
3329 return CP_ACCESS_OK;
3330}
3331
3332static uint64_t spsel_read(CPUARMState *env, const ARMCPRegInfo *ri)
3333{
3334 return env->pstate & PSTATE_SP;
3335}
3336
3337static void spsel_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
3338{
3339 update_spsel(env, val);
3340}
3341
137feaa9
FA
3342static void sctlr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3343 uint64_t value)
3344{
3345 ARMCPU *cpu = arm_env_get_cpu(env);
3346
3347 if (raw_read(env, ri) == value) {
3348 /* Skip the TLB flush if nothing actually changed; Linux likes
3349 * to do a lot of pointless SCTLR writes.
3350 */
3351 return;
3352 }
3353
06312feb
PM
3354 if (arm_feature(env, ARM_FEATURE_PMSA) && !cpu->has_mpu) {
3355 /* M bit is RAZ/WI for PMSA with no MPU implemented */
3356 value &= ~SCTLR_M;
3357 }
3358
137feaa9
FA
3359 raw_write(env, ri, value);
3360 /* ??? Lots of these bits are not implemented. */
3361 /* This may enable/disable the MMU, so do a TLB flush. */
d10eb08f 3362 tlb_flush(CPU(cpu));
137feaa9
FA
3363}
3364
3f208fd7
PM
3365static CPAccessResult fpexc32_access(CPUARMState *env, const ARMCPRegInfo *ri,
3366 bool isread)
03fbf20f
PM
3367{
3368 if ((env->cp15.cptr_el[2] & CPTR_TFP) && arm_current_el(env) == 2) {
f2cae609 3369 return CP_ACCESS_TRAP_FP_EL2;
03fbf20f
PM
3370 }
3371 if (env->cp15.cptr_el[3] & CPTR_TFP) {
f2cae609 3372 return CP_ACCESS_TRAP_FP_EL3;
03fbf20f
PM
3373 }
3374 return CP_ACCESS_OK;
3375}
3376
a8d64e73
PM
3377static void sdcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3378 uint64_t value)
3379{
3380 env->cp15.mdcr_el3 = value & SDCR_VALID_MASK;
3381}
3382
b0d2b7d0
PM
3383static const ARMCPRegInfo v8_cp_reginfo[] = {
3384 /* Minimal set of EL0-visible registers. This will need to be expanded
3385 * significantly for system emulation of AArch64 CPUs.
3386 */
3387 { .name = "NZCV", .state = ARM_CP_STATE_AA64,
3388 .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 2,
3389 .access = PL0_RW, .type = ARM_CP_NZCV },
c2b820fe
PM
3390 { .name = "DAIF", .state = ARM_CP_STATE_AA64,
3391 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 2,
7a0e58fa 3392 .type = ARM_CP_NO_RAW,
c2b820fe
PM
3393 .access = PL0_RW, .accessfn = aa64_daif_access,
3394 .fieldoffset = offsetof(CPUARMState, daif),
3395 .writefn = aa64_daif_write, .resetfn = arm_cp_reset_ignore },
b0d2b7d0
PM
3396 { .name = "FPCR", .state = ARM_CP_STATE_AA64,
3397 .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 4,
b916c9c3 3398 .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END,
fe03d45f 3399 .readfn = aa64_fpcr_read, .writefn = aa64_fpcr_write },
b0d2b7d0
PM
3400 { .name = "FPSR", .state = ARM_CP_STATE_AA64,
3401 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 4,
b916c9c3 3402 .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END,
fe03d45f 3403 .readfn = aa64_fpsr_read, .writefn = aa64_fpsr_write },
b0d2b7d0
PM
3404 { .name = "DCZID_EL0", .state = ARM_CP_STATE_AA64,
3405 .opc0 = 3, .opc1 = 3, .opc2 = 7, .crn = 0, .crm = 0,
7a0e58fa 3406 .access = PL0_R, .type = ARM_CP_NO_RAW,
aca3f40b
PM
3407 .readfn = aa64_dczid_read },
3408 { .name = "DC_ZVA", .state = ARM_CP_STATE_AA64,
3409 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 1,
3410 .access = PL0_W, .type = ARM_CP_DC_ZVA,
3411#ifndef CONFIG_USER_ONLY
3412 /* Avoid overhead of an access check that always passes in user-mode */
3413 .accessfn = aa64_zva_access,
3414#endif
3415 },
0eef9d98
PM
3416 { .name = "CURRENTEL", .state = ARM_CP_STATE_AA64,
3417 .opc0 = 3, .opc1 = 0, .opc2 = 2, .crn = 4, .crm = 2,
3418 .access = PL1_R, .type = ARM_CP_CURRENTEL },
8af35c37
PM
3419 /* Cache ops: all NOPs since we don't emulate caches */
3420 { .name = "IC_IALLUIS", .state = ARM_CP_STATE_AA64,
3421 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
3422 .access = PL1_W, .type = ARM_CP_NOP },
3423 { .name = "IC_IALLU", .state = ARM_CP_STATE_AA64,
3424 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
3425 .access = PL1_W, .type = ARM_CP_NOP },
3426 { .name = "IC_IVAU", .state = ARM_CP_STATE_AA64,
3427 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 5, .opc2 = 1,
3428 .access = PL0_W, .type = ARM_CP_NOP,
3429 .accessfn = aa64_cacheop_access },
3430 { .name = "DC_IVAC", .state = ARM_CP_STATE_AA64,
3431 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
3432 .access = PL1_W, .type = ARM_CP_NOP },
3433 { .name = "DC_ISW", .state = ARM_CP_STATE_AA64,
3434 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
3435 .access = PL1_W, .type = ARM_CP_NOP },
3436 { .name = "DC_CVAC", .state = ARM_CP_STATE_AA64,
3437 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 1,
3438 .access = PL0_W, .type = ARM_CP_NOP,
3439 .accessfn = aa64_cacheop_access },
3440 { .name = "DC_CSW", .state = ARM_CP_STATE_AA64,
3441 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
3442 .access = PL1_W, .type = ARM_CP_NOP },
3443 { .name = "DC_CVAU", .state = ARM_CP_STATE_AA64,
3444 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 11, .opc2 = 1,
3445 .access = PL0_W, .type = ARM_CP_NOP,
3446 .accessfn = aa64_cacheop_access },
3447 { .name = "DC_CIVAC", .state = ARM_CP_STATE_AA64,
3448 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 1,
3449 .access = PL0_W, .type = ARM_CP_NOP,
3450 .accessfn = aa64_cacheop_access },
3451 { .name = "DC_CISW", .state = ARM_CP_STATE_AA64,
3452 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
3453 .access = PL1_W, .type = ARM_CP_NOP },
168aa23b
PM
3454 /* TLBI operations */
3455 { .name = "TLBI_VMALLE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 3456 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
7a0e58fa 3457 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 3458 .writefn = tlbi_aa64_vmalle1is_write },
168aa23b 3459 { .name = "TLBI_VAE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 3460 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
7a0e58fa 3461 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 3462 .writefn = tlbi_aa64_vae1is_write },
168aa23b 3463 { .name = "TLBI_ASIDE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 3464 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
7a0e58fa 3465 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 3466 .writefn = tlbi_aa64_vmalle1is_write },
168aa23b 3467 { .name = "TLBI_VAAE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 3468 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
7a0e58fa 3469 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 3470 .writefn = tlbi_aa64_vae1is_write },
168aa23b 3471 { .name = "TLBI_VALE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 3472 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
7a0e58fa 3473 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 3474 .writefn = tlbi_aa64_vae1is_write },
168aa23b 3475 { .name = "TLBI_VAALE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 3476 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
7a0e58fa 3477 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 3478 .writefn = tlbi_aa64_vae1is_write },
168aa23b 3479 { .name = "TLBI_VMALLE1", .state = ARM_CP_STATE_AA64,
6ab9f499 3480 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
7a0e58fa 3481 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 3482 .writefn = tlbi_aa64_vmalle1_write },
168aa23b 3483 { .name = "TLBI_VAE1", .state = ARM_CP_STATE_AA64,
6ab9f499 3484 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
7a0e58fa 3485 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 3486 .writefn = tlbi_aa64_vae1_write },
168aa23b 3487 { .name = "TLBI_ASIDE1", .state = ARM_CP_STATE_AA64,
6ab9f499 3488 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
7a0e58fa 3489 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 3490 .writefn = tlbi_aa64_vmalle1_write },
168aa23b 3491 { .name = "TLBI_VAAE1", .state = ARM_CP_STATE_AA64,
6ab9f499 3492 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
7a0e58fa 3493 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 3494 .writefn = tlbi_aa64_vae1_write },
168aa23b 3495 { .name = "TLBI_VALE1", .state = ARM_CP_STATE_AA64,
6ab9f499 3496 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
7a0e58fa 3497 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 3498 .writefn = tlbi_aa64_vae1_write },
168aa23b 3499 { .name = "TLBI_VAALE1", .state = ARM_CP_STATE_AA64,
6ab9f499 3500 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
7a0e58fa 3501 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 3502 .writefn = tlbi_aa64_vae1_write },
cea66e91
PM
3503 { .name = "TLBI_IPAS2E1IS", .state = ARM_CP_STATE_AA64,
3504 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1,
3505 .access = PL2_W, .type = ARM_CP_NO_RAW,
3506 .writefn = tlbi_aa64_ipas2e1is_write },
3507 { .name = "TLBI_IPAS2LE1IS", .state = ARM_CP_STATE_AA64,
3508 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5,
3509 .access = PL2_W, .type = ARM_CP_NO_RAW,
3510 .writefn = tlbi_aa64_ipas2e1is_write },
83ddf975
PM
3511 { .name = "TLBI_ALLE1IS", .state = ARM_CP_STATE_AA64,
3512 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
3513 .access = PL2_W, .type = ARM_CP_NO_RAW,
fd3ed969 3514 .writefn = tlbi_aa64_alle1is_write },
43efaa33
PM
3515 { .name = "TLBI_VMALLS12E1IS", .state = ARM_CP_STATE_AA64,
3516 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 6,
3517 .access = PL2_W, .type = ARM_CP_NO_RAW,
3518 .writefn = tlbi_aa64_alle1is_write },
cea66e91
PM
3519 { .name = "TLBI_IPAS2E1", .state = ARM_CP_STATE_AA64,
3520 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1,
3521 .access = PL2_W, .type = ARM_CP_NO_RAW,
3522 .writefn = tlbi_aa64_ipas2e1_write },
3523 { .name = "TLBI_IPAS2LE1", .state = ARM_CP_STATE_AA64,
3524 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5,
3525 .access = PL2_W, .type = ARM_CP_NO_RAW,
3526 .writefn = tlbi_aa64_ipas2e1_write },
83ddf975
PM
3527 { .name = "TLBI_ALLE1", .state = ARM_CP_STATE_AA64,
3528 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
3529 .access = PL2_W, .type = ARM_CP_NO_RAW,
fd3ed969 3530 .writefn = tlbi_aa64_alle1_write },
43efaa33
PM
3531 { .name = "TLBI_VMALLS12E1", .state = ARM_CP_STATE_AA64,
3532 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 6,
3533 .access = PL2_W, .type = ARM_CP_NO_RAW,
3534 .writefn = tlbi_aa64_alle1is_write },
19525524
PM
3535#ifndef CONFIG_USER_ONLY
3536 /* 64 bit address translation operations */
3537 { .name = "AT_S1E1R", .state = ARM_CP_STATE_AA64,
3538 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 0,
060e8a48 3539 .access = PL1_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
19525524
PM
3540 { .name = "AT_S1E1W", .state = ARM_CP_STATE_AA64,
3541 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 1,
060e8a48 3542 .access = PL1_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
19525524
PM
3543 { .name = "AT_S1E0R", .state = ARM_CP_STATE_AA64,
3544 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 2,
060e8a48 3545 .access = PL1_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
19525524
PM
3546 { .name = "AT_S1E0W", .state = ARM_CP_STATE_AA64,
3547 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 3,
060e8a48 3548 .access = PL1_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
2a47df95 3549 { .name = "AT_S12E1R", .state = ARM_CP_STATE_AA64,
7a379c7e 3550 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 4,
2a47df95
PM
3551 .access = PL2_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3552 { .name = "AT_S12E1W", .state = ARM_CP_STATE_AA64,
7a379c7e 3553 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 5,
2a47df95
PM
3554 .access = PL2_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3555 { .name = "AT_S12E0R", .state = ARM_CP_STATE_AA64,
7a379c7e 3556 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 6,
2a47df95
PM
3557 .access = PL2_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3558 { .name = "AT_S12E0W", .state = ARM_CP_STATE_AA64,
7a379c7e 3559 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 7,
2a47df95
PM
3560 .access = PL2_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3561 /* AT S1E2* are elsewhere as they UNDEF from EL3 if EL2 is not present */
3562 { .name = "AT_S1E3R", .state = ARM_CP_STATE_AA64,
3563 .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 0,
3564 .access = PL3_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3565 { .name = "AT_S1E3W", .state = ARM_CP_STATE_AA64,
3566 .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 1,
3567 .access = PL3_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
c96fc9b5
EI
3568 { .name = "PAR_EL1", .state = ARM_CP_STATE_AA64,
3569 .type = ARM_CP_ALIAS,
3570 .opc0 = 3, .opc1 = 0, .crn = 7, .crm = 4, .opc2 = 0,
3571 .access = PL1_RW, .resetvalue = 0,
3572 .fieldoffset = offsetof(CPUARMState, cp15.par_el[1]),
3573 .writefn = par_write },
19525524 3574#endif
995939a6 3575 /* TLB invalidate last level of translation table walk */
9449fdf6 3576 { .name = "TLBIMVALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
7a0e58fa 3577 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_is_write },
9449fdf6 3578 { .name = "TLBIMVAALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
7a0e58fa 3579 .type = ARM_CP_NO_RAW, .access = PL1_W,
fa439fc5 3580 .writefn = tlbimvaa_is_write },
9449fdf6 3581 { .name = "TLBIMVAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
7a0e58fa 3582 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write },
9449fdf6 3583 { .name = "TLBIMVAAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
7a0e58fa 3584 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimvaa_write },
541ef8c2
SS
3585 { .name = "TLBIMVALH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
3586 .type = ARM_CP_NO_RAW, .access = PL2_W,
3587 .writefn = tlbimva_hyp_write },
3588 { .name = "TLBIMVALHIS",
3589 .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
3590 .type = ARM_CP_NO_RAW, .access = PL2_W,
3591 .writefn = tlbimva_hyp_is_write },
3592 { .name = "TLBIIPAS2",
3593 .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1,
3594 .type = ARM_CP_NO_RAW, .access = PL2_W,
3595 .writefn = tlbiipas2_write },
3596 { .name = "TLBIIPAS2IS",
3597 .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1,
3598 .type = ARM_CP_NO_RAW, .access = PL2_W,
3599 .writefn = tlbiipas2_is_write },
3600 { .name = "TLBIIPAS2L",
3601 .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5,
3602 .type = ARM_CP_NO_RAW, .access = PL2_W,
3603 .writefn = tlbiipas2_write },
3604 { .name = "TLBIIPAS2LIS",
3605 .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5,
3606 .type = ARM_CP_NO_RAW, .access = PL2_W,
3607 .writefn = tlbiipas2_is_write },
9449fdf6
PM
3608 /* 32 bit cache operations */
3609 { .name = "ICIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
3610 .type = ARM_CP_NOP, .access = PL1_W },
3611 { .name = "BPIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 6,
3612 .type = ARM_CP_NOP, .access = PL1_W },
3613 { .name = "ICIALLU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
3614 .type = ARM_CP_NOP, .access = PL1_W },
3615 { .name = "ICIMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 1,
3616 .type = ARM_CP_NOP, .access = PL1_W },
3617 { .name = "BPIALL", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 6,
3618 .type = ARM_CP_NOP, .access = PL1_W },
3619 { .name = "BPIMVA", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 7,
3620 .type = ARM_CP_NOP, .access = PL1_W },
3621 { .name = "DCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
3622 .type = ARM_CP_NOP, .access = PL1_W },
3623 { .name = "DCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
3624 .type = ARM_CP_NOP, .access = PL1_W },
3625 { .name = "DCCMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 1,
3626 .type = ARM_CP_NOP, .access = PL1_W },
3627 { .name = "DCCSW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
3628 .type = ARM_CP_NOP, .access = PL1_W },
3629 { .name = "DCCMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 11, .opc2 = 1,
3630 .type = ARM_CP_NOP, .access = PL1_W },
3631 { .name = "DCCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 1,
3632 .type = ARM_CP_NOP, .access = PL1_W },
3633 { .name = "DCCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
3634 .type = ARM_CP_NOP, .access = PL1_W },
3635 /* MMU Domain access control / MPU write buffer control */
0c17d68c
FA
3636 { .name = "DACR", .cp = 15, .opc1 = 0, .crn = 3, .crm = 0, .opc2 = 0,
3637 .access = PL1_RW, .resetvalue = 0,
3638 .writefn = dacr_write, .raw_writefn = raw_write,
3639 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
3640 offsetoflow32(CPUARMState, cp15.dacr_ns) } },
a0618a19 3641 { .name = "ELR_EL1", .state = ARM_CP_STATE_AA64,
7a0e58fa 3642 .type = ARM_CP_ALIAS,
a0618a19 3643 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 1,
6947f059
EI
3644 .access = PL1_RW,
3645 .fieldoffset = offsetof(CPUARMState, elr_el[1]) },
a65f1de9 3646 { .name = "SPSR_EL1", .state = ARM_CP_STATE_AA64,
7a0e58fa 3647 .type = ARM_CP_ALIAS,
a65f1de9 3648 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 0,
99a99c1f
SB
3649 .access = PL1_RW,
3650 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_SVC]) },
f502cfc2
PM
3651 /* We rely on the access checks not allowing the guest to write to the
3652 * state field when SPSel indicates that it's being used as the stack
3653 * pointer.
3654 */
3655 { .name = "SP_EL0", .state = ARM_CP_STATE_AA64,
3656 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 1, .opc2 = 0,
3657 .access = PL1_RW, .accessfn = sp_el0_access,
7a0e58fa 3658 .type = ARM_CP_ALIAS,
f502cfc2 3659 .fieldoffset = offsetof(CPUARMState, sp_el[0]) },
884b4dee
GB
3660 { .name = "SP_EL1", .state = ARM_CP_STATE_AA64,
3661 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 1, .opc2 = 0,
7a0e58fa 3662 .access = PL2_RW, .type = ARM_CP_ALIAS,
884b4dee 3663 .fieldoffset = offsetof(CPUARMState, sp_el[1]) },
f502cfc2
PM
3664 { .name = "SPSel", .state = ARM_CP_STATE_AA64,
3665 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 0,
7a0e58fa 3666 .type = ARM_CP_NO_RAW,
f502cfc2 3667 .access = PL1_RW, .readfn = spsel_read, .writefn = spsel_write },
03fbf20f
PM
3668 { .name = "FPEXC32_EL2", .state = ARM_CP_STATE_AA64,
3669 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 3, .opc2 = 0,
3670 .type = ARM_CP_ALIAS,
3671 .fieldoffset = offsetof(CPUARMState, vfp.xregs[ARM_VFP_FPEXC]),
3672 .access = PL2_RW, .accessfn = fpexc32_access },
6a43e0b6
PM
3673 { .name = "DACR32_EL2", .state = ARM_CP_STATE_AA64,
3674 .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 0, .opc2 = 0,
3675 .access = PL2_RW, .resetvalue = 0,
3676 .writefn = dacr_write, .raw_writefn = raw_write,
3677 .fieldoffset = offsetof(CPUARMState, cp15.dacr32_el2) },
3678 { .name = "IFSR32_EL2", .state = ARM_CP_STATE_AA64,
3679 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 0, .opc2 = 1,
3680 .access = PL2_RW, .resetvalue = 0,
3681 .fieldoffset = offsetof(CPUARMState, cp15.ifsr32_el2) },
3682 { .name = "SPSR_IRQ", .state = ARM_CP_STATE_AA64,
3683 .type = ARM_CP_ALIAS,
3684 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 0,
3685 .access = PL2_RW,
3686 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_IRQ]) },
3687 { .name = "SPSR_ABT", .state = ARM_CP_STATE_AA64,
3688 .type = ARM_CP_ALIAS,
3689 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 1,
3690 .access = PL2_RW,
3691 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_ABT]) },
3692 { .name = "SPSR_UND", .state = ARM_CP_STATE_AA64,
3693 .type = ARM_CP_ALIAS,
3694 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 2,
3695 .access = PL2_RW,
3696 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_UND]) },
3697 { .name = "SPSR_FIQ", .state = ARM_CP_STATE_AA64,
3698 .type = ARM_CP_ALIAS,
3699 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 3,
3700 .access = PL2_RW,
3701 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_FIQ]) },
a8d64e73
PM
3702 { .name = "MDCR_EL3", .state = ARM_CP_STATE_AA64,
3703 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 3, .opc2 = 1,
3704 .resetvalue = 0,
3705 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el3) },
3706 { .name = "SDCR", .type = ARM_CP_ALIAS,
3707 .cp = 15, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 1,
3708 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
3709 .writefn = sdcr_write,
3710 .fieldoffset = offsetoflow32(CPUARMState, cp15.mdcr_el3) },
b0d2b7d0
PM
3711 REGINFO_SENTINEL
3712};
3713
d42e3c26 3714/* Used to describe the behaviour of EL2 regs when EL2 does not exist. */
4771cd01 3715static const ARMCPRegInfo el3_no_el2_cp_reginfo[] = {
d42e3c26
EI
3716 { .name = "VBAR_EL2", .state = ARM_CP_STATE_AA64,
3717 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0,
3718 .access = PL2_RW,
3719 .readfn = arm_cp_read_zero, .writefn = arm_cp_write_ignore },
f149e3e8 3720 { .name = "HCR_EL2", .state = ARM_CP_STATE_AA64,
7a0e58fa 3721 .type = ARM_CP_NO_RAW,
f149e3e8
EI
3722 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
3723 .access = PL2_RW,
3724 .readfn = arm_cp_read_zero, .writefn = arm_cp_write_ignore },
c6f19164
GB
3725 { .name = "CPTR_EL2", .state = ARM_CP_STATE_BOTH,
3726 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 2,
3727 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
95f949ac
EI
3728 { .name = "MAIR_EL2", .state = ARM_CP_STATE_BOTH,
3729 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 0,
3730 .access = PL2_RW, .type = ARM_CP_CONST,
3731 .resetvalue = 0 },
3732 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
3733 .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 1,
3734 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
2179ef95
PM
3735 { .name = "AMAIR_EL2", .state = ARM_CP_STATE_BOTH,
3736 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 0,
3737 .access = PL2_RW, .type = ARM_CP_CONST,
3738 .resetvalue = 0 },
3739 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
3740 .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 1,
3741 .access = PL2_RW, .type = ARM_CP_CONST,
3742 .resetvalue = 0 },
37cd6c24
PM
3743 { .name = "AFSR0_EL2", .state = ARM_CP_STATE_BOTH,
3744 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 0,
3745 .access = PL2_RW, .type = ARM_CP_CONST,
3746 .resetvalue = 0 },
3747 { .name = "AFSR1_EL2", .state = ARM_CP_STATE_BOTH,
3748 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 1,
3749 .access = PL2_RW, .type = ARM_CP_CONST,
3750 .resetvalue = 0 },
06ec4c8c
EI
3751 { .name = "TCR_EL2", .state = ARM_CP_STATE_BOTH,
3752 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 2,
3753 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
68e9c2fe
EI
3754 { .name = "VTCR_EL2", .state = ARM_CP_STATE_BOTH,
3755 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
3756 .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any,
3757 .type = ARM_CP_CONST, .resetvalue = 0 },
b698e9cf
EI
3758 { .name = "VTTBR", .state = ARM_CP_STATE_AA32,
3759 .cp = 15, .opc1 = 6, .crm = 2,
3760 .access = PL2_RW, .accessfn = access_el3_aa32ns,
3761 .type = ARM_CP_CONST | ARM_CP_64BIT, .resetvalue = 0 },
3762 { .name = "VTTBR_EL2", .state = ARM_CP_STATE_AA64,
3763 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 0,
3764 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
b9cb5323
EI
3765 { .name = "SCTLR_EL2", .state = ARM_CP_STATE_BOTH,
3766 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 0,
3767 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
ff05f37b
EI
3768 { .name = "TPIDR_EL2", .state = ARM_CP_STATE_BOTH,
3769 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 2,
3770 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
a57633c0
EI
3771 { .name = "TTBR0_EL2", .state = ARM_CP_STATE_AA64,
3772 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
3773 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3774 { .name = "HTTBR", .cp = 15, .opc1 = 4, .crm = 2,
3775 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_CONST,
3776 .resetvalue = 0 },
0b6440af
EI
3777 { .name = "CNTHCTL_EL2", .state = ARM_CP_STATE_BOTH,
3778 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 1, .opc2 = 0,
3779 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
edac4d8a
EI
3780 { .name = "CNTVOFF_EL2", .state = ARM_CP_STATE_AA64,
3781 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 3,
3782 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3783 { .name = "CNTVOFF", .cp = 15, .opc1 = 4, .crm = 14,
3784 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_CONST,
3785 .resetvalue = 0 },
b0e66d95
EI
3786 { .name = "CNTHP_CVAL_EL2", .state = ARM_CP_STATE_AA64,
3787 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 2,
3788 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3789 { .name = "CNTHP_CVAL", .cp = 15, .opc1 = 6, .crm = 14,
3790 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_CONST,
3791 .resetvalue = 0 },
3792 { .name = "CNTHP_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
3793 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 0,
3794 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3795 { .name = "CNTHP_CTL_EL2", .state = ARM_CP_STATE_BOTH,
3796 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 1,
3797 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
14cc7b54
SF
3798 { .name = "MDCR_EL2", .state = ARM_CP_STATE_BOTH,
3799 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 1,
d6c8cf81
PM
3800 .access = PL2_RW, .accessfn = access_tda,
3801 .type = ARM_CP_CONST, .resetvalue = 0 },
59e05530
EI
3802 { .name = "HPFAR_EL2", .state = ARM_CP_STATE_BOTH,
3803 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
3804 .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any,
3805 .type = ARM_CP_CONST, .resetvalue = 0 },
2a5a9abd
AF
3806 { .name = "HSTR_EL2", .state = ARM_CP_STATE_BOTH,
3807 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 3,
3808 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
d42e3c26
EI
3809 REGINFO_SENTINEL
3810};
3811
f149e3e8
EI
3812static void hcr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
3813{
3814 ARMCPU *cpu = arm_env_get_cpu(env);
3815 uint64_t valid_mask = HCR_MASK;
3816
3817 if (arm_feature(env, ARM_FEATURE_EL3)) {
3818 valid_mask &= ~HCR_HCD;
77077a83
JK
3819 } else if (cpu->psci_conduit != QEMU_PSCI_CONDUIT_SMC) {
3820 /* Architecturally HCR.TSC is RES0 if EL3 is not implemented.
3821 * However, if we're using the SMC PSCI conduit then QEMU is
3822 * effectively acting like EL3 firmware and so the guest at
3823 * EL2 should retain the ability to prevent EL1 from being
3824 * able to make SMC calls into the ersatz firmware, so in
3825 * that case HCR.TSC should be read/write.
3826 */
f149e3e8
EI
3827 valid_mask &= ~HCR_TSC;
3828 }
3829
3830 /* Clear RES0 bits. */
3831 value &= valid_mask;
3832
3833 /* These bits change the MMU setup:
3834 * HCR_VM enables stage 2 translation
3835 * HCR_PTW forbids certain page-table setups
3836 * HCR_DC Disables stage1 and enables stage2 translation
3837 */
3838 if ((raw_read(env, ri) ^ value) & (HCR_VM | HCR_PTW | HCR_DC)) {
d10eb08f 3839 tlb_flush(CPU(cpu));
f149e3e8
EI
3840 }
3841 raw_write(env, ri, value);
3842}
3843
4771cd01 3844static const ARMCPRegInfo el2_cp_reginfo[] = {
f149e3e8
EI
3845 { .name = "HCR_EL2", .state = ARM_CP_STATE_AA64,
3846 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
3847 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
3848 .writefn = hcr_write },
3b685ba7 3849 { .name = "ELR_EL2", .state = ARM_CP_STATE_AA64,
7a0e58fa 3850 .type = ARM_CP_ALIAS,
3b685ba7
EI
3851 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 1,
3852 .access = PL2_RW,
3853 .fieldoffset = offsetof(CPUARMState, elr_el[2]) },
f2c30f42 3854 { .name = "ESR_EL2", .state = ARM_CP_STATE_AA64,
f2c30f42
EI
3855 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 0,
3856 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[2]) },
63b60551
EI
3857 { .name = "FAR_EL2", .state = ARM_CP_STATE_AA64,
3858 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 0,
3859 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[2]) },
3b685ba7 3860 { .name = "SPSR_EL2", .state = ARM_CP_STATE_AA64,
7a0e58fa 3861 .type = ARM_CP_ALIAS,
3b685ba7 3862 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 0,
99a99c1f
SB
3863 .access = PL2_RW,
3864 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_HYP]) },
d42e3c26
EI
3865 { .name = "VBAR_EL2", .state = ARM_CP_STATE_AA64,
3866 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0,
3867 .access = PL2_RW, .writefn = vbar_write,
3868 .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[2]),
3869 .resetvalue = 0 },
884b4dee
GB
3870 { .name = "SP_EL2", .state = ARM_CP_STATE_AA64,
3871 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 1, .opc2 = 0,
7a0e58fa 3872 .access = PL3_RW, .type = ARM_CP_ALIAS,
884b4dee 3873 .fieldoffset = offsetof(CPUARMState, sp_el[2]) },
c6f19164
GB
3874 { .name = "CPTR_EL2", .state = ARM_CP_STATE_BOTH,
3875 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 2,
3876 .access = PL2_RW, .accessfn = cptr_access, .resetvalue = 0,
3877 .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[2]) },
95f949ac
EI
3878 { .name = "MAIR_EL2", .state = ARM_CP_STATE_BOTH,
3879 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 0,
3880 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[2]),
3881 .resetvalue = 0 },
3882 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
3883 .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 1,
3884 .access = PL2_RW, .type = ARM_CP_ALIAS,
3885 .fieldoffset = offsetofhigh32(CPUARMState, cp15.mair_el[2]) },
2179ef95
PM
3886 { .name = "AMAIR_EL2", .state = ARM_CP_STATE_BOTH,
3887 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 0,
3888 .access = PL2_RW, .type = ARM_CP_CONST,
3889 .resetvalue = 0 },
3890 /* HAMAIR1 is mapped to AMAIR_EL2[63:32] */
3891 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
3892 .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 1,
3893 .access = PL2_RW, .type = ARM_CP_CONST,
3894 .resetvalue = 0 },
37cd6c24
PM
3895 { .name = "AFSR0_EL2", .state = ARM_CP_STATE_BOTH,
3896 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 0,
3897 .access = PL2_RW, .type = ARM_CP_CONST,
3898 .resetvalue = 0 },
3899 { .name = "AFSR1_EL2", .state = ARM_CP_STATE_BOTH,
3900 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 1,
3901 .access = PL2_RW, .type = ARM_CP_CONST,
3902 .resetvalue = 0 },
06ec4c8c
EI
3903 { .name = "TCR_EL2", .state = ARM_CP_STATE_BOTH,
3904 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 2,
6459b94c
PM
3905 .access = PL2_RW,
3906 /* no .writefn needed as this can't cause an ASID change;
3907 * no .raw_writefn or .resetfn needed as we never use mask/base_mask
3908 */
06ec4c8c 3909 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[2]) },
68e9c2fe
EI
3910 { .name = "VTCR", .state = ARM_CP_STATE_AA32,
3911 .cp = 15, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
bf06c112 3912 .type = ARM_CP_ALIAS,
68e9c2fe
EI
3913 .access = PL2_RW, .accessfn = access_el3_aa32ns,
3914 .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) },
3915 { .name = "VTCR_EL2", .state = ARM_CP_STATE_AA64,
3916 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
bf06c112
PM
3917 .access = PL2_RW,
3918 /* no .writefn needed as this can't cause an ASID change;
3919 * no .raw_writefn or .resetfn needed as we never use mask/base_mask
3920 */
68e9c2fe 3921 .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) },
b698e9cf
EI
3922 { .name = "VTTBR", .state = ARM_CP_STATE_AA32,
3923 .cp = 15, .opc1 = 6, .crm = 2,
3924 .type = ARM_CP_64BIT | ARM_CP_ALIAS,
3925 .access = PL2_RW, .accessfn = access_el3_aa32ns,
3926 .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2),
3927 .writefn = vttbr_write },
3928 { .name = "VTTBR_EL2", .state = ARM_CP_STATE_AA64,
3929 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 0,
3930 .access = PL2_RW, .writefn = vttbr_write,
3931 .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2) },
b9cb5323
EI
3932 { .name = "SCTLR_EL2", .state = ARM_CP_STATE_BOTH,
3933 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 0,
3934 .access = PL2_RW, .raw_writefn = raw_write, .writefn = sctlr_write,
3935 .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[2]) },
ff05f37b
EI
3936 { .name = "TPIDR_EL2", .state = ARM_CP_STATE_BOTH,
3937 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 2,
3938 .access = PL2_RW, .resetvalue = 0,
3939 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[2]) },
a57633c0
EI
3940 { .name = "TTBR0_EL2", .state = ARM_CP_STATE_AA64,
3941 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
3942 .access = PL2_RW, .resetvalue = 0,
3943 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
3944 { .name = "HTTBR", .cp = 15, .opc1 = 4, .crm = 2,
3945 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
a57633c0 3946 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
541ef8c2
SS
3947 { .name = "TLBIALLNSNH",
3948 .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
3949 .type = ARM_CP_NO_RAW, .access = PL2_W,
3950 .writefn = tlbiall_nsnh_write },
3951 { .name = "TLBIALLNSNHIS",
3952 .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
3953 .type = ARM_CP_NO_RAW, .access = PL2_W,
3954 .writefn = tlbiall_nsnh_is_write },
3955 { .name = "TLBIALLH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
3956 .type = ARM_CP_NO_RAW, .access = PL2_W,
3957 .writefn = tlbiall_hyp_write },
3958 { .name = "TLBIALLHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
3959 .type = ARM_CP_NO_RAW, .access = PL2_W,
3960 .writefn = tlbiall_hyp_is_write },
3961 { .name = "TLBIMVAH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
3962 .type = ARM_CP_NO_RAW, .access = PL2_W,
3963 .writefn = tlbimva_hyp_write },
3964 { .name = "TLBIMVAHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
3965 .type = ARM_CP_NO_RAW, .access = PL2_W,
3966 .writefn = tlbimva_hyp_is_write },
51da9014
EI
3967 { .name = "TLBI_ALLE2", .state = ARM_CP_STATE_AA64,
3968 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
3969 .type = ARM_CP_NO_RAW, .access = PL2_W,
fd3ed969 3970 .writefn = tlbi_aa64_alle2_write },
8742d49d
EI
3971 { .name = "TLBI_VAE2", .state = ARM_CP_STATE_AA64,
3972 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
3973 .type = ARM_CP_NO_RAW, .access = PL2_W,
fd3ed969 3974 .writefn = tlbi_aa64_vae2_write },
2bfb9d75
PM
3975 { .name = "TLBI_VALE2", .state = ARM_CP_STATE_AA64,
3976 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
3977 .access = PL2_W, .type = ARM_CP_NO_RAW,
3978 .writefn = tlbi_aa64_vae2_write },
3979 { .name = "TLBI_ALLE2IS", .state = ARM_CP_STATE_AA64,
3980 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
3981 .access = PL2_W, .type = ARM_CP_NO_RAW,
3982 .writefn = tlbi_aa64_alle2is_write },
8742d49d
EI
3983 { .name = "TLBI_VAE2IS", .state = ARM_CP_STATE_AA64,
3984 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
3985 .type = ARM_CP_NO_RAW, .access = PL2_W,
fd3ed969 3986 .writefn = tlbi_aa64_vae2is_write },
2bfb9d75
PM
3987 { .name = "TLBI_VALE2IS", .state = ARM_CP_STATE_AA64,
3988 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
3989 .access = PL2_W, .type = ARM_CP_NO_RAW,
3990 .writefn = tlbi_aa64_vae2is_write },
edac4d8a 3991#ifndef CONFIG_USER_ONLY
2a47df95
PM
3992 /* Unlike the other EL2-related AT operations, these must
3993 * UNDEF from EL3 if EL2 is not implemented, which is why we
3994 * define them here rather than with the rest of the AT ops.
3995 */
3996 { .name = "AT_S1E2R", .state = ARM_CP_STATE_AA64,
3997 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
3998 .access = PL2_W, .accessfn = at_s1e2_access,
3999 .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
4000 { .name = "AT_S1E2W", .state = ARM_CP_STATE_AA64,
4001 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
4002 .access = PL2_W, .accessfn = at_s1e2_access,
4003 .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
14db7fe0
PM
4004 /* The AArch32 ATS1H* operations are CONSTRAINED UNPREDICTABLE
4005 * if EL2 is not implemented; we choose to UNDEF. Behaviour at EL3
4006 * with SCR.NS == 0 outside Monitor mode is UNPREDICTABLE; we choose
4007 * to behave as if SCR.NS was 1.
4008 */
4009 { .name = "ATS1HR", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
4010 .access = PL2_W,
4011 .writefn = ats1h_write, .type = ARM_CP_NO_RAW },
4012 { .name = "ATS1HW", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
4013 .access = PL2_W,
4014 .writefn = ats1h_write, .type = ARM_CP_NO_RAW },
0b6440af
EI
4015 { .name = "CNTHCTL_EL2", .state = ARM_CP_STATE_BOTH,
4016 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 1, .opc2 = 0,
4017 /* ARMv7 requires bit 0 and 1 to reset to 1. ARMv8 defines the
4018 * reset values as IMPDEF. We choose to reset to 3 to comply with
4019 * both ARMv7 and ARMv8.
4020 */
4021 .access = PL2_RW, .resetvalue = 3,
4022 .fieldoffset = offsetof(CPUARMState, cp15.cnthctl_el2) },
edac4d8a
EI
4023 { .name = "CNTVOFF_EL2", .state = ARM_CP_STATE_AA64,
4024 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 3,
4025 .access = PL2_RW, .type = ARM_CP_IO, .resetvalue = 0,
4026 .writefn = gt_cntvoff_write,
4027 .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
4028 { .name = "CNTVOFF", .cp = 15, .opc1 = 4, .crm = 14,
4029 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS | ARM_CP_IO,
4030 .writefn = gt_cntvoff_write,
4031 .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
b0e66d95
EI
4032 { .name = "CNTHP_CVAL_EL2", .state = ARM_CP_STATE_AA64,
4033 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 2,
4034 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
4035 .type = ARM_CP_IO, .access = PL2_RW,
4036 .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
4037 { .name = "CNTHP_CVAL", .cp = 15, .opc1 = 6, .crm = 14,
4038 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
4039 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_IO,
4040 .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
4041 { .name = "CNTHP_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
4042 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 0,
d44ec156 4043 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
b0e66d95
EI
4044 .resetfn = gt_hyp_timer_reset,
4045 .readfn = gt_hyp_tval_read, .writefn = gt_hyp_tval_write },
4046 { .name = "CNTHP_CTL_EL2", .state = ARM_CP_STATE_BOTH,
4047 .type = ARM_CP_IO,
4048 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 1,
4049 .access = PL2_RW,
4050 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].ctl),
4051 .resetvalue = 0,
4052 .writefn = gt_hyp_ctl_write, .raw_writefn = raw_write },
edac4d8a 4053#endif
14cc7b54
SF
4054 /* The only field of MDCR_EL2 that has a defined architectural reset value
4055 * is MDCR_EL2.HPMN which should reset to the value of PMCR_EL0.N; but we
4056 * don't impelment any PMU event counters, so using zero as a reset
4057 * value for MDCR_EL2 is okay
4058 */
4059 { .name = "MDCR_EL2", .state = ARM_CP_STATE_BOTH,
4060 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 1,
4061 .access = PL2_RW, .resetvalue = 0,
4062 .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el2), },
59e05530
EI
4063 { .name = "HPFAR", .state = ARM_CP_STATE_AA32,
4064 .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
4065 .access = PL2_RW, .accessfn = access_el3_aa32ns,
4066 .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
4067 { .name = "HPFAR_EL2", .state = ARM_CP_STATE_AA64,
4068 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
4069 .access = PL2_RW,
4070 .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
2a5a9abd
AF
4071 { .name = "HSTR_EL2", .state = ARM_CP_STATE_BOTH,
4072 .cp = 15, .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 3,
4073 .access = PL2_RW,
4074 .fieldoffset = offsetof(CPUARMState, cp15.hstr_el2) },
3b685ba7
EI
4075 REGINFO_SENTINEL
4076};
4077
2f027fc5
PM
4078static CPAccessResult nsacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
4079 bool isread)
4080{
4081 /* The NSACR is RW at EL3, and RO for NS EL1 and NS EL2.
4082 * At Secure EL1 it traps to EL3.
4083 */
4084 if (arm_current_el(env) == 3) {
4085 return CP_ACCESS_OK;
4086 }
4087 if (arm_is_secure_below_el3(env)) {
4088 return CP_ACCESS_TRAP_EL3;
4089 }
4090 /* Accesses from EL1 NS and EL2 NS are UNDEF for write but allow reads. */
4091 if (isread) {
4092 return CP_ACCESS_OK;
4093 }
4094 return CP_ACCESS_TRAP_UNCATEGORIZED;
4095}
4096
60fb1a87
GB
4097static const ARMCPRegInfo el3_cp_reginfo[] = {
4098 { .name = "SCR_EL3", .state = ARM_CP_STATE_AA64,
4099 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 0,
4100 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.scr_el3),
4101 .resetvalue = 0, .writefn = scr_write },
7a0e58fa 4102 { .name = "SCR", .type = ARM_CP_ALIAS,
60fb1a87 4103 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 0,
efe4a274
PM
4104 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
4105 .fieldoffset = offsetoflow32(CPUARMState, cp15.scr_el3),
b061a82b 4106 .writefn = scr_write },
60fb1a87
GB
4107 { .name = "SDER32_EL3", .state = ARM_CP_STATE_AA64,
4108 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 1,
4109 .access = PL3_RW, .resetvalue = 0,
4110 .fieldoffset = offsetof(CPUARMState, cp15.sder) },
4111 { .name = "SDER",
4112 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 1,
4113 .access = PL3_RW, .resetvalue = 0,
4114 .fieldoffset = offsetoflow32(CPUARMState, cp15.sder) },
60fb1a87 4115 { .name = "MVBAR", .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
efe4a274
PM
4116 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
4117 .writefn = vbar_write, .resetvalue = 0,
60fb1a87 4118 .fieldoffset = offsetof(CPUARMState, cp15.mvbar) },
7dd8c9af
FA
4119 { .name = "TTBR0_EL3", .state = ARM_CP_STATE_AA64,
4120 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 0,
4121 .access = PL3_RW, .writefn = vmsa_ttbr_write, .resetvalue = 0,
4122 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[3]) },
11f136ee
FA
4123 { .name = "TCR_EL3", .state = ARM_CP_STATE_AA64,
4124 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 2,
6459b94c
PM
4125 .access = PL3_RW,
4126 /* no .writefn needed as this can't cause an ASID change;
811595a2
PM
4127 * we must provide a .raw_writefn and .resetfn because we handle
4128 * reset and migration for the AArch32 TTBCR(S), which might be
4129 * using mask and base_mask.
6459b94c 4130 */
811595a2 4131 .resetfn = vmsa_ttbcr_reset, .raw_writefn = vmsa_ttbcr_raw_write,
11f136ee 4132 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[3]) },
81547d66 4133 { .name = "ELR_EL3", .state = ARM_CP_STATE_AA64,
7a0e58fa 4134 .type = ARM_CP_ALIAS,
81547d66
EI
4135 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 1,
4136 .access = PL3_RW,
4137 .fieldoffset = offsetof(CPUARMState, elr_el[3]) },
f2c30f42 4138 { .name = "ESR_EL3", .state = ARM_CP_STATE_AA64,
f2c30f42
EI
4139 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 2, .opc2 = 0,
4140 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[3]) },
63b60551
EI
4141 { .name = "FAR_EL3", .state = ARM_CP_STATE_AA64,
4142 .opc0 = 3, .opc1 = 6, .crn = 6, .crm = 0, .opc2 = 0,
4143 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[3]) },
81547d66 4144 { .name = "SPSR_EL3", .state = ARM_CP_STATE_AA64,
7a0e58fa 4145 .type = ARM_CP_ALIAS,
81547d66 4146 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 0,
99a99c1f
SB
4147 .access = PL3_RW,
4148 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_MON]) },
a1ba125c
EI
4149 { .name = "VBAR_EL3", .state = ARM_CP_STATE_AA64,
4150 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 0,
4151 .access = PL3_RW, .writefn = vbar_write,
4152 .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[3]),
4153 .resetvalue = 0 },
c6f19164
GB
4154 { .name = "CPTR_EL3", .state = ARM_CP_STATE_AA64,
4155 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 2,
4156 .access = PL3_RW, .accessfn = cptr_access, .resetvalue = 0,
4157 .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[3]) },
4cfb8ad8
PM
4158 { .name = "TPIDR_EL3", .state = ARM_CP_STATE_AA64,
4159 .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 2,
4160 .access = PL3_RW, .resetvalue = 0,
4161 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[3]) },
2179ef95
PM
4162 { .name = "AMAIR_EL3", .state = ARM_CP_STATE_AA64,
4163 .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 3, .opc2 = 0,
4164 .access = PL3_RW, .type = ARM_CP_CONST,
4165 .resetvalue = 0 },
37cd6c24
PM
4166 { .name = "AFSR0_EL3", .state = ARM_CP_STATE_BOTH,
4167 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 0,
4168 .access = PL3_RW, .type = ARM_CP_CONST,
4169 .resetvalue = 0 },
4170 { .name = "AFSR1_EL3", .state = ARM_CP_STATE_BOTH,
4171 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 1,
4172 .access = PL3_RW, .type = ARM_CP_CONST,
4173 .resetvalue = 0 },
43efaa33
PM
4174 { .name = "TLBI_ALLE3IS", .state = ARM_CP_STATE_AA64,
4175 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 0,
4176 .access = PL3_W, .type = ARM_CP_NO_RAW,
4177 .writefn = tlbi_aa64_alle3is_write },
4178 { .name = "TLBI_VAE3IS", .state = ARM_CP_STATE_AA64,
4179 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 1,
4180 .access = PL3_W, .type = ARM_CP_NO_RAW,
4181 .writefn = tlbi_aa64_vae3is_write },
4182 { .name = "TLBI_VALE3IS", .state = ARM_CP_STATE_AA64,
4183 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 5,
4184 .access = PL3_W, .type = ARM_CP_NO_RAW,
4185 .writefn = tlbi_aa64_vae3is_write },
4186 { .name = "TLBI_ALLE3", .state = ARM_CP_STATE_AA64,
4187 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 0,
4188 .access = PL3_W, .type = ARM_CP_NO_RAW,
4189 .writefn = tlbi_aa64_alle3_write },
4190 { .name = "TLBI_VAE3", .state = ARM_CP_STATE_AA64,
4191 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 1,
4192 .access = PL3_W, .type = ARM_CP_NO_RAW,
4193 .writefn = tlbi_aa64_vae3_write },
4194 { .name = "TLBI_VALE3", .state = ARM_CP_STATE_AA64,
4195 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 5,
4196 .access = PL3_W, .type = ARM_CP_NO_RAW,
4197 .writefn = tlbi_aa64_vae3_write },
0f1a3b24
FA
4198 REGINFO_SENTINEL
4199};
4200
3f208fd7
PM
4201static CPAccessResult ctr_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
4202 bool isread)
7da845b0
PM
4203{
4204 /* Only accessible in EL0 if SCTLR.UCT is set (and only in AArch64,
4205 * but the AArch32 CTR has its own reginfo struct)
4206 */
137feaa9 4207 if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_UCT)) {
7da845b0
PM
4208 return CP_ACCESS_TRAP;
4209 }
4210 return CP_ACCESS_OK;
4211}
4212
1424ca8d
DM
4213static void oslar_write(CPUARMState *env, const ARMCPRegInfo *ri,
4214 uint64_t value)
4215{
4216 /* Writes to OSLAR_EL1 may update the OS lock status, which can be
4217 * read via a bit in OSLSR_EL1.
4218 */
4219 int oslock;
4220
4221 if (ri->state == ARM_CP_STATE_AA32) {
4222 oslock = (value == 0xC5ACCE55);
4223 } else {
4224 oslock = value & 1;
4225 }
4226
4227 env->cp15.oslsr_el1 = deposit32(env->cp15.oslsr_el1, 1, 1, oslock);
4228}
4229
50300698 4230static const ARMCPRegInfo debug_cp_reginfo[] = {
50300698 4231 /* DBGDRAR, DBGDSAR: always RAZ since we don't implement memory mapped
10aae104
PM
4232 * debug components. The AArch64 version of DBGDRAR is named MDRAR_EL1;
4233 * unlike DBGDRAR it is never accessible from EL0.
4234 * DBGDSAR is deprecated and must RAZ from v8 anyway, so it has no AArch64
4235 * accessor.
50300698
PM
4236 */
4237 { .name = "DBGDRAR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 0,
91b0a238
PM
4238 .access = PL0_R, .accessfn = access_tdra,
4239 .type = ARM_CP_CONST, .resetvalue = 0 },
10aae104
PM
4240 { .name = "MDRAR_EL1", .state = ARM_CP_STATE_AA64,
4241 .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
91b0a238
PM
4242 .access = PL1_R, .accessfn = access_tdra,
4243 .type = ARM_CP_CONST, .resetvalue = 0 },
50300698 4244 { .name = "DBGDSAR", .cp = 14, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
91b0a238
PM
4245 .access = PL0_R, .accessfn = access_tdra,
4246 .type = ARM_CP_CONST, .resetvalue = 0 },
17a9eb53 4247 /* Monitor debug system control register; the 32-bit alias is DBGDSCRext. */
10aae104
PM
4248 { .name = "MDSCR_EL1", .state = ARM_CP_STATE_BOTH,
4249 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
d6c8cf81 4250 .access = PL1_RW, .accessfn = access_tda,
0e5e8935
PM
4251 .fieldoffset = offsetof(CPUARMState, cp15.mdscr_el1),
4252 .resetvalue = 0 },
5e8b12ff
PM
4253 /* MDCCSR_EL0, aka DBGDSCRint. This is a read-only mirror of MDSCR_EL1.
4254 * We don't implement the configurable EL0 access.
4255 */
4256 { .name = "MDCCSR_EL0", .state = ARM_CP_STATE_BOTH,
4257 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
7a0e58fa 4258 .type = ARM_CP_ALIAS,
d6c8cf81 4259 .access = PL1_R, .accessfn = access_tda,
b061a82b 4260 .fieldoffset = offsetof(CPUARMState, cp15.mdscr_el1), },
10aae104
PM
4261 { .name = "OSLAR_EL1", .state = ARM_CP_STATE_BOTH,
4262 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 4,
1424ca8d 4263 .access = PL1_W, .type = ARM_CP_NO_RAW,
187f678d 4264 .accessfn = access_tdosa,
1424ca8d
DM
4265 .writefn = oslar_write },
4266 { .name = "OSLSR_EL1", .state = ARM_CP_STATE_BOTH,
4267 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 4,
4268 .access = PL1_R, .resetvalue = 10,
187f678d 4269 .accessfn = access_tdosa,
1424ca8d 4270 .fieldoffset = offsetof(CPUARMState, cp15.oslsr_el1) },
5e8b12ff
PM
4271 /* Dummy OSDLR_EL1: 32-bit Linux will read this */
4272 { .name = "OSDLR_EL1", .state = ARM_CP_STATE_BOTH,
4273 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 4,
187f678d
PM
4274 .access = PL1_RW, .accessfn = access_tdosa,
4275 .type = ARM_CP_NOP },
5e8b12ff
PM
4276 /* Dummy DBGVCR: Linux wants to clear this on startup, but we don't
4277 * implement vector catch debug events yet.
4278 */
4279 { .name = "DBGVCR",
4280 .cp = 14, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
d6c8cf81
PM
4281 .access = PL1_RW, .accessfn = access_tda,
4282 .type = ARM_CP_NOP },
4d2ec4da
PM
4283 /* Dummy DBGVCR32_EL2 (which is only for a 64-bit hypervisor
4284 * to save and restore a 32-bit guest's DBGVCR)
4285 */
4286 { .name = "DBGVCR32_EL2", .state = ARM_CP_STATE_AA64,
4287 .opc0 = 2, .opc1 = 4, .crn = 0, .crm = 7, .opc2 = 0,
4288 .access = PL2_RW, .accessfn = access_tda,
4289 .type = ARM_CP_NOP },
5dbdc434
PM
4290 /* Dummy MDCCINT_EL1, since we don't implement the Debug Communications
4291 * Channel but Linux may try to access this register. The 32-bit
4292 * alias is DBGDCCINT.
4293 */
4294 { .name = "MDCCINT_EL1", .state = ARM_CP_STATE_BOTH,
4295 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0,
4296 .access = PL1_RW, .accessfn = access_tda,
4297 .type = ARM_CP_NOP },
50300698
PM
4298 REGINFO_SENTINEL
4299};
4300
4301static const ARMCPRegInfo debug_lpae_cp_reginfo[] = {
4302 /* 64 bit access versions of the (dummy) debug registers */
4303 { .name = "DBGDRAR", .cp = 14, .crm = 1, .opc1 = 0,
4304 .access = PL0_R, .type = ARM_CP_CONST|ARM_CP_64BIT, .resetvalue = 0 },
4305 { .name = "DBGDSAR", .cp = 14, .crm = 2, .opc1 = 0,
4306 .access = PL0_R, .type = ARM_CP_CONST|ARM_CP_64BIT, .resetvalue = 0 },
4307 REGINFO_SENTINEL
4308};
4309
5be5e8ed
RH
4310/* Return the exception level to which SVE-disabled exceptions should
4311 * be taken, or 0 if SVE is enabled.
4312 */
4313static int sve_exception_el(CPUARMState *env)
4314{
4315#ifndef CONFIG_USER_ONLY
4316 unsigned current_el = arm_current_el(env);
4317
4318 /* The CPACR.ZEN controls traps to EL1:
4319 * 0, 2 : trap EL0 and EL1 accesses
4320 * 1 : trap only EL0 accesses
4321 * 3 : trap no accesses
4322 */
4323 switch (extract32(env->cp15.cpacr_el1, 16, 2)) {
4324 default:
4325 if (current_el <= 1) {
4326 /* Trap to PL1, which might be EL1 or EL3 */
4327 if (arm_is_secure(env) && !arm_el_is_aa64(env, 3)) {
4328 return 3;
4329 }
4330 return 1;
4331 }
4332 break;
4333 case 1:
4334 if (current_el == 0) {
4335 return 1;
4336 }
4337 break;
4338 case 3:
4339 break;
4340 }
4341
4342 /* Similarly for CPACR.FPEN, after having checked ZEN. */
4343 switch (extract32(env->cp15.cpacr_el1, 20, 2)) {
4344 default:
4345 if (current_el <= 1) {
4346 if (arm_is_secure(env) && !arm_el_is_aa64(env, 3)) {
4347 return 3;
4348 }
4349 return 1;
4350 }
4351 break;
4352 case 1:
4353 if (current_el == 0) {
4354 return 1;
4355 }
4356 break;
4357 case 3:
4358 break;
4359 }
4360
4361 /* CPTR_EL2. Check both TZ and TFP. */
4362 if (current_el <= 2
4363 && (env->cp15.cptr_el[2] & (CPTR_TFP | CPTR_TZ))
4364 && !arm_is_secure_below_el3(env)) {
4365 return 2;
4366 }
4367
4368 /* CPTR_EL3. Check both EZ and TFP. */
4369 if (!(env->cp15.cptr_el[3] & CPTR_EZ)
4370 || (env->cp15.cptr_el[3] & CPTR_TFP)) {
4371 return 3;
4372 }
4373#endif
4374 return 0;
4375}
4376
5be5e8ed
RH
4377static void zcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4378 uint64_t value)
4379{
4380 /* Bits other than [3:0] are RAZ/WI. */
4381 raw_write(env, ri, value & 0xf);
4382}
4383
4384static const ARMCPRegInfo zcr_el1_reginfo = {
4385 .name = "ZCR_EL1", .state = ARM_CP_STATE_AA64,
4386 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 0,
490aa7f1 4387 .access = PL1_RW, .type = ARM_CP_SVE | ARM_CP_FPU,
5be5e8ed
RH
4388 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[1]),
4389 .writefn = zcr_write, .raw_writefn = raw_write
4390};
4391
4392static const ARMCPRegInfo zcr_el2_reginfo = {
4393 .name = "ZCR_EL2", .state = ARM_CP_STATE_AA64,
4394 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 0,
490aa7f1 4395 .access = PL2_RW, .type = ARM_CP_SVE | ARM_CP_FPU,
5be5e8ed
RH
4396 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[2]),
4397 .writefn = zcr_write, .raw_writefn = raw_write
4398};
4399
4400static const ARMCPRegInfo zcr_no_el2_reginfo = {
4401 .name = "ZCR_EL2", .state = ARM_CP_STATE_AA64,
4402 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 0,
490aa7f1 4403 .access = PL2_RW, .type = ARM_CP_SVE | ARM_CP_FPU,
5be5e8ed
RH
4404 .readfn = arm_cp_read_zero, .writefn = arm_cp_write_ignore
4405};
4406
4407static const ARMCPRegInfo zcr_el3_reginfo = {
4408 .name = "ZCR_EL3", .state = ARM_CP_STATE_AA64,
4409 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 2, .opc2 = 0,
490aa7f1 4410 .access = PL3_RW, .type = ARM_CP_SVE | ARM_CP_FPU,
5be5e8ed
RH
4411 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[3]),
4412 .writefn = zcr_write, .raw_writefn = raw_write
4413};
4414
9ee98ce8
PM
4415void hw_watchpoint_update(ARMCPU *cpu, int n)
4416{
4417 CPUARMState *env = &cpu->env;
4418 vaddr len = 0;
4419 vaddr wvr = env->cp15.dbgwvr[n];
4420 uint64_t wcr = env->cp15.dbgwcr[n];
4421 int mask;
4422 int flags = BP_CPU | BP_STOP_BEFORE_ACCESS;
4423
4424 if (env->cpu_watchpoint[n]) {
4425 cpu_watchpoint_remove_by_ref(CPU(cpu), env->cpu_watchpoint[n]);
4426 env->cpu_watchpoint[n] = NULL;
4427 }
4428
4429 if (!extract64(wcr, 0, 1)) {
4430 /* E bit clear : watchpoint disabled */
4431 return;
4432 }
4433
4434 switch (extract64(wcr, 3, 2)) {
4435 case 0:
4436 /* LSC 00 is reserved and must behave as if the wp is disabled */
4437 return;
4438 case 1:
4439 flags |= BP_MEM_READ;
4440 break;
4441 case 2:
4442 flags |= BP_MEM_WRITE;
4443 break;
4444 case 3:
4445 flags |= BP_MEM_ACCESS;
4446 break;
4447 }
4448
4449 /* Attempts to use both MASK and BAS fields simultaneously are
4450 * CONSTRAINED UNPREDICTABLE; we opt to ignore BAS in this case,
4451 * thus generating a watchpoint for every byte in the masked region.
4452 */
4453 mask = extract64(wcr, 24, 4);
4454 if (mask == 1 || mask == 2) {
4455 /* Reserved values of MASK; we must act as if the mask value was
4456 * some non-reserved value, or as if the watchpoint were disabled.
4457 * We choose the latter.
4458 */
4459 return;
4460 } else if (mask) {
4461 /* Watchpoint covers an aligned area up to 2GB in size */
4462 len = 1ULL << mask;
4463 /* If masked bits in WVR are not zero it's CONSTRAINED UNPREDICTABLE
4464 * whether the watchpoint fires when the unmasked bits match; we opt
4465 * to generate the exceptions.
4466 */
4467 wvr &= ~(len - 1);
4468 } else {
4469 /* Watchpoint covers bytes defined by the byte address select bits */
4470 int bas = extract64(wcr, 5, 8);
4471 int basstart;
4472
4473 if (bas == 0) {
4474 /* This must act as if the watchpoint is disabled */
4475 return;
4476 }
4477
4478 if (extract64(wvr, 2, 1)) {
4479 /* Deprecated case of an only 4-aligned address. BAS[7:4] are
4480 * ignored, and BAS[3:0] define which bytes to watch.
4481 */
4482 bas &= 0xf;
4483 }
4484 /* The BAS bits are supposed to be programmed to indicate a contiguous
4485 * range of bytes. Otherwise it is CONSTRAINED UNPREDICTABLE whether
4486 * we fire for each byte in the word/doubleword addressed by the WVR.
4487 * We choose to ignore any non-zero bits after the first range of 1s.
4488 */
4489 basstart = ctz32(bas);
4490 len = cto32(bas >> basstart);
4491 wvr += basstart;
4492 }
4493
4494 cpu_watchpoint_insert(CPU(cpu), wvr, len, flags,
4495 &env->cpu_watchpoint[n]);
4496}
4497
4498void hw_watchpoint_update_all(ARMCPU *cpu)
4499{
4500 int i;
4501 CPUARMState *env = &cpu->env;
4502
4503 /* Completely clear out existing QEMU watchpoints and our array, to
4504 * avoid possible stale entries following migration load.
4505 */
4506 cpu_watchpoint_remove_all(CPU(cpu), BP_CPU);
4507 memset(env->cpu_watchpoint, 0, sizeof(env->cpu_watchpoint));
4508
4509 for (i = 0; i < ARRAY_SIZE(cpu->env.cpu_watchpoint); i++) {
4510 hw_watchpoint_update(cpu, i);
4511 }
4512}
4513
4514static void dbgwvr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4515 uint64_t value)
4516{
4517 ARMCPU *cpu = arm_env_get_cpu(env);
4518 int i = ri->crm;
4519
4520 /* Bits [63:49] are hardwired to the value of bit [48]; that is, the
4521 * register reads and behaves as if values written are sign extended.
4522 * Bits [1:0] are RES0.
4523 */
4524 value = sextract64(value, 0, 49) & ~3ULL;
4525
4526 raw_write(env, ri, value);
4527 hw_watchpoint_update(cpu, i);
4528}
4529
4530static void dbgwcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4531 uint64_t value)
4532{
4533 ARMCPU *cpu = arm_env_get_cpu(env);
4534 int i = ri->crm;
4535
4536 raw_write(env, ri, value);
4537 hw_watchpoint_update(cpu, i);
4538}
4539
46747d15
PM
4540void hw_breakpoint_update(ARMCPU *cpu, int n)
4541{
4542 CPUARMState *env = &cpu->env;
4543 uint64_t bvr = env->cp15.dbgbvr[n];
4544 uint64_t bcr = env->cp15.dbgbcr[n];
4545 vaddr addr;
4546 int bt;
4547 int flags = BP_CPU;
4548
4549 if (env->cpu_breakpoint[n]) {
4550 cpu_breakpoint_remove_by_ref(CPU(cpu), env->cpu_breakpoint[n]);
4551 env->cpu_breakpoint[n] = NULL;
4552 }
4553
4554 if (!extract64(bcr, 0, 1)) {
4555 /* E bit clear : watchpoint disabled */
4556 return;
4557 }
4558
4559 bt = extract64(bcr, 20, 4);
4560
4561 switch (bt) {
4562 case 4: /* unlinked address mismatch (reserved if AArch64) */
4563 case 5: /* linked address mismatch (reserved if AArch64) */
4564 qemu_log_mask(LOG_UNIMP,
4565 "arm: address mismatch breakpoint types not implemented");
4566 return;
4567 case 0: /* unlinked address match */
4568 case 1: /* linked address match */
4569 {
4570 /* Bits [63:49] are hardwired to the value of bit [48]; that is,
4571 * we behave as if the register was sign extended. Bits [1:0] are
4572 * RES0. The BAS field is used to allow setting breakpoints on 16
4573 * bit wide instructions; it is CONSTRAINED UNPREDICTABLE whether
4574 * a bp will fire if the addresses covered by the bp and the addresses
4575 * covered by the insn overlap but the insn doesn't start at the
4576 * start of the bp address range. We choose to require the insn and
4577 * the bp to have the same address. The constraints on writing to
4578 * BAS enforced in dbgbcr_write mean we have only four cases:
4579 * 0b0000 => no breakpoint
4580 * 0b0011 => breakpoint on addr
4581 * 0b1100 => breakpoint on addr + 2
4582 * 0b1111 => breakpoint on addr
4583 * See also figure D2-3 in the v8 ARM ARM (DDI0487A.c).
4584 */
4585 int bas = extract64(bcr, 5, 4);
4586 addr = sextract64(bvr, 0, 49) & ~3ULL;
4587 if (bas == 0) {
4588 return;
4589 }
4590 if (bas == 0xc) {
4591 addr += 2;
4592 }
4593 break;
4594 }
4595 case 2: /* unlinked context ID match */
4596 case 8: /* unlinked VMID match (reserved if no EL2) */
4597 case 10: /* unlinked context ID and VMID match (reserved if no EL2) */
4598 qemu_log_mask(LOG_UNIMP,
4599 "arm: unlinked context breakpoint types not implemented");
4600 return;
4601 case 9: /* linked VMID match (reserved if no EL2) */
4602 case 11: /* linked context ID and VMID match (reserved if no EL2) */
4603 case 3: /* linked context ID match */
4604 default:
4605 /* We must generate no events for Linked context matches (unless
4606 * they are linked to by some other bp/wp, which is handled in
4607 * updates for the linking bp/wp). We choose to also generate no events
4608 * for reserved values.
4609 */
4610 return;
4611 }
4612
4613 cpu_breakpoint_insert(CPU(cpu), addr, flags, &env->cpu_breakpoint[n]);
4614}
4615
4616void hw_breakpoint_update_all(ARMCPU *cpu)
4617{
4618 int i;
4619 CPUARMState *env = &cpu->env;
4620
4621 /* Completely clear out existing QEMU breakpoints and our array, to
4622 * avoid possible stale entries following migration load.
4623 */
4624 cpu_breakpoint_remove_all(CPU(cpu), BP_CPU);
4625 memset(env->cpu_breakpoint, 0, sizeof(env->cpu_breakpoint));
4626
4627 for (i = 0; i < ARRAY_SIZE(cpu->env.cpu_breakpoint); i++) {
4628 hw_breakpoint_update(cpu, i);
4629 }
4630}
4631
4632static void dbgbvr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4633 uint64_t value)
4634{
4635 ARMCPU *cpu = arm_env_get_cpu(env);
4636 int i = ri->crm;
4637
4638 raw_write(env, ri, value);
4639 hw_breakpoint_update(cpu, i);
4640}
4641
4642static void dbgbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4643 uint64_t value)
4644{
4645 ARMCPU *cpu = arm_env_get_cpu(env);
4646 int i = ri->crm;
4647
4648 /* BAS[3] is a read-only copy of BAS[2], and BAS[1] a read-only
4649 * copy of BAS[0].
4650 */
4651 value = deposit64(value, 6, 1, extract64(value, 5, 1));
4652 value = deposit64(value, 8, 1, extract64(value, 7, 1));
4653
4654 raw_write(env, ri, value);
4655 hw_breakpoint_update(cpu, i);
4656}
4657
50300698 4658static void define_debug_regs(ARMCPU *cpu)
0b45451e 4659{
50300698
PM
4660 /* Define v7 and v8 architectural debug registers.
4661 * These are just dummy implementations for now.
0b45451e
PM
4662 */
4663 int i;
3ff6fc91 4664 int wrps, brps, ctx_cmps;
48eb3ae6
PM
4665 ARMCPRegInfo dbgdidr = {
4666 .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0,
d6c8cf81
PM
4667 .access = PL0_R, .accessfn = access_tda,
4668 .type = ARM_CP_CONST, .resetvalue = cpu->dbgdidr,
48eb3ae6
PM
4669 };
4670
3ff6fc91 4671 /* Note that all these register fields hold "number of Xs minus 1". */
48eb3ae6
PM
4672 brps = extract32(cpu->dbgdidr, 24, 4);
4673 wrps = extract32(cpu->dbgdidr, 28, 4);
3ff6fc91
PM
4674 ctx_cmps = extract32(cpu->dbgdidr, 20, 4);
4675
4676 assert(ctx_cmps <= brps);
48eb3ae6
PM
4677
4678 /* The DBGDIDR and ID_AA64DFR0_EL1 define various properties
4679 * of the debug registers such as number of breakpoints;
4680 * check that if they both exist then they agree.
4681 */
4682 if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64)) {
4683 assert(extract32(cpu->id_aa64dfr0, 12, 4) == brps);
4684 assert(extract32(cpu->id_aa64dfr0, 20, 4) == wrps);
3ff6fc91 4685 assert(extract32(cpu->id_aa64dfr0, 28, 4) == ctx_cmps);
48eb3ae6 4686 }
0b45451e 4687
48eb3ae6 4688 define_one_arm_cp_reg(cpu, &dbgdidr);
50300698
PM
4689 define_arm_cp_regs(cpu, debug_cp_reginfo);
4690
4691 if (arm_feature(&cpu->env, ARM_FEATURE_LPAE)) {
4692 define_arm_cp_regs(cpu, debug_lpae_cp_reginfo);
4693 }
4694
48eb3ae6 4695 for (i = 0; i < brps + 1; i++) {
0b45451e 4696 ARMCPRegInfo dbgregs[] = {
10aae104
PM
4697 { .name = "DBGBVR", .state = ARM_CP_STATE_BOTH,
4698 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 4,
d6c8cf81 4699 .access = PL1_RW, .accessfn = access_tda,
46747d15
PM
4700 .fieldoffset = offsetof(CPUARMState, cp15.dbgbvr[i]),
4701 .writefn = dbgbvr_write, .raw_writefn = raw_write
4702 },
10aae104
PM
4703 { .name = "DBGBCR", .state = ARM_CP_STATE_BOTH,
4704 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 5,
d6c8cf81 4705 .access = PL1_RW, .accessfn = access_tda,
46747d15
PM
4706 .fieldoffset = offsetof(CPUARMState, cp15.dbgbcr[i]),
4707 .writefn = dbgbcr_write, .raw_writefn = raw_write
4708 },
48eb3ae6
PM
4709 REGINFO_SENTINEL
4710 };
4711 define_arm_cp_regs(cpu, dbgregs);
4712 }
4713
4714 for (i = 0; i < wrps + 1; i++) {
4715 ARMCPRegInfo dbgregs[] = {
10aae104
PM
4716 { .name = "DBGWVR", .state = ARM_CP_STATE_BOTH,
4717 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 6,
d6c8cf81 4718 .access = PL1_RW, .accessfn = access_tda,
9ee98ce8
PM
4719 .fieldoffset = offsetof(CPUARMState, cp15.dbgwvr[i]),
4720 .writefn = dbgwvr_write, .raw_writefn = raw_write
4721 },
10aae104
PM
4722 { .name = "DBGWCR", .state = ARM_CP_STATE_BOTH,
4723 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 7,
d6c8cf81 4724 .access = PL1_RW, .accessfn = access_tda,
9ee98ce8
PM
4725 .fieldoffset = offsetof(CPUARMState, cp15.dbgwcr[i]),
4726 .writefn = dbgwcr_write, .raw_writefn = raw_write
4727 },
4728 REGINFO_SENTINEL
0b45451e
PM
4729 };
4730 define_arm_cp_regs(cpu, dbgregs);
4731 }
4732}
4733
96a8b92e
PM
4734/* We don't know until after realize whether there's a GICv3
4735 * attached, and that is what registers the gicv3 sysregs.
4736 * So we have to fill in the GIC fields in ID_PFR/ID_PFR1_EL1/ID_AA64PFR0_EL1
4737 * at runtime.
4738 */
4739static uint64_t id_pfr1_read(CPUARMState *env, const ARMCPRegInfo *ri)
4740{
4741 ARMCPU *cpu = arm_env_get_cpu(env);
4742 uint64_t pfr1 = cpu->id_pfr1;
4743
4744 if (env->gicv3state) {
4745 pfr1 |= 1 << 28;
4746 }
4747 return pfr1;
4748}
4749
4750static uint64_t id_aa64pfr0_read(CPUARMState *env, const ARMCPRegInfo *ri)
4751{
4752 ARMCPU *cpu = arm_env_get_cpu(env);
4753 uint64_t pfr0 = cpu->id_aa64pfr0;
4754
4755 if (env->gicv3state) {
4756 pfr0 |= 1 << 24;
4757 }
4758 return pfr0;
4759}
4760
2ceb98c0
PM
4761void register_cp_regs_for_features(ARMCPU *cpu)
4762{
4763 /* Register all the coprocessor registers based on feature bits */
4764 CPUARMState *env = &cpu->env;
4765 if (arm_feature(env, ARM_FEATURE_M)) {
4766 /* M profile has no coprocessor registers */
4767 return;
4768 }
4769
e9aa6c21 4770 define_arm_cp_regs(cpu, cp_reginfo);
9449fdf6
PM
4771 if (!arm_feature(env, ARM_FEATURE_V8)) {
4772 /* Must go early as it is full of wildcards that may be
4773 * overridden by later definitions.
4774 */
4775 define_arm_cp_regs(cpu, not_v8_cp_reginfo);
4776 }
4777
7d57f408 4778 if (arm_feature(env, ARM_FEATURE_V6)) {
8515a092
PM
4779 /* The ID registers all have impdef reset values */
4780 ARMCPRegInfo v6_idregs[] = {
0ff644a7
PM
4781 { .name = "ID_PFR0", .state = ARM_CP_STATE_BOTH,
4782 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
4783 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 4784 .resetvalue = cpu->id_pfr0 },
96a8b92e
PM
4785 /* ID_PFR1 is not a plain ARM_CP_CONST because we don't know
4786 * the value of the GIC field until after we define these regs.
4787 */
0ff644a7
PM
4788 { .name = "ID_PFR1", .state = ARM_CP_STATE_BOTH,
4789 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 1,
96a8b92e
PM
4790 .access = PL1_R, .type = ARM_CP_NO_RAW,
4791 .readfn = id_pfr1_read,
4792 .writefn = arm_cp_write_ignore },
0ff644a7
PM
4793 { .name = "ID_DFR0", .state = ARM_CP_STATE_BOTH,
4794 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 2,
4795 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 4796 .resetvalue = cpu->id_dfr0 },
0ff644a7
PM
4797 { .name = "ID_AFR0", .state = ARM_CP_STATE_BOTH,
4798 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 3,
4799 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 4800 .resetvalue = cpu->id_afr0 },
0ff644a7
PM
4801 { .name = "ID_MMFR0", .state = ARM_CP_STATE_BOTH,
4802 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 4,
4803 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 4804 .resetvalue = cpu->id_mmfr0 },
0ff644a7
PM
4805 { .name = "ID_MMFR1", .state = ARM_CP_STATE_BOTH,
4806 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 5,
4807 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 4808 .resetvalue = cpu->id_mmfr1 },
0ff644a7
PM
4809 { .name = "ID_MMFR2", .state = ARM_CP_STATE_BOTH,
4810 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 6,
4811 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 4812 .resetvalue = cpu->id_mmfr2 },
0ff644a7
PM
4813 { .name = "ID_MMFR3", .state = ARM_CP_STATE_BOTH,
4814 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 7,
4815 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 4816 .resetvalue = cpu->id_mmfr3 },
0ff644a7
PM
4817 { .name = "ID_ISAR0", .state = ARM_CP_STATE_BOTH,
4818 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0,
4819 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 4820 .resetvalue = cpu->id_isar0 },
0ff644a7
PM
4821 { .name = "ID_ISAR1", .state = ARM_CP_STATE_BOTH,
4822 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 1,
4823 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 4824 .resetvalue = cpu->id_isar1 },
0ff644a7
PM
4825 { .name = "ID_ISAR2", .state = ARM_CP_STATE_BOTH,
4826 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
4827 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 4828 .resetvalue = cpu->id_isar2 },
0ff644a7
PM
4829 { .name = "ID_ISAR3", .state = ARM_CP_STATE_BOTH,
4830 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 3,
4831 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 4832 .resetvalue = cpu->id_isar3 },
0ff644a7
PM
4833 { .name = "ID_ISAR4", .state = ARM_CP_STATE_BOTH,
4834 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 4,
4835 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 4836 .resetvalue = cpu->id_isar4 },
0ff644a7
PM
4837 { .name = "ID_ISAR5", .state = ARM_CP_STATE_BOTH,
4838 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 5,
4839 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 4840 .resetvalue = cpu->id_isar5 },
e20d84c1
PM
4841 { .name = "ID_MMFR4", .state = ARM_CP_STATE_BOTH,
4842 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 6,
4843 .access = PL1_R, .type = ARM_CP_CONST,
4844 .resetvalue = cpu->id_mmfr4 },
4845 /* 7 is as yet unallocated and must RAZ */
4846 { .name = "ID_ISAR7_RESERVED", .state = ARM_CP_STATE_BOTH,
4847 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 7,
4848 .access = PL1_R, .type = ARM_CP_CONST,
8515a092
PM
4849 .resetvalue = 0 },
4850 REGINFO_SENTINEL
4851 };
4852 define_arm_cp_regs(cpu, v6_idregs);
7d57f408
PM
4853 define_arm_cp_regs(cpu, v6_cp_reginfo);
4854 } else {
4855 define_arm_cp_regs(cpu, not_v6_cp_reginfo);
4856 }
4d31c596
PM
4857 if (arm_feature(env, ARM_FEATURE_V6K)) {
4858 define_arm_cp_regs(cpu, v6k_cp_reginfo);
4859 }
5e5cf9e3 4860 if (arm_feature(env, ARM_FEATURE_V7MP) &&
452a0955 4861 !arm_feature(env, ARM_FEATURE_PMSA)) {
995939a6
PM
4862 define_arm_cp_regs(cpu, v7mp_cp_reginfo);
4863 }
e9aa6c21 4864 if (arm_feature(env, ARM_FEATURE_V7)) {
200ac0ef 4865 /* v7 performance monitor control register: same implementor
7c2cb42b
AF
4866 * field as main ID register, and we implement only the cycle
4867 * count register.
200ac0ef 4868 */
7c2cb42b 4869#ifndef CONFIG_USER_ONLY
200ac0ef
PM
4870 ARMCPRegInfo pmcr = {
4871 .name = "PMCR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 0,
8521466b 4872 .access = PL0_RW,
7a0e58fa 4873 .type = ARM_CP_IO | ARM_CP_ALIAS,
8521466b 4874 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcr),
fcd25206
PM
4875 .accessfn = pmreg_access, .writefn = pmcr_write,
4876 .raw_writefn = raw_write,
200ac0ef 4877 };
8521466b
AF
4878 ARMCPRegInfo pmcr64 = {
4879 .name = "PMCR_EL0", .state = ARM_CP_STATE_AA64,
4880 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 0,
4881 .access = PL0_RW, .accessfn = pmreg_access,
4882 .type = ARM_CP_IO,
4883 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcr),
4884 .resetvalue = cpu->midr & 0xff000000,
4885 .writefn = pmcr_write, .raw_writefn = raw_write,
4886 };
7c2cb42b 4887 define_one_arm_cp_reg(cpu, &pmcr);
8521466b 4888 define_one_arm_cp_reg(cpu, &pmcr64);
7c2cb42b 4889#endif
776d4e5c 4890 ARMCPRegInfo clidr = {
7da845b0
PM
4891 .name = "CLIDR", .state = ARM_CP_STATE_BOTH,
4892 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 1,
776d4e5c
PM
4893 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->clidr
4894 };
776d4e5c 4895 define_one_arm_cp_reg(cpu, &clidr);
e9aa6c21 4896 define_arm_cp_regs(cpu, v7_cp_reginfo);
50300698 4897 define_debug_regs(cpu);
7d57f408
PM
4898 } else {
4899 define_arm_cp_regs(cpu, not_v7_cp_reginfo);
e9aa6c21 4900 }
b0d2b7d0 4901 if (arm_feature(env, ARM_FEATURE_V8)) {
e20d84c1
PM
4902 /* AArch64 ID registers, which all have impdef reset values.
4903 * Note that within the ID register ranges the unused slots
4904 * must all RAZ, not UNDEF; future architecture versions may
4905 * define new registers here.
4906 */
e60cef86 4907 ARMCPRegInfo v8_idregs[] = {
96a8b92e
PM
4908 /* ID_AA64PFR0_EL1 is not a plain ARM_CP_CONST because we don't
4909 * know the right value for the GIC field until after we
4910 * define these regs.
4911 */
e60cef86
PM
4912 { .name = "ID_AA64PFR0_EL1", .state = ARM_CP_STATE_AA64,
4913 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 0,
96a8b92e
PM
4914 .access = PL1_R, .type = ARM_CP_NO_RAW,
4915 .readfn = id_aa64pfr0_read,
4916 .writefn = arm_cp_write_ignore },
e60cef86
PM
4917 { .name = "ID_AA64PFR1_EL1", .state = ARM_CP_STATE_AA64,
4918 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 1,
4919 .access = PL1_R, .type = ARM_CP_CONST,
4920 .resetvalue = cpu->id_aa64pfr1},
e20d84c1
PM
4921 { .name = "ID_AA64PFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4922 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 2,
4923 .access = PL1_R, .type = ARM_CP_CONST,
4924 .resetvalue = 0 },
4925 { .name = "ID_AA64PFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4926 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 3,
4927 .access = PL1_R, .type = ARM_CP_CONST,
4928 .resetvalue = 0 },
4929 { .name = "ID_AA64PFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4930 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 4,
4931 .access = PL1_R, .type = ARM_CP_CONST,
4932 .resetvalue = 0 },
4933 { .name = "ID_AA64PFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4934 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 5,
4935 .access = PL1_R, .type = ARM_CP_CONST,
4936 .resetvalue = 0 },
4937 { .name = "ID_AA64PFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4938 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 6,
4939 .access = PL1_R, .type = ARM_CP_CONST,
4940 .resetvalue = 0 },
4941 { .name = "ID_AA64PFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4942 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 7,
4943 .access = PL1_R, .type = ARM_CP_CONST,
4944 .resetvalue = 0 },
e60cef86
PM
4945 { .name = "ID_AA64DFR0_EL1", .state = ARM_CP_STATE_AA64,
4946 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 0,
4947 .access = PL1_R, .type = ARM_CP_CONST,
d6f02ce3 4948 .resetvalue = cpu->id_aa64dfr0 },
e60cef86
PM
4949 { .name = "ID_AA64DFR1_EL1", .state = ARM_CP_STATE_AA64,
4950 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 1,
4951 .access = PL1_R, .type = ARM_CP_CONST,
4952 .resetvalue = cpu->id_aa64dfr1 },
e20d84c1
PM
4953 { .name = "ID_AA64DFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4954 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 2,
4955 .access = PL1_R, .type = ARM_CP_CONST,
4956 .resetvalue = 0 },
4957 { .name = "ID_AA64DFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4958 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 3,
4959 .access = PL1_R, .type = ARM_CP_CONST,
4960 .resetvalue = 0 },
e60cef86
PM
4961 { .name = "ID_AA64AFR0_EL1", .state = ARM_CP_STATE_AA64,
4962 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 4,
4963 .access = PL1_R, .type = ARM_CP_CONST,
4964 .resetvalue = cpu->id_aa64afr0 },
4965 { .name = "ID_AA64AFR1_EL1", .state = ARM_CP_STATE_AA64,
4966 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 5,
4967 .access = PL1_R, .type = ARM_CP_CONST,
4968 .resetvalue = cpu->id_aa64afr1 },
e20d84c1
PM
4969 { .name = "ID_AA64AFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4970 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 6,
4971 .access = PL1_R, .type = ARM_CP_CONST,
4972 .resetvalue = 0 },
4973 { .name = "ID_AA64AFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4974 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 7,
4975 .access = PL1_R, .type = ARM_CP_CONST,
4976 .resetvalue = 0 },
e60cef86
PM
4977 { .name = "ID_AA64ISAR0_EL1", .state = ARM_CP_STATE_AA64,
4978 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 0,
4979 .access = PL1_R, .type = ARM_CP_CONST,
4980 .resetvalue = cpu->id_aa64isar0 },
4981 { .name = "ID_AA64ISAR1_EL1", .state = ARM_CP_STATE_AA64,
4982 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 1,
4983 .access = PL1_R, .type = ARM_CP_CONST,
4984 .resetvalue = cpu->id_aa64isar1 },
e20d84c1
PM
4985 { .name = "ID_AA64ISAR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4986 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 2,
4987 .access = PL1_R, .type = ARM_CP_CONST,
4988 .resetvalue = 0 },
4989 { .name = "ID_AA64ISAR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4990 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 3,
4991 .access = PL1_R, .type = ARM_CP_CONST,
4992 .resetvalue = 0 },
4993 { .name = "ID_AA64ISAR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4994 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 4,
4995 .access = PL1_R, .type = ARM_CP_CONST,
4996 .resetvalue = 0 },
4997 { .name = "ID_AA64ISAR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4998 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 5,
4999 .access = PL1_R, .type = ARM_CP_CONST,
5000 .resetvalue = 0 },
5001 { .name = "ID_AA64ISAR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
5002 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 6,
5003 .access = PL1_R, .type = ARM_CP_CONST,
5004 .resetvalue = 0 },
5005 { .name = "ID_AA64ISAR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
5006 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 7,
5007 .access = PL1_R, .type = ARM_CP_CONST,
5008 .resetvalue = 0 },
e60cef86
PM
5009 { .name = "ID_AA64MMFR0_EL1", .state = ARM_CP_STATE_AA64,
5010 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
5011 .access = PL1_R, .type = ARM_CP_CONST,
5012 .resetvalue = cpu->id_aa64mmfr0 },
5013 { .name = "ID_AA64MMFR1_EL1", .state = ARM_CP_STATE_AA64,
5014 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 1,
5015 .access = PL1_R, .type = ARM_CP_CONST,
5016 .resetvalue = cpu->id_aa64mmfr1 },
e20d84c1
PM
5017 { .name = "ID_AA64MMFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
5018 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 2,
5019 .access = PL1_R, .type = ARM_CP_CONST,
5020 .resetvalue = 0 },
5021 { .name = "ID_AA64MMFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
5022 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 3,
5023 .access = PL1_R, .type = ARM_CP_CONST,
5024 .resetvalue = 0 },
5025 { .name = "ID_AA64MMFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
5026 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 4,
5027 .access = PL1_R, .type = ARM_CP_CONST,
5028 .resetvalue = 0 },
5029 { .name = "ID_AA64MMFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
5030 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 5,
5031 .access = PL1_R, .type = ARM_CP_CONST,
5032 .resetvalue = 0 },
5033 { .name = "ID_AA64MMFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
5034 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 6,
5035 .access = PL1_R, .type = ARM_CP_CONST,
5036 .resetvalue = 0 },
5037 { .name = "ID_AA64MMFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
5038 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 7,
5039 .access = PL1_R, .type = ARM_CP_CONST,
5040 .resetvalue = 0 },
a50c0f51
PM
5041 { .name = "MVFR0_EL1", .state = ARM_CP_STATE_AA64,
5042 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0,
5043 .access = PL1_R, .type = ARM_CP_CONST,
5044 .resetvalue = cpu->mvfr0 },
5045 { .name = "MVFR1_EL1", .state = ARM_CP_STATE_AA64,
5046 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1,
5047 .access = PL1_R, .type = ARM_CP_CONST,
5048 .resetvalue = cpu->mvfr1 },
5049 { .name = "MVFR2_EL1", .state = ARM_CP_STATE_AA64,
5050 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2,
5051 .access = PL1_R, .type = ARM_CP_CONST,
5052 .resetvalue = cpu->mvfr2 },
e20d84c1
PM
5053 { .name = "MVFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
5054 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 3,
5055 .access = PL1_R, .type = ARM_CP_CONST,
5056 .resetvalue = 0 },
5057 { .name = "MVFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
5058 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 4,
5059 .access = PL1_R, .type = ARM_CP_CONST,
5060 .resetvalue = 0 },
5061 { .name = "MVFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
5062 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 5,
5063 .access = PL1_R, .type = ARM_CP_CONST,
5064 .resetvalue = 0 },
5065 { .name = "MVFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
5066 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 6,
5067 .access = PL1_R, .type = ARM_CP_CONST,
5068 .resetvalue = 0 },
5069 { .name = "MVFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
5070 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 7,
5071 .access = PL1_R, .type = ARM_CP_CONST,
5072 .resetvalue = 0 },
4054bfa9
AF
5073 { .name = "PMCEID0", .state = ARM_CP_STATE_AA32,
5074 .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 6,
5075 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
5076 .resetvalue = cpu->pmceid0 },
5077 { .name = "PMCEID0_EL0", .state = ARM_CP_STATE_AA64,
5078 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 6,
5079 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
5080 .resetvalue = cpu->pmceid0 },
5081 { .name = "PMCEID1", .state = ARM_CP_STATE_AA32,
5082 .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 7,
5083 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
5084 .resetvalue = cpu->pmceid1 },
5085 { .name = "PMCEID1_EL0", .state = ARM_CP_STATE_AA64,
5086 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 7,
5087 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
5088 .resetvalue = cpu->pmceid1 },
e60cef86
PM
5089 REGINFO_SENTINEL
5090 };
be8e8128
GB
5091 /* RVBAR_EL1 is only implemented if EL1 is the highest EL */
5092 if (!arm_feature(env, ARM_FEATURE_EL3) &&
5093 !arm_feature(env, ARM_FEATURE_EL2)) {
5094 ARMCPRegInfo rvbar = {
5095 .name = "RVBAR_EL1", .state = ARM_CP_STATE_AA64,
5096 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
5097 .type = ARM_CP_CONST, .access = PL1_R, .resetvalue = cpu->rvbar
5098 };
5099 define_one_arm_cp_reg(cpu, &rvbar);
5100 }
e60cef86 5101 define_arm_cp_regs(cpu, v8_idregs);
b0d2b7d0
PM
5102 define_arm_cp_regs(cpu, v8_cp_reginfo);
5103 }
3b685ba7 5104 if (arm_feature(env, ARM_FEATURE_EL2)) {
f0d574d6 5105 uint64_t vmpidr_def = mpidr_read_val(env);
731de9e6
EI
5106 ARMCPRegInfo vpidr_regs[] = {
5107 { .name = "VPIDR", .state = ARM_CP_STATE_AA32,
5108 .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
5109 .access = PL2_RW, .accessfn = access_el3_aa32ns,
36476562
PM
5110 .resetvalue = cpu->midr, .type = ARM_CP_ALIAS,
5111 .fieldoffset = offsetoflow32(CPUARMState, cp15.vpidr_el2) },
731de9e6
EI
5112 { .name = "VPIDR_EL2", .state = ARM_CP_STATE_AA64,
5113 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
5114 .access = PL2_RW, .resetvalue = cpu->midr,
5115 .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) },
f0d574d6
EI
5116 { .name = "VMPIDR", .state = ARM_CP_STATE_AA32,
5117 .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
5118 .access = PL2_RW, .accessfn = access_el3_aa32ns,
36476562
PM
5119 .resetvalue = vmpidr_def, .type = ARM_CP_ALIAS,
5120 .fieldoffset = offsetoflow32(CPUARMState, cp15.vmpidr_el2) },
f0d574d6
EI
5121 { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_AA64,
5122 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
5123 .access = PL2_RW,
5124 .resetvalue = vmpidr_def,
5125 .fieldoffset = offsetof(CPUARMState, cp15.vmpidr_el2) },
731de9e6
EI
5126 REGINFO_SENTINEL
5127 };
5128 define_arm_cp_regs(cpu, vpidr_regs);
4771cd01 5129 define_arm_cp_regs(cpu, el2_cp_reginfo);
be8e8128
GB
5130 /* RVBAR_EL2 is only implemented if EL2 is the highest EL */
5131 if (!arm_feature(env, ARM_FEATURE_EL3)) {
5132 ARMCPRegInfo rvbar = {
5133 .name = "RVBAR_EL2", .state = ARM_CP_STATE_AA64,
5134 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 1,
5135 .type = ARM_CP_CONST, .access = PL2_R, .resetvalue = cpu->rvbar
5136 };
5137 define_one_arm_cp_reg(cpu, &rvbar);
5138 }
d42e3c26
EI
5139 } else {
5140 /* If EL2 is missing but higher ELs are enabled, we need to
5141 * register the no_el2 reginfos.
5142 */
5143 if (arm_feature(env, ARM_FEATURE_EL3)) {
f0d574d6
EI
5144 /* When EL3 exists but not EL2, VPIDR and VMPIDR take the value
5145 * of MIDR_EL1 and MPIDR_EL1.
731de9e6
EI
5146 */
5147 ARMCPRegInfo vpidr_regs[] = {
5148 { .name = "VPIDR_EL2", .state = ARM_CP_STATE_BOTH,
5149 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
5150 .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any,
5151 .type = ARM_CP_CONST, .resetvalue = cpu->midr,
5152 .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) },
f0d574d6
EI
5153 { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_BOTH,
5154 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
5155 .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any,
5156 .type = ARM_CP_NO_RAW,
5157 .writefn = arm_cp_write_ignore, .readfn = mpidr_read },
731de9e6
EI
5158 REGINFO_SENTINEL
5159 };
5160 define_arm_cp_regs(cpu, vpidr_regs);
4771cd01 5161 define_arm_cp_regs(cpu, el3_no_el2_cp_reginfo);
d42e3c26 5162 }
3b685ba7 5163 }
81547d66 5164 if (arm_feature(env, ARM_FEATURE_EL3)) {
0f1a3b24 5165 define_arm_cp_regs(cpu, el3_cp_reginfo);
e24fdd23
PM
5166 ARMCPRegInfo el3_regs[] = {
5167 { .name = "RVBAR_EL3", .state = ARM_CP_STATE_AA64,
5168 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 1,
5169 .type = ARM_CP_CONST, .access = PL3_R, .resetvalue = cpu->rvbar },
5170 { .name = "SCTLR_EL3", .state = ARM_CP_STATE_AA64,
5171 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 0,
5172 .access = PL3_RW,
5173 .raw_writefn = raw_write, .writefn = sctlr_write,
5174 .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[3]),
5175 .resetvalue = cpu->reset_sctlr },
5176 REGINFO_SENTINEL
be8e8128 5177 };
e24fdd23
PM
5178
5179 define_arm_cp_regs(cpu, el3_regs);
81547d66 5180 }
2f027fc5
PM
5181 /* The behaviour of NSACR is sufficiently various that we don't
5182 * try to describe it in a single reginfo:
5183 * if EL3 is 64 bit, then trap to EL3 from S EL1,
5184 * reads as constant 0xc00 from NS EL1 and NS EL2
5185 * if EL3 is 32 bit, then RW at EL3, RO at NS EL1 and NS EL2
5186 * if v7 without EL3, register doesn't exist
5187 * if v8 without EL3, reads as constant 0xc00 from NS EL1 and NS EL2
5188 */
5189 if (arm_feature(env, ARM_FEATURE_EL3)) {
5190 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
5191 ARMCPRegInfo nsacr = {
5192 .name = "NSACR", .type = ARM_CP_CONST,
5193 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
5194 .access = PL1_RW, .accessfn = nsacr_access,
5195 .resetvalue = 0xc00
5196 };
5197 define_one_arm_cp_reg(cpu, &nsacr);
5198 } else {
5199 ARMCPRegInfo nsacr = {
5200 .name = "NSACR",
5201 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
5202 .access = PL3_RW | PL1_R,
5203 .resetvalue = 0,
5204 .fieldoffset = offsetof(CPUARMState, cp15.nsacr)
5205 };
5206 define_one_arm_cp_reg(cpu, &nsacr);
5207 }
5208 } else {
5209 if (arm_feature(env, ARM_FEATURE_V8)) {
5210 ARMCPRegInfo nsacr = {
5211 .name = "NSACR", .type = ARM_CP_CONST,
5212 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
5213 .access = PL1_R,
5214 .resetvalue = 0xc00
5215 };
5216 define_one_arm_cp_reg(cpu, &nsacr);
5217 }
5218 }
5219
452a0955 5220 if (arm_feature(env, ARM_FEATURE_PMSA)) {
6cb0b013
PC
5221 if (arm_feature(env, ARM_FEATURE_V6)) {
5222 /* PMSAv6 not implemented */
5223 assert(arm_feature(env, ARM_FEATURE_V7));
5224 define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
5225 define_arm_cp_regs(cpu, pmsav7_cp_reginfo);
5226 } else {
5227 define_arm_cp_regs(cpu, pmsav5_cp_reginfo);
5228 }
18032bec 5229 } else {
8e5d75c9 5230 define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
18032bec
PM
5231 define_arm_cp_regs(cpu, vmsa_cp_reginfo);
5232 }
c326b979
PM
5233 if (arm_feature(env, ARM_FEATURE_THUMB2EE)) {
5234 define_arm_cp_regs(cpu, t2ee_cp_reginfo);
5235 }
6cc7a3ae
PM
5236 if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) {
5237 define_arm_cp_regs(cpu, generic_timer_cp_reginfo);
5238 }
4a501606
PM
5239 if (arm_feature(env, ARM_FEATURE_VAPA)) {
5240 define_arm_cp_regs(cpu, vapa_cp_reginfo);
5241 }
c4804214
PM
5242 if (arm_feature(env, ARM_FEATURE_CACHE_TEST_CLEAN)) {
5243 define_arm_cp_regs(cpu, cache_test_clean_cp_reginfo);
5244 }
5245 if (arm_feature(env, ARM_FEATURE_CACHE_DIRTY_REG)) {
5246 define_arm_cp_regs(cpu, cache_dirty_status_cp_reginfo);
5247 }
5248 if (arm_feature(env, ARM_FEATURE_CACHE_BLOCK_OPS)) {
5249 define_arm_cp_regs(cpu, cache_block_ops_cp_reginfo);
5250 }
18032bec
PM
5251 if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
5252 define_arm_cp_regs(cpu, omap_cp_reginfo);
5253 }
34f90529
PM
5254 if (arm_feature(env, ARM_FEATURE_STRONGARM)) {
5255 define_arm_cp_regs(cpu, strongarm_cp_reginfo);
5256 }
1047b9d7
PM
5257 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
5258 define_arm_cp_regs(cpu, xscale_cp_reginfo);
5259 }
5260 if (arm_feature(env, ARM_FEATURE_DUMMY_C15_REGS)) {
5261 define_arm_cp_regs(cpu, dummy_c15_cp_reginfo);
5262 }
7ac681cf
PM
5263 if (arm_feature(env, ARM_FEATURE_LPAE)) {
5264 define_arm_cp_regs(cpu, lpae_cp_reginfo);
5265 }
7884849c
PM
5266 /* Slightly awkwardly, the OMAP and StrongARM cores need all of
5267 * cp15 crn=0 to be writes-ignored, whereas for other cores they should
5268 * be read-only (ie write causes UNDEF exception).
5269 */
5270 {
00a29f3d
PM
5271 ARMCPRegInfo id_pre_v8_midr_cp_reginfo[] = {
5272 /* Pre-v8 MIDR space.
5273 * Note that the MIDR isn't a simple constant register because
7884849c
PM
5274 * of the TI925 behaviour where writes to another register can
5275 * cause the MIDR value to change.
97ce8d61
PC
5276 *
5277 * Unimplemented registers in the c15 0 0 0 space default to
5278 * MIDR. Define MIDR first as this entire space, then CTR, TCMTR
5279 * and friends override accordingly.
7884849c
PM
5280 */
5281 { .name = "MIDR",
97ce8d61 5282 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = CP_ANY,
7884849c 5283 .access = PL1_R, .resetvalue = cpu->midr,
d4e6df63 5284 .writefn = arm_cp_write_ignore, .raw_writefn = raw_write,
731de9e6 5285 .readfn = midr_read,
97ce8d61
PC
5286 .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
5287 .type = ARM_CP_OVERRIDE },
7884849c
PM
5288 /* crn = 0 op1 = 0 crm = 3..7 : currently unassigned; we RAZ. */
5289 { .name = "DUMMY",
5290 .cp = 15, .crn = 0, .crm = 3, .opc1 = 0, .opc2 = CP_ANY,
5291 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
5292 { .name = "DUMMY",
5293 .cp = 15, .crn = 0, .crm = 4, .opc1 = 0, .opc2 = CP_ANY,
5294 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
5295 { .name = "DUMMY",
5296 .cp = 15, .crn = 0, .crm = 5, .opc1 = 0, .opc2 = CP_ANY,
5297 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
5298 { .name = "DUMMY",
5299 .cp = 15, .crn = 0, .crm = 6, .opc1 = 0, .opc2 = CP_ANY,
5300 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
5301 { .name = "DUMMY",
5302 .cp = 15, .crn = 0, .crm = 7, .opc1 = 0, .opc2 = CP_ANY,
5303 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
5304 REGINFO_SENTINEL
5305 };
00a29f3d 5306 ARMCPRegInfo id_v8_midr_cp_reginfo[] = {
00a29f3d
PM
5307 { .name = "MIDR_EL1", .state = ARM_CP_STATE_BOTH,
5308 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 0,
731de9e6
EI
5309 .access = PL1_R, .type = ARM_CP_NO_RAW, .resetvalue = cpu->midr,
5310 .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
5311 .readfn = midr_read },
ac00c79f
SF
5312 /* crn = 0 op1 = 0 crm = 0 op2 = 4,7 : AArch32 aliases of MIDR */
5313 { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
5314 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
5315 .access = PL1_R, .resetvalue = cpu->midr },
5316 { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
5317 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 7,
5318 .access = PL1_R, .resetvalue = cpu->midr },
00a29f3d
PM
5319 { .name = "REVIDR_EL1", .state = ARM_CP_STATE_BOTH,
5320 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 6,
13b72b2b 5321 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->revidr },
00a29f3d
PM
5322 REGINFO_SENTINEL
5323 };
5324 ARMCPRegInfo id_cp_reginfo[] = {
5325 /* These are common to v8 and pre-v8 */
5326 { .name = "CTR",
5327 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 1,
5328 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
5329 { .name = "CTR_EL0", .state = ARM_CP_STATE_AA64,
5330 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 0, .crm = 0,
5331 .access = PL0_R, .accessfn = ctr_el0_access,
5332 .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
5333 /* TCMTR and TLBTR exist in v8 but have no 64-bit versions */
5334 { .name = "TCMTR",
5335 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 2,
5336 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
00a29f3d
PM
5337 REGINFO_SENTINEL
5338 };
8085ce63
PC
5339 /* TLBTR is specific to VMSA */
5340 ARMCPRegInfo id_tlbtr_reginfo = {
5341 .name = "TLBTR",
5342 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 3,
5343 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0,
5344 };
3281af81
PC
5345 /* MPUIR is specific to PMSA V6+ */
5346 ARMCPRegInfo id_mpuir_reginfo = {
5347 .name = "MPUIR",
5348 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
5349 .access = PL1_R, .type = ARM_CP_CONST,
5350 .resetvalue = cpu->pmsav7_dregion << 8
5351 };
7884849c
PM
5352 ARMCPRegInfo crn0_wi_reginfo = {
5353 .name = "CRN0_WI", .cp = 15, .crn = 0, .crm = CP_ANY,
5354 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_W,
5355 .type = ARM_CP_NOP | ARM_CP_OVERRIDE
5356 };
5357 if (arm_feature(env, ARM_FEATURE_OMAPCP) ||
5358 arm_feature(env, ARM_FEATURE_STRONGARM)) {
5359 ARMCPRegInfo *r;
5360 /* Register the blanket "writes ignored" value first to cover the
a703eda1
PC
5361 * whole space. Then update the specific ID registers to allow write
5362 * access, so that they ignore writes rather than causing them to
5363 * UNDEF.
7884849c
PM
5364 */
5365 define_one_arm_cp_reg(cpu, &crn0_wi_reginfo);
00a29f3d
PM
5366 for (r = id_pre_v8_midr_cp_reginfo;
5367 r->type != ARM_CP_SENTINEL; r++) {
5368 r->access = PL1_RW;
5369 }
7884849c
PM
5370 for (r = id_cp_reginfo; r->type != ARM_CP_SENTINEL; r++) {
5371 r->access = PL1_RW;
7884849c 5372 }
10006112 5373 id_mpuir_reginfo.access = PL1_RW;
3281af81 5374 id_tlbtr_reginfo.access = PL1_RW;
7884849c 5375 }
00a29f3d
PM
5376 if (arm_feature(env, ARM_FEATURE_V8)) {
5377 define_arm_cp_regs(cpu, id_v8_midr_cp_reginfo);
5378 } else {
5379 define_arm_cp_regs(cpu, id_pre_v8_midr_cp_reginfo);
5380 }
a703eda1 5381 define_arm_cp_regs(cpu, id_cp_reginfo);
452a0955 5382 if (!arm_feature(env, ARM_FEATURE_PMSA)) {
8085ce63 5383 define_one_arm_cp_reg(cpu, &id_tlbtr_reginfo);
3281af81
PC
5384 } else if (arm_feature(env, ARM_FEATURE_V7)) {
5385 define_one_arm_cp_reg(cpu, &id_mpuir_reginfo);
8085ce63 5386 }
7884849c
PM
5387 }
5388
97ce8d61
PC
5389 if (arm_feature(env, ARM_FEATURE_MPIDR)) {
5390 define_arm_cp_regs(cpu, mpidr_cp_reginfo);
5391 }
5392
2771db27 5393 if (arm_feature(env, ARM_FEATURE_AUXCR)) {
834a6c69
PM
5394 ARMCPRegInfo auxcr_reginfo[] = {
5395 { .name = "ACTLR_EL1", .state = ARM_CP_STATE_BOTH,
5396 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 1,
5397 .access = PL1_RW, .type = ARM_CP_CONST,
5398 .resetvalue = cpu->reset_auxcr },
5399 { .name = "ACTLR_EL2", .state = ARM_CP_STATE_BOTH,
5400 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 1,
5401 .access = PL2_RW, .type = ARM_CP_CONST,
5402 .resetvalue = 0 },
5403 { .name = "ACTLR_EL3", .state = ARM_CP_STATE_AA64,
5404 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 1,
5405 .access = PL3_RW, .type = ARM_CP_CONST,
5406 .resetvalue = 0 },
5407 REGINFO_SENTINEL
2771db27 5408 };
834a6c69 5409 define_arm_cp_regs(cpu, auxcr_reginfo);
2771db27
PM
5410 }
5411
d8ba780b 5412 if (arm_feature(env, ARM_FEATURE_CBAR)) {
f318cec6
PM
5413 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
5414 /* 32 bit view is [31:18] 0...0 [43:32]. */
5415 uint32_t cbar32 = (extract64(cpu->reset_cbar, 18, 14) << 18)
5416 | extract64(cpu->reset_cbar, 32, 12);
5417 ARMCPRegInfo cbar_reginfo[] = {
5418 { .name = "CBAR",
5419 .type = ARM_CP_CONST,
5420 .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0,
5421 .access = PL1_R, .resetvalue = cpu->reset_cbar },
5422 { .name = "CBAR_EL1", .state = ARM_CP_STATE_AA64,
5423 .type = ARM_CP_CONST,
5424 .opc0 = 3, .opc1 = 1, .crn = 15, .crm = 3, .opc2 = 0,
5425 .access = PL1_R, .resetvalue = cbar32 },
5426 REGINFO_SENTINEL
5427 };
5428 /* We don't implement a r/w 64 bit CBAR currently */
5429 assert(arm_feature(env, ARM_FEATURE_CBAR_RO));
5430 define_arm_cp_regs(cpu, cbar_reginfo);
5431 } else {
5432 ARMCPRegInfo cbar = {
5433 .name = "CBAR",
5434 .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0,
5435 .access = PL1_R|PL3_W, .resetvalue = cpu->reset_cbar,
5436 .fieldoffset = offsetof(CPUARMState,
5437 cp15.c15_config_base_address)
5438 };
5439 if (arm_feature(env, ARM_FEATURE_CBAR_RO)) {
5440 cbar.access = PL1_R;
5441 cbar.fieldoffset = 0;
5442 cbar.type = ARM_CP_CONST;
5443 }
5444 define_one_arm_cp_reg(cpu, &cbar);
5445 }
d8ba780b
PC
5446 }
5447
91db4642
CLG
5448 if (arm_feature(env, ARM_FEATURE_VBAR)) {
5449 ARMCPRegInfo vbar_cp_reginfo[] = {
5450 { .name = "VBAR", .state = ARM_CP_STATE_BOTH,
5451 .opc0 = 3, .crn = 12, .crm = 0, .opc1 = 0, .opc2 = 0,
5452 .access = PL1_RW, .writefn = vbar_write,
5453 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.vbar_s),
5454 offsetof(CPUARMState, cp15.vbar_ns) },
5455 .resetvalue = 0 },
5456 REGINFO_SENTINEL
5457 };
5458 define_arm_cp_regs(cpu, vbar_cp_reginfo);
5459 }
5460
2771db27
PM
5461 /* Generic registers whose values depend on the implementation */
5462 {
5463 ARMCPRegInfo sctlr = {
5ebafdf3 5464 .name = "SCTLR", .state = ARM_CP_STATE_BOTH,
137feaa9
FA
5465 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
5466 .access = PL1_RW,
5467 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.sctlr_s),
5468 offsetof(CPUARMState, cp15.sctlr_ns) },
d4e6df63
PM
5469 .writefn = sctlr_write, .resetvalue = cpu->reset_sctlr,
5470 .raw_writefn = raw_write,
2771db27
PM
5471 };
5472 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
5473 /* Normally we would always end the TB on an SCTLR write, but Linux
5474 * arch/arm/mach-pxa/sleep.S expects two instructions following
5475 * an MMU enable to execute from cache. Imitate this behaviour.
5476 */
5477 sctlr.type |= ARM_CP_SUPPRESS_TB_END;
5478 }
5479 define_one_arm_cp_reg(cpu, &sctlr);
5480 }
5be5e8ed
RH
5481
5482 if (arm_feature(env, ARM_FEATURE_SVE)) {
5483 define_one_arm_cp_reg(cpu, &zcr_el1_reginfo);
5484 if (arm_feature(env, ARM_FEATURE_EL2)) {
5485 define_one_arm_cp_reg(cpu, &zcr_el2_reginfo);
5486 } else {
5487 define_one_arm_cp_reg(cpu, &zcr_no_el2_reginfo);
5488 }
5489 if (arm_feature(env, ARM_FEATURE_EL3)) {
5490 define_one_arm_cp_reg(cpu, &zcr_el3_reginfo);
5491 }
5492 }
2ceb98c0
PM
5493}
5494
14969266
AF
5495void arm_cpu_register_gdb_regs_for_features(ARMCPU *cpu)
5496{
22169d41 5497 CPUState *cs = CPU(cpu);
14969266
AF
5498 CPUARMState *env = &cpu->env;
5499
6a669427
PM
5500 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
5501 gdb_register_coprocessor(cs, aarch64_fpu_gdb_get_reg,
5502 aarch64_fpu_gdb_set_reg,
5503 34, "aarch64-fpu.xml", 0);
5504 } else if (arm_feature(env, ARM_FEATURE_NEON)) {
22169d41 5505 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
56aebc89
PB
5506 51, "arm-neon.xml", 0);
5507 } else if (arm_feature(env, ARM_FEATURE_VFP3)) {
22169d41 5508 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
56aebc89
PB
5509 35, "arm-vfp3.xml", 0);
5510 } else if (arm_feature(env, ARM_FEATURE_VFP)) {
22169d41 5511 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
56aebc89
PB
5512 19, "arm-vfp.xml", 0);
5513 }
200bf5b7
AB
5514 gdb_register_coprocessor(cs, arm_gdb_get_sysreg, arm_gdb_set_sysreg,
5515 arm_gen_dynamic_xml(cs),
5516 "system-registers.xml", 0);
40f137e1
PB
5517}
5518
777dc784
PM
5519/* Sort alphabetically by type name, except for "any". */
5520static gint arm_cpu_list_compare(gconstpointer a, gconstpointer b)
5adb4839 5521{
777dc784
PM
5522 ObjectClass *class_a = (ObjectClass *)a;
5523 ObjectClass *class_b = (ObjectClass *)b;
5524 const char *name_a, *name_b;
5adb4839 5525
777dc784
PM
5526 name_a = object_class_get_name(class_a);
5527 name_b = object_class_get_name(class_b);
51492fd1 5528 if (strcmp(name_a, "any-" TYPE_ARM_CPU) == 0) {
777dc784 5529 return 1;
51492fd1 5530 } else if (strcmp(name_b, "any-" TYPE_ARM_CPU) == 0) {
777dc784
PM
5531 return -1;
5532 } else {
5533 return strcmp(name_a, name_b);
5adb4839
PB
5534 }
5535}
5536
777dc784 5537static void arm_cpu_list_entry(gpointer data, gpointer user_data)
40f137e1 5538{
777dc784 5539 ObjectClass *oc = data;
92a31361 5540 CPUListState *s = user_data;
51492fd1
AF
5541 const char *typename;
5542 char *name;
3371d272 5543
51492fd1
AF
5544 typename = object_class_get_name(oc);
5545 name = g_strndup(typename, strlen(typename) - strlen("-" TYPE_ARM_CPU));
777dc784 5546 (*s->cpu_fprintf)(s->file, " %s\n",
51492fd1
AF
5547 name);
5548 g_free(name);
777dc784
PM
5549}
5550
5551void arm_cpu_list(FILE *f, fprintf_function cpu_fprintf)
5552{
92a31361 5553 CPUListState s = {
777dc784
PM
5554 .file = f,
5555 .cpu_fprintf = cpu_fprintf,
5556 };
5557 GSList *list;
5558
5559 list = object_class_get_list(TYPE_ARM_CPU, false);
5560 list = g_slist_sort(list, arm_cpu_list_compare);
5561 (*cpu_fprintf)(f, "Available CPUs:\n");
5562 g_slist_foreach(list, arm_cpu_list_entry, &s);
5563 g_slist_free(list);
a96c0514
PM
5564#ifdef CONFIG_KVM
5565 /* The 'host' CPU type is dynamically registered only if KVM is
5566 * enabled, so we have to special-case it here:
5567 */
5568 (*cpu_fprintf)(f, " host (only available in KVM mode)\n");
5569#endif
40f137e1
PB
5570}
5571
78027bb6
CR
5572static void arm_cpu_add_definition(gpointer data, gpointer user_data)
5573{
5574 ObjectClass *oc = data;
5575 CpuDefinitionInfoList **cpu_list = user_data;
5576 CpuDefinitionInfoList *entry;
5577 CpuDefinitionInfo *info;
5578 const char *typename;
5579
5580 typename = object_class_get_name(oc);
5581 info = g_malloc0(sizeof(*info));
5582 info->name = g_strndup(typename,
5583 strlen(typename) - strlen("-" TYPE_ARM_CPU));
8ed877b7 5584 info->q_typename = g_strdup(typename);
78027bb6
CR
5585
5586 entry = g_malloc0(sizeof(*entry));
5587 entry->value = info;
5588 entry->next = *cpu_list;
5589 *cpu_list = entry;
5590}
5591
5592CpuDefinitionInfoList *arch_query_cpu_definitions(Error **errp)
5593{
5594 CpuDefinitionInfoList *cpu_list = NULL;
5595 GSList *list;
5596
5597 list = object_class_get_list(TYPE_ARM_CPU, false);
5598 g_slist_foreach(list, arm_cpu_add_definition, &cpu_list);
5599 g_slist_free(list);
5600
5601 return cpu_list;
5602}
5603
6e6efd61 5604static void add_cpreg_to_hashtable(ARMCPU *cpu, const ARMCPRegInfo *r,
51a79b03 5605 void *opaque, int state, int secstate,
9c513e78
AB
5606 int crm, int opc1, int opc2,
5607 const char *name)
6e6efd61
PM
5608{
5609 /* Private utility function for define_one_arm_cp_reg_with_opaque():
5610 * add a single reginfo struct to the hash table.
5611 */
5612 uint32_t *key = g_new(uint32_t, 1);
5613 ARMCPRegInfo *r2 = g_memdup(r, sizeof(ARMCPRegInfo));
5614 int is64 = (r->type & ARM_CP_64BIT) ? 1 : 0;
3f3c82a5
FA
5615 int ns = (secstate & ARM_CP_SECSTATE_NS) ? 1 : 0;
5616
9c513e78 5617 r2->name = g_strdup(name);
3f3c82a5
FA
5618 /* Reset the secure state to the specific incoming state. This is
5619 * necessary as the register may have been defined with both states.
5620 */
5621 r2->secure = secstate;
5622
5623 if (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1]) {
5624 /* Register is banked (using both entries in array).
5625 * Overwriting fieldoffset as the array is only used to define
5626 * banked registers but later only fieldoffset is used.
f5a0a5a5 5627 */
3f3c82a5
FA
5628 r2->fieldoffset = r->bank_fieldoffsets[ns];
5629 }
5630
5631 if (state == ARM_CP_STATE_AA32) {
5632 if (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1]) {
5633 /* If the register is banked then we don't need to migrate or
5634 * reset the 32-bit instance in certain cases:
5635 *
5636 * 1) If the register has both 32-bit and 64-bit instances then we
5637 * can count on the 64-bit instance taking care of the
5638 * non-secure bank.
5639 * 2) If ARMv8 is enabled then we can count on a 64-bit version
5640 * taking care of the secure bank. This requires that separate
5641 * 32 and 64-bit definitions are provided.
5642 */
5643 if ((r->state == ARM_CP_STATE_BOTH && ns) ||
5644 (arm_feature(&cpu->env, ARM_FEATURE_V8) && !ns)) {
7a0e58fa 5645 r2->type |= ARM_CP_ALIAS;
3f3c82a5
FA
5646 }
5647 } else if ((secstate != r->secure) && !ns) {
5648 /* The register is not banked so we only want to allow migration of
5649 * the non-secure instance.
5650 */
7a0e58fa 5651 r2->type |= ARM_CP_ALIAS;
58a1d8ce 5652 }
3f3c82a5
FA
5653
5654 if (r->state == ARM_CP_STATE_BOTH) {
5655 /* We assume it is a cp15 register if the .cp field is left unset.
5656 */
5657 if (r2->cp == 0) {
5658 r2->cp = 15;
5659 }
5660
f5a0a5a5 5661#ifdef HOST_WORDS_BIGENDIAN
3f3c82a5
FA
5662 if (r2->fieldoffset) {
5663 r2->fieldoffset += sizeof(uint32_t);
5664 }
f5a0a5a5 5665#endif
3f3c82a5 5666 }
f5a0a5a5
PM
5667 }
5668 if (state == ARM_CP_STATE_AA64) {
5669 /* To allow abbreviation of ARMCPRegInfo
5670 * definitions, we treat cp == 0 as equivalent to
5671 * the value for "standard guest-visible sysreg".
58a1d8ce
PM
5672 * STATE_BOTH definitions are also always "standard
5673 * sysreg" in their AArch64 view (the .cp value may
5674 * be non-zero for the benefit of the AArch32 view).
f5a0a5a5 5675 */
58a1d8ce 5676 if (r->cp == 0 || r->state == ARM_CP_STATE_BOTH) {
f5a0a5a5
PM
5677 r2->cp = CP_REG_ARM64_SYSREG_CP;
5678 }
5679 *key = ENCODE_AA64_CP_REG(r2->cp, r2->crn, crm,
5680 r2->opc0, opc1, opc2);
5681 } else {
51a79b03 5682 *key = ENCODE_CP_REG(r2->cp, is64, ns, r2->crn, crm, opc1, opc2);
f5a0a5a5 5683 }
6e6efd61
PM
5684 if (opaque) {
5685 r2->opaque = opaque;
5686 }
67ed771d
PM
5687 /* reginfo passed to helpers is correct for the actual access,
5688 * and is never ARM_CP_STATE_BOTH:
5689 */
5690 r2->state = state;
6e6efd61
PM
5691 /* Make sure reginfo passed to helpers for wildcarded regs
5692 * has the correct crm/opc1/opc2 for this reg, not CP_ANY:
5693 */
5694 r2->crm = crm;
5695 r2->opc1 = opc1;
5696 r2->opc2 = opc2;
5697 /* By convention, for wildcarded registers only the first
5698 * entry is used for migration; the others are marked as
7a0e58fa 5699 * ALIAS so we don't try to transfer the register
6e6efd61 5700 * multiple times. Special registers (ie NOP/WFI) are
7a0e58fa 5701 * never migratable and not even raw-accessible.
6e6efd61 5702 */
7a0e58fa
PM
5703 if ((r->type & ARM_CP_SPECIAL)) {
5704 r2->type |= ARM_CP_NO_RAW;
5705 }
5706 if (((r->crm == CP_ANY) && crm != 0) ||
6e6efd61
PM
5707 ((r->opc1 == CP_ANY) && opc1 != 0) ||
5708 ((r->opc2 == CP_ANY) && opc2 != 0)) {
1f163787 5709 r2->type |= ARM_CP_ALIAS | ARM_CP_NO_GDB;
6e6efd61
PM
5710 }
5711
375421cc
PM
5712 /* Check that raw accesses are either forbidden or handled. Note that
5713 * we can't assert this earlier because the setup of fieldoffset for
5714 * banked registers has to be done first.
5715 */
5716 if (!(r2->type & ARM_CP_NO_RAW)) {
5717 assert(!raw_accessors_invalid(r2));
5718 }
5719
6e6efd61
PM
5720 /* Overriding of an existing definition must be explicitly
5721 * requested.
5722 */
5723 if (!(r->type & ARM_CP_OVERRIDE)) {
5724 ARMCPRegInfo *oldreg;
5725 oldreg = g_hash_table_lookup(cpu->cp_regs, key);
5726 if (oldreg && !(oldreg->type & ARM_CP_OVERRIDE)) {
5727 fprintf(stderr, "Register redefined: cp=%d %d bit "
5728 "crn=%d crm=%d opc1=%d opc2=%d, "
5729 "was %s, now %s\n", r2->cp, 32 + 32 * is64,
5730 r2->crn, r2->crm, r2->opc1, r2->opc2,
5731 oldreg->name, r2->name);
5732 g_assert_not_reached();
5733 }
5734 }
5735 g_hash_table_insert(cpu->cp_regs, key, r2);
5736}
5737
5738
4b6a83fb
PM
5739void define_one_arm_cp_reg_with_opaque(ARMCPU *cpu,
5740 const ARMCPRegInfo *r, void *opaque)
5741{
5742 /* Define implementations of coprocessor registers.
5743 * We store these in a hashtable because typically
5744 * there are less than 150 registers in a space which
5745 * is 16*16*16*8*8 = 262144 in size.
5746 * Wildcarding is supported for the crm, opc1 and opc2 fields.
5747 * If a register is defined twice then the second definition is
5748 * used, so this can be used to define some generic registers and
5749 * then override them with implementation specific variations.
5750 * At least one of the original and the second definition should
5751 * include ARM_CP_OVERRIDE in its type bits -- this is just a guard
5752 * against accidental use.
f5a0a5a5
PM
5753 *
5754 * The state field defines whether the register is to be
5755 * visible in the AArch32 or AArch64 execution state. If the
5756 * state is set to ARM_CP_STATE_BOTH then we synthesise a
5757 * reginfo structure for the AArch32 view, which sees the lower
5758 * 32 bits of the 64 bit register.
5759 *
5760 * Only registers visible in AArch64 may set r->opc0; opc0 cannot
5761 * be wildcarded. AArch64 registers are always considered to be 64
5762 * bits; the ARM_CP_64BIT* flag applies only to the AArch32 view of
5763 * the register, if any.
4b6a83fb 5764 */
f5a0a5a5 5765 int crm, opc1, opc2, state;
4b6a83fb
PM
5766 int crmmin = (r->crm == CP_ANY) ? 0 : r->crm;
5767 int crmmax = (r->crm == CP_ANY) ? 15 : r->crm;
5768 int opc1min = (r->opc1 == CP_ANY) ? 0 : r->opc1;
5769 int opc1max = (r->opc1 == CP_ANY) ? 7 : r->opc1;
5770 int opc2min = (r->opc2 == CP_ANY) ? 0 : r->opc2;
5771 int opc2max = (r->opc2 == CP_ANY) ? 7 : r->opc2;
5772 /* 64 bit registers have only CRm and Opc1 fields */
5773 assert(!((r->type & ARM_CP_64BIT) && (r->opc2 || r->crn)));
f5a0a5a5
PM
5774 /* op0 only exists in the AArch64 encodings */
5775 assert((r->state != ARM_CP_STATE_AA32) || (r->opc0 == 0));
5776 /* AArch64 regs are all 64 bit so ARM_CP_64BIT is meaningless */
5777 assert((r->state != ARM_CP_STATE_AA64) || !(r->type & ARM_CP_64BIT));
5778 /* The AArch64 pseudocode CheckSystemAccess() specifies that op1
5779 * encodes a minimum access level for the register. We roll this
5780 * runtime check into our general permission check code, so check
5781 * here that the reginfo's specified permissions are strict enough
5782 * to encompass the generic architectural permission check.
5783 */
5784 if (r->state != ARM_CP_STATE_AA32) {
5785 int mask = 0;
5786 switch (r->opc1) {
5787 case 0: case 1: case 2:
5788 /* min_EL EL1 */
5789 mask = PL1_RW;
5790 break;
5791 case 3:
5792 /* min_EL EL0 */
5793 mask = PL0_RW;
5794 break;
5795 case 4:
5796 /* min_EL EL2 */
5797 mask = PL2_RW;
5798 break;
5799 case 5:
5800 /* unallocated encoding, so not possible */
5801 assert(false);
5802 break;
5803 case 6:
5804 /* min_EL EL3 */
5805 mask = PL3_RW;
5806 break;
5807 case 7:
5808 /* min_EL EL1, secure mode only (we don't check the latter) */
5809 mask = PL1_RW;
5810 break;
5811 default:
5812 /* broken reginfo with out-of-range opc1 */
5813 assert(false);
5814 break;
5815 }
5816 /* assert our permissions are not too lax (stricter is fine) */
5817 assert((r->access & ~mask) == 0);
5818 }
5819
4b6a83fb
PM
5820 /* Check that the register definition has enough info to handle
5821 * reads and writes if they are permitted.
5822 */
5823 if (!(r->type & (ARM_CP_SPECIAL|ARM_CP_CONST))) {
5824 if (r->access & PL3_R) {
3f3c82a5
FA
5825 assert((r->fieldoffset ||
5826 (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
5827 r->readfn);
4b6a83fb
PM
5828 }
5829 if (r->access & PL3_W) {
3f3c82a5
FA
5830 assert((r->fieldoffset ||
5831 (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
5832 r->writefn);
4b6a83fb
PM
5833 }
5834 }
5835 /* Bad type field probably means missing sentinel at end of reg list */
5836 assert(cptype_valid(r->type));
5837 for (crm = crmmin; crm <= crmmax; crm++) {
5838 for (opc1 = opc1min; opc1 <= opc1max; opc1++) {
5839 for (opc2 = opc2min; opc2 <= opc2max; opc2++) {
f5a0a5a5
PM
5840 for (state = ARM_CP_STATE_AA32;
5841 state <= ARM_CP_STATE_AA64; state++) {
5842 if (r->state != state && r->state != ARM_CP_STATE_BOTH) {
5843 continue;
5844 }
3f3c82a5
FA
5845 if (state == ARM_CP_STATE_AA32) {
5846 /* Under AArch32 CP registers can be common
5847 * (same for secure and non-secure world) or banked.
5848 */
9c513e78
AB
5849 char *name;
5850
3f3c82a5
FA
5851 switch (r->secure) {
5852 case ARM_CP_SECSTATE_S:
5853 case ARM_CP_SECSTATE_NS:
5854 add_cpreg_to_hashtable(cpu, r, opaque, state,
9c513e78
AB
5855 r->secure, crm, opc1, opc2,
5856 r->name);
3f3c82a5
FA
5857 break;
5858 default:
9c513e78 5859 name = g_strdup_printf("%s_S", r->name);
3f3c82a5
FA
5860 add_cpreg_to_hashtable(cpu, r, opaque, state,
5861 ARM_CP_SECSTATE_S,
9c513e78
AB
5862 crm, opc1, opc2, name);
5863 g_free(name);
3f3c82a5
FA
5864 add_cpreg_to_hashtable(cpu, r, opaque, state,
5865 ARM_CP_SECSTATE_NS,
9c513e78 5866 crm, opc1, opc2, r->name);
3f3c82a5
FA
5867 break;
5868 }
5869 } else {
5870 /* AArch64 registers get mapped to non-secure instance
5871 * of AArch32 */
5872 add_cpreg_to_hashtable(cpu, r, opaque, state,
5873 ARM_CP_SECSTATE_NS,
9c513e78 5874 crm, opc1, opc2, r->name);
3f3c82a5 5875 }
f5a0a5a5 5876 }
4b6a83fb
PM
5877 }
5878 }
5879 }
5880}
5881
5882void define_arm_cp_regs_with_opaque(ARMCPU *cpu,
5883 const ARMCPRegInfo *regs, void *opaque)
5884{
5885 /* Define a whole list of registers */
5886 const ARMCPRegInfo *r;
5887 for (r = regs; r->type != ARM_CP_SENTINEL; r++) {
5888 define_one_arm_cp_reg_with_opaque(cpu, r, opaque);
5889 }
5890}
5891
60322b39 5892const ARMCPRegInfo *get_arm_cp_reginfo(GHashTable *cpregs, uint32_t encoded_cp)
4b6a83fb 5893{
60322b39 5894 return g_hash_table_lookup(cpregs, &encoded_cp);
4b6a83fb
PM
5895}
5896
c4241c7d
PM
5897void arm_cp_write_ignore(CPUARMState *env, const ARMCPRegInfo *ri,
5898 uint64_t value)
4b6a83fb
PM
5899{
5900 /* Helper coprocessor write function for write-ignore registers */
4b6a83fb
PM
5901}
5902
c4241c7d 5903uint64_t arm_cp_read_zero(CPUARMState *env, const ARMCPRegInfo *ri)
4b6a83fb
PM
5904{
5905 /* Helper coprocessor write function for read-as-zero registers */
4b6a83fb
PM
5906 return 0;
5907}
5908
f5a0a5a5
PM
5909void arm_cp_reset_ignore(CPUARMState *env, const ARMCPRegInfo *opaque)
5910{
5911 /* Helper coprocessor reset function for do-nothing-on-reset registers */
5912}
5913
af393ffc 5914static int bad_mode_switch(CPUARMState *env, int mode, CPSRWriteType write_type)
37064a8b
PM
5915{
5916 /* Return true if it is not valid for us to switch to
5917 * this CPU mode (ie all the UNPREDICTABLE cases in
5918 * the ARM ARM CPSRWriteByInstr pseudocode).
5919 */
af393ffc
PM
5920
5921 /* Changes to or from Hyp via MSR and CPS are illegal. */
5922 if (write_type == CPSRWriteByInstr &&
5923 ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_HYP ||
5924 mode == ARM_CPU_MODE_HYP)) {
5925 return 1;
5926 }
5927
37064a8b
PM
5928 switch (mode) {
5929 case ARM_CPU_MODE_USR:
10eacda7 5930 return 0;
37064a8b
PM
5931 case ARM_CPU_MODE_SYS:
5932 case ARM_CPU_MODE_SVC:
5933 case ARM_CPU_MODE_ABT:
5934 case ARM_CPU_MODE_UND:
5935 case ARM_CPU_MODE_IRQ:
5936 case ARM_CPU_MODE_FIQ:
52ff951b
PM
5937 /* Note that we don't implement the IMPDEF NSACR.RFR which in v7
5938 * allows FIQ mode to be Secure-only. (In v8 this doesn't exist.)
5939 */
10eacda7
PM
5940 /* If HCR.TGE is set then changes from Monitor to NS PL1 via MSR
5941 * and CPS are treated as illegal mode changes.
5942 */
5943 if (write_type == CPSRWriteByInstr &&
5944 (env->cp15.hcr_el2 & HCR_TGE) &&
5945 (env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON &&
5946 !arm_is_secure_below_el3(env)) {
5947 return 1;
5948 }
37064a8b 5949 return 0;
e6c8fc07
PM
5950 case ARM_CPU_MODE_HYP:
5951 return !arm_feature(env, ARM_FEATURE_EL2)
5952 || arm_current_el(env) < 2 || arm_is_secure(env);
027fc527 5953 case ARM_CPU_MODE_MON:
58ae2d1f 5954 return arm_current_el(env) < 3;
37064a8b
PM
5955 default:
5956 return 1;
5957 }
5958}
5959
2f4a40e5
AZ
5960uint32_t cpsr_read(CPUARMState *env)
5961{
5962 int ZF;
6fbe23d5
PB
5963 ZF = (env->ZF == 0);
5964 return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) |
2f4a40e5
AZ
5965 (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27)
5966 | (env->thumb << 5) | ((env->condexec_bits & 3) << 25)
5967 | ((env->condexec_bits & 0xfc) << 8)
af519934 5968 | (env->GE << 16) | (env->daif & CPSR_AIF);
2f4a40e5
AZ
5969}
5970
50866ba5
PM
5971void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask,
5972 CPSRWriteType write_type)
2f4a40e5 5973{
6e8801f9
FA
5974 uint32_t changed_daif;
5975
2f4a40e5 5976 if (mask & CPSR_NZCV) {
6fbe23d5
PB
5977 env->ZF = (~val) & CPSR_Z;
5978 env->NF = val;
2f4a40e5
AZ
5979 env->CF = (val >> 29) & 1;
5980 env->VF = (val << 3) & 0x80000000;
5981 }
5982 if (mask & CPSR_Q)
5983 env->QF = ((val & CPSR_Q) != 0);
5984 if (mask & CPSR_T)
5985 env->thumb = ((val & CPSR_T) != 0);
5986 if (mask & CPSR_IT_0_1) {
5987 env->condexec_bits &= ~3;
5988 env->condexec_bits |= (val >> 25) & 3;
5989 }
5990 if (mask & CPSR_IT_2_7) {
5991 env->condexec_bits &= 3;
5992 env->condexec_bits |= (val >> 8) & 0xfc;
5993 }
5994 if (mask & CPSR_GE) {
5995 env->GE = (val >> 16) & 0xf;
5996 }
5997
6e8801f9
FA
5998 /* In a V7 implementation that includes the security extensions but does
5999 * not include Virtualization Extensions the SCR.FW and SCR.AW bits control
6000 * whether non-secure software is allowed to change the CPSR_F and CPSR_A
6001 * bits respectively.
6002 *
6003 * In a V8 implementation, it is permitted for privileged software to
6004 * change the CPSR A/F bits regardless of the SCR.AW/FW bits.
6005 */
f8c88bbc 6006 if (write_type != CPSRWriteRaw && !arm_feature(env, ARM_FEATURE_V8) &&
6e8801f9
FA
6007 arm_feature(env, ARM_FEATURE_EL3) &&
6008 !arm_feature(env, ARM_FEATURE_EL2) &&
6009 !arm_is_secure(env)) {
6010
6011 changed_daif = (env->daif ^ val) & mask;
6012
6013 if (changed_daif & CPSR_A) {
6014 /* Check to see if we are allowed to change the masking of async
6015 * abort exceptions from a non-secure state.
6016 */
6017 if (!(env->cp15.scr_el3 & SCR_AW)) {
6018 qemu_log_mask(LOG_GUEST_ERROR,
6019 "Ignoring attempt to switch CPSR_A flag from "
6020 "non-secure world with SCR.AW bit clear\n");
6021 mask &= ~CPSR_A;
6022 }
6023 }
6024
6025 if (changed_daif & CPSR_F) {
6026 /* Check to see if we are allowed to change the masking of FIQ
6027 * exceptions from a non-secure state.
6028 */
6029 if (!(env->cp15.scr_el3 & SCR_FW)) {
6030 qemu_log_mask(LOG_GUEST_ERROR,
6031 "Ignoring attempt to switch CPSR_F flag from "
6032 "non-secure world with SCR.FW bit clear\n");
6033 mask &= ~CPSR_F;
6034 }
6035
6036 /* Check whether non-maskable FIQ (NMFI) support is enabled.
6037 * If this bit is set software is not allowed to mask
6038 * FIQs, but is allowed to set CPSR_F to 0.
6039 */
6040 if ((A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_NMFI) &&
6041 (val & CPSR_F)) {
6042 qemu_log_mask(LOG_GUEST_ERROR,
6043 "Ignoring attempt to enable CPSR_F flag "
6044 "(non-maskable FIQ [NMFI] support enabled)\n");
6045 mask &= ~CPSR_F;
6046 }
6047 }
6048 }
6049
4cc35614
PM
6050 env->daif &= ~(CPSR_AIF & mask);
6051 env->daif |= val & CPSR_AIF & mask;
6052
f8c88bbc
PM
6053 if (write_type != CPSRWriteRaw &&
6054 ((env->uncached_cpsr ^ val) & mask & CPSR_M)) {
8c4f0eb9
PM
6055 if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_USR) {
6056 /* Note that we can only get here in USR mode if this is a
6057 * gdb stub write; for this case we follow the architectural
6058 * behaviour for guest writes in USR mode of ignoring an attempt
6059 * to switch mode. (Those are caught by translate.c for writes
6060 * triggered by guest instructions.)
6061 */
6062 mask &= ~CPSR_M;
6063 } else if (bad_mode_switch(env, val & CPSR_M, write_type)) {
81907a58
PM
6064 /* Attempt to switch to an invalid mode: this is UNPREDICTABLE in
6065 * v7, and has defined behaviour in v8:
6066 * + leave CPSR.M untouched
6067 * + allow changes to the other CPSR fields
6068 * + set PSTATE.IL
6069 * For user changes via the GDB stub, we don't set PSTATE.IL,
6070 * as this would be unnecessarily harsh for a user error.
37064a8b
PM
6071 */
6072 mask &= ~CPSR_M;
81907a58
PM
6073 if (write_type != CPSRWriteByGDBStub &&
6074 arm_feature(env, ARM_FEATURE_V8)) {
6075 mask |= CPSR_IL;
6076 val |= CPSR_IL;
6077 }
37064a8b
PM
6078 } else {
6079 switch_mode(env, val & CPSR_M);
6080 }
2f4a40e5
AZ
6081 }
6082 mask &= ~CACHED_CPSR_BITS;
6083 env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask);
6084}
6085
b26eefb6
PB
6086/* Sign/zero extend */
6087uint32_t HELPER(sxtb16)(uint32_t x)
6088{
6089 uint32_t res;
6090 res = (uint16_t)(int8_t)x;
6091 res |= (uint32_t)(int8_t)(x >> 16) << 16;
6092 return res;
6093}
6094
6095uint32_t HELPER(uxtb16)(uint32_t x)
6096{
6097 uint32_t res;
6098 res = (uint16_t)(uint8_t)x;
6099 res |= (uint32_t)(uint8_t)(x >> 16) << 16;
6100 return res;
6101}
6102
3670669c
PB
6103int32_t HELPER(sdiv)(int32_t num, int32_t den)
6104{
6105 if (den == 0)
6106 return 0;
686eeb93
AJ
6107 if (num == INT_MIN && den == -1)
6108 return INT_MIN;
3670669c
PB
6109 return num / den;
6110}
6111
6112uint32_t HELPER(udiv)(uint32_t num, uint32_t den)
6113{
6114 if (den == 0)
6115 return 0;
6116 return num / den;
6117}
6118
6119uint32_t HELPER(rbit)(uint32_t x)
6120{
42fedbca 6121 return revbit32(x);
3670669c
PB
6122}
6123
5fafdf24 6124#if defined(CONFIG_USER_ONLY)
b5ff1b31 6125
9ee6e8bb 6126/* These should probably raise undefined insn exceptions. */
0ecb72a5 6127void HELPER(v7m_msr)(CPUARMState *env, uint32_t reg, uint32_t val)
9ee6e8bb 6128{
a47dddd7
AF
6129 ARMCPU *cpu = arm_env_get_cpu(env);
6130
6131 cpu_abort(CPU(cpu), "v7m_msr %d\n", reg);
9ee6e8bb
PB
6132}
6133
0ecb72a5 6134uint32_t HELPER(v7m_mrs)(CPUARMState *env, uint32_t reg)
9ee6e8bb 6135{
a47dddd7
AF
6136 ARMCPU *cpu = arm_env_get_cpu(env);
6137
6138 cpu_abort(CPU(cpu), "v7m_mrs %d\n", reg);
9ee6e8bb
PB
6139 return 0;
6140}
6141
fb602cb7
PM
6142void HELPER(v7m_bxns)(CPUARMState *env, uint32_t dest)
6143{
6144 /* translate.c should never generate calls here in user-only mode */
6145 g_assert_not_reached();
6146}
6147
3e3fa230
PM
6148void HELPER(v7m_blxns)(CPUARMState *env, uint32_t dest)
6149{
6150 /* translate.c should never generate calls here in user-only mode */
6151 g_assert_not_reached();
6152}
6153
5158de24
PM
6154uint32_t HELPER(v7m_tt)(CPUARMState *env, uint32_t addr, uint32_t op)
6155{
6156 /* The TT instructions can be used by unprivileged code, but in
6157 * user-only emulation we don't have the MPU.
6158 * Luckily since we know we are NonSecure unprivileged (and that in
6159 * turn means that the A flag wasn't specified), all the bits in the
6160 * register must be zero:
6161 * IREGION: 0 because IRVALID is 0
6162 * IRVALID: 0 because NS
6163 * S: 0 because NS
6164 * NSRW: 0 because NS
6165 * NSR: 0 because NS
6166 * RW: 0 because unpriv and A flag not set
6167 * R: 0 because unpriv and A flag not set
6168 * SRVALID: 0 because NS
6169 * MRVALID: 0 because unpriv and A flag not set
6170 * SREGION: 0 becaus SRVALID is 0
6171 * MREGION: 0 because MRVALID is 0
6172 */
6173 return 0;
6174}
6175
0ecb72a5 6176void switch_mode(CPUARMState *env, int mode)
b5ff1b31 6177{
a47dddd7
AF
6178 ARMCPU *cpu = arm_env_get_cpu(env);
6179
6180 if (mode != ARM_CPU_MODE_USR) {
6181 cpu_abort(CPU(cpu), "Tried to switch out of user mode\n");
6182 }
b5ff1b31
FB
6183}
6184
012a906b
GB
6185uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
6186 uint32_t cur_el, bool secure)
9e729b57
EI
6187{
6188 return 1;
6189}
6190
ce02049d
GB
6191void aarch64_sync_64_to_32(CPUARMState *env)
6192{
6193 g_assert_not_reached();
6194}
6195
b5ff1b31
FB
6196#else
6197
0ecb72a5 6198void switch_mode(CPUARMState *env, int mode)
b5ff1b31
FB
6199{
6200 int old_mode;
6201 int i;
6202
6203 old_mode = env->uncached_cpsr & CPSR_M;
6204 if (mode == old_mode)
6205 return;
6206
6207 if (old_mode == ARM_CPU_MODE_FIQ) {
6208 memcpy (env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t));
8637c67f 6209 memcpy (env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t));
b5ff1b31
FB
6210 } else if (mode == ARM_CPU_MODE_FIQ) {
6211 memcpy (env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t));
8637c67f 6212 memcpy (env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t));
b5ff1b31
FB
6213 }
6214
f5206413 6215 i = bank_number(old_mode);
b5ff1b31
FB
6216 env->banked_r13[i] = env->regs[13];
6217 env->banked_r14[i] = env->regs[14];
6218 env->banked_spsr[i] = env->spsr;
6219
f5206413 6220 i = bank_number(mode);
b5ff1b31
FB
6221 env->regs[13] = env->banked_r13[i];
6222 env->regs[14] = env->banked_r14[i];
6223 env->spsr = env->banked_spsr[i];
6224}
6225
0eeb17d6
GB
6226/* Physical Interrupt Target EL Lookup Table
6227 *
6228 * [ From ARM ARM section G1.13.4 (Table G1-15) ]
6229 *
6230 * The below multi-dimensional table is used for looking up the target
6231 * exception level given numerous condition criteria. Specifically, the
6232 * target EL is based on SCR and HCR routing controls as well as the
6233 * currently executing EL and secure state.
6234 *
6235 * Dimensions:
6236 * target_el_table[2][2][2][2][2][4]
6237 * | | | | | +--- Current EL
6238 * | | | | +------ Non-secure(0)/Secure(1)
6239 * | | | +--------- HCR mask override
6240 * | | +------------ SCR exec state control
6241 * | +--------------- SCR mask override
6242 * +------------------ 32-bit(0)/64-bit(1) EL3
6243 *
6244 * The table values are as such:
6245 * 0-3 = EL0-EL3
6246 * -1 = Cannot occur
6247 *
6248 * The ARM ARM target EL table includes entries indicating that an "exception
6249 * is not taken". The two cases where this is applicable are:
6250 * 1) An exception is taken from EL3 but the SCR does not have the exception
6251 * routed to EL3.
6252 * 2) An exception is taken from EL2 but the HCR does not have the exception
6253 * routed to EL2.
6254 * In these two cases, the below table contain a target of EL1. This value is
6255 * returned as it is expected that the consumer of the table data will check
6256 * for "target EL >= current EL" to ensure the exception is not taken.
6257 *
6258 * SCR HCR
6259 * 64 EA AMO From
6260 * BIT IRQ IMO Non-secure Secure
6261 * EL3 FIQ RW FMO EL0 EL1 EL2 EL3 EL0 EL1 EL2 EL3
6262 */
82c39f6a 6263static const int8_t target_el_table[2][2][2][2][2][4] = {
0eeb17d6
GB
6264 {{{{/* 0 0 0 0 */{ 1, 1, 2, -1 },{ 3, -1, -1, 3 },},
6265 {/* 0 0 0 1 */{ 2, 2, 2, -1 },{ 3, -1, -1, 3 },},},
6266 {{/* 0 0 1 0 */{ 1, 1, 2, -1 },{ 3, -1, -1, 3 },},
6267 {/* 0 0 1 1 */{ 2, 2, 2, -1 },{ 3, -1, -1, 3 },},},},
6268 {{{/* 0 1 0 0 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},
6269 {/* 0 1 0 1 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},},
6270 {{/* 0 1 1 0 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},
6271 {/* 0 1 1 1 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},},},},
6272 {{{{/* 1 0 0 0 */{ 1, 1, 2, -1 },{ 1, 1, -1, 1 },},
6273 {/* 1 0 0 1 */{ 2, 2, 2, -1 },{ 1, 1, -1, 1 },},},
6274 {{/* 1 0 1 0 */{ 1, 1, 1, -1 },{ 1, 1, -1, 1 },},
6275 {/* 1 0 1 1 */{ 2, 2, 2, -1 },{ 1, 1, -1, 1 },},},},
6276 {{{/* 1 1 0 0 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},
6277 {/* 1 1 0 1 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},},
6278 {{/* 1 1 1 0 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},
6279 {/* 1 1 1 1 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},},},},
6280};
6281
6282/*
6283 * Determine the target EL for physical exceptions
6284 */
012a906b
GB
6285uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
6286 uint32_t cur_el, bool secure)
0eeb17d6
GB
6287{
6288 CPUARMState *env = cs->env_ptr;
2cde031f 6289 int rw;
0eeb17d6
GB
6290 int scr;
6291 int hcr;
6292 int target_el;
2cde031f
SS
6293 /* Is the highest EL AArch64? */
6294 int is64 = arm_feature(env, ARM_FEATURE_AARCH64);
6295
6296 if (arm_feature(env, ARM_FEATURE_EL3)) {
6297 rw = ((env->cp15.scr_el3 & SCR_RW) == SCR_RW);
6298 } else {
6299 /* Either EL2 is the highest EL (and so the EL2 register width
6300 * is given by is64); or there is no EL2 or EL3, in which case
6301 * the value of 'rw' does not affect the table lookup anyway.
6302 */
6303 rw = is64;
6304 }
0eeb17d6
GB
6305
6306 switch (excp_idx) {
6307 case EXCP_IRQ:
6308 scr = ((env->cp15.scr_el3 & SCR_IRQ) == SCR_IRQ);
6309 hcr = ((env->cp15.hcr_el2 & HCR_IMO) == HCR_IMO);
6310 break;
6311 case EXCP_FIQ:
6312 scr = ((env->cp15.scr_el3 & SCR_FIQ) == SCR_FIQ);
6313 hcr = ((env->cp15.hcr_el2 & HCR_FMO) == HCR_FMO);
6314 break;
6315 default:
6316 scr = ((env->cp15.scr_el3 & SCR_EA) == SCR_EA);
6317 hcr = ((env->cp15.hcr_el2 & HCR_AMO) == HCR_AMO);
6318 break;
6319 };
6320
6321 /* If HCR.TGE is set then HCR is treated as being 1 */
6322 hcr |= ((env->cp15.hcr_el2 & HCR_TGE) == HCR_TGE);
6323
6324 /* Perform a table-lookup for the target EL given the current state */
6325 target_el = target_el_table[is64][scr][rw][hcr][secure][cur_el];
6326
6327 assert(target_el > 0);
6328
6329 return target_el;
6330}
6331
fd592d89
PM
6332static bool v7m_stack_write(ARMCPU *cpu, uint32_t addr, uint32_t value,
6333 ARMMMUIdx mmu_idx, bool ignfault)
9ee6e8bb 6334{
fd592d89
PM
6335 CPUState *cs = CPU(cpu);
6336 CPUARMState *env = &cpu->env;
6337 MemTxAttrs attrs = {};
6338 MemTxResult txres;
6339 target_ulong page_size;
6340 hwaddr physaddr;
6341 int prot;
6342 ARMMMUFaultInfo fi;
6343 bool secure = mmu_idx & ARM_MMU_IDX_M_S;
6344 int exc;
6345 bool exc_secure;
6346
6347 if (get_phys_addr(env, addr, MMU_DATA_STORE, mmu_idx, &physaddr,
6348 &attrs, &prot, &page_size, &fi, NULL)) {
6349 /* MPU/SAU lookup failed */
6350 if (fi.type == ARMFault_QEMU_SFault) {
6351 qemu_log_mask(CPU_LOG_INT,
6352 "...SecureFault with SFSR.AUVIOL during stacking\n");
6353 env->v7m.sfsr |= R_V7M_SFSR_AUVIOL_MASK | R_V7M_SFSR_SFARVALID_MASK;
6354 env->v7m.sfar = addr;
6355 exc = ARMV7M_EXCP_SECURE;
6356 exc_secure = false;
6357 } else {
6358 qemu_log_mask(CPU_LOG_INT, "...MemManageFault with CFSR.MSTKERR\n");
6359 env->v7m.cfsr[secure] |= R_V7M_CFSR_MSTKERR_MASK;
6360 exc = ARMV7M_EXCP_MEM;
6361 exc_secure = secure;
6362 }
6363 goto pend_fault;
6364 }
6365 address_space_stl_le(arm_addressspace(cs, attrs), physaddr, value,
6366 attrs, &txres);
6367 if (txres != MEMTX_OK) {
6368 /* BusFault trying to write the data */
6369 qemu_log_mask(CPU_LOG_INT, "...BusFault with BFSR.STKERR\n");
6370 env->v7m.cfsr[M_REG_NS] |= R_V7M_CFSR_STKERR_MASK;
6371 exc = ARMV7M_EXCP_BUS;
6372 exc_secure = false;
6373 goto pend_fault;
6374 }
6375 return true;
70d74660 6376
fd592d89
PM
6377pend_fault:
6378 /* By pending the exception at this point we are making
6379 * the IMPDEF choice "overridden exceptions pended" (see the
6380 * MergeExcInfo() pseudocode). The other choice would be to not
6381 * pend them now and then make a choice about which to throw away
6382 * later if we have two derived exceptions.
6383 * The only case when we must not pend the exception but instead
6384 * throw it away is if we are doing the push of the callee registers
6385 * and we've already generated a derived exception. Even in this
6386 * case we will still update the fault status registers.
6387 */
6388 if (!ignfault) {
6389 armv7m_nvic_set_pending_derived(env->nvic, exc, exc_secure);
6390 }
6391 return false;
9ee6e8bb
PB
6392}
6393
95695eff
PM
6394static bool v7m_stack_read(ARMCPU *cpu, uint32_t *dest, uint32_t addr,
6395 ARMMMUIdx mmu_idx)
6396{
6397 CPUState *cs = CPU(cpu);
6398 CPUARMState *env = &cpu->env;
6399 MemTxAttrs attrs = {};
6400 MemTxResult txres;
6401 target_ulong page_size;
6402 hwaddr physaddr;
6403 int prot;
6404 ARMMMUFaultInfo fi;
6405 bool secure = mmu_idx & ARM_MMU_IDX_M_S;
6406 int exc;
6407 bool exc_secure;
6408 uint32_t value;
6409
6410 if (get_phys_addr(env, addr, MMU_DATA_LOAD, mmu_idx, &physaddr,
6411 &attrs, &prot, &page_size, &fi, NULL)) {
6412 /* MPU/SAU lookup failed */
6413 if (fi.type == ARMFault_QEMU_SFault) {
6414 qemu_log_mask(CPU_LOG_INT,
6415 "...SecureFault with SFSR.AUVIOL during unstack\n");
6416 env->v7m.sfsr |= R_V7M_SFSR_AUVIOL_MASK | R_V7M_SFSR_SFARVALID_MASK;
6417 env->v7m.sfar = addr;
6418 exc = ARMV7M_EXCP_SECURE;
6419 exc_secure = false;
6420 } else {
6421 qemu_log_mask(CPU_LOG_INT,
6422 "...MemManageFault with CFSR.MUNSTKERR\n");
6423 env->v7m.cfsr[secure] |= R_V7M_CFSR_MUNSTKERR_MASK;
6424 exc = ARMV7M_EXCP_MEM;
6425 exc_secure = secure;
6426 }
6427 goto pend_fault;
6428 }
6429
6430 value = address_space_ldl(arm_addressspace(cs, attrs), physaddr,
6431 attrs, &txres);
6432 if (txres != MEMTX_OK) {
6433 /* BusFault trying to read the data */
6434 qemu_log_mask(CPU_LOG_INT, "...BusFault with BFSR.UNSTKERR\n");
6435 env->v7m.cfsr[M_REG_NS] |= R_V7M_CFSR_UNSTKERR_MASK;
6436 exc = ARMV7M_EXCP_BUS;
6437 exc_secure = false;
6438 goto pend_fault;
6439 }
6440
6441 *dest = value;
6442 return true;
6443
6444pend_fault:
6445 /* By pending the exception at this point we are making
6446 * the IMPDEF choice "overridden exceptions pended" (see the
6447 * MergeExcInfo() pseudocode). The other choice would be to not
6448 * pend them now and then make a choice about which to throw away
6449 * later if we have two derived exceptions.
6450 */
6451 armv7m_nvic_set_pending(env->nvic, exc, exc_secure);
6452 return false;
6453}
6454
fb602cb7
PM
6455/* Return true if we're using the process stack pointer (not the MSP) */
6456static bool v7m_using_psp(CPUARMState *env)
6457{
6458 /* Handler mode always uses the main stack; for thread mode
6459 * the CONTROL.SPSEL bit determines the answer.
6460 * Note that in v7M it is not possible to be in Handler mode with
6461 * CONTROL.SPSEL non-zero, but in v8M it is, so we must check both.
6462 */
6463 return !arm_v7m_is_handler_mode(env) &&
6464 env->v7m.control[env->v7m.secure] & R_V7M_CONTROL_SPSEL_MASK;
6465}
6466
3f0cddee
PM
6467/* Write to v7M CONTROL.SPSEL bit for the specified security bank.
6468 * This may change the current stack pointer between Main and Process
6469 * stack pointers if it is done for the CONTROL register for the current
6470 * security state.
de2db7ec 6471 */
3f0cddee
PM
6472static void write_v7m_control_spsel_for_secstate(CPUARMState *env,
6473 bool new_spsel,
6474 bool secstate)
9ee6e8bb 6475{
3f0cddee 6476 bool old_is_psp = v7m_using_psp(env);
de2db7ec 6477
3f0cddee
PM
6478 env->v7m.control[secstate] =
6479 deposit32(env->v7m.control[secstate],
de2db7ec
PM
6480 R_V7M_CONTROL_SPSEL_SHIFT,
6481 R_V7M_CONTROL_SPSEL_LENGTH, new_spsel);
6482
3f0cddee
PM
6483 if (secstate == env->v7m.secure) {
6484 bool new_is_psp = v7m_using_psp(env);
6485 uint32_t tmp;
abc24d86 6486
3f0cddee
PM
6487 if (old_is_psp != new_is_psp) {
6488 tmp = env->v7m.other_sp;
6489 env->v7m.other_sp = env->regs[13];
6490 env->regs[13] = tmp;
6491 }
de2db7ec
PM
6492 }
6493}
6494
3f0cddee
PM
6495/* Write to v7M CONTROL.SPSEL bit. This may change the current
6496 * stack pointer between Main and Process stack pointers.
6497 */
6498static void write_v7m_control_spsel(CPUARMState *env, bool new_spsel)
6499{
6500 write_v7m_control_spsel_for_secstate(env, new_spsel, env->v7m.secure);
6501}
6502
de2db7ec
PM
6503void write_v7m_exception(CPUARMState *env, uint32_t new_exc)
6504{
6505 /* Write a new value to v7m.exception, thus transitioning into or out
6506 * of Handler mode; this may result in a change of active stack pointer.
6507 */
6508 bool new_is_psp, old_is_psp = v7m_using_psp(env);
6509 uint32_t tmp;
abc24d86 6510
de2db7ec
PM
6511 env->v7m.exception = new_exc;
6512
6513 new_is_psp = v7m_using_psp(env);
6514
6515 if (old_is_psp != new_is_psp) {
6516 tmp = env->v7m.other_sp;
6517 env->v7m.other_sp = env->regs[13];
6518 env->regs[13] = tmp;
9ee6e8bb
PB
6519 }
6520}
6521
fb602cb7
PM
6522/* Switch M profile security state between NS and S */
6523static void switch_v7m_security_state(CPUARMState *env, bool new_secstate)
6524{
6525 uint32_t new_ss_msp, new_ss_psp;
6526
6527 if (env->v7m.secure == new_secstate) {
6528 return;
6529 }
6530
6531 /* All the banked state is accessed by looking at env->v7m.secure
6532 * except for the stack pointer; rearrange the SP appropriately.
6533 */
6534 new_ss_msp = env->v7m.other_ss_msp;
6535 new_ss_psp = env->v7m.other_ss_psp;
6536
6537 if (v7m_using_psp(env)) {
6538 env->v7m.other_ss_psp = env->regs[13];
6539 env->v7m.other_ss_msp = env->v7m.other_sp;
6540 } else {
6541 env->v7m.other_ss_msp = env->regs[13];
6542 env->v7m.other_ss_psp = env->v7m.other_sp;
6543 }
6544
6545 env->v7m.secure = new_secstate;
6546
6547 if (v7m_using_psp(env)) {
6548 env->regs[13] = new_ss_psp;
6549 env->v7m.other_sp = new_ss_msp;
6550 } else {
6551 env->regs[13] = new_ss_msp;
6552 env->v7m.other_sp = new_ss_psp;
6553 }
6554}
6555
6556void HELPER(v7m_bxns)(CPUARMState *env, uint32_t dest)
6557{
6558 /* Handle v7M BXNS:
6559 * - if the return value is a magic value, do exception return (like BX)
6560 * - otherwise bit 0 of the return value is the target security state
6561 */
d02a8698
PM
6562 uint32_t min_magic;
6563
6564 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
6565 /* Covers FNC_RETURN and EXC_RETURN magic */
6566 min_magic = FNC_RETURN_MIN_MAGIC;
6567 } else {
6568 /* EXC_RETURN magic only */
6569 min_magic = EXC_RETURN_MIN_MAGIC;
6570 }
6571
6572 if (dest >= min_magic) {
fb602cb7
PM
6573 /* This is an exception return magic value; put it where
6574 * do_v7m_exception_exit() expects and raise EXCEPTION_EXIT.
6575 * Note that if we ever add gen_ss_advance() singlestep support to
6576 * M profile this should count as an "instruction execution complete"
6577 * event (compare gen_bx_excret_final_code()).
6578 */
6579 env->regs[15] = dest & ~1;
6580 env->thumb = dest & 1;
6581 HELPER(exception_internal)(env, EXCP_EXCEPTION_EXIT);
6582 /* notreached */
6583 }
6584
6585 /* translate.c should have made BXNS UNDEF unless we're secure */
6586 assert(env->v7m.secure);
6587
6588 switch_v7m_security_state(env, dest & 1);
6589 env->thumb = 1;
6590 env->regs[15] = dest & ~1;
6591}
6592
3e3fa230
PM
6593void HELPER(v7m_blxns)(CPUARMState *env, uint32_t dest)
6594{
6595 /* Handle v7M BLXNS:
6596 * - bit 0 of the destination address is the target security state
6597 */
6598
6599 /* At this point regs[15] is the address just after the BLXNS */
6600 uint32_t nextinst = env->regs[15] | 1;
6601 uint32_t sp = env->regs[13] - 8;
6602 uint32_t saved_psr;
6603
6604 /* translate.c will have made BLXNS UNDEF unless we're secure */
6605 assert(env->v7m.secure);
6606
6607 if (dest & 1) {
6608 /* target is Secure, so this is just a normal BLX,
6609 * except that the low bit doesn't indicate Thumb/not.
6610 */
6611 env->regs[14] = nextinst;
6612 env->thumb = 1;
6613 env->regs[15] = dest & ~1;
6614 return;
6615 }
6616
6617 /* Target is non-secure: first push a stack frame */
6618 if (!QEMU_IS_ALIGNED(sp, 8)) {
6619 qemu_log_mask(LOG_GUEST_ERROR,
6620 "BLXNS with misaligned SP is UNPREDICTABLE\n");
6621 }
6622
6623 saved_psr = env->v7m.exception;
6624 if (env->v7m.control[M_REG_S] & R_V7M_CONTROL_SFPA_MASK) {
6625 saved_psr |= XPSR_SFPA;
6626 }
6627
6628 /* Note that these stores can throw exceptions on MPU faults */
6629 cpu_stl_data(env, sp, nextinst);
6630 cpu_stl_data(env, sp + 4, saved_psr);
6631
6632 env->regs[13] = sp;
6633 env->regs[14] = 0xfeffffff;
6634 if (arm_v7m_is_handler_mode(env)) {
6635 /* Write a dummy value to IPSR, to avoid leaking the current secure
6636 * exception number to non-secure code. This is guaranteed not
6637 * to cause write_v7m_exception() to actually change stacks.
6638 */
6639 write_v7m_exception(env, 1);
6640 }
6641 switch_v7m_security_state(env, 0);
6642 env->thumb = 1;
6643 env->regs[15] = dest;
6644}
6645
5b522399
PM
6646static uint32_t *get_v7m_sp_ptr(CPUARMState *env, bool secure, bool threadmode,
6647 bool spsel)
6648{
6649 /* Return a pointer to the location where we currently store the
6650 * stack pointer for the requested security state and thread mode.
6651 * This pointer will become invalid if the CPU state is updated
6652 * such that the stack pointers are switched around (eg changing
6653 * the SPSEL control bit).
6654 * Compare the v8M ARM ARM pseudocode LookUpSP_with_security_mode().
6655 * Unlike that pseudocode, we require the caller to pass us in the
6656 * SPSEL control bit value; this is because we also use this
6657 * function in handling of pushing of the callee-saves registers
6658 * part of the v8M stack frame (pseudocode PushCalleeStack()),
6659 * and in the tailchain codepath the SPSEL bit comes from the exception
6660 * return magic LR value from the previous exception. The pseudocode
6661 * opencodes the stack-selection in PushCalleeStack(), but we prefer
6662 * to make this utility function generic enough to do the job.
6663 */
6664 bool want_psp = threadmode && spsel;
6665
6666 if (secure == env->v7m.secure) {
de2db7ec
PM
6667 if (want_psp == v7m_using_psp(env)) {
6668 return &env->regs[13];
6669 } else {
6670 return &env->v7m.other_sp;
6671 }
5b522399
PM
6672 } else {
6673 if (want_psp) {
6674 return &env->v7m.other_ss_psp;
6675 } else {
6676 return &env->v7m.other_ss_msp;
6677 }
6678 }
6679}
6680
600c33f2
PM
6681static bool arm_v7m_load_vector(ARMCPU *cpu, int exc, bool targets_secure,
6682 uint32_t *pvec)
39ae2474
PM
6683{
6684 CPUState *cs = CPU(cpu);
6685 CPUARMState *env = &cpu->env;
6686 MemTxResult result;
600c33f2
PM
6687 uint32_t addr = env->v7m.vecbase[targets_secure] + exc * 4;
6688 uint32_t vector_entry;
6689 MemTxAttrs attrs = {};
6690 ARMMMUIdx mmu_idx;
6691 bool exc_secure;
6692
6693 mmu_idx = arm_v7m_mmu_idx_for_secstate_and_priv(env, targets_secure, true);
39ae2474 6694
600c33f2
PM
6695 /* We don't do a get_phys_addr() here because the rules for vector
6696 * loads are special: they always use the default memory map, and
6697 * the default memory map permits reads from all addresses.
6698 * Since there's no easy way to pass through to pmsav8_mpu_lookup()
6699 * that we want this special case which would always say "yes",
6700 * we just do the SAU lookup here followed by a direct physical load.
6701 */
6702 attrs.secure = targets_secure;
6703 attrs.user = false;
6704
6705 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
6706 V8M_SAttributes sattrs = {};
6707
6708 v8m_security_lookup(env, addr, MMU_DATA_LOAD, mmu_idx, &sattrs);
6709 if (sattrs.ns) {
6710 attrs.secure = false;
6711 } else if (!targets_secure) {
6712 /* NS access to S memory */
6713 goto load_fail;
6714 }
6715 }
6716
6717 vector_entry = address_space_ldl(arm_addressspace(cs, attrs), addr,
6718 attrs, &result);
39ae2474 6719 if (result != MEMTX_OK) {
600c33f2 6720 goto load_fail;
39ae2474 6721 }
600c33f2
PM
6722 *pvec = vector_entry;
6723 return true;
6724
6725load_fail:
6726 /* All vector table fetch fails are reported as HardFault, with
6727 * HFSR.VECTTBL and .FORCED set. (FORCED is set because
6728 * technically the underlying exception is a MemManage or BusFault
6729 * that is escalated to HardFault.) This is a terminal exception,
6730 * so we will either take the HardFault immediately or else enter
6731 * lockup (the latter case is handled in armv7m_nvic_set_pending_derived()).
6732 */
6733 exc_secure = targets_secure ||
6734 !(cpu->env.v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK);
6735 env->v7m.hfsr |= R_V7M_HFSR_VECTTBL_MASK | R_V7M_HFSR_FORCED_MASK;
6736 armv7m_nvic_set_pending_derived(env->nvic, ARMV7M_EXCP_HARD, exc_secure);
6737 return false;
39ae2474
PM
6738}
6739
65b4234f 6740static bool v7m_push_callee_stack(ARMCPU *cpu, uint32_t lr, bool dotailchain,
0094ca70 6741 bool ignore_faults)
d3392718
PM
6742{
6743 /* For v8M, push the callee-saves register part of the stack frame.
6744 * Compare the v8M pseudocode PushCalleeStack().
6745 * In the tailchaining case this may not be the current stack.
6746 */
6747 CPUARMState *env = &cpu->env;
d3392718
PM
6748 uint32_t *frame_sp_p;
6749 uint32_t frameptr;
65b4234f
PM
6750 ARMMMUIdx mmu_idx;
6751 bool stacked_ok;
d3392718
PM
6752
6753 if (dotailchain) {
65b4234f
PM
6754 bool mode = lr & R_V7M_EXCRET_MODE_MASK;
6755 bool priv = !(env->v7m.control[M_REG_S] & R_V7M_CONTROL_NPRIV_MASK) ||
6756 !mode;
6757
6758 mmu_idx = arm_v7m_mmu_idx_for_secstate_and_priv(env, M_REG_S, priv);
6759 frame_sp_p = get_v7m_sp_ptr(env, M_REG_S, mode,
d3392718
PM
6760 lr & R_V7M_EXCRET_SPSEL_MASK);
6761 } else {
65b4234f 6762 mmu_idx = core_to_arm_mmu_idx(env, cpu_mmu_index(env, false));
d3392718
PM
6763 frame_sp_p = &env->regs[13];
6764 }
6765
6766 frameptr = *frame_sp_p - 0x28;
6767
65b4234f
PM
6768 /* Write as much of the stack frame as we can. A write failure may
6769 * cause us to pend a derived exception.
6770 */
6771 stacked_ok =
6772 v7m_stack_write(cpu, frameptr, 0xfefa125b, mmu_idx, ignore_faults) &&
6773 v7m_stack_write(cpu, frameptr + 0x8, env->regs[4], mmu_idx,
6774 ignore_faults) &&
6775 v7m_stack_write(cpu, frameptr + 0xc, env->regs[5], mmu_idx,
6776 ignore_faults) &&
6777 v7m_stack_write(cpu, frameptr + 0x10, env->regs[6], mmu_idx,
6778 ignore_faults) &&
6779 v7m_stack_write(cpu, frameptr + 0x14, env->regs[7], mmu_idx,
6780 ignore_faults) &&
6781 v7m_stack_write(cpu, frameptr + 0x18, env->regs[8], mmu_idx,
6782 ignore_faults) &&
6783 v7m_stack_write(cpu, frameptr + 0x1c, env->regs[9], mmu_idx,
6784 ignore_faults) &&
6785 v7m_stack_write(cpu, frameptr + 0x20, env->regs[10], mmu_idx,
6786 ignore_faults) &&
6787 v7m_stack_write(cpu, frameptr + 0x24, env->regs[11], mmu_idx,
6788 ignore_faults);
6789
6790 /* Update SP regardless of whether any of the stack accesses failed.
6791 * When we implement v8M stack limit checking then this attempt to
6792 * update SP might also fail and result in a derived exception.
6793 */
d3392718 6794 *frame_sp_p = frameptr;
65b4234f
PM
6795
6796 return !stacked_ok;
d3392718
PM
6797}
6798
0094ca70
PM
6799static void v7m_exception_taken(ARMCPU *cpu, uint32_t lr, bool dotailchain,
6800 bool ignore_stackfaults)
39ae2474
PM
6801{
6802 /* Do the "take the exception" parts of exception entry,
6803 * but not the pushing of state to the stack. This is
6804 * similar to the pseudocode ExceptionTaken() function.
6805 */
6806 CPUARMState *env = &cpu->env;
6807 uint32_t addr;
d3392718 6808 bool targets_secure;
6c948518 6809 int exc;
65b4234f 6810 bool push_failed = false;
d3392718 6811
6c948518 6812 armv7m_nvic_get_pending_irq_info(env->nvic, &exc, &targets_secure);
d3392718
PM
6813
6814 if (arm_feature(env, ARM_FEATURE_V8)) {
6815 if (arm_feature(env, ARM_FEATURE_M_SECURITY) &&
6816 (lr & R_V7M_EXCRET_S_MASK)) {
6817 /* The background code (the owner of the registers in the
6818 * exception frame) is Secure. This means it may either already
6819 * have or now needs to push callee-saves registers.
6820 */
6821 if (targets_secure) {
6822 if (dotailchain && !(lr & R_V7M_EXCRET_ES_MASK)) {
6823 /* We took an exception from Secure to NonSecure
6824 * (which means the callee-saved registers got stacked)
6825 * and are now tailchaining to a Secure exception.
6826 * Clear DCRS so eventual return from this Secure
6827 * exception unstacks the callee-saved registers.
6828 */
6829 lr &= ~R_V7M_EXCRET_DCRS_MASK;
6830 }
6831 } else {
6832 /* We're going to a non-secure exception; push the
6833 * callee-saves registers to the stack now, if they're
6834 * not already saved.
6835 */
6836 if (lr & R_V7M_EXCRET_DCRS_MASK &&
6837 !(dotailchain && (lr & R_V7M_EXCRET_ES_MASK))) {
65b4234f
PM
6838 push_failed = v7m_push_callee_stack(cpu, lr, dotailchain,
6839 ignore_stackfaults);
d3392718
PM
6840 }
6841 lr |= R_V7M_EXCRET_DCRS_MASK;
6842 }
6843 }
6844
6845 lr &= ~R_V7M_EXCRET_ES_MASK;
6846 if (targets_secure || !arm_feature(env, ARM_FEATURE_M_SECURITY)) {
6847 lr |= R_V7M_EXCRET_ES_MASK;
6848 }
6849 lr &= ~R_V7M_EXCRET_SPSEL_MASK;
6850 if (env->v7m.control[targets_secure] & R_V7M_CONTROL_SPSEL_MASK) {
6851 lr |= R_V7M_EXCRET_SPSEL_MASK;
6852 }
6853
6854 /* Clear registers if necessary to prevent non-secure exception
6855 * code being able to see register values from secure code.
6856 * Where register values become architecturally UNKNOWN we leave
6857 * them with their previous values.
6858 */
6859 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
6860 if (!targets_secure) {
6861 /* Always clear the caller-saved registers (they have been
6862 * pushed to the stack earlier in v7m_push_stack()).
6863 * Clear callee-saved registers if the background code is
6864 * Secure (in which case these regs were saved in
6865 * v7m_push_callee_stack()).
6866 */
6867 int i;
6868
6869 for (i = 0; i < 13; i++) {
6870 /* r4..r11 are callee-saves, zero only if EXCRET.S == 1 */
6871 if (i < 4 || i > 11 || (lr & R_V7M_EXCRET_S_MASK)) {
6872 env->regs[i] = 0;
6873 }
6874 }
6875 /* Clear EAPSR */
6876 xpsr_write(env, 0, XPSR_NZCV | XPSR_Q | XPSR_GE | XPSR_IT);
6877 }
6878 }
6879 }
39ae2474 6880
65b4234f
PM
6881 if (push_failed && !ignore_stackfaults) {
6882 /* Derived exception on callee-saves register stacking:
6883 * we might now want to take a different exception which
6884 * targets a different security state, so try again from the top.
6885 */
6886 v7m_exception_taken(cpu, lr, true, true);
6887 return;
6888 }
6889
600c33f2
PM
6890 if (!arm_v7m_load_vector(cpu, exc, targets_secure, &addr)) {
6891 /* Vector load failed: derived exception */
6892 v7m_exception_taken(cpu, lr, true, true);
6893 return;
6894 }
6c948518
PM
6895
6896 /* Now we've done everything that might cause a derived exception
6897 * we can go ahead and activate whichever exception we're going to
6898 * take (which might now be the derived exception).
6899 */
6900 armv7m_nvic_acknowledge_irq(env->nvic);
6901
d3392718
PM
6902 /* Switch to target security state -- must do this before writing SPSEL */
6903 switch_v7m_security_state(env, targets_secure);
de2db7ec 6904 write_v7m_control_spsel(env, 0);
dc3c4c14 6905 arm_clear_exclusive(env);
39ae2474
PM
6906 /* Clear IT bits */
6907 env->condexec_bits = 0;
6908 env->regs[14] = lr;
39ae2474
PM
6909 env->regs[15] = addr & 0xfffffffe;
6910 env->thumb = addr & 1;
6911}
6912
0094ca70 6913static bool v7m_push_stack(ARMCPU *cpu)
39ae2474
PM
6914{
6915 /* Do the "set up stack frame" part of exception entry,
6916 * similar to pseudocode PushStack().
0094ca70
PM
6917 * Return true if we generate a derived exception (and so
6918 * should ignore further stack faults trying to process
6919 * that derived exception.)
39ae2474 6920 */
fd592d89 6921 bool stacked_ok;
39ae2474
PM
6922 CPUARMState *env = &cpu->env;
6923 uint32_t xpsr = xpsr_read(env);
fd592d89
PM
6924 uint32_t frameptr = env->regs[13];
6925 ARMMMUIdx mmu_idx = core_to_arm_mmu_idx(env, cpu_mmu_index(env, false));
39ae2474
PM
6926
6927 /* Align stack pointer if the guest wants that */
fd592d89 6928 if ((frameptr & 4) &&
9d40cd8a 6929 (env->v7m.ccr[env->v7m.secure] & R_V7M_CCR_STKALIGN_MASK)) {
fd592d89 6930 frameptr -= 4;
987ab45e 6931 xpsr |= XPSR_SPREALIGN;
39ae2474 6932 }
0094ca70 6933
fd592d89
PM
6934 frameptr -= 0x20;
6935
6936 /* Write as much of the stack frame as we can. If we fail a stack
6937 * write this will result in a derived exception being pended
6938 * (which may be taken in preference to the one we started with
6939 * if it has higher priority).
6940 */
6941 stacked_ok =
6942 v7m_stack_write(cpu, frameptr, env->regs[0], mmu_idx, false) &&
6943 v7m_stack_write(cpu, frameptr + 4, env->regs[1], mmu_idx, false) &&
6944 v7m_stack_write(cpu, frameptr + 8, env->regs[2], mmu_idx, false) &&
6945 v7m_stack_write(cpu, frameptr + 12, env->regs[3], mmu_idx, false) &&
6946 v7m_stack_write(cpu, frameptr + 16, env->regs[12], mmu_idx, false) &&
6947 v7m_stack_write(cpu, frameptr + 20, env->regs[14], mmu_idx, false) &&
6948 v7m_stack_write(cpu, frameptr + 24, env->regs[15], mmu_idx, false) &&
6949 v7m_stack_write(cpu, frameptr + 28, xpsr, mmu_idx, false);
6950
6951 /* Update SP regardless of whether any of the stack accesses failed.
6952 * When we implement v8M stack limit checking then this attempt to
6953 * update SP might also fail and result in a derived exception.
6954 */
6955 env->regs[13] = frameptr;
6956
6957 return !stacked_ok;
39ae2474
PM
6958}
6959
aa488fe3 6960static void do_v7m_exception_exit(ARMCPU *cpu)
9ee6e8bb 6961{
aa488fe3 6962 CPUARMState *env = &cpu->env;
351e527a 6963 uint32_t excret;
9ee6e8bb 6964 uint32_t xpsr;
aa488fe3 6965 bool ufault = false;
bfb2eb52
PM
6966 bool sfault = false;
6967 bool return_to_sp_process;
6968 bool return_to_handler;
aa488fe3 6969 bool rettobase = false;
5cb18069 6970 bool exc_secure = false;
5b522399 6971 bool return_to_secure;
aa488fe3 6972
d02a8698
PM
6973 /* If we're not in Handler mode then jumps to magic exception-exit
6974 * addresses don't have magic behaviour. However for the v8M
6975 * security extensions the magic secure-function-return has to
6976 * work in thread mode too, so to avoid doing an extra check in
6977 * the generated code we allow exception-exit magic to also cause the
6978 * internal exception and bring us here in thread mode. Correct code
6979 * will never try to do this (the following insn fetch will always
6980 * fault) so we the overhead of having taken an unnecessary exception
6981 * doesn't matter.
aa488fe3 6982 */
d02a8698
PM
6983 if (!arm_v7m_is_handler_mode(env)) {
6984 return;
6985 }
aa488fe3
PM
6986
6987 /* In the spec pseudocode ExceptionReturn() is called directly
6988 * from BXWritePC() and gets the full target PC value including
6989 * bit zero. In QEMU's implementation we treat it as a normal
6990 * jump-to-register (which is then caught later on), and so split
6991 * the target value up between env->regs[15] and env->thumb in
6992 * gen_bx(). Reconstitute it.
6993 */
351e527a 6994 excret = env->regs[15];
aa488fe3 6995 if (env->thumb) {
351e527a 6996 excret |= 1;
aa488fe3
PM
6997 }
6998
6999 qemu_log_mask(CPU_LOG_INT, "Exception return: magic PC %" PRIx32
7000 " previous exception %d\n",
351e527a 7001 excret, env->v7m.exception);
aa488fe3 7002
351e527a 7003 if ((excret & R_V7M_EXCRET_RES1_MASK) != R_V7M_EXCRET_RES1_MASK) {
aa488fe3 7004 qemu_log_mask(LOG_GUEST_ERROR, "M profile: zero high bits in exception "
351e527a
PM
7005 "exit PC value 0x%" PRIx32 " are UNPREDICTABLE\n",
7006 excret);
aa488fe3
PM
7007 }
7008
bfb2eb52
PM
7009 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
7010 /* EXC_RETURN.ES validation check (R_SMFL). We must do this before
7011 * we pick which FAULTMASK to clear.
7012 */
7013 if (!env->v7m.secure &&
7014 ((excret & R_V7M_EXCRET_ES_MASK) ||
7015 !(excret & R_V7M_EXCRET_DCRS_MASK))) {
7016 sfault = 1;
7017 /* For all other purposes, treat ES as 0 (R_HXSR) */
7018 excret &= ~R_V7M_EXCRET_ES_MASK;
7019 }
7020 }
7021
a20ee600 7022 if (env->v7m.exception != ARMV7M_EXCP_NMI) {
42a6686b
PM
7023 /* Auto-clear FAULTMASK on return from other than NMI.
7024 * If the security extension is implemented then this only
7025 * happens if the raw execution priority is >= 0; the
7026 * value of the ES bit in the exception return value indicates
7027 * which security state's faultmask to clear. (v8M ARM ARM R_KBNF.)
7028 */
7029 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
5cb18069 7030 exc_secure = excret & R_V7M_EXCRET_ES_MASK;
42a6686b 7031 if (armv7m_nvic_raw_execution_priority(env->nvic) >= 0) {
5cb18069 7032 env->v7m.faultmask[exc_secure] = 0;
42a6686b
PM
7033 }
7034 } else {
7035 env->v7m.faultmask[M_REG_NS] = 0;
7036 }
a20ee600 7037 }
aa488fe3 7038
5cb18069
PM
7039 switch (armv7m_nvic_complete_irq(env->nvic, env->v7m.exception,
7040 exc_secure)) {
aa488fe3
PM
7041 case -1:
7042 /* attempt to exit an exception that isn't active */
7043 ufault = true;
7044 break;
7045 case 0:
7046 /* still an irq active now */
7047 break;
7048 case 1:
7049 /* we returned to base exception level, no nesting.
7050 * (In the pseudocode this is written using "NestedActivation != 1"
7051 * where we have 'rettobase == false'.)
7052 */
7053 rettobase = true;
7054 break;
7055 default:
7056 g_assert_not_reached();
7057 }
7058
bfb2eb52
PM
7059 return_to_handler = !(excret & R_V7M_EXCRET_MODE_MASK);
7060 return_to_sp_process = excret & R_V7M_EXCRET_SPSEL_MASK;
5b522399
PM
7061 return_to_secure = arm_feature(env, ARM_FEATURE_M_SECURITY) &&
7062 (excret & R_V7M_EXCRET_S_MASK);
7063
bfb2eb52
PM
7064 if (arm_feature(env, ARM_FEATURE_V8)) {
7065 if (!arm_feature(env, ARM_FEATURE_M_SECURITY)) {
7066 /* UNPREDICTABLE if S == 1 or DCRS == 0 or ES == 1 (R_XLCP);
7067 * we choose to take the UsageFault.
7068 */
7069 if ((excret & R_V7M_EXCRET_S_MASK) ||
7070 (excret & R_V7M_EXCRET_ES_MASK) ||
7071 !(excret & R_V7M_EXCRET_DCRS_MASK)) {
7072 ufault = true;
7073 }
7074 }
7075 if (excret & R_V7M_EXCRET_RES0_MASK) {
aa488fe3
PM
7076 ufault = true;
7077 }
bfb2eb52
PM
7078 } else {
7079 /* For v7M we only recognize certain combinations of the low bits */
7080 switch (excret & 0xf) {
7081 case 1: /* Return to Handler */
7082 break;
7083 case 13: /* Return to Thread using Process stack */
7084 case 9: /* Return to Thread using Main stack */
7085 /* We only need to check NONBASETHRDENA for v7M, because in
7086 * v8M this bit does not exist (it is RES1).
7087 */
7088 if (!rettobase &&
7089 !(env->v7m.ccr[env->v7m.secure] &
7090 R_V7M_CCR_NONBASETHRDENA_MASK)) {
7091 ufault = true;
7092 }
7093 break;
7094 default:
7095 ufault = true;
7096 }
7097 }
7098
7099 if (sfault) {
7100 env->v7m.sfsr |= R_V7M_SFSR_INVER_MASK;
7101 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SECURE, false);
0094ca70 7102 v7m_exception_taken(cpu, excret, true, false);
bfb2eb52
PM
7103 qemu_log_mask(CPU_LOG_INT, "...taking SecureFault on existing "
7104 "stackframe: failed EXC_RETURN.ES validity check\n");
7105 return;
aa488fe3
PM
7106 }
7107
7108 if (ufault) {
7109 /* Bad exception return: instead of popping the exception
7110 * stack, directly take a usage fault on the current stack.
7111 */
334e8dad 7112 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_INVPC_MASK;
2fb50a33 7113 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, env->v7m.secure);
0094ca70 7114 v7m_exception_taken(cpu, excret, true, false);
aa488fe3
PM
7115 qemu_log_mask(CPU_LOG_INT, "...taking UsageFault on existing "
7116 "stackframe: failed exception return integrity check\n");
7117 return;
a20ee600 7118 }
9ee6e8bb 7119
de2db7ec
PM
7120 /* Set CONTROL.SPSEL from excret.SPSEL. Since we're still in
7121 * Handler mode (and will be until we write the new XPSR.Interrupt
7122 * field) this does not switch around the current stack pointer.
5b522399 7123 */
3f0cddee 7124 write_v7m_control_spsel_for_secstate(env, return_to_sp_process, exc_secure);
5b522399 7125
3919e60b
PM
7126 switch_v7m_security_state(env, return_to_secure);
7127
5b522399
PM
7128 {
7129 /* The stack pointer we should be reading the exception frame from
7130 * depends on bits in the magic exception return type value (and
7131 * for v8M isn't necessarily the stack pointer we will eventually
7132 * end up resuming execution with). Get a pointer to the location
7133 * in the CPU state struct where the SP we need is currently being
7134 * stored; we will use and modify it in place.
7135 * We use this limited C variable scope so we don't accidentally
7136 * use 'frame_sp_p' after we do something that makes it invalid.
fcf83ab1 7137 */
5b522399
PM
7138 uint32_t *frame_sp_p = get_v7m_sp_ptr(env,
7139 return_to_secure,
7140 !return_to_handler,
7141 return_to_sp_process);
7142 uint32_t frameptr = *frame_sp_p;
95695eff
PM
7143 bool pop_ok = true;
7144 ARMMMUIdx mmu_idx;
7145
7146 mmu_idx = arm_v7m_mmu_idx_for_secstate_and_priv(env, return_to_secure,
7147 !return_to_handler);
5b522399 7148
cb484f9a
PM
7149 if (!QEMU_IS_ALIGNED(frameptr, 8) &&
7150 arm_feature(env, ARM_FEATURE_V8)) {
7151 qemu_log_mask(LOG_GUEST_ERROR,
7152 "M profile exception return with non-8-aligned SP "
7153 "for destination state is UNPREDICTABLE\n");
7154 }
7155
907bedb3
PM
7156 /* Do we need to pop callee-saved registers? */
7157 if (return_to_secure &&
7158 ((excret & R_V7M_EXCRET_ES_MASK) == 0 ||
7159 (excret & R_V7M_EXCRET_DCRS_MASK) == 0)) {
7160 uint32_t expected_sig = 0xfefa125b;
4818bad9
PM
7161 uint32_t actual_sig;
7162
7163 pop_ok = v7m_stack_read(cpu, &actual_sig, frameptr, mmu_idx);
907bedb3 7164
4818bad9 7165 if (pop_ok && expected_sig != actual_sig) {
907bedb3
PM
7166 /* Take a SecureFault on the current stack */
7167 env->v7m.sfsr |= R_V7M_SFSR_INVIS_MASK;
7168 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SECURE, false);
0094ca70 7169 v7m_exception_taken(cpu, excret, true, false);
907bedb3
PM
7170 qemu_log_mask(CPU_LOG_INT, "...taking SecureFault on existing "
7171 "stackframe: failed exception return integrity "
7172 "signature check\n");
7173 return;
7174 }
7175
4818bad9 7176 pop_ok = pop_ok &&
95695eff
PM
7177 v7m_stack_read(cpu, &env->regs[4], frameptr + 0x8, mmu_idx) &&
7178 v7m_stack_read(cpu, &env->regs[4], frameptr + 0x8, mmu_idx) &&
7179 v7m_stack_read(cpu, &env->regs[5], frameptr + 0xc, mmu_idx) &&
7180 v7m_stack_read(cpu, &env->regs[6], frameptr + 0x10, mmu_idx) &&
7181 v7m_stack_read(cpu, &env->regs[7], frameptr + 0x14, mmu_idx) &&
7182 v7m_stack_read(cpu, &env->regs[8], frameptr + 0x18, mmu_idx) &&
7183 v7m_stack_read(cpu, &env->regs[9], frameptr + 0x1c, mmu_idx) &&
7184 v7m_stack_read(cpu, &env->regs[10], frameptr + 0x20, mmu_idx) &&
7185 v7m_stack_read(cpu, &env->regs[11], frameptr + 0x24, mmu_idx);
907bedb3
PM
7186
7187 frameptr += 0x28;
7188 }
7189
95695eff
PM
7190 /* Pop registers */
7191 pop_ok = pop_ok &&
7192 v7m_stack_read(cpu, &env->regs[0], frameptr, mmu_idx) &&
7193 v7m_stack_read(cpu, &env->regs[1], frameptr + 0x4, mmu_idx) &&
7194 v7m_stack_read(cpu, &env->regs[2], frameptr + 0x8, mmu_idx) &&
7195 v7m_stack_read(cpu, &env->regs[3], frameptr + 0xc, mmu_idx) &&
7196 v7m_stack_read(cpu, &env->regs[12], frameptr + 0x10, mmu_idx) &&
7197 v7m_stack_read(cpu, &env->regs[14], frameptr + 0x14, mmu_idx) &&
7198 v7m_stack_read(cpu, &env->regs[15], frameptr + 0x18, mmu_idx) &&
7199 v7m_stack_read(cpu, &xpsr, frameptr + 0x1c, mmu_idx);
7200
7201 if (!pop_ok) {
7202 /* v7m_stack_read() pended a fault, so take it (as a tail
7203 * chained exception on the same stack frame)
7204 */
7205 v7m_exception_taken(cpu, excret, true, false);
7206 return;
7207 }
4e4259d3
PM
7208
7209 /* Returning from an exception with a PC with bit 0 set is defined
7210 * behaviour on v8M (bit 0 is ignored), but for v7M it was specified
7211 * to be UNPREDICTABLE. In practice actual v7M hardware seems to ignore
7212 * the lsbit, and there are several RTOSes out there which incorrectly
7213 * assume the r15 in the stack frame should be a Thumb-style "lsbit
7214 * indicates ARM/Thumb" value, so ignore the bit on v7M as well, but
7215 * complain about the badly behaved guest.
7216 */
5b522399 7217 if (env->regs[15] & 1) {
5b522399 7218 env->regs[15] &= ~1U;
4e4259d3
PM
7219 if (!arm_feature(env, ARM_FEATURE_V8)) {
7220 qemu_log_mask(LOG_GUEST_ERROR,
7221 "M profile return from interrupt with misaligned "
7222 "PC is UNPREDICTABLE on v7M\n");
7223 }
5b522399 7224 }
4e4259d3 7225
224e0c30
PM
7226 if (arm_feature(env, ARM_FEATURE_V8)) {
7227 /* For v8M we have to check whether the xPSR exception field
7228 * matches the EXCRET value for return to handler/thread
7229 * before we commit to changing the SP and xPSR.
7230 */
7231 bool will_be_handler = (xpsr & XPSR_EXCP) != 0;
7232 if (return_to_handler != will_be_handler) {
7233 /* Take an INVPC UsageFault on the current stack.
7234 * By this point we will have switched to the security state
7235 * for the background state, so this UsageFault will target
7236 * that state.
7237 */
7238 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE,
7239 env->v7m.secure);
7240 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_INVPC_MASK;
0094ca70 7241 v7m_exception_taken(cpu, excret, true, false);
224e0c30
PM
7242 qemu_log_mask(CPU_LOG_INT, "...taking UsageFault on existing "
7243 "stackframe: failed exception return integrity "
7244 "check\n");
7245 return;
7246 }
7247 }
7248
5b522399
PM
7249 /* Commit to consuming the stack frame */
7250 frameptr += 0x20;
7251 /* Undo stack alignment (the SPREALIGN bit indicates that the original
7252 * pre-exception SP was not 8-aligned and we added a padding word to
7253 * align it, so we undo this by ORing in the bit that increases it
7254 * from the current 8-aligned value to the 8-unaligned value. (Adding 4
7255 * would work too but a logical OR is how the pseudocode specifies it.)
7256 */
7257 if (xpsr & XPSR_SPREALIGN) {
7258 frameptr |= 4;
7259 }
7260 *frame_sp_p = frameptr;
fcf83ab1 7261 }
5b522399 7262 /* This xpsr_write() will invalidate frame_sp_p as it may switch stack */
987ab45e 7263 xpsr_write(env, xpsr, ~XPSR_SPREALIGN);
aa488fe3
PM
7264
7265 /* The restored xPSR exception field will be zero if we're
7266 * resuming in Thread mode. If that doesn't match what the
351e527a 7267 * exception return excret specified then this is a UsageFault.
224e0c30 7268 * v7M requires we make this check here; v8M did it earlier.
aa488fe3 7269 */
15b3f556 7270 if (return_to_handler != arm_v7m_is_handler_mode(env)) {
224e0c30
PM
7271 /* Take an INVPC UsageFault by pushing the stack again;
7272 * we know we're v7M so this is never a Secure UsageFault.
2fb50a33 7273 */
0094ca70
PM
7274 bool ignore_stackfaults;
7275
224e0c30 7276 assert(!arm_feature(env, ARM_FEATURE_V8));
2fb50a33 7277 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, false);
334e8dad 7278 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_INVPC_MASK;
0094ca70
PM
7279 ignore_stackfaults = v7m_push_stack(cpu);
7280 v7m_exception_taken(cpu, excret, false, ignore_stackfaults);
aa488fe3
PM
7281 qemu_log_mask(CPU_LOG_INT, "...taking UsageFault on new stackframe: "
7282 "failed exception return integrity check\n");
7283 return;
7284 }
7285
7286 /* Otherwise, we have a successful exception exit. */
dc3c4c14 7287 arm_clear_exclusive(env);
aa488fe3 7288 qemu_log_mask(CPU_LOG_INT, "...successful exception return\n");
9ee6e8bb
PB
7289}
7290
d02a8698
PM
7291static bool do_v7m_function_return(ARMCPU *cpu)
7292{
7293 /* v8M security extensions magic function return.
7294 * We may either:
7295 * (1) throw an exception (longjump)
7296 * (2) return true if we successfully handled the function return
7297 * (3) return false if we failed a consistency check and have
7298 * pended a UsageFault that needs to be taken now
7299 *
7300 * At this point the magic return value is split between env->regs[15]
7301 * and env->thumb. We don't bother to reconstitute it because we don't
7302 * need it (all values are handled the same way).
7303 */
7304 CPUARMState *env = &cpu->env;
7305 uint32_t newpc, newpsr, newpsr_exc;
7306
7307 qemu_log_mask(CPU_LOG_INT, "...really v7M secure function return\n");
7308
7309 {
7310 bool threadmode, spsel;
7311 TCGMemOpIdx oi;
7312 ARMMMUIdx mmu_idx;
7313 uint32_t *frame_sp_p;
7314 uint32_t frameptr;
7315
7316 /* Pull the return address and IPSR from the Secure stack */
7317 threadmode = !arm_v7m_is_handler_mode(env);
7318 spsel = env->v7m.control[M_REG_S] & R_V7M_CONTROL_SPSEL_MASK;
7319
7320 frame_sp_p = get_v7m_sp_ptr(env, true, threadmode, spsel);
7321 frameptr = *frame_sp_p;
7322
7323 /* These loads may throw an exception (for MPU faults). We want to
7324 * do them as secure, so work out what MMU index that is.
7325 */
7326 mmu_idx = arm_v7m_mmu_idx_for_secstate(env, true);
7327 oi = make_memop_idx(MO_LE, arm_to_core_mmu_idx(mmu_idx));
7328 newpc = helper_le_ldul_mmu(env, frameptr, oi, 0);
7329 newpsr = helper_le_ldul_mmu(env, frameptr + 4, oi, 0);
7330
7331 /* Consistency checks on new IPSR */
7332 newpsr_exc = newpsr & XPSR_EXCP;
7333 if (!((env->v7m.exception == 0 && newpsr_exc == 0) ||
7334 (env->v7m.exception == 1 && newpsr_exc != 0))) {
7335 /* Pend the fault and tell our caller to take it */
7336 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_INVPC_MASK;
7337 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE,
7338 env->v7m.secure);
7339 qemu_log_mask(CPU_LOG_INT,
7340 "...taking INVPC UsageFault: "
7341 "IPSR consistency check failed\n");
7342 return false;
7343 }
7344
7345 *frame_sp_p = frameptr + 8;
7346 }
7347
7348 /* This invalidates frame_sp_p */
7349 switch_v7m_security_state(env, true);
7350 env->v7m.exception = newpsr_exc;
7351 env->v7m.control[M_REG_S] &= ~R_V7M_CONTROL_SFPA_MASK;
7352 if (newpsr & XPSR_SFPA) {
7353 env->v7m.control[M_REG_S] |= R_V7M_CONTROL_SFPA_MASK;
7354 }
7355 xpsr_write(env, 0, XPSR_IT);
7356 env->thumb = newpc & 1;
7357 env->regs[15] = newpc & ~1;
7358
7359 qemu_log_mask(CPU_LOG_INT, "...function return successful\n");
7360 return true;
7361}
7362
27a7ea8a
PB
7363static void arm_log_exception(int idx)
7364{
7365 if (qemu_loglevel_mask(CPU_LOG_INT)) {
7366 const char *exc = NULL;
2c4a7cc5
PM
7367 static const char * const excnames[] = {
7368 [EXCP_UDEF] = "Undefined Instruction",
7369 [EXCP_SWI] = "SVC",
7370 [EXCP_PREFETCH_ABORT] = "Prefetch Abort",
7371 [EXCP_DATA_ABORT] = "Data Abort",
7372 [EXCP_IRQ] = "IRQ",
7373 [EXCP_FIQ] = "FIQ",
7374 [EXCP_BKPT] = "Breakpoint",
7375 [EXCP_EXCEPTION_EXIT] = "QEMU v7M exception exit",
7376 [EXCP_KERNEL_TRAP] = "QEMU intercept of kernel commpage",
7377 [EXCP_HVC] = "Hypervisor Call",
7378 [EXCP_HYP_TRAP] = "Hypervisor Trap",
7379 [EXCP_SMC] = "Secure Monitor Call",
7380 [EXCP_VIRQ] = "Virtual IRQ",
7381 [EXCP_VFIQ] = "Virtual FIQ",
7382 [EXCP_SEMIHOST] = "Semihosting call",
7383 [EXCP_NOCP] = "v7M NOCP UsageFault",
7384 [EXCP_INVSTATE] = "v7M INVSTATE UsageFault",
7385 };
27a7ea8a
PB
7386
7387 if (idx >= 0 && idx < ARRAY_SIZE(excnames)) {
7388 exc = excnames[idx];
7389 }
7390 if (!exc) {
7391 exc = "unknown";
7392 }
7393 qemu_log_mask(CPU_LOG_INT, "Taking exception %d [%s]\n", idx, exc);
7394 }
7395}
7396
333e10c5
PM
7397static bool v7m_read_half_insn(ARMCPU *cpu, ARMMMUIdx mmu_idx,
7398 uint32_t addr, uint16_t *insn)
7399{
7400 /* Load a 16-bit portion of a v7M instruction, returning true on success,
7401 * or false on failure (in which case we will have pended the appropriate
7402 * exception).
7403 * We need to do the instruction fetch's MPU and SAU checks
7404 * like this because there is no MMU index that would allow
7405 * doing the load with a single function call. Instead we must
7406 * first check that the security attributes permit the load
7407 * and that they don't mismatch on the two halves of the instruction,
7408 * and then we do the load as a secure load (ie using the security
7409 * attributes of the address, not the CPU, as architecturally required).
7410 */
7411 CPUState *cs = CPU(cpu);
7412 CPUARMState *env = &cpu->env;
7413 V8M_SAttributes sattrs = {};
7414 MemTxAttrs attrs = {};
7415 ARMMMUFaultInfo fi = {};
7416 MemTxResult txres;
7417 target_ulong page_size;
7418 hwaddr physaddr;
7419 int prot;
333e10c5
PM
7420
7421 v8m_security_lookup(env, addr, MMU_INST_FETCH, mmu_idx, &sattrs);
7422 if (!sattrs.nsc || sattrs.ns) {
7423 /* This must be the second half of the insn, and it straddles a
7424 * region boundary with the second half not being S&NSC.
7425 */
7426 env->v7m.sfsr |= R_V7M_SFSR_INVEP_MASK;
7427 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SECURE, false);
7428 qemu_log_mask(CPU_LOG_INT,
7429 "...really SecureFault with SFSR.INVEP\n");
7430 return false;
7431 }
7432 if (get_phys_addr(env, addr, MMU_INST_FETCH, mmu_idx,
bc52bfeb 7433 &physaddr, &attrs, &prot, &page_size, &fi, NULL)) {
333e10c5
PM
7434 /* the MPU lookup failed */
7435 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_IACCVIOL_MASK;
7436 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_MEM, env->v7m.secure);
7437 qemu_log_mask(CPU_LOG_INT, "...really MemManage with CFSR.IACCVIOL\n");
7438 return false;
7439 }
7440 *insn = address_space_lduw_le(arm_addressspace(cs, attrs), physaddr,
7441 attrs, &txres);
7442 if (txres != MEMTX_OK) {
7443 env->v7m.cfsr[M_REG_NS] |= R_V7M_CFSR_IBUSERR_MASK;
7444 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_BUS, false);
7445 qemu_log_mask(CPU_LOG_INT, "...really BusFault with CFSR.IBUSERR\n");
7446 return false;
7447 }
7448 return true;
7449}
7450
7451static bool v7m_handle_execute_nsc(ARMCPU *cpu)
7452{
7453 /* Check whether this attempt to execute code in a Secure & NS-Callable
7454 * memory region is for an SG instruction; if so, then emulate the
7455 * effect of the SG instruction and return true. Otherwise pend
7456 * the correct kind of exception and return false.
7457 */
7458 CPUARMState *env = &cpu->env;
7459 ARMMMUIdx mmu_idx;
7460 uint16_t insn;
7461
7462 /* We should never get here unless get_phys_addr_pmsav8() caused
7463 * an exception for NS executing in S&NSC memory.
7464 */
7465 assert(!env->v7m.secure);
7466 assert(arm_feature(env, ARM_FEATURE_M_SECURITY));
7467
7468 /* We want to do the MPU lookup as secure; work out what mmu_idx that is */
7469 mmu_idx = arm_v7m_mmu_idx_for_secstate(env, true);
7470
7471 if (!v7m_read_half_insn(cpu, mmu_idx, env->regs[15], &insn)) {
7472 return false;
7473 }
7474
7475 if (!env->thumb) {
7476 goto gen_invep;
7477 }
7478
7479 if (insn != 0xe97f) {
7480 /* Not an SG instruction first half (we choose the IMPDEF
7481 * early-SG-check option).
7482 */
7483 goto gen_invep;
7484 }
7485
7486 if (!v7m_read_half_insn(cpu, mmu_idx, env->regs[15] + 2, &insn)) {
7487 return false;
7488 }
7489
7490 if (insn != 0xe97f) {
7491 /* Not an SG instruction second half (yes, both halves of the SG
7492 * insn have the same hex value)
7493 */
7494 goto gen_invep;
7495 }
7496
7497 /* OK, we have confirmed that we really have an SG instruction.
7498 * We know we're NS in S memory so don't need to repeat those checks.
7499 */
7500 qemu_log_mask(CPU_LOG_INT, "...really an SG instruction at 0x%08" PRIx32
7501 ", executing it\n", env->regs[15]);
7502 env->regs[14] &= ~1;
7503 switch_v7m_security_state(env, true);
7504 xpsr_write(env, 0, XPSR_IT);
7505 env->regs[15] += 4;
7506 return true;
7507
7508gen_invep:
7509 env->v7m.sfsr |= R_V7M_SFSR_INVEP_MASK;
7510 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SECURE, false);
7511 qemu_log_mask(CPU_LOG_INT,
7512 "...really SecureFault with SFSR.INVEP\n");
7513 return false;
7514}
7515
e6f010cc 7516void arm_v7m_cpu_do_interrupt(CPUState *cs)
9ee6e8bb 7517{
e6f010cc
AF
7518 ARMCPU *cpu = ARM_CPU(cs);
7519 CPUARMState *env = &cpu->env;
9ee6e8bb 7520 uint32_t lr;
0094ca70 7521 bool ignore_stackfaults;
9ee6e8bb 7522
27103424 7523 arm_log_exception(cs->exception_index);
3f1beaca 7524
9ee6e8bb
PB
7525 /* For exceptions we just mark as pending on the NVIC, and let that
7526 handle it. */
27103424 7527 switch (cs->exception_index) {
9ee6e8bb 7528 case EXCP_UDEF:
2fb50a33 7529 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, env->v7m.secure);
334e8dad 7530 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_UNDEFINSTR_MASK;
a25dc805 7531 break;
7517748e 7532 case EXCP_NOCP:
2fb50a33 7533 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, env->v7m.secure);
334e8dad 7534 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_NOCP_MASK;
a25dc805 7535 break;
e13886e3 7536 case EXCP_INVSTATE:
2fb50a33 7537 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, env->v7m.secure);
334e8dad 7538 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_INVSTATE_MASK;
e13886e3 7539 break;
9ee6e8bb 7540 case EXCP_SWI:
314e2296 7541 /* The PC already points to the next instruction. */
2fb50a33 7542 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SVC, env->v7m.secure);
a25dc805 7543 break;
9ee6e8bb
PB
7544 case EXCP_PREFETCH_ABORT:
7545 case EXCP_DATA_ABORT:
5dd0641d
MD
7546 /* Note that for M profile we don't have a guest facing FSR, but
7547 * the env->exception.fsr will be populated by the code that
7548 * raises the fault, in the A profile short-descriptor format.
abf1172f 7549 */
5dd0641d 7550 switch (env->exception.fsr & 0xf) {
35337cc3
PM
7551 case M_FAKE_FSR_NSC_EXEC:
7552 /* Exception generated when we try to execute code at an address
7553 * which is marked as Secure & Non-Secure Callable and the CPU
7554 * is in the Non-Secure state. The only instruction which can
7555 * be executed like this is SG (and that only if both halves of
7556 * the SG instruction have the same security attributes.)
7557 * Everything else must generate an INVEP SecureFault, so we
7558 * emulate the SG instruction here.
35337cc3 7559 */
333e10c5
PM
7560 if (v7m_handle_execute_nsc(cpu)) {
7561 return;
7562 }
35337cc3
PM
7563 break;
7564 case M_FAKE_FSR_SFAULT:
7565 /* Various flavours of SecureFault for attempts to execute or
7566 * access data in the wrong security state.
7567 */
7568 switch (cs->exception_index) {
7569 case EXCP_PREFETCH_ABORT:
7570 if (env->v7m.secure) {
7571 env->v7m.sfsr |= R_V7M_SFSR_INVTRAN_MASK;
7572 qemu_log_mask(CPU_LOG_INT,
7573 "...really SecureFault with SFSR.INVTRAN\n");
7574 } else {
7575 env->v7m.sfsr |= R_V7M_SFSR_INVEP_MASK;
7576 qemu_log_mask(CPU_LOG_INT,
7577 "...really SecureFault with SFSR.INVEP\n");
7578 }
7579 break;
7580 case EXCP_DATA_ABORT:
7581 /* This must be an NS access to S memory */
7582 env->v7m.sfsr |= R_V7M_SFSR_AUVIOL_MASK;
7583 qemu_log_mask(CPU_LOG_INT,
7584 "...really SecureFault with SFSR.AUVIOL\n");
7585 break;
7586 }
7587 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SECURE, false);
7588 break;
5dd0641d
MD
7589 case 0x8: /* External Abort */
7590 switch (cs->exception_index) {
7591 case EXCP_PREFETCH_ABORT:
c6158878
PM
7592 env->v7m.cfsr[M_REG_NS] |= R_V7M_CFSR_IBUSERR_MASK;
7593 qemu_log_mask(CPU_LOG_INT, "...with CFSR.IBUSERR\n");
5dd0641d
MD
7594 break;
7595 case EXCP_DATA_ABORT:
334e8dad 7596 env->v7m.cfsr[M_REG_NS] |=
c6158878 7597 (R_V7M_CFSR_PRECISERR_MASK | R_V7M_CFSR_BFARVALID_MASK);
5dd0641d
MD
7598 env->v7m.bfar = env->exception.vaddress;
7599 qemu_log_mask(CPU_LOG_INT,
c6158878 7600 "...with CFSR.PRECISERR and BFAR 0x%x\n",
5dd0641d
MD
7601 env->v7m.bfar);
7602 break;
7603 }
2fb50a33 7604 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_BUS, false);
5dd0641d
MD
7605 break;
7606 default:
7607 /* All other FSR values are either MPU faults or "can't happen
7608 * for M profile" cases.
7609 */
7610 switch (cs->exception_index) {
7611 case EXCP_PREFETCH_ABORT:
334e8dad 7612 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_IACCVIOL_MASK;
5dd0641d
MD
7613 qemu_log_mask(CPU_LOG_INT, "...with CFSR.IACCVIOL\n");
7614 break;
7615 case EXCP_DATA_ABORT:
334e8dad 7616 env->v7m.cfsr[env->v7m.secure] |=
5dd0641d 7617 (R_V7M_CFSR_DACCVIOL_MASK | R_V7M_CFSR_MMARVALID_MASK);
c51a5cfc 7618 env->v7m.mmfar[env->v7m.secure] = env->exception.vaddress;
5dd0641d
MD
7619 qemu_log_mask(CPU_LOG_INT,
7620 "...with CFSR.DACCVIOL and MMFAR 0x%x\n",
c51a5cfc 7621 env->v7m.mmfar[env->v7m.secure]);
5dd0641d
MD
7622 break;
7623 }
2fb50a33
PM
7624 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_MEM,
7625 env->v7m.secure);
5dd0641d
MD
7626 break;
7627 }
a25dc805 7628 break;
9ee6e8bb 7629 case EXCP_BKPT:
cfe67cef 7630 if (semihosting_enabled()) {
2ad207d4 7631 int nr;
f9fd40eb 7632 nr = arm_lduw_code(env, env->regs[15], arm_sctlr_b(env)) & 0xff;
2ad207d4
PB
7633 if (nr == 0xab) {
7634 env->regs[15] += 2;
205ace55
CC
7635 qemu_log_mask(CPU_LOG_INT,
7636 "...handling as semihosting call 0x%x\n",
7637 env->regs[0]);
2ad207d4
PB
7638 env->regs[0] = do_arm_semihosting(env);
7639 return;
7640 }
7641 }
2fb50a33 7642 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_DEBUG, false);
a25dc805 7643 break;
9ee6e8bb 7644 case EXCP_IRQ:
9ee6e8bb
PB
7645 break;
7646 case EXCP_EXCEPTION_EXIT:
d02a8698
PM
7647 if (env->regs[15] < EXC_RETURN_MIN_MAGIC) {
7648 /* Must be v8M security extension function return */
7649 assert(env->regs[15] >= FNC_RETURN_MIN_MAGIC);
7650 assert(arm_feature(env, ARM_FEATURE_M_SECURITY));
7651 if (do_v7m_function_return(cpu)) {
7652 return;
7653 }
7654 } else {
7655 do_v7m_exception_exit(cpu);
7656 return;
7657 }
7658 break;
9ee6e8bb 7659 default:
a47dddd7 7660 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
9ee6e8bb
PB
7661 return; /* Never happens. Keep compiler happy. */
7662 }
7663
d3392718
PM
7664 if (arm_feature(env, ARM_FEATURE_V8)) {
7665 lr = R_V7M_EXCRET_RES1_MASK |
7666 R_V7M_EXCRET_DCRS_MASK |
7667 R_V7M_EXCRET_FTYPE_MASK;
7668 /* The S bit indicates whether we should return to Secure
7669 * or NonSecure (ie our current state).
7670 * The ES bit indicates whether we're taking this exception
7671 * to Secure or NonSecure (ie our target state). We set it
7672 * later, in v7m_exception_taken().
7673 * The SPSEL bit is also set in v7m_exception_taken() for v8M.
7674 * This corresponds to the ARM ARM pseudocode for v8M setting
7675 * some LR bits in PushStack() and some in ExceptionTaken();
7676 * the distinction matters for the tailchain cases where we
7677 * can take an exception without pushing the stack.
7678 */
7679 if (env->v7m.secure) {
7680 lr |= R_V7M_EXCRET_S_MASK;
7681 }
7682 } else {
7683 lr = R_V7M_EXCRET_RES1_MASK |
7684 R_V7M_EXCRET_S_MASK |
7685 R_V7M_EXCRET_DCRS_MASK |
7686 R_V7M_EXCRET_FTYPE_MASK |
7687 R_V7M_EXCRET_ES_MASK;
7688 if (env->v7m.control[M_REG_NS] & R_V7M_CONTROL_SPSEL_MASK) {
7689 lr |= R_V7M_EXCRET_SPSEL_MASK;
7690 }
bd70b29b 7691 }
15b3f556 7692 if (!arm_v7m_is_handler_mode(env)) {
4d1e7a47 7693 lr |= R_V7M_EXCRET_MODE_MASK;
bd70b29b
PM
7694 }
7695
0094ca70
PM
7696 ignore_stackfaults = v7m_push_stack(cpu);
7697 v7m_exception_taken(cpu, lr, false, ignore_stackfaults);
a25dc805 7698 qemu_log_mask(CPU_LOG_INT, "... as %d\n", env->v7m.exception);
9ee6e8bb
PB
7699}
7700
ce02049d
GB
7701/* Function used to synchronize QEMU's AArch64 register set with AArch32
7702 * register set. This is necessary when switching between AArch32 and AArch64
7703 * execution state.
7704 */
7705void aarch64_sync_32_to_64(CPUARMState *env)
7706{
7707 int i;
7708 uint32_t mode = env->uncached_cpsr & CPSR_M;
7709
7710 /* We can blanket copy R[0:7] to X[0:7] */
7711 for (i = 0; i < 8; i++) {
7712 env->xregs[i] = env->regs[i];
7713 }
7714
7715 /* Unless we are in FIQ mode, x8-x12 come from the user registers r8-r12.
7716 * Otherwise, they come from the banked user regs.
7717 */
7718 if (mode == ARM_CPU_MODE_FIQ) {
7719 for (i = 8; i < 13; i++) {
7720 env->xregs[i] = env->usr_regs[i - 8];
7721 }
7722 } else {
7723 for (i = 8; i < 13; i++) {
7724 env->xregs[i] = env->regs[i];
7725 }
7726 }
7727
7728 /* Registers x13-x23 are the various mode SP and FP registers. Registers
7729 * r13 and r14 are only copied if we are in that mode, otherwise we copy
7730 * from the mode banked register.
7731 */
7732 if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
7733 env->xregs[13] = env->regs[13];
7734 env->xregs[14] = env->regs[14];
7735 } else {
7736 env->xregs[13] = env->banked_r13[bank_number(ARM_CPU_MODE_USR)];
7737 /* HYP is an exception in that it is copied from r14 */
7738 if (mode == ARM_CPU_MODE_HYP) {
7739 env->xregs[14] = env->regs[14];
7740 } else {
7741 env->xregs[14] = env->banked_r14[bank_number(ARM_CPU_MODE_USR)];
7742 }
7743 }
7744
7745 if (mode == ARM_CPU_MODE_HYP) {
7746 env->xregs[15] = env->regs[13];
7747 } else {
7748 env->xregs[15] = env->banked_r13[bank_number(ARM_CPU_MODE_HYP)];
7749 }
7750
7751 if (mode == ARM_CPU_MODE_IRQ) {
3a9148d0
SS
7752 env->xregs[16] = env->regs[14];
7753 env->xregs[17] = env->regs[13];
ce02049d 7754 } else {
3a9148d0
SS
7755 env->xregs[16] = env->banked_r14[bank_number(ARM_CPU_MODE_IRQ)];
7756 env->xregs[17] = env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)];
ce02049d
GB
7757 }
7758
7759 if (mode == ARM_CPU_MODE_SVC) {
3a9148d0
SS
7760 env->xregs[18] = env->regs[14];
7761 env->xregs[19] = env->regs[13];
ce02049d 7762 } else {
3a9148d0
SS
7763 env->xregs[18] = env->banked_r14[bank_number(ARM_CPU_MODE_SVC)];
7764 env->xregs[19] = env->banked_r13[bank_number(ARM_CPU_MODE_SVC)];
ce02049d
GB
7765 }
7766
7767 if (mode == ARM_CPU_MODE_ABT) {
3a9148d0
SS
7768 env->xregs[20] = env->regs[14];
7769 env->xregs[21] = env->regs[13];
ce02049d 7770 } else {
3a9148d0
SS
7771 env->xregs[20] = env->banked_r14[bank_number(ARM_CPU_MODE_ABT)];
7772 env->xregs[21] = env->banked_r13[bank_number(ARM_CPU_MODE_ABT)];
ce02049d
GB
7773 }
7774
7775 if (mode == ARM_CPU_MODE_UND) {
3a9148d0
SS
7776 env->xregs[22] = env->regs[14];
7777 env->xregs[23] = env->regs[13];
ce02049d 7778 } else {
3a9148d0
SS
7779 env->xregs[22] = env->banked_r14[bank_number(ARM_CPU_MODE_UND)];
7780 env->xregs[23] = env->banked_r13[bank_number(ARM_CPU_MODE_UND)];
ce02049d
GB
7781 }
7782
7783 /* Registers x24-x30 are mapped to r8-r14 in FIQ mode. If we are in FIQ
7784 * mode, then we can copy from r8-r14. Otherwise, we copy from the
7785 * FIQ bank for r8-r14.
7786 */
7787 if (mode == ARM_CPU_MODE_FIQ) {
7788 for (i = 24; i < 31; i++) {
7789 env->xregs[i] = env->regs[i - 16]; /* X[24:30] <- R[8:14] */
7790 }
7791 } else {
7792 for (i = 24; i < 29; i++) {
7793 env->xregs[i] = env->fiq_regs[i - 24];
7794 }
7795 env->xregs[29] = env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)];
7796 env->xregs[30] = env->banked_r14[bank_number(ARM_CPU_MODE_FIQ)];
7797 }
7798
7799 env->pc = env->regs[15];
7800}
7801
7802/* Function used to synchronize QEMU's AArch32 register set with AArch64
7803 * register set. This is necessary when switching between AArch32 and AArch64
7804 * execution state.
7805 */
7806void aarch64_sync_64_to_32(CPUARMState *env)
7807{
7808 int i;
7809 uint32_t mode = env->uncached_cpsr & CPSR_M;
7810
7811 /* We can blanket copy X[0:7] to R[0:7] */
7812 for (i = 0; i < 8; i++) {
7813 env->regs[i] = env->xregs[i];
7814 }
7815
7816 /* Unless we are in FIQ mode, r8-r12 come from the user registers x8-x12.
7817 * Otherwise, we copy x8-x12 into the banked user regs.
7818 */
7819 if (mode == ARM_CPU_MODE_FIQ) {
7820 for (i = 8; i < 13; i++) {
7821 env->usr_regs[i - 8] = env->xregs[i];
7822 }
7823 } else {
7824 for (i = 8; i < 13; i++) {
7825 env->regs[i] = env->xregs[i];
7826 }
7827 }
7828
7829 /* Registers r13 & r14 depend on the current mode.
7830 * If we are in a given mode, we copy the corresponding x registers to r13
7831 * and r14. Otherwise, we copy the x register to the banked r13 and r14
7832 * for the mode.
7833 */
7834 if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
7835 env->regs[13] = env->xregs[13];
7836 env->regs[14] = env->xregs[14];
7837 } else {
7838 env->banked_r13[bank_number(ARM_CPU_MODE_USR)] = env->xregs[13];
7839
7840 /* HYP is an exception in that it does not have its own banked r14 but
7841 * shares the USR r14
7842 */
7843 if (mode == ARM_CPU_MODE_HYP) {
7844 env->regs[14] = env->xregs[14];
7845 } else {
7846 env->banked_r14[bank_number(ARM_CPU_MODE_USR)] = env->xregs[14];
7847 }
7848 }
7849
7850 if (mode == ARM_CPU_MODE_HYP) {
7851 env->regs[13] = env->xregs[15];
7852 } else {
7853 env->banked_r13[bank_number(ARM_CPU_MODE_HYP)] = env->xregs[15];
7854 }
7855
7856 if (mode == ARM_CPU_MODE_IRQ) {
3a9148d0
SS
7857 env->regs[14] = env->xregs[16];
7858 env->regs[13] = env->xregs[17];
ce02049d 7859 } else {
3a9148d0
SS
7860 env->banked_r14[bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[16];
7861 env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[17];
ce02049d
GB
7862 }
7863
7864 if (mode == ARM_CPU_MODE_SVC) {
3a9148d0
SS
7865 env->regs[14] = env->xregs[18];
7866 env->regs[13] = env->xregs[19];
ce02049d 7867 } else {
3a9148d0
SS
7868 env->banked_r14[bank_number(ARM_CPU_MODE_SVC)] = env->xregs[18];
7869 env->banked_r13[bank_number(ARM_CPU_MODE_SVC)] = env->xregs[19];
ce02049d
GB
7870 }
7871
7872 if (mode == ARM_CPU_MODE_ABT) {
3a9148d0
SS
7873 env->regs[14] = env->xregs[20];
7874 env->regs[13] = env->xregs[21];
ce02049d 7875 } else {
3a9148d0
SS
7876 env->banked_r14[bank_number(ARM_CPU_MODE_ABT)] = env->xregs[20];
7877 env->banked_r13[bank_number(ARM_CPU_MODE_ABT)] = env->xregs[21];
ce02049d
GB
7878 }
7879
7880 if (mode == ARM_CPU_MODE_UND) {
3a9148d0
SS
7881 env->regs[14] = env->xregs[22];
7882 env->regs[13] = env->xregs[23];
ce02049d 7883 } else {
3a9148d0
SS
7884 env->banked_r14[bank_number(ARM_CPU_MODE_UND)] = env->xregs[22];
7885 env->banked_r13[bank_number(ARM_CPU_MODE_UND)] = env->xregs[23];
ce02049d
GB
7886 }
7887
7888 /* Registers x24-x30 are mapped to r8-r14 in FIQ mode. If we are in FIQ
7889 * mode, then we can copy to r8-r14. Otherwise, we copy to the
7890 * FIQ bank for r8-r14.
7891 */
7892 if (mode == ARM_CPU_MODE_FIQ) {
7893 for (i = 24; i < 31; i++) {
7894 env->regs[i - 16] = env->xregs[i]; /* X[24:30] -> R[8:14] */
7895 }
7896 } else {
7897 for (i = 24; i < 29; i++) {
7898 env->fiq_regs[i - 24] = env->xregs[i];
7899 }
7900 env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[29];
7901 env->banked_r14[bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[30];
7902 }
7903
7904 env->regs[15] = env->pc;
7905}
7906
966f758c 7907static void arm_cpu_do_interrupt_aarch32(CPUState *cs)
b5ff1b31 7908{
97a8ea5a
AF
7909 ARMCPU *cpu = ARM_CPU(cs);
7910 CPUARMState *env = &cpu->env;
b5ff1b31
FB
7911 uint32_t addr;
7912 uint32_t mask;
7913 int new_mode;
7914 uint32_t offset;
16a906fd 7915 uint32_t moe;
b5ff1b31 7916
16a906fd
PM
7917 /* If this is a debug exception we must update the DBGDSCR.MOE bits */
7918 switch (env->exception.syndrome >> ARM_EL_EC_SHIFT) {
7919 case EC_BREAKPOINT:
7920 case EC_BREAKPOINT_SAME_EL:
7921 moe = 1;
7922 break;
7923 case EC_WATCHPOINT:
7924 case EC_WATCHPOINT_SAME_EL:
7925 moe = 10;
7926 break;
7927 case EC_AA32_BKPT:
7928 moe = 3;
7929 break;
7930 case EC_VECTORCATCH:
7931 moe = 5;
7932 break;
7933 default:
7934 moe = 0;
7935 break;
7936 }
7937
7938 if (moe) {
7939 env->cp15.mdscr_el1 = deposit64(env->cp15.mdscr_el1, 2, 4, moe);
7940 }
7941
b5ff1b31 7942 /* TODO: Vectored interrupt controller. */
27103424 7943 switch (cs->exception_index) {
b5ff1b31
FB
7944 case EXCP_UDEF:
7945 new_mode = ARM_CPU_MODE_UND;
7946 addr = 0x04;
7947 mask = CPSR_I;
7948 if (env->thumb)
7949 offset = 2;
7950 else
7951 offset = 4;
7952 break;
7953 case EXCP_SWI:
7954 new_mode = ARM_CPU_MODE_SVC;
7955 addr = 0x08;
7956 mask = CPSR_I;
601d70b9 7957 /* The PC already points to the next instruction. */
b5ff1b31
FB
7958 offset = 0;
7959 break;
06c949e6 7960 case EXCP_BKPT:
9ee6e8bb
PB
7961 /* Fall through to prefetch abort. */
7962 case EXCP_PREFETCH_ABORT:
88ca1c2d 7963 A32_BANKED_CURRENT_REG_SET(env, ifsr, env->exception.fsr);
b848ce2b 7964 A32_BANKED_CURRENT_REG_SET(env, ifar, env->exception.vaddress);
3f1beaca 7965 qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x IFAR 0x%x\n",
88ca1c2d 7966 env->exception.fsr, (uint32_t)env->exception.vaddress);
b5ff1b31
FB
7967 new_mode = ARM_CPU_MODE_ABT;
7968 addr = 0x0c;
7969 mask = CPSR_A | CPSR_I;
7970 offset = 4;
7971 break;
7972 case EXCP_DATA_ABORT:
4a7e2d73 7973 A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr);
b848ce2b 7974 A32_BANKED_CURRENT_REG_SET(env, dfar, env->exception.vaddress);
3f1beaca 7975 qemu_log_mask(CPU_LOG_INT, "...with DFSR 0x%x DFAR 0x%x\n",
4a7e2d73 7976 env->exception.fsr,
6cd8a264 7977 (uint32_t)env->exception.vaddress);
b5ff1b31
FB
7978 new_mode = ARM_CPU_MODE_ABT;
7979 addr = 0x10;
7980 mask = CPSR_A | CPSR_I;
7981 offset = 8;
7982 break;
7983 case EXCP_IRQ:
7984 new_mode = ARM_CPU_MODE_IRQ;
7985 addr = 0x18;
7986 /* Disable IRQ and imprecise data aborts. */
7987 mask = CPSR_A | CPSR_I;
7988 offset = 4;
de38d23b
FA
7989 if (env->cp15.scr_el3 & SCR_IRQ) {
7990 /* IRQ routed to monitor mode */
7991 new_mode = ARM_CPU_MODE_MON;
7992 mask |= CPSR_F;
7993 }
b5ff1b31
FB
7994 break;
7995 case EXCP_FIQ:
7996 new_mode = ARM_CPU_MODE_FIQ;
7997 addr = 0x1c;
7998 /* Disable FIQ, IRQ and imprecise data aborts. */
7999 mask = CPSR_A | CPSR_I | CPSR_F;
de38d23b
FA
8000 if (env->cp15.scr_el3 & SCR_FIQ) {
8001 /* FIQ routed to monitor mode */
8002 new_mode = ARM_CPU_MODE_MON;
8003 }
b5ff1b31
FB
8004 offset = 4;
8005 break;
87a4b270
PM
8006 case EXCP_VIRQ:
8007 new_mode = ARM_CPU_MODE_IRQ;
8008 addr = 0x18;
8009 /* Disable IRQ and imprecise data aborts. */
8010 mask = CPSR_A | CPSR_I;
8011 offset = 4;
8012 break;
8013 case EXCP_VFIQ:
8014 new_mode = ARM_CPU_MODE_FIQ;
8015 addr = 0x1c;
8016 /* Disable FIQ, IRQ and imprecise data aborts. */
8017 mask = CPSR_A | CPSR_I | CPSR_F;
8018 offset = 4;
8019 break;
dbe9d163
FA
8020 case EXCP_SMC:
8021 new_mode = ARM_CPU_MODE_MON;
8022 addr = 0x08;
8023 mask = CPSR_A | CPSR_I | CPSR_F;
8024 offset = 0;
8025 break;
b5ff1b31 8026 default:
a47dddd7 8027 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
b5ff1b31
FB
8028 return; /* Never happens. Keep compiler happy. */
8029 }
e89e51a1
FA
8030
8031 if (new_mode == ARM_CPU_MODE_MON) {
8032 addr += env->cp15.mvbar;
137feaa9 8033 } else if (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_V) {
e89e51a1 8034 /* High vectors. When enabled, base address cannot be remapped. */
b5ff1b31 8035 addr += 0xffff0000;
8641136c
NR
8036 } else {
8037 /* ARM v7 architectures provide a vector base address register to remap
8038 * the interrupt vector table.
e89e51a1 8039 * This register is only followed in non-monitor mode, and is banked.
8641136c
NR
8040 * Note: only bits 31:5 are valid.
8041 */
fb6c91ba 8042 addr += A32_BANKED_CURRENT_REG_GET(env, vbar);
b5ff1b31 8043 }
dbe9d163
FA
8044
8045 if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON) {
8046 env->cp15.scr_el3 &= ~SCR_NS;
8047 }
8048
b5ff1b31 8049 switch_mode (env, new_mode);
662cefb7
PM
8050 /* For exceptions taken to AArch32 we must clear the SS bit in both
8051 * PSTATE and in the old-state value we save to SPSR_<mode>, so zero it now.
8052 */
8053 env->uncached_cpsr &= ~PSTATE_SS;
b5ff1b31 8054 env->spsr = cpsr_read(env);
9ee6e8bb
PB
8055 /* Clear IT bits. */
8056 env->condexec_bits = 0;
30a8cac1 8057 /* Switch to the new mode, and to the correct instruction set. */
6d7e6326 8058 env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode;
73462ddd
PC
8059 /* Set new mode endianness */
8060 env->uncached_cpsr &= ~CPSR_E;
8061 if (env->cp15.sctlr_el[arm_current_el(env)] & SCTLR_EE) {
3823b9db 8062 env->uncached_cpsr |= CPSR_E;
73462ddd 8063 }
4cc35614 8064 env->daif |= mask;
be5e7a76
DES
8065 /* this is a lie, as the was no c1_sys on V4T/V5, but who cares
8066 * and we should just guard the thumb mode on V4 */
8067 if (arm_feature(env, ARM_FEATURE_V4T)) {
137feaa9 8068 env->thumb = (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_TE) != 0;
be5e7a76 8069 }
b5ff1b31
FB
8070 env->regs[14] = env->regs[15] + offset;
8071 env->regs[15] = addr;
b5ff1b31
FB
8072}
8073
966f758c
PM
8074/* Handle exception entry to a target EL which is using AArch64 */
8075static void arm_cpu_do_interrupt_aarch64(CPUState *cs)
f3a9b694
PM
8076{
8077 ARMCPU *cpu = ARM_CPU(cs);
8078 CPUARMState *env = &cpu->env;
8079 unsigned int new_el = env->exception.target_el;
8080 target_ulong addr = env->cp15.vbar_el[new_el];
8081 unsigned int new_mode = aarch64_pstate_mode(new_el, true);
8082
8083 if (arm_current_el(env) < new_el) {
3d6f7617
PM
8084 /* Entry vector offset depends on whether the implemented EL
8085 * immediately lower than the target level is using AArch32 or AArch64
8086 */
8087 bool is_aa64;
8088
8089 switch (new_el) {
8090 case 3:
8091 is_aa64 = (env->cp15.scr_el3 & SCR_RW) != 0;
8092 break;
8093 case 2:
8094 is_aa64 = (env->cp15.hcr_el2 & HCR_RW) != 0;
8095 break;
8096 case 1:
8097 is_aa64 = is_a64(env);
8098 break;
8099 default:
8100 g_assert_not_reached();
8101 }
8102
8103 if (is_aa64) {
f3a9b694
PM
8104 addr += 0x400;
8105 } else {
8106 addr += 0x600;
8107 }
8108 } else if (pstate_read(env) & PSTATE_SP) {
8109 addr += 0x200;
8110 }
8111
f3a9b694
PM
8112 switch (cs->exception_index) {
8113 case EXCP_PREFETCH_ABORT:
8114 case EXCP_DATA_ABORT:
8115 env->cp15.far_el[new_el] = env->exception.vaddress;
8116 qemu_log_mask(CPU_LOG_INT, "...with FAR 0x%" PRIx64 "\n",
8117 env->cp15.far_el[new_el]);
8118 /* fall through */
8119 case EXCP_BKPT:
8120 case EXCP_UDEF:
8121 case EXCP_SWI:
8122 case EXCP_HVC:
8123 case EXCP_HYP_TRAP:
8124 case EXCP_SMC:
8125 env->cp15.esr_el[new_el] = env->exception.syndrome;
8126 break;
8127 case EXCP_IRQ:
8128 case EXCP_VIRQ:
8129 addr += 0x80;
8130 break;
8131 case EXCP_FIQ:
8132 case EXCP_VFIQ:
8133 addr += 0x100;
8134 break;
8135 case EXCP_SEMIHOST:
8136 qemu_log_mask(CPU_LOG_INT,
8137 "...handling as semihosting call 0x%" PRIx64 "\n",
8138 env->xregs[0]);
8139 env->xregs[0] = do_arm_semihosting(env);
8140 return;
8141 default:
8142 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
8143 }
8144
8145 if (is_a64(env)) {
8146 env->banked_spsr[aarch64_banked_spsr_index(new_el)] = pstate_read(env);
8147 aarch64_save_sp(env, arm_current_el(env));
8148 env->elr_el[new_el] = env->pc;
8149 } else {
8150 env->banked_spsr[aarch64_banked_spsr_index(new_el)] = cpsr_read(env);
f3a9b694
PM
8151 env->elr_el[new_el] = env->regs[15];
8152
8153 aarch64_sync_32_to_64(env);
8154
8155 env->condexec_bits = 0;
8156 }
8157 qemu_log_mask(CPU_LOG_INT, "...with ELR 0x%" PRIx64 "\n",
8158 env->elr_el[new_el]);
8159
8160 pstate_write(env, PSTATE_DAIF | new_mode);
8161 env->aarch64 = 1;
8162 aarch64_restore_sp(env, new_el);
8163
8164 env->pc = addr;
8165
8166 qemu_log_mask(CPU_LOG_INT, "...to EL%d PC 0x%" PRIx64 " PSTATE 0x%x\n",
8167 new_el, env->pc, pstate_read(env));
966f758c
PM
8168}
8169
904c04de
PM
8170static inline bool check_for_semihosting(CPUState *cs)
8171{
8172 /* Check whether this exception is a semihosting call; if so
8173 * then handle it and return true; otherwise return false.
8174 */
8175 ARMCPU *cpu = ARM_CPU(cs);
8176 CPUARMState *env = &cpu->env;
8177
8178 if (is_a64(env)) {
8179 if (cs->exception_index == EXCP_SEMIHOST) {
8180 /* This is always the 64-bit semihosting exception.
8181 * The "is this usermode" and "is semihosting enabled"
8182 * checks have been done at translate time.
8183 */
8184 qemu_log_mask(CPU_LOG_INT,
8185 "...handling as semihosting call 0x%" PRIx64 "\n",
8186 env->xregs[0]);
8187 env->xregs[0] = do_arm_semihosting(env);
8188 return true;
8189 }
8190 return false;
8191 } else {
8192 uint32_t imm;
8193
8194 /* Only intercept calls from privileged modes, to provide some
8195 * semblance of security.
8196 */
19a6e31c
PM
8197 if (cs->exception_index != EXCP_SEMIHOST &&
8198 (!semihosting_enabled() ||
8199 ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_USR))) {
904c04de
PM
8200 return false;
8201 }
8202
8203 switch (cs->exception_index) {
19a6e31c
PM
8204 case EXCP_SEMIHOST:
8205 /* This is always a semihosting call; the "is this usermode"
8206 * and "is semihosting enabled" checks have been done at
8207 * translate time.
8208 */
8209 break;
904c04de
PM
8210 case EXCP_SWI:
8211 /* Check for semihosting interrupt. */
8212 if (env->thumb) {
f9fd40eb 8213 imm = arm_lduw_code(env, env->regs[15] - 2, arm_sctlr_b(env))
904c04de
PM
8214 & 0xff;
8215 if (imm == 0xab) {
8216 break;
8217 }
8218 } else {
f9fd40eb 8219 imm = arm_ldl_code(env, env->regs[15] - 4, arm_sctlr_b(env))
904c04de
PM
8220 & 0xffffff;
8221 if (imm == 0x123456) {
8222 break;
8223 }
8224 }
8225 return false;
8226 case EXCP_BKPT:
8227 /* See if this is a semihosting syscall. */
8228 if (env->thumb) {
f9fd40eb 8229 imm = arm_lduw_code(env, env->regs[15], arm_sctlr_b(env))
904c04de
PM
8230 & 0xff;
8231 if (imm == 0xab) {
8232 env->regs[15] += 2;
8233 break;
8234 }
8235 }
8236 return false;
8237 default:
8238 return false;
8239 }
8240
8241 qemu_log_mask(CPU_LOG_INT,
8242 "...handling as semihosting call 0x%x\n",
8243 env->regs[0]);
8244 env->regs[0] = do_arm_semihosting(env);
8245 return true;
8246 }
8247}
8248
966f758c
PM
8249/* Handle a CPU exception for A and R profile CPUs.
8250 * Do any appropriate logging, handle PSCI calls, and then hand off
8251 * to the AArch64-entry or AArch32-entry function depending on the
8252 * target exception level's register width.
8253 */
8254void arm_cpu_do_interrupt(CPUState *cs)
8255{
8256 ARMCPU *cpu = ARM_CPU(cs);
8257 CPUARMState *env = &cpu->env;
8258 unsigned int new_el = env->exception.target_el;
8259
531c60a9 8260 assert(!arm_feature(env, ARM_FEATURE_M));
966f758c
PM
8261
8262 arm_log_exception(cs->exception_index);
8263 qemu_log_mask(CPU_LOG_INT, "...from EL%d to EL%d\n", arm_current_el(env),
8264 new_el);
8265 if (qemu_loglevel_mask(CPU_LOG_INT)
8266 && !excp_is_internal(cs->exception_index)) {
6568da45 8267 qemu_log_mask(CPU_LOG_INT, "...with ESR 0x%x/0x%" PRIx32 "\n",
966f758c
PM
8268 env->exception.syndrome >> ARM_EL_EC_SHIFT,
8269 env->exception.syndrome);
8270 }
8271
8272 if (arm_is_psci_call(cpu, cs->exception_index)) {
8273 arm_handle_psci_call(cpu);
8274 qemu_log_mask(CPU_LOG_INT, "...handled as PSCI call\n");
8275 return;
8276 }
8277
904c04de
PM
8278 /* Semihosting semantics depend on the register width of the
8279 * code that caused the exception, not the target exception level,
8280 * so must be handled here.
966f758c 8281 */
904c04de
PM
8282 if (check_for_semihosting(cs)) {
8283 return;
8284 }
8285
b5c53d1b
AL
8286 /* Hooks may change global state so BQL should be held, also the
8287 * BQL needs to be held for any modification of
8288 * cs->interrupt_request.
8289 */
8290 g_assert(qemu_mutex_iothread_locked());
8291
8292 arm_call_pre_el_change_hook(cpu);
8293
904c04de
PM
8294 assert(!excp_is_internal(cs->exception_index));
8295 if (arm_el_is_aa64(env, new_el)) {
966f758c
PM
8296 arm_cpu_do_interrupt_aarch64(cs);
8297 } else {
8298 arm_cpu_do_interrupt_aarch32(cs);
8299 }
f3a9b694 8300
bd7d00fc
PM
8301 arm_call_el_change_hook(cpu);
8302
f3a9b694
PM
8303 if (!kvm_enabled()) {
8304 cs->interrupt_request |= CPU_INTERRUPT_EXITTB;
8305 }
8306}
0480f69a
PM
8307
8308/* Return the exception level which controls this address translation regime */
8309static inline uint32_t regime_el(CPUARMState *env, ARMMMUIdx mmu_idx)
8310{
8311 switch (mmu_idx) {
8312 case ARMMMUIdx_S2NS:
8313 case ARMMMUIdx_S1E2:
8314 return 2;
8315 case ARMMMUIdx_S1E3:
8316 return 3;
8317 case ARMMMUIdx_S1SE0:
8318 return arm_el_is_aa64(env, 3) ? 1 : 3;
8319 case ARMMMUIdx_S1SE1:
8320 case ARMMMUIdx_S1NSE0:
8321 case ARMMMUIdx_S1NSE1:
62593718
PM
8322 case ARMMMUIdx_MPrivNegPri:
8323 case ARMMMUIdx_MUserNegPri:
e7b921c2
PM
8324 case ARMMMUIdx_MPriv:
8325 case ARMMMUIdx_MUser:
62593718
PM
8326 case ARMMMUIdx_MSPrivNegPri:
8327 case ARMMMUIdx_MSUserNegPri:
66787c78 8328 case ARMMMUIdx_MSPriv:
66787c78 8329 case ARMMMUIdx_MSUser:
0480f69a
PM
8330 return 1;
8331 default:
8332 g_assert_not_reached();
8333 }
8334}
8335
8336/* Return the SCTLR value which controls this address translation regime */
8337static inline uint32_t regime_sctlr(CPUARMState *env, ARMMMUIdx mmu_idx)
8338{
8339 return env->cp15.sctlr_el[regime_el(env, mmu_idx)];
8340}
8341
8342/* Return true if the specified stage of address translation is disabled */
8343static inline bool regime_translation_disabled(CPUARMState *env,
8344 ARMMMUIdx mmu_idx)
8345{
29c483a5 8346 if (arm_feature(env, ARM_FEATURE_M)) {
ecf5e8ea 8347 switch (env->v7m.mpu_ctrl[regime_is_secure(env, mmu_idx)] &
3bef7012
PM
8348 (R_V7M_MPU_CTRL_ENABLE_MASK | R_V7M_MPU_CTRL_HFNMIENA_MASK)) {
8349 case R_V7M_MPU_CTRL_ENABLE_MASK:
8350 /* Enabled, but not for HardFault and NMI */
62593718 8351 return mmu_idx & ARM_MMU_IDX_M_NEGPRI;
3bef7012
PM
8352 case R_V7M_MPU_CTRL_ENABLE_MASK | R_V7M_MPU_CTRL_HFNMIENA_MASK:
8353 /* Enabled for all cases */
8354 return false;
8355 case 0:
8356 default:
8357 /* HFNMIENA set and ENABLE clear is UNPREDICTABLE, but
8358 * we warned about that in armv7m_nvic.c when the guest set it.
8359 */
8360 return true;
8361 }
29c483a5
MD
8362 }
8363
0480f69a
PM
8364 if (mmu_idx == ARMMMUIdx_S2NS) {
8365 return (env->cp15.hcr_el2 & HCR_VM) == 0;
8366 }
8367 return (regime_sctlr(env, mmu_idx) & SCTLR_M) == 0;
8368}
8369
73462ddd
PC
8370static inline bool regime_translation_big_endian(CPUARMState *env,
8371 ARMMMUIdx mmu_idx)
8372{
8373 return (regime_sctlr(env, mmu_idx) & SCTLR_EE) != 0;
8374}
8375
0480f69a
PM
8376/* Return the TCR controlling this translation regime */
8377static inline TCR *regime_tcr(CPUARMState *env, ARMMMUIdx mmu_idx)
8378{
8379 if (mmu_idx == ARMMMUIdx_S2NS) {
68e9c2fe 8380 return &env->cp15.vtcr_el2;
0480f69a
PM
8381 }
8382 return &env->cp15.tcr_el[regime_el(env, mmu_idx)];
8383}
8384
8bd5c820
PM
8385/* Convert a possible stage1+2 MMU index into the appropriate
8386 * stage 1 MMU index
8387 */
8388static inline ARMMMUIdx stage_1_mmu_idx(ARMMMUIdx mmu_idx)
8389{
8390 if (mmu_idx == ARMMMUIdx_S12NSE0 || mmu_idx == ARMMMUIdx_S12NSE1) {
8391 mmu_idx += (ARMMMUIdx_S1NSE0 - ARMMMUIdx_S12NSE0);
8392 }
8393 return mmu_idx;
8394}
8395
86fb3fa4
TH
8396/* Returns TBI0 value for current regime el */
8397uint32_t arm_regime_tbi0(CPUARMState *env, ARMMMUIdx mmu_idx)
8398{
8399 TCR *tcr;
8400 uint32_t el;
8401
8402 /* For EL0 and EL1, TBI is controlled by stage 1's TCR, so convert
8bd5c820
PM
8403 * a stage 1+2 mmu index into the appropriate stage 1 mmu index.
8404 */
8405 mmu_idx = stage_1_mmu_idx(mmu_idx);
86fb3fa4
TH
8406
8407 tcr = regime_tcr(env, mmu_idx);
8408 el = regime_el(env, mmu_idx);
8409
8410 if (el > 1) {
8411 return extract64(tcr->raw_tcr, 20, 1);
8412 } else {
8413 return extract64(tcr->raw_tcr, 37, 1);
8414 }
8415}
8416
8417/* Returns TBI1 value for current regime el */
8418uint32_t arm_regime_tbi1(CPUARMState *env, ARMMMUIdx mmu_idx)
8419{
8420 TCR *tcr;
8421 uint32_t el;
8422
8423 /* For EL0 and EL1, TBI is controlled by stage 1's TCR, so convert
8bd5c820
PM
8424 * a stage 1+2 mmu index into the appropriate stage 1 mmu index.
8425 */
8426 mmu_idx = stage_1_mmu_idx(mmu_idx);
86fb3fa4
TH
8427
8428 tcr = regime_tcr(env, mmu_idx);
8429 el = regime_el(env, mmu_idx);
8430
8431 if (el > 1) {
8432 return 0;
8433 } else {
8434 return extract64(tcr->raw_tcr, 38, 1);
8435 }
8436}
8437
aef878be
GB
8438/* Return the TTBR associated with this translation regime */
8439static inline uint64_t regime_ttbr(CPUARMState *env, ARMMMUIdx mmu_idx,
8440 int ttbrn)
8441{
8442 if (mmu_idx == ARMMMUIdx_S2NS) {
b698e9cf 8443 return env->cp15.vttbr_el2;
aef878be
GB
8444 }
8445 if (ttbrn == 0) {
8446 return env->cp15.ttbr0_el[regime_el(env, mmu_idx)];
8447 } else {
8448 return env->cp15.ttbr1_el[regime_el(env, mmu_idx)];
8449 }
8450}
8451
0480f69a
PM
8452/* Return true if the translation regime is using LPAE format page tables */
8453static inline bool regime_using_lpae_format(CPUARMState *env,
8454 ARMMMUIdx mmu_idx)
8455{
8456 int el = regime_el(env, mmu_idx);
8457 if (el == 2 || arm_el_is_aa64(env, el)) {
8458 return true;
8459 }
8460 if (arm_feature(env, ARM_FEATURE_LPAE)
8461 && (regime_tcr(env, mmu_idx)->raw_tcr & TTBCR_EAE)) {
8462 return true;
8463 }
8464 return false;
8465}
8466
deb2db99
AR
8467/* Returns true if the stage 1 translation regime is using LPAE format page
8468 * tables. Used when raising alignment exceptions, whose FSR changes depending
8469 * on whether the long or short descriptor format is in use. */
8470bool arm_s1_regime_using_lpae_format(CPUARMState *env, ARMMMUIdx mmu_idx)
30901475 8471{
8bd5c820 8472 mmu_idx = stage_1_mmu_idx(mmu_idx);
deb2db99 8473
30901475
AB
8474 return regime_using_lpae_format(env, mmu_idx);
8475}
8476
0480f69a
PM
8477static inline bool regime_is_user(CPUARMState *env, ARMMMUIdx mmu_idx)
8478{
8479 switch (mmu_idx) {
8480 case ARMMMUIdx_S1SE0:
8481 case ARMMMUIdx_S1NSE0:
e7b921c2 8482 case ARMMMUIdx_MUser:
871bec7c 8483 case ARMMMUIdx_MSUser:
62593718
PM
8484 case ARMMMUIdx_MUserNegPri:
8485 case ARMMMUIdx_MSUserNegPri:
0480f69a
PM
8486 return true;
8487 default:
8488 return false;
8489 case ARMMMUIdx_S12NSE0:
8490 case ARMMMUIdx_S12NSE1:
8491 g_assert_not_reached();
8492 }
8493}
8494
0fbf5238
AJ
8495/* Translate section/page access permissions to page
8496 * R/W protection flags
d76951b6
AJ
8497 *
8498 * @env: CPUARMState
8499 * @mmu_idx: MMU index indicating required translation regime
8500 * @ap: The 3-bit access permissions (AP[2:0])
8501 * @domain_prot: The 2-bit domain access permissions
0fbf5238
AJ
8502 */
8503static inline int ap_to_rw_prot(CPUARMState *env, ARMMMUIdx mmu_idx,
8504 int ap, int domain_prot)
8505{
554b0b09
PM
8506 bool is_user = regime_is_user(env, mmu_idx);
8507
8508 if (domain_prot == 3) {
8509 return PAGE_READ | PAGE_WRITE;
8510 }
8511
554b0b09
PM
8512 switch (ap) {
8513 case 0:
8514 if (arm_feature(env, ARM_FEATURE_V7)) {
8515 return 0;
8516 }
554b0b09
PM
8517 switch (regime_sctlr(env, mmu_idx) & (SCTLR_S | SCTLR_R)) {
8518 case SCTLR_S:
8519 return is_user ? 0 : PAGE_READ;
8520 case SCTLR_R:
8521 return PAGE_READ;
8522 default:
8523 return 0;
8524 }
8525 case 1:
8526 return is_user ? 0 : PAGE_READ | PAGE_WRITE;
8527 case 2:
87c3d486 8528 if (is_user) {
0fbf5238 8529 return PAGE_READ;
87c3d486 8530 } else {
554b0b09 8531 return PAGE_READ | PAGE_WRITE;
87c3d486 8532 }
554b0b09
PM
8533 case 3:
8534 return PAGE_READ | PAGE_WRITE;
8535 case 4: /* Reserved. */
8536 return 0;
8537 case 5:
0fbf5238 8538 return is_user ? 0 : PAGE_READ;
554b0b09 8539 case 6:
0fbf5238 8540 return PAGE_READ;
554b0b09 8541 case 7:
87c3d486 8542 if (!arm_feature(env, ARM_FEATURE_V6K)) {
554b0b09 8543 return 0;
87c3d486 8544 }
0fbf5238 8545 return PAGE_READ;
554b0b09 8546 default:
0fbf5238 8547 g_assert_not_reached();
554b0b09 8548 }
b5ff1b31
FB
8549}
8550
d76951b6
AJ
8551/* Translate section/page access permissions to page
8552 * R/W protection flags.
8553 *
d76951b6 8554 * @ap: The 2-bit simple AP (AP[2:1])
d8e052b3 8555 * @is_user: TRUE if accessing from PL0
d76951b6 8556 */
d8e052b3 8557static inline int simple_ap_to_rw_prot_is_user(int ap, bool is_user)
d76951b6 8558{
d76951b6
AJ
8559 switch (ap) {
8560 case 0:
8561 return is_user ? 0 : PAGE_READ | PAGE_WRITE;
8562 case 1:
8563 return PAGE_READ | PAGE_WRITE;
8564 case 2:
8565 return is_user ? 0 : PAGE_READ;
8566 case 3:
8567 return PAGE_READ;
8568 default:
8569 g_assert_not_reached();
8570 }
8571}
8572
d8e052b3
AJ
8573static inline int
8574simple_ap_to_rw_prot(CPUARMState *env, ARMMMUIdx mmu_idx, int ap)
8575{
8576 return simple_ap_to_rw_prot_is_user(ap, regime_is_user(env, mmu_idx));
8577}
8578
6ab1a5ee
EI
8579/* Translate S2 section/page access permissions to protection flags
8580 *
8581 * @env: CPUARMState
8582 * @s2ap: The 2-bit stage2 access permissions (S2AP)
8583 * @xn: XN (execute-never) bit
8584 */
8585static int get_S2prot(CPUARMState *env, int s2ap, int xn)
8586{
8587 int prot = 0;
8588
8589 if (s2ap & 1) {
8590 prot |= PAGE_READ;
8591 }
8592 if (s2ap & 2) {
8593 prot |= PAGE_WRITE;
8594 }
8595 if (!xn) {
dfda6837
SS
8596 if (arm_el_is_aa64(env, 2) || prot & PAGE_READ) {
8597 prot |= PAGE_EXEC;
8598 }
6ab1a5ee
EI
8599 }
8600 return prot;
8601}
8602
d8e052b3
AJ
8603/* Translate section/page access permissions to protection flags
8604 *
8605 * @env: CPUARMState
8606 * @mmu_idx: MMU index indicating required translation regime
8607 * @is_aa64: TRUE if AArch64
8608 * @ap: The 2-bit simple AP (AP[2:1])
8609 * @ns: NS (non-secure) bit
8610 * @xn: XN (execute-never) bit
8611 * @pxn: PXN (privileged execute-never) bit
8612 */
8613static int get_S1prot(CPUARMState *env, ARMMMUIdx mmu_idx, bool is_aa64,
8614 int ap, int ns, int xn, int pxn)
8615{
8616 bool is_user = regime_is_user(env, mmu_idx);
8617 int prot_rw, user_rw;
8618 bool have_wxn;
8619 int wxn = 0;
8620
8621 assert(mmu_idx != ARMMMUIdx_S2NS);
8622
8623 user_rw = simple_ap_to_rw_prot_is_user(ap, true);
8624 if (is_user) {
8625 prot_rw = user_rw;
8626 } else {
8627 prot_rw = simple_ap_to_rw_prot_is_user(ap, false);
8628 }
8629
8630 if (ns && arm_is_secure(env) && (env->cp15.scr_el3 & SCR_SIF)) {
8631 return prot_rw;
8632 }
8633
8634 /* TODO have_wxn should be replaced with
8635 * ARM_FEATURE_V8 || (ARM_FEATURE_V7 && ARM_FEATURE_EL2)
8636 * when ARM_FEATURE_EL2 starts getting set. For now we assume all LPAE
8637 * compatible processors have EL2, which is required for [U]WXN.
8638 */
8639 have_wxn = arm_feature(env, ARM_FEATURE_LPAE);
8640
8641 if (have_wxn) {
8642 wxn = regime_sctlr(env, mmu_idx) & SCTLR_WXN;
8643 }
8644
8645 if (is_aa64) {
8646 switch (regime_el(env, mmu_idx)) {
8647 case 1:
8648 if (!is_user) {
8649 xn = pxn || (user_rw & PAGE_WRITE);
8650 }
8651 break;
8652 case 2:
8653 case 3:
8654 break;
8655 }
8656 } else if (arm_feature(env, ARM_FEATURE_V7)) {
8657 switch (regime_el(env, mmu_idx)) {
8658 case 1:
8659 case 3:
8660 if (is_user) {
8661 xn = xn || !(user_rw & PAGE_READ);
8662 } else {
8663 int uwxn = 0;
8664 if (have_wxn) {
8665 uwxn = regime_sctlr(env, mmu_idx) & SCTLR_UWXN;
8666 }
8667 xn = xn || !(prot_rw & PAGE_READ) || pxn ||
8668 (uwxn && (user_rw & PAGE_WRITE));
8669 }
8670 break;
8671 case 2:
8672 break;
8673 }
8674 } else {
8675 xn = wxn = 0;
8676 }
8677
8678 if (xn || (wxn && (prot_rw & PAGE_WRITE))) {
8679 return prot_rw;
8680 }
8681 return prot_rw | PAGE_EXEC;
8682}
8683
0480f69a
PM
8684static bool get_level1_table_address(CPUARMState *env, ARMMMUIdx mmu_idx,
8685 uint32_t *table, uint32_t address)
b2fa1797 8686{
0480f69a 8687 /* Note that we can only get here for an AArch32 PL0/PL1 lookup */
0480f69a 8688 TCR *tcr = regime_tcr(env, mmu_idx);
11f136ee 8689
11f136ee
FA
8690 if (address & tcr->mask) {
8691 if (tcr->raw_tcr & TTBCR_PD1) {
e389be16
FA
8692 /* Translation table walk disabled for TTBR1 */
8693 return false;
8694 }
aef878be 8695 *table = regime_ttbr(env, mmu_idx, 1) & 0xffffc000;
e389be16 8696 } else {
11f136ee 8697 if (tcr->raw_tcr & TTBCR_PD0) {
e389be16
FA
8698 /* Translation table walk disabled for TTBR0 */
8699 return false;
8700 }
aef878be 8701 *table = regime_ttbr(env, mmu_idx, 0) & tcr->base_mask;
e389be16
FA
8702 }
8703 *table |= (address >> 18) & 0x3ffc;
8704 return true;
b2fa1797
PB
8705}
8706
37785977
EI
8707/* Translate a S1 pagetable walk through S2 if needed. */
8708static hwaddr S1_ptw_translate(CPUARMState *env, ARMMMUIdx mmu_idx,
8709 hwaddr addr, MemTxAttrs txattrs,
37785977
EI
8710 ARMMMUFaultInfo *fi)
8711{
8712 if ((mmu_idx == ARMMMUIdx_S1NSE0 || mmu_idx == ARMMMUIdx_S1NSE1) &&
8713 !regime_translation_disabled(env, ARMMMUIdx_S2NS)) {
8714 target_ulong s2size;
8715 hwaddr s2pa;
8716 int s2prot;
8717 int ret;
8718
8719 ret = get_phys_addr_lpae(env, addr, 0, ARMMMUIdx_S2NS, &s2pa,
da909b2c 8720 &txattrs, &s2prot, &s2size, fi, NULL);
37785977 8721 if (ret) {
3b39d734 8722 assert(fi->type != ARMFault_None);
37785977
EI
8723 fi->s2addr = addr;
8724 fi->stage2 = true;
8725 fi->s1ptw = true;
8726 return ~0;
8727 }
8728 addr = s2pa;
8729 }
8730 return addr;
8731}
8732
14577270 8733/* All loads done in the course of a page table walk go through here. */
a614e698 8734static uint32_t arm_ldl_ptw(CPUState *cs, hwaddr addr, bool is_secure,
3795a6de 8735 ARMMMUIdx mmu_idx, ARMMMUFaultInfo *fi)
ebca90e4 8736{
a614e698
EI
8737 ARMCPU *cpu = ARM_CPU(cs);
8738 CPUARMState *env = &cpu->env;
ebca90e4 8739 MemTxAttrs attrs = {};
3b39d734 8740 MemTxResult result = MEMTX_OK;
5ce4ff65 8741 AddressSpace *as;
3b39d734 8742 uint32_t data;
ebca90e4
PM
8743
8744 attrs.secure = is_secure;
5ce4ff65 8745 as = arm_addressspace(cs, attrs);
3795a6de 8746 addr = S1_ptw_translate(env, mmu_idx, addr, attrs, fi);
a614e698
EI
8747 if (fi->s1ptw) {
8748 return 0;
8749 }
73462ddd 8750 if (regime_translation_big_endian(env, mmu_idx)) {
3b39d734 8751 data = address_space_ldl_be(as, addr, attrs, &result);
73462ddd 8752 } else {
3b39d734 8753 data = address_space_ldl_le(as, addr, attrs, &result);
73462ddd 8754 }
3b39d734
PM
8755 if (result == MEMTX_OK) {
8756 return data;
8757 }
8758 fi->type = ARMFault_SyncExternalOnWalk;
8759 fi->ea = arm_extabort_type(result);
8760 return 0;
ebca90e4
PM
8761}
8762
37785977 8763static uint64_t arm_ldq_ptw(CPUState *cs, hwaddr addr, bool is_secure,
3795a6de 8764 ARMMMUIdx mmu_idx, ARMMMUFaultInfo *fi)
ebca90e4 8765{
37785977
EI
8766 ARMCPU *cpu = ARM_CPU(cs);
8767 CPUARMState *env = &cpu->env;
ebca90e4 8768 MemTxAttrs attrs = {};
3b39d734 8769 MemTxResult result = MEMTX_OK;
5ce4ff65 8770 AddressSpace *as;
9aea1ea3 8771 uint64_t data;
ebca90e4
PM
8772
8773 attrs.secure = is_secure;
5ce4ff65 8774 as = arm_addressspace(cs, attrs);
3795a6de 8775 addr = S1_ptw_translate(env, mmu_idx, addr, attrs, fi);
37785977
EI
8776 if (fi->s1ptw) {
8777 return 0;
8778 }
73462ddd 8779 if (regime_translation_big_endian(env, mmu_idx)) {
3b39d734 8780 data = address_space_ldq_be(as, addr, attrs, &result);
73462ddd 8781 } else {
3b39d734
PM
8782 data = address_space_ldq_le(as, addr, attrs, &result);
8783 }
8784 if (result == MEMTX_OK) {
8785 return data;
73462ddd 8786 }
3b39d734
PM
8787 fi->type = ARMFault_SyncExternalOnWalk;
8788 fi->ea = arm_extabort_type(result);
8789 return 0;
ebca90e4
PM
8790}
8791
b7cc4e82 8792static bool get_phys_addr_v5(CPUARMState *env, uint32_t address,
03ae85f8 8793 MMUAccessType access_type, ARMMMUIdx mmu_idx,
b7cc4e82 8794 hwaddr *phys_ptr, int *prot,
f989983e 8795 target_ulong *page_size,
e14b5a23 8796 ARMMMUFaultInfo *fi)
b5ff1b31 8797{
70d74660 8798 CPUState *cs = CPU(arm_env_get_cpu(env));
f989983e 8799 int level = 1;
b5ff1b31
FB
8800 uint32_t table;
8801 uint32_t desc;
8802 int type;
8803 int ap;
e389be16 8804 int domain = 0;
dd4ebc2e 8805 int domain_prot;
a8170e5e 8806 hwaddr phys_addr;
0480f69a 8807 uint32_t dacr;
b5ff1b31 8808
9ee6e8bb
PB
8809 /* Pagetable walk. */
8810 /* Lookup l1 descriptor. */
0480f69a 8811 if (!get_level1_table_address(env, mmu_idx, &table, address)) {
e389be16 8812 /* Section translation fault if page walk is disabled by PD0 or PD1 */
f989983e 8813 fi->type = ARMFault_Translation;
e389be16
FA
8814 goto do_fault;
8815 }
a614e698 8816 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
3795a6de 8817 mmu_idx, fi);
3b39d734
PM
8818 if (fi->type != ARMFault_None) {
8819 goto do_fault;
8820 }
9ee6e8bb 8821 type = (desc & 3);
dd4ebc2e 8822 domain = (desc >> 5) & 0x0f;
0480f69a
PM
8823 if (regime_el(env, mmu_idx) == 1) {
8824 dacr = env->cp15.dacr_ns;
8825 } else {
8826 dacr = env->cp15.dacr_s;
8827 }
8828 domain_prot = (dacr >> (domain * 2)) & 3;
9ee6e8bb 8829 if (type == 0) {
601d70b9 8830 /* Section translation fault. */
f989983e 8831 fi->type = ARMFault_Translation;
9ee6e8bb
PB
8832 goto do_fault;
8833 }
f989983e
PM
8834 if (type != 2) {
8835 level = 2;
8836 }
dd4ebc2e 8837 if (domain_prot == 0 || domain_prot == 2) {
f989983e 8838 fi->type = ARMFault_Domain;
9ee6e8bb
PB
8839 goto do_fault;
8840 }
8841 if (type == 2) {
8842 /* 1Mb section. */
8843 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
8844 ap = (desc >> 10) & 3;
d4c430a8 8845 *page_size = 1024 * 1024;
9ee6e8bb
PB
8846 } else {
8847 /* Lookup l2 entry. */
554b0b09
PM
8848 if (type == 1) {
8849 /* Coarse pagetable. */
8850 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
8851 } else {
8852 /* Fine pagetable. */
8853 table = (desc & 0xfffff000) | ((address >> 8) & 0xffc);
8854 }
a614e698 8855 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
3795a6de 8856 mmu_idx, fi);
3b39d734
PM
8857 if (fi->type != ARMFault_None) {
8858 goto do_fault;
8859 }
9ee6e8bb
PB
8860 switch (desc & 3) {
8861 case 0: /* Page translation fault. */
f989983e 8862 fi->type = ARMFault_Translation;
9ee6e8bb
PB
8863 goto do_fault;
8864 case 1: /* 64k page. */
8865 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
8866 ap = (desc >> (4 + ((address >> 13) & 6))) & 3;
d4c430a8 8867 *page_size = 0x10000;
ce819861 8868 break;
9ee6e8bb
PB
8869 case 2: /* 4k page. */
8870 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
c10f7fc3 8871 ap = (desc >> (4 + ((address >> 9) & 6))) & 3;
d4c430a8 8872 *page_size = 0x1000;
ce819861 8873 break;
fc1891c7 8874 case 3: /* 1k page, or ARMv6/XScale "extended small (4k) page" */
554b0b09 8875 if (type == 1) {
fc1891c7
PM
8876 /* ARMv6/XScale extended small page format */
8877 if (arm_feature(env, ARM_FEATURE_XSCALE)
8878 || arm_feature(env, ARM_FEATURE_V6)) {
554b0b09 8879 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
fc1891c7 8880 *page_size = 0x1000;
554b0b09 8881 } else {
fc1891c7
PM
8882 /* UNPREDICTABLE in ARMv5; we choose to take a
8883 * page translation fault.
8884 */
f989983e 8885 fi->type = ARMFault_Translation;
554b0b09
PM
8886 goto do_fault;
8887 }
8888 } else {
8889 phys_addr = (desc & 0xfffffc00) | (address & 0x3ff);
fc1891c7 8890 *page_size = 0x400;
554b0b09 8891 }
9ee6e8bb 8892 ap = (desc >> 4) & 3;
ce819861
PB
8893 break;
8894 default:
9ee6e8bb
PB
8895 /* Never happens, but compiler isn't smart enough to tell. */
8896 abort();
ce819861 8897 }
9ee6e8bb 8898 }
0fbf5238
AJ
8899 *prot = ap_to_rw_prot(env, mmu_idx, ap, domain_prot);
8900 *prot |= *prot ? PAGE_EXEC : 0;
8901 if (!(*prot & (1 << access_type))) {
9ee6e8bb 8902 /* Access permission fault. */
f989983e 8903 fi->type = ARMFault_Permission;
9ee6e8bb
PB
8904 goto do_fault;
8905 }
8906 *phys_ptr = phys_addr;
b7cc4e82 8907 return false;
9ee6e8bb 8908do_fault:
f989983e
PM
8909 fi->domain = domain;
8910 fi->level = level;
b7cc4e82 8911 return true;
9ee6e8bb
PB
8912}
8913
b7cc4e82 8914static bool get_phys_addr_v6(CPUARMState *env, uint32_t address,
03ae85f8 8915 MMUAccessType access_type, ARMMMUIdx mmu_idx,
b7cc4e82 8916 hwaddr *phys_ptr, MemTxAttrs *attrs, int *prot,
f06cf243 8917 target_ulong *page_size, ARMMMUFaultInfo *fi)
9ee6e8bb 8918{
70d74660 8919 CPUState *cs = CPU(arm_env_get_cpu(env));
f06cf243 8920 int level = 1;
9ee6e8bb
PB
8921 uint32_t table;
8922 uint32_t desc;
8923 uint32_t xn;
de9b05b8 8924 uint32_t pxn = 0;
9ee6e8bb
PB
8925 int type;
8926 int ap;
de9b05b8 8927 int domain = 0;
dd4ebc2e 8928 int domain_prot;
a8170e5e 8929 hwaddr phys_addr;
0480f69a 8930 uint32_t dacr;
8bf5b6a9 8931 bool ns;
9ee6e8bb
PB
8932
8933 /* Pagetable walk. */
8934 /* Lookup l1 descriptor. */
0480f69a 8935 if (!get_level1_table_address(env, mmu_idx, &table, address)) {
e389be16 8936 /* Section translation fault if page walk is disabled by PD0 or PD1 */
f06cf243 8937 fi->type = ARMFault_Translation;
e389be16
FA
8938 goto do_fault;
8939 }
a614e698 8940 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
3795a6de 8941 mmu_idx, fi);
3b39d734
PM
8942 if (fi->type != ARMFault_None) {
8943 goto do_fault;
8944 }
9ee6e8bb 8945 type = (desc & 3);
de9b05b8
PM
8946 if (type == 0 || (type == 3 && !arm_feature(env, ARM_FEATURE_PXN))) {
8947 /* Section translation fault, or attempt to use the encoding
8948 * which is Reserved on implementations without PXN.
8949 */
f06cf243 8950 fi->type = ARMFault_Translation;
9ee6e8bb 8951 goto do_fault;
de9b05b8
PM
8952 }
8953 if ((type == 1) || !(desc & (1 << 18))) {
8954 /* Page or Section. */
dd4ebc2e 8955 domain = (desc >> 5) & 0x0f;
9ee6e8bb 8956 }
0480f69a
PM
8957 if (regime_el(env, mmu_idx) == 1) {
8958 dacr = env->cp15.dacr_ns;
8959 } else {
8960 dacr = env->cp15.dacr_s;
8961 }
f06cf243
PM
8962 if (type == 1) {
8963 level = 2;
8964 }
0480f69a 8965 domain_prot = (dacr >> (domain * 2)) & 3;
dd4ebc2e 8966 if (domain_prot == 0 || domain_prot == 2) {
f06cf243
PM
8967 /* Section or Page domain fault */
8968 fi->type = ARMFault_Domain;
9ee6e8bb
PB
8969 goto do_fault;
8970 }
de9b05b8 8971 if (type != 1) {
9ee6e8bb
PB
8972 if (desc & (1 << 18)) {
8973 /* Supersection. */
8974 phys_addr = (desc & 0xff000000) | (address & 0x00ffffff);
4e42a6ca
SF
8975 phys_addr |= (uint64_t)extract32(desc, 20, 4) << 32;
8976 phys_addr |= (uint64_t)extract32(desc, 5, 4) << 36;
d4c430a8 8977 *page_size = 0x1000000;
b5ff1b31 8978 } else {
9ee6e8bb
PB
8979 /* Section. */
8980 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
d4c430a8 8981 *page_size = 0x100000;
b5ff1b31 8982 }
9ee6e8bb
PB
8983 ap = ((desc >> 10) & 3) | ((desc >> 13) & 4);
8984 xn = desc & (1 << 4);
de9b05b8 8985 pxn = desc & 1;
8bf5b6a9 8986 ns = extract32(desc, 19, 1);
9ee6e8bb 8987 } else {
de9b05b8
PM
8988 if (arm_feature(env, ARM_FEATURE_PXN)) {
8989 pxn = (desc >> 2) & 1;
8990 }
8bf5b6a9 8991 ns = extract32(desc, 3, 1);
9ee6e8bb
PB
8992 /* Lookup l2 entry. */
8993 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
a614e698 8994 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
3795a6de 8995 mmu_idx, fi);
3b39d734
PM
8996 if (fi->type != ARMFault_None) {
8997 goto do_fault;
8998 }
9ee6e8bb
PB
8999 ap = ((desc >> 4) & 3) | ((desc >> 7) & 4);
9000 switch (desc & 3) {
9001 case 0: /* Page translation fault. */
f06cf243 9002 fi->type = ARMFault_Translation;
b5ff1b31 9003 goto do_fault;
9ee6e8bb
PB
9004 case 1: /* 64k page. */
9005 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
9006 xn = desc & (1 << 15);
d4c430a8 9007 *page_size = 0x10000;
9ee6e8bb
PB
9008 break;
9009 case 2: case 3: /* 4k page. */
9010 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
9011 xn = desc & 1;
d4c430a8 9012 *page_size = 0x1000;
9ee6e8bb
PB
9013 break;
9014 default:
9015 /* Never happens, but compiler isn't smart enough to tell. */
9016 abort();
b5ff1b31 9017 }
9ee6e8bb 9018 }
dd4ebc2e 9019 if (domain_prot == 3) {
c0034328
JR
9020 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
9021 } else {
0480f69a 9022 if (pxn && !regime_is_user(env, mmu_idx)) {
de9b05b8
PM
9023 xn = 1;
9024 }
f06cf243
PM
9025 if (xn && access_type == MMU_INST_FETCH) {
9026 fi->type = ARMFault_Permission;
c0034328 9027 goto do_fault;
f06cf243 9028 }
9ee6e8bb 9029
d76951b6
AJ
9030 if (arm_feature(env, ARM_FEATURE_V6K) &&
9031 (regime_sctlr(env, mmu_idx) & SCTLR_AFE)) {
9032 /* The simplified model uses AP[0] as an access control bit. */
9033 if ((ap & 1) == 0) {
9034 /* Access flag fault. */
f06cf243 9035 fi->type = ARMFault_AccessFlag;
d76951b6
AJ
9036 goto do_fault;
9037 }
9038 *prot = simple_ap_to_rw_prot(env, mmu_idx, ap >> 1);
9039 } else {
9040 *prot = ap_to_rw_prot(env, mmu_idx, ap, domain_prot);
c0034328 9041 }
0fbf5238
AJ
9042 if (*prot && !xn) {
9043 *prot |= PAGE_EXEC;
9044 }
9045 if (!(*prot & (1 << access_type))) {
c0034328 9046 /* Access permission fault. */
f06cf243 9047 fi->type = ARMFault_Permission;
c0034328
JR
9048 goto do_fault;
9049 }
3ad493fc 9050 }
8bf5b6a9
PM
9051 if (ns) {
9052 /* The NS bit will (as required by the architecture) have no effect if
9053 * the CPU doesn't support TZ or this is a non-secure translation
9054 * regime, because the attribute will already be non-secure.
9055 */
9056 attrs->secure = false;
9057 }
9ee6e8bb 9058 *phys_ptr = phys_addr;
b7cc4e82 9059 return false;
b5ff1b31 9060do_fault:
f06cf243
PM
9061 fi->domain = domain;
9062 fi->level = level;
b7cc4e82 9063 return true;
b5ff1b31
FB
9064}
9065
1853d5a9 9066/*
a0e966c9 9067 * check_s2_mmu_setup
1853d5a9
EI
9068 * @cpu: ARMCPU
9069 * @is_aa64: True if the translation regime is in AArch64 state
9070 * @startlevel: Suggested starting level
9071 * @inputsize: Bitsize of IPAs
9072 * @stride: Page-table stride (See the ARM ARM)
9073 *
a0e966c9
EI
9074 * Returns true if the suggested S2 translation parameters are OK and
9075 * false otherwise.
1853d5a9 9076 */
a0e966c9
EI
9077static bool check_s2_mmu_setup(ARMCPU *cpu, bool is_aa64, int level,
9078 int inputsize, int stride)
1853d5a9 9079{
98d68ec2
EI
9080 const int grainsize = stride + 3;
9081 int startsizecheck;
9082
1853d5a9
EI
9083 /* Negative levels are never allowed. */
9084 if (level < 0) {
9085 return false;
9086 }
9087
98d68ec2
EI
9088 startsizecheck = inputsize - ((3 - level) * stride + grainsize);
9089 if (startsizecheck < 1 || startsizecheck > stride + 4) {
9090 return false;
9091 }
9092
1853d5a9 9093 if (is_aa64) {
3526423e 9094 CPUARMState *env = &cpu->env;
1853d5a9
EI
9095 unsigned int pamax = arm_pamax(cpu);
9096
9097 switch (stride) {
9098 case 13: /* 64KB Pages. */
9099 if (level == 0 || (level == 1 && pamax <= 42)) {
9100 return false;
9101 }
9102 break;
9103 case 11: /* 16KB Pages. */
9104 if (level == 0 || (level == 1 && pamax <= 40)) {
9105 return false;
9106 }
9107 break;
9108 case 9: /* 4KB Pages. */
9109 if (level == 0 && pamax <= 42) {
9110 return false;
9111 }
9112 break;
9113 default:
9114 g_assert_not_reached();
9115 }
3526423e
EI
9116
9117 /* Inputsize checks. */
9118 if (inputsize > pamax &&
9119 (arm_el_is_aa64(env, 1) || inputsize > 40)) {
9120 /* This is CONSTRAINED UNPREDICTABLE and we choose to fault. */
9121 return false;
9122 }
1853d5a9 9123 } else {
1853d5a9
EI
9124 /* AArch32 only supports 4KB pages. Assert on that. */
9125 assert(stride == 9);
9126
9127 if (level == 0) {
9128 return false;
9129 }
1853d5a9
EI
9130 }
9131 return true;
9132}
9133
5b2d261d
AB
9134/* Translate from the 4-bit stage 2 representation of
9135 * memory attributes (without cache-allocation hints) to
9136 * the 8-bit representation of the stage 1 MAIR registers
9137 * (which includes allocation hints).
9138 *
9139 * ref: shared/translation/attrs/S2AttrDecode()
9140 * .../S2ConvertAttrsHints()
9141 */
9142static uint8_t convert_stage2_attrs(CPUARMState *env, uint8_t s2attrs)
9143{
9144 uint8_t hiattr = extract32(s2attrs, 2, 2);
9145 uint8_t loattr = extract32(s2attrs, 0, 2);
9146 uint8_t hihint = 0, lohint = 0;
9147
9148 if (hiattr != 0) { /* normal memory */
9149 if ((env->cp15.hcr_el2 & HCR_CD) != 0) { /* cache disabled */
9150 hiattr = loattr = 1; /* non-cacheable */
9151 } else {
9152 if (hiattr != 1) { /* Write-through or write-back */
9153 hihint = 3; /* RW allocate */
9154 }
9155 if (loattr != 1) { /* Write-through or write-back */
9156 lohint = 3; /* RW allocate */
9157 }
9158 }
9159 }
9160
9161 return (hiattr << 6) | (hihint << 4) | (loattr << 2) | lohint;
9162}
9163
b7cc4e82 9164static bool get_phys_addr_lpae(CPUARMState *env, target_ulong address,
03ae85f8 9165 MMUAccessType access_type, ARMMMUIdx mmu_idx,
b7cc4e82 9166 hwaddr *phys_ptr, MemTxAttrs *txattrs, int *prot,
da909b2c 9167 target_ulong *page_size_ptr,
5b2d261d 9168 ARMMMUFaultInfo *fi, ARMCacheAttrs *cacheattrs)
3dde962f 9169{
1853d5a9
EI
9170 ARMCPU *cpu = arm_env_get_cpu(env);
9171 CPUState *cs = CPU(cpu);
3dde962f 9172 /* Read an LPAE long-descriptor translation table. */
da909b2c 9173 ARMFaultType fault_type = ARMFault_Translation;
1b4093ea 9174 uint32_t level;
0c5fbf3b 9175 uint32_t epd = 0;
1f4c8c18 9176 int32_t t0sz, t1sz;
2c8dd318 9177 uint32_t tg;
3dde962f
PM
9178 uint64_t ttbr;
9179 int ttbr_select;
dddb5223 9180 hwaddr descaddr, indexmask, indexmask_grainsize;
3dde962f
PM
9181 uint32_t tableattrs;
9182 target_ulong page_size;
9183 uint32_t attrs;
973a5434 9184 int32_t stride = 9;
6e99f762 9185 int32_t addrsize;
4ca6a051 9186 int inputsize;
2c8dd318 9187 int32_t tbi = 0;
0480f69a 9188 TCR *tcr = regime_tcr(env, mmu_idx);
d8e052b3 9189 int ap, ns, xn, pxn;
88e8add8
GB
9190 uint32_t el = regime_el(env, mmu_idx);
9191 bool ttbr1_valid = true;
6109769a 9192 uint64_t descaddrmask;
6e99f762 9193 bool aarch64 = arm_el_is_aa64(env, el);
0480f69a
PM
9194
9195 /* TODO:
88e8add8
GB
9196 * This code does not handle the different format TCR for VTCR_EL2.
9197 * This code also does not support shareability levels.
9198 * Attribute and permission bit handling should also be checked when adding
9199 * support for those page table walks.
0480f69a 9200 */
6e99f762 9201 if (aarch64) {
1b4093ea 9202 level = 0;
6e99f762 9203 addrsize = 64;
88e8add8 9204 if (el > 1) {
1edee470
EI
9205 if (mmu_idx != ARMMMUIdx_S2NS) {
9206 tbi = extract64(tcr->raw_tcr, 20, 1);
9207 }
88e8add8
GB
9208 } else {
9209 if (extract64(address, 55, 1)) {
9210 tbi = extract64(tcr->raw_tcr, 38, 1);
9211 } else {
9212 tbi = extract64(tcr->raw_tcr, 37, 1);
9213 }
9214 }
2c8dd318 9215 tbi *= 8;
88e8add8
GB
9216
9217 /* If we are in 64-bit EL2 or EL3 then there is no TTBR1, so mark it
9218 * invalid.
9219 */
9220 if (el > 1) {
9221 ttbr1_valid = false;
9222 }
d0a2cbce 9223 } else {
1b4093ea 9224 level = 1;
6e99f762 9225 addrsize = 32;
d0a2cbce
PM
9226 /* There is no TTBR1 for EL2 */
9227 if (el == 2) {
9228 ttbr1_valid = false;
9229 }
2c8dd318 9230 }
3dde962f
PM
9231
9232 /* Determine whether this address is in the region controlled by
9233 * TTBR0 or TTBR1 (or if it is in neither region and should fault).
9234 * This is a Non-secure PL0/1 stage 1 translation, so controlled by
9235 * TTBCR/TTBR0/TTBR1 in accordance with ARM ARM DDI0406C table B-32:
9236 */
6e99f762 9237 if (aarch64) {
4ee38098
EI
9238 /* AArch64 translation. */
9239 t0sz = extract32(tcr->raw_tcr, 0, 6);
2c8dd318
RH
9240 t0sz = MIN(t0sz, 39);
9241 t0sz = MAX(t0sz, 16);
4ee38098
EI
9242 } else if (mmu_idx != ARMMMUIdx_S2NS) {
9243 /* AArch32 stage 1 translation. */
9244 t0sz = extract32(tcr->raw_tcr, 0, 3);
9245 } else {
9246 /* AArch32 stage 2 translation. */
9247 bool sext = extract32(tcr->raw_tcr, 4, 1);
9248 bool sign = extract32(tcr->raw_tcr, 3, 1);
6e99f762
SS
9249 /* Address size is 40-bit for a stage 2 translation,
9250 * and t0sz can be negative (from -8 to 7),
9251 * so we need to adjust it to use the TTBR selecting logic below.
9252 */
9253 addrsize = 40;
9254 t0sz = sextract32(tcr->raw_tcr, 0, 4) + 8;
4ee38098
EI
9255
9256 /* If the sign-extend bit is not the same as t0sz[3], the result
9257 * is unpredictable. Flag this as a guest error. */
9258 if (sign != sext) {
9259 qemu_log_mask(LOG_GUEST_ERROR,
39cba610 9260 "AArch32: VTCR.S / VTCR.T0SZ[3] mismatch\n");
4ee38098 9261 }
2c8dd318 9262 }
1f4c8c18 9263 t1sz = extract32(tcr->raw_tcr, 16, 6);
6e99f762 9264 if (aarch64) {
2c8dd318
RH
9265 t1sz = MIN(t1sz, 39);
9266 t1sz = MAX(t1sz, 16);
9267 }
6e99f762 9268 if (t0sz && !extract64(address, addrsize - t0sz, t0sz - tbi)) {
3dde962f
PM
9269 /* there is a ttbr0 region and we are in it (high bits all zero) */
9270 ttbr_select = 0;
88e8add8 9271 } else if (ttbr1_valid && t1sz &&
6e99f762 9272 !extract64(~address, addrsize - t1sz, t1sz - tbi)) {
3dde962f
PM
9273 /* there is a ttbr1 region and we are in it (high bits all one) */
9274 ttbr_select = 1;
9275 } else if (!t0sz) {
9276 /* ttbr0 region is "everything not in the ttbr1 region" */
9277 ttbr_select = 0;
88e8add8 9278 } else if (!t1sz && ttbr1_valid) {
3dde962f
PM
9279 /* ttbr1 region is "everything not in the ttbr0 region" */
9280 ttbr_select = 1;
9281 } else {
9282 /* in the gap between the two regions, this is a Translation fault */
da909b2c 9283 fault_type = ARMFault_Translation;
3dde962f
PM
9284 goto do_fault;
9285 }
9286
9287 /* Note that QEMU ignores shareability and cacheability attributes,
9288 * so we don't need to do anything with the SH, ORGN, IRGN fields
9289 * in the TTBCR. Similarly, TTBCR:A1 selects whether we get the
9290 * ASID from TTBR0 or TTBR1, but QEMU's TLB doesn't currently
9291 * implement any ASID-like capability so we can ignore it (instead
9292 * we will always flush the TLB any time the ASID is changed).
9293 */
9294 if (ttbr_select == 0) {
aef878be 9295 ttbr = regime_ttbr(env, mmu_idx, 0);
0c5fbf3b
EI
9296 if (el < 2) {
9297 epd = extract32(tcr->raw_tcr, 7, 1);
9298 }
6e99f762 9299 inputsize = addrsize - t0sz;
2c8dd318 9300
11f136ee 9301 tg = extract32(tcr->raw_tcr, 14, 2);
2c8dd318 9302 if (tg == 1) { /* 64KB pages */
973a5434 9303 stride = 13;
2c8dd318
RH
9304 }
9305 if (tg == 2) { /* 16KB pages */
973a5434 9306 stride = 11;
2c8dd318 9307 }
3dde962f 9308 } else {
88e8add8
GB
9309 /* We should only be here if TTBR1 is valid */
9310 assert(ttbr1_valid);
9311
aef878be 9312 ttbr = regime_ttbr(env, mmu_idx, 1);
11f136ee 9313 epd = extract32(tcr->raw_tcr, 23, 1);
6e99f762 9314 inputsize = addrsize - t1sz;
2c8dd318 9315
11f136ee 9316 tg = extract32(tcr->raw_tcr, 30, 2);
2c8dd318 9317 if (tg == 3) { /* 64KB pages */
973a5434 9318 stride = 13;
2c8dd318
RH
9319 }
9320 if (tg == 1) { /* 16KB pages */
973a5434 9321 stride = 11;
2c8dd318 9322 }
3dde962f
PM
9323 }
9324
0480f69a 9325 /* Here we should have set up all the parameters for the translation:
6e99f762 9326 * inputsize, ttbr, epd, stride, tbi
0480f69a
PM
9327 */
9328
3dde962f 9329 if (epd) {
88e8add8
GB
9330 /* Translation table walk disabled => Translation fault on TLB miss
9331 * Note: This is always 0 on 64-bit EL2 and EL3.
9332 */
3dde962f
PM
9333 goto do_fault;
9334 }
9335
1853d5a9
EI
9336 if (mmu_idx != ARMMMUIdx_S2NS) {
9337 /* The starting level depends on the virtual address size (which can
9338 * be up to 48 bits) and the translation granule size. It indicates
9339 * the number of strides (stride bits at a time) needed to
9340 * consume the bits of the input address. In the pseudocode this is:
9341 * level = 4 - RoundUp((inputsize - grainsize) / stride)
9342 * where their 'inputsize' is our 'inputsize', 'grainsize' is
9343 * our 'stride + 3' and 'stride' is our 'stride'.
9344 * Applying the usual "rounded up m/n is (m+n-1)/n" and simplifying:
9345 * = 4 - (inputsize - stride - 3 + stride - 1) / stride
9346 * = 4 - (inputsize - 4) / stride;
9347 */
9348 level = 4 - (inputsize - 4) / stride;
9349 } else {
9350 /* For stage 2 translations the starting level is specified by the
9351 * VTCR_EL2.SL0 field (whose interpretation depends on the page size)
9352 */
1b4093ea
SS
9353 uint32_t sl0 = extract32(tcr->raw_tcr, 6, 2);
9354 uint32_t startlevel;
1853d5a9
EI
9355 bool ok;
9356
6e99f762 9357 if (!aarch64 || stride == 9) {
1853d5a9 9358 /* AArch32 or 4KB pages */
1b4093ea 9359 startlevel = 2 - sl0;
1853d5a9
EI
9360 } else {
9361 /* 16KB or 64KB pages */
1b4093ea 9362 startlevel = 3 - sl0;
1853d5a9
EI
9363 }
9364
9365 /* Check that the starting level is valid. */
6e99f762 9366 ok = check_s2_mmu_setup(cpu, aarch64, startlevel,
1b4093ea 9367 inputsize, stride);
1853d5a9 9368 if (!ok) {
da909b2c 9369 fault_type = ARMFault_Translation;
1853d5a9
EI
9370 goto do_fault;
9371 }
1b4093ea 9372 level = startlevel;
1853d5a9 9373 }
3dde962f 9374
dddb5223
SS
9375 indexmask_grainsize = (1ULL << (stride + 3)) - 1;
9376 indexmask = (1ULL << (inputsize - (stride * (4 - level)))) - 1;
3dde962f
PM
9377
9378 /* Now we can extract the actual base address from the TTBR */
2c8dd318 9379 descaddr = extract64(ttbr, 0, 48);
dddb5223 9380 descaddr &= ~indexmask;
3dde962f 9381
6109769a 9382 /* The address field in the descriptor goes up to bit 39 for ARMv7
dddb5223
SS
9383 * but up to bit 47 for ARMv8, but we use the descaddrmask
9384 * up to bit 39 for AArch32, because we don't need other bits in that case
9385 * to construct next descriptor address (anyway they should be all zeroes).
6109769a 9386 */
6e99f762 9387 descaddrmask = ((1ull << (aarch64 ? 48 : 40)) - 1) &
dddb5223 9388 ~indexmask_grainsize;
6109769a 9389
ebca90e4
PM
9390 /* Secure accesses start with the page table in secure memory and
9391 * can be downgraded to non-secure at any step. Non-secure accesses
9392 * remain non-secure. We implement this by just ORing in the NSTable/NS
9393 * bits at each step.
9394 */
9395 tableattrs = regime_is_secure(env, mmu_idx) ? 0 : (1 << 4);
3dde962f
PM
9396 for (;;) {
9397 uint64_t descriptor;
ebca90e4 9398 bool nstable;
3dde962f 9399
dddb5223 9400 descaddr |= (address >> (stride * (4 - level))) & indexmask;
2c8dd318 9401 descaddr &= ~7ULL;
ebca90e4 9402 nstable = extract32(tableattrs, 4, 1);
3795a6de 9403 descriptor = arm_ldq_ptw(cs, descaddr, !nstable, mmu_idx, fi);
3b39d734 9404 if (fi->type != ARMFault_None) {
37785977
EI
9405 goto do_fault;
9406 }
9407
3dde962f
PM
9408 if (!(descriptor & 1) ||
9409 (!(descriptor & 2) && (level == 3))) {
9410 /* Invalid, or the Reserved level 3 encoding */
9411 goto do_fault;
9412 }
6109769a 9413 descaddr = descriptor & descaddrmask;
3dde962f
PM
9414
9415 if ((descriptor & 2) && (level < 3)) {
9416 /* Table entry. The top five bits are attributes which may
9417 * propagate down through lower levels of the table (and
9418 * which are all arranged so that 0 means "no effect", so
9419 * we can gather them up by ORing in the bits at each level).
9420 */
9421 tableattrs |= extract64(descriptor, 59, 5);
9422 level++;
dddb5223 9423 indexmask = indexmask_grainsize;
3dde962f
PM
9424 continue;
9425 }
9426 /* Block entry at level 1 or 2, or page entry at level 3.
9427 * These are basically the same thing, although the number
9428 * of bits we pull in from the vaddr varies.
9429 */
973a5434 9430 page_size = (1ULL << ((stride * (4 - level)) + 3));
3dde962f 9431 descaddr |= (address & (page_size - 1));
6ab1a5ee 9432 /* Extract attributes from the descriptor */
d615efac
IC
9433 attrs = extract64(descriptor, 2, 10)
9434 | (extract64(descriptor, 52, 12) << 10);
6ab1a5ee
EI
9435
9436 if (mmu_idx == ARMMMUIdx_S2NS) {
9437 /* Stage 2 table descriptors do not include any attribute fields */
9438 break;
9439 }
9440 /* Merge in attributes from table descriptors */
3dde962f
PM
9441 attrs |= extract32(tableattrs, 0, 2) << 11; /* XN, PXN */
9442 attrs |= extract32(tableattrs, 3, 1) << 5; /* APTable[1] => AP[2] */
9443 /* The sense of AP[1] vs APTable[0] is reversed, as APTable[0] == 1
9444 * means "force PL1 access only", which means forcing AP[1] to 0.
9445 */
9446 if (extract32(tableattrs, 2, 1)) {
9447 attrs &= ~(1 << 4);
9448 }
ebca90e4 9449 attrs |= nstable << 3; /* NS */
3dde962f
PM
9450 break;
9451 }
9452 /* Here descaddr is the final physical address, and attributes
9453 * are all in attrs.
9454 */
da909b2c 9455 fault_type = ARMFault_AccessFlag;
3dde962f
PM
9456 if ((attrs & (1 << 8)) == 0) {
9457 /* Access flag */
9458 goto do_fault;
9459 }
d8e052b3
AJ
9460
9461 ap = extract32(attrs, 4, 2);
d8e052b3 9462 xn = extract32(attrs, 12, 1);
d8e052b3 9463
6ab1a5ee
EI
9464 if (mmu_idx == ARMMMUIdx_S2NS) {
9465 ns = true;
9466 *prot = get_S2prot(env, ap, xn);
9467 } else {
9468 ns = extract32(attrs, 3, 1);
9469 pxn = extract32(attrs, 11, 1);
6e99f762 9470 *prot = get_S1prot(env, mmu_idx, aarch64, ap, ns, xn, pxn);
6ab1a5ee 9471 }
d8e052b3 9472
da909b2c 9473 fault_type = ARMFault_Permission;
d8e052b3 9474 if (!(*prot & (1 << access_type))) {
3dde962f
PM
9475 goto do_fault;
9476 }
3dde962f 9477
8bf5b6a9
PM
9478 if (ns) {
9479 /* The NS bit will (as required by the architecture) have no effect if
9480 * the CPU doesn't support TZ or this is a non-secure translation
9481 * regime, because the attribute will already be non-secure.
9482 */
9483 txattrs->secure = false;
9484 }
5b2d261d
AB
9485
9486 if (cacheattrs != NULL) {
9487 if (mmu_idx == ARMMMUIdx_S2NS) {
9488 cacheattrs->attrs = convert_stage2_attrs(env,
9489 extract32(attrs, 0, 4));
9490 } else {
9491 /* Index into MAIR registers for cache attributes */
9492 uint8_t attrindx = extract32(attrs, 0, 3);
9493 uint64_t mair = env->cp15.mair_el[regime_el(env, mmu_idx)];
9494 assert(attrindx <= 7);
9495 cacheattrs->attrs = extract64(mair, attrindx * 8, 8);
9496 }
9497 cacheattrs->shareability = extract32(attrs, 6, 2);
9498 }
9499
3dde962f
PM
9500 *phys_ptr = descaddr;
9501 *page_size_ptr = page_size;
b7cc4e82 9502 return false;
3dde962f
PM
9503
9504do_fault:
da909b2c
PM
9505 fi->type = fault_type;
9506 fi->level = level;
37785977
EI
9507 /* Tag the error as S2 for failed S1 PTW at S2 or ordinary S2. */
9508 fi->stage2 = fi->s1ptw || (mmu_idx == ARMMMUIdx_S2NS);
b7cc4e82 9509 return true;
3dde962f
PM
9510}
9511
f6bda88f
PC
9512static inline void get_phys_addr_pmsav7_default(CPUARMState *env,
9513 ARMMMUIdx mmu_idx,
9514 int32_t address, int *prot)
9515{
3a00d560
MD
9516 if (!arm_feature(env, ARM_FEATURE_M)) {
9517 *prot = PAGE_READ | PAGE_WRITE;
9518 switch (address) {
9519 case 0xF0000000 ... 0xFFFFFFFF:
9520 if (regime_sctlr(env, mmu_idx) & SCTLR_V) {
9521 /* hivecs execing is ok */
9522 *prot |= PAGE_EXEC;
9523 }
9524 break;
9525 case 0x00000000 ... 0x7FFFFFFF:
f6bda88f 9526 *prot |= PAGE_EXEC;
3a00d560
MD
9527 break;
9528 }
9529 } else {
9530 /* Default system address map for M profile cores.
9531 * The architecture specifies which regions are execute-never;
9532 * at the MPU level no other checks are defined.
9533 */
9534 switch (address) {
9535 case 0x00000000 ... 0x1fffffff: /* ROM */
9536 case 0x20000000 ... 0x3fffffff: /* SRAM */
9537 case 0x60000000 ... 0x7fffffff: /* RAM */
9538 case 0x80000000 ... 0x9fffffff: /* RAM */
9539 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
9540 break;
9541 case 0x40000000 ... 0x5fffffff: /* Peripheral */
9542 case 0xa0000000 ... 0xbfffffff: /* Device */
9543 case 0xc0000000 ... 0xdfffffff: /* Device */
9544 case 0xe0000000 ... 0xffffffff: /* System */
9545 *prot = PAGE_READ | PAGE_WRITE;
9546 break;
9547 default:
9548 g_assert_not_reached();
f6bda88f 9549 }
f6bda88f 9550 }
f6bda88f
PC
9551}
9552
29c483a5
MD
9553static bool pmsav7_use_background_region(ARMCPU *cpu,
9554 ARMMMUIdx mmu_idx, bool is_user)
9555{
9556 /* Return true if we should use the default memory map as a
9557 * "background" region if there are no hits against any MPU regions.
9558 */
9559 CPUARMState *env = &cpu->env;
9560
9561 if (is_user) {
9562 return false;
9563 }
9564
9565 if (arm_feature(env, ARM_FEATURE_M)) {
ecf5e8ea
PM
9566 return env->v7m.mpu_ctrl[regime_is_secure(env, mmu_idx)]
9567 & R_V7M_MPU_CTRL_PRIVDEFENA_MASK;
29c483a5
MD
9568 } else {
9569 return regime_sctlr(env, mmu_idx) & SCTLR_BR;
9570 }
9571}
9572
38aaa60c
PM
9573static inline bool m_is_ppb_region(CPUARMState *env, uint32_t address)
9574{
9575 /* True if address is in the M profile PPB region 0xe0000000 - 0xe00fffff */
9576 return arm_feature(env, ARM_FEATURE_M) &&
9577 extract32(address, 20, 12) == 0xe00;
9578}
9579
bf446a11
PM
9580static inline bool m_is_system_region(CPUARMState *env, uint32_t address)
9581{
9582 /* True if address is in the M profile system region
9583 * 0xe0000000 - 0xffffffff
9584 */
9585 return arm_feature(env, ARM_FEATURE_M) && extract32(address, 29, 3) == 0x7;
9586}
9587
f6bda88f 9588static bool get_phys_addr_pmsav7(CPUARMState *env, uint32_t address,
03ae85f8 9589 MMUAccessType access_type, ARMMMUIdx mmu_idx,
9375ad15
PM
9590 hwaddr *phys_ptr, int *prot,
9591 ARMMMUFaultInfo *fi)
f6bda88f
PC
9592{
9593 ARMCPU *cpu = arm_env_get_cpu(env);
9594 int n;
9595 bool is_user = regime_is_user(env, mmu_idx);
9596
9597 *phys_ptr = address;
9598 *prot = 0;
9599
38aaa60c
PM
9600 if (regime_translation_disabled(env, mmu_idx) ||
9601 m_is_ppb_region(env, address)) {
9602 /* MPU disabled or M profile PPB access: use default memory map.
9603 * The other case which uses the default memory map in the
9604 * v7M ARM ARM pseudocode is exception vector reads from the vector
9605 * table. In QEMU those accesses are done in arm_v7m_load_vector(),
9606 * which always does a direct read using address_space_ldl(), rather
9607 * than going via this function, so we don't need to check that here.
9608 */
f6bda88f
PC
9609 get_phys_addr_pmsav7_default(env, mmu_idx, address, prot);
9610 } else { /* MPU enabled */
9611 for (n = (int)cpu->pmsav7_dregion - 1; n >= 0; n--) {
9612 /* region search */
9613 uint32_t base = env->pmsav7.drbar[n];
9614 uint32_t rsize = extract32(env->pmsav7.drsr[n], 1, 5);
9615 uint32_t rmask;
9616 bool srdis = false;
9617
9618 if (!(env->pmsav7.drsr[n] & 0x1)) {
9619 continue;
9620 }
9621
9622 if (!rsize) {
c9f9f124
MD
9623 qemu_log_mask(LOG_GUEST_ERROR,
9624 "DRSR[%d]: Rsize field cannot be 0\n", n);
f6bda88f
PC
9625 continue;
9626 }
9627 rsize++;
9628 rmask = (1ull << rsize) - 1;
9629
9630 if (base & rmask) {
c9f9f124
MD
9631 qemu_log_mask(LOG_GUEST_ERROR,
9632 "DRBAR[%d]: 0x%" PRIx32 " misaligned "
9633 "to DRSR region size, mask = 0x%" PRIx32 "\n",
9634 n, base, rmask);
f6bda88f
PC
9635 continue;
9636 }
9637
9638 if (address < base || address > base + rmask) {
9639 continue;
9640 }
9641
9642 /* Region matched */
9643
9644 if (rsize >= 8) { /* no subregions for regions < 256 bytes */
9645 int i, snd;
9646 uint32_t srdis_mask;
9647
9648 rsize -= 3; /* sub region size (power of 2) */
9649 snd = ((address - base) >> rsize) & 0x7;
9650 srdis = extract32(env->pmsav7.drsr[n], snd + 8, 1);
9651
9652 srdis_mask = srdis ? 0x3 : 0x0;
9653 for (i = 2; i <= 8 && rsize < TARGET_PAGE_BITS; i *= 2) {
9654 /* This will check in groups of 2, 4 and then 8, whether
9655 * the subregion bits are consistent. rsize is incremented
9656 * back up to give the region size, considering consistent
9657 * adjacent subregions as one region. Stop testing if rsize
9658 * is already big enough for an entire QEMU page.
9659 */
9660 int snd_rounded = snd & ~(i - 1);
9661 uint32_t srdis_multi = extract32(env->pmsav7.drsr[n],
9662 snd_rounded + 8, i);
9663 if (srdis_mask ^ srdis_multi) {
9664 break;
9665 }
9666 srdis_mask = (srdis_mask << i) | srdis_mask;
9667 rsize++;
9668 }
9669 }
9670 if (rsize < TARGET_PAGE_BITS) {
c9f9f124 9671 qemu_log_mask(LOG_UNIMP,
8aec759b
PM
9672 "DRSR[%d]: No support for MPU (sub)region size of"
9673 " %" PRIu32 " bytes. Minimum is %d.\n",
9674 n, (1 << rsize), TARGET_PAGE_SIZE);
f6bda88f
PC
9675 continue;
9676 }
9677 if (srdis) {
9678 continue;
9679 }
9680 break;
9681 }
9682
9683 if (n == -1) { /* no hits */
29c483a5 9684 if (!pmsav7_use_background_region(cpu, mmu_idx, is_user)) {
f6bda88f 9685 /* background fault */
9375ad15 9686 fi->type = ARMFault_Background;
f6bda88f
PC
9687 return true;
9688 }
9689 get_phys_addr_pmsav7_default(env, mmu_idx, address, prot);
9690 } else { /* a MPU hit! */
9691 uint32_t ap = extract32(env->pmsav7.dracr[n], 8, 3);
bf446a11
PM
9692 uint32_t xn = extract32(env->pmsav7.dracr[n], 12, 1);
9693
9694 if (m_is_system_region(env, address)) {
9695 /* System space is always execute never */
9696 xn = 1;
9697 }
f6bda88f
PC
9698
9699 if (is_user) { /* User mode AP bit decoding */
9700 switch (ap) {
9701 case 0:
9702 case 1:
9703 case 5:
9704 break; /* no access */
9705 case 3:
9706 *prot |= PAGE_WRITE;
9707 /* fall through */
9708 case 2:
9709 case 6:
9710 *prot |= PAGE_READ | PAGE_EXEC;
9711 break;
8638f1ad
PM
9712 case 7:
9713 /* for v7M, same as 6; for R profile a reserved value */
9714 if (arm_feature(env, ARM_FEATURE_M)) {
9715 *prot |= PAGE_READ | PAGE_EXEC;
9716 break;
9717 }
9718 /* fall through */
f6bda88f
PC
9719 default:
9720 qemu_log_mask(LOG_GUEST_ERROR,
c9f9f124
MD
9721 "DRACR[%d]: Bad value for AP bits: 0x%"
9722 PRIx32 "\n", n, ap);
f6bda88f
PC
9723 }
9724 } else { /* Priv. mode AP bits decoding */
9725 switch (ap) {
9726 case 0:
9727 break; /* no access */
9728 case 1:
9729 case 2:
9730 case 3:
9731 *prot |= PAGE_WRITE;
9732 /* fall through */
9733 case 5:
9734 case 6:
9735 *prot |= PAGE_READ | PAGE_EXEC;
9736 break;
8638f1ad
PM
9737 case 7:
9738 /* for v7M, same as 6; for R profile a reserved value */
9739 if (arm_feature(env, ARM_FEATURE_M)) {
9740 *prot |= PAGE_READ | PAGE_EXEC;
9741 break;
9742 }
9743 /* fall through */
f6bda88f
PC
9744 default:
9745 qemu_log_mask(LOG_GUEST_ERROR,
c9f9f124
MD
9746 "DRACR[%d]: Bad value for AP bits: 0x%"
9747 PRIx32 "\n", n, ap);
f6bda88f
PC
9748 }
9749 }
9750
9751 /* execute never */
bf446a11 9752 if (xn) {
f6bda88f
PC
9753 *prot &= ~PAGE_EXEC;
9754 }
9755 }
9756 }
9757
9375ad15
PM
9758 fi->type = ARMFault_Permission;
9759 fi->level = 1;
f6bda88f
PC
9760 return !(*prot & (1 << access_type));
9761}
9762
35337cc3
PM
9763static bool v8m_is_sau_exempt(CPUARMState *env,
9764 uint32_t address, MMUAccessType access_type)
9765{
9766 /* The architecture specifies that certain address ranges are
9767 * exempt from v8M SAU/IDAU checks.
9768 */
9769 return
9770 (access_type == MMU_INST_FETCH && m_is_system_region(env, address)) ||
9771 (address >= 0xe0000000 && address <= 0xe0002fff) ||
9772 (address >= 0xe000e000 && address <= 0xe000efff) ||
9773 (address >= 0xe002e000 && address <= 0xe002efff) ||
9774 (address >= 0xe0040000 && address <= 0xe0041fff) ||
9775 (address >= 0xe00ff000 && address <= 0xe00fffff);
9776}
9777
9778static void v8m_security_lookup(CPUARMState *env, uint32_t address,
9779 MMUAccessType access_type, ARMMMUIdx mmu_idx,
9780 V8M_SAttributes *sattrs)
9781{
9782 /* Look up the security attributes for this address. Compare the
9783 * pseudocode SecurityCheck() function.
9784 * We assume the caller has zero-initialized *sattrs.
9785 */
9786 ARMCPU *cpu = arm_env_get_cpu(env);
9787 int r;
181962fd
PM
9788 bool idau_exempt = false, idau_ns = true, idau_nsc = true;
9789 int idau_region = IREGION_NOTVALID;
35337cc3 9790
181962fd
PM
9791 if (cpu->idau) {
9792 IDAUInterfaceClass *iic = IDAU_INTERFACE_GET_CLASS(cpu->idau);
9793 IDAUInterface *ii = IDAU_INTERFACE(cpu->idau);
9794
9795 iic->check(ii, address, &idau_region, &idau_exempt, &idau_ns,
9796 &idau_nsc);
9797 }
35337cc3
PM
9798
9799 if (access_type == MMU_INST_FETCH && extract32(address, 28, 4) == 0xf) {
9800 /* 0xf0000000..0xffffffff is always S for insn fetches */
9801 return;
9802 }
9803
181962fd 9804 if (idau_exempt || v8m_is_sau_exempt(env, address, access_type)) {
35337cc3
PM
9805 sattrs->ns = !regime_is_secure(env, mmu_idx);
9806 return;
9807 }
9808
181962fd
PM
9809 if (idau_region != IREGION_NOTVALID) {
9810 sattrs->irvalid = true;
9811 sattrs->iregion = idau_region;
9812 }
9813
35337cc3
PM
9814 switch (env->sau.ctrl & 3) {
9815 case 0: /* SAU.ENABLE == 0, SAU.ALLNS == 0 */
9816 break;
9817 case 2: /* SAU.ENABLE == 0, SAU.ALLNS == 1 */
9818 sattrs->ns = true;
9819 break;
9820 default: /* SAU.ENABLE == 1 */
9821 for (r = 0; r < cpu->sau_sregion; r++) {
9822 if (env->sau.rlar[r] & 1) {
9823 uint32_t base = env->sau.rbar[r] & ~0x1f;
9824 uint32_t limit = env->sau.rlar[r] | 0x1f;
9825
9826 if (base <= address && limit >= address) {
9827 if (sattrs->srvalid) {
9828 /* If we hit in more than one region then we must report
9829 * as Secure, not NS-Callable, with no valid region
9830 * number info.
9831 */
9832 sattrs->ns = false;
9833 sattrs->nsc = false;
9834 sattrs->sregion = 0;
9835 sattrs->srvalid = false;
9836 break;
9837 } else {
9838 if (env->sau.rlar[r] & 2) {
9839 sattrs->nsc = true;
9840 } else {
9841 sattrs->ns = true;
9842 }
9843 sattrs->srvalid = true;
9844 sattrs->sregion = r;
9845 }
9846 }
9847 }
9848 }
9849
181962fd
PM
9850 /* The IDAU will override the SAU lookup results if it specifies
9851 * higher security than the SAU does.
9852 */
9853 if (!idau_ns) {
9854 if (sattrs->ns || (!idau_nsc && sattrs->nsc)) {
9855 sattrs->ns = false;
9856 sattrs->nsc = idau_nsc;
9857 }
9858 }
35337cc3
PM
9859 break;
9860 }
9861}
9862
54317c0f
PM
9863static bool pmsav8_mpu_lookup(CPUARMState *env, uint32_t address,
9864 MMUAccessType access_type, ARMMMUIdx mmu_idx,
9865 hwaddr *phys_ptr, MemTxAttrs *txattrs,
3f551b5b 9866 int *prot, ARMMMUFaultInfo *fi, uint32_t *mregion)
54317c0f
PM
9867{
9868 /* Perform a PMSAv8 MPU lookup (without also doing the SAU check
9869 * that a full phys-to-virt translation does).
9870 * mregion is (if not NULL) set to the region number which matched,
9871 * or -1 if no region number is returned (MPU off, address did not
9872 * hit a region, address hit in multiple regions).
9873 */
504e3cc3
PM
9874 ARMCPU *cpu = arm_env_get_cpu(env);
9875 bool is_user = regime_is_user(env, mmu_idx);
62c58ee0 9876 uint32_t secure = regime_is_secure(env, mmu_idx);
504e3cc3
PM
9877 int n;
9878 int matchregion = -1;
9879 bool hit = false;
9880
9881 *phys_ptr = address;
9882 *prot = 0;
54317c0f
PM
9883 if (mregion) {
9884 *mregion = -1;
35337cc3
PM
9885 }
9886
504e3cc3
PM
9887 /* Unlike the ARM ARM pseudocode, we don't need to check whether this
9888 * was an exception vector read from the vector table (which is always
9889 * done using the default system address map), because those accesses
9890 * are done in arm_v7m_load_vector(), which always does a direct
9891 * read using address_space_ldl(), rather than going via this function.
9892 */
9893 if (regime_translation_disabled(env, mmu_idx)) { /* MPU disabled */
9894 hit = true;
9895 } else if (m_is_ppb_region(env, address)) {
9896 hit = true;
9897 } else if (pmsav7_use_background_region(cpu, mmu_idx, is_user)) {
9898 hit = true;
9899 } else {
9900 for (n = (int)cpu->pmsav7_dregion - 1; n >= 0; n--) {
9901 /* region search */
9902 /* Note that the base address is bits [31:5] from the register
9903 * with bits [4:0] all zeroes, but the limit address is bits
9904 * [31:5] from the register with bits [4:0] all ones.
9905 */
62c58ee0
PM
9906 uint32_t base = env->pmsav8.rbar[secure][n] & ~0x1f;
9907 uint32_t limit = env->pmsav8.rlar[secure][n] | 0x1f;
504e3cc3 9908
62c58ee0 9909 if (!(env->pmsav8.rlar[secure][n] & 0x1)) {
504e3cc3
PM
9910 /* Region disabled */
9911 continue;
9912 }
9913
9914 if (address < base || address > limit) {
9915 continue;
9916 }
9917
9918 if (hit) {
9919 /* Multiple regions match -- always a failure (unlike
9920 * PMSAv7 where highest-numbered-region wins)
9921 */
3f551b5b
PM
9922 fi->type = ARMFault_Permission;
9923 fi->level = 1;
504e3cc3
PM
9924 return true;
9925 }
9926
9927 matchregion = n;
9928 hit = true;
9929
9930 if (base & ~TARGET_PAGE_MASK) {
9931 qemu_log_mask(LOG_UNIMP,
9932 "MPU_RBAR[%d]: No support for MPU region base"
9933 "address of 0x%" PRIx32 ". Minimum alignment is "
9934 "%d\n",
9935 n, base, TARGET_PAGE_BITS);
9936 continue;
9937 }
9938 if ((limit + 1) & ~TARGET_PAGE_MASK) {
9939 qemu_log_mask(LOG_UNIMP,
9940 "MPU_RBAR[%d]: No support for MPU region limit"
9941 "address of 0x%" PRIx32 ". Minimum alignment is "
9942 "%d\n",
9943 n, limit, TARGET_PAGE_BITS);
9944 continue;
9945 }
9946 }
9947 }
9948
9949 if (!hit) {
9950 /* background fault */
3f551b5b 9951 fi->type = ARMFault_Background;
504e3cc3
PM
9952 return true;
9953 }
9954
9955 if (matchregion == -1) {
9956 /* hit using the background region */
9957 get_phys_addr_pmsav7_default(env, mmu_idx, address, prot);
9958 } else {
62c58ee0
PM
9959 uint32_t ap = extract32(env->pmsav8.rbar[secure][matchregion], 1, 2);
9960 uint32_t xn = extract32(env->pmsav8.rbar[secure][matchregion], 0, 1);
504e3cc3
PM
9961
9962 if (m_is_system_region(env, address)) {
9963 /* System space is always execute never */
9964 xn = 1;
9965 }
9966
9967 *prot = simple_ap_to_rw_prot(env, mmu_idx, ap);
9968 if (*prot && !xn) {
9969 *prot |= PAGE_EXEC;
9970 }
9971 /* We don't need to look the attribute up in the MAIR0/MAIR1
9972 * registers because that only tells us about cacheability.
9973 */
54317c0f
PM
9974 if (mregion) {
9975 *mregion = matchregion;
9976 }
504e3cc3
PM
9977 }
9978
3f551b5b
PM
9979 fi->type = ARMFault_Permission;
9980 fi->level = 1;
504e3cc3
PM
9981 return !(*prot & (1 << access_type));
9982}
9983
54317c0f
PM
9984
9985static bool get_phys_addr_pmsav8(CPUARMState *env, uint32_t address,
9986 MMUAccessType access_type, ARMMMUIdx mmu_idx,
9987 hwaddr *phys_ptr, MemTxAttrs *txattrs,
3f551b5b 9988 int *prot, ARMMMUFaultInfo *fi)
54317c0f
PM
9989{
9990 uint32_t secure = regime_is_secure(env, mmu_idx);
9991 V8M_SAttributes sattrs = {};
9992
9993 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
9994 v8m_security_lookup(env, address, access_type, mmu_idx, &sattrs);
9995 if (access_type == MMU_INST_FETCH) {
9996 /* Instruction fetches always use the MMU bank and the
9997 * transaction attribute determined by the fetch address,
9998 * regardless of CPU state. This is painful for QEMU
9999 * to handle, because it would mean we need to encode
10000 * into the mmu_idx not just the (user, negpri) information
10001 * for the current security state but also that for the
10002 * other security state, which would balloon the number
10003 * of mmu_idx values needed alarmingly.
10004 * Fortunately we can avoid this because it's not actually
10005 * possible to arbitrarily execute code from memory with
10006 * the wrong security attribute: it will always generate
10007 * an exception of some kind or another, apart from the
10008 * special case of an NS CPU executing an SG instruction
10009 * in S&NSC memory. So we always just fail the translation
10010 * here and sort things out in the exception handler
10011 * (including possibly emulating an SG instruction).
10012 */
10013 if (sattrs.ns != !secure) {
3f551b5b
PM
10014 if (sattrs.nsc) {
10015 fi->type = ARMFault_QEMU_NSCExec;
10016 } else {
10017 fi->type = ARMFault_QEMU_SFault;
10018 }
54317c0f
PM
10019 *phys_ptr = address;
10020 *prot = 0;
10021 return true;
10022 }
10023 } else {
10024 /* For data accesses we always use the MMU bank indicated
10025 * by the current CPU state, but the security attributes
10026 * might downgrade a secure access to nonsecure.
10027 */
10028 if (sattrs.ns) {
10029 txattrs->secure = false;
10030 } else if (!secure) {
10031 /* NS access to S memory must fault.
10032 * Architecturally we should first check whether the
10033 * MPU information for this address indicates that we
10034 * are doing an unaligned access to Device memory, which
10035 * should generate a UsageFault instead. QEMU does not
10036 * currently check for that kind of unaligned access though.
10037 * If we added it we would need to do so as a special case
10038 * for M_FAKE_FSR_SFAULT in arm_v7m_cpu_do_interrupt().
10039 */
3f551b5b 10040 fi->type = ARMFault_QEMU_SFault;
54317c0f
PM
10041 *phys_ptr = address;
10042 *prot = 0;
10043 return true;
10044 }
10045 }
10046 }
10047
10048 return pmsav8_mpu_lookup(env, address, access_type, mmu_idx, phys_ptr,
3f551b5b 10049 txattrs, prot, fi, NULL);
54317c0f
PM
10050}
10051
13689d43 10052static bool get_phys_addr_pmsav5(CPUARMState *env, uint32_t address,
03ae85f8 10053 MMUAccessType access_type, ARMMMUIdx mmu_idx,
53a4e5c5
PM
10054 hwaddr *phys_ptr, int *prot,
10055 ARMMMUFaultInfo *fi)
9ee6e8bb
PB
10056{
10057 int n;
10058 uint32_t mask;
10059 uint32_t base;
0480f69a 10060 bool is_user = regime_is_user(env, mmu_idx);
9ee6e8bb 10061
3279adb9
PM
10062 if (regime_translation_disabled(env, mmu_idx)) {
10063 /* MPU disabled. */
10064 *phys_ptr = address;
10065 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
10066 return false;
10067 }
10068
9ee6e8bb
PB
10069 *phys_ptr = address;
10070 for (n = 7; n >= 0; n--) {
554b0b09 10071 base = env->cp15.c6_region[n];
87c3d486 10072 if ((base & 1) == 0) {
554b0b09 10073 continue;
87c3d486 10074 }
554b0b09
PM
10075 mask = 1 << ((base >> 1) & 0x1f);
10076 /* Keep this shift separate from the above to avoid an
10077 (undefined) << 32. */
10078 mask = (mask << 1) - 1;
87c3d486 10079 if (((base ^ address) & ~mask) == 0) {
554b0b09 10080 break;
87c3d486 10081 }
9ee6e8bb 10082 }
87c3d486 10083 if (n < 0) {
53a4e5c5 10084 fi->type = ARMFault_Background;
b7cc4e82 10085 return true;
87c3d486 10086 }
9ee6e8bb 10087
03ae85f8 10088 if (access_type == MMU_INST_FETCH) {
7e09797c 10089 mask = env->cp15.pmsav5_insn_ap;
9ee6e8bb 10090 } else {
7e09797c 10091 mask = env->cp15.pmsav5_data_ap;
9ee6e8bb
PB
10092 }
10093 mask = (mask >> (n * 4)) & 0xf;
10094 switch (mask) {
10095 case 0:
53a4e5c5
PM
10096 fi->type = ARMFault_Permission;
10097 fi->level = 1;
b7cc4e82 10098 return true;
9ee6e8bb 10099 case 1:
87c3d486 10100 if (is_user) {
53a4e5c5
PM
10101 fi->type = ARMFault_Permission;
10102 fi->level = 1;
b7cc4e82 10103 return true;
87c3d486 10104 }
554b0b09
PM
10105 *prot = PAGE_READ | PAGE_WRITE;
10106 break;
9ee6e8bb 10107 case 2:
554b0b09 10108 *prot = PAGE_READ;
87c3d486 10109 if (!is_user) {
554b0b09 10110 *prot |= PAGE_WRITE;
87c3d486 10111 }
554b0b09 10112 break;
9ee6e8bb 10113 case 3:
554b0b09
PM
10114 *prot = PAGE_READ | PAGE_WRITE;
10115 break;
9ee6e8bb 10116 case 5:
87c3d486 10117 if (is_user) {
53a4e5c5
PM
10118 fi->type = ARMFault_Permission;
10119 fi->level = 1;
b7cc4e82 10120 return true;
87c3d486 10121 }
554b0b09
PM
10122 *prot = PAGE_READ;
10123 break;
9ee6e8bb 10124 case 6:
554b0b09
PM
10125 *prot = PAGE_READ;
10126 break;
9ee6e8bb 10127 default:
554b0b09 10128 /* Bad permission. */
53a4e5c5
PM
10129 fi->type = ARMFault_Permission;
10130 fi->level = 1;
b7cc4e82 10131 return true;
9ee6e8bb 10132 }
3ad493fc 10133 *prot |= PAGE_EXEC;
b7cc4e82 10134 return false;
9ee6e8bb
PB
10135}
10136
5b2d261d
AB
10137/* Combine either inner or outer cacheability attributes for normal
10138 * memory, according to table D4-42 and pseudocode procedure
10139 * CombineS1S2AttrHints() of ARM DDI 0487B.b (the ARMv8 ARM).
10140 *
10141 * NB: only stage 1 includes allocation hints (RW bits), leading to
10142 * some asymmetry.
10143 */
10144static uint8_t combine_cacheattr_nibble(uint8_t s1, uint8_t s2)
10145{
10146 if (s1 == 4 || s2 == 4) {
10147 /* non-cacheable has precedence */
10148 return 4;
10149 } else if (extract32(s1, 2, 2) == 0 || extract32(s1, 2, 2) == 2) {
10150 /* stage 1 write-through takes precedence */
10151 return s1;
10152 } else if (extract32(s2, 2, 2) == 2) {
10153 /* stage 2 write-through takes precedence, but the allocation hint
10154 * is still taken from stage 1
10155 */
10156 return (2 << 2) | extract32(s1, 0, 2);
10157 } else { /* write-back */
10158 return s1;
10159 }
10160}
10161
10162/* Combine S1 and S2 cacheability/shareability attributes, per D4.5.4
10163 * and CombineS1S2Desc()
10164 *
10165 * @s1: Attributes from stage 1 walk
10166 * @s2: Attributes from stage 2 walk
10167 */
10168static ARMCacheAttrs combine_cacheattrs(ARMCacheAttrs s1, ARMCacheAttrs s2)
10169{
10170 uint8_t s1lo = extract32(s1.attrs, 0, 4), s2lo = extract32(s2.attrs, 0, 4);
10171 uint8_t s1hi = extract32(s1.attrs, 4, 4), s2hi = extract32(s2.attrs, 4, 4);
10172 ARMCacheAttrs ret;
10173
10174 /* Combine shareability attributes (table D4-43) */
10175 if (s1.shareability == 2 || s2.shareability == 2) {
10176 /* if either are outer-shareable, the result is outer-shareable */
10177 ret.shareability = 2;
10178 } else if (s1.shareability == 3 || s2.shareability == 3) {
10179 /* if either are inner-shareable, the result is inner-shareable */
10180 ret.shareability = 3;
10181 } else {
10182 /* both non-shareable */
10183 ret.shareability = 0;
10184 }
10185
10186 /* Combine memory type and cacheability attributes */
10187 if (s1hi == 0 || s2hi == 0) {
10188 /* Device has precedence over normal */
10189 if (s1lo == 0 || s2lo == 0) {
10190 /* nGnRnE has precedence over anything */
10191 ret.attrs = 0;
10192 } else if (s1lo == 4 || s2lo == 4) {
10193 /* non-Reordering has precedence over Reordering */
10194 ret.attrs = 4; /* nGnRE */
10195 } else if (s1lo == 8 || s2lo == 8) {
10196 /* non-Gathering has precedence over Gathering */
10197 ret.attrs = 8; /* nGRE */
10198 } else {
10199 ret.attrs = 0xc; /* GRE */
10200 }
10201
10202 /* Any location for which the resultant memory type is any
10203 * type of Device memory is always treated as Outer Shareable.
10204 */
10205 ret.shareability = 2;
10206 } else { /* Normal memory */
10207 /* Outer/inner cacheability combine independently */
10208 ret.attrs = combine_cacheattr_nibble(s1hi, s2hi) << 4
10209 | combine_cacheattr_nibble(s1lo, s2lo);
10210
10211 if (ret.attrs == 0x44) {
10212 /* Any location for which the resultant memory type is Normal
10213 * Inner Non-cacheable, Outer Non-cacheable is always treated
10214 * as Outer Shareable.
10215 */
10216 ret.shareability = 2;
10217 }
10218 }
10219
10220 return ret;
10221}
10222
10223
702a9357
PM
10224/* get_phys_addr - get the physical address for this virtual address
10225 *
10226 * Find the physical address corresponding to the given virtual address,
10227 * by doing a translation table walk on MMU based systems or using the
10228 * MPU state on MPU based systems.
10229 *
b7cc4e82
PC
10230 * Returns false if the translation was successful. Otherwise, phys_ptr, attrs,
10231 * prot and page_size may not be filled in, and the populated fsr value provides
702a9357
PM
10232 * information on why the translation aborted, in the format of a
10233 * DFSR/IFSR fault register, with the following caveats:
10234 * * we honour the short vs long DFSR format differences.
10235 * * the WnR bit is never set (the caller must do this).
f6bda88f 10236 * * for PSMAv5 based systems we don't bother to return a full FSR format
702a9357
PM
10237 * value.
10238 *
10239 * @env: CPUARMState
10240 * @address: virtual address to get physical address for
10241 * @access_type: 0 for read, 1 for write, 2 for execute
d3649702 10242 * @mmu_idx: MMU index indicating required translation regime
702a9357 10243 * @phys_ptr: set to the physical address corresponding to the virtual address
8bf5b6a9 10244 * @attrs: set to the memory transaction attributes to use
702a9357
PM
10245 * @prot: set to the permissions for the page containing phys_ptr
10246 * @page_size: set to the size of the page containing phys_ptr
5b2d261d
AB
10247 * @fi: set to fault info if the translation fails
10248 * @cacheattrs: (if non-NULL) set to the cacheability/shareability attributes
702a9357 10249 */
af51f566 10250static bool get_phys_addr(CPUARMState *env, target_ulong address,
03ae85f8 10251 MMUAccessType access_type, ARMMMUIdx mmu_idx,
af51f566 10252 hwaddr *phys_ptr, MemTxAttrs *attrs, int *prot,
bc52bfeb 10253 target_ulong *page_size,
5b2d261d 10254 ARMMMUFaultInfo *fi, ARMCacheAttrs *cacheattrs)
9ee6e8bb 10255{
0480f69a 10256 if (mmu_idx == ARMMMUIdx_S12NSE0 || mmu_idx == ARMMMUIdx_S12NSE1) {
9b539263
EI
10257 /* Call ourselves recursively to do the stage 1 and then stage 2
10258 * translations.
0480f69a 10259 */
9b539263
EI
10260 if (arm_feature(env, ARM_FEATURE_EL2)) {
10261 hwaddr ipa;
10262 int s2_prot;
10263 int ret;
5b2d261d 10264 ARMCacheAttrs cacheattrs2 = {};
9b539263
EI
10265
10266 ret = get_phys_addr(env, address, access_type,
8bd5c820 10267 stage_1_mmu_idx(mmu_idx), &ipa, attrs,
bc52bfeb 10268 prot, page_size, fi, cacheattrs);
9b539263
EI
10269
10270 /* If S1 fails or S2 is disabled, return early. */
10271 if (ret || regime_translation_disabled(env, ARMMMUIdx_S2NS)) {
10272 *phys_ptr = ipa;
10273 return ret;
10274 }
10275
10276 /* S1 is done. Now do S2 translation. */
10277 ret = get_phys_addr_lpae(env, ipa, access_type, ARMMMUIdx_S2NS,
10278 phys_ptr, attrs, &s2_prot,
da909b2c 10279 page_size, fi,
5b2d261d 10280 cacheattrs != NULL ? &cacheattrs2 : NULL);
9b539263
EI
10281 fi->s2addr = ipa;
10282 /* Combine the S1 and S2 perms. */
10283 *prot &= s2_prot;
5b2d261d
AB
10284
10285 /* Combine the S1 and S2 cache attributes, if needed */
10286 if (!ret && cacheattrs != NULL) {
10287 *cacheattrs = combine_cacheattrs(*cacheattrs, cacheattrs2);
10288 }
10289
9b539263
EI
10290 return ret;
10291 } else {
10292 /*
10293 * For non-EL2 CPUs a stage1+stage2 translation is just stage 1.
10294 */
8bd5c820 10295 mmu_idx = stage_1_mmu_idx(mmu_idx);
9b539263 10296 }
0480f69a 10297 }
d3649702 10298
8bf5b6a9
PM
10299 /* The page table entries may downgrade secure to non-secure, but
10300 * cannot upgrade an non-secure translation regime's attributes
10301 * to secure.
10302 */
10303 attrs->secure = regime_is_secure(env, mmu_idx);
0995bf8c 10304 attrs->user = regime_is_user(env, mmu_idx);
8bf5b6a9 10305
0480f69a
PM
10306 /* Fast Context Switch Extension. This doesn't exist at all in v8.
10307 * In v7 and earlier it affects all stage 1 translations.
10308 */
10309 if (address < 0x02000000 && mmu_idx != ARMMMUIdx_S2NS
10310 && !arm_feature(env, ARM_FEATURE_V8)) {
10311 if (regime_el(env, mmu_idx) == 3) {
10312 address += env->cp15.fcseidr_s;
10313 } else {
10314 address += env->cp15.fcseidr_ns;
10315 }
54bf36ed 10316 }
9ee6e8bb 10317
3279adb9 10318 if (arm_feature(env, ARM_FEATURE_PMSA)) {
c9f9f124 10319 bool ret;
f6bda88f 10320 *page_size = TARGET_PAGE_SIZE;
3279adb9 10321
504e3cc3
PM
10322 if (arm_feature(env, ARM_FEATURE_V8)) {
10323 /* PMSAv8 */
10324 ret = get_phys_addr_pmsav8(env, address, access_type, mmu_idx,
3f551b5b 10325 phys_ptr, attrs, prot, fi);
504e3cc3 10326 } else if (arm_feature(env, ARM_FEATURE_V7)) {
3279adb9
PM
10327 /* PMSAv7 */
10328 ret = get_phys_addr_pmsav7(env, address, access_type, mmu_idx,
9375ad15 10329 phys_ptr, prot, fi);
3279adb9
PM
10330 } else {
10331 /* Pre-v7 MPU */
10332 ret = get_phys_addr_pmsav5(env, address, access_type, mmu_idx,
53a4e5c5 10333 phys_ptr, prot, fi);
3279adb9
PM
10334 }
10335 qemu_log_mask(CPU_LOG_MMU, "PMSA MPU lookup for %s at 0x%08" PRIx32
c9f9f124 10336 " mmu_idx %u -> %s (prot %c%c%c)\n",
709e4407
PM
10337 access_type == MMU_DATA_LOAD ? "reading" :
10338 (access_type == MMU_DATA_STORE ? "writing" : "execute"),
c9f9f124
MD
10339 (uint32_t)address, mmu_idx,
10340 ret ? "Miss" : "Hit",
10341 *prot & PAGE_READ ? 'r' : '-',
10342 *prot & PAGE_WRITE ? 'w' : '-',
10343 *prot & PAGE_EXEC ? 'x' : '-');
10344
10345 return ret;
f6bda88f
PC
10346 }
10347
3279adb9
PM
10348 /* Definitely a real MMU, not an MPU */
10349
0480f69a 10350 if (regime_translation_disabled(env, mmu_idx)) {
3279adb9 10351 /* MMU disabled. */
9ee6e8bb 10352 *phys_ptr = address;
3ad493fc 10353 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
d4c430a8 10354 *page_size = TARGET_PAGE_SIZE;
9ee6e8bb 10355 return 0;
0480f69a
PM
10356 }
10357
0480f69a 10358 if (regime_using_lpae_format(env, mmu_idx)) {
bc52bfeb
PM
10359 return get_phys_addr_lpae(env, address, access_type, mmu_idx,
10360 phys_ptr, attrs, prot, page_size,
10361 fi, cacheattrs);
0480f69a 10362 } else if (regime_sctlr(env, mmu_idx) & SCTLR_XP) {
bc52bfeb
PM
10363 return get_phys_addr_v6(env, address, access_type, mmu_idx,
10364 phys_ptr, attrs, prot, page_size, fi);
9ee6e8bb 10365 } else {
bc52bfeb 10366 return get_phys_addr_v5(env, address, access_type, mmu_idx,
f989983e 10367 phys_ptr, prot, page_size, fi);
9ee6e8bb
PB
10368 }
10369}
10370
8c6084bf 10371/* Walk the page table and (if the mapping exists) add the page
b7cc4e82
PC
10372 * to the TLB. Return false on success, or true on failure. Populate
10373 * fsr with ARM DFSR/IFSR fault register format value on failure.
8c6084bf 10374 */
b7cc4e82 10375bool arm_tlb_fill(CPUState *cs, vaddr address,
bc52bfeb 10376 MMUAccessType access_type, int mmu_idx,
e14b5a23 10377 ARMMMUFaultInfo *fi)
b5ff1b31 10378{
7510454e
AF
10379 ARMCPU *cpu = ARM_CPU(cs);
10380 CPUARMState *env = &cpu->env;
a8170e5e 10381 hwaddr phys_addr;
d4c430a8 10382 target_ulong page_size;
b5ff1b31 10383 int prot;
d3649702 10384 int ret;
8bf5b6a9 10385 MemTxAttrs attrs = {};
b5ff1b31 10386
8bd5c820
PM
10387 ret = get_phys_addr(env, address, access_type,
10388 core_to_arm_mmu_idx(env, mmu_idx), &phys_addr,
bc52bfeb 10389 &attrs, &prot, &page_size, fi, NULL);
b7cc4e82 10390 if (!ret) {
b5ff1b31 10391 /* Map a single [sub]page. */
dcd82c11
AB
10392 phys_addr &= TARGET_PAGE_MASK;
10393 address &= TARGET_PAGE_MASK;
8bf5b6a9
PM
10394 tlb_set_page_with_attrs(cs, address, phys_addr, attrs,
10395 prot, mmu_idx, page_size);
d4c430a8 10396 return 0;
b5ff1b31
FB
10397 }
10398
8c6084bf 10399 return ret;
b5ff1b31
FB
10400}
10401
0faea0c7
PM
10402hwaddr arm_cpu_get_phys_page_attrs_debug(CPUState *cs, vaddr addr,
10403 MemTxAttrs *attrs)
b5ff1b31 10404{
00b941e5 10405 ARMCPU *cpu = ARM_CPU(cs);
d3649702 10406 CPUARMState *env = &cpu->env;
a8170e5e 10407 hwaddr phys_addr;
d4c430a8 10408 target_ulong page_size;
b5ff1b31 10409 int prot;
b7cc4e82 10410 bool ret;
e14b5a23 10411 ARMMMUFaultInfo fi = {};
8bd5c820 10412 ARMMMUIdx mmu_idx = core_to_arm_mmu_idx(env, cpu_mmu_index(env, false));
b5ff1b31 10413
0faea0c7
PM
10414 *attrs = (MemTxAttrs) {};
10415
8bd5c820 10416 ret = get_phys_addr(env, addr, 0, mmu_idx, &phys_addr,
bc52bfeb 10417 attrs, &prot, &page_size, &fi, NULL);
b5ff1b31 10418
b7cc4e82 10419 if (ret) {
b5ff1b31 10420 return -1;
00b941e5 10421 }
b5ff1b31
FB
10422 return phys_addr;
10423}
10424
0ecb72a5 10425uint32_t HELPER(v7m_mrs)(CPUARMState *env, uint32_t reg)
9ee6e8bb 10426{
58117c9b
MD
10427 uint32_t mask;
10428 unsigned el = arm_current_el(env);
10429
10430 /* First handle registers which unprivileged can read */
10431
10432 switch (reg) {
10433 case 0 ... 7: /* xPSR sub-fields */
10434 mask = 0;
10435 if ((reg & 1) && el) {
987ab45e 10436 mask |= XPSR_EXCP; /* IPSR (unpriv. reads as zero) */
58117c9b
MD
10437 }
10438 if (!(reg & 4)) {
987ab45e 10439 mask |= XPSR_NZCV | XPSR_Q; /* APSR */
58117c9b
MD
10440 }
10441 /* EPSR reads as zero */
10442 return xpsr_read(env) & mask;
10443 break;
10444 case 20: /* CONTROL */
8bfc26ea 10445 return env->v7m.control[env->v7m.secure];
50f11062
PM
10446 case 0x94: /* CONTROL_NS */
10447 /* We have to handle this here because unprivileged Secure code
10448 * can read the NS CONTROL register.
10449 */
10450 if (!env->v7m.secure) {
10451 return 0;
10452 }
10453 return env->v7m.control[M_REG_NS];
58117c9b
MD
10454 }
10455
10456 if (el == 0) {
10457 return 0; /* unprivileged reads others as zero */
10458 }
a47dddd7 10459
50f11062
PM
10460 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
10461 switch (reg) {
10462 case 0x88: /* MSP_NS */
10463 if (!env->v7m.secure) {
10464 return 0;
10465 }
10466 return env->v7m.other_ss_msp;
10467 case 0x89: /* PSP_NS */
10468 if (!env->v7m.secure) {
10469 return 0;
10470 }
10471 return env->v7m.other_ss_psp;
57bb3156
PM
10472 case 0x8a: /* MSPLIM_NS */
10473 if (!env->v7m.secure) {
10474 return 0;
10475 }
10476 return env->v7m.msplim[M_REG_NS];
10477 case 0x8b: /* PSPLIM_NS */
10478 if (!env->v7m.secure) {
10479 return 0;
10480 }
10481 return env->v7m.psplim[M_REG_NS];
50f11062
PM
10482 case 0x90: /* PRIMASK_NS */
10483 if (!env->v7m.secure) {
10484 return 0;
10485 }
10486 return env->v7m.primask[M_REG_NS];
10487 case 0x91: /* BASEPRI_NS */
10488 if (!env->v7m.secure) {
10489 return 0;
10490 }
10491 return env->v7m.basepri[M_REG_NS];
10492 case 0x93: /* FAULTMASK_NS */
10493 if (!env->v7m.secure) {
10494 return 0;
10495 }
10496 return env->v7m.faultmask[M_REG_NS];
10497 case 0x98: /* SP_NS */
10498 {
10499 /* This gives the non-secure SP selected based on whether we're
10500 * currently in handler mode or not, using the NS CONTROL.SPSEL.
10501 */
10502 bool spsel = env->v7m.control[M_REG_NS] & R_V7M_CONTROL_SPSEL_MASK;
10503
10504 if (!env->v7m.secure) {
10505 return 0;
10506 }
10507 if (!arm_v7m_is_handler_mode(env) && spsel) {
10508 return env->v7m.other_ss_psp;
10509 } else {
10510 return env->v7m.other_ss_msp;
10511 }
10512 }
10513 default:
10514 break;
10515 }
10516 }
10517
9ee6e8bb 10518 switch (reg) {
9ee6e8bb 10519 case 8: /* MSP */
1169d3aa 10520 return v7m_using_psp(env) ? env->v7m.other_sp : env->regs[13];
9ee6e8bb 10521 case 9: /* PSP */
1169d3aa 10522 return v7m_using_psp(env) ? env->regs[13] : env->v7m.other_sp;
57bb3156
PM
10523 case 10: /* MSPLIM */
10524 if (!arm_feature(env, ARM_FEATURE_V8)) {
10525 goto bad_reg;
10526 }
10527 return env->v7m.msplim[env->v7m.secure];
10528 case 11: /* PSPLIM */
10529 if (!arm_feature(env, ARM_FEATURE_V8)) {
10530 goto bad_reg;
10531 }
10532 return env->v7m.psplim[env->v7m.secure];
9ee6e8bb 10533 case 16: /* PRIMASK */
6d804834 10534 return env->v7m.primask[env->v7m.secure];
82845826
SH
10535 case 17: /* BASEPRI */
10536 case 18: /* BASEPRI_MAX */
acf94941 10537 return env->v7m.basepri[env->v7m.secure];
82845826 10538 case 19: /* FAULTMASK */
42a6686b 10539 return env->v7m.faultmask[env->v7m.secure];
9ee6e8bb 10540 default:
57bb3156 10541 bad_reg:
58117c9b
MD
10542 qemu_log_mask(LOG_GUEST_ERROR, "Attempt to read unknown special"
10543 " register %d\n", reg);
9ee6e8bb
PB
10544 return 0;
10545 }
10546}
10547
b28b3377
PM
10548void HELPER(v7m_msr)(CPUARMState *env, uint32_t maskreg, uint32_t val)
10549{
10550 /* We're passed bits [11..0] of the instruction; extract
10551 * SYSm and the mask bits.
10552 * Invalid combinations of SYSm and mask are UNPREDICTABLE;
10553 * we choose to treat them as if the mask bits were valid.
10554 * NB that the pseudocode 'mask' variable is bits [11..10],
10555 * whereas ours is [11..8].
10556 */
10557 uint32_t mask = extract32(maskreg, 8, 4);
10558 uint32_t reg = extract32(maskreg, 0, 8);
10559
58117c9b
MD
10560 if (arm_current_el(env) == 0 && reg > 7) {
10561 /* only xPSR sub-fields may be written by unprivileged */
10562 return;
10563 }
a47dddd7 10564
50f11062
PM
10565 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
10566 switch (reg) {
10567 case 0x88: /* MSP_NS */
10568 if (!env->v7m.secure) {
10569 return;
10570 }
10571 env->v7m.other_ss_msp = val;
10572 return;
10573 case 0x89: /* PSP_NS */
10574 if (!env->v7m.secure) {
10575 return;
10576 }
10577 env->v7m.other_ss_psp = val;
10578 return;
57bb3156
PM
10579 case 0x8a: /* MSPLIM_NS */
10580 if (!env->v7m.secure) {
10581 return;
10582 }
10583 env->v7m.msplim[M_REG_NS] = val & ~7;
10584 return;
10585 case 0x8b: /* PSPLIM_NS */
10586 if (!env->v7m.secure) {
10587 return;
10588 }
10589 env->v7m.psplim[M_REG_NS] = val & ~7;
10590 return;
50f11062
PM
10591 case 0x90: /* PRIMASK_NS */
10592 if (!env->v7m.secure) {
10593 return;
10594 }
10595 env->v7m.primask[M_REG_NS] = val & 1;
10596 return;
10597 case 0x91: /* BASEPRI_NS */
10598 if (!env->v7m.secure) {
10599 return;
10600 }
10601 env->v7m.basepri[M_REG_NS] = val & 0xff;
10602 return;
10603 case 0x93: /* FAULTMASK_NS */
10604 if (!env->v7m.secure) {
10605 return;
10606 }
10607 env->v7m.faultmask[M_REG_NS] = val & 1;
10608 return;
6eb3a64e
PM
10609 case 0x94: /* CONTROL_NS */
10610 if (!env->v7m.secure) {
10611 return;
10612 }
10613 write_v7m_control_spsel_for_secstate(env,
10614 val & R_V7M_CONTROL_SPSEL_MASK,
10615 M_REG_NS);
10616 env->v7m.control[M_REG_NS] &= ~R_V7M_CONTROL_NPRIV_MASK;
10617 env->v7m.control[M_REG_NS] |= val & R_V7M_CONTROL_NPRIV_MASK;
10618 return;
50f11062
PM
10619 case 0x98: /* SP_NS */
10620 {
10621 /* This gives the non-secure SP selected based on whether we're
10622 * currently in handler mode or not, using the NS CONTROL.SPSEL.
10623 */
10624 bool spsel = env->v7m.control[M_REG_NS] & R_V7M_CONTROL_SPSEL_MASK;
10625
10626 if (!env->v7m.secure) {
10627 return;
10628 }
10629 if (!arm_v7m_is_handler_mode(env) && spsel) {
10630 env->v7m.other_ss_psp = val;
10631 } else {
10632 env->v7m.other_ss_msp = val;
10633 }
10634 return;
10635 }
10636 default:
10637 break;
10638 }
10639 }
10640
9ee6e8bb 10641 switch (reg) {
58117c9b
MD
10642 case 0 ... 7: /* xPSR sub-fields */
10643 /* only APSR is actually writable */
b28b3377
PM
10644 if (!(reg & 4)) {
10645 uint32_t apsrmask = 0;
10646
10647 if (mask & 8) {
987ab45e 10648 apsrmask |= XPSR_NZCV | XPSR_Q;
b28b3377
PM
10649 }
10650 if ((mask & 4) && arm_feature(env, ARM_FEATURE_THUMB_DSP)) {
987ab45e 10651 apsrmask |= XPSR_GE;
b28b3377
PM
10652 }
10653 xpsr_write(env, val, apsrmask);
58117c9b 10654 }
9ee6e8bb
PB
10655 break;
10656 case 8: /* MSP */
1169d3aa 10657 if (v7m_using_psp(env)) {
9ee6e8bb 10658 env->v7m.other_sp = val;
abc24d86 10659 } else {
9ee6e8bb 10660 env->regs[13] = val;
abc24d86 10661 }
9ee6e8bb
PB
10662 break;
10663 case 9: /* PSP */
1169d3aa 10664 if (v7m_using_psp(env)) {
9ee6e8bb 10665 env->regs[13] = val;
abc24d86 10666 } else {
9ee6e8bb 10667 env->v7m.other_sp = val;
abc24d86 10668 }
9ee6e8bb 10669 break;
57bb3156
PM
10670 case 10: /* MSPLIM */
10671 if (!arm_feature(env, ARM_FEATURE_V8)) {
10672 goto bad_reg;
10673 }
10674 env->v7m.msplim[env->v7m.secure] = val & ~7;
10675 break;
10676 case 11: /* PSPLIM */
10677 if (!arm_feature(env, ARM_FEATURE_V8)) {
10678 goto bad_reg;
10679 }
10680 env->v7m.psplim[env->v7m.secure] = val & ~7;
10681 break;
9ee6e8bb 10682 case 16: /* PRIMASK */
6d804834 10683 env->v7m.primask[env->v7m.secure] = val & 1;
9ee6e8bb 10684 break;
82845826 10685 case 17: /* BASEPRI */
acf94941 10686 env->v7m.basepri[env->v7m.secure] = val & 0xff;
9ee6e8bb 10687 break;
82845826 10688 case 18: /* BASEPRI_MAX */
9ee6e8bb 10689 val &= 0xff;
acf94941
PM
10690 if (val != 0 && (val < env->v7m.basepri[env->v7m.secure]
10691 || env->v7m.basepri[env->v7m.secure] == 0)) {
10692 env->v7m.basepri[env->v7m.secure] = val;
10693 }
9ee6e8bb 10694 break;
82845826 10695 case 19: /* FAULTMASK */
42a6686b 10696 env->v7m.faultmask[env->v7m.secure] = val & 1;
82845826 10697 break;
9ee6e8bb 10698 case 20: /* CONTROL */
792dac30
PM
10699 /* Writing to the SPSEL bit only has an effect if we are in
10700 * thread mode; other bits can be updated by any privileged code.
de2db7ec 10701 * write_v7m_control_spsel() deals with updating the SPSEL bit in
792dac30 10702 * env->v7m.control, so we only need update the others.
83d7f86d
PM
10703 * For v7M, we must just ignore explicit writes to SPSEL in handler
10704 * mode; for v8M the write is permitted but will have no effect.
792dac30 10705 */
83d7f86d
PM
10706 if (arm_feature(env, ARM_FEATURE_V8) ||
10707 !arm_v7m_is_handler_mode(env)) {
de2db7ec 10708 write_v7m_control_spsel(env, (val & R_V7M_CONTROL_SPSEL_MASK) != 0);
792dac30 10709 }
8bfc26ea
PM
10710 env->v7m.control[env->v7m.secure] &= ~R_V7M_CONTROL_NPRIV_MASK;
10711 env->v7m.control[env->v7m.secure] |= val & R_V7M_CONTROL_NPRIV_MASK;
9ee6e8bb
PB
10712 break;
10713 default:
57bb3156 10714 bad_reg:
58117c9b
MD
10715 qemu_log_mask(LOG_GUEST_ERROR, "Attempt to write unknown special"
10716 " register %d\n", reg);
9ee6e8bb
PB
10717 return;
10718 }
10719}
10720
5158de24
PM
10721uint32_t HELPER(v7m_tt)(CPUARMState *env, uint32_t addr, uint32_t op)
10722{
10723 /* Implement the TT instruction. op is bits [7:6] of the insn. */
10724 bool forceunpriv = op & 1;
10725 bool alt = op & 2;
10726 V8M_SAttributes sattrs = {};
10727 uint32_t tt_resp;
10728 bool r, rw, nsr, nsrw, mrvalid;
10729 int prot;
3f551b5b 10730 ARMMMUFaultInfo fi = {};
5158de24
PM
10731 MemTxAttrs attrs = {};
10732 hwaddr phys_addr;
5158de24
PM
10733 ARMMMUIdx mmu_idx;
10734 uint32_t mregion;
10735 bool targetpriv;
10736 bool targetsec = env->v7m.secure;
10737
10738 /* Work out what the security state and privilege level we're
10739 * interested in is...
10740 */
10741 if (alt) {
10742 targetsec = !targetsec;
10743 }
10744
10745 if (forceunpriv) {
10746 targetpriv = false;
10747 } else {
10748 targetpriv = arm_v7m_is_handler_mode(env) ||
10749 !(env->v7m.control[targetsec] & R_V7M_CONTROL_NPRIV_MASK);
10750 }
10751
10752 /* ...and then figure out which MMU index this is */
10753 mmu_idx = arm_v7m_mmu_idx_for_secstate_and_priv(env, targetsec, targetpriv);
10754
10755 /* We know that the MPU and SAU don't care about the access type
10756 * for our purposes beyond that we don't want to claim to be
10757 * an insn fetch, so we arbitrarily call this a read.
10758 */
10759
10760 /* MPU region info only available for privileged or if
10761 * inspecting the other MPU state.
10762 */
10763 if (arm_current_el(env) != 0 || alt) {
10764 /* We can ignore the return value as prot is always set */
10765 pmsav8_mpu_lookup(env, addr, MMU_DATA_LOAD, mmu_idx,
3f551b5b 10766 &phys_addr, &attrs, &prot, &fi, &mregion);
5158de24
PM
10767 if (mregion == -1) {
10768 mrvalid = false;
10769 mregion = 0;
10770 } else {
10771 mrvalid = true;
10772 }
10773 r = prot & PAGE_READ;
10774 rw = prot & PAGE_WRITE;
10775 } else {
10776 r = false;
10777 rw = false;
10778 mrvalid = false;
10779 mregion = 0;
10780 }
10781
10782 if (env->v7m.secure) {
10783 v8m_security_lookup(env, addr, MMU_DATA_LOAD, mmu_idx, &sattrs);
10784 nsr = sattrs.ns && r;
10785 nsrw = sattrs.ns && rw;
10786 } else {
10787 sattrs.ns = true;
10788 nsr = false;
10789 nsrw = false;
10790 }
10791
10792 tt_resp = (sattrs.iregion << 24) |
10793 (sattrs.irvalid << 23) |
10794 ((!sattrs.ns) << 22) |
10795 (nsrw << 21) |
10796 (nsr << 20) |
10797 (rw << 19) |
10798 (r << 18) |
10799 (sattrs.srvalid << 17) |
10800 (mrvalid << 16) |
10801 (sattrs.sregion << 8) |
10802 mregion;
10803
10804 return tt_resp;
10805}
10806
b5ff1b31 10807#endif
6ddbc6e4 10808
aca3f40b
PM
10809void HELPER(dc_zva)(CPUARMState *env, uint64_t vaddr_in)
10810{
10811 /* Implement DC ZVA, which zeroes a fixed-length block of memory.
10812 * Note that we do not implement the (architecturally mandated)
10813 * alignment fault for attempts to use this on Device memory
10814 * (which matches the usual QEMU behaviour of not implementing either
10815 * alignment faults or any memory attribute handling).
10816 */
10817
10818 ARMCPU *cpu = arm_env_get_cpu(env);
10819 uint64_t blocklen = 4 << cpu->dcz_blocksize;
10820 uint64_t vaddr = vaddr_in & ~(blocklen - 1);
10821
10822#ifndef CONFIG_USER_ONLY
10823 {
10824 /* Slightly awkwardly, QEMU's TARGET_PAGE_SIZE may be less than
10825 * the block size so we might have to do more than one TLB lookup.
10826 * We know that in fact for any v8 CPU the page size is at least 4K
10827 * and the block size must be 2K or less, but TARGET_PAGE_SIZE is only
10828 * 1K as an artefact of legacy v5 subpage support being present in the
10829 * same QEMU executable.
10830 */
10831 int maxidx = DIV_ROUND_UP(blocklen, TARGET_PAGE_SIZE);
10832 void *hostaddr[maxidx];
10833 int try, i;
97ed5ccd 10834 unsigned mmu_idx = cpu_mmu_index(env, false);
3972ef6f 10835 TCGMemOpIdx oi = make_memop_idx(MO_UB, mmu_idx);
aca3f40b
PM
10836
10837 for (try = 0; try < 2; try++) {
10838
10839 for (i = 0; i < maxidx; i++) {
10840 hostaddr[i] = tlb_vaddr_to_host(env,
10841 vaddr + TARGET_PAGE_SIZE * i,
3972ef6f 10842 1, mmu_idx);
aca3f40b
PM
10843 if (!hostaddr[i]) {
10844 break;
10845 }
10846 }
10847 if (i == maxidx) {
10848 /* If it's all in the TLB it's fair game for just writing to;
10849 * we know we don't need to update dirty status, etc.
10850 */
10851 for (i = 0; i < maxidx - 1; i++) {
10852 memset(hostaddr[i], 0, TARGET_PAGE_SIZE);
10853 }
10854 memset(hostaddr[i], 0, blocklen - (i * TARGET_PAGE_SIZE));
10855 return;
10856 }
10857 /* OK, try a store and see if we can populate the tlb. This
10858 * might cause an exception if the memory isn't writable,
10859 * in which case we will longjmp out of here. We must for
10860 * this purpose use the actual register value passed to us
10861 * so that we get the fault address right.
10862 */
01ecaf43 10863 helper_ret_stb_mmu(env, vaddr_in, 0, oi, GETPC());
aca3f40b
PM
10864 /* Now we can populate the other TLB entries, if any */
10865 for (i = 0; i < maxidx; i++) {
10866 uint64_t va = vaddr + TARGET_PAGE_SIZE * i;
10867 if (va != (vaddr_in & TARGET_PAGE_MASK)) {
01ecaf43 10868 helper_ret_stb_mmu(env, va, 0, oi, GETPC());
aca3f40b
PM
10869 }
10870 }
10871 }
10872
10873 /* Slow path (probably attempt to do this to an I/O device or
10874 * similar, or clearing of a block of code we have translations
10875 * cached for). Just do a series of byte writes as the architecture
10876 * demands. It's not worth trying to use a cpu_physical_memory_map(),
10877 * memset(), unmap() sequence here because:
10878 * + we'd need to account for the blocksize being larger than a page
10879 * + the direct-RAM access case is almost always going to be dealt
10880 * with in the fastpath code above, so there's no speed benefit
10881 * + we would have to deal with the map returning NULL because the
10882 * bounce buffer was in use
10883 */
10884 for (i = 0; i < blocklen; i++) {
01ecaf43 10885 helper_ret_stb_mmu(env, vaddr + i, 0, oi, GETPC());
aca3f40b
PM
10886 }
10887 }
10888#else
10889 memset(g2h(vaddr), 0, blocklen);
10890#endif
10891}
10892
6ddbc6e4
PB
10893/* Note that signed overflow is undefined in C. The following routines are
10894 careful to use unsigned types where modulo arithmetic is required.
10895 Failure to do so _will_ break on newer gcc. */
10896
10897/* Signed saturating arithmetic. */
10898
1654b2d6 10899/* Perform 16-bit signed saturating addition. */
6ddbc6e4
PB
10900static inline uint16_t add16_sat(uint16_t a, uint16_t b)
10901{
10902 uint16_t res;
10903
10904 res = a + b;
10905 if (((res ^ a) & 0x8000) && !((a ^ b) & 0x8000)) {
10906 if (a & 0x8000)
10907 res = 0x8000;
10908 else
10909 res = 0x7fff;
10910 }
10911 return res;
10912}
10913
1654b2d6 10914/* Perform 8-bit signed saturating addition. */
6ddbc6e4
PB
10915static inline uint8_t add8_sat(uint8_t a, uint8_t b)
10916{
10917 uint8_t res;
10918
10919 res = a + b;
10920 if (((res ^ a) & 0x80) && !((a ^ b) & 0x80)) {
10921 if (a & 0x80)
10922 res = 0x80;
10923 else
10924 res = 0x7f;
10925 }
10926 return res;
10927}
10928
1654b2d6 10929/* Perform 16-bit signed saturating subtraction. */
6ddbc6e4
PB
10930static inline uint16_t sub16_sat(uint16_t a, uint16_t b)
10931{
10932 uint16_t res;
10933
10934 res = a - b;
10935 if (((res ^ a) & 0x8000) && ((a ^ b) & 0x8000)) {
10936 if (a & 0x8000)
10937 res = 0x8000;
10938 else
10939 res = 0x7fff;
10940 }
10941 return res;
10942}
10943
1654b2d6 10944/* Perform 8-bit signed saturating subtraction. */
6ddbc6e4
PB
10945static inline uint8_t sub8_sat(uint8_t a, uint8_t b)
10946{
10947 uint8_t res;
10948
10949 res = a - b;
10950 if (((res ^ a) & 0x80) && ((a ^ b) & 0x80)) {
10951 if (a & 0x80)
10952 res = 0x80;
10953 else
10954 res = 0x7f;
10955 }
10956 return res;
10957}
10958
10959#define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16);
10960#define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16);
10961#define ADD8(a, b, n) RESULT(add8_sat(a, b), n, 8);
10962#define SUB8(a, b, n) RESULT(sub8_sat(a, b), n, 8);
10963#define PFX q
10964
10965#include "op_addsub.h"
10966
10967/* Unsigned saturating arithmetic. */
460a09c1 10968static inline uint16_t add16_usat(uint16_t a, uint16_t b)
6ddbc6e4
PB
10969{
10970 uint16_t res;
10971 res = a + b;
10972 if (res < a)
10973 res = 0xffff;
10974 return res;
10975}
10976
460a09c1 10977static inline uint16_t sub16_usat(uint16_t a, uint16_t b)
6ddbc6e4 10978{
4c4fd3f8 10979 if (a > b)
6ddbc6e4
PB
10980 return a - b;
10981 else
10982 return 0;
10983}
10984
10985static inline uint8_t add8_usat(uint8_t a, uint8_t b)
10986{
10987 uint8_t res;
10988 res = a + b;
10989 if (res < a)
10990 res = 0xff;
10991 return res;
10992}
10993
10994static inline uint8_t sub8_usat(uint8_t a, uint8_t b)
10995{
4c4fd3f8 10996 if (a > b)
6ddbc6e4
PB
10997 return a - b;
10998 else
10999 return 0;
11000}
11001
11002#define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16);
11003#define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16);
11004#define ADD8(a, b, n) RESULT(add8_usat(a, b), n, 8);
11005#define SUB8(a, b, n) RESULT(sub8_usat(a, b), n, 8);
11006#define PFX uq
11007
11008#include "op_addsub.h"
11009
11010/* Signed modulo arithmetic. */
11011#define SARITH16(a, b, n, op) do { \
11012 int32_t sum; \
db6e2e65 11013 sum = (int32_t)(int16_t)(a) op (int32_t)(int16_t)(b); \
6ddbc6e4
PB
11014 RESULT(sum, n, 16); \
11015 if (sum >= 0) \
11016 ge |= 3 << (n * 2); \
11017 } while(0)
11018
11019#define SARITH8(a, b, n, op) do { \
11020 int32_t sum; \
db6e2e65 11021 sum = (int32_t)(int8_t)(a) op (int32_t)(int8_t)(b); \
6ddbc6e4
PB
11022 RESULT(sum, n, 8); \
11023 if (sum >= 0) \
11024 ge |= 1 << n; \
11025 } while(0)
11026
11027
11028#define ADD16(a, b, n) SARITH16(a, b, n, +)
11029#define SUB16(a, b, n) SARITH16(a, b, n, -)
11030#define ADD8(a, b, n) SARITH8(a, b, n, +)
11031#define SUB8(a, b, n) SARITH8(a, b, n, -)
11032#define PFX s
11033#define ARITH_GE
11034
11035#include "op_addsub.h"
11036
11037/* Unsigned modulo arithmetic. */
11038#define ADD16(a, b, n) do { \
11039 uint32_t sum; \
11040 sum = (uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b); \
11041 RESULT(sum, n, 16); \
a87aa10b 11042 if ((sum >> 16) == 1) \
6ddbc6e4
PB
11043 ge |= 3 << (n * 2); \
11044 } while(0)
11045
11046#define ADD8(a, b, n) do { \
11047 uint32_t sum; \
11048 sum = (uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b); \
11049 RESULT(sum, n, 8); \
a87aa10b
AZ
11050 if ((sum >> 8) == 1) \
11051 ge |= 1 << n; \
6ddbc6e4
PB
11052 } while(0)
11053
11054#define SUB16(a, b, n) do { \
11055 uint32_t sum; \
11056 sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \
11057 RESULT(sum, n, 16); \
11058 if ((sum >> 16) == 0) \
11059 ge |= 3 << (n * 2); \
11060 } while(0)
11061
11062#define SUB8(a, b, n) do { \
11063 uint32_t sum; \
11064 sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \
11065 RESULT(sum, n, 8); \
11066 if ((sum >> 8) == 0) \
a87aa10b 11067 ge |= 1 << n; \
6ddbc6e4
PB
11068 } while(0)
11069
11070#define PFX u
11071#define ARITH_GE
11072
11073#include "op_addsub.h"
11074
11075/* Halved signed arithmetic. */
11076#define ADD16(a, b, n) \
11077 RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16)
11078#define SUB16(a, b, n) \
11079 RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16)
11080#define ADD8(a, b, n) \
11081 RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8)
11082#define SUB8(a, b, n) \
11083 RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8)
11084#define PFX sh
11085
11086#include "op_addsub.h"
11087
11088/* Halved unsigned arithmetic. */
11089#define ADD16(a, b, n) \
11090 RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16)
11091#define SUB16(a, b, n) \
11092 RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16)
11093#define ADD8(a, b, n) \
11094 RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8)
11095#define SUB8(a, b, n) \
11096 RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8)
11097#define PFX uh
11098
11099#include "op_addsub.h"
11100
11101static inline uint8_t do_usad(uint8_t a, uint8_t b)
11102{
11103 if (a > b)
11104 return a - b;
11105 else
11106 return b - a;
11107}
11108
11109/* Unsigned sum of absolute byte differences. */
11110uint32_t HELPER(usad8)(uint32_t a, uint32_t b)
11111{
11112 uint32_t sum;
11113 sum = do_usad(a, b);
11114 sum += do_usad(a >> 8, b >> 8);
11115 sum += do_usad(a >> 16, b >>16);
11116 sum += do_usad(a >> 24, b >> 24);
11117 return sum;
11118}
11119
11120/* For ARMv6 SEL instruction. */
11121uint32_t HELPER(sel_flags)(uint32_t flags, uint32_t a, uint32_t b)
11122{
11123 uint32_t mask;
11124
11125 mask = 0;
11126 if (flags & 1)
11127 mask |= 0xff;
11128 if (flags & 2)
11129 mask |= 0xff00;
11130 if (flags & 4)
11131 mask |= 0xff0000;
11132 if (flags & 8)
11133 mask |= 0xff000000;
11134 return (a & mask) | (b & ~mask);
11135}
11136
b90372ad
PM
11137/* VFP support. We follow the convention used for VFP instructions:
11138 Single precision routines have a "s" suffix, double precision a
4373f3ce
PB
11139 "d" suffix. */
11140
11141/* Convert host exception flags to vfp form. */
11142static inline int vfp_exceptbits_from_host(int host_bits)
11143{
11144 int target_bits = 0;
11145
11146 if (host_bits & float_flag_invalid)
11147 target_bits |= 1;
11148 if (host_bits & float_flag_divbyzero)
11149 target_bits |= 2;
11150 if (host_bits & float_flag_overflow)
11151 target_bits |= 4;
36802b6b 11152 if (host_bits & (float_flag_underflow | float_flag_output_denormal))
4373f3ce
PB
11153 target_bits |= 8;
11154 if (host_bits & float_flag_inexact)
11155 target_bits |= 0x10;
cecd8504
PM
11156 if (host_bits & float_flag_input_denormal)
11157 target_bits |= 0x80;
4373f3ce
PB
11158 return target_bits;
11159}
11160
0ecb72a5 11161uint32_t HELPER(vfp_get_fpscr)(CPUARMState *env)
4373f3ce
PB
11162{
11163 int i;
11164 uint32_t fpscr;
11165
11166 fpscr = (env->vfp.xregs[ARM_VFP_FPSCR] & 0xffc8ffff)
11167 | (env->vfp.vec_len << 16)
11168 | (env->vfp.vec_stride << 20);
11169 i = get_float_exception_flags(&env->vfp.fp_status);
3a492f3a 11170 i |= get_float_exception_flags(&env->vfp.standard_fp_status);
d81ce0ef 11171 i |= get_float_exception_flags(&env->vfp.fp_status_f16);
4373f3ce
PB
11172 fpscr |= vfp_exceptbits_from_host(i);
11173 return fpscr;
11174}
11175
0ecb72a5 11176uint32_t vfp_get_fpscr(CPUARMState *env)
01653295
PM
11177{
11178 return HELPER(vfp_get_fpscr)(env);
11179}
11180
4373f3ce
PB
11181/* Convert vfp exception flags to target form. */
11182static inline int vfp_exceptbits_to_host(int target_bits)
11183{
11184 int host_bits = 0;
11185
11186 if (target_bits & 1)
11187 host_bits |= float_flag_invalid;
11188 if (target_bits & 2)
11189 host_bits |= float_flag_divbyzero;
11190 if (target_bits & 4)
11191 host_bits |= float_flag_overflow;
11192 if (target_bits & 8)
11193 host_bits |= float_flag_underflow;
11194 if (target_bits & 0x10)
11195 host_bits |= float_flag_inexact;
cecd8504
PM
11196 if (target_bits & 0x80)
11197 host_bits |= float_flag_input_denormal;
4373f3ce
PB
11198 return host_bits;
11199}
11200
0ecb72a5 11201void HELPER(vfp_set_fpscr)(CPUARMState *env, uint32_t val)
4373f3ce
PB
11202{
11203 int i;
11204 uint32_t changed;
11205
11206 changed = env->vfp.xregs[ARM_VFP_FPSCR];
11207 env->vfp.xregs[ARM_VFP_FPSCR] = (val & 0xffc8ffff);
11208 env->vfp.vec_len = (val >> 16) & 7;
11209 env->vfp.vec_stride = (val >> 20) & 3;
11210
11211 changed ^= val;
11212 if (changed & (3 << 22)) {
11213 i = (val >> 22) & 3;
11214 switch (i) {
4d3da0f3 11215 case FPROUNDING_TIEEVEN:
4373f3ce
PB
11216 i = float_round_nearest_even;
11217 break;
4d3da0f3 11218 case FPROUNDING_POSINF:
4373f3ce
PB
11219 i = float_round_up;
11220 break;
4d3da0f3 11221 case FPROUNDING_NEGINF:
4373f3ce
PB
11222 i = float_round_down;
11223 break;
4d3da0f3 11224 case FPROUNDING_ZERO:
4373f3ce
PB
11225 i = float_round_to_zero;
11226 break;
11227 }
11228 set_float_rounding_mode(i, &env->vfp.fp_status);
d81ce0ef 11229 set_float_rounding_mode(i, &env->vfp.fp_status_f16);
4373f3ce 11230 }
d81ce0ef
AB
11231 if (changed & FPCR_FZ16) {
11232 bool ftz_enabled = val & FPCR_FZ16;
11233 set_flush_to_zero(ftz_enabled, &env->vfp.fp_status_f16);
11234 set_flush_inputs_to_zero(ftz_enabled, &env->vfp.fp_status_f16);
11235 }
11236 if (changed & FPCR_FZ) {
11237 bool ftz_enabled = val & FPCR_FZ;
11238 set_flush_to_zero(ftz_enabled, &env->vfp.fp_status);
11239 set_flush_inputs_to_zero(ftz_enabled, &env->vfp.fp_status);
11240 }
11241 if (changed & FPCR_DN) {
11242 bool dnan_enabled = val & FPCR_DN;
11243 set_default_nan_mode(dnan_enabled, &env->vfp.fp_status);
11244 set_default_nan_mode(dnan_enabled, &env->vfp.fp_status_f16);
cecd8504 11245 }
4373f3ce 11246
d81ce0ef
AB
11247 /* The exception flags are ORed together when we read fpscr so we
11248 * only need to preserve the current state in one of our
11249 * float_status values.
11250 */
b12c390b 11251 i = vfp_exceptbits_to_host(val);
4373f3ce 11252 set_float_exception_flags(i, &env->vfp.fp_status);
d81ce0ef 11253 set_float_exception_flags(0, &env->vfp.fp_status_f16);
3a492f3a 11254 set_float_exception_flags(0, &env->vfp.standard_fp_status);
4373f3ce
PB
11255}
11256
0ecb72a5 11257void vfp_set_fpscr(CPUARMState *env, uint32_t val)
01653295
PM
11258{
11259 HELPER(vfp_set_fpscr)(env, val);
11260}
11261
4373f3ce
PB
11262#define VFP_HELPER(name, p) HELPER(glue(glue(vfp_,name),p))
11263
11264#define VFP_BINOP(name) \
ae1857ec 11265float32 VFP_HELPER(name, s)(float32 a, float32 b, void *fpstp) \
4373f3ce 11266{ \
ae1857ec
PM
11267 float_status *fpst = fpstp; \
11268 return float32_ ## name(a, b, fpst); \
4373f3ce 11269} \
ae1857ec 11270float64 VFP_HELPER(name, d)(float64 a, float64 b, void *fpstp) \
4373f3ce 11271{ \
ae1857ec
PM
11272 float_status *fpst = fpstp; \
11273 return float64_ ## name(a, b, fpst); \
4373f3ce
PB
11274}
11275VFP_BINOP(add)
11276VFP_BINOP(sub)
11277VFP_BINOP(mul)
11278VFP_BINOP(div)
f71a2ae5
PM
11279VFP_BINOP(min)
11280VFP_BINOP(max)
11281VFP_BINOP(minnum)
11282VFP_BINOP(maxnum)
4373f3ce
PB
11283#undef VFP_BINOP
11284
11285float32 VFP_HELPER(neg, s)(float32 a)
11286{
11287 return float32_chs(a);
11288}
11289
11290float64 VFP_HELPER(neg, d)(float64 a)
11291{
66230e0d 11292 return float64_chs(a);
4373f3ce
PB
11293}
11294
11295float32 VFP_HELPER(abs, s)(float32 a)
11296{
11297 return float32_abs(a);
11298}
11299
11300float64 VFP_HELPER(abs, d)(float64 a)
11301{
66230e0d 11302 return float64_abs(a);
4373f3ce
PB
11303}
11304
0ecb72a5 11305float32 VFP_HELPER(sqrt, s)(float32 a, CPUARMState *env)
4373f3ce
PB
11306{
11307 return float32_sqrt(a, &env->vfp.fp_status);
11308}
11309
0ecb72a5 11310float64 VFP_HELPER(sqrt, d)(float64 a, CPUARMState *env)
4373f3ce
PB
11311{
11312 return float64_sqrt(a, &env->vfp.fp_status);
11313}
11314
11315/* XXX: check quiet/signaling case */
11316#define DO_VFP_cmp(p, type) \
0ecb72a5 11317void VFP_HELPER(cmp, p)(type a, type b, CPUARMState *env) \
4373f3ce
PB
11318{ \
11319 uint32_t flags; \
11320 switch(type ## _compare_quiet(a, b, &env->vfp.fp_status)) { \
11321 case 0: flags = 0x6; break; \
11322 case -1: flags = 0x8; break; \
11323 case 1: flags = 0x2; break; \
11324 default: case 2: flags = 0x3; break; \
11325 } \
11326 env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \
11327 | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \
11328} \
0ecb72a5 11329void VFP_HELPER(cmpe, p)(type a, type b, CPUARMState *env) \
4373f3ce
PB
11330{ \
11331 uint32_t flags; \
11332 switch(type ## _compare(a, b, &env->vfp.fp_status)) { \
11333 case 0: flags = 0x6; break; \
11334 case -1: flags = 0x8; break; \
11335 case 1: flags = 0x2; break; \
11336 default: case 2: flags = 0x3; break; \
11337 } \
11338 env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \
11339 | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \
11340}
11341DO_VFP_cmp(s, float32)
11342DO_VFP_cmp(d, float64)
11343#undef DO_VFP_cmp
11344
5500b06c 11345/* Integer to float and float to integer conversions */
4373f3ce 11346
5500b06c
PM
11347#define CONV_ITOF(name, fsz, sign) \
11348 float##fsz HELPER(name)(uint32_t x, void *fpstp) \
11349{ \
11350 float_status *fpst = fpstp; \
85836979 11351 return sign##int32_to_##float##fsz((sign##int32_t)x, fpst); \
4373f3ce
PB
11352}
11353
5500b06c
PM
11354#define CONV_FTOI(name, fsz, sign, round) \
11355uint32_t HELPER(name)(float##fsz x, void *fpstp) \
11356{ \
11357 float_status *fpst = fpstp; \
11358 if (float##fsz##_is_any_nan(x)) { \
11359 float_raise(float_flag_invalid, fpst); \
11360 return 0; \
11361 } \
11362 return float##fsz##_to_##sign##int32##round(x, fpst); \
4373f3ce
PB
11363}
11364
5500b06c
PM
11365#define FLOAT_CONVS(name, p, fsz, sign) \
11366CONV_ITOF(vfp_##name##to##p, fsz, sign) \
11367CONV_FTOI(vfp_to##name##p, fsz, sign, ) \
11368CONV_FTOI(vfp_to##name##z##p, fsz, sign, _round_to_zero)
4373f3ce 11369
93193190 11370FLOAT_CONVS(si, h, 16, )
5500b06c
PM
11371FLOAT_CONVS(si, s, 32, )
11372FLOAT_CONVS(si, d, 64, )
93193190 11373FLOAT_CONVS(ui, h, 16, u)
5500b06c
PM
11374FLOAT_CONVS(ui, s, 32, u)
11375FLOAT_CONVS(ui, d, 64, u)
4373f3ce 11376
5500b06c
PM
11377#undef CONV_ITOF
11378#undef CONV_FTOI
11379#undef FLOAT_CONVS
4373f3ce
PB
11380
11381/* floating point conversion */
0ecb72a5 11382float64 VFP_HELPER(fcvtd, s)(float32 x, CPUARMState *env)
4373f3ce 11383{
a9d173dc 11384 return float32_to_float64(x, &env->vfp.fp_status);
4373f3ce
PB
11385}
11386
0ecb72a5 11387float32 VFP_HELPER(fcvts, d)(float64 x, CPUARMState *env)
4373f3ce 11388{
a9d173dc 11389 return float64_to_float32(x, &env->vfp.fp_status);
4373f3ce
PB
11390}
11391
11392/* VFP3 fixed point conversion. */
16d5b3ca 11393#define VFP_CONV_FIX_FLOAT(name, p, fsz, isz, itype) \
8ed697e8
WN
11394float##fsz HELPER(vfp_##name##to##p)(uint##isz##_t x, uint32_t shift, \
11395 void *fpstp) \
4373f3ce 11396{ \
5500b06c 11397 float_status *fpst = fpstp; \
622465e1 11398 float##fsz tmp; \
8ed697e8 11399 tmp = itype##_to_##float##fsz(x, fpst); \
5500b06c 11400 return float##fsz##_scalbn(tmp, -(int)shift, fpst); \
16d5b3ca
WN
11401}
11402
abe66f70
PM
11403/* Notice that we want only input-denormal exception flags from the
11404 * scalbn operation: the other possible flags (overflow+inexact if
11405 * we overflow to infinity, output-denormal) aren't correct for the
11406 * complete scale-and-convert operation.
11407 */
16d5b3ca
WN
11408#define VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, round) \
11409uint##isz##_t HELPER(vfp_to##name##p##round)(float##fsz x, \
11410 uint32_t shift, \
11411 void *fpstp) \
4373f3ce 11412{ \
5500b06c 11413 float_status *fpst = fpstp; \
abe66f70 11414 int old_exc_flags = get_float_exception_flags(fpst); \
622465e1
PM
11415 float##fsz tmp; \
11416 if (float##fsz##_is_any_nan(x)) { \
5500b06c 11417 float_raise(float_flag_invalid, fpst); \
622465e1 11418 return 0; \
09d9487f 11419 } \
5500b06c 11420 tmp = float##fsz##_scalbn(x, shift, fpst); \
abe66f70
PM
11421 old_exc_flags |= get_float_exception_flags(fpst) \
11422 & float_flag_input_denormal; \
11423 set_float_exception_flags(old_exc_flags, fpst); \
16d5b3ca 11424 return float##fsz##_to_##itype##round(tmp, fpst); \
622465e1
PM
11425}
11426
16d5b3ca
WN
11427#define VFP_CONV_FIX(name, p, fsz, isz, itype) \
11428VFP_CONV_FIX_FLOAT(name, p, fsz, isz, itype) \
3c6a074a
WN
11429VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, _round_to_zero) \
11430VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, )
11431
11432#define VFP_CONV_FIX_A64(name, p, fsz, isz, itype) \
11433VFP_CONV_FIX_FLOAT(name, p, fsz, isz, itype) \
11434VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, )
16d5b3ca 11435
8ed697e8
WN
11436VFP_CONV_FIX(sh, d, 64, 64, int16)
11437VFP_CONV_FIX(sl, d, 64, 64, int32)
3c6a074a 11438VFP_CONV_FIX_A64(sq, d, 64, 64, int64)
8ed697e8
WN
11439VFP_CONV_FIX(uh, d, 64, 64, uint16)
11440VFP_CONV_FIX(ul, d, 64, 64, uint32)
3c6a074a 11441VFP_CONV_FIX_A64(uq, d, 64, 64, uint64)
8ed697e8
WN
11442VFP_CONV_FIX(sh, s, 32, 32, int16)
11443VFP_CONV_FIX(sl, s, 32, 32, int32)
3c6a074a 11444VFP_CONV_FIX_A64(sq, s, 32, 64, int64)
8ed697e8
WN
11445VFP_CONV_FIX(uh, s, 32, 32, uint16)
11446VFP_CONV_FIX(ul, s, 32, 32, uint32)
3c6a074a 11447VFP_CONV_FIX_A64(uq, s, 32, 64, uint64)
88808a02 11448
4373f3ce 11449#undef VFP_CONV_FIX
16d5b3ca
WN
11450#undef VFP_CONV_FIX_FLOAT
11451#undef VFP_CONV_FLOAT_FIX_ROUND
88808a02
RH
11452#undef VFP_CONV_FIX_A64
11453
11454/* Conversion to/from f16 can overflow to infinity before/after scaling.
564a0632
RH
11455 * Therefore we convert to f64, scale, and then convert f64 to f16; or
11456 * vice versa for conversion to integer.
11457 *
11458 * For 16- and 32-bit integers, the conversion to f64 never rounds.
11459 * For 64-bit integers, any integer that would cause rounding will also
11460 * overflow to f16 infinity, so there is no double rounding problem.
88808a02
RH
11461 */
11462
11463static float16 do_postscale_fp16(float64 f, int shift, float_status *fpst)
11464{
11465 return float64_to_float16(float64_scalbn(f, -shift, fpst), true, fpst);
11466}
11467
11468float16 HELPER(vfp_sltoh)(uint32_t x, uint32_t shift, void *fpst)
11469{
11470 return do_postscale_fp16(int32_to_float64(x, fpst), shift, fpst);
11471}
11472
11473float16 HELPER(vfp_ultoh)(uint32_t x, uint32_t shift, void *fpst)
11474{
11475 return do_postscale_fp16(uint32_to_float64(x, fpst), shift, fpst);
11476}
11477
564a0632
RH
11478float16 HELPER(vfp_sqtoh)(uint64_t x, uint32_t shift, void *fpst)
11479{
11480 return do_postscale_fp16(int64_to_float64(x, fpst), shift, fpst);
11481}
11482
11483float16 HELPER(vfp_uqtoh)(uint64_t x, uint32_t shift, void *fpst)
11484{
11485 return do_postscale_fp16(uint64_to_float64(x, fpst), shift, fpst);
11486}
11487
88808a02
RH
11488static float64 do_prescale_fp16(float16 f, int shift, float_status *fpst)
11489{
11490 if (unlikely(float16_is_any_nan(f))) {
11491 float_raise(float_flag_invalid, fpst);
11492 return 0;
11493 } else {
11494 int old_exc_flags = get_float_exception_flags(fpst);
11495 float64 ret;
11496
11497 ret = float16_to_float64(f, true, fpst);
11498 ret = float64_scalbn(ret, shift, fpst);
11499 old_exc_flags |= get_float_exception_flags(fpst)
11500 & float_flag_input_denormal;
11501 set_float_exception_flags(old_exc_flags, fpst);
11502
11503 return ret;
11504 }
11505}
11506
11507uint32_t HELPER(vfp_toshh)(float16 x, uint32_t shift, void *fpst)
11508{
11509 return float64_to_int16(do_prescale_fp16(x, shift, fpst), fpst);
11510}
11511
11512uint32_t HELPER(vfp_touhh)(float16 x, uint32_t shift, void *fpst)
11513{
11514 return float64_to_uint16(do_prescale_fp16(x, shift, fpst), fpst);
11515}
4373f3ce 11516
564a0632
RH
11517uint32_t HELPER(vfp_toslh)(float16 x, uint32_t shift, void *fpst)
11518{
11519 return float64_to_int32(do_prescale_fp16(x, shift, fpst), fpst);
11520}
11521
11522uint32_t HELPER(vfp_toulh)(float16 x, uint32_t shift, void *fpst)
11523{
11524 return float64_to_uint32(do_prescale_fp16(x, shift, fpst), fpst);
11525}
11526
11527uint64_t HELPER(vfp_tosqh)(float16 x, uint32_t shift, void *fpst)
11528{
11529 return float64_to_int64(do_prescale_fp16(x, shift, fpst), fpst);
11530}
11531
11532uint64_t HELPER(vfp_touqh)(float16 x, uint32_t shift, void *fpst)
11533{
11534 return float64_to_uint64(do_prescale_fp16(x, shift, fpst), fpst);
11535}
11536
52a1f6a3
AG
11537/* Set the current fp rounding mode and return the old one.
11538 * The argument is a softfloat float_round_ value.
11539 */
9b049916 11540uint32_t HELPER(set_rmode)(uint32_t rmode, void *fpstp)
52a1f6a3 11541{
9b049916 11542 float_status *fp_status = fpstp;
52a1f6a3
AG
11543
11544 uint32_t prev_rmode = get_float_rounding_mode(fp_status);
11545 set_float_rounding_mode(rmode, fp_status);
11546
11547 return prev_rmode;
11548}
11549
43630e58
WN
11550/* Set the current fp rounding mode in the standard fp status and return
11551 * the old one. This is for NEON instructions that need to change the
11552 * rounding mode but wish to use the standard FPSCR values for everything
11553 * else. Always set the rounding mode back to the correct value after
11554 * modifying it.
11555 * The argument is a softfloat float_round_ value.
11556 */
11557uint32_t HELPER(set_neon_rmode)(uint32_t rmode, CPUARMState *env)
11558{
11559 float_status *fp_status = &env->vfp.standard_fp_status;
11560
11561 uint32_t prev_rmode = get_float_rounding_mode(fp_status);
11562 set_float_rounding_mode(rmode, fp_status);
11563
11564 return prev_rmode;
11565}
11566
60011498 11567/* Half precision conversions. */
486624fc 11568float32 HELPER(vfp_fcvt_f16_to_f32)(float16 a, void *fpstp, uint32_t ahp_mode)
60011498 11569{
0acb9e7c
AB
11570 /* Squash FZ16 to 0 for the duration of conversion. In this case,
11571 * it would affect flushing input denormals.
11572 */
11573 float_status *fpst = fpstp;
11574 flag save = get_flush_inputs_to_zero(fpst);
11575 set_flush_inputs_to_zero(false, fpst);
11576 float32 r = float16_to_float32(a, !ahp_mode, fpst);
11577 set_flush_inputs_to_zero(save, fpst);
11578 return r;
2d981da7
PM
11579}
11580
486624fc 11581float16 HELPER(vfp_fcvt_f32_to_f16)(float32 a, void *fpstp, uint32_t ahp_mode)
2d981da7 11582{
0acb9e7c
AB
11583 /* Squash FZ16 to 0 for the duration of conversion. In this case,
11584 * it would affect flushing output denormals.
11585 */
11586 float_status *fpst = fpstp;
11587 flag save = get_flush_to_zero(fpst);
11588 set_flush_to_zero(false, fpst);
11589 float16 r = float32_to_float16(a, !ahp_mode, fpst);
11590 set_flush_to_zero(save, fpst);
11591 return r;
2d981da7
PM
11592}
11593
486624fc 11594float64 HELPER(vfp_fcvt_f16_to_f64)(float16 a, void *fpstp, uint32_t ahp_mode)
2d981da7 11595{
0acb9e7c
AB
11596 /* Squash FZ16 to 0 for the duration of conversion. In this case,
11597 * it would affect flushing input denormals.
11598 */
11599 float_status *fpst = fpstp;
11600 flag save = get_flush_inputs_to_zero(fpst);
11601 set_flush_inputs_to_zero(false, fpst);
11602 float64 r = float16_to_float64(a, !ahp_mode, fpst);
11603 set_flush_inputs_to_zero(save, fpst);
11604 return r;
2d981da7
PM
11605}
11606
486624fc 11607float16 HELPER(vfp_fcvt_f64_to_f16)(float64 a, void *fpstp, uint32_t ahp_mode)
2d981da7 11608{
0acb9e7c
AB
11609 /* Squash FZ16 to 0 for the duration of conversion. In this case,
11610 * it would affect flushing output denormals.
11611 */
11612 float_status *fpst = fpstp;
11613 flag save = get_flush_to_zero(fpst);
11614 set_flush_to_zero(false, fpst);
11615 float16 r = float64_to_float16(a, !ahp_mode, fpst);
11616 set_flush_to_zero(save, fpst);
11617 return r;
8900aad2
PM
11618}
11619
dda3ec49 11620#define float32_two make_float32(0x40000000)
6aae3df1
PM
11621#define float32_three make_float32(0x40400000)
11622#define float32_one_point_five make_float32(0x3fc00000)
dda3ec49 11623
0ecb72a5 11624float32 HELPER(recps_f32)(float32 a, float32 b, CPUARMState *env)
4373f3ce 11625{
dda3ec49
PM
11626 float_status *s = &env->vfp.standard_fp_status;
11627 if ((float32_is_infinity(a) && float32_is_zero_or_denormal(b)) ||
11628 (float32_is_infinity(b) && float32_is_zero_or_denormal(a))) {
43fe9bdb
PM
11629 if (!(float32_is_zero(a) || float32_is_zero(b))) {
11630 float_raise(float_flag_input_denormal, s);
11631 }
dda3ec49
PM
11632 return float32_two;
11633 }
11634 return float32_sub(float32_two, float32_mul(a, b, s), s);
4373f3ce
PB
11635}
11636
0ecb72a5 11637float32 HELPER(rsqrts_f32)(float32 a, float32 b, CPUARMState *env)
4373f3ce 11638{
71826966 11639 float_status *s = &env->vfp.standard_fp_status;
9ea62f57
PM
11640 float32 product;
11641 if ((float32_is_infinity(a) && float32_is_zero_or_denormal(b)) ||
11642 (float32_is_infinity(b) && float32_is_zero_or_denormal(a))) {
43fe9bdb
PM
11643 if (!(float32_is_zero(a) || float32_is_zero(b))) {
11644 float_raise(float_flag_input_denormal, s);
11645 }
6aae3df1 11646 return float32_one_point_five;
9ea62f57 11647 }
6aae3df1
PM
11648 product = float32_mul(a, b, s);
11649 return float32_div(float32_sub(float32_three, product, s), float32_two, s);
4373f3ce
PB
11650}
11651
8f8e3aa4
PB
11652/* NEON helpers. */
11653
56bf4fe2
CL
11654/* Constants 256 and 512 are used in some helpers; we avoid relying on
11655 * int->float conversions at run-time. */
11656#define float64_256 make_float64(0x4070000000000000LL)
11657#define float64_512 make_float64(0x4080000000000000LL)
5eb70735 11658#define float16_maxnorm make_float16(0x7bff)
b6d4443a
AB
11659#define float32_maxnorm make_float32(0x7f7fffff)
11660#define float64_maxnorm make_float64(0x7fefffffffffffffLL)
56bf4fe2 11661
b6d4443a
AB
11662/* Reciprocal functions
11663 *
11664 * The algorithm that must be used to calculate the estimate
5eb70735 11665 * is specified by the ARM ARM, see FPRecipEstimate()/RecipEstimate
fe0e4872 11666 */
b6d4443a 11667
5eb70735
AB
11668/* See RecipEstimate()
11669 *
11670 * input is a 9 bit fixed point number
11671 * input range 256 .. 511 for a number from 0.5 <= x < 1.0.
11672 * result range 256 .. 511 for a number from 1.0 to 511/256.
11673 */
fe0e4872 11674
5eb70735
AB
11675static int recip_estimate(int input)
11676{
11677 int a, b, r;
11678 assert(256 <= input && input < 512);
11679 a = (input * 2) + 1;
11680 b = (1 << 19) / a;
11681 r = (b + 1) >> 1;
11682 assert(256 <= r && r < 512);
11683 return r;
fe0e4872
CL
11684}
11685
5eb70735
AB
11686/*
11687 * Common wrapper to call recip_estimate
11688 *
11689 * The parameters are exponent and 64 bit fraction (without implicit
11690 * bit) where the binary point is nominally at bit 52. Returns a
11691 * float64 which can then be rounded to the appropriate size by the
11692 * callee.
11693 */
11694
11695static uint64_t call_recip_estimate(int *exp, int exp_off, uint64_t frac)
4373f3ce 11696{
5eb70735
AB
11697 uint32_t scaled, estimate;
11698 uint64_t result_frac;
11699 int result_exp;
fe0e4872 11700
5eb70735
AB
11701 /* Handle sub-normals */
11702 if (*exp == 0) {
b6d4443a 11703 if (extract64(frac, 51, 1) == 0) {
5eb70735
AB
11704 *exp = -1;
11705 frac <<= 2;
b6d4443a 11706 } else {
5eb70735 11707 frac <<= 1;
b6d4443a
AB
11708 }
11709 }
fe0e4872 11710
5eb70735
AB
11711 /* scaled = UInt('1':fraction<51:44>) */
11712 scaled = deposit32(1 << 8, 0, 8, extract64(frac, 44, 8));
11713 estimate = recip_estimate(scaled);
b6d4443a 11714
5eb70735
AB
11715 result_exp = exp_off - *exp;
11716 result_frac = deposit64(0, 44, 8, estimate);
11717 if (result_exp == 0) {
11718 result_frac = deposit64(result_frac >> 1, 51, 1, 1);
11719 } else if (result_exp == -1) {
11720 result_frac = deposit64(result_frac >> 2, 50, 2, 1);
11721 result_exp = 0;
b6d4443a
AB
11722 }
11723
5eb70735
AB
11724 *exp = result_exp;
11725
11726 return result_frac;
b6d4443a
AB
11727}
11728
11729static bool round_to_inf(float_status *fpst, bool sign_bit)
11730{
11731 switch (fpst->float_rounding_mode) {
11732 case float_round_nearest_even: /* Round to Nearest */
11733 return true;
11734 case float_round_up: /* Round to +Inf */
11735 return !sign_bit;
11736 case float_round_down: /* Round to -Inf */
11737 return sign_bit;
11738 case float_round_to_zero: /* Round to Zero */
11739 return false;
11740 }
11741
11742 g_assert_not_reached();
11743}
11744
5eb70735
AB
11745float16 HELPER(recpe_f16)(float16 input, void *fpstp)
11746{
11747 float_status *fpst = fpstp;
11748 float16 f16 = float16_squash_input_denormal(input, fpst);
11749 uint32_t f16_val = float16_val(f16);
11750 uint32_t f16_sign = float16_is_neg(f16);
11751 int f16_exp = extract32(f16_val, 10, 5);
11752 uint32_t f16_frac = extract32(f16_val, 0, 10);
11753 uint64_t f64_frac;
11754
11755 if (float16_is_any_nan(f16)) {
11756 float16 nan = f16;
11757 if (float16_is_signaling_nan(f16, fpst)) {
11758 float_raise(float_flag_invalid, fpst);
d7ecc062 11759 nan = float16_silence_nan(f16, fpst);
5eb70735
AB
11760 }
11761 if (fpst->default_nan_mode) {
11762 nan = float16_default_nan(fpst);
11763 }
11764 return nan;
11765 } else if (float16_is_infinity(f16)) {
11766 return float16_set_sign(float16_zero, float16_is_neg(f16));
11767 } else if (float16_is_zero(f16)) {
11768 float_raise(float_flag_divbyzero, fpst);
11769 return float16_set_sign(float16_infinity, float16_is_neg(f16));
11770 } else if (float16_abs(f16) < (1 << 8)) {
11771 /* Abs(value) < 2.0^-16 */
11772 float_raise(float_flag_overflow | float_flag_inexact, fpst);
11773 if (round_to_inf(fpst, f16_sign)) {
11774 return float16_set_sign(float16_infinity, f16_sign);
11775 } else {
11776 return float16_set_sign(float16_maxnorm, f16_sign);
11777 }
11778 } else if (f16_exp >= 29 && fpst->flush_to_zero) {
11779 float_raise(float_flag_underflow, fpst);
11780 return float16_set_sign(float16_zero, float16_is_neg(f16));
11781 }
11782
11783 f64_frac = call_recip_estimate(&f16_exp, 29,
11784 ((uint64_t) f16_frac) << (52 - 10));
11785
11786 /* result = sign : result_exp<4:0> : fraction<51:42> */
11787 f16_val = deposit32(0, 15, 1, f16_sign);
11788 f16_val = deposit32(f16_val, 10, 5, f16_exp);
11789 f16_val = deposit32(f16_val, 0, 10, extract64(f64_frac, 52 - 10, 10));
11790 return make_float16(f16_val);
11791}
11792
b6d4443a
AB
11793float32 HELPER(recpe_f32)(float32 input, void *fpstp)
11794{
11795 float_status *fpst = fpstp;
11796 float32 f32 = float32_squash_input_denormal(input, fpst);
11797 uint32_t f32_val = float32_val(f32);
5eb70735
AB
11798 bool f32_sign = float32_is_neg(f32);
11799 int f32_exp = extract32(f32_val, 23, 8);
b6d4443a 11800 uint32_t f32_frac = extract32(f32_val, 0, 23);
5eb70735 11801 uint64_t f64_frac;
b6d4443a
AB
11802
11803 if (float32_is_any_nan(f32)) {
11804 float32 nan = f32;
af39bc8c 11805 if (float32_is_signaling_nan(f32, fpst)) {
b6d4443a 11806 float_raise(float_flag_invalid, fpst);
d7ecc062 11807 nan = float32_silence_nan(f32, fpst);
fe0e4872 11808 }
b6d4443a 11809 if (fpst->default_nan_mode) {
af39bc8c 11810 nan = float32_default_nan(fpst);
43fe9bdb 11811 }
b6d4443a
AB
11812 return nan;
11813 } else if (float32_is_infinity(f32)) {
11814 return float32_set_sign(float32_zero, float32_is_neg(f32));
11815 } else if (float32_is_zero(f32)) {
11816 float_raise(float_flag_divbyzero, fpst);
11817 return float32_set_sign(float32_infinity, float32_is_neg(f32));
5eb70735 11818 } else if (float32_abs(f32) < (1ULL << 21)) {
b6d4443a
AB
11819 /* Abs(value) < 2.0^-128 */
11820 float_raise(float_flag_overflow | float_flag_inexact, fpst);
5eb70735
AB
11821 if (round_to_inf(fpst, f32_sign)) {
11822 return float32_set_sign(float32_infinity, f32_sign);
b6d4443a 11823 } else {
5eb70735 11824 return float32_set_sign(float32_maxnorm, f32_sign);
b6d4443a
AB
11825 }
11826 } else if (f32_exp >= 253 && fpst->flush_to_zero) {
11827 float_raise(float_flag_underflow, fpst);
11828 return float32_set_sign(float32_zero, float32_is_neg(f32));
fe0e4872
CL
11829 }
11830
5eb70735
AB
11831 f64_frac = call_recip_estimate(&f32_exp, 253,
11832 ((uint64_t) f32_frac) << (52 - 23));
fe0e4872 11833
5eb70735
AB
11834 /* result = sign : result_exp<7:0> : fraction<51:29> */
11835 f32_val = deposit32(0, 31, 1, f32_sign);
11836 f32_val = deposit32(f32_val, 23, 8, f32_exp);
11837 f32_val = deposit32(f32_val, 0, 23, extract64(f64_frac, 52 - 23, 23));
11838 return make_float32(f32_val);
b6d4443a
AB
11839}
11840
11841float64 HELPER(recpe_f64)(float64 input, void *fpstp)
11842{
11843 float_status *fpst = fpstp;
11844 float64 f64 = float64_squash_input_denormal(input, fpst);
11845 uint64_t f64_val = float64_val(f64);
5eb70735
AB
11846 bool f64_sign = float64_is_neg(f64);
11847 int f64_exp = extract64(f64_val, 52, 11);
11848 uint64_t f64_frac = extract64(f64_val, 0, 52);
b6d4443a
AB
11849
11850 /* Deal with any special cases */
11851 if (float64_is_any_nan(f64)) {
11852 float64 nan = f64;
af39bc8c 11853 if (float64_is_signaling_nan(f64, fpst)) {
b6d4443a 11854 float_raise(float_flag_invalid, fpst);
d7ecc062 11855 nan = float64_silence_nan(f64, fpst);
b6d4443a
AB
11856 }
11857 if (fpst->default_nan_mode) {
af39bc8c 11858 nan = float64_default_nan(fpst);
b6d4443a
AB
11859 }
11860 return nan;
11861 } else if (float64_is_infinity(f64)) {
11862 return float64_set_sign(float64_zero, float64_is_neg(f64));
11863 } else if (float64_is_zero(f64)) {
11864 float_raise(float_flag_divbyzero, fpst);
11865 return float64_set_sign(float64_infinity, float64_is_neg(f64));
11866 } else if ((f64_val & ~(1ULL << 63)) < (1ULL << 50)) {
11867 /* Abs(value) < 2.0^-1024 */
11868 float_raise(float_flag_overflow | float_flag_inexact, fpst);
5eb70735
AB
11869 if (round_to_inf(fpst, f64_sign)) {
11870 return float64_set_sign(float64_infinity, f64_sign);
b6d4443a 11871 } else {
5eb70735 11872 return float64_set_sign(float64_maxnorm, f64_sign);
b6d4443a 11873 }
fc1792e9 11874 } else if (f64_exp >= 2045 && fpst->flush_to_zero) {
b6d4443a
AB
11875 float_raise(float_flag_underflow, fpst);
11876 return float64_set_sign(float64_zero, float64_is_neg(f64));
11877 }
fe0e4872 11878
5eb70735 11879 f64_frac = call_recip_estimate(&f64_exp, 2045, f64_frac);
fe0e4872 11880
5eb70735
AB
11881 /* result = sign : result_exp<10:0> : fraction<51:0>; */
11882 f64_val = deposit64(0, 63, 1, f64_sign);
11883 f64_val = deposit64(f64_val, 52, 11, f64_exp);
11884 f64_val = deposit64(f64_val, 0, 52, f64_frac);
11885 return make_float64(f64_val);
4373f3ce
PB
11886}
11887
e07be5d2
CL
11888/* The algorithm that must be used to calculate the estimate
11889 * is specified by the ARM ARM.
11890 */
d719cbc7
AB
11891
11892static int do_recip_sqrt_estimate(int a)
11893{
11894 int b, estimate;
11895
11896 assert(128 <= a && a < 512);
11897 if (a < 256) {
11898 a = a * 2 + 1;
e07be5d2 11899 } else {
d719cbc7
AB
11900 a = (a >> 1) << 1;
11901 a = (a + 1) * 2;
11902 }
11903 b = 512;
11904 while (a * (b + 1) * (b + 1) < (1 << 28)) {
11905 b += 1;
11906 }
11907 estimate = (b + 1) / 2;
11908 assert(256 <= estimate && estimate < 512);
11909
11910 return estimate;
11911}
11912
e07be5d2 11913
d719cbc7
AB
11914static uint64_t recip_sqrt_estimate(int *exp , int exp_off, uint64_t frac)
11915{
11916 int estimate;
11917 uint32_t scaled;
e07be5d2 11918
d719cbc7
AB
11919 if (*exp == 0) {
11920 while (extract64(frac, 51, 1) == 0) {
11921 frac = frac << 1;
11922 *exp -= 1;
11923 }
11924 frac = extract64(frac, 0, 51) << 1;
e07be5d2 11925 }
e07be5d2 11926
d719cbc7
AB
11927 if (*exp & 1) {
11928 /* scaled = UInt('01':fraction<51:45>) */
11929 scaled = deposit32(1 << 7, 0, 7, extract64(frac, 45, 7));
11930 } else {
11931 /* scaled = UInt('1':fraction<51:44>) */
11932 scaled = deposit32(1 << 8, 0, 8, extract64(frac, 44, 8));
11933 }
11934 estimate = do_recip_sqrt_estimate(scaled);
e07be5d2 11935
d719cbc7
AB
11936 *exp = (exp_off - *exp) / 2;
11937 return extract64(estimate, 0, 8) << 44;
11938}
11939
11940float16 HELPER(rsqrte_f16)(float16 input, void *fpstp)
11941{
11942 float_status *s = fpstp;
11943 float16 f16 = float16_squash_input_denormal(input, s);
11944 uint16_t val = float16_val(f16);
11945 bool f16_sign = float16_is_neg(f16);
11946 int f16_exp = extract32(val, 10, 5);
11947 uint16_t f16_frac = extract32(val, 0, 10);
11948 uint64_t f64_frac;
11949
11950 if (float16_is_any_nan(f16)) {
11951 float16 nan = f16;
11952 if (float16_is_signaling_nan(f16, s)) {
11953 float_raise(float_flag_invalid, s);
d7ecc062 11954 nan = float16_silence_nan(f16, s);
d719cbc7
AB
11955 }
11956 if (s->default_nan_mode) {
11957 nan = float16_default_nan(s);
11958 }
11959 return nan;
11960 } else if (float16_is_zero(f16)) {
11961 float_raise(float_flag_divbyzero, s);
11962 return float16_set_sign(float16_infinity, f16_sign);
11963 } else if (f16_sign) {
11964 float_raise(float_flag_invalid, s);
11965 return float16_default_nan(s);
11966 } else if (float16_is_infinity(f16)) {
11967 return float16_zero;
11968 }
11969
11970 /* Scale and normalize to a double-precision value between 0.25 and 1.0,
11971 * preserving the parity of the exponent. */
11972
11973 f64_frac = ((uint64_t) f16_frac) << (52 - 10);
11974
11975 f64_frac = recip_sqrt_estimate(&f16_exp, 44, f64_frac);
11976
11977 /* result = sign : result_exp<4:0> : estimate<7:0> : Zeros(2) */
11978 val = deposit32(0, 15, 1, f16_sign);
11979 val = deposit32(val, 10, 5, f16_exp);
11980 val = deposit32(val, 2, 8, extract64(f64_frac, 52 - 8, 8));
11981 return make_float16(val);
e07be5d2
CL
11982}
11983
c2fb418e 11984float32 HELPER(rsqrte_f32)(float32 input, void *fpstp)
4373f3ce 11985{
c2fb418e
AB
11986 float_status *s = fpstp;
11987 float32 f32 = float32_squash_input_denormal(input, s);
11988 uint32_t val = float32_val(f32);
d719cbc7
AB
11989 uint32_t f32_sign = float32_is_neg(f32);
11990 int f32_exp = extract32(val, 23, 8);
c2fb418e
AB
11991 uint32_t f32_frac = extract32(val, 0, 23);
11992 uint64_t f64_frac;
e07be5d2 11993
c2fb418e
AB
11994 if (float32_is_any_nan(f32)) {
11995 float32 nan = f32;
af39bc8c 11996 if (float32_is_signaling_nan(f32, s)) {
e07be5d2 11997 float_raise(float_flag_invalid, s);
d7ecc062 11998 nan = float32_silence_nan(f32, s);
e07be5d2 11999 }
c2fb418e 12000 if (s->default_nan_mode) {
af39bc8c 12001 nan = float32_default_nan(s);
43fe9bdb 12002 }
c2fb418e
AB
12003 return nan;
12004 } else if (float32_is_zero(f32)) {
e07be5d2 12005 float_raise(float_flag_divbyzero, s);
c2fb418e
AB
12006 return float32_set_sign(float32_infinity, float32_is_neg(f32));
12007 } else if (float32_is_neg(f32)) {
e07be5d2 12008 float_raise(float_flag_invalid, s);
af39bc8c 12009 return float32_default_nan(s);
c2fb418e 12010 } else if (float32_is_infinity(f32)) {
e07be5d2
CL
12011 return float32_zero;
12012 }
12013
c2fb418e 12014 /* Scale and normalize to a double-precision value between 0.25 and 1.0,
e07be5d2 12015 * preserving the parity of the exponent. */
c2fb418e
AB
12016
12017 f64_frac = ((uint64_t) f32_frac) << 29;
e07be5d2 12018
d719cbc7 12019 f64_frac = recip_sqrt_estimate(&f32_exp, 380, f64_frac);
e07be5d2 12020
d719cbc7
AB
12021 /* result = sign : result_exp<4:0> : estimate<7:0> : Zeros(15) */
12022 val = deposit32(0, 31, 1, f32_sign);
12023 val = deposit32(val, 23, 8, f32_exp);
12024 val = deposit32(val, 15, 8, extract64(f64_frac, 52 - 8, 8));
e07be5d2 12025 return make_float32(val);
4373f3ce
PB
12026}
12027
c2fb418e
AB
12028float64 HELPER(rsqrte_f64)(float64 input, void *fpstp)
12029{
12030 float_status *s = fpstp;
12031 float64 f64 = float64_squash_input_denormal(input, s);
12032 uint64_t val = float64_val(f64);
d719cbc7
AB
12033 bool f64_sign = float64_is_neg(f64);
12034 int f64_exp = extract64(val, 52, 11);
c2fb418e 12035 uint64_t f64_frac = extract64(val, 0, 52);
c2fb418e
AB
12036
12037 if (float64_is_any_nan(f64)) {
12038 float64 nan = f64;
af39bc8c 12039 if (float64_is_signaling_nan(f64, s)) {
c2fb418e 12040 float_raise(float_flag_invalid, s);
d7ecc062 12041 nan = float64_silence_nan(f64, s);
c2fb418e
AB
12042 }
12043 if (s->default_nan_mode) {
af39bc8c 12044 nan = float64_default_nan(s);
c2fb418e
AB
12045 }
12046 return nan;
12047 } else if (float64_is_zero(f64)) {
12048 float_raise(float_flag_divbyzero, s);
12049 return float64_set_sign(float64_infinity, float64_is_neg(f64));
12050 } else if (float64_is_neg(f64)) {
12051 float_raise(float_flag_invalid, s);
af39bc8c 12052 return float64_default_nan(s);
c2fb418e
AB
12053 } else if (float64_is_infinity(f64)) {
12054 return float64_zero;
12055 }
12056
d719cbc7 12057 f64_frac = recip_sqrt_estimate(&f64_exp, 3068, f64_frac);
c2fb418e 12058
d719cbc7
AB
12059 /* result = sign : result_exp<4:0> : estimate<7:0> : Zeros(44) */
12060 val = deposit64(0, 61, 1, f64_sign);
12061 val = deposit64(val, 52, 11, f64_exp);
12062 val = deposit64(val, 44, 8, extract64(f64_frac, 52 - 8, 8));
12063 return make_float64(val);
c2fb418e
AB
12064}
12065
b6d4443a 12066uint32_t HELPER(recpe_u32)(uint32_t a, void *fpstp)
4373f3ce 12067{
5eb70735
AB
12068 /* float_status *s = fpstp; */
12069 int input, estimate;
fe0e4872
CL
12070
12071 if ((a & 0x80000000) == 0) {
12072 return 0xffffffff;
12073 }
12074
5eb70735
AB
12075 input = extract32(a, 23, 9);
12076 estimate = recip_estimate(input);
fe0e4872 12077
5eb70735 12078 return deposit32(0, (32 - 9), 9, estimate);
4373f3ce
PB
12079}
12080
c2fb418e 12081uint32_t HELPER(rsqrte_u32)(uint32_t a, void *fpstp)
4373f3ce 12082{
d719cbc7 12083 int estimate;
e07be5d2
CL
12084
12085 if ((a & 0xc0000000) == 0) {
12086 return 0xffffffff;
12087 }
12088
d719cbc7 12089 estimate = do_recip_sqrt_estimate(extract32(a, 23, 9));
e07be5d2 12090
d719cbc7 12091 return deposit32(0, 23, 9, estimate);
4373f3ce 12092}
fe1479c3 12093
da97f52c
PM
12094/* VFPv4 fused multiply-accumulate */
12095float32 VFP_HELPER(muladd, s)(float32 a, float32 b, float32 c, void *fpstp)
12096{
12097 float_status *fpst = fpstp;
12098 return float32_muladd(a, b, c, 0, fpst);
12099}
12100
12101float64 VFP_HELPER(muladd, d)(float64 a, float64 b, float64 c, void *fpstp)
12102{
12103 float_status *fpst = fpstp;
12104 return float64_muladd(a, b, c, 0, fpst);
12105}
d9b0848d
PM
12106
12107/* ARMv8 round to integral */
12108float32 HELPER(rints_exact)(float32 x, void *fp_status)
12109{
12110 return float32_round_to_int(x, fp_status);
12111}
12112
12113float64 HELPER(rintd_exact)(float64 x, void *fp_status)
12114{
12115 return float64_round_to_int(x, fp_status);
12116}
12117
12118float32 HELPER(rints)(float32 x, void *fp_status)
12119{
12120 int old_flags = get_float_exception_flags(fp_status), new_flags;
12121 float32 ret;
12122
12123 ret = float32_round_to_int(x, fp_status);
12124
12125 /* Suppress any inexact exceptions the conversion produced */
12126 if (!(old_flags & float_flag_inexact)) {
12127 new_flags = get_float_exception_flags(fp_status);
12128 set_float_exception_flags(new_flags & ~float_flag_inexact, fp_status);
12129 }
12130
12131 return ret;
12132}
12133
12134float64 HELPER(rintd)(float64 x, void *fp_status)
12135{
12136 int old_flags = get_float_exception_flags(fp_status), new_flags;
12137 float64 ret;
12138
12139 ret = float64_round_to_int(x, fp_status);
12140
12141 new_flags = get_float_exception_flags(fp_status);
12142
12143 /* Suppress any inexact exceptions the conversion produced */
12144 if (!(old_flags & float_flag_inexact)) {
12145 new_flags = get_float_exception_flags(fp_status);
12146 set_float_exception_flags(new_flags & ~float_flag_inexact, fp_status);
12147 }
12148
12149 return ret;
12150}
9972da66
WN
12151
12152/* Convert ARM rounding mode to softfloat */
12153int arm_rmode_to_sf(int rmode)
12154{
12155 switch (rmode) {
12156 case FPROUNDING_TIEAWAY:
12157 rmode = float_round_ties_away;
12158 break;
12159 case FPROUNDING_ODD:
12160 /* FIXME: add support for TIEAWAY and ODD */
12161 qemu_log_mask(LOG_UNIMP, "arm: unimplemented rounding mode: %d\n",
12162 rmode);
12163 case FPROUNDING_TIEEVEN:
12164 default:
12165 rmode = float_round_nearest_even;
12166 break;
12167 case FPROUNDING_POSINF:
12168 rmode = float_round_up;
12169 break;
12170 case FPROUNDING_NEGINF:
12171 rmode = float_round_down;
12172 break;
12173 case FPROUNDING_ZERO:
12174 rmode = float_round_to_zero;
12175 break;
12176 }
12177 return rmode;
12178}
eb0ecd5a 12179
aa633469
PM
12180/* CRC helpers.
12181 * The upper bytes of val (above the number specified by 'bytes') must have
12182 * been zeroed out by the caller.
12183 */
eb0ecd5a
WN
12184uint32_t HELPER(crc32)(uint32_t acc, uint32_t val, uint32_t bytes)
12185{
12186 uint8_t buf[4];
12187
aa633469 12188 stl_le_p(buf, val);
eb0ecd5a
WN
12189
12190 /* zlib crc32 converts the accumulator and output to one's complement. */
12191 return crc32(acc ^ 0xffffffff, buf, bytes) ^ 0xffffffff;
12192}
12193
12194uint32_t HELPER(crc32c)(uint32_t acc, uint32_t val, uint32_t bytes)
12195{
12196 uint8_t buf[4];
12197
aa633469 12198 stl_le_p(buf, val);
eb0ecd5a
WN
12199
12200 /* Linux crc32c converts the output to one's complement. */
12201 return crc32c(acc, buf, bytes) ^ 0xffffffff;
12202}
a9e01311
RH
12203
12204/* Return the exception level to which FP-disabled exceptions should
12205 * be taken, or 0 if FP is enabled.
12206 */
12207static inline int fp_exception_el(CPUARMState *env)
12208{
55faa212 12209#ifndef CONFIG_USER_ONLY
a9e01311
RH
12210 int fpen;
12211 int cur_el = arm_current_el(env);
12212
12213 /* CPACR and the CPTR registers don't exist before v6, so FP is
12214 * always accessible
12215 */
12216 if (!arm_feature(env, ARM_FEATURE_V6)) {
12217 return 0;
12218 }
12219
12220 /* The CPACR controls traps to EL1, or PL1 if we're 32 bit:
12221 * 0, 2 : trap EL0 and EL1/PL1 accesses
12222 * 1 : trap only EL0 accesses
12223 * 3 : trap no accesses
12224 */
12225 fpen = extract32(env->cp15.cpacr_el1, 20, 2);
12226 switch (fpen) {
12227 case 0:
12228 case 2:
12229 if (cur_el == 0 || cur_el == 1) {
12230 /* Trap to PL1, which might be EL1 or EL3 */
12231 if (arm_is_secure(env) && !arm_el_is_aa64(env, 3)) {
12232 return 3;
12233 }
12234 return 1;
12235 }
12236 if (cur_el == 3 && !is_a64(env)) {
12237 /* Secure PL1 running at EL3 */
12238 return 3;
12239 }
12240 break;
12241 case 1:
12242 if (cur_el == 0) {
12243 return 1;
12244 }
12245 break;
12246 case 3:
12247 break;
12248 }
12249
12250 /* For the CPTR registers we don't need to guard with an ARM_FEATURE
12251 * check because zero bits in the registers mean "don't trap".
12252 */
12253
12254 /* CPTR_EL2 : present in v7VE or v8 */
12255 if (cur_el <= 2 && extract32(env->cp15.cptr_el[2], 10, 1)
12256 && !arm_is_secure_below_el3(env)) {
12257 /* Trap FP ops at EL2, NS-EL1 or NS-EL0 to EL2 */
12258 return 2;
12259 }
12260
12261 /* CPTR_EL3 : present in v8 */
12262 if (extract32(env->cp15.cptr_el[3], 10, 1)) {
12263 /* Trap all FP ops to EL3 */
12264 return 3;
12265 }
55faa212 12266#endif
a9e01311
RH
12267 return 0;
12268}
12269
12270void cpu_get_tb_cpu_state(CPUARMState *env, target_ulong *pc,
b9adaa70 12271 target_ulong *cs_base, uint32_t *pflags)
a9e01311
RH
12272{
12273 ARMMMUIdx mmu_idx = core_to_arm_mmu_idx(env, cpu_mmu_index(env, false));
1db5e96c 12274 int fp_el = fp_exception_el(env);
b9adaa70
RH
12275 uint32_t flags;
12276
a9e01311 12277 if (is_a64(env)) {
1db5e96c
RH
12278 int sve_el = sve_exception_el(env);
12279 uint32_t zcr_len;
12280
a9e01311 12281 *pc = env->pc;
b9adaa70 12282 flags = ARM_TBFLAG_AARCH64_STATE_MASK;
a9e01311 12283 /* Get control bits for tagged addresses */
b9adaa70
RH
12284 flags |= (arm_regime_tbi0(env, mmu_idx) << ARM_TBFLAG_TBI0_SHIFT);
12285 flags |= (arm_regime_tbi1(env, mmu_idx) << ARM_TBFLAG_TBI1_SHIFT);
1db5e96c
RH
12286 flags |= sve_el << ARM_TBFLAG_SVEEXC_EL_SHIFT;
12287
12288 /* If SVE is disabled, but FP is enabled,
12289 then the effective len is 0. */
12290 if (sve_el != 0 && fp_el == 0) {
12291 zcr_len = 0;
12292 } else {
12293 int current_el = arm_current_el(env);
12294
12295 zcr_len = env->vfp.zcr_el[current_el <= 1 ? 1 : current_el];
12296 zcr_len &= 0xf;
12297 if (current_el < 2 && arm_feature(env, ARM_FEATURE_EL2)) {
12298 zcr_len = MIN(zcr_len, 0xf & (uint32_t)env->vfp.zcr_el[2]);
12299 }
12300 if (current_el < 3 && arm_feature(env, ARM_FEATURE_EL3)) {
12301 zcr_len = MIN(zcr_len, 0xf & (uint32_t)env->vfp.zcr_el[3]);
12302 }
12303 }
12304 flags |= zcr_len << ARM_TBFLAG_ZCR_LEN_SHIFT;
a9e01311
RH
12305 } else {
12306 *pc = env->regs[15];
b9adaa70 12307 flags = (env->thumb << ARM_TBFLAG_THUMB_SHIFT)
a9e01311
RH
12308 | (env->vfp.vec_len << ARM_TBFLAG_VECLEN_SHIFT)
12309 | (env->vfp.vec_stride << ARM_TBFLAG_VECSTRIDE_SHIFT)
12310 | (env->condexec_bits << ARM_TBFLAG_CONDEXEC_SHIFT)
12311 | (arm_sctlr_b(env) << ARM_TBFLAG_SCTLR_B_SHIFT);
12312 if (!(access_secure_reg(env))) {
b9adaa70 12313 flags |= ARM_TBFLAG_NS_MASK;
a9e01311
RH
12314 }
12315 if (env->vfp.xregs[ARM_VFP_FPEXC] & (1 << 30)
12316 || arm_el_is_aa64(env, 1)) {
b9adaa70 12317 flags |= ARM_TBFLAG_VFPEN_MASK;
a9e01311 12318 }
b9adaa70
RH
12319 flags |= (extract32(env->cp15.c15_cpar, 0, 2)
12320 << ARM_TBFLAG_XSCALE_CPAR_SHIFT);
a9e01311
RH
12321 }
12322
b9adaa70 12323 flags |= (arm_to_core_mmu_idx(mmu_idx) << ARM_TBFLAG_MMUIDX_SHIFT);
a9e01311
RH
12324
12325 /* The SS_ACTIVE and PSTATE_SS bits correspond to the state machine
12326 * states defined in the ARM ARM for software singlestep:
12327 * SS_ACTIVE PSTATE.SS State
12328 * 0 x Inactive (the TB flag for SS is always 0)
12329 * 1 0 Active-pending
12330 * 1 1 Active-not-pending
12331 */
12332 if (arm_singlestep_active(env)) {
b9adaa70 12333 flags |= ARM_TBFLAG_SS_ACTIVE_MASK;
a9e01311
RH
12334 if (is_a64(env)) {
12335 if (env->pstate & PSTATE_SS) {
b9adaa70 12336 flags |= ARM_TBFLAG_PSTATE_SS_MASK;
a9e01311
RH
12337 }
12338 } else {
12339 if (env->uncached_cpsr & PSTATE_SS) {
b9adaa70 12340 flags |= ARM_TBFLAG_PSTATE_SS_MASK;
a9e01311
RH
12341 }
12342 }
12343 }
12344 if (arm_cpu_data_is_big_endian(env)) {
b9adaa70 12345 flags |= ARM_TBFLAG_BE_DATA_MASK;
a9e01311 12346 }
1db5e96c 12347 flags |= fp_el << ARM_TBFLAG_FPEXC_EL_SHIFT;
a9e01311
RH
12348
12349 if (arm_v7m_is_handler_mode(env)) {
b9adaa70 12350 flags |= ARM_TBFLAG_HANDLER_MASK;
a9e01311
RH
12351 }
12352
b9adaa70 12353 *pflags = flags;
a9e01311
RH
12354 *cs_base = 0;
12355}