]> git.proxmox.com Git - mirror_qemu.git/blame - target/arm/helper.c
target/arm: Complete TBI clearing for user-only for SVE
[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 },
5463df16
RH
6932 { .name = "DC_IGVAC", .state = ARM_CP_STATE_AA64,
6933 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 3,
6934 .type = ARM_CP_NOP, .access = PL1_W,
6935 .accessfn = aa64_cacheop_poc_access },
6936 { .name = "DC_IGSW", .state = ARM_CP_STATE_AA64,
6937 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 4,
6938 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
6939 { .name = "DC_IGDVAC", .state = ARM_CP_STATE_AA64,
6940 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 5,
6941 .type = ARM_CP_NOP, .access = PL1_W,
6942 .accessfn = aa64_cacheop_poc_access },
6943 { .name = "DC_IGDSW", .state = ARM_CP_STATE_AA64,
6944 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 6,
6945 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
6946 { .name = "DC_CGSW", .state = ARM_CP_STATE_AA64,
6947 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 4,
6948 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
6949 { .name = "DC_CGDSW", .state = ARM_CP_STATE_AA64,
6950 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 6,
6951 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
6952 { .name = "DC_CIGSW", .state = ARM_CP_STATE_AA64,
6953 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 4,
6954 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
6955 { .name = "DC_CIGDSW", .state = ARM_CP_STATE_AA64,
6956 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 6,
6957 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
4b779ceb
RH
6958 REGINFO_SENTINEL
6959};
6960
6961static const ARMCPRegInfo mte_tco_ro_reginfo[] = {
6962 { .name = "TCO", .state = ARM_CP_STATE_AA64,
6963 .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 7,
6964 .type = ARM_CP_CONST, .access = PL0_RW, },
6965 REGINFO_SENTINEL
6966};
5463df16
RH
6967
6968static const ARMCPRegInfo mte_el0_cacheop_reginfo[] = {
6969 { .name = "DC_CGVAC", .state = ARM_CP_STATE_AA64,
6970 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 3,
6971 .type = ARM_CP_NOP, .access = PL0_W,
6972 .accessfn = aa64_cacheop_poc_access },
6973 { .name = "DC_CGDVAC", .state = ARM_CP_STATE_AA64,
6974 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 5,
6975 .type = ARM_CP_NOP, .access = PL0_W,
6976 .accessfn = aa64_cacheop_poc_access },
6977 { .name = "DC_CGVAP", .state = ARM_CP_STATE_AA64,
6978 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 3,
6979 .type = ARM_CP_NOP, .access = PL0_W,
6980 .accessfn = aa64_cacheop_poc_access },
6981 { .name = "DC_CGDVAP", .state = ARM_CP_STATE_AA64,
6982 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 5,
6983 .type = ARM_CP_NOP, .access = PL0_W,
6984 .accessfn = aa64_cacheop_poc_access },
6985 { .name = "DC_CGVADP", .state = ARM_CP_STATE_AA64,
6986 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 3,
6987 .type = ARM_CP_NOP, .access = PL0_W,
6988 .accessfn = aa64_cacheop_poc_access },
6989 { .name = "DC_CGDVADP", .state = ARM_CP_STATE_AA64,
6990 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 5,
6991 .type = ARM_CP_NOP, .access = PL0_W,
6992 .accessfn = aa64_cacheop_poc_access },
6993 { .name = "DC_CIGVAC", .state = ARM_CP_STATE_AA64,
6994 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 3,
6995 .type = ARM_CP_NOP, .access = PL0_W,
6996 .accessfn = aa64_cacheop_poc_access },
6997 { .name = "DC_CIGDVAC", .state = ARM_CP_STATE_AA64,
6998 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 5,
6999 .type = ARM_CP_NOP, .access = PL0_W,
7000 .accessfn = aa64_cacheop_poc_access },
7001 REGINFO_SENTINEL
7002};
7003
967aa94f
RH
7004#endif
7005
cb570bd3
RH
7006static CPAccessResult access_predinv(CPUARMState *env, const ARMCPRegInfo *ri,
7007 bool isread)
7008{
7009 int el = arm_current_el(env);
7010
7011 if (el == 0) {
7012 uint64_t sctlr = arm_sctlr(env, el);
7013 if (!(sctlr & SCTLR_EnRCTX)) {
7014 return CP_ACCESS_TRAP;
7015 }
7016 } else if (el == 1) {
7017 uint64_t hcr = arm_hcr_el2_eff(env);
7018 if (hcr & HCR_NV) {
7019 return CP_ACCESS_TRAP_EL2;
7020 }
7021 }
7022 return CP_ACCESS_OK;
7023}
7024
7025static const ARMCPRegInfo predinv_reginfo[] = {
7026 { .name = "CFP_RCTX", .state = ARM_CP_STATE_AA64,
7027 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 4,
7028 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7029 { .name = "DVP_RCTX", .state = ARM_CP_STATE_AA64,
7030 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 5,
7031 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7032 { .name = "CPP_RCTX", .state = ARM_CP_STATE_AA64,
7033 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 7,
7034 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7035 /*
7036 * Note the AArch32 opcodes have a different OPC1.
7037 */
7038 { .name = "CFPRCTX", .state = ARM_CP_STATE_AA32,
7039 .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 4,
7040 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7041 { .name = "DVPRCTX", .state = ARM_CP_STATE_AA32,
7042 .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 5,
7043 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7044 { .name = "CPPRCTX", .state = ARM_CP_STATE_AA32,
7045 .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 7,
7046 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7047 REGINFO_SENTINEL
7048};
7049
957e6155
PM
7050static uint64_t ccsidr2_read(CPUARMState *env, const ARMCPRegInfo *ri)
7051{
7052 /* Read the high 32 bits of the current CCSIDR */
7053 return extract64(ccsidr_read(env, ri), 32, 32);
7054}
7055
7056static const ARMCPRegInfo ccsidr2_reginfo[] = {
7057 { .name = "CCSIDR2", .state = ARM_CP_STATE_BOTH,
7058 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 2,
7059 .access = PL1_R,
7060 .accessfn = access_aa64_tid2,
7061 .readfn = ccsidr2_read, .type = ARM_CP_NO_RAW },
7062 REGINFO_SENTINEL
7063};
7064
6a4ef4e5
MZ
7065static CPAccessResult access_aa64_tid3(CPUARMState *env, const ARMCPRegInfo *ri,
7066 bool isread)
7067{
7068 if ((arm_current_el(env) < 2) && (arm_hcr_el2_eff(env) & HCR_TID3)) {
7069 return CP_ACCESS_TRAP_EL2;
7070 }
7071
7072 return CP_ACCESS_OK;
7073}
7074
7075static CPAccessResult access_aa32_tid3(CPUARMState *env, const ARMCPRegInfo *ri,
7076 bool isread)
7077{
7078 if (arm_feature(env, ARM_FEATURE_V8)) {
7079 return access_aa64_tid3(env, ri, isread);
7080 }
7081
7082 return CP_ACCESS_OK;
7083}
7084
f96f3d5f
MZ
7085static CPAccessResult access_jazelle(CPUARMState *env, const ARMCPRegInfo *ri,
7086 bool isread)
7087{
7088 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID0)) {
7089 return CP_ACCESS_TRAP_EL2;
7090 }
7091
7092 return CP_ACCESS_OK;
7093}
7094
7095static const ARMCPRegInfo jazelle_regs[] = {
7096 { .name = "JIDR",
7097 .cp = 14, .crn = 0, .crm = 0, .opc1 = 7, .opc2 = 0,
7098 .access = PL1_R, .accessfn = access_jazelle,
7099 .type = ARM_CP_CONST, .resetvalue = 0 },
7100 { .name = "JOSCR",
7101 .cp = 14, .crn = 1, .crm = 0, .opc1 = 7, .opc2 = 0,
7102 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
7103 { .name = "JMCR",
7104 .cp = 14, .crn = 2, .crm = 0, .opc1 = 7, .opc2 = 0,
7105 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
7106 REGINFO_SENTINEL
7107};
7108
e2a1a461
RH
7109static const ARMCPRegInfo vhe_reginfo[] = {
7110 { .name = "CONTEXTIDR_EL2", .state = ARM_CP_STATE_AA64,
7111 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 1,
7112 .access = PL2_RW,
7113 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[2]) },
ed30da8e
RH
7114 { .name = "TTBR1_EL2", .state = ARM_CP_STATE_AA64,
7115 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 1,
7116 .access = PL2_RW, .writefn = vmsa_tcr_ttbr_el2_write,
7117 .fieldoffset = offsetof(CPUARMState, cp15.ttbr1_el[2]) },
8c94b071
RH
7118#ifndef CONFIG_USER_ONLY
7119 { .name = "CNTHV_CVAL_EL2", .state = ARM_CP_STATE_AA64,
7120 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 2,
7121 .fieldoffset =
7122 offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYPVIRT].cval),
7123 .type = ARM_CP_IO, .access = PL2_RW,
7124 .writefn = gt_hv_cval_write, .raw_writefn = raw_write },
7125 { .name = "CNTHV_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
7126 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 0,
7127 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
7128 .resetfn = gt_hv_timer_reset,
7129 .readfn = gt_hv_tval_read, .writefn = gt_hv_tval_write },
7130 { .name = "CNTHV_CTL_EL2", .state = ARM_CP_STATE_BOTH,
7131 .type = ARM_CP_IO,
7132 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 1,
7133 .access = PL2_RW,
7134 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYPVIRT].ctl),
7135 .writefn = gt_hv_ctl_write, .raw_writefn = raw_write },
bb5972e4
RH
7136 { .name = "CNTP_CTL_EL02", .state = ARM_CP_STATE_AA64,
7137 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 1,
7138 .type = ARM_CP_IO | ARM_CP_ALIAS,
7139 .access = PL2_RW, .accessfn = e2h_access,
7140 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
7141 .writefn = gt_phys_ctl_write, .raw_writefn = raw_write },
7142 { .name = "CNTV_CTL_EL02", .state = ARM_CP_STATE_AA64,
7143 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 1,
7144 .type = ARM_CP_IO | ARM_CP_ALIAS,
7145 .access = PL2_RW, .accessfn = e2h_access,
7146 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
7147 .writefn = gt_virt_ctl_write, .raw_writefn = raw_write },
7148 { .name = "CNTP_TVAL_EL02", .state = ARM_CP_STATE_AA64,
7149 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 0,
7150 .type = ARM_CP_NO_RAW | ARM_CP_IO | ARM_CP_ALIAS,
7151 .access = PL2_RW, .accessfn = e2h_access,
7152 .readfn = gt_phys_tval_read, .writefn = gt_phys_tval_write },
7153 { .name = "CNTV_TVAL_EL02", .state = ARM_CP_STATE_AA64,
7154 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 0,
7155 .type = ARM_CP_NO_RAW | ARM_CP_IO | ARM_CP_ALIAS,
7156 .access = PL2_RW, .accessfn = e2h_access,
7157 .readfn = gt_virt_tval_read, .writefn = gt_virt_tval_write },
7158 { .name = "CNTP_CVAL_EL02", .state = ARM_CP_STATE_AA64,
7159 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 2,
7160 .type = ARM_CP_IO | ARM_CP_ALIAS,
7161 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
7162 .access = PL2_RW, .accessfn = e2h_access,
7163 .writefn = gt_phys_cval_write, .raw_writefn = raw_write },
7164 { .name = "CNTV_CVAL_EL02", .state = ARM_CP_STATE_AA64,
7165 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 2,
7166 .type = ARM_CP_IO | ARM_CP_ALIAS,
7167 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
7168 .access = PL2_RW, .accessfn = e2h_access,
7169 .writefn = gt_virt_cval_write, .raw_writefn = raw_write },
8c94b071 7170#endif
e2a1a461
RH
7171 REGINFO_SENTINEL
7172};
7173
04b07d29
RH
7174#ifndef CONFIG_USER_ONLY
7175static const ARMCPRegInfo ats1e1_reginfo[] = {
7176 { .name = "AT_S1E1R", .state = ARM_CP_STATE_AA64,
7177 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 0,
7178 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7179 .writefn = ats_write64 },
7180 { .name = "AT_S1E1W", .state = ARM_CP_STATE_AA64,
7181 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 1,
7182 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7183 .writefn = ats_write64 },
7184 REGINFO_SENTINEL
7185};
7186
7187static const ARMCPRegInfo ats1cp_reginfo[] = {
7188 { .name = "ATS1CPRP",
7189 .cp = 15, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 0,
7190 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7191 .writefn = ats_write },
7192 { .name = "ATS1CPWP",
7193 .cp = 15, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 1,
7194 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7195 .writefn = ats_write },
7196 REGINFO_SENTINEL
7197};
7198#endif
7199
f6287c24
PM
7200/*
7201 * ACTLR2 and HACTLR2 map to ACTLR_EL1[63:32] and
7202 * ACTLR_EL2[63:32]. They exist only if the ID_MMFR4.AC2 field
7203 * is non-zero, which is never for ARMv7, optionally in ARMv8
7204 * and mandatorily for ARMv8.2 and up.
7205 * ACTLR2 is banked for S and NS if EL3 is AArch32. Since QEMU's
7206 * implementation is RAZ/WI we can ignore this detail, as we
7207 * do for ACTLR.
7208 */
7209static const ARMCPRegInfo actlr2_hactlr2_reginfo[] = {
7210 { .name = "ACTLR2", .state = ARM_CP_STATE_AA32,
7211 .cp = 15, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 3,
99602377
RH
7212 .access = PL1_RW, .accessfn = access_tacr,
7213 .type = ARM_CP_CONST, .resetvalue = 0 },
f6287c24
PM
7214 { .name = "HACTLR2", .state = ARM_CP_STATE_AA32,
7215 .cp = 15, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 3,
7216 .access = PL2_RW, .type = ARM_CP_CONST,
7217 .resetvalue = 0 },
7218 REGINFO_SENTINEL
7219};
7220
2ceb98c0
PM
7221void register_cp_regs_for_features(ARMCPU *cpu)
7222{
7223 /* Register all the coprocessor registers based on feature bits */
7224 CPUARMState *env = &cpu->env;
7225 if (arm_feature(env, ARM_FEATURE_M)) {
7226 /* M profile has no coprocessor registers */
7227 return;
7228 }
7229
e9aa6c21 7230 define_arm_cp_regs(cpu, cp_reginfo);
9449fdf6
PM
7231 if (!arm_feature(env, ARM_FEATURE_V8)) {
7232 /* Must go early as it is full of wildcards that may be
7233 * overridden by later definitions.
7234 */
7235 define_arm_cp_regs(cpu, not_v8_cp_reginfo);
7236 }
7237
7d57f408 7238 if (arm_feature(env, ARM_FEATURE_V6)) {
8515a092
PM
7239 /* The ID registers all have impdef reset values */
7240 ARMCPRegInfo v6_idregs[] = {
0ff644a7
PM
7241 { .name = "ID_PFR0", .state = ARM_CP_STATE_BOTH,
7242 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
7243 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7244 .accessfn = access_aa32_tid3,
8515a092 7245 .resetvalue = cpu->id_pfr0 },
96a8b92e
PM
7246 /* ID_PFR1 is not a plain ARM_CP_CONST because we don't know
7247 * the value of the GIC field until after we define these regs.
7248 */
0ff644a7
PM
7249 { .name = "ID_PFR1", .state = ARM_CP_STATE_BOTH,
7250 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 1,
96a8b92e 7251 .access = PL1_R, .type = ARM_CP_NO_RAW,
6a4ef4e5 7252 .accessfn = access_aa32_tid3,
96a8b92e
PM
7253 .readfn = id_pfr1_read,
7254 .writefn = arm_cp_write_ignore },
0ff644a7
PM
7255 { .name = "ID_DFR0", .state = ARM_CP_STATE_BOTH,
7256 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 2,
7257 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7258 .accessfn = access_aa32_tid3,
a6179538 7259 .resetvalue = cpu->isar.id_dfr0 },
0ff644a7
PM
7260 { .name = "ID_AFR0", .state = ARM_CP_STATE_BOTH,
7261 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 3,
7262 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7263 .accessfn = access_aa32_tid3,
8515a092 7264 .resetvalue = cpu->id_afr0 },
0ff644a7
PM
7265 { .name = "ID_MMFR0", .state = ARM_CP_STATE_BOTH,
7266 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 4,
7267 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7268 .accessfn = access_aa32_tid3,
10054016 7269 .resetvalue = cpu->isar.id_mmfr0 },
0ff644a7
PM
7270 { .name = "ID_MMFR1", .state = ARM_CP_STATE_BOTH,
7271 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 5,
7272 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7273 .accessfn = access_aa32_tid3,
10054016 7274 .resetvalue = cpu->isar.id_mmfr1 },
0ff644a7
PM
7275 { .name = "ID_MMFR2", .state = ARM_CP_STATE_BOTH,
7276 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 6,
7277 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7278 .accessfn = access_aa32_tid3,
10054016 7279 .resetvalue = cpu->isar.id_mmfr2 },
0ff644a7
PM
7280 { .name = "ID_MMFR3", .state = ARM_CP_STATE_BOTH,
7281 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 7,
7282 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7283 .accessfn = access_aa32_tid3,
10054016 7284 .resetvalue = cpu->isar.id_mmfr3 },
0ff644a7
PM
7285 { .name = "ID_ISAR0", .state = ARM_CP_STATE_BOTH,
7286 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0,
7287 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7288 .accessfn = access_aa32_tid3,
47576b94 7289 .resetvalue = cpu->isar.id_isar0 },
0ff644a7
PM
7290 { .name = "ID_ISAR1", .state = ARM_CP_STATE_BOTH,
7291 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 1,
7292 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7293 .accessfn = access_aa32_tid3,
47576b94 7294 .resetvalue = cpu->isar.id_isar1 },
0ff644a7
PM
7295 { .name = "ID_ISAR2", .state = ARM_CP_STATE_BOTH,
7296 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
7297 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7298 .accessfn = access_aa32_tid3,
47576b94 7299 .resetvalue = cpu->isar.id_isar2 },
0ff644a7
PM
7300 { .name = "ID_ISAR3", .state = ARM_CP_STATE_BOTH,
7301 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 3,
7302 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7303 .accessfn = access_aa32_tid3,
47576b94 7304 .resetvalue = cpu->isar.id_isar3 },
0ff644a7
PM
7305 { .name = "ID_ISAR4", .state = ARM_CP_STATE_BOTH,
7306 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 4,
7307 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7308 .accessfn = access_aa32_tid3,
47576b94 7309 .resetvalue = cpu->isar.id_isar4 },
0ff644a7
PM
7310 { .name = "ID_ISAR5", .state = ARM_CP_STATE_BOTH,
7311 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 5,
7312 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7313 .accessfn = access_aa32_tid3,
47576b94 7314 .resetvalue = cpu->isar.id_isar5 },
e20d84c1
PM
7315 { .name = "ID_MMFR4", .state = ARM_CP_STATE_BOTH,
7316 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 6,
7317 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7318 .accessfn = access_aa32_tid3,
10054016 7319 .resetvalue = cpu->isar.id_mmfr4 },
802abf40 7320 { .name = "ID_ISAR6", .state = ARM_CP_STATE_BOTH,
e20d84c1
PM
7321 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 7,
7322 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7323 .accessfn = access_aa32_tid3,
47576b94 7324 .resetvalue = cpu->isar.id_isar6 },
8515a092
PM
7325 REGINFO_SENTINEL
7326 };
7327 define_arm_cp_regs(cpu, v6_idregs);
7d57f408
PM
7328 define_arm_cp_regs(cpu, v6_cp_reginfo);
7329 } else {
7330 define_arm_cp_regs(cpu, not_v6_cp_reginfo);
7331 }
4d31c596
PM
7332 if (arm_feature(env, ARM_FEATURE_V6K)) {
7333 define_arm_cp_regs(cpu, v6k_cp_reginfo);
7334 }
5e5cf9e3 7335 if (arm_feature(env, ARM_FEATURE_V7MP) &&
452a0955 7336 !arm_feature(env, ARM_FEATURE_PMSA)) {
995939a6
PM
7337 define_arm_cp_regs(cpu, v7mp_cp_reginfo);
7338 }
327dd510
AL
7339 if (arm_feature(env, ARM_FEATURE_V7VE)) {
7340 define_arm_cp_regs(cpu, pmovsset_cp_reginfo);
7341 }
e9aa6c21 7342 if (arm_feature(env, ARM_FEATURE_V7)) {
776d4e5c 7343 ARMCPRegInfo clidr = {
7da845b0
PM
7344 .name = "CLIDR", .state = ARM_CP_STATE_BOTH,
7345 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 1,
630fcd4d
MZ
7346 .access = PL1_R, .type = ARM_CP_CONST,
7347 .accessfn = access_aa64_tid2,
7348 .resetvalue = cpu->clidr
776d4e5c 7349 };
776d4e5c 7350 define_one_arm_cp_reg(cpu, &clidr);
e9aa6c21 7351 define_arm_cp_regs(cpu, v7_cp_reginfo);
50300698 7352 define_debug_regs(cpu);
24183fb6 7353 define_pmu_regs(cpu);
7d57f408
PM
7354 } else {
7355 define_arm_cp_regs(cpu, not_v7_cp_reginfo);
e9aa6c21 7356 }
b0d2b7d0 7357 if (arm_feature(env, ARM_FEATURE_V8)) {
e20d84c1
PM
7358 /* AArch64 ID registers, which all have impdef reset values.
7359 * Note that within the ID register ranges the unused slots
7360 * must all RAZ, not UNDEF; future architecture versions may
7361 * define new registers here.
7362 */
e60cef86 7363 ARMCPRegInfo v8_idregs[] = {
976b99b6
AB
7364 /*
7365 * ID_AA64PFR0_EL1 is not a plain ARM_CP_CONST in system
7366 * emulation because we don't know the right value for the
7367 * GIC field until after we define these regs.
96a8b92e 7368 */
e60cef86
PM
7369 { .name = "ID_AA64PFR0_EL1", .state = ARM_CP_STATE_AA64,
7370 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 0,
976b99b6
AB
7371 .access = PL1_R,
7372#ifdef CONFIG_USER_ONLY
7373 .type = ARM_CP_CONST,
7374 .resetvalue = cpu->isar.id_aa64pfr0
7375#else
7376 .type = ARM_CP_NO_RAW,
6a4ef4e5 7377 .accessfn = access_aa64_tid3,
96a8b92e 7378 .readfn = id_aa64pfr0_read,
976b99b6
AB
7379 .writefn = arm_cp_write_ignore
7380#endif
7381 },
e60cef86
PM
7382 { .name = "ID_AA64PFR1_EL1", .state = ARM_CP_STATE_AA64,
7383 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 1,
7384 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7385 .accessfn = access_aa64_tid3,
47576b94 7386 .resetvalue = cpu->isar.id_aa64pfr1},
e20d84c1
PM
7387 { .name = "ID_AA64PFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7388 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 2,
7389 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7390 .accessfn = access_aa64_tid3,
e20d84c1
PM
7391 .resetvalue = 0 },
7392 { .name = "ID_AA64PFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7393 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 3,
7394 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7395 .accessfn = access_aa64_tid3,
e20d84c1 7396 .resetvalue = 0 },
9516d772 7397 { .name = "ID_AA64ZFR0_EL1", .state = ARM_CP_STATE_AA64,
e20d84c1
PM
7398 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 4,
7399 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7400 .accessfn = access_aa64_tid3,
9516d772 7401 /* At present, only SVEver == 0 is defined anyway. */
e20d84c1
PM
7402 .resetvalue = 0 },
7403 { .name = "ID_AA64PFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7404 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 5,
7405 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7406 .accessfn = access_aa64_tid3,
e20d84c1
PM
7407 .resetvalue = 0 },
7408 { .name = "ID_AA64PFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7409 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 6,
7410 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7411 .accessfn = access_aa64_tid3,
e20d84c1
PM
7412 .resetvalue = 0 },
7413 { .name = "ID_AA64PFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7414 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 7,
7415 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7416 .accessfn = access_aa64_tid3,
e20d84c1 7417 .resetvalue = 0 },
e60cef86
PM
7418 { .name = "ID_AA64DFR0_EL1", .state = ARM_CP_STATE_AA64,
7419 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 0,
7420 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7421 .accessfn = access_aa64_tid3,
2a609df8 7422 .resetvalue = cpu->isar.id_aa64dfr0 },
e60cef86
PM
7423 { .name = "ID_AA64DFR1_EL1", .state = ARM_CP_STATE_AA64,
7424 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 1,
7425 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7426 .accessfn = access_aa64_tid3,
2a609df8 7427 .resetvalue = cpu->isar.id_aa64dfr1 },
e20d84c1
PM
7428 { .name = "ID_AA64DFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7429 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 2,
7430 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7431 .accessfn = access_aa64_tid3,
e20d84c1
PM
7432 .resetvalue = 0 },
7433 { .name = "ID_AA64DFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7434 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 3,
7435 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7436 .accessfn = access_aa64_tid3,
e20d84c1 7437 .resetvalue = 0 },
e60cef86
PM
7438 { .name = "ID_AA64AFR0_EL1", .state = ARM_CP_STATE_AA64,
7439 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 4,
7440 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7441 .accessfn = access_aa64_tid3,
e60cef86
PM
7442 .resetvalue = cpu->id_aa64afr0 },
7443 { .name = "ID_AA64AFR1_EL1", .state = ARM_CP_STATE_AA64,
7444 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 5,
7445 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7446 .accessfn = access_aa64_tid3,
e60cef86 7447 .resetvalue = cpu->id_aa64afr1 },
e20d84c1
PM
7448 { .name = "ID_AA64AFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7449 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 6,
7450 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7451 .accessfn = access_aa64_tid3,
e20d84c1
PM
7452 .resetvalue = 0 },
7453 { .name = "ID_AA64AFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7454 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 7,
7455 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7456 .accessfn = access_aa64_tid3,
e20d84c1 7457 .resetvalue = 0 },
e60cef86
PM
7458 { .name = "ID_AA64ISAR0_EL1", .state = ARM_CP_STATE_AA64,
7459 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 0,
7460 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7461 .accessfn = access_aa64_tid3,
47576b94 7462 .resetvalue = cpu->isar.id_aa64isar0 },
e60cef86
PM
7463 { .name = "ID_AA64ISAR1_EL1", .state = ARM_CP_STATE_AA64,
7464 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 1,
7465 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7466 .accessfn = access_aa64_tid3,
47576b94 7467 .resetvalue = cpu->isar.id_aa64isar1 },
e20d84c1
PM
7468 { .name = "ID_AA64ISAR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7469 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 2,
7470 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7471 .accessfn = access_aa64_tid3,
e20d84c1
PM
7472 .resetvalue = 0 },
7473 { .name = "ID_AA64ISAR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7474 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 3,
7475 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7476 .accessfn = access_aa64_tid3,
e20d84c1
PM
7477 .resetvalue = 0 },
7478 { .name = "ID_AA64ISAR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7479 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 4,
7480 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7481 .accessfn = access_aa64_tid3,
e20d84c1
PM
7482 .resetvalue = 0 },
7483 { .name = "ID_AA64ISAR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7484 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 5,
7485 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7486 .accessfn = access_aa64_tid3,
e20d84c1
PM
7487 .resetvalue = 0 },
7488 { .name = "ID_AA64ISAR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7489 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 6,
7490 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7491 .accessfn = access_aa64_tid3,
e20d84c1
PM
7492 .resetvalue = 0 },
7493 { .name = "ID_AA64ISAR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7494 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 7,
7495 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7496 .accessfn = access_aa64_tid3,
e20d84c1 7497 .resetvalue = 0 },
e60cef86
PM
7498 { .name = "ID_AA64MMFR0_EL1", .state = ARM_CP_STATE_AA64,
7499 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
7500 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7501 .accessfn = access_aa64_tid3,
3dc91ddb 7502 .resetvalue = cpu->isar.id_aa64mmfr0 },
e60cef86
PM
7503 { .name = "ID_AA64MMFR1_EL1", .state = ARM_CP_STATE_AA64,
7504 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 1,
7505 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7506 .accessfn = access_aa64_tid3,
3dc91ddb 7507 .resetvalue = cpu->isar.id_aa64mmfr1 },
64761e10 7508 { .name = "ID_AA64MMFR2_EL1", .state = ARM_CP_STATE_AA64,
e20d84c1
PM
7509 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 2,
7510 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7511 .accessfn = access_aa64_tid3,
64761e10 7512 .resetvalue = cpu->isar.id_aa64mmfr2 },
e20d84c1
PM
7513 { .name = "ID_AA64MMFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7514 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 3,
7515 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7516 .accessfn = access_aa64_tid3,
e20d84c1
PM
7517 .resetvalue = 0 },
7518 { .name = "ID_AA64MMFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7519 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 4,
7520 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7521 .accessfn = access_aa64_tid3,
e20d84c1
PM
7522 .resetvalue = 0 },
7523 { .name = "ID_AA64MMFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7524 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 5,
7525 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7526 .accessfn = access_aa64_tid3,
e20d84c1
PM
7527 .resetvalue = 0 },
7528 { .name = "ID_AA64MMFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7529 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 6,
7530 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7531 .accessfn = access_aa64_tid3,
e20d84c1
PM
7532 .resetvalue = 0 },
7533 { .name = "ID_AA64MMFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7534 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 7,
7535 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7536 .accessfn = access_aa64_tid3,
e20d84c1 7537 .resetvalue = 0 },
a50c0f51
PM
7538 { .name = "MVFR0_EL1", .state = ARM_CP_STATE_AA64,
7539 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0,
7540 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7541 .accessfn = access_aa64_tid3,
47576b94 7542 .resetvalue = cpu->isar.mvfr0 },
a50c0f51
PM
7543 { .name = "MVFR1_EL1", .state = ARM_CP_STATE_AA64,
7544 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1,
7545 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7546 .accessfn = access_aa64_tid3,
47576b94 7547 .resetvalue = cpu->isar.mvfr1 },
a50c0f51
PM
7548 { .name = "MVFR2_EL1", .state = ARM_CP_STATE_AA64,
7549 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2,
7550 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7551 .accessfn = access_aa64_tid3,
47576b94 7552 .resetvalue = cpu->isar.mvfr2 },
e20d84c1
PM
7553 { .name = "MVFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7554 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 3,
7555 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7556 .accessfn = access_aa64_tid3,
e20d84c1
PM
7557 .resetvalue = 0 },
7558 { .name = "MVFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7559 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 4,
7560 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7561 .accessfn = access_aa64_tid3,
e20d84c1
PM
7562 .resetvalue = 0 },
7563 { .name = "MVFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7564 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 5,
7565 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7566 .accessfn = access_aa64_tid3,
e20d84c1
PM
7567 .resetvalue = 0 },
7568 { .name = "MVFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7569 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 6,
7570 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7571 .accessfn = access_aa64_tid3,
e20d84c1
PM
7572 .resetvalue = 0 },
7573 { .name = "MVFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7574 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 7,
7575 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7576 .accessfn = access_aa64_tid3,
e20d84c1 7577 .resetvalue = 0 },
4054bfa9
AF
7578 { .name = "PMCEID0", .state = ARM_CP_STATE_AA32,
7579 .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 6,
7580 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
cad86737 7581 .resetvalue = extract64(cpu->pmceid0, 0, 32) },
4054bfa9
AF
7582 { .name = "PMCEID0_EL0", .state = ARM_CP_STATE_AA64,
7583 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 6,
7584 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
7585 .resetvalue = cpu->pmceid0 },
7586 { .name = "PMCEID1", .state = ARM_CP_STATE_AA32,
7587 .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 7,
7588 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
cad86737 7589 .resetvalue = extract64(cpu->pmceid1, 0, 32) },
4054bfa9
AF
7590 { .name = "PMCEID1_EL0", .state = ARM_CP_STATE_AA64,
7591 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 7,
7592 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
7593 .resetvalue = cpu->pmceid1 },
e60cef86
PM
7594 REGINFO_SENTINEL
7595 };
6c5c0fec
AB
7596#ifdef CONFIG_USER_ONLY
7597 ARMCPRegUserSpaceInfo v8_user_idregs[] = {
7598 { .name = "ID_AA64PFR0_EL1",
7599 .exported_bits = 0x000f000f00ff0000,
7600 .fixed_bits = 0x0000000000000011 },
7601 { .name = "ID_AA64PFR1_EL1",
7602 .exported_bits = 0x00000000000000f0 },
d040242e
AB
7603 { .name = "ID_AA64PFR*_EL1_RESERVED",
7604 .is_glob = true },
6c5c0fec
AB
7605 { .name = "ID_AA64ZFR0_EL1" },
7606 { .name = "ID_AA64MMFR0_EL1",
7607 .fixed_bits = 0x00000000ff000000 },
7608 { .name = "ID_AA64MMFR1_EL1" },
d040242e
AB
7609 { .name = "ID_AA64MMFR*_EL1_RESERVED",
7610 .is_glob = true },
6c5c0fec
AB
7611 { .name = "ID_AA64DFR0_EL1",
7612 .fixed_bits = 0x0000000000000006 },
7613 { .name = "ID_AA64DFR1_EL1" },
d040242e
AB
7614 { .name = "ID_AA64DFR*_EL1_RESERVED",
7615 .is_glob = true },
7616 { .name = "ID_AA64AFR*",
7617 .is_glob = true },
6c5c0fec
AB
7618 { .name = "ID_AA64ISAR0_EL1",
7619 .exported_bits = 0x00fffffff0fffff0 },
7620 { .name = "ID_AA64ISAR1_EL1",
7621 .exported_bits = 0x000000f0ffffffff },
d040242e
AB
7622 { .name = "ID_AA64ISAR*_EL1_RESERVED",
7623 .is_glob = true },
6c5c0fec
AB
7624 REGUSERINFO_SENTINEL
7625 };
7626 modify_arm_cp_regs(v8_idregs, v8_user_idregs);
7627#endif
be8e8128
GB
7628 /* RVBAR_EL1 is only implemented if EL1 is the highest EL */
7629 if (!arm_feature(env, ARM_FEATURE_EL3) &&
7630 !arm_feature(env, ARM_FEATURE_EL2)) {
7631 ARMCPRegInfo rvbar = {
7632 .name = "RVBAR_EL1", .state = ARM_CP_STATE_AA64,
7633 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
7634 .type = ARM_CP_CONST, .access = PL1_R, .resetvalue = cpu->rvbar
7635 };
7636 define_one_arm_cp_reg(cpu, &rvbar);
7637 }
e60cef86 7638 define_arm_cp_regs(cpu, v8_idregs);
b0d2b7d0
PM
7639 define_arm_cp_regs(cpu, v8_cp_reginfo);
7640 }
3b685ba7 7641 if (arm_feature(env, ARM_FEATURE_EL2)) {
f0d574d6 7642 uint64_t vmpidr_def = mpidr_read_val(env);
731de9e6
EI
7643 ARMCPRegInfo vpidr_regs[] = {
7644 { .name = "VPIDR", .state = ARM_CP_STATE_AA32,
7645 .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
7646 .access = PL2_RW, .accessfn = access_el3_aa32ns,
36476562
PM
7647 .resetvalue = cpu->midr, .type = ARM_CP_ALIAS,
7648 .fieldoffset = offsetoflow32(CPUARMState, cp15.vpidr_el2) },
731de9e6
EI
7649 { .name = "VPIDR_EL2", .state = ARM_CP_STATE_AA64,
7650 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
7651 .access = PL2_RW, .resetvalue = cpu->midr,
7652 .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) },
f0d574d6
EI
7653 { .name = "VMPIDR", .state = ARM_CP_STATE_AA32,
7654 .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
7655 .access = PL2_RW, .accessfn = access_el3_aa32ns,
36476562
PM
7656 .resetvalue = vmpidr_def, .type = ARM_CP_ALIAS,
7657 .fieldoffset = offsetoflow32(CPUARMState, cp15.vmpidr_el2) },
f0d574d6
EI
7658 { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_AA64,
7659 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
7660 .access = PL2_RW,
7661 .resetvalue = vmpidr_def,
7662 .fieldoffset = offsetof(CPUARMState, cp15.vmpidr_el2) },
731de9e6
EI
7663 REGINFO_SENTINEL
7664 };
7665 define_arm_cp_regs(cpu, vpidr_regs);
4771cd01 7666 define_arm_cp_regs(cpu, el2_cp_reginfo);
ce4afed8
PM
7667 if (arm_feature(env, ARM_FEATURE_V8)) {
7668 define_arm_cp_regs(cpu, el2_v8_cp_reginfo);
7669 }
be8e8128
GB
7670 /* RVBAR_EL2 is only implemented if EL2 is the highest EL */
7671 if (!arm_feature(env, ARM_FEATURE_EL3)) {
7672 ARMCPRegInfo rvbar = {
7673 .name = "RVBAR_EL2", .state = ARM_CP_STATE_AA64,
7674 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 1,
7675 .type = ARM_CP_CONST, .access = PL2_R, .resetvalue = cpu->rvbar
7676 };
7677 define_one_arm_cp_reg(cpu, &rvbar);
7678 }
d42e3c26
EI
7679 } else {
7680 /* If EL2 is missing but higher ELs are enabled, we need to
7681 * register the no_el2 reginfos.
7682 */
7683 if (arm_feature(env, ARM_FEATURE_EL3)) {
f0d574d6
EI
7684 /* When EL3 exists but not EL2, VPIDR and VMPIDR take the value
7685 * of MIDR_EL1 and MPIDR_EL1.
731de9e6
EI
7686 */
7687 ARMCPRegInfo vpidr_regs[] = {
7688 { .name = "VPIDR_EL2", .state = ARM_CP_STATE_BOTH,
7689 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
93dd1e61 7690 .access = PL2_RW, .accessfn = access_el3_aa32ns,
731de9e6
EI
7691 .type = ARM_CP_CONST, .resetvalue = cpu->midr,
7692 .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) },
f0d574d6
EI
7693 { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_BOTH,
7694 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
93dd1e61 7695 .access = PL2_RW, .accessfn = access_el3_aa32ns,
f0d574d6
EI
7696 .type = ARM_CP_NO_RAW,
7697 .writefn = arm_cp_write_ignore, .readfn = mpidr_read },
731de9e6
EI
7698 REGINFO_SENTINEL
7699 };
7700 define_arm_cp_regs(cpu, vpidr_regs);
4771cd01 7701 define_arm_cp_regs(cpu, el3_no_el2_cp_reginfo);
ce4afed8
PM
7702 if (arm_feature(env, ARM_FEATURE_V8)) {
7703 define_arm_cp_regs(cpu, el3_no_el2_v8_cp_reginfo);
7704 }
d42e3c26 7705 }
3b685ba7 7706 }
81547d66 7707 if (arm_feature(env, ARM_FEATURE_EL3)) {
0f1a3b24 7708 define_arm_cp_regs(cpu, el3_cp_reginfo);
e24fdd23
PM
7709 ARMCPRegInfo el3_regs[] = {
7710 { .name = "RVBAR_EL3", .state = ARM_CP_STATE_AA64,
7711 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 1,
7712 .type = ARM_CP_CONST, .access = PL3_R, .resetvalue = cpu->rvbar },
7713 { .name = "SCTLR_EL3", .state = ARM_CP_STATE_AA64,
7714 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 0,
7715 .access = PL3_RW,
7716 .raw_writefn = raw_write, .writefn = sctlr_write,
7717 .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[3]),
7718 .resetvalue = cpu->reset_sctlr },
7719 REGINFO_SENTINEL
be8e8128 7720 };
e24fdd23
PM
7721
7722 define_arm_cp_regs(cpu, el3_regs);
81547d66 7723 }
2f027fc5
PM
7724 /* The behaviour of NSACR is sufficiently various that we don't
7725 * try to describe it in a single reginfo:
7726 * if EL3 is 64 bit, then trap to EL3 from S EL1,
7727 * reads as constant 0xc00 from NS EL1 and NS EL2
7728 * if EL3 is 32 bit, then RW at EL3, RO at NS EL1 and NS EL2
7729 * if v7 without EL3, register doesn't exist
7730 * if v8 without EL3, reads as constant 0xc00 from NS EL1 and NS EL2
7731 */
7732 if (arm_feature(env, ARM_FEATURE_EL3)) {
7733 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
7734 ARMCPRegInfo nsacr = {
7735 .name = "NSACR", .type = ARM_CP_CONST,
7736 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
7737 .access = PL1_RW, .accessfn = nsacr_access,
7738 .resetvalue = 0xc00
7739 };
7740 define_one_arm_cp_reg(cpu, &nsacr);
7741 } else {
7742 ARMCPRegInfo nsacr = {
7743 .name = "NSACR",
7744 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
7745 .access = PL3_RW | PL1_R,
7746 .resetvalue = 0,
7747 .fieldoffset = offsetof(CPUARMState, cp15.nsacr)
7748 };
7749 define_one_arm_cp_reg(cpu, &nsacr);
7750 }
7751 } else {
7752 if (arm_feature(env, ARM_FEATURE_V8)) {
7753 ARMCPRegInfo nsacr = {
7754 .name = "NSACR", .type = ARM_CP_CONST,
7755 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
7756 .access = PL1_R,
7757 .resetvalue = 0xc00
7758 };
7759 define_one_arm_cp_reg(cpu, &nsacr);
7760 }
7761 }
7762
452a0955 7763 if (arm_feature(env, ARM_FEATURE_PMSA)) {
6cb0b013
PC
7764 if (arm_feature(env, ARM_FEATURE_V6)) {
7765 /* PMSAv6 not implemented */
7766 assert(arm_feature(env, ARM_FEATURE_V7));
7767 define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
7768 define_arm_cp_regs(cpu, pmsav7_cp_reginfo);
7769 } else {
7770 define_arm_cp_regs(cpu, pmsav5_cp_reginfo);
7771 }
18032bec 7772 } else {
8e5d75c9 7773 define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
18032bec 7774 define_arm_cp_regs(cpu, vmsa_cp_reginfo);
4036b7d1
PM
7775 /* TTCBR2 is introduced with ARMv8.2-AA32HPD. */
7776 if (cpu_isar_feature(aa32_hpd, cpu)) {
ab638a32
RH
7777 define_one_arm_cp_reg(cpu, &ttbcr2_reginfo);
7778 }
18032bec 7779 }
c326b979
PM
7780 if (arm_feature(env, ARM_FEATURE_THUMB2EE)) {
7781 define_arm_cp_regs(cpu, t2ee_cp_reginfo);
7782 }
6cc7a3ae
PM
7783 if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) {
7784 define_arm_cp_regs(cpu, generic_timer_cp_reginfo);
7785 }
4a501606
PM
7786 if (arm_feature(env, ARM_FEATURE_VAPA)) {
7787 define_arm_cp_regs(cpu, vapa_cp_reginfo);
7788 }
c4804214
PM
7789 if (arm_feature(env, ARM_FEATURE_CACHE_TEST_CLEAN)) {
7790 define_arm_cp_regs(cpu, cache_test_clean_cp_reginfo);
7791 }
7792 if (arm_feature(env, ARM_FEATURE_CACHE_DIRTY_REG)) {
7793 define_arm_cp_regs(cpu, cache_dirty_status_cp_reginfo);
7794 }
7795 if (arm_feature(env, ARM_FEATURE_CACHE_BLOCK_OPS)) {
7796 define_arm_cp_regs(cpu, cache_block_ops_cp_reginfo);
7797 }
18032bec
PM
7798 if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
7799 define_arm_cp_regs(cpu, omap_cp_reginfo);
7800 }
34f90529
PM
7801 if (arm_feature(env, ARM_FEATURE_STRONGARM)) {
7802 define_arm_cp_regs(cpu, strongarm_cp_reginfo);
7803 }
1047b9d7
PM
7804 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
7805 define_arm_cp_regs(cpu, xscale_cp_reginfo);
7806 }
7807 if (arm_feature(env, ARM_FEATURE_DUMMY_C15_REGS)) {
7808 define_arm_cp_regs(cpu, dummy_c15_cp_reginfo);
7809 }
7ac681cf
PM
7810 if (arm_feature(env, ARM_FEATURE_LPAE)) {
7811 define_arm_cp_regs(cpu, lpae_cp_reginfo);
7812 }
873b73c0 7813 if (cpu_isar_feature(aa32_jazelle, cpu)) {
f96f3d5f
MZ
7814 define_arm_cp_regs(cpu, jazelle_regs);
7815 }
7884849c
PM
7816 /* Slightly awkwardly, the OMAP and StrongARM cores need all of
7817 * cp15 crn=0 to be writes-ignored, whereas for other cores they should
7818 * be read-only (ie write causes UNDEF exception).
7819 */
7820 {
00a29f3d
PM
7821 ARMCPRegInfo id_pre_v8_midr_cp_reginfo[] = {
7822 /* Pre-v8 MIDR space.
7823 * Note that the MIDR isn't a simple constant register because
7884849c
PM
7824 * of the TI925 behaviour where writes to another register can
7825 * cause the MIDR value to change.
97ce8d61
PC
7826 *
7827 * Unimplemented registers in the c15 0 0 0 space default to
7828 * MIDR. Define MIDR first as this entire space, then CTR, TCMTR
7829 * and friends override accordingly.
7884849c
PM
7830 */
7831 { .name = "MIDR",
97ce8d61 7832 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = CP_ANY,
7884849c 7833 .access = PL1_R, .resetvalue = cpu->midr,
d4e6df63 7834 .writefn = arm_cp_write_ignore, .raw_writefn = raw_write,
731de9e6 7835 .readfn = midr_read,
97ce8d61
PC
7836 .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
7837 .type = ARM_CP_OVERRIDE },
7884849c
PM
7838 /* crn = 0 op1 = 0 crm = 3..7 : currently unassigned; we RAZ. */
7839 { .name = "DUMMY",
7840 .cp = 15, .crn = 0, .crm = 3, .opc1 = 0, .opc2 = CP_ANY,
7841 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
7842 { .name = "DUMMY",
7843 .cp = 15, .crn = 0, .crm = 4, .opc1 = 0, .opc2 = CP_ANY,
7844 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
7845 { .name = "DUMMY",
7846 .cp = 15, .crn = 0, .crm = 5, .opc1 = 0, .opc2 = CP_ANY,
7847 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
7848 { .name = "DUMMY",
7849 .cp = 15, .crn = 0, .crm = 6, .opc1 = 0, .opc2 = CP_ANY,
7850 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
7851 { .name = "DUMMY",
7852 .cp = 15, .crn = 0, .crm = 7, .opc1 = 0, .opc2 = CP_ANY,
7853 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
7854 REGINFO_SENTINEL
7855 };
00a29f3d 7856 ARMCPRegInfo id_v8_midr_cp_reginfo[] = {
00a29f3d
PM
7857 { .name = "MIDR_EL1", .state = ARM_CP_STATE_BOTH,
7858 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 0,
731de9e6
EI
7859 .access = PL1_R, .type = ARM_CP_NO_RAW, .resetvalue = cpu->midr,
7860 .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
7861 .readfn = midr_read },
ac00c79f
SF
7862 /* crn = 0 op1 = 0 crm = 0 op2 = 4,7 : AArch32 aliases of MIDR */
7863 { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
7864 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
7865 .access = PL1_R, .resetvalue = cpu->midr },
7866 { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
7867 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 7,
7868 .access = PL1_R, .resetvalue = cpu->midr },
00a29f3d
PM
7869 { .name = "REVIDR_EL1", .state = ARM_CP_STATE_BOTH,
7870 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 6,
93fbc983
MZ
7871 .access = PL1_R,
7872 .accessfn = access_aa64_tid1,
7873 .type = ARM_CP_CONST, .resetvalue = cpu->revidr },
00a29f3d
PM
7874 REGINFO_SENTINEL
7875 };
7876 ARMCPRegInfo id_cp_reginfo[] = {
7877 /* These are common to v8 and pre-v8 */
7878 { .name = "CTR",
7879 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 1,
630fcd4d
MZ
7880 .access = PL1_R, .accessfn = ctr_el0_access,
7881 .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
00a29f3d
PM
7882 { .name = "CTR_EL0", .state = ARM_CP_STATE_AA64,
7883 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 0, .crm = 0,
7884 .access = PL0_R, .accessfn = ctr_el0_access,
7885 .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
7886 /* TCMTR and TLBTR exist in v8 but have no 64-bit versions */
7887 { .name = "TCMTR",
7888 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 2,
93fbc983
MZ
7889 .access = PL1_R,
7890 .accessfn = access_aa32_tid1,
7891 .type = ARM_CP_CONST, .resetvalue = 0 },
00a29f3d
PM
7892 REGINFO_SENTINEL
7893 };
8085ce63
PC
7894 /* TLBTR is specific to VMSA */
7895 ARMCPRegInfo id_tlbtr_reginfo = {
7896 .name = "TLBTR",
7897 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 3,
93fbc983
MZ
7898 .access = PL1_R,
7899 .accessfn = access_aa32_tid1,
7900 .type = ARM_CP_CONST, .resetvalue = 0,
8085ce63 7901 };
3281af81
PC
7902 /* MPUIR is specific to PMSA V6+ */
7903 ARMCPRegInfo id_mpuir_reginfo = {
7904 .name = "MPUIR",
7905 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
7906 .access = PL1_R, .type = ARM_CP_CONST,
7907 .resetvalue = cpu->pmsav7_dregion << 8
7908 };
7884849c
PM
7909 ARMCPRegInfo crn0_wi_reginfo = {
7910 .name = "CRN0_WI", .cp = 15, .crn = 0, .crm = CP_ANY,
7911 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_W,
7912 .type = ARM_CP_NOP | ARM_CP_OVERRIDE
7913 };
6c5c0fec
AB
7914#ifdef CONFIG_USER_ONLY
7915 ARMCPRegUserSpaceInfo id_v8_user_midr_cp_reginfo[] = {
7916 { .name = "MIDR_EL1",
7917 .exported_bits = 0x00000000ffffffff },
7918 { .name = "REVIDR_EL1" },
7919 REGUSERINFO_SENTINEL
7920 };
7921 modify_arm_cp_regs(id_v8_midr_cp_reginfo, id_v8_user_midr_cp_reginfo);
7922#endif
7884849c
PM
7923 if (arm_feature(env, ARM_FEATURE_OMAPCP) ||
7924 arm_feature(env, ARM_FEATURE_STRONGARM)) {
7925 ARMCPRegInfo *r;
7926 /* Register the blanket "writes ignored" value first to cover the
a703eda1
PC
7927 * whole space. Then update the specific ID registers to allow write
7928 * access, so that they ignore writes rather than causing them to
7929 * UNDEF.
7884849c
PM
7930 */
7931 define_one_arm_cp_reg(cpu, &crn0_wi_reginfo);
00a29f3d
PM
7932 for (r = id_pre_v8_midr_cp_reginfo;
7933 r->type != ARM_CP_SENTINEL; r++) {
7934 r->access = PL1_RW;
7935 }
7884849c
PM
7936 for (r = id_cp_reginfo; r->type != ARM_CP_SENTINEL; r++) {
7937 r->access = PL1_RW;
7884849c 7938 }
10006112 7939 id_mpuir_reginfo.access = PL1_RW;
3281af81 7940 id_tlbtr_reginfo.access = PL1_RW;
7884849c 7941 }
00a29f3d
PM
7942 if (arm_feature(env, ARM_FEATURE_V8)) {
7943 define_arm_cp_regs(cpu, id_v8_midr_cp_reginfo);
7944 } else {
7945 define_arm_cp_regs(cpu, id_pre_v8_midr_cp_reginfo);
7946 }
a703eda1 7947 define_arm_cp_regs(cpu, id_cp_reginfo);
452a0955 7948 if (!arm_feature(env, ARM_FEATURE_PMSA)) {
8085ce63 7949 define_one_arm_cp_reg(cpu, &id_tlbtr_reginfo);
3281af81
PC
7950 } else if (arm_feature(env, ARM_FEATURE_V7)) {
7951 define_one_arm_cp_reg(cpu, &id_mpuir_reginfo);
8085ce63 7952 }
7884849c
PM
7953 }
7954
97ce8d61 7955 if (arm_feature(env, ARM_FEATURE_MPIDR)) {
52264166
AB
7956 ARMCPRegInfo mpidr_cp_reginfo[] = {
7957 { .name = "MPIDR_EL1", .state = ARM_CP_STATE_BOTH,
7958 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 5,
7959 .access = PL1_R, .readfn = mpidr_read, .type = ARM_CP_NO_RAW },
7960 REGINFO_SENTINEL
7961 };
7962#ifdef CONFIG_USER_ONLY
7963 ARMCPRegUserSpaceInfo mpidr_user_cp_reginfo[] = {
7964 { .name = "MPIDR_EL1",
7965 .fixed_bits = 0x0000000080000000 },
7966 REGUSERINFO_SENTINEL
7967 };
7968 modify_arm_cp_regs(mpidr_cp_reginfo, mpidr_user_cp_reginfo);
7969#endif
97ce8d61
PC
7970 define_arm_cp_regs(cpu, mpidr_cp_reginfo);
7971 }
7972
2771db27 7973 if (arm_feature(env, ARM_FEATURE_AUXCR)) {
834a6c69
PM
7974 ARMCPRegInfo auxcr_reginfo[] = {
7975 { .name = "ACTLR_EL1", .state = ARM_CP_STATE_BOTH,
7976 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 1,
99602377
RH
7977 .access = PL1_RW, .accessfn = access_tacr,
7978 .type = ARM_CP_CONST, .resetvalue = cpu->reset_auxcr },
834a6c69
PM
7979 { .name = "ACTLR_EL2", .state = ARM_CP_STATE_BOTH,
7980 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 1,
7981 .access = PL2_RW, .type = ARM_CP_CONST,
7982 .resetvalue = 0 },
7983 { .name = "ACTLR_EL3", .state = ARM_CP_STATE_AA64,
7984 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 1,
7985 .access = PL3_RW, .type = ARM_CP_CONST,
7986 .resetvalue = 0 },
7987 REGINFO_SENTINEL
2771db27 7988 };
834a6c69 7989 define_arm_cp_regs(cpu, auxcr_reginfo);
f6287c24
PM
7990 if (cpu_isar_feature(aa32_ac2, cpu)) {
7991 define_arm_cp_regs(cpu, actlr2_hactlr2_reginfo);
0e0456ab 7992 }
2771db27
PM
7993 }
7994
d8ba780b 7995 if (arm_feature(env, ARM_FEATURE_CBAR)) {
d56974af
LM
7996 /*
7997 * CBAR is IMPDEF, but common on Arm Cortex-A implementations.
7998 * There are two flavours:
7999 * (1) older 32-bit only cores have a simple 32-bit CBAR
8000 * (2) 64-bit cores have a 64-bit CBAR visible to AArch64, plus a
8001 * 32-bit register visible to AArch32 at a different encoding
8002 * to the "flavour 1" register and with the bits rearranged to
8003 * be able to squash a 64-bit address into the 32-bit view.
8004 * We distinguish the two via the ARM_FEATURE_AARCH64 flag, but
8005 * in future if we support AArch32-only configs of some of the
8006 * AArch64 cores we might need to add a specific feature flag
8007 * to indicate cores with "flavour 2" CBAR.
8008 */
f318cec6
PM
8009 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
8010 /* 32 bit view is [31:18] 0...0 [43:32]. */
8011 uint32_t cbar32 = (extract64(cpu->reset_cbar, 18, 14) << 18)
8012 | extract64(cpu->reset_cbar, 32, 12);
8013 ARMCPRegInfo cbar_reginfo[] = {
8014 { .name = "CBAR",
8015 .type = ARM_CP_CONST,
d56974af
LM
8016 .cp = 15, .crn = 15, .crm = 3, .opc1 = 1, .opc2 = 0,
8017 .access = PL1_R, .resetvalue = cbar32 },
f318cec6
PM
8018 { .name = "CBAR_EL1", .state = ARM_CP_STATE_AA64,
8019 .type = ARM_CP_CONST,
8020 .opc0 = 3, .opc1 = 1, .crn = 15, .crm = 3, .opc2 = 0,
d56974af 8021 .access = PL1_R, .resetvalue = cpu->reset_cbar },
f318cec6
PM
8022 REGINFO_SENTINEL
8023 };
8024 /* We don't implement a r/w 64 bit CBAR currently */
8025 assert(arm_feature(env, ARM_FEATURE_CBAR_RO));
8026 define_arm_cp_regs(cpu, cbar_reginfo);
8027 } else {
8028 ARMCPRegInfo cbar = {
8029 .name = "CBAR",
8030 .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0,
8031 .access = PL1_R|PL3_W, .resetvalue = cpu->reset_cbar,
8032 .fieldoffset = offsetof(CPUARMState,
8033 cp15.c15_config_base_address)
8034 };
8035 if (arm_feature(env, ARM_FEATURE_CBAR_RO)) {
8036 cbar.access = PL1_R;
8037 cbar.fieldoffset = 0;
8038 cbar.type = ARM_CP_CONST;
8039 }
8040 define_one_arm_cp_reg(cpu, &cbar);
8041 }
d8ba780b
PC
8042 }
8043
91db4642
CLG
8044 if (arm_feature(env, ARM_FEATURE_VBAR)) {
8045 ARMCPRegInfo vbar_cp_reginfo[] = {
8046 { .name = "VBAR", .state = ARM_CP_STATE_BOTH,
8047 .opc0 = 3, .crn = 12, .crm = 0, .opc1 = 0, .opc2 = 0,
8048 .access = PL1_RW, .writefn = vbar_write,
8049 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.vbar_s),
8050 offsetof(CPUARMState, cp15.vbar_ns) },
8051 .resetvalue = 0 },
8052 REGINFO_SENTINEL
8053 };
8054 define_arm_cp_regs(cpu, vbar_cp_reginfo);
8055 }
8056
2771db27
PM
8057 /* Generic registers whose values depend on the implementation */
8058 {
8059 ARMCPRegInfo sctlr = {
5ebafdf3 8060 .name = "SCTLR", .state = ARM_CP_STATE_BOTH,
137feaa9 8061 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
84929218 8062 .access = PL1_RW, .accessfn = access_tvm_trvm,
137feaa9
FA
8063 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.sctlr_s),
8064 offsetof(CPUARMState, cp15.sctlr_ns) },
d4e6df63
PM
8065 .writefn = sctlr_write, .resetvalue = cpu->reset_sctlr,
8066 .raw_writefn = raw_write,
2771db27
PM
8067 };
8068 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
8069 /* Normally we would always end the TB on an SCTLR write, but Linux
8070 * arch/arm/mach-pxa/sleep.S expects two instructions following
8071 * an MMU enable to execute from cache. Imitate this behaviour.
8072 */
8073 sctlr.type |= ARM_CP_SUPPRESS_TB_END;
8074 }
8075 define_one_arm_cp_reg(cpu, &sctlr);
8076 }
5be5e8ed 8077
2d7137c1 8078 if (cpu_isar_feature(aa64_lor, cpu)) {
2d7137c1
RH
8079 define_arm_cp_regs(cpu, lor_reginfo);
8080 }
220f508f
RH
8081 if (cpu_isar_feature(aa64_pan, cpu)) {
8082 define_one_arm_cp_reg(cpu, &pan_reginfo);
8083 }
04b07d29
RH
8084#ifndef CONFIG_USER_ONLY
8085 if (cpu_isar_feature(aa64_ats1e1, cpu)) {
8086 define_arm_cp_regs(cpu, ats1e1_reginfo);
8087 }
8088 if (cpu_isar_feature(aa32_ats1e1, cpu)) {
8089 define_arm_cp_regs(cpu, ats1cp_reginfo);
8090 }
8091#endif
9eeb7a1c
RH
8092 if (cpu_isar_feature(aa64_uao, cpu)) {
8093 define_one_arm_cp_reg(cpu, &uao_reginfo);
8094 }
2d7137c1 8095
e2a1a461
RH
8096 if (arm_feature(env, ARM_FEATURE_EL2) && cpu_isar_feature(aa64_vh, cpu)) {
8097 define_arm_cp_regs(cpu, vhe_reginfo);
8098 }
8099
cd208a1c 8100 if (cpu_isar_feature(aa64_sve, cpu)) {
5be5e8ed
RH
8101 define_one_arm_cp_reg(cpu, &zcr_el1_reginfo);
8102 if (arm_feature(env, ARM_FEATURE_EL2)) {
8103 define_one_arm_cp_reg(cpu, &zcr_el2_reginfo);
8104 } else {
8105 define_one_arm_cp_reg(cpu, &zcr_no_el2_reginfo);
8106 }
8107 if (arm_feature(env, ARM_FEATURE_EL3)) {
8108 define_one_arm_cp_reg(cpu, &zcr_el3_reginfo);
8109 }
8110 }
967aa94f
RH
8111
8112#ifdef TARGET_AARCH64
8113 if (cpu_isar_feature(aa64_pauth, cpu)) {
8114 define_arm_cp_regs(cpu, pauth_reginfo);
8115 }
de390645
RH
8116 if (cpu_isar_feature(aa64_rndr, cpu)) {
8117 define_arm_cp_regs(cpu, rndr_reginfo);
8118 }
0d57b499
BM
8119#ifndef CONFIG_USER_ONLY
8120 /* Data Cache clean instructions up to PoP */
8121 if (cpu_isar_feature(aa64_dcpop, cpu)) {
8122 define_one_arm_cp_reg(cpu, dcpop_reg);
8123
8124 if (cpu_isar_feature(aa64_dcpodp, cpu)) {
8125 define_one_arm_cp_reg(cpu, dcpodp_reg);
8126 }
8127 }
8128#endif /*CONFIG_USER_ONLY*/
4b779ceb
RH
8129
8130 /*
8131 * If full MTE is enabled, add all of the system registers.
8132 * If only "instructions available at EL0" are enabled,
8133 * then define only a RAZ/WI version of PSTATE.TCO.
8134 */
8135 if (cpu_isar_feature(aa64_mte, cpu)) {
8136 define_arm_cp_regs(cpu, mte_reginfo);
5463df16 8137 define_arm_cp_regs(cpu, mte_el0_cacheop_reginfo);
4b779ceb
RH
8138 } else if (cpu_isar_feature(aa64_mte_insn_reg, cpu)) {
8139 define_arm_cp_regs(cpu, mte_tco_ro_reginfo);
5463df16 8140 define_arm_cp_regs(cpu, mte_el0_cacheop_reginfo);
4b779ceb 8141 }
967aa94f 8142#endif
cb570bd3 8143
22e57073 8144 if (cpu_isar_feature(any_predinv, cpu)) {
cb570bd3
RH
8145 define_arm_cp_regs(cpu, predinv_reginfo);
8146 }
e2cce18f 8147
957e6155
PM
8148 if (cpu_isar_feature(any_ccidx, cpu)) {
8149 define_arm_cp_regs(cpu, ccsidr2_reginfo);
8150 }
8151
e2cce18f
RH
8152#ifndef CONFIG_USER_ONLY
8153 /*
8154 * Register redirections and aliases must be done last,
8155 * after the registers from the other extensions have been defined.
8156 */
8157 if (arm_feature(env, ARM_FEATURE_EL2) && cpu_isar_feature(aa64_vh, cpu)) {
8158 define_arm_vh_e2h_redirects_aliases(cpu);
8159 }
8160#endif
2ceb98c0
PM
8161}
8162
14969266
AF
8163void arm_cpu_register_gdb_regs_for_features(ARMCPU *cpu)
8164{
22169d41 8165 CPUState *cs = CPU(cpu);
14969266
AF
8166 CPUARMState *env = &cpu->env;
8167
6a669427 8168 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
d12379c5
AB
8169 /*
8170 * The lower part of each SVE register aliases to the FPU
8171 * registers so we don't need to include both.
8172 */
8173#ifdef TARGET_AARCH64
8174 if (isar_feature_aa64_sve(&cpu->isar)) {
8175 gdb_register_coprocessor(cs, arm_gdb_get_svereg, arm_gdb_set_svereg,
8176 arm_gen_dynamic_svereg_xml(cs, cs->gdb_num_regs),
8177 "sve-registers.xml", 0);
8178 } else
8179#endif
8180 {
8181 gdb_register_coprocessor(cs, aarch64_fpu_gdb_get_reg,
8182 aarch64_fpu_gdb_set_reg,
8183 34, "aarch64-fpu.xml", 0);
8184 }
6a669427 8185 } else if (arm_feature(env, ARM_FEATURE_NEON)) {
22169d41 8186 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
56aebc89 8187 51, "arm-neon.xml", 0);
a6627f5f 8188 } else if (cpu_isar_feature(aa32_simd_r32, cpu)) {
22169d41 8189 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
56aebc89 8190 35, "arm-vfp3.xml", 0);
7fbc6a40 8191 } else if (cpu_isar_feature(aa32_vfp_simd, cpu)) {
22169d41 8192 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
56aebc89
PB
8193 19, "arm-vfp.xml", 0);
8194 }
200bf5b7 8195 gdb_register_coprocessor(cs, arm_gdb_get_sysreg, arm_gdb_set_sysreg,
32d6e32a 8196 arm_gen_dynamic_sysreg_xml(cs, cs->gdb_num_regs),
200bf5b7 8197 "system-registers.xml", 0);
d12379c5 8198
40f137e1
PB
8199}
8200
777dc784
PM
8201/* Sort alphabetically by type name, except for "any". */
8202static gint arm_cpu_list_compare(gconstpointer a, gconstpointer b)
5adb4839 8203{
777dc784
PM
8204 ObjectClass *class_a = (ObjectClass *)a;
8205 ObjectClass *class_b = (ObjectClass *)b;
8206 const char *name_a, *name_b;
5adb4839 8207
777dc784
PM
8208 name_a = object_class_get_name(class_a);
8209 name_b = object_class_get_name(class_b);
51492fd1 8210 if (strcmp(name_a, "any-" TYPE_ARM_CPU) == 0) {
777dc784 8211 return 1;
51492fd1 8212 } else if (strcmp(name_b, "any-" TYPE_ARM_CPU) == 0) {
777dc784
PM
8213 return -1;
8214 } else {
8215 return strcmp(name_a, name_b);
5adb4839
PB
8216 }
8217}
8218
777dc784 8219static void arm_cpu_list_entry(gpointer data, gpointer user_data)
40f137e1 8220{
777dc784 8221 ObjectClass *oc = data;
51492fd1
AF
8222 const char *typename;
8223 char *name;
3371d272 8224
51492fd1
AF
8225 typename = object_class_get_name(oc);
8226 name = g_strndup(typename, strlen(typename) - strlen("-" TYPE_ARM_CPU));
0442428a 8227 qemu_printf(" %s\n", name);
51492fd1 8228 g_free(name);
777dc784
PM
8229}
8230
0442428a 8231void arm_cpu_list(void)
777dc784 8232{
777dc784
PM
8233 GSList *list;
8234
8235 list = object_class_get_list(TYPE_ARM_CPU, false);
8236 list = g_slist_sort(list, arm_cpu_list_compare);
0442428a
MA
8237 qemu_printf("Available CPUs:\n");
8238 g_slist_foreach(list, arm_cpu_list_entry, NULL);
777dc784 8239 g_slist_free(list);
40f137e1
PB
8240}
8241
78027bb6
CR
8242static void arm_cpu_add_definition(gpointer data, gpointer user_data)
8243{
8244 ObjectClass *oc = data;
8245 CpuDefinitionInfoList **cpu_list = user_data;
8246 CpuDefinitionInfoList *entry;
8247 CpuDefinitionInfo *info;
8248 const char *typename;
8249
8250 typename = object_class_get_name(oc);
8251 info = g_malloc0(sizeof(*info));
8252 info->name = g_strndup(typename,
8253 strlen(typename) - strlen("-" TYPE_ARM_CPU));
8ed877b7 8254 info->q_typename = g_strdup(typename);
78027bb6
CR
8255
8256 entry = g_malloc0(sizeof(*entry));
8257 entry->value = info;
8258 entry->next = *cpu_list;
8259 *cpu_list = entry;
8260}
8261
25a9d6ca 8262CpuDefinitionInfoList *qmp_query_cpu_definitions(Error **errp)
78027bb6
CR
8263{
8264 CpuDefinitionInfoList *cpu_list = NULL;
8265 GSList *list;
8266
8267 list = object_class_get_list(TYPE_ARM_CPU, false);
8268 g_slist_foreach(list, arm_cpu_add_definition, &cpu_list);
8269 g_slist_free(list);
8270
8271 return cpu_list;
8272}
8273
6e6efd61 8274static void add_cpreg_to_hashtable(ARMCPU *cpu, const ARMCPRegInfo *r,
51a79b03 8275 void *opaque, int state, int secstate,
9c513e78
AB
8276 int crm, int opc1, int opc2,
8277 const char *name)
6e6efd61
PM
8278{
8279 /* Private utility function for define_one_arm_cp_reg_with_opaque():
8280 * add a single reginfo struct to the hash table.
8281 */
8282 uint32_t *key = g_new(uint32_t, 1);
8283 ARMCPRegInfo *r2 = g_memdup(r, sizeof(ARMCPRegInfo));
8284 int is64 = (r->type & ARM_CP_64BIT) ? 1 : 0;
3f3c82a5
FA
8285 int ns = (secstate & ARM_CP_SECSTATE_NS) ? 1 : 0;
8286
9c513e78 8287 r2->name = g_strdup(name);
3f3c82a5
FA
8288 /* Reset the secure state to the specific incoming state. This is
8289 * necessary as the register may have been defined with both states.
8290 */
8291 r2->secure = secstate;
8292
8293 if (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1]) {
8294 /* Register is banked (using both entries in array).
8295 * Overwriting fieldoffset as the array is only used to define
8296 * banked registers but later only fieldoffset is used.
f5a0a5a5 8297 */
3f3c82a5
FA
8298 r2->fieldoffset = r->bank_fieldoffsets[ns];
8299 }
8300
8301 if (state == ARM_CP_STATE_AA32) {
8302 if (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1]) {
8303 /* If the register is banked then we don't need to migrate or
8304 * reset the 32-bit instance in certain cases:
8305 *
8306 * 1) If the register has both 32-bit and 64-bit instances then we
8307 * can count on the 64-bit instance taking care of the
8308 * non-secure bank.
8309 * 2) If ARMv8 is enabled then we can count on a 64-bit version
8310 * taking care of the secure bank. This requires that separate
8311 * 32 and 64-bit definitions are provided.
8312 */
8313 if ((r->state == ARM_CP_STATE_BOTH && ns) ||
8314 (arm_feature(&cpu->env, ARM_FEATURE_V8) && !ns)) {
7a0e58fa 8315 r2->type |= ARM_CP_ALIAS;
3f3c82a5
FA
8316 }
8317 } else if ((secstate != r->secure) && !ns) {
8318 /* The register is not banked so we only want to allow migration of
8319 * the non-secure instance.
8320 */
7a0e58fa 8321 r2->type |= ARM_CP_ALIAS;
58a1d8ce 8322 }
3f3c82a5
FA
8323
8324 if (r->state == ARM_CP_STATE_BOTH) {
8325 /* We assume it is a cp15 register if the .cp field is left unset.
8326 */
8327 if (r2->cp == 0) {
8328 r2->cp = 15;
8329 }
8330
f5a0a5a5 8331#ifdef HOST_WORDS_BIGENDIAN
3f3c82a5
FA
8332 if (r2->fieldoffset) {
8333 r2->fieldoffset += sizeof(uint32_t);
8334 }
f5a0a5a5 8335#endif
3f3c82a5 8336 }
f5a0a5a5
PM
8337 }
8338 if (state == ARM_CP_STATE_AA64) {
8339 /* To allow abbreviation of ARMCPRegInfo
8340 * definitions, we treat cp == 0 as equivalent to
8341 * the value for "standard guest-visible sysreg".
58a1d8ce
PM
8342 * STATE_BOTH definitions are also always "standard
8343 * sysreg" in their AArch64 view (the .cp value may
8344 * be non-zero for the benefit of the AArch32 view).
f5a0a5a5 8345 */
58a1d8ce 8346 if (r->cp == 0 || r->state == ARM_CP_STATE_BOTH) {
f5a0a5a5
PM
8347 r2->cp = CP_REG_ARM64_SYSREG_CP;
8348 }
8349 *key = ENCODE_AA64_CP_REG(r2->cp, r2->crn, crm,
8350 r2->opc0, opc1, opc2);
8351 } else {
51a79b03 8352 *key = ENCODE_CP_REG(r2->cp, is64, ns, r2->crn, crm, opc1, opc2);
f5a0a5a5 8353 }
6e6efd61
PM
8354 if (opaque) {
8355 r2->opaque = opaque;
8356 }
67ed771d
PM
8357 /* reginfo passed to helpers is correct for the actual access,
8358 * and is never ARM_CP_STATE_BOTH:
8359 */
8360 r2->state = state;
6e6efd61
PM
8361 /* Make sure reginfo passed to helpers for wildcarded regs
8362 * has the correct crm/opc1/opc2 for this reg, not CP_ANY:
8363 */
8364 r2->crm = crm;
8365 r2->opc1 = opc1;
8366 r2->opc2 = opc2;
8367 /* By convention, for wildcarded registers only the first
8368 * entry is used for migration; the others are marked as
7a0e58fa 8369 * ALIAS so we don't try to transfer the register
6e6efd61 8370 * multiple times. Special registers (ie NOP/WFI) are
7a0e58fa 8371 * never migratable and not even raw-accessible.
6e6efd61 8372 */
7a0e58fa
PM
8373 if ((r->type & ARM_CP_SPECIAL)) {
8374 r2->type |= ARM_CP_NO_RAW;
8375 }
8376 if (((r->crm == CP_ANY) && crm != 0) ||
6e6efd61
PM
8377 ((r->opc1 == CP_ANY) && opc1 != 0) ||
8378 ((r->opc2 == CP_ANY) && opc2 != 0)) {
1f163787 8379 r2->type |= ARM_CP_ALIAS | ARM_CP_NO_GDB;
6e6efd61
PM
8380 }
8381
375421cc
PM
8382 /* Check that raw accesses are either forbidden or handled. Note that
8383 * we can't assert this earlier because the setup of fieldoffset for
8384 * banked registers has to be done first.
8385 */
8386 if (!(r2->type & ARM_CP_NO_RAW)) {
8387 assert(!raw_accessors_invalid(r2));
8388 }
8389
6e6efd61
PM
8390 /* Overriding of an existing definition must be explicitly
8391 * requested.
8392 */
8393 if (!(r->type & ARM_CP_OVERRIDE)) {
8394 ARMCPRegInfo *oldreg;
8395 oldreg = g_hash_table_lookup(cpu->cp_regs, key);
8396 if (oldreg && !(oldreg->type & ARM_CP_OVERRIDE)) {
8397 fprintf(stderr, "Register redefined: cp=%d %d bit "
8398 "crn=%d crm=%d opc1=%d opc2=%d, "
8399 "was %s, now %s\n", r2->cp, 32 + 32 * is64,
8400 r2->crn, r2->crm, r2->opc1, r2->opc2,
8401 oldreg->name, r2->name);
8402 g_assert_not_reached();
8403 }
8404 }
8405 g_hash_table_insert(cpu->cp_regs, key, r2);
8406}
8407
8408
4b6a83fb
PM
8409void define_one_arm_cp_reg_with_opaque(ARMCPU *cpu,
8410 const ARMCPRegInfo *r, void *opaque)
8411{
8412 /* Define implementations of coprocessor registers.
8413 * We store these in a hashtable because typically
8414 * there are less than 150 registers in a space which
8415 * is 16*16*16*8*8 = 262144 in size.
8416 * Wildcarding is supported for the crm, opc1 and opc2 fields.
8417 * If a register is defined twice then the second definition is
8418 * used, so this can be used to define some generic registers and
8419 * then override them with implementation specific variations.
8420 * At least one of the original and the second definition should
8421 * include ARM_CP_OVERRIDE in its type bits -- this is just a guard
8422 * against accidental use.
f5a0a5a5
PM
8423 *
8424 * The state field defines whether the register is to be
8425 * visible in the AArch32 or AArch64 execution state. If the
8426 * state is set to ARM_CP_STATE_BOTH then we synthesise a
8427 * reginfo structure for the AArch32 view, which sees the lower
8428 * 32 bits of the 64 bit register.
8429 *
8430 * Only registers visible in AArch64 may set r->opc0; opc0 cannot
8431 * be wildcarded. AArch64 registers are always considered to be 64
8432 * bits; the ARM_CP_64BIT* flag applies only to the AArch32 view of
8433 * the register, if any.
4b6a83fb 8434 */
f5a0a5a5 8435 int crm, opc1, opc2, state;
4b6a83fb
PM
8436 int crmmin = (r->crm == CP_ANY) ? 0 : r->crm;
8437 int crmmax = (r->crm == CP_ANY) ? 15 : r->crm;
8438 int opc1min = (r->opc1 == CP_ANY) ? 0 : r->opc1;
8439 int opc1max = (r->opc1 == CP_ANY) ? 7 : r->opc1;
8440 int opc2min = (r->opc2 == CP_ANY) ? 0 : r->opc2;
8441 int opc2max = (r->opc2 == CP_ANY) ? 7 : r->opc2;
8442 /* 64 bit registers have only CRm and Opc1 fields */
8443 assert(!((r->type & ARM_CP_64BIT) && (r->opc2 || r->crn)));
f5a0a5a5
PM
8444 /* op0 only exists in the AArch64 encodings */
8445 assert((r->state != ARM_CP_STATE_AA32) || (r->opc0 == 0));
8446 /* AArch64 regs are all 64 bit so ARM_CP_64BIT is meaningless */
8447 assert((r->state != ARM_CP_STATE_AA64) || !(r->type & ARM_CP_64BIT));
8448 /* The AArch64 pseudocode CheckSystemAccess() specifies that op1
8449 * encodes a minimum access level for the register. We roll this
8450 * runtime check into our general permission check code, so check
8451 * here that the reginfo's specified permissions are strict enough
8452 * to encompass the generic architectural permission check.
8453 */
8454 if (r->state != ARM_CP_STATE_AA32) {
8455 int mask = 0;
8456 switch (r->opc1) {
b5bd7440
AB
8457 case 0:
8458 /* min_EL EL1, but some accessible to EL0 via kernel ABI */
8459 mask = PL0U_R | PL1_RW;
8460 break;
8461 case 1: case 2:
f5a0a5a5
PM
8462 /* min_EL EL1 */
8463 mask = PL1_RW;
8464 break;
8465 case 3:
8466 /* min_EL EL0 */
8467 mask = PL0_RW;
8468 break;
8469 case 4:
b4ecf60f 8470 case 5:
f5a0a5a5
PM
8471 /* min_EL EL2 */
8472 mask = PL2_RW;
8473 break;
f5a0a5a5
PM
8474 case 6:
8475 /* min_EL EL3 */
8476 mask = PL3_RW;
8477 break;
8478 case 7:
8479 /* min_EL EL1, secure mode only (we don't check the latter) */
8480 mask = PL1_RW;
8481 break;
8482 default:
8483 /* broken reginfo with out-of-range opc1 */
8484 assert(false);
8485 break;
8486 }
8487 /* assert our permissions are not too lax (stricter is fine) */
8488 assert((r->access & ~mask) == 0);
8489 }
8490
4b6a83fb
PM
8491 /* Check that the register definition has enough info to handle
8492 * reads and writes if they are permitted.
8493 */
8494 if (!(r->type & (ARM_CP_SPECIAL|ARM_CP_CONST))) {
8495 if (r->access & PL3_R) {
3f3c82a5
FA
8496 assert((r->fieldoffset ||
8497 (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
8498 r->readfn);
4b6a83fb
PM
8499 }
8500 if (r->access & PL3_W) {
3f3c82a5
FA
8501 assert((r->fieldoffset ||
8502 (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
8503 r->writefn);
4b6a83fb
PM
8504 }
8505 }
8506 /* Bad type field probably means missing sentinel at end of reg list */
8507 assert(cptype_valid(r->type));
8508 for (crm = crmmin; crm <= crmmax; crm++) {
8509 for (opc1 = opc1min; opc1 <= opc1max; opc1++) {
8510 for (opc2 = opc2min; opc2 <= opc2max; opc2++) {
f5a0a5a5
PM
8511 for (state = ARM_CP_STATE_AA32;
8512 state <= ARM_CP_STATE_AA64; state++) {
8513 if (r->state != state && r->state != ARM_CP_STATE_BOTH) {
8514 continue;
8515 }
3f3c82a5
FA
8516 if (state == ARM_CP_STATE_AA32) {
8517 /* Under AArch32 CP registers can be common
8518 * (same for secure and non-secure world) or banked.
8519 */
9c513e78
AB
8520 char *name;
8521
3f3c82a5
FA
8522 switch (r->secure) {
8523 case ARM_CP_SECSTATE_S:
8524 case ARM_CP_SECSTATE_NS:
8525 add_cpreg_to_hashtable(cpu, r, opaque, state,
9c513e78
AB
8526 r->secure, crm, opc1, opc2,
8527 r->name);
3f3c82a5
FA
8528 break;
8529 default:
9c513e78 8530 name = g_strdup_printf("%s_S", r->name);
3f3c82a5
FA
8531 add_cpreg_to_hashtable(cpu, r, opaque, state,
8532 ARM_CP_SECSTATE_S,
9c513e78
AB
8533 crm, opc1, opc2, name);
8534 g_free(name);
3f3c82a5
FA
8535 add_cpreg_to_hashtable(cpu, r, opaque, state,
8536 ARM_CP_SECSTATE_NS,
9c513e78 8537 crm, opc1, opc2, r->name);
3f3c82a5
FA
8538 break;
8539 }
8540 } else {
8541 /* AArch64 registers get mapped to non-secure instance
8542 * of AArch32 */
8543 add_cpreg_to_hashtable(cpu, r, opaque, state,
8544 ARM_CP_SECSTATE_NS,
9c513e78 8545 crm, opc1, opc2, r->name);
3f3c82a5 8546 }
f5a0a5a5 8547 }
4b6a83fb
PM
8548 }
8549 }
8550 }
8551}
8552
8553void define_arm_cp_regs_with_opaque(ARMCPU *cpu,
8554 const ARMCPRegInfo *regs, void *opaque)
8555{
8556 /* Define a whole list of registers */
8557 const ARMCPRegInfo *r;
8558 for (r = regs; r->type != ARM_CP_SENTINEL; r++) {
8559 define_one_arm_cp_reg_with_opaque(cpu, r, opaque);
8560 }
8561}
8562
6c5c0fec
AB
8563/*
8564 * Modify ARMCPRegInfo for access from userspace.
8565 *
8566 * This is a data driven modification directed by
8567 * ARMCPRegUserSpaceInfo. All registers become ARM_CP_CONST as
8568 * user-space cannot alter any values and dynamic values pertaining to
8569 * execution state are hidden from user space view anyway.
8570 */
8571void modify_arm_cp_regs(ARMCPRegInfo *regs, const ARMCPRegUserSpaceInfo *mods)
8572{
8573 const ARMCPRegUserSpaceInfo *m;
8574 ARMCPRegInfo *r;
8575
8576 for (m = mods; m->name; m++) {
d040242e
AB
8577 GPatternSpec *pat = NULL;
8578 if (m->is_glob) {
8579 pat = g_pattern_spec_new(m->name);
8580 }
6c5c0fec 8581 for (r = regs; r->type != ARM_CP_SENTINEL; r++) {
d040242e
AB
8582 if (pat && g_pattern_match_string(pat, r->name)) {
8583 r->type = ARM_CP_CONST;
8584 r->access = PL0U_R;
8585 r->resetvalue = 0;
8586 /* continue */
8587 } else if (strcmp(r->name, m->name) == 0) {
6c5c0fec
AB
8588 r->type = ARM_CP_CONST;
8589 r->access = PL0U_R;
8590 r->resetvalue &= m->exported_bits;
8591 r->resetvalue |= m->fixed_bits;
8592 break;
8593 }
8594 }
d040242e
AB
8595 if (pat) {
8596 g_pattern_spec_free(pat);
8597 }
6c5c0fec
AB
8598 }
8599}
8600
60322b39 8601const ARMCPRegInfo *get_arm_cp_reginfo(GHashTable *cpregs, uint32_t encoded_cp)
4b6a83fb 8602{
60322b39 8603 return g_hash_table_lookup(cpregs, &encoded_cp);
4b6a83fb
PM
8604}
8605
c4241c7d
PM
8606void arm_cp_write_ignore(CPUARMState *env, const ARMCPRegInfo *ri,
8607 uint64_t value)
4b6a83fb
PM
8608{
8609 /* Helper coprocessor write function for write-ignore registers */
4b6a83fb
PM
8610}
8611
c4241c7d 8612uint64_t arm_cp_read_zero(CPUARMState *env, const ARMCPRegInfo *ri)
4b6a83fb
PM
8613{
8614 /* Helper coprocessor write function for read-as-zero registers */
4b6a83fb
PM
8615 return 0;
8616}
8617
f5a0a5a5
PM
8618void arm_cp_reset_ignore(CPUARMState *env, const ARMCPRegInfo *opaque)
8619{
8620 /* Helper coprocessor reset function for do-nothing-on-reset registers */
8621}
8622
af393ffc 8623static int bad_mode_switch(CPUARMState *env, int mode, CPSRWriteType write_type)
37064a8b
PM
8624{
8625 /* Return true if it is not valid for us to switch to
8626 * this CPU mode (ie all the UNPREDICTABLE cases in
8627 * the ARM ARM CPSRWriteByInstr pseudocode).
8628 */
af393ffc
PM
8629
8630 /* Changes to or from Hyp via MSR and CPS are illegal. */
8631 if (write_type == CPSRWriteByInstr &&
8632 ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_HYP ||
8633 mode == ARM_CPU_MODE_HYP)) {
8634 return 1;
8635 }
8636
37064a8b
PM
8637 switch (mode) {
8638 case ARM_CPU_MODE_USR:
10eacda7 8639 return 0;
37064a8b
PM
8640 case ARM_CPU_MODE_SYS:
8641 case ARM_CPU_MODE_SVC:
8642 case ARM_CPU_MODE_ABT:
8643 case ARM_CPU_MODE_UND:
8644 case ARM_CPU_MODE_IRQ:
8645 case ARM_CPU_MODE_FIQ:
52ff951b
PM
8646 /* Note that we don't implement the IMPDEF NSACR.RFR which in v7
8647 * allows FIQ mode to be Secure-only. (In v8 this doesn't exist.)
8648 */
10eacda7
PM
8649 /* If HCR.TGE is set then changes from Monitor to NS PL1 via MSR
8650 * and CPS are treated as illegal mode changes.
8651 */
8652 if (write_type == CPSRWriteByInstr &&
10eacda7 8653 (env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON &&
7c208e0f 8654 (arm_hcr_el2_eff(env) & HCR_TGE)) {
10eacda7
PM
8655 return 1;
8656 }
37064a8b 8657 return 0;
e6c8fc07
PM
8658 case ARM_CPU_MODE_HYP:
8659 return !arm_feature(env, ARM_FEATURE_EL2)
2d2a4549 8660 || arm_current_el(env) < 2 || arm_is_secure_below_el3(env);
027fc527 8661 case ARM_CPU_MODE_MON:
58ae2d1f 8662 return arm_current_el(env) < 3;
37064a8b
PM
8663 default:
8664 return 1;
8665 }
8666}
8667
2f4a40e5
AZ
8668uint32_t cpsr_read(CPUARMState *env)
8669{
8670 int ZF;
6fbe23d5
PB
8671 ZF = (env->ZF == 0);
8672 return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) |
2f4a40e5
AZ
8673 (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27)
8674 | (env->thumb << 5) | ((env->condexec_bits & 3) << 25)
8675 | ((env->condexec_bits & 0xfc) << 8)
af519934 8676 | (env->GE << 16) | (env->daif & CPSR_AIF);
2f4a40e5
AZ
8677}
8678
50866ba5
PM
8679void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask,
8680 CPSRWriteType write_type)
2f4a40e5 8681{
6e8801f9
FA
8682 uint32_t changed_daif;
8683
2f4a40e5 8684 if (mask & CPSR_NZCV) {
6fbe23d5
PB
8685 env->ZF = (~val) & CPSR_Z;
8686 env->NF = val;
2f4a40e5
AZ
8687 env->CF = (val >> 29) & 1;
8688 env->VF = (val << 3) & 0x80000000;
8689 }
8690 if (mask & CPSR_Q)
8691 env->QF = ((val & CPSR_Q) != 0);
8692 if (mask & CPSR_T)
8693 env->thumb = ((val & CPSR_T) != 0);
8694 if (mask & CPSR_IT_0_1) {
8695 env->condexec_bits &= ~3;
8696 env->condexec_bits |= (val >> 25) & 3;
8697 }
8698 if (mask & CPSR_IT_2_7) {
8699 env->condexec_bits &= 3;
8700 env->condexec_bits |= (val >> 8) & 0xfc;
8701 }
8702 if (mask & CPSR_GE) {
8703 env->GE = (val >> 16) & 0xf;
8704 }
8705
6e8801f9
FA
8706 /* In a V7 implementation that includes the security extensions but does
8707 * not include Virtualization Extensions the SCR.FW and SCR.AW bits control
8708 * whether non-secure software is allowed to change the CPSR_F and CPSR_A
8709 * bits respectively.
8710 *
8711 * In a V8 implementation, it is permitted for privileged software to
8712 * change the CPSR A/F bits regardless of the SCR.AW/FW bits.
8713 */
f8c88bbc 8714 if (write_type != CPSRWriteRaw && !arm_feature(env, ARM_FEATURE_V8) &&
6e8801f9
FA
8715 arm_feature(env, ARM_FEATURE_EL3) &&
8716 !arm_feature(env, ARM_FEATURE_EL2) &&
8717 !arm_is_secure(env)) {
8718
8719 changed_daif = (env->daif ^ val) & mask;
8720
8721 if (changed_daif & CPSR_A) {
8722 /* Check to see if we are allowed to change the masking of async
8723 * abort exceptions from a non-secure state.
8724 */
8725 if (!(env->cp15.scr_el3 & SCR_AW)) {
8726 qemu_log_mask(LOG_GUEST_ERROR,
8727 "Ignoring attempt to switch CPSR_A flag from "
8728 "non-secure world with SCR.AW bit clear\n");
8729 mask &= ~CPSR_A;
8730 }
8731 }
8732
8733 if (changed_daif & CPSR_F) {
8734 /* Check to see if we are allowed to change the masking of FIQ
8735 * exceptions from a non-secure state.
8736 */
8737 if (!(env->cp15.scr_el3 & SCR_FW)) {
8738 qemu_log_mask(LOG_GUEST_ERROR,
8739 "Ignoring attempt to switch CPSR_F flag from "
8740 "non-secure world with SCR.FW bit clear\n");
8741 mask &= ~CPSR_F;
8742 }
8743
8744 /* Check whether non-maskable FIQ (NMFI) support is enabled.
8745 * If this bit is set software is not allowed to mask
8746 * FIQs, but is allowed to set CPSR_F to 0.
8747 */
8748 if ((A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_NMFI) &&
8749 (val & CPSR_F)) {
8750 qemu_log_mask(LOG_GUEST_ERROR,
8751 "Ignoring attempt to enable CPSR_F flag "
8752 "(non-maskable FIQ [NMFI] support enabled)\n");
8753 mask &= ~CPSR_F;
8754 }
8755 }
8756 }
8757
4cc35614
PM
8758 env->daif &= ~(CPSR_AIF & mask);
8759 env->daif |= val & CPSR_AIF & mask;
8760
f8c88bbc
PM
8761 if (write_type != CPSRWriteRaw &&
8762 ((env->uncached_cpsr ^ val) & mask & CPSR_M)) {
8c4f0eb9
PM
8763 if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_USR) {
8764 /* Note that we can only get here in USR mode if this is a
8765 * gdb stub write; for this case we follow the architectural
8766 * behaviour for guest writes in USR mode of ignoring an attempt
8767 * to switch mode. (Those are caught by translate.c for writes
8768 * triggered by guest instructions.)
8769 */
8770 mask &= ~CPSR_M;
8771 } else if (bad_mode_switch(env, val & CPSR_M, write_type)) {
81907a58
PM
8772 /* Attempt to switch to an invalid mode: this is UNPREDICTABLE in
8773 * v7, and has defined behaviour in v8:
8774 * + leave CPSR.M untouched
8775 * + allow changes to the other CPSR fields
8776 * + set PSTATE.IL
8777 * For user changes via the GDB stub, we don't set PSTATE.IL,
8778 * as this would be unnecessarily harsh for a user error.
37064a8b
PM
8779 */
8780 mask &= ~CPSR_M;
81907a58
PM
8781 if (write_type != CPSRWriteByGDBStub &&
8782 arm_feature(env, ARM_FEATURE_V8)) {
8783 mask |= CPSR_IL;
8784 val |= CPSR_IL;
8785 }
81e37284
PM
8786 qemu_log_mask(LOG_GUEST_ERROR,
8787 "Illegal AArch32 mode switch attempt from %s to %s\n",
8788 aarch32_mode_name(env->uncached_cpsr),
8789 aarch32_mode_name(val));
37064a8b 8790 } else {
81e37284
PM
8791 qemu_log_mask(CPU_LOG_INT, "%s %s to %s PC 0x%" PRIx32 "\n",
8792 write_type == CPSRWriteExceptionReturn ?
8793 "Exception return from AArch32" :
8794 "AArch32 mode switch from",
8795 aarch32_mode_name(env->uncached_cpsr),
8796 aarch32_mode_name(val), env->regs[15]);
37064a8b
PM
8797 switch_mode(env, val & CPSR_M);
8798 }
2f4a40e5
AZ
8799 }
8800 mask &= ~CACHED_CPSR_BITS;
8801 env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask);
8802}
8803
b26eefb6
PB
8804/* Sign/zero extend */
8805uint32_t HELPER(sxtb16)(uint32_t x)
8806{
8807 uint32_t res;
8808 res = (uint16_t)(int8_t)x;
8809 res |= (uint32_t)(int8_t)(x >> 16) << 16;
8810 return res;
8811}
8812
8813uint32_t HELPER(uxtb16)(uint32_t x)
8814{
8815 uint32_t res;
8816 res = (uint16_t)(uint8_t)x;
8817 res |= (uint32_t)(uint8_t)(x >> 16) << 16;
8818 return res;
8819}
8820
3670669c
PB
8821int32_t HELPER(sdiv)(int32_t num, int32_t den)
8822{
8823 if (den == 0)
8824 return 0;
686eeb93
AJ
8825 if (num == INT_MIN && den == -1)
8826 return INT_MIN;
3670669c
PB
8827 return num / den;
8828}
8829
8830uint32_t HELPER(udiv)(uint32_t num, uint32_t den)
8831{
8832 if (den == 0)
8833 return 0;
8834 return num / den;
8835}
8836
8837uint32_t HELPER(rbit)(uint32_t x)
8838{
42fedbca 8839 return revbit32(x);
3670669c
PB
8840}
8841
c47eaf9f 8842#ifdef CONFIG_USER_ONLY
b5ff1b31 8843
affdb64d 8844static void switch_mode(CPUARMState *env, int mode)
b5ff1b31 8845{
2fc0cc0e 8846 ARMCPU *cpu = env_archcpu(env);
a47dddd7
AF
8847
8848 if (mode != ARM_CPU_MODE_USR) {
8849 cpu_abort(CPU(cpu), "Tried to switch out of user mode\n");
8850 }
b5ff1b31
FB
8851}
8852
012a906b
GB
8853uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
8854 uint32_t cur_el, bool secure)
9e729b57
EI
8855{
8856 return 1;
8857}
8858
ce02049d
GB
8859void aarch64_sync_64_to_32(CPUARMState *env)
8860{
8861 g_assert_not_reached();
8862}
8863
b5ff1b31
FB
8864#else
8865
affdb64d 8866static void switch_mode(CPUARMState *env, int mode)
b5ff1b31
FB
8867{
8868 int old_mode;
8869 int i;
8870
8871 old_mode = env->uncached_cpsr & CPSR_M;
8872 if (mode == old_mode)
8873 return;
8874
8875 if (old_mode == ARM_CPU_MODE_FIQ) {
8876 memcpy (env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t));
8637c67f 8877 memcpy (env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t));
b5ff1b31
FB
8878 } else if (mode == ARM_CPU_MODE_FIQ) {
8879 memcpy (env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t));
8637c67f 8880 memcpy (env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t));
b5ff1b31
FB
8881 }
8882
f5206413 8883 i = bank_number(old_mode);
b5ff1b31 8884 env->banked_r13[i] = env->regs[13];
b5ff1b31
FB
8885 env->banked_spsr[i] = env->spsr;
8886
f5206413 8887 i = bank_number(mode);
b5ff1b31 8888 env->regs[13] = env->banked_r13[i];
b5ff1b31 8889 env->spsr = env->banked_spsr[i];
593cfa2b
PM
8890
8891 env->banked_r14[r14_bank_number(old_mode)] = env->regs[14];
8892 env->regs[14] = env->banked_r14[r14_bank_number(mode)];
b5ff1b31
FB
8893}
8894
0eeb17d6
GB
8895/* Physical Interrupt Target EL Lookup Table
8896 *
8897 * [ From ARM ARM section G1.13.4 (Table G1-15) ]
8898 *
8899 * The below multi-dimensional table is used for looking up the target
8900 * exception level given numerous condition criteria. Specifically, the
8901 * target EL is based on SCR and HCR routing controls as well as the
8902 * currently executing EL and secure state.
8903 *
8904 * Dimensions:
8905 * target_el_table[2][2][2][2][2][4]
8906 * | | | | | +--- Current EL
8907 * | | | | +------ Non-secure(0)/Secure(1)
8908 * | | | +--------- HCR mask override
8909 * | | +------------ SCR exec state control
8910 * | +--------------- SCR mask override
8911 * +------------------ 32-bit(0)/64-bit(1) EL3
8912 *
8913 * The table values are as such:
8914 * 0-3 = EL0-EL3
8915 * -1 = Cannot occur
8916 *
8917 * The ARM ARM target EL table includes entries indicating that an "exception
8918 * is not taken". The two cases where this is applicable are:
8919 * 1) An exception is taken from EL3 but the SCR does not have the exception
8920 * routed to EL3.
8921 * 2) An exception is taken from EL2 but the HCR does not have the exception
8922 * routed to EL2.
8923 * In these two cases, the below table contain a target of EL1. This value is
8924 * returned as it is expected that the consumer of the table data will check
8925 * for "target EL >= current EL" to ensure the exception is not taken.
8926 *
8927 * SCR HCR
8928 * 64 EA AMO From
8929 * BIT IRQ IMO Non-secure Secure
8930 * EL3 FIQ RW FMO EL0 EL1 EL2 EL3 EL0 EL1 EL2 EL3
8931 */
82c39f6a 8932static const int8_t target_el_table[2][2][2][2][2][4] = {
0eeb17d6
GB
8933 {{{{/* 0 0 0 0 */{ 1, 1, 2, -1 },{ 3, -1, -1, 3 },},
8934 {/* 0 0 0 1 */{ 2, 2, 2, -1 },{ 3, -1, -1, 3 },},},
8935 {{/* 0 0 1 0 */{ 1, 1, 2, -1 },{ 3, -1, -1, 3 },},
8936 {/* 0 0 1 1 */{ 2, 2, 2, -1 },{ 3, -1, -1, 3 },},},},
8937 {{{/* 0 1 0 0 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},
8938 {/* 0 1 0 1 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},},
8939 {{/* 0 1 1 0 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},
8940 {/* 0 1 1 1 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},},},},
8941 {{{{/* 1 0 0 0 */{ 1, 1, 2, -1 },{ 1, 1, -1, 1 },},
8942 {/* 1 0 0 1 */{ 2, 2, 2, -1 },{ 1, 1, -1, 1 },},},
8943 {{/* 1 0 1 0 */{ 1, 1, 1, -1 },{ 1, 1, -1, 1 },},
8944 {/* 1 0 1 1 */{ 2, 2, 2, -1 },{ 1, 1, -1, 1 },},},},
8945 {{{/* 1 1 0 0 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},
8946 {/* 1 1 0 1 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},},
8947 {{/* 1 1 1 0 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},
8948 {/* 1 1 1 1 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},},},},
8949};
8950
8951/*
8952 * Determine the target EL for physical exceptions
8953 */
012a906b
GB
8954uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
8955 uint32_t cur_el, bool secure)
0eeb17d6
GB
8956{
8957 CPUARMState *env = cs->env_ptr;
f7778444
RH
8958 bool rw;
8959 bool scr;
8960 bool hcr;
0eeb17d6 8961 int target_el;
2cde031f 8962 /* Is the highest EL AArch64? */
f7778444
RH
8963 bool is64 = arm_feature(env, ARM_FEATURE_AARCH64);
8964 uint64_t hcr_el2;
2cde031f
SS
8965
8966 if (arm_feature(env, ARM_FEATURE_EL3)) {
8967 rw = ((env->cp15.scr_el3 & SCR_RW) == SCR_RW);
8968 } else {
8969 /* Either EL2 is the highest EL (and so the EL2 register width
8970 * is given by is64); or there is no EL2 or EL3, in which case
8971 * the value of 'rw' does not affect the table lookup anyway.
8972 */
8973 rw = is64;
8974 }
0eeb17d6 8975
f7778444 8976 hcr_el2 = arm_hcr_el2_eff(env);
0eeb17d6
GB
8977 switch (excp_idx) {
8978 case EXCP_IRQ:
8979 scr = ((env->cp15.scr_el3 & SCR_IRQ) == SCR_IRQ);
f7778444 8980 hcr = hcr_el2 & HCR_IMO;
0eeb17d6
GB
8981 break;
8982 case EXCP_FIQ:
8983 scr = ((env->cp15.scr_el3 & SCR_FIQ) == SCR_FIQ);
f7778444 8984 hcr = hcr_el2 & HCR_FMO;
0eeb17d6
GB
8985 break;
8986 default:
8987 scr = ((env->cp15.scr_el3 & SCR_EA) == SCR_EA);
f7778444 8988 hcr = hcr_el2 & HCR_AMO;
0eeb17d6
GB
8989 break;
8990 };
8991
d1b31428
RH
8992 /*
8993 * For these purposes, TGE and AMO/IMO/FMO both force the
8994 * interrupt to EL2. Fold TGE into the bit extracted above.
8995 */
8996 hcr |= (hcr_el2 & HCR_TGE) != 0;
8997
0eeb17d6
GB
8998 /* Perform a table-lookup for the target EL given the current state */
8999 target_el = target_el_table[is64][scr][rw][hcr][secure][cur_el];
9000
9001 assert(target_el > 0);
9002
9003 return target_el;
9004}
9005
b59f479b
PMD
9006void arm_log_exception(int idx)
9007{
9008 if (qemu_loglevel_mask(CPU_LOG_INT)) {
9009 const char *exc = NULL;
9010 static const char * const excnames[] = {
9011 [EXCP_UDEF] = "Undefined Instruction",
9012 [EXCP_SWI] = "SVC",
9013 [EXCP_PREFETCH_ABORT] = "Prefetch Abort",
9014 [EXCP_DATA_ABORT] = "Data Abort",
9015 [EXCP_IRQ] = "IRQ",
9016 [EXCP_FIQ] = "FIQ",
9017 [EXCP_BKPT] = "Breakpoint",
9018 [EXCP_EXCEPTION_EXIT] = "QEMU v7M exception exit",
9019 [EXCP_KERNEL_TRAP] = "QEMU intercept of kernel commpage",
9020 [EXCP_HVC] = "Hypervisor Call",
9021 [EXCP_HYP_TRAP] = "Hypervisor Trap",
9022 [EXCP_SMC] = "Secure Monitor Call",
9023 [EXCP_VIRQ] = "Virtual IRQ",
9024 [EXCP_VFIQ] = "Virtual FIQ",
9025 [EXCP_SEMIHOST] = "Semihosting call",
9026 [EXCP_NOCP] = "v7M NOCP UsageFault",
9027 [EXCP_INVSTATE] = "v7M INVSTATE UsageFault",
9028 [EXCP_STKOF] = "v8M STKOF UsageFault",
9029 [EXCP_LAZYFP] = "v7M exception during lazy FP stacking",
9030 [EXCP_LSERR] = "v8M LSERR UsageFault",
9031 [EXCP_UNALIGNED] = "v7M UNALIGNED UsageFault",
9032 };
9033
9034 if (idx >= 0 && idx < ARRAY_SIZE(excnames)) {
9035 exc = excnames[idx];
9036 }
9037 if (!exc) {
9038 exc = "unknown";
9039 }
9040 qemu_log_mask(CPU_LOG_INT, "Taking exception %d [%s]\n", idx, exc);
9041 }
9042}
9043
a356dacf 9044/*
7aab5a8c
PMD
9045 * Function used to synchronize QEMU's AArch64 register set with AArch32
9046 * register set. This is necessary when switching between AArch32 and AArch64
9047 * execution state.
a356dacf 9048 */
7aab5a8c 9049void aarch64_sync_32_to_64(CPUARMState *env)
9ee6e8bb 9050{
7aab5a8c
PMD
9051 int i;
9052 uint32_t mode = env->uncached_cpsr & CPSR_M;
9053
9054 /* We can blanket copy R[0:7] to X[0:7] */
9055 for (i = 0; i < 8; i++) {
9056 env->xregs[i] = env->regs[i];
fd592d89 9057 }
70d74660 9058
9a223097 9059 /*
7aab5a8c
PMD
9060 * Unless we are in FIQ mode, x8-x12 come from the user registers r8-r12.
9061 * Otherwise, they come from the banked user regs.
fd592d89 9062 */
7aab5a8c
PMD
9063 if (mode == ARM_CPU_MODE_FIQ) {
9064 for (i = 8; i < 13; i++) {
9065 env->xregs[i] = env->usr_regs[i - 8];
9066 }
9067 } else {
9068 for (i = 8; i < 13; i++) {
9069 env->xregs[i] = env->regs[i];
9070 }
fd592d89 9071 }
9ee6e8bb 9072
7aab5a8c
PMD
9073 /*
9074 * Registers x13-x23 are the various mode SP and FP registers. Registers
9075 * r13 and r14 are only copied if we are in that mode, otherwise we copy
9076 * from the mode banked register.
9077 */
9078 if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
9079 env->xregs[13] = env->regs[13];
9080 env->xregs[14] = env->regs[14];
9081 } else {
9082 env->xregs[13] = env->banked_r13[bank_number(ARM_CPU_MODE_USR)];
9083 /* HYP is an exception in that it is copied from r14 */
9084 if (mode == ARM_CPU_MODE_HYP) {
9085 env->xregs[14] = env->regs[14];
95695eff 9086 } else {
7aab5a8c 9087 env->xregs[14] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)];
95695eff 9088 }
95695eff
PM
9089 }
9090
7aab5a8c
PMD
9091 if (mode == ARM_CPU_MODE_HYP) {
9092 env->xregs[15] = env->regs[13];
9093 } else {
9094 env->xregs[15] = env->banked_r13[bank_number(ARM_CPU_MODE_HYP)];
95695eff
PM
9095 }
9096
7aab5a8c
PMD
9097 if (mode == ARM_CPU_MODE_IRQ) {
9098 env->xregs[16] = env->regs[14];
9099 env->xregs[17] = env->regs[13];
9100 } else {
9101 env->xregs[16] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)];
9102 env->xregs[17] = env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)];
9103 }
95695eff 9104
7aab5a8c
PMD
9105 if (mode == ARM_CPU_MODE_SVC) {
9106 env->xregs[18] = env->regs[14];
9107 env->xregs[19] = env->regs[13];
9108 } else {
9109 env->xregs[18] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)];
9110 env->xregs[19] = env->banked_r13[bank_number(ARM_CPU_MODE_SVC)];
9111 }
95695eff 9112
7aab5a8c
PMD
9113 if (mode == ARM_CPU_MODE_ABT) {
9114 env->xregs[20] = env->regs[14];
9115 env->xregs[21] = env->regs[13];
9116 } else {
9117 env->xregs[20] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)];
9118 env->xregs[21] = env->banked_r13[bank_number(ARM_CPU_MODE_ABT)];
9119 }
e33cf0f8 9120
7aab5a8c
PMD
9121 if (mode == ARM_CPU_MODE_UND) {
9122 env->xregs[22] = env->regs[14];
9123 env->xregs[23] = env->regs[13];
9124 } else {
9125 env->xregs[22] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)];
9126 env->xregs[23] = env->banked_r13[bank_number(ARM_CPU_MODE_UND)];
e33cf0f8
PM
9127 }
9128
9129 /*
7aab5a8c
PMD
9130 * Registers x24-x30 are mapped to r8-r14 in FIQ mode. If we are in FIQ
9131 * mode, then we can copy from r8-r14. Otherwise, we copy from the
9132 * FIQ bank for r8-r14.
e33cf0f8 9133 */
7aab5a8c
PMD
9134 if (mode == ARM_CPU_MODE_FIQ) {
9135 for (i = 24; i < 31; i++) {
9136 env->xregs[i] = env->regs[i - 16]; /* X[24:30] <- R[8:14] */
9137 }
9138 } else {
9139 for (i = 24; i < 29; i++) {
9140 env->xregs[i] = env->fiq_regs[i - 24];
e33cf0f8 9141 }
7aab5a8c
PMD
9142 env->xregs[29] = env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)];
9143 env->xregs[30] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)];
e33cf0f8 9144 }
7aab5a8c
PMD
9145
9146 env->pc = env->regs[15];
e33cf0f8
PM
9147}
9148
9a223097 9149/*
7aab5a8c
PMD
9150 * Function used to synchronize QEMU's AArch32 register set with AArch64
9151 * register set. This is necessary when switching between AArch32 and AArch64
9152 * execution state.
de2db7ec 9153 */
7aab5a8c 9154void aarch64_sync_64_to_32(CPUARMState *env)
9ee6e8bb 9155{
7aab5a8c
PMD
9156 int i;
9157 uint32_t mode = env->uncached_cpsr & CPSR_M;
abc24d86 9158
7aab5a8c
PMD
9159 /* We can blanket copy X[0:7] to R[0:7] */
9160 for (i = 0; i < 8; i++) {
9161 env->regs[i] = env->xregs[i];
de2db7ec 9162 }
3f0cddee 9163
9a223097 9164 /*
7aab5a8c
PMD
9165 * Unless we are in FIQ mode, r8-r12 come from the user registers x8-x12.
9166 * Otherwise, we copy x8-x12 into the banked user regs.
de2db7ec 9167 */
7aab5a8c
PMD
9168 if (mode == ARM_CPU_MODE_FIQ) {
9169 for (i = 8; i < 13; i++) {
9170 env->usr_regs[i - 8] = env->xregs[i];
9171 }
9172 } else {
9173 for (i = 8; i < 13; i++) {
9174 env->regs[i] = env->xregs[i];
9175 }
fb602cb7
PM
9176 }
9177
9a223097 9178 /*
7aab5a8c
PMD
9179 * Registers r13 & r14 depend on the current mode.
9180 * If we are in a given mode, we copy the corresponding x registers to r13
9181 * and r14. Otherwise, we copy the x register to the banked r13 and r14
9182 * for the mode.
fb602cb7 9183 */
7aab5a8c
PMD
9184 if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
9185 env->regs[13] = env->xregs[13];
9186 env->regs[14] = env->xregs[14];
fb602cb7 9187 } else {
7aab5a8c 9188 env->banked_r13[bank_number(ARM_CPU_MODE_USR)] = env->xregs[13];
fb602cb7 9189
7aab5a8c
PMD
9190 /*
9191 * HYP is an exception in that it does not have its own banked r14 but
9192 * shares the USR r14
9193 */
9194 if (mode == ARM_CPU_MODE_HYP) {
9195 env->regs[14] = env->xregs[14];
9196 } else {
9197 env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)] = env->xregs[14];
9198 }
9199 }
fb602cb7 9200
7aab5a8c
PMD
9201 if (mode == ARM_CPU_MODE_HYP) {
9202 env->regs[13] = env->xregs[15];
fb602cb7 9203 } else {
7aab5a8c 9204 env->banked_r13[bank_number(ARM_CPU_MODE_HYP)] = env->xregs[15];
fb602cb7 9205 }
d02a8698 9206
7aab5a8c
PMD
9207 if (mode == ARM_CPU_MODE_IRQ) {
9208 env->regs[14] = env->xregs[16];
9209 env->regs[13] = env->xregs[17];
d02a8698 9210 } else {
7aab5a8c
PMD
9211 env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[16];
9212 env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[17];
d02a8698
PM
9213 }
9214
7aab5a8c
PMD
9215 if (mode == ARM_CPU_MODE_SVC) {
9216 env->regs[14] = env->xregs[18];
9217 env->regs[13] = env->xregs[19];
9218 } else {
9219 env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)] = env->xregs[18];
9220 env->banked_r13[bank_number(ARM_CPU_MODE_SVC)] = env->xregs[19];
fb602cb7
PM
9221 }
9222
7aab5a8c
PMD
9223 if (mode == ARM_CPU_MODE_ABT) {
9224 env->regs[14] = env->xregs[20];
9225 env->regs[13] = env->xregs[21];
9226 } else {
9227 env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)] = env->xregs[20];
9228 env->banked_r13[bank_number(ARM_CPU_MODE_ABT)] = env->xregs[21];
ce02049d
GB
9229 }
9230
9231 if (mode == ARM_CPU_MODE_UND) {
3a9148d0
SS
9232 env->regs[14] = env->xregs[22];
9233 env->regs[13] = env->xregs[23];
ce02049d 9234 } else {
593cfa2b 9235 env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)] = env->xregs[22];
3a9148d0 9236 env->banked_r13[bank_number(ARM_CPU_MODE_UND)] = env->xregs[23];
ce02049d
GB
9237 }
9238
9239 /* Registers x24-x30 are mapped to r8-r14 in FIQ mode. If we are in FIQ
9240 * mode, then we can copy to r8-r14. Otherwise, we copy to the
9241 * FIQ bank for r8-r14.
9242 */
9243 if (mode == ARM_CPU_MODE_FIQ) {
9244 for (i = 24; i < 31; i++) {
9245 env->regs[i - 16] = env->xregs[i]; /* X[24:30] -> R[8:14] */
9246 }
9247 } else {
9248 for (i = 24; i < 29; i++) {
9249 env->fiq_regs[i - 24] = env->xregs[i];
9250 }
9251 env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[29];
593cfa2b 9252 env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[30];
ce02049d
GB
9253 }
9254
9255 env->regs[15] = env->pc;
9256}
9257
dea8378b
PM
9258static void take_aarch32_exception(CPUARMState *env, int new_mode,
9259 uint32_t mask, uint32_t offset,
9260 uint32_t newpc)
9261{
4a2696c0
RH
9262 int new_el;
9263
dea8378b
PM
9264 /* Change the CPU state so as to actually take the exception. */
9265 switch_mode(env, new_mode);
4a2696c0 9266
dea8378b
PM
9267 /*
9268 * For exceptions taken to AArch32 we must clear the SS bit in both
9269 * PSTATE and in the old-state value we save to SPSR_<mode>, so zero it now.
9270 */
9271 env->uncached_cpsr &= ~PSTATE_SS;
9272 env->spsr = cpsr_read(env);
9273 /* Clear IT bits. */
9274 env->condexec_bits = 0;
9275 /* Switch to the new mode, and to the correct instruction set. */
9276 env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode;
88828bf1
CD
9277
9278 /* This must be after mode switching. */
9279 new_el = arm_current_el(env);
9280
dea8378b
PM
9281 /* Set new mode endianness */
9282 env->uncached_cpsr &= ~CPSR_E;
4a2696c0 9283 if (env->cp15.sctlr_el[new_el] & SCTLR_EE) {
dea8378b
PM
9284 env->uncached_cpsr |= CPSR_E;
9285 }
829f9fd3
PM
9286 /* J and IL must always be cleared for exception entry */
9287 env->uncached_cpsr &= ~(CPSR_IL | CPSR_J);
dea8378b
PM
9288 env->daif |= mask;
9289
9290 if (new_mode == ARM_CPU_MODE_HYP) {
9291 env->thumb = (env->cp15.sctlr_el[2] & SCTLR_TE) != 0;
9292 env->elr_el[2] = env->regs[15];
9293 } else {
4a2696c0 9294 /* CPSR.PAN is normally preserved preserved unless... */
f8af1143 9295 if (cpu_isar_feature(aa32_pan, env_archcpu(env))) {
4a2696c0
RH
9296 switch (new_el) {
9297 case 3:
9298 if (!arm_is_secure_below_el3(env)) {
9299 /* ... the target is EL3, from non-secure state. */
9300 env->uncached_cpsr &= ~CPSR_PAN;
9301 break;
9302 }
9303 /* ... the target is EL3, from secure state ... */
9304 /* fall through */
9305 case 1:
9306 /* ... the target is EL1 and SCTLR.SPAN is 0. */
9307 if (!(env->cp15.sctlr_el[new_el] & SCTLR_SPAN)) {
9308 env->uncached_cpsr |= CPSR_PAN;
9309 }
9310 break;
9311 }
9312 }
dea8378b
PM
9313 /*
9314 * this is a lie, as there was no c1_sys on V4T/V5, but who cares
9315 * and we should just guard the thumb mode on V4
9316 */
9317 if (arm_feature(env, ARM_FEATURE_V4T)) {
9318 env->thumb =
9319 (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_TE) != 0;
9320 }
9321 env->regs[14] = env->regs[15] + offset;
9322 }
9323 env->regs[15] = newpc;
a8a79c7a 9324 arm_rebuild_hflags(env);
dea8378b
PM
9325}
9326
b9bc21ff
PM
9327static void arm_cpu_do_interrupt_aarch32_hyp(CPUState *cs)
9328{
9329 /*
9330 * Handle exception entry to Hyp mode; this is sufficiently
9331 * different to entry to other AArch32 modes that we handle it
9332 * separately here.
9333 *
9334 * The vector table entry used is always the 0x14 Hyp mode entry point,
9335 * unless this is an UNDEF/HVC/abort taken from Hyp to Hyp.
9336 * The offset applied to the preferred return address is always zero
9337 * (see DDI0487C.a section G1.12.3).
9338 * PSTATE A/I/F masks are set based only on the SCR.EA/IRQ/FIQ values.
9339 */
9340 uint32_t addr, mask;
9341 ARMCPU *cpu = ARM_CPU(cs);
9342 CPUARMState *env = &cpu->env;
9343
9344 switch (cs->exception_index) {
9345 case EXCP_UDEF:
9346 addr = 0x04;
9347 break;
9348 case EXCP_SWI:
9349 addr = 0x14;
9350 break;
9351 case EXCP_BKPT:
9352 /* Fall through to prefetch abort. */
9353 case EXCP_PREFETCH_ABORT:
9354 env->cp15.ifar_s = env->exception.vaddress;
9355 qemu_log_mask(CPU_LOG_INT, "...with HIFAR 0x%x\n",
9356 (uint32_t)env->exception.vaddress);
9357 addr = 0x0c;
9358 break;
9359 case EXCP_DATA_ABORT:
9360 env->cp15.dfar_s = env->exception.vaddress;
9361 qemu_log_mask(CPU_LOG_INT, "...with HDFAR 0x%x\n",
9362 (uint32_t)env->exception.vaddress);
9363 addr = 0x10;
9364 break;
9365 case EXCP_IRQ:
9366 addr = 0x18;
9367 break;
9368 case EXCP_FIQ:
9369 addr = 0x1c;
9370 break;
9371 case EXCP_HVC:
9372 addr = 0x08;
9373 break;
9374 case EXCP_HYP_TRAP:
9375 addr = 0x14;
9bbb4ef9 9376 break;
b9bc21ff
PM
9377 default:
9378 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
9379 }
9380
9381 if (cs->exception_index != EXCP_IRQ && cs->exception_index != EXCP_FIQ) {
2ed08180
PM
9382 if (!arm_feature(env, ARM_FEATURE_V8)) {
9383 /*
9384 * QEMU syndrome values are v8-style. v7 has the IL bit
9385 * UNK/SBZP for "field not valid" cases, where v8 uses RES1.
9386 * If this is a v7 CPU, squash the IL bit in those cases.
9387 */
9388 if (cs->exception_index == EXCP_PREFETCH_ABORT ||
9389 (cs->exception_index == EXCP_DATA_ABORT &&
9390 !(env->exception.syndrome & ARM_EL_ISV)) ||
9391 syn_get_ec(env->exception.syndrome) == EC_UNCATEGORIZED) {
9392 env->exception.syndrome &= ~ARM_EL_IL;
9393 }
9394 }
b9bc21ff
PM
9395 env->cp15.esr_el[2] = env->exception.syndrome;
9396 }
9397
9398 if (arm_current_el(env) != 2 && addr < 0x14) {
9399 addr = 0x14;
9400 }
9401
9402 mask = 0;
9403 if (!(env->cp15.scr_el3 & SCR_EA)) {
9404 mask |= CPSR_A;
9405 }
9406 if (!(env->cp15.scr_el3 & SCR_IRQ)) {
9407 mask |= CPSR_I;
9408 }
9409 if (!(env->cp15.scr_el3 & SCR_FIQ)) {
9410 mask |= CPSR_F;
9411 }
9412
9413 addr += env->cp15.hvbar;
9414
9415 take_aarch32_exception(env, ARM_CPU_MODE_HYP, mask, 0, addr);
9416}
9417
966f758c 9418static void arm_cpu_do_interrupt_aarch32(CPUState *cs)
b5ff1b31 9419{
97a8ea5a
AF
9420 ARMCPU *cpu = ARM_CPU(cs);
9421 CPUARMState *env = &cpu->env;
b5ff1b31
FB
9422 uint32_t addr;
9423 uint32_t mask;
9424 int new_mode;
9425 uint32_t offset;
16a906fd 9426 uint32_t moe;
b5ff1b31 9427
16a906fd 9428 /* If this is a debug exception we must update the DBGDSCR.MOE bits */
64b91e3f 9429 switch (syn_get_ec(env->exception.syndrome)) {
16a906fd
PM
9430 case EC_BREAKPOINT:
9431 case EC_BREAKPOINT_SAME_EL:
9432 moe = 1;
9433 break;
9434 case EC_WATCHPOINT:
9435 case EC_WATCHPOINT_SAME_EL:
9436 moe = 10;
9437 break;
9438 case EC_AA32_BKPT:
9439 moe = 3;
9440 break;
9441 case EC_VECTORCATCH:
9442 moe = 5;
9443 break;
9444 default:
9445 moe = 0;
9446 break;
9447 }
9448
9449 if (moe) {
9450 env->cp15.mdscr_el1 = deposit64(env->cp15.mdscr_el1, 2, 4, moe);
9451 }
9452
b9bc21ff
PM
9453 if (env->exception.target_el == 2) {
9454 arm_cpu_do_interrupt_aarch32_hyp(cs);
9455 return;
9456 }
9457
27103424 9458 switch (cs->exception_index) {
b5ff1b31
FB
9459 case EXCP_UDEF:
9460 new_mode = ARM_CPU_MODE_UND;
9461 addr = 0x04;
9462 mask = CPSR_I;
9463 if (env->thumb)
9464 offset = 2;
9465 else
9466 offset = 4;
9467 break;
9468 case EXCP_SWI:
9469 new_mode = ARM_CPU_MODE_SVC;
9470 addr = 0x08;
9471 mask = CPSR_I;
601d70b9 9472 /* The PC already points to the next instruction. */
b5ff1b31
FB
9473 offset = 0;
9474 break;
06c949e6 9475 case EXCP_BKPT:
9ee6e8bb
PB
9476 /* Fall through to prefetch abort. */
9477 case EXCP_PREFETCH_ABORT:
88ca1c2d 9478 A32_BANKED_CURRENT_REG_SET(env, ifsr, env->exception.fsr);
b848ce2b 9479 A32_BANKED_CURRENT_REG_SET(env, ifar, env->exception.vaddress);
3f1beaca 9480 qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x IFAR 0x%x\n",
88ca1c2d 9481 env->exception.fsr, (uint32_t)env->exception.vaddress);
b5ff1b31
FB
9482 new_mode = ARM_CPU_MODE_ABT;
9483 addr = 0x0c;
9484 mask = CPSR_A | CPSR_I;
9485 offset = 4;
9486 break;
9487 case EXCP_DATA_ABORT:
4a7e2d73 9488 A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr);
b848ce2b 9489 A32_BANKED_CURRENT_REG_SET(env, dfar, env->exception.vaddress);
3f1beaca 9490 qemu_log_mask(CPU_LOG_INT, "...with DFSR 0x%x DFAR 0x%x\n",
4a7e2d73 9491 env->exception.fsr,
6cd8a264 9492 (uint32_t)env->exception.vaddress);
b5ff1b31
FB
9493 new_mode = ARM_CPU_MODE_ABT;
9494 addr = 0x10;
9495 mask = CPSR_A | CPSR_I;
9496 offset = 8;
9497 break;
9498 case EXCP_IRQ:
9499 new_mode = ARM_CPU_MODE_IRQ;
9500 addr = 0x18;
9501 /* Disable IRQ and imprecise data aborts. */
9502 mask = CPSR_A | CPSR_I;
9503 offset = 4;
de38d23b
FA
9504 if (env->cp15.scr_el3 & SCR_IRQ) {
9505 /* IRQ routed to monitor mode */
9506 new_mode = ARM_CPU_MODE_MON;
9507 mask |= CPSR_F;
9508 }
b5ff1b31
FB
9509 break;
9510 case EXCP_FIQ:
9511 new_mode = ARM_CPU_MODE_FIQ;
9512 addr = 0x1c;
9513 /* Disable FIQ, IRQ and imprecise data aborts. */
9514 mask = CPSR_A | CPSR_I | CPSR_F;
de38d23b
FA
9515 if (env->cp15.scr_el3 & SCR_FIQ) {
9516 /* FIQ routed to monitor mode */
9517 new_mode = ARM_CPU_MODE_MON;
9518 }
b5ff1b31
FB
9519 offset = 4;
9520 break;
87a4b270
PM
9521 case EXCP_VIRQ:
9522 new_mode = ARM_CPU_MODE_IRQ;
9523 addr = 0x18;
9524 /* Disable IRQ and imprecise data aborts. */
9525 mask = CPSR_A | CPSR_I;
9526 offset = 4;
9527 break;
9528 case EXCP_VFIQ:
9529 new_mode = ARM_CPU_MODE_FIQ;
9530 addr = 0x1c;
9531 /* Disable FIQ, IRQ and imprecise data aborts. */
9532 mask = CPSR_A | CPSR_I | CPSR_F;
9533 offset = 4;
9534 break;
dbe9d163
FA
9535 case EXCP_SMC:
9536 new_mode = ARM_CPU_MODE_MON;
9537 addr = 0x08;
9538 mask = CPSR_A | CPSR_I | CPSR_F;
9539 offset = 0;
9540 break;
b5ff1b31 9541 default:
a47dddd7 9542 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
b5ff1b31
FB
9543 return; /* Never happens. Keep compiler happy. */
9544 }
e89e51a1
FA
9545
9546 if (new_mode == ARM_CPU_MODE_MON) {
9547 addr += env->cp15.mvbar;
137feaa9 9548 } else if (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_V) {
e89e51a1 9549 /* High vectors. When enabled, base address cannot be remapped. */
b5ff1b31 9550 addr += 0xffff0000;
8641136c
NR
9551 } else {
9552 /* ARM v7 architectures provide a vector base address register to remap
9553 * the interrupt vector table.
e89e51a1 9554 * This register is only followed in non-monitor mode, and is banked.
8641136c
NR
9555 * Note: only bits 31:5 are valid.
9556 */
fb6c91ba 9557 addr += A32_BANKED_CURRENT_REG_GET(env, vbar);
b5ff1b31 9558 }
dbe9d163
FA
9559
9560 if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON) {
9561 env->cp15.scr_el3 &= ~SCR_NS;
9562 }
9563
dea8378b 9564 take_aarch32_exception(env, new_mode, mask, offset, addr);
b5ff1b31
FB
9565}
9566
966f758c
PM
9567/* Handle exception entry to a target EL which is using AArch64 */
9568static void arm_cpu_do_interrupt_aarch64(CPUState *cs)
f3a9b694
PM
9569{
9570 ARMCPU *cpu = ARM_CPU(cs);
9571 CPUARMState *env = &cpu->env;
9572 unsigned int new_el = env->exception.target_el;
9573 target_ulong addr = env->cp15.vbar_el[new_el];
9574 unsigned int new_mode = aarch64_pstate_mode(new_el, true);
4a2696c0 9575 unsigned int old_mode;
0ab5953b
RH
9576 unsigned int cur_el = arm_current_el(env);
9577
9a05f7b6
RH
9578 /*
9579 * Note that new_el can never be 0. If cur_el is 0, then
9580 * el0_a64 is is_a64(), else el0_a64 is ignored.
9581 */
9582 aarch64_sve_change_el(env, cur_el, new_el, is_a64(env));
f3a9b694 9583
0ab5953b 9584 if (cur_el < new_el) {
3d6f7617
PM
9585 /* Entry vector offset depends on whether the implemented EL
9586 * immediately lower than the target level is using AArch32 or AArch64
9587 */
9588 bool is_aa64;
cb092fbb 9589 uint64_t hcr;
3d6f7617
PM
9590
9591 switch (new_el) {
9592 case 3:
9593 is_aa64 = (env->cp15.scr_el3 & SCR_RW) != 0;
9594 break;
9595 case 2:
cb092fbb
RH
9596 hcr = arm_hcr_el2_eff(env);
9597 if ((hcr & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
9598 is_aa64 = (hcr & HCR_RW) != 0;
9599 break;
9600 }
9601 /* fall through */
3d6f7617
PM
9602 case 1:
9603 is_aa64 = is_a64(env);
9604 break;
9605 default:
9606 g_assert_not_reached();
9607 }
9608
9609 if (is_aa64) {
f3a9b694
PM
9610 addr += 0x400;
9611 } else {
9612 addr += 0x600;
9613 }
9614 } else if (pstate_read(env) & PSTATE_SP) {
9615 addr += 0x200;
9616 }
9617
f3a9b694
PM
9618 switch (cs->exception_index) {
9619 case EXCP_PREFETCH_ABORT:
9620 case EXCP_DATA_ABORT:
9621 env->cp15.far_el[new_el] = env->exception.vaddress;
9622 qemu_log_mask(CPU_LOG_INT, "...with FAR 0x%" PRIx64 "\n",
9623 env->cp15.far_el[new_el]);
9624 /* fall through */
9625 case EXCP_BKPT:
9626 case EXCP_UDEF:
9627 case EXCP_SWI:
9628 case EXCP_HVC:
9629 case EXCP_HYP_TRAP:
9630 case EXCP_SMC:
4be42f40
PM
9631 if (syn_get_ec(env->exception.syndrome) == EC_ADVSIMDFPACCESSTRAP) {
9632 /*
9633 * QEMU internal FP/SIMD syndromes from AArch32 include the
9634 * TA and coproc fields which are only exposed if the exception
9635 * is taken to AArch32 Hyp mode. Mask them out to get a valid
9636 * AArch64 format syndrome.
9637 */
9638 env->exception.syndrome &= ~MAKE_64BIT_MASK(0, 20);
9639 }
f3a9b694
PM
9640 env->cp15.esr_el[new_el] = env->exception.syndrome;
9641 break;
9642 case EXCP_IRQ:
9643 case EXCP_VIRQ:
9644 addr += 0x80;
9645 break;
9646 case EXCP_FIQ:
9647 case EXCP_VFIQ:
9648 addr += 0x100;
9649 break;
f3a9b694
PM
9650 default:
9651 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
9652 }
9653
9654 if (is_a64(env)) {
4a2696c0 9655 old_mode = pstate_read(env);
f3a9b694
PM
9656 aarch64_save_sp(env, arm_current_el(env));
9657 env->elr_el[new_el] = env->pc;
9658 } else {
4a2696c0 9659 old_mode = cpsr_read(env);
f3a9b694
PM
9660 env->elr_el[new_el] = env->regs[15];
9661
9662 aarch64_sync_32_to_64(env);
9663
9664 env->condexec_bits = 0;
9665 }
4a2696c0
RH
9666 env->banked_spsr[aarch64_banked_spsr_index(new_el)] = old_mode;
9667
f3a9b694
PM
9668 qemu_log_mask(CPU_LOG_INT, "...with ELR 0x%" PRIx64 "\n",
9669 env->elr_el[new_el]);
9670
4a2696c0
RH
9671 if (cpu_isar_feature(aa64_pan, cpu)) {
9672 /* The value of PSTATE.PAN is normally preserved, except when ... */
9673 new_mode |= old_mode & PSTATE_PAN;
9674 switch (new_el) {
9675 case 2:
9676 /* ... the target is EL2 with HCR_EL2.{E2H,TGE} == '11' ... */
9677 if ((arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE))
9678 != (HCR_E2H | HCR_TGE)) {
9679 break;
9680 }
9681 /* fall through */
9682 case 1:
9683 /* ... the target is EL1 ... */
9684 /* ... and SCTLR_ELx.SPAN == 0, then set to 1. */
9685 if ((env->cp15.sctlr_el[new_el] & SCTLR_SPAN) == 0) {
9686 new_mode |= PSTATE_PAN;
9687 }
9688 break;
9689 }
9690 }
9691
f3a9b694
PM
9692 pstate_write(env, PSTATE_DAIF | new_mode);
9693 env->aarch64 = 1;
9694 aarch64_restore_sp(env, new_el);
a8a79c7a 9695 helper_rebuild_hflags_a64(env, new_el);
f3a9b694
PM
9696
9697 env->pc = addr;
9698
9699 qemu_log_mask(CPU_LOG_INT, "...to EL%d PC 0x%" PRIx64 " PSTATE 0x%x\n",
9700 new_el, env->pc, pstate_read(env));
966f758c
PM
9701}
9702
ed6e6ba9
AB
9703/*
9704 * Do semihosting call and set the appropriate return value. All the
9705 * permission and validity checks have been done at translate time.
9706 *
9707 * We only see semihosting exceptions in TCG only as they are not
9708 * trapped to the hypervisor in KVM.
9709 */
91f78c58 9710#ifdef CONFIG_TCG
ed6e6ba9
AB
9711static void handle_semihosting(CPUState *cs)
9712{
904c04de
PM
9713 ARMCPU *cpu = ARM_CPU(cs);
9714 CPUARMState *env = &cpu->env;
9715
9716 if (is_a64(env)) {
ed6e6ba9
AB
9717 qemu_log_mask(CPU_LOG_INT,
9718 "...handling as semihosting call 0x%" PRIx64 "\n",
9719 env->xregs[0]);
9720 env->xregs[0] = do_arm_semihosting(env);
4ff5ef9e 9721 env->pc += 4;
904c04de 9722 } else {
904c04de
PM
9723 qemu_log_mask(CPU_LOG_INT,
9724 "...handling as semihosting call 0x%x\n",
9725 env->regs[0]);
9726 env->regs[0] = do_arm_semihosting(env);
4ff5ef9e 9727 env->regs[15] += env->thumb ? 2 : 4;
904c04de
PM
9728 }
9729}
ed6e6ba9 9730#endif
904c04de 9731
966f758c
PM
9732/* Handle a CPU exception for A and R profile CPUs.
9733 * Do any appropriate logging, handle PSCI calls, and then hand off
9734 * to the AArch64-entry or AArch32-entry function depending on the
9735 * target exception level's register width.
9736 */
9737void arm_cpu_do_interrupt(CPUState *cs)
9738{
9739 ARMCPU *cpu = ARM_CPU(cs);
9740 CPUARMState *env = &cpu->env;
9741 unsigned int new_el = env->exception.target_el;
9742
531c60a9 9743 assert(!arm_feature(env, ARM_FEATURE_M));
966f758c
PM
9744
9745 arm_log_exception(cs->exception_index);
9746 qemu_log_mask(CPU_LOG_INT, "...from EL%d to EL%d\n", arm_current_el(env),
9747 new_el);
9748 if (qemu_loglevel_mask(CPU_LOG_INT)
9749 && !excp_is_internal(cs->exception_index)) {
6568da45 9750 qemu_log_mask(CPU_LOG_INT, "...with ESR 0x%x/0x%" PRIx32 "\n",
64b91e3f 9751 syn_get_ec(env->exception.syndrome),
966f758c
PM
9752 env->exception.syndrome);
9753 }
9754
9755 if (arm_is_psci_call(cpu, cs->exception_index)) {
9756 arm_handle_psci_call(cpu);
9757 qemu_log_mask(CPU_LOG_INT, "...handled as PSCI call\n");
9758 return;
9759 }
9760
ed6e6ba9
AB
9761 /*
9762 * Semihosting semantics depend on the register width of the code
9763 * that caused the exception, not the target exception level, so
9764 * must be handled here.
966f758c 9765 */
ed6e6ba9
AB
9766#ifdef CONFIG_TCG
9767 if (cs->exception_index == EXCP_SEMIHOST) {
9768 handle_semihosting(cs);
904c04de
PM
9769 return;
9770 }
ed6e6ba9 9771#endif
904c04de 9772
b5c53d1b
AL
9773 /* Hooks may change global state so BQL should be held, also the
9774 * BQL needs to be held for any modification of
9775 * cs->interrupt_request.
9776 */
9777 g_assert(qemu_mutex_iothread_locked());
9778
9779 arm_call_pre_el_change_hook(cpu);
9780
904c04de
PM
9781 assert(!excp_is_internal(cs->exception_index));
9782 if (arm_el_is_aa64(env, new_el)) {
966f758c
PM
9783 arm_cpu_do_interrupt_aarch64(cs);
9784 } else {
9785 arm_cpu_do_interrupt_aarch32(cs);
9786 }
f3a9b694 9787
bd7d00fc
PM
9788 arm_call_el_change_hook(cpu);
9789
f3a9b694
PM
9790 if (!kvm_enabled()) {
9791 cs->interrupt_request |= CPU_INTERRUPT_EXITTB;
9792 }
9793}
c47eaf9f 9794#endif /* !CONFIG_USER_ONLY */
0480f69a 9795
aaec1432
RH
9796uint64_t arm_sctlr(CPUARMState *env, int el)
9797{
9798 /* Only EL0 needs to be adjusted for EL1&0 or EL2&0. */
9799 if (el == 0) {
9800 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, 0);
9801 el = (mmu_idx == ARMMMUIdx_E20_0 ? 2 : 1);
9802 }
9803 return env->cp15.sctlr_el[el];
9804}
c47eaf9f 9805
0480f69a 9806/* Return the SCTLR value which controls this address translation regime */
aaec1432 9807static inline uint64_t regime_sctlr(CPUARMState *env, ARMMMUIdx mmu_idx)
0480f69a
PM
9808{
9809 return env->cp15.sctlr_el[regime_el(env, mmu_idx)];
9810}
9811
aaec1432
RH
9812#ifndef CONFIG_USER_ONLY
9813
0480f69a
PM
9814/* Return true if the specified stage of address translation is disabled */
9815static inline bool regime_translation_disabled(CPUARMState *env,
9816 ARMMMUIdx mmu_idx)
9817{
29c483a5 9818 if (arm_feature(env, ARM_FEATURE_M)) {
ecf5e8ea 9819 switch (env->v7m.mpu_ctrl[regime_is_secure(env, mmu_idx)] &
3bef7012
PM
9820 (R_V7M_MPU_CTRL_ENABLE_MASK | R_V7M_MPU_CTRL_HFNMIENA_MASK)) {
9821 case R_V7M_MPU_CTRL_ENABLE_MASK:
9822 /* Enabled, but not for HardFault and NMI */
62593718 9823 return mmu_idx & ARM_MMU_IDX_M_NEGPRI;
3bef7012
PM
9824 case R_V7M_MPU_CTRL_ENABLE_MASK | R_V7M_MPU_CTRL_HFNMIENA_MASK:
9825 /* Enabled for all cases */
9826 return false;
9827 case 0:
9828 default:
9829 /* HFNMIENA set and ENABLE clear is UNPREDICTABLE, but
9830 * we warned about that in armv7m_nvic.c when the guest set it.
9831 */
9832 return true;
9833 }
29c483a5
MD
9834 }
9835
97fa9350 9836 if (mmu_idx == ARMMMUIdx_Stage2) {
9d1bab33
PM
9837 /* HCR.DC means HCR.VM behaves as 1 */
9838 return (env->cp15.hcr_el2 & (HCR_DC | HCR_VM)) == 0;
0480f69a 9839 }
3d0e3080
PM
9840
9841 if (env->cp15.hcr_el2 & HCR_TGE) {
9842 /* TGE means that NS EL0/1 act as if SCTLR_EL1.M is zero */
9843 if (!regime_is_secure(env, mmu_idx) && regime_el(env, mmu_idx) == 1) {
9844 return true;
9845 }
9846 }
9847
fee7aa46 9848 if ((env->cp15.hcr_el2 & HCR_DC) && arm_mmu_idx_is_stage1_of_2(mmu_idx)) {
9d1bab33
PM
9849 /* HCR.DC means SCTLR_EL1.M behaves as 0 */
9850 return true;
9851 }
9852
0480f69a
PM
9853 return (regime_sctlr(env, mmu_idx) & SCTLR_M) == 0;
9854}
9855
73462ddd
PC
9856static inline bool regime_translation_big_endian(CPUARMState *env,
9857 ARMMMUIdx mmu_idx)
9858{
9859 return (regime_sctlr(env, mmu_idx) & SCTLR_EE) != 0;
9860}
9861
c47eaf9f
PM
9862/* Return the TTBR associated with this translation regime */
9863static inline uint64_t regime_ttbr(CPUARMState *env, ARMMMUIdx mmu_idx,
9864 int ttbrn)
9865{
97fa9350 9866 if (mmu_idx == ARMMMUIdx_Stage2) {
c47eaf9f
PM
9867 return env->cp15.vttbr_el2;
9868 }
9869 if (ttbrn == 0) {
9870 return env->cp15.ttbr0_el[regime_el(env, mmu_idx)];
9871 } else {
9872 return env->cp15.ttbr1_el[regime_el(env, mmu_idx)];
9873 }
9874}
9875
9876#endif /* !CONFIG_USER_ONLY */
9877
8bd5c820
PM
9878/* Convert a possible stage1+2 MMU index into the appropriate
9879 * stage 1 MMU index
9880 */
9881static inline ARMMMUIdx stage_1_mmu_idx(ARMMMUIdx mmu_idx)
9882{
b9f6033c
RH
9883 switch (mmu_idx) {
9884 case ARMMMUIdx_E10_0:
9885 return ARMMMUIdx_Stage1_E0;
9886 case ARMMMUIdx_E10_1:
9887 return ARMMMUIdx_Stage1_E1;
452ef8cb
RH
9888 case ARMMMUIdx_E10_1_PAN:
9889 return ARMMMUIdx_Stage1_E1_PAN;
b9f6033c
RH
9890 default:
9891 return mmu_idx;
8bd5c820 9892 }
8bd5c820
PM
9893}
9894
0480f69a
PM
9895/* Return true if the translation regime is using LPAE format page tables */
9896static inline bool regime_using_lpae_format(CPUARMState *env,
9897 ARMMMUIdx mmu_idx)
9898{
9899 int el = regime_el(env, mmu_idx);
9900 if (el == 2 || arm_el_is_aa64(env, el)) {
9901 return true;
9902 }
9903 if (arm_feature(env, ARM_FEATURE_LPAE)
9904 && (regime_tcr(env, mmu_idx)->raw_tcr & TTBCR_EAE)) {
9905 return true;
9906 }
9907 return false;
9908}
9909
deb2db99
AR
9910/* Returns true if the stage 1 translation regime is using LPAE format page
9911 * tables. Used when raising alignment exceptions, whose FSR changes depending
9912 * on whether the long or short descriptor format is in use. */
9913bool arm_s1_regime_using_lpae_format(CPUARMState *env, ARMMMUIdx mmu_idx)
30901475 9914{
8bd5c820 9915 mmu_idx = stage_1_mmu_idx(mmu_idx);
deb2db99 9916
30901475
AB
9917 return regime_using_lpae_format(env, mmu_idx);
9918}
9919
c47eaf9f 9920#ifndef CONFIG_USER_ONLY
0480f69a
PM
9921static inline bool regime_is_user(CPUARMState *env, ARMMMUIdx mmu_idx)
9922{
9923 switch (mmu_idx) {
fba37aed 9924 case ARMMMUIdx_SE10_0:
b9f6033c 9925 case ARMMMUIdx_E20_0:
2859d7b5 9926 case ARMMMUIdx_Stage1_E0:
e7b921c2 9927 case ARMMMUIdx_MUser:
871bec7c 9928 case ARMMMUIdx_MSUser:
62593718
PM
9929 case ARMMMUIdx_MUserNegPri:
9930 case ARMMMUIdx_MSUserNegPri:
0480f69a
PM
9931 return true;
9932 default:
9933 return false;
01b98b68
RH
9934 case ARMMMUIdx_E10_0:
9935 case ARMMMUIdx_E10_1:
452ef8cb 9936 case ARMMMUIdx_E10_1_PAN:
0480f69a
PM
9937 g_assert_not_reached();
9938 }
9939}
9940
0fbf5238
AJ
9941/* Translate section/page access permissions to page
9942 * R/W protection flags
d76951b6
AJ
9943 *
9944 * @env: CPUARMState
9945 * @mmu_idx: MMU index indicating required translation regime
9946 * @ap: The 3-bit access permissions (AP[2:0])
9947 * @domain_prot: The 2-bit domain access permissions
0fbf5238
AJ
9948 */
9949static inline int ap_to_rw_prot(CPUARMState *env, ARMMMUIdx mmu_idx,
9950 int ap, int domain_prot)
9951{
554b0b09
PM
9952 bool is_user = regime_is_user(env, mmu_idx);
9953
9954 if (domain_prot == 3) {
9955 return PAGE_READ | PAGE_WRITE;
9956 }
9957
554b0b09
PM
9958 switch (ap) {
9959 case 0:
9960 if (arm_feature(env, ARM_FEATURE_V7)) {
9961 return 0;
9962 }
554b0b09
PM
9963 switch (regime_sctlr(env, mmu_idx) & (SCTLR_S | SCTLR_R)) {
9964 case SCTLR_S:
9965 return is_user ? 0 : PAGE_READ;
9966 case SCTLR_R:
9967 return PAGE_READ;
9968 default:
9969 return 0;
9970 }
9971 case 1:
9972 return is_user ? 0 : PAGE_READ | PAGE_WRITE;
9973 case 2:
87c3d486 9974 if (is_user) {
0fbf5238 9975 return PAGE_READ;
87c3d486 9976 } else {
554b0b09 9977 return PAGE_READ | PAGE_WRITE;
87c3d486 9978 }
554b0b09
PM
9979 case 3:
9980 return PAGE_READ | PAGE_WRITE;
9981 case 4: /* Reserved. */
9982 return 0;
9983 case 5:
0fbf5238 9984 return is_user ? 0 : PAGE_READ;
554b0b09 9985 case 6:
0fbf5238 9986 return PAGE_READ;
554b0b09 9987 case 7:
87c3d486 9988 if (!arm_feature(env, ARM_FEATURE_V6K)) {
554b0b09 9989 return 0;
87c3d486 9990 }
0fbf5238 9991 return PAGE_READ;
554b0b09 9992 default:
0fbf5238 9993 g_assert_not_reached();
554b0b09 9994 }
b5ff1b31
FB
9995}
9996
d76951b6
AJ
9997/* Translate section/page access permissions to page
9998 * R/W protection flags.
9999 *
d76951b6 10000 * @ap: The 2-bit simple AP (AP[2:1])
d8e052b3 10001 * @is_user: TRUE if accessing from PL0
d76951b6 10002 */
d8e052b3 10003static inline int simple_ap_to_rw_prot_is_user(int ap, bool is_user)
d76951b6 10004{
d76951b6
AJ
10005 switch (ap) {
10006 case 0:
10007 return is_user ? 0 : PAGE_READ | PAGE_WRITE;
10008 case 1:
10009 return PAGE_READ | PAGE_WRITE;
10010 case 2:
10011 return is_user ? 0 : PAGE_READ;
10012 case 3:
10013 return PAGE_READ;
10014 default:
10015 g_assert_not_reached();
10016 }
10017}
10018
d8e052b3
AJ
10019static inline int
10020simple_ap_to_rw_prot(CPUARMState *env, ARMMMUIdx mmu_idx, int ap)
10021{
10022 return simple_ap_to_rw_prot_is_user(ap, regime_is_user(env, mmu_idx));
10023}
10024
6ab1a5ee
EI
10025/* Translate S2 section/page access permissions to protection flags
10026 *
10027 * @env: CPUARMState
10028 * @s2ap: The 2-bit stage2 access permissions (S2AP)
ce3125be
PM
10029 * @xn: XN (execute-never) bits
10030 * @s1_is_el0: true if this is S2 of an S1+2 walk for EL0
6ab1a5ee 10031 */
ce3125be 10032static int get_S2prot(CPUARMState *env, int s2ap, int xn, bool s1_is_el0)
6ab1a5ee
EI
10033{
10034 int prot = 0;
10035
10036 if (s2ap & 1) {
10037 prot |= PAGE_READ;
10038 }
10039 if (s2ap & 2) {
10040 prot |= PAGE_WRITE;
10041 }
ce3125be
PM
10042
10043 if (cpu_isar_feature(any_tts2uxn, env_archcpu(env))) {
10044 switch (xn) {
10045 case 0:
dfda6837 10046 prot |= PAGE_EXEC;
ce3125be
PM
10047 break;
10048 case 1:
10049 if (s1_is_el0) {
10050 prot |= PAGE_EXEC;
10051 }
10052 break;
10053 case 2:
10054 break;
10055 case 3:
10056 if (!s1_is_el0) {
10057 prot |= PAGE_EXEC;
10058 }
10059 break;
10060 default:
10061 g_assert_not_reached();
10062 }
10063 } else {
10064 if (!extract32(xn, 1, 1)) {
10065 if (arm_el_is_aa64(env, 2) || prot & PAGE_READ) {
10066 prot |= PAGE_EXEC;
10067 }
dfda6837 10068 }
6ab1a5ee
EI
10069 }
10070 return prot;
10071}
10072
d8e052b3
AJ
10073/* Translate section/page access permissions to protection flags
10074 *
10075 * @env: CPUARMState
10076 * @mmu_idx: MMU index indicating required translation regime
10077 * @is_aa64: TRUE if AArch64
10078 * @ap: The 2-bit simple AP (AP[2:1])
10079 * @ns: NS (non-secure) bit
10080 * @xn: XN (execute-never) bit
10081 * @pxn: PXN (privileged execute-never) bit
10082 */
10083static int get_S1prot(CPUARMState *env, ARMMMUIdx mmu_idx, bool is_aa64,
10084 int ap, int ns, int xn, int pxn)
10085{
10086 bool is_user = regime_is_user(env, mmu_idx);
10087 int prot_rw, user_rw;
10088 bool have_wxn;
10089 int wxn = 0;
10090
97fa9350 10091 assert(mmu_idx != ARMMMUIdx_Stage2);
d8e052b3
AJ
10092
10093 user_rw = simple_ap_to_rw_prot_is_user(ap, true);
10094 if (is_user) {
10095 prot_rw = user_rw;
10096 } else {
81636b70 10097 if (user_rw && regime_is_pan(env, mmu_idx)) {
f4e1dbc5
PM
10098 /* PAN forbids data accesses but doesn't affect insn fetch */
10099 prot_rw = 0;
10100 } else {
10101 prot_rw = simple_ap_to_rw_prot_is_user(ap, false);
81636b70 10102 }
d8e052b3
AJ
10103 }
10104
10105 if (ns && arm_is_secure(env) && (env->cp15.scr_el3 & SCR_SIF)) {
10106 return prot_rw;
10107 }
10108
10109 /* TODO have_wxn should be replaced with
10110 * ARM_FEATURE_V8 || (ARM_FEATURE_V7 && ARM_FEATURE_EL2)
10111 * when ARM_FEATURE_EL2 starts getting set. For now we assume all LPAE
10112 * compatible processors have EL2, which is required for [U]WXN.
10113 */
10114 have_wxn = arm_feature(env, ARM_FEATURE_LPAE);
10115
10116 if (have_wxn) {
10117 wxn = regime_sctlr(env, mmu_idx) & SCTLR_WXN;
10118 }
10119
10120 if (is_aa64) {
339370b9
RH
10121 if (regime_has_2_ranges(mmu_idx) && !is_user) {
10122 xn = pxn || (user_rw & PAGE_WRITE);
d8e052b3
AJ
10123 }
10124 } else if (arm_feature(env, ARM_FEATURE_V7)) {
10125 switch (regime_el(env, mmu_idx)) {
10126 case 1:
10127 case 3:
10128 if (is_user) {
10129 xn = xn || !(user_rw & PAGE_READ);
10130 } else {
10131 int uwxn = 0;
10132 if (have_wxn) {
10133 uwxn = regime_sctlr(env, mmu_idx) & SCTLR_UWXN;
10134 }
10135 xn = xn || !(prot_rw & PAGE_READ) || pxn ||
10136 (uwxn && (user_rw & PAGE_WRITE));
10137 }
10138 break;
10139 case 2:
10140 break;
10141 }
10142 } else {
10143 xn = wxn = 0;
10144 }
10145
10146 if (xn || (wxn && (prot_rw & PAGE_WRITE))) {
10147 return prot_rw;
10148 }
10149 return prot_rw | PAGE_EXEC;
10150}
10151
0480f69a
PM
10152static bool get_level1_table_address(CPUARMState *env, ARMMMUIdx mmu_idx,
10153 uint32_t *table, uint32_t address)
b2fa1797 10154{
0480f69a 10155 /* Note that we can only get here for an AArch32 PL0/PL1 lookup */
0480f69a 10156 TCR *tcr = regime_tcr(env, mmu_idx);
11f136ee 10157
11f136ee
FA
10158 if (address & tcr->mask) {
10159 if (tcr->raw_tcr & TTBCR_PD1) {
e389be16
FA
10160 /* Translation table walk disabled for TTBR1 */
10161 return false;
10162 }
aef878be 10163 *table = regime_ttbr(env, mmu_idx, 1) & 0xffffc000;
e389be16 10164 } else {
11f136ee 10165 if (tcr->raw_tcr & TTBCR_PD0) {
e389be16
FA
10166 /* Translation table walk disabled for TTBR0 */
10167 return false;
10168 }
aef878be 10169 *table = regime_ttbr(env, mmu_idx, 0) & tcr->base_mask;
e389be16
FA
10170 }
10171 *table |= (address >> 18) & 0x3ffc;
10172 return true;
b2fa1797
PB
10173}
10174
37785977
EI
10175/* Translate a S1 pagetable walk through S2 if needed. */
10176static hwaddr S1_ptw_translate(CPUARMState *env, ARMMMUIdx mmu_idx,
10177 hwaddr addr, MemTxAttrs txattrs,
37785977
EI
10178 ARMMMUFaultInfo *fi)
10179{
fee7aa46 10180 if (arm_mmu_idx_is_stage1_of_2(mmu_idx) &&
97fa9350 10181 !regime_translation_disabled(env, ARMMMUIdx_Stage2)) {
37785977
EI
10182 target_ulong s2size;
10183 hwaddr s2pa;
10184 int s2prot;
10185 int ret;
eadb2feb
PM
10186 ARMCacheAttrs cacheattrs = {};
10187 ARMCacheAttrs *pcacheattrs = NULL;
10188
10189 if (env->cp15.hcr_el2 & HCR_PTW) {
10190 /*
10191 * PTW means we must fault if this S1 walk touches S2 Device
10192 * memory; otherwise we don't care about the attributes and can
10193 * save the S2 translation the effort of computing them.
10194 */
10195 pcacheattrs = &cacheattrs;
10196 }
37785977 10197
59dff859 10198 ret = get_phys_addr_lpae(env, addr, MMU_DATA_LOAD, ARMMMUIdx_Stage2,
ff7de2fc 10199 false,
59dff859
PM
10200 &s2pa, &txattrs, &s2prot, &s2size, fi,
10201 pcacheattrs);
37785977 10202 if (ret) {
3b39d734 10203 assert(fi->type != ARMFault_None);
37785977
EI
10204 fi->s2addr = addr;
10205 fi->stage2 = true;
10206 fi->s1ptw = true;
10207 return ~0;
10208 }
eadb2feb
PM
10209 if (pcacheattrs && (pcacheattrs->attrs & 0xf0) == 0) {
10210 /* Access was to Device memory: generate Permission fault */
10211 fi->type = ARMFault_Permission;
10212 fi->s2addr = addr;
10213 fi->stage2 = true;
10214 fi->s1ptw = true;
10215 return ~0;
10216 }
37785977
EI
10217 addr = s2pa;
10218 }
10219 return addr;
10220}
10221
14577270 10222/* All loads done in the course of a page table walk go through here. */
a614e698 10223static uint32_t arm_ldl_ptw(CPUState *cs, hwaddr addr, bool is_secure,
3795a6de 10224 ARMMMUIdx mmu_idx, ARMMMUFaultInfo *fi)
ebca90e4 10225{
a614e698
EI
10226 ARMCPU *cpu = ARM_CPU(cs);
10227 CPUARMState *env = &cpu->env;
ebca90e4 10228 MemTxAttrs attrs = {};
3b39d734 10229 MemTxResult result = MEMTX_OK;
5ce4ff65 10230 AddressSpace *as;
3b39d734 10231 uint32_t data;
ebca90e4
PM
10232
10233 attrs.secure = is_secure;
5ce4ff65 10234 as = arm_addressspace(cs, attrs);
3795a6de 10235 addr = S1_ptw_translate(env, mmu_idx, addr, attrs, fi);
a614e698
EI
10236 if (fi->s1ptw) {
10237 return 0;
10238 }
73462ddd 10239 if (regime_translation_big_endian(env, mmu_idx)) {
3b39d734 10240 data = address_space_ldl_be(as, addr, attrs, &result);
73462ddd 10241 } else {
3b39d734 10242 data = address_space_ldl_le(as, addr, attrs, &result);
73462ddd 10243 }
3b39d734
PM
10244 if (result == MEMTX_OK) {
10245 return data;
10246 }
10247 fi->type = ARMFault_SyncExternalOnWalk;
10248 fi->ea = arm_extabort_type(result);
10249 return 0;
ebca90e4
PM
10250}
10251
37785977 10252static uint64_t arm_ldq_ptw(CPUState *cs, hwaddr addr, bool is_secure,
3795a6de 10253 ARMMMUIdx mmu_idx, ARMMMUFaultInfo *fi)
ebca90e4 10254{
37785977
EI
10255 ARMCPU *cpu = ARM_CPU(cs);
10256 CPUARMState *env = &cpu->env;
ebca90e4 10257 MemTxAttrs attrs = {};
3b39d734 10258 MemTxResult result = MEMTX_OK;
5ce4ff65 10259 AddressSpace *as;
9aea1ea3 10260 uint64_t data;
ebca90e4
PM
10261
10262 attrs.secure = is_secure;
5ce4ff65 10263 as = arm_addressspace(cs, attrs);
3795a6de 10264 addr = S1_ptw_translate(env, mmu_idx, addr, attrs, fi);
37785977
EI
10265 if (fi->s1ptw) {
10266 return 0;
10267 }
73462ddd 10268 if (regime_translation_big_endian(env, mmu_idx)) {
3b39d734 10269 data = address_space_ldq_be(as, addr, attrs, &result);
73462ddd 10270 } else {
3b39d734
PM
10271 data = address_space_ldq_le(as, addr, attrs, &result);
10272 }
10273 if (result == MEMTX_OK) {
10274 return data;
73462ddd 10275 }
3b39d734
PM
10276 fi->type = ARMFault_SyncExternalOnWalk;
10277 fi->ea = arm_extabort_type(result);
10278 return 0;
ebca90e4
PM
10279}
10280
b7cc4e82 10281static bool get_phys_addr_v5(CPUARMState *env, uint32_t address,
03ae85f8 10282 MMUAccessType access_type, ARMMMUIdx mmu_idx,
b7cc4e82 10283 hwaddr *phys_ptr, int *prot,
f989983e 10284 target_ulong *page_size,
e14b5a23 10285 ARMMMUFaultInfo *fi)
b5ff1b31 10286{
2fc0cc0e 10287 CPUState *cs = env_cpu(env);
f989983e 10288 int level = 1;
b5ff1b31
FB
10289 uint32_t table;
10290 uint32_t desc;
10291 int type;
10292 int ap;
e389be16 10293 int domain = 0;
dd4ebc2e 10294 int domain_prot;
a8170e5e 10295 hwaddr phys_addr;
0480f69a 10296 uint32_t dacr;
b5ff1b31 10297
9ee6e8bb
PB
10298 /* Pagetable walk. */
10299 /* Lookup l1 descriptor. */
0480f69a 10300 if (!get_level1_table_address(env, mmu_idx, &table, address)) {
e389be16 10301 /* Section translation fault if page walk is disabled by PD0 or PD1 */
f989983e 10302 fi->type = ARMFault_Translation;
e389be16
FA
10303 goto do_fault;
10304 }
a614e698 10305 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
3795a6de 10306 mmu_idx, fi);
3b39d734
PM
10307 if (fi->type != ARMFault_None) {
10308 goto do_fault;
10309 }
9ee6e8bb 10310 type = (desc & 3);
dd4ebc2e 10311 domain = (desc >> 5) & 0x0f;
0480f69a
PM
10312 if (regime_el(env, mmu_idx) == 1) {
10313 dacr = env->cp15.dacr_ns;
10314 } else {
10315 dacr = env->cp15.dacr_s;
10316 }
10317 domain_prot = (dacr >> (domain * 2)) & 3;
9ee6e8bb 10318 if (type == 0) {
601d70b9 10319 /* Section translation fault. */
f989983e 10320 fi->type = ARMFault_Translation;
9ee6e8bb
PB
10321 goto do_fault;
10322 }
f989983e
PM
10323 if (type != 2) {
10324 level = 2;
10325 }
dd4ebc2e 10326 if (domain_prot == 0 || domain_prot == 2) {
f989983e 10327 fi->type = ARMFault_Domain;
9ee6e8bb
PB
10328 goto do_fault;
10329 }
10330 if (type == 2) {
10331 /* 1Mb section. */
10332 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
10333 ap = (desc >> 10) & 3;
d4c430a8 10334 *page_size = 1024 * 1024;
9ee6e8bb
PB
10335 } else {
10336 /* Lookup l2 entry. */
554b0b09
PM
10337 if (type == 1) {
10338 /* Coarse pagetable. */
10339 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
10340 } else {
10341 /* Fine pagetable. */
10342 table = (desc & 0xfffff000) | ((address >> 8) & 0xffc);
10343 }
a614e698 10344 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
3795a6de 10345 mmu_idx, fi);
3b39d734
PM
10346 if (fi->type != ARMFault_None) {
10347 goto do_fault;
10348 }
9ee6e8bb
PB
10349 switch (desc & 3) {
10350 case 0: /* Page translation fault. */
f989983e 10351 fi->type = ARMFault_Translation;
9ee6e8bb
PB
10352 goto do_fault;
10353 case 1: /* 64k page. */
10354 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
10355 ap = (desc >> (4 + ((address >> 13) & 6))) & 3;
d4c430a8 10356 *page_size = 0x10000;
ce819861 10357 break;
9ee6e8bb
PB
10358 case 2: /* 4k page. */
10359 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
c10f7fc3 10360 ap = (desc >> (4 + ((address >> 9) & 6))) & 3;
d4c430a8 10361 *page_size = 0x1000;
ce819861 10362 break;
fc1891c7 10363 case 3: /* 1k page, or ARMv6/XScale "extended small (4k) page" */
554b0b09 10364 if (type == 1) {
fc1891c7
PM
10365 /* ARMv6/XScale extended small page format */
10366 if (arm_feature(env, ARM_FEATURE_XSCALE)
10367 || arm_feature(env, ARM_FEATURE_V6)) {
554b0b09 10368 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
fc1891c7 10369 *page_size = 0x1000;
554b0b09 10370 } else {
fc1891c7
PM
10371 /* UNPREDICTABLE in ARMv5; we choose to take a
10372 * page translation fault.
10373 */
f989983e 10374 fi->type = ARMFault_Translation;
554b0b09
PM
10375 goto do_fault;
10376 }
10377 } else {
10378 phys_addr = (desc & 0xfffffc00) | (address & 0x3ff);
fc1891c7 10379 *page_size = 0x400;
554b0b09 10380 }
9ee6e8bb 10381 ap = (desc >> 4) & 3;
ce819861
PB
10382 break;
10383 default:
9ee6e8bb
PB
10384 /* Never happens, but compiler isn't smart enough to tell. */
10385 abort();
ce819861 10386 }
9ee6e8bb 10387 }
0fbf5238
AJ
10388 *prot = ap_to_rw_prot(env, mmu_idx, ap, domain_prot);
10389 *prot |= *prot ? PAGE_EXEC : 0;
10390 if (!(*prot & (1 << access_type))) {
9ee6e8bb 10391 /* Access permission fault. */
f989983e 10392 fi->type = ARMFault_Permission;
9ee6e8bb
PB
10393 goto do_fault;
10394 }
10395 *phys_ptr = phys_addr;
b7cc4e82 10396 return false;
9ee6e8bb 10397do_fault:
f989983e
PM
10398 fi->domain = domain;
10399 fi->level = level;
b7cc4e82 10400 return true;
9ee6e8bb
PB
10401}
10402
b7cc4e82 10403static bool get_phys_addr_v6(CPUARMState *env, uint32_t address,
03ae85f8 10404 MMUAccessType access_type, ARMMMUIdx mmu_idx,
b7cc4e82 10405 hwaddr *phys_ptr, MemTxAttrs *attrs, int *prot,
f06cf243 10406 target_ulong *page_size, ARMMMUFaultInfo *fi)
9ee6e8bb 10407{
2fc0cc0e 10408 CPUState *cs = env_cpu(env);
f06cf243 10409 int level = 1;
9ee6e8bb
PB
10410 uint32_t table;
10411 uint32_t desc;
10412 uint32_t xn;
de9b05b8 10413 uint32_t pxn = 0;
9ee6e8bb
PB
10414 int type;
10415 int ap;
de9b05b8 10416 int domain = 0;
dd4ebc2e 10417 int domain_prot;
a8170e5e 10418 hwaddr phys_addr;
0480f69a 10419 uint32_t dacr;
8bf5b6a9 10420 bool ns;
9ee6e8bb
PB
10421
10422 /* Pagetable walk. */
10423 /* Lookup l1 descriptor. */
0480f69a 10424 if (!get_level1_table_address(env, mmu_idx, &table, address)) {
e389be16 10425 /* Section translation fault if page walk is disabled by PD0 or PD1 */
f06cf243 10426 fi->type = ARMFault_Translation;
e389be16
FA
10427 goto do_fault;
10428 }
a614e698 10429 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
3795a6de 10430 mmu_idx, fi);
3b39d734
PM
10431 if (fi->type != ARMFault_None) {
10432 goto do_fault;
10433 }
9ee6e8bb 10434 type = (desc & 3);
de9b05b8
PM
10435 if (type == 0 || (type == 3 && !arm_feature(env, ARM_FEATURE_PXN))) {
10436 /* Section translation fault, or attempt to use the encoding
10437 * which is Reserved on implementations without PXN.
10438 */
f06cf243 10439 fi->type = ARMFault_Translation;
9ee6e8bb 10440 goto do_fault;
de9b05b8
PM
10441 }
10442 if ((type == 1) || !(desc & (1 << 18))) {
10443 /* Page or Section. */
dd4ebc2e 10444 domain = (desc >> 5) & 0x0f;
9ee6e8bb 10445 }
0480f69a
PM
10446 if (regime_el(env, mmu_idx) == 1) {
10447 dacr = env->cp15.dacr_ns;
10448 } else {
10449 dacr = env->cp15.dacr_s;
10450 }
f06cf243
PM
10451 if (type == 1) {
10452 level = 2;
10453 }
0480f69a 10454 domain_prot = (dacr >> (domain * 2)) & 3;
dd4ebc2e 10455 if (domain_prot == 0 || domain_prot == 2) {
f06cf243
PM
10456 /* Section or Page domain fault */
10457 fi->type = ARMFault_Domain;
9ee6e8bb
PB
10458 goto do_fault;
10459 }
de9b05b8 10460 if (type != 1) {
9ee6e8bb
PB
10461 if (desc & (1 << 18)) {
10462 /* Supersection. */
10463 phys_addr = (desc & 0xff000000) | (address & 0x00ffffff);
4e42a6ca
SF
10464 phys_addr |= (uint64_t)extract32(desc, 20, 4) << 32;
10465 phys_addr |= (uint64_t)extract32(desc, 5, 4) << 36;
d4c430a8 10466 *page_size = 0x1000000;
b5ff1b31 10467 } else {
9ee6e8bb
PB
10468 /* Section. */
10469 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
d4c430a8 10470 *page_size = 0x100000;
b5ff1b31 10471 }
9ee6e8bb
PB
10472 ap = ((desc >> 10) & 3) | ((desc >> 13) & 4);
10473 xn = desc & (1 << 4);
de9b05b8 10474 pxn = desc & 1;
8bf5b6a9 10475 ns = extract32(desc, 19, 1);
9ee6e8bb 10476 } else {
de9b05b8
PM
10477 if (arm_feature(env, ARM_FEATURE_PXN)) {
10478 pxn = (desc >> 2) & 1;
10479 }
8bf5b6a9 10480 ns = extract32(desc, 3, 1);
9ee6e8bb
PB
10481 /* Lookup l2 entry. */
10482 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
a614e698 10483 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
3795a6de 10484 mmu_idx, fi);
3b39d734
PM
10485 if (fi->type != ARMFault_None) {
10486 goto do_fault;
10487 }
9ee6e8bb
PB
10488 ap = ((desc >> 4) & 3) | ((desc >> 7) & 4);
10489 switch (desc & 3) {
10490 case 0: /* Page translation fault. */
f06cf243 10491 fi->type = ARMFault_Translation;
b5ff1b31 10492 goto do_fault;
9ee6e8bb
PB
10493 case 1: /* 64k page. */
10494 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
10495 xn = desc & (1 << 15);
d4c430a8 10496 *page_size = 0x10000;
9ee6e8bb
PB
10497 break;
10498 case 2: case 3: /* 4k page. */
10499 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
10500 xn = desc & 1;
d4c430a8 10501 *page_size = 0x1000;
9ee6e8bb
PB
10502 break;
10503 default:
10504 /* Never happens, but compiler isn't smart enough to tell. */
10505 abort();
b5ff1b31 10506 }
9ee6e8bb 10507 }
dd4ebc2e 10508 if (domain_prot == 3) {
c0034328
JR
10509 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
10510 } else {
0480f69a 10511 if (pxn && !regime_is_user(env, mmu_idx)) {
de9b05b8
PM
10512 xn = 1;
10513 }
f06cf243
PM
10514 if (xn && access_type == MMU_INST_FETCH) {
10515 fi->type = ARMFault_Permission;
c0034328 10516 goto do_fault;
f06cf243 10517 }
9ee6e8bb 10518
d76951b6
AJ
10519 if (arm_feature(env, ARM_FEATURE_V6K) &&
10520 (regime_sctlr(env, mmu_idx) & SCTLR_AFE)) {
10521 /* The simplified model uses AP[0] as an access control bit. */
10522 if ((ap & 1) == 0) {
10523 /* Access flag fault. */
f06cf243 10524 fi->type = ARMFault_AccessFlag;
d76951b6
AJ
10525 goto do_fault;
10526 }
10527 *prot = simple_ap_to_rw_prot(env, mmu_idx, ap >> 1);
10528 } else {
10529 *prot = ap_to_rw_prot(env, mmu_idx, ap, domain_prot);
c0034328 10530 }
0fbf5238
AJ
10531 if (*prot && !xn) {
10532 *prot |= PAGE_EXEC;
10533 }
10534 if (!(*prot & (1 << access_type))) {
c0034328 10535 /* Access permission fault. */
f06cf243 10536 fi->type = ARMFault_Permission;
c0034328
JR
10537 goto do_fault;
10538 }
3ad493fc 10539 }
8bf5b6a9
PM
10540 if (ns) {
10541 /* The NS bit will (as required by the architecture) have no effect if
10542 * the CPU doesn't support TZ or this is a non-secure translation
10543 * regime, because the attribute will already be non-secure.
10544 */
10545 attrs->secure = false;
10546 }
9ee6e8bb 10547 *phys_ptr = phys_addr;
b7cc4e82 10548 return false;
b5ff1b31 10549do_fault:
f06cf243
PM
10550 fi->domain = domain;
10551 fi->level = level;
b7cc4e82 10552 return true;
b5ff1b31
FB
10553}
10554
1853d5a9 10555/*
a0e966c9 10556 * check_s2_mmu_setup
1853d5a9
EI
10557 * @cpu: ARMCPU
10558 * @is_aa64: True if the translation regime is in AArch64 state
10559 * @startlevel: Suggested starting level
10560 * @inputsize: Bitsize of IPAs
10561 * @stride: Page-table stride (See the ARM ARM)
10562 *
a0e966c9
EI
10563 * Returns true if the suggested S2 translation parameters are OK and
10564 * false otherwise.
1853d5a9 10565 */
a0e966c9
EI
10566static bool check_s2_mmu_setup(ARMCPU *cpu, bool is_aa64, int level,
10567 int inputsize, int stride)
1853d5a9 10568{
98d68ec2
EI
10569 const int grainsize = stride + 3;
10570 int startsizecheck;
10571
1853d5a9
EI
10572 /* Negative levels are never allowed. */
10573 if (level < 0) {
10574 return false;
10575 }
10576
98d68ec2
EI
10577 startsizecheck = inputsize - ((3 - level) * stride + grainsize);
10578 if (startsizecheck < 1 || startsizecheck > stride + 4) {
10579 return false;
10580 }
10581
1853d5a9 10582 if (is_aa64) {
3526423e 10583 CPUARMState *env = &cpu->env;
1853d5a9
EI
10584 unsigned int pamax = arm_pamax(cpu);
10585
10586 switch (stride) {
10587 case 13: /* 64KB Pages. */
10588 if (level == 0 || (level == 1 && pamax <= 42)) {
10589 return false;
10590 }
10591 break;
10592 case 11: /* 16KB Pages. */
10593 if (level == 0 || (level == 1 && pamax <= 40)) {
10594 return false;
10595 }
10596 break;
10597 case 9: /* 4KB Pages. */
10598 if (level == 0 && pamax <= 42) {
10599 return false;
10600 }
10601 break;
10602 default:
10603 g_assert_not_reached();
10604 }
3526423e
EI
10605
10606 /* Inputsize checks. */
10607 if (inputsize > pamax &&
10608 (arm_el_is_aa64(env, 1) || inputsize > 40)) {
10609 /* This is CONSTRAINED UNPREDICTABLE and we choose to fault. */
10610 return false;
10611 }
1853d5a9 10612 } else {
1853d5a9
EI
10613 /* AArch32 only supports 4KB pages. Assert on that. */
10614 assert(stride == 9);
10615
10616 if (level == 0) {
10617 return false;
10618 }
1853d5a9
EI
10619 }
10620 return true;
10621}
10622
5b2d261d
AB
10623/* Translate from the 4-bit stage 2 representation of
10624 * memory attributes (without cache-allocation hints) to
10625 * the 8-bit representation of the stage 1 MAIR registers
10626 * (which includes allocation hints).
10627 *
10628 * ref: shared/translation/attrs/S2AttrDecode()
10629 * .../S2ConvertAttrsHints()
10630 */
10631static uint8_t convert_stage2_attrs(CPUARMState *env, uint8_t s2attrs)
10632{
10633 uint8_t hiattr = extract32(s2attrs, 2, 2);
10634 uint8_t loattr = extract32(s2attrs, 0, 2);
10635 uint8_t hihint = 0, lohint = 0;
10636
10637 if (hiattr != 0) { /* normal memory */
10638 if ((env->cp15.hcr_el2 & HCR_CD) != 0) { /* cache disabled */
10639 hiattr = loattr = 1; /* non-cacheable */
10640 } else {
10641 if (hiattr != 1) { /* Write-through or write-back */
10642 hihint = 3; /* RW allocate */
10643 }
10644 if (loattr != 1) { /* Write-through or write-back */
10645 lohint = 3; /* RW allocate */
10646 }
10647 }
10648 }
10649
10650 return (hiattr << 6) | (hihint << 4) | (loattr << 2) | lohint;
10651}
c47eaf9f 10652#endif /* !CONFIG_USER_ONLY */
5b2d261d 10653
b830a5ee
RH
10654static int aa64_va_parameter_tbi(uint64_t tcr, ARMMMUIdx mmu_idx)
10655{
10656 if (regime_has_2_ranges(mmu_idx)) {
10657 return extract64(tcr, 37, 2);
10658 } else if (mmu_idx == ARMMMUIdx_Stage2) {
10659 return 0; /* VTCR_EL2 */
10660 } else {
3e270f67
RH
10661 /* Replicate the single TBI bit so we always have 2 bits. */
10662 return extract32(tcr, 20, 1) * 3;
b830a5ee
RH
10663 }
10664}
10665
10666static int aa64_va_parameter_tbid(uint64_t tcr, ARMMMUIdx mmu_idx)
10667{
10668 if (regime_has_2_ranges(mmu_idx)) {
10669 return extract64(tcr, 51, 2);
10670 } else if (mmu_idx == ARMMMUIdx_Stage2) {
10671 return 0; /* VTCR_EL2 */
10672 } else {
3e270f67
RH
10673 /* Replicate the single TBID bit so we always have 2 bits. */
10674 return extract32(tcr, 29, 1) * 3;
b830a5ee
RH
10675 }
10676}
10677
81ae05fa
RH
10678static int aa64_va_parameter_tcma(uint64_t tcr, ARMMMUIdx mmu_idx)
10679{
10680 if (regime_has_2_ranges(mmu_idx)) {
10681 return extract64(tcr, 57, 2);
10682 } else {
10683 /* Replicate the single TCMA bit so we always have 2 bits. */
10684 return extract32(tcr, 30, 1) * 3;
10685 }
10686}
10687
b830a5ee
RH
10688ARMVAParameters aa64_va_parameters(CPUARMState *env, uint64_t va,
10689 ARMMMUIdx mmu_idx, bool data)
ba97be9f
RH
10690{
10691 uint64_t tcr = regime_tcr(env, mmu_idx)->raw_tcr;
b830a5ee
RH
10692 bool epd, hpd, using16k, using64k;
10693 int select, tsz, tbi;
ba97be9f 10694
339370b9 10695 if (!regime_has_2_ranges(mmu_idx)) {
71d18164 10696 select = 0;
ba97be9f
RH
10697 tsz = extract32(tcr, 0, 6);
10698 using64k = extract32(tcr, 14, 1);
10699 using16k = extract32(tcr, 15, 1);
97fa9350 10700 if (mmu_idx == ARMMMUIdx_Stage2) {
ba97be9f 10701 /* VTCR_EL2 */
b830a5ee 10702 hpd = false;
ba97be9f 10703 } else {
ba97be9f
RH
10704 hpd = extract32(tcr, 24, 1);
10705 }
10706 epd = false;
ba97be9f 10707 } else {
71d18164
RH
10708 /*
10709 * Bit 55 is always between the two regions, and is canonical for
10710 * determining if address tagging is enabled.
10711 */
10712 select = extract64(va, 55, 1);
10713 if (!select) {
10714 tsz = extract32(tcr, 0, 6);
10715 epd = extract32(tcr, 7, 1);
10716 using64k = extract32(tcr, 14, 1);
10717 using16k = extract32(tcr, 15, 1);
71d18164 10718 hpd = extract64(tcr, 41, 1);
71d18164
RH
10719 } else {
10720 int tg = extract32(tcr, 30, 2);
10721 using16k = tg == 1;
10722 using64k = tg == 3;
10723 tsz = extract32(tcr, 16, 6);
10724 epd = extract32(tcr, 23, 1);
71d18164 10725 hpd = extract64(tcr, 42, 1);
71d18164 10726 }
ba97be9f
RH
10727 }
10728 tsz = MIN(tsz, 39); /* TODO: ARMv8.4-TTST */
10729 tsz = MAX(tsz, 16); /* TODO: ARMv8.2-LVA */
10730
b830a5ee
RH
10731 /* Present TBI as a composite with TBID. */
10732 tbi = aa64_va_parameter_tbi(tcr, mmu_idx);
10733 if (!data) {
10734 tbi &= ~aa64_va_parameter_tbid(tcr, mmu_idx);
10735 }
10736 tbi = (tbi >> select) & 1;
10737
ba97be9f
RH
10738 return (ARMVAParameters) {
10739 .tsz = tsz,
10740 .select = select,
10741 .tbi = tbi,
10742 .epd = epd,
10743 .hpd = hpd,
10744 .using16k = using16k,
10745 .using64k = using64k,
10746 };
10747}
10748
c47eaf9f 10749#ifndef CONFIG_USER_ONLY
ba97be9f
RH
10750static ARMVAParameters aa32_va_parameters(CPUARMState *env, uint32_t va,
10751 ARMMMUIdx mmu_idx)
10752{
10753 uint64_t tcr = regime_tcr(env, mmu_idx)->raw_tcr;
10754 uint32_t el = regime_el(env, mmu_idx);
10755 int select, tsz;
10756 bool epd, hpd;
10757
97fa9350 10758 if (mmu_idx == ARMMMUIdx_Stage2) {
ba97be9f
RH
10759 /* VTCR */
10760 bool sext = extract32(tcr, 4, 1);
10761 bool sign = extract32(tcr, 3, 1);
10762
10763 /*
10764 * If the sign-extend bit is not the same as t0sz[3], the result
10765 * is unpredictable. Flag this as a guest error.
10766 */
10767 if (sign != sext) {
10768 qemu_log_mask(LOG_GUEST_ERROR,
10769 "AArch32: VTCR.S / VTCR.T0SZ[3] mismatch\n");
10770 }
10771 tsz = sextract32(tcr, 0, 4) + 8;
10772 select = 0;
10773 hpd = false;
10774 epd = false;
10775 } else if (el == 2) {
10776 /* HTCR */
10777 tsz = extract32(tcr, 0, 3);
10778 select = 0;
10779 hpd = extract64(tcr, 24, 1);
10780 epd = false;
10781 } else {
10782 int t0sz = extract32(tcr, 0, 3);
10783 int t1sz = extract32(tcr, 16, 3);
10784
10785 if (t1sz == 0) {
10786 select = va > (0xffffffffu >> t0sz);
10787 } else {
10788 /* Note that we will detect errors later. */
10789 select = va >= ~(0xffffffffu >> t1sz);
10790 }
10791 if (!select) {
10792 tsz = t0sz;
10793 epd = extract32(tcr, 7, 1);
10794 hpd = extract64(tcr, 41, 1);
10795 } else {
10796 tsz = t1sz;
10797 epd = extract32(tcr, 23, 1);
10798 hpd = extract64(tcr, 42, 1);
10799 }
10800 /* For aarch32, hpd0 is not enabled without t2e as well. */
10801 hpd &= extract32(tcr, 6, 1);
10802 }
10803
10804 return (ARMVAParameters) {
10805 .tsz = tsz,
10806 .select = select,
10807 .epd = epd,
10808 .hpd = hpd,
10809 };
10810}
10811
ff7de2fc
PM
10812/**
10813 * get_phys_addr_lpae: perform one stage of page table walk, LPAE format
10814 *
10815 * Returns false if the translation was successful. Otherwise, phys_ptr, attrs,
10816 * prot and page_size may not be filled in, and the populated fsr value provides
10817 * information on why the translation aborted, in the format of a long-format
10818 * DFSR/IFSR fault register, with the following caveats:
10819 * * the WnR bit is never set (the caller must do this).
10820 *
10821 * @env: CPUARMState
10822 * @address: virtual address to get physical address for
10823 * @access_type: MMU_DATA_LOAD, MMU_DATA_STORE or MMU_INST_FETCH
10824 * @mmu_idx: MMU index indicating required translation regime
10825 * @s1_is_el0: if @mmu_idx is ARMMMUIdx_Stage2 (so this is a stage 2 page table
10826 * walk), must be true if this is stage 2 of a stage 1+2 walk for an
10827 * EL0 access). If @mmu_idx is anything else, @s1_is_el0 is ignored.
10828 * @phys_ptr: set to the physical address corresponding to the virtual address
10829 * @attrs: set to the memory transaction attributes to use
10830 * @prot: set to the permissions for the page containing phys_ptr
10831 * @page_size_ptr: set to the size of the page containing phys_ptr
10832 * @fi: set to fault info if the translation fails
10833 * @cacheattrs: (if non-NULL) set to the cacheability/shareability attributes
10834 */
b7cc4e82 10835static bool get_phys_addr_lpae(CPUARMState *env, target_ulong address,
03ae85f8 10836 MMUAccessType access_type, ARMMMUIdx mmu_idx,
ff7de2fc 10837 bool s1_is_el0,
b7cc4e82 10838 hwaddr *phys_ptr, MemTxAttrs *txattrs, int *prot,
da909b2c 10839 target_ulong *page_size_ptr,
5b2d261d 10840 ARMMMUFaultInfo *fi, ARMCacheAttrs *cacheattrs)
3dde962f 10841{
2fc0cc0e 10842 ARMCPU *cpu = env_archcpu(env);
1853d5a9 10843 CPUState *cs = CPU(cpu);
3dde962f 10844 /* Read an LPAE long-descriptor translation table. */
da909b2c 10845 ARMFaultType fault_type = ARMFault_Translation;
1b4093ea 10846 uint32_t level;
ba97be9f 10847 ARMVAParameters param;
3dde962f 10848 uint64_t ttbr;
dddb5223 10849 hwaddr descaddr, indexmask, indexmask_grainsize;
3dde962f 10850 uint32_t tableattrs;
36d820af 10851 target_ulong page_size;
3dde962f 10852 uint32_t attrs;
ba97be9f
RH
10853 int32_t stride;
10854 int addrsize, inputsize;
0480f69a 10855 TCR *tcr = regime_tcr(env, mmu_idx);
d8e052b3 10856 int ap, ns, xn, pxn;
88e8add8 10857 uint32_t el = regime_el(env, mmu_idx);
6109769a 10858 uint64_t descaddrmask;
6e99f762 10859 bool aarch64 = arm_el_is_aa64(env, el);
1bafc2ba 10860 bool guarded = false;
0480f69a 10861
07d1be3b 10862 /* TODO: This code does not support shareability levels. */
6e99f762 10863 if (aarch64) {
ba97be9f
RH
10864 param = aa64_va_parameters(env, address, mmu_idx,
10865 access_type != MMU_INST_FETCH);
1b4093ea 10866 level = 0;
ba97be9f
RH
10867 addrsize = 64 - 8 * param.tbi;
10868 inputsize = 64 - param.tsz;
d0a2cbce 10869 } else {
ba97be9f 10870 param = aa32_va_parameters(env, address, mmu_idx);
1b4093ea 10871 level = 1;
97fa9350 10872 addrsize = (mmu_idx == ARMMMUIdx_Stage2 ? 40 : 32);
ba97be9f 10873 inputsize = addrsize - param.tsz;
2c8dd318 10874 }
3dde962f 10875
ba97be9f
RH
10876 /*
10877 * We determined the region when collecting the parameters, but we
10878 * have not yet validated that the address is valid for the region.
10879 * Extract the top bits and verify that they all match select.
36d820af
RH
10880 *
10881 * For aa32, if inputsize == addrsize, then we have selected the
10882 * region by exclusion in aa32_va_parameters and there is no more
10883 * validation to do here.
10884 */
10885 if (inputsize < addrsize) {
10886 target_ulong top_bits = sextract64(address, inputsize,
10887 addrsize - inputsize);
03f27724 10888 if (-top_bits != param.select) {
36d820af
RH
10889 /* The gap between the two regions is a Translation fault */
10890 fault_type = ARMFault_Translation;
10891 goto do_fault;
10892 }
3dde962f
PM
10893 }
10894
ba97be9f
RH
10895 if (param.using64k) {
10896 stride = 13;
10897 } else if (param.using16k) {
10898 stride = 11;
10899 } else {
10900 stride = 9;
10901 }
10902
3dde962f
PM
10903 /* Note that QEMU ignores shareability and cacheability attributes,
10904 * so we don't need to do anything with the SH, ORGN, IRGN fields
10905 * in the TTBCR. Similarly, TTBCR:A1 selects whether we get the
10906 * ASID from TTBR0 or TTBR1, but QEMU's TLB doesn't currently
10907 * implement any ASID-like capability so we can ignore it (instead
10908 * we will always flush the TLB any time the ASID is changed).
10909 */
ba97be9f 10910 ttbr = regime_ttbr(env, mmu_idx, param.select);
3dde962f 10911
0480f69a 10912 /* Here we should have set up all the parameters for the translation:
6e99f762 10913 * inputsize, ttbr, epd, stride, tbi
0480f69a
PM
10914 */
10915
ba97be9f 10916 if (param.epd) {
88e8add8
GB
10917 /* Translation table walk disabled => Translation fault on TLB miss
10918 * Note: This is always 0 on 64-bit EL2 and EL3.
10919 */
3dde962f
PM
10920 goto do_fault;
10921 }
10922
97fa9350 10923 if (mmu_idx != ARMMMUIdx_Stage2) {
1853d5a9
EI
10924 /* The starting level depends on the virtual address size (which can
10925 * be up to 48 bits) and the translation granule size. It indicates
10926 * the number of strides (stride bits at a time) needed to
10927 * consume the bits of the input address. In the pseudocode this is:
10928 * level = 4 - RoundUp((inputsize - grainsize) / stride)
10929 * where their 'inputsize' is our 'inputsize', 'grainsize' is
10930 * our 'stride + 3' and 'stride' is our 'stride'.
10931 * Applying the usual "rounded up m/n is (m+n-1)/n" and simplifying:
10932 * = 4 - (inputsize - stride - 3 + stride - 1) / stride
10933 * = 4 - (inputsize - 4) / stride;
10934 */
10935 level = 4 - (inputsize - 4) / stride;
10936 } else {
10937 /* For stage 2 translations the starting level is specified by the
10938 * VTCR_EL2.SL0 field (whose interpretation depends on the page size)
10939 */
1b4093ea
SS
10940 uint32_t sl0 = extract32(tcr->raw_tcr, 6, 2);
10941 uint32_t startlevel;
1853d5a9
EI
10942 bool ok;
10943
6e99f762 10944 if (!aarch64 || stride == 9) {
1853d5a9 10945 /* AArch32 or 4KB pages */
1b4093ea 10946 startlevel = 2 - sl0;
1853d5a9
EI
10947 } else {
10948 /* 16KB or 64KB pages */
1b4093ea 10949 startlevel = 3 - sl0;
1853d5a9
EI
10950 }
10951
10952 /* Check that the starting level is valid. */
6e99f762 10953 ok = check_s2_mmu_setup(cpu, aarch64, startlevel,
1b4093ea 10954 inputsize, stride);
1853d5a9 10955 if (!ok) {
da909b2c 10956 fault_type = ARMFault_Translation;
1853d5a9
EI
10957 goto do_fault;
10958 }
1b4093ea 10959 level = startlevel;
1853d5a9 10960 }
3dde962f 10961
dddb5223
SS
10962 indexmask_grainsize = (1ULL << (stride + 3)) - 1;
10963 indexmask = (1ULL << (inputsize - (stride * (4 - level)))) - 1;
3dde962f
PM
10964
10965 /* Now we can extract the actual base address from the TTBR */
2c8dd318 10966 descaddr = extract64(ttbr, 0, 48);
41a4bf1f
PM
10967 /*
10968 * We rely on this masking to clear the RES0 bits at the bottom of the TTBR
10969 * and also to mask out CnP (bit 0) which could validly be non-zero.
10970 */
dddb5223 10971 descaddr &= ~indexmask;
3dde962f 10972
6109769a 10973 /* The address field in the descriptor goes up to bit 39 for ARMv7
dddb5223
SS
10974 * but up to bit 47 for ARMv8, but we use the descaddrmask
10975 * up to bit 39 for AArch32, because we don't need other bits in that case
10976 * to construct next descriptor address (anyway they should be all zeroes).
6109769a 10977 */
6e99f762 10978 descaddrmask = ((1ull << (aarch64 ? 48 : 40)) - 1) &
dddb5223 10979 ~indexmask_grainsize;
6109769a 10980
ebca90e4
PM
10981 /* Secure accesses start with the page table in secure memory and
10982 * can be downgraded to non-secure at any step. Non-secure accesses
10983 * remain non-secure. We implement this by just ORing in the NSTable/NS
10984 * bits at each step.
10985 */
10986 tableattrs = regime_is_secure(env, mmu_idx) ? 0 : (1 << 4);
3dde962f
PM
10987 for (;;) {
10988 uint64_t descriptor;
ebca90e4 10989 bool nstable;
3dde962f 10990
dddb5223 10991 descaddr |= (address >> (stride * (4 - level))) & indexmask;
2c8dd318 10992 descaddr &= ~7ULL;
ebca90e4 10993 nstable = extract32(tableattrs, 4, 1);
3795a6de 10994 descriptor = arm_ldq_ptw(cs, descaddr, !nstable, mmu_idx, fi);
3b39d734 10995 if (fi->type != ARMFault_None) {
37785977
EI
10996 goto do_fault;
10997 }
10998
3dde962f
PM
10999 if (!(descriptor & 1) ||
11000 (!(descriptor & 2) && (level == 3))) {
11001 /* Invalid, or the Reserved level 3 encoding */
11002 goto do_fault;
11003 }
6109769a 11004 descaddr = descriptor & descaddrmask;
3dde962f
PM
11005
11006 if ((descriptor & 2) && (level < 3)) {
037c13c5 11007 /* Table entry. The top five bits are attributes which may
3dde962f
PM
11008 * propagate down through lower levels of the table (and
11009 * which are all arranged so that 0 means "no effect", so
11010 * we can gather them up by ORing in the bits at each level).
11011 */
11012 tableattrs |= extract64(descriptor, 59, 5);
11013 level++;
dddb5223 11014 indexmask = indexmask_grainsize;
3dde962f
PM
11015 continue;
11016 }
11017 /* Block entry at level 1 or 2, or page entry at level 3.
11018 * These are basically the same thing, although the number
11019 * of bits we pull in from the vaddr varies.
11020 */
973a5434 11021 page_size = (1ULL << ((stride * (4 - level)) + 3));
3dde962f 11022 descaddr |= (address & (page_size - 1));
6ab1a5ee 11023 /* Extract attributes from the descriptor */
d615efac
IC
11024 attrs = extract64(descriptor, 2, 10)
11025 | (extract64(descriptor, 52, 12) << 10);
6ab1a5ee 11026
97fa9350 11027 if (mmu_idx == ARMMMUIdx_Stage2) {
6ab1a5ee
EI
11028 /* Stage 2 table descriptors do not include any attribute fields */
11029 break;
11030 }
11031 /* Merge in attributes from table descriptors */
037c13c5 11032 attrs |= nstable << 3; /* NS */
1bafc2ba 11033 guarded = extract64(descriptor, 50, 1); /* GP */
ba97be9f 11034 if (param.hpd) {
037c13c5
RH
11035 /* HPD disables all the table attributes except NSTable. */
11036 break;
11037 }
11038 attrs |= extract32(tableattrs, 0, 2) << 11; /* XN, PXN */
3dde962f
PM
11039 /* The sense of AP[1] vs APTable[0] is reversed, as APTable[0] == 1
11040 * means "force PL1 access only", which means forcing AP[1] to 0.
11041 */
037c13c5
RH
11042 attrs &= ~(extract32(tableattrs, 2, 1) << 4); /* !APT[0] => AP[1] */
11043 attrs |= extract32(tableattrs, 3, 1) << 5; /* APT[1] => AP[2] */
3dde962f
PM
11044 break;
11045 }
11046 /* Here descaddr is the final physical address, and attributes
11047 * are all in attrs.
11048 */
da909b2c 11049 fault_type = ARMFault_AccessFlag;
3dde962f
PM
11050 if ((attrs & (1 << 8)) == 0) {
11051 /* Access flag */
11052 goto do_fault;
11053 }
d8e052b3
AJ
11054
11055 ap = extract32(attrs, 4, 2);
d8e052b3 11056
97fa9350 11057 if (mmu_idx == ARMMMUIdx_Stage2) {
6ab1a5ee 11058 ns = true;
ce3125be
PM
11059 xn = extract32(attrs, 11, 2);
11060 *prot = get_S2prot(env, ap, xn, s1_is_el0);
6ab1a5ee
EI
11061 } else {
11062 ns = extract32(attrs, 3, 1);
ce3125be 11063 xn = extract32(attrs, 12, 1);
6ab1a5ee 11064 pxn = extract32(attrs, 11, 1);
6e99f762 11065 *prot = get_S1prot(env, mmu_idx, aarch64, ap, ns, xn, pxn);
6ab1a5ee 11066 }
d8e052b3 11067
da909b2c 11068 fault_type = ARMFault_Permission;
d8e052b3 11069 if (!(*prot & (1 << access_type))) {
3dde962f
PM
11070 goto do_fault;
11071 }
3dde962f 11072
8bf5b6a9
PM
11073 if (ns) {
11074 /* The NS bit will (as required by the architecture) have no effect if
11075 * the CPU doesn't support TZ or this is a non-secure translation
11076 * regime, because the attribute will already be non-secure.
11077 */
11078 txattrs->secure = false;
11079 }
1bafc2ba
RH
11080 /* When in aarch64 mode, and BTI is enabled, remember GP in the IOTLB. */
11081 if (aarch64 && guarded && cpu_isar_feature(aa64_bti, cpu)) {
149d3b31 11082 arm_tlb_bti_gp(txattrs) = true;
1bafc2ba 11083 }
5b2d261d
AB
11084
11085 if (cacheattrs != NULL) {
97fa9350 11086 if (mmu_idx == ARMMMUIdx_Stage2) {
5b2d261d
AB
11087 cacheattrs->attrs = convert_stage2_attrs(env,
11088 extract32(attrs, 0, 4));
11089 } else {
11090 /* Index into MAIR registers for cache attributes */
11091 uint8_t attrindx = extract32(attrs, 0, 3);
11092 uint64_t mair = env->cp15.mair_el[regime_el(env, mmu_idx)];
11093 assert(attrindx <= 7);
11094 cacheattrs->attrs = extract64(mair, attrindx * 8, 8);
11095 }
11096 cacheattrs->shareability = extract32(attrs, 6, 2);
11097 }
11098
3dde962f
PM
11099 *phys_ptr = descaddr;
11100 *page_size_ptr = page_size;
b7cc4e82 11101 return false;
3dde962f
PM
11102
11103do_fault:
da909b2c
PM
11104 fi->type = fault_type;
11105 fi->level = level;
37785977 11106 /* Tag the error as S2 for failed S1 PTW at S2 or ordinary S2. */
97fa9350 11107 fi->stage2 = fi->s1ptw || (mmu_idx == ARMMMUIdx_Stage2);
b7cc4e82 11108 return true;
3dde962f
PM
11109}
11110
f6bda88f
PC
11111static inline void get_phys_addr_pmsav7_default(CPUARMState *env,
11112 ARMMMUIdx mmu_idx,
11113 int32_t address, int *prot)
11114{
3a00d560
MD
11115 if (!arm_feature(env, ARM_FEATURE_M)) {
11116 *prot = PAGE_READ | PAGE_WRITE;
11117 switch (address) {
11118 case 0xF0000000 ... 0xFFFFFFFF:
11119 if (regime_sctlr(env, mmu_idx) & SCTLR_V) {
11120 /* hivecs execing is ok */
11121 *prot |= PAGE_EXEC;
11122 }
11123 break;
11124 case 0x00000000 ... 0x7FFFFFFF:
f6bda88f 11125 *prot |= PAGE_EXEC;
3a00d560
MD
11126 break;
11127 }
11128 } else {
11129 /* Default system address map for M profile cores.
11130 * The architecture specifies which regions are execute-never;
11131 * at the MPU level no other checks are defined.
11132 */
11133 switch (address) {
11134 case 0x00000000 ... 0x1fffffff: /* ROM */
11135 case 0x20000000 ... 0x3fffffff: /* SRAM */
11136 case 0x60000000 ... 0x7fffffff: /* RAM */
11137 case 0x80000000 ... 0x9fffffff: /* RAM */
11138 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
11139 break;
11140 case 0x40000000 ... 0x5fffffff: /* Peripheral */
11141 case 0xa0000000 ... 0xbfffffff: /* Device */
11142 case 0xc0000000 ... 0xdfffffff: /* Device */
11143 case 0xe0000000 ... 0xffffffff: /* System */
11144 *prot = PAGE_READ | PAGE_WRITE;
11145 break;
11146 default:
11147 g_assert_not_reached();
f6bda88f 11148 }
f6bda88f 11149 }
f6bda88f
PC
11150}
11151
29c483a5
MD
11152static bool pmsav7_use_background_region(ARMCPU *cpu,
11153 ARMMMUIdx mmu_idx, bool is_user)
11154{
11155 /* Return true if we should use the default memory map as a
11156 * "background" region if there are no hits against any MPU regions.
11157 */
11158 CPUARMState *env = &cpu->env;
11159
11160 if (is_user) {
11161 return false;
11162 }
11163
11164 if (arm_feature(env, ARM_FEATURE_M)) {
ecf5e8ea
PM
11165 return env->v7m.mpu_ctrl[regime_is_secure(env, mmu_idx)]
11166 & R_V7M_MPU_CTRL_PRIVDEFENA_MASK;
29c483a5
MD
11167 } else {
11168 return regime_sctlr(env, mmu_idx) & SCTLR_BR;
11169 }
11170}
11171
38aaa60c
PM
11172static inline bool m_is_ppb_region(CPUARMState *env, uint32_t address)
11173{
11174 /* True if address is in the M profile PPB region 0xe0000000 - 0xe00fffff */
11175 return arm_feature(env, ARM_FEATURE_M) &&
11176 extract32(address, 20, 12) == 0xe00;
11177}
11178
bf446a11
PM
11179static inline bool m_is_system_region(CPUARMState *env, uint32_t address)
11180{
11181 /* True if address is in the M profile system region
11182 * 0xe0000000 - 0xffffffff
11183 */
11184 return arm_feature(env, ARM_FEATURE_M) && extract32(address, 29, 3) == 0x7;
11185}
11186
f6bda88f 11187static bool get_phys_addr_pmsav7(CPUARMState *env, uint32_t address,
03ae85f8 11188 MMUAccessType access_type, ARMMMUIdx mmu_idx,
9375ad15 11189 hwaddr *phys_ptr, int *prot,
e5e40999 11190 target_ulong *page_size,
9375ad15 11191 ARMMMUFaultInfo *fi)
f6bda88f 11192{
2fc0cc0e 11193 ARMCPU *cpu = env_archcpu(env);
f6bda88f
PC
11194 int n;
11195 bool is_user = regime_is_user(env, mmu_idx);
11196
11197 *phys_ptr = address;
e5e40999 11198 *page_size = TARGET_PAGE_SIZE;
f6bda88f
PC
11199 *prot = 0;
11200
38aaa60c
PM
11201 if (regime_translation_disabled(env, mmu_idx) ||
11202 m_is_ppb_region(env, address)) {
11203 /* MPU disabled or M profile PPB access: use default memory map.
11204 * The other case which uses the default memory map in the
11205 * v7M ARM ARM pseudocode is exception vector reads from the vector
11206 * table. In QEMU those accesses are done in arm_v7m_load_vector(),
11207 * which always does a direct read using address_space_ldl(), rather
11208 * than going via this function, so we don't need to check that here.
11209 */
f6bda88f
PC
11210 get_phys_addr_pmsav7_default(env, mmu_idx, address, prot);
11211 } else { /* MPU enabled */
11212 for (n = (int)cpu->pmsav7_dregion - 1; n >= 0; n--) {
11213 /* region search */
11214 uint32_t base = env->pmsav7.drbar[n];
11215 uint32_t rsize = extract32(env->pmsav7.drsr[n], 1, 5);
11216 uint32_t rmask;
11217 bool srdis = false;
11218
11219 if (!(env->pmsav7.drsr[n] & 0x1)) {
11220 continue;
11221 }
11222
11223 if (!rsize) {
c9f9f124
MD
11224 qemu_log_mask(LOG_GUEST_ERROR,
11225 "DRSR[%d]: Rsize field cannot be 0\n", n);
f6bda88f
PC
11226 continue;
11227 }
11228 rsize++;
11229 rmask = (1ull << rsize) - 1;
11230
11231 if (base & rmask) {
c9f9f124
MD
11232 qemu_log_mask(LOG_GUEST_ERROR,
11233 "DRBAR[%d]: 0x%" PRIx32 " misaligned "
11234 "to DRSR region size, mask = 0x%" PRIx32 "\n",
11235 n, base, rmask);
f6bda88f
PC
11236 continue;
11237 }
11238
11239 if (address < base || address > base + rmask) {
9d2b5a58
PM
11240 /*
11241 * Address not in this region. We must check whether the
11242 * region covers addresses in the same page as our address.
11243 * In that case we must not report a size that covers the
11244 * whole page for a subsequent hit against a different MPU
11245 * region or the background region, because it would result in
11246 * incorrect TLB hits for subsequent accesses to addresses that
11247 * are in this MPU region.
11248 */
11249 if (ranges_overlap(base, rmask,
11250 address & TARGET_PAGE_MASK,
11251 TARGET_PAGE_SIZE)) {
11252 *page_size = 1;
11253 }
f6bda88f
PC
11254 continue;
11255 }
11256
11257 /* Region matched */
11258
11259 if (rsize >= 8) { /* no subregions for regions < 256 bytes */
11260 int i, snd;
11261 uint32_t srdis_mask;
11262
11263 rsize -= 3; /* sub region size (power of 2) */
11264 snd = ((address - base) >> rsize) & 0x7;
11265 srdis = extract32(env->pmsav7.drsr[n], snd + 8, 1);
11266
11267 srdis_mask = srdis ? 0x3 : 0x0;
11268 for (i = 2; i <= 8 && rsize < TARGET_PAGE_BITS; i *= 2) {
11269 /* This will check in groups of 2, 4 and then 8, whether
11270 * the subregion bits are consistent. rsize is incremented
11271 * back up to give the region size, considering consistent
11272 * adjacent subregions as one region. Stop testing if rsize
11273 * is already big enough for an entire QEMU page.
11274 */
11275 int snd_rounded = snd & ~(i - 1);
11276 uint32_t srdis_multi = extract32(env->pmsav7.drsr[n],
11277 snd_rounded + 8, i);
11278 if (srdis_mask ^ srdis_multi) {
11279 break;
11280 }
11281 srdis_mask = (srdis_mask << i) | srdis_mask;
11282 rsize++;
11283 }
11284 }
f6bda88f
PC
11285 if (srdis) {
11286 continue;
11287 }
e5e40999
PM
11288 if (rsize < TARGET_PAGE_BITS) {
11289 *page_size = 1 << rsize;
11290 }
f6bda88f
PC
11291 break;
11292 }
11293
11294 if (n == -1) { /* no hits */
29c483a5 11295 if (!pmsav7_use_background_region(cpu, mmu_idx, is_user)) {
f6bda88f 11296 /* background fault */
9375ad15 11297 fi->type = ARMFault_Background;
f6bda88f
PC
11298 return true;
11299 }
11300 get_phys_addr_pmsav7_default(env, mmu_idx, address, prot);
11301 } else { /* a MPU hit! */
11302 uint32_t ap = extract32(env->pmsav7.dracr[n], 8, 3);
bf446a11
PM
11303 uint32_t xn = extract32(env->pmsav7.dracr[n], 12, 1);
11304
11305 if (m_is_system_region(env, address)) {
11306 /* System space is always execute never */
11307 xn = 1;
11308 }
f6bda88f
PC
11309
11310 if (is_user) { /* User mode AP bit decoding */
11311 switch (ap) {
11312 case 0:
11313 case 1:
11314 case 5:
11315 break; /* no access */
11316 case 3:
11317 *prot |= PAGE_WRITE;
11318 /* fall through */
11319 case 2:
11320 case 6:
11321 *prot |= PAGE_READ | PAGE_EXEC;
11322 break;
8638f1ad
PM
11323 case 7:
11324 /* for v7M, same as 6; for R profile a reserved value */
11325 if (arm_feature(env, ARM_FEATURE_M)) {
11326 *prot |= PAGE_READ | PAGE_EXEC;
11327 break;
11328 }
11329 /* fall through */
f6bda88f
PC
11330 default:
11331 qemu_log_mask(LOG_GUEST_ERROR,
c9f9f124
MD
11332 "DRACR[%d]: Bad value for AP bits: 0x%"
11333 PRIx32 "\n", n, ap);
f6bda88f
PC
11334 }
11335 } else { /* Priv. mode AP bits decoding */
11336 switch (ap) {
11337 case 0:
11338 break; /* no access */
11339 case 1:
11340 case 2:
11341 case 3:
11342 *prot |= PAGE_WRITE;
11343 /* fall through */
11344 case 5:
11345 case 6:
11346 *prot |= PAGE_READ | PAGE_EXEC;
11347 break;
8638f1ad
PM
11348 case 7:
11349 /* for v7M, same as 6; for R profile a reserved value */
11350 if (arm_feature(env, ARM_FEATURE_M)) {
11351 *prot |= PAGE_READ | PAGE_EXEC;
11352 break;
11353 }
11354 /* fall through */
f6bda88f
PC
11355 default:
11356 qemu_log_mask(LOG_GUEST_ERROR,
c9f9f124
MD
11357 "DRACR[%d]: Bad value for AP bits: 0x%"
11358 PRIx32 "\n", n, ap);
f6bda88f
PC
11359 }
11360 }
11361
11362 /* execute never */
bf446a11 11363 if (xn) {
f6bda88f
PC
11364 *prot &= ~PAGE_EXEC;
11365 }
11366 }
11367 }
11368
9375ad15
PM
11369 fi->type = ARMFault_Permission;
11370 fi->level = 1;
f6bda88f
PC
11371 return !(*prot & (1 << access_type));
11372}
11373
35337cc3
PM
11374static bool v8m_is_sau_exempt(CPUARMState *env,
11375 uint32_t address, MMUAccessType access_type)
11376{
11377 /* The architecture specifies that certain address ranges are
11378 * exempt from v8M SAU/IDAU checks.
11379 */
11380 return
11381 (access_type == MMU_INST_FETCH && m_is_system_region(env, address)) ||
11382 (address >= 0xe0000000 && address <= 0xe0002fff) ||
11383 (address >= 0xe000e000 && address <= 0xe000efff) ||
11384 (address >= 0xe002e000 && address <= 0xe002efff) ||
11385 (address >= 0xe0040000 && address <= 0xe0041fff) ||
11386 (address >= 0xe00ff000 && address <= 0xe00fffff);
11387}
11388
787a7e76 11389void v8m_security_lookup(CPUARMState *env, uint32_t address,
35337cc3
PM
11390 MMUAccessType access_type, ARMMMUIdx mmu_idx,
11391 V8M_SAttributes *sattrs)
11392{
11393 /* Look up the security attributes for this address. Compare the
11394 * pseudocode SecurityCheck() function.
11395 * We assume the caller has zero-initialized *sattrs.
11396 */
2fc0cc0e 11397 ARMCPU *cpu = env_archcpu(env);
35337cc3 11398 int r;
181962fd
PM
11399 bool idau_exempt = false, idau_ns = true, idau_nsc = true;
11400 int idau_region = IREGION_NOTVALID;
72042435
PM
11401 uint32_t addr_page_base = address & TARGET_PAGE_MASK;
11402 uint32_t addr_page_limit = addr_page_base + (TARGET_PAGE_SIZE - 1);
35337cc3 11403
181962fd
PM
11404 if (cpu->idau) {
11405 IDAUInterfaceClass *iic = IDAU_INTERFACE_GET_CLASS(cpu->idau);
11406 IDAUInterface *ii = IDAU_INTERFACE(cpu->idau);
11407
11408 iic->check(ii, address, &idau_region, &idau_exempt, &idau_ns,
11409 &idau_nsc);
11410 }
35337cc3
PM
11411
11412 if (access_type == MMU_INST_FETCH && extract32(address, 28, 4) == 0xf) {
11413 /* 0xf0000000..0xffffffff is always S for insn fetches */
11414 return;
11415 }
11416
181962fd 11417 if (idau_exempt || v8m_is_sau_exempt(env, address, access_type)) {
35337cc3
PM
11418 sattrs->ns = !regime_is_secure(env, mmu_idx);
11419 return;
11420 }
11421
181962fd
PM
11422 if (idau_region != IREGION_NOTVALID) {
11423 sattrs->irvalid = true;
11424 sattrs->iregion = idau_region;
11425 }
11426
35337cc3
PM
11427 switch (env->sau.ctrl & 3) {
11428 case 0: /* SAU.ENABLE == 0, SAU.ALLNS == 0 */
11429 break;
11430 case 2: /* SAU.ENABLE == 0, SAU.ALLNS == 1 */
11431 sattrs->ns = true;
11432 break;
11433 default: /* SAU.ENABLE == 1 */
11434 for (r = 0; r < cpu->sau_sregion; r++) {
11435 if (env->sau.rlar[r] & 1) {
11436 uint32_t base = env->sau.rbar[r] & ~0x1f;
11437 uint32_t limit = env->sau.rlar[r] | 0x1f;
11438
11439 if (base <= address && limit >= address) {
72042435
PM
11440 if (base > addr_page_base || limit < addr_page_limit) {
11441 sattrs->subpage = true;
11442 }
35337cc3
PM
11443 if (sattrs->srvalid) {
11444 /* If we hit in more than one region then we must report
11445 * as Secure, not NS-Callable, with no valid region
11446 * number info.
11447 */
11448 sattrs->ns = false;
11449 sattrs->nsc = false;
11450 sattrs->sregion = 0;
11451 sattrs->srvalid = false;
11452 break;
11453 } else {
11454 if (env->sau.rlar[r] & 2) {
11455 sattrs->nsc = true;
11456 } else {
11457 sattrs->ns = true;
11458 }
11459 sattrs->srvalid = true;
11460 sattrs->sregion = r;
11461 }
9d2b5a58
PM
11462 } else {
11463 /*
11464 * Address not in this region. We must check whether the
11465 * region covers addresses in the same page as our address.
11466 * In that case we must not report a size that covers the
11467 * whole page for a subsequent hit against a different MPU
11468 * region or the background region, because it would result
11469 * in incorrect TLB hits for subsequent accesses to
11470 * addresses that are in this MPU region.
11471 */
11472 if (limit >= base &&
11473 ranges_overlap(base, limit - base + 1,
11474 addr_page_base,
11475 TARGET_PAGE_SIZE)) {
11476 sattrs->subpage = true;
11477 }
35337cc3
PM
11478 }
11479 }
11480 }
7e3f1223
TR
11481 break;
11482 }
35337cc3 11483
7e3f1223
TR
11484 /*
11485 * The IDAU will override the SAU lookup results if it specifies
11486 * higher security than the SAU does.
11487 */
11488 if (!idau_ns) {
11489 if (sattrs->ns || (!idau_nsc && sattrs->nsc)) {
11490 sattrs->ns = false;
11491 sattrs->nsc = idau_nsc;
181962fd 11492 }
35337cc3
PM
11493 }
11494}
11495
787a7e76 11496bool pmsav8_mpu_lookup(CPUARMState *env, uint32_t address,
54317c0f
PM
11497 MMUAccessType access_type, ARMMMUIdx mmu_idx,
11498 hwaddr *phys_ptr, MemTxAttrs *txattrs,
72042435
PM
11499 int *prot, bool *is_subpage,
11500 ARMMMUFaultInfo *fi, uint32_t *mregion)
54317c0f
PM
11501{
11502 /* Perform a PMSAv8 MPU lookup (without also doing the SAU check
11503 * that a full phys-to-virt translation does).
11504 * mregion is (if not NULL) set to the region number which matched,
11505 * or -1 if no region number is returned (MPU off, address did not
11506 * hit a region, address hit in multiple regions).
72042435
PM
11507 * We set is_subpage to true if the region hit doesn't cover the
11508 * entire TARGET_PAGE the address is within.
54317c0f 11509 */
2fc0cc0e 11510 ARMCPU *cpu = env_archcpu(env);
504e3cc3 11511 bool is_user = regime_is_user(env, mmu_idx);
62c58ee0 11512 uint32_t secure = regime_is_secure(env, mmu_idx);
504e3cc3
PM
11513 int n;
11514 int matchregion = -1;
11515 bool hit = false;
72042435
PM
11516 uint32_t addr_page_base = address & TARGET_PAGE_MASK;
11517 uint32_t addr_page_limit = addr_page_base + (TARGET_PAGE_SIZE - 1);
504e3cc3 11518
72042435 11519 *is_subpage = false;
504e3cc3
PM
11520 *phys_ptr = address;
11521 *prot = 0;
54317c0f
PM
11522 if (mregion) {
11523 *mregion = -1;
35337cc3
PM
11524 }
11525
504e3cc3
PM
11526 /* Unlike the ARM ARM pseudocode, we don't need to check whether this
11527 * was an exception vector read from the vector table (which is always
11528 * done using the default system address map), because those accesses
11529 * are done in arm_v7m_load_vector(), which always does a direct
11530 * read using address_space_ldl(), rather than going via this function.
11531 */
11532 if (regime_translation_disabled(env, mmu_idx)) { /* MPU disabled */
11533 hit = true;
11534 } else if (m_is_ppb_region(env, address)) {
11535 hit = true;
504e3cc3 11536 } else {
cff21316
PM
11537 if (pmsav7_use_background_region(cpu, mmu_idx, is_user)) {
11538 hit = true;
11539 }
11540
504e3cc3
PM
11541 for (n = (int)cpu->pmsav7_dregion - 1; n >= 0; n--) {
11542 /* region search */
11543 /* Note that the base address is bits [31:5] from the register
11544 * with bits [4:0] all zeroes, but the limit address is bits
11545 * [31:5] from the register with bits [4:0] all ones.
11546 */
62c58ee0
PM
11547 uint32_t base = env->pmsav8.rbar[secure][n] & ~0x1f;
11548 uint32_t limit = env->pmsav8.rlar[secure][n] | 0x1f;
504e3cc3 11549
62c58ee0 11550 if (!(env->pmsav8.rlar[secure][n] & 0x1)) {
504e3cc3
PM
11551 /* Region disabled */
11552 continue;
11553 }
11554
11555 if (address < base || address > limit) {
9d2b5a58
PM
11556 /*
11557 * Address not in this region. We must check whether the
11558 * region covers addresses in the same page as our address.
11559 * In that case we must not report a size that covers the
11560 * whole page for a subsequent hit against a different MPU
11561 * region or the background region, because it would result in
11562 * incorrect TLB hits for subsequent accesses to addresses that
11563 * are in this MPU region.
11564 */
11565 if (limit >= base &&
11566 ranges_overlap(base, limit - base + 1,
11567 addr_page_base,
11568 TARGET_PAGE_SIZE)) {
11569 *is_subpage = true;
11570 }
504e3cc3
PM
11571 continue;
11572 }
11573
72042435
PM
11574 if (base > addr_page_base || limit < addr_page_limit) {
11575 *is_subpage = true;
11576 }
11577
cff21316 11578 if (matchregion != -1) {
504e3cc3
PM
11579 /* Multiple regions match -- always a failure (unlike
11580 * PMSAv7 where highest-numbered-region wins)
11581 */
3f551b5b
PM
11582 fi->type = ARMFault_Permission;
11583 fi->level = 1;
504e3cc3
PM
11584 return true;
11585 }
11586
11587 matchregion = n;
11588 hit = true;
504e3cc3
PM
11589 }
11590 }
11591
11592 if (!hit) {
11593 /* background fault */
3f551b5b 11594 fi->type = ARMFault_Background;
504e3cc3
PM
11595 return true;
11596 }
11597
11598 if (matchregion == -1) {
11599 /* hit using the background region */
11600 get_phys_addr_pmsav7_default(env, mmu_idx, address, prot);
11601 } else {
62c58ee0
PM
11602 uint32_t ap = extract32(env->pmsav8.rbar[secure][matchregion], 1, 2);
11603 uint32_t xn = extract32(env->pmsav8.rbar[secure][matchregion], 0, 1);
504e3cc3
PM
11604
11605 if (m_is_system_region(env, address)) {
11606 /* System space is always execute never */
11607 xn = 1;
11608 }
11609
11610 *prot = simple_ap_to_rw_prot(env, mmu_idx, ap);
11611 if (*prot && !xn) {
11612 *prot |= PAGE_EXEC;
11613 }
11614 /* We don't need to look the attribute up in the MAIR0/MAIR1
11615 * registers because that only tells us about cacheability.
11616 */
54317c0f
PM
11617 if (mregion) {
11618 *mregion = matchregion;
11619 }
504e3cc3
PM
11620 }
11621
3f551b5b
PM
11622 fi->type = ARMFault_Permission;
11623 fi->level = 1;
504e3cc3
PM
11624 return !(*prot & (1 << access_type));
11625}
11626
54317c0f
PM
11627
11628static bool get_phys_addr_pmsav8(CPUARMState *env, uint32_t address,
11629 MMUAccessType access_type, ARMMMUIdx mmu_idx,
11630 hwaddr *phys_ptr, MemTxAttrs *txattrs,
72042435
PM
11631 int *prot, target_ulong *page_size,
11632 ARMMMUFaultInfo *fi)
54317c0f
PM
11633{
11634 uint32_t secure = regime_is_secure(env, mmu_idx);
11635 V8M_SAttributes sattrs = {};
72042435
PM
11636 bool ret;
11637 bool mpu_is_subpage;
54317c0f
PM
11638
11639 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
11640 v8m_security_lookup(env, address, access_type, mmu_idx, &sattrs);
11641 if (access_type == MMU_INST_FETCH) {
11642 /* Instruction fetches always use the MMU bank and the
11643 * transaction attribute determined by the fetch address,
11644 * regardless of CPU state. This is painful for QEMU
11645 * to handle, because it would mean we need to encode
11646 * into the mmu_idx not just the (user, negpri) information
11647 * for the current security state but also that for the
11648 * other security state, which would balloon the number
11649 * of mmu_idx values needed alarmingly.
11650 * Fortunately we can avoid this because it's not actually
11651 * possible to arbitrarily execute code from memory with
11652 * the wrong security attribute: it will always generate
11653 * an exception of some kind or another, apart from the
11654 * special case of an NS CPU executing an SG instruction
11655 * in S&NSC memory. So we always just fail the translation
11656 * here and sort things out in the exception handler
11657 * (including possibly emulating an SG instruction).
11658 */
11659 if (sattrs.ns != !secure) {
3f551b5b
PM
11660 if (sattrs.nsc) {
11661 fi->type = ARMFault_QEMU_NSCExec;
11662 } else {
11663 fi->type = ARMFault_QEMU_SFault;
11664 }
72042435 11665 *page_size = sattrs.subpage ? 1 : TARGET_PAGE_SIZE;
54317c0f
PM
11666 *phys_ptr = address;
11667 *prot = 0;
11668 return true;
11669 }
11670 } else {
11671 /* For data accesses we always use the MMU bank indicated
11672 * by the current CPU state, but the security attributes
11673 * might downgrade a secure access to nonsecure.
11674 */
11675 if (sattrs.ns) {
11676 txattrs->secure = false;
11677 } else if (!secure) {
11678 /* NS access to S memory must fault.
11679 * Architecturally we should first check whether the
11680 * MPU information for this address indicates that we
11681 * are doing an unaligned access to Device memory, which
11682 * should generate a UsageFault instead. QEMU does not
11683 * currently check for that kind of unaligned access though.
11684 * If we added it we would need to do so as a special case
11685 * for M_FAKE_FSR_SFAULT in arm_v7m_cpu_do_interrupt().
11686 */
3f551b5b 11687 fi->type = ARMFault_QEMU_SFault;
72042435 11688 *page_size = sattrs.subpage ? 1 : TARGET_PAGE_SIZE;
54317c0f
PM
11689 *phys_ptr = address;
11690 *prot = 0;
11691 return true;
11692 }
11693 }
11694 }
11695
72042435
PM
11696 ret = pmsav8_mpu_lookup(env, address, access_type, mmu_idx, phys_ptr,
11697 txattrs, prot, &mpu_is_subpage, fi, NULL);
72042435
PM
11698 *page_size = sattrs.subpage || mpu_is_subpage ? 1 : TARGET_PAGE_SIZE;
11699 return ret;
54317c0f
PM
11700}
11701
13689d43 11702static bool get_phys_addr_pmsav5(CPUARMState *env, uint32_t address,
03ae85f8 11703 MMUAccessType access_type, ARMMMUIdx mmu_idx,
53a4e5c5
PM
11704 hwaddr *phys_ptr, int *prot,
11705 ARMMMUFaultInfo *fi)
9ee6e8bb
PB
11706{
11707 int n;
11708 uint32_t mask;
11709 uint32_t base;
0480f69a 11710 bool is_user = regime_is_user(env, mmu_idx);
9ee6e8bb 11711
3279adb9
PM
11712 if (regime_translation_disabled(env, mmu_idx)) {
11713 /* MPU disabled. */
11714 *phys_ptr = address;
11715 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
11716 return false;
11717 }
11718
9ee6e8bb
PB
11719 *phys_ptr = address;
11720 for (n = 7; n >= 0; n--) {
554b0b09 11721 base = env->cp15.c6_region[n];
87c3d486 11722 if ((base & 1) == 0) {
554b0b09 11723 continue;
87c3d486 11724 }
554b0b09
PM
11725 mask = 1 << ((base >> 1) & 0x1f);
11726 /* Keep this shift separate from the above to avoid an
11727 (undefined) << 32. */
11728 mask = (mask << 1) - 1;
87c3d486 11729 if (((base ^ address) & ~mask) == 0) {
554b0b09 11730 break;
87c3d486 11731 }
9ee6e8bb 11732 }
87c3d486 11733 if (n < 0) {
53a4e5c5 11734 fi->type = ARMFault_Background;
b7cc4e82 11735 return true;
87c3d486 11736 }
9ee6e8bb 11737
03ae85f8 11738 if (access_type == MMU_INST_FETCH) {
7e09797c 11739 mask = env->cp15.pmsav5_insn_ap;
9ee6e8bb 11740 } else {
7e09797c 11741 mask = env->cp15.pmsav5_data_ap;
9ee6e8bb
PB
11742 }
11743 mask = (mask >> (n * 4)) & 0xf;
11744 switch (mask) {
11745 case 0:
53a4e5c5
PM
11746 fi->type = ARMFault_Permission;
11747 fi->level = 1;
b7cc4e82 11748 return true;
9ee6e8bb 11749 case 1:
87c3d486 11750 if (is_user) {
53a4e5c5
PM
11751 fi->type = ARMFault_Permission;
11752 fi->level = 1;
b7cc4e82 11753 return true;
87c3d486 11754 }
554b0b09
PM
11755 *prot = PAGE_READ | PAGE_WRITE;
11756 break;
9ee6e8bb 11757 case 2:
554b0b09 11758 *prot = PAGE_READ;
87c3d486 11759 if (!is_user) {
554b0b09 11760 *prot |= PAGE_WRITE;
87c3d486 11761 }
554b0b09 11762 break;
9ee6e8bb 11763 case 3:
554b0b09
PM
11764 *prot = PAGE_READ | PAGE_WRITE;
11765 break;
9ee6e8bb 11766 case 5:
87c3d486 11767 if (is_user) {
53a4e5c5
PM
11768 fi->type = ARMFault_Permission;
11769 fi->level = 1;
b7cc4e82 11770 return true;
87c3d486 11771 }
554b0b09
PM
11772 *prot = PAGE_READ;
11773 break;
9ee6e8bb 11774 case 6:
554b0b09
PM
11775 *prot = PAGE_READ;
11776 break;
9ee6e8bb 11777 default:
554b0b09 11778 /* Bad permission. */
53a4e5c5
PM
11779 fi->type = ARMFault_Permission;
11780 fi->level = 1;
b7cc4e82 11781 return true;
9ee6e8bb 11782 }
3ad493fc 11783 *prot |= PAGE_EXEC;
b7cc4e82 11784 return false;
9ee6e8bb
PB
11785}
11786
5b2d261d
AB
11787/* Combine either inner or outer cacheability attributes for normal
11788 * memory, according to table D4-42 and pseudocode procedure
11789 * CombineS1S2AttrHints() of ARM DDI 0487B.b (the ARMv8 ARM).
11790 *
11791 * NB: only stage 1 includes allocation hints (RW bits), leading to
11792 * some asymmetry.
11793 */
11794static uint8_t combine_cacheattr_nibble(uint8_t s1, uint8_t s2)
11795{
11796 if (s1 == 4 || s2 == 4) {
11797 /* non-cacheable has precedence */
11798 return 4;
11799 } else if (extract32(s1, 2, 2) == 0 || extract32(s1, 2, 2) == 2) {
11800 /* stage 1 write-through takes precedence */
11801 return s1;
11802 } else if (extract32(s2, 2, 2) == 2) {
11803 /* stage 2 write-through takes precedence, but the allocation hint
11804 * is still taken from stage 1
11805 */
11806 return (2 << 2) | extract32(s1, 0, 2);
11807 } else { /* write-back */
11808 return s1;
11809 }
11810}
11811
11812/* Combine S1 and S2 cacheability/shareability attributes, per D4.5.4
11813 * and CombineS1S2Desc()
11814 *
11815 * @s1: Attributes from stage 1 walk
11816 * @s2: Attributes from stage 2 walk
11817 */
11818static ARMCacheAttrs combine_cacheattrs(ARMCacheAttrs s1, ARMCacheAttrs s2)
11819{
11820 uint8_t s1lo = extract32(s1.attrs, 0, 4), s2lo = extract32(s2.attrs, 0, 4);
11821 uint8_t s1hi = extract32(s1.attrs, 4, 4), s2hi = extract32(s2.attrs, 4, 4);
11822 ARMCacheAttrs ret;
11823
11824 /* Combine shareability attributes (table D4-43) */
11825 if (s1.shareability == 2 || s2.shareability == 2) {
11826 /* if either are outer-shareable, the result is outer-shareable */
11827 ret.shareability = 2;
11828 } else if (s1.shareability == 3 || s2.shareability == 3) {
11829 /* if either are inner-shareable, the result is inner-shareable */
11830 ret.shareability = 3;
11831 } else {
11832 /* both non-shareable */
11833 ret.shareability = 0;
11834 }
11835
11836 /* Combine memory type and cacheability attributes */
11837 if (s1hi == 0 || s2hi == 0) {
11838 /* Device has precedence over normal */
11839 if (s1lo == 0 || s2lo == 0) {
11840 /* nGnRnE has precedence over anything */
11841 ret.attrs = 0;
11842 } else if (s1lo == 4 || s2lo == 4) {
11843 /* non-Reordering has precedence over Reordering */
11844 ret.attrs = 4; /* nGnRE */
11845 } else if (s1lo == 8 || s2lo == 8) {
11846 /* non-Gathering has precedence over Gathering */
11847 ret.attrs = 8; /* nGRE */
11848 } else {
11849 ret.attrs = 0xc; /* GRE */
11850 }
11851
11852 /* Any location for which the resultant memory type is any
11853 * type of Device memory is always treated as Outer Shareable.
11854 */
11855 ret.shareability = 2;
11856 } else { /* Normal memory */
11857 /* Outer/inner cacheability combine independently */
11858 ret.attrs = combine_cacheattr_nibble(s1hi, s2hi) << 4
11859 | combine_cacheattr_nibble(s1lo, s2lo);
11860
11861 if (ret.attrs == 0x44) {
11862 /* Any location for which the resultant memory type is Normal
11863 * Inner Non-cacheable, Outer Non-cacheable is always treated
11864 * as Outer Shareable.
11865 */
11866 ret.shareability = 2;
11867 }
11868 }
11869
11870 return ret;
11871}
11872
11873
702a9357
PM
11874/* get_phys_addr - get the physical address for this virtual address
11875 *
11876 * Find the physical address corresponding to the given virtual address,
11877 * by doing a translation table walk on MMU based systems or using the
11878 * MPU state on MPU based systems.
11879 *
b7cc4e82
PC
11880 * Returns false if the translation was successful. Otherwise, phys_ptr, attrs,
11881 * prot and page_size may not be filled in, and the populated fsr value provides
702a9357
PM
11882 * information on why the translation aborted, in the format of a
11883 * DFSR/IFSR fault register, with the following caveats:
11884 * * we honour the short vs long DFSR format differences.
11885 * * the WnR bit is never set (the caller must do this).
f6bda88f 11886 * * for PSMAv5 based systems we don't bother to return a full FSR format
702a9357
PM
11887 * value.
11888 *
11889 * @env: CPUARMState
11890 * @address: virtual address to get physical address for
11891 * @access_type: 0 for read, 1 for write, 2 for execute
d3649702 11892 * @mmu_idx: MMU index indicating required translation regime
702a9357 11893 * @phys_ptr: set to the physical address corresponding to the virtual address
8bf5b6a9 11894 * @attrs: set to the memory transaction attributes to use
702a9357
PM
11895 * @prot: set to the permissions for the page containing phys_ptr
11896 * @page_size: set to the size of the page containing phys_ptr
5b2d261d
AB
11897 * @fi: set to fault info if the translation fails
11898 * @cacheattrs: (if non-NULL) set to the cacheability/shareability attributes
702a9357 11899 */
ebae861f
PMD
11900bool get_phys_addr(CPUARMState *env, target_ulong address,
11901 MMUAccessType access_type, ARMMMUIdx mmu_idx,
11902 hwaddr *phys_ptr, MemTxAttrs *attrs, int *prot,
11903 target_ulong *page_size,
11904 ARMMMUFaultInfo *fi, ARMCacheAttrs *cacheattrs)
9ee6e8bb 11905{
452ef8cb
RH
11906 if (mmu_idx == ARMMMUIdx_E10_0 ||
11907 mmu_idx == ARMMMUIdx_E10_1 ||
11908 mmu_idx == ARMMMUIdx_E10_1_PAN) {
9b539263
EI
11909 /* Call ourselves recursively to do the stage 1 and then stage 2
11910 * translations.
0480f69a 11911 */
9b539263
EI
11912 if (arm_feature(env, ARM_FEATURE_EL2)) {
11913 hwaddr ipa;
11914 int s2_prot;
11915 int ret;
5b2d261d 11916 ARMCacheAttrs cacheattrs2 = {};
9b539263
EI
11917
11918 ret = get_phys_addr(env, address, access_type,
8bd5c820 11919 stage_1_mmu_idx(mmu_idx), &ipa, attrs,
bc52bfeb 11920 prot, page_size, fi, cacheattrs);
9b539263
EI
11921
11922 /* If S1 fails or S2 is disabled, return early. */
97fa9350 11923 if (ret || regime_translation_disabled(env, ARMMMUIdx_Stage2)) {
9b539263
EI
11924 *phys_ptr = ipa;
11925 return ret;
11926 }
11927
11928 /* S1 is done. Now do S2 translation. */
97fa9350 11929 ret = get_phys_addr_lpae(env, ipa, access_type, ARMMMUIdx_Stage2,
ff7de2fc 11930 mmu_idx == ARMMMUIdx_E10_0,
9b539263 11931 phys_ptr, attrs, &s2_prot,
da909b2c 11932 page_size, fi,
5b2d261d 11933 cacheattrs != NULL ? &cacheattrs2 : NULL);
9b539263
EI
11934 fi->s2addr = ipa;
11935 /* Combine the S1 and S2 perms. */
11936 *prot &= s2_prot;
5b2d261d
AB
11937
11938 /* Combine the S1 and S2 cache attributes, if needed */
11939 if (!ret && cacheattrs != NULL) {
9d1bab33
PM
11940 if (env->cp15.hcr_el2 & HCR_DC) {
11941 /*
11942 * HCR.DC forces the first stage attributes to
11943 * Normal Non-Shareable,
11944 * Inner Write-Back Read-Allocate Write-Allocate,
11945 * Outer Write-Back Read-Allocate Write-Allocate.
11946 */
11947 cacheattrs->attrs = 0xff;
11948 cacheattrs->shareability = 0;
11949 }
5b2d261d
AB
11950 *cacheattrs = combine_cacheattrs(*cacheattrs, cacheattrs2);
11951 }
11952
9b539263
EI
11953 return ret;
11954 } else {
11955 /*
11956 * For non-EL2 CPUs a stage1+stage2 translation is just stage 1.
11957 */
8bd5c820 11958 mmu_idx = stage_1_mmu_idx(mmu_idx);
9b539263 11959 }
0480f69a 11960 }
d3649702 11961
8bf5b6a9
PM
11962 /* The page table entries may downgrade secure to non-secure, but
11963 * cannot upgrade an non-secure translation regime's attributes
11964 * to secure.
11965 */
11966 attrs->secure = regime_is_secure(env, mmu_idx);
0995bf8c 11967 attrs->user = regime_is_user(env, mmu_idx);
8bf5b6a9 11968
0480f69a
PM
11969 /* Fast Context Switch Extension. This doesn't exist at all in v8.
11970 * In v7 and earlier it affects all stage 1 translations.
11971 */
97fa9350 11972 if (address < 0x02000000 && mmu_idx != ARMMMUIdx_Stage2
0480f69a
PM
11973 && !arm_feature(env, ARM_FEATURE_V8)) {
11974 if (regime_el(env, mmu_idx) == 3) {
11975 address += env->cp15.fcseidr_s;
11976 } else {
11977 address += env->cp15.fcseidr_ns;
11978 }
54bf36ed 11979 }
9ee6e8bb 11980
3279adb9 11981 if (arm_feature(env, ARM_FEATURE_PMSA)) {
c9f9f124 11982 bool ret;
f6bda88f 11983 *page_size = TARGET_PAGE_SIZE;
3279adb9 11984
504e3cc3
PM
11985 if (arm_feature(env, ARM_FEATURE_V8)) {
11986 /* PMSAv8 */
11987 ret = get_phys_addr_pmsav8(env, address, access_type, mmu_idx,
72042435 11988 phys_ptr, attrs, prot, page_size, fi);
504e3cc3 11989 } else if (arm_feature(env, ARM_FEATURE_V7)) {
3279adb9
PM
11990 /* PMSAv7 */
11991 ret = get_phys_addr_pmsav7(env, address, access_type, mmu_idx,
e5e40999 11992 phys_ptr, prot, page_size, fi);
3279adb9
PM
11993 } else {
11994 /* Pre-v7 MPU */
11995 ret = get_phys_addr_pmsav5(env, address, access_type, mmu_idx,
53a4e5c5 11996 phys_ptr, prot, fi);
3279adb9
PM
11997 }
11998 qemu_log_mask(CPU_LOG_MMU, "PMSA MPU lookup for %s at 0x%08" PRIx32
c9f9f124 11999 " mmu_idx %u -> %s (prot %c%c%c)\n",
709e4407
PM
12000 access_type == MMU_DATA_LOAD ? "reading" :
12001 (access_type == MMU_DATA_STORE ? "writing" : "execute"),
c9f9f124
MD
12002 (uint32_t)address, mmu_idx,
12003 ret ? "Miss" : "Hit",
12004 *prot & PAGE_READ ? 'r' : '-',
12005 *prot & PAGE_WRITE ? 'w' : '-',
12006 *prot & PAGE_EXEC ? 'x' : '-');
12007
12008 return ret;
f6bda88f
PC
12009 }
12010
3279adb9
PM
12011 /* Definitely a real MMU, not an MPU */
12012
0480f69a 12013 if (regime_translation_disabled(env, mmu_idx)) {
cebfb648
RH
12014 /*
12015 * MMU disabled. S1 addresses within aa64 translation regimes are
12016 * still checked for bounds -- see AArch64.TranslateAddressS1Off.
12017 */
12018 if (mmu_idx != ARMMMUIdx_Stage2) {
12019 int r_el = regime_el(env, mmu_idx);
12020 if (arm_el_is_aa64(env, r_el)) {
12021 int pamax = arm_pamax(env_archcpu(env));
12022 uint64_t tcr = env->cp15.tcr_el[r_el].raw_tcr;
12023 int addrtop, tbi;
12024
12025 tbi = aa64_va_parameter_tbi(tcr, mmu_idx);
12026 if (access_type == MMU_INST_FETCH) {
12027 tbi &= ~aa64_va_parameter_tbid(tcr, mmu_idx);
12028 }
12029 tbi = (tbi >> extract64(address, 55, 1)) & 1;
12030 addrtop = (tbi ? 55 : 63);
12031
12032 if (extract64(address, pamax, addrtop - pamax + 1) != 0) {
12033 fi->type = ARMFault_AddressSize;
12034 fi->level = 0;
12035 fi->stage2 = false;
12036 return 1;
12037 }
12038
12039 /*
12040 * When TBI is disabled, we've just validated that all of the
12041 * bits above PAMax are zero, so logically we only need to
12042 * clear the top byte for TBI. But it's clearer to follow
12043 * the pseudocode set of addrdesc.paddress.
12044 */
12045 address = extract64(address, 0, 52);
12046 }
12047 }
9ee6e8bb 12048 *phys_ptr = address;
3ad493fc 12049 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
d4c430a8 12050 *page_size = TARGET_PAGE_SIZE;
9ee6e8bb 12051 return 0;
0480f69a
PM
12052 }
12053
0480f69a 12054 if (regime_using_lpae_format(env, mmu_idx)) {
ff7de2fc 12055 return get_phys_addr_lpae(env, address, access_type, mmu_idx, false,
bc52bfeb
PM
12056 phys_ptr, attrs, prot, page_size,
12057 fi, cacheattrs);
0480f69a 12058 } else if (regime_sctlr(env, mmu_idx) & SCTLR_XP) {
bc52bfeb
PM
12059 return get_phys_addr_v6(env, address, access_type, mmu_idx,
12060 phys_ptr, attrs, prot, page_size, fi);
9ee6e8bb 12061 } else {
bc52bfeb 12062 return get_phys_addr_v5(env, address, access_type, mmu_idx,
f989983e 12063 phys_ptr, prot, page_size, fi);
9ee6e8bb
PB
12064 }
12065}
12066
0faea0c7
PM
12067hwaddr arm_cpu_get_phys_page_attrs_debug(CPUState *cs, vaddr addr,
12068 MemTxAttrs *attrs)
b5ff1b31 12069{
00b941e5 12070 ARMCPU *cpu = ARM_CPU(cs);
d3649702 12071 CPUARMState *env = &cpu->env;
a8170e5e 12072 hwaddr phys_addr;
d4c430a8 12073 target_ulong page_size;
b5ff1b31 12074 int prot;
b7cc4e82 12075 bool ret;
e14b5a23 12076 ARMMMUFaultInfo fi = {};
50494a27 12077 ARMMMUIdx mmu_idx = arm_mmu_idx(env);
b5ff1b31 12078
0faea0c7
PM
12079 *attrs = (MemTxAttrs) {};
12080
8bd5c820 12081 ret = get_phys_addr(env, addr, 0, mmu_idx, &phys_addr,
bc52bfeb 12082 attrs, &prot, &page_size, &fi, NULL);
b5ff1b31 12083
b7cc4e82 12084 if (ret) {
b5ff1b31 12085 return -1;
00b941e5 12086 }
b5ff1b31
FB
12087 return phys_addr;
12088}
12089
b5ff1b31 12090#endif
6ddbc6e4
PB
12091
12092/* Note that signed overflow is undefined in C. The following routines are
12093 careful to use unsigned types where modulo arithmetic is required.
12094 Failure to do so _will_ break on newer gcc. */
12095
12096/* Signed saturating arithmetic. */
12097
1654b2d6 12098/* Perform 16-bit signed saturating addition. */
6ddbc6e4
PB
12099static inline uint16_t add16_sat(uint16_t a, uint16_t b)
12100{
12101 uint16_t res;
12102
12103 res = a + b;
12104 if (((res ^ a) & 0x8000) && !((a ^ b) & 0x8000)) {
12105 if (a & 0x8000)
12106 res = 0x8000;
12107 else
12108 res = 0x7fff;
12109 }
12110 return res;
12111}
12112
1654b2d6 12113/* Perform 8-bit signed saturating addition. */
6ddbc6e4
PB
12114static inline uint8_t add8_sat(uint8_t a, uint8_t b)
12115{
12116 uint8_t res;
12117
12118 res = a + b;
12119 if (((res ^ a) & 0x80) && !((a ^ b) & 0x80)) {
12120 if (a & 0x80)
12121 res = 0x80;
12122 else
12123 res = 0x7f;
12124 }
12125 return res;
12126}
12127
1654b2d6 12128/* Perform 16-bit signed saturating subtraction. */
6ddbc6e4
PB
12129static inline uint16_t sub16_sat(uint16_t a, uint16_t b)
12130{
12131 uint16_t res;
12132
12133 res = a - b;
12134 if (((res ^ a) & 0x8000) && ((a ^ b) & 0x8000)) {
12135 if (a & 0x8000)
12136 res = 0x8000;
12137 else
12138 res = 0x7fff;
12139 }
12140 return res;
12141}
12142
1654b2d6 12143/* Perform 8-bit signed saturating subtraction. */
6ddbc6e4
PB
12144static inline uint8_t sub8_sat(uint8_t a, uint8_t b)
12145{
12146 uint8_t res;
12147
12148 res = a - b;
12149 if (((res ^ a) & 0x80) && ((a ^ b) & 0x80)) {
12150 if (a & 0x80)
12151 res = 0x80;
12152 else
12153 res = 0x7f;
12154 }
12155 return res;
12156}
12157
12158#define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16);
12159#define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16);
12160#define ADD8(a, b, n) RESULT(add8_sat(a, b), n, 8);
12161#define SUB8(a, b, n) RESULT(sub8_sat(a, b), n, 8);
12162#define PFX q
12163
12164#include "op_addsub.h"
12165
12166/* Unsigned saturating arithmetic. */
460a09c1 12167static inline uint16_t add16_usat(uint16_t a, uint16_t b)
6ddbc6e4
PB
12168{
12169 uint16_t res;
12170 res = a + b;
12171 if (res < a)
12172 res = 0xffff;
12173 return res;
12174}
12175
460a09c1 12176static inline uint16_t sub16_usat(uint16_t a, uint16_t b)
6ddbc6e4 12177{
4c4fd3f8 12178 if (a > b)
6ddbc6e4
PB
12179 return a - b;
12180 else
12181 return 0;
12182}
12183
12184static inline uint8_t add8_usat(uint8_t a, uint8_t b)
12185{
12186 uint8_t res;
12187 res = a + b;
12188 if (res < a)
12189 res = 0xff;
12190 return res;
12191}
12192
12193static inline uint8_t sub8_usat(uint8_t a, uint8_t b)
12194{
4c4fd3f8 12195 if (a > b)
6ddbc6e4
PB
12196 return a - b;
12197 else
12198 return 0;
12199}
12200
12201#define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16);
12202#define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16);
12203#define ADD8(a, b, n) RESULT(add8_usat(a, b), n, 8);
12204#define SUB8(a, b, n) RESULT(sub8_usat(a, b), n, 8);
12205#define PFX uq
12206
12207#include "op_addsub.h"
12208
12209/* Signed modulo arithmetic. */
12210#define SARITH16(a, b, n, op) do { \
12211 int32_t sum; \
db6e2e65 12212 sum = (int32_t)(int16_t)(a) op (int32_t)(int16_t)(b); \
6ddbc6e4
PB
12213 RESULT(sum, n, 16); \
12214 if (sum >= 0) \
12215 ge |= 3 << (n * 2); \
12216 } while(0)
12217
12218#define SARITH8(a, b, n, op) do { \
12219 int32_t sum; \
db6e2e65 12220 sum = (int32_t)(int8_t)(a) op (int32_t)(int8_t)(b); \
6ddbc6e4
PB
12221 RESULT(sum, n, 8); \
12222 if (sum >= 0) \
12223 ge |= 1 << n; \
12224 } while(0)
12225
12226
12227#define ADD16(a, b, n) SARITH16(a, b, n, +)
12228#define SUB16(a, b, n) SARITH16(a, b, n, -)
12229#define ADD8(a, b, n) SARITH8(a, b, n, +)
12230#define SUB8(a, b, n) SARITH8(a, b, n, -)
12231#define PFX s
12232#define ARITH_GE
12233
12234#include "op_addsub.h"
12235
12236/* Unsigned modulo arithmetic. */
12237#define ADD16(a, b, n) do { \
12238 uint32_t sum; \
12239 sum = (uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b); \
12240 RESULT(sum, n, 16); \
a87aa10b 12241 if ((sum >> 16) == 1) \
6ddbc6e4
PB
12242 ge |= 3 << (n * 2); \
12243 } while(0)
12244
12245#define ADD8(a, b, n) do { \
12246 uint32_t sum; \
12247 sum = (uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b); \
12248 RESULT(sum, n, 8); \
a87aa10b
AZ
12249 if ((sum >> 8) == 1) \
12250 ge |= 1 << n; \
6ddbc6e4
PB
12251 } while(0)
12252
12253#define SUB16(a, b, n) do { \
12254 uint32_t sum; \
12255 sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \
12256 RESULT(sum, n, 16); \
12257 if ((sum >> 16) == 0) \
12258 ge |= 3 << (n * 2); \
12259 } while(0)
12260
12261#define SUB8(a, b, n) do { \
12262 uint32_t sum; \
12263 sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \
12264 RESULT(sum, n, 8); \
12265 if ((sum >> 8) == 0) \
a87aa10b 12266 ge |= 1 << n; \
6ddbc6e4
PB
12267 } while(0)
12268
12269#define PFX u
12270#define ARITH_GE
12271
12272#include "op_addsub.h"
12273
12274/* Halved signed arithmetic. */
12275#define ADD16(a, b, n) \
12276 RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16)
12277#define SUB16(a, b, n) \
12278 RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16)
12279#define ADD8(a, b, n) \
12280 RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8)
12281#define SUB8(a, b, n) \
12282 RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8)
12283#define PFX sh
12284
12285#include "op_addsub.h"
12286
12287/* Halved unsigned arithmetic. */
12288#define ADD16(a, b, n) \
12289 RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16)
12290#define SUB16(a, b, n) \
12291 RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16)
12292#define ADD8(a, b, n) \
12293 RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8)
12294#define SUB8(a, b, n) \
12295 RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8)
12296#define PFX uh
12297
12298#include "op_addsub.h"
12299
12300static inline uint8_t do_usad(uint8_t a, uint8_t b)
12301{
12302 if (a > b)
12303 return a - b;
12304 else
12305 return b - a;
12306}
12307
12308/* Unsigned sum of absolute byte differences. */
12309uint32_t HELPER(usad8)(uint32_t a, uint32_t b)
12310{
12311 uint32_t sum;
12312 sum = do_usad(a, b);
12313 sum += do_usad(a >> 8, b >> 8);
12314 sum += do_usad(a >> 16, b >>16);
12315 sum += do_usad(a >> 24, b >> 24);
12316 return sum;
12317}
12318
12319/* For ARMv6 SEL instruction. */
12320uint32_t HELPER(sel_flags)(uint32_t flags, uint32_t a, uint32_t b)
12321{
12322 uint32_t mask;
12323
12324 mask = 0;
12325 if (flags & 1)
12326 mask |= 0xff;
12327 if (flags & 2)
12328 mask |= 0xff00;
12329 if (flags & 4)
12330 mask |= 0xff0000;
12331 if (flags & 8)
12332 mask |= 0xff000000;
12333 return (a & mask) | (b & ~mask);
12334}
12335
aa633469
PM
12336/* CRC helpers.
12337 * The upper bytes of val (above the number specified by 'bytes') must have
12338 * been zeroed out by the caller.
12339 */
eb0ecd5a
WN
12340uint32_t HELPER(crc32)(uint32_t acc, uint32_t val, uint32_t bytes)
12341{
12342 uint8_t buf[4];
12343
aa633469 12344 stl_le_p(buf, val);
eb0ecd5a
WN
12345
12346 /* zlib crc32 converts the accumulator and output to one's complement. */
12347 return crc32(acc ^ 0xffffffff, buf, bytes) ^ 0xffffffff;
12348}
12349
12350uint32_t HELPER(crc32c)(uint32_t acc, uint32_t val, uint32_t bytes)
12351{
12352 uint8_t buf[4];
12353
aa633469 12354 stl_le_p(buf, val);
eb0ecd5a
WN
12355
12356 /* Linux crc32c converts the output to one's complement. */
12357 return crc32c(acc, buf, bytes) ^ 0xffffffff;
12358}
a9e01311
RH
12359
12360/* Return the exception level to which FP-disabled exceptions should
12361 * be taken, or 0 if FP is enabled.
12362 */
ced31551 12363int fp_exception_el(CPUARMState *env, int cur_el)
a9e01311 12364{
55faa212 12365#ifndef CONFIG_USER_ONLY
a9e01311
RH
12366 /* CPACR and the CPTR registers don't exist before v6, so FP is
12367 * always accessible
12368 */
12369 if (!arm_feature(env, ARM_FEATURE_V6)) {
12370 return 0;
12371 }
12372
d87513c0
PM
12373 if (arm_feature(env, ARM_FEATURE_M)) {
12374 /* CPACR can cause a NOCP UsageFault taken to current security state */
12375 if (!v7m_cpacr_pass(env, env->v7m.secure, cur_el != 0)) {
12376 return 1;
12377 }
12378
12379 if (arm_feature(env, ARM_FEATURE_M_SECURITY) && !env->v7m.secure) {
12380 if (!extract32(env->v7m.nsacr, 10, 1)) {
12381 /* FP insns cause a NOCP UsageFault taken to Secure */
12382 return 3;
12383 }
12384 }
12385
12386 return 0;
12387 }
12388
a9e01311
RH
12389 /* The CPACR controls traps to EL1, or PL1 if we're 32 bit:
12390 * 0, 2 : trap EL0 and EL1/PL1 accesses
12391 * 1 : trap only EL0 accesses
12392 * 3 : trap no accesses
c2ddb7cf 12393 * This register is ignored if E2H+TGE are both set.
a9e01311 12394 */
c2ddb7cf
RH
12395 if ((arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
12396 int fpen = extract32(env->cp15.cpacr_el1, 20, 2);
12397
12398 switch (fpen) {
12399 case 0:
12400 case 2:
12401 if (cur_el == 0 || cur_el == 1) {
12402 /* Trap to PL1, which might be EL1 or EL3 */
12403 if (arm_is_secure(env) && !arm_el_is_aa64(env, 3)) {
12404 return 3;
12405 }
12406 return 1;
12407 }
12408 if (cur_el == 3 && !is_a64(env)) {
12409 /* Secure PL1 running at EL3 */
a9e01311
RH
12410 return 3;
12411 }
c2ddb7cf
RH
12412 break;
12413 case 1:
12414 if (cur_el == 0) {
12415 return 1;
12416 }
12417 break;
12418 case 3:
12419 break;
a9e01311 12420 }
a9e01311
RH
12421 }
12422
fc1120a7
PM
12423 /*
12424 * The NSACR allows A-profile AArch32 EL3 and M-profile secure mode
12425 * to control non-secure access to the FPU. It doesn't have any
12426 * effect if EL3 is AArch64 or if EL3 doesn't exist at all.
12427 */
12428 if ((arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
12429 cur_el <= 2 && !arm_is_secure_below_el3(env))) {
12430 if (!extract32(env->cp15.nsacr, 10, 1)) {
12431 /* FP insns act as UNDEF */
12432 return cur_el == 2 ? 2 : 1;
12433 }
12434 }
12435
a9e01311
RH
12436 /* For the CPTR registers we don't need to guard with an ARM_FEATURE
12437 * check because zero bits in the registers mean "don't trap".
12438 */
12439
12440 /* CPTR_EL2 : present in v7VE or v8 */
12441 if (cur_el <= 2 && extract32(env->cp15.cptr_el[2], 10, 1)
12442 && !arm_is_secure_below_el3(env)) {
12443 /* Trap FP ops at EL2, NS-EL1 or NS-EL0 to EL2 */
12444 return 2;
12445 }
12446
12447 /* CPTR_EL3 : present in v8 */
12448 if (extract32(env->cp15.cptr_el[3], 10, 1)) {
12449 /* Trap all FP ops to EL3 */
12450 return 3;
12451 }
55faa212 12452#endif
a9e01311
RH
12453 return 0;
12454}
12455
b9f6033c
RH
12456/* Return the exception level we're running at if this is our mmu_idx */
12457int arm_mmu_idx_to_el(ARMMMUIdx mmu_idx)
12458{
12459 if (mmu_idx & ARM_MMU_IDX_M) {
12460 return mmu_idx & ARM_MMU_IDX_M_PRIV;
12461 }
12462
12463 switch (mmu_idx) {
12464 case ARMMMUIdx_E10_0:
12465 case ARMMMUIdx_E20_0:
12466 case ARMMMUIdx_SE10_0:
12467 return 0;
12468 case ARMMMUIdx_E10_1:
452ef8cb 12469 case ARMMMUIdx_E10_1_PAN:
b9f6033c 12470 case ARMMMUIdx_SE10_1:
452ef8cb 12471 case ARMMMUIdx_SE10_1_PAN:
b9f6033c
RH
12472 return 1;
12473 case ARMMMUIdx_E2:
12474 case ARMMMUIdx_E20_2:
452ef8cb 12475 case ARMMMUIdx_E20_2_PAN:
b9f6033c
RH
12476 return 2;
12477 case ARMMMUIdx_SE3:
12478 return 3;
12479 default:
12480 g_assert_not_reached();
12481 }
12482}
12483
7aab5a8c 12484#ifndef CONFIG_TCG
65e4655c
RH
12485ARMMMUIdx arm_v7m_mmu_idx_for_secstate(CPUARMState *env, bool secstate)
12486{
7aab5a8c 12487 g_assert_not_reached();
65e4655c 12488}
7aab5a8c 12489#endif
65e4655c 12490
164690b2 12491ARMMMUIdx arm_mmu_idx_el(CPUARMState *env, int el)
65e4655c 12492{
65e4655c 12493 if (arm_feature(env, ARM_FEATURE_M)) {
50494a27 12494 return arm_v7m_mmu_idx_for_secstate(env, env->v7m.secure);
65e4655c
RH
12495 }
12496
6003d980 12497 /* See ARM pseudo-function ELIsInHost. */
b9f6033c
RH
12498 switch (el) {
12499 case 0:
b9f6033c
RH
12500 if (arm_is_secure_below_el3(env)) {
12501 return ARMMMUIdx_SE10_0;
12502 }
6003d980
RH
12503 if ((env->cp15.hcr_el2 & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)
12504 && arm_el_is_aa64(env, 2)) {
12505 return ARMMMUIdx_E20_0;
12506 }
b9f6033c
RH
12507 return ARMMMUIdx_E10_0;
12508 case 1:
12509 if (arm_is_secure_below_el3(env)) {
66412260
RH
12510 if (env->pstate & PSTATE_PAN) {
12511 return ARMMMUIdx_SE10_1_PAN;
12512 }
b9f6033c
RH
12513 return ARMMMUIdx_SE10_1;
12514 }
66412260
RH
12515 if (env->pstate & PSTATE_PAN) {
12516 return ARMMMUIdx_E10_1_PAN;
12517 }
b9f6033c
RH
12518 return ARMMMUIdx_E10_1;
12519 case 2:
b9f6033c 12520 /* TODO: ARMv8.4-SecEL2 */
6003d980
RH
12521 /* Note that TGE does not apply at EL2. */
12522 if ((env->cp15.hcr_el2 & HCR_E2H) && arm_el_is_aa64(env, 2)) {
66412260
RH
12523 if (env->pstate & PSTATE_PAN) {
12524 return ARMMMUIdx_E20_2_PAN;
12525 }
6003d980
RH
12526 return ARMMMUIdx_E20_2;
12527 }
b9f6033c
RH
12528 return ARMMMUIdx_E2;
12529 case 3:
12530 return ARMMMUIdx_SE3;
12531 default:
12532 g_assert_not_reached();
65e4655c 12533 }
50494a27
RH
12534}
12535
164690b2
RH
12536ARMMMUIdx arm_mmu_idx(CPUARMState *env)
12537{
12538 return arm_mmu_idx_el(env, arm_current_el(env));
12539}
12540
64be86ab
RH
12541#ifndef CONFIG_USER_ONLY
12542ARMMMUIdx arm_stage1_mmu_idx(CPUARMState *env)
12543{
12544 return stage_1_mmu_idx(arm_mmu_idx(env));
12545}
12546#endif
12547
fdd1b228
RH
12548static uint32_t rebuild_hflags_common(CPUARMState *env, int fp_el,
12549 ARMMMUIdx mmu_idx, uint32_t flags)
12550{
12551 flags = FIELD_DP32(flags, TBFLAG_ANY, FPEXC_EL, fp_el);
12552 flags = FIELD_DP32(flags, TBFLAG_ANY, MMUIDX,
12553 arm_to_core_mmu_idx(mmu_idx));
12554
fdd1b228
RH
12555 if (arm_singlestep_active(env)) {
12556 flags = FIELD_DP32(flags, TBFLAG_ANY, SS_ACTIVE, 1);
12557 }
12558 return flags;
12559}
12560
43eccfb6
RH
12561static uint32_t rebuild_hflags_common_32(CPUARMState *env, int fp_el,
12562 ARMMMUIdx mmu_idx, uint32_t flags)
12563{
8061a649
RH
12564 bool sctlr_b = arm_sctlr_b(env);
12565
12566 if (sctlr_b) {
12567 flags = FIELD_DP32(flags, TBFLAG_A32, SCTLR_B, 1);
12568 }
12569 if (arm_cpu_data_is_big_endian_a32(env, sctlr_b)) {
12570 flags = FIELD_DP32(flags, TBFLAG_ANY, BE_DATA, 1);
12571 }
43eccfb6
RH
12572 flags = FIELD_DP32(flags, TBFLAG_A32, NS, !access_secure_reg(env));
12573
12574 return rebuild_hflags_common(env, fp_el, mmu_idx, flags);
12575}
12576
6e33ced5
RH
12577static uint32_t rebuild_hflags_m32(CPUARMState *env, int fp_el,
12578 ARMMMUIdx mmu_idx)
12579{
12580 uint32_t flags = 0;
12581
12582 if (arm_v7m_is_handler_mode(env)) {
79cabf1f 12583 flags = FIELD_DP32(flags, TBFLAG_M32, HANDLER, 1);
6e33ced5
RH
12584 }
12585
12586 /*
12587 * v8M always applies stack limit checks unless CCR.STKOFHFNMIGN
12588 * is suppressing them because the requested execution priority
12589 * is less than 0.
12590 */
12591 if (arm_feature(env, ARM_FEATURE_V8) &&
12592 !((mmu_idx & ARM_MMU_IDX_M_NEGPRI) &&
12593 (env->v7m.ccr[env->v7m.secure] & R_V7M_CCR_STKOFHFNMIGN_MASK))) {
79cabf1f 12594 flags = FIELD_DP32(flags, TBFLAG_M32, STACKCHECK, 1);
6e33ced5
RH
12595 }
12596
12597 return rebuild_hflags_common_32(env, fp_el, mmu_idx, flags);
12598}
12599
83f4baef
RH
12600static uint32_t rebuild_hflags_aprofile(CPUARMState *env)
12601{
12602 int flags = 0;
12603
12604 flags = FIELD_DP32(flags, TBFLAG_ANY, DEBUG_TARGET_EL,
12605 arm_debug_target_el(env));
12606 return flags;
12607}
12608
c747224c
RH
12609static uint32_t rebuild_hflags_a32(CPUARMState *env, int fp_el,
12610 ARMMMUIdx mmu_idx)
12611{
83f4baef 12612 uint32_t flags = rebuild_hflags_aprofile(env);
0a54d68e
RH
12613
12614 if (arm_el_is_aa64(env, 1)) {
12615 flags = FIELD_DP32(flags, TBFLAG_A32, VFPEN, 1);
12616 }
5bb0a20b
MZ
12617
12618 if (arm_current_el(env) < 2 && env->cp15.hstr_el2 &&
12619 (arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
12620 flags = FIELD_DP32(flags, TBFLAG_A32, HSTR_ACTIVE, 1);
12621 }
12622
83f4baef 12623 return rebuild_hflags_common_32(env, fp_el, mmu_idx, flags);
c747224c
RH
12624}
12625
d4d7503a
RH
12626static uint32_t rebuild_hflags_a64(CPUARMState *env, int el, int fp_el,
12627 ARMMMUIdx mmu_idx)
a9e01311 12628{
83f4baef 12629 uint32_t flags = rebuild_hflags_aprofile(env);
d4d7503a 12630 ARMMMUIdx stage1 = stage_1_mmu_idx(mmu_idx);
b830a5ee 12631 uint64_t tcr = regime_tcr(env, mmu_idx)->raw_tcr;
d4d7503a
RH
12632 uint64_t sctlr;
12633 int tbii, tbid;
b9adaa70 12634
d4d7503a 12635 flags = FIELD_DP32(flags, TBFLAG_ANY, AARCH64_STATE, 1);
cd208a1c 12636
339370b9 12637 /* Get control bits for tagged addresses. */
b830a5ee
RH
12638 tbid = aa64_va_parameter_tbi(tcr, mmu_idx);
12639 tbii = tbid & ~aa64_va_parameter_tbid(tcr, mmu_idx);
5d8634f5 12640
d4d7503a
RH
12641 flags = FIELD_DP32(flags, TBFLAG_A64, TBII, tbii);
12642 flags = FIELD_DP32(flags, TBFLAG_A64, TBID, tbid);
12643
12644 if (cpu_isar_feature(aa64_sve, env_archcpu(env))) {
12645 int sve_el = sve_exception_el(env, el);
12646 uint32_t zcr_len;
5d8634f5 12647
d4d7503a
RH
12648 /*
12649 * If SVE is disabled, but FP is enabled,
12650 * then the effective len is 0.
12651 */
12652 if (sve_el != 0 && fp_el == 0) {
12653 zcr_len = 0;
12654 } else {
12655 zcr_len = sve_zcr_len_for_el(env, el);
5d8634f5 12656 }
d4d7503a
RH
12657 flags = FIELD_DP32(flags, TBFLAG_A64, SVEEXC_EL, sve_el);
12658 flags = FIELD_DP32(flags, TBFLAG_A64, ZCR_LEN, zcr_len);
12659 }
1db5e96c 12660
aaec1432 12661 sctlr = regime_sctlr(env, stage1);
1db5e96c 12662
8061a649
RH
12663 if (arm_cpu_data_is_big_endian_a64(el, sctlr)) {
12664 flags = FIELD_DP32(flags, TBFLAG_ANY, BE_DATA, 1);
12665 }
12666
d4d7503a
RH
12667 if (cpu_isar_feature(aa64_pauth, env_archcpu(env))) {
12668 /*
12669 * In order to save space in flags, we record only whether
12670 * pauth is "inactive", meaning all insns are implemented as
12671 * a nop, or "active" when some action must be performed.
12672 * The decision of which action to take is left to a helper.
12673 */
12674 if (sctlr & (SCTLR_EnIA | SCTLR_EnIB | SCTLR_EnDA | SCTLR_EnDB)) {
12675 flags = FIELD_DP32(flags, TBFLAG_A64, PAUTH_ACTIVE, 1);
1db5e96c 12676 }
d4d7503a 12677 }
0816ef1b 12678
d4d7503a
RH
12679 if (cpu_isar_feature(aa64_bti, env_archcpu(env))) {
12680 /* Note that SCTLR_EL[23].BT == SCTLR_BT1. */
12681 if (sctlr & (el == 0 ? SCTLR_BT0 : SCTLR_BT1)) {
12682 flags = FIELD_DP32(flags, TBFLAG_A64, BT, 1);
0816ef1b 12683 }
d4d7503a 12684 }
08f1434a 12685
cc28fc30 12686 /* Compute the condition for using AccType_UNPRIV for LDTR et al. */
7a8014ab
RH
12687 if (!(env->pstate & PSTATE_UAO)) {
12688 switch (mmu_idx) {
12689 case ARMMMUIdx_E10_1:
12690 case ARMMMUIdx_E10_1_PAN:
12691 case ARMMMUIdx_SE10_1:
12692 case ARMMMUIdx_SE10_1_PAN:
12693 /* TODO: ARMv8.3-NV */
cc28fc30 12694 flags = FIELD_DP32(flags, TBFLAG_A64, UNPRIV, 1);
7a8014ab
RH
12695 break;
12696 case ARMMMUIdx_E20_2:
12697 case ARMMMUIdx_E20_2_PAN:
12698 /* TODO: ARMv8.4-SecEL2 */
12699 /*
12700 * Note that EL20_2 is gated by HCR_EL2.E2H == 1, but EL20_0 is
12701 * gated by HCR_EL2.<E2H,TGE> == '11', and so is LDTR.
12702 */
12703 if (env->cp15.hcr_el2 & HCR_TGE) {
12704 flags = FIELD_DP32(flags, TBFLAG_A64, UNPRIV, 1);
12705 }
12706 break;
12707 default:
12708 break;
cc28fc30 12709 }
cc28fc30
RH
12710 }
12711
81ae05fa
RH
12712 if (cpu_isar_feature(aa64_mte, env_archcpu(env))) {
12713 /*
12714 * Set MTE_ACTIVE if any access may be Checked, and leave clear
12715 * if all accesses must be Unchecked:
12716 * 1) If no TBI, then there are no tags in the address to check,
12717 * 2) If Tag Check Override, then all accesses are Unchecked,
12718 * 3) If Tag Check Fail == 0, then Checked access have no effect,
12719 * 4) If no Allocation Tag Access, then all accesses are Unchecked.
12720 */
12721 if (allocation_tag_access_enabled(env, el, sctlr)) {
12722 flags = FIELD_DP32(flags, TBFLAG_A64, ATA, 1);
12723 if (tbid
12724 && !(env->pstate & PSTATE_TCO)
12725 && (sctlr & (el == 0 ? SCTLR_TCF0 : SCTLR_TCF))) {
12726 flags = FIELD_DP32(flags, TBFLAG_A64, MTE_ACTIVE, 1);
12727 }
12728 }
12729 /* And again for unprivileged accesses, if required. */
12730 if (FIELD_EX32(flags, TBFLAG_A64, UNPRIV)
12731 && tbid
12732 && !(env->pstate & PSTATE_TCO)
12733 && (sctlr & SCTLR_TCF0)
12734 && allocation_tag_access_enabled(env, 0, sctlr)) {
12735 flags = FIELD_DP32(flags, TBFLAG_A64, MTE0_ACTIVE, 1);
12736 }
12737 /* Cache TCMA as well as TBI. */
12738 flags = FIELD_DP32(flags, TBFLAG_A64, TCMA,
12739 aa64_va_parameter_tcma(tcr, mmu_idx));
12740 }
12741
d4d7503a
RH
12742 return rebuild_hflags_common(env, fp_el, mmu_idx, flags);
12743}
12744
3d74e2e9
RH
12745static uint32_t rebuild_hflags_internal(CPUARMState *env)
12746{
12747 int el = arm_current_el(env);
12748 int fp_el = fp_exception_el(env, el);
164690b2 12749 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
3d74e2e9
RH
12750
12751 if (is_a64(env)) {
12752 return rebuild_hflags_a64(env, el, fp_el, mmu_idx);
12753 } else if (arm_feature(env, ARM_FEATURE_M)) {
12754 return rebuild_hflags_m32(env, fp_el, mmu_idx);
12755 } else {
12756 return rebuild_hflags_a32(env, fp_el, mmu_idx);
12757 }
12758}
12759
12760void arm_rebuild_hflags(CPUARMState *env)
12761{
12762 env->hflags = rebuild_hflags_internal(env);
12763}
12764
19717e9b
PM
12765/*
12766 * If we have triggered a EL state change we can't rely on the
12767 * translator having passed it to us, we need to recompute.
12768 */
12769void HELPER(rebuild_hflags_m32_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_m32(env, fp_el, mmu_idx);
12775}
12776
14f3c588
RH
12777void HELPER(rebuild_hflags_m32)(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_m32(env, fp_el, mmu_idx);
12783}
12784
f80741d1
AB
12785/*
12786 * If we have triggered a EL state change we can't rely on the
563152e0 12787 * translator having passed it to us, we need to recompute.
f80741d1
AB
12788 */
12789void HELPER(rebuild_hflags_a32_newel)(CPUARMState *env)
12790{
12791 int el = arm_current_el(env);
12792 int fp_el = fp_exception_el(env, el);
12793 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
12794 env->hflags = rebuild_hflags_a32(env, fp_el, mmu_idx);
12795}
12796
14f3c588
RH
12797void HELPER(rebuild_hflags_a32)(CPUARMState *env, int el)
12798{
12799 int fp_el = fp_exception_el(env, el);
12800 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
12801
12802 env->hflags = rebuild_hflags_a32(env, fp_el, mmu_idx);
12803}
12804
12805void HELPER(rebuild_hflags_a64)(CPUARMState *env, int el)
12806{
12807 int fp_el = fp_exception_el(env, el);
12808 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
12809
12810 env->hflags = rebuild_hflags_a64(env, el, fp_el, mmu_idx);
12811}
12812
0ee8b24a
PMD
12813static inline void assert_hflags_rebuild_correctly(CPUARMState *env)
12814{
12815#ifdef CONFIG_DEBUG_TCG
12816 uint32_t env_flags_current = env->hflags;
12817 uint32_t env_flags_rebuilt = rebuild_hflags_internal(env);
12818
12819 if (unlikely(env_flags_current != env_flags_rebuilt)) {
12820 fprintf(stderr, "TCG hflags mismatch (current:0x%08x rebuilt:0x%08x)\n",
12821 env_flags_current, env_flags_rebuilt);
12822 abort();
12823 }
12824#endif
12825}
12826
d4d7503a
RH
12827void cpu_get_tb_cpu_state(CPUARMState *env, target_ulong *pc,
12828 target_ulong *cs_base, uint32_t *pflags)
12829{
e979972a
RH
12830 uint32_t flags = env->hflags;
12831 uint32_t pstate_for_ss;
d4d7503a 12832
9b253fe5 12833 *cs_base = 0;
0ee8b24a 12834 assert_hflags_rebuild_correctly(env);
3d74e2e9 12835
e979972a 12836 if (FIELD_EX32(flags, TBFLAG_ANY, AARCH64_STATE)) {
d4d7503a 12837 *pc = env->pc;
d4d7503a 12838 if (cpu_isar_feature(aa64_bti, env_archcpu(env))) {
08f1434a
RH
12839 flags = FIELD_DP32(flags, TBFLAG_A64, BTYPE, env->btype);
12840 }
60e12c37 12841 pstate_for_ss = env->pstate;
a9e01311
RH
12842 } else {
12843 *pc = env->regs[15];
6e33ced5
RH
12844
12845 if (arm_feature(env, ARM_FEATURE_M)) {
9550d1bd
RH
12846 if (arm_feature(env, ARM_FEATURE_M_SECURITY) &&
12847 FIELD_EX32(env->v7m.fpccr[M_REG_S], V7M_FPCCR, S)
12848 != env->v7m.secure) {
79cabf1f 12849 flags = FIELD_DP32(flags, TBFLAG_M32, FPCCR_S_WRONG, 1);
9550d1bd
RH
12850 }
12851
12852 if ((env->v7m.fpccr[env->v7m.secure] & R_V7M_FPCCR_ASPEN_MASK) &&
12853 (!(env->v7m.control[M_REG_S] & R_V7M_CONTROL_FPCA_MASK) ||
12854 (env->v7m.secure &&
12855 !(env->v7m.control[M_REG_S] & R_V7M_CONTROL_SFPA_MASK)))) {
12856 /*
12857 * ASPEN is set, but FPCA/SFPA indicate that there is no
12858 * active FP context; we must create a new FP context before
12859 * executing any FP insn.
12860 */
79cabf1f 12861 flags = FIELD_DP32(flags, TBFLAG_M32, NEW_FP_CTXT_NEEDED, 1);
9550d1bd
RH
12862 }
12863
12864 bool is_secure = env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_S_MASK;
12865 if (env->v7m.fpccr[is_secure] & R_V7M_FPCCR_LSPACT_MASK) {
79cabf1f 12866 flags = FIELD_DP32(flags, TBFLAG_M32, LSPACT, 1);
9550d1bd 12867 }
6e33ced5 12868 } else {
bbad7c62
RH
12869 /*
12870 * Note that XSCALE_CPAR shares bits with VECSTRIDE.
12871 * Note that VECLEN+VECSTRIDE are RES0 for M-profile.
12872 */
12873 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
12874 flags = FIELD_DP32(flags, TBFLAG_A32,
12875 XSCALE_CPAR, env->cp15.c15_cpar);
12876 } else {
12877 flags = FIELD_DP32(flags, TBFLAG_A32, VECLEN,
12878 env->vfp.vec_len);
12879 flags = FIELD_DP32(flags, TBFLAG_A32, VECSTRIDE,
12880 env->vfp.vec_stride);
12881 }
0a54d68e
RH
12882 if (env->vfp.xregs[ARM_VFP_FPEXC] & (1 << 30)) {
12883 flags = FIELD_DP32(flags, TBFLAG_A32, VFPEN, 1);
12884 }
6e33ced5
RH
12885 }
12886
79cabf1f
RH
12887 flags = FIELD_DP32(flags, TBFLAG_AM32, THUMB, env->thumb);
12888 flags = FIELD_DP32(flags, TBFLAG_AM32, CONDEXEC, env->condexec_bits);
60e12c37 12889 pstate_for_ss = env->uncached_cpsr;
d4d7503a 12890 }
a9e01311 12891
60e12c37
RH
12892 /*
12893 * The SS_ACTIVE and PSTATE_SS bits correspond to the state machine
a9e01311
RH
12894 * states defined in the ARM ARM for software singlestep:
12895 * SS_ACTIVE PSTATE.SS State
12896 * 0 x Inactive (the TB flag for SS is always 0)
12897 * 1 0 Active-pending
12898 * 1 1 Active-not-pending
fdd1b228 12899 * SS_ACTIVE is set in hflags; PSTATE_SS is computed every TB.
a9e01311 12900 */
60e12c37
RH
12901 if (FIELD_EX32(flags, TBFLAG_ANY, SS_ACTIVE) &&
12902 (pstate_for_ss & PSTATE_SS)) {
12903 flags = FIELD_DP32(flags, TBFLAG_ANY, PSTATE_SS, 1);
a9e01311 12904 }
a9e01311 12905
b9adaa70 12906 *pflags = flags;
a9e01311 12907}
0ab5953b
RH
12908
12909#ifdef TARGET_AARCH64
12910/*
12911 * The manual says that when SVE is enabled and VQ is widened the
12912 * implementation is allowed to zero the previously inaccessible
12913 * portion of the registers. The corollary to that is that when
12914 * SVE is enabled and VQ is narrowed we are also allowed to zero
12915 * the now inaccessible portion of the registers.
12916 *
12917 * The intent of this is that no predicate bit beyond VQ is ever set.
12918 * Which means that some operations on predicate registers themselves
12919 * may operate on full uint64_t or even unrolled across the maximum
12920 * uint64_t[4]. Performing 4 bits of host arithmetic unconditionally
12921 * may well be cheaper than conditionals to restrict the operation
12922 * to the relevant portion of a uint16_t[16].
12923 */
12924void aarch64_sve_narrow_vq(CPUARMState *env, unsigned vq)
12925{
12926 int i, j;
12927 uint64_t pmask;
12928
12929 assert(vq >= 1 && vq <= ARM_MAX_VQ);
2fc0cc0e 12930 assert(vq <= env_archcpu(env)->sve_max_vq);
0ab5953b
RH
12931
12932 /* Zap the high bits of the zregs. */
12933 for (i = 0; i < 32; i++) {
12934 memset(&env->vfp.zregs[i].d[2 * vq], 0, 16 * (ARM_MAX_VQ - vq));
12935 }
12936
12937 /* Zap the high bits of the pregs and ffr. */
12938 pmask = 0;
12939 if (vq & 3) {
12940 pmask = ~(-1ULL << (16 * (vq & 3)));
12941 }
12942 for (j = vq / 4; j < ARM_MAX_VQ / 4; j++) {
12943 for (i = 0; i < 17; ++i) {
12944 env->vfp.pregs[i].p[j] &= pmask;
12945 }
12946 pmask = 0;
12947 }
12948}
12949
12950/*
12951 * Notice a change in SVE vector size when changing EL.
12952 */
9a05f7b6
RH
12953void aarch64_sve_change_el(CPUARMState *env, int old_el,
12954 int new_el, bool el0_a64)
0ab5953b 12955{
2fc0cc0e 12956 ARMCPU *cpu = env_archcpu(env);
0ab5953b 12957 int old_len, new_len;
9a05f7b6 12958 bool old_a64, new_a64;
0ab5953b
RH
12959
12960 /* Nothing to do if no SVE. */
cd208a1c 12961 if (!cpu_isar_feature(aa64_sve, cpu)) {
0ab5953b
RH
12962 return;
12963 }
12964
12965 /* Nothing to do if FP is disabled in either EL. */
12966 if (fp_exception_el(env, old_el) || fp_exception_el(env, new_el)) {
12967 return;
12968 }
12969
12970 /*
12971 * DDI0584A.d sec 3.2: "If SVE instructions are disabled or trapped
12972 * at ELx, or not available because the EL is in AArch32 state, then
12973 * for all purposes other than a direct read, the ZCR_ELx.LEN field
12974 * has an effective value of 0".
12975 *
12976 * Consider EL2 (aa64, vq=4) -> EL0 (aa32) -> EL1 (aa64, vq=0).
12977 * If we ignore aa32 state, we would fail to see the vq4->vq0 transition
12978 * from EL2->EL1. Thus we go ahead and narrow when entering aa32 so that
12979 * we already have the correct register contents when encountering the
12980 * vq0->vq0 transition between EL0->EL1.
12981 */
9a05f7b6
RH
12982 old_a64 = old_el ? arm_el_is_aa64(env, old_el) : el0_a64;
12983 old_len = (old_a64 && !sve_exception_el(env, old_el)
0ab5953b 12984 ? sve_zcr_len_for_el(env, old_el) : 0);
9a05f7b6
RH
12985 new_a64 = new_el ? arm_el_is_aa64(env, new_el) : el0_a64;
12986 new_len = (new_a64 && !sve_exception_el(env, new_el)
0ab5953b
RH
12987 ? sve_zcr_len_for_el(env, new_el) : 0);
12988
12989 /* When changing vector length, clear inaccessible state. */
12990 if (new_len < old_len) {
12991 aarch64_sve_narrow_vq(env, new_len + 1);
12992 }
12993}
12994#endif