]> git.proxmox.com Git - mirror_qemu.git/blame - target/arm/helper.c
target/arm: Don't use a TLB for ARMMMUIdx_Stage2
[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,
37785977 44 hwaddr *phys_ptr, MemTxAttrs *txattrs, int *prot,
da909b2c 45 target_ulong *page_size_ptr,
5b2d261d 46 ARMMMUFaultInfo *fi, ARMCacheAttrs *cacheattrs);
4a501606
PM
47#endif
48
affdb64d
PM
49static void switch_mode(CPUARMState *env, int mode);
50
a010bdbe 51static int vfp_gdb_get_reg(CPUARMState *env, GByteArray *buf, int reg)
56aebc89 52{
a6627f5f
RH
53 ARMCPU *cpu = env_archcpu(env);
54 int nregs = cpu_isar_feature(aa32_simd_r32, cpu) ? 32 : 16;
56aebc89
PB
55
56 /* VFP data registers are always little-endian. */
56aebc89 57 if (reg < nregs) {
a010bdbe 58 return gdb_get_reg64(buf, *aa32_vfp_dreg(env, reg));
56aebc89
PB
59 }
60 if (arm_feature(env, ARM_FEATURE_NEON)) {
61 /* Aliases for Q regs. */
62 nregs += 16;
63 if (reg < nregs) {
9a2b5256 64 uint64_t *q = aa32_vfp_qreg(env, reg - 32);
a010bdbe 65 return gdb_get_reg128(buf, q[0], q[1]);
56aebc89
PB
66 }
67 }
68 switch (reg - nregs) {
a010bdbe
AB
69 case 0: return gdb_get_reg32(buf, env->vfp.xregs[ARM_VFP_FPSID]); break;
70 case 1: return gdb_get_reg32(buf, vfp_get_fpscr(env)); break;
71 case 2: return gdb_get_reg32(buf, env->vfp.xregs[ARM_VFP_FPEXC]); break;
56aebc89
PB
72 }
73 return 0;
74}
75
0ecb72a5 76static int vfp_gdb_set_reg(CPUARMState *env, uint8_t *buf, int reg)
56aebc89 77{
a6627f5f
RH
78 ARMCPU *cpu = env_archcpu(env);
79 int nregs = cpu_isar_feature(aa32_simd_r32, cpu) ? 32 : 16;
56aebc89 80
56aebc89 81 if (reg < nregs) {
9a2b5256 82 *aa32_vfp_dreg(env, reg) = ldq_le_p(buf);
56aebc89
PB
83 return 8;
84 }
85 if (arm_feature(env, ARM_FEATURE_NEON)) {
86 nregs += 16;
87 if (reg < nregs) {
9a2b5256
RH
88 uint64_t *q = aa32_vfp_qreg(env, reg - 32);
89 q[0] = ldq_le_p(buf);
90 q[1] = ldq_le_p(buf + 8);
56aebc89
PB
91 return 16;
92 }
93 }
94 switch (reg - nregs) {
95 case 0: env->vfp.xregs[ARM_VFP_FPSID] = ldl_p(buf); return 4;
b0a909a4 96 case 1: vfp_set_fpscr(env, ldl_p(buf)); return 4;
71b3c3de 97 case 2: env->vfp.xregs[ARM_VFP_FPEXC] = ldl_p(buf) & (1 << 30); return 4;
56aebc89
PB
98 }
99 return 0;
100}
101
a010bdbe 102static int aarch64_fpu_gdb_get_reg(CPUARMState *env, GByteArray *buf, int reg)
6a669427
PM
103{
104 switch (reg) {
105 case 0 ... 31:
8b1ca58c
AB
106 {
107 /* 128 bit FP register - quads are in LE order */
108 uint64_t *q = aa64_vfp_qreg(env, reg);
109 return gdb_get_reg128(buf, q[1], q[0]);
110 }
6a669427
PM
111 case 32:
112 /* FPSR */
8b1ca58c 113 return gdb_get_reg32(buf, vfp_get_fpsr(env));
6a669427
PM
114 case 33:
115 /* FPCR */
8b1ca58c 116 return gdb_get_reg32(buf,vfp_get_fpcr(env));
6a669427
PM
117 default:
118 return 0;
119 }
120}
121
122static int aarch64_fpu_gdb_set_reg(CPUARMState *env, uint8_t *buf, int reg)
123{
124 switch (reg) {
125 case 0 ... 31:
126 /* 128 bit FP register */
9a2b5256
RH
127 {
128 uint64_t *q = aa64_vfp_qreg(env, reg);
129 q[0] = ldq_le_p(buf);
130 q[1] = ldq_le_p(buf + 8);
131 return 16;
132 }
6a669427
PM
133 case 32:
134 /* FPSR */
135 vfp_set_fpsr(env, ldl_p(buf));
136 return 4;
137 case 33:
138 /* FPCR */
139 vfp_set_fpcr(env, ldl_p(buf));
140 return 4;
141 default:
142 return 0;
143 }
144}
145
c4241c7d 146static uint64_t raw_read(CPUARMState *env, const ARMCPRegInfo *ri)
d4e6df63 147{
375421cc 148 assert(ri->fieldoffset);
67ed771d 149 if (cpreg_field_is_64bit(ri)) {
c4241c7d 150 return CPREG_FIELD64(env, ri);
22d9e1a9 151 } else {
c4241c7d 152 return CPREG_FIELD32(env, ri);
22d9e1a9 153 }
d4e6df63
PM
154}
155
c4241c7d
PM
156static void raw_write(CPUARMState *env, const ARMCPRegInfo *ri,
157 uint64_t value)
d4e6df63 158{
375421cc 159 assert(ri->fieldoffset);
67ed771d 160 if (cpreg_field_is_64bit(ri)) {
22d9e1a9
PM
161 CPREG_FIELD64(env, ri) = value;
162 } else {
163 CPREG_FIELD32(env, ri) = value;
164 }
d4e6df63
PM
165}
166
11f136ee
FA
167static void *raw_ptr(CPUARMState *env, const ARMCPRegInfo *ri)
168{
169 return (char *)env + ri->fieldoffset;
170}
171
49a66191 172uint64_t read_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri)
721fae12 173{
59a1c327 174 /* Raw read of a coprocessor register (as needed for migration, etc). */
721fae12 175 if (ri->type & ARM_CP_CONST) {
59a1c327 176 return ri->resetvalue;
721fae12 177 } else if (ri->raw_readfn) {
59a1c327 178 return ri->raw_readfn(env, ri);
721fae12 179 } else if (ri->readfn) {
59a1c327 180 return ri->readfn(env, ri);
721fae12 181 } else {
59a1c327 182 return raw_read(env, ri);
721fae12 183 }
721fae12
PM
184}
185
59a1c327 186static void write_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri,
7900e9f1 187 uint64_t v)
721fae12
PM
188{
189 /* Raw write of a coprocessor register (as needed for migration, etc).
721fae12
PM
190 * Note that constant registers are treated as write-ignored; the
191 * caller should check for success by whether a readback gives the
192 * value written.
193 */
194 if (ri->type & ARM_CP_CONST) {
59a1c327 195 return;
721fae12 196 } else if (ri->raw_writefn) {
c4241c7d 197 ri->raw_writefn(env, ri, v);
721fae12 198 } else if (ri->writefn) {
c4241c7d 199 ri->writefn(env, ri, v);
721fae12 200 } else {
afb2530f 201 raw_write(env, ri, v);
721fae12 202 }
721fae12
PM
203}
204
d12379c5
AB
205/**
206 * arm_get/set_gdb_*: get/set a gdb register
207 * @env: the CPU state
208 * @buf: a buffer to copy to/from
209 * @reg: register number (offset from start of group)
210 *
211 * We return the number of bytes copied
212 */
213
a010bdbe 214static int arm_gdb_get_sysreg(CPUARMState *env, GByteArray *buf, int reg)
200bf5b7 215{
2fc0cc0e 216 ARMCPU *cpu = env_archcpu(env);
200bf5b7
AB
217 const ARMCPRegInfo *ri;
218 uint32_t key;
219
448d4d14 220 key = cpu->dyn_sysreg_xml.data.cpregs.keys[reg];
200bf5b7
AB
221 ri = get_arm_cp_reginfo(cpu->cp_regs, key);
222 if (ri) {
223 if (cpreg_field_is_64bit(ri)) {
224 return gdb_get_reg64(buf, (uint64_t)read_raw_cp_reg(env, ri));
225 } else {
226 return gdb_get_reg32(buf, (uint32_t)read_raw_cp_reg(env, ri));
227 }
228 }
229 return 0;
230}
231
232static int arm_gdb_set_sysreg(CPUARMState *env, uint8_t *buf, int reg)
233{
234 return 0;
235}
236
d12379c5
AB
237#ifdef TARGET_AARCH64
238static int arm_gdb_get_svereg(CPUARMState *env, GByteArray *buf, int reg)
239{
240 ARMCPU *cpu = env_archcpu(env);
241
242 switch (reg) {
243 /* The first 32 registers are the zregs */
244 case 0 ... 31:
245 {
246 int vq, len = 0;
247 for (vq = 0; vq < cpu->sve_max_vq; vq++) {
248 len += gdb_get_reg128(buf,
249 env->vfp.zregs[reg].d[vq * 2 + 1],
250 env->vfp.zregs[reg].d[vq * 2]);
251 }
252 return len;
253 }
254 case 32:
255 return gdb_get_reg32(buf, vfp_get_fpsr(env));
256 case 33:
257 return gdb_get_reg32(buf, vfp_get_fpcr(env));
258 /* then 16 predicates and the ffr */
259 case 34 ... 50:
260 {
261 int preg = reg - 34;
262 int vq, len = 0;
263 for (vq = 0; vq < cpu->sve_max_vq; vq = vq + 4) {
264 len += gdb_get_reg64(buf, env->vfp.pregs[preg].p[vq / 4]);
265 }
266 return len;
267 }
268 case 51:
269 {
270 /*
271 * We report in Vector Granules (VG) which is 64bit in a Z reg
272 * while the ZCR works in Vector Quads (VQ) which is 128bit chunks.
273 */
274 int vq = sve_zcr_len_for_el(env, arm_current_el(env)) + 1;
275 return gdb_get_reg32(buf, vq * 2);
276 }
277 default:
278 /* gdbstub asked for something out our range */
279 qemu_log_mask(LOG_UNIMP, "%s: out of range register %d", __func__, reg);
280 break;
281 }
282
283 return 0;
284}
285
286static int arm_gdb_set_svereg(CPUARMState *env, uint8_t *buf, int reg)
287{
288 ARMCPU *cpu = env_archcpu(env);
289
290 /* The first 32 registers are the zregs */
291 switch (reg) {
292 /* The first 32 registers are the zregs */
293 case 0 ... 31:
294 {
295 int vq, len = 0;
296 uint64_t *p = (uint64_t *) buf;
297 for (vq = 0; vq < cpu->sve_max_vq; vq++) {
298 env->vfp.zregs[reg].d[vq * 2 + 1] = *p++;
299 env->vfp.zregs[reg].d[vq * 2] = *p++;
300 len += 16;
301 }
302 return len;
303 }
304 case 32:
305 vfp_set_fpsr(env, *(uint32_t *)buf);
306 return 4;
307 case 33:
308 vfp_set_fpcr(env, *(uint32_t *)buf);
309 return 4;
310 case 34 ... 50:
311 {
312 int preg = reg - 34;
313 int vq, len = 0;
314 uint64_t *p = (uint64_t *) buf;
315 for (vq = 0; vq < cpu->sve_max_vq; vq = vq + 4) {
316 env->vfp.pregs[preg].p[vq / 4] = *p++;
317 len += 8;
318 }
319 return len;
320 }
321 case 51:
322 /* cannot set vg via gdbstub */
323 return 0;
324 default:
325 /* gdbstub asked for something out our range */
326 break;
327 }
328
329 return 0;
330}
331#endif /* TARGET_AARCH64 */
332
375421cc
PM
333static bool raw_accessors_invalid(const ARMCPRegInfo *ri)
334{
335 /* Return true if the regdef would cause an assertion if you called
336 * read_raw_cp_reg() or write_raw_cp_reg() on it (ie if it is a
337 * program bug for it not to have the NO_RAW flag).
338 * NB that returning false here doesn't necessarily mean that calling
339 * read/write_raw_cp_reg() is safe, because we can't distinguish "has
340 * read/write access functions which are safe for raw use" from "has
341 * read/write access functions which have side effects but has forgotten
342 * to provide raw access functions".
343 * The tests here line up with the conditions in read/write_raw_cp_reg()
344 * and assertions in raw_read()/raw_write().
345 */
346 if ((ri->type & ARM_CP_CONST) ||
347 ri->fieldoffset ||
348 ((ri->raw_writefn || ri->writefn) && (ri->raw_readfn || ri->readfn))) {
349 return false;
350 }
351 return true;
352}
353
b698e4ee 354bool write_cpustate_to_list(ARMCPU *cpu, bool kvm_sync)
721fae12
PM
355{
356 /* Write the coprocessor state from cpu->env to the (index,value) list. */
357 int i;
358 bool ok = true;
359
360 for (i = 0; i < cpu->cpreg_array_len; i++) {
361 uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
362 const ARMCPRegInfo *ri;
b698e4ee 363 uint64_t newval;
59a1c327 364
60322b39 365 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
721fae12
PM
366 if (!ri) {
367 ok = false;
368 continue;
369 }
7a0e58fa 370 if (ri->type & ARM_CP_NO_RAW) {
721fae12
PM
371 continue;
372 }
b698e4ee
PM
373
374 newval = read_raw_cp_reg(&cpu->env, ri);
375 if (kvm_sync) {
376 /*
377 * Only sync if the previous list->cpustate sync succeeded.
378 * Rather than tracking the success/failure state for every
379 * item in the list, we just recheck "does the raw write we must
380 * have made in write_list_to_cpustate() read back OK" here.
381 */
382 uint64_t oldval = cpu->cpreg_values[i];
383
384 if (oldval == newval) {
385 continue;
386 }
387
388 write_raw_cp_reg(&cpu->env, ri, oldval);
389 if (read_raw_cp_reg(&cpu->env, ri) != oldval) {
390 continue;
391 }
392
393 write_raw_cp_reg(&cpu->env, ri, newval);
394 }
395 cpu->cpreg_values[i] = newval;
721fae12
PM
396 }
397 return ok;
398}
399
400bool write_list_to_cpustate(ARMCPU *cpu)
401{
402 int i;
403 bool ok = true;
404
405 for (i = 0; i < cpu->cpreg_array_len; i++) {
406 uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
407 uint64_t v = cpu->cpreg_values[i];
721fae12
PM
408 const ARMCPRegInfo *ri;
409
60322b39 410 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
721fae12
PM
411 if (!ri) {
412 ok = false;
413 continue;
414 }
7a0e58fa 415 if (ri->type & ARM_CP_NO_RAW) {
721fae12
PM
416 continue;
417 }
418 /* Write value and confirm it reads back as written
419 * (to catch read-only registers and partially read-only
420 * registers where the incoming migration value doesn't match)
421 */
59a1c327
PM
422 write_raw_cp_reg(&cpu->env, ri, v);
423 if (read_raw_cp_reg(&cpu->env, ri) != v) {
721fae12
PM
424 ok = false;
425 }
426 }
427 return ok;
428}
429
430static void add_cpreg_to_list(gpointer key, gpointer opaque)
431{
432 ARMCPU *cpu = opaque;
433 uint64_t regidx;
434 const ARMCPRegInfo *ri;
435
436 regidx = *(uint32_t *)key;
60322b39 437 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
721fae12 438
7a0e58fa 439 if (!(ri->type & (ARM_CP_NO_RAW|ARM_CP_ALIAS))) {
721fae12
PM
440 cpu->cpreg_indexes[cpu->cpreg_array_len] = cpreg_to_kvm_id(regidx);
441 /* The value array need not be initialized at this point */
442 cpu->cpreg_array_len++;
443 }
444}
445
446static void count_cpreg(gpointer key, gpointer opaque)
447{
448 ARMCPU *cpu = opaque;
449 uint64_t regidx;
450 const ARMCPRegInfo *ri;
451
452 regidx = *(uint32_t *)key;
60322b39 453 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
721fae12 454
7a0e58fa 455 if (!(ri->type & (ARM_CP_NO_RAW|ARM_CP_ALIAS))) {
721fae12
PM
456 cpu->cpreg_array_len++;
457 }
458}
459
460static gint cpreg_key_compare(gconstpointer a, gconstpointer b)
461{
cbf239b7
AR
462 uint64_t aidx = cpreg_to_kvm_id(*(uint32_t *)a);
463 uint64_t bidx = cpreg_to_kvm_id(*(uint32_t *)b);
721fae12 464
cbf239b7
AR
465 if (aidx > bidx) {
466 return 1;
467 }
468 if (aidx < bidx) {
469 return -1;
470 }
471 return 0;
721fae12
PM
472}
473
474void init_cpreg_list(ARMCPU *cpu)
475{
476 /* Initialise the cpreg_tuples[] array based on the cp_regs hash.
477 * Note that we require cpreg_tuples[] to be sorted by key ID.
478 */
57b6d95e 479 GList *keys;
721fae12
PM
480 int arraylen;
481
57b6d95e 482 keys = g_hash_table_get_keys(cpu->cp_regs);
721fae12
PM
483 keys = g_list_sort(keys, cpreg_key_compare);
484
485 cpu->cpreg_array_len = 0;
486
487 g_list_foreach(keys, count_cpreg, cpu);
488
489 arraylen = cpu->cpreg_array_len;
490 cpu->cpreg_indexes = g_new(uint64_t, arraylen);
491 cpu->cpreg_values = g_new(uint64_t, arraylen);
492 cpu->cpreg_vmstate_indexes = g_new(uint64_t, arraylen);
493 cpu->cpreg_vmstate_values = g_new(uint64_t, arraylen);
494 cpu->cpreg_vmstate_array_len = cpu->cpreg_array_len;
495 cpu->cpreg_array_len = 0;
496
497 g_list_foreach(keys, add_cpreg_to_list, cpu);
498
499 assert(cpu->cpreg_array_len == arraylen);
500
501 g_list_free(keys);
502}
503
68e9c2fe
EI
504/*
505 * Some registers are not accessible if EL3.NS=0 and EL3 is using AArch32 but
506 * they are accessible when EL3 is using AArch64 regardless of EL3.NS.
507 *
508 * access_el3_aa32ns: Used to check AArch32 register views.
509 * access_el3_aa32ns_aa64any: Used to check both AArch32/64 register views.
510 */
511static CPAccessResult access_el3_aa32ns(CPUARMState *env,
3f208fd7
PM
512 const ARMCPRegInfo *ri,
513 bool isread)
68e9c2fe
EI
514{
515 bool secure = arm_is_secure_below_el3(env);
516
517 assert(!arm_el_is_aa64(env, 3));
518 if (secure) {
519 return CP_ACCESS_TRAP_UNCATEGORIZED;
520 }
521 return CP_ACCESS_OK;
522}
523
524static CPAccessResult access_el3_aa32ns_aa64any(CPUARMState *env,
3f208fd7
PM
525 const ARMCPRegInfo *ri,
526 bool isread)
68e9c2fe
EI
527{
528 if (!arm_el_is_aa64(env, 3)) {
3f208fd7 529 return access_el3_aa32ns(env, ri, isread);
68e9c2fe
EI
530 }
531 return CP_ACCESS_OK;
532}
533
5513c3ab
PM
534/* Some secure-only AArch32 registers trap to EL3 if used from
535 * Secure EL1 (but are just ordinary UNDEF in other non-EL3 contexts).
536 * Note that an access from Secure EL1 can only happen if EL3 is AArch64.
537 * We assume that the .access field is set to PL1_RW.
538 */
539static CPAccessResult access_trap_aa32s_el1(CPUARMState *env,
3f208fd7
PM
540 const ARMCPRegInfo *ri,
541 bool isread)
5513c3ab
PM
542{
543 if (arm_current_el(env) == 3) {
544 return CP_ACCESS_OK;
545 }
546 if (arm_is_secure_below_el3(env)) {
547 return CP_ACCESS_TRAP_EL3;
548 }
549 /* This will be EL1 NS and EL2 NS, which just UNDEF */
550 return CP_ACCESS_TRAP_UNCATEGORIZED;
551}
552
187f678d
PM
553/* Check for traps to "powerdown debug" registers, which are controlled
554 * by MDCR.TDOSA
555 */
556static CPAccessResult access_tdosa(CPUARMState *env, const ARMCPRegInfo *ri,
557 bool isread)
558{
559 int el = arm_current_el(env);
30ac6339
PM
560 bool mdcr_el2_tdosa = (env->cp15.mdcr_el2 & MDCR_TDOSA) ||
561 (env->cp15.mdcr_el2 & MDCR_TDE) ||
7c208e0f 562 (arm_hcr_el2_eff(env) & HCR_TGE);
187f678d 563
30ac6339 564 if (el < 2 && mdcr_el2_tdosa && !arm_is_secure_below_el3(env)) {
187f678d
PM
565 return CP_ACCESS_TRAP_EL2;
566 }
567 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TDOSA)) {
568 return CP_ACCESS_TRAP_EL3;
569 }
570 return CP_ACCESS_OK;
571}
572
91b0a238
PM
573/* Check for traps to "debug ROM" registers, which are controlled
574 * by MDCR_EL2.TDRA for EL2 but by the more general MDCR_EL3.TDA for EL3.
575 */
576static CPAccessResult access_tdra(CPUARMState *env, const ARMCPRegInfo *ri,
577 bool isread)
578{
579 int el = arm_current_el(env);
30ac6339
PM
580 bool mdcr_el2_tdra = (env->cp15.mdcr_el2 & MDCR_TDRA) ||
581 (env->cp15.mdcr_el2 & MDCR_TDE) ||
7c208e0f 582 (arm_hcr_el2_eff(env) & HCR_TGE);
91b0a238 583
30ac6339 584 if (el < 2 && mdcr_el2_tdra && !arm_is_secure_below_el3(env)) {
91b0a238
PM
585 return CP_ACCESS_TRAP_EL2;
586 }
587 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TDA)) {
588 return CP_ACCESS_TRAP_EL3;
589 }
590 return CP_ACCESS_OK;
591}
592
d6c8cf81
PM
593/* Check for traps to general debug registers, which are controlled
594 * by MDCR_EL2.TDA for EL2 and MDCR_EL3.TDA for EL3.
595 */
596static CPAccessResult access_tda(CPUARMState *env, const ARMCPRegInfo *ri,
597 bool isread)
598{
599 int el = arm_current_el(env);
30ac6339
PM
600 bool mdcr_el2_tda = (env->cp15.mdcr_el2 & MDCR_TDA) ||
601 (env->cp15.mdcr_el2 & MDCR_TDE) ||
7c208e0f 602 (arm_hcr_el2_eff(env) & HCR_TGE);
d6c8cf81 603
30ac6339 604 if (el < 2 && mdcr_el2_tda && !arm_is_secure_below_el3(env)) {
d6c8cf81
PM
605 return CP_ACCESS_TRAP_EL2;
606 }
607 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TDA)) {
608 return CP_ACCESS_TRAP_EL3;
609 }
610 return CP_ACCESS_OK;
611}
612
1fce1ba9
PM
613/* Check for traps to performance monitor registers, which are controlled
614 * by MDCR_EL2.TPM for EL2 and MDCR_EL3.TPM for EL3.
615 */
616static CPAccessResult access_tpm(CPUARMState *env, const ARMCPRegInfo *ri,
617 bool isread)
618{
619 int el = arm_current_el(env);
620
621 if (el < 2 && (env->cp15.mdcr_el2 & MDCR_TPM)
622 && !arm_is_secure_below_el3(env)) {
623 return CP_ACCESS_TRAP_EL2;
624 }
625 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) {
626 return CP_ACCESS_TRAP_EL3;
627 }
628 return CP_ACCESS_OK;
629}
630
84929218
RH
631/* Check for traps from EL1 due to HCR_EL2.TVM and HCR_EL2.TRVM. */
632static CPAccessResult access_tvm_trvm(CPUARMState *env, const ARMCPRegInfo *ri,
633 bool isread)
634{
635 if (arm_current_el(env) == 1) {
636 uint64_t trap = isread ? HCR_TRVM : HCR_TVM;
637 if (arm_hcr_el2_eff(env) & trap) {
638 return CP_ACCESS_TRAP_EL2;
639 }
640 }
641 return CP_ACCESS_OK;
642}
643
1803d271
RH
644/* Check for traps from EL1 due to HCR_EL2.TSW. */
645static CPAccessResult access_tsw(CPUARMState *env, const ARMCPRegInfo *ri,
646 bool isread)
647{
648 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TSW)) {
649 return CP_ACCESS_TRAP_EL2;
650 }
651 return CP_ACCESS_OK;
652}
653
99602377
RH
654/* Check for traps from EL1 due to HCR_EL2.TACR. */
655static CPAccessResult access_tacr(CPUARMState *env, const ARMCPRegInfo *ri,
656 bool isread)
657{
658 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TACR)) {
659 return CP_ACCESS_TRAP_EL2;
660 }
661 return CP_ACCESS_OK;
662}
663
30881b73
RH
664/* Check for traps from EL1 due to HCR_EL2.TTLB. */
665static CPAccessResult access_ttlb(CPUARMState *env, const ARMCPRegInfo *ri,
666 bool isread)
667{
668 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TTLB)) {
669 return CP_ACCESS_TRAP_EL2;
670 }
671 return CP_ACCESS_OK;
672}
673
c4241c7d 674static void dacr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
c983fe6c 675{
2fc0cc0e 676 ARMCPU *cpu = env_archcpu(env);
00c8cb0a 677
8d5c773e 678 raw_write(env, ri, value);
d10eb08f 679 tlb_flush(CPU(cpu)); /* Flush TLB as domain not tracked in TLB */
c983fe6c
PM
680}
681
c4241c7d 682static void fcse_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
08de207b 683{
2fc0cc0e 684 ARMCPU *cpu = env_archcpu(env);
00c8cb0a 685
8d5c773e 686 if (raw_read(env, ri) != value) {
08de207b
PM
687 /* Unlike real hardware the qemu TLB uses virtual addresses,
688 * not modified virtual addresses, so this causes a TLB flush.
689 */
d10eb08f 690 tlb_flush(CPU(cpu));
8d5c773e 691 raw_write(env, ri, value);
08de207b 692 }
08de207b 693}
c4241c7d
PM
694
695static void contextidr_write(CPUARMState *env, const ARMCPRegInfo *ri,
696 uint64_t value)
08de207b 697{
2fc0cc0e 698 ARMCPU *cpu = env_archcpu(env);
00c8cb0a 699
452a0955 700 if (raw_read(env, ri) != value && !arm_feature(env, ARM_FEATURE_PMSA)
014406b5 701 && !extended_addresses_enabled(env)) {
08de207b
PM
702 /* For VMSA (when not using the LPAE long descriptor page table
703 * format) this register includes the ASID, so do a TLB flush.
704 * For PMSA it is purely a process ID and no action is needed.
705 */
d10eb08f 706 tlb_flush(CPU(cpu));
08de207b 707 }
8d5c773e 708 raw_write(env, ri, value);
08de207b
PM
709}
710
b4ab8ce9
PM
711/* IS variants of TLB operations must affect all cores */
712static void tlbiall_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
713 uint64_t value)
714{
29a0af61 715 CPUState *cs = env_cpu(env);
b4ab8ce9
PM
716
717 tlb_flush_all_cpus_synced(cs);
718}
719
720static void tlbiasid_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
721 uint64_t value)
722{
29a0af61 723 CPUState *cs = env_cpu(env);
b4ab8ce9
PM
724
725 tlb_flush_all_cpus_synced(cs);
726}
727
728static void tlbimva_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
729 uint64_t value)
730{
29a0af61 731 CPUState *cs = env_cpu(env);
b4ab8ce9
PM
732
733 tlb_flush_page_all_cpus_synced(cs, value & TARGET_PAGE_MASK);
734}
735
736static void tlbimvaa_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
737 uint64_t value)
738{
29a0af61 739 CPUState *cs = env_cpu(env);
b4ab8ce9
PM
740
741 tlb_flush_page_all_cpus_synced(cs, value & TARGET_PAGE_MASK);
742}
743
744/*
745 * Non-IS variants of TLB operations are upgraded to
746 * IS versions if we are at NS EL1 and HCR_EL2.FB is set to
747 * force broadcast of these operations.
748 */
749static bool tlb_force_broadcast(CPUARMState *env)
750{
751 return (env->cp15.hcr_el2 & HCR_FB) &&
752 arm_current_el(env) == 1 && arm_is_secure_below_el3(env);
753}
754
c4241c7d
PM
755static void tlbiall_write(CPUARMState *env, const ARMCPRegInfo *ri,
756 uint64_t value)
d929823f
PM
757{
758 /* Invalidate all (TLBIALL) */
527db2be 759 CPUState *cs = env_cpu(env);
00c8cb0a 760
b4ab8ce9 761 if (tlb_force_broadcast(env)) {
527db2be
RH
762 tlb_flush_all_cpus_synced(cs);
763 } else {
764 tlb_flush(cs);
b4ab8ce9 765 }
d929823f
PM
766}
767
c4241c7d
PM
768static void tlbimva_write(CPUARMState *env, const ARMCPRegInfo *ri,
769 uint64_t value)
d929823f
PM
770{
771 /* Invalidate single TLB entry by MVA and ASID (TLBIMVA) */
527db2be 772 CPUState *cs = env_cpu(env);
31b030d4 773
527db2be 774 value &= TARGET_PAGE_MASK;
b4ab8ce9 775 if (tlb_force_broadcast(env)) {
527db2be
RH
776 tlb_flush_page_all_cpus_synced(cs, value);
777 } else {
778 tlb_flush_page(cs, value);
b4ab8ce9 779 }
d929823f
PM
780}
781
c4241c7d
PM
782static void tlbiasid_write(CPUARMState *env, const ARMCPRegInfo *ri,
783 uint64_t value)
d929823f
PM
784{
785 /* Invalidate by ASID (TLBIASID) */
527db2be 786 CPUState *cs = env_cpu(env);
00c8cb0a 787
b4ab8ce9 788 if (tlb_force_broadcast(env)) {
527db2be
RH
789 tlb_flush_all_cpus_synced(cs);
790 } else {
791 tlb_flush(cs);
b4ab8ce9 792 }
d929823f
PM
793}
794
c4241c7d
PM
795static void tlbimvaa_write(CPUARMState *env, const ARMCPRegInfo *ri,
796 uint64_t value)
d929823f
PM
797{
798 /* Invalidate single entry by MVA, all ASIDs (TLBIMVAA) */
527db2be 799 CPUState *cs = env_cpu(env);
31b030d4 800
527db2be 801 value &= TARGET_PAGE_MASK;
b4ab8ce9 802 if (tlb_force_broadcast(env)) {
527db2be
RH
803 tlb_flush_page_all_cpus_synced(cs, value);
804 } else {
805 tlb_flush_page(cs, value);
b4ab8ce9 806 }
fa439fc5
PM
807}
808
541ef8c2
SS
809static void tlbiall_nsnh_write(CPUARMState *env, const ARMCPRegInfo *ri,
810 uint64_t value)
811{
29a0af61 812 CPUState *cs = env_cpu(env);
541ef8c2 813
0336cbf8 814 tlb_flush_by_mmuidx(cs,
01b98b68 815 ARMMMUIdxBit_E10_1 |
452ef8cb 816 ARMMMUIdxBit_E10_1_PAN |
bf05340c 817 ARMMMUIdxBit_E10_0);
541ef8c2
SS
818}
819
820static void tlbiall_nsnh_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
821 uint64_t value)
822{
29a0af61 823 CPUState *cs = env_cpu(env);
541ef8c2 824
a67cf277 825 tlb_flush_by_mmuidx_all_cpus_synced(cs,
01b98b68 826 ARMMMUIdxBit_E10_1 |
452ef8cb 827 ARMMMUIdxBit_E10_1_PAN |
bf05340c 828 ARMMMUIdxBit_E10_0);
541ef8c2
SS
829}
830
541ef8c2
SS
831
832static void tlbiall_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
833 uint64_t value)
834{
29a0af61 835 CPUState *cs = env_cpu(env);
541ef8c2 836
e013b741 837 tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_E2);
541ef8c2
SS
838}
839
840static void tlbiall_hyp_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
841 uint64_t value)
842{
29a0af61 843 CPUState *cs = env_cpu(env);
541ef8c2 844
e013b741 845 tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_E2);
541ef8c2
SS
846}
847
848static void tlbimva_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
849 uint64_t value)
850{
29a0af61 851 CPUState *cs = env_cpu(env);
541ef8c2
SS
852 uint64_t pageaddr = value & ~MAKE_64BIT_MASK(0, 12);
853
e013b741 854 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_E2);
541ef8c2
SS
855}
856
857static void tlbimva_hyp_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
858 uint64_t value)
859{
29a0af61 860 CPUState *cs = env_cpu(env);
541ef8c2
SS
861 uint64_t pageaddr = value & ~MAKE_64BIT_MASK(0, 12);
862
a67cf277 863 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
e013b741 864 ARMMMUIdxBit_E2);
541ef8c2
SS
865}
866
e9aa6c21 867static const ARMCPRegInfo cp_reginfo[] = {
54bf36ed
FA
868 /* Define the secure and non-secure FCSE 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. There is also no
871 * v8 EL1 version of the register so the non-secure instance stands alone.
872 */
9c513e78 873 { .name = "FCSEIDR",
54bf36ed
FA
874 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
875 .access = PL1_RW, .secure = ARM_CP_SECSTATE_NS,
876 .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_ns),
877 .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
9c513e78 878 { .name = "FCSEIDR_S",
54bf36ed
FA
879 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
880 .access = PL1_RW, .secure = ARM_CP_SECSTATE_S,
881 .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_s),
d4e6df63 882 .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
54bf36ed
FA
883 /* Define the secure and non-secure context identifier CP registers
884 * separately because there is no secure bank in V8 (no _EL3). This allows
885 * the secure register to be properly reset and migrated. In the
886 * non-secure case, the 32-bit register will have reset and migration
887 * disabled during registration as it is handled by the 64-bit instance.
888 */
889 { .name = "CONTEXTIDR_EL1", .state = ARM_CP_STATE_BOTH,
014406b5 890 .opc0 = 3, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
84929218
RH
891 .access = PL1_RW, .accessfn = access_tvm_trvm,
892 .secure = ARM_CP_SECSTATE_NS,
54bf36ed
FA
893 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[1]),
894 .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
9c513e78 895 { .name = "CONTEXTIDR_S", .state = ARM_CP_STATE_AA32,
54bf36ed 896 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
84929218
RH
897 .access = PL1_RW, .accessfn = access_tvm_trvm,
898 .secure = ARM_CP_SECSTATE_S,
54bf36ed 899 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_s),
d4e6df63 900 .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
9449fdf6
PM
901 REGINFO_SENTINEL
902};
903
904static const ARMCPRegInfo not_v8_cp_reginfo[] = {
905 /* NB: Some of these registers exist in v8 but with more precise
906 * definitions that don't use CP_ANY wildcards (mostly in v8_cp_reginfo[]).
907 */
908 /* MMU Domain access control / MPU write buffer control */
0c17d68c
FA
909 { .name = "DACR",
910 .cp = 15, .opc1 = CP_ANY, .crn = 3, .crm = CP_ANY, .opc2 = CP_ANY,
84929218 911 .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
0c17d68c
FA
912 .writefn = dacr_write, .raw_writefn = raw_write,
913 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
914 offsetoflow32(CPUARMState, cp15.dacr_ns) } },
a903c449
EI
915 /* ARMv7 allocates a range of implementation defined TLB LOCKDOWN regs.
916 * For v6 and v5, these mappings are overly broad.
4fdd17dd 917 */
a903c449
EI
918 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 0,
919 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
920 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 1,
921 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
922 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 4,
923 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
924 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 8,
4fdd17dd 925 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
c4804214
PM
926 /* Cache maintenance ops; some of this space may be overridden later. */
927 { .name = "CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
928 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
929 .type = ARM_CP_NOP | ARM_CP_OVERRIDE },
e9aa6c21
PM
930 REGINFO_SENTINEL
931};
932
7d57f408
PM
933static const ARMCPRegInfo not_v6_cp_reginfo[] = {
934 /* Not all pre-v6 cores implemented this WFI, so this is slightly
935 * over-broad.
936 */
937 { .name = "WFI_v5", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = 2,
938 .access = PL1_W, .type = ARM_CP_WFI },
939 REGINFO_SENTINEL
940};
941
942static const ARMCPRegInfo not_v7_cp_reginfo[] = {
943 /* Standard v6 WFI (also used in some pre-v6 cores); not in v7 (which
944 * is UNPREDICTABLE; we choose to NOP as most implementations do).
945 */
946 { .name = "WFI_v6", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
947 .access = PL1_W, .type = ARM_CP_WFI },
34f90529
PM
948 /* L1 cache lockdown. Not architectural in v6 and earlier but in practice
949 * implemented in 926, 946, 1026, 1136, 1176 and 11MPCore. StrongARM and
950 * OMAPCP will override this space.
951 */
952 { .name = "DLOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 0,
953 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_data),
954 .resetvalue = 0 },
955 { .name = "ILOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 1,
956 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_insn),
957 .resetvalue = 0 },
776d4e5c
PM
958 /* v6 doesn't have the cache ID registers but Linux reads them anyway */
959 { .name = "DUMMY", .cp = 15, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = CP_ANY,
7a0e58fa 960 .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 961 .resetvalue = 0 },
50300698
PM
962 /* We don't implement pre-v7 debug but most CPUs had at least a DBGDIDR;
963 * implementing it as RAZ means the "debug architecture version" bits
964 * will read as a reserved value, which should cause Linux to not try
965 * to use the debug hardware.
966 */
967 { .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0,
968 .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
995939a6
PM
969 /* MMU TLB control. Note that the wildcarding means we cover not just
970 * the unified TLB ops but also the dside/iside/inner-shareable variants.
971 */
972 { .name = "TLBIALL", .cp = 15, .crn = 8, .crm = CP_ANY,
973 .opc1 = CP_ANY, .opc2 = 0, .access = PL1_W, .writefn = tlbiall_write,
7a0e58fa 974 .type = ARM_CP_NO_RAW },
995939a6
PM
975 { .name = "TLBIMVA", .cp = 15, .crn = 8, .crm = CP_ANY,
976 .opc1 = CP_ANY, .opc2 = 1, .access = PL1_W, .writefn = tlbimva_write,
7a0e58fa 977 .type = ARM_CP_NO_RAW },
995939a6
PM
978 { .name = "TLBIASID", .cp = 15, .crn = 8, .crm = CP_ANY,
979 .opc1 = CP_ANY, .opc2 = 2, .access = PL1_W, .writefn = tlbiasid_write,
7a0e58fa 980 .type = ARM_CP_NO_RAW },
995939a6
PM
981 { .name = "TLBIMVAA", .cp = 15, .crn = 8, .crm = CP_ANY,
982 .opc1 = CP_ANY, .opc2 = 3, .access = PL1_W, .writefn = tlbimvaa_write,
7a0e58fa 983 .type = ARM_CP_NO_RAW },
a903c449
EI
984 { .name = "PRRR", .cp = 15, .crn = 10, .crm = 2,
985 .opc1 = 0, .opc2 = 0, .access = PL1_RW, .type = ARM_CP_NOP },
986 { .name = "NMRR", .cp = 15, .crn = 10, .crm = 2,
987 .opc1 = 0, .opc2 = 1, .access = PL1_RW, .type = ARM_CP_NOP },
7d57f408
PM
988 REGINFO_SENTINEL
989};
990
c4241c7d
PM
991static void cpacr_write(CPUARMState *env, const ARMCPRegInfo *ri,
992 uint64_t value)
2771db27 993{
f0aff255
FA
994 uint32_t mask = 0;
995
996 /* In ARMv8 most bits of CPACR_EL1 are RES0. */
997 if (!arm_feature(env, ARM_FEATURE_V8)) {
998 /* ARMv7 defines bits for unimplemented coprocessors as RAZ/WI.
999 * ASEDIS [31] and D32DIS [30] are both UNK/SBZP without VFP.
1000 * TRCDIS [28] is RAZ/WI since we do not implement a trace macrocell.
1001 */
7fbc6a40 1002 if (cpu_isar_feature(aa32_vfp_simd, env_archcpu(env))) {
f0aff255
FA
1003 /* VFP coprocessor: cp10 & cp11 [23:20] */
1004 mask |= (1 << 31) | (1 << 30) | (0xf << 20);
1005
1006 if (!arm_feature(env, ARM_FEATURE_NEON)) {
1007 /* ASEDIS [31] bit is RAO/WI */
1008 value |= (1 << 31);
1009 }
1010
1011 /* VFPv3 and upwards with NEON implement 32 double precision
1012 * registers (D0-D31).
1013 */
a6627f5f 1014 if (!cpu_isar_feature(aa32_simd_r32, env_archcpu(env))) {
f0aff255
FA
1015 /* D32DIS [30] is RAO/WI if D16-31 are not implemented. */
1016 value |= (1 << 30);
1017 }
1018 }
1019 value &= mask;
2771db27 1020 }
fc1120a7
PM
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 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
1027 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
1028 value &= ~(0xf << 20);
1029 value |= env->cp15.cpacr_el1 & (0xf << 20);
1030 }
1031
7ebd5f2e 1032 env->cp15.cpacr_el1 = value;
2771db27
PM
1033}
1034
fc1120a7
PM
1035static uint64_t cpacr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1036{
1037 /*
1038 * For A-profile AArch32 EL3 (but not M-profile secure mode), if NSACR.CP10
1039 * is 0 then CPACR.{CP11,CP10} ignore writes and read as 0b00.
1040 */
1041 uint64_t value = env->cp15.cpacr_el1;
1042
1043 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
1044 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
1045 value &= ~(0xf << 20);
1046 }
1047 return value;
1048}
1049
1050
5deac39c
PM
1051static void cpacr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1052{
1053 /* Call cpacr_write() so that we reset with the correct RAO bits set
1054 * for our CPU features.
1055 */
1056 cpacr_write(env, ri, 0);
1057}
1058
3f208fd7
PM
1059static CPAccessResult cpacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
1060 bool isread)
c6f19164
GB
1061{
1062 if (arm_feature(env, ARM_FEATURE_V8)) {
1063 /* Check if CPACR accesses are to be trapped to EL2 */
1064 if (arm_current_el(env) == 1 &&
1065 (env->cp15.cptr_el[2] & CPTR_TCPAC) && !arm_is_secure(env)) {
1066 return CP_ACCESS_TRAP_EL2;
1067 /* Check if CPACR accesses are to be trapped to EL3 */
1068 } else if (arm_current_el(env) < 3 &&
1069 (env->cp15.cptr_el[3] & CPTR_TCPAC)) {
1070 return CP_ACCESS_TRAP_EL3;
1071 }
1072 }
1073
1074 return CP_ACCESS_OK;
1075}
1076
3f208fd7
PM
1077static CPAccessResult cptr_access(CPUARMState *env, const ARMCPRegInfo *ri,
1078 bool isread)
c6f19164
GB
1079{
1080 /* Check if CPTR accesses are set to trap to EL3 */
1081 if (arm_current_el(env) == 2 && (env->cp15.cptr_el[3] & CPTR_TCPAC)) {
1082 return CP_ACCESS_TRAP_EL3;
1083 }
1084
1085 return CP_ACCESS_OK;
1086}
1087
7d57f408
PM
1088static const ARMCPRegInfo v6_cp_reginfo[] = {
1089 /* prefetch by MVA in v6, NOP in v7 */
1090 { .name = "MVA_prefetch",
1091 .cp = 15, .crn = 7, .crm = 13, .opc1 = 0, .opc2 = 1,
1092 .access = PL1_W, .type = ARM_CP_NOP },
6df99dec
SS
1093 /* We need to break the TB after ISB to execute self-modifying code
1094 * correctly and also to take any pending interrupts immediately.
1095 * So use arm_cp_write_ignore() function instead of ARM_CP_NOP flag.
1096 */
7d57f408 1097 { .name = "ISB", .cp = 15, .crn = 7, .crm = 5, .opc1 = 0, .opc2 = 4,
6df99dec 1098 .access = PL0_W, .type = ARM_CP_NO_RAW, .writefn = arm_cp_write_ignore },
091fd17c 1099 { .name = "DSB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 4,
7d57f408 1100 .access = PL0_W, .type = ARM_CP_NOP },
091fd17c 1101 { .name = "DMB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 5,
7d57f408 1102 .access = PL0_W, .type = ARM_CP_NOP },
06d76f31 1103 { .name = "IFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 2,
84929218 1104 .access = PL1_RW, .accessfn = access_tvm_trvm,
b848ce2b
FA
1105 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ifar_s),
1106 offsetof(CPUARMState, cp15.ifar_ns) },
06d76f31
PM
1107 .resetvalue = 0, },
1108 /* Watchpoint Fault Address Register : should actually only be present
1109 * for 1136, 1176, 11MPCore.
1110 */
1111 { .name = "WFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 1,
1112 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0, },
34222fb8 1113 { .name = "CPACR", .state = ARM_CP_STATE_BOTH, .opc0 = 3,
c6f19164 1114 .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 2, .accessfn = cpacr_access,
7ebd5f2e 1115 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.cpacr_el1),
fc1120a7 1116 .resetfn = cpacr_reset, .writefn = cpacr_write, .readfn = cpacr_read },
7d57f408
PM
1117 REGINFO_SENTINEL
1118};
1119
7ece99b1
AL
1120/* Definitions for the PMU registers */
1121#define PMCRN_MASK 0xf800
1122#define PMCRN_SHIFT 11
f4efb4b2 1123#define PMCRLC 0x40
a1ed04dd
PM
1124#define PMCRDP 0x20
1125#define PMCRX 0x10
7ece99b1
AL
1126#define PMCRD 0x8
1127#define PMCRC 0x4
5ecdd3e4 1128#define PMCRP 0x2
7ece99b1 1129#define PMCRE 0x1
62d96ff4
PM
1130/*
1131 * Mask of PMCR bits writeable by guest (not including WO bits like C, P,
1132 * which can be written as 1 to trigger behaviour but which stay RAZ).
1133 */
1134#define PMCR_WRITEABLE_MASK (PMCRLC | PMCRDP | PMCRX | PMCRD | PMCRE)
7ece99b1 1135
033614c4
AL
1136#define PMXEVTYPER_P 0x80000000
1137#define PMXEVTYPER_U 0x40000000
1138#define PMXEVTYPER_NSK 0x20000000
1139#define PMXEVTYPER_NSU 0x10000000
1140#define PMXEVTYPER_NSH 0x08000000
1141#define PMXEVTYPER_M 0x04000000
1142#define PMXEVTYPER_MT 0x02000000
1143#define PMXEVTYPER_EVTCOUNT 0x0000ffff
1144#define PMXEVTYPER_MASK (PMXEVTYPER_P | PMXEVTYPER_U | PMXEVTYPER_NSK | \
1145 PMXEVTYPER_NSU | PMXEVTYPER_NSH | \
1146 PMXEVTYPER_M | PMXEVTYPER_MT | \
1147 PMXEVTYPER_EVTCOUNT)
1148
4b8afa1f
AL
1149#define PMCCFILTR 0xf8000000
1150#define PMCCFILTR_M PMXEVTYPER_M
1151#define PMCCFILTR_EL0 (PMCCFILTR | PMCCFILTR_M)
1152
7ece99b1
AL
1153static inline uint32_t pmu_num_counters(CPUARMState *env)
1154{
1155 return (env->cp15.c9_pmcr & PMCRN_MASK) >> PMCRN_SHIFT;
1156}
1157
1158/* Bits allowed to be set/cleared for PMCNTEN* and PMINTEN* */
1159static inline uint64_t pmu_counter_mask(CPUARMState *env)
1160{
1161 return (1 << 31) | ((1 << pmu_num_counters(env)) - 1);
1162}
1163
57a4a11b
AL
1164typedef struct pm_event {
1165 uint16_t number; /* PMEVTYPER.evtCount is 16 bits wide */
1166 /* If the event is supported on this CPU (used to generate PMCEID[01]) */
1167 bool (*supported)(CPUARMState *);
1168 /*
1169 * Retrieve the current count of the underlying event. The programmed
1170 * counters hold a difference from the return value from this function
1171 */
1172 uint64_t (*get_count)(CPUARMState *);
4e7beb0c
AL
1173 /*
1174 * Return how many nanoseconds it will take (at a minimum) for count events
1175 * to occur. A negative value indicates the counter will never overflow, or
1176 * that the counter has otherwise arranged for the overflow bit to be set
1177 * and the PMU interrupt to be raised on overflow.
1178 */
1179 int64_t (*ns_per_count)(uint64_t);
57a4a11b
AL
1180} pm_event;
1181
b2e23725
AL
1182static bool event_always_supported(CPUARMState *env)
1183{
1184 return true;
1185}
1186
0d4bfd7d
AL
1187static uint64_t swinc_get_count(CPUARMState *env)
1188{
1189 /*
1190 * SW_INCR events are written directly to the pmevcntr's by writes to
1191 * PMSWINC, so there is no underlying count maintained by the PMU itself
1192 */
1193 return 0;
1194}
1195
4e7beb0c
AL
1196static int64_t swinc_ns_per(uint64_t ignored)
1197{
1198 return -1;
1199}
1200
b2e23725
AL
1201/*
1202 * Return the underlying cycle count for the PMU cycle counters. If we're in
1203 * usermode, simply return 0.
1204 */
1205static uint64_t cycles_get_count(CPUARMState *env)
1206{
1207#ifndef CONFIG_USER_ONLY
1208 return muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
1209 ARM_CPU_FREQ, NANOSECONDS_PER_SECOND);
1210#else
1211 return cpu_get_host_ticks();
1212#endif
1213}
1214
1215#ifndef CONFIG_USER_ONLY
4e7beb0c
AL
1216static int64_t cycles_ns_per(uint64_t cycles)
1217{
1218 return (ARM_CPU_FREQ / NANOSECONDS_PER_SECOND) * cycles;
1219}
1220
b2e23725
AL
1221static bool instructions_supported(CPUARMState *env)
1222{
1223 return use_icount == 1 /* Precise instruction counting */;
1224}
1225
1226static uint64_t instructions_get_count(CPUARMState *env)
1227{
1228 return (uint64_t)cpu_get_icount_raw();
1229}
4e7beb0c
AL
1230
1231static int64_t instructions_ns_per(uint64_t icount)
1232{
1233 return cpu_icount_to_ns((int64_t)icount);
1234}
b2e23725
AL
1235#endif
1236
0727f63b
PM
1237static bool pmu_8_1_events_supported(CPUARMState *env)
1238{
1239 /* For events which are supported in any v8.1 PMU */
1240 return cpu_isar_feature(any_pmu_8_1, env_archcpu(env));
1241}
1242
15dd1ebd
PM
1243static bool pmu_8_4_events_supported(CPUARMState *env)
1244{
1245 /* For events which are supported in any v8.1 PMU */
1246 return cpu_isar_feature(any_pmu_8_4, env_archcpu(env));
1247}
1248
0727f63b
PM
1249static uint64_t zero_event_get_count(CPUARMState *env)
1250{
1251 /* For events which on QEMU never fire, so their count is always zero */
1252 return 0;
1253}
1254
1255static int64_t zero_event_ns_per(uint64_t cycles)
1256{
1257 /* An event which never fires can never overflow */
1258 return -1;
1259}
1260
57a4a11b 1261static const pm_event pm_events[] = {
0d4bfd7d
AL
1262 { .number = 0x000, /* SW_INCR */
1263 .supported = event_always_supported,
1264 .get_count = swinc_get_count,
4e7beb0c 1265 .ns_per_count = swinc_ns_per,
0d4bfd7d 1266 },
b2e23725
AL
1267#ifndef CONFIG_USER_ONLY
1268 { .number = 0x008, /* INST_RETIRED, Instruction architecturally executed */
1269 .supported = instructions_supported,
1270 .get_count = instructions_get_count,
4e7beb0c 1271 .ns_per_count = instructions_ns_per,
b2e23725
AL
1272 },
1273 { .number = 0x011, /* CPU_CYCLES, Cycle */
1274 .supported = event_always_supported,
1275 .get_count = cycles_get_count,
4e7beb0c 1276 .ns_per_count = cycles_ns_per,
0727f63b 1277 },
b2e23725 1278#endif
0727f63b
PM
1279 { .number = 0x023, /* STALL_FRONTEND */
1280 .supported = pmu_8_1_events_supported,
1281 .get_count = zero_event_get_count,
1282 .ns_per_count = zero_event_ns_per,
1283 },
1284 { .number = 0x024, /* STALL_BACKEND */
1285 .supported = pmu_8_1_events_supported,
1286 .get_count = zero_event_get_count,
1287 .ns_per_count = zero_event_ns_per,
1288 },
15dd1ebd
PM
1289 { .number = 0x03c, /* STALL */
1290 .supported = pmu_8_4_events_supported,
1291 .get_count = zero_event_get_count,
1292 .ns_per_count = zero_event_ns_per,
1293 },
57a4a11b
AL
1294};
1295
1296/*
1297 * Note: Before increasing MAX_EVENT_ID beyond 0x3f into the 0x40xx range of
1298 * events (i.e. the statistical profiling extension), this implementation
1299 * should first be updated to something sparse instead of the current
1300 * supported_event_map[] array.
1301 */
15dd1ebd 1302#define MAX_EVENT_ID 0x3c
57a4a11b
AL
1303#define UNSUPPORTED_EVENT UINT16_MAX
1304static uint16_t supported_event_map[MAX_EVENT_ID + 1];
1305
1306/*
bf8d0969
AL
1307 * Called upon CPU initialization to initialize PMCEID[01]_EL0 and build a map
1308 * of ARM event numbers to indices in our pm_events array.
57a4a11b
AL
1309 *
1310 * Note: Events in the 0x40XX range are not currently supported.
1311 */
bf8d0969 1312void pmu_init(ARMCPU *cpu)
57a4a11b 1313{
57a4a11b
AL
1314 unsigned int i;
1315
bf8d0969
AL
1316 /*
1317 * Empty supported_event_map and cpu->pmceid[01] before adding supported
1318 * events to them
1319 */
57a4a11b
AL
1320 for (i = 0; i < ARRAY_SIZE(supported_event_map); i++) {
1321 supported_event_map[i] = UNSUPPORTED_EVENT;
1322 }
bf8d0969
AL
1323 cpu->pmceid0 = 0;
1324 cpu->pmceid1 = 0;
57a4a11b
AL
1325
1326 for (i = 0; i < ARRAY_SIZE(pm_events); i++) {
1327 const pm_event *cnt = &pm_events[i];
1328 assert(cnt->number <= MAX_EVENT_ID);
1329 /* We do not currently support events in the 0x40xx range */
1330 assert(cnt->number <= 0x3f);
1331
bf8d0969 1332 if (cnt->supported(&cpu->env)) {
57a4a11b 1333 supported_event_map[cnt->number] = i;
67da43d6 1334 uint64_t event_mask = 1ULL << (cnt->number & 0x1f);
bf8d0969
AL
1335 if (cnt->number & 0x20) {
1336 cpu->pmceid1 |= event_mask;
1337 } else {
1338 cpu->pmceid0 |= event_mask;
1339 }
57a4a11b
AL
1340 }
1341 }
57a4a11b
AL
1342}
1343
5ecdd3e4
AL
1344/*
1345 * Check at runtime whether a PMU event is supported for the current machine
1346 */
1347static bool event_supported(uint16_t number)
1348{
1349 if (number > MAX_EVENT_ID) {
1350 return false;
1351 }
1352 return supported_event_map[number] != UNSUPPORTED_EVENT;
1353}
1354
3f208fd7
PM
1355static CPAccessResult pmreg_access(CPUARMState *env, const ARMCPRegInfo *ri,
1356 bool isread)
200ac0ef 1357{
3b163b01 1358 /* Performance monitor registers user accessibility is controlled
1fce1ba9
PM
1359 * by PMUSERENR. MDCR_EL2.TPM and MDCR_EL3.TPM allow configurable
1360 * trapping to EL2 or EL3 for other accesses.
200ac0ef 1361 */
1fce1ba9
PM
1362 int el = arm_current_el(env);
1363
6ecd0b6b 1364 if (el == 0 && !(env->cp15.c9_pmuserenr & 1)) {
fcd25206 1365 return CP_ACCESS_TRAP;
200ac0ef 1366 }
1fce1ba9
PM
1367 if (el < 2 && (env->cp15.mdcr_el2 & MDCR_TPM)
1368 && !arm_is_secure_below_el3(env)) {
1369 return CP_ACCESS_TRAP_EL2;
1370 }
1371 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) {
1372 return CP_ACCESS_TRAP_EL3;
1373 }
1374
fcd25206 1375 return CP_ACCESS_OK;
200ac0ef
PM
1376}
1377
6ecd0b6b
AB
1378static CPAccessResult pmreg_access_xevcntr(CPUARMState *env,
1379 const ARMCPRegInfo *ri,
1380 bool isread)
1381{
1382 /* ER: event counter read trap control */
1383 if (arm_feature(env, ARM_FEATURE_V8)
1384 && arm_current_el(env) == 0
1385 && (env->cp15.c9_pmuserenr & (1 << 3)) != 0
1386 && isread) {
1387 return CP_ACCESS_OK;
1388 }
1389
1390 return pmreg_access(env, ri, isread);
1391}
1392
1393static CPAccessResult pmreg_access_swinc(CPUARMState *env,
1394 const ARMCPRegInfo *ri,
1395 bool isread)
1396{
1397 /* SW: software increment write trap control */
1398 if (arm_feature(env, ARM_FEATURE_V8)
1399 && arm_current_el(env) == 0
1400 && (env->cp15.c9_pmuserenr & (1 << 1)) != 0
1401 && !isread) {
1402 return CP_ACCESS_OK;
1403 }
1404
1405 return pmreg_access(env, ri, isread);
1406}
1407
6ecd0b6b
AB
1408static CPAccessResult pmreg_access_selr(CPUARMState *env,
1409 const ARMCPRegInfo *ri,
1410 bool isread)
1411{
1412 /* ER: event counter read trap control */
1413 if (arm_feature(env, ARM_FEATURE_V8)
1414 && arm_current_el(env) == 0
1415 && (env->cp15.c9_pmuserenr & (1 << 3)) != 0) {
1416 return CP_ACCESS_OK;
1417 }
1418
1419 return pmreg_access(env, ri, isread);
1420}
1421
1422static CPAccessResult pmreg_access_ccntr(CPUARMState *env,
1423 const ARMCPRegInfo *ri,
1424 bool isread)
1425{
1426 /* CR: cycle counter read trap control */
1427 if (arm_feature(env, ARM_FEATURE_V8)
1428 && arm_current_el(env) == 0
1429 && (env->cp15.c9_pmuserenr & (1 << 2)) != 0
1430 && isread) {
1431 return CP_ACCESS_OK;
1432 }
1433
1434 return pmreg_access(env, ri, isread);
1435}
1436
033614c4
AL
1437/* Returns true if the counter (pass 31 for PMCCNTR) should count events using
1438 * the current EL, security state, and register configuration.
1439 */
1440static bool pmu_counter_enabled(CPUARMState *env, uint8_t counter)
87124fde 1441{
033614c4
AL
1442 uint64_t filter;
1443 bool e, p, u, nsk, nsu, nsh, m;
1444 bool enabled, prohibited, filtered;
1445 bool secure = arm_is_secure(env);
1446 int el = arm_current_el(env);
1447 uint8_t hpmn = env->cp15.mdcr_el2 & MDCR_HPMN;
87124fde 1448
cbbb3041
AJ
1449 if (!arm_feature(env, ARM_FEATURE_PMU)) {
1450 return false;
1451 }
1452
033614c4
AL
1453 if (!arm_feature(env, ARM_FEATURE_EL2) ||
1454 (counter < hpmn || counter == 31)) {
1455 e = env->cp15.c9_pmcr & PMCRE;
1456 } else {
1457 e = env->cp15.mdcr_el2 & MDCR_HPME;
87124fde 1458 }
033614c4 1459 enabled = e && (env->cp15.c9_pmcnten & (1 << counter));
87124fde 1460
033614c4
AL
1461 if (!secure) {
1462 if (el == 2 && (counter < hpmn || counter == 31)) {
1463 prohibited = env->cp15.mdcr_el2 & MDCR_HPMD;
1464 } else {
1465 prohibited = false;
1466 }
1467 } else {
1468 prohibited = arm_feature(env, ARM_FEATURE_EL3) &&
1469 (env->cp15.mdcr_el3 & MDCR_SPME);
1470 }
1471
1472 if (prohibited && counter == 31) {
1473 prohibited = env->cp15.c9_pmcr & PMCRDP;
1474 }
1475
5ecdd3e4
AL
1476 if (counter == 31) {
1477 filter = env->cp15.pmccfiltr_el0;
1478 } else {
1479 filter = env->cp15.c14_pmevtyper[counter];
1480 }
033614c4
AL
1481
1482 p = filter & PMXEVTYPER_P;
1483 u = filter & PMXEVTYPER_U;
1484 nsk = arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_NSK);
1485 nsu = arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_NSU);
1486 nsh = arm_feature(env, ARM_FEATURE_EL2) && (filter & PMXEVTYPER_NSH);
1487 m = arm_el_is_aa64(env, 1) &&
1488 arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_M);
1489
1490 if (el == 0) {
1491 filtered = secure ? u : u != nsu;
1492 } else if (el == 1) {
1493 filtered = secure ? p : p != nsk;
1494 } else if (el == 2) {
1495 filtered = !nsh;
1496 } else { /* EL3 */
1497 filtered = m != p;
1498 }
1499
5ecdd3e4
AL
1500 if (counter != 31) {
1501 /*
1502 * If not checking PMCCNTR, ensure the counter is setup to an event we
1503 * support
1504 */
1505 uint16_t event = filter & PMXEVTYPER_EVTCOUNT;
1506 if (!event_supported(event)) {
1507 return false;
1508 }
1509 }
1510
033614c4 1511 return enabled && !prohibited && !filtered;
87124fde 1512}
033614c4 1513
f4efb4b2
AL
1514static void pmu_update_irq(CPUARMState *env)
1515{
2fc0cc0e 1516 ARMCPU *cpu = env_archcpu(env);
f4efb4b2
AL
1517 qemu_set_irq(cpu->pmu_interrupt, (env->cp15.c9_pmcr & PMCRE) &&
1518 (env->cp15.c9_pminten & env->cp15.c9_pmovsr));
1519}
1520
5d05b9d4
AL
1521/*
1522 * Ensure c15_ccnt is the guest-visible count so that operations such as
1523 * enabling/disabling the counter or filtering, modifying the count itself,
1524 * etc. can be done logically. This is essentially a no-op if the counter is
1525 * not enabled at the time of the call.
1526 */
f2b2f53f 1527static void pmccntr_op_start(CPUARMState *env)
ec7b4ce4 1528{
b2e23725 1529 uint64_t cycles = cycles_get_count(env);
ec7b4ce4 1530
033614c4 1531 if (pmu_counter_enabled(env, 31)) {
5d05b9d4
AL
1532 uint64_t eff_cycles = cycles;
1533 if (env->cp15.c9_pmcr & PMCRD) {
1534 /* Increment once every 64 processor clock cycles */
1535 eff_cycles /= 64;
1536 }
1537
f4efb4b2
AL
1538 uint64_t new_pmccntr = eff_cycles - env->cp15.c15_ccnt_delta;
1539
1540 uint64_t overflow_mask = env->cp15.c9_pmcr & PMCRLC ? \
1541 1ull << 63 : 1ull << 31;
1542 if (env->cp15.c15_ccnt & ~new_pmccntr & overflow_mask) {
1543 env->cp15.c9_pmovsr |= (1 << 31);
1544 pmu_update_irq(env);
1545 }
1546
1547 env->cp15.c15_ccnt = new_pmccntr;
ec7b4ce4 1548 }
5d05b9d4
AL
1549 env->cp15.c15_ccnt_delta = cycles;
1550}
ec7b4ce4 1551
5d05b9d4
AL
1552/*
1553 * If PMCCNTR is enabled, recalculate the delta between the clock and the
1554 * guest-visible count. A call to pmccntr_op_finish should follow every call to
1555 * pmccntr_op_start.
1556 */
f2b2f53f 1557static void pmccntr_op_finish(CPUARMState *env)
5d05b9d4 1558{
033614c4 1559 if (pmu_counter_enabled(env, 31)) {
4e7beb0c
AL
1560#ifndef CONFIG_USER_ONLY
1561 /* Calculate when the counter will next overflow */
1562 uint64_t remaining_cycles = -env->cp15.c15_ccnt;
1563 if (!(env->cp15.c9_pmcr & PMCRLC)) {
1564 remaining_cycles = (uint32_t)remaining_cycles;
1565 }
1566 int64_t overflow_in = cycles_ns_per(remaining_cycles);
1567
1568 if (overflow_in > 0) {
1569 int64_t overflow_at = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
1570 overflow_in;
2fc0cc0e 1571 ARMCPU *cpu = env_archcpu(env);
4e7beb0c
AL
1572 timer_mod_anticipate_ns(cpu->pmu_timer, overflow_at);
1573 }
1574#endif
5d05b9d4 1575
4e7beb0c 1576 uint64_t prev_cycles = env->cp15.c15_ccnt_delta;
5d05b9d4
AL
1577 if (env->cp15.c9_pmcr & PMCRD) {
1578 /* Increment once every 64 processor clock cycles */
1579 prev_cycles /= 64;
1580 }
5d05b9d4 1581 env->cp15.c15_ccnt_delta = prev_cycles - env->cp15.c15_ccnt;
ec7b4ce4
AF
1582 }
1583}
1584
5ecdd3e4
AL
1585static void pmevcntr_op_start(CPUARMState *env, uint8_t counter)
1586{
1587
1588 uint16_t event = env->cp15.c14_pmevtyper[counter] & PMXEVTYPER_EVTCOUNT;
1589 uint64_t count = 0;
1590 if (event_supported(event)) {
1591 uint16_t event_idx = supported_event_map[event];
1592 count = pm_events[event_idx].get_count(env);
1593 }
1594
1595 if (pmu_counter_enabled(env, counter)) {
f4efb4b2
AL
1596 uint32_t new_pmevcntr = count - env->cp15.c14_pmevcntr_delta[counter];
1597
1598 if (env->cp15.c14_pmevcntr[counter] & ~new_pmevcntr & INT32_MIN) {
1599 env->cp15.c9_pmovsr |= (1 << counter);
1600 pmu_update_irq(env);
1601 }
1602 env->cp15.c14_pmevcntr[counter] = new_pmevcntr;
5ecdd3e4
AL
1603 }
1604 env->cp15.c14_pmevcntr_delta[counter] = count;
1605}
1606
1607static void pmevcntr_op_finish(CPUARMState *env, uint8_t counter)
1608{
1609 if (pmu_counter_enabled(env, counter)) {
4e7beb0c
AL
1610#ifndef CONFIG_USER_ONLY
1611 uint16_t event = env->cp15.c14_pmevtyper[counter] & PMXEVTYPER_EVTCOUNT;
1612 uint16_t event_idx = supported_event_map[event];
1613 uint64_t delta = UINT32_MAX -
1614 (uint32_t)env->cp15.c14_pmevcntr[counter] + 1;
1615 int64_t overflow_in = pm_events[event_idx].ns_per_count(delta);
1616
1617 if (overflow_in > 0) {
1618 int64_t overflow_at = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
1619 overflow_in;
2fc0cc0e 1620 ARMCPU *cpu = env_archcpu(env);
4e7beb0c
AL
1621 timer_mod_anticipate_ns(cpu->pmu_timer, overflow_at);
1622 }
1623#endif
1624
5ecdd3e4
AL
1625 env->cp15.c14_pmevcntr_delta[counter] -=
1626 env->cp15.c14_pmevcntr[counter];
1627 }
1628}
1629
5d05b9d4
AL
1630void pmu_op_start(CPUARMState *env)
1631{
5ecdd3e4 1632 unsigned int i;
5d05b9d4 1633 pmccntr_op_start(env);
5ecdd3e4
AL
1634 for (i = 0; i < pmu_num_counters(env); i++) {
1635 pmevcntr_op_start(env, i);
1636 }
5d05b9d4
AL
1637}
1638
1639void pmu_op_finish(CPUARMState *env)
1640{
5ecdd3e4 1641 unsigned int i;
5d05b9d4 1642 pmccntr_op_finish(env);
5ecdd3e4
AL
1643 for (i = 0; i < pmu_num_counters(env); i++) {
1644 pmevcntr_op_finish(env, i);
1645 }
5d05b9d4
AL
1646}
1647
033614c4
AL
1648void pmu_pre_el_change(ARMCPU *cpu, void *ignored)
1649{
1650 pmu_op_start(&cpu->env);
1651}
1652
1653void pmu_post_el_change(ARMCPU *cpu, void *ignored)
1654{
1655 pmu_op_finish(&cpu->env);
1656}
1657
4e7beb0c
AL
1658void arm_pmu_timer_cb(void *opaque)
1659{
1660 ARMCPU *cpu = opaque;
1661
1662 /*
1663 * Update all the counter values based on the current underlying counts,
1664 * triggering interrupts to be raised, if necessary. pmu_op_finish() also
1665 * has the effect of setting the cpu->pmu_timer to the next earliest time a
1666 * counter may expire.
1667 */
1668 pmu_op_start(&cpu->env);
1669 pmu_op_finish(&cpu->env);
1670}
1671
c4241c7d
PM
1672static void pmcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1673 uint64_t value)
200ac0ef 1674{
5d05b9d4 1675 pmu_op_start(env);
7c2cb42b
AF
1676
1677 if (value & PMCRC) {
1678 /* The counter has been reset */
1679 env->cp15.c15_ccnt = 0;
1680 }
1681
5ecdd3e4
AL
1682 if (value & PMCRP) {
1683 unsigned int i;
1684 for (i = 0; i < pmu_num_counters(env); i++) {
1685 env->cp15.c14_pmevcntr[i] = 0;
1686 }
1687 }
1688
62d96ff4
PM
1689 env->cp15.c9_pmcr &= ~PMCR_WRITEABLE_MASK;
1690 env->cp15.c9_pmcr |= (value & PMCR_WRITEABLE_MASK);
7c2cb42b 1691
5d05b9d4 1692 pmu_op_finish(env);
7c2cb42b
AF
1693}
1694
0d4bfd7d
AL
1695static void pmswinc_write(CPUARMState *env, const ARMCPRegInfo *ri,
1696 uint64_t value)
1697{
1698 unsigned int i;
1699 for (i = 0; i < pmu_num_counters(env); i++) {
1700 /* Increment a counter's count iff: */
1701 if ((value & (1 << i)) && /* counter's bit is set */
1702 /* counter is enabled and not filtered */
1703 pmu_counter_enabled(env, i) &&
1704 /* counter is SW_INCR */
1705 (env->cp15.c14_pmevtyper[i] & PMXEVTYPER_EVTCOUNT) == 0x0) {
1706 pmevcntr_op_start(env, i);
f4efb4b2
AL
1707
1708 /*
1709 * Detect if this write causes an overflow since we can't predict
1710 * PMSWINC overflows like we can for other events
1711 */
1712 uint32_t new_pmswinc = env->cp15.c14_pmevcntr[i] + 1;
1713
1714 if (env->cp15.c14_pmevcntr[i] & ~new_pmswinc & INT32_MIN) {
1715 env->cp15.c9_pmovsr |= (1 << i);
1716 pmu_update_irq(env);
1717 }
1718
1719 env->cp15.c14_pmevcntr[i] = new_pmswinc;
1720
0d4bfd7d
AL
1721 pmevcntr_op_finish(env, i);
1722 }
1723 }
1724}
1725
7c2cb42b
AF
1726static uint64_t pmccntr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1727{
5d05b9d4
AL
1728 uint64_t ret;
1729 pmccntr_op_start(env);
1730 ret = env->cp15.c15_ccnt;
1731 pmccntr_op_finish(env);
1732 return ret;
7c2cb42b
AF
1733}
1734
6b040780
WH
1735static void pmselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1736 uint64_t value)
1737{
1738 /* The value of PMSELR.SEL affects the behavior of PMXEVTYPER and
1739 * PMXEVCNTR. We allow [0..31] to be written to PMSELR here; in the
1740 * meanwhile, we check PMSELR.SEL when PMXEVTYPER and PMXEVCNTR are
1741 * accessed.
1742 */
1743 env->cp15.c9_pmselr = value & 0x1f;
1744}
1745
7c2cb42b
AF
1746static void pmccntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1747 uint64_t value)
1748{
5d05b9d4
AL
1749 pmccntr_op_start(env);
1750 env->cp15.c15_ccnt = value;
1751 pmccntr_op_finish(env);
200ac0ef 1752}
421c7ebd
PC
1753
1754static void pmccntr_write32(CPUARMState *env, const ARMCPRegInfo *ri,
1755 uint64_t value)
1756{
1757 uint64_t cur_val = pmccntr_read(env, NULL);
1758
1759 pmccntr_write(env, ri, deposit64(cur_val, 0, 32, value));
1760}
1761
0614601c
AF
1762static void pmccfiltr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1763 uint64_t value)
1764{
5d05b9d4 1765 pmccntr_op_start(env);
4b8afa1f
AL
1766 env->cp15.pmccfiltr_el0 = value & PMCCFILTR_EL0;
1767 pmccntr_op_finish(env);
1768}
1769
1770static void pmccfiltr_write_a32(CPUARMState *env, const ARMCPRegInfo *ri,
1771 uint64_t value)
1772{
1773 pmccntr_op_start(env);
1774 /* M is not accessible from AArch32 */
1775 env->cp15.pmccfiltr_el0 = (env->cp15.pmccfiltr_el0 & PMCCFILTR_M) |
1776 (value & PMCCFILTR);
5d05b9d4 1777 pmccntr_op_finish(env);
0614601c
AF
1778}
1779
4b8afa1f
AL
1780static uint64_t pmccfiltr_read_a32(CPUARMState *env, const ARMCPRegInfo *ri)
1781{
1782 /* M is not visible in AArch32 */
1783 return env->cp15.pmccfiltr_el0 & PMCCFILTR;
1784}
1785
c4241c7d 1786static void pmcntenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
200ac0ef
PM
1787 uint64_t value)
1788{
7ece99b1 1789 value &= pmu_counter_mask(env);
200ac0ef 1790 env->cp15.c9_pmcnten |= value;
200ac0ef
PM
1791}
1792
c4241c7d
PM
1793static void pmcntenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1794 uint64_t value)
200ac0ef 1795{
7ece99b1 1796 value &= pmu_counter_mask(env);
200ac0ef 1797 env->cp15.c9_pmcnten &= ~value;
200ac0ef
PM
1798}
1799
c4241c7d
PM
1800static void pmovsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1801 uint64_t value)
200ac0ef 1802{
599b71e2 1803 value &= pmu_counter_mask(env);
200ac0ef 1804 env->cp15.c9_pmovsr &= ~value;
f4efb4b2 1805 pmu_update_irq(env);
200ac0ef
PM
1806}
1807
327dd510
AL
1808static void pmovsset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1809 uint64_t value)
1810{
1811 value &= pmu_counter_mask(env);
1812 env->cp15.c9_pmovsr |= value;
f4efb4b2 1813 pmu_update_irq(env);
327dd510
AL
1814}
1815
5ecdd3e4
AL
1816static void pmevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
1817 uint64_t value, const uint8_t counter)
200ac0ef 1818{
5ecdd3e4
AL
1819 if (counter == 31) {
1820 pmccfiltr_write(env, ri, value);
1821 } else if (counter < pmu_num_counters(env)) {
1822 pmevcntr_op_start(env, counter);
1823
1824 /*
1825 * If this counter's event type is changing, store the current
1826 * underlying count for the new type in c14_pmevcntr_delta[counter] so
1827 * pmevcntr_op_finish has the correct baseline when it converts back to
1828 * a delta.
1829 */
1830 uint16_t old_event = env->cp15.c14_pmevtyper[counter] &
1831 PMXEVTYPER_EVTCOUNT;
1832 uint16_t new_event = value & PMXEVTYPER_EVTCOUNT;
1833 if (old_event != new_event) {
1834 uint64_t count = 0;
1835 if (event_supported(new_event)) {
1836 uint16_t event_idx = supported_event_map[new_event];
1837 count = pm_events[event_idx].get_count(env);
1838 }
1839 env->cp15.c14_pmevcntr_delta[counter] = count;
1840 }
1841
1842 env->cp15.c14_pmevtyper[counter] = value & PMXEVTYPER_MASK;
1843 pmevcntr_op_finish(env, counter);
1844 }
fdb86656
WH
1845 /* Attempts to access PMXEVTYPER are CONSTRAINED UNPREDICTABLE when
1846 * PMSELR value is equal to or greater than the number of implemented
1847 * counters, but not equal to 0x1f. We opt to behave as a RAZ/WI.
1848 */
5ecdd3e4
AL
1849}
1850
1851static uint64_t pmevtyper_read(CPUARMState *env, const ARMCPRegInfo *ri,
1852 const uint8_t counter)
1853{
1854 if (counter == 31) {
1855 return env->cp15.pmccfiltr_el0;
1856 } else if (counter < pmu_num_counters(env)) {
1857 return env->cp15.c14_pmevtyper[counter];
1858 } else {
1859 /*
1860 * We opt to behave as a RAZ/WI when attempts to access PMXEVTYPER
1861 * are CONSTRAINED UNPREDICTABLE. See comments in pmevtyper_write().
1862 */
1863 return 0;
1864 }
1865}
1866
1867static void pmevtyper_writefn(CPUARMState *env, const ARMCPRegInfo *ri,
1868 uint64_t value)
1869{
1870 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1871 pmevtyper_write(env, ri, value, counter);
1872}
1873
1874static void pmevtyper_rawwrite(CPUARMState *env, const ARMCPRegInfo *ri,
1875 uint64_t value)
1876{
1877 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1878 env->cp15.c14_pmevtyper[counter] = value;
1879
1880 /*
1881 * pmevtyper_rawwrite is called between a pair of pmu_op_start and
1882 * pmu_op_finish calls when loading saved state for a migration. Because
1883 * we're potentially updating the type of event here, the value written to
1884 * c14_pmevcntr_delta by the preceeding pmu_op_start call may be for a
1885 * different counter type. Therefore, we need to set this value to the
1886 * current count for the counter type we're writing so that pmu_op_finish
1887 * has the correct count for its calculation.
1888 */
1889 uint16_t event = value & PMXEVTYPER_EVTCOUNT;
1890 if (event_supported(event)) {
1891 uint16_t event_idx = supported_event_map[event];
1892 env->cp15.c14_pmevcntr_delta[counter] =
1893 pm_events[event_idx].get_count(env);
fdb86656
WH
1894 }
1895}
1896
5ecdd3e4
AL
1897static uint64_t pmevtyper_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
1898{
1899 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1900 return pmevtyper_read(env, ri, counter);
1901}
1902
1903static void pmxevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
1904 uint64_t value)
1905{
1906 pmevtyper_write(env, ri, value, env->cp15.c9_pmselr & 31);
1907}
1908
fdb86656
WH
1909static uint64_t pmxevtyper_read(CPUARMState *env, const ARMCPRegInfo *ri)
1910{
5ecdd3e4
AL
1911 return pmevtyper_read(env, ri, env->cp15.c9_pmselr & 31);
1912}
1913
1914static void pmevcntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1915 uint64_t value, uint8_t counter)
1916{
1917 if (counter < pmu_num_counters(env)) {
1918 pmevcntr_op_start(env, counter);
1919 env->cp15.c14_pmevcntr[counter] = value;
1920 pmevcntr_op_finish(env, counter);
1921 }
1922 /*
1923 * We opt to behave as a RAZ/WI when attempts to access PM[X]EVCNTR
1924 * are CONSTRAINED UNPREDICTABLE.
fdb86656 1925 */
5ecdd3e4
AL
1926}
1927
1928static uint64_t pmevcntr_read(CPUARMState *env, const ARMCPRegInfo *ri,
1929 uint8_t counter)
1930{
1931 if (counter < pmu_num_counters(env)) {
1932 uint64_t ret;
1933 pmevcntr_op_start(env, counter);
1934 ret = env->cp15.c14_pmevcntr[counter];
1935 pmevcntr_op_finish(env, counter);
1936 return ret;
fdb86656 1937 } else {
5ecdd3e4
AL
1938 /* We opt to behave as a RAZ/WI when attempts to access PM[X]EVCNTR
1939 * are CONSTRAINED UNPREDICTABLE. */
fdb86656
WH
1940 return 0;
1941 }
200ac0ef
PM
1942}
1943
5ecdd3e4
AL
1944static void pmevcntr_writefn(CPUARMState *env, const ARMCPRegInfo *ri,
1945 uint64_t value)
1946{
1947 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1948 pmevcntr_write(env, ri, value, counter);
1949}
1950
1951static uint64_t pmevcntr_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
1952{
1953 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1954 return pmevcntr_read(env, ri, counter);
1955}
1956
1957static void pmevcntr_rawwrite(CPUARMState *env, const ARMCPRegInfo *ri,
1958 uint64_t value)
1959{
1960 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1961 assert(counter < pmu_num_counters(env));
1962 env->cp15.c14_pmevcntr[counter] = value;
1963 pmevcntr_write(env, ri, value, counter);
1964}
1965
1966static uint64_t pmevcntr_rawread(CPUARMState *env, const ARMCPRegInfo *ri)
1967{
1968 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1969 assert(counter < pmu_num_counters(env));
1970 return env->cp15.c14_pmevcntr[counter];
1971}
1972
1973static void pmxevcntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1974 uint64_t value)
1975{
1976 pmevcntr_write(env, ri, value, env->cp15.c9_pmselr & 31);
1977}
1978
1979static uint64_t pmxevcntr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1980{
1981 return pmevcntr_read(env, ri, env->cp15.c9_pmselr & 31);
1982}
1983
c4241c7d 1984static void pmuserenr_write(CPUARMState *env, const ARMCPRegInfo *ri,
200ac0ef
PM
1985 uint64_t value)
1986{
6ecd0b6b
AB
1987 if (arm_feature(env, ARM_FEATURE_V8)) {
1988 env->cp15.c9_pmuserenr = value & 0xf;
1989 } else {
1990 env->cp15.c9_pmuserenr = value & 1;
1991 }
200ac0ef
PM
1992}
1993
c4241c7d
PM
1994static void pmintenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1995 uint64_t value)
200ac0ef
PM
1996{
1997 /* We have no event counters so only the C bit can be changed */
7ece99b1 1998 value &= pmu_counter_mask(env);
200ac0ef 1999 env->cp15.c9_pminten |= value;
f4efb4b2 2000 pmu_update_irq(env);
200ac0ef
PM
2001}
2002
c4241c7d
PM
2003static void pmintenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2004 uint64_t value)
200ac0ef 2005{
7ece99b1 2006 value &= pmu_counter_mask(env);
200ac0ef 2007 env->cp15.c9_pminten &= ~value;
f4efb4b2 2008 pmu_update_irq(env);
200ac0ef
PM
2009}
2010
c4241c7d
PM
2011static void vbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
2012 uint64_t value)
8641136c 2013{
a505d7fe
PM
2014 /* Note that even though the AArch64 view of this register has bits
2015 * [10:0] all RES0 we can only mask the bottom 5, to comply with the
2016 * architectural requirements for bits which are RES0 only in some
2017 * contexts. (ARMv8 would permit us to do no masking at all, but ARMv7
2018 * requires the bottom five bits to be RAZ/WI because they're UNK/SBZP.)
2019 */
855ea66d 2020 raw_write(env, ri, value & ~0x1FULL);
8641136c
NR
2021}
2022
64e0e2de
EI
2023static void scr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
2024{
ea22747c
RH
2025 /* Begin with base v8.0 state. */
2026 uint32_t valid_mask = 0x3fff;
2fc0cc0e 2027 ARMCPU *cpu = env_archcpu(env);
ea22747c
RH
2028
2029 if (arm_el_is_aa64(env, 3)) {
2030 value |= SCR_FW | SCR_AW; /* these two bits are RES1. */
2031 valid_mask &= ~SCR_NET;
2032 } else {
2033 valid_mask &= ~(SCR_RW | SCR_ST);
2034 }
64e0e2de
EI
2035
2036 if (!arm_feature(env, ARM_FEATURE_EL2)) {
2037 valid_mask &= ~SCR_HCE;
2038
2039 /* On ARMv7, SMD (or SCD as it is called in v7) is only
2040 * supported if EL2 exists. The bit is UNK/SBZP when
2041 * EL2 is unavailable. In QEMU ARMv7, we force it to always zero
2042 * when EL2 is unavailable.
4eb27640 2043 * On ARMv8, this bit is always available.
64e0e2de 2044 */
4eb27640
GB
2045 if (arm_feature(env, ARM_FEATURE_V7) &&
2046 !arm_feature(env, ARM_FEATURE_V8)) {
64e0e2de
EI
2047 valid_mask &= ~SCR_SMD;
2048 }
2049 }
2d7137c1
RH
2050 if (cpu_isar_feature(aa64_lor, cpu)) {
2051 valid_mask |= SCR_TLOR;
2052 }
ef682cdb
RH
2053 if (cpu_isar_feature(aa64_pauth, cpu)) {
2054 valid_mask |= SCR_API | SCR_APK;
2055 }
64e0e2de
EI
2056
2057 /* Clear all-context RES0 bits. */
2058 value &= valid_mask;
2059 raw_write(env, ri, value);
2060}
2061
630fcd4d
MZ
2062static CPAccessResult access_aa64_tid2(CPUARMState *env,
2063 const ARMCPRegInfo *ri,
2064 bool isread)
2065{
2066 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID2)) {
2067 return CP_ACCESS_TRAP_EL2;
2068 }
2069
2070 return CP_ACCESS_OK;
2071}
2072
c4241c7d 2073static uint64_t ccsidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
776d4e5c 2074{
2fc0cc0e 2075 ARMCPU *cpu = env_archcpu(env);
b85a1fd6
FA
2076
2077 /* Acquire the CSSELR index from the bank corresponding to the CCSIDR
2078 * bank
2079 */
2080 uint32_t index = A32_BANKED_REG_GET(env, csselr,
2081 ri->secure & ARM_CP_SECSTATE_S);
2082
2083 return cpu->ccsidr[index];
776d4e5c
PM
2084}
2085
c4241c7d
PM
2086static void csselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2087 uint64_t value)
776d4e5c 2088{
8d5c773e 2089 raw_write(env, ri, value & 0xf);
776d4e5c
PM
2090}
2091
1090b9c6
PM
2092static uint64_t isr_read(CPUARMState *env, const ARMCPRegInfo *ri)
2093{
29a0af61 2094 CPUState *cs = env_cpu(env);
f7778444 2095 uint64_t hcr_el2 = arm_hcr_el2_eff(env);
1090b9c6 2096 uint64_t ret = 0;
7cf95aed
MZ
2097 bool allow_virt = (arm_current_el(env) == 1 &&
2098 (!arm_is_secure_below_el3(env) ||
2099 (env->cp15.scr_el3 & SCR_EEL2)));
1090b9c6 2100
7cf95aed 2101 if (allow_virt && (hcr_el2 & HCR_IMO)) {
636540e9
PM
2102 if (cs->interrupt_request & CPU_INTERRUPT_VIRQ) {
2103 ret |= CPSR_I;
2104 }
2105 } else {
2106 if (cs->interrupt_request & CPU_INTERRUPT_HARD) {
2107 ret |= CPSR_I;
2108 }
1090b9c6 2109 }
636540e9 2110
7cf95aed 2111 if (allow_virt && (hcr_el2 & HCR_FMO)) {
636540e9
PM
2112 if (cs->interrupt_request & CPU_INTERRUPT_VFIQ) {
2113 ret |= CPSR_F;
2114 }
2115 } else {
2116 if (cs->interrupt_request & CPU_INTERRUPT_FIQ) {
2117 ret |= CPSR_F;
2118 }
1090b9c6 2119 }
636540e9 2120
1090b9c6
PM
2121 /* External aborts are not possible in QEMU so A bit is always clear */
2122 return ret;
2123}
2124
93fbc983
MZ
2125static CPAccessResult access_aa64_tid1(CPUARMState *env, const ARMCPRegInfo *ri,
2126 bool isread)
2127{
2128 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID1)) {
2129 return CP_ACCESS_TRAP_EL2;
2130 }
2131
2132 return CP_ACCESS_OK;
2133}
2134
2135static CPAccessResult access_aa32_tid1(CPUARMState *env, const ARMCPRegInfo *ri,
2136 bool isread)
2137{
2138 if (arm_feature(env, ARM_FEATURE_V8)) {
2139 return access_aa64_tid1(env, ri, isread);
2140 }
2141
2142 return CP_ACCESS_OK;
2143}
2144
e9aa6c21 2145static const ARMCPRegInfo v7_cp_reginfo[] = {
7d57f408
PM
2146 /* the old v6 WFI, UNPREDICTABLE in v7 but we choose to NOP */
2147 { .name = "NOP", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
2148 .access = PL1_W, .type = ARM_CP_NOP },
200ac0ef
PM
2149 /* Performance monitors are implementation defined in v7,
2150 * but with an ARM recommended set of registers, which we
ac689a2e 2151 * follow.
200ac0ef
PM
2152 *
2153 * Performance registers fall into three categories:
2154 * (a) always UNDEF in PL0, RW in PL1 (PMINTENSET, PMINTENCLR)
2155 * (b) RO in PL0 (ie UNDEF on write), RW in PL1 (PMUSERENR)
2156 * (c) UNDEF in PL0 if PMUSERENR.EN==0, otherwise accessible (all others)
2157 * For the cases controlled by PMUSERENR we must set .access to PL0_RW
2158 * or PL0_RO as appropriate and then check PMUSERENR in the helper fn.
2159 */
2160 { .name = "PMCNTENSET", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 1,
7a0e58fa 2161 .access = PL0_RW, .type = ARM_CP_ALIAS,
8521466b 2162 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
fcd25206
PM
2163 .writefn = pmcntenset_write,
2164 .accessfn = pmreg_access,
2165 .raw_writefn = raw_write },
8521466b
AF
2166 { .name = "PMCNTENSET_EL0", .state = ARM_CP_STATE_AA64,
2167 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 1,
2168 .access = PL0_RW, .accessfn = pmreg_access,
2169 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten), .resetvalue = 0,
2170 .writefn = pmcntenset_write, .raw_writefn = raw_write },
200ac0ef 2171 { .name = "PMCNTENCLR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 2,
8521466b
AF
2172 .access = PL0_RW,
2173 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
fcd25206
PM
2174 .accessfn = pmreg_access,
2175 .writefn = pmcntenclr_write,
7a0e58fa 2176 .type = ARM_CP_ALIAS },
8521466b
AF
2177 { .name = "PMCNTENCLR_EL0", .state = ARM_CP_STATE_AA64,
2178 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 2,
2179 .access = PL0_RW, .accessfn = pmreg_access,
7a0e58fa 2180 .type = ARM_CP_ALIAS,
8521466b
AF
2181 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten),
2182 .writefn = pmcntenclr_write },
200ac0ef 2183 { .name = "PMOVSR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 3,
f4efb4b2 2184 .access = PL0_RW, .type = ARM_CP_IO,
e4e91a21 2185 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr),
fcd25206
PM
2186 .accessfn = pmreg_access,
2187 .writefn = pmovsr_write,
2188 .raw_writefn = raw_write },
978364f1
AF
2189 { .name = "PMOVSCLR_EL0", .state = ARM_CP_STATE_AA64,
2190 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 3,
2191 .access = PL0_RW, .accessfn = pmreg_access,
f4efb4b2 2192 .type = ARM_CP_ALIAS | ARM_CP_IO,
978364f1
AF
2193 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
2194 .writefn = pmovsr_write,
2195 .raw_writefn = raw_write },
200ac0ef 2196 { .name = "PMSWINC", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 4,
f4efb4b2
AL
2197 .access = PL0_W, .accessfn = pmreg_access_swinc,
2198 .type = ARM_CP_NO_RAW | ARM_CP_IO,
0d4bfd7d
AL
2199 .writefn = pmswinc_write },
2200 { .name = "PMSWINC_EL0", .state = ARM_CP_STATE_AA64,
2201 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 4,
f4efb4b2
AL
2202 .access = PL0_W, .accessfn = pmreg_access_swinc,
2203 .type = ARM_CP_NO_RAW | ARM_CP_IO,
0d4bfd7d 2204 .writefn = pmswinc_write },
6b040780
WH
2205 { .name = "PMSELR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 5,
2206 .access = PL0_RW, .type = ARM_CP_ALIAS,
2207 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmselr),
6ecd0b6b 2208 .accessfn = pmreg_access_selr, .writefn = pmselr_write,
6b040780
WH
2209 .raw_writefn = raw_write},
2210 { .name = "PMSELR_EL0", .state = ARM_CP_STATE_AA64,
2211 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 5,
6ecd0b6b 2212 .access = PL0_RW, .accessfn = pmreg_access_selr,
6b040780
WH
2213 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmselr),
2214 .writefn = pmselr_write, .raw_writefn = raw_write, },
200ac0ef 2215 { .name = "PMCCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 0,
169c8938 2216 .access = PL0_RW, .resetvalue = 0, .type = ARM_CP_ALIAS | ARM_CP_IO,
421c7ebd 2217 .readfn = pmccntr_read, .writefn = pmccntr_write32,
6ecd0b6b 2218 .accessfn = pmreg_access_ccntr },
8521466b
AF
2219 { .name = "PMCCNTR_EL0", .state = ARM_CP_STATE_AA64,
2220 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 0,
6ecd0b6b 2221 .access = PL0_RW, .accessfn = pmreg_access_ccntr,
8521466b 2222 .type = ARM_CP_IO,
980ebe87
AL
2223 .fieldoffset = offsetof(CPUARMState, cp15.c15_ccnt),
2224 .readfn = pmccntr_read, .writefn = pmccntr_write,
2225 .raw_readfn = raw_read, .raw_writefn = raw_write, },
4b8afa1f
AL
2226 { .name = "PMCCFILTR", .cp = 15, .opc1 = 0, .crn = 14, .crm = 15, .opc2 = 7,
2227 .writefn = pmccfiltr_write_a32, .readfn = pmccfiltr_read_a32,
2228 .access = PL0_RW, .accessfn = pmreg_access,
2229 .type = ARM_CP_ALIAS | ARM_CP_IO,
2230 .resetvalue = 0, },
8521466b
AF
2231 { .name = "PMCCFILTR_EL0", .state = ARM_CP_STATE_AA64,
2232 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 15, .opc2 = 7,
980ebe87 2233 .writefn = pmccfiltr_write, .raw_writefn = raw_write,
8521466b
AF
2234 .access = PL0_RW, .accessfn = pmreg_access,
2235 .type = ARM_CP_IO,
2236 .fieldoffset = offsetof(CPUARMState, cp15.pmccfiltr_el0),
2237 .resetvalue = 0, },
200ac0ef 2238 { .name = "PMXEVTYPER", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 1,
5ecdd3e4
AL
2239 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2240 .accessfn = pmreg_access,
fdb86656
WH
2241 .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
2242 { .name = "PMXEVTYPER_EL0", .state = ARM_CP_STATE_AA64,
2243 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 1,
5ecdd3e4
AL
2244 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2245 .accessfn = pmreg_access,
fdb86656 2246 .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
200ac0ef 2247 { .name = "PMXEVCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 2,
5ecdd3e4
AL
2248 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2249 .accessfn = pmreg_access_xevcntr,
2250 .writefn = pmxevcntr_write, .readfn = pmxevcntr_read },
2251 { .name = "PMXEVCNTR_EL0", .state = ARM_CP_STATE_AA64,
2252 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 2,
2253 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2254 .accessfn = pmreg_access_xevcntr,
2255 .writefn = pmxevcntr_write, .readfn = pmxevcntr_read },
200ac0ef 2256 { .name = "PMUSERENR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 0,
1fce1ba9 2257 .access = PL0_R | PL1_RW, .accessfn = access_tpm,
e4e91a21 2258 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmuserenr),
200ac0ef 2259 .resetvalue = 0,
d4e6df63 2260 .writefn = pmuserenr_write, .raw_writefn = raw_write },
8a83ffc2
AF
2261 { .name = "PMUSERENR_EL0", .state = ARM_CP_STATE_AA64,
2262 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 0,
1fce1ba9 2263 .access = PL0_R | PL1_RW, .accessfn = access_tpm, .type = ARM_CP_ALIAS,
8a83ffc2
AF
2264 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmuserenr),
2265 .resetvalue = 0,
2266 .writefn = pmuserenr_write, .raw_writefn = raw_write },
200ac0ef 2267 { .name = "PMINTENSET", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 1,
1fce1ba9 2268 .access = PL1_RW, .accessfn = access_tpm,
b7d793ad 2269 .type = ARM_CP_ALIAS | ARM_CP_IO,
e6ec5457 2270 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pminten),
200ac0ef 2271 .resetvalue = 0,
d4e6df63 2272 .writefn = pmintenset_write, .raw_writefn = raw_write },
e6ec5457
WH
2273 { .name = "PMINTENSET_EL1", .state = ARM_CP_STATE_AA64,
2274 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 1,
2275 .access = PL1_RW, .accessfn = access_tpm,
2276 .type = ARM_CP_IO,
2277 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2278 .writefn = pmintenset_write, .raw_writefn = raw_write,
2279 .resetvalue = 0x0 },
200ac0ef 2280 { .name = "PMINTENCLR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 2,
fc5f6856
AL
2281 .access = PL1_RW, .accessfn = access_tpm,
2282 .type = ARM_CP_ALIAS | ARM_CP_IO,
200ac0ef 2283 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
b061a82b 2284 .writefn = pmintenclr_write, },
978364f1
AF
2285 { .name = "PMINTENCLR_EL1", .state = ARM_CP_STATE_AA64,
2286 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 2,
fc5f6856
AL
2287 .access = PL1_RW, .accessfn = access_tpm,
2288 .type = ARM_CP_ALIAS | ARM_CP_IO,
978364f1
AF
2289 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2290 .writefn = pmintenclr_write },
7da845b0
PM
2291 { .name = "CCSIDR", .state = ARM_CP_STATE_BOTH,
2292 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 0,
630fcd4d
MZ
2293 .access = PL1_R,
2294 .accessfn = access_aa64_tid2,
2295 .readfn = ccsidr_read, .type = ARM_CP_NO_RAW },
7da845b0
PM
2296 { .name = "CSSELR", .state = ARM_CP_STATE_BOTH,
2297 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 2, .opc2 = 0,
630fcd4d
MZ
2298 .access = PL1_RW,
2299 .accessfn = access_aa64_tid2,
2300 .writefn = csselr_write, .resetvalue = 0,
b85a1fd6
FA
2301 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.csselr_s),
2302 offsetof(CPUARMState, cp15.csselr_ns) } },
776d4e5c
PM
2303 /* Auxiliary ID register: this actually has an IMPDEF value but for now
2304 * just RAZ for all cores:
2305 */
0ff644a7
PM
2306 { .name = "AIDR", .state = ARM_CP_STATE_BOTH,
2307 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 7,
93fbc983
MZ
2308 .access = PL1_R, .type = ARM_CP_CONST,
2309 .accessfn = access_aa64_tid1,
2310 .resetvalue = 0 },
f32cdad5
PM
2311 /* Auxiliary fault status registers: these also are IMPDEF, and we
2312 * choose to RAZ/WI for all cores.
2313 */
2314 { .name = "AFSR0_EL1", .state = ARM_CP_STATE_BOTH,
2315 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 0,
84929218
RH
2316 .access = PL1_RW, .accessfn = access_tvm_trvm,
2317 .type = ARM_CP_CONST, .resetvalue = 0 },
f32cdad5
PM
2318 { .name = "AFSR1_EL1", .state = ARM_CP_STATE_BOTH,
2319 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 1,
84929218
RH
2320 .access = PL1_RW, .accessfn = access_tvm_trvm,
2321 .type = ARM_CP_CONST, .resetvalue = 0 },
b0fe2427
PM
2322 /* MAIR can just read-as-written because we don't implement caches
2323 * and so don't need to care about memory attributes.
2324 */
2325 { .name = "MAIR_EL1", .state = ARM_CP_STATE_AA64,
2326 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
84929218
RH
2327 .access = PL1_RW, .accessfn = access_tvm_trvm,
2328 .fieldoffset = offsetof(CPUARMState, cp15.mair_el[1]),
b0fe2427 2329 .resetvalue = 0 },
4cfb8ad8
PM
2330 { .name = "MAIR_EL3", .state = ARM_CP_STATE_AA64,
2331 .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 2, .opc2 = 0,
2332 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[3]),
2333 .resetvalue = 0 },
b0fe2427
PM
2334 /* For non-long-descriptor page tables these are PRRR and NMRR;
2335 * regardless they still act as reads-as-written for QEMU.
b0fe2427 2336 */
1281f8e3 2337 /* MAIR0/1 are defined separately from their 64-bit counterpart which
be693c87
GB
2338 * allows them to assign the correct fieldoffset based on the endianness
2339 * handled in the field definitions.
2340 */
a903c449 2341 { .name = "MAIR0", .state = ARM_CP_STATE_AA32,
84929218
RH
2342 .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
2343 .access = PL1_RW, .accessfn = access_tvm_trvm,
be693c87
GB
2344 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair0_s),
2345 offsetof(CPUARMState, cp15.mair0_ns) },
b0fe2427 2346 .resetfn = arm_cp_reset_ignore },
a903c449 2347 { .name = "MAIR1", .state = ARM_CP_STATE_AA32,
84929218
RH
2348 .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 1,
2349 .access = PL1_RW, .accessfn = access_tvm_trvm,
be693c87
GB
2350 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair1_s),
2351 offsetof(CPUARMState, cp15.mair1_ns) },
b0fe2427 2352 .resetfn = arm_cp_reset_ignore },
1090b9c6
PM
2353 { .name = "ISR_EL1", .state = ARM_CP_STATE_BOTH,
2354 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 0,
7a0e58fa 2355 .type = ARM_CP_NO_RAW, .access = PL1_R, .readfn = isr_read },
995939a6
PM
2356 /* 32 bit ITLB invalidates */
2357 { .name = "ITLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 0,
30881b73
RH
2358 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2359 .writefn = tlbiall_write },
995939a6 2360 { .name = "ITLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 1,
30881b73
RH
2361 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2362 .writefn = tlbimva_write },
995939a6 2363 { .name = "ITLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 2,
30881b73
RH
2364 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2365 .writefn = tlbiasid_write },
995939a6
PM
2366 /* 32 bit DTLB invalidates */
2367 { .name = "DTLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 0,
30881b73
RH
2368 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2369 .writefn = tlbiall_write },
995939a6 2370 { .name = "DTLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 1,
30881b73
RH
2371 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2372 .writefn = tlbimva_write },
995939a6 2373 { .name = "DTLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 2,
30881b73
RH
2374 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2375 .writefn = tlbiasid_write },
995939a6
PM
2376 /* 32 bit TLB invalidates */
2377 { .name = "TLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
30881b73
RH
2378 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2379 .writefn = tlbiall_write },
995939a6 2380 { .name = "TLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
30881b73
RH
2381 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2382 .writefn = tlbimva_write },
995939a6 2383 { .name = "TLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
30881b73
RH
2384 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2385 .writefn = tlbiasid_write },
995939a6 2386 { .name = "TLBIMVAA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
30881b73
RH
2387 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2388 .writefn = tlbimvaa_write },
995939a6
PM
2389 REGINFO_SENTINEL
2390};
2391
2392static const ARMCPRegInfo v7mp_cp_reginfo[] = {
2393 /* 32 bit TLB invalidates, Inner Shareable */
2394 { .name = "TLBIALLIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
30881b73
RH
2395 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2396 .writefn = tlbiall_is_write },
995939a6 2397 { .name = "TLBIMVAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
30881b73
RH
2398 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2399 .writefn = tlbimva_is_write },
995939a6 2400 { .name = "TLBIASIDIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
30881b73 2401 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
fa439fc5 2402 .writefn = tlbiasid_is_write },
995939a6 2403 { .name = "TLBIMVAAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
30881b73 2404 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
fa439fc5 2405 .writefn = tlbimvaa_is_write },
e9aa6c21
PM
2406 REGINFO_SENTINEL
2407};
2408
327dd510
AL
2409static const ARMCPRegInfo pmovsset_cp_reginfo[] = {
2410 /* PMOVSSET is not implemented in v7 before v7ve */
2411 { .name = "PMOVSSET", .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 3,
2412 .access = PL0_RW, .accessfn = pmreg_access,
f4efb4b2 2413 .type = ARM_CP_ALIAS | ARM_CP_IO,
327dd510
AL
2414 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr),
2415 .writefn = pmovsset_write,
2416 .raw_writefn = raw_write },
2417 { .name = "PMOVSSET_EL0", .state = ARM_CP_STATE_AA64,
2418 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 3,
2419 .access = PL0_RW, .accessfn = pmreg_access,
f4efb4b2 2420 .type = ARM_CP_ALIAS | ARM_CP_IO,
327dd510
AL
2421 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
2422 .writefn = pmovsset_write,
2423 .raw_writefn = raw_write },
2424 REGINFO_SENTINEL
2425};
2426
c4241c7d
PM
2427static void teecr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2428 uint64_t value)
c326b979
PM
2429{
2430 value &= 1;
2431 env->teecr = value;
c326b979
PM
2432}
2433
3f208fd7
PM
2434static CPAccessResult teehbr_access(CPUARMState *env, const ARMCPRegInfo *ri,
2435 bool isread)
c326b979 2436{
dcbff19b 2437 if (arm_current_el(env) == 0 && (env->teecr & 1)) {
92611c00 2438 return CP_ACCESS_TRAP;
c326b979 2439 }
92611c00 2440 return CP_ACCESS_OK;
c326b979
PM
2441}
2442
2443static const ARMCPRegInfo t2ee_cp_reginfo[] = {
2444 { .name = "TEECR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 6, .opc2 = 0,
2445 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, teecr),
2446 .resetvalue = 0,
2447 .writefn = teecr_write },
2448 { .name = "TEEHBR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 6, .opc2 = 0,
2449 .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, teehbr),
92611c00 2450 .accessfn = teehbr_access, .resetvalue = 0 },
c326b979
PM
2451 REGINFO_SENTINEL
2452};
2453
4d31c596 2454static const ARMCPRegInfo v6k_cp_reginfo[] = {
e4fe830b
PM
2455 { .name = "TPIDR_EL0", .state = ARM_CP_STATE_AA64,
2456 .opc0 = 3, .opc1 = 3, .opc2 = 2, .crn = 13, .crm = 0,
2457 .access = PL0_RW,
54bf36ed 2458 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[0]), .resetvalue = 0 },
4d31c596
PM
2459 { .name = "TPIDRURW", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 2,
2460 .access = PL0_RW,
54bf36ed
FA
2461 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrurw_s),
2462 offsetoflow32(CPUARMState, cp15.tpidrurw_ns) },
e4fe830b
PM
2463 .resetfn = arm_cp_reset_ignore },
2464 { .name = "TPIDRRO_EL0", .state = ARM_CP_STATE_AA64,
2465 .opc0 = 3, .opc1 = 3, .opc2 = 3, .crn = 13, .crm = 0,
2466 .access = PL0_R|PL1_W,
54bf36ed
FA
2467 .fieldoffset = offsetof(CPUARMState, cp15.tpidrro_el[0]),
2468 .resetvalue = 0},
4d31c596
PM
2469 { .name = "TPIDRURO", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 3,
2470 .access = PL0_R|PL1_W,
54bf36ed
FA
2471 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidruro_s),
2472 offsetoflow32(CPUARMState, cp15.tpidruro_ns) },
e4fe830b 2473 .resetfn = arm_cp_reset_ignore },
54bf36ed 2474 { .name = "TPIDR_EL1", .state = ARM_CP_STATE_AA64,
e4fe830b 2475 .opc0 = 3, .opc1 = 0, .opc2 = 4, .crn = 13, .crm = 0,
4d31c596 2476 .access = PL1_RW,
54bf36ed
FA
2477 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[1]), .resetvalue = 0 },
2478 { .name = "TPIDRPRW", .opc1 = 0, .cp = 15, .crn = 13, .crm = 0, .opc2 = 4,
2479 .access = PL1_RW,
2480 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrprw_s),
2481 offsetoflow32(CPUARMState, cp15.tpidrprw_ns) },
2482 .resetvalue = 0 },
4d31c596
PM
2483 REGINFO_SENTINEL
2484};
2485
55d284af
PM
2486#ifndef CONFIG_USER_ONLY
2487
3f208fd7
PM
2488static CPAccessResult gt_cntfrq_access(CPUARMState *env, const ARMCPRegInfo *ri,
2489 bool isread)
00108f2d 2490{
75502672
PM
2491 /* CNTFRQ: not visible from PL0 if both PL0PCTEN and PL0VCTEN are zero.
2492 * Writable only at the highest implemented exception level.
2493 */
2494 int el = arm_current_el(env);
5bc84371
RH
2495 uint64_t hcr;
2496 uint32_t cntkctl;
75502672
PM
2497
2498 switch (el) {
2499 case 0:
5bc84371
RH
2500 hcr = arm_hcr_el2_eff(env);
2501 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2502 cntkctl = env->cp15.cnthctl_el2;
2503 } else {
2504 cntkctl = env->cp15.c14_cntkctl;
2505 }
2506 if (!extract32(cntkctl, 0, 2)) {
75502672
PM
2507 return CP_ACCESS_TRAP;
2508 }
2509 break;
2510 case 1:
2511 if (!isread && ri->state == ARM_CP_STATE_AA32 &&
2512 arm_is_secure_below_el3(env)) {
2513 /* Accesses from 32-bit Secure EL1 UNDEF (*not* trap to EL3!) */
2514 return CP_ACCESS_TRAP_UNCATEGORIZED;
2515 }
2516 break;
2517 case 2:
2518 case 3:
2519 break;
00108f2d 2520 }
75502672
PM
2521
2522 if (!isread && el < arm_highest_el(env)) {
2523 return CP_ACCESS_TRAP_UNCATEGORIZED;
2524 }
2525
00108f2d
PM
2526 return CP_ACCESS_OK;
2527}
2528
3f208fd7
PM
2529static CPAccessResult gt_counter_access(CPUARMState *env, int timeridx,
2530 bool isread)
00108f2d 2531{
0b6440af
EI
2532 unsigned int cur_el = arm_current_el(env);
2533 bool secure = arm_is_secure(env);
5bc84371 2534 uint64_t hcr = arm_hcr_el2_eff(env);
0b6440af 2535
5bc84371
RH
2536 switch (cur_el) {
2537 case 0:
2538 /* If HCR_EL2.<E2H,TGE> == '11': check CNTHCTL_EL2.EL0[PV]CTEN. */
2539 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2540 return (extract32(env->cp15.cnthctl_el2, timeridx, 1)
2541 ? CP_ACCESS_OK : CP_ACCESS_TRAP_EL2);
2542 }
0b6440af 2543
5bc84371
RH
2544 /* CNT[PV]CT: not visible from PL0 if EL0[PV]CTEN is zero */
2545 if (!extract32(env->cp15.c14_cntkctl, timeridx, 1)) {
2546 return CP_ACCESS_TRAP;
2547 }
2548
2549 /* If HCR_EL2.<E2H,TGE> == '10': check CNTHCTL_EL2.EL1PCTEN. */
2550 if (hcr & HCR_E2H) {
2551 if (timeridx == GTIMER_PHYS &&
2552 !extract32(env->cp15.cnthctl_el2, 10, 1)) {
2553 return CP_ACCESS_TRAP_EL2;
2554 }
2555 } else {
2556 /* If HCR_EL2.<E2H> == 0: check CNTHCTL_EL2.EL1PCEN. */
2557 if (arm_feature(env, ARM_FEATURE_EL2) &&
2558 timeridx == GTIMER_PHYS && !secure &&
2559 !extract32(env->cp15.cnthctl_el2, 1, 1)) {
2560 return CP_ACCESS_TRAP_EL2;
2561 }
2562 }
2563 break;
2564
2565 case 1:
2566 /* Check CNTHCTL_EL2.EL1PCTEN, which changes location based on E2H. */
2567 if (arm_feature(env, ARM_FEATURE_EL2) &&
2568 timeridx == GTIMER_PHYS && !secure &&
2569 (hcr & HCR_E2H
2570 ? !extract32(env->cp15.cnthctl_el2, 10, 1)
2571 : !extract32(env->cp15.cnthctl_el2, 0, 1))) {
2572 return CP_ACCESS_TRAP_EL2;
2573 }
2574 break;
0b6440af 2575 }
00108f2d
PM
2576 return CP_ACCESS_OK;
2577}
2578
3f208fd7
PM
2579static CPAccessResult gt_timer_access(CPUARMState *env, int timeridx,
2580 bool isread)
00108f2d 2581{
0b6440af
EI
2582 unsigned int cur_el = arm_current_el(env);
2583 bool secure = arm_is_secure(env);
5bc84371 2584 uint64_t hcr = arm_hcr_el2_eff(env);
0b6440af 2585
5bc84371
RH
2586 switch (cur_el) {
2587 case 0:
2588 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2589 /* If HCR_EL2.<E2H,TGE> == '11': check CNTHCTL_EL2.EL0[PV]TEN. */
2590 return (extract32(env->cp15.cnthctl_el2, 9 - timeridx, 1)
2591 ? CP_ACCESS_OK : CP_ACCESS_TRAP_EL2);
2592 }
0b6440af 2593
5bc84371
RH
2594 /*
2595 * CNT[PV]_CVAL, CNT[PV]_CTL, CNT[PV]_TVAL: not visible from
2596 * EL0 if EL0[PV]TEN is zero.
2597 */
2598 if (!extract32(env->cp15.c14_cntkctl, 9 - timeridx, 1)) {
2599 return CP_ACCESS_TRAP;
2600 }
2601 /* fall through */
2602
2603 case 1:
2604 if (arm_feature(env, ARM_FEATURE_EL2) &&
2605 timeridx == GTIMER_PHYS && !secure) {
2606 if (hcr & HCR_E2H) {
2607 /* If HCR_EL2.<E2H,TGE> == '10': check CNTHCTL_EL2.EL1PTEN. */
2608 if (!extract32(env->cp15.cnthctl_el2, 11, 1)) {
2609 return CP_ACCESS_TRAP_EL2;
2610 }
2611 } else {
2612 /* If HCR_EL2.<E2H> == 0: check CNTHCTL_EL2.EL1PCEN. */
2613 if (!extract32(env->cp15.cnthctl_el2, 1, 1)) {
2614 return CP_ACCESS_TRAP_EL2;
2615 }
2616 }
2617 }
2618 break;
0b6440af 2619 }
00108f2d
PM
2620 return CP_ACCESS_OK;
2621}
2622
2623static CPAccessResult gt_pct_access(CPUARMState *env,
3f208fd7
PM
2624 const ARMCPRegInfo *ri,
2625 bool isread)
00108f2d 2626{
3f208fd7 2627 return gt_counter_access(env, GTIMER_PHYS, isread);
00108f2d
PM
2628}
2629
2630static CPAccessResult gt_vct_access(CPUARMState *env,
3f208fd7
PM
2631 const ARMCPRegInfo *ri,
2632 bool isread)
00108f2d 2633{
3f208fd7 2634 return gt_counter_access(env, GTIMER_VIRT, isread);
00108f2d
PM
2635}
2636
3f208fd7
PM
2637static CPAccessResult gt_ptimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
2638 bool isread)
00108f2d 2639{
3f208fd7 2640 return gt_timer_access(env, GTIMER_PHYS, isread);
00108f2d
PM
2641}
2642
3f208fd7
PM
2643static CPAccessResult gt_vtimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
2644 bool isread)
00108f2d 2645{
3f208fd7 2646 return gt_timer_access(env, GTIMER_VIRT, isread);
00108f2d
PM
2647}
2648
b4d3978c 2649static CPAccessResult gt_stimer_access(CPUARMState *env,
3f208fd7
PM
2650 const ARMCPRegInfo *ri,
2651 bool isread)
b4d3978c
PM
2652{
2653 /* The AArch64 register view of the secure physical timer is
2654 * always accessible from EL3, and configurably accessible from
2655 * Secure EL1.
2656 */
2657 switch (arm_current_el(env)) {
2658 case 1:
2659 if (!arm_is_secure(env)) {
2660 return CP_ACCESS_TRAP;
2661 }
2662 if (!(env->cp15.scr_el3 & SCR_ST)) {
2663 return CP_ACCESS_TRAP_EL3;
2664 }
2665 return CP_ACCESS_OK;
2666 case 0:
2667 case 2:
2668 return CP_ACCESS_TRAP;
2669 case 3:
2670 return CP_ACCESS_OK;
2671 default:
2672 g_assert_not_reached();
2673 }
2674}
2675
55d284af
PM
2676static uint64_t gt_get_countervalue(CPUARMState *env)
2677{
7def8754
AJ
2678 ARMCPU *cpu = env_archcpu(env);
2679
2680 return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) / gt_cntfrq_period_ns(cpu);
55d284af
PM
2681}
2682
2683static void gt_recalc_timer(ARMCPU *cpu, int timeridx)
2684{
2685 ARMGenericTimer *gt = &cpu->env.cp15.c14_timer[timeridx];
2686
2687 if (gt->ctl & 1) {
2688 /* Timer enabled: calculate and set current ISTATUS, irq, and
2689 * reset timer to when ISTATUS next has to change
2690 */
edac4d8a
EI
2691 uint64_t offset = timeridx == GTIMER_VIRT ?
2692 cpu->env.cp15.cntvoff_el2 : 0;
55d284af
PM
2693 uint64_t count = gt_get_countervalue(&cpu->env);
2694 /* Note that this must be unsigned 64 bit arithmetic: */
edac4d8a 2695 int istatus = count - offset >= gt->cval;
55d284af 2696 uint64_t nexttick;
194cbc49 2697 int irqstate;
55d284af
PM
2698
2699 gt->ctl = deposit32(gt->ctl, 2, 1, istatus);
194cbc49
PM
2700
2701 irqstate = (istatus && !(gt->ctl & 2));
2702 qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate);
2703
55d284af
PM
2704 if (istatus) {
2705 /* Next transition is when count rolls back over to zero */
2706 nexttick = UINT64_MAX;
2707 } else {
2708 /* Next transition is when we hit cval */
edac4d8a 2709 nexttick = gt->cval + offset;
55d284af
PM
2710 }
2711 /* Note that the desired next expiry time might be beyond the
2712 * signed-64-bit range of a QEMUTimer -- in this case we just
2713 * set the timer for as far in the future as possible. When the
2714 * timer expires we will reset the timer for any remaining period.
2715 */
7def8754 2716 if (nexttick > INT64_MAX / gt_cntfrq_period_ns(cpu)) {
4a0245b6
AJ
2717 timer_mod_ns(cpu->gt_timer[timeridx], INT64_MAX);
2718 } else {
2719 timer_mod(cpu->gt_timer[timeridx], nexttick);
55d284af 2720 }
194cbc49 2721 trace_arm_gt_recalc(timeridx, irqstate, nexttick);
55d284af
PM
2722 } else {
2723 /* Timer disabled: ISTATUS and timer output always clear */
2724 gt->ctl &= ~4;
2725 qemu_set_irq(cpu->gt_timer_outputs[timeridx], 0);
bc72ad67 2726 timer_del(cpu->gt_timer[timeridx]);
194cbc49 2727 trace_arm_gt_recalc_disabled(timeridx);
55d284af
PM
2728 }
2729}
2730
0e3eca4c
EI
2731static void gt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri,
2732 int timeridx)
55d284af 2733{
2fc0cc0e 2734 ARMCPU *cpu = env_archcpu(env);
55d284af 2735
bc72ad67 2736 timer_del(cpu->gt_timer[timeridx]);
55d284af
PM
2737}
2738
c4241c7d 2739static uint64_t gt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
55d284af 2740{
c4241c7d 2741 return gt_get_countervalue(env);
55d284af
PM
2742}
2743
53d1f856
RH
2744static uint64_t gt_virt_cnt_offset(CPUARMState *env)
2745{
2746 uint64_t hcr;
2747
2748 switch (arm_current_el(env)) {
2749 case 2:
2750 hcr = arm_hcr_el2_eff(env);
2751 if (hcr & HCR_E2H) {
2752 return 0;
2753 }
2754 break;
2755 case 0:
2756 hcr = arm_hcr_el2_eff(env);
2757 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2758 return 0;
2759 }
2760 break;
2761 }
2762
2763 return env->cp15.cntvoff_el2;
2764}
2765
edac4d8a
EI
2766static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
2767{
53d1f856 2768 return gt_get_countervalue(env) - gt_virt_cnt_offset(env);
edac4d8a
EI
2769}
2770
c4241c7d 2771static void gt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
0e3eca4c 2772 int timeridx,
c4241c7d 2773 uint64_t value)
55d284af 2774{
194cbc49 2775 trace_arm_gt_cval_write(timeridx, value);
55d284af 2776 env->cp15.c14_timer[timeridx].cval = value;
2fc0cc0e 2777 gt_recalc_timer(env_archcpu(env), timeridx);
55d284af 2778}
c4241c7d 2779
0e3eca4c
EI
2780static uint64_t gt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri,
2781 int timeridx)
55d284af 2782{
53d1f856
RH
2783 uint64_t offset = 0;
2784
2785 switch (timeridx) {
2786 case GTIMER_VIRT:
8c94b071 2787 case GTIMER_HYPVIRT:
53d1f856
RH
2788 offset = gt_virt_cnt_offset(env);
2789 break;
2790 }
55d284af 2791
c4241c7d 2792 return (uint32_t)(env->cp15.c14_timer[timeridx].cval -
edac4d8a 2793 (gt_get_countervalue(env) - offset));
55d284af
PM
2794}
2795
c4241c7d 2796static void gt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
0e3eca4c 2797 int timeridx,
c4241c7d 2798 uint64_t value)
55d284af 2799{
53d1f856
RH
2800 uint64_t offset = 0;
2801
2802 switch (timeridx) {
2803 case GTIMER_VIRT:
8c94b071 2804 case GTIMER_HYPVIRT:
53d1f856
RH
2805 offset = gt_virt_cnt_offset(env);
2806 break;
2807 }
55d284af 2808
194cbc49 2809 trace_arm_gt_tval_write(timeridx, value);
edac4d8a 2810 env->cp15.c14_timer[timeridx].cval = gt_get_countervalue(env) - offset +
18084b2f 2811 sextract64(value, 0, 32);
2fc0cc0e 2812 gt_recalc_timer(env_archcpu(env), timeridx);
55d284af
PM
2813}
2814
c4241c7d 2815static void gt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
0e3eca4c 2816 int timeridx,
c4241c7d 2817 uint64_t value)
55d284af 2818{
2fc0cc0e 2819 ARMCPU *cpu = env_archcpu(env);
55d284af
PM
2820 uint32_t oldval = env->cp15.c14_timer[timeridx].ctl;
2821
194cbc49 2822 trace_arm_gt_ctl_write(timeridx, value);
d3afacc7 2823 env->cp15.c14_timer[timeridx].ctl = deposit64(oldval, 0, 2, value);
55d284af
PM
2824 if ((oldval ^ value) & 1) {
2825 /* Enable toggled */
2826 gt_recalc_timer(cpu, timeridx);
d3afacc7 2827 } else if ((oldval ^ value) & 2) {
55d284af
PM
2828 /* IMASK toggled: don't need to recalculate,
2829 * just set the interrupt line based on ISTATUS
2830 */
194cbc49
PM
2831 int irqstate = (oldval & 4) && !(value & 2);
2832
2833 trace_arm_gt_imask_toggle(timeridx, irqstate);
2834 qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate);
55d284af 2835 }
55d284af
PM
2836}
2837
0e3eca4c
EI
2838static void gt_phys_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2839{
2840 gt_timer_reset(env, ri, GTIMER_PHYS);
2841}
2842
2843static void gt_phys_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2844 uint64_t value)
2845{
2846 gt_cval_write(env, ri, GTIMER_PHYS, value);
2847}
2848
2849static uint64_t gt_phys_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2850{
2851 return gt_tval_read(env, ri, GTIMER_PHYS);
2852}
2853
2854static void gt_phys_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2855 uint64_t value)
2856{
2857 gt_tval_write(env, ri, GTIMER_PHYS, value);
2858}
2859
2860static void gt_phys_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2861 uint64_t value)
2862{
2863 gt_ctl_write(env, ri, GTIMER_PHYS, value);
2864}
2865
bb5972e4
RH
2866static int gt_phys_redir_timeridx(CPUARMState *env)
2867{
2868 switch (arm_mmu_idx(env)) {
2869 case ARMMMUIdx_E20_0:
2870 case ARMMMUIdx_E20_2:
452ef8cb 2871 case ARMMMUIdx_E20_2_PAN:
bb5972e4
RH
2872 return GTIMER_HYP;
2873 default:
2874 return GTIMER_PHYS;
2875 }
2876}
2877
2878static int gt_virt_redir_timeridx(CPUARMState *env)
2879{
2880 switch (arm_mmu_idx(env)) {
2881 case ARMMMUIdx_E20_0:
2882 case ARMMMUIdx_E20_2:
452ef8cb 2883 case ARMMMUIdx_E20_2_PAN:
bb5972e4
RH
2884 return GTIMER_HYPVIRT;
2885 default:
2886 return GTIMER_VIRT;
2887 }
2888}
2889
2890static uint64_t gt_phys_redir_cval_read(CPUARMState *env,
2891 const ARMCPRegInfo *ri)
2892{
2893 int timeridx = gt_phys_redir_timeridx(env);
2894 return env->cp15.c14_timer[timeridx].cval;
2895}
2896
2897static void gt_phys_redir_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2898 uint64_t value)
2899{
2900 int timeridx = gt_phys_redir_timeridx(env);
2901 gt_cval_write(env, ri, timeridx, value);
2902}
2903
2904static uint64_t gt_phys_redir_tval_read(CPUARMState *env,
2905 const ARMCPRegInfo *ri)
2906{
2907 int timeridx = gt_phys_redir_timeridx(env);
2908 return gt_tval_read(env, ri, timeridx);
2909}
2910
2911static void gt_phys_redir_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2912 uint64_t value)
2913{
2914 int timeridx = gt_phys_redir_timeridx(env);
2915 gt_tval_write(env, ri, timeridx, value);
2916}
2917
2918static uint64_t gt_phys_redir_ctl_read(CPUARMState *env,
2919 const ARMCPRegInfo *ri)
2920{
2921 int timeridx = gt_phys_redir_timeridx(env);
2922 return env->cp15.c14_timer[timeridx].ctl;
2923}
2924
2925static void gt_phys_redir_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2926 uint64_t value)
2927{
2928 int timeridx = gt_phys_redir_timeridx(env);
2929 gt_ctl_write(env, ri, timeridx, value);
2930}
2931
0e3eca4c
EI
2932static void gt_virt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2933{
2934 gt_timer_reset(env, ri, GTIMER_VIRT);
2935}
2936
2937static void gt_virt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2938 uint64_t value)
2939{
2940 gt_cval_write(env, ri, GTIMER_VIRT, value);
2941}
2942
2943static uint64_t gt_virt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2944{
2945 return gt_tval_read(env, ri, GTIMER_VIRT);
2946}
2947
2948static void gt_virt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2949 uint64_t value)
2950{
2951 gt_tval_write(env, ri, GTIMER_VIRT, value);
2952}
2953
2954static void gt_virt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2955 uint64_t value)
2956{
2957 gt_ctl_write(env, ri, GTIMER_VIRT, value);
2958}
2959
edac4d8a
EI
2960static void gt_cntvoff_write(CPUARMState *env, const ARMCPRegInfo *ri,
2961 uint64_t value)
2962{
2fc0cc0e 2963 ARMCPU *cpu = env_archcpu(env);
edac4d8a 2964
194cbc49 2965 trace_arm_gt_cntvoff_write(value);
edac4d8a
EI
2966 raw_write(env, ri, value);
2967 gt_recalc_timer(cpu, GTIMER_VIRT);
2968}
2969
bb5972e4
RH
2970static uint64_t gt_virt_redir_cval_read(CPUARMState *env,
2971 const ARMCPRegInfo *ri)
2972{
2973 int timeridx = gt_virt_redir_timeridx(env);
2974 return env->cp15.c14_timer[timeridx].cval;
2975}
2976
2977static void gt_virt_redir_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2978 uint64_t value)
2979{
2980 int timeridx = gt_virt_redir_timeridx(env);
2981 gt_cval_write(env, ri, timeridx, value);
2982}
2983
2984static uint64_t gt_virt_redir_tval_read(CPUARMState *env,
2985 const ARMCPRegInfo *ri)
2986{
2987 int timeridx = gt_virt_redir_timeridx(env);
2988 return gt_tval_read(env, ri, timeridx);
2989}
2990
2991static void gt_virt_redir_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2992 uint64_t value)
2993{
2994 int timeridx = gt_virt_redir_timeridx(env);
2995 gt_tval_write(env, ri, timeridx, value);
2996}
2997
2998static uint64_t gt_virt_redir_ctl_read(CPUARMState *env,
2999 const ARMCPRegInfo *ri)
3000{
3001 int timeridx = gt_virt_redir_timeridx(env);
3002 return env->cp15.c14_timer[timeridx].ctl;
3003}
3004
3005static void gt_virt_redir_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
3006 uint64_t value)
3007{
3008 int timeridx = gt_virt_redir_timeridx(env);
3009 gt_ctl_write(env, ri, timeridx, value);
3010}
3011
b0e66d95
EI
3012static void gt_hyp_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
3013{
3014 gt_timer_reset(env, ri, GTIMER_HYP);
3015}
3016
3017static void gt_hyp_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3018 uint64_t value)
3019{
3020 gt_cval_write(env, ri, GTIMER_HYP, value);
3021}
3022
3023static uint64_t gt_hyp_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
3024{
3025 return gt_tval_read(env, ri, GTIMER_HYP);
3026}
3027
3028static void gt_hyp_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3029 uint64_t value)
3030{
3031 gt_tval_write(env, ri, GTIMER_HYP, value);
3032}
3033
3034static void gt_hyp_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
3035 uint64_t value)
3036{
3037 gt_ctl_write(env, ri, GTIMER_HYP, value);
3038}
3039
b4d3978c
PM
3040static void gt_sec_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
3041{
3042 gt_timer_reset(env, ri, GTIMER_SEC);
3043}
3044
3045static void gt_sec_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3046 uint64_t value)
3047{
3048 gt_cval_write(env, ri, GTIMER_SEC, value);
3049}
3050
3051static uint64_t gt_sec_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
3052{
3053 return gt_tval_read(env, ri, GTIMER_SEC);
3054}
3055
3056static void gt_sec_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3057 uint64_t value)
3058{
3059 gt_tval_write(env, ri, GTIMER_SEC, value);
3060}
3061
3062static void gt_sec_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
3063 uint64_t value)
3064{
3065 gt_ctl_write(env, ri, GTIMER_SEC, value);
3066}
3067
8c94b071
RH
3068static void gt_hv_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
3069{
3070 gt_timer_reset(env, ri, GTIMER_HYPVIRT);
3071}
3072
3073static void gt_hv_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3074 uint64_t value)
3075{
3076 gt_cval_write(env, ri, GTIMER_HYPVIRT, value);
3077}
3078
3079static uint64_t gt_hv_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
3080{
3081 return gt_tval_read(env, ri, GTIMER_HYPVIRT);
3082}
3083
3084static void gt_hv_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3085 uint64_t value)
3086{
3087 gt_tval_write(env, ri, GTIMER_HYPVIRT, value);
3088}
3089
3090static void gt_hv_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
3091 uint64_t value)
3092{
3093 gt_ctl_write(env, ri, GTIMER_HYPVIRT, value);
3094}
3095
55d284af
PM
3096void arm_gt_ptimer_cb(void *opaque)
3097{
3098 ARMCPU *cpu = opaque;
3099
3100 gt_recalc_timer(cpu, GTIMER_PHYS);
3101}
3102
3103void arm_gt_vtimer_cb(void *opaque)
3104{
3105 ARMCPU *cpu = opaque;
3106
3107 gt_recalc_timer(cpu, GTIMER_VIRT);
3108}
3109
b0e66d95
EI
3110void arm_gt_htimer_cb(void *opaque)
3111{
3112 ARMCPU *cpu = opaque;
3113
3114 gt_recalc_timer(cpu, GTIMER_HYP);
3115}
3116
b4d3978c
PM
3117void arm_gt_stimer_cb(void *opaque)
3118{
3119 ARMCPU *cpu = opaque;
3120
3121 gt_recalc_timer(cpu, GTIMER_SEC);
3122}
3123
8c94b071
RH
3124void arm_gt_hvtimer_cb(void *opaque)
3125{
3126 ARMCPU *cpu = opaque;
3127
3128 gt_recalc_timer(cpu, GTIMER_HYPVIRT);
3129}
3130
96eec6b2
AJ
3131static void arm_gt_cntfrq_reset(CPUARMState *env, const ARMCPRegInfo *opaque)
3132{
3133 ARMCPU *cpu = env_archcpu(env);
3134
3135 cpu->env.cp15.c14_cntfrq = cpu->gt_cntfrq_hz;
3136}
3137
55d284af
PM
3138static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
3139 /* Note that CNTFRQ is purely reads-as-written for the benefit
3140 * of software; writing it doesn't actually change the timer frequency.
3141 * Our reset value matches the fixed frequency we implement the timer at.
3142 */
3143 { .name = "CNTFRQ", .cp = 15, .crn = 14, .crm = 0, .opc1 = 0, .opc2 = 0,
7a0e58fa 3144 .type = ARM_CP_ALIAS,
a7adc4b7
PM
3145 .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
3146 .fieldoffset = offsetoflow32(CPUARMState, cp15.c14_cntfrq),
a7adc4b7
PM
3147 },
3148 { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
3149 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
3150 .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
55d284af 3151 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
96eec6b2 3152 .resetfn = arm_gt_cntfrq_reset,
55d284af
PM
3153 },
3154 /* overall control: mostly access permissions */
a7adc4b7
PM
3155 { .name = "CNTKCTL", .state = ARM_CP_STATE_BOTH,
3156 .opc0 = 3, .opc1 = 0, .crn = 14, .crm = 1, .opc2 = 0,
55d284af
PM
3157 .access = PL1_RW,
3158 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntkctl),
3159 .resetvalue = 0,
3160 },
3161 /* per-timer control */
3162 { .name = "CNTP_CTL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
9ff9dd3c 3163 .secure = ARM_CP_SECSTATE_NS,
daf1dc5f 3164 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
a7adc4b7
PM
3165 .accessfn = gt_ptimer_access,
3166 .fieldoffset = offsetoflow32(CPUARMState,
3167 cp15.c14_timer[GTIMER_PHYS].ctl),
bb5972e4
RH
3168 .readfn = gt_phys_redir_ctl_read, .raw_readfn = raw_read,
3169 .writefn = gt_phys_redir_ctl_write, .raw_writefn = raw_write,
a7adc4b7 3170 },
9c513e78 3171 { .name = "CNTP_CTL_S",
9ff9dd3c
PM
3172 .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
3173 .secure = ARM_CP_SECSTATE_S,
daf1dc5f 3174 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
9ff9dd3c
PM
3175 .accessfn = gt_ptimer_access,
3176 .fieldoffset = offsetoflow32(CPUARMState,
3177 cp15.c14_timer[GTIMER_SEC].ctl),
3178 .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
3179 },
a7adc4b7
PM
3180 { .name = "CNTP_CTL_EL0", .state = ARM_CP_STATE_AA64,
3181 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 1,
daf1dc5f 3182 .type = ARM_CP_IO, .access = PL0_RW,
a7adc4b7 3183 .accessfn = gt_ptimer_access,
55d284af
PM
3184 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
3185 .resetvalue = 0,
bb5972e4
RH
3186 .readfn = gt_phys_redir_ctl_read, .raw_readfn = raw_read,
3187 .writefn = gt_phys_redir_ctl_write, .raw_writefn = raw_write,
55d284af
PM
3188 },
3189 { .name = "CNTV_CTL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 1,
daf1dc5f 3190 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
a7adc4b7
PM
3191 .accessfn = gt_vtimer_access,
3192 .fieldoffset = offsetoflow32(CPUARMState,
3193 cp15.c14_timer[GTIMER_VIRT].ctl),
bb5972e4
RH
3194 .readfn = gt_virt_redir_ctl_read, .raw_readfn = raw_read,
3195 .writefn = gt_virt_redir_ctl_write, .raw_writefn = raw_write,
a7adc4b7
PM
3196 },
3197 { .name = "CNTV_CTL_EL0", .state = ARM_CP_STATE_AA64,
3198 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 1,
daf1dc5f 3199 .type = ARM_CP_IO, .access = PL0_RW,
a7adc4b7 3200 .accessfn = gt_vtimer_access,
55d284af
PM
3201 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
3202 .resetvalue = 0,
bb5972e4
RH
3203 .readfn = gt_virt_redir_ctl_read, .raw_readfn = raw_read,
3204 .writefn = gt_virt_redir_ctl_write, .raw_writefn = raw_write,
55d284af
PM
3205 },
3206 /* TimerValue views: a 32 bit downcounting view of the underlying state */
3207 { .name = "CNTP_TVAL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
9ff9dd3c 3208 .secure = ARM_CP_SECSTATE_NS,
daf1dc5f 3209 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
00108f2d 3210 .accessfn = gt_ptimer_access,
bb5972e4 3211 .readfn = gt_phys_redir_tval_read, .writefn = gt_phys_redir_tval_write,
55d284af 3212 },
9c513e78 3213 { .name = "CNTP_TVAL_S",
9ff9dd3c
PM
3214 .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
3215 .secure = ARM_CP_SECSTATE_S,
daf1dc5f 3216 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
9ff9dd3c
PM
3217 .accessfn = gt_ptimer_access,
3218 .readfn = gt_sec_tval_read, .writefn = gt_sec_tval_write,
3219 },
a7adc4b7
PM
3220 { .name = "CNTP_TVAL_EL0", .state = ARM_CP_STATE_AA64,
3221 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 0,
daf1dc5f 3222 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
0e3eca4c 3223 .accessfn = gt_ptimer_access, .resetfn = gt_phys_timer_reset,
bb5972e4 3224 .readfn = gt_phys_redir_tval_read, .writefn = gt_phys_redir_tval_write,
a7adc4b7 3225 },
55d284af 3226 { .name = "CNTV_TVAL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 0,
daf1dc5f 3227 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
00108f2d 3228 .accessfn = gt_vtimer_access,
bb5972e4 3229 .readfn = gt_virt_redir_tval_read, .writefn = gt_virt_redir_tval_write,
55d284af 3230 },
a7adc4b7
PM
3231 { .name = "CNTV_TVAL_EL0", .state = ARM_CP_STATE_AA64,
3232 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 0,
daf1dc5f 3233 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
0e3eca4c 3234 .accessfn = gt_vtimer_access, .resetfn = gt_virt_timer_reset,
bb5972e4 3235 .readfn = gt_virt_redir_tval_read, .writefn = gt_virt_redir_tval_write,
a7adc4b7 3236 },
55d284af
PM
3237 /* The counter itself */
3238 { .name = "CNTPCT", .cp = 15, .crm = 14, .opc1 = 0,
7a0e58fa 3239 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
00108f2d 3240 .accessfn = gt_pct_access,
a7adc4b7
PM
3241 .readfn = gt_cnt_read, .resetfn = arm_cp_reset_ignore,
3242 },
3243 { .name = "CNTPCT_EL0", .state = ARM_CP_STATE_AA64,
3244 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 1,
7a0e58fa 3245 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
d57b9ee8 3246 .accessfn = gt_pct_access, .readfn = gt_cnt_read,
55d284af
PM
3247 },
3248 { .name = "CNTVCT", .cp = 15, .crm = 14, .opc1 = 1,
7a0e58fa 3249 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
00108f2d 3250 .accessfn = gt_vct_access,
edac4d8a 3251 .readfn = gt_virt_cnt_read, .resetfn = arm_cp_reset_ignore,
a7adc4b7
PM
3252 },
3253 { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
3254 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
7a0e58fa 3255 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
d57b9ee8 3256 .accessfn = gt_vct_access, .readfn = gt_virt_cnt_read,
55d284af
PM
3257 },
3258 /* Comparison value, indicating when the timer goes off */
3259 { .name = "CNTP_CVAL", .cp = 15, .crm = 14, .opc1 = 2,
9ff9dd3c 3260 .secure = ARM_CP_SECSTATE_NS,
daf1dc5f 3261 .access = PL0_RW,
7a0e58fa 3262 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
55d284af 3263 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
b061a82b 3264 .accessfn = gt_ptimer_access,
bb5972e4
RH
3265 .readfn = gt_phys_redir_cval_read, .raw_readfn = raw_read,
3266 .writefn = gt_phys_redir_cval_write, .raw_writefn = raw_write,
a7adc4b7 3267 },
9c513e78 3268 { .name = "CNTP_CVAL_S", .cp = 15, .crm = 14, .opc1 = 2,
9ff9dd3c 3269 .secure = ARM_CP_SECSTATE_S,
daf1dc5f 3270 .access = PL0_RW,
9ff9dd3c
PM
3271 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
3272 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
3273 .accessfn = gt_ptimer_access,
3274 .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
3275 },
a7adc4b7
PM
3276 { .name = "CNTP_CVAL_EL0", .state = ARM_CP_STATE_AA64,
3277 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 2,
daf1dc5f 3278 .access = PL0_RW,
a7adc4b7
PM
3279 .type = ARM_CP_IO,
3280 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
12cde08a 3281 .resetvalue = 0, .accessfn = gt_ptimer_access,
bb5972e4
RH
3282 .readfn = gt_phys_redir_cval_read, .raw_readfn = raw_read,
3283 .writefn = gt_phys_redir_cval_write, .raw_writefn = raw_write,
55d284af
PM
3284 },
3285 { .name = "CNTV_CVAL", .cp = 15, .crm = 14, .opc1 = 3,
daf1dc5f 3286 .access = PL0_RW,
7a0e58fa 3287 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
55d284af 3288 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
b061a82b 3289 .accessfn = gt_vtimer_access,
bb5972e4
RH
3290 .readfn = gt_virt_redir_cval_read, .raw_readfn = raw_read,
3291 .writefn = gt_virt_redir_cval_write, .raw_writefn = raw_write,
a7adc4b7
PM
3292 },
3293 { .name = "CNTV_CVAL_EL0", .state = ARM_CP_STATE_AA64,
3294 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 2,
daf1dc5f 3295 .access = PL0_RW,
a7adc4b7
PM
3296 .type = ARM_CP_IO,
3297 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
3298 .resetvalue = 0, .accessfn = gt_vtimer_access,
bb5972e4
RH
3299 .readfn = gt_virt_redir_cval_read, .raw_readfn = raw_read,
3300 .writefn = gt_virt_redir_cval_write, .raw_writefn = raw_write,
55d284af 3301 },
b4d3978c
PM
3302 /* Secure timer -- this is actually restricted to only EL3
3303 * and configurably Secure-EL1 via the accessfn.
3304 */
3305 { .name = "CNTPS_TVAL_EL1", .state = ARM_CP_STATE_AA64,
3306 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 0,
3307 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW,
3308 .accessfn = gt_stimer_access,
3309 .readfn = gt_sec_tval_read,
3310 .writefn = gt_sec_tval_write,
3311 .resetfn = gt_sec_timer_reset,
3312 },
3313 { .name = "CNTPS_CTL_EL1", .state = ARM_CP_STATE_AA64,
3314 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 1,
3315 .type = ARM_CP_IO, .access = PL1_RW,
3316 .accessfn = gt_stimer_access,
3317 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].ctl),
3318 .resetvalue = 0,
3319 .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
3320 },
3321 { .name = "CNTPS_CVAL_EL1", .state = ARM_CP_STATE_AA64,
3322 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 2,
3323 .type = ARM_CP_IO, .access = PL1_RW,
3324 .accessfn = gt_stimer_access,
3325 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
3326 .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
3327 },
55d284af
PM
3328 REGINFO_SENTINEL
3329};
3330
bb5972e4
RH
3331static CPAccessResult e2h_access(CPUARMState *env, const ARMCPRegInfo *ri,
3332 bool isread)
3333{
3334 if (!(arm_hcr_el2_eff(env) & HCR_E2H)) {
3335 return CP_ACCESS_TRAP;
3336 }
3337 return CP_ACCESS_OK;
3338}
3339
55d284af 3340#else
26c4a83b
AB
3341
3342/* In user-mode most of the generic timer registers are inaccessible
3343 * however modern kernels (4.12+) allow access to cntvct_el0
55d284af 3344 */
26c4a83b
AB
3345
3346static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
3347{
7def8754
AJ
3348 ARMCPU *cpu = env_archcpu(env);
3349
26c4a83b
AB
3350 /* Currently we have no support for QEMUTimer in linux-user so we
3351 * can't call gt_get_countervalue(env), instead we directly
3352 * call the lower level functions.
3353 */
7def8754 3354 return cpu_get_clock() / gt_cntfrq_period_ns(cpu);
26c4a83b
AB
3355}
3356
6cc7a3ae 3357static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
26c4a83b
AB
3358 { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
3359 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
3360 .type = ARM_CP_CONST, .access = PL0_R /* no PL1_RW in linux-user */,
3361 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
3362 .resetvalue = NANOSECONDS_PER_SECOND / GTIMER_SCALE,
3363 },
3364 { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
3365 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
3366 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3367 .readfn = gt_virt_cnt_read,
3368 },
6cc7a3ae
PM
3369 REGINFO_SENTINEL
3370};
3371
55d284af
PM
3372#endif
3373
c4241c7d 3374static void par_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
4a501606 3375{
891a2fe7 3376 if (arm_feature(env, ARM_FEATURE_LPAE)) {
8d5c773e 3377 raw_write(env, ri, value);
891a2fe7 3378 } else if (arm_feature(env, ARM_FEATURE_V7)) {
8d5c773e 3379 raw_write(env, ri, value & 0xfffff6ff);
4a501606 3380 } else {
8d5c773e 3381 raw_write(env, ri, value & 0xfffff1ff);
4a501606 3382 }
4a501606
PM
3383}
3384
3385#ifndef CONFIG_USER_ONLY
3386/* get_phys_addr() isn't present for user-mode-only targets */
702a9357 3387
3f208fd7
PM
3388static CPAccessResult ats_access(CPUARMState *env, const ARMCPRegInfo *ri,
3389 bool isread)
92611c00
PM
3390{
3391 if (ri->opc2 & 4) {
87562e4f
PM
3392 /* The ATS12NSO* operations must trap to EL3 if executed in
3393 * Secure EL1 (which can only happen if EL3 is AArch64).
3394 * They are simply UNDEF if executed from NS EL1.
3395 * They function normally from EL2 or EL3.
92611c00 3396 */
87562e4f
PM
3397 if (arm_current_el(env) == 1) {
3398 if (arm_is_secure_below_el3(env)) {
3399 return CP_ACCESS_TRAP_UNCATEGORIZED_EL3;
3400 }
3401 return CP_ACCESS_TRAP_UNCATEGORIZED;
3402 }
92611c00
PM
3403 }
3404 return CP_ACCESS_OK;
3405}
3406
9fb005b0 3407#ifdef CONFIG_TCG
060e8a48 3408static uint64_t do_ats_write(CPUARMState *env, uint64_t value,
03ae85f8 3409 MMUAccessType access_type, ARMMMUIdx mmu_idx)
4a501606 3410{
a8170e5e 3411 hwaddr phys_addr;
4a501606
PM
3412 target_ulong page_size;
3413 int prot;
b7cc4e82 3414 bool ret;
01c097f7 3415 uint64_t par64;
1313e2d7 3416 bool format64 = false;
8bf5b6a9 3417 MemTxAttrs attrs = {};
e14b5a23 3418 ARMMMUFaultInfo fi = {};
5b2d261d 3419 ARMCacheAttrs cacheattrs = {};
4a501606 3420
5b2d261d 3421 ret = get_phys_addr(env, value, access_type, mmu_idx, &phys_addr, &attrs,
bc52bfeb 3422 &prot, &page_size, &fi, &cacheattrs);
1313e2d7 3423
0710b2fa
PM
3424 if (ret) {
3425 /*
3426 * Some kinds of translation fault must cause exceptions rather
3427 * than being reported in the PAR.
3428 */
3429 int current_el = arm_current_el(env);
3430 int target_el;
3431 uint32_t syn, fsr, fsc;
3432 bool take_exc = false;
3433
3434 if (fi.s1ptw && current_el == 1 && !arm_is_secure(env)
fee7aa46 3435 && arm_mmu_idx_is_stage1_of_2(mmu_idx)) {
0710b2fa
PM
3436 /*
3437 * Synchronous stage 2 fault on an access made as part of the
3438 * translation table walk for AT S1E0* or AT S1E1* insn
3439 * executed from NS EL1. If this is a synchronous external abort
3440 * and SCR_EL3.EA == 1, then we take a synchronous external abort
3441 * to EL3. Otherwise the fault is taken as an exception to EL2,
3442 * and HPFAR_EL2 holds the faulting IPA.
3443 */
3444 if (fi.type == ARMFault_SyncExternalOnWalk &&
3445 (env->cp15.scr_el3 & SCR_EA)) {
3446 target_el = 3;
3447 } else {
3448 env->cp15.hpfar_el2 = extract64(fi.s2addr, 12, 47) << 4;
3449 target_el = 2;
3450 }
3451 take_exc = true;
3452 } else if (fi.type == ARMFault_SyncExternalOnWalk) {
3453 /*
3454 * Synchronous external aborts during a translation table walk
3455 * are taken as Data Abort exceptions.
3456 */
3457 if (fi.stage2) {
3458 if (current_el == 3) {
3459 target_el = 3;
3460 } else {
3461 target_el = 2;
3462 }
3463 } else {
3464 target_el = exception_target_el(env);
3465 }
3466 take_exc = true;
3467 }
3468
3469 if (take_exc) {
3470 /* Construct FSR and FSC using same logic as arm_deliver_fault() */
3471 if (target_el == 2 || arm_el_is_aa64(env, target_el) ||
3472 arm_s1_regime_using_lpae_format(env, mmu_idx)) {
3473 fsr = arm_fi_to_lfsc(&fi);
3474 fsc = extract32(fsr, 0, 6);
3475 } else {
3476 fsr = arm_fi_to_sfsc(&fi);
3477 fsc = 0x3f;
3478 }
3479 /*
3480 * Report exception with ESR indicating a fault due to a
3481 * translation table walk for a cache maintenance instruction.
3482 */
3483 syn = syn_data_abort_no_iss(current_el == target_el,
3484 fi.ea, 1, fi.s1ptw, 1, fsc);
3485 env->exception.vaddress = value;
3486 env->exception.fsr = fsr;
3487 raise_exception(env, EXCP_DATA_ABORT, syn, target_el);
3488 }
3489 }
3490
1313e2d7
EI
3491 if (is_a64(env)) {
3492 format64 = true;
3493 } else if (arm_feature(env, ARM_FEATURE_LPAE)) {
3494 /*
3495 * ATS1Cxx:
3496 * * TTBCR.EAE determines whether the result is returned using the
3497 * 32-bit or the 64-bit PAR format
3498 * * Instructions executed in Hyp mode always use the 64bit format
3499 *
3500 * ATS1S2NSOxx uses the 64bit format if any of the following is true:
3501 * * The Non-secure TTBCR.EAE bit is set to 1
3502 * * The implementation includes EL2, and the value of HCR.VM is 1
3503 *
9d1bab33
PM
3504 * (Note that HCR.DC makes HCR.VM behave as if it is 1.)
3505 *
23463e0e 3506 * ATS1Hx always uses the 64bit format.
1313e2d7
EI
3507 */
3508 format64 = arm_s1_regime_using_lpae_format(env, mmu_idx);
3509
3510 if (arm_feature(env, ARM_FEATURE_EL2)) {
452ef8cb
RH
3511 if (mmu_idx == ARMMMUIdx_E10_0 ||
3512 mmu_idx == ARMMMUIdx_E10_1 ||
3513 mmu_idx == ARMMMUIdx_E10_1_PAN) {
9d1bab33 3514 format64 |= env->cp15.hcr_el2 & (HCR_VM | HCR_DC);
1313e2d7
EI
3515 } else {
3516 format64 |= arm_current_el(env) == 2;
3517 }
3518 }
3519 }
3520
3521 if (format64) {
5efe9ed4 3522 /* Create a 64-bit PAR */
01c097f7 3523 par64 = (1 << 11); /* LPAE bit always set */
b7cc4e82 3524 if (!ret) {
702a9357 3525 par64 |= phys_addr & ~0xfffULL;
8bf5b6a9
PM
3526 if (!attrs.secure) {
3527 par64 |= (1 << 9); /* NS */
3528 }
5b2d261d
AB
3529 par64 |= (uint64_t)cacheattrs.attrs << 56; /* ATTR */
3530 par64 |= cacheattrs.shareability << 7; /* SH */
4a501606 3531 } else {
5efe9ed4
PM
3532 uint32_t fsr = arm_fi_to_lfsc(&fi);
3533
702a9357 3534 par64 |= 1; /* F */
b7cc4e82 3535 par64 |= (fsr & 0x3f) << 1; /* FS */
0f7b791b
PM
3536 if (fi.stage2) {
3537 par64 |= (1 << 9); /* S */
3538 }
3539 if (fi.s1ptw) {
3540 par64 |= (1 << 8); /* PTW */
3541 }
4a501606
PM
3542 }
3543 } else {
b7cc4e82 3544 /* fsr is a DFSR/IFSR value for the short descriptor
702a9357
PM
3545 * translation table format (with WnR always clear).
3546 * Convert it to a 32-bit PAR.
3547 */
b7cc4e82 3548 if (!ret) {
702a9357
PM
3549 /* We do not set any attribute bits in the PAR */
3550 if (page_size == (1 << 24)
3551 && arm_feature(env, ARM_FEATURE_V7)) {
01c097f7 3552 par64 = (phys_addr & 0xff000000) | (1 << 1);
702a9357 3553 } else {
01c097f7 3554 par64 = phys_addr & 0xfffff000;
702a9357 3555 }
8bf5b6a9
PM
3556 if (!attrs.secure) {
3557 par64 |= (1 << 9); /* NS */
3558 }
702a9357 3559 } else {
5efe9ed4
PM
3560 uint32_t fsr = arm_fi_to_sfsc(&fi);
3561
b7cc4e82
PC
3562 par64 = ((fsr & (1 << 10)) >> 5) | ((fsr & (1 << 12)) >> 6) |
3563 ((fsr & 0xf) << 1) | 1;
702a9357 3564 }
4a501606 3565 }
060e8a48
PM
3566 return par64;
3567}
9fb005b0 3568#endif /* CONFIG_TCG */
060e8a48
PM
3569
3570static void ats_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
3571{
9fb005b0 3572#ifdef CONFIG_TCG
03ae85f8 3573 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
060e8a48 3574 uint64_t par64;
d3649702
PM
3575 ARMMMUIdx mmu_idx;
3576 int el = arm_current_el(env);
3577 bool secure = arm_is_secure_below_el3(env);
060e8a48 3578
d3649702
PM
3579 switch (ri->opc2 & 6) {
3580 case 0:
04b07d29 3581 /* stage 1 current state PL1: ATS1CPR, ATS1CPW, ATS1CPRP, ATS1CPWP */
d3649702
PM
3582 switch (el) {
3583 case 3:
127b2b08 3584 mmu_idx = ARMMMUIdx_SE3;
d3649702
PM
3585 break;
3586 case 2:
04b07d29
RH
3587 g_assert(!secure); /* TODO: ARMv8.4-SecEL2 */
3588 /* fall through */
d3649702 3589 case 1:
04b07d29
RH
3590 if (ri->crm == 9 && (env->uncached_cpsr & CPSR_PAN)) {
3591 mmu_idx = (secure ? ARMMMUIdx_SE10_1_PAN
3592 : ARMMMUIdx_Stage1_E1_PAN);
3593 } else {
3594 mmu_idx = secure ? ARMMMUIdx_SE10_1 : ARMMMUIdx_Stage1_E1;
3595 }
d3649702
PM
3596 break;
3597 default:
3598 g_assert_not_reached();
3599 }
3600 break;
3601 case 2:
3602 /* stage 1 current state PL0: ATS1CUR, ATS1CUW */
3603 switch (el) {
3604 case 3:
fba37aed 3605 mmu_idx = ARMMMUIdx_SE10_0;
d3649702
PM
3606 break;
3607 case 2:
2859d7b5 3608 mmu_idx = ARMMMUIdx_Stage1_E0;
d3649702
PM
3609 break;
3610 case 1:
fba37aed 3611 mmu_idx = secure ? ARMMMUIdx_SE10_0 : ARMMMUIdx_Stage1_E0;
d3649702
PM
3612 break;
3613 default:
3614 g_assert_not_reached();
3615 }
3616 break;
3617 case 4:
3618 /* stage 1+2 NonSecure PL1: ATS12NSOPR, ATS12NSOPW */
01b98b68 3619 mmu_idx = ARMMMUIdx_E10_1;
d3649702
PM
3620 break;
3621 case 6:
3622 /* stage 1+2 NonSecure PL0: ATS12NSOUR, ATS12NSOUW */
01b98b68 3623 mmu_idx = ARMMMUIdx_E10_0;
d3649702
PM
3624 break;
3625 default:
3626 g_assert_not_reached();
3627 }
3628
3629 par64 = do_ats_write(env, value, access_type, mmu_idx);
01c097f7
FA
3630
3631 A32_BANKED_CURRENT_REG_SET(env, par, par64);
9fb005b0
PMD
3632#else
3633 /* Handled by hardware accelerator. */
3634 g_assert_not_reached();
3635#endif /* CONFIG_TCG */
4a501606 3636}
060e8a48 3637
14db7fe0
PM
3638static void ats1h_write(CPUARMState *env, const ARMCPRegInfo *ri,
3639 uint64_t value)
3640{
9fb005b0 3641#ifdef CONFIG_TCG
03ae85f8 3642 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
14db7fe0
PM
3643 uint64_t par64;
3644
e013b741 3645 par64 = do_ats_write(env, value, access_type, ARMMMUIdx_E2);
14db7fe0
PM
3646
3647 A32_BANKED_CURRENT_REG_SET(env, par, par64);
9fb005b0
PMD
3648#else
3649 /* Handled by hardware accelerator. */
3650 g_assert_not_reached();
3651#endif /* CONFIG_TCG */
14db7fe0
PM
3652}
3653
3f208fd7
PM
3654static CPAccessResult at_s1e2_access(CPUARMState *env, const ARMCPRegInfo *ri,
3655 bool isread)
2a47df95
PM
3656{
3657 if (arm_current_el(env) == 3 && !(env->cp15.scr_el3 & SCR_NS)) {
3658 return CP_ACCESS_TRAP;
3659 }
3660 return CP_ACCESS_OK;
3661}
3662
060e8a48
PM
3663static void ats_write64(CPUARMState *env, const ARMCPRegInfo *ri,
3664 uint64_t value)
3665{
9fb005b0 3666#ifdef CONFIG_TCG
03ae85f8 3667 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
d3649702
PM
3668 ARMMMUIdx mmu_idx;
3669 int secure = arm_is_secure_below_el3(env);
3670
3671 switch (ri->opc2 & 6) {
3672 case 0:
3673 switch (ri->opc1) {
04b07d29
RH
3674 case 0: /* AT S1E1R, AT S1E1W, AT S1E1RP, AT S1E1WP */
3675 if (ri->crm == 9 && (env->pstate & PSTATE_PAN)) {
3676 mmu_idx = (secure ? ARMMMUIdx_SE10_1_PAN
3677 : ARMMMUIdx_Stage1_E1_PAN);
3678 } else {
3679 mmu_idx = secure ? ARMMMUIdx_SE10_1 : ARMMMUIdx_Stage1_E1;
3680 }
d3649702
PM
3681 break;
3682 case 4: /* AT S1E2R, AT S1E2W */
e013b741 3683 mmu_idx = ARMMMUIdx_E2;
d3649702
PM
3684 break;
3685 case 6: /* AT S1E3R, AT S1E3W */
127b2b08 3686 mmu_idx = ARMMMUIdx_SE3;
d3649702
PM
3687 break;
3688 default:
3689 g_assert_not_reached();
3690 }
3691 break;
3692 case 2: /* AT S1E0R, AT S1E0W */
fba37aed 3693 mmu_idx = secure ? ARMMMUIdx_SE10_0 : ARMMMUIdx_Stage1_E0;
d3649702
PM
3694 break;
3695 case 4: /* AT S12E1R, AT S12E1W */
fba37aed 3696 mmu_idx = secure ? ARMMMUIdx_SE10_1 : ARMMMUIdx_E10_1;
d3649702
PM
3697 break;
3698 case 6: /* AT S12E0R, AT S12E0W */
fba37aed 3699 mmu_idx = secure ? ARMMMUIdx_SE10_0 : ARMMMUIdx_E10_0;
d3649702
PM
3700 break;
3701 default:
3702 g_assert_not_reached();
3703 }
060e8a48 3704
d3649702 3705 env->cp15.par_el[1] = do_ats_write(env, value, access_type, mmu_idx);
9fb005b0
PMD
3706#else
3707 /* Handled by hardware accelerator. */
3708 g_assert_not_reached();
3709#endif /* CONFIG_TCG */
060e8a48 3710}
4a501606
PM
3711#endif
3712
3713static const ARMCPRegInfo vapa_cp_reginfo[] = {
3714 { .name = "PAR", .cp = 15, .crn = 7, .crm = 4, .opc1 = 0, .opc2 = 0,
3715 .access = PL1_RW, .resetvalue = 0,
01c097f7
FA
3716 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.par_s),
3717 offsetoflow32(CPUARMState, cp15.par_ns) },
4a501606
PM
3718 .writefn = par_write },
3719#ifndef CONFIG_USER_ONLY
87562e4f 3720 /* This underdecoding is safe because the reginfo is NO_RAW. */
4a501606 3721 { .name = "ATS", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = CP_ANY,
92611c00 3722 .access = PL1_W, .accessfn = ats_access,
0710b2fa 3723 .writefn = ats_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
4a501606
PM
3724#endif
3725 REGINFO_SENTINEL
3726};
3727
18032bec
PM
3728/* Return basic MPU access permission bits. */
3729static uint32_t simple_mpu_ap_bits(uint32_t val)
3730{
3731 uint32_t ret;
3732 uint32_t mask;
3733 int i;
3734 ret = 0;
3735 mask = 3;
3736 for (i = 0; i < 16; i += 2) {
3737 ret |= (val >> i) & mask;
3738 mask <<= 2;
3739 }
3740 return ret;
3741}
3742
3743/* Pad basic MPU access permission bits to extended format. */
3744static uint32_t extended_mpu_ap_bits(uint32_t val)
3745{
3746 uint32_t ret;
3747 uint32_t mask;
3748 int i;
3749 ret = 0;
3750 mask = 3;
3751 for (i = 0; i < 16; i += 2) {
3752 ret |= (val & mask) << i;
3753 mask <<= 2;
3754 }
3755 return ret;
3756}
3757
c4241c7d
PM
3758static void pmsav5_data_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
3759 uint64_t value)
18032bec 3760{
7e09797c 3761 env->cp15.pmsav5_data_ap = extended_mpu_ap_bits(value);
18032bec
PM
3762}
3763
c4241c7d 3764static uint64_t pmsav5_data_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
18032bec 3765{
7e09797c 3766 return simple_mpu_ap_bits(env->cp15.pmsav5_data_ap);
18032bec
PM
3767}
3768
c4241c7d
PM
3769static void pmsav5_insn_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
3770 uint64_t value)
18032bec 3771{
7e09797c 3772 env->cp15.pmsav5_insn_ap = extended_mpu_ap_bits(value);
18032bec
PM
3773}
3774
c4241c7d 3775static uint64_t pmsav5_insn_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
18032bec 3776{
7e09797c 3777 return simple_mpu_ap_bits(env->cp15.pmsav5_insn_ap);
18032bec
PM
3778}
3779
6cb0b013
PC
3780static uint64_t pmsav7_read(CPUARMState *env, const ARMCPRegInfo *ri)
3781{
3782 uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
3783
3784 if (!u32p) {
3785 return 0;
3786 }
3787
1bc04a88 3788 u32p += env->pmsav7.rnr[M_REG_NS];
6cb0b013
PC
3789 return *u32p;
3790}
3791
3792static void pmsav7_write(CPUARMState *env, const ARMCPRegInfo *ri,
3793 uint64_t value)
3794{
2fc0cc0e 3795 ARMCPU *cpu = env_archcpu(env);
6cb0b013
PC
3796 uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
3797
3798 if (!u32p) {
3799 return;
3800 }
3801
1bc04a88 3802 u32p += env->pmsav7.rnr[M_REG_NS];
d10eb08f 3803 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
6cb0b013
PC
3804 *u32p = value;
3805}
3806
6cb0b013
PC
3807static void pmsav7_rgnr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3808 uint64_t value)
3809{
2fc0cc0e 3810 ARMCPU *cpu = env_archcpu(env);
6cb0b013
PC
3811 uint32_t nrgs = cpu->pmsav7_dregion;
3812
3813 if (value >= nrgs) {
3814 qemu_log_mask(LOG_GUEST_ERROR,
3815 "PMSAv7 RGNR write >= # supported regions, %" PRIu32
3816 " > %" PRIu32 "\n", (uint32_t)value, nrgs);
3817 return;
3818 }
3819
3820 raw_write(env, ri, value);
3821}
3822
3823static const ARMCPRegInfo pmsav7_cp_reginfo[] = {
69ceea64
PM
3824 /* Reset for all these registers is handled in arm_cpu_reset(),
3825 * because the PMSAv7 is also used by M-profile CPUs, which do
3826 * not register cpregs but still need the state to be reset.
3827 */
6cb0b013
PC
3828 { .name = "DRBAR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 0,
3829 .access = PL1_RW, .type = ARM_CP_NO_RAW,
3830 .fieldoffset = offsetof(CPUARMState, pmsav7.drbar),
69ceea64
PM
3831 .readfn = pmsav7_read, .writefn = pmsav7_write,
3832 .resetfn = arm_cp_reset_ignore },
6cb0b013
PC
3833 { .name = "DRSR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 2,
3834 .access = PL1_RW, .type = ARM_CP_NO_RAW,
3835 .fieldoffset = offsetof(CPUARMState, pmsav7.drsr),
69ceea64
PM
3836 .readfn = pmsav7_read, .writefn = pmsav7_write,
3837 .resetfn = arm_cp_reset_ignore },
6cb0b013
PC
3838 { .name = "DRACR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 4,
3839 .access = PL1_RW, .type = ARM_CP_NO_RAW,
3840 .fieldoffset = offsetof(CPUARMState, pmsav7.dracr),
69ceea64
PM
3841 .readfn = pmsav7_read, .writefn = pmsav7_write,
3842 .resetfn = arm_cp_reset_ignore },
6cb0b013
PC
3843 { .name = "RGNR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 2, .opc2 = 0,
3844 .access = PL1_RW,
1bc04a88 3845 .fieldoffset = offsetof(CPUARMState, pmsav7.rnr[M_REG_NS]),
69ceea64
PM
3846 .writefn = pmsav7_rgnr_write,
3847 .resetfn = arm_cp_reset_ignore },
6cb0b013
PC
3848 REGINFO_SENTINEL
3849};
3850
18032bec
PM
3851static const ARMCPRegInfo pmsav5_cp_reginfo[] = {
3852 { .name = "DATA_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
7a0e58fa 3853 .access = PL1_RW, .type = ARM_CP_ALIAS,
7e09797c 3854 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
18032bec
PM
3855 .readfn = pmsav5_data_ap_read, .writefn = pmsav5_data_ap_write, },
3856 { .name = "INSN_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
7a0e58fa 3857 .access = PL1_RW, .type = ARM_CP_ALIAS,
7e09797c 3858 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
18032bec
PM
3859 .readfn = pmsav5_insn_ap_read, .writefn = pmsav5_insn_ap_write, },
3860 { .name = "DATA_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 2,
3861 .access = PL1_RW,
7e09797c
PM
3862 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
3863 .resetvalue = 0, },
18032bec
PM
3864 { .name = "INSN_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 3,
3865 .access = PL1_RW,
7e09797c
PM
3866 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
3867 .resetvalue = 0, },
ecce5c3c
PM
3868 { .name = "DCACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
3869 .access = PL1_RW,
3870 .fieldoffset = offsetof(CPUARMState, cp15.c2_data), .resetvalue = 0, },
3871 { .name = "ICACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 1,
3872 .access = PL1_RW,
3873 .fieldoffset = offsetof(CPUARMState, cp15.c2_insn), .resetvalue = 0, },
06d76f31 3874 /* Protection region base and size registers */
e508a92b
PM
3875 { .name = "946_PRBS0", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0,
3876 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3877 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[0]) },
3878 { .name = "946_PRBS1", .cp = 15, .crn = 6, .crm = 1, .opc1 = 0,
3879 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3880 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[1]) },
3881 { .name = "946_PRBS2", .cp = 15, .crn = 6, .crm = 2, .opc1 = 0,
3882 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3883 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[2]) },
3884 { .name = "946_PRBS3", .cp = 15, .crn = 6, .crm = 3, .opc1 = 0,
3885 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3886 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[3]) },
3887 { .name = "946_PRBS4", .cp = 15, .crn = 6, .crm = 4, .opc1 = 0,
3888 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3889 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[4]) },
3890 { .name = "946_PRBS5", .cp = 15, .crn = 6, .crm = 5, .opc1 = 0,
3891 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3892 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[5]) },
3893 { .name = "946_PRBS6", .cp = 15, .crn = 6, .crm = 6, .opc1 = 0,
3894 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3895 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[6]) },
3896 { .name = "946_PRBS7", .cp = 15, .crn = 6, .crm = 7, .opc1 = 0,
3897 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3898 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[7]) },
18032bec
PM
3899 REGINFO_SENTINEL
3900};
3901
c4241c7d
PM
3902static void vmsa_ttbcr_raw_write(CPUARMState *env, const ARMCPRegInfo *ri,
3903 uint64_t value)
ecce5c3c 3904{
11f136ee 3905 TCR *tcr = raw_ptr(env, ri);
2ebcebe2
PM
3906 int maskshift = extract32(value, 0, 3);
3907
e389be16
FA
3908 if (!arm_feature(env, ARM_FEATURE_V8)) {
3909 if (arm_feature(env, ARM_FEATURE_LPAE) && (value & TTBCR_EAE)) {
3910 /* Pre ARMv8 bits [21:19], [15:14] and [6:3] are UNK/SBZP when
3911 * using Long-desciptor translation table format */
3912 value &= ~((7 << 19) | (3 << 14) | (0xf << 3));
3913 } else if (arm_feature(env, ARM_FEATURE_EL3)) {
3914 /* In an implementation that includes the Security Extensions
3915 * TTBCR has additional fields PD0 [4] and PD1 [5] for
3916 * Short-descriptor translation table format.
3917 */
3918 value &= TTBCR_PD1 | TTBCR_PD0 | TTBCR_N;
3919 } else {
3920 value &= TTBCR_N;
3921 }
e42c4db3 3922 }
e389be16 3923
b6af0975 3924 /* Update the masks corresponding to the TCR bank being written
11f136ee 3925 * Note that we always calculate mask and base_mask, but
e42c4db3 3926 * they are only used for short-descriptor tables (ie if EAE is 0);
11f136ee
FA
3927 * for long-descriptor tables the TCR fields are used differently
3928 * and the mask and base_mask values are meaningless.
e42c4db3 3929 */
11f136ee
FA
3930 tcr->raw_tcr = value;
3931 tcr->mask = ~(((uint32_t)0xffffffffu) >> maskshift);
3932 tcr->base_mask = ~((uint32_t)0x3fffu >> maskshift);
ecce5c3c
PM
3933}
3934
c4241c7d
PM
3935static void vmsa_ttbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3936 uint64_t value)
d4e6df63 3937{
2fc0cc0e 3938 ARMCPU *cpu = env_archcpu(env);
ab638a32 3939 TCR *tcr = raw_ptr(env, ri);
00c8cb0a 3940
d4e6df63
PM
3941 if (arm_feature(env, ARM_FEATURE_LPAE)) {
3942 /* With LPAE the TTBCR could result in a change of ASID
3943 * via the TTBCR.A1 bit, so do a TLB flush.
3944 */
d10eb08f 3945 tlb_flush(CPU(cpu));
d4e6df63 3946 }
ab638a32
RH
3947 /* Preserve the high half of TCR_EL1, set via TTBCR2. */
3948 value = deposit64(tcr->raw_tcr, 0, 32, value);
c4241c7d 3949 vmsa_ttbcr_raw_write(env, ri, value);
d4e6df63
PM
3950}
3951
ecce5c3c
PM
3952static void vmsa_ttbcr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
3953{
11f136ee
FA
3954 TCR *tcr = raw_ptr(env, ri);
3955
3956 /* Reset both the TCR as well as the masks corresponding to the bank of
3957 * the TCR being reset.
3958 */
3959 tcr->raw_tcr = 0;
3960 tcr->mask = 0;
3961 tcr->base_mask = 0xffffc000u;
ecce5c3c
PM
3962}
3963
d06dc933 3964static void vmsa_tcr_el12_write(CPUARMState *env, const ARMCPRegInfo *ri,
cb2e37df
PM
3965 uint64_t value)
3966{
2fc0cc0e 3967 ARMCPU *cpu = env_archcpu(env);
11f136ee 3968 TCR *tcr = raw_ptr(env, ri);
00c8cb0a 3969
cb2e37df 3970 /* For AArch64 the A1 bit could result in a change of ASID, so TLB flush. */
d10eb08f 3971 tlb_flush(CPU(cpu));
11f136ee 3972 tcr->raw_tcr = value;
cb2e37df
PM
3973}
3974
327ed10f
PM
3975static void vmsa_ttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3976 uint64_t value)
3977{
93f379b0
RH
3978 /* If the ASID changes (with a 64-bit write), we must flush the TLB. */
3979 if (cpreg_field_is_64bit(ri) &&
3980 extract64(raw_read(env, ri) ^ value, 48, 16) != 0) {
2fc0cc0e 3981 ARMCPU *cpu = env_archcpu(env);
d10eb08f 3982 tlb_flush(CPU(cpu));
327ed10f
PM
3983 }
3984 raw_write(env, ri, value);
3985}
3986
ed30da8e
RH
3987static void vmsa_tcr_ttbr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
3988 uint64_t value)
3989{
d06dc933
RH
3990 /*
3991 * If we are running with E2&0 regime, then an ASID is active.
3992 * Flush if that might be changing. Note we're not checking
3993 * TCR_EL2.A1 to know if this is really the TTBRx_EL2 that
3994 * holds the active ASID, only checking the field that might.
3995 */
3996 if (extract64(raw_read(env, ri) ^ value, 48, 16) &&
3997 (arm_hcr_el2_eff(env) & HCR_E2H)) {
3998 tlb_flush_by_mmuidx(env_cpu(env),
452ef8cb
RH
3999 ARMMMUIdxBit_E20_2 |
4000 ARMMMUIdxBit_E20_2_PAN |
4001 ARMMMUIdxBit_E20_0);
d06dc933 4002 }
ed30da8e
RH
4003 raw_write(env, ri, value);
4004}
4005
b698e9cf
EI
4006static void vttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4007 uint64_t value)
4008{
2fc0cc0e 4009 ARMCPU *cpu = env_archcpu(env);
b698e9cf
EI
4010 CPUState *cs = CPU(cpu);
4011
97fa9350
RH
4012 /*
4013 * A change in VMID to the stage2 page table (Stage2) invalidates
4014 * the combined stage 1&2 tlbs (EL10_1 and EL10_0).
4015 */
b698e9cf 4016 if (raw_read(env, ri) != value) {
0336cbf8 4017 tlb_flush_by_mmuidx(cs,
01b98b68 4018 ARMMMUIdxBit_E10_1 |
452ef8cb 4019 ARMMMUIdxBit_E10_1_PAN |
bf05340c 4020 ARMMMUIdxBit_E10_0);
b698e9cf
EI
4021 raw_write(env, ri, value);
4022 }
4023}
4024
8e5d75c9 4025static const ARMCPRegInfo vmsa_pmsa_cp_reginfo[] = {
18032bec 4026 { .name = "DFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
84929218 4027 .access = PL1_RW, .accessfn = access_tvm_trvm, .type = ARM_CP_ALIAS,
4a7e2d73 4028 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dfsr_s),
b061a82b 4029 offsetoflow32(CPUARMState, cp15.dfsr_ns) }, },
18032bec 4030 { .name = "IFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
84929218 4031 .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
88ca1c2d
FA
4032 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.ifsr_s),
4033 offsetoflow32(CPUARMState, cp15.ifsr_ns) } },
8e5d75c9 4034 { .name = "DFAR", .cp = 15, .opc1 = 0, .crn = 6, .crm = 0, .opc2 = 0,
84929218 4035 .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
8e5d75c9
PC
4036 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.dfar_s),
4037 offsetof(CPUARMState, cp15.dfar_ns) } },
4038 { .name = "FAR_EL1", .state = ARM_CP_STATE_AA64,
4039 .opc0 = 3, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 0,
84929218
RH
4040 .access = PL1_RW, .accessfn = access_tvm_trvm,
4041 .fieldoffset = offsetof(CPUARMState, cp15.far_el[1]),
8e5d75c9
PC
4042 .resetvalue = 0, },
4043 REGINFO_SENTINEL
4044};
4045
4046static const ARMCPRegInfo vmsa_cp_reginfo[] = {
6cd8a264
RH
4047 { .name = "ESR_EL1", .state = ARM_CP_STATE_AA64,
4048 .opc0 = 3, .crn = 5, .crm = 2, .opc1 = 0, .opc2 = 0,
84929218 4049 .access = PL1_RW, .accessfn = access_tvm_trvm,
d81c519c 4050 .fieldoffset = offsetof(CPUARMState, cp15.esr_el[1]), .resetvalue = 0, },
327ed10f 4051 { .name = "TTBR0_EL1", .state = ARM_CP_STATE_BOTH,
7dd8c9af 4052 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 0,
84929218
RH
4053 .access = PL1_RW, .accessfn = access_tvm_trvm,
4054 .writefn = vmsa_ttbr_write, .resetvalue = 0,
7dd8c9af
FA
4055 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
4056 offsetof(CPUARMState, cp15.ttbr0_ns) } },
327ed10f 4057 { .name = "TTBR1_EL1", .state = ARM_CP_STATE_BOTH,
7dd8c9af 4058 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 1,
84929218
RH
4059 .access = PL1_RW, .accessfn = access_tvm_trvm,
4060 .writefn = vmsa_ttbr_write, .resetvalue = 0,
7dd8c9af
FA
4061 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
4062 offsetof(CPUARMState, cp15.ttbr1_ns) } },
cb2e37df
PM
4063 { .name = "TCR_EL1", .state = ARM_CP_STATE_AA64,
4064 .opc0 = 3, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
84929218
RH
4065 .access = PL1_RW, .accessfn = access_tvm_trvm,
4066 .writefn = vmsa_tcr_el12_write,
cb2e37df 4067 .resetfn = vmsa_ttbcr_reset, .raw_writefn = raw_write,
11f136ee 4068 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[1]) },
cb2e37df 4069 { .name = "TTBCR", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
84929218
RH
4070 .access = PL1_RW, .accessfn = access_tvm_trvm,
4071 .type = ARM_CP_ALIAS, .writefn = vmsa_ttbcr_write,
b061a82b 4072 .raw_writefn = vmsa_ttbcr_raw_write,
11f136ee
FA
4073 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tcr_el[3]),
4074 offsetoflow32(CPUARMState, cp15.tcr_el[1])} },
18032bec
PM
4075 REGINFO_SENTINEL
4076};
4077
ab638a32
RH
4078/* Note that unlike TTBCR, writing to TTBCR2 does not require flushing
4079 * qemu tlbs nor adjusting cached masks.
4080 */
4081static const ARMCPRegInfo ttbcr2_reginfo = {
4082 .name = "TTBCR2", .cp = 15, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 3,
84929218
RH
4083 .access = PL1_RW, .accessfn = access_tvm_trvm,
4084 .type = ARM_CP_ALIAS,
ab638a32
RH
4085 .bank_fieldoffsets = { offsetofhigh32(CPUARMState, cp15.tcr_el[3]),
4086 offsetofhigh32(CPUARMState, cp15.tcr_el[1]) },
4087};
4088
c4241c7d
PM
4089static void omap_ticonfig_write(CPUARMState *env, const ARMCPRegInfo *ri,
4090 uint64_t value)
1047b9d7
PM
4091{
4092 env->cp15.c15_ticonfig = value & 0xe7;
4093 /* The OS_TYPE bit in this register changes the reported CPUID! */
4094 env->cp15.c0_cpuid = (value & (1 << 5)) ?
4095 ARM_CPUID_TI915T : ARM_CPUID_TI925T;
1047b9d7
PM
4096}
4097
c4241c7d
PM
4098static void omap_threadid_write(CPUARMState *env, const ARMCPRegInfo *ri,
4099 uint64_t value)
1047b9d7
PM
4100{
4101 env->cp15.c15_threadid = value & 0xffff;
1047b9d7
PM
4102}
4103
c4241c7d
PM
4104static void omap_wfi_write(CPUARMState *env, const ARMCPRegInfo *ri,
4105 uint64_t value)
1047b9d7
PM
4106{
4107 /* Wait-for-interrupt (deprecated) */
2fc0cc0e 4108 cpu_interrupt(env_cpu(env), CPU_INTERRUPT_HALT);
1047b9d7
PM
4109}
4110
c4241c7d
PM
4111static void omap_cachemaint_write(CPUARMState *env, const ARMCPRegInfo *ri,
4112 uint64_t value)
c4804214
PM
4113{
4114 /* On OMAP there are registers indicating the max/min index of dcache lines
4115 * containing a dirty line; cache flush operations have to reset these.
4116 */
4117 env->cp15.c15_i_max = 0x000;
4118 env->cp15.c15_i_min = 0xff0;
c4804214
PM
4119}
4120
18032bec
PM
4121static const ARMCPRegInfo omap_cp_reginfo[] = {
4122 { .name = "DFSR", .cp = 15, .crn = 5, .crm = CP_ANY,
4123 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_OVERRIDE,
d81c519c 4124 .fieldoffset = offsetoflow32(CPUARMState, cp15.esr_el[1]),
6cd8a264 4125 .resetvalue = 0, },
1047b9d7
PM
4126 { .name = "", .cp = 15, .crn = 15, .crm = 0, .opc1 = 0, .opc2 = 0,
4127 .access = PL1_RW, .type = ARM_CP_NOP },
4128 { .name = "TICONFIG", .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0,
4129 .access = PL1_RW,
4130 .fieldoffset = offsetof(CPUARMState, cp15.c15_ticonfig), .resetvalue = 0,
4131 .writefn = omap_ticonfig_write },
4132 { .name = "IMAX", .cp = 15, .crn = 15, .crm = 2, .opc1 = 0, .opc2 = 0,
4133 .access = PL1_RW,
4134 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_max), .resetvalue = 0, },
4135 { .name = "IMIN", .cp = 15, .crn = 15, .crm = 3, .opc1 = 0, .opc2 = 0,
4136 .access = PL1_RW, .resetvalue = 0xff0,
4137 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_min) },
4138 { .name = "THREADID", .cp = 15, .crn = 15, .crm = 4, .opc1 = 0, .opc2 = 0,
4139 .access = PL1_RW,
4140 .fieldoffset = offsetof(CPUARMState, cp15.c15_threadid), .resetvalue = 0,
4141 .writefn = omap_threadid_write },
4142 { .name = "TI925T_STATUS", .cp = 15, .crn = 15,
4143 .crm = 8, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
7a0e58fa 4144 .type = ARM_CP_NO_RAW,
1047b9d7
PM
4145 .readfn = arm_cp_read_zero, .writefn = omap_wfi_write, },
4146 /* TODO: Peripheral port remap register:
4147 * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt controller
4148 * base address at $rn & ~0xfff and map size of 0x200 << ($rn & 0xfff),
4149 * when MMU is off.
4150 */
c4804214 4151 { .name = "OMAP_CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
d4e6df63 4152 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
7a0e58fa 4153 .type = ARM_CP_OVERRIDE | ARM_CP_NO_RAW,
c4804214 4154 .writefn = omap_cachemaint_write },
34f90529
PM
4155 { .name = "C9", .cp = 15, .crn = 9,
4156 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW,
4157 .type = ARM_CP_CONST | ARM_CP_OVERRIDE, .resetvalue = 0 },
1047b9d7
PM
4158 REGINFO_SENTINEL
4159};
4160
c4241c7d
PM
4161static void xscale_cpar_write(CPUARMState *env, const ARMCPRegInfo *ri,
4162 uint64_t value)
1047b9d7 4163{
c0f4af17 4164 env->cp15.c15_cpar = value & 0x3fff;
1047b9d7
PM
4165}
4166
4167static const ARMCPRegInfo xscale_cp_reginfo[] = {
4168 { .name = "XSCALE_CPAR",
4169 .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
4170 .fieldoffset = offsetof(CPUARMState, cp15.c15_cpar), .resetvalue = 0,
4171 .writefn = xscale_cpar_write, },
2771db27
PM
4172 { .name = "XSCALE_AUXCR",
4173 .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 1, .access = PL1_RW,
4174 .fieldoffset = offsetof(CPUARMState, cp15.c1_xscaleauxcr),
4175 .resetvalue = 0, },
3b771579
PM
4176 /* XScale specific cache-lockdown: since we have no cache we NOP these
4177 * and hope the guest does not really rely on cache behaviour.
4178 */
4179 { .name = "XSCALE_LOCK_ICACHE_LINE",
4180 .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 0,
4181 .access = PL1_W, .type = ARM_CP_NOP },
4182 { .name = "XSCALE_UNLOCK_ICACHE",
4183 .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 1,
4184 .access = PL1_W, .type = ARM_CP_NOP },
4185 { .name = "XSCALE_DCACHE_LOCK",
4186 .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 0,
4187 .access = PL1_RW, .type = ARM_CP_NOP },
4188 { .name = "XSCALE_UNLOCK_DCACHE",
4189 .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 1,
4190 .access = PL1_W, .type = ARM_CP_NOP },
1047b9d7
PM
4191 REGINFO_SENTINEL
4192};
4193
4194static const ARMCPRegInfo dummy_c15_cp_reginfo[] = {
4195 /* RAZ/WI the whole crn=15 space, when we don't have a more specific
4196 * implementation of this implementation-defined space.
4197 * Ideally this should eventually disappear in favour of actually
4198 * implementing the correct behaviour for all cores.
4199 */
4200 { .name = "C15_IMPDEF", .cp = 15, .crn = 15,
4201 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
3671cd87 4202 .access = PL1_RW,
7a0e58fa 4203 .type = ARM_CP_CONST | ARM_CP_NO_RAW | ARM_CP_OVERRIDE,
d4e6df63 4204 .resetvalue = 0 },
18032bec
PM
4205 REGINFO_SENTINEL
4206};
4207
c4804214
PM
4208static const ARMCPRegInfo cache_dirty_status_cp_reginfo[] = {
4209 /* Cache status: RAZ because we have no cache so it's always clean */
4210 { .name = "CDSR", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 6,
7a0e58fa 4211 .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 4212 .resetvalue = 0 },
c4804214
PM
4213 REGINFO_SENTINEL
4214};
4215
4216static const ARMCPRegInfo cache_block_ops_cp_reginfo[] = {
4217 /* We never have a a block transfer operation in progress */
4218 { .name = "BXSR", .cp = 15, .crn = 7, .crm = 12, .opc1 = 0, .opc2 = 4,
7a0e58fa 4219 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 4220 .resetvalue = 0 },
30b05bba
PM
4221 /* The cache ops themselves: these all NOP for QEMU */
4222 { .name = "IICR", .cp = 15, .crm = 5, .opc1 = 0,
4223 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
4224 { .name = "IDCR", .cp = 15, .crm = 6, .opc1 = 0,
4225 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
4226 { .name = "CDCR", .cp = 15, .crm = 12, .opc1 = 0,
4227 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
4228 { .name = "PIR", .cp = 15, .crm = 12, .opc1 = 1,
4229 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
4230 { .name = "PDR", .cp = 15, .crm = 12, .opc1 = 2,
4231 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
4232 { .name = "CIDCR", .cp = 15, .crm = 14, .opc1 = 0,
4233 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
c4804214
PM
4234 REGINFO_SENTINEL
4235};
4236
4237static const ARMCPRegInfo cache_test_clean_cp_reginfo[] = {
4238 /* The cache test-and-clean instructions always return (1 << 30)
4239 * to indicate that there are no dirty cache lines.
4240 */
4241 { .name = "TC_DCACHE", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 3,
7a0e58fa 4242 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 4243 .resetvalue = (1 << 30) },
c4804214 4244 { .name = "TCI_DCACHE", .cp = 15, .crn = 7, .crm = 14, .opc1 = 0, .opc2 = 3,
7a0e58fa 4245 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 4246 .resetvalue = (1 << 30) },
c4804214
PM
4247 REGINFO_SENTINEL
4248};
4249
34f90529
PM
4250static const ARMCPRegInfo strongarm_cp_reginfo[] = {
4251 /* Ignore ReadBuffer accesses */
4252 { .name = "C9_READBUFFER", .cp = 15, .crn = 9,
4253 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
d4e6df63 4254 .access = PL1_RW, .resetvalue = 0,
7a0e58fa 4255 .type = ARM_CP_CONST | ARM_CP_OVERRIDE | ARM_CP_NO_RAW },
34f90529
PM
4256 REGINFO_SENTINEL
4257};
4258
731de9e6
EI
4259static uint64_t midr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4260{
2fc0cc0e 4261 ARMCPU *cpu = env_archcpu(env);
731de9e6
EI
4262 unsigned int cur_el = arm_current_el(env);
4263 bool secure = arm_is_secure(env);
4264
4265 if (arm_feature(&cpu->env, ARM_FEATURE_EL2) && !secure && cur_el == 1) {
4266 return env->cp15.vpidr_el2;
4267 }
4268 return raw_read(env, ri);
4269}
4270
06a7e647 4271static uint64_t mpidr_read_val(CPUARMState *env)
81bdde9d 4272{
2fc0cc0e 4273 ARMCPU *cpu = env_archcpu(env);
eb5e1d3c
PF
4274 uint64_t mpidr = cpu->mp_affinity;
4275
81bdde9d 4276 if (arm_feature(env, ARM_FEATURE_V7MP)) {
78dbbbe4 4277 mpidr |= (1U << 31);
81bdde9d
PM
4278 /* Cores which are uniprocessor (non-coherent)
4279 * but still implement the MP extensions set
a8e81b31 4280 * bit 30. (For instance, Cortex-R5).
81bdde9d 4281 */
a8e81b31
PC
4282 if (cpu->mp_is_up) {
4283 mpidr |= (1u << 30);
4284 }
81bdde9d 4285 }
c4241c7d 4286 return mpidr;
81bdde9d
PM
4287}
4288
06a7e647
EI
4289static uint64_t mpidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4290{
f0d574d6
EI
4291 unsigned int cur_el = arm_current_el(env);
4292 bool secure = arm_is_secure(env);
4293
4294 if (arm_feature(env, ARM_FEATURE_EL2) && !secure && cur_el == 1) {
4295 return env->cp15.vmpidr_el2;
4296 }
06a7e647
EI
4297 return mpidr_read_val(env);
4298}
4299
7ac681cf 4300static const ARMCPRegInfo lpae_cp_reginfo[] = {
a903c449 4301 /* NOP AMAIR0/1 */
b0fe2427
PM
4302 { .name = "AMAIR0", .state = ARM_CP_STATE_BOTH,
4303 .opc0 = 3, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 0,
84929218
RH
4304 .access = PL1_RW, .accessfn = access_tvm_trvm,
4305 .type = ARM_CP_CONST, .resetvalue = 0 },
b0fe2427 4306 /* AMAIR1 is mapped to AMAIR_EL1[63:32] */
7ac681cf 4307 { .name = "AMAIR1", .cp = 15, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 1,
84929218
RH
4308 .access = PL1_RW, .accessfn = access_tvm_trvm,
4309 .type = ARM_CP_CONST, .resetvalue = 0 },
891a2fe7 4310 { .name = "PAR", .cp = 15, .crm = 7, .opc1 = 0,
01c097f7
FA
4311 .access = PL1_RW, .type = ARM_CP_64BIT, .resetvalue = 0,
4312 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.par_s),
4313 offsetof(CPUARMState, cp15.par_ns)} },
891a2fe7 4314 { .name = "TTBR0", .cp = 15, .crm = 2, .opc1 = 0,
84929218
RH
4315 .access = PL1_RW, .accessfn = access_tvm_trvm,
4316 .type = ARM_CP_64BIT | ARM_CP_ALIAS,
7dd8c9af
FA
4317 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
4318 offsetof(CPUARMState, cp15.ttbr0_ns) },
b061a82b 4319 .writefn = vmsa_ttbr_write, },
891a2fe7 4320 { .name = "TTBR1", .cp = 15, .crm = 2, .opc1 = 1,
84929218
RH
4321 .access = PL1_RW, .accessfn = access_tvm_trvm,
4322 .type = ARM_CP_64BIT | ARM_CP_ALIAS,
7dd8c9af
FA
4323 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
4324 offsetof(CPUARMState, cp15.ttbr1_ns) },
b061a82b 4325 .writefn = vmsa_ttbr_write, },
7ac681cf
PM
4326 REGINFO_SENTINEL
4327};
4328
c4241c7d 4329static uint64_t aa64_fpcr_read(CPUARMState *env, const ARMCPRegInfo *ri)
b0d2b7d0 4330{
c4241c7d 4331 return vfp_get_fpcr(env);
b0d2b7d0
PM
4332}
4333
c4241c7d
PM
4334static void aa64_fpcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4335 uint64_t value)
b0d2b7d0
PM
4336{
4337 vfp_set_fpcr(env, value);
b0d2b7d0
PM
4338}
4339
c4241c7d 4340static uint64_t aa64_fpsr_read(CPUARMState *env, const ARMCPRegInfo *ri)
b0d2b7d0 4341{
c4241c7d 4342 return vfp_get_fpsr(env);
b0d2b7d0
PM
4343}
4344
c4241c7d
PM
4345static void aa64_fpsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4346 uint64_t value)
b0d2b7d0
PM
4347{
4348 vfp_set_fpsr(env, value);
b0d2b7d0
PM
4349}
4350
3f208fd7
PM
4351static CPAccessResult aa64_daif_access(CPUARMState *env, const ARMCPRegInfo *ri,
4352 bool isread)
c2b820fe 4353{
aaec1432 4354 if (arm_current_el(env) == 0 && !(arm_sctlr(env, 0) & SCTLR_UMA)) {
c2b820fe
PM
4355 return CP_ACCESS_TRAP;
4356 }
4357 return CP_ACCESS_OK;
4358}
4359
4360static void aa64_daif_write(CPUARMState *env, const ARMCPRegInfo *ri,
4361 uint64_t value)
4362{
4363 env->daif = value & PSTATE_DAIF;
4364}
4365
220f508f
RH
4366static uint64_t aa64_pan_read(CPUARMState *env, const ARMCPRegInfo *ri)
4367{
4368 return env->pstate & PSTATE_PAN;
4369}
4370
4371static void aa64_pan_write(CPUARMState *env, const ARMCPRegInfo *ri,
4372 uint64_t value)
4373{
4374 env->pstate = (env->pstate & ~PSTATE_PAN) | (value & PSTATE_PAN);
4375}
4376
4377static const ARMCPRegInfo pan_reginfo = {
4378 .name = "PAN", .state = ARM_CP_STATE_AA64,
4379 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 3,
4380 .type = ARM_CP_NO_RAW, .access = PL1_RW,
4381 .readfn = aa64_pan_read, .writefn = aa64_pan_write
4382};
4383
9eeb7a1c
RH
4384static uint64_t aa64_uao_read(CPUARMState *env, const ARMCPRegInfo *ri)
4385{
4386 return env->pstate & PSTATE_UAO;
4387}
4388
4389static void aa64_uao_write(CPUARMState *env, const ARMCPRegInfo *ri,
4390 uint64_t value)
4391{
4392 env->pstate = (env->pstate & ~PSTATE_UAO) | (value & PSTATE_UAO);
4393}
4394
4395static const ARMCPRegInfo uao_reginfo = {
4396 .name = "UAO", .state = ARM_CP_STATE_AA64,
4397 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 4,
4398 .type = ARM_CP_NO_RAW, .access = PL1_RW,
4399 .readfn = aa64_uao_read, .writefn = aa64_uao_write
4400};
4401
38262d8a
RH
4402static CPAccessResult aa64_cacheop_poc_access(CPUARMState *env,
4403 const ARMCPRegInfo *ri,
4404 bool isread)
8af35c37 4405{
38262d8a
RH
4406 /* Cache invalidate/clean to Point of Coherency or Persistence... */
4407 switch (arm_current_el(env)) {
4408 case 0:
4409 /* ... EL0 must UNDEF unless SCTLR_EL1.UCI is set. */
4410 if (!(arm_sctlr(env, 0) & SCTLR_UCI)) {
4411 return CP_ACCESS_TRAP;
4412 }
4413 /* fall through */
4414 case 1:
4415 /* ... EL1 must trap to EL2 if HCR_EL2.TPCP is set. */
4416 if (arm_hcr_el2_eff(env) & HCR_TPCP) {
4417 return CP_ACCESS_TRAP_EL2;
4418 }
4419 break;
8af35c37
PM
4420 }
4421 return CP_ACCESS_OK;
4422}
4423
38262d8a 4424static CPAccessResult aa64_cacheop_pou_access(CPUARMState *env,
1bed4d2e
RH
4425 const ARMCPRegInfo *ri,
4426 bool isread)
4427{
38262d8a 4428 /* Cache invalidate/clean to Point of Unification... */
1bed4d2e
RH
4429 switch (arm_current_el(env)) {
4430 case 0:
4431 /* ... EL0 must UNDEF unless SCTLR_EL1.UCI is set. */
4432 if (!(arm_sctlr(env, 0) & SCTLR_UCI)) {
4433 return CP_ACCESS_TRAP;
4434 }
4435 /* fall through */
4436 case 1:
38262d8a
RH
4437 /* ... EL1 must trap to EL2 if HCR_EL2.TPU is set. */
4438 if (arm_hcr_el2_eff(env) & HCR_TPU) {
1bed4d2e
RH
4439 return CP_ACCESS_TRAP_EL2;
4440 }
4441 break;
4442 }
4443 return CP_ACCESS_OK;
4444}
4445
dbb1fb27
AB
4446/* See: D4.7.2 TLB maintenance requirements and the TLB maintenance instructions
4447 * Page D4-1736 (DDI0487A.b)
4448 */
4449
b7e0730d
RH
4450static int vae1_tlbmask(CPUARMState *env)
4451{
85d0dc9f 4452 /* Since we exclude secure first, we may read HCR_EL2 directly. */
b7e0730d 4453 if (arm_is_secure_below_el3(env)) {
452ef8cb
RH
4454 return ARMMMUIdxBit_SE10_1 |
4455 ARMMMUIdxBit_SE10_1_PAN |
4456 ARMMMUIdxBit_SE10_0;
85d0dc9f
RH
4457 } else if ((env->cp15.hcr_el2 & (HCR_E2H | HCR_TGE))
4458 == (HCR_E2H | HCR_TGE)) {
452ef8cb
RH
4459 return ARMMMUIdxBit_E20_2 |
4460 ARMMMUIdxBit_E20_2_PAN |
4461 ARMMMUIdxBit_E20_0;
b7e0730d 4462 } else {
452ef8cb
RH
4463 return ARMMMUIdxBit_E10_1 |
4464 ARMMMUIdxBit_E10_1_PAN |
4465 ARMMMUIdxBit_E10_0;
b7e0730d
RH
4466 }
4467}
4468
fd3ed969
PM
4469static void tlbi_aa64_vmalle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4470 uint64_t value)
168aa23b 4471{
29a0af61 4472 CPUState *cs = env_cpu(env);
b7e0730d 4473 int mask = vae1_tlbmask(env);
dbb1fb27 4474
b7e0730d 4475 tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
168aa23b
PM
4476}
4477
b4ab8ce9
PM
4478static void tlbi_aa64_vmalle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4479 uint64_t value)
4480{
29a0af61 4481 CPUState *cs = env_cpu(env);
b7e0730d 4482 int mask = vae1_tlbmask(env);
b4ab8ce9
PM
4483
4484 if (tlb_force_broadcast(env)) {
527db2be
RH
4485 tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
4486 } else {
4487 tlb_flush_by_mmuidx(cs, mask);
b4ab8ce9 4488 }
b4ab8ce9
PM
4489}
4490
90c19cdf 4491static int alle1_tlbmask(CPUARMState *env)
168aa23b 4492{
90c19cdf
RH
4493 /*
4494 * Note that the 'ALL' scope must invalidate both stage 1 and
fd3ed969
PM
4495 * stage 2 translations, whereas most other scopes only invalidate
4496 * stage 1 translations.
4497 */
fd3ed969 4498 if (arm_is_secure_below_el3(env)) {
452ef8cb
RH
4499 return ARMMMUIdxBit_SE10_1 |
4500 ARMMMUIdxBit_SE10_1_PAN |
4501 ARMMMUIdxBit_SE10_0;
fd3ed969 4502 } else {
452ef8cb
RH
4503 return ARMMMUIdxBit_E10_1 |
4504 ARMMMUIdxBit_E10_1_PAN |
4505 ARMMMUIdxBit_E10_0;
fd3ed969 4506 }
168aa23b
PM
4507}
4508
85d0dc9f
RH
4509static int e2_tlbmask(CPUARMState *env)
4510{
4511 /* TODO: ARMv8.4-SecEL2 */
452ef8cb
RH
4512 return ARMMMUIdxBit_E20_0 |
4513 ARMMMUIdxBit_E20_2 |
4514 ARMMMUIdxBit_E20_2_PAN |
4515 ARMMMUIdxBit_E2;
85d0dc9f
RH
4516}
4517
90c19cdf
RH
4518static void tlbi_aa64_alle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4519 uint64_t value)
4520{
4521 CPUState *cs = env_cpu(env);
4522 int mask = alle1_tlbmask(env);
4523
4524 tlb_flush_by_mmuidx(cs, mask);
4525}
4526
fd3ed969 4527static void tlbi_aa64_alle2_write(CPUARMState *env, const ARMCPRegInfo *ri,
fa439fc5
PM
4528 uint64_t value)
4529{
85d0dc9f
RH
4530 CPUState *cs = env_cpu(env);
4531 int mask = e2_tlbmask(env);
fd3ed969 4532
85d0dc9f 4533 tlb_flush_by_mmuidx(cs, mask);
fd3ed969
PM
4534}
4535
43efaa33
PM
4536static void tlbi_aa64_alle3_write(CPUARMState *env, const ARMCPRegInfo *ri,
4537 uint64_t value)
4538{
2fc0cc0e 4539 ARMCPU *cpu = env_archcpu(env);
43efaa33
PM
4540 CPUState *cs = CPU(cpu);
4541
127b2b08 4542 tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_SE3);
43efaa33
PM
4543}
4544
fd3ed969
PM
4545static void tlbi_aa64_alle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4546 uint64_t value)
4547{
29a0af61 4548 CPUState *cs = env_cpu(env);
90c19cdf
RH
4549 int mask = alle1_tlbmask(env);
4550
4551 tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
fa439fc5
PM
4552}
4553
2bfb9d75
PM
4554static void tlbi_aa64_alle2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4555 uint64_t value)
4556{
29a0af61 4557 CPUState *cs = env_cpu(env);
85d0dc9f 4558 int mask = e2_tlbmask(env);
2bfb9d75 4559
85d0dc9f 4560 tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
2bfb9d75
PM
4561}
4562
43efaa33
PM
4563static void tlbi_aa64_alle3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4564 uint64_t value)
4565{
29a0af61 4566 CPUState *cs = env_cpu(env);
43efaa33 4567
127b2b08 4568 tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_SE3);
43efaa33
PM
4569}
4570
fd3ed969
PM
4571static void tlbi_aa64_vae2_write(CPUARMState *env, const ARMCPRegInfo *ri,
4572 uint64_t value)
fa439fc5 4573{
fd3ed969
PM
4574 /* Invalidate by VA, EL2
4575 * Currently handles both VAE2 and VALE2, since we don't support
4576 * flush-last-level-only.
4577 */
85d0dc9f
RH
4578 CPUState *cs = env_cpu(env);
4579 int mask = e2_tlbmask(env);
fd3ed969
PM
4580 uint64_t pageaddr = sextract64(value << 12, 0, 56);
4581
85d0dc9f 4582 tlb_flush_page_by_mmuidx(cs, pageaddr, mask);
fd3ed969
PM
4583}
4584
43efaa33
PM
4585static void tlbi_aa64_vae3_write(CPUARMState *env, const ARMCPRegInfo *ri,
4586 uint64_t value)
4587{
4588 /* Invalidate by VA, EL3
4589 * Currently handles both VAE3 and VALE3, since we don't support
4590 * flush-last-level-only.
4591 */
2fc0cc0e 4592 ARMCPU *cpu = env_archcpu(env);
43efaa33
PM
4593 CPUState *cs = CPU(cpu);
4594 uint64_t pageaddr = sextract64(value << 12, 0, 56);
4595
127b2b08 4596 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_SE3);
43efaa33
PM
4597}
4598
fd3ed969
PM
4599static void tlbi_aa64_vae1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4600 uint64_t value)
4601{
90c19cdf
RH
4602 CPUState *cs = env_cpu(env);
4603 int mask = vae1_tlbmask(env);
fa439fc5
PM
4604 uint64_t pageaddr = sextract64(value << 12, 0, 56);
4605
90c19cdf 4606 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr, mask);
fa439fc5
PM
4607}
4608
b4ab8ce9
PM
4609static void tlbi_aa64_vae1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4610 uint64_t value)
4611{
4612 /* Invalidate by VA, EL1&0 (AArch64 version).
4613 * Currently handles all of VAE1, VAAE1, VAALE1 and VALE1,
4614 * since we don't support flush-for-specific-ASID-only or
4615 * flush-last-level-only.
4616 */
90c19cdf
RH
4617 CPUState *cs = env_cpu(env);
4618 int mask = vae1_tlbmask(env);
b4ab8ce9
PM
4619 uint64_t pageaddr = sextract64(value << 12, 0, 56);
4620
4621 if (tlb_force_broadcast(env)) {
527db2be
RH
4622 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr, mask);
4623 } else {
4624 tlb_flush_page_by_mmuidx(cs, pageaddr, mask);
b4ab8ce9 4625 }
b4ab8ce9
PM
4626}
4627
fd3ed969
PM
4628static void tlbi_aa64_vae2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4629 uint64_t value)
fa439fc5 4630{
29a0af61 4631 CPUState *cs = env_cpu(env);
fd3ed969 4632 uint64_t pageaddr = sextract64(value << 12, 0, 56);
fa439fc5 4633
a67cf277 4634 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
e013b741 4635 ARMMMUIdxBit_E2);
fa439fc5
PM
4636}
4637
43efaa33
PM
4638static void tlbi_aa64_vae3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4639 uint64_t value)
4640{
29a0af61 4641 CPUState *cs = env_cpu(env);
43efaa33
PM
4642 uint64_t pageaddr = sextract64(value << 12, 0, 56);
4643
a67cf277 4644 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
127b2b08 4645 ARMMMUIdxBit_SE3);
43efaa33
PM
4646}
4647
3f208fd7
PM
4648static CPAccessResult aa64_zva_access(CPUARMState *env, const ARMCPRegInfo *ri,
4649 bool isread)
aca3f40b 4650{
4351cb72
RH
4651 int cur_el = arm_current_el(env);
4652
4653 if (cur_el < 2) {
4654 uint64_t hcr = arm_hcr_el2_eff(env);
4655
4656 if (cur_el == 0) {
4657 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
4658 if (!(env->cp15.sctlr_el[2] & SCTLR_DZE)) {
4659 return CP_ACCESS_TRAP_EL2;
4660 }
4661 } else {
4662 if (!(env->cp15.sctlr_el[1] & SCTLR_DZE)) {
4663 return CP_ACCESS_TRAP;
4664 }
4665 if (hcr & HCR_TDZ) {
4666 return CP_ACCESS_TRAP_EL2;
4667 }
4668 }
4669 } else if (hcr & HCR_TDZ) {
4670 return CP_ACCESS_TRAP_EL2;
4671 }
aca3f40b
PM
4672 }
4673 return CP_ACCESS_OK;
4674}
4675
4676static uint64_t aa64_dczid_read(CPUARMState *env, const ARMCPRegInfo *ri)
4677{
2fc0cc0e 4678 ARMCPU *cpu = env_archcpu(env);
aca3f40b
PM
4679 int dzp_bit = 1 << 4;
4680
4681 /* DZP indicates whether DC ZVA access is allowed */
3f208fd7 4682 if (aa64_zva_access(env, NULL, false) == CP_ACCESS_OK) {
aca3f40b
PM
4683 dzp_bit = 0;
4684 }
4685 return cpu->dcz_blocksize | dzp_bit;
4686}
4687
3f208fd7
PM
4688static CPAccessResult sp_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
4689 bool isread)
f502cfc2 4690{
cdcf1405 4691 if (!(env->pstate & PSTATE_SP)) {
f502cfc2
PM
4692 /* Access to SP_EL0 is undefined if it's being used as
4693 * the stack pointer.
4694 */
4695 return CP_ACCESS_TRAP_UNCATEGORIZED;
4696 }
4697 return CP_ACCESS_OK;
4698}
4699
4700static uint64_t spsel_read(CPUARMState *env, const ARMCPRegInfo *ri)
4701{
4702 return env->pstate & PSTATE_SP;
4703}
4704
4705static void spsel_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
4706{
4707 update_spsel(env, val);
4708}
4709
137feaa9
FA
4710static void sctlr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4711 uint64_t value)
4712{
2fc0cc0e 4713 ARMCPU *cpu = env_archcpu(env);
137feaa9
FA
4714
4715 if (raw_read(env, ri) == value) {
4716 /* Skip the TLB flush if nothing actually changed; Linux likes
4717 * to do a lot of pointless SCTLR writes.
4718 */
4719 return;
4720 }
4721
06312feb
PM
4722 if (arm_feature(env, ARM_FEATURE_PMSA) && !cpu->has_mpu) {
4723 /* M bit is RAZ/WI for PMSA with no MPU implemented */
4724 value &= ~SCTLR_M;
4725 }
4726
137feaa9
FA
4727 raw_write(env, ri, value);
4728 /* ??? Lots of these bits are not implemented. */
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,
5149 .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any,
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,
5197 .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any,
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 }
ef682cdb 5254 }
f149e3e8
EI
5255
5256 /* Clear RES0 bits. */
5257 value &= valid_mask;
5258
5259 /* These bits change the MMU setup:
5260 * HCR_VM enables stage 2 translation
5261 * HCR_PTW forbids certain page-table setups
5262 * HCR_DC Disables stage1 and enables stage2 translation
5263 */
ce4afed8 5264 if ((env->cp15.hcr_el2 ^ value) & (HCR_VM | HCR_PTW | HCR_DC)) {
d10eb08f 5265 tlb_flush(CPU(cpu));
f149e3e8 5266 }
ce4afed8 5267 env->cp15.hcr_el2 = value;
89430fc6
PM
5268
5269 /*
5270 * Updates to VI and VF require us to update the status of
5271 * virtual interrupts, which are the logical OR of these bits
5272 * and the state of the input lines from the GIC. (This requires
5273 * that we have the iothread lock, which is done by marking the
5274 * reginfo structs as ARM_CP_IO.)
5275 * Note that if a write to HCR pends a VIRQ or VFIQ it is never
5276 * possible for it to be taken immediately, because VIRQ and
5277 * VFIQ are masked unless running at EL0 or EL1, and HCR
5278 * can only be written at EL2.
5279 */
5280 g_assert(qemu_mutex_iothread_locked());
5281 arm_cpu_update_virq(cpu);
5282 arm_cpu_update_vfiq(cpu);
ce4afed8
PM
5283}
5284
d1fb4da2
RH
5285static void hcr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
5286{
5287 do_hcr_write(env, value, 0);
5288}
5289
ce4afed8
PM
5290static void hcr_writehigh(CPUARMState *env, const ARMCPRegInfo *ri,
5291 uint64_t value)
5292{
5293 /* Handle HCR2 write, i.e. write to high half of HCR_EL2 */
5294 value = deposit64(env->cp15.hcr_el2, 32, 32, value);
d1fb4da2 5295 do_hcr_write(env, value, MAKE_64BIT_MASK(0, 32));
ce4afed8
PM
5296}
5297
5298static void hcr_writelow(CPUARMState *env, const ARMCPRegInfo *ri,
5299 uint64_t value)
5300{
5301 /* Handle HCR write, i.e. write to low half of HCR_EL2 */
5302 value = deposit64(env->cp15.hcr_el2, 0, 32, value);
d1fb4da2 5303 do_hcr_write(env, value, MAKE_64BIT_MASK(32, 32));
f149e3e8
EI
5304}
5305
f7778444
RH
5306/*
5307 * Return the effective value of HCR_EL2.
5308 * Bits that are not included here:
5309 * RW (read from SCR_EL3.RW as needed)
5310 */
5311uint64_t arm_hcr_el2_eff(CPUARMState *env)
5312{
5313 uint64_t ret = env->cp15.hcr_el2;
5314
5315 if (arm_is_secure_below_el3(env)) {
5316 /*
5317 * "This register has no effect if EL2 is not enabled in the
5318 * current Security state". This is ARMv8.4-SecEL2 speak for
5319 * !(SCR_EL3.NS==1 || SCR_EL3.EEL2==1).
5320 *
5321 * Prior to that, the language was "In an implementation that
5322 * includes EL3, when the value of SCR_EL3.NS is 0 the PE behaves
5323 * as if this field is 0 for all purposes other than a direct
5324 * read or write access of HCR_EL2". With lots of enumeration
5325 * on a per-field basis. In current QEMU, this is condition
5326 * is arm_is_secure_below_el3.
5327 *
5328 * Since the v8.4 language applies to the entire register, and
5329 * appears to be backward compatible, use that.
5330 */
4990e1d3
RH
5331 return 0;
5332 }
5333
5334 /*
5335 * For a cpu that supports both aarch64 and aarch32, we can set bits
5336 * in HCR_EL2 (e.g. via EL3) that are RES0 when we enter EL2 as aa32.
5337 * Ignore all of the bits in HCR+HCR2 that are not valid for aarch32.
5338 */
5339 if (!arm_el_is_aa64(env, 2)) {
5340 uint64_t aa32_valid;
5341
5342 /*
5343 * These bits are up-to-date as of ARMv8.6.
5344 * For HCR, it's easiest to list just the 2 bits that are invalid.
5345 * For HCR2, list those that are valid.
5346 */
5347 aa32_valid = MAKE_64BIT_MASK(0, 32) & ~(HCR_RW | HCR_TDZ);
5348 aa32_valid |= (HCR_CD | HCR_ID | HCR_TERR | HCR_TEA | HCR_MIOCNCE |
5349 HCR_TID4 | HCR_TICAB | HCR_TOCU | HCR_TTLBIS);
5350 ret &= aa32_valid;
5351 }
5352
5353 if (ret & HCR_TGE) {
5354 /* These bits are up-to-date as of ARMv8.6. */
f7778444
RH
5355 if (ret & HCR_E2H) {
5356 ret &= ~(HCR_VM | HCR_FMO | HCR_IMO | HCR_AMO |
5357 HCR_BSU_MASK | HCR_DC | HCR_TWI | HCR_TWE |
5358 HCR_TID0 | HCR_TID2 | HCR_TPCP | HCR_TPU |
4990e1d3
RH
5359 HCR_TDZ | HCR_CD | HCR_ID | HCR_MIOCNCE |
5360 HCR_TID4 | HCR_TICAB | HCR_TOCU | HCR_ENSCXT |
5361 HCR_TTLBIS | HCR_TTLBOS | HCR_TID5);
f7778444
RH
5362 } else {
5363 ret |= HCR_FMO | HCR_IMO | HCR_AMO;
5364 }
5365 ret &= ~(HCR_SWIO | HCR_PTW | HCR_VF | HCR_VI | HCR_VSE |
5366 HCR_FB | HCR_TID1 | HCR_TID3 | HCR_TSC | HCR_TACR |
5367 HCR_TSW | HCR_TTLB | HCR_TVM | HCR_HCD | HCR_TRVM |
5368 HCR_TLOR);
5369 }
5370
5371 return ret;
5372}
5373
fc1120a7
PM
5374static void cptr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
5375 uint64_t value)
5376{
5377 /*
5378 * For A-profile AArch32 EL3, if NSACR.CP10
5379 * is 0 then HCPTR.{TCP11,TCP10} ignore writes and read as 1.
5380 */
5381 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
5382 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
5383 value &= ~(0x3 << 10);
5384 value |= env->cp15.cptr_el[2] & (0x3 << 10);
5385 }
5386 env->cp15.cptr_el[2] = value;
5387}
5388
5389static uint64_t cptr_el2_read(CPUARMState *env, const ARMCPRegInfo *ri)
5390{
5391 /*
5392 * For A-profile AArch32 EL3, if NSACR.CP10
5393 * is 0 then HCPTR.{TCP11,TCP10} ignore writes and read as 1.
5394 */
5395 uint64_t value = env->cp15.cptr_el[2];
5396
5397 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
5398 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
5399 value |= 0x3 << 10;
5400 }
5401 return value;
5402}
5403
4771cd01 5404static const ARMCPRegInfo el2_cp_reginfo[] = {
f149e3e8 5405 { .name = "HCR_EL2", .state = ARM_CP_STATE_AA64,
89430fc6 5406 .type = ARM_CP_IO,
f149e3e8
EI
5407 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
5408 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
c624ea0f 5409 .writefn = hcr_write },
ce4afed8 5410 { .name = "HCR", .state = ARM_CP_STATE_AA32,
89430fc6 5411 .type = ARM_CP_ALIAS | ARM_CP_IO,
ce4afed8
PM
5412 .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
5413 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
c624ea0f 5414 .writefn = hcr_writelow },
831a2fca
PM
5415 { .name = "HACR_EL2", .state = ARM_CP_STATE_BOTH,
5416 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 7,
5417 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3b685ba7 5418 { .name = "ELR_EL2", .state = ARM_CP_STATE_AA64,
7a0e58fa 5419 .type = ARM_CP_ALIAS,
3b685ba7
EI
5420 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 1,
5421 .access = PL2_RW,
5422 .fieldoffset = offsetof(CPUARMState, elr_el[2]) },
68e78e33 5423 { .name = "ESR_EL2", .state = ARM_CP_STATE_BOTH,
f2c30f42
EI
5424 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 0,
5425 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[2]) },
cba517c3 5426 { .name = "FAR_EL2", .state = ARM_CP_STATE_BOTH,
63b60551
EI
5427 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 0,
5428 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[2]) },
cba517c3
PM
5429 { .name = "HIFAR", .state = ARM_CP_STATE_AA32,
5430 .type = ARM_CP_ALIAS,
5431 .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 2,
5432 .access = PL2_RW,
5433 .fieldoffset = offsetofhigh32(CPUARMState, cp15.far_el[2]) },
3b685ba7 5434 { .name = "SPSR_EL2", .state = ARM_CP_STATE_AA64,
7a0e58fa 5435 .type = ARM_CP_ALIAS,
3b685ba7 5436 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 0,
99a99c1f
SB
5437 .access = PL2_RW,
5438 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_HYP]) },
d79e0c06 5439 { .name = "VBAR_EL2", .state = ARM_CP_STATE_BOTH,
d42e3c26
EI
5440 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0,
5441 .access = PL2_RW, .writefn = vbar_write,
5442 .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[2]),
5443 .resetvalue = 0 },
884b4dee
GB
5444 { .name = "SP_EL2", .state = ARM_CP_STATE_AA64,
5445 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 1, .opc2 = 0,
7a0e58fa 5446 .access = PL3_RW, .type = ARM_CP_ALIAS,
884b4dee 5447 .fieldoffset = offsetof(CPUARMState, sp_el[2]) },
c6f19164
GB
5448 { .name = "CPTR_EL2", .state = ARM_CP_STATE_BOTH,
5449 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 2,
5450 .access = PL2_RW, .accessfn = cptr_access, .resetvalue = 0,
fc1120a7
PM
5451 .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[2]),
5452 .readfn = cptr_el2_read, .writefn = cptr_el2_write },
95f949ac
EI
5453 { .name = "MAIR_EL2", .state = ARM_CP_STATE_BOTH,
5454 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 0,
5455 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[2]),
5456 .resetvalue = 0 },
5457 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
b5ede85b 5458 .cp = 15, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 1,
95f949ac
EI
5459 .access = PL2_RW, .type = ARM_CP_ALIAS,
5460 .fieldoffset = offsetofhigh32(CPUARMState, cp15.mair_el[2]) },
2179ef95
PM
5461 { .name = "AMAIR_EL2", .state = ARM_CP_STATE_BOTH,
5462 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 0,
5463 .access = PL2_RW, .type = ARM_CP_CONST,
5464 .resetvalue = 0 },
5465 /* HAMAIR1 is mapped to AMAIR_EL2[63:32] */
55b53c71 5466 { .name = "HAMAIR1", .state = ARM_CP_STATE_AA32,
b5ede85b 5467 .cp = 15, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 1,
2179ef95
PM
5468 .access = PL2_RW, .type = ARM_CP_CONST,
5469 .resetvalue = 0 },
37cd6c24
PM
5470 { .name = "AFSR0_EL2", .state = ARM_CP_STATE_BOTH,
5471 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 0,
5472 .access = PL2_RW, .type = ARM_CP_CONST,
5473 .resetvalue = 0 },
5474 { .name = "AFSR1_EL2", .state = ARM_CP_STATE_BOTH,
5475 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 1,
5476 .access = PL2_RW, .type = ARM_CP_CONST,
5477 .resetvalue = 0 },
06ec4c8c
EI
5478 { .name = "TCR_EL2", .state = ARM_CP_STATE_BOTH,
5479 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 2,
d06dc933
RH
5480 .access = PL2_RW, .writefn = vmsa_tcr_el12_write,
5481 /* no .raw_writefn or .resetfn needed as we never use mask/base_mask */
06ec4c8c 5482 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[2]) },
68e9c2fe
EI
5483 { .name = "VTCR", .state = ARM_CP_STATE_AA32,
5484 .cp = 15, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
bf06c112 5485 .type = ARM_CP_ALIAS,
68e9c2fe
EI
5486 .access = PL2_RW, .accessfn = access_el3_aa32ns,
5487 .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) },
5488 { .name = "VTCR_EL2", .state = ARM_CP_STATE_AA64,
5489 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
bf06c112
PM
5490 .access = PL2_RW,
5491 /* no .writefn needed as this can't cause an ASID change;
5492 * no .raw_writefn or .resetfn needed as we never use mask/base_mask
5493 */
68e9c2fe 5494 .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) },
b698e9cf
EI
5495 { .name = "VTTBR", .state = ARM_CP_STATE_AA32,
5496 .cp = 15, .opc1 = 6, .crm = 2,
5497 .type = ARM_CP_64BIT | ARM_CP_ALIAS,
5498 .access = PL2_RW, .accessfn = access_el3_aa32ns,
5499 .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2),
5500 .writefn = vttbr_write },
5501 { .name = "VTTBR_EL2", .state = ARM_CP_STATE_AA64,
5502 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 0,
5503 .access = PL2_RW, .writefn = vttbr_write,
5504 .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2) },
b9cb5323
EI
5505 { .name = "SCTLR_EL2", .state = ARM_CP_STATE_BOTH,
5506 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 0,
5507 .access = PL2_RW, .raw_writefn = raw_write, .writefn = sctlr_write,
5508 .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[2]) },
ff05f37b
EI
5509 { .name = "TPIDR_EL2", .state = ARM_CP_STATE_BOTH,
5510 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 2,
5511 .access = PL2_RW, .resetvalue = 0,
5512 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[2]) },
a57633c0
EI
5513 { .name = "TTBR0_EL2", .state = ARM_CP_STATE_AA64,
5514 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
ed30da8e 5515 .access = PL2_RW, .resetvalue = 0, .writefn = vmsa_tcr_ttbr_el2_write,
a57633c0
EI
5516 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
5517 { .name = "HTTBR", .cp = 15, .opc1 = 4, .crm = 2,
5518 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
a57633c0 5519 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
541ef8c2
SS
5520 { .name = "TLBIALLNSNH",
5521 .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
5522 .type = ARM_CP_NO_RAW, .access = PL2_W,
5523 .writefn = tlbiall_nsnh_write },
5524 { .name = "TLBIALLNSNHIS",
5525 .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
5526 .type = ARM_CP_NO_RAW, .access = PL2_W,
5527 .writefn = tlbiall_nsnh_is_write },
5528 { .name = "TLBIALLH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
5529 .type = ARM_CP_NO_RAW, .access = PL2_W,
5530 .writefn = tlbiall_hyp_write },
5531 { .name = "TLBIALLHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
5532 .type = ARM_CP_NO_RAW, .access = PL2_W,
5533 .writefn = tlbiall_hyp_is_write },
5534 { .name = "TLBIMVAH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
5535 .type = ARM_CP_NO_RAW, .access = PL2_W,
5536 .writefn = tlbimva_hyp_write },
5537 { .name = "TLBIMVAHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
5538 .type = ARM_CP_NO_RAW, .access = PL2_W,
5539 .writefn = tlbimva_hyp_is_write },
51da9014
EI
5540 { .name = "TLBI_ALLE2", .state = ARM_CP_STATE_AA64,
5541 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
5542 .type = ARM_CP_NO_RAW, .access = PL2_W,
fd3ed969 5543 .writefn = tlbi_aa64_alle2_write },
8742d49d
EI
5544 { .name = "TLBI_VAE2", .state = ARM_CP_STATE_AA64,
5545 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
5546 .type = ARM_CP_NO_RAW, .access = PL2_W,
fd3ed969 5547 .writefn = tlbi_aa64_vae2_write },
2bfb9d75
PM
5548 { .name = "TLBI_VALE2", .state = ARM_CP_STATE_AA64,
5549 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
5550 .access = PL2_W, .type = ARM_CP_NO_RAW,
5551 .writefn = tlbi_aa64_vae2_write },
5552 { .name = "TLBI_ALLE2IS", .state = ARM_CP_STATE_AA64,
5553 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
5554 .access = PL2_W, .type = ARM_CP_NO_RAW,
5555 .writefn = tlbi_aa64_alle2is_write },
8742d49d
EI
5556 { .name = "TLBI_VAE2IS", .state = ARM_CP_STATE_AA64,
5557 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
5558 .type = ARM_CP_NO_RAW, .access = PL2_W,
fd3ed969 5559 .writefn = tlbi_aa64_vae2is_write },
2bfb9d75
PM
5560 { .name = "TLBI_VALE2IS", .state = ARM_CP_STATE_AA64,
5561 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
5562 .access = PL2_W, .type = ARM_CP_NO_RAW,
5563 .writefn = tlbi_aa64_vae2is_write },
edac4d8a 5564#ifndef CONFIG_USER_ONLY
2a47df95
PM
5565 /* Unlike the other EL2-related AT operations, these must
5566 * UNDEF from EL3 if EL2 is not implemented, which is why we
5567 * define them here rather than with the rest of the AT ops.
5568 */
5569 { .name = "AT_S1E2R", .state = ARM_CP_STATE_AA64,
5570 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
5571 .access = PL2_W, .accessfn = at_s1e2_access,
0710b2fa 5572 .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC, .writefn = ats_write64 },
2a47df95
PM
5573 { .name = "AT_S1E2W", .state = ARM_CP_STATE_AA64,
5574 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
5575 .access = PL2_W, .accessfn = at_s1e2_access,
0710b2fa 5576 .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC, .writefn = ats_write64 },
14db7fe0
PM
5577 /* The AArch32 ATS1H* operations are CONSTRAINED UNPREDICTABLE
5578 * if EL2 is not implemented; we choose to UNDEF. Behaviour at EL3
5579 * with SCR.NS == 0 outside Monitor mode is UNPREDICTABLE; we choose
5580 * to behave as if SCR.NS was 1.
5581 */
5582 { .name = "ATS1HR", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
5583 .access = PL2_W,
0710b2fa 5584 .writefn = ats1h_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
14db7fe0
PM
5585 { .name = "ATS1HW", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
5586 .access = PL2_W,
0710b2fa 5587 .writefn = ats1h_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
0b6440af
EI
5588 { .name = "CNTHCTL_EL2", .state = ARM_CP_STATE_BOTH,
5589 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 1, .opc2 = 0,
5590 /* ARMv7 requires bit 0 and 1 to reset to 1. ARMv8 defines the
5591 * reset values as IMPDEF. We choose to reset to 3 to comply with
5592 * both ARMv7 and ARMv8.
5593 */
5594 .access = PL2_RW, .resetvalue = 3,
5595 .fieldoffset = offsetof(CPUARMState, cp15.cnthctl_el2) },
edac4d8a
EI
5596 { .name = "CNTVOFF_EL2", .state = ARM_CP_STATE_AA64,
5597 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 3,
5598 .access = PL2_RW, .type = ARM_CP_IO, .resetvalue = 0,
5599 .writefn = gt_cntvoff_write,
5600 .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
5601 { .name = "CNTVOFF", .cp = 15, .opc1 = 4, .crm = 14,
5602 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS | ARM_CP_IO,
5603 .writefn = gt_cntvoff_write,
5604 .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
b0e66d95
EI
5605 { .name = "CNTHP_CVAL_EL2", .state = ARM_CP_STATE_AA64,
5606 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 2,
5607 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
5608 .type = ARM_CP_IO, .access = PL2_RW,
5609 .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
5610 { .name = "CNTHP_CVAL", .cp = 15, .opc1 = 6, .crm = 14,
5611 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
5612 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_IO,
5613 .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
5614 { .name = "CNTHP_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
5615 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 0,
d44ec156 5616 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
b0e66d95
EI
5617 .resetfn = gt_hyp_timer_reset,
5618 .readfn = gt_hyp_tval_read, .writefn = gt_hyp_tval_write },
5619 { .name = "CNTHP_CTL_EL2", .state = ARM_CP_STATE_BOTH,
5620 .type = ARM_CP_IO,
5621 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 1,
5622 .access = PL2_RW,
5623 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].ctl),
5624 .resetvalue = 0,
5625 .writefn = gt_hyp_ctl_write, .raw_writefn = raw_write },
edac4d8a 5626#endif
14cc7b54
SF
5627 /* The only field of MDCR_EL2 that has a defined architectural reset value
5628 * is MDCR_EL2.HPMN which should reset to the value of PMCR_EL0.N; but we
5ecdd3e4 5629 * don't implement any PMU event counters, so using zero as a reset
14cc7b54
SF
5630 * value for MDCR_EL2 is okay
5631 */
5632 { .name = "MDCR_EL2", .state = ARM_CP_STATE_BOTH,
5633 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 1,
5634 .access = PL2_RW, .resetvalue = 0,
5635 .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el2), },
59e05530
EI
5636 { .name = "HPFAR", .state = ARM_CP_STATE_AA32,
5637 .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
5638 .access = PL2_RW, .accessfn = access_el3_aa32ns,
5639 .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
5640 { .name = "HPFAR_EL2", .state = ARM_CP_STATE_AA64,
5641 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
5642 .access = PL2_RW,
5643 .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
2a5a9abd
AF
5644 { .name = "HSTR_EL2", .state = ARM_CP_STATE_BOTH,
5645 .cp = 15, .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 3,
5646 .access = PL2_RW,
5647 .fieldoffset = offsetof(CPUARMState, cp15.hstr_el2) },
3b685ba7
EI
5648 REGINFO_SENTINEL
5649};
5650
ce4afed8
PM
5651static const ARMCPRegInfo el2_v8_cp_reginfo[] = {
5652 { .name = "HCR2", .state = ARM_CP_STATE_AA32,
89430fc6 5653 .type = ARM_CP_ALIAS | ARM_CP_IO,
ce4afed8
PM
5654 .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 4,
5655 .access = PL2_RW,
5656 .fieldoffset = offsetofhigh32(CPUARMState, cp15.hcr_el2),
5657 .writefn = hcr_writehigh },
5658 REGINFO_SENTINEL
5659};
5660
2f027fc5
PM
5661static CPAccessResult nsacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
5662 bool isread)
5663{
5664 /* The NSACR is RW at EL3, and RO for NS EL1 and NS EL2.
5665 * At Secure EL1 it traps to EL3.
5666 */
5667 if (arm_current_el(env) == 3) {
5668 return CP_ACCESS_OK;
5669 }
5670 if (arm_is_secure_below_el3(env)) {
5671 return CP_ACCESS_TRAP_EL3;
5672 }
5673 /* Accesses from EL1 NS and EL2 NS are UNDEF for write but allow reads. */
5674 if (isread) {
5675 return CP_ACCESS_OK;
5676 }
5677 return CP_ACCESS_TRAP_UNCATEGORIZED;
5678}
5679
60fb1a87
GB
5680static const ARMCPRegInfo el3_cp_reginfo[] = {
5681 { .name = "SCR_EL3", .state = ARM_CP_STATE_AA64,
5682 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 0,
5683 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.scr_el3),
5684 .resetvalue = 0, .writefn = scr_write },
f80741d1 5685 { .name = "SCR", .type = ARM_CP_ALIAS | ARM_CP_NEWEL,
60fb1a87 5686 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 0,
efe4a274
PM
5687 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
5688 .fieldoffset = offsetoflow32(CPUARMState, cp15.scr_el3),
b061a82b 5689 .writefn = scr_write },
60fb1a87
GB
5690 { .name = "SDER32_EL3", .state = ARM_CP_STATE_AA64,
5691 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 1,
5692 .access = PL3_RW, .resetvalue = 0,
5693 .fieldoffset = offsetof(CPUARMState, cp15.sder) },
5694 { .name = "SDER",
5695 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 1,
5696 .access = PL3_RW, .resetvalue = 0,
5697 .fieldoffset = offsetoflow32(CPUARMState, cp15.sder) },
60fb1a87 5698 { .name = "MVBAR", .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
efe4a274
PM
5699 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
5700 .writefn = vbar_write, .resetvalue = 0,
60fb1a87 5701 .fieldoffset = offsetof(CPUARMState, cp15.mvbar) },
7dd8c9af
FA
5702 { .name = "TTBR0_EL3", .state = ARM_CP_STATE_AA64,
5703 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 0,
f478847f 5704 .access = PL3_RW, .resetvalue = 0,
7dd8c9af 5705 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[3]) },
11f136ee
FA
5706 { .name = "TCR_EL3", .state = ARM_CP_STATE_AA64,
5707 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 2,
6459b94c
PM
5708 .access = PL3_RW,
5709 /* no .writefn needed as this can't cause an ASID change;
811595a2
PM
5710 * we must provide a .raw_writefn and .resetfn because we handle
5711 * reset and migration for the AArch32 TTBCR(S), which might be
5712 * using mask and base_mask.
6459b94c 5713 */
811595a2 5714 .resetfn = vmsa_ttbcr_reset, .raw_writefn = vmsa_ttbcr_raw_write,
11f136ee 5715 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[3]) },
81547d66 5716 { .name = "ELR_EL3", .state = ARM_CP_STATE_AA64,
7a0e58fa 5717 .type = ARM_CP_ALIAS,
81547d66
EI
5718 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 1,
5719 .access = PL3_RW,
5720 .fieldoffset = offsetof(CPUARMState, elr_el[3]) },
f2c30f42 5721 { .name = "ESR_EL3", .state = ARM_CP_STATE_AA64,
f2c30f42
EI
5722 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 2, .opc2 = 0,
5723 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[3]) },
63b60551
EI
5724 { .name = "FAR_EL3", .state = ARM_CP_STATE_AA64,
5725 .opc0 = 3, .opc1 = 6, .crn = 6, .crm = 0, .opc2 = 0,
5726 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[3]) },
81547d66 5727 { .name = "SPSR_EL3", .state = ARM_CP_STATE_AA64,
7a0e58fa 5728 .type = ARM_CP_ALIAS,
81547d66 5729 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 0,
99a99c1f
SB
5730 .access = PL3_RW,
5731 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_MON]) },
a1ba125c
EI
5732 { .name = "VBAR_EL3", .state = ARM_CP_STATE_AA64,
5733 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 0,
5734 .access = PL3_RW, .writefn = vbar_write,
5735 .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[3]),
5736 .resetvalue = 0 },
c6f19164
GB
5737 { .name = "CPTR_EL3", .state = ARM_CP_STATE_AA64,
5738 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 2,
5739 .access = PL3_RW, .accessfn = cptr_access, .resetvalue = 0,
5740 .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[3]) },
4cfb8ad8
PM
5741 { .name = "TPIDR_EL3", .state = ARM_CP_STATE_AA64,
5742 .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 2,
5743 .access = PL3_RW, .resetvalue = 0,
5744 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[3]) },
2179ef95
PM
5745 { .name = "AMAIR_EL3", .state = ARM_CP_STATE_AA64,
5746 .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 3, .opc2 = 0,
5747 .access = PL3_RW, .type = ARM_CP_CONST,
5748 .resetvalue = 0 },
37cd6c24
PM
5749 { .name = "AFSR0_EL3", .state = ARM_CP_STATE_BOTH,
5750 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 0,
5751 .access = PL3_RW, .type = ARM_CP_CONST,
5752 .resetvalue = 0 },
5753 { .name = "AFSR1_EL3", .state = ARM_CP_STATE_BOTH,
5754 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 1,
5755 .access = PL3_RW, .type = ARM_CP_CONST,
5756 .resetvalue = 0 },
43efaa33
PM
5757 { .name = "TLBI_ALLE3IS", .state = ARM_CP_STATE_AA64,
5758 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 0,
5759 .access = PL3_W, .type = ARM_CP_NO_RAW,
5760 .writefn = tlbi_aa64_alle3is_write },
5761 { .name = "TLBI_VAE3IS", .state = ARM_CP_STATE_AA64,
5762 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 1,
5763 .access = PL3_W, .type = ARM_CP_NO_RAW,
5764 .writefn = tlbi_aa64_vae3is_write },
5765 { .name = "TLBI_VALE3IS", .state = ARM_CP_STATE_AA64,
5766 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 5,
5767 .access = PL3_W, .type = ARM_CP_NO_RAW,
5768 .writefn = tlbi_aa64_vae3is_write },
5769 { .name = "TLBI_ALLE3", .state = ARM_CP_STATE_AA64,
5770 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 0,
5771 .access = PL3_W, .type = ARM_CP_NO_RAW,
5772 .writefn = tlbi_aa64_alle3_write },
5773 { .name = "TLBI_VAE3", .state = ARM_CP_STATE_AA64,
5774 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 1,
5775 .access = PL3_W, .type = ARM_CP_NO_RAW,
5776 .writefn = tlbi_aa64_vae3_write },
5777 { .name = "TLBI_VALE3", .state = ARM_CP_STATE_AA64,
5778 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 5,
5779 .access = PL3_W, .type = ARM_CP_NO_RAW,
5780 .writefn = tlbi_aa64_vae3_write },
0f1a3b24
FA
5781 REGINFO_SENTINEL
5782};
5783
e2cce18f
RH
5784#ifndef CONFIG_USER_ONLY
5785/* Test if system register redirection is to occur in the current state. */
5786static bool redirect_for_e2h(CPUARMState *env)
5787{
5788 return arm_current_el(env) == 2 && (arm_hcr_el2_eff(env) & HCR_E2H);
5789}
5790
5791static uint64_t el2_e2h_read(CPUARMState *env, const ARMCPRegInfo *ri)
5792{
5793 CPReadFn *readfn;
5794
5795 if (redirect_for_e2h(env)) {
5796 /* Switch to the saved EL2 version of the register. */
5797 ri = ri->opaque;
5798 readfn = ri->readfn;
5799 } else {
5800 readfn = ri->orig_readfn;
5801 }
5802 if (readfn == NULL) {
5803 readfn = raw_read;
5804 }
5805 return readfn(env, ri);
5806}
5807
5808static void el2_e2h_write(CPUARMState *env, const ARMCPRegInfo *ri,
5809 uint64_t value)
5810{
5811 CPWriteFn *writefn;
5812
5813 if (redirect_for_e2h(env)) {
5814 /* Switch to the saved EL2 version of the register. */
5815 ri = ri->opaque;
5816 writefn = ri->writefn;
5817 } else {
5818 writefn = ri->orig_writefn;
5819 }
5820 if (writefn == NULL) {
5821 writefn = raw_write;
5822 }
5823 writefn(env, ri, value);
5824}
5825
5826static void define_arm_vh_e2h_redirects_aliases(ARMCPU *cpu)
5827{
5828 struct E2HAlias {
5829 uint32_t src_key, dst_key, new_key;
5830 const char *src_name, *dst_name, *new_name;
5831 bool (*feature)(const ARMISARegisters *id);
5832 };
5833
5834#define K(op0, op1, crn, crm, op2) \
5835 ENCODE_AA64_CP_REG(CP_REG_ARM64_SYSREG_CP, crn, crm, op0, op1, op2)
5836
5837 static const struct E2HAlias aliases[] = {
5838 { K(3, 0, 1, 0, 0), K(3, 4, 1, 0, 0), K(3, 5, 1, 0, 0),
5839 "SCTLR", "SCTLR_EL2", "SCTLR_EL12" },
5840 { K(3, 0, 1, 0, 2), K(3, 4, 1, 1, 2), K(3, 5, 1, 0, 2),
5841 "CPACR", "CPTR_EL2", "CPACR_EL12" },
5842 { K(3, 0, 2, 0, 0), K(3, 4, 2, 0, 0), K(3, 5, 2, 0, 0),
5843 "TTBR0_EL1", "TTBR0_EL2", "TTBR0_EL12" },
5844 { K(3, 0, 2, 0, 1), K(3, 4, 2, 0, 1), K(3, 5, 2, 0, 1),
5845 "TTBR1_EL1", "TTBR1_EL2", "TTBR1_EL12" },
5846 { K(3, 0, 2, 0, 2), K(3, 4, 2, 0, 2), K(3, 5, 2, 0, 2),
5847 "TCR_EL1", "TCR_EL2", "TCR_EL12" },
5848 { K(3, 0, 4, 0, 0), K(3, 4, 4, 0, 0), K(3, 5, 4, 0, 0),
5849 "SPSR_EL1", "SPSR_EL2", "SPSR_EL12" },
5850 { K(3, 0, 4, 0, 1), K(3, 4, 4, 0, 1), K(3, 5, 4, 0, 1),
5851 "ELR_EL1", "ELR_EL2", "ELR_EL12" },
5852 { K(3, 0, 5, 1, 0), K(3, 4, 5, 1, 0), K(3, 5, 5, 1, 0),
5853 "AFSR0_EL1", "AFSR0_EL2", "AFSR0_EL12" },
5854 { K(3, 0, 5, 1, 1), K(3, 4, 5, 1, 1), K(3, 5, 5, 1, 1),
5855 "AFSR1_EL1", "AFSR1_EL2", "AFSR1_EL12" },
5856 { K(3, 0, 5, 2, 0), K(3, 4, 5, 2, 0), K(3, 5, 5, 2, 0),
5857 "ESR_EL1", "ESR_EL2", "ESR_EL12" },
5858 { K(3, 0, 6, 0, 0), K(3, 4, 6, 0, 0), K(3, 5, 6, 0, 0),
5859 "FAR_EL1", "FAR_EL2", "FAR_EL12" },
5860 { K(3, 0, 10, 2, 0), K(3, 4, 10, 2, 0), K(3, 5, 10, 2, 0),
5861 "MAIR_EL1", "MAIR_EL2", "MAIR_EL12" },
5862 { K(3, 0, 10, 3, 0), K(3, 4, 10, 3, 0), K(3, 5, 10, 3, 0),
5863 "AMAIR0", "AMAIR_EL2", "AMAIR_EL12" },
5864 { K(3, 0, 12, 0, 0), K(3, 4, 12, 0, 0), K(3, 5, 12, 0, 0),
5865 "VBAR", "VBAR_EL2", "VBAR_EL12" },
5866 { K(3, 0, 13, 0, 1), K(3, 4, 13, 0, 1), K(3, 5, 13, 0, 1),
5867 "CONTEXTIDR_EL1", "CONTEXTIDR_EL2", "CONTEXTIDR_EL12" },
5868 { K(3, 0, 14, 1, 0), K(3, 4, 14, 1, 0), K(3, 5, 14, 1, 0),
5869 "CNTKCTL", "CNTHCTL_EL2", "CNTKCTL_EL12" },
5870
5871 /*
5872 * Note that redirection of ZCR is mentioned in the description
5873 * of ZCR_EL2, and aliasing in the description of ZCR_EL1, but
5874 * not in the summary table.
5875 */
5876 { K(3, 0, 1, 2, 0), K(3, 4, 1, 2, 0), K(3, 5, 1, 2, 0),
5877 "ZCR_EL1", "ZCR_EL2", "ZCR_EL12", isar_feature_aa64_sve },
5878
5879 /* TODO: ARMv8.2-SPE -- PMSCR_EL2 */
5880 /* TODO: ARMv8.4-Trace -- TRFCR_EL2 */
5881 };
5882#undef K
5883
5884 size_t i;
5885
5886 for (i = 0; i < ARRAY_SIZE(aliases); i++) {
5887 const struct E2HAlias *a = &aliases[i];
5888 ARMCPRegInfo *src_reg, *dst_reg;
5889
5890 if (a->feature && !a->feature(&cpu->isar)) {
5891 continue;
5892 }
5893
5894 src_reg = g_hash_table_lookup(cpu->cp_regs, &a->src_key);
5895 dst_reg = g_hash_table_lookup(cpu->cp_regs, &a->dst_key);
5896 g_assert(src_reg != NULL);
5897 g_assert(dst_reg != NULL);
5898
5899 /* Cross-compare names to detect typos in the keys. */
5900 g_assert(strcmp(src_reg->name, a->src_name) == 0);
5901 g_assert(strcmp(dst_reg->name, a->dst_name) == 0);
5902
5903 /* None of the core system registers use opaque; we will. */
5904 g_assert(src_reg->opaque == NULL);
5905
5906 /* Create alias before redirection so we dup the right data. */
5907 if (a->new_key) {
5908 ARMCPRegInfo *new_reg = g_memdup(src_reg, sizeof(ARMCPRegInfo));
5909 uint32_t *new_key = g_memdup(&a->new_key, sizeof(uint32_t));
5910 bool ok;
5911
5912 new_reg->name = a->new_name;
5913 new_reg->type |= ARM_CP_ALIAS;
5914 /* Remove PL1/PL0 access, leaving PL2/PL3 R/W in place. */
5915 new_reg->access &= PL2_RW | PL3_RW;
5916
5917 ok = g_hash_table_insert(cpu->cp_regs, new_key, new_reg);
5918 g_assert(ok);
5919 }
5920
5921 src_reg->opaque = dst_reg;
5922 src_reg->orig_readfn = src_reg->readfn ?: raw_read;
5923 src_reg->orig_writefn = src_reg->writefn ?: raw_write;
5924 if (!src_reg->raw_readfn) {
5925 src_reg->raw_readfn = raw_read;
5926 }
5927 if (!src_reg->raw_writefn) {
5928 src_reg->raw_writefn = raw_write;
5929 }
5930 src_reg->readfn = el2_e2h_read;
5931 src_reg->writefn = el2_e2h_write;
5932 }
5933}
5934#endif
5935
3f208fd7
PM
5936static CPAccessResult ctr_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
5937 bool isread)
7da845b0 5938{
97475a89
RH
5939 int cur_el = arm_current_el(env);
5940
5941 if (cur_el < 2) {
5942 uint64_t hcr = arm_hcr_el2_eff(env);
5943
5944 if (cur_el == 0) {
5945 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
5946 if (!(env->cp15.sctlr_el[2] & SCTLR_UCT)) {
5947 return CP_ACCESS_TRAP_EL2;
5948 }
5949 } else {
5950 if (!(env->cp15.sctlr_el[1] & SCTLR_UCT)) {
5951 return CP_ACCESS_TRAP;
5952 }
5953 if (hcr & HCR_TID2) {
5954 return CP_ACCESS_TRAP_EL2;
5955 }
5956 }
5957 } else if (hcr & HCR_TID2) {
5958 return CP_ACCESS_TRAP_EL2;
5959 }
7da845b0 5960 }
630fcd4d
MZ
5961
5962 if (arm_current_el(env) < 2 && arm_hcr_el2_eff(env) & HCR_TID2) {
5963 return CP_ACCESS_TRAP_EL2;
5964 }
5965
7da845b0
PM
5966 return CP_ACCESS_OK;
5967}
5968
1424ca8d
DM
5969static void oslar_write(CPUARMState *env, const ARMCPRegInfo *ri,
5970 uint64_t value)
5971{
5972 /* Writes to OSLAR_EL1 may update the OS lock status, which can be
5973 * read via a bit in OSLSR_EL1.
5974 */
5975 int oslock;
5976
5977 if (ri->state == ARM_CP_STATE_AA32) {
5978 oslock = (value == 0xC5ACCE55);
5979 } else {
5980 oslock = value & 1;
5981 }
5982
5983 env->cp15.oslsr_el1 = deposit32(env->cp15.oslsr_el1, 1, 1, oslock);
5984}
5985
50300698 5986static const ARMCPRegInfo debug_cp_reginfo[] = {
50300698 5987 /* DBGDRAR, DBGDSAR: always RAZ since we don't implement memory mapped
10aae104
PM
5988 * debug components. The AArch64 version of DBGDRAR is named MDRAR_EL1;
5989 * unlike DBGDRAR it is never accessible from EL0.
5990 * DBGDSAR is deprecated and must RAZ from v8 anyway, so it has no AArch64
5991 * accessor.
50300698
PM
5992 */
5993 { .name = "DBGDRAR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 0,
91b0a238
PM
5994 .access = PL0_R, .accessfn = access_tdra,
5995 .type = ARM_CP_CONST, .resetvalue = 0 },
10aae104
PM
5996 { .name = "MDRAR_EL1", .state = ARM_CP_STATE_AA64,
5997 .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
91b0a238
PM
5998 .access = PL1_R, .accessfn = access_tdra,
5999 .type = ARM_CP_CONST, .resetvalue = 0 },
50300698 6000 { .name = "DBGDSAR", .cp = 14, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
91b0a238
PM
6001 .access = PL0_R, .accessfn = access_tdra,
6002 .type = ARM_CP_CONST, .resetvalue = 0 },
17a9eb53 6003 /* Monitor debug system control register; the 32-bit alias is DBGDSCRext. */
10aae104
PM
6004 { .name = "MDSCR_EL1", .state = ARM_CP_STATE_BOTH,
6005 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
d6c8cf81 6006 .access = PL1_RW, .accessfn = access_tda,
0e5e8935
PM
6007 .fieldoffset = offsetof(CPUARMState, cp15.mdscr_el1),
6008 .resetvalue = 0 },
5e8b12ff
PM
6009 /* MDCCSR_EL0, aka DBGDSCRint. This is a read-only mirror of MDSCR_EL1.
6010 * We don't implement the configurable EL0 access.
6011 */
6012 { .name = "MDCCSR_EL0", .state = ARM_CP_STATE_BOTH,
6013 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
7a0e58fa 6014 .type = ARM_CP_ALIAS,
d6c8cf81 6015 .access = PL1_R, .accessfn = access_tda,
b061a82b 6016 .fieldoffset = offsetof(CPUARMState, cp15.mdscr_el1), },
10aae104
PM
6017 { .name = "OSLAR_EL1", .state = ARM_CP_STATE_BOTH,
6018 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 4,
1424ca8d 6019 .access = PL1_W, .type = ARM_CP_NO_RAW,
187f678d 6020 .accessfn = access_tdosa,
1424ca8d
DM
6021 .writefn = oslar_write },
6022 { .name = "OSLSR_EL1", .state = ARM_CP_STATE_BOTH,
6023 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 4,
6024 .access = PL1_R, .resetvalue = 10,
187f678d 6025 .accessfn = access_tdosa,
1424ca8d 6026 .fieldoffset = offsetof(CPUARMState, cp15.oslsr_el1) },
5e8b12ff
PM
6027 /* Dummy OSDLR_EL1: 32-bit Linux will read this */
6028 { .name = "OSDLR_EL1", .state = ARM_CP_STATE_BOTH,
6029 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 4,
187f678d
PM
6030 .access = PL1_RW, .accessfn = access_tdosa,
6031 .type = ARM_CP_NOP },
5e8b12ff
PM
6032 /* Dummy DBGVCR: Linux wants to clear this on startup, but we don't
6033 * implement vector catch debug events yet.
6034 */
6035 { .name = "DBGVCR",
6036 .cp = 14, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
d6c8cf81
PM
6037 .access = PL1_RW, .accessfn = access_tda,
6038 .type = ARM_CP_NOP },
4d2ec4da
PM
6039 /* Dummy DBGVCR32_EL2 (which is only for a 64-bit hypervisor
6040 * to save and restore a 32-bit guest's DBGVCR)
6041 */
6042 { .name = "DBGVCR32_EL2", .state = ARM_CP_STATE_AA64,
6043 .opc0 = 2, .opc1 = 4, .crn = 0, .crm = 7, .opc2 = 0,
6044 .access = PL2_RW, .accessfn = access_tda,
6045 .type = ARM_CP_NOP },
5dbdc434
PM
6046 /* Dummy MDCCINT_EL1, since we don't implement the Debug Communications
6047 * Channel but Linux may try to access this register. The 32-bit
6048 * alias is DBGDCCINT.
6049 */
6050 { .name = "MDCCINT_EL1", .state = ARM_CP_STATE_BOTH,
6051 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0,
6052 .access = PL1_RW, .accessfn = access_tda,
6053 .type = ARM_CP_NOP },
50300698
PM
6054 REGINFO_SENTINEL
6055};
6056
6057static const ARMCPRegInfo debug_lpae_cp_reginfo[] = {
6058 /* 64 bit access versions of the (dummy) debug registers */
6059 { .name = "DBGDRAR", .cp = 14, .crm = 1, .opc1 = 0,
6060 .access = PL0_R, .type = ARM_CP_CONST|ARM_CP_64BIT, .resetvalue = 0 },
6061 { .name = "DBGDSAR", .cp = 14, .crm = 2, .opc1 = 0,
6062 .access = PL0_R, .type = ARM_CP_CONST|ARM_CP_64BIT, .resetvalue = 0 },
6063 REGINFO_SENTINEL
6064};
6065
60eed086
RH
6066/* Return the exception level to which exceptions should be taken
6067 * via SVEAccessTrap. If an exception should be routed through
6068 * AArch64.AdvSIMDFPAccessTrap, return 0; fp_exception_el should
6069 * take care of raising that exception.
6070 * C.f. the ARM pseudocode function CheckSVEEnabled.
5be5e8ed 6071 */
ced31551 6072int sve_exception_el(CPUARMState *env, int el)
5be5e8ed
RH
6073{
6074#ifndef CONFIG_USER_ONLY
c2ddb7cf
RH
6075 uint64_t hcr_el2 = arm_hcr_el2_eff(env);
6076
6077 if (el <= 1 && (hcr_el2 & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
60eed086
RH
6078 bool disabled = false;
6079
6080 /* The CPACR.ZEN controls traps to EL1:
6081 * 0, 2 : trap EL0 and EL1 accesses
6082 * 1 : trap only EL0 accesses
6083 * 3 : trap no accesses
6084 */
6085 if (!extract32(env->cp15.cpacr_el1, 16, 1)) {
6086 disabled = true;
6087 } else if (!extract32(env->cp15.cpacr_el1, 17, 1)) {
2de7ace2 6088 disabled = el == 0;
5be5e8ed 6089 }
60eed086
RH
6090 if (disabled) {
6091 /* route_to_el2 */
c2ddb7cf 6092 return hcr_el2 & HCR_TGE ? 2 : 1;
5be5e8ed 6093 }
5be5e8ed 6094
60eed086
RH
6095 /* Check CPACR.FPEN. */
6096 if (!extract32(env->cp15.cpacr_el1, 20, 1)) {
6097 disabled = true;
6098 } else if (!extract32(env->cp15.cpacr_el1, 21, 1)) {
2de7ace2 6099 disabled = el == 0;
5be5e8ed 6100 }
60eed086
RH
6101 if (disabled) {
6102 return 0;
5be5e8ed 6103 }
5be5e8ed
RH
6104 }
6105
60eed086
RH
6106 /* CPTR_EL2. Since TZ and TFP are positive,
6107 * they will be zero when EL2 is not present.
6108 */
2de7ace2 6109 if (el <= 2 && !arm_is_secure_below_el3(env)) {
60eed086
RH
6110 if (env->cp15.cptr_el[2] & CPTR_TZ) {
6111 return 2;
6112 }
6113 if (env->cp15.cptr_el[2] & CPTR_TFP) {
6114 return 0;
6115 }
5be5e8ed
RH
6116 }
6117
60eed086
RH
6118 /* CPTR_EL3. Since EZ is negative we must check for EL3. */
6119 if (arm_feature(env, ARM_FEATURE_EL3)
6120 && !(env->cp15.cptr_el[3] & CPTR_EZ)) {
5be5e8ed
RH
6121 return 3;
6122 }
6123#endif
6124 return 0;
6125}
6126
0df9142d
AJ
6127static uint32_t sve_zcr_get_valid_len(ARMCPU *cpu, uint32_t start_len)
6128{
6e553f2a 6129 uint32_t end_len;
0df9142d 6130
6e553f2a
RH
6131 end_len = start_len &= 0xf;
6132 if (!test_bit(start_len, cpu->sve_vq_map)) {
6133 end_len = find_last_bit(cpu->sve_vq_map, start_len);
6134 assert(end_len < start_len);
6135 }
6136 return end_len;
0df9142d
AJ
6137}
6138
0ab5953b
RH
6139/*
6140 * Given that SVE is enabled, return the vector length for EL.
6141 */
ced31551 6142uint32_t sve_zcr_len_for_el(CPUARMState *env, int el)
0ab5953b 6143{
2fc0cc0e 6144 ARMCPU *cpu = env_archcpu(env);
0ab5953b
RH
6145 uint32_t zcr_len = cpu->sve_max_vq - 1;
6146
6147 if (el <= 1) {
6148 zcr_len = MIN(zcr_len, 0xf & (uint32_t)env->vfp.zcr_el[1]);
6149 }
6a02a732 6150 if (el <= 2 && arm_feature(env, ARM_FEATURE_EL2)) {
0ab5953b
RH
6151 zcr_len = MIN(zcr_len, 0xf & (uint32_t)env->vfp.zcr_el[2]);
6152 }
6a02a732 6153 if (arm_feature(env, ARM_FEATURE_EL3)) {
0ab5953b
RH
6154 zcr_len = MIN(zcr_len, 0xf & (uint32_t)env->vfp.zcr_el[3]);
6155 }
0df9142d
AJ
6156
6157 return sve_zcr_get_valid_len(cpu, zcr_len);
0ab5953b
RH
6158}
6159
5be5e8ed
RH
6160static void zcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6161 uint64_t value)
6162{
0ab5953b
RH
6163 int cur_el = arm_current_el(env);
6164 int old_len = sve_zcr_len_for_el(env, cur_el);
6165 int new_len;
6166
5be5e8ed 6167 /* Bits other than [3:0] are RAZ/WI. */
7b351d98 6168 QEMU_BUILD_BUG_ON(ARM_MAX_VQ > 16);
5be5e8ed 6169 raw_write(env, ri, value & 0xf);
0ab5953b
RH
6170
6171 /*
6172 * Because we arrived here, we know both FP and SVE are enabled;
6173 * otherwise we would have trapped access to the ZCR_ELn register.
6174 */
6175 new_len = sve_zcr_len_for_el(env, cur_el);
6176 if (new_len < old_len) {
6177 aarch64_sve_narrow_vq(env, new_len + 1);
6178 }
5be5e8ed
RH
6179}
6180
6181static const ARMCPRegInfo zcr_el1_reginfo = {
6182 .name = "ZCR_EL1", .state = ARM_CP_STATE_AA64,
6183 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 0,
11d7870b 6184 .access = PL1_RW, .type = ARM_CP_SVE,
5be5e8ed
RH
6185 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[1]),
6186 .writefn = zcr_write, .raw_writefn = raw_write
6187};
6188
6189static const ARMCPRegInfo zcr_el2_reginfo = {
6190 .name = "ZCR_EL2", .state = ARM_CP_STATE_AA64,
6191 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 0,
11d7870b 6192 .access = PL2_RW, .type = ARM_CP_SVE,
5be5e8ed
RH
6193 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[2]),
6194 .writefn = zcr_write, .raw_writefn = raw_write
6195};
6196
6197static const ARMCPRegInfo zcr_no_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 .readfn = arm_cp_read_zero, .writefn = arm_cp_write_ignore
6202};
6203
6204static const ARMCPRegInfo zcr_el3_reginfo = {
6205 .name = "ZCR_EL3", .state = ARM_CP_STATE_AA64,
6206 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 2, .opc2 = 0,
11d7870b 6207 .access = PL3_RW, .type = ARM_CP_SVE,
5be5e8ed
RH
6208 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[3]),
6209 .writefn = zcr_write, .raw_writefn = raw_write
6210};
6211
9ee98ce8
PM
6212void hw_watchpoint_update(ARMCPU *cpu, int n)
6213{
6214 CPUARMState *env = &cpu->env;
6215 vaddr len = 0;
6216 vaddr wvr = env->cp15.dbgwvr[n];
6217 uint64_t wcr = env->cp15.dbgwcr[n];
6218 int mask;
6219 int flags = BP_CPU | BP_STOP_BEFORE_ACCESS;
6220
6221 if (env->cpu_watchpoint[n]) {
6222 cpu_watchpoint_remove_by_ref(CPU(cpu), env->cpu_watchpoint[n]);
6223 env->cpu_watchpoint[n] = NULL;
6224 }
6225
6226 if (!extract64(wcr, 0, 1)) {
6227 /* E bit clear : watchpoint disabled */
6228 return;
6229 }
6230
6231 switch (extract64(wcr, 3, 2)) {
6232 case 0:
6233 /* LSC 00 is reserved and must behave as if the wp is disabled */
6234 return;
6235 case 1:
6236 flags |= BP_MEM_READ;
6237 break;
6238 case 2:
6239 flags |= BP_MEM_WRITE;
6240 break;
6241 case 3:
6242 flags |= BP_MEM_ACCESS;
6243 break;
6244 }
6245
6246 /* Attempts to use both MASK and BAS fields simultaneously are
6247 * CONSTRAINED UNPREDICTABLE; we opt to ignore BAS in this case,
6248 * thus generating a watchpoint for every byte in the masked region.
6249 */
6250 mask = extract64(wcr, 24, 4);
6251 if (mask == 1 || mask == 2) {
6252 /* Reserved values of MASK; we must act as if the mask value was
6253 * some non-reserved value, or as if the watchpoint were disabled.
6254 * We choose the latter.
6255 */
6256 return;
6257 } else if (mask) {
6258 /* Watchpoint covers an aligned area up to 2GB in size */
6259 len = 1ULL << mask;
6260 /* If masked bits in WVR are not zero it's CONSTRAINED UNPREDICTABLE
6261 * whether the watchpoint fires when the unmasked bits match; we opt
6262 * to generate the exceptions.
6263 */
6264 wvr &= ~(len - 1);
6265 } else {
6266 /* Watchpoint covers bytes defined by the byte address select bits */
6267 int bas = extract64(wcr, 5, 8);
6268 int basstart;
6269
9ee98ce8
PM
6270 if (extract64(wvr, 2, 1)) {
6271 /* Deprecated case of an only 4-aligned address. BAS[7:4] are
6272 * ignored, and BAS[3:0] define which bytes to watch.
6273 */
6274 bas &= 0xf;
6275 }
ae1111d4
RH
6276
6277 if (bas == 0) {
6278 /* This must act as if the watchpoint is disabled */
6279 return;
6280 }
6281
9ee98ce8
PM
6282 /* The BAS bits are supposed to be programmed to indicate a contiguous
6283 * range of bytes. Otherwise it is CONSTRAINED UNPREDICTABLE whether
6284 * we fire for each byte in the word/doubleword addressed by the WVR.
6285 * We choose to ignore any non-zero bits after the first range of 1s.
6286 */
6287 basstart = ctz32(bas);
6288 len = cto32(bas >> basstart);
6289 wvr += basstart;
6290 }
6291
6292 cpu_watchpoint_insert(CPU(cpu), wvr, len, flags,
6293 &env->cpu_watchpoint[n]);
6294}
6295
6296void hw_watchpoint_update_all(ARMCPU *cpu)
6297{
6298 int i;
6299 CPUARMState *env = &cpu->env;
6300
6301 /* Completely clear out existing QEMU watchpoints and our array, to
6302 * avoid possible stale entries following migration load.
6303 */
6304 cpu_watchpoint_remove_all(CPU(cpu), BP_CPU);
6305 memset(env->cpu_watchpoint, 0, sizeof(env->cpu_watchpoint));
6306
6307 for (i = 0; i < ARRAY_SIZE(cpu->env.cpu_watchpoint); i++) {
6308 hw_watchpoint_update(cpu, i);
6309 }
6310}
6311
6312static void dbgwvr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6313 uint64_t value)
6314{
2fc0cc0e 6315 ARMCPU *cpu = env_archcpu(env);
9ee98ce8
PM
6316 int i = ri->crm;
6317
6318 /* Bits [63:49] are hardwired to the value of bit [48]; that is, the
6319 * register reads and behaves as if values written are sign extended.
6320 * Bits [1:0] are RES0.
6321 */
6322 value = sextract64(value, 0, 49) & ~3ULL;
6323
6324 raw_write(env, ri, value);
6325 hw_watchpoint_update(cpu, i);
6326}
6327
6328static void dbgwcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6329 uint64_t value)
6330{
2fc0cc0e 6331 ARMCPU *cpu = env_archcpu(env);
9ee98ce8
PM
6332 int i = ri->crm;
6333
6334 raw_write(env, ri, value);
6335 hw_watchpoint_update(cpu, i);
6336}
6337
46747d15
PM
6338void hw_breakpoint_update(ARMCPU *cpu, int n)
6339{
6340 CPUARMState *env = &cpu->env;
6341 uint64_t bvr = env->cp15.dbgbvr[n];
6342 uint64_t bcr = env->cp15.dbgbcr[n];
6343 vaddr addr;
6344 int bt;
6345 int flags = BP_CPU;
6346
6347 if (env->cpu_breakpoint[n]) {
6348 cpu_breakpoint_remove_by_ref(CPU(cpu), env->cpu_breakpoint[n]);
6349 env->cpu_breakpoint[n] = NULL;
6350 }
6351
6352 if (!extract64(bcr, 0, 1)) {
6353 /* E bit clear : watchpoint disabled */
6354 return;
6355 }
6356
6357 bt = extract64(bcr, 20, 4);
6358
6359 switch (bt) {
6360 case 4: /* unlinked address mismatch (reserved if AArch64) */
6361 case 5: /* linked address mismatch (reserved if AArch64) */
6362 qemu_log_mask(LOG_UNIMP,
0221c8fd 6363 "arm: address mismatch breakpoint types not implemented\n");
46747d15
PM
6364 return;
6365 case 0: /* unlinked address match */
6366 case 1: /* linked address match */
6367 {
6368 /* Bits [63:49] are hardwired to the value of bit [48]; that is,
6369 * we behave as if the register was sign extended. Bits [1:0] are
6370 * RES0. The BAS field is used to allow setting breakpoints on 16
6371 * bit wide instructions; it is CONSTRAINED UNPREDICTABLE whether
6372 * a bp will fire if the addresses covered by the bp and the addresses
6373 * covered by the insn overlap but the insn doesn't start at the
6374 * start of the bp address range. We choose to require the insn and
6375 * the bp to have the same address. The constraints on writing to
6376 * BAS enforced in dbgbcr_write mean we have only four cases:
6377 * 0b0000 => no breakpoint
6378 * 0b0011 => breakpoint on addr
6379 * 0b1100 => breakpoint on addr + 2
6380 * 0b1111 => breakpoint on addr
6381 * See also figure D2-3 in the v8 ARM ARM (DDI0487A.c).
6382 */
6383 int bas = extract64(bcr, 5, 4);
6384 addr = sextract64(bvr, 0, 49) & ~3ULL;
6385 if (bas == 0) {
6386 return;
6387 }
6388 if (bas == 0xc) {
6389 addr += 2;
6390 }
6391 break;
6392 }
6393 case 2: /* unlinked context ID match */
6394 case 8: /* unlinked VMID match (reserved if no EL2) */
6395 case 10: /* unlinked context ID and VMID match (reserved if no EL2) */
6396 qemu_log_mask(LOG_UNIMP,
0221c8fd 6397 "arm: unlinked context breakpoint types not implemented\n");
46747d15
PM
6398 return;
6399 case 9: /* linked VMID match (reserved if no EL2) */
6400 case 11: /* linked context ID and VMID match (reserved if no EL2) */
6401 case 3: /* linked context ID match */
6402 default:
6403 /* We must generate no events for Linked context matches (unless
6404 * they are linked to by some other bp/wp, which is handled in
6405 * updates for the linking bp/wp). We choose to also generate no events
6406 * for reserved values.
6407 */
6408 return;
6409 }
6410
6411 cpu_breakpoint_insert(CPU(cpu), addr, flags, &env->cpu_breakpoint[n]);
6412}
6413
6414void hw_breakpoint_update_all(ARMCPU *cpu)
6415{
6416 int i;
6417 CPUARMState *env = &cpu->env;
6418
6419 /* Completely clear out existing QEMU breakpoints and our array, to
6420 * avoid possible stale entries following migration load.
6421 */
6422 cpu_breakpoint_remove_all(CPU(cpu), BP_CPU);
6423 memset(env->cpu_breakpoint, 0, sizeof(env->cpu_breakpoint));
6424
6425 for (i = 0; i < ARRAY_SIZE(cpu->env.cpu_breakpoint); i++) {
6426 hw_breakpoint_update(cpu, i);
6427 }
6428}
6429
6430static void dbgbvr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6431 uint64_t value)
6432{
2fc0cc0e 6433 ARMCPU *cpu = env_archcpu(env);
46747d15
PM
6434 int i = ri->crm;
6435
6436 raw_write(env, ri, value);
6437 hw_breakpoint_update(cpu, i);
6438}
6439
6440static void dbgbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6441 uint64_t value)
6442{
2fc0cc0e 6443 ARMCPU *cpu = env_archcpu(env);
46747d15
PM
6444 int i = ri->crm;
6445
6446 /* BAS[3] is a read-only copy of BAS[2], and BAS[1] a read-only
6447 * copy of BAS[0].
6448 */
6449 value = deposit64(value, 6, 1, extract64(value, 5, 1));
6450 value = deposit64(value, 8, 1, extract64(value, 7, 1));
6451
6452 raw_write(env, ri, value);
6453 hw_breakpoint_update(cpu, i);
6454}
6455
50300698 6456static void define_debug_regs(ARMCPU *cpu)
0b45451e 6457{
50300698
PM
6458 /* Define v7 and v8 architectural debug registers.
6459 * These are just dummy implementations for now.
0b45451e
PM
6460 */
6461 int i;
3ff6fc91 6462 int wrps, brps, ctx_cmps;
48eb3ae6
PM
6463 ARMCPRegInfo dbgdidr = {
6464 .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0,
d6c8cf81 6465 .access = PL0_R, .accessfn = access_tda,
4426d361 6466 .type = ARM_CP_CONST, .resetvalue = cpu->isar.dbgdidr,
48eb3ae6
PM
6467 };
6468
3ff6fc91 6469 /* Note that all these register fields hold "number of Xs minus 1". */
88ce6c6e
PM
6470 brps = arm_num_brps(cpu);
6471 wrps = arm_num_wrps(cpu);
6472 ctx_cmps = arm_num_ctx_cmps(cpu);
3ff6fc91
PM
6473
6474 assert(ctx_cmps <= brps);
48eb3ae6 6475
48eb3ae6 6476 define_one_arm_cp_reg(cpu, &dbgdidr);
50300698
PM
6477 define_arm_cp_regs(cpu, debug_cp_reginfo);
6478
6479 if (arm_feature(&cpu->env, ARM_FEATURE_LPAE)) {
6480 define_arm_cp_regs(cpu, debug_lpae_cp_reginfo);
6481 }
6482
88ce6c6e 6483 for (i = 0; i < brps; i++) {
0b45451e 6484 ARMCPRegInfo dbgregs[] = {
10aae104
PM
6485 { .name = "DBGBVR", .state = ARM_CP_STATE_BOTH,
6486 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 4,
d6c8cf81 6487 .access = PL1_RW, .accessfn = access_tda,
46747d15
PM
6488 .fieldoffset = offsetof(CPUARMState, cp15.dbgbvr[i]),
6489 .writefn = dbgbvr_write, .raw_writefn = raw_write
6490 },
10aae104
PM
6491 { .name = "DBGBCR", .state = ARM_CP_STATE_BOTH,
6492 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 5,
d6c8cf81 6493 .access = PL1_RW, .accessfn = access_tda,
46747d15
PM
6494 .fieldoffset = offsetof(CPUARMState, cp15.dbgbcr[i]),
6495 .writefn = dbgbcr_write, .raw_writefn = raw_write
6496 },
48eb3ae6
PM
6497 REGINFO_SENTINEL
6498 };
6499 define_arm_cp_regs(cpu, dbgregs);
6500 }
6501
88ce6c6e 6502 for (i = 0; i < wrps; i++) {
48eb3ae6 6503 ARMCPRegInfo dbgregs[] = {
10aae104
PM
6504 { .name = "DBGWVR", .state = ARM_CP_STATE_BOTH,
6505 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 6,
d6c8cf81 6506 .access = PL1_RW, .accessfn = access_tda,
9ee98ce8
PM
6507 .fieldoffset = offsetof(CPUARMState, cp15.dbgwvr[i]),
6508 .writefn = dbgwvr_write, .raw_writefn = raw_write
6509 },
10aae104
PM
6510 { .name = "DBGWCR", .state = ARM_CP_STATE_BOTH,
6511 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 7,
d6c8cf81 6512 .access = PL1_RW, .accessfn = access_tda,
9ee98ce8
PM
6513 .fieldoffset = offsetof(CPUARMState, cp15.dbgwcr[i]),
6514 .writefn = dbgwcr_write, .raw_writefn = raw_write
6515 },
6516 REGINFO_SENTINEL
0b45451e
PM
6517 };
6518 define_arm_cp_regs(cpu, dbgregs);
6519 }
6520}
6521
24183fb6
PM
6522static void define_pmu_regs(ARMCPU *cpu)
6523{
6524 /*
6525 * v7 performance monitor control register: same implementor
6526 * field as main ID register, and we implement four counters in
6527 * addition to the cycle count register.
6528 */
6529 unsigned int i, pmcrn = 4;
6530 ARMCPRegInfo pmcr = {
6531 .name = "PMCR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 0,
6532 .access = PL0_RW,
6533 .type = ARM_CP_IO | ARM_CP_ALIAS,
6534 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcr),
6535 .accessfn = pmreg_access, .writefn = pmcr_write,
6536 .raw_writefn = raw_write,
6537 };
6538 ARMCPRegInfo pmcr64 = {
6539 .name = "PMCR_EL0", .state = ARM_CP_STATE_AA64,
6540 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 0,
6541 .access = PL0_RW, .accessfn = pmreg_access,
6542 .type = ARM_CP_IO,
6543 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcr),
62d96ff4
PM
6544 .resetvalue = (cpu->midr & 0xff000000) | (pmcrn << PMCRN_SHIFT) |
6545 PMCRLC,
24183fb6
PM
6546 .writefn = pmcr_write, .raw_writefn = raw_write,
6547 };
6548 define_one_arm_cp_reg(cpu, &pmcr);
6549 define_one_arm_cp_reg(cpu, &pmcr64);
6550 for (i = 0; i < pmcrn; i++) {
6551 char *pmevcntr_name = g_strdup_printf("PMEVCNTR%d", i);
6552 char *pmevcntr_el0_name = g_strdup_printf("PMEVCNTR%d_EL0", i);
6553 char *pmevtyper_name = g_strdup_printf("PMEVTYPER%d", i);
6554 char *pmevtyper_el0_name = g_strdup_printf("PMEVTYPER%d_EL0", i);
6555 ARMCPRegInfo pmev_regs[] = {
6556 { .name = pmevcntr_name, .cp = 15, .crn = 14,
6557 .crm = 8 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7,
6558 .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS,
6559 .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn,
6560 .accessfn = pmreg_access },
6561 { .name = pmevcntr_el0_name, .state = ARM_CP_STATE_AA64,
6562 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 8 | (3 & (i >> 3)),
6563 .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access,
6564 .type = ARM_CP_IO,
6565 .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn,
6566 .raw_readfn = pmevcntr_rawread,
6567 .raw_writefn = pmevcntr_rawwrite },
6568 { .name = pmevtyper_name, .cp = 15, .crn = 14,
6569 .crm = 12 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7,
6570 .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS,
6571 .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn,
6572 .accessfn = pmreg_access },
6573 { .name = pmevtyper_el0_name, .state = ARM_CP_STATE_AA64,
6574 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 12 | (3 & (i >> 3)),
6575 .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access,
6576 .type = ARM_CP_IO,
6577 .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn,
6578 .raw_writefn = pmevtyper_rawwrite },
6579 REGINFO_SENTINEL
6580 };
6581 define_arm_cp_regs(cpu, pmev_regs);
6582 g_free(pmevcntr_name);
6583 g_free(pmevcntr_el0_name);
6584 g_free(pmevtyper_name);
6585 g_free(pmevtyper_el0_name);
6586 }
a6179538 6587 if (cpu_isar_feature(aa32_pmu_8_1, cpu)) {
24183fb6
PM
6588 ARMCPRegInfo v81_pmu_regs[] = {
6589 { .name = "PMCEID2", .state = ARM_CP_STATE_AA32,
6590 .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 4,
6591 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6592 .resetvalue = extract64(cpu->pmceid0, 32, 32) },
6593 { .name = "PMCEID3", .state = ARM_CP_STATE_AA32,
6594 .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 5,
6595 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6596 .resetvalue = extract64(cpu->pmceid1, 32, 32) },
6597 REGINFO_SENTINEL
6598 };
6599 define_arm_cp_regs(cpu, v81_pmu_regs);
6600 }
15dd1ebd
PM
6601 if (cpu_isar_feature(any_pmu_8_4, cpu)) {
6602 static const ARMCPRegInfo v84_pmmir = {
6603 .name = "PMMIR_EL1", .state = ARM_CP_STATE_BOTH,
6604 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 6,
6605 .access = PL1_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6606 .resetvalue = 0
6607 };
6608 define_one_arm_cp_reg(cpu, &v84_pmmir);
6609 }
24183fb6
PM
6610}
6611
96a8b92e
PM
6612/* We don't know until after realize whether there's a GICv3
6613 * attached, and that is what registers the gicv3 sysregs.
6614 * So we have to fill in the GIC fields in ID_PFR/ID_PFR1_EL1/ID_AA64PFR0_EL1
6615 * at runtime.
6616 */
6617static uint64_t id_pfr1_read(CPUARMState *env, const ARMCPRegInfo *ri)
6618{
2fc0cc0e 6619 ARMCPU *cpu = env_archcpu(env);
96a8b92e
PM
6620 uint64_t pfr1 = cpu->id_pfr1;
6621
6622 if (env->gicv3state) {
6623 pfr1 |= 1 << 28;
6624 }
6625 return pfr1;
6626}
6627
976b99b6 6628#ifndef CONFIG_USER_ONLY
96a8b92e
PM
6629static uint64_t id_aa64pfr0_read(CPUARMState *env, const ARMCPRegInfo *ri)
6630{
2fc0cc0e 6631 ARMCPU *cpu = env_archcpu(env);
47576b94 6632 uint64_t pfr0 = cpu->isar.id_aa64pfr0;
96a8b92e
PM
6633
6634 if (env->gicv3state) {
6635 pfr0 |= 1 << 24;
6636 }
6637 return pfr0;
6638}
976b99b6 6639#endif
96a8b92e 6640
2d7137c1
RH
6641/* Shared logic between LORID and the rest of the LOR* registers.
6642 * Secure state has already been delt with.
6643 */
6644static CPAccessResult access_lor_ns(CPUARMState *env)
6645{
6646 int el = arm_current_el(env);
6647
6648 if (el < 2 && (arm_hcr_el2_eff(env) & HCR_TLOR)) {
6649 return CP_ACCESS_TRAP_EL2;
6650 }
6651 if (el < 3 && (env->cp15.scr_el3 & SCR_TLOR)) {
6652 return CP_ACCESS_TRAP_EL3;
6653 }
6654 return CP_ACCESS_OK;
6655}
6656
6657static CPAccessResult access_lorid(CPUARMState *env, const ARMCPRegInfo *ri,
6658 bool isread)
6659{
6660 if (arm_is_secure_below_el3(env)) {
6661 /* Access ok in secure mode. */
6662 return CP_ACCESS_OK;
6663 }
6664 return access_lor_ns(env);
6665}
6666
6667static CPAccessResult access_lor_other(CPUARMState *env,
6668 const ARMCPRegInfo *ri, bool isread)
6669{
6670 if (arm_is_secure_below_el3(env)) {
6671 /* Access denied in secure mode. */
6672 return CP_ACCESS_TRAP;
6673 }
6674 return access_lor_ns(env);
6675}
6676
d8564ee4
RH
6677/*
6678 * A trivial implementation of ARMv8.1-LOR leaves all of these
6679 * registers fixed at 0, which indicates that there are zero
6680 * supported Limited Ordering regions.
6681 */
6682static const ARMCPRegInfo lor_reginfo[] = {
6683 { .name = "LORSA_EL1", .state = ARM_CP_STATE_AA64,
6684 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 0,
6685 .access = PL1_RW, .accessfn = access_lor_other,
6686 .type = ARM_CP_CONST, .resetvalue = 0 },
6687 { .name = "LOREA_EL1", .state = ARM_CP_STATE_AA64,
6688 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 1,
6689 .access = PL1_RW, .accessfn = access_lor_other,
6690 .type = ARM_CP_CONST, .resetvalue = 0 },
6691 { .name = "LORN_EL1", .state = ARM_CP_STATE_AA64,
6692 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 2,
6693 .access = PL1_RW, .accessfn = access_lor_other,
6694 .type = ARM_CP_CONST, .resetvalue = 0 },
6695 { .name = "LORC_EL1", .state = ARM_CP_STATE_AA64,
6696 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 3,
6697 .access = PL1_RW, .accessfn = access_lor_other,
6698 .type = ARM_CP_CONST, .resetvalue = 0 },
6699 { .name = "LORID_EL1", .state = ARM_CP_STATE_AA64,
6700 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 7,
6701 .access = PL1_R, .accessfn = access_lorid,
6702 .type = ARM_CP_CONST, .resetvalue = 0 },
6703 REGINFO_SENTINEL
6704};
6705
967aa94f
RH
6706#ifdef TARGET_AARCH64
6707static CPAccessResult access_pauth(CPUARMState *env, const ARMCPRegInfo *ri,
6708 bool isread)
6709{
6710 int el = arm_current_el(env);
6711
6712 if (el < 2 &&
6713 arm_feature(env, ARM_FEATURE_EL2) &&
6714 !(arm_hcr_el2_eff(env) & HCR_APK)) {
6715 return CP_ACCESS_TRAP_EL2;
6716 }
6717 if (el < 3 &&
6718 arm_feature(env, ARM_FEATURE_EL3) &&
6719 !(env->cp15.scr_el3 & SCR_APK)) {
6720 return CP_ACCESS_TRAP_EL3;
6721 }
6722 return CP_ACCESS_OK;
6723}
6724
6725static const ARMCPRegInfo pauth_reginfo[] = {
6726 { .name = "APDAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6727 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 0,
6728 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 6729 .fieldoffset = offsetof(CPUARMState, keys.apda.lo) },
967aa94f
RH
6730 { .name = "APDAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6731 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 1,
6732 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 6733 .fieldoffset = offsetof(CPUARMState, keys.apda.hi) },
967aa94f
RH
6734 { .name = "APDBKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6735 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 2,
6736 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 6737 .fieldoffset = offsetof(CPUARMState, keys.apdb.lo) },
967aa94f
RH
6738 { .name = "APDBKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6739 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 3,
6740 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 6741 .fieldoffset = offsetof(CPUARMState, keys.apdb.hi) },
967aa94f
RH
6742 { .name = "APGAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6743 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 0,
6744 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 6745 .fieldoffset = offsetof(CPUARMState, keys.apga.lo) },
967aa94f
RH
6746 { .name = "APGAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6747 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 1,
6748 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 6749 .fieldoffset = offsetof(CPUARMState, keys.apga.hi) },
967aa94f
RH
6750 { .name = "APIAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6751 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 0,
6752 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 6753 .fieldoffset = offsetof(CPUARMState, keys.apia.lo) },
967aa94f
RH
6754 { .name = "APIAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6755 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 1,
6756 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 6757 .fieldoffset = offsetof(CPUARMState, keys.apia.hi) },
967aa94f
RH
6758 { .name = "APIBKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6759 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 2,
6760 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 6761 .fieldoffset = offsetof(CPUARMState, keys.apib.lo) },
967aa94f
RH
6762 { .name = "APIBKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6763 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 3,
6764 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 6765 .fieldoffset = offsetof(CPUARMState, keys.apib.hi) },
967aa94f
RH
6766 REGINFO_SENTINEL
6767};
de390645
RH
6768
6769static uint64_t rndr_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
6770{
6771 Error *err = NULL;
6772 uint64_t ret;
6773
6774 /* Success sets NZCV = 0000. */
6775 env->NF = env->CF = env->VF = 0, env->ZF = 1;
6776
6777 if (qemu_guest_getrandom(&ret, sizeof(ret), &err) < 0) {
6778 /*
6779 * ??? Failed, for unknown reasons in the crypto subsystem.
6780 * The best we can do is log the reason and return the
6781 * timed-out indication to the guest. There is no reason
6782 * we know to expect this failure to be transitory, so the
6783 * guest may well hang retrying the operation.
6784 */
6785 qemu_log_mask(LOG_UNIMP, "%s: Crypto failure: %s",
6786 ri->name, error_get_pretty(err));
6787 error_free(err);
6788
6789 env->ZF = 0; /* NZCF = 0100 */
6790 return 0;
6791 }
6792 return ret;
6793}
6794
6795/* We do not support re-seeding, so the two registers operate the same. */
6796static const ARMCPRegInfo rndr_reginfo[] = {
6797 { .name = "RNDR", .state = ARM_CP_STATE_AA64,
6798 .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END | ARM_CP_IO,
6799 .opc0 = 3, .opc1 = 3, .crn = 2, .crm = 4, .opc2 = 0,
6800 .access = PL0_R, .readfn = rndr_readfn },
6801 { .name = "RNDRRS", .state = ARM_CP_STATE_AA64,
6802 .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END | ARM_CP_IO,
6803 .opc0 = 3, .opc1 = 3, .crn = 2, .crm = 4, .opc2 = 1,
6804 .access = PL0_R, .readfn = rndr_readfn },
6805 REGINFO_SENTINEL
6806};
0d57b499
BM
6807
6808#ifndef CONFIG_USER_ONLY
6809static void dccvap_writefn(CPUARMState *env, const ARMCPRegInfo *opaque,
6810 uint64_t value)
6811{
6812 ARMCPU *cpu = env_archcpu(env);
6813 /* CTR_EL0 System register -> DminLine, bits [19:16] */
6814 uint64_t dline_size = 4 << ((cpu->ctr >> 16) & 0xF);
6815 uint64_t vaddr_in = (uint64_t) value;
6816 uint64_t vaddr = vaddr_in & ~(dline_size - 1);
6817 void *haddr;
6818 int mem_idx = cpu_mmu_index(env, false);
6819
6820 /* This won't be crossing page boundaries */
6821 haddr = probe_read(env, vaddr, dline_size, mem_idx, GETPC());
6822 if (haddr) {
6823
6824 ram_addr_t offset;
6825 MemoryRegion *mr;
6826
6827 /* RCU lock is already being held */
6828 mr = memory_region_from_host(haddr, &offset);
6829
6830 if (mr) {
6831 memory_region_do_writeback(mr, offset, dline_size);
6832 }
6833 }
6834}
6835
6836static const ARMCPRegInfo dcpop_reg[] = {
6837 { .name = "DC_CVAP", .state = ARM_CP_STATE_AA64,
6838 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 1,
6839 .access = PL0_W, .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END,
1bed4d2e 6840 .accessfn = aa64_cacheop_poc_access, .writefn = dccvap_writefn },
0d57b499
BM
6841 REGINFO_SENTINEL
6842};
6843
6844static const ARMCPRegInfo dcpodp_reg[] = {
6845 { .name = "DC_CVADP", .state = ARM_CP_STATE_AA64,
6846 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .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#endif /*CONFIG_USER_ONLY*/
6852
967aa94f
RH
6853#endif
6854
cb570bd3
RH
6855static CPAccessResult access_predinv(CPUARMState *env, const ARMCPRegInfo *ri,
6856 bool isread)
6857{
6858 int el = arm_current_el(env);
6859
6860 if (el == 0) {
6861 uint64_t sctlr = arm_sctlr(env, el);
6862 if (!(sctlr & SCTLR_EnRCTX)) {
6863 return CP_ACCESS_TRAP;
6864 }
6865 } else if (el == 1) {
6866 uint64_t hcr = arm_hcr_el2_eff(env);
6867 if (hcr & HCR_NV) {
6868 return CP_ACCESS_TRAP_EL2;
6869 }
6870 }
6871 return CP_ACCESS_OK;
6872}
6873
6874static const ARMCPRegInfo predinv_reginfo[] = {
6875 { .name = "CFP_RCTX", .state = ARM_CP_STATE_AA64,
6876 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 4,
6877 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
6878 { .name = "DVP_RCTX", .state = ARM_CP_STATE_AA64,
6879 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 5,
6880 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
6881 { .name = "CPP_RCTX", .state = ARM_CP_STATE_AA64,
6882 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 7,
6883 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
6884 /*
6885 * Note the AArch32 opcodes have a different OPC1.
6886 */
6887 { .name = "CFPRCTX", .state = ARM_CP_STATE_AA32,
6888 .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 4,
6889 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
6890 { .name = "DVPRCTX", .state = ARM_CP_STATE_AA32,
6891 .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 5,
6892 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
6893 { .name = "CPPRCTX", .state = ARM_CP_STATE_AA32,
6894 .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 7,
6895 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
6896 REGINFO_SENTINEL
6897};
6898
957e6155
PM
6899static uint64_t ccsidr2_read(CPUARMState *env, const ARMCPRegInfo *ri)
6900{
6901 /* Read the high 32 bits of the current CCSIDR */
6902 return extract64(ccsidr_read(env, ri), 32, 32);
6903}
6904
6905static const ARMCPRegInfo ccsidr2_reginfo[] = {
6906 { .name = "CCSIDR2", .state = ARM_CP_STATE_BOTH,
6907 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 2,
6908 .access = PL1_R,
6909 .accessfn = access_aa64_tid2,
6910 .readfn = ccsidr2_read, .type = ARM_CP_NO_RAW },
6911 REGINFO_SENTINEL
6912};
6913
6a4ef4e5
MZ
6914static CPAccessResult access_aa64_tid3(CPUARMState *env, const ARMCPRegInfo *ri,
6915 bool isread)
6916{
6917 if ((arm_current_el(env) < 2) && (arm_hcr_el2_eff(env) & HCR_TID3)) {
6918 return CP_ACCESS_TRAP_EL2;
6919 }
6920
6921 return CP_ACCESS_OK;
6922}
6923
6924static CPAccessResult access_aa32_tid3(CPUARMState *env, const ARMCPRegInfo *ri,
6925 bool isread)
6926{
6927 if (arm_feature(env, ARM_FEATURE_V8)) {
6928 return access_aa64_tid3(env, ri, isread);
6929 }
6930
6931 return CP_ACCESS_OK;
6932}
6933
f96f3d5f
MZ
6934static CPAccessResult access_jazelle(CPUARMState *env, const ARMCPRegInfo *ri,
6935 bool isread)
6936{
6937 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID0)) {
6938 return CP_ACCESS_TRAP_EL2;
6939 }
6940
6941 return CP_ACCESS_OK;
6942}
6943
6944static const ARMCPRegInfo jazelle_regs[] = {
6945 { .name = "JIDR",
6946 .cp = 14, .crn = 0, .crm = 0, .opc1 = 7, .opc2 = 0,
6947 .access = PL1_R, .accessfn = access_jazelle,
6948 .type = ARM_CP_CONST, .resetvalue = 0 },
6949 { .name = "JOSCR",
6950 .cp = 14, .crn = 1, .crm = 0, .opc1 = 7, .opc2 = 0,
6951 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
6952 { .name = "JMCR",
6953 .cp = 14, .crn = 2, .crm = 0, .opc1 = 7, .opc2 = 0,
6954 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
6955 REGINFO_SENTINEL
6956};
6957
e2a1a461
RH
6958static const ARMCPRegInfo vhe_reginfo[] = {
6959 { .name = "CONTEXTIDR_EL2", .state = ARM_CP_STATE_AA64,
6960 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 1,
6961 .access = PL2_RW,
6962 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[2]) },
ed30da8e
RH
6963 { .name = "TTBR1_EL2", .state = ARM_CP_STATE_AA64,
6964 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 1,
6965 .access = PL2_RW, .writefn = vmsa_tcr_ttbr_el2_write,
6966 .fieldoffset = offsetof(CPUARMState, cp15.ttbr1_el[2]) },
8c94b071
RH
6967#ifndef CONFIG_USER_ONLY
6968 { .name = "CNTHV_CVAL_EL2", .state = ARM_CP_STATE_AA64,
6969 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 2,
6970 .fieldoffset =
6971 offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYPVIRT].cval),
6972 .type = ARM_CP_IO, .access = PL2_RW,
6973 .writefn = gt_hv_cval_write, .raw_writefn = raw_write },
6974 { .name = "CNTHV_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
6975 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 0,
6976 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
6977 .resetfn = gt_hv_timer_reset,
6978 .readfn = gt_hv_tval_read, .writefn = gt_hv_tval_write },
6979 { .name = "CNTHV_CTL_EL2", .state = ARM_CP_STATE_BOTH,
6980 .type = ARM_CP_IO,
6981 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 1,
6982 .access = PL2_RW,
6983 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYPVIRT].ctl),
6984 .writefn = gt_hv_ctl_write, .raw_writefn = raw_write },
bb5972e4
RH
6985 { .name = "CNTP_CTL_EL02", .state = ARM_CP_STATE_AA64,
6986 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 1,
6987 .type = ARM_CP_IO | ARM_CP_ALIAS,
6988 .access = PL2_RW, .accessfn = e2h_access,
6989 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
6990 .writefn = gt_phys_ctl_write, .raw_writefn = raw_write },
6991 { .name = "CNTV_CTL_EL02", .state = ARM_CP_STATE_AA64,
6992 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 1,
6993 .type = ARM_CP_IO | ARM_CP_ALIAS,
6994 .access = PL2_RW, .accessfn = e2h_access,
6995 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
6996 .writefn = gt_virt_ctl_write, .raw_writefn = raw_write },
6997 { .name = "CNTP_TVAL_EL02", .state = ARM_CP_STATE_AA64,
6998 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 0,
6999 .type = ARM_CP_NO_RAW | ARM_CP_IO | ARM_CP_ALIAS,
7000 .access = PL2_RW, .accessfn = e2h_access,
7001 .readfn = gt_phys_tval_read, .writefn = gt_phys_tval_write },
7002 { .name = "CNTV_TVAL_EL02", .state = ARM_CP_STATE_AA64,
7003 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 0,
7004 .type = ARM_CP_NO_RAW | ARM_CP_IO | ARM_CP_ALIAS,
7005 .access = PL2_RW, .accessfn = e2h_access,
7006 .readfn = gt_virt_tval_read, .writefn = gt_virt_tval_write },
7007 { .name = "CNTP_CVAL_EL02", .state = ARM_CP_STATE_AA64,
7008 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 2,
7009 .type = ARM_CP_IO | ARM_CP_ALIAS,
7010 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
7011 .access = PL2_RW, .accessfn = e2h_access,
7012 .writefn = gt_phys_cval_write, .raw_writefn = raw_write },
7013 { .name = "CNTV_CVAL_EL02", .state = ARM_CP_STATE_AA64,
7014 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 2,
7015 .type = ARM_CP_IO | ARM_CP_ALIAS,
7016 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
7017 .access = PL2_RW, .accessfn = e2h_access,
7018 .writefn = gt_virt_cval_write, .raw_writefn = raw_write },
8c94b071 7019#endif
e2a1a461
RH
7020 REGINFO_SENTINEL
7021};
7022
04b07d29
RH
7023#ifndef CONFIG_USER_ONLY
7024static const ARMCPRegInfo ats1e1_reginfo[] = {
7025 { .name = "AT_S1E1R", .state = ARM_CP_STATE_AA64,
7026 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 0,
7027 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7028 .writefn = ats_write64 },
7029 { .name = "AT_S1E1W", .state = ARM_CP_STATE_AA64,
7030 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 1,
7031 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7032 .writefn = ats_write64 },
7033 REGINFO_SENTINEL
7034};
7035
7036static const ARMCPRegInfo ats1cp_reginfo[] = {
7037 { .name = "ATS1CPRP",
7038 .cp = 15, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 0,
7039 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7040 .writefn = ats_write },
7041 { .name = "ATS1CPWP",
7042 .cp = 15, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 1,
7043 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7044 .writefn = ats_write },
7045 REGINFO_SENTINEL
7046};
7047#endif
7048
f6287c24
PM
7049/*
7050 * ACTLR2 and HACTLR2 map to ACTLR_EL1[63:32] and
7051 * ACTLR_EL2[63:32]. They exist only if the ID_MMFR4.AC2 field
7052 * is non-zero, which is never for ARMv7, optionally in ARMv8
7053 * and mandatorily for ARMv8.2 and up.
7054 * ACTLR2 is banked for S and NS if EL3 is AArch32. Since QEMU's
7055 * implementation is RAZ/WI we can ignore this detail, as we
7056 * do for ACTLR.
7057 */
7058static const ARMCPRegInfo actlr2_hactlr2_reginfo[] = {
7059 { .name = "ACTLR2", .state = ARM_CP_STATE_AA32,
7060 .cp = 15, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 3,
99602377
RH
7061 .access = PL1_RW, .accessfn = access_tacr,
7062 .type = ARM_CP_CONST, .resetvalue = 0 },
f6287c24
PM
7063 { .name = "HACTLR2", .state = ARM_CP_STATE_AA32,
7064 .cp = 15, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 3,
7065 .access = PL2_RW, .type = ARM_CP_CONST,
7066 .resetvalue = 0 },
7067 REGINFO_SENTINEL
7068};
7069
2ceb98c0
PM
7070void register_cp_regs_for_features(ARMCPU *cpu)
7071{
7072 /* Register all the coprocessor registers based on feature bits */
7073 CPUARMState *env = &cpu->env;
7074 if (arm_feature(env, ARM_FEATURE_M)) {
7075 /* M profile has no coprocessor registers */
7076 return;
7077 }
7078
e9aa6c21 7079 define_arm_cp_regs(cpu, cp_reginfo);
9449fdf6
PM
7080 if (!arm_feature(env, ARM_FEATURE_V8)) {
7081 /* Must go early as it is full of wildcards that may be
7082 * overridden by later definitions.
7083 */
7084 define_arm_cp_regs(cpu, not_v8_cp_reginfo);
7085 }
7086
7d57f408 7087 if (arm_feature(env, ARM_FEATURE_V6)) {
8515a092
PM
7088 /* The ID registers all have impdef reset values */
7089 ARMCPRegInfo v6_idregs[] = {
0ff644a7
PM
7090 { .name = "ID_PFR0", .state = ARM_CP_STATE_BOTH,
7091 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
7092 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7093 .accessfn = access_aa32_tid3,
8515a092 7094 .resetvalue = cpu->id_pfr0 },
96a8b92e
PM
7095 /* ID_PFR1 is not a plain ARM_CP_CONST because we don't know
7096 * the value of the GIC field until after we define these regs.
7097 */
0ff644a7
PM
7098 { .name = "ID_PFR1", .state = ARM_CP_STATE_BOTH,
7099 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 1,
96a8b92e 7100 .access = PL1_R, .type = ARM_CP_NO_RAW,
6a4ef4e5 7101 .accessfn = access_aa32_tid3,
96a8b92e
PM
7102 .readfn = id_pfr1_read,
7103 .writefn = arm_cp_write_ignore },
0ff644a7
PM
7104 { .name = "ID_DFR0", .state = ARM_CP_STATE_BOTH,
7105 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 2,
7106 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7107 .accessfn = access_aa32_tid3,
a6179538 7108 .resetvalue = cpu->isar.id_dfr0 },
0ff644a7
PM
7109 { .name = "ID_AFR0", .state = ARM_CP_STATE_BOTH,
7110 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 3,
7111 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7112 .accessfn = access_aa32_tid3,
8515a092 7113 .resetvalue = cpu->id_afr0 },
0ff644a7
PM
7114 { .name = "ID_MMFR0", .state = ARM_CP_STATE_BOTH,
7115 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 4,
7116 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7117 .accessfn = access_aa32_tid3,
10054016 7118 .resetvalue = cpu->isar.id_mmfr0 },
0ff644a7
PM
7119 { .name = "ID_MMFR1", .state = ARM_CP_STATE_BOTH,
7120 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 5,
7121 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7122 .accessfn = access_aa32_tid3,
10054016 7123 .resetvalue = cpu->isar.id_mmfr1 },
0ff644a7
PM
7124 { .name = "ID_MMFR2", .state = ARM_CP_STATE_BOTH,
7125 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 6,
7126 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7127 .accessfn = access_aa32_tid3,
10054016 7128 .resetvalue = cpu->isar.id_mmfr2 },
0ff644a7
PM
7129 { .name = "ID_MMFR3", .state = ARM_CP_STATE_BOTH,
7130 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 7,
7131 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7132 .accessfn = access_aa32_tid3,
10054016 7133 .resetvalue = cpu->isar.id_mmfr3 },
0ff644a7
PM
7134 { .name = "ID_ISAR0", .state = ARM_CP_STATE_BOTH,
7135 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0,
7136 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7137 .accessfn = access_aa32_tid3,
47576b94 7138 .resetvalue = cpu->isar.id_isar0 },
0ff644a7
PM
7139 { .name = "ID_ISAR1", .state = ARM_CP_STATE_BOTH,
7140 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 1,
7141 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7142 .accessfn = access_aa32_tid3,
47576b94 7143 .resetvalue = cpu->isar.id_isar1 },
0ff644a7
PM
7144 { .name = "ID_ISAR2", .state = ARM_CP_STATE_BOTH,
7145 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
7146 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7147 .accessfn = access_aa32_tid3,
47576b94 7148 .resetvalue = cpu->isar.id_isar2 },
0ff644a7
PM
7149 { .name = "ID_ISAR3", .state = ARM_CP_STATE_BOTH,
7150 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 3,
7151 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7152 .accessfn = access_aa32_tid3,
47576b94 7153 .resetvalue = cpu->isar.id_isar3 },
0ff644a7
PM
7154 { .name = "ID_ISAR4", .state = ARM_CP_STATE_BOTH,
7155 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 4,
7156 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7157 .accessfn = access_aa32_tid3,
47576b94 7158 .resetvalue = cpu->isar.id_isar4 },
0ff644a7
PM
7159 { .name = "ID_ISAR5", .state = ARM_CP_STATE_BOTH,
7160 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 5,
7161 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7162 .accessfn = access_aa32_tid3,
47576b94 7163 .resetvalue = cpu->isar.id_isar5 },
e20d84c1
PM
7164 { .name = "ID_MMFR4", .state = ARM_CP_STATE_BOTH,
7165 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 6,
7166 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7167 .accessfn = access_aa32_tid3,
10054016 7168 .resetvalue = cpu->isar.id_mmfr4 },
802abf40 7169 { .name = "ID_ISAR6", .state = ARM_CP_STATE_BOTH,
e20d84c1
PM
7170 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 7,
7171 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7172 .accessfn = access_aa32_tid3,
47576b94 7173 .resetvalue = cpu->isar.id_isar6 },
8515a092
PM
7174 REGINFO_SENTINEL
7175 };
7176 define_arm_cp_regs(cpu, v6_idregs);
7d57f408
PM
7177 define_arm_cp_regs(cpu, v6_cp_reginfo);
7178 } else {
7179 define_arm_cp_regs(cpu, not_v6_cp_reginfo);
7180 }
4d31c596
PM
7181 if (arm_feature(env, ARM_FEATURE_V6K)) {
7182 define_arm_cp_regs(cpu, v6k_cp_reginfo);
7183 }
5e5cf9e3 7184 if (arm_feature(env, ARM_FEATURE_V7MP) &&
452a0955 7185 !arm_feature(env, ARM_FEATURE_PMSA)) {
995939a6
PM
7186 define_arm_cp_regs(cpu, v7mp_cp_reginfo);
7187 }
327dd510
AL
7188 if (arm_feature(env, ARM_FEATURE_V7VE)) {
7189 define_arm_cp_regs(cpu, pmovsset_cp_reginfo);
7190 }
e9aa6c21 7191 if (arm_feature(env, ARM_FEATURE_V7)) {
776d4e5c 7192 ARMCPRegInfo clidr = {
7da845b0
PM
7193 .name = "CLIDR", .state = ARM_CP_STATE_BOTH,
7194 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 1,
630fcd4d
MZ
7195 .access = PL1_R, .type = ARM_CP_CONST,
7196 .accessfn = access_aa64_tid2,
7197 .resetvalue = cpu->clidr
776d4e5c 7198 };
776d4e5c 7199 define_one_arm_cp_reg(cpu, &clidr);
e9aa6c21 7200 define_arm_cp_regs(cpu, v7_cp_reginfo);
50300698 7201 define_debug_regs(cpu);
24183fb6 7202 define_pmu_regs(cpu);
7d57f408
PM
7203 } else {
7204 define_arm_cp_regs(cpu, not_v7_cp_reginfo);
e9aa6c21 7205 }
b0d2b7d0 7206 if (arm_feature(env, ARM_FEATURE_V8)) {
e20d84c1
PM
7207 /* AArch64 ID registers, which all have impdef reset values.
7208 * Note that within the ID register ranges the unused slots
7209 * must all RAZ, not UNDEF; future architecture versions may
7210 * define new registers here.
7211 */
e60cef86 7212 ARMCPRegInfo v8_idregs[] = {
976b99b6
AB
7213 /*
7214 * ID_AA64PFR0_EL1 is not a plain ARM_CP_CONST in system
7215 * emulation because we don't know the right value for the
7216 * GIC field until after we define these regs.
96a8b92e 7217 */
e60cef86
PM
7218 { .name = "ID_AA64PFR0_EL1", .state = ARM_CP_STATE_AA64,
7219 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 0,
976b99b6
AB
7220 .access = PL1_R,
7221#ifdef CONFIG_USER_ONLY
7222 .type = ARM_CP_CONST,
7223 .resetvalue = cpu->isar.id_aa64pfr0
7224#else
7225 .type = ARM_CP_NO_RAW,
6a4ef4e5 7226 .accessfn = access_aa64_tid3,
96a8b92e 7227 .readfn = id_aa64pfr0_read,
976b99b6
AB
7228 .writefn = arm_cp_write_ignore
7229#endif
7230 },
e60cef86
PM
7231 { .name = "ID_AA64PFR1_EL1", .state = ARM_CP_STATE_AA64,
7232 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 1,
7233 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7234 .accessfn = access_aa64_tid3,
47576b94 7235 .resetvalue = cpu->isar.id_aa64pfr1},
e20d84c1
PM
7236 { .name = "ID_AA64PFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7237 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 2,
7238 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7239 .accessfn = access_aa64_tid3,
e20d84c1
PM
7240 .resetvalue = 0 },
7241 { .name = "ID_AA64PFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7242 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 3,
7243 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7244 .accessfn = access_aa64_tid3,
e20d84c1 7245 .resetvalue = 0 },
9516d772 7246 { .name = "ID_AA64ZFR0_EL1", .state = ARM_CP_STATE_AA64,
e20d84c1
PM
7247 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 4,
7248 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7249 .accessfn = access_aa64_tid3,
9516d772 7250 /* At present, only SVEver == 0 is defined anyway. */
e20d84c1
PM
7251 .resetvalue = 0 },
7252 { .name = "ID_AA64PFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7253 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 5,
7254 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7255 .accessfn = access_aa64_tid3,
e20d84c1
PM
7256 .resetvalue = 0 },
7257 { .name = "ID_AA64PFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7258 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 6,
7259 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7260 .accessfn = access_aa64_tid3,
e20d84c1
PM
7261 .resetvalue = 0 },
7262 { .name = "ID_AA64PFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7263 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 7,
7264 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7265 .accessfn = access_aa64_tid3,
e20d84c1 7266 .resetvalue = 0 },
e60cef86
PM
7267 { .name = "ID_AA64DFR0_EL1", .state = ARM_CP_STATE_AA64,
7268 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 0,
7269 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7270 .accessfn = access_aa64_tid3,
2a609df8 7271 .resetvalue = cpu->isar.id_aa64dfr0 },
e60cef86
PM
7272 { .name = "ID_AA64DFR1_EL1", .state = ARM_CP_STATE_AA64,
7273 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 1,
7274 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7275 .accessfn = access_aa64_tid3,
2a609df8 7276 .resetvalue = cpu->isar.id_aa64dfr1 },
e20d84c1
PM
7277 { .name = "ID_AA64DFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7278 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 2,
7279 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7280 .accessfn = access_aa64_tid3,
e20d84c1
PM
7281 .resetvalue = 0 },
7282 { .name = "ID_AA64DFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7283 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 3,
7284 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7285 .accessfn = access_aa64_tid3,
e20d84c1 7286 .resetvalue = 0 },
e60cef86
PM
7287 { .name = "ID_AA64AFR0_EL1", .state = ARM_CP_STATE_AA64,
7288 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 4,
7289 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7290 .accessfn = access_aa64_tid3,
e60cef86
PM
7291 .resetvalue = cpu->id_aa64afr0 },
7292 { .name = "ID_AA64AFR1_EL1", .state = ARM_CP_STATE_AA64,
7293 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 5,
7294 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7295 .accessfn = access_aa64_tid3,
e60cef86 7296 .resetvalue = cpu->id_aa64afr1 },
e20d84c1
PM
7297 { .name = "ID_AA64AFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7298 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 6,
7299 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7300 .accessfn = access_aa64_tid3,
e20d84c1
PM
7301 .resetvalue = 0 },
7302 { .name = "ID_AA64AFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7303 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 7,
7304 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7305 .accessfn = access_aa64_tid3,
e20d84c1 7306 .resetvalue = 0 },
e60cef86
PM
7307 { .name = "ID_AA64ISAR0_EL1", .state = ARM_CP_STATE_AA64,
7308 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 0,
7309 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7310 .accessfn = access_aa64_tid3,
47576b94 7311 .resetvalue = cpu->isar.id_aa64isar0 },
e60cef86
PM
7312 { .name = "ID_AA64ISAR1_EL1", .state = ARM_CP_STATE_AA64,
7313 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 1,
7314 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7315 .accessfn = access_aa64_tid3,
47576b94 7316 .resetvalue = cpu->isar.id_aa64isar1 },
e20d84c1
PM
7317 { .name = "ID_AA64ISAR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7318 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 2,
7319 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7320 .accessfn = access_aa64_tid3,
e20d84c1
PM
7321 .resetvalue = 0 },
7322 { .name = "ID_AA64ISAR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7323 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 3,
7324 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7325 .accessfn = access_aa64_tid3,
e20d84c1
PM
7326 .resetvalue = 0 },
7327 { .name = "ID_AA64ISAR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7328 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 4,
7329 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7330 .accessfn = access_aa64_tid3,
e20d84c1
PM
7331 .resetvalue = 0 },
7332 { .name = "ID_AA64ISAR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7333 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 5,
7334 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7335 .accessfn = access_aa64_tid3,
e20d84c1
PM
7336 .resetvalue = 0 },
7337 { .name = "ID_AA64ISAR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7338 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 6,
7339 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7340 .accessfn = access_aa64_tid3,
e20d84c1
PM
7341 .resetvalue = 0 },
7342 { .name = "ID_AA64ISAR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7343 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 7,
7344 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7345 .accessfn = access_aa64_tid3,
e20d84c1 7346 .resetvalue = 0 },
e60cef86
PM
7347 { .name = "ID_AA64MMFR0_EL1", .state = ARM_CP_STATE_AA64,
7348 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
7349 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7350 .accessfn = access_aa64_tid3,
3dc91ddb 7351 .resetvalue = cpu->isar.id_aa64mmfr0 },
e60cef86
PM
7352 { .name = "ID_AA64MMFR1_EL1", .state = ARM_CP_STATE_AA64,
7353 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 1,
7354 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7355 .accessfn = access_aa64_tid3,
3dc91ddb 7356 .resetvalue = cpu->isar.id_aa64mmfr1 },
64761e10 7357 { .name = "ID_AA64MMFR2_EL1", .state = ARM_CP_STATE_AA64,
e20d84c1
PM
7358 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 2,
7359 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7360 .accessfn = access_aa64_tid3,
64761e10 7361 .resetvalue = cpu->isar.id_aa64mmfr2 },
e20d84c1
PM
7362 { .name = "ID_AA64MMFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7363 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 3,
7364 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7365 .accessfn = access_aa64_tid3,
e20d84c1
PM
7366 .resetvalue = 0 },
7367 { .name = "ID_AA64MMFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7368 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 4,
7369 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7370 .accessfn = access_aa64_tid3,
e20d84c1
PM
7371 .resetvalue = 0 },
7372 { .name = "ID_AA64MMFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7373 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 5,
7374 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7375 .accessfn = access_aa64_tid3,
e20d84c1
PM
7376 .resetvalue = 0 },
7377 { .name = "ID_AA64MMFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7378 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 6,
7379 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7380 .accessfn = access_aa64_tid3,
e20d84c1
PM
7381 .resetvalue = 0 },
7382 { .name = "ID_AA64MMFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7383 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 7,
7384 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7385 .accessfn = access_aa64_tid3,
e20d84c1 7386 .resetvalue = 0 },
a50c0f51
PM
7387 { .name = "MVFR0_EL1", .state = ARM_CP_STATE_AA64,
7388 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0,
7389 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7390 .accessfn = access_aa64_tid3,
47576b94 7391 .resetvalue = cpu->isar.mvfr0 },
a50c0f51
PM
7392 { .name = "MVFR1_EL1", .state = ARM_CP_STATE_AA64,
7393 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1,
7394 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7395 .accessfn = access_aa64_tid3,
47576b94 7396 .resetvalue = cpu->isar.mvfr1 },
a50c0f51
PM
7397 { .name = "MVFR2_EL1", .state = ARM_CP_STATE_AA64,
7398 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2,
7399 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7400 .accessfn = access_aa64_tid3,
47576b94 7401 .resetvalue = cpu->isar.mvfr2 },
e20d84c1
PM
7402 { .name = "MVFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7403 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 3,
7404 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7405 .accessfn = access_aa64_tid3,
e20d84c1
PM
7406 .resetvalue = 0 },
7407 { .name = "MVFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7408 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 4,
7409 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7410 .accessfn = access_aa64_tid3,
e20d84c1
PM
7411 .resetvalue = 0 },
7412 { .name = "MVFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7413 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 5,
7414 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7415 .accessfn = access_aa64_tid3,
e20d84c1
PM
7416 .resetvalue = 0 },
7417 { .name = "MVFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7418 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 6,
7419 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7420 .accessfn = access_aa64_tid3,
e20d84c1
PM
7421 .resetvalue = 0 },
7422 { .name = "MVFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7423 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 7,
7424 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7425 .accessfn = access_aa64_tid3,
e20d84c1 7426 .resetvalue = 0 },
4054bfa9
AF
7427 { .name = "PMCEID0", .state = ARM_CP_STATE_AA32,
7428 .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 6,
7429 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
cad86737 7430 .resetvalue = extract64(cpu->pmceid0, 0, 32) },
4054bfa9
AF
7431 { .name = "PMCEID0_EL0", .state = ARM_CP_STATE_AA64,
7432 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 6,
7433 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
7434 .resetvalue = cpu->pmceid0 },
7435 { .name = "PMCEID1", .state = ARM_CP_STATE_AA32,
7436 .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 7,
7437 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
cad86737 7438 .resetvalue = extract64(cpu->pmceid1, 0, 32) },
4054bfa9
AF
7439 { .name = "PMCEID1_EL0", .state = ARM_CP_STATE_AA64,
7440 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 7,
7441 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
7442 .resetvalue = cpu->pmceid1 },
e60cef86
PM
7443 REGINFO_SENTINEL
7444 };
6c5c0fec
AB
7445#ifdef CONFIG_USER_ONLY
7446 ARMCPRegUserSpaceInfo v8_user_idregs[] = {
7447 { .name = "ID_AA64PFR0_EL1",
7448 .exported_bits = 0x000f000f00ff0000,
7449 .fixed_bits = 0x0000000000000011 },
7450 { .name = "ID_AA64PFR1_EL1",
7451 .exported_bits = 0x00000000000000f0 },
d040242e
AB
7452 { .name = "ID_AA64PFR*_EL1_RESERVED",
7453 .is_glob = true },
6c5c0fec
AB
7454 { .name = "ID_AA64ZFR0_EL1" },
7455 { .name = "ID_AA64MMFR0_EL1",
7456 .fixed_bits = 0x00000000ff000000 },
7457 { .name = "ID_AA64MMFR1_EL1" },
d040242e
AB
7458 { .name = "ID_AA64MMFR*_EL1_RESERVED",
7459 .is_glob = true },
6c5c0fec
AB
7460 { .name = "ID_AA64DFR0_EL1",
7461 .fixed_bits = 0x0000000000000006 },
7462 { .name = "ID_AA64DFR1_EL1" },
d040242e
AB
7463 { .name = "ID_AA64DFR*_EL1_RESERVED",
7464 .is_glob = true },
7465 { .name = "ID_AA64AFR*",
7466 .is_glob = true },
6c5c0fec
AB
7467 { .name = "ID_AA64ISAR0_EL1",
7468 .exported_bits = 0x00fffffff0fffff0 },
7469 { .name = "ID_AA64ISAR1_EL1",
7470 .exported_bits = 0x000000f0ffffffff },
d040242e
AB
7471 { .name = "ID_AA64ISAR*_EL1_RESERVED",
7472 .is_glob = true },
6c5c0fec
AB
7473 REGUSERINFO_SENTINEL
7474 };
7475 modify_arm_cp_regs(v8_idregs, v8_user_idregs);
7476#endif
be8e8128
GB
7477 /* RVBAR_EL1 is only implemented if EL1 is the highest EL */
7478 if (!arm_feature(env, ARM_FEATURE_EL3) &&
7479 !arm_feature(env, ARM_FEATURE_EL2)) {
7480 ARMCPRegInfo rvbar = {
7481 .name = "RVBAR_EL1", .state = ARM_CP_STATE_AA64,
7482 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
7483 .type = ARM_CP_CONST, .access = PL1_R, .resetvalue = cpu->rvbar
7484 };
7485 define_one_arm_cp_reg(cpu, &rvbar);
7486 }
e60cef86 7487 define_arm_cp_regs(cpu, v8_idregs);
b0d2b7d0
PM
7488 define_arm_cp_regs(cpu, v8_cp_reginfo);
7489 }
3b685ba7 7490 if (arm_feature(env, ARM_FEATURE_EL2)) {
f0d574d6 7491 uint64_t vmpidr_def = mpidr_read_val(env);
731de9e6
EI
7492 ARMCPRegInfo vpidr_regs[] = {
7493 { .name = "VPIDR", .state = ARM_CP_STATE_AA32,
7494 .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
7495 .access = PL2_RW, .accessfn = access_el3_aa32ns,
36476562
PM
7496 .resetvalue = cpu->midr, .type = ARM_CP_ALIAS,
7497 .fieldoffset = offsetoflow32(CPUARMState, cp15.vpidr_el2) },
731de9e6
EI
7498 { .name = "VPIDR_EL2", .state = ARM_CP_STATE_AA64,
7499 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
7500 .access = PL2_RW, .resetvalue = cpu->midr,
7501 .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) },
f0d574d6
EI
7502 { .name = "VMPIDR", .state = ARM_CP_STATE_AA32,
7503 .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
7504 .access = PL2_RW, .accessfn = access_el3_aa32ns,
36476562
PM
7505 .resetvalue = vmpidr_def, .type = ARM_CP_ALIAS,
7506 .fieldoffset = offsetoflow32(CPUARMState, cp15.vmpidr_el2) },
f0d574d6
EI
7507 { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_AA64,
7508 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
7509 .access = PL2_RW,
7510 .resetvalue = vmpidr_def,
7511 .fieldoffset = offsetof(CPUARMState, cp15.vmpidr_el2) },
731de9e6
EI
7512 REGINFO_SENTINEL
7513 };
7514 define_arm_cp_regs(cpu, vpidr_regs);
4771cd01 7515 define_arm_cp_regs(cpu, el2_cp_reginfo);
ce4afed8
PM
7516 if (arm_feature(env, ARM_FEATURE_V8)) {
7517 define_arm_cp_regs(cpu, el2_v8_cp_reginfo);
7518 }
be8e8128
GB
7519 /* RVBAR_EL2 is only implemented if EL2 is the highest EL */
7520 if (!arm_feature(env, ARM_FEATURE_EL3)) {
7521 ARMCPRegInfo rvbar = {
7522 .name = "RVBAR_EL2", .state = ARM_CP_STATE_AA64,
7523 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 1,
7524 .type = ARM_CP_CONST, .access = PL2_R, .resetvalue = cpu->rvbar
7525 };
7526 define_one_arm_cp_reg(cpu, &rvbar);
7527 }
d42e3c26
EI
7528 } else {
7529 /* If EL2 is missing but higher ELs are enabled, we need to
7530 * register the no_el2 reginfos.
7531 */
7532 if (arm_feature(env, ARM_FEATURE_EL3)) {
f0d574d6
EI
7533 /* When EL3 exists but not EL2, VPIDR and VMPIDR take the value
7534 * of MIDR_EL1 and MPIDR_EL1.
731de9e6
EI
7535 */
7536 ARMCPRegInfo vpidr_regs[] = {
7537 { .name = "VPIDR_EL2", .state = ARM_CP_STATE_BOTH,
7538 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
7539 .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any,
7540 .type = ARM_CP_CONST, .resetvalue = cpu->midr,
7541 .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) },
f0d574d6
EI
7542 { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_BOTH,
7543 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
7544 .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any,
7545 .type = ARM_CP_NO_RAW,
7546 .writefn = arm_cp_write_ignore, .readfn = mpidr_read },
731de9e6
EI
7547 REGINFO_SENTINEL
7548 };
7549 define_arm_cp_regs(cpu, vpidr_regs);
4771cd01 7550 define_arm_cp_regs(cpu, el3_no_el2_cp_reginfo);
ce4afed8
PM
7551 if (arm_feature(env, ARM_FEATURE_V8)) {
7552 define_arm_cp_regs(cpu, el3_no_el2_v8_cp_reginfo);
7553 }
d42e3c26 7554 }
3b685ba7 7555 }
81547d66 7556 if (arm_feature(env, ARM_FEATURE_EL3)) {
0f1a3b24 7557 define_arm_cp_regs(cpu, el3_cp_reginfo);
e24fdd23
PM
7558 ARMCPRegInfo el3_regs[] = {
7559 { .name = "RVBAR_EL3", .state = ARM_CP_STATE_AA64,
7560 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 1,
7561 .type = ARM_CP_CONST, .access = PL3_R, .resetvalue = cpu->rvbar },
7562 { .name = "SCTLR_EL3", .state = ARM_CP_STATE_AA64,
7563 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 0,
7564 .access = PL3_RW,
7565 .raw_writefn = raw_write, .writefn = sctlr_write,
7566 .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[3]),
7567 .resetvalue = cpu->reset_sctlr },
7568 REGINFO_SENTINEL
be8e8128 7569 };
e24fdd23
PM
7570
7571 define_arm_cp_regs(cpu, el3_regs);
81547d66 7572 }
2f027fc5
PM
7573 /* The behaviour of NSACR is sufficiently various that we don't
7574 * try to describe it in a single reginfo:
7575 * if EL3 is 64 bit, then trap to EL3 from S EL1,
7576 * reads as constant 0xc00 from NS EL1 and NS EL2
7577 * if EL3 is 32 bit, then RW at EL3, RO at NS EL1 and NS EL2
7578 * if v7 without EL3, register doesn't exist
7579 * if v8 without EL3, reads as constant 0xc00 from NS EL1 and NS EL2
7580 */
7581 if (arm_feature(env, ARM_FEATURE_EL3)) {
7582 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
7583 ARMCPRegInfo nsacr = {
7584 .name = "NSACR", .type = ARM_CP_CONST,
7585 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
7586 .access = PL1_RW, .accessfn = nsacr_access,
7587 .resetvalue = 0xc00
7588 };
7589 define_one_arm_cp_reg(cpu, &nsacr);
7590 } else {
7591 ARMCPRegInfo nsacr = {
7592 .name = "NSACR",
7593 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
7594 .access = PL3_RW | PL1_R,
7595 .resetvalue = 0,
7596 .fieldoffset = offsetof(CPUARMState, cp15.nsacr)
7597 };
7598 define_one_arm_cp_reg(cpu, &nsacr);
7599 }
7600 } else {
7601 if (arm_feature(env, ARM_FEATURE_V8)) {
7602 ARMCPRegInfo nsacr = {
7603 .name = "NSACR", .type = ARM_CP_CONST,
7604 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
7605 .access = PL1_R,
7606 .resetvalue = 0xc00
7607 };
7608 define_one_arm_cp_reg(cpu, &nsacr);
7609 }
7610 }
7611
452a0955 7612 if (arm_feature(env, ARM_FEATURE_PMSA)) {
6cb0b013
PC
7613 if (arm_feature(env, ARM_FEATURE_V6)) {
7614 /* PMSAv6 not implemented */
7615 assert(arm_feature(env, ARM_FEATURE_V7));
7616 define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
7617 define_arm_cp_regs(cpu, pmsav7_cp_reginfo);
7618 } else {
7619 define_arm_cp_regs(cpu, pmsav5_cp_reginfo);
7620 }
18032bec 7621 } else {
8e5d75c9 7622 define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
18032bec 7623 define_arm_cp_regs(cpu, vmsa_cp_reginfo);
4036b7d1
PM
7624 /* TTCBR2 is introduced with ARMv8.2-AA32HPD. */
7625 if (cpu_isar_feature(aa32_hpd, cpu)) {
ab638a32
RH
7626 define_one_arm_cp_reg(cpu, &ttbcr2_reginfo);
7627 }
18032bec 7628 }
c326b979
PM
7629 if (arm_feature(env, ARM_FEATURE_THUMB2EE)) {
7630 define_arm_cp_regs(cpu, t2ee_cp_reginfo);
7631 }
6cc7a3ae
PM
7632 if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) {
7633 define_arm_cp_regs(cpu, generic_timer_cp_reginfo);
7634 }
4a501606
PM
7635 if (arm_feature(env, ARM_FEATURE_VAPA)) {
7636 define_arm_cp_regs(cpu, vapa_cp_reginfo);
7637 }
c4804214
PM
7638 if (arm_feature(env, ARM_FEATURE_CACHE_TEST_CLEAN)) {
7639 define_arm_cp_regs(cpu, cache_test_clean_cp_reginfo);
7640 }
7641 if (arm_feature(env, ARM_FEATURE_CACHE_DIRTY_REG)) {
7642 define_arm_cp_regs(cpu, cache_dirty_status_cp_reginfo);
7643 }
7644 if (arm_feature(env, ARM_FEATURE_CACHE_BLOCK_OPS)) {
7645 define_arm_cp_regs(cpu, cache_block_ops_cp_reginfo);
7646 }
18032bec
PM
7647 if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
7648 define_arm_cp_regs(cpu, omap_cp_reginfo);
7649 }
34f90529
PM
7650 if (arm_feature(env, ARM_FEATURE_STRONGARM)) {
7651 define_arm_cp_regs(cpu, strongarm_cp_reginfo);
7652 }
1047b9d7
PM
7653 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
7654 define_arm_cp_regs(cpu, xscale_cp_reginfo);
7655 }
7656 if (arm_feature(env, ARM_FEATURE_DUMMY_C15_REGS)) {
7657 define_arm_cp_regs(cpu, dummy_c15_cp_reginfo);
7658 }
7ac681cf
PM
7659 if (arm_feature(env, ARM_FEATURE_LPAE)) {
7660 define_arm_cp_regs(cpu, lpae_cp_reginfo);
7661 }
873b73c0 7662 if (cpu_isar_feature(aa32_jazelle, cpu)) {
f96f3d5f
MZ
7663 define_arm_cp_regs(cpu, jazelle_regs);
7664 }
7884849c
PM
7665 /* Slightly awkwardly, the OMAP and StrongARM cores need all of
7666 * cp15 crn=0 to be writes-ignored, whereas for other cores they should
7667 * be read-only (ie write causes UNDEF exception).
7668 */
7669 {
00a29f3d
PM
7670 ARMCPRegInfo id_pre_v8_midr_cp_reginfo[] = {
7671 /* Pre-v8 MIDR space.
7672 * Note that the MIDR isn't a simple constant register because
7884849c
PM
7673 * of the TI925 behaviour where writes to another register can
7674 * cause the MIDR value to change.
97ce8d61
PC
7675 *
7676 * Unimplemented registers in the c15 0 0 0 space default to
7677 * MIDR. Define MIDR first as this entire space, then CTR, TCMTR
7678 * and friends override accordingly.
7884849c
PM
7679 */
7680 { .name = "MIDR",
97ce8d61 7681 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = CP_ANY,
7884849c 7682 .access = PL1_R, .resetvalue = cpu->midr,
d4e6df63 7683 .writefn = arm_cp_write_ignore, .raw_writefn = raw_write,
731de9e6 7684 .readfn = midr_read,
97ce8d61
PC
7685 .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
7686 .type = ARM_CP_OVERRIDE },
7884849c
PM
7687 /* crn = 0 op1 = 0 crm = 3..7 : currently unassigned; we RAZ. */
7688 { .name = "DUMMY",
7689 .cp = 15, .crn = 0, .crm = 3, .opc1 = 0, .opc2 = CP_ANY,
7690 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
7691 { .name = "DUMMY",
7692 .cp = 15, .crn = 0, .crm = 4, .opc1 = 0, .opc2 = CP_ANY,
7693 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
7694 { .name = "DUMMY",
7695 .cp = 15, .crn = 0, .crm = 5, .opc1 = 0, .opc2 = CP_ANY,
7696 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
7697 { .name = "DUMMY",
7698 .cp = 15, .crn = 0, .crm = 6, .opc1 = 0, .opc2 = CP_ANY,
7699 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
7700 { .name = "DUMMY",
7701 .cp = 15, .crn = 0, .crm = 7, .opc1 = 0, .opc2 = CP_ANY,
7702 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
7703 REGINFO_SENTINEL
7704 };
00a29f3d 7705 ARMCPRegInfo id_v8_midr_cp_reginfo[] = {
00a29f3d
PM
7706 { .name = "MIDR_EL1", .state = ARM_CP_STATE_BOTH,
7707 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 0,
731de9e6
EI
7708 .access = PL1_R, .type = ARM_CP_NO_RAW, .resetvalue = cpu->midr,
7709 .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
7710 .readfn = midr_read },
ac00c79f
SF
7711 /* crn = 0 op1 = 0 crm = 0 op2 = 4,7 : AArch32 aliases of MIDR */
7712 { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
7713 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
7714 .access = PL1_R, .resetvalue = cpu->midr },
7715 { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
7716 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 7,
7717 .access = PL1_R, .resetvalue = cpu->midr },
00a29f3d
PM
7718 { .name = "REVIDR_EL1", .state = ARM_CP_STATE_BOTH,
7719 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 6,
93fbc983
MZ
7720 .access = PL1_R,
7721 .accessfn = access_aa64_tid1,
7722 .type = ARM_CP_CONST, .resetvalue = cpu->revidr },
00a29f3d
PM
7723 REGINFO_SENTINEL
7724 };
7725 ARMCPRegInfo id_cp_reginfo[] = {
7726 /* These are common to v8 and pre-v8 */
7727 { .name = "CTR",
7728 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 1,
630fcd4d
MZ
7729 .access = PL1_R, .accessfn = ctr_el0_access,
7730 .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
00a29f3d
PM
7731 { .name = "CTR_EL0", .state = ARM_CP_STATE_AA64,
7732 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 0, .crm = 0,
7733 .access = PL0_R, .accessfn = ctr_el0_access,
7734 .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
7735 /* TCMTR and TLBTR exist in v8 but have no 64-bit versions */
7736 { .name = "TCMTR",
7737 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 2,
93fbc983
MZ
7738 .access = PL1_R,
7739 .accessfn = access_aa32_tid1,
7740 .type = ARM_CP_CONST, .resetvalue = 0 },
00a29f3d
PM
7741 REGINFO_SENTINEL
7742 };
8085ce63
PC
7743 /* TLBTR is specific to VMSA */
7744 ARMCPRegInfo id_tlbtr_reginfo = {
7745 .name = "TLBTR",
7746 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 3,
93fbc983
MZ
7747 .access = PL1_R,
7748 .accessfn = access_aa32_tid1,
7749 .type = ARM_CP_CONST, .resetvalue = 0,
8085ce63 7750 };
3281af81
PC
7751 /* MPUIR is specific to PMSA V6+ */
7752 ARMCPRegInfo id_mpuir_reginfo = {
7753 .name = "MPUIR",
7754 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
7755 .access = PL1_R, .type = ARM_CP_CONST,
7756 .resetvalue = cpu->pmsav7_dregion << 8
7757 };
7884849c
PM
7758 ARMCPRegInfo crn0_wi_reginfo = {
7759 .name = "CRN0_WI", .cp = 15, .crn = 0, .crm = CP_ANY,
7760 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_W,
7761 .type = ARM_CP_NOP | ARM_CP_OVERRIDE
7762 };
6c5c0fec
AB
7763#ifdef CONFIG_USER_ONLY
7764 ARMCPRegUserSpaceInfo id_v8_user_midr_cp_reginfo[] = {
7765 { .name = "MIDR_EL1",
7766 .exported_bits = 0x00000000ffffffff },
7767 { .name = "REVIDR_EL1" },
7768 REGUSERINFO_SENTINEL
7769 };
7770 modify_arm_cp_regs(id_v8_midr_cp_reginfo, id_v8_user_midr_cp_reginfo);
7771#endif
7884849c
PM
7772 if (arm_feature(env, ARM_FEATURE_OMAPCP) ||
7773 arm_feature(env, ARM_FEATURE_STRONGARM)) {
7774 ARMCPRegInfo *r;
7775 /* Register the blanket "writes ignored" value first to cover the
a703eda1
PC
7776 * whole space. Then update the specific ID registers to allow write
7777 * access, so that they ignore writes rather than causing them to
7778 * UNDEF.
7884849c
PM
7779 */
7780 define_one_arm_cp_reg(cpu, &crn0_wi_reginfo);
00a29f3d
PM
7781 for (r = id_pre_v8_midr_cp_reginfo;
7782 r->type != ARM_CP_SENTINEL; r++) {
7783 r->access = PL1_RW;
7784 }
7884849c
PM
7785 for (r = id_cp_reginfo; r->type != ARM_CP_SENTINEL; r++) {
7786 r->access = PL1_RW;
7884849c 7787 }
10006112 7788 id_mpuir_reginfo.access = PL1_RW;
3281af81 7789 id_tlbtr_reginfo.access = PL1_RW;
7884849c 7790 }
00a29f3d
PM
7791 if (arm_feature(env, ARM_FEATURE_V8)) {
7792 define_arm_cp_regs(cpu, id_v8_midr_cp_reginfo);
7793 } else {
7794 define_arm_cp_regs(cpu, id_pre_v8_midr_cp_reginfo);
7795 }
a703eda1 7796 define_arm_cp_regs(cpu, id_cp_reginfo);
452a0955 7797 if (!arm_feature(env, ARM_FEATURE_PMSA)) {
8085ce63 7798 define_one_arm_cp_reg(cpu, &id_tlbtr_reginfo);
3281af81
PC
7799 } else if (arm_feature(env, ARM_FEATURE_V7)) {
7800 define_one_arm_cp_reg(cpu, &id_mpuir_reginfo);
8085ce63 7801 }
7884849c
PM
7802 }
7803
97ce8d61 7804 if (arm_feature(env, ARM_FEATURE_MPIDR)) {
52264166
AB
7805 ARMCPRegInfo mpidr_cp_reginfo[] = {
7806 { .name = "MPIDR_EL1", .state = ARM_CP_STATE_BOTH,
7807 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 5,
7808 .access = PL1_R, .readfn = mpidr_read, .type = ARM_CP_NO_RAW },
7809 REGINFO_SENTINEL
7810 };
7811#ifdef CONFIG_USER_ONLY
7812 ARMCPRegUserSpaceInfo mpidr_user_cp_reginfo[] = {
7813 { .name = "MPIDR_EL1",
7814 .fixed_bits = 0x0000000080000000 },
7815 REGUSERINFO_SENTINEL
7816 };
7817 modify_arm_cp_regs(mpidr_cp_reginfo, mpidr_user_cp_reginfo);
7818#endif
97ce8d61
PC
7819 define_arm_cp_regs(cpu, mpidr_cp_reginfo);
7820 }
7821
2771db27 7822 if (arm_feature(env, ARM_FEATURE_AUXCR)) {
834a6c69
PM
7823 ARMCPRegInfo auxcr_reginfo[] = {
7824 { .name = "ACTLR_EL1", .state = ARM_CP_STATE_BOTH,
7825 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 1,
99602377
RH
7826 .access = PL1_RW, .accessfn = access_tacr,
7827 .type = ARM_CP_CONST, .resetvalue = cpu->reset_auxcr },
834a6c69
PM
7828 { .name = "ACTLR_EL2", .state = ARM_CP_STATE_BOTH,
7829 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 1,
7830 .access = PL2_RW, .type = ARM_CP_CONST,
7831 .resetvalue = 0 },
7832 { .name = "ACTLR_EL3", .state = ARM_CP_STATE_AA64,
7833 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 1,
7834 .access = PL3_RW, .type = ARM_CP_CONST,
7835 .resetvalue = 0 },
7836 REGINFO_SENTINEL
2771db27 7837 };
834a6c69 7838 define_arm_cp_regs(cpu, auxcr_reginfo);
f6287c24
PM
7839 if (cpu_isar_feature(aa32_ac2, cpu)) {
7840 define_arm_cp_regs(cpu, actlr2_hactlr2_reginfo);
0e0456ab 7841 }
2771db27
PM
7842 }
7843
d8ba780b 7844 if (arm_feature(env, ARM_FEATURE_CBAR)) {
d56974af
LM
7845 /*
7846 * CBAR is IMPDEF, but common on Arm Cortex-A implementations.
7847 * There are two flavours:
7848 * (1) older 32-bit only cores have a simple 32-bit CBAR
7849 * (2) 64-bit cores have a 64-bit CBAR visible to AArch64, plus a
7850 * 32-bit register visible to AArch32 at a different encoding
7851 * to the "flavour 1" register and with the bits rearranged to
7852 * be able to squash a 64-bit address into the 32-bit view.
7853 * We distinguish the two via the ARM_FEATURE_AARCH64 flag, but
7854 * in future if we support AArch32-only configs of some of the
7855 * AArch64 cores we might need to add a specific feature flag
7856 * to indicate cores with "flavour 2" CBAR.
7857 */
f318cec6
PM
7858 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
7859 /* 32 bit view is [31:18] 0...0 [43:32]. */
7860 uint32_t cbar32 = (extract64(cpu->reset_cbar, 18, 14) << 18)
7861 | extract64(cpu->reset_cbar, 32, 12);
7862 ARMCPRegInfo cbar_reginfo[] = {
7863 { .name = "CBAR",
7864 .type = ARM_CP_CONST,
d56974af
LM
7865 .cp = 15, .crn = 15, .crm = 3, .opc1 = 1, .opc2 = 0,
7866 .access = PL1_R, .resetvalue = cbar32 },
f318cec6
PM
7867 { .name = "CBAR_EL1", .state = ARM_CP_STATE_AA64,
7868 .type = ARM_CP_CONST,
7869 .opc0 = 3, .opc1 = 1, .crn = 15, .crm = 3, .opc2 = 0,
d56974af 7870 .access = PL1_R, .resetvalue = cpu->reset_cbar },
f318cec6
PM
7871 REGINFO_SENTINEL
7872 };
7873 /* We don't implement a r/w 64 bit CBAR currently */
7874 assert(arm_feature(env, ARM_FEATURE_CBAR_RO));
7875 define_arm_cp_regs(cpu, cbar_reginfo);
7876 } else {
7877 ARMCPRegInfo cbar = {
7878 .name = "CBAR",
7879 .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0,
7880 .access = PL1_R|PL3_W, .resetvalue = cpu->reset_cbar,
7881 .fieldoffset = offsetof(CPUARMState,
7882 cp15.c15_config_base_address)
7883 };
7884 if (arm_feature(env, ARM_FEATURE_CBAR_RO)) {
7885 cbar.access = PL1_R;
7886 cbar.fieldoffset = 0;
7887 cbar.type = ARM_CP_CONST;
7888 }
7889 define_one_arm_cp_reg(cpu, &cbar);
7890 }
d8ba780b
PC
7891 }
7892
91db4642
CLG
7893 if (arm_feature(env, ARM_FEATURE_VBAR)) {
7894 ARMCPRegInfo vbar_cp_reginfo[] = {
7895 { .name = "VBAR", .state = ARM_CP_STATE_BOTH,
7896 .opc0 = 3, .crn = 12, .crm = 0, .opc1 = 0, .opc2 = 0,
7897 .access = PL1_RW, .writefn = vbar_write,
7898 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.vbar_s),
7899 offsetof(CPUARMState, cp15.vbar_ns) },
7900 .resetvalue = 0 },
7901 REGINFO_SENTINEL
7902 };
7903 define_arm_cp_regs(cpu, vbar_cp_reginfo);
7904 }
7905
2771db27
PM
7906 /* Generic registers whose values depend on the implementation */
7907 {
7908 ARMCPRegInfo sctlr = {
5ebafdf3 7909 .name = "SCTLR", .state = ARM_CP_STATE_BOTH,
137feaa9 7910 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
84929218 7911 .access = PL1_RW, .accessfn = access_tvm_trvm,
137feaa9
FA
7912 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.sctlr_s),
7913 offsetof(CPUARMState, cp15.sctlr_ns) },
d4e6df63
PM
7914 .writefn = sctlr_write, .resetvalue = cpu->reset_sctlr,
7915 .raw_writefn = raw_write,
2771db27
PM
7916 };
7917 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
7918 /* Normally we would always end the TB on an SCTLR write, but Linux
7919 * arch/arm/mach-pxa/sleep.S expects two instructions following
7920 * an MMU enable to execute from cache. Imitate this behaviour.
7921 */
7922 sctlr.type |= ARM_CP_SUPPRESS_TB_END;
7923 }
7924 define_one_arm_cp_reg(cpu, &sctlr);
7925 }
5be5e8ed 7926
2d7137c1 7927 if (cpu_isar_feature(aa64_lor, cpu)) {
2d7137c1
RH
7928 define_arm_cp_regs(cpu, lor_reginfo);
7929 }
220f508f
RH
7930 if (cpu_isar_feature(aa64_pan, cpu)) {
7931 define_one_arm_cp_reg(cpu, &pan_reginfo);
7932 }
04b07d29
RH
7933#ifndef CONFIG_USER_ONLY
7934 if (cpu_isar_feature(aa64_ats1e1, cpu)) {
7935 define_arm_cp_regs(cpu, ats1e1_reginfo);
7936 }
7937 if (cpu_isar_feature(aa32_ats1e1, cpu)) {
7938 define_arm_cp_regs(cpu, ats1cp_reginfo);
7939 }
7940#endif
9eeb7a1c
RH
7941 if (cpu_isar_feature(aa64_uao, cpu)) {
7942 define_one_arm_cp_reg(cpu, &uao_reginfo);
7943 }
2d7137c1 7944
e2a1a461
RH
7945 if (arm_feature(env, ARM_FEATURE_EL2) && cpu_isar_feature(aa64_vh, cpu)) {
7946 define_arm_cp_regs(cpu, vhe_reginfo);
7947 }
7948
cd208a1c 7949 if (cpu_isar_feature(aa64_sve, cpu)) {
5be5e8ed
RH
7950 define_one_arm_cp_reg(cpu, &zcr_el1_reginfo);
7951 if (arm_feature(env, ARM_FEATURE_EL2)) {
7952 define_one_arm_cp_reg(cpu, &zcr_el2_reginfo);
7953 } else {
7954 define_one_arm_cp_reg(cpu, &zcr_no_el2_reginfo);
7955 }
7956 if (arm_feature(env, ARM_FEATURE_EL3)) {
7957 define_one_arm_cp_reg(cpu, &zcr_el3_reginfo);
7958 }
7959 }
967aa94f
RH
7960
7961#ifdef TARGET_AARCH64
7962 if (cpu_isar_feature(aa64_pauth, cpu)) {
7963 define_arm_cp_regs(cpu, pauth_reginfo);
7964 }
de390645
RH
7965 if (cpu_isar_feature(aa64_rndr, cpu)) {
7966 define_arm_cp_regs(cpu, rndr_reginfo);
7967 }
0d57b499
BM
7968#ifndef CONFIG_USER_ONLY
7969 /* Data Cache clean instructions up to PoP */
7970 if (cpu_isar_feature(aa64_dcpop, cpu)) {
7971 define_one_arm_cp_reg(cpu, dcpop_reg);
7972
7973 if (cpu_isar_feature(aa64_dcpodp, cpu)) {
7974 define_one_arm_cp_reg(cpu, dcpodp_reg);
7975 }
7976 }
7977#endif /*CONFIG_USER_ONLY*/
967aa94f 7978#endif
cb570bd3 7979
22e57073 7980 if (cpu_isar_feature(any_predinv, cpu)) {
cb570bd3
RH
7981 define_arm_cp_regs(cpu, predinv_reginfo);
7982 }
e2cce18f 7983
957e6155
PM
7984 if (cpu_isar_feature(any_ccidx, cpu)) {
7985 define_arm_cp_regs(cpu, ccsidr2_reginfo);
7986 }
7987
e2cce18f
RH
7988#ifndef CONFIG_USER_ONLY
7989 /*
7990 * Register redirections and aliases must be done last,
7991 * after the registers from the other extensions have been defined.
7992 */
7993 if (arm_feature(env, ARM_FEATURE_EL2) && cpu_isar_feature(aa64_vh, cpu)) {
7994 define_arm_vh_e2h_redirects_aliases(cpu);
7995 }
7996#endif
2ceb98c0
PM
7997}
7998
14969266
AF
7999void arm_cpu_register_gdb_regs_for_features(ARMCPU *cpu)
8000{
22169d41 8001 CPUState *cs = CPU(cpu);
14969266
AF
8002 CPUARMState *env = &cpu->env;
8003
6a669427 8004 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
d12379c5
AB
8005 /*
8006 * The lower part of each SVE register aliases to the FPU
8007 * registers so we don't need to include both.
8008 */
8009#ifdef TARGET_AARCH64
8010 if (isar_feature_aa64_sve(&cpu->isar)) {
8011 gdb_register_coprocessor(cs, arm_gdb_get_svereg, arm_gdb_set_svereg,
8012 arm_gen_dynamic_svereg_xml(cs, cs->gdb_num_regs),
8013 "sve-registers.xml", 0);
8014 } else
8015#endif
8016 {
8017 gdb_register_coprocessor(cs, aarch64_fpu_gdb_get_reg,
8018 aarch64_fpu_gdb_set_reg,
8019 34, "aarch64-fpu.xml", 0);
8020 }
6a669427 8021 } else if (arm_feature(env, ARM_FEATURE_NEON)) {
22169d41 8022 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
56aebc89 8023 51, "arm-neon.xml", 0);
a6627f5f 8024 } else if (cpu_isar_feature(aa32_simd_r32, cpu)) {
22169d41 8025 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
56aebc89 8026 35, "arm-vfp3.xml", 0);
7fbc6a40 8027 } else if (cpu_isar_feature(aa32_vfp_simd, cpu)) {
22169d41 8028 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
56aebc89
PB
8029 19, "arm-vfp.xml", 0);
8030 }
200bf5b7 8031 gdb_register_coprocessor(cs, arm_gdb_get_sysreg, arm_gdb_set_sysreg,
32d6e32a 8032 arm_gen_dynamic_sysreg_xml(cs, cs->gdb_num_regs),
200bf5b7 8033 "system-registers.xml", 0);
d12379c5 8034
40f137e1
PB
8035}
8036
777dc784
PM
8037/* Sort alphabetically by type name, except for "any". */
8038static gint arm_cpu_list_compare(gconstpointer a, gconstpointer b)
5adb4839 8039{
777dc784
PM
8040 ObjectClass *class_a = (ObjectClass *)a;
8041 ObjectClass *class_b = (ObjectClass *)b;
8042 const char *name_a, *name_b;
5adb4839 8043
777dc784
PM
8044 name_a = object_class_get_name(class_a);
8045 name_b = object_class_get_name(class_b);
51492fd1 8046 if (strcmp(name_a, "any-" TYPE_ARM_CPU) == 0) {
777dc784 8047 return 1;
51492fd1 8048 } else if (strcmp(name_b, "any-" TYPE_ARM_CPU) == 0) {
777dc784
PM
8049 return -1;
8050 } else {
8051 return strcmp(name_a, name_b);
5adb4839
PB
8052 }
8053}
8054
777dc784 8055static void arm_cpu_list_entry(gpointer data, gpointer user_data)
40f137e1 8056{
777dc784 8057 ObjectClass *oc = data;
51492fd1
AF
8058 const char *typename;
8059 char *name;
3371d272 8060
51492fd1
AF
8061 typename = object_class_get_name(oc);
8062 name = g_strndup(typename, strlen(typename) - strlen("-" TYPE_ARM_CPU));
0442428a 8063 qemu_printf(" %s\n", name);
51492fd1 8064 g_free(name);
777dc784
PM
8065}
8066
0442428a 8067void arm_cpu_list(void)
777dc784 8068{
777dc784
PM
8069 GSList *list;
8070
8071 list = object_class_get_list(TYPE_ARM_CPU, false);
8072 list = g_slist_sort(list, arm_cpu_list_compare);
0442428a
MA
8073 qemu_printf("Available CPUs:\n");
8074 g_slist_foreach(list, arm_cpu_list_entry, NULL);
777dc784 8075 g_slist_free(list);
40f137e1
PB
8076}
8077
78027bb6
CR
8078static void arm_cpu_add_definition(gpointer data, gpointer user_data)
8079{
8080 ObjectClass *oc = data;
8081 CpuDefinitionInfoList **cpu_list = user_data;
8082 CpuDefinitionInfoList *entry;
8083 CpuDefinitionInfo *info;
8084 const char *typename;
8085
8086 typename = object_class_get_name(oc);
8087 info = g_malloc0(sizeof(*info));
8088 info->name = g_strndup(typename,
8089 strlen(typename) - strlen("-" TYPE_ARM_CPU));
8ed877b7 8090 info->q_typename = g_strdup(typename);
78027bb6
CR
8091
8092 entry = g_malloc0(sizeof(*entry));
8093 entry->value = info;
8094 entry->next = *cpu_list;
8095 *cpu_list = entry;
8096}
8097
25a9d6ca 8098CpuDefinitionInfoList *qmp_query_cpu_definitions(Error **errp)
78027bb6
CR
8099{
8100 CpuDefinitionInfoList *cpu_list = NULL;
8101 GSList *list;
8102
8103 list = object_class_get_list(TYPE_ARM_CPU, false);
8104 g_slist_foreach(list, arm_cpu_add_definition, &cpu_list);
8105 g_slist_free(list);
8106
8107 return cpu_list;
8108}
8109
6e6efd61 8110static void add_cpreg_to_hashtable(ARMCPU *cpu, const ARMCPRegInfo *r,
51a79b03 8111 void *opaque, int state, int secstate,
9c513e78
AB
8112 int crm, int opc1, int opc2,
8113 const char *name)
6e6efd61
PM
8114{
8115 /* Private utility function for define_one_arm_cp_reg_with_opaque():
8116 * add a single reginfo struct to the hash table.
8117 */
8118 uint32_t *key = g_new(uint32_t, 1);
8119 ARMCPRegInfo *r2 = g_memdup(r, sizeof(ARMCPRegInfo));
8120 int is64 = (r->type & ARM_CP_64BIT) ? 1 : 0;
3f3c82a5
FA
8121 int ns = (secstate & ARM_CP_SECSTATE_NS) ? 1 : 0;
8122
9c513e78 8123 r2->name = g_strdup(name);
3f3c82a5
FA
8124 /* Reset the secure state to the specific incoming state. This is
8125 * necessary as the register may have been defined with both states.
8126 */
8127 r2->secure = secstate;
8128
8129 if (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1]) {
8130 /* Register is banked (using both entries in array).
8131 * Overwriting fieldoffset as the array is only used to define
8132 * banked registers but later only fieldoffset is used.
f5a0a5a5 8133 */
3f3c82a5
FA
8134 r2->fieldoffset = r->bank_fieldoffsets[ns];
8135 }
8136
8137 if (state == ARM_CP_STATE_AA32) {
8138 if (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1]) {
8139 /* If the register is banked then we don't need to migrate or
8140 * reset the 32-bit instance in certain cases:
8141 *
8142 * 1) If the register has both 32-bit and 64-bit instances then we
8143 * can count on the 64-bit instance taking care of the
8144 * non-secure bank.
8145 * 2) If ARMv8 is enabled then we can count on a 64-bit version
8146 * taking care of the secure bank. This requires that separate
8147 * 32 and 64-bit definitions are provided.
8148 */
8149 if ((r->state == ARM_CP_STATE_BOTH && ns) ||
8150 (arm_feature(&cpu->env, ARM_FEATURE_V8) && !ns)) {
7a0e58fa 8151 r2->type |= ARM_CP_ALIAS;
3f3c82a5
FA
8152 }
8153 } else if ((secstate != r->secure) && !ns) {
8154 /* The register is not banked so we only want to allow migration of
8155 * the non-secure instance.
8156 */
7a0e58fa 8157 r2->type |= ARM_CP_ALIAS;
58a1d8ce 8158 }
3f3c82a5
FA
8159
8160 if (r->state == ARM_CP_STATE_BOTH) {
8161 /* We assume it is a cp15 register if the .cp field is left unset.
8162 */
8163 if (r2->cp == 0) {
8164 r2->cp = 15;
8165 }
8166
f5a0a5a5 8167#ifdef HOST_WORDS_BIGENDIAN
3f3c82a5
FA
8168 if (r2->fieldoffset) {
8169 r2->fieldoffset += sizeof(uint32_t);
8170 }
f5a0a5a5 8171#endif
3f3c82a5 8172 }
f5a0a5a5
PM
8173 }
8174 if (state == ARM_CP_STATE_AA64) {
8175 /* To allow abbreviation of ARMCPRegInfo
8176 * definitions, we treat cp == 0 as equivalent to
8177 * the value for "standard guest-visible sysreg".
58a1d8ce
PM
8178 * STATE_BOTH definitions are also always "standard
8179 * sysreg" in their AArch64 view (the .cp value may
8180 * be non-zero for the benefit of the AArch32 view).
f5a0a5a5 8181 */
58a1d8ce 8182 if (r->cp == 0 || r->state == ARM_CP_STATE_BOTH) {
f5a0a5a5
PM
8183 r2->cp = CP_REG_ARM64_SYSREG_CP;
8184 }
8185 *key = ENCODE_AA64_CP_REG(r2->cp, r2->crn, crm,
8186 r2->opc0, opc1, opc2);
8187 } else {
51a79b03 8188 *key = ENCODE_CP_REG(r2->cp, is64, ns, r2->crn, crm, opc1, opc2);
f5a0a5a5 8189 }
6e6efd61
PM
8190 if (opaque) {
8191 r2->opaque = opaque;
8192 }
67ed771d
PM
8193 /* reginfo passed to helpers is correct for the actual access,
8194 * and is never ARM_CP_STATE_BOTH:
8195 */
8196 r2->state = state;
6e6efd61
PM
8197 /* Make sure reginfo passed to helpers for wildcarded regs
8198 * has the correct crm/opc1/opc2 for this reg, not CP_ANY:
8199 */
8200 r2->crm = crm;
8201 r2->opc1 = opc1;
8202 r2->opc2 = opc2;
8203 /* By convention, for wildcarded registers only the first
8204 * entry is used for migration; the others are marked as
7a0e58fa 8205 * ALIAS so we don't try to transfer the register
6e6efd61 8206 * multiple times. Special registers (ie NOP/WFI) are
7a0e58fa 8207 * never migratable and not even raw-accessible.
6e6efd61 8208 */
7a0e58fa
PM
8209 if ((r->type & ARM_CP_SPECIAL)) {
8210 r2->type |= ARM_CP_NO_RAW;
8211 }
8212 if (((r->crm == CP_ANY) && crm != 0) ||
6e6efd61
PM
8213 ((r->opc1 == CP_ANY) && opc1 != 0) ||
8214 ((r->opc2 == CP_ANY) && opc2 != 0)) {
1f163787 8215 r2->type |= ARM_CP_ALIAS | ARM_CP_NO_GDB;
6e6efd61
PM
8216 }
8217
375421cc
PM
8218 /* Check that raw accesses are either forbidden or handled. Note that
8219 * we can't assert this earlier because the setup of fieldoffset for
8220 * banked registers has to be done first.
8221 */
8222 if (!(r2->type & ARM_CP_NO_RAW)) {
8223 assert(!raw_accessors_invalid(r2));
8224 }
8225
6e6efd61
PM
8226 /* Overriding of an existing definition must be explicitly
8227 * requested.
8228 */
8229 if (!(r->type & ARM_CP_OVERRIDE)) {
8230 ARMCPRegInfo *oldreg;
8231 oldreg = g_hash_table_lookup(cpu->cp_regs, key);
8232 if (oldreg && !(oldreg->type & ARM_CP_OVERRIDE)) {
8233 fprintf(stderr, "Register redefined: cp=%d %d bit "
8234 "crn=%d crm=%d opc1=%d opc2=%d, "
8235 "was %s, now %s\n", r2->cp, 32 + 32 * is64,
8236 r2->crn, r2->crm, r2->opc1, r2->opc2,
8237 oldreg->name, r2->name);
8238 g_assert_not_reached();
8239 }
8240 }
8241 g_hash_table_insert(cpu->cp_regs, key, r2);
8242}
8243
8244
4b6a83fb
PM
8245void define_one_arm_cp_reg_with_opaque(ARMCPU *cpu,
8246 const ARMCPRegInfo *r, void *opaque)
8247{
8248 /* Define implementations of coprocessor registers.
8249 * We store these in a hashtable because typically
8250 * there are less than 150 registers in a space which
8251 * is 16*16*16*8*8 = 262144 in size.
8252 * Wildcarding is supported for the crm, opc1 and opc2 fields.
8253 * If a register is defined twice then the second definition is
8254 * used, so this can be used to define some generic registers and
8255 * then override them with implementation specific variations.
8256 * At least one of the original and the second definition should
8257 * include ARM_CP_OVERRIDE in its type bits -- this is just a guard
8258 * against accidental use.
f5a0a5a5
PM
8259 *
8260 * The state field defines whether the register is to be
8261 * visible in the AArch32 or AArch64 execution state. If the
8262 * state is set to ARM_CP_STATE_BOTH then we synthesise a
8263 * reginfo structure for the AArch32 view, which sees the lower
8264 * 32 bits of the 64 bit register.
8265 *
8266 * Only registers visible in AArch64 may set r->opc0; opc0 cannot
8267 * be wildcarded. AArch64 registers are always considered to be 64
8268 * bits; the ARM_CP_64BIT* flag applies only to the AArch32 view of
8269 * the register, if any.
4b6a83fb 8270 */
f5a0a5a5 8271 int crm, opc1, opc2, state;
4b6a83fb
PM
8272 int crmmin = (r->crm == CP_ANY) ? 0 : r->crm;
8273 int crmmax = (r->crm == CP_ANY) ? 15 : r->crm;
8274 int opc1min = (r->opc1 == CP_ANY) ? 0 : r->opc1;
8275 int opc1max = (r->opc1 == CP_ANY) ? 7 : r->opc1;
8276 int opc2min = (r->opc2 == CP_ANY) ? 0 : r->opc2;
8277 int opc2max = (r->opc2 == CP_ANY) ? 7 : r->opc2;
8278 /* 64 bit registers have only CRm and Opc1 fields */
8279 assert(!((r->type & ARM_CP_64BIT) && (r->opc2 || r->crn)));
f5a0a5a5
PM
8280 /* op0 only exists in the AArch64 encodings */
8281 assert((r->state != ARM_CP_STATE_AA32) || (r->opc0 == 0));
8282 /* AArch64 regs are all 64 bit so ARM_CP_64BIT is meaningless */
8283 assert((r->state != ARM_CP_STATE_AA64) || !(r->type & ARM_CP_64BIT));
8284 /* The AArch64 pseudocode CheckSystemAccess() specifies that op1
8285 * encodes a minimum access level for the register. We roll this
8286 * runtime check into our general permission check code, so check
8287 * here that the reginfo's specified permissions are strict enough
8288 * to encompass the generic architectural permission check.
8289 */
8290 if (r->state != ARM_CP_STATE_AA32) {
8291 int mask = 0;
8292 switch (r->opc1) {
b5bd7440
AB
8293 case 0:
8294 /* min_EL EL1, but some accessible to EL0 via kernel ABI */
8295 mask = PL0U_R | PL1_RW;
8296 break;
8297 case 1: case 2:
f5a0a5a5
PM
8298 /* min_EL EL1 */
8299 mask = PL1_RW;
8300 break;
8301 case 3:
8302 /* min_EL EL0 */
8303 mask = PL0_RW;
8304 break;
8305 case 4:
b4ecf60f 8306 case 5:
f5a0a5a5
PM
8307 /* min_EL EL2 */
8308 mask = PL2_RW;
8309 break;
f5a0a5a5
PM
8310 case 6:
8311 /* min_EL EL3 */
8312 mask = PL3_RW;
8313 break;
8314 case 7:
8315 /* min_EL EL1, secure mode only (we don't check the latter) */
8316 mask = PL1_RW;
8317 break;
8318 default:
8319 /* broken reginfo with out-of-range opc1 */
8320 assert(false);
8321 break;
8322 }
8323 /* assert our permissions are not too lax (stricter is fine) */
8324 assert((r->access & ~mask) == 0);
8325 }
8326
4b6a83fb
PM
8327 /* Check that the register definition has enough info to handle
8328 * reads and writes if they are permitted.
8329 */
8330 if (!(r->type & (ARM_CP_SPECIAL|ARM_CP_CONST))) {
8331 if (r->access & PL3_R) {
3f3c82a5
FA
8332 assert((r->fieldoffset ||
8333 (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
8334 r->readfn);
4b6a83fb
PM
8335 }
8336 if (r->access & PL3_W) {
3f3c82a5
FA
8337 assert((r->fieldoffset ||
8338 (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
8339 r->writefn);
4b6a83fb
PM
8340 }
8341 }
8342 /* Bad type field probably means missing sentinel at end of reg list */
8343 assert(cptype_valid(r->type));
8344 for (crm = crmmin; crm <= crmmax; crm++) {
8345 for (opc1 = opc1min; opc1 <= opc1max; opc1++) {
8346 for (opc2 = opc2min; opc2 <= opc2max; opc2++) {
f5a0a5a5
PM
8347 for (state = ARM_CP_STATE_AA32;
8348 state <= ARM_CP_STATE_AA64; state++) {
8349 if (r->state != state && r->state != ARM_CP_STATE_BOTH) {
8350 continue;
8351 }
3f3c82a5
FA
8352 if (state == ARM_CP_STATE_AA32) {
8353 /* Under AArch32 CP registers can be common
8354 * (same for secure and non-secure world) or banked.
8355 */
9c513e78
AB
8356 char *name;
8357
3f3c82a5
FA
8358 switch (r->secure) {
8359 case ARM_CP_SECSTATE_S:
8360 case ARM_CP_SECSTATE_NS:
8361 add_cpreg_to_hashtable(cpu, r, opaque, state,
9c513e78
AB
8362 r->secure, crm, opc1, opc2,
8363 r->name);
3f3c82a5
FA
8364 break;
8365 default:
9c513e78 8366 name = g_strdup_printf("%s_S", r->name);
3f3c82a5
FA
8367 add_cpreg_to_hashtable(cpu, r, opaque, state,
8368 ARM_CP_SECSTATE_S,
9c513e78
AB
8369 crm, opc1, opc2, name);
8370 g_free(name);
3f3c82a5
FA
8371 add_cpreg_to_hashtable(cpu, r, opaque, state,
8372 ARM_CP_SECSTATE_NS,
9c513e78 8373 crm, opc1, opc2, r->name);
3f3c82a5
FA
8374 break;
8375 }
8376 } else {
8377 /* AArch64 registers get mapped to non-secure instance
8378 * of AArch32 */
8379 add_cpreg_to_hashtable(cpu, r, opaque, state,
8380 ARM_CP_SECSTATE_NS,
9c513e78 8381 crm, opc1, opc2, r->name);
3f3c82a5 8382 }
f5a0a5a5 8383 }
4b6a83fb
PM
8384 }
8385 }
8386 }
8387}
8388
8389void define_arm_cp_regs_with_opaque(ARMCPU *cpu,
8390 const ARMCPRegInfo *regs, void *opaque)
8391{
8392 /* Define a whole list of registers */
8393 const ARMCPRegInfo *r;
8394 for (r = regs; r->type != ARM_CP_SENTINEL; r++) {
8395 define_one_arm_cp_reg_with_opaque(cpu, r, opaque);
8396 }
8397}
8398
6c5c0fec
AB
8399/*
8400 * Modify ARMCPRegInfo for access from userspace.
8401 *
8402 * This is a data driven modification directed by
8403 * ARMCPRegUserSpaceInfo. All registers become ARM_CP_CONST as
8404 * user-space cannot alter any values and dynamic values pertaining to
8405 * execution state are hidden from user space view anyway.
8406 */
8407void modify_arm_cp_regs(ARMCPRegInfo *regs, const ARMCPRegUserSpaceInfo *mods)
8408{
8409 const ARMCPRegUserSpaceInfo *m;
8410 ARMCPRegInfo *r;
8411
8412 for (m = mods; m->name; m++) {
d040242e
AB
8413 GPatternSpec *pat = NULL;
8414 if (m->is_glob) {
8415 pat = g_pattern_spec_new(m->name);
8416 }
6c5c0fec 8417 for (r = regs; r->type != ARM_CP_SENTINEL; r++) {
d040242e
AB
8418 if (pat && g_pattern_match_string(pat, r->name)) {
8419 r->type = ARM_CP_CONST;
8420 r->access = PL0U_R;
8421 r->resetvalue = 0;
8422 /* continue */
8423 } else if (strcmp(r->name, m->name) == 0) {
6c5c0fec
AB
8424 r->type = ARM_CP_CONST;
8425 r->access = PL0U_R;
8426 r->resetvalue &= m->exported_bits;
8427 r->resetvalue |= m->fixed_bits;
8428 break;
8429 }
8430 }
d040242e
AB
8431 if (pat) {
8432 g_pattern_spec_free(pat);
8433 }
6c5c0fec
AB
8434 }
8435}
8436
60322b39 8437const ARMCPRegInfo *get_arm_cp_reginfo(GHashTable *cpregs, uint32_t encoded_cp)
4b6a83fb 8438{
60322b39 8439 return g_hash_table_lookup(cpregs, &encoded_cp);
4b6a83fb
PM
8440}
8441
c4241c7d
PM
8442void arm_cp_write_ignore(CPUARMState *env, const ARMCPRegInfo *ri,
8443 uint64_t value)
4b6a83fb
PM
8444{
8445 /* Helper coprocessor write function for write-ignore registers */
4b6a83fb
PM
8446}
8447
c4241c7d 8448uint64_t arm_cp_read_zero(CPUARMState *env, const ARMCPRegInfo *ri)
4b6a83fb
PM
8449{
8450 /* Helper coprocessor write function for read-as-zero registers */
4b6a83fb
PM
8451 return 0;
8452}
8453
f5a0a5a5
PM
8454void arm_cp_reset_ignore(CPUARMState *env, const ARMCPRegInfo *opaque)
8455{
8456 /* Helper coprocessor reset function for do-nothing-on-reset registers */
8457}
8458
af393ffc 8459static int bad_mode_switch(CPUARMState *env, int mode, CPSRWriteType write_type)
37064a8b
PM
8460{
8461 /* Return true if it is not valid for us to switch to
8462 * this CPU mode (ie all the UNPREDICTABLE cases in
8463 * the ARM ARM CPSRWriteByInstr pseudocode).
8464 */
af393ffc
PM
8465
8466 /* Changes to or from Hyp via MSR and CPS are illegal. */
8467 if (write_type == CPSRWriteByInstr &&
8468 ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_HYP ||
8469 mode == ARM_CPU_MODE_HYP)) {
8470 return 1;
8471 }
8472
37064a8b
PM
8473 switch (mode) {
8474 case ARM_CPU_MODE_USR:
10eacda7 8475 return 0;
37064a8b
PM
8476 case ARM_CPU_MODE_SYS:
8477 case ARM_CPU_MODE_SVC:
8478 case ARM_CPU_MODE_ABT:
8479 case ARM_CPU_MODE_UND:
8480 case ARM_CPU_MODE_IRQ:
8481 case ARM_CPU_MODE_FIQ:
52ff951b
PM
8482 /* Note that we don't implement the IMPDEF NSACR.RFR which in v7
8483 * allows FIQ mode to be Secure-only. (In v8 this doesn't exist.)
8484 */
10eacda7
PM
8485 /* If HCR.TGE is set then changes from Monitor to NS PL1 via MSR
8486 * and CPS are treated as illegal mode changes.
8487 */
8488 if (write_type == CPSRWriteByInstr &&
10eacda7 8489 (env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON &&
7c208e0f 8490 (arm_hcr_el2_eff(env) & HCR_TGE)) {
10eacda7
PM
8491 return 1;
8492 }
37064a8b 8493 return 0;
e6c8fc07
PM
8494 case ARM_CPU_MODE_HYP:
8495 return !arm_feature(env, ARM_FEATURE_EL2)
2d2a4549 8496 || arm_current_el(env) < 2 || arm_is_secure_below_el3(env);
027fc527 8497 case ARM_CPU_MODE_MON:
58ae2d1f 8498 return arm_current_el(env) < 3;
37064a8b
PM
8499 default:
8500 return 1;
8501 }
8502}
8503
2f4a40e5
AZ
8504uint32_t cpsr_read(CPUARMState *env)
8505{
8506 int ZF;
6fbe23d5
PB
8507 ZF = (env->ZF == 0);
8508 return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) |
2f4a40e5
AZ
8509 (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27)
8510 | (env->thumb << 5) | ((env->condexec_bits & 3) << 25)
8511 | ((env->condexec_bits & 0xfc) << 8)
af519934 8512 | (env->GE << 16) | (env->daif & CPSR_AIF);
2f4a40e5
AZ
8513}
8514
50866ba5
PM
8515void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask,
8516 CPSRWriteType write_type)
2f4a40e5 8517{
6e8801f9
FA
8518 uint32_t changed_daif;
8519
2f4a40e5 8520 if (mask & CPSR_NZCV) {
6fbe23d5
PB
8521 env->ZF = (~val) & CPSR_Z;
8522 env->NF = val;
2f4a40e5
AZ
8523 env->CF = (val >> 29) & 1;
8524 env->VF = (val << 3) & 0x80000000;
8525 }
8526 if (mask & CPSR_Q)
8527 env->QF = ((val & CPSR_Q) != 0);
8528 if (mask & CPSR_T)
8529 env->thumb = ((val & CPSR_T) != 0);
8530 if (mask & CPSR_IT_0_1) {
8531 env->condexec_bits &= ~3;
8532 env->condexec_bits |= (val >> 25) & 3;
8533 }
8534 if (mask & CPSR_IT_2_7) {
8535 env->condexec_bits &= 3;
8536 env->condexec_bits |= (val >> 8) & 0xfc;
8537 }
8538 if (mask & CPSR_GE) {
8539 env->GE = (val >> 16) & 0xf;
8540 }
8541
6e8801f9
FA
8542 /* In a V7 implementation that includes the security extensions but does
8543 * not include Virtualization Extensions the SCR.FW and SCR.AW bits control
8544 * whether non-secure software is allowed to change the CPSR_F and CPSR_A
8545 * bits respectively.
8546 *
8547 * In a V8 implementation, it is permitted for privileged software to
8548 * change the CPSR A/F bits regardless of the SCR.AW/FW bits.
8549 */
f8c88bbc 8550 if (write_type != CPSRWriteRaw && !arm_feature(env, ARM_FEATURE_V8) &&
6e8801f9
FA
8551 arm_feature(env, ARM_FEATURE_EL3) &&
8552 !arm_feature(env, ARM_FEATURE_EL2) &&
8553 !arm_is_secure(env)) {
8554
8555 changed_daif = (env->daif ^ val) & mask;
8556
8557 if (changed_daif & CPSR_A) {
8558 /* Check to see if we are allowed to change the masking of async
8559 * abort exceptions from a non-secure state.
8560 */
8561 if (!(env->cp15.scr_el3 & SCR_AW)) {
8562 qemu_log_mask(LOG_GUEST_ERROR,
8563 "Ignoring attempt to switch CPSR_A flag from "
8564 "non-secure world with SCR.AW bit clear\n");
8565 mask &= ~CPSR_A;
8566 }
8567 }
8568
8569 if (changed_daif & CPSR_F) {
8570 /* Check to see if we are allowed to change the masking of FIQ
8571 * exceptions from a non-secure state.
8572 */
8573 if (!(env->cp15.scr_el3 & SCR_FW)) {
8574 qemu_log_mask(LOG_GUEST_ERROR,
8575 "Ignoring attempt to switch CPSR_F flag from "
8576 "non-secure world with SCR.FW bit clear\n");
8577 mask &= ~CPSR_F;
8578 }
8579
8580 /* Check whether non-maskable FIQ (NMFI) support is enabled.
8581 * If this bit is set software is not allowed to mask
8582 * FIQs, but is allowed to set CPSR_F to 0.
8583 */
8584 if ((A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_NMFI) &&
8585 (val & CPSR_F)) {
8586 qemu_log_mask(LOG_GUEST_ERROR,
8587 "Ignoring attempt to enable CPSR_F flag "
8588 "(non-maskable FIQ [NMFI] support enabled)\n");
8589 mask &= ~CPSR_F;
8590 }
8591 }
8592 }
8593
4cc35614
PM
8594 env->daif &= ~(CPSR_AIF & mask);
8595 env->daif |= val & CPSR_AIF & mask;
8596
f8c88bbc
PM
8597 if (write_type != CPSRWriteRaw &&
8598 ((env->uncached_cpsr ^ val) & mask & CPSR_M)) {
8c4f0eb9
PM
8599 if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_USR) {
8600 /* Note that we can only get here in USR mode if this is a
8601 * gdb stub write; for this case we follow the architectural
8602 * behaviour for guest writes in USR mode of ignoring an attempt
8603 * to switch mode. (Those are caught by translate.c for writes
8604 * triggered by guest instructions.)
8605 */
8606 mask &= ~CPSR_M;
8607 } else if (bad_mode_switch(env, val & CPSR_M, write_type)) {
81907a58
PM
8608 /* Attempt to switch to an invalid mode: this is UNPREDICTABLE in
8609 * v7, and has defined behaviour in v8:
8610 * + leave CPSR.M untouched
8611 * + allow changes to the other CPSR fields
8612 * + set PSTATE.IL
8613 * For user changes via the GDB stub, we don't set PSTATE.IL,
8614 * as this would be unnecessarily harsh for a user error.
37064a8b
PM
8615 */
8616 mask &= ~CPSR_M;
81907a58
PM
8617 if (write_type != CPSRWriteByGDBStub &&
8618 arm_feature(env, ARM_FEATURE_V8)) {
8619 mask |= CPSR_IL;
8620 val |= CPSR_IL;
8621 }
81e37284
PM
8622 qemu_log_mask(LOG_GUEST_ERROR,
8623 "Illegal AArch32 mode switch attempt from %s to %s\n",
8624 aarch32_mode_name(env->uncached_cpsr),
8625 aarch32_mode_name(val));
37064a8b 8626 } else {
81e37284
PM
8627 qemu_log_mask(CPU_LOG_INT, "%s %s to %s PC 0x%" PRIx32 "\n",
8628 write_type == CPSRWriteExceptionReturn ?
8629 "Exception return from AArch32" :
8630 "AArch32 mode switch from",
8631 aarch32_mode_name(env->uncached_cpsr),
8632 aarch32_mode_name(val), env->regs[15]);
37064a8b
PM
8633 switch_mode(env, val & CPSR_M);
8634 }
2f4a40e5
AZ
8635 }
8636 mask &= ~CACHED_CPSR_BITS;
8637 env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask);
8638}
8639
b26eefb6
PB
8640/* Sign/zero extend */
8641uint32_t HELPER(sxtb16)(uint32_t x)
8642{
8643 uint32_t res;
8644 res = (uint16_t)(int8_t)x;
8645 res |= (uint32_t)(int8_t)(x >> 16) << 16;
8646 return res;
8647}
8648
8649uint32_t HELPER(uxtb16)(uint32_t x)
8650{
8651 uint32_t res;
8652 res = (uint16_t)(uint8_t)x;
8653 res |= (uint32_t)(uint8_t)(x >> 16) << 16;
8654 return res;
8655}
8656
3670669c
PB
8657int32_t HELPER(sdiv)(int32_t num, int32_t den)
8658{
8659 if (den == 0)
8660 return 0;
686eeb93
AJ
8661 if (num == INT_MIN && den == -1)
8662 return INT_MIN;
3670669c
PB
8663 return num / den;
8664}
8665
8666uint32_t HELPER(udiv)(uint32_t num, uint32_t den)
8667{
8668 if (den == 0)
8669 return 0;
8670 return num / den;
8671}
8672
8673uint32_t HELPER(rbit)(uint32_t x)
8674{
42fedbca 8675 return revbit32(x);
3670669c
PB
8676}
8677
c47eaf9f 8678#ifdef CONFIG_USER_ONLY
b5ff1b31 8679
affdb64d 8680static void switch_mode(CPUARMState *env, int mode)
b5ff1b31 8681{
2fc0cc0e 8682 ARMCPU *cpu = env_archcpu(env);
a47dddd7
AF
8683
8684 if (mode != ARM_CPU_MODE_USR) {
8685 cpu_abort(CPU(cpu), "Tried to switch out of user mode\n");
8686 }
b5ff1b31
FB
8687}
8688
012a906b
GB
8689uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
8690 uint32_t cur_el, bool secure)
9e729b57
EI
8691{
8692 return 1;
8693}
8694
ce02049d
GB
8695void aarch64_sync_64_to_32(CPUARMState *env)
8696{
8697 g_assert_not_reached();
8698}
8699
b5ff1b31
FB
8700#else
8701
affdb64d 8702static void switch_mode(CPUARMState *env, int mode)
b5ff1b31
FB
8703{
8704 int old_mode;
8705 int i;
8706
8707 old_mode = env->uncached_cpsr & CPSR_M;
8708 if (mode == old_mode)
8709 return;
8710
8711 if (old_mode == ARM_CPU_MODE_FIQ) {
8712 memcpy (env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t));
8637c67f 8713 memcpy (env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t));
b5ff1b31
FB
8714 } else if (mode == ARM_CPU_MODE_FIQ) {
8715 memcpy (env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t));
8637c67f 8716 memcpy (env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t));
b5ff1b31
FB
8717 }
8718
f5206413 8719 i = bank_number(old_mode);
b5ff1b31 8720 env->banked_r13[i] = env->regs[13];
b5ff1b31
FB
8721 env->banked_spsr[i] = env->spsr;
8722
f5206413 8723 i = bank_number(mode);
b5ff1b31 8724 env->regs[13] = env->banked_r13[i];
b5ff1b31 8725 env->spsr = env->banked_spsr[i];
593cfa2b
PM
8726
8727 env->banked_r14[r14_bank_number(old_mode)] = env->regs[14];
8728 env->regs[14] = env->banked_r14[r14_bank_number(mode)];
b5ff1b31
FB
8729}
8730
0eeb17d6
GB
8731/* Physical Interrupt Target EL Lookup Table
8732 *
8733 * [ From ARM ARM section G1.13.4 (Table G1-15) ]
8734 *
8735 * The below multi-dimensional table is used for looking up the target
8736 * exception level given numerous condition criteria. Specifically, the
8737 * target EL is based on SCR and HCR routing controls as well as the
8738 * currently executing EL and secure state.
8739 *
8740 * Dimensions:
8741 * target_el_table[2][2][2][2][2][4]
8742 * | | | | | +--- Current EL
8743 * | | | | +------ Non-secure(0)/Secure(1)
8744 * | | | +--------- HCR mask override
8745 * | | +------------ SCR exec state control
8746 * | +--------------- SCR mask override
8747 * +------------------ 32-bit(0)/64-bit(1) EL3
8748 *
8749 * The table values are as such:
8750 * 0-3 = EL0-EL3
8751 * -1 = Cannot occur
8752 *
8753 * The ARM ARM target EL table includes entries indicating that an "exception
8754 * is not taken". The two cases where this is applicable are:
8755 * 1) An exception is taken from EL3 but the SCR does not have the exception
8756 * routed to EL3.
8757 * 2) An exception is taken from EL2 but the HCR does not have the exception
8758 * routed to EL2.
8759 * In these two cases, the below table contain a target of EL1. This value is
8760 * returned as it is expected that the consumer of the table data will check
8761 * for "target EL >= current EL" to ensure the exception is not taken.
8762 *
8763 * SCR HCR
8764 * 64 EA AMO From
8765 * BIT IRQ IMO Non-secure Secure
8766 * EL3 FIQ RW FMO EL0 EL1 EL2 EL3 EL0 EL1 EL2 EL3
8767 */
82c39f6a 8768static const int8_t target_el_table[2][2][2][2][2][4] = {
0eeb17d6
GB
8769 {{{{/* 0 0 0 0 */{ 1, 1, 2, -1 },{ 3, -1, -1, 3 },},
8770 {/* 0 0 0 1 */{ 2, 2, 2, -1 },{ 3, -1, -1, 3 },},},
8771 {{/* 0 0 1 0 */{ 1, 1, 2, -1 },{ 3, -1, -1, 3 },},
8772 {/* 0 0 1 1 */{ 2, 2, 2, -1 },{ 3, -1, -1, 3 },},},},
8773 {{{/* 0 1 0 0 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},
8774 {/* 0 1 0 1 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},},
8775 {{/* 0 1 1 0 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},
8776 {/* 0 1 1 1 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},},},},
8777 {{{{/* 1 0 0 0 */{ 1, 1, 2, -1 },{ 1, 1, -1, 1 },},
8778 {/* 1 0 0 1 */{ 2, 2, 2, -1 },{ 1, 1, -1, 1 },},},
8779 {{/* 1 0 1 0 */{ 1, 1, 1, -1 },{ 1, 1, -1, 1 },},
8780 {/* 1 0 1 1 */{ 2, 2, 2, -1 },{ 1, 1, -1, 1 },},},},
8781 {{{/* 1 1 0 0 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},
8782 {/* 1 1 0 1 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},},
8783 {{/* 1 1 1 0 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},
8784 {/* 1 1 1 1 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},},},},
8785};
8786
8787/*
8788 * Determine the target EL for physical exceptions
8789 */
012a906b
GB
8790uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
8791 uint32_t cur_el, bool secure)
0eeb17d6
GB
8792{
8793 CPUARMState *env = cs->env_ptr;
f7778444
RH
8794 bool rw;
8795 bool scr;
8796 bool hcr;
0eeb17d6 8797 int target_el;
2cde031f 8798 /* Is the highest EL AArch64? */
f7778444
RH
8799 bool is64 = arm_feature(env, ARM_FEATURE_AARCH64);
8800 uint64_t hcr_el2;
2cde031f
SS
8801
8802 if (arm_feature(env, ARM_FEATURE_EL3)) {
8803 rw = ((env->cp15.scr_el3 & SCR_RW) == SCR_RW);
8804 } else {
8805 /* Either EL2 is the highest EL (and so the EL2 register width
8806 * is given by is64); or there is no EL2 or EL3, in which case
8807 * the value of 'rw' does not affect the table lookup anyway.
8808 */
8809 rw = is64;
8810 }
0eeb17d6 8811
f7778444 8812 hcr_el2 = arm_hcr_el2_eff(env);
0eeb17d6
GB
8813 switch (excp_idx) {
8814 case EXCP_IRQ:
8815 scr = ((env->cp15.scr_el3 & SCR_IRQ) == SCR_IRQ);
f7778444 8816 hcr = hcr_el2 & HCR_IMO;
0eeb17d6
GB
8817 break;
8818 case EXCP_FIQ:
8819 scr = ((env->cp15.scr_el3 & SCR_FIQ) == SCR_FIQ);
f7778444 8820 hcr = hcr_el2 & HCR_FMO;
0eeb17d6
GB
8821 break;
8822 default:
8823 scr = ((env->cp15.scr_el3 & SCR_EA) == SCR_EA);
f7778444 8824 hcr = hcr_el2 & HCR_AMO;
0eeb17d6
GB
8825 break;
8826 };
8827
d1b31428
RH
8828 /*
8829 * For these purposes, TGE and AMO/IMO/FMO both force the
8830 * interrupt to EL2. Fold TGE into the bit extracted above.
8831 */
8832 hcr |= (hcr_el2 & HCR_TGE) != 0;
8833
0eeb17d6
GB
8834 /* Perform a table-lookup for the target EL given the current state */
8835 target_el = target_el_table[is64][scr][rw][hcr][secure][cur_el];
8836
8837 assert(target_el > 0);
8838
8839 return target_el;
8840}
8841
b59f479b
PMD
8842void arm_log_exception(int idx)
8843{
8844 if (qemu_loglevel_mask(CPU_LOG_INT)) {
8845 const char *exc = NULL;
8846 static const char * const excnames[] = {
8847 [EXCP_UDEF] = "Undefined Instruction",
8848 [EXCP_SWI] = "SVC",
8849 [EXCP_PREFETCH_ABORT] = "Prefetch Abort",
8850 [EXCP_DATA_ABORT] = "Data Abort",
8851 [EXCP_IRQ] = "IRQ",
8852 [EXCP_FIQ] = "FIQ",
8853 [EXCP_BKPT] = "Breakpoint",
8854 [EXCP_EXCEPTION_EXIT] = "QEMU v7M exception exit",
8855 [EXCP_KERNEL_TRAP] = "QEMU intercept of kernel commpage",
8856 [EXCP_HVC] = "Hypervisor Call",
8857 [EXCP_HYP_TRAP] = "Hypervisor Trap",
8858 [EXCP_SMC] = "Secure Monitor Call",
8859 [EXCP_VIRQ] = "Virtual IRQ",
8860 [EXCP_VFIQ] = "Virtual FIQ",
8861 [EXCP_SEMIHOST] = "Semihosting call",
8862 [EXCP_NOCP] = "v7M NOCP UsageFault",
8863 [EXCP_INVSTATE] = "v7M INVSTATE UsageFault",
8864 [EXCP_STKOF] = "v8M STKOF UsageFault",
8865 [EXCP_LAZYFP] = "v7M exception during lazy FP stacking",
8866 [EXCP_LSERR] = "v8M LSERR UsageFault",
8867 [EXCP_UNALIGNED] = "v7M UNALIGNED UsageFault",
8868 };
8869
8870 if (idx >= 0 && idx < ARRAY_SIZE(excnames)) {
8871 exc = excnames[idx];
8872 }
8873 if (!exc) {
8874 exc = "unknown";
8875 }
8876 qemu_log_mask(CPU_LOG_INT, "Taking exception %d [%s]\n", idx, exc);
8877 }
8878}
8879
a356dacf 8880/*
7aab5a8c
PMD
8881 * Function used to synchronize QEMU's AArch64 register set with AArch32
8882 * register set. This is necessary when switching between AArch32 and AArch64
8883 * execution state.
a356dacf 8884 */
7aab5a8c 8885void aarch64_sync_32_to_64(CPUARMState *env)
9ee6e8bb 8886{
7aab5a8c
PMD
8887 int i;
8888 uint32_t mode = env->uncached_cpsr & CPSR_M;
8889
8890 /* We can blanket copy R[0:7] to X[0:7] */
8891 for (i = 0; i < 8; i++) {
8892 env->xregs[i] = env->regs[i];
fd592d89 8893 }
70d74660 8894
9a223097 8895 /*
7aab5a8c
PMD
8896 * Unless we are in FIQ mode, x8-x12 come from the user registers r8-r12.
8897 * Otherwise, they come from the banked user regs.
fd592d89 8898 */
7aab5a8c
PMD
8899 if (mode == ARM_CPU_MODE_FIQ) {
8900 for (i = 8; i < 13; i++) {
8901 env->xregs[i] = env->usr_regs[i - 8];
8902 }
8903 } else {
8904 for (i = 8; i < 13; i++) {
8905 env->xregs[i] = env->regs[i];
8906 }
fd592d89 8907 }
9ee6e8bb 8908
7aab5a8c
PMD
8909 /*
8910 * Registers x13-x23 are the various mode SP and FP registers. Registers
8911 * r13 and r14 are only copied if we are in that mode, otherwise we copy
8912 * from the mode banked register.
8913 */
8914 if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
8915 env->xregs[13] = env->regs[13];
8916 env->xregs[14] = env->regs[14];
8917 } else {
8918 env->xregs[13] = env->banked_r13[bank_number(ARM_CPU_MODE_USR)];
8919 /* HYP is an exception in that it is copied from r14 */
8920 if (mode == ARM_CPU_MODE_HYP) {
8921 env->xregs[14] = env->regs[14];
95695eff 8922 } else {
7aab5a8c 8923 env->xregs[14] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)];
95695eff 8924 }
95695eff
PM
8925 }
8926
7aab5a8c
PMD
8927 if (mode == ARM_CPU_MODE_HYP) {
8928 env->xregs[15] = env->regs[13];
8929 } else {
8930 env->xregs[15] = env->banked_r13[bank_number(ARM_CPU_MODE_HYP)];
95695eff
PM
8931 }
8932
7aab5a8c
PMD
8933 if (mode == ARM_CPU_MODE_IRQ) {
8934 env->xregs[16] = env->regs[14];
8935 env->xregs[17] = env->regs[13];
8936 } else {
8937 env->xregs[16] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)];
8938 env->xregs[17] = env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)];
8939 }
95695eff 8940
7aab5a8c
PMD
8941 if (mode == ARM_CPU_MODE_SVC) {
8942 env->xregs[18] = env->regs[14];
8943 env->xregs[19] = env->regs[13];
8944 } else {
8945 env->xregs[18] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)];
8946 env->xregs[19] = env->banked_r13[bank_number(ARM_CPU_MODE_SVC)];
8947 }
95695eff 8948
7aab5a8c
PMD
8949 if (mode == ARM_CPU_MODE_ABT) {
8950 env->xregs[20] = env->regs[14];
8951 env->xregs[21] = env->regs[13];
8952 } else {
8953 env->xregs[20] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)];
8954 env->xregs[21] = env->banked_r13[bank_number(ARM_CPU_MODE_ABT)];
8955 }
e33cf0f8 8956
7aab5a8c
PMD
8957 if (mode == ARM_CPU_MODE_UND) {
8958 env->xregs[22] = env->regs[14];
8959 env->xregs[23] = env->regs[13];
8960 } else {
8961 env->xregs[22] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)];
8962 env->xregs[23] = env->banked_r13[bank_number(ARM_CPU_MODE_UND)];
e33cf0f8
PM
8963 }
8964
8965 /*
7aab5a8c
PMD
8966 * Registers x24-x30 are mapped to r8-r14 in FIQ mode. If we are in FIQ
8967 * mode, then we can copy from r8-r14. Otherwise, we copy from the
8968 * FIQ bank for r8-r14.
e33cf0f8 8969 */
7aab5a8c
PMD
8970 if (mode == ARM_CPU_MODE_FIQ) {
8971 for (i = 24; i < 31; i++) {
8972 env->xregs[i] = env->regs[i - 16]; /* X[24:30] <- R[8:14] */
8973 }
8974 } else {
8975 for (i = 24; i < 29; i++) {
8976 env->xregs[i] = env->fiq_regs[i - 24];
e33cf0f8 8977 }
7aab5a8c
PMD
8978 env->xregs[29] = env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)];
8979 env->xregs[30] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)];
e33cf0f8 8980 }
7aab5a8c
PMD
8981
8982 env->pc = env->regs[15];
e33cf0f8
PM
8983}
8984
9a223097 8985/*
7aab5a8c
PMD
8986 * Function used to synchronize QEMU's AArch32 register set with AArch64
8987 * register set. This is necessary when switching between AArch32 and AArch64
8988 * execution state.
de2db7ec 8989 */
7aab5a8c 8990void aarch64_sync_64_to_32(CPUARMState *env)
9ee6e8bb 8991{
7aab5a8c
PMD
8992 int i;
8993 uint32_t mode = env->uncached_cpsr & CPSR_M;
abc24d86 8994
7aab5a8c
PMD
8995 /* We can blanket copy X[0:7] to R[0:7] */
8996 for (i = 0; i < 8; i++) {
8997 env->regs[i] = env->xregs[i];
de2db7ec 8998 }
3f0cddee 8999
9a223097 9000 /*
7aab5a8c
PMD
9001 * Unless we are in FIQ mode, r8-r12 come from the user registers x8-x12.
9002 * Otherwise, we copy x8-x12 into the banked user regs.
de2db7ec 9003 */
7aab5a8c
PMD
9004 if (mode == ARM_CPU_MODE_FIQ) {
9005 for (i = 8; i < 13; i++) {
9006 env->usr_regs[i - 8] = env->xregs[i];
9007 }
9008 } else {
9009 for (i = 8; i < 13; i++) {
9010 env->regs[i] = env->xregs[i];
9011 }
fb602cb7
PM
9012 }
9013
9a223097 9014 /*
7aab5a8c
PMD
9015 * Registers r13 & r14 depend on the current mode.
9016 * If we are in a given mode, we copy the corresponding x registers to r13
9017 * and r14. Otherwise, we copy the x register to the banked r13 and r14
9018 * for the mode.
fb602cb7 9019 */
7aab5a8c
PMD
9020 if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
9021 env->regs[13] = env->xregs[13];
9022 env->regs[14] = env->xregs[14];
fb602cb7 9023 } else {
7aab5a8c 9024 env->banked_r13[bank_number(ARM_CPU_MODE_USR)] = env->xregs[13];
fb602cb7 9025
7aab5a8c
PMD
9026 /*
9027 * HYP is an exception in that it does not have its own banked r14 but
9028 * shares the USR r14
9029 */
9030 if (mode == ARM_CPU_MODE_HYP) {
9031 env->regs[14] = env->xregs[14];
9032 } else {
9033 env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)] = env->xregs[14];
9034 }
9035 }
fb602cb7 9036
7aab5a8c
PMD
9037 if (mode == ARM_CPU_MODE_HYP) {
9038 env->regs[13] = env->xregs[15];
fb602cb7 9039 } else {
7aab5a8c 9040 env->banked_r13[bank_number(ARM_CPU_MODE_HYP)] = env->xregs[15];
fb602cb7 9041 }
d02a8698 9042
7aab5a8c
PMD
9043 if (mode == ARM_CPU_MODE_IRQ) {
9044 env->regs[14] = env->xregs[16];
9045 env->regs[13] = env->xregs[17];
d02a8698 9046 } else {
7aab5a8c
PMD
9047 env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[16];
9048 env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[17];
d02a8698
PM
9049 }
9050
7aab5a8c
PMD
9051 if (mode == ARM_CPU_MODE_SVC) {
9052 env->regs[14] = env->xregs[18];
9053 env->regs[13] = env->xregs[19];
9054 } else {
9055 env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)] = env->xregs[18];
9056 env->banked_r13[bank_number(ARM_CPU_MODE_SVC)] = env->xregs[19];
fb602cb7
PM
9057 }
9058
7aab5a8c
PMD
9059 if (mode == ARM_CPU_MODE_ABT) {
9060 env->regs[14] = env->xregs[20];
9061 env->regs[13] = env->xregs[21];
9062 } else {
9063 env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)] = env->xregs[20];
9064 env->banked_r13[bank_number(ARM_CPU_MODE_ABT)] = env->xregs[21];
ce02049d
GB
9065 }
9066
9067 if (mode == ARM_CPU_MODE_UND) {
3a9148d0
SS
9068 env->regs[14] = env->xregs[22];
9069 env->regs[13] = env->xregs[23];
ce02049d 9070 } else {
593cfa2b 9071 env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)] = env->xregs[22];
3a9148d0 9072 env->banked_r13[bank_number(ARM_CPU_MODE_UND)] = env->xregs[23];
ce02049d
GB
9073 }
9074
9075 /* Registers x24-x30 are mapped to r8-r14 in FIQ mode. If we are in FIQ
9076 * mode, then we can copy to r8-r14. Otherwise, we copy to the
9077 * FIQ bank for r8-r14.
9078 */
9079 if (mode == ARM_CPU_MODE_FIQ) {
9080 for (i = 24; i < 31; i++) {
9081 env->regs[i - 16] = env->xregs[i]; /* X[24:30] -> R[8:14] */
9082 }
9083 } else {
9084 for (i = 24; i < 29; i++) {
9085 env->fiq_regs[i - 24] = env->xregs[i];
9086 }
9087 env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[29];
593cfa2b 9088 env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[30];
ce02049d
GB
9089 }
9090
9091 env->regs[15] = env->pc;
9092}
9093
dea8378b
PM
9094static void take_aarch32_exception(CPUARMState *env, int new_mode,
9095 uint32_t mask, uint32_t offset,
9096 uint32_t newpc)
9097{
4a2696c0
RH
9098 int new_el;
9099
dea8378b
PM
9100 /* Change the CPU state so as to actually take the exception. */
9101 switch_mode(env, new_mode);
4a2696c0 9102
dea8378b
PM
9103 /*
9104 * For exceptions taken to AArch32 we must clear the SS bit in both
9105 * PSTATE and in the old-state value we save to SPSR_<mode>, so zero it now.
9106 */
9107 env->uncached_cpsr &= ~PSTATE_SS;
9108 env->spsr = cpsr_read(env);
9109 /* Clear IT bits. */
9110 env->condexec_bits = 0;
9111 /* Switch to the new mode, and to the correct instruction set. */
9112 env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode;
88828bf1
CD
9113
9114 /* This must be after mode switching. */
9115 new_el = arm_current_el(env);
9116
dea8378b
PM
9117 /* Set new mode endianness */
9118 env->uncached_cpsr &= ~CPSR_E;
4a2696c0 9119 if (env->cp15.sctlr_el[new_el] & SCTLR_EE) {
dea8378b
PM
9120 env->uncached_cpsr |= CPSR_E;
9121 }
829f9fd3
PM
9122 /* J and IL must always be cleared for exception entry */
9123 env->uncached_cpsr &= ~(CPSR_IL | CPSR_J);
dea8378b
PM
9124 env->daif |= mask;
9125
9126 if (new_mode == ARM_CPU_MODE_HYP) {
9127 env->thumb = (env->cp15.sctlr_el[2] & SCTLR_TE) != 0;
9128 env->elr_el[2] = env->regs[15];
9129 } else {
4a2696c0 9130 /* CPSR.PAN is normally preserved preserved unless... */
f8af1143 9131 if (cpu_isar_feature(aa32_pan, env_archcpu(env))) {
4a2696c0
RH
9132 switch (new_el) {
9133 case 3:
9134 if (!arm_is_secure_below_el3(env)) {
9135 /* ... the target is EL3, from non-secure state. */
9136 env->uncached_cpsr &= ~CPSR_PAN;
9137 break;
9138 }
9139 /* ... the target is EL3, from secure state ... */
9140 /* fall through */
9141 case 1:
9142 /* ... the target is EL1 and SCTLR.SPAN is 0. */
9143 if (!(env->cp15.sctlr_el[new_el] & SCTLR_SPAN)) {
9144 env->uncached_cpsr |= CPSR_PAN;
9145 }
9146 break;
9147 }
9148 }
dea8378b
PM
9149 /*
9150 * this is a lie, as there was no c1_sys on V4T/V5, but who cares
9151 * and we should just guard the thumb mode on V4
9152 */
9153 if (arm_feature(env, ARM_FEATURE_V4T)) {
9154 env->thumb =
9155 (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_TE) != 0;
9156 }
9157 env->regs[14] = env->regs[15] + offset;
9158 }
9159 env->regs[15] = newpc;
a8a79c7a 9160 arm_rebuild_hflags(env);
dea8378b
PM
9161}
9162
b9bc21ff
PM
9163static void arm_cpu_do_interrupt_aarch32_hyp(CPUState *cs)
9164{
9165 /*
9166 * Handle exception entry to Hyp mode; this is sufficiently
9167 * different to entry to other AArch32 modes that we handle it
9168 * separately here.
9169 *
9170 * The vector table entry used is always the 0x14 Hyp mode entry point,
9171 * unless this is an UNDEF/HVC/abort taken from Hyp to Hyp.
9172 * The offset applied to the preferred return address is always zero
9173 * (see DDI0487C.a section G1.12.3).
9174 * PSTATE A/I/F masks are set based only on the SCR.EA/IRQ/FIQ values.
9175 */
9176 uint32_t addr, mask;
9177 ARMCPU *cpu = ARM_CPU(cs);
9178 CPUARMState *env = &cpu->env;
9179
9180 switch (cs->exception_index) {
9181 case EXCP_UDEF:
9182 addr = 0x04;
9183 break;
9184 case EXCP_SWI:
9185 addr = 0x14;
9186 break;
9187 case EXCP_BKPT:
9188 /* Fall through to prefetch abort. */
9189 case EXCP_PREFETCH_ABORT:
9190 env->cp15.ifar_s = env->exception.vaddress;
9191 qemu_log_mask(CPU_LOG_INT, "...with HIFAR 0x%x\n",
9192 (uint32_t)env->exception.vaddress);
9193 addr = 0x0c;
9194 break;
9195 case EXCP_DATA_ABORT:
9196 env->cp15.dfar_s = env->exception.vaddress;
9197 qemu_log_mask(CPU_LOG_INT, "...with HDFAR 0x%x\n",
9198 (uint32_t)env->exception.vaddress);
9199 addr = 0x10;
9200 break;
9201 case EXCP_IRQ:
9202 addr = 0x18;
9203 break;
9204 case EXCP_FIQ:
9205 addr = 0x1c;
9206 break;
9207 case EXCP_HVC:
9208 addr = 0x08;
9209 break;
9210 case EXCP_HYP_TRAP:
9211 addr = 0x14;
9bbb4ef9 9212 break;
b9bc21ff
PM
9213 default:
9214 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
9215 }
9216
9217 if (cs->exception_index != EXCP_IRQ && cs->exception_index != EXCP_FIQ) {
2ed08180
PM
9218 if (!arm_feature(env, ARM_FEATURE_V8)) {
9219 /*
9220 * QEMU syndrome values are v8-style. v7 has the IL bit
9221 * UNK/SBZP for "field not valid" cases, where v8 uses RES1.
9222 * If this is a v7 CPU, squash the IL bit in those cases.
9223 */
9224 if (cs->exception_index == EXCP_PREFETCH_ABORT ||
9225 (cs->exception_index == EXCP_DATA_ABORT &&
9226 !(env->exception.syndrome & ARM_EL_ISV)) ||
9227 syn_get_ec(env->exception.syndrome) == EC_UNCATEGORIZED) {
9228 env->exception.syndrome &= ~ARM_EL_IL;
9229 }
9230 }
b9bc21ff
PM
9231 env->cp15.esr_el[2] = env->exception.syndrome;
9232 }
9233
9234 if (arm_current_el(env) != 2 && addr < 0x14) {
9235 addr = 0x14;
9236 }
9237
9238 mask = 0;
9239 if (!(env->cp15.scr_el3 & SCR_EA)) {
9240 mask |= CPSR_A;
9241 }
9242 if (!(env->cp15.scr_el3 & SCR_IRQ)) {
9243 mask |= CPSR_I;
9244 }
9245 if (!(env->cp15.scr_el3 & SCR_FIQ)) {
9246 mask |= CPSR_F;
9247 }
9248
9249 addr += env->cp15.hvbar;
9250
9251 take_aarch32_exception(env, ARM_CPU_MODE_HYP, mask, 0, addr);
9252}
9253
966f758c 9254static void arm_cpu_do_interrupt_aarch32(CPUState *cs)
b5ff1b31 9255{
97a8ea5a
AF
9256 ARMCPU *cpu = ARM_CPU(cs);
9257 CPUARMState *env = &cpu->env;
b5ff1b31
FB
9258 uint32_t addr;
9259 uint32_t mask;
9260 int new_mode;
9261 uint32_t offset;
16a906fd 9262 uint32_t moe;
b5ff1b31 9263
16a906fd 9264 /* If this is a debug exception we must update the DBGDSCR.MOE bits */
64b91e3f 9265 switch (syn_get_ec(env->exception.syndrome)) {
16a906fd
PM
9266 case EC_BREAKPOINT:
9267 case EC_BREAKPOINT_SAME_EL:
9268 moe = 1;
9269 break;
9270 case EC_WATCHPOINT:
9271 case EC_WATCHPOINT_SAME_EL:
9272 moe = 10;
9273 break;
9274 case EC_AA32_BKPT:
9275 moe = 3;
9276 break;
9277 case EC_VECTORCATCH:
9278 moe = 5;
9279 break;
9280 default:
9281 moe = 0;
9282 break;
9283 }
9284
9285 if (moe) {
9286 env->cp15.mdscr_el1 = deposit64(env->cp15.mdscr_el1, 2, 4, moe);
9287 }
9288
b9bc21ff
PM
9289 if (env->exception.target_el == 2) {
9290 arm_cpu_do_interrupt_aarch32_hyp(cs);
9291 return;
9292 }
9293
27103424 9294 switch (cs->exception_index) {
b5ff1b31
FB
9295 case EXCP_UDEF:
9296 new_mode = ARM_CPU_MODE_UND;
9297 addr = 0x04;
9298 mask = CPSR_I;
9299 if (env->thumb)
9300 offset = 2;
9301 else
9302 offset = 4;
9303 break;
9304 case EXCP_SWI:
9305 new_mode = ARM_CPU_MODE_SVC;
9306 addr = 0x08;
9307 mask = CPSR_I;
601d70b9 9308 /* The PC already points to the next instruction. */
b5ff1b31
FB
9309 offset = 0;
9310 break;
06c949e6 9311 case EXCP_BKPT:
9ee6e8bb
PB
9312 /* Fall through to prefetch abort. */
9313 case EXCP_PREFETCH_ABORT:
88ca1c2d 9314 A32_BANKED_CURRENT_REG_SET(env, ifsr, env->exception.fsr);
b848ce2b 9315 A32_BANKED_CURRENT_REG_SET(env, ifar, env->exception.vaddress);
3f1beaca 9316 qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x IFAR 0x%x\n",
88ca1c2d 9317 env->exception.fsr, (uint32_t)env->exception.vaddress);
b5ff1b31
FB
9318 new_mode = ARM_CPU_MODE_ABT;
9319 addr = 0x0c;
9320 mask = CPSR_A | CPSR_I;
9321 offset = 4;
9322 break;
9323 case EXCP_DATA_ABORT:
4a7e2d73 9324 A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr);
b848ce2b 9325 A32_BANKED_CURRENT_REG_SET(env, dfar, env->exception.vaddress);
3f1beaca 9326 qemu_log_mask(CPU_LOG_INT, "...with DFSR 0x%x DFAR 0x%x\n",
4a7e2d73 9327 env->exception.fsr,
6cd8a264 9328 (uint32_t)env->exception.vaddress);
b5ff1b31
FB
9329 new_mode = ARM_CPU_MODE_ABT;
9330 addr = 0x10;
9331 mask = CPSR_A | CPSR_I;
9332 offset = 8;
9333 break;
9334 case EXCP_IRQ:
9335 new_mode = ARM_CPU_MODE_IRQ;
9336 addr = 0x18;
9337 /* Disable IRQ and imprecise data aborts. */
9338 mask = CPSR_A | CPSR_I;
9339 offset = 4;
de38d23b
FA
9340 if (env->cp15.scr_el3 & SCR_IRQ) {
9341 /* IRQ routed to monitor mode */
9342 new_mode = ARM_CPU_MODE_MON;
9343 mask |= CPSR_F;
9344 }
b5ff1b31
FB
9345 break;
9346 case EXCP_FIQ:
9347 new_mode = ARM_CPU_MODE_FIQ;
9348 addr = 0x1c;
9349 /* Disable FIQ, IRQ and imprecise data aborts. */
9350 mask = CPSR_A | CPSR_I | CPSR_F;
de38d23b
FA
9351 if (env->cp15.scr_el3 & SCR_FIQ) {
9352 /* FIQ routed to monitor mode */
9353 new_mode = ARM_CPU_MODE_MON;
9354 }
b5ff1b31
FB
9355 offset = 4;
9356 break;
87a4b270
PM
9357 case EXCP_VIRQ:
9358 new_mode = ARM_CPU_MODE_IRQ;
9359 addr = 0x18;
9360 /* Disable IRQ and imprecise data aborts. */
9361 mask = CPSR_A | CPSR_I;
9362 offset = 4;
9363 break;
9364 case EXCP_VFIQ:
9365 new_mode = ARM_CPU_MODE_FIQ;
9366 addr = 0x1c;
9367 /* Disable FIQ, IRQ and imprecise data aborts. */
9368 mask = CPSR_A | CPSR_I | CPSR_F;
9369 offset = 4;
9370 break;
dbe9d163
FA
9371 case EXCP_SMC:
9372 new_mode = ARM_CPU_MODE_MON;
9373 addr = 0x08;
9374 mask = CPSR_A | CPSR_I | CPSR_F;
9375 offset = 0;
9376 break;
b5ff1b31 9377 default:
a47dddd7 9378 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
b5ff1b31
FB
9379 return; /* Never happens. Keep compiler happy. */
9380 }
e89e51a1
FA
9381
9382 if (new_mode == ARM_CPU_MODE_MON) {
9383 addr += env->cp15.mvbar;
137feaa9 9384 } else if (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_V) {
e89e51a1 9385 /* High vectors. When enabled, base address cannot be remapped. */
b5ff1b31 9386 addr += 0xffff0000;
8641136c
NR
9387 } else {
9388 /* ARM v7 architectures provide a vector base address register to remap
9389 * the interrupt vector table.
e89e51a1 9390 * This register is only followed in non-monitor mode, and is banked.
8641136c
NR
9391 * Note: only bits 31:5 are valid.
9392 */
fb6c91ba 9393 addr += A32_BANKED_CURRENT_REG_GET(env, vbar);
b5ff1b31 9394 }
dbe9d163
FA
9395
9396 if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON) {
9397 env->cp15.scr_el3 &= ~SCR_NS;
9398 }
9399
dea8378b 9400 take_aarch32_exception(env, new_mode, mask, offset, addr);
b5ff1b31
FB
9401}
9402
966f758c
PM
9403/* Handle exception entry to a target EL which is using AArch64 */
9404static void arm_cpu_do_interrupt_aarch64(CPUState *cs)
f3a9b694
PM
9405{
9406 ARMCPU *cpu = ARM_CPU(cs);
9407 CPUARMState *env = &cpu->env;
9408 unsigned int new_el = env->exception.target_el;
9409 target_ulong addr = env->cp15.vbar_el[new_el];
9410 unsigned int new_mode = aarch64_pstate_mode(new_el, true);
4a2696c0 9411 unsigned int old_mode;
0ab5953b
RH
9412 unsigned int cur_el = arm_current_el(env);
9413
9a05f7b6
RH
9414 /*
9415 * Note that new_el can never be 0. If cur_el is 0, then
9416 * el0_a64 is is_a64(), else el0_a64 is ignored.
9417 */
9418 aarch64_sve_change_el(env, cur_el, new_el, is_a64(env));
f3a9b694 9419
0ab5953b 9420 if (cur_el < new_el) {
3d6f7617
PM
9421 /* Entry vector offset depends on whether the implemented EL
9422 * immediately lower than the target level is using AArch32 or AArch64
9423 */
9424 bool is_aa64;
cb092fbb 9425 uint64_t hcr;
3d6f7617
PM
9426
9427 switch (new_el) {
9428 case 3:
9429 is_aa64 = (env->cp15.scr_el3 & SCR_RW) != 0;
9430 break;
9431 case 2:
cb092fbb
RH
9432 hcr = arm_hcr_el2_eff(env);
9433 if ((hcr & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
9434 is_aa64 = (hcr & HCR_RW) != 0;
9435 break;
9436 }
9437 /* fall through */
3d6f7617
PM
9438 case 1:
9439 is_aa64 = is_a64(env);
9440 break;
9441 default:
9442 g_assert_not_reached();
9443 }
9444
9445 if (is_aa64) {
f3a9b694
PM
9446 addr += 0x400;
9447 } else {
9448 addr += 0x600;
9449 }
9450 } else if (pstate_read(env) & PSTATE_SP) {
9451 addr += 0x200;
9452 }
9453
f3a9b694
PM
9454 switch (cs->exception_index) {
9455 case EXCP_PREFETCH_ABORT:
9456 case EXCP_DATA_ABORT:
9457 env->cp15.far_el[new_el] = env->exception.vaddress;
9458 qemu_log_mask(CPU_LOG_INT, "...with FAR 0x%" PRIx64 "\n",
9459 env->cp15.far_el[new_el]);
9460 /* fall through */
9461 case EXCP_BKPT:
9462 case EXCP_UDEF:
9463 case EXCP_SWI:
9464 case EXCP_HVC:
9465 case EXCP_HYP_TRAP:
9466 case EXCP_SMC:
4be42f40
PM
9467 if (syn_get_ec(env->exception.syndrome) == EC_ADVSIMDFPACCESSTRAP) {
9468 /*
9469 * QEMU internal FP/SIMD syndromes from AArch32 include the
9470 * TA and coproc fields which are only exposed if the exception
9471 * is taken to AArch32 Hyp mode. Mask them out to get a valid
9472 * AArch64 format syndrome.
9473 */
9474 env->exception.syndrome &= ~MAKE_64BIT_MASK(0, 20);
9475 }
f3a9b694
PM
9476 env->cp15.esr_el[new_el] = env->exception.syndrome;
9477 break;
9478 case EXCP_IRQ:
9479 case EXCP_VIRQ:
9480 addr += 0x80;
9481 break;
9482 case EXCP_FIQ:
9483 case EXCP_VFIQ:
9484 addr += 0x100;
9485 break;
f3a9b694
PM
9486 default:
9487 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
9488 }
9489
9490 if (is_a64(env)) {
4a2696c0 9491 old_mode = pstate_read(env);
f3a9b694
PM
9492 aarch64_save_sp(env, arm_current_el(env));
9493 env->elr_el[new_el] = env->pc;
9494 } else {
4a2696c0 9495 old_mode = cpsr_read(env);
f3a9b694
PM
9496 env->elr_el[new_el] = env->regs[15];
9497
9498 aarch64_sync_32_to_64(env);
9499
9500 env->condexec_bits = 0;
9501 }
4a2696c0
RH
9502 env->banked_spsr[aarch64_banked_spsr_index(new_el)] = old_mode;
9503
f3a9b694
PM
9504 qemu_log_mask(CPU_LOG_INT, "...with ELR 0x%" PRIx64 "\n",
9505 env->elr_el[new_el]);
9506
4a2696c0
RH
9507 if (cpu_isar_feature(aa64_pan, cpu)) {
9508 /* The value of PSTATE.PAN is normally preserved, except when ... */
9509 new_mode |= old_mode & PSTATE_PAN;
9510 switch (new_el) {
9511 case 2:
9512 /* ... the target is EL2 with HCR_EL2.{E2H,TGE} == '11' ... */
9513 if ((arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE))
9514 != (HCR_E2H | HCR_TGE)) {
9515 break;
9516 }
9517 /* fall through */
9518 case 1:
9519 /* ... the target is EL1 ... */
9520 /* ... and SCTLR_ELx.SPAN == 0, then set to 1. */
9521 if ((env->cp15.sctlr_el[new_el] & SCTLR_SPAN) == 0) {
9522 new_mode |= PSTATE_PAN;
9523 }
9524 break;
9525 }
9526 }
9527
f3a9b694
PM
9528 pstate_write(env, PSTATE_DAIF | new_mode);
9529 env->aarch64 = 1;
9530 aarch64_restore_sp(env, new_el);
a8a79c7a 9531 helper_rebuild_hflags_a64(env, new_el);
f3a9b694
PM
9532
9533 env->pc = addr;
9534
9535 qemu_log_mask(CPU_LOG_INT, "...to EL%d PC 0x%" PRIx64 " PSTATE 0x%x\n",
9536 new_el, env->pc, pstate_read(env));
966f758c
PM
9537}
9538
ed6e6ba9
AB
9539/*
9540 * Do semihosting call and set the appropriate return value. All the
9541 * permission and validity checks have been done at translate time.
9542 *
9543 * We only see semihosting exceptions in TCG only as they are not
9544 * trapped to the hypervisor in KVM.
9545 */
91f78c58 9546#ifdef CONFIG_TCG
ed6e6ba9
AB
9547static void handle_semihosting(CPUState *cs)
9548{
904c04de
PM
9549 ARMCPU *cpu = ARM_CPU(cs);
9550 CPUARMState *env = &cpu->env;
9551
9552 if (is_a64(env)) {
ed6e6ba9
AB
9553 qemu_log_mask(CPU_LOG_INT,
9554 "...handling as semihosting call 0x%" PRIx64 "\n",
9555 env->xregs[0]);
9556 env->xregs[0] = do_arm_semihosting(env);
4ff5ef9e 9557 env->pc += 4;
904c04de 9558 } else {
904c04de
PM
9559 qemu_log_mask(CPU_LOG_INT,
9560 "...handling as semihosting call 0x%x\n",
9561 env->regs[0]);
9562 env->regs[0] = do_arm_semihosting(env);
4ff5ef9e 9563 env->regs[15] += env->thumb ? 2 : 4;
904c04de
PM
9564 }
9565}
ed6e6ba9 9566#endif
904c04de 9567
966f758c
PM
9568/* Handle a CPU exception for A and R profile CPUs.
9569 * Do any appropriate logging, handle PSCI calls, and then hand off
9570 * to the AArch64-entry or AArch32-entry function depending on the
9571 * target exception level's register width.
9572 */
9573void arm_cpu_do_interrupt(CPUState *cs)
9574{
9575 ARMCPU *cpu = ARM_CPU(cs);
9576 CPUARMState *env = &cpu->env;
9577 unsigned int new_el = env->exception.target_el;
9578
531c60a9 9579 assert(!arm_feature(env, ARM_FEATURE_M));
966f758c
PM
9580
9581 arm_log_exception(cs->exception_index);
9582 qemu_log_mask(CPU_LOG_INT, "...from EL%d to EL%d\n", arm_current_el(env),
9583 new_el);
9584 if (qemu_loglevel_mask(CPU_LOG_INT)
9585 && !excp_is_internal(cs->exception_index)) {
6568da45 9586 qemu_log_mask(CPU_LOG_INT, "...with ESR 0x%x/0x%" PRIx32 "\n",
64b91e3f 9587 syn_get_ec(env->exception.syndrome),
966f758c
PM
9588 env->exception.syndrome);
9589 }
9590
9591 if (arm_is_psci_call(cpu, cs->exception_index)) {
9592 arm_handle_psci_call(cpu);
9593 qemu_log_mask(CPU_LOG_INT, "...handled as PSCI call\n");
9594 return;
9595 }
9596
ed6e6ba9
AB
9597 /*
9598 * Semihosting semantics depend on the register width of the code
9599 * that caused the exception, not the target exception level, so
9600 * must be handled here.
966f758c 9601 */
ed6e6ba9
AB
9602#ifdef CONFIG_TCG
9603 if (cs->exception_index == EXCP_SEMIHOST) {
9604 handle_semihosting(cs);
904c04de
PM
9605 return;
9606 }
ed6e6ba9 9607#endif
904c04de 9608
b5c53d1b
AL
9609 /* Hooks may change global state so BQL should be held, also the
9610 * BQL needs to be held for any modification of
9611 * cs->interrupt_request.
9612 */
9613 g_assert(qemu_mutex_iothread_locked());
9614
9615 arm_call_pre_el_change_hook(cpu);
9616
904c04de
PM
9617 assert(!excp_is_internal(cs->exception_index));
9618 if (arm_el_is_aa64(env, new_el)) {
966f758c
PM
9619 arm_cpu_do_interrupt_aarch64(cs);
9620 } else {
9621 arm_cpu_do_interrupt_aarch32(cs);
9622 }
f3a9b694 9623
bd7d00fc
PM
9624 arm_call_el_change_hook(cpu);
9625
f3a9b694
PM
9626 if (!kvm_enabled()) {
9627 cs->interrupt_request |= CPU_INTERRUPT_EXITTB;
9628 }
9629}
c47eaf9f 9630#endif /* !CONFIG_USER_ONLY */
0480f69a
PM
9631
9632/* Return the exception level which controls this address translation regime */
b9f6033c 9633static uint32_t regime_el(CPUARMState *env, ARMMMUIdx mmu_idx)
0480f69a
PM
9634{
9635 switch (mmu_idx) {
b9f6033c
RH
9636 case ARMMMUIdx_E20_0:
9637 case ARMMMUIdx_E20_2:
452ef8cb 9638 case ARMMMUIdx_E20_2_PAN:
97fa9350 9639 case ARMMMUIdx_Stage2:
e013b741 9640 case ARMMMUIdx_E2:
0480f69a 9641 return 2;
127b2b08 9642 case ARMMMUIdx_SE3:
0480f69a 9643 return 3;
fba37aed 9644 case ARMMMUIdx_SE10_0:
0480f69a 9645 return arm_el_is_aa64(env, 3) ? 1 : 3;
fba37aed 9646 case ARMMMUIdx_SE10_1:
452ef8cb 9647 case ARMMMUIdx_SE10_1_PAN:
2859d7b5
RH
9648 case ARMMMUIdx_Stage1_E0:
9649 case ARMMMUIdx_Stage1_E1:
452ef8cb 9650 case ARMMMUIdx_Stage1_E1_PAN:
b9f6033c
RH
9651 case ARMMMUIdx_E10_0:
9652 case ARMMMUIdx_E10_1:
452ef8cb 9653 case ARMMMUIdx_E10_1_PAN:
62593718
PM
9654 case ARMMMUIdx_MPrivNegPri:
9655 case ARMMMUIdx_MUserNegPri:
e7b921c2
PM
9656 case ARMMMUIdx_MPriv:
9657 case ARMMMUIdx_MUser:
62593718
PM
9658 case ARMMMUIdx_MSPrivNegPri:
9659 case ARMMMUIdx_MSUserNegPri:
66787c78 9660 case ARMMMUIdx_MSPriv:
66787c78 9661 case ARMMMUIdx_MSUser:
0480f69a
PM
9662 return 1;
9663 default:
9664 g_assert_not_reached();
9665 }
9666}
9667
aaec1432
RH
9668uint64_t arm_sctlr(CPUARMState *env, int el)
9669{
9670 /* Only EL0 needs to be adjusted for EL1&0 or EL2&0. */
9671 if (el == 0) {
9672 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, 0);
9673 el = (mmu_idx == ARMMMUIdx_E20_0 ? 2 : 1);
9674 }
9675 return env->cp15.sctlr_el[el];
9676}
c47eaf9f 9677
0480f69a 9678/* Return the SCTLR value which controls this address translation regime */
aaec1432 9679static inline uint64_t regime_sctlr(CPUARMState *env, ARMMMUIdx mmu_idx)
0480f69a
PM
9680{
9681 return env->cp15.sctlr_el[regime_el(env, mmu_idx)];
9682}
9683
aaec1432
RH
9684#ifndef CONFIG_USER_ONLY
9685
0480f69a
PM
9686/* Return true if the specified stage of address translation is disabled */
9687static inline bool regime_translation_disabled(CPUARMState *env,
9688 ARMMMUIdx mmu_idx)
9689{
29c483a5 9690 if (arm_feature(env, ARM_FEATURE_M)) {
ecf5e8ea 9691 switch (env->v7m.mpu_ctrl[regime_is_secure(env, mmu_idx)] &
3bef7012
PM
9692 (R_V7M_MPU_CTRL_ENABLE_MASK | R_V7M_MPU_CTRL_HFNMIENA_MASK)) {
9693 case R_V7M_MPU_CTRL_ENABLE_MASK:
9694 /* Enabled, but not for HardFault and NMI */
62593718 9695 return mmu_idx & ARM_MMU_IDX_M_NEGPRI;
3bef7012
PM
9696 case R_V7M_MPU_CTRL_ENABLE_MASK | R_V7M_MPU_CTRL_HFNMIENA_MASK:
9697 /* Enabled for all cases */
9698 return false;
9699 case 0:
9700 default:
9701 /* HFNMIENA set and ENABLE clear is UNPREDICTABLE, but
9702 * we warned about that in armv7m_nvic.c when the guest set it.
9703 */
9704 return true;
9705 }
29c483a5
MD
9706 }
9707
97fa9350 9708 if (mmu_idx == ARMMMUIdx_Stage2) {
9d1bab33
PM
9709 /* HCR.DC means HCR.VM behaves as 1 */
9710 return (env->cp15.hcr_el2 & (HCR_DC | HCR_VM)) == 0;
0480f69a 9711 }
3d0e3080
PM
9712
9713 if (env->cp15.hcr_el2 & HCR_TGE) {
9714 /* TGE means that NS EL0/1 act as if SCTLR_EL1.M is zero */
9715 if (!regime_is_secure(env, mmu_idx) && regime_el(env, mmu_idx) == 1) {
9716 return true;
9717 }
9718 }
9719
fee7aa46 9720 if ((env->cp15.hcr_el2 & HCR_DC) && arm_mmu_idx_is_stage1_of_2(mmu_idx)) {
9d1bab33
PM
9721 /* HCR.DC means SCTLR_EL1.M behaves as 0 */
9722 return true;
9723 }
9724
0480f69a
PM
9725 return (regime_sctlr(env, mmu_idx) & SCTLR_M) == 0;
9726}
9727
73462ddd
PC
9728static inline bool regime_translation_big_endian(CPUARMState *env,
9729 ARMMMUIdx mmu_idx)
9730{
9731 return (regime_sctlr(env, mmu_idx) & SCTLR_EE) != 0;
9732}
9733
c47eaf9f
PM
9734/* Return the TTBR associated with this translation regime */
9735static inline uint64_t regime_ttbr(CPUARMState *env, ARMMMUIdx mmu_idx,
9736 int ttbrn)
9737{
97fa9350 9738 if (mmu_idx == ARMMMUIdx_Stage2) {
c47eaf9f
PM
9739 return env->cp15.vttbr_el2;
9740 }
9741 if (ttbrn == 0) {
9742 return env->cp15.ttbr0_el[regime_el(env, mmu_idx)];
9743 } else {
9744 return env->cp15.ttbr1_el[regime_el(env, mmu_idx)];
9745 }
9746}
9747
9748#endif /* !CONFIG_USER_ONLY */
9749
0480f69a
PM
9750/* Return the TCR controlling this translation regime */
9751static inline TCR *regime_tcr(CPUARMState *env, ARMMMUIdx mmu_idx)
9752{
97fa9350 9753 if (mmu_idx == ARMMMUIdx_Stage2) {
68e9c2fe 9754 return &env->cp15.vtcr_el2;
0480f69a
PM
9755 }
9756 return &env->cp15.tcr_el[regime_el(env, mmu_idx)];
9757}
9758
8bd5c820
PM
9759/* Convert a possible stage1+2 MMU index into the appropriate
9760 * stage 1 MMU index
9761 */
9762static inline ARMMMUIdx stage_1_mmu_idx(ARMMMUIdx mmu_idx)
9763{
b9f6033c
RH
9764 switch (mmu_idx) {
9765 case ARMMMUIdx_E10_0:
9766 return ARMMMUIdx_Stage1_E0;
9767 case ARMMMUIdx_E10_1:
9768 return ARMMMUIdx_Stage1_E1;
452ef8cb
RH
9769 case ARMMMUIdx_E10_1_PAN:
9770 return ARMMMUIdx_Stage1_E1_PAN;
b9f6033c
RH
9771 default:
9772 return mmu_idx;
8bd5c820 9773 }
8bd5c820
PM
9774}
9775
0480f69a
PM
9776/* Return true if the translation regime is using LPAE format page tables */
9777static inline bool regime_using_lpae_format(CPUARMState *env,
9778 ARMMMUIdx mmu_idx)
9779{
9780 int el = regime_el(env, mmu_idx);
9781 if (el == 2 || arm_el_is_aa64(env, el)) {
9782 return true;
9783 }
9784 if (arm_feature(env, ARM_FEATURE_LPAE)
9785 && (regime_tcr(env, mmu_idx)->raw_tcr & TTBCR_EAE)) {
9786 return true;
9787 }
9788 return false;
9789}
9790
deb2db99
AR
9791/* Returns true if the stage 1 translation regime is using LPAE format page
9792 * tables. Used when raising alignment exceptions, whose FSR changes depending
9793 * on whether the long or short descriptor format is in use. */
9794bool arm_s1_regime_using_lpae_format(CPUARMState *env, ARMMMUIdx mmu_idx)
30901475 9795{
8bd5c820 9796 mmu_idx = stage_1_mmu_idx(mmu_idx);
deb2db99 9797
30901475
AB
9798 return regime_using_lpae_format(env, mmu_idx);
9799}
9800
c47eaf9f 9801#ifndef CONFIG_USER_ONLY
0480f69a
PM
9802static inline bool regime_is_user(CPUARMState *env, ARMMMUIdx mmu_idx)
9803{
9804 switch (mmu_idx) {
fba37aed 9805 case ARMMMUIdx_SE10_0:
b9f6033c 9806 case ARMMMUIdx_E20_0:
2859d7b5 9807 case ARMMMUIdx_Stage1_E0:
e7b921c2 9808 case ARMMMUIdx_MUser:
871bec7c 9809 case ARMMMUIdx_MSUser:
62593718
PM
9810 case ARMMMUIdx_MUserNegPri:
9811 case ARMMMUIdx_MSUserNegPri:
0480f69a
PM
9812 return true;
9813 default:
9814 return false;
01b98b68
RH
9815 case ARMMMUIdx_E10_0:
9816 case ARMMMUIdx_E10_1:
452ef8cb 9817 case ARMMMUIdx_E10_1_PAN:
0480f69a
PM
9818 g_assert_not_reached();
9819 }
9820}
9821
0fbf5238
AJ
9822/* Translate section/page access permissions to page
9823 * R/W protection flags
d76951b6
AJ
9824 *
9825 * @env: CPUARMState
9826 * @mmu_idx: MMU index indicating required translation regime
9827 * @ap: The 3-bit access permissions (AP[2:0])
9828 * @domain_prot: The 2-bit domain access permissions
0fbf5238
AJ
9829 */
9830static inline int ap_to_rw_prot(CPUARMState *env, ARMMMUIdx mmu_idx,
9831 int ap, int domain_prot)
9832{
554b0b09
PM
9833 bool is_user = regime_is_user(env, mmu_idx);
9834
9835 if (domain_prot == 3) {
9836 return PAGE_READ | PAGE_WRITE;
9837 }
9838
554b0b09
PM
9839 switch (ap) {
9840 case 0:
9841 if (arm_feature(env, ARM_FEATURE_V7)) {
9842 return 0;
9843 }
554b0b09
PM
9844 switch (regime_sctlr(env, mmu_idx) & (SCTLR_S | SCTLR_R)) {
9845 case SCTLR_S:
9846 return is_user ? 0 : PAGE_READ;
9847 case SCTLR_R:
9848 return PAGE_READ;
9849 default:
9850 return 0;
9851 }
9852 case 1:
9853 return is_user ? 0 : PAGE_READ | PAGE_WRITE;
9854 case 2:
87c3d486 9855 if (is_user) {
0fbf5238 9856 return PAGE_READ;
87c3d486 9857 } else {
554b0b09 9858 return PAGE_READ | PAGE_WRITE;
87c3d486 9859 }
554b0b09
PM
9860 case 3:
9861 return PAGE_READ | PAGE_WRITE;
9862 case 4: /* Reserved. */
9863 return 0;
9864 case 5:
0fbf5238 9865 return is_user ? 0 : PAGE_READ;
554b0b09 9866 case 6:
0fbf5238 9867 return PAGE_READ;
554b0b09 9868 case 7:
87c3d486 9869 if (!arm_feature(env, ARM_FEATURE_V6K)) {
554b0b09 9870 return 0;
87c3d486 9871 }
0fbf5238 9872 return PAGE_READ;
554b0b09 9873 default:
0fbf5238 9874 g_assert_not_reached();
554b0b09 9875 }
b5ff1b31
FB
9876}
9877
d76951b6
AJ
9878/* Translate section/page access permissions to page
9879 * R/W protection flags.
9880 *
d76951b6 9881 * @ap: The 2-bit simple AP (AP[2:1])
d8e052b3 9882 * @is_user: TRUE if accessing from PL0
d76951b6 9883 */
d8e052b3 9884static inline int simple_ap_to_rw_prot_is_user(int ap, bool is_user)
d76951b6 9885{
d76951b6
AJ
9886 switch (ap) {
9887 case 0:
9888 return is_user ? 0 : PAGE_READ | PAGE_WRITE;
9889 case 1:
9890 return PAGE_READ | PAGE_WRITE;
9891 case 2:
9892 return is_user ? 0 : PAGE_READ;
9893 case 3:
9894 return PAGE_READ;
9895 default:
9896 g_assert_not_reached();
9897 }
9898}
9899
d8e052b3
AJ
9900static inline int
9901simple_ap_to_rw_prot(CPUARMState *env, ARMMMUIdx mmu_idx, int ap)
9902{
9903 return simple_ap_to_rw_prot_is_user(ap, regime_is_user(env, mmu_idx));
9904}
9905
6ab1a5ee
EI
9906/* Translate S2 section/page access permissions to protection flags
9907 *
9908 * @env: CPUARMState
9909 * @s2ap: The 2-bit stage2 access permissions (S2AP)
9910 * @xn: XN (execute-never) bit
9911 */
9912static int get_S2prot(CPUARMState *env, int s2ap, int xn)
9913{
9914 int prot = 0;
9915
9916 if (s2ap & 1) {
9917 prot |= PAGE_READ;
9918 }
9919 if (s2ap & 2) {
9920 prot |= PAGE_WRITE;
9921 }
9922 if (!xn) {
dfda6837
SS
9923 if (arm_el_is_aa64(env, 2) || prot & PAGE_READ) {
9924 prot |= PAGE_EXEC;
9925 }
6ab1a5ee
EI
9926 }
9927 return prot;
9928}
9929
d8e052b3
AJ
9930/* Translate section/page access permissions to protection flags
9931 *
9932 * @env: CPUARMState
9933 * @mmu_idx: MMU index indicating required translation regime
9934 * @is_aa64: TRUE if AArch64
9935 * @ap: The 2-bit simple AP (AP[2:1])
9936 * @ns: NS (non-secure) bit
9937 * @xn: XN (execute-never) bit
9938 * @pxn: PXN (privileged execute-never) bit
9939 */
9940static int get_S1prot(CPUARMState *env, ARMMMUIdx mmu_idx, bool is_aa64,
9941 int ap, int ns, int xn, int pxn)
9942{
9943 bool is_user = regime_is_user(env, mmu_idx);
9944 int prot_rw, user_rw;
9945 bool have_wxn;
9946 int wxn = 0;
9947
97fa9350 9948 assert(mmu_idx != ARMMMUIdx_Stage2);
d8e052b3
AJ
9949
9950 user_rw = simple_ap_to_rw_prot_is_user(ap, true);
9951 if (is_user) {
9952 prot_rw = user_rw;
9953 } else {
81636b70 9954 if (user_rw && regime_is_pan(env, mmu_idx)) {
f4e1dbc5
PM
9955 /* PAN forbids data accesses but doesn't affect insn fetch */
9956 prot_rw = 0;
9957 } else {
9958 prot_rw = simple_ap_to_rw_prot_is_user(ap, false);
81636b70 9959 }
d8e052b3
AJ
9960 }
9961
9962 if (ns && arm_is_secure(env) && (env->cp15.scr_el3 & SCR_SIF)) {
9963 return prot_rw;
9964 }
9965
9966 /* TODO have_wxn should be replaced with
9967 * ARM_FEATURE_V8 || (ARM_FEATURE_V7 && ARM_FEATURE_EL2)
9968 * when ARM_FEATURE_EL2 starts getting set. For now we assume all LPAE
9969 * compatible processors have EL2, which is required for [U]WXN.
9970 */
9971 have_wxn = arm_feature(env, ARM_FEATURE_LPAE);
9972
9973 if (have_wxn) {
9974 wxn = regime_sctlr(env, mmu_idx) & SCTLR_WXN;
9975 }
9976
9977 if (is_aa64) {
339370b9
RH
9978 if (regime_has_2_ranges(mmu_idx) && !is_user) {
9979 xn = pxn || (user_rw & PAGE_WRITE);
d8e052b3
AJ
9980 }
9981 } else if (arm_feature(env, ARM_FEATURE_V7)) {
9982 switch (regime_el(env, mmu_idx)) {
9983 case 1:
9984 case 3:
9985 if (is_user) {
9986 xn = xn || !(user_rw & PAGE_READ);
9987 } else {
9988 int uwxn = 0;
9989 if (have_wxn) {
9990 uwxn = regime_sctlr(env, mmu_idx) & SCTLR_UWXN;
9991 }
9992 xn = xn || !(prot_rw & PAGE_READ) || pxn ||
9993 (uwxn && (user_rw & PAGE_WRITE));
9994 }
9995 break;
9996 case 2:
9997 break;
9998 }
9999 } else {
10000 xn = wxn = 0;
10001 }
10002
10003 if (xn || (wxn && (prot_rw & PAGE_WRITE))) {
10004 return prot_rw;
10005 }
10006 return prot_rw | PAGE_EXEC;
10007}
10008
0480f69a
PM
10009static bool get_level1_table_address(CPUARMState *env, ARMMMUIdx mmu_idx,
10010 uint32_t *table, uint32_t address)
b2fa1797 10011{
0480f69a 10012 /* Note that we can only get here for an AArch32 PL0/PL1 lookup */
0480f69a 10013 TCR *tcr = regime_tcr(env, mmu_idx);
11f136ee 10014
11f136ee
FA
10015 if (address & tcr->mask) {
10016 if (tcr->raw_tcr & TTBCR_PD1) {
e389be16
FA
10017 /* Translation table walk disabled for TTBR1 */
10018 return false;
10019 }
aef878be 10020 *table = regime_ttbr(env, mmu_idx, 1) & 0xffffc000;
e389be16 10021 } else {
11f136ee 10022 if (tcr->raw_tcr & TTBCR_PD0) {
e389be16
FA
10023 /* Translation table walk disabled for TTBR0 */
10024 return false;
10025 }
aef878be 10026 *table = regime_ttbr(env, mmu_idx, 0) & tcr->base_mask;
e389be16
FA
10027 }
10028 *table |= (address >> 18) & 0x3ffc;
10029 return true;
b2fa1797
PB
10030}
10031
37785977
EI
10032/* Translate a S1 pagetable walk through S2 if needed. */
10033static hwaddr S1_ptw_translate(CPUARMState *env, ARMMMUIdx mmu_idx,
10034 hwaddr addr, MemTxAttrs txattrs,
37785977
EI
10035 ARMMMUFaultInfo *fi)
10036{
fee7aa46 10037 if (arm_mmu_idx_is_stage1_of_2(mmu_idx) &&
97fa9350 10038 !regime_translation_disabled(env, ARMMMUIdx_Stage2)) {
37785977
EI
10039 target_ulong s2size;
10040 hwaddr s2pa;
10041 int s2prot;
10042 int ret;
eadb2feb
PM
10043 ARMCacheAttrs cacheattrs = {};
10044 ARMCacheAttrs *pcacheattrs = NULL;
10045
10046 if (env->cp15.hcr_el2 & HCR_PTW) {
10047 /*
10048 * PTW means we must fault if this S1 walk touches S2 Device
10049 * memory; otherwise we don't care about the attributes and can
10050 * save the S2 translation the effort of computing them.
10051 */
10052 pcacheattrs = &cacheattrs;
10053 }
37785977 10054
97fa9350 10055 ret = get_phys_addr_lpae(env, addr, 0, ARMMMUIdx_Stage2, &s2pa,
eadb2feb 10056 &txattrs, &s2prot, &s2size, fi, pcacheattrs);
37785977 10057 if (ret) {
3b39d734 10058 assert(fi->type != ARMFault_None);
37785977
EI
10059 fi->s2addr = addr;
10060 fi->stage2 = true;
10061 fi->s1ptw = true;
10062 return ~0;
10063 }
eadb2feb
PM
10064 if (pcacheattrs && (pcacheattrs->attrs & 0xf0) == 0) {
10065 /* Access was to Device memory: generate Permission fault */
10066 fi->type = ARMFault_Permission;
10067 fi->s2addr = addr;
10068 fi->stage2 = true;
10069 fi->s1ptw = true;
10070 return ~0;
10071 }
37785977
EI
10072 addr = s2pa;
10073 }
10074 return addr;
10075}
10076
14577270 10077/* All loads done in the course of a page table walk go through here. */
a614e698 10078static uint32_t arm_ldl_ptw(CPUState *cs, hwaddr addr, bool is_secure,
3795a6de 10079 ARMMMUIdx mmu_idx, ARMMMUFaultInfo *fi)
ebca90e4 10080{
a614e698
EI
10081 ARMCPU *cpu = ARM_CPU(cs);
10082 CPUARMState *env = &cpu->env;
ebca90e4 10083 MemTxAttrs attrs = {};
3b39d734 10084 MemTxResult result = MEMTX_OK;
5ce4ff65 10085 AddressSpace *as;
3b39d734 10086 uint32_t data;
ebca90e4
PM
10087
10088 attrs.secure = is_secure;
5ce4ff65 10089 as = arm_addressspace(cs, attrs);
3795a6de 10090 addr = S1_ptw_translate(env, mmu_idx, addr, attrs, fi);
a614e698
EI
10091 if (fi->s1ptw) {
10092 return 0;
10093 }
73462ddd 10094 if (regime_translation_big_endian(env, mmu_idx)) {
3b39d734 10095 data = address_space_ldl_be(as, addr, attrs, &result);
73462ddd 10096 } else {
3b39d734 10097 data = address_space_ldl_le(as, addr, attrs, &result);
73462ddd 10098 }
3b39d734
PM
10099 if (result == MEMTX_OK) {
10100 return data;
10101 }
10102 fi->type = ARMFault_SyncExternalOnWalk;
10103 fi->ea = arm_extabort_type(result);
10104 return 0;
ebca90e4
PM
10105}
10106
37785977 10107static uint64_t arm_ldq_ptw(CPUState *cs, hwaddr addr, bool is_secure,
3795a6de 10108 ARMMMUIdx mmu_idx, ARMMMUFaultInfo *fi)
ebca90e4 10109{
37785977
EI
10110 ARMCPU *cpu = ARM_CPU(cs);
10111 CPUARMState *env = &cpu->env;
ebca90e4 10112 MemTxAttrs attrs = {};
3b39d734 10113 MemTxResult result = MEMTX_OK;
5ce4ff65 10114 AddressSpace *as;
9aea1ea3 10115 uint64_t data;
ebca90e4
PM
10116
10117 attrs.secure = is_secure;
5ce4ff65 10118 as = arm_addressspace(cs, attrs);
3795a6de 10119 addr = S1_ptw_translate(env, mmu_idx, addr, attrs, fi);
37785977
EI
10120 if (fi->s1ptw) {
10121 return 0;
10122 }
73462ddd 10123 if (regime_translation_big_endian(env, mmu_idx)) {
3b39d734 10124 data = address_space_ldq_be(as, addr, attrs, &result);
73462ddd 10125 } else {
3b39d734
PM
10126 data = address_space_ldq_le(as, addr, attrs, &result);
10127 }
10128 if (result == MEMTX_OK) {
10129 return data;
73462ddd 10130 }
3b39d734
PM
10131 fi->type = ARMFault_SyncExternalOnWalk;
10132 fi->ea = arm_extabort_type(result);
10133 return 0;
ebca90e4
PM
10134}
10135
b7cc4e82 10136static bool get_phys_addr_v5(CPUARMState *env, uint32_t address,
03ae85f8 10137 MMUAccessType access_type, ARMMMUIdx mmu_idx,
b7cc4e82 10138 hwaddr *phys_ptr, int *prot,
f989983e 10139 target_ulong *page_size,
e14b5a23 10140 ARMMMUFaultInfo *fi)
b5ff1b31 10141{
2fc0cc0e 10142 CPUState *cs = env_cpu(env);
f989983e 10143 int level = 1;
b5ff1b31
FB
10144 uint32_t table;
10145 uint32_t desc;
10146 int type;
10147 int ap;
e389be16 10148 int domain = 0;
dd4ebc2e 10149 int domain_prot;
a8170e5e 10150 hwaddr phys_addr;
0480f69a 10151 uint32_t dacr;
b5ff1b31 10152
9ee6e8bb
PB
10153 /* Pagetable walk. */
10154 /* Lookup l1 descriptor. */
0480f69a 10155 if (!get_level1_table_address(env, mmu_idx, &table, address)) {
e389be16 10156 /* Section translation fault if page walk is disabled by PD0 or PD1 */
f989983e 10157 fi->type = ARMFault_Translation;
e389be16
FA
10158 goto do_fault;
10159 }
a614e698 10160 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
3795a6de 10161 mmu_idx, fi);
3b39d734
PM
10162 if (fi->type != ARMFault_None) {
10163 goto do_fault;
10164 }
9ee6e8bb 10165 type = (desc & 3);
dd4ebc2e 10166 domain = (desc >> 5) & 0x0f;
0480f69a
PM
10167 if (regime_el(env, mmu_idx) == 1) {
10168 dacr = env->cp15.dacr_ns;
10169 } else {
10170 dacr = env->cp15.dacr_s;
10171 }
10172 domain_prot = (dacr >> (domain * 2)) & 3;
9ee6e8bb 10173 if (type == 0) {
601d70b9 10174 /* Section translation fault. */
f989983e 10175 fi->type = ARMFault_Translation;
9ee6e8bb
PB
10176 goto do_fault;
10177 }
f989983e
PM
10178 if (type != 2) {
10179 level = 2;
10180 }
dd4ebc2e 10181 if (domain_prot == 0 || domain_prot == 2) {
f989983e 10182 fi->type = ARMFault_Domain;
9ee6e8bb
PB
10183 goto do_fault;
10184 }
10185 if (type == 2) {
10186 /* 1Mb section. */
10187 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
10188 ap = (desc >> 10) & 3;
d4c430a8 10189 *page_size = 1024 * 1024;
9ee6e8bb
PB
10190 } else {
10191 /* Lookup l2 entry. */
554b0b09
PM
10192 if (type == 1) {
10193 /* Coarse pagetable. */
10194 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
10195 } else {
10196 /* Fine pagetable. */
10197 table = (desc & 0xfffff000) | ((address >> 8) & 0xffc);
10198 }
a614e698 10199 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
3795a6de 10200 mmu_idx, fi);
3b39d734
PM
10201 if (fi->type != ARMFault_None) {
10202 goto do_fault;
10203 }
9ee6e8bb
PB
10204 switch (desc & 3) {
10205 case 0: /* Page translation fault. */
f989983e 10206 fi->type = ARMFault_Translation;
9ee6e8bb
PB
10207 goto do_fault;
10208 case 1: /* 64k page. */
10209 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
10210 ap = (desc >> (4 + ((address >> 13) & 6))) & 3;
d4c430a8 10211 *page_size = 0x10000;
ce819861 10212 break;
9ee6e8bb
PB
10213 case 2: /* 4k page. */
10214 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
c10f7fc3 10215 ap = (desc >> (4 + ((address >> 9) & 6))) & 3;
d4c430a8 10216 *page_size = 0x1000;
ce819861 10217 break;
fc1891c7 10218 case 3: /* 1k page, or ARMv6/XScale "extended small (4k) page" */
554b0b09 10219 if (type == 1) {
fc1891c7
PM
10220 /* ARMv6/XScale extended small page format */
10221 if (arm_feature(env, ARM_FEATURE_XSCALE)
10222 || arm_feature(env, ARM_FEATURE_V6)) {
554b0b09 10223 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
fc1891c7 10224 *page_size = 0x1000;
554b0b09 10225 } else {
fc1891c7
PM
10226 /* UNPREDICTABLE in ARMv5; we choose to take a
10227 * page translation fault.
10228 */
f989983e 10229 fi->type = ARMFault_Translation;
554b0b09
PM
10230 goto do_fault;
10231 }
10232 } else {
10233 phys_addr = (desc & 0xfffffc00) | (address & 0x3ff);
fc1891c7 10234 *page_size = 0x400;
554b0b09 10235 }
9ee6e8bb 10236 ap = (desc >> 4) & 3;
ce819861
PB
10237 break;
10238 default:
9ee6e8bb
PB
10239 /* Never happens, but compiler isn't smart enough to tell. */
10240 abort();
ce819861 10241 }
9ee6e8bb 10242 }
0fbf5238
AJ
10243 *prot = ap_to_rw_prot(env, mmu_idx, ap, domain_prot);
10244 *prot |= *prot ? PAGE_EXEC : 0;
10245 if (!(*prot & (1 << access_type))) {
9ee6e8bb 10246 /* Access permission fault. */
f989983e 10247 fi->type = ARMFault_Permission;
9ee6e8bb
PB
10248 goto do_fault;
10249 }
10250 *phys_ptr = phys_addr;
b7cc4e82 10251 return false;
9ee6e8bb 10252do_fault:
f989983e
PM
10253 fi->domain = domain;
10254 fi->level = level;
b7cc4e82 10255 return true;
9ee6e8bb
PB
10256}
10257
b7cc4e82 10258static bool get_phys_addr_v6(CPUARMState *env, uint32_t address,
03ae85f8 10259 MMUAccessType access_type, ARMMMUIdx mmu_idx,
b7cc4e82 10260 hwaddr *phys_ptr, MemTxAttrs *attrs, int *prot,
f06cf243 10261 target_ulong *page_size, ARMMMUFaultInfo *fi)
9ee6e8bb 10262{
2fc0cc0e 10263 CPUState *cs = env_cpu(env);
f06cf243 10264 int level = 1;
9ee6e8bb
PB
10265 uint32_t table;
10266 uint32_t desc;
10267 uint32_t xn;
de9b05b8 10268 uint32_t pxn = 0;
9ee6e8bb
PB
10269 int type;
10270 int ap;
de9b05b8 10271 int domain = 0;
dd4ebc2e 10272 int domain_prot;
a8170e5e 10273 hwaddr phys_addr;
0480f69a 10274 uint32_t dacr;
8bf5b6a9 10275 bool ns;
9ee6e8bb
PB
10276
10277 /* Pagetable walk. */
10278 /* Lookup l1 descriptor. */
0480f69a 10279 if (!get_level1_table_address(env, mmu_idx, &table, address)) {
e389be16 10280 /* Section translation fault if page walk is disabled by PD0 or PD1 */
f06cf243 10281 fi->type = ARMFault_Translation;
e389be16
FA
10282 goto do_fault;
10283 }
a614e698 10284 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
3795a6de 10285 mmu_idx, fi);
3b39d734
PM
10286 if (fi->type != ARMFault_None) {
10287 goto do_fault;
10288 }
9ee6e8bb 10289 type = (desc & 3);
de9b05b8
PM
10290 if (type == 0 || (type == 3 && !arm_feature(env, ARM_FEATURE_PXN))) {
10291 /* Section translation fault, or attempt to use the encoding
10292 * which is Reserved on implementations without PXN.
10293 */
f06cf243 10294 fi->type = ARMFault_Translation;
9ee6e8bb 10295 goto do_fault;
de9b05b8
PM
10296 }
10297 if ((type == 1) || !(desc & (1 << 18))) {
10298 /* Page or Section. */
dd4ebc2e 10299 domain = (desc >> 5) & 0x0f;
9ee6e8bb 10300 }
0480f69a
PM
10301 if (regime_el(env, mmu_idx) == 1) {
10302 dacr = env->cp15.dacr_ns;
10303 } else {
10304 dacr = env->cp15.dacr_s;
10305 }
f06cf243
PM
10306 if (type == 1) {
10307 level = 2;
10308 }
0480f69a 10309 domain_prot = (dacr >> (domain * 2)) & 3;
dd4ebc2e 10310 if (domain_prot == 0 || domain_prot == 2) {
f06cf243
PM
10311 /* Section or Page domain fault */
10312 fi->type = ARMFault_Domain;
9ee6e8bb
PB
10313 goto do_fault;
10314 }
de9b05b8 10315 if (type != 1) {
9ee6e8bb
PB
10316 if (desc & (1 << 18)) {
10317 /* Supersection. */
10318 phys_addr = (desc & 0xff000000) | (address & 0x00ffffff);
4e42a6ca
SF
10319 phys_addr |= (uint64_t)extract32(desc, 20, 4) << 32;
10320 phys_addr |= (uint64_t)extract32(desc, 5, 4) << 36;
d4c430a8 10321 *page_size = 0x1000000;
b5ff1b31 10322 } else {
9ee6e8bb
PB
10323 /* Section. */
10324 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
d4c430a8 10325 *page_size = 0x100000;
b5ff1b31 10326 }
9ee6e8bb
PB
10327 ap = ((desc >> 10) & 3) | ((desc >> 13) & 4);
10328 xn = desc & (1 << 4);
de9b05b8 10329 pxn = desc & 1;
8bf5b6a9 10330 ns = extract32(desc, 19, 1);
9ee6e8bb 10331 } else {
de9b05b8
PM
10332 if (arm_feature(env, ARM_FEATURE_PXN)) {
10333 pxn = (desc >> 2) & 1;
10334 }
8bf5b6a9 10335 ns = extract32(desc, 3, 1);
9ee6e8bb
PB
10336 /* Lookup l2 entry. */
10337 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
a614e698 10338 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
3795a6de 10339 mmu_idx, fi);
3b39d734
PM
10340 if (fi->type != ARMFault_None) {
10341 goto do_fault;
10342 }
9ee6e8bb
PB
10343 ap = ((desc >> 4) & 3) | ((desc >> 7) & 4);
10344 switch (desc & 3) {
10345 case 0: /* Page translation fault. */
f06cf243 10346 fi->type = ARMFault_Translation;
b5ff1b31 10347 goto do_fault;
9ee6e8bb
PB
10348 case 1: /* 64k page. */
10349 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
10350 xn = desc & (1 << 15);
d4c430a8 10351 *page_size = 0x10000;
9ee6e8bb
PB
10352 break;
10353 case 2: case 3: /* 4k page. */
10354 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
10355 xn = desc & 1;
d4c430a8 10356 *page_size = 0x1000;
9ee6e8bb
PB
10357 break;
10358 default:
10359 /* Never happens, but compiler isn't smart enough to tell. */
10360 abort();
b5ff1b31 10361 }
9ee6e8bb 10362 }
dd4ebc2e 10363 if (domain_prot == 3) {
c0034328
JR
10364 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
10365 } else {
0480f69a 10366 if (pxn && !regime_is_user(env, mmu_idx)) {
de9b05b8
PM
10367 xn = 1;
10368 }
f06cf243
PM
10369 if (xn && access_type == MMU_INST_FETCH) {
10370 fi->type = ARMFault_Permission;
c0034328 10371 goto do_fault;
f06cf243 10372 }
9ee6e8bb 10373
d76951b6
AJ
10374 if (arm_feature(env, ARM_FEATURE_V6K) &&
10375 (regime_sctlr(env, mmu_idx) & SCTLR_AFE)) {
10376 /* The simplified model uses AP[0] as an access control bit. */
10377 if ((ap & 1) == 0) {
10378 /* Access flag fault. */
f06cf243 10379 fi->type = ARMFault_AccessFlag;
d76951b6
AJ
10380 goto do_fault;
10381 }
10382 *prot = simple_ap_to_rw_prot(env, mmu_idx, ap >> 1);
10383 } else {
10384 *prot = ap_to_rw_prot(env, mmu_idx, ap, domain_prot);
c0034328 10385 }
0fbf5238
AJ
10386 if (*prot && !xn) {
10387 *prot |= PAGE_EXEC;
10388 }
10389 if (!(*prot & (1 << access_type))) {
c0034328 10390 /* Access permission fault. */
f06cf243 10391 fi->type = ARMFault_Permission;
c0034328
JR
10392 goto do_fault;
10393 }
3ad493fc 10394 }
8bf5b6a9
PM
10395 if (ns) {
10396 /* The NS bit will (as required by the architecture) have no effect if
10397 * the CPU doesn't support TZ or this is a non-secure translation
10398 * regime, because the attribute will already be non-secure.
10399 */
10400 attrs->secure = false;
10401 }
9ee6e8bb 10402 *phys_ptr = phys_addr;
b7cc4e82 10403 return false;
b5ff1b31 10404do_fault:
f06cf243
PM
10405 fi->domain = domain;
10406 fi->level = level;
b7cc4e82 10407 return true;
b5ff1b31
FB
10408}
10409
1853d5a9 10410/*
a0e966c9 10411 * check_s2_mmu_setup
1853d5a9
EI
10412 * @cpu: ARMCPU
10413 * @is_aa64: True if the translation regime is in AArch64 state
10414 * @startlevel: Suggested starting level
10415 * @inputsize: Bitsize of IPAs
10416 * @stride: Page-table stride (See the ARM ARM)
10417 *
a0e966c9
EI
10418 * Returns true if the suggested S2 translation parameters are OK and
10419 * false otherwise.
1853d5a9 10420 */
a0e966c9
EI
10421static bool check_s2_mmu_setup(ARMCPU *cpu, bool is_aa64, int level,
10422 int inputsize, int stride)
1853d5a9 10423{
98d68ec2
EI
10424 const int grainsize = stride + 3;
10425 int startsizecheck;
10426
1853d5a9
EI
10427 /* Negative levels are never allowed. */
10428 if (level < 0) {
10429 return false;
10430 }
10431
98d68ec2
EI
10432 startsizecheck = inputsize - ((3 - level) * stride + grainsize);
10433 if (startsizecheck < 1 || startsizecheck > stride + 4) {
10434 return false;
10435 }
10436
1853d5a9 10437 if (is_aa64) {
3526423e 10438 CPUARMState *env = &cpu->env;
1853d5a9
EI
10439 unsigned int pamax = arm_pamax(cpu);
10440
10441 switch (stride) {
10442 case 13: /* 64KB Pages. */
10443 if (level == 0 || (level == 1 && pamax <= 42)) {
10444 return false;
10445 }
10446 break;
10447 case 11: /* 16KB Pages. */
10448 if (level == 0 || (level == 1 && pamax <= 40)) {
10449 return false;
10450 }
10451 break;
10452 case 9: /* 4KB Pages. */
10453 if (level == 0 && pamax <= 42) {
10454 return false;
10455 }
10456 break;
10457 default:
10458 g_assert_not_reached();
10459 }
3526423e
EI
10460
10461 /* Inputsize checks. */
10462 if (inputsize > pamax &&
10463 (arm_el_is_aa64(env, 1) || inputsize > 40)) {
10464 /* This is CONSTRAINED UNPREDICTABLE and we choose to fault. */
10465 return false;
10466 }
1853d5a9 10467 } else {
1853d5a9
EI
10468 /* AArch32 only supports 4KB pages. Assert on that. */
10469 assert(stride == 9);
10470
10471 if (level == 0) {
10472 return false;
10473 }
1853d5a9
EI
10474 }
10475 return true;
10476}
10477
5b2d261d
AB
10478/* Translate from the 4-bit stage 2 representation of
10479 * memory attributes (without cache-allocation hints) to
10480 * the 8-bit representation of the stage 1 MAIR registers
10481 * (which includes allocation hints).
10482 *
10483 * ref: shared/translation/attrs/S2AttrDecode()
10484 * .../S2ConvertAttrsHints()
10485 */
10486static uint8_t convert_stage2_attrs(CPUARMState *env, uint8_t s2attrs)
10487{
10488 uint8_t hiattr = extract32(s2attrs, 2, 2);
10489 uint8_t loattr = extract32(s2attrs, 0, 2);
10490 uint8_t hihint = 0, lohint = 0;
10491
10492 if (hiattr != 0) { /* normal memory */
10493 if ((env->cp15.hcr_el2 & HCR_CD) != 0) { /* cache disabled */
10494 hiattr = loattr = 1; /* non-cacheable */
10495 } else {
10496 if (hiattr != 1) { /* Write-through or write-back */
10497 hihint = 3; /* RW allocate */
10498 }
10499 if (loattr != 1) { /* Write-through or write-back */
10500 lohint = 3; /* RW allocate */
10501 }
10502 }
10503 }
10504
10505 return (hiattr << 6) | (hihint << 4) | (loattr << 2) | lohint;
10506}
c47eaf9f 10507#endif /* !CONFIG_USER_ONLY */
5b2d261d 10508
b830a5ee
RH
10509static int aa64_va_parameter_tbi(uint64_t tcr, ARMMMUIdx mmu_idx)
10510{
10511 if (regime_has_2_ranges(mmu_idx)) {
10512 return extract64(tcr, 37, 2);
10513 } else if (mmu_idx == ARMMMUIdx_Stage2) {
10514 return 0; /* VTCR_EL2 */
10515 } else {
3e270f67
RH
10516 /* Replicate the single TBI bit so we always have 2 bits. */
10517 return extract32(tcr, 20, 1) * 3;
b830a5ee
RH
10518 }
10519}
10520
10521static int aa64_va_parameter_tbid(uint64_t tcr, ARMMMUIdx mmu_idx)
10522{
10523 if (regime_has_2_ranges(mmu_idx)) {
10524 return extract64(tcr, 51, 2);
10525 } else if (mmu_idx == ARMMMUIdx_Stage2) {
10526 return 0; /* VTCR_EL2 */
10527 } else {
3e270f67
RH
10528 /* Replicate the single TBID bit so we always have 2 bits. */
10529 return extract32(tcr, 29, 1) * 3;
b830a5ee
RH
10530 }
10531}
10532
10533ARMVAParameters aa64_va_parameters(CPUARMState *env, uint64_t va,
10534 ARMMMUIdx mmu_idx, bool data)
ba97be9f
RH
10535{
10536 uint64_t tcr = regime_tcr(env, mmu_idx)->raw_tcr;
b830a5ee
RH
10537 bool epd, hpd, using16k, using64k;
10538 int select, tsz, tbi;
ba97be9f 10539
339370b9 10540 if (!regime_has_2_ranges(mmu_idx)) {
71d18164 10541 select = 0;
ba97be9f
RH
10542 tsz = extract32(tcr, 0, 6);
10543 using64k = extract32(tcr, 14, 1);
10544 using16k = extract32(tcr, 15, 1);
97fa9350 10545 if (mmu_idx == ARMMMUIdx_Stage2) {
ba97be9f 10546 /* VTCR_EL2 */
b830a5ee 10547 hpd = false;
ba97be9f 10548 } else {
ba97be9f
RH
10549 hpd = extract32(tcr, 24, 1);
10550 }
10551 epd = false;
ba97be9f 10552 } else {
71d18164
RH
10553 /*
10554 * Bit 55 is always between the two regions, and is canonical for
10555 * determining if address tagging is enabled.
10556 */
10557 select = extract64(va, 55, 1);
10558 if (!select) {
10559 tsz = extract32(tcr, 0, 6);
10560 epd = extract32(tcr, 7, 1);
10561 using64k = extract32(tcr, 14, 1);
10562 using16k = extract32(tcr, 15, 1);
71d18164 10563 hpd = extract64(tcr, 41, 1);
71d18164
RH
10564 } else {
10565 int tg = extract32(tcr, 30, 2);
10566 using16k = tg == 1;
10567 using64k = tg == 3;
10568 tsz = extract32(tcr, 16, 6);
10569 epd = extract32(tcr, 23, 1);
71d18164 10570 hpd = extract64(tcr, 42, 1);
71d18164 10571 }
ba97be9f
RH
10572 }
10573 tsz = MIN(tsz, 39); /* TODO: ARMv8.4-TTST */
10574 tsz = MAX(tsz, 16); /* TODO: ARMv8.2-LVA */
10575
b830a5ee
RH
10576 /* Present TBI as a composite with TBID. */
10577 tbi = aa64_va_parameter_tbi(tcr, mmu_idx);
10578 if (!data) {
10579 tbi &= ~aa64_va_parameter_tbid(tcr, mmu_idx);
10580 }
10581 tbi = (tbi >> select) & 1;
10582
ba97be9f
RH
10583 return (ARMVAParameters) {
10584 .tsz = tsz,
10585 .select = select,
10586 .tbi = tbi,
10587 .epd = epd,
10588 .hpd = hpd,
10589 .using16k = using16k,
10590 .using64k = using64k,
10591 };
10592}
10593
c47eaf9f 10594#ifndef CONFIG_USER_ONLY
ba97be9f
RH
10595static ARMVAParameters aa32_va_parameters(CPUARMState *env, uint32_t va,
10596 ARMMMUIdx mmu_idx)
10597{
10598 uint64_t tcr = regime_tcr(env, mmu_idx)->raw_tcr;
10599 uint32_t el = regime_el(env, mmu_idx);
10600 int select, tsz;
10601 bool epd, hpd;
10602
97fa9350 10603 if (mmu_idx == ARMMMUIdx_Stage2) {
ba97be9f
RH
10604 /* VTCR */
10605 bool sext = extract32(tcr, 4, 1);
10606 bool sign = extract32(tcr, 3, 1);
10607
10608 /*
10609 * If the sign-extend bit is not the same as t0sz[3], the result
10610 * is unpredictable. Flag this as a guest error.
10611 */
10612 if (sign != sext) {
10613 qemu_log_mask(LOG_GUEST_ERROR,
10614 "AArch32: VTCR.S / VTCR.T0SZ[3] mismatch\n");
10615 }
10616 tsz = sextract32(tcr, 0, 4) + 8;
10617 select = 0;
10618 hpd = false;
10619 epd = false;
10620 } else if (el == 2) {
10621 /* HTCR */
10622 tsz = extract32(tcr, 0, 3);
10623 select = 0;
10624 hpd = extract64(tcr, 24, 1);
10625 epd = false;
10626 } else {
10627 int t0sz = extract32(tcr, 0, 3);
10628 int t1sz = extract32(tcr, 16, 3);
10629
10630 if (t1sz == 0) {
10631 select = va > (0xffffffffu >> t0sz);
10632 } else {
10633 /* Note that we will detect errors later. */
10634 select = va >= ~(0xffffffffu >> t1sz);
10635 }
10636 if (!select) {
10637 tsz = t0sz;
10638 epd = extract32(tcr, 7, 1);
10639 hpd = extract64(tcr, 41, 1);
10640 } else {
10641 tsz = t1sz;
10642 epd = extract32(tcr, 23, 1);
10643 hpd = extract64(tcr, 42, 1);
10644 }
10645 /* For aarch32, hpd0 is not enabled without t2e as well. */
10646 hpd &= extract32(tcr, 6, 1);
10647 }
10648
10649 return (ARMVAParameters) {
10650 .tsz = tsz,
10651 .select = select,
10652 .epd = epd,
10653 .hpd = hpd,
10654 };
10655}
10656
b7cc4e82 10657static bool get_phys_addr_lpae(CPUARMState *env, target_ulong address,
03ae85f8 10658 MMUAccessType access_type, ARMMMUIdx mmu_idx,
b7cc4e82 10659 hwaddr *phys_ptr, MemTxAttrs *txattrs, int *prot,
da909b2c 10660 target_ulong *page_size_ptr,
5b2d261d 10661 ARMMMUFaultInfo *fi, ARMCacheAttrs *cacheattrs)
3dde962f 10662{
2fc0cc0e 10663 ARMCPU *cpu = env_archcpu(env);
1853d5a9 10664 CPUState *cs = CPU(cpu);
3dde962f 10665 /* Read an LPAE long-descriptor translation table. */
da909b2c 10666 ARMFaultType fault_type = ARMFault_Translation;
1b4093ea 10667 uint32_t level;
ba97be9f 10668 ARMVAParameters param;
3dde962f 10669 uint64_t ttbr;
dddb5223 10670 hwaddr descaddr, indexmask, indexmask_grainsize;
3dde962f 10671 uint32_t tableattrs;
36d820af 10672 target_ulong page_size;
3dde962f 10673 uint32_t attrs;
ba97be9f
RH
10674 int32_t stride;
10675 int addrsize, inputsize;
0480f69a 10676 TCR *tcr = regime_tcr(env, mmu_idx);
d8e052b3 10677 int ap, ns, xn, pxn;
88e8add8 10678 uint32_t el = regime_el(env, mmu_idx);
6109769a 10679 uint64_t descaddrmask;
6e99f762 10680 bool aarch64 = arm_el_is_aa64(env, el);
1bafc2ba 10681 bool guarded = false;
0480f69a 10682
07d1be3b 10683 /* TODO: This code does not support shareability levels. */
6e99f762 10684 if (aarch64) {
ba97be9f
RH
10685 param = aa64_va_parameters(env, address, mmu_idx,
10686 access_type != MMU_INST_FETCH);
1b4093ea 10687 level = 0;
ba97be9f
RH
10688 addrsize = 64 - 8 * param.tbi;
10689 inputsize = 64 - param.tsz;
d0a2cbce 10690 } else {
ba97be9f 10691 param = aa32_va_parameters(env, address, mmu_idx);
1b4093ea 10692 level = 1;
97fa9350 10693 addrsize = (mmu_idx == ARMMMUIdx_Stage2 ? 40 : 32);
ba97be9f 10694 inputsize = addrsize - param.tsz;
2c8dd318 10695 }
3dde962f 10696
ba97be9f
RH
10697 /*
10698 * We determined the region when collecting the parameters, but we
10699 * have not yet validated that the address is valid for the region.
10700 * Extract the top bits and verify that they all match select.
36d820af
RH
10701 *
10702 * For aa32, if inputsize == addrsize, then we have selected the
10703 * region by exclusion in aa32_va_parameters and there is no more
10704 * validation to do here.
10705 */
10706 if (inputsize < addrsize) {
10707 target_ulong top_bits = sextract64(address, inputsize,
10708 addrsize - inputsize);
03f27724 10709 if (-top_bits != param.select) {
36d820af
RH
10710 /* The gap between the two regions is a Translation fault */
10711 fault_type = ARMFault_Translation;
10712 goto do_fault;
10713 }
3dde962f
PM
10714 }
10715
ba97be9f
RH
10716 if (param.using64k) {
10717 stride = 13;
10718 } else if (param.using16k) {
10719 stride = 11;
10720 } else {
10721 stride = 9;
10722 }
10723
3dde962f
PM
10724 /* Note that QEMU ignores shareability and cacheability attributes,
10725 * so we don't need to do anything with the SH, ORGN, IRGN fields
10726 * in the TTBCR. Similarly, TTBCR:A1 selects whether we get the
10727 * ASID from TTBR0 or TTBR1, but QEMU's TLB doesn't currently
10728 * implement any ASID-like capability so we can ignore it (instead
10729 * we will always flush the TLB any time the ASID is changed).
10730 */
ba97be9f 10731 ttbr = regime_ttbr(env, mmu_idx, param.select);
3dde962f 10732
0480f69a 10733 /* Here we should have set up all the parameters for the translation:
6e99f762 10734 * inputsize, ttbr, epd, stride, tbi
0480f69a
PM
10735 */
10736
ba97be9f 10737 if (param.epd) {
88e8add8
GB
10738 /* Translation table walk disabled => Translation fault on TLB miss
10739 * Note: This is always 0 on 64-bit EL2 and EL3.
10740 */
3dde962f
PM
10741 goto do_fault;
10742 }
10743
97fa9350 10744 if (mmu_idx != ARMMMUIdx_Stage2) {
1853d5a9
EI
10745 /* The starting level depends on the virtual address size (which can
10746 * be up to 48 bits) and the translation granule size. It indicates
10747 * the number of strides (stride bits at a time) needed to
10748 * consume the bits of the input address. In the pseudocode this is:
10749 * level = 4 - RoundUp((inputsize - grainsize) / stride)
10750 * where their 'inputsize' is our 'inputsize', 'grainsize' is
10751 * our 'stride + 3' and 'stride' is our 'stride'.
10752 * Applying the usual "rounded up m/n is (m+n-1)/n" and simplifying:
10753 * = 4 - (inputsize - stride - 3 + stride - 1) / stride
10754 * = 4 - (inputsize - 4) / stride;
10755 */
10756 level = 4 - (inputsize - 4) / stride;
10757 } else {
10758 /* For stage 2 translations the starting level is specified by the
10759 * VTCR_EL2.SL0 field (whose interpretation depends on the page size)
10760 */
1b4093ea
SS
10761 uint32_t sl0 = extract32(tcr->raw_tcr, 6, 2);
10762 uint32_t startlevel;
1853d5a9
EI
10763 bool ok;
10764
6e99f762 10765 if (!aarch64 || stride == 9) {
1853d5a9 10766 /* AArch32 or 4KB pages */
1b4093ea 10767 startlevel = 2 - sl0;
1853d5a9
EI
10768 } else {
10769 /* 16KB or 64KB pages */
1b4093ea 10770 startlevel = 3 - sl0;
1853d5a9
EI
10771 }
10772
10773 /* Check that the starting level is valid. */
6e99f762 10774 ok = check_s2_mmu_setup(cpu, aarch64, startlevel,
1b4093ea 10775 inputsize, stride);
1853d5a9 10776 if (!ok) {
da909b2c 10777 fault_type = ARMFault_Translation;
1853d5a9
EI
10778 goto do_fault;
10779 }
1b4093ea 10780 level = startlevel;
1853d5a9 10781 }
3dde962f 10782
dddb5223
SS
10783 indexmask_grainsize = (1ULL << (stride + 3)) - 1;
10784 indexmask = (1ULL << (inputsize - (stride * (4 - level)))) - 1;
3dde962f
PM
10785
10786 /* Now we can extract the actual base address from the TTBR */
2c8dd318 10787 descaddr = extract64(ttbr, 0, 48);
41a4bf1f
PM
10788 /*
10789 * We rely on this masking to clear the RES0 bits at the bottom of the TTBR
10790 * and also to mask out CnP (bit 0) which could validly be non-zero.
10791 */
dddb5223 10792 descaddr &= ~indexmask;
3dde962f 10793
6109769a 10794 /* The address field in the descriptor goes up to bit 39 for ARMv7
dddb5223
SS
10795 * but up to bit 47 for ARMv8, but we use the descaddrmask
10796 * up to bit 39 for AArch32, because we don't need other bits in that case
10797 * to construct next descriptor address (anyway they should be all zeroes).
6109769a 10798 */
6e99f762 10799 descaddrmask = ((1ull << (aarch64 ? 48 : 40)) - 1) &
dddb5223 10800 ~indexmask_grainsize;
6109769a 10801
ebca90e4
PM
10802 /* Secure accesses start with the page table in secure memory and
10803 * can be downgraded to non-secure at any step. Non-secure accesses
10804 * remain non-secure. We implement this by just ORing in the NSTable/NS
10805 * bits at each step.
10806 */
10807 tableattrs = regime_is_secure(env, mmu_idx) ? 0 : (1 << 4);
3dde962f
PM
10808 for (;;) {
10809 uint64_t descriptor;
ebca90e4 10810 bool nstable;
3dde962f 10811
dddb5223 10812 descaddr |= (address >> (stride * (4 - level))) & indexmask;
2c8dd318 10813 descaddr &= ~7ULL;
ebca90e4 10814 nstable = extract32(tableattrs, 4, 1);
3795a6de 10815 descriptor = arm_ldq_ptw(cs, descaddr, !nstable, mmu_idx, fi);
3b39d734 10816 if (fi->type != ARMFault_None) {
37785977
EI
10817 goto do_fault;
10818 }
10819
3dde962f
PM
10820 if (!(descriptor & 1) ||
10821 (!(descriptor & 2) && (level == 3))) {
10822 /* Invalid, or the Reserved level 3 encoding */
10823 goto do_fault;
10824 }
6109769a 10825 descaddr = descriptor & descaddrmask;
3dde962f
PM
10826
10827 if ((descriptor & 2) && (level < 3)) {
037c13c5 10828 /* Table entry. The top five bits are attributes which may
3dde962f
PM
10829 * propagate down through lower levels of the table (and
10830 * which are all arranged so that 0 means "no effect", so
10831 * we can gather them up by ORing in the bits at each level).
10832 */
10833 tableattrs |= extract64(descriptor, 59, 5);
10834 level++;
dddb5223 10835 indexmask = indexmask_grainsize;
3dde962f
PM
10836 continue;
10837 }
10838 /* Block entry at level 1 or 2, or page entry at level 3.
10839 * These are basically the same thing, although the number
10840 * of bits we pull in from the vaddr varies.
10841 */
973a5434 10842 page_size = (1ULL << ((stride * (4 - level)) + 3));
3dde962f 10843 descaddr |= (address & (page_size - 1));
6ab1a5ee 10844 /* Extract attributes from the descriptor */
d615efac
IC
10845 attrs = extract64(descriptor, 2, 10)
10846 | (extract64(descriptor, 52, 12) << 10);
6ab1a5ee 10847
97fa9350 10848 if (mmu_idx == ARMMMUIdx_Stage2) {
6ab1a5ee
EI
10849 /* Stage 2 table descriptors do not include any attribute fields */
10850 break;
10851 }
10852 /* Merge in attributes from table descriptors */
037c13c5 10853 attrs |= nstable << 3; /* NS */
1bafc2ba 10854 guarded = extract64(descriptor, 50, 1); /* GP */
ba97be9f 10855 if (param.hpd) {
037c13c5
RH
10856 /* HPD disables all the table attributes except NSTable. */
10857 break;
10858 }
10859 attrs |= extract32(tableattrs, 0, 2) << 11; /* XN, PXN */
3dde962f
PM
10860 /* The sense of AP[1] vs APTable[0] is reversed, as APTable[0] == 1
10861 * means "force PL1 access only", which means forcing AP[1] to 0.
10862 */
037c13c5
RH
10863 attrs &= ~(extract32(tableattrs, 2, 1) << 4); /* !APT[0] => AP[1] */
10864 attrs |= extract32(tableattrs, 3, 1) << 5; /* APT[1] => AP[2] */
3dde962f
PM
10865 break;
10866 }
10867 /* Here descaddr is the final physical address, and attributes
10868 * are all in attrs.
10869 */
da909b2c 10870 fault_type = ARMFault_AccessFlag;
3dde962f
PM
10871 if ((attrs & (1 << 8)) == 0) {
10872 /* Access flag */
10873 goto do_fault;
10874 }
d8e052b3
AJ
10875
10876 ap = extract32(attrs, 4, 2);
d8e052b3 10877 xn = extract32(attrs, 12, 1);
d8e052b3 10878
97fa9350 10879 if (mmu_idx == ARMMMUIdx_Stage2) {
6ab1a5ee
EI
10880 ns = true;
10881 *prot = get_S2prot(env, ap, xn);
10882 } else {
10883 ns = extract32(attrs, 3, 1);
10884 pxn = extract32(attrs, 11, 1);
6e99f762 10885 *prot = get_S1prot(env, mmu_idx, aarch64, ap, ns, xn, pxn);
6ab1a5ee 10886 }
d8e052b3 10887
da909b2c 10888 fault_type = ARMFault_Permission;
d8e052b3 10889 if (!(*prot & (1 << access_type))) {
3dde962f
PM
10890 goto do_fault;
10891 }
3dde962f 10892
8bf5b6a9
PM
10893 if (ns) {
10894 /* The NS bit will (as required by the architecture) have no effect if
10895 * the CPU doesn't support TZ or this is a non-secure translation
10896 * regime, because the attribute will already be non-secure.
10897 */
10898 txattrs->secure = false;
10899 }
1bafc2ba
RH
10900 /* When in aarch64 mode, and BTI is enabled, remember GP in the IOTLB. */
10901 if (aarch64 && guarded && cpu_isar_feature(aa64_bti, cpu)) {
10902 txattrs->target_tlb_bit0 = true;
10903 }
5b2d261d
AB
10904
10905 if (cacheattrs != NULL) {
97fa9350 10906 if (mmu_idx == ARMMMUIdx_Stage2) {
5b2d261d
AB
10907 cacheattrs->attrs = convert_stage2_attrs(env,
10908 extract32(attrs, 0, 4));
10909 } else {
10910 /* Index into MAIR registers for cache attributes */
10911 uint8_t attrindx = extract32(attrs, 0, 3);
10912 uint64_t mair = env->cp15.mair_el[regime_el(env, mmu_idx)];
10913 assert(attrindx <= 7);
10914 cacheattrs->attrs = extract64(mair, attrindx * 8, 8);
10915 }
10916 cacheattrs->shareability = extract32(attrs, 6, 2);
10917 }
10918
3dde962f
PM
10919 *phys_ptr = descaddr;
10920 *page_size_ptr = page_size;
b7cc4e82 10921 return false;
3dde962f
PM
10922
10923do_fault:
da909b2c
PM
10924 fi->type = fault_type;
10925 fi->level = level;
37785977 10926 /* Tag the error as S2 for failed S1 PTW at S2 or ordinary S2. */
97fa9350 10927 fi->stage2 = fi->s1ptw || (mmu_idx == ARMMMUIdx_Stage2);
b7cc4e82 10928 return true;
3dde962f
PM
10929}
10930
f6bda88f
PC
10931static inline void get_phys_addr_pmsav7_default(CPUARMState *env,
10932 ARMMMUIdx mmu_idx,
10933 int32_t address, int *prot)
10934{
3a00d560
MD
10935 if (!arm_feature(env, ARM_FEATURE_M)) {
10936 *prot = PAGE_READ | PAGE_WRITE;
10937 switch (address) {
10938 case 0xF0000000 ... 0xFFFFFFFF:
10939 if (regime_sctlr(env, mmu_idx) & SCTLR_V) {
10940 /* hivecs execing is ok */
10941 *prot |= PAGE_EXEC;
10942 }
10943 break;
10944 case 0x00000000 ... 0x7FFFFFFF:
f6bda88f 10945 *prot |= PAGE_EXEC;
3a00d560
MD
10946 break;
10947 }
10948 } else {
10949 /* Default system address map for M profile cores.
10950 * The architecture specifies which regions are execute-never;
10951 * at the MPU level no other checks are defined.
10952 */
10953 switch (address) {
10954 case 0x00000000 ... 0x1fffffff: /* ROM */
10955 case 0x20000000 ... 0x3fffffff: /* SRAM */
10956 case 0x60000000 ... 0x7fffffff: /* RAM */
10957 case 0x80000000 ... 0x9fffffff: /* RAM */
10958 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
10959 break;
10960 case 0x40000000 ... 0x5fffffff: /* Peripheral */
10961 case 0xa0000000 ... 0xbfffffff: /* Device */
10962 case 0xc0000000 ... 0xdfffffff: /* Device */
10963 case 0xe0000000 ... 0xffffffff: /* System */
10964 *prot = PAGE_READ | PAGE_WRITE;
10965 break;
10966 default:
10967 g_assert_not_reached();
f6bda88f 10968 }
f6bda88f 10969 }
f6bda88f
PC
10970}
10971
29c483a5
MD
10972static bool pmsav7_use_background_region(ARMCPU *cpu,
10973 ARMMMUIdx mmu_idx, bool is_user)
10974{
10975 /* Return true if we should use the default memory map as a
10976 * "background" region if there are no hits against any MPU regions.
10977 */
10978 CPUARMState *env = &cpu->env;
10979
10980 if (is_user) {
10981 return false;
10982 }
10983
10984 if (arm_feature(env, ARM_FEATURE_M)) {
ecf5e8ea
PM
10985 return env->v7m.mpu_ctrl[regime_is_secure(env, mmu_idx)]
10986 & R_V7M_MPU_CTRL_PRIVDEFENA_MASK;
29c483a5
MD
10987 } else {
10988 return regime_sctlr(env, mmu_idx) & SCTLR_BR;
10989 }
10990}
10991
38aaa60c
PM
10992static inline bool m_is_ppb_region(CPUARMState *env, uint32_t address)
10993{
10994 /* True if address is in the M profile PPB region 0xe0000000 - 0xe00fffff */
10995 return arm_feature(env, ARM_FEATURE_M) &&
10996 extract32(address, 20, 12) == 0xe00;
10997}
10998
bf446a11
PM
10999static inline bool m_is_system_region(CPUARMState *env, uint32_t address)
11000{
11001 /* True if address is in the M profile system region
11002 * 0xe0000000 - 0xffffffff
11003 */
11004 return arm_feature(env, ARM_FEATURE_M) && extract32(address, 29, 3) == 0x7;
11005}
11006
f6bda88f 11007static bool get_phys_addr_pmsav7(CPUARMState *env, uint32_t address,
03ae85f8 11008 MMUAccessType access_type, ARMMMUIdx mmu_idx,
9375ad15 11009 hwaddr *phys_ptr, int *prot,
e5e40999 11010 target_ulong *page_size,
9375ad15 11011 ARMMMUFaultInfo *fi)
f6bda88f 11012{
2fc0cc0e 11013 ARMCPU *cpu = env_archcpu(env);
f6bda88f
PC
11014 int n;
11015 bool is_user = regime_is_user(env, mmu_idx);
11016
11017 *phys_ptr = address;
e5e40999 11018 *page_size = TARGET_PAGE_SIZE;
f6bda88f
PC
11019 *prot = 0;
11020
38aaa60c
PM
11021 if (regime_translation_disabled(env, mmu_idx) ||
11022 m_is_ppb_region(env, address)) {
11023 /* MPU disabled or M profile PPB access: use default memory map.
11024 * The other case which uses the default memory map in the
11025 * v7M ARM ARM pseudocode is exception vector reads from the vector
11026 * table. In QEMU those accesses are done in arm_v7m_load_vector(),
11027 * which always does a direct read using address_space_ldl(), rather
11028 * than going via this function, so we don't need to check that here.
11029 */
f6bda88f
PC
11030 get_phys_addr_pmsav7_default(env, mmu_idx, address, prot);
11031 } else { /* MPU enabled */
11032 for (n = (int)cpu->pmsav7_dregion - 1; n >= 0; n--) {
11033 /* region search */
11034 uint32_t base = env->pmsav7.drbar[n];
11035 uint32_t rsize = extract32(env->pmsav7.drsr[n], 1, 5);
11036 uint32_t rmask;
11037 bool srdis = false;
11038
11039 if (!(env->pmsav7.drsr[n] & 0x1)) {
11040 continue;
11041 }
11042
11043 if (!rsize) {
c9f9f124
MD
11044 qemu_log_mask(LOG_GUEST_ERROR,
11045 "DRSR[%d]: Rsize field cannot be 0\n", n);
f6bda88f
PC
11046 continue;
11047 }
11048 rsize++;
11049 rmask = (1ull << rsize) - 1;
11050
11051 if (base & rmask) {
c9f9f124
MD
11052 qemu_log_mask(LOG_GUEST_ERROR,
11053 "DRBAR[%d]: 0x%" PRIx32 " misaligned "
11054 "to DRSR region size, mask = 0x%" PRIx32 "\n",
11055 n, base, rmask);
f6bda88f
PC
11056 continue;
11057 }
11058
11059 if (address < base || address > base + rmask) {
9d2b5a58
PM
11060 /*
11061 * Address not in this region. We must check whether the
11062 * region covers addresses in the same page as our address.
11063 * In that case we must not report a size that covers the
11064 * whole page for a subsequent hit against a different MPU
11065 * region or the background region, because it would result in
11066 * incorrect TLB hits for subsequent accesses to addresses that
11067 * are in this MPU region.
11068 */
11069 if (ranges_overlap(base, rmask,
11070 address & TARGET_PAGE_MASK,
11071 TARGET_PAGE_SIZE)) {
11072 *page_size = 1;
11073 }
f6bda88f
PC
11074 continue;
11075 }
11076
11077 /* Region matched */
11078
11079 if (rsize >= 8) { /* no subregions for regions < 256 bytes */
11080 int i, snd;
11081 uint32_t srdis_mask;
11082
11083 rsize -= 3; /* sub region size (power of 2) */
11084 snd = ((address - base) >> rsize) & 0x7;
11085 srdis = extract32(env->pmsav7.drsr[n], snd + 8, 1);
11086
11087 srdis_mask = srdis ? 0x3 : 0x0;
11088 for (i = 2; i <= 8 && rsize < TARGET_PAGE_BITS; i *= 2) {
11089 /* This will check in groups of 2, 4 and then 8, whether
11090 * the subregion bits are consistent. rsize is incremented
11091 * back up to give the region size, considering consistent
11092 * adjacent subregions as one region. Stop testing if rsize
11093 * is already big enough for an entire QEMU page.
11094 */
11095 int snd_rounded = snd & ~(i - 1);
11096 uint32_t srdis_multi = extract32(env->pmsav7.drsr[n],
11097 snd_rounded + 8, i);
11098 if (srdis_mask ^ srdis_multi) {
11099 break;
11100 }
11101 srdis_mask = (srdis_mask << i) | srdis_mask;
11102 rsize++;
11103 }
11104 }
f6bda88f
PC
11105 if (srdis) {
11106 continue;
11107 }
e5e40999
PM
11108 if (rsize < TARGET_PAGE_BITS) {
11109 *page_size = 1 << rsize;
11110 }
f6bda88f
PC
11111 break;
11112 }
11113
11114 if (n == -1) { /* no hits */
29c483a5 11115 if (!pmsav7_use_background_region(cpu, mmu_idx, is_user)) {
f6bda88f 11116 /* background fault */
9375ad15 11117 fi->type = ARMFault_Background;
f6bda88f
PC
11118 return true;
11119 }
11120 get_phys_addr_pmsav7_default(env, mmu_idx, address, prot);
11121 } else { /* a MPU hit! */
11122 uint32_t ap = extract32(env->pmsav7.dracr[n], 8, 3);
bf446a11
PM
11123 uint32_t xn = extract32(env->pmsav7.dracr[n], 12, 1);
11124
11125 if (m_is_system_region(env, address)) {
11126 /* System space is always execute never */
11127 xn = 1;
11128 }
f6bda88f
PC
11129
11130 if (is_user) { /* User mode AP bit decoding */
11131 switch (ap) {
11132 case 0:
11133 case 1:
11134 case 5:
11135 break; /* no access */
11136 case 3:
11137 *prot |= PAGE_WRITE;
11138 /* fall through */
11139 case 2:
11140 case 6:
11141 *prot |= PAGE_READ | PAGE_EXEC;
11142 break;
8638f1ad
PM
11143 case 7:
11144 /* for v7M, same as 6; for R profile a reserved value */
11145 if (arm_feature(env, ARM_FEATURE_M)) {
11146 *prot |= PAGE_READ | PAGE_EXEC;
11147 break;
11148 }
11149 /* fall through */
f6bda88f
PC
11150 default:
11151 qemu_log_mask(LOG_GUEST_ERROR,
c9f9f124
MD
11152 "DRACR[%d]: Bad value for AP bits: 0x%"
11153 PRIx32 "\n", n, ap);
f6bda88f
PC
11154 }
11155 } else { /* Priv. mode AP bits decoding */
11156 switch (ap) {
11157 case 0:
11158 break; /* no access */
11159 case 1:
11160 case 2:
11161 case 3:
11162 *prot |= PAGE_WRITE;
11163 /* fall through */
11164 case 5:
11165 case 6:
11166 *prot |= PAGE_READ | PAGE_EXEC;
11167 break;
8638f1ad
PM
11168 case 7:
11169 /* for v7M, same as 6; for R profile a reserved value */
11170 if (arm_feature(env, ARM_FEATURE_M)) {
11171 *prot |= PAGE_READ | PAGE_EXEC;
11172 break;
11173 }
11174 /* fall through */
f6bda88f
PC
11175 default:
11176 qemu_log_mask(LOG_GUEST_ERROR,
c9f9f124
MD
11177 "DRACR[%d]: Bad value for AP bits: 0x%"
11178 PRIx32 "\n", n, ap);
f6bda88f
PC
11179 }
11180 }
11181
11182 /* execute never */
bf446a11 11183 if (xn) {
f6bda88f
PC
11184 *prot &= ~PAGE_EXEC;
11185 }
11186 }
11187 }
11188
9375ad15
PM
11189 fi->type = ARMFault_Permission;
11190 fi->level = 1;
f6bda88f
PC
11191 return !(*prot & (1 << access_type));
11192}
11193
35337cc3
PM
11194static bool v8m_is_sau_exempt(CPUARMState *env,
11195 uint32_t address, MMUAccessType access_type)
11196{
11197 /* The architecture specifies that certain address ranges are
11198 * exempt from v8M SAU/IDAU checks.
11199 */
11200 return
11201 (access_type == MMU_INST_FETCH && m_is_system_region(env, address)) ||
11202 (address >= 0xe0000000 && address <= 0xe0002fff) ||
11203 (address >= 0xe000e000 && address <= 0xe000efff) ||
11204 (address >= 0xe002e000 && address <= 0xe002efff) ||
11205 (address >= 0xe0040000 && address <= 0xe0041fff) ||
11206 (address >= 0xe00ff000 && address <= 0xe00fffff);
11207}
11208
787a7e76 11209void v8m_security_lookup(CPUARMState *env, uint32_t address,
35337cc3
PM
11210 MMUAccessType access_type, ARMMMUIdx mmu_idx,
11211 V8M_SAttributes *sattrs)
11212{
11213 /* Look up the security attributes for this address. Compare the
11214 * pseudocode SecurityCheck() function.
11215 * We assume the caller has zero-initialized *sattrs.
11216 */
2fc0cc0e 11217 ARMCPU *cpu = env_archcpu(env);
35337cc3 11218 int r;
181962fd
PM
11219 bool idau_exempt = false, idau_ns = true, idau_nsc = true;
11220 int idau_region = IREGION_NOTVALID;
72042435
PM
11221 uint32_t addr_page_base = address & TARGET_PAGE_MASK;
11222 uint32_t addr_page_limit = addr_page_base + (TARGET_PAGE_SIZE - 1);
35337cc3 11223
181962fd
PM
11224 if (cpu->idau) {
11225 IDAUInterfaceClass *iic = IDAU_INTERFACE_GET_CLASS(cpu->idau);
11226 IDAUInterface *ii = IDAU_INTERFACE(cpu->idau);
11227
11228 iic->check(ii, address, &idau_region, &idau_exempt, &idau_ns,
11229 &idau_nsc);
11230 }
35337cc3
PM
11231
11232 if (access_type == MMU_INST_FETCH && extract32(address, 28, 4) == 0xf) {
11233 /* 0xf0000000..0xffffffff is always S for insn fetches */
11234 return;
11235 }
11236
181962fd 11237 if (idau_exempt || v8m_is_sau_exempt(env, address, access_type)) {
35337cc3
PM
11238 sattrs->ns = !regime_is_secure(env, mmu_idx);
11239 return;
11240 }
11241
181962fd
PM
11242 if (idau_region != IREGION_NOTVALID) {
11243 sattrs->irvalid = true;
11244 sattrs->iregion = idau_region;
11245 }
11246
35337cc3
PM
11247 switch (env->sau.ctrl & 3) {
11248 case 0: /* SAU.ENABLE == 0, SAU.ALLNS == 0 */
11249 break;
11250 case 2: /* SAU.ENABLE == 0, SAU.ALLNS == 1 */
11251 sattrs->ns = true;
11252 break;
11253 default: /* SAU.ENABLE == 1 */
11254 for (r = 0; r < cpu->sau_sregion; r++) {
11255 if (env->sau.rlar[r] & 1) {
11256 uint32_t base = env->sau.rbar[r] & ~0x1f;
11257 uint32_t limit = env->sau.rlar[r] | 0x1f;
11258
11259 if (base <= address && limit >= address) {
72042435
PM
11260 if (base > addr_page_base || limit < addr_page_limit) {
11261 sattrs->subpage = true;
11262 }
35337cc3
PM
11263 if (sattrs->srvalid) {
11264 /* If we hit in more than one region then we must report
11265 * as Secure, not NS-Callable, with no valid region
11266 * number info.
11267 */
11268 sattrs->ns = false;
11269 sattrs->nsc = false;
11270 sattrs->sregion = 0;
11271 sattrs->srvalid = false;
11272 break;
11273 } else {
11274 if (env->sau.rlar[r] & 2) {
11275 sattrs->nsc = true;
11276 } else {
11277 sattrs->ns = true;
11278 }
11279 sattrs->srvalid = true;
11280 sattrs->sregion = r;
11281 }
9d2b5a58
PM
11282 } else {
11283 /*
11284 * Address not in this region. We must check whether the
11285 * region covers addresses in the same page as our address.
11286 * In that case we must not report a size that covers the
11287 * whole page for a subsequent hit against a different MPU
11288 * region or the background region, because it would result
11289 * in incorrect TLB hits for subsequent accesses to
11290 * addresses that are in this MPU region.
11291 */
11292 if (limit >= base &&
11293 ranges_overlap(base, limit - base + 1,
11294 addr_page_base,
11295 TARGET_PAGE_SIZE)) {
11296 sattrs->subpage = true;
11297 }
35337cc3
PM
11298 }
11299 }
11300 }
7e3f1223
TR
11301 break;
11302 }
35337cc3 11303
7e3f1223
TR
11304 /*
11305 * The IDAU will override the SAU lookup results if it specifies
11306 * higher security than the SAU does.
11307 */
11308 if (!idau_ns) {
11309 if (sattrs->ns || (!idau_nsc && sattrs->nsc)) {
11310 sattrs->ns = false;
11311 sattrs->nsc = idau_nsc;
181962fd 11312 }
35337cc3
PM
11313 }
11314}
11315
787a7e76 11316bool pmsav8_mpu_lookup(CPUARMState *env, uint32_t address,
54317c0f
PM
11317 MMUAccessType access_type, ARMMMUIdx mmu_idx,
11318 hwaddr *phys_ptr, MemTxAttrs *txattrs,
72042435
PM
11319 int *prot, bool *is_subpage,
11320 ARMMMUFaultInfo *fi, uint32_t *mregion)
54317c0f
PM
11321{
11322 /* Perform a PMSAv8 MPU lookup (without also doing the SAU check
11323 * that a full phys-to-virt translation does).
11324 * mregion is (if not NULL) set to the region number which matched,
11325 * or -1 if no region number is returned (MPU off, address did not
11326 * hit a region, address hit in multiple regions).
72042435
PM
11327 * We set is_subpage to true if the region hit doesn't cover the
11328 * entire TARGET_PAGE the address is within.
54317c0f 11329 */
2fc0cc0e 11330 ARMCPU *cpu = env_archcpu(env);
504e3cc3 11331 bool is_user = regime_is_user(env, mmu_idx);
62c58ee0 11332 uint32_t secure = regime_is_secure(env, mmu_idx);
504e3cc3
PM
11333 int n;
11334 int matchregion = -1;
11335 bool hit = false;
72042435
PM
11336 uint32_t addr_page_base = address & TARGET_PAGE_MASK;
11337 uint32_t addr_page_limit = addr_page_base + (TARGET_PAGE_SIZE - 1);
504e3cc3 11338
72042435 11339 *is_subpage = false;
504e3cc3
PM
11340 *phys_ptr = address;
11341 *prot = 0;
54317c0f
PM
11342 if (mregion) {
11343 *mregion = -1;
35337cc3
PM
11344 }
11345
504e3cc3
PM
11346 /* Unlike the ARM ARM pseudocode, we don't need to check whether this
11347 * was an exception vector read from the vector table (which is always
11348 * done using the default system address map), because those accesses
11349 * are done in arm_v7m_load_vector(), which always does a direct
11350 * read using address_space_ldl(), rather than going via this function.
11351 */
11352 if (regime_translation_disabled(env, mmu_idx)) { /* MPU disabled */
11353 hit = true;
11354 } else if (m_is_ppb_region(env, address)) {
11355 hit = true;
504e3cc3 11356 } else {
cff21316
PM
11357 if (pmsav7_use_background_region(cpu, mmu_idx, is_user)) {
11358 hit = true;
11359 }
11360
504e3cc3
PM
11361 for (n = (int)cpu->pmsav7_dregion - 1; n >= 0; n--) {
11362 /* region search */
11363 /* Note that the base address is bits [31:5] from the register
11364 * with bits [4:0] all zeroes, but the limit address is bits
11365 * [31:5] from the register with bits [4:0] all ones.
11366 */
62c58ee0
PM
11367 uint32_t base = env->pmsav8.rbar[secure][n] & ~0x1f;
11368 uint32_t limit = env->pmsav8.rlar[secure][n] | 0x1f;
504e3cc3 11369
62c58ee0 11370 if (!(env->pmsav8.rlar[secure][n] & 0x1)) {
504e3cc3
PM
11371 /* Region disabled */
11372 continue;
11373 }
11374
11375 if (address < base || address > limit) {
9d2b5a58
PM
11376 /*
11377 * Address not in this region. We must check whether the
11378 * region covers addresses in the same page as our address.
11379 * In that case we must not report a size that covers the
11380 * whole page for a subsequent hit against a different MPU
11381 * region or the background region, because it would result in
11382 * incorrect TLB hits for subsequent accesses to addresses that
11383 * are in this MPU region.
11384 */
11385 if (limit >= base &&
11386 ranges_overlap(base, limit - base + 1,
11387 addr_page_base,
11388 TARGET_PAGE_SIZE)) {
11389 *is_subpage = true;
11390 }
504e3cc3
PM
11391 continue;
11392 }
11393
72042435
PM
11394 if (base > addr_page_base || limit < addr_page_limit) {
11395 *is_subpage = true;
11396 }
11397
cff21316 11398 if (matchregion != -1) {
504e3cc3
PM
11399 /* Multiple regions match -- always a failure (unlike
11400 * PMSAv7 where highest-numbered-region wins)
11401 */
3f551b5b
PM
11402 fi->type = ARMFault_Permission;
11403 fi->level = 1;
504e3cc3
PM
11404 return true;
11405 }
11406
11407 matchregion = n;
11408 hit = true;
504e3cc3
PM
11409 }
11410 }
11411
11412 if (!hit) {
11413 /* background fault */
3f551b5b 11414 fi->type = ARMFault_Background;
504e3cc3
PM
11415 return true;
11416 }
11417
11418 if (matchregion == -1) {
11419 /* hit using the background region */
11420 get_phys_addr_pmsav7_default(env, mmu_idx, address, prot);
11421 } else {
62c58ee0
PM
11422 uint32_t ap = extract32(env->pmsav8.rbar[secure][matchregion], 1, 2);
11423 uint32_t xn = extract32(env->pmsav8.rbar[secure][matchregion], 0, 1);
504e3cc3
PM
11424
11425 if (m_is_system_region(env, address)) {
11426 /* System space is always execute never */
11427 xn = 1;
11428 }
11429
11430 *prot = simple_ap_to_rw_prot(env, mmu_idx, ap);
11431 if (*prot && !xn) {
11432 *prot |= PAGE_EXEC;
11433 }
11434 /* We don't need to look the attribute up in the MAIR0/MAIR1
11435 * registers because that only tells us about cacheability.
11436 */
54317c0f
PM
11437 if (mregion) {
11438 *mregion = matchregion;
11439 }
504e3cc3
PM
11440 }
11441
3f551b5b
PM
11442 fi->type = ARMFault_Permission;
11443 fi->level = 1;
504e3cc3
PM
11444 return !(*prot & (1 << access_type));
11445}
11446
54317c0f
PM
11447
11448static bool get_phys_addr_pmsav8(CPUARMState *env, uint32_t address,
11449 MMUAccessType access_type, ARMMMUIdx mmu_idx,
11450 hwaddr *phys_ptr, MemTxAttrs *txattrs,
72042435
PM
11451 int *prot, target_ulong *page_size,
11452 ARMMMUFaultInfo *fi)
54317c0f
PM
11453{
11454 uint32_t secure = regime_is_secure(env, mmu_idx);
11455 V8M_SAttributes sattrs = {};
72042435
PM
11456 bool ret;
11457 bool mpu_is_subpage;
54317c0f
PM
11458
11459 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
11460 v8m_security_lookup(env, address, access_type, mmu_idx, &sattrs);
11461 if (access_type == MMU_INST_FETCH) {
11462 /* Instruction fetches always use the MMU bank and the
11463 * transaction attribute determined by the fetch address,
11464 * regardless of CPU state. This is painful for QEMU
11465 * to handle, because it would mean we need to encode
11466 * into the mmu_idx not just the (user, negpri) information
11467 * for the current security state but also that for the
11468 * other security state, which would balloon the number
11469 * of mmu_idx values needed alarmingly.
11470 * Fortunately we can avoid this because it's not actually
11471 * possible to arbitrarily execute code from memory with
11472 * the wrong security attribute: it will always generate
11473 * an exception of some kind or another, apart from the
11474 * special case of an NS CPU executing an SG instruction
11475 * in S&NSC memory. So we always just fail the translation
11476 * here and sort things out in the exception handler
11477 * (including possibly emulating an SG instruction).
11478 */
11479 if (sattrs.ns != !secure) {
3f551b5b
PM
11480 if (sattrs.nsc) {
11481 fi->type = ARMFault_QEMU_NSCExec;
11482 } else {
11483 fi->type = ARMFault_QEMU_SFault;
11484 }
72042435 11485 *page_size = sattrs.subpage ? 1 : TARGET_PAGE_SIZE;
54317c0f
PM
11486 *phys_ptr = address;
11487 *prot = 0;
11488 return true;
11489 }
11490 } else {
11491 /* For data accesses we always use the MMU bank indicated
11492 * by the current CPU state, but the security attributes
11493 * might downgrade a secure access to nonsecure.
11494 */
11495 if (sattrs.ns) {
11496 txattrs->secure = false;
11497 } else if (!secure) {
11498 /* NS access to S memory must fault.
11499 * Architecturally we should first check whether the
11500 * MPU information for this address indicates that we
11501 * are doing an unaligned access to Device memory, which
11502 * should generate a UsageFault instead. QEMU does not
11503 * currently check for that kind of unaligned access though.
11504 * If we added it we would need to do so as a special case
11505 * for M_FAKE_FSR_SFAULT in arm_v7m_cpu_do_interrupt().
11506 */
3f551b5b 11507 fi->type = ARMFault_QEMU_SFault;
72042435 11508 *page_size = sattrs.subpage ? 1 : TARGET_PAGE_SIZE;
54317c0f
PM
11509 *phys_ptr = address;
11510 *prot = 0;
11511 return true;
11512 }
11513 }
11514 }
11515
72042435
PM
11516 ret = pmsav8_mpu_lookup(env, address, access_type, mmu_idx, phys_ptr,
11517 txattrs, prot, &mpu_is_subpage, fi, NULL);
72042435
PM
11518 *page_size = sattrs.subpage || mpu_is_subpage ? 1 : TARGET_PAGE_SIZE;
11519 return ret;
54317c0f
PM
11520}
11521
13689d43 11522static bool get_phys_addr_pmsav5(CPUARMState *env, uint32_t address,
03ae85f8 11523 MMUAccessType access_type, ARMMMUIdx mmu_idx,
53a4e5c5
PM
11524 hwaddr *phys_ptr, int *prot,
11525 ARMMMUFaultInfo *fi)
9ee6e8bb
PB
11526{
11527 int n;
11528 uint32_t mask;
11529 uint32_t base;
0480f69a 11530 bool is_user = regime_is_user(env, mmu_idx);
9ee6e8bb 11531
3279adb9
PM
11532 if (regime_translation_disabled(env, mmu_idx)) {
11533 /* MPU disabled. */
11534 *phys_ptr = address;
11535 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
11536 return false;
11537 }
11538
9ee6e8bb
PB
11539 *phys_ptr = address;
11540 for (n = 7; n >= 0; n--) {
554b0b09 11541 base = env->cp15.c6_region[n];
87c3d486 11542 if ((base & 1) == 0) {
554b0b09 11543 continue;
87c3d486 11544 }
554b0b09
PM
11545 mask = 1 << ((base >> 1) & 0x1f);
11546 /* Keep this shift separate from the above to avoid an
11547 (undefined) << 32. */
11548 mask = (mask << 1) - 1;
87c3d486 11549 if (((base ^ address) & ~mask) == 0) {
554b0b09 11550 break;
87c3d486 11551 }
9ee6e8bb 11552 }
87c3d486 11553 if (n < 0) {
53a4e5c5 11554 fi->type = ARMFault_Background;
b7cc4e82 11555 return true;
87c3d486 11556 }
9ee6e8bb 11557
03ae85f8 11558 if (access_type == MMU_INST_FETCH) {
7e09797c 11559 mask = env->cp15.pmsav5_insn_ap;
9ee6e8bb 11560 } else {
7e09797c 11561 mask = env->cp15.pmsav5_data_ap;
9ee6e8bb
PB
11562 }
11563 mask = (mask >> (n * 4)) & 0xf;
11564 switch (mask) {
11565 case 0:
53a4e5c5
PM
11566 fi->type = ARMFault_Permission;
11567 fi->level = 1;
b7cc4e82 11568 return true;
9ee6e8bb 11569 case 1:
87c3d486 11570 if (is_user) {
53a4e5c5
PM
11571 fi->type = ARMFault_Permission;
11572 fi->level = 1;
b7cc4e82 11573 return true;
87c3d486 11574 }
554b0b09
PM
11575 *prot = PAGE_READ | PAGE_WRITE;
11576 break;
9ee6e8bb 11577 case 2:
554b0b09 11578 *prot = PAGE_READ;
87c3d486 11579 if (!is_user) {
554b0b09 11580 *prot |= PAGE_WRITE;
87c3d486 11581 }
554b0b09 11582 break;
9ee6e8bb 11583 case 3:
554b0b09
PM
11584 *prot = PAGE_READ | PAGE_WRITE;
11585 break;
9ee6e8bb 11586 case 5:
87c3d486 11587 if (is_user) {
53a4e5c5
PM
11588 fi->type = ARMFault_Permission;
11589 fi->level = 1;
b7cc4e82 11590 return true;
87c3d486 11591 }
554b0b09
PM
11592 *prot = PAGE_READ;
11593 break;
9ee6e8bb 11594 case 6:
554b0b09
PM
11595 *prot = PAGE_READ;
11596 break;
9ee6e8bb 11597 default:
554b0b09 11598 /* Bad permission. */
53a4e5c5
PM
11599 fi->type = ARMFault_Permission;
11600 fi->level = 1;
b7cc4e82 11601 return true;
9ee6e8bb 11602 }
3ad493fc 11603 *prot |= PAGE_EXEC;
b7cc4e82 11604 return false;
9ee6e8bb
PB
11605}
11606
5b2d261d
AB
11607/* Combine either inner or outer cacheability attributes for normal
11608 * memory, according to table D4-42 and pseudocode procedure
11609 * CombineS1S2AttrHints() of ARM DDI 0487B.b (the ARMv8 ARM).
11610 *
11611 * NB: only stage 1 includes allocation hints (RW bits), leading to
11612 * some asymmetry.
11613 */
11614static uint8_t combine_cacheattr_nibble(uint8_t s1, uint8_t s2)
11615{
11616 if (s1 == 4 || s2 == 4) {
11617 /* non-cacheable has precedence */
11618 return 4;
11619 } else if (extract32(s1, 2, 2) == 0 || extract32(s1, 2, 2) == 2) {
11620 /* stage 1 write-through takes precedence */
11621 return s1;
11622 } else if (extract32(s2, 2, 2) == 2) {
11623 /* stage 2 write-through takes precedence, but the allocation hint
11624 * is still taken from stage 1
11625 */
11626 return (2 << 2) | extract32(s1, 0, 2);
11627 } else { /* write-back */
11628 return s1;
11629 }
11630}
11631
11632/* Combine S1 and S2 cacheability/shareability attributes, per D4.5.4
11633 * and CombineS1S2Desc()
11634 *
11635 * @s1: Attributes from stage 1 walk
11636 * @s2: Attributes from stage 2 walk
11637 */
11638static ARMCacheAttrs combine_cacheattrs(ARMCacheAttrs s1, ARMCacheAttrs s2)
11639{
11640 uint8_t s1lo = extract32(s1.attrs, 0, 4), s2lo = extract32(s2.attrs, 0, 4);
11641 uint8_t s1hi = extract32(s1.attrs, 4, 4), s2hi = extract32(s2.attrs, 4, 4);
11642 ARMCacheAttrs ret;
11643
11644 /* Combine shareability attributes (table D4-43) */
11645 if (s1.shareability == 2 || s2.shareability == 2) {
11646 /* if either are outer-shareable, the result is outer-shareable */
11647 ret.shareability = 2;
11648 } else if (s1.shareability == 3 || s2.shareability == 3) {
11649 /* if either are inner-shareable, the result is inner-shareable */
11650 ret.shareability = 3;
11651 } else {
11652 /* both non-shareable */
11653 ret.shareability = 0;
11654 }
11655
11656 /* Combine memory type and cacheability attributes */
11657 if (s1hi == 0 || s2hi == 0) {
11658 /* Device has precedence over normal */
11659 if (s1lo == 0 || s2lo == 0) {
11660 /* nGnRnE has precedence over anything */
11661 ret.attrs = 0;
11662 } else if (s1lo == 4 || s2lo == 4) {
11663 /* non-Reordering has precedence over Reordering */
11664 ret.attrs = 4; /* nGnRE */
11665 } else if (s1lo == 8 || s2lo == 8) {
11666 /* non-Gathering has precedence over Gathering */
11667 ret.attrs = 8; /* nGRE */
11668 } else {
11669 ret.attrs = 0xc; /* GRE */
11670 }
11671
11672 /* Any location for which the resultant memory type is any
11673 * type of Device memory is always treated as Outer Shareable.
11674 */
11675 ret.shareability = 2;
11676 } else { /* Normal memory */
11677 /* Outer/inner cacheability combine independently */
11678 ret.attrs = combine_cacheattr_nibble(s1hi, s2hi) << 4
11679 | combine_cacheattr_nibble(s1lo, s2lo);
11680
11681 if (ret.attrs == 0x44) {
11682 /* Any location for which the resultant memory type is Normal
11683 * Inner Non-cacheable, Outer Non-cacheable is always treated
11684 * as Outer Shareable.
11685 */
11686 ret.shareability = 2;
11687 }
11688 }
11689
11690 return ret;
11691}
11692
11693
702a9357
PM
11694/* get_phys_addr - get the physical address for this virtual address
11695 *
11696 * Find the physical address corresponding to the given virtual address,
11697 * by doing a translation table walk on MMU based systems or using the
11698 * MPU state on MPU based systems.
11699 *
b7cc4e82
PC
11700 * Returns false if the translation was successful. Otherwise, phys_ptr, attrs,
11701 * prot and page_size may not be filled in, and the populated fsr value provides
702a9357
PM
11702 * information on why the translation aborted, in the format of a
11703 * DFSR/IFSR fault register, with the following caveats:
11704 * * we honour the short vs long DFSR format differences.
11705 * * the WnR bit is never set (the caller must do this).
f6bda88f 11706 * * for PSMAv5 based systems we don't bother to return a full FSR format
702a9357
PM
11707 * value.
11708 *
11709 * @env: CPUARMState
11710 * @address: virtual address to get physical address for
11711 * @access_type: 0 for read, 1 for write, 2 for execute
d3649702 11712 * @mmu_idx: MMU index indicating required translation regime
702a9357 11713 * @phys_ptr: set to the physical address corresponding to the virtual address
8bf5b6a9 11714 * @attrs: set to the memory transaction attributes to use
702a9357
PM
11715 * @prot: set to the permissions for the page containing phys_ptr
11716 * @page_size: set to the size of the page containing phys_ptr
5b2d261d
AB
11717 * @fi: set to fault info if the translation fails
11718 * @cacheattrs: (if non-NULL) set to the cacheability/shareability attributes
702a9357 11719 */
ebae861f
PMD
11720bool get_phys_addr(CPUARMState *env, target_ulong address,
11721 MMUAccessType access_type, ARMMMUIdx mmu_idx,
11722 hwaddr *phys_ptr, MemTxAttrs *attrs, int *prot,
11723 target_ulong *page_size,
11724 ARMMMUFaultInfo *fi, ARMCacheAttrs *cacheattrs)
9ee6e8bb 11725{
452ef8cb
RH
11726 if (mmu_idx == ARMMMUIdx_E10_0 ||
11727 mmu_idx == ARMMMUIdx_E10_1 ||
11728 mmu_idx == ARMMMUIdx_E10_1_PAN) {
9b539263
EI
11729 /* Call ourselves recursively to do the stage 1 and then stage 2
11730 * translations.
0480f69a 11731 */
9b539263
EI
11732 if (arm_feature(env, ARM_FEATURE_EL2)) {
11733 hwaddr ipa;
11734 int s2_prot;
11735 int ret;
5b2d261d 11736 ARMCacheAttrs cacheattrs2 = {};
9b539263
EI
11737
11738 ret = get_phys_addr(env, address, access_type,
8bd5c820 11739 stage_1_mmu_idx(mmu_idx), &ipa, attrs,
bc52bfeb 11740 prot, page_size, fi, cacheattrs);
9b539263
EI
11741
11742 /* If S1 fails or S2 is disabled, return early. */
97fa9350 11743 if (ret || regime_translation_disabled(env, ARMMMUIdx_Stage2)) {
9b539263
EI
11744 *phys_ptr = ipa;
11745 return ret;
11746 }
11747
11748 /* S1 is done. Now do S2 translation. */
97fa9350 11749 ret = get_phys_addr_lpae(env, ipa, access_type, ARMMMUIdx_Stage2,
9b539263 11750 phys_ptr, attrs, &s2_prot,
da909b2c 11751 page_size, fi,
5b2d261d 11752 cacheattrs != NULL ? &cacheattrs2 : NULL);
9b539263
EI
11753 fi->s2addr = ipa;
11754 /* Combine the S1 and S2 perms. */
11755 *prot &= s2_prot;
5b2d261d
AB
11756
11757 /* Combine the S1 and S2 cache attributes, if needed */
11758 if (!ret && cacheattrs != NULL) {
9d1bab33
PM
11759 if (env->cp15.hcr_el2 & HCR_DC) {
11760 /*
11761 * HCR.DC forces the first stage attributes to
11762 * Normal Non-Shareable,
11763 * Inner Write-Back Read-Allocate Write-Allocate,
11764 * Outer Write-Back Read-Allocate Write-Allocate.
11765 */
11766 cacheattrs->attrs = 0xff;
11767 cacheattrs->shareability = 0;
11768 }
5b2d261d
AB
11769 *cacheattrs = combine_cacheattrs(*cacheattrs, cacheattrs2);
11770 }
11771
9b539263
EI
11772 return ret;
11773 } else {
11774 /*
11775 * For non-EL2 CPUs a stage1+stage2 translation is just stage 1.
11776 */
8bd5c820 11777 mmu_idx = stage_1_mmu_idx(mmu_idx);
9b539263 11778 }
0480f69a 11779 }
d3649702 11780
8bf5b6a9
PM
11781 /* The page table entries may downgrade secure to non-secure, but
11782 * cannot upgrade an non-secure translation regime's attributes
11783 * to secure.
11784 */
11785 attrs->secure = regime_is_secure(env, mmu_idx);
0995bf8c 11786 attrs->user = regime_is_user(env, mmu_idx);
8bf5b6a9 11787
0480f69a
PM
11788 /* Fast Context Switch Extension. This doesn't exist at all in v8.
11789 * In v7 and earlier it affects all stage 1 translations.
11790 */
97fa9350 11791 if (address < 0x02000000 && mmu_idx != ARMMMUIdx_Stage2
0480f69a
PM
11792 && !arm_feature(env, ARM_FEATURE_V8)) {
11793 if (regime_el(env, mmu_idx) == 3) {
11794 address += env->cp15.fcseidr_s;
11795 } else {
11796 address += env->cp15.fcseidr_ns;
11797 }
54bf36ed 11798 }
9ee6e8bb 11799
3279adb9 11800 if (arm_feature(env, ARM_FEATURE_PMSA)) {
c9f9f124 11801 bool ret;
f6bda88f 11802 *page_size = TARGET_PAGE_SIZE;
3279adb9 11803
504e3cc3
PM
11804 if (arm_feature(env, ARM_FEATURE_V8)) {
11805 /* PMSAv8 */
11806 ret = get_phys_addr_pmsav8(env, address, access_type, mmu_idx,
72042435 11807 phys_ptr, attrs, prot, page_size, fi);
504e3cc3 11808 } else if (arm_feature(env, ARM_FEATURE_V7)) {
3279adb9
PM
11809 /* PMSAv7 */
11810 ret = get_phys_addr_pmsav7(env, address, access_type, mmu_idx,
e5e40999 11811 phys_ptr, prot, page_size, fi);
3279adb9
PM
11812 } else {
11813 /* Pre-v7 MPU */
11814 ret = get_phys_addr_pmsav5(env, address, access_type, mmu_idx,
53a4e5c5 11815 phys_ptr, prot, fi);
3279adb9
PM
11816 }
11817 qemu_log_mask(CPU_LOG_MMU, "PMSA MPU lookup for %s at 0x%08" PRIx32
c9f9f124 11818 " mmu_idx %u -> %s (prot %c%c%c)\n",
709e4407
PM
11819 access_type == MMU_DATA_LOAD ? "reading" :
11820 (access_type == MMU_DATA_STORE ? "writing" : "execute"),
c9f9f124
MD
11821 (uint32_t)address, mmu_idx,
11822 ret ? "Miss" : "Hit",
11823 *prot & PAGE_READ ? 'r' : '-',
11824 *prot & PAGE_WRITE ? 'w' : '-',
11825 *prot & PAGE_EXEC ? 'x' : '-');
11826
11827 return ret;
f6bda88f
PC
11828 }
11829
3279adb9
PM
11830 /* Definitely a real MMU, not an MPU */
11831
0480f69a 11832 if (regime_translation_disabled(env, mmu_idx)) {
cebfb648
RH
11833 /*
11834 * MMU disabled. S1 addresses within aa64 translation regimes are
11835 * still checked for bounds -- see AArch64.TranslateAddressS1Off.
11836 */
11837 if (mmu_idx != ARMMMUIdx_Stage2) {
11838 int r_el = regime_el(env, mmu_idx);
11839 if (arm_el_is_aa64(env, r_el)) {
11840 int pamax = arm_pamax(env_archcpu(env));
11841 uint64_t tcr = env->cp15.tcr_el[r_el].raw_tcr;
11842 int addrtop, tbi;
11843
11844 tbi = aa64_va_parameter_tbi(tcr, mmu_idx);
11845 if (access_type == MMU_INST_FETCH) {
11846 tbi &= ~aa64_va_parameter_tbid(tcr, mmu_idx);
11847 }
11848 tbi = (tbi >> extract64(address, 55, 1)) & 1;
11849 addrtop = (tbi ? 55 : 63);
11850
11851 if (extract64(address, pamax, addrtop - pamax + 1) != 0) {
11852 fi->type = ARMFault_AddressSize;
11853 fi->level = 0;
11854 fi->stage2 = false;
11855 return 1;
11856 }
11857
11858 /*
11859 * When TBI is disabled, we've just validated that all of the
11860 * bits above PAMax are zero, so logically we only need to
11861 * clear the top byte for TBI. But it's clearer to follow
11862 * the pseudocode set of addrdesc.paddress.
11863 */
11864 address = extract64(address, 0, 52);
11865 }
11866 }
9ee6e8bb 11867 *phys_ptr = address;
3ad493fc 11868 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
d4c430a8 11869 *page_size = TARGET_PAGE_SIZE;
9ee6e8bb 11870 return 0;
0480f69a
PM
11871 }
11872
0480f69a 11873 if (regime_using_lpae_format(env, mmu_idx)) {
bc52bfeb
PM
11874 return get_phys_addr_lpae(env, address, access_type, mmu_idx,
11875 phys_ptr, attrs, prot, page_size,
11876 fi, cacheattrs);
0480f69a 11877 } else if (regime_sctlr(env, mmu_idx) & SCTLR_XP) {
bc52bfeb
PM
11878 return get_phys_addr_v6(env, address, access_type, mmu_idx,
11879 phys_ptr, attrs, prot, page_size, fi);
9ee6e8bb 11880 } else {
bc52bfeb 11881 return get_phys_addr_v5(env, address, access_type, mmu_idx,
f989983e 11882 phys_ptr, prot, page_size, fi);
9ee6e8bb
PB
11883 }
11884}
11885
0faea0c7
PM
11886hwaddr arm_cpu_get_phys_page_attrs_debug(CPUState *cs, vaddr addr,
11887 MemTxAttrs *attrs)
b5ff1b31 11888{
00b941e5 11889 ARMCPU *cpu = ARM_CPU(cs);
d3649702 11890 CPUARMState *env = &cpu->env;
a8170e5e 11891 hwaddr phys_addr;
d4c430a8 11892 target_ulong page_size;
b5ff1b31 11893 int prot;
b7cc4e82 11894 bool ret;
e14b5a23 11895 ARMMMUFaultInfo fi = {};
50494a27 11896 ARMMMUIdx mmu_idx = arm_mmu_idx(env);
b5ff1b31 11897
0faea0c7
PM
11898 *attrs = (MemTxAttrs) {};
11899
8bd5c820 11900 ret = get_phys_addr(env, addr, 0, mmu_idx, &phys_addr,
bc52bfeb 11901 attrs, &prot, &page_size, &fi, NULL);
b5ff1b31 11902
b7cc4e82 11903 if (ret) {
b5ff1b31 11904 return -1;
00b941e5 11905 }
b5ff1b31
FB
11906 return phys_addr;
11907}
11908
b5ff1b31 11909#endif
6ddbc6e4
PB
11910
11911/* Note that signed overflow is undefined in C. The following routines are
11912 careful to use unsigned types where modulo arithmetic is required.
11913 Failure to do so _will_ break on newer gcc. */
11914
11915/* Signed saturating arithmetic. */
11916
1654b2d6 11917/* Perform 16-bit signed saturating addition. */
6ddbc6e4
PB
11918static inline uint16_t add16_sat(uint16_t a, uint16_t b)
11919{
11920 uint16_t res;
11921
11922 res = a + b;
11923 if (((res ^ a) & 0x8000) && !((a ^ b) & 0x8000)) {
11924 if (a & 0x8000)
11925 res = 0x8000;
11926 else
11927 res = 0x7fff;
11928 }
11929 return res;
11930}
11931
1654b2d6 11932/* Perform 8-bit signed saturating addition. */
6ddbc6e4
PB
11933static inline uint8_t add8_sat(uint8_t a, uint8_t b)
11934{
11935 uint8_t res;
11936
11937 res = a + b;
11938 if (((res ^ a) & 0x80) && !((a ^ b) & 0x80)) {
11939 if (a & 0x80)
11940 res = 0x80;
11941 else
11942 res = 0x7f;
11943 }
11944 return res;
11945}
11946
1654b2d6 11947/* Perform 16-bit signed saturating subtraction. */
6ddbc6e4
PB
11948static inline uint16_t sub16_sat(uint16_t a, uint16_t b)
11949{
11950 uint16_t res;
11951
11952 res = a - b;
11953 if (((res ^ a) & 0x8000) && ((a ^ b) & 0x8000)) {
11954 if (a & 0x8000)
11955 res = 0x8000;
11956 else
11957 res = 0x7fff;
11958 }
11959 return res;
11960}
11961
1654b2d6 11962/* Perform 8-bit signed saturating subtraction. */
6ddbc6e4
PB
11963static inline uint8_t sub8_sat(uint8_t a, uint8_t b)
11964{
11965 uint8_t res;
11966
11967 res = a - b;
11968 if (((res ^ a) & 0x80) && ((a ^ b) & 0x80)) {
11969 if (a & 0x80)
11970 res = 0x80;
11971 else
11972 res = 0x7f;
11973 }
11974 return res;
11975}
11976
11977#define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16);
11978#define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16);
11979#define ADD8(a, b, n) RESULT(add8_sat(a, b), n, 8);
11980#define SUB8(a, b, n) RESULT(sub8_sat(a, b), n, 8);
11981#define PFX q
11982
11983#include "op_addsub.h"
11984
11985/* Unsigned saturating arithmetic. */
460a09c1 11986static inline uint16_t add16_usat(uint16_t a, uint16_t b)
6ddbc6e4
PB
11987{
11988 uint16_t res;
11989 res = a + b;
11990 if (res < a)
11991 res = 0xffff;
11992 return res;
11993}
11994
460a09c1 11995static inline uint16_t sub16_usat(uint16_t a, uint16_t b)
6ddbc6e4 11996{
4c4fd3f8 11997 if (a > b)
6ddbc6e4
PB
11998 return a - b;
11999 else
12000 return 0;
12001}
12002
12003static inline uint8_t add8_usat(uint8_t a, uint8_t b)
12004{
12005 uint8_t res;
12006 res = a + b;
12007 if (res < a)
12008 res = 0xff;
12009 return res;
12010}
12011
12012static inline uint8_t sub8_usat(uint8_t a, uint8_t b)
12013{
4c4fd3f8 12014 if (a > b)
6ddbc6e4
PB
12015 return a - b;
12016 else
12017 return 0;
12018}
12019
12020#define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16);
12021#define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16);
12022#define ADD8(a, b, n) RESULT(add8_usat(a, b), n, 8);
12023#define SUB8(a, b, n) RESULT(sub8_usat(a, b), n, 8);
12024#define PFX uq
12025
12026#include "op_addsub.h"
12027
12028/* Signed modulo arithmetic. */
12029#define SARITH16(a, b, n, op) do { \
12030 int32_t sum; \
db6e2e65 12031 sum = (int32_t)(int16_t)(a) op (int32_t)(int16_t)(b); \
6ddbc6e4
PB
12032 RESULT(sum, n, 16); \
12033 if (sum >= 0) \
12034 ge |= 3 << (n * 2); \
12035 } while(0)
12036
12037#define SARITH8(a, b, n, op) do { \
12038 int32_t sum; \
db6e2e65 12039 sum = (int32_t)(int8_t)(a) op (int32_t)(int8_t)(b); \
6ddbc6e4
PB
12040 RESULT(sum, n, 8); \
12041 if (sum >= 0) \
12042 ge |= 1 << n; \
12043 } while(0)
12044
12045
12046#define ADD16(a, b, n) SARITH16(a, b, n, +)
12047#define SUB16(a, b, n) SARITH16(a, b, n, -)
12048#define ADD8(a, b, n) SARITH8(a, b, n, +)
12049#define SUB8(a, b, n) SARITH8(a, b, n, -)
12050#define PFX s
12051#define ARITH_GE
12052
12053#include "op_addsub.h"
12054
12055/* Unsigned modulo arithmetic. */
12056#define ADD16(a, b, n) do { \
12057 uint32_t sum; \
12058 sum = (uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b); \
12059 RESULT(sum, n, 16); \
a87aa10b 12060 if ((sum >> 16) == 1) \
6ddbc6e4
PB
12061 ge |= 3 << (n * 2); \
12062 } while(0)
12063
12064#define ADD8(a, b, n) do { \
12065 uint32_t sum; \
12066 sum = (uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b); \
12067 RESULT(sum, n, 8); \
a87aa10b
AZ
12068 if ((sum >> 8) == 1) \
12069 ge |= 1 << n; \
6ddbc6e4
PB
12070 } while(0)
12071
12072#define SUB16(a, b, n) do { \
12073 uint32_t sum; \
12074 sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \
12075 RESULT(sum, n, 16); \
12076 if ((sum >> 16) == 0) \
12077 ge |= 3 << (n * 2); \
12078 } while(0)
12079
12080#define SUB8(a, b, n) do { \
12081 uint32_t sum; \
12082 sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \
12083 RESULT(sum, n, 8); \
12084 if ((sum >> 8) == 0) \
a87aa10b 12085 ge |= 1 << n; \
6ddbc6e4
PB
12086 } while(0)
12087
12088#define PFX u
12089#define ARITH_GE
12090
12091#include "op_addsub.h"
12092
12093/* Halved signed arithmetic. */
12094#define ADD16(a, b, n) \
12095 RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16)
12096#define SUB16(a, b, n) \
12097 RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16)
12098#define ADD8(a, b, n) \
12099 RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8)
12100#define SUB8(a, b, n) \
12101 RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8)
12102#define PFX sh
12103
12104#include "op_addsub.h"
12105
12106/* Halved unsigned arithmetic. */
12107#define ADD16(a, b, n) \
12108 RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16)
12109#define SUB16(a, b, n) \
12110 RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16)
12111#define ADD8(a, b, n) \
12112 RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8)
12113#define SUB8(a, b, n) \
12114 RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8)
12115#define PFX uh
12116
12117#include "op_addsub.h"
12118
12119static inline uint8_t do_usad(uint8_t a, uint8_t b)
12120{
12121 if (a > b)
12122 return a - b;
12123 else
12124 return b - a;
12125}
12126
12127/* Unsigned sum of absolute byte differences. */
12128uint32_t HELPER(usad8)(uint32_t a, uint32_t b)
12129{
12130 uint32_t sum;
12131 sum = do_usad(a, b);
12132 sum += do_usad(a >> 8, b >> 8);
12133 sum += do_usad(a >> 16, b >>16);
12134 sum += do_usad(a >> 24, b >> 24);
12135 return sum;
12136}
12137
12138/* For ARMv6 SEL instruction. */
12139uint32_t HELPER(sel_flags)(uint32_t flags, uint32_t a, uint32_t b)
12140{
12141 uint32_t mask;
12142
12143 mask = 0;
12144 if (flags & 1)
12145 mask |= 0xff;
12146 if (flags & 2)
12147 mask |= 0xff00;
12148 if (flags & 4)
12149 mask |= 0xff0000;
12150 if (flags & 8)
12151 mask |= 0xff000000;
12152 return (a & mask) | (b & ~mask);
12153}
12154
aa633469
PM
12155/* CRC helpers.
12156 * The upper bytes of val (above the number specified by 'bytes') must have
12157 * been zeroed out by the caller.
12158 */
eb0ecd5a
WN
12159uint32_t HELPER(crc32)(uint32_t acc, uint32_t val, uint32_t bytes)
12160{
12161 uint8_t buf[4];
12162
aa633469 12163 stl_le_p(buf, val);
eb0ecd5a
WN
12164
12165 /* zlib crc32 converts the accumulator and output to one's complement. */
12166 return crc32(acc ^ 0xffffffff, buf, bytes) ^ 0xffffffff;
12167}
12168
12169uint32_t HELPER(crc32c)(uint32_t acc, uint32_t val, uint32_t bytes)
12170{
12171 uint8_t buf[4];
12172
aa633469 12173 stl_le_p(buf, val);
eb0ecd5a
WN
12174
12175 /* Linux crc32c converts the output to one's complement. */
12176 return crc32c(acc, buf, bytes) ^ 0xffffffff;
12177}
a9e01311
RH
12178
12179/* Return the exception level to which FP-disabled exceptions should
12180 * be taken, or 0 if FP is enabled.
12181 */
ced31551 12182int fp_exception_el(CPUARMState *env, int cur_el)
a9e01311 12183{
55faa212 12184#ifndef CONFIG_USER_ONLY
a9e01311
RH
12185 /* CPACR and the CPTR registers don't exist before v6, so FP is
12186 * always accessible
12187 */
12188 if (!arm_feature(env, ARM_FEATURE_V6)) {
12189 return 0;
12190 }
12191
d87513c0
PM
12192 if (arm_feature(env, ARM_FEATURE_M)) {
12193 /* CPACR can cause a NOCP UsageFault taken to current security state */
12194 if (!v7m_cpacr_pass(env, env->v7m.secure, cur_el != 0)) {
12195 return 1;
12196 }
12197
12198 if (arm_feature(env, ARM_FEATURE_M_SECURITY) && !env->v7m.secure) {
12199 if (!extract32(env->v7m.nsacr, 10, 1)) {
12200 /* FP insns cause a NOCP UsageFault taken to Secure */
12201 return 3;
12202 }
12203 }
12204
12205 return 0;
12206 }
12207
a9e01311
RH
12208 /* The CPACR controls traps to EL1, or PL1 if we're 32 bit:
12209 * 0, 2 : trap EL0 and EL1/PL1 accesses
12210 * 1 : trap only EL0 accesses
12211 * 3 : trap no accesses
c2ddb7cf 12212 * This register is ignored if E2H+TGE are both set.
a9e01311 12213 */
c2ddb7cf
RH
12214 if ((arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
12215 int fpen = extract32(env->cp15.cpacr_el1, 20, 2);
12216
12217 switch (fpen) {
12218 case 0:
12219 case 2:
12220 if (cur_el == 0 || cur_el == 1) {
12221 /* Trap to PL1, which might be EL1 or EL3 */
12222 if (arm_is_secure(env) && !arm_el_is_aa64(env, 3)) {
12223 return 3;
12224 }
12225 return 1;
12226 }
12227 if (cur_el == 3 && !is_a64(env)) {
12228 /* Secure PL1 running at EL3 */
a9e01311
RH
12229 return 3;
12230 }
c2ddb7cf
RH
12231 break;
12232 case 1:
12233 if (cur_el == 0) {
12234 return 1;
12235 }
12236 break;
12237 case 3:
12238 break;
a9e01311 12239 }
a9e01311
RH
12240 }
12241
fc1120a7
PM
12242 /*
12243 * The NSACR allows A-profile AArch32 EL3 and M-profile secure mode
12244 * to control non-secure access to the FPU. It doesn't have any
12245 * effect if EL3 is AArch64 or if EL3 doesn't exist at all.
12246 */
12247 if ((arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
12248 cur_el <= 2 && !arm_is_secure_below_el3(env))) {
12249 if (!extract32(env->cp15.nsacr, 10, 1)) {
12250 /* FP insns act as UNDEF */
12251 return cur_el == 2 ? 2 : 1;
12252 }
12253 }
12254
a9e01311
RH
12255 /* For the CPTR registers we don't need to guard with an ARM_FEATURE
12256 * check because zero bits in the registers mean "don't trap".
12257 */
12258
12259 /* CPTR_EL2 : present in v7VE or v8 */
12260 if (cur_el <= 2 && extract32(env->cp15.cptr_el[2], 10, 1)
12261 && !arm_is_secure_below_el3(env)) {
12262 /* Trap FP ops at EL2, NS-EL1 or NS-EL0 to EL2 */
12263 return 2;
12264 }
12265
12266 /* CPTR_EL3 : present in v8 */
12267 if (extract32(env->cp15.cptr_el[3], 10, 1)) {
12268 /* Trap all FP ops to EL3 */
12269 return 3;
12270 }
55faa212 12271#endif
a9e01311
RH
12272 return 0;
12273}
12274
b9f6033c
RH
12275/* Return the exception level we're running at if this is our mmu_idx */
12276int arm_mmu_idx_to_el(ARMMMUIdx mmu_idx)
12277{
12278 if (mmu_idx & ARM_MMU_IDX_M) {
12279 return mmu_idx & ARM_MMU_IDX_M_PRIV;
12280 }
12281
12282 switch (mmu_idx) {
12283 case ARMMMUIdx_E10_0:
12284 case ARMMMUIdx_E20_0:
12285 case ARMMMUIdx_SE10_0:
12286 return 0;
12287 case ARMMMUIdx_E10_1:
452ef8cb 12288 case ARMMMUIdx_E10_1_PAN:
b9f6033c 12289 case ARMMMUIdx_SE10_1:
452ef8cb 12290 case ARMMMUIdx_SE10_1_PAN:
b9f6033c
RH
12291 return 1;
12292 case ARMMMUIdx_E2:
12293 case ARMMMUIdx_E20_2:
452ef8cb 12294 case ARMMMUIdx_E20_2_PAN:
b9f6033c
RH
12295 return 2;
12296 case ARMMMUIdx_SE3:
12297 return 3;
12298 default:
12299 g_assert_not_reached();
12300 }
12301}
12302
7aab5a8c 12303#ifndef CONFIG_TCG
65e4655c
RH
12304ARMMMUIdx arm_v7m_mmu_idx_for_secstate(CPUARMState *env, bool secstate)
12305{
7aab5a8c 12306 g_assert_not_reached();
65e4655c 12307}
7aab5a8c 12308#endif
65e4655c 12309
164690b2 12310ARMMMUIdx arm_mmu_idx_el(CPUARMState *env, int el)
65e4655c 12311{
65e4655c 12312 if (arm_feature(env, ARM_FEATURE_M)) {
50494a27 12313 return arm_v7m_mmu_idx_for_secstate(env, env->v7m.secure);
65e4655c
RH
12314 }
12315
6003d980 12316 /* See ARM pseudo-function ELIsInHost. */
b9f6033c
RH
12317 switch (el) {
12318 case 0:
b9f6033c
RH
12319 if (arm_is_secure_below_el3(env)) {
12320 return ARMMMUIdx_SE10_0;
12321 }
6003d980
RH
12322 if ((env->cp15.hcr_el2 & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)
12323 && arm_el_is_aa64(env, 2)) {
12324 return ARMMMUIdx_E20_0;
12325 }
b9f6033c
RH
12326 return ARMMMUIdx_E10_0;
12327 case 1:
12328 if (arm_is_secure_below_el3(env)) {
66412260
RH
12329 if (env->pstate & PSTATE_PAN) {
12330 return ARMMMUIdx_SE10_1_PAN;
12331 }
b9f6033c
RH
12332 return ARMMMUIdx_SE10_1;
12333 }
66412260
RH
12334 if (env->pstate & PSTATE_PAN) {
12335 return ARMMMUIdx_E10_1_PAN;
12336 }
b9f6033c
RH
12337 return ARMMMUIdx_E10_1;
12338 case 2:
b9f6033c 12339 /* TODO: ARMv8.4-SecEL2 */
6003d980
RH
12340 /* Note that TGE does not apply at EL2. */
12341 if ((env->cp15.hcr_el2 & HCR_E2H) && arm_el_is_aa64(env, 2)) {
66412260
RH
12342 if (env->pstate & PSTATE_PAN) {
12343 return ARMMMUIdx_E20_2_PAN;
12344 }
6003d980
RH
12345 return ARMMMUIdx_E20_2;
12346 }
b9f6033c
RH
12347 return ARMMMUIdx_E2;
12348 case 3:
12349 return ARMMMUIdx_SE3;
12350 default:
12351 g_assert_not_reached();
65e4655c 12352 }
50494a27
RH
12353}
12354
164690b2
RH
12355ARMMMUIdx arm_mmu_idx(CPUARMState *env)
12356{
12357 return arm_mmu_idx_el(env, arm_current_el(env));
12358}
12359
64be86ab
RH
12360#ifndef CONFIG_USER_ONLY
12361ARMMMUIdx arm_stage1_mmu_idx(CPUARMState *env)
12362{
12363 return stage_1_mmu_idx(arm_mmu_idx(env));
12364}
12365#endif
12366
fdd1b228
RH
12367static uint32_t rebuild_hflags_common(CPUARMState *env, int fp_el,
12368 ARMMMUIdx mmu_idx, uint32_t flags)
12369{
12370 flags = FIELD_DP32(flags, TBFLAG_ANY, FPEXC_EL, fp_el);
12371 flags = FIELD_DP32(flags, TBFLAG_ANY, MMUIDX,
12372 arm_to_core_mmu_idx(mmu_idx));
12373
fdd1b228
RH
12374 if (arm_singlestep_active(env)) {
12375 flags = FIELD_DP32(flags, TBFLAG_ANY, SS_ACTIVE, 1);
12376 }
12377 return flags;
12378}
12379
43eccfb6
RH
12380static uint32_t rebuild_hflags_common_32(CPUARMState *env, int fp_el,
12381 ARMMMUIdx mmu_idx, uint32_t flags)
12382{
8061a649
RH
12383 bool sctlr_b = arm_sctlr_b(env);
12384
12385 if (sctlr_b) {
12386 flags = FIELD_DP32(flags, TBFLAG_A32, SCTLR_B, 1);
12387 }
12388 if (arm_cpu_data_is_big_endian_a32(env, sctlr_b)) {
12389 flags = FIELD_DP32(flags, TBFLAG_ANY, BE_DATA, 1);
12390 }
43eccfb6
RH
12391 flags = FIELD_DP32(flags, TBFLAG_A32, NS, !access_secure_reg(env));
12392
12393 return rebuild_hflags_common(env, fp_el, mmu_idx, flags);
12394}
12395
6e33ced5
RH
12396static uint32_t rebuild_hflags_m32(CPUARMState *env, int fp_el,
12397 ARMMMUIdx mmu_idx)
12398{
12399 uint32_t flags = 0;
12400
12401 if (arm_v7m_is_handler_mode(env)) {
79cabf1f 12402 flags = FIELD_DP32(flags, TBFLAG_M32, HANDLER, 1);
6e33ced5
RH
12403 }
12404
12405 /*
12406 * v8M always applies stack limit checks unless CCR.STKOFHFNMIGN
12407 * is suppressing them because the requested execution priority
12408 * is less than 0.
12409 */
12410 if (arm_feature(env, ARM_FEATURE_V8) &&
12411 !((mmu_idx & ARM_MMU_IDX_M_NEGPRI) &&
12412 (env->v7m.ccr[env->v7m.secure] & R_V7M_CCR_STKOFHFNMIGN_MASK))) {
79cabf1f 12413 flags = FIELD_DP32(flags, TBFLAG_M32, STACKCHECK, 1);
6e33ced5
RH
12414 }
12415
12416 return rebuild_hflags_common_32(env, fp_el, mmu_idx, flags);
12417}
12418
83f4baef
RH
12419static uint32_t rebuild_hflags_aprofile(CPUARMState *env)
12420{
12421 int flags = 0;
12422
12423 flags = FIELD_DP32(flags, TBFLAG_ANY, DEBUG_TARGET_EL,
12424 arm_debug_target_el(env));
12425 return flags;
12426}
12427
c747224c
RH
12428static uint32_t rebuild_hflags_a32(CPUARMState *env, int fp_el,
12429 ARMMMUIdx mmu_idx)
12430{
83f4baef 12431 uint32_t flags = rebuild_hflags_aprofile(env);
0a54d68e
RH
12432
12433 if (arm_el_is_aa64(env, 1)) {
12434 flags = FIELD_DP32(flags, TBFLAG_A32, VFPEN, 1);
12435 }
5bb0a20b
MZ
12436
12437 if (arm_current_el(env) < 2 && env->cp15.hstr_el2 &&
12438 (arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
12439 flags = FIELD_DP32(flags, TBFLAG_A32, HSTR_ACTIVE, 1);
12440 }
12441
83f4baef 12442 return rebuild_hflags_common_32(env, fp_el, mmu_idx, flags);
c747224c
RH
12443}
12444
d4d7503a
RH
12445static uint32_t rebuild_hflags_a64(CPUARMState *env, int el, int fp_el,
12446 ARMMMUIdx mmu_idx)
a9e01311 12447{
83f4baef 12448 uint32_t flags = rebuild_hflags_aprofile(env);
d4d7503a 12449 ARMMMUIdx stage1 = stage_1_mmu_idx(mmu_idx);
b830a5ee 12450 uint64_t tcr = regime_tcr(env, mmu_idx)->raw_tcr;
d4d7503a
RH
12451 uint64_t sctlr;
12452 int tbii, tbid;
b9adaa70 12453
d4d7503a 12454 flags = FIELD_DP32(flags, TBFLAG_ANY, AARCH64_STATE, 1);
cd208a1c 12455
339370b9 12456 /* Get control bits for tagged addresses. */
b830a5ee
RH
12457 tbid = aa64_va_parameter_tbi(tcr, mmu_idx);
12458 tbii = tbid & ~aa64_va_parameter_tbid(tcr, mmu_idx);
5d8634f5 12459
d4d7503a
RH
12460 flags = FIELD_DP32(flags, TBFLAG_A64, TBII, tbii);
12461 flags = FIELD_DP32(flags, TBFLAG_A64, TBID, tbid);
12462
12463 if (cpu_isar_feature(aa64_sve, env_archcpu(env))) {
12464 int sve_el = sve_exception_el(env, el);
12465 uint32_t zcr_len;
5d8634f5 12466
d4d7503a
RH
12467 /*
12468 * If SVE is disabled, but FP is enabled,
12469 * then the effective len is 0.
12470 */
12471 if (sve_el != 0 && fp_el == 0) {
12472 zcr_len = 0;
12473 } else {
12474 zcr_len = sve_zcr_len_for_el(env, el);
5d8634f5 12475 }
d4d7503a
RH
12476 flags = FIELD_DP32(flags, TBFLAG_A64, SVEEXC_EL, sve_el);
12477 flags = FIELD_DP32(flags, TBFLAG_A64, ZCR_LEN, zcr_len);
12478 }
1db5e96c 12479
aaec1432 12480 sctlr = regime_sctlr(env, stage1);
1db5e96c 12481
8061a649
RH
12482 if (arm_cpu_data_is_big_endian_a64(el, sctlr)) {
12483 flags = FIELD_DP32(flags, TBFLAG_ANY, BE_DATA, 1);
12484 }
12485
d4d7503a
RH
12486 if (cpu_isar_feature(aa64_pauth, env_archcpu(env))) {
12487 /*
12488 * In order to save space in flags, we record only whether
12489 * pauth is "inactive", meaning all insns are implemented as
12490 * a nop, or "active" when some action must be performed.
12491 * The decision of which action to take is left to a helper.
12492 */
12493 if (sctlr & (SCTLR_EnIA | SCTLR_EnIB | SCTLR_EnDA | SCTLR_EnDB)) {
12494 flags = FIELD_DP32(flags, TBFLAG_A64, PAUTH_ACTIVE, 1);
1db5e96c 12495 }
d4d7503a 12496 }
0816ef1b 12497
d4d7503a
RH
12498 if (cpu_isar_feature(aa64_bti, env_archcpu(env))) {
12499 /* Note that SCTLR_EL[23].BT == SCTLR_BT1. */
12500 if (sctlr & (el == 0 ? SCTLR_BT0 : SCTLR_BT1)) {
12501 flags = FIELD_DP32(flags, TBFLAG_A64, BT, 1);
0816ef1b 12502 }
d4d7503a 12503 }
08f1434a 12504
cc28fc30 12505 /* Compute the condition for using AccType_UNPRIV for LDTR et al. */
7a8014ab
RH
12506 if (!(env->pstate & PSTATE_UAO)) {
12507 switch (mmu_idx) {
12508 case ARMMMUIdx_E10_1:
12509 case ARMMMUIdx_E10_1_PAN:
12510 case ARMMMUIdx_SE10_1:
12511 case ARMMMUIdx_SE10_1_PAN:
12512 /* TODO: ARMv8.3-NV */
cc28fc30 12513 flags = FIELD_DP32(flags, TBFLAG_A64, UNPRIV, 1);
7a8014ab
RH
12514 break;
12515 case ARMMMUIdx_E20_2:
12516 case ARMMMUIdx_E20_2_PAN:
12517 /* TODO: ARMv8.4-SecEL2 */
12518 /*
12519 * Note that EL20_2 is gated by HCR_EL2.E2H == 1, but EL20_0 is
12520 * gated by HCR_EL2.<E2H,TGE> == '11', and so is LDTR.
12521 */
12522 if (env->cp15.hcr_el2 & HCR_TGE) {
12523 flags = FIELD_DP32(flags, TBFLAG_A64, UNPRIV, 1);
12524 }
12525 break;
12526 default:
12527 break;
cc28fc30 12528 }
cc28fc30
RH
12529 }
12530
d4d7503a
RH
12531 return rebuild_hflags_common(env, fp_el, mmu_idx, flags);
12532}
12533
3d74e2e9
RH
12534static uint32_t rebuild_hflags_internal(CPUARMState *env)
12535{
12536 int el = arm_current_el(env);
12537 int fp_el = fp_exception_el(env, el);
164690b2 12538 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
3d74e2e9
RH
12539
12540 if (is_a64(env)) {
12541 return rebuild_hflags_a64(env, el, fp_el, mmu_idx);
12542 } else if (arm_feature(env, ARM_FEATURE_M)) {
12543 return rebuild_hflags_m32(env, fp_el, mmu_idx);
12544 } else {
12545 return rebuild_hflags_a32(env, fp_el, mmu_idx);
12546 }
12547}
12548
12549void arm_rebuild_hflags(CPUARMState *env)
12550{
12551 env->hflags = rebuild_hflags_internal(env);
12552}
12553
19717e9b
PM
12554/*
12555 * If we have triggered a EL state change we can't rely on the
12556 * translator having passed it to us, we need to recompute.
12557 */
12558void HELPER(rebuild_hflags_m32_newel)(CPUARMState *env)
12559{
12560 int el = arm_current_el(env);
12561 int fp_el = fp_exception_el(env, el);
12562 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
12563 env->hflags = rebuild_hflags_m32(env, fp_el, mmu_idx);
12564}
12565
14f3c588
RH
12566void HELPER(rebuild_hflags_m32)(CPUARMState *env, int el)
12567{
12568 int fp_el = fp_exception_el(env, el);
12569 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
12570
12571 env->hflags = rebuild_hflags_m32(env, fp_el, mmu_idx);
12572}
12573
f80741d1
AB
12574/*
12575 * If we have triggered a EL state change we can't rely on the
563152e0 12576 * translator having passed it to us, we need to recompute.
f80741d1
AB
12577 */
12578void HELPER(rebuild_hflags_a32_newel)(CPUARMState *env)
12579{
12580 int el = arm_current_el(env);
12581 int fp_el = fp_exception_el(env, el);
12582 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
12583 env->hflags = rebuild_hflags_a32(env, fp_el, mmu_idx);
12584}
12585
14f3c588
RH
12586void HELPER(rebuild_hflags_a32)(CPUARMState *env, int el)
12587{
12588 int fp_el = fp_exception_el(env, el);
12589 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
12590
12591 env->hflags = rebuild_hflags_a32(env, fp_el, mmu_idx);
12592}
12593
12594void HELPER(rebuild_hflags_a64)(CPUARMState *env, int el)
12595{
12596 int fp_el = fp_exception_el(env, el);
12597 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
12598
12599 env->hflags = rebuild_hflags_a64(env, el, fp_el, mmu_idx);
12600}
12601
0ee8b24a
PMD
12602static inline void assert_hflags_rebuild_correctly(CPUARMState *env)
12603{
12604#ifdef CONFIG_DEBUG_TCG
12605 uint32_t env_flags_current = env->hflags;
12606 uint32_t env_flags_rebuilt = rebuild_hflags_internal(env);
12607
12608 if (unlikely(env_flags_current != env_flags_rebuilt)) {
12609 fprintf(stderr, "TCG hflags mismatch (current:0x%08x rebuilt:0x%08x)\n",
12610 env_flags_current, env_flags_rebuilt);
12611 abort();
12612 }
12613#endif
12614}
12615
d4d7503a
RH
12616void cpu_get_tb_cpu_state(CPUARMState *env, target_ulong *pc,
12617 target_ulong *cs_base, uint32_t *pflags)
12618{
e979972a
RH
12619 uint32_t flags = env->hflags;
12620 uint32_t pstate_for_ss;
d4d7503a 12621
9b253fe5 12622 *cs_base = 0;
0ee8b24a 12623 assert_hflags_rebuild_correctly(env);
3d74e2e9 12624
e979972a 12625 if (FIELD_EX32(flags, TBFLAG_ANY, AARCH64_STATE)) {
d4d7503a 12626 *pc = env->pc;
d4d7503a 12627 if (cpu_isar_feature(aa64_bti, env_archcpu(env))) {
08f1434a
RH
12628 flags = FIELD_DP32(flags, TBFLAG_A64, BTYPE, env->btype);
12629 }
60e12c37 12630 pstate_for_ss = env->pstate;
a9e01311
RH
12631 } else {
12632 *pc = env->regs[15];
6e33ced5
RH
12633
12634 if (arm_feature(env, ARM_FEATURE_M)) {
9550d1bd
RH
12635 if (arm_feature(env, ARM_FEATURE_M_SECURITY) &&
12636 FIELD_EX32(env->v7m.fpccr[M_REG_S], V7M_FPCCR, S)
12637 != env->v7m.secure) {
79cabf1f 12638 flags = FIELD_DP32(flags, TBFLAG_M32, FPCCR_S_WRONG, 1);
9550d1bd
RH
12639 }
12640
12641 if ((env->v7m.fpccr[env->v7m.secure] & R_V7M_FPCCR_ASPEN_MASK) &&
12642 (!(env->v7m.control[M_REG_S] & R_V7M_CONTROL_FPCA_MASK) ||
12643 (env->v7m.secure &&
12644 !(env->v7m.control[M_REG_S] & R_V7M_CONTROL_SFPA_MASK)))) {
12645 /*
12646 * ASPEN is set, but FPCA/SFPA indicate that there is no
12647 * active FP context; we must create a new FP context before
12648 * executing any FP insn.
12649 */
79cabf1f 12650 flags = FIELD_DP32(flags, TBFLAG_M32, NEW_FP_CTXT_NEEDED, 1);
9550d1bd
RH
12651 }
12652
12653 bool is_secure = env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_S_MASK;
12654 if (env->v7m.fpccr[is_secure] & R_V7M_FPCCR_LSPACT_MASK) {
79cabf1f 12655 flags = FIELD_DP32(flags, TBFLAG_M32, LSPACT, 1);
9550d1bd 12656 }
6e33ced5 12657 } else {
bbad7c62
RH
12658 /*
12659 * Note that XSCALE_CPAR shares bits with VECSTRIDE.
12660 * Note that VECLEN+VECSTRIDE are RES0 for M-profile.
12661 */
12662 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
12663 flags = FIELD_DP32(flags, TBFLAG_A32,
12664 XSCALE_CPAR, env->cp15.c15_cpar);
12665 } else {
12666 flags = FIELD_DP32(flags, TBFLAG_A32, VECLEN,
12667 env->vfp.vec_len);
12668 flags = FIELD_DP32(flags, TBFLAG_A32, VECSTRIDE,
12669 env->vfp.vec_stride);
12670 }
0a54d68e
RH
12671 if (env->vfp.xregs[ARM_VFP_FPEXC] & (1 << 30)) {
12672 flags = FIELD_DP32(flags, TBFLAG_A32, VFPEN, 1);
12673 }
6e33ced5
RH
12674 }
12675
79cabf1f
RH
12676 flags = FIELD_DP32(flags, TBFLAG_AM32, THUMB, env->thumb);
12677 flags = FIELD_DP32(flags, TBFLAG_AM32, CONDEXEC, env->condexec_bits);
60e12c37 12678 pstate_for_ss = env->uncached_cpsr;
d4d7503a 12679 }
a9e01311 12680
60e12c37
RH
12681 /*
12682 * The SS_ACTIVE and PSTATE_SS bits correspond to the state machine
a9e01311
RH
12683 * states defined in the ARM ARM for software singlestep:
12684 * SS_ACTIVE PSTATE.SS State
12685 * 0 x Inactive (the TB flag for SS is always 0)
12686 * 1 0 Active-pending
12687 * 1 1 Active-not-pending
fdd1b228 12688 * SS_ACTIVE is set in hflags; PSTATE_SS is computed every TB.
a9e01311 12689 */
60e12c37
RH
12690 if (FIELD_EX32(flags, TBFLAG_ANY, SS_ACTIVE) &&
12691 (pstate_for_ss & PSTATE_SS)) {
12692 flags = FIELD_DP32(flags, TBFLAG_ANY, PSTATE_SS, 1);
a9e01311 12693 }
a9e01311 12694
b9adaa70 12695 *pflags = flags;
a9e01311 12696}
0ab5953b
RH
12697
12698#ifdef TARGET_AARCH64
12699/*
12700 * The manual says that when SVE is enabled and VQ is widened the
12701 * implementation is allowed to zero the previously inaccessible
12702 * portion of the registers. The corollary to that is that when
12703 * SVE is enabled and VQ is narrowed we are also allowed to zero
12704 * the now inaccessible portion of the registers.
12705 *
12706 * The intent of this is that no predicate bit beyond VQ is ever set.
12707 * Which means that some operations on predicate registers themselves
12708 * may operate on full uint64_t or even unrolled across the maximum
12709 * uint64_t[4]. Performing 4 bits of host arithmetic unconditionally
12710 * may well be cheaper than conditionals to restrict the operation
12711 * to the relevant portion of a uint16_t[16].
12712 */
12713void aarch64_sve_narrow_vq(CPUARMState *env, unsigned vq)
12714{
12715 int i, j;
12716 uint64_t pmask;
12717
12718 assert(vq >= 1 && vq <= ARM_MAX_VQ);
2fc0cc0e 12719 assert(vq <= env_archcpu(env)->sve_max_vq);
0ab5953b
RH
12720
12721 /* Zap the high bits of the zregs. */
12722 for (i = 0; i < 32; i++) {
12723 memset(&env->vfp.zregs[i].d[2 * vq], 0, 16 * (ARM_MAX_VQ - vq));
12724 }
12725
12726 /* Zap the high bits of the pregs and ffr. */
12727 pmask = 0;
12728 if (vq & 3) {
12729 pmask = ~(-1ULL << (16 * (vq & 3)));
12730 }
12731 for (j = vq / 4; j < ARM_MAX_VQ / 4; j++) {
12732 for (i = 0; i < 17; ++i) {
12733 env->vfp.pregs[i].p[j] &= pmask;
12734 }
12735 pmask = 0;
12736 }
12737}
12738
12739/*
12740 * Notice a change in SVE vector size when changing EL.
12741 */
9a05f7b6
RH
12742void aarch64_sve_change_el(CPUARMState *env, int old_el,
12743 int new_el, bool el0_a64)
0ab5953b 12744{
2fc0cc0e 12745 ARMCPU *cpu = env_archcpu(env);
0ab5953b 12746 int old_len, new_len;
9a05f7b6 12747 bool old_a64, new_a64;
0ab5953b
RH
12748
12749 /* Nothing to do if no SVE. */
cd208a1c 12750 if (!cpu_isar_feature(aa64_sve, cpu)) {
0ab5953b
RH
12751 return;
12752 }
12753
12754 /* Nothing to do if FP is disabled in either EL. */
12755 if (fp_exception_el(env, old_el) || fp_exception_el(env, new_el)) {
12756 return;
12757 }
12758
12759 /*
12760 * DDI0584A.d sec 3.2: "If SVE instructions are disabled or trapped
12761 * at ELx, or not available because the EL is in AArch32 state, then
12762 * for all purposes other than a direct read, the ZCR_ELx.LEN field
12763 * has an effective value of 0".
12764 *
12765 * Consider EL2 (aa64, vq=4) -> EL0 (aa32) -> EL1 (aa64, vq=0).
12766 * If we ignore aa32 state, we would fail to see the vq4->vq0 transition
12767 * from EL2->EL1. Thus we go ahead and narrow when entering aa32 so that
12768 * we already have the correct register contents when encountering the
12769 * vq0->vq0 transition between EL0->EL1.
12770 */
9a05f7b6
RH
12771 old_a64 = old_el ? arm_el_is_aa64(env, old_el) : el0_a64;
12772 old_len = (old_a64 && !sve_exception_el(env, old_el)
0ab5953b 12773 ? sve_zcr_len_for_el(env, old_el) : 0);
9a05f7b6
RH
12774 new_a64 = new_el ? arm_el_is_aa64(env, new_el) : el0_a64;
12775 new_len = (new_a64 && !sve_exception_el(env, new_el)
0ab5953b
RH
12776 ? sve_zcr_len_for_el(env, new_el) : 0);
12777
12778 /* When changing vector length, clear inaccessible state. */
12779 if (new_len < old_len) {
12780 aarch64_sve_narrow_vq(env, new_len + 1);
12781 }
12782}
12783#endif