]> Git Repo - qemu.git/blame - target/arm/helper.c
target/arm: translate NS bit in page-walks
[qemu.git] / target / arm / helper.c
CommitLineData
ed3baad1
PMD
1/*
2 * ARM generic helpers.
3 *
4 * This code is licensed under the GNU GPL v2 or later.
5 *
6 * SPDX-License-Identifier: GPL-2.0-or-later
7 */
db725815 8
74c21bd0 9#include "qemu/osdep.h"
63159601 10#include "qemu/units.h"
181962fd 11#include "target/arm/idau.h"
194cbc49 12#include "trace.h"
b5ff1b31 13#include "cpu.h"
ccd38087 14#include "internals.h"
022c62cb 15#include "exec/gdbstub.h"
2ef6175a 16#include "exec/helper-proto.h"
1de7afc9 17#include "qemu/host-utils.h"
db725815 18#include "qemu/main-loop.h"
1de7afc9 19#include "qemu/bitops.h"
eb0ecd5a 20#include "qemu/crc32c.h"
0442428a 21#include "qemu/qemu-print.h"
63c91552 22#include "exec/exec-all.h"
eb0ecd5a 23#include <zlib.h> /* For crc32 */
64552b6b 24#include "hw/irq.h"
f1672e6f 25#include "hw/semihosting/semihost.h"
b2e23725 26#include "sysemu/cpus.h"
740b1759 27#include "sysemu/cpu-timers.h"
f3a9b694 28#include "sysemu/kvm.h"
2a609df8 29#include "sysemu/tcg.h"
9d2b5a58 30#include "qemu/range.h"
7f7b4e7a 31#include "qapi/qapi-commands-machine-target.h"
de390645
RH
32#include "qapi/error.h"
33#include "qemu/guest-random.h"
91f78c58
PMD
34#ifdef CONFIG_TCG
35#include "arm_ldst.h"
7aab5a8c 36#include "exec/cpu_ldst.h"
0bb446d8 37#include "hw/semihosting/common-semi.h"
91f78c58 38#endif
0b03bdfc 39
352c98e5
LV
40#define ARM_CPU_FREQ 1000000000 /* FIXME: 1 GHz, should be configurable */
41
4a501606 42#ifndef CONFIG_USER_ONLY
7c2cb42b 43
98e87797 44static bool get_phys_addr_lpae(CPUARMState *env, uint64_t address,
03ae85f8 45 MMUAccessType access_type, ARMMMUIdx mmu_idx,
ff7de2fc 46 bool s1_is_el0,
37785977 47 hwaddr *phys_ptr, MemTxAttrs *txattrs, int *prot,
da909b2c 48 target_ulong *page_size_ptr,
7e98e21c
RH
49 ARMMMUFaultInfo *fi, ARMCacheAttrs *cacheattrs)
50 __attribute__((nonnull));
4a501606
PM
51#endif
52
affdb64d 53static void switch_mode(CPUARMState *env, int mode);
ea04dce7 54static int aa64_va_parameter_tbi(uint64_t tcr, ARMMMUIdx mmu_idx);
affdb64d 55
a010bdbe 56static int vfp_gdb_get_reg(CPUARMState *env, GByteArray *buf, int reg)
56aebc89 57{
a6627f5f
RH
58 ARMCPU *cpu = env_archcpu(env);
59 int nregs = cpu_isar_feature(aa32_simd_r32, cpu) ? 32 : 16;
56aebc89
PB
60
61 /* VFP data registers are always little-endian. */
56aebc89 62 if (reg < nregs) {
a010bdbe 63 return gdb_get_reg64(buf, *aa32_vfp_dreg(env, reg));
56aebc89
PB
64 }
65 if (arm_feature(env, ARM_FEATURE_NEON)) {
66 /* Aliases for Q regs. */
67 nregs += 16;
68 if (reg < nregs) {
9a2b5256 69 uint64_t *q = aa32_vfp_qreg(env, reg - 32);
a010bdbe 70 return gdb_get_reg128(buf, q[0], q[1]);
56aebc89
PB
71 }
72 }
73 switch (reg - nregs) {
a010bdbe
AB
74 case 0: return gdb_get_reg32(buf, env->vfp.xregs[ARM_VFP_FPSID]); break;
75 case 1: return gdb_get_reg32(buf, vfp_get_fpscr(env)); break;
76 case 2: return gdb_get_reg32(buf, env->vfp.xregs[ARM_VFP_FPEXC]); break;
56aebc89
PB
77 }
78 return 0;
79}
80
0ecb72a5 81static int vfp_gdb_set_reg(CPUARMState *env, uint8_t *buf, int reg)
56aebc89 82{
a6627f5f
RH
83 ARMCPU *cpu = env_archcpu(env);
84 int nregs = cpu_isar_feature(aa32_simd_r32, cpu) ? 32 : 16;
56aebc89 85
56aebc89 86 if (reg < nregs) {
9a2b5256 87 *aa32_vfp_dreg(env, reg) = ldq_le_p(buf);
56aebc89
PB
88 return 8;
89 }
90 if (arm_feature(env, ARM_FEATURE_NEON)) {
91 nregs += 16;
92 if (reg < nregs) {
9a2b5256
RH
93 uint64_t *q = aa32_vfp_qreg(env, reg - 32);
94 q[0] = ldq_le_p(buf);
95 q[1] = ldq_le_p(buf + 8);
56aebc89
PB
96 return 16;
97 }
98 }
99 switch (reg - nregs) {
100 case 0: env->vfp.xregs[ARM_VFP_FPSID] = ldl_p(buf); return 4;
b0a909a4 101 case 1: vfp_set_fpscr(env, ldl_p(buf)); return 4;
71b3c3de 102 case 2: env->vfp.xregs[ARM_VFP_FPEXC] = ldl_p(buf) & (1 << 30); return 4;
56aebc89
PB
103 }
104 return 0;
105}
106
a010bdbe 107static int aarch64_fpu_gdb_get_reg(CPUARMState *env, GByteArray *buf, int reg)
6a669427
PM
108{
109 switch (reg) {
110 case 0 ... 31:
8b1ca58c
AB
111 {
112 /* 128 bit FP register - quads are in LE order */
113 uint64_t *q = aa64_vfp_qreg(env, reg);
114 return gdb_get_reg128(buf, q[1], q[0]);
115 }
6a669427
PM
116 case 32:
117 /* FPSR */
8b1ca58c 118 return gdb_get_reg32(buf, vfp_get_fpsr(env));
6a669427
PM
119 case 33:
120 /* FPCR */
8b1ca58c 121 return gdb_get_reg32(buf,vfp_get_fpcr(env));
6a669427
PM
122 default:
123 return 0;
124 }
125}
126
127static int aarch64_fpu_gdb_set_reg(CPUARMState *env, uint8_t *buf, int reg)
128{
129 switch (reg) {
130 case 0 ... 31:
131 /* 128 bit FP register */
9a2b5256
RH
132 {
133 uint64_t *q = aa64_vfp_qreg(env, reg);
134 q[0] = ldq_le_p(buf);
135 q[1] = ldq_le_p(buf + 8);
136 return 16;
137 }
6a669427
PM
138 case 32:
139 /* FPSR */
140 vfp_set_fpsr(env, ldl_p(buf));
141 return 4;
142 case 33:
143 /* FPCR */
144 vfp_set_fpcr(env, ldl_p(buf));
145 return 4;
146 default:
147 return 0;
148 }
149}
150
c4241c7d 151static uint64_t raw_read(CPUARMState *env, const ARMCPRegInfo *ri)
d4e6df63 152{
375421cc 153 assert(ri->fieldoffset);
67ed771d 154 if (cpreg_field_is_64bit(ri)) {
c4241c7d 155 return CPREG_FIELD64(env, ri);
22d9e1a9 156 } else {
c4241c7d 157 return CPREG_FIELD32(env, ri);
22d9e1a9 158 }
d4e6df63
PM
159}
160
c4241c7d
PM
161static void raw_write(CPUARMState *env, const ARMCPRegInfo *ri,
162 uint64_t value)
d4e6df63 163{
375421cc 164 assert(ri->fieldoffset);
67ed771d 165 if (cpreg_field_is_64bit(ri)) {
22d9e1a9
PM
166 CPREG_FIELD64(env, ri) = value;
167 } else {
168 CPREG_FIELD32(env, ri) = value;
169 }
d4e6df63
PM
170}
171
11f136ee
FA
172static void *raw_ptr(CPUARMState *env, const ARMCPRegInfo *ri)
173{
174 return (char *)env + ri->fieldoffset;
175}
176
49a66191 177uint64_t read_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri)
721fae12 178{
59a1c327 179 /* Raw read of a coprocessor register (as needed for migration, etc). */
721fae12 180 if (ri->type & ARM_CP_CONST) {
59a1c327 181 return ri->resetvalue;
721fae12 182 } else if (ri->raw_readfn) {
59a1c327 183 return ri->raw_readfn(env, ri);
721fae12 184 } else if (ri->readfn) {
59a1c327 185 return ri->readfn(env, ri);
721fae12 186 } else {
59a1c327 187 return raw_read(env, ri);
721fae12 188 }
721fae12
PM
189}
190
59a1c327 191static void write_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri,
7900e9f1 192 uint64_t v)
721fae12
PM
193{
194 /* Raw write of a coprocessor register (as needed for migration, etc).
721fae12
PM
195 * Note that constant registers are treated as write-ignored; the
196 * caller should check for success by whether a readback gives the
197 * value written.
198 */
199 if (ri->type & ARM_CP_CONST) {
59a1c327 200 return;
721fae12 201 } else if (ri->raw_writefn) {
c4241c7d 202 ri->raw_writefn(env, ri, v);
721fae12 203 } else if (ri->writefn) {
c4241c7d 204 ri->writefn(env, ri, v);
721fae12 205 } else {
afb2530f 206 raw_write(env, ri, v);
721fae12 207 }
721fae12
PM
208}
209
d12379c5
AB
210/**
211 * arm_get/set_gdb_*: get/set a gdb register
212 * @env: the CPU state
213 * @buf: a buffer to copy to/from
214 * @reg: register number (offset from start of group)
215 *
216 * We return the number of bytes copied
217 */
218
a010bdbe 219static int arm_gdb_get_sysreg(CPUARMState *env, GByteArray *buf, int reg)
200bf5b7 220{
2fc0cc0e 221 ARMCPU *cpu = env_archcpu(env);
200bf5b7
AB
222 const ARMCPRegInfo *ri;
223 uint32_t key;
224
448d4d14 225 key = cpu->dyn_sysreg_xml.data.cpregs.keys[reg];
200bf5b7
AB
226 ri = get_arm_cp_reginfo(cpu->cp_regs, key);
227 if (ri) {
228 if (cpreg_field_is_64bit(ri)) {
229 return gdb_get_reg64(buf, (uint64_t)read_raw_cp_reg(env, ri));
230 } else {
231 return gdb_get_reg32(buf, (uint32_t)read_raw_cp_reg(env, ri));
232 }
233 }
234 return 0;
235}
236
237static int arm_gdb_set_sysreg(CPUARMState *env, uint8_t *buf, int reg)
238{
239 return 0;
240}
241
d12379c5
AB
242#ifdef TARGET_AARCH64
243static int arm_gdb_get_svereg(CPUARMState *env, GByteArray *buf, int reg)
244{
245 ARMCPU *cpu = env_archcpu(env);
246
247 switch (reg) {
248 /* The first 32 registers are the zregs */
249 case 0 ... 31:
250 {
251 int vq, len = 0;
252 for (vq = 0; vq < cpu->sve_max_vq; vq++) {
253 len += gdb_get_reg128(buf,
254 env->vfp.zregs[reg].d[vq * 2 + 1],
255 env->vfp.zregs[reg].d[vq * 2]);
256 }
257 return len;
258 }
259 case 32:
260 return gdb_get_reg32(buf, vfp_get_fpsr(env));
261 case 33:
262 return gdb_get_reg32(buf, vfp_get_fpcr(env));
263 /* then 16 predicates and the ffr */
264 case 34 ... 50:
265 {
266 int preg = reg - 34;
267 int vq, len = 0;
268 for (vq = 0; vq < cpu->sve_max_vq; vq = vq + 4) {
269 len += gdb_get_reg64(buf, env->vfp.pregs[preg].p[vq / 4]);
270 }
271 return len;
272 }
273 case 51:
274 {
275 /*
276 * We report in Vector Granules (VG) which is 64bit in a Z reg
277 * while the ZCR works in Vector Quads (VQ) which is 128bit chunks.
278 */
279 int vq = sve_zcr_len_for_el(env, arm_current_el(env)) + 1;
797920b9 280 return gdb_get_reg64(buf, vq * 2);
d12379c5
AB
281 }
282 default:
283 /* gdbstub asked for something out our range */
284 qemu_log_mask(LOG_UNIMP, "%s: out of range register %d", __func__, reg);
285 break;
286 }
287
288 return 0;
289}
290
291static int arm_gdb_set_svereg(CPUARMState *env, uint8_t *buf, int reg)
292{
293 ARMCPU *cpu = env_archcpu(env);
294
295 /* The first 32 registers are the zregs */
296 switch (reg) {
297 /* The first 32 registers are the zregs */
298 case 0 ... 31:
299 {
300 int vq, len = 0;
301 uint64_t *p = (uint64_t *) buf;
302 for (vq = 0; vq < cpu->sve_max_vq; vq++) {
303 env->vfp.zregs[reg].d[vq * 2 + 1] = *p++;
304 env->vfp.zregs[reg].d[vq * 2] = *p++;
305 len += 16;
306 }
307 return len;
308 }
309 case 32:
310 vfp_set_fpsr(env, *(uint32_t *)buf);
311 return 4;
312 case 33:
313 vfp_set_fpcr(env, *(uint32_t *)buf);
314 return 4;
315 case 34 ... 50:
316 {
317 int preg = reg - 34;
318 int vq, len = 0;
319 uint64_t *p = (uint64_t *) buf;
320 for (vq = 0; vq < cpu->sve_max_vq; vq = vq + 4) {
321 env->vfp.pregs[preg].p[vq / 4] = *p++;
322 len += 8;
323 }
324 return len;
325 }
326 case 51:
327 /* cannot set vg via gdbstub */
328 return 0;
329 default:
330 /* gdbstub asked for something out our range */
331 break;
332 }
333
334 return 0;
335}
336#endif /* TARGET_AARCH64 */
337
375421cc
PM
338static bool raw_accessors_invalid(const ARMCPRegInfo *ri)
339{
340 /* Return true if the regdef would cause an assertion if you called
341 * read_raw_cp_reg() or write_raw_cp_reg() on it (ie if it is a
342 * program bug for it not to have the NO_RAW flag).
343 * NB that returning false here doesn't necessarily mean that calling
344 * read/write_raw_cp_reg() is safe, because we can't distinguish "has
345 * read/write access functions which are safe for raw use" from "has
346 * read/write access functions which have side effects but has forgotten
347 * to provide raw access functions".
348 * The tests here line up with the conditions in read/write_raw_cp_reg()
349 * and assertions in raw_read()/raw_write().
350 */
351 if ((ri->type & ARM_CP_CONST) ||
352 ri->fieldoffset ||
353 ((ri->raw_writefn || ri->writefn) && (ri->raw_readfn || ri->readfn))) {
354 return false;
355 }
356 return true;
357}
358
b698e4ee 359bool write_cpustate_to_list(ARMCPU *cpu, bool kvm_sync)
721fae12
PM
360{
361 /* Write the coprocessor state from cpu->env to the (index,value) list. */
362 int i;
363 bool ok = true;
364
365 for (i = 0; i < cpu->cpreg_array_len; i++) {
366 uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
367 const ARMCPRegInfo *ri;
b698e4ee 368 uint64_t newval;
59a1c327 369
60322b39 370 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
721fae12
PM
371 if (!ri) {
372 ok = false;
373 continue;
374 }
7a0e58fa 375 if (ri->type & ARM_CP_NO_RAW) {
721fae12
PM
376 continue;
377 }
b698e4ee
PM
378
379 newval = read_raw_cp_reg(&cpu->env, ri);
380 if (kvm_sync) {
381 /*
382 * Only sync if the previous list->cpustate sync succeeded.
383 * Rather than tracking the success/failure state for every
384 * item in the list, we just recheck "does the raw write we must
385 * have made in write_list_to_cpustate() read back OK" here.
386 */
387 uint64_t oldval = cpu->cpreg_values[i];
388
389 if (oldval == newval) {
390 continue;
391 }
392
393 write_raw_cp_reg(&cpu->env, ri, oldval);
394 if (read_raw_cp_reg(&cpu->env, ri) != oldval) {
395 continue;
396 }
397
398 write_raw_cp_reg(&cpu->env, ri, newval);
399 }
400 cpu->cpreg_values[i] = newval;
721fae12
PM
401 }
402 return ok;
403}
404
405bool write_list_to_cpustate(ARMCPU *cpu)
406{
407 int i;
408 bool ok = true;
409
410 for (i = 0; i < cpu->cpreg_array_len; i++) {
411 uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
412 uint64_t v = cpu->cpreg_values[i];
721fae12
PM
413 const ARMCPRegInfo *ri;
414
60322b39 415 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
721fae12
PM
416 if (!ri) {
417 ok = false;
418 continue;
419 }
7a0e58fa 420 if (ri->type & ARM_CP_NO_RAW) {
721fae12
PM
421 continue;
422 }
423 /* Write value and confirm it reads back as written
424 * (to catch read-only registers and partially read-only
425 * registers where the incoming migration value doesn't match)
426 */
59a1c327
PM
427 write_raw_cp_reg(&cpu->env, ri, v);
428 if (read_raw_cp_reg(&cpu->env, ri) != v) {
721fae12
PM
429 ok = false;
430 }
431 }
432 return ok;
433}
434
435static void add_cpreg_to_list(gpointer key, gpointer opaque)
436{
437 ARMCPU *cpu = opaque;
438 uint64_t regidx;
439 const ARMCPRegInfo *ri;
440
441 regidx = *(uint32_t *)key;
60322b39 442 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
721fae12 443
7a0e58fa 444 if (!(ri->type & (ARM_CP_NO_RAW|ARM_CP_ALIAS))) {
721fae12
PM
445 cpu->cpreg_indexes[cpu->cpreg_array_len] = cpreg_to_kvm_id(regidx);
446 /* The value array need not be initialized at this point */
447 cpu->cpreg_array_len++;
448 }
449}
450
451static void count_cpreg(gpointer key, gpointer opaque)
452{
453 ARMCPU *cpu = opaque;
454 uint64_t regidx;
455 const ARMCPRegInfo *ri;
456
457 regidx = *(uint32_t *)key;
60322b39 458 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
721fae12 459
7a0e58fa 460 if (!(ri->type & (ARM_CP_NO_RAW|ARM_CP_ALIAS))) {
721fae12
PM
461 cpu->cpreg_array_len++;
462 }
463}
464
465static gint cpreg_key_compare(gconstpointer a, gconstpointer b)
466{
cbf239b7
AR
467 uint64_t aidx = cpreg_to_kvm_id(*(uint32_t *)a);
468 uint64_t bidx = cpreg_to_kvm_id(*(uint32_t *)b);
721fae12 469
cbf239b7
AR
470 if (aidx > bidx) {
471 return 1;
472 }
473 if (aidx < bidx) {
474 return -1;
475 }
476 return 0;
721fae12
PM
477}
478
479void init_cpreg_list(ARMCPU *cpu)
480{
481 /* Initialise the cpreg_tuples[] array based on the cp_regs hash.
482 * Note that we require cpreg_tuples[] to be sorted by key ID.
483 */
57b6d95e 484 GList *keys;
721fae12
PM
485 int arraylen;
486
57b6d95e 487 keys = g_hash_table_get_keys(cpu->cp_regs);
721fae12
PM
488 keys = g_list_sort(keys, cpreg_key_compare);
489
490 cpu->cpreg_array_len = 0;
491
492 g_list_foreach(keys, count_cpreg, cpu);
493
494 arraylen = cpu->cpreg_array_len;
495 cpu->cpreg_indexes = g_new(uint64_t, arraylen);
496 cpu->cpreg_values = g_new(uint64_t, arraylen);
497 cpu->cpreg_vmstate_indexes = g_new(uint64_t, arraylen);
498 cpu->cpreg_vmstate_values = g_new(uint64_t, arraylen);
499 cpu->cpreg_vmstate_array_len = cpu->cpreg_array_len;
500 cpu->cpreg_array_len = 0;
501
502 g_list_foreach(keys, add_cpreg_to_list, cpu);
503
504 assert(cpu->cpreg_array_len == arraylen);
505
506 g_list_free(keys);
507}
508
68e9c2fe 509/*
93dd1e61 510 * Some registers are not accessible from AArch32 EL3 if SCR.NS == 0.
68e9c2fe
EI
511 */
512static CPAccessResult access_el3_aa32ns(CPUARMState *env,
3f208fd7
PM
513 const ARMCPRegInfo *ri,
514 bool isread)
68e9c2fe 515{
93dd1e61
EI
516 if (!is_a64(env) && arm_current_el(env) == 3 &&
517 arm_is_secure_below_el3(env)) {
68e9c2fe
EI
518 return CP_ACCESS_TRAP_UNCATEGORIZED;
519 }
520 return CP_ACCESS_OK;
521}
522
5513c3ab
PM
523/* Some secure-only AArch32 registers trap to EL3 if used from
524 * Secure EL1 (but are just ordinary UNDEF in other non-EL3 contexts).
525 * Note that an access from Secure EL1 can only happen if EL3 is AArch64.
526 * We assume that the .access field is set to PL1_RW.
527 */
528static CPAccessResult access_trap_aa32s_el1(CPUARMState *env,
3f208fd7
PM
529 const ARMCPRegInfo *ri,
530 bool isread)
5513c3ab
PM
531{
532 if (arm_current_el(env) == 3) {
533 return CP_ACCESS_OK;
534 }
535 if (arm_is_secure_below_el3(env)) {
536 return CP_ACCESS_TRAP_EL3;
537 }
538 /* This will be EL1 NS and EL2 NS, which just UNDEF */
539 return CP_ACCESS_TRAP_UNCATEGORIZED;
540}
541
59dd089c
RDC
542static uint64_t arm_mdcr_el2_eff(CPUARMState *env)
543{
544 return arm_is_el2_enabled(env) ? env->cp15.mdcr_el2 : 0;
545}
546
187f678d
PM
547/* Check for traps to "powerdown debug" registers, which are controlled
548 * by MDCR.TDOSA
549 */
550static CPAccessResult access_tdosa(CPUARMState *env, const ARMCPRegInfo *ri,
551 bool isread)
552{
553 int el = arm_current_el(env);
59dd089c
RDC
554 uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
555 bool mdcr_el2_tdosa = (mdcr_el2 & MDCR_TDOSA) || (mdcr_el2 & MDCR_TDE) ||
7c208e0f 556 (arm_hcr_el2_eff(env) & HCR_TGE);
187f678d 557
59dd089c 558 if (el < 2 && mdcr_el2_tdosa) {
187f678d
PM
559 return CP_ACCESS_TRAP_EL2;
560 }
561 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TDOSA)) {
562 return CP_ACCESS_TRAP_EL3;
563 }
564 return CP_ACCESS_OK;
565}
566
91b0a238
PM
567/* Check for traps to "debug ROM" registers, which are controlled
568 * by MDCR_EL2.TDRA for EL2 but by the more general MDCR_EL3.TDA for EL3.
569 */
570static CPAccessResult access_tdra(CPUARMState *env, const ARMCPRegInfo *ri,
571 bool isread)
572{
573 int el = arm_current_el(env);
59dd089c
RDC
574 uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
575 bool mdcr_el2_tdra = (mdcr_el2 & MDCR_TDRA) || (mdcr_el2 & MDCR_TDE) ||
7c208e0f 576 (arm_hcr_el2_eff(env) & HCR_TGE);
91b0a238 577
59dd089c 578 if (el < 2 && mdcr_el2_tdra) {
91b0a238
PM
579 return CP_ACCESS_TRAP_EL2;
580 }
581 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TDA)) {
582 return CP_ACCESS_TRAP_EL3;
583 }
584 return CP_ACCESS_OK;
585}
586
d6c8cf81
PM
587/* Check for traps to general debug registers, which are controlled
588 * by MDCR_EL2.TDA for EL2 and MDCR_EL3.TDA for EL3.
589 */
590static CPAccessResult access_tda(CPUARMState *env, const ARMCPRegInfo *ri,
591 bool isread)
592{
593 int el = arm_current_el(env);
59dd089c
RDC
594 uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
595 bool mdcr_el2_tda = (mdcr_el2 & MDCR_TDA) || (mdcr_el2 & MDCR_TDE) ||
7c208e0f 596 (arm_hcr_el2_eff(env) & HCR_TGE);
d6c8cf81 597
59dd089c 598 if (el < 2 && mdcr_el2_tda) {
d6c8cf81
PM
599 return CP_ACCESS_TRAP_EL2;
600 }
601 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TDA)) {
602 return CP_ACCESS_TRAP_EL3;
603 }
604 return CP_ACCESS_OK;
605}
606
1fce1ba9
PM
607/* Check for traps to performance monitor registers, which are controlled
608 * by MDCR_EL2.TPM for EL2 and MDCR_EL3.TPM for EL3.
609 */
610static CPAccessResult access_tpm(CPUARMState *env, const ARMCPRegInfo *ri,
611 bool isread)
612{
613 int el = arm_current_el(env);
59dd089c 614 uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
1fce1ba9 615
59dd089c 616 if (el < 2 && (mdcr_el2 & MDCR_TPM)) {
1fce1ba9
PM
617 return CP_ACCESS_TRAP_EL2;
618 }
619 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) {
620 return CP_ACCESS_TRAP_EL3;
621 }
622 return CP_ACCESS_OK;
623}
624
84929218
RH
625/* Check for traps from EL1 due to HCR_EL2.TVM and HCR_EL2.TRVM. */
626static CPAccessResult access_tvm_trvm(CPUARMState *env, const ARMCPRegInfo *ri,
627 bool isread)
628{
629 if (arm_current_el(env) == 1) {
630 uint64_t trap = isread ? HCR_TRVM : HCR_TVM;
631 if (arm_hcr_el2_eff(env) & trap) {
632 return CP_ACCESS_TRAP_EL2;
633 }
634 }
635 return CP_ACCESS_OK;
636}
637
1803d271
RH
638/* Check for traps from EL1 due to HCR_EL2.TSW. */
639static CPAccessResult access_tsw(CPUARMState *env, const ARMCPRegInfo *ri,
640 bool isread)
641{
642 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TSW)) {
643 return CP_ACCESS_TRAP_EL2;
644 }
645 return CP_ACCESS_OK;
646}
647
99602377
RH
648/* Check for traps from EL1 due to HCR_EL2.TACR. */
649static CPAccessResult access_tacr(CPUARMState *env, const ARMCPRegInfo *ri,
650 bool isread)
651{
652 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TACR)) {
653 return CP_ACCESS_TRAP_EL2;
654 }
655 return CP_ACCESS_OK;
656}
657
30881b73
RH
658/* Check for traps from EL1 due to HCR_EL2.TTLB. */
659static CPAccessResult access_ttlb(CPUARMState *env, const ARMCPRegInfo *ri,
660 bool isread)
661{
662 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TTLB)) {
663 return CP_ACCESS_TRAP_EL2;
664 }
665 return CP_ACCESS_OK;
666}
667
c4241c7d 668static void dacr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
c983fe6c 669{
2fc0cc0e 670 ARMCPU *cpu = env_archcpu(env);
00c8cb0a 671
8d5c773e 672 raw_write(env, ri, value);
d10eb08f 673 tlb_flush(CPU(cpu)); /* Flush TLB as domain not tracked in TLB */
c983fe6c
PM
674}
675
c4241c7d 676static void fcse_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
08de207b 677{
2fc0cc0e 678 ARMCPU *cpu = env_archcpu(env);
00c8cb0a 679
8d5c773e 680 if (raw_read(env, ri) != value) {
08de207b
PM
681 /* Unlike real hardware the qemu TLB uses virtual addresses,
682 * not modified virtual addresses, so this causes a TLB flush.
683 */
d10eb08f 684 tlb_flush(CPU(cpu));
8d5c773e 685 raw_write(env, ri, value);
08de207b 686 }
08de207b 687}
c4241c7d
PM
688
689static void contextidr_write(CPUARMState *env, const ARMCPRegInfo *ri,
690 uint64_t value)
08de207b 691{
2fc0cc0e 692 ARMCPU *cpu = env_archcpu(env);
00c8cb0a 693
452a0955 694 if (raw_read(env, ri) != value && !arm_feature(env, ARM_FEATURE_PMSA)
014406b5 695 && !extended_addresses_enabled(env)) {
08de207b
PM
696 /* For VMSA (when not using the LPAE long descriptor page table
697 * format) this register includes the ASID, so do a TLB flush.
698 * For PMSA it is purely a process ID and no action is needed.
699 */
d10eb08f 700 tlb_flush(CPU(cpu));
08de207b 701 }
8d5c773e 702 raw_write(env, ri, value);
08de207b
PM
703}
704
b4ab8ce9
PM
705/* IS variants of TLB operations must affect all cores */
706static void tlbiall_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
707 uint64_t value)
708{
29a0af61 709 CPUState *cs = env_cpu(env);
b4ab8ce9
PM
710
711 tlb_flush_all_cpus_synced(cs);
712}
713
714static void tlbiasid_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
715 uint64_t value)
716{
29a0af61 717 CPUState *cs = env_cpu(env);
b4ab8ce9
PM
718
719 tlb_flush_all_cpus_synced(cs);
720}
721
722static void tlbimva_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
723 uint64_t value)
724{
29a0af61 725 CPUState *cs = env_cpu(env);
b4ab8ce9
PM
726
727 tlb_flush_page_all_cpus_synced(cs, value & TARGET_PAGE_MASK);
728}
729
730static void tlbimvaa_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
731 uint64_t value)
732{
29a0af61 733 CPUState *cs = env_cpu(env);
b4ab8ce9
PM
734
735 tlb_flush_page_all_cpus_synced(cs, value & TARGET_PAGE_MASK);
736}
737
738/*
739 * Non-IS variants of TLB operations are upgraded to
373e7ffd 740 * IS versions if we are at EL1 and HCR_EL2.FB is effectively set to
b4ab8ce9
PM
741 * force broadcast of these operations.
742 */
743static bool tlb_force_broadcast(CPUARMState *env)
744{
373e7ffd 745 return arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_FB);
b4ab8ce9
PM
746}
747
c4241c7d
PM
748static void tlbiall_write(CPUARMState *env, const ARMCPRegInfo *ri,
749 uint64_t value)
d929823f
PM
750{
751 /* Invalidate all (TLBIALL) */
527db2be 752 CPUState *cs = env_cpu(env);
00c8cb0a 753
b4ab8ce9 754 if (tlb_force_broadcast(env)) {
527db2be
RH
755 tlb_flush_all_cpus_synced(cs);
756 } else {
757 tlb_flush(cs);
b4ab8ce9 758 }
d929823f
PM
759}
760
c4241c7d
PM
761static void tlbimva_write(CPUARMState *env, const ARMCPRegInfo *ri,
762 uint64_t value)
d929823f
PM
763{
764 /* Invalidate single TLB entry by MVA and ASID (TLBIMVA) */
527db2be 765 CPUState *cs = env_cpu(env);
31b030d4 766
527db2be 767 value &= TARGET_PAGE_MASK;
b4ab8ce9 768 if (tlb_force_broadcast(env)) {
527db2be
RH
769 tlb_flush_page_all_cpus_synced(cs, value);
770 } else {
771 tlb_flush_page(cs, value);
b4ab8ce9 772 }
d929823f
PM
773}
774
c4241c7d
PM
775static void tlbiasid_write(CPUARMState *env, const ARMCPRegInfo *ri,
776 uint64_t value)
d929823f
PM
777{
778 /* Invalidate by ASID (TLBIASID) */
527db2be 779 CPUState *cs = env_cpu(env);
00c8cb0a 780
b4ab8ce9 781 if (tlb_force_broadcast(env)) {
527db2be
RH
782 tlb_flush_all_cpus_synced(cs);
783 } else {
784 tlb_flush(cs);
b4ab8ce9 785 }
d929823f
PM
786}
787
c4241c7d
PM
788static void tlbimvaa_write(CPUARMState *env, const ARMCPRegInfo *ri,
789 uint64_t value)
d929823f
PM
790{
791 /* Invalidate single entry by MVA, all ASIDs (TLBIMVAA) */
527db2be 792 CPUState *cs = env_cpu(env);
31b030d4 793
527db2be 794 value &= TARGET_PAGE_MASK;
b4ab8ce9 795 if (tlb_force_broadcast(env)) {
527db2be
RH
796 tlb_flush_page_all_cpus_synced(cs, value);
797 } else {
798 tlb_flush_page(cs, value);
b4ab8ce9 799 }
fa439fc5
PM
800}
801
541ef8c2
SS
802static void tlbiall_nsnh_write(CPUARMState *env, const ARMCPRegInfo *ri,
803 uint64_t value)
804{
29a0af61 805 CPUState *cs = env_cpu(env);
541ef8c2 806
0336cbf8 807 tlb_flush_by_mmuidx(cs,
01b98b68 808 ARMMMUIdxBit_E10_1 |
452ef8cb 809 ARMMMUIdxBit_E10_1_PAN |
bf05340c 810 ARMMMUIdxBit_E10_0);
541ef8c2
SS
811}
812
813static void tlbiall_nsnh_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
814 uint64_t value)
815{
29a0af61 816 CPUState *cs = env_cpu(env);
541ef8c2 817
a67cf277 818 tlb_flush_by_mmuidx_all_cpus_synced(cs,
01b98b68 819 ARMMMUIdxBit_E10_1 |
452ef8cb 820 ARMMMUIdxBit_E10_1_PAN |
bf05340c 821 ARMMMUIdxBit_E10_0);
541ef8c2
SS
822}
823
541ef8c2
SS
824
825static void tlbiall_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
826 uint64_t value)
827{
29a0af61 828 CPUState *cs = env_cpu(env);
541ef8c2 829
e013b741 830 tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_E2);
541ef8c2
SS
831}
832
833static void tlbiall_hyp_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
834 uint64_t value)
835{
29a0af61 836 CPUState *cs = env_cpu(env);
541ef8c2 837
e013b741 838 tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_E2);
541ef8c2
SS
839}
840
841static void tlbimva_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
842 uint64_t value)
843{
29a0af61 844 CPUState *cs = env_cpu(env);
541ef8c2
SS
845 uint64_t pageaddr = value & ~MAKE_64BIT_MASK(0, 12);
846
e013b741 847 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_E2);
541ef8c2
SS
848}
849
850static void tlbimva_hyp_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
851 uint64_t value)
852{
29a0af61 853 CPUState *cs = env_cpu(env);
541ef8c2
SS
854 uint64_t pageaddr = value & ~MAKE_64BIT_MASK(0, 12);
855
a67cf277 856 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
e013b741 857 ARMMMUIdxBit_E2);
541ef8c2
SS
858}
859
e9aa6c21 860static const ARMCPRegInfo cp_reginfo[] = {
54bf36ed
FA
861 /* Define the secure and non-secure FCSE identifier CP registers
862 * separately because there is no secure bank in V8 (no _EL3). This allows
863 * the secure register to be properly reset and migrated. There is also no
864 * v8 EL1 version of the register so the non-secure instance stands alone.
865 */
9c513e78 866 { .name = "FCSEIDR",
54bf36ed
FA
867 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
868 .access = PL1_RW, .secure = ARM_CP_SECSTATE_NS,
869 .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_ns),
870 .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
9c513e78 871 { .name = "FCSEIDR_S",
54bf36ed
FA
872 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
873 .access = PL1_RW, .secure = ARM_CP_SECSTATE_S,
874 .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_s),
d4e6df63 875 .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
54bf36ed
FA
876 /* Define the secure and non-secure context identifier CP registers
877 * separately because there is no secure bank in V8 (no _EL3). This allows
878 * the secure register to be properly reset and migrated. In the
879 * non-secure case, the 32-bit register will have reset and migration
880 * disabled during registration as it is handled by the 64-bit instance.
881 */
882 { .name = "CONTEXTIDR_EL1", .state = ARM_CP_STATE_BOTH,
014406b5 883 .opc0 = 3, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
84929218
RH
884 .access = PL1_RW, .accessfn = access_tvm_trvm,
885 .secure = ARM_CP_SECSTATE_NS,
54bf36ed
FA
886 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[1]),
887 .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
9c513e78 888 { .name = "CONTEXTIDR_S", .state = ARM_CP_STATE_AA32,
54bf36ed 889 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
84929218
RH
890 .access = PL1_RW, .accessfn = access_tvm_trvm,
891 .secure = ARM_CP_SECSTATE_S,
54bf36ed 892 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_s),
d4e6df63 893 .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
9449fdf6
PM
894 REGINFO_SENTINEL
895};
896
897static const ARMCPRegInfo not_v8_cp_reginfo[] = {
898 /* NB: Some of these registers exist in v8 but with more precise
899 * definitions that don't use CP_ANY wildcards (mostly in v8_cp_reginfo[]).
900 */
901 /* MMU Domain access control / MPU write buffer control */
0c17d68c
FA
902 { .name = "DACR",
903 .cp = 15, .opc1 = CP_ANY, .crn = 3, .crm = CP_ANY, .opc2 = CP_ANY,
84929218 904 .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
0c17d68c
FA
905 .writefn = dacr_write, .raw_writefn = raw_write,
906 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
907 offsetoflow32(CPUARMState, cp15.dacr_ns) } },
a903c449
EI
908 /* ARMv7 allocates a range of implementation defined TLB LOCKDOWN regs.
909 * For v6 and v5, these mappings are overly broad.
4fdd17dd 910 */
a903c449
EI
911 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 0,
912 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
913 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 1,
914 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
915 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 4,
916 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
917 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 8,
4fdd17dd 918 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
c4804214
PM
919 /* Cache maintenance ops; some of this space may be overridden later. */
920 { .name = "CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
921 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
922 .type = ARM_CP_NOP | ARM_CP_OVERRIDE },
e9aa6c21
PM
923 REGINFO_SENTINEL
924};
925
7d57f408
PM
926static const ARMCPRegInfo not_v6_cp_reginfo[] = {
927 /* Not all pre-v6 cores implemented this WFI, so this is slightly
928 * over-broad.
929 */
930 { .name = "WFI_v5", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = 2,
931 .access = PL1_W, .type = ARM_CP_WFI },
932 REGINFO_SENTINEL
933};
934
935static const ARMCPRegInfo not_v7_cp_reginfo[] = {
936 /* Standard v6 WFI (also used in some pre-v6 cores); not in v7 (which
937 * is UNPREDICTABLE; we choose to NOP as most implementations do).
938 */
939 { .name = "WFI_v6", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
940 .access = PL1_W, .type = ARM_CP_WFI },
34f90529
PM
941 /* L1 cache lockdown. Not architectural in v6 and earlier but in practice
942 * implemented in 926, 946, 1026, 1136, 1176 and 11MPCore. StrongARM and
943 * OMAPCP will override this space.
944 */
945 { .name = "DLOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 0,
946 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_data),
947 .resetvalue = 0 },
948 { .name = "ILOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 1,
949 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_insn),
950 .resetvalue = 0 },
776d4e5c
PM
951 /* v6 doesn't have the cache ID registers but Linux reads them anyway */
952 { .name = "DUMMY", .cp = 15, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = CP_ANY,
7a0e58fa 953 .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 954 .resetvalue = 0 },
50300698
PM
955 /* We don't implement pre-v7 debug but most CPUs had at least a DBGDIDR;
956 * implementing it as RAZ means the "debug architecture version" bits
957 * will read as a reserved value, which should cause Linux to not try
958 * to use the debug hardware.
959 */
960 { .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0,
961 .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
995939a6
PM
962 /* MMU TLB control. Note that the wildcarding means we cover not just
963 * the unified TLB ops but also the dside/iside/inner-shareable variants.
964 */
965 { .name = "TLBIALL", .cp = 15, .crn = 8, .crm = CP_ANY,
966 .opc1 = CP_ANY, .opc2 = 0, .access = PL1_W, .writefn = tlbiall_write,
7a0e58fa 967 .type = ARM_CP_NO_RAW },
995939a6
PM
968 { .name = "TLBIMVA", .cp = 15, .crn = 8, .crm = CP_ANY,
969 .opc1 = CP_ANY, .opc2 = 1, .access = PL1_W, .writefn = tlbimva_write,
7a0e58fa 970 .type = ARM_CP_NO_RAW },
995939a6
PM
971 { .name = "TLBIASID", .cp = 15, .crn = 8, .crm = CP_ANY,
972 .opc1 = CP_ANY, .opc2 = 2, .access = PL1_W, .writefn = tlbiasid_write,
7a0e58fa 973 .type = ARM_CP_NO_RAW },
995939a6
PM
974 { .name = "TLBIMVAA", .cp = 15, .crn = 8, .crm = CP_ANY,
975 .opc1 = CP_ANY, .opc2 = 3, .access = PL1_W, .writefn = tlbimvaa_write,
7a0e58fa 976 .type = ARM_CP_NO_RAW },
a903c449
EI
977 { .name = "PRRR", .cp = 15, .crn = 10, .crm = 2,
978 .opc1 = 0, .opc2 = 0, .access = PL1_RW, .type = ARM_CP_NOP },
979 { .name = "NMRR", .cp = 15, .crn = 10, .crm = 2,
980 .opc1 = 0, .opc2 = 1, .access = PL1_RW, .type = ARM_CP_NOP },
7d57f408
PM
981 REGINFO_SENTINEL
982};
983
c4241c7d
PM
984static void cpacr_write(CPUARMState *env, const ARMCPRegInfo *ri,
985 uint64_t value)
2771db27 986{
f0aff255
FA
987 uint32_t mask = 0;
988
989 /* In ARMv8 most bits of CPACR_EL1 are RES0. */
990 if (!arm_feature(env, ARM_FEATURE_V8)) {
991 /* ARMv7 defines bits for unimplemented coprocessors as RAZ/WI.
992 * ASEDIS [31] and D32DIS [30] are both UNK/SBZP without VFP.
993 * TRCDIS [28] is RAZ/WI since we do not implement a trace macrocell.
994 */
7fbc6a40 995 if (cpu_isar_feature(aa32_vfp_simd, env_archcpu(env))) {
f0aff255
FA
996 /* VFP coprocessor: cp10 & cp11 [23:20] */
997 mask |= (1 << 31) | (1 << 30) | (0xf << 20);
998
999 if (!arm_feature(env, ARM_FEATURE_NEON)) {
1000 /* ASEDIS [31] bit is RAO/WI */
1001 value |= (1 << 31);
1002 }
1003
1004 /* VFPv3 and upwards with NEON implement 32 double precision
1005 * registers (D0-D31).
1006 */
a6627f5f 1007 if (!cpu_isar_feature(aa32_simd_r32, env_archcpu(env))) {
f0aff255
FA
1008 /* D32DIS [30] is RAO/WI if D16-31 are not implemented. */
1009 value |= (1 << 30);
1010 }
1011 }
1012 value &= mask;
2771db27 1013 }
fc1120a7
PM
1014
1015 /*
1016 * For A-profile AArch32 EL3 (but not M-profile secure mode), if NSACR.CP10
1017 * is 0 then CPACR.{CP11,CP10} ignore writes and read as 0b00.
1018 */
1019 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
1020 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
1021 value &= ~(0xf << 20);
1022 value |= env->cp15.cpacr_el1 & (0xf << 20);
1023 }
1024
7ebd5f2e 1025 env->cp15.cpacr_el1 = value;
2771db27
PM
1026}
1027
fc1120a7
PM
1028static uint64_t cpacr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1029{
1030 /*
1031 * For A-profile AArch32 EL3 (but not M-profile secure mode), if NSACR.CP10
1032 * is 0 then CPACR.{CP11,CP10} ignore writes and read as 0b00.
1033 */
1034 uint64_t value = env->cp15.cpacr_el1;
1035
1036 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
1037 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
1038 value &= ~(0xf << 20);
1039 }
1040 return value;
1041}
1042
1043
5deac39c
PM
1044static void cpacr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1045{
1046 /* Call cpacr_write() so that we reset with the correct RAO bits set
1047 * for our CPU features.
1048 */
1049 cpacr_write(env, ri, 0);
1050}
1051
3f208fd7
PM
1052static CPAccessResult cpacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
1053 bool isread)
c6f19164
GB
1054{
1055 if (arm_feature(env, ARM_FEATURE_V8)) {
1056 /* Check if CPACR accesses are to be trapped to EL2 */
e6ef0169
RDC
1057 if (arm_current_el(env) == 1 && arm_is_el2_enabled(env) &&
1058 (env->cp15.cptr_el[2] & CPTR_TCPAC)) {
c6f19164
GB
1059 return CP_ACCESS_TRAP_EL2;
1060 /* Check if CPACR accesses are to be trapped to EL3 */
1061 } else if (arm_current_el(env) < 3 &&
1062 (env->cp15.cptr_el[3] & CPTR_TCPAC)) {
1063 return CP_ACCESS_TRAP_EL3;
1064 }
1065 }
1066
1067 return CP_ACCESS_OK;
1068}
1069
3f208fd7
PM
1070static CPAccessResult cptr_access(CPUARMState *env, const ARMCPRegInfo *ri,
1071 bool isread)
c6f19164
GB
1072{
1073 /* Check if CPTR accesses are set to trap to EL3 */
1074 if (arm_current_el(env) == 2 && (env->cp15.cptr_el[3] & CPTR_TCPAC)) {
1075 return CP_ACCESS_TRAP_EL3;
1076 }
1077
1078 return CP_ACCESS_OK;
1079}
1080
7d57f408
PM
1081static const ARMCPRegInfo v6_cp_reginfo[] = {
1082 /* prefetch by MVA in v6, NOP in v7 */
1083 { .name = "MVA_prefetch",
1084 .cp = 15, .crn = 7, .crm = 13, .opc1 = 0, .opc2 = 1,
1085 .access = PL1_W, .type = ARM_CP_NOP },
6df99dec
SS
1086 /* We need to break the TB after ISB to execute self-modifying code
1087 * correctly and also to take any pending interrupts immediately.
1088 * So use arm_cp_write_ignore() function instead of ARM_CP_NOP flag.
1089 */
7d57f408 1090 { .name = "ISB", .cp = 15, .crn = 7, .crm = 5, .opc1 = 0, .opc2 = 4,
6df99dec 1091 .access = PL0_W, .type = ARM_CP_NO_RAW, .writefn = arm_cp_write_ignore },
091fd17c 1092 { .name = "DSB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 4,
7d57f408 1093 .access = PL0_W, .type = ARM_CP_NOP },
091fd17c 1094 { .name = "DMB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 5,
7d57f408 1095 .access = PL0_W, .type = ARM_CP_NOP },
06d76f31 1096 { .name = "IFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 2,
84929218 1097 .access = PL1_RW, .accessfn = access_tvm_trvm,
b848ce2b
FA
1098 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ifar_s),
1099 offsetof(CPUARMState, cp15.ifar_ns) },
06d76f31
PM
1100 .resetvalue = 0, },
1101 /* Watchpoint Fault Address Register : should actually only be present
1102 * for 1136, 1176, 11MPCore.
1103 */
1104 { .name = "WFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 1,
1105 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0, },
34222fb8 1106 { .name = "CPACR", .state = ARM_CP_STATE_BOTH, .opc0 = 3,
c6f19164 1107 .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 2, .accessfn = cpacr_access,
7ebd5f2e 1108 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.cpacr_el1),
fc1120a7 1109 .resetfn = cpacr_reset, .writefn = cpacr_write, .readfn = cpacr_read },
7d57f408
PM
1110 REGINFO_SENTINEL
1111};
1112
7ece99b1
AL
1113/* Definitions for the PMU registers */
1114#define PMCRN_MASK 0xf800
1115#define PMCRN_SHIFT 11
f4efb4b2 1116#define PMCRLC 0x40
a1ed04dd
PM
1117#define PMCRDP 0x20
1118#define PMCRX 0x10
7ece99b1
AL
1119#define PMCRD 0x8
1120#define PMCRC 0x4
5ecdd3e4 1121#define PMCRP 0x2
7ece99b1 1122#define PMCRE 0x1
62d96ff4
PM
1123/*
1124 * Mask of PMCR bits writeable by guest (not including WO bits like C, P,
1125 * which can be written as 1 to trigger behaviour but which stay RAZ).
1126 */
1127#define PMCR_WRITEABLE_MASK (PMCRLC | PMCRDP | PMCRX | PMCRD | PMCRE)
7ece99b1 1128
033614c4
AL
1129#define PMXEVTYPER_P 0x80000000
1130#define PMXEVTYPER_U 0x40000000
1131#define PMXEVTYPER_NSK 0x20000000
1132#define PMXEVTYPER_NSU 0x10000000
1133#define PMXEVTYPER_NSH 0x08000000
1134#define PMXEVTYPER_M 0x04000000
1135#define PMXEVTYPER_MT 0x02000000
1136#define PMXEVTYPER_EVTCOUNT 0x0000ffff
1137#define PMXEVTYPER_MASK (PMXEVTYPER_P | PMXEVTYPER_U | PMXEVTYPER_NSK | \
1138 PMXEVTYPER_NSU | PMXEVTYPER_NSH | \
1139 PMXEVTYPER_M | PMXEVTYPER_MT | \
1140 PMXEVTYPER_EVTCOUNT)
1141
4b8afa1f
AL
1142#define PMCCFILTR 0xf8000000
1143#define PMCCFILTR_M PMXEVTYPER_M
1144#define PMCCFILTR_EL0 (PMCCFILTR | PMCCFILTR_M)
1145
7ece99b1
AL
1146static inline uint32_t pmu_num_counters(CPUARMState *env)
1147{
1148 return (env->cp15.c9_pmcr & PMCRN_MASK) >> PMCRN_SHIFT;
1149}
1150
1151/* Bits allowed to be set/cleared for PMCNTEN* and PMINTEN* */
1152static inline uint64_t pmu_counter_mask(CPUARMState *env)
1153{
1154 return (1 << 31) | ((1 << pmu_num_counters(env)) - 1);
1155}
1156
57a4a11b
AL
1157typedef struct pm_event {
1158 uint16_t number; /* PMEVTYPER.evtCount is 16 bits wide */
1159 /* If the event is supported on this CPU (used to generate PMCEID[01]) */
1160 bool (*supported)(CPUARMState *);
1161 /*
1162 * Retrieve the current count of the underlying event. The programmed
1163 * counters hold a difference from the return value from this function
1164 */
1165 uint64_t (*get_count)(CPUARMState *);
4e7beb0c
AL
1166 /*
1167 * Return how many nanoseconds it will take (at a minimum) for count events
1168 * to occur. A negative value indicates the counter will never overflow, or
1169 * that the counter has otherwise arranged for the overflow bit to be set
1170 * and the PMU interrupt to be raised on overflow.
1171 */
1172 int64_t (*ns_per_count)(uint64_t);
57a4a11b
AL
1173} pm_event;
1174
b2e23725
AL
1175static bool event_always_supported(CPUARMState *env)
1176{
1177 return true;
1178}
1179
0d4bfd7d
AL
1180static uint64_t swinc_get_count(CPUARMState *env)
1181{
1182 /*
1183 * SW_INCR events are written directly to the pmevcntr's by writes to
1184 * PMSWINC, so there is no underlying count maintained by the PMU itself
1185 */
1186 return 0;
1187}
1188
4e7beb0c
AL
1189static int64_t swinc_ns_per(uint64_t ignored)
1190{
1191 return -1;
1192}
1193
b2e23725
AL
1194/*
1195 * Return the underlying cycle count for the PMU cycle counters. If we're in
1196 * usermode, simply return 0.
1197 */
1198static uint64_t cycles_get_count(CPUARMState *env)
1199{
1200#ifndef CONFIG_USER_ONLY
1201 return muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
1202 ARM_CPU_FREQ, NANOSECONDS_PER_SECOND);
1203#else
1204 return cpu_get_host_ticks();
1205#endif
1206}
1207
1208#ifndef CONFIG_USER_ONLY
4e7beb0c
AL
1209static int64_t cycles_ns_per(uint64_t cycles)
1210{
1211 return (ARM_CPU_FREQ / NANOSECONDS_PER_SECOND) * cycles;
1212}
1213
b2e23725
AL
1214static bool instructions_supported(CPUARMState *env)
1215{
740b1759 1216 return icount_enabled() == 1; /* Precise instruction counting */
b2e23725
AL
1217}
1218
1219static uint64_t instructions_get_count(CPUARMState *env)
1220{
8191d368 1221 return (uint64_t)icount_get_raw();
b2e23725 1222}
4e7beb0c
AL
1223
1224static int64_t instructions_ns_per(uint64_t icount)
1225{
8191d368 1226 return icount_to_ns((int64_t)icount);
4e7beb0c 1227}
b2e23725
AL
1228#endif
1229
0727f63b
PM
1230static bool pmu_8_1_events_supported(CPUARMState *env)
1231{
1232 /* For events which are supported in any v8.1 PMU */
1233 return cpu_isar_feature(any_pmu_8_1, env_archcpu(env));
1234}
1235
15dd1ebd
PM
1236static bool pmu_8_4_events_supported(CPUARMState *env)
1237{
1238 /* For events which are supported in any v8.1 PMU */
1239 return cpu_isar_feature(any_pmu_8_4, env_archcpu(env));
1240}
1241
0727f63b
PM
1242static uint64_t zero_event_get_count(CPUARMState *env)
1243{
1244 /* For events which on QEMU never fire, so their count is always zero */
1245 return 0;
1246}
1247
1248static int64_t zero_event_ns_per(uint64_t cycles)
1249{
1250 /* An event which never fires can never overflow */
1251 return -1;
1252}
1253
57a4a11b 1254static const pm_event pm_events[] = {
0d4bfd7d
AL
1255 { .number = 0x000, /* SW_INCR */
1256 .supported = event_always_supported,
1257 .get_count = swinc_get_count,
4e7beb0c 1258 .ns_per_count = swinc_ns_per,
0d4bfd7d 1259 },
b2e23725
AL
1260#ifndef CONFIG_USER_ONLY
1261 { .number = 0x008, /* INST_RETIRED, Instruction architecturally executed */
1262 .supported = instructions_supported,
1263 .get_count = instructions_get_count,
4e7beb0c 1264 .ns_per_count = instructions_ns_per,
b2e23725
AL
1265 },
1266 { .number = 0x011, /* CPU_CYCLES, Cycle */
1267 .supported = event_always_supported,
1268 .get_count = cycles_get_count,
4e7beb0c 1269 .ns_per_count = cycles_ns_per,
0727f63b 1270 },
b2e23725 1271#endif
0727f63b
PM
1272 { .number = 0x023, /* STALL_FRONTEND */
1273 .supported = pmu_8_1_events_supported,
1274 .get_count = zero_event_get_count,
1275 .ns_per_count = zero_event_ns_per,
1276 },
1277 { .number = 0x024, /* STALL_BACKEND */
1278 .supported = pmu_8_1_events_supported,
1279 .get_count = zero_event_get_count,
1280 .ns_per_count = zero_event_ns_per,
1281 },
15dd1ebd
PM
1282 { .number = 0x03c, /* STALL */
1283 .supported = pmu_8_4_events_supported,
1284 .get_count = zero_event_get_count,
1285 .ns_per_count = zero_event_ns_per,
1286 },
57a4a11b
AL
1287};
1288
1289/*
1290 * Note: Before increasing MAX_EVENT_ID beyond 0x3f into the 0x40xx range of
1291 * events (i.e. the statistical profiling extension), this implementation
1292 * should first be updated to something sparse instead of the current
1293 * supported_event_map[] array.
1294 */
15dd1ebd 1295#define MAX_EVENT_ID 0x3c
57a4a11b
AL
1296#define UNSUPPORTED_EVENT UINT16_MAX
1297static uint16_t supported_event_map[MAX_EVENT_ID + 1];
1298
1299/*
bf8d0969
AL
1300 * Called upon CPU initialization to initialize PMCEID[01]_EL0 and build a map
1301 * of ARM event numbers to indices in our pm_events array.
57a4a11b
AL
1302 *
1303 * Note: Events in the 0x40XX range are not currently supported.
1304 */
bf8d0969 1305void pmu_init(ARMCPU *cpu)
57a4a11b 1306{
57a4a11b
AL
1307 unsigned int i;
1308
bf8d0969
AL
1309 /*
1310 * Empty supported_event_map and cpu->pmceid[01] before adding supported
1311 * events to them
1312 */
57a4a11b
AL
1313 for (i = 0; i < ARRAY_SIZE(supported_event_map); i++) {
1314 supported_event_map[i] = UNSUPPORTED_EVENT;
1315 }
bf8d0969
AL
1316 cpu->pmceid0 = 0;
1317 cpu->pmceid1 = 0;
57a4a11b
AL
1318
1319 for (i = 0; i < ARRAY_SIZE(pm_events); i++) {
1320 const pm_event *cnt = &pm_events[i];
1321 assert(cnt->number <= MAX_EVENT_ID);
1322 /* We do not currently support events in the 0x40xx range */
1323 assert(cnt->number <= 0x3f);
1324
bf8d0969 1325 if (cnt->supported(&cpu->env)) {
57a4a11b 1326 supported_event_map[cnt->number] = i;
67da43d6 1327 uint64_t event_mask = 1ULL << (cnt->number & 0x1f);
bf8d0969
AL
1328 if (cnt->number & 0x20) {
1329 cpu->pmceid1 |= event_mask;
1330 } else {
1331 cpu->pmceid0 |= event_mask;
1332 }
57a4a11b
AL
1333 }
1334 }
57a4a11b
AL
1335}
1336
5ecdd3e4
AL
1337/*
1338 * Check at runtime whether a PMU event is supported for the current machine
1339 */
1340static bool event_supported(uint16_t number)
1341{
1342 if (number > MAX_EVENT_ID) {
1343 return false;
1344 }
1345 return supported_event_map[number] != UNSUPPORTED_EVENT;
1346}
1347
3f208fd7
PM
1348static CPAccessResult pmreg_access(CPUARMState *env, const ARMCPRegInfo *ri,
1349 bool isread)
200ac0ef 1350{
3b163b01 1351 /* Performance monitor registers user accessibility is controlled
1fce1ba9
PM
1352 * by PMUSERENR. MDCR_EL2.TPM and MDCR_EL3.TPM allow configurable
1353 * trapping to EL2 or EL3 for other accesses.
200ac0ef 1354 */
1fce1ba9 1355 int el = arm_current_el(env);
59dd089c 1356 uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
1fce1ba9 1357
6ecd0b6b 1358 if (el == 0 && !(env->cp15.c9_pmuserenr & 1)) {
fcd25206 1359 return CP_ACCESS_TRAP;
200ac0ef 1360 }
59dd089c 1361 if (el < 2 && (mdcr_el2 & MDCR_TPM)) {
1fce1ba9
PM
1362 return CP_ACCESS_TRAP_EL2;
1363 }
1364 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) {
1365 return CP_ACCESS_TRAP_EL3;
1366 }
1367
fcd25206 1368 return CP_ACCESS_OK;
200ac0ef
PM
1369}
1370
6ecd0b6b
AB
1371static CPAccessResult pmreg_access_xevcntr(CPUARMState *env,
1372 const ARMCPRegInfo *ri,
1373 bool isread)
1374{
1375 /* ER: event counter read trap control */
1376 if (arm_feature(env, ARM_FEATURE_V8)
1377 && arm_current_el(env) == 0
1378 && (env->cp15.c9_pmuserenr & (1 << 3)) != 0
1379 && isread) {
1380 return CP_ACCESS_OK;
1381 }
1382
1383 return pmreg_access(env, ri, isread);
1384}
1385
1386static CPAccessResult pmreg_access_swinc(CPUARMState *env,
1387 const ARMCPRegInfo *ri,
1388 bool isread)
1389{
1390 /* SW: software increment write trap control */
1391 if (arm_feature(env, ARM_FEATURE_V8)
1392 && arm_current_el(env) == 0
1393 && (env->cp15.c9_pmuserenr & (1 << 1)) != 0
1394 && !isread) {
1395 return CP_ACCESS_OK;
1396 }
1397
1398 return pmreg_access(env, ri, isread);
1399}
1400
6ecd0b6b
AB
1401static CPAccessResult pmreg_access_selr(CPUARMState *env,
1402 const ARMCPRegInfo *ri,
1403 bool isread)
1404{
1405 /* ER: event counter read trap control */
1406 if (arm_feature(env, ARM_FEATURE_V8)
1407 && arm_current_el(env) == 0
1408 && (env->cp15.c9_pmuserenr & (1 << 3)) != 0) {
1409 return CP_ACCESS_OK;
1410 }
1411
1412 return pmreg_access(env, ri, isread);
1413}
1414
1415static CPAccessResult pmreg_access_ccntr(CPUARMState *env,
1416 const ARMCPRegInfo *ri,
1417 bool isread)
1418{
1419 /* CR: cycle counter read trap control */
1420 if (arm_feature(env, ARM_FEATURE_V8)
1421 && arm_current_el(env) == 0
1422 && (env->cp15.c9_pmuserenr & (1 << 2)) != 0
1423 && isread) {
1424 return CP_ACCESS_OK;
1425 }
1426
1427 return pmreg_access(env, ri, isread);
1428}
1429
033614c4
AL
1430/* Returns true if the counter (pass 31 for PMCCNTR) should count events using
1431 * the current EL, security state, and register configuration.
1432 */
1433static bool pmu_counter_enabled(CPUARMState *env, uint8_t counter)
87124fde 1434{
033614c4
AL
1435 uint64_t filter;
1436 bool e, p, u, nsk, nsu, nsh, m;
1437 bool enabled, prohibited, filtered;
1438 bool secure = arm_is_secure(env);
1439 int el = arm_current_el(env);
59dd089c
RDC
1440 uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
1441 uint8_t hpmn = mdcr_el2 & MDCR_HPMN;
87124fde 1442
cbbb3041
AJ
1443 if (!arm_feature(env, ARM_FEATURE_PMU)) {
1444 return false;
1445 }
1446
033614c4
AL
1447 if (!arm_feature(env, ARM_FEATURE_EL2) ||
1448 (counter < hpmn || counter == 31)) {
1449 e = env->cp15.c9_pmcr & PMCRE;
1450 } else {
59dd089c 1451 e = mdcr_el2 & MDCR_HPME;
87124fde 1452 }
033614c4 1453 enabled = e && (env->cp15.c9_pmcnten & (1 << counter));
87124fde 1454
033614c4
AL
1455 if (!secure) {
1456 if (el == 2 && (counter < hpmn || counter == 31)) {
59dd089c 1457 prohibited = mdcr_el2 & MDCR_HPMD;
033614c4
AL
1458 } else {
1459 prohibited = false;
1460 }
1461 } else {
1462 prohibited = arm_feature(env, ARM_FEATURE_EL3) &&
db1f3afb 1463 !(env->cp15.mdcr_el3 & MDCR_SPME);
033614c4
AL
1464 }
1465
1466 if (prohibited && counter == 31) {
1467 prohibited = env->cp15.c9_pmcr & PMCRDP;
1468 }
1469
5ecdd3e4
AL
1470 if (counter == 31) {
1471 filter = env->cp15.pmccfiltr_el0;
1472 } else {
1473 filter = env->cp15.c14_pmevtyper[counter];
1474 }
033614c4
AL
1475
1476 p = filter & PMXEVTYPER_P;
1477 u = filter & PMXEVTYPER_U;
1478 nsk = arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_NSK);
1479 nsu = arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_NSU);
1480 nsh = arm_feature(env, ARM_FEATURE_EL2) && (filter & PMXEVTYPER_NSH);
1481 m = arm_el_is_aa64(env, 1) &&
1482 arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_M);
1483
1484 if (el == 0) {
1485 filtered = secure ? u : u != nsu;
1486 } else if (el == 1) {
1487 filtered = secure ? p : p != nsk;
1488 } else if (el == 2) {
1489 filtered = !nsh;
1490 } else { /* EL3 */
1491 filtered = m != p;
1492 }
1493
5ecdd3e4
AL
1494 if (counter != 31) {
1495 /*
1496 * If not checking PMCCNTR, ensure the counter is setup to an event we
1497 * support
1498 */
1499 uint16_t event = filter & PMXEVTYPER_EVTCOUNT;
1500 if (!event_supported(event)) {
1501 return false;
1502 }
1503 }
1504
033614c4 1505 return enabled && !prohibited && !filtered;
87124fde 1506}
033614c4 1507
f4efb4b2
AL
1508static void pmu_update_irq(CPUARMState *env)
1509{
2fc0cc0e 1510 ARMCPU *cpu = env_archcpu(env);
f4efb4b2
AL
1511 qemu_set_irq(cpu->pmu_interrupt, (env->cp15.c9_pmcr & PMCRE) &&
1512 (env->cp15.c9_pminten & env->cp15.c9_pmovsr));
1513}
1514
5d05b9d4
AL
1515/*
1516 * Ensure c15_ccnt is the guest-visible count so that operations such as
1517 * enabling/disabling the counter or filtering, modifying the count itself,
1518 * etc. can be done logically. This is essentially a no-op if the counter is
1519 * not enabled at the time of the call.
1520 */
f2b2f53f 1521static void pmccntr_op_start(CPUARMState *env)
ec7b4ce4 1522{
b2e23725 1523 uint64_t cycles = cycles_get_count(env);
ec7b4ce4 1524
033614c4 1525 if (pmu_counter_enabled(env, 31)) {
5d05b9d4
AL
1526 uint64_t eff_cycles = cycles;
1527 if (env->cp15.c9_pmcr & PMCRD) {
1528 /* Increment once every 64 processor clock cycles */
1529 eff_cycles /= 64;
1530 }
1531
f4efb4b2
AL
1532 uint64_t new_pmccntr = eff_cycles - env->cp15.c15_ccnt_delta;
1533
1534 uint64_t overflow_mask = env->cp15.c9_pmcr & PMCRLC ? \
1535 1ull << 63 : 1ull << 31;
1536 if (env->cp15.c15_ccnt & ~new_pmccntr & overflow_mask) {
1537 env->cp15.c9_pmovsr |= (1 << 31);
1538 pmu_update_irq(env);
1539 }
1540
1541 env->cp15.c15_ccnt = new_pmccntr;
ec7b4ce4 1542 }
5d05b9d4
AL
1543 env->cp15.c15_ccnt_delta = cycles;
1544}
ec7b4ce4 1545
5d05b9d4
AL
1546/*
1547 * If PMCCNTR is enabled, recalculate the delta between the clock and the
1548 * guest-visible count. A call to pmccntr_op_finish should follow every call to
1549 * pmccntr_op_start.
1550 */
f2b2f53f 1551static void pmccntr_op_finish(CPUARMState *env)
5d05b9d4 1552{
033614c4 1553 if (pmu_counter_enabled(env, 31)) {
4e7beb0c
AL
1554#ifndef CONFIG_USER_ONLY
1555 /* Calculate when the counter will next overflow */
1556 uint64_t remaining_cycles = -env->cp15.c15_ccnt;
1557 if (!(env->cp15.c9_pmcr & PMCRLC)) {
1558 remaining_cycles = (uint32_t)remaining_cycles;
1559 }
1560 int64_t overflow_in = cycles_ns_per(remaining_cycles);
1561
1562 if (overflow_in > 0) {
1563 int64_t overflow_at = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
1564 overflow_in;
2fc0cc0e 1565 ARMCPU *cpu = env_archcpu(env);
4e7beb0c
AL
1566 timer_mod_anticipate_ns(cpu->pmu_timer, overflow_at);
1567 }
1568#endif
5d05b9d4 1569
4e7beb0c 1570 uint64_t prev_cycles = env->cp15.c15_ccnt_delta;
5d05b9d4
AL
1571 if (env->cp15.c9_pmcr & PMCRD) {
1572 /* Increment once every 64 processor clock cycles */
1573 prev_cycles /= 64;
1574 }
5d05b9d4 1575 env->cp15.c15_ccnt_delta = prev_cycles - env->cp15.c15_ccnt;
ec7b4ce4
AF
1576 }
1577}
1578
5ecdd3e4
AL
1579static void pmevcntr_op_start(CPUARMState *env, uint8_t counter)
1580{
1581
1582 uint16_t event = env->cp15.c14_pmevtyper[counter] & PMXEVTYPER_EVTCOUNT;
1583 uint64_t count = 0;
1584 if (event_supported(event)) {
1585 uint16_t event_idx = supported_event_map[event];
1586 count = pm_events[event_idx].get_count(env);
1587 }
1588
1589 if (pmu_counter_enabled(env, counter)) {
f4efb4b2
AL
1590 uint32_t new_pmevcntr = count - env->cp15.c14_pmevcntr_delta[counter];
1591
1592 if (env->cp15.c14_pmevcntr[counter] & ~new_pmevcntr & INT32_MIN) {
1593 env->cp15.c9_pmovsr |= (1 << counter);
1594 pmu_update_irq(env);
1595 }
1596 env->cp15.c14_pmevcntr[counter] = new_pmevcntr;
5ecdd3e4
AL
1597 }
1598 env->cp15.c14_pmevcntr_delta[counter] = count;
1599}
1600
1601static void pmevcntr_op_finish(CPUARMState *env, uint8_t counter)
1602{
1603 if (pmu_counter_enabled(env, counter)) {
4e7beb0c
AL
1604#ifndef CONFIG_USER_ONLY
1605 uint16_t event = env->cp15.c14_pmevtyper[counter] & PMXEVTYPER_EVTCOUNT;
1606 uint16_t event_idx = supported_event_map[event];
1607 uint64_t delta = UINT32_MAX -
1608 (uint32_t)env->cp15.c14_pmevcntr[counter] + 1;
1609 int64_t overflow_in = pm_events[event_idx].ns_per_count(delta);
1610
1611 if (overflow_in > 0) {
1612 int64_t overflow_at = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
1613 overflow_in;
2fc0cc0e 1614 ARMCPU *cpu = env_archcpu(env);
4e7beb0c
AL
1615 timer_mod_anticipate_ns(cpu->pmu_timer, overflow_at);
1616 }
1617#endif
1618
5ecdd3e4
AL
1619 env->cp15.c14_pmevcntr_delta[counter] -=
1620 env->cp15.c14_pmevcntr[counter];
1621 }
1622}
1623
5d05b9d4
AL
1624void pmu_op_start(CPUARMState *env)
1625{
5ecdd3e4 1626 unsigned int i;
5d05b9d4 1627 pmccntr_op_start(env);
5ecdd3e4
AL
1628 for (i = 0; i < pmu_num_counters(env); i++) {
1629 pmevcntr_op_start(env, i);
1630 }
5d05b9d4
AL
1631}
1632
1633void pmu_op_finish(CPUARMState *env)
1634{
5ecdd3e4 1635 unsigned int i;
5d05b9d4 1636 pmccntr_op_finish(env);
5ecdd3e4
AL
1637 for (i = 0; i < pmu_num_counters(env); i++) {
1638 pmevcntr_op_finish(env, i);
1639 }
5d05b9d4
AL
1640}
1641
033614c4
AL
1642void pmu_pre_el_change(ARMCPU *cpu, void *ignored)
1643{
1644 pmu_op_start(&cpu->env);
1645}
1646
1647void pmu_post_el_change(ARMCPU *cpu, void *ignored)
1648{
1649 pmu_op_finish(&cpu->env);
1650}
1651
4e7beb0c
AL
1652void arm_pmu_timer_cb(void *opaque)
1653{
1654 ARMCPU *cpu = opaque;
1655
1656 /*
1657 * Update all the counter values based on the current underlying counts,
1658 * triggering interrupts to be raised, if necessary. pmu_op_finish() also
1659 * has the effect of setting the cpu->pmu_timer to the next earliest time a
1660 * counter may expire.
1661 */
1662 pmu_op_start(&cpu->env);
1663 pmu_op_finish(&cpu->env);
1664}
1665
c4241c7d
PM
1666static void pmcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1667 uint64_t value)
200ac0ef 1668{
5d05b9d4 1669 pmu_op_start(env);
7c2cb42b
AF
1670
1671 if (value & PMCRC) {
1672 /* The counter has been reset */
1673 env->cp15.c15_ccnt = 0;
1674 }
1675
5ecdd3e4
AL
1676 if (value & PMCRP) {
1677 unsigned int i;
1678 for (i = 0; i < pmu_num_counters(env); i++) {
1679 env->cp15.c14_pmevcntr[i] = 0;
1680 }
1681 }
1682
62d96ff4
PM
1683 env->cp15.c9_pmcr &= ~PMCR_WRITEABLE_MASK;
1684 env->cp15.c9_pmcr |= (value & PMCR_WRITEABLE_MASK);
7c2cb42b 1685
5d05b9d4 1686 pmu_op_finish(env);
7c2cb42b
AF
1687}
1688
0d4bfd7d
AL
1689static void pmswinc_write(CPUARMState *env, const ARMCPRegInfo *ri,
1690 uint64_t value)
1691{
1692 unsigned int i;
1693 for (i = 0; i < pmu_num_counters(env); i++) {
1694 /* Increment a counter's count iff: */
1695 if ((value & (1 << i)) && /* counter's bit is set */
1696 /* counter is enabled and not filtered */
1697 pmu_counter_enabled(env, i) &&
1698 /* counter is SW_INCR */
1699 (env->cp15.c14_pmevtyper[i] & PMXEVTYPER_EVTCOUNT) == 0x0) {
1700 pmevcntr_op_start(env, i);
f4efb4b2
AL
1701
1702 /*
1703 * Detect if this write causes an overflow since we can't predict
1704 * PMSWINC overflows like we can for other events
1705 */
1706 uint32_t new_pmswinc = env->cp15.c14_pmevcntr[i] + 1;
1707
1708 if (env->cp15.c14_pmevcntr[i] & ~new_pmswinc & INT32_MIN) {
1709 env->cp15.c9_pmovsr |= (1 << i);
1710 pmu_update_irq(env);
1711 }
1712
1713 env->cp15.c14_pmevcntr[i] = new_pmswinc;
1714
0d4bfd7d
AL
1715 pmevcntr_op_finish(env, i);
1716 }
1717 }
1718}
1719
7c2cb42b
AF
1720static uint64_t pmccntr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1721{
5d05b9d4
AL
1722 uint64_t ret;
1723 pmccntr_op_start(env);
1724 ret = env->cp15.c15_ccnt;
1725 pmccntr_op_finish(env);
1726 return ret;
7c2cb42b
AF
1727}
1728
6b040780
WH
1729static void pmselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1730 uint64_t value)
1731{
1732 /* The value of PMSELR.SEL affects the behavior of PMXEVTYPER and
1733 * PMXEVCNTR. We allow [0..31] to be written to PMSELR here; in the
1734 * meanwhile, we check PMSELR.SEL when PMXEVTYPER and PMXEVCNTR are
1735 * accessed.
1736 */
1737 env->cp15.c9_pmselr = value & 0x1f;
1738}
1739
7c2cb42b
AF
1740static void pmccntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1741 uint64_t value)
1742{
5d05b9d4
AL
1743 pmccntr_op_start(env);
1744 env->cp15.c15_ccnt = value;
1745 pmccntr_op_finish(env);
200ac0ef 1746}
421c7ebd
PC
1747
1748static void pmccntr_write32(CPUARMState *env, const ARMCPRegInfo *ri,
1749 uint64_t value)
1750{
1751 uint64_t cur_val = pmccntr_read(env, NULL);
1752
1753 pmccntr_write(env, ri, deposit64(cur_val, 0, 32, value));
1754}
1755
0614601c
AF
1756static void pmccfiltr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1757 uint64_t value)
1758{
5d05b9d4 1759 pmccntr_op_start(env);
4b8afa1f
AL
1760 env->cp15.pmccfiltr_el0 = value & PMCCFILTR_EL0;
1761 pmccntr_op_finish(env);
1762}
1763
1764static void pmccfiltr_write_a32(CPUARMState *env, const ARMCPRegInfo *ri,
1765 uint64_t value)
1766{
1767 pmccntr_op_start(env);
1768 /* M is not accessible from AArch32 */
1769 env->cp15.pmccfiltr_el0 = (env->cp15.pmccfiltr_el0 & PMCCFILTR_M) |
1770 (value & PMCCFILTR);
5d05b9d4 1771 pmccntr_op_finish(env);
0614601c
AF
1772}
1773
4b8afa1f
AL
1774static uint64_t pmccfiltr_read_a32(CPUARMState *env, const ARMCPRegInfo *ri)
1775{
1776 /* M is not visible in AArch32 */
1777 return env->cp15.pmccfiltr_el0 & PMCCFILTR;
1778}
1779
c4241c7d 1780static void pmcntenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
200ac0ef
PM
1781 uint64_t value)
1782{
7ece99b1 1783 value &= pmu_counter_mask(env);
200ac0ef 1784 env->cp15.c9_pmcnten |= value;
200ac0ef
PM
1785}
1786
c4241c7d
PM
1787static void pmcntenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1788 uint64_t value)
200ac0ef 1789{
7ece99b1 1790 value &= pmu_counter_mask(env);
200ac0ef 1791 env->cp15.c9_pmcnten &= ~value;
200ac0ef
PM
1792}
1793
c4241c7d
PM
1794static void pmovsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1795 uint64_t value)
200ac0ef 1796{
599b71e2 1797 value &= pmu_counter_mask(env);
200ac0ef 1798 env->cp15.c9_pmovsr &= ~value;
f4efb4b2 1799 pmu_update_irq(env);
200ac0ef
PM
1800}
1801
327dd510
AL
1802static void pmovsset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1803 uint64_t value)
1804{
1805 value &= pmu_counter_mask(env);
1806 env->cp15.c9_pmovsr |= value;
f4efb4b2 1807 pmu_update_irq(env);
327dd510
AL
1808}
1809
5ecdd3e4
AL
1810static void pmevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
1811 uint64_t value, const uint8_t counter)
200ac0ef 1812{
5ecdd3e4
AL
1813 if (counter == 31) {
1814 pmccfiltr_write(env, ri, value);
1815 } else if (counter < pmu_num_counters(env)) {
1816 pmevcntr_op_start(env, counter);
1817
1818 /*
1819 * If this counter's event type is changing, store the current
1820 * underlying count for the new type in c14_pmevcntr_delta[counter] so
1821 * pmevcntr_op_finish has the correct baseline when it converts back to
1822 * a delta.
1823 */
1824 uint16_t old_event = env->cp15.c14_pmevtyper[counter] &
1825 PMXEVTYPER_EVTCOUNT;
1826 uint16_t new_event = value & PMXEVTYPER_EVTCOUNT;
1827 if (old_event != new_event) {
1828 uint64_t count = 0;
1829 if (event_supported(new_event)) {
1830 uint16_t event_idx = supported_event_map[new_event];
1831 count = pm_events[event_idx].get_count(env);
1832 }
1833 env->cp15.c14_pmevcntr_delta[counter] = count;
1834 }
1835
1836 env->cp15.c14_pmevtyper[counter] = value & PMXEVTYPER_MASK;
1837 pmevcntr_op_finish(env, counter);
1838 }
fdb86656
WH
1839 /* Attempts to access PMXEVTYPER are CONSTRAINED UNPREDICTABLE when
1840 * PMSELR value is equal to or greater than the number of implemented
1841 * counters, but not equal to 0x1f. We opt to behave as a RAZ/WI.
1842 */
5ecdd3e4
AL
1843}
1844
1845static uint64_t pmevtyper_read(CPUARMState *env, const ARMCPRegInfo *ri,
1846 const uint8_t counter)
1847{
1848 if (counter == 31) {
1849 return env->cp15.pmccfiltr_el0;
1850 } else if (counter < pmu_num_counters(env)) {
1851 return env->cp15.c14_pmevtyper[counter];
1852 } else {
1853 /*
1854 * We opt to behave as a RAZ/WI when attempts to access PMXEVTYPER
1855 * are CONSTRAINED UNPREDICTABLE. See comments in pmevtyper_write().
1856 */
1857 return 0;
1858 }
1859}
1860
1861static void pmevtyper_writefn(CPUARMState *env, const ARMCPRegInfo *ri,
1862 uint64_t value)
1863{
1864 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1865 pmevtyper_write(env, ri, value, counter);
1866}
1867
1868static void pmevtyper_rawwrite(CPUARMState *env, const ARMCPRegInfo *ri,
1869 uint64_t value)
1870{
1871 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1872 env->cp15.c14_pmevtyper[counter] = value;
1873
1874 /*
1875 * pmevtyper_rawwrite is called between a pair of pmu_op_start and
1876 * pmu_op_finish calls when loading saved state for a migration. Because
1877 * we're potentially updating the type of event here, the value written to
1878 * c14_pmevcntr_delta by the preceeding pmu_op_start call may be for a
1879 * different counter type. Therefore, we need to set this value to the
1880 * current count for the counter type we're writing so that pmu_op_finish
1881 * has the correct count for its calculation.
1882 */
1883 uint16_t event = value & PMXEVTYPER_EVTCOUNT;
1884 if (event_supported(event)) {
1885 uint16_t event_idx = supported_event_map[event];
1886 env->cp15.c14_pmevcntr_delta[counter] =
1887 pm_events[event_idx].get_count(env);
fdb86656
WH
1888 }
1889}
1890
5ecdd3e4
AL
1891static uint64_t pmevtyper_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
1892{
1893 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1894 return pmevtyper_read(env, ri, counter);
1895}
1896
1897static void pmxevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
1898 uint64_t value)
1899{
1900 pmevtyper_write(env, ri, value, env->cp15.c9_pmselr & 31);
1901}
1902
fdb86656
WH
1903static uint64_t pmxevtyper_read(CPUARMState *env, const ARMCPRegInfo *ri)
1904{
5ecdd3e4
AL
1905 return pmevtyper_read(env, ri, env->cp15.c9_pmselr & 31);
1906}
1907
1908static void pmevcntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1909 uint64_t value, uint8_t counter)
1910{
1911 if (counter < pmu_num_counters(env)) {
1912 pmevcntr_op_start(env, counter);
1913 env->cp15.c14_pmevcntr[counter] = value;
1914 pmevcntr_op_finish(env, counter);
1915 }
1916 /*
1917 * We opt to behave as a RAZ/WI when attempts to access PM[X]EVCNTR
1918 * are CONSTRAINED UNPREDICTABLE.
fdb86656 1919 */
5ecdd3e4
AL
1920}
1921
1922static uint64_t pmevcntr_read(CPUARMState *env, const ARMCPRegInfo *ri,
1923 uint8_t counter)
1924{
1925 if (counter < pmu_num_counters(env)) {
1926 uint64_t ret;
1927 pmevcntr_op_start(env, counter);
1928 ret = env->cp15.c14_pmevcntr[counter];
1929 pmevcntr_op_finish(env, counter);
1930 return ret;
fdb86656 1931 } else {
5ecdd3e4
AL
1932 /* We opt to behave as a RAZ/WI when attempts to access PM[X]EVCNTR
1933 * are CONSTRAINED UNPREDICTABLE. */
fdb86656
WH
1934 return 0;
1935 }
200ac0ef
PM
1936}
1937
5ecdd3e4
AL
1938static void pmevcntr_writefn(CPUARMState *env, const ARMCPRegInfo *ri,
1939 uint64_t value)
1940{
1941 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1942 pmevcntr_write(env, ri, value, counter);
1943}
1944
1945static uint64_t pmevcntr_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
1946{
1947 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1948 return pmevcntr_read(env, ri, counter);
1949}
1950
1951static void pmevcntr_rawwrite(CPUARMState *env, const ARMCPRegInfo *ri,
1952 uint64_t value)
1953{
1954 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1955 assert(counter < pmu_num_counters(env));
1956 env->cp15.c14_pmevcntr[counter] = value;
1957 pmevcntr_write(env, ri, value, counter);
1958}
1959
1960static uint64_t pmevcntr_rawread(CPUARMState *env, const ARMCPRegInfo *ri)
1961{
1962 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1963 assert(counter < pmu_num_counters(env));
1964 return env->cp15.c14_pmevcntr[counter];
1965}
1966
1967static void pmxevcntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1968 uint64_t value)
1969{
1970 pmevcntr_write(env, ri, value, env->cp15.c9_pmselr & 31);
1971}
1972
1973static uint64_t pmxevcntr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1974{
1975 return pmevcntr_read(env, ri, env->cp15.c9_pmselr & 31);
1976}
1977
c4241c7d 1978static void pmuserenr_write(CPUARMState *env, const ARMCPRegInfo *ri,
200ac0ef
PM
1979 uint64_t value)
1980{
6ecd0b6b
AB
1981 if (arm_feature(env, ARM_FEATURE_V8)) {
1982 env->cp15.c9_pmuserenr = value & 0xf;
1983 } else {
1984 env->cp15.c9_pmuserenr = value & 1;
1985 }
200ac0ef
PM
1986}
1987
c4241c7d
PM
1988static void pmintenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1989 uint64_t value)
200ac0ef
PM
1990{
1991 /* We have no event counters so only the C bit can be changed */
7ece99b1 1992 value &= pmu_counter_mask(env);
200ac0ef 1993 env->cp15.c9_pminten |= value;
f4efb4b2 1994 pmu_update_irq(env);
200ac0ef
PM
1995}
1996
c4241c7d
PM
1997static void pmintenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1998 uint64_t value)
200ac0ef 1999{
7ece99b1 2000 value &= pmu_counter_mask(env);
200ac0ef 2001 env->cp15.c9_pminten &= ~value;
f4efb4b2 2002 pmu_update_irq(env);
200ac0ef
PM
2003}
2004
c4241c7d
PM
2005static void vbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
2006 uint64_t value)
8641136c 2007{
a505d7fe
PM
2008 /* Note that even though the AArch64 view of this register has bits
2009 * [10:0] all RES0 we can only mask the bottom 5, to comply with the
2010 * architectural requirements for bits which are RES0 only in some
2011 * contexts. (ARMv8 would permit us to do no masking at all, but ARMv7
2012 * requires the bottom five bits to be RAZ/WI because they're UNK/SBZP.)
2013 */
855ea66d 2014 raw_write(env, ri, value & ~0x1FULL);
8641136c
NR
2015}
2016
64e0e2de
EI
2017static void scr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
2018{
ea22747c
RH
2019 /* Begin with base v8.0 state. */
2020 uint32_t valid_mask = 0x3fff;
2fc0cc0e 2021 ARMCPU *cpu = env_archcpu(env);
ea22747c 2022
252e8c69 2023 if (ri->state == ARM_CP_STATE_AA64) {
ea22747c
RH
2024 value |= SCR_FW | SCR_AW; /* these two bits are RES1. */
2025 valid_mask &= ~SCR_NET;
252e8c69
RH
2026
2027 if (cpu_isar_feature(aa64_lor, cpu)) {
2028 valid_mask |= SCR_TLOR;
2029 }
2030 if (cpu_isar_feature(aa64_pauth, cpu)) {
2031 valid_mask |= SCR_API | SCR_APK;
2032 }
8ddb300b
RH
2033 if (cpu_isar_feature(aa64_mte, cpu)) {
2034 valid_mask |= SCR_ATA;
2035 }
ea22747c
RH
2036 } else {
2037 valid_mask &= ~(SCR_RW | SCR_ST);
2038 }
64e0e2de
EI
2039
2040 if (!arm_feature(env, ARM_FEATURE_EL2)) {
2041 valid_mask &= ~SCR_HCE;
2042
2043 /* On ARMv7, SMD (or SCD as it is called in v7) is only
2044 * supported if EL2 exists. The bit is UNK/SBZP when
2045 * EL2 is unavailable. In QEMU ARMv7, we force it to always zero
2046 * when EL2 is unavailable.
4eb27640 2047 * On ARMv8, this bit is always available.
64e0e2de 2048 */
4eb27640
GB
2049 if (arm_feature(env, ARM_FEATURE_V7) &&
2050 !arm_feature(env, ARM_FEATURE_V8)) {
64e0e2de
EI
2051 valid_mask &= ~SCR_SMD;
2052 }
2053 }
2054
2055 /* Clear all-context RES0 bits. */
2056 value &= valid_mask;
2057 raw_write(env, ri, value);
2058}
2059
630fcd4d
MZ
2060static CPAccessResult access_aa64_tid2(CPUARMState *env,
2061 const ARMCPRegInfo *ri,
2062 bool isread)
2063{
2064 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID2)) {
2065 return CP_ACCESS_TRAP_EL2;
2066 }
2067
2068 return CP_ACCESS_OK;
2069}
2070
c4241c7d 2071static uint64_t ccsidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
776d4e5c 2072{
2fc0cc0e 2073 ARMCPU *cpu = env_archcpu(env);
b85a1fd6
FA
2074
2075 /* Acquire the CSSELR index from the bank corresponding to the CCSIDR
2076 * bank
2077 */
2078 uint32_t index = A32_BANKED_REG_GET(env, csselr,
2079 ri->secure & ARM_CP_SECSTATE_S);
2080
2081 return cpu->ccsidr[index];
776d4e5c
PM
2082}
2083
c4241c7d
PM
2084static void csselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2085 uint64_t value)
776d4e5c 2086{
8d5c773e 2087 raw_write(env, ri, value & 0xf);
776d4e5c
PM
2088}
2089
1090b9c6
PM
2090static uint64_t isr_read(CPUARMState *env, const ARMCPRegInfo *ri)
2091{
29a0af61 2092 CPUState *cs = env_cpu(env);
cc974d5c
RDC
2093 bool el1 = arm_current_el(env) == 1;
2094 uint64_t hcr_el2 = el1 ? arm_hcr_el2_eff(env) : 0;
1090b9c6
PM
2095 uint64_t ret = 0;
2096
cc974d5c 2097 if (hcr_el2 & HCR_IMO) {
636540e9
PM
2098 if (cs->interrupt_request & CPU_INTERRUPT_VIRQ) {
2099 ret |= CPSR_I;
2100 }
2101 } else {
2102 if (cs->interrupt_request & CPU_INTERRUPT_HARD) {
2103 ret |= CPSR_I;
2104 }
1090b9c6 2105 }
636540e9 2106
cc974d5c 2107 if (hcr_el2 & HCR_FMO) {
636540e9
PM
2108 if (cs->interrupt_request & CPU_INTERRUPT_VFIQ) {
2109 ret |= CPSR_F;
2110 }
2111 } else {
2112 if (cs->interrupt_request & CPU_INTERRUPT_FIQ) {
2113 ret |= CPSR_F;
2114 }
1090b9c6 2115 }
636540e9 2116
1090b9c6
PM
2117 /* External aborts are not possible in QEMU so A bit is always clear */
2118 return ret;
2119}
2120
93fbc983
MZ
2121static CPAccessResult access_aa64_tid1(CPUARMState *env, const ARMCPRegInfo *ri,
2122 bool isread)
2123{
2124 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID1)) {
2125 return CP_ACCESS_TRAP_EL2;
2126 }
2127
2128 return CP_ACCESS_OK;
2129}
2130
2131static CPAccessResult access_aa32_tid1(CPUARMState *env, const ARMCPRegInfo *ri,
2132 bool isread)
2133{
2134 if (arm_feature(env, ARM_FEATURE_V8)) {
2135 return access_aa64_tid1(env, ri, isread);
2136 }
2137
2138 return CP_ACCESS_OK;
2139}
2140
e9aa6c21 2141static const ARMCPRegInfo v7_cp_reginfo[] = {
7d57f408
PM
2142 /* the old v6 WFI, UNPREDICTABLE in v7 but we choose to NOP */
2143 { .name = "NOP", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
2144 .access = PL1_W, .type = ARM_CP_NOP },
200ac0ef
PM
2145 /* Performance monitors are implementation defined in v7,
2146 * but with an ARM recommended set of registers, which we
ac689a2e 2147 * follow.
200ac0ef
PM
2148 *
2149 * Performance registers fall into three categories:
2150 * (a) always UNDEF in PL0, RW in PL1 (PMINTENSET, PMINTENCLR)
2151 * (b) RO in PL0 (ie UNDEF on write), RW in PL1 (PMUSERENR)
2152 * (c) UNDEF in PL0 if PMUSERENR.EN==0, otherwise accessible (all others)
2153 * For the cases controlled by PMUSERENR we must set .access to PL0_RW
2154 * or PL0_RO as appropriate and then check PMUSERENR in the helper fn.
2155 */
2156 { .name = "PMCNTENSET", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 1,
7a0e58fa 2157 .access = PL0_RW, .type = ARM_CP_ALIAS,
8521466b 2158 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
fcd25206
PM
2159 .writefn = pmcntenset_write,
2160 .accessfn = pmreg_access,
2161 .raw_writefn = raw_write },
8521466b
AF
2162 { .name = "PMCNTENSET_EL0", .state = ARM_CP_STATE_AA64,
2163 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 1,
2164 .access = PL0_RW, .accessfn = pmreg_access,
2165 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten), .resetvalue = 0,
2166 .writefn = pmcntenset_write, .raw_writefn = raw_write },
200ac0ef 2167 { .name = "PMCNTENCLR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 2,
8521466b
AF
2168 .access = PL0_RW,
2169 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
fcd25206
PM
2170 .accessfn = pmreg_access,
2171 .writefn = pmcntenclr_write,
7a0e58fa 2172 .type = ARM_CP_ALIAS },
8521466b
AF
2173 { .name = "PMCNTENCLR_EL0", .state = ARM_CP_STATE_AA64,
2174 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 2,
2175 .access = PL0_RW, .accessfn = pmreg_access,
7a0e58fa 2176 .type = ARM_CP_ALIAS,
8521466b
AF
2177 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten),
2178 .writefn = pmcntenclr_write },
200ac0ef 2179 { .name = "PMOVSR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 3,
f4efb4b2 2180 .access = PL0_RW, .type = ARM_CP_IO,
e4e91a21 2181 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr),
fcd25206
PM
2182 .accessfn = pmreg_access,
2183 .writefn = pmovsr_write,
2184 .raw_writefn = raw_write },
978364f1
AF
2185 { .name = "PMOVSCLR_EL0", .state = ARM_CP_STATE_AA64,
2186 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 3,
2187 .access = PL0_RW, .accessfn = pmreg_access,
f4efb4b2 2188 .type = ARM_CP_ALIAS | ARM_CP_IO,
978364f1
AF
2189 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
2190 .writefn = pmovsr_write,
2191 .raw_writefn = raw_write },
200ac0ef 2192 { .name = "PMSWINC", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 4,
f4efb4b2
AL
2193 .access = PL0_W, .accessfn = pmreg_access_swinc,
2194 .type = ARM_CP_NO_RAW | ARM_CP_IO,
0d4bfd7d
AL
2195 .writefn = pmswinc_write },
2196 { .name = "PMSWINC_EL0", .state = ARM_CP_STATE_AA64,
2197 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 4,
f4efb4b2
AL
2198 .access = PL0_W, .accessfn = pmreg_access_swinc,
2199 .type = ARM_CP_NO_RAW | ARM_CP_IO,
0d4bfd7d 2200 .writefn = pmswinc_write },
6b040780
WH
2201 { .name = "PMSELR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 5,
2202 .access = PL0_RW, .type = ARM_CP_ALIAS,
2203 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmselr),
6ecd0b6b 2204 .accessfn = pmreg_access_selr, .writefn = pmselr_write,
6b040780
WH
2205 .raw_writefn = raw_write},
2206 { .name = "PMSELR_EL0", .state = ARM_CP_STATE_AA64,
2207 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 5,
6ecd0b6b 2208 .access = PL0_RW, .accessfn = pmreg_access_selr,
6b040780
WH
2209 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmselr),
2210 .writefn = pmselr_write, .raw_writefn = raw_write, },
200ac0ef 2211 { .name = "PMCCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 0,
169c8938 2212 .access = PL0_RW, .resetvalue = 0, .type = ARM_CP_ALIAS | ARM_CP_IO,
421c7ebd 2213 .readfn = pmccntr_read, .writefn = pmccntr_write32,
6ecd0b6b 2214 .accessfn = pmreg_access_ccntr },
8521466b
AF
2215 { .name = "PMCCNTR_EL0", .state = ARM_CP_STATE_AA64,
2216 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 0,
6ecd0b6b 2217 .access = PL0_RW, .accessfn = pmreg_access_ccntr,
8521466b 2218 .type = ARM_CP_IO,
980ebe87
AL
2219 .fieldoffset = offsetof(CPUARMState, cp15.c15_ccnt),
2220 .readfn = pmccntr_read, .writefn = pmccntr_write,
2221 .raw_readfn = raw_read, .raw_writefn = raw_write, },
4b8afa1f
AL
2222 { .name = "PMCCFILTR", .cp = 15, .opc1 = 0, .crn = 14, .crm = 15, .opc2 = 7,
2223 .writefn = pmccfiltr_write_a32, .readfn = pmccfiltr_read_a32,
2224 .access = PL0_RW, .accessfn = pmreg_access,
2225 .type = ARM_CP_ALIAS | ARM_CP_IO,
2226 .resetvalue = 0, },
8521466b
AF
2227 { .name = "PMCCFILTR_EL0", .state = ARM_CP_STATE_AA64,
2228 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 15, .opc2 = 7,
980ebe87 2229 .writefn = pmccfiltr_write, .raw_writefn = raw_write,
8521466b
AF
2230 .access = PL0_RW, .accessfn = pmreg_access,
2231 .type = ARM_CP_IO,
2232 .fieldoffset = offsetof(CPUARMState, cp15.pmccfiltr_el0),
2233 .resetvalue = 0, },
200ac0ef 2234 { .name = "PMXEVTYPER", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 1,
5ecdd3e4
AL
2235 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2236 .accessfn = pmreg_access,
fdb86656
WH
2237 .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
2238 { .name = "PMXEVTYPER_EL0", .state = ARM_CP_STATE_AA64,
2239 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 1,
5ecdd3e4
AL
2240 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2241 .accessfn = pmreg_access,
fdb86656 2242 .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
200ac0ef 2243 { .name = "PMXEVCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 2,
5ecdd3e4
AL
2244 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2245 .accessfn = pmreg_access_xevcntr,
2246 .writefn = pmxevcntr_write, .readfn = pmxevcntr_read },
2247 { .name = "PMXEVCNTR_EL0", .state = ARM_CP_STATE_AA64,
2248 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 2,
2249 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2250 .accessfn = pmreg_access_xevcntr,
2251 .writefn = pmxevcntr_write, .readfn = pmxevcntr_read },
200ac0ef 2252 { .name = "PMUSERENR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 0,
1fce1ba9 2253 .access = PL0_R | PL1_RW, .accessfn = access_tpm,
e4e91a21 2254 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmuserenr),
200ac0ef 2255 .resetvalue = 0,
d4e6df63 2256 .writefn = pmuserenr_write, .raw_writefn = raw_write },
8a83ffc2
AF
2257 { .name = "PMUSERENR_EL0", .state = ARM_CP_STATE_AA64,
2258 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 0,
1fce1ba9 2259 .access = PL0_R | PL1_RW, .accessfn = access_tpm, .type = ARM_CP_ALIAS,
8a83ffc2
AF
2260 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmuserenr),
2261 .resetvalue = 0,
2262 .writefn = pmuserenr_write, .raw_writefn = raw_write },
200ac0ef 2263 { .name = "PMINTENSET", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 1,
1fce1ba9 2264 .access = PL1_RW, .accessfn = access_tpm,
b7d793ad 2265 .type = ARM_CP_ALIAS | ARM_CP_IO,
e6ec5457 2266 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pminten),
200ac0ef 2267 .resetvalue = 0,
d4e6df63 2268 .writefn = pmintenset_write, .raw_writefn = raw_write },
e6ec5457
WH
2269 { .name = "PMINTENSET_EL1", .state = ARM_CP_STATE_AA64,
2270 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 1,
2271 .access = PL1_RW, .accessfn = access_tpm,
2272 .type = ARM_CP_IO,
2273 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2274 .writefn = pmintenset_write, .raw_writefn = raw_write,
2275 .resetvalue = 0x0 },
200ac0ef 2276 { .name = "PMINTENCLR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 2,
fc5f6856 2277 .access = PL1_RW, .accessfn = access_tpm,
887c0f15 2278 .type = ARM_CP_ALIAS | ARM_CP_IO | ARM_CP_NO_RAW,
200ac0ef 2279 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
b061a82b 2280 .writefn = pmintenclr_write, },
978364f1
AF
2281 { .name = "PMINTENCLR_EL1", .state = ARM_CP_STATE_AA64,
2282 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 2,
fc5f6856 2283 .access = PL1_RW, .accessfn = access_tpm,
887c0f15 2284 .type = ARM_CP_ALIAS | ARM_CP_IO | ARM_CP_NO_RAW,
978364f1
AF
2285 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2286 .writefn = pmintenclr_write },
7da845b0
PM
2287 { .name = "CCSIDR", .state = ARM_CP_STATE_BOTH,
2288 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 0,
630fcd4d
MZ
2289 .access = PL1_R,
2290 .accessfn = access_aa64_tid2,
2291 .readfn = ccsidr_read, .type = ARM_CP_NO_RAW },
7da845b0
PM
2292 { .name = "CSSELR", .state = ARM_CP_STATE_BOTH,
2293 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 2, .opc2 = 0,
630fcd4d
MZ
2294 .access = PL1_RW,
2295 .accessfn = access_aa64_tid2,
2296 .writefn = csselr_write, .resetvalue = 0,
b85a1fd6
FA
2297 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.csselr_s),
2298 offsetof(CPUARMState, cp15.csselr_ns) } },
776d4e5c
PM
2299 /* Auxiliary ID register: this actually has an IMPDEF value but for now
2300 * just RAZ for all cores:
2301 */
0ff644a7
PM
2302 { .name = "AIDR", .state = ARM_CP_STATE_BOTH,
2303 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 7,
93fbc983
MZ
2304 .access = PL1_R, .type = ARM_CP_CONST,
2305 .accessfn = access_aa64_tid1,
2306 .resetvalue = 0 },
f32cdad5
PM
2307 /* Auxiliary fault status registers: these also are IMPDEF, and we
2308 * choose to RAZ/WI for all cores.
2309 */
2310 { .name = "AFSR0_EL1", .state = ARM_CP_STATE_BOTH,
2311 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 0,
84929218
RH
2312 .access = PL1_RW, .accessfn = access_tvm_trvm,
2313 .type = ARM_CP_CONST, .resetvalue = 0 },
f32cdad5
PM
2314 { .name = "AFSR1_EL1", .state = ARM_CP_STATE_BOTH,
2315 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 1,
84929218
RH
2316 .access = PL1_RW, .accessfn = access_tvm_trvm,
2317 .type = ARM_CP_CONST, .resetvalue = 0 },
b0fe2427
PM
2318 /* MAIR can just read-as-written because we don't implement caches
2319 * and so don't need to care about memory attributes.
2320 */
2321 { .name = "MAIR_EL1", .state = ARM_CP_STATE_AA64,
2322 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
84929218
RH
2323 .access = PL1_RW, .accessfn = access_tvm_trvm,
2324 .fieldoffset = offsetof(CPUARMState, cp15.mair_el[1]),
b0fe2427 2325 .resetvalue = 0 },
4cfb8ad8
PM
2326 { .name = "MAIR_EL3", .state = ARM_CP_STATE_AA64,
2327 .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 2, .opc2 = 0,
2328 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[3]),
2329 .resetvalue = 0 },
b0fe2427
PM
2330 /* For non-long-descriptor page tables these are PRRR and NMRR;
2331 * regardless they still act as reads-as-written for QEMU.
b0fe2427 2332 */
1281f8e3 2333 /* MAIR0/1 are defined separately from their 64-bit counterpart which
be693c87
GB
2334 * allows them to assign the correct fieldoffset based on the endianness
2335 * handled in the field definitions.
2336 */
a903c449 2337 { .name = "MAIR0", .state = ARM_CP_STATE_AA32,
84929218
RH
2338 .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
2339 .access = PL1_RW, .accessfn = access_tvm_trvm,
be693c87
GB
2340 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair0_s),
2341 offsetof(CPUARMState, cp15.mair0_ns) },
b0fe2427 2342 .resetfn = arm_cp_reset_ignore },
a903c449 2343 { .name = "MAIR1", .state = ARM_CP_STATE_AA32,
84929218
RH
2344 .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 1,
2345 .access = PL1_RW, .accessfn = access_tvm_trvm,
be693c87
GB
2346 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair1_s),
2347 offsetof(CPUARMState, cp15.mair1_ns) },
b0fe2427 2348 .resetfn = arm_cp_reset_ignore },
1090b9c6
PM
2349 { .name = "ISR_EL1", .state = ARM_CP_STATE_BOTH,
2350 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 0,
7a0e58fa 2351 .type = ARM_CP_NO_RAW, .access = PL1_R, .readfn = isr_read },
995939a6
PM
2352 /* 32 bit ITLB invalidates */
2353 { .name = "ITLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 0,
30881b73
RH
2354 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2355 .writefn = tlbiall_write },
995939a6 2356 { .name = "ITLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 1,
30881b73
RH
2357 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2358 .writefn = tlbimva_write },
995939a6 2359 { .name = "ITLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 2,
30881b73
RH
2360 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2361 .writefn = tlbiasid_write },
995939a6
PM
2362 /* 32 bit DTLB invalidates */
2363 { .name = "DTLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 0,
30881b73
RH
2364 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2365 .writefn = tlbiall_write },
995939a6 2366 { .name = "DTLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 1,
30881b73
RH
2367 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2368 .writefn = tlbimva_write },
995939a6 2369 { .name = "DTLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 2,
30881b73
RH
2370 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2371 .writefn = tlbiasid_write },
995939a6
PM
2372 /* 32 bit TLB invalidates */
2373 { .name = "TLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
30881b73
RH
2374 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2375 .writefn = tlbiall_write },
995939a6 2376 { .name = "TLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
30881b73
RH
2377 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2378 .writefn = tlbimva_write },
995939a6 2379 { .name = "TLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
30881b73
RH
2380 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2381 .writefn = tlbiasid_write },
995939a6 2382 { .name = "TLBIMVAA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
30881b73
RH
2383 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2384 .writefn = tlbimvaa_write },
995939a6
PM
2385 REGINFO_SENTINEL
2386};
2387
2388static const ARMCPRegInfo v7mp_cp_reginfo[] = {
2389 /* 32 bit TLB invalidates, Inner Shareable */
2390 { .name = "TLBIALLIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
30881b73
RH
2391 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2392 .writefn = tlbiall_is_write },
995939a6 2393 { .name = "TLBIMVAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
30881b73
RH
2394 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2395 .writefn = tlbimva_is_write },
995939a6 2396 { .name = "TLBIASIDIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
30881b73 2397 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
fa439fc5 2398 .writefn = tlbiasid_is_write },
995939a6 2399 { .name = "TLBIMVAAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
30881b73 2400 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
fa439fc5 2401 .writefn = tlbimvaa_is_write },
e9aa6c21
PM
2402 REGINFO_SENTINEL
2403};
2404
327dd510
AL
2405static const ARMCPRegInfo pmovsset_cp_reginfo[] = {
2406 /* PMOVSSET is not implemented in v7 before v7ve */
2407 { .name = "PMOVSSET", .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 3,
2408 .access = PL0_RW, .accessfn = pmreg_access,
f4efb4b2 2409 .type = ARM_CP_ALIAS | ARM_CP_IO,
327dd510
AL
2410 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr),
2411 .writefn = pmovsset_write,
2412 .raw_writefn = raw_write },
2413 { .name = "PMOVSSET_EL0", .state = ARM_CP_STATE_AA64,
2414 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 3,
2415 .access = PL0_RW, .accessfn = pmreg_access,
f4efb4b2 2416 .type = ARM_CP_ALIAS | ARM_CP_IO,
327dd510
AL
2417 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
2418 .writefn = pmovsset_write,
2419 .raw_writefn = raw_write },
2420 REGINFO_SENTINEL
2421};
2422
c4241c7d
PM
2423static void teecr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2424 uint64_t value)
c326b979
PM
2425{
2426 value &= 1;
2427 env->teecr = value;
c326b979
PM
2428}
2429
3f208fd7
PM
2430static CPAccessResult teehbr_access(CPUARMState *env, const ARMCPRegInfo *ri,
2431 bool isread)
c326b979 2432{
dcbff19b 2433 if (arm_current_el(env) == 0 && (env->teecr & 1)) {
92611c00 2434 return CP_ACCESS_TRAP;
c326b979 2435 }
92611c00 2436 return CP_ACCESS_OK;
c326b979
PM
2437}
2438
2439static const ARMCPRegInfo t2ee_cp_reginfo[] = {
2440 { .name = "TEECR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 6, .opc2 = 0,
2441 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, teecr),
2442 .resetvalue = 0,
2443 .writefn = teecr_write },
2444 { .name = "TEEHBR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 6, .opc2 = 0,
2445 .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, teehbr),
92611c00 2446 .accessfn = teehbr_access, .resetvalue = 0 },
c326b979
PM
2447 REGINFO_SENTINEL
2448};
2449
4d31c596 2450static const ARMCPRegInfo v6k_cp_reginfo[] = {
e4fe830b
PM
2451 { .name = "TPIDR_EL0", .state = ARM_CP_STATE_AA64,
2452 .opc0 = 3, .opc1 = 3, .opc2 = 2, .crn = 13, .crm = 0,
2453 .access = PL0_RW,
54bf36ed 2454 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[0]), .resetvalue = 0 },
4d31c596
PM
2455 { .name = "TPIDRURW", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 2,
2456 .access = PL0_RW,
54bf36ed
FA
2457 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrurw_s),
2458 offsetoflow32(CPUARMState, cp15.tpidrurw_ns) },
e4fe830b
PM
2459 .resetfn = arm_cp_reset_ignore },
2460 { .name = "TPIDRRO_EL0", .state = ARM_CP_STATE_AA64,
2461 .opc0 = 3, .opc1 = 3, .opc2 = 3, .crn = 13, .crm = 0,
2462 .access = PL0_R|PL1_W,
54bf36ed
FA
2463 .fieldoffset = offsetof(CPUARMState, cp15.tpidrro_el[0]),
2464 .resetvalue = 0},
4d31c596
PM
2465 { .name = "TPIDRURO", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 3,
2466 .access = PL0_R|PL1_W,
54bf36ed
FA
2467 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidruro_s),
2468 offsetoflow32(CPUARMState, cp15.tpidruro_ns) },
e4fe830b 2469 .resetfn = arm_cp_reset_ignore },
54bf36ed 2470 { .name = "TPIDR_EL1", .state = ARM_CP_STATE_AA64,
e4fe830b 2471 .opc0 = 3, .opc1 = 0, .opc2 = 4, .crn = 13, .crm = 0,
4d31c596 2472 .access = PL1_RW,
54bf36ed
FA
2473 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[1]), .resetvalue = 0 },
2474 { .name = "TPIDRPRW", .opc1 = 0, .cp = 15, .crn = 13, .crm = 0, .opc2 = 4,
2475 .access = PL1_RW,
2476 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrprw_s),
2477 offsetoflow32(CPUARMState, cp15.tpidrprw_ns) },
2478 .resetvalue = 0 },
4d31c596
PM
2479 REGINFO_SENTINEL
2480};
2481
55d284af
PM
2482#ifndef CONFIG_USER_ONLY
2483
3f208fd7
PM
2484static CPAccessResult gt_cntfrq_access(CPUARMState *env, const ARMCPRegInfo *ri,
2485 bool isread)
00108f2d 2486{
75502672
PM
2487 /* CNTFRQ: not visible from PL0 if both PL0PCTEN and PL0VCTEN are zero.
2488 * Writable only at the highest implemented exception level.
2489 */
2490 int el = arm_current_el(env);
5bc84371
RH
2491 uint64_t hcr;
2492 uint32_t cntkctl;
75502672
PM
2493
2494 switch (el) {
2495 case 0:
5bc84371
RH
2496 hcr = arm_hcr_el2_eff(env);
2497 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2498 cntkctl = env->cp15.cnthctl_el2;
2499 } else {
2500 cntkctl = env->cp15.c14_cntkctl;
2501 }
2502 if (!extract32(cntkctl, 0, 2)) {
75502672
PM
2503 return CP_ACCESS_TRAP;
2504 }
2505 break;
2506 case 1:
2507 if (!isread && ri->state == ARM_CP_STATE_AA32 &&
2508 arm_is_secure_below_el3(env)) {
2509 /* Accesses from 32-bit Secure EL1 UNDEF (*not* trap to EL3!) */
2510 return CP_ACCESS_TRAP_UNCATEGORIZED;
2511 }
2512 break;
2513 case 2:
2514 case 3:
2515 break;
00108f2d 2516 }
75502672
PM
2517
2518 if (!isread && el < arm_highest_el(env)) {
2519 return CP_ACCESS_TRAP_UNCATEGORIZED;
2520 }
2521
00108f2d
PM
2522 return CP_ACCESS_OK;
2523}
2524
3f208fd7
PM
2525static CPAccessResult gt_counter_access(CPUARMState *env, int timeridx,
2526 bool isread)
00108f2d 2527{
0b6440af 2528 unsigned int cur_el = arm_current_el(env);
e6ef0169 2529 bool has_el2 = arm_is_el2_enabled(env);
5bc84371 2530 uint64_t hcr = arm_hcr_el2_eff(env);
0b6440af 2531
5bc84371
RH
2532 switch (cur_el) {
2533 case 0:
2534 /* If HCR_EL2.<E2H,TGE> == '11': check CNTHCTL_EL2.EL0[PV]CTEN. */
2535 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2536 return (extract32(env->cp15.cnthctl_el2, timeridx, 1)
2537 ? CP_ACCESS_OK : CP_ACCESS_TRAP_EL2);
2538 }
0b6440af 2539
5bc84371
RH
2540 /* CNT[PV]CT: not visible from PL0 if EL0[PV]CTEN is zero */
2541 if (!extract32(env->cp15.c14_cntkctl, timeridx, 1)) {
2542 return CP_ACCESS_TRAP;
2543 }
2544
2545 /* If HCR_EL2.<E2H,TGE> == '10': check CNTHCTL_EL2.EL1PCTEN. */
2546 if (hcr & HCR_E2H) {
2547 if (timeridx == GTIMER_PHYS &&
2548 !extract32(env->cp15.cnthctl_el2, 10, 1)) {
2549 return CP_ACCESS_TRAP_EL2;
2550 }
2551 } else {
2552 /* If HCR_EL2.<E2H> == 0: check CNTHCTL_EL2.EL1PCEN. */
e6ef0169 2553 if (has_el2 && timeridx == GTIMER_PHYS &&
5bc84371
RH
2554 !extract32(env->cp15.cnthctl_el2, 1, 1)) {
2555 return CP_ACCESS_TRAP_EL2;
2556 }
2557 }
2558 break;
2559
2560 case 1:
2561 /* Check CNTHCTL_EL2.EL1PCTEN, which changes location based on E2H. */
e6ef0169 2562 if (has_el2 && timeridx == GTIMER_PHYS &&
5bc84371
RH
2563 (hcr & HCR_E2H
2564 ? !extract32(env->cp15.cnthctl_el2, 10, 1)
2565 : !extract32(env->cp15.cnthctl_el2, 0, 1))) {
2566 return CP_ACCESS_TRAP_EL2;
2567 }
2568 break;
0b6440af 2569 }
00108f2d
PM
2570 return CP_ACCESS_OK;
2571}
2572
3f208fd7
PM
2573static CPAccessResult gt_timer_access(CPUARMState *env, int timeridx,
2574 bool isread)
00108f2d 2575{
0b6440af 2576 unsigned int cur_el = arm_current_el(env);
e6ef0169 2577 bool has_el2 = arm_is_el2_enabled(env);
5bc84371 2578 uint64_t hcr = arm_hcr_el2_eff(env);
0b6440af 2579
5bc84371
RH
2580 switch (cur_el) {
2581 case 0:
2582 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2583 /* If HCR_EL2.<E2H,TGE> == '11': check CNTHCTL_EL2.EL0[PV]TEN. */
2584 return (extract32(env->cp15.cnthctl_el2, 9 - timeridx, 1)
2585 ? CP_ACCESS_OK : CP_ACCESS_TRAP_EL2);
2586 }
0b6440af 2587
5bc84371
RH
2588 /*
2589 * CNT[PV]_CVAL, CNT[PV]_CTL, CNT[PV]_TVAL: not visible from
2590 * EL0 if EL0[PV]TEN is zero.
2591 */
2592 if (!extract32(env->cp15.c14_cntkctl, 9 - timeridx, 1)) {
2593 return CP_ACCESS_TRAP;
2594 }
2595 /* fall through */
2596
2597 case 1:
e6ef0169 2598 if (has_el2 && timeridx == GTIMER_PHYS) {
5bc84371
RH
2599 if (hcr & HCR_E2H) {
2600 /* If HCR_EL2.<E2H,TGE> == '10': check CNTHCTL_EL2.EL1PTEN. */
2601 if (!extract32(env->cp15.cnthctl_el2, 11, 1)) {
2602 return CP_ACCESS_TRAP_EL2;
2603 }
2604 } else {
2605 /* If HCR_EL2.<E2H> == 0: check CNTHCTL_EL2.EL1PCEN. */
2606 if (!extract32(env->cp15.cnthctl_el2, 1, 1)) {
2607 return CP_ACCESS_TRAP_EL2;
2608 }
2609 }
2610 }
2611 break;
0b6440af 2612 }
00108f2d
PM
2613 return CP_ACCESS_OK;
2614}
2615
2616static CPAccessResult gt_pct_access(CPUARMState *env,
3f208fd7
PM
2617 const ARMCPRegInfo *ri,
2618 bool isread)
00108f2d 2619{
3f208fd7 2620 return gt_counter_access(env, GTIMER_PHYS, isread);
00108f2d
PM
2621}
2622
2623static CPAccessResult gt_vct_access(CPUARMState *env,
3f208fd7
PM
2624 const ARMCPRegInfo *ri,
2625 bool isread)
00108f2d 2626{
3f208fd7 2627 return gt_counter_access(env, GTIMER_VIRT, isread);
00108f2d
PM
2628}
2629
3f208fd7
PM
2630static CPAccessResult gt_ptimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
2631 bool isread)
00108f2d 2632{
3f208fd7 2633 return gt_timer_access(env, GTIMER_PHYS, isread);
00108f2d
PM
2634}
2635
3f208fd7
PM
2636static CPAccessResult gt_vtimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
2637 bool isread)
00108f2d 2638{
3f208fd7 2639 return gt_timer_access(env, GTIMER_VIRT, isread);
00108f2d
PM
2640}
2641
b4d3978c 2642static CPAccessResult gt_stimer_access(CPUARMState *env,
3f208fd7
PM
2643 const ARMCPRegInfo *ri,
2644 bool isread)
b4d3978c
PM
2645{
2646 /* The AArch64 register view of the secure physical timer is
2647 * always accessible from EL3, and configurably accessible from
2648 * Secure EL1.
2649 */
2650 switch (arm_current_el(env)) {
2651 case 1:
2652 if (!arm_is_secure(env)) {
2653 return CP_ACCESS_TRAP;
2654 }
2655 if (!(env->cp15.scr_el3 & SCR_ST)) {
2656 return CP_ACCESS_TRAP_EL3;
2657 }
2658 return CP_ACCESS_OK;
2659 case 0:
2660 case 2:
2661 return CP_ACCESS_TRAP;
2662 case 3:
2663 return CP_ACCESS_OK;
2664 default:
2665 g_assert_not_reached();
2666 }
2667}
2668
55d284af
PM
2669static uint64_t gt_get_countervalue(CPUARMState *env)
2670{
7def8754
AJ
2671 ARMCPU *cpu = env_archcpu(env);
2672
2673 return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) / gt_cntfrq_period_ns(cpu);
55d284af
PM
2674}
2675
2676static void gt_recalc_timer(ARMCPU *cpu, int timeridx)
2677{
2678 ARMGenericTimer *gt = &cpu->env.cp15.c14_timer[timeridx];
2679
2680 if (gt->ctl & 1) {
2681 /* Timer enabled: calculate and set current ISTATUS, irq, and
2682 * reset timer to when ISTATUS next has to change
2683 */
edac4d8a
EI
2684 uint64_t offset = timeridx == GTIMER_VIRT ?
2685 cpu->env.cp15.cntvoff_el2 : 0;
55d284af
PM
2686 uint64_t count = gt_get_countervalue(&cpu->env);
2687 /* Note that this must be unsigned 64 bit arithmetic: */
edac4d8a 2688 int istatus = count - offset >= gt->cval;
55d284af 2689 uint64_t nexttick;
194cbc49 2690 int irqstate;
55d284af
PM
2691
2692 gt->ctl = deposit32(gt->ctl, 2, 1, istatus);
194cbc49
PM
2693
2694 irqstate = (istatus && !(gt->ctl & 2));
2695 qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate);
2696
55d284af
PM
2697 if (istatus) {
2698 /* Next transition is when count rolls back over to zero */
2699 nexttick = UINT64_MAX;
2700 } else {
2701 /* Next transition is when we hit cval */
edac4d8a 2702 nexttick = gt->cval + offset;
55d284af
PM
2703 }
2704 /* Note that the desired next expiry time might be beyond the
2705 * signed-64-bit range of a QEMUTimer -- in this case we just
2706 * set the timer for as far in the future as possible. When the
2707 * timer expires we will reset the timer for any remaining period.
2708 */
7def8754 2709 if (nexttick > INT64_MAX / gt_cntfrq_period_ns(cpu)) {
4a0245b6
AJ
2710 timer_mod_ns(cpu->gt_timer[timeridx], INT64_MAX);
2711 } else {
2712 timer_mod(cpu->gt_timer[timeridx], nexttick);
55d284af 2713 }
194cbc49 2714 trace_arm_gt_recalc(timeridx, irqstate, nexttick);
55d284af
PM
2715 } else {
2716 /* Timer disabled: ISTATUS and timer output always clear */
2717 gt->ctl &= ~4;
2718 qemu_set_irq(cpu->gt_timer_outputs[timeridx], 0);
bc72ad67 2719 timer_del(cpu->gt_timer[timeridx]);
194cbc49 2720 trace_arm_gt_recalc_disabled(timeridx);
55d284af
PM
2721 }
2722}
2723
0e3eca4c
EI
2724static void gt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri,
2725 int timeridx)
55d284af 2726{
2fc0cc0e 2727 ARMCPU *cpu = env_archcpu(env);
55d284af 2728
bc72ad67 2729 timer_del(cpu->gt_timer[timeridx]);
55d284af
PM
2730}
2731
c4241c7d 2732static uint64_t gt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
55d284af 2733{
c4241c7d 2734 return gt_get_countervalue(env);
55d284af
PM
2735}
2736
53d1f856
RH
2737static uint64_t gt_virt_cnt_offset(CPUARMState *env)
2738{
2739 uint64_t hcr;
2740
2741 switch (arm_current_el(env)) {
2742 case 2:
2743 hcr = arm_hcr_el2_eff(env);
2744 if (hcr & HCR_E2H) {
2745 return 0;
2746 }
2747 break;
2748 case 0:
2749 hcr = arm_hcr_el2_eff(env);
2750 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2751 return 0;
2752 }
2753 break;
2754 }
2755
2756 return env->cp15.cntvoff_el2;
2757}
2758
edac4d8a
EI
2759static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
2760{
53d1f856 2761 return gt_get_countervalue(env) - gt_virt_cnt_offset(env);
edac4d8a
EI
2762}
2763
c4241c7d 2764static void gt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
0e3eca4c 2765 int timeridx,
c4241c7d 2766 uint64_t value)
55d284af 2767{
194cbc49 2768 trace_arm_gt_cval_write(timeridx, value);
55d284af 2769 env->cp15.c14_timer[timeridx].cval = value;
2fc0cc0e 2770 gt_recalc_timer(env_archcpu(env), timeridx);
55d284af 2771}
c4241c7d 2772
0e3eca4c
EI
2773static uint64_t gt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri,
2774 int timeridx)
55d284af 2775{
53d1f856
RH
2776 uint64_t offset = 0;
2777
2778 switch (timeridx) {
2779 case GTIMER_VIRT:
8c94b071 2780 case GTIMER_HYPVIRT:
53d1f856
RH
2781 offset = gt_virt_cnt_offset(env);
2782 break;
2783 }
55d284af 2784
c4241c7d 2785 return (uint32_t)(env->cp15.c14_timer[timeridx].cval -
edac4d8a 2786 (gt_get_countervalue(env) - offset));
55d284af
PM
2787}
2788
c4241c7d 2789static void gt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
0e3eca4c 2790 int timeridx,
c4241c7d 2791 uint64_t value)
55d284af 2792{
53d1f856
RH
2793 uint64_t offset = 0;
2794
2795 switch (timeridx) {
2796 case GTIMER_VIRT:
8c94b071 2797 case GTIMER_HYPVIRT:
53d1f856
RH
2798 offset = gt_virt_cnt_offset(env);
2799 break;
2800 }
55d284af 2801
194cbc49 2802 trace_arm_gt_tval_write(timeridx, value);
edac4d8a 2803 env->cp15.c14_timer[timeridx].cval = gt_get_countervalue(env) - offset +
18084b2f 2804 sextract64(value, 0, 32);
2fc0cc0e 2805 gt_recalc_timer(env_archcpu(env), timeridx);
55d284af
PM
2806}
2807
c4241c7d 2808static void gt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
0e3eca4c 2809 int timeridx,
c4241c7d 2810 uint64_t value)
55d284af 2811{
2fc0cc0e 2812 ARMCPU *cpu = env_archcpu(env);
55d284af
PM
2813 uint32_t oldval = env->cp15.c14_timer[timeridx].ctl;
2814
194cbc49 2815 trace_arm_gt_ctl_write(timeridx, value);
d3afacc7 2816 env->cp15.c14_timer[timeridx].ctl = deposit64(oldval, 0, 2, value);
55d284af
PM
2817 if ((oldval ^ value) & 1) {
2818 /* Enable toggled */
2819 gt_recalc_timer(cpu, timeridx);
d3afacc7 2820 } else if ((oldval ^ value) & 2) {
55d284af
PM
2821 /* IMASK toggled: don't need to recalculate,
2822 * just set the interrupt line based on ISTATUS
2823 */
194cbc49
PM
2824 int irqstate = (oldval & 4) && !(value & 2);
2825
2826 trace_arm_gt_imask_toggle(timeridx, irqstate);
2827 qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate);
55d284af 2828 }
55d284af
PM
2829}
2830
0e3eca4c
EI
2831static void gt_phys_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2832{
2833 gt_timer_reset(env, ri, GTIMER_PHYS);
2834}
2835
2836static void gt_phys_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2837 uint64_t value)
2838{
2839 gt_cval_write(env, ri, GTIMER_PHYS, value);
2840}
2841
2842static uint64_t gt_phys_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2843{
2844 return gt_tval_read(env, ri, GTIMER_PHYS);
2845}
2846
2847static void gt_phys_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2848 uint64_t value)
2849{
2850 gt_tval_write(env, ri, GTIMER_PHYS, value);
2851}
2852
2853static void gt_phys_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2854 uint64_t value)
2855{
2856 gt_ctl_write(env, ri, GTIMER_PHYS, value);
2857}
2858
bb5972e4
RH
2859static int gt_phys_redir_timeridx(CPUARMState *env)
2860{
2861 switch (arm_mmu_idx(env)) {
2862 case ARMMMUIdx_E20_0:
2863 case ARMMMUIdx_E20_2:
452ef8cb 2864 case ARMMMUIdx_E20_2_PAN:
b6ad6062
RDC
2865 case ARMMMUIdx_SE20_0:
2866 case ARMMMUIdx_SE20_2:
2867 case ARMMMUIdx_SE20_2_PAN:
bb5972e4
RH
2868 return GTIMER_HYP;
2869 default:
2870 return GTIMER_PHYS;
2871 }
2872}
2873
2874static int gt_virt_redir_timeridx(CPUARMState *env)
2875{
2876 switch (arm_mmu_idx(env)) {
2877 case ARMMMUIdx_E20_0:
2878 case ARMMMUIdx_E20_2:
452ef8cb 2879 case ARMMMUIdx_E20_2_PAN:
b6ad6062
RDC
2880 case ARMMMUIdx_SE20_0:
2881 case ARMMMUIdx_SE20_2:
2882 case ARMMMUIdx_SE20_2_PAN:
bb5972e4
RH
2883 return GTIMER_HYPVIRT;
2884 default:
2885 return GTIMER_VIRT;
2886 }
2887}
2888
2889static uint64_t gt_phys_redir_cval_read(CPUARMState *env,
2890 const ARMCPRegInfo *ri)
2891{
2892 int timeridx = gt_phys_redir_timeridx(env);
2893 return env->cp15.c14_timer[timeridx].cval;
2894}
2895
2896static void gt_phys_redir_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2897 uint64_t value)
2898{
2899 int timeridx = gt_phys_redir_timeridx(env);
2900 gt_cval_write(env, ri, timeridx, value);
2901}
2902
2903static uint64_t gt_phys_redir_tval_read(CPUARMState *env,
2904 const ARMCPRegInfo *ri)
2905{
2906 int timeridx = gt_phys_redir_timeridx(env);
2907 return gt_tval_read(env, ri, timeridx);
2908}
2909
2910static void gt_phys_redir_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2911 uint64_t value)
2912{
2913 int timeridx = gt_phys_redir_timeridx(env);
2914 gt_tval_write(env, ri, timeridx, value);
2915}
2916
2917static uint64_t gt_phys_redir_ctl_read(CPUARMState *env,
2918 const ARMCPRegInfo *ri)
2919{
2920 int timeridx = gt_phys_redir_timeridx(env);
2921 return env->cp15.c14_timer[timeridx].ctl;
2922}
2923
2924static void gt_phys_redir_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2925 uint64_t value)
2926{
2927 int timeridx = gt_phys_redir_timeridx(env);
2928 gt_ctl_write(env, ri, timeridx, value);
2929}
2930
0e3eca4c
EI
2931static void gt_virt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2932{
2933 gt_timer_reset(env, ri, GTIMER_VIRT);
2934}
2935
2936static void gt_virt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2937 uint64_t value)
2938{
2939 gt_cval_write(env, ri, GTIMER_VIRT, value);
2940}
2941
2942static uint64_t gt_virt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2943{
2944 return gt_tval_read(env, ri, GTIMER_VIRT);
2945}
2946
2947static void gt_virt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2948 uint64_t value)
2949{
2950 gt_tval_write(env, ri, GTIMER_VIRT, value);
2951}
2952
2953static void gt_virt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2954 uint64_t value)
2955{
2956 gt_ctl_write(env, ri, GTIMER_VIRT, value);
2957}
2958
edac4d8a
EI
2959static void gt_cntvoff_write(CPUARMState *env, const ARMCPRegInfo *ri,
2960 uint64_t value)
2961{
2fc0cc0e 2962 ARMCPU *cpu = env_archcpu(env);
edac4d8a 2963
194cbc49 2964 trace_arm_gt_cntvoff_write(value);
edac4d8a
EI
2965 raw_write(env, ri, value);
2966 gt_recalc_timer(cpu, GTIMER_VIRT);
2967}
2968
bb5972e4
RH
2969static uint64_t gt_virt_redir_cval_read(CPUARMState *env,
2970 const ARMCPRegInfo *ri)
2971{
2972 int timeridx = gt_virt_redir_timeridx(env);
2973 return env->cp15.c14_timer[timeridx].cval;
2974}
2975
2976static void gt_virt_redir_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2977 uint64_t value)
2978{
2979 int timeridx = gt_virt_redir_timeridx(env);
2980 gt_cval_write(env, ri, timeridx, value);
2981}
2982
2983static uint64_t gt_virt_redir_tval_read(CPUARMState *env,
2984 const ARMCPRegInfo *ri)
2985{
2986 int timeridx = gt_virt_redir_timeridx(env);
2987 return gt_tval_read(env, ri, timeridx);
2988}
2989
2990static void gt_virt_redir_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2991 uint64_t value)
2992{
2993 int timeridx = gt_virt_redir_timeridx(env);
2994 gt_tval_write(env, ri, timeridx, value);
2995}
2996
2997static uint64_t gt_virt_redir_ctl_read(CPUARMState *env,
2998 const ARMCPRegInfo *ri)
2999{
3000 int timeridx = gt_virt_redir_timeridx(env);
3001 return env->cp15.c14_timer[timeridx].ctl;
3002}
3003
3004static void gt_virt_redir_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
3005 uint64_t value)
3006{
3007 int timeridx = gt_virt_redir_timeridx(env);
3008 gt_ctl_write(env, ri, timeridx, value);
3009}
3010
b0e66d95
EI
3011static void gt_hyp_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
3012{
3013 gt_timer_reset(env, ri, GTIMER_HYP);
3014}
3015
3016static void gt_hyp_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3017 uint64_t value)
3018{
3019 gt_cval_write(env, ri, GTIMER_HYP, value);
3020}
3021
3022static uint64_t gt_hyp_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
3023{
3024 return gt_tval_read(env, ri, GTIMER_HYP);
3025}
3026
3027static void gt_hyp_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3028 uint64_t value)
3029{
3030 gt_tval_write(env, ri, GTIMER_HYP, value);
3031}
3032
3033static void gt_hyp_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
3034 uint64_t value)
3035{
3036 gt_ctl_write(env, ri, GTIMER_HYP, value);
3037}
3038
b4d3978c
PM
3039static void gt_sec_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
3040{
3041 gt_timer_reset(env, ri, GTIMER_SEC);
3042}
3043
3044static void gt_sec_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3045 uint64_t value)
3046{
3047 gt_cval_write(env, ri, GTIMER_SEC, value);
3048}
3049
3050static uint64_t gt_sec_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
3051{
3052 return gt_tval_read(env, ri, GTIMER_SEC);
3053}
3054
3055static void gt_sec_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3056 uint64_t value)
3057{
3058 gt_tval_write(env, ri, GTIMER_SEC, value);
3059}
3060
3061static void gt_sec_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
3062 uint64_t value)
3063{
3064 gt_ctl_write(env, ri, GTIMER_SEC, value);
3065}
3066
8c94b071
RH
3067static void gt_hv_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
3068{
3069 gt_timer_reset(env, ri, GTIMER_HYPVIRT);
3070}
3071
3072static void gt_hv_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3073 uint64_t value)
3074{
3075 gt_cval_write(env, ri, GTIMER_HYPVIRT, value);
3076}
3077
3078static uint64_t gt_hv_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
3079{
3080 return gt_tval_read(env, ri, GTIMER_HYPVIRT);
3081}
3082
3083static void gt_hv_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3084 uint64_t value)
3085{
3086 gt_tval_write(env, ri, GTIMER_HYPVIRT, value);
3087}
3088
3089static void gt_hv_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
3090 uint64_t value)
3091{
3092 gt_ctl_write(env, ri, GTIMER_HYPVIRT, value);
3093}
3094
55d284af
PM
3095void arm_gt_ptimer_cb(void *opaque)
3096{
3097 ARMCPU *cpu = opaque;
3098
3099 gt_recalc_timer(cpu, GTIMER_PHYS);
3100}
3101
3102void arm_gt_vtimer_cb(void *opaque)
3103{
3104 ARMCPU *cpu = opaque;
3105
3106 gt_recalc_timer(cpu, GTIMER_VIRT);
3107}
3108
b0e66d95
EI
3109void arm_gt_htimer_cb(void *opaque)
3110{
3111 ARMCPU *cpu = opaque;
3112
3113 gt_recalc_timer(cpu, GTIMER_HYP);
3114}
3115
b4d3978c
PM
3116void arm_gt_stimer_cb(void *opaque)
3117{
3118 ARMCPU *cpu = opaque;
3119
3120 gt_recalc_timer(cpu, GTIMER_SEC);
3121}
3122
8c94b071
RH
3123void arm_gt_hvtimer_cb(void *opaque)
3124{
3125 ARMCPU *cpu = opaque;
3126
3127 gt_recalc_timer(cpu, GTIMER_HYPVIRT);
3128}
3129
96eec6b2
AJ
3130static void arm_gt_cntfrq_reset(CPUARMState *env, const ARMCPRegInfo *opaque)
3131{
3132 ARMCPU *cpu = env_archcpu(env);
3133
3134 cpu->env.cp15.c14_cntfrq = cpu->gt_cntfrq_hz;
3135}
3136
55d284af
PM
3137static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
3138 /* Note that CNTFRQ is purely reads-as-written for the benefit
3139 * of software; writing it doesn't actually change the timer frequency.
3140 * Our reset value matches the fixed frequency we implement the timer at.
3141 */
3142 { .name = "CNTFRQ", .cp = 15, .crn = 14, .crm = 0, .opc1 = 0, .opc2 = 0,
7a0e58fa 3143 .type = ARM_CP_ALIAS,
a7adc4b7
PM
3144 .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
3145 .fieldoffset = offsetoflow32(CPUARMState, cp15.c14_cntfrq),
a7adc4b7
PM
3146 },
3147 { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
3148 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
3149 .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
55d284af 3150 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
96eec6b2 3151 .resetfn = arm_gt_cntfrq_reset,
55d284af
PM
3152 },
3153 /* overall control: mostly access permissions */
a7adc4b7
PM
3154 { .name = "CNTKCTL", .state = ARM_CP_STATE_BOTH,
3155 .opc0 = 3, .opc1 = 0, .crn = 14, .crm = 1, .opc2 = 0,
55d284af
PM
3156 .access = PL1_RW,
3157 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntkctl),
3158 .resetvalue = 0,
3159 },
3160 /* per-timer control */
3161 { .name = "CNTP_CTL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
9ff9dd3c 3162 .secure = ARM_CP_SECSTATE_NS,
daf1dc5f 3163 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
a7adc4b7
PM
3164 .accessfn = gt_ptimer_access,
3165 .fieldoffset = offsetoflow32(CPUARMState,
3166 cp15.c14_timer[GTIMER_PHYS].ctl),
bb5972e4
RH
3167 .readfn = gt_phys_redir_ctl_read, .raw_readfn = raw_read,
3168 .writefn = gt_phys_redir_ctl_write, .raw_writefn = raw_write,
a7adc4b7 3169 },
9c513e78 3170 { .name = "CNTP_CTL_S",
9ff9dd3c
PM
3171 .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
3172 .secure = ARM_CP_SECSTATE_S,
daf1dc5f 3173 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
9ff9dd3c
PM
3174 .accessfn = gt_ptimer_access,
3175 .fieldoffset = offsetoflow32(CPUARMState,
3176 cp15.c14_timer[GTIMER_SEC].ctl),
3177 .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
3178 },
a7adc4b7
PM
3179 { .name = "CNTP_CTL_EL0", .state = ARM_CP_STATE_AA64,
3180 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 1,
daf1dc5f 3181 .type = ARM_CP_IO, .access = PL0_RW,
a7adc4b7 3182 .accessfn = gt_ptimer_access,
55d284af
PM
3183 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
3184 .resetvalue = 0,
bb5972e4
RH
3185 .readfn = gt_phys_redir_ctl_read, .raw_readfn = raw_read,
3186 .writefn = gt_phys_redir_ctl_write, .raw_writefn = raw_write,
55d284af
PM
3187 },
3188 { .name = "CNTV_CTL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 1,
daf1dc5f 3189 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
a7adc4b7
PM
3190 .accessfn = gt_vtimer_access,
3191 .fieldoffset = offsetoflow32(CPUARMState,
3192 cp15.c14_timer[GTIMER_VIRT].ctl),
bb5972e4
RH
3193 .readfn = gt_virt_redir_ctl_read, .raw_readfn = raw_read,
3194 .writefn = gt_virt_redir_ctl_write, .raw_writefn = raw_write,
a7adc4b7
PM
3195 },
3196 { .name = "CNTV_CTL_EL0", .state = ARM_CP_STATE_AA64,
3197 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 1,
daf1dc5f 3198 .type = ARM_CP_IO, .access = PL0_RW,
a7adc4b7 3199 .accessfn = gt_vtimer_access,
55d284af
PM
3200 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
3201 .resetvalue = 0,
bb5972e4
RH
3202 .readfn = gt_virt_redir_ctl_read, .raw_readfn = raw_read,
3203 .writefn = gt_virt_redir_ctl_write, .raw_writefn = raw_write,
55d284af
PM
3204 },
3205 /* TimerValue views: a 32 bit downcounting view of the underlying state */
3206 { .name = "CNTP_TVAL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
9ff9dd3c 3207 .secure = ARM_CP_SECSTATE_NS,
daf1dc5f 3208 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
00108f2d 3209 .accessfn = gt_ptimer_access,
bb5972e4 3210 .readfn = gt_phys_redir_tval_read, .writefn = gt_phys_redir_tval_write,
55d284af 3211 },
9c513e78 3212 { .name = "CNTP_TVAL_S",
9ff9dd3c
PM
3213 .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
3214 .secure = ARM_CP_SECSTATE_S,
daf1dc5f 3215 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
9ff9dd3c
PM
3216 .accessfn = gt_ptimer_access,
3217 .readfn = gt_sec_tval_read, .writefn = gt_sec_tval_write,
3218 },
a7adc4b7
PM
3219 { .name = "CNTP_TVAL_EL0", .state = ARM_CP_STATE_AA64,
3220 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 0,
daf1dc5f 3221 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
0e3eca4c 3222 .accessfn = gt_ptimer_access, .resetfn = gt_phys_timer_reset,
bb5972e4 3223 .readfn = gt_phys_redir_tval_read, .writefn = gt_phys_redir_tval_write,
a7adc4b7 3224 },
55d284af 3225 { .name = "CNTV_TVAL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 0,
daf1dc5f 3226 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
00108f2d 3227 .accessfn = gt_vtimer_access,
bb5972e4 3228 .readfn = gt_virt_redir_tval_read, .writefn = gt_virt_redir_tval_write,
55d284af 3229 },
a7adc4b7
PM
3230 { .name = "CNTV_TVAL_EL0", .state = ARM_CP_STATE_AA64,
3231 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 0,
daf1dc5f 3232 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
0e3eca4c 3233 .accessfn = gt_vtimer_access, .resetfn = gt_virt_timer_reset,
bb5972e4 3234 .readfn = gt_virt_redir_tval_read, .writefn = gt_virt_redir_tval_write,
a7adc4b7 3235 },
55d284af
PM
3236 /* The counter itself */
3237 { .name = "CNTPCT", .cp = 15, .crm = 14, .opc1 = 0,
7a0e58fa 3238 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
00108f2d 3239 .accessfn = gt_pct_access,
a7adc4b7
PM
3240 .readfn = gt_cnt_read, .resetfn = arm_cp_reset_ignore,
3241 },
3242 { .name = "CNTPCT_EL0", .state = ARM_CP_STATE_AA64,
3243 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 1,
7a0e58fa 3244 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
d57b9ee8 3245 .accessfn = gt_pct_access, .readfn = gt_cnt_read,
55d284af
PM
3246 },
3247 { .name = "CNTVCT", .cp = 15, .crm = 14, .opc1 = 1,
7a0e58fa 3248 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
00108f2d 3249 .accessfn = gt_vct_access,
edac4d8a 3250 .readfn = gt_virt_cnt_read, .resetfn = arm_cp_reset_ignore,
a7adc4b7
PM
3251 },
3252 { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
3253 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
7a0e58fa 3254 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
d57b9ee8 3255 .accessfn = gt_vct_access, .readfn = gt_virt_cnt_read,
55d284af
PM
3256 },
3257 /* Comparison value, indicating when the timer goes off */
3258 { .name = "CNTP_CVAL", .cp = 15, .crm = 14, .opc1 = 2,
9ff9dd3c 3259 .secure = ARM_CP_SECSTATE_NS,
daf1dc5f 3260 .access = PL0_RW,
7a0e58fa 3261 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
55d284af 3262 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
b061a82b 3263 .accessfn = gt_ptimer_access,
bb5972e4
RH
3264 .readfn = gt_phys_redir_cval_read, .raw_readfn = raw_read,
3265 .writefn = gt_phys_redir_cval_write, .raw_writefn = raw_write,
a7adc4b7 3266 },
9c513e78 3267 { .name = "CNTP_CVAL_S", .cp = 15, .crm = 14, .opc1 = 2,
9ff9dd3c 3268 .secure = ARM_CP_SECSTATE_S,
daf1dc5f 3269 .access = PL0_RW,
9ff9dd3c
PM
3270 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
3271 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
3272 .accessfn = gt_ptimer_access,
3273 .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
3274 },
a7adc4b7
PM
3275 { .name = "CNTP_CVAL_EL0", .state = ARM_CP_STATE_AA64,
3276 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 2,
daf1dc5f 3277 .access = PL0_RW,
a7adc4b7
PM
3278 .type = ARM_CP_IO,
3279 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
12cde08a 3280 .resetvalue = 0, .accessfn = gt_ptimer_access,
bb5972e4
RH
3281 .readfn = gt_phys_redir_cval_read, .raw_readfn = raw_read,
3282 .writefn = gt_phys_redir_cval_write, .raw_writefn = raw_write,
55d284af
PM
3283 },
3284 { .name = "CNTV_CVAL", .cp = 15, .crm = 14, .opc1 = 3,
daf1dc5f 3285 .access = PL0_RW,
7a0e58fa 3286 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
55d284af 3287 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
b061a82b 3288 .accessfn = gt_vtimer_access,
bb5972e4
RH
3289 .readfn = gt_virt_redir_cval_read, .raw_readfn = raw_read,
3290 .writefn = gt_virt_redir_cval_write, .raw_writefn = raw_write,
a7adc4b7
PM
3291 },
3292 { .name = "CNTV_CVAL_EL0", .state = ARM_CP_STATE_AA64,
3293 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 2,
daf1dc5f 3294 .access = PL0_RW,
a7adc4b7
PM
3295 .type = ARM_CP_IO,
3296 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
3297 .resetvalue = 0, .accessfn = gt_vtimer_access,
bb5972e4
RH
3298 .readfn = gt_virt_redir_cval_read, .raw_readfn = raw_read,
3299 .writefn = gt_virt_redir_cval_write, .raw_writefn = raw_write,
55d284af 3300 },
b4d3978c
PM
3301 /* Secure timer -- this is actually restricted to only EL3
3302 * and configurably Secure-EL1 via the accessfn.
3303 */
3304 { .name = "CNTPS_TVAL_EL1", .state = ARM_CP_STATE_AA64,
3305 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 0,
3306 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW,
3307 .accessfn = gt_stimer_access,
3308 .readfn = gt_sec_tval_read,
3309 .writefn = gt_sec_tval_write,
3310 .resetfn = gt_sec_timer_reset,
3311 },
3312 { .name = "CNTPS_CTL_EL1", .state = ARM_CP_STATE_AA64,
3313 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 1,
3314 .type = ARM_CP_IO, .access = PL1_RW,
3315 .accessfn = gt_stimer_access,
3316 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].ctl),
3317 .resetvalue = 0,
3318 .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
3319 },
3320 { .name = "CNTPS_CVAL_EL1", .state = ARM_CP_STATE_AA64,
3321 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 2,
3322 .type = ARM_CP_IO, .access = PL1_RW,
3323 .accessfn = gt_stimer_access,
3324 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
3325 .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
3326 },
55d284af
PM
3327 REGINFO_SENTINEL
3328};
3329
bb5972e4
RH
3330static CPAccessResult e2h_access(CPUARMState *env, const ARMCPRegInfo *ri,
3331 bool isread)
3332{
3333 if (!(arm_hcr_el2_eff(env) & HCR_E2H)) {
3334 return CP_ACCESS_TRAP;
3335 }
3336 return CP_ACCESS_OK;
3337}
3338
55d284af 3339#else
26c4a83b
AB
3340
3341/* In user-mode most of the generic timer registers are inaccessible
3342 * however modern kernels (4.12+) allow access to cntvct_el0
55d284af 3343 */
26c4a83b
AB
3344
3345static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
3346{
7def8754
AJ
3347 ARMCPU *cpu = env_archcpu(env);
3348
26c4a83b
AB
3349 /* Currently we have no support for QEMUTimer in linux-user so we
3350 * can't call gt_get_countervalue(env), instead we directly
3351 * call the lower level functions.
3352 */
7def8754 3353 return cpu_get_clock() / gt_cntfrq_period_ns(cpu);
26c4a83b
AB
3354}
3355
6cc7a3ae 3356static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
26c4a83b
AB
3357 { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
3358 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
3359 .type = ARM_CP_CONST, .access = PL0_R /* no PL1_RW in linux-user */,
3360 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
3361 .resetvalue = NANOSECONDS_PER_SECOND / GTIMER_SCALE,
3362 },
3363 { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
3364 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
3365 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3366 .readfn = gt_virt_cnt_read,
3367 },
6cc7a3ae
PM
3368 REGINFO_SENTINEL
3369};
3370
55d284af
PM
3371#endif
3372
c4241c7d 3373static void par_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
4a501606 3374{
891a2fe7 3375 if (arm_feature(env, ARM_FEATURE_LPAE)) {
8d5c773e 3376 raw_write(env, ri, value);
891a2fe7 3377 } else if (arm_feature(env, ARM_FEATURE_V7)) {
8d5c773e 3378 raw_write(env, ri, value & 0xfffff6ff);
4a501606 3379 } else {
8d5c773e 3380 raw_write(env, ri, value & 0xfffff1ff);
4a501606 3381 }
4a501606
PM
3382}
3383
3384#ifndef CONFIG_USER_ONLY
3385/* get_phys_addr() isn't present for user-mode-only targets */
702a9357 3386
3f208fd7
PM
3387static CPAccessResult ats_access(CPUARMState *env, const ARMCPRegInfo *ri,
3388 bool isread)
92611c00
PM
3389{
3390 if (ri->opc2 & 4) {
87562e4f
PM
3391 /* The ATS12NSO* operations must trap to EL3 if executed in
3392 * Secure EL1 (which can only happen if EL3 is AArch64).
3393 * They are simply UNDEF if executed from NS EL1.
3394 * They function normally from EL2 or EL3.
92611c00 3395 */
87562e4f
PM
3396 if (arm_current_el(env) == 1) {
3397 if (arm_is_secure_below_el3(env)) {
3398 return CP_ACCESS_TRAP_UNCATEGORIZED_EL3;
3399 }
3400 return CP_ACCESS_TRAP_UNCATEGORIZED;
3401 }
92611c00
PM
3402 }
3403 return CP_ACCESS_OK;
3404}
3405
9fb005b0 3406#ifdef CONFIG_TCG
060e8a48 3407static uint64_t do_ats_write(CPUARMState *env, uint64_t value,
03ae85f8 3408 MMUAccessType access_type, ARMMMUIdx mmu_idx)
4a501606 3409{
a8170e5e 3410 hwaddr phys_addr;
4a501606
PM
3411 target_ulong page_size;
3412 int prot;
b7cc4e82 3413 bool ret;
01c097f7 3414 uint64_t par64;
1313e2d7 3415 bool format64 = false;
8bf5b6a9 3416 MemTxAttrs attrs = {};
e14b5a23 3417 ARMMMUFaultInfo fi = {};
5b2d261d 3418 ARMCacheAttrs cacheattrs = {};
4a501606 3419
5b2d261d 3420 ret = get_phys_addr(env, value, access_type, mmu_idx, &phys_addr, &attrs,
bc52bfeb 3421 &prot, &page_size, &fi, &cacheattrs);
1313e2d7 3422
0710b2fa
PM
3423 if (ret) {
3424 /*
3425 * Some kinds of translation fault must cause exceptions rather
3426 * than being reported in the PAR.
3427 */
3428 int current_el = arm_current_el(env);
3429 int target_el;
3430 uint32_t syn, fsr, fsc;
3431 bool take_exc = false;
3432
3433 if (fi.s1ptw && current_el == 1 && !arm_is_secure(env)
fee7aa46 3434 && arm_mmu_idx_is_stage1_of_2(mmu_idx)) {
0710b2fa
PM
3435 /*
3436 * Synchronous stage 2 fault on an access made as part of the
3437 * translation table walk for AT S1E0* or AT S1E1* insn
3438 * executed from NS EL1. If this is a synchronous external abort
3439 * and SCR_EL3.EA == 1, then we take a synchronous external abort
3440 * to EL3. Otherwise the fault is taken as an exception to EL2,
3441 * and HPFAR_EL2 holds the faulting IPA.
3442 */
3443 if (fi.type == ARMFault_SyncExternalOnWalk &&
3444 (env->cp15.scr_el3 & SCR_EA)) {
3445 target_el = 3;
3446 } else {
3447 env->cp15.hpfar_el2 = extract64(fi.s2addr, 12, 47) << 4;
3448 target_el = 2;
3449 }
3450 take_exc = true;
3451 } else if (fi.type == ARMFault_SyncExternalOnWalk) {
3452 /*
3453 * Synchronous external aborts during a translation table walk
3454 * are taken as Data Abort exceptions.
3455 */
3456 if (fi.stage2) {
3457 if (current_el == 3) {
3458 target_el = 3;
3459 } else {
3460 target_el = 2;
3461 }
3462 } else {
3463 target_el = exception_target_el(env);
3464 }
3465 take_exc = true;
3466 }
3467
3468 if (take_exc) {
3469 /* Construct FSR and FSC using same logic as arm_deliver_fault() */
3470 if (target_el == 2 || arm_el_is_aa64(env, target_el) ||
3471 arm_s1_regime_using_lpae_format(env, mmu_idx)) {
3472 fsr = arm_fi_to_lfsc(&fi);
3473 fsc = extract32(fsr, 0, 6);
3474 } else {
3475 fsr = arm_fi_to_sfsc(&fi);
3476 fsc = 0x3f;
3477 }
3478 /*
3479 * Report exception with ESR indicating a fault due to a
3480 * translation table walk for a cache maintenance instruction.
3481 */
e24fd076 3482 syn = syn_data_abort_no_iss(current_el == target_el, 0,
0710b2fa
PM
3483 fi.ea, 1, fi.s1ptw, 1, fsc);
3484 env->exception.vaddress = value;
3485 env->exception.fsr = fsr;
3486 raise_exception(env, EXCP_DATA_ABORT, syn, target_el);
3487 }
3488 }
3489
1313e2d7
EI
3490 if (is_a64(env)) {
3491 format64 = true;
3492 } else if (arm_feature(env, ARM_FEATURE_LPAE)) {
3493 /*
3494 * ATS1Cxx:
3495 * * TTBCR.EAE determines whether the result is returned using the
3496 * 32-bit or the 64-bit PAR format
3497 * * Instructions executed in Hyp mode always use the 64bit format
3498 *
3499 * ATS1S2NSOxx uses the 64bit format if any of the following is true:
3500 * * The Non-secure TTBCR.EAE bit is set to 1
3501 * * The implementation includes EL2, and the value of HCR.VM is 1
3502 *
9d1bab33
PM
3503 * (Note that HCR.DC makes HCR.VM behave as if it is 1.)
3504 *
23463e0e 3505 * ATS1Hx always uses the 64bit format.
1313e2d7
EI
3506 */
3507 format64 = arm_s1_regime_using_lpae_format(env, mmu_idx);
3508
3509 if (arm_feature(env, ARM_FEATURE_EL2)) {
452ef8cb
RH
3510 if (mmu_idx == ARMMMUIdx_E10_0 ||
3511 mmu_idx == ARMMMUIdx_E10_1 ||
3512 mmu_idx == ARMMMUIdx_E10_1_PAN) {
9d1bab33 3513 format64 |= env->cp15.hcr_el2 & (HCR_VM | HCR_DC);
1313e2d7
EI
3514 } else {
3515 format64 |= arm_current_el(env) == 2;
3516 }
3517 }
3518 }
3519
3520 if (format64) {
5efe9ed4 3521 /* Create a 64-bit PAR */
01c097f7 3522 par64 = (1 << 11); /* LPAE bit always set */
b7cc4e82 3523 if (!ret) {
702a9357 3524 par64 |= phys_addr & ~0xfffULL;
8bf5b6a9
PM
3525 if (!attrs.secure) {
3526 par64 |= (1 << 9); /* NS */
3527 }
5b2d261d
AB
3528 par64 |= (uint64_t)cacheattrs.attrs << 56; /* ATTR */
3529 par64 |= cacheattrs.shareability << 7; /* SH */
4a501606 3530 } else {
5efe9ed4
PM
3531 uint32_t fsr = arm_fi_to_lfsc(&fi);
3532
702a9357 3533 par64 |= 1; /* F */
b7cc4e82 3534 par64 |= (fsr & 0x3f) << 1; /* FS */
0f7b791b
PM
3535 if (fi.stage2) {
3536 par64 |= (1 << 9); /* S */
3537 }
3538 if (fi.s1ptw) {
3539 par64 |= (1 << 8); /* PTW */
3540 }
4a501606
PM
3541 }
3542 } else {
b7cc4e82 3543 /* fsr is a DFSR/IFSR value for the short descriptor
702a9357
PM
3544 * translation table format (with WnR always clear).
3545 * Convert it to a 32-bit PAR.
3546 */
b7cc4e82 3547 if (!ret) {
702a9357
PM
3548 /* We do not set any attribute bits in the PAR */
3549 if (page_size == (1 << 24)
3550 && arm_feature(env, ARM_FEATURE_V7)) {
01c097f7 3551 par64 = (phys_addr & 0xff000000) | (1 << 1);
702a9357 3552 } else {
01c097f7 3553 par64 = phys_addr & 0xfffff000;
702a9357 3554 }
8bf5b6a9
PM
3555 if (!attrs.secure) {
3556 par64 |= (1 << 9); /* NS */
3557 }
702a9357 3558 } else {
5efe9ed4
PM
3559 uint32_t fsr = arm_fi_to_sfsc(&fi);
3560
b7cc4e82
PC
3561 par64 = ((fsr & (1 << 10)) >> 5) | ((fsr & (1 << 12)) >> 6) |
3562 ((fsr & 0xf) << 1) | 1;
702a9357 3563 }
4a501606 3564 }
060e8a48
PM
3565 return par64;
3566}
9fb005b0 3567#endif /* CONFIG_TCG */
060e8a48
PM
3568
3569static void ats_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
3570{
9fb005b0 3571#ifdef CONFIG_TCG
03ae85f8 3572 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
060e8a48 3573 uint64_t par64;
d3649702
PM
3574 ARMMMUIdx mmu_idx;
3575 int el = arm_current_el(env);
3576 bool secure = arm_is_secure_below_el3(env);
060e8a48 3577
d3649702
PM
3578 switch (ri->opc2 & 6) {
3579 case 0:
04b07d29 3580 /* stage 1 current state PL1: ATS1CPR, ATS1CPW, ATS1CPRP, ATS1CPWP */
d3649702
PM
3581 switch (el) {
3582 case 3:
127b2b08 3583 mmu_idx = ARMMMUIdx_SE3;
d3649702
PM
3584 break;
3585 case 2:
b6ad6062 3586 g_assert(!secure); /* ARMv8.4-SecEL2 is 64-bit only */
04b07d29 3587 /* fall through */
d3649702 3588 case 1:
04b07d29
RH
3589 if (ri->crm == 9 && (env->uncached_cpsr & CPSR_PAN)) {
3590 mmu_idx = (secure ? ARMMMUIdx_SE10_1_PAN
3591 : ARMMMUIdx_Stage1_E1_PAN);
3592 } else {
3593 mmu_idx = secure ? ARMMMUIdx_SE10_1 : ARMMMUIdx_Stage1_E1;
3594 }
d3649702
PM
3595 break;
3596 default:
3597 g_assert_not_reached();
3598 }
3599 break;
3600 case 2:
3601 /* stage 1 current state PL0: ATS1CUR, ATS1CUW */
3602 switch (el) {
3603 case 3:
fba37aed 3604 mmu_idx = ARMMMUIdx_SE10_0;
d3649702
PM
3605 break;
3606 case 2:
2859d7b5 3607 mmu_idx = ARMMMUIdx_Stage1_E0;
d3649702
PM
3608 break;
3609 case 1:
fba37aed 3610 mmu_idx = secure ? ARMMMUIdx_SE10_0 : ARMMMUIdx_Stage1_E0;
d3649702
PM
3611 break;
3612 default:
3613 g_assert_not_reached();
3614 }
3615 break;
3616 case 4:
3617 /* stage 1+2 NonSecure PL1: ATS12NSOPR, ATS12NSOPW */
01b98b68 3618 mmu_idx = ARMMMUIdx_E10_1;
d3649702
PM
3619 break;
3620 case 6:
3621 /* stage 1+2 NonSecure PL0: ATS12NSOUR, ATS12NSOUW */
01b98b68 3622 mmu_idx = ARMMMUIdx_E10_0;
d3649702
PM
3623 break;
3624 default:
3625 g_assert_not_reached();
3626 }
3627
3628 par64 = do_ats_write(env, value, access_type, mmu_idx);
01c097f7
FA
3629
3630 A32_BANKED_CURRENT_REG_SET(env, par, par64);
9fb005b0
PMD
3631#else
3632 /* Handled by hardware accelerator. */
3633 g_assert_not_reached();
3634#endif /* CONFIG_TCG */
4a501606 3635}
060e8a48 3636
14db7fe0
PM
3637static void ats1h_write(CPUARMState *env, const ARMCPRegInfo *ri,
3638 uint64_t value)
3639{
9fb005b0 3640#ifdef CONFIG_TCG
03ae85f8 3641 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
14db7fe0
PM
3642 uint64_t par64;
3643
e013b741 3644 par64 = do_ats_write(env, value, access_type, ARMMMUIdx_E2);
14db7fe0
PM
3645
3646 A32_BANKED_CURRENT_REG_SET(env, par, par64);
9fb005b0
PMD
3647#else
3648 /* Handled by hardware accelerator. */
3649 g_assert_not_reached();
3650#endif /* CONFIG_TCG */
14db7fe0
PM
3651}
3652
3f208fd7
PM
3653static CPAccessResult at_s1e2_access(CPUARMState *env, const ARMCPRegInfo *ri,
3654 bool isread)
2a47df95
PM
3655{
3656 if (arm_current_el(env) == 3 && !(env->cp15.scr_el3 & SCR_NS)) {
3657 return CP_ACCESS_TRAP;
3658 }
3659 return CP_ACCESS_OK;
3660}
3661
060e8a48
PM
3662static void ats_write64(CPUARMState *env, const ARMCPRegInfo *ri,
3663 uint64_t value)
3664{
9fb005b0 3665#ifdef CONFIG_TCG
03ae85f8 3666 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
d3649702
PM
3667 ARMMMUIdx mmu_idx;
3668 int secure = arm_is_secure_below_el3(env);
3669
3670 switch (ri->opc2 & 6) {
3671 case 0:
3672 switch (ri->opc1) {
04b07d29
RH
3673 case 0: /* AT S1E1R, AT S1E1W, AT S1E1RP, AT S1E1WP */
3674 if (ri->crm == 9 && (env->pstate & PSTATE_PAN)) {
3675 mmu_idx = (secure ? ARMMMUIdx_SE10_1_PAN
3676 : ARMMMUIdx_Stage1_E1_PAN);
3677 } else {
3678 mmu_idx = secure ? ARMMMUIdx_SE10_1 : ARMMMUIdx_Stage1_E1;
3679 }
d3649702
PM
3680 break;
3681 case 4: /* AT S1E2R, AT S1E2W */
b6ad6062 3682 mmu_idx = secure ? ARMMMUIdx_SE2 : ARMMMUIdx_E2;
d3649702
PM
3683 break;
3684 case 6: /* AT S1E3R, AT S1E3W */
127b2b08 3685 mmu_idx = ARMMMUIdx_SE3;
d3649702
PM
3686 break;
3687 default:
3688 g_assert_not_reached();
3689 }
3690 break;
3691 case 2: /* AT S1E0R, AT S1E0W */
fba37aed 3692 mmu_idx = secure ? ARMMMUIdx_SE10_0 : ARMMMUIdx_Stage1_E0;
d3649702
PM
3693 break;
3694 case 4: /* AT S12E1R, AT S12E1W */
fba37aed 3695 mmu_idx = secure ? ARMMMUIdx_SE10_1 : ARMMMUIdx_E10_1;
d3649702
PM
3696 break;
3697 case 6: /* AT S12E0R, AT S12E0W */
fba37aed 3698 mmu_idx = secure ? ARMMMUIdx_SE10_0 : ARMMMUIdx_E10_0;
d3649702
PM
3699 break;
3700 default:
3701 g_assert_not_reached();
3702 }
060e8a48 3703
d3649702 3704 env->cp15.par_el[1] = do_ats_write(env, value, access_type, mmu_idx);
9fb005b0
PMD
3705#else
3706 /* Handled by hardware accelerator. */
3707 g_assert_not_reached();
3708#endif /* CONFIG_TCG */
060e8a48 3709}
4a501606
PM
3710#endif
3711
3712static const ARMCPRegInfo vapa_cp_reginfo[] = {
3713 { .name = "PAR", .cp = 15, .crn = 7, .crm = 4, .opc1 = 0, .opc2 = 0,
3714 .access = PL1_RW, .resetvalue = 0,
01c097f7
FA
3715 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.par_s),
3716 offsetoflow32(CPUARMState, cp15.par_ns) },
4a501606
PM
3717 .writefn = par_write },
3718#ifndef CONFIG_USER_ONLY
87562e4f 3719 /* This underdecoding is safe because the reginfo is NO_RAW. */
4a501606 3720 { .name = "ATS", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = CP_ANY,
92611c00 3721 .access = PL1_W, .accessfn = ats_access,
0710b2fa 3722 .writefn = ats_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
4a501606
PM
3723#endif
3724 REGINFO_SENTINEL
3725};
3726
18032bec
PM
3727/* Return basic MPU access permission bits. */
3728static uint32_t simple_mpu_ap_bits(uint32_t val)
3729{
3730 uint32_t ret;
3731 uint32_t mask;
3732 int i;
3733 ret = 0;
3734 mask = 3;
3735 for (i = 0; i < 16; i += 2) {
3736 ret |= (val >> i) & mask;
3737 mask <<= 2;
3738 }
3739 return ret;
3740}
3741
3742/* Pad basic MPU access permission bits to extended format. */
3743static uint32_t extended_mpu_ap_bits(uint32_t val)
3744{
3745 uint32_t ret;
3746 uint32_t mask;
3747 int i;
3748 ret = 0;
3749 mask = 3;
3750 for (i = 0; i < 16; i += 2) {
3751 ret |= (val & mask) << i;
3752 mask <<= 2;
3753 }
3754 return ret;
3755}
3756
c4241c7d
PM
3757static void pmsav5_data_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
3758 uint64_t value)
18032bec 3759{
7e09797c 3760 env->cp15.pmsav5_data_ap = extended_mpu_ap_bits(value);
18032bec
PM
3761}
3762
c4241c7d 3763static uint64_t pmsav5_data_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
18032bec 3764{
7e09797c 3765 return simple_mpu_ap_bits(env->cp15.pmsav5_data_ap);
18032bec
PM
3766}
3767
c4241c7d
PM
3768static void pmsav5_insn_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
3769 uint64_t value)
18032bec 3770{
7e09797c 3771 env->cp15.pmsav5_insn_ap = extended_mpu_ap_bits(value);
18032bec
PM
3772}
3773
c4241c7d 3774static uint64_t pmsav5_insn_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
18032bec 3775{
7e09797c 3776 return simple_mpu_ap_bits(env->cp15.pmsav5_insn_ap);
18032bec
PM
3777}
3778
6cb0b013
PC
3779static uint64_t pmsav7_read(CPUARMState *env, const ARMCPRegInfo *ri)
3780{
3781 uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
3782
3783 if (!u32p) {
3784 return 0;
3785 }
3786
1bc04a88 3787 u32p += env->pmsav7.rnr[M_REG_NS];
6cb0b013
PC
3788 return *u32p;
3789}
3790
3791static void pmsav7_write(CPUARMState *env, const ARMCPRegInfo *ri,
3792 uint64_t value)
3793{
2fc0cc0e 3794 ARMCPU *cpu = env_archcpu(env);
6cb0b013
PC
3795 uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
3796
3797 if (!u32p) {
3798 return;
3799 }
3800
1bc04a88 3801 u32p += env->pmsav7.rnr[M_REG_NS];
d10eb08f 3802 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
6cb0b013
PC
3803 *u32p = value;
3804}
3805
6cb0b013
PC
3806static void pmsav7_rgnr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3807 uint64_t value)
3808{
2fc0cc0e 3809 ARMCPU *cpu = env_archcpu(env);
6cb0b013
PC
3810 uint32_t nrgs = cpu->pmsav7_dregion;
3811
3812 if (value >= nrgs) {
3813 qemu_log_mask(LOG_GUEST_ERROR,
3814 "PMSAv7 RGNR write >= # supported regions, %" PRIu32
3815 " > %" PRIu32 "\n", (uint32_t)value, nrgs);
3816 return;
3817 }
3818
3819 raw_write(env, ri, value);
3820}
3821
3822static const ARMCPRegInfo pmsav7_cp_reginfo[] = {
69ceea64
PM
3823 /* Reset for all these registers is handled in arm_cpu_reset(),
3824 * because the PMSAv7 is also used by M-profile CPUs, which do
3825 * not register cpregs but still need the state to be reset.
3826 */
6cb0b013
PC
3827 { .name = "DRBAR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 0,
3828 .access = PL1_RW, .type = ARM_CP_NO_RAW,
3829 .fieldoffset = offsetof(CPUARMState, pmsav7.drbar),
69ceea64
PM
3830 .readfn = pmsav7_read, .writefn = pmsav7_write,
3831 .resetfn = arm_cp_reset_ignore },
6cb0b013
PC
3832 { .name = "DRSR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 2,
3833 .access = PL1_RW, .type = ARM_CP_NO_RAW,
3834 .fieldoffset = offsetof(CPUARMState, pmsav7.drsr),
69ceea64
PM
3835 .readfn = pmsav7_read, .writefn = pmsav7_write,
3836 .resetfn = arm_cp_reset_ignore },
6cb0b013
PC
3837 { .name = "DRACR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 4,
3838 .access = PL1_RW, .type = ARM_CP_NO_RAW,
3839 .fieldoffset = offsetof(CPUARMState, pmsav7.dracr),
69ceea64
PM
3840 .readfn = pmsav7_read, .writefn = pmsav7_write,
3841 .resetfn = arm_cp_reset_ignore },
6cb0b013
PC
3842 { .name = "RGNR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 2, .opc2 = 0,
3843 .access = PL1_RW,
1bc04a88 3844 .fieldoffset = offsetof(CPUARMState, pmsav7.rnr[M_REG_NS]),
69ceea64
PM
3845 .writefn = pmsav7_rgnr_write,
3846 .resetfn = arm_cp_reset_ignore },
6cb0b013
PC
3847 REGINFO_SENTINEL
3848};
3849
18032bec
PM
3850static const ARMCPRegInfo pmsav5_cp_reginfo[] = {
3851 { .name = "DATA_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
7a0e58fa 3852 .access = PL1_RW, .type = ARM_CP_ALIAS,
7e09797c 3853 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
18032bec
PM
3854 .readfn = pmsav5_data_ap_read, .writefn = pmsav5_data_ap_write, },
3855 { .name = "INSN_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
7a0e58fa 3856 .access = PL1_RW, .type = ARM_CP_ALIAS,
7e09797c 3857 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
18032bec
PM
3858 .readfn = pmsav5_insn_ap_read, .writefn = pmsav5_insn_ap_write, },
3859 { .name = "DATA_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 2,
3860 .access = PL1_RW,
7e09797c
PM
3861 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
3862 .resetvalue = 0, },
18032bec
PM
3863 { .name = "INSN_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 3,
3864 .access = PL1_RW,
7e09797c
PM
3865 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
3866 .resetvalue = 0, },
ecce5c3c
PM
3867 { .name = "DCACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
3868 .access = PL1_RW,
3869 .fieldoffset = offsetof(CPUARMState, cp15.c2_data), .resetvalue = 0, },
3870 { .name = "ICACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 1,
3871 .access = PL1_RW,
3872 .fieldoffset = offsetof(CPUARMState, cp15.c2_insn), .resetvalue = 0, },
06d76f31 3873 /* Protection region base and size registers */
e508a92b
PM
3874 { .name = "946_PRBS0", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0,
3875 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3876 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[0]) },
3877 { .name = "946_PRBS1", .cp = 15, .crn = 6, .crm = 1, .opc1 = 0,
3878 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3879 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[1]) },
3880 { .name = "946_PRBS2", .cp = 15, .crn = 6, .crm = 2, .opc1 = 0,
3881 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3882 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[2]) },
3883 { .name = "946_PRBS3", .cp = 15, .crn = 6, .crm = 3, .opc1 = 0,
3884 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3885 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[3]) },
3886 { .name = "946_PRBS4", .cp = 15, .crn = 6, .crm = 4, .opc1 = 0,
3887 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3888 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[4]) },
3889 { .name = "946_PRBS5", .cp = 15, .crn = 6, .crm = 5, .opc1 = 0,
3890 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3891 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[5]) },
3892 { .name = "946_PRBS6", .cp = 15, .crn = 6, .crm = 6, .opc1 = 0,
3893 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3894 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[6]) },
3895 { .name = "946_PRBS7", .cp = 15, .crn = 6, .crm = 7, .opc1 = 0,
3896 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3897 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[7]) },
18032bec
PM
3898 REGINFO_SENTINEL
3899};
3900
c4241c7d
PM
3901static void vmsa_ttbcr_raw_write(CPUARMState *env, const ARMCPRegInfo *ri,
3902 uint64_t value)
ecce5c3c 3903{
11f136ee 3904 TCR *tcr = raw_ptr(env, ri);
2ebcebe2
PM
3905 int maskshift = extract32(value, 0, 3);
3906
e389be16
FA
3907 if (!arm_feature(env, ARM_FEATURE_V8)) {
3908 if (arm_feature(env, ARM_FEATURE_LPAE) && (value & TTBCR_EAE)) {
3909 /* Pre ARMv8 bits [21:19], [15:14] and [6:3] are UNK/SBZP when
3910 * using Long-desciptor translation table format */
3911 value &= ~((7 << 19) | (3 << 14) | (0xf << 3));
3912 } else if (arm_feature(env, ARM_FEATURE_EL3)) {
3913 /* In an implementation that includes the Security Extensions
3914 * TTBCR has additional fields PD0 [4] and PD1 [5] for
3915 * Short-descriptor translation table format.
3916 */
3917 value &= TTBCR_PD1 | TTBCR_PD0 | TTBCR_N;
3918 } else {
3919 value &= TTBCR_N;
3920 }
e42c4db3 3921 }
e389be16 3922
b6af0975 3923 /* Update the masks corresponding to the TCR bank being written
11f136ee 3924 * Note that we always calculate mask and base_mask, but
e42c4db3 3925 * they are only used for short-descriptor tables (ie if EAE is 0);
11f136ee
FA
3926 * for long-descriptor tables the TCR fields are used differently
3927 * and the mask and base_mask values are meaningless.
e42c4db3 3928 */
11f136ee
FA
3929 tcr->raw_tcr = value;
3930 tcr->mask = ~(((uint32_t)0xffffffffu) >> maskshift);
3931 tcr->base_mask = ~((uint32_t)0x3fffu >> maskshift);
ecce5c3c
PM
3932}
3933
c4241c7d
PM
3934static void vmsa_ttbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3935 uint64_t value)
d4e6df63 3936{
2fc0cc0e 3937 ARMCPU *cpu = env_archcpu(env);
ab638a32 3938 TCR *tcr = raw_ptr(env, ri);
00c8cb0a 3939
d4e6df63
PM
3940 if (arm_feature(env, ARM_FEATURE_LPAE)) {
3941 /* With LPAE the TTBCR could result in a change of ASID
3942 * via the TTBCR.A1 bit, so do a TLB flush.
3943 */
d10eb08f 3944 tlb_flush(CPU(cpu));
d4e6df63 3945 }
ab638a32
RH
3946 /* Preserve the high half of TCR_EL1, set via TTBCR2. */
3947 value = deposit64(tcr->raw_tcr, 0, 32, value);
c4241c7d 3948 vmsa_ttbcr_raw_write(env, ri, value);
d4e6df63
PM
3949}
3950
ecce5c3c
PM
3951static void vmsa_ttbcr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
3952{
11f136ee
FA
3953 TCR *tcr = raw_ptr(env, ri);
3954
3955 /* Reset both the TCR as well as the masks corresponding to the bank of
3956 * the TCR being reset.
3957 */
3958 tcr->raw_tcr = 0;
3959 tcr->mask = 0;
3960 tcr->base_mask = 0xffffc000u;
ecce5c3c
PM
3961}
3962
d06dc933 3963static void vmsa_tcr_el12_write(CPUARMState *env, const ARMCPRegInfo *ri,
cb2e37df
PM
3964 uint64_t value)
3965{
2fc0cc0e 3966 ARMCPU *cpu = env_archcpu(env);
11f136ee 3967 TCR *tcr = raw_ptr(env, ri);
00c8cb0a 3968
cb2e37df 3969 /* For AArch64 the A1 bit could result in a change of ASID, so TLB flush. */
d10eb08f 3970 tlb_flush(CPU(cpu));
11f136ee 3971 tcr->raw_tcr = value;
cb2e37df
PM
3972}
3973
327ed10f
PM
3974static void vmsa_ttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3975 uint64_t value)
3976{
93f379b0
RH
3977 /* If the ASID changes (with a 64-bit write), we must flush the TLB. */
3978 if (cpreg_field_is_64bit(ri) &&
3979 extract64(raw_read(env, ri) ^ value, 48, 16) != 0) {
2fc0cc0e 3980 ARMCPU *cpu = env_archcpu(env);
d10eb08f 3981 tlb_flush(CPU(cpu));
327ed10f
PM
3982 }
3983 raw_write(env, ri, value);
3984}
3985
ed30da8e
RH
3986static void vmsa_tcr_ttbr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
3987 uint64_t value)
3988{
d06dc933
RH
3989 /*
3990 * If we are running with E2&0 regime, then an ASID is active.
3991 * Flush if that might be changing. Note we're not checking
3992 * TCR_EL2.A1 to know if this is really the TTBRx_EL2 that
3993 * holds the active ASID, only checking the field that might.
3994 */
3995 if (extract64(raw_read(env, ri) ^ value, 48, 16) &&
3996 (arm_hcr_el2_eff(env) & HCR_E2H)) {
b6ad6062
RDC
3997 uint16_t mask = ARMMMUIdxBit_E20_2 |
3998 ARMMMUIdxBit_E20_2_PAN |
3999 ARMMMUIdxBit_E20_0;
4000
4001 if (arm_is_secure_below_el3(env)) {
4002 mask >>= ARM_MMU_IDX_A_NS;
4003 }
4004
4005 tlb_flush_by_mmuidx(env_cpu(env), mask);
d06dc933 4006 }
ed30da8e
RH
4007 raw_write(env, ri, value);
4008}
4009
b698e9cf
EI
4010static void vttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4011 uint64_t value)
4012{
2fc0cc0e 4013 ARMCPU *cpu = env_archcpu(env);
b698e9cf
EI
4014 CPUState *cs = CPU(cpu);
4015
97fa9350
RH
4016 /*
4017 * A change in VMID to the stage2 page table (Stage2) invalidates
4018 * the combined stage 1&2 tlbs (EL10_1 and EL10_0).
4019 */
b698e9cf 4020 if (raw_read(env, ri) != value) {
c4f060e8
RDC
4021 uint16_t mask = ARMMMUIdxBit_E10_1 |
4022 ARMMMUIdxBit_E10_1_PAN |
4023 ARMMMUIdxBit_E10_0;
4024
4025 if (arm_is_secure_below_el3(env)) {
4026 mask >>= ARM_MMU_IDX_A_NS;
4027 }
4028
4029 tlb_flush_by_mmuidx(cs, mask);
b698e9cf
EI
4030 raw_write(env, ri, value);
4031 }
4032}
4033
8e5d75c9 4034static const ARMCPRegInfo vmsa_pmsa_cp_reginfo[] = {
18032bec 4035 { .name = "DFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
84929218 4036 .access = PL1_RW, .accessfn = access_tvm_trvm, .type = ARM_CP_ALIAS,
4a7e2d73 4037 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dfsr_s),
b061a82b 4038 offsetoflow32(CPUARMState, cp15.dfsr_ns) }, },
18032bec 4039 { .name = "IFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
84929218 4040 .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
88ca1c2d
FA
4041 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.ifsr_s),
4042 offsetoflow32(CPUARMState, cp15.ifsr_ns) } },
8e5d75c9 4043 { .name = "DFAR", .cp = 15, .opc1 = 0, .crn = 6, .crm = 0, .opc2 = 0,
84929218 4044 .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
8e5d75c9
PC
4045 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.dfar_s),
4046 offsetof(CPUARMState, cp15.dfar_ns) } },
4047 { .name = "FAR_EL1", .state = ARM_CP_STATE_AA64,
4048 .opc0 = 3, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 0,
84929218
RH
4049 .access = PL1_RW, .accessfn = access_tvm_trvm,
4050 .fieldoffset = offsetof(CPUARMState, cp15.far_el[1]),
8e5d75c9
PC
4051 .resetvalue = 0, },
4052 REGINFO_SENTINEL
4053};
4054
4055static const ARMCPRegInfo vmsa_cp_reginfo[] = {
6cd8a264
RH
4056 { .name = "ESR_EL1", .state = ARM_CP_STATE_AA64,
4057 .opc0 = 3, .crn = 5, .crm = 2, .opc1 = 0, .opc2 = 0,
84929218 4058 .access = PL1_RW, .accessfn = access_tvm_trvm,
d81c519c 4059 .fieldoffset = offsetof(CPUARMState, cp15.esr_el[1]), .resetvalue = 0, },
327ed10f 4060 { .name = "TTBR0_EL1", .state = ARM_CP_STATE_BOTH,
7dd8c9af 4061 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 0,
84929218
RH
4062 .access = PL1_RW, .accessfn = access_tvm_trvm,
4063 .writefn = vmsa_ttbr_write, .resetvalue = 0,
7dd8c9af
FA
4064 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
4065 offsetof(CPUARMState, cp15.ttbr0_ns) } },
327ed10f 4066 { .name = "TTBR1_EL1", .state = ARM_CP_STATE_BOTH,
7dd8c9af 4067 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 1,
84929218
RH
4068 .access = PL1_RW, .accessfn = access_tvm_trvm,
4069 .writefn = vmsa_ttbr_write, .resetvalue = 0,
7dd8c9af
FA
4070 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
4071 offsetof(CPUARMState, cp15.ttbr1_ns) } },
cb2e37df
PM
4072 { .name = "TCR_EL1", .state = ARM_CP_STATE_AA64,
4073 .opc0 = 3, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
84929218
RH
4074 .access = PL1_RW, .accessfn = access_tvm_trvm,
4075 .writefn = vmsa_tcr_el12_write,
cb2e37df 4076 .resetfn = vmsa_ttbcr_reset, .raw_writefn = raw_write,
11f136ee 4077 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[1]) },
cb2e37df 4078 { .name = "TTBCR", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
84929218
RH
4079 .access = PL1_RW, .accessfn = access_tvm_trvm,
4080 .type = ARM_CP_ALIAS, .writefn = vmsa_ttbcr_write,
b061a82b 4081 .raw_writefn = vmsa_ttbcr_raw_write,
11f136ee
FA
4082 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tcr_el[3]),
4083 offsetoflow32(CPUARMState, cp15.tcr_el[1])} },
18032bec
PM
4084 REGINFO_SENTINEL
4085};
4086
ab638a32
RH
4087/* Note that unlike TTBCR, writing to TTBCR2 does not require flushing
4088 * qemu tlbs nor adjusting cached masks.
4089 */
4090static const ARMCPRegInfo ttbcr2_reginfo = {
4091 .name = "TTBCR2", .cp = 15, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 3,
84929218
RH
4092 .access = PL1_RW, .accessfn = access_tvm_trvm,
4093 .type = ARM_CP_ALIAS,
ab638a32
RH
4094 .bank_fieldoffsets = { offsetofhigh32(CPUARMState, cp15.tcr_el[3]),
4095 offsetofhigh32(CPUARMState, cp15.tcr_el[1]) },
4096};
4097
c4241c7d
PM
4098static void omap_ticonfig_write(CPUARMState *env, const ARMCPRegInfo *ri,
4099 uint64_t value)
1047b9d7
PM
4100{
4101 env->cp15.c15_ticonfig = value & 0xe7;
4102 /* The OS_TYPE bit in this register changes the reported CPUID! */
4103 env->cp15.c0_cpuid = (value & (1 << 5)) ?
4104 ARM_CPUID_TI915T : ARM_CPUID_TI925T;
1047b9d7
PM
4105}
4106
c4241c7d
PM
4107static void omap_threadid_write(CPUARMState *env, const ARMCPRegInfo *ri,
4108 uint64_t value)
1047b9d7
PM
4109{
4110 env->cp15.c15_threadid = value & 0xffff;
1047b9d7
PM
4111}
4112
c4241c7d
PM
4113static void omap_wfi_write(CPUARMState *env, const ARMCPRegInfo *ri,
4114 uint64_t value)
1047b9d7
PM
4115{
4116 /* Wait-for-interrupt (deprecated) */
2fc0cc0e 4117 cpu_interrupt(env_cpu(env), CPU_INTERRUPT_HALT);
1047b9d7
PM
4118}
4119
c4241c7d
PM
4120static void omap_cachemaint_write(CPUARMState *env, const ARMCPRegInfo *ri,
4121 uint64_t value)
c4804214
PM
4122{
4123 /* On OMAP there are registers indicating the max/min index of dcache lines
4124 * containing a dirty line; cache flush operations have to reset these.
4125 */
4126 env->cp15.c15_i_max = 0x000;
4127 env->cp15.c15_i_min = 0xff0;
c4804214
PM
4128}
4129
18032bec
PM
4130static const ARMCPRegInfo omap_cp_reginfo[] = {
4131 { .name = "DFSR", .cp = 15, .crn = 5, .crm = CP_ANY,
4132 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_OVERRIDE,
d81c519c 4133 .fieldoffset = offsetoflow32(CPUARMState, cp15.esr_el[1]),
6cd8a264 4134 .resetvalue = 0, },
1047b9d7
PM
4135 { .name = "", .cp = 15, .crn = 15, .crm = 0, .opc1 = 0, .opc2 = 0,
4136 .access = PL1_RW, .type = ARM_CP_NOP },
4137 { .name = "TICONFIG", .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0,
4138 .access = PL1_RW,
4139 .fieldoffset = offsetof(CPUARMState, cp15.c15_ticonfig), .resetvalue = 0,
4140 .writefn = omap_ticonfig_write },
4141 { .name = "IMAX", .cp = 15, .crn = 15, .crm = 2, .opc1 = 0, .opc2 = 0,
4142 .access = PL1_RW,
4143 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_max), .resetvalue = 0, },
4144 { .name = "IMIN", .cp = 15, .crn = 15, .crm = 3, .opc1 = 0, .opc2 = 0,
4145 .access = PL1_RW, .resetvalue = 0xff0,
4146 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_min) },
4147 { .name = "THREADID", .cp = 15, .crn = 15, .crm = 4, .opc1 = 0, .opc2 = 0,
4148 .access = PL1_RW,
4149 .fieldoffset = offsetof(CPUARMState, cp15.c15_threadid), .resetvalue = 0,
4150 .writefn = omap_threadid_write },
4151 { .name = "TI925T_STATUS", .cp = 15, .crn = 15,
4152 .crm = 8, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
7a0e58fa 4153 .type = ARM_CP_NO_RAW,
1047b9d7
PM
4154 .readfn = arm_cp_read_zero, .writefn = omap_wfi_write, },
4155 /* TODO: Peripheral port remap register:
4156 * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt controller
4157 * base address at $rn & ~0xfff and map size of 0x200 << ($rn & 0xfff),
4158 * when MMU is off.
4159 */
c4804214 4160 { .name = "OMAP_CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
d4e6df63 4161 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
7a0e58fa 4162 .type = ARM_CP_OVERRIDE | ARM_CP_NO_RAW,
c4804214 4163 .writefn = omap_cachemaint_write },
34f90529
PM
4164 { .name = "C9", .cp = 15, .crn = 9,
4165 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW,
4166 .type = ARM_CP_CONST | ARM_CP_OVERRIDE, .resetvalue = 0 },
1047b9d7
PM
4167 REGINFO_SENTINEL
4168};
4169
c4241c7d
PM
4170static void xscale_cpar_write(CPUARMState *env, const ARMCPRegInfo *ri,
4171 uint64_t value)
1047b9d7 4172{
c0f4af17 4173 env->cp15.c15_cpar = value & 0x3fff;
1047b9d7
PM
4174}
4175
4176static const ARMCPRegInfo xscale_cp_reginfo[] = {
4177 { .name = "XSCALE_CPAR",
4178 .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
4179 .fieldoffset = offsetof(CPUARMState, cp15.c15_cpar), .resetvalue = 0,
4180 .writefn = xscale_cpar_write, },
2771db27
PM
4181 { .name = "XSCALE_AUXCR",
4182 .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 1, .access = PL1_RW,
4183 .fieldoffset = offsetof(CPUARMState, cp15.c1_xscaleauxcr),
4184 .resetvalue = 0, },
3b771579
PM
4185 /* XScale specific cache-lockdown: since we have no cache we NOP these
4186 * and hope the guest does not really rely on cache behaviour.
4187 */
4188 { .name = "XSCALE_LOCK_ICACHE_LINE",
4189 .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 0,
4190 .access = PL1_W, .type = ARM_CP_NOP },
4191 { .name = "XSCALE_UNLOCK_ICACHE",
4192 .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 1,
4193 .access = PL1_W, .type = ARM_CP_NOP },
4194 { .name = "XSCALE_DCACHE_LOCK",
4195 .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 0,
4196 .access = PL1_RW, .type = ARM_CP_NOP },
4197 { .name = "XSCALE_UNLOCK_DCACHE",
4198 .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 1,
4199 .access = PL1_W, .type = ARM_CP_NOP },
1047b9d7
PM
4200 REGINFO_SENTINEL
4201};
4202
4203static const ARMCPRegInfo dummy_c15_cp_reginfo[] = {
4204 /* RAZ/WI the whole crn=15 space, when we don't have a more specific
4205 * implementation of this implementation-defined space.
4206 * Ideally this should eventually disappear in favour of actually
4207 * implementing the correct behaviour for all cores.
4208 */
4209 { .name = "C15_IMPDEF", .cp = 15, .crn = 15,
4210 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
3671cd87 4211 .access = PL1_RW,
7a0e58fa 4212 .type = ARM_CP_CONST | ARM_CP_NO_RAW | ARM_CP_OVERRIDE,
d4e6df63 4213 .resetvalue = 0 },
18032bec
PM
4214 REGINFO_SENTINEL
4215};
4216
c4804214
PM
4217static const ARMCPRegInfo cache_dirty_status_cp_reginfo[] = {
4218 /* Cache status: RAZ because we have no cache so it's always clean */
4219 { .name = "CDSR", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 6,
7a0e58fa 4220 .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 4221 .resetvalue = 0 },
c4804214
PM
4222 REGINFO_SENTINEL
4223};
4224
4225static const ARMCPRegInfo cache_block_ops_cp_reginfo[] = {
4226 /* We never have a a block transfer operation in progress */
4227 { .name = "BXSR", .cp = 15, .crn = 7, .crm = 12, .opc1 = 0, .opc2 = 4,
7a0e58fa 4228 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 4229 .resetvalue = 0 },
30b05bba
PM
4230 /* The cache ops themselves: these all NOP for QEMU */
4231 { .name = "IICR", .cp = 15, .crm = 5, .opc1 = 0,
4232 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
4233 { .name = "IDCR", .cp = 15, .crm = 6, .opc1 = 0,
4234 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
4235 { .name = "CDCR", .cp = 15, .crm = 12, .opc1 = 0,
4236 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
4237 { .name = "PIR", .cp = 15, .crm = 12, .opc1 = 1,
4238 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
4239 { .name = "PDR", .cp = 15, .crm = 12, .opc1 = 2,
4240 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
4241 { .name = "CIDCR", .cp = 15, .crm = 14, .opc1 = 0,
4242 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
c4804214
PM
4243 REGINFO_SENTINEL
4244};
4245
4246static const ARMCPRegInfo cache_test_clean_cp_reginfo[] = {
4247 /* The cache test-and-clean instructions always return (1 << 30)
4248 * to indicate that there are no dirty cache lines.
4249 */
4250 { .name = "TC_DCACHE", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 3,
7a0e58fa 4251 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 4252 .resetvalue = (1 << 30) },
c4804214 4253 { .name = "TCI_DCACHE", .cp = 15, .crn = 7, .crm = 14, .opc1 = 0, .opc2 = 3,
7a0e58fa 4254 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 4255 .resetvalue = (1 << 30) },
c4804214
PM
4256 REGINFO_SENTINEL
4257};
4258
34f90529
PM
4259static const ARMCPRegInfo strongarm_cp_reginfo[] = {
4260 /* Ignore ReadBuffer accesses */
4261 { .name = "C9_READBUFFER", .cp = 15, .crn = 9,
4262 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
d4e6df63 4263 .access = PL1_RW, .resetvalue = 0,
7a0e58fa 4264 .type = ARM_CP_CONST | ARM_CP_OVERRIDE | ARM_CP_NO_RAW },
34f90529
PM
4265 REGINFO_SENTINEL
4266};
4267
731de9e6
EI
4268static uint64_t midr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4269{
731de9e6 4270 unsigned int cur_el = arm_current_el(env);
731de9e6 4271
e6ef0169 4272 if (arm_is_el2_enabled(env) && cur_el == 1) {
731de9e6
EI
4273 return env->cp15.vpidr_el2;
4274 }
4275 return raw_read(env, ri);
4276}
4277
06a7e647 4278static uint64_t mpidr_read_val(CPUARMState *env)
81bdde9d 4279{
2fc0cc0e 4280 ARMCPU *cpu = env_archcpu(env);
eb5e1d3c
PF
4281 uint64_t mpidr = cpu->mp_affinity;
4282
81bdde9d 4283 if (arm_feature(env, ARM_FEATURE_V7MP)) {
78dbbbe4 4284 mpidr |= (1U << 31);
81bdde9d
PM
4285 /* Cores which are uniprocessor (non-coherent)
4286 * but still implement the MP extensions set
a8e81b31 4287 * bit 30. (For instance, Cortex-R5).
81bdde9d 4288 */
a8e81b31
PC
4289 if (cpu->mp_is_up) {
4290 mpidr |= (1u << 30);
4291 }
81bdde9d 4292 }
c4241c7d 4293 return mpidr;
81bdde9d
PM
4294}
4295
06a7e647
EI
4296static uint64_t mpidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4297{
f0d574d6 4298 unsigned int cur_el = arm_current_el(env);
f0d574d6 4299
e6ef0169 4300 if (arm_is_el2_enabled(env) && cur_el == 1) {
f0d574d6
EI
4301 return env->cp15.vmpidr_el2;
4302 }
06a7e647
EI
4303 return mpidr_read_val(env);
4304}
4305
7ac681cf 4306static const ARMCPRegInfo lpae_cp_reginfo[] = {
a903c449 4307 /* NOP AMAIR0/1 */
b0fe2427
PM
4308 { .name = "AMAIR0", .state = ARM_CP_STATE_BOTH,
4309 .opc0 = 3, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 0,
84929218
RH
4310 .access = PL1_RW, .accessfn = access_tvm_trvm,
4311 .type = ARM_CP_CONST, .resetvalue = 0 },
b0fe2427 4312 /* AMAIR1 is mapped to AMAIR_EL1[63:32] */
7ac681cf 4313 { .name = "AMAIR1", .cp = 15, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 1,
84929218
RH
4314 .access = PL1_RW, .accessfn = access_tvm_trvm,
4315 .type = ARM_CP_CONST, .resetvalue = 0 },
891a2fe7 4316 { .name = "PAR", .cp = 15, .crm = 7, .opc1 = 0,
01c097f7
FA
4317 .access = PL1_RW, .type = ARM_CP_64BIT, .resetvalue = 0,
4318 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.par_s),
4319 offsetof(CPUARMState, cp15.par_ns)} },
891a2fe7 4320 { .name = "TTBR0", .cp = 15, .crm = 2, .opc1 = 0,
84929218
RH
4321 .access = PL1_RW, .accessfn = access_tvm_trvm,
4322 .type = ARM_CP_64BIT | ARM_CP_ALIAS,
7dd8c9af
FA
4323 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
4324 offsetof(CPUARMState, cp15.ttbr0_ns) },
b061a82b 4325 .writefn = vmsa_ttbr_write, },
891a2fe7 4326 { .name = "TTBR1", .cp = 15, .crm = 2, .opc1 = 1,
84929218
RH
4327 .access = PL1_RW, .accessfn = access_tvm_trvm,
4328 .type = ARM_CP_64BIT | ARM_CP_ALIAS,
7dd8c9af
FA
4329 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
4330 offsetof(CPUARMState, cp15.ttbr1_ns) },
b061a82b 4331 .writefn = vmsa_ttbr_write, },
7ac681cf
PM
4332 REGINFO_SENTINEL
4333};
4334
c4241c7d 4335static uint64_t aa64_fpcr_read(CPUARMState *env, const ARMCPRegInfo *ri)
b0d2b7d0 4336{
c4241c7d 4337 return vfp_get_fpcr(env);
b0d2b7d0
PM
4338}
4339
c4241c7d
PM
4340static void aa64_fpcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4341 uint64_t value)
b0d2b7d0
PM
4342{
4343 vfp_set_fpcr(env, value);
b0d2b7d0
PM
4344}
4345
c4241c7d 4346static uint64_t aa64_fpsr_read(CPUARMState *env, const ARMCPRegInfo *ri)
b0d2b7d0 4347{
c4241c7d 4348 return vfp_get_fpsr(env);
b0d2b7d0
PM
4349}
4350
c4241c7d
PM
4351static void aa64_fpsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4352 uint64_t value)
b0d2b7d0
PM
4353{
4354 vfp_set_fpsr(env, value);
b0d2b7d0
PM
4355}
4356
3f208fd7
PM
4357static CPAccessResult aa64_daif_access(CPUARMState *env, const ARMCPRegInfo *ri,
4358 bool isread)
c2b820fe 4359{
aaec1432 4360 if (arm_current_el(env) == 0 && !(arm_sctlr(env, 0) & SCTLR_UMA)) {
c2b820fe
PM
4361 return CP_ACCESS_TRAP;
4362 }
4363 return CP_ACCESS_OK;
4364}
4365
4366static void aa64_daif_write(CPUARMState *env, const ARMCPRegInfo *ri,
4367 uint64_t value)
4368{
4369 env->daif = value & PSTATE_DAIF;
4370}
4371
220f508f
RH
4372static uint64_t aa64_pan_read(CPUARMState *env, const ARMCPRegInfo *ri)
4373{
4374 return env->pstate & PSTATE_PAN;
4375}
4376
4377static void aa64_pan_write(CPUARMState *env, const ARMCPRegInfo *ri,
4378 uint64_t value)
4379{
4380 env->pstate = (env->pstate & ~PSTATE_PAN) | (value & PSTATE_PAN);
4381}
4382
4383static const ARMCPRegInfo pan_reginfo = {
4384 .name = "PAN", .state = ARM_CP_STATE_AA64,
4385 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 3,
4386 .type = ARM_CP_NO_RAW, .access = PL1_RW,
4387 .readfn = aa64_pan_read, .writefn = aa64_pan_write
4388};
4389
9eeb7a1c
RH
4390static uint64_t aa64_uao_read(CPUARMState *env, const ARMCPRegInfo *ri)
4391{
4392 return env->pstate & PSTATE_UAO;
4393}
4394
4395static void aa64_uao_write(CPUARMState *env, const ARMCPRegInfo *ri,
4396 uint64_t value)
4397{
4398 env->pstate = (env->pstate & ~PSTATE_UAO) | (value & PSTATE_UAO);
4399}
4400
4401static const ARMCPRegInfo uao_reginfo = {
4402 .name = "UAO", .state = ARM_CP_STATE_AA64,
4403 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 4,
4404 .type = ARM_CP_NO_RAW, .access = PL1_RW,
4405 .readfn = aa64_uao_read, .writefn = aa64_uao_write
4406};
4407
38262d8a
RH
4408static CPAccessResult aa64_cacheop_poc_access(CPUARMState *env,
4409 const ARMCPRegInfo *ri,
4410 bool isread)
8af35c37 4411{
38262d8a
RH
4412 /* Cache invalidate/clean to Point of Coherency or Persistence... */
4413 switch (arm_current_el(env)) {
4414 case 0:
4415 /* ... EL0 must UNDEF unless SCTLR_EL1.UCI is set. */
4416 if (!(arm_sctlr(env, 0) & SCTLR_UCI)) {
4417 return CP_ACCESS_TRAP;
4418 }
4419 /* fall through */
4420 case 1:
4421 /* ... EL1 must trap to EL2 if HCR_EL2.TPCP is set. */
4422 if (arm_hcr_el2_eff(env) & HCR_TPCP) {
4423 return CP_ACCESS_TRAP_EL2;
4424 }
4425 break;
8af35c37
PM
4426 }
4427 return CP_ACCESS_OK;
4428}
4429
38262d8a 4430static CPAccessResult aa64_cacheop_pou_access(CPUARMState *env,
1bed4d2e
RH
4431 const ARMCPRegInfo *ri,
4432 bool isread)
4433{
38262d8a 4434 /* Cache invalidate/clean to Point of Unification... */
1bed4d2e
RH
4435 switch (arm_current_el(env)) {
4436 case 0:
4437 /* ... EL0 must UNDEF unless SCTLR_EL1.UCI is set. */
4438 if (!(arm_sctlr(env, 0) & SCTLR_UCI)) {
4439 return CP_ACCESS_TRAP;
4440 }
4441 /* fall through */
4442 case 1:
38262d8a
RH
4443 /* ... EL1 must trap to EL2 if HCR_EL2.TPU is set. */
4444 if (arm_hcr_el2_eff(env) & HCR_TPU) {
1bed4d2e
RH
4445 return CP_ACCESS_TRAP_EL2;
4446 }
4447 break;
4448 }
4449 return CP_ACCESS_OK;
4450}
4451
dbb1fb27
AB
4452/* See: D4.7.2 TLB maintenance requirements and the TLB maintenance instructions
4453 * Page D4-1736 (DDI0487A.b)
4454 */
4455
b7e0730d
RH
4456static int vae1_tlbmask(CPUARMState *env)
4457{
e04a5752
RDC
4458 uint64_t hcr = arm_hcr_el2_eff(env);
4459
4460 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
b6ad6062
RDC
4461 uint16_t mask = ARMMMUIdxBit_E20_2 |
4462 ARMMMUIdxBit_E20_2_PAN |
4463 ARMMMUIdxBit_E20_0;
4464
4465 if (arm_is_secure_below_el3(env)) {
4466 mask >>= ARM_MMU_IDX_A_NS;
4467 }
4468
4469 return mask;
e04a5752
RDC
4470 } else if (arm_is_secure_below_el3(env)) {
4471 return ARMMMUIdxBit_SE10_1 |
4472 ARMMMUIdxBit_SE10_1_PAN |
4473 ARMMMUIdxBit_SE10_0;
b7e0730d 4474 } else {
452ef8cb
RH
4475 return ARMMMUIdxBit_E10_1 |
4476 ARMMMUIdxBit_E10_1_PAN |
4477 ARMMMUIdxBit_E10_0;
b7e0730d
RH
4478 }
4479}
4480
ea04dce7
RH
4481/* Return 56 if TBI is enabled, 64 otherwise. */
4482static int tlbbits_for_regime(CPUARMState *env, ARMMMUIdx mmu_idx,
4483 uint64_t addr)
4484{
4485 uint64_t tcr = regime_tcr(env, mmu_idx)->raw_tcr;
4486 int tbi = aa64_va_parameter_tbi(tcr, mmu_idx);
4487 int select = extract64(addr, 55, 1);
4488
4489 return (tbi >> select) & 1 ? 56 : 64;
4490}
4491
4492static int vae1_tlbbits(CPUARMState *env, uint64_t addr)
4493{
b6ad6062 4494 uint64_t hcr = arm_hcr_el2_eff(env);
ea04dce7
RH
4495 ARMMMUIdx mmu_idx;
4496
4497 /* Only the regime of the mmu_idx below is significant. */
b6ad6062 4498 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
ea04dce7
RH
4499 mmu_idx = ARMMMUIdx_E20_0;
4500 } else {
4501 mmu_idx = ARMMMUIdx_E10_0;
4502 }
b6ad6062
RDC
4503
4504 if (arm_is_secure_below_el3(env)) {
4505 mmu_idx &= ~ARM_MMU_IDX_A_NS;
4506 }
4507
ea04dce7
RH
4508 return tlbbits_for_regime(env, mmu_idx, addr);
4509}
4510
fd3ed969
PM
4511static void tlbi_aa64_vmalle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4512 uint64_t value)
168aa23b 4513{
29a0af61 4514 CPUState *cs = env_cpu(env);
b7e0730d 4515 int mask = vae1_tlbmask(env);
dbb1fb27 4516
b7e0730d 4517 tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
168aa23b
PM
4518}
4519
b4ab8ce9
PM
4520static void tlbi_aa64_vmalle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4521 uint64_t value)
4522{
29a0af61 4523 CPUState *cs = env_cpu(env);
b7e0730d 4524 int mask = vae1_tlbmask(env);
b4ab8ce9
PM
4525
4526 if (tlb_force_broadcast(env)) {
527db2be
RH
4527 tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
4528 } else {
4529 tlb_flush_by_mmuidx(cs, mask);
b4ab8ce9 4530 }
b4ab8ce9
PM
4531}
4532
90c19cdf 4533static int alle1_tlbmask(CPUARMState *env)
168aa23b 4534{
90c19cdf
RH
4535 /*
4536 * Note that the 'ALL' scope must invalidate both stage 1 and
fd3ed969
PM
4537 * stage 2 translations, whereas most other scopes only invalidate
4538 * stage 1 translations.
4539 */
fd3ed969 4540 if (arm_is_secure_below_el3(env)) {
452ef8cb
RH
4541 return ARMMMUIdxBit_SE10_1 |
4542 ARMMMUIdxBit_SE10_1_PAN |
4543 ARMMMUIdxBit_SE10_0;
fd3ed969 4544 } else {
452ef8cb
RH
4545 return ARMMMUIdxBit_E10_1 |
4546 ARMMMUIdxBit_E10_1_PAN |
4547 ARMMMUIdxBit_E10_0;
fd3ed969 4548 }
168aa23b
PM
4549}
4550
85d0dc9f
RH
4551static int e2_tlbmask(CPUARMState *env)
4552{
b6ad6062
RDC
4553 if (arm_is_secure_below_el3(env)) {
4554 return ARMMMUIdxBit_SE20_0 |
4555 ARMMMUIdxBit_SE20_2 |
4556 ARMMMUIdxBit_SE20_2_PAN |
4557 ARMMMUIdxBit_SE2;
4558 } else {
4559 return ARMMMUIdxBit_E20_0 |
4560 ARMMMUIdxBit_E20_2 |
4561 ARMMMUIdxBit_E20_2_PAN |
4562 ARMMMUIdxBit_E2;
4563 }
85d0dc9f
RH
4564}
4565
90c19cdf
RH
4566static void tlbi_aa64_alle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4567 uint64_t value)
4568{
4569 CPUState *cs = env_cpu(env);
4570 int mask = alle1_tlbmask(env);
4571
4572 tlb_flush_by_mmuidx(cs, mask);
4573}
4574
fd3ed969 4575static void tlbi_aa64_alle2_write(CPUARMState *env, const ARMCPRegInfo *ri,
fa439fc5
PM
4576 uint64_t value)
4577{
85d0dc9f
RH
4578 CPUState *cs = env_cpu(env);
4579 int mask = e2_tlbmask(env);
fd3ed969 4580
85d0dc9f 4581 tlb_flush_by_mmuidx(cs, mask);
fd3ed969
PM
4582}
4583
43efaa33
PM
4584static void tlbi_aa64_alle3_write(CPUARMState *env, const ARMCPRegInfo *ri,
4585 uint64_t value)
4586{
2fc0cc0e 4587 ARMCPU *cpu = env_archcpu(env);
43efaa33
PM
4588 CPUState *cs = CPU(cpu);
4589
127b2b08 4590 tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_SE3);
43efaa33
PM
4591}
4592
fd3ed969
PM
4593static void tlbi_aa64_alle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4594 uint64_t value)
4595{
29a0af61 4596 CPUState *cs = env_cpu(env);
90c19cdf
RH
4597 int mask = alle1_tlbmask(env);
4598
4599 tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
fa439fc5
PM
4600}
4601
2bfb9d75
PM
4602static void tlbi_aa64_alle2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4603 uint64_t value)
4604{
29a0af61 4605 CPUState *cs = env_cpu(env);
85d0dc9f 4606 int mask = e2_tlbmask(env);
2bfb9d75 4607
85d0dc9f 4608 tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
2bfb9d75
PM
4609}
4610
43efaa33
PM
4611static void tlbi_aa64_alle3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4612 uint64_t value)
4613{
29a0af61 4614 CPUState *cs = env_cpu(env);
43efaa33 4615
127b2b08 4616 tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_SE3);
43efaa33
PM
4617}
4618
fd3ed969
PM
4619static void tlbi_aa64_vae2_write(CPUARMState *env, const ARMCPRegInfo *ri,
4620 uint64_t value)
fa439fc5 4621{
fd3ed969
PM
4622 /* Invalidate by VA, EL2
4623 * Currently handles both VAE2 and VALE2, since we don't support
4624 * flush-last-level-only.
4625 */
85d0dc9f
RH
4626 CPUState *cs = env_cpu(env);
4627 int mask = e2_tlbmask(env);
fd3ed969
PM
4628 uint64_t pageaddr = sextract64(value << 12, 0, 56);
4629
85d0dc9f 4630 tlb_flush_page_by_mmuidx(cs, pageaddr, mask);
fd3ed969
PM
4631}
4632
43efaa33
PM
4633static void tlbi_aa64_vae3_write(CPUARMState *env, const ARMCPRegInfo *ri,
4634 uint64_t value)
4635{
4636 /* Invalidate by VA, EL3
4637 * Currently handles both VAE3 and VALE3, since we don't support
4638 * flush-last-level-only.
4639 */
2fc0cc0e 4640 ARMCPU *cpu = env_archcpu(env);
43efaa33
PM
4641 CPUState *cs = CPU(cpu);
4642 uint64_t pageaddr = sextract64(value << 12, 0, 56);
4643
127b2b08 4644 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_SE3);
43efaa33
PM
4645}
4646
fd3ed969
PM
4647static void tlbi_aa64_vae1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4648 uint64_t value)
4649{
90c19cdf
RH
4650 CPUState *cs = env_cpu(env);
4651 int mask = vae1_tlbmask(env);
fa439fc5 4652 uint64_t pageaddr = sextract64(value << 12, 0, 56);
ea04dce7 4653 int bits = vae1_tlbbits(env, pageaddr);
fa439fc5 4654
ea04dce7 4655 tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr, mask, bits);
fa439fc5
PM
4656}
4657
b4ab8ce9
PM
4658static void tlbi_aa64_vae1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4659 uint64_t value)
4660{
4661 /* Invalidate by VA, EL1&0 (AArch64 version).
4662 * Currently handles all of VAE1, VAAE1, VAALE1 and VALE1,
4663 * since we don't support flush-for-specific-ASID-only or
4664 * flush-last-level-only.
4665 */
90c19cdf
RH
4666 CPUState *cs = env_cpu(env);
4667 int mask = vae1_tlbmask(env);
b4ab8ce9 4668 uint64_t pageaddr = sextract64(value << 12, 0, 56);
ea04dce7 4669 int bits = vae1_tlbbits(env, pageaddr);
b4ab8ce9
PM
4670
4671 if (tlb_force_broadcast(env)) {
ea04dce7 4672 tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr, mask, bits);
527db2be 4673 } else {
ea04dce7 4674 tlb_flush_page_bits_by_mmuidx(cs, pageaddr, mask, bits);
b4ab8ce9 4675 }
b4ab8ce9
PM
4676}
4677
fd3ed969
PM
4678static void tlbi_aa64_vae2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4679 uint64_t value)
fa439fc5 4680{
29a0af61 4681 CPUState *cs = env_cpu(env);
fd3ed969 4682 uint64_t pageaddr = sextract64(value << 12, 0, 56);
b6ad6062
RDC
4683 bool secure = arm_is_secure_below_el3(env);
4684 int mask = secure ? ARMMMUIdxBit_SE2 : ARMMMUIdxBit_E2;
4685 int bits = tlbbits_for_regime(env, secure ? ARMMMUIdx_E2 : ARMMMUIdx_SE2,
4686 pageaddr);
fa439fc5 4687
b6ad6062 4688 tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr, mask, bits);
fa439fc5
PM
4689}
4690
43efaa33
PM
4691static void tlbi_aa64_vae3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4692 uint64_t value)
4693{
29a0af61 4694 CPUState *cs = env_cpu(env);
43efaa33 4695 uint64_t pageaddr = sextract64(value << 12, 0, 56);
ea04dce7 4696 int bits = tlbbits_for_regime(env, ARMMMUIdx_SE3, pageaddr);
43efaa33 4697
ea04dce7
RH
4698 tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr,
4699 ARMMMUIdxBit_SE3, bits);
43efaa33
PM
4700}
4701
3f208fd7
PM
4702static CPAccessResult aa64_zva_access(CPUARMState *env, const ARMCPRegInfo *ri,
4703 bool isread)
aca3f40b 4704{
4351cb72
RH
4705 int cur_el = arm_current_el(env);
4706
4707 if (cur_el < 2) {
4708 uint64_t hcr = arm_hcr_el2_eff(env);
4709
4710 if (cur_el == 0) {
4711 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
4712 if (!(env->cp15.sctlr_el[2] & SCTLR_DZE)) {
4713 return CP_ACCESS_TRAP_EL2;
4714 }
4715 } else {
4716 if (!(env->cp15.sctlr_el[1] & SCTLR_DZE)) {
4717 return CP_ACCESS_TRAP;
4718 }
4719 if (hcr & HCR_TDZ) {
4720 return CP_ACCESS_TRAP_EL2;
4721 }
4722 }
4723 } else if (hcr & HCR_TDZ) {
4724 return CP_ACCESS_TRAP_EL2;
4725 }
aca3f40b
PM
4726 }
4727 return CP_ACCESS_OK;
4728}
4729
4730static uint64_t aa64_dczid_read(CPUARMState *env, const ARMCPRegInfo *ri)
4731{
2fc0cc0e 4732 ARMCPU *cpu = env_archcpu(env);
aca3f40b
PM
4733 int dzp_bit = 1 << 4;
4734
4735 /* DZP indicates whether DC ZVA access is allowed */
3f208fd7 4736 if (aa64_zva_access(env, NULL, false) == CP_ACCESS_OK) {
aca3f40b
PM
4737 dzp_bit = 0;
4738 }
4739 return cpu->dcz_blocksize | dzp_bit;
4740}
4741
3f208fd7
PM
4742static CPAccessResult sp_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
4743 bool isread)
f502cfc2 4744{
cdcf1405 4745 if (!(env->pstate & PSTATE_SP)) {
f502cfc2
PM
4746 /* Access to SP_EL0 is undefined if it's being used as
4747 * the stack pointer.
4748 */
4749 return CP_ACCESS_TRAP_UNCATEGORIZED;
4750 }
4751 return CP_ACCESS_OK;
4752}
4753
4754static uint64_t spsel_read(CPUARMState *env, const ARMCPRegInfo *ri)
4755{
4756 return env->pstate & PSTATE_SP;
4757}
4758
4759static void spsel_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
4760{
4761 update_spsel(env, val);
4762}
4763
137feaa9
FA
4764static void sctlr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4765 uint64_t value)
4766{
2fc0cc0e 4767 ARMCPU *cpu = env_archcpu(env);
137feaa9 4768
f00faf13
RH
4769 if (arm_feature(env, ARM_FEATURE_PMSA) && !cpu->has_mpu) {
4770 /* M bit is RAZ/WI for PMSA with no MPU implemented */
4771 value &= ~SCTLR_M;
4772 }
4773
4774 /* ??? Lots of these bits are not implemented. */
4775
4776 if (ri->state == ARM_CP_STATE_AA64 && !cpu_isar_feature(aa64_mte, cpu)) {
4777 if (ri->opc1 == 6) { /* SCTLR_EL3 */
4778 value &= ~(SCTLR_ITFSB | SCTLR_TCF | SCTLR_ATA);
4779 } else {
4780 value &= ~(SCTLR_ITFSB | SCTLR_TCF0 | SCTLR_TCF |
4781 SCTLR_ATA0 | SCTLR_ATA);
4782 }
4783 }
4784
137feaa9
FA
4785 if (raw_read(env, ri) == value) {
4786 /* Skip the TLB flush if nothing actually changed; Linux likes
4787 * to do a lot of pointless SCTLR writes.
4788 */
4789 return;
4790 }
4791
4792 raw_write(env, ri, value);
f00faf13 4793
137feaa9 4794 /* This may enable/disable the MMU, so do a TLB flush. */
d10eb08f 4795 tlb_flush(CPU(cpu));
2e5dcf36
RH
4796
4797 if (ri->type & ARM_CP_SUPPRESS_TB_END) {
4798 /*
4799 * Normally we would always end the TB on an SCTLR write; see the
4800 * comment in ARMCPRegInfo sctlr initialization below for why Xscale
4801 * is special. Setting ARM_CP_SUPPRESS_TB_END also stops the rebuild
4802 * of hflags from the translator, so do it here.
4803 */
4804 arm_rebuild_hflags(env);
4805 }
137feaa9
FA
4806}
4807
3f208fd7
PM
4808static CPAccessResult fpexc32_access(CPUARMState *env, const ARMCPRegInfo *ri,
4809 bool isread)
03fbf20f
PM
4810{
4811 if ((env->cp15.cptr_el[2] & CPTR_TFP) && arm_current_el(env) == 2) {
f2cae609 4812 return CP_ACCESS_TRAP_FP_EL2;
03fbf20f
PM
4813 }
4814 if (env->cp15.cptr_el[3] & CPTR_TFP) {
f2cae609 4815 return CP_ACCESS_TRAP_FP_EL3;
03fbf20f
PM
4816 }
4817 return CP_ACCESS_OK;
4818}
4819
a8d64e73
PM
4820static void sdcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4821 uint64_t value)
4822{
4823 env->cp15.mdcr_el3 = value & SDCR_VALID_MASK;
4824}
4825
b0d2b7d0
PM
4826static const ARMCPRegInfo v8_cp_reginfo[] = {
4827 /* Minimal set of EL0-visible registers. This will need to be expanded
4828 * significantly for system emulation of AArch64 CPUs.
4829 */
4830 { .name = "NZCV", .state = ARM_CP_STATE_AA64,
4831 .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 2,
4832 .access = PL0_RW, .type = ARM_CP_NZCV },
c2b820fe
PM
4833 { .name = "DAIF", .state = ARM_CP_STATE_AA64,
4834 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 2,
7a0e58fa 4835 .type = ARM_CP_NO_RAW,
c2b820fe
PM
4836 .access = PL0_RW, .accessfn = aa64_daif_access,
4837 .fieldoffset = offsetof(CPUARMState, daif),
4838 .writefn = aa64_daif_write, .resetfn = arm_cp_reset_ignore },
b0d2b7d0
PM
4839 { .name = "FPCR", .state = ARM_CP_STATE_AA64,
4840 .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 4,
b916c9c3 4841 .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END,
fe03d45f 4842 .readfn = aa64_fpcr_read, .writefn = aa64_fpcr_write },
b0d2b7d0
PM
4843 { .name = "FPSR", .state = ARM_CP_STATE_AA64,
4844 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 4,
b916c9c3 4845 .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END,
fe03d45f 4846 .readfn = aa64_fpsr_read, .writefn = aa64_fpsr_write },
b0d2b7d0
PM
4847 { .name = "DCZID_EL0", .state = ARM_CP_STATE_AA64,
4848 .opc0 = 3, .opc1 = 3, .opc2 = 7, .crn = 0, .crm = 0,
7a0e58fa 4849 .access = PL0_R, .type = ARM_CP_NO_RAW,
aca3f40b
PM
4850 .readfn = aa64_dczid_read },
4851 { .name = "DC_ZVA", .state = ARM_CP_STATE_AA64,
4852 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 1,
4853 .access = PL0_W, .type = ARM_CP_DC_ZVA,
4854#ifndef CONFIG_USER_ONLY
4855 /* Avoid overhead of an access check that always passes in user-mode */
4856 .accessfn = aa64_zva_access,
4857#endif
4858 },
0eef9d98
PM
4859 { .name = "CURRENTEL", .state = ARM_CP_STATE_AA64,
4860 .opc0 = 3, .opc1 = 0, .opc2 = 2, .crn = 4, .crm = 2,
4861 .access = PL1_R, .type = ARM_CP_CURRENTEL },
8af35c37
PM
4862 /* Cache ops: all NOPs since we don't emulate caches */
4863 { .name = "IC_IALLUIS", .state = ARM_CP_STATE_AA64,
4864 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
38262d8a
RH
4865 .access = PL1_W, .type = ARM_CP_NOP,
4866 .accessfn = aa64_cacheop_pou_access },
8af35c37
PM
4867 { .name = "IC_IALLU", .state = ARM_CP_STATE_AA64,
4868 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
38262d8a
RH
4869 .access = PL1_W, .type = ARM_CP_NOP,
4870 .accessfn = aa64_cacheop_pou_access },
8af35c37
PM
4871 { .name = "IC_IVAU", .state = ARM_CP_STATE_AA64,
4872 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 5, .opc2 = 1,
4873 .access = PL0_W, .type = ARM_CP_NOP,
38262d8a 4874 .accessfn = aa64_cacheop_pou_access },
8af35c37
PM
4875 { .name = "DC_IVAC", .state = ARM_CP_STATE_AA64,
4876 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
1bed4d2e
RH
4877 .access = PL1_W, .accessfn = aa64_cacheop_poc_access,
4878 .type = ARM_CP_NOP },
8af35c37
PM
4879 { .name = "DC_ISW", .state = ARM_CP_STATE_AA64,
4880 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
1803d271 4881 .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
8af35c37
PM
4882 { .name = "DC_CVAC", .state = ARM_CP_STATE_AA64,
4883 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 1,
4884 .access = PL0_W, .type = ARM_CP_NOP,
1bed4d2e 4885 .accessfn = aa64_cacheop_poc_access },
8af35c37
PM
4886 { .name = "DC_CSW", .state = ARM_CP_STATE_AA64,
4887 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
1803d271 4888 .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
8af35c37
PM
4889 { .name = "DC_CVAU", .state = ARM_CP_STATE_AA64,
4890 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 11, .opc2 = 1,
4891 .access = PL0_W, .type = ARM_CP_NOP,
38262d8a 4892 .accessfn = aa64_cacheop_pou_access },
8af35c37
PM
4893 { .name = "DC_CIVAC", .state = ARM_CP_STATE_AA64,
4894 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 1,
4895 .access = PL0_W, .type = ARM_CP_NOP,
1bed4d2e 4896 .accessfn = aa64_cacheop_poc_access },
8af35c37
PM
4897 { .name = "DC_CISW", .state = ARM_CP_STATE_AA64,
4898 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
1803d271 4899 .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
168aa23b
PM
4900 /* TLBI operations */
4901 { .name = "TLBI_VMALLE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 4902 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
30881b73 4903 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
fd3ed969 4904 .writefn = tlbi_aa64_vmalle1is_write },
168aa23b 4905 { .name = "TLBI_VAE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 4906 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
30881b73 4907 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
fd3ed969 4908 .writefn = tlbi_aa64_vae1is_write },
168aa23b 4909 { .name = "TLBI_ASIDE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 4910 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
30881b73 4911 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
fd3ed969 4912 .writefn = tlbi_aa64_vmalle1is_write },
168aa23b 4913 { .name = "TLBI_VAAE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 4914 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
30881b73 4915 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
fd3ed969 4916 .writefn = tlbi_aa64_vae1is_write },
168aa23b 4917 { .name = "TLBI_VALE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 4918 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
30881b73 4919 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
fd3ed969 4920 .writefn = tlbi_aa64_vae1is_write },
168aa23b 4921 { .name = "TLBI_VAALE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 4922 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
30881b73 4923 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
fd3ed969 4924 .writefn = tlbi_aa64_vae1is_write },
168aa23b 4925 { .name = "TLBI_VMALLE1", .state = ARM_CP_STATE_AA64,
6ab9f499 4926 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
30881b73 4927 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
fd3ed969 4928 .writefn = tlbi_aa64_vmalle1_write },
168aa23b 4929 { .name = "TLBI_VAE1", .state = ARM_CP_STATE_AA64,
6ab9f499 4930 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
30881b73 4931 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
fd3ed969 4932 .writefn = tlbi_aa64_vae1_write },
168aa23b 4933 { .name = "TLBI_ASIDE1", .state = ARM_CP_STATE_AA64,
6ab9f499 4934 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
30881b73 4935 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
fd3ed969 4936 .writefn = tlbi_aa64_vmalle1_write },
168aa23b 4937 { .name = "TLBI_VAAE1", .state = ARM_CP_STATE_AA64,
6ab9f499 4938 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
30881b73 4939 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
fd3ed969 4940 .writefn = tlbi_aa64_vae1_write },
168aa23b 4941 { .name = "TLBI_VALE1", .state = ARM_CP_STATE_AA64,
6ab9f499 4942 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
30881b73 4943 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
fd3ed969 4944 .writefn = tlbi_aa64_vae1_write },
168aa23b 4945 { .name = "TLBI_VAALE1", .state = ARM_CP_STATE_AA64,
6ab9f499 4946 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
30881b73 4947 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
fd3ed969 4948 .writefn = tlbi_aa64_vae1_write },
cea66e91
PM
4949 { .name = "TLBI_IPAS2E1IS", .state = ARM_CP_STATE_AA64,
4950 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1,
bf05340c 4951 .access = PL2_W, .type = ARM_CP_NOP },
cea66e91
PM
4952 { .name = "TLBI_IPAS2LE1IS", .state = ARM_CP_STATE_AA64,
4953 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5,
bf05340c 4954 .access = PL2_W, .type = ARM_CP_NOP },
83ddf975
PM
4955 { .name = "TLBI_ALLE1IS", .state = ARM_CP_STATE_AA64,
4956 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
4957 .access = PL2_W, .type = ARM_CP_NO_RAW,
fd3ed969 4958 .writefn = tlbi_aa64_alle1is_write },
43efaa33
PM
4959 { .name = "TLBI_VMALLS12E1IS", .state = ARM_CP_STATE_AA64,
4960 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 6,
4961 .access = PL2_W, .type = ARM_CP_NO_RAW,
4962 .writefn = tlbi_aa64_alle1is_write },
cea66e91
PM
4963 { .name = "TLBI_IPAS2E1", .state = ARM_CP_STATE_AA64,
4964 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1,
bf05340c 4965 .access = PL2_W, .type = ARM_CP_NOP },
cea66e91
PM
4966 { .name = "TLBI_IPAS2LE1", .state = ARM_CP_STATE_AA64,
4967 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5,
bf05340c 4968 .access = PL2_W, .type = ARM_CP_NOP },
83ddf975
PM
4969 { .name = "TLBI_ALLE1", .state = ARM_CP_STATE_AA64,
4970 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
4971 .access = PL2_W, .type = ARM_CP_NO_RAW,
fd3ed969 4972 .writefn = tlbi_aa64_alle1_write },
43efaa33
PM
4973 { .name = "TLBI_VMALLS12E1", .state = ARM_CP_STATE_AA64,
4974 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 6,
4975 .access = PL2_W, .type = ARM_CP_NO_RAW,
4976 .writefn = tlbi_aa64_alle1is_write },
19525524
PM
4977#ifndef CONFIG_USER_ONLY
4978 /* 64 bit address translation operations */
4979 { .name = "AT_S1E1R", .state = ARM_CP_STATE_AA64,
4980 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 0,
0710b2fa
PM
4981 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4982 .writefn = ats_write64 },
19525524
PM
4983 { .name = "AT_S1E1W", .state = ARM_CP_STATE_AA64,
4984 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 1,
0710b2fa
PM
4985 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4986 .writefn = ats_write64 },
19525524
PM
4987 { .name = "AT_S1E0R", .state = ARM_CP_STATE_AA64,
4988 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 2,
0710b2fa
PM
4989 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4990 .writefn = ats_write64 },
19525524
PM
4991 { .name = "AT_S1E0W", .state = ARM_CP_STATE_AA64,
4992 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 3,
0710b2fa
PM
4993 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4994 .writefn = ats_write64 },
2a47df95 4995 { .name = "AT_S12E1R", .state = ARM_CP_STATE_AA64,
7a379c7e 4996 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 4,
0710b2fa
PM
4997 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4998 .writefn = ats_write64 },
2a47df95 4999 { .name = "AT_S12E1W", .state = ARM_CP_STATE_AA64,
7a379c7e 5000 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 5,
0710b2fa
PM
5001 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5002 .writefn = ats_write64 },
2a47df95 5003 { .name = "AT_S12E0R", .state = ARM_CP_STATE_AA64,
7a379c7e 5004 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 6,
0710b2fa
PM
5005 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5006 .writefn = ats_write64 },
2a47df95 5007 { .name = "AT_S12E0W", .state = ARM_CP_STATE_AA64,
7a379c7e 5008 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 7,
0710b2fa
PM
5009 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5010 .writefn = ats_write64 },
2a47df95
PM
5011 /* AT S1E2* are elsewhere as they UNDEF from EL3 if EL2 is not present */
5012 { .name = "AT_S1E3R", .state = ARM_CP_STATE_AA64,
5013 .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 0,
0710b2fa
PM
5014 .access = PL3_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5015 .writefn = ats_write64 },
2a47df95
PM
5016 { .name = "AT_S1E3W", .state = ARM_CP_STATE_AA64,
5017 .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 1,
0710b2fa
PM
5018 .access = PL3_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5019 .writefn = ats_write64 },
c96fc9b5
EI
5020 { .name = "PAR_EL1", .state = ARM_CP_STATE_AA64,
5021 .type = ARM_CP_ALIAS,
5022 .opc0 = 3, .opc1 = 0, .crn = 7, .crm = 4, .opc2 = 0,
5023 .access = PL1_RW, .resetvalue = 0,
5024 .fieldoffset = offsetof(CPUARMState, cp15.par_el[1]),
5025 .writefn = par_write },
19525524 5026#endif
995939a6 5027 /* TLB invalidate last level of translation table walk */
9449fdf6 5028 { .name = "TLBIMVALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
30881b73
RH
5029 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
5030 .writefn = tlbimva_is_write },
9449fdf6 5031 { .name = "TLBIMVAALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
30881b73 5032 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
fa439fc5 5033 .writefn = tlbimvaa_is_write },
9449fdf6 5034 { .name = "TLBIMVAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
30881b73
RH
5035 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
5036 .writefn = tlbimva_write },
9449fdf6 5037 { .name = "TLBIMVAAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
30881b73
RH
5038 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
5039 .writefn = tlbimvaa_write },
541ef8c2
SS
5040 { .name = "TLBIMVALH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
5041 .type = ARM_CP_NO_RAW, .access = PL2_W,
5042 .writefn = tlbimva_hyp_write },
5043 { .name = "TLBIMVALHIS",
5044 .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
5045 .type = ARM_CP_NO_RAW, .access = PL2_W,
5046 .writefn = tlbimva_hyp_is_write },
5047 { .name = "TLBIIPAS2",
5048 .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1,
bf05340c 5049 .type = ARM_CP_NOP, .access = PL2_W },
541ef8c2
SS
5050 { .name = "TLBIIPAS2IS",
5051 .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1,
bf05340c 5052 .type = ARM_CP_NOP, .access = PL2_W },
541ef8c2
SS
5053 { .name = "TLBIIPAS2L",
5054 .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5,
bf05340c 5055 .type = ARM_CP_NOP, .access = PL2_W },
541ef8c2
SS
5056 { .name = "TLBIIPAS2LIS",
5057 .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5,
bf05340c 5058 .type = ARM_CP_NOP, .access = PL2_W },
9449fdf6
PM
5059 /* 32 bit cache operations */
5060 { .name = "ICIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
38262d8a 5061 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_pou_access },
9449fdf6
PM
5062 { .name = "BPIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 6,
5063 .type = ARM_CP_NOP, .access = PL1_W },
5064 { .name = "ICIALLU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
38262d8a 5065 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_pou_access },
9449fdf6 5066 { .name = "ICIMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 1,
38262d8a 5067 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_pou_access },
9449fdf6
PM
5068 { .name = "BPIALL", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 6,
5069 .type = ARM_CP_NOP, .access = PL1_W },
5070 { .name = "BPIMVA", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 7,
5071 .type = ARM_CP_NOP, .access = PL1_W },
5072 { .name = "DCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
1bed4d2e 5073 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
9449fdf6 5074 { .name = "DCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
1803d271 5075 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
9449fdf6 5076 { .name = "DCCMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 1,
1bed4d2e 5077 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
9449fdf6 5078 { .name = "DCCSW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
1803d271 5079 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
9449fdf6 5080 { .name = "DCCMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 11, .opc2 = 1,
38262d8a 5081 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_pou_access },
9449fdf6 5082 { .name = "DCCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 1,
1bed4d2e 5083 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
9449fdf6 5084 { .name = "DCCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
1803d271 5085 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
9449fdf6 5086 /* MMU Domain access control / MPU write buffer control */
0c17d68c 5087 { .name = "DACR", .cp = 15, .opc1 = 0, .crn = 3, .crm = 0, .opc2 = 0,
84929218 5088 .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
0c17d68c
FA
5089 .writefn = dacr_write, .raw_writefn = raw_write,
5090 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
5091 offsetoflow32(CPUARMState, cp15.dacr_ns) } },
a0618a19 5092 { .name = "ELR_EL1", .state = ARM_CP_STATE_AA64,
7a0e58fa 5093 .type = ARM_CP_ALIAS,
a0618a19 5094 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 1,
6947f059
EI
5095 .access = PL1_RW,
5096 .fieldoffset = offsetof(CPUARMState, elr_el[1]) },
a65f1de9 5097 { .name = "SPSR_EL1", .state = ARM_CP_STATE_AA64,
7a0e58fa 5098 .type = ARM_CP_ALIAS,
a65f1de9 5099 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 0,
99a99c1f
SB
5100 .access = PL1_RW,
5101 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_SVC]) },
f502cfc2
PM
5102 /* We rely on the access checks not allowing the guest to write to the
5103 * state field when SPSel indicates that it's being used as the stack
5104 * pointer.
5105 */
5106 { .name = "SP_EL0", .state = ARM_CP_STATE_AA64,
5107 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 1, .opc2 = 0,
5108 .access = PL1_RW, .accessfn = sp_el0_access,
7a0e58fa 5109 .type = ARM_CP_ALIAS,
f502cfc2 5110 .fieldoffset = offsetof(CPUARMState, sp_el[0]) },
884b4dee
GB
5111 { .name = "SP_EL1", .state = ARM_CP_STATE_AA64,
5112 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 1, .opc2 = 0,
7a0e58fa 5113 .access = PL2_RW, .type = ARM_CP_ALIAS,
884b4dee 5114 .fieldoffset = offsetof(CPUARMState, sp_el[1]) },
f502cfc2
PM
5115 { .name = "SPSel", .state = ARM_CP_STATE_AA64,
5116 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 0,
7a0e58fa 5117 .type = ARM_CP_NO_RAW,
f502cfc2 5118 .access = PL1_RW, .readfn = spsel_read, .writefn = spsel_write },
03fbf20f
PM
5119 { .name = "FPEXC32_EL2", .state = ARM_CP_STATE_AA64,
5120 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 3, .opc2 = 0,
5121 .type = ARM_CP_ALIAS,
5122 .fieldoffset = offsetof(CPUARMState, vfp.xregs[ARM_VFP_FPEXC]),
5123 .access = PL2_RW, .accessfn = fpexc32_access },
6a43e0b6
PM
5124 { .name = "DACR32_EL2", .state = ARM_CP_STATE_AA64,
5125 .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 0, .opc2 = 0,
5126 .access = PL2_RW, .resetvalue = 0,
5127 .writefn = dacr_write, .raw_writefn = raw_write,
5128 .fieldoffset = offsetof(CPUARMState, cp15.dacr32_el2) },
5129 { .name = "IFSR32_EL2", .state = ARM_CP_STATE_AA64,
5130 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 0, .opc2 = 1,
5131 .access = PL2_RW, .resetvalue = 0,
5132 .fieldoffset = offsetof(CPUARMState, cp15.ifsr32_el2) },
5133 { .name = "SPSR_IRQ", .state = ARM_CP_STATE_AA64,
5134 .type = ARM_CP_ALIAS,
5135 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 0,
5136 .access = PL2_RW,
5137 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_IRQ]) },
5138 { .name = "SPSR_ABT", .state = ARM_CP_STATE_AA64,
5139 .type = ARM_CP_ALIAS,
5140 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 1,
5141 .access = PL2_RW,
5142 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_ABT]) },
5143 { .name = "SPSR_UND", .state = ARM_CP_STATE_AA64,
5144 .type = ARM_CP_ALIAS,
5145 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 2,
5146 .access = PL2_RW,
5147 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_UND]) },
5148 { .name = "SPSR_FIQ", .state = ARM_CP_STATE_AA64,
5149 .type = ARM_CP_ALIAS,
5150 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 3,
5151 .access = PL2_RW,
5152 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_FIQ]) },
a8d64e73
PM
5153 { .name = "MDCR_EL3", .state = ARM_CP_STATE_AA64,
5154 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 3, .opc2 = 1,
5155 .resetvalue = 0,
5156 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el3) },
5157 { .name = "SDCR", .type = ARM_CP_ALIAS,
5158 .cp = 15, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 1,
5159 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
5160 .writefn = sdcr_write,
5161 .fieldoffset = offsetoflow32(CPUARMState, cp15.mdcr_el3) },
b0d2b7d0
PM
5162 REGINFO_SENTINEL
5163};
5164
d42e3c26 5165/* Used to describe the behaviour of EL2 regs when EL2 does not exist. */
4771cd01 5166static const ARMCPRegInfo el3_no_el2_cp_reginfo[] = {
d79e0c06 5167 { .name = "VBAR_EL2", .state = ARM_CP_STATE_BOTH,
d42e3c26
EI
5168 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0,
5169 .access = PL2_RW,
5170 .readfn = arm_cp_read_zero, .writefn = arm_cp_write_ignore },
ce4afed8 5171 { .name = "HCR_EL2", .state = ARM_CP_STATE_BOTH,
f149e3e8
EI
5172 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
5173 .access = PL2_RW,
ce4afed8 5174 .type = ARM_CP_CONST, .resetvalue = 0 },
831a2fca
PM
5175 { .name = "HACR_EL2", .state = ARM_CP_STATE_BOTH,
5176 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 7,
5177 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
68e78e33
PM
5178 { .name = "ESR_EL2", .state = ARM_CP_STATE_BOTH,
5179 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 0,
5180 .access = PL2_RW,
5181 .type = ARM_CP_CONST, .resetvalue = 0 },
c6f19164
GB
5182 { .name = "CPTR_EL2", .state = ARM_CP_STATE_BOTH,
5183 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 2,
5184 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
95f949ac
EI
5185 { .name = "MAIR_EL2", .state = ARM_CP_STATE_BOTH,
5186 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 0,
5187 .access = PL2_RW, .type = ARM_CP_CONST,
5188 .resetvalue = 0 },
5189 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
b5ede85b 5190 .cp = 15, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 1,
95f949ac 5191 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
2179ef95
PM
5192 { .name = "AMAIR_EL2", .state = ARM_CP_STATE_BOTH,
5193 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 0,
5194 .access = PL2_RW, .type = ARM_CP_CONST,
5195 .resetvalue = 0 },
55b53c71 5196 { .name = "HAMAIR1", .state = ARM_CP_STATE_AA32,
b5ede85b 5197 .cp = 15, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 1,
2179ef95
PM
5198 .access = PL2_RW, .type = ARM_CP_CONST,
5199 .resetvalue = 0 },
37cd6c24
PM
5200 { .name = "AFSR0_EL2", .state = ARM_CP_STATE_BOTH,
5201 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 0,
5202 .access = PL2_RW, .type = ARM_CP_CONST,
5203 .resetvalue = 0 },
5204 { .name = "AFSR1_EL2", .state = ARM_CP_STATE_BOTH,
5205 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 1,
5206 .access = PL2_RW, .type = ARM_CP_CONST,
5207 .resetvalue = 0 },
06ec4c8c
EI
5208 { .name = "TCR_EL2", .state = ARM_CP_STATE_BOTH,
5209 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 2,
5210 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
68e9c2fe
EI
5211 { .name = "VTCR_EL2", .state = ARM_CP_STATE_BOTH,
5212 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
93dd1e61 5213 .access = PL2_RW, .accessfn = access_el3_aa32ns,
68e9c2fe 5214 .type = ARM_CP_CONST, .resetvalue = 0 },
b698e9cf
EI
5215 { .name = "VTTBR", .state = ARM_CP_STATE_AA32,
5216 .cp = 15, .opc1 = 6, .crm = 2,
5217 .access = PL2_RW, .accessfn = access_el3_aa32ns,
5218 .type = ARM_CP_CONST | ARM_CP_64BIT, .resetvalue = 0 },
5219 { .name = "VTTBR_EL2", .state = ARM_CP_STATE_AA64,
5220 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 0,
5221 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
b9cb5323
EI
5222 { .name = "SCTLR_EL2", .state = ARM_CP_STATE_BOTH,
5223 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 0,
5224 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
ff05f37b
EI
5225 { .name = "TPIDR_EL2", .state = ARM_CP_STATE_BOTH,
5226 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 2,
5227 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
a57633c0
EI
5228 { .name = "TTBR0_EL2", .state = ARM_CP_STATE_AA64,
5229 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
5230 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
5231 { .name = "HTTBR", .cp = 15, .opc1 = 4, .crm = 2,
5232 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_CONST,
5233 .resetvalue = 0 },
0b6440af
EI
5234 { .name = "CNTHCTL_EL2", .state = ARM_CP_STATE_BOTH,
5235 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 1, .opc2 = 0,
5236 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
edac4d8a
EI
5237 { .name = "CNTVOFF_EL2", .state = ARM_CP_STATE_AA64,
5238 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 3,
5239 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
5240 { .name = "CNTVOFF", .cp = 15, .opc1 = 4, .crm = 14,
5241 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_CONST,
5242 .resetvalue = 0 },
b0e66d95
EI
5243 { .name = "CNTHP_CVAL_EL2", .state = ARM_CP_STATE_AA64,
5244 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 2,
5245 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
5246 { .name = "CNTHP_CVAL", .cp = 15, .opc1 = 6, .crm = 14,
5247 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_CONST,
5248 .resetvalue = 0 },
5249 { .name = "CNTHP_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
5250 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 0,
5251 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
5252 { .name = "CNTHP_CTL_EL2", .state = ARM_CP_STATE_BOTH,
5253 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 1,
5254 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
14cc7b54
SF
5255 { .name = "MDCR_EL2", .state = ARM_CP_STATE_BOTH,
5256 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 1,
d6c8cf81
PM
5257 .access = PL2_RW, .accessfn = access_tda,
5258 .type = ARM_CP_CONST, .resetvalue = 0 },
59e05530
EI
5259 { .name = "HPFAR_EL2", .state = ARM_CP_STATE_BOTH,
5260 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
93dd1e61 5261 .access = PL2_RW, .accessfn = access_el3_aa32ns,
59e05530 5262 .type = ARM_CP_CONST, .resetvalue = 0 },
2a5a9abd
AF
5263 { .name = "HSTR_EL2", .state = ARM_CP_STATE_BOTH,
5264 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 3,
5265 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
cba517c3
PM
5266 { .name = "FAR_EL2", .state = ARM_CP_STATE_BOTH,
5267 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 0,
5268 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
5269 { .name = "HIFAR", .state = ARM_CP_STATE_AA32,
5270 .type = ARM_CP_CONST,
5271 .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 2,
5272 .access = PL2_RW, .resetvalue = 0 },
d42e3c26
EI
5273 REGINFO_SENTINEL
5274};
5275
ce4afed8
PM
5276/* Ditto, but for registers which exist in ARMv8 but not v7 */
5277static const ARMCPRegInfo el3_no_el2_v8_cp_reginfo[] = {
5278 { .name = "HCR2", .state = ARM_CP_STATE_AA32,
5279 .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 4,
5280 .access = PL2_RW,
5281 .type = ARM_CP_CONST, .resetvalue = 0 },
5282 REGINFO_SENTINEL
5283};
5284
d1fb4da2 5285static void do_hcr_write(CPUARMState *env, uint64_t value, uint64_t valid_mask)
f149e3e8 5286{
2fc0cc0e 5287 ARMCPU *cpu = env_archcpu(env);
d1fb4da2
RH
5288
5289 if (arm_feature(env, ARM_FEATURE_V8)) {
5290 valid_mask |= MAKE_64BIT_MASK(0, 34); /* ARMv8.0 */
5291 } else {
5292 valid_mask |= MAKE_64BIT_MASK(0, 28); /* ARMv7VE */
5293 }
f149e3e8
EI
5294
5295 if (arm_feature(env, ARM_FEATURE_EL3)) {
5296 valid_mask &= ~HCR_HCD;
77077a83
JK
5297 } else if (cpu->psci_conduit != QEMU_PSCI_CONDUIT_SMC) {
5298 /* Architecturally HCR.TSC is RES0 if EL3 is not implemented.
5299 * However, if we're using the SMC PSCI conduit then QEMU is
5300 * effectively acting like EL3 firmware and so the guest at
5301 * EL2 should retain the ability to prevent EL1 from being
5302 * able to make SMC calls into the ersatz firmware, so in
5303 * that case HCR.TSC should be read/write.
5304 */
f149e3e8
EI
5305 valid_mask &= ~HCR_TSC;
5306 }
d1fb4da2
RH
5307
5308 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
5309 if (cpu_isar_feature(aa64_vh, cpu)) {
5310 valid_mask |= HCR_E2H;
5311 }
5312 if (cpu_isar_feature(aa64_lor, cpu)) {
5313 valid_mask |= HCR_TLOR;
5314 }
5315 if (cpu_isar_feature(aa64_pauth, cpu)) {
5316 valid_mask |= HCR_API | HCR_APK;
5317 }
8ddb300b
RH
5318 if (cpu_isar_feature(aa64_mte, cpu)) {
5319 valid_mask |= HCR_ATA | HCR_DCT | HCR_TID5;
5320 }
ef682cdb 5321 }
f149e3e8
EI
5322
5323 /* Clear RES0 bits. */
5324 value &= valid_mask;
5325
8ddb300b
RH
5326 /*
5327 * These bits change the MMU setup:
f149e3e8
EI
5328 * HCR_VM enables stage 2 translation
5329 * HCR_PTW forbids certain page-table setups
8ddb300b
RH
5330 * HCR_DC disables stage1 and enables stage2 translation
5331 * HCR_DCT enables tagging on (disabled) stage1 translation
f149e3e8 5332 */
8ddb300b 5333 if ((env->cp15.hcr_el2 ^ value) & (HCR_VM | HCR_PTW | HCR_DC | HCR_DCT)) {
d10eb08f 5334 tlb_flush(CPU(cpu));
f149e3e8 5335 }
ce4afed8 5336 env->cp15.hcr_el2 = value;
89430fc6
PM
5337
5338 /*
5339 * Updates to VI and VF require us to update the status of
5340 * virtual interrupts, which are the logical OR of these bits
5341 * and the state of the input lines from the GIC. (This requires
5342 * that we have the iothread lock, which is done by marking the
5343 * reginfo structs as ARM_CP_IO.)
5344 * Note that if a write to HCR pends a VIRQ or VFIQ it is never
5345 * possible for it to be taken immediately, because VIRQ and
5346 * VFIQ are masked unless running at EL0 or EL1, and HCR
5347 * can only be written at EL2.
5348 */
5349 g_assert(qemu_mutex_iothread_locked());
5350 arm_cpu_update_virq(cpu);
5351 arm_cpu_update_vfiq(cpu);
ce4afed8
PM
5352}
5353
d1fb4da2
RH
5354static void hcr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
5355{
5356 do_hcr_write(env, value, 0);
5357}
5358
ce4afed8
PM
5359static void hcr_writehigh(CPUARMState *env, const ARMCPRegInfo *ri,
5360 uint64_t value)
5361{
5362 /* Handle HCR2 write, i.e. write to high half of HCR_EL2 */
5363 value = deposit64(env->cp15.hcr_el2, 32, 32, value);
d1fb4da2 5364 do_hcr_write(env, value, MAKE_64BIT_MASK(0, 32));
ce4afed8
PM
5365}
5366
5367static void hcr_writelow(CPUARMState *env, const ARMCPRegInfo *ri,
5368 uint64_t value)
5369{
5370 /* Handle HCR write, i.e. write to low half of HCR_EL2 */
5371 value = deposit64(env->cp15.hcr_el2, 0, 32, value);
d1fb4da2 5372 do_hcr_write(env, value, MAKE_64BIT_MASK(32, 32));
f149e3e8
EI
5373}
5374
f7778444
RH
5375/*
5376 * Return the effective value of HCR_EL2.
5377 * Bits that are not included here:
5378 * RW (read from SCR_EL3.RW as needed)
5379 */
5380uint64_t arm_hcr_el2_eff(CPUARMState *env)
5381{
5382 uint64_t ret = env->cp15.hcr_el2;
5383
e6ef0169 5384 if (!arm_is_el2_enabled(env)) {
f7778444
RH
5385 /*
5386 * "This register has no effect if EL2 is not enabled in the
5387 * current Security state". This is ARMv8.4-SecEL2 speak for
5388 * !(SCR_EL3.NS==1 || SCR_EL3.EEL2==1).
5389 *
5390 * Prior to that, the language was "In an implementation that
5391 * includes EL3, when the value of SCR_EL3.NS is 0 the PE behaves
5392 * as if this field is 0 for all purposes other than a direct
5393 * read or write access of HCR_EL2". With lots of enumeration
5394 * on a per-field basis. In current QEMU, this is condition
5395 * is arm_is_secure_below_el3.
5396 *
5397 * Since the v8.4 language applies to the entire register, and
5398 * appears to be backward compatible, use that.
5399 */
4990e1d3
RH
5400 return 0;
5401 }
5402
5403 /*
5404 * For a cpu that supports both aarch64 and aarch32, we can set bits
5405 * in HCR_EL2 (e.g. via EL3) that are RES0 when we enter EL2 as aa32.
5406 * Ignore all of the bits in HCR+HCR2 that are not valid for aarch32.
5407 */
5408 if (!arm_el_is_aa64(env, 2)) {
5409 uint64_t aa32_valid;
5410
5411 /*
5412 * These bits are up-to-date as of ARMv8.6.
5413 * For HCR, it's easiest to list just the 2 bits that are invalid.
5414 * For HCR2, list those that are valid.
5415 */
5416 aa32_valid = MAKE_64BIT_MASK(0, 32) & ~(HCR_RW | HCR_TDZ);
5417 aa32_valid |= (HCR_CD | HCR_ID | HCR_TERR | HCR_TEA | HCR_MIOCNCE |
5418 HCR_TID4 | HCR_TICAB | HCR_TOCU | HCR_TTLBIS);
5419 ret &= aa32_valid;
5420 }
5421
5422 if (ret & HCR_TGE) {
5423 /* These bits are up-to-date as of ARMv8.6. */
f7778444
RH
5424 if (ret & HCR_E2H) {
5425 ret &= ~(HCR_VM | HCR_FMO | HCR_IMO | HCR_AMO |
5426 HCR_BSU_MASK | HCR_DC | HCR_TWI | HCR_TWE |
5427 HCR_TID0 | HCR_TID2 | HCR_TPCP | HCR_TPU |
4990e1d3
RH
5428 HCR_TDZ | HCR_CD | HCR_ID | HCR_MIOCNCE |
5429 HCR_TID4 | HCR_TICAB | HCR_TOCU | HCR_ENSCXT |
5430 HCR_TTLBIS | HCR_TTLBOS | HCR_TID5);
f7778444
RH
5431 } else {
5432 ret |= HCR_FMO | HCR_IMO | HCR_AMO;
5433 }
5434 ret &= ~(HCR_SWIO | HCR_PTW | HCR_VF | HCR_VI | HCR_VSE |
5435 HCR_FB | HCR_TID1 | HCR_TID3 | HCR_TSC | HCR_TACR |
5436 HCR_TSW | HCR_TTLB | HCR_TVM | HCR_HCD | HCR_TRVM |
5437 HCR_TLOR);
5438 }
5439
5440 return ret;
5441}
5442
fc1120a7
PM
5443static void cptr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
5444 uint64_t value)
5445{
5446 /*
5447 * For A-profile AArch32 EL3, if NSACR.CP10
5448 * is 0 then HCPTR.{TCP11,TCP10} ignore writes and read as 1.
5449 */
5450 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
5451 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
5452 value &= ~(0x3 << 10);
5453 value |= env->cp15.cptr_el[2] & (0x3 << 10);
5454 }
5455 env->cp15.cptr_el[2] = value;
5456}
5457
5458static uint64_t cptr_el2_read(CPUARMState *env, const ARMCPRegInfo *ri)
5459{
5460 /*
5461 * For A-profile AArch32 EL3, if NSACR.CP10
5462 * is 0 then HCPTR.{TCP11,TCP10} ignore writes and read as 1.
5463 */
5464 uint64_t value = env->cp15.cptr_el[2];
5465
5466 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
5467 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
5468 value |= 0x3 << 10;
5469 }
5470 return value;
5471}
5472
4771cd01 5473static const ARMCPRegInfo el2_cp_reginfo[] = {
f149e3e8 5474 { .name = "HCR_EL2", .state = ARM_CP_STATE_AA64,
89430fc6 5475 .type = ARM_CP_IO,
f149e3e8
EI
5476 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
5477 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
c624ea0f 5478 .writefn = hcr_write },
ce4afed8 5479 { .name = "HCR", .state = ARM_CP_STATE_AA32,
89430fc6 5480 .type = ARM_CP_ALIAS | ARM_CP_IO,
ce4afed8
PM
5481 .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
5482 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
c624ea0f 5483 .writefn = hcr_writelow },
831a2fca
PM
5484 { .name = "HACR_EL2", .state = ARM_CP_STATE_BOTH,
5485 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 7,
5486 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3b685ba7 5487 { .name = "ELR_EL2", .state = ARM_CP_STATE_AA64,
7a0e58fa 5488 .type = ARM_CP_ALIAS,
3b685ba7
EI
5489 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 1,
5490 .access = PL2_RW,
5491 .fieldoffset = offsetof(CPUARMState, elr_el[2]) },
68e78e33 5492 { .name = "ESR_EL2", .state = ARM_CP_STATE_BOTH,
f2c30f42
EI
5493 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 0,
5494 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[2]) },
cba517c3 5495 { .name = "FAR_EL2", .state = ARM_CP_STATE_BOTH,
63b60551
EI
5496 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 0,
5497 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[2]) },
cba517c3
PM
5498 { .name = "HIFAR", .state = ARM_CP_STATE_AA32,
5499 .type = ARM_CP_ALIAS,
5500 .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 2,
5501 .access = PL2_RW,
5502 .fieldoffset = offsetofhigh32(CPUARMState, cp15.far_el[2]) },
3b685ba7 5503 { .name = "SPSR_EL2", .state = ARM_CP_STATE_AA64,
7a0e58fa 5504 .type = ARM_CP_ALIAS,
3b685ba7 5505 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 0,
99a99c1f
SB
5506 .access = PL2_RW,
5507 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_HYP]) },
d79e0c06 5508 { .name = "VBAR_EL2", .state = ARM_CP_STATE_BOTH,
d42e3c26
EI
5509 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0,
5510 .access = PL2_RW, .writefn = vbar_write,
5511 .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[2]),
5512 .resetvalue = 0 },
884b4dee
GB
5513 { .name = "SP_EL2", .state = ARM_CP_STATE_AA64,
5514 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 1, .opc2 = 0,
7a0e58fa 5515 .access = PL3_RW, .type = ARM_CP_ALIAS,
884b4dee 5516 .fieldoffset = offsetof(CPUARMState, sp_el[2]) },
c6f19164
GB
5517 { .name = "CPTR_EL2", .state = ARM_CP_STATE_BOTH,
5518 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 2,
5519 .access = PL2_RW, .accessfn = cptr_access, .resetvalue = 0,
fc1120a7
PM
5520 .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[2]),
5521 .readfn = cptr_el2_read, .writefn = cptr_el2_write },
95f949ac
EI
5522 { .name = "MAIR_EL2", .state = ARM_CP_STATE_BOTH,
5523 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 0,
5524 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[2]),
5525 .resetvalue = 0 },
5526 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
b5ede85b 5527 .cp = 15, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 1,
95f949ac
EI
5528 .access = PL2_RW, .type = ARM_CP_ALIAS,
5529 .fieldoffset = offsetofhigh32(CPUARMState, cp15.mair_el[2]) },
2179ef95
PM
5530 { .name = "AMAIR_EL2", .state = ARM_CP_STATE_BOTH,
5531 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 0,
5532 .access = PL2_RW, .type = ARM_CP_CONST,
5533 .resetvalue = 0 },
5534 /* HAMAIR1 is mapped to AMAIR_EL2[63:32] */
55b53c71 5535 { .name = "HAMAIR1", .state = ARM_CP_STATE_AA32,
b5ede85b 5536 .cp = 15, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 1,
2179ef95
PM
5537 .access = PL2_RW, .type = ARM_CP_CONST,
5538 .resetvalue = 0 },
37cd6c24
PM
5539 { .name = "AFSR0_EL2", .state = ARM_CP_STATE_BOTH,
5540 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 0,
5541 .access = PL2_RW, .type = ARM_CP_CONST,
5542 .resetvalue = 0 },
5543 { .name = "AFSR1_EL2", .state = ARM_CP_STATE_BOTH,
5544 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 1,
5545 .access = PL2_RW, .type = ARM_CP_CONST,
5546 .resetvalue = 0 },
06ec4c8c
EI
5547 { .name = "TCR_EL2", .state = ARM_CP_STATE_BOTH,
5548 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 2,
d06dc933
RH
5549 .access = PL2_RW, .writefn = vmsa_tcr_el12_write,
5550 /* no .raw_writefn or .resetfn needed as we never use mask/base_mask */
06ec4c8c 5551 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[2]) },
68e9c2fe
EI
5552 { .name = "VTCR", .state = ARM_CP_STATE_AA32,
5553 .cp = 15, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
bf06c112 5554 .type = ARM_CP_ALIAS,
68e9c2fe
EI
5555 .access = PL2_RW, .accessfn = access_el3_aa32ns,
5556 .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) },
5557 { .name = "VTCR_EL2", .state = ARM_CP_STATE_AA64,
5558 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
bf06c112
PM
5559 .access = PL2_RW,
5560 /* no .writefn needed as this can't cause an ASID change;
5561 * no .raw_writefn or .resetfn needed as we never use mask/base_mask
5562 */
68e9c2fe 5563 .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) },
b698e9cf
EI
5564 { .name = "VTTBR", .state = ARM_CP_STATE_AA32,
5565 .cp = 15, .opc1 = 6, .crm = 2,
5566 .type = ARM_CP_64BIT | ARM_CP_ALIAS,
5567 .access = PL2_RW, .accessfn = access_el3_aa32ns,
5568 .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2),
5569 .writefn = vttbr_write },
5570 { .name = "VTTBR_EL2", .state = ARM_CP_STATE_AA64,
5571 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 0,
5572 .access = PL2_RW, .writefn = vttbr_write,
5573 .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2) },
b9cb5323
EI
5574 { .name = "SCTLR_EL2", .state = ARM_CP_STATE_BOTH,
5575 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 0,
5576 .access = PL2_RW, .raw_writefn = raw_write, .writefn = sctlr_write,
5577 .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[2]) },
ff05f37b
EI
5578 { .name = "TPIDR_EL2", .state = ARM_CP_STATE_BOTH,
5579 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 2,
5580 .access = PL2_RW, .resetvalue = 0,
5581 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[2]) },
a57633c0
EI
5582 { .name = "TTBR0_EL2", .state = ARM_CP_STATE_AA64,
5583 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
ed30da8e 5584 .access = PL2_RW, .resetvalue = 0, .writefn = vmsa_tcr_ttbr_el2_write,
a57633c0
EI
5585 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
5586 { .name = "HTTBR", .cp = 15, .opc1 = 4, .crm = 2,
5587 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
a57633c0 5588 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
541ef8c2
SS
5589 { .name = "TLBIALLNSNH",
5590 .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
5591 .type = ARM_CP_NO_RAW, .access = PL2_W,
5592 .writefn = tlbiall_nsnh_write },
5593 { .name = "TLBIALLNSNHIS",
5594 .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
5595 .type = ARM_CP_NO_RAW, .access = PL2_W,
5596 .writefn = tlbiall_nsnh_is_write },
5597 { .name = "TLBIALLH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
5598 .type = ARM_CP_NO_RAW, .access = PL2_W,
5599 .writefn = tlbiall_hyp_write },
5600 { .name = "TLBIALLHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
5601 .type = ARM_CP_NO_RAW, .access = PL2_W,
5602 .writefn = tlbiall_hyp_is_write },
5603 { .name = "TLBIMVAH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
5604 .type = ARM_CP_NO_RAW, .access = PL2_W,
5605 .writefn = tlbimva_hyp_write },
5606 { .name = "TLBIMVAHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
5607 .type = ARM_CP_NO_RAW, .access = PL2_W,
5608 .writefn = tlbimva_hyp_is_write },
51da9014
EI
5609 { .name = "TLBI_ALLE2", .state = ARM_CP_STATE_AA64,
5610 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
5611 .type = ARM_CP_NO_RAW, .access = PL2_W,
fd3ed969 5612 .writefn = tlbi_aa64_alle2_write },
8742d49d
EI
5613 { .name = "TLBI_VAE2", .state = ARM_CP_STATE_AA64,
5614 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
5615 .type = ARM_CP_NO_RAW, .access = PL2_W,
fd3ed969 5616 .writefn = tlbi_aa64_vae2_write },
2bfb9d75
PM
5617 { .name = "TLBI_VALE2", .state = ARM_CP_STATE_AA64,
5618 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
5619 .access = PL2_W, .type = ARM_CP_NO_RAW,
5620 .writefn = tlbi_aa64_vae2_write },
5621 { .name = "TLBI_ALLE2IS", .state = ARM_CP_STATE_AA64,
5622 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
5623 .access = PL2_W, .type = ARM_CP_NO_RAW,
5624 .writefn = tlbi_aa64_alle2is_write },
8742d49d
EI
5625 { .name = "TLBI_VAE2IS", .state = ARM_CP_STATE_AA64,
5626 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
5627 .type = ARM_CP_NO_RAW, .access = PL2_W,
fd3ed969 5628 .writefn = tlbi_aa64_vae2is_write },
2bfb9d75
PM
5629 { .name = "TLBI_VALE2IS", .state = ARM_CP_STATE_AA64,
5630 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
5631 .access = PL2_W, .type = ARM_CP_NO_RAW,
5632 .writefn = tlbi_aa64_vae2is_write },
edac4d8a 5633#ifndef CONFIG_USER_ONLY
2a47df95
PM
5634 /* Unlike the other EL2-related AT operations, these must
5635 * UNDEF from EL3 if EL2 is not implemented, which is why we
5636 * define them here rather than with the rest of the AT ops.
5637 */
5638 { .name = "AT_S1E2R", .state = ARM_CP_STATE_AA64,
5639 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
5640 .access = PL2_W, .accessfn = at_s1e2_access,
0710b2fa 5641 .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC, .writefn = ats_write64 },
2a47df95
PM
5642 { .name = "AT_S1E2W", .state = ARM_CP_STATE_AA64,
5643 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
5644 .access = PL2_W, .accessfn = at_s1e2_access,
0710b2fa 5645 .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC, .writefn = ats_write64 },
14db7fe0
PM
5646 /* The AArch32 ATS1H* operations are CONSTRAINED UNPREDICTABLE
5647 * if EL2 is not implemented; we choose to UNDEF. Behaviour at EL3
5648 * with SCR.NS == 0 outside Monitor mode is UNPREDICTABLE; we choose
5649 * to behave as if SCR.NS was 1.
5650 */
5651 { .name = "ATS1HR", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
5652 .access = PL2_W,
0710b2fa 5653 .writefn = ats1h_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
14db7fe0
PM
5654 { .name = "ATS1HW", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
5655 .access = PL2_W,
0710b2fa 5656 .writefn = ats1h_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
0b6440af
EI
5657 { .name = "CNTHCTL_EL2", .state = ARM_CP_STATE_BOTH,
5658 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 1, .opc2 = 0,
5659 /* ARMv7 requires bit 0 and 1 to reset to 1. ARMv8 defines the
5660 * reset values as IMPDEF. We choose to reset to 3 to comply with
5661 * both ARMv7 and ARMv8.
5662 */
5663 .access = PL2_RW, .resetvalue = 3,
5664 .fieldoffset = offsetof(CPUARMState, cp15.cnthctl_el2) },
edac4d8a
EI
5665 { .name = "CNTVOFF_EL2", .state = ARM_CP_STATE_AA64,
5666 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 3,
5667 .access = PL2_RW, .type = ARM_CP_IO, .resetvalue = 0,
5668 .writefn = gt_cntvoff_write,
5669 .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
5670 { .name = "CNTVOFF", .cp = 15, .opc1 = 4, .crm = 14,
5671 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS | ARM_CP_IO,
5672 .writefn = gt_cntvoff_write,
5673 .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
b0e66d95
EI
5674 { .name = "CNTHP_CVAL_EL2", .state = ARM_CP_STATE_AA64,
5675 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 2,
5676 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
5677 .type = ARM_CP_IO, .access = PL2_RW,
5678 .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
5679 { .name = "CNTHP_CVAL", .cp = 15, .opc1 = 6, .crm = 14,
5680 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
5681 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_IO,
5682 .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
5683 { .name = "CNTHP_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
5684 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 0,
d44ec156 5685 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
b0e66d95
EI
5686 .resetfn = gt_hyp_timer_reset,
5687 .readfn = gt_hyp_tval_read, .writefn = gt_hyp_tval_write },
5688 { .name = "CNTHP_CTL_EL2", .state = ARM_CP_STATE_BOTH,
5689 .type = ARM_CP_IO,
5690 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 1,
5691 .access = PL2_RW,
5692 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].ctl),
5693 .resetvalue = 0,
5694 .writefn = gt_hyp_ctl_write, .raw_writefn = raw_write },
edac4d8a 5695#endif
14cc7b54
SF
5696 /* The only field of MDCR_EL2 that has a defined architectural reset value
5697 * is MDCR_EL2.HPMN which should reset to the value of PMCR_EL0.N; but we
5ecdd3e4 5698 * don't implement any PMU event counters, so using zero as a reset
14cc7b54
SF
5699 * value for MDCR_EL2 is okay
5700 */
5701 { .name = "MDCR_EL2", .state = ARM_CP_STATE_BOTH,
5702 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 1,
5703 .access = PL2_RW, .resetvalue = 0,
5704 .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el2), },
59e05530
EI
5705 { .name = "HPFAR", .state = ARM_CP_STATE_AA32,
5706 .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
5707 .access = PL2_RW, .accessfn = access_el3_aa32ns,
5708 .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
5709 { .name = "HPFAR_EL2", .state = ARM_CP_STATE_AA64,
5710 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
5711 .access = PL2_RW,
5712 .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
2a5a9abd
AF
5713 { .name = "HSTR_EL2", .state = ARM_CP_STATE_BOTH,
5714 .cp = 15, .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 3,
5715 .access = PL2_RW,
5716 .fieldoffset = offsetof(CPUARMState, cp15.hstr_el2) },
3b685ba7
EI
5717 REGINFO_SENTINEL
5718};
5719
ce4afed8
PM
5720static const ARMCPRegInfo el2_v8_cp_reginfo[] = {
5721 { .name = "HCR2", .state = ARM_CP_STATE_AA32,
89430fc6 5722 .type = ARM_CP_ALIAS | ARM_CP_IO,
ce4afed8
PM
5723 .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 4,
5724 .access = PL2_RW,
5725 .fieldoffset = offsetofhigh32(CPUARMState, cp15.hcr_el2),
5726 .writefn = hcr_writehigh },
5727 REGINFO_SENTINEL
5728};
5729
e9152ee9
RDC
5730static CPAccessResult sel2_access(CPUARMState *env, const ARMCPRegInfo *ri,
5731 bool isread)
5732{
5733 if (arm_current_el(env) == 3 || arm_is_secure_below_el3(env)) {
5734 return CP_ACCESS_OK;
5735 }
5736 return CP_ACCESS_TRAP_UNCATEGORIZED;
5737}
5738
5739static const ARMCPRegInfo el2_sec_cp_reginfo[] = {
5740 { .name = "VSTTBR_EL2", .state = ARM_CP_STATE_AA64,
5741 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 6, .opc2 = 0,
5742 .access = PL2_RW, .accessfn = sel2_access,
5743 .fieldoffset = offsetof(CPUARMState, cp15.vsttbr_el2) },
5744 { .name = "VSTCR_EL2", .state = ARM_CP_STATE_AA64,
5745 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 6, .opc2 = 2,
5746 .access = PL2_RW, .accessfn = sel2_access,
5747 .fieldoffset = offsetof(CPUARMState, cp15.vstcr_el2) },
5748 REGINFO_SENTINEL
5749};
5750
2f027fc5
PM
5751static CPAccessResult nsacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
5752 bool isread)
5753{
5754 /* The NSACR is RW at EL3, and RO for NS EL1 and NS EL2.
5755 * At Secure EL1 it traps to EL3.
5756 */
5757 if (arm_current_el(env) == 3) {
5758 return CP_ACCESS_OK;
5759 }
5760 if (arm_is_secure_below_el3(env)) {
5761 return CP_ACCESS_TRAP_EL3;
5762 }
5763 /* Accesses from EL1 NS and EL2 NS are UNDEF for write but allow reads. */
5764 if (isread) {
5765 return CP_ACCESS_OK;
5766 }
5767 return CP_ACCESS_TRAP_UNCATEGORIZED;
5768}
5769
60fb1a87
GB
5770static const ARMCPRegInfo el3_cp_reginfo[] = {
5771 { .name = "SCR_EL3", .state = ARM_CP_STATE_AA64,
5772 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 0,
5773 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.scr_el3),
5774 .resetvalue = 0, .writefn = scr_write },
f80741d1 5775 { .name = "SCR", .type = ARM_CP_ALIAS | ARM_CP_NEWEL,
60fb1a87 5776 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 0,
efe4a274
PM
5777 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
5778 .fieldoffset = offsetoflow32(CPUARMState, cp15.scr_el3),
b061a82b 5779 .writefn = scr_write },
60fb1a87
GB
5780 { .name = "SDER32_EL3", .state = ARM_CP_STATE_AA64,
5781 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 1,
5782 .access = PL3_RW, .resetvalue = 0,
5783 .fieldoffset = offsetof(CPUARMState, cp15.sder) },
5784 { .name = "SDER",
5785 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 1,
5786 .access = PL3_RW, .resetvalue = 0,
5787 .fieldoffset = offsetoflow32(CPUARMState, cp15.sder) },
60fb1a87 5788 { .name = "MVBAR", .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
efe4a274
PM
5789 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
5790 .writefn = vbar_write, .resetvalue = 0,
60fb1a87 5791 .fieldoffset = offsetof(CPUARMState, cp15.mvbar) },
7dd8c9af
FA
5792 { .name = "TTBR0_EL3", .state = ARM_CP_STATE_AA64,
5793 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 0,
f478847f 5794 .access = PL3_RW, .resetvalue = 0,
7dd8c9af 5795 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[3]) },
11f136ee
FA
5796 { .name = "TCR_EL3", .state = ARM_CP_STATE_AA64,
5797 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 2,
6459b94c
PM
5798 .access = PL3_RW,
5799 /* no .writefn needed as this can't cause an ASID change;
811595a2
PM
5800 * we must provide a .raw_writefn and .resetfn because we handle
5801 * reset and migration for the AArch32 TTBCR(S), which might be
5802 * using mask and base_mask.
6459b94c 5803 */
811595a2 5804 .resetfn = vmsa_ttbcr_reset, .raw_writefn = vmsa_ttbcr_raw_write,
11f136ee 5805 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[3]) },
81547d66 5806 { .name = "ELR_EL3", .state = ARM_CP_STATE_AA64,
7a0e58fa 5807 .type = ARM_CP_ALIAS,
81547d66
EI
5808 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 1,
5809 .access = PL3_RW,
5810 .fieldoffset = offsetof(CPUARMState, elr_el[3]) },
f2c30f42 5811 { .name = "ESR_EL3", .state = ARM_CP_STATE_AA64,
f2c30f42
EI
5812 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 2, .opc2 = 0,
5813 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[3]) },
63b60551
EI
5814 { .name = "FAR_EL3", .state = ARM_CP_STATE_AA64,
5815 .opc0 = 3, .opc1 = 6, .crn = 6, .crm = 0, .opc2 = 0,
5816 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[3]) },
81547d66 5817 { .name = "SPSR_EL3", .state = ARM_CP_STATE_AA64,
7a0e58fa 5818 .type = ARM_CP_ALIAS,
81547d66 5819 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 0,
99a99c1f
SB
5820 .access = PL3_RW,
5821 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_MON]) },
a1ba125c
EI
5822 { .name = "VBAR_EL3", .state = ARM_CP_STATE_AA64,
5823 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 0,
5824 .access = PL3_RW, .writefn = vbar_write,
5825 .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[3]),
5826 .resetvalue = 0 },
c6f19164
GB
5827 { .name = "CPTR_EL3", .state = ARM_CP_STATE_AA64,
5828 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 2,
5829 .access = PL3_RW, .accessfn = cptr_access, .resetvalue = 0,
5830 .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[3]) },
4cfb8ad8
PM
5831 { .name = "TPIDR_EL3", .state = ARM_CP_STATE_AA64,
5832 .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 2,
5833 .access = PL3_RW, .resetvalue = 0,
5834 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[3]) },
2179ef95
PM
5835 { .name = "AMAIR_EL3", .state = ARM_CP_STATE_AA64,
5836 .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 3, .opc2 = 0,
5837 .access = PL3_RW, .type = ARM_CP_CONST,
5838 .resetvalue = 0 },
37cd6c24
PM
5839 { .name = "AFSR0_EL3", .state = ARM_CP_STATE_BOTH,
5840 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 0,
5841 .access = PL3_RW, .type = ARM_CP_CONST,
5842 .resetvalue = 0 },
5843 { .name = "AFSR1_EL3", .state = ARM_CP_STATE_BOTH,
5844 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 1,
5845 .access = PL3_RW, .type = ARM_CP_CONST,
5846 .resetvalue = 0 },
43efaa33
PM
5847 { .name = "TLBI_ALLE3IS", .state = ARM_CP_STATE_AA64,
5848 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 0,
5849 .access = PL3_W, .type = ARM_CP_NO_RAW,
5850 .writefn = tlbi_aa64_alle3is_write },
5851 { .name = "TLBI_VAE3IS", .state = ARM_CP_STATE_AA64,
5852 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 1,
5853 .access = PL3_W, .type = ARM_CP_NO_RAW,
5854 .writefn = tlbi_aa64_vae3is_write },
5855 { .name = "TLBI_VALE3IS", .state = ARM_CP_STATE_AA64,
5856 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 5,
5857 .access = PL3_W, .type = ARM_CP_NO_RAW,
5858 .writefn = tlbi_aa64_vae3is_write },
5859 { .name = "TLBI_ALLE3", .state = ARM_CP_STATE_AA64,
5860 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 0,
5861 .access = PL3_W, .type = ARM_CP_NO_RAW,
5862 .writefn = tlbi_aa64_alle3_write },
5863 { .name = "TLBI_VAE3", .state = ARM_CP_STATE_AA64,
5864 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 1,
5865 .access = PL3_W, .type = ARM_CP_NO_RAW,
5866 .writefn = tlbi_aa64_vae3_write },
5867 { .name = "TLBI_VALE3", .state = ARM_CP_STATE_AA64,
5868 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 5,
5869 .access = PL3_W, .type = ARM_CP_NO_RAW,
5870 .writefn = tlbi_aa64_vae3_write },
0f1a3b24
FA
5871 REGINFO_SENTINEL
5872};
5873
e2cce18f
RH
5874#ifndef CONFIG_USER_ONLY
5875/* Test if system register redirection is to occur in the current state. */
5876static bool redirect_for_e2h(CPUARMState *env)
5877{
5878 return arm_current_el(env) == 2 && (arm_hcr_el2_eff(env) & HCR_E2H);
5879}
5880
5881static uint64_t el2_e2h_read(CPUARMState *env, const ARMCPRegInfo *ri)
5882{
5883 CPReadFn *readfn;
5884
5885 if (redirect_for_e2h(env)) {
5886 /* Switch to the saved EL2 version of the register. */
5887 ri = ri->opaque;
5888 readfn = ri->readfn;
5889 } else {
5890 readfn = ri->orig_readfn;
5891 }
5892 if (readfn == NULL) {
5893 readfn = raw_read;
5894 }
5895 return readfn(env, ri);
5896}
5897
5898static void el2_e2h_write(CPUARMState *env, const ARMCPRegInfo *ri,
5899 uint64_t value)
5900{
5901 CPWriteFn *writefn;
5902
5903 if (redirect_for_e2h(env)) {
5904 /* Switch to the saved EL2 version of the register. */
5905 ri = ri->opaque;
5906 writefn = ri->writefn;
5907 } else {
5908 writefn = ri->orig_writefn;
5909 }
5910 if (writefn == NULL) {
5911 writefn = raw_write;
5912 }
5913 writefn(env, ri, value);
5914}
5915
5916static void define_arm_vh_e2h_redirects_aliases(ARMCPU *cpu)
5917{
5918 struct E2HAlias {
5919 uint32_t src_key, dst_key, new_key;
5920 const char *src_name, *dst_name, *new_name;
5921 bool (*feature)(const ARMISARegisters *id);
5922 };
5923
5924#define K(op0, op1, crn, crm, op2) \
5925 ENCODE_AA64_CP_REG(CP_REG_ARM64_SYSREG_CP, crn, crm, op0, op1, op2)
5926
5927 static const struct E2HAlias aliases[] = {
5928 { K(3, 0, 1, 0, 0), K(3, 4, 1, 0, 0), K(3, 5, 1, 0, 0),
5929 "SCTLR", "SCTLR_EL2", "SCTLR_EL12" },
5930 { K(3, 0, 1, 0, 2), K(3, 4, 1, 1, 2), K(3, 5, 1, 0, 2),
5931 "CPACR", "CPTR_EL2", "CPACR_EL12" },
5932 { K(3, 0, 2, 0, 0), K(3, 4, 2, 0, 0), K(3, 5, 2, 0, 0),
5933 "TTBR0_EL1", "TTBR0_EL2", "TTBR0_EL12" },
5934 { K(3, 0, 2, 0, 1), K(3, 4, 2, 0, 1), K(3, 5, 2, 0, 1),
5935 "TTBR1_EL1", "TTBR1_EL2", "TTBR1_EL12" },
5936 { K(3, 0, 2, 0, 2), K(3, 4, 2, 0, 2), K(3, 5, 2, 0, 2),
5937 "TCR_EL1", "TCR_EL2", "TCR_EL12" },
5938 { K(3, 0, 4, 0, 0), K(3, 4, 4, 0, 0), K(3, 5, 4, 0, 0),
5939 "SPSR_EL1", "SPSR_EL2", "SPSR_EL12" },
5940 { K(3, 0, 4, 0, 1), K(3, 4, 4, 0, 1), K(3, 5, 4, 0, 1),
5941 "ELR_EL1", "ELR_EL2", "ELR_EL12" },
5942 { K(3, 0, 5, 1, 0), K(3, 4, 5, 1, 0), K(3, 5, 5, 1, 0),
5943 "AFSR0_EL1", "AFSR0_EL2", "AFSR0_EL12" },
5944 { K(3, 0, 5, 1, 1), K(3, 4, 5, 1, 1), K(3, 5, 5, 1, 1),
5945 "AFSR1_EL1", "AFSR1_EL2", "AFSR1_EL12" },
5946 { K(3, 0, 5, 2, 0), K(3, 4, 5, 2, 0), K(3, 5, 5, 2, 0),
5947 "ESR_EL1", "ESR_EL2", "ESR_EL12" },
5948 { K(3, 0, 6, 0, 0), K(3, 4, 6, 0, 0), K(3, 5, 6, 0, 0),
5949 "FAR_EL1", "FAR_EL2", "FAR_EL12" },
5950 { K(3, 0, 10, 2, 0), K(3, 4, 10, 2, 0), K(3, 5, 10, 2, 0),
5951 "MAIR_EL1", "MAIR_EL2", "MAIR_EL12" },
5952 { K(3, 0, 10, 3, 0), K(3, 4, 10, 3, 0), K(3, 5, 10, 3, 0),
5953 "AMAIR0", "AMAIR_EL2", "AMAIR_EL12" },
5954 { K(3, 0, 12, 0, 0), K(3, 4, 12, 0, 0), K(3, 5, 12, 0, 0),
5955 "VBAR", "VBAR_EL2", "VBAR_EL12" },
5956 { K(3, 0, 13, 0, 1), K(3, 4, 13, 0, 1), K(3, 5, 13, 0, 1),
5957 "CONTEXTIDR_EL1", "CONTEXTIDR_EL2", "CONTEXTIDR_EL12" },
5958 { K(3, 0, 14, 1, 0), K(3, 4, 14, 1, 0), K(3, 5, 14, 1, 0),
5959 "CNTKCTL", "CNTHCTL_EL2", "CNTKCTL_EL12" },
5960
5961 /*
5962 * Note that redirection of ZCR is mentioned in the description
5963 * of ZCR_EL2, and aliasing in the description of ZCR_EL1, but
5964 * not in the summary table.
5965 */
5966 { K(3, 0, 1, 2, 0), K(3, 4, 1, 2, 0), K(3, 5, 1, 2, 0),
5967 "ZCR_EL1", "ZCR_EL2", "ZCR_EL12", isar_feature_aa64_sve },
5968
4b779ceb
RH
5969 { K(3, 0, 5, 6, 0), K(3, 4, 5, 6, 0), K(3, 5, 5, 6, 0),
5970 "TFSR_EL1", "TFSR_EL2", "TFSR_EL12", isar_feature_aa64_mte },
5971
e2cce18f
RH
5972 /* TODO: ARMv8.2-SPE -- PMSCR_EL2 */
5973 /* TODO: ARMv8.4-Trace -- TRFCR_EL2 */
5974 };
5975#undef K
5976
5977 size_t i;
5978
5979 for (i = 0; i < ARRAY_SIZE(aliases); i++) {
5980 const struct E2HAlias *a = &aliases[i];
5981 ARMCPRegInfo *src_reg, *dst_reg;
5982
5983 if (a->feature && !a->feature(&cpu->isar)) {
5984 continue;
5985 }
5986
5987 src_reg = g_hash_table_lookup(cpu->cp_regs, &a->src_key);
5988 dst_reg = g_hash_table_lookup(cpu->cp_regs, &a->dst_key);
5989 g_assert(src_reg != NULL);
5990 g_assert(dst_reg != NULL);
5991
5992 /* Cross-compare names to detect typos in the keys. */
5993 g_assert(strcmp(src_reg->name, a->src_name) == 0);
5994 g_assert(strcmp(dst_reg->name, a->dst_name) == 0);
5995
5996 /* None of the core system registers use opaque; we will. */
5997 g_assert(src_reg->opaque == NULL);
5998
5999 /* Create alias before redirection so we dup the right data. */
6000 if (a->new_key) {
6001 ARMCPRegInfo *new_reg = g_memdup(src_reg, sizeof(ARMCPRegInfo));
6002 uint32_t *new_key = g_memdup(&a->new_key, sizeof(uint32_t));
6003 bool ok;
6004
6005 new_reg->name = a->new_name;
6006 new_reg->type |= ARM_CP_ALIAS;
6007 /* Remove PL1/PL0 access, leaving PL2/PL3 R/W in place. */
6008 new_reg->access &= PL2_RW | PL3_RW;
6009
6010 ok = g_hash_table_insert(cpu->cp_regs, new_key, new_reg);
6011 g_assert(ok);
6012 }
6013
6014 src_reg->opaque = dst_reg;
6015 src_reg->orig_readfn = src_reg->readfn ?: raw_read;
6016 src_reg->orig_writefn = src_reg->writefn ?: raw_write;
6017 if (!src_reg->raw_readfn) {
6018 src_reg->raw_readfn = raw_read;
6019 }
6020 if (!src_reg->raw_writefn) {
6021 src_reg->raw_writefn = raw_write;
6022 }
6023 src_reg->readfn = el2_e2h_read;
6024 src_reg->writefn = el2_e2h_write;
6025 }
6026}
6027#endif
6028
3f208fd7
PM
6029static CPAccessResult ctr_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
6030 bool isread)
7da845b0 6031{
97475a89
RH
6032 int cur_el = arm_current_el(env);
6033
6034 if (cur_el < 2) {
6035 uint64_t hcr = arm_hcr_el2_eff(env);
6036
6037 if (cur_el == 0) {
6038 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
6039 if (!(env->cp15.sctlr_el[2] & SCTLR_UCT)) {
6040 return CP_ACCESS_TRAP_EL2;
6041 }
6042 } else {
6043 if (!(env->cp15.sctlr_el[1] & SCTLR_UCT)) {
6044 return CP_ACCESS_TRAP;
6045 }
6046 if (hcr & HCR_TID2) {
6047 return CP_ACCESS_TRAP_EL2;
6048 }
6049 }
6050 } else if (hcr & HCR_TID2) {
6051 return CP_ACCESS_TRAP_EL2;
6052 }
7da845b0 6053 }
630fcd4d
MZ
6054
6055 if (arm_current_el(env) < 2 && arm_hcr_el2_eff(env) & HCR_TID2) {
6056 return CP_ACCESS_TRAP_EL2;
6057 }
6058
7da845b0
PM
6059 return CP_ACCESS_OK;
6060}
6061
1424ca8d
DM
6062static void oslar_write(CPUARMState *env, const ARMCPRegInfo *ri,
6063 uint64_t value)
6064{
6065 /* Writes to OSLAR_EL1 may update the OS lock status, which can be
6066 * read via a bit in OSLSR_EL1.
6067 */
6068 int oslock;
6069
6070 if (ri->state == ARM_CP_STATE_AA32) {
6071 oslock = (value == 0xC5ACCE55);
6072 } else {
6073 oslock = value & 1;
6074 }
6075
6076 env->cp15.oslsr_el1 = deposit32(env->cp15.oslsr_el1, 1, 1, oslock);
6077}
6078
50300698 6079static const ARMCPRegInfo debug_cp_reginfo[] = {
50300698 6080 /* DBGDRAR, DBGDSAR: always RAZ since we don't implement memory mapped
10aae104
PM
6081 * debug components. The AArch64 version of DBGDRAR is named MDRAR_EL1;
6082 * unlike DBGDRAR it is never accessible from EL0.
6083 * DBGDSAR is deprecated and must RAZ from v8 anyway, so it has no AArch64
6084 * accessor.
50300698
PM
6085 */
6086 { .name = "DBGDRAR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 0,
91b0a238
PM
6087 .access = PL0_R, .accessfn = access_tdra,
6088 .type = ARM_CP_CONST, .resetvalue = 0 },
10aae104
PM
6089 { .name = "MDRAR_EL1", .state = ARM_CP_STATE_AA64,
6090 .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
91b0a238
PM
6091 .access = PL1_R, .accessfn = access_tdra,
6092 .type = ARM_CP_CONST, .resetvalue = 0 },
50300698 6093 { .name = "DBGDSAR", .cp = 14, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
91b0a238
PM
6094 .access = PL0_R, .accessfn = access_tdra,
6095 .type = ARM_CP_CONST, .resetvalue = 0 },
17a9eb53 6096 /* Monitor debug system control register; the 32-bit alias is DBGDSCRext. */
10aae104
PM
6097 { .name = "MDSCR_EL1", .state = ARM_CP_STATE_BOTH,
6098 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
d6c8cf81 6099 .access = PL1_RW, .accessfn = access_tda,
0e5e8935
PM
6100 .fieldoffset = offsetof(CPUARMState, cp15.mdscr_el1),
6101 .resetvalue = 0 },
5e8b12ff
PM
6102 /* MDCCSR_EL0, aka DBGDSCRint. This is a read-only mirror of MDSCR_EL1.
6103 * We don't implement the configurable EL0 access.
6104 */
6105 { .name = "MDCCSR_EL0", .state = ARM_CP_STATE_BOTH,
6106 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
7a0e58fa 6107 .type = ARM_CP_ALIAS,
d6c8cf81 6108 .access = PL1_R, .accessfn = access_tda,
b061a82b 6109 .fieldoffset = offsetof(CPUARMState, cp15.mdscr_el1), },
10aae104
PM
6110 { .name = "OSLAR_EL1", .state = ARM_CP_STATE_BOTH,
6111 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 4,
1424ca8d 6112 .access = PL1_W, .type = ARM_CP_NO_RAW,
187f678d 6113 .accessfn = access_tdosa,
1424ca8d
DM
6114 .writefn = oslar_write },
6115 { .name = "OSLSR_EL1", .state = ARM_CP_STATE_BOTH,
6116 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 4,
6117 .access = PL1_R, .resetvalue = 10,
187f678d 6118 .accessfn = access_tdosa,
1424ca8d 6119 .fieldoffset = offsetof(CPUARMState, cp15.oslsr_el1) },
5e8b12ff
PM
6120 /* Dummy OSDLR_EL1: 32-bit Linux will read this */
6121 { .name = "OSDLR_EL1", .state = ARM_CP_STATE_BOTH,
6122 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 4,
187f678d
PM
6123 .access = PL1_RW, .accessfn = access_tdosa,
6124 .type = ARM_CP_NOP },
5e8b12ff
PM
6125 /* Dummy DBGVCR: Linux wants to clear this on startup, but we don't
6126 * implement vector catch debug events yet.
6127 */
6128 { .name = "DBGVCR",
6129 .cp = 14, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
d6c8cf81
PM
6130 .access = PL1_RW, .accessfn = access_tda,
6131 .type = ARM_CP_NOP },
4d2ec4da
PM
6132 /* Dummy DBGVCR32_EL2 (which is only for a 64-bit hypervisor
6133 * to save and restore a 32-bit guest's DBGVCR)
6134 */
6135 { .name = "DBGVCR32_EL2", .state = ARM_CP_STATE_AA64,
6136 .opc0 = 2, .opc1 = 4, .crn = 0, .crm = 7, .opc2 = 0,
6137 .access = PL2_RW, .accessfn = access_tda,
6138 .type = ARM_CP_NOP },
5dbdc434
PM
6139 /* Dummy MDCCINT_EL1, since we don't implement the Debug Communications
6140 * Channel but Linux may try to access this register. The 32-bit
6141 * alias is DBGDCCINT.
6142 */
6143 { .name = "MDCCINT_EL1", .state = ARM_CP_STATE_BOTH,
6144 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0,
6145 .access = PL1_RW, .accessfn = access_tda,
6146 .type = ARM_CP_NOP },
50300698
PM
6147 REGINFO_SENTINEL
6148};
6149
6150static const ARMCPRegInfo debug_lpae_cp_reginfo[] = {
6151 /* 64 bit access versions of the (dummy) debug registers */
6152 { .name = "DBGDRAR", .cp = 14, .crm = 1, .opc1 = 0,
6153 .access = PL0_R, .type = ARM_CP_CONST|ARM_CP_64BIT, .resetvalue = 0 },
6154 { .name = "DBGDSAR", .cp = 14, .crm = 2, .opc1 = 0,
6155 .access = PL0_R, .type = ARM_CP_CONST|ARM_CP_64BIT, .resetvalue = 0 },
6156 REGINFO_SENTINEL
6157};
6158
60eed086
RH
6159/* Return the exception level to which exceptions should be taken
6160 * via SVEAccessTrap. If an exception should be routed through
6161 * AArch64.AdvSIMDFPAccessTrap, return 0; fp_exception_el should
6162 * take care of raising that exception.
6163 * C.f. the ARM pseudocode function CheckSVEEnabled.
5be5e8ed 6164 */
ced31551 6165int sve_exception_el(CPUARMState *env, int el)
5be5e8ed
RH
6166{
6167#ifndef CONFIG_USER_ONLY
c2ddb7cf
RH
6168 uint64_t hcr_el2 = arm_hcr_el2_eff(env);
6169
6170 if (el <= 1 && (hcr_el2 & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
60eed086
RH
6171 bool disabled = false;
6172
6173 /* The CPACR.ZEN controls traps to EL1:
6174 * 0, 2 : trap EL0 and EL1 accesses
6175 * 1 : trap only EL0 accesses
6176 * 3 : trap no accesses
6177 */
6178 if (!extract32(env->cp15.cpacr_el1, 16, 1)) {
6179 disabled = true;
6180 } else if (!extract32(env->cp15.cpacr_el1, 17, 1)) {
2de7ace2 6181 disabled = el == 0;
5be5e8ed 6182 }
60eed086
RH
6183 if (disabled) {
6184 /* route_to_el2 */
c2ddb7cf 6185 return hcr_el2 & HCR_TGE ? 2 : 1;
5be5e8ed 6186 }
5be5e8ed 6187
60eed086
RH
6188 /* Check CPACR.FPEN. */
6189 if (!extract32(env->cp15.cpacr_el1, 20, 1)) {
6190 disabled = true;
6191 } else if (!extract32(env->cp15.cpacr_el1, 21, 1)) {
2de7ace2 6192 disabled = el == 0;
5be5e8ed 6193 }
60eed086
RH
6194 if (disabled) {
6195 return 0;
5be5e8ed 6196 }
5be5e8ed
RH
6197 }
6198
60eed086
RH
6199 /* CPTR_EL2. Since TZ and TFP are positive,
6200 * they will be zero when EL2 is not present.
6201 */
e6ef0169 6202 if (el <= 2 && arm_is_el2_enabled(env)) {
60eed086
RH
6203 if (env->cp15.cptr_el[2] & CPTR_TZ) {
6204 return 2;
6205 }
6206 if (env->cp15.cptr_el[2] & CPTR_TFP) {
6207 return 0;
6208 }
5be5e8ed
RH
6209 }
6210
60eed086
RH
6211 /* CPTR_EL3. Since EZ is negative we must check for EL3. */
6212 if (arm_feature(env, ARM_FEATURE_EL3)
6213 && !(env->cp15.cptr_el[3] & CPTR_EZ)) {
5be5e8ed
RH
6214 return 3;
6215 }
6216#endif
6217 return 0;
6218}
6219
0df9142d
AJ
6220static uint32_t sve_zcr_get_valid_len(ARMCPU *cpu, uint32_t start_len)
6221{
6e553f2a 6222 uint32_t end_len;
0df9142d 6223
6e553f2a
RH
6224 end_len = start_len &= 0xf;
6225 if (!test_bit(start_len, cpu->sve_vq_map)) {
6226 end_len = find_last_bit(cpu->sve_vq_map, start_len);
6227 assert(end_len < start_len);
6228 }
6229 return end_len;
0df9142d
AJ
6230}
6231
0ab5953b
RH
6232/*
6233 * Given that SVE is enabled, return the vector length for EL.
6234 */
ced31551 6235uint32_t sve_zcr_len_for_el(CPUARMState *env, int el)
0ab5953b 6236{
2fc0cc0e 6237 ARMCPU *cpu = env_archcpu(env);
0ab5953b
RH
6238 uint32_t zcr_len = cpu->sve_max_vq - 1;
6239
6240 if (el <= 1) {
6241 zcr_len = MIN(zcr_len, 0xf & (uint32_t)env->vfp.zcr_el[1]);
6242 }
6a02a732 6243 if (el <= 2 && arm_feature(env, ARM_FEATURE_EL2)) {
0ab5953b
RH
6244 zcr_len = MIN(zcr_len, 0xf & (uint32_t)env->vfp.zcr_el[2]);
6245 }
6a02a732 6246 if (arm_feature(env, ARM_FEATURE_EL3)) {
0ab5953b
RH
6247 zcr_len = MIN(zcr_len, 0xf & (uint32_t)env->vfp.zcr_el[3]);
6248 }
0df9142d
AJ
6249
6250 return sve_zcr_get_valid_len(cpu, zcr_len);
0ab5953b
RH
6251}
6252
5be5e8ed
RH
6253static void zcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6254 uint64_t value)
6255{
0ab5953b
RH
6256 int cur_el = arm_current_el(env);
6257 int old_len = sve_zcr_len_for_el(env, cur_el);
6258 int new_len;
6259
5be5e8ed 6260 /* Bits other than [3:0] are RAZ/WI. */
7b351d98 6261 QEMU_BUILD_BUG_ON(ARM_MAX_VQ > 16);
5be5e8ed 6262 raw_write(env, ri, value & 0xf);
0ab5953b
RH
6263
6264 /*
6265 * Because we arrived here, we know both FP and SVE are enabled;
6266 * otherwise we would have trapped access to the ZCR_ELn register.
6267 */
6268 new_len = sve_zcr_len_for_el(env, cur_el);
6269 if (new_len < old_len) {
6270 aarch64_sve_narrow_vq(env, new_len + 1);
6271 }
5be5e8ed
RH
6272}
6273
6274static const ARMCPRegInfo zcr_el1_reginfo = {
6275 .name = "ZCR_EL1", .state = ARM_CP_STATE_AA64,
6276 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 0,
11d7870b 6277 .access = PL1_RW, .type = ARM_CP_SVE,
5be5e8ed
RH
6278 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[1]),
6279 .writefn = zcr_write, .raw_writefn = raw_write
6280};
6281
6282static const ARMCPRegInfo zcr_el2_reginfo = {
6283 .name = "ZCR_EL2", .state = ARM_CP_STATE_AA64,
6284 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 0,
11d7870b 6285 .access = PL2_RW, .type = ARM_CP_SVE,
5be5e8ed
RH
6286 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[2]),
6287 .writefn = zcr_write, .raw_writefn = raw_write
6288};
6289
6290static const ARMCPRegInfo zcr_no_el2_reginfo = {
6291 .name = "ZCR_EL2", .state = ARM_CP_STATE_AA64,
6292 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 0,
11d7870b 6293 .access = PL2_RW, .type = ARM_CP_SVE,
5be5e8ed
RH
6294 .readfn = arm_cp_read_zero, .writefn = arm_cp_write_ignore
6295};
6296
6297static const ARMCPRegInfo zcr_el3_reginfo = {
6298 .name = "ZCR_EL3", .state = ARM_CP_STATE_AA64,
6299 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 2, .opc2 = 0,
11d7870b 6300 .access = PL3_RW, .type = ARM_CP_SVE,
5be5e8ed
RH
6301 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[3]),
6302 .writefn = zcr_write, .raw_writefn = raw_write
6303};
6304
9ee98ce8
PM
6305void hw_watchpoint_update(ARMCPU *cpu, int n)
6306{
6307 CPUARMState *env = &cpu->env;
6308 vaddr len = 0;
6309 vaddr wvr = env->cp15.dbgwvr[n];
6310 uint64_t wcr = env->cp15.dbgwcr[n];
6311 int mask;
6312 int flags = BP_CPU | BP_STOP_BEFORE_ACCESS;
6313
6314 if (env->cpu_watchpoint[n]) {
6315 cpu_watchpoint_remove_by_ref(CPU(cpu), env->cpu_watchpoint[n]);
6316 env->cpu_watchpoint[n] = NULL;
6317 }
6318
6319 if (!extract64(wcr, 0, 1)) {
6320 /* E bit clear : watchpoint disabled */
6321 return;
6322 }
6323
6324 switch (extract64(wcr, 3, 2)) {
6325 case 0:
6326 /* LSC 00 is reserved and must behave as if the wp is disabled */
6327 return;
6328 case 1:
6329 flags |= BP_MEM_READ;
6330 break;
6331 case 2:
6332 flags |= BP_MEM_WRITE;
6333 break;
6334 case 3:
6335 flags |= BP_MEM_ACCESS;
6336 break;
6337 }
6338
6339 /* Attempts to use both MASK and BAS fields simultaneously are
6340 * CONSTRAINED UNPREDICTABLE; we opt to ignore BAS in this case,
6341 * thus generating a watchpoint for every byte in the masked region.
6342 */
6343 mask = extract64(wcr, 24, 4);
6344 if (mask == 1 || mask == 2) {
6345 /* Reserved values of MASK; we must act as if the mask value was
6346 * some non-reserved value, or as if the watchpoint were disabled.
6347 * We choose the latter.
6348 */
6349 return;
6350 } else if (mask) {
6351 /* Watchpoint covers an aligned area up to 2GB in size */
6352 len = 1ULL << mask;
6353 /* If masked bits in WVR are not zero it's CONSTRAINED UNPREDICTABLE
6354 * whether the watchpoint fires when the unmasked bits match; we opt
6355 * to generate the exceptions.
6356 */
6357 wvr &= ~(len - 1);
6358 } else {
6359 /* Watchpoint covers bytes defined by the byte address select bits */
6360 int bas = extract64(wcr, 5, 8);
6361 int basstart;
6362
9ee98ce8
PM
6363 if (extract64(wvr, 2, 1)) {
6364 /* Deprecated case of an only 4-aligned address. BAS[7:4] are
6365 * ignored, and BAS[3:0] define which bytes to watch.
6366 */
6367 bas &= 0xf;
6368 }
ae1111d4
RH
6369
6370 if (bas == 0) {
6371 /* This must act as if the watchpoint is disabled */
6372 return;
6373 }
6374
9ee98ce8
PM
6375 /* The BAS bits are supposed to be programmed to indicate a contiguous
6376 * range of bytes. Otherwise it is CONSTRAINED UNPREDICTABLE whether
6377 * we fire for each byte in the word/doubleword addressed by the WVR.
6378 * We choose to ignore any non-zero bits after the first range of 1s.
6379 */
6380 basstart = ctz32(bas);
6381 len = cto32(bas >> basstart);
6382 wvr += basstart;
6383 }
6384
6385 cpu_watchpoint_insert(CPU(cpu), wvr, len, flags,
6386 &env->cpu_watchpoint[n]);
6387}
6388
6389void hw_watchpoint_update_all(ARMCPU *cpu)
6390{
6391 int i;
6392 CPUARMState *env = &cpu->env;
6393
6394 /* Completely clear out existing QEMU watchpoints and our array, to
6395 * avoid possible stale entries following migration load.
6396 */
6397 cpu_watchpoint_remove_all(CPU(cpu), BP_CPU);
6398 memset(env->cpu_watchpoint, 0, sizeof(env->cpu_watchpoint));
6399
6400 for (i = 0; i < ARRAY_SIZE(cpu->env.cpu_watchpoint); i++) {
6401 hw_watchpoint_update(cpu, i);
6402 }
6403}
6404
6405static void dbgwvr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6406 uint64_t value)
6407{
2fc0cc0e 6408 ARMCPU *cpu = env_archcpu(env);
9ee98ce8
PM
6409 int i = ri->crm;
6410
6411 /* Bits [63:49] are hardwired to the value of bit [48]; that is, the
6412 * register reads and behaves as if values written are sign extended.
6413 * Bits [1:0] are RES0.
6414 */
6415 value = sextract64(value, 0, 49) & ~3ULL;
6416
6417 raw_write(env, ri, value);
6418 hw_watchpoint_update(cpu, i);
6419}
6420
6421static void dbgwcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6422 uint64_t value)
6423{
2fc0cc0e 6424 ARMCPU *cpu = env_archcpu(env);
9ee98ce8
PM
6425 int i = ri->crm;
6426
6427 raw_write(env, ri, value);
6428 hw_watchpoint_update(cpu, i);
6429}
6430
46747d15
PM
6431void hw_breakpoint_update(ARMCPU *cpu, int n)
6432{
6433 CPUARMState *env = &cpu->env;
6434 uint64_t bvr = env->cp15.dbgbvr[n];
6435 uint64_t bcr = env->cp15.dbgbcr[n];
6436 vaddr addr;
6437 int bt;
6438 int flags = BP_CPU;
6439
6440 if (env->cpu_breakpoint[n]) {
6441 cpu_breakpoint_remove_by_ref(CPU(cpu), env->cpu_breakpoint[n]);
6442 env->cpu_breakpoint[n] = NULL;
6443 }
6444
6445 if (!extract64(bcr, 0, 1)) {
6446 /* E bit clear : watchpoint disabled */
6447 return;
6448 }
6449
6450 bt = extract64(bcr, 20, 4);
6451
6452 switch (bt) {
6453 case 4: /* unlinked address mismatch (reserved if AArch64) */
6454 case 5: /* linked address mismatch (reserved if AArch64) */
6455 qemu_log_mask(LOG_UNIMP,
0221c8fd 6456 "arm: address mismatch breakpoint types not implemented\n");
46747d15
PM
6457 return;
6458 case 0: /* unlinked address match */
6459 case 1: /* linked address match */
6460 {
6461 /* Bits [63:49] are hardwired to the value of bit [48]; that is,
6462 * we behave as if the register was sign extended. Bits [1:0] are
6463 * RES0. The BAS field is used to allow setting breakpoints on 16
6464 * bit wide instructions; it is CONSTRAINED UNPREDICTABLE whether
6465 * a bp will fire if the addresses covered by the bp and the addresses
6466 * covered by the insn overlap but the insn doesn't start at the
6467 * start of the bp address range. We choose to require the insn and
6468 * the bp to have the same address. The constraints on writing to
6469 * BAS enforced in dbgbcr_write mean we have only four cases:
6470 * 0b0000 => no breakpoint
6471 * 0b0011 => breakpoint on addr
6472 * 0b1100 => breakpoint on addr + 2
6473 * 0b1111 => breakpoint on addr
6474 * See also figure D2-3 in the v8 ARM ARM (DDI0487A.c).
6475 */
6476 int bas = extract64(bcr, 5, 4);
6477 addr = sextract64(bvr, 0, 49) & ~3ULL;
6478 if (bas == 0) {
6479 return;
6480 }
6481 if (bas == 0xc) {
6482 addr += 2;
6483 }
6484 break;
6485 }
6486 case 2: /* unlinked context ID match */
6487 case 8: /* unlinked VMID match (reserved if no EL2) */
6488 case 10: /* unlinked context ID and VMID match (reserved if no EL2) */
6489 qemu_log_mask(LOG_UNIMP,
0221c8fd 6490 "arm: unlinked context breakpoint types not implemented\n");
46747d15
PM
6491 return;
6492 case 9: /* linked VMID match (reserved if no EL2) */
6493 case 11: /* linked context ID and VMID match (reserved if no EL2) */
6494 case 3: /* linked context ID match */
6495 default:
6496 /* We must generate no events for Linked context matches (unless
6497 * they are linked to by some other bp/wp, which is handled in
6498 * updates for the linking bp/wp). We choose to also generate no events
6499 * for reserved values.
6500 */
6501 return;
6502 }
6503
6504 cpu_breakpoint_insert(CPU(cpu), addr, flags, &env->cpu_breakpoint[n]);
6505}
6506
6507void hw_breakpoint_update_all(ARMCPU *cpu)
6508{
6509 int i;
6510 CPUARMState *env = &cpu->env;
6511
6512 /* Completely clear out existing QEMU breakpoints and our array, to
6513 * avoid possible stale entries following migration load.
6514 */
6515 cpu_breakpoint_remove_all(CPU(cpu), BP_CPU);
6516 memset(env->cpu_breakpoint, 0, sizeof(env->cpu_breakpoint));
6517
6518 for (i = 0; i < ARRAY_SIZE(cpu->env.cpu_breakpoint); i++) {
6519 hw_breakpoint_update(cpu, i);
6520 }
6521}
6522
6523static void dbgbvr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6524 uint64_t value)
6525{
2fc0cc0e 6526 ARMCPU *cpu = env_archcpu(env);
46747d15
PM
6527 int i = ri->crm;
6528
6529 raw_write(env, ri, value);
6530 hw_breakpoint_update(cpu, i);
6531}
6532
6533static void dbgbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6534 uint64_t value)
6535{
2fc0cc0e 6536 ARMCPU *cpu = env_archcpu(env);
46747d15
PM
6537 int i = ri->crm;
6538
6539 /* BAS[3] is a read-only copy of BAS[2], and BAS[1] a read-only
6540 * copy of BAS[0].
6541 */
6542 value = deposit64(value, 6, 1, extract64(value, 5, 1));
6543 value = deposit64(value, 8, 1, extract64(value, 7, 1));
6544
6545 raw_write(env, ri, value);
6546 hw_breakpoint_update(cpu, i);
6547}
6548
50300698 6549static void define_debug_regs(ARMCPU *cpu)
0b45451e 6550{
50300698
PM
6551 /* Define v7 and v8 architectural debug registers.
6552 * These are just dummy implementations for now.
0b45451e
PM
6553 */
6554 int i;
3ff6fc91 6555 int wrps, brps, ctx_cmps;
48eb3ae6
PM
6556 ARMCPRegInfo dbgdidr = {
6557 .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0,
d6c8cf81 6558 .access = PL0_R, .accessfn = access_tda,
4426d361 6559 .type = ARM_CP_CONST, .resetvalue = cpu->isar.dbgdidr,
48eb3ae6
PM
6560 };
6561
3ff6fc91 6562 /* Note that all these register fields hold "number of Xs minus 1". */
88ce6c6e
PM
6563 brps = arm_num_brps(cpu);
6564 wrps = arm_num_wrps(cpu);
6565 ctx_cmps = arm_num_ctx_cmps(cpu);
3ff6fc91
PM
6566
6567 assert(ctx_cmps <= brps);
48eb3ae6 6568
48eb3ae6 6569 define_one_arm_cp_reg(cpu, &dbgdidr);
50300698
PM
6570 define_arm_cp_regs(cpu, debug_cp_reginfo);
6571
6572 if (arm_feature(&cpu->env, ARM_FEATURE_LPAE)) {
6573 define_arm_cp_regs(cpu, debug_lpae_cp_reginfo);
6574 }
6575
88ce6c6e 6576 for (i = 0; i < brps; i++) {
0b45451e 6577 ARMCPRegInfo dbgregs[] = {
10aae104
PM
6578 { .name = "DBGBVR", .state = ARM_CP_STATE_BOTH,
6579 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 4,
d6c8cf81 6580 .access = PL1_RW, .accessfn = access_tda,
46747d15
PM
6581 .fieldoffset = offsetof(CPUARMState, cp15.dbgbvr[i]),
6582 .writefn = dbgbvr_write, .raw_writefn = raw_write
6583 },
10aae104
PM
6584 { .name = "DBGBCR", .state = ARM_CP_STATE_BOTH,
6585 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 5,
d6c8cf81 6586 .access = PL1_RW, .accessfn = access_tda,
46747d15
PM
6587 .fieldoffset = offsetof(CPUARMState, cp15.dbgbcr[i]),
6588 .writefn = dbgbcr_write, .raw_writefn = raw_write
6589 },
48eb3ae6
PM
6590 REGINFO_SENTINEL
6591 };
6592 define_arm_cp_regs(cpu, dbgregs);
6593 }
6594
88ce6c6e 6595 for (i = 0; i < wrps; i++) {
48eb3ae6 6596 ARMCPRegInfo dbgregs[] = {
10aae104
PM
6597 { .name = "DBGWVR", .state = ARM_CP_STATE_BOTH,
6598 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 6,
d6c8cf81 6599 .access = PL1_RW, .accessfn = access_tda,
9ee98ce8
PM
6600 .fieldoffset = offsetof(CPUARMState, cp15.dbgwvr[i]),
6601 .writefn = dbgwvr_write, .raw_writefn = raw_write
6602 },
10aae104
PM
6603 { .name = "DBGWCR", .state = ARM_CP_STATE_BOTH,
6604 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 7,
d6c8cf81 6605 .access = PL1_RW, .accessfn = access_tda,
9ee98ce8
PM
6606 .fieldoffset = offsetof(CPUARMState, cp15.dbgwcr[i]),
6607 .writefn = dbgwcr_write, .raw_writefn = raw_write
6608 },
6609 REGINFO_SENTINEL
0b45451e
PM
6610 };
6611 define_arm_cp_regs(cpu, dbgregs);
6612 }
6613}
6614
24183fb6
PM
6615static void define_pmu_regs(ARMCPU *cpu)
6616{
6617 /*
6618 * v7 performance monitor control register: same implementor
6619 * field as main ID register, and we implement four counters in
6620 * addition to the cycle count register.
6621 */
6622 unsigned int i, pmcrn = 4;
6623 ARMCPRegInfo pmcr = {
6624 .name = "PMCR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 0,
6625 .access = PL0_RW,
6626 .type = ARM_CP_IO | ARM_CP_ALIAS,
6627 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcr),
6628 .accessfn = pmreg_access, .writefn = pmcr_write,
6629 .raw_writefn = raw_write,
6630 };
6631 ARMCPRegInfo pmcr64 = {
6632 .name = "PMCR_EL0", .state = ARM_CP_STATE_AA64,
6633 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 0,
6634 .access = PL0_RW, .accessfn = pmreg_access,
6635 .type = ARM_CP_IO,
6636 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcr),
62d96ff4
PM
6637 .resetvalue = (cpu->midr & 0xff000000) | (pmcrn << PMCRN_SHIFT) |
6638 PMCRLC,
24183fb6
PM
6639 .writefn = pmcr_write, .raw_writefn = raw_write,
6640 };
6641 define_one_arm_cp_reg(cpu, &pmcr);
6642 define_one_arm_cp_reg(cpu, &pmcr64);
6643 for (i = 0; i < pmcrn; i++) {
6644 char *pmevcntr_name = g_strdup_printf("PMEVCNTR%d", i);
6645 char *pmevcntr_el0_name = g_strdup_printf("PMEVCNTR%d_EL0", i);
6646 char *pmevtyper_name = g_strdup_printf("PMEVTYPER%d", i);
6647 char *pmevtyper_el0_name = g_strdup_printf("PMEVTYPER%d_EL0", i);
6648 ARMCPRegInfo pmev_regs[] = {
6649 { .name = pmevcntr_name, .cp = 15, .crn = 14,
6650 .crm = 8 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7,
6651 .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS,
6652 .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn,
6653 .accessfn = pmreg_access },
6654 { .name = pmevcntr_el0_name, .state = ARM_CP_STATE_AA64,
6655 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 8 | (3 & (i >> 3)),
6656 .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access,
6657 .type = ARM_CP_IO,
6658 .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn,
6659 .raw_readfn = pmevcntr_rawread,
6660 .raw_writefn = pmevcntr_rawwrite },
6661 { .name = pmevtyper_name, .cp = 15, .crn = 14,
6662 .crm = 12 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7,
6663 .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS,
6664 .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn,
6665 .accessfn = pmreg_access },
6666 { .name = pmevtyper_el0_name, .state = ARM_CP_STATE_AA64,
6667 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 12 | (3 & (i >> 3)),
6668 .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access,
6669 .type = ARM_CP_IO,
6670 .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn,
6671 .raw_writefn = pmevtyper_rawwrite },
6672 REGINFO_SENTINEL
6673 };
6674 define_arm_cp_regs(cpu, pmev_regs);
6675 g_free(pmevcntr_name);
6676 g_free(pmevcntr_el0_name);
6677 g_free(pmevtyper_name);
6678 g_free(pmevtyper_el0_name);
6679 }
a6179538 6680 if (cpu_isar_feature(aa32_pmu_8_1, cpu)) {
24183fb6
PM
6681 ARMCPRegInfo v81_pmu_regs[] = {
6682 { .name = "PMCEID2", .state = ARM_CP_STATE_AA32,
6683 .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 4,
6684 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6685 .resetvalue = extract64(cpu->pmceid0, 32, 32) },
6686 { .name = "PMCEID3", .state = ARM_CP_STATE_AA32,
6687 .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 5,
6688 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6689 .resetvalue = extract64(cpu->pmceid1, 32, 32) },
6690 REGINFO_SENTINEL
6691 };
6692 define_arm_cp_regs(cpu, v81_pmu_regs);
6693 }
15dd1ebd
PM
6694 if (cpu_isar_feature(any_pmu_8_4, cpu)) {
6695 static const ARMCPRegInfo v84_pmmir = {
6696 .name = "PMMIR_EL1", .state = ARM_CP_STATE_BOTH,
6697 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 6,
6698 .access = PL1_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6699 .resetvalue = 0
6700 };
6701 define_one_arm_cp_reg(cpu, &v84_pmmir);
6702 }
24183fb6
PM
6703}
6704
96a8b92e
PM
6705/* We don't know until after realize whether there's a GICv3
6706 * attached, and that is what registers the gicv3 sysregs.
6707 * So we have to fill in the GIC fields in ID_PFR/ID_PFR1_EL1/ID_AA64PFR0_EL1
6708 * at runtime.
6709 */
6710static uint64_t id_pfr1_read(CPUARMState *env, const ARMCPRegInfo *ri)
6711{
2fc0cc0e 6712 ARMCPU *cpu = env_archcpu(env);
8a130a7b 6713 uint64_t pfr1 = cpu->isar.id_pfr1;
96a8b92e
PM
6714
6715 if (env->gicv3state) {
6716 pfr1 |= 1 << 28;
6717 }
6718 return pfr1;
6719}
6720
976b99b6 6721#ifndef CONFIG_USER_ONLY
96a8b92e
PM
6722static uint64_t id_aa64pfr0_read(CPUARMState *env, const ARMCPRegInfo *ri)
6723{
2fc0cc0e 6724 ARMCPU *cpu = env_archcpu(env);
47576b94 6725 uint64_t pfr0 = cpu->isar.id_aa64pfr0;
96a8b92e
PM
6726
6727 if (env->gicv3state) {
6728 pfr0 |= 1 << 24;
6729 }
6730 return pfr0;
6731}
976b99b6 6732#endif
96a8b92e 6733
2d7137c1 6734/* Shared logic between LORID and the rest of the LOR* registers.
9bd268ba 6735 * Secure state exclusion has already been dealt with.
2d7137c1 6736 */
9bd268ba
RDC
6737static CPAccessResult access_lor_ns(CPUARMState *env,
6738 const ARMCPRegInfo *ri, bool isread)
2d7137c1
RH
6739{
6740 int el = arm_current_el(env);
6741
6742 if (el < 2 && (arm_hcr_el2_eff(env) & HCR_TLOR)) {
6743 return CP_ACCESS_TRAP_EL2;
6744 }
6745 if (el < 3 && (env->cp15.scr_el3 & SCR_TLOR)) {
6746 return CP_ACCESS_TRAP_EL3;
6747 }
6748 return CP_ACCESS_OK;
6749}
6750
2d7137c1
RH
6751static CPAccessResult access_lor_other(CPUARMState *env,
6752 const ARMCPRegInfo *ri, bool isread)
6753{
6754 if (arm_is_secure_below_el3(env)) {
6755 /* Access denied in secure mode. */
6756 return CP_ACCESS_TRAP;
6757 }
9bd268ba 6758 return access_lor_ns(env, ri, isread);
2d7137c1
RH
6759}
6760
d8564ee4
RH
6761/*
6762 * A trivial implementation of ARMv8.1-LOR leaves all of these
6763 * registers fixed at 0, which indicates that there are zero
6764 * supported Limited Ordering regions.
6765 */
6766static const ARMCPRegInfo lor_reginfo[] = {
6767 { .name = "LORSA_EL1", .state = ARM_CP_STATE_AA64,
6768 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 0,
6769 .access = PL1_RW, .accessfn = access_lor_other,
6770 .type = ARM_CP_CONST, .resetvalue = 0 },
6771 { .name = "LOREA_EL1", .state = ARM_CP_STATE_AA64,
6772 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 1,
6773 .access = PL1_RW, .accessfn = access_lor_other,
6774 .type = ARM_CP_CONST, .resetvalue = 0 },
6775 { .name = "LORN_EL1", .state = ARM_CP_STATE_AA64,
6776 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 2,
6777 .access = PL1_RW, .accessfn = access_lor_other,
6778 .type = ARM_CP_CONST, .resetvalue = 0 },
6779 { .name = "LORC_EL1", .state = ARM_CP_STATE_AA64,
6780 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 3,
6781 .access = PL1_RW, .accessfn = access_lor_other,
6782 .type = ARM_CP_CONST, .resetvalue = 0 },
6783 { .name = "LORID_EL1", .state = ARM_CP_STATE_AA64,
6784 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 7,
9bd268ba 6785 .access = PL1_R, .accessfn = access_lor_ns,
d8564ee4
RH
6786 .type = ARM_CP_CONST, .resetvalue = 0 },
6787 REGINFO_SENTINEL
6788};
6789
967aa94f
RH
6790#ifdef TARGET_AARCH64
6791static CPAccessResult access_pauth(CPUARMState *env, const ARMCPRegInfo *ri,
6792 bool isread)
6793{
6794 int el = arm_current_el(env);
6795
6796 if (el < 2 &&
6797 arm_feature(env, ARM_FEATURE_EL2) &&
6798 !(arm_hcr_el2_eff(env) & HCR_APK)) {
6799 return CP_ACCESS_TRAP_EL2;
6800 }
6801 if (el < 3 &&
6802 arm_feature(env, ARM_FEATURE_EL3) &&
6803 !(env->cp15.scr_el3 & SCR_APK)) {
6804 return CP_ACCESS_TRAP_EL3;
6805 }
6806 return CP_ACCESS_OK;
6807}
6808
6809static const ARMCPRegInfo pauth_reginfo[] = {
6810 { .name = "APDAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6811 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 0,
6812 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 6813 .fieldoffset = offsetof(CPUARMState, keys.apda.lo) },
967aa94f
RH
6814 { .name = "APDAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6815 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 1,
6816 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 6817 .fieldoffset = offsetof(CPUARMState, keys.apda.hi) },
967aa94f
RH
6818 { .name = "APDBKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6819 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 2,
6820 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 6821 .fieldoffset = offsetof(CPUARMState, keys.apdb.lo) },
967aa94f
RH
6822 { .name = "APDBKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6823 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 3,
6824 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 6825 .fieldoffset = offsetof(CPUARMState, keys.apdb.hi) },
967aa94f
RH
6826 { .name = "APGAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6827 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 0,
6828 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 6829 .fieldoffset = offsetof(CPUARMState, keys.apga.lo) },
967aa94f
RH
6830 { .name = "APGAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6831 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 1,
6832 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 6833 .fieldoffset = offsetof(CPUARMState, keys.apga.hi) },
967aa94f
RH
6834 { .name = "APIAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6835 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 0,
6836 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 6837 .fieldoffset = offsetof(CPUARMState, keys.apia.lo) },
967aa94f
RH
6838 { .name = "APIAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6839 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 1,
6840 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 6841 .fieldoffset = offsetof(CPUARMState, keys.apia.hi) },
967aa94f
RH
6842 { .name = "APIBKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6843 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 2,
6844 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 6845 .fieldoffset = offsetof(CPUARMState, keys.apib.lo) },
967aa94f
RH
6846 { .name = "APIBKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6847 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 3,
6848 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 6849 .fieldoffset = offsetof(CPUARMState, keys.apib.hi) },
967aa94f
RH
6850 REGINFO_SENTINEL
6851};
de390645
RH
6852
6853static uint64_t rndr_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
6854{
6855 Error *err = NULL;
6856 uint64_t ret;
6857
6858 /* Success sets NZCV = 0000. */
6859 env->NF = env->CF = env->VF = 0, env->ZF = 1;
6860
6861 if (qemu_guest_getrandom(&ret, sizeof(ret), &err) < 0) {
6862 /*
6863 * ??? Failed, for unknown reasons in the crypto subsystem.
6864 * The best we can do is log the reason and return the
6865 * timed-out indication to the guest. There is no reason
6866 * we know to expect this failure to be transitory, so the
6867 * guest may well hang retrying the operation.
6868 */
6869 qemu_log_mask(LOG_UNIMP, "%s: Crypto failure: %s",
6870 ri->name, error_get_pretty(err));
6871 error_free(err);
6872
6873 env->ZF = 0; /* NZCF = 0100 */
6874 return 0;
6875 }
6876 return ret;
6877}
6878
6879/* We do not support re-seeding, so the two registers operate the same. */
6880static const ARMCPRegInfo rndr_reginfo[] = {
6881 { .name = "RNDR", .state = ARM_CP_STATE_AA64,
6882 .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END | ARM_CP_IO,
6883 .opc0 = 3, .opc1 = 3, .crn = 2, .crm = 4, .opc2 = 0,
6884 .access = PL0_R, .readfn = rndr_readfn },
6885 { .name = "RNDRRS", .state = ARM_CP_STATE_AA64,
6886 .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END | ARM_CP_IO,
6887 .opc0 = 3, .opc1 = 3, .crn = 2, .crm = 4, .opc2 = 1,
6888 .access = PL0_R, .readfn = rndr_readfn },
6889 REGINFO_SENTINEL
6890};
0d57b499
BM
6891
6892#ifndef CONFIG_USER_ONLY
6893static void dccvap_writefn(CPUARMState *env, const ARMCPRegInfo *opaque,
6894 uint64_t value)
6895{
6896 ARMCPU *cpu = env_archcpu(env);
6897 /* CTR_EL0 System register -> DminLine, bits [19:16] */
6898 uint64_t dline_size = 4 << ((cpu->ctr >> 16) & 0xF);
6899 uint64_t vaddr_in = (uint64_t) value;
6900 uint64_t vaddr = vaddr_in & ~(dline_size - 1);
6901 void *haddr;
6902 int mem_idx = cpu_mmu_index(env, false);
6903
6904 /* This won't be crossing page boundaries */
6905 haddr = probe_read(env, vaddr, dline_size, mem_idx, GETPC());
6906 if (haddr) {
6907
6908 ram_addr_t offset;
6909 MemoryRegion *mr;
6910
6911 /* RCU lock is already being held */
6912 mr = memory_region_from_host(haddr, &offset);
6913
6914 if (mr) {
4dfe59d1 6915 memory_region_writeback(mr, offset, dline_size);
0d57b499
BM
6916 }
6917 }
6918}
6919
6920static const ARMCPRegInfo dcpop_reg[] = {
6921 { .name = "DC_CVAP", .state = ARM_CP_STATE_AA64,
6922 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 1,
6923 .access = PL0_W, .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END,
1bed4d2e 6924 .accessfn = aa64_cacheop_poc_access, .writefn = dccvap_writefn },
0d57b499
BM
6925 REGINFO_SENTINEL
6926};
6927
6928static const ARMCPRegInfo dcpodp_reg[] = {
6929 { .name = "DC_CVADP", .state = ARM_CP_STATE_AA64,
6930 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 1,
6931 .access = PL0_W, .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END,
1bed4d2e 6932 .accessfn = aa64_cacheop_poc_access, .writefn = dccvap_writefn },
0d57b499
BM
6933 REGINFO_SENTINEL
6934};
6935#endif /*CONFIG_USER_ONLY*/
6936
4b779ceb
RH
6937static CPAccessResult access_aa64_tid5(CPUARMState *env, const ARMCPRegInfo *ri,
6938 bool isread)
6939{
6940 if ((arm_current_el(env) < 2) && (arm_hcr_el2_eff(env) & HCR_TID5)) {
6941 return CP_ACCESS_TRAP_EL2;
6942 }
6943
6944 return CP_ACCESS_OK;
6945}
6946
6947static CPAccessResult access_mte(CPUARMState *env, const ARMCPRegInfo *ri,
6948 bool isread)
6949{
6950 int el = arm_current_el(env);
6951
4301acd7
RH
6952 if (el < 2 && arm_feature(env, ARM_FEATURE_EL2)) {
6953 uint64_t hcr = arm_hcr_el2_eff(env);
6954 if (!(hcr & HCR_ATA) && (!(hcr & HCR_E2H) || !(hcr & HCR_TGE))) {
6955 return CP_ACCESS_TRAP_EL2;
6956 }
4b779ceb
RH
6957 }
6958 if (el < 3 &&
6959 arm_feature(env, ARM_FEATURE_EL3) &&
6960 !(env->cp15.scr_el3 & SCR_ATA)) {
6961 return CP_ACCESS_TRAP_EL3;
6962 }
6963 return CP_ACCESS_OK;
6964}
6965
6966static uint64_t tco_read(CPUARMState *env, const ARMCPRegInfo *ri)
6967{
6968 return env->pstate & PSTATE_TCO;
6969}
6970
6971static void tco_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
6972{
6973 env->pstate = (env->pstate & ~PSTATE_TCO) | (val & PSTATE_TCO);
6974}
6975
6976static const ARMCPRegInfo mte_reginfo[] = {
6977 { .name = "TFSRE0_EL1", .state = ARM_CP_STATE_AA64,
6978 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 6, .opc2 = 1,
6979 .access = PL1_RW, .accessfn = access_mte,
6980 .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[0]) },
6981 { .name = "TFSR_EL1", .state = ARM_CP_STATE_AA64,
6982 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 6, .opc2 = 0,
6983 .access = PL1_RW, .accessfn = access_mte,
6984 .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[1]) },
6985 { .name = "TFSR_EL2", .state = ARM_CP_STATE_AA64,
6986 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 6, .opc2 = 0,
6987 .access = PL2_RW, .accessfn = access_mte,
6988 .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[2]) },
6989 { .name = "TFSR_EL3", .state = ARM_CP_STATE_AA64,
6990 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 6, .opc2 = 0,
6991 .access = PL3_RW,
6992 .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[3]) },
6993 { .name = "RGSR_EL1", .state = ARM_CP_STATE_AA64,
6994 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 5,
6995 .access = PL1_RW, .accessfn = access_mte,
6996 .fieldoffset = offsetof(CPUARMState, cp15.rgsr_el1) },
6997 { .name = "GCR_EL1", .state = ARM_CP_STATE_AA64,
6998 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 6,
6999 .access = PL1_RW, .accessfn = access_mte,
7000 .fieldoffset = offsetof(CPUARMState, cp15.gcr_el1) },
7001 { .name = "GMID_EL1", .state = ARM_CP_STATE_AA64,
7002 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 4,
7003 .access = PL1_R, .accessfn = access_aa64_tid5,
7004 .type = ARM_CP_CONST, .resetvalue = GMID_EL1_BS },
7005 { .name = "TCO", .state = ARM_CP_STATE_AA64,
7006 .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 7,
7007 .type = ARM_CP_NO_RAW,
7008 .access = PL0_RW, .readfn = tco_read, .writefn = tco_write },
5463df16
RH
7009 { .name = "DC_IGVAC", .state = ARM_CP_STATE_AA64,
7010 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 3,
7011 .type = ARM_CP_NOP, .access = PL1_W,
7012 .accessfn = aa64_cacheop_poc_access },
7013 { .name = "DC_IGSW", .state = ARM_CP_STATE_AA64,
7014 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 4,
7015 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7016 { .name = "DC_IGDVAC", .state = ARM_CP_STATE_AA64,
7017 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 5,
7018 .type = ARM_CP_NOP, .access = PL1_W,
7019 .accessfn = aa64_cacheop_poc_access },
7020 { .name = "DC_IGDSW", .state = ARM_CP_STATE_AA64,
7021 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 6,
7022 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7023 { .name = "DC_CGSW", .state = ARM_CP_STATE_AA64,
7024 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 4,
7025 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7026 { .name = "DC_CGDSW", .state = ARM_CP_STATE_AA64,
7027 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 6,
7028 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7029 { .name = "DC_CIGSW", .state = ARM_CP_STATE_AA64,
7030 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 4,
7031 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7032 { .name = "DC_CIGDSW", .state = ARM_CP_STATE_AA64,
7033 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 6,
7034 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
4b779ceb
RH
7035 REGINFO_SENTINEL
7036};
7037
7038static const ARMCPRegInfo mte_tco_ro_reginfo[] = {
7039 { .name = "TCO", .state = ARM_CP_STATE_AA64,
7040 .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 7,
7041 .type = ARM_CP_CONST, .access = PL0_RW, },
7042 REGINFO_SENTINEL
7043};
5463df16
RH
7044
7045static const ARMCPRegInfo mte_el0_cacheop_reginfo[] = {
7046 { .name = "DC_CGVAC", .state = ARM_CP_STATE_AA64,
7047 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 3,
7048 .type = ARM_CP_NOP, .access = PL0_W,
7049 .accessfn = aa64_cacheop_poc_access },
7050 { .name = "DC_CGDVAC", .state = ARM_CP_STATE_AA64,
7051 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 5,
7052 .type = ARM_CP_NOP, .access = PL0_W,
7053 .accessfn = aa64_cacheop_poc_access },
7054 { .name = "DC_CGVAP", .state = ARM_CP_STATE_AA64,
7055 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 3,
7056 .type = ARM_CP_NOP, .access = PL0_W,
7057 .accessfn = aa64_cacheop_poc_access },
7058 { .name = "DC_CGDVAP", .state = ARM_CP_STATE_AA64,
7059 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 5,
7060 .type = ARM_CP_NOP, .access = PL0_W,
7061 .accessfn = aa64_cacheop_poc_access },
7062 { .name = "DC_CGVADP", .state = ARM_CP_STATE_AA64,
7063 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 3,
7064 .type = ARM_CP_NOP, .access = PL0_W,
7065 .accessfn = aa64_cacheop_poc_access },
7066 { .name = "DC_CGDVADP", .state = ARM_CP_STATE_AA64,
7067 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 5,
7068 .type = ARM_CP_NOP, .access = PL0_W,
7069 .accessfn = aa64_cacheop_poc_access },
7070 { .name = "DC_CIGVAC", .state = ARM_CP_STATE_AA64,
7071 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 3,
7072 .type = ARM_CP_NOP, .access = PL0_W,
7073 .accessfn = aa64_cacheop_poc_access },
7074 { .name = "DC_CIGDVAC", .state = ARM_CP_STATE_AA64,
7075 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 5,
7076 .type = ARM_CP_NOP, .access = PL0_W,
7077 .accessfn = aa64_cacheop_poc_access },
eb821168
RH
7078 { .name = "DC_GVA", .state = ARM_CP_STATE_AA64,
7079 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 3,
7080 .access = PL0_W, .type = ARM_CP_DC_GVA,
7081#ifndef CONFIG_USER_ONLY
7082 /* Avoid overhead of an access check that always passes in user-mode */
7083 .accessfn = aa64_zva_access,
7084#endif
7085 },
7086 { .name = "DC_GZVA", .state = ARM_CP_STATE_AA64,
7087 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 4,
7088 .access = PL0_W, .type = ARM_CP_DC_GZVA,
7089#ifndef CONFIG_USER_ONLY
7090 /* Avoid overhead of an access check that always passes in user-mode */
7091 .accessfn = aa64_zva_access,
7092#endif
7093 },
5463df16
RH
7094 REGINFO_SENTINEL
7095};
7096
967aa94f
RH
7097#endif
7098
cb570bd3
RH
7099static CPAccessResult access_predinv(CPUARMState *env, const ARMCPRegInfo *ri,
7100 bool isread)
7101{
7102 int el = arm_current_el(env);
7103
7104 if (el == 0) {
7105 uint64_t sctlr = arm_sctlr(env, el);
7106 if (!(sctlr & SCTLR_EnRCTX)) {
7107 return CP_ACCESS_TRAP;
7108 }
7109 } else if (el == 1) {
7110 uint64_t hcr = arm_hcr_el2_eff(env);
7111 if (hcr & HCR_NV) {
7112 return CP_ACCESS_TRAP_EL2;
7113 }
7114 }
7115 return CP_ACCESS_OK;
7116}
7117
7118static const ARMCPRegInfo predinv_reginfo[] = {
7119 { .name = "CFP_RCTX", .state = ARM_CP_STATE_AA64,
7120 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 4,
7121 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7122 { .name = "DVP_RCTX", .state = ARM_CP_STATE_AA64,
7123 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 5,
7124 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7125 { .name = "CPP_RCTX", .state = ARM_CP_STATE_AA64,
7126 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 7,
7127 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7128 /*
7129 * Note the AArch32 opcodes have a different OPC1.
7130 */
7131 { .name = "CFPRCTX", .state = ARM_CP_STATE_AA32,
7132 .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 4,
7133 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7134 { .name = "DVPRCTX", .state = ARM_CP_STATE_AA32,
7135 .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 5,
7136 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7137 { .name = "CPPRCTX", .state = ARM_CP_STATE_AA32,
7138 .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 7,
7139 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7140 REGINFO_SENTINEL
7141};
7142
957e6155
PM
7143static uint64_t ccsidr2_read(CPUARMState *env, const ARMCPRegInfo *ri)
7144{
7145 /* Read the high 32 bits of the current CCSIDR */
7146 return extract64(ccsidr_read(env, ri), 32, 32);
7147}
7148
7149static const ARMCPRegInfo ccsidr2_reginfo[] = {
7150 { .name = "CCSIDR2", .state = ARM_CP_STATE_BOTH,
7151 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 2,
7152 .access = PL1_R,
7153 .accessfn = access_aa64_tid2,
7154 .readfn = ccsidr2_read, .type = ARM_CP_NO_RAW },
7155 REGINFO_SENTINEL
7156};
7157
6a4ef4e5
MZ
7158static CPAccessResult access_aa64_tid3(CPUARMState *env, const ARMCPRegInfo *ri,
7159 bool isread)
7160{
7161 if ((arm_current_el(env) < 2) && (arm_hcr_el2_eff(env) & HCR_TID3)) {
7162 return CP_ACCESS_TRAP_EL2;
7163 }
7164
7165 return CP_ACCESS_OK;
7166}
7167
7168static CPAccessResult access_aa32_tid3(CPUARMState *env, const ARMCPRegInfo *ri,
7169 bool isread)
7170{
7171 if (arm_feature(env, ARM_FEATURE_V8)) {
7172 return access_aa64_tid3(env, ri, isread);
7173 }
7174
7175 return CP_ACCESS_OK;
7176}
7177
f96f3d5f
MZ
7178static CPAccessResult access_jazelle(CPUARMState *env, const ARMCPRegInfo *ri,
7179 bool isread)
7180{
7181 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID0)) {
7182 return CP_ACCESS_TRAP_EL2;
7183 }
7184
7185 return CP_ACCESS_OK;
7186}
7187
7188static const ARMCPRegInfo jazelle_regs[] = {
7189 { .name = "JIDR",
7190 .cp = 14, .crn = 0, .crm = 0, .opc1 = 7, .opc2 = 0,
7191 .access = PL1_R, .accessfn = access_jazelle,
7192 .type = ARM_CP_CONST, .resetvalue = 0 },
7193 { .name = "JOSCR",
7194 .cp = 14, .crn = 1, .crm = 0, .opc1 = 7, .opc2 = 0,
7195 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
7196 { .name = "JMCR",
7197 .cp = 14, .crn = 2, .crm = 0, .opc1 = 7, .opc2 = 0,
7198 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
7199 REGINFO_SENTINEL
7200};
7201
e2a1a461
RH
7202static const ARMCPRegInfo vhe_reginfo[] = {
7203 { .name = "CONTEXTIDR_EL2", .state = ARM_CP_STATE_AA64,
7204 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 1,
7205 .access = PL2_RW,
7206 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[2]) },
ed30da8e
RH
7207 { .name = "TTBR1_EL2", .state = ARM_CP_STATE_AA64,
7208 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 1,
7209 .access = PL2_RW, .writefn = vmsa_tcr_ttbr_el2_write,
7210 .fieldoffset = offsetof(CPUARMState, cp15.ttbr1_el[2]) },
8c94b071
RH
7211#ifndef CONFIG_USER_ONLY
7212 { .name = "CNTHV_CVAL_EL2", .state = ARM_CP_STATE_AA64,
7213 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 2,
7214 .fieldoffset =
7215 offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYPVIRT].cval),
7216 .type = ARM_CP_IO, .access = PL2_RW,
7217 .writefn = gt_hv_cval_write, .raw_writefn = raw_write },
7218 { .name = "CNTHV_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
7219 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 0,
7220 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
7221 .resetfn = gt_hv_timer_reset,
7222 .readfn = gt_hv_tval_read, .writefn = gt_hv_tval_write },
7223 { .name = "CNTHV_CTL_EL2", .state = ARM_CP_STATE_BOTH,
7224 .type = ARM_CP_IO,
7225 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 1,
7226 .access = PL2_RW,
7227 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYPVIRT].ctl),
7228 .writefn = gt_hv_ctl_write, .raw_writefn = raw_write },
bb5972e4
RH
7229 { .name = "CNTP_CTL_EL02", .state = ARM_CP_STATE_AA64,
7230 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 1,
7231 .type = ARM_CP_IO | ARM_CP_ALIAS,
7232 .access = PL2_RW, .accessfn = e2h_access,
7233 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
7234 .writefn = gt_phys_ctl_write, .raw_writefn = raw_write },
7235 { .name = "CNTV_CTL_EL02", .state = ARM_CP_STATE_AA64,
7236 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 1,
7237 .type = ARM_CP_IO | ARM_CP_ALIAS,
7238 .access = PL2_RW, .accessfn = e2h_access,
7239 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
7240 .writefn = gt_virt_ctl_write, .raw_writefn = raw_write },
7241 { .name = "CNTP_TVAL_EL02", .state = ARM_CP_STATE_AA64,
7242 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 0,
7243 .type = ARM_CP_NO_RAW | ARM_CP_IO | ARM_CP_ALIAS,
7244 .access = PL2_RW, .accessfn = e2h_access,
7245 .readfn = gt_phys_tval_read, .writefn = gt_phys_tval_write },
7246 { .name = "CNTV_TVAL_EL02", .state = ARM_CP_STATE_AA64,
7247 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 0,
7248 .type = ARM_CP_NO_RAW | ARM_CP_IO | ARM_CP_ALIAS,
7249 .access = PL2_RW, .accessfn = e2h_access,
7250 .readfn = gt_virt_tval_read, .writefn = gt_virt_tval_write },
7251 { .name = "CNTP_CVAL_EL02", .state = ARM_CP_STATE_AA64,
7252 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 2,
7253 .type = ARM_CP_IO | ARM_CP_ALIAS,
7254 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
7255 .access = PL2_RW, .accessfn = e2h_access,
7256 .writefn = gt_phys_cval_write, .raw_writefn = raw_write },
7257 { .name = "CNTV_CVAL_EL02", .state = ARM_CP_STATE_AA64,
7258 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 2,
7259 .type = ARM_CP_IO | ARM_CP_ALIAS,
7260 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
7261 .access = PL2_RW, .accessfn = e2h_access,
7262 .writefn = gt_virt_cval_write, .raw_writefn = raw_write },
8c94b071 7263#endif
e2a1a461
RH
7264 REGINFO_SENTINEL
7265};
7266
04b07d29
RH
7267#ifndef CONFIG_USER_ONLY
7268static const ARMCPRegInfo ats1e1_reginfo[] = {
7269 { .name = "AT_S1E1R", .state = ARM_CP_STATE_AA64,
7270 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 0,
7271 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7272 .writefn = ats_write64 },
7273 { .name = "AT_S1E1W", .state = ARM_CP_STATE_AA64,
7274 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 1,
7275 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7276 .writefn = ats_write64 },
7277 REGINFO_SENTINEL
7278};
7279
7280static const ARMCPRegInfo ats1cp_reginfo[] = {
7281 { .name = "ATS1CPRP",
7282 .cp = 15, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 0,
7283 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7284 .writefn = ats_write },
7285 { .name = "ATS1CPWP",
7286 .cp = 15, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 1,
7287 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7288 .writefn = ats_write },
7289 REGINFO_SENTINEL
7290};
7291#endif
7292
f6287c24
PM
7293/*
7294 * ACTLR2 and HACTLR2 map to ACTLR_EL1[63:32] and
7295 * ACTLR_EL2[63:32]. They exist only if the ID_MMFR4.AC2 field
7296 * is non-zero, which is never for ARMv7, optionally in ARMv8
7297 * and mandatorily for ARMv8.2 and up.
7298 * ACTLR2 is banked for S and NS if EL3 is AArch32. Since QEMU's
7299 * implementation is RAZ/WI we can ignore this detail, as we
7300 * do for ACTLR.
7301 */
7302static const ARMCPRegInfo actlr2_hactlr2_reginfo[] = {
7303 { .name = "ACTLR2", .state = ARM_CP_STATE_AA32,
7304 .cp = 15, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 3,
99602377
RH
7305 .access = PL1_RW, .accessfn = access_tacr,
7306 .type = ARM_CP_CONST, .resetvalue = 0 },
f6287c24
PM
7307 { .name = "HACTLR2", .state = ARM_CP_STATE_AA32,
7308 .cp = 15, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 3,
7309 .access = PL2_RW, .type = ARM_CP_CONST,
7310 .resetvalue = 0 },
7311 REGINFO_SENTINEL
7312};
7313
2ceb98c0
PM
7314void register_cp_regs_for_features(ARMCPU *cpu)
7315{
7316 /* Register all the coprocessor registers based on feature bits */
7317 CPUARMState *env = &cpu->env;
7318 if (arm_feature(env, ARM_FEATURE_M)) {
7319 /* M profile has no coprocessor registers */
7320 return;
7321 }
7322
e9aa6c21 7323 define_arm_cp_regs(cpu, cp_reginfo);
9449fdf6
PM
7324 if (!arm_feature(env, ARM_FEATURE_V8)) {
7325 /* Must go early as it is full of wildcards that may be
7326 * overridden by later definitions.
7327 */
7328 define_arm_cp_regs(cpu, not_v8_cp_reginfo);
7329 }
7330
7d57f408 7331 if (arm_feature(env, ARM_FEATURE_V6)) {
8515a092
PM
7332 /* The ID registers all have impdef reset values */
7333 ARMCPRegInfo v6_idregs[] = {
0ff644a7
PM
7334 { .name = "ID_PFR0", .state = ARM_CP_STATE_BOTH,
7335 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
7336 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7337 .accessfn = access_aa32_tid3,
8a130a7b 7338 .resetvalue = cpu->isar.id_pfr0 },
96a8b92e
PM
7339 /* ID_PFR1 is not a plain ARM_CP_CONST because we don't know
7340 * the value of the GIC field until after we define these regs.
7341 */
0ff644a7
PM
7342 { .name = "ID_PFR1", .state = ARM_CP_STATE_BOTH,
7343 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 1,
96a8b92e 7344 .access = PL1_R, .type = ARM_CP_NO_RAW,
6a4ef4e5 7345 .accessfn = access_aa32_tid3,
96a8b92e
PM
7346 .readfn = id_pfr1_read,
7347 .writefn = arm_cp_write_ignore },
0ff644a7
PM
7348 { .name = "ID_DFR0", .state = ARM_CP_STATE_BOTH,
7349 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 2,
7350 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7351 .accessfn = access_aa32_tid3,
a6179538 7352 .resetvalue = cpu->isar.id_dfr0 },
0ff644a7
PM
7353 { .name = "ID_AFR0", .state = ARM_CP_STATE_BOTH,
7354 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 3,
7355 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7356 .accessfn = access_aa32_tid3,
8515a092 7357 .resetvalue = cpu->id_afr0 },
0ff644a7
PM
7358 { .name = "ID_MMFR0", .state = ARM_CP_STATE_BOTH,
7359 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 4,
7360 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7361 .accessfn = access_aa32_tid3,
10054016 7362 .resetvalue = cpu->isar.id_mmfr0 },
0ff644a7
PM
7363 { .name = "ID_MMFR1", .state = ARM_CP_STATE_BOTH,
7364 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 5,
7365 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7366 .accessfn = access_aa32_tid3,
10054016 7367 .resetvalue = cpu->isar.id_mmfr1 },
0ff644a7
PM
7368 { .name = "ID_MMFR2", .state = ARM_CP_STATE_BOTH,
7369 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 6,
7370 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7371 .accessfn = access_aa32_tid3,
10054016 7372 .resetvalue = cpu->isar.id_mmfr2 },
0ff644a7
PM
7373 { .name = "ID_MMFR3", .state = ARM_CP_STATE_BOTH,
7374 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 7,
7375 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7376 .accessfn = access_aa32_tid3,
10054016 7377 .resetvalue = cpu->isar.id_mmfr3 },
0ff644a7
PM
7378 { .name = "ID_ISAR0", .state = ARM_CP_STATE_BOTH,
7379 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0,
7380 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7381 .accessfn = access_aa32_tid3,
47576b94 7382 .resetvalue = cpu->isar.id_isar0 },
0ff644a7
PM
7383 { .name = "ID_ISAR1", .state = ARM_CP_STATE_BOTH,
7384 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 1,
7385 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7386 .accessfn = access_aa32_tid3,
47576b94 7387 .resetvalue = cpu->isar.id_isar1 },
0ff644a7
PM
7388 { .name = "ID_ISAR2", .state = ARM_CP_STATE_BOTH,
7389 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
7390 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7391 .accessfn = access_aa32_tid3,
47576b94 7392 .resetvalue = cpu->isar.id_isar2 },
0ff644a7
PM
7393 { .name = "ID_ISAR3", .state = ARM_CP_STATE_BOTH,
7394 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 3,
7395 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7396 .accessfn = access_aa32_tid3,
47576b94 7397 .resetvalue = cpu->isar.id_isar3 },
0ff644a7
PM
7398 { .name = "ID_ISAR4", .state = ARM_CP_STATE_BOTH,
7399 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 4,
7400 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7401 .accessfn = access_aa32_tid3,
47576b94 7402 .resetvalue = cpu->isar.id_isar4 },
0ff644a7
PM
7403 { .name = "ID_ISAR5", .state = ARM_CP_STATE_BOTH,
7404 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 5,
7405 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7406 .accessfn = access_aa32_tid3,
47576b94 7407 .resetvalue = cpu->isar.id_isar5 },
e20d84c1
PM
7408 { .name = "ID_MMFR4", .state = ARM_CP_STATE_BOTH,
7409 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 6,
7410 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7411 .accessfn = access_aa32_tid3,
10054016 7412 .resetvalue = cpu->isar.id_mmfr4 },
802abf40 7413 { .name = "ID_ISAR6", .state = ARM_CP_STATE_BOTH,
e20d84c1
PM
7414 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 7,
7415 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7416 .accessfn = access_aa32_tid3,
47576b94 7417 .resetvalue = cpu->isar.id_isar6 },
8515a092
PM
7418 REGINFO_SENTINEL
7419 };
7420 define_arm_cp_regs(cpu, v6_idregs);
7d57f408
PM
7421 define_arm_cp_regs(cpu, v6_cp_reginfo);
7422 } else {
7423 define_arm_cp_regs(cpu, not_v6_cp_reginfo);
7424 }
4d31c596
PM
7425 if (arm_feature(env, ARM_FEATURE_V6K)) {
7426 define_arm_cp_regs(cpu, v6k_cp_reginfo);
7427 }
5e5cf9e3 7428 if (arm_feature(env, ARM_FEATURE_V7MP) &&
452a0955 7429 !arm_feature(env, ARM_FEATURE_PMSA)) {
995939a6
PM
7430 define_arm_cp_regs(cpu, v7mp_cp_reginfo);
7431 }
327dd510
AL
7432 if (arm_feature(env, ARM_FEATURE_V7VE)) {
7433 define_arm_cp_regs(cpu, pmovsset_cp_reginfo);
7434 }
e9aa6c21 7435 if (arm_feature(env, ARM_FEATURE_V7)) {
776d4e5c 7436 ARMCPRegInfo clidr = {
7da845b0
PM
7437 .name = "CLIDR", .state = ARM_CP_STATE_BOTH,
7438 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 1,
630fcd4d
MZ
7439 .access = PL1_R, .type = ARM_CP_CONST,
7440 .accessfn = access_aa64_tid2,
7441 .resetvalue = cpu->clidr
776d4e5c 7442 };
776d4e5c 7443 define_one_arm_cp_reg(cpu, &clidr);
e9aa6c21 7444 define_arm_cp_regs(cpu, v7_cp_reginfo);
50300698 7445 define_debug_regs(cpu);
24183fb6 7446 define_pmu_regs(cpu);
7d57f408
PM
7447 } else {
7448 define_arm_cp_regs(cpu, not_v7_cp_reginfo);
e9aa6c21 7449 }
b0d2b7d0 7450 if (arm_feature(env, ARM_FEATURE_V8)) {
e20d84c1
PM
7451 /* AArch64 ID registers, which all have impdef reset values.
7452 * Note that within the ID register ranges the unused slots
7453 * must all RAZ, not UNDEF; future architecture versions may
7454 * define new registers here.
7455 */
e60cef86 7456 ARMCPRegInfo v8_idregs[] = {
976b99b6
AB
7457 /*
7458 * ID_AA64PFR0_EL1 is not a plain ARM_CP_CONST in system
7459 * emulation because we don't know the right value for the
7460 * GIC field until after we define these regs.
96a8b92e 7461 */
e60cef86
PM
7462 { .name = "ID_AA64PFR0_EL1", .state = ARM_CP_STATE_AA64,
7463 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 0,
976b99b6
AB
7464 .access = PL1_R,
7465#ifdef CONFIG_USER_ONLY
7466 .type = ARM_CP_CONST,
7467 .resetvalue = cpu->isar.id_aa64pfr0
7468#else
7469 .type = ARM_CP_NO_RAW,
6a4ef4e5 7470 .accessfn = access_aa64_tid3,
96a8b92e 7471 .readfn = id_aa64pfr0_read,
976b99b6
AB
7472 .writefn = arm_cp_write_ignore
7473#endif
7474 },
e60cef86
PM
7475 { .name = "ID_AA64PFR1_EL1", .state = ARM_CP_STATE_AA64,
7476 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 1,
7477 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7478 .accessfn = access_aa64_tid3,
47576b94 7479 .resetvalue = cpu->isar.id_aa64pfr1},
e20d84c1
PM
7480 { .name = "ID_AA64PFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7481 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 2,
7482 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7483 .accessfn = access_aa64_tid3,
e20d84c1
PM
7484 .resetvalue = 0 },
7485 { .name = "ID_AA64PFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7486 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 3,
7487 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7488 .accessfn = access_aa64_tid3,
e20d84c1 7489 .resetvalue = 0 },
9516d772 7490 { .name = "ID_AA64ZFR0_EL1", .state = ARM_CP_STATE_AA64,
e20d84c1
PM
7491 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 4,
7492 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7493 .accessfn = access_aa64_tid3,
9516d772 7494 /* At present, only SVEver == 0 is defined anyway. */
e20d84c1
PM
7495 .resetvalue = 0 },
7496 { .name = "ID_AA64PFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7497 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 5,
7498 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7499 .accessfn = access_aa64_tid3,
e20d84c1
PM
7500 .resetvalue = 0 },
7501 { .name = "ID_AA64PFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7502 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 6,
7503 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7504 .accessfn = access_aa64_tid3,
e20d84c1
PM
7505 .resetvalue = 0 },
7506 { .name = "ID_AA64PFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7507 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 7,
7508 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7509 .accessfn = access_aa64_tid3,
e20d84c1 7510 .resetvalue = 0 },
e60cef86
PM
7511 { .name = "ID_AA64DFR0_EL1", .state = ARM_CP_STATE_AA64,
7512 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 0,
7513 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7514 .accessfn = access_aa64_tid3,
2a609df8 7515 .resetvalue = cpu->isar.id_aa64dfr0 },
e60cef86
PM
7516 { .name = "ID_AA64DFR1_EL1", .state = ARM_CP_STATE_AA64,
7517 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 1,
7518 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7519 .accessfn = access_aa64_tid3,
2a609df8 7520 .resetvalue = cpu->isar.id_aa64dfr1 },
e20d84c1
PM
7521 { .name = "ID_AA64DFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7522 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 2,
7523 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7524 .accessfn = access_aa64_tid3,
e20d84c1
PM
7525 .resetvalue = 0 },
7526 { .name = "ID_AA64DFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7527 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 3,
7528 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7529 .accessfn = access_aa64_tid3,
e20d84c1 7530 .resetvalue = 0 },
e60cef86
PM
7531 { .name = "ID_AA64AFR0_EL1", .state = ARM_CP_STATE_AA64,
7532 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 4,
7533 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7534 .accessfn = access_aa64_tid3,
e60cef86
PM
7535 .resetvalue = cpu->id_aa64afr0 },
7536 { .name = "ID_AA64AFR1_EL1", .state = ARM_CP_STATE_AA64,
7537 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 5,
7538 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7539 .accessfn = access_aa64_tid3,
e60cef86 7540 .resetvalue = cpu->id_aa64afr1 },
e20d84c1
PM
7541 { .name = "ID_AA64AFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7542 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 6,
7543 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7544 .accessfn = access_aa64_tid3,
e20d84c1
PM
7545 .resetvalue = 0 },
7546 { .name = "ID_AA64AFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7547 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 7,
7548 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7549 .accessfn = access_aa64_tid3,
e20d84c1 7550 .resetvalue = 0 },
e60cef86
PM
7551 { .name = "ID_AA64ISAR0_EL1", .state = ARM_CP_STATE_AA64,
7552 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 0,
7553 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7554 .accessfn = access_aa64_tid3,
47576b94 7555 .resetvalue = cpu->isar.id_aa64isar0 },
e60cef86
PM
7556 { .name = "ID_AA64ISAR1_EL1", .state = ARM_CP_STATE_AA64,
7557 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 1,
7558 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7559 .accessfn = access_aa64_tid3,
47576b94 7560 .resetvalue = cpu->isar.id_aa64isar1 },
e20d84c1
PM
7561 { .name = "ID_AA64ISAR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7562 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 2,
7563 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7564 .accessfn = access_aa64_tid3,
e20d84c1
PM
7565 .resetvalue = 0 },
7566 { .name = "ID_AA64ISAR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7567 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 3,
7568 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7569 .accessfn = access_aa64_tid3,
e20d84c1
PM
7570 .resetvalue = 0 },
7571 { .name = "ID_AA64ISAR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7572 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 4,
7573 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7574 .accessfn = access_aa64_tid3,
e20d84c1
PM
7575 .resetvalue = 0 },
7576 { .name = "ID_AA64ISAR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7577 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 5,
7578 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7579 .accessfn = access_aa64_tid3,
e20d84c1
PM
7580 .resetvalue = 0 },
7581 { .name = "ID_AA64ISAR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7582 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 6,
7583 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7584 .accessfn = access_aa64_tid3,
e20d84c1
PM
7585 .resetvalue = 0 },
7586 { .name = "ID_AA64ISAR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7587 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 7,
7588 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7589 .accessfn = access_aa64_tid3,
e20d84c1 7590 .resetvalue = 0 },
e60cef86
PM
7591 { .name = "ID_AA64MMFR0_EL1", .state = ARM_CP_STATE_AA64,
7592 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
7593 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7594 .accessfn = access_aa64_tid3,
3dc91ddb 7595 .resetvalue = cpu->isar.id_aa64mmfr0 },
e60cef86
PM
7596 { .name = "ID_AA64MMFR1_EL1", .state = ARM_CP_STATE_AA64,
7597 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 1,
7598 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7599 .accessfn = access_aa64_tid3,
3dc91ddb 7600 .resetvalue = cpu->isar.id_aa64mmfr1 },
64761e10 7601 { .name = "ID_AA64MMFR2_EL1", .state = ARM_CP_STATE_AA64,
e20d84c1
PM
7602 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 2,
7603 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7604 .accessfn = access_aa64_tid3,
64761e10 7605 .resetvalue = cpu->isar.id_aa64mmfr2 },
e20d84c1
PM
7606 { .name = "ID_AA64MMFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7607 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 3,
7608 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7609 .accessfn = access_aa64_tid3,
e20d84c1
PM
7610 .resetvalue = 0 },
7611 { .name = "ID_AA64MMFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7612 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 4,
7613 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7614 .accessfn = access_aa64_tid3,
e20d84c1
PM
7615 .resetvalue = 0 },
7616 { .name = "ID_AA64MMFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7617 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 5,
7618 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7619 .accessfn = access_aa64_tid3,
e20d84c1
PM
7620 .resetvalue = 0 },
7621 { .name = "ID_AA64MMFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7622 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 6,
7623 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7624 .accessfn = access_aa64_tid3,
e20d84c1
PM
7625 .resetvalue = 0 },
7626 { .name = "ID_AA64MMFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7627 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 7,
7628 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7629 .accessfn = access_aa64_tid3,
e20d84c1 7630 .resetvalue = 0 },
a50c0f51
PM
7631 { .name = "MVFR0_EL1", .state = ARM_CP_STATE_AA64,
7632 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0,
7633 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7634 .accessfn = access_aa64_tid3,
47576b94 7635 .resetvalue = cpu->isar.mvfr0 },
a50c0f51
PM
7636 { .name = "MVFR1_EL1", .state = ARM_CP_STATE_AA64,
7637 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1,
7638 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7639 .accessfn = access_aa64_tid3,
47576b94 7640 .resetvalue = cpu->isar.mvfr1 },
a50c0f51
PM
7641 { .name = "MVFR2_EL1", .state = ARM_CP_STATE_AA64,
7642 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2,
7643 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7644 .accessfn = access_aa64_tid3,
47576b94 7645 .resetvalue = cpu->isar.mvfr2 },
e20d84c1
PM
7646 { .name = "MVFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7647 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 3,
7648 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7649 .accessfn = access_aa64_tid3,
e20d84c1
PM
7650 .resetvalue = 0 },
7651 { .name = "MVFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7652 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 4,
7653 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7654 .accessfn = access_aa64_tid3,
e20d84c1
PM
7655 .resetvalue = 0 },
7656 { .name = "MVFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7657 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 5,
7658 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7659 .accessfn = access_aa64_tid3,
e20d84c1
PM
7660 .resetvalue = 0 },
7661 { .name = "MVFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7662 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 6,
7663 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7664 .accessfn = access_aa64_tid3,
e20d84c1
PM
7665 .resetvalue = 0 },
7666 { .name = "MVFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7667 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 7,
7668 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7669 .accessfn = access_aa64_tid3,
e20d84c1 7670 .resetvalue = 0 },
4054bfa9
AF
7671 { .name = "PMCEID0", .state = ARM_CP_STATE_AA32,
7672 .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 6,
7673 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
cad86737 7674 .resetvalue = extract64(cpu->pmceid0, 0, 32) },
4054bfa9
AF
7675 { .name = "PMCEID0_EL0", .state = ARM_CP_STATE_AA64,
7676 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 6,
7677 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
7678 .resetvalue = cpu->pmceid0 },
7679 { .name = "PMCEID1", .state = ARM_CP_STATE_AA32,
7680 .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 7,
7681 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
cad86737 7682 .resetvalue = extract64(cpu->pmceid1, 0, 32) },
4054bfa9
AF
7683 { .name = "PMCEID1_EL0", .state = ARM_CP_STATE_AA64,
7684 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 7,
7685 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
7686 .resetvalue = cpu->pmceid1 },
e60cef86
PM
7687 REGINFO_SENTINEL
7688 };
6c5c0fec
AB
7689#ifdef CONFIG_USER_ONLY
7690 ARMCPRegUserSpaceInfo v8_user_idregs[] = {
7691 { .name = "ID_AA64PFR0_EL1",
7692 .exported_bits = 0x000f000f00ff0000,
7693 .fixed_bits = 0x0000000000000011 },
7694 { .name = "ID_AA64PFR1_EL1",
7695 .exported_bits = 0x00000000000000f0 },
d040242e
AB
7696 { .name = "ID_AA64PFR*_EL1_RESERVED",
7697 .is_glob = true },
6c5c0fec
AB
7698 { .name = "ID_AA64ZFR0_EL1" },
7699 { .name = "ID_AA64MMFR0_EL1",
7700 .fixed_bits = 0x00000000ff000000 },
7701 { .name = "ID_AA64MMFR1_EL1" },
d040242e
AB
7702 { .name = "ID_AA64MMFR*_EL1_RESERVED",
7703 .is_glob = true },
6c5c0fec
AB
7704 { .name = "ID_AA64DFR0_EL1",
7705 .fixed_bits = 0x0000000000000006 },
7706 { .name = "ID_AA64DFR1_EL1" },
d040242e
AB
7707 { .name = "ID_AA64DFR*_EL1_RESERVED",
7708 .is_glob = true },
7709 { .name = "ID_AA64AFR*",
7710 .is_glob = true },
6c5c0fec
AB
7711 { .name = "ID_AA64ISAR0_EL1",
7712 .exported_bits = 0x00fffffff0fffff0 },
7713 { .name = "ID_AA64ISAR1_EL1",
7714 .exported_bits = 0x000000f0ffffffff },
d040242e
AB
7715 { .name = "ID_AA64ISAR*_EL1_RESERVED",
7716 .is_glob = true },
6c5c0fec
AB
7717 REGUSERINFO_SENTINEL
7718 };
7719 modify_arm_cp_regs(v8_idregs, v8_user_idregs);
7720#endif
be8e8128
GB
7721 /* RVBAR_EL1 is only implemented if EL1 is the highest EL */
7722 if (!arm_feature(env, ARM_FEATURE_EL3) &&
7723 !arm_feature(env, ARM_FEATURE_EL2)) {
7724 ARMCPRegInfo rvbar = {
7725 .name = "RVBAR_EL1", .state = ARM_CP_STATE_AA64,
7726 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
7727 .type = ARM_CP_CONST, .access = PL1_R, .resetvalue = cpu->rvbar
7728 };
7729 define_one_arm_cp_reg(cpu, &rvbar);
7730 }
e60cef86 7731 define_arm_cp_regs(cpu, v8_idregs);
b0d2b7d0
PM
7732 define_arm_cp_regs(cpu, v8_cp_reginfo);
7733 }
3b685ba7 7734 if (arm_feature(env, ARM_FEATURE_EL2)) {
f0d574d6 7735 uint64_t vmpidr_def = mpidr_read_val(env);
731de9e6
EI
7736 ARMCPRegInfo vpidr_regs[] = {
7737 { .name = "VPIDR", .state = ARM_CP_STATE_AA32,
7738 .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
7739 .access = PL2_RW, .accessfn = access_el3_aa32ns,
36476562
PM
7740 .resetvalue = cpu->midr, .type = ARM_CP_ALIAS,
7741 .fieldoffset = offsetoflow32(CPUARMState, cp15.vpidr_el2) },
731de9e6
EI
7742 { .name = "VPIDR_EL2", .state = ARM_CP_STATE_AA64,
7743 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
7744 .access = PL2_RW, .resetvalue = cpu->midr,
7745 .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) },
f0d574d6
EI
7746 { .name = "VMPIDR", .state = ARM_CP_STATE_AA32,
7747 .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
7748 .access = PL2_RW, .accessfn = access_el3_aa32ns,
36476562
PM
7749 .resetvalue = vmpidr_def, .type = ARM_CP_ALIAS,
7750 .fieldoffset = offsetoflow32(CPUARMState, cp15.vmpidr_el2) },
f0d574d6
EI
7751 { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_AA64,
7752 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
7753 .access = PL2_RW,
7754 .resetvalue = vmpidr_def,
7755 .fieldoffset = offsetof(CPUARMState, cp15.vmpidr_el2) },
731de9e6
EI
7756 REGINFO_SENTINEL
7757 };
7758 define_arm_cp_regs(cpu, vpidr_regs);
4771cd01 7759 define_arm_cp_regs(cpu, el2_cp_reginfo);
ce4afed8
PM
7760 if (arm_feature(env, ARM_FEATURE_V8)) {
7761 define_arm_cp_regs(cpu, el2_v8_cp_reginfo);
7762 }
e9152ee9
RDC
7763 if (cpu_isar_feature(aa64_sel2, cpu)) {
7764 define_arm_cp_regs(cpu, el2_sec_cp_reginfo);
7765 }
be8e8128
GB
7766 /* RVBAR_EL2 is only implemented if EL2 is the highest EL */
7767 if (!arm_feature(env, ARM_FEATURE_EL3)) {
7768 ARMCPRegInfo rvbar = {
7769 .name = "RVBAR_EL2", .state = ARM_CP_STATE_AA64,
7770 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 1,
7771 .type = ARM_CP_CONST, .access = PL2_R, .resetvalue = cpu->rvbar
7772 };
7773 define_one_arm_cp_reg(cpu, &rvbar);
7774 }
d42e3c26
EI
7775 } else {
7776 /* If EL2 is missing but higher ELs are enabled, we need to
7777 * register the no_el2 reginfos.
7778 */
7779 if (arm_feature(env, ARM_FEATURE_EL3)) {
f0d574d6
EI
7780 /* When EL3 exists but not EL2, VPIDR and VMPIDR take the value
7781 * of MIDR_EL1 and MPIDR_EL1.
731de9e6
EI
7782 */
7783 ARMCPRegInfo vpidr_regs[] = {
7784 { .name = "VPIDR_EL2", .state = ARM_CP_STATE_BOTH,
7785 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
93dd1e61 7786 .access = PL2_RW, .accessfn = access_el3_aa32ns,
731de9e6
EI
7787 .type = ARM_CP_CONST, .resetvalue = cpu->midr,
7788 .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) },
f0d574d6
EI
7789 { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_BOTH,
7790 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
93dd1e61 7791 .access = PL2_RW, .accessfn = access_el3_aa32ns,
f0d574d6
EI
7792 .type = ARM_CP_NO_RAW,
7793 .writefn = arm_cp_write_ignore, .readfn = mpidr_read },
731de9e6
EI
7794 REGINFO_SENTINEL
7795 };
7796 define_arm_cp_regs(cpu, vpidr_regs);
4771cd01 7797 define_arm_cp_regs(cpu, el3_no_el2_cp_reginfo);
ce4afed8
PM
7798 if (arm_feature(env, ARM_FEATURE_V8)) {
7799 define_arm_cp_regs(cpu, el3_no_el2_v8_cp_reginfo);
7800 }
d42e3c26 7801 }
3b685ba7 7802 }
81547d66 7803 if (arm_feature(env, ARM_FEATURE_EL3)) {
0f1a3b24 7804 define_arm_cp_regs(cpu, el3_cp_reginfo);
e24fdd23
PM
7805 ARMCPRegInfo el3_regs[] = {
7806 { .name = "RVBAR_EL3", .state = ARM_CP_STATE_AA64,
7807 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 1,
7808 .type = ARM_CP_CONST, .access = PL3_R, .resetvalue = cpu->rvbar },
7809 { .name = "SCTLR_EL3", .state = ARM_CP_STATE_AA64,
7810 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 0,
7811 .access = PL3_RW,
7812 .raw_writefn = raw_write, .writefn = sctlr_write,
7813 .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[3]),
7814 .resetvalue = cpu->reset_sctlr },
7815 REGINFO_SENTINEL
be8e8128 7816 };
e24fdd23
PM
7817
7818 define_arm_cp_regs(cpu, el3_regs);
81547d66 7819 }
2f027fc5
PM
7820 /* The behaviour of NSACR is sufficiently various that we don't
7821 * try to describe it in a single reginfo:
7822 * if EL3 is 64 bit, then trap to EL3 from S EL1,
7823 * reads as constant 0xc00 from NS EL1 and NS EL2
7824 * if EL3 is 32 bit, then RW at EL3, RO at NS EL1 and NS EL2
7825 * if v7 without EL3, register doesn't exist
7826 * if v8 without EL3, reads as constant 0xc00 from NS EL1 and NS EL2
7827 */
7828 if (arm_feature(env, ARM_FEATURE_EL3)) {
7829 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
7830 ARMCPRegInfo nsacr = {
7831 .name = "NSACR", .type = ARM_CP_CONST,
7832 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
7833 .access = PL1_RW, .accessfn = nsacr_access,
7834 .resetvalue = 0xc00
7835 };
7836 define_one_arm_cp_reg(cpu, &nsacr);
7837 } else {
7838 ARMCPRegInfo nsacr = {
7839 .name = "NSACR",
7840 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
7841 .access = PL3_RW | PL1_R,
7842 .resetvalue = 0,
7843 .fieldoffset = offsetof(CPUARMState, cp15.nsacr)
7844 };
7845 define_one_arm_cp_reg(cpu, &nsacr);
7846 }
7847 } else {
7848 if (arm_feature(env, ARM_FEATURE_V8)) {
7849 ARMCPRegInfo nsacr = {
7850 .name = "NSACR", .type = ARM_CP_CONST,
7851 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
7852 .access = PL1_R,
7853 .resetvalue = 0xc00
7854 };
7855 define_one_arm_cp_reg(cpu, &nsacr);
7856 }
7857 }
7858
452a0955 7859 if (arm_feature(env, ARM_FEATURE_PMSA)) {
6cb0b013
PC
7860 if (arm_feature(env, ARM_FEATURE_V6)) {
7861 /* PMSAv6 not implemented */
7862 assert(arm_feature(env, ARM_FEATURE_V7));
7863 define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
7864 define_arm_cp_regs(cpu, pmsav7_cp_reginfo);
7865 } else {
7866 define_arm_cp_regs(cpu, pmsav5_cp_reginfo);
7867 }
18032bec 7868 } else {
8e5d75c9 7869 define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
18032bec 7870 define_arm_cp_regs(cpu, vmsa_cp_reginfo);
4036b7d1
PM
7871 /* TTCBR2 is introduced with ARMv8.2-AA32HPD. */
7872 if (cpu_isar_feature(aa32_hpd, cpu)) {
ab638a32
RH
7873 define_one_arm_cp_reg(cpu, &ttbcr2_reginfo);
7874 }
18032bec 7875 }
c326b979
PM
7876 if (arm_feature(env, ARM_FEATURE_THUMB2EE)) {
7877 define_arm_cp_regs(cpu, t2ee_cp_reginfo);
7878 }
6cc7a3ae
PM
7879 if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) {
7880 define_arm_cp_regs(cpu, generic_timer_cp_reginfo);
7881 }
4a501606
PM
7882 if (arm_feature(env, ARM_FEATURE_VAPA)) {
7883 define_arm_cp_regs(cpu, vapa_cp_reginfo);
7884 }
c4804214
PM
7885 if (arm_feature(env, ARM_FEATURE_CACHE_TEST_CLEAN)) {
7886 define_arm_cp_regs(cpu, cache_test_clean_cp_reginfo);
7887 }
7888 if (arm_feature(env, ARM_FEATURE_CACHE_DIRTY_REG)) {
7889 define_arm_cp_regs(cpu, cache_dirty_status_cp_reginfo);
7890 }
7891 if (arm_feature(env, ARM_FEATURE_CACHE_BLOCK_OPS)) {
7892 define_arm_cp_regs(cpu, cache_block_ops_cp_reginfo);
7893 }
18032bec
PM
7894 if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
7895 define_arm_cp_regs(cpu, omap_cp_reginfo);
7896 }
34f90529
PM
7897 if (arm_feature(env, ARM_FEATURE_STRONGARM)) {
7898 define_arm_cp_regs(cpu, strongarm_cp_reginfo);
7899 }
1047b9d7
PM
7900 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
7901 define_arm_cp_regs(cpu, xscale_cp_reginfo);
7902 }
7903 if (arm_feature(env, ARM_FEATURE_DUMMY_C15_REGS)) {
7904 define_arm_cp_regs(cpu, dummy_c15_cp_reginfo);
7905 }
7ac681cf
PM
7906 if (arm_feature(env, ARM_FEATURE_LPAE)) {
7907 define_arm_cp_regs(cpu, lpae_cp_reginfo);
7908 }
873b73c0 7909 if (cpu_isar_feature(aa32_jazelle, cpu)) {
f96f3d5f
MZ
7910 define_arm_cp_regs(cpu, jazelle_regs);
7911 }
7884849c
PM
7912 /* Slightly awkwardly, the OMAP and StrongARM cores need all of
7913 * cp15 crn=0 to be writes-ignored, whereas for other cores they should
7914 * be read-only (ie write causes UNDEF exception).
7915 */
7916 {
00a29f3d
PM
7917 ARMCPRegInfo id_pre_v8_midr_cp_reginfo[] = {
7918 /* Pre-v8 MIDR space.
7919 * Note that the MIDR isn't a simple constant register because
7884849c
PM
7920 * of the TI925 behaviour where writes to another register can
7921 * cause the MIDR value to change.
97ce8d61
PC
7922 *
7923 * Unimplemented registers in the c15 0 0 0 space default to
7924 * MIDR. Define MIDR first as this entire space, then CTR, TCMTR
7925 * and friends override accordingly.
7884849c
PM
7926 */
7927 { .name = "MIDR",
97ce8d61 7928 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = CP_ANY,
7884849c 7929 .access = PL1_R, .resetvalue = cpu->midr,
d4e6df63 7930 .writefn = arm_cp_write_ignore, .raw_writefn = raw_write,
731de9e6 7931 .readfn = midr_read,
97ce8d61
PC
7932 .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
7933 .type = ARM_CP_OVERRIDE },
7884849c
PM
7934 /* crn = 0 op1 = 0 crm = 3..7 : currently unassigned; we RAZ. */
7935 { .name = "DUMMY",
7936 .cp = 15, .crn = 0, .crm = 3, .opc1 = 0, .opc2 = CP_ANY,
7937 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
7938 { .name = "DUMMY",
7939 .cp = 15, .crn = 0, .crm = 4, .opc1 = 0, .opc2 = CP_ANY,
7940 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
7941 { .name = "DUMMY",
7942 .cp = 15, .crn = 0, .crm = 5, .opc1 = 0, .opc2 = CP_ANY,
7943 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
7944 { .name = "DUMMY",
7945 .cp = 15, .crn = 0, .crm = 6, .opc1 = 0, .opc2 = CP_ANY,
7946 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
7947 { .name = "DUMMY",
7948 .cp = 15, .crn = 0, .crm = 7, .opc1 = 0, .opc2 = CP_ANY,
7949 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
7950 REGINFO_SENTINEL
7951 };
00a29f3d 7952 ARMCPRegInfo id_v8_midr_cp_reginfo[] = {
00a29f3d
PM
7953 { .name = "MIDR_EL1", .state = ARM_CP_STATE_BOTH,
7954 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 0,
731de9e6
EI
7955 .access = PL1_R, .type = ARM_CP_NO_RAW, .resetvalue = cpu->midr,
7956 .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
7957 .readfn = midr_read },
ac00c79f
SF
7958 /* crn = 0 op1 = 0 crm = 0 op2 = 4,7 : AArch32 aliases of MIDR */
7959 { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
7960 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
7961 .access = PL1_R, .resetvalue = cpu->midr },
7962 { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
7963 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 7,
7964 .access = PL1_R, .resetvalue = cpu->midr },
00a29f3d
PM
7965 { .name = "REVIDR_EL1", .state = ARM_CP_STATE_BOTH,
7966 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 6,
93fbc983
MZ
7967 .access = PL1_R,
7968 .accessfn = access_aa64_tid1,
7969 .type = ARM_CP_CONST, .resetvalue = cpu->revidr },
00a29f3d
PM
7970 REGINFO_SENTINEL
7971 };
7972 ARMCPRegInfo id_cp_reginfo[] = {
7973 /* These are common to v8 and pre-v8 */
7974 { .name = "CTR",
7975 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 1,
630fcd4d
MZ
7976 .access = PL1_R, .accessfn = ctr_el0_access,
7977 .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
00a29f3d
PM
7978 { .name = "CTR_EL0", .state = ARM_CP_STATE_AA64,
7979 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 0, .crm = 0,
7980 .access = PL0_R, .accessfn = ctr_el0_access,
7981 .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
7982 /* TCMTR and TLBTR exist in v8 but have no 64-bit versions */
7983 { .name = "TCMTR",
7984 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 2,
93fbc983
MZ
7985 .access = PL1_R,
7986 .accessfn = access_aa32_tid1,
7987 .type = ARM_CP_CONST, .resetvalue = 0 },
00a29f3d
PM
7988 REGINFO_SENTINEL
7989 };
8085ce63
PC
7990 /* TLBTR is specific to VMSA */
7991 ARMCPRegInfo id_tlbtr_reginfo = {
7992 .name = "TLBTR",
7993 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 3,
93fbc983
MZ
7994 .access = PL1_R,
7995 .accessfn = access_aa32_tid1,
7996 .type = ARM_CP_CONST, .resetvalue = 0,
8085ce63 7997 };
3281af81
PC
7998 /* MPUIR is specific to PMSA V6+ */
7999 ARMCPRegInfo id_mpuir_reginfo = {
8000 .name = "MPUIR",
8001 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
8002 .access = PL1_R, .type = ARM_CP_CONST,
8003 .resetvalue = cpu->pmsav7_dregion << 8
8004 };
7884849c
PM
8005 ARMCPRegInfo crn0_wi_reginfo = {
8006 .name = "CRN0_WI", .cp = 15, .crn = 0, .crm = CP_ANY,
8007 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_W,
8008 .type = ARM_CP_NOP | ARM_CP_OVERRIDE
8009 };
6c5c0fec
AB
8010#ifdef CONFIG_USER_ONLY
8011 ARMCPRegUserSpaceInfo id_v8_user_midr_cp_reginfo[] = {
8012 { .name = "MIDR_EL1",
8013 .exported_bits = 0x00000000ffffffff },
8014 { .name = "REVIDR_EL1" },
8015 REGUSERINFO_SENTINEL
8016 };
8017 modify_arm_cp_regs(id_v8_midr_cp_reginfo, id_v8_user_midr_cp_reginfo);
8018#endif
7884849c
PM
8019 if (arm_feature(env, ARM_FEATURE_OMAPCP) ||
8020 arm_feature(env, ARM_FEATURE_STRONGARM)) {
8021 ARMCPRegInfo *r;
8022 /* Register the blanket "writes ignored" value first to cover the
a703eda1
PC
8023 * whole space. Then update the specific ID registers to allow write
8024 * access, so that they ignore writes rather than causing them to
8025 * UNDEF.
7884849c
PM
8026 */
8027 define_one_arm_cp_reg(cpu, &crn0_wi_reginfo);
00a29f3d
PM
8028 for (r = id_pre_v8_midr_cp_reginfo;
8029 r->type != ARM_CP_SENTINEL; r++) {
8030 r->access = PL1_RW;
8031 }
7884849c
PM
8032 for (r = id_cp_reginfo; r->type != ARM_CP_SENTINEL; r++) {
8033 r->access = PL1_RW;
7884849c 8034 }
10006112 8035 id_mpuir_reginfo.access = PL1_RW;
3281af81 8036 id_tlbtr_reginfo.access = PL1_RW;
7884849c 8037 }
00a29f3d
PM
8038 if (arm_feature(env, ARM_FEATURE_V8)) {
8039 define_arm_cp_regs(cpu, id_v8_midr_cp_reginfo);
8040 } else {
8041 define_arm_cp_regs(cpu, id_pre_v8_midr_cp_reginfo);
8042 }
a703eda1 8043 define_arm_cp_regs(cpu, id_cp_reginfo);
452a0955 8044 if (!arm_feature(env, ARM_FEATURE_PMSA)) {
8085ce63 8045 define_one_arm_cp_reg(cpu, &id_tlbtr_reginfo);
3281af81
PC
8046 } else if (arm_feature(env, ARM_FEATURE_V7)) {
8047 define_one_arm_cp_reg(cpu, &id_mpuir_reginfo);
8085ce63 8048 }
7884849c
PM
8049 }
8050
97ce8d61 8051 if (arm_feature(env, ARM_FEATURE_MPIDR)) {
52264166
AB
8052 ARMCPRegInfo mpidr_cp_reginfo[] = {
8053 { .name = "MPIDR_EL1", .state = ARM_CP_STATE_BOTH,
8054 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 5,
8055 .access = PL1_R, .readfn = mpidr_read, .type = ARM_CP_NO_RAW },
8056 REGINFO_SENTINEL
8057 };
8058#ifdef CONFIG_USER_ONLY
8059 ARMCPRegUserSpaceInfo mpidr_user_cp_reginfo[] = {
8060 { .name = "MPIDR_EL1",
8061 .fixed_bits = 0x0000000080000000 },
8062 REGUSERINFO_SENTINEL
8063 };
8064 modify_arm_cp_regs(mpidr_cp_reginfo, mpidr_user_cp_reginfo);
8065#endif
97ce8d61
PC
8066 define_arm_cp_regs(cpu, mpidr_cp_reginfo);
8067 }
8068
2771db27 8069 if (arm_feature(env, ARM_FEATURE_AUXCR)) {
834a6c69
PM
8070 ARMCPRegInfo auxcr_reginfo[] = {
8071 { .name = "ACTLR_EL1", .state = ARM_CP_STATE_BOTH,
8072 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 1,
99602377
RH
8073 .access = PL1_RW, .accessfn = access_tacr,
8074 .type = ARM_CP_CONST, .resetvalue = cpu->reset_auxcr },
834a6c69
PM
8075 { .name = "ACTLR_EL2", .state = ARM_CP_STATE_BOTH,
8076 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 1,
8077 .access = PL2_RW, .type = ARM_CP_CONST,
8078 .resetvalue = 0 },
8079 { .name = "ACTLR_EL3", .state = ARM_CP_STATE_AA64,
8080 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 1,
8081 .access = PL3_RW, .type = ARM_CP_CONST,
8082 .resetvalue = 0 },
8083 REGINFO_SENTINEL
2771db27 8084 };
834a6c69 8085 define_arm_cp_regs(cpu, auxcr_reginfo);
f6287c24
PM
8086 if (cpu_isar_feature(aa32_ac2, cpu)) {
8087 define_arm_cp_regs(cpu, actlr2_hactlr2_reginfo);
0e0456ab 8088 }
2771db27
PM
8089 }
8090
d8ba780b 8091 if (arm_feature(env, ARM_FEATURE_CBAR)) {
d56974af
LM
8092 /*
8093 * CBAR is IMPDEF, but common on Arm Cortex-A implementations.
8094 * There are two flavours:
8095 * (1) older 32-bit only cores have a simple 32-bit CBAR
8096 * (2) 64-bit cores have a 64-bit CBAR visible to AArch64, plus a
8097 * 32-bit register visible to AArch32 at a different encoding
8098 * to the "flavour 1" register and with the bits rearranged to
8099 * be able to squash a 64-bit address into the 32-bit view.
8100 * We distinguish the two via the ARM_FEATURE_AARCH64 flag, but
8101 * in future if we support AArch32-only configs of some of the
8102 * AArch64 cores we might need to add a specific feature flag
8103 * to indicate cores with "flavour 2" CBAR.
8104 */
f318cec6
PM
8105 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
8106 /* 32 bit view is [31:18] 0...0 [43:32]. */
8107 uint32_t cbar32 = (extract64(cpu->reset_cbar, 18, 14) << 18)
8108 | extract64(cpu->reset_cbar, 32, 12);
8109 ARMCPRegInfo cbar_reginfo[] = {
8110 { .name = "CBAR",
8111 .type = ARM_CP_CONST,
d56974af
LM
8112 .cp = 15, .crn = 15, .crm = 3, .opc1 = 1, .opc2 = 0,
8113 .access = PL1_R, .resetvalue = cbar32 },
f318cec6
PM
8114 { .name = "CBAR_EL1", .state = ARM_CP_STATE_AA64,
8115 .type = ARM_CP_CONST,
8116 .opc0 = 3, .opc1 = 1, .crn = 15, .crm = 3, .opc2 = 0,
d56974af 8117 .access = PL1_R, .resetvalue = cpu->reset_cbar },
f318cec6
PM
8118 REGINFO_SENTINEL
8119 };
8120 /* We don't implement a r/w 64 bit CBAR currently */
8121 assert(arm_feature(env, ARM_FEATURE_CBAR_RO));
8122 define_arm_cp_regs(cpu, cbar_reginfo);
8123 } else {
8124 ARMCPRegInfo cbar = {
8125 .name = "CBAR",
8126 .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0,
8127 .access = PL1_R|PL3_W, .resetvalue = cpu->reset_cbar,
8128 .fieldoffset = offsetof(CPUARMState,
8129 cp15.c15_config_base_address)
8130 };
8131 if (arm_feature(env, ARM_FEATURE_CBAR_RO)) {
8132 cbar.access = PL1_R;
8133 cbar.fieldoffset = 0;
8134 cbar.type = ARM_CP_CONST;
8135 }
8136 define_one_arm_cp_reg(cpu, &cbar);
8137 }
d8ba780b
PC
8138 }
8139
91db4642
CLG
8140 if (arm_feature(env, ARM_FEATURE_VBAR)) {
8141 ARMCPRegInfo vbar_cp_reginfo[] = {
8142 { .name = "VBAR", .state = ARM_CP_STATE_BOTH,
8143 .opc0 = 3, .crn = 12, .crm = 0, .opc1 = 0, .opc2 = 0,
8144 .access = PL1_RW, .writefn = vbar_write,
8145 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.vbar_s),
8146 offsetof(CPUARMState, cp15.vbar_ns) },
8147 .resetvalue = 0 },
8148 REGINFO_SENTINEL
8149 };
8150 define_arm_cp_regs(cpu, vbar_cp_reginfo);
8151 }
8152
2771db27
PM
8153 /* Generic registers whose values depend on the implementation */
8154 {
8155 ARMCPRegInfo sctlr = {
5ebafdf3 8156 .name = "SCTLR", .state = ARM_CP_STATE_BOTH,
137feaa9 8157 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
84929218 8158 .access = PL1_RW, .accessfn = access_tvm_trvm,
137feaa9
FA
8159 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.sctlr_s),
8160 offsetof(CPUARMState, cp15.sctlr_ns) },
d4e6df63
PM
8161 .writefn = sctlr_write, .resetvalue = cpu->reset_sctlr,
8162 .raw_writefn = raw_write,
2771db27
PM
8163 };
8164 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
8165 /* Normally we would always end the TB on an SCTLR write, but Linux
8166 * arch/arm/mach-pxa/sleep.S expects two instructions following
8167 * an MMU enable to execute from cache. Imitate this behaviour.
8168 */
8169 sctlr.type |= ARM_CP_SUPPRESS_TB_END;
8170 }
8171 define_one_arm_cp_reg(cpu, &sctlr);
8172 }
5be5e8ed 8173
2d7137c1 8174 if (cpu_isar_feature(aa64_lor, cpu)) {
2d7137c1
RH
8175 define_arm_cp_regs(cpu, lor_reginfo);
8176 }
220f508f
RH
8177 if (cpu_isar_feature(aa64_pan, cpu)) {
8178 define_one_arm_cp_reg(cpu, &pan_reginfo);
8179 }
04b07d29
RH
8180#ifndef CONFIG_USER_ONLY
8181 if (cpu_isar_feature(aa64_ats1e1, cpu)) {
8182 define_arm_cp_regs(cpu, ats1e1_reginfo);
8183 }
8184 if (cpu_isar_feature(aa32_ats1e1, cpu)) {
8185 define_arm_cp_regs(cpu, ats1cp_reginfo);
8186 }
8187#endif
9eeb7a1c
RH
8188 if (cpu_isar_feature(aa64_uao, cpu)) {
8189 define_one_arm_cp_reg(cpu, &uao_reginfo);
8190 }
2d7137c1 8191
e2a1a461
RH
8192 if (arm_feature(env, ARM_FEATURE_EL2) && cpu_isar_feature(aa64_vh, cpu)) {
8193 define_arm_cp_regs(cpu, vhe_reginfo);
8194 }
8195
cd208a1c 8196 if (cpu_isar_feature(aa64_sve, cpu)) {
5be5e8ed
RH
8197 define_one_arm_cp_reg(cpu, &zcr_el1_reginfo);
8198 if (arm_feature(env, ARM_FEATURE_EL2)) {
8199 define_one_arm_cp_reg(cpu, &zcr_el2_reginfo);
8200 } else {
8201 define_one_arm_cp_reg(cpu, &zcr_no_el2_reginfo);
8202 }
8203 if (arm_feature(env, ARM_FEATURE_EL3)) {
8204 define_one_arm_cp_reg(cpu, &zcr_el3_reginfo);
8205 }
8206 }
967aa94f
RH
8207
8208#ifdef TARGET_AARCH64
8209 if (cpu_isar_feature(aa64_pauth, cpu)) {
8210 define_arm_cp_regs(cpu, pauth_reginfo);
8211 }
de390645
RH
8212 if (cpu_isar_feature(aa64_rndr, cpu)) {
8213 define_arm_cp_regs(cpu, rndr_reginfo);
8214 }
0d57b499
BM
8215#ifndef CONFIG_USER_ONLY
8216 /* Data Cache clean instructions up to PoP */
8217 if (cpu_isar_feature(aa64_dcpop, cpu)) {
8218 define_one_arm_cp_reg(cpu, dcpop_reg);
8219
8220 if (cpu_isar_feature(aa64_dcpodp, cpu)) {
8221 define_one_arm_cp_reg(cpu, dcpodp_reg);
8222 }
8223 }
8224#endif /*CONFIG_USER_ONLY*/
4b779ceb
RH
8225
8226 /*
8227 * If full MTE is enabled, add all of the system registers.
8228 * If only "instructions available at EL0" are enabled,
8229 * then define only a RAZ/WI version of PSTATE.TCO.
8230 */
8231 if (cpu_isar_feature(aa64_mte, cpu)) {
8232 define_arm_cp_regs(cpu, mte_reginfo);
5463df16 8233 define_arm_cp_regs(cpu, mte_el0_cacheop_reginfo);
4b779ceb
RH
8234 } else if (cpu_isar_feature(aa64_mte_insn_reg, cpu)) {
8235 define_arm_cp_regs(cpu, mte_tco_ro_reginfo);
5463df16 8236 define_arm_cp_regs(cpu, mte_el0_cacheop_reginfo);
4b779ceb 8237 }
967aa94f 8238#endif
cb570bd3 8239
22e57073 8240 if (cpu_isar_feature(any_predinv, cpu)) {
cb570bd3
RH
8241 define_arm_cp_regs(cpu, predinv_reginfo);
8242 }
e2cce18f 8243
957e6155
PM
8244 if (cpu_isar_feature(any_ccidx, cpu)) {
8245 define_arm_cp_regs(cpu, ccsidr2_reginfo);
8246 }
8247
e2cce18f
RH
8248#ifndef CONFIG_USER_ONLY
8249 /*
8250 * Register redirections and aliases must be done last,
8251 * after the registers from the other extensions have been defined.
8252 */
8253 if (arm_feature(env, ARM_FEATURE_EL2) && cpu_isar_feature(aa64_vh, cpu)) {
8254 define_arm_vh_e2h_redirects_aliases(cpu);
8255 }
8256#endif
2ceb98c0
PM
8257}
8258
14969266
AF
8259void arm_cpu_register_gdb_regs_for_features(ARMCPU *cpu)
8260{
22169d41 8261 CPUState *cs = CPU(cpu);
14969266
AF
8262 CPUARMState *env = &cpu->env;
8263
6a669427 8264 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
d12379c5
AB
8265 /*
8266 * The lower part of each SVE register aliases to the FPU
8267 * registers so we don't need to include both.
8268 */
8269#ifdef TARGET_AARCH64
8270 if (isar_feature_aa64_sve(&cpu->isar)) {
8271 gdb_register_coprocessor(cs, arm_gdb_get_svereg, arm_gdb_set_svereg,
8272 arm_gen_dynamic_svereg_xml(cs, cs->gdb_num_regs),
8273 "sve-registers.xml", 0);
8274 } else
8275#endif
8276 {
8277 gdb_register_coprocessor(cs, aarch64_fpu_gdb_get_reg,
8278 aarch64_fpu_gdb_set_reg,
8279 34, "aarch64-fpu.xml", 0);
8280 }
6a669427 8281 } else if (arm_feature(env, ARM_FEATURE_NEON)) {
22169d41 8282 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
56aebc89 8283 51, "arm-neon.xml", 0);
a6627f5f 8284 } else if (cpu_isar_feature(aa32_simd_r32, cpu)) {
22169d41 8285 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
56aebc89 8286 35, "arm-vfp3.xml", 0);
7fbc6a40 8287 } else if (cpu_isar_feature(aa32_vfp_simd, cpu)) {
22169d41 8288 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
56aebc89
PB
8289 19, "arm-vfp.xml", 0);
8290 }
200bf5b7 8291 gdb_register_coprocessor(cs, arm_gdb_get_sysreg, arm_gdb_set_sysreg,
32d6e32a 8292 arm_gen_dynamic_sysreg_xml(cs, cs->gdb_num_regs),
200bf5b7 8293 "system-registers.xml", 0);
d12379c5 8294
40f137e1
PB
8295}
8296
777dc784
PM
8297/* Sort alphabetically by type name, except for "any". */
8298static gint arm_cpu_list_compare(gconstpointer a, gconstpointer b)
5adb4839 8299{
777dc784
PM
8300 ObjectClass *class_a = (ObjectClass *)a;
8301 ObjectClass *class_b = (ObjectClass *)b;
8302 const char *name_a, *name_b;
5adb4839 8303
777dc784
PM
8304 name_a = object_class_get_name(class_a);
8305 name_b = object_class_get_name(class_b);
51492fd1 8306 if (strcmp(name_a, "any-" TYPE_ARM_CPU) == 0) {
777dc784 8307 return 1;
51492fd1 8308 } else if (strcmp(name_b, "any-" TYPE_ARM_CPU) == 0) {
777dc784
PM
8309 return -1;
8310 } else {
8311 return strcmp(name_a, name_b);
5adb4839
PB
8312 }
8313}
8314
777dc784 8315static void arm_cpu_list_entry(gpointer data, gpointer user_data)
40f137e1 8316{
777dc784 8317 ObjectClass *oc = data;
51492fd1
AF
8318 const char *typename;
8319 char *name;
3371d272 8320
51492fd1
AF
8321 typename = object_class_get_name(oc);
8322 name = g_strndup(typename, strlen(typename) - strlen("-" TYPE_ARM_CPU));
0442428a 8323 qemu_printf(" %s\n", name);
51492fd1 8324 g_free(name);
777dc784
PM
8325}
8326
0442428a 8327void arm_cpu_list(void)
777dc784 8328{
777dc784
PM
8329 GSList *list;
8330
8331 list = object_class_get_list(TYPE_ARM_CPU, false);
8332 list = g_slist_sort(list, arm_cpu_list_compare);
0442428a
MA
8333 qemu_printf("Available CPUs:\n");
8334 g_slist_foreach(list, arm_cpu_list_entry, NULL);
777dc784 8335 g_slist_free(list);
40f137e1
PB
8336}
8337
78027bb6
CR
8338static void arm_cpu_add_definition(gpointer data, gpointer user_data)
8339{
8340 ObjectClass *oc = data;
8341 CpuDefinitionInfoList **cpu_list = user_data;
78027bb6
CR
8342 CpuDefinitionInfo *info;
8343 const char *typename;
8344
8345 typename = object_class_get_name(oc);
8346 info = g_malloc0(sizeof(*info));
8347 info->name = g_strndup(typename,
8348 strlen(typename) - strlen("-" TYPE_ARM_CPU));
8ed877b7 8349 info->q_typename = g_strdup(typename);
78027bb6 8350
54aa3de7 8351 QAPI_LIST_PREPEND(*cpu_list, info);
78027bb6
CR
8352}
8353
25a9d6ca 8354CpuDefinitionInfoList *qmp_query_cpu_definitions(Error **errp)
78027bb6
CR
8355{
8356 CpuDefinitionInfoList *cpu_list = NULL;
8357 GSList *list;
8358
8359 list = object_class_get_list(TYPE_ARM_CPU, false);
8360 g_slist_foreach(list, arm_cpu_add_definition, &cpu_list);
8361 g_slist_free(list);
8362
8363 return cpu_list;
8364}
8365
6e6efd61 8366static void add_cpreg_to_hashtable(ARMCPU *cpu, const ARMCPRegInfo *r,
51a79b03 8367 void *opaque, int state, int secstate,
9c513e78
AB
8368 int crm, int opc1, int opc2,
8369 const char *name)
6e6efd61
PM
8370{
8371 /* Private utility function for define_one_arm_cp_reg_with_opaque():
8372 * add a single reginfo struct to the hash table.
8373 */
8374 uint32_t *key = g_new(uint32_t, 1);
8375 ARMCPRegInfo *r2 = g_memdup(r, sizeof(ARMCPRegInfo));
8376 int is64 = (r->type & ARM_CP_64BIT) ? 1 : 0;
3f3c82a5
FA
8377 int ns = (secstate & ARM_CP_SECSTATE_NS) ? 1 : 0;
8378
9c513e78 8379 r2->name = g_strdup(name);
3f3c82a5
FA
8380 /* Reset the secure state to the specific incoming state. This is
8381 * necessary as the register may have been defined with both states.
8382 */
8383 r2->secure = secstate;
8384
8385 if (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1]) {
8386 /* Register is banked (using both entries in array).
8387 * Overwriting fieldoffset as the array is only used to define
8388 * banked registers but later only fieldoffset is used.
f5a0a5a5 8389 */
3f3c82a5
FA
8390 r2->fieldoffset = r->bank_fieldoffsets[ns];
8391 }
8392
8393 if (state == ARM_CP_STATE_AA32) {
8394 if (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1]) {
8395 /* If the register is banked then we don't need to migrate or
8396 * reset the 32-bit instance in certain cases:
8397 *
8398 * 1) If the register has both 32-bit and 64-bit instances then we
8399 * can count on the 64-bit instance taking care of the
8400 * non-secure bank.
8401 * 2) If ARMv8 is enabled then we can count on a 64-bit version
8402 * taking care of the secure bank. This requires that separate
8403 * 32 and 64-bit definitions are provided.
8404 */
8405 if ((r->state == ARM_CP_STATE_BOTH && ns) ||
8406 (arm_feature(&cpu->env, ARM_FEATURE_V8) && !ns)) {
7a0e58fa 8407 r2->type |= ARM_CP_ALIAS;
3f3c82a5
FA
8408 }
8409 } else if ((secstate != r->secure) && !ns) {
8410 /* The register is not banked so we only want to allow migration of
8411 * the non-secure instance.
8412 */
7a0e58fa 8413 r2->type |= ARM_CP_ALIAS;
58a1d8ce 8414 }
3f3c82a5
FA
8415
8416 if (r->state == ARM_CP_STATE_BOTH) {
8417 /* We assume it is a cp15 register if the .cp field is left unset.
8418 */
8419 if (r2->cp == 0) {
8420 r2->cp = 15;
8421 }
8422
f5a0a5a5 8423#ifdef HOST_WORDS_BIGENDIAN
3f3c82a5
FA
8424 if (r2->fieldoffset) {
8425 r2->fieldoffset += sizeof(uint32_t);
8426 }
f5a0a5a5 8427#endif
3f3c82a5 8428 }
f5a0a5a5
PM
8429 }
8430 if (state == ARM_CP_STATE_AA64) {
8431 /* To allow abbreviation of ARMCPRegInfo
8432 * definitions, we treat cp == 0 as equivalent to
8433 * the value for "standard guest-visible sysreg".
58a1d8ce
PM
8434 * STATE_BOTH definitions are also always "standard
8435 * sysreg" in their AArch64 view (the .cp value may
8436 * be non-zero for the benefit of the AArch32 view).
f5a0a5a5 8437 */
58a1d8ce 8438 if (r->cp == 0 || r->state == ARM_CP_STATE_BOTH) {
f5a0a5a5
PM
8439 r2->cp = CP_REG_ARM64_SYSREG_CP;
8440 }
8441 *key = ENCODE_AA64_CP_REG(r2->cp, r2->crn, crm,
8442 r2->opc0, opc1, opc2);
8443 } else {
51a79b03 8444 *key = ENCODE_CP_REG(r2->cp, is64, ns, r2->crn, crm, opc1, opc2);
f5a0a5a5 8445 }
6e6efd61
PM
8446 if (opaque) {
8447 r2->opaque = opaque;
8448 }
67ed771d
PM
8449 /* reginfo passed to helpers is correct for the actual access,
8450 * and is never ARM_CP_STATE_BOTH:
8451 */
8452 r2->state = state;
6e6efd61
PM
8453 /* Make sure reginfo passed to helpers for wildcarded regs
8454 * has the correct crm/opc1/opc2 for this reg, not CP_ANY:
8455 */
8456 r2->crm = crm;
8457 r2->opc1 = opc1;
8458 r2->opc2 = opc2;
8459 /* By convention, for wildcarded registers only the first
8460 * entry is used for migration; the others are marked as
7a0e58fa 8461 * ALIAS so we don't try to transfer the register
6e6efd61 8462 * multiple times. Special registers (ie NOP/WFI) are
7a0e58fa 8463 * never migratable and not even raw-accessible.
6e6efd61 8464 */
7a0e58fa
PM
8465 if ((r->type & ARM_CP_SPECIAL)) {
8466 r2->type |= ARM_CP_NO_RAW;
8467 }
8468 if (((r->crm == CP_ANY) && crm != 0) ||
6e6efd61
PM
8469 ((r->opc1 == CP_ANY) && opc1 != 0) ||
8470 ((r->opc2 == CP_ANY) && opc2 != 0)) {
1f163787 8471 r2->type |= ARM_CP_ALIAS | ARM_CP_NO_GDB;
6e6efd61
PM
8472 }
8473
375421cc
PM
8474 /* Check that raw accesses are either forbidden or handled. Note that
8475 * we can't assert this earlier because the setup of fieldoffset for
8476 * banked registers has to be done first.
8477 */
8478 if (!(r2->type & ARM_CP_NO_RAW)) {
8479 assert(!raw_accessors_invalid(r2));
8480 }
8481
6e6efd61
PM
8482 /* Overriding of an existing definition must be explicitly
8483 * requested.
8484 */
8485 if (!(r->type & ARM_CP_OVERRIDE)) {
8486 ARMCPRegInfo *oldreg;
8487 oldreg = g_hash_table_lookup(cpu->cp_regs, key);
8488 if (oldreg && !(oldreg->type & ARM_CP_OVERRIDE)) {
8489 fprintf(stderr, "Register redefined: cp=%d %d bit "
8490 "crn=%d crm=%d opc1=%d opc2=%d, "
8491 "was %s, now %s\n", r2->cp, 32 + 32 * is64,
8492 r2->crn, r2->crm, r2->opc1, r2->opc2,
8493 oldreg->name, r2->name);
8494 g_assert_not_reached();
8495 }
8496 }
8497 g_hash_table_insert(cpu->cp_regs, key, r2);
8498}
8499
8500
4b6a83fb
PM
8501void define_one_arm_cp_reg_with_opaque(ARMCPU *cpu,
8502 const ARMCPRegInfo *r, void *opaque)
8503{
8504 /* Define implementations of coprocessor registers.
8505 * We store these in a hashtable because typically
8506 * there are less than 150 registers in a space which
8507 * is 16*16*16*8*8 = 262144 in size.
8508 * Wildcarding is supported for the crm, opc1 and opc2 fields.
8509 * If a register is defined twice then the second definition is
8510 * used, so this can be used to define some generic registers and
8511 * then override them with implementation specific variations.
8512 * At least one of the original and the second definition should
8513 * include ARM_CP_OVERRIDE in its type bits -- this is just a guard
8514 * against accidental use.
f5a0a5a5
PM
8515 *
8516 * The state field defines whether the register is to be
8517 * visible in the AArch32 or AArch64 execution state. If the
8518 * state is set to ARM_CP_STATE_BOTH then we synthesise a
8519 * reginfo structure for the AArch32 view, which sees the lower
8520 * 32 bits of the 64 bit register.
8521 *
8522 * Only registers visible in AArch64 may set r->opc0; opc0 cannot
8523 * be wildcarded. AArch64 registers are always considered to be 64
8524 * bits; the ARM_CP_64BIT* flag applies only to the AArch32 view of
8525 * the register, if any.
4b6a83fb 8526 */
f5a0a5a5 8527 int crm, opc1, opc2, state;
4b6a83fb
PM
8528 int crmmin = (r->crm == CP_ANY) ? 0 : r->crm;
8529 int crmmax = (r->crm == CP_ANY) ? 15 : r->crm;
8530 int opc1min = (r->opc1 == CP_ANY) ? 0 : r->opc1;
8531 int opc1max = (r->opc1 == CP_ANY) ? 7 : r->opc1;
8532 int opc2min = (r->opc2 == CP_ANY) ? 0 : r->opc2;
8533 int opc2max = (r->opc2 == CP_ANY) ? 7 : r->opc2;
8534 /* 64 bit registers have only CRm and Opc1 fields */
8535 assert(!((r->type & ARM_CP_64BIT) && (r->opc2 || r->crn)));
f5a0a5a5
PM
8536 /* op0 only exists in the AArch64 encodings */
8537 assert((r->state != ARM_CP_STATE_AA32) || (r->opc0 == 0));
8538 /* AArch64 regs are all 64 bit so ARM_CP_64BIT is meaningless */
8539 assert((r->state != ARM_CP_STATE_AA64) || !(r->type & ARM_CP_64BIT));
cd8be50e
PM
8540 /*
8541 * This API is only for Arm's system coprocessors (14 and 15) or
8542 * (M-profile or v7A-and-earlier only) for implementation defined
8543 * coprocessors in the range 0..7. Our decode assumes this, since
8544 * 8..13 can be used for other insns including VFP and Neon. See
8545 * valid_cp() in translate.c. Assert here that we haven't tried
8546 * to use an invalid coprocessor number.
8547 */
8548 switch (r->state) {
8549 case ARM_CP_STATE_BOTH:
8550 /* 0 has a special meaning, but otherwise the same rules as AA32. */
8551 if (r->cp == 0) {
8552 break;
8553 }
8554 /* fall through */
8555 case ARM_CP_STATE_AA32:
8556 if (arm_feature(&cpu->env, ARM_FEATURE_V8) &&
8557 !arm_feature(&cpu->env, ARM_FEATURE_M)) {
8558 assert(r->cp >= 14 && r->cp <= 15);
8559 } else {
8560 assert(r->cp < 8 || (r->cp >= 14 && r->cp <= 15));
8561 }
8562 break;
8563 case ARM_CP_STATE_AA64:
8564 assert(r->cp == 0 || r->cp == CP_REG_ARM64_SYSREG_CP);
8565 break;
8566 default:
8567 g_assert_not_reached();
8568 }
f5a0a5a5
PM
8569 /* The AArch64 pseudocode CheckSystemAccess() specifies that op1
8570 * encodes a minimum access level for the register. We roll this
8571 * runtime check into our general permission check code, so check
8572 * here that the reginfo's specified permissions are strict enough
8573 * to encompass the generic architectural permission check.
8574 */
8575 if (r->state != ARM_CP_STATE_AA32) {
8576 int mask = 0;
8577 switch (r->opc1) {
b5bd7440
AB
8578 case 0:
8579 /* min_EL EL1, but some accessible to EL0 via kernel ABI */
8580 mask = PL0U_R | PL1_RW;
8581 break;
8582 case 1: case 2:
f5a0a5a5
PM
8583 /* min_EL EL1 */
8584 mask = PL1_RW;
8585 break;
8586 case 3:
8587 /* min_EL EL0 */
8588 mask = PL0_RW;
8589 break;
8590 case 4:
b4ecf60f 8591 case 5:
f5a0a5a5
PM
8592 /* min_EL EL2 */
8593 mask = PL2_RW;
8594 break;
f5a0a5a5
PM
8595 case 6:
8596 /* min_EL EL3 */
8597 mask = PL3_RW;
8598 break;
8599 case 7:
8600 /* min_EL EL1, secure mode only (we don't check the latter) */
8601 mask = PL1_RW;
8602 break;
8603 default:
8604 /* broken reginfo with out-of-range opc1 */
8605 assert(false);
8606 break;
8607 }
8608 /* assert our permissions are not too lax (stricter is fine) */
8609 assert((r->access & ~mask) == 0);
8610 }
8611
4b6a83fb
PM
8612 /* Check that the register definition has enough info to handle
8613 * reads and writes if they are permitted.
8614 */
8615 if (!(r->type & (ARM_CP_SPECIAL|ARM_CP_CONST))) {
8616 if (r->access & PL3_R) {
3f3c82a5
FA
8617 assert((r->fieldoffset ||
8618 (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
8619 r->readfn);
4b6a83fb
PM
8620 }
8621 if (r->access & PL3_W) {
3f3c82a5
FA
8622 assert((r->fieldoffset ||
8623 (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
8624 r->writefn);
4b6a83fb
PM
8625 }
8626 }
8627 /* Bad type field probably means missing sentinel at end of reg list */
8628 assert(cptype_valid(r->type));
8629 for (crm = crmmin; crm <= crmmax; crm++) {
8630 for (opc1 = opc1min; opc1 <= opc1max; opc1++) {
8631 for (opc2 = opc2min; opc2 <= opc2max; opc2++) {
f5a0a5a5
PM
8632 for (state = ARM_CP_STATE_AA32;
8633 state <= ARM_CP_STATE_AA64; state++) {
8634 if (r->state != state && r->state != ARM_CP_STATE_BOTH) {
8635 continue;
8636 }
3f3c82a5
FA
8637 if (state == ARM_CP_STATE_AA32) {
8638 /* Under AArch32 CP registers can be common
8639 * (same for secure and non-secure world) or banked.
8640 */
9c513e78
AB
8641 char *name;
8642
3f3c82a5
FA
8643 switch (r->secure) {
8644 case ARM_CP_SECSTATE_S:
8645 case ARM_CP_SECSTATE_NS:
8646 add_cpreg_to_hashtable(cpu, r, opaque, state,
9c513e78
AB
8647 r->secure, crm, opc1, opc2,
8648 r->name);
3f3c82a5
FA
8649 break;
8650 default:
9c513e78 8651 name = g_strdup_printf("%s_S", r->name);
3f3c82a5
FA
8652 add_cpreg_to_hashtable(cpu, r, opaque, state,
8653 ARM_CP_SECSTATE_S,
9c513e78
AB
8654 crm, opc1, opc2, name);
8655 g_free(name);
3f3c82a5
FA
8656 add_cpreg_to_hashtable(cpu, r, opaque, state,
8657 ARM_CP_SECSTATE_NS,
9c513e78 8658 crm, opc1, opc2, r->name);
3f3c82a5
FA
8659 break;
8660 }
8661 } else {
8662 /* AArch64 registers get mapped to non-secure instance
8663 * of AArch32 */
8664 add_cpreg_to_hashtable(cpu, r, opaque, state,
8665 ARM_CP_SECSTATE_NS,
9c513e78 8666 crm, opc1, opc2, r->name);
3f3c82a5 8667 }
f5a0a5a5 8668 }
4b6a83fb
PM
8669 }
8670 }
8671 }
8672}
8673
8674void define_arm_cp_regs_with_opaque(ARMCPU *cpu,
8675 const ARMCPRegInfo *regs, void *opaque)
8676{
8677 /* Define a whole list of registers */
8678 const ARMCPRegInfo *r;
8679 for (r = regs; r->type != ARM_CP_SENTINEL; r++) {
8680 define_one_arm_cp_reg_with_opaque(cpu, r, opaque);
8681 }
8682}
8683
6c5c0fec
AB
8684/*
8685 * Modify ARMCPRegInfo for access from userspace.
8686 *
8687 * This is a data driven modification directed by
8688 * ARMCPRegUserSpaceInfo. All registers become ARM_CP_CONST as
8689 * user-space cannot alter any values and dynamic values pertaining to
8690 * execution state are hidden from user space view anyway.
8691 */
8692void modify_arm_cp_regs(ARMCPRegInfo *regs, const ARMCPRegUserSpaceInfo *mods)
8693{
8694 const ARMCPRegUserSpaceInfo *m;
8695 ARMCPRegInfo *r;
8696
8697 for (m = mods; m->name; m++) {
d040242e
AB
8698 GPatternSpec *pat = NULL;
8699 if (m->is_glob) {
8700 pat = g_pattern_spec_new(m->name);
8701 }
6c5c0fec 8702 for (r = regs; r->type != ARM_CP_SENTINEL; r++) {
d040242e
AB
8703 if (pat && g_pattern_match_string(pat, r->name)) {
8704 r->type = ARM_CP_CONST;
8705 r->access = PL0U_R;
8706 r->resetvalue = 0;
8707 /* continue */
8708 } else if (strcmp(r->name, m->name) == 0) {
6c5c0fec
AB
8709 r->type = ARM_CP_CONST;
8710 r->access = PL0U_R;
8711 r->resetvalue &= m->exported_bits;
8712 r->resetvalue |= m->fixed_bits;
8713 break;
8714 }
8715 }
d040242e
AB
8716 if (pat) {
8717 g_pattern_spec_free(pat);
8718 }
6c5c0fec
AB
8719 }
8720}
8721
60322b39 8722const ARMCPRegInfo *get_arm_cp_reginfo(GHashTable *cpregs, uint32_t encoded_cp)
4b6a83fb 8723{
60322b39 8724 return g_hash_table_lookup(cpregs, &encoded_cp);
4b6a83fb
PM
8725}
8726
c4241c7d
PM
8727void arm_cp_write_ignore(CPUARMState *env, const ARMCPRegInfo *ri,
8728 uint64_t value)
4b6a83fb
PM
8729{
8730 /* Helper coprocessor write function for write-ignore registers */
4b6a83fb
PM
8731}
8732
c4241c7d 8733uint64_t arm_cp_read_zero(CPUARMState *env, const ARMCPRegInfo *ri)
4b6a83fb
PM
8734{
8735 /* Helper coprocessor write function for read-as-zero registers */
4b6a83fb
PM
8736 return 0;
8737}
8738
f5a0a5a5
PM
8739void arm_cp_reset_ignore(CPUARMState *env, const ARMCPRegInfo *opaque)
8740{
8741 /* Helper coprocessor reset function for do-nothing-on-reset registers */
8742}
8743
af393ffc 8744static int bad_mode_switch(CPUARMState *env, int mode, CPSRWriteType write_type)
37064a8b
PM
8745{
8746 /* Return true if it is not valid for us to switch to
8747 * this CPU mode (ie all the UNPREDICTABLE cases in
8748 * the ARM ARM CPSRWriteByInstr pseudocode).
8749 */
af393ffc
PM
8750
8751 /* Changes to or from Hyp via MSR and CPS are illegal. */
8752 if (write_type == CPSRWriteByInstr &&
8753 ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_HYP ||
8754 mode == ARM_CPU_MODE_HYP)) {
8755 return 1;
8756 }
8757
37064a8b
PM
8758 switch (mode) {
8759 case ARM_CPU_MODE_USR:
10eacda7 8760 return 0;
37064a8b
PM
8761 case ARM_CPU_MODE_SYS:
8762 case ARM_CPU_MODE_SVC:
8763 case ARM_CPU_MODE_ABT:
8764 case ARM_CPU_MODE_UND:
8765 case ARM_CPU_MODE_IRQ:
8766 case ARM_CPU_MODE_FIQ:
52ff951b
PM
8767 /* Note that we don't implement the IMPDEF NSACR.RFR which in v7
8768 * allows FIQ mode to be Secure-only. (In v8 this doesn't exist.)
8769 */
10eacda7
PM
8770 /* If HCR.TGE is set then changes from Monitor to NS PL1 via MSR
8771 * and CPS are treated as illegal mode changes.
8772 */
8773 if (write_type == CPSRWriteByInstr &&
10eacda7 8774 (env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON &&
7c208e0f 8775 (arm_hcr_el2_eff(env) & HCR_TGE)) {
10eacda7
PM
8776 return 1;
8777 }
37064a8b 8778 return 0;
e6c8fc07 8779 case ARM_CPU_MODE_HYP:
e6ef0169 8780 return !arm_is_el2_enabled(env) || arm_current_el(env) < 2;
027fc527 8781 case ARM_CPU_MODE_MON:
58ae2d1f 8782 return arm_current_el(env) < 3;
37064a8b
PM
8783 default:
8784 return 1;
8785 }
8786}
8787
2f4a40e5
AZ
8788uint32_t cpsr_read(CPUARMState *env)
8789{
8790 int ZF;
6fbe23d5
PB
8791 ZF = (env->ZF == 0);
8792 return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) |
2f4a40e5
AZ
8793 (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27)
8794 | (env->thumb << 5) | ((env->condexec_bits & 3) << 25)
8795 | ((env->condexec_bits & 0xfc) << 8)
af519934 8796 | (env->GE << 16) | (env->daif & CPSR_AIF);
2f4a40e5
AZ
8797}
8798
50866ba5
PM
8799void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask,
8800 CPSRWriteType write_type)
2f4a40e5 8801{
6e8801f9
FA
8802 uint32_t changed_daif;
8803
2f4a40e5 8804 if (mask & CPSR_NZCV) {
6fbe23d5
PB
8805 env->ZF = (~val) & CPSR_Z;
8806 env->NF = val;
2f4a40e5
AZ
8807 env->CF = (val >> 29) & 1;
8808 env->VF = (val << 3) & 0x80000000;
8809 }
8810 if (mask & CPSR_Q)
8811 env->QF = ((val & CPSR_Q) != 0);
8812 if (mask & CPSR_T)
8813 env->thumb = ((val & CPSR_T) != 0);
8814 if (mask & CPSR_IT_0_1) {
8815 env->condexec_bits &= ~3;
8816 env->condexec_bits |= (val >> 25) & 3;
8817 }
8818 if (mask & CPSR_IT_2_7) {
8819 env->condexec_bits &= 3;
8820 env->condexec_bits |= (val >> 8) & 0xfc;
8821 }
8822 if (mask & CPSR_GE) {
8823 env->GE = (val >> 16) & 0xf;
8824 }
8825
6e8801f9
FA
8826 /* In a V7 implementation that includes the security extensions but does
8827 * not include Virtualization Extensions the SCR.FW and SCR.AW bits control
8828 * whether non-secure software is allowed to change the CPSR_F and CPSR_A
8829 * bits respectively.
8830 *
8831 * In a V8 implementation, it is permitted for privileged software to
8832 * change the CPSR A/F bits regardless of the SCR.AW/FW bits.
8833 */
f8c88bbc 8834 if (write_type != CPSRWriteRaw && !arm_feature(env, ARM_FEATURE_V8) &&
6e8801f9
FA
8835 arm_feature(env, ARM_FEATURE_EL3) &&
8836 !arm_feature(env, ARM_FEATURE_EL2) &&
8837 !arm_is_secure(env)) {
8838
8839 changed_daif = (env->daif ^ val) & mask;
8840
8841 if (changed_daif & CPSR_A) {
8842 /* Check to see if we are allowed to change the masking of async
8843 * abort exceptions from a non-secure state.
8844 */
8845 if (!(env->cp15.scr_el3 & SCR_AW)) {
8846 qemu_log_mask(LOG_GUEST_ERROR,
8847 "Ignoring attempt to switch CPSR_A flag from "
8848 "non-secure world with SCR.AW bit clear\n");
8849 mask &= ~CPSR_A;
8850 }
8851 }
8852
8853 if (changed_daif & CPSR_F) {
8854 /* Check to see if we are allowed to change the masking of FIQ
8855 * exceptions from a non-secure state.
8856 */
8857 if (!(env->cp15.scr_el3 & SCR_FW)) {
8858 qemu_log_mask(LOG_GUEST_ERROR,
8859 "Ignoring attempt to switch CPSR_F flag from "
8860 "non-secure world with SCR.FW bit clear\n");
8861 mask &= ~CPSR_F;
8862 }
8863
8864 /* Check whether non-maskable FIQ (NMFI) support is enabled.
8865 * If this bit is set software is not allowed to mask
8866 * FIQs, but is allowed to set CPSR_F to 0.
8867 */
8868 if ((A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_NMFI) &&
8869 (val & CPSR_F)) {
8870 qemu_log_mask(LOG_GUEST_ERROR,
8871 "Ignoring attempt to enable CPSR_F flag "
8872 "(non-maskable FIQ [NMFI] support enabled)\n");
8873 mask &= ~CPSR_F;
8874 }
8875 }
8876 }
8877
4cc35614
PM
8878 env->daif &= ~(CPSR_AIF & mask);
8879 env->daif |= val & CPSR_AIF & mask;
8880
f8c88bbc
PM
8881 if (write_type != CPSRWriteRaw &&
8882 ((env->uncached_cpsr ^ val) & mask & CPSR_M)) {
8c4f0eb9
PM
8883 if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_USR) {
8884 /* Note that we can only get here in USR mode if this is a
8885 * gdb stub write; for this case we follow the architectural
8886 * behaviour for guest writes in USR mode of ignoring an attempt
8887 * to switch mode. (Those are caught by translate.c for writes
8888 * triggered by guest instructions.)
8889 */
8890 mask &= ~CPSR_M;
8891 } else if (bad_mode_switch(env, val & CPSR_M, write_type)) {
81907a58
PM
8892 /* Attempt to switch to an invalid mode: this is UNPREDICTABLE in
8893 * v7, and has defined behaviour in v8:
8894 * + leave CPSR.M untouched
8895 * + allow changes to the other CPSR fields
8896 * + set PSTATE.IL
8897 * For user changes via the GDB stub, we don't set PSTATE.IL,
8898 * as this would be unnecessarily harsh for a user error.
37064a8b
PM
8899 */
8900 mask &= ~CPSR_M;
81907a58
PM
8901 if (write_type != CPSRWriteByGDBStub &&
8902 arm_feature(env, ARM_FEATURE_V8)) {
8903 mask |= CPSR_IL;
8904 val |= CPSR_IL;
8905 }
81e37284
PM
8906 qemu_log_mask(LOG_GUEST_ERROR,
8907 "Illegal AArch32 mode switch attempt from %s to %s\n",
8908 aarch32_mode_name(env->uncached_cpsr),
8909 aarch32_mode_name(val));
37064a8b 8910 } else {
81e37284
PM
8911 qemu_log_mask(CPU_LOG_INT, "%s %s to %s PC 0x%" PRIx32 "\n",
8912 write_type == CPSRWriteExceptionReturn ?
8913 "Exception return from AArch32" :
8914 "AArch32 mode switch from",
8915 aarch32_mode_name(env->uncached_cpsr),
8916 aarch32_mode_name(val), env->regs[15]);
37064a8b
PM
8917 switch_mode(env, val & CPSR_M);
8918 }
2f4a40e5
AZ
8919 }
8920 mask &= ~CACHED_CPSR_BITS;
8921 env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask);
8922}
8923
b26eefb6
PB
8924/* Sign/zero extend */
8925uint32_t HELPER(sxtb16)(uint32_t x)
8926{
8927 uint32_t res;
8928 res = (uint16_t)(int8_t)x;
8929 res |= (uint32_t)(int8_t)(x >> 16) << 16;
8930 return res;
8931}
8932
8933uint32_t HELPER(uxtb16)(uint32_t x)
8934{
8935 uint32_t res;
8936 res = (uint16_t)(uint8_t)x;
8937 res |= (uint32_t)(uint8_t)(x >> 16) << 16;
8938 return res;
8939}
8940
3670669c
PB
8941int32_t HELPER(sdiv)(int32_t num, int32_t den)
8942{
8943 if (den == 0)
8944 return 0;
686eeb93
AJ
8945 if (num == INT_MIN && den == -1)
8946 return INT_MIN;
3670669c
PB
8947 return num / den;
8948}
8949
8950uint32_t HELPER(udiv)(uint32_t num, uint32_t den)
8951{
8952 if (den == 0)
8953 return 0;
8954 return num / den;
8955}
8956
8957uint32_t HELPER(rbit)(uint32_t x)
8958{
42fedbca 8959 return revbit32(x);
3670669c
PB
8960}
8961
c47eaf9f 8962#ifdef CONFIG_USER_ONLY
b5ff1b31 8963
affdb64d 8964static void switch_mode(CPUARMState *env, int mode)
b5ff1b31 8965{
2fc0cc0e 8966 ARMCPU *cpu = env_archcpu(env);
a47dddd7
AF
8967
8968 if (mode != ARM_CPU_MODE_USR) {
8969 cpu_abort(CPU(cpu), "Tried to switch out of user mode\n");
8970 }
b5ff1b31
FB
8971}
8972
012a906b
GB
8973uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
8974 uint32_t cur_el, bool secure)
9e729b57
EI
8975{
8976 return 1;
8977}
8978
ce02049d
GB
8979void aarch64_sync_64_to_32(CPUARMState *env)
8980{
8981 g_assert_not_reached();
8982}
8983
b5ff1b31
FB
8984#else
8985
affdb64d 8986static void switch_mode(CPUARMState *env, int mode)
b5ff1b31
FB
8987{
8988 int old_mode;
8989 int i;
8990
8991 old_mode = env->uncached_cpsr & CPSR_M;
8992 if (mode == old_mode)
8993 return;
8994
8995 if (old_mode == ARM_CPU_MODE_FIQ) {
8996 memcpy (env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t));
8637c67f 8997 memcpy (env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t));
b5ff1b31
FB
8998 } else if (mode == ARM_CPU_MODE_FIQ) {
8999 memcpy (env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t));
8637c67f 9000 memcpy (env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t));
b5ff1b31
FB
9001 }
9002
f5206413 9003 i = bank_number(old_mode);
b5ff1b31 9004 env->banked_r13[i] = env->regs[13];
b5ff1b31
FB
9005 env->banked_spsr[i] = env->spsr;
9006
f5206413 9007 i = bank_number(mode);
b5ff1b31 9008 env->regs[13] = env->banked_r13[i];
b5ff1b31 9009 env->spsr = env->banked_spsr[i];
593cfa2b
PM
9010
9011 env->banked_r14[r14_bank_number(old_mode)] = env->regs[14];
9012 env->regs[14] = env->banked_r14[r14_bank_number(mode)];
b5ff1b31
FB
9013}
9014
0eeb17d6
GB
9015/* Physical Interrupt Target EL Lookup Table
9016 *
9017 * [ From ARM ARM section G1.13.4 (Table G1-15) ]
9018 *
9019 * The below multi-dimensional table is used for looking up the target
9020 * exception level given numerous condition criteria. Specifically, the
9021 * target EL is based on SCR and HCR routing controls as well as the
9022 * currently executing EL and secure state.
9023 *
9024 * Dimensions:
9025 * target_el_table[2][2][2][2][2][4]
9026 * | | | | | +--- Current EL
9027 * | | | | +------ Non-secure(0)/Secure(1)
9028 * | | | +--------- HCR mask override
9029 * | | +------------ SCR exec state control
9030 * | +--------------- SCR mask override
9031 * +------------------ 32-bit(0)/64-bit(1) EL3
9032 *
9033 * The table values are as such:
9034 * 0-3 = EL0-EL3
9035 * -1 = Cannot occur
9036 *
9037 * The ARM ARM target EL table includes entries indicating that an "exception
9038 * is not taken". The two cases where this is applicable are:
9039 * 1) An exception is taken from EL3 but the SCR does not have the exception
9040 * routed to EL3.
9041 * 2) An exception is taken from EL2 but the HCR does not have the exception
9042 * routed to EL2.
9043 * In these two cases, the below table contain a target of EL1. This value is
9044 * returned as it is expected that the consumer of the table data will check
9045 * for "target EL >= current EL" to ensure the exception is not taken.
9046 *
9047 * SCR HCR
9048 * 64 EA AMO From
9049 * BIT IRQ IMO Non-secure Secure
9050 * EL3 FIQ RW FMO EL0 EL1 EL2 EL3 EL0 EL1 EL2 EL3
9051 */
82c39f6a 9052static const int8_t target_el_table[2][2][2][2][2][4] = {
0eeb17d6
GB
9053 {{{{/* 0 0 0 0 */{ 1, 1, 2, -1 },{ 3, -1, -1, 3 },},
9054 {/* 0 0 0 1 */{ 2, 2, 2, -1 },{ 3, -1, -1, 3 },},},
9055 {{/* 0 0 1 0 */{ 1, 1, 2, -1 },{ 3, -1, -1, 3 },},
9056 {/* 0 0 1 1 */{ 2, 2, 2, -1 },{ 3, -1, -1, 3 },},},},
9057 {{{/* 0 1 0 0 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},
9058 {/* 0 1 0 1 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},},
9059 {{/* 0 1 1 0 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},
9060 {/* 0 1 1 1 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},},},},
9061 {{{{/* 1 0 0 0 */{ 1, 1, 2, -1 },{ 1, 1, -1, 1 },},
6c85f906
RDC
9062 {/* 1 0 0 1 */{ 2, 2, 2, -1 },{ 2, 2, -1, 1 },},},
9063 {{/* 1 0 1 0 */{ 1, 1, 1, -1 },{ 1, 1, 1, 1 },},
9064 {/* 1 0 1 1 */{ 2, 2, 2, -1 },{ 2, 2, 2, 1 },},},},
0eeb17d6
GB
9065 {{{/* 1 1 0 0 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},
9066 {/* 1 1 0 1 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},},
6c85f906
RDC
9067 {{/* 1 1 1 0 */{ 3, 3, 3, -1 },{ 3, 3, 3, 3 },},
9068 {/* 1 1 1 1 */{ 3, 3, 3, -1 },{ 3, 3, 3, 3 },},},},},
0eeb17d6
GB
9069};
9070
9071/*
9072 * Determine the target EL for physical exceptions
9073 */
012a906b
GB
9074uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
9075 uint32_t cur_el, bool secure)
0eeb17d6
GB
9076{
9077 CPUARMState *env = cs->env_ptr;
f7778444
RH
9078 bool rw;
9079 bool scr;
9080 bool hcr;
0eeb17d6 9081 int target_el;
2cde031f 9082 /* Is the highest EL AArch64? */
f7778444
RH
9083 bool is64 = arm_feature(env, ARM_FEATURE_AARCH64);
9084 uint64_t hcr_el2;
2cde031f
SS
9085
9086 if (arm_feature(env, ARM_FEATURE_EL3)) {
9087 rw = ((env->cp15.scr_el3 & SCR_RW) == SCR_RW);
9088 } else {
9089 /* Either EL2 is the highest EL (and so the EL2 register width
9090 * is given by is64); or there is no EL2 or EL3, in which case
9091 * the value of 'rw' does not affect the table lookup anyway.
9092 */
9093 rw = is64;
9094 }
0eeb17d6 9095
f7778444 9096 hcr_el2 = arm_hcr_el2_eff(env);
0eeb17d6
GB
9097 switch (excp_idx) {
9098 case EXCP_IRQ:
9099 scr = ((env->cp15.scr_el3 & SCR_IRQ) == SCR_IRQ);
f7778444 9100 hcr = hcr_el2 & HCR_IMO;
0eeb17d6
GB
9101 break;
9102 case EXCP_FIQ:
9103 scr = ((env->cp15.scr_el3 & SCR_FIQ) == SCR_FIQ);
f7778444 9104 hcr = hcr_el2 & HCR_FMO;
0eeb17d6
GB
9105 break;
9106 default:
9107 scr = ((env->cp15.scr_el3 & SCR_EA) == SCR_EA);
f7778444 9108 hcr = hcr_el2 & HCR_AMO;
0eeb17d6
GB
9109 break;
9110 };
9111
d1b31428
RH
9112 /*
9113 * For these purposes, TGE and AMO/IMO/FMO both force the
9114 * interrupt to EL2. Fold TGE into the bit extracted above.
9115 */
9116 hcr |= (hcr_el2 & HCR_TGE) != 0;
9117
0eeb17d6
GB
9118 /* Perform a table-lookup for the target EL given the current state */
9119 target_el = target_el_table[is64][scr][rw][hcr][secure][cur_el];
9120
9121 assert(target_el > 0);
9122
9123 return target_el;
9124}
9125
b59f479b
PMD
9126void arm_log_exception(int idx)
9127{
9128 if (qemu_loglevel_mask(CPU_LOG_INT)) {
9129 const char *exc = NULL;
9130 static const char * const excnames[] = {
9131 [EXCP_UDEF] = "Undefined Instruction",
9132 [EXCP_SWI] = "SVC",
9133 [EXCP_PREFETCH_ABORT] = "Prefetch Abort",
9134 [EXCP_DATA_ABORT] = "Data Abort",
9135 [EXCP_IRQ] = "IRQ",
9136 [EXCP_FIQ] = "FIQ",
9137 [EXCP_BKPT] = "Breakpoint",
9138 [EXCP_EXCEPTION_EXIT] = "QEMU v7M exception exit",
9139 [EXCP_KERNEL_TRAP] = "QEMU intercept of kernel commpage",
9140 [EXCP_HVC] = "Hypervisor Call",
9141 [EXCP_HYP_TRAP] = "Hypervisor Trap",
9142 [EXCP_SMC] = "Secure Monitor Call",
9143 [EXCP_VIRQ] = "Virtual IRQ",
9144 [EXCP_VFIQ] = "Virtual FIQ",
9145 [EXCP_SEMIHOST] = "Semihosting call",
9146 [EXCP_NOCP] = "v7M NOCP UsageFault",
9147 [EXCP_INVSTATE] = "v7M INVSTATE UsageFault",
9148 [EXCP_STKOF] = "v8M STKOF UsageFault",
9149 [EXCP_LAZYFP] = "v7M exception during lazy FP stacking",
9150 [EXCP_LSERR] = "v8M LSERR UsageFault",
9151 [EXCP_UNALIGNED] = "v7M UNALIGNED UsageFault",
9152 };
9153
9154 if (idx >= 0 && idx < ARRAY_SIZE(excnames)) {
9155 exc = excnames[idx];
9156 }
9157 if (!exc) {
9158 exc = "unknown";
9159 }
9160 qemu_log_mask(CPU_LOG_INT, "Taking exception %d [%s]\n", idx, exc);
9161 }
9162}
9163
a356dacf 9164/*
7aab5a8c
PMD
9165 * Function used to synchronize QEMU's AArch64 register set with AArch32
9166 * register set. This is necessary when switching between AArch32 and AArch64
9167 * execution state.
a356dacf 9168 */
7aab5a8c 9169void aarch64_sync_32_to_64(CPUARMState *env)
9ee6e8bb 9170{
7aab5a8c
PMD
9171 int i;
9172 uint32_t mode = env->uncached_cpsr & CPSR_M;
9173
9174 /* We can blanket copy R[0:7] to X[0:7] */
9175 for (i = 0; i < 8; i++) {
9176 env->xregs[i] = env->regs[i];
fd592d89 9177 }
70d74660 9178
9a223097 9179 /*
7aab5a8c
PMD
9180 * Unless we are in FIQ mode, x8-x12 come from the user registers r8-r12.
9181 * Otherwise, they come from the banked user regs.
fd592d89 9182 */
7aab5a8c
PMD
9183 if (mode == ARM_CPU_MODE_FIQ) {
9184 for (i = 8; i < 13; i++) {
9185 env->xregs[i] = env->usr_regs[i - 8];
9186 }
9187 } else {
9188 for (i = 8; i < 13; i++) {
9189 env->xregs[i] = env->regs[i];
9190 }
fd592d89 9191 }
9ee6e8bb 9192
7aab5a8c
PMD
9193 /*
9194 * Registers x13-x23 are the various mode SP and FP registers. Registers
9195 * r13 and r14 are only copied if we are in that mode, otherwise we copy
9196 * from the mode banked register.
9197 */
9198 if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
9199 env->xregs[13] = env->regs[13];
9200 env->xregs[14] = env->regs[14];
9201 } else {
9202 env->xregs[13] = env->banked_r13[bank_number(ARM_CPU_MODE_USR)];
9203 /* HYP is an exception in that it is copied from r14 */
9204 if (mode == ARM_CPU_MODE_HYP) {
9205 env->xregs[14] = env->regs[14];
95695eff 9206 } else {
7aab5a8c 9207 env->xregs[14] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)];
95695eff 9208 }
95695eff
PM
9209 }
9210
7aab5a8c
PMD
9211 if (mode == ARM_CPU_MODE_HYP) {
9212 env->xregs[15] = env->regs[13];
9213 } else {
9214 env->xregs[15] = env->banked_r13[bank_number(ARM_CPU_MODE_HYP)];
95695eff
PM
9215 }
9216
7aab5a8c
PMD
9217 if (mode == ARM_CPU_MODE_IRQ) {
9218 env->xregs[16] = env->regs[14];
9219 env->xregs[17] = env->regs[13];
9220 } else {
9221 env->xregs[16] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)];
9222 env->xregs[17] = env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)];
9223 }
95695eff 9224
7aab5a8c
PMD
9225 if (mode == ARM_CPU_MODE_SVC) {
9226 env->xregs[18] = env->regs[14];
9227 env->xregs[19] = env->regs[13];
9228 } else {
9229 env->xregs[18] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)];
9230 env->xregs[19] = env->banked_r13[bank_number(ARM_CPU_MODE_SVC)];
9231 }
95695eff 9232
7aab5a8c
PMD
9233 if (mode == ARM_CPU_MODE_ABT) {
9234 env->xregs[20] = env->regs[14];
9235 env->xregs[21] = env->regs[13];
9236 } else {
9237 env->xregs[20] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)];
9238 env->xregs[21] = env->banked_r13[bank_number(ARM_CPU_MODE_ABT)];
9239 }
e33cf0f8 9240
7aab5a8c
PMD
9241 if (mode == ARM_CPU_MODE_UND) {
9242 env->xregs[22] = env->regs[14];
9243 env->xregs[23] = env->regs[13];
9244 } else {
9245 env->xregs[22] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)];
9246 env->xregs[23] = env->banked_r13[bank_number(ARM_CPU_MODE_UND)];
e33cf0f8
PM
9247 }
9248
9249 /*
7aab5a8c
PMD
9250 * Registers x24-x30 are mapped to r8-r14 in FIQ mode. If we are in FIQ
9251 * mode, then we can copy from r8-r14. Otherwise, we copy from the
9252 * FIQ bank for r8-r14.
e33cf0f8 9253 */
7aab5a8c
PMD
9254 if (mode == ARM_CPU_MODE_FIQ) {
9255 for (i = 24; i < 31; i++) {
9256 env->xregs[i] = env->regs[i - 16]; /* X[24:30] <- R[8:14] */
9257 }
9258 } else {
9259 for (i = 24; i < 29; i++) {
9260 env->xregs[i] = env->fiq_regs[i - 24];
e33cf0f8 9261 }
7aab5a8c
PMD
9262 env->xregs[29] = env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)];
9263 env->xregs[30] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)];
e33cf0f8 9264 }
7aab5a8c
PMD
9265
9266 env->pc = env->regs[15];
e33cf0f8
PM
9267}
9268
9a223097 9269/*
7aab5a8c
PMD
9270 * Function used to synchronize QEMU's AArch32 register set with AArch64
9271 * register set. This is necessary when switching between AArch32 and AArch64
9272 * execution state.
de2db7ec 9273 */
7aab5a8c 9274void aarch64_sync_64_to_32(CPUARMState *env)
9ee6e8bb 9275{
7aab5a8c
PMD
9276 int i;
9277 uint32_t mode = env->uncached_cpsr & CPSR_M;
abc24d86 9278
7aab5a8c
PMD
9279 /* We can blanket copy X[0:7] to R[0:7] */
9280 for (i = 0; i < 8; i++) {
9281 env->regs[i] = env->xregs[i];
de2db7ec 9282 }
3f0cddee 9283
9a223097 9284 /*
7aab5a8c
PMD
9285 * Unless we are in FIQ mode, r8-r12 come from the user registers x8-x12.
9286 * Otherwise, we copy x8-x12 into the banked user regs.
de2db7ec 9287 */
7aab5a8c
PMD
9288 if (mode == ARM_CPU_MODE_FIQ) {
9289 for (i = 8; i < 13; i++) {
9290 env->usr_regs[i - 8] = env->xregs[i];
9291 }
9292 } else {
9293 for (i = 8; i < 13; i++) {
9294 env->regs[i] = env->xregs[i];
9295 }
fb602cb7
PM
9296 }
9297
9a223097 9298 /*
7aab5a8c
PMD
9299 * Registers r13 & r14 depend on the current mode.
9300 * If we are in a given mode, we copy the corresponding x registers to r13
9301 * and r14. Otherwise, we copy the x register to the banked r13 and r14
9302 * for the mode.
fb602cb7 9303 */
7aab5a8c
PMD
9304 if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
9305 env->regs[13] = env->xregs[13];
9306 env->regs[14] = env->xregs[14];
fb602cb7 9307 } else {
7aab5a8c 9308 env->banked_r13[bank_number(ARM_CPU_MODE_USR)] = env->xregs[13];
fb602cb7 9309
7aab5a8c
PMD
9310 /*
9311 * HYP is an exception in that it does not have its own banked r14 but
9312 * shares the USR r14
9313 */
9314 if (mode == ARM_CPU_MODE_HYP) {
9315 env->regs[14] = env->xregs[14];
9316 } else {
9317 env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)] = env->xregs[14];
9318 }
9319 }
fb602cb7 9320
7aab5a8c
PMD
9321 if (mode == ARM_CPU_MODE_HYP) {
9322 env->regs[13] = env->xregs[15];
fb602cb7 9323 } else {
7aab5a8c 9324 env->banked_r13[bank_number(ARM_CPU_MODE_HYP)] = env->xregs[15];
fb602cb7 9325 }
d02a8698 9326
7aab5a8c
PMD
9327 if (mode == ARM_CPU_MODE_IRQ) {
9328 env->regs[14] = env->xregs[16];
9329 env->regs[13] = env->xregs[17];
d02a8698 9330 } else {
7aab5a8c
PMD
9331 env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[16];
9332 env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[17];
d02a8698
PM
9333 }
9334
7aab5a8c
PMD
9335 if (mode == ARM_CPU_MODE_SVC) {
9336 env->regs[14] = env->xregs[18];
9337 env->regs[13] = env->xregs[19];
9338 } else {
9339 env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)] = env->xregs[18];
9340 env->banked_r13[bank_number(ARM_CPU_MODE_SVC)] = env->xregs[19];
fb602cb7
PM
9341 }
9342
7aab5a8c
PMD
9343 if (mode == ARM_CPU_MODE_ABT) {
9344 env->regs[14] = env->xregs[20];
9345 env->regs[13] = env->xregs[21];
9346 } else {
9347 env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)] = env->xregs[20];
9348 env->banked_r13[bank_number(ARM_CPU_MODE_ABT)] = env->xregs[21];
ce02049d
GB
9349 }
9350
9351 if (mode == ARM_CPU_MODE_UND) {
3a9148d0
SS
9352 env->regs[14] = env->xregs[22];
9353 env->regs[13] = env->xregs[23];
ce02049d 9354 } else {
593cfa2b 9355 env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)] = env->xregs[22];
3a9148d0 9356 env->banked_r13[bank_number(ARM_CPU_MODE_UND)] = env->xregs[23];
ce02049d
GB
9357 }
9358
9359 /* Registers x24-x30 are mapped to r8-r14 in FIQ mode. If we are in FIQ
9360 * mode, then we can copy to r8-r14. Otherwise, we copy to the
9361 * FIQ bank for r8-r14.
9362 */
9363 if (mode == ARM_CPU_MODE_FIQ) {
9364 for (i = 24; i < 31; i++) {
9365 env->regs[i - 16] = env->xregs[i]; /* X[24:30] -> R[8:14] */
9366 }
9367 } else {
9368 for (i = 24; i < 29; i++) {
9369 env->fiq_regs[i - 24] = env->xregs[i];
9370 }
9371 env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[29];
593cfa2b 9372 env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[30];
ce02049d
GB
9373 }
9374
9375 env->regs[15] = env->pc;
9376}
9377
dea8378b
PM
9378static void take_aarch32_exception(CPUARMState *env, int new_mode,
9379 uint32_t mask, uint32_t offset,
9380 uint32_t newpc)
9381{
4a2696c0
RH
9382 int new_el;
9383
dea8378b
PM
9384 /* Change the CPU state so as to actually take the exception. */
9385 switch_mode(env, new_mode);
4a2696c0 9386
dea8378b
PM
9387 /*
9388 * For exceptions taken to AArch32 we must clear the SS bit in both
9389 * PSTATE and in the old-state value we save to SPSR_<mode>, so zero it now.
9390 */
9391 env->uncached_cpsr &= ~PSTATE_SS;
9392 env->spsr = cpsr_read(env);
9393 /* Clear IT bits. */
9394 env->condexec_bits = 0;
9395 /* Switch to the new mode, and to the correct instruction set. */
9396 env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode;
88828bf1
CD
9397
9398 /* This must be after mode switching. */
9399 new_el = arm_current_el(env);
9400
dea8378b
PM
9401 /* Set new mode endianness */
9402 env->uncached_cpsr &= ~CPSR_E;
4a2696c0 9403 if (env->cp15.sctlr_el[new_el] & SCTLR_EE) {
dea8378b
PM
9404 env->uncached_cpsr |= CPSR_E;
9405 }
829f9fd3
PM
9406 /* J and IL must always be cleared for exception entry */
9407 env->uncached_cpsr &= ~(CPSR_IL | CPSR_J);
dea8378b
PM
9408 env->daif |= mask;
9409
9410 if (new_mode == ARM_CPU_MODE_HYP) {
9411 env->thumb = (env->cp15.sctlr_el[2] & SCTLR_TE) != 0;
9412 env->elr_el[2] = env->regs[15];
9413 } else {
4a2696c0 9414 /* CPSR.PAN is normally preserved preserved unless... */
f8af1143 9415 if (cpu_isar_feature(aa32_pan, env_archcpu(env))) {
4a2696c0
RH
9416 switch (new_el) {
9417 case 3:
9418 if (!arm_is_secure_below_el3(env)) {
9419 /* ... the target is EL3, from non-secure state. */
9420 env->uncached_cpsr &= ~CPSR_PAN;
9421 break;
9422 }
9423 /* ... the target is EL3, from secure state ... */
9424 /* fall through */
9425 case 1:
9426 /* ... the target is EL1 and SCTLR.SPAN is 0. */
9427 if (!(env->cp15.sctlr_el[new_el] & SCTLR_SPAN)) {
9428 env->uncached_cpsr |= CPSR_PAN;
9429 }
9430 break;
9431 }
9432 }
dea8378b
PM
9433 /*
9434 * this is a lie, as there was no c1_sys on V4T/V5, but who cares
9435 * and we should just guard the thumb mode on V4
9436 */
9437 if (arm_feature(env, ARM_FEATURE_V4T)) {
9438 env->thumb =
9439 (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_TE) != 0;
9440 }
9441 env->regs[14] = env->regs[15] + offset;
9442 }
9443 env->regs[15] = newpc;
a8a79c7a 9444 arm_rebuild_hflags(env);
dea8378b
PM
9445}
9446
b9bc21ff
PM
9447static void arm_cpu_do_interrupt_aarch32_hyp(CPUState *cs)
9448{
9449 /*
9450 * Handle exception entry to Hyp mode; this is sufficiently
9451 * different to entry to other AArch32 modes that we handle it
9452 * separately here.
9453 *
9454 * The vector table entry used is always the 0x14 Hyp mode entry point,
9455 * unless this is an UNDEF/HVC/abort taken from Hyp to Hyp.
9456 * The offset applied to the preferred return address is always zero
9457 * (see DDI0487C.a section G1.12.3).
9458 * PSTATE A/I/F masks are set based only on the SCR.EA/IRQ/FIQ values.
9459 */
9460 uint32_t addr, mask;
9461 ARMCPU *cpu = ARM_CPU(cs);
9462 CPUARMState *env = &cpu->env;
9463
9464 switch (cs->exception_index) {
9465 case EXCP_UDEF:
9466 addr = 0x04;
9467 break;
9468 case EXCP_SWI:
9469 addr = 0x14;
9470 break;
9471 case EXCP_BKPT:
9472 /* Fall through to prefetch abort. */
9473 case EXCP_PREFETCH_ABORT:
9474 env->cp15.ifar_s = env->exception.vaddress;
9475 qemu_log_mask(CPU_LOG_INT, "...with HIFAR 0x%x\n",
9476 (uint32_t)env->exception.vaddress);
9477 addr = 0x0c;
9478 break;
9479 case EXCP_DATA_ABORT:
9480 env->cp15.dfar_s = env->exception.vaddress;
9481 qemu_log_mask(CPU_LOG_INT, "...with HDFAR 0x%x\n",
9482 (uint32_t)env->exception.vaddress);
9483 addr = 0x10;
9484 break;
9485 case EXCP_IRQ:
9486 addr = 0x18;
9487 break;
9488 case EXCP_FIQ:
9489 addr = 0x1c;
9490 break;
9491 case EXCP_HVC:
9492 addr = 0x08;
9493 break;
9494 case EXCP_HYP_TRAP:
9495 addr = 0x14;
9bbb4ef9 9496 break;
b9bc21ff
PM
9497 default:
9498 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
9499 }
9500
9501 if (cs->exception_index != EXCP_IRQ && cs->exception_index != EXCP_FIQ) {
2ed08180
PM
9502 if (!arm_feature(env, ARM_FEATURE_V8)) {
9503 /*
9504 * QEMU syndrome values are v8-style. v7 has the IL bit
9505 * UNK/SBZP for "field not valid" cases, where v8 uses RES1.
9506 * If this is a v7 CPU, squash the IL bit in those cases.
9507 */
9508 if (cs->exception_index == EXCP_PREFETCH_ABORT ||
9509 (cs->exception_index == EXCP_DATA_ABORT &&
9510 !(env->exception.syndrome & ARM_EL_ISV)) ||
9511 syn_get_ec(env->exception.syndrome) == EC_UNCATEGORIZED) {
9512 env->exception.syndrome &= ~ARM_EL_IL;
9513 }
9514 }
b9bc21ff
PM
9515 env->cp15.esr_el[2] = env->exception.syndrome;
9516 }
9517
9518 if (arm_current_el(env) != 2 && addr < 0x14) {
9519 addr = 0x14;
9520 }
9521
9522 mask = 0;
9523 if (!(env->cp15.scr_el3 & SCR_EA)) {
9524 mask |= CPSR_A;
9525 }
9526 if (!(env->cp15.scr_el3 & SCR_IRQ)) {
9527 mask |= CPSR_I;
9528 }
9529 if (!(env->cp15.scr_el3 & SCR_FIQ)) {
9530 mask |= CPSR_F;
9531 }
9532
9533 addr += env->cp15.hvbar;
9534
9535 take_aarch32_exception(env, ARM_CPU_MODE_HYP, mask, 0, addr);
9536}
9537
966f758c 9538static void arm_cpu_do_interrupt_aarch32(CPUState *cs)
b5ff1b31 9539{
97a8ea5a
AF
9540 ARMCPU *cpu = ARM_CPU(cs);
9541 CPUARMState *env = &cpu->env;
b5ff1b31
FB
9542 uint32_t addr;
9543 uint32_t mask;
9544 int new_mode;
9545 uint32_t offset;
16a906fd 9546 uint32_t moe;
b5ff1b31 9547
16a906fd 9548 /* If this is a debug exception we must update the DBGDSCR.MOE bits */
64b91e3f 9549 switch (syn_get_ec(env->exception.syndrome)) {
16a906fd
PM
9550 case EC_BREAKPOINT:
9551 case EC_BREAKPOINT_SAME_EL:
9552 moe = 1;
9553 break;
9554 case EC_WATCHPOINT:
9555 case EC_WATCHPOINT_SAME_EL:
9556 moe = 10;
9557 break;
9558 case EC_AA32_BKPT:
9559 moe = 3;
9560 break;
9561 case EC_VECTORCATCH:
9562 moe = 5;
9563 break;
9564 default:
9565 moe = 0;
9566 break;
9567 }
9568
9569 if (moe) {
9570 env->cp15.mdscr_el1 = deposit64(env->cp15.mdscr_el1, 2, 4, moe);
9571 }
9572
b9bc21ff
PM
9573 if (env->exception.target_el == 2) {
9574 arm_cpu_do_interrupt_aarch32_hyp(cs);
9575 return;
9576 }
9577
27103424 9578 switch (cs->exception_index) {
b5ff1b31
FB
9579 case EXCP_UDEF:
9580 new_mode = ARM_CPU_MODE_UND;
9581 addr = 0x04;
9582 mask = CPSR_I;
9583 if (env->thumb)
9584 offset = 2;
9585 else
9586 offset = 4;
9587 break;
9588 case EXCP_SWI:
9589 new_mode = ARM_CPU_MODE_SVC;
9590 addr = 0x08;
9591 mask = CPSR_I;
601d70b9 9592 /* The PC already points to the next instruction. */
b5ff1b31
FB
9593 offset = 0;
9594 break;
06c949e6 9595 case EXCP_BKPT:
9ee6e8bb
PB
9596 /* Fall through to prefetch abort. */
9597 case EXCP_PREFETCH_ABORT:
88ca1c2d 9598 A32_BANKED_CURRENT_REG_SET(env, ifsr, env->exception.fsr);
b848ce2b 9599 A32_BANKED_CURRENT_REG_SET(env, ifar, env->exception.vaddress);
3f1beaca 9600 qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x IFAR 0x%x\n",
88ca1c2d 9601 env->exception.fsr, (uint32_t)env->exception.vaddress);
b5ff1b31
FB
9602 new_mode = ARM_CPU_MODE_ABT;
9603 addr = 0x0c;
9604 mask = CPSR_A | CPSR_I;
9605 offset = 4;
9606 break;
9607 case EXCP_DATA_ABORT:
4a7e2d73 9608 A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr);
b848ce2b 9609 A32_BANKED_CURRENT_REG_SET(env, dfar, env->exception.vaddress);
3f1beaca 9610 qemu_log_mask(CPU_LOG_INT, "...with DFSR 0x%x DFAR 0x%x\n",
4a7e2d73 9611 env->exception.fsr,
6cd8a264 9612 (uint32_t)env->exception.vaddress);
b5ff1b31
FB
9613 new_mode = ARM_CPU_MODE_ABT;
9614 addr = 0x10;
9615 mask = CPSR_A | CPSR_I;
9616 offset = 8;
9617 break;
9618 case EXCP_IRQ:
9619 new_mode = ARM_CPU_MODE_IRQ;
9620 addr = 0x18;
9621 /* Disable IRQ and imprecise data aborts. */
9622 mask = CPSR_A | CPSR_I;
9623 offset = 4;
de38d23b
FA
9624 if (env->cp15.scr_el3 & SCR_IRQ) {
9625 /* IRQ routed to monitor mode */
9626 new_mode = ARM_CPU_MODE_MON;
9627 mask |= CPSR_F;
9628 }
b5ff1b31
FB
9629 break;
9630 case EXCP_FIQ:
9631 new_mode = ARM_CPU_MODE_FIQ;
9632 addr = 0x1c;
9633 /* Disable FIQ, IRQ and imprecise data aborts. */
9634 mask = CPSR_A | CPSR_I | CPSR_F;
de38d23b
FA
9635 if (env->cp15.scr_el3 & SCR_FIQ) {
9636 /* FIQ routed to monitor mode */
9637 new_mode = ARM_CPU_MODE_MON;
9638 }
b5ff1b31
FB
9639 offset = 4;
9640 break;
87a4b270
PM
9641 case EXCP_VIRQ:
9642 new_mode = ARM_CPU_MODE_IRQ;
9643 addr = 0x18;
9644 /* Disable IRQ and imprecise data aborts. */
9645 mask = CPSR_A | CPSR_I;
9646 offset = 4;
9647 break;
9648 case EXCP_VFIQ:
9649 new_mode = ARM_CPU_MODE_FIQ;
9650 addr = 0x1c;
9651 /* Disable FIQ, IRQ and imprecise data aborts. */
9652 mask = CPSR_A | CPSR_I | CPSR_F;
9653 offset = 4;
9654 break;
dbe9d163
FA
9655 case EXCP_SMC:
9656 new_mode = ARM_CPU_MODE_MON;
9657 addr = 0x08;
9658 mask = CPSR_A | CPSR_I | CPSR_F;
9659 offset = 0;
9660 break;
b5ff1b31 9661 default:
a47dddd7 9662 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
b5ff1b31
FB
9663 return; /* Never happens. Keep compiler happy. */
9664 }
e89e51a1
FA
9665
9666 if (new_mode == ARM_CPU_MODE_MON) {
9667 addr += env->cp15.mvbar;
137feaa9 9668 } else if (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_V) {
e89e51a1 9669 /* High vectors. When enabled, base address cannot be remapped. */
b5ff1b31 9670 addr += 0xffff0000;
8641136c
NR
9671 } else {
9672 /* ARM v7 architectures provide a vector base address register to remap
9673 * the interrupt vector table.
e89e51a1 9674 * This register is only followed in non-monitor mode, and is banked.
8641136c
NR
9675 * Note: only bits 31:5 are valid.
9676 */
fb6c91ba 9677 addr += A32_BANKED_CURRENT_REG_GET(env, vbar);
b5ff1b31 9678 }
dbe9d163
FA
9679
9680 if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON) {
9681 env->cp15.scr_el3 &= ~SCR_NS;
9682 }
9683
dea8378b 9684 take_aarch32_exception(env, new_mode, mask, offset, addr);
b5ff1b31
FB
9685}
9686
a65dabf7
PM
9687static int aarch64_regnum(CPUARMState *env, int aarch32_reg)
9688{
9689 /*
9690 * Return the register number of the AArch64 view of the AArch32
9691 * register @aarch32_reg. The CPUARMState CPSR is assumed to still
9692 * be that of the AArch32 mode the exception came from.
9693 */
9694 int mode = env->uncached_cpsr & CPSR_M;
9695
9696 switch (aarch32_reg) {
9697 case 0 ... 7:
9698 return aarch32_reg;
9699 case 8 ... 12:
9700 return mode == ARM_CPU_MODE_FIQ ? aarch32_reg + 16 : aarch32_reg;
9701 case 13:
9702 switch (mode) {
9703 case ARM_CPU_MODE_USR:
9704 case ARM_CPU_MODE_SYS:
9705 return 13;
9706 case ARM_CPU_MODE_HYP:
9707 return 15;
9708 case ARM_CPU_MODE_IRQ:
9709 return 17;
9710 case ARM_CPU_MODE_SVC:
9711 return 19;
9712 case ARM_CPU_MODE_ABT:
9713 return 21;
9714 case ARM_CPU_MODE_UND:
9715 return 23;
9716 case ARM_CPU_MODE_FIQ:
9717 return 29;
9718 default:
9719 g_assert_not_reached();
9720 }
9721 case 14:
9722 switch (mode) {
9723 case ARM_CPU_MODE_USR:
9724 case ARM_CPU_MODE_SYS:
9725 case ARM_CPU_MODE_HYP:
9726 return 14;
9727 case ARM_CPU_MODE_IRQ:
9728 return 16;
9729 case ARM_CPU_MODE_SVC:
9730 return 18;
9731 case ARM_CPU_MODE_ABT:
9732 return 20;
9733 case ARM_CPU_MODE_UND:
9734 return 22;
9735 case ARM_CPU_MODE_FIQ:
9736 return 30;
9737 default:
9738 g_assert_not_reached();
9739 }
9740 case 15:
9741 return 31;
9742 default:
9743 g_assert_not_reached();
9744 }
9745}
9746
966f758c
PM
9747/* Handle exception entry to a target EL which is using AArch64 */
9748static void arm_cpu_do_interrupt_aarch64(CPUState *cs)
f3a9b694
PM
9749{
9750 ARMCPU *cpu = ARM_CPU(cs);
9751 CPUARMState *env = &cpu->env;
9752 unsigned int new_el = env->exception.target_el;
9753 target_ulong addr = env->cp15.vbar_el[new_el];
9754 unsigned int new_mode = aarch64_pstate_mode(new_el, true);
4a2696c0 9755 unsigned int old_mode;
0ab5953b 9756 unsigned int cur_el = arm_current_el(env);
a65dabf7 9757 int rt;
0ab5953b 9758
9a05f7b6
RH
9759 /*
9760 * Note that new_el can never be 0. If cur_el is 0, then
9761 * el0_a64 is is_a64(), else el0_a64 is ignored.
9762 */
9763 aarch64_sve_change_el(env, cur_el, new_el, is_a64(env));
f3a9b694 9764
0ab5953b 9765 if (cur_el < new_el) {
3d6f7617
PM
9766 /* Entry vector offset depends on whether the implemented EL
9767 * immediately lower than the target level is using AArch32 or AArch64
9768 */
9769 bool is_aa64;
cb092fbb 9770 uint64_t hcr;
3d6f7617
PM
9771
9772 switch (new_el) {
9773 case 3:
9774 is_aa64 = (env->cp15.scr_el3 & SCR_RW) != 0;
9775 break;
9776 case 2:
cb092fbb
RH
9777 hcr = arm_hcr_el2_eff(env);
9778 if ((hcr & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
9779 is_aa64 = (hcr & HCR_RW) != 0;
9780 break;
9781 }
9782 /* fall through */
3d6f7617
PM
9783 case 1:
9784 is_aa64 = is_a64(env);
9785 break;
9786 default:
9787 g_assert_not_reached();
9788 }
9789
9790 if (is_aa64) {
f3a9b694
PM
9791 addr += 0x400;
9792 } else {
9793 addr += 0x600;
9794 }
9795 } else if (pstate_read(env) & PSTATE_SP) {
9796 addr += 0x200;
9797 }
9798
f3a9b694
PM
9799 switch (cs->exception_index) {
9800 case EXCP_PREFETCH_ABORT:
9801 case EXCP_DATA_ABORT:
9802 env->cp15.far_el[new_el] = env->exception.vaddress;
9803 qemu_log_mask(CPU_LOG_INT, "...with FAR 0x%" PRIx64 "\n",
9804 env->cp15.far_el[new_el]);
9805 /* fall through */
9806 case EXCP_BKPT:
9807 case EXCP_UDEF:
9808 case EXCP_SWI:
9809 case EXCP_HVC:
9810 case EXCP_HYP_TRAP:
9811 case EXCP_SMC:
a65dabf7
PM
9812 switch (syn_get_ec(env->exception.syndrome)) {
9813 case EC_ADVSIMDFPACCESSTRAP:
4be42f40
PM
9814 /*
9815 * QEMU internal FP/SIMD syndromes from AArch32 include the
9816 * TA and coproc fields which are only exposed if the exception
9817 * is taken to AArch32 Hyp mode. Mask them out to get a valid
9818 * AArch64 format syndrome.
9819 */
9820 env->exception.syndrome &= ~MAKE_64BIT_MASK(0, 20);
a65dabf7
PM
9821 break;
9822 case EC_CP14RTTRAP:
9823 case EC_CP15RTTRAP:
9824 case EC_CP14DTTRAP:
9825 /*
9826 * For a trap on AArch32 MRC/MCR/LDC/STC the Rt field is currently
9827 * the raw register field from the insn; when taking this to
9828 * AArch64 we must convert it to the AArch64 view of the register
9829 * number. Notice that we read a 4-bit AArch32 register number and
9830 * write back a 5-bit AArch64 one.
9831 */
9832 rt = extract32(env->exception.syndrome, 5, 4);
9833 rt = aarch64_regnum(env, rt);
9834 env->exception.syndrome = deposit32(env->exception.syndrome,
9835 5, 5, rt);
9836 break;
9837 case EC_CP15RRTTRAP:
9838 case EC_CP14RRTTRAP:
9839 /* Similarly for MRRC/MCRR traps for Rt and Rt2 fields */
9840 rt = extract32(env->exception.syndrome, 5, 4);
9841 rt = aarch64_regnum(env, rt);
9842 env->exception.syndrome = deposit32(env->exception.syndrome,
9843 5, 5, rt);
9844 rt = extract32(env->exception.syndrome, 10, 4);
9845 rt = aarch64_regnum(env, rt);
9846 env->exception.syndrome = deposit32(env->exception.syndrome,
9847 10, 5, rt);
9848 break;
4be42f40 9849 }
f3a9b694
PM
9850 env->cp15.esr_el[new_el] = env->exception.syndrome;
9851 break;
9852 case EXCP_IRQ:
9853 case EXCP_VIRQ:
9854 addr += 0x80;
9855 break;
9856 case EXCP_FIQ:
9857 case EXCP_VFIQ:
9858 addr += 0x100;
9859 break;
f3a9b694
PM
9860 default:
9861 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
9862 }
9863
9864 if (is_a64(env)) {
4a2696c0 9865 old_mode = pstate_read(env);
f3a9b694
PM
9866 aarch64_save_sp(env, arm_current_el(env));
9867 env->elr_el[new_el] = env->pc;
9868 } else {
4a2696c0 9869 old_mode = cpsr_read(env);
f3a9b694
PM
9870 env->elr_el[new_el] = env->regs[15];
9871
9872 aarch64_sync_32_to_64(env);
9873
9874 env->condexec_bits = 0;
9875 }
4a2696c0
RH
9876 env->banked_spsr[aarch64_banked_spsr_index(new_el)] = old_mode;
9877
f3a9b694
PM
9878 qemu_log_mask(CPU_LOG_INT, "...with ELR 0x%" PRIx64 "\n",
9879 env->elr_el[new_el]);
9880
4a2696c0
RH
9881 if (cpu_isar_feature(aa64_pan, cpu)) {
9882 /* The value of PSTATE.PAN is normally preserved, except when ... */
9883 new_mode |= old_mode & PSTATE_PAN;
9884 switch (new_el) {
9885 case 2:
9886 /* ... the target is EL2 with HCR_EL2.{E2H,TGE} == '11' ... */
9887 if ((arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE))
9888 != (HCR_E2H | HCR_TGE)) {
9889 break;
9890 }
9891 /* fall through */
9892 case 1:
9893 /* ... the target is EL1 ... */
9894 /* ... and SCTLR_ELx.SPAN == 0, then set to 1. */
9895 if ((env->cp15.sctlr_el[new_el] & SCTLR_SPAN) == 0) {
9896 new_mode |= PSTATE_PAN;
9897 }
9898 break;
9899 }
9900 }
34669338
RH
9901 if (cpu_isar_feature(aa64_mte, cpu)) {
9902 new_mode |= PSTATE_TCO;
9903 }
4a2696c0 9904
f3a9b694
PM
9905 pstate_write(env, PSTATE_DAIF | new_mode);
9906 env->aarch64 = 1;
9907 aarch64_restore_sp(env, new_el);
a8a79c7a 9908 helper_rebuild_hflags_a64(env, new_el);
f3a9b694
PM
9909
9910 env->pc = addr;
9911
9912 qemu_log_mask(CPU_LOG_INT, "...to EL%d PC 0x%" PRIx64 " PSTATE 0x%x\n",
9913 new_el, env->pc, pstate_read(env));
966f758c
PM
9914}
9915
ed6e6ba9
AB
9916/*
9917 * Do semihosting call and set the appropriate return value. All the
9918 * permission and validity checks have been done at translate time.
9919 *
9920 * We only see semihosting exceptions in TCG only as they are not
9921 * trapped to the hypervisor in KVM.
9922 */
91f78c58 9923#ifdef CONFIG_TCG
ed6e6ba9
AB
9924static void handle_semihosting(CPUState *cs)
9925{
904c04de
PM
9926 ARMCPU *cpu = ARM_CPU(cs);
9927 CPUARMState *env = &cpu->env;
9928
9929 if (is_a64(env)) {
ed6e6ba9
AB
9930 qemu_log_mask(CPU_LOG_INT,
9931 "...handling as semihosting call 0x%" PRIx64 "\n",
9932 env->xregs[0]);
0bb446d8 9933 env->xregs[0] = do_common_semihosting(cs);
4ff5ef9e 9934 env->pc += 4;
904c04de 9935 } else {
904c04de
PM
9936 qemu_log_mask(CPU_LOG_INT,
9937 "...handling as semihosting call 0x%x\n",
9938 env->regs[0]);
0bb446d8 9939 env->regs[0] = do_common_semihosting(cs);
4ff5ef9e 9940 env->regs[15] += env->thumb ? 2 : 4;
904c04de
PM
9941 }
9942}
ed6e6ba9 9943#endif
904c04de 9944
966f758c
PM
9945/* Handle a CPU exception for A and R profile CPUs.
9946 * Do any appropriate logging, handle PSCI calls, and then hand off
9947 * to the AArch64-entry or AArch32-entry function depending on the
9948 * target exception level's register width.
9949 */
9950void arm_cpu_do_interrupt(CPUState *cs)
9951{
9952 ARMCPU *cpu = ARM_CPU(cs);
9953 CPUARMState *env = &cpu->env;
9954 unsigned int new_el = env->exception.target_el;
9955
531c60a9 9956 assert(!arm_feature(env, ARM_FEATURE_M));
966f758c
PM
9957
9958 arm_log_exception(cs->exception_index);
9959 qemu_log_mask(CPU_LOG_INT, "...from EL%d to EL%d\n", arm_current_el(env),
9960 new_el);
9961 if (qemu_loglevel_mask(CPU_LOG_INT)
9962 && !excp_is_internal(cs->exception_index)) {
6568da45 9963 qemu_log_mask(CPU_LOG_INT, "...with ESR 0x%x/0x%" PRIx32 "\n",
64b91e3f 9964 syn_get_ec(env->exception.syndrome),
966f758c
PM
9965 env->exception.syndrome);
9966 }
9967
9968 if (arm_is_psci_call(cpu, cs->exception_index)) {
9969 arm_handle_psci_call(cpu);
9970 qemu_log_mask(CPU_LOG_INT, "...handled as PSCI call\n");
9971 return;
9972 }
9973
ed6e6ba9
AB
9974 /*
9975 * Semihosting semantics depend on the register width of the code
9976 * that caused the exception, not the target exception level, so
9977 * must be handled here.
966f758c 9978 */
ed6e6ba9
AB
9979#ifdef CONFIG_TCG
9980 if (cs->exception_index == EXCP_SEMIHOST) {
9981 handle_semihosting(cs);
904c04de
PM
9982 return;
9983 }
ed6e6ba9 9984#endif
904c04de 9985
b5c53d1b
AL
9986 /* Hooks may change global state so BQL should be held, also the
9987 * BQL needs to be held for any modification of
9988 * cs->interrupt_request.
9989 */
9990 g_assert(qemu_mutex_iothread_locked());
9991
9992 arm_call_pre_el_change_hook(cpu);
9993
904c04de
PM
9994 assert(!excp_is_internal(cs->exception_index));
9995 if (arm_el_is_aa64(env, new_el)) {
966f758c
PM
9996 arm_cpu_do_interrupt_aarch64(cs);
9997 } else {
9998 arm_cpu_do_interrupt_aarch32(cs);
9999 }
f3a9b694 10000
bd7d00fc
PM
10001 arm_call_el_change_hook(cpu);
10002
f3a9b694
PM
10003 if (!kvm_enabled()) {
10004 cs->interrupt_request |= CPU_INTERRUPT_EXITTB;
10005 }
10006}
c47eaf9f 10007#endif /* !CONFIG_USER_ONLY */
0480f69a 10008
aaec1432
RH
10009uint64_t arm_sctlr(CPUARMState *env, int el)
10010{
10011 /* Only EL0 needs to be adjusted for EL1&0 or EL2&0. */
10012 if (el == 0) {
10013 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, 0);
b6ad6062
RDC
10014 el = (mmu_idx == ARMMMUIdx_E20_0 || mmu_idx == ARMMMUIdx_SE20_0)
10015 ? 2 : 1;
aaec1432
RH
10016 }
10017 return env->cp15.sctlr_el[el];
10018}
c47eaf9f 10019
0480f69a 10020/* Return the SCTLR value which controls this address translation regime */
aaec1432 10021static inline uint64_t regime_sctlr(CPUARMState *env, ARMMMUIdx mmu_idx)
0480f69a
PM
10022{
10023 return env->cp15.sctlr_el[regime_el(env, mmu_idx)];
10024}
10025
aaec1432
RH
10026#ifndef CONFIG_USER_ONLY
10027
0480f69a
PM
10028/* Return true if the specified stage of address translation is disabled */
10029static inline bool regime_translation_disabled(CPUARMState *env,
10030 ARMMMUIdx mmu_idx)
10031{
e04a5752
RDC
10032 uint64_t hcr_el2;
10033
29c483a5 10034 if (arm_feature(env, ARM_FEATURE_M)) {
ecf5e8ea 10035 switch (env->v7m.mpu_ctrl[regime_is_secure(env, mmu_idx)] &
3bef7012
PM
10036 (R_V7M_MPU_CTRL_ENABLE_MASK | R_V7M_MPU_CTRL_HFNMIENA_MASK)) {
10037 case R_V7M_MPU_CTRL_ENABLE_MASK:
10038 /* Enabled, but not for HardFault and NMI */
62593718 10039 return mmu_idx & ARM_MMU_IDX_M_NEGPRI;
3bef7012
PM
10040 case R_V7M_MPU_CTRL_ENABLE_MASK | R_V7M_MPU_CTRL_HFNMIENA_MASK:
10041 /* Enabled for all cases */
10042 return false;
10043 case 0:
10044 default:
10045 /* HFNMIENA set and ENABLE clear is UNPREDICTABLE, but
10046 * we warned about that in armv7m_nvic.c when the guest set it.
10047 */
10048 return true;
10049 }
29c483a5
MD
10050 }
10051
e04a5752
RDC
10052 hcr_el2 = arm_hcr_el2_eff(env);
10053
97fa9350 10054 if (mmu_idx == ARMMMUIdx_Stage2) {
9d1bab33 10055 /* HCR.DC means HCR.VM behaves as 1 */
e04a5752 10056 return (hcr_el2 & (HCR_DC | HCR_VM)) == 0;
0480f69a 10057 }
3d0e3080 10058
e04a5752 10059 if (hcr_el2 & HCR_TGE) {
3d0e3080
PM
10060 /* TGE means that NS EL0/1 act as if SCTLR_EL1.M is zero */
10061 if (!regime_is_secure(env, mmu_idx) && regime_el(env, mmu_idx) == 1) {
10062 return true;
10063 }
10064 }
10065
e04a5752 10066 if ((hcr_el2 & HCR_DC) && arm_mmu_idx_is_stage1_of_2(mmu_idx)) {
9d1bab33
PM
10067 /* HCR.DC means SCTLR_EL1.M behaves as 0 */
10068 return true;
10069 }
10070
0480f69a
PM
10071 return (regime_sctlr(env, mmu_idx) & SCTLR_M) == 0;
10072}
10073
73462ddd
PC
10074static inline bool regime_translation_big_endian(CPUARMState *env,
10075 ARMMMUIdx mmu_idx)
10076{
10077 return (regime_sctlr(env, mmu_idx) & SCTLR_EE) != 0;
10078}
10079
c47eaf9f
PM
10080/* Return the TTBR associated with this translation regime */
10081static inline uint64_t regime_ttbr(CPUARMState *env, ARMMMUIdx mmu_idx,
10082 int ttbrn)
10083{
97fa9350 10084 if (mmu_idx == ARMMMUIdx_Stage2) {
c47eaf9f
PM
10085 return env->cp15.vttbr_el2;
10086 }
10087 if (ttbrn == 0) {
10088 return env->cp15.ttbr0_el[regime_el(env, mmu_idx)];
10089 } else {
10090 return env->cp15.ttbr1_el[regime_el(env, mmu_idx)];
10091 }
10092}
10093
10094#endif /* !CONFIG_USER_ONLY */
10095
8bd5c820
PM
10096/* Convert a possible stage1+2 MMU index into the appropriate
10097 * stage 1 MMU index
10098 */
10099static inline ARMMMUIdx stage_1_mmu_idx(ARMMMUIdx mmu_idx)
10100{
b9f6033c
RH
10101 switch (mmu_idx) {
10102 case ARMMMUIdx_E10_0:
10103 return ARMMMUIdx_Stage1_E0;
10104 case ARMMMUIdx_E10_1:
10105 return ARMMMUIdx_Stage1_E1;
452ef8cb
RH
10106 case ARMMMUIdx_E10_1_PAN:
10107 return ARMMMUIdx_Stage1_E1_PAN;
b9f6033c
RH
10108 default:
10109 return mmu_idx;
8bd5c820 10110 }
8bd5c820
PM
10111}
10112
0480f69a
PM
10113/* Return true if the translation regime is using LPAE format page tables */
10114static inline bool regime_using_lpae_format(CPUARMState *env,
10115 ARMMMUIdx mmu_idx)
10116{
10117 int el = regime_el(env, mmu_idx);
10118 if (el == 2 || arm_el_is_aa64(env, el)) {
10119 return true;
10120 }
10121 if (arm_feature(env, ARM_FEATURE_LPAE)
10122 && (regime_tcr(env, mmu_idx)->raw_tcr & TTBCR_EAE)) {
10123 return true;
10124 }
10125 return false;
10126}
10127
deb2db99
AR
10128/* Returns true if the stage 1 translation regime is using LPAE format page
10129 * tables. Used when raising alignment exceptions, whose FSR changes depending
10130 * on whether the long or short descriptor format is in use. */
10131bool arm_s1_regime_using_lpae_format(CPUARMState *env, ARMMMUIdx mmu_idx)
30901475 10132{
8bd5c820 10133 mmu_idx = stage_1_mmu_idx(mmu_idx);
deb2db99 10134
30901475
AB
10135 return regime_using_lpae_format(env, mmu_idx);
10136}
10137
c47eaf9f 10138#ifndef CONFIG_USER_ONLY
0480f69a
PM
10139static inline bool regime_is_user(CPUARMState *env, ARMMMUIdx mmu_idx)
10140{
10141 switch (mmu_idx) {
fba37aed 10142 case ARMMMUIdx_SE10_0:
b9f6033c 10143 case ARMMMUIdx_E20_0:
b6ad6062 10144 case ARMMMUIdx_SE20_0:
2859d7b5 10145 case ARMMMUIdx_Stage1_E0:
e7b921c2 10146 case ARMMMUIdx_MUser:
871bec7c 10147 case ARMMMUIdx_MSUser:
62593718
PM
10148 case ARMMMUIdx_MUserNegPri:
10149 case ARMMMUIdx_MSUserNegPri:
0480f69a
PM
10150 return true;
10151 default:
10152 return false;
01b98b68
RH
10153 case ARMMMUIdx_E10_0:
10154 case ARMMMUIdx_E10_1:
452ef8cb 10155 case ARMMMUIdx_E10_1_PAN:
0480f69a
PM
10156 g_assert_not_reached();
10157 }
10158}
10159
0fbf5238
AJ
10160/* Translate section/page access permissions to page
10161 * R/W protection flags
d76951b6
AJ
10162 *
10163 * @env: CPUARMState
10164 * @mmu_idx: MMU index indicating required translation regime
10165 * @ap: The 3-bit access permissions (AP[2:0])
10166 * @domain_prot: The 2-bit domain access permissions
0fbf5238
AJ
10167 */
10168static inline int ap_to_rw_prot(CPUARMState *env, ARMMMUIdx mmu_idx,
10169 int ap, int domain_prot)
10170{
554b0b09
PM
10171 bool is_user = regime_is_user(env, mmu_idx);
10172
10173 if (domain_prot == 3) {
10174 return PAGE_READ | PAGE_WRITE;
10175 }
10176
554b0b09
PM
10177 switch (ap) {
10178 case 0:
10179 if (arm_feature(env, ARM_FEATURE_V7)) {
10180 return 0;
10181 }
554b0b09
PM
10182 switch (regime_sctlr(env, mmu_idx) & (SCTLR_S | SCTLR_R)) {
10183 case SCTLR_S:
10184 return is_user ? 0 : PAGE_READ;
10185 case SCTLR_R:
10186 return PAGE_READ;
10187 default:
10188 return 0;
10189 }
10190 case 1:
10191 return is_user ? 0 : PAGE_READ | PAGE_WRITE;
10192 case 2:
87c3d486 10193 if (is_user) {
0fbf5238 10194 return PAGE_READ;
87c3d486 10195 } else {
554b0b09 10196 return PAGE_READ | PAGE_WRITE;
87c3d486 10197 }
554b0b09
PM
10198 case 3:
10199 return PAGE_READ | PAGE_WRITE;
10200 case 4: /* Reserved. */
10201 return 0;
10202 case 5:
0fbf5238 10203 return is_user ? 0 : PAGE_READ;
554b0b09 10204 case 6:
0fbf5238 10205 return PAGE_READ;
554b0b09 10206 case 7:
87c3d486 10207 if (!arm_feature(env, ARM_FEATURE_V6K)) {
554b0b09 10208 return 0;
87c3d486 10209 }
0fbf5238 10210 return PAGE_READ;
554b0b09 10211 default:
0fbf5238 10212 g_assert_not_reached();
554b0b09 10213 }
b5ff1b31
FB
10214}
10215
d76951b6
AJ
10216/* Translate section/page access permissions to page
10217 * R/W protection flags.
10218 *
d76951b6 10219 * @ap: The 2-bit simple AP (AP[2:1])
d8e052b3 10220 * @is_user: TRUE if accessing from PL0
d76951b6 10221 */
d8e052b3 10222static inline int simple_ap_to_rw_prot_is_user(int ap, bool is_user)
d76951b6 10223{
d76951b6
AJ
10224 switch (ap) {
10225 case 0:
10226 return is_user ? 0 : PAGE_READ | PAGE_WRITE;
10227 case 1:
10228 return PAGE_READ | PAGE_WRITE;
10229 case 2:
10230 return is_user ? 0 : PAGE_READ;
10231 case 3:
10232 return PAGE_READ;
10233 default:
10234 g_assert_not_reached();
10235 }
10236}
10237
d8e052b3
AJ
10238static inline int
10239simple_ap_to_rw_prot(CPUARMState *env, ARMMMUIdx mmu_idx, int ap)
10240{
10241 return simple_ap_to_rw_prot_is_user(ap, regime_is_user(env, mmu_idx));
10242}
10243
6ab1a5ee
EI
10244/* Translate S2 section/page access permissions to protection flags
10245 *
10246 * @env: CPUARMState
10247 * @s2ap: The 2-bit stage2 access permissions (S2AP)
ce3125be
PM
10248 * @xn: XN (execute-never) bits
10249 * @s1_is_el0: true if this is S2 of an S1+2 walk for EL0
6ab1a5ee 10250 */
ce3125be 10251static int get_S2prot(CPUARMState *env, int s2ap, int xn, bool s1_is_el0)
6ab1a5ee
EI
10252{
10253 int prot = 0;
10254
10255 if (s2ap & 1) {
10256 prot |= PAGE_READ;
10257 }
10258 if (s2ap & 2) {
10259 prot |= PAGE_WRITE;
10260 }
ce3125be
PM
10261
10262 if (cpu_isar_feature(any_tts2uxn, env_archcpu(env))) {
10263 switch (xn) {
10264 case 0:
dfda6837 10265 prot |= PAGE_EXEC;
ce3125be
PM
10266 break;
10267 case 1:
10268 if (s1_is_el0) {
10269 prot |= PAGE_EXEC;
10270 }
10271 break;
10272 case 2:
10273 break;
10274 case 3:
10275 if (!s1_is_el0) {
10276 prot |= PAGE_EXEC;
10277 }
10278 break;
10279 default:
10280 g_assert_not_reached();
10281 }
10282 } else {
10283 if (!extract32(xn, 1, 1)) {
10284 if (arm_el_is_aa64(env, 2) || prot & PAGE_READ) {
10285 prot |= PAGE_EXEC;
10286 }
dfda6837 10287 }
6ab1a5ee
EI
10288 }
10289 return prot;
10290}
10291
d8e052b3
AJ
10292/* Translate section/page access permissions to protection flags
10293 *
10294 * @env: CPUARMState
10295 * @mmu_idx: MMU index indicating required translation regime
10296 * @is_aa64: TRUE if AArch64
10297 * @ap: The 2-bit simple AP (AP[2:1])
10298 * @ns: NS (non-secure) bit
10299 * @xn: XN (execute-never) bit
10300 * @pxn: PXN (privileged execute-never) bit
10301 */
10302static int get_S1prot(CPUARMState *env, ARMMMUIdx mmu_idx, bool is_aa64,
10303 int ap, int ns, int xn, int pxn)
10304{
10305 bool is_user = regime_is_user(env, mmu_idx);
10306 int prot_rw, user_rw;
10307 bool have_wxn;
10308 int wxn = 0;
10309
97fa9350 10310 assert(mmu_idx != ARMMMUIdx_Stage2);
d8e052b3
AJ
10311
10312 user_rw = simple_ap_to_rw_prot_is_user(ap, true);
10313 if (is_user) {
10314 prot_rw = user_rw;
10315 } else {
81636b70 10316 if (user_rw && regime_is_pan(env, mmu_idx)) {
f4e1dbc5
PM
10317 /* PAN forbids data accesses but doesn't affect insn fetch */
10318 prot_rw = 0;
10319 } else {
10320 prot_rw = simple_ap_to_rw_prot_is_user(ap, false);
81636b70 10321 }
d8e052b3
AJ
10322 }
10323
10324 if (ns && arm_is_secure(env) && (env->cp15.scr_el3 & SCR_SIF)) {
10325 return prot_rw;
10326 }
10327
10328 /* TODO have_wxn should be replaced with
10329 * ARM_FEATURE_V8 || (ARM_FEATURE_V7 && ARM_FEATURE_EL2)
10330 * when ARM_FEATURE_EL2 starts getting set. For now we assume all LPAE
10331 * compatible processors have EL2, which is required for [U]WXN.
10332 */
10333 have_wxn = arm_feature(env, ARM_FEATURE_LPAE);
10334
10335 if (have_wxn) {
10336 wxn = regime_sctlr(env, mmu_idx) & SCTLR_WXN;
10337 }
10338
10339 if (is_aa64) {
339370b9
RH
10340 if (regime_has_2_ranges(mmu_idx) && !is_user) {
10341 xn = pxn || (user_rw & PAGE_WRITE);
d8e052b3
AJ
10342 }
10343 } else if (arm_feature(env, ARM_FEATURE_V7)) {
10344 switch (regime_el(env, mmu_idx)) {
10345 case 1:
10346 case 3:
10347 if (is_user) {
10348 xn = xn || !(user_rw & PAGE_READ);
10349 } else {
10350 int uwxn = 0;
10351 if (have_wxn) {
10352 uwxn = regime_sctlr(env, mmu_idx) & SCTLR_UWXN;
10353 }
10354 xn = xn || !(prot_rw & PAGE_READ) || pxn ||
10355 (uwxn && (user_rw & PAGE_WRITE));
10356 }
10357 break;
10358 case 2:
10359 break;
10360 }
10361 } else {
10362 xn = wxn = 0;
10363 }
10364
10365 if (xn || (wxn && (prot_rw & PAGE_WRITE))) {
10366 return prot_rw;
10367 }
10368 return prot_rw | PAGE_EXEC;
10369}
10370
0480f69a
PM
10371static bool get_level1_table_address(CPUARMState *env, ARMMMUIdx mmu_idx,
10372 uint32_t *table, uint32_t address)
b2fa1797 10373{
0480f69a 10374 /* Note that we can only get here for an AArch32 PL0/PL1 lookup */
0480f69a 10375 TCR *tcr = regime_tcr(env, mmu_idx);
11f136ee 10376
11f136ee
FA
10377 if (address & tcr->mask) {
10378 if (tcr->raw_tcr & TTBCR_PD1) {
e389be16
FA
10379 /* Translation table walk disabled for TTBR1 */
10380 return false;
10381 }
aef878be 10382 *table = regime_ttbr(env, mmu_idx, 1) & 0xffffc000;
e389be16 10383 } else {
11f136ee 10384 if (tcr->raw_tcr & TTBCR_PD0) {
e389be16
FA
10385 /* Translation table walk disabled for TTBR0 */
10386 return false;
10387 }
aef878be 10388 *table = regime_ttbr(env, mmu_idx, 0) & tcr->base_mask;
e389be16
FA
10389 }
10390 *table |= (address >> 18) & 0x3ffc;
10391 return true;
b2fa1797
PB
10392}
10393
37785977
EI
10394/* Translate a S1 pagetable walk through S2 if needed. */
10395static hwaddr S1_ptw_translate(CPUARMState *env, ARMMMUIdx mmu_idx,
3d4bd397 10396 hwaddr addr, bool *is_secure,
37785977
EI
10397 ARMMMUFaultInfo *fi)
10398{
fee7aa46 10399 if (arm_mmu_idx_is_stage1_of_2(mmu_idx) &&
97fa9350 10400 !regime_translation_disabled(env, ARMMMUIdx_Stage2)) {
37785977
EI
10401 target_ulong s2size;
10402 hwaddr s2pa;
10403 int s2prot;
10404 int ret;
eadb2feb 10405 ARMCacheAttrs cacheattrs = {};
3d4bd397
RDC
10406 MemTxAttrs txattrs = {};
10407
10408 assert(!*is_secure); /* TODO: S-EL2 */
37785977 10409
59dff859 10410 ret = get_phys_addr_lpae(env, addr, MMU_DATA_LOAD, ARMMMUIdx_Stage2,
ff7de2fc 10411 false,
59dff859 10412 &s2pa, &txattrs, &s2prot, &s2size, fi,
a6d6f37a 10413 &cacheattrs);
37785977 10414 if (ret) {
3b39d734 10415 assert(fi->type != ARMFault_None);
37785977
EI
10416 fi->s2addr = addr;
10417 fi->stage2 = true;
10418 fi->s1ptw = true;
10419 return ~0;
10420 }
e04a5752
RDC
10421 if ((arm_hcr_el2_eff(env) & HCR_PTW) &&
10422 (cacheattrs.attrs & 0xf0) == 0) {
a6d6f37a
RH
10423 /*
10424 * PTW set and S1 walk touched S2 Device memory:
10425 * generate Permission fault.
10426 */
eadb2feb
PM
10427 fi->type = ARMFault_Permission;
10428 fi->s2addr = addr;
10429 fi->stage2 = true;
10430 fi->s1ptw = true;
10431 return ~0;
10432 }
588c6dd1
RDC
10433
10434 if (arm_is_secure_below_el3(env)) {
10435 /* Check if page table walk is to secure or non-secure PA space. */
10436 if (*is_secure) {
10437 *is_secure = !(env->cp15.vstcr_el2.raw_tcr & VSTCR_SW);
10438 } else {
10439 *is_secure = !(env->cp15.vtcr_el2.raw_tcr & VTCR_NSW);
10440 }
10441 } else {
10442 assert(!*is_secure);
10443 }
10444
37785977
EI
10445 addr = s2pa;
10446 }
10447 return addr;
10448}
10449
14577270 10450/* All loads done in the course of a page table walk go through here. */
a614e698 10451static uint32_t arm_ldl_ptw(CPUState *cs, hwaddr addr, bool is_secure,
3795a6de 10452 ARMMMUIdx mmu_idx, ARMMMUFaultInfo *fi)
ebca90e4 10453{
a614e698
EI
10454 ARMCPU *cpu = ARM_CPU(cs);
10455 CPUARMState *env = &cpu->env;
ebca90e4 10456 MemTxAttrs attrs = {};
3b39d734 10457 MemTxResult result = MEMTX_OK;
5ce4ff65 10458 AddressSpace *as;
3b39d734 10459 uint32_t data;
ebca90e4 10460
3d4bd397 10461 addr = S1_ptw_translate(env, mmu_idx, addr, &is_secure, fi);
ebca90e4 10462 attrs.secure = is_secure;
5ce4ff65 10463 as = arm_addressspace(cs, attrs);
a614e698
EI
10464 if (fi->s1ptw) {
10465 return 0;
10466 }
73462ddd 10467 if (regime_translation_big_endian(env, mmu_idx)) {
3b39d734 10468 data = address_space_ldl_be(as, addr, attrs, &result);
73462ddd 10469 } else {
3b39d734 10470 data = address_space_ldl_le(as, addr, attrs, &result);
73462ddd 10471 }
3b39d734
PM
10472 if (result == MEMTX_OK) {
10473 return data;
10474 }
10475 fi->type = ARMFault_SyncExternalOnWalk;
10476 fi->ea = arm_extabort_type(result);
10477 return 0;
ebca90e4
PM
10478}
10479
37785977 10480static uint64_t arm_ldq_ptw(CPUState *cs, hwaddr addr, bool is_secure,
3795a6de 10481 ARMMMUIdx mmu_idx, ARMMMUFaultInfo *fi)
ebca90e4 10482{
37785977
EI
10483 ARMCPU *cpu = ARM_CPU(cs);
10484 CPUARMState *env = &cpu->env;
ebca90e4 10485 MemTxAttrs attrs = {};
3b39d734 10486 MemTxResult result = MEMTX_OK;
5ce4ff65 10487 AddressSpace *as;
9aea1ea3 10488 uint64_t data;
ebca90e4 10489
3d4bd397 10490 addr = S1_ptw_translate(env, mmu_idx, addr, &is_secure, fi);
ebca90e4 10491 attrs.secure = is_secure;
5ce4ff65 10492 as = arm_addressspace(cs, attrs);
37785977
EI
10493 if (fi->s1ptw) {
10494 return 0;
10495 }
73462ddd 10496 if (regime_translation_big_endian(env, mmu_idx)) {
3b39d734 10497 data = address_space_ldq_be(as, addr, attrs, &result);
73462ddd 10498 } else {
3b39d734
PM
10499 data = address_space_ldq_le(as, addr, attrs, &result);
10500 }
10501 if (result == MEMTX_OK) {
10502 return data;
73462ddd 10503 }
3b39d734
PM
10504 fi->type = ARMFault_SyncExternalOnWalk;
10505 fi->ea = arm_extabort_type(result);
10506 return 0;
ebca90e4
PM
10507}
10508
b7cc4e82 10509static bool get_phys_addr_v5(CPUARMState *env, uint32_t address,
03ae85f8 10510 MMUAccessType access_type, ARMMMUIdx mmu_idx,
b7cc4e82 10511 hwaddr *phys_ptr, int *prot,
f989983e 10512 target_ulong *page_size,
e14b5a23 10513 ARMMMUFaultInfo *fi)
b5ff1b31 10514{
2fc0cc0e 10515 CPUState *cs = env_cpu(env);
f989983e 10516 int level = 1;
b5ff1b31
FB
10517 uint32_t table;
10518 uint32_t desc;
10519 int type;
10520 int ap;
e389be16 10521 int domain = 0;
dd4ebc2e 10522 int domain_prot;
a8170e5e 10523 hwaddr phys_addr;
0480f69a 10524 uint32_t dacr;
b5ff1b31 10525
9ee6e8bb
PB
10526 /* Pagetable walk. */
10527 /* Lookup l1 descriptor. */
0480f69a 10528 if (!get_level1_table_address(env, mmu_idx, &table, address)) {
e389be16 10529 /* Section translation fault if page walk is disabled by PD0 or PD1 */
f989983e 10530 fi->type = ARMFault_Translation;
e389be16
FA
10531 goto do_fault;
10532 }
a614e698 10533 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
3795a6de 10534 mmu_idx, fi);
3b39d734
PM
10535 if (fi->type != ARMFault_None) {
10536 goto do_fault;
10537 }
9ee6e8bb 10538 type = (desc & 3);
dd4ebc2e 10539 domain = (desc >> 5) & 0x0f;
0480f69a
PM
10540 if (regime_el(env, mmu_idx) == 1) {
10541 dacr = env->cp15.dacr_ns;
10542 } else {
10543 dacr = env->cp15.dacr_s;
10544 }
10545 domain_prot = (dacr >> (domain * 2)) & 3;
9ee6e8bb 10546 if (type == 0) {
601d70b9 10547 /* Section translation fault. */
f989983e 10548 fi->type = ARMFault_Translation;
9ee6e8bb
PB
10549 goto do_fault;
10550 }
f989983e
PM
10551 if (type != 2) {
10552 level = 2;
10553 }
dd4ebc2e 10554 if (domain_prot == 0 || domain_prot == 2) {
f989983e 10555 fi->type = ARMFault_Domain;
9ee6e8bb
PB
10556 goto do_fault;
10557 }
10558 if (type == 2) {
10559 /* 1Mb section. */
10560 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
10561 ap = (desc >> 10) & 3;
d4c430a8 10562 *page_size = 1024 * 1024;
9ee6e8bb
PB
10563 } else {
10564 /* Lookup l2 entry. */
554b0b09
PM
10565 if (type == 1) {
10566 /* Coarse pagetable. */
10567 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
10568 } else {
10569 /* Fine pagetable. */
10570 table = (desc & 0xfffff000) | ((address >> 8) & 0xffc);
10571 }
a614e698 10572 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
3795a6de 10573 mmu_idx, fi);
3b39d734
PM
10574 if (fi->type != ARMFault_None) {
10575 goto do_fault;
10576 }
9ee6e8bb
PB
10577 switch (desc & 3) {
10578 case 0: /* Page translation fault. */
f989983e 10579 fi->type = ARMFault_Translation;
9ee6e8bb
PB
10580 goto do_fault;
10581 case 1: /* 64k page. */
10582 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
10583 ap = (desc >> (4 + ((address >> 13) & 6))) & 3;
d4c430a8 10584 *page_size = 0x10000;
ce819861 10585 break;
9ee6e8bb
PB
10586 case 2: /* 4k page. */
10587 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
c10f7fc3 10588 ap = (desc >> (4 + ((address >> 9) & 6))) & 3;
d4c430a8 10589 *page_size = 0x1000;
ce819861 10590 break;
fc1891c7 10591 case 3: /* 1k page, or ARMv6/XScale "extended small (4k) page" */
554b0b09 10592 if (type == 1) {
fc1891c7
PM
10593 /* ARMv6/XScale extended small page format */
10594 if (arm_feature(env, ARM_FEATURE_XSCALE)
10595 || arm_feature(env, ARM_FEATURE_V6)) {
554b0b09 10596 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
fc1891c7 10597 *page_size = 0x1000;
554b0b09 10598 } else {
fc1891c7
PM
10599 /* UNPREDICTABLE in ARMv5; we choose to take a
10600 * page translation fault.
10601 */
f989983e 10602 fi->type = ARMFault_Translation;
554b0b09
PM
10603 goto do_fault;
10604 }
10605 } else {
10606 phys_addr = (desc & 0xfffffc00) | (address & 0x3ff);
fc1891c7 10607 *page_size = 0x400;
554b0b09 10608 }
9ee6e8bb 10609 ap = (desc >> 4) & 3;
ce819861
PB
10610 break;
10611 default:
9ee6e8bb
PB
10612 /* Never happens, but compiler isn't smart enough to tell. */
10613 abort();
ce819861 10614 }
9ee6e8bb 10615 }
0fbf5238
AJ
10616 *prot = ap_to_rw_prot(env, mmu_idx, ap, domain_prot);
10617 *prot |= *prot ? PAGE_EXEC : 0;
10618 if (!(*prot & (1 << access_type))) {
9ee6e8bb 10619 /* Access permission fault. */
f989983e 10620 fi->type = ARMFault_Permission;
9ee6e8bb
PB
10621 goto do_fault;
10622 }
10623 *phys_ptr = phys_addr;
b7cc4e82 10624 return false;
9ee6e8bb 10625do_fault:
f989983e
PM
10626 fi->domain = domain;
10627 fi->level = level;
b7cc4e82 10628 return true;
9ee6e8bb
PB
10629}
10630
b7cc4e82 10631static bool get_phys_addr_v6(CPUARMState *env, uint32_t address,
03ae85f8 10632 MMUAccessType access_type, ARMMMUIdx mmu_idx,
b7cc4e82 10633 hwaddr *phys_ptr, MemTxAttrs *attrs, int *prot,
f06cf243 10634 target_ulong *page_size, ARMMMUFaultInfo *fi)
9ee6e8bb 10635{
2fc0cc0e 10636 CPUState *cs = env_cpu(env);
0ae0326b 10637 ARMCPU *cpu = env_archcpu(env);
f06cf243 10638 int level = 1;
9ee6e8bb
PB
10639 uint32_t table;
10640 uint32_t desc;
10641 uint32_t xn;
de9b05b8 10642 uint32_t pxn = 0;
9ee6e8bb
PB
10643 int type;
10644 int ap;
de9b05b8 10645 int domain = 0;
dd4ebc2e 10646 int domain_prot;
a8170e5e 10647 hwaddr phys_addr;
0480f69a 10648 uint32_t dacr;
8bf5b6a9 10649 bool ns;
9ee6e8bb
PB
10650
10651 /* Pagetable walk. */
10652 /* Lookup l1 descriptor. */
0480f69a 10653 if (!get_level1_table_address(env, mmu_idx, &table, address)) {
e389be16 10654 /* Section translation fault if page walk is disabled by PD0 or PD1 */
f06cf243 10655 fi->type = ARMFault_Translation;
e389be16
FA
10656 goto do_fault;
10657 }
a614e698 10658 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
3795a6de 10659 mmu_idx, fi);
3b39d734
PM
10660 if (fi->type != ARMFault_None) {
10661 goto do_fault;
10662 }
9ee6e8bb 10663 type = (desc & 3);
0ae0326b 10664 if (type == 0 || (type == 3 && !cpu_isar_feature(aa32_pxn, cpu))) {
de9b05b8
PM
10665 /* Section translation fault, or attempt to use the encoding
10666 * which is Reserved on implementations without PXN.
10667 */
f06cf243 10668 fi->type = ARMFault_Translation;
9ee6e8bb 10669 goto do_fault;
de9b05b8
PM
10670 }
10671 if ((type == 1) || !(desc & (1 << 18))) {
10672 /* Page or Section. */
dd4ebc2e 10673 domain = (desc >> 5) & 0x0f;
9ee6e8bb 10674 }
0480f69a
PM
10675 if (regime_el(env, mmu_idx) == 1) {
10676 dacr = env->cp15.dacr_ns;
10677 } else {
10678 dacr = env->cp15.dacr_s;
10679 }
f06cf243
PM
10680 if (type == 1) {
10681 level = 2;
10682 }
0480f69a 10683 domain_prot = (dacr >> (domain * 2)) & 3;
dd4ebc2e 10684 if (domain_prot == 0 || domain_prot == 2) {
f06cf243
PM
10685 /* Section or Page domain fault */
10686 fi->type = ARMFault_Domain;
9ee6e8bb
PB
10687 goto do_fault;
10688 }
de9b05b8 10689 if (type != 1) {
9ee6e8bb
PB
10690 if (desc & (1 << 18)) {
10691 /* Supersection. */
10692 phys_addr = (desc & 0xff000000) | (address & 0x00ffffff);
4e42a6ca
SF
10693 phys_addr |= (uint64_t)extract32(desc, 20, 4) << 32;
10694 phys_addr |= (uint64_t)extract32(desc, 5, 4) << 36;
d4c430a8 10695 *page_size = 0x1000000;
b5ff1b31 10696 } else {
9ee6e8bb
PB
10697 /* Section. */
10698 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
d4c430a8 10699 *page_size = 0x100000;
b5ff1b31 10700 }
9ee6e8bb
PB
10701 ap = ((desc >> 10) & 3) | ((desc >> 13) & 4);
10702 xn = desc & (1 << 4);
de9b05b8 10703 pxn = desc & 1;
8bf5b6a9 10704 ns = extract32(desc, 19, 1);
9ee6e8bb 10705 } else {
0ae0326b 10706 if (cpu_isar_feature(aa32_pxn, cpu)) {
de9b05b8
PM
10707 pxn = (desc >> 2) & 1;
10708 }
8bf5b6a9 10709 ns = extract32(desc, 3, 1);
9ee6e8bb
PB
10710 /* Lookup l2 entry. */
10711 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
a614e698 10712 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
3795a6de 10713 mmu_idx, fi);
3b39d734
PM
10714 if (fi->type != ARMFault_None) {
10715 goto do_fault;
10716 }
9ee6e8bb
PB
10717 ap = ((desc >> 4) & 3) | ((desc >> 7) & 4);
10718 switch (desc & 3) {
10719 case 0: /* Page translation fault. */
f06cf243 10720 fi->type = ARMFault_Translation;
b5ff1b31 10721 goto do_fault;
9ee6e8bb
PB
10722 case 1: /* 64k page. */
10723 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
10724 xn = desc & (1 << 15);
d4c430a8 10725 *page_size = 0x10000;
9ee6e8bb
PB
10726 break;
10727 case 2: case 3: /* 4k page. */
10728 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
10729 xn = desc & 1;
d4c430a8 10730 *page_size = 0x1000;
9ee6e8bb
PB
10731 break;
10732 default:
10733 /* Never happens, but compiler isn't smart enough to tell. */
10734 abort();
b5ff1b31 10735 }
9ee6e8bb 10736 }
dd4ebc2e 10737 if (domain_prot == 3) {
c0034328
JR
10738 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
10739 } else {
0480f69a 10740 if (pxn && !regime_is_user(env, mmu_idx)) {
de9b05b8
PM
10741 xn = 1;
10742 }
f06cf243
PM
10743 if (xn && access_type == MMU_INST_FETCH) {
10744 fi->type = ARMFault_Permission;
c0034328 10745 goto do_fault;
f06cf243 10746 }
9ee6e8bb 10747
d76951b6
AJ
10748 if (arm_feature(env, ARM_FEATURE_V6K) &&
10749 (regime_sctlr(env, mmu_idx) & SCTLR_AFE)) {
10750 /* The simplified model uses AP[0] as an access control bit. */
10751 if ((ap & 1) == 0) {
10752 /* Access flag fault. */
f06cf243 10753 fi->type = ARMFault_AccessFlag;
d76951b6
AJ
10754 goto do_fault;
10755 }
10756 *prot = simple_ap_to_rw_prot(env, mmu_idx, ap >> 1);
10757 } else {
10758 *prot = ap_to_rw_prot(env, mmu_idx, ap, domain_prot);
c0034328 10759 }
0fbf5238
AJ
10760 if (*prot && !xn) {
10761 *prot |= PAGE_EXEC;
10762 }
10763 if (!(*prot & (1 << access_type))) {
c0034328 10764 /* Access permission fault. */
f06cf243 10765 fi->type = ARMFault_Permission;
c0034328
JR
10766 goto do_fault;
10767 }
3ad493fc 10768 }
8bf5b6a9
PM
10769 if (ns) {
10770 /* The NS bit will (as required by the architecture) have no effect if
10771 * the CPU doesn't support TZ or this is a non-secure translation
10772 * regime, because the attribute will already be non-secure.
10773 */
10774 attrs->secure = false;
10775 }
9ee6e8bb 10776 *phys_ptr = phys_addr;
b7cc4e82 10777 return false;
b5ff1b31 10778do_fault:
f06cf243
PM
10779 fi->domain = domain;
10780 fi->level = level;
b7cc4e82 10781 return true;
b5ff1b31
FB
10782}
10783
1853d5a9 10784/*
a0e966c9 10785 * check_s2_mmu_setup
1853d5a9
EI
10786 * @cpu: ARMCPU
10787 * @is_aa64: True if the translation regime is in AArch64 state
10788 * @startlevel: Suggested starting level
10789 * @inputsize: Bitsize of IPAs
10790 * @stride: Page-table stride (See the ARM ARM)
10791 *
a0e966c9
EI
10792 * Returns true if the suggested S2 translation parameters are OK and
10793 * false otherwise.
1853d5a9 10794 */
a0e966c9
EI
10795static bool check_s2_mmu_setup(ARMCPU *cpu, bool is_aa64, int level,
10796 int inputsize, int stride)
1853d5a9 10797{
98d68ec2
EI
10798 const int grainsize = stride + 3;
10799 int startsizecheck;
10800
1853d5a9
EI
10801 /* Negative levels are never allowed. */
10802 if (level < 0) {
10803 return false;
10804 }
10805
98d68ec2
EI
10806 startsizecheck = inputsize - ((3 - level) * stride + grainsize);
10807 if (startsizecheck < 1 || startsizecheck > stride + 4) {
10808 return false;
10809 }
10810
1853d5a9 10811 if (is_aa64) {
3526423e 10812 CPUARMState *env = &cpu->env;
1853d5a9
EI
10813 unsigned int pamax = arm_pamax(cpu);
10814
10815 switch (stride) {
10816 case 13: /* 64KB Pages. */
10817 if (level == 0 || (level == 1 && pamax <= 42)) {
10818 return false;
10819 }
10820 break;
10821 case 11: /* 16KB Pages. */
10822 if (level == 0 || (level == 1 && pamax <= 40)) {
10823 return false;
10824 }
10825 break;
10826 case 9: /* 4KB Pages. */
10827 if (level == 0 && pamax <= 42) {
10828 return false;
10829 }
10830 break;
10831 default:
10832 g_assert_not_reached();
10833 }
3526423e
EI
10834
10835 /* Inputsize checks. */
10836 if (inputsize > pamax &&
10837 (arm_el_is_aa64(env, 1) || inputsize > 40)) {
10838 /* This is CONSTRAINED UNPREDICTABLE and we choose to fault. */
10839 return false;
10840 }
1853d5a9 10841 } else {
1853d5a9
EI
10842 /* AArch32 only supports 4KB pages. Assert on that. */
10843 assert(stride == 9);
10844
10845 if (level == 0) {
10846 return false;
10847 }
1853d5a9
EI
10848 }
10849 return true;
10850}
10851
5b2d261d
AB
10852/* Translate from the 4-bit stage 2 representation of
10853 * memory attributes (without cache-allocation hints) to
10854 * the 8-bit representation of the stage 1 MAIR registers
10855 * (which includes allocation hints).
10856 *
10857 * ref: shared/translation/attrs/S2AttrDecode()
10858 * .../S2ConvertAttrsHints()
10859 */
10860static uint8_t convert_stage2_attrs(CPUARMState *env, uint8_t s2attrs)
10861{
10862 uint8_t hiattr = extract32(s2attrs, 2, 2);
10863 uint8_t loattr = extract32(s2attrs, 0, 2);
10864 uint8_t hihint = 0, lohint = 0;
10865
10866 if (hiattr != 0) { /* normal memory */
e04a5752 10867 if (arm_hcr_el2_eff(env) & HCR_CD) { /* cache disabled */
5b2d261d
AB
10868 hiattr = loattr = 1; /* non-cacheable */
10869 } else {
10870 if (hiattr != 1) { /* Write-through or write-back */
10871 hihint = 3; /* RW allocate */
10872 }
10873 if (loattr != 1) { /* Write-through or write-back */
10874 lohint = 3; /* RW allocate */
10875 }
10876 }
10877 }
10878
10879 return (hiattr << 6) | (hihint << 4) | (loattr << 2) | lohint;
10880}
c47eaf9f 10881#endif /* !CONFIG_USER_ONLY */
5b2d261d 10882
b830a5ee
RH
10883static int aa64_va_parameter_tbi(uint64_t tcr, ARMMMUIdx mmu_idx)
10884{
10885 if (regime_has_2_ranges(mmu_idx)) {
10886 return extract64(tcr, 37, 2);
10887 } else if (mmu_idx == ARMMMUIdx_Stage2) {
10888 return 0; /* VTCR_EL2 */
10889 } else {
3e270f67
RH
10890 /* Replicate the single TBI bit so we always have 2 bits. */
10891 return extract32(tcr, 20, 1) * 3;
b830a5ee
RH
10892 }
10893}
10894
10895static int aa64_va_parameter_tbid(uint64_t tcr, ARMMMUIdx mmu_idx)
10896{
10897 if (regime_has_2_ranges(mmu_idx)) {
10898 return extract64(tcr, 51, 2);
10899 } else if (mmu_idx == ARMMMUIdx_Stage2) {
10900 return 0; /* VTCR_EL2 */
10901 } else {
3e270f67
RH
10902 /* Replicate the single TBID bit so we always have 2 bits. */
10903 return extract32(tcr, 29, 1) * 3;
b830a5ee
RH
10904 }
10905}
10906
81ae05fa
RH
10907static int aa64_va_parameter_tcma(uint64_t tcr, ARMMMUIdx mmu_idx)
10908{
10909 if (regime_has_2_ranges(mmu_idx)) {
10910 return extract64(tcr, 57, 2);
10911 } else {
10912 /* Replicate the single TCMA bit so we always have 2 bits. */
10913 return extract32(tcr, 30, 1) * 3;
10914 }
10915}
10916
b830a5ee
RH
10917ARMVAParameters aa64_va_parameters(CPUARMState *env, uint64_t va,
10918 ARMMMUIdx mmu_idx, bool data)
ba97be9f
RH
10919{
10920 uint64_t tcr = regime_tcr(env, mmu_idx)->raw_tcr;
b830a5ee 10921 bool epd, hpd, using16k, using64k;
c36c65ea 10922 int select, tsz, tbi, max_tsz;
ba97be9f 10923
339370b9 10924 if (!regime_has_2_ranges(mmu_idx)) {
71d18164 10925 select = 0;
ba97be9f
RH
10926 tsz = extract32(tcr, 0, 6);
10927 using64k = extract32(tcr, 14, 1);
10928 using16k = extract32(tcr, 15, 1);
97fa9350 10929 if (mmu_idx == ARMMMUIdx_Stage2) {
ba97be9f 10930 /* VTCR_EL2 */
b830a5ee 10931 hpd = false;
ba97be9f 10932 } else {
ba97be9f
RH
10933 hpd = extract32(tcr, 24, 1);
10934 }
10935 epd = false;
ba97be9f 10936 } else {
71d18164
RH
10937 /*
10938 * Bit 55 is always between the two regions, and is canonical for
10939 * determining if address tagging is enabled.
10940 */
10941 select = extract64(va, 55, 1);
10942 if (!select) {
10943 tsz = extract32(tcr, 0, 6);
10944 epd = extract32(tcr, 7, 1);
10945 using64k = extract32(tcr, 14, 1);
10946 using16k = extract32(tcr, 15, 1);
71d18164 10947 hpd = extract64(tcr, 41, 1);
71d18164
RH
10948 } else {
10949 int tg = extract32(tcr, 30, 2);
10950 using16k = tg == 1;
10951 using64k = tg == 3;
10952 tsz = extract32(tcr, 16, 6);
10953 epd = extract32(tcr, 23, 1);
71d18164 10954 hpd = extract64(tcr, 42, 1);
71d18164 10955 }
ba97be9f 10956 }
c36c65ea
RDC
10957
10958 if (cpu_isar_feature(aa64_st, env_archcpu(env))) {
10959 max_tsz = 48 - using64k;
10960 } else {
10961 max_tsz = 39;
10962 }
10963
10964 tsz = MIN(tsz, max_tsz);
ba97be9f
RH
10965 tsz = MAX(tsz, 16); /* TODO: ARMv8.2-LVA */
10966
b830a5ee
RH
10967 /* Present TBI as a composite with TBID. */
10968 tbi = aa64_va_parameter_tbi(tcr, mmu_idx);
10969 if (!data) {
10970 tbi &= ~aa64_va_parameter_tbid(tcr, mmu_idx);
10971 }
10972 tbi = (tbi >> select) & 1;
10973
ba97be9f
RH
10974 return (ARMVAParameters) {
10975 .tsz = tsz,
10976 .select = select,
10977 .tbi = tbi,
10978 .epd = epd,
10979 .hpd = hpd,
10980 .using16k = using16k,
10981 .using64k = using64k,
10982 };
10983}
10984
c47eaf9f 10985#ifndef CONFIG_USER_ONLY
ba97be9f
RH
10986static ARMVAParameters aa32_va_parameters(CPUARMState *env, uint32_t va,
10987 ARMMMUIdx mmu_idx)
10988{
10989 uint64_t tcr = regime_tcr(env, mmu_idx)->raw_tcr;
10990 uint32_t el = regime_el(env, mmu_idx);
10991 int select, tsz;
10992 bool epd, hpd;
10993
97fa9350 10994 if (mmu_idx == ARMMMUIdx_Stage2) {
ba97be9f
RH
10995 /* VTCR */
10996 bool sext = extract32(tcr, 4, 1);
10997 bool sign = extract32(tcr, 3, 1);
10998
10999 /*
11000 * If the sign-extend bit is not the same as t0sz[3], the result
11001 * is unpredictable. Flag this as a guest error.
11002 */
11003 if (sign != sext) {
11004 qemu_log_mask(LOG_GUEST_ERROR,
11005 "AArch32: VTCR.S / VTCR.T0SZ[3] mismatch\n");
11006 }
11007 tsz = sextract32(tcr, 0, 4) + 8;
11008 select = 0;
11009 hpd = false;
11010 epd = false;
11011 } else if (el == 2) {
11012 /* HTCR */
11013 tsz = extract32(tcr, 0, 3);
11014 select = 0;
11015 hpd = extract64(tcr, 24, 1);
11016 epd = false;
11017 } else {
11018 int t0sz = extract32(tcr, 0, 3);
11019 int t1sz = extract32(tcr, 16, 3);
11020
11021 if (t1sz == 0) {
11022 select = va > (0xffffffffu >> t0sz);
11023 } else {
11024 /* Note that we will detect errors later. */
11025 select = va >= ~(0xffffffffu >> t1sz);
11026 }
11027 if (!select) {
11028 tsz = t0sz;
11029 epd = extract32(tcr, 7, 1);
11030 hpd = extract64(tcr, 41, 1);
11031 } else {
11032 tsz = t1sz;
11033 epd = extract32(tcr, 23, 1);
11034 hpd = extract64(tcr, 42, 1);
11035 }
11036 /* For aarch32, hpd0 is not enabled without t2e as well. */
11037 hpd &= extract32(tcr, 6, 1);
11038 }
11039
11040 return (ARMVAParameters) {
11041 .tsz = tsz,
11042 .select = select,
11043 .epd = epd,
11044 .hpd = hpd,
11045 };
11046}
11047
ff7de2fc
PM
11048/**
11049 * get_phys_addr_lpae: perform one stage of page table walk, LPAE format
11050 *
11051 * Returns false if the translation was successful. Otherwise, phys_ptr, attrs,
11052 * prot and page_size may not be filled in, and the populated fsr value provides
11053 * information on why the translation aborted, in the format of a long-format
11054 * DFSR/IFSR fault register, with the following caveats:
11055 * * the WnR bit is never set (the caller must do this).
11056 *
11057 * @env: CPUARMState
11058 * @address: virtual address to get physical address for
11059 * @access_type: MMU_DATA_LOAD, MMU_DATA_STORE or MMU_INST_FETCH
11060 * @mmu_idx: MMU index indicating required translation regime
11061 * @s1_is_el0: if @mmu_idx is ARMMMUIdx_Stage2 (so this is a stage 2 page table
11062 * walk), must be true if this is stage 2 of a stage 1+2 walk for an
11063 * EL0 access). If @mmu_idx is anything else, @s1_is_el0 is ignored.
11064 * @phys_ptr: set to the physical address corresponding to the virtual address
11065 * @attrs: set to the memory transaction attributes to use
11066 * @prot: set to the permissions for the page containing phys_ptr
11067 * @page_size_ptr: set to the size of the page containing phys_ptr
11068 * @fi: set to fault info if the translation fails
11069 * @cacheattrs: (if non-NULL) set to the cacheability/shareability attributes
11070 */
98e87797 11071static bool get_phys_addr_lpae(CPUARMState *env, uint64_t address,
03ae85f8 11072 MMUAccessType access_type, ARMMMUIdx mmu_idx,
ff7de2fc 11073 bool s1_is_el0,
b7cc4e82 11074 hwaddr *phys_ptr, MemTxAttrs *txattrs, int *prot,
da909b2c 11075 target_ulong *page_size_ptr,
5b2d261d 11076 ARMMMUFaultInfo *fi, ARMCacheAttrs *cacheattrs)
3dde962f 11077{
2fc0cc0e 11078 ARMCPU *cpu = env_archcpu(env);
1853d5a9 11079 CPUState *cs = CPU(cpu);
3dde962f 11080 /* Read an LPAE long-descriptor translation table. */
da909b2c 11081 ARMFaultType fault_type = ARMFault_Translation;
1b4093ea 11082 uint32_t level;
ba97be9f 11083 ARMVAParameters param;
3dde962f 11084 uint64_t ttbr;
dddb5223 11085 hwaddr descaddr, indexmask, indexmask_grainsize;
3dde962f 11086 uint32_t tableattrs;
36d820af 11087 target_ulong page_size;
3dde962f 11088 uint32_t attrs;
ba97be9f
RH
11089 int32_t stride;
11090 int addrsize, inputsize;
0480f69a 11091 TCR *tcr = regime_tcr(env, mmu_idx);
d8e052b3 11092 int ap, ns, xn, pxn;
88e8add8 11093 uint32_t el = regime_el(env, mmu_idx);
6109769a 11094 uint64_t descaddrmask;
6e99f762 11095 bool aarch64 = arm_el_is_aa64(env, el);
1bafc2ba 11096 bool guarded = false;
0480f69a 11097
07d1be3b 11098 /* TODO: This code does not support shareability levels. */
6e99f762 11099 if (aarch64) {
ba97be9f
RH
11100 param = aa64_va_parameters(env, address, mmu_idx,
11101 access_type != MMU_INST_FETCH);
1b4093ea 11102 level = 0;
ba97be9f
RH
11103 addrsize = 64 - 8 * param.tbi;
11104 inputsize = 64 - param.tsz;
d0a2cbce 11105 } else {
ba97be9f 11106 param = aa32_va_parameters(env, address, mmu_idx);
1b4093ea 11107 level = 1;
97fa9350 11108 addrsize = (mmu_idx == ARMMMUIdx_Stage2 ? 40 : 32);
ba97be9f 11109 inputsize = addrsize - param.tsz;
2c8dd318 11110 }
3dde962f 11111
ba97be9f
RH
11112 /*
11113 * We determined the region when collecting the parameters, but we
11114 * have not yet validated that the address is valid for the region.
11115 * Extract the top bits and verify that they all match select.
36d820af
RH
11116 *
11117 * For aa32, if inputsize == addrsize, then we have selected the
11118 * region by exclusion in aa32_va_parameters and there is no more
11119 * validation to do here.
11120 */
11121 if (inputsize < addrsize) {
11122 target_ulong top_bits = sextract64(address, inputsize,
11123 addrsize - inputsize);
03f27724 11124 if (-top_bits != param.select) {
36d820af
RH
11125 /* The gap between the two regions is a Translation fault */
11126 fault_type = ARMFault_Translation;
11127 goto do_fault;
11128 }
3dde962f
PM
11129 }
11130
ba97be9f
RH
11131 if (param.using64k) {
11132 stride = 13;
11133 } else if (param.using16k) {
11134 stride = 11;
11135 } else {
11136 stride = 9;
11137 }
11138
3dde962f
PM
11139 /* Note that QEMU ignores shareability and cacheability attributes,
11140 * so we don't need to do anything with the SH, ORGN, IRGN fields
11141 * in the TTBCR. Similarly, TTBCR:A1 selects whether we get the
11142 * ASID from TTBR0 or TTBR1, but QEMU's TLB doesn't currently
11143 * implement any ASID-like capability so we can ignore it (instead
11144 * we will always flush the TLB any time the ASID is changed).
11145 */
ba97be9f 11146 ttbr = regime_ttbr(env, mmu_idx, param.select);
3dde962f 11147
0480f69a 11148 /* Here we should have set up all the parameters for the translation:
6e99f762 11149 * inputsize, ttbr, epd, stride, tbi
0480f69a
PM
11150 */
11151
ba97be9f 11152 if (param.epd) {
88e8add8
GB
11153 /* Translation table walk disabled => Translation fault on TLB miss
11154 * Note: This is always 0 on 64-bit EL2 and EL3.
11155 */
3dde962f
PM
11156 goto do_fault;
11157 }
11158
97fa9350 11159 if (mmu_idx != ARMMMUIdx_Stage2) {
1853d5a9
EI
11160 /* The starting level depends on the virtual address size (which can
11161 * be up to 48 bits) and the translation granule size. It indicates
11162 * the number of strides (stride bits at a time) needed to
11163 * consume the bits of the input address. In the pseudocode this is:
11164 * level = 4 - RoundUp((inputsize - grainsize) / stride)
11165 * where their 'inputsize' is our 'inputsize', 'grainsize' is
11166 * our 'stride + 3' and 'stride' is our 'stride'.
11167 * Applying the usual "rounded up m/n is (m+n-1)/n" and simplifying:
11168 * = 4 - (inputsize - stride - 3 + stride - 1) / stride
11169 * = 4 - (inputsize - 4) / stride;
11170 */
11171 level = 4 - (inputsize - 4) / stride;
11172 } else {
11173 /* For stage 2 translations the starting level is specified by the
11174 * VTCR_EL2.SL0 field (whose interpretation depends on the page size)
11175 */
1b4093ea
SS
11176 uint32_t sl0 = extract32(tcr->raw_tcr, 6, 2);
11177 uint32_t startlevel;
1853d5a9
EI
11178 bool ok;
11179
6e99f762 11180 if (!aarch64 || stride == 9) {
1853d5a9 11181 /* AArch32 or 4KB pages */
1b4093ea 11182 startlevel = 2 - sl0;
c36c65ea
RDC
11183
11184 if (cpu_isar_feature(aa64_st, cpu)) {
11185 startlevel &= 3;
11186 }
1853d5a9
EI
11187 } else {
11188 /* 16KB or 64KB pages */
1b4093ea 11189 startlevel = 3 - sl0;
1853d5a9
EI
11190 }
11191
11192 /* Check that the starting level is valid. */
6e99f762 11193 ok = check_s2_mmu_setup(cpu, aarch64, startlevel,
1b4093ea 11194 inputsize, stride);
1853d5a9 11195 if (!ok) {
da909b2c 11196 fault_type = ARMFault_Translation;
1853d5a9
EI
11197 goto do_fault;
11198 }
1b4093ea 11199 level = startlevel;
1853d5a9 11200 }
3dde962f 11201
dddb5223
SS
11202 indexmask_grainsize = (1ULL << (stride + 3)) - 1;
11203 indexmask = (1ULL << (inputsize - (stride * (4 - level)))) - 1;
3dde962f
PM
11204
11205 /* Now we can extract the actual base address from the TTBR */
2c8dd318 11206 descaddr = extract64(ttbr, 0, 48);
41a4bf1f
PM
11207 /*
11208 * We rely on this masking to clear the RES0 bits at the bottom of the TTBR
11209 * and also to mask out CnP (bit 0) which could validly be non-zero.
11210 */
dddb5223 11211 descaddr &= ~indexmask;
3dde962f 11212
6109769a 11213 /* The address field in the descriptor goes up to bit 39 for ARMv7
dddb5223
SS
11214 * but up to bit 47 for ARMv8, but we use the descaddrmask
11215 * up to bit 39 for AArch32, because we don't need other bits in that case
11216 * to construct next descriptor address (anyway they should be all zeroes).
6109769a 11217 */
6e99f762 11218 descaddrmask = ((1ull << (aarch64 ? 48 : 40)) - 1) &
dddb5223 11219 ~indexmask_grainsize;
6109769a 11220
ebca90e4
PM
11221 /* Secure accesses start with the page table in secure memory and
11222 * can be downgraded to non-secure at any step. Non-secure accesses
11223 * remain non-secure. We implement this by just ORing in the NSTable/NS
11224 * bits at each step.
11225 */
11226 tableattrs = regime_is_secure(env, mmu_idx) ? 0 : (1 << 4);
3dde962f
PM
11227 for (;;) {
11228 uint64_t descriptor;
ebca90e4 11229 bool nstable;
3dde962f 11230
dddb5223 11231 descaddr |= (address >> (stride * (4 - level))) & indexmask;
2c8dd318 11232 descaddr &= ~7ULL;
ebca90e4 11233 nstable = extract32(tableattrs, 4, 1);
3795a6de 11234 descriptor = arm_ldq_ptw(cs, descaddr, !nstable, mmu_idx, fi);
3b39d734 11235 if (fi->type != ARMFault_None) {
37785977
EI
11236 goto do_fault;
11237 }
11238
3dde962f
PM
11239 if (!(descriptor & 1) ||
11240 (!(descriptor & 2) && (level == 3))) {
11241 /* Invalid, or the Reserved level 3 encoding */
11242 goto do_fault;
11243 }
6109769a 11244 descaddr = descriptor & descaddrmask;
3dde962f
PM
11245
11246 if ((descriptor & 2) && (level < 3)) {
037c13c5 11247 /* Table entry. The top five bits are attributes which may
3dde962f
PM
11248 * propagate down through lower levels of the table (and
11249 * which are all arranged so that 0 means "no effect", so
11250 * we can gather them up by ORing in the bits at each level).
11251 */
11252 tableattrs |= extract64(descriptor, 59, 5);
11253 level++;
dddb5223 11254 indexmask = indexmask_grainsize;
3dde962f
PM
11255 continue;
11256 }
11257 /* Block entry at level 1 or 2, or page entry at level 3.
11258 * These are basically the same thing, although the number
11259 * of bits we pull in from the vaddr varies.
11260 */
973a5434 11261 page_size = (1ULL << ((stride * (4 - level)) + 3));
3dde962f 11262 descaddr |= (address & (page_size - 1));
6ab1a5ee 11263 /* Extract attributes from the descriptor */
d615efac
IC
11264 attrs = extract64(descriptor, 2, 10)
11265 | (extract64(descriptor, 52, 12) << 10);
6ab1a5ee 11266
97fa9350 11267 if (mmu_idx == ARMMMUIdx_Stage2) {
6ab1a5ee
EI
11268 /* Stage 2 table descriptors do not include any attribute fields */
11269 break;
11270 }
11271 /* Merge in attributes from table descriptors */
037c13c5 11272 attrs |= nstable << 3; /* NS */
1bafc2ba 11273 guarded = extract64(descriptor, 50, 1); /* GP */
ba97be9f 11274 if (param.hpd) {
037c13c5
RH
11275 /* HPD disables all the table attributes except NSTable. */
11276 break;
11277 }
11278 attrs |= extract32(tableattrs, 0, 2) << 11; /* XN, PXN */
3dde962f
PM
11279 /* The sense of AP[1] vs APTable[0] is reversed, as APTable[0] == 1
11280 * means "force PL1 access only", which means forcing AP[1] to 0.
11281 */
037c13c5
RH
11282 attrs &= ~(extract32(tableattrs, 2, 1) << 4); /* !APT[0] => AP[1] */
11283 attrs |= extract32(tableattrs, 3, 1) << 5; /* APT[1] => AP[2] */
3dde962f
PM
11284 break;
11285 }
11286 /* Here descaddr is the final physical address, and attributes
11287 * are all in attrs.
11288 */
da909b2c 11289 fault_type = ARMFault_AccessFlag;
3dde962f
PM
11290 if ((attrs & (1 << 8)) == 0) {
11291 /* Access flag */
11292 goto do_fault;
11293 }
d8e052b3
AJ
11294
11295 ap = extract32(attrs, 4, 2);
d8e052b3 11296
97fa9350 11297 if (mmu_idx == ARMMMUIdx_Stage2) {
6ab1a5ee 11298 ns = true;
ce3125be
PM
11299 xn = extract32(attrs, 11, 2);
11300 *prot = get_S2prot(env, ap, xn, s1_is_el0);
6ab1a5ee
EI
11301 } else {
11302 ns = extract32(attrs, 3, 1);
ce3125be 11303 xn = extract32(attrs, 12, 1);
6ab1a5ee 11304 pxn = extract32(attrs, 11, 1);
6e99f762 11305 *prot = get_S1prot(env, mmu_idx, aarch64, ap, ns, xn, pxn);
6ab1a5ee 11306 }
d8e052b3 11307
da909b2c 11308 fault_type = ARMFault_Permission;
d8e052b3 11309 if (!(*prot & (1 << access_type))) {
3dde962f
PM
11310 goto do_fault;
11311 }
3dde962f 11312
8bf5b6a9
PM
11313 if (ns) {
11314 /* The NS bit will (as required by the architecture) have no effect if
11315 * the CPU doesn't support TZ or this is a non-secure translation
11316 * regime, because the attribute will already be non-secure.
11317 */
11318 txattrs->secure = false;
11319 }
1bafc2ba
RH
11320 /* When in aarch64 mode, and BTI is enabled, remember GP in the IOTLB. */
11321 if (aarch64 && guarded && cpu_isar_feature(aa64_bti, cpu)) {
149d3b31 11322 arm_tlb_bti_gp(txattrs) = true;
1bafc2ba 11323 }
5b2d261d 11324
7e98e21c
RH
11325 if (mmu_idx == ARMMMUIdx_Stage2) {
11326 cacheattrs->attrs = convert_stage2_attrs(env, extract32(attrs, 0, 4));
11327 } else {
11328 /* Index into MAIR registers for cache attributes */
11329 uint8_t attrindx = extract32(attrs, 0, 3);
11330 uint64_t mair = env->cp15.mair_el[regime_el(env, mmu_idx)];
11331 assert(attrindx <= 7);
11332 cacheattrs->attrs = extract64(mair, attrindx * 8, 8);
5b2d261d 11333 }
7e98e21c 11334 cacheattrs->shareability = extract32(attrs, 6, 2);
5b2d261d 11335
3dde962f
PM
11336 *phys_ptr = descaddr;
11337 *page_size_ptr = page_size;
b7cc4e82 11338 return false;
3dde962f
PM
11339
11340do_fault:
da909b2c
PM
11341 fi->type = fault_type;
11342 fi->level = level;
37785977 11343 /* Tag the error as S2 for failed S1 PTW at S2 or ordinary S2. */
97fa9350 11344 fi->stage2 = fi->s1ptw || (mmu_idx == ARMMMUIdx_Stage2);
b7cc4e82 11345 return true;
3dde962f
PM
11346}
11347
f6bda88f
PC
11348static inline void get_phys_addr_pmsav7_default(CPUARMState *env,
11349 ARMMMUIdx mmu_idx,
11350 int32_t address, int *prot)
11351{
3a00d560
MD
11352 if (!arm_feature(env, ARM_FEATURE_M)) {
11353 *prot = PAGE_READ | PAGE_WRITE;
11354 switch (address) {
11355 case 0xF0000000 ... 0xFFFFFFFF:
11356 if (regime_sctlr(env, mmu_idx) & SCTLR_V) {
11357 /* hivecs execing is ok */
11358 *prot |= PAGE_EXEC;
11359 }
11360 break;
11361 case 0x00000000 ... 0x7FFFFFFF:
f6bda88f 11362 *prot |= PAGE_EXEC;
3a00d560
MD
11363 break;
11364 }
11365 } else {
11366 /* Default system address map for M profile cores.
11367 * The architecture specifies which regions are execute-never;
11368 * at the MPU level no other checks are defined.
11369 */
11370 switch (address) {
11371 case 0x00000000 ... 0x1fffffff: /* ROM */
11372 case 0x20000000 ... 0x3fffffff: /* SRAM */
11373 case 0x60000000 ... 0x7fffffff: /* RAM */
11374 case 0x80000000 ... 0x9fffffff: /* RAM */
11375 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
11376 break;
11377 case 0x40000000 ... 0x5fffffff: /* Peripheral */
11378 case 0xa0000000 ... 0xbfffffff: /* Device */
11379 case 0xc0000000 ... 0xdfffffff: /* Device */
11380 case 0xe0000000 ... 0xffffffff: /* System */
11381 *prot = PAGE_READ | PAGE_WRITE;
11382 break;
11383 default:
11384 g_assert_not_reached();
f6bda88f 11385 }
f6bda88f 11386 }
f6bda88f
PC
11387}
11388
29c483a5
MD
11389static bool pmsav7_use_background_region(ARMCPU *cpu,
11390 ARMMMUIdx mmu_idx, bool is_user)
11391{
11392 /* Return true if we should use the default memory map as a
11393 * "background" region if there are no hits against any MPU regions.
11394 */
11395 CPUARMState *env = &cpu->env;
11396
11397 if (is_user) {
11398 return false;
11399 }
11400
11401 if (arm_feature(env, ARM_FEATURE_M)) {
ecf5e8ea
PM
11402 return env->v7m.mpu_ctrl[regime_is_secure(env, mmu_idx)]
11403 & R_V7M_MPU_CTRL_PRIVDEFENA_MASK;
29c483a5
MD
11404 } else {
11405 return regime_sctlr(env, mmu_idx) & SCTLR_BR;
11406 }
11407}
11408
38aaa60c
PM
11409static inline bool m_is_ppb_region(CPUARMState *env, uint32_t address)
11410{
11411 /* True if address is in the M profile PPB region 0xe0000000 - 0xe00fffff */
11412 return arm_feature(env, ARM_FEATURE_M) &&
11413 extract32(address, 20, 12) == 0xe00;
11414}
11415
bf446a11
PM
11416static inline bool m_is_system_region(CPUARMState *env, uint32_t address)
11417{
11418 /* True if address is in the M profile system region
11419 * 0xe0000000 - 0xffffffff
11420 */
11421 return arm_feature(env, ARM_FEATURE_M) && extract32(address, 29, 3) == 0x7;
11422}
11423
f6bda88f 11424static bool get_phys_addr_pmsav7(CPUARMState *env, uint32_t address,
03ae85f8 11425 MMUAccessType access_type, ARMMMUIdx mmu_idx,
9375ad15 11426 hwaddr *phys_ptr, int *prot,
e5e40999 11427 target_ulong *page_size,
9375ad15 11428 ARMMMUFaultInfo *fi)
f6bda88f 11429{
2fc0cc0e 11430 ARMCPU *cpu = env_archcpu(env);
f6bda88f
PC
11431 int n;
11432 bool is_user = regime_is_user(env, mmu_idx);
11433
11434 *phys_ptr = address;
e5e40999 11435 *page_size = TARGET_PAGE_SIZE;
f6bda88f
PC
11436 *prot = 0;
11437
38aaa60c
PM
11438 if (regime_translation_disabled(env, mmu_idx) ||
11439 m_is_ppb_region(env, address)) {
11440 /* MPU disabled or M profile PPB access: use default memory map.
11441 * The other case which uses the default memory map in the
11442 * v7M ARM ARM pseudocode is exception vector reads from the vector
11443 * table. In QEMU those accesses are done in arm_v7m_load_vector(),
11444 * which always does a direct read using address_space_ldl(), rather
11445 * than going via this function, so we don't need to check that here.
11446 */
f6bda88f
PC
11447 get_phys_addr_pmsav7_default(env, mmu_idx, address, prot);
11448 } else { /* MPU enabled */
11449 for (n = (int)cpu->pmsav7_dregion - 1; n >= 0; n--) {
11450 /* region search */
11451 uint32_t base = env->pmsav7.drbar[n];
11452 uint32_t rsize = extract32(env->pmsav7.drsr[n], 1, 5);
11453 uint32_t rmask;
11454 bool srdis = false;
11455
11456 if (!(env->pmsav7.drsr[n] & 0x1)) {
11457 continue;
11458 }
11459
11460 if (!rsize) {
c9f9f124
MD
11461 qemu_log_mask(LOG_GUEST_ERROR,
11462 "DRSR[%d]: Rsize field cannot be 0\n", n);
f6bda88f
PC
11463 continue;
11464 }
11465 rsize++;
11466 rmask = (1ull << rsize) - 1;
11467
11468 if (base & rmask) {
c9f9f124
MD
11469 qemu_log_mask(LOG_GUEST_ERROR,
11470 "DRBAR[%d]: 0x%" PRIx32 " misaligned "
11471 "to DRSR region size, mask = 0x%" PRIx32 "\n",
11472 n, base, rmask);
f6bda88f
PC
11473 continue;
11474 }
11475
11476 if (address < base || address > base + rmask) {
9d2b5a58
PM
11477 /*
11478 * Address not in this region. We must check whether the
11479 * region covers addresses in the same page as our address.
11480 * In that case we must not report a size that covers the
11481 * whole page for a subsequent hit against a different MPU
11482 * region or the background region, because it would result in
11483 * incorrect TLB hits for subsequent accesses to addresses that
11484 * are in this MPU region.
11485 */
11486 if (ranges_overlap(base, rmask,
11487 address & TARGET_PAGE_MASK,
11488 TARGET_PAGE_SIZE)) {
11489 *page_size = 1;
11490 }
f6bda88f
PC
11491 continue;
11492 }
11493
11494 /* Region matched */
11495
11496 if (rsize >= 8) { /* no subregions for regions < 256 bytes */
11497 int i, snd;
11498 uint32_t srdis_mask;
11499
11500 rsize -= 3; /* sub region size (power of 2) */
11501 snd = ((address - base) >> rsize) & 0x7;
11502 srdis = extract32(env->pmsav7.drsr[n], snd + 8, 1);
11503
11504 srdis_mask = srdis ? 0x3 : 0x0;
11505 for (i = 2; i <= 8 && rsize < TARGET_PAGE_BITS; i *= 2) {
11506 /* This will check in groups of 2, 4 and then 8, whether
11507 * the subregion bits are consistent. rsize is incremented
11508 * back up to give the region size, considering consistent
11509 * adjacent subregions as one region. Stop testing if rsize
11510 * is already big enough for an entire QEMU page.
11511 */
11512 int snd_rounded = snd & ~(i - 1);
11513 uint32_t srdis_multi = extract32(env->pmsav7.drsr[n],
11514 snd_rounded + 8, i);
11515 if (srdis_mask ^ srdis_multi) {
11516 break;
11517 }
11518 srdis_mask = (srdis_mask << i) | srdis_mask;
11519 rsize++;
11520 }
11521 }
f6bda88f
PC
11522 if (srdis) {
11523 continue;
11524 }
e5e40999
PM
11525 if (rsize < TARGET_PAGE_BITS) {
11526 *page_size = 1 << rsize;
11527 }
f6bda88f
PC
11528 break;
11529 }
11530
11531 if (n == -1) { /* no hits */
29c483a5 11532 if (!pmsav7_use_background_region(cpu, mmu_idx, is_user)) {
f6bda88f 11533 /* background fault */
9375ad15 11534 fi->type = ARMFault_Background;
f6bda88f
PC
11535 return true;
11536 }
11537 get_phys_addr_pmsav7_default(env, mmu_idx, address, prot);
11538 } else { /* a MPU hit! */
11539 uint32_t ap = extract32(env->pmsav7.dracr[n], 8, 3);
bf446a11
PM
11540 uint32_t xn = extract32(env->pmsav7.dracr[n], 12, 1);
11541
11542 if (m_is_system_region(env, address)) {
11543 /* System space is always execute never */
11544 xn = 1;
11545 }
f6bda88f
PC
11546
11547 if (is_user) { /* User mode AP bit decoding */
11548 switch (ap) {
11549 case 0:
11550 case 1:
11551 case 5:
11552 break; /* no access */
11553 case 3:
11554 *prot |= PAGE_WRITE;
11555 /* fall through */
11556 case 2:
11557 case 6:
11558 *prot |= PAGE_READ | PAGE_EXEC;
11559 break;
8638f1ad
PM
11560 case 7:
11561 /* for v7M, same as 6; for R profile a reserved value */
11562 if (arm_feature(env, ARM_FEATURE_M)) {
11563 *prot |= PAGE_READ | PAGE_EXEC;
11564 break;
11565 }
11566 /* fall through */
f6bda88f
PC
11567 default:
11568 qemu_log_mask(LOG_GUEST_ERROR,
c9f9f124
MD
11569 "DRACR[%d]: Bad value for AP bits: 0x%"
11570 PRIx32 "\n", n, ap);
f6bda88f
PC
11571 }
11572 } else { /* Priv. mode AP bits decoding */
11573 switch (ap) {
11574 case 0:
11575 break; /* no access */
11576 case 1:
11577 case 2:
11578 case 3:
11579 *prot |= PAGE_WRITE;
11580 /* fall through */
11581 case 5:
11582 case 6:
11583 *prot |= PAGE_READ | PAGE_EXEC;
11584 break;
8638f1ad
PM
11585 case 7:
11586 /* for v7M, same as 6; for R profile a reserved value */
11587 if (arm_feature(env, ARM_FEATURE_M)) {
11588 *prot |= PAGE_READ | PAGE_EXEC;
11589 break;
11590 }
11591 /* fall through */
f6bda88f
PC
11592 default:
11593 qemu_log_mask(LOG_GUEST_ERROR,
c9f9f124
MD
11594 "DRACR[%d]: Bad value for AP bits: 0x%"
11595 PRIx32 "\n", n, ap);
f6bda88f
PC
11596 }
11597 }
11598
11599 /* execute never */
bf446a11 11600 if (xn) {
f6bda88f
PC
11601 *prot &= ~PAGE_EXEC;
11602 }
11603 }
11604 }
11605
9375ad15
PM
11606 fi->type = ARMFault_Permission;
11607 fi->level = 1;
f6bda88f
PC
11608 return !(*prot & (1 << access_type));
11609}
11610
35337cc3
PM
11611static bool v8m_is_sau_exempt(CPUARMState *env,
11612 uint32_t address, MMUAccessType access_type)
11613{
11614 /* The architecture specifies that certain address ranges are
11615 * exempt from v8M SAU/IDAU checks.
11616 */
11617 return
11618 (access_type == MMU_INST_FETCH && m_is_system_region(env, address)) ||
11619 (address >= 0xe0000000 && address <= 0xe0002fff) ||
11620 (address >= 0xe000e000 && address <= 0xe000efff) ||
11621 (address >= 0xe002e000 && address <= 0xe002efff) ||
11622 (address >= 0xe0040000 && address <= 0xe0041fff) ||
11623 (address >= 0xe00ff000 && address <= 0xe00fffff);
11624}
11625
787a7e76 11626void v8m_security_lookup(CPUARMState *env, uint32_t address,
35337cc3
PM
11627 MMUAccessType access_type, ARMMMUIdx mmu_idx,
11628 V8M_SAttributes *sattrs)
11629{
11630 /* Look up the security attributes for this address. Compare the
11631 * pseudocode SecurityCheck() function.
11632 * We assume the caller has zero-initialized *sattrs.
11633 */
2fc0cc0e 11634 ARMCPU *cpu = env_archcpu(env);
35337cc3 11635 int r;
181962fd
PM
11636 bool idau_exempt = false, idau_ns = true, idau_nsc = true;
11637 int idau_region = IREGION_NOTVALID;
72042435
PM
11638 uint32_t addr_page_base = address & TARGET_PAGE_MASK;
11639 uint32_t addr_page_limit = addr_page_base + (TARGET_PAGE_SIZE - 1);
35337cc3 11640
181962fd
PM
11641 if (cpu->idau) {
11642 IDAUInterfaceClass *iic = IDAU_INTERFACE_GET_CLASS(cpu->idau);
11643 IDAUInterface *ii = IDAU_INTERFACE(cpu->idau);
11644
11645 iic->check(ii, address, &idau_region, &idau_exempt, &idau_ns,
11646 &idau_nsc);
11647 }
35337cc3
PM
11648
11649 if (access_type == MMU_INST_FETCH && extract32(address, 28, 4) == 0xf) {
11650 /* 0xf0000000..0xffffffff is always S for insn fetches */
11651 return;
11652 }
11653
181962fd 11654 if (idau_exempt || v8m_is_sau_exempt(env, address, access_type)) {
35337cc3
PM
11655 sattrs->ns = !regime_is_secure(env, mmu_idx);
11656 return;
11657 }
11658
181962fd
PM
11659 if (idau_region != IREGION_NOTVALID) {
11660 sattrs->irvalid = true;
11661 sattrs->iregion = idau_region;
11662 }
11663
35337cc3
PM
11664 switch (env->sau.ctrl & 3) {
11665 case 0: /* SAU.ENABLE == 0, SAU.ALLNS == 0 */
11666 break;
11667 case 2: /* SAU.ENABLE == 0, SAU.ALLNS == 1 */
11668 sattrs->ns = true;
11669 break;
11670 default: /* SAU.ENABLE == 1 */
11671 for (r = 0; r < cpu->sau_sregion; r++) {
11672 if (env->sau.rlar[r] & 1) {
11673 uint32_t base = env->sau.rbar[r] & ~0x1f;
11674 uint32_t limit = env->sau.rlar[r] | 0x1f;
11675
11676 if (base <= address && limit >= address) {
72042435
PM
11677 if (base > addr_page_base || limit < addr_page_limit) {
11678 sattrs->subpage = true;
11679 }
35337cc3
PM
11680 if (sattrs->srvalid) {
11681 /* If we hit in more than one region then we must report
11682 * as Secure, not NS-Callable, with no valid region
11683 * number info.
11684 */
11685 sattrs->ns = false;
11686 sattrs->nsc = false;
11687 sattrs->sregion = 0;
11688 sattrs->srvalid = false;
11689 break;
11690 } else {
11691 if (env->sau.rlar[r] & 2) {
11692 sattrs->nsc = true;
11693 } else {
11694 sattrs->ns = true;
11695 }
11696 sattrs->srvalid = true;
11697 sattrs->sregion = r;
11698 }
9d2b5a58
PM
11699 } else {
11700 /*
11701 * Address not in this region. We must check whether the
11702 * region covers addresses in the same page as our address.
11703 * In that case we must not report a size that covers the
11704 * whole page for a subsequent hit against a different MPU
11705 * region or the background region, because it would result
11706 * in incorrect TLB hits for subsequent accesses to
11707 * addresses that are in this MPU region.
11708 */
11709 if (limit >= base &&
11710 ranges_overlap(base, limit - base + 1,
11711 addr_page_base,
11712 TARGET_PAGE_SIZE)) {
11713 sattrs->subpage = true;
11714 }
35337cc3
PM
11715 }
11716 }
11717 }
7e3f1223
TR
11718 break;
11719 }
35337cc3 11720
7e3f1223
TR
11721 /*
11722 * The IDAU will override the SAU lookup results if it specifies
11723 * higher security than the SAU does.
11724 */
11725 if (!idau_ns) {
11726 if (sattrs->ns || (!idau_nsc && sattrs->nsc)) {
11727 sattrs->ns = false;
11728 sattrs->nsc = idau_nsc;
181962fd 11729 }
35337cc3
PM
11730 }
11731}
11732
787a7e76 11733bool pmsav8_mpu_lookup(CPUARMState *env, uint32_t address,
54317c0f
PM
11734 MMUAccessType access_type, ARMMMUIdx mmu_idx,
11735 hwaddr *phys_ptr, MemTxAttrs *txattrs,
72042435
PM
11736 int *prot, bool *is_subpage,
11737 ARMMMUFaultInfo *fi, uint32_t *mregion)
54317c0f
PM
11738{
11739 /* Perform a PMSAv8 MPU lookup (without also doing the SAU check
11740 * that a full phys-to-virt translation does).
11741 * mregion is (if not NULL) set to the region number which matched,
11742 * or -1 if no region number is returned (MPU off, address did not
11743 * hit a region, address hit in multiple regions).
72042435
PM
11744 * We set is_subpage to true if the region hit doesn't cover the
11745 * entire TARGET_PAGE the address is within.
54317c0f 11746 */
2fc0cc0e 11747 ARMCPU *cpu = env_archcpu(env);
504e3cc3 11748 bool is_user = regime_is_user(env, mmu_idx);
62c58ee0 11749 uint32_t secure = regime_is_secure(env, mmu_idx);
504e3cc3
PM
11750 int n;
11751 int matchregion = -1;
11752 bool hit = false;
72042435
PM
11753 uint32_t addr_page_base = address & TARGET_PAGE_MASK;
11754 uint32_t addr_page_limit = addr_page_base + (TARGET_PAGE_SIZE - 1);
504e3cc3 11755
72042435 11756 *is_subpage = false;
504e3cc3
PM
11757 *phys_ptr = address;
11758 *prot = 0;
54317c0f
PM
11759 if (mregion) {
11760 *mregion = -1;
35337cc3
PM
11761 }
11762
504e3cc3
PM
11763 /* Unlike the ARM ARM pseudocode, we don't need to check whether this
11764 * was an exception vector read from the vector table (which is always
11765 * done using the default system address map), because those accesses
11766 * are done in arm_v7m_load_vector(), which always does a direct
11767 * read using address_space_ldl(), rather than going via this function.
11768 */
11769 if (regime_translation_disabled(env, mmu_idx)) { /* MPU disabled */
11770 hit = true;
11771 } else if (m_is_ppb_region(env, address)) {
11772 hit = true;
504e3cc3 11773 } else {
cff21316
PM
11774 if (pmsav7_use_background_region(cpu, mmu_idx, is_user)) {
11775 hit = true;
11776 }
11777
504e3cc3
PM
11778 for (n = (int)cpu->pmsav7_dregion - 1; n >= 0; n--) {
11779 /* region search */
11780 /* Note that the base address is bits [31:5] from the register
11781 * with bits [4:0] all zeroes, but the limit address is bits
11782 * [31:5] from the register with bits [4:0] all ones.
11783 */
62c58ee0
PM
11784 uint32_t base = env->pmsav8.rbar[secure][n] & ~0x1f;
11785 uint32_t limit = env->pmsav8.rlar[secure][n] | 0x1f;
504e3cc3 11786
62c58ee0 11787 if (!(env->pmsav8.rlar[secure][n] & 0x1)) {
504e3cc3
PM
11788 /* Region disabled */
11789 continue;
11790 }
11791
11792 if (address < base || address > limit) {
9d2b5a58
PM
11793 /*
11794 * Address not in this region. We must check whether the
11795 * region covers addresses in the same page as our address.
11796 * In that case we must not report a size that covers the
11797 * whole page for a subsequent hit against a different MPU
11798 * region or the background region, because it would result in
11799 * incorrect TLB hits for subsequent accesses to addresses that
11800 * are in this MPU region.
11801 */
11802 if (limit >= base &&
11803 ranges_overlap(base, limit - base + 1,
11804 addr_page_base,
11805 TARGET_PAGE_SIZE)) {
11806 *is_subpage = true;
11807 }
504e3cc3
PM
11808 continue;
11809 }
11810
72042435
PM
11811 if (base > addr_page_base || limit < addr_page_limit) {
11812 *is_subpage = true;
11813 }
11814
cff21316 11815 if (matchregion != -1) {
504e3cc3
PM
11816 /* Multiple regions match -- always a failure (unlike
11817 * PMSAv7 where highest-numbered-region wins)
11818 */
3f551b5b
PM
11819 fi->type = ARMFault_Permission;
11820 fi->level = 1;
504e3cc3
PM
11821 return true;
11822 }
11823
11824 matchregion = n;
11825 hit = true;
504e3cc3
PM
11826 }
11827 }
11828
11829 if (!hit) {
11830 /* background fault */
3f551b5b 11831 fi->type = ARMFault_Background;
504e3cc3
PM
11832 return true;
11833 }
11834
11835 if (matchregion == -1) {
11836 /* hit using the background region */
11837 get_phys_addr_pmsav7_default(env, mmu_idx, address, prot);
11838 } else {
62c58ee0
PM
11839 uint32_t ap = extract32(env->pmsav8.rbar[secure][matchregion], 1, 2);
11840 uint32_t xn = extract32(env->pmsav8.rbar[secure][matchregion], 0, 1);
cad8e2e3
PM
11841 bool pxn = false;
11842
11843 if (arm_feature(env, ARM_FEATURE_V8_1M)) {
11844 pxn = extract32(env->pmsav8.rlar[secure][matchregion], 4, 1);
11845 }
504e3cc3
PM
11846
11847 if (m_is_system_region(env, address)) {
11848 /* System space is always execute never */
11849 xn = 1;
11850 }
11851
11852 *prot = simple_ap_to_rw_prot(env, mmu_idx, ap);
cad8e2e3 11853 if (*prot && !xn && !(pxn && !is_user)) {
504e3cc3
PM
11854 *prot |= PAGE_EXEC;
11855 }
11856 /* We don't need to look the attribute up in the MAIR0/MAIR1
11857 * registers because that only tells us about cacheability.
11858 */
54317c0f
PM
11859 if (mregion) {
11860 *mregion = matchregion;
11861 }
504e3cc3
PM
11862 }
11863
3f551b5b
PM
11864 fi->type = ARMFault_Permission;
11865 fi->level = 1;
504e3cc3
PM
11866 return !(*prot & (1 << access_type));
11867}
11868
54317c0f
PM
11869
11870static bool get_phys_addr_pmsav8(CPUARMState *env, uint32_t address,
11871 MMUAccessType access_type, ARMMMUIdx mmu_idx,
11872 hwaddr *phys_ptr, MemTxAttrs *txattrs,
72042435
PM
11873 int *prot, target_ulong *page_size,
11874 ARMMMUFaultInfo *fi)
54317c0f
PM
11875{
11876 uint32_t secure = regime_is_secure(env, mmu_idx);
11877 V8M_SAttributes sattrs = {};
72042435
PM
11878 bool ret;
11879 bool mpu_is_subpage;
54317c0f
PM
11880
11881 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
11882 v8m_security_lookup(env, address, access_type, mmu_idx, &sattrs);
11883 if (access_type == MMU_INST_FETCH) {
11884 /* Instruction fetches always use the MMU bank and the
11885 * transaction attribute determined by the fetch address,
11886 * regardless of CPU state. This is painful for QEMU
11887 * to handle, because it would mean we need to encode
11888 * into the mmu_idx not just the (user, negpri) information
11889 * for the current security state but also that for the
11890 * other security state, which would balloon the number
11891 * of mmu_idx values needed alarmingly.
11892 * Fortunately we can avoid this because it's not actually
11893 * possible to arbitrarily execute code from memory with
11894 * the wrong security attribute: it will always generate
11895 * an exception of some kind or another, apart from the
11896 * special case of an NS CPU executing an SG instruction
11897 * in S&NSC memory. So we always just fail the translation
11898 * here and sort things out in the exception handler
11899 * (including possibly emulating an SG instruction).
11900 */
11901 if (sattrs.ns != !secure) {
3f551b5b
PM
11902 if (sattrs.nsc) {
11903 fi->type = ARMFault_QEMU_NSCExec;
11904 } else {
11905 fi->type = ARMFault_QEMU_SFault;
11906 }
72042435 11907 *page_size = sattrs.subpage ? 1 : TARGET_PAGE_SIZE;
54317c0f
PM
11908 *phys_ptr = address;
11909 *prot = 0;
11910 return true;
11911 }
11912 } else {
11913 /* For data accesses we always use the MMU bank indicated
11914 * by the current CPU state, but the security attributes
11915 * might downgrade a secure access to nonsecure.
11916 */
11917 if (sattrs.ns) {
11918 txattrs->secure = false;
11919 } else if (!secure) {
11920 /* NS access to S memory must fault.
11921 * Architecturally we should first check whether the
11922 * MPU information for this address indicates that we
11923 * are doing an unaligned access to Device memory, which
11924 * should generate a UsageFault instead. QEMU does not
11925 * currently check for that kind of unaligned access though.
11926 * If we added it we would need to do so as a special case
11927 * for M_FAKE_FSR_SFAULT in arm_v7m_cpu_do_interrupt().
11928 */
3f551b5b 11929 fi->type = ARMFault_QEMU_SFault;
72042435 11930 *page_size = sattrs.subpage ? 1 : TARGET_PAGE_SIZE;
54317c0f
PM
11931 *phys_ptr = address;
11932 *prot = 0;
11933 return true;
11934 }
11935 }
11936 }
11937
72042435
PM
11938 ret = pmsav8_mpu_lookup(env, address, access_type, mmu_idx, phys_ptr,
11939 txattrs, prot, &mpu_is_subpage, fi, NULL);
72042435
PM
11940 *page_size = sattrs.subpage || mpu_is_subpage ? 1 : TARGET_PAGE_SIZE;
11941 return ret;
54317c0f
PM
11942}
11943
13689d43 11944static bool get_phys_addr_pmsav5(CPUARMState *env, uint32_t address,
03ae85f8 11945 MMUAccessType access_type, ARMMMUIdx mmu_idx,
53a4e5c5
PM
11946 hwaddr *phys_ptr, int *prot,
11947 ARMMMUFaultInfo *fi)
9ee6e8bb
PB
11948{
11949 int n;
11950 uint32_t mask;
11951 uint32_t base;
0480f69a 11952 bool is_user = regime_is_user(env, mmu_idx);
9ee6e8bb 11953
3279adb9
PM
11954 if (regime_translation_disabled(env, mmu_idx)) {
11955 /* MPU disabled. */
11956 *phys_ptr = address;
11957 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
11958 return false;
11959 }
11960
9ee6e8bb
PB
11961 *phys_ptr = address;
11962 for (n = 7; n >= 0; n--) {
554b0b09 11963 base = env->cp15.c6_region[n];
87c3d486 11964 if ((base & 1) == 0) {
554b0b09 11965 continue;
87c3d486 11966 }
554b0b09
PM
11967 mask = 1 << ((base >> 1) & 0x1f);
11968 /* Keep this shift separate from the above to avoid an
11969 (undefined) << 32. */
11970 mask = (mask << 1) - 1;
87c3d486 11971 if (((base ^ address) & ~mask) == 0) {
554b0b09 11972 break;
87c3d486 11973 }
9ee6e8bb 11974 }
87c3d486 11975 if (n < 0) {
53a4e5c5 11976 fi->type = ARMFault_Background;
b7cc4e82 11977 return true;
87c3d486 11978 }
9ee6e8bb 11979
03ae85f8 11980 if (access_type == MMU_INST_FETCH) {
7e09797c 11981 mask = env->cp15.pmsav5_insn_ap;
9ee6e8bb 11982 } else {
7e09797c 11983 mask = env->cp15.pmsav5_data_ap;
9ee6e8bb
PB
11984 }
11985 mask = (mask >> (n * 4)) & 0xf;
11986 switch (mask) {
11987 case 0:
53a4e5c5
PM
11988 fi->type = ARMFault_Permission;
11989 fi->level = 1;
b7cc4e82 11990 return true;
9ee6e8bb 11991 case 1:
87c3d486 11992 if (is_user) {
53a4e5c5
PM
11993 fi->type = ARMFault_Permission;
11994 fi->level = 1;
b7cc4e82 11995 return true;
87c3d486 11996 }
554b0b09
PM
11997 *prot = PAGE_READ | PAGE_WRITE;
11998 break;
9ee6e8bb 11999 case 2:
554b0b09 12000 *prot = PAGE_READ;
87c3d486 12001 if (!is_user) {
554b0b09 12002 *prot |= PAGE_WRITE;
87c3d486 12003 }
554b0b09 12004 break;
9ee6e8bb 12005 case 3:
554b0b09
PM
12006 *prot = PAGE_READ | PAGE_WRITE;
12007 break;
9ee6e8bb 12008 case 5:
87c3d486 12009 if (is_user) {
53a4e5c5
PM
12010 fi->type = ARMFault_Permission;
12011 fi->level = 1;
b7cc4e82 12012 return true;
87c3d486 12013 }
554b0b09
PM
12014 *prot = PAGE_READ;
12015 break;
9ee6e8bb 12016 case 6:
554b0b09
PM
12017 *prot = PAGE_READ;
12018 break;
9ee6e8bb 12019 default:
554b0b09 12020 /* Bad permission. */
53a4e5c5
PM
12021 fi->type = ARMFault_Permission;
12022 fi->level = 1;
b7cc4e82 12023 return true;
9ee6e8bb 12024 }
3ad493fc 12025 *prot |= PAGE_EXEC;
b7cc4e82 12026 return false;
9ee6e8bb
PB
12027}
12028
5b2d261d
AB
12029/* Combine either inner or outer cacheability attributes for normal
12030 * memory, according to table D4-42 and pseudocode procedure
12031 * CombineS1S2AttrHints() of ARM DDI 0487B.b (the ARMv8 ARM).
12032 *
12033 * NB: only stage 1 includes allocation hints (RW bits), leading to
12034 * some asymmetry.
12035 */
12036static uint8_t combine_cacheattr_nibble(uint8_t s1, uint8_t s2)
12037{
12038 if (s1 == 4 || s2 == 4) {
12039 /* non-cacheable has precedence */
12040 return 4;
12041 } else if (extract32(s1, 2, 2) == 0 || extract32(s1, 2, 2) == 2) {
12042 /* stage 1 write-through takes precedence */
12043 return s1;
12044 } else if (extract32(s2, 2, 2) == 2) {
12045 /* stage 2 write-through takes precedence, but the allocation hint
12046 * is still taken from stage 1
12047 */
12048 return (2 << 2) | extract32(s1, 0, 2);
12049 } else { /* write-back */
12050 return s1;
12051 }
12052}
12053
12054/* Combine S1 and S2 cacheability/shareability attributes, per D4.5.4
12055 * and CombineS1S2Desc()
12056 *
12057 * @s1: Attributes from stage 1 walk
12058 * @s2: Attributes from stage 2 walk
12059 */
12060static ARMCacheAttrs combine_cacheattrs(ARMCacheAttrs s1, ARMCacheAttrs s2)
12061{
337a03f0 12062 uint8_t s1lo, s2lo, s1hi, s2hi;
5b2d261d 12063 ARMCacheAttrs ret;
337a03f0
RH
12064 bool tagged = false;
12065
12066 if (s1.attrs == 0xf0) {
12067 tagged = true;
12068 s1.attrs = 0xff;
12069 }
12070
12071 s1lo = extract32(s1.attrs, 0, 4);
12072 s2lo = extract32(s2.attrs, 0, 4);
12073 s1hi = extract32(s1.attrs, 4, 4);
12074 s2hi = extract32(s2.attrs, 4, 4);
5b2d261d
AB
12075
12076 /* Combine shareability attributes (table D4-43) */
12077 if (s1.shareability == 2 || s2.shareability == 2) {
12078 /* if either are outer-shareable, the result is outer-shareable */
12079 ret.shareability = 2;
12080 } else if (s1.shareability == 3 || s2.shareability == 3) {
12081 /* if either are inner-shareable, the result is inner-shareable */
12082 ret.shareability = 3;
12083 } else {
12084 /* both non-shareable */
12085 ret.shareability = 0;
12086 }
12087
12088 /* Combine memory type and cacheability attributes */
12089 if (s1hi == 0 || s2hi == 0) {
12090 /* Device has precedence over normal */
12091 if (s1lo == 0 || s2lo == 0) {
12092 /* nGnRnE has precedence over anything */
12093 ret.attrs = 0;
12094 } else if (s1lo == 4 || s2lo == 4) {
12095 /* non-Reordering has precedence over Reordering */
12096 ret.attrs = 4; /* nGnRE */
12097 } else if (s1lo == 8 || s2lo == 8) {
12098 /* non-Gathering has precedence over Gathering */
12099 ret.attrs = 8; /* nGRE */
12100 } else {
12101 ret.attrs = 0xc; /* GRE */
12102 }
12103
12104 /* Any location for which the resultant memory type is any
12105 * type of Device memory is always treated as Outer Shareable.
12106 */
12107 ret.shareability = 2;
12108 } else { /* Normal memory */
12109 /* Outer/inner cacheability combine independently */
12110 ret.attrs = combine_cacheattr_nibble(s1hi, s2hi) << 4
12111 | combine_cacheattr_nibble(s1lo, s2lo);
12112
12113 if (ret.attrs == 0x44) {
12114 /* Any location for which the resultant memory type is Normal
12115 * Inner Non-cacheable, Outer Non-cacheable is always treated
12116 * as Outer Shareable.
12117 */
12118 ret.shareability = 2;
12119 }
12120 }
12121
337a03f0
RH
12122 /* TODO: CombineS1S2Desc does not consider transient, only WB, RWA. */
12123 if (tagged && ret.attrs == 0xff) {
12124 ret.attrs = 0xf0;
12125 }
12126
5b2d261d
AB
12127 return ret;
12128}
12129
12130
702a9357
PM
12131/* get_phys_addr - get the physical address for this virtual address
12132 *
12133 * Find the physical address corresponding to the given virtual address,
12134 * by doing a translation table walk on MMU based systems or using the
12135 * MPU state on MPU based systems.
12136 *
b7cc4e82
PC
12137 * Returns false if the translation was successful. Otherwise, phys_ptr, attrs,
12138 * prot and page_size may not be filled in, and the populated fsr value provides
702a9357
PM
12139 * information on why the translation aborted, in the format of a
12140 * DFSR/IFSR fault register, with the following caveats:
12141 * * we honour the short vs long DFSR format differences.
12142 * * the WnR bit is never set (the caller must do this).
f6bda88f 12143 * * for PSMAv5 based systems we don't bother to return a full FSR format
702a9357
PM
12144 * value.
12145 *
12146 * @env: CPUARMState
12147 * @address: virtual address to get physical address for
12148 * @access_type: 0 for read, 1 for write, 2 for execute
d3649702 12149 * @mmu_idx: MMU index indicating required translation regime
702a9357 12150 * @phys_ptr: set to the physical address corresponding to the virtual address
8bf5b6a9 12151 * @attrs: set to the memory transaction attributes to use
702a9357
PM
12152 * @prot: set to the permissions for the page containing phys_ptr
12153 * @page_size: set to the size of the page containing phys_ptr
5b2d261d
AB
12154 * @fi: set to fault info if the translation fails
12155 * @cacheattrs: (if non-NULL) set to the cacheability/shareability attributes
702a9357 12156 */
ebae861f
PMD
12157bool get_phys_addr(CPUARMState *env, target_ulong address,
12158 MMUAccessType access_type, ARMMMUIdx mmu_idx,
12159 hwaddr *phys_ptr, MemTxAttrs *attrs, int *prot,
12160 target_ulong *page_size,
12161 ARMMMUFaultInfo *fi, ARMCacheAttrs *cacheattrs)
9ee6e8bb 12162{
452ef8cb
RH
12163 if (mmu_idx == ARMMMUIdx_E10_0 ||
12164 mmu_idx == ARMMMUIdx_E10_1 ||
12165 mmu_idx == ARMMMUIdx_E10_1_PAN) {
9b539263
EI
12166 /* Call ourselves recursively to do the stage 1 and then stage 2
12167 * translations.
0480f69a 12168 */
9b539263
EI
12169 if (arm_feature(env, ARM_FEATURE_EL2)) {
12170 hwaddr ipa;
12171 int s2_prot;
12172 int ret;
5b2d261d 12173 ARMCacheAttrs cacheattrs2 = {};
9b539263
EI
12174
12175 ret = get_phys_addr(env, address, access_type,
8bd5c820 12176 stage_1_mmu_idx(mmu_idx), &ipa, attrs,
bc52bfeb 12177 prot, page_size, fi, cacheattrs);
9b539263
EI
12178
12179 /* If S1 fails or S2 is disabled, return early. */
97fa9350 12180 if (ret || regime_translation_disabled(env, ARMMMUIdx_Stage2)) {
9b539263
EI
12181 *phys_ptr = ipa;
12182 return ret;
12183 }
12184
12185 /* S1 is done. Now do S2 translation. */
97fa9350 12186 ret = get_phys_addr_lpae(env, ipa, access_type, ARMMMUIdx_Stage2,
ff7de2fc 12187 mmu_idx == ARMMMUIdx_E10_0,
9b539263 12188 phys_ptr, attrs, &s2_prot,
7e98e21c 12189 page_size, fi, &cacheattrs2);
9b539263
EI
12190 fi->s2addr = ipa;
12191 /* Combine the S1 and S2 perms. */
12192 *prot &= s2_prot;
5b2d261d 12193
7e98e21c
RH
12194 /* If S2 fails, return early. */
12195 if (ret) {
12196 return ret;
5b2d261d
AB
12197 }
12198
7e98e21c 12199 /* Combine the S1 and S2 cache attributes. */
e04a5752 12200 if (arm_hcr_el2_eff(env) & HCR_DC) {
7e98e21c
RH
12201 /*
12202 * HCR.DC forces the first stage attributes to
12203 * Normal Non-Shareable,
12204 * Inner Write-Back Read-Allocate Write-Allocate,
12205 * Outer Write-Back Read-Allocate Write-Allocate.
337a03f0 12206 * Do not overwrite Tagged within attrs.
7e98e21c 12207 */
337a03f0
RH
12208 if (cacheattrs->attrs != 0xf0) {
12209 cacheattrs->attrs = 0xff;
12210 }
7e98e21c
RH
12211 cacheattrs->shareability = 0;
12212 }
12213 *cacheattrs = combine_cacheattrs(*cacheattrs, cacheattrs2);
12214 return 0;
9b539263
EI
12215 } else {
12216 /*
12217 * For non-EL2 CPUs a stage1+stage2 translation is just stage 1.
12218 */
8bd5c820 12219 mmu_idx = stage_1_mmu_idx(mmu_idx);
9b539263 12220 }
0480f69a 12221 }
d3649702 12222
8bf5b6a9
PM
12223 /* The page table entries may downgrade secure to non-secure, but
12224 * cannot upgrade an non-secure translation regime's attributes
12225 * to secure.
12226 */
12227 attrs->secure = regime_is_secure(env, mmu_idx);
0995bf8c 12228 attrs->user = regime_is_user(env, mmu_idx);
8bf5b6a9 12229
0480f69a
PM
12230 /* Fast Context Switch Extension. This doesn't exist at all in v8.
12231 * In v7 and earlier it affects all stage 1 translations.
12232 */
97fa9350 12233 if (address < 0x02000000 && mmu_idx != ARMMMUIdx_Stage2
0480f69a
PM
12234 && !arm_feature(env, ARM_FEATURE_V8)) {
12235 if (regime_el(env, mmu_idx) == 3) {
12236 address += env->cp15.fcseidr_s;
12237 } else {
12238 address += env->cp15.fcseidr_ns;
12239 }
54bf36ed 12240 }
9ee6e8bb 12241
3279adb9 12242 if (arm_feature(env, ARM_FEATURE_PMSA)) {
c9f9f124 12243 bool ret;
f6bda88f 12244 *page_size = TARGET_PAGE_SIZE;
3279adb9 12245
504e3cc3
PM
12246 if (arm_feature(env, ARM_FEATURE_V8)) {
12247 /* PMSAv8 */
12248 ret = get_phys_addr_pmsav8(env, address, access_type, mmu_idx,
72042435 12249 phys_ptr, attrs, prot, page_size, fi);
504e3cc3 12250 } else if (arm_feature(env, ARM_FEATURE_V7)) {
3279adb9
PM
12251 /* PMSAv7 */
12252 ret = get_phys_addr_pmsav7(env, address, access_type, mmu_idx,
e5e40999 12253 phys_ptr, prot, page_size, fi);
3279adb9
PM
12254 } else {
12255 /* Pre-v7 MPU */
12256 ret = get_phys_addr_pmsav5(env, address, access_type, mmu_idx,
53a4e5c5 12257 phys_ptr, prot, fi);
3279adb9
PM
12258 }
12259 qemu_log_mask(CPU_LOG_MMU, "PMSA MPU lookup for %s at 0x%08" PRIx32
c9f9f124 12260 " mmu_idx %u -> %s (prot %c%c%c)\n",
709e4407
PM
12261 access_type == MMU_DATA_LOAD ? "reading" :
12262 (access_type == MMU_DATA_STORE ? "writing" : "execute"),
c9f9f124
MD
12263 (uint32_t)address, mmu_idx,
12264 ret ? "Miss" : "Hit",
12265 *prot & PAGE_READ ? 'r' : '-',
12266 *prot & PAGE_WRITE ? 'w' : '-',
12267 *prot & PAGE_EXEC ? 'x' : '-');
12268
12269 return ret;
f6bda88f
PC
12270 }
12271
3279adb9
PM
12272 /* Definitely a real MMU, not an MPU */
12273
0480f69a 12274 if (regime_translation_disabled(env, mmu_idx)) {
337a03f0
RH
12275 uint64_t hcr;
12276 uint8_t memattr;
12277
cebfb648
RH
12278 /*
12279 * MMU disabled. S1 addresses within aa64 translation regimes are
12280 * still checked for bounds -- see AArch64.TranslateAddressS1Off.
12281 */
12282 if (mmu_idx != ARMMMUIdx_Stage2) {
12283 int r_el = regime_el(env, mmu_idx);
12284 if (arm_el_is_aa64(env, r_el)) {
12285 int pamax = arm_pamax(env_archcpu(env));
12286 uint64_t tcr = env->cp15.tcr_el[r_el].raw_tcr;
12287 int addrtop, tbi;
12288
12289 tbi = aa64_va_parameter_tbi(tcr, mmu_idx);
12290 if (access_type == MMU_INST_FETCH) {
12291 tbi &= ~aa64_va_parameter_tbid(tcr, mmu_idx);
12292 }
12293 tbi = (tbi >> extract64(address, 55, 1)) & 1;
12294 addrtop = (tbi ? 55 : 63);
12295
12296 if (extract64(address, pamax, addrtop - pamax + 1) != 0) {
12297 fi->type = ARMFault_AddressSize;
12298 fi->level = 0;
12299 fi->stage2 = false;
12300 return 1;
12301 }
12302
12303 /*
12304 * When TBI is disabled, we've just validated that all of the
12305 * bits above PAMax are zero, so logically we only need to
12306 * clear the top byte for TBI. But it's clearer to follow
12307 * the pseudocode set of addrdesc.paddress.
12308 */
12309 address = extract64(address, 0, 52);
12310 }
12311 }
9ee6e8bb 12312 *phys_ptr = address;
3ad493fc 12313 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
d4c430a8 12314 *page_size = TARGET_PAGE_SIZE;
337a03f0
RH
12315
12316 /* Fill in cacheattr a-la AArch64.TranslateAddressS1Off. */
12317 hcr = arm_hcr_el2_eff(env);
12318 cacheattrs->shareability = 0;
12319 if (hcr & HCR_DC) {
12320 if (hcr & HCR_DCT) {
12321 memattr = 0xf0; /* Tagged, Normal, WB, RWA */
12322 } else {
12323 memattr = 0xff; /* Normal, WB, RWA */
12324 }
12325 } else if (access_type == MMU_INST_FETCH) {
12326 if (regime_sctlr(env, mmu_idx) & SCTLR_I) {
12327 memattr = 0xee; /* Normal, WT, RA, NT */
12328 } else {
12329 memattr = 0x44; /* Normal, NC, No */
12330 }
12331 cacheattrs->shareability = 2; /* outer sharable */
12332 } else {
12333 memattr = 0x00; /* Device, nGnRnE */
12334 }
12335 cacheattrs->attrs = memattr;
9ee6e8bb 12336 return 0;
0480f69a
PM
12337 }
12338
0480f69a 12339 if (regime_using_lpae_format(env, mmu_idx)) {
ff7de2fc 12340 return get_phys_addr_lpae(env, address, access_type, mmu_idx, false,
bc52bfeb
PM
12341 phys_ptr, attrs, prot, page_size,
12342 fi, cacheattrs);
0480f69a 12343 } else if (regime_sctlr(env, mmu_idx) & SCTLR_XP) {
bc52bfeb
PM
12344 return get_phys_addr_v6(env, address, access_type, mmu_idx,
12345 phys_ptr, attrs, prot, page_size, fi);
9ee6e8bb 12346 } else {
bc52bfeb 12347 return get_phys_addr_v5(env, address, access_type, mmu_idx,
f989983e 12348 phys_ptr, prot, page_size, fi);
9ee6e8bb
PB
12349 }
12350}
12351
0faea0c7
PM
12352hwaddr arm_cpu_get_phys_page_attrs_debug(CPUState *cs, vaddr addr,
12353 MemTxAttrs *attrs)
b5ff1b31 12354{
00b941e5 12355 ARMCPU *cpu = ARM_CPU(cs);
d3649702 12356 CPUARMState *env = &cpu->env;
a8170e5e 12357 hwaddr phys_addr;
d4c430a8 12358 target_ulong page_size;
b5ff1b31 12359 int prot;
b7cc4e82 12360 bool ret;
e14b5a23 12361 ARMMMUFaultInfo fi = {};
50494a27 12362 ARMMMUIdx mmu_idx = arm_mmu_idx(env);
7e98e21c 12363 ARMCacheAttrs cacheattrs = {};
b5ff1b31 12364
0faea0c7
PM
12365 *attrs = (MemTxAttrs) {};
12366
8bd5c820 12367 ret = get_phys_addr(env, addr, 0, mmu_idx, &phys_addr,
7e98e21c 12368 attrs, &prot, &page_size, &fi, &cacheattrs);
b5ff1b31 12369
b7cc4e82 12370 if (ret) {
b5ff1b31 12371 return -1;
00b941e5 12372 }
b5ff1b31
FB
12373 return phys_addr;
12374}
12375
b5ff1b31 12376#endif
6ddbc6e4
PB
12377
12378/* Note that signed overflow is undefined in C. The following routines are
12379 careful to use unsigned types where modulo arithmetic is required.
12380 Failure to do so _will_ break on newer gcc. */
12381
12382/* Signed saturating arithmetic. */
12383
1654b2d6 12384/* Perform 16-bit signed saturating addition. */
6ddbc6e4
PB
12385static inline uint16_t add16_sat(uint16_t a, uint16_t b)
12386{
12387 uint16_t res;
12388
12389 res = a + b;
12390 if (((res ^ a) & 0x8000) && !((a ^ b) & 0x8000)) {
12391 if (a & 0x8000)
12392 res = 0x8000;
12393 else
12394 res = 0x7fff;
12395 }
12396 return res;
12397}
12398
1654b2d6 12399/* Perform 8-bit signed saturating addition. */
6ddbc6e4
PB
12400static inline uint8_t add8_sat(uint8_t a, uint8_t b)
12401{
12402 uint8_t res;
12403
12404 res = a + b;
12405 if (((res ^ a) & 0x80) && !((a ^ b) & 0x80)) {
12406 if (a & 0x80)
12407 res = 0x80;
12408 else
12409 res = 0x7f;
12410 }
12411 return res;
12412}
12413
1654b2d6 12414/* Perform 16-bit signed saturating subtraction. */
6ddbc6e4
PB
12415static inline uint16_t sub16_sat(uint16_t a, uint16_t b)
12416{
12417 uint16_t res;
12418
12419 res = a - b;
12420 if (((res ^ a) & 0x8000) && ((a ^ b) & 0x8000)) {
12421 if (a & 0x8000)
12422 res = 0x8000;
12423 else
12424 res = 0x7fff;
12425 }
12426 return res;
12427}
12428
1654b2d6 12429/* Perform 8-bit signed saturating subtraction. */
6ddbc6e4
PB
12430static inline uint8_t sub8_sat(uint8_t a, uint8_t b)
12431{
12432 uint8_t res;
12433
12434 res = a - b;
12435 if (((res ^ a) & 0x80) && ((a ^ b) & 0x80)) {
12436 if (a & 0x80)
12437 res = 0x80;
12438 else
12439 res = 0x7f;
12440 }
12441 return res;
12442}
12443
12444#define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16);
12445#define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16);
12446#define ADD8(a, b, n) RESULT(add8_sat(a, b), n, 8);
12447#define SUB8(a, b, n) RESULT(sub8_sat(a, b), n, 8);
12448#define PFX q
12449
12450#include "op_addsub.h"
12451
12452/* Unsigned saturating arithmetic. */
460a09c1 12453static inline uint16_t add16_usat(uint16_t a, uint16_t b)
6ddbc6e4
PB
12454{
12455 uint16_t res;
12456 res = a + b;
12457 if (res < a)
12458 res = 0xffff;
12459 return res;
12460}
12461
460a09c1 12462static inline uint16_t sub16_usat(uint16_t a, uint16_t b)
6ddbc6e4 12463{
4c4fd3f8 12464 if (a > b)
6ddbc6e4
PB
12465 return a - b;
12466 else
12467 return 0;
12468}
12469
12470static inline uint8_t add8_usat(uint8_t a, uint8_t b)
12471{
12472 uint8_t res;
12473 res = a + b;
12474 if (res < a)
12475 res = 0xff;
12476 return res;
12477}
12478
12479static inline uint8_t sub8_usat(uint8_t a, uint8_t b)
12480{
4c4fd3f8 12481 if (a > b)
6ddbc6e4
PB
12482 return a - b;
12483 else
12484 return 0;
12485}
12486
12487#define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16);
12488#define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16);
12489#define ADD8(a, b, n) RESULT(add8_usat(a, b), n, 8);
12490#define SUB8(a, b, n) RESULT(sub8_usat(a, b), n, 8);
12491#define PFX uq
12492
12493#include "op_addsub.h"
12494
12495/* Signed modulo arithmetic. */
12496#define SARITH16(a, b, n, op) do { \
12497 int32_t sum; \
db6e2e65 12498 sum = (int32_t)(int16_t)(a) op (int32_t)(int16_t)(b); \
6ddbc6e4
PB
12499 RESULT(sum, n, 16); \
12500 if (sum >= 0) \
12501 ge |= 3 << (n * 2); \
12502 } while(0)
12503
12504#define SARITH8(a, b, n, op) do { \
12505 int32_t sum; \
db6e2e65 12506 sum = (int32_t)(int8_t)(a) op (int32_t)(int8_t)(b); \
6ddbc6e4
PB
12507 RESULT(sum, n, 8); \
12508 if (sum >= 0) \
12509 ge |= 1 << n; \
12510 } while(0)
12511
12512
12513#define ADD16(a, b, n) SARITH16(a, b, n, +)
12514#define SUB16(a, b, n) SARITH16(a, b, n, -)
12515#define ADD8(a, b, n) SARITH8(a, b, n, +)
12516#define SUB8(a, b, n) SARITH8(a, b, n, -)
12517#define PFX s
12518#define ARITH_GE
12519
12520#include "op_addsub.h"
12521
12522/* Unsigned modulo arithmetic. */
12523#define ADD16(a, b, n) do { \
12524 uint32_t sum; \
12525 sum = (uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b); \
12526 RESULT(sum, n, 16); \
a87aa10b 12527 if ((sum >> 16) == 1) \
6ddbc6e4
PB
12528 ge |= 3 << (n * 2); \
12529 } while(0)
12530
12531#define ADD8(a, b, n) do { \
12532 uint32_t sum; \
12533 sum = (uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b); \
12534 RESULT(sum, n, 8); \
a87aa10b
AZ
12535 if ((sum >> 8) == 1) \
12536 ge |= 1 << n; \
6ddbc6e4
PB
12537 } while(0)
12538
12539#define SUB16(a, b, n) do { \
12540 uint32_t sum; \
12541 sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \
12542 RESULT(sum, n, 16); \
12543 if ((sum >> 16) == 0) \
12544 ge |= 3 << (n * 2); \
12545 } while(0)
12546
12547#define SUB8(a, b, n) do { \
12548 uint32_t sum; \
12549 sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \
12550 RESULT(sum, n, 8); \
12551 if ((sum >> 8) == 0) \
a87aa10b 12552 ge |= 1 << n; \
6ddbc6e4
PB
12553 } while(0)
12554
12555#define PFX u
12556#define ARITH_GE
12557
12558#include "op_addsub.h"
12559
12560/* Halved signed arithmetic. */
12561#define ADD16(a, b, n) \
12562 RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16)
12563#define SUB16(a, b, n) \
12564 RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16)
12565#define ADD8(a, b, n) \
12566 RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8)
12567#define SUB8(a, b, n) \
12568 RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8)
12569#define PFX sh
12570
12571#include "op_addsub.h"
12572
12573/* Halved unsigned arithmetic. */
12574#define ADD16(a, b, n) \
12575 RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16)
12576#define SUB16(a, b, n) \
12577 RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16)
12578#define ADD8(a, b, n) \
12579 RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8)
12580#define SUB8(a, b, n) \
12581 RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8)
12582#define PFX uh
12583
12584#include "op_addsub.h"
12585
12586static inline uint8_t do_usad(uint8_t a, uint8_t b)
12587{
12588 if (a > b)
12589 return a - b;
12590 else
12591 return b - a;
12592}
12593
12594/* Unsigned sum of absolute byte differences. */
12595uint32_t HELPER(usad8)(uint32_t a, uint32_t b)
12596{
12597 uint32_t sum;
12598 sum = do_usad(a, b);
12599 sum += do_usad(a >> 8, b >> 8);
bdc3b6f5 12600 sum += do_usad(a >> 16, b >> 16);
6ddbc6e4
PB
12601 sum += do_usad(a >> 24, b >> 24);
12602 return sum;
12603}
12604
12605/* For ARMv6 SEL instruction. */
12606uint32_t HELPER(sel_flags)(uint32_t flags, uint32_t a, uint32_t b)
12607{
12608 uint32_t mask;
12609
12610 mask = 0;
12611 if (flags & 1)
12612 mask |= 0xff;
12613 if (flags & 2)
12614 mask |= 0xff00;
12615 if (flags & 4)
12616 mask |= 0xff0000;
12617 if (flags & 8)
12618 mask |= 0xff000000;
12619 return (a & mask) | (b & ~mask);
12620}
12621
aa633469
PM
12622/* CRC helpers.
12623 * The upper bytes of val (above the number specified by 'bytes') must have
12624 * been zeroed out by the caller.
12625 */
eb0ecd5a
WN
12626uint32_t HELPER(crc32)(uint32_t acc, uint32_t val, uint32_t bytes)
12627{
12628 uint8_t buf[4];
12629
aa633469 12630 stl_le_p(buf, val);
eb0ecd5a
WN
12631
12632 /* zlib crc32 converts the accumulator and output to one's complement. */
12633 return crc32(acc ^ 0xffffffff, buf, bytes) ^ 0xffffffff;
12634}
12635
12636uint32_t HELPER(crc32c)(uint32_t acc, uint32_t val, uint32_t bytes)
12637{
12638 uint8_t buf[4];
12639
aa633469 12640 stl_le_p(buf, val);
eb0ecd5a
WN
12641
12642 /* Linux crc32c converts the output to one's complement. */
12643 return crc32c(acc, buf, bytes) ^ 0xffffffff;
12644}
a9e01311
RH
12645
12646/* Return the exception level to which FP-disabled exceptions should
12647 * be taken, or 0 if FP is enabled.
12648 */
ced31551 12649int fp_exception_el(CPUARMState *env, int cur_el)
a9e01311 12650{
55faa212 12651#ifndef CONFIG_USER_ONLY
a9e01311
RH
12652 /* CPACR and the CPTR registers don't exist before v6, so FP is
12653 * always accessible
12654 */
12655 if (!arm_feature(env, ARM_FEATURE_V6)) {
12656 return 0;
12657 }
12658
d87513c0
PM
12659 if (arm_feature(env, ARM_FEATURE_M)) {
12660 /* CPACR can cause a NOCP UsageFault taken to current security state */
12661 if (!v7m_cpacr_pass(env, env->v7m.secure, cur_el != 0)) {
12662 return 1;
12663 }
12664
12665 if (arm_feature(env, ARM_FEATURE_M_SECURITY) && !env->v7m.secure) {
12666 if (!extract32(env->v7m.nsacr, 10, 1)) {
12667 /* FP insns cause a NOCP UsageFault taken to Secure */
12668 return 3;
12669 }
12670 }
12671
12672 return 0;
12673 }
12674
a9e01311
RH
12675 /* The CPACR controls traps to EL1, or PL1 if we're 32 bit:
12676 * 0, 2 : trap EL0 and EL1/PL1 accesses
12677 * 1 : trap only EL0 accesses
12678 * 3 : trap no accesses
c2ddb7cf 12679 * This register is ignored if E2H+TGE are both set.
a9e01311 12680 */
c2ddb7cf
RH
12681 if ((arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
12682 int fpen = extract32(env->cp15.cpacr_el1, 20, 2);
12683
12684 switch (fpen) {
12685 case 0:
12686 case 2:
12687 if (cur_el == 0 || cur_el == 1) {
12688 /* Trap to PL1, which might be EL1 or EL3 */
12689 if (arm_is_secure(env) && !arm_el_is_aa64(env, 3)) {
12690 return 3;
12691 }
12692 return 1;
12693 }
12694 if (cur_el == 3 && !is_a64(env)) {
12695 /* Secure PL1 running at EL3 */
a9e01311
RH
12696 return 3;
12697 }
c2ddb7cf
RH
12698 break;
12699 case 1:
12700 if (cur_el == 0) {
12701 return 1;
12702 }
12703 break;
12704 case 3:
12705 break;
a9e01311 12706 }
a9e01311
RH
12707 }
12708
fc1120a7
PM
12709 /*
12710 * The NSACR allows A-profile AArch32 EL3 and M-profile secure mode
12711 * to control non-secure access to the FPU. It doesn't have any
12712 * effect if EL3 is AArch64 or if EL3 doesn't exist at all.
12713 */
12714 if ((arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
12715 cur_el <= 2 && !arm_is_secure_below_el3(env))) {
12716 if (!extract32(env->cp15.nsacr, 10, 1)) {
12717 /* FP insns act as UNDEF */
12718 return cur_el == 2 ? 2 : 1;
12719 }
12720 }
12721
a9e01311
RH
12722 /* For the CPTR registers we don't need to guard with an ARM_FEATURE
12723 * check because zero bits in the registers mean "don't trap".
12724 */
12725
12726 /* CPTR_EL2 : present in v7VE or v8 */
12727 if (cur_el <= 2 && extract32(env->cp15.cptr_el[2], 10, 1)
e6ef0169 12728 && arm_is_el2_enabled(env)) {
a9e01311
RH
12729 /* Trap FP ops at EL2, NS-EL1 or NS-EL0 to EL2 */
12730 return 2;
12731 }
12732
12733 /* CPTR_EL3 : present in v8 */
12734 if (extract32(env->cp15.cptr_el[3], 10, 1)) {
12735 /* Trap all FP ops to EL3 */
12736 return 3;
12737 }
55faa212 12738#endif
a9e01311
RH
12739 return 0;
12740}
12741
b9f6033c
RH
12742/* Return the exception level we're running at if this is our mmu_idx */
12743int arm_mmu_idx_to_el(ARMMMUIdx mmu_idx)
12744{
12745 if (mmu_idx & ARM_MMU_IDX_M) {
12746 return mmu_idx & ARM_MMU_IDX_M_PRIV;
12747 }
12748
12749 switch (mmu_idx) {
12750 case ARMMMUIdx_E10_0:
12751 case ARMMMUIdx_E20_0:
12752 case ARMMMUIdx_SE10_0:
b6ad6062 12753 case ARMMMUIdx_SE20_0:
b9f6033c
RH
12754 return 0;
12755 case ARMMMUIdx_E10_1:
452ef8cb 12756 case ARMMMUIdx_E10_1_PAN:
b9f6033c 12757 case ARMMMUIdx_SE10_1:
452ef8cb 12758 case ARMMMUIdx_SE10_1_PAN:
b9f6033c
RH
12759 return 1;
12760 case ARMMMUIdx_E2:
12761 case ARMMMUIdx_E20_2:
452ef8cb 12762 case ARMMMUIdx_E20_2_PAN:
b6ad6062
RDC
12763 case ARMMMUIdx_SE2:
12764 case ARMMMUIdx_SE20_2:
12765 case ARMMMUIdx_SE20_2_PAN:
b9f6033c
RH
12766 return 2;
12767 case ARMMMUIdx_SE3:
12768 return 3;
12769 default:
12770 g_assert_not_reached();
12771 }
12772}
12773
7aab5a8c 12774#ifndef CONFIG_TCG
65e4655c
RH
12775ARMMMUIdx arm_v7m_mmu_idx_for_secstate(CPUARMState *env, bool secstate)
12776{
7aab5a8c 12777 g_assert_not_reached();
65e4655c 12778}
7aab5a8c 12779#endif
65e4655c 12780
164690b2 12781ARMMMUIdx arm_mmu_idx_el(CPUARMState *env, int el)
65e4655c 12782{
b6ad6062
RDC
12783 ARMMMUIdx idx;
12784 uint64_t hcr;
12785
65e4655c 12786 if (arm_feature(env, ARM_FEATURE_M)) {
50494a27 12787 return arm_v7m_mmu_idx_for_secstate(env, env->v7m.secure);
65e4655c
RH
12788 }
12789
6003d980 12790 /* See ARM pseudo-function ELIsInHost. */
b9f6033c
RH
12791 switch (el) {
12792 case 0:
b6ad6062
RDC
12793 hcr = arm_hcr_el2_eff(env);
12794 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
12795 idx = ARMMMUIdx_E20_0;
12796 } else {
12797 idx = ARMMMUIdx_E10_0;
6003d980 12798 }
b6ad6062 12799 break;
b9f6033c 12800 case 1:
66412260 12801 if (env->pstate & PSTATE_PAN) {
b6ad6062
RDC
12802 idx = ARMMMUIdx_E10_1_PAN;
12803 } else {
12804 idx = ARMMMUIdx_E10_1;
66412260 12805 }
b6ad6062 12806 break;
b9f6033c 12807 case 2:
6003d980 12808 /* Note that TGE does not apply at EL2. */
b6ad6062 12809 if (arm_hcr_el2_eff(env) & HCR_E2H) {
66412260 12810 if (env->pstate & PSTATE_PAN) {
b6ad6062
RDC
12811 idx = ARMMMUIdx_E20_2_PAN;
12812 } else {
12813 idx = ARMMMUIdx_E20_2;
66412260 12814 }
b6ad6062
RDC
12815 } else {
12816 idx = ARMMMUIdx_E2;
6003d980 12817 }
b6ad6062 12818 break;
b9f6033c
RH
12819 case 3:
12820 return ARMMMUIdx_SE3;
12821 default:
12822 g_assert_not_reached();
65e4655c 12823 }
b6ad6062
RDC
12824
12825 if (arm_is_secure_below_el3(env)) {
12826 idx &= ~ARM_MMU_IDX_A_NS;
12827 }
12828
12829 return idx;
50494a27
RH
12830}
12831
164690b2
RH
12832ARMMMUIdx arm_mmu_idx(CPUARMState *env)
12833{
12834 return arm_mmu_idx_el(env, arm_current_el(env));
12835}
12836
64be86ab
RH
12837#ifndef CONFIG_USER_ONLY
12838ARMMMUIdx arm_stage1_mmu_idx(CPUARMState *env)
12839{
12840 return stage_1_mmu_idx(arm_mmu_idx(env));
12841}
12842#endif
12843
fdd1b228
RH
12844static uint32_t rebuild_hflags_common(CPUARMState *env, int fp_el,
12845 ARMMMUIdx mmu_idx, uint32_t flags)
12846{
12847 flags = FIELD_DP32(flags, TBFLAG_ANY, FPEXC_EL, fp_el);
12848 flags = FIELD_DP32(flags, TBFLAG_ANY, MMUIDX,
12849 arm_to_core_mmu_idx(mmu_idx));
12850
fdd1b228
RH
12851 if (arm_singlestep_active(env)) {
12852 flags = FIELD_DP32(flags, TBFLAG_ANY, SS_ACTIVE, 1);
12853 }
12854 return flags;
12855}
12856
43eccfb6
RH
12857static uint32_t rebuild_hflags_common_32(CPUARMState *env, int fp_el,
12858 ARMMMUIdx mmu_idx, uint32_t flags)
12859{
8061a649
RH
12860 bool sctlr_b = arm_sctlr_b(env);
12861
12862 if (sctlr_b) {
12863 flags = FIELD_DP32(flags, TBFLAG_A32, SCTLR_B, 1);
12864 }
12865 if (arm_cpu_data_is_big_endian_a32(env, sctlr_b)) {
12866 flags = FIELD_DP32(flags, TBFLAG_ANY, BE_DATA, 1);
12867 }
43eccfb6
RH
12868 flags = FIELD_DP32(flags, TBFLAG_A32, NS, !access_secure_reg(env));
12869
12870 return rebuild_hflags_common(env, fp_el, mmu_idx, flags);
12871}
12872
6e33ced5
RH
12873static uint32_t rebuild_hflags_m32(CPUARMState *env, int fp_el,
12874 ARMMMUIdx mmu_idx)
12875{
12876 uint32_t flags = 0;
12877
12878 if (arm_v7m_is_handler_mode(env)) {
79cabf1f 12879 flags = FIELD_DP32(flags, TBFLAG_M32, HANDLER, 1);
6e33ced5
RH
12880 }
12881
12882 /*
12883 * v8M always applies stack limit checks unless CCR.STKOFHFNMIGN
12884 * is suppressing them because the requested execution priority
12885 * is less than 0.
12886 */
12887 if (arm_feature(env, ARM_FEATURE_V8) &&
12888 !((mmu_idx & ARM_MMU_IDX_M_NEGPRI) &&
12889 (env->v7m.ccr[env->v7m.secure] & R_V7M_CCR_STKOFHFNMIGN_MASK))) {
79cabf1f 12890 flags = FIELD_DP32(flags, TBFLAG_M32, STACKCHECK, 1);
6e33ced5
RH
12891 }
12892
12893 return rebuild_hflags_common_32(env, fp_el, mmu_idx, flags);
12894}
12895
83f4baef
RH
12896static uint32_t rebuild_hflags_aprofile(CPUARMState *env)
12897{
12898 int flags = 0;
12899
12900 flags = FIELD_DP32(flags, TBFLAG_ANY, DEBUG_TARGET_EL,
12901 arm_debug_target_el(env));
12902 return flags;
12903}
12904
c747224c
RH
12905static uint32_t rebuild_hflags_a32(CPUARMState *env, int fp_el,
12906 ARMMMUIdx mmu_idx)
12907{
83f4baef 12908 uint32_t flags = rebuild_hflags_aprofile(env);
0a54d68e
RH
12909
12910 if (arm_el_is_aa64(env, 1)) {
12911 flags = FIELD_DP32(flags, TBFLAG_A32, VFPEN, 1);
12912 }
5bb0a20b
MZ
12913
12914 if (arm_current_el(env) < 2 && env->cp15.hstr_el2 &&
12915 (arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
12916 flags = FIELD_DP32(flags, TBFLAG_A32, HSTR_ACTIVE, 1);
12917 }
12918
83f4baef 12919 return rebuild_hflags_common_32(env, fp_el, mmu_idx, flags);
c747224c
RH
12920}
12921
d4d7503a
RH
12922static uint32_t rebuild_hflags_a64(CPUARMState *env, int el, int fp_el,
12923 ARMMMUIdx mmu_idx)
a9e01311 12924{
83f4baef 12925 uint32_t flags = rebuild_hflags_aprofile(env);
d4d7503a 12926 ARMMMUIdx stage1 = stage_1_mmu_idx(mmu_idx);
b830a5ee 12927 uint64_t tcr = regime_tcr(env, mmu_idx)->raw_tcr;
d4d7503a
RH
12928 uint64_t sctlr;
12929 int tbii, tbid;
b9adaa70 12930
d4d7503a 12931 flags = FIELD_DP32(flags, TBFLAG_ANY, AARCH64_STATE, 1);
cd208a1c 12932
339370b9 12933 /* Get control bits for tagged addresses. */
b830a5ee
RH
12934 tbid = aa64_va_parameter_tbi(tcr, mmu_idx);
12935 tbii = tbid & ~aa64_va_parameter_tbid(tcr, mmu_idx);
5d8634f5 12936
d4d7503a
RH
12937 flags = FIELD_DP32(flags, TBFLAG_A64, TBII, tbii);
12938 flags = FIELD_DP32(flags, TBFLAG_A64, TBID, tbid);
12939
12940 if (cpu_isar_feature(aa64_sve, env_archcpu(env))) {
12941 int sve_el = sve_exception_el(env, el);
12942 uint32_t zcr_len;
5d8634f5 12943
d4d7503a
RH
12944 /*
12945 * If SVE is disabled, but FP is enabled,
12946 * then the effective len is 0.
12947 */
12948 if (sve_el != 0 && fp_el == 0) {
12949 zcr_len = 0;
12950 } else {
12951 zcr_len = sve_zcr_len_for_el(env, el);
5d8634f5 12952 }
d4d7503a
RH
12953 flags = FIELD_DP32(flags, TBFLAG_A64, SVEEXC_EL, sve_el);
12954 flags = FIELD_DP32(flags, TBFLAG_A64, ZCR_LEN, zcr_len);
12955 }
1db5e96c 12956
aaec1432 12957 sctlr = regime_sctlr(env, stage1);
1db5e96c 12958
8061a649
RH
12959 if (arm_cpu_data_is_big_endian_a64(el, sctlr)) {
12960 flags = FIELD_DP32(flags, TBFLAG_ANY, BE_DATA, 1);
12961 }
12962
d4d7503a
RH
12963 if (cpu_isar_feature(aa64_pauth, env_archcpu(env))) {
12964 /*
12965 * In order to save space in flags, we record only whether
12966 * pauth is "inactive", meaning all insns are implemented as
12967 * a nop, or "active" when some action must be performed.
12968 * The decision of which action to take is left to a helper.
12969 */
12970 if (sctlr & (SCTLR_EnIA | SCTLR_EnIB | SCTLR_EnDA | SCTLR_EnDB)) {
12971 flags = FIELD_DP32(flags, TBFLAG_A64, PAUTH_ACTIVE, 1);
1db5e96c 12972 }
d4d7503a 12973 }
0816ef1b 12974
d4d7503a
RH
12975 if (cpu_isar_feature(aa64_bti, env_archcpu(env))) {
12976 /* Note that SCTLR_EL[23].BT == SCTLR_BT1. */
12977 if (sctlr & (el == 0 ? SCTLR_BT0 : SCTLR_BT1)) {
12978 flags = FIELD_DP32(flags, TBFLAG_A64, BT, 1);
0816ef1b 12979 }
d4d7503a 12980 }
08f1434a 12981
cc28fc30 12982 /* Compute the condition for using AccType_UNPRIV for LDTR et al. */
7a8014ab
RH
12983 if (!(env->pstate & PSTATE_UAO)) {
12984 switch (mmu_idx) {
12985 case ARMMMUIdx_E10_1:
12986 case ARMMMUIdx_E10_1_PAN:
12987 case ARMMMUIdx_SE10_1:
12988 case ARMMMUIdx_SE10_1_PAN:
12989 /* TODO: ARMv8.3-NV */
cc28fc30 12990 flags = FIELD_DP32(flags, TBFLAG_A64, UNPRIV, 1);
7a8014ab
RH
12991 break;
12992 case ARMMMUIdx_E20_2:
12993 case ARMMMUIdx_E20_2_PAN:
b6ad6062
RDC
12994 case ARMMMUIdx_SE20_2:
12995 case ARMMMUIdx_SE20_2_PAN:
7a8014ab
RH
12996 /*
12997 * Note that EL20_2 is gated by HCR_EL2.E2H == 1, but EL20_0 is
12998 * gated by HCR_EL2.<E2H,TGE> == '11', and so is LDTR.
12999 */
13000 if (env->cp15.hcr_el2 & HCR_TGE) {
13001 flags = FIELD_DP32(flags, TBFLAG_A64, UNPRIV, 1);
13002 }
13003 break;
13004 default:
13005 break;
cc28fc30 13006 }
cc28fc30
RH
13007 }
13008
81ae05fa
RH
13009 if (cpu_isar_feature(aa64_mte, env_archcpu(env))) {
13010 /*
13011 * Set MTE_ACTIVE if any access may be Checked, and leave clear
13012 * if all accesses must be Unchecked:
13013 * 1) If no TBI, then there are no tags in the address to check,
13014 * 2) If Tag Check Override, then all accesses are Unchecked,
13015 * 3) If Tag Check Fail == 0, then Checked access have no effect,
13016 * 4) If no Allocation Tag Access, then all accesses are Unchecked.
13017 */
13018 if (allocation_tag_access_enabled(env, el, sctlr)) {
13019 flags = FIELD_DP32(flags, TBFLAG_A64, ATA, 1);
13020 if (tbid
13021 && !(env->pstate & PSTATE_TCO)
13022 && (sctlr & (el == 0 ? SCTLR_TCF0 : SCTLR_TCF))) {
13023 flags = FIELD_DP32(flags, TBFLAG_A64, MTE_ACTIVE, 1);
13024 }
13025 }
13026 /* And again for unprivileged accesses, if required. */
13027 if (FIELD_EX32(flags, TBFLAG_A64, UNPRIV)
13028 && tbid
13029 && !(env->pstate & PSTATE_TCO)
cc97b001 13030 && (sctlr & SCTLR_TCF)
81ae05fa
RH
13031 && allocation_tag_access_enabled(env, 0, sctlr)) {
13032 flags = FIELD_DP32(flags, TBFLAG_A64, MTE0_ACTIVE, 1);
13033 }
13034 /* Cache TCMA as well as TBI. */
13035 flags = FIELD_DP32(flags, TBFLAG_A64, TCMA,
13036 aa64_va_parameter_tcma(tcr, mmu_idx));
13037 }
13038
d4d7503a
RH
13039 return rebuild_hflags_common(env, fp_el, mmu_idx, flags);
13040}
13041
3d74e2e9
RH
13042static uint32_t rebuild_hflags_internal(CPUARMState *env)
13043{
13044 int el = arm_current_el(env);
13045 int fp_el = fp_exception_el(env, el);
164690b2 13046 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
3d74e2e9
RH
13047
13048 if (is_a64(env)) {
13049 return rebuild_hflags_a64(env, el, fp_el, mmu_idx);
13050 } else if (arm_feature(env, ARM_FEATURE_M)) {
13051 return rebuild_hflags_m32(env, fp_el, mmu_idx);
13052 } else {
13053 return rebuild_hflags_a32(env, fp_el, mmu_idx);
13054 }
13055}
13056
13057void arm_rebuild_hflags(CPUARMState *env)
13058{
13059 env->hflags = rebuild_hflags_internal(env);
13060}
13061
19717e9b
PM
13062/*
13063 * If we have triggered a EL state change we can't rely on the
13064 * translator having passed it to us, we need to recompute.
13065 */
13066void HELPER(rebuild_hflags_m32_newel)(CPUARMState *env)
13067{
13068 int el = arm_current_el(env);
13069 int fp_el = fp_exception_el(env, el);
13070 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
13071 env->hflags = rebuild_hflags_m32(env, fp_el, mmu_idx);
13072}
13073
14f3c588
RH
13074void HELPER(rebuild_hflags_m32)(CPUARMState *env, int el)
13075{
13076 int fp_el = fp_exception_el(env, el);
13077 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
13078
13079 env->hflags = rebuild_hflags_m32(env, fp_el, mmu_idx);
13080}
13081
f80741d1
AB
13082/*
13083 * If we have triggered a EL state change we can't rely on the
563152e0 13084 * translator having passed it to us, we need to recompute.
f80741d1
AB
13085 */
13086void HELPER(rebuild_hflags_a32_newel)(CPUARMState *env)
13087{
13088 int el = arm_current_el(env);
13089 int fp_el = fp_exception_el(env, el);
13090 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
13091 env->hflags = rebuild_hflags_a32(env, fp_el, mmu_idx);
13092}
13093
14f3c588
RH
13094void HELPER(rebuild_hflags_a32)(CPUARMState *env, int el)
13095{
13096 int fp_el = fp_exception_el(env, el);
13097 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
13098
13099 env->hflags = rebuild_hflags_a32(env, fp_el, mmu_idx);
13100}
13101
13102void HELPER(rebuild_hflags_a64)(CPUARMState *env, int el)
13103{
13104 int fp_el = fp_exception_el(env, el);
13105 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
13106
13107 env->hflags = rebuild_hflags_a64(env, el, fp_el, mmu_idx);
13108}
13109
0ee8b24a
PMD
13110static inline void assert_hflags_rebuild_correctly(CPUARMState *env)
13111{
13112#ifdef CONFIG_DEBUG_TCG
13113 uint32_t env_flags_current = env->hflags;
13114 uint32_t env_flags_rebuilt = rebuild_hflags_internal(env);
13115
13116 if (unlikely(env_flags_current != env_flags_rebuilt)) {
13117 fprintf(stderr, "TCG hflags mismatch (current:0x%08x rebuilt:0x%08x)\n",
13118 env_flags_current, env_flags_rebuilt);
13119 abort();
13120 }
13121#endif
13122}
13123
d4d7503a
RH
13124void cpu_get_tb_cpu_state(CPUARMState *env, target_ulong *pc,
13125 target_ulong *cs_base, uint32_t *pflags)
13126{
e979972a
RH
13127 uint32_t flags = env->hflags;
13128 uint32_t pstate_for_ss;
d4d7503a 13129
9b253fe5 13130 *cs_base = 0;
0ee8b24a 13131 assert_hflags_rebuild_correctly(env);
3d74e2e9 13132
e979972a 13133 if (FIELD_EX32(flags, TBFLAG_ANY, AARCH64_STATE)) {
d4d7503a 13134 *pc = env->pc;
d4d7503a 13135 if (cpu_isar_feature(aa64_bti, env_archcpu(env))) {
08f1434a
RH
13136 flags = FIELD_DP32(flags, TBFLAG_A64, BTYPE, env->btype);
13137 }
60e12c37 13138 pstate_for_ss = env->pstate;
a9e01311
RH
13139 } else {
13140 *pc = env->regs[15];
6e33ced5
RH
13141
13142 if (arm_feature(env, ARM_FEATURE_M)) {
9550d1bd
RH
13143 if (arm_feature(env, ARM_FEATURE_M_SECURITY) &&
13144 FIELD_EX32(env->v7m.fpccr[M_REG_S], V7M_FPCCR, S)
13145 != env->v7m.secure) {
79cabf1f 13146 flags = FIELD_DP32(flags, TBFLAG_M32, FPCCR_S_WRONG, 1);
9550d1bd
RH
13147 }
13148
13149 if ((env->v7m.fpccr[env->v7m.secure] & R_V7M_FPCCR_ASPEN_MASK) &&
13150 (!(env->v7m.control[M_REG_S] & R_V7M_CONTROL_FPCA_MASK) ||
13151 (env->v7m.secure &&
13152 !(env->v7m.control[M_REG_S] & R_V7M_CONTROL_SFPA_MASK)))) {
13153 /*
13154 * ASPEN is set, but FPCA/SFPA indicate that there is no
13155 * active FP context; we must create a new FP context before
13156 * executing any FP insn.
13157 */
79cabf1f 13158 flags = FIELD_DP32(flags, TBFLAG_M32, NEW_FP_CTXT_NEEDED, 1);
9550d1bd
RH
13159 }
13160
13161 bool is_secure = env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_S_MASK;
13162 if (env->v7m.fpccr[is_secure] & R_V7M_FPCCR_LSPACT_MASK) {
79cabf1f 13163 flags = FIELD_DP32(flags, TBFLAG_M32, LSPACT, 1);
9550d1bd 13164 }
6e33ced5 13165 } else {
bbad7c62
RH
13166 /*
13167 * Note that XSCALE_CPAR shares bits with VECSTRIDE.
13168 * Note that VECLEN+VECSTRIDE are RES0 for M-profile.
13169 */
13170 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
13171 flags = FIELD_DP32(flags, TBFLAG_A32,
13172 XSCALE_CPAR, env->cp15.c15_cpar);
13173 } else {
13174 flags = FIELD_DP32(flags, TBFLAG_A32, VECLEN,
13175 env->vfp.vec_len);
13176 flags = FIELD_DP32(flags, TBFLAG_A32, VECSTRIDE,
13177 env->vfp.vec_stride);
13178 }
0a54d68e
RH
13179 if (env->vfp.xregs[ARM_VFP_FPEXC] & (1 << 30)) {
13180 flags = FIELD_DP32(flags, TBFLAG_A32, VFPEN, 1);
13181 }
6e33ced5
RH
13182 }
13183
79cabf1f
RH
13184 flags = FIELD_DP32(flags, TBFLAG_AM32, THUMB, env->thumb);
13185 flags = FIELD_DP32(flags, TBFLAG_AM32, CONDEXEC, env->condexec_bits);
60e12c37 13186 pstate_for_ss = env->uncached_cpsr;
d4d7503a 13187 }
a9e01311 13188
60e12c37
RH
13189 /*
13190 * The SS_ACTIVE and PSTATE_SS bits correspond to the state machine
a9e01311
RH
13191 * states defined in the ARM ARM for software singlestep:
13192 * SS_ACTIVE PSTATE.SS State
13193 * 0 x Inactive (the TB flag for SS is always 0)
13194 * 1 0 Active-pending
13195 * 1 1 Active-not-pending
fdd1b228 13196 * SS_ACTIVE is set in hflags; PSTATE_SS is computed every TB.
a9e01311 13197 */
60e12c37
RH
13198 if (FIELD_EX32(flags, TBFLAG_ANY, SS_ACTIVE) &&
13199 (pstate_for_ss & PSTATE_SS)) {
13200 flags = FIELD_DP32(flags, TBFLAG_ANY, PSTATE_SS, 1);
a9e01311 13201 }
a9e01311 13202
b9adaa70 13203 *pflags = flags;
a9e01311 13204}
0ab5953b
RH
13205
13206#ifdef TARGET_AARCH64
13207/*
13208 * The manual says that when SVE is enabled and VQ is widened the
13209 * implementation is allowed to zero the previously inaccessible
13210 * portion of the registers. The corollary to that is that when
13211 * SVE is enabled and VQ is narrowed we are also allowed to zero
13212 * the now inaccessible portion of the registers.
13213 *
13214 * The intent of this is that no predicate bit beyond VQ is ever set.
13215 * Which means that some operations on predicate registers themselves
13216 * may operate on full uint64_t or even unrolled across the maximum
13217 * uint64_t[4]. Performing 4 bits of host arithmetic unconditionally
13218 * may well be cheaper than conditionals to restrict the operation
13219 * to the relevant portion of a uint16_t[16].
13220 */
13221void aarch64_sve_narrow_vq(CPUARMState *env, unsigned vq)
13222{
13223 int i, j;
13224 uint64_t pmask;
13225
13226 assert(vq >= 1 && vq <= ARM_MAX_VQ);
2fc0cc0e 13227 assert(vq <= env_archcpu(env)->sve_max_vq);
0ab5953b
RH
13228
13229 /* Zap the high bits of the zregs. */
13230 for (i = 0; i < 32; i++) {
13231 memset(&env->vfp.zregs[i].d[2 * vq], 0, 16 * (ARM_MAX_VQ - vq));
13232 }
13233
13234 /* Zap the high bits of the pregs and ffr. */
13235 pmask = 0;
13236 if (vq & 3) {
13237 pmask = ~(-1ULL << (16 * (vq & 3)));
13238 }
13239 for (j = vq / 4; j < ARM_MAX_VQ / 4; j++) {
13240 for (i = 0; i < 17; ++i) {
13241 env->vfp.pregs[i].p[j] &= pmask;
13242 }
13243 pmask = 0;
13244 }
13245}
13246
13247/*
13248 * Notice a change in SVE vector size when changing EL.
13249 */
9a05f7b6
RH
13250void aarch64_sve_change_el(CPUARMState *env, int old_el,
13251 int new_el, bool el0_a64)
0ab5953b 13252{
2fc0cc0e 13253 ARMCPU *cpu = env_archcpu(env);
0ab5953b 13254 int old_len, new_len;
9a05f7b6 13255 bool old_a64, new_a64;
0ab5953b
RH
13256
13257 /* Nothing to do if no SVE. */
cd208a1c 13258 if (!cpu_isar_feature(aa64_sve, cpu)) {
0ab5953b
RH
13259 return;
13260 }
13261
13262 /* Nothing to do if FP is disabled in either EL. */
13263 if (fp_exception_el(env, old_el) || fp_exception_el(env, new_el)) {
13264 return;
13265 }
13266
13267 /*
13268 * DDI0584A.d sec 3.2: "If SVE instructions are disabled or trapped
13269 * at ELx, or not available because the EL is in AArch32 state, then
13270 * for all purposes other than a direct read, the ZCR_ELx.LEN field
13271 * has an effective value of 0".
13272 *
13273 * Consider EL2 (aa64, vq=4) -> EL0 (aa32) -> EL1 (aa64, vq=0).
13274 * If we ignore aa32 state, we would fail to see the vq4->vq0 transition
13275 * from EL2->EL1. Thus we go ahead and narrow when entering aa32 so that
13276 * we already have the correct register contents when encountering the
13277 * vq0->vq0 transition between EL0->EL1.
13278 */
9a05f7b6
RH
13279 old_a64 = old_el ? arm_el_is_aa64(env, old_el) : el0_a64;
13280 old_len = (old_a64 && !sve_exception_el(env, old_el)
0ab5953b 13281 ? sve_zcr_len_for_el(env, old_el) : 0);
9a05f7b6
RH
13282 new_a64 = new_el ? arm_el_is_aa64(env, new_el) : el0_a64;
13283 new_len = (new_a64 && !sve_exception_el(env, new_el)
0ab5953b
RH
13284 ? sve_zcr_len_for_el(env, new_el) : 0);
13285
13286 /* When changing vector length, clear inaccessible state. */
13287 if (new_len < old_len) {
13288 aarch64_sve_narrow_vq(env, new_len + 1);
13289 }
13290}
13291#endif
This page took 4.228649 seconds and 4 git commands to generate.