]> git.proxmox.com Git - mirror_qemu.git/blame - target/arm/helper.c
target/arm: Add MTE bits to tb_flags
[mirror_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"
f3a9b694 27#include "sysemu/kvm.h"
2a609df8 28#include "sysemu/tcg.h"
9d2b5a58 29#include "qemu/range.h"
7f7b4e7a 30#include "qapi/qapi-commands-machine-target.h"
de390645
RH
31#include "qapi/error.h"
32#include "qemu/guest-random.h"
91f78c58
PMD
33#ifdef CONFIG_TCG
34#include "arm_ldst.h"
7aab5a8c 35#include "exec/cpu_ldst.h"
91f78c58 36#endif
0b03bdfc 37
352c98e5
LV
38#define ARM_CPU_FREQ 1000000000 /* FIXME: 1 GHz, should be configurable */
39
4a501606 40#ifndef CONFIG_USER_ONLY
7c2cb42b 41
37785977 42static bool get_phys_addr_lpae(CPUARMState *env, target_ulong address,
03ae85f8 43 MMUAccessType access_type, ARMMMUIdx mmu_idx,
ff7de2fc 44 bool s1_is_el0,
37785977 45 hwaddr *phys_ptr, MemTxAttrs *txattrs, int *prot,
da909b2c 46 target_ulong *page_size_ptr,
5b2d261d 47 ARMMMUFaultInfo *fi, ARMCacheAttrs *cacheattrs);
4a501606
PM
48#endif
49
affdb64d
PM
50static void switch_mode(CPUARMState *env, int mode);
51
a010bdbe 52static int vfp_gdb_get_reg(CPUARMState *env, GByteArray *buf, int reg)
56aebc89 53{
a6627f5f
RH
54 ARMCPU *cpu = env_archcpu(env);
55 int nregs = cpu_isar_feature(aa32_simd_r32, cpu) ? 32 : 16;
56aebc89
PB
56
57 /* VFP data registers are always little-endian. */
56aebc89 58 if (reg < nregs) {
a010bdbe 59 return gdb_get_reg64(buf, *aa32_vfp_dreg(env, reg));
56aebc89
PB
60 }
61 if (arm_feature(env, ARM_FEATURE_NEON)) {
62 /* Aliases for Q regs. */
63 nregs += 16;
64 if (reg < nregs) {
9a2b5256 65 uint64_t *q = aa32_vfp_qreg(env, reg - 32);
a010bdbe 66 return gdb_get_reg128(buf, q[0], q[1]);
56aebc89
PB
67 }
68 }
69 switch (reg - nregs) {
a010bdbe
AB
70 case 0: return gdb_get_reg32(buf, env->vfp.xregs[ARM_VFP_FPSID]); break;
71 case 1: return gdb_get_reg32(buf, vfp_get_fpscr(env)); break;
72 case 2: return gdb_get_reg32(buf, env->vfp.xregs[ARM_VFP_FPEXC]); break;
56aebc89
PB
73 }
74 return 0;
75}
76
0ecb72a5 77static int vfp_gdb_set_reg(CPUARMState *env, uint8_t *buf, int reg)
56aebc89 78{
a6627f5f
RH
79 ARMCPU *cpu = env_archcpu(env);
80 int nregs = cpu_isar_feature(aa32_simd_r32, cpu) ? 32 : 16;
56aebc89 81
56aebc89 82 if (reg < nregs) {
9a2b5256 83 *aa32_vfp_dreg(env, reg) = ldq_le_p(buf);
56aebc89
PB
84 return 8;
85 }
86 if (arm_feature(env, ARM_FEATURE_NEON)) {
87 nregs += 16;
88 if (reg < nregs) {
9a2b5256
RH
89 uint64_t *q = aa32_vfp_qreg(env, reg - 32);
90 q[0] = ldq_le_p(buf);
91 q[1] = ldq_le_p(buf + 8);
56aebc89
PB
92 return 16;
93 }
94 }
95 switch (reg - nregs) {
96 case 0: env->vfp.xregs[ARM_VFP_FPSID] = ldl_p(buf); return 4;
b0a909a4 97 case 1: vfp_set_fpscr(env, ldl_p(buf)); return 4;
71b3c3de 98 case 2: env->vfp.xregs[ARM_VFP_FPEXC] = ldl_p(buf) & (1 << 30); return 4;
56aebc89
PB
99 }
100 return 0;
101}
102
a010bdbe 103static int aarch64_fpu_gdb_get_reg(CPUARMState *env, GByteArray *buf, int reg)
6a669427
PM
104{
105 switch (reg) {
106 case 0 ... 31:
8b1ca58c
AB
107 {
108 /* 128 bit FP register - quads are in LE order */
109 uint64_t *q = aa64_vfp_qreg(env, reg);
110 return gdb_get_reg128(buf, q[1], q[0]);
111 }
6a669427
PM
112 case 32:
113 /* FPSR */
8b1ca58c 114 return gdb_get_reg32(buf, vfp_get_fpsr(env));
6a669427
PM
115 case 33:
116 /* FPCR */
8b1ca58c 117 return gdb_get_reg32(buf,vfp_get_fpcr(env));
6a669427
PM
118 default:
119 return 0;
120 }
121}
122
123static int aarch64_fpu_gdb_set_reg(CPUARMState *env, uint8_t *buf, int reg)
124{
125 switch (reg) {
126 case 0 ... 31:
127 /* 128 bit FP register */
9a2b5256
RH
128 {
129 uint64_t *q = aa64_vfp_qreg(env, reg);
130 q[0] = ldq_le_p(buf);
131 q[1] = ldq_le_p(buf + 8);
132 return 16;
133 }
6a669427
PM
134 case 32:
135 /* FPSR */
136 vfp_set_fpsr(env, ldl_p(buf));
137 return 4;
138 case 33:
139 /* FPCR */
140 vfp_set_fpcr(env, ldl_p(buf));
141 return 4;
142 default:
143 return 0;
144 }
145}
146
c4241c7d 147static uint64_t raw_read(CPUARMState *env, const ARMCPRegInfo *ri)
d4e6df63 148{
375421cc 149 assert(ri->fieldoffset);
67ed771d 150 if (cpreg_field_is_64bit(ri)) {
c4241c7d 151 return CPREG_FIELD64(env, ri);
22d9e1a9 152 } else {
c4241c7d 153 return CPREG_FIELD32(env, ri);
22d9e1a9 154 }
d4e6df63
PM
155}
156
c4241c7d
PM
157static void raw_write(CPUARMState *env, const ARMCPRegInfo *ri,
158 uint64_t value)
d4e6df63 159{
375421cc 160 assert(ri->fieldoffset);
67ed771d 161 if (cpreg_field_is_64bit(ri)) {
22d9e1a9
PM
162 CPREG_FIELD64(env, ri) = value;
163 } else {
164 CPREG_FIELD32(env, ri) = value;
165 }
d4e6df63
PM
166}
167
11f136ee
FA
168static void *raw_ptr(CPUARMState *env, const ARMCPRegInfo *ri)
169{
170 return (char *)env + ri->fieldoffset;
171}
172
49a66191 173uint64_t read_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri)
721fae12 174{
59a1c327 175 /* Raw read of a coprocessor register (as needed for migration, etc). */
721fae12 176 if (ri->type & ARM_CP_CONST) {
59a1c327 177 return ri->resetvalue;
721fae12 178 } else if (ri->raw_readfn) {
59a1c327 179 return ri->raw_readfn(env, ri);
721fae12 180 } else if (ri->readfn) {
59a1c327 181 return ri->readfn(env, ri);
721fae12 182 } else {
59a1c327 183 return raw_read(env, ri);
721fae12 184 }
721fae12
PM
185}
186
59a1c327 187static void write_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri,
7900e9f1 188 uint64_t v)
721fae12
PM
189{
190 /* Raw write of a coprocessor register (as needed for migration, etc).
721fae12
PM
191 * Note that constant registers are treated as write-ignored; the
192 * caller should check for success by whether a readback gives the
193 * value written.
194 */
195 if (ri->type & ARM_CP_CONST) {
59a1c327 196 return;
721fae12 197 } else if (ri->raw_writefn) {
c4241c7d 198 ri->raw_writefn(env, ri, v);
721fae12 199 } else if (ri->writefn) {
c4241c7d 200 ri->writefn(env, ri, v);
721fae12 201 } else {
afb2530f 202 raw_write(env, ri, v);
721fae12 203 }
721fae12
PM
204}
205
d12379c5
AB
206/**
207 * arm_get/set_gdb_*: get/set a gdb register
208 * @env: the CPU state
209 * @buf: a buffer to copy to/from
210 * @reg: register number (offset from start of group)
211 *
212 * We return the number of bytes copied
213 */
214
a010bdbe 215static int arm_gdb_get_sysreg(CPUARMState *env, GByteArray *buf, int reg)
200bf5b7 216{
2fc0cc0e 217 ARMCPU *cpu = env_archcpu(env);
200bf5b7
AB
218 const ARMCPRegInfo *ri;
219 uint32_t key;
220
448d4d14 221 key = cpu->dyn_sysreg_xml.data.cpregs.keys[reg];
200bf5b7
AB
222 ri = get_arm_cp_reginfo(cpu->cp_regs, key);
223 if (ri) {
224 if (cpreg_field_is_64bit(ri)) {
225 return gdb_get_reg64(buf, (uint64_t)read_raw_cp_reg(env, ri));
226 } else {
227 return gdb_get_reg32(buf, (uint32_t)read_raw_cp_reg(env, ri));
228 }
229 }
230 return 0;
231}
232
233static int arm_gdb_set_sysreg(CPUARMState *env, uint8_t *buf, int reg)
234{
235 return 0;
236}
237
d12379c5
AB
238#ifdef TARGET_AARCH64
239static int arm_gdb_get_svereg(CPUARMState *env, GByteArray *buf, int reg)
240{
241 ARMCPU *cpu = env_archcpu(env);
242
243 switch (reg) {
244 /* The first 32 registers are the zregs */
245 case 0 ... 31:
246 {
247 int vq, len = 0;
248 for (vq = 0; vq < cpu->sve_max_vq; vq++) {
249 len += gdb_get_reg128(buf,
250 env->vfp.zregs[reg].d[vq * 2 + 1],
251 env->vfp.zregs[reg].d[vq * 2]);
252 }
253 return len;
254 }
255 case 32:
256 return gdb_get_reg32(buf, vfp_get_fpsr(env));
257 case 33:
258 return gdb_get_reg32(buf, vfp_get_fpcr(env));
259 /* then 16 predicates and the ffr */
260 case 34 ... 50:
261 {
262 int preg = reg - 34;
263 int vq, len = 0;
264 for (vq = 0; vq < cpu->sve_max_vq; vq = vq + 4) {
265 len += gdb_get_reg64(buf, env->vfp.pregs[preg].p[vq / 4]);
266 }
267 return len;
268 }
269 case 51:
270 {
271 /*
272 * We report in Vector Granules (VG) which is 64bit in a Z reg
273 * while the ZCR works in Vector Quads (VQ) which is 128bit chunks.
274 */
275 int vq = sve_zcr_len_for_el(env, arm_current_el(env)) + 1;
276 return gdb_get_reg32(buf, vq * 2);
277 }
278 default:
279 /* gdbstub asked for something out our range */
280 qemu_log_mask(LOG_UNIMP, "%s: out of range register %d", __func__, reg);
281 break;
282 }
283
284 return 0;
285}
286
287static int arm_gdb_set_svereg(CPUARMState *env, uint8_t *buf, int reg)
288{
289 ARMCPU *cpu = env_archcpu(env);
290
291 /* The first 32 registers are the zregs */
292 switch (reg) {
293 /* The first 32 registers are the zregs */
294 case 0 ... 31:
295 {
296 int vq, len = 0;
297 uint64_t *p = (uint64_t *) buf;
298 for (vq = 0; vq < cpu->sve_max_vq; vq++) {
299 env->vfp.zregs[reg].d[vq * 2 + 1] = *p++;
300 env->vfp.zregs[reg].d[vq * 2] = *p++;
301 len += 16;
302 }
303 return len;
304 }
305 case 32:
306 vfp_set_fpsr(env, *(uint32_t *)buf);
307 return 4;
308 case 33:
309 vfp_set_fpcr(env, *(uint32_t *)buf);
310 return 4;
311 case 34 ... 50:
312 {
313 int preg = reg - 34;
314 int vq, len = 0;
315 uint64_t *p = (uint64_t *) buf;
316 for (vq = 0; vq < cpu->sve_max_vq; vq = vq + 4) {
317 env->vfp.pregs[preg].p[vq / 4] = *p++;
318 len += 8;
319 }
320 return len;
321 }
322 case 51:
323 /* cannot set vg via gdbstub */
324 return 0;
325 default:
326 /* gdbstub asked for something out our range */
327 break;
328 }
329
330 return 0;
331}
332#endif /* TARGET_AARCH64 */
333
375421cc
PM
334static bool raw_accessors_invalid(const ARMCPRegInfo *ri)
335{
336 /* Return true if the regdef would cause an assertion if you called
337 * read_raw_cp_reg() or write_raw_cp_reg() on it (ie if it is a
338 * program bug for it not to have the NO_RAW flag).
339 * NB that returning false here doesn't necessarily mean that calling
340 * read/write_raw_cp_reg() is safe, because we can't distinguish "has
341 * read/write access functions which are safe for raw use" from "has
342 * read/write access functions which have side effects but has forgotten
343 * to provide raw access functions".
344 * The tests here line up with the conditions in read/write_raw_cp_reg()
345 * and assertions in raw_read()/raw_write().
346 */
347 if ((ri->type & ARM_CP_CONST) ||
348 ri->fieldoffset ||
349 ((ri->raw_writefn || ri->writefn) && (ri->raw_readfn || ri->readfn))) {
350 return false;
351 }
352 return true;
353}
354
b698e4ee 355bool write_cpustate_to_list(ARMCPU *cpu, bool kvm_sync)
721fae12
PM
356{
357 /* Write the coprocessor state from cpu->env to the (index,value) list. */
358 int i;
359 bool ok = true;
360
361 for (i = 0; i < cpu->cpreg_array_len; i++) {
362 uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
363 const ARMCPRegInfo *ri;
b698e4ee 364 uint64_t newval;
59a1c327 365
60322b39 366 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
721fae12
PM
367 if (!ri) {
368 ok = false;
369 continue;
370 }
7a0e58fa 371 if (ri->type & ARM_CP_NO_RAW) {
721fae12
PM
372 continue;
373 }
b698e4ee
PM
374
375 newval = read_raw_cp_reg(&cpu->env, ri);
376 if (kvm_sync) {
377 /*
378 * Only sync if the previous list->cpustate sync succeeded.
379 * Rather than tracking the success/failure state for every
380 * item in the list, we just recheck "does the raw write we must
381 * have made in write_list_to_cpustate() read back OK" here.
382 */
383 uint64_t oldval = cpu->cpreg_values[i];
384
385 if (oldval == newval) {
386 continue;
387 }
388
389 write_raw_cp_reg(&cpu->env, ri, oldval);
390 if (read_raw_cp_reg(&cpu->env, ri) != oldval) {
391 continue;
392 }
393
394 write_raw_cp_reg(&cpu->env, ri, newval);
395 }
396 cpu->cpreg_values[i] = newval;
721fae12
PM
397 }
398 return ok;
399}
400
401bool write_list_to_cpustate(ARMCPU *cpu)
402{
403 int i;
404 bool ok = true;
405
406 for (i = 0; i < cpu->cpreg_array_len; i++) {
407 uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
408 uint64_t v = cpu->cpreg_values[i];
721fae12
PM
409 const ARMCPRegInfo *ri;
410
60322b39 411 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
721fae12
PM
412 if (!ri) {
413 ok = false;
414 continue;
415 }
7a0e58fa 416 if (ri->type & ARM_CP_NO_RAW) {
721fae12
PM
417 continue;
418 }
419 /* Write value and confirm it reads back as written
420 * (to catch read-only registers and partially read-only
421 * registers where the incoming migration value doesn't match)
422 */
59a1c327
PM
423 write_raw_cp_reg(&cpu->env, ri, v);
424 if (read_raw_cp_reg(&cpu->env, ri) != v) {
721fae12
PM
425 ok = false;
426 }
427 }
428 return ok;
429}
430
431static void add_cpreg_to_list(gpointer key, gpointer opaque)
432{
433 ARMCPU *cpu = opaque;
434 uint64_t regidx;
435 const ARMCPRegInfo *ri;
436
437 regidx = *(uint32_t *)key;
60322b39 438 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
721fae12 439
7a0e58fa 440 if (!(ri->type & (ARM_CP_NO_RAW|ARM_CP_ALIAS))) {
721fae12
PM
441 cpu->cpreg_indexes[cpu->cpreg_array_len] = cpreg_to_kvm_id(regidx);
442 /* The value array need not be initialized at this point */
443 cpu->cpreg_array_len++;
444 }
445}
446
447static void count_cpreg(gpointer key, gpointer opaque)
448{
449 ARMCPU *cpu = opaque;
450 uint64_t regidx;
451 const ARMCPRegInfo *ri;
452
453 regidx = *(uint32_t *)key;
60322b39 454 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
721fae12 455
7a0e58fa 456 if (!(ri->type & (ARM_CP_NO_RAW|ARM_CP_ALIAS))) {
721fae12
PM
457 cpu->cpreg_array_len++;
458 }
459}
460
461static gint cpreg_key_compare(gconstpointer a, gconstpointer b)
462{
cbf239b7
AR
463 uint64_t aidx = cpreg_to_kvm_id(*(uint32_t *)a);
464 uint64_t bidx = cpreg_to_kvm_id(*(uint32_t *)b);
721fae12 465
cbf239b7
AR
466 if (aidx > bidx) {
467 return 1;
468 }
469 if (aidx < bidx) {
470 return -1;
471 }
472 return 0;
721fae12
PM
473}
474
475void init_cpreg_list(ARMCPU *cpu)
476{
477 /* Initialise the cpreg_tuples[] array based on the cp_regs hash.
478 * Note that we require cpreg_tuples[] to be sorted by key ID.
479 */
57b6d95e 480 GList *keys;
721fae12
PM
481 int arraylen;
482
57b6d95e 483 keys = g_hash_table_get_keys(cpu->cp_regs);
721fae12
PM
484 keys = g_list_sort(keys, cpreg_key_compare);
485
486 cpu->cpreg_array_len = 0;
487
488 g_list_foreach(keys, count_cpreg, cpu);
489
490 arraylen = cpu->cpreg_array_len;
491 cpu->cpreg_indexes = g_new(uint64_t, arraylen);
492 cpu->cpreg_values = g_new(uint64_t, arraylen);
493 cpu->cpreg_vmstate_indexes = g_new(uint64_t, arraylen);
494 cpu->cpreg_vmstate_values = g_new(uint64_t, arraylen);
495 cpu->cpreg_vmstate_array_len = cpu->cpreg_array_len;
496 cpu->cpreg_array_len = 0;
497
498 g_list_foreach(keys, add_cpreg_to_list, cpu);
499
500 assert(cpu->cpreg_array_len == arraylen);
501
502 g_list_free(keys);
503}
504
68e9c2fe 505/*
93dd1e61 506 * Some registers are not accessible from AArch32 EL3 if SCR.NS == 0.
68e9c2fe
EI
507 */
508static CPAccessResult access_el3_aa32ns(CPUARMState *env,
3f208fd7
PM
509 const ARMCPRegInfo *ri,
510 bool isread)
68e9c2fe 511{
93dd1e61
EI
512 if (!is_a64(env) && arm_current_el(env) == 3 &&
513 arm_is_secure_below_el3(env)) {
68e9c2fe
EI
514 return CP_ACCESS_TRAP_UNCATEGORIZED;
515 }
516 return CP_ACCESS_OK;
517}
518
5513c3ab
PM
519/* Some secure-only AArch32 registers trap to EL3 if used from
520 * Secure EL1 (but are just ordinary UNDEF in other non-EL3 contexts).
521 * Note that an access from Secure EL1 can only happen if EL3 is AArch64.
522 * We assume that the .access field is set to PL1_RW.
523 */
524static CPAccessResult access_trap_aa32s_el1(CPUARMState *env,
3f208fd7
PM
525 const ARMCPRegInfo *ri,
526 bool isread)
5513c3ab
PM
527{
528 if (arm_current_el(env) == 3) {
529 return CP_ACCESS_OK;
530 }
531 if (arm_is_secure_below_el3(env)) {
532 return CP_ACCESS_TRAP_EL3;
533 }
534 /* This will be EL1 NS and EL2 NS, which just UNDEF */
535 return CP_ACCESS_TRAP_UNCATEGORIZED;
536}
537
187f678d
PM
538/* Check for traps to "powerdown debug" registers, which are controlled
539 * by MDCR.TDOSA
540 */
541static CPAccessResult access_tdosa(CPUARMState *env, const ARMCPRegInfo *ri,
542 bool isread)
543{
544 int el = arm_current_el(env);
30ac6339
PM
545 bool mdcr_el2_tdosa = (env->cp15.mdcr_el2 & MDCR_TDOSA) ||
546 (env->cp15.mdcr_el2 & MDCR_TDE) ||
7c208e0f 547 (arm_hcr_el2_eff(env) & HCR_TGE);
187f678d 548
30ac6339 549 if (el < 2 && mdcr_el2_tdosa && !arm_is_secure_below_el3(env)) {
187f678d
PM
550 return CP_ACCESS_TRAP_EL2;
551 }
552 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TDOSA)) {
553 return CP_ACCESS_TRAP_EL3;
554 }
555 return CP_ACCESS_OK;
556}
557
91b0a238
PM
558/* Check for traps to "debug ROM" registers, which are controlled
559 * by MDCR_EL2.TDRA for EL2 but by the more general MDCR_EL3.TDA for EL3.
560 */
561static CPAccessResult access_tdra(CPUARMState *env, const ARMCPRegInfo *ri,
562 bool isread)
563{
564 int el = arm_current_el(env);
30ac6339
PM
565 bool mdcr_el2_tdra = (env->cp15.mdcr_el2 & MDCR_TDRA) ||
566 (env->cp15.mdcr_el2 & MDCR_TDE) ||
7c208e0f 567 (arm_hcr_el2_eff(env) & HCR_TGE);
91b0a238 568
30ac6339 569 if (el < 2 && mdcr_el2_tdra && !arm_is_secure_below_el3(env)) {
91b0a238
PM
570 return CP_ACCESS_TRAP_EL2;
571 }
572 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TDA)) {
573 return CP_ACCESS_TRAP_EL3;
574 }
575 return CP_ACCESS_OK;
576}
577
d6c8cf81
PM
578/* Check for traps to general debug registers, which are controlled
579 * by MDCR_EL2.TDA for EL2 and MDCR_EL3.TDA for EL3.
580 */
581static CPAccessResult access_tda(CPUARMState *env, const ARMCPRegInfo *ri,
582 bool isread)
583{
584 int el = arm_current_el(env);
30ac6339
PM
585 bool mdcr_el2_tda = (env->cp15.mdcr_el2 & MDCR_TDA) ||
586 (env->cp15.mdcr_el2 & MDCR_TDE) ||
7c208e0f 587 (arm_hcr_el2_eff(env) & HCR_TGE);
d6c8cf81 588
30ac6339 589 if (el < 2 && mdcr_el2_tda && !arm_is_secure_below_el3(env)) {
d6c8cf81
PM
590 return CP_ACCESS_TRAP_EL2;
591 }
592 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TDA)) {
593 return CP_ACCESS_TRAP_EL3;
594 }
595 return CP_ACCESS_OK;
596}
597
1fce1ba9
PM
598/* Check for traps to performance monitor registers, which are controlled
599 * by MDCR_EL2.TPM for EL2 and MDCR_EL3.TPM for EL3.
600 */
601static CPAccessResult access_tpm(CPUARMState *env, const ARMCPRegInfo *ri,
602 bool isread)
603{
604 int el = arm_current_el(env);
605
606 if (el < 2 && (env->cp15.mdcr_el2 & MDCR_TPM)
607 && !arm_is_secure_below_el3(env)) {
608 return CP_ACCESS_TRAP_EL2;
609 }
610 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) {
611 return CP_ACCESS_TRAP_EL3;
612 }
613 return CP_ACCESS_OK;
614}
615
84929218
RH
616/* Check for traps from EL1 due to HCR_EL2.TVM and HCR_EL2.TRVM. */
617static CPAccessResult access_tvm_trvm(CPUARMState *env, const ARMCPRegInfo *ri,
618 bool isread)
619{
620 if (arm_current_el(env) == 1) {
621 uint64_t trap = isread ? HCR_TRVM : HCR_TVM;
622 if (arm_hcr_el2_eff(env) & trap) {
623 return CP_ACCESS_TRAP_EL2;
624 }
625 }
626 return CP_ACCESS_OK;
627}
628
1803d271
RH
629/* Check for traps from EL1 due to HCR_EL2.TSW. */
630static CPAccessResult access_tsw(CPUARMState *env, const ARMCPRegInfo *ri,
631 bool isread)
632{
633 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TSW)) {
634 return CP_ACCESS_TRAP_EL2;
635 }
636 return CP_ACCESS_OK;
637}
638
99602377
RH
639/* Check for traps from EL1 due to HCR_EL2.TACR. */
640static CPAccessResult access_tacr(CPUARMState *env, const ARMCPRegInfo *ri,
641 bool isread)
642{
643 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TACR)) {
644 return CP_ACCESS_TRAP_EL2;
645 }
646 return CP_ACCESS_OK;
647}
648
30881b73
RH
649/* Check for traps from EL1 due to HCR_EL2.TTLB. */
650static CPAccessResult access_ttlb(CPUARMState *env, const ARMCPRegInfo *ri,
651 bool isread)
652{
653 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TTLB)) {
654 return CP_ACCESS_TRAP_EL2;
655 }
656 return CP_ACCESS_OK;
657}
658
c4241c7d 659static void dacr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
c983fe6c 660{
2fc0cc0e 661 ARMCPU *cpu = env_archcpu(env);
00c8cb0a 662
8d5c773e 663 raw_write(env, ri, value);
d10eb08f 664 tlb_flush(CPU(cpu)); /* Flush TLB as domain not tracked in TLB */
c983fe6c
PM
665}
666
c4241c7d 667static void fcse_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
08de207b 668{
2fc0cc0e 669 ARMCPU *cpu = env_archcpu(env);
00c8cb0a 670
8d5c773e 671 if (raw_read(env, ri) != value) {
08de207b
PM
672 /* Unlike real hardware the qemu TLB uses virtual addresses,
673 * not modified virtual addresses, so this causes a TLB flush.
674 */
d10eb08f 675 tlb_flush(CPU(cpu));
8d5c773e 676 raw_write(env, ri, value);
08de207b 677 }
08de207b 678}
c4241c7d
PM
679
680static void contextidr_write(CPUARMState *env, const ARMCPRegInfo *ri,
681 uint64_t value)
08de207b 682{
2fc0cc0e 683 ARMCPU *cpu = env_archcpu(env);
00c8cb0a 684
452a0955 685 if (raw_read(env, ri) != value && !arm_feature(env, ARM_FEATURE_PMSA)
014406b5 686 && !extended_addresses_enabled(env)) {
08de207b
PM
687 /* For VMSA (when not using the LPAE long descriptor page table
688 * format) this register includes the ASID, so do a TLB flush.
689 * For PMSA it is purely a process ID and no action is needed.
690 */
d10eb08f 691 tlb_flush(CPU(cpu));
08de207b 692 }
8d5c773e 693 raw_write(env, ri, value);
08de207b
PM
694}
695
b4ab8ce9
PM
696/* IS variants of TLB operations must affect all cores */
697static void tlbiall_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
698 uint64_t value)
699{
29a0af61 700 CPUState *cs = env_cpu(env);
b4ab8ce9
PM
701
702 tlb_flush_all_cpus_synced(cs);
703}
704
705static void tlbiasid_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
706 uint64_t value)
707{
29a0af61 708 CPUState *cs = env_cpu(env);
b4ab8ce9
PM
709
710 tlb_flush_all_cpus_synced(cs);
711}
712
713static void tlbimva_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
714 uint64_t value)
715{
29a0af61 716 CPUState *cs = env_cpu(env);
b4ab8ce9
PM
717
718 tlb_flush_page_all_cpus_synced(cs, value & TARGET_PAGE_MASK);
719}
720
721static void tlbimvaa_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
722 uint64_t value)
723{
29a0af61 724 CPUState *cs = env_cpu(env);
b4ab8ce9
PM
725
726 tlb_flush_page_all_cpus_synced(cs, value & TARGET_PAGE_MASK);
727}
728
729/*
730 * Non-IS variants of TLB operations are upgraded to
731 * IS versions if we are at NS EL1 and HCR_EL2.FB is set to
732 * force broadcast of these operations.
733 */
734static bool tlb_force_broadcast(CPUARMState *env)
735{
736 return (env->cp15.hcr_el2 & HCR_FB) &&
737 arm_current_el(env) == 1 && arm_is_secure_below_el3(env);
738}
739
c4241c7d
PM
740static void tlbiall_write(CPUARMState *env, const ARMCPRegInfo *ri,
741 uint64_t value)
d929823f
PM
742{
743 /* Invalidate all (TLBIALL) */
527db2be 744 CPUState *cs = env_cpu(env);
00c8cb0a 745
b4ab8ce9 746 if (tlb_force_broadcast(env)) {
527db2be
RH
747 tlb_flush_all_cpus_synced(cs);
748 } else {
749 tlb_flush(cs);
b4ab8ce9 750 }
d929823f
PM
751}
752
c4241c7d
PM
753static void tlbimva_write(CPUARMState *env, const ARMCPRegInfo *ri,
754 uint64_t value)
d929823f
PM
755{
756 /* Invalidate single TLB entry by MVA and ASID (TLBIMVA) */
527db2be 757 CPUState *cs = env_cpu(env);
31b030d4 758
527db2be 759 value &= TARGET_PAGE_MASK;
b4ab8ce9 760 if (tlb_force_broadcast(env)) {
527db2be
RH
761 tlb_flush_page_all_cpus_synced(cs, value);
762 } else {
763 tlb_flush_page(cs, value);
b4ab8ce9 764 }
d929823f
PM
765}
766
c4241c7d
PM
767static void tlbiasid_write(CPUARMState *env, const ARMCPRegInfo *ri,
768 uint64_t value)
d929823f
PM
769{
770 /* Invalidate by ASID (TLBIASID) */
527db2be 771 CPUState *cs = env_cpu(env);
00c8cb0a 772
b4ab8ce9 773 if (tlb_force_broadcast(env)) {
527db2be
RH
774 tlb_flush_all_cpus_synced(cs);
775 } else {
776 tlb_flush(cs);
b4ab8ce9 777 }
d929823f
PM
778}
779
c4241c7d
PM
780static void tlbimvaa_write(CPUARMState *env, const ARMCPRegInfo *ri,
781 uint64_t value)
d929823f
PM
782{
783 /* Invalidate single entry by MVA, all ASIDs (TLBIMVAA) */
527db2be 784 CPUState *cs = env_cpu(env);
31b030d4 785
527db2be 786 value &= TARGET_PAGE_MASK;
b4ab8ce9 787 if (tlb_force_broadcast(env)) {
527db2be
RH
788 tlb_flush_page_all_cpus_synced(cs, value);
789 } else {
790 tlb_flush_page(cs, value);
b4ab8ce9 791 }
fa439fc5
PM
792}
793
541ef8c2
SS
794static void tlbiall_nsnh_write(CPUARMState *env, const ARMCPRegInfo *ri,
795 uint64_t value)
796{
29a0af61 797 CPUState *cs = env_cpu(env);
541ef8c2 798
0336cbf8 799 tlb_flush_by_mmuidx(cs,
01b98b68 800 ARMMMUIdxBit_E10_1 |
452ef8cb 801 ARMMMUIdxBit_E10_1_PAN |
bf05340c 802 ARMMMUIdxBit_E10_0);
541ef8c2
SS
803}
804
805static void tlbiall_nsnh_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
806 uint64_t value)
807{
29a0af61 808 CPUState *cs = env_cpu(env);
541ef8c2 809
a67cf277 810 tlb_flush_by_mmuidx_all_cpus_synced(cs,
01b98b68 811 ARMMMUIdxBit_E10_1 |
452ef8cb 812 ARMMMUIdxBit_E10_1_PAN |
bf05340c 813 ARMMMUIdxBit_E10_0);
541ef8c2
SS
814}
815
541ef8c2
SS
816
817static void tlbiall_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
818 uint64_t value)
819{
29a0af61 820 CPUState *cs = env_cpu(env);
541ef8c2 821
e013b741 822 tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_E2);
541ef8c2
SS
823}
824
825static void tlbiall_hyp_is_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_all_cpus_synced(cs, ARMMMUIdxBit_E2);
541ef8c2
SS
831}
832
833static void tlbimva_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
834 uint64_t value)
835{
29a0af61 836 CPUState *cs = env_cpu(env);
541ef8c2
SS
837 uint64_t pageaddr = value & ~MAKE_64BIT_MASK(0, 12);
838
e013b741 839 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_E2);
541ef8c2
SS
840}
841
842static void tlbimva_hyp_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
843 uint64_t value)
844{
29a0af61 845 CPUState *cs = env_cpu(env);
541ef8c2
SS
846 uint64_t pageaddr = value & ~MAKE_64BIT_MASK(0, 12);
847
a67cf277 848 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
e013b741 849 ARMMMUIdxBit_E2);
541ef8c2
SS
850}
851
e9aa6c21 852static const ARMCPRegInfo cp_reginfo[] = {
54bf36ed
FA
853 /* Define the secure and non-secure FCSE identifier CP registers
854 * separately because there is no secure bank in V8 (no _EL3). This allows
855 * the secure register to be properly reset and migrated. There is also no
856 * v8 EL1 version of the register so the non-secure instance stands alone.
857 */
9c513e78 858 { .name = "FCSEIDR",
54bf36ed
FA
859 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
860 .access = PL1_RW, .secure = ARM_CP_SECSTATE_NS,
861 .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_ns),
862 .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
9c513e78 863 { .name = "FCSEIDR_S",
54bf36ed
FA
864 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
865 .access = PL1_RW, .secure = ARM_CP_SECSTATE_S,
866 .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_s),
d4e6df63 867 .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
54bf36ed
FA
868 /* Define the secure and non-secure context identifier CP registers
869 * separately because there is no secure bank in V8 (no _EL3). This allows
870 * the secure register to be properly reset and migrated. In the
871 * non-secure case, the 32-bit register will have reset and migration
872 * disabled during registration as it is handled by the 64-bit instance.
873 */
874 { .name = "CONTEXTIDR_EL1", .state = ARM_CP_STATE_BOTH,
014406b5 875 .opc0 = 3, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
84929218
RH
876 .access = PL1_RW, .accessfn = access_tvm_trvm,
877 .secure = ARM_CP_SECSTATE_NS,
54bf36ed
FA
878 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[1]),
879 .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
9c513e78 880 { .name = "CONTEXTIDR_S", .state = ARM_CP_STATE_AA32,
54bf36ed 881 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
84929218
RH
882 .access = PL1_RW, .accessfn = access_tvm_trvm,
883 .secure = ARM_CP_SECSTATE_S,
54bf36ed 884 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_s),
d4e6df63 885 .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
9449fdf6
PM
886 REGINFO_SENTINEL
887};
888
889static const ARMCPRegInfo not_v8_cp_reginfo[] = {
890 /* NB: Some of these registers exist in v8 but with more precise
891 * definitions that don't use CP_ANY wildcards (mostly in v8_cp_reginfo[]).
892 */
893 /* MMU Domain access control / MPU write buffer control */
0c17d68c
FA
894 { .name = "DACR",
895 .cp = 15, .opc1 = CP_ANY, .crn = 3, .crm = CP_ANY, .opc2 = CP_ANY,
84929218 896 .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
0c17d68c
FA
897 .writefn = dacr_write, .raw_writefn = raw_write,
898 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
899 offsetoflow32(CPUARMState, cp15.dacr_ns) } },
a903c449
EI
900 /* ARMv7 allocates a range of implementation defined TLB LOCKDOWN regs.
901 * For v6 and v5, these mappings are overly broad.
4fdd17dd 902 */
a903c449
EI
903 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 0,
904 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
905 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 1,
906 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
907 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 4,
908 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
909 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 8,
4fdd17dd 910 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
c4804214
PM
911 /* Cache maintenance ops; some of this space may be overridden later. */
912 { .name = "CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
913 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
914 .type = ARM_CP_NOP | ARM_CP_OVERRIDE },
e9aa6c21
PM
915 REGINFO_SENTINEL
916};
917
7d57f408
PM
918static const ARMCPRegInfo not_v6_cp_reginfo[] = {
919 /* Not all pre-v6 cores implemented this WFI, so this is slightly
920 * over-broad.
921 */
922 { .name = "WFI_v5", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = 2,
923 .access = PL1_W, .type = ARM_CP_WFI },
924 REGINFO_SENTINEL
925};
926
927static const ARMCPRegInfo not_v7_cp_reginfo[] = {
928 /* Standard v6 WFI (also used in some pre-v6 cores); not in v7 (which
929 * is UNPREDICTABLE; we choose to NOP as most implementations do).
930 */
931 { .name = "WFI_v6", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
932 .access = PL1_W, .type = ARM_CP_WFI },
34f90529
PM
933 /* L1 cache lockdown. Not architectural in v6 and earlier but in practice
934 * implemented in 926, 946, 1026, 1136, 1176 and 11MPCore. StrongARM and
935 * OMAPCP will override this space.
936 */
937 { .name = "DLOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 0,
938 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_data),
939 .resetvalue = 0 },
940 { .name = "ILOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 1,
941 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_insn),
942 .resetvalue = 0 },
776d4e5c
PM
943 /* v6 doesn't have the cache ID registers but Linux reads them anyway */
944 { .name = "DUMMY", .cp = 15, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = CP_ANY,
7a0e58fa 945 .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 946 .resetvalue = 0 },
50300698
PM
947 /* We don't implement pre-v7 debug but most CPUs had at least a DBGDIDR;
948 * implementing it as RAZ means the "debug architecture version" bits
949 * will read as a reserved value, which should cause Linux to not try
950 * to use the debug hardware.
951 */
952 { .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0,
953 .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
995939a6
PM
954 /* MMU TLB control. Note that the wildcarding means we cover not just
955 * the unified TLB ops but also the dside/iside/inner-shareable variants.
956 */
957 { .name = "TLBIALL", .cp = 15, .crn = 8, .crm = CP_ANY,
958 .opc1 = CP_ANY, .opc2 = 0, .access = PL1_W, .writefn = tlbiall_write,
7a0e58fa 959 .type = ARM_CP_NO_RAW },
995939a6
PM
960 { .name = "TLBIMVA", .cp = 15, .crn = 8, .crm = CP_ANY,
961 .opc1 = CP_ANY, .opc2 = 1, .access = PL1_W, .writefn = tlbimva_write,
7a0e58fa 962 .type = ARM_CP_NO_RAW },
995939a6
PM
963 { .name = "TLBIASID", .cp = 15, .crn = 8, .crm = CP_ANY,
964 .opc1 = CP_ANY, .opc2 = 2, .access = PL1_W, .writefn = tlbiasid_write,
7a0e58fa 965 .type = ARM_CP_NO_RAW },
995939a6
PM
966 { .name = "TLBIMVAA", .cp = 15, .crn = 8, .crm = CP_ANY,
967 .opc1 = CP_ANY, .opc2 = 3, .access = PL1_W, .writefn = tlbimvaa_write,
7a0e58fa 968 .type = ARM_CP_NO_RAW },
a903c449
EI
969 { .name = "PRRR", .cp = 15, .crn = 10, .crm = 2,
970 .opc1 = 0, .opc2 = 0, .access = PL1_RW, .type = ARM_CP_NOP },
971 { .name = "NMRR", .cp = 15, .crn = 10, .crm = 2,
972 .opc1 = 0, .opc2 = 1, .access = PL1_RW, .type = ARM_CP_NOP },
7d57f408
PM
973 REGINFO_SENTINEL
974};
975
c4241c7d
PM
976static void cpacr_write(CPUARMState *env, const ARMCPRegInfo *ri,
977 uint64_t value)
2771db27 978{
f0aff255
FA
979 uint32_t mask = 0;
980
981 /* In ARMv8 most bits of CPACR_EL1 are RES0. */
982 if (!arm_feature(env, ARM_FEATURE_V8)) {
983 /* ARMv7 defines bits for unimplemented coprocessors as RAZ/WI.
984 * ASEDIS [31] and D32DIS [30] are both UNK/SBZP without VFP.
985 * TRCDIS [28] is RAZ/WI since we do not implement a trace macrocell.
986 */
7fbc6a40 987 if (cpu_isar_feature(aa32_vfp_simd, env_archcpu(env))) {
f0aff255
FA
988 /* VFP coprocessor: cp10 & cp11 [23:20] */
989 mask |= (1 << 31) | (1 << 30) | (0xf << 20);
990
991 if (!arm_feature(env, ARM_FEATURE_NEON)) {
992 /* ASEDIS [31] bit is RAO/WI */
993 value |= (1 << 31);
994 }
995
996 /* VFPv3 and upwards with NEON implement 32 double precision
997 * registers (D0-D31).
998 */
a6627f5f 999 if (!cpu_isar_feature(aa32_simd_r32, env_archcpu(env))) {
f0aff255
FA
1000 /* D32DIS [30] is RAO/WI if D16-31 are not implemented. */
1001 value |= (1 << 30);
1002 }
1003 }
1004 value &= mask;
2771db27 1005 }
fc1120a7
PM
1006
1007 /*
1008 * For A-profile AArch32 EL3 (but not M-profile secure mode), if NSACR.CP10
1009 * is 0 then CPACR.{CP11,CP10} ignore writes and read as 0b00.
1010 */
1011 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
1012 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
1013 value &= ~(0xf << 20);
1014 value |= env->cp15.cpacr_el1 & (0xf << 20);
1015 }
1016
7ebd5f2e 1017 env->cp15.cpacr_el1 = value;
2771db27
PM
1018}
1019
fc1120a7
PM
1020static uint64_t cpacr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1021{
1022 /*
1023 * For A-profile AArch32 EL3 (but not M-profile secure mode), if NSACR.CP10
1024 * is 0 then CPACR.{CP11,CP10} ignore writes and read as 0b00.
1025 */
1026 uint64_t value = env->cp15.cpacr_el1;
1027
1028 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
1029 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
1030 value &= ~(0xf << 20);
1031 }
1032 return value;
1033}
1034
1035
5deac39c
PM
1036static void cpacr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1037{
1038 /* Call cpacr_write() so that we reset with the correct RAO bits set
1039 * for our CPU features.
1040 */
1041 cpacr_write(env, ri, 0);
1042}
1043
3f208fd7
PM
1044static CPAccessResult cpacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
1045 bool isread)
c6f19164
GB
1046{
1047 if (arm_feature(env, ARM_FEATURE_V8)) {
1048 /* Check if CPACR accesses are to be trapped to EL2 */
1049 if (arm_current_el(env) == 1 &&
1050 (env->cp15.cptr_el[2] & CPTR_TCPAC) && !arm_is_secure(env)) {
1051 return CP_ACCESS_TRAP_EL2;
1052 /* Check if CPACR accesses are to be trapped to EL3 */
1053 } else if (arm_current_el(env) < 3 &&
1054 (env->cp15.cptr_el[3] & CPTR_TCPAC)) {
1055 return CP_ACCESS_TRAP_EL3;
1056 }
1057 }
1058
1059 return CP_ACCESS_OK;
1060}
1061
3f208fd7
PM
1062static CPAccessResult cptr_access(CPUARMState *env, const ARMCPRegInfo *ri,
1063 bool isread)
c6f19164
GB
1064{
1065 /* Check if CPTR accesses are set to trap to EL3 */
1066 if (arm_current_el(env) == 2 && (env->cp15.cptr_el[3] & CPTR_TCPAC)) {
1067 return CP_ACCESS_TRAP_EL3;
1068 }
1069
1070 return CP_ACCESS_OK;
1071}
1072
7d57f408
PM
1073static const ARMCPRegInfo v6_cp_reginfo[] = {
1074 /* prefetch by MVA in v6, NOP in v7 */
1075 { .name = "MVA_prefetch",
1076 .cp = 15, .crn = 7, .crm = 13, .opc1 = 0, .opc2 = 1,
1077 .access = PL1_W, .type = ARM_CP_NOP },
6df99dec
SS
1078 /* We need to break the TB after ISB to execute self-modifying code
1079 * correctly and also to take any pending interrupts immediately.
1080 * So use arm_cp_write_ignore() function instead of ARM_CP_NOP flag.
1081 */
7d57f408 1082 { .name = "ISB", .cp = 15, .crn = 7, .crm = 5, .opc1 = 0, .opc2 = 4,
6df99dec 1083 .access = PL0_W, .type = ARM_CP_NO_RAW, .writefn = arm_cp_write_ignore },
091fd17c 1084 { .name = "DSB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 4,
7d57f408 1085 .access = PL0_W, .type = ARM_CP_NOP },
091fd17c 1086 { .name = "DMB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 5,
7d57f408 1087 .access = PL0_W, .type = ARM_CP_NOP },
06d76f31 1088 { .name = "IFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 2,
84929218 1089 .access = PL1_RW, .accessfn = access_tvm_trvm,
b848ce2b
FA
1090 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ifar_s),
1091 offsetof(CPUARMState, cp15.ifar_ns) },
06d76f31
PM
1092 .resetvalue = 0, },
1093 /* Watchpoint Fault Address Register : should actually only be present
1094 * for 1136, 1176, 11MPCore.
1095 */
1096 { .name = "WFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 1,
1097 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0, },
34222fb8 1098 { .name = "CPACR", .state = ARM_CP_STATE_BOTH, .opc0 = 3,
c6f19164 1099 .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 2, .accessfn = cpacr_access,
7ebd5f2e 1100 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.cpacr_el1),
fc1120a7 1101 .resetfn = cpacr_reset, .writefn = cpacr_write, .readfn = cpacr_read },
7d57f408
PM
1102 REGINFO_SENTINEL
1103};
1104
7ece99b1
AL
1105/* Definitions for the PMU registers */
1106#define PMCRN_MASK 0xf800
1107#define PMCRN_SHIFT 11
f4efb4b2 1108#define PMCRLC 0x40
a1ed04dd
PM
1109#define PMCRDP 0x20
1110#define PMCRX 0x10
7ece99b1
AL
1111#define PMCRD 0x8
1112#define PMCRC 0x4
5ecdd3e4 1113#define PMCRP 0x2
7ece99b1 1114#define PMCRE 0x1
62d96ff4
PM
1115/*
1116 * Mask of PMCR bits writeable by guest (not including WO bits like C, P,
1117 * which can be written as 1 to trigger behaviour but which stay RAZ).
1118 */
1119#define PMCR_WRITEABLE_MASK (PMCRLC | PMCRDP | PMCRX | PMCRD | PMCRE)
7ece99b1 1120
033614c4
AL
1121#define PMXEVTYPER_P 0x80000000
1122#define PMXEVTYPER_U 0x40000000
1123#define PMXEVTYPER_NSK 0x20000000
1124#define PMXEVTYPER_NSU 0x10000000
1125#define PMXEVTYPER_NSH 0x08000000
1126#define PMXEVTYPER_M 0x04000000
1127#define PMXEVTYPER_MT 0x02000000
1128#define PMXEVTYPER_EVTCOUNT 0x0000ffff
1129#define PMXEVTYPER_MASK (PMXEVTYPER_P | PMXEVTYPER_U | PMXEVTYPER_NSK | \
1130 PMXEVTYPER_NSU | PMXEVTYPER_NSH | \
1131 PMXEVTYPER_M | PMXEVTYPER_MT | \
1132 PMXEVTYPER_EVTCOUNT)
1133
4b8afa1f
AL
1134#define PMCCFILTR 0xf8000000
1135#define PMCCFILTR_M PMXEVTYPER_M
1136#define PMCCFILTR_EL0 (PMCCFILTR | PMCCFILTR_M)
1137
7ece99b1
AL
1138static inline uint32_t pmu_num_counters(CPUARMState *env)
1139{
1140 return (env->cp15.c9_pmcr & PMCRN_MASK) >> PMCRN_SHIFT;
1141}
1142
1143/* Bits allowed to be set/cleared for PMCNTEN* and PMINTEN* */
1144static inline uint64_t pmu_counter_mask(CPUARMState *env)
1145{
1146 return (1 << 31) | ((1 << pmu_num_counters(env)) - 1);
1147}
1148
57a4a11b
AL
1149typedef struct pm_event {
1150 uint16_t number; /* PMEVTYPER.evtCount is 16 bits wide */
1151 /* If the event is supported on this CPU (used to generate PMCEID[01]) */
1152 bool (*supported)(CPUARMState *);
1153 /*
1154 * Retrieve the current count of the underlying event. The programmed
1155 * counters hold a difference from the return value from this function
1156 */
1157 uint64_t (*get_count)(CPUARMState *);
4e7beb0c
AL
1158 /*
1159 * Return how many nanoseconds it will take (at a minimum) for count events
1160 * to occur. A negative value indicates the counter will never overflow, or
1161 * that the counter has otherwise arranged for the overflow bit to be set
1162 * and the PMU interrupt to be raised on overflow.
1163 */
1164 int64_t (*ns_per_count)(uint64_t);
57a4a11b
AL
1165} pm_event;
1166
b2e23725
AL
1167static bool event_always_supported(CPUARMState *env)
1168{
1169 return true;
1170}
1171
0d4bfd7d
AL
1172static uint64_t swinc_get_count(CPUARMState *env)
1173{
1174 /*
1175 * SW_INCR events are written directly to the pmevcntr's by writes to
1176 * PMSWINC, so there is no underlying count maintained by the PMU itself
1177 */
1178 return 0;
1179}
1180
4e7beb0c
AL
1181static int64_t swinc_ns_per(uint64_t ignored)
1182{
1183 return -1;
1184}
1185
b2e23725
AL
1186/*
1187 * Return the underlying cycle count for the PMU cycle counters. If we're in
1188 * usermode, simply return 0.
1189 */
1190static uint64_t cycles_get_count(CPUARMState *env)
1191{
1192#ifndef CONFIG_USER_ONLY
1193 return muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
1194 ARM_CPU_FREQ, NANOSECONDS_PER_SECOND);
1195#else
1196 return cpu_get_host_ticks();
1197#endif
1198}
1199
1200#ifndef CONFIG_USER_ONLY
4e7beb0c
AL
1201static int64_t cycles_ns_per(uint64_t cycles)
1202{
1203 return (ARM_CPU_FREQ / NANOSECONDS_PER_SECOND) * cycles;
1204}
1205
b2e23725
AL
1206static bool instructions_supported(CPUARMState *env)
1207{
1208 return use_icount == 1 /* Precise instruction counting */;
1209}
1210
1211static uint64_t instructions_get_count(CPUARMState *env)
1212{
1213 return (uint64_t)cpu_get_icount_raw();
1214}
4e7beb0c
AL
1215
1216static int64_t instructions_ns_per(uint64_t icount)
1217{
1218 return cpu_icount_to_ns((int64_t)icount);
1219}
b2e23725
AL
1220#endif
1221
0727f63b
PM
1222static bool pmu_8_1_events_supported(CPUARMState *env)
1223{
1224 /* For events which are supported in any v8.1 PMU */
1225 return cpu_isar_feature(any_pmu_8_1, env_archcpu(env));
1226}
1227
15dd1ebd
PM
1228static bool pmu_8_4_events_supported(CPUARMState *env)
1229{
1230 /* For events which are supported in any v8.1 PMU */
1231 return cpu_isar_feature(any_pmu_8_4, env_archcpu(env));
1232}
1233
0727f63b
PM
1234static uint64_t zero_event_get_count(CPUARMState *env)
1235{
1236 /* For events which on QEMU never fire, so their count is always zero */
1237 return 0;
1238}
1239
1240static int64_t zero_event_ns_per(uint64_t cycles)
1241{
1242 /* An event which never fires can never overflow */
1243 return -1;
1244}
1245
57a4a11b 1246static const pm_event pm_events[] = {
0d4bfd7d
AL
1247 { .number = 0x000, /* SW_INCR */
1248 .supported = event_always_supported,
1249 .get_count = swinc_get_count,
4e7beb0c 1250 .ns_per_count = swinc_ns_per,
0d4bfd7d 1251 },
b2e23725
AL
1252#ifndef CONFIG_USER_ONLY
1253 { .number = 0x008, /* INST_RETIRED, Instruction architecturally executed */
1254 .supported = instructions_supported,
1255 .get_count = instructions_get_count,
4e7beb0c 1256 .ns_per_count = instructions_ns_per,
b2e23725
AL
1257 },
1258 { .number = 0x011, /* CPU_CYCLES, Cycle */
1259 .supported = event_always_supported,
1260 .get_count = cycles_get_count,
4e7beb0c 1261 .ns_per_count = cycles_ns_per,
0727f63b 1262 },
b2e23725 1263#endif
0727f63b
PM
1264 { .number = 0x023, /* STALL_FRONTEND */
1265 .supported = pmu_8_1_events_supported,
1266 .get_count = zero_event_get_count,
1267 .ns_per_count = zero_event_ns_per,
1268 },
1269 { .number = 0x024, /* STALL_BACKEND */
1270 .supported = pmu_8_1_events_supported,
1271 .get_count = zero_event_get_count,
1272 .ns_per_count = zero_event_ns_per,
1273 },
15dd1ebd
PM
1274 { .number = 0x03c, /* STALL */
1275 .supported = pmu_8_4_events_supported,
1276 .get_count = zero_event_get_count,
1277 .ns_per_count = zero_event_ns_per,
1278 },
57a4a11b
AL
1279};
1280
1281/*
1282 * Note: Before increasing MAX_EVENT_ID beyond 0x3f into the 0x40xx range of
1283 * events (i.e. the statistical profiling extension), this implementation
1284 * should first be updated to something sparse instead of the current
1285 * supported_event_map[] array.
1286 */
15dd1ebd 1287#define MAX_EVENT_ID 0x3c
57a4a11b
AL
1288#define UNSUPPORTED_EVENT UINT16_MAX
1289static uint16_t supported_event_map[MAX_EVENT_ID + 1];
1290
1291/*
bf8d0969
AL
1292 * Called upon CPU initialization to initialize PMCEID[01]_EL0 and build a map
1293 * of ARM event numbers to indices in our pm_events array.
57a4a11b
AL
1294 *
1295 * Note: Events in the 0x40XX range are not currently supported.
1296 */
bf8d0969 1297void pmu_init(ARMCPU *cpu)
57a4a11b 1298{
57a4a11b
AL
1299 unsigned int i;
1300
bf8d0969
AL
1301 /*
1302 * Empty supported_event_map and cpu->pmceid[01] before adding supported
1303 * events to them
1304 */
57a4a11b
AL
1305 for (i = 0; i < ARRAY_SIZE(supported_event_map); i++) {
1306 supported_event_map[i] = UNSUPPORTED_EVENT;
1307 }
bf8d0969
AL
1308 cpu->pmceid0 = 0;
1309 cpu->pmceid1 = 0;
57a4a11b
AL
1310
1311 for (i = 0; i < ARRAY_SIZE(pm_events); i++) {
1312 const pm_event *cnt = &pm_events[i];
1313 assert(cnt->number <= MAX_EVENT_ID);
1314 /* We do not currently support events in the 0x40xx range */
1315 assert(cnt->number <= 0x3f);
1316
bf8d0969 1317 if (cnt->supported(&cpu->env)) {
57a4a11b 1318 supported_event_map[cnt->number] = i;
67da43d6 1319 uint64_t event_mask = 1ULL << (cnt->number & 0x1f);
bf8d0969
AL
1320 if (cnt->number & 0x20) {
1321 cpu->pmceid1 |= event_mask;
1322 } else {
1323 cpu->pmceid0 |= event_mask;
1324 }
57a4a11b
AL
1325 }
1326 }
57a4a11b
AL
1327}
1328
5ecdd3e4
AL
1329/*
1330 * Check at runtime whether a PMU event is supported for the current machine
1331 */
1332static bool event_supported(uint16_t number)
1333{
1334 if (number > MAX_EVENT_ID) {
1335 return false;
1336 }
1337 return supported_event_map[number] != UNSUPPORTED_EVENT;
1338}
1339
3f208fd7
PM
1340static CPAccessResult pmreg_access(CPUARMState *env, const ARMCPRegInfo *ri,
1341 bool isread)
200ac0ef 1342{
3b163b01 1343 /* Performance monitor registers user accessibility is controlled
1fce1ba9
PM
1344 * by PMUSERENR. MDCR_EL2.TPM and MDCR_EL3.TPM allow configurable
1345 * trapping to EL2 or EL3 for other accesses.
200ac0ef 1346 */
1fce1ba9
PM
1347 int el = arm_current_el(env);
1348
6ecd0b6b 1349 if (el == 0 && !(env->cp15.c9_pmuserenr & 1)) {
fcd25206 1350 return CP_ACCESS_TRAP;
200ac0ef 1351 }
1fce1ba9
PM
1352 if (el < 2 && (env->cp15.mdcr_el2 & MDCR_TPM)
1353 && !arm_is_secure_below_el3(env)) {
1354 return CP_ACCESS_TRAP_EL2;
1355 }
1356 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) {
1357 return CP_ACCESS_TRAP_EL3;
1358 }
1359
fcd25206 1360 return CP_ACCESS_OK;
200ac0ef
PM
1361}
1362
6ecd0b6b
AB
1363static CPAccessResult pmreg_access_xevcntr(CPUARMState *env,
1364 const ARMCPRegInfo *ri,
1365 bool isread)
1366{
1367 /* ER: event counter read trap control */
1368 if (arm_feature(env, ARM_FEATURE_V8)
1369 && arm_current_el(env) == 0
1370 && (env->cp15.c9_pmuserenr & (1 << 3)) != 0
1371 && isread) {
1372 return CP_ACCESS_OK;
1373 }
1374
1375 return pmreg_access(env, ri, isread);
1376}
1377
1378static CPAccessResult pmreg_access_swinc(CPUARMState *env,
1379 const ARMCPRegInfo *ri,
1380 bool isread)
1381{
1382 /* SW: software increment write trap control */
1383 if (arm_feature(env, ARM_FEATURE_V8)
1384 && arm_current_el(env) == 0
1385 && (env->cp15.c9_pmuserenr & (1 << 1)) != 0
1386 && !isread) {
1387 return CP_ACCESS_OK;
1388 }
1389
1390 return pmreg_access(env, ri, isread);
1391}
1392
6ecd0b6b
AB
1393static CPAccessResult pmreg_access_selr(CPUARMState *env,
1394 const ARMCPRegInfo *ri,
1395 bool isread)
1396{
1397 /* ER: event counter read trap control */
1398 if (arm_feature(env, ARM_FEATURE_V8)
1399 && arm_current_el(env) == 0
1400 && (env->cp15.c9_pmuserenr & (1 << 3)) != 0) {
1401 return CP_ACCESS_OK;
1402 }
1403
1404 return pmreg_access(env, ri, isread);
1405}
1406
1407static CPAccessResult pmreg_access_ccntr(CPUARMState *env,
1408 const ARMCPRegInfo *ri,
1409 bool isread)
1410{
1411 /* CR: cycle counter read trap control */
1412 if (arm_feature(env, ARM_FEATURE_V8)
1413 && arm_current_el(env) == 0
1414 && (env->cp15.c9_pmuserenr & (1 << 2)) != 0
1415 && isread) {
1416 return CP_ACCESS_OK;
1417 }
1418
1419 return pmreg_access(env, ri, isread);
1420}
1421
033614c4
AL
1422/* Returns true if the counter (pass 31 for PMCCNTR) should count events using
1423 * the current EL, security state, and register configuration.
1424 */
1425static bool pmu_counter_enabled(CPUARMState *env, uint8_t counter)
87124fde 1426{
033614c4
AL
1427 uint64_t filter;
1428 bool e, p, u, nsk, nsu, nsh, m;
1429 bool enabled, prohibited, filtered;
1430 bool secure = arm_is_secure(env);
1431 int el = arm_current_el(env);
1432 uint8_t hpmn = env->cp15.mdcr_el2 & MDCR_HPMN;
87124fde 1433
cbbb3041
AJ
1434 if (!arm_feature(env, ARM_FEATURE_PMU)) {
1435 return false;
1436 }
1437
033614c4
AL
1438 if (!arm_feature(env, ARM_FEATURE_EL2) ||
1439 (counter < hpmn || counter == 31)) {
1440 e = env->cp15.c9_pmcr & PMCRE;
1441 } else {
1442 e = env->cp15.mdcr_el2 & MDCR_HPME;
87124fde 1443 }
033614c4 1444 enabled = e && (env->cp15.c9_pmcnten & (1 << counter));
87124fde 1445
033614c4
AL
1446 if (!secure) {
1447 if (el == 2 && (counter < hpmn || counter == 31)) {
1448 prohibited = env->cp15.mdcr_el2 & MDCR_HPMD;
1449 } else {
1450 prohibited = false;
1451 }
1452 } else {
1453 prohibited = arm_feature(env, ARM_FEATURE_EL3) &&
1454 (env->cp15.mdcr_el3 & MDCR_SPME);
1455 }
1456
1457 if (prohibited && counter == 31) {
1458 prohibited = env->cp15.c9_pmcr & PMCRDP;
1459 }
1460
5ecdd3e4
AL
1461 if (counter == 31) {
1462 filter = env->cp15.pmccfiltr_el0;
1463 } else {
1464 filter = env->cp15.c14_pmevtyper[counter];
1465 }
033614c4
AL
1466
1467 p = filter & PMXEVTYPER_P;
1468 u = filter & PMXEVTYPER_U;
1469 nsk = arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_NSK);
1470 nsu = arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_NSU);
1471 nsh = arm_feature(env, ARM_FEATURE_EL2) && (filter & PMXEVTYPER_NSH);
1472 m = arm_el_is_aa64(env, 1) &&
1473 arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_M);
1474
1475 if (el == 0) {
1476 filtered = secure ? u : u != nsu;
1477 } else if (el == 1) {
1478 filtered = secure ? p : p != nsk;
1479 } else if (el == 2) {
1480 filtered = !nsh;
1481 } else { /* EL3 */
1482 filtered = m != p;
1483 }
1484
5ecdd3e4
AL
1485 if (counter != 31) {
1486 /*
1487 * If not checking PMCCNTR, ensure the counter is setup to an event we
1488 * support
1489 */
1490 uint16_t event = filter & PMXEVTYPER_EVTCOUNT;
1491 if (!event_supported(event)) {
1492 return false;
1493 }
1494 }
1495
033614c4 1496 return enabled && !prohibited && !filtered;
87124fde 1497}
033614c4 1498
f4efb4b2
AL
1499static void pmu_update_irq(CPUARMState *env)
1500{
2fc0cc0e 1501 ARMCPU *cpu = env_archcpu(env);
f4efb4b2
AL
1502 qemu_set_irq(cpu->pmu_interrupt, (env->cp15.c9_pmcr & PMCRE) &&
1503 (env->cp15.c9_pminten & env->cp15.c9_pmovsr));
1504}
1505
5d05b9d4
AL
1506/*
1507 * Ensure c15_ccnt is the guest-visible count so that operations such as
1508 * enabling/disabling the counter or filtering, modifying the count itself,
1509 * etc. can be done logically. This is essentially a no-op if the counter is
1510 * not enabled at the time of the call.
1511 */
f2b2f53f 1512static void pmccntr_op_start(CPUARMState *env)
ec7b4ce4 1513{
b2e23725 1514 uint64_t cycles = cycles_get_count(env);
ec7b4ce4 1515
033614c4 1516 if (pmu_counter_enabled(env, 31)) {
5d05b9d4
AL
1517 uint64_t eff_cycles = cycles;
1518 if (env->cp15.c9_pmcr & PMCRD) {
1519 /* Increment once every 64 processor clock cycles */
1520 eff_cycles /= 64;
1521 }
1522
f4efb4b2
AL
1523 uint64_t new_pmccntr = eff_cycles - env->cp15.c15_ccnt_delta;
1524
1525 uint64_t overflow_mask = env->cp15.c9_pmcr & PMCRLC ? \
1526 1ull << 63 : 1ull << 31;
1527 if (env->cp15.c15_ccnt & ~new_pmccntr & overflow_mask) {
1528 env->cp15.c9_pmovsr |= (1 << 31);
1529 pmu_update_irq(env);
1530 }
1531
1532 env->cp15.c15_ccnt = new_pmccntr;
ec7b4ce4 1533 }
5d05b9d4
AL
1534 env->cp15.c15_ccnt_delta = cycles;
1535}
ec7b4ce4 1536
5d05b9d4
AL
1537/*
1538 * If PMCCNTR is enabled, recalculate the delta between the clock and the
1539 * guest-visible count. A call to pmccntr_op_finish should follow every call to
1540 * pmccntr_op_start.
1541 */
f2b2f53f 1542static void pmccntr_op_finish(CPUARMState *env)
5d05b9d4 1543{
033614c4 1544 if (pmu_counter_enabled(env, 31)) {
4e7beb0c
AL
1545#ifndef CONFIG_USER_ONLY
1546 /* Calculate when the counter will next overflow */
1547 uint64_t remaining_cycles = -env->cp15.c15_ccnt;
1548 if (!(env->cp15.c9_pmcr & PMCRLC)) {
1549 remaining_cycles = (uint32_t)remaining_cycles;
1550 }
1551 int64_t overflow_in = cycles_ns_per(remaining_cycles);
1552
1553 if (overflow_in > 0) {
1554 int64_t overflow_at = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
1555 overflow_in;
2fc0cc0e 1556 ARMCPU *cpu = env_archcpu(env);
4e7beb0c
AL
1557 timer_mod_anticipate_ns(cpu->pmu_timer, overflow_at);
1558 }
1559#endif
5d05b9d4 1560
4e7beb0c 1561 uint64_t prev_cycles = env->cp15.c15_ccnt_delta;
5d05b9d4
AL
1562 if (env->cp15.c9_pmcr & PMCRD) {
1563 /* Increment once every 64 processor clock cycles */
1564 prev_cycles /= 64;
1565 }
5d05b9d4 1566 env->cp15.c15_ccnt_delta = prev_cycles - env->cp15.c15_ccnt;
ec7b4ce4
AF
1567 }
1568}
1569
5ecdd3e4
AL
1570static void pmevcntr_op_start(CPUARMState *env, uint8_t counter)
1571{
1572
1573 uint16_t event = env->cp15.c14_pmevtyper[counter] & PMXEVTYPER_EVTCOUNT;
1574 uint64_t count = 0;
1575 if (event_supported(event)) {
1576 uint16_t event_idx = supported_event_map[event];
1577 count = pm_events[event_idx].get_count(env);
1578 }
1579
1580 if (pmu_counter_enabled(env, counter)) {
f4efb4b2
AL
1581 uint32_t new_pmevcntr = count - env->cp15.c14_pmevcntr_delta[counter];
1582
1583 if (env->cp15.c14_pmevcntr[counter] & ~new_pmevcntr & INT32_MIN) {
1584 env->cp15.c9_pmovsr |= (1 << counter);
1585 pmu_update_irq(env);
1586 }
1587 env->cp15.c14_pmevcntr[counter] = new_pmevcntr;
5ecdd3e4
AL
1588 }
1589 env->cp15.c14_pmevcntr_delta[counter] = count;
1590}
1591
1592static void pmevcntr_op_finish(CPUARMState *env, uint8_t counter)
1593{
1594 if (pmu_counter_enabled(env, counter)) {
4e7beb0c
AL
1595#ifndef CONFIG_USER_ONLY
1596 uint16_t event = env->cp15.c14_pmevtyper[counter] & PMXEVTYPER_EVTCOUNT;
1597 uint16_t event_idx = supported_event_map[event];
1598 uint64_t delta = UINT32_MAX -
1599 (uint32_t)env->cp15.c14_pmevcntr[counter] + 1;
1600 int64_t overflow_in = pm_events[event_idx].ns_per_count(delta);
1601
1602 if (overflow_in > 0) {
1603 int64_t overflow_at = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
1604 overflow_in;
2fc0cc0e 1605 ARMCPU *cpu = env_archcpu(env);
4e7beb0c
AL
1606 timer_mod_anticipate_ns(cpu->pmu_timer, overflow_at);
1607 }
1608#endif
1609
5ecdd3e4
AL
1610 env->cp15.c14_pmevcntr_delta[counter] -=
1611 env->cp15.c14_pmevcntr[counter];
1612 }
1613}
1614
5d05b9d4
AL
1615void pmu_op_start(CPUARMState *env)
1616{
5ecdd3e4 1617 unsigned int i;
5d05b9d4 1618 pmccntr_op_start(env);
5ecdd3e4
AL
1619 for (i = 0; i < pmu_num_counters(env); i++) {
1620 pmevcntr_op_start(env, i);
1621 }
5d05b9d4
AL
1622}
1623
1624void pmu_op_finish(CPUARMState *env)
1625{
5ecdd3e4 1626 unsigned int i;
5d05b9d4 1627 pmccntr_op_finish(env);
5ecdd3e4
AL
1628 for (i = 0; i < pmu_num_counters(env); i++) {
1629 pmevcntr_op_finish(env, i);
1630 }
5d05b9d4
AL
1631}
1632
033614c4
AL
1633void pmu_pre_el_change(ARMCPU *cpu, void *ignored)
1634{
1635 pmu_op_start(&cpu->env);
1636}
1637
1638void pmu_post_el_change(ARMCPU *cpu, void *ignored)
1639{
1640 pmu_op_finish(&cpu->env);
1641}
1642
4e7beb0c
AL
1643void arm_pmu_timer_cb(void *opaque)
1644{
1645 ARMCPU *cpu = opaque;
1646
1647 /*
1648 * Update all the counter values based on the current underlying counts,
1649 * triggering interrupts to be raised, if necessary. pmu_op_finish() also
1650 * has the effect of setting the cpu->pmu_timer to the next earliest time a
1651 * counter may expire.
1652 */
1653 pmu_op_start(&cpu->env);
1654 pmu_op_finish(&cpu->env);
1655}
1656
c4241c7d
PM
1657static void pmcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1658 uint64_t value)
200ac0ef 1659{
5d05b9d4 1660 pmu_op_start(env);
7c2cb42b
AF
1661
1662 if (value & PMCRC) {
1663 /* The counter has been reset */
1664 env->cp15.c15_ccnt = 0;
1665 }
1666
5ecdd3e4
AL
1667 if (value & PMCRP) {
1668 unsigned int i;
1669 for (i = 0; i < pmu_num_counters(env); i++) {
1670 env->cp15.c14_pmevcntr[i] = 0;
1671 }
1672 }
1673
62d96ff4
PM
1674 env->cp15.c9_pmcr &= ~PMCR_WRITEABLE_MASK;
1675 env->cp15.c9_pmcr |= (value & PMCR_WRITEABLE_MASK);
7c2cb42b 1676
5d05b9d4 1677 pmu_op_finish(env);
7c2cb42b
AF
1678}
1679
0d4bfd7d
AL
1680static void pmswinc_write(CPUARMState *env, const ARMCPRegInfo *ri,
1681 uint64_t value)
1682{
1683 unsigned int i;
1684 for (i = 0; i < pmu_num_counters(env); i++) {
1685 /* Increment a counter's count iff: */
1686 if ((value & (1 << i)) && /* counter's bit is set */
1687 /* counter is enabled and not filtered */
1688 pmu_counter_enabled(env, i) &&
1689 /* counter is SW_INCR */
1690 (env->cp15.c14_pmevtyper[i] & PMXEVTYPER_EVTCOUNT) == 0x0) {
1691 pmevcntr_op_start(env, i);
f4efb4b2
AL
1692
1693 /*
1694 * Detect if this write causes an overflow since we can't predict
1695 * PMSWINC overflows like we can for other events
1696 */
1697 uint32_t new_pmswinc = env->cp15.c14_pmevcntr[i] + 1;
1698
1699 if (env->cp15.c14_pmevcntr[i] & ~new_pmswinc & INT32_MIN) {
1700 env->cp15.c9_pmovsr |= (1 << i);
1701 pmu_update_irq(env);
1702 }
1703
1704 env->cp15.c14_pmevcntr[i] = new_pmswinc;
1705
0d4bfd7d
AL
1706 pmevcntr_op_finish(env, i);
1707 }
1708 }
1709}
1710
7c2cb42b
AF
1711static uint64_t pmccntr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1712{
5d05b9d4
AL
1713 uint64_t ret;
1714 pmccntr_op_start(env);
1715 ret = env->cp15.c15_ccnt;
1716 pmccntr_op_finish(env);
1717 return ret;
7c2cb42b
AF
1718}
1719
6b040780
WH
1720static void pmselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1721 uint64_t value)
1722{
1723 /* The value of PMSELR.SEL affects the behavior of PMXEVTYPER and
1724 * PMXEVCNTR. We allow [0..31] to be written to PMSELR here; in the
1725 * meanwhile, we check PMSELR.SEL when PMXEVTYPER and PMXEVCNTR are
1726 * accessed.
1727 */
1728 env->cp15.c9_pmselr = value & 0x1f;
1729}
1730
7c2cb42b
AF
1731static void pmccntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1732 uint64_t value)
1733{
5d05b9d4
AL
1734 pmccntr_op_start(env);
1735 env->cp15.c15_ccnt = value;
1736 pmccntr_op_finish(env);
200ac0ef 1737}
421c7ebd
PC
1738
1739static void pmccntr_write32(CPUARMState *env, const ARMCPRegInfo *ri,
1740 uint64_t value)
1741{
1742 uint64_t cur_val = pmccntr_read(env, NULL);
1743
1744 pmccntr_write(env, ri, deposit64(cur_val, 0, 32, value));
1745}
1746
0614601c
AF
1747static void pmccfiltr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1748 uint64_t value)
1749{
5d05b9d4 1750 pmccntr_op_start(env);
4b8afa1f
AL
1751 env->cp15.pmccfiltr_el0 = value & PMCCFILTR_EL0;
1752 pmccntr_op_finish(env);
1753}
1754
1755static void pmccfiltr_write_a32(CPUARMState *env, const ARMCPRegInfo *ri,
1756 uint64_t value)
1757{
1758 pmccntr_op_start(env);
1759 /* M is not accessible from AArch32 */
1760 env->cp15.pmccfiltr_el0 = (env->cp15.pmccfiltr_el0 & PMCCFILTR_M) |
1761 (value & PMCCFILTR);
5d05b9d4 1762 pmccntr_op_finish(env);
0614601c
AF
1763}
1764
4b8afa1f
AL
1765static uint64_t pmccfiltr_read_a32(CPUARMState *env, const ARMCPRegInfo *ri)
1766{
1767 /* M is not visible in AArch32 */
1768 return env->cp15.pmccfiltr_el0 & PMCCFILTR;
1769}
1770
c4241c7d 1771static void pmcntenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
200ac0ef
PM
1772 uint64_t value)
1773{
7ece99b1 1774 value &= pmu_counter_mask(env);
200ac0ef 1775 env->cp15.c9_pmcnten |= value;
200ac0ef
PM
1776}
1777
c4241c7d
PM
1778static void pmcntenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1779 uint64_t value)
200ac0ef 1780{
7ece99b1 1781 value &= pmu_counter_mask(env);
200ac0ef 1782 env->cp15.c9_pmcnten &= ~value;
200ac0ef
PM
1783}
1784
c4241c7d
PM
1785static void pmovsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1786 uint64_t value)
200ac0ef 1787{
599b71e2 1788 value &= pmu_counter_mask(env);
200ac0ef 1789 env->cp15.c9_pmovsr &= ~value;
f4efb4b2 1790 pmu_update_irq(env);
200ac0ef
PM
1791}
1792
327dd510
AL
1793static void pmovsset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1794 uint64_t value)
1795{
1796 value &= pmu_counter_mask(env);
1797 env->cp15.c9_pmovsr |= value;
f4efb4b2 1798 pmu_update_irq(env);
327dd510
AL
1799}
1800
5ecdd3e4
AL
1801static void pmevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
1802 uint64_t value, const uint8_t counter)
200ac0ef 1803{
5ecdd3e4
AL
1804 if (counter == 31) {
1805 pmccfiltr_write(env, ri, value);
1806 } else if (counter < pmu_num_counters(env)) {
1807 pmevcntr_op_start(env, counter);
1808
1809 /*
1810 * If this counter's event type is changing, store the current
1811 * underlying count for the new type in c14_pmevcntr_delta[counter] so
1812 * pmevcntr_op_finish has the correct baseline when it converts back to
1813 * a delta.
1814 */
1815 uint16_t old_event = env->cp15.c14_pmevtyper[counter] &
1816 PMXEVTYPER_EVTCOUNT;
1817 uint16_t new_event = value & PMXEVTYPER_EVTCOUNT;
1818 if (old_event != new_event) {
1819 uint64_t count = 0;
1820 if (event_supported(new_event)) {
1821 uint16_t event_idx = supported_event_map[new_event];
1822 count = pm_events[event_idx].get_count(env);
1823 }
1824 env->cp15.c14_pmevcntr_delta[counter] = count;
1825 }
1826
1827 env->cp15.c14_pmevtyper[counter] = value & PMXEVTYPER_MASK;
1828 pmevcntr_op_finish(env, counter);
1829 }
fdb86656
WH
1830 /* Attempts to access PMXEVTYPER are CONSTRAINED UNPREDICTABLE when
1831 * PMSELR value is equal to or greater than the number of implemented
1832 * counters, but not equal to 0x1f. We opt to behave as a RAZ/WI.
1833 */
5ecdd3e4
AL
1834}
1835
1836static uint64_t pmevtyper_read(CPUARMState *env, const ARMCPRegInfo *ri,
1837 const uint8_t counter)
1838{
1839 if (counter == 31) {
1840 return env->cp15.pmccfiltr_el0;
1841 } else if (counter < pmu_num_counters(env)) {
1842 return env->cp15.c14_pmevtyper[counter];
1843 } else {
1844 /*
1845 * We opt to behave as a RAZ/WI when attempts to access PMXEVTYPER
1846 * are CONSTRAINED UNPREDICTABLE. See comments in pmevtyper_write().
1847 */
1848 return 0;
1849 }
1850}
1851
1852static void pmevtyper_writefn(CPUARMState *env, const ARMCPRegInfo *ri,
1853 uint64_t value)
1854{
1855 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1856 pmevtyper_write(env, ri, value, counter);
1857}
1858
1859static void pmevtyper_rawwrite(CPUARMState *env, const ARMCPRegInfo *ri,
1860 uint64_t value)
1861{
1862 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1863 env->cp15.c14_pmevtyper[counter] = value;
1864
1865 /*
1866 * pmevtyper_rawwrite is called between a pair of pmu_op_start and
1867 * pmu_op_finish calls when loading saved state for a migration. Because
1868 * we're potentially updating the type of event here, the value written to
1869 * c14_pmevcntr_delta by the preceeding pmu_op_start call may be for a
1870 * different counter type. Therefore, we need to set this value to the
1871 * current count for the counter type we're writing so that pmu_op_finish
1872 * has the correct count for its calculation.
1873 */
1874 uint16_t event = value & PMXEVTYPER_EVTCOUNT;
1875 if (event_supported(event)) {
1876 uint16_t event_idx = supported_event_map[event];
1877 env->cp15.c14_pmevcntr_delta[counter] =
1878 pm_events[event_idx].get_count(env);
fdb86656
WH
1879 }
1880}
1881
5ecdd3e4
AL
1882static uint64_t pmevtyper_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
1883{
1884 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1885 return pmevtyper_read(env, ri, counter);
1886}
1887
1888static void pmxevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
1889 uint64_t value)
1890{
1891 pmevtyper_write(env, ri, value, env->cp15.c9_pmselr & 31);
1892}
1893
fdb86656
WH
1894static uint64_t pmxevtyper_read(CPUARMState *env, const ARMCPRegInfo *ri)
1895{
5ecdd3e4
AL
1896 return pmevtyper_read(env, ri, env->cp15.c9_pmselr & 31);
1897}
1898
1899static void pmevcntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1900 uint64_t value, uint8_t counter)
1901{
1902 if (counter < pmu_num_counters(env)) {
1903 pmevcntr_op_start(env, counter);
1904 env->cp15.c14_pmevcntr[counter] = value;
1905 pmevcntr_op_finish(env, counter);
1906 }
1907 /*
1908 * We opt to behave as a RAZ/WI when attempts to access PM[X]EVCNTR
1909 * are CONSTRAINED UNPREDICTABLE.
fdb86656 1910 */
5ecdd3e4
AL
1911}
1912
1913static uint64_t pmevcntr_read(CPUARMState *env, const ARMCPRegInfo *ri,
1914 uint8_t counter)
1915{
1916 if (counter < pmu_num_counters(env)) {
1917 uint64_t ret;
1918 pmevcntr_op_start(env, counter);
1919 ret = env->cp15.c14_pmevcntr[counter];
1920 pmevcntr_op_finish(env, counter);
1921 return ret;
fdb86656 1922 } else {
5ecdd3e4
AL
1923 /* We opt to behave as a RAZ/WI when attempts to access PM[X]EVCNTR
1924 * are CONSTRAINED UNPREDICTABLE. */
fdb86656
WH
1925 return 0;
1926 }
200ac0ef
PM
1927}
1928
5ecdd3e4
AL
1929static void pmevcntr_writefn(CPUARMState *env, const ARMCPRegInfo *ri,
1930 uint64_t value)
1931{
1932 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1933 pmevcntr_write(env, ri, value, counter);
1934}
1935
1936static uint64_t pmevcntr_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
1937{
1938 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1939 return pmevcntr_read(env, ri, counter);
1940}
1941
1942static void pmevcntr_rawwrite(CPUARMState *env, const ARMCPRegInfo *ri,
1943 uint64_t value)
1944{
1945 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1946 assert(counter < pmu_num_counters(env));
1947 env->cp15.c14_pmevcntr[counter] = value;
1948 pmevcntr_write(env, ri, value, counter);
1949}
1950
1951static uint64_t pmevcntr_rawread(CPUARMState *env, const ARMCPRegInfo *ri)
1952{
1953 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1954 assert(counter < pmu_num_counters(env));
1955 return env->cp15.c14_pmevcntr[counter];
1956}
1957
1958static void pmxevcntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1959 uint64_t value)
1960{
1961 pmevcntr_write(env, ri, value, env->cp15.c9_pmselr & 31);
1962}
1963
1964static uint64_t pmxevcntr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1965{
1966 return pmevcntr_read(env, ri, env->cp15.c9_pmselr & 31);
1967}
1968
c4241c7d 1969static void pmuserenr_write(CPUARMState *env, const ARMCPRegInfo *ri,
200ac0ef
PM
1970 uint64_t value)
1971{
6ecd0b6b
AB
1972 if (arm_feature(env, ARM_FEATURE_V8)) {
1973 env->cp15.c9_pmuserenr = value & 0xf;
1974 } else {
1975 env->cp15.c9_pmuserenr = value & 1;
1976 }
200ac0ef
PM
1977}
1978
c4241c7d
PM
1979static void pmintenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1980 uint64_t value)
200ac0ef
PM
1981{
1982 /* We have no event counters so only the C bit can be changed */
7ece99b1 1983 value &= pmu_counter_mask(env);
200ac0ef 1984 env->cp15.c9_pminten |= value;
f4efb4b2 1985 pmu_update_irq(env);
200ac0ef
PM
1986}
1987
c4241c7d
PM
1988static void pmintenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1989 uint64_t value)
200ac0ef 1990{
7ece99b1 1991 value &= pmu_counter_mask(env);
200ac0ef 1992 env->cp15.c9_pminten &= ~value;
f4efb4b2 1993 pmu_update_irq(env);
200ac0ef
PM
1994}
1995
c4241c7d
PM
1996static void vbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
1997 uint64_t value)
8641136c 1998{
a505d7fe
PM
1999 /* Note that even though the AArch64 view of this register has bits
2000 * [10:0] all RES0 we can only mask the bottom 5, to comply with the
2001 * architectural requirements for bits which are RES0 only in some
2002 * contexts. (ARMv8 would permit us to do no masking at all, but ARMv7
2003 * requires the bottom five bits to be RAZ/WI because they're UNK/SBZP.)
2004 */
855ea66d 2005 raw_write(env, ri, value & ~0x1FULL);
8641136c
NR
2006}
2007
64e0e2de
EI
2008static void scr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
2009{
ea22747c
RH
2010 /* Begin with base v8.0 state. */
2011 uint32_t valid_mask = 0x3fff;
2fc0cc0e 2012 ARMCPU *cpu = env_archcpu(env);
ea22747c 2013
252e8c69 2014 if (ri->state == ARM_CP_STATE_AA64) {
ea22747c
RH
2015 value |= SCR_FW | SCR_AW; /* these two bits are RES1. */
2016 valid_mask &= ~SCR_NET;
252e8c69
RH
2017
2018 if (cpu_isar_feature(aa64_lor, cpu)) {
2019 valid_mask |= SCR_TLOR;
2020 }
2021 if (cpu_isar_feature(aa64_pauth, cpu)) {
2022 valid_mask |= SCR_API | SCR_APK;
2023 }
8ddb300b
RH
2024 if (cpu_isar_feature(aa64_mte, cpu)) {
2025 valid_mask |= SCR_ATA;
2026 }
ea22747c
RH
2027 } else {
2028 valid_mask &= ~(SCR_RW | SCR_ST);
2029 }
64e0e2de
EI
2030
2031 if (!arm_feature(env, ARM_FEATURE_EL2)) {
2032 valid_mask &= ~SCR_HCE;
2033
2034 /* On ARMv7, SMD (or SCD as it is called in v7) is only
2035 * supported if EL2 exists. The bit is UNK/SBZP when
2036 * EL2 is unavailable. In QEMU ARMv7, we force it to always zero
2037 * when EL2 is unavailable.
4eb27640 2038 * On ARMv8, this bit is always available.
64e0e2de 2039 */
4eb27640
GB
2040 if (arm_feature(env, ARM_FEATURE_V7) &&
2041 !arm_feature(env, ARM_FEATURE_V8)) {
64e0e2de
EI
2042 valid_mask &= ~SCR_SMD;
2043 }
2044 }
2045
2046 /* Clear all-context RES0 bits. */
2047 value &= valid_mask;
2048 raw_write(env, ri, value);
2049}
2050
630fcd4d
MZ
2051static CPAccessResult access_aa64_tid2(CPUARMState *env,
2052 const ARMCPRegInfo *ri,
2053 bool isread)
2054{
2055 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID2)) {
2056 return CP_ACCESS_TRAP_EL2;
2057 }
2058
2059 return CP_ACCESS_OK;
2060}
2061
c4241c7d 2062static uint64_t ccsidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
776d4e5c 2063{
2fc0cc0e 2064 ARMCPU *cpu = env_archcpu(env);
b85a1fd6
FA
2065
2066 /* Acquire the CSSELR index from the bank corresponding to the CCSIDR
2067 * bank
2068 */
2069 uint32_t index = A32_BANKED_REG_GET(env, csselr,
2070 ri->secure & ARM_CP_SECSTATE_S);
2071
2072 return cpu->ccsidr[index];
776d4e5c
PM
2073}
2074
c4241c7d
PM
2075static void csselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2076 uint64_t value)
776d4e5c 2077{
8d5c773e 2078 raw_write(env, ri, value & 0xf);
776d4e5c
PM
2079}
2080
1090b9c6
PM
2081static uint64_t isr_read(CPUARMState *env, const ARMCPRegInfo *ri)
2082{
29a0af61 2083 CPUState *cs = env_cpu(env);
f7778444 2084 uint64_t hcr_el2 = arm_hcr_el2_eff(env);
1090b9c6 2085 uint64_t ret = 0;
7cf95aed
MZ
2086 bool allow_virt = (arm_current_el(env) == 1 &&
2087 (!arm_is_secure_below_el3(env) ||
2088 (env->cp15.scr_el3 & SCR_EEL2)));
1090b9c6 2089
7cf95aed 2090 if (allow_virt && (hcr_el2 & HCR_IMO)) {
636540e9
PM
2091 if (cs->interrupt_request & CPU_INTERRUPT_VIRQ) {
2092 ret |= CPSR_I;
2093 }
2094 } else {
2095 if (cs->interrupt_request & CPU_INTERRUPT_HARD) {
2096 ret |= CPSR_I;
2097 }
1090b9c6 2098 }
636540e9 2099
7cf95aed 2100 if (allow_virt && (hcr_el2 & HCR_FMO)) {
636540e9
PM
2101 if (cs->interrupt_request & CPU_INTERRUPT_VFIQ) {
2102 ret |= CPSR_F;
2103 }
2104 } else {
2105 if (cs->interrupt_request & CPU_INTERRUPT_FIQ) {
2106 ret |= CPSR_F;
2107 }
1090b9c6 2108 }
636540e9 2109
1090b9c6
PM
2110 /* External aborts are not possible in QEMU so A bit is always clear */
2111 return ret;
2112}
2113
93fbc983
MZ
2114static CPAccessResult access_aa64_tid1(CPUARMState *env, const ARMCPRegInfo *ri,
2115 bool isread)
2116{
2117 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID1)) {
2118 return CP_ACCESS_TRAP_EL2;
2119 }
2120
2121 return CP_ACCESS_OK;
2122}
2123
2124static CPAccessResult access_aa32_tid1(CPUARMState *env, const ARMCPRegInfo *ri,
2125 bool isread)
2126{
2127 if (arm_feature(env, ARM_FEATURE_V8)) {
2128 return access_aa64_tid1(env, ri, isread);
2129 }
2130
2131 return CP_ACCESS_OK;
2132}
2133
e9aa6c21 2134static const ARMCPRegInfo v7_cp_reginfo[] = {
7d57f408
PM
2135 /* the old v6 WFI, UNPREDICTABLE in v7 but we choose to NOP */
2136 { .name = "NOP", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
2137 .access = PL1_W, .type = ARM_CP_NOP },
200ac0ef
PM
2138 /* Performance monitors are implementation defined in v7,
2139 * but with an ARM recommended set of registers, which we
ac689a2e 2140 * follow.
200ac0ef
PM
2141 *
2142 * Performance registers fall into three categories:
2143 * (a) always UNDEF in PL0, RW in PL1 (PMINTENSET, PMINTENCLR)
2144 * (b) RO in PL0 (ie UNDEF on write), RW in PL1 (PMUSERENR)
2145 * (c) UNDEF in PL0 if PMUSERENR.EN==0, otherwise accessible (all others)
2146 * For the cases controlled by PMUSERENR we must set .access to PL0_RW
2147 * or PL0_RO as appropriate and then check PMUSERENR in the helper fn.
2148 */
2149 { .name = "PMCNTENSET", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 1,
7a0e58fa 2150 .access = PL0_RW, .type = ARM_CP_ALIAS,
8521466b 2151 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
fcd25206
PM
2152 .writefn = pmcntenset_write,
2153 .accessfn = pmreg_access,
2154 .raw_writefn = raw_write },
8521466b
AF
2155 { .name = "PMCNTENSET_EL0", .state = ARM_CP_STATE_AA64,
2156 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 1,
2157 .access = PL0_RW, .accessfn = pmreg_access,
2158 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten), .resetvalue = 0,
2159 .writefn = pmcntenset_write, .raw_writefn = raw_write },
200ac0ef 2160 { .name = "PMCNTENCLR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 2,
8521466b
AF
2161 .access = PL0_RW,
2162 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
fcd25206
PM
2163 .accessfn = pmreg_access,
2164 .writefn = pmcntenclr_write,
7a0e58fa 2165 .type = ARM_CP_ALIAS },
8521466b
AF
2166 { .name = "PMCNTENCLR_EL0", .state = ARM_CP_STATE_AA64,
2167 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 2,
2168 .access = PL0_RW, .accessfn = pmreg_access,
7a0e58fa 2169 .type = ARM_CP_ALIAS,
8521466b
AF
2170 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten),
2171 .writefn = pmcntenclr_write },
200ac0ef 2172 { .name = "PMOVSR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 3,
f4efb4b2 2173 .access = PL0_RW, .type = ARM_CP_IO,
e4e91a21 2174 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr),
fcd25206
PM
2175 .accessfn = pmreg_access,
2176 .writefn = pmovsr_write,
2177 .raw_writefn = raw_write },
978364f1
AF
2178 { .name = "PMOVSCLR_EL0", .state = ARM_CP_STATE_AA64,
2179 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 3,
2180 .access = PL0_RW, .accessfn = pmreg_access,
f4efb4b2 2181 .type = ARM_CP_ALIAS | ARM_CP_IO,
978364f1
AF
2182 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
2183 .writefn = pmovsr_write,
2184 .raw_writefn = raw_write },
200ac0ef 2185 { .name = "PMSWINC", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 4,
f4efb4b2
AL
2186 .access = PL0_W, .accessfn = pmreg_access_swinc,
2187 .type = ARM_CP_NO_RAW | ARM_CP_IO,
0d4bfd7d
AL
2188 .writefn = pmswinc_write },
2189 { .name = "PMSWINC_EL0", .state = ARM_CP_STATE_AA64,
2190 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 4,
f4efb4b2
AL
2191 .access = PL0_W, .accessfn = pmreg_access_swinc,
2192 .type = ARM_CP_NO_RAW | ARM_CP_IO,
0d4bfd7d 2193 .writefn = pmswinc_write },
6b040780
WH
2194 { .name = "PMSELR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 5,
2195 .access = PL0_RW, .type = ARM_CP_ALIAS,
2196 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmselr),
6ecd0b6b 2197 .accessfn = pmreg_access_selr, .writefn = pmselr_write,
6b040780
WH
2198 .raw_writefn = raw_write},
2199 { .name = "PMSELR_EL0", .state = ARM_CP_STATE_AA64,
2200 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 5,
6ecd0b6b 2201 .access = PL0_RW, .accessfn = pmreg_access_selr,
6b040780
WH
2202 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmselr),
2203 .writefn = pmselr_write, .raw_writefn = raw_write, },
200ac0ef 2204 { .name = "PMCCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 0,
169c8938 2205 .access = PL0_RW, .resetvalue = 0, .type = ARM_CP_ALIAS | ARM_CP_IO,
421c7ebd 2206 .readfn = pmccntr_read, .writefn = pmccntr_write32,
6ecd0b6b 2207 .accessfn = pmreg_access_ccntr },
8521466b
AF
2208 { .name = "PMCCNTR_EL0", .state = ARM_CP_STATE_AA64,
2209 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 0,
6ecd0b6b 2210 .access = PL0_RW, .accessfn = pmreg_access_ccntr,
8521466b 2211 .type = ARM_CP_IO,
980ebe87
AL
2212 .fieldoffset = offsetof(CPUARMState, cp15.c15_ccnt),
2213 .readfn = pmccntr_read, .writefn = pmccntr_write,
2214 .raw_readfn = raw_read, .raw_writefn = raw_write, },
4b8afa1f
AL
2215 { .name = "PMCCFILTR", .cp = 15, .opc1 = 0, .crn = 14, .crm = 15, .opc2 = 7,
2216 .writefn = pmccfiltr_write_a32, .readfn = pmccfiltr_read_a32,
2217 .access = PL0_RW, .accessfn = pmreg_access,
2218 .type = ARM_CP_ALIAS | ARM_CP_IO,
2219 .resetvalue = 0, },
8521466b
AF
2220 { .name = "PMCCFILTR_EL0", .state = ARM_CP_STATE_AA64,
2221 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 15, .opc2 = 7,
980ebe87 2222 .writefn = pmccfiltr_write, .raw_writefn = raw_write,
8521466b
AF
2223 .access = PL0_RW, .accessfn = pmreg_access,
2224 .type = ARM_CP_IO,
2225 .fieldoffset = offsetof(CPUARMState, cp15.pmccfiltr_el0),
2226 .resetvalue = 0, },
200ac0ef 2227 { .name = "PMXEVTYPER", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 1,
5ecdd3e4
AL
2228 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2229 .accessfn = pmreg_access,
fdb86656
WH
2230 .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
2231 { .name = "PMXEVTYPER_EL0", .state = ARM_CP_STATE_AA64,
2232 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 1,
5ecdd3e4
AL
2233 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2234 .accessfn = pmreg_access,
fdb86656 2235 .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
200ac0ef 2236 { .name = "PMXEVCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 2,
5ecdd3e4
AL
2237 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2238 .accessfn = pmreg_access_xevcntr,
2239 .writefn = pmxevcntr_write, .readfn = pmxevcntr_read },
2240 { .name = "PMXEVCNTR_EL0", .state = ARM_CP_STATE_AA64,
2241 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 2,
2242 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2243 .accessfn = pmreg_access_xevcntr,
2244 .writefn = pmxevcntr_write, .readfn = pmxevcntr_read },
200ac0ef 2245 { .name = "PMUSERENR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 0,
1fce1ba9 2246 .access = PL0_R | PL1_RW, .accessfn = access_tpm,
e4e91a21 2247 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmuserenr),
200ac0ef 2248 .resetvalue = 0,
d4e6df63 2249 .writefn = pmuserenr_write, .raw_writefn = raw_write },
8a83ffc2
AF
2250 { .name = "PMUSERENR_EL0", .state = ARM_CP_STATE_AA64,
2251 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 0,
1fce1ba9 2252 .access = PL0_R | PL1_RW, .accessfn = access_tpm, .type = ARM_CP_ALIAS,
8a83ffc2
AF
2253 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmuserenr),
2254 .resetvalue = 0,
2255 .writefn = pmuserenr_write, .raw_writefn = raw_write },
200ac0ef 2256 { .name = "PMINTENSET", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 1,
1fce1ba9 2257 .access = PL1_RW, .accessfn = access_tpm,
b7d793ad 2258 .type = ARM_CP_ALIAS | ARM_CP_IO,
e6ec5457 2259 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pminten),
200ac0ef 2260 .resetvalue = 0,
d4e6df63 2261 .writefn = pmintenset_write, .raw_writefn = raw_write },
e6ec5457
WH
2262 { .name = "PMINTENSET_EL1", .state = ARM_CP_STATE_AA64,
2263 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 1,
2264 .access = PL1_RW, .accessfn = access_tpm,
2265 .type = ARM_CP_IO,
2266 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2267 .writefn = pmintenset_write, .raw_writefn = raw_write,
2268 .resetvalue = 0x0 },
200ac0ef 2269 { .name = "PMINTENCLR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 2,
fc5f6856
AL
2270 .access = PL1_RW, .accessfn = access_tpm,
2271 .type = ARM_CP_ALIAS | ARM_CP_IO,
200ac0ef 2272 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
b061a82b 2273 .writefn = pmintenclr_write, },
978364f1
AF
2274 { .name = "PMINTENCLR_EL1", .state = ARM_CP_STATE_AA64,
2275 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 2,
fc5f6856
AL
2276 .access = PL1_RW, .accessfn = access_tpm,
2277 .type = ARM_CP_ALIAS | ARM_CP_IO,
978364f1
AF
2278 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2279 .writefn = pmintenclr_write },
7da845b0
PM
2280 { .name = "CCSIDR", .state = ARM_CP_STATE_BOTH,
2281 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 0,
630fcd4d
MZ
2282 .access = PL1_R,
2283 .accessfn = access_aa64_tid2,
2284 .readfn = ccsidr_read, .type = ARM_CP_NO_RAW },
7da845b0
PM
2285 { .name = "CSSELR", .state = ARM_CP_STATE_BOTH,
2286 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 2, .opc2 = 0,
630fcd4d
MZ
2287 .access = PL1_RW,
2288 .accessfn = access_aa64_tid2,
2289 .writefn = csselr_write, .resetvalue = 0,
b85a1fd6
FA
2290 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.csselr_s),
2291 offsetof(CPUARMState, cp15.csselr_ns) } },
776d4e5c
PM
2292 /* Auxiliary ID register: this actually has an IMPDEF value but for now
2293 * just RAZ for all cores:
2294 */
0ff644a7
PM
2295 { .name = "AIDR", .state = ARM_CP_STATE_BOTH,
2296 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 7,
93fbc983
MZ
2297 .access = PL1_R, .type = ARM_CP_CONST,
2298 .accessfn = access_aa64_tid1,
2299 .resetvalue = 0 },
f32cdad5
PM
2300 /* Auxiliary fault status registers: these also are IMPDEF, and we
2301 * choose to RAZ/WI for all cores.
2302 */
2303 { .name = "AFSR0_EL1", .state = ARM_CP_STATE_BOTH,
2304 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 0,
84929218
RH
2305 .access = PL1_RW, .accessfn = access_tvm_trvm,
2306 .type = ARM_CP_CONST, .resetvalue = 0 },
f32cdad5
PM
2307 { .name = "AFSR1_EL1", .state = ARM_CP_STATE_BOTH,
2308 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 1,
84929218
RH
2309 .access = PL1_RW, .accessfn = access_tvm_trvm,
2310 .type = ARM_CP_CONST, .resetvalue = 0 },
b0fe2427
PM
2311 /* MAIR can just read-as-written because we don't implement caches
2312 * and so don't need to care about memory attributes.
2313 */
2314 { .name = "MAIR_EL1", .state = ARM_CP_STATE_AA64,
2315 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
84929218
RH
2316 .access = PL1_RW, .accessfn = access_tvm_trvm,
2317 .fieldoffset = offsetof(CPUARMState, cp15.mair_el[1]),
b0fe2427 2318 .resetvalue = 0 },
4cfb8ad8
PM
2319 { .name = "MAIR_EL3", .state = ARM_CP_STATE_AA64,
2320 .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 2, .opc2 = 0,
2321 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[3]),
2322 .resetvalue = 0 },
b0fe2427
PM
2323 /* For non-long-descriptor page tables these are PRRR and NMRR;
2324 * regardless they still act as reads-as-written for QEMU.
b0fe2427 2325 */
1281f8e3 2326 /* MAIR0/1 are defined separately from their 64-bit counterpart which
be693c87
GB
2327 * allows them to assign the correct fieldoffset based on the endianness
2328 * handled in the field definitions.
2329 */
a903c449 2330 { .name = "MAIR0", .state = ARM_CP_STATE_AA32,
84929218
RH
2331 .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
2332 .access = PL1_RW, .accessfn = access_tvm_trvm,
be693c87
GB
2333 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair0_s),
2334 offsetof(CPUARMState, cp15.mair0_ns) },
b0fe2427 2335 .resetfn = arm_cp_reset_ignore },
a903c449 2336 { .name = "MAIR1", .state = ARM_CP_STATE_AA32,
84929218
RH
2337 .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 1,
2338 .access = PL1_RW, .accessfn = access_tvm_trvm,
be693c87
GB
2339 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair1_s),
2340 offsetof(CPUARMState, cp15.mair1_ns) },
b0fe2427 2341 .resetfn = arm_cp_reset_ignore },
1090b9c6
PM
2342 { .name = "ISR_EL1", .state = ARM_CP_STATE_BOTH,
2343 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 0,
7a0e58fa 2344 .type = ARM_CP_NO_RAW, .access = PL1_R, .readfn = isr_read },
995939a6
PM
2345 /* 32 bit ITLB invalidates */
2346 { .name = "ITLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 0,
30881b73
RH
2347 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2348 .writefn = tlbiall_write },
995939a6 2349 { .name = "ITLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 1,
30881b73
RH
2350 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2351 .writefn = tlbimva_write },
995939a6 2352 { .name = "ITLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 2,
30881b73
RH
2353 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2354 .writefn = tlbiasid_write },
995939a6
PM
2355 /* 32 bit DTLB invalidates */
2356 { .name = "DTLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 0,
30881b73
RH
2357 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2358 .writefn = tlbiall_write },
995939a6 2359 { .name = "DTLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 1,
30881b73
RH
2360 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2361 .writefn = tlbimva_write },
995939a6 2362 { .name = "DTLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 2,
30881b73
RH
2363 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2364 .writefn = tlbiasid_write },
995939a6
PM
2365 /* 32 bit TLB invalidates */
2366 { .name = "TLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
30881b73
RH
2367 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2368 .writefn = tlbiall_write },
995939a6 2369 { .name = "TLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
30881b73
RH
2370 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2371 .writefn = tlbimva_write },
995939a6 2372 { .name = "TLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
30881b73
RH
2373 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2374 .writefn = tlbiasid_write },
995939a6 2375 { .name = "TLBIMVAA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
30881b73
RH
2376 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2377 .writefn = tlbimvaa_write },
995939a6
PM
2378 REGINFO_SENTINEL
2379};
2380
2381static const ARMCPRegInfo v7mp_cp_reginfo[] = {
2382 /* 32 bit TLB invalidates, Inner Shareable */
2383 { .name = "TLBIALLIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
30881b73
RH
2384 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2385 .writefn = tlbiall_is_write },
995939a6 2386 { .name = "TLBIMVAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
30881b73
RH
2387 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2388 .writefn = tlbimva_is_write },
995939a6 2389 { .name = "TLBIASIDIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
30881b73 2390 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
fa439fc5 2391 .writefn = tlbiasid_is_write },
995939a6 2392 { .name = "TLBIMVAAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
30881b73 2393 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
fa439fc5 2394 .writefn = tlbimvaa_is_write },
e9aa6c21
PM
2395 REGINFO_SENTINEL
2396};
2397
327dd510
AL
2398static const ARMCPRegInfo pmovsset_cp_reginfo[] = {
2399 /* PMOVSSET is not implemented in v7 before v7ve */
2400 { .name = "PMOVSSET", .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 3,
2401 .access = PL0_RW, .accessfn = pmreg_access,
f4efb4b2 2402 .type = ARM_CP_ALIAS | ARM_CP_IO,
327dd510
AL
2403 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr),
2404 .writefn = pmovsset_write,
2405 .raw_writefn = raw_write },
2406 { .name = "PMOVSSET_EL0", .state = ARM_CP_STATE_AA64,
2407 .opc0 = 3, .opc1 = 3, .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 = offsetof(CPUARMState, cp15.c9_pmovsr),
2411 .writefn = pmovsset_write,
2412 .raw_writefn = raw_write },
2413 REGINFO_SENTINEL
2414};
2415
c4241c7d
PM
2416static void teecr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2417 uint64_t value)
c326b979
PM
2418{
2419 value &= 1;
2420 env->teecr = value;
c326b979
PM
2421}
2422
3f208fd7
PM
2423static CPAccessResult teehbr_access(CPUARMState *env, const ARMCPRegInfo *ri,
2424 bool isread)
c326b979 2425{
dcbff19b 2426 if (arm_current_el(env) == 0 && (env->teecr & 1)) {
92611c00 2427 return CP_ACCESS_TRAP;
c326b979 2428 }
92611c00 2429 return CP_ACCESS_OK;
c326b979
PM
2430}
2431
2432static const ARMCPRegInfo t2ee_cp_reginfo[] = {
2433 { .name = "TEECR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 6, .opc2 = 0,
2434 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, teecr),
2435 .resetvalue = 0,
2436 .writefn = teecr_write },
2437 { .name = "TEEHBR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 6, .opc2 = 0,
2438 .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, teehbr),
92611c00 2439 .accessfn = teehbr_access, .resetvalue = 0 },
c326b979
PM
2440 REGINFO_SENTINEL
2441};
2442
4d31c596 2443static const ARMCPRegInfo v6k_cp_reginfo[] = {
e4fe830b
PM
2444 { .name = "TPIDR_EL0", .state = ARM_CP_STATE_AA64,
2445 .opc0 = 3, .opc1 = 3, .opc2 = 2, .crn = 13, .crm = 0,
2446 .access = PL0_RW,
54bf36ed 2447 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[0]), .resetvalue = 0 },
4d31c596
PM
2448 { .name = "TPIDRURW", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 2,
2449 .access = PL0_RW,
54bf36ed
FA
2450 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrurw_s),
2451 offsetoflow32(CPUARMState, cp15.tpidrurw_ns) },
e4fe830b
PM
2452 .resetfn = arm_cp_reset_ignore },
2453 { .name = "TPIDRRO_EL0", .state = ARM_CP_STATE_AA64,
2454 .opc0 = 3, .opc1 = 3, .opc2 = 3, .crn = 13, .crm = 0,
2455 .access = PL0_R|PL1_W,
54bf36ed
FA
2456 .fieldoffset = offsetof(CPUARMState, cp15.tpidrro_el[0]),
2457 .resetvalue = 0},
4d31c596
PM
2458 { .name = "TPIDRURO", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 3,
2459 .access = PL0_R|PL1_W,
54bf36ed
FA
2460 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidruro_s),
2461 offsetoflow32(CPUARMState, cp15.tpidruro_ns) },
e4fe830b 2462 .resetfn = arm_cp_reset_ignore },
54bf36ed 2463 { .name = "TPIDR_EL1", .state = ARM_CP_STATE_AA64,
e4fe830b 2464 .opc0 = 3, .opc1 = 0, .opc2 = 4, .crn = 13, .crm = 0,
4d31c596 2465 .access = PL1_RW,
54bf36ed
FA
2466 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[1]), .resetvalue = 0 },
2467 { .name = "TPIDRPRW", .opc1 = 0, .cp = 15, .crn = 13, .crm = 0, .opc2 = 4,
2468 .access = PL1_RW,
2469 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrprw_s),
2470 offsetoflow32(CPUARMState, cp15.tpidrprw_ns) },
2471 .resetvalue = 0 },
4d31c596
PM
2472 REGINFO_SENTINEL
2473};
2474
55d284af
PM
2475#ifndef CONFIG_USER_ONLY
2476
3f208fd7
PM
2477static CPAccessResult gt_cntfrq_access(CPUARMState *env, const ARMCPRegInfo *ri,
2478 bool isread)
00108f2d 2479{
75502672
PM
2480 /* CNTFRQ: not visible from PL0 if both PL0PCTEN and PL0VCTEN are zero.
2481 * Writable only at the highest implemented exception level.
2482 */
2483 int el = arm_current_el(env);
5bc84371
RH
2484 uint64_t hcr;
2485 uint32_t cntkctl;
75502672
PM
2486
2487 switch (el) {
2488 case 0:
5bc84371
RH
2489 hcr = arm_hcr_el2_eff(env);
2490 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2491 cntkctl = env->cp15.cnthctl_el2;
2492 } else {
2493 cntkctl = env->cp15.c14_cntkctl;
2494 }
2495 if (!extract32(cntkctl, 0, 2)) {
75502672
PM
2496 return CP_ACCESS_TRAP;
2497 }
2498 break;
2499 case 1:
2500 if (!isread && ri->state == ARM_CP_STATE_AA32 &&
2501 arm_is_secure_below_el3(env)) {
2502 /* Accesses from 32-bit Secure EL1 UNDEF (*not* trap to EL3!) */
2503 return CP_ACCESS_TRAP_UNCATEGORIZED;
2504 }
2505 break;
2506 case 2:
2507 case 3:
2508 break;
00108f2d 2509 }
75502672
PM
2510
2511 if (!isread && el < arm_highest_el(env)) {
2512 return CP_ACCESS_TRAP_UNCATEGORIZED;
2513 }
2514
00108f2d
PM
2515 return CP_ACCESS_OK;
2516}
2517
3f208fd7
PM
2518static CPAccessResult gt_counter_access(CPUARMState *env, int timeridx,
2519 bool isread)
00108f2d 2520{
0b6440af
EI
2521 unsigned int cur_el = arm_current_el(env);
2522 bool secure = arm_is_secure(env);
5bc84371 2523 uint64_t hcr = arm_hcr_el2_eff(env);
0b6440af 2524
5bc84371
RH
2525 switch (cur_el) {
2526 case 0:
2527 /* If HCR_EL2.<E2H,TGE> == '11': check CNTHCTL_EL2.EL0[PV]CTEN. */
2528 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2529 return (extract32(env->cp15.cnthctl_el2, timeridx, 1)
2530 ? CP_ACCESS_OK : CP_ACCESS_TRAP_EL2);
2531 }
0b6440af 2532
5bc84371
RH
2533 /* CNT[PV]CT: not visible from PL0 if EL0[PV]CTEN is zero */
2534 if (!extract32(env->cp15.c14_cntkctl, timeridx, 1)) {
2535 return CP_ACCESS_TRAP;
2536 }
2537
2538 /* If HCR_EL2.<E2H,TGE> == '10': check CNTHCTL_EL2.EL1PCTEN. */
2539 if (hcr & HCR_E2H) {
2540 if (timeridx == GTIMER_PHYS &&
2541 !extract32(env->cp15.cnthctl_el2, 10, 1)) {
2542 return CP_ACCESS_TRAP_EL2;
2543 }
2544 } else {
2545 /* If HCR_EL2.<E2H> == 0: check CNTHCTL_EL2.EL1PCEN. */
2546 if (arm_feature(env, ARM_FEATURE_EL2) &&
2547 timeridx == GTIMER_PHYS && !secure &&
2548 !extract32(env->cp15.cnthctl_el2, 1, 1)) {
2549 return CP_ACCESS_TRAP_EL2;
2550 }
2551 }
2552 break;
2553
2554 case 1:
2555 /* Check CNTHCTL_EL2.EL1PCTEN, which changes location based on E2H. */
2556 if (arm_feature(env, ARM_FEATURE_EL2) &&
2557 timeridx == GTIMER_PHYS && !secure &&
2558 (hcr & HCR_E2H
2559 ? !extract32(env->cp15.cnthctl_el2, 10, 1)
2560 : !extract32(env->cp15.cnthctl_el2, 0, 1))) {
2561 return CP_ACCESS_TRAP_EL2;
2562 }
2563 break;
0b6440af 2564 }
00108f2d
PM
2565 return CP_ACCESS_OK;
2566}
2567
3f208fd7
PM
2568static CPAccessResult gt_timer_access(CPUARMState *env, int timeridx,
2569 bool isread)
00108f2d 2570{
0b6440af
EI
2571 unsigned int cur_el = arm_current_el(env);
2572 bool secure = arm_is_secure(env);
5bc84371 2573 uint64_t hcr = arm_hcr_el2_eff(env);
0b6440af 2574
5bc84371
RH
2575 switch (cur_el) {
2576 case 0:
2577 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2578 /* If HCR_EL2.<E2H,TGE> == '11': check CNTHCTL_EL2.EL0[PV]TEN. */
2579 return (extract32(env->cp15.cnthctl_el2, 9 - timeridx, 1)
2580 ? CP_ACCESS_OK : CP_ACCESS_TRAP_EL2);
2581 }
0b6440af 2582
5bc84371
RH
2583 /*
2584 * CNT[PV]_CVAL, CNT[PV]_CTL, CNT[PV]_TVAL: not visible from
2585 * EL0 if EL0[PV]TEN is zero.
2586 */
2587 if (!extract32(env->cp15.c14_cntkctl, 9 - timeridx, 1)) {
2588 return CP_ACCESS_TRAP;
2589 }
2590 /* fall through */
2591
2592 case 1:
2593 if (arm_feature(env, ARM_FEATURE_EL2) &&
2594 timeridx == GTIMER_PHYS && !secure) {
2595 if (hcr & HCR_E2H) {
2596 /* If HCR_EL2.<E2H,TGE> == '10': check CNTHCTL_EL2.EL1PTEN. */
2597 if (!extract32(env->cp15.cnthctl_el2, 11, 1)) {
2598 return CP_ACCESS_TRAP_EL2;
2599 }
2600 } else {
2601 /* If HCR_EL2.<E2H> == 0: check CNTHCTL_EL2.EL1PCEN. */
2602 if (!extract32(env->cp15.cnthctl_el2, 1, 1)) {
2603 return CP_ACCESS_TRAP_EL2;
2604 }
2605 }
2606 }
2607 break;
0b6440af 2608 }
00108f2d
PM
2609 return CP_ACCESS_OK;
2610}
2611
2612static CPAccessResult gt_pct_access(CPUARMState *env,
3f208fd7
PM
2613 const ARMCPRegInfo *ri,
2614 bool isread)
00108f2d 2615{
3f208fd7 2616 return gt_counter_access(env, GTIMER_PHYS, isread);
00108f2d
PM
2617}
2618
2619static CPAccessResult gt_vct_access(CPUARMState *env,
3f208fd7
PM
2620 const ARMCPRegInfo *ri,
2621 bool isread)
00108f2d 2622{
3f208fd7 2623 return gt_counter_access(env, GTIMER_VIRT, isread);
00108f2d
PM
2624}
2625
3f208fd7
PM
2626static CPAccessResult gt_ptimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
2627 bool isread)
00108f2d 2628{
3f208fd7 2629 return gt_timer_access(env, GTIMER_PHYS, isread);
00108f2d
PM
2630}
2631
3f208fd7
PM
2632static CPAccessResult gt_vtimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
2633 bool isread)
00108f2d 2634{
3f208fd7 2635 return gt_timer_access(env, GTIMER_VIRT, isread);
00108f2d
PM
2636}
2637
b4d3978c 2638static CPAccessResult gt_stimer_access(CPUARMState *env,
3f208fd7
PM
2639 const ARMCPRegInfo *ri,
2640 bool isread)
b4d3978c
PM
2641{
2642 /* The AArch64 register view of the secure physical timer is
2643 * always accessible from EL3, and configurably accessible from
2644 * Secure EL1.
2645 */
2646 switch (arm_current_el(env)) {
2647 case 1:
2648 if (!arm_is_secure(env)) {
2649 return CP_ACCESS_TRAP;
2650 }
2651 if (!(env->cp15.scr_el3 & SCR_ST)) {
2652 return CP_ACCESS_TRAP_EL3;
2653 }
2654 return CP_ACCESS_OK;
2655 case 0:
2656 case 2:
2657 return CP_ACCESS_TRAP;
2658 case 3:
2659 return CP_ACCESS_OK;
2660 default:
2661 g_assert_not_reached();
2662 }
2663}
2664
55d284af
PM
2665static uint64_t gt_get_countervalue(CPUARMState *env)
2666{
7def8754
AJ
2667 ARMCPU *cpu = env_archcpu(env);
2668
2669 return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) / gt_cntfrq_period_ns(cpu);
55d284af
PM
2670}
2671
2672static void gt_recalc_timer(ARMCPU *cpu, int timeridx)
2673{
2674 ARMGenericTimer *gt = &cpu->env.cp15.c14_timer[timeridx];
2675
2676 if (gt->ctl & 1) {
2677 /* Timer enabled: calculate and set current ISTATUS, irq, and
2678 * reset timer to when ISTATUS next has to change
2679 */
edac4d8a
EI
2680 uint64_t offset = timeridx == GTIMER_VIRT ?
2681 cpu->env.cp15.cntvoff_el2 : 0;
55d284af
PM
2682 uint64_t count = gt_get_countervalue(&cpu->env);
2683 /* Note that this must be unsigned 64 bit arithmetic: */
edac4d8a 2684 int istatus = count - offset >= gt->cval;
55d284af 2685 uint64_t nexttick;
194cbc49 2686 int irqstate;
55d284af
PM
2687
2688 gt->ctl = deposit32(gt->ctl, 2, 1, istatus);
194cbc49
PM
2689
2690 irqstate = (istatus && !(gt->ctl & 2));
2691 qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate);
2692
55d284af
PM
2693 if (istatus) {
2694 /* Next transition is when count rolls back over to zero */
2695 nexttick = UINT64_MAX;
2696 } else {
2697 /* Next transition is when we hit cval */
edac4d8a 2698 nexttick = gt->cval + offset;
55d284af
PM
2699 }
2700 /* Note that the desired next expiry time might be beyond the
2701 * signed-64-bit range of a QEMUTimer -- in this case we just
2702 * set the timer for as far in the future as possible. When the
2703 * timer expires we will reset the timer for any remaining period.
2704 */
7def8754 2705 if (nexttick > INT64_MAX / gt_cntfrq_period_ns(cpu)) {
4a0245b6
AJ
2706 timer_mod_ns(cpu->gt_timer[timeridx], INT64_MAX);
2707 } else {
2708 timer_mod(cpu->gt_timer[timeridx], nexttick);
55d284af 2709 }
194cbc49 2710 trace_arm_gt_recalc(timeridx, irqstate, nexttick);
55d284af
PM
2711 } else {
2712 /* Timer disabled: ISTATUS and timer output always clear */
2713 gt->ctl &= ~4;
2714 qemu_set_irq(cpu->gt_timer_outputs[timeridx], 0);
bc72ad67 2715 timer_del(cpu->gt_timer[timeridx]);
194cbc49 2716 trace_arm_gt_recalc_disabled(timeridx);
55d284af
PM
2717 }
2718}
2719
0e3eca4c
EI
2720static void gt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri,
2721 int timeridx)
55d284af 2722{
2fc0cc0e 2723 ARMCPU *cpu = env_archcpu(env);
55d284af 2724
bc72ad67 2725 timer_del(cpu->gt_timer[timeridx]);
55d284af
PM
2726}
2727
c4241c7d 2728static uint64_t gt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
55d284af 2729{
c4241c7d 2730 return gt_get_countervalue(env);
55d284af
PM
2731}
2732
53d1f856
RH
2733static uint64_t gt_virt_cnt_offset(CPUARMState *env)
2734{
2735 uint64_t hcr;
2736
2737 switch (arm_current_el(env)) {
2738 case 2:
2739 hcr = arm_hcr_el2_eff(env);
2740 if (hcr & HCR_E2H) {
2741 return 0;
2742 }
2743 break;
2744 case 0:
2745 hcr = arm_hcr_el2_eff(env);
2746 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2747 return 0;
2748 }
2749 break;
2750 }
2751
2752 return env->cp15.cntvoff_el2;
2753}
2754
edac4d8a
EI
2755static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
2756{
53d1f856 2757 return gt_get_countervalue(env) - gt_virt_cnt_offset(env);
edac4d8a
EI
2758}
2759
c4241c7d 2760static void gt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
0e3eca4c 2761 int timeridx,
c4241c7d 2762 uint64_t value)
55d284af 2763{
194cbc49 2764 trace_arm_gt_cval_write(timeridx, value);
55d284af 2765 env->cp15.c14_timer[timeridx].cval = value;
2fc0cc0e 2766 gt_recalc_timer(env_archcpu(env), timeridx);
55d284af 2767}
c4241c7d 2768
0e3eca4c
EI
2769static uint64_t gt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri,
2770 int timeridx)
55d284af 2771{
53d1f856
RH
2772 uint64_t offset = 0;
2773
2774 switch (timeridx) {
2775 case GTIMER_VIRT:
8c94b071 2776 case GTIMER_HYPVIRT:
53d1f856
RH
2777 offset = gt_virt_cnt_offset(env);
2778 break;
2779 }
55d284af 2780
c4241c7d 2781 return (uint32_t)(env->cp15.c14_timer[timeridx].cval -
edac4d8a 2782 (gt_get_countervalue(env) - offset));
55d284af
PM
2783}
2784
c4241c7d 2785static void gt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
0e3eca4c 2786 int timeridx,
c4241c7d 2787 uint64_t value)
55d284af 2788{
53d1f856
RH
2789 uint64_t offset = 0;
2790
2791 switch (timeridx) {
2792 case GTIMER_VIRT:
8c94b071 2793 case GTIMER_HYPVIRT:
53d1f856
RH
2794 offset = gt_virt_cnt_offset(env);
2795 break;
2796 }
55d284af 2797
194cbc49 2798 trace_arm_gt_tval_write(timeridx, value);
edac4d8a 2799 env->cp15.c14_timer[timeridx].cval = gt_get_countervalue(env) - offset +
18084b2f 2800 sextract64(value, 0, 32);
2fc0cc0e 2801 gt_recalc_timer(env_archcpu(env), timeridx);
55d284af
PM
2802}
2803
c4241c7d 2804static void gt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
0e3eca4c 2805 int timeridx,
c4241c7d 2806 uint64_t value)
55d284af 2807{
2fc0cc0e 2808 ARMCPU *cpu = env_archcpu(env);
55d284af
PM
2809 uint32_t oldval = env->cp15.c14_timer[timeridx].ctl;
2810
194cbc49 2811 trace_arm_gt_ctl_write(timeridx, value);
d3afacc7 2812 env->cp15.c14_timer[timeridx].ctl = deposit64(oldval, 0, 2, value);
55d284af
PM
2813 if ((oldval ^ value) & 1) {
2814 /* Enable toggled */
2815 gt_recalc_timer(cpu, timeridx);
d3afacc7 2816 } else if ((oldval ^ value) & 2) {
55d284af
PM
2817 /* IMASK toggled: don't need to recalculate,
2818 * just set the interrupt line based on ISTATUS
2819 */
194cbc49
PM
2820 int irqstate = (oldval & 4) && !(value & 2);
2821
2822 trace_arm_gt_imask_toggle(timeridx, irqstate);
2823 qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate);
55d284af 2824 }
55d284af
PM
2825}
2826
0e3eca4c
EI
2827static void gt_phys_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2828{
2829 gt_timer_reset(env, ri, GTIMER_PHYS);
2830}
2831
2832static void gt_phys_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2833 uint64_t value)
2834{
2835 gt_cval_write(env, ri, GTIMER_PHYS, value);
2836}
2837
2838static uint64_t gt_phys_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2839{
2840 return gt_tval_read(env, ri, GTIMER_PHYS);
2841}
2842
2843static void gt_phys_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2844 uint64_t value)
2845{
2846 gt_tval_write(env, ri, GTIMER_PHYS, value);
2847}
2848
2849static void gt_phys_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2850 uint64_t value)
2851{
2852 gt_ctl_write(env, ri, GTIMER_PHYS, value);
2853}
2854
bb5972e4
RH
2855static int gt_phys_redir_timeridx(CPUARMState *env)
2856{
2857 switch (arm_mmu_idx(env)) {
2858 case ARMMMUIdx_E20_0:
2859 case ARMMMUIdx_E20_2:
452ef8cb 2860 case ARMMMUIdx_E20_2_PAN:
bb5972e4
RH
2861 return GTIMER_HYP;
2862 default:
2863 return GTIMER_PHYS;
2864 }
2865}
2866
2867static int gt_virt_redir_timeridx(CPUARMState *env)
2868{
2869 switch (arm_mmu_idx(env)) {
2870 case ARMMMUIdx_E20_0:
2871 case ARMMMUIdx_E20_2:
452ef8cb 2872 case ARMMMUIdx_E20_2_PAN:
bb5972e4
RH
2873 return GTIMER_HYPVIRT;
2874 default:
2875 return GTIMER_VIRT;
2876 }
2877}
2878
2879static uint64_t gt_phys_redir_cval_read(CPUARMState *env,
2880 const ARMCPRegInfo *ri)
2881{
2882 int timeridx = gt_phys_redir_timeridx(env);
2883 return env->cp15.c14_timer[timeridx].cval;
2884}
2885
2886static void gt_phys_redir_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2887 uint64_t value)
2888{
2889 int timeridx = gt_phys_redir_timeridx(env);
2890 gt_cval_write(env, ri, timeridx, value);
2891}
2892
2893static uint64_t gt_phys_redir_tval_read(CPUARMState *env,
2894 const ARMCPRegInfo *ri)
2895{
2896 int timeridx = gt_phys_redir_timeridx(env);
2897 return gt_tval_read(env, ri, timeridx);
2898}
2899
2900static void gt_phys_redir_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2901 uint64_t value)
2902{
2903 int timeridx = gt_phys_redir_timeridx(env);
2904 gt_tval_write(env, ri, timeridx, value);
2905}
2906
2907static uint64_t gt_phys_redir_ctl_read(CPUARMState *env,
2908 const ARMCPRegInfo *ri)
2909{
2910 int timeridx = gt_phys_redir_timeridx(env);
2911 return env->cp15.c14_timer[timeridx].ctl;
2912}
2913
2914static void gt_phys_redir_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2915 uint64_t value)
2916{
2917 int timeridx = gt_phys_redir_timeridx(env);
2918 gt_ctl_write(env, ri, timeridx, value);
2919}
2920
0e3eca4c
EI
2921static void gt_virt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2922{
2923 gt_timer_reset(env, ri, GTIMER_VIRT);
2924}
2925
2926static void gt_virt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2927 uint64_t value)
2928{
2929 gt_cval_write(env, ri, GTIMER_VIRT, value);
2930}
2931
2932static uint64_t gt_virt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2933{
2934 return gt_tval_read(env, ri, GTIMER_VIRT);
2935}
2936
2937static void gt_virt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2938 uint64_t value)
2939{
2940 gt_tval_write(env, ri, GTIMER_VIRT, value);
2941}
2942
2943static void gt_virt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2944 uint64_t value)
2945{
2946 gt_ctl_write(env, ri, GTIMER_VIRT, value);
2947}
2948
edac4d8a
EI
2949static void gt_cntvoff_write(CPUARMState *env, const ARMCPRegInfo *ri,
2950 uint64_t value)
2951{
2fc0cc0e 2952 ARMCPU *cpu = env_archcpu(env);
edac4d8a 2953
194cbc49 2954 trace_arm_gt_cntvoff_write(value);
edac4d8a
EI
2955 raw_write(env, ri, value);
2956 gt_recalc_timer(cpu, GTIMER_VIRT);
2957}
2958
bb5972e4
RH
2959static uint64_t gt_virt_redir_cval_read(CPUARMState *env,
2960 const ARMCPRegInfo *ri)
2961{
2962 int timeridx = gt_virt_redir_timeridx(env);
2963 return env->cp15.c14_timer[timeridx].cval;
2964}
2965
2966static void gt_virt_redir_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2967 uint64_t value)
2968{
2969 int timeridx = gt_virt_redir_timeridx(env);
2970 gt_cval_write(env, ri, timeridx, value);
2971}
2972
2973static uint64_t gt_virt_redir_tval_read(CPUARMState *env,
2974 const ARMCPRegInfo *ri)
2975{
2976 int timeridx = gt_virt_redir_timeridx(env);
2977 return gt_tval_read(env, ri, timeridx);
2978}
2979
2980static void gt_virt_redir_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2981 uint64_t value)
2982{
2983 int timeridx = gt_virt_redir_timeridx(env);
2984 gt_tval_write(env, ri, timeridx, value);
2985}
2986
2987static uint64_t gt_virt_redir_ctl_read(CPUARMState *env,
2988 const ARMCPRegInfo *ri)
2989{
2990 int timeridx = gt_virt_redir_timeridx(env);
2991 return env->cp15.c14_timer[timeridx].ctl;
2992}
2993
2994static void gt_virt_redir_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2995 uint64_t value)
2996{
2997 int timeridx = gt_virt_redir_timeridx(env);
2998 gt_ctl_write(env, ri, timeridx, value);
2999}
3000
b0e66d95
EI
3001static void gt_hyp_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
3002{
3003 gt_timer_reset(env, ri, GTIMER_HYP);
3004}
3005
3006static void gt_hyp_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3007 uint64_t value)
3008{
3009 gt_cval_write(env, ri, GTIMER_HYP, value);
3010}
3011
3012static uint64_t gt_hyp_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
3013{
3014 return gt_tval_read(env, ri, GTIMER_HYP);
3015}
3016
3017static void gt_hyp_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3018 uint64_t value)
3019{
3020 gt_tval_write(env, ri, GTIMER_HYP, value);
3021}
3022
3023static void gt_hyp_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
3024 uint64_t value)
3025{
3026 gt_ctl_write(env, ri, GTIMER_HYP, value);
3027}
3028
b4d3978c
PM
3029static void gt_sec_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
3030{
3031 gt_timer_reset(env, ri, GTIMER_SEC);
3032}
3033
3034static void gt_sec_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3035 uint64_t value)
3036{
3037 gt_cval_write(env, ri, GTIMER_SEC, value);
3038}
3039
3040static uint64_t gt_sec_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
3041{
3042 return gt_tval_read(env, ri, GTIMER_SEC);
3043}
3044
3045static void gt_sec_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3046 uint64_t value)
3047{
3048 gt_tval_write(env, ri, GTIMER_SEC, value);
3049}
3050
3051static void gt_sec_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
3052 uint64_t value)
3053{
3054 gt_ctl_write(env, ri, GTIMER_SEC, value);
3055}
3056
8c94b071
RH
3057static void gt_hv_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
3058{
3059 gt_timer_reset(env, ri, GTIMER_HYPVIRT);
3060}
3061
3062static void gt_hv_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3063 uint64_t value)
3064{
3065 gt_cval_write(env, ri, GTIMER_HYPVIRT, value);
3066}
3067
3068static uint64_t gt_hv_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
3069{
3070 return gt_tval_read(env, ri, GTIMER_HYPVIRT);
3071}
3072
3073static void gt_hv_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3074 uint64_t value)
3075{
3076 gt_tval_write(env, ri, GTIMER_HYPVIRT, value);
3077}
3078
3079static void gt_hv_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
3080 uint64_t value)
3081{
3082 gt_ctl_write(env, ri, GTIMER_HYPVIRT, value);
3083}
3084
55d284af
PM
3085void arm_gt_ptimer_cb(void *opaque)
3086{
3087 ARMCPU *cpu = opaque;
3088
3089 gt_recalc_timer(cpu, GTIMER_PHYS);
3090}
3091
3092void arm_gt_vtimer_cb(void *opaque)
3093{
3094 ARMCPU *cpu = opaque;
3095
3096 gt_recalc_timer(cpu, GTIMER_VIRT);
3097}
3098
b0e66d95
EI
3099void arm_gt_htimer_cb(void *opaque)
3100{
3101 ARMCPU *cpu = opaque;
3102
3103 gt_recalc_timer(cpu, GTIMER_HYP);
3104}
3105
b4d3978c
PM
3106void arm_gt_stimer_cb(void *opaque)
3107{
3108 ARMCPU *cpu = opaque;
3109
3110 gt_recalc_timer(cpu, GTIMER_SEC);
3111}
3112
8c94b071
RH
3113void arm_gt_hvtimer_cb(void *opaque)
3114{
3115 ARMCPU *cpu = opaque;
3116
3117 gt_recalc_timer(cpu, GTIMER_HYPVIRT);
3118}
3119
96eec6b2
AJ
3120static void arm_gt_cntfrq_reset(CPUARMState *env, const ARMCPRegInfo *opaque)
3121{
3122 ARMCPU *cpu = env_archcpu(env);
3123
3124 cpu->env.cp15.c14_cntfrq = cpu->gt_cntfrq_hz;
3125}
3126
55d284af
PM
3127static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
3128 /* Note that CNTFRQ is purely reads-as-written for the benefit
3129 * of software; writing it doesn't actually change the timer frequency.
3130 * Our reset value matches the fixed frequency we implement the timer at.
3131 */
3132 { .name = "CNTFRQ", .cp = 15, .crn = 14, .crm = 0, .opc1 = 0, .opc2 = 0,
7a0e58fa 3133 .type = ARM_CP_ALIAS,
a7adc4b7
PM
3134 .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
3135 .fieldoffset = offsetoflow32(CPUARMState, cp15.c14_cntfrq),
a7adc4b7
PM
3136 },
3137 { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
3138 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
3139 .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
55d284af 3140 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
96eec6b2 3141 .resetfn = arm_gt_cntfrq_reset,
55d284af
PM
3142 },
3143 /* overall control: mostly access permissions */
a7adc4b7
PM
3144 { .name = "CNTKCTL", .state = ARM_CP_STATE_BOTH,
3145 .opc0 = 3, .opc1 = 0, .crn = 14, .crm = 1, .opc2 = 0,
55d284af
PM
3146 .access = PL1_RW,
3147 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntkctl),
3148 .resetvalue = 0,
3149 },
3150 /* per-timer control */
3151 { .name = "CNTP_CTL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
9ff9dd3c 3152 .secure = ARM_CP_SECSTATE_NS,
daf1dc5f 3153 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
a7adc4b7
PM
3154 .accessfn = gt_ptimer_access,
3155 .fieldoffset = offsetoflow32(CPUARMState,
3156 cp15.c14_timer[GTIMER_PHYS].ctl),
bb5972e4
RH
3157 .readfn = gt_phys_redir_ctl_read, .raw_readfn = raw_read,
3158 .writefn = gt_phys_redir_ctl_write, .raw_writefn = raw_write,
a7adc4b7 3159 },
9c513e78 3160 { .name = "CNTP_CTL_S",
9ff9dd3c
PM
3161 .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
3162 .secure = ARM_CP_SECSTATE_S,
daf1dc5f 3163 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
9ff9dd3c
PM
3164 .accessfn = gt_ptimer_access,
3165 .fieldoffset = offsetoflow32(CPUARMState,
3166 cp15.c14_timer[GTIMER_SEC].ctl),
3167 .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
3168 },
a7adc4b7
PM
3169 { .name = "CNTP_CTL_EL0", .state = ARM_CP_STATE_AA64,
3170 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 1,
daf1dc5f 3171 .type = ARM_CP_IO, .access = PL0_RW,
a7adc4b7 3172 .accessfn = gt_ptimer_access,
55d284af
PM
3173 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
3174 .resetvalue = 0,
bb5972e4
RH
3175 .readfn = gt_phys_redir_ctl_read, .raw_readfn = raw_read,
3176 .writefn = gt_phys_redir_ctl_write, .raw_writefn = raw_write,
55d284af
PM
3177 },
3178 { .name = "CNTV_CTL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 1,
daf1dc5f 3179 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
a7adc4b7
PM
3180 .accessfn = gt_vtimer_access,
3181 .fieldoffset = offsetoflow32(CPUARMState,
3182 cp15.c14_timer[GTIMER_VIRT].ctl),
bb5972e4
RH
3183 .readfn = gt_virt_redir_ctl_read, .raw_readfn = raw_read,
3184 .writefn = gt_virt_redir_ctl_write, .raw_writefn = raw_write,
a7adc4b7
PM
3185 },
3186 { .name = "CNTV_CTL_EL0", .state = ARM_CP_STATE_AA64,
3187 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 1,
daf1dc5f 3188 .type = ARM_CP_IO, .access = PL0_RW,
a7adc4b7 3189 .accessfn = gt_vtimer_access,
55d284af
PM
3190 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
3191 .resetvalue = 0,
bb5972e4
RH
3192 .readfn = gt_virt_redir_ctl_read, .raw_readfn = raw_read,
3193 .writefn = gt_virt_redir_ctl_write, .raw_writefn = raw_write,
55d284af
PM
3194 },
3195 /* TimerValue views: a 32 bit downcounting view of the underlying state */
3196 { .name = "CNTP_TVAL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
9ff9dd3c 3197 .secure = ARM_CP_SECSTATE_NS,
daf1dc5f 3198 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
00108f2d 3199 .accessfn = gt_ptimer_access,
bb5972e4 3200 .readfn = gt_phys_redir_tval_read, .writefn = gt_phys_redir_tval_write,
55d284af 3201 },
9c513e78 3202 { .name = "CNTP_TVAL_S",
9ff9dd3c
PM
3203 .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
3204 .secure = ARM_CP_SECSTATE_S,
daf1dc5f 3205 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
9ff9dd3c
PM
3206 .accessfn = gt_ptimer_access,
3207 .readfn = gt_sec_tval_read, .writefn = gt_sec_tval_write,
3208 },
a7adc4b7
PM
3209 { .name = "CNTP_TVAL_EL0", .state = ARM_CP_STATE_AA64,
3210 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 0,
daf1dc5f 3211 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
0e3eca4c 3212 .accessfn = gt_ptimer_access, .resetfn = gt_phys_timer_reset,
bb5972e4 3213 .readfn = gt_phys_redir_tval_read, .writefn = gt_phys_redir_tval_write,
a7adc4b7 3214 },
55d284af 3215 { .name = "CNTV_TVAL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 0,
daf1dc5f 3216 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
00108f2d 3217 .accessfn = gt_vtimer_access,
bb5972e4 3218 .readfn = gt_virt_redir_tval_read, .writefn = gt_virt_redir_tval_write,
55d284af 3219 },
a7adc4b7
PM
3220 { .name = "CNTV_TVAL_EL0", .state = ARM_CP_STATE_AA64,
3221 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 0,
daf1dc5f 3222 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
0e3eca4c 3223 .accessfn = gt_vtimer_access, .resetfn = gt_virt_timer_reset,
bb5972e4 3224 .readfn = gt_virt_redir_tval_read, .writefn = gt_virt_redir_tval_write,
a7adc4b7 3225 },
55d284af
PM
3226 /* The counter itself */
3227 { .name = "CNTPCT", .cp = 15, .crm = 14, .opc1 = 0,
7a0e58fa 3228 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
00108f2d 3229 .accessfn = gt_pct_access,
a7adc4b7
PM
3230 .readfn = gt_cnt_read, .resetfn = arm_cp_reset_ignore,
3231 },
3232 { .name = "CNTPCT_EL0", .state = ARM_CP_STATE_AA64,
3233 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 1,
7a0e58fa 3234 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
d57b9ee8 3235 .accessfn = gt_pct_access, .readfn = gt_cnt_read,
55d284af
PM
3236 },
3237 { .name = "CNTVCT", .cp = 15, .crm = 14, .opc1 = 1,
7a0e58fa 3238 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
00108f2d 3239 .accessfn = gt_vct_access,
edac4d8a 3240 .readfn = gt_virt_cnt_read, .resetfn = arm_cp_reset_ignore,
a7adc4b7
PM
3241 },
3242 { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
3243 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
7a0e58fa 3244 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
d57b9ee8 3245 .accessfn = gt_vct_access, .readfn = gt_virt_cnt_read,
55d284af
PM
3246 },
3247 /* Comparison value, indicating when the timer goes off */
3248 { .name = "CNTP_CVAL", .cp = 15, .crm = 14, .opc1 = 2,
9ff9dd3c 3249 .secure = ARM_CP_SECSTATE_NS,
daf1dc5f 3250 .access = PL0_RW,
7a0e58fa 3251 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
55d284af 3252 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
b061a82b 3253 .accessfn = gt_ptimer_access,
bb5972e4
RH
3254 .readfn = gt_phys_redir_cval_read, .raw_readfn = raw_read,
3255 .writefn = gt_phys_redir_cval_write, .raw_writefn = raw_write,
a7adc4b7 3256 },
9c513e78 3257 { .name = "CNTP_CVAL_S", .cp = 15, .crm = 14, .opc1 = 2,
9ff9dd3c 3258 .secure = ARM_CP_SECSTATE_S,
daf1dc5f 3259 .access = PL0_RW,
9ff9dd3c
PM
3260 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
3261 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
3262 .accessfn = gt_ptimer_access,
3263 .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
3264 },
a7adc4b7
PM
3265 { .name = "CNTP_CVAL_EL0", .state = ARM_CP_STATE_AA64,
3266 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 2,
daf1dc5f 3267 .access = PL0_RW,
a7adc4b7
PM
3268 .type = ARM_CP_IO,
3269 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
12cde08a 3270 .resetvalue = 0, .accessfn = gt_ptimer_access,
bb5972e4
RH
3271 .readfn = gt_phys_redir_cval_read, .raw_readfn = raw_read,
3272 .writefn = gt_phys_redir_cval_write, .raw_writefn = raw_write,
55d284af
PM
3273 },
3274 { .name = "CNTV_CVAL", .cp = 15, .crm = 14, .opc1 = 3,
daf1dc5f 3275 .access = PL0_RW,
7a0e58fa 3276 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
55d284af 3277 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
b061a82b 3278 .accessfn = gt_vtimer_access,
bb5972e4
RH
3279 .readfn = gt_virt_redir_cval_read, .raw_readfn = raw_read,
3280 .writefn = gt_virt_redir_cval_write, .raw_writefn = raw_write,
a7adc4b7
PM
3281 },
3282 { .name = "CNTV_CVAL_EL0", .state = ARM_CP_STATE_AA64,
3283 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 2,
daf1dc5f 3284 .access = PL0_RW,
a7adc4b7
PM
3285 .type = ARM_CP_IO,
3286 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
3287 .resetvalue = 0, .accessfn = gt_vtimer_access,
bb5972e4
RH
3288 .readfn = gt_virt_redir_cval_read, .raw_readfn = raw_read,
3289 .writefn = gt_virt_redir_cval_write, .raw_writefn = raw_write,
55d284af 3290 },
b4d3978c
PM
3291 /* Secure timer -- this is actually restricted to only EL3
3292 * and configurably Secure-EL1 via the accessfn.
3293 */
3294 { .name = "CNTPS_TVAL_EL1", .state = ARM_CP_STATE_AA64,
3295 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 0,
3296 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW,
3297 .accessfn = gt_stimer_access,
3298 .readfn = gt_sec_tval_read,
3299 .writefn = gt_sec_tval_write,
3300 .resetfn = gt_sec_timer_reset,
3301 },
3302 { .name = "CNTPS_CTL_EL1", .state = ARM_CP_STATE_AA64,
3303 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 1,
3304 .type = ARM_CP_IO, .access = PL1_RW,
3305 .accessfn = gt_stimer_access,
3306 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].ctl),
3307 .resetvalue = 0,
3308 .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
3309 },
3310 { .name = "CNTPS_CVAL_EL1", .state = ARM_CP_STATE_AA64,
3311 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 2,
3312 .type = ARM_CP_IO, .access = PL1_RW,
3313 .accessfn = gt_stimer_access,
3314 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
3315 .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
3316 },
55d284af
PM
3317 REGINFO_SENTINEL
3318};
3319
bb5972e4
RH
3320static CPAccessResult e2h_access(CPUARMState *env, const ARMCPRegInfo *ri,
3321 bool isread)
3322{
3323 if (!(arm_hcr_el2_eff(env) & HCR_E2H)) {
3324 return CP_ACCESS_TRAP;
3325 }
3326 return CP_ACCESS_OK;
3327}
3328
55d284af 3329#else
26c4a83b
AB
3330
3331/* In user-mode most of the generic timer registers are inaccessible
3332 * however modern kernels (4.12+) allow access to cntvct_el0
55d284af 3333 */
26c4a83b
AB
3334
3335static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
3336{
7def8754
AJ
3337 ARMCPU *cpu = env_archcpu(env);
3338
26c4a83b
AB
3339 /* Currently we have no support for QEMUTimer in linux-user so we
3340 * can't call gt_get_countervalue(env), instead we directly
3341 * call the lower level functions.
3342 */
7def8754 3343 return cpu_get_clock() / gt_cntfrq_period_ns(cpu);
26c4a83b
AB
3344}
3345
6cc7a3ae 3346static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
26c4a83b
AB
3347 { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
3348 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
3349 .type = ARM_CP_CONST, .access = PL0_R /* no PL1_RW in linux-user */,
3350 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
3351 .resetvalue = NANOSECONDS_PER_SECOND / GTIMER_SCALE,
3352 },
3353 { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
3354 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
3355 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3356 .readfn = gt_virt_cnt_read,
3357 },
6cc7a3ae
PM
3358 REGINFO_SENTINEL
3359};
3360
55d284af
PM
3361#endif
3362
c4241c7d 3363static void par_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
4a501606 3364{
891a2fe7 3365 if (arm_feature(env, ARM_FEATURE_LPAE)) {
8d5c773e 3366 raw_write(env, ri, value);
891a2fe7 3367 } else if (arm_feature(env, ARM_FEATURE_V7)) {
8d5c773e 3368 raw_write(env, ri, value & 0xfffff6ff);
4a501606 3369 } else {
8d5c773e 3370 raw_write(env, ri, value & 0xfffff1ff);
4a501606 3371 }
4a501606
PM
3372}
3373
3374#ifndef CONFIG_USER_ONLY
3375/* get_phys_addr() isn't present for user-mode-only targets */
702a9357 3376
3f208fd7
PM
3377static CPAccessResult ats_access(CPUARMState *env, const ARMCPRegInfo *ri,
3378 bool isread)
92611c00
PM
3379{
3380 if (ri->opc2 & 4) {
87562e4f
PM
3381 /* The ATS12NSO* operations must trap to EL3 if executed in
3382 * Secure EL1 (which can only happen if EL3 is AArch64).
3383 * They are simply UNDEF if executed from NS EL1.
3384 * They function normally from EL2 or EL3.
92611c00 3385 */
87562e4f
PM
3386 if (arm_current_el(env) == 1) {
3387 if (arm_is_secure_below_el3(env)) {
3388 return CP_ACCESS_TRAP_UNCATEGORIZED_EL3;
3389 }
3390 return CP_ACCESS_TRAP_UNCATEGORIZED;
3391 }
92611c00
PM
3392 }
3393 return CP_ACCESS_OK;
3394}
3395
9fb005b0 3396#ifdef CONFIG_TCG
060e8a48 3397static uint64_t do_ats_write(CPUARMState *env, uint64_t value,
03ae85f8 3398 MMUAccessType access_type, ARMMMUIdx mmu_idx)
4a501606 3399{
a8170e5e 3400 hwaddr phys_addr;
4a501606
PM
3401 target_ulong page_size;
3402 int prot;
b7cc4e82 3403 bool ret;
01c097f7 3404 uint64_t par64;
1313e2d7 3405 bool format64 = false;
8bf5b6a9 3406 MemTxAttrs attrs = {};
e14b5a23 3407 ARMMMUFaultInfo fi = {};
5b2d261d 3408 ARMCacheAttrs cacheattrs = {};
4a501606 3409
5b2d261d 3410 ret = get_phys_addr(env, value, access_type, mmu_idx, &phys_addr, &attrs,
bc52bfeb 3411 &prot, &page_size, &fi, &cacheattrs);
1313e2d7 3412
0710b2fa
PM
3413 if (ret) {
3414 /*
3415 * Some kinds of translation fault must cause exceptions rather
3416 * than being reported in the PAR.
3417 */
3418 int current_el = arm_current_el(env);
3419 int target_el;
3420 uint32_t syn, fsr, fsc;
3421 bool take_exc = false;
3422
3423 if (fi.s1ptw && current_el == 1 && !arm_is_secure(env)
fee7aa46 3424 && arm_mmu_idx_is_stage1_of_2(mmu_idx)) {
0710b2fa
PM
3425 /*
3426 * Synchronous stage 2 fault on an access made as part of the
3427 * translation table walk for AT S1E0* or AT S1E1* insn
3428 * executed from NS EL1. If this is a synchronous external abort
3429 * and SCR_EL3.EA == 1, then we take a synchronous external abort
3430 * to EL3. Otherwise the fault is taken as an exception to EL2,
3431 * and HPFAR_EL2 holds the faulting IPA.
3432 */
3433 if (fi.type == ARMFault_SyncExternalOnWalk &&
3434 (env->cp15.scr_el3 & SCR_EA)) {
3435 target_el = 3;
3436 } else {
3437 env->cp15.hpfar_el2 = extract64(fi.s2addr, 12, 47) << 4;
3438 target_el = 2;
3439 }
3440 take_exc = true;
3441 } else if (fi.type == ARMFault_SyncExternalOnWalk) {
3442 /*
3443 * Synchronous external aborts during a translation table walk
3444 * are taken as Data Abort exceptions.
3445 */
3446 if (fi.stage2) {
3447 if (current_el == 3) {
3448 target_el = 3;
3449 } else {
3450 target_el = 2;
3451 }
3452 } else {
3453 target_el = exception_target_el(env);
3454 }
3455 take_exc = true;
3456 }
3457
3458 if (take_exc) {
3459 /* Construct FSR and FSC using same logic as arm_deliver_fault() */
3460 if (target_el == 2 || arm_el_is_aa64(env, target_el) ||
3461 arm_s1_regime_using_lpae_format(env, mmu_idx)) {
3462 fsr = arm_fi_to_lfsc(&fi);
3463 fsc = extract32(fsr, 0, 6);
3464 } else {
3465 fsr = arm_fi_to_sfsc(&fi);
3466 fsc = 0x3f;
3467 }
3468 /*
3469 * Report exception with ESR indicating a fault due to a
3470 * translation table walk for a cache maintenance instruction.
3471 */
e24fd076 3472 syn = syn_data_abort_no_iss(current_el == target_el, 0,
0710b2fa
PM
3473 fi.ea, 1, fi.s1ptw, 1, fsc);
3474 env->exception.vaddress = value;
3475 env->exception.fsr = fsr;
3476 raise_exception(env, EXCP_DATA_ABORT, syn, target_el);
3477 }
3478 }
3479
1313e2d7
EI
3480 if (is_a64(env)) {
3481 format64 = true;
3482 } else if (arm_feature(env, ARM_FEATURE_LPAE)) {
3483 /*
3484 * ATS1Cxx:
3485 * * TTBCR.EAE determines whether the result is returned using the
3486 * 32-bit or the 64-bit PAR format
3487 * * Instructions executed in Hyp mode always use the 64bit format
3488 *
3489 * ATS1S2NSOxx uses the 64bit format if any of the following is true:
3490 * * The Non-secure TTBCR.EAE bit is set to 1
3491 * * The implementation includes EL2, and the value of HCR.VM is 1
3492 *
9d1bab33
PM
3493 * (Note that HCR.DC makes HCR.VM behave as if it is 1.)
3494 *
23463e0e 3495 * ATS1Hx always uses the 64bit format.
1313e2d7
EI
3496 */
3497 format64 = arm_s1_regime_using_lpae_format(env, mmu_idx);
3498
3499 if (arm_feature(env, ARM_FEATURE_EL2)) {
452ef8cb
RH
3500 if (mmu_idx == ARMMMUIdx_E10_0 ||
3501 mmu_idx == ARMMMUIdx_E10_1 ||
3502 mmu_idx == ARMMMUIdx_E10_1_PAN) {
9d1bab33 3503 format64 |= env->cp15.hcr_el2 & (HCR_VM | HCR_DC);
1313e2d7
EI
3504 } else {
3505 format64 |= arm_current_el(env) == 2;
3506 }
3507 }
3508 }
3509
3510 if (format64) {
5efe9ed4 3511 /* Create a 64-bit PAR */
01c097f7 3512 par64 = (1 << 11); /* LPAE bit always set */
b7cc4e82 3513 if (!ret) {
702a9357 3514 par64 |= phys_addr & ~0xfffULL;
8bf5b6a9
PM
3515 if (!attrs.secure) {
3516 par64 |= (1 << 9); /* NS */
3517 }
5b2d261d
AB
3518 par64 |= (uint64_t)cacheattrs.attrs << 56; /* ATTR */
3519 par64 |= cacheattrs.shareability << 7; /* SH */
4a501606 3520 } else {
5efe9ed4
PM
3521 uint32_t fsr = arm_fi_to_lfsc(&fi);
3522
702a9357 3523 par64 |= 1; /* F */
b7cc4e82 3524 par64 |= (fsr & 0x3f) << 1; /* FS */
0f7b791b
PM
3525 if (fi.stage2) {
3526 par64 |= (1 << 9); /* S */
3527 }
3528 if (fi.s1ptw) {
3529 par64 |= (1 << 8); /* PTW */
3530 }
4a501606
PM
3531 }
3532 } else {
b7cc4e82 3533 /* fsr is a DFSR/IFSR value for the short descriptor
702a9357
PM
3534 * translation table format (with WnR always clear).
3535 * Convert it to a 32-bit PAR.
3536 */
b7cc4e82 3537 if (!ret) {
702a9357
PM
3538 /* We do not set any attribute bits in the PAR */
3539 if (page_size == (1 << 24)
3540 && arm_feature(env, ARM_FEATURE_V7)) {
01c097f7 3541 par64 = (phys_addr & 0xff000000) | (1 << 1);
702a9357 3542 } else {
01c097f7 3543 par64 = phys_addr & 0xfffff000;
702a9357 3544 }
8bf5b6a9
PM
3545 if (!attrs.secure) {
3546 par64 |= (1 << 9); /* NS */
3547 }
702a9357 3548 } else {
5efe9ed4
PM
3549 uint32_t fsr = arm_fi_to_sfsc(&fi);
3550
b7cc4e82
PC
3551 par64 = ((fsr & (1 << 10)) >> 5) | ((fsr & (1 << 12)) >> 6) |
3552 ((fsr & 0xf) << 1) | 1;
702a9357 3553 }
4a501606 3554 }
060e8a48
PM
3555 return par64;
3556}
9fb005b0 3557#endif /* CONFIG_TCG */
060e8a48
PM
3558
3559static void ats_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
3560{
9fb005b0 3561#ifdef CONFIG_TCG
03ae85f8 3562 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
060e8a48 3563 uint64_t par64;
d3649702
PM
3564 ARMMMUIdx mmu_idx;
3565 int el = arm_current_el(env);
3566 bool secure = arm_is_secure_below_el3(env);
060e8a48 3567
d3649702
PM
3568 switch (ri->opc2 & 6) {
3569 case 0:
04b07d29 3570 /* stage 1 current state PL1: ATS1CPR, ATS1CPW, ATS1CPRP, ATS1CPWP */
d3649702
PM
3571 switch (el) {
3572 case 3:
127b2b08 3573 mmu_idx = ARMMMUIdx_SE3;
d3649702
PM
3574 break;
3575 case 2:
04b07d29
RH
3576 g_assert(!secure); /* TODO: ARMv8.4-SecEL2 */
3577 /* fall through */
d3649702 3578 case 1:
04b07d29
RH
3579 if (ri->crm == 9 && (env->uncached_cpsr & CPSR_PAN)) {
3580 mmu_idx = (secure ? ARMMMUIdx_SE10_1_PAN
3581 : ARMMMUIdx_Stage1_E1_PAN);
3582 } else {
3583 mmu_idx = secure ? ARMMMUIdx_SE10_1 : ARMMMUIdx_Stage1_E1;
3584 }
d3649702
PM
3585 break;
3586 default:
3587 g_assert_not_reached();
3588 }
3589 break;
3590 case 2:
3591 /* stage 1 current state PL0: ATS1CUR, ATS1CUW */
3592 switch (el) {
3593 case 3:
fba37aed 3594 mmu_idx = ARMMMUIdx_SE10_0;
d3649702
PM
3595 break;
3596 case 2:
2859d7b5 3597 mmu_idx = ARMMMUIdx_Stage1_E0;
d3649702
PM
3598 break;
3599 case 1:
fba37aed 3600 mmu_idx = secure ? ARMMMUIdx_SE10_0 : ARMMMUIdx_Stage1_E0;
d3649702
PM
3601 break;
3602 default:
3603 g_assert_not_reached();
3604 }
3605 break;
3606 case 4:
3607 /* stage 1+2 NonSecure PL1: ATS12NSOPR, ATS12NSOPW */
01b98b68 3608 mmu_idx = ARMMMUIdx_E10_1;
d3649702
PM
3609 break;
3610 case 6:
3611 /* stage 1+2 NonSecure PL0: ATS12NSOUR, ATS12NSOUW */
01b98b68 3612 mmu_idx = ARMMMUIdx_E10_0;
d3649702
PM
3613 break;
3614 default:
3615 g_assert_not_reached();
3616 }
3617
3618 par64 = do_ats_write(env, value, access_type, mmu_idx);
01c097f7
FA
3619
3620 A32_BANKED_CURRENT_REG_SET(env, par, par64);
9fb005b0
PMD
3621#else
3622 /* Handled by hardware accelerator. */
3623 g_assert_not_reached();
3624#endif /* CONFIG_TCG */
4a501606 3625}
060e8a48 3626
14db7fe0
PM
3627static void ats1h_write(CPUARMState *env, const ARMCPRegInfo *ri,
3628 uint64_t value)
3629{
9fb005b0 3630#ifdef CONFIG_TCG
03ae85f8 3631 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
14db7fe0
PM
3632 uint64_t par64;
3633
e013b741 3634 par64 = do_ats_write(env, value, access_type, ARMMMUIdx_E2);
14db7fe0
PM
3635
3636 A32_BANKED_CURRENT_REG_SET(env, par, par64);
9fb005b0
PMD
3637#else
3638 /* Handled by hardware accelerator. */
3639 g_assert_not_reached();
3640#endif /* CONFIG_TCG */
14db7fe0
PM
3641}
3642
3f208fd7
PM
3643static CPAccessResult at_s1e2_access(CPUARMState *env, const ARMCPRegInfo *ri,
3644 bool isread)
2a47df95
PM
3645{
3646 if (arm_current_el(env) == 3 && !(env->cp15.scr_el3 & SCR_NS)) {
3647 return CP_ACCESS_TRAP;
3648 }
3649 return CP_ACCESS_OK;
3650}
3651
060e8a48
PM
3652static void ats_write64(CPUARMState *env, const ARMCPRegInfo *ri,
3653 uint64_t value)
3654{
9fb005b0 3655#ifdef CONFIG_TCG
03ae85f8 3656 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
d3649702
PM
3657 ARMMMUIdx mmu_idx;
3658 int secure = arm_is_secure_below_el3(env);
3659
3660 switch (ri->opc2 & 6) {
3661 case 0:
3662 switch (ri->opc1) {
04b07d29
RH
3663 case 0: /* AT S1E1R, AT S1E1W, AT S1E1RP, AT S1E1WP */
3664 if (ri->crm == 9 && (env->pstate & PSTATE_PAN)) {
3665 mmu_idx = (secure ? ARMMMUIdx_SE10_1_PAN
3666 : ARMMMUIdx_Stage1_E1_PAN);
3667 } else {
3668 mmu_idx = secure ? ARMMMUIdx_SE10_1 : ARMMMUIdx_Stage1_E1;
3669 }
d3649702
PM
3670 break;
3671 case 4: /* AT S1E2R, AT S1E2W */
e013b741 3672 mmu_idx = ARMMMUIdx_E2;
d3649702
PM
3673 break;
3674 case 6: /* AT S1E3R, AT S1E3W */
127b2b08 3675 mmu_idx = ARMMMUIdx_SE3;
d3649702
PM
3676 break;
3677 default:
3678 g_assert_not_reached();
3679 }
3680 break;
3681 case 2: /* AT S1E0R, AT S1E0W */
fba37aed 3682 mmu_idx = secure ? ARMMMUIdx_SE10_0 : ARMMMUIdx_Stage1_E0;
d3649702
PM
3683 break;
3684 case 4: /* AT S12E1R, AT S12E1W */
fba37aed 3685 mmu_idx = secure ? ARMMMUIdx_SE10_1 : ARMMMUIdx_E10_1;
d3649702
PM
3686 break;
3687 case 6: /* AT S12E0R, AT S12E0W */
fba37aed 3688 mmu_idx = secure ? ARMMMUIdx_SE10_0 : ARMMMUIdx_E10_0;
d3649702
PM
3689 break;
3690 default:
3691 g_assert_not_reached();
3692 }
060e8a48 3693
d3649702 3694 env->cp15.par_el[1] = do_ats_write(env, value, access_type, mmu_idx);
9fb005b0
PMD
3695#else
3696 /* Handled by hardware accelerator. */
3697 g_assert_not_reached();
3698#endif /* CONFIG_TCG */
060e8a48 3699}
4a501606
PM
3700#endif
3701
3702static const ARMCPRegInfo vapa_cp_reginfo[] = {
3703 { .name = "PAR", .cp = 15, .crn = 7, .crm = 4, .opc1 = 0, .opc2 = 0,
3704 .access = PL1_RW, .resetvalue = 0,
01c097f7
FA
3705 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.par_s),
3706 offsetoflow32(CPUARMState, cp15.par_ns) },
4a501606
PM
3707 .writefn = par_write },
3708#ifndef CONFIG_USER_ONLY
87562e4f 3709 /* This underdecoding is safe because the reginfo is NO_RAW. */
4a501606 3710 { .name = "ATS", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = CP_ANY,
92611c00 3711 .access = PL1_W, .accessfn = ats_access,
0710b2fa 3712 .writefn = ats_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
4a501606
PM
3713#endif
3714 REGINFO_SENTINEL
3715};
3716
18032bec
PM
3717/* Return basic MPU access permission bits. */
3718static uint32_t simple_mpu_ap_bits(uint32_t val)
3719{
3720 uint32_t ret;
3721 uint32_t mask;
3722 int i;
3723 ret = 0;
3724 mask = 3;
3725 for (i = 0; i < 16; i += 2) {
3726 ret |= (val >> i) & mask;
3727 mask <<= 2;
3728 }
3729 return ret;
3730}
3731
3732/* Pad basic MPU access permission bits to extended format. */
3733static uint32_t extended_mpu_ap_bits(uint32_t val)
3734{
3735 uint32_t ret;
3736 uint32_t mask;
3737 int i;
3738 ret = 0;
3739 mask = 3;
3740 for (i = 0; i < 16; i += 2) {
3741 ret |= (val & mask) << i;
3742 mask <<= 2;
3743 }
3744 return ret;
3745}
3746
c4241c7d
PM
3747static void pmsav5_data_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
3748 uint64_t value)
18032bec 3749{
7e09797c 3750 env->cp15.pmsav5_data_ap = extended_mpu_ap_bits(value);
18032bec
PM
3751}
3752
c4241c7d 3753static uint64_t pmsav5_data_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
18032bec 3754{
7e09797c 3755 return simple_mpu_ap_bits(env->cp15.pmsav5_data_ap);
18032bec
PM
3756}
3757
c4241c7d
PM
3758static void pmsav5_insn_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
3759 uint64_t value)
18032bec 3760{
7e09797c 3761 env->cp15.pmsav5_insn_ap = extended_mpu_ap_bits(value);
18032bec
PM
3762}
3763
c4241c7d 3764static uint64_t pmsav5_insn_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
18032bec 3765{
7e09797c 3766 return simple_mpu_ap_bits(env->cp15.pmsav5_insn_ap);
18032bec
PM
3767}
3768
6cb0b013
PC
3769static uint64_t pmsav7_read(CPUARMState *env, const ARMCPRegInfo *ri)
3770{
3771 uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
3772
3773 if (!u32p) {
3774 return 0;
3775 }
3776
1bc04a88 3777 u32p += env->pmsav7.rnr[M_REG_NS];
6cb0b013
PC
3778 return *u32p;
3779}
3780
3781static void pmsav7_write(CPUARMState *env, const ARMCPRegInfo *ri,
3782 uint64_t value)
3783{
2fc0cc0e 3784 ARMCPU *cpu = env_archcpu(env);
6cb0b013
PC
3785 uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
3786
3787 if (!u32p) {
3788 return;
3789 }
3790
1bc04a88 3791 u32p += env->pmsav7.rnr[M_REG_NS];
d10eb08f 3792 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
6cb0b013
PC
3793 *u32p = value;
3794}
3795
6cb0b013
PC
3796static void pmsav7_rgnr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3797 uint64_t value)
3798{
2fc0cc0e 3799 ARMCPU *cpu = env_archcpu(env);
6cb0b013
PC
3800 uint32_t nrgs = cpu->pmsav7_dregion;
3801
3802 if (value >= nrgs) {
3803 qemu_log_mask(LOG_GUEST_ERROR,
3804 "PMSAv7 RGNR write >= # supported regions, %" PRIu32
3805 " > %" PRIu32 "\n", (uint32_t)value, nrgs);
3806 return;
3807 }
3808
3809 raw_write(env, ri, value);
3810}
3811
3812static const ARMCPRegInfo pmsav7_cp_reginfo[] = {
69ceea64
PM
3813 /* Reset for all these registers is handled in arm_cpu_reset(),
3814 * because the PMSAv7 is also used by M-profile CPUs, which do
3815 * not register cpregs but still need the state to be reset.
3816 */
6cb0b013
PC
3817 { .name = "DRBAR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 0,
3818 .access = PL1_RW, .type = ARM_CP_NO_RAW,
3819 .fieldoffset = offsetof(CPUARMState, pmsav7.drbar),
69ceea64
PM
3820 .readfn = pmsav7_read, .writefn = pmsav7_write,
3821 .resetfn = arm_cp_reset_ignore },
6cb0b013
PC
3822 { .name = "DRSR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 2,
3823 .access = PL1_RW, .type = ARM_CP_NO_RAW,
3824 .fieldoffset = offsetof(CPUARMState, pmsav7.drsr),
69ceea64
PM
3825 .readfn = pmsav7_read, .writefn = pmsav7_write,
3826 .resetfn = arm_cp_reset_ignore },
6cb0b013
PC
3827 { .name = "DRACR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 4,
3828 .access = PL1_RW, .type = ARM_CP_NO_RAW,
3829 .fieldoffset = offsetof(CPUARMState, pmsav7.dracr),
69ceea64
PM
3830 .readfn = pmsav7_read, .writefn = pmsav7_write,
3831 .resetfn = arm_cp_reset_ignore },
6cb0b013
PC
3832 { .name = "RGNR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 2, .opc2 = 0,
3833 .access = PL1_RW,
1bc04a88 3834 .fieldoffset = offsetof(CPUARMState, pmsav7.rnr[M_REG_NS]),
69ceea64
PM
3835 .writefn = pmsav7_rgnr_write,
3836 .resetfn = arm_cp_reset_ignore },
6cb0b013
PC
3837 REGINFO_SENTINEL
3838};
3839
18032bec
PM
3840static const ARMCPRegInfo pmsav5_cp_reginfo[] = {
3841 { .name = "DATA_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
7a0e58fa 3842 .access = PL1_RW, .type = ARM_CP_ALIAS,
7e09797c 3843 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
18032bec
PM
3844 .readfn = pmsav5_data_ap_read, .writefn = pmsav5_data_ap_write, },
3845 { .name = "INSN_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
7a0e58fa 3846 .access = PL1_RW, .type = ARM_CP_ALIAS,
7e09797c 3847 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
18032bec
PM
3848 .readfn = pmsav5_insn_ap_read, .writefn = pmsav5_insn_ap_write, },
3849 { .name = "DATA_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 2,
3850 .access = PL1_RW,
7e09797c
PM
3851 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
3852 .resetvalue = 0, },
18032bec
PM
3853 { .name = "INSN_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 3,
3854 .access = PL1_RW,
7e09797c
PM
3855 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
3856 .resetvalue = 0, },
ecce5c3c
PM
3857 { .name = "DCACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
3858 .access = PL1_RW,
3859 .fieldoffset = offsetof(CPUARMState, cp15.c2_data), .resetvalue = 0, },
3860 { .name = "ICACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 1,
3861 .access = PL1_RW,
3862 .fieldoffset = offsetof(CPUARMState, cp15.c2_insn), .resetvalue = 0, },
06d76f31 3863 /* Protection region base and size registers */
e508a92b
PM
3864 { .name = "946_PRBS0", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0,
3865 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3866 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[0]) },
3867 { .name = "946_PRBS1", .cp = 15, .crn = 6, .crm = 1, .opc1 = 0,
3868 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3869 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[1]) },
3870 { .name = "946_PRBS2", .cp = 15, .crn = 6, .crm = 2, .opc1 = 0,
3871 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3872 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[2]) },
3873 { .name = "946_PRBS3", .cp = 15, .crn = 6, .crm = 3, .opc1 = 0,
3874 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3875 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[3]) },
3876 { .name = "946_PRBS4", .cp = 15, .crn = 6, .crm = 4, .opc1 = 0,
3877 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3878 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[4]) },
3879 { .name = "946_PRBS5", .cp = 15, .crn = 6, .crm = 5, .opc1 = 0,
3880 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3881 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[5]) },
3882 { .name = "946_PRBS6", .cp = 15, .crn = 6, .crm = 6, .opc1 = 0,
3883 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3884 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[6]) },
3885 { .name = "946_PRBS7", .cp = 15, .crn = 6, .crm = 7, .opc1 = 0,
3886 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3887 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[7]) },
18032bec
PM
3888 REGINFO_SENTINEL
3889};
3890
c4241c7d
PM
3891static void vmsa_ttbcr_raw_write(CPUARMState *env, const ARMCPRegInfo *ri,
3892 uint64_t value)
ecce5c3c 3893{
11f136ee 3894 TCR *tcr = raw_ptr(env, ri);
2ebcebe2
PM
3895 int maskshift = extract32(value, 0, 3);
3896
e389be16
FA
3897 if (!arm_feature(env, ARM_FEATURE_V8)) {
3898 if (arm_feature(env, ARM_FEATURE_LPAE) && (value & TTBCR_EAE)) {
3899 /* Pre ARMv8 bits [21:19], [15:14] and [6:3] are UNK/SBZP when
3900 * using Long-desciptor translation table format */
3901 value &= ~((7 << 19) | (3 << 14) | (0xf << 3));
3902 } else if (arm_feature(env, ARM_FEATURE_EL3)) {
3903 /* In an implementation that includes the Security Extensions
3904 * TTBCR has additional fields PD0 [4] and PD1 [5] for
3905 * Short-descriptor translation table format.
3906 */
3907 value &= TTBCR_PD1 | TTBCR_PD0 | TTBCR_N;
3908 } else {
3909 value &= TTBCR_N;
3910 }
e42c4db3 3911 }
e389be16 3912
b6af0975 3913 /* Update the masks corresponding to the TCR bank being written
11f136ee 3914 * Note that we always calculate mask and base_mask, but
e42c4db3 3915 * they are only used for short-descriptor tables (ie if EAE is 0);
11f136ee
FA
3916 * for long-descriptor tables the TCR fields are used differently
3917 * and the mask and base_mask values are meaningless.
e42c4db3 3918 */
11f136ee
FA
3919 tcr->raw_tcr = value;
3920 tcr->mask = ~(((uint32_t)0xffffffffu) >> maskshift);
3921 tcr->base_mask = ~((uint32_t)0x3fffu >> maskshift);
ecce5c3c
PM
3922}
3923
c4241c7d
PM
3924static void vmsa_ttbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3925 uint64_t value)
d4e6df63 3926{
2fc0cc0e 3927 ARMCPU *cpu = env_archcpu(env);
ab638a32 3928 TCR *tcr = raw_ptr(env, ri);
00c8cb0a 3929
d4e6df63
PM
3930 if (arm_feature(env, ARM_FEATURE_LPAE)) {
3931 /* With LPAE the TTBCR could result in a change of ASID
3932 * via the TTBCR.A1 bit, so do a TLB flush.
3933 */
d10eb08f 3934 tlb_flush(CPU(cpu));
d4e6df63 3935 }
ab638a32
RH
3936 /* Preserve the high half of TCR_EL1, set via TTBCR2. */
3937 value = deposit64(tcr->raw_tcr, 0, 32, value);
c4241c7d 3938 vmsa_ttbcr_raw_write(env, ri, value);
d4e6df63
PM
3939}
3940
ecce5c3c
PM
3941static void vmsa_ttbcr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
3942{
11f136ee
FA
3943 TCR *tcr = raw_ptr(env, ri);
3944
3945 /* Reset both the TCR as well as the masks corresponding to the bank of
3946 * the TCR being reset.
3947 */
3948 tcr->raw_tcr = 0;
3949 tcr->mask = 0;
3950 tcr->base_mask = 0xffffc000u;
ecce5c3c
PM
3951}
3952
d06dc933 3953static void vmsa_tcr_el12_write(CPUARMState *env, const ARMCPRegInfo *ri,
cb2e37df
PM
3954 uint64_t value)
3955{
2fc0cc0e 3956 ARMCPU *cpu = env_archcpu(env);
11f136ee 3957 TCR *tcr = raw_ptr(env, ri);
00c8cb0a 3958
cb2e37df 3959 /* For AArch64 the A1 bit could result in a change of ASID, so TLB flush. */
d10eb08f 3960 tlb_flush(CPU(cpu));
11f136ee 3961 tcr->raw_tcr = value;
cb2e37df
PM
3962}
3963
327ed10f
PM
3964static void vmsa_ttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3965 uint64_t value)
3966{
93f379b0
RH
3967 /* If the ASID changes (with a 64-bit write), we must flush the TLB. */
3968 if (cpreg_field_is_64bit(ri) &&
3969 extract64(raw_read(env, ri) ^ value, 48, 16) != 0) {
2fc0cc0e 3970 ARMCPU *cpu = env_archcpu(env);
d10eb08f 3971 tlb_flush(CPU(cpu));
327ed10f
PM
3972 }
3973 raw_write(env, ri, value);
3974}
3975
ed30da8e
RH
3976static void vmsa_tcr_ttbr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
3977 uint64_t value)
3978{
d06dc933
RH
3979 /*
3980 * If we are running with E2&0 regime, then an ASID is active.
3981 * Flush if that might be changing. Note we're not checking
3982 * TCR_EL2.A1 to know if this is really the TTBRx_EL2 that
3983 * holds the active ASID, only checking the field that might.
3984 */
3985 if (extract64(raw_read(env, ri) ^ value, 48, 16) &&
3986 (arm_hcr_el2_eff(env) & HCR_E2H)) {
3987 tlb_flush_by_mmuidx(env_cpu(env),
452ef8cb
RH
3988 ARMMMUIdxBit_E20_2 |
3989 ARMMMUIdxBit_E20_2_PAN |
3990 ARMMMUIdxBit_E20_0);
d06dc933 3991 }
ed30da8e
RH
3992 raw_write(env, ri, value);
3993}
3994
b698e9cf
EI
3995static void vttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3996 uint64_t value)
3997{
2fc0cc0e 3998 ARMCPU *cpu = env_archcpu(env);
b698e9cf
EI
3999 CPUState *cs = CPU(cpu);
4000
97fa9350
RH
4001 /*
4002 * A change in VMID to the stage2 page table (Stage2) invalidates
4003 * the combined stage 1&2 tlbs (EL10_1 and EL10_0).
4004 */
b698e9cf 4005 if (raw_read(env, ri) != value) {
0336cbf8 4006 tlb_flush_by_mmuidx(cs,
01b98b68 4007 ARMMMUIdxBit_E10_1 |
452ef8cb 4008 ARMMMUIdxBit_E10_1_PAN |
bf05340c 4009 ARMMMUIdxBit_E10_0);
b698e9cf
EI
4010 raw_write(env, ri, value);
4011 }
4012}
4013
8e5d75c9 4014static const ARMCPRegInfo vmsa_pmsa_cp_reginfo[] = {
18032bec 4015 { .name = "DFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
84929218 4016 .access = PL1_RW, .accessfn = access_tvm_trvm, .type = ARM_CP_ALIAS,
4a7e2d73 4017 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dfsr_s),
b061a82b 4018 offsetoflow32(CPUARMState, cp15.dfsr_ns) }, },
18032bec 4019 { .name = "IFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
84929218 4020 .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
88ca1c2d
FA
4021 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.ifsr_s),
4022 offsetoflow32(CPUARMState, cp15.ifsr_ns) } },
8e5d75c9 4023 { .name = "DFAR", .cp = 15, .opc1 = 0, .crn = 6, .crm = 0, .opc2 = 0,
84929218 4024 .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
8e5d75c9
PC
4025 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.dfar_s),
4026 offsetof(CPUARMState, cp15.dfar_ns) } },
4027 { .name = "FAR_EL1", .state = ARM_CP_STATE_AA64,
4028 .opc0 = 3, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 0,
84929218
RH
4029 .access = PL1_RW, .accessfn = access_tvm_trvm,
4030 .fieldoffset = offsetof(CPUARMState, cp15.far_el[1]),
8e5d75c9
PC
4031 .resetvalue = 0, },
4032 REGINFO_SENTINEL
4033};
4034
4035static const ARMCPRegInfo vmsa_cp_reginfo[] = {
6cd8a264
RH
4036 { .name = "ESR_EL1", .state = ARM_CP_STATE_AA64,
4037 .opc0 = 3, .crn = 5, .crm = 2, .opc1 = 0, .opc2 = 0,
84929218 4038 .access = PL1_RW, .accessfn = access_tvm_trvm,
d81c519c 4039 .fieldoffset = offsetof(CPUARMState, cp15.esr_el[1]), .resetvalue = 0, },
327ed10f 4040 { .name = "TTBR0_EL1", .state = ARM_CP_STATE_BOTH,
7dd8c9af 4041 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 0,
84929218
RH
4042 .access = PL1_RW, .accessfn = access_tvm_trvm,
4043 .writefn = vmsa_ttbr_write, .resetvalue = 0,
7dd8c9af
FA
4044 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
4045 offsetof(CPUARMState, cp15.ttbr0_ns) } },
327ed10f 4046 { .name = "TTBR1_EL1", .state = ARM_CP_STATE_BOTH,
7dd8c9af 4047 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 1,
84929218
RH
4048 .access = PL1_RW, .accessfn = access_tvm_trvm,
4049 .writefn = vmsa_ttbr_write, .resetvalue = 0,
7dd8c9af
FA
4050 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
4051 offsetof(CPUARMState, cp15.ttbr1_ns) } },
cb2e37df
PM
4052 { .name = "TCR_EL1", .state = ARM_CP_STATE_AA64,
4053 .opc0 = 3, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
84929218
RH
4054 .access = PL1_RW, .accessfn = access_tvm_trvm,
4055 .writefn = vmsa_tcr_el12_write,
cb2e37df 4056 .resetfn = vmsa_ttbcr_reset, .raw_writefn = raw_write,
11f136ee 4057 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[1]) },
cb2e37df 4058 { .name = "TTBCR", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
84929218
RH
4059 .access = PL1_RW, .accessfn = access_tvm_trvm,
4060 .type = ARM_CP_ALIAS, .writefn = vmsa_ttbcr_write,
b061a82b 4061 .raw_writefn = vmsa_ttbcr_raw_write,
11f136ee
FA
4062 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tcr_el[3]),
4063 offsetoflow32(CPUARMState, cp15.tcr_el[1])} },
18032bec
PM
4064 REGINFO_SENTINEL
4065};
4066
ab638a32
RH
4067/* Note that unlike TTBCR, writing to TTBCR2 does not require flushing
4068 * qemu tlbs nor adjusting cached masks.
4069 */
4070static const ARMCPRegInfo ttbcr2_reginfo = {
4071 .name = "TTBCR2", .cp = 15, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 3,
84929218
RH
4072 .access = PL1_RW, .accessfn = access_tvm_trvm,
4073 .type = ARM_CP_ALIAS,
ab638a32
RH
4074 .bank_fieldoffsets = { offsetofhigh32(CPUARMState, cp15.tcr_el[3]),
4075 offsetofhigh32(CPUARMState, cp15.tcr_el[1]) },
4076};
4077
c4241c7d
PM
4078static void omap_ticonfig_write(CPUARMState *env, const ARMCPRegInfo *ri,
4079 uint64_t value)
1047b9d7
PM
4080{
4081 env->cp15.c15_ticonfig = value & 0xe7;
4082 /* The OS_TYPE bit in this register changes the reported CPUID! */
4083 env->cp15.c0_cpuid = (value & (1 << 5)) ?
4084 ARM_CPUID_TI915T : ARM_CPUID_TI925T;
1047b9d7
PM
4085}
4086
c4241c7d
PM
4087static void omap_threadid_write(CPUARMState *env, const ARMCPRegInfo *ri,
4088 uint64_t value)
1047b9d7
PM
4089{
4090 env->cp15.c15_threadid = value & 0xffff;
1047b9d7
PM
4091}
4092
c4241c7d
PM
4093static void omap_wfi_write(CPUARMState *env, const ARMCPRegInfo *ri,
4094 uint64_t value)
1047b9d7
PM
4095{
4096 /* Wait-for-interrupt (deprecated) */
2fc0cc0e 4097 cpu_interrupt(env_cpu(env), CPU_INTERRUPT_HALT);
1047b9d7
PM
4098}
4099
c4241c7d
PM
4100static void omap_cachemaint_write(CPUARMState *env, const ARMCPRegInfo *ri,
4101 uint64_t value)
c4804214
PM
4102{
4103 /* On OMAP there are registers indicating the max/min index of dcache lines
4104 * containing a dirty line; cache flush operations have to reset these.
4105 */
4106 env->cp15.c15_i_max = 0x000;
4107 env->cp15.c15_i_min = 0xff0;
c4804214
PM
4108}
4109
18032bec
PM
4110static const ARMCPRegInfo omap_cp_reginfo[] = {
4111 { .name = "DFSR", .cp = 15, .crn = 5, .crm = CP_ANY,
4112 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_OVERRIDE,
d81c519c 4113 .fieldoffset = offsetoflow32(CPUARMState, cp15.esr_el[1]),
6cd8a264 4114 .resetvalue = 0, },
1047b9d7
PM
4115 { .name = "", .cp = 15, .crn = 15, .crm = 0, .opc1 = 0, .opc2 = 0,
4116 .access = PL1_RW, .type = ARM_CP_NOP },
4117 { .name = "TICONFIG", .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0,
4118 .access = PL1_RW,
4119 .fieldoffset = offsetof(CPUARMState, cp15.c15_ticonfig), .resetvalue = 0,
4120 .writefn = omap_ticonfig_write },
4121 { .name = "IMAX", .cp = 15, .crn = 15, .crm = 2, .opc1 = 0, .opc2 = 0,
4122 .access = PL1_RW,
4123 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_max), .resetvalue = 0, },
4124 { .name = "IMIN", .cp = 15, .crn = 15, .crm = 3, .opc1 = 0, .opc2 = 0,
4125 .access = PL1_RW, .resetvalue = 0xff0,
4126 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_min) },
4127 { .name = "THREADID", .cp = 15, .crn = 15, .crm = 4, .opc1 = 0, .opc2 = 0,
4128 .access = PL1_RW,
4129 .fieldoffset = offsetof(CPUARMState, cp15.c15_threadid), .resetvalue = 0,
4130 .writefn = omap_threadid_write },
4131 { .name = "TI925T_STATUS", .cp = 15, .crn = 15,
4132 .crm = 8, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
7a0e58fa 4133 .type = ARM_CP_NO_RAW,
1047b9d7
PM
4134 .readfn = arm_cp_read_zero, .writefn = omap_wfi_write, },
4135 /* TODO: Peripheral port remap register:
4136 * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt controller
4137 * base address at $rn & ~0xfff and map size of 0x200 << ($rn & 0xfff),
4138 * when MMU is off.
4139 */
c4804214 4140 { .name = "OMAP_CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
d4e6df63 4141 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
7a0e58fa 4142 .type = ARM_CP_OVERRIDE | ARM_CP_NO_RAW,
c4804214 4143 .writefn = omap_cachemaint_write },
34f90529
PM
4144 { .name = "C9", .cp = 15, .crn = 9,
4145 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW,
4146 .type = ARM_CP_CONST | ARM_CP_OVERRIDE, .resetvalue = 0 },
1047b9d7
PM
4147 REGINFO_SENTINEL
4148};
4149
c4241c7d
PM
4150static void xscale_cpar_write(CPUARMState *env, const ARMCPRegInfo *ri,
4151 uint64_t value)
1047b9d7 4152{
c0f4af17 4153 env->cp15.c15_cpar = value & 0x3fff;
1047b9d7
PM
4154}
4155
4156static const ARMCPRegInfo xscale_cp_reginfo[] = {
4157 { .name = "XSCALE_CPAR",
4158 .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
4159 .fieldoffset = offsetof(CPUARMState, cp15.c15_cpar), .resetvalue = 0,
4160 .writefn = xscale_cpar_write, },
2771db27
PM
4161 { .name = "XSCALE_AUXCR",
4162 .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 1, .access = PL1_RW,
4163 .fieldoffset = offsetof(CPUARMState, cp15.c1_xscaleauxcr),
4164 .resetvalue = 0, },
3b771579
PM
4165 /* XScale specific cache-lockdown: since we have no cache we NOP these
4166 * and hope the guest does not really rely on cache behaviour.
4167 */
4168 { .name = "XSCALE_LOCK_ICACHE_LINE",
4169 .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 0,
4170 .access = PL1_W, .type = ARM_CP_NOP },
4171 { .name = "XSCALE_UNLOCK_ICACHE",
4172 .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 1,
4173 .access = PL1_W, .type = ARM_CP_NOP },
4174 { .name = "XSCALE_DCACHE_LOCK",
4175 .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 0,
4176 .access = PL1_RW, .type = ARM_CP_NOP },
4177 { .name = "XSCALE_UNLOCK_DCACHE",
4178 .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 1,
4179 .access = PL1_W, .type = ARM_CP_NOP },
1047b9d7
PM
4180 REGINFO_SENTINEL
4181};
4182
4183static const ARMCPRegInfo dummy_c15_cp_reginfo[] = {
4184 /* RAZ/WI the whole crn=15 space, when we don't have a more specific
4185 * implementation of this implementation-defined space.
4186 * Ideally this should eventually disappear in favour of actually
4187 * implementing the correct behaviour for all cores.
4188 */
4189 { .name = "C15_IMPDEF", .cp = 15, .crn = 15,
4190 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
3671cd87 4191 .access = PL1_RW,
7a0e58fa 4192 .type = ARM_CP_CONST | ARM_CP_NO_RAW | ARM_CP_OVERRIDE,
d4e6df63 4193 .resetvalue = 0 },
18032bec
PM
4194 REGINFO_SENTINEL
4195};
4196
c4804214
PM
4197static const ARMCPRegInfo cache_dirty_status_cp_reginfo[] = {
4198 /* Cache status: RAZ because we have no cache so it's always clean */
4199 { .name = "CDSR", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 6,
7a0e58fa 4200 .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 4201 .resetvalue = 0 },
c4804214
PM
4202 REGINFO_SENTINEL
4203};
4204
4205static const ARMCPRegInfo cache_block_ops_cp_reginfo[] = {
4206 /* We never have a a block transfer operation in progress */
4207 { .name = "BXSR", .cp = 15, .crn = 7, .crm = 12, .opc1 = 0, .opc2 = 4,
7a0e58fa 4208 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 4209 .resetvalue = 0 },
30b05bba
PM
4210 /* The cache ops themselves: these all NOP for QEMU */
4211 { .name = "IICR", .cp = 15, .crm = 5, .opc1 = 0,
4212 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
4213 { .name = "IDCR", .cp = 15, .crm = 6, .opc1 = 0,
4214 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
4215 { .name = "CDCR", .cp = 15, .crm = 12, .opc1 = 0,
4216 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
4217 { .name = "PIR", .cp = 15, .crm = 12, .opc1 = 1,
4218 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
4219 { .name = "PDR", .cp = 15, .crm = 12, .opc1 = 2,
4220 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
4221 { .name = "CIDCR", .cp = 15, .crm = 14, .opc1 = 0,
4222 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
c4804214
PM
4223 REGINFO_SENTINEL
4224};
4225
4226static const ARMCPRegInfo cache_test_clean_cp_reginfo[] = {
4227 /* The cache test-and-clean instructions always return (1 << 30)
4228 * to indicate that there are no dirty cache lines.
4229 */
4230 { .name = "TC_DCACHE", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 3,
7a0e58fa 4231 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 4232 .resetvalue = (1 << 30) },
c4804214 4233 { .name = "TCI_DCACHE", .cp = 15, .crn = 7, .crm = 14, .opc1 = 0, .opc2 = 3,
7a0e58fa 4234 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 4235 .resetvalue = (1 << 30) },
c4804214
PM
4236 REGINFO_SENTINEL
4237};
4238
34f90529
PM
4239static const ARMCPRegInfo strongarm_cp_reginfo[] = {
4240 /* Ignore ReadBuffer accesses */
4241 { .name = "C9_READBUFFER", .cp = 15, .crn = 9,
4242 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
d4e6df63 4243 .access = PL1_RW, .resetvalue = 0,
7a0e58fa 4244 .type = ARM_CP_CONST | ARM_CP_OVERRIDE | ARM_CP_NO_RAW },
34f90529
PM
4245 REGINFO_SENTINEL
4246};
4247
731de9e6
EI
4248static uint64_t midr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4249{
2fc0cc0e 4250 ARMCPU *cpu = env_archcpu(env);
731de9e6
EI
4251 unsigned int cur_el = arm_current_el(env);
4252 bool secure = arm_is_secure(env);
4253
4254 if (arm_feature(&cpu->env, ARM_FEATURE_EL2) && !secure && cur_el == 1) {
4255 return env->cp15.vpidr_el2;
4256 }
4257 return raw_read(env, ri);
4258}
4259
06a7e647 4260static uint64_t mpidr_read_val(CPUARMState *env)
81bdde9d 4261{
2fc0cc0e 4262 ARMCPU *cpu = env_archcpu(env);
eb5e1d3c
PF
4263 uint64_t mpidr = cpu->mp_affinity;
4264
81bdde9d 4265 if (arm_feature(env, ARM_FEATURE_V7MP)) {
78dbbbe4 4266 mpidr |= (1U << 31);
81bdde9d
PM
4267 /* Cores which are uniprocessor (non-coherent)
4268 * but still implement the MP extensions set
a8e81b31 4269 * bit 30. (For instance, Cortex-R5).
81bdde9d 4270 */
a8e81b31
PC
4271 if (cpu->mp_is_up) {
4272 mpidr |= (1u << 30);
4273 }
81bdde9d 4274 }
c4241c7d 4275 return mpidr;
81bdde9d
PM
4276}
4277
06a7e647
EI
4278static uint64_t mpidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4279{
f0d574d6
EI
4280 unsigned int cur_el = arm_current_el(env);
4281 bool secure = arm_is_secure(env);
4282
4283 if (arm_feature(env, ARM_FEATURE_EL2) && !secure && cur_el == 1) {
4284 return env->cp15.vmpidr_el2;
4285 }
06a7e647
EI
4286 return mpidr_read_val(env);
4287}
4288
7ac681cf 4289static const ARMCPRegInfo lpae_cp_reginfo[] = {
a903c449 4290 /* NOP AMAIR0/1 */
b0fe2427
PM
4291 { .name = "AMAIR0", .state = ARM_CP_STATE_BOTH,
4292 .opc0 = 3, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 0,
84929218
RH
4293 .access = PL1_RW, .accessfn = access_tvm_trvm,
4294 .type = ARM_CP_CONST, .resetvalue = 0 },
b0fe2427 4295 /* AMAIR1 is mapped to AMAIR_EL1[63:32] */
7ac681cf 4296 { .name = "AMAIR1", .cp = 15, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 1,
84929218
RH
4297 .access = PL1_RW, .accessfn = access_tvm_trvm,
4298 .type = ARM_CP_CONST, .resetvalue = 0 },
891a2fe7 4299 { .name = "PAR", .cp = 15, .crm = 7, .opc1 = 0,
01c097f7
FA
4300 .access = PL1_RW, .type = ARM_CP_64BIT, .resetvalue = 0,
4301 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.par_s),
4302 offsetof(CPUARMState, cp15.par_ns)} },
891a2fe7 4303 { .name = "TTBR0", .cp = 15, .crm = 2, .opc1 = 0,
84929218
RH
4304 .access = PL1_RW, .accessfn = access_tvm_trvm,
4305 .type = ARM_CP_64BIT | ARM_CP_ALIAS,
7dd8c9af
FA
4306 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
4307 offsetof(CPUARMState, cp15.ttbr0_ns) },
b061a82b 4308 .writefn = vmsa_ttbr_write, },
891a2fe7 4309 { .name = "TTBR1", .cp = 15, .crm = 2, .opc1 = 1,
84929218
RH
4310 .access = PL1_RW, .accessfn = access_tvm_trvm,
4311 .type = ARM_CP_64BIT | ARM_CP_ALIAS,
7dd8c9af
FA
4312 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
4313 offsetof(CPUARMState, cp15.ttbr1_ns) },
b061a82b 4314 .writefn = vmsa_ttbr_write, },
7ac681cf
PM
4315 REGINFO_SENTINEL
4316};
4317
c4241c7d 4318static uint64_t aa64_fpcr_read(CPUARMState *env, const ARMCPRegInfo *ri)
b0d2b7d0 4319{
c4241c7d 4320 return vfp_get_fpcr(env);
b0d2b7d0
PM
4321}
4322
c4241c7d
PM
4323static void aa64_fpcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4324 uint64_t value)
b0d2b7d0
PM
4325{
4326 vfp_set_fpcr(env, value);
b0d2b7d0
PM
4327}
4328
c4241c7d 4329static uint64_t aa64_fpsr_read(CPUARMState *env, const ARMCPRegInfo *ri)
b0d2b7d0 4330{
c4241c7d 4331 return vfp_get_fpsr(env);
b0d2b7d0
PM
4332}
4333
c4241c7d
PM
4334static void aa64_fpsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4335 uint64_t value)
b0d2b7d0
PM
4336{
4337 vfp_set_fpsr(env, value);
b0d2b7d0
PM
4338}
4339
3f208fd7
PM
4340static CPAccessResult aa64_daif_access(CPUARMState *env, const ARMCPRegInfo *ri,
4341 bool isread)
c2b820fe 4342{
aaec1432 4343 if (arm_current_el(env) == 0 && !(arm_sctlr(env, 0) & SCTLR_UMA)) {
c2b820fe
PM
4344 return CP_ACCESS_TRAP;
4345 }
4346 return CP_ACCESS_OK;
4347}
4348
4349static void aa64_daif_write(CPUARMState *env, const ARMCPRegInfo *ri,
4350 uint64_t value)
4351{
4352 env->daif = value & PSTATE_DAIF;
4353}
4354
220f508f
RH
4355static uint64_t aa64_pan_read(CPUARMState *env, const ARMCPRegInfo *ri)
4356{
4357 return env->pstate & PSTATE_PAN;
4358}
4359
4360static void aa64_pan_write(CPUARMState *env, const ARMCPRegInfo *ri,
4361 uint64_t value)
4362{
4363 env->pstate = (env->pstate & ~PSTATE_PAN) | (value & PSTATE_PAN);
4364}
4365
4366static const ARMCPRegInfo pan_reginfo = {
4367 .name = "PAN", .state = ARM_CP_STATE_AA64,
4368 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 3,
4369 .type = ARM_CP_NO_RAW, .access = PL1_RW,
4370 .readfn = aa64_pan_read, .writefn = aa64_pan_write
4371};
4372
9eeb7a1c
RH
4373static uint64_t aa64_uao_read(CPUARMState *env, const ARMCPRegInfo *ri)
4374{
4375 return env->pstate & PSTATE_UAO;
4376}
4377
4378static void aa64_uao_write(CPUARMState *env, const ARMCPRegInfo *ri,
4379 uint64_t value)
4380{
4381 env->pstate = (env->pstate & ~PSTATE_UAO) | (value & PSTATE_UAO);
4382}
4383
4384static const ARMCPRegInfo uao_reginfo = {
4385 .name = "UAO", .state = ARM_CP_STATE_AA64,
4386 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 4,
4387 .type = ARM_CP_NO_RAW, .access = PL1_RW,
4388 .readfn = aa64_uao_read, .writefn = aa64_uao_write
4389};
4390
38262d8a
RH
4391static CPAccessResult aa64_cacheop_poc_access(CPUARMState *env,
4392 const ARMCPRegInfo *ri,
4393 bool isread)
8af35c37 4394{
38262d8a
RH
4395 /* Cache invalidate/clean to Point of Coherency or Persistence... */
4396 switch (arm_current_el(env)) {
4397 case 0:
4398 /* ... EL0 must UNDEF unless SCTLR_EL1.UCI is set. */
4399 if (!(arm_sctlr(env, 0) & SCTLR_UCI)) {
4400 return CP_ACCESS_TRAP;
4401 }
4402 /* fall through */
4403 case 1:
4404 /* ... EL1 must trap to EL2 if HCR_EL2.TPCP is set. */
4405 if (arm_hcr_el2_eff(env) & HCR_TPCP) {
4406 return CP_ACCESS_TRAP_EL2;
4407 }
4408 break;
8af35c37
PM
4409 }
4410 return CP_ACCESS_OK;
4411}
4412
38262d8a 4413static CPAccessResult aa64_cacheop_pou_access(CPUARMState *env,
1bed4d2e
RH
4414 const ARMCPRegInfo *ri,
4415 bool isread)
4416{
38262d8a 4417 /* Cache invalidate/clean to Point of Unification... */
1bed4d2e
RH
4418 switch (arm_current_el(env)) {
4419 case 0:
4420 /* ... EL0 must UNDEF unless SCTLR_EL1.UCI is set. */
4421 if (!(arm_sctlr(env, 0) & SCTLR_UCI)) {
4422 return CP_ACCESS_TRAP;
4423 }
4424 /* fall through */
4425 case 1:
38262d8a
RH
4426 /* ... EL1 must trap to EL2 if HCR_EL2.TPU is set. */
4427 if (arm_hcr_el2_eff(env) & HCR_TPU) {
1bed4d2e
RH
4428 return CP_ACCESS_TRAP_EL2;
4429 }
4430 break;
4431 }
4432 return CP_ACCESS_OK;
4433}
4434
dbb1fb27
AB
4435/* See: D4.7.2 TLB maintenance requirements and the TLB maintenance instructions
4436 * Page D4-1736 (DDI0487A.b)
4437 */
4438
b7e0730d
RH
4439static int vae1_tlbmask(CPUARMState *env)
4440{
85d0dc9f 4441 /* Since we exclude secure first, we may read HCR_EL2 directly. */
b7e0730d 4442 if (arm_is_secure_below_el3(env)) {
452ef8cb
RH
4443 return ARMMMUIdxBit_SE10_1 |
4444 ARMMMUIdxBit_SE10_1_PAN |
4445 ARMMMUIdxBit_SE10_0;
85d0dc9f
RH
4446 } else if ((env->cp15.hcr_el2 & (HCR_E2H | HCR_TGE))
4447 == (HCR_E2H | HCR_TGE)) {
452ef8cb
RH
4448 return ARMMMUIdxBit_E20_2 |
4449 ARMMMUIdxBit_E20_2_PAN |
4450 ARMMMUIdxBit_E20_0;
b7e0730d 4451 } else {
452ef8cb
RH
4452 return ARMMMUIdxBit_E10_1 |
4453 ARMMMUIdxBit_E10_1_PAN |
4454 ARMMMUIdxBit_E10_0;
b7e0730d
RH
4455 }
4456}
4457
fd3ed969
PM
4458static void tlbi_aa64_vmalle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4459 uint64_t value)
168aa23b 4460{
29a0af61 4461 CPUState *cs = env_cpu(env);
b7e0730d 4462 int mask = vae1_tlbmask(env);
dbb1fb27 4463
b7e0730d 4464 tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
168aa23b
PM
4465}
4466
b4ab8ce9
PM
4467static void tlbi_aa64_vmalle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4468 uint64_t value)
4469{
29a0af61 4470 CPUState *cs = env_cpu(env);
b7e0730d 4471 int mask = vae1_tlbmask(env);
b4ab8ce9
PM
4472
4473 if (tlb_force_broadcast(env)) {
527db2be
RH
4474 tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
4475 } else {
4476 tlb_flush_by_mmuidx(cs, mask);
b4ab8ce9 4477 }
b4ab8ce9
PM
4478}
4479
90c19cdf 4480static int alle1_tlbmask(CPUARMState *env)
168aa23b 4481{
90c19cdf
RH
4482 /*
4483 * Note that the 'ALL' scope must invalidate both stage 1 and
fd3ed969
PM
4484 * stage 2 translations, whereas most other scopes only invalidate
4485 * stage 1 translations.
4486 */
fd3ed969 4487 if (arm_is_secure_below_el3(env)) {
452ef8cb
RH
4488 return ARMMMUIdxBit_SE10_1 |
4489 ARMMMUIdxBit_SE10_1_PAN |
4490 ARMMMUIdxBit_SE10_0;
fd3ed969 4491 } else {
452ef8cb
RH
4492 return ARMMMUIdxBit_E10_1 |
4493 ARMMMUIdxBit_E10_1_PAN |
4494 ARMMMUIdxBit_E10_0;
fd3ed969 4495 }
168aa23b
PM
4496}
4497
85d0dc9f
RH
4498static int e2_tlbmask(CPUARMState *env)
4499{
4500 /* TODO: ARMv8.4-SecEL2 */
452ef8cb
RH
4501 return ARMMMUIdxBit_E20_0 |
4502 ARMMMUIdxBit_E20_2 |
4503 ARMMMUIdxBit_E20_2_PAN |
4504 ARMMMUIdxBit_E2;
85d0dc9f
RH
4505}
4506
90c19cdf
RH
4507static void tlbi_aa64_alle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4508 uint64_t value)
4509{
4510 CPUState *cs = env_cpu(env);
4511 int mask = alle1_tlbmask(env);
4512
4513 tlb_flush_by_mmuidx(cs, mask);
4514}
4515
fd3ed969 4516static void tlbi_aa64_alle2_write(CPUARMState *env, const ARMCPRegInfo *ri,
fa439fc5
PM
4517 uint64_t value)
4518{
85d0dc9f
RH
4519 CPUState *cs = env_cpu(env);
4520 int mask = e2_tlbmask(env);
fd3ed969 4521
85d0dc9f 4522 tlb_flush_by_mmuidx(cs, mask);
fd3ed969
PM
4523}
4524
43efaa33
PM
4525static void tlbi_aa64_alle3_write(CPUARMState *env, const ARMCPRegInfo *ri,
4526 uint64_t value)
4527{
2fc0cc0e 4528 ARMCPU *cpu = env_archcpu(env);
43efaa33
PM
4529 CPUState *cs = CPU(cpu);
4530
127b2b08 4531 tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_SE3);
43efaa33
PM
4532}
4533
fd3ed969
PM
4534static void tlbi_aa64_alle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4535 uint64_t value)
4536{
29a0af61 4537 CPUState *cs = env_cpu(env);
90c19cdf
RH
4538 int mask = alle1_tlbmask(env);
4539
4540 tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
fa439fc5
PM
4541}
4542
2bfb9d75
PM
4543static void tlbi_aa64_alle2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4544 uint64_t value)
4545{
29a0af61 4546 CPUState *cs = env_cpu(env);
85d0dc9f 4547 int mask = e2_tlbmask(env);
2bfb9d75 4548
85d0dc9f 4549 tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
2bfb9d75
PM
4550}
4551
43efaa33
PM
4552static void tlbi_aa64_alle3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4553 uint64_t value)
4554{
29a0af61 4555 CPUState *cs = env_cpu(env);
43efaa33 4556
127b2b08 4557 tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_SE3);
43efaa33
PM
4558}
4559
fd3ed969
PM
4560static void tlbi_aa64_vae2_write(CPUARMState *env, const ARMCPRegInfo *ri,
4561 uint64_t value)
fa439fc5 4562{
fd3ed969
PM
4563 /* Invalidate by VA, EL2
4564 * Currently handles both VAE2 and VALE2, since we don't support
4565 * flush-last-level-only.
4566 */
85d0dc9f
RH
4567 CPUState *cs = env_cpu(env);
4568 int mask = e2_tlbmask(env);
fd3ed969
PM
4569 uint64_t pageaddr = sextract64(value << 12, 0, 56);
4570
85d0dc9f 4571 tlb_flush_page_by_mmuidx(cs, pageaddr, mask);
fd3ed969
PM
4572}
4573
43efaa33
PM
4574static void tlbi_aa64_vae3_write(CPUARMState *env, const ARMCPRegInfo *ri,
4575 uint64_t value)
4576{
4577 /* Invalidate by VA, EL3
4578 * Currently handles both VAE3 and VALE3, since we don't support
4579 * flush-last-level-only.
4580 */
2fc0cc0e 4581 ARMCPU *cpu = env_archcpu(env);
43efaa33
PM
4582 CPUState *cs = CPU(cpu);
4583 uint64_t pageaddr = sextract64(value << 12, 0, 56);
4584
127b2b08 4585 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_SE3);
43efaa33
PM
4586}
4587
fd3ed969
PM
4588static void tlbi_aa64_vae1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4589 uint64_t value)
4590{
90c19cdf
RH
4591 CPUState *cs = env_cpu(env);
4592 int mask = vae1_tlbmask(env);
fa439fc5
PM
4593 uint64_t pageaddr = sextract64(value << 12, 0, 56);
4594
90c19cdf 4595 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr, mask);
fa439fc5
PM
4596}
4597
b4ab8ce9
PM
4598static void tlbi_aa64_vae1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4599 uint64_t value)
4600{
4601 /* Invalidate by VA, EL1&0 (AArch64 version).
4602 * Currently handles all of VAE1, VAAE1, VAALE1 and VALE1,
4603 * since we don't support flush-for-specific-ASID-only or
4604 * flush-last-level-only.
4605 */
90c19cdf
RH
4606 CPUState *cs = env_cpu(env);
4607 int mask = vae1_tlbmask(env);
b4ab8ce9
PM
4608 uint64_t pageaddr = sextract64(value << 12, 0, 56);
4609
4610 if (tlb_force_broadcast(env)) {
527db2be
RH
4611 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr, mask);
4612 } else {
4613 tlb_flush_page_by_mmuidx(cs, pageaddr, mask);
b4ab8ce9 4614 }
b4ab8ce9
PM
4615}
4616
fd3ed969
PM
4617static void tlbi_aa64_vae2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4618 uint64_t value)
fa439fc5 4619{
29a0af61 4620 CPUState *cs = env_cpu(env);
fd3ed969 4621 uint64_t pageaddr = sextract64(value << 12, 0, 56);
fa439fc5 4622
a67cf277 4623 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
e013b741 4624 ARMMMUIdxBit_E2);
fa439fc5
PM
4625}
4626
43efaa33
PM
4627static void tlbi_aa64_vae3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4628 uint64_t value)
4629{
29a0af61 4630 CPUState *cs = env_cpu(env);
43efaa33
PM
4631 uint64_t pageaddr = sextract64(value << 12, 0, 56);
4632
a67cf277 4633 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
127b2b08 4634 ARMMMUIdxBit_SE3);
43efaa33
PM
4635}
4636
3f208fd7
PM
4637static CPAccessResult aa64_zva_access(CPUARMState *env, const ARMCPRegInfo *ri,
4638 bool isread)
aca3f40b 4639{
4351cb72
RH
4640 int cur_el = arm_current_el(env);
4641
4642 if (cur_el < 2) {
4643 uint64_t hcr = arm_hcr_el2_eff(env);
4644
4645 if (cur_el == 0) {
4646 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
4647 if (!(env->cp15.sctlr_el[2] & SCTLR_DZE)) {
4648 return CP_ACCESS_TRAP_EL2;
4649 }
4650 } else {
4651 if (!(env->cp15.sctlr_el[1] & SCTLR_DZE)) {
4652 return CP_ACCESS_TRAP;
4653 }
4654 if (hcr & HCR_TDZ) {
4655 return CP_ACCESS_TRAP_EL2;
4656 }
4657 }
4658 } else if (hcr & HCR_TDZ) {
4659 return CP_ACCESS_TRAP_EL2;
4660 }
aca3f40b
PM
4661 }
4662 return CP_ACCESS_OK;
4663}
4664
4665static uint64_t aa64_dczid_read(CPUARMState *env, const ARMCPRegInfo *ri)
4666{
2fc0cc0e 4667 ARMCPU *cpu = env_archcpu(env);
aca3f40b
PM
4668 int dzp_bit = 1 << 4;
4669
4670 /* DZP indicates whether DC ZVA access is allowed */
3f208fd7 4671 if (aa64_zva_access(env, NULL, false) == CP_ACCESS_OK) {
aca3f40b
PM
4672 dzp_bit = 0;
4673 }
4674 return cpu->dcz_blocksize | dzp_bit;
4675}
4676
3f208fd7
PM
4677static CPAccessResult sp_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
4678 bool isread)
f502cfc2 4679{
cdcf1405 4680 if (!(env->pstate & PSTATE_SP)) {
f502cfc2
PM
4681 /* Access to SP_EL0 is undefined if it's being used as
4682 * the stack pointer.
4683 */
4684 return CP_ACCESS_TRAP_UNCATEGORIZED;
4685 }
4686 return CP_ACCESS_OK;
4687}
4688
4689static uint64_t spsel_read(CPUARMState *env, const ARMCPRegInfo *ri)
4690{
4691 return env->pstate & PSTATE_SP;
4692}
4693
4694static void spsel_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
4695{
4696 update_spsel(env, val);
4697}
4698
137feaa9
FA
4699static void sctlr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4700 uint64_t value)
4701{
2fc0cc0e 4702 ARMCPU *cpu = env_archcpu(env);
137feaa9 4703
f00faf13
RH
4704 if (arm_feature(env, ARM_FEATURE_PMSA) && !cpu->has_mpu) {
4705 /* M bit is RAZ/WI for PMSA with no MPU implemented */
4706 value &= ~SCTLR_M;
4707 }
4708
4709 /* ??? Lots of these bits are not implemented. */
4710
4711 if (ri->state == ARM_CP_STATE_AA64 && !cpu_isar_feature(aa64_mte, cpu)) {
4712 if (ri->opc1 == 6) { /* SCTLR_EL3 */
4713 value &= ~(SCTLR_ITFSB | SCTLR_TCF | SCTLR_ATA);
4714 } else {
4715 value &= ~(SCTLR_ITFSB | SCTLR_TCF0 | SCTLR_TCF |
4716 SCTLR_ATA0 | SCTLR_ATA);
4717 }
4718 }
4719
137feaa9
FA
4720 if (raw_read(env, ri) == value) {
4721 /* Skip the TLB flush if nothing actually changed; Linux likes
4722 * to do a lot of pointless SCTLR writes.
4723 */
4724 return;
4725 }
4726
4727 raw_write(env, ri, value);
f00faf13 4728
137feaa9 4729 /* This may enable/disable the MMU, so do a TLB flush. */
d10eb08f 4730 tlb_flush(CPU(cpu));
2e5dcf36
RH
4731
4732 if (ri->type & ARM_CP_SUPPRESS_TB_END) {
4733 /*
4734 * Normally we would always end the TB on an SCTLR write; see the
4735 * comment in ARMCPRegInfo sctlr initialization below for why Xscale
4736 * is special. Setting ARM_CP_SUPPRESS_TB_END also stops the rebuild
4737 * of hflags from the translator, so do it here.
4738 */
4739 arm_rebuild_hflags(env);
4740 }
137feaa9
FA
4741}
4742
3f208fd7
PM
4743static CPAccessResult fpexc32_access(CPUARMState *env, const ARMCPRegInfo *ri,
4744 bool isread)
03fbf20f
PM
4745{
4746 if ((env->cp15.cptr_el[2] & CPTR_TFP) && arm_current_el(env) == 2) {
f2cae609 4747 return CP_ACCESS_TRAP_FP_EL2;
03fbf20f
PM
4748 }
4749 if (env->cp15.cptr_el[3] & CPTR_TFP) {
f2cae609 4750 return CP_ACCESS_TRAP_FP_EL3;
03fbf20f
PM
4751 }
4752 return CP_ACCESS_OK;
4753}
4754
a8d64e73
PM
4755static void sdcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4756 uint64_t value)
4757{
4758 env->cp15.mdcr_el3 = value & SDCR_VALID_MASK;
4759}
4760
b0d2b7d0
PM
4761static const ARMCPRegInfo v8_cp_reginfo[] = {
4762 /* Minimal set of EL0-visible registers. This will need to be expanded
4763 * significantly for system emulation of AArch64 CPUs.
4764 */
4765 { .name = "NZCV", .state = ARM_CP_STATE_AA64,
4766 .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 2,
4767 .access = PL0_RW, .type = ARM_CP_NZCV },
c2b820fe
PM
4768 { .name = "DAIF", .state = ARM_CP_STATE_AA64,
4769 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 2,
7a0e58fa 4770 .type = ARM_CP_NO_RAW,
c2b820fe
PM
4771 .access = PL0_RW, .accessfn = aa64_daif_access,
4772 .fieldoffset = offsetof(CPUARMState, daif),
4773 .writefn = aa64_daif_write, .resetfn = arm_cp_reset_ignore },
b0d2b7d0
PM
4774 { .name = "FPCR", .state = ARM_CP_STATE_AA64,
4775 .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 4,
b916c9c3 4776 .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END,
fe03d45f 4777 .readfn = aa64_fpcr_read, .writefn = aa64_fpcr_write },
b0d2b7d0
PM
4778 { .name = "FPSR", .state = ARM_CP_STATE_AA64,
4779 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 4,
b916c9c3 4780 .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END,
fe03d45f 4781 .readfn = aa64_fpsr_read, .writefn = aa64_fpsr_write },
b0d2b7d0
PM
4782 { .name = "DCZID_EL0", .state = ARM_CP_STATE_AA64,
4783 .opc0 = 3, .opc1 = 3, .opc2 = 7, .crn = 0, .crm = 0,
7a0e58fa 4784 .access = PL0_R, .type = ARM_CP_NO_RAW,
aca3f40b
PM
4785 .readfn = aa64_dczid_read },
4786 { .name = "DC_ZVA", .state = ARM_CP_STATE_AA64,
4787 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 1,
4788 .access = PL0_W, .type = ARM_CP_DC_ZVA,
4789#ifndef CONFIG_USER_ONLY
4790 /* Avoid overhead of an access check that always passes in user-mode */
4791 .accessfn = aa64_zva_access,
4792#endif
4793 },
0eef9d98
PM
4794 { .name = "CURRENTEL", .state = ARM_CP_STATE_AA64,
4795 .opc0 = 3, .opc1 = 0, .opc2 = 2, .crn = 4, .crm = 2,
4796 .access = PL1_R, .type = ARM_CP_CURRENTEL },
8af35c37
PM
4797 /* Cache ops: all NOPs since we don't emulate caches */
4798 { .name = "IC_IALLUIS", .state = ARM_CP_STATE_AA64,
4799 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
38262d8a
RH
4800 .access = PL1_W, .type = ARM_CP_NOP,
4801 .accessfn = aa64_cacheop_pou_access },
8af35c37
PM
4802 { .name = "IC_IALLU", .state = ARM_CP_STATE_AA64,
4803 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
38262d8a
RH
4804 .access = PL1_W, .type = ARM_CP_NOP,
4805 .accessfn = aa64_cacheop_pou_access },
8af35c37
PM
4806 { .name = "IC_IVAU", .state = ARM_CP_STATE_AA64,
4807 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 5, .opc2 = 1,
4808 .access = PL0_W, .type = ARM_CP_NOP,
38262d8a 4809 .accessfn = aa64_cacheop_pou_access },
8af35c37
PM
4810 { .name = "DC_IVAC", .state = ARM_CP_STATE_AA64,
4811 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
1bed4d2e
RH
4812 .access = PL1_W, .accessfn = aa64_cacheop_poc_access,
4813 .type = ARM_CP_NOP },
8af35c37
PM
4814 { .name = "DC_ISW", .state = ARM_CP_STATE_AA64,
4815 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
1803d271 4816 .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
8af35c37
PM
4817 { .name = "DC_CVAC", .state = ARM_CP_STATE_AA64,
4818 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 1,
4819 .access = PL0_W, .type = ARM_CP_NOP,
1bed4d2e 4820 .accessfn = aa64_cacheop_poc_access },
8af35c37
PM
4821 { .name = "DC_CSW", .state = ARM_CP_STATE_AA64,
4822 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
1803d271 4823 .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
8af35c37
PM
4824 { .name = "DC_CVAU", .state = ARM_CP_STATE_AA64,
4825 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 11, .opc2 = 1,
4826 .access = PL0_W, .type = ARM_CP_NOP,
38262d8a 4827 .accessfn = aa64_cacheop_pou_access },
8af35c37
PM
4828 { .name = "DC_CIVAC", .state = ARM_CP_STATE_AA64,
4829 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 1,
4830 .access = PL0_W, .type = ARM_CP_NOP,
1bed4d2e 4831 .accessfn = aa64_cacheop_poc_access },
8af35c37
PM
4832 { .name = "DC_CISW", .state = ARM_CP_STATE_AA64,
4833 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
1803d271 4834 .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
168aa23b
PM
4835 /* TLBI operations */
4836 { .name = "TLBI_VMALLE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 4837 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
30881b73 4838 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
fd3ed969 4839 .writefn = tlbi_aa64_vmalle1is_write },
168aa23b 4840 { .name = "TLBI_VAE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 4841 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
30881b73 4842 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
fd3ed969 4843 .writefn = tlbi_aa64_vae1is_write },
168aa23b 4844 { .name = "TLBI_ASIDE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 4845 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
30881b73 4846 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
fd3ed969 4847 .writefn = tlbi_aa64_vmalle1is_write },
168aa23b 4848 { .name = "TLBI_VAAE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 4849 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
30881b73 4850 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
fd3ed969 4851 .writefn = tlbi_aa64_vae1is_write },
168aa23b 4852 { .name = "TLBI_VALE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 4853 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
30881b73 4854 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
fd3ed969 4855 .writefn = tlbi_aa64_vae1is_write },
168aa23b 4856 { .name = "TLBI_VAALE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 4857 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
30881b73 4858 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
fd3ed969 4859 .writefn = tlbi_aa64_vae1is_write },
168aa23b 4860 { .name = "TLBI_VMALLE1", .state = ARM_CP_STATE_AA64,
6ab9f499 4861 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
30881b73 4862 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
fd3ed969 4863 .writefn = tlbi_aa64_vmalle1_write },
168aa23b 4864 { .name = "TLBI_VAE1", .state = ARM_CP_STATE_AA64,
6ab9f499 4865 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
30881b73 4866 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
fd3ed969 4867 .writefn = tlbi_aa64_vae1_write },
168aa23b 4868 { .name = "TLBI_ASIDE1", .state = ARM_CP_STATE_AA64,
6ab9f499 4869 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
30881b73 4870 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
fd3ed969 4871 .writefn = tlbi_aa64_vmalle1_write },
168aa23b 4872 { .name = "TLBI_VAAE1", .state = ARM_CP_STATE_AA64,
6ab9f499 4873 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
30881b73 4874 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
fd3ed969 4875 .writefn = tlbi_aa64_vae1_write },
168aa23b 4876 { .name = "TLBI_VALE1", .state = ARM_CP_STATE_AA64,
6ab9f499 4877 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
30881b73 4878 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
fd3ed969 4879 .writefn = tlbi_aa64_vae1_write },
168aa23b 4880 { .name = "TLBI_VAALE1", .state = ARM_CP_STATE_AA64,
6ab9f499 4881 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
30881b73 4882 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
fd3ed969 4883 .writefn = tlbi_aa64_vae1_write },
cea66e91
PM
4884 { .name = "TLBI_IPAS2E1IS", .state = ARM_CP_STATE_AA64,
4885 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1,
bf05340c 4886 .access = PL2_W, .type = ARM_CP_NOP },
cea66e91
PM
4887 { .name = "TLBI_IPAS2LE1IS", .state = ARM_CP_STATE_AA64,
4888 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5,
bf05340c 4889 .access = PL2_W, .type = ARM_CP_NOP },
83ddf975
PM
4890 { .name = "TLBI_ALLE1IS", .state = ARM_CP_STATE_AA64,
4891 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
4892 .access = PL2_W, .type = ARM_CP_NO_RAW,
fd3ed969 4893 .writefn = tlbi_aa64_alle1is_write },
43efaa33
PM
4894 { .name = "TLBI_VMALLS12E1IS", .state = ARM_CP_STATE_AA64,
4895 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 6,
4896 .access = PL2_W, .type = ARM_CP_NO_RAW,
4897 .writefn = tlbi_aa64_alle1is_write },
cea66e91
PM
4898 { .name = "TLBI_IPAS2E1", .state = ARM_CP_STATE_AA64,
4899 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1,
bf05340c 4900 .access = PL2_W, .type = ARM_CP_NOP },
cea66e91
PM
4901 { .name = "TLBI_IPAS2LE1", .state = ARM_CP_STATE_AA64,
4902 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5,
bf05340c 4903 .access = PL2_W, .type = ARM_CP_NOP },
83ddf975
PM
4904 { .name = "TLBI_ALLE1", .state = ARM_CP_STATE_AA64,
4905 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
4906 .access = PL2_W, .type = ARM_CP_NO_RAW,
fd3ed969 4907 .writefn = tlbi_aa64_alle1_write },
43efaa33
PM
4908 { .name = "TLBI_VMALLS12E1", .state = ARM_CP_STATE_AA64,
4909 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 6,
4910 .access = PL2_W, .type = ARM_CP_NO_RAW,
4911 .writefn = tlbi_aa64_alle1is_write },
19525524
PM
4912#ifndef CONFIG_USER_ONLY
4913 /* 64 bit address translation operations */
4914 { .name = "AT_S1E1R", .state = ARM_CP_STATE_AA64,
4915 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 0,
0710b2fa
PM
4916 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4917 .writefn = ats_write64 },
19525524
PM
4918 { .name = "AT_S1E1W", .state = ARM_CP_STATE_AA64,
4919 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 1,
0710b2fa
PM
4920 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4921 .writefn = ats_write64 },
19525524
PM
4922 { .name = "AT_S1E0R", .state = ARM_CP_STATE_AA64,
4923 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 2,
0710b2fa
PM
4924 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4925 .writefn = ats_write64 },
19525524
PM
4926 { .name = "AT_S1E0W", .state = ARM_CP_STATE_AA64,
4927 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 3,
0710b2fa
PM
4928 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4929 .writefn = ats_write64 },
2a47df95 4930 { .name = "AT_S12E1R", .state = ARM_CP_STATE_AA64,
7a379c7e 4931 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 4,
0710b2fa
PM
4932 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4933 .writefn = ats_write64 },
2a47df95 4934 { .name = "AT_S12E1W", .state = ARM_CP_STATE_AA64,
7a379c7e 4935 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 5,
0710b2fa
PM
4936 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4937 .writefn = ats_write64 },
2a47df95 4938 { .name = "AT_S12E0R", .state = ARM_CP_STATE_AA64,
7a379c7e 4939 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 6,
0710b2fa
PM
4940 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4941 .writefn = ats_write64 },
2a47df95 4942 { .name = "AT_S12E0W", .state = ARM_CP_STATE_AA64,
7a379c7e 4943 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 7,
0710b2fa
PM
4944 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4945 .writefn = ats_write64 },
2a47df95
PM
4946 /* AT S1E2* are elsewhere as they UNDEF from EL3 if EL2 is not present */
4947 { .name = "AT_S1E3R", .state = ARM_CP_STATE_AA64,
4948 .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 0,
0710b2fa
PM
4949 .access = PL3_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4950 .writefn = ats_write64 },
2a47df95
PM
4951 { .name = "AT_S1E3W", .state = ARM_CP_STATE_AA64,
4952 .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 1,
0710b2fa
PM
4953 .access = PL3_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4954 .writefn = ats_write64 },
c96fc9b5
EI
4955 { .name = "PAR_EL1", .state = ARM_CP_STATE_AA64,
4956 .type = ARM_CP_ALIAS,
4957 .opc0 = 3, .opc1 = 0, .crn = 7, .crm = 4, .opc2 = 0,
4958 .access = PL1_RW, .resetvalue = 0,
4959 .fieldoffset = offsetof(CPUARMState, cp15.par_el[1]),
4960 .writefn = par_write },
19525524 4961#endif
995939a6 4962 /* TLB invalidate last level of translation table walk */
9449fdf6 4963 { .name = "TLBIMVALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
30881b73
RH
4964 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
4965 .writefn = tlbimva_is_write },
9449fdf6 4966 { .name = "TLBIMVAALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
30881b73 4967 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
fa439fc5 4968 .writefn = tlbimvaa_is_write },
9449fdf6 4969 { .name = "TLBIMVAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
30881b73
RH
4970 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
4971 .writefn = tlbimva_write },
9449fdf6 4972 { .name = "TLBIMVAAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
30881b73
RH
4973 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
4974 .writefn = tlbimvaa_write },
541ef8c2
SS
4975 { .name = "TLBIMVALH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
4976 .type = ARM_CP_NO_RAW, .access = PL2_W,
4977 .writefn = tlbimva_hyp_write },
4978 { .name = "TLBIMVALHIS",
4979 .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
4980 .type = ARM_CP_NO_RAW, .access = PL2_W,
4981 .writefn = tlbimva_hyp_is_write },
4982 { .name = "TLBIIPAS2",
4983 .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1,
bf05340c 4984 .type = ARM_CP_NOP, .access = PL2_W },
541ef8c2
SS
4985 { .name = "TLBIIPAS2IS",
4986 .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1,
bf05340c 4987 .type = ARM_CP_NOP, .access = PL2_W },
541ef8c2
SS
4988 { .name = "TLBIIPAS2L",
4989 .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5,
bf05340c 4990 .type = ARM_CP_NOP, .access = PL2_W },
541ef8c2
SS
4991 { .name = "TLBIIPAS2LIS",
4992 .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5,
bf05340c 4993 .type = ARM_CP_NOP, .access = PL2_W },
9449fdf6
PM
4994 /* 32 bit cache operations */
4995 { .name = "ICIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
38262d8a 4996 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_pou_access },
9449fdf6
PM
4997 { .name = "BPIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 6,
4998 .type = ARM_CP_NOP, .access = PL1_W },
4999 { .name = "ICIALLU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
38262d8a 5000 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_pou_access },
9449fdf6 5001 { .name = "ICIMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 1,
38262d8a 5002 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_pou_access },
9449fdf6
PM
5003 { .name = "BPIALL", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 6,
5004 .type = ARM_CP_NOP, .access = PL1_W },
5005 { .name = "BPIMVA", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 7,
5006 .type = ARM_CP_NOP, .access = PL1_W },
5007 { .name = "DCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
1bed4d2e 5008 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
9449fdf6 5009 { .name = "DCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
1803d271 5010 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
9449fdf6 5011 { .name = "DCCMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 1,
1bed4d2e 5012 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
9449fdf6 5013 { .name = "DCCSW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
1803d271 5014 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
9449fdf6 5015 { .name = "DCCMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 11, .opc2 = 1,
38262d8a 5016 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_pou_access },
9449fdf6 5017 { .name = "DCCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 1,
1bed4d2e 5018 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
9449fdf6 5019 { .name = "DCCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
1803d271 5020 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
9449fdf6 5021 /* MMU Domain access control / MPU write buffer control */
0c17d68c 5022 { .name = "DACR", .cp = 15, .opc1 = 0, .crn = 3, .crm = 0, .opc2 = 0,
84929218 5023 .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
0c17d68c
FA
5024 .writefn = dacr_write, .raw_writefn = raw_write,
5025 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
5026 offsetoflow32(CPUARMState, cp15.dacr_ns) } },
a0618a19 5027 { .name = "ELR_EL1", .state = ARM_CP_STATE_AA64,
7a0e58fa 5028 .type = ARM_CP_ALIAS,
a0618a19 5029 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 1,
6947f059
EI
5030 .access = PL1_RW,
5031 .fieldoffset = offsetof(CPUARMState, elr_el[1]) },
a65f1de9 5032 { .name = "SPSR_EL1", .state = ARM_CP_STATE_AA64,
7a0e58fa 5033 .type = ARM_CP_ALIAS,
a65f1de9 5034 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 0,
99a99c1f
SB
5035 .access = PL1_RW,
5036 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_SVC]) },
f502cfc2
PM
5037 /* We rely on the access checks not allowing the guest to write to the
5038 * state field when SPSel indicates that it's being used as the stack
5039 * pointer.
5040 */
5041 { .name = "SP_EL0", .state = ARM_CP_STATE_AA64,
5042 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 1, .opc2 = 0,
5043 .access = PL1_RW, .accessfn = sp_el0_access,
7a0e58fa 5044 .type = ARM_CP_ALIAS,
f502cfc2 5045 .fieldoffset = offsetof(CPUARMState, sp_el[0]) },
884b4dee
GB
5046 { .name = "SP_EL1", .state = ARM_CP_STATE_AA64,
5047 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 1, .opc2 = 0,
7a0e58fa 5048 .access = PL2_RW, .type = ARM_CP_ALIAS,
884b4dee 5049 .fieldoffset = offsetof(CPUARMState, sp_el[1]) },
f502cfc2
PM
5050 { .name = "SPSel", .state = ARM_CP_STATE_AA64,
5051 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 0,
7a0e58fa 5052 .type = ARM_CP_NO_RAW,
f502cfc2 5053 .access = PL1_RW, .readfn = spsel_read, .writefn = spsel_write },
03fbf20f
PM
5054 { .name = "FPEXC32_EL2", .state = ARM_CP_STATE_AA64,
5055 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 3, .opc2 = 0,
5056 .type = ARM_CP_ALIAS,
5057 .fieldoffset = offsetof(CPUARMState, vfp.xregs[ARM_VFP_FPEXC]),
5058 .access = PL2_RW, .accessfn = fpexc32_access },
6a43e0b6
PM
5059 { .name = "DACR32_EL2", .state = ARM_CP_STATE_AA64,
5060 .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 0, .opc2 = 0,
5061 .access = PL2_RW, .resetvalue = 0,
5062 .writefn = dacr_write, .raw_writefn = raw_write,
5063 .fieldoffset = offsetof(CPUARMState, cp15.dacr32_el2) },
5064 { .name = "IFSR32_EL2", .state = ARM_CP_STATE_AA64,
5065 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 0, .opc2 = 1,
5066 .access = PL2_RW, .resetvalue = 0,
5067 .fieldoffset = offsetof(CPUARMState, cp15.ifsr32_el2) },
5068 { .name = "SPSR_IRQ", .state = ARM_CP_STATE_AA64,
5069 .type = ARM_CP_ALIAS,
5070 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 0,
5071 .access = PL2_RW,
5072 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_IRQ]) },
5073 { .name = "SPSR_ABT", .state = ARM_CP_STATE_AA64,
5074 .type = ARM_CP_ALIAS,
5075 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 1,
5076 .access = PL2_RW,
5077 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_ABT]) },
5078 { .name = "SPSR_UND", .state = ARM_CP_STATE_AA64,
5079 .type = ARM_CP_ALIAS,
5080 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 2,
5081 .access = PL2_RW,
5082 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_UND]) },
5083 { .name = "SPSR_FIQ", .state = ARM_CP_STATE_AA64,
5084 .type = ARM_CP_ALIAS,
5085 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 3,
5086 .access = PL2_RW,
5087 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_FIQ]) },
a8d64e73
PM
5088 { .name = "MDCR_EL3", .state = ARM_CP_STATE_AA64,
5089 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 3, .opc2 = 1,
5090 .resetvalue = 0,
5091 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el3) },
5092 { .name = "SDCR", .type = ARM_CP_ALIAS,
5093 .cp = 15, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 1,
5094 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
5095 .writefn = sdcr_write,
5096 .fieldoffset = offsetoflow32(CPUARMState, cp15.mdcr_el3) },
b0d2b7d0
PM
5097 REGINFO_SENTINEL
5098};
5099
d42e3c26 5100/* Used to describe the behaviour of EL2 regs when EL2 does not exist. */
4771cd01 5101static const ARMCPRegInfo el3_no_el2_cp_reginfo[] = {
d79e0c06 5102 { .name = "VBAR_EL2", .state = ARM_CP_STATE_BOTH,
d42e3c26
EI
5103 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0,
5104 .access = PL2_RW,
5105 .readfn = arm_cp_read_zero, .writefn = arm_cp_write_ignore },
ce4afed8 5106 { .name = "HCR_EL2", .state = ARM_CP_STATE_BOTH,
7a0e58fa 5107 .type = ARM_CP_NO_RAW,
f149e3e8
EI
5108 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
5109 .access = PL2_RW,
ce4afed8 5110 .type = ARM_CP_CONST, .resetvalue = 0 },
831a2fca
PM
5111 { .name = "HACR_EL2", .state = ARM_CP_STATE_BOTH,
5112 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 7,
5113 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
68e78e33
PM
5114 { .name = "ESR_EL2", .state = ARM_CP_STATE_BOTH,
5115 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 0,
5116 .access = PL2_RW,
5117 .type = ARM_CP_CONST, .resetvalue = 0 },
c6f19164
GB
5118 { .name = "CPTR_EL2", .state = ARM_CP_STATE_BOTH,
5119 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 2,
5120 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
95f949ac
EI
5121 { .name = "MAIR_EL2", .state = ARM_CP_STATE_BOTH,
5122 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 0,
5123 .access = PL2_RW, .type = ARM_CP_CONST,
5124 .resetvalue = 0 },
5125 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
b5ede85b 5126 .cp = 15, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 1,
95f949ac 5127 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
2179ef95
PM
5128 { .name = "AMAIR_EL2", .state = ARM_CP_STATE_BOTH,
5129 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 0,
5130 .access = PL2_RW, .type = ARM_CP_CONST,
5131 .resetvalue = 0 },
55b53c71 5132 { .name = "HAMAIR1", .state = ARM_CP_STATE_AA32,
b5ede85b 5133 .cp = 15, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 1,
2179ef95
PM
5134 .access = PL2_RW, .type = ARM_CP_CONST,
5135 .resetvalue = 0 },
37cd6c24
PM
5136 { .name = "AFSR0_EL2", .state = ARM_CP_STATE_BOTH,
5137 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 0,
5138 .access = PL2_RW, .type = ARM_CP_CONST,
5139 .resetvalue = 0 },
5140 { .name = "AFSR1_EL2", .state = ARM_CP_STATE_BOTH,
5141 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 1,
5142 .access = PL2_RW, .type = ARM_CP_CONST,
5143 .resetvalue = 0 },
06ec4c8c
EI
5144 { .name = "TCR_EL2", .state = ARM_CP_STATE_BOTH,
5145 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 2,
5146 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
68e9c2fe
EI
5147 { .name = "VTCR_EL2", .state = ARM_CP_STATE_BOTH,
5148 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
93dd1e61 5149 .access = PL2_RW, .accessfn = access_el3_aa32ns,
68e9c2fe 5150 .type = ARM_CP_CONST, .resetvalue = 0 },
b698e9cf
EI
5151 { .name = "VTTBR", .state = ARM_CP_STATE_AA32,
5152 .cp = 15, .opc1 = 6, .crm = 2,
5153 .access = PL2_RW, .accessfn = access_el3_aa32ns,
5154 .type = ARM_CP_CONST | ARM_CP_64BIT, .resetvalue = 0 },
5155 { .name = "VTTBR_EL2", .state = ARM_CP_STATE_AA64,
5156 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 0,
5157 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
b9cb5323
EI
5158 { .name = "SCTLR_EL2", .state = ARM_CP_STATE_BOTH,
5159 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 0,
5160 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
ff05f37b
EI
5161 { .name = "TPIDR_EL2", .state = ARM_CP_STATE_BOTH,
5162 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 2,
5163 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
a57633c0
EI
5164 { .name = "TTBR0_EL2", .state = ARM_CP_STATE_AA64,
5165 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
5166 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
5167 { .name = "HTTBR", .cp = 15, .opc1 = 4, .crm = 2,
5168 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_CONST,
5169 .resetvalue = 0 },
0b6440af
EI
5170 { .name = "CNTHCTL_EL2", .state = ARM_CP_STATE_BOTH,
5171 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 1, .opc2 = 0,
5172 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
edac4d8a
EI
5173 { .name = "CNTVOFF_EL2", .state = ARM_CP_STATE_AA64,
5174 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 3,
5175 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
5176 { .name = "CNTVOFF", .cp = 15, .opc1 = 4, .crm = 14,
5177 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_CONST,
5178 .resetvalue = 0 },
b0e66d95
EI
5179 { .name = "CNTHP_CVAL_EL2", .state = ARM_CP_STATE_AA64,
5180 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 2,
5181 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
5182 { .name = "CNTHP_CVAL", .cp = 15, .opc1 = 6, .crm = 14,
5183 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_CONST,
5184 .resetvalue = 0 },
5185 { .name = "CNTHP_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
5186 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 0,
5187 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
5188 { .name = "CNTHP_CTL_EL2", .state = ARM_CP_STATE_BOTH,
5189 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 1,
5190 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
14cc7b54
SF
5191 { .name = "MDCR_EL2", .state = ARM_CP_STATE_BOTH,
5192 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 1,
d6c8cf81
PM
5193 .access = PL2_RW, .accessfn = access_tda,
5194 .type = ARM_CP_CONST, .resetvalue = 0 },
59e05530
EI
5195 { .name = "HPFAR_EL2", .state = ARM_CP_STATE_BOTH,
5196 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
93dd1e61 5197 .access = PL2_RW, .accessfn = access_el3_aa32ns,
59e05530 5198 .type = ARM_CP_CONST, .resetvalue = 0 },
2a5a9abd
AF
5199 { .name = "HSTR_EL2", .state = ARM_CP_STATE_BOTH,
5200 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 3,
5201 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
cba517c3
PM
5202 { .name = "FAR_EL2", .state = ARM_CP_STATE_BOTH,
5203 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 0,
5204 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
5205 { .name = "HIFAR", .state = ARM_CP_STATE_AA32,
5206 .type = ARM_CP_CONST,
5207 .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 2,
5208 .access = PL2_RW, .resetvalue = 0 },
d42e3c26
EI
5209 REGINFO_SENTINEL
5210};
5211
ce4afed8
PM
5212/* Ditto, but for registers which exist in ARMv8 but not v7 */
5213static const ARMCPRegInfo el3_no_el2_v8_cp_reginfo[] = {
5214 { .name = "HCR2", .state = ARM_CP_STATE_AA32,
5215 .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 4,
5216 .access = PL2_RW,
5217 .type = ARM_CP_CONST, .resetvalue = 0 },
5218 REGINFO_SENTINEL
5219};
5220
d1fb4da2 5221static void do_hcr_write(CPUARMState *env, uint64_t value, uint64_t valid_mask)
f149e3e8 5222{
2fc0cc0e 5223 ARMCPU *cpu = env_archcpu(env);
d1fb4da2
RH
5224
5225 if (arm_feature(env, ARM_FEATURE_V8)) {
5226 valid_mask |= MAKE_64BIT_MASK(0, 34); /* ARMv8.0 */
5227 } else {
5228 valid_mask |= MAKE_64BIT_MASK(0, 28); /* ARMv7VE */
5229 }
f149e3e8
EI
5230
5231 if (arm_feature(env, ARM_FEATURE_EL3)) {
5232 valid_mask &= ~HCR_HCD;
77077a83
JK
5233 } else if (cpu->psci_conduit != QEMU_PSCI_CONDUIT_SMC) {
5234 /* Architecturally HCR.TSC is RES0 if EL3 is not implemented.
5235 * However, if we're using the SMC PSCI conduit then QEMU is
5236 * effectively acting like EL3 firmware and so the guest at
5237 * EL2 should retain the ability to prevent EL1 from being
5238 * able to make SMC calls into the ersatz firmware, so in
5239 * that case HCR.TSC should be read/write.
5240 */
f149e3e8
EI
5241 valid_mask &= ~HCR_TSC;
5242 }
d1fb4da2
RH
5243
5244 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
5245 if (cpu_isar_feature(aa64_vh, cpu)) {
5246 valid_mask |= HCR_E2H;
5247 }
5248 if (cpu_isar_feature(aa64_lor, cpu)) {
5249 valid_mask |= HCR_TLOR;
5250 }
5251 if (cpu_isar_feature(aa64_pauth, cpu)) {
5252 valid_mask |= HCR_API | HCR_APK;
5253 }
8ddb300b
RH
5254 if (cpu_isar_feature(aa64_mte, cpu)) {
5255 valid_mask |= HCR_ATA | HCR_DCT | HCR_TID5;
5256 }
ef682cdb 5257 }
f149e3e8
EI
5258
5259 /* Clear RES0 bits. */
5260 value &= valid_mask;
5261
8ddb300b
RH
5262 /*
5263 * These bits change the MMU setup:
f149e3e8
EI
5264 * HCR_VM enables stage 2 translation
5265 * HCR_PTW forbids certain page-table setups
8ddb300b
RH
5266 * HCR_DC disables stage1 and enables stage2 translation
5267 * HCR_DCT enables tagging on (disabled) stage1 translation
f149e3e8 5268 */
8ddb300b 5269 if ((env->cp15.hcr_el2 ^ value) & (HCR_VM | HCR_PTW | HCR_DC | HCR_DCT)) {
d10eb08f 5270 tlb_flush(CPU(cpu));
f149e3e8 5271 }
ce4afed8 5272 env->cp15.hcr_el2 = value;
89430fc6
PM
5273
5274 /*
5275 * Updates to VI and VF require us to update the status of
5276 * virtual interrupts, which are the logical OR of these bits
5277 * and the state of the input lines from the GIC. (This requires
5278 * that we have the iothread lock, which is done by marking the
5279 * reginfo structs as ARM_CP_IO.)
5280 * Note that if a write to HCR pends a VIRQ or VFIQ it is never
5281 * possible for it to be taken immediately, because VIRQ and
5282 * VFIQ are masked unless running at EL0 or EL1, and HCR
5283 * can only be written at EL2.
5284 */
5285 g_assert(qemu_mutex_iothread_locked());
5286 arm_cpu_update_virq(cpu);
5287 arm_cpu_update_vfiq(cpu);
ce4afed8
PM
5288}
5289
d1fb4da2
RH
5290static void hcr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
5291{
5292 do_hcr_write(env, value, 0);
5293}
5294
ce4afed8
PM
5295static void hcr_writehigh(CPUARMState *env, const ARMCPRegInfo *ri,
5296 uint64_t value)
5297{
5298 /* Handle HCR2 write, i.e. write to high half of HCR_EL2 */
5299 value = deposit64(env->cp15.hcr_el2, 32, 32, value);
d1fb4da2 5300 do_hcr_write(env, value, MAKE_64BIT_MASK(0, 32));
ce4afed8
PM
5301}
5302
5303static void hcr_writelow(CPUARMState *env, const ARMCPRegInfo *ri,
5304 uint64_t value)
5305{
5306 /* Handle HCR write, i.e. write to low half of HCR_EL2 */
5307 value = deposit64(env->cp15.hcr_el2, 0, 32, value);
d1fb4da2 5308 do_hcr_write(env, value, MAKE_64BIT_MASK(32, 32));
f149e3e8
EI
5309}
5310
f7778444
RH
5311/*
5312 * Return the effective value of HCR_EL2.
5313 * Bits that are not included here:
5314 * RW (read from SCR_EL3.RW as needed)
5315 */
5316uint64_t arm_hcr_el2_eff(CPUARMState *env)
5317{
5318 uint64_t ret = env->cp15.hcr_el2;
5319
5320 if (arm_is_secure_below_el3(env)) {
5321 /*
5322 * "This register has no effect if EL2 is not enabled in the
5323 * current Security state". This is ARMv8.4-SecEL2 speak for
5324 * !(SCR_EL3.NS==1 || SCR_EL3.EEL2==1).
5325 *
5326 * Prior to that, the language was "In an implementation that
5327 * includes EL3, when the value of SCR_EL3.NS is 0 the PE behaves
5328 * as if this field is 0 for all purposes other than a direct
5329 * read or write access of HCR_EL2". With lots of enumeration
5330 * on a per-field basis. In current QEMU, this is condition
5331 * is arm_is_secure_below_el3.
5332 *
5333 * Since the v8.4 language applies to the entire register, and
5334 * appears to be backward compatible, use that.
5335 */
4990e1d3
RH
5336 return 0;
5337 }
5338
5339 /*
5340 * For a cpu that supports both aarch64 and aarch32, we can set bits
5341 * in HCR_EL2 (e.g. via EL3) that are RES0 when we enter EL2 as aa32.
5342 * Ignore all of the bits in HCR+HCR2 that are not valid for aarch32.
5343 */
5344 if (!arm_el_is_aa64(env, 2)) {
5345 uint64_t aa32_valid;
5346
5347 /*
5348 * These bits are up-to-date as of ARMv8.6.
5349 * For HCR, it's easiest to list just the 2 bits that are invalid.
5350 * For HCR2, list those that are valid.
5351 */
5352 aa32_valid = MAKE_64BIT_MASK(0, 32) & ~(HCR_RW | HCR_TDZ);
5353 aa32_valid |= (HCR_CD | HCR_ID | HCR_TERR | HCR_TEA | HCR_MIOCNCE |
5354 HCR_TID4 | HCR_TICAB | HCR_TOCU | HCR_TTLBIS);
5355 ret &= aa32_valid;
5356 }
5357
5358 if (ret & HCR_TGE) {
5359 /* These bits are up-to-date as of ARMv8.6. */
f7778444
RH
5360 if (ret & HCR_E2H) {
5361 ret &= ~(HCR_VM | HCR_FMO | HCR_IMO | HCR_AMO |
5362 HCR_BSU_MASK | HCR_DC | HCR_TWI | HCR_TWE |
5363 HCR_TID0 | HCR_TID2 | HCR_TPCP | HCR_TPU |
4990e1d3
RH
5364 HCR_TDZ | HCR_CD | HCR_ID | HCR_MIOCNCE |
5365 HCR_TID4 | HCR_TICAB | HCR_TOCU | HCR_ENSCXT |
5366 HCR_TTLBIS | HCR_TTLBOS | HCR_TID5);
f7778444
RH
5367 } else {
5368 ret |= HCR_FMO | HCR_IMO | HCR_AMO;
5369 }
5370 ret &= ~(HCR_SWIO | HCR_PTW | HCR_VF | HCR_VI | HCR_VSE |
5371 HCR_FB | HCR_TID1 | HCR_TID3 | HCR_TSC | HCR_TACR |
5372 HCR_TSW | HCR_TTLB | HCR_TVM | HCR_HCD | HCR_TRVM |
5373 HCR_TLOR);
5374 }
5375
5376 return ret;
5377}
5378
fc1120a7
PM
5379static void cptr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
5380 uint64_t value)
5381{
5382 /*
5383 * For A-profile AArch32 EL3, if NSACR.CP10
5384 * is 0 then HCPTR.{TCP11,TCP10} ignore writes and read as 1.
5385 */
5386 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
5387 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
5388 value &= ~(0x3 << 10);
5389 value |= env->cp15.cptr_el[2] & (0x3 << 10);
5390 }
5391 env->cp15.cptr_el[2] = value;
5392}
5393
5394static uint64_t cptr_el2_read(CPUARMState *env, const ARMCPRegInfo *ri)
5395{
5396 /*
5397 * For A-profile AArch32 EL3, if NSACR.CP10
5398 * is 0 then HCPTR.{TCP11,TCP10} ignore writes and read as 1.
5399 */
5400 uint64_t value = env->cp15.cptr_el[2];
5401
5402 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
5403 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
5404 value |= 0x3 << 10;
5405 }
5406 return value;
5407}
5408
4771cd01 5409static const ARMCPRegInfo el2_cp_reginfo[] = {
f149e3e8 5410 { .name = "HCR_EL2", .state = ARM_CP_STATE_AA64,
89430fc6 5411 .type = ARM_CP_IO,
f149e3e8
EI
5412 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
5413 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
c624ea0f 5414 .writefn = hcr_write },
ce4afed8 5415 { .name = "HCR", .state = ARM_CP_STATE_AA32,
89430fc6 5416 .type = ARM_CP_ALIAS | ARM_CP_IO,
ce4afed8
PM
5417 .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
5418 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
c624ea0f 5419 .writefn = hcr_writelow },
831a2fca
PM
5420 { .name = "HACR_EL2", .state = ARM_CP_STATE_BOTH,
5421 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 7,
5422 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3b685ba7 5423 { .name = "ELR_EL2", .state = ARM_CP_STATE_AA64,
7a0e58fa 5424 .type = ARM_CP_ALIAS,
3b685ba7
EI
5425 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 1,
5426 .access = PL2_RW,
5427 .fieldoffset = offsetof(CPUARMState, elr_el[2]) },
68e78e33 5428 { .name = "ESR_EL2", .state = ARM_CP_STATE_BOTH,
f2c30f42
EI
5429 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 0,
5430 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[2]) },
cba517c3 5431 { .name = "FAR_EL2", .state = ARM_CP_STATE_BOTH,
63b60551
EI
5432 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 0,
5433 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[2]) },
cba517c3
PM
5434 { .name = "HIFAR", .state = ARM_CP_STATE_AA32,
5435 .type = ARM_CP_ALIAS,
5436 .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 2,
5437 .access = PL2_RW,
5438 .fieldoffset = offsetofhigh32(CPUARMState, cp15.far_el[2]) },
3b685ba7 5439 { .name = "SPSR_EL2", .state = ARM_CP_STATE_AA64,
7a0e58fa 5440 .type = ARM_CP_ALIAS,
3b685ba7 5441 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 0,
99a99c1f
SB
5442 .access = PL2_RW,
5443 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_HYP]) },
d79e0c06 5444 { .name = "VBAR_EL2", .state = ARM_CP_STATE_BOTH,
d42e3c26
EI
5445 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0,
5446 .access = PL2_RW, .writefn = vbar_write,
5447 .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[2]),
5448 .resetvalue = 0 },
884b4dee
GB
5449 { .name = "SP_EL2", .state = ARM_CP_STATE_AA64,
5450 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 1, .opc2 = 0,
7a0e58fa 5451 .access = PL3_RW, .type = ARM_CP_ALIAS,
884b4dee 5452 .fieldoffset = offsetof(CPUARMState, sp_el[2]) },
c6f19164
GB
5453 { .name = "CPTR_EL2", .state = ARM_CP_STATE_BOTH,
5454 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 2,
5455 .access = PL2_RW, .accessfn = cptr_access, .resetvalue = 0,
fc1120a7
PM
5456 .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[2]),
5457 .readfn = cptr_el2_read, .writefn = cptr_el2_write },
95f949ac
EI
5458 { .name = "MAIR_EL2", .state = ARM_CP_STATE_BOTH,
5459 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 0,
5460 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[2]),
5461 .resetvalue = 0 },
5462 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
b5ede85b 5463 .cp = 15, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 1,
95f949ac
EI
5464 .access = PL2_RW, .type = ARM_CP_ALIAS,
5465 .fieldoffset = offsetofhigh32(CPUARMState, cp15.mair_el[2]) },
2179ef95
PM
5466 { .name = "AMAIR_EL2", .state = ARM_CP_STATE_BOTH,
5467 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 0,
5468 .access = PL2_RW, .type = ARM_CP_CONST,
5469 .resetvalue = 0 },
5470 /* HAMAIR1 is mapped to AMAIR_EL2[63:32] */
55b53c71 5471 { .name = "HAMAIR1", .state = ARM_CP_STATE_AA32,
b5ede85b 5472 .cp = 15, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 1,
2179ef95
PM
5473 .access = PL2_RW, .type = ARM_CP_CONST,
5474 .resetvalue = 0 },
37cd6c24
PM
5475 { .name = "AFSR0_EL2", .state = ARM_CP_STATE_BOTH,
5476 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 0,
5477 .access = PL2_RW, .type = ARM_CP_CONST,
5478 .resetvalue = 0 },
5479 { .name = "AFSR1_EL2", .state = ARM_CP_STATE_BOTH,
5480 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 1,
5481 .access = PL2_RW, .type = ARM_CP_CONST,
5482 .resetvalue = 0 },
06ec4c8c
EI
5483 { .name = "TCR_EL2", .state = ARM_CP_STATE_BOTH,
5484 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 2,
d06dc933
RH
5485 .access = PL2_RW, .writefn = vmsa_tcr_el12_write,
5486 /* no .raw_writefn or .resetfn needed as we never use mask/base_mask */
06ec4c8c 5487 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[2]) },
68e9c2fe
EI
5488 { .name = "VTCR", .state = ARM_CP_STATE_AA32,
5489 .cp = 15, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
bf06c112 5490 .type = ARM_CP_ALIAS,
68e9c2fe
EI
5491 .access = PL2_RW, .accessfn = access_el3_aa32ns,
5492 .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) },
5493 { .name = "VTCR_EL2", .state = ARM_CP_STATE_AA64,
5494 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
bf06c112
PM
5495 .access = PL2_RW,
5496 /* no .writefn needed as this can't cause an ASID change;
5497 * no .raw_writefn or .resetfn needed as we never use mask/base_mask
5498 */
68e9c2fe 5499 .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) },
b698e9cf
EI
5500 { .name = "VTTBR", .state = ARM_CP_STATE_AA32,
5501 .cp = 15, .opc1 = 6, .crm = 2,
5502 .type = ARM_CP_64BIT | ARM_CP_ALIAS,
5503 .access = PL2_RW, .accessfn = access_el3_aa32ns,
5504 .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2),
5505 .writefn = vttbr_write },
5506 { .name = "VTTBR_EL2", .state = ARM_CP_STATE_AA64,
5507 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 0,
5508 .access = PL2_RW, .writefn = vttbr_write,
5509 .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2) },
b9cb5323
EI
5510 { .name = "SCTLR_EL2", .state = ARM_CP_STATE_BOTH,
5511 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 0,
5512 .access = PL2_RW, .raw_writefn = raw_write, .writefn = sctlr_write,
5513 .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[2]) },
ff05f37b
EI
5514 { .name = "TPIDR_EL2", .state = ARM_CP_STATE_BOTH,
5515 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 2,
5516 .access = PL2_RW, .resetvalue = 0,
5517 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[2]) },
a57633c0
EI
5518 { .name = "TTBR0_EL2", .state = ARM_CP_STATE_AA64,
5519 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
ed30da8e 5520 .access = PL2_RW, .resetvalue = 0, .writefn = vmsa_tcr_ttbr_el2_write,
a57633c0
EI
5521 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
5522 { .name = "HTTBR", .cp = 15, .opc1 = 4, .crm = 2,
5523 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
a57633c0 5524 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
541ef8c2
SS
5525 { .name = "TLBIALLNSNH",
5526 .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
5527 .type = ARM_CP_NO_RAW, .access = PL2_W,
5528 .writefn = tlbiall_nsnh_write },
5529 { .name = "TLBIALLNSNHIS",
5530 .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
5531 .type = ARM_CP_NO_RAW, .access = PL2_W,
5532 .writefn = tlbiall_nsnh_is_write },
5533 { .name = "TLBIALLH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
5534 .type = ARM_CP_NO_RAW, .access = PL2_W,
5535 .writefn = tlbiall_hyp_write },
5536 { .name = "TLBIALLHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
5537 .type = ARM_CP_NO_RAW, .access = PL2_W,
5538 .writefn = tlbiall_hyp_is_write },
5539 { .name = "TLBIMVAH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
5540 .type = ARM_CP_NO_RAW, .access = PL2_W,
5541 .writefn = tlbimva_hyp_write },
5542 { .name = "TLBIMVAHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
5543 .type = ARM_CP_NO_RAW, .access = PL2_W,
5544 .writefn = tlbimva_hyp_is_write },
51da9014
EI
5545 { .name = "TLBI_ALLE2", .state = ARM_CP_STATE_AA64,
5546 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
5547 .type = ARM_CP_NO_RAW, .access = PL2_W,
fd3ed969 5548 .writefn = tlbi_aa64_alle2_write },
8742d49d
EI
5549 { .name = "TLBI_VAE2", .state = ARM_CP_STATE_AA64,
5550 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
5551 .type = ARM_CP_NO_RAW, .access = PL2_W,
fd3ed969 5552 .writefn = tlbi_aa64_vae2_write },
2bfb9d75
PM
5553 { .name = "TLBI_VALE2", .state = ARM_CP_STATE_AA64,
5554 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
5555 .access = PL2_W, .type = ARM_CP_NO_RAW,
5556 .writefn = tlbi_aa64_vae2_write },
5557 { .name = "TLBI_ALLE2IS", .state = ARM_CP_STATE_AA64,
5558 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
5559 .access = PL2_W, .type = ARM_CP_NO_RAW,
5560 .writefn = tlbi_aa64_alle2is_write },
8742d49d
EI
5561 { .name = "TLBI_VAE2IS", .state = ARM_CP_STATE_AA64,
5562 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
5563 .type = ARM_CP_NO_RAW, .access = PL2_W,
fd3ed969 5564 .writefn = tlbi_aa64_vae2is_write },
2bfb9d75
PM
5565 { .name = "TLBI_VALE2IS", .state = ARM_CP_STATE_AA64,
5566 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
5567 .access = PL2_W, .type = ARM_CP_NO_RAW,
5568 .writefn = tlbi_aa64_vae2is_write },
edac4d8a 5569#ifndef CONFIG_USER_ONLY
2a47df95
PM
5570 /* Unlike the other EL2-related AT operations, these must
5571 * UNDEF from EL3 if EL2 is not implemented, which is why we
5572 * define them here rather than with the rest of the AT ops.
5573 */
5574 { .name = "AT_S1E2R", .state = ARM_CP_STATE_AA64,
5575 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
5576 .access = PL2_W, .accessfn = at_s1e2_access,
0710b2fa 5577 .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC, .writefn = ats_write64 },
2a47df95
PM
5578 { .name = "AT_S1E2W", .state = ARM_CP_STATE_AA64,
5579 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
5580 .access = PL2_W, .accessfn = at_s1e2_access,
0710b2fa 5581 .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC, .writefn = ats_write64 },
14db7fe0
PM
5582 /* The AArch32 ATS1H* operations are CONSTRAINED UNPREDICTABLE
5583 * if EL2 is not implemented; we choose to UNDEF. Behaviour at EL3
5584 * with SCR.NS == 0 outside Monitor mode is UNPREDICTABLE; we choose
5585 * to behave as if SCR.NS was 1.
5586 */
5587 { .name = "ATS1HR", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
5588 .access = PL2_W,
0710b2fa 5589 .writefn = ats1h_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
14db7fe0
PM
5590 { .name = "ATS1HW", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
5591 .access = PL2_W,
0710b2fa 5592 .writefn = ats1h_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
0b6440af
EI
5593 { .name = "CNTHCTL_EL2", .state = ARM_CP_STATE_BOTH,
5594 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 1, .opc2 = 0,
5595 /* ARMv7 requires bit 0 and 1 to reset to 1. ARMv8 defines the
5596 * reset values as IMPDEF. We choose to reset to 3 to comply with
5597 * both ARMv7 and ARMv8.
5598 */
5599 .access = PL2_RW, .resetvalue = 3,
5600 .fieldoffset = offsetof(CPUARMState, cp15.cnthctl_el2) },
edac4d8a
EI
5601 { .name = "CNTVOFF_EL2", .state = ARM_CP_STATE_AA64,
5602 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 3,
5603 .access = PL2_RW, .type = ARM_CP_IO, .resetvalue = 0,
5604 .writefn = gt_cntvoff_write,
5605 .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
5606 { .name = "CNTVOFF", .cp = 15, .opc1 = 4, .crm = 14,
5607 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS | ARM_CP_IO,
5608 .writefn = gt_cntvoff_write,
5609 .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
b0e66d95
EI
5610 { .name = "CNTHP_CVAL_EL2", .state = ARM_CP_STATE_AA64,
5611 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 2,
5612 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
5613 .type = ARM_CP_IO, .access = PL2_RW,
5614 .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
5615 { .name = "CNTHP_CVAL", .cp = 15, .opc1 = 6, .crm = 14,
5616 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
5617 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_IO,
5618 .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
5619 { .name = "CNTHP_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
5620 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 0,
d44ec156 5621 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
b0e66d95
EI
5622 .resetfn = gt_hyp_timer_reset,
5623 .readfn = gt_hyp_tval_read, .writefn = gt_hyp_tval_write },
5624 { .name = "CNTHP_CTL_EL2", .state = ARM_CP_STATE_BOTH,
5625 .type = ARM_CP_IO,
5626 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 1,
5627 .access = PL2_RW,
5628 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].ctl),
5629 .resetvalue = 0,
5630 .writefn = gt_hyp_ctl_write, .raw_writefn = raw_write },
edac4d8a 5631#endif
14cc7b54
SF
5632 /* The only field of MDCR_EL2 that has a defined architectural reset value
5633 * is MDCR_EL2.HPMN which should reset to the value of PMCR_EL0.N; but we
5ecdd3e4 5634 * don't implement any PMU event counters, so using zero as a reset
14cc7b54
SF
5635 * value for MDCR_EL2 is okay
5636 */
5637 { .name = "MDCR_EL2", .state = ARM_CP_STATE_BOTH,
5638 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 1,
5639 .access = PL2_RW, .resetvalue = 0,
5640 .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el2), },
59e05530
EI
5641 { .name = "HPFAR", .state = ARM_CP_STATE_AA32,
5642 .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
5643 .access = PL2_RW, .accessfn = access_el3_aa32ns,
5644 .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
5645 { .name = "HPFAR_EL2", .state = ARM_CP_STATE_AA64,
5646 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
5647 .access = PL2_RW,
5648 .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
2a5a9abd
AF
5649 { .name = "HSTR_EL2", .state = ARM_CP_STATE_BOTH,
5650 .cp = 15, .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 3,
5651 .access = PL2_RW,
5652 .fieldoffset = offsetof(CPUARMState, cp15.hstr_el2) },
3b685ba7
EI
5653 REGINFO_SENTINEL
5654};
5655
ce4afed8
PM
5656static const ARMCPRegInfo el2_v8_cp_reginfo[] = {
5657 { .name = "HCR2", .state = ARM_CP_STATE_AA32,
89430fc6 5658 .type = ARM_CP_ALIAS | ARM_CP_IO,
ce4afed8
PM
5659 .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 4,
5660 .access = PL2_RW,
5661 .fieldoffset = offsetofhigh32(CPUARMState, cp15.hcr_el2),
5662 .writefn = hcr_writehigh },
5663 REGINFO_SENTINEL
5664};
5665
2f027fc5
PM
5666static CPAccessResult nsacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
5667 bool isread)
5668{
5669 /* The NSACR is RW at EL3, and RO for NS EL1 and NS EL2.
5670 * At Secure EL1 it traps to EL3.
5671 */
5672 if (arm_current_el(env) == 3) {
5673 return CP_ACCESS_OK;
5674 }
5675 if (arm_is_secure_below_el3(env)) {
5676 return CP_ACCESS_TRAP_EL3;
5677 }
5678 /* Accesses from EL1 NS and EL2 NS are UNDEF for write but allow reads. */
5679 if (isread) {
5680 return CP_ACCESS_OK;
5681 }
5682 return CP_ACCESS_TRAP_UNCATEGORIZED;
5683}
5684
60fb1a87
GB
5685static const ARMCPRegInfo el3_cp_reginfo[] = {
5686 { .name = "SCR_EL3", .state = ARM_CP_STATE_AA64,
5687 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 0,
5688 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.scr_el3),
5689 .resetvalue = 0, .writefn = scr_write },
f80741d1 5690 { .name = "SCR", .type = ARM_CP_ALIAS | ARM_CP_NEWEL,
60fb1a87 5691 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 0,
efe4a274
PM
5692 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
5693 .fieldoffset = offsetoflow32(CPUARMState, cp15.scr_el3),
b061a82b 5694 .writefn = scr_write },
60fb1a87
GB
5695 { .name = "SDER32_EL3", .state = ARM_CP_STATE_AA64,
5696 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 1,
5697 .access = PL3_RW, .resetvalue = 0,
5698 .fieldoffset = offsetof(CPUARMState, cp15.sder) },
5699 { .name = "SDER",
5700 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 1,
5701 .access = PL3_RW, .resetvalue = 0,
5702 .fieldoffset = offsetoflow32(CPUARMState, cp15.sder) },
60fb1a87 5703 { .name = "MVBAR", .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
efe4a274
PM
5704 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
5705 .writefn = vbar_write, .resetvalue = 0,
60fb1a87 5706 .fieldoffset = offsetof(CPUARMState, cp15.mvbar) },
7dd8c9af
FA
5707 { .name = "TTBR0_EL3", .state = ARM_CP_STATE_AA64,
5708 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 0,
f478847f 5709 .access = PL3_RW, .resetvalue = 0,
7dd8c9af 5710 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[3]) },
11f136ee
FA
5711 { .name = "TCR_EL3", .state = ARM_CP_STATE_AA64,
5712 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 2,
6459b94c
PM
5713 .access = PL3_RW,
5714 /* no .writefn needed as this can't cause an ASID change;
811595a2
PM
5715 * we must provide a .raw_writefn and .resetfn because we handle
5716 * reset and migration for the AArch32 TTBCR(S), which might be
5717 * using mask and base_mask.
6459b94c 5718 */
811595a2 5719 .resetfn = vmsa_ttbcr_reset, .raw_writefn = vmsa_ttbcr_raw_write,
11f136ee 5720 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[3]) },
81547d66 5721 { .name = "ELR_EL3", .state = ARM_CP_STATE_AA64,
7a0e58fa 5722 .type = ARM_CP_ALIAS,
81547d66
EI
5723 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 1,
5724 .access = PL3_RW,
5725 .fieldoffset = offsetof(CPUARMState, elr_el[3]) },
f2c30f42 5726 { .name = "ESR_EL3", .state = ARM_CP_STATE_AA64,
f2c30f42
EI
5727 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 2, .opc2 = 0,
5728 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[3]) },
63b60551
EI
5729 { .name = "FAR_EL3", .state = ARM_CP_STATE_AA64,
5730 .opc0 = 3, .opc1 = 6, .crn = 6, .crm = 0, .opc2 = 0,
5731 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[3]) },
81547d66 5732 { .name = "SPSR_EL3", .state = ARM_CP_STATE_AA64,
7a0e58fa 5733 .type = ARM_CP_ALIAS,
81547d66 5734 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 0,
99a99c1f
SB
5735 .access = PL3_RW,
5736 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_MON]) },
a1ba125c
EI
5737 { .name = "VBAR_EL3", .state = ARM_CP_STATE_AA64,
5738 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 0,
5739 .access = PL3_RW, .writefn = vbar_write,
5740 .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[3]),
5741 .resetvalue = 0 },
c6f19164
GB
5742 { .name = "CPTR_EL3", .state = ARM_CP_STATE_AA64,
5743 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 2,
5744 .access = PL3_RW, .accessfn = cptr_access, .resetvalue = 0,
5745 .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[3]) },
4cfb8ad8
PM
5746 { .name = "TPIDR_EL3", .state = ARM_CP_STATE_AA64,
5747 .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 2,
5748 .access = PL3_RW, .resetvalue = 0,
5749 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[3]) },
2179ef95
PM
5750 { .name = "AMAIR_EL3", .state = ARM_CP_STATE_AA64,
5751 .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 3, .opc2 = 0,
5752 .access = PL3_RW, .type = ARM_CP_CONST,
5753 .resetvalue = 0 },
37cd6c24
PM
5754 { .name = "AFSR0_EL3", .state = ARM_CP_STATE_BOTH,
5755 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 0,
5756 .access = PL3_RW, .type = ARM_CP_CONST,
5757 .resetvalue = 0 },
5758 { .name = "AFSR1_EL3", .state = ARM_CP_STATE_BOTH,
5759 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 1,
5760 .access = PL3_RW, .type = ARM_CP_CONST,
5761 .resetvalue = 0 },
43efaa33
PM
5762 { .name = "TLBI_ALLE3IS", .state = ARM_CP_STATE_AA64,
5763 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 0,
5764 .access = PL3_W, .type = ARM_CP_NO_RAW,
5765 .writefn = tlbi_aa64_alle3is_write },
5766 { .name = "TLBI_VAE3IS", .state = ARM_CP_STATE_AA64,
5767 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 1,
5768 .access = PL3_W, .type = ARM_CP_NO_RAW,
5769 .writefn = tlbi_aa64_vae3is_write },
5770 { .name = "TLBI_VALE3IS", .state = ARM_CP_STATE_AA64,
5771 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 5,
5772 .access = PL3_W, .type = ARM_CP_NO_RAW,
5773 .writefn = tlbi_aa64_vae3is_write },
5774 { .name = "TLBI_ALLE3", .state = ARM_CP_STATE_AA64,
5775 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 0,
5776 .access = PL3_W, .type = ARM_CP_NO_RAW,
5777 .writefn = tlbi_aa64_alle3_write },
5778 { .name = "TLBI_VAE3", .state = ARM_CP_STATE_AA64,
5779 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 1,
5780 .access = PL3_W, .type = ARM_CP_NO_RAW,
5781 .writefn = tlbi_aa64_vae3_write },
5782 { .name = "TLBI_VALE3", .state = ARM_CP_STATE_AA64,
5783 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 5,
5784 .access = PL3_W, .type = ARM_CP_NO_RAW,
5785 .writefn = tlbi_aa64_vae3_write },
0f1a3b24
FA
5786 REGINFO_SENTINEL
5787};
5788
e2cce18f
RH
5789#ifndef CONFIG_USER_ONLY
5790/* Test if system register redirection is to occur in the current state. */
5791static bool redirect_for_e2h(CPUARMState *env)
5792{
5793 return arm_current_el(env) == 2 && (arm_hcr_el2_eff(env) & HCR_E2H);
5794}
5795
5796static uint64_t el2_e2h_read(CPUARMState *env, const ARMCPRegInfo *ri)
5797{
5798 CPReadFn *readfn;
5799
5800 if (redirect_for_e2h(env)) {
5801 /* Switch to the saved EL2 version of the register. */
5802 ri = ri->opaque;
5803 readfn = ri->readfn;
5804 } else {
5805 readfn = ri->orig_readfn;
5806 }
5807 if (readfn == NULL) {
5808 readfn = raw_read;
5809 }
5810 return readfn(env, ri);
5811}
5812
5813static void el2_e2h_write(CPUARMState *env, const ARMCPRegInfo *ri,
5814 uint64_t value)
5815{
5816 CPWriteFn *writefn;
5817
5818 if (redirect_for_e2h(env)) {
5819 /* Switch to the saved EL2 version of the register. */
5820 ri = ri->opaque;
5821 writefn = ri->writefn;
5822 } else {
5823 writefn = ri->orig_writefn;
5824 }
5825 if (writefn == NULL) {
5826 writefn = raw_write;
5827 }
5828 writefn(env, ri, value);
5829}
5830
5831static void define_arm_vh_e2h_redirects_aliases(ARMCPU *cpu)
5832{
5833 struct E2HAlias {
5834 uint32_t src_key, dst_key, new_key;
5835 const char *src_name, *dst_name, *new_name;
5836 bool (*feature)(const ARMISARegisters *id);
5837 };
5838
5839#define K(op0, op1, crn, crm, op2) \
5840 ENCODE_AA64_CP_REG(CP_REG_ARM64_SYSREG_CP, crn, crm, op0, op1, op2)
5841
5842 static const struct E2HAlias aliases[] = {
5843 { K(3, 0, 1, 0, 0), K(3, 4, 1, 0, 0), K(3, 5, 1, 0, 0),
5844 "SCTLR", "SCTLR_EL2", "SCTLR_EL12" },
5845 { K(3, 0, 1, 0, 2), K(3, 4, 1, 1, 2), K(3, 5, 1, 0, 2),
5846 "CPACR", "CPTR_EL2", "CPACR_EL12" },
5847 { K(3, 0, 2, 0, 0), K(3, 4, 2, 0, 0), K(3, 5, 2, 0, 0),
5848 "TTBR0_EL1", "TTBR0_EL2", "TTBR0_EL12" },
5849 { K(3, 0, 2, 0, 1), K(3, 4, 2, 0, 1), K(3, 5, 2, 0, 1),
5850 "TTBR1_EL1", "TTBR1_EL2", "TTBR1_EL12" },
5851 { K(3, 0, 2, 0, 2), K(3, 4, 2, 0, 2), K(3, 5, 2, 0, 2),
5852 "TCR_EL1", "TCR_EL2", "TCR_EL12" },
5853 { K(3, 0, 4, 0, 0), K(3, 4, 4, 0, 0), K(3, 5, 4, 0, 0),
5854 "SPSR_EL1", "SPSR_EL2", "SPSR_EL12" },
5855 { K(3, 0, 4, 0, 1), K(3, 4, 4, 0, 1), K(3, 5, 4, 0, 1),
5856 "ELR_EL1", "ELR_EL2", "ELR_EL12" },
5857 { K(3, 0, 5, 1, 0), K(3, 4, 5, 1, 0), K(3, 5, 5, 1, 0),
5858 "AFSR0_EL1", "AFSR0_EL2", "AFSR0_EL12" },
5859 { K(3, 0, 5, 1, 1), K(3, 4, 5, 1, 1), K(3, 5, 5, 1, 1),
5860 "AFSR1_EL1", "AFSR1_EL2", "AFSR1_EL12" },
5861 { K(3, 0, 5, 2, 0), K(3, 4, 5, 2, 0), K(3, 5, 5, 2, 0),
5862 "ESR_EL1", "ESR_EL2", "ESR_EL12" },
5863 { K(3, 0, 6, 0, 0), K(3, 4, 6, 0, 0), K(3, 5, 6, 0, 0),
5864 "FAR_EL1", "FAR_EL2", "FAR_EL12" },
5865 { K(3, 0, 10, 2, 0), K(3, 4, 10, 2, 0), K(3, 5, 10, 2, 0),
5866 "MAIR_EL1", "MAIR_EL2", "MAIR_EL12" },
5867 { K(3, 0, 10, 3, 0), K(3, 4, 10, 3, 0), K(3, 5, 10, 3, 0),
5868 "AMAIR0", "AMAIR_EL2", "AMAIR_EL12" },
5869 { K(3, 0, 12, 0, 0), K(3, 4, 12, 0, 0), K(3, 5, 12, 0, 0),
5870 "VBAR", "VBAR_EL2", "VBAR_EL12" },
5871 { K(3, 0, 13, 0, 1), K(3, 4, 13, 0, 1), K(3, 5, 13, 0, 1),
5872 "CONTEXTIDR_EL1", "CONTEXTIDR_EL2", "CONTEXTIDR_EL12" },
5873 { K(3, 0, 14, 1, 0), K(3, 4, 14, 1, 0), K(3, 5, 14, 1, 0),
5874 "CNTKCTL", "CNTHCTL_EL2", "CNTKCTL_EL12" },
5875
5876 /*
5877 * Note that redirection of ZCR is mentioned in the description
5878 * of ZCR_EL2, and aliasing in the description of ZCR_EL1, but
5879 * not in the summary table.
5880 */
5881 { K(3, 0, 1, 2, 0), K(3, 4, 1, 2, 0), K(3, 5, 1, 2, 0),
5882 "ZCR_EL1", "ZCR_EL2", "ZCR_EL12", isar_feature_aa64_sve },
5883
4b779ceb
RH
5884 { K(3, 0, 5, 6, 0), K(3, 4, 5, 6, 0), K(3, 5, 5, 6, 0),
5885 "TFSR_EL1", "TFSR_EL2", "TFSR_EL12", isar_feature_aa64_mte },
5886
e2cce18f
RH
5887 /* TODO: ARMv8.2-SPE -- PMSCR_EL2 */
5888 /* TODO: ARMv8.4-Trace -- TRFCR_EL2 */
5889 };
5890#undef K
5891
5892 size_t i;
5893
5894 for (i = 0; i < ARRAY_SIZE(aliases); i++) {
5895 const struct E2HAlias *a = &aliases[i];
5896 ARMCPRegInfo *src_reg, *dst_reg;
5897
5898 if (a->feature && !a->feature(&cpu->isar)) {
5899 continue;
5900 }
5901
5902 src_reg = g_hash_table_lookup(cpu->cp_regs, &a->src_key);
5903 dst_reg = g_hash_table_lookup(cpu->cp_regs, &a->dst_key);
5904 g_assert(src_reg != NULL);
5905 g_assert(dst_reg != NULL);
5906
5907 /* Cross-compare names to detect typos in the keys. */
5908 g_assert(strcmp(src_reg->name, a->src_name) == 0);
5909 g_assert(strcmp(dst_reg->name, a->dst_name) == 0);
5910
5911 /* None of the core system registers use opaque; we will. */
5912 g_assert(src_reg->opaque == NULL);
5913
5914 /* Create alias before redirection so we dup the right data. */
5915 if (a->new_key) {
5916 ARMCPRegInfo *new_reg = g_memdup(src_reg, sizeof(ARMCPRegInfo));
5917 uint32_t *new_key = g_memdup(&a->new_key, sizeof(uint32_t));
5918 bool ok;
5919
5920 new_reg->name = a->new_name;
5921 new_reg->type |= ARM_CP_ALIAS;
5922 /* Remove PL1/PL0 access, leaving PL2/PL3 R/W in place. */
5923 new_reg->access &= PL2_RW | PL3_RW;
5924
5925 ok = g_hash_table_insert(cpu->cp_regs, new_key, new_reg);
5926 g_assert(ok);
5927 }
5928
5929 src_reg->opaque = dst_reg;
5930 src_reg->orig_readfn = src_reg->readfn ?: raw_read;
5931 src_reg->orig_writefn = src_reg->writefn ?: raw_write;
5932 if (!src_reg->raw_readfn) {
5933 src_reg->raw_readfn = raw_read;
5934 }
5935 if (!src_reg->raw_writefn) {
5936 src_reg->raw_writefn = raw_write;
5937 }
5938 src_reg->readfn = el2_e2h_read;
5939 src_reg->writefn = el2_e2h_write;
5940 }
5941}
5942#endif
5943
3f208fd7
PM
5944static CPAccessResult ctr_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
5945 bool isread)
7da845b0 5946{
97475a89
RH
5947 int cur_el = arm_current_el(env);
5948
5949 if (cur_el < 2) {
5950 uint64_t hcr = arm_hcr_el2_eff(env);
5951
5952 if (cur_el == 0) {
5953 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
5954 if (!(env->cp15.sctlr_el[2] & SCTLR_UCT)) {
5955 return CP_ACCESS_TRAP_EL2;
5956 }
5957 } else {
5958 if (!(env->cp15.sctlr_el[1] & SCTLR_UCT)) {
5959 return CP_ACCESS_TRAP;
5960 }
5961 if (hcr & HCR_TID2) {
5962 return CP_ACCESS_TRAP_EL2;
5963 }
5964 }
5965 } else if (hcr & HCR_TID2) {
5966 return CP_ACCESS_TRAP_EL2;
5967 }
7da845b0 5968 }
630fcd4d
MZ
5969
5970 if (arm_current_el(env) < 2 && arm_hcr_el2_eff(env) & HCR_TID2) {
5971 return CP_ACCESS_TRAP_EL2;
5972 }
5973
7da845b0
PM
5974 return CP_ACCESS_OK;
5975}
5976
1424ca8d
DM
5977static void oslar_write(CPUARMState *env, const ARMCPRegInfo *ri,
5978 uint64_t value)
5979{
5980 /* Writes to OSLAR_EL1 may update the OS lock status, which can be
5981 * read via a bit in OSLSR_EL1.
5982 */
5983 int oslock;
5984
5985 if (ri->state == ARM_CP_STATE_AA32) {
5986 oslock = (value == 0xC5ACCE55);
5987 } else {
5988 oslock = value & 1;
5989 }
5990
5991 env->cp15.oslsr_el1 = deposit32(env->cp15.oslsr_el1, 1, 1, oslock);
5992}
5993
50300698 5994static const ARMCPRegInfo debug_cp_reginfo[] = {
50300698 5995 /* DBGDRAR, DBGDSAR: always RAZ since we don't implement memory mapped
10aae104
PM
5996 * debug components. The AArch64 version of DBGDRAR is named MDRAR_EL1;
5997 * unlike DBGDRAR it is never accessible from EL0.
5998 * DBGDSAR is deprecated and must RAZ from v8 anyway, so it has no AArch64
5999 * accessor.
50300698
PM
6000 */
6001 { .name = "DBGDRAR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 0,
91b0a238
PM
6002 .access = PL0_R, .accessfn = access_tdra,
6003 .type = ARM_CP_CONST, .resetvalue = 0 },
10aae104
PM
6004 { .name = "MDRAR_EL1", .state = ARM_CP_STATE_AA64,
6005 .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
91b0a238
PM
6006 .access = PL1_R, .accessfn = access_tdra,
6007 .type = ARM_CP_CONST, .resetvalue = 0 },
50300698 6008 { .name = "DBGDSAR", .cp = 14, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
91b0a238
PM
6009 .access = PL0_R, .accessfn = access_tdra,
6010 .type = ARM_CP_CONST, .resetvalue = 0 },
17a9eb53 6011 /* Monitor debug system control register; the 32-bit alias is DBGDSCRext. */
10aae104
PM
6012 { .name = "MDSCR_EL1", .state = ARM_CP_STATE_BOTH,
6013 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
d6c8cf81 6014 .access = PL1_RW, .accessfn = access_tda,
0e5e8935
PM
6015 .fieldoffset = offsetof(CPUARMState, cp15.mdscr_el1),
6016 .resetvalue = 0 },
5e8b12ff
PM
6017 /* MDCCSR_EL0, aka DBGDSCRint. This is a read-only mirror of MDSCR_EL1.
6018 * We don't implement the configurable EL0 access.
6019 */
6020 { .name = "MDCCSR_EL0", .state = ARM_CP_STATE_BOTH,
6021 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
7a0e58fa 6022 .type = ARM_CP_ALIAS,
d6c8cf81 6023 .access = PL1_R, .accessfn = access_tda,
b061a82b 6024 .fieldoffset = offsetof(CPUARMState, cp15.mdscr_el1), },
10aae104
PM
6025 { .name = "OSLAR_EL1", .state = ARM_CP_STATE_BOTH,
6026 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 4,
1424ca8d 6027 .access = PL1_W, .type = ARM_CP_NO_RAW,
187f678d 6028 .accessfn = access_tdosa,
1424ca8d
DM
6029 .writefn = oslar_write },
6030 { .name = "OSLSR_EL1", .state = ARM_CP_STATE_BOTH,
6031 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 4,
6032 .access = PL1_R, .resetvalue = 10,
187f678d 6033 .accessfn = access_tdosa,
1424ca8d 6034 .fieldoffset = offsetof(CPUARMState, cp15.oslsr_el1) },
5e8b12ff
PM
6035 /* Dummy OSDLR_EL1: 32-bit Linux will read this */
6036 { .name = "OSDLR_EL1", .state = ARM_CP_STATE_BOTH,
6037 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 4,
187f678d
PM
6038 .access = PL1_RW, .accessfn = access_tdosa,
6039 .type = ARM_CP_NOP },
5e8b12ff
PM
6040 /* Dummy DBGVCR: Linux wants to clear this on startup, but we don't
6041 * implement vector catch debug events yet.
6042 */
6043 { .name = "DBGVCR",
6044 .cp = 14, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
d6c8cf81
PM
6045 .access = PL1_RW, .accessfn = access_tda,
6046 .type = ARM_CP_NOP },
4d2ec4da
PM
6047 /* Dummy DBGVCR32_EL2 (which is only for a 64-bit hypervisor
6048 * to save and restore a 32-bit guest's DBGVCR)
6049 */
6050 { .name = "DBGVCR32_EL2", .state = ARM_CP_STATE_AA64,
6051 .opc0 = 2, .opc1 = 4, .crn = 0, .crm = 7, .opc2 = 0,
6052 .access = PL2_RW, .accessfn = access_tda,
6053 .type = ARM_CP_NOP },
5dbdc434
PM
6054 /* Dummy MDCCINT_EL1, since we don't implement the Debug Communications
6055 * Channel but Linux may try to access this register. The 32-bit
6056 * alias is DBGDCCINT.
6057 */
6058 { .name = "MDCCINT_EL1", .state = ARM_CP_STATE_BOTH,
6059 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0,
6060 .access = PL1_RW, .accessfn = access_tda,
6061 .type = ARM_CP_NOP },
50300698
PM
6062 REGINFO_SENTINEL
6063};
6064
6065static const ARMCPRegInfo debug_lpae_cp_reginfo[] = {
6066 /* 64 bit access versions of the (dummy) debug registers */
6067 { .name = "DBGDRAR", .cp = 14, .crm = 1, .opc1 = 0,
6068 .access = PL0_R, .type = ARM_CP_CONST|ARM_CP_64BIT, .resetvalue = 0 },
6069 { .name = "DBGDSAR", .cp = 14, .crm = 2, .opc1 = 0,
6070 .access = PL0_R, .type = ARM_CP_CONST|ARM_CP_64BIT, .resetvalue = 0 },
6071 REGINFO_SENTINEL
6072};
6073
60eed086
RH
6074/* Return the exception level to which exceptions should be taken
6075 * via SVEAccessTrap. If an exception should be routed through
6076 * AArch64.AdvSIMDFPAccessTrap, return 0; fp_exception_el should
6077 * take care of raising that exception.
6078 * C.f. the ARM pseudocode function CheckSVEEnabled.
5be5e8ed 6079 */
ced31551 6080int sve_exception_el(CPUARMState *env, int el)
5be5e8ed
RH
6081{
6082#ifndef CONFIG_USER_ONLY
c2ddb7cf
RH
6083 uint64_t hcr_el2 = arm_hcr_el2_eff(env);
6084
6085 if (el <= 1 && (hcr_el2 & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
60eed086
RH
6086 bool disabled = false;
6087
6088 /* The CPACR.ZEN controls traps to EL1:
6089 * 0, 2 : trap EL0 and EL1 accesses
6090 * 1 : trap only EL0 accesses
6091 * 3 : trap no accesses
6092 */
6093 if (!extract32(env->cp15.cpacr_el1, 16, 1)) {
6094 disabled = true;
6095 } else if (!extract32(env->cp15.cpacr_el1, 17, 1)) {
2de7ace2 6096 disabled = el == 0;
5be5e8ed 6097 }
60eed086
RH
6098 if (disabled) {
6099 /* route_to_el2 */
c2ddb7cf 6100 return hcr_el2 & HCR_TGE ? 2 : 1;
5be5e8ed 6101 }
5be5e8ed 6102
60eed086
RH
6103 /* Check CPACR.FPEN. */
6104 if (!extract32(env->cp15.cpacr_el1, 20, 1)) {
6105 disabled = true;
6106 } else if (!extract32(env->cp15.cpacr_el1, 21, 1)) {
2de7ace2 6107 disabled = el == 0;
5be5e8ed 6108 }
60eed086
RH
6109 if (disabled) {
6110 return 0;
5be5e8ed 6111 }
5be5e8ed
RH
6112 }
6113
60eed086
RH
6114 /* CPTR_EL2. Since TZ and TFP are positive,
6115 * they will be zero when EL2 is not present.
6116 */
2de7ace2 6117 if (el <= 2 && !arm_is_secure_below_el3(env)) {
60eed086
RH
6118 if (env->cp15.cptr_el[2] & CPTR_TZ) {
6119 return 2;
6120 }
6121 if (env->cp15.cptr_el[2] & CPTR_TFP) {
6122 return 0;
6123 }
5be5e8ed
RH
6124 }
6125
60eed086
RH
6126 /* CPTR_EL3. Since EZ is negative we must check for EL3. */
6127 if (arm_feature(env, ARM_FEATURE_EL3)
6128 && !(env->cp15.cptr_el[3] & CPTR_EZ)) {
5be5e8ed
RH
6129 return 3;
6130 }
6131#endif
6132 return 0;
6133}
6134
0df9142d
AJ
6135static uint32_t sve_zcr_get_valid_len(ARMCPU *cpu, uint32_t start_len)
6136{
6e553f2a 6137 uint32_t end_len;
0df9142d 6138
6e553f2a
RH
6139 end_len = start_len &= 0xf;
6140 if (!test_bit(start_len, cpu->sve_vq_map)) {
6141 end_len = find_last_bit(cpu->sve_vq_map, start_len);
6142 assert(end_len < start_len);
6143 }
6144 return end_len;
0df9142d
AJ
6145}
6146
0ab5953b
RH
6147/*
6148 * Given that SVE is enabled, return the vector length for EL.
6149 */
ced31551 6150uint32_t sve_zcr_len_for_el(CPUARMState *env, int el)
0ab5953b 6151{
2fc0cc0e 6152 ARMCPU *cpu = env_archcpu(env);
0ab5953b
RH
6153 uint32_t zcr_len = cpu->sve_max_vq - 1;
6154
6155 if (el <= 1) {
6156 zcr_len = MIN(zcr_len, 0xf & (uint32_t)env->vfp.zcr_el[1]);
6157 }
6a02a732 6158 if (el <= 2 && arm_feature(env, ARM_FEATURE_EL2)) {
0ab5953b
RH
6159 zcr_len = MIN(zcr_len, 0xf & (uint32_t)env->vfp.zcr_el[2]);
6160 }
6a02a732 6161 if (arm_feature(env, ARM_FEATURE_EL3)) {
0ab5953b
RH
6162 zcr_len = MIN(zcr_len, 0xf & (uint32_t)env->vfp.zcr_el[3]);
6163 }
0df9142d
AJ
6164
6165 return sve_zcr_get_valid_len(cpu, zcr_len);
0ab5953b
RH
6166}
6167
5be5e8ed
RH
6168static void zcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6169 uint64_t value)
6170{
0ab5953b
RH
6171 int cur_el = arm_current_el(env);
6172 int old_len = sve_zcr_len_for_el(env, cur_el);
6173 int new_len;
6174
5be5e8ed 6175 /* Bits other than [3:0] are RAZ/WI. */
7b351d98 6176 QEMU_BUILD_BUG_ON(ARM_MAX_VQ > 16);
5be5e8ed 6177 raw_write(env, ri, value & 0xf);
0ab5953b
RH
6178
6179 /*
6180 * Because we arrived here, we know both FP and SVE are enabled;
6181 * otherwise we would have trapped access to the ZCR_ELn register.
6182 */
6183 new_len = sve_zcr_len_for_el(env, cur_el);
6184 if (new_len < old_len) {
6185 aarch64_sve_narrow_vq(env, new_len + 1);
6186 }
5be5e8ed
RH
6187}
6188
6189static const ARMCPRegInfo zcr_el1_reginfo = {
6190 .name = "ZCR_EL1", .state = ARM_CP_STATE_AA64,
6191 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 0,
11d7870b 6192 .access = PL1_RW, .type = ARM_CP_SVE,
5be5e8ed
RH
6193 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[1]),
6194 .writefn = zcr_write, .raw_writefn = raw_write
6195};
6196
6197static const ARMCPRegInfo zcr_el2_reginfo = {
6198 .name = "ZCR_EL2", .state = ARM_CP_STATE_AA64,
6199 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 0,
11d7870b 6200 .access = PL2_RW, .type = ARM_CP_SVE,
5be5e8ed
RH
6201 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[2]),
6202 .writefn = zcr_write, .raw_writefn = raw_write
6203};
6204
6205static const ARMCPRegInfo zcr_no_el2_reginfo = {
6206 .name = "ZCR_EL2", .state = ARM_CP_STATE_AA64,
6207 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 0,
11d7870b 6208 .access = PL2_RW, .type = ARM_CP_SVE,
5be5e8ed
RH
6209 .readfn = arm_cp_read_zero, .writefn = arm_cp_write_ignore
6210};
6211
6212static const ARMCPRegInfo zcr_el3_reginfo = {
6213 .name = "ZCR_EL3", .state = ARM_CP_STATE_AA64,
6214 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 2, .opc2 = 0,
11d7870b 6215 .access = PL3_RW, .type = ARM_CP_SVE,
5be5e8ed
RH
6216 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[3]),
6217 .writefn = zcr_write, .raw_writefn = raw_write
6218};
6219
9ee98ce8
PM
6220void hw_watchpoint_update(ARMCPU *cpu, int n)
6221{
6222 CPUARMState *env = &cpu->env;
6223 vaddr len = 0;
6224 vaddr wvr = env->cp15.dbgwvr[n];
6225 uint64_t wcr = env->cp15.dbgwcr[n];
6226 int mask;
6227 int flags = BP_CPU | BP_STOP_BEFORE_ACCESS;
6228
6229 if (env->cpu_watchpoint[n]) {
6230 cpu_watchpoint_remove_by_ref(CPU(cpu), env->cpu_watchpoint[n]);
6231 env->cpu_watchpoint[n] = NULL;
6232 }
6233
6234 if (!extract64(wcr, 0, 1)) {
6235 /* E bit clear : watchpoint disabled */
6236 return;
6237 }
6238
6239 switch (extract64(wcr, 3, 2)) {
6240 case 0:
6241 /* LSC 00 is reserved and must behave as if the wp is disabled */
6242 return;
6243 case 1:
6244 flags |= BP_MEM_READ;
6245 break;
6246 case 2:
6247 flags |= BP_MEM_WRITE;
6248 break;
6249 case 3:
6250 flags |= BP_MEM_ACCESS;
6251 break;
6252 }
6253
6254 /* Attempts to use both MASK and BAS fields simultaneously are
6255 * CONSTRAINED UNPREDICTABLE; we opt to ignore BAS in this case,
6256 * thus generating a watchpoint for every byte in the masked region.
6257 */
6258 mask = extract64(wcr, 24, 4);
6259 if (mask == 1 || mask == 2) {
6260 /* Reserved values of MASK; we must act as if the mask value was
6261 * some non-reserved value, or as if the watchpoint were disabled.
6262 * We choose the latter.
6263 */
6264 return;
6265 } else if (mask) {
6266 /* Watchpoint covers an aligned area up to 2GB in size */
6267 len = 1ULL << mask;
6268 /* If masked bits in WVR are not zero it's CONSTRAINED UNPREDICTABLE
6269 * whether the watchpoint fires when the unmasked bits match; we opt
6270 * to generate the exceptions.
6271 */
6272 wvr &= ~(len - 1);
6273 } else {
6274 /* Watchpoint covers bytes defined by the byte address select bits */
6275 int bas = extract64(wcr, 5, 8);
6276 int basstart;
6277
9ee98ce8
PM
6278 if (extract64(wvr, 2, 1)) {
6279 /* Deprecated case of an only 4-aligned address. BAS[7:4] are
6280 * ignored, and BAS[3:0] define which bytes to watch.
6281 */
6282 bas &= 0xf;
6283 }
ae1111d4
RH
6284
6285 if (bas == 0) {
6286 /* This must act as if the watchpoint is disabled */
6287 return;
6288 }
6289
9ee98ce8
PM
6290 /* The BAS bits are supposed to be programmed to indicate a contiguous
6291 * range of bytes. Otherwise it is CONSTRAINED UNPREDICTABLE whether
6292 * we fire for each byte in the word/doubleword addressed by the WVR.
6293 * We choose to ignore any non-zero bits after the first range of 1s.
6294 */
6295 basstart = ctz32(bas);
6296 len = cto32(bas >> basstart);
6297 wvr += basstart;
6298 }
6299
6300 cpu_watchpoint_insert(CPU(cpu), wvr, len, flags,
6301 &env->cpu_watchpoint[n]);
6302}
6303
6304void hw_watchpoint_update_all(ARMCPU *cpu)
6305{
6306 int i;
6307 CPUARMState *env = &cpu->env;
6308
6309 /* Completely clear out existing QEMU watchpoints and our array, to
6310 * avoid possible stale entries following migration load.
6311 */
6312 cpu_watchpoint_remove_all(CPU(cpu), BP_CPU);
6313 memset(env->cpu_watchpoint, 0, sizeof(env->cpu_watchpoint));
6314
6315 for (i = 0; i < ARRAY_SIZE(cpu->env.cpu_watchpoint); i++) {
6316 hw_watchpoint_update(cpu, i);
6317 }
6318}
6319
6320static void dbgwvr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6321 uint64_t value)
6322{
2fc0cc0e 6323 ARMCPU *cpu = env_archcpu(env);
9ee98ce8
PM
6324 int i = ri->crm;
6325
6326 /* Bits [63:49] are hardwired to the value of bit [48]; that is, the
6327 * register reads and behaves as if values written are sign extended.
6328 * Bits [1:0] are RES0.
6329 */
6330 value = sextract64(value, 0, 49) & ~3ULL;
6331
6332 raw_write(env, ri, value);
6333 hw_watchpoint_update(cpu, i);
6334}
6335
6336static void dbgwcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6337 uint64_t value)
6338{
2fc0cc0e 6339 ARMCPU *cpu = env_archcpu(env);
9ee98ce8
PM
6340 int i = ri->crm;
6341
6342 raw_write(env, ri, value);
6343 hw_watchpoint_update(cpu, i);
6344}
6345
46747d15
PM
6346void hw_breakpoint_update(ARMCPU *cpu, int n)
6347{
6348 CPUARMState *env = &cpu->env;
6349 uint64_t bvr = env->cp15.dbgbvr[n];
6350 uint64_t bcr = env->cp15.dbgbcr[n];
6351 vaddr addr;
6352 int bt;
6353 int flags = BP_CPU;
6354
6355 if (env->cpu_breakpoint[n]) {
6356 cpu_breakpoint_remove_by_ref(CPU(cpu), env->cpu_breakpoint[n]);
6357 env->cpu_breakpoint[n] = NULL;
6358 }
6359
6360 if (!extract64(bcr, 0, 1)) {
6361 /* E bit clear : watchpoint disabled */
6362 return;
6363 }
6364
6365 bt = extract64(bcr, 20, 4);
6366
6367 switch (bt) {
6368 case 4: /* unlinked address mismatch (reserved if AArch64) */
6369 case 5: /* linked address mismatch (reserved if AArch64) */
6370 qemu_log_mask(LOG_UNIMP,
0221c8fd 6371 "arm: address mismatch breakpoint types not implemented\n");
46747d15
PM
6372 return;
6373 case 0: /* unlinked address match */
6374 case 1: /* linked address match */
6375 {
6376 /* Bits [63:49] are hardwired to the value of bit [48]; that is,
6377 * we behave as if the register was sign extended. Bits [1:0] are
6378 * RES0. The BAS field is used to allow setting breakpoints on 16
6379 * bit wide instructions; it is CONSTRAINED UNPREDICTABLE whether
6380 * a bp will fire if the addresses covered by the bp and the addresses
6381 * covered by the insn overlap but the insn doesn't start at the
6382 * start of the bp address range. We choose to require the insn and
6383 * the bp to have the same address. The constraints on writing to
6384 * BAS enforced in dbgbcr_write mean we have only four cases:
6385 * 0b0000 => no breakpoint
6386 * 0b0011 => breakpoint on addr
6387 * 0b1100 => breakpoint on addr + 2
6388 * 0b1111 => breakpoint on addr
6389 * See also figure D2-3 in the v8 ARM ARM (DDI0487A.c).
6390 */
6391 int bas = extract64(bcr, 5, 4);
6392 addr = sextract64(bvr, 0, 49) & ~3ULL;
6393 if (bas == 0) {
6394 return;
6395 }
6396 if (bas == 0xc) {
6397 addr += 2;
6398 }
6399 break;
6400 }
6401 case 2: /* unlinked context ID match */
6402 case 8: /* unlinked VMID match (reserved if no EL2) */
6403 case 10: /* unlinked context ID and VMID match (reserved if no EL2) */
6404 qemu_log_mask(LOG_UNIMP,
0221c8fd 6405 "arm: unlinked context breakpoint types not implemented\n");
46747d15
PM
6406 return;
6407 case 9: /* linked VMID match (reserved if no EL2) */
6408 case 11: /* linked context ID and VMID match (reserved if no EL2) */
6409 case 3: /* linked context ID match */
6410 default:
6411 /* We must generate no events for Linked context matches (unless
6412 * they are linked to by some other bp/wp, which is handled in
6413 * updates for the linking bp/wp). We choose to also generate no events
6414 * for reserved values.
6415 */
6416 return;
6417 }
6418
6419 cpu_breakpoint_insert(CPU(cpu), addr, flags, &env->cpu_breakpoint[n]);
6420}
6421
6422void hw_breakpoint_update_all(ARMCPU *cpu)
6423{
6424 int i;
6425 CPUARMState *env = &cpu->env;
6426
6427 /* Completely clear out existing QEMU breakpoints and our array, to
6428 * avoid possible stale entries following migration load.
6429 */
6430 cpu_breakpoint_remove_all(CPU(cpu), BP_CPU);
6431 memset(env->cpu_breakpoint, 0, sizeof(env->cpu_breakpoint));
6432
6433 for (i = 0; i < ARRAY_SIZE(cpu->env.cpu_breakpoint); i++) {
6434 hw_breakpoint_update(cpu, i);
6435 }
6436}
6437
6438static void dbgbvr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6439 uint64_t value)
6440{
2fc0cc0e 6441 ARMCPU *cpu = env_archcpu(env);
46747d15
PM
6442 int i = ri->crm;
6443
6444 raw_write(env, ri, value);
6445 hw_breakpoint_update(cpu, i);
6446}
6447
6448static void dbgbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6449 uint64_t value)
6450{
2fc0cc0e 6451 ARMCPU *cpu = env_archcpu(env);
46747d15
PM
6452 int i = ri->crm;
6453
6454 /* BAS[3] is a read-only copy of BAS[2], and BAS[1] a read-only
6455 * copy of BAS[0].
6456 */
6457 value = deposit64(value, 6, 1, extract64(value, 5, 1));
6458 value = deposit64(value, 8, 1, extract64(value, 7, 1));
6459
6460 raw_write(env, ri, value);
6461 hw_breakpoint_update(cpu, i);
6462}
6463
50300698 6464static void define_debug_regs(ARMCPU *cpu)
0b45451e 6465{
50300698
PM
6466 /* Define v7 and v8 architectural debug registers.
6467 * These are just dummy implementations for now.
0b45451e
PM
6468 */
6469 int i;
3ff6fc91 6470 int wrps, brps, ctx_cmps;
48eb3ae6
PM
6471 ARMCPRegInfo dbgdidr = {
6472 .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0,
d6c8cf81 6473 .access = PL0_R, .accessfn = access_tda,
4426d361 6474 .type = ARM_CP_CONST, .resetvalue = cpu->isar.dbgdidr,
48eb3ae6
PM
6475 };
6476
3ff6fc91 6477 /* Note that all these register fields hold "number of Xs minus 1". */
88ce6c6e
PM
6478 brps = arm_num_brps(cpu);
6479 wrps = arm_num_wrps(cpu);
6480 ctx_cmps = arm_num_ctx_cmps(cpu);
3ff6fc91
PM
6481
6482 assert(ctx_cmps <= brps);
48eb3ae6 6483
48eb3ae6 6484 define_one_arm_cp_reg(cpu, &dbgdidr);
50300698
PM
6485 define_arm_cp_regs(cpu, debug_cp_reginfo);
6486
6487 if (arm_feature(&cpu->env, ARM_FEATURE_LPAE)) {
6488 define_arm_cp_regs(cpu, debug_lpae_cp_reginfo);
6489 }
6490
88ce6c6e 6491 for (i = 0; i < brps; i++) {
0b45451e 6492 ARMCPRegInfo dbgregs[] = {
10aae104
PM
6493 { .name = "DBGBVR", .state = ARM_CP_STATE_BOTH,
6494 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 4,
d6c8cf81 6495 .access = PL1_RW, .accessfn = access_tda,
46747d15
PM
6496 .fieldoffset = offsetof(CPUARMState, cp15.dbgbvr[i]),
6497 .writefn = dbgbvr_write, .raw_writefn = raw_write
6498 },
10aae104
PM
6499 { .name = "DBGBCR", .state = ARM_CP_STATE_BOTH,
6500 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 5,
d6c8cf81 6501 .access = PL1_RW, .accessfn = access_tda,
46747d15
PM
6502 .fieldoffset = offsetof(CPUARMState, cp15.dbgbcr[i]),
6503 .writefn = dbgbcr_write, .raw_writefn = raw_write
6504 },
48eb3ae6
PM
6505 REGINFO_SENTINEL
6506 };
6507 define_arm_cp_regs(cpu, dbgregs);
6508 }
6509
88ce6c6e 6510 for (i = 0; i < wrps; i++) {
48eb3ae6 6511 ARMCPRegInfo dbgregs[] = {
10aae104
PM
6512 { .name = "DBGWVR", .state = ARM_CP_STATE_BOTH,
6513 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 6,
d6c8cf81 6514 .access = PL1_RW, .accessfn = access_tda,
9ee98ce8
PM
6515 .fieldoffset = offsetof(CPUARMState, cp15.dbgwvr[i]),
6516 .writefn = dbgwvr_write, .raw_writefn = raw_write
6517 },
10aae104
PM
6518 { .name = "DBGWCR", .state = ARM_CP_STATE_BOTH,
6519 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 7,
d6c8cf81 6520 .access = PL1_RW, .accessfn = access_tda,
9ee98ce8
PM
6521 .fieldoffset = offsetof(CPUARMState, cp15.dbgwcr[i]),
6522 .writefn = dbgwcr_write, .raw_writefn = raw_write
6523 },
6524 REGINFO_SENTINEL
0b45451e
PM
6525 };
6526 define_arm_cp_regs(cpu, dbgregs);
6527 }
6528}
6529
24183fb6
PM
6530static void define_pmu_regs(ARMCPU *cpu)
6531{
6532 /*
6533 * v7 performance monitor control register: same implementor
6534 * field as main ID register, and we implement four counters in
6535 * addition to the cycle count register.
6536 */
6537 unsigned int i, pmcrn = 4;
6538 ARMCPRegInfo pmcr = {
6539 .name = "PMCR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 0,
6540 .access = PL0_RW,
6541 .type = ARM_CP_IO | ARM_CP_ALIAS,
6542 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcr),
6543 .accessfn = pmreg_access, .writefn = pmcr_write,
6544 .raw_writefn = raw_write,
6545 };
6546 ARMCPRegInfo pmcr64 = {
6547 .name = "PMCR_EL0", .state = ARM_CP_STATE_AA64,
6548 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 0,
6549 .access = PL0_RW, .accessfn = pmreg_access,
6550 .type = ARM_CP_IO,
6551 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcr),
62d96ff4
PM
6552 .resetvalue = (cpu->midr & 0xff000000) | (pmcrn << PMCRN_SHIFT) |
6553 PMCRLC,
24183fb6
PM
6554 .writefn = pmcr_write, .raw_writefn = raw_write,
6555 };
6556 define_one_arm_cp_reg(cpu, &pmcr);
6557 define_one_arm_cp_reg(cpu, &pmcr64);
6558 for (i = 0; i < pmcrn; i++) {
6559 char *pmevcntr_name = g_strdup_printf("PMEVCNTR%d", i);
6560 char *pmevcntr_el0_name = g_strdup_printf("PMEVCNTR%d_EL0", i);
6561 char *pmevtyper_name = g_strdup_printf("PMEVTYPER%d", i);
6562 char *pmevtyper_el0_name = g_strdup_printf("PMEVTYPER%d_EL0", i);
6563 ARMCPRegInfo pmev_regs[] = {
6564 { .name = pmevcntr_name, .cp = 15, .crn = 14,
6565 .crm = 8 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7,
6566 .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS,
6567 .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn,
6568 .accessfn = pmreg_access },
6569 { .name = pmevcntr_el0_name, .state = ARM_CP_STATE_AA64,
6570 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 8 | (3 & (i >> 3)),
6571 .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access,
6572 .type = ARM_CP_IO,
6573 .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn,
6574 .raw_readfn = pmevcntr_rawread,
6575 .raw_writefn = pmevcntr_rawwrite },
6576 { .name = pmevtyper_name, .cp = 15, .crn = 14,
6577 .crm = 12 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7,
6578 .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS,
6579 .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn,
6580 .accessfn = pmreg_access },
6581 { .name = pmevtyper_el0_name, .state = ARM_CP_STATE_AA64,
6582 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 12 | (3 & (i >> 3)),
6583 .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access,
6584 .type = ARM_CP_IO,
6585 .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn,
6586 .raw_writefn = pmevtyper_rawwrite },
6587 REGINFO_SENTINEL
6588 };
6589 define_arm_cp_regs(cpu, pmev_regs);
6590 g_free(pmevcntr_name);
6591 g_free(pmevcntr_el0_name);
6592 g_free(pmevtyper_name);
6593 g_free(pmevtyper_el0_name);
6594 }
a6179538 6595 if (cpu_isar_feature(aa32_pmu_8_1, cpu)) {
24183fb6
PM
6596 ARMCPRegInfo v81_pmu_regs[] = {
6597 { .name = "PMCEID2", .state = ARM_CP_STATE_AA32,
6598 .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 4,
6599 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6600 .resetvalue = extract64(cpu->pmceid0, 32, 32) },
6601 { .name = "PMCEID3", .state = ARM_CP_STATE_AA32,
6602 .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 5,
6603 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6604 .resetvalue = extract64(cpu->pmceid1, 32, 32) },
6605 REGINFO_SENTINEL
6606 };
6607 define_arm_cp_regs(cpu, v81_pmu_regs);
6608 }
15dd1ebd
PM
6609 if (cpu_isar_feature(any_pmu_8_4, cpu)) {
6610 static const ARMCPRegInfo v84_pmmir = {
6611 .name = "PMMIR_EL1", .state = ARM_CP_STATE_BOTH,
6612 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 6,
6613 .access = PL1_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6614 .resetvalue = 0
6615 };
6616 define_one_arm_cp_reg(cpu, &v84_pmmir);
6617 }
24183fb6
PM
6618}
6619
96a8b92e
PM
6620/* We don't know until after realize whether there's a GICv3
6621 * attached, and that is what registers the gicv3 sysregs.
6622 * So we have to fill in the GIC fields in ID_PFR/ID_PFR1_EL1/ID_AA64PFR0_EL1
6623 * at runtime.
6624 */
6625static uint64_t id_pfr1_read(CPUARMState *env, const ARMCPRegInfo *ri)
6626{
2fc0cc0e 6627 ARMCPU *cpu = env_archcpu(env);
96a8b92e
PM
6628 uint64_t pfr1 = cpu->id_pfr1;
6629
6630 if (env->gicv3state) {
6631 pfr1 |= 1 << 28;
6632 }
6633 return pfr1;
6634}
6635
976b99b6 6636#ifndef CONFIG_USER_ONLY
96a8b92e
PM
6637static uint64_t id_aa64pfr0_read(CPUARMState *env, const ARMCPRegInfo *ri)
6638{
2fc0cc0e 6639 ARMCPU *cpu = env_archcpu(env);
47576b94 6640 uint64_t pfr0 = cpu->isar.id_aa64pfr0;
96a8b92e
PM
6641
6642 if (env->gicv3state) {
6643 pfr0 |= 1 << 24;
6644 }
6645 return pfr0;
6646}
976b99b6 6647#endif
96a8b92e 6648
2d7137c1
RH
6649/* Shared logic between LORID and the rest of the LOR* registers.
6650 * Secure state has already been delt with.
6651 */
6652static CPAccessResult access_lor_ns(CPUARMState *env)
6653{
6654 int el = arm_current_el(env);
6655
6656 if (el < 2 && (arm_hcr_el2_eff(env) & HCR_TLOR)) {
6657 return CP_ACCESS_TRAP_EL2;
6658 }
6659 if (el < 3 && (env->cp15.scr_el3 & SCR_TLOR)) {
6660 return CP_ACCESS_TRAP_EL3;
6661 }
6662 return CP_ACCESS_OK;
6663}
6664
6665static CPAccessResult access_lorid(CPUARMState *env, const ARMCPRegInfo *ri,
6666 bool isread)
6667{
6668 if (arm_is_secure_below_el3(env)) {
6669 /* Access ok in secure mode. */
6670 return CP_ACCESS_OK;
6671 }
6672 return access_lor_ns(env);
6673}
6674
6675static CPAccessResult access_lor_other(CPUARMState *env,
6676 const ARMCPRegInfo *ri, bool isread)
6677{
6678 if (arm_is_secure_below_el3(env)) {
6679 /* Access denied in secure mode. */
6680 return CP_ACCESS_TRAP;
6681 }
6682 return access_lor_ns(env);
6683}
6684
d8564ee4
RH
6685/*
6686 * A trivial implementation of ARMv8.1-LOR leaves all of these
6687 * registers fixed at 0, which indicates that there are zero
6688 * supported Limited Ordering regions.
6689 */
6690static const ARMCPRegInfo lor_reginfo[] = {
6691 { .name = "LORSA_EL1", .state = ARM_CP_STATE_AA64,
6692 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 0,
6693 .access = PL1_RW, .accessfn = access_lor_other,
6694 .type = ARM_CP_CONST, .resetvalue = 0 },
6695 { .name = "LOREA_EL1", .state = ARM_CP_STATE_AA64,
6696 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 1,
6697 .access = PL1_RW, .accessfn = access_lor_other,
6698 .type = ARM_CP_CONST, .resetvalue = 0 },
6699 { .name = "LORN_EL1", .state = ARM_CP_STATE_AA64,
6700 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 2,
6701 .access = PL1_RW, .accessfn = access_lor_other,
6702 .type = ARM_CP_CONST, .resetvalue = 0 },
6703 { .name = "LORC_EL1", .state = ARM_CP_STATE_AA64,
6704 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 3,
6705 .access = PL1_RW, .accessfn = access_lor_other,
6706 .type = ARM_CP_CONST, .resetvalue = 0 },
6707 { .name = "LORID_EL1", .state = ARM_CP_STATE_AA64,
6708 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 7,
6709 .access = PL1_R, .accessfn = access_lorid,
6710 .type = ARM_CP_CONST, .resetvalue = 0 },
6711 REGINFO_SENTINEL
6712};
6713
967aa94f
RH
6714#ifdef TARGET_AARCH64
6715static CPAccessResult access_pauth(CPUARMState *env, const ARMCPRegInfo *ri,
6716 bool isread)
6717{
6718 int el = arm_current_el(env);
6719
6720 if (el < 2 &&
6721 arm_feature(env, ARM_FEATURE_EL2) &&
6722 !(arm_hcr_el2_eff(env) & HCR_APK)) {
6723 return CP_ACCESS_TRAP_EL2;
6724 }
6725 if (el < 3 &&
6726 arm_feature(env, ARM_FEATURE_EL3) &&
6727 !(env->cp15.scr_el3 & SCR_APK)) {
6728 return CP_ACCESS_TRAP_EL3;
6729 }
6730 return CP_ACCESS_OK;
6731}
6732
6733static const ARMCPRegInfo pauth_reginfo[] = {
6734 { .name = "APDAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6735 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 0,
6736 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 6737 .fieldoffset = offsetof(CPUARMState, keys.apda.lo) },
967aa94f
RH
6738 { .name = "APDAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6739 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 1,
6740 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 6741 .fieldoffset = offsetof(CPUARMState, keys.apda.hi) },
967aa94f
RH
6742 { .name = "APDBKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6743 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 2,
6744 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 6745 .fieldoffset = offsetof(CPUARMState, keys.apdb.lo) },
967aa94f
RH
6746 { .name = "APDBKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6747 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 3,
6748 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 6749 .fieldoffset = offsetof(CPUARMState, keys.apdb.hi) },
967aa94f
RH
6750 { .name = "APGAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6751 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 0,
6752 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 6753 .fieldoffset = offsetof(CPUARMState, keys.apga.lo) },
967aa94f
RH
6754 { .name = "APGAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6755 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 1,
6756 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 6757 .fieldoffset = offsetof(CPUARMState, keys.apga.hi) },
967aa94f
RH
6758 { .name = "APIAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6759 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 0,
6760 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 6761 .fieldoffset = offsetof(CPUARMState, keys.apia.lo) },
967aa94f
RH
6762 { .name = "APIAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6763 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 1,
6764 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 6765 .fieldoffset = offsetof(CPUARMState, keys.apia.hi) },
967aa94f
RH
6766 { .name = "APIBKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6767 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 2,
6768 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 6769 .fieldoffset = offsetof(CPUARMState, keys.apib.lo) },
967aa94f
RH
6770 { .name = "APIBKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6771 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 3,
6772 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 6773 .fieldoffset = offsetof(CPUARMState, keys.apib.hi) },
967aa94f
RH
6774 REGINFO_SENTINEL
6775};
de390645
RH
6776
6777static uint64_t rndr_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
6778{
6779 Error *err = NULL;
6780 uint64_t ret;
6781
6782 /* Success sets NZCV = 0000. */
6783 env->NF = env->CF = env->VF = 0, env->ZF = 1;
6784
6785 if (qemu_guest_getrandom(&ret, sizeof(ret), &err) < 0) {
6786 /*
6787 * ??? Failed, for unknown reasons in the crypto subsystem.
6788 * The best we can do is log the reason and return the
6789 * timed-out indication to the guest. There is no reason
6790 * we know to expect this failure to be transitory, so the
6791 * guest may well hang retrying the operation.
6792 */
6793 qemu_log_mask(LOG_UNIMP, "%s: Crypto failure: %s",
6794 ri->name, error_get_pretty(err));
6795 error_free(err);
6796
6797 env->ZF = 0; /* NZCF = 0100 */
6798 return 0;
6799 }
6800 return ret;
6801}
6802
6803/* We do not support re-seeding, so the two registers operate the same. */
6804static const ARMCPRegInfo rndr_reginfo[] = {
6805 { .name = "RNDR", .state = ARM_CP_STATE_AA64,
6806 .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END | ARM_CP_IO,
6807 .opc0 = 3, .opc1 = 3, .crn = 2, .crm = 4, .opc2 = 0,
6808 .access = PL0_R, .readfn = rndr_readfn },
6809 { .name = "RNDRRS", .state = ARM_CP_STATE_AA64,
6810 .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END | ARM_CP_IO,
6811 .opc0 = 3, .opc1 = 3, .crn = 2, .crm = 4, .opc2 = 1,
6812 .access = PL0_R, .readfn = rndr_readfn },
6813 REGINFO_SENTINEL
6814};
0d57b499
BM
6815
6816#ifndef CONFIG_USER_ONLY
6817static void dccvap_writefn(CPUARMState *env, const ARMCPRegInfo *opaque,
6818 uint64_t value)
6819{
6820 ARMCPU *cpu = env_archcpu(env);
6821 /* CTR_EL0 System register -> DminLine, bits [19:16] */
6822 uint64_t dline_size = 4 << ((cpu->ctr >> 16) & 0xF);
6823 uint64_t vaddr_in = (uint64_t) value;
6824 uint64_t vaddr = vaddr_in & ~(dline_size - 1);
6825 void *haddr;
6826 int mem_idx = cpu_mmu_index(env, false);
6827
6828 /* This won't be crossing page boundaries */
6829 haddr = probe_read(env, vaddr, dline_size, mem_idx, GETPC());
6830 if (haddr) {
6831
6832 ram_addr_t offset;
6833 MemoryRegion *mr;
6834
6835 /* RCU lock is already being held */
6836 mr = memory_region_from_host(haddr, &offset);
6837
6838 if (mr) {
4dfe59d1 6839 memory_region_writeback(mr, offset, dline_size);
0d57b499
BM
6840 }
6841 }
6842}
6843
6844static const ARMCPRegInfo dcpop_reg[] = {
6845 { .name = "DC_CVAP", .state = ARM_CP_STATE_AA64,
6846 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 1,
6847 .access = PL0_W, .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END,
1bed4d2e 6848 .accessfn = aa64_cacheop_poc_access, .writefn = dccvap_writefn },
0d57b499
BM
6849 REGINFO_SENTINEL
6850};
6851
6852static const ARMCPRegInfo dcpodp_reg[] = {
6853 { .name = "DC_CVADP", .state = ARM_CP_STATE_AA64,
6854 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 1,
6855 .access = PL0_W, .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END,
1bed4d2e 6856 .accessfn = aa64_cacheop_poc_access, .writefn = dccvap_writefn },
0d57b499
BM
6857 REGINFO_SENTINEL
6858};
6859#endif /*CONFIG_USER_ONLY*/
6860
4b779ceb
RH
6861static CPAccessResult access_aa64_tid5(CPUARMState *env, const ARMCPRegInfo *ri,
6862 bool isread)
6863{
6864 if ((arm_current_el(env) < 2) && (arm_hcr_el2_eff(env) & HCR_TID5)) {
6865 return CP_ACCESS_TRAP_EL2;
6866 }
6867
6868 return CP_ACCESS_OK;
6869}
6870
6871static CPAccessResult access_mte(CPUARMState *env, const ARMCPRegInfo *ri,
6872 bool isread)
6873{
6874 int el = arm_current_el(env);
6875
6876 if (el < 2 &&
6877 arm_feature(env, ARM_FEATURE_EL2) &&
6878 !(arm_hcr_el2_eff(env) & HCR_ATA)) {
6879 return CP_ACCESS_TRAP_EL2;
6880 }
6881 if (el < 3 &&
6882 arm_feature(env, ARM_FEATURE_EL3) &&
6883 !(env->cp15.scr_el3 & SCR_ATA)) {
6884 return CP_ACCESS_TRAP_EL3;
6885 }
6886 return CP_ACCESS_OK;
6887}
6888
6889static uint64_t tco_read(CPUARMState *env, const ARMCPRegInfo *ri)
6890{
6891 return env->pstate & PSTATE_TCO;
6892}
6893
6894static void tco_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
6895{
6896 env->pstate = (env->pstate & ~PSTATE_TCO) | (val & PSTATE_TCO);
6897}
6898
6899static const ARMCPRegInfo mte_reginfo[] = {
6900 { .name = "TFSRE0_EL1", .state = ARM_CP_STATE_AA64,
6901 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 6, .opc2 = 1,
6902 .access = PL1_RW, .accessfn = access_mte,
6903 .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[0]) },
6904 { .name = "TFSR_EL1", .state = ARM_CP_STATE_AA64,
6905 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 6, .opc2 = 0,
6906 .access = PL1_RW, .accessfn = access_mte,
6907 .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[1]) },
6908 { .name = "TFSR_EL2", .state = ARM_CP_STATE_AA64,
6909 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 6, .opc2 = 0,
6910 .access = PL2_RW, .accessfn = access_mte,
6911 .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[2]) },
6912 { .name = "TFSR_EL3", .state = ARM_CP_STATE_AA64,
6913 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 6, .opc2 = 0,
6914 .access = PL3_RW,
6915 .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[3]) },
6916 { .name = "RGSR_EL1", .state = ARM_CP_STATE_AA64,
6917 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 5,
6918 .access = PL1_RW, .accessfn = access_mte,
6919 .fieldoffset = offsetof(CPUARMState, cp15.rgsr_el1) },
6920 { .name = "GCR_EL1", .state = ARM_CP_STATE_AA64,
6921 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 6,
6922 .access = PL1_RW, .accessfn = access_mte,
6923 .fieldoffset = offsetof(CPUARMState, cp15.gcr_el1) },
6924 { .name = "GMID_EL1", .state = ARM_CP_STATE_AA64,
6925 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 4,
6926 .access = PL1_R, .accessfn = access_aa64_tid5,
6927 .type = ARM_CP_CONST, .resetvalue = GMID_EL1_BS },
6928 { .name = "TCO", .state = ARM_CP_STATE_AA64,
6929 .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 7,
6930 .type = ARM_CP_NO_RAW,
6931 .access = PL0_RW, .readfn = tco_read, .writefn = tco_write },
6932 REGINFO_SENTINEL
6933};
6934
6935static const ARMCPRegInfo mte_tco_ro_reginfo[] = {
6936 { .name = "TCO", .state = ARM_CP_STATE_AA64,
6937 .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 7,
6938 .type = ARM_CP_CONST, .access = PL0_RW, },
6939 REGINFO_SENTINEL
6940};
967aa94f
RH
6941#endif
6942
cb570bd3
RH
6943static CPAccessResult access_predinv(CPUARMState *env, const ARMCPRegInfo *ri,
6944 bool isread)
6945{
6946 int el = arm_current_el(env);
6947
6948 if (el == 0) {
6949 uint64_t sctlr = arm_sctlr(env, el);
6950 if (!(sctlr & SCTLR_EnRCTX)) {
6951 return CP_ACCESS_TRAP;
6952 }
6953 } else if (el == 1) {
6954 uint64_t hcr = arm_hcr_el2_eff(env);
6955 if (hcr & HCR_NV) {
6956 return CP_ACCESS_TRAP_EL2;
6957 }
6958 }
6959 return CP_ACCESS_OK;
6960}
6961
6962static const ARMCPRegInfo predinv_reginfo[] = {
6963 { .name = "CFP_RCTX", .state = ARM_CP_STATE_AA64,
6964 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 4,
6965 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
6966 { .name = "DVP_RCTX", .state = ARM_CP_STATE_AA64,
6967 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 5,
6968 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
6969 { .name = "CPP_RCTX", .state = ARM_CP_STATE_AA64,
6970 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 7,
6971 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
6972 /*
6973 * Note the AArch32 opcodes have a different OPC1.
6974 */
6975 { .name = "CFPRCTX", .state = ARM_CP_STATE_AA32,
6976 .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 4,
6977 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
6978 { .name = "DVPRCTX", .state = ARM_CP_STATE_AA32,
6979 .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 5,
6980 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
6981 { .name = "CPPRCTX", .state = ARM_CP_STATE_AA32,
6982 .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 7,
6983 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
6984 REGINFO_SENTINEL
6985};
6986
957e6155
PM
6987static uint64_t ccsidr2_read(CPUARMState *env, const ARMCPRegInfo *ri)
6988{
6989 /* Read the high 32 bits of the current CCSIDR */
6990 return extract64(ccsidr_read(env, ri), 32, 32);
6991}
6992
6993static const ARMCPRegInfo ccsidr2_reginfo[] = {
6994 { .name = "CCSIDR2", .state = ARM_CP_STATE_BOTH,
6995 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 2,
6996 .access = PL1_R,
6997 .accessfn = access_aa64_tid2,
6998 .readfn = ccsidr2_read, .type = ARM_CP_NO_RAW },
6999 REGINFO_SENTINEL
7000};
7001
6a4ef4e5
MZ
7002static CPAccessResult access_aa64_tid3(CPUARMState *env, const ARMCPRegInfo *ri,
7003 bool isread)
7004{
7005 if ((arm_current_el(env) < 2) && (arm_hcr_el2_eff(env) & HCR_TID3)) {
7006 return CP_ACCESS_TRAP_EL2;
7007 }
7008
7009 return CP_ACCESS_OK;
7010}
7011
7012static CPAccessResult access_aa32_tid3(CPUARMState *env, const ARMCPRegInfo *ri,
7013 bool isread)
7014{
7015 if (arm_feature(env, ARM_FEATURE_V8)) {
7016 return access_aa64_tid3(env, ri, isread);
7017 }
7018
7019 return CP_ACCESS_OK;
7020}
7021
f96f3d5f
MZ
7022static CPAccessResult access_jazelle(CPUARMState *env, const ARMCPRegInfo *ri,
7023 bool isread)
7024{
7025 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID0)) {
7026 return CP_ACCESS_TRAP_EL2;
7027 }
7028
7029 return CP_ACCESS_OK;
7030}
7031
7032static const ARMCPRegInfo jazelle_regs[] = {
7033 { .name = "JIDR",
7034 .cp = 14, .crn = 0, .crm = 0, .opc1 = 7, .opc2 = 0,
7035 .access = PL1_R, .accessfn = access_jazelle,
7036 .type = ARM_CP_CONST, .resetvalue = 0 },
7037 { .name = "JOSCR",
7038 .cp = 14, .crn = 1, .crm = 0, .opc1 = 7, .opc2 = 0,
7039 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
7040 { .name = "JMCR",
7041 .cp = 14, .crn = 2, .crm = 0, .opc1 = 7, .opc2 = 0,
7042 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
7043 REGINFO_SENTINEL
7044};
7045
e2a1a461
RH
7046static const ARMCPRegInfo vhe_reginfo[] = {
7047 { .name = "CONTEXTIDR_EL2", .state = ARM_CP_STATE_AA64,
7048 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 1,
7049 .access = PL2_RW,
7050 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[2]) },
ed30da8e
RH
7051 { .name = "TTBR1_EL2", .state = ARM_CP_STATE_AA64,
7052 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 1,
7053 .access = PL2_RW, .writefn = vmsa_tcr_ttbr_el2_write,
7054 .fieldoffset = offsetof(CPUARMState, cp15.ttbr1_el[2]) },
8c94b071
RH
7055#ifndef CONFIG_USER_ONLY
7056 { .name = "CNTHV_CVAL_EL2", .state = ARM_CP_STATE_AA64,
7057 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 2,
7058 .fieldoffset =
7059 offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYPVIRT].cval),
7060 .type = ARM_CP_IO, .access = PL2_RW,
7061 .writefn = gt_hv_cval_write, .raw_writefn = raw_write },
7062 { .name = "CNTHV_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
7063 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 0,
7064 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
7065 .resetfn = gt_hv_timer_reset,
7066 .readfn = gt_hv_tval_read, .writefn = gt_hv_tval_write },
7067 { .name = "CNTHV_CTL_EL2", .state = ARM_CP_STATE_BOTH,
7068 .type = ARM_CP_IO,
7069 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 1,
7070 .access = PL2_RW,
7071 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYPVIRT].ctl),
7072 .writefn = gt_hv_ctl_write, .raw_writefn = raw_write },
bb5972e4
RH
7073 { .name = "CNTP_CTL_EL02", .state = ARM_CP_STATE_AA64,
7074 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 1,
7075 .type = ARM_CP_IO | ARM_CP_ALIAS,
7076 .access = PL2_RW, .accessfn = e2h_access,
7077 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
7078 .writefn = gt_phys_ctl_write, .raw_writefn = raw_write },
7079 { .name = "CNTV_CTL_EL02", .state = ARM_CP_STATE_AA64,
7080 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 1,
7081 .type = ARM_CP_IO | ARM_CP_ALIAS,
7082 .access = PL2_RW, .accessfn = e2h_access,
7083 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
7084 .writefn = gt_virt_ctl_write, .raw_writefn = raw_write },
7085 { .name = "CNTP_TVAL_EL02", .state = ARM_CP_STATE_AA64,
7086 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 0,
7087 .type = ARM_CP_NO_RAW | ARM_CP_IO | ARM_CP_ALIAS,
7088 .access = PL2_RW, .accessfn = e2h_access,
7089 .readfn = gt_phys_tval_read, .writefn = gt_phys_tval_write },
7090 { .name = "CNTV_TVAL_EL02", .state = ARM_CP_STATE_AA64,
7091 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 0,
7092 .type = ARM_CP_NO_RAW | ARM_CP_IO | ARM_CP_ALIAS,
7093 .access = PL2_RW, .accessfn = e2h_access,
7094 .readfn = gt_virt_tval_read, .writefn = gt_virt_tval_write },
7095 { .name = "CNTP_CVAL_EL02", .state = ARM_CP_STATE_AA64,
7096 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 2,
7097 .type = ARM_CP_IO | ARM_CP_ALIAS,
7098 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
7099 .access = PL2_RW, .accessfn = e2h_access,
7100 .writefn = gt_phys_cval_write, .raw_writefn = raw_write },
7101 { .name = "CNTV_CVAL_EL02", .state = ARM_CP_STATE_AA64,
7102 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 2,
7103 .type = ARM_CP_IO | ARM_CP_ALIAS,
7104 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
7105 .access = PL2_RW, .accessfn = e2h_access,
7106 .writefn = gt_virt_cval_write, .raw_writefn = raw_write },
8c94b071 7107#endif
e2a1a461
RH
7108 REGINFO_SENTINEL
7109};
7110
04b07d29
RH
7111#ifndef CONFIG_USER_ONLY
7112static const ARMCPRegInfo ats1e1_reginfo[] = {
7113 { .name = "AT_S1E1R", .state = ARM_CP_STATE_AA64,
7114 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 0,
7115 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7116 .writefn = ats_write64 },
7117 { .name = "AT_S1E1W", .state = ARM_CP_STATE_AA64,
7118 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 1,
7119 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7120 .writefn = ats_write64 },
7121 REGINFO_SENTINEL
7122};
7123
7124static const ARMCPRegInfo ats1cp_reginfo[] = {
7125 { .name = "ATS1CPRP",
7126 .cp = 15, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 0,
7127 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7128 .writefn = ats_write },
7129 { .name = "ATS1CPWP",
7130 .cp = 15, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 1,
7131 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7132 .writefn = ats_write },
7133 REGINFO_SENTINEL
7134};
7135#endif
7136
f6287c24
PM
7137/*
7138 * ACTLR2 and HACTLR2 map to ACTLR_EL1[63:32] and
7139 * ACTLR_EL2[63:32]. They exist only if the ID_MMFR4.AC2 field
7140 * is non-zero, which is never for ARMv7, optionally in ARMv8
7141 * and mandatorily for ARMv8.2 and up.
7142 * ACTLR2 is banked for S and NS if EL3 is AArch32. Since QEMU's
7143 * implementation is RAZ/WI we can ignore this detail, as we
7144 * do for ACTLR.
7145 */
7146static const ARMCPRegInfo actlr2_hactlr2_reginfo[] = {
7147 { .name = "ACTLR2", .state = ARM_CP_STATE_AA32,
7148 .cp = 15, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 3,
99602377
RH
7149 .access = PL1_RW, .accessfn = access_tacr,
7150 .type = ARM_CP_CONST, .resetvalue = 0 },
f6287c24
PM
7151 { .name = "HACTLR2", .state = ARM_CP_STATE_AA32,
7152 .cp = 15, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 3,
7153 .access = PL2_RW, .type = ARM_CP_CONST,
7154 .resetvalue = 0 },
7155 REGINFO_SENTINEL
7156};
7157
2ceb98c0
PM
7158void register_cp_regs_for_features(ARMCPU *cpu)
7159{
7160 /* Register all the coprocessor registers based on feature bits */
7161 CPUARMState *env = &cpu->env;
7162 if (arm_feature(env, ARM_FEATURE_M)) {
7163 /* M profile has no coprocessor registers */
7164 return;
7165 }
7166
e9aa6c21 7167 define_arm_cp_regs(cpu, cp_reginfo);
9449fdf6
PM
7168 if (!arm_feature(env, ARM_FEATURE_V8)) {
7169 /* Must go early as it is full of wildcards that may be
7170 * overridden by later definitions.
7171 */
7172 define_arm_cp_regs(cpu, not_v8_cp_reginfo);
7173 }
7174
7d57f408 7175 if (arm_feature(env, ARM_FEATURE_V6)) {
8515a092
PM
7176 /* The ID registers all have impdef reset values */
7177 ARMCPRegInfo v6_idregs[] = {
0ff644a7
PM
7178 { .name = "ID_PFR0", .state = ARM_CP_STATE_BOTH,
7179 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
7180 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7181 .accessfn = access_aa32_tid3,
8515a092 7182 .resetvalue = cpu->id_pfr0 },
96a8b92e
PM
7183 /* ID_PFR1 is not a plain ARM_CP_CONST because we don't know
7184 * the value of the GIC field until after we define these regs.
7185 */
0ff644a7
PM
7186 { .name = "ID_PFR1", .state = ARM_CP_STATE_BOTH,
7187 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 1,
96a8b92e 7188 .access = PL1_R, .type = ARM_CP_NO_RAW,
6a4ef4e5 7189 .accessfn = access_aa32_tid3,
96a8b92e
PM
7190 .readfn = id_pfr1_read,
7191 .writefn = arm_cp_write_ignore },
0ff644a7
PM
7192 { .name = "ID_DFR0", .state = ARM_CP_STATE_BOTH,
7193 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 2,
7194 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7195 .accessfn = access_aa32_tid3,
a6179538 7196 .resetvalue = cpu->isar.id_dfr0 },
0ff644a7
PM
7197 { .name = "ID_AFR0", .state = ARM_CP_STATE_BOTH,
7198 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 3,
7199 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7200 .accessfn = access_aa32_tid3,
8515a092 7201 .resetvalue = cpu->id_afr0 },
0ff644a7
PM
7202 { .name = "ID_MMFR0", .state = ARM_CP_STATE_BOTH,
7203 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 4,
7204 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7205 .accessfn = access_aa32_tid3,
10054016 7206 .resetvalue = cpu->isar.id_mmfr0 },
0ff644a7
PM
7207 { .name = "ID_MMFR1", .state = ARM_CP_STATE_BOTH,
7208 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 5,
7209 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7210 .accessfn = access_aa32_tid3,
10054016 7211 .resetvalue = cpu->isar.id_mmfr1 },
0ff644a7
PM
7212 { .name = "ID_MMFR2", .state = ARM_CP_STATE_BOTH,
7213 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 6,
7214 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7215 .accessfn = access_aa32_tid3,
10054016 7216 .resetvalue = cpu->isar.id_mmfr2 },
0ff644a7
PM
7217 { .name = "ID_MMFR3", .state = ARM_CP_STATE_BOTH,
7218 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 7,
7219 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7220 .accessfn = access_aa32_tid3,
10054016 7221 .resetvalue = cpu->isar.id_mmfr3 },
0ff644a7
PM
7222 { .name = "ID_ISAR0", .state = ARM_CP_STATE_BOTH,
7223 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0,
7224 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7225 .accessfn = access_aa32_tid3,
47576b94 7226 .resetvalue = cpu->isar.id_isar0 },
0ff644a7
PM
7227 { .name = "ID_ISAR1", .state = ARM_CP_STATE_BOTH,
7228 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 1,
7229 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7230 .accessfn = access_aa32_tid3,
47576b94 7231 .resetvalue = cpu->isar.id_isar1 },
0ff644a7
PM
7232 { .name = "ID_ISAR2", .state = ARM_CP_STATE_BOTH,
7233 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
7234 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7235 .accessfn = access_aa32_tid3,
47576b94 7236 .resetvalue = cpu->isar.id_isar2 },
0ff644a7
PM
7237 { .name = "ID_ISAR3", .state = ARM_CP_STATE_BOTH,
7238 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 3,
7239 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7240 .accessfn = access_aa32_tid3,
47576b94 7241 .resetvalue = cpu->isar.id_isar3 },
0ff644a7
PM
7242 { .name = "ID_ISAR4", .state = ARM_CP_STATE_BOTH,
7243 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 4,
7244 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7245 .accessfn = access_aa32_tid3,
47576b94 7246 .resetvalue = cpu->isar.id_isar4 },
0ff644a7
PM
7247 { .name = "ID_ISAR5", .state = ARM_CP_STATE_BOTH,
7248 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 5,
7249 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7250 .accessfn = access_aa32_tid3,
47576b94 7251 .resetvalue = cpu->isar.id_isar5 },
e20d84c1
PM
7252 { .name = "ID_MMFR4", .state = ARM_CP_STATE_BOTH,
7253 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 6,
7254 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7255 .accessfn = access_aa32_tid3,
10054016 7256 .resetvalue = cpu->isar.id_mmfr4 },
802abf40 7257 { .name = "ID_ISAR6", .state = ARM_CP_STATE_BOTH,
e20d84c1
PM
7258 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 7,
7259 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7260 .accessfn = access_aa32_tid3,
47576b94 7261 .resetvalue = cpu->isar.id_isar6 },
8515a092
PM
7262 REGINFO_SENTINEL
7263 };
7264 define_arm_cp_regs(cpu, v6_idregs);
7d57f408
PM
7265 define_arm_cp_regs(cpu, v6_cp_reginfo);
7266 } else {
7267 define_arm_cp_regs(cpu, not_v6_cp_reginfo);
7268 }
4d31c596
PM
7269 if (arm_feature(env, ARM_FEATURE_V6K)) {
7270 define_arm_cp_regs(cpu, v6k_cp_reginfo);
7271 }
5e5cf9e3 7272 if (arm_feature(env, ARM_FEATURE_V7MP) &&
452a0955 7273 !arm_feature(env, ARM_FEATURE_PMSA)) {
995939a6
PM
7274 define_arm_cp_regs(cpu, v7mp_cp_reginfo);
7275 }
327dd510
AL
7276 if (arm_feature(env, ARM_FEATURE_V7VE)) {
7277 define_arm_cp_regs(cpu, pmovsset_cp_reginfo);
7278 }
e9aa6c21 7279 if (arm_feature(env, ARM_FEATURE_V7)) {
776d4e5c 7280 ARMCPRegInfo clidr = {
7da845b0
PM
7281 .name = "CLIDR", .state = ARM_CP_STATE_BOTH,
7282 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 1,
630fcd4d
MZ
7283 .access = PL1_R, .type = ARM_CP_CONST,
7284 .accessfn = access_aa64_tid2,
7285 .resetvalue = cpu->clidr
776d4e5c 7286 };
776d4e5c 7287 define_one_arm_cp_reg(cpu, &clidr);
e9aa6c21 7288 define_arm_cp_regs(cpu, v7_cp_reginfo);
50300698 7289 define_debug_regs(cpu);
24183fb6 7290 define_pmu_regs(cpu);
7d57f408
PM
7291 } else {
7292 define_arm_cp_regs(cpu, not_v7_cp_reginfo);
e9aa6c21 7293 }
b0d2b7d0 7294 if (arm_feature(env, ARM_FEATURE_V8)) {
e20d84c1
PM
7295 /* AArch64 ID registers, which all have impdef reset values.
7296 * Note that within the ID register ranges the unused slots
7297 * must all RAZ, not UNDEF; future architecture versions may
7298 * define new registers here.
7299 */
e60cef86 7300 ARMCPRegInfo v8_idregs[] = {
976b99b6
AB
7301 /*
7302 * ID_AA64PFR0_EL1 is not a plain ARM_CP_CONST in system
7303 * emulation because we don't know the right value for the
7304 * GIC field until after we define these regs.
96a8b92e 7305 */
e60cef86
PM
7306 { .name = "ID_AA64PFR0_EL1", .state = ARM_CP_STATE_AA64,
7307 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 0,
976b99b6
AB
7308 .access = PL1_R,
7309#ifdef CONFIG_USER_ONLY
7310 .type = ARM_CP_CONST,
7311 .resetvalue = cpu->isar.id_aa64pfr0
7312#else
7313 .type = ARM_CP_NO_RAW,
6a4ef4e5 7314 .accessfn = access_aa64_tid3,
96a8b92e 7315 .readfn = id_aa64pfr0_read,
976b99b6
AB
7316 .writefn = arm_cp_write_ignore
7317#endif
7318 },
e60cef86
PM
7319 { .name = "ID_AA64PFR1_EL1", .state = ARM_CP_STATE_AA64,
7320 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 1,
7321 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7322 .accessfn = access_aa64_tid3,
47576b94 7323 .resetvalue = cpu->isar.id_aa64pfr1},
e20d84c1
PM
7324 { .name = "ID_AA64PFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7325 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 2,
7326 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7327 .accessfn = access_aa64_tid3,
e20d84c1
PM
7328 .resetvalue = 0 },
7329 { .name = "ID_AA64PFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7330 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 3,
7331 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7332 .accessfn = access_aa64_tid3,
e20d84c1 7333 .resetvalue = 0 },
9516d772 7334 { .name = "ID_AA64ZFR0_EL1", .state = ARM_CP_STATE_AA64,
e20d84c1
PM
7335 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 4,
7336 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7337 .accessfn = access_aa64_tid3,
9516d772 7338 /* At present, only SVEver == 0 is defined anyway. */
e20d84c1
PM
7339 .resetvalue = 0 },
7340 { .name = "ID_AA64PFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7341 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 5,
7342 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7343 .accessfn = access_aa64_tid3,
e20d84c1
PM
7344 .resetvalue = 0 },
7345 { .name = "ID_AA64PFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7346 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 6,
7347 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7348 .accessfn = access_aa64_tid3,
e20d84c1
PM
7349 .resetvalue = 0 },
7350 { .name = "ID_AA64PFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7351 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 7,
7352 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7353 .accessfn = access_aa64_tid3,
e20d84c1 7354 .resetvalue = 0 },
e60cef86
PM
7355 { .name = "ID_AA64DFR0_EL1", .state = ARM_CP_STATE_AA64,
7356 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 0,
7357 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7358 .accessfn = access_aa64_tid3,
2a609df8 7359 .resetvalue = cpu->isar.id_aa64dfr0 },
e60cef86
PM
7360 { .name = "ID_AA64DFR1_EL1", .state = ARM_CP_STATE_AA64,
7361 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 1,
7362 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7363 .accessfn = access_aa64_tid3,
2a609df8 7364 .resetvalue = cpu->isar.id_aa64dfr1 },
e20d84c1
PM
7365 { .name = "ID_AA64DFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7366 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 2,
7367 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7368 .accessfn = access_aa64_tid3,
e20d84c1
PM
7369 .resetvalue = 0 },
7370 { .name = "ID_AA64DFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7371 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 3,
7372 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7373 .accessfn = access_aa64_tid3,
e20d84c1 7374 .resetvalue = 0 },
e60cef86
PM
7375 { .name = "ID_AA64AFR0_EL1", .state = ARM_CP_STATE_AA64,
7376 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 4,
7377 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7378 .accessfn = access_aa64_tid3,
e60cef86
PM
7379 .resetvalue = cpu->id_aa64afr0 },
7380 { .name = "ID_AA64AFR1_EL1", .state = ARM_CP_STATE_AA64,
7381 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 5,
7382 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7383 .accessfn = access_aa64_tid3,
e60cef86 7384 .resetvalue = cpu->id_aa64afr1 },
e20d84c1
PM
7385 { .name = "ID_AA64AFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7386 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 6,
7387 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7388 .accessfn = access_aa64_tid3,
e20d84c1
PM
7389 .resetvalue = 0 },
7390 { .name = "ID_AA64AFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7391 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 7,
7392 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7393 .accessfn = access_aa64_tid3,
e20d84c1 7394 .resetvalue = 0 },
e60cef86
PM
7395 { .name = "ID_AA64ISAR0_EL1", .state = ARM_CP_STATE_AA64,
7396 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 0,
7397 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7398 .accessfn = access_aa64_tid3,
47576b94 7399 .resetvalue = cpu->isar.id_aa64isar0 },
e60cef86
PM
7400 { .name = "ID_AA64ISAR1_EL1", .state = ARM_CP_STATE_AA64,
7401 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 1,
7402 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7403 .accessfn = access_aa64_tid3,
47576b94 7404 .resetvalue = cpu->isar.id_aa64isar1 },
e20d84c1
PM
7405 { .name = "ID_AA64ISAR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7406 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 2,
7407 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7408 .accessfn = access_aa64_tid3,
e20d84c1
PM
7409 .resetvalue = 0 },
7410 { .name = "ID_AA64ISAR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7411 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 3,
7412 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7413 .accessfn = access_aa64_tid3,
e20d84c1
PM
7414 .resetvalue = 0 },
7415 { .name = "ID_AA64ISAR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7416 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 4,
7417 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7418 .accessfn = access_aa64_tid3,
e20d84c1
PM
7419 .resetvalue = 0 },
7420 { .name = "ID_AA64ISAR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7421 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 5,
7422 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7423 .accessfn = access_aa64_tid3,
e20d84c1
PM
7424 .resetvalue = 0 },
7425 { .name = "ID_AA64ISAR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7426 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 6,
7427 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7428 .accessfn = access_aa64_tid3,
e20d84c1
PM
7429 .resetvalue = 0 },
7430 { .name = "ID_AA64ISAR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7431 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 7,
7432 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7433 .accessfn = access_aa64_tid3,
e20d84c1 7434 .resetvalue = 0 },
e60cef86
PM
7435 { .name = "ID_AA64MMFR0_EL1", .state = ARM_CP_STATE_AA64,
7436 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
7437 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7438 .accessfn = access_aa64_tid3,
3dc91ddb 7439 .resetvalue = cpu->isar.id_aa64mmfr0 },
e60cef86
PM
7440 { .name = "ID_AA64MMFR1_EL1", .state = ARM_CP_STATE_AA64,
7441 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 1,
7442 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7443 .accessfn = access_aa64_tid3,
3dc91ddb 7444 .resetvalue = cpu->isar.id_aa64mmfr1 },
64761e10 7445 { .name = "ID_AA64MMFR2_EL1", .state = ARM_CP_STATE_AA64,
e20d84c1
PM
7446 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 2,
7447 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7448 .accessfn = access_aa64_tid3,
64761e10 7449 .resetvalue = cpu->isar.id_aa64mmfr2 },
e20d84c1
PM
7450 { .name = "ID_AA64MMFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7451 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 3,
7452 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7453 .accessfn = access_aa64_tid3,
e20d84c1
PM
7454 .resetvalue = 0 },
7455 { .name = "ID_AA64MMFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7456 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 4,
7457 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7458 .accessfn = access_aa64_tid3,
e20d84c1
PM
7459 .resetvalue = 0 },
7460 { .name = "ID_AA64MMFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7461 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 5,
7462 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7463 .accessfn = access_aa64_tid3,
e20d84c1
PM
7464 .resetvalue = 0 },
7465 { .name = "ID_AA64MMFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7466 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 6,
7467 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7468 .accessfn = access_aa64_tid3,
e20d84c1
PM
7469 .resetvalue = 0 },
7470 { .name = "ID_AA64MMFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7471 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 7,
7472 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7473 .accessfn = access_aa64_tid3,
e20d84c1 7474 .resetvalue = 0 },
a50c0f51
PM
7475 { .name = "MVFR0_EL1", .state = ARM_CP_STATE_AA64,
7476 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0,
7477 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7478 .accessfn = access_aa64_tid3,
47576b94 7479 .resetvalue = cpu->isar.mvfr0 },
a50c0f51
PM
7480 { .name = "MVFR1_EL1", .state = ARM_CP_STATE_AA64,
7481 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1,
7482 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7483 .accessfn = access_aa64_tid3,
47576b94 7484 .resetvalue = cpu->isar.mvfr1 },
a50c0f51
PM
7485 { .name = "MVFR2_EL1", .state = ARM_CP_STATE_AA64,
7486 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2,
7487 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7488 .accessfn = access_aa64_tid3,
47576b94 7489 .resetvalue = cpu->isar.mvfr2 },
e20d84c1
PM
7490 { .name = "MVFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7491 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 3,
7492 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7493 .accessfn = access_aa64_tid3,
e20d84c1
PM
7494 .resetvalue = 0 },
7495 { .name = "MVFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7496 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 4,
7497 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7498 .accessfn = access_aa64_tid3,
e20d84c1
PM
7499 .resetvalue = 0 },
7500 { .name = "MVFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7501 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 5,
7502 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7503 .accessfn = access_aa64_tid3,
e20d84c1
PM
7504 .resetvalue = 0 },
7505 { .name = "MVFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7506 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 6,
7507 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7508 .accessfn = access_aa64_tid3,
e20d84c1
PM
7509 .resetvalue = 0 },
7510 { .name = "MVFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7511 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 7,
7512 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7513 .accessfn = access_aa64_tid3,
e20d84c1 7514 .resetvalue = 0 },
4054bfa9
AF
7515 { .name = "PMCEID0", .state = ARM_CP_STATE_AA32,
7516 .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 6,
7517 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
cad86737 7518 .resetvalue = extract64(cpu->pmceid0, 0, 32) },
4054bfa9
AF
7519 { .name = "PMCEID0_EL0", .state = ARM_CP_STATE_AA64,
7520 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 6,
7521 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
7522 .resetvalue = cpu->pmceid0 },
7523 { .name = "PMCEID1", .state = ARM_CP_STATE_AA32,
7524 .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 7,
7525 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
cad86737 7526 .resetvalue = extract64(cpu->pmceid1, 0, 32) },
4054bfa9
AF
7527 { .name = "PMCEID1_EL0", .state = ARM_CP_STATE_AA64,
7528 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 7,
7529 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
7530 .resetvalue = cpu->pmceid1 },
e60cef86
PM
7531 REGINFO_SENTINEL
7532 };
6c5c0fec
AB
7533#ifdef CONFIG_USER_ONLY
7534 ARMCPRegUserSpaceInfo v8_user_idregs[] = {
7535 { .name = "ID_AA64PFR0_EL1",
7536 .exported_bits = 0x000f000f00ff0000,
7537 .fixed_bits = 0x0000000000000011 },
7538 { .name = "ID_AA64PFR1_EL1",
7539 .exported_bits = 0x00000000000000f0 },
d040242e
AB
7540 { .name = "ID_AA64PFR*_EL1_RESERVED",
7541 .is_glob = true },
6c5c0fec
AB
7542 { .name = "ID_AA64ZFR0_EL1" },
7543 { .name = "ID_AA64MMFR0_EL1",
7544 .fixed_bits = 0x00000000ff000000 },
7545 { .name = "ID_AA64MMFR1_EL1" },
d040242e
AB
7546 { .name = "ID_AA64MMFR*_EL1_RESERVED",
7547 .is_glob = true },
6c5c0fec
AB
7548 { .name = "ID_AA64DFR0_EL1",
7549 .fixed_bits = 0x0000000000000006 },
7550 { .name = "ID_AA64DFR1_EL1" },
d040242e
AB
7551 { .name = "ID_AA64DFR*_EL1_RESERVED",
7552 .is_glob = true },
7553 { .name = "ID_AA64AFR*",
7554 .is_glob = true },
6c5c0fec
AB
7555 { .name = "ID_AA64ISAR0_EL1",
7556 .exported_bits = 0x00fffffff0fffff0 },
7557 { .name = "ID_AA64ISAR1_EL1",
7558 .exported_bits = 0x000000f0ffffffff },
d040242e
AB
7559 { .name = "ID_AA64ISAR*_EL1_RESERVED",
7560 .is_glob = true },
6c5c0fec
AB
7561 REGUSERINFO_SENTINEL
7562 };
7563 modify_arm_cp_regs(v8_idregs, v8_user_idregs);
7564#endif
be8e8128
GB
7565 /* RVBAR_EL1 is only implemented if EL1 is the highest EL */
7566 if (!arm_feature(env, ARM_FEATURE_EL3) &&
7567 !arm_feature(env, ARM_FEATURE_EL2)) {
7568 ARMCPRegInfo rvbar = {
7569 .name = "RVBAR_EL1", .state = ARM_CP_STATE_AA64,
7570 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
7571 .type = ARM_CP_CONST, .access = PL1_R, .resetvalue = cpu->rvbar
7572 };
7573 define_one_arm_cp_reg(cpu, &rvbar);
7574 }
e60cef86 7575 define_arm_cp_regs(cpu, v8_idregs);
b0d2b7d0
PM
7576 define_arm_cp_regs(cpu, v8_cp_reginfo);
7577 }
3b685ba7 7578 if (arm_feature(env, ARM_FEATURE_EL2)) {
f0d574d6 7579 uint64_t vmpidr_def = mpidr_read_val(env);
731de9e6
EI
7580 ARMCPRegInfo vpidr_regs[] = {
7581 { .name = "VPIDR", .state = ARM_CP_STATE_AA32,
7582 .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
7583 .access = PL2_RW, .accessfn = access_el3_aa32ns,
36476562
PM
7584 .resetvalue = cpu->midr, .type = ARM_CP_ALIAS,
7585 .fieldoffset = offsetoflow32(CPUARMState, cp15.vpidr_el2) },
731de9e6
EI
7586 { .name = "VPIDR_EL2", .state = ARM_CP_STATE_AA64,
7587 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
7588 .access = PL2_RW, .resetvalue = cpu->midr,
7589 .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) },
f0d574d6
EI
7590 { .name = "VMPIDR", .state = ARM_CP_STATE_AA32,
7591 .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
7592 .access = PL2_RW, .accessfn = access_el3_aa32ns,
36476562
PM
7593 .resetvalue = vmpidr_def, .type = ARM_CP_ALIAS,
7594 .fieldoffset = offsetoflow32(CPUARMState, cp15.vmpidr_el2) },
f0d574d6
EI
7595 { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_AA64,
7596 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
7597 .access = PL2_RW,
7598 .resetvalue = vmpidr_def,
7599 .fieldoffset = offsetof(CPUARMState, cp15.vmpidr_el2) },
731de9e6
EI
7600 REGINFO_SENTINEL
7601 };
7602 define_arm_cp_regs(cpu, vpidr_regs);
4771cd01 7603 define_arm_cp_regs(cpu, el2_cp_reginfo);
ce4afed8
PM
7604 if (arm_feature(env, ARM_FEATURE_V8)) {
7605 define_arm_cp_regs(cpu, el2_v8_cp_reginfo);
7606 }
be8e8128
GB
7607 /* RVBAR_EL2 is only implemented if EL2 is the highest EL */
7608 if (!arm_feature(env, ARM_FEATURE_EL3)) {
7609 ARMCPRegInfo rvbar = {
7610 .name = "RVBAR_EL2", .state = ARM_CP_STATE_AA64,
7611 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 1,
7612 .type = ARM_CP_CONST, .access = PL2_R, .resetvalue = cpu->rvbar
7613 };
7614 define_one_arm_cp_reg(cpu, &rvbar);
7615 }
d42e3c26
EI
7616 } else {
7617 /* If EL2 is missing but higher ELs are enabled, we need to
7618 * register the no_el2 reginfos.
7619 */
7620 if (arm_feature(env, ARM_FEATURE_EL3)) {
f0d574d6
EI
7621 /* When EL3 exists but not EL2, VPIDR and VMPIDR take the value
7622 * of MIDR_EL1 and MPIDR_EL1.
731de9e6
EI
7623 */
7624 ARMCPRegInfo vpidr_regs[] = {
7625 { .name = "VPIDR_EL2", .state = ARM_CP_STATE_BOTH,
7626 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
93dd1e61 7627 .access = PL2_RW, .accessfn = access_el3_aa32ns,
731de9e6
EI
7628 .type = ARM_CP_CONST, .resetvalue = cpu->midr,
7629 .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) },
f0d574d6
EI
7630 { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_BOTH,
7631 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
93dd1e61 7632 .access = PL2_RW, .accessfn = access_el3_aa32ns,
f0d574d6
EI
7633 .type = ARM_CP_NO_RAW,
7634 .writefn = arm_cp_write_ignore, .readfn = mpidr_read },
731de9e6
EI
7635 REGINFO_SENTINEL
7636 };
7637 define_arm_cp_regs(cpu, vpidr_regs);
4771cd01 7638 define_arm_cp_regs(cpu, el3_no_el2_cp_reginfo);
ce4afed8
PM
7639 if (arm_feature(env, ARM_FEATURE_V8)) {
7640 define_arm_cp_regs(cpu, el3_no_el2_v8_cp_reginfo);
7641 }
d42e3c26 7642 }
3b685ba7 7643 }
81547d66 7644 if (arm_feature(env, ARM_FEATURE_EL3)) {
0f1a3b24 7645 define_arm_cp_regs(cpu, el3_cp_reginfo);
e24fdd23
PM
7646 ARMCPRegInfo el3_regs[] = {
7647 { .name = "RVBAR_EL3", .state = ARM_CP_STATE_AA64,
7648 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 1,
7649 .type = ARM_CP_CONST, .access = PL3_R, .resetvalue = cpu->rvbar },
7650 { .name = "SCTLR_EL3", .state = ARM_CP_STATE_AA64,
7651 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 0,
7652 .access = PL3_RW,
7653 .raw_writefn = raw_write, .writefn = sctlr_write,
7654 .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[3]),
7655 .resetvalue = cpu->reset_sctlr },
7656 REGINFO_SENTINEL
be8e8128 7657 };
e24fdd23
PM
7658
7659 define_arm_cp_regs(cpu, el3_regs);
81547d66 7660 }
2f027fc5
PM
7661 /* The behaviour of NSACR is sufficiently various that we don't
7662 * try to describe it in a single reginfo:
7663 * if EL3 is 64 bit, then trap to EL3 from S EL1,
7664 * reads as constant 0xc00 from NS EL1 and NS EL2
7665 * if EL3 is 32 bit, then RW at EL3, RO at NS EL1 and NS EL2
7666 * if v7 without EL3, register doesn't exist
7667 * if v8 without EL3, reads as constant 0xc00 from NS EL1 and NS EL2
7668 */
7669 if (arm_feature(env, ARM_FEATURE_EL3)) {
7670 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
7671 ARMCPRegInfo nsacr = {
7672 .name = "NSACR", .type = ARM_CP_CONST,
7673 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
7674 .access = PL1_RW, .accessfn = nsacr_access,
7675 .resetvalue = 0xc00
7676 };
7677 define_one_arm_cp_reg(cpu, &nsacr);
7678 } else {
7679 ARMCPRegInfo nsacr = {
7680 .name = "NSACR",
7681 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
7682 .access = PL3_RW | PL1_R,
7683 .resetvalue = 0,
7684 .fieldoffset = offsetof(CPUARMState, cp15.nsacr)
7685 };
7686 define_one_arm_cp_reg(cpu, &nsacr);
7687 }
7688 } else {
7689 if (arm_feature(env, ARM_FEATURE_V8)) {
7690 ARMCPRegInfo nsacr = {
7691 .name = "NSACR", .type = ARM_CP_CONST,
7692 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
7693 .access = PL1_R,
7694 .resetvalue = 0xc00
7695 };
7696 define_one_arm_cp_reg(cpu, &nsacr);
7697 }
7698 }
7699
452a0955 7700 if (arm_feature(env, ARM_FEATURE_PMSA)) {
6cb0b013
PC
7701 if (arm_feature(env, ARM_FEATURE_V6)) {
7702 /* PMSAv6 not implemented */
7703 assert(arm_feature(env, ARM_FEATURE_V7));
7704 define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
7705 define_arm_cp_regs(cpu, pmsav7_cp_reginfo);
7706 } else {
7707 define_arm_cp_regs(cpu, pmsav5_cp_reginfo);
7708 }
18032bec 7709 } else {
8e5d75c9 7710 define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
18032bec 7711 define_arm_cp_regs(cpu, vmsa_cp_reginfo);
4036b7d1
PM
7712 /* TTCBR2 is introduced with ARMv8.2-AA32HPD. */
7713 if (cpu_isar_feature(aa32_hpd, cpu)) {
ab638a32
RH
7714 define_one_arm_cp_reg(cpu, &ttbcr2_reginfo);
7715 }
18032bec 7716 }
c326b979
PM
7717 if (arm_feature(env, ARM_FEATURE_THUMB2EE)) {
7718 define_arm_cp_regs(cpu, t2ee_cp_reginfo);
7719 }
6cc7a3ae
PM
7720 if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) {
7721 define_arm_cp_regs(cpu, generic_timer_cp_reginfo);
7722 }
4a501606
PM
7723 if (arm_feature(env, ARM_FEATURE_VAPA)) {
7724 define_arm_cp_regs(cpu, vapa_cp_reginfo);
7725 }
c4804214
PM
7726 if (arm_feature(env, ARM_FEATURE_CACHE_TEST_CLEAN)) {
7727 define_arm_cp_regs(cpu, cache_test_clean_cp_reginfo);
7728 }
7729 if (arm_feature(env, ARM_FEATURE_CACHE_DIRTY_REG)) {
7730 define_arm_cp_regs(cpu, cache_dirty_status_cp_reginfo);
7731 }
7732 if (arm_feature(env, ARM_FEATURE_CACHE_BLOCK_OPS)) {
7733 define_arm_cp_regs(cpu, cache_block_ops_cp_reginfo);
7734 }
18032bec
PM
7735 if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
7736 define_arm_cp_regs(cpu, omap_cp_reginfo);
7737 }
34f90529
PM
7738 if (arm_feature(env, ARM_FEATURE_STRONGARM)) {
7739 define_arm_cp_regs(cpu, strongarm_cp_reginfo);
7740 }
1047b9d7
PM
7741 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
7742 define_arm_cp_regs(cpu, xscale_cp_reginfo);
7743 }
7744 if (arm_feature(env, ARM_FEATURE_DUMMY_C15_REGS)) {
7745 define_arm_cp_regs(cpu, dummy_c15_cp_reginfo);
7746 }
7ac681cf
PM
7747 if (arm_feature(env, ARM_FEATURE_LPAE)) {
7748 define_arm_cp_regs(cpu, lpae_cp_reginfo);
7749 }
873b73c0 7750 if (cpu_isar_feature(aa32_jazelle, cpu)) {
f96f3d5f
MZ
7751 define_arm_cp_regs(cpu, jazelle_regs);
7752 }
7884849c
PM
7753 /* Slightly awkwardly, the OMAP and StrongARM cores need all of
7754 * cp15 crn=0 to be writes-ignored, whereas for other cores they should
7755 * be read-only (ie write causes UNDEF exception).
7756 */
7757 {
00a29f3d
PM
7758 ARMCPRegInfo id_pre_v8_midr_cp_reginfo[] = {
7759 /* Pre-v8 MIDR space.
7760 * Note that the MIDR isn't a simple constant register because
7884849c
PM
7761 * of the TI925 behaviour where writes to another register can
7762 * cause the MIDR value to change.
97ce8d61
PC
7763 *
7764 * Unimplemented registers in the c15 0 0 0 space default to
7765 * MIDR. Define MIDR first as this entire space, then CTR, TCMTR
7766 * and friends override accordingly.
7884849c
PM
7767 */
7768 { .name = "MIDR",
97ce8d61 7769 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = CP_ANY,
7884849c 7770 .access = PL1_R, .resetvalue = cpu->midr,
d4e6df63 7771 .writefn = arm_cp_write_ignore, .raw_writefn = raw_write,
731de9e6 7772 .readfn = midr_read,
97ce8d61
PC
7773 .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
7774 .type = ARM_CP_OVERRIDE },
7884849c
PM
7775 /* crn = 0 op1 = 0 crm = 3..7 : currently unassigned; we RAZ. */
7776 { .name = "DUMMY",
7777 .cp = 15, .crn = 0, .crm = 3, .opc1 = 0, .opc2 = CP_ANY,
7778 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
7779 { .name = "DUMMY",
7780 .cp = 15, .crn = 0, .crm = 4, .opc1 = 0, .opc2 = CP_ANY,
7781 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
7782 { .name = "DUMMY",
7783 .cp = 15, .crn = 0, .crm = 5, .opc1 = 0, .opc2 = CP_ANY,
7784 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
7785 { .name = "DUMMY",
7786 .cp = 15, .crn = 0, .crm = 6, .opc1 = 0, .opc2 = CP_ANY,
7787 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
7788 { .name = "DUMMY",
7789 .cp = 15, .crn = 0, .crm = 7, .opc1 = 0, .opc2 = CP_ANY,
7790 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
7791 REGINFO_SENTINEL
7792 };
00a29f3d 7793 ARMCPRegInfo id_v8_midr_cp_reginfo[] = {
00a29f3d
PM
7794 { .name = "MIDR_EL1", .state = ARM_CP_STATE_BOTH,
7795 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 0,
731de9e6
EI
7796 .access = PL1_R, .type = ARM_CP_NO_RAW, .resetvalue = cpu->midr,
7797 .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
7798 .readfn = midr_read },
ac00c79f
SF
7799 /* crn = 0 op1 = 0 crm = 0 op2 = 4,7 : AArch32 aliases of MIDR */
7800 { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
7801 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
7802 .access = PL1_R, .resetvalue = cpu->midr },
7803 { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
7804 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 7,
7805 .access = PL1_R, .resetvalue = cpu->midr },
00a29f3d
PM
7806 { .name = "REVIDR_EL1", .state = ARM_CP_STATE_BOTH,
7807 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 6,
93fbc983
MZ
7808 .access = PL1_R,
7809 .accessfn = access_aa64_tid1,
7810 .type = ARM_CP_CONST, .resetvalue = cpu->revidr },
00a29f3d
PM
7811 REGINFO_SENTINEL
7812 };
7813 ARMCPRegInfo id_cp_reginfo[] = {
7814 /* These are common to v8 and pre-v8 */
7815 { .name = "CTR",
7816 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 1,
630fcd4d
MZ
7817 .access = PL1_R, .accessfn = ctr_el0_access,
7818 .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
00a29f3d
PM
7819 { .name = "CTR_EL0", .state = ARM_CP_STATE_AA64,
7820 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 0, .crm = 0,
7821 .access = PL0_R, .accessfn = ctr_el0_access,
7822 .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
7823 /* TCMTR and TLBTR exist in v8 but have no 64-bit versions */
7824 { .name = "TCMTR",
7825 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 2,
93fbc983
MZ
7826 .access = PL1_R,
7827 .accessfn = access_aa32_tid1,
7828 .type = ARM_CP_CONST, .resetvalue = 0 },
00a29f3d
PM
7829 REGINFO_SENTINEL
7830 };
8085ce63
PC
7831 /* TLBTR is specific to VMSA */
7832 ARMCPRegInfo id_tlbtr_reginfo = {
7833 .name = "TLBTR",
7834 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 3,
93fbc983
MZ
7835 .access = PL1_R,
7836 .accessfn = access_aa32_tid1,
7837 .type = ARM_CP_CONST, .resetvalue = 0,
8085ce63 7838 };
3281af81
PC
7839 /* MPUIR is specific to PMSA V6+ */
7840 ARMCPRegInfo id_mpuir_reginfo = {
7841 .name = "MPUIR",
7842 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
7843 .access = PL1_R, .type = ARM_CP_CONST,
7844 .resetvalue = cpu->pmsav7_dregion << 8
7845 };
7884849c
PM
7846 ARMCPRegInfo crn0_wi_reginfo = {
7847 .name = "CRN0_WI", .cp = 15, .crn = 0, .crm = CP_ANY,
7848 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_W,
7849 .type = ARM_CP_NOP | ARM_CP_OVERRIDE
7850 };
6c5c0fec
AB
7851#ifdef CONFIG_USER_ONLY
7852 ARMCPRegUserSpaceInfo id_v8_user_midr_cp_reginfo[] = {
7853 { .name = "MIDR_EL1",
7854 .exported_bits = 0x00000000ffffffff },
7855 { .name = "REVIDR_EL1" },
7856 REGUSERINFO_SENTINEL
7857 };
7858 modify_arm_cp_regs(id_v8_midr_cp_reginfo, id_v8_user_midr_cp_reginfo);
7859#endif
7884849c
PM
7860 if (arm_feature(env, ARM_FEATURE_OMAPCP) ||
7861 arm_feature(env, ARM_FEATURE_STRONGARM)) {
7862 ARMCPRegInfo *r;
7863 /* Register the blanket "writes ignored" value first to cover the
a703eda1
PC
7864 * whole space. Then update the specific ID registers to allow write
7865 * access, so that they ignore writes rather than causing them to
7866 * UNDEF.
7884849c
PM
7867 */
7868 define_one_arm_cp_reg(cpu, &crn0_wi_reginfo);
00a29f3d
PM
7869 for (r = id_pre_v8_midr_cp_reginfo;
7870 r->type != ARM_CP_SENTINEL; r++) {
7871 r->access = PL1_RW;
7872 }
7884849c
PM
7873 for (r = id_cp_reginfo; r->type != ARM_CP_SENTINEL; r++) {
7874 r->access = PL1_RW;
7884849c 7875 }
10006112 7876 id_mpuir_reginfo.access = PL1_RW;
3281af81 7877 id_tlbtr_reginfo.access = PL1_RW;
7884849c 7878 }
00a29f3d
PM
7879 if (arm_feature(env, ARM_FEATURE_V8)) {
7880 define_arm_cp_regs(cpu, id_v8_midr_cp_reginfo);
7881 } else {
7882 define_arm_cp_regs(cpu, id_pre_v8_midr_cp_reginfo);
7883 }
a703eda1 7884 define_arm_cp_regs(cpu, id_cp_reginfo);
452a0955 7885 if (!arm_feature(env, ARM_FEATURE_PMSA)) {
8085ce63 7886 define_one_arm_cp_reg(cpu, &id_tlbtr_reginfo);
3281af81
PC
7887 } else if (arm_feature(env, ARM_FEATURE_V7)) {
7888 define_one_arm_cp_reg(cpu, &id_mpuir_reginfo);
8085ce63 7889 }
7884849c
PM
7890 }
7891
97ce8d61 7892 if (arm_feature(env, ARM_FEATURE_MPIDR)) {
52264166
AB
7893 ARMCPRegInfo mpidr_cp_reginfo[] = {
7894 { .name = "MPIDR_EL1", .state = ARM_CP_STATE_BOTH,
7895 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 5,
7896 .access = PL1_R, .readfn = mpidr_read, .type = ARM_CP_NO_RAW },
7897 REGINFO_SENTINEL
7898 };
7899#ifdef CONFIG_USER_ONLY
7900 ARMCPRegUserSpaceInfo mpidr_user_cp_reginfo[] = {
7901 { .name = "MPIDR_EL1",
7902 .fixed_bits = 0x0000000080000000 },
7903 REGUSERINFO_SENTINEL
7904 };
7905 modify_arm_cp_regs(mpidr_cp_reginfo, mpidr_user_cp_reginfo);
7906#endif
97ce8d61
PC
7907 define_arm_cp_regs(cpu, mpidr_cp_reginfo);
7908 }
7909
2771db27 7910 if (arm_feature(env, ARM_FEATURE_AUXCR)) {
834a6c69
PM
7911 ARMCPRegInfo auxcr_reginfo[] = {
7912 { .name = "ACTLR_EL1", .state = ARM_CP_STATE_BOTH,
7913 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 1,
99602377
RH
7914 .access = PL1_RW, .accessfn = access_tacr,
7915 .type = ARM_CP_CONST, .resetvalue = cpu->reset_auxcr },
834a6c69
PM
7916 { .name = "ACTLR_EL2", .state = ARM_CP_STATE_BOTH,
7917 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 1,
7918 .access = PL2_RW, .type = ARM_CP_CONST,
7919 .resetvalue = 0 },
7920 { .name = "ACTLR_EL3", .state = ARM_CP_STATE_AA64,
7921 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 1,
7922 .access = PL3_RW, .type = ARM_CP_CONST,
7923 .resetvalue = 0 },
7924 REGINFO_SENTINEL
2771db27 7925 };
834a6c69 7926 define_arm_cp_regs(cpu, auxcr_reginfo);
f6287c24
PM
7927 if (cpu_isar_feature(aa32_ac2, cpu)) {
7928 define_arm_cp_regs(cpu, actlr2_hactlr2_reginfo);
0e0456ab 7929 }
2771db27
PM
7930 }
7931
d8ba780b 7932 if (arm_feature(env, ARM_FEATURE_CBAR)) {
d56974af
LM
7933 /*
7934 * CBAR is IMPDEF, but common on Arm Cortex-A implementations.
7935 * There are two flavours:
7936 * (1) older 32-bit only cores have a simple 32-bit CBAR
7937 * (2) 64-bit cores have a 64-bit CBAR visible to AArch64, plus a
7938 * 32-bit register visible to AArch32 at a different encoding
7939 * to the "flavour 1" register and with the bits rearranged to
7940 * be able to squash a 64-bit address into the 32-bit view.
7941 * We distinguish the two via the ARM_FEATURE_AARCH64 flag, but
7942 * in future if we support AArch32-only configs of some of the
7943 * AArch64 cores we might need to add a specific feature flag
7944 * to indicate cores with "flavour 2" CBAR.
7945 */
f318cec6
PM
7946 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
7947 /* 32 bit view is [31:18] 0...0 [43:32]. */
7948 uint32_t cbar32 = (extract64(cpu->reset_cbar, 18, 14) << 18)
7949 | extract64(cpu->reset_cbar, 32, 12);
7950 ARMCPRegInfo cbar_reginfo[] = {
7951 { .name = "CBAR",
7952 .type = ARM_CP_CONST,
d56974af
LM
7953 .cp = 15, .crn = 15, .crm = 3, .opc1 = 1, .opc2 = 0,
7954 .access = PL1_R, .resetvalue = cbar32 },
f318cec6
PM
7955 { .name = "CBAR_EL1", .state = ARM_CP_STATE_AA64,
7956 .type = ARM_CP_CONST,
7957 .opc0 = 3, .opc1 = 1, .crn = 15, .crm = 3, .opc2 = 0,
d56974af 7958 .access = PL1_R, .resetvalue = cpu->reset_cbar },
f318cec6
PM
7959 REGINFO_SENTINEL
7960 };
7961 /* We don't implement a r/w 64 bit CBAR currently */
7962 assert(arm_feature(env, ARM_FEATURE_CBAR_RO));
7963 define_arm_cp_regs(cpu, cbar_reginfo);
7964 } else {
7965 ARMCPRegInfo cbar = {
7966 .name = "CBAR",
7967 .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0,
7968 .access = PL1_R|PL3_W, .resetvalue = cpu->reset_cbar,
7969 .fieldoffset = offsetof(CPUARMState,
7970 cp15.c15_config_base_address)
7971 };
7972 if (arm_feature(env, ARM_FEATURE_CBAR_RO)) {
7973 cbar.access = PL1_R;
7974 cbar.fieldoffset = 0;
7975 cbar.type = ARM_CP_CONST;
7976 }
7977 define_one_arm_cp_reg(cpu, &cbar);
7978 }
d8ba780b
PC
7979 }
7980
91db4642
CLG
7981 if (arm_feature(env, ARM_FEATURE_VBAR)) {
7982 ARMCPRegInfo vbar_cp_reginfo[] = {
7983 { .name = "VBAR", .state = ARM_CP_STATE_BOTH,
7984 .opc0 = 3, .crn = 12, .crm = 0, .opc1 = 0, .opc2 = 0,
7985 .access = PL1_RW, .writefn = vbar_write,
7986 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.vbar_s),
7987 offsetof(CPUARMState, cp15.vbar_ns) },
7988 .resetvalue = 0 },
7989 REGINFO_SENTINEL
7990 };
7991 define_arm_cp_regs(cpu, vbar_cp_reginfo);
7992 }
7993
2771db27
PM
7994 /* Generic registers whose values depend on the implementation */
7995 {
7996 ARMCPRegInfo sctlr = {
5ebafdf3 7997 .name = "SCTLR", .state = ARM_CP_STATE_BOTH,
137feaa9 7998 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
84929218 7999 .access = PL1_RW, .accessfn = access_tvm_trvm,
137feaa9
FA
8000 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.sctlr_s),
8001 offsetof(CPUARMState, cp15.sctlr_ns) },
d4e6df63
PM
8002 .writefn = sctlr_write, .resetvalue = cpu->reset_sctlr,
8003 .raw_writefn = raw_write,
2771db27
PM
8004 };
8005 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
8006 /* Normally we would always end the TB on an SCTLR write, but Linux
8007 * arch/arm/mach-pxa/sleep.S expects two instructions following
8008 * an MMU enable to execute from cache. Imitate this behaviour.
8009 */
8010 sctlr.type |= ARM_CP_SUPPRESS_TB_END;
8011 }
8012 define_one_arm_cp_reg(cpu, &sctlr);
8013 }
5be5e8ed 8014
2d7137c1 8015 if (cpu_isar_feature(aa64_lor, cpu)) {
2d7137c1
RH
8016 define_arm_cp_regs(cpu, lor_reginfo);
8017 }
220f508f
RH
8018 if (cpu_isar_feature(aa64_pan, cpu)) {
8019 define_one_arm_cp_reg(cpu, &pan_reginfo);
8020 }
04b07d29
RH
8021#ifndef CONFIG_USER_ONLY
8022 if (cpu_isar_feature(aa64_ats1e1, cpu)) {
8023 define_arm_cp_regs(cpu, ats1e1_reginfo);
8024 }
8025 if (cpu_isar_feature(aa32_ats1e1, cpu)) {
8026 define_arm_cp_regs(cpu, ats1cp_reginfo);
8027 }
8028#endif
9eeb7a1c
RH
8029 if (cpu_isar_feature(aa64_uao, cpu)) {
8030 define_one_arm_cp_reg(cpu, &uao_reginfo);
8031 }
2d7137c1 8032
e2a1a461
RH
8033 if (arm_feature(env, ARM_FEATURE_EL2) && cpu_isar_feature(aa64_vh, cpu)) {
8034 define_arm_cp_regs(cpu, vhe_reginfo);
8035 }
8036
cd208a1c 8037 if (cpu_isar_feature(aa64_sve, cpu)) {
5be5e8ed
RH
8038 define_one_arm_cp_reg(cpu, &zcr_el1_reginfo);
8039 if (arm_feature(env, ARM_FEATURE_EL2)) {
8040 define_one_arm_cp_reg(cpu, &zcr_el2_reginfo);
8041 } else {
8042 define_one_arm_cp_reg(cpu, &zcr_no_el2_reginfo);
8043 }
8044 if (arm_feature(env, ARM_FEATURE_EL3)) {
8045 define_one_arm_cp_reg(cpu, &zcr_el3_reginfo);
8046 }
8047 }
967aa94f
RH
8048
8049#ifdef TARGET_AARCH64
8050 if (cpu_isar_feature(aa64_pauth, cpu)) {
8051 define_arm_cp_regs(cpu, pauth_reginfo);
8052 }
de390645
RH
8053 if (cpu_isar_feature(aa64_rndr, cpu)) {
8054 define_arm_cp_regs(cpu, rndr_reginfo);
8055 }
0d57b499
BM
8056#ifndef CONFIG_USER_ONLY
8057 /* Data Cache clean instructions up to PoP */
8058 if (cpu_isar_feature(aa64_dcpop, cpu)) {
8059 define_one_arm_cp_reg(cpu, dcpop_reg);
8060
8061 if (cpu_isar_feature(aa64_dcpodp, cpu)) {
8062 define_one_arm_cp_reg(cpu, dcpodp_reg);
8063 }
8064 }
8065#endif /*CONFIG_USER_ONLY*/
4b779ceb
RH
8066
8067 /*
8068 * If full MTE is enabled, add all of the system registers.
8069 * If only "instructions available at EL0" are enabled,
8070 * then define only a RAZ/WI version of PSTATE.TCO.
8071 */
8072 if (cpu_isar_feature(aa64_mte, cpu)) {
8073 define_arm_cp_regs(cpu, mte_reginfo);
8074 } else if (cpu_isar_feature(aa64_mte_insn_reg, cpu)) {
8075 define_arm_cp_regs(cpu, mte_tco_ro_reginfo);
8076 }
967aa94f 8077#endif
cb570bd3 8078
22e57073 8079 if (cpu_isar_feature(any_predinv, cpu)) {
cb570bd3
RH
8080 define_arm_cp_regs(cpu, predinv_reginfo);
8081 }
e2cce18f 8082
957e6155
PM
8083 if (cpu_isar_feature(any_ccidx, cpu)) {
8084 define_arm_cp_regs(cpu, ccsidr2_reginfo);
8085 }
8086
e2cce18f
RH
8087#ifndef CONFIG_USER_ONLY
8088 /*
8089 * Register redirections and aliases must be done last,
8090 * after the registers from the other extensions have been defined.
8091 */
8092 if (arm_feature(env, ARM_FEATURE_EL2) && cpu_isar_feature(aa64_vh, cpu)) {
8093 define_arm_vh_e2h_redirects_aliases(cpu);
8094 }
8095#endif
2ceb98c0
PM
8096}
8097
14969266
AF
8098void arm_cpu_register_gdb_regs_for_features(ARMCPU *cpu)
8099{
22169d41 8100 CPUState *cs = CPU(cpu);
14969266
AF
8101 CPUARMState *env = &cpu->env;
8102
6a669427 8103 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
d12379c5
AB
8104 /*
8105 * The lower part of each SVE register aliases to the FPU
8106 * registers so we don't need to include both.
8107 */
8108#ifdef TARGET_AARCH64
8109 if (isar_feature_aa64_sve(&cpu->isar)) {
8110 gdb_register_coprocessor(cs, arm_gdb_get_svereg, arm_gdb_set_svereg,
8111 arm_gen_dynamic_svereg_xml(cs, cs->gdb_num_regs),
8112 "sve-registers.xml", 0);
8113 } else
8114#endif
8115 {
8116 gdb_register_coprocessor(cs, aarch64_fpu_gdb_get_reg,
8117 aarch64_fpu_gdb_set_reg,
8118 34, "aarch64-fpu.xml", 0);
8119 }
6a669427 8120 } else if (arm_feature(env, ARM_FEATURE_NEON)) {
22169d41 8121 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
56aebc89 8122 51, "arm-neon.xml", 0);
a6627f5f 8123 } else if (cpu_isar_feature(aa32_simd_r32, cpu)) {
22169d41 8124 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
56aebc89 8125 35, "arm-vfp3.xml", 0);
7fbc6a40 8126 } else if (cpu_isar_feature(aa32_vfp_simd, cpu)) {
22169d41 8127 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
56aebc89
PB
8128 19, "arm-vfp.xml", 0);
8129 }
200bf5b7 8130 gdb_register_coprocessor(cs, arm_gdb_get_sysreg, arm_gdb_set_sysreg,
32d6e32a 8131 arm_gen_dynamic_sysreg_xml(cs, cs->gdb_num_regs),
200bf5b7 8132 "system-registers.xml", 0);
d12379c5 8133
40f137e1
PB
8134}
8135
777dc784
PM
8136/* Sort alphabetically by type name, except for "any". */
8137static gint arm_cpu_list_compare(gconstpointer a, gconstpointer b)
5adb4839 8138{
777dc784
PM
8139 ObjectClass *class_a = (ObjectClass *)a;
8140 ObjectClass *class_b = (ObjectClass *)b;
8141 const char *name_a, *name_b;
5adb4839 8142
777dc784
PM
8143 name_a = object_class_get_name(class_a);
8144 name_b = object_class_get_name(class_b);
51492fd1 8145 if (strcmp(name_a, "any-" TYPE_ARM_CPU) == 0) {
777dc784 8146 return 1;
51492fd1 8147 } else if (strcmp(name_b, "any-" TYPE_ARM_CPU) == 0) {
777dc784
PM
8148 return -1;
8149 } else {
8150 return strcmp(name_a, name_b);
5adb4839
PB
8151 }
8152}
8153
777dc784 8154static void arm_cpu_list_entry(gpointer data, gpointer user_data)
40f137e1 8155{
777dc784 8156 ObjectClass *oc = data;
51492fd1
AF
8157 const char *typename;
8158 char *name;
3371d272 8159
51492fd1
AF
8160 typename = object_class_get_name(oc);
8161 name = g_strndup(typename, strlen(typename) - strlen("-" TYPE_ARM_CPU));
0442428a 8162 qemu_printf(" %s\n", name);
51492fd1 8163 g_free(name);
777dc784
PM
8164}
8165
0442428a 8166void arm_cpu_list(void)
777dc784 8167{
777dc784
PM
8168 GSList *list;
8169
8170 list = object_class_get_list(TYPE_ARM_CPU, false);
8171 list = g_slist_sort(list, arm_cpu_list_compare);
0442428a
MA
8172 qemu_printf("Available CPUs:\n");
8173 g_slist_foreach(list, arm_cpu_list_entry, NULL);
777dc784 8174 g_slist_free(list);
40f137e1
PB
8175}
8176
78027bb6
CR
8177static void arm_cpu_add_definition(gpointer data, gpointer user_data)
8178{
8179 ObjectClass *oc = data;
8180 CpuDefinitionInfoList **cpu_list = user_data;
8181 CpuDefinitionInfoList *entry;
8182 CpuDefinitionInfo *info;
8183 const char *typename;
8184
8185 typename = object_class_get_name(oc);
8186 info = g_malloc0(sizeof(*info));
8187 info->name = g_strndup(typename,
8188 strlen(typename) - strlen("-" TYPE_ARM_CPU));
8ed877b7 8189 info->q_typename = g_strdup(typename);
78027bb6
CR
8190
8191 entry = g_malloc0(sizeof(*entry));
8192 entry->value = info;
8193 entry->next = *cpu_list;
8194 *cpu_list = entry;
8195}
8196
25a9d6ca 8197CpuDefinitionInfoList *qmp_query_cpu_definitions(Error **errp)
78027bb6
CR
8198{
8199 CpuDefinitionInfoList *cpu_list = NULL;
8200 GSList *list;
8201
8202 list = object_class_get_list(TYPE_ARM_CPU, false);
8203 g_slist_foreach(list, arm_cpu_add_definition, &cpu_list);
8204 g_slist_free(list);
8205
8206 return cpu_list;
8207}
8208
6e6efd61 8209static void add_cpreg_to_hashtable(ARMCPU *cpu, const ARMCPRegInfo *r,
51a79b03 8210 void *opaque, int state, int secstate,
9c513e78
AB
8211 int crm, int opc1, int opc2,
8212 const char *name)
6e6efd61
PM
8213{
8214 /* Private utility function for define_one_arm_cp_reg_with_opaque():
8215 * add a single reginfo struct to the hash table.
8216 */
8217 uint32_t *key = g_new(uint32_t, 1);
8218 ARMCPRegInfo *r2 = g_memdup(r, sizeof(ARMCPRegInfo));
8219 int is64 = (r->type & ARM_CP_64BIT) ? 1 : 0;
3f3c82a5
FA
8220 int ns = (secstate & ARM_CP_SECSTATE_NS) ? 1 : 0;
8221
9c513e78 8222 r2->name = g_strdup(name);
3f3c82a5
FA
8223 /* Reset the secure state to the specific incoming state. This is
8224 * necessary as the register may have been defined with both states.
8225 */
8226 r2->secure = secstate;
8227
8228 if (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1]) {
8229 /* Register is banked (using both entries in array).
8230 * Overwriting fieldoffset as the array is only used to define
8231 * banked registers but later only fieldoffset is used.
f5a0a5a5 8232 */
3f3c82a5
FA
8233 r2->fieldoffset = r->bank_fieldoffsets[ns];
8234 }
8235
8236 if (state == ARM_CP_STATE_AA32) {
8237 if (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1]) {
8238 /* If the register is banked then we don't need to migrate or
8239 * reset the 32-bit instance in certain cases:
8240 *
8241 * 1) If the register has both 32-bit and 64-bit instances then we
8242 * can count on the 64-bit instance taking care of the
8243 * non-secure bank.
8244 * 2) If ARMv8 is enabled then we can count on a 64-bit version
8245 * taking care of the secure bank. This requires that separate
8246 * 32 and 64-bit definitions are provided.
8247 */
8248 if ((r->state == ARM_CP_STATE_BOTH && ns) ||
8249 (arm_feature(&cpu->env, ARM_FEATURE_V8) && !ns)) {
7a0e58fa 8250 r2->type |= ARM_CP_ALIAS;
3f3c82a5
FA
8251 }
8252 } else if ((secstate != r->secure) && !ns) {
8253 /* The register is not banked so we only want to allow migration of
8254 * the non-secure instance.
8255 */
7a0e58fa 8256 r2->type |= ARM_CP_ALIAS;
58a1d8ce 8257 }
3f3c82a5
FA
8258
8259 if (r->state == ARM_CP_STATE_BOTH) {
8260 /* We assume it is a cp15 register if the .cp field is left unset.
8261 */
8262 if (r2->cp == 0) {
8263 r2->cp = 15;
8264 }
8265
f5a0a5a5 8266#ifdef HOST_WORDS_BIGENDIAN
3f3c82a5
FA
8267 if (r2->fieldoffset) {
8268 r2->fieldoffset += sizeof(uint32_t);
8269 }
f5a0a5a5 8270#endif
3f3c82a5 8271 }
f5a0a5a5
PM
8272 }
8273 if (state == ARM_CP_STATE_AA64) {
8274 /* To allow abbreviation of ARMCPRegInfo
8275 * definitions, we treat cp == 0 as equivalent to
8276 * the value for "standard guest-visible sysreg".
58a1d8ce
PM
8277 * STATE_BOTH definitions are also always "standard
8278 * sysreg" in their AArch64 view (the .cp value may
8279 * be non-zero for the benefit of the AArch32 view).
f5a0a5a5 8280 */
58a1d8ce 8281 if (r->cp == 0 || r->state == ARM_CP_STATE_BOTH) {
f5a0a5a5
PM
8282 r2->cp = CP_REG_ARM64_SYSREG_CP;
8283 }
8284 *key = ENCODE_AA64_CP_REG(r2->cp, r2->crn, crm,
8285 r2->opc0, opc1, opc2);
8286 } else {
51a79b03 8287 *key = ENCODE_CP_REG(r2->cp, is64, ns, r2->crn, crm, opc1, opc2);
f5a0a5a5 8288 }
6e6efd61
PM
8289 if (opaque) {
8290 r2->opaque = opaque;
8291 }
67ed771d
PM
8292 /* reginfo passed to helpers is correct for the actual access,
8293 * and is never ARM_CP_STATE_BOTH:
8294 */
8295 r2->state = state;
6e6efd61
PM
8296 /* Make sure reginfo passed to helpers for wildcarded regs
8297 * has the correct crm/opc1/opc2 for this reg, not CP_ANY:
8298 */
8299 r2->crm = crm;
8300 r2->opc1 = opc1;
8301 r2->opc2 = opc2;
8302 /* By convention, for wildcarded registers only the first
8303 * entry is used for migration; the others are marked as
7a0e58fa 8304 * ALIAS so we don't try to transfer the register
6e6efd61 8305 * multiple times. Special registers (ie NOP/WFI) are
7a0e58fa 8306 * never migratable and not even raw-accessible.
6e6efd61 8307 */
7a0e58fa
PM
8308 if ((r->type & ARM_CP_SPECIAL)) {
8309 r2->type |= ARM_CP_NO_RAW;
8310 }
8311 if (((r->crm == CP_ANY) && crm != 0) ||
6e6efd61
PM
8312 ((r->opc1 == CP_ANY) && opc1 != 0) ||
8313 ((r->opc2 == CP_ANY) && opc2 != 0)) {
1f163787 8314 r2->type |= ARM_CP_ALIAS | ARM_CP_NO_GDB;
6e6efd61
PM
8315 }
8316
375421cc
PM
8317 /* Check that raw accesses are either forbidden or handled. Note that
8318 * we can't assert this earlier because the setup of fieldoffset for
8319 * banked registers has to be done first.
8320 */
8321 if (!(r2->type & ARM_CP_NO_RAW)) {
8322 assert(!raw_accessors_invalid(r2));
8323 }
8324
6e6efd61
PM
8325 /* Overriding of an existing definition must be explicitly
8326 * requested.
8327 */
8328 if (!(r->type & ARM_CP_OVERRIDE)) {
8329 ARMCPRegInfo *oldreg;
8330 oldreg = g_hash_table_lookup(cpu->cp_regs, key);
8331 if (oldreg && !(oldreg->type & ARM_CP_OVERRIDE)) {
8332 fprintf(stderr, "Register redefined: cp=%d %d bit "
8333 "crn=%d crm=%d opc1=%d opc2=%d, "
8334 "was %s, now %s\n", r2->cp, 32 + 32 * is64,
8335 r2->crn, r2->crm, r2->opc1, r2->opc2,
8336 oldreg->name, r2->name);
8337 g_assert_not_reached();
8338 }
8339 }
8340 g_hash_table_insert(cpu->cp_regs, key, r2);
8341}
8342
8343
4b6a83fb
PM
8344void define_one_arm_cp_reg_with_opaque(ARMCPU *cpu,
8345 const ARMCPRegInfo *r, void *opaque)
8346{
8347 /* Define implementations of coprocessor registers.
8348 * We store these in a hashtable because typically
8349 * there are less than 150 registers in a space which
8350 * is 16*16*16*8*8 = 262144 in size.
8351 * Wildcarding is supported for the crm, opc1 and opc2 fields.
8352 * If a register is defined twice then the second definition is
8353 * used, so this can be used to define some generic registers and
8354 * then override them with implementation specific variations.
8355 * At least one of the original and the second definition should
8356 * include ARM_CP_OVERRIDE in its type bits -- this is just a guard
8357 * against accidental use.
f5a0a5a5
PM
8358 *
8359 * The state field defines whether the register is to be
8360 * visible in the AArch32 or AArch64 execution state. If the
8361 * state is set to ARM_CP_STATE_BOTH then we synthesise a
8362 * reginfo structure for the AArch32 view, which sees the lower
8363 * 32 bits of the 64 bit register.
8364 *
8365 * Only registers visible in AArch64 may set r->opc0; opc0 cannot
8366 * be wildcarded. AArch64 registers are always considered to be 64
8367 * bits; the ARM_CP_64BIT* flag applies only to the AArch32 view of
8368 * the register, if any.
4b6a83fb 8369 */
f5a0a5a5 8370 int crm, opc1, opc2, state;
4b6a83fb
PM
8371 int crmmin = (r->crm == CP_ANY) ? 0 : r->crm;
8372 int crmmax = (r->crm == CP_ANY) ? 15 : r->crm;
8373 int opc1min = (r->opc1 == CP_ANY) ? 0 : r->opc1;
8374 int opc1max = (r->opc1 == CP_ANY) ? 7 : r->opc1;
8375 int opc2min = (r->opc2 == CP_ANY) ? 0 : r->opc2;
8376 int opc2max = (r->opc2 == CP_ANY) ? 7 : r->opc2;
8377 /* 64 bit registers have only CRm and Opc1 fields */
8378 assert(!((r->type & ARM_CP_64BIT) && (r->opc2 || r->crn)));
f5a0a5a5
PM
8379 /* op0 only exists in the AArch64 encodings */
8380 assert((r->state != ARM_CP_STATE_AA32) || (r->opc0 == 0));
8381 /* AArch64 regs are all 64 bit so ARM_CP_64BIT is meaningless */
8382 assert((r->state != ARM_CP_STATE_AA64) || !(r->type & ARM_CP_64BIT));
8383 /* The AArch64 pseudocode CheckSystemAccess() specifies that op1
8384 * encodes a minimum access level for the register. We roll this
8385 * runtime check into our general permission check code, so check
8386 * here that the reginfo's specified permissions are strict enough
8387 * to encompass the generic architectural permission check.
8388 */
8389 if (r->state != ARM_CP_STATE_AA32) {
8390 int mask = 0;
8391 switch (r->opc1) {
b5bd7440
AB
8392 case 0:
8393 /* min_EL EL1, but some accessible to EL0 via kernel ABI */
8394 mask = PL0U_R | PL1_RW;
8395 break;
8396 case 1: case 2:
f5a0a5a5
PM
8397 /* min_EL EL1 */
8398 mask = PL1_RW;
8399 break;
8400 case 3:
8401 /* min_EL EL0 */
8402 mask = PL0_RW;
8403 break;
8404 case 4:
b4ecf60f 8405 case 5:
f5a0a5a5
PM
8406 /* min_EL EL2 */
8407 mask = PL2_RW;
8408 break;
f5a0a5a5
PM
8409 case 6:
8410 /* min_EL EL3 */
8411 mask = PL3_RW;
8412 break;
8413 case 7:
8414 /* min_EL EL1, secure mode only (we don't check the latter) */
8415 mask = PL1_RW;
8416 break;
8417 default:
8418 /* broken reginfo with out-of-range opc1 */
8419 assert(false);
8420 break;
8421 }
8422 /* assert our permissions are not too lax (stricter is fine) */
8423 assert((r->access & ~mask) == 0);
8424 }
8425
4b6a83fb
PM
8426 /* Check that the register definition has enough info to handle
8427 * reads and writes if they are permitted.
8428 */
8429 if (!(r->type & (ARM_CP_SPECIAL|ARM_CP_CONST))) {
8430 if (r->access & PL3_R) {
3f3c82a5
FA
8431 assert((r->fieldoffset ||
8432 (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
8433 r->readfn);
4b6a83fb
PM
8434 }
8435 if (r->access & PL3_W) {
3f3c82a5
FA
8436 assert((r->fieldoffset ||
8437 (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
8438 r->writefn);
4b6a83fb
PM
8439 }
8440 }
8441 /* Bad type field probably means missing sentinel at end of reg list */
8442 assert(cptype_valid(r->type));
8443 for (crm = crmmin; crm <= crmmax; crm++) {
8444 for (opc1 = opc1min; opc1 <= opc1max; opc1++) {
8445 for (opc2 = opc2min; opc2 <= opc2max; opc2++) {
f5a0a5a5
PM
8446 for (state = ARM_CP_STATE_AA32;
8447 state <= ARM_CP_STATE_AA64; state++) {
8448 if (r->state != state && r->state != ARM_CP_STATE_BOTH) {
8449 continue;
8450 }
3f3c82a5
FA
8451 if (state == ARM_CP_STATE_AA32) {
8452 /* Under AArch32 CP registers can be common
8453 * (same for secure and non-secure world) or banked.
8454 */
9c513e78
AB
8455 char *name;
8456
3f3c82a5
FA
8457 switch (r->secure) {
8458 case ARM_CP_SECSTATE_S:
8459 case ARM_CP_SECSTATE_NS:
8460 add_cpreg_to_hashtable(cpu, r, opaque, state,
9c513e78
AB
8461 r->secure, crm, opc1, opc2,
8462 r->name);
3f3c82a5
FA
8463 break;
8464 default:
9c513e78 8465 name = g_strdup_printf("%s_S", r->name);
3f3c82a5
FA
8466 add_cpreg_to_hashtable(cpu, r, opaque, state,
8467 ARM_CP_SECSTATE_S,
9c513e78
AB
8468 crm, opc1, opc2, name);
8469 g_free(name);
3f3c82a5
FA
8470 add_cpreg_to_hashtable(cpu, r, opaque, state,
8471 ARM_CP_SECSTATE_NS,
9c513e78 8472 crm, opc1, opc2, r->name);
3f3c82a5
FA
8473 break;
8474 }
8475 } else {
8476 /* AArch64 registers get mapped to non-secure instance
8477 * of AArch32 */
8478 add_cpreg_to_hashtable(cpu, r, opaque, state,
8479 ARM_CP_SECSTATE_NS,
9c513e78 8480 crm, opc1, opc2, r->name);
3f3c82a5 8481 }
f5a0a5a5 8482 }
4b6a83fb
PM
8483 }
8484 }
8485 }
8486}
8487
8488void define_arm_cp_regs_with_opaque(ARMCPU *cpu,
8489 const ARMCPRegInfo *regs, void *opaque)
8490{
8491 /* Define a whole list of registers */
8492 const ARMCPRegInfo *r;
8493 for (r = regs; r->type != ARM_CP_SENTINEL; r++) {
8494 define_one_arm_cp_reg_with_opaque(cpu, r, opaque);
8495 }
8496}
8497
6c5c0fec
AB
8498/*
8499 * Modify ARMCPRegInfo for access from userspace.
8500 *
8501 * This is a data driven modification directed by
8502 * ARMCPRegUserSpaceInfo. All registers become ARM_CP_CONST as
8503 * user-space cannot alter any values and dynamic values pertaining to
8504 * execution state are hidden from user space view anyway.
8505 */
8506void modify_arm_cp_regs(ARMCPRegInfo *regs, const ARMCPRegUserSpaceInfo *mods)
8507{
8508 const ARMCPRegUserSpaceInfo *m;
8509 ARMCPRegInfo *r;
8510
8511 for (m = mods; m->name; m++) {
d040242e
AB
8512 GPatternSpec *pat = NULL;
8513 if (m->is_glob) {
8514 pat = g_pattern_spec_new(m->name);
8515 }
6c5c0fec 8516 for (r = regs; r->type != ARM_CP_SENTINEL; r++) {
d040242e
AB
8517 if (pat && g_pattern_match_string(pat, r->name)) {
8518 r->type = ARM_CP_CONST;
8519 r->access = PL0U_R;
8520 r->resetvalue = 0;
8521 /* continue */
8522 } else if (strcmp(r->name, m->name) == 0) {
6c5c0fec
AB
8523 r->type = ARM_CP_CONST;
8524 r->access = PL0U_R;
8525 r->resetvalue &= m->exported_bits;
8526 r->resetvalue |= m->fixed_bits;
8527 break;
8528 }
8529 }
d040242e
AB
8530 if (pat) {
8531 g_pattern_spec_free(pat);
8532 }
6c5c0fec
AB
8533 }
8534}
8535
60322b39 8536const ARMCPRegInfo *get_arm_cp_reginfo(GHashTable *cpregs, uint32_t encoded_cp)
4b6a83fb 8537{
60322b39 8538 return g_hash_table_lookup(cpregs, &encoded_cp);
4b6a83fb
PM
8539}
8540
c4241c7d
PM
8541void arm_cp_write_ignore(CPUARMState *env, const ARMCPRegInfo *ri,
8542 uint64_t value)
4b6a83fb
PM
8543{
8544 /* Helper coprocessor write function for write-ignore registers */
4b6a83fb
PM
8545}
8546
c4241c7d 8547uint64_t arm_cp_read_zero(CPUARMState *env, const ARMCPRegInfo *ri)
4b6a83fb
PM
8548{
8549 /* Helper coprocessor write function for read-as-zero registers */
4b6a83fb
PM
8550 return 0;
8551}
8552
f5a0a5a5
PM
8553void arm_cp_reset_ignore(CPUARMState *env, const ARMCPRegInfo *opaque)
8554{
8555 /* Helper coprocessor reset function for do-nothing-on-reset registers */
8556}
8557
af393ffc 8558static int bad_mode_switch(CPUARMState *env, int mode, CPSRWriteType write_type)
37064a8b
PM
8559{
8560 /* Return true if it is not valid for us to switch to
8561 * this CPU mode (ie all the UNPREDICTABLE cases in
8562 * the ARM ARM CPSRWriteByInstr pseudocode).
8563 */
af393ffc
PM
8564
8565 /* Changes to or from Hyp via MSR and CPS are illegal. */
8566 if (write_type == CPSRWriteByInstr &&
8567 ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_HYP ||
8568 mode == ARM_CPU_MODE_HYP)) {
8569 return 1;
8570 }
8571
37064a8b
PM
8572 switch (mode) {
8573 case ARM_CPU_MODE_USR:
10eacda7 8574 return 0;
37064a8b
PM
8575 case ARM_CPU_MODE_SYS:
8576 case ARM_CPU_MODE_SVC:
8577 case ARM_CPU_MODE_ABT:
8578 case ARM_CPU_MODE_UND:
8579 case ARM_CPU_MODE_IRQ:
8580 case ARM_CPU_MODE_FIQ:
52ff951b
PM
8581 /* Note that we don't implement the IMPDEF NSACR.RFR which in v7
8582 * allows FIQ mode to be Secure-only. (In v8 this doesn't exist.)
8583 */
10eacda7
PM
8584 /* If HCR.TGE is set then changes from Monitor to NS PL1 via MSR
8585 * and CPS are treated as illegal mode changes.
8586 */
8587 if (write_type == CPSRWriteByInstr &&
10eacda7 8588 (env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON &&
7c208e0f 8589 (arm_hcr_el2_eff(env) & HCR_TGE)) {
10eacda7
PM
8590 return 1;
8591 }
37064a8b 8592 return 0;
e6c8fc07
PM
8593 case ARM_CPU_MODE_HYP:
8594 return !arm_feature(env, ARM_FEATURE_EL2)
2d2a4549 8595 || arm_current_el(env) < 2 || arm_is_secure_below_el3(env);
027fc527 8596 case ARM_CPU_MODE_MON:
58ae2d1f 8597 return arm_current_el(env) < 3;
37064a8b
PM
8598 default:
8599 return 1;
8600 }
8601}
8602
2f4a40e5
AZ
8603uint32_t cpsr_read(CPUARMState *env)
8604{
8605 int ZF;
6fbe23d5
PB
8606 ZF = (env->ZF == 0);
8607 return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) |
2f4a40e5
AZ
8608 (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27)
8609 | (env->thumb << 5) | ((env->condexec_bits & 3) << 25)
8610 | ((env->condexec_bits & 0xfc) << 8)
af519934 8611 | (env->GE << 16) | (env->daif & CPSR_AIF);
2f4a40e5
AZ
8612}
8613
50866ba5
PM
8614void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask,
8615 CPSRWriteType write_type)
2f4a40e5 8616{
6e8801f9
FA
8617 uint32_t changed_daif;
8618
2f4a40e5 8619 if (mask & CPSR_NZCV) {
6fbe23d5
PB
8620 env->ZF = (~val) & CPSR_Z;
8621 env->NF = val;
2f4a40e5
AZ
8622 env->CF = (val >> 29) & 1;
8623 env->VF = (val << 3) & 0x80000000;
8624 }
8625 if (mask & CPSR_Q)
8626 env->QF = ((val & CPSR_Q) != 0);
8627 if (mask & CPSR_T)
8628 env->thumb = ((val & CPSR_T) != 0);
8629 if (mask & CPSR_IT_0_1) {
8630 env->condexec_bits &= ~3;
8631 env->condexec_bits |= (val >> 25) & 3;
8632 }
8633 if (mask & CPSR_IT_2_7) {
8634 env->condexec_bits &= 3;
8635 env->condexec_bits |= (val >> 8) & 0xfc;
8636 }
8637 if (mask & CPSR_GE) {
8638 env->GE = (val >> 16) & 0xf;
8639 }
8640
6e8801f9
FA
8641 /* In a V7 implementation that includes the security extensions but does
8642 * not include Virtualization Extensions the SCR.FW and SCR.AW bits control
8643 * whether non-secure software is allowed to change the CPSR_F and CPSR_A
8644 * bits respectively.
8645 *
8646 * In a V8 implementation, it is permitted for privileged software to
8647 * change the CPSR A/F bits regardless of the SCR.AW/FW bits.
8648 */
f8c88bbc 8649 if (write_type != CPSRWriteRaw && !arm_feature(env, ARM_FEATURE_V8) &&
6e8801f9
FA
8650 arm_feature(env, ARM_FEATURE_EL3) &&
8651 !arm_feature(env, ARM_FEATURE_EL2) &&
8652 !arm_is_secure(env)) {
8653
8654 changed_daif = (env->daif ^ val) & mask;
8655
8656 if (changed_daif & CPSR_A) {
8657 /* Check to see if we are allowed to change the masking of async
8658 * abort exceptions from a non-secure state.
8659 */
8660 if (!(env->cp15.scr_el3 & SCR_AW)) {
8661 qemu_log_mask(LOG_GUEST_ERROR,
8662 "Ignoring attempt to switch CPSR_A flag from "
8663 "non-secure world with SCR.AW bit clear\n");
8664 mask &= ~CPSR_A;
8665 }
8666 }
8667
8668 if (changed_daif & CPSR_F) {
8669 /* Check to see if we are allowed to change the masking of FIQ
8670 * exceptions from a non-secure state.
8671 */
8672 if (!(env->cp15.scr_el3 & SCR_FW)) {
8673 qemu_log_mask(LOG_GUEST_ERROR,
8674 "Ignoring attempt to switch CPSR_F flag from "
8675 "non-secure world with SCR.FW bit clear\n");
8676 mask &= ~CPSR_F;
8677 }
8678
8679 /* Check whether non-maskable FIQ (NMFI) support is enabled.
8680 * If this bit is set software is not allowed to mask
8681 * FIQs, but is allowed to set CPSR_F to 0.
8682 */
8683 if ((A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_NMFI) &&
8684 (val & CPSR_F)) {
8685 qemu_log_mask(LOG_GUEST_ERROR,
8686 "Ignoring attempt to enable CPSR_F flag "
8687 "(non-maskable FIQ [NMFI] support enabled)\n");
8688 mask &= ~CPSR_F;
8689 }
8690 }
8691 }
8692
4cc35614
PM
8693 env->daif &= ~(CPSR_AIF & mask);
8694 env->daif |= val & CPSR_AIF & mask;
8695
f8c88bbc
PM
8696 if (write_type != CPSRWriteRaw &&
8697 ((env->uncached_cpsr ^ val) & mask & CPSR_M)) {
8c4f0eb9
PM
8698 if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_USR) {
8699 /* Note that we can only get here in USR mode if this is a
8700 * gdb stub write; for this case we follow the architectural
8701 * behaviour for guest writes in USR mode of ignoring an attempt
8702 * to switch mode. (Those are caught by translate.c for writes
8703 * triggered by guest instructions.)
8704 */
8705 mask &= ~CPSR_M;
8706 } else if (bad_mode_switch(env, val & CPSR_M, write_type)) {
81907a58
PM
8707 /* Attempt to switch to an invalid mode: this is UNPREDICTABLE in
8708 * v7, and has defined behaviour in v8:
8709 * + leave CPSR.M untouched
8710 * + allow changes to the other CPSR fields
8711 * + set PSTATE.IL
8712 * For user changes via the GDB stub, we don't set PSTATE.IL,
8713 * as this would be unnecessarily harsh for a user error.
37064a8b
PM
8714 */
8715 mask &= ~CPSR_M;
81907a58
PM
8716 if (write_type != CPSRWriteByGDBStub &&
8717 arm_feature(env, ARM_FEATURE_V8)) {
8718 mask |= CPSR_IL;
8719 val |= CPSR_IL;
8720 }
81e37284
PM
8721 qemu_log_mask(LOG_GUEST_ERROR,
8722 "Illegal AArch32 mode switch attempt from %s to %s\n",
8723 aarch32_mode_name(env->uncached_cpsr),
8724 aarch32_mode_name(val));
37064a8b 8725 } else {
81e37284
PM
8726 qemu_log_mask(CPU_LOG_INT, "%s %s to %s PC 0x%" PRIx32 "\n",
8727 write_type == CPSRWriteExceptionReturn ?
8728 "Exception return from AArch32" :
8729 "AArch32 mode switch from",
8730 aarch32_mode_name(env->uncached_cpsr),
8731 aarch32_mode_name(val), env->regs[15]);
37064a8b
PM
8732 switch_mode(env, val & CPSR_M);
8733 }
2f4a40e5
AZ
8734 }
8735 mask &= ~CACHED_CPSR_BITS;
8736 env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask);
8737}
8738
b26eefb6
PB
8739/* Sign/zero extend */
8740uint32_t HELPER(sxtb16)(uint32_t x)
8741{
8742 uint32_t res;
8743 res = (uint16_t)(int8_t)x;
8744 res |= (uint32_t)(int8_t)(x >> 16) << 16;
8745 return res;
8746}
8747
8748uint32_t HELPER(uxtb16)(uint32_t x)
8749{
8750 uint32_t res;
8751 res = (uint16_t)(uint8_t)x;
8752 res |= (uint32_t)(uint8_t)(x >> 16) << 16;
8753 return res;
8754}
8755
3670669c
PB
8756int32_t HELPER(sdiv)(int32_t num, int32_t den)
8757{
8758 if (den == 0)
8759 return 0;
686eeb93
AJ
8760 if (num == INT_MIN && den == -1)
8761 return INT_MIN;
3670669c
PB
8762 return num / den;
8763}
8764
8765uint32_t HELPER(udiv)(uint32_t num, uint32_t den)
8766{
8767 if (den == 0)
8768 return 0;
8769 return num / den;
8770}
8771
8772uint32_t HELPER(rbit)(uint32_t x)
8773{
42fedbca 8774 return revbit32(x);
3670669c
PB
8775}
8776
c47eaf9f 8777#ifdef CONFIG_USER_ONLY
b5ff1b31 8778
affdb64d 8779static void switch_mode(CPUARMState *env, int mode)
b5ff1b31 8780{
2fc0cc0e 8781 ARMCPU *cpu = env_archcpu(env);
a47dddd7
AF
8782
8783 if (mode != ARM_CPU_MODE_USR) {
8784 cpu_abort(CPU(cpu), "Tried to switch out of user mode\n");
8785 }
b5ff1b31
FB
8786}
8787
012a906b
GB
8788uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
8789 uint32_t cur_el, bool secure)
9e729b57
EI
8790{
8791 return 1;
8792}
8793
ce02049d
GB
8794void aarch64_sync_64_to_32(CPUARMState *env)
8795{
8796 g_assert_not_reached();
8797}
8798
b5ff1b31
FB
8799#else
8800
affdb64d 8801static void switch_mode(CPUARMState *env, int mode)
b5ff1b31
FB
8802{
8803 int old_mode;
8804 int i;
8805
8806 old_mode = env->uncached_cpsr & CPSR_M;
8807 if (mode == old_mode)
8808 return;
8809
8810 if (old_mode == ARM_CPU_MODE_FIQ) {
8811 memcpy (env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t));
8637c67f 8812 memcpy (env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t));
b5ff1b31
FB
8813 } else if (mode == ARM_CPU_MODE_FIQ) {
8814 memcpy (env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t));
8637c67f 8815 memcpy (env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t));
b5ff1b31
FB
8816 }
8817
f5206413 8818 i = bank_number(old_mode);
b5ff1b31 8819 env->banked_r13[i] = env->regs[13];
b5ff1b31
FB
8820 env->banked_spsr[i] = env->spsr;
8821
f5206413 8822 i = bank_number(mode);
b5ff1b31 8823 env->regs[13] = env->banked_r13[i];
b5ff1b31 8824 env->spsr = env->banked_spsr[i];
593cfa2b
PM
8825
8826 env->banked_r14[r14_bank_number(old_mode)] = env->regs[14];
8827 env->regs[14] = env->banked_r14[r14_bank_number(mode)];
b5ff1b31
FB
8828}
8829
0eeb17d6
GB
8830/* Physical Interrupt Target EL Lookup Table
8831 *
8832 * [ From ARM ARM section G1.13.4 (Table G1-15) ]
8833 *
8834 * The below multi-dimensional table is used for looking up the target
8835 * exception level given numerous condition criteria. Specifically, the
8836 * target EL is based on SCR and HCR routing controls as well as the
8837 * currently executing EL and secure state.
8838 *
8839 * Dimensions:
8840 * target_el_table[2][2][2][2][2][4]
8841 * | | | | | +--- Current EL
8842 * | | | | +------ Non-secure(0)/Secure(1)
8843 * | | | +--------- HCR mask override
8844 * | | +------------ SCR exec state control
8845 * | +--------------- SCR mask override
8846 * +------------------ 32-bit(0)/64-bit(1) EL3
8847 *
8848 * The table values are as such:
8849 * 0-3 = EL0-EL3
8850 * -1 = Cannot occur
8851 *
8852 * The ARM ARM target EL table includes entries indicating that an "exception
8853 * is not taken". The two cases where this is applicable are:
8854 * 1) An exception is taken from EL3 but the SCR does not have the exception
8855 * routed to EL3.
8856 * 2) An exception is taken from EL2 but the HCR does not have the exception
8857 * routed to EL2.
8858 * In these two cases, the below table contain a target of EL1. This value is
8859 * returned as it is expected that the consumer of the table data will check
8860 * for "target EL >= current EL" to ensure the exception is not taken.
8861 *
8862 * SCR HCR
8863 * 64 EA AMO From
8864 * BIT IRQ IMO Non-secure Secure
8865 * EL3 FIQ RW FMO EL0 EL1 EL2 EL3 EL0 EL1 EL2 EL3
8866 */
82c39f6a 8867static const int8_t target_el_table[2][2][2][2][2][4] = {
0eeb17d6
GB
8868 {{{{/* 0 0 0 0 */{ 1, 1, 2, -1 },{ 3, -1, -1, 3 },},
8869 {/* 0 0 0 1 */{ 2, 2, 2, -1 },{ 3, -1, -1, 3 },},},
8870 {{/* 0 0 1 0 */{ 1, 1, 2, -1 },{ 3, -1, -1, 3 },},
8871 {/* 0 0 1 1 */{ 2, 2, 2, -1 },{ 3, -1, -1, 3 },},},},
8872 {{{/* 0 1 0 0 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},
8873 {/* 0 1 0 1 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},},
8874 {{/* 0 1 1 0 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},
8875 {/* 0 1 1 1 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},},},},
8876 {{{{/* 1 0 0 0 */{ 1, 1, 2, -1 },{ 1, 1, -1, 1 },},
8877 {/* 1 0 0 1 */{ 2, 2, 2, -1 },{ 1, 1, -1, 1 },},},
8878 {{/* 1 0 1 0 */{ 1, 1, 1, -1 },{ 1, 1, -1, 1 },},
8879 {/* 1 0 1 1 */{ 2, 2, 2, -1 },{ 1, 1, -1, 1 },},},},
8880 {{{/* 1 1 0 0 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},
8881 {/* 1 1 0 1 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},},
8882 {{/* 1 1 1 0 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},
8883 {/* 1 1 1 1 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},},},},
8884};
8885
8886/*
8887 * Determine the target EL for physical exceptions
8888 */
012a906b
GB
8889uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
8890 uint32_t cur_el, bool secure)
0eeb17d6
GB
8891{
8892 CPUARMState *env = cs->env_ptr;
f7778444
RH
8893 bool rw;
8894 bool scr;
8895 bool hcr;
0eeb17d6 8896 int target_el;
2cde031f 8897 /* Is the highest EL AArch64? */
f7778444
RH
8898 bool is64 = arm_feature(env, ARM_FEATURE_AARCH64);
8899 uint64_t hcr_el2;
2cde031f
SS
8900
8901 if (arm_feature(env, ARM_FEATURE_EL3)) {
8902 rw = ((env->cp15.scr_el3 & SCR_RW) == SCR_RW);
8903 } else {
8904 /* Either EL2 is the highest EL (and so the EL2 register width
8905 * is given by is64); or there is no EL2 or EL3, in which case
8906 * the value of 'rw' does not affect the table lookup anyway.
8907 */
8908 rw = is64;
8909 }
0eeb17d6 8910
f7778444 8911 hcr_el2 = arm_hcr_el2_eff(env);
0eeb17d6
GB
8912 switch (excp_idx) {
8913 case EXCP_IRQ:
8914 scr = ((env->cp15.scr_el3 & SCR_IRQ) == SCR_IRQ);
f7778444 8915 hcr = hcr_el2 & HCR_IMO;
0eeb17d6
GB
8916 break;
8917 case EXCP_FIQ:
8918 scr = ((env->cp15.scr_el3 & SCR_FIQ) == SCR_FIQ);
f7778444 8919 hcr = hcr_el2 & HCR_FMO;
0eeb17d6
GB
8920 break;
8921 default:
8922 scr = ((env->cp15.scr_el3 & SCR_EA) == SCR_EA);
f7778444 8923 hcr = hcr_el2 & HCR_AMO;
0eeb17d6
GB
8924 break;
8925 };
8926
d1b31428
RH
8927 /*
8928 * For these purposes, TGE and AMO/IMO/FMO both force the
8929 * interrupt to EL2. Fold TGE into the bit extracted above.
8930 */
8931 hcr |= (hcr_el2 & HCR_TGE) != 0;
8932
0eeb17d6
GB
8933 /* Perform a table-lookup for the target EL given the current state */
8934 target_el = target_el_table[is64][scr][rw][hcr][secure][cur_el];
8935
8936 assert(target_el > 0);
8937
8938 return target_el;
8939}
8940
b59f479b
PMD
8941void arm_log_exception(int idx)
8942{
8943 if (qemu_loglevel_mask(CPU_LOG_INT)) {
8944 const char *exc = NULL;
8945 static const char * const excnames[] = {
8946 [EXCP_UDEF] = "Undefined Instruction",
8947 [EXCP_SWI] = "SVC",
8948 [EXCP_PREFETCH_ABORT] = "Prefetch Abort",
8949 [EXCP_DATA_ABORT] = "Data Abort",
8950 [EXCP_IRQ] = "IRQ",
8951 [EXCP_FIQ] = "FIQ",
8952 [EXCP_BKPT] = "Breakpoint",
8953 [EXCP_EXCEPTION_EXIT] = "QEMU v7M exception exit",
8954 [EXCP_KERNEL_TRAP] = "QEMU intercept of kernel commpage",
8955 [EXCP_HVC] = "Hypervisor Call",
8956 [EXCP_HYP_TRAP] = "Hypervisor Trap",
8957 [EXCP_SMC] = "Secure Monitor Call",
8958 [EXCP_VIRQ] = "Virtual IRQ",
8959 [EXCP_VFIQ] = "Virtual FIQ",
8960 [EXCP_SEMIHOST] = "Semihosting call",
8961 [EXCP_NOCP] = "v7M NOCP UsageFault",
8962 [EXCP_INVSTATE] = "v7M INVSTATE UsageFault",
8963 [EXCP_STKOF] = "v8M STKOF UsageFault",
8964 [EXCP_LAZYFP] = "v7M exception during lazy FP stacking",
8965 [EXCP_LSERR] = "v8M LSERR UsageFault",
8966 [EXCP_UNALIGNED] = "v7M UNALIGNED UsageFault",
8967 };
8968
8969 if (idx >= 0 && idx < ARRAY_SIZE(excnames)) {
8970 exc = excnames[idx];
8971 }
8972 if (!exc) {
8973 exc = "unknown";
8974 }
8975 qemu_log_mask(CPU_LOG_INT, "Taking exception %d [%s]\n", idx, exc);
8976 }
8977}
8978
a356dacf 8979/*
7aab5a8c
PMD
8980 * Function used to synchronize QEMU's AArch64 register set with AArch32
8981 * register set. This is necessary when switching between AArch32 and AArch64
8982 * execution state.
a356dacf 8983 */
7aab5a8c 8984void aarch64_sync_32_to_64(CPUARMState *env)
9ee6e8bb 8985{
7aab5a8c
PMD
8986 int i;
8987 uint32_t mode = env->uncached_cpsr & CPSR_M;
8988
8989 /* We can blanket copy R[0:7] to X[0:7] */
8990 for (i = 0; i < 8; i++) {
8991 env->xregs[i] = env->regs[i];
fd592d89 8992 }
70d74660 8993
9a223097 8994 /*
7aab5a8c
PMD
8995 * Unless we are in FIQ mode, x8-x12 come from the user registers r8-r12.
8996 * Otherwise, they come from the banked user regs.
fd592d89 8997 */
7aab5a8c
PMD
8998 if (mode == ARM_CPU_MODE_FIQ) {
8999 for (i = 8; i < 13; i++) {
9000 env->xregs[i] = env->usr_regs[i - 8];
9001 }
9002 } else {
9003 for (i = 8; i < 13; i++) {
9004 env->xregs[i] = env->regs[i];
9005 }
fd592d89 9006 }
9ee6e8bb 9007
7aab5a8c
PMD
9008 /*
9009 * Registers x13-x23 are the various mode SP and FP registers. Registers
9010 * r13 and r14 are only copied if we are in that mode, otherwise we copy
9011 * from the mode banked register.
9012 */
9013 if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
9014 env->xregs[13] = env->regs[13];
9015 env->xregs[14] = env->regs[14];
9016 } else {
9017 env->xregs[13] = env->banked_r13[bank_number(ARM_CPU_MODE_USR)];
9018 /* HYP is an exception in that it is copied from r14 */
9019 if (mode == ARM_CPU_MODE_HYP) {
9020 env->xregs[14] = env->regs[14];
95695eff 9021 } else {
7aab5a8c 9022 env->xregs[14] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)];
95695eff 9023 }
95695eff
PM
9024 }
9025
7aab5a8c
PMD
9026 if (mode == ARM_CPU_MODE_HYP) {
9027 env->xregs[15] = env->regs[13];
9028 } else {
9029 env->xregs[15] = env->banked_r13[bank_number(ARM_CPU_MODE_HYP)];
95695eff
PM
9030 }
9031
7aab5a8c
PMD
9032 if (mode == ARM_CPU_MODE_IRQ) {
9033 env->xregs[16] = env->regs[14];
9034 env->xregs[17] = env->regs[13];
9035 } else {
9036 env->xregs[16] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)];
9037 env->xregs[17] = env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)];
9038 }
95695eff 9039
7aab5a8c
PMD
9040 if (mode == ARM_CPU_MODE_SVC) {
9041 env->xregs[18] = env->regs[14];
9042 env->xregs[19] = env->regs[13];
9043 } else {
9044 env->xregs[18] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)];
9045 env->xregs[19] = env->banked_r13[bank_number(ARM_CPU_MODE_SVC)];
9046 }
95695eff 9047
7aab5a8c
PMD
9048 if (mode == ARM_CPU_MODE_ABT) {
9049 env->xregs[20] = env->regs[14];
9050 env->xregs[21] = env->regs[13];
9051 } else {
9052 env->xregs[20] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)];
9053 env->xregs[21] = env->banked_r13[bank_number(ARM_CPU_MODE_ABT)];
9054 }
e33cf0f8 9055
7aab5a8c
PMD
9056 if (mode == ARM_CPU_MODE_UND) {
9057 env->xregs[22] = env->regs[14];
9058 env->xregs[23] = env->regs[13];
9059 } else {
9060 env->xregs[22] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)];
9061 env->xregs[23] = env->banked_r13[bank_number(ARM_CPU_MODE_UND)];
e33cf0f8
PM
9062 }
9063
9064 /*
7aab5a8c
PMD
9065 * Registers x24-x30 are mapped to r8-r14 in FIQ mode. If we are in FIQ
9066 * mode, then we can copy from r8-r14. Otherwise, we copy from the
9067 * FIQ bank for r8-r14.
e33cf0f8 9068 */
7aab5a8c
PMD
9069 if (mode == ARM_CPU_MODE_FIQ) {
9070 for (i = 24; i < 31; i++) {
9071 env->xregs[i] = env->regs[i - 16]; /* X[24:30] <- R[8:14] */
9072 }
9073 } else {
9074 for (i = 24; i < 29; i++) {
9075 env->xregs[i] = env->fiq_regs[i - 24];
e33cf0f8 9076 }
7aab5a8c
PMD
9077 env->xregs[29] = env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)];
9078 env->xregs[30] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)];
e33cf0f8 9079 }
7aab5a8c
PMD
9080
9081 env->pc = env->regs[15];
e33cf0f8
PM
9082}
9083
9a223097 9084/*
7aab5a8c
PMD
9085 * Function used to synchronize QEMU's AArch32 register set with AArch64
9086 * register set. This is necessary when switching between AArch32 and AArch64
9087 * execution state.
de2db7ec 9088 */
7aab5a8c 9089void aarch64_sync_64_to_32(CPUARMState *env)
9ee6e8bb 9090{
7aab5a8c
PMD
9091 int i;
9092 uint32_t mode = env->uncached_cpsr & CPSR_M;
abc24d86 9093
7aab5a8c
PMD
9094 /* We can blanket copy X[0:7] to R[0:7] */
9095 for (i = 0; i < 8; i++) {
9096 env->regs[i] = env->xregs[i];
de2db7ec 9097 }
3f0cddee 9098
9a223097 9099 /*
7aab5a8c
PMD
9100 * Unless we are in FIQ mode, r8-r12 come from the user registers x8-x12.
9101 * Otherwise, we copy x8-x12 into the banked user regs.
de2db7ec 9102 */
7aab5a8c
PMD
9103 if (mode == ARM_CPU_MODE_FIQ) {
9104 for (i = 8; i < 13; i++) {
9105 env->usr_regs[i - 8] = env->xregs[i];
9106 }
9107 } else {
9108 for (i = 8; i < 13; i++) {
9109 env->regs[i] = env->xregs[i];
9110 }
fb602cb7
PM
9111 }
9112
9a223097 9113 /*
7aab5a8c
PMD
9114 * Registers r13 & r14 depend on the current mode.
9115 * If we are in a given mode, we copy the corresponding x registers to r13
9116 * and r14. Otherwise, we copy the x register to the banked r13 and r14
9117 * for the mode.
fb602cb7 9118 */
7aab5a8c
PMD
9119 if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
9120 env->regs[13] = env->xregs[13];
9121 env->regs[14] = env->xregs[14];
fb602cb7 9122 } else {
7aab5a8c 9123 env->banked_r13[bank_number(ARM_CPU_MODE_USR)] = env->xregs[13];
fb602cb7 9124
7aab5a8c
PMD
9125 /*
9126 * HYP is an exception in that it does not have its own banked r14 but
9127 * shares the USR r14
9128 */
9129 if (mode == ARM_CPU_MODE_HYP) {
9130 env->regs[14] = env->xregs[14];
9131 } else {
9132 env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)] = env->xregs[14];
9133 }
9134 }
fb602cb7 9135
7aab5a8c
PMD
9136 if (mode == ARM_CPU_MODE_HYP) {
9137 env->regs[13] = env->xregs[15];
fb602cb7 9138 } else {
7aab5a8c 9139 env->banked_r13[bank_number(ARM_CPU_MODE_HYP)] = env->xregs[15];
fb602cb7 9140 }
d02a8698 9141
7aab5a8c
PMD
9142 if (mode == ARM_CPU_MODE_IRQ) {
9143 env->regs[14] = env->xregs[16];
9144 env->regs[13] = env->xregs[17];
d02a8698 9145 } else {
7aab5a8c
PMD
9146 env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[16];
9147 env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[17];
d02a8698
PM
9148 }
9149
7aab5a8c
PMD
9150 if (mode == ARM_CPU_MODE_SVC) {
9151 env->regs[14] = env->xregs[18];
9152 env->regs[13] = env->xregs[19];
9153 } else {
9154 env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)] = env->xregs[18];
9155 env->banked_r13[bank_number(ARM_CPU_MODE_SVC)] = env->xregs[19];
fb602cb7
PM
9156 }
9157
7aab5a8c
PMD
9158 if (mode == ARM_CPU_MODE_ABT) {
9159 env->regs[14] = env->xregs[20];
9160 env->regs[13] = env->xregs[21];
9161 } else {
9162 env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)] = env->xregs[20];
9163 env->banked_r13[bank_number(ARM_CPU_MODE_ABT)] = env->xregs[21];
ce02049d
GB
9164 }
9165
9166 if (mode == ARM_CPU_MODE_UND) {
3a9148d0
SS
9167 env->regs[14] = env->xregs[22];
9168 env->regs[13] = env->xregs[23];
ce02049d 9169 } else {
593cfa2b 9170 env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)] = env->xregs[22];
3a9148d0 9171 env->banked_r13[bank_number(ARM_CPU_MODE_UND)] = env->xregs[23];
ce02049d
GB
9172 }
9173
9174 /* Registers x24-x30 are mapped to r8-r14 in FIQ mode. If we are in FIQ
9175 * mode, then we can copy to r8-r14. Otherwise, we copy to the
9176 * FIQ bank for r8-r14.
9177 */
9178 if (mode == ARM_CPU_MODE_FIQ) {
9179 for (i = 24; i < 31; i++) {
9180 env->regs[i - 16] = env->xregs[i]; /* X[24:30] -> R[8:14] */
9181 }
9182 } else {
9183 for (i = 24; i < 29; i++) {
9184 env->fiq_regs[i - 24] = env->xregs[i];
9185 }
9186 env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[29];
593cfa2b 9187 env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[30];
ce02049d
GB
9188 }
9189
9190 env->regs[15] = env->pc;
9191}
9192
dea8378b
PM
9193static void take_aarch32_exception(CPUARMState *env, int new_mode,
9194 uint32_t mask, uint32_t offset,
9195 uint32_t newpc)
9196{
4a2696c0
RH
9197 int new_el;
9198
dea8378b
PM
9199 /* Change the CPU state so as to actually take the exception. */
9200 switch_mode(env, new_mode);
4a2696c0 9201
dea8378b
PM
9202 /*
9203 * For exceptions taken to AArch32 we must clear the SS bit in both
9204 * PSTATE and in the old-state value we save to SPSR_<mode>, so zero it now.
9205 */
9206 env->uncached_cpsr &= ~PSTATE_SS;
9207 env->spsr = cpsr_read(env);
9208 /* Clear IT bits. */
9209 env->condexec_bits = 0;
9210 /* Switch to the new mode, and to the correct instruction set. */
9211 env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode;
88828bf1
CD
9212
9213 /* This must be after mode switching. */
9214 new_el = arm_current_el(env);
9215
dea8378b
PM
9216 /* Set new mode endianness */
9217 env->uncached_cpsr &= ~CPSR_E;
4a2696c0 9218 if (env->cp15.sctlr_el[new_el] & SCTLR_EE) {
dea8378b
PM
9219 env->uncached_cpsr |= CPSR_E;
9220 }
829f9fd3
PM
9221 /* J and IL must always be cleared for exception entry */
9222 env->uncached_cpsr &= ~(CPSR_IL | CPSR_J);
dea8378b
PM
9223 env->daif |= mask;
9224
9225 if (new_mode == ARM_CPU_MODE_HYP) {
9226 env->thumb = (env->cp15.sctlr_el[2] & SCTLR_TE) != 0;
9227 env->elr_el[2] = env->regs[15];
9228 } else {
4a2696c0 9229 /* CPSR.PAN is normally preserved preserved unless... */
f8af1143 9230 if (cpu_isar_feature(aa32_pan, env_archcpu(env))) {
4a2696c0
RH
9231 switch (new_el) {
9232 case 3:
9233 if (!arm_is_secure_below_el3(env)) {
9234 /* ... the target is EL3, from non-secure state. */
9235 env->uncached_cpsr &= ~CPSR_PAN;
9236 break;
9237 }
9238 /* ... the target is EL3, from secure state ... */
9239 /* fall through */
9240 case 1:
9241 /* ... the target is EL1 and SCTLR.SPAN is 0. */
9242 if (!(env->cp15.sctlr_el[new_el] & SCTLR_SPAN)) {
9243 env->uncached_cpsr |= CPSR_PAN;
9244 }
9245 break;
9246 }
9247 }
dea8378b
PM
9248 /*
9249 * this is a lie, as there was no c1_sys on V4T/V5, but who cares
9250 * and we should just guard the thumb mode on V4
9251 */
9252 if (arm_feature(env, ARM_FEATURE_V4T)) {
9253 env->thumb =
9254 (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_TE) != 0;
9255 }
9256 env->regs[14] = env->regs[15] + offset;
9257 }
9258 env->regs[15] = newpc;
a8a79c7a 9259 arm_rebuild_hflags(env);
dea8378b
PM
9260}
9261
b9bc21ff
PM
9262static void arm_cpu_do_interrupt_aarch32_hyp(CPUState *cs)
9263{
9264 /*
9265 * Handle exception entry to Hyp mode; this is sufficiently
9266 * different to entry to other AArch32 modes that we handle it
9267 * separately here.
9268 *
9269 * The vector table entry used is always the 0x14 Hyp mode entry point,
9270 * unless this is an UNDEF/HVC/abort taken from Hyp to Hyp.
9271 * The offset applied to the preferred return address is always zero
9272 * (see DDI0487C.a section G1.12.3).
9273 * PSTATE A/I/F masks are set based only on the SCR.EA/IRQ/FIQ values.
9274 */
9275 uint32_t addr, mask;
9276 ARMCPU *cpu = ARM_CPU(cs);
9277 CPUARMState *env = &cpu->env;
9278
9279 switch (cs->exception_index) {
9280 case EXCP_UDEF:
9281 addr = 0x04;
9282 break;
9283 case EXCP_SWI:
9284 addr = 0x14;
9285 break;
9286 case EXCP_BKPT:
9287 /* Fall through to prefetch abort. */
9288 case EXCP_PREFETCH_ABORT:
9289 env->cp15.ifar_s = env->exception.vaddress;
9290 qemu_log_mask(CPU_LOG_INT, "...with HIFAR 0x%x\n",
9291 (uint32_t)env->exception.vaddress);
9292 addr = 0x0c;
9293 break;
9294 case EXCP_DATA_ABORT:
9295 env->cp15.dfar_s = env->exception.vaddress;
9296 qemu_log_mask(CPU_LOG_INT, "...with HDFAR 0x%x\n",
9297 (uint32_t)env->exception.vaddress);
9298 addr = 0x10;
9299 break;
9300 case EXCP_IRQ:
9301 addr = 0x18;
9302 break;
9303 case EXCP_FIQ:
9304 addr = 0x1c;
9305 break;
9306 case EXCP_HVC:
9307 addr = 0x08;
9308 break;
9309 case EXCP_HYP_TRAP:
9310 addr = 0x14;
9bbb4ef9 9311 break;
b9bc21ff
PM
9312 default:
9313 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
9314 }
9315
9316 if (cs->exception_index != EXCP_IRQ && cs->exception_index != EXCP_FIQ) {
2ed08180
PM
9317 if (!arm_feature(env, ARM_FEATURE_V8)) {
9318 /*
9319 * QEMU syndrome values are v8-style. v7 has the IL bit
9320 * UNK/SBZP for "field not valid" cases, where v8 uses RES1.
9321 * If this is a v7 CPU, squash the IL bit in those cases.
9322 */
9323 if (cs->exception_index == EXCP_PREFETCH_ABORT ||
9324 (cs->exception_index == EXCP_DATA_ABORT &&
9325 !(env->exception.syndrome & ARM_EL_ISV)) ||
9326 syn_get_ec(env->exception.syndrome) == EC_UNCATEGORIZED) {
9327 env->exception.syndrome &= ~ARM_EL_IL;
9328 }
9329 }
b9bc21ff
PM
9330 env->cp15.esr_el[2] = env->exception.syndrome;
9331 }
9332
9333 if (arm_current_el(env) != 2 && addr < 0x14) {
9334 addr = 0x14;
9335 }
9336
9337 mask = 0;
9338 if (!(env->cp15.scr_el3 & SCR_EA)) {
9339 mask |= CPSR_A;
9340 }
9341 if (!(env->cp15.scr_el3 & SCR_IRQ)) {
9342 mask |= CPSR_I;
9343 }
9344 if (!(env->cp15.scr_el3 & SCR_FIQ)) {
9345 mask |= CPSR_F;
9346 }
9347
9348 addr += env->cp15.hvbar;
9349
9350 take_aarch32_exception(env, ARM_CPU_MODE_HYP, mask, 0, addr);
9351}
9352
966f758c 9353static void arm_cpu_do_interrupt_aarch32(CPUState *cs)
b5ff1b31 9354{
97a8ea5a
AF
9355 ARMCPU *cpu = ARM_CPU(cs);
9356 CPUARMState *env = &cpu->env;
b5ff1b31
FB
9357 uint32_t addr;
9358 uint32_t mask;
9359 int new_mode;
9360 uint32_t offset;
16a906fd 9361 uint32_t moe;
b5ff1b31 9362
16a906fd 9363 /* If this is a debug exception we must update the DBGDSCR.MOE bits */
64b91e3f 9364 switch (syn_get_ec(env->exception.syndrome)) {
16a906fd
PM
9365 case EC_BREAKPOINT:
9366 case EC_BREAKPOINT_SAME_EL:
9367 moe = 1;
9368 break;
9369 case EC_WATCHPOINT:
9370 case EC_WATCHPOINT_SAME_EL:
9371 moe = 10;
9372 break;
9373 case EC_AA32_BKPT:
9374 moe = 3;
9375 break;
9376 case EC_VECTORCATCH:
9377 moe = 5;
9378 break;
9379 default:
9380 moe = 0;
9381 break;
9382 }
9383
9384 if (moe) {
9385 env->cp15.mdscr_el1 = deposit64(env->cp15.mdscr_el1, 2, 4, moe);
9386 }
9387
b9bc21ff
PM
9388 if (env->exception.target_el == 2) {
9389 arm_cpu_do_interrupt_aarch32_hyp(cs);
9390 return;
9391 }
9392
27103424 9393 switch (cs->exception_index) {
b5ff1b31
FB
9394 case EXCP_UDEF:
9395 new_mode = ARM_CPU_MODE_UND;
9396 addr = 0x04;
9397 mask = CPSR_I;
9398 if (env->thumb)
9399 offset = 2;
9400 else
9401 offset = 4;
9402 break;
9403 case EXCP_SWI:
9404 new_mode = ARM_CPU_MODE_SVC;
9405 addr = 0x08;
9406 mask = CPSR_I;
601d70b9 9407 /* The PC already points to the next instruction. */
b5ff1b31
FB
9408 offset = 0;
9409 break;
06c949e6 9410 case EXCP_BKPT:
9ee6e8bb
PB
9411 /* Fall through to prefetch abort. */
9412 case EXCP_PREFETCH_ABORT:
88ca1c2d 9413 A32_BANKED_CURRENT_REG_SET(env, ifsr, env->exception.fsr);
b848ce2b 9414 A32_BANKED_CURRENT_REG_SET(env, ifar, env->exception.vaddress);
3f1beaca 9415 qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x IFAR 0x%x\n",
88ca1c2d 9416 env->exception.fsr, (uint32_t)env->exception.vaddress);
b5ff1b31
FB
9417 new_mode = ARM_CPU_MODE_ABT;
9418 addr = 0x0c;
9419 mask = CPSR_A | CPSR_I;
9420 offset = 4;
9421 break;
9422 case EXCP_DATA_ABORT:
4a7e2d73 9423 A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr);
b848ce2b 9424 A32_BANKED_CURRENT_REG_SET(env, dfar, env->exception.vaddress);
3f1beaca 9425 qemu_log_mask(CPU_LOG_INT, "...with DFSR 0x%x DFAR 0x%x\n",
4a7e2d73 9426 env->exception.fsr,
6cd8a264 9427 (uint32_t)env->exception.vaddress);
b5ff1b31
FB
9428 new_mode = ARM_CPU_MODE_ABT;
9429 addr = 0x10;
9430 mask = CPSR_A | CPSR_I;
9431 offset = 8;
9432 break;
9433 case EXCP_IRQ:
9434 new_mode = ARM_CPU_MODE_IRQ;
9435 addr = 0x18;
9436 /* Disable IRQ and imprecise data aborts. */
9437 mask = CPSR_A | CPSR_I;
9438 offset = 4;
de38d23b
FA
9439 if (env->cp15.scr_el3 & SCR_IRQ) {
9440 /* IRQ routed to monitor mode */
9441 new_mode = ARM_CPU_MODE_MON;
9442 mask |= CPSR_F;
9443 }
b5ff1b31
FB
9444 break;
9445 case EXCP_FIQ:
9446 new_mode = ARM_CPU_MODE_FIQ;
9447 addr = 0x1c;
9448 /* Disable FIQ, IRQ and imprecise data aborts. */
9449 mask = CPSR_A | CPSR_I | CPSR_F;
de38d23b
FA
9450 if (env->cp15.scr_el3 & SCR_FIQ) {
9451 /* FIQ routed to monitor mode */
9452 new_mode = ARM_CPU_MODE_MON;
9453 }
b5ff1b31
FB
9454 offset = 4;
9455 break;
87a4b270
PM
9456 case EXCP_VIRQ:
9457 new_mode = ARM_CPU_MODE_IRQ;
9458 addr = 0x18;
9459 /* Disable IRQ and imprecise data aborts. */
9460 mask = CPSR_A | CPSR_I;
9461 offset = 4;
9462 break;
9463 case EXCP_VFIQ:
9464 new_mode = ARM_CPU_MODE_FIQ;
9465 addr = 0x1c;
9466 /* Disable FIQ, IRQ and imprecise data aborts. */
9467 mask = CPSR_A | CPSR_I | CPSR_F;
9468 offset = 4;
9469 break;
dbe9d163
FA
9470 case EXCP_SMC:
9471 new_mode = ARM_CPU_MODE_MON;
9472 addr = 0x08;
9473 mask = CPSR_A | CPSR_I | CPSR_F;
9474 offset = 0;
9475 break;
b5ff1b31 9476 default:
a47dddd7 9477 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
b5ff1b31
FB
9478 return; /* Never happens. Keep compiler happy. */
9479 }
e89e51a1
FA
9480
9481 if (new_mode == ARM_CPU_MODE_MON) {
9482 addr += env->cp15.mvbar;
137feaa9 9483 } else if (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_V) {
e89e51a1 9484 /* High vectors. When enabled, base address cannot be remapped. */
b5ff1b31 9485 addr += 0xffff0000;
8641136c
NR
9486 } else {
9487 /* ARM v7 architectures provide a vector base address register to remap
9488 * the interrupt vector table.
e89e51a1 9489 * This register is only followed in non-monitor mode, and is banked.
8641136c
NR
9490 * Note: only bits 31:5 are valid.
9491 */
fb6c91ba 9492 addr += A32_BANKED_CURRENT_REG_GET(env, vbar);
b5ff1b31 9493 }
dbe9d163
FA
9494
9495 if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON) {
9496 env->cp15.scr_el3 &= ~SCR_NS;
9497 }
9498
dea8378b 9499 take_aarch32_exception(env, new_mode, mask, offset, addr);
b5ff1b31
FB
9500}
9501
966f758c
PM
9502/* Handle exception entry to a target EL which is using AArch64 */
9503static void arm_cpu_do_interrupt_aarch64(CPUState *cs)
f3a9b694
PM
9504{
9505 ARMCPU *cpu = ARM_CPU(cs);
9506 CPUARMState *env = &cpu->env;
9507 unsigned int new_el = env->exception.target_el;
9508 target_ulong addr = env->cp15.vbar_el[new_el];
9509 unsigned int new_mode = aarch64_pstate_mode(new_el, true);
4a2696c0 9510 unsigned int old_mode;
0ab5953b
RH
9511 unsigned int cur_el = arm_current_el(env);
9512
9a05f7b6
RH
9513 /*
9514 * Note that new_el can never be 0. If cur_el is 0, then
9515 * el0_a64 is is_a64(), else el0_a64 is ignored.
9516 */
9517 aarch64_sve_change_el(env, cur_el, new_el, is_a64(env));
f3a9b694 9518
0ab5953b 9519 if (cur_el < new_el) {
3d6f7617
PM
9520 /* Entry vector offset depends on whether the implemented EL
9521 * immediately lower than the target level is using AArch32 or AArch64
9522 */
9523 bool is_aa64;
cb092fbb 9524 uint64_t hcr;
3d6f7617
PM
9525
9526 switch (new_el) {
9527 case 3:
9528 is_aa64 = (env->cp15.scr_el3 & SCR_RW) != 0;
9529 break;
9530 case 2:
cb092fbb
RH
9531 hcr = arm_hcr_el2_eff(env);
9532 if ((hcr & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
9533 is_aa64 = (hcr & HCR_RW) != 0;
9534 break;
9535 }
9536 /* fall through */
3d6f7617
PM
9537 case 1:
9538 is_aa64 = is_a64(env);
9539 break;
9540 default:
9541 g_assert_not_reached();
9542 }
9543
9544 if (is_aa64) {
f3a9b694
PM
9545 addr += 0x400;
9546 } else {
9547 addr += 0x600;
9548 }
9549 } else if (pstate_read(env) & PSTATE_SP) {
9550 addr += 0x200;
9551 }
9552
f3a9b694
PM
9553 switch (cs->exception_index) {
9554 case EXCP_PREFETCH_ABORT:
9555 case EXCP_DATA_ABORT:
9556 env->cp15.far_el[new_el] = env->exception.vaddress;
9557 qemu_log_mask(CPU_LOG_INT, "...with FAR 0x%" PRIx64 "\n",
9558 env->cp15.far_el[new_el]);
9559 /* fall through */
9560 case EXCP_BKPT:
9561 case EXCP_UDEF:
9562 case EXCP_SWI:
9563 case EXCP_HVC:
9564 case EXCP_HYP_TRAP:
9565 case EXCP_SMC:
4be42f40
PM
9566 if (syn_get_ec(env->exception.syndrome) == EC_ADVSIMDFPACCESSTRAP) {
9567 /*
9568 * QEMU internal FP/SIMD syndromes from AArch32 include the
9569 * TA and coproc fields which are only exposed if the exception
9570 * is taken to AArch32 Hyp mode. Mask them out to get a valid
9571 * AArch64 format syndrome.
9572 */
9573 env->exception.syndrome &= ~MAKE_64BIT_MASK(0, 20);
9574 }
f3a9b694
PM
9575 env->cp15.esr_el[new_el] = env->exception.syndrome;
9576 break;
9577 case EXCP_IRQ:
9578 case EXCP_VIRQ:
9579 addr += 0x80;
9580 break;
9581 case EXCP_FIQ:
9582 case EXCP_VFIQ:
9583 addr += 0x100;
9584 break;
f3a9b694
PM
9585 default:
9586 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
9587 }
9588
9589 if (is_a64(env)) {
4a2696c0 9590 old_mode = pstate_read(env);
f3a9b694
PM
9591 aarch64_save_sp(env, arm_current_el(env));
9592 env->elr_el[new_el] = env->pc;
9593 } else {
4a2696c0 9594 old_mode = cpsr_read(env);
f3a9b694
PM
9595 env->elr_el[new_el] = env->regs[15];
9596
9597 aarch64_sync_32_to_64(env);
9598
9599 env->condexec_bits = 0;
9600 }
4a2696c0
RH
9601 env->banked_spsr[aarch64_banked_spsr_index(new_el)] = old_mode;
9602
f3a9b694
PM
9603 qemu_log_mask(CPU_LOG_INT, "...with ELR 0x%" PRIx64 "\n",
9604 env->elr_el[new_el]);
9605
4a2696c0
RH
9606 if (cpu_isar_feature(aa64_pan, cpu)) {
9607 /* The value of PSTATE.PAN is normally preserved, except when ... */
9608 new_mode |= old_mode & PSTATE_PAN;
9609 switch (new_el) {
9610 case 2:
9611 /* ... the target is EL2 with HCR_EL2.{E2H,TGE} == '11' ... */
9612 if ((arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE))
9613 != (HCR_E2H | HCR_TGE)) {
9614 break;
9615 }
9616 /* fall through */
9617 case 1:
9618 /* ... the target is EL1 ... */
9619 /* ... and SCTLR_ELx.SPAN == 0, then set to 1. */
9620 if ((env->cp15.sctlr_el[new_el] & SCTLR_SPAN) == 0) {
9621 new_mode |= PSTATE_PAN;
9622 }
9623 break;
9624 }
9625 }
9626
f3a9b694
PM
9627 pstate_write(env, PSTATE_DAIF | new_mode);
9628 env->aarch64 = 1;
9629 aarch64_restore_sp(env, new_el);
a8a79c7a 9630 helper_rebuild_hflags_a64(env, new_el);
f3a9b694
PM
9631
9632 env->pc = addr;
9633
9634 qemu_log_mask(CPU_LOG_INT, "...to EL%d PC 0x%" PRIx64 " PSTATE 0x%x\n",
9635 new_el, env->pc, pstate_read(env));
966f758c
PM
9636}
9637
ed6e6ba9
AB
9638/*
9639 * Do semihosting call and set the appropriate return value. All the
9640 * permission and validity checks have been done at translate time.
9641 *
9642 * We only see semihosting exceptions in TCG only as they are not
9643 * trapped to the hypervisor in KVM.
9644 */
91f78c58 9645#ifdef CONFIG_TCG
ed6e6ba9
AB
9646static void handle_semihosting(CPUState *cs)
9647{
904c04de
PM
9648 ARMCPU *cpu = ARM_CPU(cs);
9649 CPUARMState *env = &cpu->env;
9650
9651 if (is_a64(env)) {
ed6e6ba9
AB
9652 qemu_log_mask(CPU_LOG_INT,
9653 "...handling as semihosting call 0x%" PRIx64 "\n",
9654 env->xregs[0]);
9655 env->xregs[0] = do_arm_semihosting(env);
4ff5ef9e 9656 env->pc += 4;
904c04de 9657 } else {
904c04de
PM
9658 qemu_log_mask(CPU_LOG_INT,
9659 "...handling as semihosting call 0x%x\n",
9660 env->regs[0]);
9661 env->regs[0] = do_arm_semihosting(env);
4ff5ef9e 9662 env->regs[15] += env->thumb ? 2 : 4;
904c04de
PM
9663 }
9664}
ed6e6ba9 9665#endif
904c04de 9666
966f758c
PM
9667/* Handle a CPU exception for A and R profile CPUs.
9668 * Do any appropriate logging, handle PSCI calls, and then hand off
9669 * to the AArch64-entry or AArch32-entry function depending on the
9670 * target exception level's register width.
9671 */
9672void arm_cpu_do_interrupt(CPUState *cs)
9673{
9674 ARMCPU *cpu = ARM_CPU(cs);
9675 CPUARMState *env = &cpu->env;
9676 unsigned int new_el = env->exception.target_el;
9677
531c60a9 9678 assert(!arm_feature(env, ARM_FEATURE_M));
966f758c
PM
9679
9680 arm_log_exception(cs->exception_index);
9681 qemu_log_mask(CPU_LOG_INT, "...from EL%d to EL%d\n", arm_current_el(env),
9682 new_el);
9683 if (qemu_loglevel_mask(CPU_LOG_INT)
9684 && !excp_is_internal(cs->exception_index)) {
6568da45 9685 qemu_log_mask(CPU_LOG_INT, "...with ESR 0x%x/0x%" PRIx32 "\n",
64b91e3f 9686 syn_get_ec(env->exception.syndrome),
966f758c
PM
9687 env->exception.syndrome);
9688 }
9689
9690 if (arm_is_psci_call(cpu, cs->exception_index)) {
9691 arm_handle_psci_call(cpu);
9692 qemu_log_mask(CPU_LOG_INT, "...handled as PSCI call\n");
9693 return;
9694 }
9695
ed6e6ba9
AB
9696 /*
9697 * Semihosting semantics depend on the register width of the code
9698 * that caused the exception, not the target exception level, so
9699 * must be handled here.
966f758c 9700 */
ed6e6ba9
AB
9701#ifdef CONFIG_TCG
9702 if (cs->exception_index == EXCP_SEMIHOST) {
9703 handle_semihosting(cs);
904c04de
PM
9704 return;
9705 }
ed6e6ba9 9706#endif
904c04de 9707
b5c53d1b
AL
9708 /* Hooks may change global state so BQL should be held, also the
9709 * BQL needs to be held for any modification of
9710 * cs->interrupt_request.
9711 */
9712 g_assert(qemu_mutex_iothread_locked());
9713
9714 arm_call_pre_el_change_hook(cpu);
9715
904c04de
PM
9716 assert(!excp_is_internal(cs->exception_index));
9717 if (arm_el_is_aa64(env, new_el)) {
966f758c
PM
9718 arm_cpu_do_interrupt_aarch64(cs);
9719 } else {
9720 arm_cpu_do_interrupt_aarch32(cs);
9721 }
f3a9b694 9722
bd7d00fc
PM
9723 arm_call_el_change_hook(cpu);
9724
f3a9b694
PM
9725 if (!kvm_enabled()) {
9726 cs->interrupt_request |= CPU_INTERRUPT_EXITTB;
9727 }
9728}
c47eaf9f 9729#endif /* !CONFIG_USER_ONLY */
0480f69a
PM
9730
9731/* Return the exception level which controls this address translation regime */
b9f6033c 9732static uint32_t regime_el(CPUARMState *env, ARMMMUIdx mmu_idx)
0480f69a
PM
9733{
9734 switch (mmu_idx) {
b9f6033c
RH
9735 case ARMMMUIdx_E20_0:
9736 case ARMMMUIdx_E20_2:
452ef8cb 9737 case ARMMMUIdx_E20_2_PAN:
97fa9350 9738 case ARMMMUIdx_Stage2:
e013b741 9739 case ARMMMUIdx_E2:
0480f69a 9740 return 2;
127b2b08 9741 case ARMMMUIdx_SE3:
0480f69a 9742 return 3;
fba37aed 9743 case ARMMMUIdx_SE10_0:
0480f69a 9744 return arm_el_is_aa64(env, 3) ? 1 : 3;
fba37aed 9745 case ARMMMUIdx_SE10_1:
452ef8cb 9746 case ARMMMUIdx_SE10_1_PAN:
2859d7b5
RH
9747 case ARMMMUIdx_Stage1_E0:
9748 case ARMMMUIdx_Stage1_E1:
452ef8cb 9749 case ARMMMUIdx_Stage1_E1_PAN:
b9f6033c
RH
9750 case ARMMMUIdx_E10_0:
9751 case ARMMMUIdx_E10_1:
452ef8cb 9752 case ARMMMUIdx_E10_1_PAN:
62593718
PM
9753 case ARMMMUIdx_MPrivNegPri:
9754 case ARMMMUIdx_MUserNegPri:
e7b921c2
PM
9755 case ARMMMUIdx_MPriv:
9756 case ARMMMUIdx_MUser:
62593718
PM
9757 case ARMMMUIdx_MSPrivNegPri:
9758 case ARMMMUIdx_MSUserNegPri:
66787c78 9759 case ARMMMUIdx_MSPriv:
66787c78 9760 case ARMMMUIdx_MSUser:
0480f69a
PM
9761 return 1;
9762 default:
9763 g_assert_not_reached();
9764 }
9765}
9766
aaec1432
RH
9767uint64_t arm_sctlr(CPUARMState *env, int el)
9768{
9769 /* Only EL0 needs to be adjusted for EL1&0 or EL2&0. */
9770 if (el == 0) {
9771 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, 0);
9772 el = (mmu_idx == ARMMMUIdx_E20_0 ? 2 : 1);
9773 }
9774 return env->cp15.sctlr_el[el];
9775}
c47eaf9f 9776
0480f69a 9777/* Return the SCTLR value which controls this address translation regime */
aaec1432 9778static inline uint64_t regime_sctlr(CPUARMState *env, ARMMMUIdx mmu_idx)
0480f69a
PM
9779{
9780 return env->cp15.sctlr_el[regime_el(env, mmu_idx)];
9781}
9782
aaec1432
RH
9783#ifndef CONFIG_USER_ONLY
9784
0480f69a
PM
9785/* Return true if the specified stage of address translation is disabled */
9786static inline bool regime_translation_disabled(CPUARMState *env,
9787 ARMMMUIdx mmu_idx)
9788{
29c483a5 9789 if (arm_feature(env, ARM_FEATURE_M)) {
ecf5e8ea 9790 switch (env->v7m.mpu_ctrl[regime_is_secure(env, mmu_idx)] &
3bef7012
PM
9791 (R_V7M_MPU_CTRL_ENABLE_MASK | R_V7M_MPU_CTRL_HFNMIENA_MASK)) {
9792 case R_V7M_MPU_CTRL_ENABLE_MASK:
9793 /* Enabled, but not for HardFault and NMI */
62593718 9794 return mmu_idx & ARM_MMU_IDX_M_NEGPRI;
3bef7012
PM
9795 case R_V7M_MPU_CTRL_ENABLE_MASK | R_V7M_MPU_CTRL_HFNMIENA_MASK:
9796 /* Enabled for all cases */
9797 return false;
9798 case 0:
9799 default:
9800 /* HFNMIENA set and ENABLE clear is UNPREDICTABLE, but
9801 * we warned about that in armv7m_nvic.c when the guest set it.
9802 */
9803 return true;
9804 }
29c483a5
MD
9805 }
9806
97fa9350 9807 if (mmu_idx == ARMMMUIdx_Stage2) {
9d1bab33
PM
9808 /* HCR.DC means HCR.VM behaves as 1 */
9809 return (env->cp15.hcr_el2 & (HCR_DC | HCR_VM)) == 0;
0480f69a 9810 }
3d0e3080
PM
9811
9812 if (env->cp15.hcr_el2 & HCR_TGE) {
9813 /* TGE means that NS EL0/1 act as if SCTLR_EL1.M is zero */
9814 if (!regime_is_secure(env, mmu_idx) && regime_el(env, mmu_idx) == 1) {
9815 return true;
9816 }
9817 }
9818
fee7aa46 9819 if ((env->cp15.hcr_el2 & HCR_DC) && arm_mmu_idx_is_stage1_of_2(mmu_idx)) {
9d1bab33
PM
9820 /* HCR.DC means SCTLR_EL1.M behaves as 0 */
9821 return true;
9822 }
9823
0480f69a
PM
9824 return (regime_sctlr(env, mmu_idx) & SCTLR_M) == 0;
9825}
9826
73462ddd
PC
9827static inline bool regime_translation_big_endian(CPUARMState *env,
9828 ARMMMUIdx mmu_idx)
9829{
9830 return (regime_sctlr(env, mmu_idx) & SCTLR_EE) != 0;
9831}
9832
c47eaf9f
PM
9833/* Return the TTBR associated with this translation regime */
9834static inline uint64_t regime_ttbr(CPUARMState *env, ARMMMUIdx mmu_idx,
9835 int ttbrn)
9836{
97fa9350 9837 if (mmu_idx == ARMMMUIdx_Stage2) {
c47eaf9f
PM
9838 return env->cp15.vttbr_el2;
9839 }
9840 if (ttbrn == 0) {
9841 return env->cp15.ttbr0_el[regime_el(env, mmu_idx)];
9842 } else {
9843 return env->cp15.ttbr1_el[regime_el(env, mmu_idx)];
9844 }
9845}
9846
9847#endif /* !CONFIG_USER_ONLY */
9848
0480f69a
PM
9849/* Return the TCR controlling this translation regime */
9850static inline TCR *regime_tcr(CPUARMState *env, ARMMMUIdx mmu_idx)
9851{
97fa9350 9852 if (mmu_idx == ARMMMUIdx_Stage2) {
68e9c2fe 9853 return &env->cp15.vtcr_el2;
0480f69a
PM
9854 }
9855 return &env->cp15.tcr_el[regime_el(env, mmu_idx)];
9856}
9857
8bd5c820
PM
9858/* Convert a possible stage1+2 MMU index into the appropriate
9859 * stage 1 MMU index
9860 */
9861static inline ARMMMUIdx stage_1_mmu_idx(ARMMMUIdx mmu_idx)
9862{
b9f6033c
RH
9863 switch (mmu_idx) {
9864 case ARMMMUIdx_E10_0:
9865 return ARMMMUIdx_Stage1_E0;
9866 case ARMMMUIdx_E10_1:
9867 return ARMMMUIdx_Stage1_E1;
452ef8cb
RH
9868 case ARMMMUIdx_E10_1_PAN:
9869 return ARMMMUIdx_Stage1_E1_PAN;
b9f6033c
RH
9870 default:
9871 return mmu_idx;
8bd5c820 9872 }
8bd5c820
PM
9873}
9874
0480f69a
PM
9875/* Return true if the translation regime is using LPAE format page tables */
9876static inline bool regime_using_lpae_format(CPUARMState *env,
9877 ARMMMUIdx mmu_idx)
9878{
9879 int el = regime_el(env, mmu_idx);
9880 if (el == 2 || arm_el_is_aa64(env, el)) {
9881 return true;
9882 }
9883 if (arm_feature(env, ARM_FEATURE_LPAE)
9884 && (regime_tcr(env, mmu_idx)->raw_tcr & TTBCR_EAE)) {
9885 return true;
9886 }
9887 return false;
9888}
9889
deb2db99
AR
9890/* Returns true if the stage 1 translation regime is using LPAE format page
9891 * tables. Used when raising alignment exceptions, whose FSR changes depending
9892 * on whether the long or short descriptor format is in use. */
9893bool arm_s1_regime_using_lpae_format(CPUARMState *env, ARMMMUIdx mmu_idx)
30901475 9894{
8bd5c820 9895 mmu_idx = stage_1_mmu_idx(mmu_idx);
deb2db99 9896
30901475
AB
9897 return regime_using_lpae_format(env, mmu_idx);
9898}
9899
c47eaf9f 9900#ifndef CONFIG_USER_ONLY
0480f69a
PM
9901static inline bool regime_is_user(CPUARMState *env, ARMMMUIdx mmu_idx)
9902{
9903 switch (mmu_idx) {
fba37aed 9904 case ARMMMUIdx_SE10_0:
b9f6033c 9905 case ARMMMUIdx_E20_0:
2859d7b5 9906 case ARMMMUIdx_Stage1_E0:
e7b921c2 9907 case ARMMMUIdx_MUser:
871bec7c 9908 case ARMMMUIdx_MSUser:
62593718
PM
9909 case ARMMMUIdx_MUserNegPri:
9910 case ARMMMUIdx_MSUserNegPri:
0480f69a
PM
9911 return true;
9912 default:
9913 return false;
01b98b68
RH
9914 case ARMMMUIdx_E10_0:
9915 case ARMMMUIdx_E10_1:
452ef8cb 9916 case ARMMMUIdx_E10_1_PAN:
0480f69a
PM
9917 g_assert_not_reached();
9918 }
9919}
9920
0fbf5238
AJ
9921/* Translate section/page access permissions to page
9922 * R/W protection flags
d76951b6
AJ
9923 *
9924 * @env: CPUARMState
9925 * @mmu_idx: MMU index indicating required translation regime
9926 * @ap: The 3-bit access permissions (AP[2:0])
9927 * @domain_prot: The 2-bit domain access permissions
0fbf5238
AJ
9928 */
9929static inline int ap_to_rw_prot(CPUARMState *env, ARMMMUIdx mmu_idx,
9930 int ap, int domain_prot)
9931{
554b0b09
PM
9932 bool is_user = regime_is_user(env, mmu_idx);
9933
9934 if (domain_prot == 3) {
9935 return PAGE_READ | PAGE_WRITE;
9936 }
9937
554b0b09
PM
9938 switch (ap) {
9939 case 0:
9940 if (arm_feature(env, ARM_FEATURE_V7)) {
9941 return 0;
9942 }
554b0b09
PM
9943 switch (regime_sctlr(env, mmu_idx) & (SCTLR_S | SCTLR_R)) {
9944 case SCTLR_S:
9945 return is_user ? 0 : PAGE_READ;
9946 case SCTLR_R:
9947 return PAGE_READ;
9948 default:
9949 return 0;
9950 }
9951 case 1:
9952 return is_user ? 0 : PAGE_READ | PAGE_WRITE;
9953 case 2:
87c3d486 9954 if (is_user) {
0fbf5238 9955 return PAGE_READ;
87c3d486 9956 } else {
554b0b09 9957 return PAGE_READ | PAGE_WRITE;
87c3d486 9958 }
554b0b09
PM
9959 case 3:
9960 return PAGE_READ | PAGE_WRITE;
9961 case 4: /* Reserved. */
9962 return 0;
9963 case 5:
0fbf5238 9964 return is_user ? 0 : PAGE_READ;
554b0b09 9965 case 6:
0fbf5238 9966 return PAGE_READ;
554b0b09 9967 case 7:
87c3d486 9968 if (!arm_feature(env, ARM_FEATURE_V6K)) {
554b0b09 9969 return 0;
87c3d486 9970 }
0fbf5238 9971 return PAGE_READ;
554b0b09 9972 default:
0fbf5238 9973 g_assert_not_reached();
554b0b09 9974 }
b5ff1b31
FB
9975}
9976
d76951b6
AJ
9977/* Translate section/page access permissions to page
9978 * R/W protection flags.
9979 *
d76951b6 9980 * @ap: The 2-bit simple AP (AP[2:1])
d8e052b3 9981 * @is_user: TRUE if accessing from PL0
d76951b6 9982 */
d8e052b3 9983static inline int simple_ap_to_rw_prot_is_user(int ap, bool is_user)
d76951b6 9984{
d76951b6
AJ
9985 switch (ap) {
9986 case 0:
9987 return is_user ? 0 : PAGE_READ | PAGE_WRITE;
9988 case 1:
9989 return PAGE_READ | PAGE_WRITE;
9990 case 2:
9991 return is_user ? 0 : PAGE_READ;
9992 case 3:
9993 return PAGE_READ;
9994 default:
9995 g_assert_not_reached();
9996 }
9997}
9998
d8e052b3
AJ
9999static inline int
10000simple_ap_to_rw_prot(CPUARMState *env, ARMMMUIdx mmu_idx, int ap)
10001{
10002 return simple_ap_to_rw_prot_is_user(ap, regime_is_user(env, mmu_idx));
10003}
10004
6ab1a5ee
EI
10005/* Translate S2 section/page access permissions to protection flags
10006 *
10007 * @env: CPUARMState
10008 * @s2ap: The 2-bit stage2 access permissions (S2AP)
ce3125be
PM
10009 * @xn: XN (execute-never) bits
10010 * @s1_is_el0: true if this is S2 of an S1+2 walk for EL0
6ab1a5ee 10011 */
ce3125be 10012static int get_S2prot(CPUARMState *env, int s2ap, int xn, bool s1_is_el0)
6ab1a5ee
EI
10013{
10014 int prot = 0;
10015
10016 if (s2ap & 1) {
10017 prot |= PAGE_READ;
10018 }
10019 if (s2ap & 2) {
10020 prot |= PAGE_WRITE;
10021 }
ce3125be
PM
10022
10023 if (cpu_isar_feature(any_tts2uxn, env_archcpu(env))) {
10024 switch (xn) {
10025 case 0:
dfda6837 10026 prot |= PAGE_EXEC;
ce3125be
PM
10027 break;
10028 case 1:
10029 if (s1_is_el0) {
10030 prot |= PAGE_EXEC;
10031 }
10032 break;
10033 case 2:
10034 break;
10035 case 3:
10036 if (!s1_is_el0) {
10037 prot |= PAGE_EXEC;
10038 }
10039 break;
10040 default:
10041 g_assert_not_reached();
10042 }
10043 } else {
10044 if (!extract32(xn, 1, 1)) {
10045 if (arm_el_is_aa64(env, 2) || prot & PAGE_READ) {
10046 prot |= PAGE_EXEC;
10047 }
dfda6837 10048 }
6ab1a5ee
EI
10049 }
10050 return prot;
10051}
10052
d8e052b3
AJ
10053/* Translate section/page access permissions to protection flags
10054 *
10055 * @env: CPUARMState
10056 * @mmu_idx: MMU index indicating required translation regime
10057 * @is_aa64: TRUE if AArch64
10058 * @ap: The 2-bit simple AP (AP[2:1])
10059 * @ns: NS (non-secure) bit
10060 * @xn: XN (execute-never) bit
10061 * @pxn: PXN (privileged execute-never) bit
10062 */
10063static int get_S1prot(CPUARMState *env, ARMMMUIdx mmu_idx, bool is_aa64,
10064 int ap, int ns, int xn, int pxn)
10065{
10066 bool is_user = regime_is_user(env, mmu_idx);
10067 int prot_rw, user_rw;
10068 bool have_wxn;
10069 int wxn = 0;
10070
97fa9350 10071 assert(mmu_idx != ARMMMUIdx_Stage2);
d8e052b3
AJ
10072
10073 user_rw = simple_ap_to_rw_prot_is_user(ap, true);
10074 if (is_user) {
10075 prot_rw = user_rw;
10076 } else {
81636b70 10077 if (user_rw && regime_is_pan(env, mmu_idx)) {
f4e1dbc5
PM
10078 /* PAN forbids data accesses but doesn't affect insn fetch */
10079 prot_rw = 0;
10080 } else {
10081 prot_rw = simple_ap_to_rw_prot_is_user(ap, false);
81636b70 10082 }
d8e052b3
AJ
10083 }
10084
10085 if (ns && arm_is_secure(env) && (env->cp15.scr_el3 & SCR_SIF)) {
10086 return prot_rw;
10087 }
10088
10089 /* TODO have_wxn should be replaced with
10090 * ARM_FEATURE_V8 || (ARM_FEATURE_V7 && ARM_FEATURE_EL2)
10091 * when ARM_FEATURE_EL2 starts getting set. For now we assume all LPAE
10092 * compatible processors have EL2, which is required for [U]WXN.
10093 */
10094 have_wxn = arm_feature(env, ARM_FEATURE_LPAE);
10095
10096 if (have_wxn) {
10097 wxn = regime_sctlr(env, mmu_idx) & SCTLR_WXN;
10098 }
10099
10100 if (is_aa64) {
339370b9
RH
10101 if (regime_has_2_ranges(mmu_idx) && !is_user) {
10102 xn = pxn || (user_rw & PAGE_WRITE);
d8e052b3
AJ
10103 }
10104 } else if (arm_feature(env, ARM_FEATURE_V7)) {
10105 switch (regime_el(env, mmu_idx)) {
10106 case 1:
10107 case 3:
10108 if (is_user) {
10109 xn = xn || !(user_rw & PAGE_READ);
10110 } else {
10111 int uwxn = 0;
10112 if (have_wxn) {
10113 uwxn = regime_sctlr(env, mmu_idx) & SCTLR_UWXN;
10114 }
10115 xn = xn || !(prot_rw & PAGE_READ) || pxn ||
10116 (uwxn && (user_rw & PAGE_WRITE));
10117 }
10118 break;
10119 case 2:
10120 break;
10121 }
10122 } else {
10123 xn = wxn = 0;
10124 }
10125
10126 if (xn || (wxn && (prot_rw & PAGE_WRITE))) {
10127 return prot_rw;
10128 }
10129 return prot_rw | PAGE_EXEC;
10130}
10131
0480f69a
PM
10132static bool get_level1_table_address(CPUARMState *env, ARMMMUIdx mmu_idx,
10133 uint32_t *table, uint32_t address)
b2fa1797 10134{
0480f69a 10135 /* Note that we can only get here for an AArch32 PL0/PL1 lookup */
0480f69a 10136 TCR *tcr = regime_tcr(env, mmu_idx);
11f136ee 10137
11f136ee
FA
10138 if (address & tcr->mask) {
10139 if (tcr->raw_tcr & TTBCR_PD1) {
e389be16
FA
10140 /* Translation table walk disabled for TTBR1 */
10141 return false;
10142 }
aef878be 10143 *table = regime_ttbr(env, mmu_idx, 1) & 0xffffc000;
e389be16 10144 } else {
11f136ee 10145 if (tcr->raw_tcr & TTBCR_PD0) {
e389be16
FA
10146 /* Translation table walk disabled for TTBR0 */
10147 return false;
10148 }
aef878be 10149 *table = regime_ttbr(env, mmu_idx, 0) & tcr->base_mask;
e389be16
FA
10150 }
10151 *table |= (address >> 18) & 0x3ffc;
10152 return true;
b2fa1797
PB
10153}
10154
37785977
EI
10155/* Translate a S1 pagetable walk through S2 if needed. */
10156static hwaddr S1_ptw_translate(CPUARMState *env, ARMMMUIdx mmu_idx,
10157 hwaddr addr, MemTxAttrs txattrs,
37785977
EI
10158 ARMMMUFaultInfo *fi)
10159{
fee7aa46 10160 if (arm_mmu_idx_is_stage1_of_2(mmu_idx) &&
97fa9350 10161 !regime_translation_disabled(env, ARMMMUIdx_Stage2)) {
37785977
EI
10162 target_ulong s2size;
10163 hwaddr s2pa;
10164 int s2prot;
10165 int ret;
eadb2feb
PM
10166 ARMCacheAttrs cacheattrs = {};
10167 ARMCacheAttrs *pcacheattrs = NULL;
10168
10169 if (env->cp15.hcr_el2 & HCR_PTW) {
10170 /*
10171 * PTW means we must fault if this S1 walk touches S2 Device
10172 * memory; otherwise we don't care about the attributes and can
10173 * save the S2 translation the effort of computing them.
10174 */
10175 pcacheattrs = &cacheattrs;
10176 }
37785977 10177
59dff859 10178 ret = get_phys_addr_lpae(env, addr, MMU_DATA_LOAD, ARMMMUIdx_Stage2,
ff7de2fc 10179 false,
59dff859
PM
10180 &s2pa, &txattrs, &s2prot, &s2size, fi,
10181 pcacheattrs);
37785977 10182 if (ret) {
3b39d734 10183 assert(fi->type != ARMFault_None);
37785977
EI
10184 fi->s2addr = addr;
10185 fi->stage2 = true;
10186 fi->s1ptw = true;
10187 return ~0;
10188 }
eadb2feb
PM
10189 if (pcacheattrs && (pcacheattrs->attrs & 0xf0) == 0) {
10190 /* Access was to Device memory: generate Permission fault */
10191 fi->type = ARMFault_Permission;
10192 fi->s2addr = addr;
10193 fi->stage2 = true;
10194 fi->s1ptw = true;
10195 return ~0;
10196 }
37785977
EI
10197 addr = s2pa;
10198 }
10199 return addr;
10200}
10201
14577270 10202/* All loads done in the course of a page table walk go through here. */
a614e698 10203static uint32_t arm_ldl_ptw(CPUState *cs, hwaddr addr, bool is_secure,
3795a6de 10204 ARMMMUIdx mmu_idx, ARMMMUFaultInfo *fi)
ebca90e4 10205{
a614e698
EI
10206 ARMCPU *cpu = ARM_CPU(cs);
10207 CPUARMState *env = &cpu->env;
ebca90e4 10208 MemTxAttrs attrs = {};
3b39d734 10209 MemTxResult result = MEMTX_OK;
5ce4ff65 10210 AddressSpace *as;
3b39d734 10211 uint32_t data;
ebca90e4
PM
10212
10213 attrs.secure = is_secure;
5ce4ff65 10214 as = arm_addressspace(cs, attrs);
3795a6de 10215 addr = S1_ptw_translate(env, mmu_idx, addr, attrs, fi);
a614e698
EI
10216 if (fi->s1ptw) {
10217 return 0;
10218 }
73462ddd 10219 if (regime_translation_big_endian(env, mmu_idx)) {
3b39d734 10220 data = address_space_ldl_be(as, addr, attrs, &result);
73462ddd 10221 } else {
3b39d734 10222 data = address_space_ldl_le(as, addr, attrs, &result);
73462ddd 10223 }
3b39d734
PM
10224 if (result == MEMTX_OK) {
10225 return data;
10226 }
10227 fi->type = ARMFault_SyncExternalOnWalk;
10228 fi->ea = arm_extabort_type(result);
10229 return 0;
ebca90e4
PM
10230}
10231
37785977 10232static uint64_t arm_ldq_ptw(CPUState *cs, hwaddr addr, bool is_secure,
3795a6de 10233 ARMMMUIdx mmu_idx, ARMMMUFaultInfo *fi)
ebca90e4 10234{
37785977
EI
10235 ARMCPU *cpu = ARM_CPU(cs);
10236 CPUARMState *env = &cpu->env;
ebca90e4 10237 MemTxAttrs attrs = {};
3b39d734 10238 MemTxResult result = MEMTX_OK;
5ce4ff65 10239 AddressSpace *as;
9aea1ea3 10240 uint64_t data;
ebca90e4
PM
10241
10242 attrs.secure = is_secure;
5ce4ff65 10243 as = arm_addressspace(cs, attrs);
3795a6de 10244 addr = S1_ptw_translate(env, mmu_idx, addr, attrs, fi);
37785977
EI
10245 if (fi->s1ptw) {
10246 return 0;
10247 }
73462ddd 10248 if (regime_translation_big_endian(env, mmu_idx)) {
3b39d734 10249 data = address_space_ldq_be(as, addr, attrs, &result);
73462ddd 10250 } else {
3b39d734
PM
10251 data = address_space_ldq_le(as, addr, attrs, &result);
10252 }
10253 if (result == MEMTX_OK) {
10254 return data;
73462ddd 10255 }
3b39d734
PM
10256 fi->type = ARMFault_SyncExternalOnWalk;
10257 fi->ea = arm_extabort_type(result);
10258 return 0;
ebca90e4
PM
10259}
10260
b7cc4e82 10261static bool get_phys_addr_v5(CPUARMState *env, uint32_t address,
03ae85f8 10262 MMUAccessType access_type, ARMMMUIdx mmu_idx,
b7cc4e82 10263 hwaddr *phys_ptr, int *prot,
f989983e 10264 target_ulong *page_size,
e14b5a23 10265 ARMMMUFaultInfo *fi)
b5ff1b31 10266{
2fc0cc0e 10267 CPUState *cs = env_cpu(env);
f989983e 10268 int level = 1;
b5ff1b31
FB
10269 uint32_t table;
10270 uint32_t desc;
10271 int type;
10272 int ap;
e389be16 10273 int domain = 0;
dd4ebc2e 10274 int domain_prot;
a8170e5e 10275 hwaddr phys_addr;
0480f69a 10276 uint32_t dacr;
b5ff1b31 10277
9ee6e8bb
PB
10278 /* Pagetable walk. */
10279 /* Lookup l1 descriptor. */
0480f69a 10280 if (!get_level1_table_address(env, mmu_idx, &table, address)) {
e389be16 10281 /* Section translation fault if page walk is disabled by PD0 or PD1 */
f989983e 10282 fi->type = ARMFault_Translation;
e389be16
FA
10283 goto do_fault;
10284 }
a614e698 10285 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
3795a6de 10286 mmu_idx, fi);
3b39d734
PM
10287 if (fi->type != ARMFault_None) {
10288 goto do_fault;
10289 }
9ee6e8bb 10290 type = (desc & 3);
dd4ebc2e 10291 domain = (desc >> 5) & 0x0f;
0480f69a
PM
10292 if (regime_el(env, mmu_idx) == 1) {
10293 dacr = env->cp15.dacr_ns;
10294 } else {
10295 dacr = env->cp15.dacr_s;
10296 }
10297 domain_prot = (dacr >> (domain * 2)) & 3;
9ee6e8bb 10298 if (type == 0) {
601d70b9 10299 /* Section translation fault. */
f989983e 10300 fi->type = ARMFault_Translation;
9ee6e8bb
PB
10301 goto do_fault;
10302 }
f989983e
PM
10303 if (type != 2) {
10304 level = 2;
10305 }
dd4ebc2e 10306 if (domain_prot == 0 || domain_prot == 2) {
f989983e 10307 fi->type = ARMFault_Domain;
9ee6e8bb
PB
10308 goto do_fault;
10309 }
10310 if (type == 2) {
10311 /* 1Mb section. */
10312 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
10313 ap = (desc >> 10) & 3;
d4c430a8 10314 *page_size = 1024 * 1024;
9ee6e8bb
PB
10315 } else {
10316 /* Lookup l2 entry. */
554b0b09
PM
10317 if (type == 1) {
10318 /* Coarse pagetable. */
10319 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
10320 } else {
10321 /* Fine pagetable. */
10322 table = (desc & 0xfffff000) | ((address >> 8) & 0xffc);
10323 }
a614e698 10324 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
3795a6de 10325 mmu_idx, fi);
3b39d734
PM
10326 if (fi->type != ARMFault_None) {
10327 goto do_fault;
10328 }
9ee6e8bb
PB
10329 switch (desc & 3) {
10330 case 0: /* Page translation fault. */
f989983e 10331 fi->type = ARMFault_Translation;
9ee6e8bb
PB
10332 goto do_fault;
10333 case 1: /* 64k page. */
10334 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
10335 ap = (desc >> (4 + ((address >> 13) & 6))) & 3;
d4c430a8 10336 *page_size = 0x10000;
ce819861 10337 break;
9ee6e8bb
PB
10338 case 2: /* 4k page. */
10339 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
c10f7fc3 10340 ap = (desc >> (4 + ((address >> 9) & 6))) & 3;
d4c430a8 10341 *page_size = 0x1000;
ce819861 10342 break;
fc1891c7 10343 case 3: /* 1k page, or ARMv6/XScale "extended small (4k) page" */
554b0b09 10344 if (type == 1) {
fc1891c7
PM
10345 /* ARMv6/XScale extended small page format */
10346 if (arm_feature(env, ARM_FEATURE_XSCALE)
10347 || arm_feature(env, ARM_FEATURE_V6)) {
554b0b09 10348 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
fc1891c7 10349 *page_size = 0x1000;
554b0b09 10350 } else {
fc1891c7
PM
10351 /* UNPREDICTABLE in ARMv5; we choose to take a
10352 * page translation fault.
10353 */
f989983e 10354 fi->type = ARMFault_Translation;
554b0b09
PM
10355 goto do_fault;
10356 }
10357 } else {
10358 phys_addr = (desc & 0xfffffc00) | (address & 0x3ff);
fc1891c7 10359 *page_size = 0x400;
554b0b09 10360 }
9ee6e8bb 10361 ap = (desc >> 4) & 3;
ce819861
PB
10362 break;
10363 default:
9ee6e8bb
PB
10364 /* Never happens, but compiler isn't smart enough to tell. */
10365 abort();
ce819861 10366 }
9ee6e8bb 10367 }
0fbf5238
AJ
10368 *prot = ap_to_rw_prot(env, mmu_idx, ap, domain_prot);
10369 *prot |= *prot ? PAGE_EXEC : 0;
10370 if (!(*prot & (1 << access_type))) {
9ee6e8bb 10371 /* Access permission fault. */
f989983e 10372 fi->type = ARMFault_Permission;
9ee6e8bb
PB
10373 goto do_fault;
10374 }
10375 *phys_ptr = phys_addr;
b7cc4e82 10376 return false;
9ee6e8bb 10377do_fault:
f989983e
PM
10378 fi->domain = domain;
10379 fi->level = level;
b7cc4e82 10380 return true;
9ee6e8bb
PB
10381}
10382
b7cc4e82 10383static bool get_phys_addr_v6(CPUARMState *env, uint32_t address,
03ae85f8 10384 MMUAccessType access_type, ARMMMUIdx mmu_idx,
b7cc4e82 10385 hwaddr *phys_ptr, MemTxAttrs *attrs, int *prot,
f06cf243 10386 target_ulong *page_size, ARMMMUFaultInfo *fi)
9ee6e8bb 10387{
2fc0cc0e 10388 CPUState *cs = env_cpu(env);
f06cf243 10389 int level = 1;
9ee6e8bb
PB
10390 uint32_t table;
10391 uint32_t desc;
10392 uint32_t xn;
de9b05b8 10393 uint32_t pxn = 0;
9ee6e8bb
PB
10394 int type;
10395 int ap;
de9b05b8 10396 int domain = 0;
dd4ebc2e 10397 int domain_prot;
a8170e5e 10398 hwaddr phys_addr;
0480f69a 10399 uint32_t dacr;
8bf5b6a9 10400 bool ns;
9ee6e8bb
PB
10401
10402 /* Pagetable walk. */
10403 /* Lookup l1 descriptor. */
0480f69a 10404 if (!get_level1_table_address(env, mmu_idx, &table, address)) {
e389be16 10405 /* Section translation fault if page walk is disabled by PD0 or PD1 */
f06cf243 10406 fi->type = ARMFault_Translation;
e389be16
FA
10407 goto do_fault;
10408 }
a614e698 10409 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
3795a6de 10410 mmu_idx, fi);
3b39d734
PM
10411 if (fi->type != ARMFault_None) {
10412 goto do_fault;
10413 }
9ee6e8bb 10414 type = (desc & 3);
de9b05b8
PM
10415 if (type == 0 || (type == 3 && !arm_feature(env, ARM_FEATURE_PXN))) {
10416 /* Section translation fault, or attempt to use the encoding
10417 * which is Reserved on implementations without PXN.
10418 */
f06cf243 10419 fi->type = ARMFault_Translation;
9ee6e8bb 10420 goto do_fault;
de9b05b8
PM
10421 }
10422 if ((type == 1) || !(desc & (1 << 18))) {
10423 /* Page or Section. */
dd4ebc2e 10424 domain = (desc >> 5) & 0x0f;
9ee6e8bb 10425 }
0480f69a
PM
10426 if (regime_el(env, mmu_idx) == 1) {
10427 dacr = env->cp15.dacr_ns;
10428 } else {
10429 dacr = env->cp15.dacr_s;
10430 }
f06cf243
PM
10431 if (type == 1) {
10432 level = 2;
10433 }
0480f69a 10434 domain_prot = (dacr >> (domain * 2)) & 3;
dd4ebc2e 10435 if (domain_prot == 0 || domain_prot == 2) {
f06cf243
PM
10436 /* Section or Page domain fault */
10437 fi->type = ARMFault_Domain;
9ee6e8bb
PB
10438 goto do_fault;
10439 }
de9b05b8 10440 if (type != 1) {
9ee6e8bb
PB
10441 if (desc & (1 << 18)) {
10442 /* Supersection. */
10443 phys_addr = (desc & 0xff000000) | (address & 0x00ffffff);
4e42a6ca
SF
10444 phys_addr |= (uint64_t)extract32(desc, 20, 4) << 32;
10445 phys_addr |= (uint64_t)extract32(desc, 5, 4) << 36;
d4c430a8 10446 *page_size = 0x1000000;
b5ff1b31 10447 } else {
9ee6e8bb
PB
10448 /* Section. */
10449 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
d4c430a8 10450 *page_size = 0x100000;
b5ff1b31 10451 }
9ee6e8bb
PB
10452 ap = ((desc >> 10) & 3) | ((desc >> 13) & 4);
10453 xn = desc & (1 << 4);
de9b05b8 10454 pxn = desc & 1;
8bf5b6a9 10455 ns = extract32(desc, 19, 1);
9ee6e8bb 10456 } else {
de9b05b8
PM
10457 if (arm_feature(env, ARM_FEATURE_PXN)) {
10458 pxn = (desc >> 2) & 1;
10459 }
8bf5b6a9 10460 ns = extract32(desc, 3, 1);
9ee6e8bb
PB
10461 /* Lookup l2 entry. */
10462 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
a614e698 10463 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
3795a6de 10464 mmu_idx, fi);
3b39d734
PM
10465 if (fi->type != ARMFault_None) {
10466 goto do_fault;
10467 }
9ee6e8bb
PB
10468 ap = ((desc >> 4) & 3) | ((desc >> 7) & 4);
10469 switch (desc & 3) {
10470 case 0: /* Page translation fault. */
f06cf243 10471 fi->type = ARMFault_Translation;
b5ff1b31 10472 goto do_fault;
9ee6e8bb
PB
10473 case 1: /* 64k page. */
10474 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
10475 xn = desc & (1 << 15);
d4c430a8 10476 *page_size = 0x10000;
9ee6e8bb
PB
10477 break;
10478 case 2: case 3: /* 4k page. */
10479 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
10480 xn = desc & 1;
d4c430a8 10481 *page_size = 0x1000;
9ee6e8bb
PB
10482 break;
10483 default:
10484 /* Never happens, but compiler isn't smart enough to tell. */
10485 abort();
b5ff1b31 10486 }
9ee6e8bb 10487 }
dd4ebc2e 10488 if (domain_prot == 3) {
c0034328
JR
10489 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
10490 } else {
0480f69a 10491 if (pxn && !regime_is_user(env, mmu_idx)) {
de9b05b8
PM
10492 xn = 1;
10493 }
f06cf243
PM
10494 if (xn && access_type == MMU_INST_FETCH) {
10495 fi->type = ARMFault_Permission;
c0034328 10496 goto do_fault;
f06cf243 10497 }
9ee6e8bb 10498
d76951b6
AJ
10499 if (arm_feature(env, ARM_FEATURE_V6K) &&
10500 (regime_sctlr(env, mmu_idx) & SCTLR_AFE)) {
10501 /* The simplified model uses AP[0] as an access control bit. */
10502 if ((ap & 1) == 0) {
10503 /* Access flag fault. */
f06cf243 10504 fi->type = ARMFault_AccessFlag;
d76951b6
AJ
10505 goto do_fault;
10506 }
10507 *prot = simple_ap_to_rw_prot(env, mmu_idx, ap >> 1);
10508 } else {
10509 *prot = ap_to_rw_prot(env, mmu_idx, ap, domain_prot);
c0034328 10510 }
0fbf5238
AJ
10511 if (*prot && !xn) {
10512 *prot |= PAGE_EXEC;
10513 }
10514 if (!(*prot & (1 << access_type))) {
c0034328 10515 /* Access permission fault. */
f06cf243 10516 fi->type = ARMFault_Permission;
c0034328
JR
10517 goto do_fault;
10518 }
3ad493fc 10519 }
8bf5b6a9
PM
10520 if (ns) {
10521 /* The NS bit will (as required by the architecture) have no effect if
10522 * the CPU doesn't support TZ or this is a non-secure translation
10523 * regime, because the attribute will already be non-secure.
10524 */
10525 attrs->secure = false;
10526 }
9ee6e8bb 10527 *phys_ptr = phys_addr;
b7cc4e82 10528 return false;
b5ff1b31 10529do_fault:
f06cf243
PM
10530 fi->domain = domain;
10531 fi->level = level;
b7cc4e82 10532 return true;
b5ff1b31
FB
10533}
10534
1853d5a9 10535/*
a0e966c9 10536 * check_s2_mmu_setup
1853d5a9
EI
10537 * @cpu: ARMCPU
10538 * @is_aa64: True if the translation regime is in AArch64 state
10539 * @startlevel: Suggested starting level
10540 * @inputsize: Bitsize of IPAs
10541 * @stride: Page-table stride (See the ARM ARM)
10542 *
a0e966c9
EI
10543 * Returns true if the suggested S2 translation parameters are OK and
10544 * false otherwise.
1853d5a9 10545 */
a0e966c9
EI
10546static bool check_s2_mmu_setup(ARMCPU *cpu, bool is_aa64, int level,
10547 int inputsize, int stride)
1853d5a9 10548{
98d68ec2
EI
10549 const int grainsize = stride + 3;
10550 int startsizecheck;
10551
1853d5a9
EI
10552 /* Negative levels are never allowed. */
10553 if (level < 0) {
10554 return false;
10555 }
10556
98d68ec2
EI
10557 startsizecheck = inputsize - ((3 - level) * stride + grainsize);
10558 if (startsizecheck < 1 || startsizecheck > stride + 4) {
10559 return false;
10560 }
10561
1853d5a9 10562 if (is_aa64) {
3526423e 10563 CPUARMState *env = &cpu->env;
1853d5a9
EI
10564 unsigned int pamax = arm_pamax(cpu);
10565
10566 switch (stride) {
10567 case 13: /* 64KB Pages. */
10568 if (level == 0 || (level == 1 && pamax <= 42)) {
10569 return false;
10570 }
10571 break;
10572 case 11: /* 16KB Pages. */
10573 if (level == 0 || (level == 1 && pamax <= 40)) {
10574 return false;
10575 }
10576 break;
10577 case 9: /* 4KB Pages. */
10578 if (level == 0 && pamax <= 42) {
10579 return false;
10580 }
10581 break;
10582 default:
10583 g_assert_not_reached();
10584 }
3526423e
EI
10585
10586 /* Inputsize checks. */
10587 if (inputsize > pamax &&
10588 (arm_el_is_aa64(env, 1) || inputsize > 40)) {
10589 /* This is CONSTRAINED UNPREDICTABLE and we choose to fault. */
10590 return false;
10591 }
1853d5a9 10592 } else {
1853d5a9
EI
10593 /* AArch32 only supports 4KB pages. Assert on that. */
10594 assert(stride == 9);
10595
10596 if (level == 0) {
10597 return false;
10598 }
1853d5a9
EI
10599 }
10600 return true;
10601}
10602
5b2d261d
AB
10603/* Translate from the 4-bit stage 2 representation of
10604 * memory attributes (without cache-allocation hints) to
10605 * the 8-bit representation of the stage 1 MAIR registers
10606 * (which includes allocation hints).
10607 *
10608 * ref: shared/translation/attrs/S2AttrDecode()
10609 * .../S2ConvertAttrsHints()
10610 */
10611static uint8_t convert_stage2_attrs(CPUARMState *env, uint8_t s2attrs)
10612{
10613 uint8_t hiattr = extract32(s2attrs, 2, 2);
10614 uint8_t loattr = extract32(s2attrs, 0, 2);
10615 uint8_t hihint = 0, lohint = 0;
10616
10617 if (hiattr != 0) { /* normal memory */
10618 if ((env->cp15.hcr_el2 & HCR_CD) != 0) { /* cache disabled */
10619 hiattr = loattr = 1; /* non-cacheable */
10620 } else {
10621 if (hiattr != 1) { /* Write-through or write-back */
10622 hihint = 3; /* RW allocate */
10623 }
10624 if (loattr != 1) { /* Write-through or write-back */
10625 lohint = 3; /* RW allocate */
10626 }
10627 }
10628 }
10629
10630 return (hiattr << 6) | (hihint << 4) | (loattr << 2) | lohint;
10631}
c47eaf9f 10632#endif /* !CONFIG_USER_ONLY */
5b2d261d 10633
b830a5ee
RH
10634static int aa64_va_parameter_tbi(uint64_t tcr, ARMMMUIdx mmu_idx)
10635{
10636 if (regime_has_2_ranges(mmu_idx)) {
10637 return extract64(tcr, 37, 2);
10638 } else if (mmu_idx == ARMMMUIdx_Stage2) {
10639 return 0; /* VTCR_EL2 */
10640 } else {
3e270f67
RH
10641 /* Replicate the single TBI bit so we always have 2 bits. */
10642 return extract32(tcr, 20, 1) * 3;
b830a5ee
RH
10643 }
10644}
10645
10646static int aa64_va_parameter_tbid(uint64_t tcr, ARMMMUIdx mmu_idx)
10647{
10648 if (regime_has_2_ranges(mmu_idx)) {
10649 return extract64(tcr, 51, 2);
10650 } else if (mmu_idx == ARMMMUIdx_Stage2) {
10651 return 0; /* VTCR_EL2 */
10652 } else {
3e270f67
RH
10653 /* Replicate the single TBID bit so we always have 2 bits. */
10654 return extract32(tcr, 29, 1) * 3;
b830a5ee
RH
10655 }
10656}
10657
81ae05fa
RH
10658static int aa64_va_parameter_tcma(uint64_t tcr, ARMMMUIdx mmu_idx)
10659{
10660 if (regime_has_2_ranges(mmu_idx)) {
10661 return extract64(tcr, 57, 2);
10662 } else {
10663 /* Replicate the single TCMA bit so we always have 2 bits. */
10664 return extract32(tcr, 30, 1) * 3;
10665 }
10666}
10667
b830a5ee
RH
10668ARMVAParameters aa64_va_parameters(CPUARMState *env, uint64_t va,
10669 ARMMMUIdx mmu_idx, bool data)
ba97be9f
RH
10670{
10671 uint64_t tcr = regime_tcr(env, mmu_idx)->raw_tcr;
b830a5ee
RH
10672 bool epd, hpd, using16k, using64k;
10673 int select, tsz, tbi;
ba97be9f 10674
339370b9 10675 if (!regime_has_2_ranges(mmu_idx)) {
71d18164 10676 select = 0;
ba97be9f
RH
10677 tsz = extract32(tcr, 0, 6);
10678 using64k = extract32(tcr, 14, 1);
10679 using16k = extract32(tcr, 15, 1);
97fa9350 10680 if (mmu_idx == ARMMMUIdx_Stage2) {
ba97be9f 10681 /* VTCR_EL2 */
b830a5ee 10682 hpd = false;
ba97be9f 10683 } else {
ba97be9f
RH
10684 hpd = extract32(tcr, 24, 1);
10685 }
10686 epd = false;
ba97be9f 10687 } else {
71d18164
RH
10688 /*
10689 * Bit 55 is always between the two regions, and is canonical for
10690 * determining if address tagging is enabled.
10691 */
10692 select = extract64(va, 55, 1);
10693 if (!select) {
10694 tsz = extract32(tcr, 0, 6);
10695 epd = extract32(tcr, 7, 1);
10696 using64k = extract32(tcr, 14, 1);
10697 using16k = extract32(tcr, 15, 1);
71d18164 10698 hpd = extract64(tcr, 41, 1);
71d18164
RH
10699 } else {
10700 int tg = extract32(tcr, 30, 2);
10701 using16k = tg == 1;
10702 using64k = tg == 3;
10703 tsz = extract32(tcr, 16, 6);
10704 epd = extract32(tcr, 23, 1);
71d18164 10705 hpd = extract64(tcr, 42, 1);
71d18164 10706 }
ba97be9f
RH
10707 }
10708 tsz = MIN(tsz, 39); /* TODO: ARMv8.4-TTST */
10709 tsz = MAX(tsz, 16); /* TODO: ARMv8.2-LVA */
10710
b830a5ee
RH
10711 /* Present TBI as a composite with TBID. */
10712 tbi = aa64_va_parameter_tbi(tcr, mmu_idx);
10713 if (!data) {
10714 tbi &= ~aa64_va_parameter_tbid(tcr, mmu_idx);
10715 }
10716 tbi = (tbi >> select) & 1;
10717
ba97be9f
RH
10718 return (ARMVAParameters) {
10719 .tsz = tsz,
10720 .select = select,
10721 .tbi = tbi,
10722 .epd = epd,
10723 .hpd = hpd,
10724 .using16k = using16k,
10725 .using64k = using64k,
10726 };
10727}
10728
c47eaf9f 10729#ifndef CONFIG_USER_ONLY
ba97be9f
RH
10730static ARMVAParameters aa32_va_parameters(CPUARMState *env, uint32_t va,
10731 ARMMMUIdx mmu_idx)
10732{
10733 uint64_t tcr = regime_tcr(env, mmu_idx)->raw_tcr;
10734 uint32_t el = regime_el(env, mmu_idx);
10735 int select, tsz;
10736 bool epd, hpd;
10737
97fa9350 10738 if (mmu_idx == ARMMMUIdx_Stage2) {
ba97be9f
RH
10739 /* VTCR */
10740 bool sext = extract32(tcr, 4, 1);
10741 bool sign = extract32(tcr, 3, 1);
10742
10743 /*
10744 * If the sign-extend bit is not the same as t0sz[3], the result
10745 * is unpredictable. Flag this as a guest error.
10746 */
10747 if (sign != sext) {
10748 qemu_log_mask(LOG_GUEST_ERROR,
10749 "AArch32: VTCR.S / VTCR.T0SZ[3] mismatch\n");
10750 }
10751 tsz = sextract32(tcr, 0, 4) + 8;
10752 select = 0;
10753 hpd = false;
10754 epd = false;
10755 } else if (el == 2) {
10756 /* HTCR */
10757 tsz = extract32(tcr, 0, 3);
10758 select = 0;
10759 hpd = extract64(tcr, 24, 1);
10760 epd = false;
10761 } else {
10762 int t0sz = extract32(tcr, 0, 3);
10763 int t1sz = extract32(tcr, 16, 3);
10764
10765 if (t1sz == 0) {
10766 select = va > (0xffffffffu >> t0sz);
10767 } else {
10768 /* Note that we will detect errors later. */
10769 select = va >= ~(0xffffffffu >> t1sz);
10770 }
10771 if (!select) {
10772 tsz = t0sz;
10773 epd = extract32(tcr, 7, 1);
10774 hpd = extract64(tcr, 41, 1);
10775 } else {
10776 tsz = t1sz;
10777 epd = extract32(tcr, 23, 1);
10778 hpd = extract64(tcr, 42, 1);
10779 }
10780 /* For aarch32, hpd0 is not enabled without t2e as well. */
10781 hpd &= extract32(tcr, 6, 1);
10782 }
10783
10784 return (ARMVAParameters) {
10785 .tsz = tsz,
10786 .select = select,
10787 .epd = epd,
10788 .hpd = hpd,
10789 };
10790}
10791
ff7de2fc
PM
10792/**
10793 * get_phys_addr_lpae: perform one stage of page table walk, LPAE format
10794 *
10795 * Returns false if the translation was successful. Otherwise, phys_ptr, attrs,
10796 * prot and page_size may not be filled in, and the populated fsr value provides
10797 * information on why the translation aborted, in the format of a long-format
10798 * DFSR/IFSR fault register, with the following caveats:
10799 * * the WnR bit is never set (the caller must do this).
10800 *
10801 * @env: CPUARMState
10802 * @address: virtual address to get physical address for
10803 * @access_type: MMU_DATA_LOAD, MMU_DATA_STORE or MMU_INST_FETCH
10804 * @mmu_idx: MMU index indicating required translation regime
10805 * @s1_is_el0: if @mmu_idx is ARMMMUIdx_Stage2 (so this is a stage 2 page table
10806 * walk), must be true if this is stage 2 of a stage 1+2 walk for an
10807 * EL0 access). If @mmu_idx is anything else, @s1_is_el0 is ignored.
10808 * @phys_ptr: set to the physical address corresponding to the virtual address
10809 * @attrs: set to the memory transaction attributes to use
10810 * @prot: set to the permissions for the page containing phys_ptr
10811 * @page_size_ptr: set to the size of the page containing phys_ptr
10812 * @fi: set to fault info if the translation fails
10813 * @cacheattrs: (if non-NULL) set to the cacheability/shareability attributes
10814 */
b7cc4e82 10815static bool get_phys_addr_lpae(CPUARMState *env, target_ulong address,
03ae85f8 10816 MMUAccessType access_type, ARMMMUIdx mmu_idx,
ff7de2fc 10817 bool s1_is_el0,
b7cc4e82 10818 hwaddr *phys_ptr, MemTxAttrs *txattrs, int *prot,
da909b2c 10819 target_ulong *page_size_ptr,
5b2d261d 10820 ARMMMUFaultInfo *fi, ARMCacheAttrs *cacheattrs)
3dde962f 10821{
2fc0cc0e 10822 ARMCPU *cpu = env_archcpu(env);
1853d5a9 10823 CPUState *cs = CPU(cpu);
3dde962f 10824 /* Read an LPAE long-descriptor translation table. */
da909b2c 10825 ARMFaultType fault_type = ARMFault_Translation;
1b4093ea 10826 uint32_t level;
ba97be9f 10827 ARMVAParameters param;
3dde962f 10828 uint64_t ttbr;
dddb5223 10829 hwaddr descaddr, indexmask, indexmask_grainsize;
3dde962f 10830 uint32_t tableattrs;
36d820af 10831 target_ulong page_size;
3dde962f 10832 uint32_t attrs;
ba97be9f
RH
10833 int32_t stride;
10834 int addrsize, inputsize;
0480f69a 10835 TCR *tcr = regime_tcr(env, mmu_idx);
d8e052b3 10836 int ap, ns, xn, pxn;
88e8add8 10837 uint32_t el = regime_el(env, mmu_idx);
6109769a 10838 uint64_t descaddrmask;
6e99f762 10839 bool aarch64 = arm_el_is_aa64(env, el);
1bafc2ba 10840 bool guarded = false;
0480f69a 10841
07d1be3b 10842 /* TODO: This code does not support shareability levels. */
6e99f762 10843 if (aarch64) {
ba97be9f
RH
10844 param = aa64_va_parameters(env, address, mmu_idx,
10845 access_type != MMU_INST_FETCH);
1b4093ea 10846 level = 0;
ba97be9f
RH
10847 addrsize = 64 - 8 * param.tbi;
10848 inputsize = 64 - param.tsz;
d0a2cbce 10849 } else {
ba97be9f 10850 param = aa32_va_parameters(env, address, mmu_idx);
1b4093ea 10851 level = 1;
97fa9350 10852 addrsize = (mmu_idx == ARMMMUIdx_Stage2 ? 40 : 32);
ba97be9f 10853 inputsize = addrsize - param.tsz;
2c8dd318 10854 }
3dde962f 10855
ba97be9f
RH
10856 /*
10857 * We determined the region when collecting the parameters, but we
10858 * have not yet validated that the address is valid for the region.
10859 * Extract the top bits and verify that they all match select.
36d820af
RH
10860 *
10861 * For aa32, if inputsize == addrsize, then we have selected the
10862 * region by exclusion in aa32_va_parameters and there is no more
10863 * validation to do here.
10864 */
10865 if (inputsize < addrsize) {
10866 target_ulong top_bits = sextract64(address, inputsize,
10867 addrsize - inputsize);
03f27724 10868 if (-top_bits != param.select) {
36d820af
RH
10869 /* The gap between the two regions is a Translation fault */
10870 fault_type = ARMFault_Translation;
10871 goto do_fault;
10872 }
3dde962f
PM
10873 }
10874
ba97be9f
RH
10875 if (param.using64k) {
10876 stride = 13;
10877 } else if (param.using16k) {
10878 stride = 11;
10879 } else {
10880 stride = 9;
10881 }
10882
3dde962f
PM
10883 /* Note that QEMU ignores shareability and cacheability attributes,
10884 * so we don't need to do anything with the SH, ORGN, IRGN fields
10885 * in the TTBCR. Similarly, TTBCR:A1 selects whether we get the
10886 * ASID from TTBR0 or TTBR1, but QEMU's TLB doesn't currently
10887 * implement any ASID-like capability so we can ignore it (instead
10888 * we will always flush the TLB any time the ASID is changed).
10889 */
ba97be9f 10890 ttbr = regime_ttbr(env, mmu_idx, param.select);
3dde962f 10891
0480f69a 10892 /* Here we should have set up all the parameters for the translation:
6e99f762 10893 * inputsize, ttbr, epd, stride, tbi
0480f69a
PM
10894 */
10895
ba97be9f 10896 if (param.epd) {
88e8add8
GB
10897 /* Translation table walk disabled => Translation fault on TLB miss
10898 * Note: This is always 0 on 64-bit EL2 and EL3.
10899 */
3dde962f
PM
10900 goto do_fault;
10901 }
10902
97fa9350 10903 if (mmu_idx != ARMMMUIdx_Stage2) {
1853d5a9
EI
10904 /* The starting level depends on the virtual address size (which can
10905 * be up to 48 bits) and the translation granule size. It indicates
10906 * the number of strides (stride bits at a time) needed to
10907 * consume the bits of the input address. In the pseudocode this is:
10908 * level = 4 - RoundUp((inputsize - grainsize) / stride)
10909 * where their 'inputsize' is our 'inputsize', 'grainsize' is
10910 * our 'stride + 3' and 'stride' is our 'stride'.
10911 * Applying the usual "rounded up m/n is (m+n-1)/n" and simplifying:
10912 * = 4 - (inputsize - stride - 3 + stride - 1) / stride
10913 * = 4 - (inputsize - 4) / stride;
10914 */
10915 level = 4 - (inputsize - 4) / stride;
10916 } else {
10917 /* For stage 2 translations the starting level is specified by the
10918 * VTCR_EL2.SL0 field (whose interpretation depends on the page size)
10919 */
1b4093ea
SS
10920 uint32_t sl0 = extract32(tcr->raw_tcr, 6, 2);
10921 uint32_t startlevel;
1853d5a9
EI
10922 bool ok;
10923
6e99f762 10924 if (!aarch64 || stride == 9) {
1853d5a9 10925 /* AArch32 or 4KB pages */
1b4093ea 10926 startlevel = 2 - sl0;
1853d5a9
EI
10927 } else {
10928 /* 16KB or 64KB pages */
1b4093ea 10929 startlevel = 3 - sl0;
1853d5a9
EI
10930 }
10931
10932 /* Check that the starting level is valid. */
6e99f762 10933 ok = check_s2_mmu_setup(cpu, aarch64, startlevel,
1b4093ea 10934 inputsize, stride);
1853d5a9 10935 if (!ok) {
da909b2c 10936 fault_type = ARMFault_Translation;
1853d5a9
EI
10937 goto do_fault;
10938 }
1b4093ea 10939 level = startlevel;
1853d5a9 10940 }
3dde962f 10941
dddb5223
SS
10942 indexmask_grainsize = (1ULL << (stride + 3)) - 1;
10943 indexmask = (1ULL << (inputsize - (stride * (4 - level)))) - 1;
3dde962f
PM
10944
10945 /* Now we can extract the actual base address from the TTBR */
2c8dd318 10946 descaddr = extract64(ttbr, 0, 48);
41a4bf1f
PM
10947 /*
10948 * We rely on this masking to clear the RES0 bits at the bottom of the TTBR
10949 * and also to mask out CnP (bit 0) which could validly be non-zero.
10950 */
dddb5223 10951 descaddr &= ~indexmask;
3dde962f 10952
6109769a 10953 /* The address field in the descriptor goes up to bit 39 for ARMv7
dddb5223
SS
10954 * but up to bit 47 for ARMv8, but we use the descaddrmask
10955 * up to bit 39 for AArch32, because we don't need other bits in that case
10956 * to construct next descriptor address (anyway they should be all zeroes).
6109769a 10957 */
6e99f762 10958 descaddrmask = ((1ull << (aarch64 ? 48 : 40)) - 1) &
dddb5223 10959 ~indexmask_grainsize;
6109769a 10960
ebca90e4
PM
10961 /* Secure accesses start with the page table in secure memory and
10962 * can be downgraded to non-secure at any step. Non-secure accesses
10963 * remain non-secure. We implement this by just ORing in the NSTable/NS
10964 * bits at each step.
10965 */
10966 tableattrs = regime_is_secure(env, mmu_idx) ? 0 : (1 << 4);
3dde962f
PM
10967 for (;;) {
10968 uint64_t descriptor;
ebca90e4 10969 bool nstable;
3dde962f 10970
dddb5223 10971 descaddr |= (address >> (stride * (4 - level))) & indexmask;
2c8dd318 10972 descaddr &= ~7ULL;
ebca90e4 10973 nstable = extract32(tableattrs, 4, 1);
3795a6de 10974 descriptor = arm_ldq_ptw(cs, descaddr, !nstable, mmu_idx, fi);
3b39d734 10975 if (fi->type != ARMFault_None) {
37785977
EI
10976 goto do_fault;
10977 }
10978
3dde962f
PM
10979 if (!(descriptor & 1) ||
10980 (!(descriptor & 2) && (level == 3))) {
10981 /* Invalid, or the Reserved level 3 encoding */
10982 goto do_fault;
10983 }
6109769a 10984 descaddr = descriptor & descaddrmask;
3dde962f
PM
10985
10986 if ((descriptor & 2) && (level < 3)) {
037c13c5 10987 /* Table entry. The top five bits are attributes which may
3dde962f
PM
10988 * propagate down through lower levels of the table (and
10989 * which are all arranged so that 0 means "no effect", so
10990 * we can gather them up by ORing in the bits at each level).
10991 */
10992 tableattrs |= extract64(descriptor, 59, 5);
10993 level++;
dddb5223 10994 indexmask = indexmask_grainsize;
3dde962f
PM
10995 continue;
10996 }
10997 /* Block entry at level 1 or 2, or page entry at level 3.
10998 * These are basically the same thing, although the number
10999 * of bits we pull in from the vaddr varies.
11000 */
973a5434 11001 page_size = (1ULL << ((stride * (4 - level)) + 3));
3dde962f 11002 descaddr |= (address & (page_size - 1));
6ab1a5ee 11003 /* Extract attributes from the descriptor */
d615efac
IC
11004 attrs = extract64(descriptor, 2, 10)
11005 | (extract64(descriptor, 52, 12) << 10);
6ab1a5ee 11006
97fa9350 11007 if (mmu_idx == ARMMMUIdx_Stage2) {
6ab1a5ee
EI
11008 /* Stage 2 table descriptors do not include any attribute fields */
11009 break;
11010 }
11011 /* Merge in attributes from table descriptors */
037c13c5 11012 attrs |= nstable << 3; /* NS */
1bafc2ba 11013 guarded = extract64(descriptor, 50, 1); /* GP */
ba97be9f 11014 if (param.hpd) {
037c13c5
RH
11015 /* HPD disables all the table attributes except NSTable. */
11016 break;
11017 }
11018 attrs |= extract32(tableattrs, 0, 2) << 11; /* XN, PXN */
3dde962f
PM
11019 /* The sense of AP[1] vs APTable[0] is reversed, as APTable[0] == 1
11020 * means "force PL1 access only", which means forcing AP[1] to 0.
11021 */
037c13c5
RH
11022 attrs &= ~(extract32(tableattrs, 2, 1) << 4); /* !APT[0] => AP[1] */
11023 attrs |= extract32(tableattrs, 3, 1) << 5; /* APT[1] => AP[2] */
3dde962f
PM
11024 break;
11025 }
11026 /* Here descaddr is the final physical address, and attributes
11027 * are all in attrs.
11028 */
da909b2c 11029 fault_type = ARMFault_AccessFlag;
3dde962f
PM
11030 if ((attrs & (1 << 8)) == 0) {
11031 /* Access flag */
11032 goto do_fault;
11033 }
d8e052b3
AJ
11034
11035 ap = extract32(attrs, 4, 2);
d8e052b3 11036
97fa9350 11037 if (mmu_idx == ARMMMUIdx_Stage2) {
6ab1a5ee 11038 ns = true;
ce3125be
PM
11039 xn = extract32(attrs, 11, 2);
11040 *prot = get_S2prot(env, ap, xn, s1_is_el0);
6ab1a5ee
EI
11041 } else {
11042 ns = extract32(attrs, 3, 1);
ce3125be 11043 xn = extract32(attrs, 12, 1);
6ab1a5ee 11044 pxn = extract32(attrs, 11, 1);
6e99f762 11045 *prot = get_S1prot(env, mmu_idx, aarch64, ap, ns, xn, pxn);
6ab1a5ee 11046 }
d8e052b3 11047
da909b2c 11048 fault_type = ARMFault_Permission;
d8e052b3 11049 if (!(*prot & (1 << access_type))) {
3dde962f
PM
11050 goto do_fault;
11051 }
3dde962f 11052
8bf5b6a9
PM
11053 if (ns) {
11054 /* The NS bit will (as required by the architecture) have no effect if
11055 * the CPU doesn't support TZ or this is a non-secure translation
11056 * regime, because the attribute will already be non-secure.
11057 */
11058 txattrs->secure = false;
11059 }
1bafc2ba
RH
11060 /* When in aarch64 mode, and BTI is enabled, remember GP in the IOTLB. */
11061 if (aarch64 && guarded && cpu_isar_feature(aa64_bti, cpu)) {
11062 txattrs->target_tlb_bit0 = true;
11063 }
5b2d261d
AB
11064
11065 if (cacheattrs != NULL) {
97fa9350 11066 if (mmu_idx == ARMMMUIdx_Stage2) {
5b2d261d
AB
11067 cacheattrs->attrs = convert_stage2_attrs(env,
11068 extract32(attrs, 0, 4));
11069 } else {
11070 /* Index into MAIR registers for cache attributes */
11071 uint8_t attrindx = extract32(attrs, 0, 3);
11072 uint64_t mair = env->cp15.mair_el[regime_el(env, mmu_idx)];
11073 assert(attrindx <= 7);
11074 cacheattrs->attrs = extract64(mair, attrindx * 8, 8);
11075 }
11076 cacheattrs->shareability = extract32(attrs, 6, 2);
11077 }
11078
3dde962f
PM
11079 *phys_ptr = descaddr;
11080 *page_size_ptr = page_size;
b7cc4e82 11081 return false;
3dde962f
PM
11082
11083do_fault:
da909b2c
PM
11084 fi->type = fault_type;
11085 fi->level = level;
37785977 11086 /* Tag the error as S2 for failed S1 PTW at S2 or ordinary S2. */
97fa9350 11087 fi->stage2 = fi->s1ptw || (mmu_idx == ARMMMUIdx_Stage2);
b7cc4e82 11088 return true;
3dde962f
PM
11089}
11090
f6bda88f
PC
11091static inline void get_phys_addr_pmsav7_default(CPUARMState *env,
11092 ARMMMUIdx mmu_idx,
11093 int32_t address, int *prot)
11094{
3a00d560
MD
11095 if (!arm_feature(env, ARM_FEATURE_M)) {
11096 *prot = PAGE_READ | PAGE_WRITE;
11097 switch (address) {
11098 case 0xF0000000 ... 0xFFFFFFFF:
11099 if (regime_sctlr(env, mmu_idx) & SCTLR_V) {
11100 /* hivecs execing is ok */
11101 *prot |= PAGE_EXEC;
11102 }
11103 break;
11104 case 0x00000000 ... 0x7FFFFFFF:
f6bda88f 11105 *prot |= PAGE_EXEC;
3a00d560
MD
11106 break;
11107 }
11108 } else {
11109 /* Default system address map for M profile cores.
11110 * The architecture specifies which regions are execute-never;
11111 * at the MPU level no other checks are defined.
11112 */
11113 switch (address) {
11114 case 0x00000000 ... 0x1fffffff: /* ROM */
11115 case 0x20000000 ... 0x3fffffff: /* SRAM */
11116 case 0x60000000 ... 0x7fffffff: /* RAM */
11117 case 0x80000000 ... 0x9fffffff: /* RAM */
11118 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
11119 break;
11120 case 0x40000000 ... 0x5fffffff: /* Peripheral */
11121 case 0xa0000000 ... 0xbfffffff: /* Device */
11122 case 0xc0000000 ... 0xdfffffff: /* Device */
11123 case 0xe0000000 ... 0xffffffff: /* System */
11124 *prot = PAGE_READ | PAGE_WRITE;
11125 break;
11126 default:
11127 g_assert_not_reached();
f6bda88f 11128 }
f6bda88f 11129 }
f6bda88f
PC
11130}
11131
29c483a5
MD
11132static bool pmsav7_use_background_region(ARMCPU *cpu,
11133 ARMMMUIdx mmu_idx, bool is_user)
11134{
11135 /* Return true if we should use the default memory map as a
11136 * "background" region if there are no hits against any MPU regions.
11137 */
11138 CPUARMState *env = &cpu->env;
11139
11140 if (is_user) {
11141 return false;
11142 }
11143
11144 if (arm_feature(env, ARM_FEATURE_M)) {
ecf5e8ea
PM
11145 return env->v7m.mpu_ctrl[regime_is_secure(env, mmu_idx)]
11146 & R_V7M_MPU_CTRL_PRIVDEFENA_MASK;
29c483a5
MD
11147 } else {
11148 return regime_sctlr(env, mmu_idx) & SCTLR_BR;
11149 }
11150}
11151
38aaa60c
PM
11152static inline bool m_is_ppb_region(CPUARMState *env, uint32_t address)
11153{
11154 /* True if address is in the M profile PPB region 0xe0000000 - 0xe00fffff */
11155 return arm_feature(env, ARM_FEATURE_M) &&
11156 extract32(address, 20, 12) == 0xe00;
11157}
11158
bf446a11
PM
11159static inline bool m_is_system_region(CPUARMState *env, uint32_t address)
11160{
11161 /* True if address is in the M profile system region
11162 * 0xe0000000 - 0xffffffff
11163 */
11164 return arm_feature(env, ARM_FEATURE_M) && extract32(address, 29, 3) == 0x7;
11165}
11166
f6bda88f 11167static bool get_phys_addr_pmsav7(CPUARMState *env, uint32_t address,
03ae85f8 11168 MMUAccessType access_type, ARMMMUIdx mmu_idx,
9375ad15 11169 hwaddr *phys_ptr, int *prot,
e5e40999 11170 target_ulong *page_size,
9375ad15 11171 ARMMMUFaultInfo *fi)
f6bda88f 11172{
2fc0cc0e 11173 ARMCPU *cpu = env_archcpu(env);
f6bda88f
PC
11174 int n;
11175 bool is_user = regime_is_user(env, mmu_idx);
11176
11177 *phys_ptr = address;
e5e40999 11178 *page_size = TARGET_PAGE_SIZE;
f6bda88f
PC
11179 *prot = 0;
11180
38aaa60c
PM
11181 if (regime_translation_disabled(env, mmu_idx) ||
11182 m_is_ppb_region(env, address)) {
11183 /* MPU disabled or M profile PPB access: use default memory map.
11184 * The other case which uses the default memory map in the
11185 * v7M ARM ARM pseudocode is exception vector reads from the vector
11186 * table. In QEMU those accesses are done in arm_v7m_load_vector(),
11187 * which always does a direct read using address_space_ldl(), rather
11188 * than going via this function, so we don't need to check that here.
11189 */
f6bda88f
PC
11190 get_phys_addr_pmsav7_default(env, mmu_idx, address, prot);
11191 } else { /* MPU enabled */
11192 for (n = (int)cpu->pmsav7_dregion - 1; n >= 0; n--) {
11193 /* region search */
11194 uint32_t base = env->pmsav7.drbar[n];
11195 uint32_t rsize = extract32(env->pmsav7.drsr[n], 1, 5);
11196 uint32_t rmask;
11197 bool srdis = false;
11198
11199 if (!(env->pmsav7.drsr[n] & 0x1)) {
11200 continue;
11201 }
11202
11203 if (!rsize) {
c9f9f124
MD
11204 qemu_log_mask(LOG_GUEST_ERROR,
11205 "DRSR[%d]: Rsize field cannot be 0\n", n);
f6bda88f
PC
11206 continue;
11207 }
11208 rsize++;
11209 rmask = (1ull << rsize) - 1;
11210
11211 if (base & rmask) {
c9f9f124
MD
11212 qemu_log_mask(LOG_GUEST_ERROR,
11213 "DRBAR[%d]: 0x%" PRIx32 " misaligned "
11214 "to DRSR region size, mask = 0x%" PRIx32 "\n",
11215 n, base, rmask);
f6bda88f
PC
11216 continue;
11217 }
11218
11219 if (address < base || address > base + rmask) {
9d2b5a58
PM
11220 /*
11221 * Address not in this region. We must check whether the
11222 * region covers addresses in the same page as our address.
11223 * In that case we must not report a size that covers the
11224 * whole page for a subsequent hit against a different MPU
11225 * region or the background region, because it would result in
11226 * incorrect TLB hits for subsequent accesses to addresses that
11227 * are in this MPU region.
11228 */
11229 if (ranges_overlap(base, rmask,
11230 address & TARGET_PAGE_MASK,
11231 TARGET_PAGE_SIZE)) {
11232 *page_size = 1;
11233 }
f6bda88f
PC
11234 continue;
11235 }
11236
11237 /* Region matched */
11238
11239 if (rsize >= 8) { /* no subregions for regions < 256 bytes */
11240 int i, snd;
11241 uint32_t srdis_mask;
11242
11243 rsize -= 3; /* sub region size (power of 2) */
11244 snd = ((address - base) >> rsize) & 0x7;
11245 srdis = extract32(env->pmsav7.drsr[n], snd + 8, 1);
11246
11247 srdis_mask = srdis ? 0x3 : 0x0;
11248 for (i = 2; i <= 8 && rsize < TARGET_PAGE_BITS; i *= 2) {
11249 /* This will check in groups of 2, 4 and then 8, whether
11250 * the subregion bits are consistent. rsize is incremented
11251 * back up to give the region size, considering consistent
11252 * adjacent subregions as one region. Stop testing if rsize
11253 * is already big enough for an entire QEMU page.
11254 */
11255 int snd_rounded = snd & ~(i - 1);
11256 uint32_t srdis_multi = extract32(env->pmsav7.drsr[n],
11257 snd_rounded + 8, i);
11258 if (srdis_mask ^ srdis_multi) {
11259 break;
11260 }
11261 srdis_mask = (srdis_mask << i) | srdis_mask;
11262 rsize++;
11263 }
11264 }
f6bda88f
PC
11265 if (srdis) {
11266 continue;
11267 }
e5e40999
PM
11268 if (rsize < TARGET_PAGE_BITS) {
11269 *page_size = 1 << rsize;
11270 }
f6bda88f
PC
11271 break;
11272 }
11273
11274 if (n == -1) { /* no hits */
29c483a5 11275 if (!pmsav7_use_background_region(cpu, mmu_idx, is_user)) {
f6bda88f 11276 /* background fault */
9375ad15 11277 fi->type = ARMFault_Background;
f6bda88f
PC
11278 return true;
11279 }
11280 get_phys_addr_pmsav7_default(env, mmu_idx, address, prot);
11281 } else { /* a MPU hit! */
11282 uint32_t ap = extract32(env->pmsav7.dracr[n], 8, 3);
bf446a11
PM
11283 uint32_t xn = extract32(env->pmsav7.dracr[n], 12, 1);
11284
11285 if (m_is_system_region(env, address)) {
11286 /* System space is always execute never */
11287 xn = 1;
11288 }
f6bda88f
PC
11289
11290 if (is_user) { /* User mode AP bit decoding */
11291 switch (ap) {
11292 case 0:
11293 case 1:
11294 case 5:
11295 break; /* no access */
11296 case 3:
11297 *prot |= PAGE_WRITE;
11298 /* fall through */
11299 case 2:
11300 case 6:
11301 *prot |= PAGE_READ | PAGE_EXEC;
11302 break;
8638f1ad
PM
11303 case 7:
11304 /* for v7M, same as 6; for R profile a reserved value */
11305 if (arm_feature(env, ARM_FEATURE_M)) {
11306 *prot |= PAGE_READ | PAGE_EXEC;
11307 break;
11308 }
11309 /* fall through */
f6bda88f
PC
11310 default:
11311 qemu_log_mask(LOG_GUEST_ERROR,
c9f9f124
MD
11312 "DRACR[%d]: Bad value for AP bits: 0x%"
11313 PRIx32 "\n", n, ap);
f6bda88f
PC
11314 }
11315 } else { /* Priv. mode AP bits decoding */
11316 switch (ap) {
11317 case 0:
11318 break; /* no access */
11319 case 1:
11320 case 2:
11321 case 3:
11322 *prot |= PAGE_WRITE;
11323 /* fall through */
11324 case 5:
11325 case 6:
11326 *prot |= PAGE_READ | PAGE_EXEC;
11327 break;
8638f1ad
PM
11328 case 7:
11329 /* for v7M, same as 6; for R profile a reserved value */
11330 if (arm_feature(env, ARM_FEATURE_M)) {
11331 *prot |= PAGE_READ | PAGE_EXEC;
11332 break;
11333 }
11334 /* fall through */
f6bda88f
PC
11335 default:
11336 qemu_log_mask(LOG_GUEST_ERROR,
c9f9f124
MD
11337 "DRACR[%d]: Bad value for AP bits: 0x%"
11338 PRIx32 "\n", n, ap);
f6bda88f
PC
11339 }
11340 }
11341
11342 /* execute never */
bf446a11 11343 if (xn) {
f6bda88f
PC
11344 *prot &= ~PAGE_EXEC;
11345 }
11346 }
11347 }
11348
9375ad15
PM
11349 fi->type = ARMFault_Permission;
11350 fi->level = 1;
f6bda88f
PC
11351 return !(*prot & (1 << access_type));
11352}
11353
35337cc3
PM
11354static bool v8m_is_sau_exempt(CPUARMState *env,
11355 uint32_t address, MMUAccessType access_type)
11356{
11357 /* The architecture specifies that certain address ranges are
11358 * exempt from v8M SAU/IDAU checks.
11359 */
11360 return
11361 (access_type == MMU_INST_FETCH && m_is_system_region(env, address)) ||
11362 (address >= 0xe0000000 && address <= 0xe0002fff) ||
11363 (address >= 0xe000e000 && address <= 0xe000efff) ||
11364 (address >= 0xe002e000 && address <= 0xe002efff) ||
11365 (address >= 0xe0040000 && address <= 0xe0041fff) ||
11366 (address >= 0xe00ff000 && address <= 0xe00fffff);
11367}
11368
787a7e76 11369void v8m_security_lookup(CPUARMState *env, uint32_t address,
35337cc3
PM
11370 MMUAccessType access_type, ARMMMUIdx mmu_idx,
11371 V8M_SAttributes *sattrs)
11372{
11373 /* Look up the security attributes for this address. Compare the
11374 * pseudocode SecurityCheck() function.
11375 * We assume the caller has zero-initialized *sattrs.
11376 */
2fc0cc0e 11377 ARMCPU *cpu = env_archcpu(env);
35337cc3 11378 int r;
181962fd
PM
11379 bool idau_exempt = false, idau_ns = true, idau_nsc = true;
11380 int idau_region = IREGION_NOTVALID;
72042435
PM
11381 uint32_t addr_page_base = address & TARGET_PAGE_MASK;
11382 uint32_t addr_page_limit = addr_page_base + (TARGET_PAGE_SIZE - 1);
35337cc3 11383
181962fd
PM
11384 if (cpu->idau) {
11385 IDAUInterfaceClass *iic = IDAU_INTERFACE_GET_CLASS(cpu->idau);
11386 IDAUInterface *ii = IDAU_INTERFACE(cpu->idau);
11387
11388 iic->check(ii, address, &idau_region, &idau_exempt, &idau_ns,
11389 &idau_nsc);
11390 }
35337cc3
PM
11391
11392 if (access_type == MMU_INST_FETCH && extract32(address, 28, 4) == 0xf) {
11393 /* 0xf0000000..0xffffffff is always S for insn fetches */
11394 return;
11395 }
11396
181962fd 11397 if (idau_exempt || v8m_is_sau_exempt(env, address, access_type)) {
35337cc3
PM
11398 sattrs->ns = !regime_is_secure(env, mmu_idx);
11399 return;
11400 }
11401
181962fd
PM
11402 if (idau_region != IREGION_NOTVALID) {
11403 sattrs->irvalid = true;
11404 sattrs->iregion = idau_region;
11405 }
11406
35337cc3
PM
11407 switch (env->sau.ctrl & 3) {
11408 case 0: /* SAU.ENABLE == 0, SAU.ALLNS == 0 */
11409 break;
11410 case 2: /* SAU.ENABLE == 0, SAU.ALLNS == 1 */
11411 sattrs->ns = true;
11412 break;
11413 default: /* SAU.ENABLE == 1 */
11414 for (r = 0; r < cpu->sau_sregion; r++) {
11415 if (env->sau.rlar[r] & 1) {
11416 uint32_t base = env->sau.rbar[r] & ~0x1f;
11417 uint32_t limit = env->sau.rlar[r] | 0x1f;
11418
11419 if (base <= address && limit >= address) {
72042435
PM
11420 if (base > addr_page_base || limit < addr_page_limit) {
11421 sattrs->subpage = true;
11422 }
35337cc3
PM
11423 if (sattrs->srvalid) {
11424 /* If we hit in more than one region then we must report
11425 * as Secure, not NS-Callable, with no valid region
11426 * number info.
11427 */
11428 sattrs->ns = false;
11429 sattrs->nsc = false;
11430 sattrs->sregion = 0;
11431 sattrs->srvalid = false;
11432 break;
11433 } else {
11434 if (env->sau.rlar[r] & 2) {
11435 sattrs->nsc = true;
11436 } else {
11437 sattrs->ns = true;
11438 }
11439 sattrs->srvalid = true;
11440 sattrs->sregion = r;
11441 }
9d2b5a58
PM
11442 } else {
11443 /*
11444 * Address not in this region. We must check whether the
11445 * region covers addresses in the same page as our address.
11446 * In that case we must not report a size that covers the
11447 * whole page for a subsequent hit against a different MPU
11448 * region or the background region, because it would result
11449 * in incorrect TLB hits for subsequent accesses to
11450 * addresses that are in this MPU region.
11451 */
11452 if (limit >= base &&
11453 ranges_overlap(base, limit - base + 1,
11454 addr_page_base,
11455 TARGET_PAGE_SIZE)) {
11456 sattrs->subpage = true;
11457 }
35337cc3
PM
11458 }
11459 }
11460 }
7e3f1223
TR
11461 break;
11462 }
35337cc3 11463
7e3f1223
TR
11464 /*
11465 * The IDAU will override the SAU lookup results if it specifies
11466 * higher security than the SAU does.
11467 */
11468 if (!idau_ns) {
11469 if (sattrs->ns || (!idau_nsc && sattrs->nsc)) {
11470 sattrs->ns = false;
11471 sattrs->nsc = idau_nsc;
181962fd 11472 }
35337cc3
PM
11473 }
11474}
11475
787a7e76 11476bool pmsav8_mpu_lookup(CPUARMState *env, uint32_t address,
54317c0f
PM
11477 MMUAccessType access_type, ARMMMUIdx mmu_idx,
11478 hwaddr *phys_ptr, MemTxAttrs *txattrs,
72042435
PM
11479 int *prot, bool *is_subpage,
11480 ARMMMUFaultInfo *fi, uint32_t *mregion)
54317c0f
PM
11481{
11482 /* Perform a PMSAv8 MPU lookup (without also doing the SAU check
11483 * that a full phys-to-virt translation does).
11484 * mregion is (if not NULL) set to the region number which matched,
11485 * or -1 if no region number is returned (MPU off, address did not
11486 * hit a region, address hit in multiple regions).
72042435
PM
11487 * We set is_subpage to true if the region hit doesn't cover the
11488 * entire TARGET_PAGE the address is within.
54317c0f 11489 */
2fc0cc0e 11490 ARMCPU *cpu = env_archcpu(env);
504e3cc3 11491 bool is_user = regime_is_user(env, mmu_idx);
62c58ee0 11492 uint32_t secure = regime_is_secure(env, mmu_idx);
504e3cc3
PM
11493 int n;
11494 int matchregion = -1;
11495 bool hit = false;
72042435
PM
11496 uint32_t addr_page_base = address & TARGET_PAGE_MASK;
11497 uint32_t addr_page_limit = addr_page_base + (TARGET_PAGE_SIZE - 1);
504e3cc3 11498
72042435 11499 *is_subpage = false;
504e3cc3
PM
11500 *phys_ptr = address;
11501 *prot = 0;
54317c0f
PM
11502 if (mregion) {
11503 *mregion = -1;
35337cc3
PM
11504 }
11505
504e3cc3
PM
11506 /* Unlike the ARM ARM pseudocode, we don't need to check whether this
11507 * was an exception vector read from the vector table (which is always
11508 * done using the default system address map), because those accesses
11509 * are done in arm_v7m_load_vector(), which always does a direct
11510 * read using address_space_ldl(), rather than going via this function.
11511 */
11512 if (regime_translation_disabled(env, mmu_idx)) { /* MPU disabled */
11513 hit = true;
11514 } else if (m_is_ppb_region(env, address)) {
11515 hit = true;
504e3cc3 11516 } else {
cff21316
PM
11517 if (pmsav7_use_background_region(cpu, mmu_idx, is_user)) {
11518 hit = true;
11519 }
11520
504e3cc3
PM
11521 for (n = (int)cpu->pmsav7_dregion - 1; n >= 0; n--) {
11522 /* region search */
11523 /* Note that the base address is bits [31:5] from the register
11524 * with bits [4:0] all zeroes, but the limit address is bits
11525 * [31:5] from the register with bits [4:0] all ones.
11526 */
62c58ee0
PM
11527 uint32_t base = env->pmsav8.rbar[secure][n] & ~0x1f;
11528 uint32_t limit = env->pmsav8.rlar[secure][n] | 0x1f;
504e3cc3 11529
62c58ee0 11530 if (!(env->pmsav8.rlar[secure][n] & 0x1)) {
504e3cc3
PM
11531 /* Region disabled */
11532 continue;
11533 }
11534
11535 if (address < base || address > limit) {
9d2b5a58
PM
11536 /*
11537 * Address not in this region. We must check whether the
11538 * region covers addresses in the same page as our address.
11539 * In that case we must not report a size that covers the
11540 * whole page for a subsequent hit against a different MPU
11541 * region or the background region, because it would result in
11542 * incorrect TLB hits for subsequent accesses to addresses that
11543 * are in this MPU region.
11544 */
11545 if (limit >= base &&
11546 ranges_overlap(base, limit - base + 1,
11547 addr_page_base,
11548 TARGET_PAGE_SIZE)) {
11549 *is_subpage = true;
11550 }
504e3cc3
PM
11551 continue;
11552 }
11553
72042435
PM
11554 if (base > addr_page_base || limit < addr_page_limit) {
11555 *is_subpage = true;
11556 }
11557
cff21316 11558 if (matchregion != -1) {
504e3cc3
PM
11559 /* Multiple regions match -- always a failure (unlike
11560 * PMSAv7 where highest-numbered-region wins)
11561 */
3f551b5b
PM
11562 fi->type = ARMFault_Permission;
11563 fi->level = 1;
504e3cc3
PM
11564 return true;
11565 }
11566
11567 matchregion = n;
11568 hit = true;
504e3cc3
PM
11569 }
11570 }
11571
11572 if (!hit) {
11573 /* background fault */
3f551b5b 11574 fi->type = ARMFault_Background;
504e3cc3
PM
11575 return true;
11576 }
11577
11578 if (matchregion == -1) {
11579 /* hit using the background region */
11580 get_phys_addr_pmsav7_default(env, mmu_idx, address, prot);
11581 } else {
62c58ee0
PM
11582 uint32_t ap = extract32(env->pmsav8.rbar[secure][matchregion], 1, 2);
11583 uint32_t xn = extract32(env->pmsav8.rbar[secure][matchregion], 0, 1);
504e3cc3
PM
11584
11585 if (m_is_system_region(env, address)) {
11586 /* System space is always execute never */
11587 xn = 1;
11588 }
11589
11590 *prot = simple_ap_to_rw_prot(env, mmu_idx, ap);
11591 if (*prot && !xn) {
11592 *prot |= PAGE_EXEC;
11593 }
11594 /* We don't need to look the attribute up in the MAIR0/MAIR1
11595 * registers because that only tells us about cacheability.
11596 */
54317c0f
PM
11597 if (mregion) {
11598 *mregion = matchregion;
11599 }
504e3cc3
PM
11600 }
11601
3f551b5b
PM
11602 fi->type = ARMFault_Permission;
11603 fi->level = 1;
504e3cc3
PM
11604 return !(*prot & (1 << access_type));
11605}
11606
54317c0f
PM
11607
11608static bool get_phys_addr_pmsav8(CPUARMState *env, uint32_t address,
11609 MMUAccessType access_type, ARMMMUIdx mmu_idx,
11610 hwaddr *phys_ptr, MemTxAttrs *txattrs,
72042435
PM
11611 int *prot, target_ulong *page_size,
11612 ARMMMUFaultInfo *fi)
54317c0f
PM
11613{
11614 uint32_t secure = regime_is_secure(env, mmu_idx);
11615 V8M_SAttributes sattrs = {};
72042435
PM
11616 bool ret;
11617 bool mpu_is_subpage;
54317c0f
PM
11618
11619 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
11620 v8m_security_lookup(env, address, access_type, mmu_idx, &sattrs);
11621 if (access_type == MMU_INST_FETCH) {
11622 /* Instruction fetches always use the MMU bank and the
11623 * transaction attribute determined by the fetch address,
11624 * regardless of CPU state. This is painful for QEMU
11625 * to handle, because it would mean we need to encode
11626 * into the mmu_idx not just the (user, negpri) information
11627 * for the current security state but also that for the
11628 * other security state, which would balloon the number
11629 * of mmu_idx values needed alarmingly.
11630 * Fortunately we can avoid this because it's not actually
11631 * possible to arbitrarily execute code from memory with
11632 * the wrong security attribute: it will always generate
11633 * an exception of some kind or another, apart from the
11634 * special case of an NS CPU executing an SG instruction
11635 * in S&NSC memory. So we always just fail the translation
11636 * here and sort things out in the exception handler
11637 * (including possibly emulating an SG instruction).
11638 */
11639 if (sattrs.ns != !secure) {
3f551b5b
PM
11640 if (sattrs.nsc) {
11641 fi->type = ARMFault_QEMU_NSCExec;
11642 } else {
11643 fi->type = ARMFault_QEMU_SFault;
11644 }
72042435 11645 *page_size = sattrs.subpage ? 1 : TARGET_PAGE_SIZE;
54317c0f
PM
11646 *phys_ptr = address;
11647 *prot = 0;
11648 return true;
11649 }
11650 } else {
11651 /* For data accesses we always use the MMU bank indicated
11652 * by the current CPU state, but the security attributes
11653 * might downgrade a secure access to nonsecure.
11654 */
11655 if (sattrs.ns) {
11656 txattrs->secure = false;
11657 } else if (!secure) {
11658 /* NS access to S memory must fault.
11659 * Architecturally we should first check whether the
11660 * MPU information for this address indicates that we
11661 * are doing an unaligned access to Device memory, which
11662 * should generate a UsageFault instead. QEMU does not
11663 * currently check for that kind of unaligned access though.
11664 * If we added it we would need to do so as a special case
11665 * for M_FAKE_FSR_SFAULT in arm_v7m_cpu_do_interrupt().
11666 */
3f551b5b 11667 fi->type = ARMFault_QEMU_SFault;
72042435 11668 *page_size = sattrs.subpage ? 1 : TARGET_PAGE_SIZE;
54317c0f
PM
11669 *phys_ptr = address;
11670 *prot = 0;
11671 return true;
11672 }
11673 }
11674 }
11675
72042435
PM
11676 ret = pmsav8_mpu_lookup(env, address, access_type, mmu_idx, phys_ptr,
11677 txattrs, prot, &mpu_is_subpage, fi, NULL);
72042435
PM
11678 *page_size = sattrs.subpage || mpu_is_subpage ? 1 : TARGET_PAGE_SIZE;
11679 return ret;
54317c0f
PM
11680}
11681
13689d43 11682static bool get_phys_addr_pmsav5(CPUARMState *env, uint32_t address,
03ae85f8 11683 MMUAccessType access_type, ARMMMUIdx mmu_idx,
53a4e5c5
PM
11684 hwaddr *phys_ptr, int *prot,
11685 ARMMMUFaultInfo *fi)
9ee6e8bb
PB
11686{
11687 int n;
11688 uint32_t mask;
11689 uint32_t base;
0480f69a 11690 bool is_user = regime_is_user(env, mmu_idx);
9ee6e8bb 11691
3279adb9
PM
11692 if (regime_translation_disabled(env, mmu_idx)) {
11693 /* MPU disabled. */
11694 *phys_ptr = address;
11695 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
11696 return false;
11697 }
11698
9ee6e8bb
PB
11699 *phys_ptr = address;
11700 for (n = 7; n >= 0; n--) {
554b0b09 11701 base = env->cp15.c6_region[n];
87c3d486 11702 if ((base & 1) == 0) {
554b0b09 11703 continue;
87c3d486 11704 }
554b0b09
PM
11705 mask = 1 << ((base >> 1) & 0x1f);
11706 /* Keep this shift separate from the above to avoid an
11707 (undefined) << 32. */
11708 mask = (mask << 1) - 1;
87c3d486 11709 if (((base ^ address) & ~mask) == 0) {
554b0b09 11710 break;
87c3d486 11711 }
9ee6e8bb 11712 }
87c3d486 11713 if (n < 0) {
53a4e5c5 11714 fi->type = ARMFault_Background;
b7cc4e82 11715 return true;
87c3d486 11716 }
9ee6e8bb 11717
03ae85f8 11718 if (access_type == MMU_INST_FETCH) {
7e09797c 11719 mask = env->cp15.pmsav5_insn_ap;
9ee6e8bb 11720 } else {
7e09797c 11721 mask = env->cp15.pmsav5_data_ap;
9ee6e8bb
PB
11722 }
11723 mask = (mask >> (n * 4)) & 0xf;
11724 switch (mask) {
11725 case 0:
53a4e5c5
PM
11726 fi->type = ARMFault_Permission;
11727 fi->level = 1;
b7cc4e82 11728 return true;
9ee6e8bb 11729 case 1:
87c3d486 11730 if (is_user) {
53a4e5c5
PM
11731 fi->type = ARMFault_Permission;
11732 fi->level = 1;
b7cc4e82 11733 return true;
87c3d486 11734 }
554b0b09
PM
11735 *prot = PAGE_READ | PAGE_WRITE;
11736 break;
9ee6e8bb 11737 case 2:
554b0b09 11738 *prot = PAGE_READ;
87c3d486 11739 if (!is_user) {
554b0b09 11740 *prot |= PAGE_WRITE;
87c3d486 11741 }
554b0b09 11742 break;
9ee6e8bb 11743 case 3:
554b0b09
PM
11744 *prot = PAGE_READ | PAGE_WRITE;
11745 break;
9ee6e8bb 11746 case 5:
87c3d486 11747 if (is_user) {
53a4e5c5
PM
11748 fi->type = ARMFault_Permission;
11749 fi->level = 1;
b7cc4e82 11750 return true;
87c3d486 11751 }
554b0b09
PM
11752 *prot = PAGE_READ;
11753 break;
9ee6e8bb 11754 case 6:
554b0b09
PM
11755 *prot = PAGE_READ;
11756 break;
9ee6e8bb 11757 default:
554b0b09 11758 /* Bad permission. */
53a4e5c5
PM
11759 fi->type = ARMFault_Permission;
11760 fi->level = 1;
b7cc4e82 11761 return true;
9ee6e8bb 11762 }
3ad493fc 11763 *prot |= PAGE_EXEC;
b7cc4e82 11764 return false;
9ee6e8bb
PB
11765}
11766
5b2d261d
AB
11767/* Combine either inner or outer cacheability attributes for normal
11768 * memory, according to table D4-42 and pseudocode procedure
11769 * CombineS1S2AttrHints() of ARM DDI 0487B.b (the ARMv8 ARM).
11770 *
11771 * NB: only stage 1 includes allocation hints (RW bits), leading to
11772 * some asymmetry.
11773 */
11774static uint8_t combine_cacheattr_nibble(uint8_t s1, uint8_t s2)
11775{
11776 if (s1 == 4 || s2 == 4) {
11777 /* non-cacheable has precedence */
11778 return 4;
11779 } else if (extract32(s1, 2, 2) == 0 || extract32(s1, 2, 2) == 2) {
11780 /* stage 1 write-through takes precedence */
11781 return s1;
11782 } else if (extract32(s2, 2, 2) == 2) {
11783 /* stage 2 write-through takes precedence, but the allocation hint
11784 * is still taken from stage 1
11785 */
11786 return (2 << 2) | extract32(s1, 0, 2);
11787 } else { /* write-back */
11788 return s1;
11789 }
11790}
11791
11792/* Combine S1 and S2 cacheability/shareability attributes, per D4.5.4
11793 * and CombineS1S2Desc()
11794 *
11795 * @s1: Attributes from stage 1 walk
11796 * @s2: Attributes from stage 2 walk
11797 */
11798static ARMCacheAttrs combine_cacheattrs(ARMCacheAttrs s1, ARMCacheAttrs s2)
11799{
11800 uint8_t s1lo = extract32(s1.attrs, 0, 4), s2lo = extract32(s2.attrs, 0, 4);
11801 uint8_t s1hi = extract32(s1.attrs, 4, 4), s2hi = extract32(s2.attrs, 4, 4);
11802 ARMCacheAttrs ret;
11803
11804 /* Combine shareability attributes (table D4-43) */
11805 if (s1.shareability == 2 || s2.shareability == 2) {
11806 /* if either are outer-shareable, the result is outer-shareable */
11807 ret.shareability = 2;
11808 } else if (s1.shareability == 3 || s2.shareability == 3) {
11809 /* if either are inner-shareable, the result is inner-shareable */
11810 ret.shareability = 3;
11811 } else {
11812 /* both non-shareable */
11813 ret.shareability = 0;
11814 }
11815
11816 /* Combine memory type and cacheability attributes */
11817 if (s1hi == 0 || s2hi == 0) {
11818 /* Device has precedence over normal */
11819 if (s1lo == 0 || s2lo == 0) {
11820 /* nGnRnE has precedence over anything */
11821 ret.attrs = 0;
11822 } else if (s1lo == 4 || s2lo == 4) {
11823 /* non-Reordering has precedence over Reordering */
11824 ret.attrs = 4; /* nGnRE */
11825 } else if (s1lo == 8 || s2lo == 8) {
11826 /* non-Gathering has precedence over Gathering */
11827 ret.attrs = 8; /* nGRE */
11828 } else {
11829 ret.attrs = 0xc; /* GRE */
11830 }
11831
11832 /* Any location for which the resultant memory type is any
11833 * type of Device memory is always treated as Outer Shareable.
11834 */
11835 ret.shareability = 2;
11836 } else { /* Normal memory */
11837 /* Outer/inner cacheability combine independently */
11838 ret.attrs = combine_cacheattr_nibble(s1hi, s2hi) << 4
11839 | combine_cacheattr_nibble(s1lo, s2lo);
11840
11841 if (ret.attrs == 0x44) {
11842 /* Any location for which the resultant memory type is Normal
11843 * Inner Non-cacheable, Outer Non-cacheable is always treated
11844 * as Outer Shareable.
11845 */
11846 ret.shareability = 2;
11847 }
11848 }
11849
11850 return ret;
11851}
11852
11853
702a9357
PM
11854/* get_phys_addr - get the physical address for this virtual address
11855 *
11856 * Find the physical address corresponding to the given virtual address,
11857 * by doing a translation table walk on MMU based systems or using the
11858 * MPU state on MPU based systems.
11859 *
b7cc4e82
PC
11860 * Returns false if the translation was successful. Otherwise, phys_ptr, attrs,
11861 * prot and page_size may not be filled in, and the populated fsr value provides
702a9357
PM
11862 * information on why the translation aborted, in the format of a
11863 * DFSR/IFSR fault register, with the following caveats:
11864 * * we honour the short vs long DFSR format differences.
11865 * * the WnR bit is never set (the caller must do this).
f6bda88f 11866 * * for PSMAv5 based systems we don't bother to return a full FSR format
702a9357
PM
11867 * value.
11868 *
11869 * @env: CPUARMState
11870 * @address: virtual address to get physical address for
11871 * @access_type: 0 for read, 1 for write, 2 for execute
d3649702 11872 * @mmu_idx: MMU index indicating required translation regime
702a9357 11873 * @phys_ptr: set to the physical address corresponding to the virtual address
8bf5b6a9 11874 * @attrs: set to the memory transaction attributes to use
702a9357
PM
11875 * @prot: set to the permissions for the page containing phys_ptr
11876 * @page_size: set to the size of the page containing phys_ptr
5b2d261d
AB
11877 * @fi: set to fault info if the translation fails
11878 * @cacheattrs: (if non-NULL) set to the cacheability/shareability attributes
702a9357 11879 */
ebae861f
PMD
11880bool get_phys_addr(CPUARMState *env, target_ulong address,
11881 MMUAccessType access_type, ARMMMUIdx mmu_idx,
11882 hwaddr *phys_ptr, MemTxAttrs *attrs, int *prot,
11883 target_ulong *page_size,
11884 ARMMMUFaultInfo *fi, ARMCacheAttrs *cacheattrs)
9ee6e8bb 11885{
452ef8cb
RH
11886 if (mmu_idx == ARMMMUIdx_E10_0 ||
11887 mmu_idx == ARMMMUIdx_E10_1 ||
11888 mmu_idx == ARMMMUIdx_E10_1_PAN) {
9b539263
EI
11889 /* Call ourselves recursively to do the stage 1 and then stage 2
11890 * translations.
0480f69a 11891 */
9b539263
EI
11892 if (arm_feature(env, ARM_FEATURE_EL2)) {
11893 hwaddr ipa;
11894 int s2_prot;
11895 int ret;
5b2d261d 11896 ARMCacheAttrs cacheattrs2 = {};
9b539263
EI
11897
11898 ret = get_phys_addr(env, address, access_type,
8bd5c820 11899 stage_1_mmu_idx(mmu_idx), &ipa, attrs,
bc52bfeb 11900 prot, page_size, fi, cacheattrs);
9b539263
EI
11901
11902 /* If S1 fails or S2 is disabled, return early. */
97fa9350 11903 if (ret || regime_translation_disabled(env, ARMMMUIdx_Stage2)) {
9b539263
EI
11904 *phys_ptr = ipa;
11905 return ret;
11906 }
11907
11908 /* S1 is done. Now do S2 translation. */
97fa9350 11909 ret = get_phys_addr_lpae(env, ipa, access_type, ARMMMUIdx_Stage2,
ff7de2fc 11910 mmu_idx == ARMMMUIdx_E10_0,
9b539263 11911 phys_ptr, attrs, &s2_prot,
da909b2c 11912 page_size, fi,
5b2d261d 11913 cacheattrs != NULL ? &cacheattrs2 : NULL);
9b539263
EI
11914 fi->s2addr = ipa;
11915 /* Combine the S1 and S2 perms. */
11916 *prot &= s2_prot;
5b2d261d
AB
11917
11918 /* Combine the S1 and S2 cache attributes, if needed */
11919 if (!ret && cacheattrs != NULL) {
9d1bab33
PM
11920 if (env->cp15.hcr_el2 & HCR_DC) {
11921 /*
11922 * HCR.DC forces the first stage attributes to
11923 * Normal Non-Shareable,
11924 * Inner Write-Back Read-Allocate Write-Allocate,
11925 * Outer Write-Back Read-Allocate Write-Allocate.
11926 */
11927 cacheattrs->attrs = 0xff;
11928 cacheattrs->shareability = 0;
11929 }
5b2d261d
AB
11930 *cacheattrs = combine_cacheattrs(*cacheattrs, cacheattrs2);
11931 }
11932
9b539263
EI
11933 return ret;
11934 } else {
11935 /*
11936 * For non-EL2 CPUs a stage1+stage2 translation is just stage 1.
11937 */
8bd5c820 11938 mmu_idx = stage_1_mmu_idx(mmu_idx);
9b539263 11939 }
0480f69a 11940 }
d3649702 11941
8bf5b6a9
PM
11942 /* The page table entries may downgrade secure to non-secure, but
11943 * cannot upgrade an non-secure translation regime's attributes
11944 * to secure.
11945 */
11946 attrs->secure = regime_is_secure(env, mmu_idx);
0995bf8c 11947 attrs->user = regime_is_user(env, mmu_idx);
8bf5b6a9 11948
0480f69a
PM
11949 /* Fast Context Switch Extension. This doesn't exist at all in v8.
11950 * In v7 and earlier it affects all stage 1 translations.
11951 */
97fa9350 11952 if (address < 0x02000000 && mmu_idx != ARMMMUIdx_Stage2
0480f69a
PM
11953 && !arm_feature(env, ARM_FEATURE_V8)) {
11954 if (regime_el(env, mmu_idx) == 3) {
11955 address += env->cp15.fcseidr_s;
11956 } else {
11957 address += env->cp15.fcseidr_ns;
11958 }
54bf36ed 11959 }
9ee6e8bb 11960
3279adb9 11961 if (arm_feature(env, ARM_FEATURE_PMSA)) {
c9f9f124 11962 bool ret;
f6bda88f 11963 *page_size = TARGET_PAGE_SIZE;
3279adb9 11964
504e3cc3
PM
11965 if (arm_feature(env, ARM_FEATURE_V8)) {
11966 /* PMSAv8 */
11967 ret = get_phys_addr_pmsav8(env, address, access_type, mmu_idx,
72042435 11968 phys_ptr, attrs, prot, page_size, fi);
504e3cc3 11969 } else if (arm_feature(env, ARM_FEATURE_V7)) {
3279adb9
PM
11970 /* PMSAv7 */
11971 ret = get_phys_addr_pmsav7(env, address, access_type, mmu_idx,
e5e40999 11972 phys_ptr, prot, page_size, fi);
3279adb9
PM
11973 } else {
11974 /* Pre-v7 MPU */
11975 ret = get_phys_addr_pmsav5(env, address, access_type, mmu_idx,
53a4e5c5 11976 phys_ptr, prot, fi);
3279adb9
PM
11977 }
11978 qemu_log_mask(CPU_LOG_MMU, "PMSA MPU lookup for %s at 0x%08" PRIx32
c9f9f124 11979 " mmu_idx %u -> %s (prot %c%c%c)\n",
709e4407
PM
11980 access_type == MMU_DATA_LOAD ? "reading" :
11981 (access_type == MMU_DATA_STORE ? "writing" : "execute"),
c9f9f124
MD
11982 (uint32_t)address, mmu_idx,
11983 ret ? "Miss" : "Hit",
11984 *prot & PAGE_READ ? 'r' : '-',
11985 *prot & PAGE_WRITE ? 'w' : '-',
11986 *prot & PAGE_EXEC ? 'x' : '-');
11987
11988 return ret;
f6bda88f
PC
11989 }
11990
3279adb9
PM
11991 /* Definitely a real MMU, not an MPU */
11992
0480f69a 11993 if (regime_translation_disabled(env, mmu_idx)) {
cebfb648
RH
11994 /*
11995 * MMU disabled. S1 addresses within aa64 translation regimes are
11996 * still checked for bounds -- see AArch64.TranslateAddressS1Off.
11997 */
11998 if (mmu_idx != ARMMMUIdx_Stage2) {
11999 int r_el = regime_el(env, mmu_idx);
12000 if (arm_el_is_aa64(env, r_el)) {
12001 int pamax = arm_pamax(env_archcpu(env));
12002 uint64_t tcr = env->cp15.tcr_el[r_el].raw_tcr;
12003 int addrtop, tbi;
12004
12005 tbi = aa64_va_parameter_tbi(tcr, mmu_idx);
12006 if (access_type == MMU_INST_FETCH) {
12007 tbi &= ~aa64_va_parameter_tbid(tcr, mmu_idx);
12008 }
12009 tbi = (tbi >> extract64(address, 55, 1)) & 1;
12010 addrtop = (tbi ? 55 : 63);
12011
12012 if (extract64(address, pamax, addrtop - pamax + 1) != 0) {
12013 fi->type = ARMFault_AddressSize;
12014 fi->level = 0;
12015 fi->stage2 = false;
12016 return 1;
12017 }
12018
12019 /*
12020 * When TBI is disabled, we've just validated that all of the
12021 * bits above PAMax are zero, so logically we only need to
12022 * clear the top byte for TBI. But it's clearer to follow
12023 * the pseudocode set of addrdesc.paddress.
12024 */
12025 address = extract64(address, 0, 52);
12026 }
12027 }
9ee6e8bb 12028 *phys_ptr = address;
3ad493fc 12029 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
d4c430a8 12030 *page_size = TARGET_PAGE_SIZE;
9ee6e8bb 12031 return 0;
0480f69a
PM
12032 }
12033
0480f69a 12034 if (regime_using_lpae_format(env, mmu_idx)) {
ff7de2fc 12035 return get_phys_addr_lpae(env, address, access_type, mmu_idx, false,
bc52bfeb
PM
12036 phys_ptr, attrs, prot, page_size,
12037 fi, cacheattrs);
0480f69a 12038 } else if (regime_sctlr(env, mmu_idx) & SCTLR_XP) {
bc52bfeb
PM
12039 return get_phys_addr_v6(env, address, access_type, mmu_idx,
12040 phys_ptr, attrs, prot, page_size, fi);
9ee6e8bb 12041 } else {
bc52bfeb 12042 return get_phys_addr_v5(env, address, access_type, mmu_idx,
f989983e 12043 phys_ptr, prot, page_size, fi);
9ee6e8bb
PB
12044 }
12045}
12046
0faea0c7
PM
12047hwaddr arm_cpu_get_phys_page_attrs_debug(CPUState *cs, vaddr addr,
12048 MemTxAttrs *attrs)
b5ff1b31 12049{
00b941e5 12050 ARMCPU *cpu = ARM_CPU(cs);
d3649702 12051 CPUARMState *env = &cpu->env;
a8170e5e 12052 hwaddr phys_addr;
d4c430a8 12053 target_ulong page_size;
b5ff1b31 12054 int prot;
b7cc4e82 12055 bool ret;
e14b5a23 12056 ARMMMUFaultInfo fi = {};
50494a27 12057 ARMMMUIdx mmu_idx = arm_mmu_idx(env);
b5ff1b31 12058
0faea0c7
PM
12059 *attrs = (MemTxAttrs) {};
12060
8bd5c820 12061 ret = get_phys_addr(env, addr, 0, mmu_idx, &phys_addr,
bc52bfeb 12062 attrs, &prot, &page_size, &fi, NULL);
b5ff1b31 12063
b7cc4e82 12064 if (ret) {
b5ff1b31 12065 return -1;
00b941e5 12066 }
b5ff1b31
FB
12067 return phys_addr;
12068}
12069
b5ff1b31 12070#endif
6ddbc6e4
PB
12071
12072/* Note that signed overflow is undefined in C. The following routines are
12073 careful to use unsigned types where modulo arithmetic is required.
12074 Failure to do so _will_ break on newer gcc. */
12075
12076/* Signed saturating arithmetic. */
12077
1654b2d6 12078/* Perform 16-bit signed saturating addition. */
6ddbc6e4
PB
12079static inline uint16_t add16_sat(uint16_t a, uint16_t b)
12080{
12081 uint16_t res;
12082
12083 res = a + b;
12084 if (((res ^ a) & 0x8000) && !((a ^ b) & 0x8000)) {
12085 if (a & 0x8000)
12086 res = 0x8000;
12087 else
12088 res = 0x7fff;
12089 }
12090 return res;
12091}
12092
1654b2d6 12093/* Perform 8-bit signed saturating addition. */
6ddbc6e4
PB
12094static inline uint8_t add8_sat(uint8_t a, uint8_t b)
12095{
12096 uint8_t res;
12097
12098 res = a + b;
12099 if (((res ^ a) & 0x80) && !((a ^ b) & 0x80)) {
12100 if (a & 0x80)
12101 res = 0x80;
12102 else
12103 res = 0x7f;
12104 }
12105 return res;
12106}
12107
1654b2d6 12108/* Perform 16-bit signed saturating subtraction. */
6ddbc6e4
PB
12109static inline uint16_t sub16_sat(uint16_t a, uint16_t b)
12110{
12111 uint16_t res;
12112
12113 res = a - b;
12114 if (((res ^ a) & 0x8000) && ((a ^ b) & 0x8000)) {
12115 if (a & 0x8000)
12116 res = 0x8000;
12117 else
12118 res = 0x7fff;
12119 }
12120 return res;
12121}
12122
1654b2d6 12123/* Perform 8-bit signed saturating subtraction. */
6ddbc6e4
PB
12124static inline uint8_t sub8_sat(uint8_t a, uint8_t b)
12125{
12126 uint8_t res;
12127
12128 res = a - b;
12129 if (((res ^ a) & 0x80) && ((a ^ b) & 0x80)) {
12130 if (a & 0x80)
12131 res = 0x80;
12132 else
12133 res = 0x7f;
12134 }
12135 return res;
12136}
12137
12138#define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16);
12139#define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16);
12140#define ADD8(a, b, n) RESULT(add8_sat(a, b), n, 8);
12141#define SUB8(a, b, n) RESULT(sub8_sat(a, b), n, 8);
12142#define PFX q
12143
12144#include "op_addsub.h"
12145
12146/* Unsigned saturating arithmetic. */
460a09c1 12147static inline uint16_t add16_usat(uint16_t a, uint16_t b)
6ddbc6e4
PB
12148{
12149 uint16_t res;
12150 res = a + b;
12151 if (res < a)
12152 res = 0xffff;
12153 return res;
12154}
12155
460a09c1 12156static inline uint16_t sub16_usat(uint16_t a, uint16_t b)
6ddbc6e4 12157{
4c4fd3f8 12158 if (a > b)
6ddbc6e4
PB
12159 return a - b;
12160 else
12161 return 0;
12162}
12163
12164static inline uint8_t add8_usat(uint8_t a, uint8_t b)
12165{
12166 uint8_t res;
12167 res = a + b;
12168 if (res < a)
12169 res = 0xff;
12170 return res;
12171}
12172
12173static inline uint8_t sub8_usat(uint8_t a, uint8_t b)
12174{
4c4fd3f8 12175 if (a > b)
6ddbc6e4
PB
12176 return a - b;
12177 else
12178 return 0;
12179}
12180
12181#define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16);
12182#define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16);
12183#define ADD8(a, b, n) RESULT(add8_usat(a, b), n, 8);
12184#define SUB8(a, b, n) RESULT(sub8_usat(a, b), n, 8);
12185#define PFX uq
12186
12187#include "op_addsub.h"
12188
12189/* Signed modulo arithmetic. */
12190#define SARITH16(a, b, n, op) do { \
12191 int32_t sum; \
db6e2e65 12192 sum = (int32_t)(int16_t)(a) op (int32_t)(int16_t)(b); \
6ddbc6e4
PB
12193 RESULT(sum, n, 16); \
12194 if (sum >= 0) \
12195 ge |= 3 << (n * 2); \
12196 } while(0)
12197
12198#define SARITH8(a, b, n, op) do { \
12199 int32_t sum; \
db6e2e65 12200 sum = (int32_t)(int8_t)(a) op (int32_t)(int8_t)(b); \
6ddbc6e4
PB
12201 RESULT(sum, n, 8); \
12202 if (sum >= 0) \
12203 ge |= 1 << n; \
12204 } while(0)
12205
12206
12207#define ADD16(a, b, n) SARITH16(a, b, n, +)
12208#define SUB16(a, b, n) SARITH16(a, b, n, -)
12209#define ADD8(a, b, n) SARITH8(a, b, n, +)
12210#define SUB8(a, b, n) SARITH8(a, b, n, -)
12211#define PFX s
12212#define ARITH_GE
12213
12214#include "op_addsub.h"
12215
12216/* Unsigned modulo arithmetic. */
12217#define ADD16(a, b, n) do { \
12218 uint32_t sum; \
12219 sum = (uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b); \
12220 RESULT(sum, n, 16); \
a87aa10b 12221 if ((sum >> 16) == 1) \
6ddbc6e4
PB
12222 ge |= 3 << (n * 2); \
12223 } while(0)
12224
12225#define ADD8(a, b, n) do { \
12226 uint32_t sum; \
12227 sum = (uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b); \
12228 RESULT(sum, n, 8); \
a87aa10b
AZ
12229 if ((sum >> 8) == 1) \
12230 ge |= 1 << n; \
6ddbc6e4
PB
12231 } while(0)
12232
12233#define SUB16(a, b, n) do { \
12234 uint32_t sum; \
12235 sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \
12236 RESULT(sum, n, 16); \
12237 if ((sum >> 16) == 0) \
12238 ge |= 3 << (n * 2); \
12239 } while(0)
12240
12241#define SUB8(a, b, n) do { \
12242 uint32_t sum; \
12243 sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \
12244 RESULT(sum, n, 8); \
12245 if ((sum >> 8) == 0) \
a87aa10b 12246 ge |= 1 << n; \
6ddbc6e4
PB
12247 } while(0)
12248
12249#define PFX u
12250#define ARITH_GE
12251
12252#include "op_addsub.h"
12253
12254/* Halved signed arithmetic. */
12255#define ADD16(a, b, n) \
12256 RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16)
12257#define SUB16(a, b, n) \
12258 RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16)
12259#define ADD8(a, b, n) \
12260 RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8)
12261#define SUB8(a, b, n) \
12262 RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8)
12263#define PFX sh
12264
12265#include "op_addsub.h"
12266
12267/* Halved unsigned arithmetic. */
12268#define ADD16(a, b, n) \
12269 RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16)
12270#define SUB16(a, b, n) \
12271 RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16)
12272#define ADD8(a, b, n) \
12273 RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8)
12274#define SUB8(a, b, n) \
12275 RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8)
12276#define PFX uh
12277
12278#include "op_addsub.h"
12279
12280static inline uint8_t do_usad(uint8_t a, uint8_t b)
12281{
12282 if (a > b)
12283 return a - b;
12284 else
12285 return b - a;
12286}
12287
12288/* Unsigned sum of absolute byte differences. */
12289uint32_t HELPER(usad8)(uint32_t a, uint32_t b)
12290{
12291 uint32_t sum;
12292 sum = do_usad(a, b);
12293 sum += do_usad(a >> 8, b >> 8);
12294 sum += do_usad(a >> 16, b >>16);
12295 sum += do_usad(a >> 24, b >> 24);
12296 return sum;
12297}
12298
12299/* For ARMv6 SEL instruction. */
12300uint32_t HELPER(sel_flags)(uint32_t flags, uint32_t a, uint32_t b)
12301{
12302 uint32_t mask;
12303
12304 mask = 0;
12305 if (flags & 1)
12306 mask |= 0xff;
12307 if (flags & 2)
12308 mask |= 0xff00;
12309 if (flags & 4)
12310 mask |= 0xff0000;
12311 if (flags & 8)
12312 mask |= 0xff000000;
12313 return (a & mask) | (b & ~mask);
12314}
12315
aa633469
PM
12316/* CRC helpers.
12317 * The upper bytes of val (above the number specified by 'bytes') must have
12318 * been zeroed out by the caller.
12319 */
eb0ecd5a
WN
12320uint32_t HELPER(crc32)(uint32_t acc, uint32_t val, uint32_t bytes)
12321{
12322 uint8_t buf[4];
12323
aa633469 12324 stl_le_p(buf, val);
eb0ecd5a
WN
12325
12326 /* zlib crc32 converts the accumulator and output to one's complement. */
12327 return crc32(acc ^ 0xffffffff, buf, bytes) ^ 0xffffffff;
12328}
12329
12330uint32_t HELPER(crc32c)(uint32_t acc, uint32_t val, uint32_t bytes)
12331{
12332 uint8_t buf[4];
12333
aa633469 12334 stl_le_p(buf, val);
eb0ecd5a
WN
12335
12336 /* Linux crc32c converts the output to one's complement. */
12337 return crc32c(acc, buf, bytes) ^ 0xffffffff;
12338}
a9e01311
RH
12339
12340/* Return the exception level to which FP-disabled exceptions should
12341 * be taken, or 0 if FP is enabled.
12342 */
ced31551 12343int fp_exception_el(CPUARMState *env, int cur_el)
a9e01311 12344{
55faa212 12345#ifndef CONFIG_USER_ONLY
a9e01311
RH
12346 /* CPACR and the CPTR registers don't exist before v6, so FP is
12347 * always accessible
12348 */
12349 if (!arm_feature(env, ARM_FEATURE_V6)) {
12350 return 0;
12351 }
12352
d87513c0
PM
12353 if (arm_feature(env, ARM_FEATURE_M)) {
12354 /* CPACR can cause a NOCP UsageFault taken to current security state */
12355 if (!v7m_cpacr_pass(env, env->v7m.secure, cur_el != 0)) {
12356 return 1;
12357 }
12358
12359 if (arm_feature(env, ARM_FEATURE_M_SECURITY) && !env->v7m.secure) {
12360 if (!extract32(env->v7m.nsacr, 10, 1)) {
12361 /* FP insns cause a NOCP UsageFault taken to Secure */
12362 return 3;
12363 }
12364 }
12365
12366 return 0;
12367 }
12368
a9e01311
RH
12369 /* The CPACR controls traps to EL1, or PL1 if we're 32 bit:
12370 * 0, 2 : trap EL0 and EL1/PL1 accesses
12371 * 1 : trap only EL0 accesses
12372 * 3 : trap no accesses
c2ddb7cf 12373 * This register is ignored if E2H+TGE are both set.
a9e01311 12374 */
c2ddb7cf
RH
12375 if ((arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
12376 int fpen = extract32(env->cp15.cpacr_el1, 20, 2);
12377
12378 switch (fpen) {
12379 case 0:
12380 case 2:
12381 if (cur_el == 0 || cur_el == 1) {
12382 /* Trap to PL1, which might be EL1 or EL3 */
12383 if (arm_is_secure(env) && !arm_el_is_aa64(env, 3)) {
12384 return 3;
12385 }
12386 return 1;
12387 }
12388 if (cur_el == 3 && !is_a64(env)) {
12389 /* Secure PL1 running at EL3 */
a9e01311
RH
12390 return 3;
12391 }
c2ddb7cf
RH
12392 break;
12393 case 1:
12394 if (cur_el == 0) {
12395 return 1;
12396 }
12397 break;
12398 case 3:
12399 break;
a9e01311 12400 }
a9e01311
RH
12401 }
12402
fc1120a7
PM
12403 /*
12404 * The NSACR allows A-profile AArch32 EL3 and M-profile secure mode
12405 * to control non-secure access to the FPU. It doesn't have any
12406 * effect if EL3 is AArch64 or if EL3 doesn't exist at all.
12407 */
12408 if ((arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
12409 cur_el <= 2 && !arm_is_secure_below_el3(env))) {
12410 if (!extract32(env->cp15.nsacr, 10, 1)) {
12411 /* FP insns act as UNDEF */
12412 return cur_el == 2 ? 2 : 1;
12413 }
12414 }
12415
a9e01311
RH
12416 /* For the CPTR registers we don't need to guard with an ARM_FEATURE
12417 * check because zero bits in the registers mean "don't trap".
12418 */
12419
12420 /* CPTR_EL2 : present in v7VE or v8 */
12421 if (cur_el <= 2 && extract32(env->cp15.cptr_el[2], 10, 1)
12422 && !arm_is_secure_below_el3(env)) {
12423 /* Trap FP ops at EL2, NS-EL1 or NS-EL0 to EL2 */
12424 return 2;
12425 }
12426
12427 /* CPTR_EL3 : present in v8 */
12428 if (extract32(env->cp15.cptr_el[3], 10, 1)) {
12429 /* Trap all FP ops to EL3 */
12430 return 3;
12431 }
55faa212 12432#endif
a9e01311
RH
12433 return 0;
12434}
12435
b9f6033c
RH
12436/* Return the exception level we're running at if this is our mmu_idx */
12437int arm_mmu_idx_to_el(ARMMMUIdx mmu_idx)
12438{
12439 if (mmu_idx & ARM_MMU_IDX_M) {
12440 return mmu_idx & ARM_MMU_IDX_M_PRIV;
12441 }
12442
12443 switch (mmu_idx) {
12444 case ARMMMUIdx_E10_0:
12445 case ARMMMUIdx_E20_0:
12446 case ARMMMUIdx_SE10_0:
12447 return 0;
12448 case ARMMMUIdx_E10_1:
452ef8cb 12449 case ARMMMUIdx_E10_1_PAN:
b9f6033c 12450 case ARMMMUIdx_SE10_1:
452ef8cb 12451 case ARMMMUIdx_SE10_1_PAN:
b9f6033c
RH
12452 return 1;
12453 case ARMMMUIdx_E2:
12454 case ARMMMUIdx_E20_2:
452ef8cb 12455 case ARMMMUIdx_E20_2_PAN:
b9f6033c
RH
12456 return 2;
12457 case ARMMMUIdx_SE3:
12458 return 3;
12459 default:
12460 g_assert_not_reached();
12461 }
12462}
12463
7aab5a8c 12464#ifndef CONFIG_TCG
65e4655c
RH
12465ARMMMUIdx arm_v7m_mmu_idx_for_secstate(CPUARMState *env, bool secstate)
12466{
7aab5a8c 12467 g_assert_not_reached();
65e4655c 12468}
7aab5a8c 12469#endif
65e4655c 12470
164690b2 12471ARMMMUIdx arm_mmu_idx_el(CPUARMState *env, int el)
65e4655c 12472{
65e4655c 12473 if (arm_feature(env, ARM_FEATURE_M)) {
50494a27 12474 return arm_v7m_mmu_idx_for_secstate(env, env->v7m.secure);
65e4655c
RH
12475 }
12476
6003d980 12477 /* See ARM pseudo-function ELIsInHost. */
b9f6033c
RH
12478 switch (el) {
12479 case 0:
b9f6033c
RH
12480 if (arm_is_secure_below_el3(env)) {
12481 return ARMMMUIdx_SE10_0;
12482 }
6003d980
RH
12483 if ((env->cp15.hcr_el2 & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)
12484 && arm_el_is_aa64(env, 2)) {
12485 return ARMMMUIdx_E20_0;
12486 }
b9f6033c
RH
12487 return ARMMMUIdx_E10_0;
12488 case 1:
12489 if (arm_is_secure_below_el3(env)) {
66412260
RH
12490 if (env->pstate & PSTATE_PAN) {
12491 return ARMMMUIdx_SE10_1_PAN;
12492 }
b9f6033c
RH
12493 return ARMMMUIdx_SE10_1;
12494 }
66412260
RH
12495 if (env->pstate & PSTATE_PAN) {
12496 return ARMMMUIdx_E10_1_PAN;
12497 }
b9f6033c
RH
12498 return ARMMMUIdx_E10_1;
12499 case 2:
b9f6033c 12500 /* TODO: ARMv8.4-SecEL2 */
6003d980
RH
12501 /* Note that TGE does not apply at EL2. */
12502 if ((env->cp15.hcr_el2 & HCR_E2H) && arm_el_is_aa64(env, 2)) {
66412260
RH
12503 if (env->pstate & PSTATE_PAN) {
12504 return ARMMMUIdx_E20_2_PAN;
12505 }
6003d980
RH
12506 return ARMMMUIdx_E20_2;
12507 }
b9f6033c
RH
12508 return ARMMMUIdx_E2;
12509 case 3:
12510 return ARMMMUIdx_SE3;
12511 default:
12512 g_assert_not_reached();
65e4655c 12513 }
50494a27
RH
12514}
12515
164690b2
RH
12516ARMMMUIdx arm_mmu_idx(CPUARMState *env)
12517{
12518 return arm_mmu_idx_el(env, arm_current_el(env));
12519}
12520
64be86ab
RH
12521#ifndef CONFIG_USER_ONLY
12522ARMMMUIdx arm_stage1_mmu_idx(CPUARMState *env)
12523{
12524 return stage_1_mmu_idx(arm_mmu_idx(env));
12525}
12526#endif
12527
fdd1b228
RH
12528static uint32_t rebuild_hflags_common(CPUARMState *env, int fp_el,
12529 ARMMMUIdx mmu_idx, uint32_t flags)
12530{
12531 flags = FIELD_DP32(flags, TBFLAG_ANY, FPEXC_EL, fp_el);
12532 flags = FIELD_DP32(flags, TBFLAG_ANY, MMUIDX,
12533 arm_to_core_mmu_idx(mmu_idx));
12534
fdd1b228
RH
12535 if (arm_singlestep_active(env)) {
12536 flags = FIELD_DP32(flags, TBFLAG_ANY, SS_ACTIVE, 1);
12537 }
12538 return flags;
12539}
12540
43eccfb6
RH
12541static uint32_t rebuild_hflags_common_32(CPUARMState *env, int fp_el,
12542 ARMMMUIdx mmu_idx, uint32_t flags)
12543{
8061a649
RH
12544 bool sctlr_b = arm_sctlr_b(env);
12545
12546 if (sctlr_b) {
12547 flags = FIELD_DP32(flags, TBFLAG_A32, SCTLR_B, 1);
12548 }
12549 if (arm_cpu_data_is_big_endian_a32(env, sctlr_b)) {
12550 flags = FIELD_DP32(flags, TBFLAG_ANY, BE_DATA, 1);
12551 }
43eccfb6
RH
12552 flags = FIELD_DP32(flags, TBFLAG_A32, NS, !access_secure_reg(env));
12553
12554 return rebuild_hflags_common(env, fp_el, mmu_idx, flags);
12555}
12556
6e33ced5
RH
12557static uint32_t rebuild_hflags_m32(CPUARMState *env, int fp_el,
12558 ARMMMUIdx mmu_idx)
12559{
12560 uint32_t flags = 0;
12561
12562 if (arm_v7m_is_handler_mode(env)) {
79cabf1f 12563 flags = FIELD_DP32(flags, TBFLAG_M32, HANDLER, 1);
6e33ced5
RH
12564 }
12565
12566 /*
12567 * v8M always applies stack limit checks unless CCR.STKOFHFNMIGN
12568 * is suppressing them because the requested execution priority
12569 * is less than 0.
12570 */
12571 if (arm_feature(env, ARM_FEATURE_V8) &&
12572 !((mmu_idx & ARM_MMU_IDX_M_NEGPRI) &&
12573 (env->v7m.ccr[env->v7m.secure] & R_V7M_CCR_STKOFHFNMIGN_MASK))) {
79cabf1f 12574 flags = FIELD_DP32(flags, TBFLAG_M32, STACKCHECK, 1);
6e33ced5
RH
12575 }
12576
12577 return rebuild_hflags_common_32(env, fp_el, mmu_idx, flags);
12578}
12579
83f4baef
RH
12580static uint32_t rebuild_hflags_aprofile(CPUARMState *env)
12581{
12582 int flags = 0;
12583
12584 flags = FIELD_DP32(flags, TBFLAG_ANY, DEBUG_TARGET_EL,
12585 arm_debug_target_el(env));
12586 return flags;
12587}
12588
c747224c
RH
12589static uint32_t rebuild_hflags_a32(CPUARMState *env, int fp_el,
12590 ARMMMUIdx mmu_idx)
12591{
83f4baef 12592 uint32_t flags = rebuild_hflags_aprofile(env);
0a54d68e
RH
12593
12594 if (arm_el_is_aa64(env, 1)) {
12595 flags = FIELD_DP32(flags, TBFLAG_A32, VFPEN, 1);
12596 }
5bb0a20b
MZ
12597
12598 if (arm_current_el(env) < 2 && env->cp15.hstr_el2 &&
12599 (arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
12600 flags = FIELD_DP32(flags, TBFLAG_A32, HSTR_ACTIVE, 1);
12601 }
12602
83f4baef 12603 return rebuild_hflags_common_32(env, fp_el, mmu_idx, flags);
c747224c
RH
12604}
12605
d4d7503a
RH
12606static uint32_t rebuild_hflags_a64(CPUARMState *env, int el, int fp_el,
12607 ARMMMUIdx mmu_idx)
a9e01311 12608{
83f4baef 12609 uint32_t flags = rebuild_hflags_aprofile(env);
d4d7503a 12610 ARMMMUIdx stage1 = stage_1_mmu_idx(mmu_idx);
b830a5ee 12611 uint64_t tcr = regime_tcr(env, mmu_idx)->raw_tcr;
d4d7503a
RH
12612 uint64_t sctlr;
12613 int tbii, tbid;
b9adaa70 12614
d4d7503a 12615 flags = FIELD_DP32(flags, TBFLAG_ANY, AARCH64_STATE, 1);
cd208a1c 12616
339370b9 12617 /* Get control bits for tagged addresses. */
b830a5ee
RH
12618 tbid = aa64_va_parameter_tbi(tcr, mmu_idx);
12619 tbii = tbid & ~aa64_va_parameter_tbid(tcr, mmu_idx);
5d8634f5 12620
d4d7503a
RH
12621 flags = FIELD_DP32(flags, TBFLAG_A64, TBII, tbii);
12622 flags = FIELD_DP32(flags, TBFLAG_A64, TBID, tbid);
12623
12624 if (cpu_isar_feature(aa64_sve, env_archcpu(env))) {
12625 int sve_el = sve_exception_el(env, el);
12626 uint32_t zcr_len;
5d8634f5 12627
d4d7503a
RH
12628 /*
12629 * If SVE is disabled, but FP is enabled,
12630 * then the effective len is 0.
12631 */
12632 if (sve_el != 0 && fp_el == 0) {
12633 zcr_len = 0;
12634 } else {
12635 zcr_len = sve_zcr_len_for_el(env, el);
5d8634f5 12636 }
d4d7503a
RH
12637 flags = FIELD_DP32(flags, TBFLAG_A64, SVEEXC_EL, sve_el);
12638 flags = FIELD_DP32(flags, TBFLAG_A64, ZCR_LEN, zcr_len);
12639 }
1db5e96c 12640
aaec1432 12641 sctlr = regime_sctlr(env, stage1);
1db5e96c 12642
8061a649
RH
12643 if (arm_cpu_data_is_big_endian_a64(el, sctlr)) {
12644 flags = FIELD_DP32(flags, TBFLAG_ANY, BE_DATA, 1);
12645 }
12646
d4d7503a
RH
12647 if (cpu_isar_feature(aa64_pauth, env_archcpu(env))) {
12648 /*
12649 * In order to save space in flags, we record only whether
12650 * pauth is "inactive", meaning all insns are implemented as
12651 * a nop, or "active" when some action must be performed.
12652 * The decision of which action to take is left to a helper.
12653 */
12654 if (sctlr & (SCTLR_EnIA | SCTLR_EnIB | SCTLR_EnDA | SCTLR_EnDB)) {
12655 flags = FIELD_DP32(flags, TBFLAG_A64, PAUTH_ACTIVE, 1);
1db5e96c 12656 }
d4d7503a 12657 }
0816ef1b 12658
d4d7503a
RH
12659 if (cpu_isar_feature(aa64_bti, env_archcpu(env))) {
12660 /* Note that SCTLR_EL[23].BT == SCTLR_BT1. */
12661 if (sctlr & (el == 0 ? SCTLR_BT0 : SCTLR_BT1)) {
12662 flags = FIELD_DP32(flags, TBFLAG_A64, BT, 1);
0816ef1b 12663 }
d4d7503a 12664 }
08f1434a 12665
cc28fc30 12666 /* Compute the condition for using AccType_UNPRIV for LDTR et al. */
7a8014ab
RH
12667 if (!(env->pstate & PSTATE_UAO)) {
12668 switch (mmu_idx) {
12669 case ARMMMUIdx_E10_1:
12670 case ARMMMUIdx_E10_1_PAN:
12671 case ARMMMUIdx_SE10_1:
12672 case ARMMMUIdx_SE10_1_PAN:
12673 /* TODO: ARMv8.3-NV */
cc28fc30 12674 flags = FIELD_DP32(flags, TBFLAG_A64, UNPRIV, 1);
7a8014ab
RH
12675 break;
12676 case ARMMMUIdx_E20_2:
12677 case ARMMMUIdx_E20_2_PAN:
12678 /* TODO: ARMv8.4-SecEL2 */
12679 /*
12680 * Note that EL20_2 is gated by HCR_EL2.E2H == 1, but EL20_0 is
12681 * gated by HCR_EL2.<E2H,TGE> == '11', and so is LDTR.
12682 */
12683 if (env->cp15.hcr_el2 & HCR_TGE) {
12684 flags = FIELD_DP32(flags, TBFLAG_A64, UNPRIV, 1);
12685 }
12686 break;
12687 default:
12688 break;
cc28fc30 12689 }
cc28fc30
RH
12690 }
12691
81ae05fa
RH
12692 if (cpu_isar_feature(aa64_mte, env_archcpu(env))) {
12693 /*
12694 * Set MTE_ACTIVE if any access may be Checked, and leave clear
12695 * if all accesses must be Unchecked:
12696 * 1) If no TBI, then there are no tags in the address to check,
12697 * 2) If Tag Check Override, then all accesses are Unchecked,
12698 * 3) If Tag Check Fail == 0, then Checked access have no effect,
12699 * 4) If no Allocation Tag Access, then all accesses are Unchecked.
12700 */
12701 if (allocation_tag_access_enabled(env, el, sctlr)) {
12702 flags = FIELD_DP32(flags, TBFLAG_A64, ATA, 1);
12703 if (tbid
12704 && !(env->pstate & PSTATE_TCO)
12705 && (sctlr & (el == 0 ? SCTLR_TCF0 : SCTLR_TCF))) {
12706 flags = FIELD_DP32(flags, TBFLAG_A64, MTE_ACTIVE, 1);
12707 }
12708 }
12709 /* And again for unprivileged accesses, if required. */
12710 if (FIELD_EX32(flags, TBFLAG_A64, UNPRIV)
12711 && tbid
12712 && !(env->pstate & PSTATE_TCO)
12713 && (sctlr & SCTLR_TCF0)
12714 && allocation_tag_access_enabled(env, 0, sctlr)) {
12715 flags = FIELD_DP32(flags, TBFLAG_A64, MTE0_ACTIVE, 1);
12716 }
12717 /* Cache TCMA as well as TBI. */
12718 flags = FIELD_DP32(flags, TBFLAG_A64, TCMA,
12719 aa64_va_parameter_tcma(tcr, mmu_idx));
12720 }
12721
d4d7503a
RH
12722 return rebuild_hflags_common(env, fp_el, mmu_idx, flags);
12723}
12724
3d74e2e9
RH
12725static uint32_t rebuild_hflags_internal(CPUARMState *env)
12726{
12727 int el = arm_current_el(env);
12728 int fp_el = fp_exception_el(env, el);
164690b2 12729 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
3d74e2e9
RH
12730
12731 if (is_a64(env)) {
12732 return rebuild_hflags_a64(env, el, fp_el, mmu_idx);
12733 } else if (arm_feature(env, ARM_FEATURE_M)) {
12734 return rebuild_hflags_m32(env, fp_el, mmu_idx);
12735 } else {
12736 return rebuild_hflags_a32(env, fp_el, mmu_idx);
12737 }
12738}
12739
12740void arm_rebuild_hflags(CPUARMState *env)
12741{
12742 env->hflags = rebuild_hflags_internal(env);
12743}
12744
19717e9b
PM
12745/*
12746 * If we have triggered a EL state change we can't rely on the
12747 * translator having passed it to us, we need to recompute.
12748 */
12749void HELPER(rebuild_hflags_m32_newel)(CPUARMState *env)
12750{
12751 int el = arm_current_el(env);
12752 int fp_el = fp_exception_el(env, el);
12753 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
12754 env->hflags = rebuild_hflags_m32(env, fp_el, mmu_idx);
12755}
12756
14f3c588
RH
12757void HELPER(rebuild_hflags_m32)(CPUARMState *env, int el)
12758{
12759 int fp_el = fp_exception_el(env, el);
12760 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
12761
12762 env->hflags = rebuild_hflags_m32(env, fp_el, mmu_idx);
12763}
12764
f80741d1
AB
12765/*
12766 * If we have triggered a EL state change we can't rely on the
563152e0 12767 * translator having passed it to us, we need to recompute.
f80741d1
AB
12768 */
12769void HELPER(rebuild_hflags_a32_newel)(CPUARMState *env)
12770{
12771 int el = arm_current_el(env);
12772 int fp_el = fp_exception_el(env, el);
12773 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
12774 env->hflags = rebuild_hflags_a32(env, fp_el, mmu_idx);
12775}
12776
14f3c588
RH
12777void HELPER(rebuild_hflags_a32)(CPUARMState *env, int el)
12778{
12779 int fp_el = fp_exception_el(env, el);
12780 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
12781
12782 env->hflags = rebuild_hflags_a32(env, fp_el, mmu_idx);
12783}
12784
12785void HELPER(rebuild_hflags_a64)(CPUARMState *env, int el)
12786{
12787 int fp_el = fp_exception_el(env, el);
12788 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
12789
12790 env->hflags = rebuild_hflags_a64(env, el, fp_el, mmu_idx);
12791}
12792
0ee8b24a
PMD
12793static inline void assert_hflags_rebuild_correctly(CPUARMState *env)
12794{
12795#ifdef CONFIG_DEBUG_TCG
12796 uint32_t env_flags_current = env->hflags;
12797 uint32_t env_flags_rebuilt = rebuild_hflags_internal(env);
12798
12799 if (unlikely(env_flags_current != env_flags_rebuilt)) {
12800 fprintf(stderr, "TCG hflags mismatch (current:0x%08x rebuilt:0x%08x)\n",
12801 env_flags_current, env_flags_rebuilt);
12802 abort();
12803 }
12804#endif
12805}
12806
d4d7503a
RH
12807void cpu_get_tb_cpu_state(CPUARMState *env, target_ulong *pc,
12808 target_ulong *cs_base, uint32_t *pflags)
12809{
e979972a
RH
12810 uint32_t flags = env->hflags;
12811 uint32_t pstate_for_ss;
d4d7503a 12812
9b253fe5 12813 *cs_base = 0;
0ee8b24a 12814 assert_hflags_rebuild_correctly(env);
3d74e2e9 12815
e979972a 12816 if (FIELD_EX32(flags, TBFLAG_ANY, AARCH64_STATE)) {
d4d7503a 12817 *pc = env->pc;
d4d7503a 12818 if (cpu_isar_feature(aa64_bti, env_archcpu(env))) {
08f1434a
RH
12819 flags = FIELD_DP32(flags, TBFLAG_A64, BTYPE, env->btype);
12820 }
60e12c37 12821 pstate_for_ss = env->pstate;
a9e01311
RH
12822 } else {
12823 *pc = env->regs[15];
6e33ced5
RH
12824
12825 if (arm_feature(env, ARM_FEATURE_M)) {
9550d1bd
RH
12826 if (arm_feature(env, ARM_FEATURE_M_SECURITY) &&
12827 FIELD_EX32(env->v7m.fpccr[M_REG_S], V7M_FPCCR, S)
12828 != env->v7m.secure) {
79cabf1f 12829 flags = FIELD_DP32(flags, TBFLAG_M32, FPCCR_S_WRONG, 1);
9550d1bd
RH
12830 }
12831
12832 if ((env->v7m.fpccr[env->v7m.secure] & R_V7M_FPCCR_ASPEN_MASK) &&
12833 (!(env->v7m.control[M_REG_S] & R_V7M_CONTROL_FPCA_MASK) ||
12834 (env->v7m.secure &&
12835 !(env->v7m.control[M_REG_S] & R_V7M_CONTROL_SFPA_MASK)))) {
12836 /*
12837 * ASPEN is set, but FPCA/SFPA indicate that there is no
12838 * active FP context; we must create a new FP context before
12839 * executing any FP insn.
12840 */
79cabf1f 12841 flags = FIELD_DP32(flags, TBFLAG_M32, NEW_FP_CTXT_NEEDED, 1);
9550d1bd
RH
12842 }
12843
12844 bool is_secure = env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_S_MASK;
12845 if (env->v7m.fpccr[is_secure] & R_V7M_FPCCR_LSPACT_MASK) {
79cabf1f 12846 flags = FIELD_DP32(flags, TBFLAG_M32, LSPACT, 1);
9550d1bd 12847 }
6e33ced5 12848 } else {
bbad7c62
RH
12849 /*
12850 * Note that XSCALE_CPAR shares bits with VECSTRIDE.
12851 * Note that VECLEN+VECSTRIDE are RES0 for M-profile.
12852 */
12853 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
12854 flags = FIELD_DP32(flags, TBFLAG_A32,
12855 XSCALE_CPAR, env->cp15.c15_cpar);
12856 } else {
12857 flags = FIELD_DP32(flags, TBFLAG_A32, VECLEN,
12858 env->vfp.vec_len);
12859 flags = FIELD_DP32(flags, TBFLAG_A32, VECSTRIDE,
12860 env->vfp.vec_stride);
12861 }
0a54d68e
RH
12862 if (env->vfp.xregs[ARM_VFP_FPEXC] & (1 << 30)) {
12863 flags = FIELD_DP32(flags, TBFLAG_A32, VFPEN, 1);
12864 }
6e33ced5
RH
12865 }
12866
79cabf1f
RH
12867 flags = FIELD_DP32(flags, TBFLAG_AM32, THUMB, env->thumb);
12868 flags = FIELD_DP32(flags, TBFLAG_AM32, CONDEXEC, env->condexec_bits);
60e12c37 12869 pstate_for_ss = env->uncached_cpsr;
d4d7503a 12870 }
a9e01311 12871
60e12c37
RH
12872 /*
12873 * The SS_ACTIVE and PSTATE_SS bits correspond to the state machine
a9e01311
RH
12874 * states defined in the ARM ARM for software singlestep:
12875 * SS_ACTIVE PSTATE.SS State
12876 * 0 x Inactive (the TB flag for SS is always 0)
12877 * 1 0 Active-pending
12878 * 1 1 Active-not-pending
fdd1b228 12879 * SS_ACTIVE is set in hflags; PSTATE_SS is computed every TB.
a9e01311 12880 */
60e12c37
RH
12881 if (FIELD_EX32(flags, TBFLAG_ANY, SS_ACTIVE) &&
12882 (pstate_for_ss & PSTATE_SS)) {
12883 flags = FIELD_DP32(flags, TBFLAG_ANY, PSTATE_SS, 1);
a9e01311 12884 }
a9e01311 12885
b9adaa70 12886 *pflags = flags;
a9e01311 12887}
0ab5953b
RH
12888
12889#ifdef TARGET_AARCH64
12890/*
12891 * The manual says that when SVE is enabled and VQ is widened the
12892 * implementation is allowed to zero the previously inaccessible
12893 * portion of the registers. The corollary to that is that when
12894 * SVE is enabled and VQ is narrowed we are also allowed to zero
12895 * the now inaccessible portion of the registers.
12896 *
12897 * The intent of this is that no predicate bit beyond VQ is ever set.
12898 * Which means that some operations on predicate registers themselves
12899 * may operate on full uint64_t or even unrolled across the maximum
12900 * uint64_t[4]. Performing 4 bits of host arithmetic unconditionally
12901 * may well be cheaper than conditionals to restrict the operation
12902 * to the relevant portion of a uint16_t[16].
12903 */
12904void aarch64_sve_narrow_vq(CPUARMState *env, unsigned vq)
12905{
12906 int i, j;
12907 uint64_t pmask;
12908
12909 assert(vq >= 1 && vq <= ARM_MAX_VQ);
2fc0cc0e 12910 assert(vq <= env_archcpu(env)->sve_max_vq);
0ab5953b
RH
12911
12912 /* Zap the high bits of the zregs. */
12913 for (i = 0; i < 32; i++) {
12914 memset(&env->vfp.zregs[i].d[2 * vq], 0, 16 * (ARM_MAX_VQ - vq));
12915 }
12916
12917 /* Zap the high bits of the pregs and ffr. */
12918 pmask = 0;
12919 if (vq & 3) {
12920 pmask = ~(-1ULL << (16 * (vq & 3)));
12921 }
12922 for (j = vq / 4; j < ARM_MAX_VQ / 4; j++) {
12923 for (i = 0; i < 17; ++i) {
12924 env->vfp.pregs[i].p[j] &= pmask;
12925 }
12926 pmask = 0;
12927 }
12928}
12929
12930/*
12931 * Notice a change in SVE vector size when changing EL.
12932 */
9a05f7b6
RH
12933void aarch64_sve_change_el(CPUARMState *env, int old_el,
12934 int new_el, bool el0_a64)
0ab5953b 12935{
2fc0cc0e 12936 ARMCPU *cpu = env_archcpu(env);
0ab5953b 12937 int old_len, new_len;
9a05f7b6 12938 bool old_a64, new_a64;
0ab5953b
RH
12939
12940 /* Nothing to do if no SVE. */
cd208a1c 12941 if (!cpu_isar_feature(aa64_sve, cpu)) {
0ab5953b
RH
12942 return;
12943 }
12944
12945 /* Nothing to do if FP is disabled in either EL. */
12946 if (fp_exception_el(env, old_el) || fp_exception_el(env, new_el)) {
12947 return;
12948 }
12949
12950 /*
12951 * DDI0584A.d sec 3.2: "If SVE instructions are disabled or trapped
12952 * at ELx, or not available because the EL is in AArch32 state, then
12953 * for all purposes other than a direct read, the ZCR_ELx.LEN field
12954 * has an effective value of 0".
12955 *
12956 * Consider EL2 (aa64, vq=4) -> EL0 (aa32) -> EL1 (aa64, vq=0).
12957 * If we ignore aa32 state, we would fail to see the vq4->vq0 transition
12958 * from EL2->EL1. Thus we go ahead and narrow when entering aa32 so that
12959 * we already have the correct register contents when encountering the
12960 * vq0->vq0 transition between EL0->EL1.
12961 */
9a05f7b6
RH
12962 old_a64 = old_el ? arm_el_is_aa64(env, old_el) : el0_a64;
12963 old_len = (old_a64 && !sve_exception_el(env, old_el)
0ab5953b 12964 ? sve_zcr_len_for_el(env, old_el) : 0);
9a05f7b6
RH
12965 new_a64 = new_el ? arm_el_is_aa64(env, new_el) : el0_a64;
12966 new_len = (new_a64 && !sve_exception_el(env, new_el)
0ab5953b
RH
12967 ? sve_zcr_len_for_el(env, new_el) : 0);
12968
12969 /* When changing vector length, clear inaccessible state. */
12970 if (new_len < old_len) {
12971 aarch64_sve_narrow_vq(env, new_len + 1);
12972 }
12973}
12974#endif