]> git.ipfire.org Git - thirdparty/qemu.git/blame - target/arm/helper.c
target/arm: Honor the HCR_EL2.{TVM,TRVM} bits
[thirdparty/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
0ecb72a5 51static int vfp_gdb_get_reg(CPUARMState *env, uint8_t *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) {
9a2b5256 58 stq_le_p(buf, *aa32_vfp_dreg(env, reg));
56aebc89
PB
59 return 8;
60 }
61 if (arm_feature(env, ARM_FEATURE_NEON)) {
62 /* Aliases for Q regs. */
63 nregs += 16;
64 if (reg < nregs) {
9a2b5256
RH
65 uint64_t *q = aa32_vfp_qreg(env, reg - 32);
66 stq_le_p(buf, q[0]);
67 stq_le_p(buf + 8, q[1]);
56aebc89
PB
68 return 16;
69 }
70 }
71 switch (reg - nregs) {
72 case 0: stl_p(buf, env->vfp.xregs[ARM_VFP_FPSID]); return 4;
b0a909a4 73 case 1: stl_p(buf, vfp_get_fpscr(env)); return 4;
56aebc89
PB
74 case 2: stl_p(buf, env->vfp.xregs[ARM_VFP_FPEXC]); return 4;
75 }
76 return 0;
77}
78
0ecb72a5 79static int vfp_gdb_set_reg(CPUARMState *env, uint8_t *buf, int reg)
56aebc89 80{
a6627f5f
RH
81 ARMCPU *cpu = env_archcpu(env);
82 int nregs = cpu_isar_feature(aa32_simd_r32, cpu) ? 32 : 16;
56aebc89 83
56aebc89 84 if (reg < nregs) {
9a2b5256 85 *aa32_vfp_dreg(env, reg) = ldq_le_p(buf);
56aebc89
PB
86 return 8;
87 }
88 if (arm_feature(env, ARM_FEATURE_NEON)) {
89 nregs += 16;
90 if (reg < nregs) {
9a2b5256
RH
91 uint64_t *q = aa32_vfp_qreg(env, reg - 32);
92 q[0] = ldq_le_p(buf);
93 q[1] = ldq_le_p(buf + 8);
56aebc89
PB
94 return 16;
95 }
96 }
97 switch (reg - nregs) {
98 case 0: env->vfp.xregs[ARM_VFP_FPSID] = ldl_p(buf); return 4;
b0a909a4 99 case 1: vfp_set_fpscr(env, ldl_p(buf)); return 4;
71b3c3de 100 case 2: env->vfp.xregs[ARM_VFP_FPEXC] = ldl_p(buf) & (1 << 30); return 4;
56aebc89
PB
101 }
102 return 0;
103}
104
6a669427
PM
105static int aarch64_fpu_gdb_get_reg(CPUARMState *env, uint8_t *buf, int reg)
106{
107 switch (reg) {
108 case 0 ... 31:
109 /* 128 bit FP register */
9a2b5256
RH
110 {
111 uint64_t *q = aa64_vfp_qreg(env, reg);
112 stq_le_p(buf, q[0]);
113 stq_le_p(buf + 8, q[1]);
114 return 16;
115 }
6a669427
PM
116 case 32:
117 /* FPSR */
118 stl_p(buf, vfp_get_fpsr(env));
119 return 4;
120 case 33:
121 /* FPCR */
122 stl_p(buf, vfp_get_fpcr(env));
123 return 4;
124 default:
125 return 0;
126 }
127}
128
129static int aarch64_fpu_gdb_set_reg(CPUARMState *env, uint8_t *buf, int reg)
130{
131 switch (reg) {
132 case 0 ... 31:
133 /* 128 bit FP register */
9a2b5256
RH
134 {
135 uint64_t *q = aa64_vfp_qreg(env, reg);
136 q[0] = ldq_le_p(buf);
137 q[1] = ldq_le_p(buf + 8);
138 return 16;
139 }
6a669427
PM
140 case 32:
141 /* FPSR */
142 vfp_set_fpsr(env, ldl_p(buf));
143 return 4;
144 case 33:
145 /* FPCR */
146 vfp_set_fpcr(env, ldl_p(buf));
147 return 4;
148 default:
149 return 0;
150 }
151}
152
c4241c7d 153static uint64_t raw_read(CPUARMState *env, const ARMCPRegInfo *ri)
d4e6df63 154{
375421cc 155 assert(ri->fieldoffset);
67ed771d 156 if (cpreg_field_is_64bit(ri)) {
c4241c7d 157 return CPREG_FIELD64(env, ri);
22d9e1a9 158 } else {
c4241c7d 159 return CPREG_FIELD32(env, ri);
22d9e1a9 160 }
d4e6df63
PM
161}
162
c4241c7d
PM
163static void raw_write(CPUARMState *env, const ARMCPRegInfo *ri,
164 uint64_t value)
d4e6df63 165{
375421cc 166 assert(ri->fieldoffset);
67ed771d 167 if (cpreg_field_is_64bit(ri)) {
22d9e1a9
PM
168 CPREG_FIELD64(env, ri) = value;
169 } else {
170 CPREG_FIELD32(env, ri) = value;
171 }
d4e6df63
PM
172}
173
11f136ee
FA
174static void *raw_ptr(CPUARMState *env, const ARMCPRegInfo *ri)
175{
176 return (char *)env + ri->fieldoffset;
177}
178
49a66191 179uint64_t read_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri)
721fae12 180{
59a1c327 181 /* Raw read of a coprocessor register (as needed for migration, etc). */
721fae12 182 if (ri->type & ARM_CP_CONST) {
59a1c327 183 return ri->resetvalue;
721fae12 184 } else if (ri->raw_readfn) {
59a1c327 185 return ri->raw_readfn(env, ri);
721fae12 186 } else if (ri->readfn) {
59a1c327 187 return ri->readfn(env, ri);
721fae12 188 } else {
59a1c327 189 return raw_read(env, ri);
721fae12 190 }
721fae12
PM
191}
192
59a1c327 193static void write_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri,
7900e9f1 194 uint64_t v)
721fae12
PM
195{
196 /* Raw write of a coprocessor register (as needed for migration, etc).
721fae12
PM
197 * Note that constant registers are treated as write-ignored; the
198 * caller should check for success by whether a readback gives the
199 * value written.
200 */
201 if (ri->type & ARM_CP_CONST) {
59a1c327 202 return;
721fae12 203 } else if (ri->raw_writefn) {
c4241c7d 204 ri->raw_writefn(env, ri, v);
721fae12 205 } else if (ri->writefn) {
c4241c7d 206 ri->writefn(env, ri, v);
721fae12 207 } else {
afb2530f 208 raw_write(env, ri, v);
721fae12 209 }
721fae12
PM
210}
211
200bf5b7
AB
212static int arm_gdb_get_sysreg(CPUARMState *env, uint8_t *buf, int reg)
213{
2fc0cc0e 214 ARMCPU *cpu = env_archcpu(env);
200bf5b7
AB
215 const ARMCPRegInfo *ri;
216 uint32_t key;
217
218 key = cpu->dyn_xml.cpregs_keys[reg];
219 ri = get_arm_cp_reginfo(cpu->cp_regs, key);
220 if (ri) {
221 if (cpreg_field_is_64bit(ri)) {
222 return gdb_get_reg64(buf, (uint64_t)read_raw_cp_reg(env, ri));
223 } else {
224 return gdb_get_reg32(buf, (uint32_t)read_raw_cp_reg(env, ri));
225 }
226 }
227 return 0;
228}
229
230static int arm_gdb_set_sysreg(CPUARMState *env, uint8_t *buf, int reg)
231{
232 return 0;
233}
234
375421cc
PM
235static bool raw_accessors_invalid(const ARMCPRegInfo *ri)
236{
237 /* Return true if the regdef would cause an assertion if you called
238 * read_raw_cp_reg() or write_raw_cp_reg() on it (ie if it is a
239 * program bug for it not to have the NO_RAW flag).
240 * NB that returning false here doesn't necessarily mean that calling
241 * read/write_raw_cp_reg() is safe, because we can't distinguish "has
242 * read/write access functions which are safe for raw use" from "has
243 * read/write access functions which have side effects but has forgotten
244 * to provide raw access functions".
245 * The tests here line up with the conditions in read/write_raw_cp_reg()
246 * and assertions in raw_read()/raw_write().
247 */
248 if ((ri->type & ARM_CP_CONST) ||
249 ri->fieldoffset ||
250 ((ri->raw_writefn || ri->writefn) && (ri->raw_readfn || ri->readfn))) {
251 return false;
252 }
253 return true;
254}
255
b698e4ee 256bool write_cpustate_to_list(ARMCPU *cpu, bool kvm_sync)
721fae12
PM
257{
258 /* Write the coprocessor state from cpu->env to the (index,value) list. */
259 int i;
260 bool ok = true;
261
262 for (i = 0; i < cpu->cpreg_array_len; i++) {
263 uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
264 const ARMCPRegInfo *ri;
b698e4ee 265 uint64_t newval;
59a1c327 266
60322b39 267 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
721fae12
PM
268 if (!ri) {
269 ok = false;
270 continue;
271 }
7a0e58fa 272 if (ri->type & ARM_CP_NO_RAW) {
721fae12
PM
273 continue;
274 }
b698e4ee
PM
275
276 newval = read_raw_cp_reg(&cpu->env, ri);
277 if (kvm_sync) {
278 /*
279 * Only sync if the previous list->cpustate sync succeeded.
280 * Rather than tracking the success/failure state for every
281 * item in the list, we just recheck "does the raw write we must
282 * have made in write_list_to_cpustate() read back OK" here.
283 */
284 uint64_t oldval = cpu->cpreg_values[i];
285
286 if (oldval == newval) {
287 continue;
288 }
289
290 write_raw_cp_reg(&cpu->env, ri, oldval);
291 if (read_raw_cp_reg(&cpu->env, ri) != oldval) {
292 continue;
293 }
294
295 write_raw_cp_reg(&cpu->env, ri, newval);
296 }
297 cpu->cpreg_values[i] = newval;
721fae12
PM
298 }
299 return ok;
300}
301
302bool write_list_to_cpustate(ARMCPU *cpu)
303{
304 int i;
305 bool ok = true;
306
307 for (i = 0; i < cpu->cpreg_array_len; i++) {
308 uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
309 uint64_t v = cpu->cpreg_values[i];
721fae12
PM
310 const ARMCPRegInfo *ri;
311
60322b39 312 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
721fae12
PM
313 if (!ri) {
314 ok = false;
315 continue;
316 }
7a0e58fa 317 if (ri->type & ARM_CP_NO_RAW) {
721fae12
PM
318 continue;
319 }
320 /* Write value and confirm it reads back as written
321 * (to catch read-only registers and partially read-only
322 * registers where the incoming migration value doesn't match)
323 */
59a1c327
PM
324 write_raw_cp_reg(&cpu->env, ri, v);
325 if (read_raw_cp_reg(&cpu->env, ri) != v) {
721fae12
PM
326 ok = false;
327 }
328 }
329 return ok;
330}
331
332static void add_cpreg_to_list(gpointer key, gpointer opaque)
333{
334 ARMCPU *cpu = opaque;
335 uint64_t regidx;
336 const ARMCPRegInfo *ri;
337
338 regidx = *(uint32_t *)key;
60322b39 339 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
721fae12 340
7a0e58fa 341 if (!(ri->type & (ARM_CP_NO_RAW|ARM_CP_ALIAS))) {
721fae12
PM
342 cpu->cpreg_indexes[cpu->cpreg_array_len] = cpreg_to_kvm_id(regidx);
343 /* The value array need not be initialized at this point */
344 cpu->cpreg_array_len++;
345 }
346}
347
348static void count_cpreg(gpointer key, gpointer opaque)
349{
350 ARMCPU *cpu = opaque;
351 uint64_t regidx;
352 const ARMCPRegInfo *ri;
353
354 regidx = *(uint32_t *)key;
60322b39 355 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
721fae12 356
7a0e58fa 357 if (!(ri->type & (ARM_CP_NO_RAW|ARM_CP_ALIAS))) {
721fae12
PM
358 cpu->cpreg_array_len++;
359 }
360}
361
362static gint cpreg_key_compare(gconstpointer a, gconstpointer b)
363{
cbf239b7
AR
364 uint64_t aidx = cpreg_to_kvm_id(*(uint32_t *)a);
365 uint64_t bidx = cpreg_to_kvm_id(*(uint32_t *)b);
721fae12 366
cbf239b7
AR
367 if (aidx > bidx) {
368 return 1;
369 }
370 if (aidx < bidx) {
371 return -1;
372 }
373 return 0;
721fae12
PM
374}
375
376void init_cpreg_list(ARMCPU *cpu)
377{
378 /* Initialise the cpreg_tuples[] array based on the cp_regs hash.
379 * Note that we require cpreg_tuples[] to be sorted by key ID.
380 */
57b6d95e 381 GList *keys;
721fae12
PM
382 int arraylen;
383
57b6d95e 384 keys = g_hash_table_get_keys(cpu->cp_regs);
721fae12
PM
385 keys = g_list_sort(keys, cpreg_key_compare);
386
387 cpu->cpreg_array_len = 0;
388
389 g_list_foreach(keys, count_cpreg, cpu);
390
391 arraylen = cpu->cpreg_array_len;
392 cpu->cpreg_indexes = g_new(uint64_t, arraylen);
393 cpu->cpreg_values = g_new(uint64_t, arraylen);
394 cpu->cpreg_vmstate_indexes = g_new(uint64_t, arraylen);
395 cpu->cpreg_vmstate_values = g_new(uint64_t, arraylen);
396 cpu->cpreg_vmstate_array_len = cpu->cpreg_array_len;
397 cpu->cpreg_array_len = 0;
398
399 g_list_foreach(keys, add_cpreg_to_list, cpu);
400
401 assert(cpu->cpreg_array_len == arraylen);
402
403 g_list_free(keys);
404}
405
68e9c2fe
EI
406/*
407 * Some registers are not accessible if EL3.NS=0 and EL3 is using AArch32 but
408 * they are accessible when EL3 is using AArch64 regardless of EL3.NS.
409 *
410 * access_el3_aa32ns: Used to check AArch32 register views.
411 * access_el3_aa32ns_aa64any: Used to check both AArch32/64 register views.
412 */
413static CPAccessResult access_el3_aa32ns(CPUARMState *env,
3f208fd7
PM
414 const ARMCPRegInfo *ri,
415 bool isread)
68e9c2fe
EI
416{
417 bool secure = arm_is_secure_below_el3(env);
418
419 assert(!arm_el_is_aa64(env, 3));
420 if (secure) {
421 return CP_ACCESS_TRAP_UNCATEGORIZED;
422 }
423 return CP_ACCESS_OK;
424}
425
426static CPAccessResult access_el3_aa32ns_aa64any(CPUARMState *env,
3f208fd7
PM
427 const ARMCPRegInfo *ri,
428 bool isread)
68e9c2fe
EI
429{
430 if (!arm_el_is_aa64(env, 3)) {
3f208fd7 431 return access_el3_aa32ns(env, ri, isread);
68e9c2fe
EI
432 }
433 return CP_ACCESS_OK;
434}
435
5513c3ab
PM
436/* Some secure-only AArch32 registers trap to EL3 if used from
437 * Secure EL1 (but are just ordinary UNDEF in other non-EL3 contexts).
438 * Note that an access from Secure EL1 can only happen if EL3 is AArch64.
439 * We assume that the .access field is set to PL1_RW.
440 */
441static CPAccessResult access_trap_aa32s_el1(CPUARMState *env,
3f208fd7
PM
442 const ARMCPRegInfo *ri,
443 bool isread)
5513c3ab
PM
444{
445 if (arm_current_el(env) == 3) {
446 return CP_ACCESS_OK;
447 }
448 if (arm_is_secure_below_el3(env)) {
449 return CP_ACCESS_TRAP_EL3;
450 }
451 /* This will be EL1 NS and EL2 NS, which just UNDEF */
452 return CP_ACCESS_TRAP_UNCATEGORIZED;
453}
454
187f678d
PM
455/* Check for traps to "powerdown debug" registers, which are controlled
456 * by MDCR.TDOSA
457 */
458static CPAccessResult access_tdosa(CPUARMState *env, const ARMCPRegInfo *ri,
459 bool isread)
460{
461 int el = arm_current_el(env);
30ac6339
PM
462 bool mdcr_el2_tdosa = (env->cp15.mdcr_el2 & MDCR_TDOSA) ||
463 (env->cp15.mdcr_el2 & MDCR_TDE) ||
7c208e0f 464 (arm_hcr_el2_eff(env) & HCR_TGE);
187f678d 465
30ac6339 466 if (el < 2 && mdcr_el2_tdosa && !arm_is_secure_below_el3(env)) {
187f678d
PM
467 return CP_ACCESS_TRAP_EL2;
468 }
469 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TDOSA)) {
470 return CP_ACCESS_TRAP_EL3;
471 }
472 return CP_ACCESS_OK;
473}
474
91b0a238
PM
475/* Check for traps to "debug ROM" registers, which are controlled
476 * by MDCR_EL2.TDRA for EL2 but by the more general MDCR_EL3.TDA for EL3.
477 */
478static CPAccessResult access_tdra(CPUARMState *env, const ARMCPRegInfo *ri,
479 bool isread)
480{
481 int el = arm_current_el(env);
30ac6339
PM
482 bool mdcr_el2_tdra = (env->cp15.mdcr_el2 & MDCR_TDRA) ||
483 (env->cp15.mdcr_el2 & MDCR_TDE) ||
7c208e0f 484 (arm_hcr_el2_eff(env) & HCR_TGE);
91b0a238 485
30ac6339 486 if (el < 2 && mdcr_el2_tdra && !arm_is_secure_below_el3(env)) {
91b0a238
PM
487 return CP_ACCESS_TRAP_EL2;
488 }
489 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TDA)) {
490 return CP_ACCESS_TRAP_EL3;
491 }
492 return CP_ACCESS_OK;
493}
494
d6c8cf81
PM
495/* Check for traps to general debug registers, which are controlled
496 * by MDCR_EL2.TDA for EL2 and MDCR_EL3.TDA for EL3.
497 */
498static CPAccessResult access_tda(CPUARMState *env, const ARMCPRegInfo *ri,
499 bool isread)
500{
501 int el = arm_current_el(env);
30ac6339
PM
502 bool mdcr_el2_tda = (env->cp15.mdcr_el2 & MDCR_TDA) ||
503 (env->cp15.mdcr_el2 & MDCR_TDE) ||
7c208e0f 504 (arm_hcr_el2_eff(env) & HCR_TGE);
d6c8cf81 505
30ac6339 506 if (el < 2 && mdcr_el2_tda && !arm_is_secure_below_el3(env)) {
d6c8cf81
PM
507 return CP_ACCESS_TRAP_EL2;
508 }
509 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TDA)) {
510 return CP_ACCESS_TRAP_EL3;
511 }
512 return CP_ACCESS_OK;
513}
514
1fce1ba9
PM
515/* Check for traps to performance monitor registers, which are controlled
516 * by MDCR_EL2.TPM for EL2 and MDCR_EL3.TPM for EL3.
517 */
518static CPAccessResult access_tpm(CPUARMState *env, const ARMCPRegInfo *ri,
519 bool isread)
520{
521 int el = arm_current_el(env);
522
523 if (el < 2 && (env->cp15.mdcr_el2 & MDCR_TPM)
524 && !arm_is_secure_below_el3(env)) {
525 return CP_ACCESS_TRAP_EL2;
526 }
527 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) {
528 return CP_ACCESS_TRAP_EL3;
529 }
530 return CP_ACCESS_OK;
531}
532
84929218
RH
533/* Check for traps from EL1 due to HCR_EL2.TVM and HCR_EL2.TRVM. */
534static CPAccessResult access_tvm_trvm(CPUARMState *env, const ARMCPRegInfo *ri,
535 bool isread)
536{
537 if (arm_current_el(env) == 1) {
538 uint64_t trap = isread ? HCR_TRVM : HCR_TVM;
539 if (arm_hcr_el2_eff(env) & trap) {
540 return CP_ACCESS_TRAP_EL2;
541 }
542 }
543 return CP_ACCESS_OK;
544}
545
c4241c7d 546static void dacr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
c983fe6c 547{
2fc0cc0e 548 ARMCPU *cpu = env_archcpu(env);
00c8cb0a 549
8d5c773e 550 raw_write(env, ri, value);
d10eb08f 551 tlb_flush(CPU(cpu)); /* Flush TLB as domain not tracked in TLB */
c983fe6c
PM
552}
553
c4241c7d 554static void fcse_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
08de207b 555{
2fc0cc0e 556 ARMCPU *cpu = env_archcpu(env);
00c8cb0a 557
8d5c773e 558 if (raw_read(env, ri) != value) {
08de207b
PM
559 /* Unlike real hardware the qemu TLB uses virtual addresses,
560 * not modified virtual addresses, so this causes a TLB flush.
561 */
d10eb08f 562 tlb_flush(CPU(cpu));
8d5c773e 563 raw_write(env, ri, value);
08de207b 564 }
08de207b 565}
c4241c7d
PM
566
567static void contextidr_write(CPUARMState *env, const ARMCPRegInfo *ri,
568 uint64_t value)
08de207b 569{
2fc0cc0e 570 ARMCPU *cpu = env_archcpu(env);
00c8cb0a 571
452a0955 572 if (raw_read(env, ri) != value && !arm_feature(env, ARM_FEATURE_PMSA)
014406b5 573 && !extended_addresses_enabled(env)) {
08de207b
PM
574 /* For VMSA (when not using the LPAE long descriptor page table
575 * format) this register includes the ASID, so do a TLB flush.
576 * For PMSA it is purely a process ID and no action is needed.
577 */
d10eb08f 578 tlb_flush(CPU(cpu));
08de207b 579 }
8d5c773e 580 raw_write(env, ri, value);
08de207b
PM
581}
582
b4ab8ce9
PM
583/* IS variants of TLB operations must affect all cores */
584static void tlbiall_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
585 uint64_t value)
586{
29a0af61 587 CPUState *cs = env_cpu(env);
b4ab8ce9
PM
588
589 tlb_flush_all_cpus_synced(cs);
590}
591
592static void tlbiasid_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
593 uint64_t value)
594{
29a0af61 595 CPUState *cs = env_cpu(env);
b4ab8ce9
PM
596
597 tlb_flush_all_cpus_synced(cs);
598}
599
600static void tlbimva_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
601 uint64_t value)
602{
29a0af61 603 CPUState *cs = env_cpu(env);
b4ab8ce9
PM
604
605 tlb_flush_page_all_cpus_synced(cs, value & TARGET_PAGE_MASK);
606}
607
608static void tlbimvaa_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
609 uint64_t value)
610{
29a0af61 611 CPUState *cs = env_cpu(env);
b4ab8ce9
PM
612
613 tlb_flush_page_all_cpus_synced(cs, value & TARGET_PAGE_MASK);
614}
615
616/*
617 * Non-IS variants of TLB operations are upgraded to
618 * IS versions if we are at NS EL1 and HCR_EL2.FB is set to
619 * force broadcast of these operations.
620 */
621static bool tlb_force_broadcast(CPUARMState *env)
622{
623 return (env->cp15.hcr_el2 & HCR_FB) &&
624 arm_current_el(env) == 1 && arm_is_secure_below_el3(env);
625}
626
c4241c7d
PM
627static void tlbiall_write(CPUARMState *env, const ARMCPRegInfo *ri,
628 uint64_t value)
d929823f
PM
629{
630 /* Invalidate all (TLBIALL) */
527db2be 631 CPUState *cs = env_cpu(env);
00c8cb0a 632
b4ab8ce9 633 if (tlb_force_broadcast(env)) {
527db2be
RH
634 tlb_flush_all_cpus_synced(cs);
635 } else {
636 tlb_flush(cs);
b4ab8ce9 637 }
d929823f
PM
638}
639
c4241c7d
PM
640static void tlbimva_write(CPUARMState *env, const ARMCPRegInfo *ri,
641 uint64_t value)
d929823f
PM
642{
643 /* Invalidate single TLB entry by MVA and ASID (TLBIMVA) */
527db2be 644 CPUState *cs = env_cpu(env);
31b030d4 645
527db2be 646 value &= TARGET_PAGE_MASK;
b4ab8ce9 647 if (tlb_force_broadcast(env)) {
527db2be
RH
648 tlb_flush_page_all_cpus_synced(cs, value);
649 } else {
650 tlb_flush_page(cs, value);
b4ab8ce9 651 }
d929823f
PM
652}
653
c4241c7d
PM
654static void tlbiasid_write(CPUARMState *env, const ARMCPRegInfo *ri,
655 uint64_t value)
d929823f
PM
656{
657 /* Invalidate by ASID (TLBIASID) */
527db2be 658 CPUState *cs = env_cpu(env);
00c8cb0a 659
b4ab8ce9 660 if (tlb_force_broadcast(env)) {
527db2be
RH
661 tlb_flush_all_cpus_synced(cs);
662 } else {
663 tlb_flush(cs);
b4ab8ce9 664 }
d929823f
PM
665}
666
c4241c7d
PM
667static void tlbimvaa_write(CPUARMState *env, const ARMCPRegInfo *ri,
668 uint64_t value)
d929823f
PM
669{
670 /* Invalidate single entry by MVA, all ASIDs (TLBIMVAA) */
527db2be 671 CPUState *cs = env_cpu(env);
31b030d4 672
527db2be 673 value &= TARGET_PAGE_MASK;
b4ab8ce9 674 if (tlb_force_broadcast(env)) {
527db2be
RH
675 tlb_flush_page_all_cpus_synced(cs, value);
676 } else {
677 tlb_flush_page(cs, value);
b4ab8ce9 678 }
fa439fc5
PM
679}
680
541ef8c2
SS
681static void tlbiall_nsnh_write(CPUARMState *env, const ARMCPRegInfo *ri,
682 uint64_t value)
683{
29a0af61 684 CPUState *cs = env_cpu(env);
541ef8c2 685
0336cbf8 686 tlb_flush_by_mmuidx(cs,
01b98b68 687 ARMMMUIdxBit_E10_1 |
452ef8cb 688 ARMMMUIdxBit_E10_1_PAN |
01b98b68 689 ARMMMUIdxBit_E10_0 |
97fa9350 690 ARMMMUIdxBit_Stage2);
541ef8c2
SS
691}
692
693static void tlbiall_nsnh_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
694 uint64_t value)
695{
29a0af61 696 CPUState *cs = env_cpu(env);
541ef8c2 697
a67cf277 698 tlb_flush_by_mmuidx_all_cpus_synced(cs,
01b98b68 699 ARMMMUIdxBit_E10_1 |
452ef8cb 700 ARMMMUIdxBit_E10_1_PAN |
01b98b68 701 ARMMMUIdxBit_E10_0 |
97fa9350 702 ARMMMUIdxBit_Stage2);
541ef8c2
SS
703}
704
705static void tlbiipas2_write(CPUARMState *env, const ARMCPRegInfo *ri,
706 uint64_t value)
707{
708 /* Invalidate by IPA. This has to invalidate any structures that
709 * contain only stage 2 translation information, but does not need
710 * to apply to structures that contain combined stage 1 and stage 2
711 * translation information.
712 * This must NOP if EL2 isn't implemented or SCR_EL3.NS is zero.
713 */
29a0af61 714 CPUState *cs = env_cpu(env);
541ef8c2
SS
715 uint64_t pageaddr;
716
717 if (!arm_feature(env, ARM_FEATURE_EL2) || !(env->cp15.scr_el3 & SCR_NS)) {
718 return;
719 }
720
721 pageaddr = sextract64(value << 12, 0, 40);
722
97fa9350 723 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_Stage2);
541ef8c2
SS
724}
725
726static void tlbiipas2_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
727 uint64_t value)
728{
29a0af61 729 CPUState *cs = env_cpu(env);
541ef8c2
SS
730 uint64_t pageaddr;
731
732 if (!arm_feature(env, ARM_FEATURE_EL2) || !(env->cp15.scr_el3 & SCR_NS)) {
733 return;
734 }
735
736 pageaddr = sextract64(value << 12, 0, 40);
737
a67cf277 738 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
97fa9350 739 ARMMMUIdxBit_Stage2);
541ef8c2
SS
740}
741
742static void tlbiall_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
743 uint64_t value)
744{
29a0af61 745 CPUState *cs = env_cpu(env);
541ef8c2 746
e013b741 747 tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_E2);
541ef8c2
SS
748}
749
750static void tlbiall_hyp_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
751 uint64_t value)
752{
29a0af61 753 CPUState *cs = env_cpu(env);
541ef8c2 754
e013b741 755 tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_E2);
541ef8c2
SS
756}
757
758static void tlbimva_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
759 uint64_t value)
760{
29a0af61 761 CPUState *cs = env_cpu(env);
541ef8c2
SS
762 uint64_t pageaddr = value & ~MAKE_64BIT_MASK(0, 12);
763
e013b741 764 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_E2);
541ef8c2
SS
765}
766
767static void tlbimva_hyp_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
768 uint64_t value)
769{
29a0af61 770 CPUState *cs = env_cpu(env);
541ef8c2
SS
771 uint64_t pageaddr = value & ~MAKE_64BIT_MASK(0, 12);
772
a67cf277 773 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
e013b741 774 ARMMMUIdxBit_E2);
541ef8c2
SS
775}
776
e9aa6c21 777static const ARMCPRegInfo cp_reginfo[] = {
54bf36ed
FA
778 /* Define the secure and non-secure FCSE identifier CP registers
779 * separately because there is no secure bank in V8 (no _EL3). This allows
780 * the secure register to be properly reset and migrated. There is also no
781 * v8 EL1 version of the register so the non-secure instance stands alone.
782 */
9c513e78 783 { .name = "FCSEIDR",
54bf36ed
FA
784 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
785 .access = PL1_RW, .secure = ARM_CP_SECSTATE_NS,
786 .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_ns),
787 .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
9c513e78 788 { .name = "FCSEIDR_S",
54bf36ed
FA
789 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
790 .access = PL1_RW, .secure = ARM_CP_SECSTATE_S,
791 .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_s),
d4e6df63 792 .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
54bf36ed
FA
793 /* Define the secure and non-secure context identifier CP registers
794 * separately because there is no secure bank in V8 (no _EL3). This allows
795 * the secure register to be properly reset and migrated. In the
796 * non-secure case, the 32-bit register will have reset and migration
797 * disabled during registration as it is handled by the 64-bit instance.
798 */
799 { .name = "CONTEXTIDR_EL1", .state = ARM_CP_STATE_BOTH,
014406b5 800 .opc0 = 3, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
84929218
RH
801 .access = PL1_RW, .accessfn = access_tvm_trvm,
802 .secure = ARM_CP_SECSTATE_NS,
54bf36ed
FA
803 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[1]),
804 .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
9c513e78 805 { .name = "CONTEXTIDR_S", .state = ARM_CP_STATE_AA32,
54bf36ed 806 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
84929218
RH
807 .access = PL1_RW, .accessfn = access_tvm_trvm,
808 .secure = ARM_CP_SECSTATE_S,
54bf36ed 809 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_s),
d4e6df63 810 .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
9449fdf6
PM
811 REGINFO_SENTINEL
812};
813
814static const ARMCPRegInfo not_v8_cp_reginfo[] = {
815 /* NB: Some of these registers exist in v8 but with more precise
816 * definitions that don't use CP_ANY wildcards (mostly in v8_cp_reginfo[]).
817 */
818 /* MMU Domain access control / MPU write buffer control */
0c17d68c
FA
819 { .name = "DACR",
820 .cp = 15, .opc1 = CP_ANY, .crn = 3, .crm = CP_ANY, .opc2 = CP_ANY,
84929218 821 .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
0c17d68c
FA
822 .writefn = dacr_write, .raw_writefn = raw_write,
823 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
824 offsetoflow32(CPUARMState, cp15.dacr_ns) } },
a903c449
EI
825 /* ARMv7 allocates a range of implementation defined TLB LOCKDOWN regs.
826 * For v6 and v5, these mappings are overly broad.
4fdd17dd 827 */
a903c449
EI
828 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 0,
829 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
830 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 1,
831 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
832 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 4,
833 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
834 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 8,
4fdd17dd 835 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
c4804214
PM
836 /* Cache maintenance ops; some of this space may be overridden later. */
837 { .name = "CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
838 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
839 .type = ARM_CP_NOP | ARM_CP_OVERRIDE },
e9aa6c21
PM
840 REGINFO_SENTINEL
841};
842
7d57f408
PM
843static const ARMCPRegInfo not_v6_cp_reginfo[] = {
844 /* Not all pre-v6 cores implemented this WFI, so this is slightly
845 * over-broad.
846 */
847 { .name = "WFI_v5", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = 2,
848 .access = PL1_W, .type = ARM_CP_WFI },
849 REGINFO_SENTINEL
850};
851
852static const ARMCPRegInfo not_v7_cp_reginfo[] = {
853 /* Standard v6 WFI (also used in some pre-v6 cores); not in v7 (which
854 * is UNPREDICTABLE; we choose to NOP as most implementations do).
855 */
856 { .name = "WFI_v6", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
857 .access = PL1_W, .type = ARM_CP_WFI },
34f90529
PM
858 /* L1 cache lockdown. Not architectural in v6 and earlier but in practice
859 * implemented in 926, 946, 1026, 1136, 1176 and 11MPCore. StrongARM and
860 * OMAPCP will override this space.
861 */
862 { .name = "DLOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 0,
863 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_data),
864 .resetvalue = 0 },
865 { .name = "ILOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 1,
866 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_insn),
867 .resetvalue = 0 },
776d4e5c
PM
868 /* v6 doesn't have the cache ID registers but Linux reads them anyway */
869 { .name = "DUMMY", .cp = 15, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = CP_ANY,
7a0e58fa 870 .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 871 .resetvalue = 0 },
50300698
PM
872 /* We don't implement pre-v7 debug but most CPUs had at least a DBGDIDR;
873 * implementing it as RAZ means the "debug architecture version" bits
874 * will read as a reserved value, which should cause Linux to not try
875 * to use the debug hardware.
876 */
877 { .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0,
878 .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
995939a6
PM
879 /* MMU TLB control. Note that the wildcarding means we cover not just
880 * the unified TLB ops but also the dside/iside/inner-shareable variants.
881 */
882 { .name = "TLBIALL", .cp = 15, .crn = 8, .crm = CP_ANY,
883 .opc1 = CP_ANY, .opc2 = 0, .access = PL1_W, .writefn = tlbiall_write,
7a0e58fa 884 .type = ARM_CP_NO_RAW },
995939a6
PM
885 { .name = "TLBIMVA", .cp = 15, .crn = 8, .crm = CP_ANY,
886 .opc1 = CP_ANY, .opc2 = 1, .access = PL1_W, .writefn = tlbimva_write,
7a0e58fa 887 .type = ARM_CP_NO_RAW },
995939a6
PM
888 { .name = "TLBIASID", .cp = 15, .crn = 8, .crm = CP_ANY,
889 .opc1 = CP_ANY, .opc2 = 2, .access = PL1_W, .writefn = tlbiasid_write,
7a0e58fa 890 .type = ARM_CP_NO_RAW },
995939a6
PM
891 { .name = "TLBIMVAA", .cp = 15, .crn = 8, .crm = CP_ANY,
892 .opc1 = CP_ANY, .opc2 = 3, .access = PL1_W, .writefn = tlbimvaa_write,
7a0e58fa 893 .type = ARM_CP_NO_RAW },
a903c449
EI
894 { .name = "PRRR", .cp = 15, .crn = 10, .crm = 2,
895 .opc1 = 0, .opc2 = 0, .access = PL1_RW, .type = ARM_CP_NOP },
896 { .name = "NMRR", .cp = 15, .crn = 10, .crm = 2,
897 .opc1 = 0, .opc2 = 1, .access = PL1_RW, .type = ARM_CP_NOP },
7d57f408
PM
898 REGINFO_SENTINEL
899};
900
c4241c7d
PM
901static void cpacr_write(CPUARMState *env, const ARMCPRegInfo *ri,
902 uint64_t value)
2771db27 903{
f0aff255
FA
904 uint32_t mask = 0;
905
906 /* In ARMv8 most bits of CPACR_EL1 are RES0. */
907 if (!arm_feature(env, ARM_FEATURE_V8)) {
908 /* ARMv7 defines bits for unimplemented coprocessors as RAZ/WI.
909 * ASEDIS [31] and D32DIS [30] are both UNK/SBZP without VFP.
910 * TRCDIS [28] is RAZ/WI since we do not implement a trace macrocell.
911 */
7fbc6a40 912 if (cpu_isar_feature(aa32_vfp_simd, env_archcpu(env))) {
f0aff255
FA
913 /* VFP coprocessor: cp10 & cp11 [23:20] */
914 mask |= (1 << 31) | (1 << 30) | (0xf << 20);
915
916 if (!arm_feature(env, ARM_FEATURE_NEON)) {
917 /* ASEDIS [31] bit is RAO/WI */
918 value |= (1 << 31);
919 }
920
921 /* VFPv3 and upwards with NEON implement 32 double precision
922 * registers (D0-D31).
923 */
a6627f5f 924 if (!cpu_isar_feature(aa32_simd_r32, env_archcpu(env))) {
f0aff255
FA
925 /* D32DIS [30] is RAO/WI if D16-31 are not implemented. */
926 value |= (1 << 30);
927 }
928 }
929 value &= mask;
2771db27 930 }
fc1120a7
PM
931
932 /*
933 * For A-profile AArch32 EL3 (but not M-profile secure mode), if NSACR.CP10
934 * is 0 then CPACR.{CP11,CP10} ignore writes and read as 0b00.
935 */
936 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
937 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
938 value &= ~(0xf << 20);
939 value |= env->cp15.cpacr_el1 & (0xf << 20);
940 }
941
7ebd5f2e 942 env->cp15.cpacr_el1 = value;
2771db27
PM
943}
944
fc1120a7
PM
945static uint64_t cpacr_read(CPUARMState *env, const ARMCPRegInfo *ri)
946{
947 /*
948 * For A-profile AArch32 EL3 (but not M-profile secure mode), if NSACR.CP10
949 * is 0 then CPACR.{CP11,CP10} ignore writes and read as 0b00.
950 */
951 uint64_t value = env->cp15.cpacr_el1;
952
953 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
954 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
955 value &= ~(0xf << 20);
956 }
957 return value;
958}
959
960
5deac39c
PM
961static void cpacr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
962{
963 /* Call cpacr_write() so that we reset with the correct RAO bits set
964 * for our CPU features.
965 */
966 cpacr_write(env, ri, 0);
967}
968
3f208fd7
PM
969static CPAccessResult cpacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
970 bool isread)
c6f19164
GB
971{
972 if (arm_feature(env, ARM_FEATURE_V8)) {
973 /* Check if CPACR accesses are to be trapped to EL2 */
974 if (arm_current_el(env) == 1 &&
975 (env->cp15.cptr_el[2] & CPTR_TCPAC) && !arm_is_secure(env)) {
976 return CP_ACCESS_TRAP_EL2;
977 /* Check if CPACR accesses are to be trapped to EL3 */
978 } else if (arm_current_el(env) < 3 &&
979 (env->cp15.cptr_el[3] & CPTR_TCPAC)) {
980 return CP_ACCESS_TRAP_EL3;
981 }
982 }
983
984 return CP_ACCESS_OK;
985}
986
3f208fd7
PM
987static CPAccessResult cptr_access(CPUARMState *env, const ARMCPRegInfo *ri,
988 bool isread)
c6f19164
GB
989{
990 /* Check if CPTR accesses are set to trap to EL3 */
991 if (arm_current_el(env) == 2 && (env->cp15.cptr_el[3] & CPTR_TCPAC)) {
992 return CP_ACCESS_TRAP_EL3;
993 }
994
995 return CP_ACCESS_OK;
996}
997
7d57f408
PM
998static const ARMCPRegInfo v6_cp_reginfo[] = {
999 /* prefetch by MVA in v6, NOP in v7 */
1000 { .name = "MVA_prefetch",
1001 .cp = 15, .crn = 7, .crm = 13, .opc1 = 0, .opc2 = 1,
1002 .access = PL1_W, .type = ARM_CP_NOP },
6df99dec
SS
1003 /* We need to break the TB after ISB to execute self-modifying code
1004 * correctly and also to take any pending interrupts immediately.
1005 * So use arm_cp_write_ignore() function instead of ARM_CP_NOP flag.
1006 */
7d57f408 1007 { .name = "ISB", .cp = 15, .crn = 7, .crm = 5, .opc1 = 0, .opc2 = 4,
6df99dec 1008 .access = PL0_W, .type = ARM_CP_NO_RAW, .writefn = arm_cp_write_ignore },
091fd17c 1009 { .name = "DSB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 4,
7d57f408 1010 .access = PL0_W, .type = ARM_CP_NOP },
091fd17c 1011 { .name = "DMB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 5,
7d57f408 1012 .access = PL0_W, .type = ARM_CP_NOP },
06d76f31 1013 { .name = "IFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 2,
84929218 1014 .access = PL1_RW, .accessfn = access_tvm_trvm,
b848ce2b
FA
1015 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ifar_s),
1016 offsetof(CPUARMState, cp15.ifar_ns) },
06d76f31
PM
1017 .resetvalue = 0, },
1018 /* Watchpoint Fault Address Register : should actually only be present
1019 * for 1136, 1176, 11MPCore.
1020 */
1021 { .name = "WFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 1,
1022 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0, },
34222fb8 1023 { .name = "CPACR", .state = ARM_CP_STATE_BOTH, .opc0 = 3,
c6f19164 1024 .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 2, .accessfn = cpacr_access,
7ebd5f2e 1025 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.cpacr_el1),
fc1120a7 1026 .resetfn = cpacr_reset, .writefn = cpacr_write, .readfn = cpacr_read },
7d57f408
PM
1027 REGINFO_SENTINEL
1028};
1029
7ece99b1
AL
1030/* Definitions for the PMU registers */
1031#define PMCRN_MASK 0xf800
1032#define PMCRN_SHIFT 11
f4efb4b2 1033#define PMCRLC 0x40
a1ed04dd
PM
1034#define PMCRDP 0x20
1035#define PMCRX 0x10
7ece99b1
AL
1036#define PMCRD 0x8
1037#define PMCRC 0x4
5ecdd3e4 1038#define PMCRP 0x2
7ece99b1 1039#define PMCRE 0x1
62d96ff4
PM
1040/*
1041 * Mask of PMCR bits writeable by guest (not including WO bits like C, P,
1042 * which can be written as 1 to trigger behaviour but which stay RAZ).
1043 */
1044#define PMCR_WRITEABLE_MASK (PMCRLC | PMCRDP | PMCRX | PMCRD | PMCRE)
7ece99b1 1045
033614c4
AL
1046#define PMXEVTYPER_P 0x80000000
1047#define PMXEVTYPER_U 0x40000000
1048#define PMXEVTYPER_NSK 0x20000000
1049#define PMXEVTYPER_NSU 0x10000000
1050#define PMXEVTYPER_NSH 0x08000000
1051#define PMXEVTYPER_M 0x04000000
1052#define PMXEVTYPER_MT 0x02000000
1053#define PMXEVTYPER_EVTCOUNT 0x0000ffff
1054#define PMXEVTYPER_MASK (PMXEVTYPER_P | PMXEVTYPER_U | PMXEVTYPER_NSK | \
1055 PMXEVTYPER_NSU | PMXEVTYPER_NSH | \
1056 PMXEVTYPER_M | PMXEVTYPER_MT | \
1057 PMXEVTYPER_EVTCOUNT)
1058
4b8afa1f
AL
1059#define PMCCFILTR 0xf8000000
1060#define PMCCFILTR_M PMXEVTYPER_M
1061#define PMCCFILTR_EL0 (PMCCFILTR | PMCCFILTR_M)
1062
7ece99b1
AL
1063static inline uint32_t pmu_num_counters(CPUARMState *env)
1064{
1065 return (env->cp15.c9_pmcr & PMCRN_MASK) >> PMCRN_SHIFT;
1066}
1067
1068/* Bits allowed to be set/cleared for PMCNTEN* and PMINTEN* */
1069static inline uint64_t pmu_counter_mask(CPUARMState *env)
1070{
1071 return (1 << 31) | ((1 << pmu_num_counters(env)) - 1);
1072}
1073
57a4a11b
AL
1074typedef struct pm_event {
1075 uint16_t number; /* PMEVTYPER.evtCount is 16 bits wide */
1076 /* If the event is supported on this CPU (used to generate PMCEID[01]) */
1077 bool (*supported)(CPUARMState *);
1078 /*
1079 * Retrieve the current count of the underlying event. The programmed
1080 * counters hold a difference from the return value from this function
1081 */
1082 uint64_t (*get_count)(CPUARMState *);
4e7beb0c
AL
1083 /*
1084 * Return how many nanoseconds it will take (at a minimum) for count events
1085 * to occur. A negative value indicates the counter will never overflow, or
1086 * that the counter has otherwise arranged for the overflow bit to be set
1087 * and the PMU interrupt to be raised on overflow.
1088 */
1089 int64_t (*ns_per_count)(uint64_t);
57a4a11b
AL
1090} pm_event;
1091
b2e23725
AL
1092static bool event_always_supported(CPUARMState *env)
1093{
1094 return true;
1095}
1096
0d4bfd7d
AL
1097static uint64_t swinc_get_count(CPUARMState *env)
1098{
1099 /*
1100 * SW_INCR events are written directly to the pmevcntr's by writes to
1101 * PMSWINC, so there is no underlying count maintained by the PMU itself
1102 */
1103 return 0;
1104}
1105
4e7beb0c
AL
1106static int64_t swinc_ns_per(uint64_t ignored)
1107{
1108 return -1;
1109}
1110
b2e23725
AL
1111/*
1112 * Return the underlying cycle count for the PMU cycle counters. If we're in
1113 * usermode, simply return 0.
1114 */
1115static uint64_t cycles_get_count(CPUARMState *env)
1116{
1117#ifndef CONFIG_USER_ONLY
1118 return muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
1119 ARM_CPU_FREQ, NANOSECONDS_PER_SECOND);
1120#else
1121 return cpu_get_host_ticks();
1122#endif
1123}
1124
1125#ifndef CONFIG_USER_ONLY
4e7beb0c
AL
1126static int64_t cycles_ns_per(uint64_t cycles)
1127{
1128 return (ARM_CPU_FREQ / NANOSECONDS_PER_SECOND) * cycles;
1129}
1130
b2e23725
AL
1131static bool instructions_supported(CPUARMState *env)
1132{
1133 return use_icount == 1 /* Precise instruction counting */;
1134}
1135
1136static uint64_t instructions_get_count(CPUARMState *env)
1137{
1138 return (uint64_t)cpu_get_icount_raw();
1139}
4e7beb0c
AL
1140
1141static int64_t instructions_ns_per(uint64_t icount)
1142{
1143 return cpu_icount_to_ns((int64_t)icount);
1144}
b2e23725
AL
1145#endif
1146
0727f63b
PM
1147static bool pmu_8_1_events_supported(CPUARMState *env)
1148{
1149 /* For events which are supported in any v8.1 PMU */
1150 return cpu_isar_feature(any_pmu_8_1, env_archcpu(env));
1151}
1152
15dd1ebd
PM
1153static bool pmu_8_4_events_supported(CPUARMState *env)
1154{
1155 /* For events which are supported in any v8.1 PMU */
1156 return cpu_isar_feature(any_pmu_8_4, env_archcpu(env));
1157}
1158
0727f63b
PM
1159static uint64_t zero_event_get_count(CPUARMState *env)
1160{
1161 /* For events which on QEMU never fire, so their count is always zero */
1162 return 0;
1163}
1164
1165static int64_t zero_event_ns_per(uint64_t cycles)
1166{
1167 /* An event which never fires can never overflow */
1168 return -1;
1169}
1170
57a4a11b 1171static const pm_event pm_events[] = {
0d4bfd7d
AL
1172 { .number = 0x000, /* SW_INCR */
1173 .supported = event_always_supported,
1174 .get_count = swinc_get_count,
4e7beb0c 1175 .ns_per_count = swinc_ns_per,
0d4bfd7d 1176 },
b2e23725
AL
1177#ifndef CONFIG_USER_ONLY
1178 { .number = 0x008, /* INST_RETIRED, Instruction architecturally executed */
1179 .supported = instructions_supported,
1180 .get_count = instructions_get_count,
4e7beb0c 1181 .ns_per_count = instructions_ns_per,
b2e23725
AL
1182 },
1183 { .number = 0x011, /* CPU_CYCLES, Cycle */
1184 .supported = event_always_supported,
1185 .get_count = cycles_get_count,
4e7beb0c 1186 .ns_per_count = cycles_ns_per,
0727f63b 1187 },
b2e23725 1188#endif
0727f63b
PM
1189 { .number = 0x023, /* STALL_FRONTEND */
1190 .supported = pmu_8_1_events_supported,
1191 .get_count = zero_event_get_count,
1192 .ns_per_count = zero_event_ns_per,
1193 },
1194 { .number = 0x024, /* STALL_BACKEND */
1195 .supported = pmu_8_1_events_supported,
1196 .get_count = zero_event_get_count,
1197 .ns_per_count = zero_event_ns_per,
1198 },
15dd1ebd
PM
1199 { .number = 0x03c, /* STALL */
1200 .supported = pmu_8_4_events_supported,
1201 .get_count = zero_event_get_count,
1202 .ns_per_count = zero_event_ns_per,
1203 },
57a4a11b
AL
1204};
1205
1206/*
1207 * Note: Before increasing MAX_EVENT_ID beyond 0x3f into the 0x40xx range of
1208 * events (i.e. the statistical profiling extension), this implementation
1209 * should first be updated to something sparse instead of the current
1210 * supported_event_map[] array.
1211 */
15dd1ebd 1212#define MAX_EVENT_ID 0x3c
57a4a11b
AL
1213#define UNSUPPORTED_EVENT UINT16_MAX
1214static uint16_t supported_event_map[MAX_EVENT_ID + 1];
1215
1216/*
bf8d0969
AL
1217 * Called upon CPU initialization to initialize PMCEID[01]_EL0 and build a map
1218 * of ARM event numbers to indices in our pm_events array.
57a4a11b
AL
1219 *
1220 * Note: Events in the 0x40XX range are not currently supported.
1221 */
bf8d0969 1222void pmu_init(ARMCPU *cpu)
57a4a11b 1223{
57a4a11b
AL
1224 unsigned int i;
1225
bf8d0969
AL
1226 /*
1227 * Empty supported_event_map and cpu->pmceid[01] before adding supported
1228 * events to them
1229 */
57a4a11b
AL
1230 for (i = 0; i < ARRAY_SIZE(supported_event_map); i++) {
1231 supported_event_map[i] = UNSUPPORTED_EVENT;
1232 }
bf8d0969
AL
1233 cpu->pmceid0 = 0;
1234 cpu->pmceid1 = 0;
57a4a11b
AL
1235
1236 for (i = 0; i < ARRAY_SIZE(pm_events); i++) {
1237 const pm_event *cnt = &pm_events[i];
1238 assert(cnt->number <= MAX_EVENT_ID);
1239 /* We do not currently support events in the 0x40xx range */
1240 assert(cnt->number <= 0x3f);
1241
bf8d0969 1242 if (cnt->supported(&cpu->env)) {
57a4a11b 1243 supported_event_map[cnt->number] = i;
67da43d6 1244 uint64_t event_mask = 1ULL << (cnt->number & 0x1f);
bf8d0969
AL
1245 if (cnt->number & 0x20) {
1246 cpu->pmceid1 |= event_mask;
1247 } else {
1248 cpu->pmceid0 |= event_mask;
1249 }
57a4a11b
AL
1250 }
1251 }
57a4a11b
AL
1252}
1253
5ecdd3e4
AL
1254/*
1255 * Check at runtime whether a PMU event is supported for the current machine
1256 */
1257static bool event_supported(uint16_t number)
1258{
1259 if (number > MAX_EVENT_ID) {
1260 return false;
1261 }
1262 return supported_event_map[number] != UNSUPPORTED_EVENT;
1263}
1264
3f208fd7
PM
1265static CPAccessResult pmreg_access(CPUARMState *env, const ARMCPRegInfo *ri,
1266 bool isread)
200ac0ef 1267{
3b163b01 1268 /* Performance monitor registers user accessibility is controlled
1fce1ba9
PM
1269 * by PMUSERENR. MDCR_EL2.TPM and MDCR_EL3.TPM allow configurable
1270 * trapping to EL2 or EL3 for other accesses.
200ac0ef 1271 */
1fce1ba9
PM
1272 int el = arm_current_el(env);
1273
6ecd0b6b 1274 if (el == 0 && !(env->cp15.c9_pmuserenr & 1)) {
fcd25206 1275 return CP_ACCESS_TRAP;
200ac0ef 1276 }
1fce1ba9
PM
1277 if (el < 2 && (env->cp15.mdcr_el2 & MDCR_TPM)
1278 && !arm_is_secure_below_el3(env)) {
1279 return CP_ACCESS_TRAP_EL2;
1280 }
1281 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) {
1282 return CP_ACCESS_TRAP_EL3;
1283 }
1284
fcd25206 1285 return CP_ACCESS_OK;
200ac0ef
PM
1286}
1287
6ecd0b6b
AB
1288static CPAccessResult pmreg_access_xevcntr(CPUARMState *env,
1289 const ARMCPRegInfo *ri,
1290 bool isread)
1291{
1292 /* ER: event counter read trap control */
1293 if (arm_feature(env, ARM_FEATURE_V8)
1294 && arm_current_el(env) == 0
1295 && (env->cp15.c9_pmuserenr & (1 << 3)) != 0
1296 && isread) {
1297 return CP_ACCESS_OK;
1298 }
1299
1300 return pmreg_access(env, ri, isread);
1301}
1302
1303static CPAccessResult pmreg_access_swinc(CPUARMState *env,
1304 const ARMCPRegInfo *ri,
1305 bool isread)
1306{
1307 /* SW: software increment write trap control */
1308 if (arm_feature(env, ARM_FEATURE_V8)
1309 && arm_current_el(env) == 0
1310 && (env->cp15.c9_pmuserenr & (1 << 1)) != 0
1311 && !isread) {
1312 return CP_ACCESS_OK;
1313 }
1314
1315 return pmreg_access(env, ri, isread);
1316}
1317
6ecd0b6b
AB
1318static CPAccessResult pmreg_access_selr(CPUARMState *env,
1319 const ARMCPRegInfo *ri,
1320 bool isread)
1321{
1322 /* ER: event counter read trap control */
1323 if (arm_feature(env, ARM_FEATURE_V8)
1324 && arm_current_el(env) == 0
1325 && (env->cp15.c9_pmuserenr & (1 << 3)) != 0) {
1326 return CP_ACCESS_OK;
1327 }
1328
1329 return pmreg_access(env, ri, isread);
1330}
1331
1332static CPAccessResult pmreg_access_ccntr(CPUARMState *env,
1333 const ARMCPRegInfo *ri,
1334 bool isread)
1335{
1336 /* CR: cycle counter read trap control */
1337 if (arm_feature(env, ARM_FEATURE_V8)
1338 && arm_current_el(env) == 0
1339 && (env->cp15.c9_pmuserenr & (1 << 2)) != 0
1340 && isread) {
1341 return CP_ACCESS_OK;
1342 }
1343
1344 return pmreg_access(env, ri, isread);
1345}
1346
033614c4
AL
1347/* Returns true if the counter (pass 31 for PMCCNTR) should count events using
1348 * the current EL, security state, and register configuration.
1349 */
1350static bool pmu_counter_enabled(CPUARMState *env, uint8_t counter)
87124fde 1351{
033614c4
AL
1352 uint64_t filter;
1353 bool e, p, u, nsk, nsu, nsh, m;
1354 bool enabled, prohibited, filtered;
1355 bool secure = arm_is_secure(env);
1356 int el = arm_current_el(env);
1357 uint8_t hpmn = env->cp15.mdcr_el2 & MDCR_HPMN;
87124fde 1358
cbbb3041
AJ
1359 if (!arm_feature(env, ARM_FEATURE_PMU)) {
1360 return false;
1361 }
1362
033614c4
AL
1363 if (!arm_feature(env, ARM_FEATURE_EL2) ||
1364 (counter < hpmn || counter == 31)) {
1365 e = env->cp15.c9_pmcr & PMCRE;
1366 } else {
1367 e = env->cp15.mdcr_el2 & MDCR_HPME;
87124fde 1368 }
033614c4 1369 enabled = e && (env->cp15.c9_pmcnten & (1 << counter));
87124fde 1370
033614c4
AL
1371 if (!secure) {
1372 if (el == 2 && (counter < hpmn || counter == 31)) {
1373 prohibited = env->cp15.mdcr_el2 & MDCR_HPMD;
1374 } else {
1375 prohibited = false;
1376 }
1377 } else {
1378 prohibited = arm_feature(env, ARM_FEATURE_EL3) &&
1379 (env->cp15.mdcr_el3 & MDCR_SPME);
1380 }
1381
1382 if (prohibited && counter == 31) {
1383 prohibited = env->cp15.c9_pmcr & PMCRDP;
1384 }
1385
5ecdd3e4
AL
1386 if (counter == 31) {
1387 filter = env->cp15.pmccfiltr_el0;
1388 } else {
1389 filter = env->cp15.c14_pmevtyper[counter];
1390 }
033614c4
AL
1391
1392 p = filter & PMXEVTYPER_P;
1393 u = filter & PMXEVTYPER_U;
1394 nsk = arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_NSK);
1395 nsu = arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_NSU);
1396 nsh = arm_feature(env, ARM_FEATURE_EL2) && (filter & PMXEVTYPER_NSH);
1397 m = arm_el_is_aa64(env, 1) &&
1398 arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_M);
1399
1400 if (el == 0) {
1401 filtered = secure ? u : u != nsu;
1402 } else if (el == 1) {
1403 filtered = secure ? p : p != nsk;
1404 } else if (el == 2) {
1405 filtered = !nsh;
1406 } else { /* EL3 */
1407 filtered = m != p;
1408 }
1409
5ecdd3e4
AL
1410 if (counter != 31) {
1411 /*
1412 * If not checking PMCCNTR, ensure the counter is setup to an event we
1413 * support
1414 */
1415 uint16_t event = filter & PMXEVTYPER_EVTCOUNT;
1416 if (!event_supported(event)) {
1417 return false;
1418 }
1419 }
1420
033614c4 1421 return enabled && !prohibited && !filtered;
87124fde 1422}
033614c4 1423
f4efb4b2
AL
1424static void pmu_update_irq(CPUARMState *env)
1425{
2fc0cc0e 1426 ARMCPU *cpu = env_archcpu(env);
f4efb4b2
AL
1427 qemu_set_irq(cpu->pmu_interrupt, (env->cp15.c9_pmcr & PMCRE) &&
1428 (env->cp15.c9_pminten & env->cp15.c9_pmovsr));
1429}
1430
5d05b9d4
AL
1431/*
1432 * Ensure c15_ccnt is the guest-visible count so that operations such as
1433 * enabling/disabling the counter or filtering, modifying the count itself,
1434 * etc. can be done logically. This is essentially a no-op if the counter is
1435 * not enabled at the time of the call.
1436 */
f2b2f53f 1437static void pmccntr_op_start(CPUARMState *env)
ec7b4ce4 1438{
b2e23725 1439 uint64_t cycles = cycles_get_count(env);
ec7b4ce4 1440
033614c4 1441 if (pmu_counter_enabled(env, 31)) {
5d05b9d4
AL
1442 uint64_t eff_cycles = cycles;
1443 if (env->cp15.c9_pmcr & PMCRD) {
1444 /* Increment once every 64 processor clock cycles */
1445 eff_cycles /= 64;
1446 }
1447
f4efb4b2
AL
1448 uint64_t new_pmccntr = eff_cycles - env->cp15.c15_ccnt_delta;
1449
1450 uint64_t overflow_mask = env->cp15.c9_pmcr & PMCRLC ? \
1451 1ull << 63 : 1ull << 31;
1452 if (env->cp15.c15_ccnt & ~new_pmccntr & overflow_mask) {
1453 env->cp15.c9_pmovsr |= (1 << 31);
1454 pmu_update_irq(env);
1455 }
1456
1457 env->cp15.c15_ccnt = new_pmccntr;
ec7b4ce4 1458 }
5d05b9d4
AL
1459 env->cp15.c15_ccnt_delta = cycles;
1460}
ec7b4ce4 1461
5d05b9d4
AL
1462/*
1463 * If PMCCNTR is enabled, recalculate the delta between the clock and the
1464 * guest-visible count. A call to pmccntr_op_finish should follow every call to
1465 * pmccntr_op_start.
1466 */
f2b2f53f 1467static void pmccntr_op_finish(CPUARMState *env)
5d05b9d4 1468{
033614c4 1469 if (pmu_counter_enabled(env, 31)) {
4e7beb0c
AL
1470#ifndef CONFIG_USER_ONLY
1471 /* Calculate when the counter will next overflow */
1472 uint64_t remaining_cycles = -env->cp15.c15_ccnt;
1473 if (!(env->cp15.c9_pmcr & PMCRLC)) {
1474 remaining_cycles = (uint32_t)remaining_cycles;
1475 }
1476 int64_t overflow_in = cycles_ns_per(remaining_cycles);
1477
1478 if (overflow_in > 0) {
1479 int64_t overflow_at = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
1480 overflow_in;
2fc0cc0e 1481 ARMCPU *cpu = env_archcpu(env);
4e7beb0c
AL
1482 timer_mod_anticipate_ns(cpu->pmu_timer, overflow_at);
1483 }
1484#endif
5d05b9d4 1485
4e7beb0c 1486 uint64_t prev_cycles = env->cp15.c15_ccnt_delta;
5d05b9d4
AL
1487 if (env->cp15.c9_pmcr & PMCRD) {
1488 /* Increment once every 64 processor clock cycles */
1489 prev_cycles /= 64;
1490 }
5d05b9d4 1491 env->cp15.c15_ccnt_delta = prev_cycles - env->cp15.c15_ccnt;
ec7b4ce4
AF
1492 }
1493}
1494
5ecdd3e4
AL
1495static void pmevcntr_op_start(CPUARMState *env, uint8_t counter)
1496{
1497
1498 uint16_t event = env->cp15.c14_pmevtyper[counter] & PMXEVTYPER_EVTCOUNT;
1499 uint64_t count = 0;
1500 if (event_supported(event)) {
1501 uint16_t event_idx = supported_event_map[event];
1502 count = pm_events[event_idx].get_count(env);
1503 }
1504
1505 if (pmu_counter_enabled(env, counter)) {
f4efb4b2
AL
1506 uint32_t new_pmevcntr = count - env->cp15.c14_pmevcntr_delta[counter];
1507
1508 if (env->cp15.c14_pmevcntr[counter] & ~new_pmevcntr & INT32_MIN) {
1509 env->cp15.c9_pmovsr |= (1 << counter);
1510 pmu_update_irq(env);
1511 }
1512 env->cp15.c14_pmevcntr[counter] = new_pmevcntr;
5ecdd3e4
AL
1513 }
1514 env->cp15.c14_pmevcntr_delta[counter] = count;
1515}
1516
1517static void pmevcntr_op_finish(CPUARMState *env, uint8_t counter)
1518{
1519 if (pmu_counter_enabled(env, counter)) {
4e7beb0c
AL
1520#ifndef CONFIG_USER_ONLY
1521 uint16_t event = env->cp15.c14_pmevtyper[counter] & PMXEVTYPER_EVTCOUNT;
1522 uint16_t event_idx = supported_event_map[event];
1523 uint64_t delta = UINT32_MAX -
1524 (uint32_t)env->cp15.c14_pmevcntr[counter] + 1;
1525 int64_t overflow_in = pm_events[event_idx].ns_per_count(delta);
1526
1527 if (overflow_in > 0) {
1528 int64_t overflow_at = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
1529 overflow_in;
2fc0cc0e 1530 ARMCPU *cpu = env_archcpu(env);
4e7beb0c
AL
1531 timer_mod_anticipate_ns(cpu->pmu_timer, overflow_at);
1532 }
1533#endif
1534
5ecdd3e4
AL
1535 env->cp15.c14_pmevcntr_delta[counter] -=
1536 env->cp15.c14_pmevcntr[counter];
1537 }
1538}
1539
5d05b9d4
AL
1540void pmu_op_start(CPUARMState *env)
1541{
5ecdd3e4 1542 unsigned int i;
5d05b9d4 1543 pmccntr_op_start(env);
5ecdd3e4
AL
1544 for (i = 0; i < pmu_num_counters(env); i++) {
1545 pmevcntr_op_start(env, i);
1546 }
5d05b9d4
AL
1547}
1548
1549void pmu_op_finish(CPUARMState *env)
1550{
5ecdd3e4 1551 unsigned int i;
5d05b9d4 1552 pmccntr_op_finish(env);
5ecdd3e4
AL
1553 for (i = 0; i < pmu_num_counters(env); i++) {
1554 pmevcntr_op_finish(env, i);
1555 }
5d05b9d4
AL
1556}
1557
033614c4
AL
1558void pmu_pre_el_change(ARMCPU *cpu, void *ignored)
1559{
1560 pmu_op_start(&cpu->env);
1561}
1562
1563void pmu_post_el_change(ARMCPU *cpu, void *ignored)
1564{
1565 pmu_op_finish(&cpu->env);
1566}
1567
4e7beb0c
AL
1568void arm_pmu_timer_cb(void *opaque)
1569{
1570 ARMCPU *cpu = opaque;
1571
1572 /*
1573 * Update all the counter values based on the current underlying counts,
1574 * triggering interrupts to be raised, if necessary. pmu_op_finish() also
1575 * has the effect of setting the cpu->pmu_timer to the next earliest time a
1576 * counter may expire.
1577 */
1578 pmu_op_start(&cpu->env);
1579 pmu_op_finish(&cpu->env);
1580}
1581
c4241c7d
PM
1582static void pmcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1583 uint64_t value)
200ac0ef 1584{
5d05b9d4 1585 pmu_op_start(env);
7c2cb42b
AF
1586
1587 if (value & PMCRC) {
1588 /* The counter has been reset */
1589 env->cp15.c15_ccnt = 0;
1590 }
1591
5ecdd3e4
AL
1592 if (value & PMCRP) {
1593 unsigned int i;
1594 for (i = 0; i < pmu_num_counters(env); i++) {
1595 env->cp15.c14_pmevcntr[i] = 0;
1596 }
1597 }
1598
62d96ff4
PM
1599 env->cp15.c9_pmcr &= ~PMCR_WRITEABLE_MASK;
1600 env->cp15.c9_pmcr |= (value & PMCR_WRITEABLE_MASK);
7c2cb42b 1601
5d05b9d4 1602 pmu_op_finish(env);
7c2cb42b
AF
1603}
1604
0d4bfd7d
AL
1605static void pmswinc_write(CPUARMState *env, const ARMCPRegInfo *ri,
1606 uint64_t value)
1607{
1608 unsigned int i;
1609 for (i = 0; i < pmu_num_counters(env); i++) {
1610 /* Increment a counter's count iff: */
1611 if ((value & (1 << i)) && /* counter's bit is set */
1612 /* counter is enabled and not filtered */
1613 pmu_counter_enabled(env, i) &&
1614 /* counter is SW_INCR */
1615 (env->cp15.c14_pmevtyper[i] & PMXEVTYPER_EVTCOUNT) == 0x0) {
1616 pmevcntr_op_start(env, i);
f4efb4b2
AL
1617
1618 /*
1619 * Detect if this write causes an overflow since we can't predict
1620 * PMSWINC overflows like we can for other events
1621 */
1622 uint32_t new_pmswinc = env->cp15.c14_pmevcntr[i] + 1;
1623
1624 if (env->cp15.c14_pmevcntr[i] & ~new_pmswinc & INT32_MIN) {
1625 env->cp15.c9_pmovsr |= (1 << i);
1626 pmu_update_irq(env);
1627 }
1628
1629 env->cp15.c14_pmevcntr[i] = new_pmswinc;
1630
0d4bfd7d
AL
1631 pmevcntr_op_finish(env, i);
1632 }
1633 }
1634}
1635
7c2cb42b
AF
1636static uint64_t pmccntr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1637{
5d05b9d4
AL
1638 uint64_t ret;
1639 pmccntr_op_start(env);
1640 ret = env->cp15.c15_ccnt;
1641 pmccntr_op_finish(env);
1642 return ret;
7c2cb42b
AF
1643}
1644
6b040780
WH
1645static void pmselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1646 uint64_t value)
1647{
1648 /* The value of PMSELR.SEL affects the behavior of PMXEVTYPER and
1649 * PMXEVCNTR. We allow [0..31] to be written to PMSELR here; in the
1650 * meanwhile, we check PMSELR.SEL when PMXEVTYPER and PMXEVCNTR are
1651 * accessed.
1652 */
1653 env->cp15.c9_pmselr = value & 0x1f;
1654}
1655
7c2cb42b
AF
1656static void pmccntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1657 uint64_t value)
1658{
5d05b9d4
AL
1659 pmccntr_op_start(env);
1660 env->cp15.c15_ccnt = value;
1661 pmccntr_op_finish(env);
200ac0ef 1662}
421c7ebd
PC
1663
1664static void pmccntr_write32(CPUARMState *env, const ARMCPRegInfo *ri,
1665 uint64_t value)
1666{
1667 uint64_t cur_val = pmccntr_read(env, NULL);
1668
1669 pmccntr_write(env, ri, deposit64(cur_val, 0, 32, value));
1670}
1671
0614601c
AF
1672static void pmccfiltr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1673 uint64_t value)
1674{
5d05b9d4 1675 pmccntr_op_start(env);
4b8afa1f
AL
1676 env->cp15.pmccfiltr_el0 = value & PMCCFILTR_EL0;
1677 pmccntr_op_finish(env);
1678}
1679
1680static void pmccfiltr_write_a32(CPUARMState *env, const ARMCPRegInfo *ri,
1681 uint64_t value)
1682{
1683 pmccntr_op_start(env);
1684 /* M is not accessible from AArch32 */
1685 env->cp15.pmccfiltr_el0 = (env->cp15.pmccfiltr_el0 & PMCCFILTR_M) |
1686 (value & PMCCFILTR);
5d05b9d4 1687 pmccntr_op_finish(env);
0614601c
AF
1688}
1689
4b8afa1f
AL
1690static uint64_t pmccfiltr_read_a32(CPUARMState *env, const ARMCPRegInfo *ri)
1691{
1692 /* M is not visible in AArch32 */
1693 return env->cp15.pmccfiltr_el0 & PMCCFILTR;
1694}
1695
c4241c7d 1696static void pmcntenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
200ac0ef
PM
1697 uint64_t value)
1698{
7ece99b1 1699 value &= pmu_counter_mask(env);
200ac0ef 1700 env->cp15.c9_pmcnten |= value;
200ac0ef
PM
1701}
1702
c4241c7d
PM
1703static void pmcntenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1704 uint64_t value)
200ac0ef 1705{
7ece99b1 1706 value &= pmu_counter_mask(env);
200ac0ef 1707 env->cp15.c9_pmcnten &= ~value;
200ac0ef
PM
1708}
1709
c4241c7d
PM
1710static void pmovsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1711 uint64_t value)
200ac0ef 1712{
599b71e2 1713 value &= pmu_counter_mask(env);
200ac0ef 1714 env->cp15.c9_pmovsr &= ~value;
f4efb4b2 1715 pmu_update_irq(env);
200ac0ef
PM
1716}
1717
327dd510
AL
1718static void pmovsset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1719 uint64_t value)
1720{
1721 value &= pmu_counter_mask(env);
1722 env->cp15.c9_pmovsr |= value;
f4efb4b2 1723 pmu_update_irq(env);
327dd510
AL
1724}
1725
5ecdd3e4
AL
1726static void pmevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
1727 uint64_t value, const uint8_t counter)
200ac0ef 1728{
5ecdd3e4
AL
1729 if (counter == 31) {
1730 pmccfiltr_write(env, ri, value);
1731 } else if (counter < pmu_num_counters(env)) {
1732 pmevcntr_op_start(env, counter);
1733
1734 /*
1735 * If this counter's event type is changing, store the current
1736 * underlying count for the new type in c14_pmevcntr_delta[counter] so
1737 * pmevcntr_op_finish has the correct baseline when it converts back to
1738 * a delta.
1739 */
1740 uint16_t old_event = env->cp15.c14_pmevtyper[counter] &
1741 PMXEVTYPER_EVTCOUNT;
1742 uint16_t new_event = value & PMXEVTYPER_EVTCOUNT;
1743 if (old_event != new_event) {
1744 uint64_t count = 0;
1745 if (event_supported(new_event)) {
1746 uint16_t event_idx = supported_event_map[new_event];
1747 count = pm_events[event_idx].get_count(env);
1748 }
1749 env->cp15.c14_pmevcntr_delta[counter] = count;
1750 }
1751
1752 env->cp15.c14_pmevtyper[counter] = value & PMXEVTYPER_MASK;
1753 pmevcntr_op_finish(env, counter);
1754 }
fdb86656
WH
1755 /* Attempts to access PMXEVTYPER are CONSTRAINED UNPREDICTABLE when
1756 * PMSELR value is equal to or greater than the number of implemented
1757 * counters, but not equal to 0x1f. We opt to behave as a RAZ/WI.
1758 */
5ecdd3e4
AL
1759}
1760
1761static uint64_t pmevtyper_read(CPUARMState *env, const ARMCPRegInfo *ri,
1762 const uint8_t counter)
1763{
1764 if (counter == 31) {
1765 return env->cp15.pmccfiltr_el0;
1766 } else if (counter < pmu_num_counters(env)) {
1767 return env->cp15.c14_pmevtyper[counter];
1768 } else {
1769 /*
1770 * We opt to behave as a RAZ/WI when attempts to access PMXEVTYPER
1771 * are CONSTRAINED UNPREDICTABLE. See comments in pmevtyper_write().
1772 */
1773 return 0;
1774 }
1775}
1776
1777static void pmevtyper_writefn(CPUARMState *env, const ARMCPRegInfo *ri,
1778 uint64_t value)
1779{
1780 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1781 pmevtyper_write(env, ri, value, counter);
1782}
1783
1784static void pmevtyper_rawwrite(CPUARMState *env, const ARMCPRegInfo *ri,
1785 uint64_t value)
1786{
1787 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1788 env->cp15.c14_pmevtyper[counter] = value;
1789
1790 /*
1791 * pmevtyper_rawwrite is called between a pair of pmu_op_start and
1792 * pmu_op_finish calls when loading saved state for a migration. Because
1793 * we're potentially updating the type of event here, the value written to
1794 * c14_pmevcntr_delta by the preceeding pmu_op_start call may be for a
1795 * different counter type. Therefore, we need to set this value to the
1796 * current count for the counter type we're writing so that pmu_op_finish
1797 * has the correct count for its calculation.
1798 */
1799 uint16_t event = value & PMXEVTYPER_EVTCOUNT;
1800 if (event_supported(event)) {
1801 uint16_t event_idx = supported_event_map[event];
1802 env->cp15.c14_pmevcntr_delta[counter] =
1803 pm_events[event_idx].get_count(env);
fdb86656
WH
1804 }
1805}
1806
5ecdd3e4
AL
1807static uint64_t pmevtyper_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
1808{
1809 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1810 return pmevtyper_read(env, ri, counter);
1811}
1812
1813static void pmxevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
1814 uint64_t value)
1815{
1816 pmevtyper_write(env, ri, value, env->cp15.c9_pmselr & 31);
1817}
1818
fdb86656
WH
1819static uint64_t pmxevtyper_read(CPUARMState *env, const ARMCPRegInfo *ri)
1820{
5ecdd3e4
AL
1821 return pmevtyper_read(env, ri, env->cp15.c9_pmselr & 31);
1822}
1823
1824static void pmevcntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1825 uint64_t value, uint8_t counter)
1826{
1827 if (counter < pmu_num_counters(env)) {
1828 pmevcntr_op_start(env, counter);
1829 env->cp15.c14_pmevcntr[counter] = value;
1830 pmevcntr_op_finish(env, counter);
1831 }
1832 /*
1833 * We opt to behave as a RAZ/WI when attempts to access PM[X]EVCNTR
1834 * are CONSTRAINED UNPREDICTABLE.
fdb86656 1835 */
5ecdd3e4
AL
1836}
1837
1838static uint64_t pmevcntr_read(CPUARMState *env, const ARMCPRegInfo *ri,
1839 uint8_t counter)
1840{
1841 if (counter < pmu_num_counters(env)) {
1842 uint64_t ret;
1843 pmevcntr_op_start(env, counter);
1844 ret = env->cp15.c14_pmevcntr[counter];
1845 pmevcntr_op_finish(env, counter);
1846 return ret;
fdb86656 1847 } else {
5ecdd3e4
AL
1848 /* We opt to behave as a RAZ/WI when attempts to access PM[X]EVCNTR
1849 * are CONSTRAINED UNPREDICTABLE. */
fdb86656
WH
1850 return 0;
1851 }
200ac0ef
PM
1852}
1853
5ecdd3e4
AL
1854static void pmevcntr_writefn(CPUARMState *env, const ARMCPRegInfo *ri,
1855 uint64_t value)
1856{
1857 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1858 pmevcntr_write(env, ri, value, counter);
1859}
1860
1861static uint64_t pmevcntr_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
1862{
1863 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1864 return pmevcntr_read(env, ri, counter);
1865}
1866
1867static void pmevcntr_rawwrite(CPUARMState *env, const ARMCPRegInfo *ri,
1868 uint64_t value)
1869{
1870 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1871 assert(counter < pmu_num_counters(env));
1872 env->cp15.c14_pmevcntr[counter] = value;
1873 pmevcntr_write(env, ri, value, counter);
1874}
1875
1876static uint64_t pmevcntr_rawread(CPUARMState *env, const ARMCPRegInfo *ri)
1877{
1878 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1879 assert(counter < pmu_num_counters(env));
1880 return env->cp15.c14_pmevcntr[counter];
1881}
1882
1883static void pmxevcntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1884 uint64_t value)
1885{
1886 pmevcntr_write(env, ri, value, env->cp15.c9_pmselr & 31);
1887}
1888
1889static uint64_t pmxevcntr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1890{
1891 return pmevcntr_read(env, ri, env->cp15.c9_pmselr & 31);
1892}
1893
c4241c7d 1894static void pmuserenr_write(CPUARMState *env, const ARMCPRegInfo *ri,
200ac0ef
PM
1895 uint64_t value)
1896{
6ecd0b6b
AB
1897 if (arm_feature(env, ARM_FEATURE_V8)) {
1898 env->cp15.c9_pmuserenr = value & 0xf;
1899 } else {
1900 env->cp15.c9_pmuserenr = value & 1;
1901 }
200ac0ef
PM
1902}
1903
c4241c7d
PM
1904static void pmintenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1905 uint64_t value)
200ac0ef
PM
1906{
1907 /* We have no event counters so only the C bit can be changed */
7ece99b1 1908 value &= pmu_counter_mask(env);
200ac0ef 1909 env->cp15.c9_pminten |= value;
f4efb4b2 1910 pmu_update_irq(env);
200ac0ef
PM
1911}
1912
c4241c7d
PM
1913static void pmintenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1914 uint64_t value)
200ac0ef 1915{
7ece99b1 1916 value &= pmu_counter_mask(env);
200ac0ef 1917 env->cp15.c9_pminten &= ~value;
f4efb4b2 1918 pmu_update_irq(env);
200ac0ef
PM
1919}
1920
c4241c7d
PM
1921static void vbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
1922 uint64_t value)
8641136c 1923{
a505d7fe
PM
1924 /* Note that even though the AArch64 view of this register has bits
1925 * [10:0] all RES0 we can only mask the bottom 5, to comply with the
1926 * architectural requirements for bits which are RES0 only in some
1927 * contexts. (ARMv8 would permit us to do no masking at all, but ARMv7
1928 * requires the bottom five bits to be RAZ/WI because they're UNK/SBZP.)
1929 */
855ea66d 1930 raw_write(env, ri, value & ~0x1FULL);
8641136c
NR
1931}
1932
64e0e2de
EI
1933static void scr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
1934{
ea22747c
RH
1935 /* Begin with base v8.0 state. */
1936 uint32_t valid_mask = 0x3fff;
2fc0cc0e 1937 ARMCPU *cpu = env_archcpu(env);
ea22747c
RH
1938
1939 if (arm_el_is_aa64(env, 3)) {
1940 value |= SCR_FW | SCR_AW; /* these two bits are RES1. */
1941 valid_mask &= ~SCR_NET;
1942 } else {
1943 valid_mask &= ~(SCR_RW | SCR_ST);
1944 }
64e0e2de
EI
1945
1946 if (!arm_feature(env, ARM_FEATURE_EL2)) {
1947 valid_mask &= ~SCR_HCE;
1948
1949 /* On ARMv7, SMD (or SCD as it is called in v7) is only
1950 * supported if EL2 exists. The bit is UNK/SBZP when
1951 * EL2 is unavailable. In QEMU ARMv7, we force it to always zero
1952 * when EL2 is unavailable.
4eb27640 1953 * On ARMv8, this bit is always available.
64e0e2de 1954 */
4eb27640
GB
1955 if (arm_feature(env, ARM_FEATURE_V7) &&
1956 !arm_feature(env, ARM_FEATURE_V8)) {
64e0e2de
EI
1957 valid_mask &= ~SCR_SMD;
1958 }
1959 }
2d7137c1
RH
1960 if (cpu_isar_feature(aa64_lor, cpu)) {
1961 valid_mask |= SCR_TLOR;
1962 }
ef682cdb
RH
1963 if (cpu_isar_feature(aa64_pauth, cpu)) {
1964 valid_mask |= SCR_API | SCR_APK;
1965 }
64e0e2de
EI
1966
1967 /* Clear all-context RES0 bits. */
1968 value &= valid_mask;
1969 raw_write(env, ri, value);
1970}
1971
630fcd4d
MZ
1972static CPAccessResult access_aa64_tid2(CPUARMState *env,
1973 const ARMCPRegInfo *ri,
1974 bool isread)
1975{
1976 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID2)) {
1977 return CP_ACCESS_TRAP_EL2;
1978 }
1979
1980 return CP_ACCESS_OK;
1981}
1982
c4241c7d 1983static uint64_t ccsidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
776d4e5c 1984{
2fc0cc0e 1985 ARMCPU *cpu = env_archcpu(env);
b85a1fd6
FA
1986
1987 /* Acquire the CSSELR index from the bank corresponding to the CCSIDR
1988 * bank
1989 */
1990 uint32_t index = A32_BANKED_REG_GET(env, csselr,
1991 ri->secure & ARM_CP_SECSTATE_S);
1992
1993 return cpu->ccsidr[index];
776d4e5c
PM
1994}
1995
c4241c7d
PM
1996static void csselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1997 uint64_t value)
776d4e5c 1998{
8d5c773e 1999 raw_write(env, ri, value & 0xf);
776d4e5c
PM
2000}
2001
1090b9c6
PM
2002static uint64_t isr_read(CPUARMState *env, const ARMCPRegInfo *ri)
2003{
29a0af61 2004 CPUState *cs = env_cpu(env);
f7778444 2005 uint64_t hcr_el2 = arm_hcr_el2_eff(env);
1090b9c6 2006 uint64_t ret = 0;
7cf95aed
MZ
2007 bool allow_virt = (arm_current_el(env) == 1 &&
2008 (!arm_is_secure_below_el3(env) ||
2009 (env->cp15.scr_el3 & SCR_EEL2)));
1090b9c6 2010
7cf95aed 2011 if (allow_virt && (hcr_el2 & HCR_IMO)) {
636540e9
PM
2012 if (cs->interrupt_request & CPU_INTERRUPT_VIRQ) {
2013 ret |= CPSR_I;
2014 }
2015 } else {
2016 if (cs->interrupt_request & CPU_INTERRUPT_HARD) {
2017 ret |= CPSR_I;
2018 }
1090b9c6 2019 }
636540e9 2020
7cf95aed 2021 if (allow_virt && (hcr_el2 & HCR_FMO)) {
636540e9
PM
2022 if (cs->interrupt_request & CPU_INTERRUPT_VFIQ) {
2023 ret |= CPSR_F;
2024 }
2025 } else {
2026 if (cs->interrupt_request & CPU_INTERRUPT_FIQ) {
2027 ret |= CPSR_F;
2028 }
1090b9c6 2029 }
636540e9 2030
1090b9c6
PM
2031 /* External aborts are not possible in QEMU so A bit is always clear */
2032 return ret;
2033}
2034
93fbc983
MZ
2035static CPAccessResult access_aa64_tid1(CPUARMState *env, const ARMCPRegInfo *ri,
2036 bool isread)
2037{
2038 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID1)) {
2039 return CP_ACCESS_TRAP_EL2;
2040 }
2041
2042 return CP_ACCESS_OK;
2043}
2044
2045static CPAccessResult access_aa32_tid1(CPUARMState *env, const ARMCPRegInfo *ri,
2046 bool isread)
2047{
2048 if (arm_feature(env, ARM_FEATURE_V8)) {
2049 return access_aa64_tid1(env, ri, isread);
2050 }
2051
2052 return CP_ACCESS_OK;
2053}
2054
e9aa6c21 2055static const ARMCPRegInfo v7_cp_reginfo[] = {
7d57f408
PM
2056 /* the old v6 WFI, UNPREDICTABLE in v7 but we choose to NOP */
2057 { .name = "NOP", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
2058 .access = PL1_W, .type = ARM_CP_NOP },
200ac0ef
PM
2059 /* Performance monitors are implementation defined in v7,
2060 * but with an ARM recommended set of registers, which we
ac689a2e 2061 * follow.
200ac0ef
PM
2062 *
2063 * Performance registers fall into three categories:
2064 * (a) always UNDEF in PL0, RW in PL1 (PMINTENSET, PMINTENCLR)
2065 * (b) RO in PL0 (ie UNDEF on write), RW in PL1 (PMUSERENR)
2066 * (c) UNDEF in PL0 if PMUSERENR.EN==0, otherwise accessible (all others)
2067 * For the cases controlled by PMUSERENR we must set .access to PL0_RW
2068 * or PL0_RO as appropriate and then check PMUSERENR in the helper fn.
2069 */
2070 { .name = "PMCNTENSET", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 1,
7a0e58fa 2071 .access = PL0_RW, .type = ARM_CP_ALIAS,
8521466b 2072 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
fcd25206
PM
2073 .writefn = pmcntenset_write,
2074 .accessfn = pmreg_access,
2075 .raw_writefn = raw_write },
8521466b
AF
2076 { .name = "PMCNTENSET_EL0", .state = ARM_CP_STATE_AA64,
2077 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 1,
2078 .access = PL0_RW, .accessfn = pmreg_access,
2079 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten), .resetvalue = 0,
2080 .writefn = pmcntenset_write, .raw_writefn = raw_write },
200ac0ef 2081 { .name = "PMCNTENCLR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 2,
8521466b
AF
2082 .access = PL0_RW,
2083 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
fcd25206
PM
2084 .accessfn = pmreg_access,
2085 .writefn = pmcntenclr_write,
7a0e58fa 2086 .type = ARM_CP_ALIAS },
8521466b
AF
2087 { .name = "PMCNTENCLR_EL0", .state = ARM_CP_STATE_AA64,
2088 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 2,
2089 .access = PL0_RW, .accessfn = pmreg_access,
7a0e58fa 2090 .type = ARM_CP_ALIAS,
8521466b
AF
2091 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten),
2092 .writefn = pmcntenclr_write },
200ac0ef 2093 { .name = "PMOVSR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 3,
f4efb4b2 2094 .access = PL0_RW, .type = ARM_CP_IO,
e4e91a21 2095 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr),
fcd25206
PM
2096 .accessfn = pmreg_access,
2097 .writefn = pmovsr_write,
2098 .raw_writefn = raw_write },
978364f1
AF
2099 { .name = "PMOVSCLR_EL0", .state = ARM_CP_STATE_AA64,
2100 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 3,
2101 .access = PL0_RW, .accessfn = pmreg_access,
f4efb4b2 2102 .type = ARM_CP_ALIAS | ARM_CP_IO,
978364f1
AF
2103 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
2104 .writefn = pmovsr_write,
2105 .raw_writefn = raw_write },
200ac0ef 2106 { .name = "PMSWINC", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 4,
f4efb4b2
AL
2107 .access = PL0_W, .accessfn = pmreg_access_swinc,
2108 .type = ARM_CP_NO_RAW | ARM_CP_IO,
0d4bfd7d
AL
2109 .writefn = pmswinc_write },
2110 { .name = "PMSWINC_EL0", .state = ARM_CP_STATE_AA64,
2111 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 4,
f4efb4b2
AL
2112 .access = PL0_W, .accessfn = pmreg_access_swinc,
2113 .type = ARM_CP_NO_RAW | ARM_CP_IO,
0d4bfd7d 2114 .writefn = pmswinc_write },
6b040780
WH
2115 { .name = "PMSELR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 5,
2116 .access = PL0_RW, .type = ARM_CP_ALIAS,
2117 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmselr),
6ecd0b6b 2118 .accessfn = pmreg_access_selr, .writefn = pmselr_write,
6b040780
WH
2119 .raw_writefn = raw_write},
2120 { .name = "PMSELR_EL0", .state = ARM_CP_STATE_AA64,
2121 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 5,
6ecd0b6b 2122 .access = PL0_RW, .accessfn = pmreg_access_selr,
6b040780
WH
2123 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmselr),
2124 .writefn = pmselr_write, .raw_writefn = raw_write, },
200ac0ef 2125 { .name = "PMCCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 0,
169c8938 2126 .access = PL0_RW, .resetvalue = 0, .type = ARM_CP_ALIAS | ARM_CP_IO,
421c7ebd 2127 .readfn = pmccntr_read, .writefn = pmccntr_write32,
6ecd0b6b 2128 .accessfn = pmreg_access_ccntr },
8521466b
AF
2129 { .name = "PMCCNTR_EL0", .state = ARM_CP_STATE_AA64,
2130 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 0,
6ecd0b6b 2131 .access = PL0_RW, .accessfn = pmreg_access_ccntr,
8521466b 2132 .type = ARM_CP_IO,
980ebe87
AL
2133 .fieldoffset = offsetof(CPUARMState, cp15.c15_ccnt),
2134 .readfn = pmccntr_read, .writefn = pmccntr_write,
2135 .raw_readfn = raw_read, .raw_writefn = raw_write, },
4b8afa1f
AL
2136 { .name = "PMCCFILTR", .cp = 15, .opc1 = 0, .crn = 14, .crm = 15, .opc2 = 7,
2137 .writefn = pmccfiltr_write_a32, .readfn = pmccfiltr_read_a32,
2138 .access = PL0_RW, .accessfn = pmreg_access,
2139 .type = ARM_CP_ALIAS | ARM_CP_IO,
2140 .resetvalue = 0, },
8521466b
AF
2141 { .name = "PMCCFILTR_EL0", .state = ARM_CP_STATE_AA64,
2142 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 15, .opc2 = 7,
980ebe87 2143 .writefn = pmccfiltr_write, .raw_writefn = raw_write,
8521466b
AF
2144 .access = PL0_RW, .accessfn = pmreg_access,
2145 .type = ARM_CP_IO,
2146 .fieldoffset = offsetof(CPUARMState, cp15.pmccfiltr_el0),
2147 .resetvalue = 0, },
200ac0ef 2148 { .name = "PMXEVTYPER", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 1,
5ecdd3e4
AL
2149 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2150 .accessfn = pmreg_access,
fdb86656
WH
2151 .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
2152 { .name = "PMXEVTYPER_EL0", .state = ARM_CP_STATE_AA64,
2153 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 1,
5ecdd3e4
AL
2154 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2155 .accessfn = pmreg_access,
fdb86656 2156 .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
200ac0ef 2157 { .name = "PMXEVCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 2,
5ecdd3e4
AL
2158 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2159 .accessfn = pmreg_access_xevcntr,
2160 .writefn = pmxevcntr_write, .readfn = pmxevcntr_read },
2161 { .name = "PMXEVCNTR_EL0", .state = ARM_CP_STATE_AA64,
2162 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 2,
2163 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2164 .accessfn = pmreg_access_xevcntr,
2165 .writefn = pmxevcntr_write, .readfn = pmxevcntr_read },
200ac0ef 2166 { .name = "PMUSERENR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 0,
1fce1ba9 2167 .access = PL0_R | PL1_RW, .accessfn = access_tpm,
e4e91a21 2168 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmuserenr),
200ac0ef 2169 .resetvalue = 0,
d4e6df63 2170 .writefn = pmuserenr_write, .raw_writefn = raw_write },
8a83ffc2
AF
2171 { .name = "PMUSERENR_EL0", .state = ARM_CP_STATE_AA64,
2172 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 0,
1fce1ba9 2173 .access = PL0_R | PL1_RW, .accessfn = access_tpm, .type = ARM_CP_ALIAS,
8a83ffc2
AF
2174 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmuserenr),
2175 .resetvalue = 0,
2176 .writefn = pmuserenr_write, .raw_writefn = raw_write },
200ac0ef 2177 { .name = "PMINTENSET", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 1,
1fce1ba9 2178 .access = PL1_RW, .accessfn = access_tpm,
b7d793ad 2179 .type = ARM_CP_ALIAS | ARM_CP_IO,
e6ec5457 2180 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pminten),
200ac0ef 2181 .resetvalue = 0,
d4e6df63 2182 .writefn = pmintenset_write, .raw_writefn = raw_write },
e6ec5457
WH
2183 { .name = "PMINTENSET_EL1", .state = ARM_CP_STATE_AA64,
2184 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 1,
2185 .access = PL1_RW, .accessfn = access_tpm,
2186 .type = ARM_CP_IO,
2187 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2188 .writefn = pmintenset_write, .raw_writefn = raw_write,
2189 .resetvalue = 0x0 },
200ac0ef 2190 { .name = "PMINTENCLR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 2,
fc5f6856
AL
2191 .access = PL1_RW, .accessfn = access_tpm,
2192 .type = ARM_CP_ALIAS | ARM_CP_IO,
200ac0ef 2193 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
b061a82b 2194 .writefn = pmintenclr_write, },
978364f1
AF
2195 { .name = "PMINTENCLR_EL1", .state = ARM_CP_STATE_AA64,
2196 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 2,
fc5f6856
AL
2197 .access = PL1_RW, .accessfn = access_tpm,
2198 .type = ARM_CP_ALIAS | ARM_CP_IO,
978364f1
AF
2199 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2200 .writefn = pmintenclr_write },
7da845b0
PM
2201 { .name = "CCSIDR", .state = ARM_CP_STATE_BOTH,
2202 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 0,
630fcd4d
MZ
2203 .access = PL1_R,
2204 .accessfn = access_aa64_tid2,
2205 .readfn = ccsidr_read, .type = ARM_CP_NO_RAW },
7da845b0
PM
2206 { .name = "CSSELR", .state = ARM_CP_STATE_BOTH,
2207 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 2, .opc2 = 0,
630fcd4d
MZ
2208 .access = PL1_RW,
2209 .accessfn = access_aa64_tid2,
2210 .writefn = csselr_write, .resetvalue = 0,
b85a1fd6
FA
2211 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.csselr_s),
2212 offsetof(CPUARMState, cp15.csselr_ns) } },
776d4e5c
PM
2213 /* Auxiliary ID register: this actually has an IMPDEF value but for now
2214 * just RAZ for all cores:
2215 */
0ff644a7
PM
2216 { .name = "AIDR", .state = ARM_CP_STATE_BOTH,
2217 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 7,
93fbc983
MZ
2218 .access = PL1_R, .type = ARM_CP_CONST,
2219 .accessfn = access_aa64_tid1,
2220 .resetvalue = 0 },
f32cdad5
PM
2221 /* Auxiliary fault status registers: these also are IMPDEF, and we
2222 * choose to RAZ/WI for all cores.
2223 */
2224 { .name = "AFSR0_EL1", .state = ARM_CP_STATE_BOTH,
2225 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 0,
84929218
RH
2226 .access = PL1_RW, .accessfn = access_tvm_trvm,
2227 .type = ARM_CP_CONST, .resetvalue = 0 },
f32cdad5
PM
2228 { .name = "AFSR1_EL1", .state = ARM_CP_STATE_BOTH,
2229 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 1,
84929218
RH
2230 .access = PL1_RW, .accessfn = access_tvm_trvm,
2231 .type = ARM_CP_CONST, .resetvalue = 0 },
b0fe2427
PM
2232 /* MAIR can just read-as-written because we don't implement caches
2233 * and so don't need to care about memory attributes.
2234 */
2235 { .name = "MAIR_EL1", .state = ARM_CP_STATE_AA64,
2236 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
84929218
RH
2237 .access = PL1_RW, .accessfn = access_tvm_trvm,
2238 .fieldoffset = offsetof(CPUARMState, cp15.mair_el[1]),
b0fe2427 2239 .resetvalue = 0 },
4cfb8ad8
PM
2240 { .name = "MAIR_EL3", .state = ARM_CP_STATE_AA64,
2241 .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 2, .opc2 = 0,
2242 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[3]),
2243 .resetvalue = 0 },
b0fe2427
PM
2244 /* For non-long-descriptor page tables these are PRRR and NMRR;
2245 * regardless they still act as reads-as-written for QEMU.
b0fe2427 2246 */
1281f8e3 2247 /* MAIR0/1 are defined separately from their 64-bit counterpart which
be693c87
GB
2248 * allows them to assign the correct fieldoffset based on the endianness
2249 * handled in the field definitions.
2250 */
a903c449 2251 { .name = "MAIR0", .state = ARM_CP_STATE_AA32,
84929218
RH
2252 .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
2253 .access = PL1_RW, .accessfn = access_tvm_trvm,
be693c87
GB
2254 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair0_s),
2255 offsetof(CPUARMState, cp15.mair0_ns) },
b0fe2427 2256 .resetfn = arm_cp_reset_ignore },
a903c449 2257 { .name = "MAIR1", .state = ARM_CP_STATE_AA32,
84929218
RH
2258 .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 1,
2259 .access = PL1_RW, .accessfn = access_tvm_trvm,
be693c87
GB
2260 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair1_s),
2261 offsetof(CPUARMState, cp15.mair1_ns) },
b0fe2427 2262 .resetfn = arm_cp_reset_ignore },
1090b9c6
PM
2263 { .name = "ISR_EL1", .state = ARM_CP_STATE_BOTH,
2264 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 0,
7a0e58fa 2265 .type = ARM_CP_NO_RAW, .access = PL1_R, .readfn = isr_read },
995939a6
PM
2266 /* 32 bit ITLB invalidates */
2267 { .name = "ITLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 0,
7a0e58fa 2268 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_write },
995939a6 2269 { .name = "ITLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 1,
7a0e58fa 2270 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write },
995939a6 2271 { .name = "ITLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 2,
7a0e58fa 2272 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiasid_write },
995939a6
PM
2273 /* 32 bit DTLB invalidates */
2274 { .name = "DTLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 0,
7a0e58fa 2275 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_write },
995939a6 2276 { .name = "DTLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 1,
7a0e58fa 2277 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write },
995939a6 2278 { .name = "DTLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 2,
7a0e58fa 2279 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiasid_write },
995939a6
PM
2280 /* 32 bit TLB invalidates */
2281 { .name = "TLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
7a0e58fa 2282 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_write },
995939a6 2283 { .name = "TLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
7a0e58fa 2284 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write },
995939a6 2285 { .name = "TLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
7a0e58fa 2286 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiasid_write },
995939a6 2287 { .name = "TLBIMVAA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
7a0e58fa 2288 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimvaa_write },
995939a6
PM
2289 REGINFO_SENTINEL
2290};
2291
2292static const ARMCPRegInfo v7mp_cp_reginfo[] = {
2293 /* 32 bit TLB invalidates, Inner Shareable */
2294 { .name = "TLBIALLIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
7a0e58fa 2295 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_is_write },
995939a6 2296 { .name = "TLBIMVAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
7a0e58fa 2297 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_is_write },
995939a6 2298 { .name = "TLBIASIDIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
7a0e58fa 2299 .type = ARM_CP_NO_RAW, .access = PL1_W,
fa439fc5 2300 .writefn = tlbiasid_is_write },
995939a6 2301 { .name = "TLBIMVAAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
7a0e58fa 2302 .type = ARM_CP_NO_RAW, .access = PL1_W,
fa439fc5 2303 .writefn = tlbimvaa_is_write },
e9aa6c21
PM
2304 REGINFO_SENTINEL
2305};
2306
327dd510
AL
2307static const ARMCPRegInfo pmovsset_cp_reginfo[] = {
2308 /* PMOVSSET is not implemented in v7 before v7ve */
2309 { .name = "PMOVSSET", .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 3,
2310 .access = PL0_RW, .accessfn = pmreg_access,
f4efb4b2 2311 .type = ARM_CP_ALIAS | ARM_CP_IO,
327dd510
AL
2312 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr),
2313 .writefn = pmovsset_write,
2314 .raw_writefn = raw_write },
2315 { .name = "PMOVSSET_EL0", .state = ARM_CP_STATE_AA64,
2316 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 3,
2317 .access = PL0_RW, .accessfn = pmreg_access,
f4efb4b2 2318 .type = ARM_CP_ALIAS | ARM_CP_IO,
327dd510
AL
2319 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
2320 .writefn = pmovsset_write,
2321 .raw_writefn = raw_write },
2322 REGINFO_SENTINEL
2323};
2324
c4241c7d
PM
2325static void teecr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2326 uint64_t value)
c326b979
PM
2327{
2328 value &= 1;
2329 env->teecr = value;
c326b979
PM
2330}
2331
3f208fd7
PM
2332static CPAccessResult teehbr_access(CPUARMState *env, const ARMCPRegInfo *ri,
2333 bool isread)
c326b979 2334{
dcbff19b 2335 if (arm_current_el(env) == 0 && (env->teecr & 1)) {
92611c00 2336 return CP_ACCESS_TRAP;
c326b979 2337 }
92611c00 2338 return CP_ACCESS_OK;
c326b979
PM
2339}
2340
2341static const ARMCPRegInfo t2ee_cp_reginfo[] = {
2342 { .name = "TEECR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 6, .opc2 = 0,
2343 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, teecr),
2344 .resetvalue = 0,
2345 .writefn = teecr_write },
2346 { .name = "TEEHBR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 6, .opc2 = 0,
2347 .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, teehbr),
92611c00 2348 .accessfn = teehbr_access, .resetvalue = 0 },
c326b979
PM
2349 REGINFO_SENTINEL
2350};
2351
4d31c596 2352static const ARMCPRegInfo v6k_cp_reginfo[] = {
e4fe830b
PM
2353 { .name = "TPIDR_EL0", .state = ARM_CP_STATE_AA64,
2354 .opc0 = 3, .opc1 = 3, .opc2 = 2, .crn = 13, .crm = 0,
2355 .access = PL0_RW,
54bf36ed 2356 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[0]), .resetvalue = 0 },
4d31c596
PM
2357 { .name = "TPIDRURW", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 2,
2358 .access = PL0_RW,
54bf36ed
FA
2359 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrurw_s),
2360 offsetoflow32(CPUARMState, cp15.tpidrurw_ns) },
e4fe830b
PM
2361 .resetfn = arm_cp_reset_ignore },
2362 { .name = "TPIDRRO_EL0", .state = ARM_CP_STATE_AA64,
2363 .opc0 = 3, .opc1 = 3, .opc2 = 3, .crn = 13, .crm = 0,
2364 .access = PL0_R|PL1_W,
54bf36ed
FA
2365 .fieldoffset = offsetof(CPUARMState, cp15.tpidrro_el[0]),
2366 .resetvalue = 0},
4d31c596
PM
2367 { .name = "TPIDRURO", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 3,
2368 .access = PL0_R|PL1_W,
54bf36ed
FA
2369 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidruro_s),
2370 offsetoflow32(CPUARMState, cp15.tpidruro_ns) },
e4fe830b 2371 .resetfn = arm_cp_reset_ignore },
54bf36ed 2372 { .name = "TPIDR_EL1", .state = ARM_CP_STATE_AA64,
e4fe830b 2373 .opc0 = 3, .opc1 = 0, .opc2 = 4, .crn = 13, .crm = 0,
4d31c596 2374 .access = PL1_RW,
54bf36ed
FA
2375 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[1]), .resetvalue = 0 },
2376 { .name = "TPIDRPRW", .opc1 = 0, .cp = 15, .crn = 13, .crm = 0, .opc2 = 4,
2377 .access = PL1_RW,
2378 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrprw_s),
2379 offsetoflow32(CPUARMState, cp15.tpidrprw_ns) },
2380 .resetvalue = 0 },
4d31c596
PM
2381 REGINFO_SENTINEL
2382};
2383
55d284af
PM
2384#ifndef CONFIG_USER_ONLY
2385
3f208fd7
PM
2386static CPAccessResult gt_cntfrq_access(CPUARMState *env, const ARMCPRegInfo *ri,
2387 bool isread)
00108f2d 2388{
75502672
PM
2389 /* CNTFRQ: not visible from PL0 if both PL0PCTEN and PL0VCTEN are zero.
2390 * Writable only at the highest implemented exception level.
2391 */
2392 int el = arm_current_el(env);
5bc84371
RH
2393 uint64_t hcr;
2394 uint32_t cntkctl;
75502672
PM
2395
2396 switch (el) {
2397 case 0:
5bc84371
RH
2398 hcr = arm_hcr_el2_eff(env);
2399 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2400 cntkctl = env->cp15.cnthctl_el2;
2401 } else {
2402 cntkctl = env->cp15.c14_cntkctl;
2403 }
2404 if (!extract32(cntkctl, 0, 2)) {
75502672
PM
2405 return CP_ACCESS_TRAP;
2406 }
2407 break;
2408 case 1:
2409 if (!isread && ri->state == ARM_CP_STATE_AA32 &&
2410 arm_is_secure_below_el3(env)) {
2411 /* Accesses from 32-bit Secure EL1 UNDEF (*not* trap to EL3!) */
2412 return CP_ACCESS_TRAP_UNCATEGORIZED;
2413 }
2414 break;
2415 case 2:
2416 case 3:
2417 break;
00108f2d 2418 }
75502672
PM
2419
2420 if (!isread && el < arm_highest_el(env)) {
2421 return CP_ACCESS_TRAP_UNCATEGORIZED;
2422 }
2423
00108f2d
PM
2424 return CP_ACCESS_OK;
2425}
2426
3f208fd7
PM
2427static CPAccessResult gt_counter_access(CPUARMState *env, int timeridx,
2428 bool isread)
00108f2d 2429{
0b6440af
EI
2430 unsigned int cur_el = arm_current_el(env);
2431 bool secure = arm_is_secure(env);
5bc84371 2432 uint64_t hcr = arm_hcr_el2_eff(env);
0b6440af 2433
5bc84371
RH
2434 switch (cur_el) {
2435 case 0:
2436 /* If HCR_EL2.<E2H,TGE> == '11': check CNTHCTL_EL2.EL0[PV]CTEN. */
2437 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2438 return (extract32(env->cp15.cnthctl_el2, timeridx, 1)
2439 ? CP_ACCESS_OK : CP_ACCESS_TRAP_EL2);
2440 }
0b6440af 2441
5bc84371
RH
2442 /* CNT[PV]CT: not visible from PL0 if EL0[PV]CTEN is zero */
2443 if (!extract32(env->cp15.c14_cntkctl, timeridx, 1)) {
2444 return CP_ACCESS_TRAP;
2445 }
2446
2447 /* If HCR_EL2.<E2H,TGE> == '10': check CNTHCTL_EL2.EL1PCTEN. */
2448 if (hcr & HCR_E2H) {
2449 if (timeridx == GTIMER_PHYS &&
2450 !extract32(env->cp15.cnthctl_el2, 10, 1)) {
2451 return CP_ACCESS_TRAP_EL2;
2452 }
2453 } else {
2454 /* If HCR_EL2.<E2H> == 0: check CNTHCTL_EL2.EL1PCEN. */
2455 if (arm_feature(env, ARM_FEATURE_EL2) &&
2456 timeridx == GTIMER_PHYS && !secure &&
2457 !extract32(env->cp15.cnthctl_el2, 1, 1)) {
2458 return CP_ACCESS_TRAP_EL2;
2459 }
2460 }
2461 break;
2462
2463 case 1:
2464 /* Check CNTHCTL_EL2.EL1PCTEN, which changes location based on E2H. */
2465 if (arm_feature(env, ARM_FEATURE_EL2) &&
2466 timeridx == GTIMER_PHYS && !secure &&
2467 (hcr & HCR_E2H
2468 ? !extract32(env->cp15.cnthctl_el2, 10, 1)
2469 : !extract32(env->cp15.cnthctl_el2, 0, 1))) {
2470 return CP_ACCESS_TRAP_EL2;
2471 }
2472 break;
0b6440af 2473 }
00108f2d
PM
2474 return CP_ACCESS_OK;
2475}
2476
3f208fd7
PM
2477static CPAccessResult gt_timer_access(CPUARMState *env, int timeridx,
2478 bool isread)
00108f2d 2479{
0b6440af
EI
2480 unsigned int cur_el = arm_current_el(env);
2481 bool secure = arm_is_secure(env);
5bc84371 2482 uint64_t hcr = arm_hcr_el2_eff(env);
0b6440af 2483
5bc84371
RH
2484 switch (cur_el) {
2485 case 0:
2486 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2487 /* If HCR_EL2.<E2H,TGE> == '11': check CNTHCTL_EL2.EL0[PV]TEN. */
2488 return (extract32(env->cp15.cnthctl_el2, 9 - timeridx, 1)
2489 ? CP_ACCESS_OK : CP_ACCESS_TRAP_EL2);
2490 }
0b6440af 2491
5bc84371
RH
2492 /*
2493 * CNT[PV]_CVAL, CNT[PV]_CTL, CNT[PV]_TVAL: not visible from
2494 * EL0 if EL0[PV]TEN is zero.
2495 */
2496 if (!extract32(env->cp15.c14_cntkctl, 9 - timeridx, 1)) {
2497 return CP_ACCESS_TRAP;
2498 }
2499 /* fall through */
2500
2501 case 1:
2502 if (arm_feature(env, ARM_FEATURE_EL2) &&
2503 timeridx == GTIMER_PHYS && !secure) {
2504 if (hcr & HCR_E2H) {
2505 /* If HCR_EL2.<E2H,TGE> == '10': check CNTHCTL_EL2.EL1PTEN. */
2506 if (!extract32(env->cp15.cnthctl_el2, 11, 1)) {
2507 return CP_ACCESS_TRAP_EL2;
2508 }
2509 } else {
2510 /* If HCR_EL2.<E2H> == 0: check CNTHCTL_EL2.EL1PCEN. */
2511 if (!extract32(env->cp15.cnthctl_el2, 1, 1)) {
2512 return CP_ACCESS_TRAP_EL2;
2513 }
2514 }
2515 }
2516 break;
0b6440af 2517 }
00108f2d
PM
2518 return CP_ACCESS_OK;
2519}
2520
2521static CPAccessResult gt_pct_access(CPUARMState *env,
3f208fd7
PM
2522 const ARMCPRegInfo *ri,
2523 bool isread)
00108f2d 2524{
3f208fd7 2525 return gt_counter_access(env, GTIMER_PHYS, isread);
00108f2d
PM
2526}
2527
2528static CPAccessResult gt_vct_access(CPUARMState *env,
3f208fd7
PM
2529 const ARMCPRegInfo *ri,
2530 bool isread)
00108f2d 2531{
3f208fd7 2532 return gt_counter_access(env, GTIMER_VIRT, isread);
00108f2d
PM
2533}
2534
3f208fd7
PM
2535static CPAccessResult gt_ptimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
2536 bool isread)
00108f2d 2537{
3f208fd7 2538 return gt_timer_access(env, GTIMER_PHYS, isread);
00108f2d
PM
2539}
2540
3f208fd7
PM
2541static CPAccessResult gt_vtimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
2542 bool isread)
00108f2d 2543{
3f208fd7 2544 return gt_timer_access(env, GTIMER_VIRT, isread);
00108f2d
PM
2545}
2546
b4d3978c 2547static CPAccessResult gt_stimer_access(CPUARMState *env,
3f208fd7
PM
2548 const ARMCPRegInfo *ri,
2549 bool isread)
b4d3978c
PM
2550{
2551 /* The AArch64 register view of the secure physical timer is
2552 * always accessible from EL3, and configurably accessible from
2553 * Secure EL1.
2554 */
2555 switch (arm_current_el(env)) {
2556 case 1:
2557 if (!arm_is_secure(env)) {
2558 return CP_ACCESS_TRAP;
2559 }
2560 if (!(env->cp15.scr_el3 & SCR_ST)) {
2561 return CP_ACCESS_TRAP_EL3;
2562 }
2563 return CP_ACCESS_OK;
2564 case 0:
2565 case 2:
2566 return CP_ACCESS_TRAP;
2567 case 3:
2568 return CP_ACCESS_OK;
2569 default:
2570 g_assert_not_reached();
2571 }
2572}
2573
55d284af
PM
2574static uint64_t gt_get_countervalue(CPUARMState *env)
2575{
7def8754
AJ
2576 ARMCPU *cpu = env_archcpu(env);
2577
2578 return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) / gt_cntfrq_period_ns(cpu);
55d284af
PM
2579}
2580
2581static void gt_recalc_timer(ARMCPU *cpu, int timeridx)
2582{
2583 ARMGenericTimer *gt = &cpu->env.cp15.c14_timer[timeridx];
2584
2585 if (gt->ctl & 1) {
2586 /* Timer enabled: calculate and set current ISTATUS, irq, and
2587 * reset timer to when ISTATUS next has to change
2588 */
edac4d8a
EI
2589 uint64_t offset = timeridx == GTIMER_VIRT ?
2590 cpu->env.cp15.cntvoff_el2 : 0;
55d284af
PM
2591 uint64_t count = gt_get_countervalue(&cpu->env);
2592 /* Note that this must be unsigned 64 bit arithmetic: */
edac4d8a 2593 int istatus = count - offset >= gt->cval;
55d284af 2594 uint64_t nexttick;
194cbc49 2595 int irqstate;
55d284af
PM
2596
2597 gt->ctl = deposit32(gt->ctl, 2, 1, istatus);
194cbc49
PM
2598
2599 irqstate = (istatus && !(gt->ctl & 2));
2600 qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate);
2601
55d284af
PM
2602 if (istatus) {
2603 /* Next transition is when count rolls back over to zero */
2604 nexttick = UINT64_MAX;
2605 } else {
2606 /* Next transition is when we hit cval */
edac4d8a 2607 nexttick = gt->cval + offset;
55d284af
PM
2608 }
2609 /* Note that the desired next expiry time might be beyond the
2610 * signed-64-bit range of a QEMUTimer -- in this case we just
2611 * set the timer for as far in the future as possible. When the
2612 * timer expires we will reset the timer for any remaining period.
2613 */
7def8754 2614 if (nexttick > INT64_MAX / gt_cntfrq_period_ns(cpu)) {
4a0245b6
AJ
2615 timer_mod_ns(cpu->gt_timer[timeridx], INT64_MAX);
2616 } else {
2617 timer_mod(cpu->gt_timer[timeridx], nexttick);
55d284af 2618 }
194cbc49 2619 trace_arm_gt_recalc(timeridx, irqstate, nexttick);
55d284af
PM
2620 } else {
2621 /* Timer disabled: ISTATUS and timer output always clear */
2622 gt->ctl &= ~4;
2623 qemu_set_irq(cpu->gt_timer_outputs[timeridx], 0);
bc72ad67 2624 timer_del(cpu->gt_timer[timeridx]);
194cbc49 2625 trace_arm_gt_recalc_disabled(timeridx);
55d284af
PM
2626 }
2627}
2628
0e3eca4c
EI
2629static void gt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri,
2630 int timeridx)
55d284af 2631{
2fc0cc0e 2632 ARMCPU *cpu = env_archcpu(env);
55d284af 2633
bc72ad67 2634 timer_del(cpu->gt_timer[timeridx]);
55d284af
PM
2635}
2636
c4241c7d 2637static uint64_t gt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
55d284af 2638{
c4241c7d 2639 return gt_get_countervalue(env);
55d284af
PM
2640}
2641
53d1f856
RH
2642static uint64_t gt_virt_cnt_offset(CPUARMState *env)
2643{
2644 uint64_t hcr;
2645
2646 switch (arm_current_el(env)) {
2647 case 2:
2648 hcr = arm_hcr_el2_eff(env);
2649 if (hcr & HCR_E2H) {
2650 return 0;
2651 }
2652 break;
2653 case 0:
2654 hcr = arm_hcr_el2_eff(env);
2655 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2656 return 0;
2657 }
2658 break;
2659 }
2660
2661 return env->cp15.cntvoff_el2;
2662}
2663
edac4d8a
EI
2664static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
2665{
53d1f856 2666 return gt_get_countervalue(env) - gt_virt_cnt_offset(env);
edac4d8a
EI
2667}
2668
c4241c7d 2669static void gt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
0e3eca4c 2670 int timeridx,
c4241c7d 2671 uint64_t value)
55d284af 2672{
194cbc49 2673 trace_arm_gt_cval_write(timeridx, value);
55d284af 2674 env->cp15.c14_timer[timeridx].cval = value;
2fc0cc0e 2675 gt_recalc_timer(env_archcpu(env), timeridx);
55d284af 2676}
c4241c7d 2677
0e3eca4c
EI
2678static uint64_t gt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri,
2679 int timeridx)
55d284af 2680{
53d1f856
RH
2681 uint64_t offset = 0;
2682
2683 switch (timeridx) {
2684 case GTIMER_VIRT:
8c94b071 2685 case GTIMER_HYPVIRT:
53d1f856
RH
2686 offset = gt_virt_cnt_offset(env);
2687 break;
2688 }
55d284af 2689
c4241c7d 2690 return (uint32_t)(env->cp15.c14_timer[timeridx].cval -
edac4d8a 2691 (gt_get_countervalue(env) - offset));
55d284af
PM
2692}
2693
c4241c7d 2694static void gt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
0e3eca4c 2695 int timeridx,
c4241c7d 2696 uint64_t value)
55d284af 2697{
53d1f856
RH
2698 uint64_t offset = 0;
2699
2700 switch (timeridx) {
2701 case GTIMER_VIRT:
8c94b071 2702 case GTIMER_HYPVIRT:
53d1f856
RH
2703 offset = gt_virt_cnt_offset(env);
2704 break;
2705 }
55d284af 2706
194cbc49 2707 trace_arm_gt_tval_write(timeridx, value);
edac4d8a 2708 env->cp15.c14_timer[timeridx].cval = gt_get_countervalue(env) - offset +
18084b2f 2709 sextract64(value, 0, 32);
2fc0cc0e 2710 gt_recalc_timer(env_archcpu(env), timeridx);
55d284af
PM
2711}
2712
c4241c7d 2713static void gt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
0e3eca4c 2714 int timeridx,
c4241c7d 2715 uint64_t value)
55d284af 2716{
2fc0cc0e 2717 ARMCPU *cpu = env_archcpu(env);
55d284af
PM
2718 uint32_t oldval = env->cp15.c14_timer[timeridx].ctl;
2719
194cbc49 2720 trace_arm_gt_ctl_write(timeridx, value);
d3afacc7 2721 env->cp15.c14_timer[timeridx].ctl = deposit64(oldval, 0, 2, value);
55d284af
PM
2722 if ((oldval ^ value) & 1) {
2723 /* Enable toggled */
2724 gt_recalc_timer(cpu, timeridx);
d3afacc7 2725 } else if ((oldval ^ value) & 2) {
55d284af
PM
2726 /* IMASK toggled: don't need to recalculate,
2727 * just set the interrupt line based on ISTATUS
2728 */
194cbc49
PM
2729 int irqstate = (oldval & 4) && !(value & 2);
2730
2731 trace_arm_gt_imask_toggle(timeridx, irqstate);
2732 qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate);
55d284af 2733 }
55d284af
PM
2734}
2735
0e3eca4c
EI
2736static void gt_phys_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2737{
2738 gt_timer_reset(env, ri, GTIMER_PHYS);
2739}
2740
2741static void gt_phys_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2742 uint64_t value)
2743{
2744 gt_cval_write(env, ri, GTIMER_PHYS, value);
2745}
2746
2747static uint64_t gt_phys_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2748{
2749 return gt_tval_read(env, ri, GTIMER_PHYS);
2750}
2751
2752static void gt_phys_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2753 uint64_t value)
2754{
2755 gt_tval_write(env, ri, GTIMER_PHYS, value);
2756}
2757
2758static void gt_phys_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2759 uint64_t value)
2760{
2761 gt_ctl_write(env, ri, GTIMER_PHYS, value);
2762}
2763
bb5972e4
RH
2764static int gt_phys_redir_timeridx(CPUARMState *env)
2765{
2766 switch (arm_mmu_idx(env)) {
2767 case ARMMMUIdx_E20_0:
2768 case ARMMMUIdx_E20_2:
452ef8cb 2769 case ARMMMUIdx_E20_2_PAN:
bb5972e4
RH
2770 return GTIMER_HYP;
2771 default:
2772 return GTIMER_PHYS;
2773 }
2774}
2775
2776static int gt_virt_redir_timeridx(CPUARMState *env)
2777{
2778 switch (arm_mmu_idx(env)) {
2779 case ARMMMUIdx_E20_0:
2780 case ARMMMUIdx_E20_2:
452ef8cb 2781 case ARMMMUIdx_E20_2_PAN:
bb5972e4
RH
2782 return GTIMER_HYPVIRT;
2783 default:
2784 return GTIMER_VIRT;
2785 }
2786}
2787
2788static uint64_t gt_phys_redir_cval_read(CPUARMState *env,
2789 const ARMCPRegInfo *ri)
2790{
2791 int timeridx = gt_phys_redir_timeridx(env);
2792 return env->cp15.c14_timer[timeridx].cval;
2793}
2794
2795static void gt_phys_redir_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2796 uint64_t value)
2797{
2798 int timeridx = gt_phys_redir_timeridx(env);
2799 gt_cval_write(env, ri, timeridx, value);
2800}
2801
2802static uint64_t gt_phys_redir_tval_read(CPUARMState *env,
2803 const ARMCPRegInfo *ri)
2804{
2805 int timeridx = gt_phys_redir_timeridx(env);
2806 return gt_tval_read(env, ri, timeridx);
2807}
2808
2809static void gt_phys_redir_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2810 uint64_t value)
2811{
2812 int timeridx = gt_phys_redir_timeridx(env);
2813 gt_tval_write(env, ri, timeridx, value);
2814}
2815
2816static uint64_t gt_phys_redir_ctl_read(CPUARMState *env,
2817 const ARMCPRegInfo *ri)
2818{
2819 int timeridx = gt_phys_redir_timeridx(env);
2820 return env->cp15.c14_timer[timeridx].ctl;
2821}
2822
2823static void gt_phys_redir_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2824 uint64_t value)
2825{
2826 int timeridx = gt_phys_redir_timeridx(env);
2827 gt_ctl_write(env, ri, timeridx, value);
2828}
2829
0e3eca4c
EI
2830static void gt_virt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2831{
2832 gt_timer_reset(env, ri, GTIMER_VIRT);
2833}
2834
2835static void gt_virt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2836 uint64_t value)
2837{
2838 gt_cval_write(env, ri, GTIMER_VIRT, value);
2839}
2840
2841static uint64_t gt_virt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2842{
2843 return gt_tval_read(env, ri, GTIMER_VIRT);
2844}
2845
2846static void gt_virt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2847 uint64_t value)
2848{
2849 gt_tval_write(env, ri, GTIMER_VIRT, value);
2850}
2851
2852static void gt_virt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2853 uint64_t value)
2854{
2855 gt_ctl_write(env, ri, GTIMER_VIRT, value);
2856}
2857
edac4d8a
EI
2858static void gt_cntvoff_write(CPUARMState *env, const ARMCPRegInfo *ri,
2859 uint64_t value)
2860{
2fc0cc0e 2861 ARMCPU *cpu = env_archcpu(env);
edac4d8a 2862
194cbc49 2863 trace_arm_gt_cntvoff_write(value);
edac4d8a
EI
2864 raw_write(env, ri, value);
2865 gt_recalc_timer(cpu, GTIMER_VIRT);
2866}
2867
bb5972e4
RH
2868static uint64_t gt_virt_redir_cval_read(CPUARMState *env,
2869 const ARMCPRegInfo *ri)
2870{
2871 int timeridx = gt_virt_redir_timeridx(env);
2872 return env->cp15.c14_timer[timeridx].cval;
2873}
2874
2875static void gt_virt_redir_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2876 uint64_t value)
2877{
2878 int timeridx = gt_virt_redir_timeridx(env);
2879 gt_cval_write(env, ri, timeridx, value);
2880}
2881
2882static uint64_t gt_virt_redir_tval_read(CPUARMState *env,
2883 const ARMCPRegInfo *ri)
2884{
2885 int timeridx = gt_virt_redir_timeridx(env);
2886 return gt_tval_read(env, ri, timeridx);
2887}
2888
2889static void gt_virt_redir_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2890 uint64_t value)
2891{
2892 int timeridx = gt_virt_redir_timeridx(env);
2893 gt_tval_write(env, ri, timeridx, value);
2894}
2895
2896static uint64_t gt_virt_redir_ctl_read(CPUARMState *env,
2897 const ARMCPRegInfo *ri)
2898{
2899 int timeridx = gt_virt_redir_timeridx(env);
2900 return env->cp15.c14_timer[timeridx].ctl;
2901}
2902
2903static void gt_virt_redir_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2904 uint64_t value)
2905{
2906 int timeridx = gt_virt_redir_timeridx(env);
2907 gt_ctl_write(env, ri, timeridx, value);
2908}
2909
b0e66d95
EI
2910static void gt_hyp_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2911{
2912 gt_timer_reset(env, ri, GTIMER_HYP);
2913}
2914
2915static void gt_hyp_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2916 uint64_t value)
2917{
2918 gt_cval_write(env, ri, GTIMER_HYP, value);
2919}
2920
2921static uint64_t gt_hyp_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2922{
2923 return gt_tval_read(env, ri, GTIMER_HYP);
2924}
2925
2926static void gt_hyp_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2927 uint64_t value)
2928{
2929 gt_tval_write(env, ri, GTIMER_HYP, value);
2930}
2931
2932static void gt_hyp_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2933 uint64_t value)
2934{
2935 gt_ctl_write(env, ri, GTIMER_HYP, value);
2936}
2937
b4d3978c
PM
2938static void gt_sec_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2939{
2940 gt_timer_reset(env, ri, GTIMER_SEC);
2941}
2942
2943static void gt_sec_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2944 uint64_t value)
2945{
2946 gt_cval_write(env, ri, GTIMER_SEC, value);
2947}
2948
2949static uint64_t gt_sec_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2950{
2951 return gt_tval_read(env, ri, GTIMER_SEC);
2952}
2953
2954static void gt_sec_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2955 uint64_t value)
2956{
2957 gt_tval_write(env, ri, GTIMER_SEC, value);
2958}
2959
2960static void gt_sec_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2961 uint64_t value)
2962{
2963 gt_ctl_write(env, ri, GTIMER_SEC, value);
2964}
2965
8c94b071
RH
2966static void gt_hv_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2967{
2968 gt_timer_reset(env, ri, GTIMER_HYPVIRT);
2969}
2970
2971static void gt_hv_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2972 uint64_t value)
2973{
2974 gt_cval_write(env, ri, GTIMER_HYPVIRT, value);
2975}
2976
2977static uint64_t gt_hv_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2978{
2979 return gt_tval_read(env, ri, GTIMER_HYPVIRT);
2980}
2981
2982static void gt_hv_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2983 uint64_t value)
2984{
2985 gt_tval_write(env, ri, GTIMER_HYPVIRT, value);
2986}
2987
2988static void gt_hv_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2989 uint64_t value)
2990{
2991 gt_ctl_write(env, ri, GTIMER_HYPVIRT, value);
2992}
2993
55d284af
PM
2994void arm_gt_ptimer_cb(void *opaque)
2995{
2996 ARMCPU *cpu = opaque;
2997
2998 gt_recalc_timer(cpu, GTIMER_PHYS);
2999}
3000
3001void arm_gt_vtimer_cb(void *opaque)
3002{
3003 ARMCPU *cpu = opaque;
3004
3005 gt_recalc_timer(cpu, GTIMER_VIRT);
3006}
3007
b0e66d95
EI
3008void arm_gt_htimer_cb(void *opaque)
3009{
3010 ARMCPU *cpu = opaque;
3011
3012 gt_recalc_timer(cpu, GTIMER_HYP);
3013}
3014
b4d3978c
PM
3015void arm_gt_stimer_cb(void *opaque)
3016{
3017 ARMCPU *cpu = opaque;
3018
3019 gt_recalc_timer(cpu, GTIMER_SEC);
3020}
3021
8c94b071
RH
3022void arm_gt_hvtimer_cb(void *opaque)
3023{
3024 ARMCPU *cpu = opaque;
3025
3026 gt_recalc_timer(cpu, GTIMER_HYPVIRT);
3027}
3028
96eec6b2
AJ
3029static void arm_gt_cntfrq_reset(CPUARMState *env, const ARMCPRegInfo *opaque)
3030{
3031 ARMCPU *cpu = env_archcpu(env);
3032
3033 cpu->env.cp15.c14_cntfrq = cpu->gt_cntfrq_hz;
3034}
3035
55d284af
PM
3036static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
3037 /* Note that CNTFRQ is purely reads-as-written for the benefit
3038 * of software; writing it doesn't actually change the timer frequency.
3039 * Our reset value matches the fixed frequency we implement the timer at.
3040 */
3041 { .name = "CNTFRQ", .cp = 15, .crn = 14, .crm = 0, .opc1 = 0, .opc2 = 0,
7a0e58fa 3042 .type = ARM_CP_ALIAS,
a7adc4b7
PM
3043 .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
3044 .fieldoffset = offsetoflow32(CPUARMState, cp15.c14_cntfrq),
a7adc4b7
PM
3045 },
3046 { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
3047 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
3048 .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
55d284af 3049 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
96eec6b2 3050 .resetfn = arm_gt_cntfrq_reset,
55d284af
PM
3051 },
3052 /* overall control: mostly access permissions */
a7adc4b7
PM
3053 { .name = "CNTKCTL", .state = ARM_CP_STATE_BOTH,
3054 .opc0 = 3, .opc1 = 0, .crn = 14, .crm = 1, .opc2 = 0,
55d284af
PM
3055 .access = PL1_RW,
3056 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntkctl),
3057 .resetvalue = 0,
3058 },
3059 /* per-timer control */
3060 { .name = "CNTP_CTL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
9ff9dd3c 3061 .secure = ARM_CP_SECSTATE_NS,
daf1dc5f 3062 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
a7adc4b7
PM
3063 .accessfn = gt_ptimer_access,
3064 .fieldoffset = offsetoflow32(CPUARMState,
3065 cp15.c14_timer[GTIMER_PHYS].ctl),
bb5972e4
RH
3066 .readfn = gt_phys_redir_ctl_read, .raw_readfn = raw_read,
3067 .writefn = gt_phys_redir_ctl_write, .raw_writefn = raw_write,
a7adc4b7 3068 },
9c513e78 3069 { .name = "CNTP_CTL_S",
9ff9dd3c
PM
3070 .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
3071 .secure = ARM_CP_SECSTATE_S,
daf1dc5f 3072 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
9ff9dd3c
PM
3073 .accessfn = gt_ptimer_access,
3074 .fieldoffset = offsetoflow32(CPUARMState,
3075 cp15.c14_timer[GTIMER_SEC].ctl),
3076 .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
3077 },
a7adc4b7
PM
3078 { .name = "CNTP_CTL_EL0", .state = ARM_CP_STATE_AA64,
3079 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 1,
daf1dc5f 3080 .type = ARM_CP_IO, .access = PL0_RW,
a7adc4b7 3081 .accessfn = gt_ptimer_access,
55d284af
PM
3082 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
3083 .resetvalue = 0,
bb5972e4
RH
3084 .readfn = gt_phys_redir_ctl_read, .raw_readfn = raw_read,
3085 .writefn = gt_phys_redir_ctl_write, .raw_writefn = raw_write,
55d284af
PM
3086 },
3087 { .name = "CNTV_CTL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 1,
daf1dc5f 3088 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
a7adc4b7
PM
3089 .accessfn = gt_vtimer_access,
3090 .fieldoffset = offsetoflow32(CPUARMState,
3091 cp15.c14_timer[GTIMER_VIRT].ctl),
bb5972e4
RH
3092 .readfn = gt_virt_redir_ctl_read, .raw_readfn = raw_read,
3093 .writefn = gt_virt_redir_ctl_write, .raw_writefn = raw_write,
a7adc4b7
PM
3094 },
3095 { .name = "CNTV_CTL_EL0", .state = ARM_CP_STATE_AA64,
3096 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 1,
daf1dc5f 3097 .type = ARM_CP_IO, .access = PL0_RW,
a7adc4b7 3098 .accessfn = gt_vtimer_access,
55d284af
PM
3099 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
3100 .resetvalue = 0,
bb5972e4
RH
3101 .readfn = gt_virt_redir_ctl_read, .raw_readfn = raw_read,
3102 .writefn = gt_virt_redir_ctl_write, .raw_writefn = raw_write,
55d284af
PM
3103 },
3104 /* TimerValue views: a 32 bit downcounting view of the underlying state */
3105 { .name = "CNTP_TVAL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
9ff9dd3c 3106 .secure = ARM_CP_SECSTATE_NS,
daf1dc5f 3107 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
00108f2d 3108 .accessfn = gt_ptimer_access,
bb5972e4 3109 .readfn = gt_phys_redir_tval_read, .writefn = gt_phys_redir_tval_write,
55d284af 3110 },
9c513e78 3111 { .name = "CNTP_TVAL_S",
9ff9dd3c
PM
3112 .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
3113 .secure = ARM_CP_SECSTATE_S,
daf1dc5f 3114 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
9ff9dd3c
PM
3115 .accessfn = gt_ptimer_access,
3116 .readfn = gt_sec_tval_read, .writefn = gt_sec_tval_write,
3117 },
a7adc4b7
PM
3118 { .name = "CNTP_TVAL_EL0", .state = ARM_CP_STATE_AA64,
3119 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 0,
daf1dc5f 3120 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
0e3eca4c 3121 .accessfn = gt_ptimer_access, .resetfn = gt_phys_timer_reset,
bb5972e4 3122 .readfn = gt_phys_redir_tval_read, .writefn = gt_phys_redir_tval_write,
a7adc4b7 3123 },
55d284af 3124 { .name = "CNTV_TVAL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 0,
daf1dc5f 3125 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
00108f2d 3126 .accessfn = gt_vtimer_access,
bb5972e4 3127 .readfn = gt_virt_redir_tval_read, .writefn = gt_virt_redir_tval_write,
55d284af 3128 },
a7adc4b7
PM
3129 { .name = "CNTV_TVAL_EL0", .state = ARM_CP_STATE_AA64,
3130 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 0,
daf1dc5f 3131 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
0e3eca4c 3132 .accessfn = gt_vtimer_access, .resetfn = gt_virt_timer_reset,
bb5972e4 3133 .readfn = gt_virt_redir_tval_read, .writefn = gt_virt_redir_tval_write,
a7adc4b7 3134 },
55d284af
PM
3135 /* The counter itself */
3136 { .name = "CNTPCT", .cp = 15, .crm = 14, .opc1 = 0,
7a0e58fa 3137 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
00108f2d 3138 .accessfn = gt_pct_access,
a7adc4b7
PM
3139 .readfn = gt_cnt_read, .resetfn = arm_cp_reset_ignore,
3140 },
3141 { .name = "CNTPCT_EL0", .state = ARM_CP_STATE_AA64,
3142 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 1,
7a0e58fa 3143 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
d57b9ee8 3144 .accessfn = gt_pct_access, .readfn = gt_cnt_read,
55d284af
PM
3145 },
3146 { .name = "CNTVCT", .cp = 15, .crm = 14, .opc1 = 1,
7a0e58fa 3147 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
00108f2d 3148 .accessfn = gt_vct_access,
edac4d8a 3149 .readfn = gt_virt_cnt_read, .resetfn = arm_cp_reset_ignore,
a7adc4b7
PM
3150 },
3151 { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
3152 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
7a0e58fa 3153 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
d57b9ee8 3154 .accessfn = gt_vct_access, .readfn = gt_virt_cnt_read,
55d284af
PM
3155 },
3156 /* Comparison value, indicating when the timer goes off */
3157 { .name = "CNTP_CVAL", .cp = 15, .crm = 14, .opc1 = 2,
9ff9dd3c 3158 .secure = ARM_CP_SECSTATE_NS,
daf1dc5f 3159 .access = PL0_RW,
7a0e58fa 3160 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
55d284af 3161 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
b061a82b 3162 .accessfn = gt_ptimer_access,
bb5972e4
RH
3163 .readfn = gt_phys_redir_cval_read, .raw_readfn = raw_read,
3164 .writefn = gt_phys_redir_cval_write, .raw_writefn = raw_write,
a7adc4b7 3165 },
9c513e78 3166 { .name = "CNTP_CVAL_S", .cp = 15, .crm = 14, .opc1 = 2,
9ff9dd3c 3167 .secure = ARM_CP_SECSTATE_S,
daf1dc5f 3168 .access = PL0_RW,
9ff9dd3c
PM
3169 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
3170 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
3171 .accessfn = gt_ptimer_access,
3172 .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
3173 },
a7adc4b7
PM
3174 { .name = "CNTP_CVAL_EL0", .state = ARM_CP_STATE_AA64,
3175 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 2,
daf1dc5f 3176 .access = PL0_RW,
a7adc4b7
PM
3177 .type = ARM_CP_IO,
3178 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
12cde08a 3179 .resetvalue = 0, .accessfn = gt_ptimer_access,
bb5972e4
RH
3180 .readfn = gt_phys_redir_cval_read, .raw_readfn = raw_read,
3181 .writefn = gt_phys_redir_cval_write, .raw_writefn = raw_write,
55d284af
PM
3182 },
3183 { .name = "CNTV_CVAL", .cp = 15, .crm = 14, .opc1 = 3,
daf1dc5f 3184 .access = PL0_RW,
7a0e58fa 3185 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
55d284af 3186 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
b061a82b 3187 .accessfn = gt_vtimer_access,
bb5972e4
RH
3188 .readfn = gt_virt_redir_cval_read, .raw_readfn = raw_read,
3189 .writefn = gt_virt_redir_cval_write, .raw_writefn = raw_write,
a7adc4b7
PM
3190 },
3191 { .name = "CNTV_CVAL_EL0", .state = ARM_CP_STATE_AA64,
3192 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 2,
daf1dc5f 3193 .access = PL0_RW,
a7adc4b7
PM
3194 .type = ARM_CP_IO,
3195 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
3196 .resetvalue = 0, .accessfn = gt_vtimer_access,
bb5972e4
RH
3197 .readfn = gt_virt_redir_cval_read, .raw_readfn = raw_read,
3198 .writefn = gt_virt_redir_cval_write, .raw_writefn = raw_write,
55d284af 3199 },
b4d3978c
PM
3200 /* Secure timer -- this is actually restricted to only EL3
3201 * and configurably Secure-EL1 via the accessfn.
3202 */
3203 { .name = "CNTPS_TVAL_EL1", .state = ARM_CP_STATE_AA64,
3204 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 0,
3205 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW,
3206 .accessfn = gt_stimer_access,
3207 .readfn = gt_sec_tval_read,
3208 .writefn = gt_sec_tval_write,
3209 .resetfn = gt_sec_timer_reset,
3210 },
3211 { .name = "CNTPS_CTL_EL1", .state = ARM_CP_STATE_AA64,
3212 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 1,
3213 .type = ARM_CP_IO, .access = PL1_RW,
3214 .accessfn = gt_stimer_access,
3215 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].ctl),
3216 .resetvalue = 0,
3217 .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
3218 },
3219 { .name = "CNTPS_CVAL_EL1", .state = ARM_CP_STATE_AA64,
3220 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 2,
3221 .type = ARM_CP_IO, .access = PL1_RW,
3222 .accessfn = gt_stimer_access,
3223 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
3224 .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
3225 },
55d284af
PM
3226 REGINFO_SENTINEL
3227};
3228
bb5972e4
RH
3229static CPAccessResult e2h_access(CPUARMState *env, const ARMCPRegInfo *ri,
3230 bool isread)
3231{
3232 if (!(arm_hcr_el2_eff(env) & HCR_E2H)) {
3233 return CP_ACCESS_TRAP;
3234 }
3235 return CP_ACCESS_OK;
3236}
3237
55d284af 3238#else
26c4a83b
AB
3239
3240/* In user-mode most of the generic timer registers are inaccessible
3241 * however modern kernels (4.12+) allow access to cntvct_el0
55d284af 3242 */
26c4a83b
AB
3243
3244static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
3245{
7def8754
AJ
3246 ARMCPU *cpu = env_archcpu(env);
3247
26c4a83b
AB
3248 /* Currently we have no support for QEMUTimer in linux-user so we
3249 * can't call gt_get_countervalue(env), instead we directly
3250 * call the lower level functions.
3251 */
7def8754 3252 return cpu_get_clock() / gt_cntfrq_period_ns(cpu);
26c4a83b
AB
3253}
3254
6cc7a3ae 3255static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
26c4a83b
AB
3256 { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
3257 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
3258 .type = ARM_CP_CONST, .access = PL0_R /* no PL1_RW in linux-user */,
3259 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
3260 .resetvalue = NANOSECONDS_PER_SECOND / GTIMER_SCALE,
3261 },
3262 { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
3263 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
3264 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3265 .readfn = gt_virt_cnt_read,
3266 },
6cc7a3ae
PM
3267 REGINFO_SENTINEL
3268};
3269
55d284af
PM
3270#endif
3271
c4241c7d 3272static void par_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
4a501606 3273{
891a2fe7 3274 if (arm_feature(env, ARM_FEATURE_LPAE)) {
8d5c773e 3275 raw_write(env, ri, value);
891a2fe7 3276 } else if (arm_feature(env, ARM_FEATURE_V7)) {
8d5c773e 3277 raw_write(env, ri, value & 0xfffff6ff);
4a501606 3278 } else {
8d5c773e 3279 raw_write(env, ri, value & 0xfffff1ff);
4a501606 3280 }
4a501606
PM
3281}
3282
3283#ifndef CONFIG_USER_ONLY
3284/* get_phys_addr() isn't present for user-mode-only targets */
702a9357 3285
3f208fd7
PM
3286static CPAccessResult ats_access(CPUARMState *env, const ARMCPRegInfo *ri,
3287 bool isread)
92611c00
PM
3288{
3289 if (ri->opc2 & 4) {
87562e4f
PM
3290 /* The ATS12NSO* operations must trap to EL3 if executed in
3291 * Secure EL1 (which can only happen if EL3 is AArch64).
3292 * They are simply UNDEF if executed from NS EL1.
3293 * They function normally from EL2 or EL3.
92611c00 3294 */
87562e4f
PM
3295 if (arm_current_el(env) == 1) {
3296 if (arm_is_secure_below_el3(env)) {
3297 return CP_ACCESS_TRAP_UNCATEGORIZED_EL3;
3298 }
3299 return CP_ACCESS_TRAP_UNCATEGORIZED;
3300 }
92611c00
PM
3301 }
3302 return CP_ACCESS_OK;
3303}
3304
060e8a48 3305static uint64_t do_ats_write(CPUARMState *env, uint64_t value,
03ae85f8 3306 MMUAccessType access_type, ARMMMUIdx mmu_idx)
4a501606 3307{
a8170e5e 3308 hwaddr phys_addr;
4a501606
PM
3309 target_ulong page_size;
3310 int prot;
b7cc4e82 3311 bool ret;
01c097f7 3312 uint64_t par64;
1313e2d7 3313 bool format64 = false;
8bf5b6a9 3314 MemTxAttrs attrs = {};
e14b5a23 3315 ARMMMUFaultInfo fi = {};
5b2d261d 3316 ARMCacheAttrs cacheattrs = {};
4a501606 3317
5b2d261d 3318 ret = get_phys_addr(env, value, access_type, mmu_idx, &phys_addr, &attrs,
bc52bfeb 3319 &prot, &page_size, &fi, &cacheattrs);
1313e2d7 3320
0710b2fa
PM
3321 if (ret) {
3322 /*
3323 * Some kinds of translation fault must cause exceptions rather
3324 * than being reported in the PAR.
3325 */
3326 int current_el = arm_current_el(env);
3327 int target_el;
3328 uint32_t syn, fsr, fsc;
3329 bool take_exc = false;
3330
3331 if (fi.s1ptw && current_el == 1 && !arm_is_secure(env)
fee7aa46 3332 && arm_mmu_idx_is_stage1_of_2(mmu_idx)) {
0710b2fa
PM
3333 /*
3334 * Synchronous stage 2 fault on an access made as part of the
3335 * translation table walk for AT S1E0* or AT S1E1* insn
3336 * executed from NS EL1. If this is a synchronous external abort
3337 * and SCR_EL3.EA == 1, then we take a synchronous external abort
3338 * to EL3. Otherwise the fault is taken as an exception to EL2,
3339 * and HPFAR_EL2 holds the faulting IPA.
3340 */
3341 if (fi.type == ARMFault_SyncExternalOnWalk &&
3342 (env->cp15.scr_el3 & SCR_EA)) {
3343 target_el = 3;
3344 } else {
3345 env->cp15.hpfar_el2 = extract64(fi.s2addr, 12, 47) << 4;
3346 target_el = 2;
3347 }
3348 take_exc = true;
3349 } else if (fi.type == ARMFault_SyncExternalOnWalk) {
3350 /*
3351 * Synchronous external aborts during a translation table walk
3352 * are taken as Data Abort exceptions.
3353 */
3354 if (fi.stage2) {
3355 if (current_el == 3) {
3356 target_el = 3;
3357 } else {
3358 target_el = 2;
3359 }
3360 } else {
3361 target_el = exception_target_el(env);
3362 }
3363 take_exc = true;
3364 }
3365
3366 if (take_exc) {
3367 /* Construct FSR and FSC using same logic as arm_deliver_fault() */
3368 if (target_el == 2 || arm_el_is_aa64(env, target_el) ||
3369 arm_s1_regime_using_lpae_format(env, mmu_idx)) {
3370 fsr = arm_fi_to_lfsc(&fi);
3371 fsc = extract32(fsr, 0, 6);
3372 } else {
3373 fsr = arm_fi_to_sfsc(&fi);
3374 fsc = 0x3f;
3375 }
3376 /*
3377 * Report exception with ESR indicating a fault due to a
3378 * translation table walk for a cache maintenance instruction.
3379 */
3380 syn = syn_data_abort_no_iss(current_el == target_el,
3381 fi.ea, 1, fi.s1ptw, 1, fsc);
3382 env->exception.vaddress = value;
3383 env->exception.fsr = fsr;
3384 raise_exception(env, EXCP_DATA_ABORT, syn, target_el);
3385 }
3386 }
3387
1313e2d7
EI
3388 if (is_a64(env)) {
3389 format64 = true;
3390 } else if (arm_feature(env, ARM_FEATURE_LPAE)) {
3391 /*
3392 * ATS1Cxx:
3393 * * TTBCR.EAE determines whether the result is returned using the
3394 * 32-bit or the 64-bit PAR format
3395 * * Instructions executed in Hyp mode always use the 64bit format
3396 *
3397 * ATS1S2NSOxx uses the 64bit format if any of the following is true:
3398 * * The Non-secure TTBCR.EAE bit is set to 1
3399 * * The implementation includes EL2, and the value of HCR.VM is 1
3400 *
9d1bab33
PM
3401 * (Note that HCR.DC makes HCR.VM behave as if it is 1.)
3402 *
23463e0e 3403 * ATS1Hx always uses the 64bit format.
1313e2d7
EI
3404 */
3405 format64 = arm_s1_regime_using_lpae_format(env, mmu_idx);
3406
3407 if (arm_feature(env, ARM_FEATURE_EL2)) {
452ef8cb
RH
3408 if (mmu_idx == ARMMMUIdx_E10_0 ||
3409 mmu_idx == ARMMMUIdx_E10_1 ||
3410 mmu_idx == ARMMMUIdx_E10_1_PAN) {
9d1bab33 3411 format64 |= env->cp15.hcr_el2 & (HCR_VM | HCR_DC);
1313e2d7
EI
3412 } else {
3413 format64 |= arm_current_el(env) == 2;
3414 }
3415 }
3416 }
3417
3418 if (format64) {
5efe9ed4 3419 /* Create a 64-bit PAR */
01c097f7 3420 par64 = (1 << 11); /* LPAE bit always set */
b7cc4e82 3421 if (!ret) {
702a9357 3422 par64 |= phys_addr & ~0xfffULL;
8bf5b6a9
PM
3423 if (!attrs.secure) {
3424 par64 |= (1 << 9); /* NS */
3425 }
5b2d261d
AB
3426 par64 |= (uint64_t)cacheattrs.attrs << 56; /* ATTR */
3427 par64 |= cacheattrs.shareability << 7; /* SH */
4a501606 3428 } else {
5efe9ed4
PM
3429 uint32_t fsr = arm_fi_to_lfsc(&fi);
3430
702a9357 3431 par64 |= 1; /* F */
b7cc4e82 3432 par64 |= (fsr & 0x3f) << 1; /* FS */
0f7b791b
PM
3433 if (fi.stage2) {
3434 par64 |= (1 << 9); /* S */
3435 }
3436 if (fi.s1ptw) {
3437 par64 |= (1 << 8); /* PTW */
3438 }
4a501606
PM
3439 }
3440 } else {
b7cc4e82 3441 /* fsr is a DFSR/IFSR value for the short descriptor
702a9357
PM
3442 * translation table format (with WnR always clear).
3443 * Convert it to a 32-bit PAR.
3444 */
b7cc4e82 3445 if (!ret) {
702a9357
PM
3446 /* We do not set any attribute bits in the PAR */
3447 if (page_size == (1 << 24)
3448 && arm_feature(env, ARM_FEATURE_V7)) {
01c097f7 3449 par64 = (phys_addr & 0xff000000) | (1 << 1);
702a9357 3450 } else {
01c097f7 3451 par64 = phys_addr & 0xfffff000;
702a9357 3452 }
8bf5b6a9
PM
3453 if (!attrs.secure) {
3454 par64 |= (1 << 9); /* NS */
3455 }
702a9357 3456 } else {
5efe9ed4
PM
3457 uint32_t fsr = arm_fi_to_sfsc(&fi);
3458
b7cc4e82
PC
3459 par64 = ((fsr & (1 << 10)) >> 5) | ((fsr & (1 << 12)) >> 6) |
3460 ((fsr & 0xf) << 1) | 1;
702a9357 3461 }
4a501606 3462 }
060e8a48
PM
3463 return par64;
3464}
3465
3466static void ats_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
3467{
03ae85f8 3468 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
060e8a48 3469 uint64_t par64;
d3649702
PM
3470 ARMMMUIdx mmu_idx;
3471 int el = arm_current_el(env);
3472 bool secure = arm_is_secure_below_el3(env);
060e8a48 3473
d3649702
PM
3474 switch (ri->opc2 & 6) {
3475 case 0:
04b07d29 3476 /* stage 1 current state PL1: ATS1CPR, ATS1CPW, ATS1CPRP, ATS1CPWP */
d3649702
PM
3477 switch (el) {
3478 case 3:
127b2b08 3479 mmu_idx = ARMMMUIdx_SE3;
d3649702
PM
3480 break;
3481 case 2:
04b07d29
RH
3482 g_assert(!secure); /* TODO: ARMv8.4-SecEL2 */
3483 /* fall through */
d3649702 3484 case 1:
04b07d29
RH
3485 if (ri->crm == 9 && (env->uncached_cpsr & CPSR_PAN)) {
3486 mmu_idx = (secure ? ARMMMUIdx_SE10_1_PAN
3487 : ARMMMUIdx_Stage1_E1_PAN);
3488 } else {
3489 mmu_idx = secure ? ARMMMUIdx_SE10_1 : ARMMMUIdx_Stage1_E1;
3490 }
d3649702
PM
3491 break;
3492 default:
3493 g_assert_not_reached();
3494 }
3495 break;
3496 case 2:
3497 /* stage 1 current state PL0: ATS1CUR, ATS1CUW */
3498 switch (el) {
3499 case 3:
fba37aed 3500 mmu_idx = ARMMMUIdx_SE10_0;
d3649702
PM
3501 break;
3502 case 2:
2859d7b5 3503 mmu_idx = ARMMMUIdx_Stage1_E0;
d3649702
PM
3504 break;
3505 case 1:
fba37aed 3506 mmu_idx = secure ? ARMMMUIdx_SE10_0 : ARMMMUIdx_Stage1_E0;
d3649702
PM
3507 break;
3508 default:
3509 g_assert_not_reached();
3510 }
3511 break;
3512 case 4:
3513 /* stage 1+2 NonSecure PL1: ATS12NSOPR, ATS12NSOPW */
01b98b68 3514 mmu_idx = ARMMMUIdx_E10_1;
d3649702
PM
3515 break;
3516 case 6:
3517 /* stage 1+2 NonSecure PL0: ATS12NSOUR, ATS12NSOUW */
01b98b68 3518 mmu_idx = ARMMMUIdx_E10_0;
d3649702
PM
3519 break;
3520 default:
3521 g_assert_not_reached();
3522 }
3523
3524 par64 = do_ats_write(env, value, access_type, mmu_idx);
01c097f7
FA
3525
3526 A32_BANKED_CURRENT_REG_SET(env, par, par64);
4a501606 3527}
060e8a48 3528
14db7fe0
PM
3529static void ats1h_write(CPUARMState *env, const ARMCPRegInfo *ri,
3530 uint64_t value)
3531{
03ae85f8 3532 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
14db7fe0
PM
3533 uint64_t par64;
3534
e013b741 3535 par64 = do_ats_write(env, value, access_type, ARMMMUIdx_E2);
14db7fe0
PM
3536
3537 A32_BANKED_CURRENT_REG_SET(env, par, par64);
3538}
3539
3f208fd7
PM
3540static CPAccessResult at_s1e2_access(CPUARMState *env, const ARMCPRegInfo *ri,
3541 bool isread)
2a47df95
PM
3542{
3543 if (arm_current_el(env) == 3 && !(env->cp15.scr_el3 & SCR_NS)) {
3544 return CP_ACCESS_TRAP;
3545 }
3546 return CP_ACCESS_OK;
3547}
3548
060e8a48
PM
3549static void ats_write64(CPUARMState *env, const ARMCPRegInfo *ri,
3550 uint64_t value)
3551{
03ae85f8 3552 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
d3649702
PM
3553 ARMMMUIdx mmu_idx;
3554 int secure = arm_is_secure_below_el3(env);
3555
3556 switch (ri->opc2 & 6) {
3557 case 0:
3558 switch (ri->opc1) {
04b07d29
RH
3559 case 0: /* AT S1E1R, AT S1E1W, AT S1E1RP, AT S1E1WP */
3560 if (ri->crm == 9 && (env->pstate & PSTATE_PAN)) {
3561 mmu_idx = (secure ? ARMMMUIdx_SE10_1_PAN
3562 : ARMMMUIdx_Stage1_E1_PAN);
3563 } else {
3564 mmu_idx = secure ? ARMMMUIdx_SE10_1 : ARMMMUIdx_Stage1_E1;
3565 }
d3649702
PM
3566 break;
3567 case 4: /* AT S1E2R, AT S1E2W */
e013b741 3568 mmu_idx = ARMMMUIdx_E2;
d3649702
PM
3569 break;
3570 case 6: /* AT S1E3R, AT S1E3W */
127b2b08 3571 mmu_idx = ARMMMUIdx_SE3;
d3649702
PM
3572 break;
3573 default:
3574 g_assert_not_reached();
3575 }
3576 break;
3577 case 2: /* AT S1E0R, AT S1E0W */
fba37aed 3578 mmu_idx = secure ? ARMMMUIdx_SE10_0 : ARMMMUIdx_Stage1_E0;
d3649702
PM
3579 break;
3580 case 4: /* AT S12E1R, AT S12E1W */
fba37aed 3581 mmu_idx = secure ? ARMMMUIdx_SE10_1 : ARMMMUIdx_E10_1;
d3649702
PM
3582 break;
3583 case 6: /* AT S12E0R, AT S12E0W */
fba37aed 3584 mmu_idx = secure ? ARMMMUIdx_SE10_0 : ARMMMUIdx_E10_0;
d3649702
PM
3585 break;
3586 default:
3587 g_assert_not_reached();
3588 }
060e8a48 3589
d3649702 3590 env->cp15.par_el[1] = do_ats_write(env, value, access_type, mmu_idx);
060e8a48 3591}
4a501606
PM
3592#endif
3593
3594static const ARMCPRegInfo vapa_cp_reginfo[] = {
3595 { .name = "PAR", .cp = 15, .crn = 7, .crm = 4, .opc1 = 0, .opc2 = 0,
3596 .access = PL1_RW, .resetvalue = 0,
01c097f7
FA
3597 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.par_s),
3598 offsetoflow32(CPUARMState, cp15.par_ns) },
4a501606
PM
3599 .writefn = par_write },
3600#ifndef CONFIG_USER_ONLY
87562e4f 3601 /* This underdecoding is safe because the reginfo is NO_RAW. */
4a501606 3602 { .name = "ATS", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = CP_ANY,
92611c00 3603 .access = PL1_W, .accessfn = ats_access,
0710b2fa 3604 .writefn = ats_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
4a501606
PM
3605#endif
3606 REGINFO_SENTINEL
3607};
3608
18032bec
PM
3609/* Return basic MPU access permission bits. */
3610static uint32_t simple_mpu_ap_bits(uint32_t val)
3611{
3612 uint32_t ret;
3613 uint32_t mask;
3614 int i;
3615 ret = 0;
3616 mask = 3;
3617 for (i = 0; i < 16; i += 2) {
3618 ret |= (val >> i) & mask;
3619 mask <<= 2;
3620 }
3621 return ret;
3622}
3623
3624/* Pad basic MPU access permission bits to extended format. */
3625static uint32_t extended_mpu_ap_bits(uint32_t val)
3626{
3627 uint32_t ret;
3628 uint32_t mask;
3629 int i;
3630 ret = 0;
3631 mask = 3;
3632 for (i = 0; i < 16; i += 2) {
3633 ret |= (val & mask) << i;
3634 mask <<= 2;
3635 }
3636 return ret;
3637}
3638
c4241c7d
PM
3639static void pmsav5_data_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
3640 uint64_t value)
18032bec 3641{
7e09797c 3642 env->cp15.pmsav5_data_ap = extended_mpu_ap_bits(value);
18032bec
PM
3643}
3644
c4241c7d 3645static uint64_t pmsav5_data_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
18032bec 3646{
7e09797c 3647 return simple_mpu_ap_bits(env->cp15.pmsav5_data_ap);
18032bec
PM
3648}
3649
c4241c7d
PM
3650static void pmsav5_insn_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
3651 uint64_t value)
18032bec 3652{
7e09797c 3653 env->cp15.pmsav5_insn_ap = extended_mpu_ap_bits(value);
18032bec
PM
3654}
3655
c4241c7d 3656static uint64_t pmsav5_insn_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
18032bec 3657{
7e09797c 3658 return simple_mpu_ap_bits(env->cp15.pmsav5_insn_ap);
18032bec
PM
3659}
3660
6cb0b013
PC
3661static uint64_t pmsav7_read(CPUARMState *env, const ARMCPRegInfo *ri)
3662{
3663 uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
3664
3665 if (!u32p) {
3666 return 0;
3667 }
3668
1bc04a88 3669 u32p += env->pmsav7.rnr[M_REG_NS];
6cb0b013
PC
3670 return *u32p;
3671}
3672
3673static void pmsav7_write(CPUARMState *env, const ARMCPRegInfo *ri,
3674 uint64_t value)
3675{
2fc0cc0e 3676 ARMCPU *cpu = env_archcpu(env);
6cb0b013
PC
3677 uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
3678
3679 if (!u32p) {
3680 return;
3681 }
3682
1bc04a88 3683 u32p += env->pmsav7.rnr[M_REG_NS];
d10eb08f 3684 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
6cb0b013
PC
3685 *u32p = value;
3686}
3687
6cb0b013
PC
3688static void pmsav7_rgnr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3689 uint64_t value)
3690{
2fc0cc0e 3691 ARMCPU *cpu = env_archcpu(env);
6cb0b013
PC
3692 uint32_t nrgs = cpu->pmsav7_dregion;
3693
3694 if (value >= nrgs) {
3695 qemu_log_mask(LOG_GUEST_ERROR,
3696 "PMSAv7 RGNR write >= # supported regions, %" PRIu32
3697 " > %" PRIu32 "\n", (uint32_t)value, nrgs);
3698 return;
3699 }
3700
3701 raw_write(env, ri, value);
3702}
3703
3704static const ARMCPRegInfo pmsav7_cp_reginfo[] = {
69ceea64
PM
3705 /* Reset for all these registers is handled in arm_cpu_reset(),
3706 * because the PMSAv7 is also used by M-profile CPUs, which do
3707 * not register cpregs but still need the state to be reset.
3708 */
6cb0b013
PC
3709 { .name = "DRBAR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 0,
3710 .access = PL1_RW, .type = ARM_CP_NO_RAW,
3711 .fieldoffset = offsetof(CPUARMState, pmsav7.drbar),
69ceea64
PM
3712 .readfn = pmsav7_read, .writefn = pmsav7_write,
3713 .resetfn = arm_cp_reset_ignore },
6cb0b013
PC
3714 { .name = "DRSR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 2,
3715 .access = PL1_RW, .type = ARM_CP_NO_RAW,
3716 .fieldoffset = offsetof(CPUARMState, pmsav7.drsr),
69ceea64
PM
3717 .readfn = pmsav7_read, .writefn = pmsav7_write,
3718 .resetfn = arm_cp_reset_ignore },
6cb0b013
PC
3719 { .name = "DRACR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 4,
3720 .access = PL1_RW, .type = ARM_CP_NO_RAW,
3721 .fieldoffset = offsetof(CPUARMState, pmsav7.dracr),
69ceea64
PM
3722 .readfn = pmsav7_read, .writefn = pmsav7_write,
3723 .resetfn = arm_cp_reset_ignore },
6cb0b013
PC
3724 { .name = "RGNR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 2, .opc2 = 0,
3725 .access = PL1_RW,
1bc04a88 3726 .fieldoffset = offsetof(CPUARMState, pmsav7.rnr[M_REG_NS]),
69ceea64
PM
3727 .writefn = pmsav7_rgnr_write,
3728 .resetfn = arm_cp_reset_ignore },
6cb0b013
PC
3729 REGINFO_SENTINEL
3730};
3731
18032bec
PM
3732static const ARMCPRegInfo pmsav5_cp_reginfo[] = {
3733 { .name = "DATA_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
7a0e58fa 3734 .access = PL1_RW, .type = ARM_CP_ALIAS,
7e09797c 3735 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
18032bec
PM
3736 .readfn = pmsav5_data_ap_read, .writefn = pmsav5_data_ap_write, },
3737 { .name = "INSN_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
7a0e58fa 3738 .access = PL1_RW, .type = ARM_CP_ALIAS,
7e09797c 3739 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
18032bec
PM
3740 .readfn = pmsav5_insn_ap_read, .writefn = pmsav5_insn_ap_write, },
3741 { .name = "DATA_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 2,
3742 .access = PL1_RW,
7e09797c
PM
3743 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
3744 .resetvalue = 0, },
18032bec
PM
3745 { .name = "INSN_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 3,
3746 .access = PL1_RW,
7e09797c
PM
3747 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
3748 .resetvalue = 0, },
ecce5c3c
PM
3749 { .name = "DCACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
3750 .access = PL1_RW,
3751 .fieldoffset = offsetof(CPUARMState, cp15.c2_data), .resetvalue = 0, },
3752 { .name = "ICACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 1,
3753 .access = PL1_RW,
3754 .fieldoffset = offsetof(CPUARMState, cp15.c2_insn), .resetvalue = 0, },
06d76f31 3755 /* Protection region base and size registers */
e508a92b
PM
3756 { .name = "946_PRBS0", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0,
3757 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3758 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[0]) },
3759 { .name = "946_PRBS1", .cp = 15, .crn = 6, .crm = 1, .opc1 = 0,
3760 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3761 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[1]) },
3762 { .name = "946_PRBS2", .cp = 15, .crn = 6, .crm = 2, .opc1 = 0,
3763 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3764 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[2]) },
3765 { .name = "946_PRBS3", .cp = 15, .crn = 6, .crm = 3, .opc1 = 0,
3766 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3767 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[3]) },
3768 { .name = "946_PRBS4", .cp = 15, .crn = 6, .crm = 4, .opc1 = 0,
3769 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3770 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[4]) },
3771 { .name = "946_PRBS5", .cp = 15, .crn = 6, .crm = 5, .opc1 = 0,
3772 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3773 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[5]) },
3774 { .name = "946_PRBS6", .cp = 15, .crn = 6, .crm = 6, .opc1 = 0,
3775 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3776 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[6]) },
3777 { .name = "946_PRBS7", .cp = 15, .crn = 6, .crm = 7, .opc1 = 0,
3778 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3779 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[7]) },
18032bec
PM
3780 REGINFO_SENTINEL
3781};
3782
c4241c7d
PM
3783static void vmsa_ttbcr_raw_write(CPUARMState *env, const ARMCPRegInfo *ri,
3784 uint64_t value)
ecce5c3c 3785{
11f136ee 3786 TCR *tcr = raw_ptr(env, ri);
2ebcebe2
PM
3787 int maskshift = extract32(value, 0, 3);
3788
e389be16
FA
3789 if (!arm_feature(env, ARM_FEATURE_V8)) {
3790 if (arm_feature(env, ARM_FEATURE_LPAE) && (value & TTBCR_EAE)) {
3791 /* Pre ARMv8 bits [21:19], [15:14] and [6:3] are UNK/SBZP when
3792 * using Long-desciptor translation table format */
3793 value &= ~((7 << 19) | (3 << 14) | (0xf << 3));
3794 } else if (arm_feature(env, ARM_FEATURE_EL3)) {
3795 /* In an implementation that includes the Security Extensions
3796 * TTBCR has additional fields PD0 [4] and PD1 [5] for
3797 * Short-descriptor translation table format.
3798 */
3799 value &= TTBCR_PD1 | TTBCR_PD0 | TTBCR_N;
3800 } else {
3801 value &= TTBCR_N;
3802 }
e42c4db3 3803 }
e389be16 3804
b6af0975 3805 /* Update the masks corresponding to the TCR bank being written
11f136ee 3806 * Note that we always calculate mask and base_mask, but
e42c4db3 3807 * they are only used for short-descriptor tables (ie if EAE is 0);
11f136ee
FA
3808 * for long-descriptor tables the TCR fields are used differently
3809 * and the mask and base_mask values are meaningless.
e42c4db3 3810 */
11f136ee
FA
3811 tcr->raw_tcr = value;
3812 tcr->mask = ~(((uint32_t)0xffffffffu) >> maskshift);
3813 tcr->base_mask = ~((uint32_t)0x3fffu >> maskshift);
ecce5c3c
PM
3814}
3815
c4241c7d
PM
3816static void vmsa_ttbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3817 uint64_t value)
d4e6df63 3818{
2fc0cc0e 3819 ARMCPU *cpu = env_archcpu(env);
ab638a32 3820 TCR *tcr = raw_ptr(env, ri);
00c8cb0a 3821
d4e6df63
PM
3822 if (arm_feature(env, ARM_FEATURE_LPAE)) {
3823 /* With LPAE the TTBCR could result in a change of ASID
3824 * via the TTBCR.A1 bit, so do a TLB flush.
3825 */
d10eb08f 3826 tlb_flush(CPU(cpu));
d4e6df63 3827 }
ab638a32
RH
3828 /* Preserve the high half of TCR_EL1, set via TTBCR2. */
3829 value = deposit64(tcr->raw_tcr, 0, 32, value);
c4241c7d 3830 vmsa_ttbcr_raw_write(env, ri, value);
d4e6df63
PM
3831}
3832
ecce5c3c
PM
3833static void vmsa_ttbcr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
3834{
11f136ee
FA
3835 TCR *tcr = raw_ptr(env, ri);
3836
3837 /* Reset both the TCR as well as the masks corresponding to the bank of
3838 * the TCR being reset.
3839 */
3840 tcr->raw_tcr = 0;
3841 tcr->mask = 0;
3842 tcr->base_mask = 0xffffc000u;
ecce5c3c
PM
3843}
3844
d06dc933 3845static void vmsa_tcr_el12_write(CPUARMState *env, const ARMCPRegInfo *ri,
cb2e37df
PM
3846 uint64_t value)
3847{
2fc0cc0e 3848 ARMCPU *cpu = env_archcpu(env);
11f136ee 3849 TCR *tcr = raw_ptr(env, ri);
00c8cb0a 3850
cb2e37df 3851 /* For AArch64 the A1 bit could result in a change of ASID, so TLB flush. */
d10eb08f 3852 tlb_flush(CPU(cpu));
11f136ee 3853 tcr->raw_tcr = value;
cb2e37df
PM
3854}
3855
327ed10f
PM
3856static void vmsa_ttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3857 uint64_t value)
3858{
93f379b0
RH
3859 /* If the ASID changes (with a 64-bit write), we must flush the TLB. */
3860 if (cpreg_field_is_64bit(ri) &&
3861 extract64(raw_read(env, ri) ^ value, 48, 16) != 0) {
2fc0cc0e 3862 ARMCPU *cpu = env_archcpu(env);
d10eb08f 3863 tlb_flush(CPU(cpu));
327ed10f
PM
3864 }
3865 raw_write(env, ri, value);
3866}
3867
ed30da8e
RH
3868static void vmsa_tcr_ttbr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
3869 uint64_t value)
3870{
d06dc933
RH
3871 /*
3872 * If we are running with E2&0 regime, then an ASID is active.
3873 * Flush if that might be changing. Note we're not checking
3874 * TCR_EL2.A1 to know if this is really the TTBRx_EL2 that
3875 * holds the active ASID, only checking the field that might.
3876 */
3877 if (extract64(raw_read(env, ri) ^ value, 48, 16) &&
3878 (arm_hcr_el2_eff(env) & HCR_E2H)) {
3879 tlb_flush_by_mmuidx(env_cpu(env),
452ef8cb
RH
3880 ARMMMUIdxBit_E20_2 |
3881 ARMMMUIdxBit_E20_2_PAN |
3882 ARMMMUIdxBit_E20_0);
d06dc933 3883 }
ed30da8e
RH
3884 raw_write(env, ri, value);
3885}
3886
b698e9cf
EI
3887static void vttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3888 uint64_t value)
3889{
2fc0cc0e 3890 ARMCPU *cpu = env_archcpu(env);
b698e9cf
EI
3891 CPUState *cs = CPU(cpu);
3892
97fa9350
RH
3893 /*
3894 * A change in VMID to the stage2 page table (Stage2) invalidates
3895 * the combined stage 1&2 tlbs (EL10_1 and EL10_0).
3896 */
b698e9cf 3897 if (raw_read(env, ri) != value) {
0336cbf8 3898 tlb_flush_by_mmuidx(cs,
01b98b68 3899 ARMMMUIdxBit_E10_1 |
452ef8cb 3900 ARMMMUIdxBit_E10_1_PAN |
01b98b68 3901 ARMMMUIdxBit_E10_0 |
97fa9350 3902 ARMMMUIdxBit_Stage2);
b698e9cf
EI
3903 raw_write(env, ri, value);
3904 }
3905}
3906
8e5d75c9 3907static const ARMCPRegInfo vmsa_pmsa_cp_reginfo[] = {
18032bec 3908 { .name = "DFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
84929218 3909 .access = PL1_RW, .accessfn = access_tvm_trvm, .type = ARM_CP_ALIAS,
4a7e2d73 3910 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dfsr_s),
b061a82b 3911 offsetoflow32(CPUARMState, cp15.dfsr_ns) }, },
18032bec 3912 { .name = "IFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
84929218 3913 .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
88ca1c2d
FA
3914 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.ifsr_s),
3915 offsetoflow32(CPUARMState, cp15.ifsr_ns) } },
8e5d75c9 3916 { .name = "DFAR", .cp = 15, .opc1 = 0, .crn = 6, .crm = 0, .opc2 = 0,
84929218 3917 .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
8e5d75c9
PC
3918 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.dfar_s),
3919 offsetof(CPUARMState, cp15.dfar_ns) } },
3920 { .name = "FAR_EL1", .state = ARM_CP_STATE_AA64,
3921 .opc0 = 3, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 0,
84929218
RH
3922 .access = PL1_RW, .accessfn = access_tvm_trvm,
3923 .fieldoffset = offsetof(CPUARMState, cp15.far_el[1]),
8e5d75c9
PC
3924 .resetvalue = 0, },
3925 REGINFO_SENTINEL
3926};
3927
3928static const ARMCPRegInfo vmsa_cp_reginfo[] = {
6cd8a264
RH
3929 { .name = "ESR_EL1", .state = ARM_CP_STATE_AA64,
3930 .opc0 = 3, .crn = 5, .crm = 2, .opc1 = 0, .opc2 = 0,
84929218 3931 .access = PL1_RW, .accessfn = access_tvm_trvm,
d81c519c 3932 .fieldoffset = offsetof(CPUARMState, cp15.esr_el[1]), .resetvalue = 0, },
327ed10f 3933 { .name = "TTBR0_EL1", .state = ARM_CP_STATE_BOTH,
7dd8c9af 3934 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 0,
84929218
RH
3935 .access = PL1_RW, .accessfn = access_tvm_trvm,
3936 .writefn = vmsa_ttbr_write, .resetvalue = 0,
7dd8c9af
FA
3937 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
3938 offsetof(CPUARMState, cp15.ttbr0_ns) } },
327ed10f 3939 { .name = "TTBR1_EL1", .state = ARM_CP_STATE_BOTH,
7dd8c9af 3940 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 1,
84929218
RH
3941 .access = PL1_RW, .accessfn = access_tvm_trvm,
3942 .writefn = vmsa_ttbr_write, .resetvalue = 0,
7dd8c9af
FA
3943 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
3944 offsetof(CPUARMState, cp15.ttbr1_ns) } },
cb2e37df
PM
3945 { .name = "TCR_EL1", .state = ARM_CP_STATE_AA64,
3946 .opc0 = 3, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
84929218
RH
3947 .access = PL1_RW, .accessfn = access_tvm_trvm,
3948 .writefn = vmsa_tcr_el12_write,
cb2e37df 3949 .resetfn = vmsa_ttbcr_reset, .raw_writefn = raw_write,
11f136ee 3950 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[1]) },
cb2e37df 3951 { .name = "TTBCR", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
84929218
RH
3952 .access = PL1_RW, .accessfn = access_tvm_trvm,
3953 .type = ARM_CP_ALIAS, .writefn = vmsa_ttbcr_write,
b061a82b 3954 .raw_writefn = vmsa_ttbcr_raw_write,
11f136ee
FA
3955 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tcr_el[3]),
3956 offsetoflow32(CPUARMState, cp15.tcr_el[1])} },
18032bec
PM
3957 REGINFO_SENTINEL
3958};
3959
ab638a32
RH
3960/* Note that unlike TTBCR, writing to TTBCR2 does not require flushing
3961 * qemu tlbs nor adjusting cached masks.
3962 */
3963static const ARMCPRegInfo ttbcr2_reginfo = {
3964 .name = "TTBCR2", .cp = 15, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 3,
84929218
RH
3965 .access = PL1_RW, .accessfn = access_tvm_trvm,
3966 .type = ARM_CP_ALIAS,
ab638a32
RH
3967 .bank_fieldoffsets = { offsetofhigh32(CPUARMState, cp15.tcr_el[3]),
3968 offsetofhigh32(CPUARMState, cp15.tcr_el[1]) },
3969};
3970
c4241c7d
PM
3971static void omap_ticonfig_write(CPUARMState *env, const ARMCPRegInfo *ri,
3972 uint64_t value)
1047b9d7
PM
3973{
3974 env->cp15.c15_ticonfig = value & 0xe7;
3975 /* The OS_TYPE bit in this register changes the reported CPUID! */
3976 env->cp15.c0_cpuid = (value & (1 << 5)) ?
3977 ARM_CPUID_TI915T : ARM_CPUID_TI925T;
1047b9d7
PM
3978}
3979
c4241c7d
PM
3980static void omap_threadid_write(CPUARMState *env, const ARMCPRegInfo *ri,
3981 uint64_t value)
1047b9d7
PM
3982{
3983 env->cp15.c15_threadid = value & 0xffff;
1047b9d7
PM
3984}
3985
c4241c7d
PM
3986static void omap_wfi_write(CPUARMState *env, const ARMCPRegInfo *ri,
3987 uint64_t value)
1047b9d7
PM
3988{
3989 /* Wait-for-interrupt (deprecated) */
2fc0cc0e 3990 cpu_interrupt(env_cpu(env), CPU_INTERRUPT_HALT);
1047b9d7
PM
3991}
3992
c4241c7d
PM
3993static void omap_cachemaint_write(CPUARMState *env, const ARMCPRegInfo *ri,
3994 uint64_t value)
c4804214
PM
3995{
3996 /* On OMAP there are registers indicating the max/min index of dcache lines
3997 * containing a dirty line; cache flush operations have to reset these.
3998 */
3999 env->cp15.c15_i_max = 0x000;
4000 env->cp15.c15_i_min = 0xff0;
c4804214
PM
4001}
4002
18032bec
PM
4003static const ARMCPRegInfo omap_cp_reginfo[] = {
4004 { .name = "DFSR", .cp = 15, .crn = 5, .crm = CP_ANY,
4005 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_OVERRIDE,
d81c519c 4006 .fieldoffset = offsetoflow32(CPUARMState, cp15.esr_el[1]),
6cd8a264 4007 .resetvalue = 0, },
1047b9d7
PM
4008 { .name = "", .cp = 15, .crn = 15, .crm = 0, .opc1 = 0, .opc2 = 0,
4009 .access = PL1_RW, .type = ARM_CP_NOP },
4010 { .name = "TICONFIG", .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0,
4011 .access = PL1_RW,
4012 .fieldoffset = offsetof(CPUARMState, cp15.c15_ticonfig), .resetvalue = 0,
4013 .writefn = omap_ticonfig_write },
4014 { .name = "IMAX", .cp = 15, .crn = 15, .crm = 2, .opc1 = 0, .opc2 = 0,
4015 .access = PL1_RW,
4016 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_max), .resetvalue = 0, },
4017 { .name = "IMIN", .cp = 15, .crn = 15, .crm = 3, .opc1 = 0, .opc2 = 0,
4018 .access = PL1_RW, .resetvalue = 0xff0,
4019 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_min) },
4020 { .name = "THREADID", .cp = 15, .crn = 15, .crm = 4, .opc1 = 0, .opc2 = 0,
4021 .access = PL1_RW,
4022 .fieldoffset = offsetof(CPUARMState, cp15.c15_threadid), .resetvalue = 0,
4023 .writefn = omap_threadid_write },
4024 { .name = "TI925T_STATUS", .cp = 15, .crn = 15,
4025 .crm = 8, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
7a0e58fa 4026 .type = ARM_CP_NO_RAW,
1047b9d7
PM
4027 .readfn = arm_cp_read_zero, .writefn = omap_wfi_write, },
4028 /* TODO: Peripheral port remap register:
4029 * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt controller
4030 * base address at $rn & ~0xfff and map size of 0x200 << ($rn & 0xfff),
4031 * when MMU is off.
4032 */
c4804214 4033 { .name = "OMAP_CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
d4e6df63 4034 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
7a0e58fa 4035 .type = ARM_CP_OVERRIDE | ARM_CP_NO_RAW,
c4804214 4036 .writefn = omap_cachemaint_write },
34f90529
PM
4037 { .name = "C9", .cp = 15, .crn = 9,
4038 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW,
4039 .type = ARM_CP_CONST | ARM_CP_OVERRIDE, .resetvalue = 0 },
1047b9d7
PM
4040 REGINFO_SENTINEL
4041};
4042
c4241c7d
PM
4043static void xscale_cpar_write(CPUARMState *env, const ARMCPRegInfo *ri,
4044 uint64_t value)
1047b9d7 4045{
c0f4af17 4046 env->cp15.c15_cpar = value & 0x3fff;
1047b9d7
PM
4047}
4048
4049static const ARMCPRegInfo xscale_cp_reginfo[] = {
4050 { .name = "XSCALE_CPAR",
4051 .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
4052 .fieldoffset = offsetof(CPUARMState, cp15.c15_cpar), .resetvalue = 0,
4053 .writefn = xscale_cpar_write, },
2771db27
PM
4054 { .name = "XSCALE_AUXCR",
4055 .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 1, .access = PL1_RW,
4056 .fieldoffset = offsetof(CPUARMState, cp15.c1_xscaleauxcr),
4057 .resetvalue = 0, },
3b771579
PM
4058 /* XScale specific cache-lockdown: since we have no cache we NOP these
4059 * and hope the guest does not really rely on cache behaviour.
4060 */
4061 { .name = "XSCALE_LOCK_ICACHE_LINE",
4062 .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 0,
4063 .access = PL1_W, .type = ARM_CP_NOP },
4064 { .name = "XSCALE_UNLOCK_ICACHE",
4065 .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 1,
4066 .access = PL1_W, .type = ARM_CP_NOP },
4067 { .name = "XSCALE_DCACHE_LOCK",
4068 .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 0,
4069 .access = PL1_RW, .type = ARM_CP_NOP },
4070 { .name = "XSCALE_UNLOCK_DCACHE",
4071 .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 1,
4072 .access = PL1_W, .type = ARM_CP_NOP },
1047b9d7
PM
4073 REGINFO_SENTINEL
4074};
4075
4076static const ARMCPRegInfo dummy_c15_cp_reginfo[] = {
4077 /* RAZ/WI the whole crn=15 space, when we don't have a more specific
4078 * implementation of this implementation-defined space.
4079 * Ideally this should eventually disappear in favour of actually
4080 * implementing the correct behaviour for all cores.
4081 */
4082 { .name = "C15_IMPDEF", .cp = 15, .crn = 15,
4083 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
3671cd87 4084 .access = PL1_RW,
7a0e58fa 4085 .type = ARM_CP_CONST | ARM_CP_NO_RAW | ARM_CP_OVERRIDE,
d4e6df63 4086 .resetvalue = 0 },
18032bec
PM
4087 REGINFO_SENTINEL
4088};
4089
c4804214
PM
4090static const ARMCPRegInfo cache_dirty_status_cp_reginfo[] = {
4091 /* Cache status: RAZ because we have no cache so it's always clean */
4092 { .name = "CDSR", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 6,
7a0e58fa 4093 .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 4094 .resetvalue = 0 },
c4804214
PM
4095 REGINFO_SENTINEL
4096};
4097
4098static const ARMCPRegInfo cache_block_ops_cp_reginfo[] = {
4099 /* We never have a a block transfer operation in progress */
4100 { .name = "BXSR", .cp = 15, .crn = 7, .crm = 12, .opc1 = 0, .opc2 = 4,
7a0e58fa 4101 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 4102 .resetvalue = 0 },
30b05bba
PM
4103 /* The cache ops themselves: these all NOP for QEMU */
4104 { .name = "IICR", .cp = 15, .crm = 5, .opc1 = 0,
4105 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
4106 { .name = "IDCR", .cp = 15, .crm = 6, .opc1 = 0,
4107 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
4108 { .name = "CDCR", .cp = 15, .crm = 12, .opc1 = 0,
4109 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
4110 { .name = "PIR", .cp = 15, .crm = 12, .opc1 = 1,
4111 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
4112 { .name = "PDR", .cp = 15, .crm = 12, .opc1 = 2,
4113 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
4114 { .name = "CIDCR", .cp = 15, .crm = 14, .opc1 = 0,
4115 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
c4804214
PM
4116 REGINFO_SENTINEL
4117};
4118
4119static const ARMCPRegInfo cache_test_clean_cp_reginfo[] = {
4120 /* The cache test-and-clean instructions always return (1 << 30)
4121 * to indicate that there are no dirty cache lines.
4122 */
4123 { .name = "TC_DCACHE", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 3,
7a0e58fa 4124 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 4125 .resetvalue = (1 << 30) },
c4804214 4126 { .name = "TCI_DCACHE", .cp = 15, .crn = 7, .crm = 14, .opc1 = 0, .opc2 = 3,
7a0e58fa 4127 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 4128 .resetvalue = (1 << 30) },
c4804214
PM
4129 REGINFO_SENTINEL
4130};
4131
34f90529
PM
4132static const ARMCPRegInfo strongarm_cp_reginfo[] = {
4133 /* Ignore ReadBuffer accesses */
4134 { .name = "C9_READBUFFER", .cp = 15, .crn = 9,
4135 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
d4e6df63 4136 .access = PL1_RW, .resetvalue = 0,
7a0e58fa 4137 .type = ARM_CP_CONST | ARM_CP_OVERRIDE | ARM_CP_NO_RAW },
34f90529
PM
4138 REGINFO_SENTINEL
4139};
4140
731de9e6
EI
4141static uint64_t midr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4142{
2fc0cc0e 4143 ARMCPU *cpu = env_archcpu(env);
731de9e6
EI
4144 unsigned int cur_el = arm_current_el(env);
4145 bool secure = arm_is_secure(env);
4146
4147 if (arm_feature(&cpu->env, ARM_FEATURE_EL2) && !secure && cur_el == 1) {
4148 return env->cp15.vpidr_el2;
4149 }
4150 return raw_read(env, ri);
4151}
4152
06a7e647 4153static uint64_t mpidr_read_val(CPUARMState *env)
81bdde9d 4154{
2fc0cc0e 4155 ARMCPU *cpu = env_archcpu(env);
eb5e1d3c
PF
4156 uint64_t mpidr = cpu->mp_affinity;
4157
81bdde9d 4158 if (arm_feature(env, ARM_FEATURE_V7MP)) {
78dbbbe4 4159 mpidr |= (1U << 31);
81bdde9d
PM
4160 /* Cores which are uniprocessor (non-coherent)
4161 * but still implement the MP extensions set
a8e81b31 4162 * bit 30. (For instance, Cortex-R5).
81bdde9d 4163 */
a8e81b31
PC
4164 if (cpu->mp_is_up) {
4165 mpidr |= (1u << 30);
4166 }
81bdde9d 4167 }
c4241c7d 4168 return mpidr;
81bdde9d
PM
4169}
4170
06a7e647
EI
4171static uint64_t mpidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4172{
f0d574d6
EI
4173 unsigned int cur_el = arm_current_el(env);
4174 bool secure = arm_is_secure(env);
4175
4176 if (arm_feature(env, ARM_FEATURE_EL2) && !secure && cur_el == 1) {
4177 return env->cp15.vmpidr_el2;
4178 }
06a7e647
EI
4179 return mpidr_read_val(env);
4180}
4181
7ac681cf 4182static const ARMCPRegInfo lpae_cp_reginfo[] = {
a903c449 4183 /* NOP AMAIR0/1 */
b0fe2427
PM
4184 { .name = "AMAIR0", .state = ARM_CP_STATE_BOTH,
4185 .opc0 = 3, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 0,
84929218
RH
4186 .access = PL1_RW, .accessfn = access_tvm_trvm,
4187 .type = ARM_CP_CONST, .resetvalue = 0 },
b0fe2427 4188 /* AMAIR1 is mapped to AMAIR_EL1[63:32] */
7ac681cf 4189 { .name = "AMAIR1", .cp = 15, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 1,
84929218
RH
4190 .access = PL1_RW, .accessfn = access_tvm_trvm,
4191 .type = ARM_CP_CONST, .resetvalue = 0 },
891a2fe7 4192 { .name = "PAR", .cp = 15, .crm = 7, .opc1 = 0,
01c097f7
FA
4193 .access = PL1_RW, .type = ARM_CP_64BIT, .resetvalue = 0,
4194 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.par_s),
4195 offsetof(CPUARMState, cp15.par_ns)} },
891a2fe7 4196 { .name = "TTBR0", .cp = 15, .crm = 2, .opc1 = 0,
84929218
RH
4197 .access = PL1_RW, .accessfn = access_tvm_trvm,
4198 .type = ARM_CP_64BIT | ARM_CP_ALIAS,
7dd8c9af
FA
4199 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
4200 offsetof(CPUARMState, cp15.ttbr0_ns) },
b061a82b 4201 .writefn = vmsa_ttbr_write, },
891a2fe7 4202 { .name = "TTBR1", .cp = 15, .crm = 2, .opc1 = 1,
84929218
RH
4203 .access = PL1_RW, .accessfn = access_tvm_trvm,
4204 .type = ARM_CP_64BIT | ARM_CP_ALIAS,
7dd8c9af
FA
4205 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
4206 offsetof(CPUARMState, cp15.ttbr1_ns) },
b061a82b 4207 .writefn = vmsa_ttbr_write, },
7ac681cf
PM
4208 REGINFO_SENTINEL
4209};
4210
c4241c7d 4211static uint64_t aa64_fpcr_read(CPUARMState *env, const ARMCPRegInfo *ri)
b0d2b7d0 4212{
c4241c7d 4213 return vfp_get_fpcr(env);
b0d2b7d0
PM
4214}
4215
c4241c7d
PM
4216static void aa64_fpcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4217 uint64_t value)
b0d2b7d0
PM
4218{
4219 vfp_set_fpcr(env, value);
b0d2b7d0
PM
4220}
4221
c4241c7d 4222static uint64_t aa64_fpsr_read(CPUARMState *env, const ARMCPRegInfo *ri)
b0d2b7d0 4223{
c4241c7d 4224 return vfp_get_fpsr(env);
b0d2b7d0
PM
4225}
4226
c4241c7d
PM
4227static void aa64_fpsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4228 uint64_t value)
b0d2b7d0
PM
4229{
4230 vfp_set_fpsr(env, value);
b0d2b7d0
PM
4231}
4232
3f208fd7
PM
4233static CPAccessResult aa64_daif_access(CPUARMState *env, const ARMCPRegInfo *ri,
4234 bool isread)
c2b820fe 4235{
aaec1432 4236 if (arm_current_el(env) == 0 && !(arm_sctlr(env, 0) & SCTLR_UMA)) {
c2b820fe
PM
4237 return CP_ACCESS_TRAP;
4238 }
4239 return CP_ACCESS_OK;
4240}
4241
4242static void aa64_daif_write(CPUARMState *env, const ARMCPRegInfo *ri,
4243 uint64_t value)
4244{
4245 env->daif = value & PSTATE_DAIF;
4246}
4247
220f508f
RH
4248static uint64_t aa64_pan_read(CPUARMState *env, const ARMCPRegInfo *ri)
4249{
4250 return env->pstate & PSTATE_PAN;
4251}
4252
4253static void aa64_pan_write(CPUARMState *env, const ARMCPRegInfo *ri,
4254 uint64_t value)
4255{
4256 env->pstate = (env->pstate & ~PSTATE_PAN) | (value & PSTATE_PAN);
4257}
4258
4259static const ARMCPRegInfo pan_reginfo = {
4260 .name = "PAN", .state = ARM_CP_STATE_AA64,
4261 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 3,
4262 .type = ARM_CP_NO_RAW, .access = PL1_RW,
4263 .readfn = aa64_pan_read, .writefn = aa64_pan_write
4264};
4265
9eeb7a1c
RH
4266static uint64_t aa64_uao_read(CPUARMState *env, const ARMCPRegInfo *ri)
4267{
4268 return env->pstate & PSTATE_UAO;
4269}
4270
4271static void aa64_uao_write(CPUARMState *env, const ARMCPRegInfo *ri,
4272 uint64_t value)
4273{
4274 env->pstate = (env->pstate & ~PSTATE_UAO) | (value & PSTATE_UAO);
4275}
4276
4277static const ARMCPRegInfo uao_reginfo = {
4278 .name = "UAO", .state = ARM_CP_STATE_AA64,
4279 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 4,
4280 .type = ARM_CP_NO_RAW, .access = PL1_RW,
4281 .readfn = aa64_uao_read, .writefn = aa64_uao_write
4282};
4283
8af35c37 4284static CPAccessResult aa64_cacheop_access(CPUARMState *env,
3f208fd7
PM
4285 const ARMCPRegInfo *ri,
4286 bool isread)
8af35c37
PM
4287{
4288 /* Cache invalidate/clean: NOP, but EL0 must UNDEF unless
4289 * SCTLR_EL1.UCI is set.
4290 */
aaec1432 4291 if (arm_current_el(env) == 0 && !(arm_sctlr(env, 0) & SCTLR_UCI)) {
8af35c37
PM
4292 return CP_ACCESS_TRAP;
4293 }
4294 return CP_ACCESS_OK;
4295}
4296
dbb1fb27
AB
4297/* See: D4.7.2 TLB maintenance requirements and the TLB maintenance instructions
4298 * Page D4-1736 (DDI0487A.b)
4299 */
4300
b7e0730d
RH
4301static int vae1_tlbmask(CPUARMState *env)
4302{
85d0dc9f 4303 /* Since we exclude secure first, we may read HCR_EL2 directly. */
b7e0730d 4304 if (arm_is_secure_below_el3(env)) {
452ef8cb
RH
4305 return ARMMMUIdxBit_SE10_1 |
4306 ARMMMUIdxBit_SE10_1_PAN |
4307 ARMMMUIdxBit_SE10_0;
85d0dc9f
RH
4308 } else if ((env->cp15.hcr_el2 & (HCR_E2H | HCR_TGE))
4309 == (HCR_E2H | HCR_TGE)) {
452ef8cb
RH
4310 return ARMMMUIdxBit_E20_2 |
4311 ARMMMUIdxBit_E20_2_PAN |
4312 ARMMMUIdxBit_E20_0;
b7e0730d 4313 } else {
452ef8cb
RH
4314 return ARMMMUIdxBit_E10_1 |
4315 ARMMMUIdxBit_E10_1_PAN |
4316 ARMMMUIdxBit_E10_0;
b7e0730d
RH
4317 }
4318}
4319
fd3ed969
PM
4320static void tlbi_aa64_vmalle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4321 uint64_t value)
168aa23b 4322{
29a0af61 4323 CPUState *cs = env_cpu(env);
b7e0730d 4324 int mask = vae1_tlbmask(env);
dbb1fb27 4325
b7e0730d 4326 tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
168aa23b
PM
4327}
4328
b4ab8ce9
PM
4329static void tlbi_aa64_vmalle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4330 uint64_t value)
4331{
29a0af61 4332 CPUState *cs = env_cpu(env);
b7e0730d 4333 int mask = vae1_tlbmask(env);
b4ab8ce9
PM
4334
4335 if (tlb_force_broadcast(env)) {
527db2be
RH
4336 tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
4337 } else {
4338 tlb_flush_by_mmuidx(cs, mask);
b4ab8ce9 4339 }
b4ab8ce9
PM
4340}
4341
90c19cdf 4342static int alle1_tlbmask(CPUARMState *env)
168aa23b 4343{
90c19cdf
RH
4344 /*
4345 * Note that the 'ALL' scope must invalidate both stage 1 and
fd3ed969
PM
4346 * stage 2 translations, whereas most other scopes only invalidate
4347 * stage 1 translations.
4348 */
fd3ed969 4349 if (arm_is_secure_below_el3(env)) {
452ef8cb
RH
4350 return ARMMMUIdxBit_SE10_1 |
4351 ARMMMUIdxBit_SE10_1_PAN |
4352 ARMMMUIdxBit_SE10_0;
90c19cdf 4353 } else if (arm_feature(env, ARM_FEATURE_EL2)) {
452ef8cb
RH
4354 return ARMMMUIdxBit_E10_1 |
4355 ARMMMUIdxBit_E10_1_PAN |
4356 ARMMMUIdxBit_E10_0 |
4357 ARMMMUIdxBit_Stage2;
fd3ed969 4358 } else {
452ef8cb
RH
4359 return ARMMMUIdxBit_E10_1 |
4360 ARMMMUIdxBit_E10_1_PAN |
4361 ARMMMUIdxBit_E10_0;
fd3ed969 4362 }
168aa23b
PM
4363}
4364
85d0dc9f
RH
4365static int e2_tlbmask(CPUARMState *env)
4366{
4367 /* TODO: ARMv8.4-SecEL2 */
452ef8cb
RH
4368 return ARMMMUIdxBit_E20_0 |
4369 ARMMMUIdxBit_E20_2 |
4370 ARMMMUIdxBit_E20_2_PAN |
4371 ARMMMUIdxBit_E2;
85d0dc9f
RH
4372}
4373
90c19cdf
RH
4374static void tlbi_aa64_alle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4375 uint64_t value)
4376{
4377 CPUState *cs = env_cpu(env);
4378 int mask = alle1_tlbmask(env);
4379
4380 tlb_flush_by_mmuidx(cs, mask);
4381}
4382
fd3ed969 4383static void tlbi_aa64_alle2_write(CPUARMState *env, const ARMCPRegInfo *ri,
fa439fc5
PM
4384 uint64_t value)
4385{
85d0dc9f
RH
4386 CPUState *cs = env_cpu(env);
4387 int mask = e2_tlbmask(env);
fd3ed969 4388
85d0dc9f 4389 tlb_flush_by_mmuidx(cs, mask);
fd3ed969
PM
4390}
4391
43efaa33
PM
4392static void tlbi_aa64_alle3_write(CPUARMState *env, const ARMCPRegInfo *ri,
4393 uint64_t value)
4394{
2fc0cc0e 4395 ARMCPU *cpu = env_archcpu(env);
43efaa33
PM
4396 CPUState *cs = CPU(cpu);
4397
127b2b08 4398 tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_SE3);
43efaa33
PM
4399}
4400
fd3ed969
PM
4401static void tlbi_aa64_alle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4402 uint64_t value)
4403{
29a0af61 4404 CPUState *cs = env_cpu(env);
90c19cdf
RH
4405 int mask = alle1_tlbmask(env);
4406
4407 tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
fa439fc5
PM
4408}
4409
2bfb9d75
PM
4410static void tlbi_aa64_alle2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4411 uint64_t value)
4412{
29a0af61 4413 CPUState *cs = env_cpu(env);
85d0dc9f 4414 int mask = e2_tlbmask(env);
2bfb9d75 4415
85d0dc9f 4416 tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
2bfb9d75
PM
4417}
4418
43efaa33
PM
4419static void tlbi_aa64_alle3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4420 uint64_t value)
4421{
29a0af61 4422 CPUState *cs = env_cpu(env);
43efaa33 4423
127b2b08 4424 tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_SE3);
43efaa33
PM
4425}
4426
fd3ed969
PM
4427static void tlbi_aa64_vae2_write(CPUARMState *env, const ARMCPRegInfo *ri,
4428 uint64_t value)
fa439fc5 4429{
fd3ed969
PM
4430 /* Invalidate by VA, EL2
4431 * Currently handles both VAE2 and VALE2, since we don't support
4432 * flush-last-level-only.
4433 */
85d0dc9f
RH
4434 CPUState *cs = env_cpu(env);
4435 int mask = e2_tlbmask(env);
fd3ed969
PM
4436 uint64_t pageaddr = sextract64(value << 12, 0, 56);
4437
85d0dc9f 4438 tlb_flush_page_by_mmuidx(cs, pageaddr, mask);
fd3ed969
PM
4439}
4440
43efaa33
PM
4441static void tlbi_aa64_vae3_write(CPUARMState *env, const ARMCPRegInfo *ri,
4442 uint64_t value)
4443{
4444 /* Invalidate by VA, EL3
4445 * Currently handles both VAE3 and VALE3, since we don't support
4446 * flush-last-level-only.
4447 */
2fc0cc0e 4448 ARMCPU *cpu = env_archcpu(env);
43efaa33
PM
4449 CPUState *cs = CPU(cpu);
4450 uint64_t pageaddr = sextract64(value << 12, 0, 56);
4451
127b2b08 4452 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_SE3);
43efaa33
PM
4453}
4454
fd3ed969
PM
4455static void tlbi_aa64_vae1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4456 uint64_t value)
4457{
90c19cdf
RH
4458 CPUState *cs = env_cpu(env);
4459 int mask = vae1_tlbmask(env);
fa439fc5
PM
4460 uint64_t pageaddr = sextract64(value << 12, 0, 56);
4461
90c19cdf 4462 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr, mask);
fa439fc5
PM
4463}
4464
b4ab8ce9
PM
4465static void tlbi_aa64_vae1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4466 uint64_t value)
4467{
4468 /* Invalidate by VA, EL1&0 (AArch64 version).
4469 * Currently handles all of VAE1, VAAE1, VAALE1 and VALE1,
4470 * since we don't support flush-for-specific-ASID-only or
4471 * flush-last-level-only.
4472 */
90c19cdf
RH
4473 CPUState *cs = env_cpu(env);
4474 int mask = vae1_tlbmask(env);
b4ab8ce9
PM
4475 uint64_t pageaddr = sextract64(value << 12, 0, 56);
4476
4477 if (tlb_force_broadcast(env)) {
527db2be
RH
4478 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr, mask);
4479 } else {
4480 tlb_flush_page_by_mmuidx(cs, pageaddr, mask);
b4ab8ce9 4481 }
b4ab8ce9
PM
4482}
4483
fd3ed969
PM
4484static void tlbi_aa64_vae2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4485 uint64_t value)
fa439fc5 4486{
29a0af61 4487 CPUState *cs = env_cpu(env);
fd3ed969 4488 uint64_t pageaddr = sextract64(value << 12, 0, 56);
fa439fc5 4489
a67cf277 4490 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
e013b741 4491 ARMMMUIdxBit_E2);
fa439fc5
PM
4492}
4493
43efaa33
PM
4494static void tlbi_aa64_vae3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4495 uint64_t value)
4496{
29a0af61 4497 CPUState *cs = env_cpu(env);
43efaa33
PM
4498 uint64_t pageaddr = sextract64(value << 12, 0, 56);
4499
a67cf277 4500 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
127b2b08 4501 ARMMMUIdxBit_SE3);
43efaa33
PM
4502}
4503
cea66e91
PM
4504static void tlbi_aa64_ipas2e1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4505 uint64_t value)
4506{
4507 /* Invalidate by IPA. This has to invalidate any structures that
4508 * contain only stage 2 translation information, but does not need
4509 * to apply to structures that contain combined stage 1 and stage 2
4510 * translation information.
4511 * This must NOP if EL2 isn't implemented or SCR_EL3.NS is zero.
4512 */
2fc0cc0e 4513 ARMCPU *cpu = env_archcpu(env);
cea66e91
PM
4514 CPUState *cs = CPU(cpu);
4515 uint64_t pageaddr;
4516
4517 if (!arm_feature(env, ARM_FEATURE_EL2) || !(env->cp15.scr_el3 & SCR_NS)) {
4518 return;
4519 }
4520
4521 pageaddr = sextract64(value << 12, 0, 48);
4522
97fa9350 4523 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_Stage2);
cea66e91
PM
4524}
4525
4526static void tlbi_aa64_ipas2e1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4527 uint64_t value)
4528{
29a0af61 4529 CPUState *cs = env_cpu(env);
cea66e91
PM
4530 uint64_t pageaddr;
4531
4532 if (!arm_feature(env, ARM_FEATURE_EL2) || !(env->cp15.scr_el3 & SCR_NS)) {
4533 return;
4534 }
4535
4536 pageaddr = sextract64(value << 12, 0, 48);
4537
a67cf277 4538 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
97fa9350 4539 ARMMMUIdxBit_Stage2);
cea66e91
PM
4540}
4541
3f208fd7
PM
4542static CPAccessResult aa64_zva_access(CPUARMState *env, const ARMCPRegInfo *ri,
4543 bool isread)
aca3f40b 4544{
4351cb72
RH
4545 int cur_el = arm_current_el(env);
4546
4547 if (cur_el < 2) {
4548 uint64_t hcr = arm_hcr_el2_eff(env);
4549
4550 if (cur_el == 0) {
4551 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
4552 if (!(env->cp15.sctlr_el[2] & SCTLR_DZE)) {
4553 return CP_ACCESS_TRAP_EL2;
4554 }
4555 } else {
4556 if (!(env->cp15.sctlr_el[1] & SCTLR_DZE)) {
4557 return CP_ACCESS_TRAP;
4558 }
4559 if (hcr & HCR_TDZ) {
4560 return CP_ACCESS_TRAP_EL2;
4561 }
4562 }
4563 } else if (hcr & HCR_TDZ) {
4564 return CP_ACCESS_TRAP_EL2;
4565 }
aca3f40b
PM
4566 }
4567 return CP_ACCESS_OK;
4568}
4569
4570static uint64_t aa64_dczid_read(CPUARMState *env, const ARMCPRegInfo *ri)
4571{
2fc0cc0e 4572 ARMCPU *cpu = env_archcpu(env);
aca3f40b
PM
4573 int dzp_bit = 1 << 4;
4574
4575 /* DZP indicates whether DC ZVA access is allowed */
3f208fd7 4576 if (aa64_zva_access(env, NULL, false) == CP_ACCESS_OK) {
aca3f40b
PM
4577 dzp_bit = 0;
4578 }
4579 return cpu->dcz_blocksize | dzp_bit;
4580}
4581
3f208fd7
PM
4582static CPAccessResult sp_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
4583 bool isread)
f502cfc2 4584{
cdcf1405 4585 if (!(env->pstate & PSTATE_SP)) {
f502cfc2
PM
4586 /* Access to SP_EL0 is undefined if it's being used as
4587 * the stack pointer.
4588 */
4589 return CP_ACCESS_TRAP_UNCATEGORIZED;
4590 }
4591 return CP_ACCESS_OK;
4592}
4593
4594static uint64_t spsel_read(CPUARMState *env, const ARMCPRegInfo *ri)
4595{
4596 return env->pstate & PSTATE_SP;
4597}
4598
4599static void spsel_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
4600{
4601 update_spsel(env, val);
4602}
4603
137feaa9
FA
4604static void sctlr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4605 uint64_t value)
4606{
2fc0cc0e 4607 ARMCPU *cpu = env_archcpu(env);
137feaa9
FA
4608
4609 if (raw_read(env, ri) == value) {
4610 /* Skip the TLB flush if nothing actually changed; Linux likes
4611 * to do a lot of pointless SCTLR writes.
4612 */
4613 return;
4614 }
4615
06312feb
PM
4616 if (arm_feature(env, ARM_FEATURE_PMSA) && !cpu->has_mpu) {
4617 /* M bit is RAZ/WI for PMSA with no MPU implemented */
4618 value &= ~SCTLR_M;
4619 }
4620
137feaa9
FA
4621 raw_write(env, ri, value);
4622 /* ??? Lots of these bits are not implemented. */
4623 /* This may enable/disable the MMU, so do a TLB flush. */
d10eb08f 4624 tlb_flush(CPU(cpu));
2e5dcf36
RH
4625
4626 if (ri->type & ARM_CP_SUPPRESS_TB_END) {
4627 /*
4628 * Normally we would always end the TB on an SCTLR write; see the
4629 * comment in ARMCPRegInfo sctlr initialization below for why Xscale
4630 * is special. Setting ARM_CP_SUPPRESS_TB_END also stops the rebuild
4631 * of hflags from the translator, so do it here.
4632 */
4633 arm_rebuild_hflags(env);
4634 }
137feaa9
FA
4635}
4636
3f208fd7
PM
4637static CPAccessResult fpexc32_access(CPUARMState *env, const ARMCPRegInfo *ri,
4638 bool isread)
03fbf20f
PM
4639{
4640 if ((env->cp15.cptr_el[2] & CPTR_TFP) && arm_current_el(env) == 2) {
f2cae609 4641 return CP_ACCESS_TRAP_FP_EL2;
03fbf20f
PM
4642 }
4643 if (env->cp15.cptr_el[3] & CPTR_TFP) {
f2cae609 4644 return CP_ACCESS_TRAP_FP_EL3;
03fbf20f
PM
4645 }
4646 return CP_ACCESS_OK;
4647}
4648
a8d64e73
PM
4649static void sdcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4650 uint64_t value)
4651{
4652 env->cp15.mdcr_el3 = value & SDCR_VALID_MASK;
4653}
4654
b0d2b7d0
PM
4655static const ARMCPRegInfo v8_cp_reginfo[] = {
4656 /* Minimal set of EL0-visible registers. This will need to be expanded
4657 * significantly for system emulation of AArch64 CPUs.
4658 */
4659 { .name = "NZCV", .state = ARM_CP_STATE_AA64,
4660 .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 2,
4661 .access = PL0_RW, .type = ARM_CP_NZCV },
c2b820fe
PM
4662 { .name = "DAIF", .state = ARM_CP_STATE_AA64,
4663 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 2,
7a0e58fa 4664 .type = ARM_CP_NO_RAW,
c2b820fe
PM
4665 .access = PL0_RW, .accessfn = aa64_daif_access,
4666 .fieldoffset = offsetof(CPUARMState, daif),
4667 .writefn = aa64_daif_write, .resetfn = arm_cp_reset_ignore },
b0d2b7d0
PM
4668 { .name = "FPCR", .state = ARM_CP_STATE_AA64,
4669 .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 4,
b916c9c3 4670 .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END,
fe03d45f 4671 .readfn = aa64_fpcr_read, .writefn = aa64_fpcr_write },
b0d2b7d0
PM
4672 { .name = "FPSR", .state = ARM_CP_STATE_AA64,
4673 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 4,
b916c9c3 4674 .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END,
fe03d45f 4675 .readfn = aa64_fpsr_read, .writefn = aa64_fpsr_write },
b0d2b7d0
PM
4676 { .name = "DCZID_EL0", .state = ARM_CP_STATE_AA64,
4677 .opc0 = 3, .opc1 = 3, .opc2 = 7, .crn = 0, .crm = 0,
7a0e58fa 4678 .access = PL0_R, .type = ARM_CP_NO_RAW,
aca3f40b
PM
4679 .readfn = aa64_dczid_read },
4680 { .name = "DC_ZVA", .state = ARM_CP_STATE_AA64,
4681 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 1,
4682 .access = PL0_W, .type = ARM_CP_DC_ZVA,
4683#ifndef CONFIG_USER_ONLY
4684 /* Avoid overhead of an access check that always passes in user-mode */
4685 .accessfn = aa64_zva_access,
4686#endif
4687 },
0eef9d98
PM
4688 { .name = "CURRENTEL", .state = ARM_CP_STATE_AA64,
4689 .opc0 = 3, .opc1 = 0, .opc2 = 2, .crn = 4, .crm = 2,
4690 .access = PL1_R, .type = ARM_CP_CURRENTEL },
8af35c37
PM
4691 /* Cache ops: all NOPs since we don't emulate caches */
4692 { .name = "IC_IALLUIS", .state = ARM_CP_STATE_AA64,
4693 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
4694 .access = PL1_W, .type = ARM_CP_NOP },
4695 { .name = "IC_IALLU", .state = ARM_CP_STATE_AA64,
4696 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
4697 .access = PL1_W, .type = ARM_CP_NOP },
4698 { .name = "IC_IVAU", .state = ARM_CP_STATE_AA64,
4699 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 5, .opc2 = 1,
4700 .access = PL0_W, .type = ARM_CP_NOP,
4701 .accessfn = aa64_cacheop_access },
4702 { .name = "DC_IVAC", .state = ARM_CP_STATE_AA64,
4703 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
4704 .access = PL1_W, .type = ARM_CP_NOP },
4705 { .name = "DC_ISW", .state = ARM_CP_STATE_AA64,
4706 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
4707 .access = PL1_W, .type = ARM_CP_NOP },
4708 { .name = "DC_CVAC", .state = ARM_CP_STATE_AA64,
4709 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 1,
4710 .access = PL0_W, .type = ARM_CP_NOP,
4711 .accessfn = aa64_cacheop_access },
4712 { .name = "DC_CSW", .state = ARM_CP_STATE_AA64,
4713 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
4714 .access = PL1_W, .type = ARM_CP_NOP },
4715 { .name = "DC_CVAU", .state = ARM_CP_STATE_AA64,
4716 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 11, .opc2 = 1,
4717 .access = PL0_W, .type = ARM_CP_NOP,
4718 .accessfn = aa64_cacheop_access },
4719 { .name = "DC_CIVAC", .state = ARM_CP_STATE_AA64,
4720 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 1,
4721 .access = PL0_W, .type = ARM_CP_NOP,
4722 .accessfn = aa64_cacheop_access },
4723 { .name = "DC_CISW", .state = ARM_CP_STATE_AA64,
4724 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
4725 .access = PL1_W, .type = ARM_CP_NOP },
168aa23b
PM
4726 /* TLBI operations */
4727 { .name = "TLBI_VMALLE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 4728 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
7a0e58fa 4729 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 4730 .writefn = tlbi_aa64_vmalle1is_write },
168aa23b 4731 { .name = "TLBI_VAE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 4732 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
7a0e58fa 4733 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 4734 .writefn = tlbi_aa64_vae1is_write },
168aa23b 4735 { .name = "TLBI_ASIDE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 4736 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
7a0e58fa 4737 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 4738 .writefn = tlbi_aa64_vmalle1is_write },
168aa23b 4739 { .name = "TLBI_VAAE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 4740 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
7a0e58fa 4741 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 4742 .writefn = tlbi_aa64_vae1is_write },
168aa23b 4743 { .name = "TLBI_VALE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 4744 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
7a0e58fa 4745 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 4746 .writefn = tlbi_aa64_vae1is_write },
168aa23b 4747 { .name = "TLBI_VAALE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 4748 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
7a0e58fa 4749 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 4750 .writefn = tlbi_aa64_vae1is_write },
168aa23b 4751 { .name = "TLBI_VMALLE1", .state = ARM_CP_STATE_AA64,
6ab9f499 4752 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
7a0e58fa 4753 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 4754 .writefn = tlbi_aa64_vmalle1_write },
168aa23b 4755 { .name = "TLBI_VAE1", .state = ARM_CP_STATE_AA64,
6ab9f499 4756 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
7a0e58fa 4757 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 4758 .writefn = tlbi_aa64_vae1_write },
168aa23b 4759 { .name = "TLBI_ASIDE1", .state = ARM_CP_STATE_AA64,
6ab9f499 4760 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
7a0e58fa 4761 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 4762 .writefn = tlbi_aa64_vmalle1_write },
168aa23b 4763 { .name = "TLBI_VAAE1", .state = ARM_CP_STATE_AA64,
6ab9f499 4764 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
7a0e58fa 4765 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 4766 .writefn = tlbi_aa64_vae1_write },
168aa23b 4767 { .name = "TLBI_VALE1", .state = ARM_CP_STATE_AA64,
6ab9f499 4768 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
7a0e58fa 4769 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 4770 .writefn = tlbi_aa64_vae1_write },
168aa23b 4771 { .name = "TLBI_VAALE1", .state = ARM_CP_STATE_AA64,
6ab9f499 4772 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
7a0e58fa 4773 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 4774 .writefn = tlbi_aa64_vae1_write },
cea66e91
PM
4775 { .name = "TLBI_IPAS2E1IS", .state = ARM_CP_STATE_AA64,
4776 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1,
4777 .access = PL2_W, .type = ARM_CP_NO_RAW,
4778 .writefn = tlbi_aa64_ipas2e1is_write },
4779 { .name = "TLBI_IPAS2LE1IS", .state = ARM_CP_STATE_AA64,
4780 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5,
4781 .access = PL2_W, .type = ARM_CP_NO_RAW,
4782 .writefn = tlbi_aa64_ipas2e1is_write },
83ddf975
PM
4783 { .name = "TLBI_ALLE1IS", .state = ARM_CP_STATE_AA64,
4784 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
4785 .access = PL2_W, .type = ARM_CP_NO_RAW,
fd3ed969 4786 .writefn = tlbi_aa64_alle1is_write },
43efaa33
PM
4787 { .name = "TLBI_VMALLS12E1IS", .state = ARM_CP_STATE_AA64,
4788 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 6,
4789 .access = PL2_W, .type = ARM_CP_NO_RAW,
4790 .writefn = tlbi_aa64_alle1is_write },
cea66e91
PM
4791 { .name = "TLBI_IPAS2E1", .state = ARM_CP_STATE_AA64,
4792 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1,
4793 .access = PL2_W, .type = ARM_CP_NO_RAW,
4794 .writefn = tlbi_aa64_ipas2e1_write },
4795 { .name = "TLBI_IPAS2LE1", .state = ARM_CP_STATE_AA64,
4796 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5,
4797 .access = PL2_W, .type = ARM_CP_NO_RAW,
4798 .writefn = tlbi_aa64_ipas2e1_write },
83ddf975
PM
4799 { .name = "TLBI_ALLE1", .state = ARM_CP_STATE_AA64,
4800 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
4801 .access = PL2_W, .type = ARM_CP_NO_RAW,
fd3ed969 4802 .writefn = tlbi_aa64_alle1_write },
43efaa33
PM
4803 { .name = "TLBI_VMALLS12E1", .state = ARM_CP_STATE_AA64,
4804 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 6,
4805 .access = PL2_W, .type = ARM_CP_NO_RAW,
4806 .writefn = tlbi_aa64_alle1is_write },
19525524
PM
4807#ifndef CONFIG_USER_ONLY
4808 /* 64 bit address translation operations */
4809 { .name = "AT_S1E1R", .state = ARM_CP_STATE_AA64,
4810 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 0,
0710b2fa
PM
4811 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4812 .writefn = ats_write64 },
19525524
PM
4813 { .name = "AT_S1E1W", .state = ARM_CP_STATE_AA64,
4814 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 1,
0710b2fa
PM
4815 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4816 .writefn = ats_write64 },
19525524
PM
4817 { .name = "AT_S1E0R", .state = ARM_CP_STATE_AA64,
4818 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 2,
0710b2fa
PM
4819 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4820 .writefn = ats_write64 },
19525524
PM
4821 { .name = "AT_S1E0W", .state = ARM_CP_STATE_AA64,
4822 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 3,
0710b2fa
PM
4823 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4824 .writefn = ats_write64 },
2a47df95 4825 { .name = "AT_S12E1R", .state = ARM_CP_STATE_AA64,
7a379c7e 4826 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 4,
0710b2fa
PM
4827 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4828 .writefn = ats_write64 },
2a47df95 4829 { .name = "AT_S12E1W", .state = ARM_CP_STATE_AA64,
7a379c7e 4830 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 5,
0710b2fa
PM
4831 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4832 .writefn = ats_write64 },
2a47df95 4833 { .name = "AT_S12E0R", .state = ARM_CP_STATE_AA64,
7a379c7e 4834 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 6,
0710b2fa
PM
4835 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4836 .writefn = ats_write64 },
2a47df95 4837 { .name = "AT_S12E0W", .state = ARM_CP_STATE_AA64,
7a379c7e 4838 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 7,
0710b2fa
PM
4839 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4840 .writefn = ats_write64 },
2a47df95
PM
4841 /* AT S1E2* are elsewhere as they UNDEF from EL3 if EL2 is not present */
4842 { .name = "AT_S1E3R", .state = ARM_CP_STATE_AA64,
4843 .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 0,
0710b2fa
PM
4844 .access = PL3_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4845 .writefn = ats_write64 },
2a47df95
PM
4846 { .name = "AT_S1E3W", .state = ARM_CP_STATE_AA64,
4847 .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 1,
0710b2fa
PM
4848 .access = PL3_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4849 .writefn = ats_write64 },
c96fc9b5
EI
4850 { .name = "PAR_EL1", .state = ARM_CP_STATE_AA64,
4851 .type = ARM_CP_ALIAS,
4852 .opc0 = 3, .opc1 = 0, .crn = 7, .crm = 4, .opc2 = 0,
4853 .access = PL1_RW, .resetvalue = 0,
4854 .fieldoffset = offsetof(CPUARMState, cp15.par_el[1]),
4855 .writefn = par_write },
19525524 4856#endif
995939a6 4857 /* TLB invalidate last level of translation table walk */
9449fdf6 4858 { .name = "TLBIMVALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
7a0e58fa 4859 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_is_write },
9449fdf6 4860 { .name = "TLBIMVAALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
7a0e58fa 4861 .type = ARM_CP_NO_RAW, .access = PL1_W,
fa439fc5 4862 .writefn = tlbimvaa_is_write },
9449fdf6 4863 { .name = "TLBIMVAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
7a0e58fa 4864 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write },
9449fdf6 4865 { .name = "TLBIMVAAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
7a0e58fa 4866 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimvaa_write },
541ef8c2
SS
4867 { .name = "TLBIMVALH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
4868 .type = ARM_CP_NO_RAW, .access = PL2_W,
4869 .writefn = tlbimva_hyp_write },
4870 { .name = "TLBIMVALHIS",
4871 .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
4872 .type = ARM_CP_NO_RAW, .access = PL2_W,
4873 .writefn = tlbimva_hyp_is_write },
4874 { .name = "TLBIIPAS2",
4875 .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1,
4876 .type = ARM_CP_NO_RAW, .access = PL2_W,
4877 .writefn = tlbiipas2_write },
4878 { .name = "TLBIIPAS2IS",
4879 .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1,
4880 .type = ARM_CP_NO_RAW, .access = PL2_W,
4881 .writefn = tlbiipas2_is_write },
4882 { .name = "TLBIIPAS2L",
4883 .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5,
4884 .type = ARM_CP_NO_RAW, .access = PL2_W,
4885 .writefn = tlbiipas2_write },
4886 { .name = "TLBIIPAS2LIS",
4887 .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5,
4888 .type = ARM_CP_NO_RAW, .access = PL2_W,
4889 .writefn = tlbiipas2_is_write },
9449fdf6
PM
4890 /* 32 bit cache operations */
4891 { .name = "ICIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
4892 .type = ARM_CP_NOP, .access = PL1_W },
4893 { .name = "BPIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 6,
4894 .type = ARM_CP_NOP, .access = PL1_W },
4895 { .name = "ICIALLU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
4896 .type = ARM_CP_NOP, .access = PL1_W },
4897 { .name = "ICIMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 1,
4898 .type = ARM_CP_NOP, .access = PL1_W },
4899 { .name = "BPIALL", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 6,
4900 .type = ARM_CP_NOP, .access = PL1_W },
4901 { .name = "BPIMVA", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 7,
4902 .type = ARM_CP_NOP, .access = PL1_W },
4903 { .name = "DCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
4904 .type = ARM_CP_NOP, .access = PL1_W },
4905 { .name = "DCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
4906 .type = ARM_CP_NOP, .access = PL1_W },
4907 { .name = "DCCMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 1,
4908 .type = ARM_CP_NOP, .access = PL1_W },
4909 { .name = "DCCSW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
4910 .type = ARM_CP_NOP, .access = PL1_W },
4911 { .name = "DCCMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 11, .opc2 = 1,
4912 .type = ARM_CP_NOP, .access = PL1_W },
4913 { .name = "DCCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 1,
4914 .type = ARM_CP_NOP, .access = PL1_W },
4915 { .name = "DCCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
4916 .type = ARM_CP_NOP, .access = PL1_W },
4917 /* MMU Domain access control / MPU write buffer control */
0c17d68c 4918 { .name = "DACR", .cp = 15, .opc1 = 0, .crn = 3, .crm = 0, .opc2 = 0,
84929218 4919 .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
0c17d68c
FA
4920 .writefn = dacr_write, .raw_writefn = raw_write,
4921 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
4922 offsetoflow32(CPUARMState, cp15.dacr_ns) } },
a0618a19 4923 { .name = "ELR_EL1", .state = ARM_CP_STATE_AA64,
7a0e58fa 4924 .type = ARM_CP_ALIAS,
a0618a19 4925 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 1,
6947f059
EI
4926 .access = PL1_RW,
4927 .fieldoffset = offsetof(CPUARMState, elr_el[1]) },
a65f1de9 4928 { .name = "SPSR_EL1", .state = ARM_CP_STATE_AA64,
7a0e58fa 4929 .type = ARM_CP_ALIAS,
a65f1de9 4930 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 0,
99a99c1f
SB
4931 .access = PL1_RW,
4932 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_SVC]) },
f502cfc2
PM
4933 /* We rely on the access checks not allowing the guest to write to the
4934 * state field when SPSel indicates that it's being used as the stack
4935 * pointer.
4936 */
4937 { .name = "SP_EL0", .state = ARM_CP_STATE_AA64,
4938 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 1, .opc2 = 0,
4939 .access = PL1_RW, .accessfn = sp_el0_access,
7a0e58fa 4940 .type = ARM_CP_ALIAS,
f502cfc2 4941 .fieldoffset = offsetof(CPUARMState, sp_el[0]) },
884b4dee
GB
4942 { .name = "SP_EL1", .state = ARM_CP_STATE_AA64,
4943 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 1, .opc2 = 0,
7a0e58fa 4944 .access = PL2_RW, .type = ARM_CP_ALIAS,
884b4dee 4945 .fieldoffset = offsetof(CPUARMState, sp_el[1]) },
f502cfc2
PM
4946 { .name = "SPSel", .state = ARM_CP_STATE_AA64,
4947 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 0,
7a0e58fa 4948 .type = ARM_CP_NO_RAW,
f502cfc2 4949 .access = PL1_RW, .readfn = spsel_read, .writefn = spsel_write },
03fbf20f
PM
4950 { .name = "FPEXC32_EL2", .state = ARM_CP_STATE_AA64,
4951 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 3, .opc2 = 0,
4952 .type = ARM_CP_ALIAS,
4953 .fieldoffset = offsetof(CPUARMState, vfp.xregs[ARM_VFP_FPEXC]),
4954 .access = PL2_RW, .accessfn = fpexc32_access },
6a43e0b6
PM
4955 { .name = "DACR32_EL2", .state = ARM_CP_STATE_AA64,
4956 .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 0, .opc2 = 0,
4957 .access = PL2_RW, .resetvalue = 0,
4958 .writefn = dacr_write, .raw_writefn = raw_write,
4959 .fieldoffset = offsetof(CPUARMState, cp15.dacr32_el2) },
4960 { .name = "IFSR32_EL2", .state = ARM_CP_STATE_AA64,
4961 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 0, .opc2 = 1,
4962 .access = PL2_RW, .resetvalue = 0,
4963 .fieldoffset = offsetof(CPUARMState, cp15.ifsr32_el2) },
4964 { .name = "SPSR_IRQ", .state = ARM_CP_STATE_AA64,
4965 .type = ARM_CP_ALIAS,
4966 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 0,
4967 .access = PL2_RW,
4968 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_IRQ]) },
4969 { .name = "SPSR_ABT", .state = ARM_CP_STATE_AA64,
4970 .type = ARM_CP_ALIAS,
4971 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 1,
4972 .access = PL2_RW,
4973 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_ABT]) },
4974 { .name = "SPSR_UND", .state = ARM_CP_STATE_AA64,
4975 .type = ARM_CP_ALIAS,
4976 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 2,
4977 .access = PL2_RW,
4978 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_UND]) },
4979 { .name = "SPSR_FIQ", .state = ARM_CP_STATE_AA64,
4980 .type = ARM_CP_ALIAS,
4981 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 3,
4982 .access = PL2_RW,
4983 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_FIQ]) },
a8d64e73
PM
4984 { .name = "MDCR_EL3", .state = ARM_CP_STATE_AA64,
4985 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 3, .opc2 = 1,
4986 .resetvalue = 0,
4987 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el3) },
4988 { .name = "SDCR", .type = ARM_CP_ALIAS,
4989 .cp = 15, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 1,
4990 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
4991 .writefn = sdcr_write,
4992 .fieldoffset = offsetoflow32(CPUARMState, cp15.mdcr_el3) },
b0d2b7d0
PM
4993 REGINFO_SENTINEL
4994};
4995
d42e3c26 4996/* Used to describe the behaviour of EL2 regs when EL2 does not exist. */
4771cd01 4997static const ARMCPRegInfo el3_no_el2_cp_reginfo[] = {
d79e0c06 4998 { .name = "VBAR_EL2", .state = ARM_CP_STATE_BOTH,
d42e3c26
EI
4999 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0,
5000 .access = PL2_RW,
5001 .readfn = arm_cp_read_zero, .writefn = arm_cp_write_ignore },
ce4afed8 5002 { .name = "HCR_EL2", .state = ARM_CP_STATE_BOTH,
7a0e58fa 5003 .type = ARM_CP_NO_RAW,
f149e3e8
EI
5004 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
5005 .access = PL2_RW,
ce4afed8 5006 .type = ARM_CP_CONST, .resetvalue = 0 },
831a2fca
PM
5007 { .name = "HACR_EL2", .state = ARM_CP_STATE_BOTH,
5008 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 7,
5009 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
68e78e33
PM
5010 { .name = "ESR_EL2", .state = ARM_CP_STATE_BOTH,
5011 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 0,
5012 .access = PL2_RW,
5013 .type = ARM_CP_CONST, .resetvalue = 0 },
c6f19164
GB
5014 { .name = "CPTR_EL2", .state = ARM_CP_STATE_BOTH,
5015 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 2,
5016 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
95f949ac
EI
5017 { .name = "MAIR_EL2", .state = ARM_CP_STATE_BOTH,
5018 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 0,
5019 .access = PL2_RW, .type = ARM_CP_CONST,
5020 .resetvalue = 0 },
5021 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
b5ede85b 5022 .cp = 15, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 1,
95f949ac 5023 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
2179ef95
PM
5024 { .name = "AMAIR_EL2", .state = ARM_CP_STATE_BOTH,
5025 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 0,
5026 .access = PL2_RW, .type = ARM_CP_CONST,
5027 .resetvalue = 0 },
55b53c71 5028 { .name = "HAMAIR1", .state = ARM_CP_STATE_AA32,
b5ede85b 5029 .cp = 15, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 1,
2179ef95
PM
5030 .access = PL2_RW, .type = ARM_CP_CONST,
5031 .resetvalue = 0 },
37cd6c24
PM
5032 { .name = "AFSR0_EL2", .state = ARM_CP_STATE_BOTH,
5033 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 0,
5034 .access = PL2_RW, .type = ARM_CP_CONST,
5035 .resetvalue = 0 },
5036 { .name = "AFSR1_EL2", .state = ARM_CP_STATE_BOTH,
5037 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 1,
5038 .access = PL2_RW, .type = ARM_CP_CONST,
5039 .resetvalue = 0 },
06ec4c8c
EI
5040 { .name = "TCR_EL2", .state = ARM_CP_STATE_BOTH,
5041 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 2,
5042 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
68e9c2fe
EI
5043 { .name = "VTCR_EL2", .state = ARM_CP_STATE_BOTH,
5044 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
5045 .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any,
5046 .type = ARM_CP_CONST, .resetvalue = 0 },
b698e9cf
EI
5047 { .name = "VTTBR", .state = ARM_CP_STATE_AA32,
5048 .cp = 15, .opc1 = 6, .crm = 2,
5049 .access = PL2_RW, .accessfn = access_el3_aa32ns,
5050 .type = ARM_CP_CONST | ARM_CP_64BIT, .resetvalue = 0 },
5051 { .name = "VTTBR_EL2", .state = ARM_CP_STATE_AA64,
5052 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 0,
5053 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
b9cb5323
EI
5054 { .name = "SCTLR_EL2", .state = ARM_CP_STATE_BOTH,
5055 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 0,
5056 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
ff05f37b
EI
5057 { .name = "TPIDR_EL2", .state = ARM_CP_STATE_BOTH,
5058 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 2,
5059 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
a57633c0
EI
5060 { .name = "TTBR0_EL2", .state = ARM_CP_STATE_AA64,
5061 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
5062 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
5063 { .name = "HTTBR", .cp = 15, .opc1 = 4, .crm = 2,
5064 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_CONST,
5065 .resetvalue = 0 },
0b6440af
EI
5066 { .name = "CNTHCTL_EL2", .state = ARM_CP_STATE_BOTH,
5067 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 1, .opc2 = 0,
5068 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
edac4d8a
EI
5069 { .name = "CNTVOFF_EL2", .state = ARM_CP_STATE_AA64,
5070 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 3,
5071 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
5072 { .name = "CNTVOFF", .cp = 15, .opc1 = 4, .crm = 14,
5073 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_CONST,
5074 .resetvalue = 0 },
b0e66d95
EI
5075 { .name = "CNTHP_CVAL_EL2", .state = ARM_CP_STATE_AA64,
5076 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 2,
5077 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
5078 { .name = "CNTHP_CVAL", .cp = 15, .opc1 = 6, .crm = 14,
5079 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_CONST,
5080 .resetvalue = 0 },
5081 { .name = "CNTHP_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
5082 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 0,
5083 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
5084 { .name = "CNTHP_CTL_EL2", .state = ARM_CP_STATE_BOTH,
5085 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 1,
5086 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
14cc7b54
SF
5087 { .name = "MDCR_EL2", .state = ARM_CP_STATE_BOTH,
5088 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 1,
d6c8cf81
PM
5089 .access = PL2_RW, .accessfn = access_tda,
5090 .type = ARM_CP_CONST, .resetvalue = 0 },
59e05530
EI
5091 { .name = "HPFAR_EL2", .state = ARM_CP_STATE_BOTH,
5092 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
5093 .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any,
5094 .type = ARM_CP_CONST, .resetvalue = 0 },
2a5a9abd
AF
5095 { .name = "HSTR_EL2", .state = ARM_CP_STATE_BOTH,
5096 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 3,
5097 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
cba517c3
PM
5098 { .name = "FAR_EL2", .state = ARM_CP_STATE_BOTH,
5099 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 0,
5100 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
5101 { .name = "HIFAR", .state = ARM_CP_STATE_AA32,
5102 .type = ARM_CP_CONST,
5103 .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 2,
5104 .access = PL2_RW, .resetvalue = 0 },
d42e3c26
EI
5105 REGINFO_SENTINEL
5106};
5107
ce4afed8
PM
5108/* Ditto, but for registers which exist in ARMv8 but not v7 */
5109static const ARMCPRegInfo el3_no_el2_v8_cp_reginfo[] = {
5110 { .name = "HCR2", .state = ARM_CP_STATE_AA32,
5111 .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 4,
5112 .access = PL2_RW,
5113 .type = ARM_CP_CONST, .resetvalue = 0 },
5114 REGINFO_SENTINEL
5115};
5116
d1fb4da2 5117static void do_hcr_write(CPUARMState *env, uint64_t value, uint64_t valid_mask)
f149e3e8 5118{
2fc0cc0e 5119 ARMCPU *cpu = env_archcpu(env);
d1fb4da2
RH
5120
5121 if (arm_feature(env, ARM_FEATURE_V8)) {
5122 valid_mask |= MAKE_64BIT_MASK(0, 34); /* ARMv8.0 */
5123 } else {
5124 valid_mask |= MAKE_64BIT_MASK(0, 28); /* ARMv7VE */
5125 }
f149e3e8
EI
5126
5127 if (arm_feature(env, ARM_FEATURE_EL3)) {
5128 valid_mask &= ~HCR_HCD;
77077a83
JK
5129 } else if (cpu->psci_conduit != QEMU_PSCI_CONDUIT_SMC) {
5130 /* Architecturally HCR.TSC is RES0 if EL3 is not implemented.
5131 * However, if we're using the SMC PSCI conduit then QEMU is
5132 * effectively acting like EL3 firmware and so the guest at
5133 * EL2 should retain the ability to prevent EL1 from being
5134 * able to make SMC calls into the ersatz firmware, so in
5135 * that case HCR.TSC should be read/write.
5136 */
f149e3e8
EI
5137 valid_mask &= ~HCR_TSC;
5138 }
d1fb4da2
RH
5139
5140 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
5141 if (cpu_isar_feature(aa64_vh, cpu)) {
5142 valid_mask |= HCR_E2H;
5143 }
5144 if (cpu_isar_feature(aa64_lor, cpu)) {
5145 valid_mask |= HCR_TLOR;
5146 }
5147 if (cpu_isar_feature(aa64_pauth, cpu)) {
5148 valid_mask |= HCR_API | HCR_APK;
5149 }
ef682cdb 5150 }
f149e3e8
EI
5151
5152 /* Clear RES0 bits. */
5153 value &= valid_mask;
5154
5155 /* These bits change the MMU setup:
5156 * HCR_VM enables stage 2 translation
5157 * HCR_PTW forbids certain page-table setups
5158 * HCR_DC Disables stage1 and enables stage2 translation
5159 */
ce4afed8 5160 if ((env->cp15.hcr_el2 ^ value) & (HCR_VM | HCR_PTW | HCR_DC)) {
d10eb08f 5161 tlb_flush(CPU(cpu));
f149e3e8 5162 }
ce4afed8 5163 env->cp15.hcr_el2 = value;
89430fc6
PM
5164
5165 /*
5166 * Updates to VI and VF require us to update the status of
5167 * virtual interrupts, which are the logical OR of these bits
5168 * and the state of the input lines from the GIC. (This requires
5169 * that we have the iothread lock, which is done by marking the
5170 * reginfo structs as ARM_CP_IO.)
5171 * Note that if a write to HCR pends a VIRQ or VFIQ it is never
5172 * possible for it to be taken immediately, because VIRQ and
5173 * VFIQ are masked unless running at EL0 or EL1, and HCR
5174 * can only be written at EL2.
5175 */
5176 g_assert(qemu_mutex_iothread_locked());
5177 arm_cpu_update_virq(cpu);
5178 arm_cpu_update_vfiq(cpu);
ce4afed8
PM
5179}
5180
d1fb4da2
RH
5181static void hcr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
5182{
5183 do_hcr_write(env, value, 0);
5184}
5185
ce4afed8
PM
5186static void hcr_writehigh(CPUARMState *env, const ARMCPRegInfo *ri,
5187 uint64_t value)
5188{
5189 /* Handle HCR2 write, i.e. write to high half of HCR_EL2 */
5190 value = deposit64(env->cp15.hcr_el2, 32, 32, value);
d1fb4da2 5191 do_hcr_write(env, value, MAKE_64BIT_MASK(0, 32));
ce4afed8
PM
5192}
5193
5194static void hcr_writelow(CPUARMState *env, const ARMCPRegInfo *ri,
5195 uint64_t value)
5196{
5197 /* Handle HCR write, i.e. write to low half of HCR_EL2 */
5198 value = deposit64(env->cp15.hcr_el2, 0, 32, value);
d1fb4da2 5199 do_hcr_write(env, value, MAKE_64BIT_MASK(32, 32));
f149e3e8
EI
5200}
5201
f7778444
RH
5202/*
5203 * Return the effective value of HCR_EL2.
5204 * Bits that are not included here:
5205 * RW (read from SCR_EL3.RW as needed)
5206 */
5207uint64_t arm_hcr_el2_eff(CPUARMState *env)
5208{
5209 uint64_t ret = env->cp15.hcr_el2;
5210
5211 if (arm_is_secure_below_el3(env)) {
5212 /*
5213 * "This register has no effect if EL2 is not enabled in the
5214 * current Security state". This is ARMv8.4-SecEL2 speak for
5215 * !(SCR_EL3.NS==1 || SCR_EL3.EEL2==1).
5216 *
5217 * Prior to that, the language was "In an implementation that
5218 * includes EL3, when the value of SCR_EL3.NS is 0 the PE behaves
5219 * as if this field is 0 for all purposes other than a direct
5220 * read or write access of HCR_EL2". With lots of enumeration
5221 * on a per-field basis. In current QEMU, this is condition
5222 * is arm_is_secure_below_el3.
5223 *
5224 * Since the v8.4 language applies to the entire register, and
5225 * appears to be backward compatible, use that.
5226 */
4990e1d3
RH
5227 return 0;
5228 }
5229
5230 /*
5231 * For a cpu that supports both aarch64 and aarch32, we can set bits
5232 * in HCR_EL2 (e.g. via EL3) that are RES0 when we enter EL2 as aa32.
5233 * Ignore all of the bits in HCR+HCR2 that are not valid for aarch32.
5234 */
5235 if (!arm_el_is_aa64(env, 2)) {
5236 uint64_t aa32_valid;
5237
5238 /*
5239 * These bits are up-to-date as of ARMv8.6.
5240 * For HCR, it's easiest to list just the 2 bits that are invalid.
5241 * For HCR2, list those that are valid.
5242 */
5243 aa32_valid = MAKE_64BIT_MASK(0, 32) & ~(HCR_RW | HCR_TDZ);
5244 aa32_valid |= (HCR_CD | HCR_ID | HCR_TERR | HCR_TEA | HCR_MIOCNCE |
5245 HCR_TID4 | HCR_TICAB | HCR_TOCU | HCR_TTLBIS);
5246 ret &= aa32_valid;
5247 }
5248
5249 if (ret & HCR_TGE) {
5250 /* These bits are up-to-date as of ARMv8.6. */
f7778444
RH
5251 if (ret & HCR_E2H) {
5252 ret &= ~(HCR_VM | HCR_FMO | HCR_IMO | HCR_AMO |
5253 HCR_BSU_MASK | HCR_DC | HCR_TWI | HCR_TWE |
5254 HCR_TID0 | HCR_TID2 | HCR_TPCP | HCR_TPU |
4990e1d3
RH
5255 HCR_TDZ | HCR_CD | HCR_ID | HCR_MIOCNCE |
5256 HCR_TID4 | HCR_TICAB | HCR_TOCU | HCR_ENSCXT |
5257 HCR_TTLBIS | HCR_TTLBOS | HCR_TID5);
f7778444
RH
5258 } else {
5259 ret |= HCR_FMO | HCR_IMO | HCR_AMO;
5260 }
5261 ret &= ~(HCR_SWIO | HCR_PTW | HCR_VF | HCR_VI | HCR_VSE |
5262 HCR_FB | HCR_TID1 | HCR_TID3 | HCR_TSC | HCR_TACR |
5263 HCR_TSW | HCR_TTLB | HCR_TVM | HCR_HCD | HCR_TRVM |
5264 HCR_TLOR);
5265 }
5266
5267 return ret;
5268}
5269
fc1120a7
PM
5270static void cptr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
5271 uint64_t value)
5272{
5273 /*
5274 * For A-profile AArch32 EL3, if NSACR.CP10
5275 * is 0 then HCPTR.{TCP11,TCP10} ignore writes and read as 1.
5276 */
5277 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
5278 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
5279 value &= ~(0x3 << 10);
5280 value |= env->cp15.cptr_el[2] & (0x3 << 10);
5281 }
5282 env->cp15.cptr_el[2] = value;
5283}
5284
5285static uint64_t cptr_el2_read(CPUARMState *env, const ARMCPRegInfo *ri)
5286{
5287 /*
5288 * For A-profile AArch32 EL3, if NSACR.CP10
5289 * is 0 then HCPTR.{TCP11,TCP10} ignore writes and read as 1.
5290 */
5291 uint64_t value = env->cp15.cptr_el[2];
5292
5293 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
5294 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
5295 value |= 0x3 << 10;
5296 }
5297 return value;
5298}
5299
4771cd01 5300static const ARMCPRegInfo el2_cp_reginfo[] = {
f149e3e8 5301 { .name = "HCR_EL2", .state = ARM_CP_STATE_AA64,
89430fc6 5302 .type = ARM_CP_IO,
f149e3e8
EI
5303 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
5304 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
c624ea0f 5305 .writefn = hcr_write },
ce4afed8 5306 { .name = "HCR", .state = ARM_CP_STATE_AA32,
89430fc6 5307 .type = ARM_CP_ALIAS | ARM_CP_IO,
ce4afed8
PM
5308 .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
5309 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
c624ea0f 5310 .writefn = hcr_writelow },
831a2fca
PM
5311 { .name = "HACR_EL2", .state = ARM_CP_STATE_BOTH,
5312 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 7,
5313 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3b685ba7 5314 { .name = "ELR_EL2", .state = ARM_CP_STATE_AA64,
7a0e58fa 5315 .type = ARM_CP_ALIAS,
3b685ba7
EI
5316 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 1,
5317 .access = PL2_RW,
5318 .fieldoffset = offsetof(CPUARMState, elr_el[2]) },
68e78e33 5319 { .name = "ESR_EL2", .state = ARM_CP_STATE_BOTH,
f2c30f42
EI
5320 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 0,
5321 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[2]) },
cba517c3 5322 { .name = "FAR_EL2", .state = ARM_CP_STATE_BOTH,
63b60551
EI
5323 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 0,
5324 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[2]) },
cba517c3
PM
5325 { .name = "HIFAR", .state = ARM_CP_STATE_AA32,
5326 .type = ARM_CP_ALIAS,
5327 .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 2,
5328 .access = PL2_RW,
5329 .fieldoffset = offsetofhigh32(CPUARMState, cp15.far_el[2]) },
3b685ba7 5330 { .name = "SPSR_EL2", .state = ARM_CP_STATE_AA64,
7a0e58fa 5331 .type = ARM_CP_ALIAS,
3b685ba7 5332 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 0,
99a99c1f
SB
5333 .access = PL2_RW,
5334 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_HYP]) },
d79e0c06 5335 { .name = "VBAR_EL2", .state = ARM_CP_STATE_BOTH,
d42e3c26
EI
5336 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0,
5337 .access = PL2_RW, .writefn = vbar_write,
5338 .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[2]),
5339 .resetvalue = 0 },
884b4dee
GB
5340 { .name = "SP_EL2", .state = ARM_CP_STATE_AA64,
5341 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 1, .opc2 = 0,
7a0e58fa 5342 .access = PL3_RW, .type = ARM_CP_ALIAS,
884b4dee 5343 .fieldoffset = offsetof(CPUARMState, sp_el[2]) },
c6f19164
GB
5344 { .name = "CPTR_EL2", .state = ARM_CP_STATE_BOTH,
5345 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 2,
5346 .access = PL2_RW, .accessfn = cptr_access, .resetvalue = 0,
fc1120a7
PM
5347 .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[2]),
5348 .readfn = cptr_el2_read, .writefn = cptr_el2_write },
95f949ac
EI
5349 { .name = "MAIR_EL2", .state = ARM_CP_STATE_BOTH,
5350 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 0,
5351 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[2]),
5352 .resetvalue = 0 },
5353 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
b5ede85b 5354 .cp = 15, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 1,
95f949ac
EI
5355 .access = PL2_RW, .type = ARM_CP_ALIAS,
5356 .fieldoffset = offsetofhigh32(CPUARMState, cp15.mair_el[2]) },
2179ef95
PM
5357 { .name = "AMAIR_EL2", .state = ARM_CP_STATE_BOTH,
5358 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 0,
5359 .access = PL2_RW, .type = ARM_CP_CONST,
5360 .resetvalue = 0 },
5361 /* HAMAIR1 is mapped to AMAIR_EL2[63:32] */
55b53c71 5362 { .name = "HAMAIR1", .state = ARM_CP_STATE_AA32,
b5ede85b 5363 .cp = 15, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 1,
2179ef95
PM
5364 .access = PL2_RW, .type = ARM_CP_CONST,
5365 .resetvalue = 0 },
37cd6c24
PM
5366 { .name = "AFSR0_EL2", .state = ARM_CP_STATE_BOTH,
5367 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 0,
5368 .access = PL2_RW, .type = ARM_CP_CONST,
5369 .resetvalue = 0 },
5370 { .name = "AFSR1_EL2", .state = ARM_CP_STATE_BOTH,
5371 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 1,
5372 .access = PL2_RW, .type = ARM_CP_CONST,
5373 .resetvalue = 0 },
06ec4c8c
EI
5374 { .name = "TCR_EL2", .state = ARM_CP_STATE_BOTH,
5375 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 2,
d06dc933
RH
5376 .access = PL2_RW, .writefn = vmsa_tcr_el12_write,
5377 /* no .raw_writefn or .resetfn needed as we never use mask/base_mask */
06ec4c8c 5378 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[2]) },
68e9c2fe
EI
5379 { .name = "VTCR", .state = ARM_CP_STATE_AA32,
5380 .cp = 15, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
bf06c112 5381 .type = ARM_CP_ALIAS,
68e9c2fe
EI
5382 .access = PL2_RW, .accessfn = access_el3_aa32ns,
5383 .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) },
5384 { .name = "VTCR_EL2", .state = ARM_CP_STATE_AA64,
5385 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
bf06c112
PM
5386 .access = PL2_RW,
5387 /* no .writefn needed as this can't cause an ASID change;
5388 * no .raw_writefn or .resetfn needed as we never use mask/base_mask
5389 */
68e9c2fe 5390 .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) },
b698e9cf
EI
5391 { .name = "VTTBR", .state = ARM_CP_STATE_AA32,
5392 .cp = 15, .opc1 = 6, .crm = 2,
5393 .type = ARM_CP_64BIT | ARM_CP_ALIAS,
5394 .access = PL2_RW, .accessfn = access_el3_aa32ns,
5395 .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2),
5396 .writefn = vttbr_write },
5397 { .name = "VTTBR_EL2", .state = ARM_CP_STATE_AA64,
5398 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 0,
5399 .access = PL2_RW, .writefn = vttbr_write,
5400 .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2) },
b9cb5323
EI
5401 { .name = "SCTLR_EL2", .state = ARM_CP_STATE_BOTH,
5402 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 0,
5403 .access = PL2_RW, .raw_writefn = raw_write, .writefn = sctlr_write,
5404 .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[2]) },
ff05f37b
EI
5405 { .name = "TPIDR_EL2", .state = ARM_CP_STATE_BOTH,
5406 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 2,
5407 .access = PL2_RW, .resetvalue = 0,
5408 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[2]) },
a57633c0
EI
5409 { .name = "TTBR0_EL2", .state = ARM_CP_STATE_AA64,
5410 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
ed30da8e 5411 .access = PL2_RW, .resetvalue = 0, .writefn = vmsa_tcr_ttbr_el2_write,
a57633c0
EI
5412 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
5413 { .name = "HTTBR", .cp = 15, .opc1 = 4, .crm = 2,
5414 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
a57633c0 5415 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
541ef8c2
SS
5416 { .name = "TLBIALLNSNH",
5417 .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
5418 .type = ARM_CP_NO_RAW, .access = PL2_W,
5419 .writefn = tlbiall_nsnh_write },
5420 { .name = "TLBIALLNSNHIS",
5421 .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
5422 .type = ARM_CP_NO_RAW, .access = PL2_W,
5423 .writefn = tlbiall_nsnh_is_write },
5424 { .name = "TLBIALLH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
5425 .type = ARM_CP_NO_RAW, .access = PL2_W,
5426 .writefn = tlbiall_hyp_write },
5427 { .name = "TLBIALLHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
5428 .type = ARM_CP_NO_RAW, .access = PL2_W,
5429 .writefn = tlbiall_hyp_is_write },
5430 { .name = "TLBIMVAH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
5431 .type = ARM_CP_NO_RAW, .access = PL2_W,
5432 .writefn = tlbimva_hyp_write },
5433 { .name = "TLBIMVAHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
5434 .type = ARM_CP_NO_RAW, .access = PL2_W,
5435 .writefn = tlbimva_hyp_is_write },
51da9014
EI
5436 { .name = "TLBI_ALLE2", .state = ARM_CP_STATE_AA64,
5437 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
5438 .type = ARM_CP_NO_RAW, .access = PL2_W,
fd3ed969 5439 .writefn = tlbi_aa64_alle2_write },
8742d49d
EI
5440 { .name = "TLBI_VAE2", .state = ARM_CP_STATE_AA64,
5441 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
5442 .type = ARM_CP_NO_RAW, .access = PL2_W,
fd3ed969 5443 .writefn = tlbi_aa64_vae2_write },
2bfb9d75
PM
5444 { .name = "TLBI_VALE2", .state = ARM_CP_STATE_AA64,
5445 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
5446 .access = PL2_W, .type = ARM_CP_NO_RAW,
5447 .writefn = tlbi_aa64_vae2_write },
5448 { .name = "TLBI_ALLE2IS", .state = ARM_CP_STATE_AA64,
5449 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
5450 .access = PL2_W, .type = ARM_CP_NO_RAW,
5451 .writefn = tlbi_aa64_alle2is_write },
8742d49d
EI
5452 { .name = "TLBI_VAE2IS", .state = ARM_CP_STATE_AA64,
5453 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
5454 .type = ARM_CP_NO_RAW, .access = PL2_W,
fd3ed969 5455 .writefn = tlbi_aa64_vae2is_write },
2bfb9d75
PM
5456 { .name = "TLBI_VALE2IS", .state = ARM_CP_STATE_AA64,
5457 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
5458 .access = PL2_W, .type = ARM_CP_NO_RAW,
5459 .writefn = tlbi_aa64_vae2is_write },
edac4d8a 5460#ifndef CONFIG_USER_ONLY
2a47df95
PM
5461 /* Unlike the other EL2-related AT operations, these must
5462 * UNDEF from EL3 if EL2 is not implemented, which is why we
5463 * define them here rather than with the rest of the AT ops.
5464 */
5465 { .name = "AT_S1E2R", .state = ARM_CP_STATE_AA64,
5466 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
5467 .access = PL2_W, .accessfn = at_s1e2_access,
0710b2fa 5468 .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC, .writefn = ats_write64 },
2a47df95
PM
5469 { .name = "AT_S1E2W", .state = ARM_CP_STATE_AA64,
5470 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
5471 .access = PL2_W, .accessfn = at_s1e2_access,
0710b2fa 5472 .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC, .writefn = ats_write64 },
14db7fe0
PM
5473 /* The AArch32 ATS1H* operations are CONSTRAINED UNPREDICTABLE
5474 * if EL2 is not implemented; we choose to UNDEF. Behaviour at EL3
5475 * with SCR.NS == 0 outside Monitor mode is UNPREDICTABLE; we choose
5476 * to behave as if SCR.NS was 1.
5477 */
5478 { .name = "ATS1HR", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
5479 .access = PL2_W,
0710b2fa 5480 .writefn = ats1h_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
14db7fe0
PM
5481 { .name = "ATS1HW", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
5482 .access = PL2_W,
0710b2fa 5483 .writefn = ats1h_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
0b6440af
EI
5484 { .name = "CNTHCTL_EL2", .state = ARM_CP_STATE_BOTH,
5485 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 1, .opc2 = 0,
5486 /* ARMv7 requires bit 0 and 1 to reset to 1. ARMv8 defines the
5487 * reset values as IMPDEF. We choose to reset to 3 to comply with
5488 * both ARMv7 and ARMv8.
5489 */
5490 .access = PL2_RW, .resetvalue = 3,
5491 .fieldoffset = offsetof(CPUARMState, cp15.cnthctl_el2) },
edac4d8a
EI
5492 { .name = "CNTVOFF_EL2", .state = ARM_CP_STATE_AA64,
5493 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 3,
5494 .access = PL2_RW, .type = ARM_CP_IO, .resetvalue = 0,
5495 .writefn = gt_cntvoff_write,
5496 .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
5497 { .name = "CNTVOFF", .cp = 15, .opc1 = 4, .crm = 14,
5498 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS | ARM_CP_IO,
5499 .writefn = gt_cntvoff_write,
5500 .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
b0e66d95
EI
5501 { .name = "CNTHP_CVAL_EL2", .state = ARM_CP_STATE_AA64,
5502 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 2,
5503 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
5504 .type = ARM_CP_IO, .access = PL2_RW,
5505 .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
5506 { .name = "CNTHP_CVAL", .cp = 15, .opc1 = 6, .crm = 14,
5507 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
5508 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_IO,
5509 .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
5510 { .name = "CNTHP_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
5511 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 0,
d44ec156 5512 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
b0e66d95
EI
5513 .resetfn = gt_hyp_timer_reset,
5514 .readfn = gt_hyp_tval_read, .writefn = gt_hyp_tval_write },
5515 { .name = "CNTHP_CTL_EL2", .state = ARM_CP_STATE_BOTH,
5516 .type = ARM_CP_IO,
5517 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 1,
5518 .access = PL2_RW,
5519 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].ctl),
5520 .resetvalue = 0,
5521 .writefn = gt_hyp_ctl_write, .raw_writefn = raw_write },
edac4d8a 5522#endif
14cc7b54
SF
5523 /* The only field of MDCR_EL2 that has a defined architectural reset value
5524 * is MDCR_EL2.HPMN which should reset to the value of PMCR_EL0.N; but we
5ecdd3e4 5525 * don't implement any PMU event counters, so using zero as a reset
14cc7b54
SF
5526 * value for MDCR_EL2 is okay
5527 */
5528 { .name = "MDCR_EL2", .state = ARM_CP_STATE_BOTH,
5529 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 1,
5530 .access = PL2_RW, .resetvalue = 0,
5531 .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el2), },
59e05530
EI
5532 { .name = "HPFAR", .state = ARM_CP_STATE_AA32,
5533 .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
5534 .access = PL2_RW, .accessfn = access_el3_aa32ns,
5535 .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
5536 { .name = "HPFAR_EL2", .state = ARM_CP_STATE_AA64,
5537 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
5538 .access = PL2_RW,
5539 .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
2a5a9abd
AF
5540 { .name = "HSTR_EL2", .state = ARM_CP_STATE_BOTH,
5541 .cp = 15, .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 3,
5542 .access = PL2_RW,
5543 .fieldoffset = offsetof(CPUARMState, cp15.hstr_el2) },
3b685ba7
EI
5544 REGINFO_SENTINEL
5545};
5546
ce4afed8
PM
5547static const ARMCPRegInfo el2_v8_cp_reginfo[] = {
5548 { .name = "HCR2", .state = ARM_CP_STATE_AA32,
89430fc6 5549 .type = ARM_CP_ALIAS | ARM_CP_IO,
ce4afed8
PM
5550 .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 4,
5551 .access = PL2_RW,
5552 .fieldoffset = offsetofhigh32(CPUARMState, cp15.hcr_el2),
5553 .writefn = hcr_writehigh },
5554 REGINFO_SENTINEL
5555};
5556
2f027fc5
PM
5557static CPAccessResult nsacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
5558 bool isread)
5559{
5560 /* The NSACR is RW at EL3, and RO for NS EL1 and NS EL2.
5561 * At Secure EL1 it traps to EL3.
5562 */
5563 if (arm_current_el(env) == 3) {
5564 return CP_ACCESS_OK;
5565 }
5566 if (arm_is_secure_below_el3(env)) {
5567 return CP_ACCESS_TRAP_EL3;
5568 }
5569 /* Accesses from EL1 NS and EL2 NS are UNDEF for write but allow reads. */
5570 if (isread) {
5571 return CP_ACCESS_OK;
5572 }
5573 return CP_ACCESS_TRAP_UNCATEGORIZED;
5574}
5575
60fb1a87
GB
5576static const ARMCPRegInfo el3_cp_reginfo[] = {
5577 { .name = "SCR_EL3", .state = ARM_CP_STATE_AA64,
5578 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 0,
5579 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.scr_el3),
5580 .resetvalue = 0, .writefn = scr_write },
f80741d1 5581 { .name = "SCR", .type = ARM_CP_ALIAS | ARM_CP_NEWEL,
60fb1a87 5582 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 0,
efe4a274
PM
5583 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
5584 .fieldoffset = offsetoflow32(CPUARMState, cp15.scr_el3),
b061a82b 5585 .writefn = scr_write },
60fb1a87
GB
5586 { .name = "SDER32_EL3", .state = ARM_CP_STATE_AA64,
5587 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 1,
5588 .access = PL3_RW, .resetvalue = 0,
5589 .fieldoffset = offsetof(CPUARMState, cp15.sder) },
5590 { .name = "SDER",
5591 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 1,
5592 .access = PL3_RW, .resetvalue = 0,
5593 .fieldoffset = offsetoflow32(CPUARMState, cp15.sder) },
60fb1a87 5594 { .name = "MVBAR", .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
efe4a274
PM
5595 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
5596 .writefn = vbar_write, .resetvalue = 0,
60fb1a87 5597 .fieldoffset = offsetof(CPUARMState, cp15.mvbar) },
7dd8c9af
FA
5598 { .name = "TTBR0_EL3", .state = ARM_CP_STATE_AA64,
5599 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 0,
f478847f 5600 .access = PL3_RW, .resetvalue = 0,
7dd8c9af 5601 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[3]) },
11f136ee
FA
5602 { .name = "TCR_EL3", .state = ARM_CP_STATE_AA64,
5603 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 2,
6459b94c
PM
5604 .access = PL3_RW,
5605 /* no .writefn needed as this can't cause an ASID change;
811595a2
PM
5606 * we must provide a .raw_writefn and .resetfn because we handle
5607 * reset and migration for the AArch32 TTBCR(S), which might be
5608 * using mask and base_mask.
6459b94c 5609 */
811595a2 5610 .resetfn = vmsa_ttbcr_reset, .raw_writefn = vmsa_ttbcr_raw_write,
11f136ee 5611 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[3]) },
81547d66 5612 { .name = "ELR_EL3", .state = ARM_CP_STATE_AA64,
7a0e58fa 5613 .type = ARM_CP_ALIAS,
81547d66
EI
5614 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 1,
5615 .access = PL3_RW,
5616 .fieldoffset = offsetof(CPUARMState, elr_el[3]) },
f2c30f42 5617 { .name = "ESR_EL3", .state = ARM_CP_STATE_AA64,
f2c30f42
EI
5618 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 2, .opc2 = 0,
5619 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[3]) },
63b60551
EI
5620 { .name = "FAR_EL3", .state = ARM_CP_STATE_AA64,
5621 .opc0 = 3, .opc1 = 6, .crn = 6, .crm = 0, .opc2 = 0,
5622 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[3]) },
81547d66 5623 { .name = "SPSR_EL3", .state = ARM_CP_STATE_AA64,
7a0e58fa 5624 .type = ARM_CP_ALIAS,
81547d66 5625 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 0,
99a99c1f
SB
5626 .access = PL3_RW,
5627 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_MON]) },
a1ba125c
EI
5628 { .name = "VBAR_EL3", .state = ARM_CP_STATE_AA64,
5629 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 0,
5630 .access = PL3_RW, .writefn = vbar_write,
5631 .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[3]),
5632 .resetvalue = 0 },
c6f19164
GB
5633 { .name = "CPTR_EL3", .state = ARM_CP_STATE_AA64,
5634 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 2,
5635 .access = PL3_RW, .accessfn = cptr_access, .resetvalue = 0,
5636 .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[3]) },
4cfb8ad8
PM
5637 { .name = "TPIDR_EL3", .state = ARM_CP_STATE_AA64,
5638 .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 2,
5639 .access = PL3_RW, .resetvalue = 0,
5640 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[3]) },
2179ef95
PM
5641 { .name = "AMAIR_EL3", .state = ARM_CP_STATE_AA64,
5642 .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 3, .opc2 = 0,
5643 .access = PL3_RW, .type = ARM_CP_CONST,
5644 .resetvalue = 0 },
37cd6c24
PM
5645 { .name = "AFSR0_EL3", .state = ARM_CP_STATE_BOTH,
5646 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 0,
5647 .access = PL3_RW, .type = ARM_CP_CONST,
5648 .resetvalue = 0 },
5649 { .name = "AFSR1_EL3", .state = ARM_CP_STATE_BOTH,
5650 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 1,
5651 .access = PL3_RW, .type = ARM_CP_CONST,
5652 .resetvalue = 0 },
43efaa33
PM
5653 { .name = "TLBI_ALLE3IS", .state = ARM_CP_STATE_AA64,
5654 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 0,
5655 .access = PL3_W, .type = ARM_CP_NO_RAW,
5656 .writefn = tlbi_aa64_alle3is_write },
5657 { .name = "TLBI_VAE3IS", .state = ARM_CP_STATE_AA64,
5658 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 1,
5659 .access = PL3_W, .type = ARM_CP_NO_RAW,
5660 .writefn = tlbi_aa64_vae3is_write },
5661 { .name = "TLBI_VALE3IS", .state = ARM_CP_STATE_AA64,
5662 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 5,
5663 .access = PL3_W, .type = ARM_CP_NO_RAW,
5664 .writefn = tlbi_aa64_vae3is_write },
5665 { .name = "TLBI_ALLE3", .state = ARM_CP_STATE_AA64,
5666 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 0,
5667 .access = PL3_W, .type = ARM_CP_NO_RAW,
5668 .writefn = tlbi_aa64_alle3_write },
5669 { .name = "TLBI_VAE3", .state = ARM_CP_STATE_AA64,
5670 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 1,
5671 .access = PL3_W, .type = ARM_CP_NO_RAW,
5672 .writefn = tlbi_aa64_vae3_write },
5673 { .name = "TLBI_VALE3", .state = ARM_CP_STATE_AA64,
5674 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 5,
5675 .access = PL3_W, .type = ARM_CP_NO_RAW,
5676 .writefn = tlbi_aa64_vae3_write },
0f1a3b24
FA
5677 REGINFO_SENTINEL
5678};
5679
e2cce18f
RH
5680#ifndef CONFIG_USER_ONLY
5681/* Test if system register redirection is to occur in the current state. */
5682static bool redirect_for_e2h(CPUARMState *env)
5683{
5684 return arm_current_el(env) == 2 && (arm_hcr_el2_eff(env) & HCR_E2H);
5685}
5686
5687static uint64_t el2_e2h_read(CPUARMState *env, const ARMCPRegInfo *ri)
5688{
5689 CPReadFn *readfn;
5690
5691 if (redirect_for_e2h(env)) {
5692 /* Switch to the saved EL2 version of the register. */
5693 ri = ri->opaque;
5694 readfn = ri->readfn;
5695 } else {
5696 readfn = ri->orig_readfn;
5697 }
5698 if (readfn == NULL) {
5699 readfn = raw_read;
5700 }
5701 return readfn(env, ri);
5702}
5703
5704static void el2_e2h_write(CPUARMState *env, const ARMCPRegInfo *ri,
5705 uint64_t value)
5706{
5707 CPWriteFn *writefn;
5708
5709 if (redirect_for_e2h(env)) {
5710 /* Switch to the saved EL2 version of the register. */
5711 ri = ri->opaque;
5712 writefn = ri->writefn;
5713 } else {
5714 writefn = ri->orig_writefn;
5715 }
5716 if (writefn == NULL) {
5717 writefn = raw_write;
5718 }
5719 writefn(env, ri, value);
5720}
5721
5722static void define_arm_vh_e2h_redirects_aliases(ARMCPU *cpu)
5723{
5724 struct E2HAlias {
5725 uint32_t src_key, dst_key, new_key;
5726 const char *src_name, *dst_name, *new_name;
5727 bool (*feature)(const ARMISARegisters *id);
5728 };
5729
5730#define K(op0, op1, crn, crm, op2) \
5731 ENCODE_AA64_CP_REG(CP_REG_ARM64_SYSREG_CP, crn, crm, op0, op1, op2)
5732
5733 static const struct E2HAlias aliases[] = {
5734 { K(3, 0, 1, 0, 0), K(3, 4, 1, 0, 0), K(3, 5, 1, 0, 0),
5735 "SCTLR", "SCTLR_EL2", "SCTLR_EL12" },
5736 { K(3, 0, 1, 0, 2), K(3, 4, 1, 1, 2), K(3, 5, 1, 0, 2),
5737 "CPACR", "CPTR_EL2", "CPACR_EL12" },
5738 { K(3, 0, 2, 0, 0), K(3, 4, 2, 0, 0), K(3, 5, 2, 0, 0),
5739 "TTBR0_EL1", "TTBR0_EL2", "TTBR0_EL12" },
5740 { K(3, 0, 2, 0, 1), K(3, 4, 2, 0, 1), K(3, 5, 2, 0, 1),
5741 "TTBR1_EL1", "TTBR1_EL2", "TTBR1_EL12" },
5742 { K(3, 0, 2, 0, 2), K(3, 4, 2, 0, 2), K(3, 5, 2, 0, 2),
5743 "TCR_EL1", "TCR_EL2", "TCR_EL12" },
5744 { K(3, 0, 4, 0, 0), K(3, 4, 4, 0, 0), K(3, 5, 4, 0, 0),
5745 "SPSR_EL1", "SPSR_EL2", "SPSR_EL12" },
5746 { K(3, 0, 4, 0, 1), K(3, 4, 4, 0, 1), K(3, 5, 4, 0, 1),
5747 "ELR_EL1", "ELR_EL2", "ELR_EL12" },
5748 { K(3, 0, 5, 1, 0), K(3, 4, 5, 1, 0), K(3, 5, 5, 1, 0),
5749 "AFSR0_EL1", "AFSR0_EL2", "AFSR0_EL12" },
5750 { K(3, 0, 5, 1, 1), K(3, 4, 5, 1, 1), K(3, 5, 5, 1, 1),
5751 "AFSR1_EL1", "AFSR1_EL2", "AFSR1_EL12" },
5752 { K(3, 0, 5, 2, 0), K(3, 4, 5, 2, 0), K(3, 5, 5, 2, 0),
5753 "ESR_EL1", "ESR_EL2", "ESR_EL12" },
5754 { K(3, 0, 6, 0, 0), K(3, 4, 6, 0, 0), K(3, 5, 6, 0, 0),
5755 "FAR_EL1", "FAR_EL2", "FAR_EL12" },
5756 { K(3, 0, 10, 2, 0), K(3, 4, 10, 2, 0), K(3, 5, 10, 2, 0),
5757 "MAIR_EL1", "MAIR_EL2", "MAIR_EL12" },
5758 { K(3, 0, 10, 3, 0), K(3, 4, 10, 3, 0), K(3, 5, 10, 3, 0),
5759 "AMAIR0", "AMAIR_EL2", "AMAIR_EL12" },
5760 { K(3, 0, 12, 0, 0), K(3, 4, 12, 0, 0), K(3, 5, 12, 0, 0),
5761 "VBAR", "VBAR_EL2", "VBAR_EL12" },
5762 { K(3, 0, 13, 0, 1), K(3, 4, 13, 0, 1), K(3, 5, 13, 0, 1),
5763 "CONTEXTIDR_EL1", "CONTEXTIDR_EL2", "CONTEXTIDR_EL12" },
5764 { K(3, 0, 14, 1, 0), K(3, 4, 14, 1, 0), K(3, 5, 14, 1, 0),
5765 "CNTKCTL", "CNTHCTL_EL2", "CNTKCTL_EL12" },
5766
5767 /*
5768 * Note that redirection of ZCR is mentioned in the description
5769 * of ZCR_EL2, and aliasing in the description of ZCR_EL1, but
5770 * not in the summary table.
5771 */
5772 { K(3, 0, 1, 2, 0), K(3, 4, 1, 2, 0), K(3, 5, 1, 2, 0),
5773 "ZCR_EL1", "ZCR_EL2", "ZCR_EL12", isar_feature_aa64_sve },
5774
5775 /* TODO: ARMv8.2-SPE -- PMSCR_EL2 */
5776 /* TODO: ARMv8.4-Trace -- TRFCR_EL2 */
5777 };
5778#undef K
5779
5780 size_t i;
5781
5782 for (i = 0; i < ARRAY_SIZE(aliases); i++) {
5783 const struct E2HAlias *a = &aliases[i];
5784 ARMCPRegInfo *src_reg, *dst_reg;
5785
5786 if (a->feature && !a->feature(&cpu->isar)) {
5787 continue;
5788 }
5789
5790 src_reg = g_hash_table_lookup(cpu->cp_regs, &a->src_key);
5791 dst_reg = g_hash_table_lookup(cpu->cp_regs, &a->dst_key);
5792 g_assert(src_reg != NULL);
5793 g_assert(dst_reg != NULL);
5794
5795 /* Cross-compare names to detect typos in the keys. */
5796 g_assert(strcmp(src_reg->name, a->src_name) == 0);
5797 g_assert(strcmp(dst_reg->name, a->dst_name) == 0);
5798
5799 /* None of the core system registers use opaque; we will. */
5800 g_assert(src_reg->opaque == NULL);
5801
5802 /* Create alias before redirection so we dup the right data. */
5803 if (a->new_key) {
5804 ARMCPRegInfo *new_reg = g_memdup(src_reg, sizeof(ARMCPRegInfo));
5805 uint32_t *new_key = g_memdup(&a->new_key, sizeof(uint32_t));
5806 bool ok;
5807
5808 new_reg->name = a->new_name;
5809 new_reg->type |= ARM_CP_ALIAS;
5810 /* Remove PL1/PL0 access, leaving PL2/PL3 R/W in place. */
5811 new_reg->access &= PL2_RW | PL3_RW;
5812
5813 ok = g_hash_table_insert(cpu->cp_regs, new_key, new_reg);
5814 g_assert(ok);
5815 }
5816
5817 src_reg->opaque = dst_reg;
5818 src_reg->orig_readfn = src_reg->readfn ?: raw_read;
5819 src_reg->orig_writefn = src_reg->writefn ?: raw_write;
5820 if (!src_reg->raw_readfn) {
5821 src_reg->raw_readfn = raw_read;
5822 }
5823 if (!src_reg->raw_writefn) {
5824 src_reg->raw_writefn = raw_write;
5825 }
5826 src_reg->readfn = el2_e2h_read;
5827 src_reg->writefn = el2_e2h_write;
5828 }
5829}
5830#endif
5831
3f208fd7
PM
5832static CPAccessResult ctr_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
5833 bool isread)
7da845b0 5834{
97475a89
RH
5835 int cur_el = arm_current_el(env);
5836
5837 if (cur_el < 2) {
5838 uint64_t hcr = arm_hcr_el2_eff(env);
5839
5840 if (cur_el == 0) {
5841 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
5842 if (!(env->cp15.sctlr_el[2] & SCTLR_UCT)) {
5843 return CP_ACCESS_TRAP_EL2;
5844 }
5845 } else {
5846 if (!(env->cp15.sctlr_el[1] & SCTLR_UCT)) {
5847 return CP_ACCESS_TRAP;
5848 }
5849 if (hcr & HCR_TID2) {
5850 return CP_ACCESS_TRAP_EL2;
5851 }
5852 }
5853 } else if (hcr & HCR_TID2) {
5854 return CP_ACCESS_TRAP_EL2;
5855 }
7da845b0 5856 }
630fcd4d
MZ
5857
5858 if (arm_current_el(env) < 2 && arm_hcr_el2_eff(env) & HCR_TID2) {
5859 return CP_ACCESS_TRAP_EL2;
5860 }
5861
7da845b0
PM
5862 return CP_ACCESS_OK;
5863}
5864
1424ca8d
DM
5865static void oslar_write(CPUARMState *env, const ARMCPRegInfo *ri,
5866 uint64_t value)
5867{
5868 /* Writes to OSLAR_EL1 may update the OS lock status, which can be
5869 * read via a bit in OSLSR_EL1.
5870 */
5871 int oslock;
5872
5873 if (ri->state == ARM_CP_STATE_AA32) {
5874 oslock = (value == 0xC5ACCE55);
5875 } else {
5876 oslock = value & 1;
5877 }
5878
5879 env->cp15.oslsr_el1 = deposit32(env->cp15.oslsr_el1, 1, 1, oslock);
5880}
5881
50300698 5882static const ARMCPRegInfo debug_cp_reginfo[] = {
50300698 5883 /* DBGDRAR, DBGDSAR: always RAZ since we don't implement memory mapped
10aae104
PM
5884 * debug components. The AArch64 version of DBGDRAR is named MDRAR_EL1;
5885 * unlike DBGDRAR it is never accessible from EL0.
5886 * DBGDSAR is deprecated and must RAZ from v8 anyway, so it has no AArch64
5887 * accessor.
50300698
PM
5888 */
5889 { .name = "DBGDRAR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 0,
91b0a238
PM
5890 .access = PL0_R, .accessfn = access_tdra,
5891 .type = ARM_CP_CONST, .resetvalue = 0 },
10aae104
PM
5892 { .name = "MDRAR_EL1", .state = ARM_CP_STATE_AA64,
5893 .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
91b0a238
PM
5894 .access = PL1_R, .accessfn = access_tdra,
5895 .type = ARM_CP_CONST, .resetvalue = 0 },
50300698 5896 { .name = "DBGDSAR", .cp = 14, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
91b0a238
PM
5897 .access = PL0_R, .accessfn = access_tdra,
5898 .type = ARM_CP_CONST, .resetvalue = 0 },
17a9eb53 5899 /* Monitor debug system control register; the 32-bit alias is DBGDSCRext. */
10aae104
PM
5900 { .name = "MDSCR_EL1", .state = ARM_CP_STATE_BOTH,
5901 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
d6c8cf81 5902 .access = PL1_RW, .accessfn = access_tda,
0e5e8935
PM
5903 .fieldoffset = offsetof(CPUARMState, cp15.mdscr_el1),
5904 .resetvalue = 0 },
5e8b12ff
PM
5905 /* MDCCSR_EL0, aka DBGDSCRint. This is a read-only mirror of MDSCR_EL1.
5906 * We don't implement the configurable EL0 access.
5907 */
5908 { .name = "MDCCSR_EL0", .state = ARM_CP_STATE_BOTH,
5909 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
7a0e58fa 5910 .type = ARM_CP_ALIAS,
d6c8cf81 5911 .access = PL1_R, .accessfn = access_tda,
b061a82b 5912 .fieldoffset = offsetof(CPUARMState, cp15.mdscr_el1), },
10aae104
PM
5913 { .name = "OSLAR_EL1", .state = ARM_CP_STATE_BOTH,
5914 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 4,
1424ca8d 5915 .access = PL1_W, .type = ARM_CP_NO_RAW,
187f678d 5916 .accessfn = access_tdosa,
1424ca8d
DM
5917 .writefn = oslar_write },
5918 { .name = "OSLSR_EL1", .state = ARM_CP_STATE_BOTH,
5919 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 4,
5920 .access = PL1_R, .resetvalue = 10,
187f678d 5921 .accessfn = access_tdosa,
1424ca8d 5922 .fieldoffset = offsetof(CPUARMState, cp15.oslsr_el1) },
5e8b12ff
PM
5923 /* Dummy OSDLR_EL1: 32-bit Linux will read this */
5924 { .name = "OSDLR_EL1", .state = ARM_CP_STATE_BOTH,
5925 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 4,
187f678d
PM
5926 .access = PL1_RW, .accessfn = access_tdosa,
5927 .type = ARM_CP_NOP },
5e8b12ff
PM
5928 /* Dummy DBGVCR: Linux wants to clear this on startup, but we don't
5929 * implement vector catch debug events yet.
5930 */
5931 { .name = "DBGVCR",
5932 .cp = 14, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
d6c8cf81
PM
5933 .access = PL1_RW, .accessfn = access_tda,
5934 .type = ARM_CP_NOP },
4d2ec4da
PM
5935 /* Dummy DBGVCR32_EL2 (which is only for a 64-bit hypervisor
5936 * to save and restore a 32-bit guest's DBGVCR)
5937 */
5938 { .name = "DBGVCR32_EL2", .state = ARM_CP_STATE_AA64,
5939 .opc0 = 2, .opc1 = 4, .crn = 0, .crm = 7, .opc2 = 0,
5940 .access = PL2_RW, .accessfn = access_tda,
5941 .type = ARM_CP_NOP },
5dbdc434
PM
5942 /* Dummy MDCCINT_EL1, since we don't implement the Debug Communications
5943 * Channel but Linux may try to access this register. The 32-bit
5944 * alias is DBGDCCINT.
5945 */
5946 { .name = "MDCCINT_EL1", .state = ARM_CP_STATE_BOTH,
5947 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0,
5948 .access = PL1_RW, .accessfn = access_tda,
5949 .type = ARM_CP_NOP },
50300698
PM
5950 REGINFO_SENTINEL
5951};
5952
5953static const ARMCPRegInfo debug_lpae_cp_reginfo[] = {
5954 /* 64 bit access versions of the (dummy) debug registers */
5955 { .name = "DBGDRAR", .cp = 14, .crm = 1, .opc1 = 0,
5956 .access = PL0_R, .type = ARM_CP_CONST|ARM_CP_64BIT, .resetvalue = 0 },
5957 { .name = "DBGDSAR", .cp = 14, .crm = 2, .opc1 = 0,
5958 .access = PL0_R, .type = ARM_CP_CONST|ARM_CP_64BIT, .resetvalue = 0 },
5959 REGINFO_SENTINEL
5960};
5961
60eed086
RH
5962/* Return the exception level to which exceptions should be taken
5963 * via SVEAccessTrap. If an exception should be routed through
5964 * AArch64.AdvSIMDFPAccessTrap, return 0; fp_exception_el should
5965 * take care of raising that exception.
5966 * C.f. the ARM pseudocode function CheckSVEEnabled.
5be5e8ed 5967 */
ced31551 5968int sve_exception_el(CPUARMState *env, int el)
5be5e8ed
RH
5969{
5970#ifndef CONFIG_USER_ONLY
c2ddb7cf
RH
5971 uint64_t hcr_el2 = arm_hcr_el2_eff(env);
5972
5973 if (el <= 1 && (hcr_el2 & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
60eed086
RH
5974 bool disabled = false;
5975
5976 /* The CPACR.ZEN controls traps to EL1:
5977 * 0, 2 : trap EL0 and EL1 accesses
5978 * 1 : trap only EL0 accesses
5979 * 3 : trap no accesses
5980 */
5981 if (!extract32(env->cp15.cpacr_el1, 16, 1)) {
5982 disabled = true;
5983 } else if (!extract32(env->cp15.cpacr_el1, 17, 1)) {
2de7ace2 5984 disabled = el == 0;
5be5e8ed 5985 }
60eed086
RH
5986 if (disabled) {
5987 /* route_to_el2 */
c2ddb7cf 5988 return hcr_el2 & HCR_TGE ? 2 : 1;
5be5e8ed 5989 }
5be5e8ed 5990
60eed086
RH
5991 /* Check CPACR.FPEN. */
5992 if (!extract32(env->cp15.cpacr_el1, 20, 1)) {
5993 disabled = true;
5994 } else if (!extract32(env->cp15.cpacr_el1, 21, 1)) {
2de7ace2 5995 disabled = el == 0;
5be5e8ed 5996 }
60eed086
RH
5997 if (disabled) {
5998 return 0;
5be5e8ed 5999 }
5be5e8ed
RH
6000 }
6001
60eed086
RH
6002 /* CPTR_EL2. Since TZ and TFP are positive,
6003 * they will be zero when EL2 is not present.
6004 */
2de7ace2 6005 if (el <= 2 && !arm_is_secure_below_el3(env)) {
60eed086
RH
6006 if (env->cp15.cptr_el[2] & CPTR_TZ) {
6007 return 2;
6008 }
6009 if (env->cp15.cptr_el[2] & CPTR_TFP) {
6010 return 0;
6011 }
5be5e8ed
RH
6012 }
6013
60eed086
RH
6014 /* CPTR_EL3. Since EZ is negative we must check for EL3. */
6015 if (arm_feature(env, ARM_FEATURE_EL3)
6016 && !(env->cp15.cptr_el[3] & CPTR_EZ)) {
5be5e8ed
RH
6017 return 3;
6018 }
6019#endif
6020 return 0;
6021}
6022
0df9142d
AJ
6023static uint32_t sve_zcr_get_valid_len(ARMCPU *cpu, uint32_t start_len)
6024{
6e553f2a 6025 uint32_t end_len;
0df9142d 6026
6e553f2a
RH
6027 end_len = start_len &= 0xf;
6028 if (!test_bit(start_len, cpu->sve_vq_map)) {
6029 end_len = find_last_bit(cpu->sve_vq_map, start_len);
6030 assert(end_len < start_len);
6031 }
6032 return end_len;
0df9142d
AJ
6033}
6034
0ab5953b
RH
6035/*
6036 * Given that SVE is enabled, return the vector length for EL.
6037 */
ced31551 6038uint32_t sve_zcr_len_for_el(CPUARMState *env, int el)
0ab5953b 6039{
2fc0cc0e 6040 ARMCPU *cpu = env_archcpu(env);
0ab5953b
RH
6041 uint32_t zcr_len = cpu->sve_max_vq - 1;
6042
6043 if (el <= 1) {
6044 zcr_len = MIN(zcr_len, 0xf & (uint32_t)env->vfp.zcr_el[1]);
6045 }
6a02a732 6046 if (el <= 2 && arm_feature(env, ARM_FEATURE_EL2)) {
0ab5953b
RH
6047 zcr_len = MIN(zcr_len, 0xf & (uint32_t)env->vfp.zcr_el[2]);
6048 }
6a02a732 6049 if (arm_feature(env, ARM_FEATURE_EL3)) {
0ab5953b
RH
6050 zcr_len = MIN(zcr_len, 0xf & (uint32_t)env->vfp.zcr_el[3]);
6051 }
0df9142d
AJ
6052
6053 return sve_zcr_get_valid_len(cpu, zcr_len);
0ab5953b
RH
6054}
6055
5be5e8ed
RH
6056static void zcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6057 uint64_t value)
6058{
0ab5953b
RH
6059 int cur_el = arm_current_el(env);
6060 int old_len = sve_zcr_len_for_el(env, cur_el);
6061 int new_len;
6062
5be5e8ed 6063 /* Bits other than [3:0] are RAZ/WI. */
7b351d98 6064 QEMU_BUILD_BUG_ON(ARM_MAX_VQ > 16);
5be5e8ed 6065 raw_write(env, ri, value & 0xf);
0ab5953b
RH
6066
6067 /*
6068 * Because we arrived here, we know both FP and SVE are enabled;
6069 * otherwise we would have trapped access to the ZCR_ELn register.
6070 */
6071 new_len = sve_zcr_len_for_el(env, cur_el);
6072 if (new_len < old_len) {
6073 aarch64_sve_narrow_vq(env, new_len + 1);
6074 }
5be5e8ed
RH
6075}
6076
6077static const ARMCPRegInfo zcr_el1_reginfo = {
6078 .name = "ZCR_EL1", .state = ARM_CP_STATE_AA64,
6079 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 0,
11d7870b 6080 .access = PL1_RW, .type = ARM_CP_SVE,
5be5e8ed
RH
6081 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[1]),
6082 .writefn = zcr_write, .raw_writefn = raw_write
6083};
6084
6085static const ARMCPRegInfo zcr_el2_reginfo = {
6086 .name = "ZCR_EL2", .state = ARM_CP_STATE_AA64,
6087 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 0,
11d7870b 6088 .access = PL2_RW, .type = ARM_CP_SVE,
5be5e8ed
RH
6089 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[2]),
6090 .writefn = zcr_write, .raw_writefn = raw_write
6091};
6092
6093static const ARMCPRegInfo zcr_no_el2_reginfo = {
6094 .name = "ZCR_EL2", .state = ARM_CP_STATE_AA64,
6095 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 0,
11d7870b 6096 .access = PL2_RW, .type = ARM_CP_SVE,
5be5e8ed
RH
6097 .readfn = arm_cp_read_zero, .writefn = arm_cp_write_ignore
6098};
6099
6100static const ARMCPRegInfo zcr_el3_reginfo = {
6101 .name = "ZCR_EL3", .state = ARM_CP_STATE_AA64,
6102 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 2, .opc2 = 0,
11d7870b 6103 .access = PL3_RW, .type = ARM_CP_SVE,
5be5e8ed
RH
6104 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[3]),
6105 .writefn = zcr_write, .raw_writefn = raw_write
6106};
6107
9ee98ce8
PM
6108void hw_watchpoint_update(ARMCPU *cpu, int n)
6109{
6110 CPUARMState *env = &cpu->env;
6111 vaddr len = 0;
6112 vaddr wvr = env->cp15.dbgwvr[n];
6113 uint64_t wcr = env->cp15.dbgwcr[n];
6114 int mask;
6115 int flags = BP_CPU | BP_STOP_BEFORE_ACCESS;
6116
6117 if (env->cpu_watchpoint[n]) {
6118 cpu_watchpoint_remove_by_ref(CPU(cpu), env->cpu_watchpoint[n]);
6119 env->cpu_watchpoint[n] = NULL;
6120 }
6121
6122 if (!extract64(wcr, 0, 1)) {
6123 /* E bit clear : watchpoint disabled */
6124 return;
6125 }
6126
6127 switch (extract64(wcr, 3, 2)) {
6128 case 0:
6129 /* LSC 00 is reserved and must behave as if the wp is disabled */
6130 return;
6131 case 1:
6132 flags |= BP_MEM_READ;
6133 break;
6134 case 2:
6135 flags |= BP_MEM_WRITE;
6136 break;
6137 case 3:
6138 flags |= BP_MEM_ACCESS;
6139 break;
6140 }
6141
6142 /* Attempts to use both MASK and BAS fields simultaneously are
6143 * CONSTRAINED UNPREDICTABLE; we opt to ignore BAS in this case,
6144 * thus generating a watchpoint for every byte in the masked region.
6145 */
6146 mask = extract64(wcr, 24, 4);
6147 if (mask == 1 || mask == 2) {
6148 /* Reserved values of MASK; we must act as if the mask value was
6149 * some non-reserved value, or as if the watchpoint were disabled.
6150 * We choose the latter.
6151 */
6152 return;
6153 } else if (mask) {
6154 /* Watchpoint covers an aligned area up to 2GB in size */
6155 len = 1ULL << mask;
6156 /* If masked bits in WVR are not zero it's CONSTRAINED UNPREDICTABLE
6157 * whether the watchpoint fires when the unmasked bits match; we opt
6158 * to generate the exceptions.
6159 */
6160 wvr &= ~(len - 1);
6161 } else {
6162 /* Watchpoint covers bytes defined by the byte address select bits */
6163 int bas = extract64(wcr, 5, 8);
6164 int basstart;
6165
6166 if (bas == 0) {
6167 /* This must act as if the watchpoint is disabled */
6168 return;
6169 }
6170
6171 if (extract64(wvr, 2, 1)) {
6172 /* Deprecated case of an only 4-aligned address. BAS[7:4] are
6173 * ignored, and BAS[3:0] define which bytes to watch.
6174 */
6175 bas &= 0xf;
6176 }
6177 /* The BAS bits are supposed to be programmed to indicate a contiguous
6178 * range of bytes. Otherwise it is CONSTRAINED UNPREDICTABLE whether
6179 * we fire for each byte in the word/doubleword addressed by the WVR.
6180 * We choose to ignore any non-zero bits after the first range of 1s.
6181 */
6182 basstart = ctz32(bas);
6183 len = cto32(bas >> basstart);
6184 wvr += basstart;
6185 }
6186
6187 cpu_watchpoint_insert(CPU(cpu), wvr, len, flags,
6188 &env->cpu_watchpoint[n]);
6189}
6190
6191void hw_watchpoint_update_all(ARMCPU *cpu)
6192{
6193 int i;
6194 CPUARMState *env = &cpu->env;
6195
6196 /* Completely clear out existing QEMU watchpoints and our array, to
6197 * avoid possible stale entries following migration load.
6198 */
6199 cpu_watchpoint_remove_all(CPU(cpu), BP_CPU);
6200 memset(env->cpu_watchpoint, 0, sizeof(env->cpu_watchpoint));
6201
6202 for (i = 0; i < ARRAY_SIZE(cpu->env.cpu_watchpoint); i++) {
6203 hw_watchpoint_update(cpu, i);
6204 }
6205}
6206
6207static void dbgwvr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6208 uint64_t value)
6209{
2fc0cc0e 6210 ARMCPU *cpu = env_archcpu(env);
9ee98ce8
PM
6211 int i = ri->crm;
6212
6213 /* Bits [63:49] are hardwired to the value of bit [48]; that is, the
6214 * register reads and behaves as if values written are sign extended.
6215 * Bits [1:0] are RES0.
6216 */
6217 value = sextract64(value, 0, 49) & ~3ULL;
6218
6219 raw_write(env, ri, value);
6220 hw_watchpoint_update(cpu, i);
6221}
6222
6223static void dbgwcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6224 uint64_t value)
6225{
2fc0cc0e 6226 ARMCPU *cpu = env_archcpu(env);
9ee98ce8
PM
6227 int i = ri->crm;
6228
6229 raw_write(env, ri, value);
6230 hw_watchpoint_update(cpu, i);
6231}
6232
46747d15
PM
6233void hw_breakpoint_update(ARMCPU *cpu, int n)
6234{
6235 CPUARMState *env = &cpu->env;
6236 uint64_t bvr = env->cp15.dbgbvr[n];
6237 uint64_t bcr = env->cp15.dbgbcr[n];
6238 vaddr addr;
6239 int bt;
6240 int flags = BP_CPU;
6241
6242 if (env->cpu_breakpoint[n]) {
6243 cpu_breakpoint_remove_by_ref(CPU(cpu), env->cpu_breakpoint[n]);
6244 env->cpu_breakpoint[n] = NULL;
6245 }
6246
6247 if (!extract64(bcr, 0, 1)) {
6248 /* E bit clear : watchpoint disabled */
6249 return;
6250 }
6251
6252 bt = extract64(bcr, 20, 4);
6253
6254 switch (bt) {
6255 case 4: /* unlinked address mismatch (reserved if AArch64) */
6256 case 5: /* linked address mismatch (reserved if AArch64) */
6257 qemu_log_mask(LOG_UNIMP,
0221c8fd 6258 "arm: address mismatch breakpoint types not implemented\n");
46747d15
PM
6259 return;
6260 case 0: /* unlinked address match */
6261 case 1: /* linked address match */
6262 {
6263 /* Bits [63:49] are hardwired to the value of bit [48]; that is,
6264 * we behave as if the register was sign extended. Bits [1:0] are
6265 * RES0. The BAS field is used to allow setting breakpoints on 16
6266 * bit wide instructions; it is CONSTRAINED UNPREDICTABLE whether
6267 * a bp will fire if the addresses covered by the bp and the addresses
6268 * covered by the insn overlap but the insn doesn't start at the
6269 * start of the bp address range. We choose to require the insn and
6270 * the bp to have the same address. The constraints on writing to
6271 * BAS enforced in dbgbcr_write mean we have only four cases:
6272 * 0b0000 => no breakpoint
6273 * 0b0011 => breakpoint on addr
6274 * 0b1100 => breakpoint on addr + 2
6275 * 0b1111 => breakpoint on addr
6276 * See also figure D2-3 in the v8 ARM ARM (DDI0487A.c).
6277 */
6278 int bas = extract64(bcr, 5, 4);
6279 addr = sextract64(bvr, 0, 49) & ~3ULL;
6280 if (bas == 0) {
6281 return;
6282 }
6283 if (bas == 0xc) {
6284 addr += 2;
6285 }
6286 break;
6287 }
6288 case 2: /* unlinked context ID match */
6289 case 8: /* unlinked VMID match (reserved if no EL2) */
6290 case 10: /* unlinked context ID and VMID match (reserved if no EL2) */
6291 qemu_log_mask(LOG_UNIMP,
0221c8fd 6292 "arm: unlinked context breakpoint types not implemented\n");
46747d15
PM
6293 return;
6294 case 9: /* linked VMID match (reserved if no EL2) */
6295 case 11: /* linked context ID and VMID match (reserved if no EL2) */
6296 case 3: /* linked context ID match */
6297 default:
6298 /* We must generate no events for Linked context matches (unless
6299 * they are linked to by some other bp/wp, which is handled in
6300 * updates for the linking bp/wp). We choose to also generate no events
6301 * for reserved values.
6302 */
6303 return;
6304 }
6305
6306 cpu_breakpoint_insert(CPU(cpu), addr, flags, &env->cpu_breakpoint[n]);
6307}
6308
6309void hw_breakpoint_update_all(ARMCPU *cpu)
6310{
6311 int i;
6312 CPUARMState *env = &cpu->env;
6313
6314 /* Completely clear out existing QEMU breakpoints and our array, to
6315 * avoid possible stale entries following migration load.
6316 */
6317 cpu_breakpoint_remove_all(CPU(cpu), BP_CPU);
6318 memset(env->cpu_breakpoint, 0, sizeof(env->cpu_breakpoint));
6319
6320 for (i = 0; i < ARRAY_SIZE(cpu->env.cpu_breakpoint); i++) {
6321 hw_breakpoint_update(cpu, i);
6322 }
6323}
6324
6325static void dbgbvr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6326 uint64_t value)
6327{
2fc0cc0e 6328 ARMCPU *cpu = env_archcpu(env);
46747d15
PM
6329 int i = ri->crm;
6330
6331 raw_write(env, ri, value);
6332 hw_breakpoint_update(cpu, i);
6333}
6334
6335static void dbgbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6336 uint64_t value)
6337{
2fc0cc0e 6338 ARMCPU *cpu = env_archcpu(env);
46747d15
PM
6339 int i = ri->crm;
6340
6341 /* BAS[3] is a read-only copy of BAS[2], and BAS[1] a read-only
6342 * copy of BAS[0].
6343 */
6344 value = deposit64(value, 6, 1, extract64(value, 5, 1));
6345 value = deposit64(value, 8, 1, extract64(value, 7, 1));
6346
6347 raw_write(env, ri, value);
6348 hw_breakpoint_update(cpu, i);
6349}
6350
50300698 6351static void define_debug_regs(ARMCPU *cpu)
0b45451e 6352{
50300698
PM
6353 /* Define v7 and v8 architectural debug registers.
6354 * These are just dummy implementations for now.
0b45451e
PM
6355 */
6356 int i;
3ff6fc91 6357 int wrps, brps, ctx_cmps;
48eb3ae6
PM
6358 ARMCPRegInfo dbgdidr = {
6359 .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0,
d6c8cf81 6360 .access = PL0_R, .accessfn = access_tda,
4426d361 6361 .type = ARM_CP_CONST, .resetvalue = cpu->isar.dbgdidr,
48eb3ae6
PM
6362 };
6363
3ff6fc91 6364 /* Note that all these register fields hold "number of Xs minus 1". */
88ce6c6e
PM
6365 brps = arm_num_brps(cpu);
6366 wrps = arm_num_wrps(cpu);
6367 ctx_cmps = arm_num_ctx_cmps(cpu);
3ff6fc91
PM
6368
6369 assert(ctx_cmps <= brps);
48eb3ae6 6370
48eb3ae6 6371 define_one_arm_cp_reg(cpu, &dbgdidr);
50300698
PM
6372 define_arm_cp_regs(cpu, debug_cp_reginfo);
6373
6374 if (arm_feature(&cpu->env, ARM_FEATURE_LPAE)) {
6375 define_arm_cp_regs(cpu, debug_lpae_cp_reginfo);
6376 }
6377
88ce6c6e 6378 for (i = 0; i < brps; i++) {
0b45451e 6379 ARMCPRegInfo dbgregs[] = {
10aae104
PM
6380 { .name = "DBGBVR", .state = ARM_CP_STATE_BOTH,
6381 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 4,
d6c8cf81 6382 .access = PL1_RW, .accessfn = access_tda,
46747d15
PM
6383 .fieldoffset = offsetof(CPUARMState, cp15.dbgbvr[i]),
6384 .writefn = dbgbvr_write, .raw_writefn = raw_write
6385 },
10aae104
PM
6386 { .name = "DBGBCR", .state = ARM_CP_STATE_BOTH,
6387 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 5,
d6c8cf81 6388 .access = PL1_RW, .accessfn = access_tda,
46747d15
PM
6389 .fieldoffset = offsetof(CPUARMState, cp15.dbgbcr[i]),
6390 .writefn = dbgbcr_write, .raw_writefn = raw_write
6391 },
48eb3ae6
PM
6392 REGINFO_SENTINEL
6393 };
6394 define_arm_cp_regs(cpu, dbgregs);
6395 }
6396
88ce6c6e 6397 for (i = 0; i < wrps; i++) {
48eb3ae6 6398 ARMCPRegInfo dbgregs[] = {
10aae104
PM
6399 { .name = "DBGWVR", .state = ARM_CP_STATE_BOTH,
6400 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 6,
d6c8cf81 6401 .access = PL1_RW, .accessfn = access_tda,
9ee98ce8
PM
6402 .fieldoffset = offsetof(CPUARMState, cp15.dbgwvr[i]),
6403 .writefn = dbgwvr_write, .raw_writefn = raw_write
6404 },
10aae104
PM
6405 { .name = "DBGWCR", .state = ARM_CP_STATE_BOTH,
6406 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 7,
d6c8cf81 6407 .access = PL1_RW, .accessfn = access_tda,
9ee98ce8
PM
6408 .fieldoffset = offsetof(CPUARMState, cp15.dbgwcr[i]),
6409 .writefn = dbgwcr_write, .raw_writefn = raw_write
6410 },
6411 REGINFO_SENTINEL
0b45451e
PM
6412 };
6413 define_arm_cp_regs(cpu, dbgregs);
6414 }
6415}
6416
24183fb6
PM
6417static void define_pmu_regs(ARMCPU *cpu)
6418{
6419 /*
6420 * v7 performance monitor control register: same implementor
6421 * field as main ID register, and we implement four counters in
6422 * addition to the cycle count register.
6423 */
6424 unsigned int i, pmcrn = 4;
6425 ARMCPRegInfo pmcr = {
6426 .name = "PMCR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 0,
6427 .access = PL0_RW,
6428 .type = ARM_CP_IO | ARM_CP_ALIAS,
6429 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcr),
6430 .accessfn = pmreg_access, .writefn = pmcr_write,
6431 .raw_writefn = raw_write,
6432 };
6433 ARMCPRegInfo pmcr64 = {
6434 .name = "PMCR_EL0", .state = ARM_CP_STATE_AA64,
6435 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 0,
6436 .access = PL0_RW, .accessfn = pmreg_access,
6437 .type = ARM_CP_IO,
6438 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcr),
62d96ff4
PM
6439 .resetvalue = (cpu->midr & 0xff000000) | (pmcrn << PMCRN_SHIFT) |
6440 PMCRLC,
24183fb6
PM
6441 .writefn = pmcr_write, .raw_writefn = raw_write,
6442 };
6443 define_one_arm_cp_reg(cpu, &pmcr);
6444 define_one_arm_cp_reg(cpu, &pmcr64);
6445 for (i = 0; i < pmcrn; i++) {
6446 char *pmevcntr_name = g_strdup_printf("PMEVCNTR%d", i);
6447 char *pmevcntr_el0_name = g_strdup_printf("PMEVCNTR%d_EL0", i);
6448 char *pmevtyper_name = g_strdup_printf("PMEVTYPER%d", i);
6449 char *pmevtyper_el0_name = g_strdup_printf("PMEVTYPER%d_EL0", i);
6450 ARMCPRegInfo pmev_regs[] = {
6451 { .name = pmevcntr_name, .cp = 15, .crn = 14,
6452 .crm = 8 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7,
6453 .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS,
6454 .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn,
6455 .accessfn = pmreg_access },
6456 { .name = pmevcntr_el0_name, .state = ARM_CP_STATE_AA64,
6457 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 8 | (3 & (i >> 3)),
6458 .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access,
6459 .type = ARM_CP_IO,
6460 .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn,
6461 .raw_readfn = pmevcntr_rawread,
6462 .raw_writefn = pmevcntr_rawwrite },
6463 { .name = pmevtyper_name, .cp = 15, .crn = 14,
6464 .crm = 12 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7,
6465 .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS,
6466 .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn,
6467 .accessfn = pmreg_access },
6468 { .name = pmevtyper_el0_name, .state = ARM_CP_STATE_AA64,
6469 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 12 | (3 & (i >> 3)),
6470 .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access,
6471 .type = ARM_CP_IO,
6472 .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn,
6473 .raw_writefn = pmevtyper_rawwrite },
6474 REGINFO_SENTINEL
6475 };
6476 define_arm_cp_regs(cpu, pmev_regs);
6477 g_free(pmevcntr_name);
6478 g_free(pmevcntr_el0_name);
6479 g_free(pmevtyper_name);
6480 g_free(pmevtyper_el0_name);
6481 }
a6179538 6482 if (cpu_isar_feature(aa32_pmu_8_1, cpu)) {
24183fb6
PM
6483 ARMCPRegInfo v81_pmu_regs[] = {
6484 { .name = "PMCEID2", .state = ARM_CP_STATE_AA32,
6485 .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 4,
6486 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6487 .resetvalue = extract64(cpu->pmceid0, 32, 32) },
6488 { .name = "PMCEID3", .state = ARM_CP_STATE_AA32,
6489 .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 5,
6490 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6491 .resetvalue = extract64(cpu->pmceid1, 32, 32) },
6492 REGINFO_SENTINEL
6493 };
6494 define_arm_cp_regs(cpu, v81_pmu_regs);
6495 }
15dd1ebd
PM
6496 if (cpu_isar_feature(any_pmu_8_4, cpu)) {
6497 static const ARMCPRegInfo v84_pmmir = {
6498 .name = "PMMIR_EL1", .state = ARM_CP_STATE_BOTH,
6499 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 6,
6500 .access = PL1_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6501 .resetvalue = 0
6502 };
6503 define_one_arm_cp_reg(cpu, &v84_pmmir);
6504 }
24183fb6
PM
6505}
6506
96a8b92e
PM
6507/* We don't know until after realize whether there's a GICv3
6508 * attached, and that is what registers the gicv3 sysregs.
6509 * So we have to fill in the GIC fields in ID_PFR/ID_PFR1_EL1/ID_AA64PFR0_EL1
6510 * at runtime.
6511 */
6512static uint64_t id_pfr1_read(CPUARMState *env, const ARMCPRegInfo *ri)
6513{
2fc0cc0e 6514 ARMCPU *cpu = env_archcpu(env);
96a8b92e
PM
6515 uint64_t pfr1 = cpu->id_pfr1;
6516
6517 if (env->gicv3state) {
6518 pfr1 |= 1 << 28;
6519 }
6520 return pfr1;
6521}
6522
6523static uint64_t id_aa64pfr0_read(CPUARMState *env, const ARMCPRegInfo *ri)
6524{
2fc0cc0e 6525 ARMCPU *cpu = env_archcpu(env);
47576b94 6526 uint64_t pfr0 = cpu->isar.id_aa64pfr0;
96a8b92e
PM
6527
6528 if (env->gicv3state) {
6529 pfr0 |= 1 << 24;
6530 }
6531 return pfr0;
6532}
6533
2d7137c1
RH
6534/* Shared logic between LORID and the rest of the LOR* registers.
6535 * Secure state has already been delt with.
6536 */
6537static CPAccessResult access_lor_ns(CPUARMState *env)
6538{
6539 int el = arm_current_el(env);
6540
6541 if (el < 2 && (arm_hcr_el2_eff(env) & HCR_TLOR)) {
6542 return CP_ACCESS_TRAP_EL2;
6543 }
6544 if (el < 3 && (env->cp15.scr_el3 & SCR_TLOR)) {
6545 return CP_ACCESS_TRAP_EL3;
6546 }
6547 return CP_ACCESS_OK;
6548}
6549
6550static CPAccessResult access_lorid(CPUARMState *env, const ARMCPRegInfo *ri,
6551 bool isread)
6552{
6553 if (arm_is_secure_below_el3(env)) {
6554 /* Access ok in secure mode. */
6555 return CP_ACCESS_OK;
6556 }
6557 return access_lor_ns(env);
6558}
6559
6560static CPAccessResult access_lor_other(CPUARMState *env,
6561 const ARMCPRegInfo *ri, bool isread)
6562{
6563 if (arm_is_secure_below_el3(env)) {
6564 /* Access denied in secure mode. */
6565 return CP_ACCESS_TRAP;
6566 }
6567 return access_lor_ns(env);
6568}
6569
d8564ee4
RH
6570/*
6571 * A trivial implementation of ARMv8.1-LOR leaves all of these
6572 * registers fixed at 0, which indicates that there are zero
6573 * supported Limited Ordering regions.
6574 */
6575static const ARMCPRegInfo lor_reginfo[] = {
6576 { .name = "LORSA_EL1", .state = ARM_CP_STATE_AA64,
6577 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 0,
6578 .access = PL1_RW, .accessfn = access_lor_other,
6579 .type = ARM_CP_CONST, .resetvalue = 0 },
6580 { .name = "LOREA_EL1", .state = ARM_CP_STATE_AA64,
6581 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 1,
6582 .access = PL1_RW, .accessfn = access_lor_other,
6583 .type = ARM_CP_CONST, .resetvalue = 0 },
6584 { .name = "LORN_EL1", .state = ARM_CP_STATE_AA64,
6585 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 2,
6586 .access = PL1_RW, .accessfn = access_lor_other,
6587 .type = ARM_CP_CONST, .resetvalue = 0 },
6588 { .name = "LORC_EL1", .state = ARM_CP_STATE_AA64,
6589 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 3,
6590 .access = PL1_RW, .accessfn = access_lor_other,
6591 .type = ARM_CP_CONST, .resetvalue = 0 },
6592 { .name = "LORID_EL1", .state = ARM_CP_STATE_AA64,
6593 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 7,
6594 .access = PL1_R, .accessfn = access_lorid,
6595 .type = ARM_CP_CONST, .resetvalue = 0 },
6596 REGINFO_SENTINEL
6597};
6598
967aa94f
RH
6599#ifdef TARGET_AARCH64
6600static CPAccessResult access_pauth(CPUARMState *env, const ARMCPRegInfo *ri,
6601 bool isread)
6602{
6603 int el = arm_current_el(env);
6604
6605 if (el < 2 &&
6606 arm_feature(env, ARM_FEATURE_EL2) &&
6607 !(arm_hcr_el2_eff(env) & HCR_APK)) {
6608 return CP_ACCESS_TRAP_EL2;
6609 }
6610 if (el < 3 &&
6611 arm_feature(env, ARM_FEATURE_EL3) &&
6612 !(env->cp15.scr_el3 & SCR_APK)) {
6613 return CP_ACCESS_TRAP_EL3;
6614 }
6615 return CP_ACCESS_OK;
6616}
6617
6618static const ARMCPRegInfo pauth_reginfo[] = {
6619 { .name = "APDAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6620 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 0,
6621 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 6622 .fieldoffset = offsetof(CPUARMState, keys.apda.lo) },
967aa94f
RH
6623 { .name = "APDAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6624 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 1,
6625 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 6626 .fieldoffset = offsetof(CPUARMState, keys.apda.hi) },
967aa94f
RH
6627 { .name = "APDBKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6628 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 2,
6629 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 6630 .fieldoffset = offsetof(CPUARMState, keys.apdb.lo) },
967aa94f
RH
6631 { .name = "APDBKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6632 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 3,
6633 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 6634 .fieldoffset = offsetof(CPUARMState, keys.apdb.hi) },
967aa94f
RH
6635 { .name = "APGAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6636 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 0,
6637 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 6638 .fieldoffset = offsetof(CPUARMState, keys.apga.lo) },
967aa94f
RH
6639 { .name = "APGAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6640 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 1,
6641 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 6642 .fieldoffset = offsetof(CPUARMState, keys.apga.hi) },
967aa94f
RH
6643 { .name = "APIAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6644 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 0,
6645 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 6646 .fieldoffset = offsetof(CPUARMState, keys.apia.lo) },
967aa94f
RH
6647 { .name = "APIAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6648 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 1,
6649 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 6650 .fieldoffset = offsetof(CPUARMState, keys.apia.hi) },
967aa94f
RH
6651 { .name = "APIBKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6652 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 2,
6653 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 6654 .fieldoffset = offsetof(CPUARMState, keys.apib.lo) },
967aa94f
RH
6655 { .name = "APIBKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6656 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 3,
6657 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 6658 .fieldoffset = offsetof(CPUARMState, keys.apib.hi) },
967aa94f
RH
6659 REGINFO_SENTINEL
6660};
de390645
RH
6661
6662static uint64_t rndr_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
6663{
6664 Error *err = NULL;
6665 uint64_t ret;
6666
6667 /* Success sets NZCV = 0000. */
6668 env->NF = env->CF = env->VF = 0, env->ZF = 1;
6669
6670 if (qemu_guest_getrandom(&ret, sizeof(ret), &err) < 0) {
6671 /*
6672 * ??? Failed, for unknown reasons in the crypto subsystem.
6673 * The best we can do is log the reason and return the
6674 * timed-out indication to the guest. There is no reason
6675 * we know to expect this failure to be transitory, so the
6676 * guest may well hang retrying the operation.
6677 */
6678 qemu_log_mask(LOG_UNIMP, "%s: Crypto failure: %s",
6679 ri->name, error_get_pretty(err));
6680 error_free(err);
6681
6682 env->ZF = 0; /* NZCF = 0100 */
6683 return 0;
6684 }
6685 return ret;
6686}
6687
6688/* We do not support re-seeding, so the two registers operate the same. */
6689static const ARMCPRegInfo rndr_reginfo[] = {
6690 { .name = "RNDR", .state = ARM_CP_STATE_AA64,
6691 .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END | ARM_CP_IO,
6692 .opc0 = 3, .opc1 = 3, .crn = 2, .crm = 4, .opc2 = 0,
6693 .access = PL0_R, .readfn = rndr_readfn },
6694 { .name = "RNDRRS", .state = ARM_CP_STATE_AA64,
6695 .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END | ARM_CP_IO,
6696 .opc0 = 3, .opc1 = 3, .crn = 2, .crm = 4, .opc2 = 1,
6697 .access = PL0_R, .readfn = rndr_readfn },
6698 REGINFO_SENTINEL
6699};
0d57b499
BM
6700
6701#ifndef CONFIG_USER_ONLY
6702static void dccvap_writefn(CPUARMState *env, const ARMCPRegInfo *opaque,
6703 uint64_t value)
6704{
6705 ARMCPU *cpu = env_archcpu(env);
6706 /* CTR_EL0 System register -> DminLine, bits [19:16] */
6707 uint64_t dline_size = 4 << ((cpu->ctr >> 16) & 0xF);
6708 uint64_t vaddr_in = (uint64_t) value;
6709 uint64_t vaddr = vaddr_in & ~(dline_size - 1);
6710 void *haddr;
6711 int mem_idx = cpu_mmu_index(env, false);
6712
6713 /* This won't be crossing page boundaries */
6714 haddr = probe_read(env, vaddr, dline_size, mem_idx, GETPC());
6715 if (haddr) {
6716
6717 ram_addr_t offset;
6718 MemoryRegion *mr;
6719
6720 /* RCU lock is already being held */
6721 mr = memory_region_from_host(haddr, &offset);
6722
6723 if (mr) {
6724 memory_region_do_writeback(mr, offset, dline_size);
6725 }
6726 }
6727}
6728
6729static const ARMCPRegInfo dcpop_reg[] = {
6730 { .name = "DC_CVAP", .state = ARM_CP_STATE_AA64,
6731 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 1,
6732 .access = PL0_W, .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END,
6733 .accessfn = aa64_cacheop_access, .writefn = dccvap_writefn },
6734 REGINFO_SENTINEL
6735};
6736
6737static const ARMCPRegInfo dcpodp_reg[] = {
6738 { .name = "DC_CVADP", .state = ARM_CP_STATE_AA64,
6739 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 1,
6740 .access = PL0_W, .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END,
6741 .accessfn = aa64_cacheop_access, .writefn = dccvap_writefn },
6742 REGINFO_SENTINEL
6743};
6744#endif /*CONFIG_USER_ONLY*/
6745
967aa94f
RH
6746#endif
6747
cb570bd3
RH
6748static CPAccessResult access_predinv(CPUARMState *env, const ARMCPRegInfo *ri,
6749 bool isread)
6750{
6751 int el = arm_current_el(env);
6752
6753 if (el == 0) {
6754 uint64_t sctlr = arm_sctlr(env, el);
6755 if (!(sctlr & SCTLR_EnRCTX)) {
6756 return CP_ACCESS_TRAP;
6757 }
6758 } else if (el == 1) {
6759 uint64_t hcr = arm_hcr_el2_eff(env);
6760 if (hcr & HCR_NV) {
6761 return CP_ACCESS_TRAP_EL2;
6762 }
6763 }
6764 return CP_ACCESS_OK;
6765}
6766
6767static const ARMCPRegInfo predinv_reginfo[] = {
6768 { .name = "CFP_RCTX", .state = ARM_CP_STATE_AA64,
6769 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 4,
6770 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
6771 { .name = "DVP_RCTX", .state = ARM_CP_STATE_AA64,
6772 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 5,
6773 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
6774 { .name = "CPP_RCTX", .state = ARM_CP_STATE_AA64,
6775 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 7,
6776 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
6777 /*
6778 * Note the AArch32 opcodes have a different OPC1.
6779 */
6780 { .name = "CFPRCTX", .state = ARM_CP_STATE_AA32,
6781 .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 4,
6782 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
6783 { .name = "DVPRCTX", .state = ARM_CP_STATE_AA32,
6784 .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 5,
6785 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
6786 { .name = "CPPRCTX", .state = ARM_CP_STATE_AA32,
6787 .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 7,
6788 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
6789 REGINFO_SENTINEL
6790};
6791
957e6155
PM
6792static uint64_t ccsidr2_read(CPUARMState *env, const ARMCPRegInfo *ri)
6793{
6794 /* Read the high 32 bits of the current CCSIDR */
6795 return extract64(ccsidr_read(env, ri), 32, 32);
6796}
6797
6798static const ARMCPRegInfo ccsidr2_reginfo[] = {
6799 { .name = "CCSIDR2", .state = ARM_CP_STATE_BOTH,
6800 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 2,
6801 .access = PL1_R,
6802 .accessfn = access_aa64_tid2,
6803 .readfn = ccsidr2_read, .type = ARM_CP_NO_RAW },
6804 REGINFO_SENTINEL
6805};
6806
6a4ef4e5
MZ
6807static CPAccessResult access_aa64_tid3(CPUARMState *env, const ARMCPRegInfo *ri,
6808 bool isread)
6809{
6810 if ((arm_current_el(env) < 2) && (arm_hcr_el2_eff(env) & HCR_TID3)) {
6811 return CP_ACCESS_TRAP_EL2;
6812 }
6813
6814 return CP_ACCESS_OK;
6815}
6816
6817static CPAccessResult access_aa32_tid3(CPUARMState *env, const ARMCPRegInfo *ri,
6818 bool isread)
6819{
6820 if (arm_feature(env, ARM_FEATURE_V8)) {
6821 return access_aa64_tid3(env, ri, isread);
6822 }
6823
6824 return CP_ACCESS_OK;
6825}
6826
f96f3d5f
MZ
6827static CPAccessResult access_jazelle(CPUARMState *env, const ARMCPRegInfo *ri,
6828 bool isread)
6829{
6830 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID0)) {
6831 return CP_ACCESS_TRAP_EL2;
6832 }
6833
6834 return CP_ACCESS_OK;
6835}
6836
6837static const ARMCPRegInfo jazelle_regs[] = {
6838 { .name = "JIDR",
6839 .cp = 14, .crn = 0, .crm = 0, .opc1 = 7, .opc2 = 0,
6840 .access = PL1_R, .accessfn = access_jazelle,
6841 .type = ARM_CP_CONST, .resetvalue = 0 },
6842 { .name = "JOSCR",
6843 .cp = 14, .crn = 1, .crm = 0, .opc1 = 7, .opc2 = 0,
6844 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
6845 { .name = "JMCR",
6846 .cp = 14, .crn = 2, .crm = 0, .opc1 = 7, .opc2 = 0,
6847 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
6848 REGINFO_SENTINEL
6849};
6850
e2a1a461
RH
6851static const ARMCPRegInfo vhe_reginfo[] = {
6852 { .name = "CONTEXTIDR_EL2", .state = ARM_CP_STATE_AA64,
6853 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 1,
6854 .access = PL2_RW,
6855 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[2]) },
ed30da8e
RH
6856 { .name = "TTBR1_EL2", .state = ARM_CP_STATE_AA64,
6857 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 1,
6858 .access = PL2_RW, .writefn = vmsa_tcr_ttbr_el2_write,
6859 .fieldoffset = offsetof(CPUARMState, cp15.ttbr1_el[2]) },
8c94b071
RH
6860#ifndef CONFIG_USER_ONLY
6861 { .name = "CNTHV_CVAL_EL2", .state = ARM_CP_STATE_AA64,
6862 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 2,
6863 .fieldoffset =
6864 offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYPVIRT].cval),
6865 .type = ARM_CP_IO, .access = PL2_RW,
6866 .writefn = gt_hv_cval_write, .raw_writefn = raw_write },
6867 { .name = "CNTHV_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
6868 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 0,
6869 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
6870 .resetfn = gt_hv_timer_reset,
6871 .readfn = gt_hv_tval_read, .writefn = gt_hv_tval_write },
6872 { .name = "CNTHV_CTL_EL2", .state = ARM_CP_STATE_BOTH,
6873 .type = ARM_CP_IO,
6874 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 1,
6875 .access = PL2_RW,
6876 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYPVIRT].ctl),
6877 .writefn = gt_hv_ctl_write, .raw_writefn = raw_write },
bb5972e4
RH
6878 { .name = "CNTP_CTL_EL02", .state = ARM_CP_STATE_AA64,
6879 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 1,
6880 .type = ARM_CP_IO | ARM_CP_ALIAS,
6881 .access = PL2_RW, .accessfn = e2h_access,
6882 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
6883 .writefn = gt_phys_ctl_write, .raw_writefn = raw_write },
6884 { .name = "CNTV_CTL_EL02", .state = ARM_CP_STATE_AA64,
6885 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 1,
6886 .type = ARM_CP_IO | ARM_CP_ALIAS,
6887 .access = PL2_RW, .accessfn = e2h_access,
6888 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
6889 .writefn = gt_virt_ctl_write, .raw_writefn = raw_write },
6890 { .name = "CNTP_TVAL_EL02", .state = ARM_CP_STATE_AA64,
6891 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 0,
6892 .type = ARM_CP_NO_RAW | ARM_CP_IO | ARM_CP_ALIAS,
6893 .access = PL2_RW, .accessfn = e2h_access,
6894 .readfn = gt_phys_tval_read, .writefn = gt_phys_tval_write },
6895 { .name = "CNTV_TVAL_EL02", .state = ARM_CP_STATE_AA64,
6896 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 0,
6897 .type = ARM_CP_NO_RAW | ARM_CP_IO | ARM_CP_ALIAS,
6898 .access = PL2_RW, .accessfn = e2h_access,
6899 .readfn = gt_virt_tval_read, .writefn = gt_virt_tval_write },
6900 { .name = "CNTP_CVAL_EL02", .state = ARM_CP_STATE_AA64,
6901 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 2,
6902 .type = ARM_CP_IO | ARM_CP_ALIAS,
6903 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
6904 .access = PL2_RW, .accessfn = e2h_access,
6905 .writefn = gt_phys_cval_write, .raw_writefn = raw_write },
6906 { .name = "CNTV_CVAL_EL02", .state = ARM_CP_STATE_AA64,
6907 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 2,
6908 .type = ARM_CP_IO | ARM_CP_ALIAS,
6909 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
6910 .access = PL2_RW, .accessfn = e2h_access,
6911 .writefn = gt_virt_cval_write, .raw_writefn = raw_write },
8c94b071 6912#endif
e2a1a461
RH
6913 REGINFO_SENTINEL
6914};
6915
04b07d29
RH
6916#ifndef CONFIG_USER_ONLY
6917static const ARMCPRegInfo ats1e1_reginfo[] = {
6918 { .name = "AT_S1E1R", .state = ARM_CP_STATE_AA64,
6919 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 0,
6920 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
6921 .writefn = ats_write64 },
6922 { .name = "AT_S1E1W", .state = ARM_CP_STATE_AA64,
6923 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 1,
6924 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
6925 .writefn = ats_write64 },
6926 REGINFO_SENTINEL
6927};
6928
6929static const ARMCPRegInfo ats1cp_reginfo[] = {
6930 { .name = "ATS1CPRP",
6931 .cp = 15, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 0,
6932 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
6933 .writefn = ats_write },
6934 { .name = "ATS1CPWP",
6935 .cp = 15, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 1,
6936 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
6937 .writefn = ats_write },
6938 REGINFO_SENTINEL
6939};
6940#endif
6941
f6287c24
PM
6942/*
6943 * ACTLR2 and HACTLR2 map to ACTLR_EL1[63:32] and
6944 * ACTLR_EL2[63:32]. They exist only if the ID_MMFR4.AC2 field
6945 * is non-zero, which is never for ARMv7, optionally in ARMv8
6946 * and mandatorily for ARMv8.2 and up.
6947 * ACTLR2 is banked for S and NS if EL3 is AArch32. Since QEMU's
6948 * implementation is RAZ/WI we can ignore this detail, as we
6949 * do for ACTLR.
6950 */
6951static const ARMCPRegInfo actlr2_hactlr2_reginfo[] = {
6952 { .name = "ACTLR2", .state = ARM_CP_STATE_AA32,
6953 .cp = 15, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 3,
6954 .access = PL1_RW, .type = ARM_CP_CONST,
6955 .resetvalue = 0 },
6956 { .name = "HACTLR2", .state = ARM_CP_STATE_AA32,
6957 .cp = 15, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 3,
6958 .access = PL2_RW, .type = ARM_CP_CONST,
6959 .resetvalue = 0 },
6960 REGINFO_SENTINEL
6961};
6962
2ceb98c0
PM
6963void register_cp_regs_for_features(ARMCPU *cpu)
6964{
6965 /* Register all the coprocessor registers based on feature bits */
6966 CPUARMState *env = &cpu->env;
6967 if (arm_feature(env, ARM_FEATURE_M)) {
6968 /* M profile has no coprocessor registers */
6969 return;
6970 }
6971
e9aa6c21 6972 define_arm_cp_regs(cpu, cp_reginfo);
9449fdf6
PM
6973 if (!arm_feature(env, ARM_FEATURE_V8)) {
6974 /* Must go early as it is full of wildcards that may be
6975 * overridden by later definitions.
6976 */
6977 define_arm_cp_regs(cpu, not_v8_cp_reginfo);
6978 }
6979
7d57f408 6980 if (arm_feature(env, ARM_FEATURE_V6)) {
8515a092
PM
6981 /* The ID registers all have impdef reset values */
6982 ARMCPRegInfo v6_idregs[] = {
0ff644a7
PM
6983 { .name = "ID_PFR0", .state = ARM_CP_STATE_BOTH,
6984 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
6985 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 6986 .accessfn = access_aa32_tid3,
8515a092 6987 .resetvalue = cpu->id_pfr0 },
96a8b92e
PM
6988 /* ID_PFR1 is not a plain ARM_CP_CONST because we don't know
6989 * the value of the GIC field until after we define these regs.
6990 */
0ff644a7
PM
6991 { .name = "ID_PFR1", .state = ARM_CP_STATE_BOTH,
6992 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 1,
96a8b92e 6993 .access = PL1_R, .type = ARM_CP_NO_RAW,
6a4ef4e5 6994 .accessfn = access_aa32_tid3,
96a8b92e
PM
6995 .readfn = id_pfr1_read,
6996 .writefn = arm_cp_write_ignore },
0ff644a7
PM
6997 { .name = "ID_DFR0", .state = ARM_CP_STATE_BOTH,
6998 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 2,
6999 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7000 .accessfn = access_aa32_tid3,
a6179538 7001 .resetvalue = cpu->isar.id_dfr0 },
0ff644a7
PM
7002 { .name = "ID_AFR0", .state = ARM_CP_STATE_BOTH,
7003 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 3,
7004 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7005 .accessfn = access_aa32_tid3,
8515a092 7006 .resetvalue = cpu->id_afr0 },
0ff644a7
PM
7007 { .name = "ID_MMFR0", .state = ARM_CP_STATE_BOTH,
7008 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 4,
7009 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7010 .accessfn = access_aa32_tid3,
10054016 7011 .resetvalue = cpu->isar.id_mmfr0 },
0ff644a7
PM
7012 { .name = "ID_MMFR1", .state = ARM_CP_STATE_BOTH,
7013 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 5,
7014 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7015 .accessfn = access_aa32_tid3,
10054016 7016 .resetvalue = cpu->isar.id_mmfr1 },
0ff644a7
PM
7017 { .name = "ID_MMFR2", .state = ARM_CP_STATE_BOTH,
7018 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 6,
7019 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7020 .accessfn = access_aa32_tid3,
10054016 7021 .resetvalue = cpu->isar.id_mmfr2 },
0ff644a7
PM
7022 { .name = "ID_MMFR3", .state = ARM_CP_STATE_BOTH,
7023 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 7,
7024 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7025 .accessfn = access_aa32_tid3,
10054016 7026 .resetvalue = cpu->isar.id_mmfr3 },
0ff644a7
PM
7027 { .name = "ID_ISAR0", .state = ARM_CP_STATE_BOTH,
7028 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0,
7029 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7030 .accessfn = access_aa32_tid3,
47576b94 7031 .resetvalue = cpu->isar.id_isar0 },
0ff644a7
PM
7032 { .name = "ID_ISAR1", .state = ARM_CP_STATE_BOTH,
7033 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 1,
7034 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7035 .accessfn = access_aa32_tid3,
47576b94 7036 .resetvalue = cpu->isar.id_isar1 },
0ff644a7
PM
7037 { .name = "ID_ISAR2", .state = ARM_CP_STATE_BOTH,
7038 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
7039 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7040 .accessfn = access_aa32_tid3,
47576b94 7041 .resetvalue = cpu->isar.id_isar2 },
0ff644a7
PM
7042 { .name = "ID_ISAR3", .state = ARM_CP_STATE_BOTH,
7043 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 3,
7044 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7045 .accessfn = access_aa32_tid3,
47576b94 7046 .resetvalue = cpu->isar.id_isar3 },
0ff644a7
PM
7047 { .name = "ID_ISAR4", .state = ARM_CP_STATE_BOTH,
7048 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 4,
7049 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7050 .accessfn = access_aa32_tid3,
47576b94 7051 .resetvalue = cpu->isar.id_isar4 },
0ff644a7
PM
7052 { .name = "ID_ISAR5", .state = ARM_CP_STATE_BOTH,
7053 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 5,
7054 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7055 .accessfn = access_aa32_tid3,
47576b94 7056 .resetvalue = cpu->isar.id_isar5 },
e20d84c1
PM
7057 { .name = "ID_MMFR4", .state = ARM_CP_STATE_BOTH,
7058 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 6,
7059 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7060 .accessfn = access_aa32_tid3,
10054016 7061 .resetvalue = cpu->isar.id_mmfr4 },
802abf40 7062 { .name = "ID_ISAR6", .state = ARM_CP_STATE_BOTH,
e20d84c1
PM
7063 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 7,
7064 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7065 .accessfn = access_aa32_tid3,
47576b94 7066 .resetvalue = cpu->isar.id_isar6 },
8515a092
PM
7067 REGINFO_SENTINEL
7068 };
7069 define_arm_cp_regs(cpu, v6_idregs);
7d57f408
PM
7070 define_arm_cp_regs(cpu, v6_cp_reginfo);
7071 } else {
7072 define_arm_cp_regs(cpu, not_v6_cp_reginfo);
7073 }
4d31c596
PM
7074 if (arm_feature(env, ARM_FEATURE_V6K)) {
7075 define_arm_cp_regs(cpu, v6k_cp_reginfo);
7076 }
5e5cf9e3 7077 if (arm_feature(env, ARM_FEATURE_V7MP) &&
452a0955 7078 !arm_feature(env, ARM_FEATURE_PMSA)) {
995939a6
PM
7079 define_arm_cp_regs(cpu, v7mp_cp_reginfo);
7080 }
327dd510
AL
7081 if (arm_feature(env, ARM_FEATURE_V7VE)) {
7082 define_arm_cp_regs(cpu, pmovsset_cp_reginfo);
7083 }
e9aa6c21 7084 if (arm_feature(env, ARM_FEATURE_V7)) {
776d4e5c 7085 ARMCPRegInfo clidr = {
7da845b0
PM
7086 .name = "CLIDR", .state = ARM_CP_STATE_BOTH,
7087 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 1,
630fcd4d
MZ
7088 .access = PL1_R, .type = ARM_CP_CONST,
7089 .accessfn = access_aa64_tid2,
7090 .resetvalue = cpu->clidr
776d4e5c 7091 };
776d4e5c 7092 define_one_arm_cp_reg(cpu, &clidr);
e9aa6c21 7093 define_arm_cp_regs(cpu, v7_cp_reginfo);
50300698 7094 define_debug_regs(cpu);
24183fb6 7095 define_pmu_regs(cpu);
7d57f408
PM
7096 } else {
7097 define_arm_cp_regs(cpu, not_v7_cp_reginfo);
e9aa6c21 7098 }
b0d2b7d0 7099 if (arm_feature(env, ARM_FEATURE_V8)) {
e20d84c1
PM
7100 /* AArch64 ID registers, which all have impdef reset values.
7101 * Note that within the ID register ranges the unused slots
7102 * must all RAZ, not UNDEF; future architecture versions may
7103 * define new registers here.
7104 */
e60cef86 7105 ARMCPRegInfo v8_idregs[] = {
96a8b92e
PM
7106 /* ID_AA64PFR0_EL1 is not a plain ARM_CP_CONST because we don't
7107 * know the right value for the GIC field until after we
7108 * define these regs.
7109 */
e60cef86
PM
7110 { .name = "ID_AA64PFR0_EL1", .state = ARM_CP_STATE_AA64,
7111 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 0,
96a8b92e 7112 .access = PL1_R, .type = ARM_CP_NO_RAW,
6a4ef4e5 7113 .accessfn = access_aa64_tid3,
96a8b92e
PM
7114 .readfn = id_aa64pfr0_read,
7115 .writefn = arm_cp_write_ignore },
e60cef86
PM
7116 { .name = "ID_AA64PFR1_EL1", .state = ARM_CP_STATE_AA64,
7117 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 1,
7118 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7119 .accessfn = access_aa64_tid3,
47576b94 7120 .resetvalue = cpu->isar.id_aa64pfr1},
e20d84c1
PM
7121 { .name = "ID_AA64PFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7122 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 2,
7123 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7124 .accessfn = access_aa64_tid3,
e20d84c1
PM
7125 .resetvalue = 0 },
7126 { .name = "ID_AA64PFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7127 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 3,
7128 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7129 .accessfn = access_aa64_tid3,
e20d84c1 7130 .resetvalue = 0 },
9516d772 7131 { .name = "ID_AA64ZFR0_EL1", .state = ARM_CP_STATE_AA64,
e20d84c1
PM
7132 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 4,
7133 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7134 .accessfn = access_aa64_tid3,
9516d772 7135 /* At present, only SVEver == 0 is defined anyway. */
e20d84c1
PM
7136 .resetvalue = 0 },
7137 { .name = "ID_AA64PFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7138 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 5,
7139 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7140 .accessfn = access_aa64_tid3,
e20d84c1
PM
7141 .resetvalue = 0 },
7142 { .name = "ID_AA64PFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7143 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 6,
7144 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7145 .accessfn = access_aa64_tid3,
e20d84c1
PM
7146 .resetvalue = 0 },
7147 { .name = "ID_AA64PFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7148 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 7,
7149 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7150 .accessfn = access_aa64_tid3,
e20d84c1 7151 .resetvalue = 0 },
e60cef86
PM
7152 { .name = "ID_AA64DFR0_EL1", .state = ARM_CP_STATE_AA64,
7153 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 0,
7154 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7155 .accessfn = access_aa64_tid3,
2a609df8 7156 .resetvalue = cpu->isar.id_aa64dfr0 },
e60cef86
PM
7157 { .name = "ID_AA64DFR1_EL1", .state = ARM_CP_STATE_AA64,
7158 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 1,
7159 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7160 .accessfn = access_aa64_tid3,
2a609df8 7161 .resetvalue = cpu->isar.id_aa64dfr1 },
e20d84c1
PM
7162 { .name = "ID_AA64DFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7163 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 2,
7164 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7165 .accessfn = access_aa64_tid3,
e20d84c1
PM
7166 .resetvalue = 0 },
7167 { .name = "ID_AA64DFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7168 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 3,
7169 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7170 .accessfn = access_aa64_tid3,
e20d84c1 7171 .resetvalue = 0 },
e60cef86
PM
7172 { .name = "ID_AA64AFR0_EL1", .state = ARM_CP_STATE_AA64,
7173 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 4,
7174 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7175 .accessfn = access_aa64_tid3,
e60cef86
PM
7176 .resetvalue = cpu->id_aa64afr0 },
7177 { .name = "ID_AA64AFR1_EL1", .state = ARM_CP_STATE_AA64,
7178 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 5,
7179 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7180 .accessfn = access_aa64_tid3,
e60cef86 7181 .resetvalue = cpu->id_aa64afr1 },
e20d84c1
PM
7182 { .name = "ID_AA64AFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7183 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 6,
7184 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7185 .accessfn = access_aa64_tid3,
e20d84c1
PM
7186 .resetvalue = 0 },
7187 { .name = "ID_AA64AFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7188 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 7,
7189 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7190 .accessfn = access_aa64_tid3,
e20d84c1 7191 .resetvalue = 0 },
e60cef86
PM
7192 { .name = "ID_AA64ISAR0_EL1", .state = ARM_CP_STATE_AA64,
7193 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 0,
7194 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7195 .accessfn = access_aa64_tid3,
47576b94 7196 .resetvalue = cpu->isar.id_aa64isar0 },
e60cef86
PM
7197 { .name = "ID_AA64ISAR1_EL1", .state = ARM_CP_STATE_AA64,
7198 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 1,
7199 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7200 .accessfn = access_aa64_tid3,
47576b94 7201 .resetvalue = cpu->isar.id_aa64isar1 },
e20d84c1
PM
7202 { .name = "ID_AA64ISAR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7203 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 2,
7204 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7205 .accessfn = access_aa64_tid3,
e20d84c1
PM
7206 .resetvalue = 0 },
7207 { .name = "ID_AA64ISAR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7208 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 3,
7209 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7210 .accessfn = access_aa64_tid3,
e20d84c1
PM
7211 .resetvalue = 0 },
7212 { .name = "ID_AA64ISAR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7213 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 4,
7214 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7215 .accessfn = access_aa64_tid3,
e20d84c1
PM
7216 .resetvalue = 0 },
7217 { .name = "ID_AA64ISAR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7218 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 5,
7219 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7220 .accessfn = access_aa64_tid3,
e20d84c1
PM
7221 .resetvalue = 0 },
7222 { .name = "ID_AA64ISAR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7223 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 6,
7224 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7225 .accessfn = access_aa64_tid3,
e20d84c1
PM
7226 .resetvalue = 0 },
7227 { .name = "ID_AA64ISAR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7228 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 7,
7229 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7230 .accessfn = access_aa64_tid3,
e20d84c1 7231 .resetvalue = 0 },
e60cef86
PM
7232 { .name = "ID_AA64MMFR0_EL1", .state = ARM_CP_STATE_AA64,
7233 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
7234 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7235 .accessfn = access_aa64_tid3,
3dc91ddb 7236 .resetvalue = cpu->isar.id_aa64mmfr0 },
e60cef86
PM
7237 { .name = "ID_AA64MMFR1_EL1", .state = ARM_CP_STATE_AA64,
7238 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 1,
7239 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7240 .accessfn = access_aa64_tid3,
3dc91ddb 7241 .resetvalue = cpu->isar.id_aa64mmfr1 },
64761e10 7242 { .name = "ID_AA64MMFR2_EL1", .state = ARM_CP_STATE_AA64,
e20d84c1
PM
7243 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 2,
7244 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7245 .accessfn = access_aa64_tid3,
64761e10 7246 .resetvalue = cpu->isar.id_aa64mmfr2 },
e20d84c1
PM
7247 { .name = "ID_AA64MMFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7248 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 3,
7249 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7250 .accessfn = access_aa64_tid3,
e20d84c1
PM
7251 .resetvalue = 0 },
7252 { .name = "ID_AA64MMFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7253 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 4,
7254 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7255 .accessfn = access_aa64_tid3,
e20d84c1
PM
7256 .resetvalue = 0 },
7257 { .name = "ID_AA64MMFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7258 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 5,
7259 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7260 .accessfn = access_aa64_tid3,
e20d84c1
PM
7261 .resetvalue = 0 },
7262 { .name = "ID_AA64MMFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7263 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 6,
7264 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7265 .accessfn = access_aa64_tid3,
e20d84c1
PM
7266 .resetvalue = 0 },
7267 { .name = "ID_AA64MMFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7268 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 7,
7269 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7270 .accessfn = access_aa64_tid3,
e20d84c1 7271 .resetvalue = 0 },
a50c0f51
PM
7272 { .name = "MVFR0_EL1", .state = ARM_CP_STATE_AA64,
7273 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0,
7274 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7275 .accessfn = access_aa64_tid3,
47576b94 7276 .resetvalue = cpu->isar.mvfr0 },
a50c0f51
PM
7277 { .name = "MVFR1_EL1", .state = ARM_CP_STATE_AA64,
7278 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1,
7279 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7280 .accessfn = access_aa64_tid3,
47576b94 7281 .resetvalue = cpu->isar.mvfr1 },
a50c0f51
PM
7282 { .name = "MVFR2_EL1", .state = ARM_CP_STATE_AA64,
7283 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2,
7284 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7285 .accessfn = access_aa64_tid3,
47576b94 7286 .resetvalue = cpu->isar.mvfr2 },
e20d84c1
PM
7287 { .name = "MVFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7288 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 3,
7289 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7290 .accessfn = access_aa64_tid3,
e20d84c1
PM
7291 .resetvalue = 0 },
7292 { .name = "MVFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7293 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 4,
7294 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7295 .accessfn = access_aa64_tid3,
e20d84c1
PM
7296 .resetvalue = 0 },
7297 { .name = "MVFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7298 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 5,
7299 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7300 .accessfn = access_aa64_tid3,
e20d84c1
PM
7301 .resetvalue = 0 },
7302 { .name = "MVFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7303 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 6,
7304 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7305 .accessfn = access_aa64_tid3,
e20d84c1
PM
7306 .resetvalue = 0 },
7307 { .name = "MVFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7308 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 7,
7309 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7310 .accessfn = access_aa64_tid3,
e20d84c1 7311 .resetvalue = 0 },
4054bfa9
AF
7312 { .name = "PMCEID0", .state = ARM_CP_STATE_AA32,
7313 .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 6,
7314 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
cad86737 7315 .resetvalue = extract64(cpu->pmceid0, 0, 32) },
4054bfa9
AF
7316 { .name = "PMCEID0_EL0", .state = ARM_CP_STATE_AA64,
7317 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 6,
7318 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
7319 .resetvalue = cpu->pmceid0 },
7320 { .name = "PMCEID1", .state = ARM_CP_STATE_AA32,
7321 .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 7,
7322 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
cad86737 7323 .resetvalue = extract64(cpu->pmceid1, 0, 32) },
4054bfa9
AF
7324 { .name = "PMCEID1_EL0", .state = ARM_CP_STATE_AA64,
7325 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 7,
7326 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
7327 .resetvalue = cpu->pmceid1 },
e60cef86
PM
7328 REGINFO_SENTINEL
7329 };
6c5c0fec
AB
7330#ifdef CONFIG_USER_ONLY
7331 ARMCPRegUserSpaceInfo v8_user_idregs[] = {
7332 { .name = "ID_AA64PFR0_EL1",
7333 .exported_bits = 0x000f000f00ff0000,
7334 .fixed_bits = 0x0000000000000011 },
7335 { .name = "ID_AA64PFR1_EL1",
7336 .exported_bits = 0x00000000000000f0 },
d040242e
AB
7337 { .name = "ID_AA64PFR*_EL1_RESERVED",
7338 .is_glob = true },
6c5c0fec
AB
7339 { .name = "ID_AA64ZFR0_EL1" },
7340 { .name = "ID_AA64MMFR0_EL1",
7341 .fixed_bits = 0x00000000ff000000 },
7342 { .name = "ID_AA64MMFR1_EL1" },
d040242e
AB
7343 { .name = "ID_AA64MMFR*_EL1_RESERVED",
7344 .is_glob = true },
6c5c0fec
AB
7345 { .name = "ID_AA64DFR0_EL1",
7346 .fixed_bits = 0x0000000000000006 },
7347 { .name = "ID_AA64DFR1_EL1" },
d040242e
AB
7348 { .name = "ID_AA64DFR*_EL1_RESERVED",
7349 .is_glob = true },
7350 { .name = "ID_AA64AFR*",
7351 .is_glob = true },
6c5c0fec
AB
7352 { .name = "ID_AA64ISAR0_EL1",
7353 .exported_bits = 0x00fffffff0fffff0 },
7354 { .name = "ID_AA64ISAR1_EL1",
7355 .exported_bits = 0x000000f0ffffffff },
d040242e
AB
7356 { .name = "ID_AA64ISAR*_EL1_RESERVED",
7357 .is_glob = true },
6c5c0fec
AB
7358 REGUSERINFO_SENTINEL
7359 };
7360 modify_arm_cp_regs(v8_idregs, v8_user_idregs);
7361#endif
be8e8128
GB
7362 /* RVBAR_EL1 is only implemented if EL1 is the highest EL */
7363 if (!arm_feature(env, ARM_FEATURE_EL3) &&
7364 !arm_feature(env, ARM_FEATURE_EL2)) {
7365 ARMCPRegInfo rvbar = {
7366 .name = "RVBAR_EL1", .state = ARM_CP_STATE_AA64,
7367 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
7368 .type = ARM_CP_CONST, .access = PL1_R, .resetvalue = cpu->rvbar
7369 };
7370 define_one_arm_cp_reg(cpu, &rvbar);
7371 }
e60cef86 7372 define_arm_cp_regs(cpu, v8_idregs);
b0d2b7d0
PM
7373 define_arm_cp_regs(cpu, v8_cp_reginfo);
7374 }
3b685ba7 7375 if (arm_feature(env, ARM_FEATURE_EL2)) {
f0d574d6 7376 uint64_t vmpidr_def = mpidr_read_val(env);
731de9e6
EI
7377 ARMCPRegInfo vpidr_regs[] = {
7378 { .name = "VPIDR", .state = ARM_CP_STATE_AA32,
7379 .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
7380 .access = PL2_RW, .accessfn = access_el3_aa32ns,
36476562
PM
7381 .resetvalue = cpu->midr, .type = ARM_CP_ALIAS,
7382 .fieldoffset = offsetoflow32(CPUARMState, cp15.vpidr_el2) },
731de9e6
EI
7383 { .name = "VPIDR_EL2", .state = ARM_CP_STATE_AA64,
7384 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
7385 .access = PL2_RW, .resetvalue = cpu->midr,
7386 .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) },
f0d574d6
EI
7387 { .name = "VMPIDR", .state = ARM_CP_STATE_AA32,
7388 .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
7389 .access = PL2_RW, .accessfn = access_el3_aa32ns,
36476562
PM
7390 .resetvalue = vmpidr_def, .type = ARM_CP_ALIAS,
7391 .fieldoffset = offsetoflow32(CPUARMState, cp15.vmpidr_el2) },
f0d574d6
EI
7392 { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_AA64,
7393 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
7394 .access = PL2_RW,
7395 .resetvalue = vmpidr_def,
7396 .fieldoffset = offsetof(CPUARMState, cp15.vmpidr_el2) },
731de9e6
EI
7397 REGINFO_SENTINEL
7398 };
7399 define_arm_cp_regs(cpu, vpidr_regs);
4771cd01 7400 define_arm_cp_regs(cpu, el2_cp_reginfo);
ce4afed8
PM
7401 if (arm_feature(env, ARM_FEATURE_V8)) {
7402 define_arm_cp_regs(cpu, el2_v8_cp_reginfo);
7403 }
be8e8128
GB
7404 /* RVBAR_EL2 is only implemented if EL2 is the highest EL */
7405 if (!arm_feature(env, ARM_FEATURE_EL3)) {
7406 ARMCPRegInfo rvbar = {
7407 .name = "RVBAR_EL2", .state = ARM_CP_STATE_AA64,
7408 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 1,
7409 .type = ARM_CP_CONST, .access = PL2_R, .resetvalue = cpu->rvbar
7410 };
7411 define_one_arm_cp_reg(cpu, &rvbar);
7412 }
d42e3c26
EI
7413 } else {
7414 /* If EL2 is missing but higher ELs are enabled, we need to
7415 * register the no_el2 reginfos.
7416 */
7417 if (arm_feature(env, ARM_FEATURE_EL3)) {
f0d574d6
EI
7418 /* When EL3 exists but not EL2, VPIDR and VMPIDR take the value
7419 * of MIDR_EL1 and MPIDR_EL1.
731de9e6
EI
7420 */
7421 ARMCPRegInfo vpidr_regs[] = {
7422 { .name = "VPIDR_EL2", .state = ARM_CP_STATE_BOTH,
7423 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
7424 .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any,
7425 .type = ARM_CP_CONST, .resetvalue = cpu->midr,
7426 .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) },
f0d574d6
EI
7427 { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_BOTH,
7428 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
7429 .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any,
7430 .type = ARM_CP_NO_RAW,
7431 .writefn = arm_cp_write_ignore, .readfn = mpidr_read },
731de9e6
EI
7432 REGINFO_SENTINEL
7433 };
7434 define_arm_cp_regs(cpu, vpidr_regs);
4771cd01 7435 define_arm_cp_regs(cpu, el3_no_el2_cp_reginfo);
ce4afed8
PM
7436 if (arm_feature(env, ARM_FEATURE_V8)) {
7437 define_arm_cp_regs(cpu, el3_no_el2_v8_cp_reginfo);
7438 }
d42e3c26 7439 }
3b685ba7 7440 }
81547d66 7441 if (arm_feature(env, ARM_FEATURE_EL3)) {
0f1a3b24 7442 define_arm_cp_regs(cpu, el3_cp_reginfo);
e24fdd23
PM
7443 ARMCPRegInfo el3_regs[] = {
7444 { .name = "RVBAR_EL3", .state = ARM_CP_STATE_AA64,
7445 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 1,
7446 .type = ARM_CP_CONST, .access = PL3_R, .resetvalue = cpu->rvbar },
7447 { .name = "SCTLR_EL3", .state = ARM_CP_STATE_AA64,
7448 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 0,
7449 .access = PL3_RW,
7450 .raw_writefn = raw_write, .writefn = sctlr_write,
7451 .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[3]),
7452 .resetvalue = cpu->reset_sctlr },
7453 REGINFO_SENTINEL
be8e8128 7454 };
e24fdd23
PM
7455
7456 define_arm_cp_regs(cpu, el3_regs);
81547d66 7457 }
2f027fc5
PM
7458 /* The behaviour of NSACR is sufficiently various that we don't
7459 * try to describe it in a single reginfo:
7460 * if EL3 is 64 bit, then trap to EL3 from S EL1,
7461 * reads as constant 0xc00 from NS EL1 and NS EL2
7462 * if EL3 is 32 bit, then RW at EL3, RO at NS EL1 and NS EL2
7463 * if v7 without EL3, register doesn't exist
7464 * if v8 without EL3, reads as constant 0xc00 from NS EL1 and NS EL2
7465 */
7466 if (arm_feature(env, ARM_FEATURE_EL3)) {
7467 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
7468 ARMCPRegInfo nsacr = {
7469 .name = "NSACR", .type = ARM_CP_CONST,
7470 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
7471 .access = PL1_RW, .accessfn = nsacr_access,
7472 .resetvalue = 0xc00
7473 };
7474 define_one_arm_cp_reg(cpu, &nsacr);
7475 } else {
7476 ARMCPRegInfo nsacr = {
7477 .name = "NSACR",
7478 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
7479 .access = PL3_RW | PL1_R,
7480 .resetvalue = 0,
7481 .fieldoffset = offsetof(CPUARMState, cp15.nsacr)
7482 };
7483 define_one_arm_cp_reg(cpu, &nsacr);
7484 }
7485 } else {
7486 if (arm_feature(env, ARM_FEATURE_V8)) {
7487 ARMCPRegInfo nsacr = {
7488 .name = "NSACR", .type = ARM_CP_CONST,
7489 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
7490 .access = PL1_R,
7491 .resetvalue = 0xc00
7492 };
7493 define_one_arm_cp_reg(cpu, &nsacr);
7494 }
7495 }
7496
452a0955 7497 if (arm_feature(env, ARM_FEATURE_PMSA)) {
6cb0b013
PC
7498 if (arm_feature(env, ARM_FEATURE_V6)) {
7499 /* PMSAv6 not implemented */
7500 assert(arm_feature(env, ARM_FEATURE_V7));
7501 define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
7502 define_arm_cp_regs(cpu, pmsav7_cp_reginfo);
7503 } else {
7504 define_arm_cp_regs(cpu, pmsav5_cp_reginfo);
7505 }
18032bec 7506 } else {
8e5d75c9 7507 define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
18032bec 7508 define_arm_cp_regs(cpu, vmsa_cp_reginfo);
4036b7d1
PM
7509 /* TTCBR2 is introduced with ARMv8.2-AA32HPD. */
7510 if (cpu_isar_feature(aa32_hpd, cpu)) {
ab638a32
RH
7511 define_one_arm_cp_reg(cpu, &ttbcr2_reginfo);
7512 }
18032bec 7513 }
c326b979
PM
7514 if (arm_feature(env, ARM_FEATURE_THUMB2EE)) {
7515 define_arm_cp_regs(cpu, t2ee_cp_reginfo);
7516 }
6cc7a3ae
PM
7517 if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) {
7518 define_arm_cp_regs(cpu, generic_timer_cp_reginfo);
7519 }
4a501606
PM
7520 if (arm_feature(env, ARM_FEATURE_VAPA)) {
7521 define_arm_cp_regs(cpu, vapa_cp_reginfo);
7522 }
c4804214
PM
7523 if (arm_feature(env, ARM_FEATURE_CACHE_TEST_CLEAN)) {
7524 define_arm_cp_regs(cpu, cache_test_clean_cp_reginfo);
7525 }
7526 if (arm_feature(env, ARM_FEATURE_CACHE_DIRTY_REG)) {
7527 define_arm_cp_regs(cpu, cache_dirty_status_cp_reginfo);
7528 }
7529 if (arm_feature(env, ARM_FEATURE_CACHE_BLOCK_OPS)) {
7530 define_arm_cp_regs(cpu, cache_block_ops_cp_reginfo);
7531 }
18032bec
PM
7532 if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
7533 define_arm_cp_regs(cpu, omap_cp_reginfo);
7534 }
34f90529
PM
7535 if (arm_feature(env, ARM_FEATURE_STRONGARM)) {
7536 define_arm_cp_regs(cpu, strongarm_cp_reginfo);
7537 }
1047b9d7
PM
7538 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
7539 define_arm_cp_regs(cpu, xscale_cp_reginfo);
7540 }
7541 if (arm_feature(env, ARM_FEATURE_DUMMY_C15_REGS)) {
7542 define_arm_cp_regs(cpu, dummy_c15_cp_reginfo);
7543 }
7ac681cf
PM
7544 if (arm_feature(env, ARM_FEATURE_LPAE)) {
7545 define_arm_cp_regs(cpu, lpae_cp_reginfo);
7546 }
873b73c0 7547 if (cpu_isar_feature(aa32_jazelle, cpu)) {
f96f3d5f
MZ
7548 define_arm_cp_regs(cpu, jazelle_regs);
7549 }
7884849c
PM
7550 /* Slightly awkwardly, the OMAP and StrongARM cores need all of
7551 * cp15 crn=0 to be writes-ignored, whereas for other cores they should
7552 * be read-only (ie write causes UNDEF exception).
7553 */
7554 {
00a29f3d
PM
7555 ARMCPRegInfo id_pre_v8_midr_cp_reginfo[] = {
7556 /* Pre-v8 MIDR space.
7557 * Note that the MIDR isn't a simple constant register because
7884849c
PM
7558 * of the TI925 behaviour where writes to another register can
7559 * cause the MIDR value to change.
97ce8d61
PC
7560 *
7561 * Unimplemented registers in the c15 0 0 0 space default to
7562 * MIDR. Define MIDR first as this entire space, then CTR, TCMTR
7563 * and friends override accordingly.
7884849c
PM
7564 */
7565 { .name = "MIDR",
97ce8d61 7566 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = CP_ANY,
7884849c 7567 .access = PL1_R, .resetvalue = cpu->midr,
d4e6df63 7568 .writefn = arm_cp_write_ignore, .raw_writefn = raw_write,
731de9e6 7569 .readfn = midr_read,
97ce8d61
PC
7570 .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
7571 .type = ARM_CP_OVERRIDE },
7884849c
PM
7572 /* crn = 0 op1 = 0 crm = 3..7 : currently unassigned; we RAZ. */
7573 { .name = "DUMMY",
7574 .cp = 15, .crn = 0, .crm = 3, .opc1 = 0, .opc2 = CP_ANY,
7575 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
7576 { .name = "DUMMY",
7577 .cp = 15, .crn = 0, .crm = 4, .opc1 = 0, .opc2 = CP_ANY,
7578 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
7579 { .name = "DUMMY",
7580 .cp = 15, .crn = 0, .crm = 5, .opc1 = 0, .opc2 = CP_ANY,
7581 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
7582 { .name = "DUMMY",
7583 .cp = 15, .crn = 0, .crm = 6, .opc1 = 0, .opc2 = CP_ANY,
7584 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
7585 { .name = "DUMMY",
7586 .cp = 15, .crn = 0, .crm = 7, .opc1 = 0, .opc2 = CP_ANY,
7587 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
7588 REGINFO_SENTINEL
7589 };
00a29f3d 7590 ARMCPRegInfo id_v8_midr_cp_reginfo[] = {
00a29f3d
PM
7591 { .name = "MIDR_EL1", .state = ARM_CP_STATE_BOTH,
7592 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 0,
731de9e6
EI
7593 .access = PL1_R, .type = ARM_CP_NO_RAW, .resetvalue = cpu->midr,
7594 .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
7595 .readfn = midr_read },
ac00c79f
SF
7596 /* crn = 0 op1 = 0 crm = 0 op2 = 4,7 : AArch32 aliases of MIDR */
7597 { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
7598 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
7599 .access = PL1_R, .resetvalue = cpu->midr },
7600 { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
7601 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 7,
7602 .access = PL1_R, .resetvalue = cpu->midr },
00a29f3d
PM
7603 { .name = "REVIDR_EL1", .state = ARM_CP_STATE_BOTH,
7604 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 6,
93fbc983
MZ
7605 .access = PL1_R,
7606 .accessfn = access_aa64_tid1,
7607 .type = ARM_CP_CONST, .resetvalue = cpu->revidr },
00a29f3d
PM
7608 REGINFO_SENTINEL
7609 };
7610 ARMCPRegInfo id_cp_reginfo[] = {
7611 /* These are common to v8 and pre-v8 */
7612 { .name = "CTR",
7613 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 1,
630fcd4d
MZ
7614 .access = PL1_R, .accessfn = ctr_el0_access,
7615 .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
00a29f3d
PM
7616 { .name = "CTR_EL0", .state = ARM_CP_STATE_AA64,
7617 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 0, .crm = 0,
7618 .access = PL0_R, .accessfn = ctr_el0_access,
7619 .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
7620 /* TCMTR and TLBTR exist in v8 but have no 64-bit versions */
7621 { .name = "TCMTR",
7622 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 2,
93fbc983
MZ
7623 .access = PL1_R,
7624 .accessfn = access_aa32_tid1,
7625 .type = ARM_CP_CONST, .resetvalue = 0 },
00a29f3d
PM
7626 REGINFO_SENTINEL
7627 };
8085ce63
PC
7628 /* TLBTR is specific to VMSA */
7629 ARMCPRegInfo id_tlbtr_reginfo = {
7630 .name = "TLBTR",
7631 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 3,
93fbc983
MZ
7632 .access = PL1_R,
7633 .accessfn = access_aa32_tid1,
7634 .type = ARM_CP_CONST, .resetvalue = 0,
8085ce63 7635 };
3281af81
PC
7636 /* MPUIR is specific to PMSA V6+ */
7637 ARMCPRegInfo id_mpuir_reginfo = {
7638 .name = "MPUIR",
7639 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
7640 .access = PL1_R, .type = ARM_CP_CONST,
7641 .resetvalue = cpu->pmsav7_dregion << 8
7642 };
7884849c
PM
7643 ARMCPRegInfo crn0_wi_reginfo = {
7644 .name = "CRN0_WI", .cp = 15, .crn = 0, .crm = CP_ANY,
7645 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_W,
7646 .type = ARM_CP_NOP | ARM_CP_OVERRIDE
7647 };
6c5c0fec
AB
7648#ifdef CONFIG_USER_ONLY
7649 ARMCPRegUserSpaceInfo id_v8_user_midr_cp_reginfo[] = {
7650 { .name = "MIDR_EL1",
7651 .exported_bits = 0x00000000ffffffff },
7652 { .name = "REVIDR_EL1" },
7653 REGUSERINFO_SENTINEL
7654 };
7655 modify_arm_cp_regs(id_v8_midr_cp_reginfo, id_v8_user_midr_cp_reginfo);
7656#endif
7884849c
PM
7657 if (arm_feature(env, ARM_FEATURE_OMAPCP) ||
7658 arm_feature(env, ARM_FEATURE_STRONGARM)) {
7659 ARMCPRegInfo *r;
7660 /* Register the blanket "writes ignored" value first to cover the
a703eda1
PC
7661 * whole space. Then update the specific ID registers to allow write
7662 * access, so that they ignore writes rather than causing them to
7663 * UNDEF.
7884849c
PM
7664 */
7665 define_one_arm_cp_reg(cpu, &crn0_wi_reginfo);
00a29f3d
PM
7666 for (r = id_pre_v8_midr_cp_reginfo;
7667 r->type != ARM_CP_SENTINEL; r++) {
7668 r->access = PL1_RW;
7669 }
7884849c
PM
7670 for (r = id_cp_reginfo; r->type != ARM_CP_SENTINEL; r++) {
7671 r->access = PL1_RW;
7884849c 7672 }
10006112 7673 id_mpuir_reginfo.access = PL1_RW;
3281af81 7674 id_tlbtr_reginfo.access = PL1_RW;
7884849c 7675 }
00a29f3d
PM
7676 if (arm_feature(env, ARM_FEATURE_V8)) {
7677 define_arm_cp_regs(cpu, id_v8_midr_cp_reginfo);
7678 } else {
7679 define_arm_cp_regs(cpu, id_pre_v8_midr_cp_reginfo);
7680 }
a703eda1 7681 define_arm_cp_regs(cpu, id_cp_reginfo);
452a0955 7682 if (!arm_feature(env, ARM_FEATURE_PMSA)) {
8085ce63 7683 define_one_arm_cp_reg(cpu, &id_tlbtr_reginfo);
3281af81
PC
7684 } else if (arm_feature(env, ARM_FEATURE_V7)) {
7685 define_one_arm_cp_reg(cpu, &id_mpuir_reginfo);
8085ce63 7686 }
7884849c
PM
7687 }
7688
97ce8d61 7689 if (arm_feature(env, ARM_FEATURE_MPIDR)) {
52264166
AB
7690 ARMCPRegInfo mpidr_cp_reginfo[] = {
7691 { .name = "MPIDR_EL1", .state = ARM_CP_STATE_BOTH,
7692 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 5,
7693 .access = PL1_R, .readfn = mpidr_read, .type = ARM_CP_NO_RAW },
7694 REGINFO_SENTINEL
7695 };
7696#ifdef CONFIG_USER_ONLY
7697 ARMCPRegUserSpaceInfo mpidr_user_cp_reginfo[] = {
7698 { .name = "MPIDR_EL1",
7699 .fixed_bits = 0x0000000080000000 },
7700 REGUSERINFO_SENTINEL
7701 };
7702 modify_arm_cp_regs(mpidr_cp_reginfo, mpidr_user_cp_reginfo);
7703#endif
97ce8d61
PC
7704 define_arm_cp_regs(cpu, mpidr_cp_reginfo);
7705 }
7706
2771db27 7707 if (arm_feature(env, ARM_FEATURE_AUXCR)) {
834a6c69
PM
7708 ARMCPRegInfo auxcr_reginfo[] = {
7709 { .name = "ACTLR_EL1", .state = ARM_CP_STATE_BOTH,
7710 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 1,
7711 .access = PL1_RW, .type = ARM_CP_CONST,
7712 .resetvalue = cpu->reset_auxcr },
7713 { .name = "ACTLR_EL2", .state = ARM_CP_STATE_BOTH,
7714 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 1,
7715 .access = PL2_RW, .type = ARM_CP_CONST,
7716 .resetvalue = 0 },
7717 { .name = "ACTLR_EL3", .state = ARM_CP_STATE_AA64,
7718 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 1,
7719 .access = PL3_RW, .type = ARM_CP_CONST,
7720 .resetvalue = 0 },
7721 REGINFO_SENTINEL
2771db27 7722 };
834a6c69 7723 define_arm_cp_regs(cpu, auxcr_reginfo);
f6287c24
PM
7724 if (cpu_isar_feature(aa32_ac2, cpu)) {
7725 define_arm_cp_regs(cpu, actlr2_hactlr2_reginfo);
0e0456ab 7726 }
2771db27
PM
7727 }
7728
d8ba780b 7729 if (arm_feature(env, ARM_FEATURE_CBAR)) {
d56974af
LM
7730 /*
7731 * CBAR is IMPDEF, but common on Arm Cortex-A implementations.
7732 * There are two flavours:
7733 * (1) older 32-bit only cores have a simple 32-bit CBAR
7734 * (2) 64-bit cores have a 64-bit CBAR visible to AArch64, plus a
7735 * 32-bit register visible to AArch32 at a different encoding
7736 * to the "flavour 1" register and with the bits rearranged to
7737 * be able to squash a 64-bit address into the 32-bit view.
7738 * We distinguish the two via the ARM_FEATURE_AARCH64 flag, but
7739 * in future if we support AArch32-only configs of some of the
7740 * AArch64 cores we might need to add a specific feature flag
7741 * to indicate cores with "flavour 2" CBAR.
7742 */
f318cec6
PM
7743 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
7744 /* 32 bit view is [31:18] 0...0 [43:32]. */
7745 uint32_t cbar32 = (extract64(cpu->reset_cbar, 18, 14) << 18)
7746 | extract64(cpu->reset_cbar, 32, 12);
7747 ARMCPRegInfo cbar_reginfo[] = {
7748 { .name = "CBAR",
7749 .type = ARM_CP_CONST,
d56974af
LM
7750 .cp = 15, .crn = 15, .crm = 3, .opc1 = 1, .opc2 = 0,
7751 .access = PL1_R, .resetvalue = cbar32 },
f318cec6
PM
7752 { .name = "CBAR_EL1", .state = ARM_CP_STATE_AA64,
7753 .type = ARM_CP_CONST,
7754 .opc0 = 3, .opc1 = 1, .crn = 15, .crm = 3, .opc2 = 0,
d56974af 7755 .access = PL1_R, .resetvalue = cpu->reset_cbar },
f318cec6
PM
7756 REGINFO_SENTINEL
7757 };
7758 /* We don't implement a r/w 64 bit CBAR currently */
7759 assert(arm_feature(env, ARM_FEATURE_CBAR_RO));
7760 define_arm_cp_regs(cpu, cbar_reginfo);
7761 } else {
7762 ARMCPRegInfo cbar = {
7763 .name = "CBAR",
7764 .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0,
7765 .access = PL1_R|PL3_W, .resetvalue = cpu->reset_cbar,
7766 .fieldoffset = offsetof(CPUARMState,
7767 cp15.c15_config_base_address)
7768 };
7769 if (arm_feature(env, ARM_FEATURE_CBAR_RO)) {
7770 cbar.access = PL1_R;
7771 cbar.fieldoffset = 0;
7772 cbar.type = ARM_CP_CONST;
7773 }
7774 define_one_arm_cp_reg(cpu, &cbar);
7775 }
d8ba780b
PC
7776 }
7777
91db4642
CLG
7778 if (arm_feature(env, ARM_FEATURE_VBAR)) {
7779 ARMCPRegInfo vbar_cp_reginfo[] = {
7780 { .name = "VBAR", .state = ARM_CP_STATE_BOTH,
7781 .opc0 = 3, .crn = 12, .crm = 0, .opc1 = 0, .opc2 = 0,
7782 .access = PL1_RW, .writefn = vbar_write,
7783 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.vbar_s),
7784 offsetof(CPUARMState, cp15.vbar_ns) },
7785 .resetvalue = 0 },
7786 REGINFO_SENTINEL
7787 };
7788 define_arm_cp_regs(cpu, vbar_cp_reginfo);
7789 }
7790
2771db27
PM
7791 /* Generic registers whose values depend on the implementation */
7792 {
7793 ARMCPRegInfo sctlr = {
5ebafdf3 7794 .name = "SCTLR", .state = ARM_CP_STATE_BOTH,
137feaa9 7795 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
84929218 7796 .access = PL1_RW, .accessfn = access_tvm_trvm,
137feaa9
FA
7797 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.sctlr_s),
7798 offsetof(CPUARMState, cp15.sctlr_ns) },
d4e6df63
PM
7799 .writefn = sctlr_write, .resetvalue = cpu->reset_sctlr,
7800 .raw_writefn = raw_write,
2771db27
PM
7801 };
7802 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
7803 /* Normally we would always end the TB on an SCTLR write, but Linux
7804 * arch/arm/mach-pxa/sleep.S expects two instructions following
7805 * an MMU enable to execute from cache. Imitate this behaviour.
7806 */
7807 sctlr.type |= ARM_CP_SUPPRESS_TB_END;
7808 }
7809 define_one_arm_cp_reg(cpu, &sctlr);
7810 }
5be5e8ed 7811
2d7137c1 7812 if (cpu_isar_feature(aa64_lor, cpu)) {
2d7137c1
RH
7813 define_arm_cp_regs(cpu, lor_reginfo);
7814 }
220f508f
RH
7815 if (cpu_isar_feature(aa64_pan, cpu)) {
7816 define_one_arm_cp_reg(cpu, &pan_reginfo);
7817 }
04b07d29
RH
7818#ifndef CONFIG_USER_ONLY
7819 if (cpu_isar_feature(aa64_ats1e1, cpu)) {
7820 define_arm_cp_regs(cpu, ats1e1_reginfo);
7821 }
7822 if (cpu_isar_feature(aa32_ats1e1, cpu)) {
7823 define_arm_cp_regs(cpu, ats1cp_reginfo);
7824 }
7825#endif
9eeb7a1c
RH
7826 if (cpu_isar_feature(aa64_uao, cpu)) {
7827 define_one_arm_cp_reg(cpu, &uao_reginfo);
7828 }
2d7137c1 7829
e2a1a461
RH
7830 if (arm_feature(env, ARM_FEATURE_EL2) && cpu_isar_feature(aa64_vh, cpu)) {
7831 define_arm_cp_regs(cpu, vhe_reginfo);
7832 }
7833
cd208a1c 7834 if (cpu_isar_feature(aa64_sve, cpu)) {
5be5e8ed
RH
7835 define_one_arm_cp_reg(cpu, &zcr_el1_reginfo);
7836 if (arm_feature(env, ARM_FEATURE_EL2)) {
7837 define_one_arm_cp_reg(cpu, &zcr_el2_reginfo);
7838 } else {
7839 define_one_arm_cp_reg(cpu, &zcr_no_el2_reginfo);
7840 }
7841 if (arm_feature(env, ARM_FEATURE_EL3)) {
7842 define_one_arm_cp_reg(cpu, &zcr_el3_reginfo);
7843 }
7844 }
967aa94f
RH
7845
7846#ifdef TARGET_AARCH64
7847 if (cpu_isar_feature(aa64_pauth, cpu)) {
7848 define_arm_cp_regs(cpu, pauth_reginfo);
7849 }
de390645
RH
7850 if (cpu_isar_feature(aa64_rndr, cpu)) {
7851 define_arm_cp_regs(cpu, rndr_reginfo);
7852 }
0d57b499
BM
7853#ifndef CONFIG_USER_ONLY
7854 /* Data Cache clean instructions up to PoP */
7855 if (cpu_isar_feature(aa64_dcpop, cpu)) {
7856 define_one_arm_cp_reg(cpu, dcpop_reg);
7857
7858 if (cpu_isar_feature(aa64_dcpodp, cpu)) {
7859 define_one_arm_cp_reg(cpu, dcpodp_reg);
7860 }
7861 }
7862#endif /*CONFIG_USER_ONLY*/
967aa94f 7863#endif
cb570bd3 7864
22e57073 7865 if (cpu_isar_feature(any_predinv, cpu)) {
cb570bd3
RH
7866 define_arm_cp_regs(cpu, predinv_reginfo);
7867 }
e2cce18f 7868
957e6155
PM
7869 if (cpu_isar_feature(any_ccidx, cpu)) {
7870 define_arm_cp_regs(cpu, ccsidr2_reginfo);
7871 }
7872
e2cce18f
RH
7873#ifndef CONFIG_USER_ONLY
7874 /*
7875 * Register redirections and aliases must be done last,
7876 * after the registers from the other extensions have been defined.
7877 */
7878 if (arm_feature(env, ARM_FEATURE_EL2) && cpu_isar_feature(aa64_vh, cpu)) {
7879 define_arm_vh_e2h_redirects_aliases(cpu);
7880 }
7881#endif
2ceb98c0
PM
7882}
7883
14969266
AF
7884void arm_cpu_register_gdb_regs_for_features(ARMCPU *cpu)
7885{
22169d41 7886 CPUState *cs = CPU(cpu);
14969266
AF
7887 CPUARMState *env = &cpu->env;
7888
6a669427
PM
7889 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
7890 gdb_register_coprocessor(cs, aarch64_fpu_gdb_get_reg,
7891 aarch64_fpu_gdb_set_reg,
7892 34, "aarch64-fpu.xml", 0);
7893 } else if (arm_feature(env, ARM_FEATURE_NEON)) {
22169d41 7894 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
56aebc89 7895 51, "arm-neon.xml", 0);
a6627f5f 7896 } else if (cpu_isar_feature(aa32_simd_r32, cpu)) {
22169d41 7897 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
56aebc89 7898 35, "arm-vfp3.xml", 0);
7fbc6a40 7899 } else if (cpu_isar_feature(aa32_vfp_simd, cpu)) {
22169d41 7900 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
56aebc89
PB
7901 19, "arm-vfp.xml", 0);
7902 }
200bf5b7
AB
7903 gdb_register_coprocessor(cs, arm_gdb_get_sysreg, arm_gdb_set_sysreg,
7904 arm_gen_dynamic_xml(cs),
7905 "system-registers.xml", 0);
40f137e1
PB
7906}
7907
777dc784
PM
7908/* Sort alphabetically by type name, except for "any". */
7909static gint arm_cpu_list_compare(gconstpointer a, gconstpointer b)
5adb4839 7910{
777dc784
PM
7911 ObjectClass *class_a = (ObjectClass *)a;
7912 ObjectClass *class_b = (ObjectClass *)b;
7913 const char *name_a, *name_b;
5adb4839 7914
777dc784
PM
7915 name_a = object_class_get_name(class_a);
7916 name_b = object_class_get_name(class_b);
51492fd1 7917 if (strcmp(name_a, "any-" TYPE_ARM_CPU) == 0) {
777dc784 7918 return 1;
51492fd1 7919 } else if (strcmp(name_b, "any-" TYPE_ARM_CPU) == 0) {
777dc784
PM
7920 return -1;
7921 } else {
7922 return strcmp(name_a, name_b);
5adb4839
PB
7923 }
7924}
7925
777dc784 7926static void arm_cpu_list_entry(gpointer data, gpointer user_data)
40f137e1 7927{
777dc784 7928 ObjectClass *oc = data;
51492fd1
AF
7929 const char *typename;
7930 char *name;
3371d272 7931
51492fd1
AF
7932 typename = object_class_get_name(oc);
7933 name = g_strndup(typename, strlen(typename) - strlen("-" TYPE_ARM_CPU));
0442428a 7934 qemu_printf(" %s\n", name);
51492fd1 7935 g_free(name);
777dc784
PM
7936}
7937
0442428a 7938void arm_cpu_list(void)
777dc784 7939{
777dc784
PM
7940 GSList *list;
7941
7942 list = object_class_get_list(TYPE_ARM_CPU, false);
7943 list = g_slist_sort(list, arm_cpu_list_compare);
0442428a
MA
7944 qemu_printf("Available CPUs:\n");
7945 g_slist_foreach(list, arm_cpu_list_entry, NULL);
777dc784 7946 g_slist_free(list);
40f137e1
PB
7947}
7948
78027bb6
CR
7949static void arm_cpu_add_definition(gpointer data, gpointer user_data)
7950{
7951 ObjectClass *oc = data;
7952 CpuDefinitionInfoList **cpu_list = user_data;
7953 CpuDefinitionInfoList *entry;
7954 CpuDefinitionInfo *info;
7955 const char *typename;
7956
7957 typename = object_class_get_name(oc);
7958 info = g_malloc0(sizeof(*info));
7959 info->name = g_strndup(typename,
7960 strlen(typename) - strlen("-" TYPE_ARM_CPU));
8ed877b7 7961 info->q_typename = g_strdup(typename);
78027bb6
CR
7962
7963 entry = g_malloc0(sizeof(*entry));
7964 entry->value = info;
7965 entry->next = *cpu_list;
7966 *cpu_list = entry;
7967}
7968
25a9d6ca 7969CpuDefinitionInfoList *qmp_query_cpu_definitions(Error **errp)
78027bb6
CR
7970{
7971 CpuDefinitionInfoList *cpu_list = NULL;
7972 GSList *list;
7973
7974 list = object_class_get_list(TYPE_ARM_CPU, false);
7975 g_slist_foreach(list, arm_cpu_add_definition, &cpu_list);
7976 g_slist_free(list);
7977
7978 return cpu_list;
7979}
7980
6e6efd61 7981static void add_cpreg_to_hashtable(ARMCPU *cpu, const ARMCPRegInfo *r,
51a79b03 7982 void *opaque, int state, int secstate,
9c513e78
AB
7983 int crm, int opc1, int opc2,
7984 const char *name)
6e6efd61
PM
7985{
7986 /* Private utility function for define_one_arm_cp_reg_with_opaque():
7987 * add a single reginfo struct to the hash table.
7988 */
7989 uint32_t *key = g_new(uint32_t, 1);
7990 ARMCPRegInfo *r2 = g_memdup(r, sizeof(ARMCPRegInfo));
7991 int is64 = (r->type & ARM_CP_64BIT) ? 1 : 0;
3f3c82a5
FA
7992 int ns = (secstate & ARM_CP_SECSTATE_NS) ? 1 : 0;
7993
9c513e78 7994 r2->name = g_strdup(name);
3f3c82a5
FA
7995 /* Reset the secure state to the specific incoming state. This is
7996 * necessary as the register may have been defined with both states.
7997 */
7998 r2->secure = secstate;
7999
8000 if (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1]) {
8001 /* Register is banked (using both entries in array).
8002 * Overwriting fieldoffset as the array is only used to define
8003 * banked registers but later only fieldoffset is used.
f5a0a5a5 8004 */
3f3c82a5
FA
8005 r2->fieldoffset = r->bank_fieldoffsets[ns];
8006 }
8007
8008 if (state == ARM_CP_STATE_AA32) {
8009 if (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1]) {
8010 /* If the register is banked then we don't need to migrate or
8011 * reset the 32-bit instance in certain cases:
8012 *
8013 * 1) If the register has both 32-bit and 64-bit instances then we
8014 * can count on the 64-bit instance taking care of the
8015 * non-secure bank.
8016 * 2) If ARMv8 is enabled then we can count on a 64-bit version
8017 * taking care of the secure bank. This requires that separate
8018 * 32 and 64-bit definitions are provided.
8019 */
8020 if ((r->state == ARM_CP_STATE_BOTH && ns) ||
8021 (arm_feature(&cpu->env, ARM_FEATURE_V8) && !ns)) {
7a0e58fa 8022 r2->type |= ARM_CP_ALIAS;
3f3c82a5
FA
8023 }
8024 } else if ((secstate != r->secure) && !ns) {
8025 /* The register is not banked so we only want to allow migration of
8026 * the non-secure instance.
8027 */
7a0e58fa 8028 r2->type |= ARM_CP_ALIAS;
58a1d8ce 8029 }
3f3c82a5
FA
8030
8031 if (r->state == ARM_CP_STATE_BOTH) {
8032 /* We assume it is a cp15 register if the .cp field is left unset.
8033 */
8034 if (r2->cp == 0) {
8035 r2->cp = 15;
8036 }
8037
f5a0a5a5 8038#ifdef HOST_WORDS_BIGENDIAN
3f3c82a5
FA
8039 if (r2->fieldoffset) {
8040 r2->fieldoffset += sizeof(uint32_t);
8041 }
f5a0a5a5 8042#endif
3f3c82a5 8043 }
f5a0a5a5
PM
8044 }
8045 if (state == ARM_CP_STATE_AA64) {
8046 /* To allow abbreviation of ARMCPRegInfo
8047 * definitions, we treat cp == 0 as equivalent to
8048 * the value for "standard guest-visible sysreg".
58a1d8ce
PM
8049 * STATE_BOTH definitions are also always "standard
8050 * sysreg" in their AArch64 view (the .cp value may
8051 * be non-zero for the benefit of the AArch32 view).
f5a0a5a5 8052 */
58a1d8ce 8053 if (r->cp == 0 || r->state == ARM_CP_STATE_BOTH) {
f5a0a5a5
PM
8054 r2->cp = CP_REG_ARM64_SYSREG_CP;
8055 }
8056 *key = ENCODE_AA64_CP_REG(r2->cp, r2->crn, crm,
8057 r2->opc0, opc1, opc2);
8058 } else {
51a79b03 8059 *key = ENCODE_CP_REG(r2->cp, is64, ns, r2->crn, crm, opc1, opc2);
f5a0a5a5 8060 }
6e6efd61
PM
8061 if (opaque) {
8062 r2->opaque = opaque;
8063 }
67ed771d
PM
8064 /* reginfo passed to helpers is correct for the actual access,
8065 * and is never ARM_CP_STATE_BOTH:
8066 */
8067 r2->state = state;
6e6efd61
PM
8068 /* Make sure reginfo passed to helpers for wildcarded regs
8069 * has the correct crm/opc1/opc2 for this reg, not CP_ANY:
8070 */
8071 r2->crm = crm;
8072 r2->opc1 = opc1;
8073 r2->opc2 = opc2;
8074 /* By convention, for wildcarded registers only the first
8075 * entry is used for migration; the others are marked as
7a0e58fa 8076 * ALIAS so we don't try to transfer the register
6e6efd61 8077 * multiple times. Special registers (ie NOP/WFI) are
7a0e58fa 8078 * never migratable and not even raw-accessible.
6e6efd61 8079 */
7a0e58fa
PM
8080 if ((r->type & ARM_CP_SPECIAL)) {
8081 r2->type |= ARM_CP_NO_RAW;
8082 }
8083 if (((r->crm == CP_ANY) && crm != 0) ||
6e6efd61
PM
8084 ((r->opc1 == CP_ANY) && opc1 != 0) ||
8085 ((r->opc2 == CP_ANY) && opc2 != 0)) {
1f163787 8086 r2->type |= ARM_CP_ALIAS | ARM_CP_NO_GDB;
6e6efd61
PM
8087 }
8088
375421cc
PM
8089 /* Check that raw accesses are either forbidden or handled. Note that
8090 * we can't assert this earlier because the setup of fieldoffset for
8091 * banked registers has to be done first.
8092 */
8093 if (!(r2->type & ARM_CP_NO_RAW)) {
8094 assert(!raw_accessors_invalid(r2));
8095 }
8096
6e6efd61
PM
8097 /* Overriding of an existing definition must be explicitly
8098 * requested.
8099 */
8100 if (!(r->type & ARM_CP_OVERRIDE)) {
8101 ARMCPRegInfo *oldreg;
8102 oldreg = g_hash_table_lookup(cpu->cp_regs, key);
8103 if (oldreg && !(oldreg->type & ARM_CP_OVERRIDE)) {
8104 fprintf(stderr, "Register redefined: cp=%d %d bit "
8105 "crn=%d crm=%d opc1=%d opc2=%d, "
8106 "was %s, now %s\n", r2->cp, 32 + 32 * is64,
8107 r2->crn, r2->crm, r2->opc1, r2->opc2,
8108 oldreg->name, r2->name);
8109 g_assert_not_reached();
8110 }
8111 }
8112 g_hash_table_insert(cpu->cp_regs, key, r2);
8113}
8114
8115
4b6a83fb
PM
8116void define_one_arm_cp_reg_with_opaque(ARMCPU *cpu,
8117 const ARMCPRegInfo *r, void *opaque)
8118{
8119 /* Define implementations of coprocessor registers.
8120 * We store these in a hashtable because typically
8121 * there are less than 150 registers in a space which
8122 * is 16*16*16*8*8 = 262144 in size.
8123 * Wildcarding is supported for the crm, opc1 and opc2 fields.
8124 * If a register is defined twice then the second definition is
8125 * used, so this can be used to define some generic registers and
8126 * then override them with implementation specific variations.
8127 * At least one of the original and the second definition should
8128 * include ARM_CP_OVERRIDE in its type bits -- this is just a guard
8129 * against accidental use.
f5a0a5a5
PM
8130 *
8131 * The state field defines whether the register is to be
8132 * visible in the AArch32 or AArch64 execution state. If the
8133 * state is set to ARM_CP_STATE_BOTH then we synthesise a
8134 * reginfo structure for the AArch32 view, which sees the lower
8135 * 32 bits of the 64 bit register.
8136 *
8137 * Only registers visible in AArch64 may set r->opc0; opc0 cannot
8138 * be wildcarded. AArch64 registers are always considered to be 64
8139 * bits; the ARM_CP_64BIT* flag applies only to the AArch32 view of
8140 * the register, if any.
4b6a83fb 8141 */
f5a0a5a5 8142 int crm, opc1, opc2, state;
4b6a83fb
PM
8143 int crmmin = (r->crm == CP_ANY) ? 0 : r->crm;
8144 int crmmax = (r->crm == CP_ANY) ? 15 : r->crm;
8145 int opc1min = (r->opc1 == CP_ANY) ? 0 : r->opc1;
8146 int opc1max = (r->opc1 == CP_ANY) ? 7 : r->opc1;
8147 int opc2min = (r->opc2 == CP_ANY) ? 0 : r->opc2;
8148 int opc2max = (r->opc2 == CP_ANY) ? 7 : r->opc2;
8149 /* 64 bit registers have only CRm and Opc1 fields */
8150 assert(!((r->type & ARM_CP_64BIT) && (r->opc2 || r->crn)));
f5a0a5a5
PM
8151 /* op0 only exists in the AArch64 encodings */
8152 assert((r->state != ARM_CP_STATE_AA32) || (r->opc0 == 0));
8153 /* AArch64 regs are all 64 bit so ARM_CP_64BIT is meaningless */
8154 assert((r->state != ARM_CP_STATE_AA64) || !(r->type & ARM_CP_64BIT));
8155 /* The AArch64 pseudocode CheckSystemAccess() specifies that op1
8156 * encodes a minimum access level for the register. We roll this
8157 * runtime check into our general permission check code, so check
8158 * here that the reginfo's specified permissions are strict enough
8159 * to encompass the generic architectural permission check.
8160 */
8161 if (r->state != ARM_CP_STATE_AA32) {
8162 int mask = 0;
8163 switch (r->opc1) {
b5bd7440
AB
8164 case 0:
8165 /* min_EL EL1, but some accessible to EL0 via kernel ABI */
8166 mask = PL0U_R | PL1_RW;
8167 break;
8168 case 1: case 2:
f5a0a5a5
PM
8169 /* min_EL EL1 */
8170 mask = PL1_RW;
8171 break;
8172 case 3:
8173 /* min_EL EL0 */
8174 mask = PL0_RW;
8175 break;
8176 case 4:
b4ecf60f 8177 case 5:
f5a0a5a5
PM
8178 /* min_EL EL2 */
8179 mask = PL2_RW;
8180 break;
f5a0a5a5
PM
8181 case 6:
8182 /* min_EL EL3 */
8183 mask = PL3_RW;
8184 break;
8185 case 7:
8186 /* min_EL EL1, secure mode only (we don't check the latter) */
8187 mask = PL1_RW;
8188 break;
8189 default:
8190 /* broken reginfo with out-of-range opc1 */
8191 assert(false);
8192 break;
8193 }
8194 /* assert our permissions are not too lax (stricter is fine) */
8195 assert((r->access & ~mask) == 0);
8196 }
8197
4b6a83fb
PM
8198 /* Check that the register definition has enough info to handle
8199 * reads and writes if they are permitted.
8200 */
8201 if (!(r->type & (ARM_CP_SPECIAL|ARM_CP_CONST))) {
8202 if (r->access & PL3_R) {
3f3c82a5
FA
8203 assert((r->fieldoffset ||
8204 (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
8205 r->readfn);
4b6a83fb
PM
8206 }
8207 if (r->access & PL3_W) {
3f3c82a5
FA
8208 assert((r->fieldoffset ||
8209 (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
8210 r->writefn);
4b6a83fb
PM
8211 }
8212 }
8213 /* Bad type field probably means missing sentinel at end of reg list */
8214 assert(cptype_valid(r->type));
8215 for (crm = crmmin; crm <= crmmax; crm++) {
8216 for (opc1 = opc1min; opc1 <= opc1max; opc1++) {
8217 for (opc2 = opc2min; opc2 <= opc2max; opc2++) {
f5a0a5a5
PM
8218 for (state = ARM_CP_STATE_AA32;
8219 state <= ARM_CP_STATE_AA64; state++) {
8220 if (r->state != state && r->state != ARM_CP_STATE_BOTH) {
8221 continue;
8222 }
3f3c82a5
FA
8223 if (state == ARM_CP_STATE_AA32) {
8224 /* Under AArch32 CP registers can be common
8225 * (same for secure and non-secure world) or banked.
8226 */
9c513e78
AB
8227 char *name;
8228
3f3c82a5
FA
8229 switch (r->secure) {
8230 case ARM_CP_SECSTATE_S:
8231 case ARM_CP_SECSTATE_NS:
8232 add_cpreg_to_hashtable(cpu, r, opaque, state,
9c513e78
AB
8233 r->secure, crm, opc1, opc2,
8234 r->name);
3f3c82a5
FA
8235 break;
8236 default:
9c513e78 8237 name = g_strdup_printf("%s_S", r->name);
3f3c82a5
FA
8238 add_cpreg_to_hashtable(cpu, r, opaque, state,
8239 ARM_CP_SECSTATE_S,
9c513e78
AB
8240 crm, opc1, opc2, name);
8241 g_free(name);
3f3c82a5
FA
8242 add_cpreg_to_hashtable(cpu, r, opaque, state,
8243 ARM_CP_SECSTATE_NS,
9c513e78 8244 crm, opc1, opc2, r->name);
3f3c82a5
FA
8245 break;
8246 }
8247 } else {
8248 /* AArch64 registers get mapped to non-secure instance
8249 * of AArch32 */
8250 add_cpreg_to_hashtable(cpu, r, opaque, state,
8251 ARM_CP_SECSTATE_NS,
9c513e78 8252 crm, opc1, opc2, r->name);
3f3c82a5 8253 }
f5a0a5a5 8254 }
4b6a83fb
PM
8255 }
8256 }
8257 }
8258}
8259
8260void define_arm_cp_regs_with_opaque(ARMCPU *cpu,
8261 const ARMCPRegInfo *regs, void *opaque)
8262{
8263 /* Define a whole list of registers */
8264 const ARMCPRegInfo *r;
8265 for (r = regs; r->type != ARM_CP_SENTINEL; r++) {
8266 define_one_arm_cp_reg_with_opaque(cpu, r, opaque);
8267 }
8268}
8269
6c5c0fec
AB
8270/*
8271 * Modify ARMCPRegInfo for access from userspace.
8272 *
8273 * This is a data driven modification directed by
8274 * ARMCPRegUserSpaceInfo. All registers become ARM_CP_CONST as
8275 * user-space cannot alter any values and dynamic values pertaining to
8276 * execution state are hidden from user space view anyway.
8277 */
8278void modify_arm_cp_regs(ARMCPRegInfo *regs, const ARMCPRegUserSpaceInfo *mods)
8279{
8280 const ARMCPRegUserSpaceInfo *m;
8281 ARMCPRegInfo *r;
8282
8283 for (m = mods; m->name; m++) {
d040242e
AB
8284 GPatternSpec *pat = NULL;
8285 if (m->is_glob) {
8286 pat = g_pattern_spec_new(m->name);
8287 }
6c5c0fec 8288 for (r = regs; r->type != ARM_CP_SENTINEL; r++) {
d040242e
AB
8289 if (pat && g_pattern_match_string(pat, r->name)) {
8290 r->type = ARM_CP_CONST;
8291 r->access = PL0U_R;
8292 r->resetvalue = 0;
8293 /* continue */
8294 } else if (strcmp(r->name, m->name) == 0) {
6c5c0fec
AB
8295 r->type = ARM_CP_CONST;
8296 r->access = PL0U_R;
8297 r->resetvalue &= m->exported_bits;
8298 r->resetvalue |= m->fixed_bits;
8299 break;
8300 }
8301 }
d040242e
AB
8302 if (pat) {
8303 g_pattern_spec_free(pat);
8304 }
6c5c0fec
AB
8305 }
8306}
8307
60322b39 8308const ARMCPRegInfo *get_arm_cp_reginfo(GHashTable *cpregs, uint32_t encoded_cp)
4b6a83fb 8309{
60322b39 8310 return g_hash_table_lookup(cpregs, &encoded_cp);
4b6a83fb
PM
8311}
8312
c4241c7d
PM
8313void arm_cp_write_ignore(CPUARMState *env, const ARMCPRegInfo *ri,
8314 uint64_t value)
4b6a83fb
PM
8315{
8316 /* Helper coprocessor write function for write-ignore registers */
4b6a83fb
PM
8317}
8318
c4241c7d 8319uint64_t arm_cp_read_zero(CPUARMState *env, const ARMCPRegInfo *ri)
4b6a83fb
PM
8320{
8321 /* Helper coprocessor write function for read-as-zero registers */
4b6a83fb
PM
8322 return 0;
8323}
8324
f5a0a5a5
PM
8325void arm_cp_reset_ignore(CPUARMState *env, const ARMCPRegInfo *opaque)
8326{
8327 /* Helper coprocessor reset function for do-nothing-on-reset registers */
8328}
8329
af393ffc 8330static int bad_mode_switch(CPUARMState *env, int mode, CPSRWriteType write_type)
37064a8b
PM
8331{
8332 /* Return true if it is not valid for us to switch to
8333 * this CPU mode (ie all the UNPREDICTABLE cases in
8334 * the ARM ARM CPSRWriteByInstr pseudocode).
8335 */
af393ffc
PM
8336
8337 /* Changes to or from Hyp via MSR and CPS are illegal. */
8338 if (write_type == CPSRWriteByInstr &&
8339 ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_HYP ||
8340 mode == ARM_CPU_MODE_HYP)) {
8341 return 1;
8342 }
8343
37064a8b
PM
8344 switch (mode) {
8345 case ARM_CPU_MODE_USR:
10eacda7 8346 return 0;
37064a8b
PM
8347 case ARM_CPU_MODE_SYS:
8348 case ARM_CPU_MODE_SVC:
8349 case ARM_CPU_MODE_ABT:
8350 case ARM_CPU_MODE_UND:
8351 case ARM_CPU_MODE_IRQ:
8352 case ARM_CPU_MODE_FIQ:
52ff951b
PM
8353 /* Note that we don't implement the IMPDEF NSACR.RFR which in v7
8354 * allows FIQ mode to be Secure-only. (In v8 this doesn't exist.)
8355 */
10eacda7
PM
8356 /* If HCR.TGE is set then changes from Monitor to NS PL1 via MSR
8357 * and CPS are treated as illegal mode changes.
8358 */
8359 if (write_type == CPSRWriteByInstr &&
10eacda7 8360 (env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON &&
7c208e0f 8361 (arm_hcr_el2_eff(env) & HCR_TGE)) {
10eacda7
PM
8362 return 1;
8363 }
37064a8b 8364 return 0;
e6c8fc07
PM
8365 case ARM_CPU_MODE_HYP:
8366 return !arm_feature(env, ARM_FEATURE_EL2)
2d2a4549 8367 || arm_current_el(env) < 2 || arm_is_secure_below_el3(env);
027fc527 8368 case ARM_CPU_MODE_MON:
58ae2d1f 8369 return arm_current_el(env) < 3;
37064a8b
PM
8370 default:
8371 return 1;
8372 }
8373}
8374
2f4a40e5
AZ
8375uint32_t cpsr_read(CPUARMState *env)
8376{
8377 int ZF;
6fbe23d5
PB
8378 ZF = (env->ZF == 0);
8379 return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) |
2f4a40e5
AZ
8380 (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27)
8381 | (env->thumb << 5) | ((env->condexec_bits & 3) << 25)
8382 | ((env->condexec_bits & 0xfc) << 8)
af519934 8383 | (env->GE << 16) | (env->daif & CPSR_AIF);
2f4a40e5
AZ
8384}
8385
50866ba5
PM
8386void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask,
8387 CPSRWriteType write_type)
2f4a40e5 8388{
6e8801f9
FA
8389 uint32_t changed_daif;
8390
2f4a40e5 8391 if (mask & CPSR_NZCV) {
6fbe23d5
PB
8392 env->ZF = (~val) & CPSR_Z;
8393 env->NF = val;
2f4a40e5
AZ
8394 env->CF = (val >> 29) & 1;
8395 env->VF = (val << 3) & 0x80000000;
8396 }
8397 if (mask & CPSR_Q)
8398 env->QF = ((val & CPSR_Q) != 0);
8399 if (mask & CPSR_T)
8400 env->thumb = ((val & CPSR_T) != 0);
8401 if (mask & CPSR_IT_0_1) {
8402 env->condexec_bits &= ~3;
8403 env->condexec_bits |= (val >> 25) & 3;
8404 }
8405 if (mask & CPSR_IT_2_7) {
8406 env->condexec_bits &= 3;
8407 env->condexec_bits |= (val >> 8) & 0xfc;
8408 }
8409 if (mask & CPSR_GE) {
8410 env->GE = (val >> 16) & 0xf;
8411 }
8412
6e8801f9
FA
8413 /* In a V7 implementation that includes the security extensions but does
8414 * not include Virtualization Extensions the SCR.FW and SCR.AW bits control
8415 * whether non-secure software is allowed to change the CPSR_F and CPSR_A
8416 * bits respectively.
8417 *
8418 * In a V8 implementation, it is permitted for privileged software to
8419 * change the CPSR A/F bits regardless of the SCR.AW/FW bits.
8420 */
f8c88bbc 8421 if (write_type != CPSRWriteRaw && !arm_feature(env, ARM_FEATURE_V8) &&
6e8801f9
FA
8422 arm_feature(env, ARM_FEATURE_EL3) &&
8423 !arm_feature(env, ARM_FEATURE_EL2) &&
8424 !arm_is_secure(env)) {
8425
8426 changed_daif = (env->daif ^ val) & mask;
8427
8428 if (changed_daif & CPSR_A) {
8429 /* Check to see if we are allowed to change the masking of async
8430 * abort exceptions from a non-secure state.
8431 */
8432 if (!(env->cp15.scr_el3 & SCR_AW)) {
8433 qemu_log_mask(LOG_GUEST_ERROR,
8434 "Ignoring attempt to switch CPSR_A flag from "
8435 "non-secure world with SCR.AW bit clear\n");
8436 mask &= ~CPSR_A;
8437 }
8438 }
8439
8440 if (changed_daif & CPSR_F) {
8441 /* Check to see if we are allowed to change the masking of FIQ
8442 * exceptions from a non-secure state.
8443 */
8444 if (!(env->cp15.scr_el3 & SCR_FW)) {
8445 qemu_log_mask(LOG_GUEST_ERROR,
8446 "Ignoring attempt to switch CPSR_F flag from "
8447 "non-secure world with SCR.FW bit clear\n");
8448 mask &= ~CPSR_F;
8449 }
8450
8451 /* Check whether non-maskable FIQ (NMFI) support is enabled.
8452 * If this bit is set software is not allowed to mask
8453 * FIQs, but is allowed to set CPSR_F to 0.
8454 */
8455 if ((A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_NMFI) &&
8456 (val & CPSR_F)) {
8457 qemu_log_mask(LOG_GUEST_ERROR,
8458 "Ignoring attempt to enable CPSR_F flag "
8459 "(non-maskable FIQ [NMFI] support enabled)\n");
8460 mask &= ~CPSR_F;
8461 }
8462 }
8463 }
8464
4cc35614
PM
8465 env->daif &= ~(CPSR_AIF & mask);
8466 env->daif |= val & CPSR_AIF & mask;
8467
f8c88bbc
PM
8468 if (write_type != CPSRWriteRaw &&
8469 ((env->uncached_cpsr ^ val) & mask & CPSR_M)) {
8c4f0eb9
PM
8470 if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_USR) {
8471 /* Note that we can only get here in USR mode if this is a
8472 * gdb stub write; for this case we follow the architectural
8473 * behaviour for guest writes in USR mode of ignoring an attempt
8474 * to switch mode. (Those are caught by translate.c for writes
8475 * triggered by guest instructions.)
8476 */
8477 mask &= ~CPSR_M;
8478 } else if (bad_mode_switch(env, val & CPSR_M, write_type)) {
81907a58
PM
8479 /* Attempt to switch to an invalid mode: this is UNPREDICTABLE in
8480 * v7, and has defined behaviour in v8:
8481 * + leave CPSR.M untouched
8482 * + allow changes to the other CPSR fields
8483 * + set PSTATE.IL
8484 * For user changes via the GDB stub, we don't set PSTATE.IL,
8485 * as this would be unnecessarily harsh for a user error.
37064a8b
PM
8486 */
8487 mask &= ~CPSR_M;
81907a58
PM
8488 if (write_type != CPSRWriteByGDBStub &&
8489 arm_feature(env, ARM_FEATURE_V8)) {
8490 mask |= CPSR_IL;
8491 val |= CPSR_IL;
8492 }
81e37284
PM
8493 qemu_log_mask(LOG_GUEST_ERROR,
8494 "Illegal AArch32 mode switch attempt from %s to %s\n",
8495 aarch32_mode_name(env->uncached_cpsr),
8496 aarch32_mode_name(val));
37064a8b 8497 } else {
81e37284
PM
8498 qemu_log_mask(CPU_LOG_INT, "%s %s to %s PC 0x%" PRIx32 "\n",
8499 write_type == CPSRWriteExceptionReturn ?
8500 "Exception return from AArch32" :
8501 "AArch32 mode switch from",
8502 aarch32_mode_name(env->uncached_cpsr),
8503 aarch32_mode_name(val), env->regs[15]);
37064a8b
PM
8504 switch_mode(env, val & CPSR_M);
8505 }
2f4a40e5
AZ
8506 }
8507 mask &= ~CACHED_CPSR_BITS;
8508 env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask);
8509}
8510
b26eefb6
PB
8511/* Sign/zero extend */
8512uint32_t HELPER(sxtb16)(uint32_t x)
8513{
8514 uint32_t res;
8515 res = (uint16_t)(int8_t)x;
8516 res |= (uint32_t)(int8_t)(x >> 16) << 16;
8517 return res;
8518}
8519
8520uint32_t HELPER(uxtb16)(uint32_t x)
8521{
8522 uint32_t res;
8523 res = (uint16_t)(uint8_t)x;
8524 res |= (uint32_t)(uint8_t)(x >> 16) << 16;
8525 return res;
8526}
8527
3670669c
PB
8528int32_t HELPER(sdiv)(int32_t num, int32_t den)
8529{
8530 if (den == 0)
8531 return 0;
686eeb93
AJ
8532 if (num == INT_MIN && den == -1)
8533 return INT_MIN;
3670669c
PB
8534 return num / den;
8535}
8536
8537uint32_t HELPER(udiv)(uint32_t num, uint32_t den)
8538{
8539 if (den == 0)
8540 return 0;
8541 return num / den;
8542}
8543
8544uint32_t HELPER(rbit)(uint32_t x)
8545{
42fedbca 8546 return revbit32(x);
3670669c
PB
8547}
8548
c47eaf9f 8549#ifdef CONFIG_USER_ONLY
b5ff1b31 8550
affdb64d 8551static void switch_mode(CPUARMState *env, int mode)
b5ff1b31 8552{
2fc0cc0e 8553 ARMCPU *cpu = env_archcpu(env);
a47dddd7
AF
8554
8555 if (mode != ARM_CPU_MODE_USR) {
8556 cpu_abort(CPU(cpu), "Tried to switch out of user mode\n");
8557 }
b5ff1b31
FB
8558}
8559
012a906b
GB
8560uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
8561 uint32_t cur_el, bool secure)
9e729b57
EI
8562{
8563 return 1;
8564}
8565
ce02049d
GB
8566void aarch64_sync_64_to_32(CPUARMState *env)
8567{
8568 g_assert_not_reached();
8569}
8570
b5ff1b31
FB
8571#else
8572
affdb64d 8573static void switch_mode(CPUARMState *env, int mode)
b5ff1b31
FB
8574{
8575 int old_mode;
8576 int i;
8577
8578 old_mode = env->uncached_cpsr & CPSR_M;
8579 if (mode == old_mode)
8580 return;
8581
8582 if (old_mode == ARM_CPU_MODE_FIQ) {
8583 memcpy (env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t));
8637c67f 8584 memcpy (env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t));
b5ff1b31
FB
8585 } else if (mode == ARM_CPU_MODE_FIQ) {
8586 memcpy (env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t));
8637c67f 8587 memcpy (env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t));
b5ff1b31
FB
8588 }
8589
f5206413 8590 i = bank_number(old_mode);
b5ff1b31 8591 env->banked_r13[i] = env->regs[13];
b5ff1b31
FB
8592 env->banked_spsr[i] = env->spsr;
8593
f5206413 8594 i = bank_number(mode);
b5ff1b31 8595 env->regs[13] = env->banked_r13[i];
b5ff1b31 8596 env->spsr = env->banked_spsr[i];
593cfa2b
PM
8597
8598 env->banked_r14[r14_bank_number(old_mode)] = env->regs[14];
8599 env->regs[14] = env->banked_r14[r14_bank_number(mode)];
b5ff1b31
FB
8600}
8601
0eeb17d6
GB
8602/* Physical Interrupt Target EL Lookup Table
8603 *
8604 * [ From ARM ARM section G1.13.4 (Table G1-15) ]
8605 *
8606 * The below multi-dimensional table is used for looking up the target
8607 * exception level given numerous condition criteria. Specifically, the
8608 * target EL is based on SCR and HCR routing controls as well as the
8609 * currently executing EL and secure state.
8610 *
8611 * Dimensions:
8612 * target_el_table[2][2][2][2][2][4]
8613 * | | | | | +--- Current EL
8614 * | | | | +------ Non-secure(0)/Secure(1)
8615 * | | | +--------- HCR mask override
8616 * | | +------------ SCR exec state control
8617 * | +--------------- SCR mask override
8618 * +------------------ 32-bit(0)/64-bit(1) EL3
8619 *
8620 * The table values are as such:
8621 * 0-3 = EL0-EL3
8622 * -1 = Cannot occur
8623 *
8624 * The ARM ARM target EL table includes entries indicating that an "exception
8625 * is not taken". The two cases where this is applicable are:
8626 * 1) An exception is taken from EL3 but the SCR does not have the exception
8627 * routed to EL3.
8628 * 2) An exception is taken from EL2 but the HCR does not have the exception
8629 * routed to EL2.
8630 * In these two cases, the below table contain a target of EL1. This value is
8631 * returned as it is expected that the consumer of the table data will check
8632 * for "target EL >= current EL" to ensure the exception is not taken.
8633 *
8634 * SCR HCR
8635 * 64 EA AMO From
8636 * BIT IRQ IMO Non-secure Secure
8637 * EL3 FIQ RW FMO EL0 EL1 EL2 EL3 EL0 EL1 EL2 EL3
8638 */
82c39f6a 8639static const int8_t target_el_table[2][2][2][2][2][4] = {
0eeb17d6
GB
8640 {{{{/* 0 0 0 0 */{ 1, 1, 2, -1 },{ 3, -1, -1, 3 },},
8641 {/* 0 0 0 1 */{ 2, 2, 2, -1 },{ 3, -1, -1, 3 },},},
8642 {{/* 0 0 1 0 */{ 1, 1, 2, -1 },{ 3, -1, -1, 3 },},
8643 {/* 0 0 1 1 */{ 2, 2, 2, -1 },{ 3, -1, -1, 3 },},},},
8644 {{{/* 0 1 0 0 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},
8645 {/* 0 1 0 1 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},},
8646 {{/* 0 1 1 0 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},
8647 {/* 0 1 1 1 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},},},},
8648 {{{{/* 1 0 0 0 */{ 1, 1, 2, -1 },{ 1, 1, -1, 1 },},
8649 {/* 1 0 0 1 */{ 2, 2, 2, -1 },{ 1, 1, -1, 1 },},},
8650 {{/* 1 0 1 0 */{ 1, 1, 1, -1 },{ 1, 1, -1, 1 },},
8651 {/* 1 0 1 1 */{ 2, 2, 2, -1 },{ 1, 1, -1, 1 },},},},
8652 {{{/* 1 1 0 0 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},
8653 {/* 1 1 0 1 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},},
8654 {{/* 1 1 1 0 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},
8655 {/* 1 1 1 1 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},},},},
8656};
8657
8658/*
8659 * Determine the target EL for physical exceptions
8660 */
012a906b
GB
8661uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
8662 uint32_t cur_el, bool secure)
0eeb17d6
GB
8663{
8664 CPUARMState *env = cs->env_ptr;
f7778444
RH
8665 bool rw;
8666 bool scr;
8667 bool hcr;
0eeb17d6 8668 int target_el;
2cde031f 8669 /* Is the highest EL AArch64? */
f7778444
RH
8670 bool is64 = arm_feature(env, ARM_FEATURE_AARCH64);
8671 uint64_t hcr_el2;
2cde031f
SS
8672
8673 if (arm_feature(env, ARM_FEATURE_EL3)) {
8674 rw = ((env->cp15.scr_el3 & SCR_RW) == SCR_RW);
8675 } else {
8676 /* Either EL2 is the highest EL (and so the EL2 register width
8677 * is given by is64); or there is no EL2 or EL3, in which case
8678 * the value of 'rw' does not affect the table lookup anyway.
8679 */
8680 rw = is64;
8681 }
0eeb17d6 8682
f7778444 8683 hcr_el2 = arm_hcr_el2_eff(env);
0eeb17d6
GB
8684 switch (excp_idx) {
8685 case EXCP_IRQ:
8686 scr = ((env->cp15.scr_el3 & SCR_IRQ) == SCR_IRQ);
f7778444 8687 hcr = hcr_el2 & HCR_IMO;
0eeb17d6
GB
8688 break;
8689 case EXCP_FIQ:
8690 scr = ((env->cp15.scr_el3 & SCR_FIQ) == SCR_FIQ);
f7778444 8691 hcr = hcr_el2 & HCR_FMO;
0eeb17d6
GB
8692 break;
8693 default:
8694 scr = ((env->cp15.scr_el3 & SCR_EA) == SCR_EA);
f7778444 8695 hcr = hcr_el2 & HCR_AMO;
0eeb17d6
GB
8696 break;
8697 };
8698
d1b31428
RH
8699 /*
8700 * For these purposes, TGE and AMO/IMO/FMO both force the
8701 * interrupt to EL2. Fold TGE into the bit extracted above.
8702 */
8703 hcr |= (hcr_el2 & HCR_TGE) != 0;
8704
0eeb17d6
GB
8705 /* Perform a table-lookup for the target EL given the current state */
8706 target_el = target_el_table[is64][scr][rw][hcr][secure][cur_el];
8707
8708 assert(target_el > 0);
8709
8710 return target_el;
8711}
8712
b59f479b
PMD
8713void arm_log_exception(int idx)
8714{
8715 if (qemu_loglevel_mask(CPU_LOG_INT)) {
8716 const char *exc = NULL;
8717 static const char * const excnames[] = {
8718 [EXCP_UDEF] = "Undefined Instruction",
8719 [EXCP_SWI] = "SVC",
8720 [EXCP_PREFETCH_ABORT] = "Prefetch Abort",
8721 [EXCP_DATA_ABORT] = "Data Abort",
8722 [EXCP_IRQ] = "IRQ",
8723 [EXCP_FIQ] = "FIQ",
8724 [EXCP_BKPT] = "Breakpoint",
8725 [EXCP_EXCEPTION_EXIT] = "QEMU v7M exception exit",
8726 [EXCP_KERNEL_TRAP] = "QEMU intercept of kernel commpage",
8727 [EXCP_HVC] = "Hypervisor Call",
8728 [EXCP_HYP_TRAP] = "Hypervisor Trap",
8729 [EXCP_SMC] = "Secure Monitor Call",
8730 [EXCP_VIRQ] = "Virtual IRQ",
8731 [EXCP_VFIQ] = "Virtual FIQ",
8732 [EXCP_SEMIHOST] = "Semihosting call",
8733 [EXCP_NOCP] = "v7M NOCP UsageFault",
8734 [EXCP_INVSTATE] = "v7M INVSTATE UsageFault",
8735 [EXCP_STKOF] = "v8M STKOF UsageFault",
8736 [EXCP_LAZYFP] = "v7M exception during lazy FP stacking",
8737 [EXCP_LSERR] = "v8M LSERR UsageFault",
8738 [EXCP_UNALIGNED] = "v7M UNALIGNED UsageFault",
8739 };
8740
8741 if (idx >= 0 && idx < ARRAY_SIZE(excnames)) {
8742 exc = excnames[idx];
8743 }
8744 if (!exc) {
8745 exc = "unknown";
8746 }
8747 qemu_log_mask(CPU_LOG_INT, "Taking exception %d [%s]\n", idx, exc);
8748 }
8749}
8750
a356dacf 8751/*
7aab5a8c
PMD
8752 * Function used to synchronize QEMU's AArch64 register set with AArch32
8753 * register set. This is necessary when switching between AArch32 and AArch64
8754 * execution state.
a356dacf 8755 */
7aab5a8c 8756void aarch64_sync_32_to_64(CPUARMState *env)
9ee6e8bb 8757{
7aab5a8c
PMD
8758 int i;
8759 uint32_t mode = env->uncached_cpsr & CPSR_M;
8760
8761 /* We can blanket copy R[0:7] to X[0:7] */
8762 for (i = 0; i < 8; i++) {
8763 env->xregs[i] = env->regs[i];
fd592d89 8764 }
70d74660 8765
9a223097 8766 /*
7aab5a8c
PMD
8767 * Unless we are in FIQ mode, x8-x12 come from the user registers r8-r12.
8768 * Otherwise, they come from the banked user regs.
fd592d89 8769 */
7aab5a8c
PMD
8770 if (mode == ARM_CPU_MODE_FIQ) {
8771 for (i = 8; i < 13; i++) {
8772 env->xregs[i] = env->usr_regs[i - 8];
8773 }
8774 } else {
8775 for (i = 8; i < 13; i++) {
8776 env->xregs[i] = env->regs[i];
8777 }
fd592d89 8778 }
9ee6e8bb 8779
7aab5a8c
PMD
8780 /*
8781 * Registers x13-x23 are the various mode SP and FP registers. Registers
8782 * r13 and r14 are only copied if we are in that mode, otherwise we copy
8783 * from the mode banked register.
8784 */
8785 if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
8786 env->xregs[13] = env->regs[13];
8787 env->xregs[14] = env->regs[14];
8788 } else {
8789 env->xregs[13] = env->banked_r13[bank_number(ARM_CPU_MODE_USR)];
8790 /* HYP is an exception in that it is copied from r14 */
8791 if (mode == ARM_CPU_MODE_HYP) {
8792 env->xregs[14] = env->regs[14];
95695eff 8793 } else {
7aab5a8c 8794 env->xregs[14] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)];
95695eff 8795 }
95695eff
PM
8796 }
8797
7aab5a8c
PMD
8798 if (mode == ARM_CPU_MODE_HYP) {
8799 env->xregs[15] = env->regs[13];
8800 } else {
8801 env->xregs[15] = env->banked_r13[bank_number(ARM_CPU_MODE_HYP)];
95695eff
PM
8802 }
8803
7aab5a8c
PMD
8804 if (mode == ARM_CPU_MODE_IRQ) {
8805 env->xregs[16] = env->regs[14];
8806 env->xregs[17] = env->regs[13];
8807 } else {
8808 env->xregs[16] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)];
8809 env->xregs[17] = env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)];
8810 }
95695eff 8811
7aab5a8c
PMD
8812 if (mode == ARM_CPU_MODE_SVC) {
8813 env->xregs[18] = env->regs[14];
8814 env->xregs[19] = env->regs[13];
8815 } else {
8816 env->xregs[18] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)];
8817 env->xregs[19] = env->banked_r13[bank_number(ARM_CPU_MODE_SVC)];
8818 }
95695eff 8819
7aab5a8c
PMD
8820 if (mode == ARM_CPU_MODE_ABT) {
8821 env->xregs[20] = env->regs[14];
8822 env->xregs[21] = env->regs[13];
8823 } else {
8824 env->xregs[20] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)];
8825 env->xregs[21] = env->banked_r13[bank_number(ARM_CPU_MODE_ABT)];
8826 }
e33cf0f8 8827
7aab5a8c
PMD
8828 if (mode == ARM_CPU_MODE_UND) {
8829 env->xregs[22] = env->regs[14];
8830 env->xregs[23] = env->regs[13];
8831 } else {
8832 env->xregs[22] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)];
8833 env->xregs[23] = env->banked_r13[bank_number(ARM_CPU_MODE_UND)];
e33cf0f8
PM
8834 }
8835
8836 /*
7aab5a8c
PMD
8837 * Registers x24-x30 are mapped to r8-r14 in FIQ mode. If we are in FIQ
8838 * mode, then we can copy from r8-r14. Otherwise, we copy from the
8839 * FIQ bank for r8-r14.
e33cf0f8 8840 */
7aab5a8c
PMD
8841 if (mode == ARM_CPU_MODE_FIQ) {
8842 for (i = 24; i < 31; i++) {
8843 env->xregs[i] = env->regs[i - 16]; /* X[24:30] <- R[8:14] */
8844 }
8845 } else {
8846 for (i = 24; i < 29; i++) {
8847 env->xregs[i] = env->fiq_regs[i - 24];
e33cf0f8 8848 }
7aab5a8c
PMD
8849 env->xregs[29] = env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)];
8850 env->xregs[30] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)];
e33cf0f8 8851 }
7aab5a8c
PMD
8852
8853 env->pc = env->regs[15];
e33cf0f8
PM
8854}
8855
9a223097 8856/*
7aab5a8c
PMD
8857 * Function used to synchronize QEMU's AArch32 register set with AArch64
8858 * register set. This is necessary when switching between AArch32 and AArch64
8859 * execution state.
de2db7ec 8860 */
7aab5a8c 8861void aarch64_sync_64_to_32(CPUARMState *env)
9ee6e8bb 8862{
7aab5a8c
PMD
8863 int i;
8864 uint32_t mode = env->uncached_cpsr & CPSR_M;
abc24d86 8865
7aab5a8c
PMD
8866 /* We can blanket copy X[0:7] to R[0:7] */
8867 for (i = 0; i < 8; i++) {
8868 env->regs[i] = env->xregs[i];
de2db7ec 8869 }
3f0cddee 8870
9a223097 8871 /*
7aab5a8c
PMD
8872 * Unless we are in FIQ mode, r8-r12 come from the user registers x8-x12.
8873 * Otherwise, we copy x8-x12 into the banked user regs.
de2db7ec 8874 */
7aab5a8c
PMD
8875 if (mode == ARM_CPU_MODE_FIQ) {
8876 for (i = 8; i < 13; i++) {
8877 env->usr_regs[i - 8] = env->xregs[i];
8878 }
8879 } else {
8880 for (i = 8; i < 13; i++) {
8881 env->regs[i] = env->xregs[i];
8882 }
fb602cb7
PM
8883 }
8884
9a223097 8885 /*
7aab5a8c
PMD
8886 * Registers r13 & r14 depend on the current mode.
8887 * If we are in a given mode, we copy the corresponding x registers to r13
8888 * and r14. Otherwise, we copy the x register to the banked r13 and r14
8889 * for the mode.
fb602cb7 8890 */
7aab5a8c
PMD
8891 if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
8892 env->regs[13] = env->xregs[13];
8893 env->regs[14] = env->xregs[14];
fb602cb7 8894 } else {
7aab5a8c 8895 env->banked_r13[bank_number(ARM_CPU_MODE_USR)] = env->xregs[13];
fb602cb7 8896
7aab5a8c
PMD
8897 /*
8898 * HYP is an exception in that it does not have its own banked r14 but
8899 * shares the USR r14
8900 */
8901 if (mode == ARM_CPU_MODE_HYP) {
8902 env->regs[14] = env->xregs[14];
8903 } else {
8904 env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)] = env->xregs[14];
8905 }
8906 }
fb602cb7 8907
7aab5a8c
PMD
8908 if (mode == ARM_CPU_MODE_HYP) {
8909 env->regs[13] = env->xregs[15];
fb602cb7 8910 } else {
7aab5a8c 8911 env->banked_r13[bank_number(ARM_CPU_MODE_HYP)] = env->xregs[15];
fb602cb7 8912 }
d02a8698 8913
7aab5a8c
PMD
8914 if (mode == ARM_CPU_MODE_IRQ) {
8915 env->regs[14] = env->xregs[16];
8916 env->regs[13] = env->xregs[17];
d02a8698 8917 } else {
7aab5a8c
PMD
8918 env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[16];
8919 env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[17];
d02a8698
PM
8920 }
8921
7aab5a8c
PMD
8922 if (mode == ARM_CPU_MODE_SVC) {
8923 env->regs[14] = env->xregs[18];
8924 env->regs[13] = env->xregs[19];
8925 } else {
8926 env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)] = env->xregs[18];
8927 env->banked_r13[bank_number(ARM_CPU_MODE_SVC)] = env->xregs[19];
fb602cb7
PM
8928 }
8929
7aab5a8c
PMD
8930 if (mode == ARM_CPU_MODE_ABT) {
8931 env->regs[14] = env->xregs[20];
8932 env->regs[13] = env->xregs[21];
8933 } else {
8934 env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)] = env->xregs[20];
8935 env->banked_r13[bank_number(ARM_CPU_MODE_ABT)] = env->xregs[21];
ce02049d
GB
8936 }
8937
8938 if (mode == ARM_CPU_MODE_UND) {
3a9148d0
SS
8939 env->regs[14] = env->xregs[22];
8940 env->regs[13] = env->xregs[23];
ce02049d 8941 } else {
593cfa2b 8942 env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)] = env->xregs[22];
3a9148d0 8943 env->banked_r13[bank_number(ARM_CPU_MODE_UND)] = env->xregs[23];
ce02049d
GB
8944 }
8945
8946 /* Registers x24-x30 are mapped to r8-r14 in FIQ mode. If we are in FIQ
8947 * mode, then we can copy to r8-r14. Otherwise, we copy to the
8948 * FIQ bank for r8-r14.
8949 */
8950 if (mode == ARM_CPU_MODE_FIQ) {
8951 for (i = 24; i < 31; i++) {
8952 env->regs[i - 16] = env->xregs[i]; /* X[24:30] -> R[8:14] */
8953 }
8954 } else {
8955 for (i = 24; i < 29; i++) {
8956 env->fiq_regs[i - 24] = env->xregs[i];
8957 }
8958 env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[29];
593cfa2b 8959 env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[30];
ce02049d
GB
8960 }
8961
8962 env->regs[15] = env->pc;
8963}
8964
dea8378b
PM
8965static void take_aarch32_exception(CPUARMState *env, int new_mode,
8966 uint32_t mask, uint32_t offset,
8967 uint32_t newpc)
8968{
4a2696c0
RH
8969 int new_el;
8970
dea8378b
PM
8971 /* Change the CPU state so as to actually take the exception. */
8972 switch_mode(env, new_mode);
4a2696c0
RH
8973 new_el = arm_current_el(env);
8974
dea8378b
PM
8975 /*
8976 * For exceptions taken to AArch32 we must clear the SS bit in both
8977 * PSTATE and in the old-state value we save to SPSR_<mode>, so zero it now.
8978 */
8979 env->uncached_cpsr &= ~PSTATE_SS;
8980 env->spsr = cpsr_read(env);
8981 /* Clear IT bits. */
8982 env->condexec_bits = 0;
8983 /* Switch to the new mode, and to the correct instruction set. */
8984 env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode;
8985 /* Set new mode endianness */
8986 env->uncached_cpsr &= ~CPSR_E;
4a2696c0 8987 if (env->cp15.sctlr_el[new_el] & SCTLR_EE) {
dea8378b
PM
8988 env->uncached_cpsr |= CPSR_E;
8989 }
829f9fd3
PM
8990 /* J and IL must always be cleared for exception entry */
8991 env->uncached_cpsr &= ~(CPSR_IL | CPSR_J);
dea8378b
PM
8992 env->daif |= mask;
8993
8994 if (new_mode == ARM_CPU_MODE_HYP) {
8995 env->thumb = (env->cp15.sctlr_el[2] & SCTLR_TE) != 0;
8996 env->elr_el[2] = env->regs[15];
8997 } else {
4a2696c0 8998 /* CPSR.PAN is normally preserved preserved unless... */
f8af1143 8999 if (cpu_isar_feature(aa32_pan, env_archcpu(env))) {
4a2696c0
RH
9000 switch (new_el) {
9001 case 3:
9002 if (!arm_is_secure_below_el3(env)) {
9003 /* ... the target is EL3, from non-secure state. */
9004 env->uncached_cpsr &= ~CPSR_PAN;
9005 break;
9006 }
9007 /* ... the target is EL3, from secure state ... */
9008 /* fall through */
9009 case 1:
9010 /* ... the target is EL1 and SCTLR.SPAN is 0. */
9011 if (!(env->cp15.sctlr_el[new_el] & SCTLR_SPAN)) {
9012 env->uncached_cpsr |= CPSR_PAN;
9013 }
9014 break;
9015 }
9016 }
dea8378b
PM
9017 /*
9018 * this is a lie, as there was no c1_sys on V4T/V5, but who cares
9019 * and we should just guard the thumb mode on V4
9020 */
9021 if (arm_feature(env, ARM_FEATURE_V4T)) {
9022 env->thumb =
9023 (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_TE) != 0;
9024 }
9025 env->regs[14] = env->regs[15] + offset;
9026 }
9027 env->regs[15] = newpc;
a8a79c7a 9028 arm_rebuild_hflags(env);
dea8378b
PM
9029}
9030
b9bc21ff
PM
9031static void arm_cpu_do_interrupt_aarch32_hyp(CPUState *cs)
9032{
9033 /*
9034 * Handle exception entry to Hyp mode; this is sufficiently
9035 * different to entry to other AArch32 modes that we handle it
9036 * separately here.
9037 *
9038 * The vector table entry used is always the 0x14 Hyp mode entry point,
9039 * unless this is an UNDEF/HVC/abort taken from Hyp to Hyp.
9040 * The offset applied to the preferred return address is always zero
9041 * (see DDI0487C.a section G1.12.3).
9042 * PSTATE A/I/F masks are set based only on the SCR.EA/IRQ/FIQ values.
9043 */
9044 uint32_t addr, mask;
9045 ARMCPU *cpu = ARM_CPU(cs);
9046 CPUARMState *env = &cpu->env;
9047
9048 switch (cs->exception_index) {
9049 case EXCP_UDEF:
9050 addr = 0x04;
9051 break;
9052 case EXCP_SWI:
9053 addr = 0x14;
9054 break;
9055 case EXCP_BKPT:
9056 /* Fall through to prefetch abort. */
9057 case EXCP_PREFETCH_ABORT:
9058 env->cp15.ifar_s = env->exception.vaddress;
9059 qemu_log_mask(CPU_LOG_INT, "...with HIFAR 0x%x\n",
9060 (uint32_t)env->exception.vaddress);
9061 addr = 0x0c;
9062 break;
9063 case EXCP_DATA_ABORT:
9064 env->cp15.dfar_s = env->exception.vaddress;
9065 qemu_log_mask(CPU_LOG_INT, "...with HDFAR 0x%x\n",
9066 (uint32_t)env->exception.vaddress);
9067 addr = 0x10;
9068 break;
9069 case EXCP_IRQ:
9070 addr = 0x18;
9071 break;
9072 case EXCP_FIQ:
9073 addr = 0x1c;
9074 break;
9075 case EXCP_HVC:
9076 addr = 0x08;
9077 break;
9078 case EXCP_HYP_TRAP:
9079 addr = 0x14;
9bbb4ef9 9080 break;
b9bc21ff
PM
9081 default:
9082 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
9083 }
9084
9085 if (cs->exception_index != EXCP_IRQ && cs->exception_index != EXCP_FIQ) {
2ed08180
PM
9086 if (!arm_feature(env, ARM_FEATURE_V8)) {
9087 /*
9088 * QEMU syndrome values are v8-style. v7 has the IL bit
9089 * UNK/SBZP for "field not valid" cases, where v8 uses RES1.
9090 * If this is a v7 CPU, squash the IL bit in those cases.
9091 */
9092 if (cs->exception_index == EXCP_PREFETCH_ABORT ||
9093 (cs->exception_index == EXCP_DATA_ABORT &&
9094 !(env->exception.syndrome & ARM_EL_ISV)) ||
9095 syn_get_ec(env->exception.syndrome) == EC_UNCATEGORIZED) {
9096 env->exception.syndrome &= ~ARM_EL_IL;
9097 }
9098 }
b9bc21ff
PM
9099 env->cp15.esr_el[2] = env->exception.syndrome;
9100 }
9101
9102 if (arm_current_el(env) != 2 && addr < 0x14) {
9103 addr = 0x14;
9104 }
9105
9106 mask = 0;
9107 if (!(env->cp15.scr_el3 & SCR_EA)) {
9108 mask |= CPSR_A;
9109 }
9110 if (!(env->cp15.scr_el3 & SCR_IRQ)) {
9111 mask |= CPSR_I;
9112 }
9113 if (!(env->cp15.scr_el3 & SCR_FIQ)) {
9114 mask |= CPSR_F;
9115 }
9116
9117 addr += env->cp15.hvbar;
9118
9119 take_aarch32_exception(env, ARM_CPU_MODE_HYP, mask, 0, addr);
9120}
9121
966f758c 9122static void arm_cpu_do_interrupt_aarch32(CPUState *cs)
b5ff1b31 9123{
97a8ea5a
AF
9124 ARMCPU *cpu = ARM_CPU(cs);
9125 CPUARMState *env = &cpu->env;
b5ff1b31
FB
9126 uint32_t addr;
9127 uint32_t mask;
9128 int new_mode;
9129 uint32_t offset;
16a906fd 9130 uint32_t moe;
b5ff1b31 9131
16a906fd 9132 /* If this is a debug exception we must update the DBGDSCR.MOE bits */
64b91e3f 9133 switch (syn_get_ec(env->exception.syndrome)) {
16a906fd
PM
9134 case EC_BREAKPOINT:
9135 case EC_BREAKPOINT_SAME_EL:
9136 moe = 1;
9137 break;
9138 case EC_WATCHPOINT:
9139 case EC_WATCHPOINT_SAME_EL:
9140 moe = 10;
9141 break;
9142 case EC_AA32_BKPT:
9143 moe = 3;
9144 break;
9145 case EC_VECTORCATCH:
9146 moe = 5;
9147 break;
9148 default:
9149 moe = 0;
9150 break;
9151 }
9152
9153 if (moe) {
9154 env->cp15.mdscr_el1 = deposit64(env->cp15.mdscr_el1, 2, 4, moe);
9155 }
9156
b9bc21ff
PM
9157 if (env->exception.target_el == 2) {
9158 arm_cpu_do_interrupt_aarch32_hyp(cs);
9159 return;
9160 }
9161
27103424 9162 switch (cs->exception_index) {
b5ff1b31
FB
9163 case EXCP_UDEF:
9164 new_mode = ARM_CPU_MODE_UND;
9165 addr = 0x04;
9166 mask = CPSR_I;
9167 if (env->thumb)
9168 offset = 2;
9169 else
9170 offset = 4;
9171 break;
9172 case EXCP_SWI:
9173 new_mode = ARM_CPU_MODE_SVC;
9174 addr = 0x08;
9175 mask = CPSR_I;
601d70b9 9176 /* The PC already points to the next instruction. */
b5ff1b31
FB
9177 offset = 0;
9178 break;
06c949e6 9179 case EXCP_BKPT:
9ee6e8bb
PB
9180 /* Fall through to prefetch abort. */
9181 case EXCP_PREFETCH_ABORT:
88ca1c2d 9182 A32_BANKED_CURRENT_REG_SET(env, ifsr, env->exception.fsr);
b848ce2b 9183 A32_BANKED_CURRENT_REG_SET(env, ifar, env->exception.vaddress);
3f1beaca 9184 qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x IFAR 0x%x\n",
88ca1c2d 9185 env->exception.fsr, (uint32_t)env->exception.vaddress);
b5ff1b31
FB
9186 new_mode = ARM_CPU_MODE_ABT;
9187 addr = 0x0c;
9188 mask = CPSR_A | CPSR_I;
9189 offset = 4;
9190 break;
9191 case EXCP_DATA_ABORT:
4a7e2d73 9192 A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr);
b848ce2b 9193 A32_BANKED_CURRENT_REG_SET(env, dfar, env->exception.vaddress);
3f1beaca 9194 qemu_log_mask(CPU_LOG_INT, "...with DFSR 0x%x DFAR 0x%x\n",
4a7e2d73 9195 env->exception.fsr,
6cd8a264 9196 (uint32_t)env->exception.vaddress);
b5ff1b31
FB
9197 new_mode = ARM_CPU_MODE_ABT;
9198 addr = 0x10;
9199 mask = CPSR_A | CPSR_I;
9200 offset = 8;
9201 break;
9202 case EXCP_IRQ:
9203 new_mode = ARM_CPU_MODE_IRQ;
9204 addr = 0x18;
9205 /* Disable IRQ and imprecise data aborts. */
9206 mask = CPSR_A | CPSR_I;
9207 offset = 4;
de38d23b
FA
9208 if (env->cp15.scr_el3 & SCR_IRQ) {
9209 /* IRQ routed to monitor mode */
9210 new_mode = ARM_CPU_MODE_MON;
9211 mask |= CPSR_F;
9212 }
b5ff1b31
FB
9213 break;
9214 case EXCP_FIQ:
9215 new_mode = ARM_CPU_MODE_FIQ;
9216 addr = 0x1c;
9217 /* Disable FIQ, IRQ and imprecise data aborts. */
9218 mask = CPSR_A | CPSR_I | CPSR_F;
de38d23b
FA
9219 if (env->cp15.scr_el3 & SCR_FIQ) {
9220 /* FIQ routed to monitor mode */
9221 new_mode = ARM_CPU_MODE_MON;
9222 }
b5ff1b31
FB
9223 offset = 4;
9224 break;
87a4b270
PM
9225 case EXCP_VIRQ:
9226 new_mode = ARM_CPU_MODE_IRQ;
9227 addr = 0x18;
9228 /* Disable IRQ and imprecise data aborts. */
9229 mask = CPSR_A | CPSR_I;
9230 offset = 4;
9231 break;
9232 case EXCP_VFIQ:
9233 new_mode = ARM_CPU_MODE_FIQ;
9234 addr = 0x1c;
9235 /* Disable FIQ, IRQ and imprecise data aborts. */
9236 mask = CPSR_A | CPSR_I | CPSR_F;
9237 offset = 4;
9238 break;
dbe9d163
FA
9239 case EXCP_SMC:
9240 new_mode = ARM_CPU_MODE_MON;
9241 addr = 0x08;
9242 mask = CPSR_A | CPSR_I | CPSR_F;
9243 offset = 0;
9244 break;
b5ff1b31 9245 default:
a47dddd7 9246 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
b5ff1b31
FB
9247 return; /* Never happens. Keep compiler happy. */
9248 }
e89e51a1
FA
9249
9250 if (new_mode == ARM_CPU_MODE_MON) {
9251 addr += env->cp15.mvbar;
137feaa9 9252 } else if (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_V) {
e89e51a1 9253 /* High vectors. When enabled, base address cannot be remapped. */
b5ff1b31 9254 addr += 0xffff0000;
8641136c
NR
9255 } else {
9256 /* ARM v7 architectures provide a vector base address register to remap
9257 * the interrupt vector table.
e89e51a1 9258 * This register is only followed in non-monitor mode, and is banked.
8641136c
NR
9259 * Note: only bits 31:5 are valid.
9260 */
fb6c91ba 9261 addr += A32_BANKED_CURRENT_REG_GET(env, vbar);
b5ff1b31 9262 }
dbe9d163
FA
9263
9264 if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON) {
9265 env->cp15.scr_el3 &= ~SCR_NS;
9266 }
9267
dea8378b 9268 take_aarch32_exception(env, new_mode, mask, offset, addr);
b5ff1b31
FB
9269}
9270
966f758c
PM
9271/* Handle exception entry to a target EL which is using AArch64 */
9272static void arm_cpu_do_interrupt_aarch64(CPUState *cs)
f3a9b694
PM
9273{
9274 ARMCPU *cpu = ARM_CPU(cs);
9275 CPUARMState *env = &cpu->env;
9276 unsigned int new_el = env->exception.target_el;
9277 target_ulong addr = env->cp15.vbar_el[new_el];
9278 unsigned int new_mode = aarch64_pstate_mode(new_el, true);
4a2696c0 9279 unsigned int old_mode;
0ab5953b
RH
9280 unsigned int cur_el = arm_current_el(env);
9281
9a05f7b6
RH
9282 /*
9283 * Note that new_el can never be 0. If cur_el is 0, then
9284 * el0_a64 is is_a64(), else el0_a64 is ignored.
9285 */
9286 aarch64_sve_change_el(env, cur_el, new_el, is_a64(env));
f3a9b694 9287
0ab5953b 9288 if (cur_el < new_el) {
3d6f7617
PM
9289 /* Entry vector offset depends on whether the implemented EL
9290 * immediately lower than the target level is using AArch32 or AArch64
9291 */
9292 bool is_aa64;
cb092fbb 9293 uint64_t hcr;
3d6f7617
PM
9294
9295 switch (new_el) {
9296 case 3:
9297 is_aa64 = (env->cp15.scr_el3 & SCR_RW) != 0;
9298 break;
9299 case 2:
cb092fbb
RH
9300 hcr = arm_hcr_el2_eff(env);
9301 if ((hcr & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
9302 is_aa64 = (hcr & HCR_RW) != 0;
9303 break;
9304 }
9305 /* fall through */
3d6f7617
PM
9306 case 1:
9307 is_aa64 = is_a64(env);
9308 break;
9309 default:
9310 g_assert_not_reached();
9311 }
9312
9313 if (is_aa64) {
f3a9b694
PM
9314 addr += 0x400;
9315 } else {
9316 addr += 0x600;
9317 }
9318 } else if (pstate_read(env) & PSTATE_SP) {
9319 addr += 0x200;
9320 }
9321
f3a9b694
PM
9322 switch (cs->exception_index) {
9323 case EXCP_PREFETCH_ABORT:
9324 case EXCP_DATA_ABORT:
9325 env->cp15.far_el[new_el] = env->exception.vaddress;
9326 qemu_log_mask(CPU_LOG_INT, "...with FAR 0x%" PRIx64 "\n",
9327 env->cp15.far_el[new_el]);
9328 /* fall through */
9329 case EXCP_BKPT:
9330 case EXCP_UDEF:
9331 case EXCP_SWI:
9332 case EXCP_HVC:
9333 case EXCP_HYP_TRAP:
9334 case EXCP_SMC:
4be42f40
PM
9335 if (syn_get_ec(env->exception.syndrome) == EC_ADVSIMDFPACCESSTRAP) {
9336 /*
9337 * QEMU internal FP/SIMD syndromes from AArch32 include the
9338 * TA and coproc fields which are only exposed if the exception
9339 * is taken to AArch32 Hyp mode. Mask them out to get a valid
9340 * AArch64 format syndrome.
9341 */
9342 env->exception.syndrome &= ~MAKE_64BIT_MASK(0, 20);
9343 }
f3a9b694
PM
9344 env->cp15.esr_el[new_el] = env->exception.syndrome;
9345 break;
9346 case EXCP_IRQ:
9347 case EXCP_VIRQ:
9348 addr += 0x80;
9349 break;
9350 case EXCP_FIQ:
9351 case EXCP_VFIQ:
9352 addr += 0x100;
9353 break;
f3a9b694
PM
9354 default:
9355 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
9356 }
9357
9358 if (is_a64(env)) {
4a2696c0 9359 old_mode = pstate_read(env);
f3a9b694
PM
9360 aarch64_save_sp(env, arm_current_el(env));
9361 env->elr_el[new_el] = env->pc;
9362 } else {
4a2696c0 9363 old_mode = cpsr_read(env);
f3a9b694
PM
9364 env->elr_el[new_el] = env->regs[15];
9365
9366 aarch64_sync_32_to_64(env);
9367
9368 env->condexec_bits = 0;
9369 }
4a2696c0
RH
9370 env->banked_spsr[aarch64_banked_spsr_index(new_el)] = old_mode;
9371
f3a9b694
PM
9372 qemu_log_mask(CPU_LOG_INT, "...with ELR 0x%" PRIx64 "\n",
9373 env->elr_el[new_el]);
9374
4a2696c0
RH
9375 if (cpu_isar_feature(aa64_pan, cpu)) {
9376 /* The value of PSTATE.PAN is normally preserved, except when ... */
9377 new_mode |= old_mode & PSTATE_PAN;
9378 switch (new_el) {
9379 case 2:
9380 /* ... the target is EL2 with HCR_EL2.{E2H,TGE} == '11' ... */
9381 if ((arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE))
9382 != (HCR_E2H | HCR_TGE)) {
9383 break;
9384 }
9385 /* fall through */
9386 case 1:
9387 /* ... the target is EL1 ... */
9388 /* ... and SCTLR_ELx.SPAN == 0, then set to 1. */
9389 if ((env->cp15.sctlr_el[new_el] & SCTLR_SPAN) == 0) {
9390 new_mode |= PSTATE_PAN;
9391 }
9392 break;
9393 }
9394 }
9395
f3a9b694
PM
9396 pstate_write(env, PSTATE_DAIF | new_mode);
9397 env->aarch64 = 1;
9398 aarch64_restore_sp(env, new_el);
a8a79c7a 9399 helper_rebuild_hflags_a64(env, new_el);
f3a9b694
PM
9400
9401 env->pc = addr;
9402
9403 qemu_log_mask(CPU_LOG_INT, "...to EL%d PC 0x%" PRIx64 " PSTATE 0x%x\n",
9404 new_el, env->pc, pstate_read(env));
966f758c
PM
9405}
9406
ed6e6ba9
AB
9407/*
9408 * Do semihosting call and set the appropriate return value. All the
9409 * permission and validity checks have been done at translate time.
9410 *
9411 * We only see semihosting exceptions in TCG only as they are not
9412 * trapped to the hypervisor in KVM.
9413 */
91f78c58 9414#ifdef CONFIG_TCG
ed6e6ba9
AB
9415static void handle_semihosting(CPUState *cs)
9416{
904c04de
PM
9417 ARMCPU *cpu = ARM_CPU(cs);
9418 CPUARMState *env = &cpu->env;
9419
9420 if (is_a64(env)) {
ed6e6ba9
AB
9421 qemu_log_mask(CPU_LOG_INT,
9422 "...handling as semihosting call 0x%" PRIx64 "\n",
9423 env->xregs[0]);
9424 env->xregs[0] = do_arm_semihosting(env);
4ff5ef9e 9425 env->pc += 4;
904c04de 9426 } else {
904c04de
PM
9427 qemu_log_mask(CPU_LOG_INT,
9428 "...handling as semihosting call 0x%x\n",
9429 env->regs[0]);
9430 env->regs[0] = do_arm_semihosting(env);
4ff5ef9e 9431 env->regs[15] += env->thumb ? 2 : 4;
904c04de
PM
9432 }
9433}
ed6e6ba9 9434#endif
904c04de 9435
966f758c
PM
9436/* Handle a CPU exception for A and R profile CPUs.
9437 * Do any appropriate logging, handle PSCI calls, and then hand off
9438 * to the AArch64-entry or AArch32-entry function depending on the
9439 * target exception level's register width.
9440 */
9441void arm_cpu_do_interrupt(CPUState *cs)
9442{
9443 ARMCPU *cpu = ARM_CPU(cs);
9444 CPUARMState *env = &cpu->env;
9445 unsigned int new_el = env->exception.target_el;
9446
531c60a9 9447 assert(!arm_feature(env, ARM_FEATURE_M));
966f758c
PM
9448
9449 arm_log_exception(cs->exception_index);
9450 qemu_log_mask(CPU_LOG_INT, "...from EL%d to EL%d\n", arm_current_el(env),
9451 new_el);
9452 if (qemu_loglevel_mask(CPU_LOG_INT)
9453 && !excp_is_internal(cs->exception_index)) {
6568da45 9454 qemu_log_mask(CPU_LOG_INT, "...with ESR 0x%x/0x%" PRIx32 "\n",
64b91e3f 9455 syn_get_ec(env->exception.syndrome),
966f758c
PM
9456 env->exception.syndrome);
9457 }
9458
9459 if (arm_is_psci_call(cpu, cs->exception_index)) {
9460 arm_handle_psci_call(cpu);
9461 qemu_log_mask(CPU_LOG_INT, "...handled as PSCI call\n");
9462 return;
9463 }
9464
ed6e6ba9
AB
9465 /*
9466 * Semihosting semantics depend on the register width of the code
9467 * that caused the exception, not the target exception level, so
9468 * must be handled here.
966f758c 9469 */
ed6e6ba9
AB
9470#ifdef CONFIG_TCG
9471 if (cs->exception_index == EXCP_SEMIHOST) {
9472 handle_semihosting(cs);
904c04de
PM
9473 return;
9474 }
ed6e6ba9 9475#endif
904c04de 9476
b5c53d1b
AL
9477 /* Hooks may change global state so BQL should be held, also the
9478 * BQL needs to be held for any modification of
9479 * cs->interrupt_request.
9480 */
9481 g_assert(qemu_mutex_iothread_locked());
9482
9483 arm_call_pre_el_change_hook(cpu);
9484
904c04de
PM
9485 assert(!excp_is_internal(cs->exception_index));
9486 if (arm_el_is_aa64(env, new_el)) {
966f758c
PM
9487 arm_cpu_do_interrupt_aarch64(cs);
9488 } else {
9489 arm_cpu_do_interrupt_aarch32(cs);
9490 }
f3a9b694 9491
bd7d00fc
PM
9492 arm_call_el_change_hook(cpu);
9493
f3a9b694
PM
9494 if (!kvm_enabled()) {
9495 cs->interrupt_request |= CPU_INTERRUPT_EXITTB;
9496 }
9497}
c47eaf9f 9498#endif /* !CONFIG_USER_ONLY */
0480f69a
PM
9499
9500/* Return the exception level which controls this address translation regime */
b9f6033c 9501static uint32_t regime_el(CPUARMState *env, ARMMMUIdx mmu_idx)
0480f69a
PM
9502{
9503 switch (mmu_idx) {
b9f6033c
RH
9504 case ARMMMUIdx_E20_0:
9505 case ARMMMUIdx_E20_2:
452ef8cb 9506 case ARMMMUIdx_E20_2_PAN:
97fa9350 9507 case ARMMMUIdx_Stage2:
e013b741 9508 case ARMMMUIdx_E2:
0480f69a 9509 return 2;
127b2b08 9510 case ARMMMUIdx_SE3:
0480f69a 9511 return 3;
fba37aed 9512 case ARMMMUIdx_SE10_0:
0480f69a 9513 return arm_el_is_aa64(env, 3) ? 1 : 3;
fba37aed 9514 case ARMMMUIdx_SE10_1:
452ef8cb 9515 case ARMMMUIdx_SE10_1_PAN:
2859d7b5
RH
9516 case ARMMMUIdx_Stage1_E0:
9517 case ARMMMUIdx_Stage1_E1:
452ef8cb 9518 case ARMMMUIdx_Stage1_E1_PAN:
b9f6033c
RH
9519 case ARMMMUIdx_E10_0:
9520 case ARMMMUIdx_E10_1:
452ef8cb 9521 case ARMMMUIdx_E10_1_PAN:
62593718
PM
9522 case ARMMMUIdx_MPrivNegPri:
9523 case ARMMMUIdx_MUserNegPri:
e7b921c2
PM
9524 case ARMMMUIdx_MPriv:
9525 case ARMMMUIdx_MUser:
62593718
PM
9526 case ARMMMUIdx_MSPrivNegPri:
9527 case ARMMMUIdx_MSUserNegPri:
66787c78 9528 case ARMMMUIdx_MSPriv:
66787c78 9529 case ARMMMUIdx_MSUser:
0480f69a
PM
9530 return 1;
9531 default:
9532 g_assert_not_reached();
9533 }
9534}
9535
aaec1432
RH
9536uint64_t arm_sctlr(CPUARMState *env, int el)
9537{
9538 /* Only EL0 needs to be adjusted for EL1&0 or EL2&0. */
9539 if (el == 0) {
9540 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, 0);
9541 el = (mmu_idx == ARMMMUIdx_E20_0 ? 2 : 1);
9542 }
9543 return env->cp15.sctlr_el[el];
9544}
c47eaf9f 9545
0480f69a 9546/* Return the SCTLR value which controls this address translation regime */
aaec1432 9547static inline uint64_t regime_sctlr(CPUARMState *env, ARMMMUIdx mmu_idx)
0480f69a
PM
9548{
9549 return env->cp15.sctlr_el[regime_el(env, mmu_idx)];
9550}
9551
aaec1432
RH
9552#ifndef CONFIG_USER_ONLY
9553
0480f69a
PM
9554/* Return true if the specified stage of address translation is disabled */
9555static inline bool regime_translation_disabled(CPUARMState *env,
9556 ARMMMUIdx mmu_idx)
9557{
29c483a5 9558 if (arm_feature(env, ARM_FEATURE_M)) {
ecf5e8ea 9559 switch (env->v7m.mpu_ctrl[regime_is_secure(env, mmu_idx)] &
3bef7012
PM
9560 (R_V7M_MPU_CTRL_ENABLE_MASK | R_V7M_MPU_CTRL_HFNMIENA_MASK)) {
9561 case R_V7M_MPU_CTRL_ENABLE_MASK:
9562 /* Enabled, but not for HardFault and NMI */
62593718 9563 return mmu_idx & ARM_MMU_IDX_M_NEGPRI;
3bef7012
PM
9564 case R_V7M_MPU_CTRL_ENABLE_MASK | R_V7M_MPU_CTRL_HFNMIENA_MASK:
9565 /* Enabled for all cases */
9566 return false;
9567 case 0:
9568 default:
9569 /* HFNMIENA set and ENABLE clear is UNPREDICTABLE, but
9570 * we warned about that in armv7m_nvic.c when the guest set it.
9571 */
9572 return true;
9573 }
29c483a5
MD
9574 }
9575
97fa9350 9576 if (mmu_idx == ARMMMUIdx_Stage2) {
9d1bab33
PM
9577 /* HCR.DC means HCR.VM behaves as 1 */
9578 return (env->cp15.hcr_el2 & (HCR_DC | HCR_VM)) == 0;
0480f69a 9579 }
3d0e3080
PM
9580
9581 if (env->cp15.hcr_el2 & HCR_TGE) {
9582 /* TGE means that NS EL0/1 act as if SCTLR_EL1.M is zero */
9583 if (!regime_is_secure(env, mmu_idx) && regime_el(env, mmu_idx) == 1) {
9584 return true;
9585 }
9586 }
9587
fee7aa46 9588 if ((env->cp15.hcr_el2 & HCR_DC) && arm_mmu_idx_is_stage1_of_2(mmu_idx)) {
9d1bab33
PM
9589 /* HCR.DC means SCTLR_EL1.M behaves as 0 */
9590 return true;
9591 }
9592
0480f69a
PM
9593 return (regime_sctlr(env, mmu_idx) & SCTLR_M) == 0;
9594}
9595
73462ddd
PC
9596static inline bool regime_translation_big_endian(CPUARMState *env,
9597 ARMMMUIdx mmu_idx)
9598{
9599 return (regime_sctlr(env, mmu_idx) & SCTLR_EE) != 0;
9600}
9601
c47eaf9f
PM
9602/* Return the TTBR associated with this translation regime */
9603static inline uint64_t regime_ttbr(CPUARMState *env, ARMMMUIdx mmu_idx,
9604 int ttbrn)
9605{
97fa9350 9606 if (mmu_idx == ARMMMUIdx_Stage2) {
c47eaf9f
PM
9607 return env->cp15.vttbr_el2;
9608 }
9609 if (ttbrn == 0) {
9610 return env->cp15.ttbr0_el[regime_el(env, mmu_idx)];
9611 } else {
9612 return env->cp15.ttbr1_el[regime_el(env, mmu_idx)];
9613 }
9614}
9615
9616#endif /* !CONFIG_USER_ONLY */
9617
0480f69a
PM
9618/* Return the TCR controlling this translation regime */
9619static inline TCR *regime_tcr(CPUARMState *env, ARMMMUIdx mmu_idx)
9620{
97fa9350 9621 if (mmu_idx == ARMMMUIdx_Stage2) {
68e9c2fe 9622 return &env->cp15.vtcr_el2;
0480f69a
PM
9623 }
9624 return &env->cp15.tcr_el[regime_el(env, mmu_idx)];
9625}
9626
8bd5c820
PM
9627/* Convert a possible stage1+2 MMU index into the appropriate
9628 * stage 1 MMU index
9629 */
9630static inline ARMMMUIdx stage_1_mmu_idx(ARMMMUIdx mmu_idx)
9631{
b9f6033c
RH
9632 switch (mmu_idx) {
9633 case ARMMMUIdx_E10_0:
9634 return ARMMMUIdx_Stage1_E0;
9635 case ARMMMUIdx_E10_1:
9636 return ARMMMUIdx_Stage1_E1;
452ef8cb
RH
9637 case ARMMMUIdx_E10_1_PAN:
9638 return ARMMMUIdx_Stage1_E1_PAN;
b9f6033c
RH
9639 default:
9640 return mmu_idx;
8bd5c820 9641 }
8bd5c820
PM
9642}
9643
0480f69a
PM
9644/* Return true if the translation regime is using LPAE format page tables */
9645static inline bool regime_using_lpae_format(CPUARMState *env,
9646 ARMMMUIdx mmu_idx)
9647{
9648 int el = regime_el(env, mmu_idx);
9649 if (el == 2 || arm_el_is_aa64(env, el)) {
9650 return true;
9651 }
9652 if (arm_feature(env, ARM_FEATURE_LPAE)
9653 && (regime_tcr(env, mmu_idx)->raw_tcr & TTBCR_EAE)) {
9654 return true;
9655 }
9656 return false;
9657}
9658
deb2db99
AR
9659/* Returns true if the stage 1 translation regime is using LPAE format page
9660 * tables. Used when raising alignment exceptions, whose FSR changes depending
9661 * on whether the long or short descriptor format is in use. */
9662bool arm_s1_regime_using_lpae_format(CPUARMState *env, ARMMMUIdx mmu_idx)
30901475 9663{
8bd5c820 9664 mmu_idx = stage_1_mmu_idx(mmu_idx);
deb2db99 9665
30901475
AB
9666 return regime_using_lpae_format(env, mmu_idx);
9667}
9668
c47eaf9f 9669#ifndef CONFIG_USER_ONLY
0480f69a
PM
9670static inline bool regime_is_user(CPUARMState *env, ARMMMUIdx mmu_idx)
9671{
9672 switch (mmu_idx) {
fba37aed 9673 case ARMMMUIdx_SE10_0:
b9f6033c 9674 case ARMMMUIdx_E20_0:
2859d7b5 9675 case ARMMMUIdx_Stage1_E0:
e7b921c2 9676 case ARMMMUIdx_MUser:
871bec7c 9677 case ARMMMUIdx_MSUser:
62593718
PM
9678 case ARMMMUIdx_MUserNegPri:
9679 case ARMMMUIdx_MSUserNegPri:
0480f69a
PM
9680 return true;
9681 default:
9682 return false;
01b98b68
RH
9683 case ARMMMUIdx_E10_0:
9684 case ARMMMUIdx_E10_1:
452ef8cb 9685 case ARMMMUIdx_E10_1_PAN:
0480f69a
PM
9686 g_assert_not_reached();
9687 }
9688}
9689
0fbf5238
AJ
9690/* Translate section/page access permissions to page
9691 * R/W protection flags
d76951b6
AJ
9692 *
9693 * @env: CPUARMState
9694 * @mmu_idx: MMU index indicating required translation regime
9695 * @ap: The 3-bit access permissions (AP[2:0])
9696 * @domain_prot: The 2-bit domain access permissions
0fbf5238
AJ
9697 */
9698static inline int ap_to_rw_prot(CPUARMState *env, ARMMMUIdx mmu_idx,
9699 int ap, int domain_prot)
9700{
554b0b09
PM
9701 bool is_user = regime_is_user(env, mmu_idx);
9702
9703 if (domain_prot == 3) {
9704 return PAGE_READ | PAGE_WRITE;
9705 }
9706
554b0b09
PM
9707 switch (ap) {
9708 case 0:
9709 if (arm_feature(env, ARM_FEATURE_V7)) {
9710 return 0;
9711 }
554b0b09
PM
9712 switch (regime_sctlr(env, mmu_idx) & (SCTLR_S | SCTLR_R)) {
9713 case SCTLR_S:
9714 return is_user ? 0 : PAGE_READ;
9715 case SCTLR_R:
9716 return PAGE_READ;
9717 default:
9718 return 0;
9719 }
9720 case 1:
9721 return is_user ? 0 : PAGE_READ | PAGE_WRITE;
9722 case 2:
87c3d486 9723 if (is_user) {
0fbf5238 9724 return PAGE_READ;
87c3d486 9725 } else {
554b0b09 9726 return PAGE_READ | PAGE_WRITE;
87c3d486 9727 }
554b0b09
PM
9728 case 3:
9729 return PAGE_READ | PAGE_WRITE;
9730 case 4: /* Reserved. */
9731 return 0;
9732 case 5:
0fbf5238 9733 return is_user ? 0 : PAGE_READ;
554b0b09 9734 case 6:
0fbf5238 9735 return PAGE_READ;
554b0b09 9736 case 7:
87c3d486 9737 if (!arm_feature(env, ARM_FEATURE_V6K)) {
554b0b09 9738 return 0;
87c3d486 9739 }
0fbf5238 9740 return PAGE_READ;
554b0b09 9741 default:
0fbf5238 9742 g_assert_not_reached();
554b0b09 9743 }
b5ff1b31
FB
9744}
9745
d76951b6
AJ
9746/* Translate section/page access permissions to page
9747 * R/W protection flags.
9748 *
d76951b6 9749 * @ap: The 2-bit simple AP (AP[2:1])
d8e052b3 9750 * @is_user: TRUE if accessing from PL0
d76951b6 9751 */
d8e052b3 9752static inline int simple_ap_to_rw_prot_is_user(int ap, bool is_user)
d76951b6 9753{
d76951b6
AJ
9754 switch (ap) {
9755 case 0:
9756 return is_user ? 0 : PAGE_READ | PAGE_WRITE;
9757 case 1:
9758 return PAGE_READ | PAGE_WRITE;
9759 case 2:
9760 return is_user ? 0 : PAGE_READ;
9761 case 3:
9762 return PAGE_READ;
9763 default:
9764 g_assert_not_reached();
9765 }
9766}
9767
d8e052b3
AJ
9768static inline int
9769simple_ap_to_rw_prot(CPUARMState *env, ARMMMUIdx mmu_idx, int ap)
9770{
9771 return simple_ap_to_rw_prot_is_user(ap, regime_is_user(env, mmu_idx));
9772}
9773
6ab1a5ee
EI
9774/* Translate S2 section/page access permissions to protection flags
9775 *
9776 * @env: CPUARMState
9777 * @s2ap: The 2-bit stage2 access permissions (S2AP)
9778 * @xn: XN (execute-never) bit
9779 */
9780static int get_S2prot(CPUARMState *env, int s2ap, int xn)
9781{
9782 int prot = 0;
9783
9784 if (s2ap & 1) {
9785 prot |= PAGE_READ;
9786 }
9787 if (s2ap & 2) {
9788 prot |= PAGE_WRITE;
9789 }
9790 if (!xn) {
dfda6837
SS
9791 if (arm_el_is_aa64(env, 2) || prot & PAGE_READ) {
9792 prot |= PAGE_EXEC;
9793 }
6ab1a5ee
EI
9794 }
9795 return prot;
9796}
9797
d8e052b3
AJ
9798/* Translate section/page access permissions to protection flags
9799 *
9800 * @env: CPUARMState
9801 * @mmu_idx: MMU index indicating required translation regime
9802 * @is_aa64: TRUE if AArch64
9803 * @ap: The 2-bit simple AP (AP[2:1])
9804 * @ns: NS (non-secure) bit
9805 * @xn: XN (execute-never) bit
9806 * @pxn: PXN (privileged execute-never) bit
9807 */
9808static int get_S1prot(CPUARMState *env, ARMMMUIdx mmu_idx, bool is_aa64,
9809 int ap, int ns, int xn, int pxn)
9810{
9811 bool is_user = regime_is_user(env, mmu_idx);
9812 int prot_rw, user_rw;
9813 bool have_wxn;
9814 int wxn = 0;
9815
97fa9350 9816 assert(mmu_idx != ARMMMUIdx_Stage2);
d8e052b3
AJ
9817
9818 user_rw = simple_ap_to_rw_prot_is_user(ap, true);
9819 if (is_user) {
9820 prot_rw = user_rw;
9821 } else {
81636b70
RH
9822 if (user_rw && regime_is_pan(env, mmu_idx)) {
9823 return 0;
9824 }
d8e052b3
AJ
9825 prot_rw = simple_ap_to_rw_prot_is_user(ap, false);
9826 }
9827
9828 if (ns && arm_is_secure(env) && (env->cp15.scr_el3 & SCR_SIF)) {
9829 return prot_rw;
9830 }
9831
9832 /* TODO have_wxn should be replaced with
9833 * ARM_FEATURE_V8 || (ARM_FEATURE_V7 && ARM_FEATURE_EL2)
9834 * when ARM_FEATURE_EL2 starts getting set. For now we assume all LPAE
9835 * compatible processors have EL2, which is required for [U]WXN.
9836 */
9837 have_wxn = arm_feature(env, ARM_FEATURE_LPAE);
9838
9839 if (have_wxn) {
9840 wxn = regime_sctlr(env, mmu_idx) & SCTLR_WXN;
9841 }
9842
9843 if (is_aa64) {
339370b9
RH
9844 if (regime_has_2_ranges(mmu_idx) && !is_user) {
9845 xn = pxn || (user_rw & PAGE_WRITE);
d8e052b3
AJ
9846 }
9847 } else if (arm_feature(env, ARM_FEATURE_V7)) {
9848 switch (regime_el(env, mmu_idx)) {
9849 case 1:
9850 case 3:
9851 if (is_user) {
9852 xn = xn || !(user_rw & PAGE_READ);
9853 } else {
9854 int uwxn = 0;
9855 if (have_wxn) {
9856 uwxn = regime_sctlr(env, mmu_idx) & SCTLR_UWXN;
9857 }
9858 xn = xn || !(prot_rw & PAGE_READ) || pxn ||
9859 (uwxn && (user_rw & PAGE_WRITE));
9860 }
9861 break;
9862 case 2:
9863 break;
9864 }
9865 } else {
9866 xn = wxn = 0;
9867 }
9868
9869 if (xn || (wxn && (prot_rw & PAGE_WRITE))) {
9870 return prot_rw;
9871 }
9872 return prot_rw | PAGE_EXEC;
9873}
9874
0480f69a
PM
9875static bool get_level1_table_address(CPUARMState *env, ARMMMUIdx mmu_idx,
9876 uint32_t *table, uint32_t address)
b2fa1797 9877{
0480f69a 9878 /* Note that we can only get here for an AArch32 PL0/PL1 lookup */
0480f69a 9879 TCR *tcr = regime_tcr(env, mmu_idx);
11f136ee 9880
11f136ee
FA
9881 if (address & tcr->mask) {
9882 if (tcr->raw_tcr & TTBCR_PD1) {
e389be16
FA
9883 /* Translation table walk disabled for TTBR1 */
9884 return false;
9885 }
aef878be 9886 *table = regime_ttbr(env, mmu_idx, 1) & 0xffffc000;
e389be16 9887 } else {
11f136ee 9888 if (tcr->raw_tcr & TTBCR_PD0) {
e389be16
FA
9889 /* Translation table walk disabled for TTBR0 */
9890 return false;
9891 }
aef878be 9892 *table = regime_ttbr(env, mmu_idx, 0) & tcr->base_mask;
e389be16
FA
9893 }
9894 *table |= (address >> 18) & 0x3ffc;
9895 return true;
b2fa1797
PB
9896}
9897
37785977
EI
9898/* Translate a S1 pagetable walk through S2 if needed. */
9899static hwaddr S1_ptw_translate(CPUARMState *env, ARMMMUIdx mmu_idx,
9900 hwaddr addr, MemTxAttrs txattrs,
37785977
EI
9901 ARMMMUFaultInfo *fi)
9902{
fee7aa46 9903 if (arm_mmu_idx_is_stage1_of_2(mmu_idx) &&
97fa9350 9904 !regime_translation_disabled(env, ARMMMUIdx_Stage2)) {
37785977
EI
9905 target_ulong s2size;
9906 hwaddr s2pa;
9907 int s2prot;
9908 int ret;
eadb2feb
PM
9909 ARMCacheAttrs cacheattrs = {};
9910 ARMCacheAttrs *pcacheattrs = NULL;
9911
9912 if (env->cp15.hcr_el2 & HCR_PTW) {
9913 /*
9914 * PTW means we must fault if this S1 walk touches S2 Device
9915 * memory; otherwise we don't care about the attributes and can
9916 * save the S2 translation the effort of computing them.
9917 */
9918 pcacheattrs = &cacheattrs;
9919 }
37785977 9920
97fa9350 9921 ret = get_phys_addr_lpae(env, addr, 0, ARMMMUIdx_Stage2, &s2pa,
eadb2feb 9922 &txattrs, &s2prot, &s2size, fi, pcacheattrs);
37785977 9923 if (ret) {
3b39d734 9924 assert(fi->type != ARMFault_None);
37785977
EI
9925 fi->s2addr = addr;
9926 fi->stage2 = true;
9927 fi->s1ptw = true;
9928 return ~0;
9929 }
eadb2feb
PM
9930 if (pcacheattrs && (pcacheattrs->attrs & 0xf0) == 0) {
9931 /* Access was to Device memory: generate Permission fault */
9932 fi->type = ARMFault_Permission;
9933 fi->s2addr = addr;
9934 fi->stage2 = true;
9935 fi->s1ptw = true;
9936 return ~0;
9937 }
37785977
EI
9938 addr = s2pa;
9939 }
9940 return addr;
9941}
9942
14577270 9943/* All loads done in the course of a page table walk go through here. */
a614e698 9944static uint32_t arm_ldl_ptw(CPUState *cs, hwaddr addr, bool is_secure,
3795a6de 9945 ARMMMUIdx mmu_idx, ARMMMUFaultInfo *fi)
ebca90e4 9946{
a614e698
EI
9947 ARMCPU *cpu = ARM_CPU(cs);
9948 CPUARMState *env = &cpu->env;
ebca90e4 9949 MemTxAttrs attrs = {};
3b39d734 9950 MemTxResult result = MEMTX_OK;
5ce4ff65 9951 AddressSpace *as;
3b39d734 9952 uint32_t data;
ebca90e4
PM
9953
9954 attrs.secure = is_secure;
5ce4ff65 9955 as = arm_addressspace(cs, attrs);
3795a6de 9956 addr = S1_ptw_translate(env, mmu_idx, addr, attrs, fi);
a614e698
EI
9957 if (fi->s1ptw) {
9958 return 0;
9959 }
73462ddd 9960 if (regime_translation_big_endian(env, mmu_idx)) {
3b39d734 9961 data = address_space_ldl_be(as, addr, attrs, &result);
73462ddd 9962 } else {
3b39d734 9963 data = address_space_ldl_le(as, addr, attrs, &result);
73462ddd 9964 }
3b39d734
PM
9965 if (result == MEMTX_OK) {
9966 return data;
9967 }
9968 fi->type = ARMFault_SyncExternalOnWalk;
9969 fi->ea = arm_extabort_type(result);
9970 return 0;
ebca90e4
PM
9971}
9972
37785977 9973static uint64_t arm_ldq_ptw(CPUState *cs, hwaddr addr, bool is_secure,
3795a6de 9974 ARMMMUIdx mmu_idx, ARMMMUFaultInfo *fi)
ebca90e4 9975{
37785977
EI
9976 ARMCPU *cpu = ARM_CPU(cs);
9977 CPUARMState *env = &cpu->env;
ebca90e4 9978 MemTxAttrs attrs = {};
3b39d734 9979 MemTxResult result = MEMTX_OK;
5ce4ff65 9980 AddressSpace *as;
9aea1ea3 9981 uint64_t data;
ebca90e4
PM
9982
9983 attrs.secure = is_secure;
5ce4ff65 9984 as = arm_addressspace(cs, attrs);
3795a6de 9985 addr = S1_ptw_translate(env, mmu_idx, addr, attrs, fi);
37785977
EI
9986 if (fi->s1ptw) {
9987 return 0;
9988 }
73462ddd 9989 if (regime_translation_big_endian(env, mmu_idx)) {
3b39d734 9990 data = address_space_ldq_be(as, addr, attrs, &result);
73462ddd 9991 } else {
3b39d734
PM
9992 data = address_space_ldq_le(as, addr, attrs, &result);
9993 }
9994 if (result == MEMTX_OK) {
9995 return data;
73462ddd 9996 }
3b39d734
PM
9997 fi->type = ARMFault_SyncExternalOnWalk;
9998 fi->ea = arm_extabort_type(result);
9999 return 0;
ebca90e4
PM
10000}
10001
b7cc4e82 10002static bool get_phys_addr_v5(CPUARMState *env, uint32_t address,
03ae85f8 10003 MMUAccessType access_type, ARMMMUIdx mmu_idx,
b7cc4e82 10004 hwaddr *phys_ptr, int *prot,
f989983e 10005 target_ulong *page_size,
e14b5a23 10006 ARMMMUFaultInfo *fi)
b5ff1b31 10007{
2fc0cc0e 10008 CPUState *cs = env_cpu(env);
f989983e 10009 int level = 1;
b5ff1b31
FB
10010 uint32_t table;
10011 uint32_t desc;
10012 int type;
10013 int ap;
e389be16 10014 int domain = 0;
dd4ebc2e 10015 int domain_prot;
a8170e5e 10016 hwaddr phys_addr;
0480f69a 10017 uint32_t dacr;
b5ff1b31 10018
9ee6e8bb
PB
10019 /* Pagetable walk. */
10020 /* Lookup l1 descriptor. */
0480f69a 10021 if (!get_level1_table_address(env, mmu_idx, &table, address)) {
e389be16 10022 /* Section translation fault if page walk is disabled by PD0 or PD1 */
f989983e 10023 fi->type = ARMFault_Translation;
e389be16
FA
10024 goto do_fault;
10025 }
a614e698 10026 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
3795a6de 10027 mmu_idx, fi);
3b39d734
PM
10028 if (fi->type != ARMFault_None) {
10029 goto do_fault;
10030 }
9ee6e8bb 10031 type = (desc & 3);
dd4ebc2e 10032 domain = (desc >> 5) & 0x0f;
0480f69a
PM
10033 if (regime_el(env, mmu_idx) == 1) {
10034 dacr = env->cp15.dacr_ns;
10035 } else {
10036 dacr = env->cp15.dacr_s;
10037 }
10038 domain_prot = (dacr >> (domain * 2)) & 3;
9ee6e8bb 10039 if (type == 0) {
601d70b9 10040 /* Section translation fault. */
f989983e 10041 fi->type = ARMFault_Translation;
9ee6e8bb
PB
10042 goto do_fault;
10043 }
f989983e
PM
10044 if (type != 2) {
10045 level = 2;
10046 }
dd4ebc2e 10047 if (domain_prot == 0 || domain_prot == 2) {
f989983e 10048 fi->type = ARMFault_Domain;
9ee6e8bb
PB
10049 goto do_fault;
10050 }
10051 if (type == 2) {
10052 /* 1Mb section. */
10053 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
10054 ap = (desc >> 10) & 3;
d4c430a8 10055 *page_size = 1024 * 1024;
9ee6e8bb
PB
10056 } else {
10057 /* Lookup l2 entry. */
554b0b09
PM
10058 if (type == 1) {
10059 /* Coarse pagetable. */
10060 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
10061 } else {
10062 /* Fine pagetable. */
10063 table = (desc & 0xfffff000) | ((address >> 8) & 0xffc);
10064 }
a614e698 10065 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
3795a6de 10066 mmu_idx, fi);
3b39d734
PM
10067 if (fi->type != ARMFault_None) {
10068 goto do_fault;
10069 }
9ee6e8bb
PB
10070 switch (desc & 3) {
10071 case 0: /* Page translation fault. */
f989983e 10072 fi->type = ARMFault_Translation;
9ee6e8bb
PB
10073 goto do_fault;
10074 case 1: /* 64k page. */
10075 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
10076 ap = (desc >> (4 + ((address >> 13) & 6))) & 3;
d4c430a8 10077 *page_size = 0x10000;
ce819861 10078 break;
9ee6e8bb
PB
10079 case 2: /* 4k page. */
10080 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
c10f7fc3 10081 ap = (desc >> (4 + ((address >> 9) & 6))) & 3;
d4c430a8 10082 *page_size = 0x1000;
ce819861 10083 break;
fc1891c7 10084 case 3: /* 1k page, or ARMv6/XScale "extended small (4k) page" */
554b0b09 10085 if (type == 1) {
fc1891c7
PM
10086 /* ARMv6/XScale extended small page format */
10087 if (arm_feature(env, ARM_FEATURE_XSCALE)
10088 || arm_feature(env, ARM_FEATURE_V6)) {
554b0b09 10089 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
fc1891c7 10090 *page_size = 0x1000;
554b0b09 10091 } else {
fc1891c7
PM
10092 /* UNPREDICTABLE in ARMv5; we choose to take a
10093 * page translation fault.
10094 */
f989983e 10095 fi->type = ARMFault_Translation;
554b0b09
PM
10096 goto do_fault;
10097 }
10098 } else {
10099 phys_addr = (desc & 0xfffffc00) | (address & 0x3ff);
fc1891c7 10100 *page_size = 0x400;
554b0b09 10101 }
9ee6e8bb 10102 ap = (desc >> 4) & 3;
ce819861
PB
10103 break;
10104 default:
9ee6e8bb
PB
10105 /* Never happens, but compiler isn't smart enough to tell. */
10106 abort();
ce819861 10107 }
9ee6e8bb 10108 }
0fbf5238
AJ
10109 *prot = ap_to_rw_prot(env, mmu_idx, ap, domain_prot);
10110 *prot |= *prot ? PAGE_EXEC : 0;
10111 if (!(*prot & (1 << access_type))) {
9ee6e8bb 10112 /* Access permission fault. */
f989983e 10113 fi->type = ARMFault_Permission;
9ee6e8bb
PB
10114 goto do_fault;
10115 }
10116 *phys_ptr = phys_addr;
b7cc4e82 10117 return false;
9ee6e8bb 10118do_fault:
f989983e
PM
10119 fi->domain = domain;
10120 fi->level = level;
b7cc4e82 10121 return true;
9ee6e8bb
PB
10122}
10123
b7cc4e82 10124static bool get_phys_addr_v6(CPUARMState *env, uint32_t address,
03ae85f8 10125 MMUAccessType access_type, ARMMMUIdx mmu_idx,
b7cc4e82 10126 hwaddr *phys_ptr, MemTxAttrs *attrs, int *prot,
f06cf243 10127 target_ulong *page_size, ARMMMUFaultInfo *fi)
9ee6e8bb 10128{
2fc0cc0e 10129 CPUState *cs = env_cpu(env);
f06cf243 10130 int level = 1;
9ee6e8bb
PB
10131 uint32_t table;
10132 uint32_t desc;
10133 uint32_t xn;
de9b05b8 10134 uint32_t pxn = 0;
9ee6e8bb
PB
10135 int type;
10136 int ap;
de9b05b8 10137 int domain = 0;
dd4ebc2e 10138 int domain_prot;
a8170e5e 10139 hwaddr phys_addr;
0480f69a 10140 uint32_t dacr;
8bf5b6a9 10141 bool ns;
9ee6e8bb
PB
10142
10143 /* Pagetable walk. */
10144 /* Lookup l1 descriptor. */
0480f69a 10145 if (!get_level1_table_address(env, mmu_idx, &table, address)) {
e389be16 10146 /* Section translation fault if page walk is disabled by PD0 or PD1 */
f06cf243 10147 fi->type = ARMFault_Translation;
e389be16
FA
10148 goto do_fault;
10149 }
a614e698 10150 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
3795a6de 10151 mmu_idx, fi);
3b39d734
PM
10152 if (fi->type != ARMFault_None) {
10153 goto do_fault;
10154 }
9ee6e8bb 10155 type = (desc & 3);
de9b05b8
PM
10156 if (type == 0 || (type == 3 && !arm_feature(env, ARM_FEATURE_PXN))) {
10157 /* Section translation fault, or attempt to use the encoding
10158 * which is Reserved on implementations without PXN.
10159 */
f06cf243 10160 fi->type = ARMFault_Translation;
9ee6e8bb 10161 goto do_fault;
de9b05b8
PM
10162 }
10163 if ((type == 1) || !(desc & (1 << 18))) {
10164 /* Page or Section. */
dd4ebc2e 10165 domain = (desc >> 5) & 0x0f;
9ee6e8bb 10166 }
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 }
f06cf243
PM
10172 if (type == 1) {
10173 level = 2;
10174 }
0480f69a 10175 domain_prot = (dacr >> (domain * 2)) & 3;
dd4ebc2e 10176 if (domain_prot == 0 || domain_prot == 2) {
f06cf243
PM
10177 /* Section or Page domain fault */
10178 fi->type = ARMFault_Domain;
9ee6e8bb
PB
10179 goto do_fault;
10180 }
de9b05b8 10181 if (type != 1) {
9ee6e8bb
PB
10182 if (desc & (1 << 18)) {
10183 /* Supersection. */
10184 phys_addr = (desc & 0xff000000) | (address & 0x00ffffff);
4e42a6ca
SF
10185 phys_addr |= (uint64_t)extract32(desc, 20, 4) << 32;
10186 phys_addr |= (uint64_t)extract32(desc, 5, 4) << 36;
d4c430a8 10187 *page_size = 0x1000000;
b5ff1b31 10188 } else {
9ee6e8bb
PB
10189 /* Section. */
10190 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
d4c430a8 10191 *page_size = 0x100000;
b5ff1b31 10192 }
9ee6e8bb
PB
10193 ap = ((desc >> 10) & 3) | ((desc >> 13) & 4);
10194 xn = desc & (1 << 4);
de9b05b8 10195 pxn = desc & 1;
8bf5b6a9 10196 ns = extract32(desc, 19, 1);
9ee6e8bb 10197 } else {
de9b05b8
PM
10198 if (arm_feature(env, ARM_FEATURE_PXN)) {
10199 pxn = (desc >> 2) & 1;
10200 }
8bf5b6a9 10201 ns = extract32(desc, 3, 1);
9ee6e8bb
PB
10202 /* Lookup l2 entry. */
10203 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
a614e698 10204 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
3795a6de 10205 mmu_idx, fi);
3b39d734
PM
10206 if (fi->type != ARMFault_None) {
10207 goto do_fault;
10208 }
9ee6e8bb
PB
10209 ap = ((desc >> 4) & 3) | ((desc >> 7) & 4);
10210 switch (desc & 3) {
10211 case 0: /* Page translation fault. */
f06cf243 10212 fi->type = ARMFault_Translation;
b5ff1b31 10213 goto do_fault;
9ee6e8bb
PB
10214 case 1: /* 64k page. */
10215 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
10216 xn = desc & (1 << 15);
d4c430a8 10217 *page_size = 0x10000;
9ee6e8bb
PB
10218 break;
10219 case 2: case 3: /* 4k page. */
10220 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
10221 xn = desc & 1;
d4c430a8 10222 *page_size = 0x1000;
9ee6e8bb
PB
10223 break;
10224 default:
10225 /* Never happens, but compiler isn't smart enough to tell. */
10226 abort();
b5ff1b31 10227 }
9ee6e8bb 10228 }
dd4ebc2e 10229 if (domain_prot == 3) {
c0034328
JR
10230 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
10231 } else {
0480f69a 10232 if (pxn && !regime_is_user(env, mmu_idx)) {
de9b05b8
PM
10233 xn = 1;
10234 }
f06cf243
PM
10235 if (xn && access_type == MMU_INST_FETCH) {
10236 fi->type = ARMFault_Permission;
c0034328 10237 goto do_fault;
f06cf243 10238 }
9ee6e8bb 10239
d76951b6
AJ
10240 if (arm_feature(env, ARM_FEATURE_V6K) &&
10241 (regime_sctlr(env, mmu_idx) & SCTLR_AFE)) {
10242 /* The simplified model uses AP[0] as an access control bit. */
10243 if ((ap & 1) == 0) {
10244 /* Access flag fault. */
f06cf243 10245 fi->type = ARMFault_AccessFlag;
d76951b6
AJ
10246 goto do_fault;
10247 }
10248 *prot = simple_ap_to_rw_prot(env, mmu_idx, ap >> 1);
10249 } else {
10250 *prot = ap_to_rw_prot(env, mmu_idx, ap, domain_prot);
c0034328 10251 }
0fbf5238
AJ
10252 if (*prot && !xn) {
10253 *prot |= PAGE_EXEC;
10254 }
10255 if (!(*prot & (1 << access_type))) {
c0034328 10256 /* Access permission fault. */
f06cf243 10257 fi->type = ARMFault_Permission;
c0034328
JR
10258 goto do_fault;
10259 }
3ad493fc 10260 }
8bf5b6a9
PM
10261 if (ns) {
10262 /* The NS bit will (as required by the architecture) have no effect if
10263 * the CPU doesn't support TZ or this is a non-secure translation
10264 * regime, because the attribute will already be non-secure.
10265 */
10266 attrs->secure = false;
10267 }
9ee6e8bb 10268 *phys_ptr = phys_addr;
b7cc4e82 10269 return false;
b5ff1b31 10270do_fault:
f06cf243
PM
10271 fi->domain = domain;
10272 fi->level = level;
b7cc4e82 10273 return true;
b5ff1b31
FB
10274}
10275
1853d5a9 10276/*
a0e966c9 10277 * check_s2_mmu_setup
1853d5a9
EI
10278 * @cpu: ARMCPU
10279 * @is_aa64: True if the translation regime is in AArch64 state
10280 * @startlevel: Suggested starting level
10281 * @inputsize: Bitsize of IPAs
10282 * @stride: Page-table stride (See the ARM ARM)
10283 *
a0e966c9
EI
10284 * Returns true if the suggested S2 translation parameters are OK and
10285 * false otherwise.
1853d5a9 10286 */
a0e966c9
EI
10287static bool check_s2_mmu_setup(ARMCPU *cpu, bool is_aa64, int level,
10288 int inputsize, int stride)
1853d5a9 10289{
98d68ec2
EI
10290 const int grainsize = stride + 3;
10291 int startsizecheck;
10292
1853d5a9
EI
10293 /* Negative levels are never allowed. */
10294 if (level < 0) {
10295 return false;
10296 }
10297
98d68ec2
EI
10298 startsizecheck = inputsize - ((3 - level) * stride + grainsize);
10299 if (startsizecheck < 1 || startsizecheck > stride + 4) {
10300 return false;
10301 }
10302
1853d5a9 10303 if (is_aa64) {
3526423e 10304 CPUARMState *env = &cpu->env;
1853d5a9
EI
10305 unsigned int pamax = arm_pamax(cpu);
10306
10307 switch (stride) {
10308 case 13: /* 64KB Pages. */
10309 if (level == 0 || (level == 1 && pamax <= 42)) {
10310 return false;
10311 }
10312 break;
10313 case 11: /* 16KB Pages. */
10314 if (level == 0 || (level == 1 && pamax <= 40)) {
10315 return false;
10316 }
10317 break;
10318 case 9: /* 4KB Pages. */
10319 if (level == 0 && pamax <= 42) {
10320 return false;
10321 }
10322 break;
10323 default:
10324 g_assert_not_reached();
10325 }
3526423e
EI
10326
10327 /* Inputsize checks. */
10328 if (inputsize > pamax &&
10329 (arm_el_is_aa64(env, 1) || inputsize > 40)) {
10330 /* This is CONSTRAINED UNPREDICTABLE and we choose to fault. */
10331 return false;
10332 }
1853d5a9 10333 } else {
1853d5a9
EI
10334 /* AArch32 only supports 4KB pages. Assert on that. */
10335 assert(stride == 9);
10336
10337 if (level == 0) {
10338 return false;
10339 }
1853d5a9
EI
10340 }
10341 return true;
10342}
10343
5b2d261d
AB
10344/* Translate from the 4-bit stage 2 representation of
10345 * memory attributes (without cache-allocation hints) to
10346 * the 8-bit representation of the stage 1 MAIR registers
10347 * (which includes allocation hints).
10348 *
10349 * ref: shared/translation/attrs/S2AttrDecode()
10350 * .../S2ConvertAttrsHints()
10351 */
10352static uint8_t convert_stage2_attrs(CPUARMState *env, uint8_t s2attrs)
10353{
10354 uint8_t hiattr = extract32(s2attrs, 2, 2);
10355 uint8_t loattr = extract32(s2attrs, 0, 2);
10356 uint8_t hihint = 0, lohint = 0;
10357
10358 if (hiattr != 0) { /* normal memory */
10359 if ((env->cp15.hcr_el2 & HCR_CD) != 0) { /* cache disabled */
10360 hiattr = loattr = 1; /* non-cacheable */
10361 } else {
10362 if (hiattr != 1) { /* Write-through or write-back */
10363 hihint = 3; /* RW allocate */
10364 }
10365 if (loattr != 1) { /* Write-through or write-back */
10366 lohint = 3; /* RW allocate */
10367 }
10368 }
10369 }
10370
10371 return (hiattr << 6) | (hihint << 4) | (loattr << 2) | lohint;
10372}
c47eaf9f 10373#endif /* !CONFIG_USER_ONLY */
5b2d261d 10374
b830a5ee
RH
10375static int aa64_va_parameter_tbi(uint64_t tcr, ARMMMUIdx mmu_idx)
10376{
10377 if (regime_has_2_ranges(mmu_idx)) {
10378 return extract64(tcr, 37, 2);
10379 } else if (mmu_idx == ARMMMUIdx_Stage2) {
10380 return 0; /* VTCR_EL2 */
10381 } else {
10382 return extract32(tcr, 20, 1);
10383 }
10384}
10385
10386static int aa64_va_parameter_tbid(uint64_t tcr, ARMMMUIdx mmu_idx)
10387{
10388 if (regime_has_2_ranges(mmu_idx)) {
10389 return extract64(tcr, 51, 2);
10390 } else if (mmu_idx == ARMMMUIdx_Stage2) {
10391 return 0; /* VTCR_EL2 */
10392 } else {
10393 return extract32(tcr, 29, 1);
10394 }
10395}
10396
10397ARMVAParameters aa64_va_parameters(CPUARMState *env, uint64_t va,
10398 ARMMMUIdx mmu_idx, bool data)
ba97be9f
RH
10399{
10400 uint64_t tcr = regime_tcr(env, mmu_idx)->raw_tcr;
b830a5ee
RH
10401 bool epd, hpd, using16k, using64k;
10402 int select, tsz, tbi;
ba97be9f 10403
339370b9 10404 if (!regime_has_2_ranges(mmu_idx)) {
71d18164 10405 select = 0;
ba97be9f
RH
10406 tsz = extract32(tcr, 0, 6);
10407 using64k = extract32(tcr, 14, 1);
10408 using16k = extract32(tcr, 15, 1);
97fa9350 10409 if (mmu_idx == ARMMMUIdx_Stage2) {
ba97be9f 10410 /* VTCR_EL2 */
b830a5ee 10411 hpd = false;
ba97be9f 10412 } else {
ba97be9f
RH
10413 hpd = extract32(tcr, 24, 1);
10414 }
10415 epd = false;
ba97be9f 10416 } else {
71d18164
RH
10417 /*
10418 * Bit 55 is always between the two regions, and is canonical for
10419 * determining if address tagging is enabled.
10420 */
10421 select = extract64(va, 55, 1);
10422 if (!select) {
10423 tsz = extract32(tcr, 0, 6);
10424 epd = extract32(tcr, 7, 1);
10425 using64k = extract32(tcr, 14, 1);
10426 using16k = extract32(tcr, 15, 1);
71d18164 10427 hpd = extract64(tcr, 41, 1);
71d18164
RH
10428 } else {
10429 int tg = extract32(tcr, 30, 2);
10430 using16k = tg == 1;
10431 using64k = tg == 3;
10432 tsz = extract32(tcr, 16, 6);
10433 epd = extract32(tcr, 23, 1);
71d18164 10434 hpd = extract64(tcr, 42, 1);
71d18164 10435 }
ba97be9f
RH
10436 }
10437 tsz = MIN(tsz, 39); /* TODO: ARMv8.4-TTST */
10438 tsz = MAX(tsz, 16); /* TODO: ARMv8.2-LVA */
10439
b830a5ee
RH
10440 /* Present TBI as a composite with TBID. */
10441 tbi = aa64_va_parameter_tbi(tcr, mmu_idx);
10442 if (!data) {
10443 tbi &= ~aa64_va_parameter_tbid(tcr, mmu_idx);
10444 }
10445 tbi = (tbi >> select) & 1;
10446
ba97be9f
RH
10447 return (ARMVAParameters) {
10448 .tsz = tsz,
10449 .select = select,
10450 .tbi = tbi,
10451 .epd = epd,
10452 .hpd = hpd,
10453 .using16k = using16k,
10454 .using64k = using64k,
10455 };
10456}
10457
c47eaf9f 10458#ifndef CONFIG_USER_ONLY
ba97be9f
RH
10459static ARMVAParameters aa32_va_parameters(CPUARMState *env, uint32_t va,
10460 ARMMMUIdx mmu_idx)
10461{
10462 uint64_t tcr = regime_tcr(env, mmu_idx)->raw_tcr;
10463 uint32_t el = regime_el(env, mmu_idx);
10464 int select, tsz;
10465 bool epd, hpd;
10466
97fa9350 10467 if (mmu_idx == ARMMMUIdx_Stage2) {
ba97be9f
RH
10468 /* VTCR */
10469 bool sext = extract32(tcr, 4, 1);
10470 bool sign = extract32(tcr, 3, 1);
10471
10472 /*
10473 * If the sign-extend bit is not the same as t0sz[3], the result
10474 * is unpredictable. Flag this as a guest error.
10475 */
10476 if (sign != sext) {
10477 qemu_log_mask(LOG_GUEST_ERROR,
10478 "AArch32: VTCR.S / VTCR.T0SZ[3] mismatch\n");
10479 }
10480 tsz = sextract32(tcr, 0, 4) + 8;
10481 select = 0;
10482 hpd = false;
10483 epd = false;
10484 } else if (el == 2) {
10485 /* HTCR */
10486 tsz = extract32(tcr, 0, 3);
10487 select = 0;
10488 hpd = extract64(tcr, 24, 1);
10489 epd = false;
10490 } else {
10491 int t0sz = extract32(tcr, 0, 3);
10492 int t1sz = extract32(tcr, 16, 3);
10493
10494 if (t1sz == 0) {
10495 select = va > (0xffffffffu >> t0sz);
10496 } else {
10497 /* Note that we will detect errors later. */
10498 select = va >= ~(0xffffffffu >> t1sz);
10499 }
10500 if (!select) {
10501 tsz = t0sz;
10502 epd = extract32(tcr, 7, 1);
10503 hpd = extract64(tcr, 41, 1);
10504 } else {
10505 tsz = t1sz;
10506 epd = extract32(tcr, 23, 1);
10507 hpd = extract64(tcr, 42, 1);
10508 }
10509 /* For aarch32, hpd0 is not enabled without t2e as well. */
10510 hpd &= extract32(tcr, 6, 1);
10511 }
10512
10513 return (ARMVAParameters) {
10514 .tsz = tsz,
10515 .select = select,
10516 .epd = epd,
10517 .hpd = hpd,
10518 };
10519}
10520
b7cc4e82 10521static bool get_phys_addr_lpae(CPUARMState *env, target_ulong address,
03ae85f8 10522 MMUAccessType access_type, ARMMMUIdx mmu_idx,
b7cc4e82 10523 hwaddr *phys_ptr, MemTxAttrs *txattrs, int *prot,
da909b2c 10524 target_ulong *page_size_ptr,
5b2d261d 10525 ARMMMUFaultInfo *fi, ARMCacheAttrs *cacheattrs)
3dde962f 10526{
2fc0cc0e 10527 ARMCPU *cpu = env_archcpu(env);
1853d5a9 10528 CPUState *cs = CPU(cpu);
3dde962f 10529 /* Read an LPAE long-descriptor translation table. */
da909b2c 10530 ARMFaultType fault_type = ARMFault_Translation;
1b4093ea 10531 uint32_t level;
ba97be9f 10532 ARMVAParameters param;
3dde962f 10533 uint64_t ttbr;
dddb5223 10534 hwaddr descaddr, indexmask, indexmask_grainsize;
3dde962f 10535 uint32_t tableattrs;
36d820af 10536 target_ulong page_size;
3dde962f 10537 uint32_t attrs;
ba97be9f
RH
10538 int32_t stride;
10539 int addrsize, inputsize;
0480f69a 10540 TCR *tcr = regime_tcr(env, mmu_idx);
d8e052b3 10541 int ap, ns, xn, pxn;
88e8add8 10542 uint32_t el = regime_el(env, mmu_idx);
6109769a 10543 uint64_t descaddrmask;
6e99f762 10544 bool aarch64 = arm_el_is_aa64(env, el);
1bafc2ba 10545 bool guarded = false;
0480f69a
PM
10546
10547 /* TODO:
88e8add8
GB
10548 * This code does not handle the different format TCR for VTCR_EL2.
10549 * This code also does not support shareability levels.
10550 * Attribute and permission bit handling should also be checked when adding
10551 * support for those page table walks.
0480f69a 10552 */
6e99f762 10553 if (aarch64) {
ba97be9f
RH
10554 param = aa64_va_parameters(env, address, mmu_idx,
10555 access_type != MMU_INST_FETCH);
1b4093ea 10556 level = 0;
ba97be9f
RH
10557 addrsize = 64 - 8 * param.tbi;
10558 inputsize = 64 - param.tsz;
d0a2cbce 10559 } else {
ba97be9f 10560 param = aa32_va_parameters(env, address, mmu_idx);
1b4093ea 10561 level = 1;
97fa9350 10562 addrsize = (mmu_idx == ARMMMUIdx_Stage2 ? 40 : 32);
ba97be9f 10563 inputsize = addrsize - param.tsz;
2c8dd318 10564 }
3dde962f 10565
ba97be9f
RH
10566 /*
10567 * We determined the region when collecting the parameters, but we
10568 * have not yet validated that the address is valid for the region.
10569 * Extract the top bits and verify that they all match select.
36d820af
RH
10570 *
10571 * For aa32, if inputsize == addrsize, then we have selected the
10572 * region by exclusion in aa32_va_parameters and there is no more
10573 * validation to do here.
10574 */
10575 if (inputsize < addrsize) {
10576 target_ulong top_bits = sextract64(address, inputsize,
10577 addrsize - inputsize);
03f27724 10578 if (-top_bits != param.select) {
36d820af
RH
10579 /* The gap between the two regions is a Translation fault */
10580 fault_type = ARMFault_Translation;
10581 goto do_fault;
10582 }
3dde962f
PM
10583 }
10584
ba97be9f
RH
10585 if (param.using64k) {
10586 stride = 13;
10587 } else if (param.using16k) {
10588 stride = 11;
10589 } else {
10590 stride = 9;
10591 }
10592
3dde962f
PM
10593 /* Note that QEMU ignores shareability and cacheability attributes,
10594 * so we don't need to do anything with the SH, ORGN, IRGN fields
10595 * in the TTBCR. Similarly, TTBCR:A1 selects whether we get the
10596 * ASID from TTBR0 or TTBR1, but QEMU's TLB doesn't currently
10597 * implement any ASID-like capability so we can ignore it (instead
10598 * we will always flush the TLB any time the ASID is changed).
10599 */
ba97be9f 10600 ttbr = regime_ttbr(env, mmu_idx, param.select);
3dde962f 10601
0480f69a 10602 /* Here we should have set up all the parameters for the translation:
6e99f762 10603 * inputsize, ttbr, epd, stride, tbi
0480f69a
PM
10604 */
10605
ba97be9f 10606 if (param.epd) {
88e8add8
GB
10607 /* Translation table walk disabled => Translation fault on TLB miss
10608 * Note: This is always 0 on 64-bit EL2 and EL3.
10609 */
3dde962f
PM
10610 goto do_fault;
10611 }
10612
97fa9350 10613 if (mmu_idx != ARMMMUIdx_Stage2) {
1853d5a9
EI
10614 /* The starting level depends on the virtual address size (which can
10615 * be up to 48 bits) and the translation granule size. It indicates
10616 * the number of strides (stride bits at a time) needed to
10617 * consume the bits of the input address. In the pseudocode this is:
10618 * level = 4 - RoundUp((inputsize - grainsize) / stride)
10619 * where their 'inputsize' is our 'inputsize', 'grainsize' is
10620 * our 'stride + 3' and 'stride' is our 'stride'.
10621 * Applying the usual "rounded up m/n is (m+n-1)/n" and simplifying:
10622 * = 4 - (inputsize - stride - 3 + stride - 1) / stride
10623 * = 4 - (inputsize - 4) / stride;
10624 */
10625 level = 4 - (inputsize - 4) / stride;
10626 } else {
10627 /* For stage 2 translations the starting level is specified by the
10628 * VTCR_EL2.SL0 field (whose interpretation depends on the page size)
10629 */
1b4093ea
SS
10630 uint32_t sl0 = extract32(tcr->raw_tcr, 6, 2);
10631 uint32_t startlevel;
1853d5a9
EI
10632 bool ok;
10633
6e99f762 10634 if (!aarch64 || stride == 9) {
1853d5a9 10635 /* AArch32 or 4KB pages */
1b4093ea 10636 startlevel = 2 - sl0;
1853d5a9
EI
10637 } else {
10638 /* 16KB or 64KB pages */
1b4093ea 10639 startlevel = 3 - sl0;
1853d5a9
EI
10640 }
10641
10642 /* Check that the starting level is valid. */
6e99f762 10643 ok = check_s2_mmu_setup(cpu, aarch64, startlevel,
1b4093ea 10644 inputsize, stride);
1853d5a9 10645 if (!ok) {
da909b2c 10646 fault_type = ARMFault_Translation;
1853d5a9
EI
10647 goto do_fault;
10648 }
1b4093ea 10649 level = startlevel;
1853d5a9 10650 }
3dde962f 10651
dddb5223
SS
10652 indexmask_grainsize = (1ULL << (stride + 3)) - 1;
10653 indexmask = (1ULL << (inputsize - (stride * (4 - level)))) - 1;
3dde962f
PM
10654
10655 /* Now we can extract the actual base address from the TTBR */
2c8dd318 10656 descaddr = extract64(ttbr, 0, 48);
41a4bf1f
PM
10657 /*
10658 * We rely on this masking to clear the RES0 bits at the bottom of the TTBR
10659 * and also to mask out CnP (bit 0) which could validly be non-zero.
10660 */
dddb5223 10661 descaddr &= ~indexmask;
3dde962f 10662
6109769a 10663 /* The address field in the descriptor goes up to bit 39 for ARMv7
dddb5223
SS
10664 * but up to bit 47 for ARMv8, but we use the descaddrmask
10665 * up to bit 39 for AArch32, because we don't need other bits in that case
10666 * to construct next descriptor address (anyway they should be all zeroes).
6109769a 10667 */
6e99f762 10668 descaddrmask = ((1ull << (aarch64 ? 48 : 40)) - 1) &
dddb5223 10669 ~indexmask_grainsize;
6109769a 10670
ebca90e4
PM
10671 /* Secure accesses start with the page table in secure memory and
10672 * can be downgraded to non-secure at any step. Non-secure accesses
10673 * remain non-secure. We implement this by just ORing in the NSTable/NS
10674 * bits at each step.
10675 */
10676 tableattrs = regime_is_secure(env, mmu_idx) ? 0 : (1 << 4);
3dde962f
PM
10677 for (;;) {
10678 uint64_t descriptor;
ebca90e4 10679 bool nstable;
3dde962f 10680
dddb5223 10681 descaddr |= (address >> (stride * (4 - level))) & indexmask;
2c8dd318 10682 descaddr &= ~7ULL;
ebca90e4 10683 nstable = extract32(tableattrs, 4, 1);
3795a6de 10684 descriptor = arm_ldq_ptw(cs, descaddr, !nstable, mmu_idx, fi);
3b39d734 10685 if (fi->type != ARMFault_None) {
37785977
EI
10686 goto do_fault;
10687 }
10688
3dde962f
PM
10689 if (!(descriptor & 1) ||
10690 (!(descriptor & 2) && (level == 3))) {
10691 /* Invalid, or the Reserved level 3 encoding */
10692 goto do_fault;
10693 }
6109769a 10694 descaddr = descriptor & descaddrmask;
3dde962f
PM
10695
10696 if ((descriptor & 2) && (level < 3)) {
037c13c5 10697 /* Table entry. The top five bits are attributes which may
3dde962f
PM
10698 * propagate down through lower levels of the table (and
10699 * which are all arranged so that 0 means "no effect", so
10700 * we can gather them up by ORing in the bits at each level).
10701 */
10702 tableattrs |= extract64(descriptor, 59, 5);
10703 level++;
dddb5223 10704 indexmask = indexmask_grainsize;
3dde962f
PM
10705 continue;
10706 }
10707 /* Block entry at level 1 or 2, or page entry at level 3.
10708 * These are basically the same thing, although the number
10709 * of bits we pull in from the vaddr varies.
10710 */
973a5434 10711 page_size = (1ULL << ((stride * (4 - level)) + 3));
3dde962f 10712 descaddr |= (address & (page_size - 1));
6ab1a5ee 10713 /* Extract attributes from the descriptor */
d615efac
IC
10714 attrs = extract64(descriptor, 2, 10)
10715 | (extract64(descriptor, 52, 12) << 10);
6ab1a5ee 10716
97fa9350 10717 if (mmu_idx == ARMMMUIdx_Stage2) {
6ab1a5ee
EI
10718 /* Stage 2 table descriptors do not include any attribute fields */
10719 break;
10720 }
10721 /* Merge in attributes from table descriptors */
037c13c5 10722 attrs |= nstable << 3; /* NS */
1bafc2ba 10723 guarded = extract64(descriptor, 50, 1); /* GP */
ba97be9f 10724 if (param.hpd) {
037c13c5
RH
10725 /* HPD disables all the table attributes except NSTable. */
10726 break;
10727 }
10728 attrs |= extract32(tableattrs, 0, 2) << 11; /* XN, PXN */
3dde962f
PM
10729 /* The sense of AP[1] vs APTable[0] is reversed, as APTable[0] == 1
10730 * means "force PL1 access only", which means forcing AP[1] to 0.
10731 */
037c13c5
RH
10732 attrs &= ~(extract32(tableattrs, 2, 1) << 4); /* !APT[0] => AP[1] */
10733 attrs |= extract32(tableattrs, 3, 1) << 5; /* APT[1] => AP[2] */
3dde962f
PM
10734 break;
10735 }
10736 /* Here descaddr is the final physical address, and attributes
10737 * are all in attrs.
10738 */
da909b2c 10739 fault_type = ARMFault_AccessFlag;
3dde962f
PM
10740 if ((attrs & (1 << 8)) == 0) {
10741 /* Access flag */
10742 goto do_fault;
10743 }
d8e052b3
AJ
10744
10745 ap = extract32(attrs, 4, 2);
d8e052b3 10746 xn = extract32(attrs, 12, 1);
d8e052b3 10747
97fa9350 10748 if (mmu_idx == ARMMMUIdx_Stage2) {
6ab1a5ee
EI
10749 ns = true;
10750 *prot = get_S2prot(env, ap, xn);
10751 } else {
10752 ns = extract32(attrs, 3, 1);
10753 pxn = extract32(attrs, 11, 1);
6e99f762 10754 *prot = get_S1prot(env, mmu_idx, aarch64, ap, ns, xn, pxn);
6ab1a5ee 10755 }
d8e052b3 10756
da909b2c 10757 fault_type = ARMFault_Permission;
d8e052b3 10758 if (!(*prot & (1 << access_type))) {
3dde962f
PM
10759 goto do_fault;
10760 }
3dde962f 10761
8bf5b6a9
PM
10762 if (ns) {
10763 /* The NS bit will (as required by the architecture) have no effect if
10764 * the CPU doesn't support TZ or this is a non-secure translation
10765 * regime, because the attribute will already be non-secure.
10766 */
10767 txattrs->secure = false;
10768 }
1bafc2ba
RH
10769 /* When in aarch64 mode, and BTI is enabled, remember GP in the IOTLB. */
10770 if (aarch64 && guarded && cpu_isar_feature(aa64_bti, cpu)) {
10771 txattrs->target_tlb_bit0 = true;
10772 }
5b2d261d
AB
10773
10774 if (cacheattrs != NULL) {
97fa9350 10775 if (mmu_idx == ARMMMUIdx_Stage2) {
5b2d261d
AB
10776 cacheattrs->attrs = convert_stage2_attrs(env,
10777 extract32(attrs, 0, 4));
10778 } else {
10779 /* Index into MAIR registers for cache attributes */
10780 uint8_t attrindx = extract32(attrs, 0, 3);
10781 uint64_t mair = env->cp15.mair_el[regime_el(env, mmu_idx)];
10782 assert(attrindx <= 7);
10783 cacheattrs->attrs = extract64(mair, attrindx * 8, 8);
10784 }
10785 cacheattrs->shareability = extract32(attrs, 6, 2);
10786 }
10787
3dde962f
PM
10788 *phys_ptr = descaddr;
10789 *page_size_ptr = page_size;
b7cc4e82 10790 return false;
3dde962f
PM
10791
10792do_fault:
da909b2c
PM
10793 fi->type = fault_type;
10794 fi->level = level;
37785977 10795 /* Tag the error as S2 for failed S1 PTW at S2 or ordinary S2. */
97fa9350 10796 fi->stage2 = fi->s1ptw || (mmu_idx == ARMMMUIdx_Stage2);
b7cc4e82 10797 return true;
3dde962f
PM
10798}
10799
f6bda88f
PC
10800static inline void get_phys_addr_pmsav7_default(CPUARMState *env,
10801 ARMMMUIdx mmu_idx,
10802 int32_t address, int *prot)
10803{
3a00d560
MD
10804 if (!arm_feature(env, ARM_FEATURE_M)) {
10805 *prot = PAGE_READ | PAGE_WRITE;
10806 switch (address) {
10807 case 0xF0000000 ... 0xFFFFFFFF:
10808 if (regime_sctlr(env, mmu_idx) & SCTLR_V) {
10809 /* hivecs execing is ok */
10810 *prot |= PAGE_EXEC;
10811 }
10812 break;
10813 case 0x00000000 ... 0x7FFFFFFF:
f6bda88f 10814 *prot |= PAGE_EXEC;
3a00d560
MD
10815 break;
10816 }
10817 } else {
10818 /* Default system address map for M profile cores.
10819 * The architecture specifies which regions are execute-never;
10820 * at the MPU level no other checks are defined.
10821 */
10822 switch (address) {
10823 case 0x00000000 ... 0x1fffffff: /* ROM */
10824 case 0x20000000 ... 0x3fffffff: /* SRAM */
10825 case 0x60000000 ... 0x7fffffff: /* RAM */
10826 case 0x80000000 ... 0x9fffffff: /* RAM */
10827 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
10828 break;
10829 case 0x40000000 ... 0x5fffffff: /* Peripheral */
10830 case 0xa0000000 ... 0xbfffffff: /* Device */
10831 case 0xc0000000 ... 0xdfffffff: /* Device */
10832 case 0xe0000000 ... 0xffffffff: /* System */
10833 *prot = PAGE_READ | PAGE_WRITE;
10834 break;
10835 default:
10836 g_assert_not_reached();
f6bda88f 10837 }
f6bda88f 10838 }
f6bda88f
PC
10839}
10840
29c483a5
MD
10841static bool pmsav7_use_background_region(ARMCPU *cpu,
10842 ARMMMUIdx mmu_idx, bool is_user)
10843{
10844 /* Return true if we should use the default memory map as a
10845 * "background" region if there are no hits against any MPU regions.
10846 */
10847 CPUARMState *env = &cpu->env;
10848
10849 if (is_user) {
10850 return false;
10851 }
10852
10853 if (arm_feature(env, ARM_FEATURE_M)) {
ecf5e8ea
PM
10854 return env->v7m.mpu_ctrl[regime_is_secure(env, mmu_idx)]
10855 & R_V7M_MPU_CTRL_PRIVDEFENA_MASK;
29c483a5
MD
10856 } else {
10857 return regime_sctlr(env, mmu_idx) & SCTLR_BR;
10858 }
10859}
10860
38aaa60c
PM
10861static inline bool m_is_ppb_region(CPUARMState *env, uint32_t address)
10862{
10863 /* True if address is in the M profile PPB region 0xe0000000 - 0xe00fffff */
10864 return arm_feature(env, ARM_FEATURE_M) &&
10865 extract32(address, 20, 12) == 0xe00;
10866}
10867
bf446a11
PM
10868static inline bool m_is_system_region(CPUARMState *env, uint32_t address)
10869{
10870 /* True if address is in the M profile system region
10871 * 0xe0000000 - 0xffffffff
10872 */
10873 return arm_feature(env, ARM_FEATURE_M) && extract32(address, 29, 3) == 0x7;
10874}
10875
f6bda88f 10876static bool get_phys_addr_pmsav7(CPUARMState *env, uint32_t address,
03ae85f8 10877 MMUAccessType access_type, ARMMMUIdx mmu_idx,
9375ad15 10878 hwaddr *phys_ptr, int *prot,
e5e40999 10879 target_ulong *page_size,
9375ad15 10880 ARMMMUFaultInfo *fi)
f6bda88f 10881{
2fc0cc0e 10882 ARMCPU *cpu = env_archcpu(env);
f6bda88f
PC
10883 int n;
10884 bool is_user = regime_is_user(env, mmu_idx);
10885
10886 *phys_ptr = address;
e5e40999 10887 *page_size = TARGET_PAGE_SIZE;
f6bda88f
PC
10888 *prot = 0;
10889
38aaa60c
PM
10890 if (regime_translation_disabled(env, mmu_idx) ||
10891 m_is_ppb_region(env, address)) {
10892 /* MPU disabled or M profile PPB access: use default memory map.
10893 * The other case which uses the default memory map in the
10894 * v7M ARM ARM pseudocode is exception vector reads from the vector
10895 * table. In QEMU those accesses are done in arm_v7m_load_vector(),
10896 * which always does a direct read using address_space_ldl(), rather
10897 * than going via this function, so we don't need to check that here.
10898 */
f6bda88f
PC
10899 get_phys_addr_pmsav7_default(env, mmu_idx, address, prot);
10900 } else { /* MPU enabled */
10901 for (n = (int)cpu->pmsav7_dregion - 1; n >= 0; n--) {
10902 /* region search */
10903 uint32_t base = env->pmsav7.drbar[n];
10904 uint32_t rsize = extract32(env->pmsav7.drsr[n], 1, 5);
10905 uint32_t rmask;
10906 bool srdis = false;
10907
10908 if (!(env->pmsav7.drsr[n] & 0x1)) {
10909 continue;
10910 }
10911
10912 if (!rsize) {
c9f9f124
MD
10913 qemu_log_mask(LOG_GUEST_ERROR,
10914 "DRSR[%d]: Rsize field cannot be 0\n", n);
f6bda88f
PC
10915 continue;
10916 }
10917 rsize++;
10918 rmask = (1ull << rsize) - 1;
10919
10920 if (base & rmask) {
c9f9f124
MD
10921 qemu_log_mask(LOG_GUEST_ERROR,
10922 "DRBAR[%d]: 0x%" PRIx32 " misaligned "
10923 "to DRSR region size, mask = 0x%" PRIx32 "\n",
10924 n, base, rmask);
f6bda88f
PC
10925 continue;
10926 }
10927
10928 if (address < base || address > base + rmask) {
9d2b5a58
PM
10929 /*
10930 * Address not in this region. We must check whether the
10931 * region covers addresses in the same page as our address.
10932 * In that case we must not report a size that covers the
10933 * whole page for a subsequent hit against a different MPU
10934 * region or the background region, because it would result in
10935 * incorrect TLB hits for subsequent accesses to addresses that
10936 * are in this MPU region.
10937 */
10938 if (ranges_overlap(base, rmask,
10939 address & TARGET_PAGE_MASK,
10940 TARGET_PAGE_SIZE)) {
10941 *page_size = 1;
10942 }
f6bda88f
PC
10943 continue;
10944 }
10945
10946 /* Region matched */
10947
10948 if (rsize >= 8) { /* no subregions for regions < 256 bytes */
10949 int i, snd;
10950 uint32_t srdis_mask;
10951
10952 rsize -= 3; /* sub region size (power of 2) */
10953 snd = ((address - base) >> rsize) & 0x7;
10954 srdis = extract32(env->pmsav7.drsr[n], snd + 8, 1);
10955
10956 srdis_mask = srdis ? 0x3 : 0x0;
10957 for (i = 2; i <= 8 && rsize < TARGET_PAGE_BITS; i *= 2) {
10958 /* This will check in groups of 2, 4 and then 8, whether
10959 * the subregion bits are consistent. rsize is incremented
10960 * back up to give the region size, considering consistent
10961 * adjacent subregions as one region. Stop testing if rsize
10962 * is already big enough for an entire QEMU page.
10963 */
10964 int snd_rounded = snd & ~(i - 1);
10965 uint32_t srdis_multi = extract32(env->pmsav7.drsr[n],
10966 snd_rounded + 8, i);
10967 if (srdis_mask ^ srdis_multi) {
10968 break;
10969 }
10970 srdis_mask = (srdis_mask << i) | srdis_mask;
10971 rsize++;
10972 }
10973 }
f6bda88f
PC
10974 if (srdis) {
10975 continue;
10976 }
e5e40999
PM
10977 if (rsize < TARGET_PAGE_BITS) {
10978 *page_size = 1 << rsize;
10979 }
f6bda88f
PC
10980 break;
10981 }
10982
10983 if (n == -1) { /* no hits */
29c483a5 10984 if (!pmsav7_use_background_region(cpu, mmu_idx, is_user)) {
f6bda88f 10985 /* background fault */
9375ad15 10986 fi->type = ARMFault_Background;
f6bda88f
PC
10987 return true;
10988 }
10989 get_phys_addr_pmsav7_default(env, mmu_idx, address, prot);
10990 } else { /* a MPU hit! */
10991 uint32_t ap = extract32(env->pmsav7.dracr[n], 8, 3);
bf446a11
PM
10992 uint32_t xn = extract32(env->pmsav7.dracr[n], 12, 1);
10993
10994 if (m_is_system_region(env, address)) {
10995 /* System space is always execute never */
10996 xn = 1;
10997 }
f6bda88f
PC
10998
10999 if (is_user) { /* User mode AP bit decoding */
11000 switch (ap) {
11001 case 0:
11002 case 1:
11003 case 5:
11004 break; /* no access */
11005 case 3:
11006 *prot |= PAGE_WRITE;
11007 /* fall through */
11008 case 2:
11009 case 6:
11010 *prot |= PAGE_READ | PAGE_EXEC;
11011 break;
8638f1ad
PM
11012 case 7:
11013 /* for v7M, same as 6; for R profile a reserved value */
11014 if (arm_feature(env, ARM_FEATURE_M)) {
11015 *prot |= PAGE_READ | PAGE_EXEC;
11016 break;
11017 }
11018 /* fall through */
f6bda88f
PC
11019 default:
11020 qemu_log_mask(LOG_GUEST_ERROR,
c9f9f124
MD
11021 "DRACR[%d]: Bad value for AP bits: 0x%"
11022 PRIx32 "\n", n, ap);
f6bda88f
PC
11023 }
11024 } else { /* Priv. mode AP bits decoding */
11025 switch (ap) {
11026 case 0:
11027 break; /* no access */
11028 case 1:
11029 case 2:
11030 case 3:
11031 *prot |= PAGE_WRITE;
11032 /* fall through */
11033 case 5:
11034 case 6:
11035 *prot |= PAGE_READ | PAGE_EXEC;
11036 break;
8638f1ad
PM
11037 case 7:
11038 /* for v7M, same as 6; for R profile a reserved value */
11039 if (arm_feature(env, ARM_FEATURE_M)) {
11040 *prot |= PAGE_READ | PAGE_EXEC;
11041 break;
11042 }
11043 /* fall through */
f6bda88f
PC
11044 default:
11045 qemu_log_mask(LOG_GUEST_ERROR,
c9f9f124
MD
11046 "DRACR[%d]: Bad value for AP bits: 0x%"
11047 PRIx32 "\n", n, ap);
f6bda88f
PC
11048 }
11049 }
11050
11051 /* execute never */
bf446a11 11052 if (xn) {
f6bda88f
PC
11053 *prot &= ~PAGE_EXEC;
11054 }
11055 }
11056 }
11057
9375ad15
PM
11058 fi->type = ARMFault_Permission;
11059 fi->level = 1;
f6bda88f
PC
11060 return !(*prot & (1 << access_type));
11061}
11062
35337cc3
PM
11063static bool v8m_is_sau_exempt(CPUARMState *env,
11064 uint32_t address, MMUAccessType access_type)
11065{
11066 /* The architecture specifies that certain address ranges are
11067 * exempt from v8M SAU/IDAU checks.
11068 */
11069 return
11070 (access_type == MMU_INST_FETCH && m_is_system_region(env, address)) ||
11071 (address >= 0xe0000000 && address <= 0xe0002fff) ||
11072 (address >= 0xe000e000 && address <= 0xe000efff) ||
11073 (address >= 0xe002e000 && address <= 0xe002efff) ||
11074 (address >= 0xe0040000 && address <= 0xe0041fff) ||
11075 (address >= 0xe00ff000 && address <= 0xe00fffff);
11076}
11077
787a7e76 11078void v8m_security_lookup(CPUARMState *env, uint32_t address,
35337cc3
PM
11079 MMUAccessType access_type, ARMMMUIdx mmu_idx,
11080 V8M_SAttributes *sattrs)
11081{
11082 /* Look up the security attributes for this address. Compare the
11083 * pseudocode SecurityCheck() function.
11084 * We assume the caller has zero-initialized *sattrs.
11085 */
2fc0cc0e 11086 ARMCPU *cpu = env_archcpu(env);
35337cc3 11087 int r;
181962fd
PM
11088 bool idau_exempt = false, idau_ns = true, idau_nsc = true;
11089 int idau_region = IREGION_NOTVALID;
72042435
PM
11090 uint32_t addr_page_base = address & TARGET_PAGE_MASK;
11091 uint32_t addr_page_limit = addr_page_base + (TARGET_PAGE_SIZE - 1);
35337cc3 11092
181962fd
PM
11093 if (cpu->idau) {
11094 IDAUInterfaceClass *iic = IDAU_INTERFACE_GET_CLASS(cpu->idau);
11095 IDAUInterface *ii = IDAU_INTERFACE(cpu->idau);
11096
11097 iic->check(ii, address, &idau_region, &idau_exempt, &idau_ns,
11098 &idau_nsc);
11099 }
35337cc3
PM
11100
11101 if (access_type == MMU_INST_FETCH && extract32(address, 28, 4) == 0xf) {
11102 /* 0xf0000000..0xffffffff is always S for insn fetches */
11103 return;
11104 }
11105
181962fd 11106 if (idau_exempt || v8m_is_sau_exempt(env, address, access_type)) {
35337cc3
PM
11107 sattrs->ns = !regime_is_secure(env, mmu_idx);
11108 return;
11109 }
11110
181962fd
PM
11111 if (idau_region != IREGION_NOTVALID) {
11112 sattrs->irvalid = true;
11113 sattrs->iregion = idau_region;
11114 }
11115
35337cc3
PM
11116 switch (env->sau.ctrl & 3) {
11117 case 0: /* SAU.ENABLE == 0, SAU.ALLNS == 0 */
11118 break;
11119 case 2: /* SAU.ENABLE == 0, SAU.ALLNS == 1 */
11120 sattrs->ns = true;
11121 break;
11122 default: /* SAU.ENABLE == 1 */
11123 for (r = 0; r < cpu->sau_sregion; r++) {
11124 if (env->sau.rlar[r] & 1) {
11125 uint32_t base = env->sau.rbar[r] & ~0x1f;
11126 uint32_t limit = env->sau.rlar[r] | 0x1f;
11127
11128 if (base <= address && limit >= address) {
72042435
PM
11129 if (base > addr_page_base || limit < addr_page_limit) {
11130 sattrs->subpage = true;
11131 }
35337cc3
PM
11132 if (sattrs->srvalid) {
11133 /* If we hit in more than one region then we must report
11134 * as Secure, not NS-Callable, with no valid region
11135 * number info.
11136 */
11137 sattrs->ns = false;
11138 sattrs->nsc = false;
11139 sattrs->sregion = 0;
11140 sattrs->srvalid = false;
11141 break;
11142 } else {
11143 if (env->sau.rlar[r] & 2) {
11144 sattrs->nsc = true;
11145 } else {
11146 sattrs->ns = true;
11147 }
11148 sattrs->srvalid = true;
11149 sattrs->sregion = r;
11150 }
9d2b5a58
PM
11151 } else {
11152 /*
11153 * Address not in this region. We must check whether the
11154 * region covers addresses in the same page as our address.
11155 * In that case we must not report a size that covers the
11156 * whole page for a subsequent hit against a different MPU
11157 * region or the background region, because it would result
11158 * in incorrect TLB hits for subsequent accesses to
11159 * addresses that are in this MPU region.
11160 */
11161 if (limit >= base &&
11162 ranges_overlap(base, limit - base + 1,
11163 addr_page_base,
11164 TARGET_PAGE_SIZE)) {
11165 sattrs->subpage = true;
11166 }
35337cc3
PM
11167 }
11168 }
11169 }
7e3f1223
TR
11170 break;
11171 }
35337cc3 11172
7e3f1223
TR
11173 /*
11174 * The IDAU will override the SAU lookup results if it specifies
11175 * higher security than the SAU does.
11176 */
11177 if (!idau_ns) {
11178 if (sattrs->ns || (!idau_nsc && sattrs->nsc)) {
11179 sattrs->ns = false;
11180 sattrs->nsc = idau_nsc;
181962fd 11181 }
35337cc3
PM
11182 }
11183}
11184
787a7e76 11185bool pmsav8_mpu_lookup(CPUARMState *env, uint32_t address,
54317c0f
PM
11186 MMUAccessType access_type, ARMMMUIdx mmu_idx,
11187 hwaddr *phys_ptr, MemTxAttrs *txattrs,
72042435
PM
11188 int *prot, bool *is_subpage,
11189 ARMMMUFaultInfo *fi, uint32_t *mregion)
54317c0f
PM
11190{
11191 /* Perform a PMSAv8 MPU lookup (without also doing the SAU check
11192 * that a full phys-to-virt translation does).
11193 * mregion is (if not NULL) set to the region number which matched,
11194 * or -1 if no region number is returned (MPU off, address did not
11195 * hit a region, address hit in multiple regions).
72042435
PM
11196 * We set is_subpage to true if the region hit doesn't cover the
11197 * entire TARGET_PAGE the address is within.
54317c0f 11198 */
2fc0cc0e 11199 ARMCPU *cpu = env_archcpu(env);
504e3cc3 11200 bool is_user = regime_is_user(env, mmu_idx);
62c58ee0 11201 uint32_t secure = regime_is_secure(env, mmu_idx);
504e3cc3
PM
11202 int n;
11203 int matchregion = -1;
11204 bool hit = false;
72042435
PM
11205 uint32_t addr_page_base = address & TARGET_PAGE_MASK;
11206 uint32_t addr_page_limit = addr_page_base + (TARGET_PAGE_SIZE - 1);
504e3cc3 11207
72042435 11208 *is_subpage = false;
504e3cc3
PM
11209 *phys_ptr = address;
11210 *prot = 0;
54317c0f
PM
11211 if (mregion) {
11212 *mregion = -1;
35337cc3
PM
11213 }
11214
504e3cc3
PM
11215 /* Unlike the ARM ARM pseudocode, we don't need to check whether this
11216 * was an exception vector read from the vector table (which is always
11217 * done using the default system address map), because those accesses
11218 * are done in arm_v7m_load_vector(), which always does a direct
11219 * read using address_space_ldl(), rather than going via this function.
11220 */
11221 if (regime_translation_disabled(env, mmu_idx)) { /* MPU disabled */
11222 hit = true;
11223 } else if (m_is_ppb_region(env, address)) {
11224 hit = true;
504e3cc3 11225 } else {
cff21316
PM
11226 if (pmsav7_use_background_region(cpu, mmu_idx, is_user)) {
11227 hit = true;
11228 }
11229
504e3cc3
PM
11230 for (n = (int)cpu->pmsav7_dregion - 1; n >= 0; n--) {
11231 /* region search */
11232 /* Note that the base address is bits [31:5] from the register
11233 * with bits [4:0] all zeroes, but the limit address is bits
11234 * [31:5] from the register with bits [4:0] all ones.
11235 */
62c58ee0
PM
11236 uint32_t base = env->pmsav8.rbar[secure][n] & ~0x1f;
11237 uint32_t limit = env->pmsav8.rlar[secure][n] | 0x1f;
504e3cc3 11238
62c58ee0 11239 if (!(env->pmsav8.rlar[secure][n] & 0x1)) {
504e3cc3
PM
11240 /* Region disabled */
11241 continue;
11242 }
11243
11244 if (address < base || address > limit) {
9d2b5a58
PM
11245 /*
11246 * Address not in this region. We must check whether the
11247 * region covers addresses in the same page as our address.
11248 * In that case we must not report a size that covers the
11249 * whole page for a subsequent hit against a different MPU
11250 * region or the background region, because it would result in
11251 * incorrect TLB hits for subsequent accesses to addresses that
11252 * are in this MPU region.
11253 */
11254 if (limit >= base &&
11255 ranges_overlap(base, limit - base + 1,
11256 addr_page_base,
11257 TARGET_PAGE_SIZE)) {
11258 *is_subpage = true;
11259 }
504e3cc3
PM
11260 continue;
11261 }
11262
72042435
PM
11263 if (base > addr_page_base || limit < addr_page_limit) {
11264 *is_subpage = true;
11265 }
11266
cff21316 11267 if (matchregion != -1) {
504e3cc3
PM
11268 /* Multiple regions match -- always a failure (unlike
11269 * PMSAv7 where highest-numbered-region wins)
11270 */
3f551b5b
PM
11271 fi->type = ARMFault_Permission;
11272 fi->level = 1;
504e3cc3
PM
11273 return true;
11274 }
11275
11276 matchregion = n;
11277 hit = true;
504e3cc3
PM
11278 }
11279 }
11280
11281 if (!hit) {
11282 /* background fault */
3f551b5b 11283 fi->type = ARMFault_Background;
504e3cc3
PM
11284 return true;
11285 }
11286
11287 if (matchregion == -1) {
11288 /* hit using the background region */
11289 get_phys_addr_pmsav7_default(env, mmu_idx, address, prot);
11290 } else {
62c58ee0
PM
11291 uint32_t ap = extract32(env->pmsav8.rbar[secure][matchregion], 1, 2);
11292 uint32_t xn = extract32(env->pmsav8.rbar[secure][matchregion], 0, 1);
504e3cc3
PM
11293
11294 if (m_is_system_region(env, address)) {
11295 /* System space is always execute never */
11296 xn = 1;
11297 }
11298
11299 *prot = simple_ap_to_rw_prot(env, mmu_idx, ap);
11300 if (*prot && !xn) {
11301 *prot |= PAGE_EXEC;
11302 }
11303 /* We don't need to look the attribute up in the MAIR0/MAIR1
11304 * registers because that only tells us about cacheability.
11305 */
54317c0f
PM
11306 if (mregion) {
11307 *mregion = matchregion;
11308 }
504e3cc3
PM
11309 }
11310
3f551b5b
PM
11311 fi->type = ARMFault_Permission;
11312 fi->level = 1;
504e3cc3
PM
11313 return !(*prot & (1 << access_type));
11314}
11315
54317c0f
PM
11316
11317static bool get_phys_addr_pmsav8(CPUARMState *env, uint32_t address,
11318 MMUAccessType access_type, ARMMMUIdx mmu_idx,
11319 hwaddr *phys_ptr, MemTxAttrs *txattrs,
72042435
PM
11320 int *prot, target_ulong *page_size,
11321 ARMMMUFaultInfo *fi)
54317c0f
PM
11322{
11323 uint32_t secure = regime_is_secure(env, mmu_idx);
11324 V8M_SAttributes sattrs = {};
72042435
PM
11325 bool ret;
11326 bool mpu_is_subpage;
54317c0f
PM
11327
11328 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
11329 v8m_security_lookup(env, address, access_type, mmu_idx, &sattrs);
11330 if (access_type == MMU_INST_FETCH) {
11331 /* Instruction fetches always use the MMU bank and the
11332 * transaction attribute determined by the fetch address,
11333 * regardless of CPU state. This is painful for QEMU
11334 * to handle, because it would mean we need to encode
11335 * into the mmu_idx not just the (user, negpri) information
11336 * for the current security state but also that for the
11337 * other security state, which would balloon the number
11338 * of mmu_idx values needed alarmingly.
11339 * Fortunately we can avoid this because it's not actually
11340 * possible to arbitrarily execute code from memory with
11341 * the wrong security attribute: it will always generate
11342 * an exception of some kind or another, apart from the
11343 * special case of an NS CPU executing an SG instruction
11344 * in S&NSC memory. So we always just fail the translation
11345 * here and sort things out in the exception handler
11346 * (including possibly emulating an SG instruction).
11347 */
11348 if (sattrs.ns != !secure) {
3f551b5b
PM
11349 if (sattrs.nsc) {
11350 fi->type = ARMFault_QEMU_NSCExec;
11351 } else {
11352 fi->type = ARMFault_QEMU_SFault;
11353 }
72042435 11354 *page_size = sattrs.subpage ? 1 : TARGET_PAGE_SIZE;
54317c0f
PM
11355 *phys_ptr = address;
11356 *prot = 0;
11357 return true;
11358 }
11359 } else {
11360 /* For data accesses we always use the MMU bank indicated
11361 * by the current CPU state, but the security attributes
11362 * might downgrade a secure access to nonsecure.
11363 */
11364 if (sattrs.ns) {
11365 txattrs->secure = false;
11366 } else if (!secure) {
11367 /* NS access to S memory must fault.
11368 * Architecturally we should first check whether the
11369 * MPU information for this address indicates that we
11370 * are doing an unaligned access to Device memory, which
11371 * should generate a UsageFault instead. QEMU does not
11372 * currently check for that kind of unaligned access though.
11373 * If we added it we would need to do so as a special case
11374 * for M_FAKE_FSR_SFAULT in arm_v7m_cpu_do_interrupt().
11375 */
3f551b5b 11376 fi->type = ARMFault_QEMU_SFault;
72042435 11377 *page_size = sattrs.subpage ? 1 : TARGET_PAGE_SIZE;
54317c0f
PM
11378 *phys_ptr = address;
11379 *prot = 0;
11380 return true;
11381 }
11382 }
11383 }
11384
72042435
PM
11385 ret = pmsav8_mpu_lookup(env, address, access_type, mmu_idx, phys_ptr,
11386 txattrs, prot, &mpu_is_subpage, fi, NULL);
72042435
PM
11387 *page_size = sattrs.subpage || mpu_is_subpage ? 1 : TARGET_PAGE_SIZE;
11388 return ret;
54317c0f
PM
11389}
11390
13689d43 11391static bool get_phys_addr_pmsav5(CPUARMState *env, uint32_t address,
03ae85f8 11392 MMUAccessType access_type, ARMMMUIdx mmu_idx,
53a4e5c5
PM
11393 hwaddr *phys_ptr, int *prot,
11394 ARMMMUFaultInfo *fi)
9ee6e8bb
PB
11395{
11396 int n;
11397 uint32_t mask;
11398 uint32_t base;
0480f69a 11399 bool is_user = regime_is_user(env, mmu_idx);
9ee6e8bb 11400
3279adb9
PM
11401 if (regime_translation_disabled(env, mmu_idx)) {
11402 /* MPU disabled. */
11403 *phys_ptr = address;
11404 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
11405 return false;
11406 }
11407
9ee6e8bb
PB
11408 *phys_ptr = address;
11409 for (n = 7; n >= 0; n--) {
554b0b09 11410 base = env->cp15.c6_region[n];
87c3d486 11411 if ((base & 1) == 0) {
554b0b09 11412 continue;
87c3d486 11413 }
554b0b09
PM
11414 mask = 1 << ((base >> 1) & 0x1f);
11415 /* Keep this shift separate from the above to avoid an
11416 (undefined) << 32. */
11417 mask = (mask << 1) - 1;
87c3d486 11418 if (((base ^ address) & ~mask) == 0) {
554b0b09 11419 break;
87c3d486 11420 }
9ee6e8bb 11421 }
87c3d486 11422 if (n < 0) {
53a4e5c5 11423 fi->type = ARMFault_Background;
b7cc4e82 11424 return true;
87c3d486 11425 }
9ee6e8bb 11426
03ae85f8 11427 if (access_type == MMU_INST_FETCH) {
7e09797c 11428 mask = env->cp15.pmsav5_insn_ap;
9ee6e8bb 11429 } else {
7e09797c 11430 mask = env->cp15.pmsav5_data_ap;
9ee6e8bb
PB
11431 }
11432 mask = (mask >> (n * 4)) & 0xf;
11433 switch (mask) {
11434 case 0:
53a4e5c5
PM
11435 fi->type = ARMFault_Permission;
11436 fi->level = 1;
b7cc4e82 11437 return true;
9ee6e8bb 11438 case 1:
87c3d486 11439 if (is_user) {
53a4e5c5
PM
11440 fi->type = ARMFault_Permission;
11441 fi->level = 1;
b7cc4e82 11442 return true;
87c3d486 11443 }
554b0b09
PM
11444 *prot = PAGE_READ | PAGE_WRITE;
11445 break;
9ee6e8bb 11446 case 2:
554b0b09 11447 *prot = PAGE_READ;
87c3d486 11448 if (!is_user) {
554b0b09 11449 *prot |= PAGE_WRITE;
87c3d486 11450 }
554b0b09 11451 break;
9ee6e8bb 11452 case 3:
554b0b09
PM
11453 *prot = PAGE_READ | PAGE_WRITE;
11454 break;
9ee6e8bb 11455 case 5:
87c3d486 11456 if (is_user) {
53a4e5c5
PM
11457 fi->type = ARMFault_Permission;
11458 fi->level = 1;
b7cc4e82 11459 return true;
87c3d486 11460 }
554b0b09
PM
11461 *prot = PAGE_READ;
11462 break;
9ee6e8bb 11463 case 6:
554b0b09
PM
11464 *prot = PAGE_READ;
11465 break;
9ee6e8bb 11466 default:
554b0b09 11467 /* Bad permission. */
53a4e5c5
PM
11468 fi->type = ARMFault_Permission;
11469 fi->level = 1;
b7cc4e82 11470 return true;
9ee6e8bb 11471 }
3ad493fc 11472 *prot |= PAGE_EXEC;
b7cc4e82 11473 return false;
9ee6e8bb
PB
11474}
11475
5b2d261d
AB
11476/* Combine either inner or outer cacheability attributes for normal
11477 * memory, according to table D4-42 and pseudocode procedure
11478 * CombineS1S2AttrHints() of ARM DDI 0487B.b (the ARMv8 ARM).
11479 *
11480 * NB: only stage 1 includes allocation hints (RW bits), leading to
11481 * some asymmetry.
11482 */
11483static uint8_t combine_cacheattr_nibble(uint8_t s1, uint8_t s2)
11484{
11485 if (s1 == 4 || s2 == 4) {
11486 /* non-cacheable has precedence */
11487 return 4;
11488 } else if (extract32(s1, 2, 2) == 0 || extract32(s1, 2, 2) == 2) {
11489 /* stage 1 write-through takes precedence */
11490 return s1;
11491 } else if (extract32(s2, 2, 2) == 2) {
11492 /* stage 2 write-through takes precedence, but the allocation hint
11493 * is still taken from stage 1
11494 */
11495 return (2 << 2) | extract32(s1, 0, 2);
11496 } else { /* write-back */
11497 return s1;
11498 }
11499}
11500
11501/* Combine S1 and S2 cacheability/shareability attributes, per D4.5.4
11502 * and CombineS1S2Desc()
11503 *
11504 * @s1: Attributes from stage 1 walk
11505 * @s2: Attributes from stage 2 walk
11506 */
11507static ARMCacheAttrs combine_cacheattrs(ARMCacheAttrs s1, ARMCacheAttrs s2)
11508{
11509 uint8_t s1lo = extract32(s1.attrs, 0, 4), s2lo = extract32(s2.attrs, 0, 4);
11510 uint8_t s1hi = extract32(s1.attrs, 4, 4), s2hi = extract32(s2.attrs, 4, 4);
11511 ARMCacheAttrs ret;
11512
11513 /* Combine shareability attributes (table D4-43) */
11514 if (s1.shareability == 2 || s2.shareability == 2) {
11515 /* if either are outer-shareable, the result is outer-shareable */
11516 ret.shareability = 2;
11517 } else if (s1.shareability == 3 || s2.shareability == 3) {
11518 /* if either are inner-shareable, the result is inner-shareable */
11519 ret.shareability = 3;
11520 } else {
11521 /* both non-shareable */
11522 ret.shareability = 0;
11523 }
11524
11525 /* Combine memory type and cacheability attributes */
11526 if (s1hi == 0 || s2hi == 0) {
11527 /* Device has precedence over normal */
11528 if (s1lo == 0 || s2lo == 0) {
11529 /* nGnRnE has precedence over anything */
11530 ret.attrs = 0;
11531 } else if (s1lo == 4 || s2lo == 4) {
11532 /* non-Reordering has precedence over Reordering */
11533 ret.attrs = 4; /* nGnRE */
11534 } else if (s1lo == 8 || s2lo == 8) {
11535 /* non-Gathering has precedence over Gathering */
11536 ret.attrs = 8; /* nGRE */
11537 } else {
11538 ret.attrs = 0xc; /* GRE */
11539 }
11540
11541 /* Any location for which the resultant memory type is any
11542 * type of Device memory is always treated as Outer Shareable.
11543 */
11544 ret.shareability = 2;
11545 } else { /* Normal memory */
11546 /* Outer/inner cacheability combine independently */
11547 ret.attrs = combine_cacheattr_nibble(s1hi, s2hi) << 4
11548 | combine_cacheattr_nibble(s1lo, s2lo);
11549
11550 if (ret.attrs == 0x44) {
11551 /* Any location for which the resultant memory type is Normal
11552 * Inner Non-cacheable, Outer Non-cacheable is always treated
11553 * as Outer Shareable.
11554 */
11555 ret.shareability = 2;
11556 }
11557 }
11558
11559 return ret;
11560}
11561
11562
702a9357
PM
11563/* get_phys_addr - get the physical address for this virtual address
11564 *
11565 * Find the physical address corresponding to the given virtual address,
11566 * by doing a translation table walk on MMU based systems or using the
11567 * MPU state on MPU based systems.
11568 *
b7cc4e82
PC
11569 * Returns false if the translation was successful. Otherwise, phys_ptr, attrs,
11570 * prot and page_size may not be filled in, and the populated fsr value provides
702a9357
PM
11571 * information on why the translation aborted, in the format of a
11572 * DFSR/IFSR fault register, with the following caveats:
11573 * * we honour the short vs long DFSR format differences.
11574 * * the WnR bit is never set (the caller must do this).
f6bda88f 11575 * * for PSMAv5 based systems we don't bother to return a full FSR format
702a9357
PM
11576 * value.
11577 *
11578 * @env: CPUARMState
11579 * @address: virtual address to get physical address for
11580 * @access_type: 0 for read, 1 for write, 2 for execute
d3649702 11581 * @mmu_idx: MMU index indicating required translation regime
702a9357 11582 * @phys_ptr: set to the physical address corresponding to the virtual address
8bf5b6a9 11583 * @attrs: set to the memory transaction attributes to use
702a9357
PM
11584 * @prot: set to the permissions for the page containing phys_ptr
11585 * @page_size: set to the size of the page containing phys_ptr
5b2d261d
AB
11586 * @fi: set to fault info if the translation fails
11587 * @cacheattrs: (if non-NULL) set to the cacheability/shareability attributes
702a9357 11588 */
ebae861f
PMD
11589bool get_phys_addr(CPUARMState *env, target_ulong address,
11590 MMUAccessType access_type, ARMMMUIdx mmu_idx,
11591 hwaddr *phys_ptr, MemTxAttrs *attrs, int *prot,
11592 target_ulong *page_size,
11593 ARMMMUFaultInfo *fi, ARMCacheAttrs *cacheattrs)
9ee6e8bb 11594{
452ef8cb
RH
11595 if (mmu_idx == ARMMMUIdx_E10_0 ||
11596 mmu_idx == ARMMMUIdx_E10_1 ||
11597 mmu_idx == ARMMMUIdx_E10_1_PAN) {
9b539263
EI
11598 /* Call ourselves recursively to do the stage 1 and then stage 2
11599 * translations.
0480f69a 11600 */
9b539263
EI
11601 if (arm_feature(env, ARM_FEATURE_EL2)) {
11602 hwaddr ipa;
11603 int s2_prot;
11604 int ret;
5b2d261d 11605 ARMCacheAttrs cacheattrs2 = {};
9b539263
EI
11606
11607 ret = get_phys_addr(env, address, access_type,
8bd5c820 11608 stage_1_mmu_idx(mmu_idx), &ipa, attrs,
bc52bfeb 11609 prot, page_size, fi, cacheattrs);
9b539263
EI
11610
11611 /* If S1 fails or S2 is disabled, return early. */
97fa9350 11612 if (ret || regime_translation_disabled(env, ARMMMUIdx_Stage2)) {
9b539263
EI
11613 *phys_ptr = ipa;
11614 return ret;
11615 }
11616
11617 /* S1 is done. Now do S2 translation. */
97fa9350 11618 ret = get_phys_addr_lpae(env, ipa, access_type, ARMMMUIdx_Stage2,
9b539263 11619 phys_ptr, attrs, &s2_prot,
da909b2c 11620 page_size, fi,
5b2d261d 11621 cacheattrs != NULL ? &cacheattrs2 : NULL);
9b539263
EI
11622 fi->s2addr = ipa;
11623 /* Combine the S1 and S2 perms. */
11624 *prot &= s2_prot;
5b2d261d
AB
11625
11626 /* Combine the S1 and S2 cache attributes, if needed */
11627 if (!ret && cacheattrs != NULL) {
9d1bab33
PM
11628 if (env->cp15.hcr_el2 & HCR_DC) {
11629 /*
11630 * HCR.DC forces the first stage attributes to
11631 * Normal Non-Shareable,
11632 * Inner Write-Back Read-Allocate Write-Allocate,
11633 * Outer Write-Back Read-Allocate Write-Allocate.
11634 */
11635 cacheattrs->attrs = 0xff;
11636 cacheattrs->shareability = 0;
11637 }
5b2d261d
AB
11638 *cacheattrs = combine_cacheattrs(*cacheattrs, cacheattrs2);
11639 }
11640
9b539263
EI
11641 return ret;
11642 } else {
11643 /*
11644 * For non-EL2 CPUs a stage1+stage2 translation is just stage 1.
11645 */
8bd5c820 11646 mmu_idx = stage_1_mmu_idx(mmu_idx);
9b539263 11647 }
0480f69a 11648 }
d3649702 11649
8bf5b6a9
PM
11650 /* The page table entries may downgrade secure to non-secure, but
11651 * cannot upgrade an non-secure translation regime's attributes
11652 * to secure.
11653 */
11654 attrs->secure = regime_is_secure(env, mmu_idx);
0995bf8c 11655 attrs->user = regime_is_user(env, mmu_idx);
8bf5b6a9 11656
0480f69a
PM
11657 /* Fast Context Switch Extension. This doesn't exist at all in v8.
11658 * In v7 and earlier it affects all stage 1 translations.
11659 */
97fa9350 11660 if (address < 0x02000000 && mmu_idx != ARMMMUIdx_Stage2
0480f69a
PM
11661 && !arm_feature(env, ARM_FEATURE_V8)) {
11662 if (regime_el(env, mmu_idx) == 3) {
11663 address += env->cp15.fcseidr_s;
11664 } else {
11665 address += env->cp15.fcseidr_ns;
11666 }
54bf36ed 11667 }
9ee6e8bb 11668
3279adb9 11669 if (arm_feature(env, ARM_FEATURE_PMSA)) {
c9f9f124 11670 bool ret;
f6bda88f 11671 *page_size = TARGET_PAGE_SIZE;
3279adb9 11672
504e3cc3
PM
11673 if (arm_feature(env, ARM_FEATURE_V8)) {
11674 /* PMSAv8 */
11675 ret = get_phys_addr_pmsav8(env, address, access_type, mmu_idx,
72042435 11676 phys_ptr, attrs, prot, page_size, fi);
504e3cc3 11677 } else if (arm_feature(env, ARM_FEATURE_V7)) {
3279adb9
PM
11678 /* PMSAv7 */
11679 ret = get_phys_addr_pmsav7(env, address, access_type, mmu_idx,
e5e40999 11680 phys_ptr, prot, page_size, fi);
3279adb9
PM
11681 } else {
11682 /* Pre-v7 MPU */
11683 ret = get_phys_addr_pmsav5(env, address, access_type, mmu_idx,
53a4e5c5 11684 phys_ptr, prot, fi);
3279adb9
PM
11685 }
11686 qemu_log_mask(CPU_LOG_MMU, "PMSA MPU lookup for %s at 0x%08" PRIx32
c9f9f124 11687 " mmu_idx %u -> %s (prot %c%c%c)\n",
709e4407
PM
11688 access_type == MMU_DATA_LOAD ? "reading" :
11689 (access_type == MMU_DATA_STORE ? "writing" : "execute"),
c9f9f124
MD
11690 (uint32_t)address, mmu_idx,
11691 ret ? "Miss" : "Hit",
11692 *prot & PAGE_READ ? 'r' : '-',
11693 *prot & PAGE_WRITE ? 'w' : '-',
11694 *prot & PAGE_EXEC ? 'x' : '-');
11695
11696 return ret;
f6bda88f
PC
11697 }
11698
3279adb9
PM
11699 /* Definitely a real MMU, not an MPU */
11700
0480f69a 11701 if (regime_translation_disabled(env, mmu_idx)) {
3279adb9 11702 /* MMU disabled. */
9ee6e8bb 11703 *phys_ptr = address;
3ad493fc 11704 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
d4c430a8 11705 *page_size = TARGET_PAGE_SIZE;
9ee6e8bb 11706 return 0;
0480f69a
PM
11707 }
11708
0480f69a 11709 if (regime_using_lpae_format(env, mmu_idx)) {
bc52bfeb
PM
11710 return get_phys_addr_lpae(env, address, access_type, mmu_idx,
11711 phys_ptr, attrs, prot, page_size,
11712 fi, cacheattrs);
0480f69a 11713 } else if (regime_sctlr(env, mmu_idx) & SCTLR_XP) {
bc52bfeb
PM
11714 return get_phys_addr_v6(env, address, access_type, mmu_idx,
11715 phys_ptr, attrs, prot, page_size, fi);
9ee6e8bb 11716 } else {
bc52bfeb 11717 return get_phys_addr_v5(env, address, access_type, mmu_idx,
f989983e 11718 phys_ptr, prot, page_size, fi);
9ee6e8bb
PB
11719 }
11720}
11721
0faea0c7
PM
11722hwaddr arm_cpu_get_phys_page_attrs_debug(CPUState *cs, vaddr addr,
11723 MemTxAttrs *attrs)
b5ff1b31 11724{
00b941e5 11725 ARMCPU *cpu = ARM_CPU(cs);
d3649702 11726 CPUARMState *env = &cpu->env;
a8170e5e 11727 hwaddr phys_addr;
d4c430a8 11728 target_ulong page_size;
b5ff1b31 11729 int prot;
b7cc4e82 11730 bool ret;
e14b5a23 11731 ARMMMUFaultInfo fi = {};
50494a27 11732 ARMMMUIdx mmu_idx = arm_mmu_idx(env);
b5ff1b31 11733
0faea0c7
PM
11734 *attrs = (MemTxAttrs) {};
11735
8bd5c820 11736 ret = get_phys_addr(env, addr, 0, mmu_idx, &phys_addr,
bc52bfeb 11737 attrs, &prot, &page_size, &fi, NULL);
b5ff1b31 11738
b7cc4e82 11739 if (ret) {
b5ff1b31 11740 return -1;
00b941e5 11741 }
b5ff1b31
FB
11742 return phys_addr;
11743}
11744
b5ff1b31 11745#endif
6ddbc6e4
PB
11746
11747/* Note that signed overflow is undefined in C. The following routines are
11748 careful to use unsigned types where modulo arithmetic is required.
11749 Failure to do so _will_ break on newer gcc. */
11750
11751/* Signed saturating arithmetic. */
11752
1654b2d6 11753/* Perform 16-bit signed saturating addition. */
6ddbc6e4
PB
11754static inline uint16_t add16_sat(uint16_t a, uint16_t b)
11755{
11756 uint16_t res;
11757
11758 res = a + b;
11759 if (((res ^ a) & 0x8000) && !((a ^ b) & 0x8000)) {
11760 if (a & 0x8000)
11761 res = 0x8000;
11762 else
11763 res = 0x7fff;
11764 }
11765 return res;
11766}
11767
1654b2d6 11768/* Perform 8-bit signed saturating addition. */
6ddbc6e4
PB
11769static inline uint8_t add8_sat(uint8_t a, uint8_t b)
11770{
11771 uint8_t res;
11772
11773 res = a + b;
11774 if (((res ^ a) & 0x80) && !((a ^ b) & 0x80)) {
11775 if (a & 0x80)
11776 res = 0x80;
11777 else
11778 res = 0x7f;
11779 }
11780 return res;
11781}
11782
1654b2d6 11783/* Perform 16-bit signed saturating subtraction. */
6ddbc6e4
PB
11784static inline uint16_t sub16_sat(uint16_t a, uint16_t b)
11785{
11786 uint16_t res;
11787
11788 res = a - b;
11789 if (((res ^ a) & 0x8000) && ((a ^ b) & 0x8000)) {
11790 if (a & 0x8000)
11791 res = 0x8000;
11792 else
11793 res = 0x7fff;
11794 }
11795 return res;
11796}
11797
1654b2d6 11798/* Perform 8-bit signed saturating subtraction. */
6ddbc6e4
PB
11799static inline uint8_t sub8_sat(uint8_t a, uint8_t b)
11800{
11801 uint8_t res;
11802
11803 res = a - b;
11804 if (((res ^ a) & 0x80) && ((a ^ b) & 0x80)) {
11805 if (a & 0x80)
11806 res = 0x80;
11807 else
11808 res = 0x7f;
11809 }
11810 return res;
11811}
11812
11813#define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16);
11814#define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16);
11815#define ADD8(a, b, n) RESULT(add8_sat(a, b), n, 8);
11816#define SUB8(a, b, n) RESULT(sub8_sat(a, b), n, 8);
11817#define PFX q
11818
11819#include "op_addsub.h"
11820
11821/* Unsigned saturating arithmetic. */
460a09c1 11822static inline uint16_t add16_usat(uint16_t a, uint16_t b)
6ddbc6e4
PB
11823{
11824 uint16_t res;
11825 res = a + b;
11826 if (res < a)
11827 res = 0xffff;
11828 return res;
11829}
11830
460a09c1 11831static inline uint16_t sub16_usat(uint16_t a, uint16_t b)
6ddbc6e4 11832{
4c4fd3f8 11833 if (a > b)
6ddbc6e4
PB
11834 return a - b;
11835 else
11836 return 0;
11837}
11838
11839static inline uint8_t add8_usat(uint8_t a, uint8_t b)
11840{
11841 uint8_t res;
11842 res = a + b;
11843 if (res < a)
11844 res = 0xff;
11845 return res;
11846}
11847
11848static inline uint8_t sub8_usat(uint8_t a, uint8_t b)
11849{
4c4fd3f8 11850 if (a > b)
6ddbc6e4
PB
11851 return a - b;
11852 else
11853 return 0;
11854}
11855
11856#define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16);
11857#define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16);
11858#define ADD8(a, b, n) RESULT(add8_usat(a, b), n, 8);
11859#define SUB8(a, b, n) RESULT(sub8_usat(a, b), n, 8);
11860#define PFX uq
11861
11862#include "op_addsub.h"
11863
11864/* Signed modulo arithmetic. */
11865#define SARITH16(a, b, n, op) do { \
11866 int32_t sum; \
db6e2e65 11867 sum = (int32_t)(int16_t)(a) op (int32_t)(int16_t)(b); \
6ddbc6e4
PB
11868 RESULT(sum, n, 16); \
11869 if (sum >= 0) \
11870 ge |= 3 << (n * 2); \
11871 } while(0)
11872
11873#define SARITH8(a, b, n, op) do { \
11874 int32_t sum; \
db6e2e65 11875 sum = (int32_t)(int8_t)(a) op (int32_t)(int8_t)(b); \
6ddbc6e4
PB
11876 RESULT(sum, n, 8); \
11877 if (sum >= 0) \
11878 ge |= 1 << n; \
11879 } while(0)
11880
11881
11882#define ADD16(a, b, n) SARITH16(a, b, n, +)
11883#define SUB16(a, b, n) SARITH16(a, b, n, -)
11884#define ADD8(a, b, n) SARITH8(a, b, n, +)
11885#define SUB8(a, b, n) SARITH8(a, b, n, -)
11886#define PFX s
11887#define ARITH_GE
11888
11889#include "op_addsub.h"
11890
11891/* Unsigned modulo arithmetic. */
11892#define ADD16(a, b, n) do { \
11893 uint32_t sum; \
11894 sum = (uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b); \
11895 RESULT(sum, n, 16); \
a87aa10b 11896 if ((sum >> 16) == 1) \
6ddbc6e4
PB
11897 ge |= 3 << (n * 2); \
11898 } while(0)
11899
11900#define ADD8(a, b, n) do { \
11901 uint32_t sum; \
11902 sum = (uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b); \
11903 RESULT(sum, n, 8); \
a87aa10b
AZ
11904 if ((sum >> 8) == 1) \
11905 ge |= 1 << n; \
6ddbc6e4
PB
11906 } while(0)
11907
11908#define SUB16(a, b, n) do { \
11909 uint32_t sum; \
11910 sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \
11911 RESULT(sum, n, 16); \
11912 if ((sum >> 16) == 0) \
11913 ge |= 3 << (n * 2); \
11914 } while(0)
11915
11916#define SUB8(a, b, n) do { \
11917 uint32_t sum; \
11918 sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \
11919 RESULT(sum, n, 8); \
11920 if ((sum >> 8) == 0) \
a87aa10b 11921 ge |= 1 << n; \
6ddbc6e4
PB
11922 } while(0)
11923
11924#define PFX u
11925#define ARITH_GE
11926
11927#include "op_addsub.h"
11928
11929/* Halved signed arithmetic. */
11930#define ADD16(a, b, n) \
11931 RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16)
11932#define SUB16(a, b, n) \
11933 RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16)
11934#define ADD8(a, b, n) \
11935 RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8)
11936#define SUB8(a, b, n) \
11937 RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8)
11938#define PFX sh
11939
11940#include "op_addsub.h"
11941
11942/* Halved unsigned arithmetic. */
11943#define ADD16(a, b, n) \
11944 RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16)
11945#define SUB16(a, b, n) \
11946 RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16)
11947#define ADD8(a, b, n) \
11948 RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8)
11949#define SUB8(a, b, n) \
11950 RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8)
11951#define PFX uh
11952
11953#include "op_addsub.h"
11954
11955static inline uint8_t do_usad(uint8_t a, uint8_t b)
11956{
11957 if (a > b)
11958 return a - b;
11959 else
11960 return b - a;
11961}
11962
11963/* Unsigned sum of absolute byte differences. */
11964uint32_t HELPER(usad8)(uint32_t a, uint32_t b)
11965{
11966 uint32_t sum;
11967 sum = do_usad(a, b);
11968 sum += do_usad(a >> 8, b >> 8);
11969 sum += do_usad(a >> 16, b >>16);
11970 sum += do_usad(a >> 24, b >> 24);
11971 return sum;
11972}
11973
11974/* For ARMv6 SEL instruction. */
11975uint32_t HELPER(sel_flags)(uint32_t flags, uint32_t a, uint32_t b)
11976{
11977 uint32_t mask;
11978
11979 mask = 0;
11980 if (flags & 1)
11981 mask |= 0xff;
11982 if (flags & 2)
11983 mask |= 0xff00;
11984 if (flags & 4)
11985 mask |= 0xff0000;
11986 if (flags & 8)
11987 mask |= 0xff000000;
11988 return (a & mask) | (b & ~mask);
11989}
11990
aa633469
PM
11991/* CRC helpers.
11992 * The upper bytes of val (above the number specified by 'bytes') must have
11993 * been zeroed out by the caller.
11994 */
eb0ecd5a
WN
11995uint32_t HELPER(crc32)(uint32_t acc, uint32_t val, uint32_t bytes)
11996{
11997 uint8_t buf[4];
11998
aa633469 11999 stl_le_p(buf, val);
eb0ecd5a
WN
12000
12001 /* zlib crc32 converts the accumulator and output to one's complement. */
12002 return crc32(acc ^ 0xffffffff, buf, bytes) ^ 0xffffffff;
12003}
12004
12005uint32_t HELPER(crc32c)(uint32_t acc, uint32_t val, uint32_t bytes)
12006{
12007 uint8_t buf[4];
12008
aa633469 12009 stl_le_p(buf, val);
eb0ecd5a
WN
12010
12011 /* Linux crc32c converts the output to one's complement. */
12012 return crc32c(acc, buf, bytes) ^ 0xffffffff;
12013}
a9e01311
RH
12014
12015/* Return the exception level to which FP-disabled exceptions should
12016 * be taken, or 0 if FP is enabled.
12017 */
ced31551 12018int fp_exception_el(CPUARMState *env, int cur_el)
a9e01311 12019{
55faa212 12020#ifndef CONFIG_USER_ONLY
a9e01311
RH
12021 /* CPACR and the CPTR registers don't exist before v6, so FP is
12022 * always accessible
12023 */
12024 if (!arm_feature(env, ARM_FEATURE_V6)) {
12025 return 0;
12026 }
12027
d87513c0
PM
12028 if (arm_feature(env, ARM_FEATURE_M)) {
12029 /* CPACR can cause a NOCP UsageFault taken to current security state */
12030 if (!v7m_cpacr_pass(env, env->v7m.secure, cur_el != 0)) {
12031 return 1;
12032 }
12033
12034 if (arm_feature(env, ARM_FEATURE_M_SECURITY) && !env->v7m.secure) {
12035 if (!extract32(env->v7m.nsacr, 10, 1)) {
12036 /* FP insns cause a NOCP UsageFault taken to Secure */
12037 return 3;
12038 }
12039 }
12040
12041 return 0;
12042 }
12043
a9e01311
RH
12044 /* The CPACR controls traps to EL1, or PL1 if we're 32 bit:
12045 * 0, 2 : trap EL0 and EL1/PL1 accesses
12046 * 1 : trap only EL0 accesses
12047 * 3 : trap no accesses
c2ddb7cf 12048 * This register is ignored if E2H+TGE are both set.
a9e01311 12049 */
c2ddb7cf
RH
12050 if ((arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
12051 int fpen = extract32(env->cp15.cpacr_el1, 20, 2);
12052
12053 switch (fpen) {
12054 case 0:
12055 case 2:
12056 if (cur_el == 0 || cur_el == 1) {
12057 /* Trap to PL1, which might be EL1 or EL3 */
12058 if (arm_is_secure(env) && !arm_el_is_aa64(env, 3)) {
12059 return 3;
12060 }
12061 return 1;
12062 }
12063 if (cur_el == 3 && !is_a64(env)) {
12064 /* Secure PL1 running at EL3 */
a9e01311
RH
12065 return 3;
12066 }
c2ddb7cf
RH
12067 break;
12068 case 1:
12069 if (cur_el == 0) {
12070 return 1;
12071 }
12072 break;
12073 case 3:
12074 break;
a9e01311 12075 }
a9e01311
RH
12076 }
12077
fc1120a7
PM
12078 /*
12079 * The NSACR allows A-profile AArch32 EL3 and M-profile secure mode
12080 * to control non-secure access to the FPU. It doesn't have any
12081 * effect if EL3 is AArch64 or if EL3 doesn't exist at all.
12082 */
12083 if ((arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
12084 cur_el <= 2 && !arm_is_secure_below_el3(env))) {
12085 if (!extract32(env->cp15.nsacr, 10, 1)) {
12086 /* FP insns act as UNDEF */
12087 return cur_el == 2 ? 2 : 1;
12088 }
12089 }
12090
a9e01311
RH
12091 /* For the CPTR registers we don't need to guard with an ARM_FEATURE
12092 * check because zero bits in the registers mean "don't trap".
12093 */
12094
12095 /* CPTR_EL2 : present in v7VE or v8 */
12096 if (cur_el <= 2 && extract32(env->cp15.cptr_el[2], 10, 1)
12097 && !arm_is_secure_below_el3(env)) {
12098 /* Trap FP ops at EL2, NS-EL1 or NS-EL0 to EL2 */
12099 return 2;
12100 }
12101
12102 /* CPTR_EL3 : present in v8 */
12103 if (extract32(env->cp15.cptr_el[3], 10, 1)) {
12104 /* Trap all FP ops to EL3 */
12105 return 3;
12106 }
55faa212 12107#endif
a9e01311
RH
12108 return 0;
12109}
12110
b9f6033c
RH
12111/* Return the exception level we're running at if this is our mmu_idx */
12112int arm_mmu_idx_to_el(ARMMMUIdx mmu_idx)
12113{
12114 if (mmu_idx & ARM_MMU_IDX_M) {
12115 return mmu_idx & ARM_MMU_IDX_M_PRIV;
12116 }
12117
12118 switch (mmu_idx) {
12119 case ARMMMUIdx_E10_0:
12120 case ARMMMUIdx_E20_0:
12121 case ARMMMUIdx_SE10_0:
12122 return 0;
12123 case ARMMMUIdx_E10_1:
452ef8cb 12124 case ARMMMUIdx_E10_1_PAN:
b9f6033c 12125 case ARMMMUIdx_SE10_1:
452ef8cb 12126 case ARMMMUIdx_SE10_1_PAN:
b9f6033c
RH
12127 return 1;
12128 case ARMMMUIdx_E2:
12129 case ARMMMUIdx_E20_2:
452ef8cb 12130 case ARMMMUIdx_E20_2_PAN:
b9f6033c
RH
12131 return 2;
12132 case ARMMMUIdx_SE3:
12133 return 3;
12134 default:
12135 g_assert_not_reached();
12136 }
12137}
12138
7aab5a8c 12139#ifndef CONFIG_TCG
65e4655c
RH
12140ARMMMUIdx arm_v7m_mmu_idx_for_secstate(CPUARMState *env, bool secstate)
12141{
7aab5a8c 12142 g_assert_not_reached();
65e4655c 12143}
7aab5a8c 12144#endif
65e4655c 12145
164690b2 12146ARMMMUIdx arm_mmu_idx_el(CPUARMState *env, int el)
65e4655c 12147{
65e4655c 12148 if (arm_feature(env, ARM_FEATURE_M)) {
50494a27 12149 return arm_v7m_mmu_idx_for_secstate(env, env->v7m.secure);
65e4655c
RH
12150 }
12151
6003d980 12152 /* See ARM pseudo-function ELIsInHost. */
b9f6033c
RH
12153 switch (el) {
12154 case 0:
b9f6033c
RH
12155 if (arm_is_secure_below_el3(env)) {
12156 return ARMMMUIdx_SE10_0;
12157 }
6003d980
RH
12158 if ((env->cp15.hcr_el2 & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)
12159 && arm_el_is_aa64(env, 2)) {
12160 return ARMMMUIdx_E20_0;
12161 }
b9f6033c
RH
12162 return ARMMMUIdx_E10_0;
12163 case 1:
12164 if (arm_is_secure_below_el3(env)) {
66412260
RH
12165 if (env->pstate & PSTATE_PAN) {
12166 return ARMMMUIdx_SE10_1_PAN;
12167 }
b9f6033c
RH
12168 return ARMMMUIdx_SE10_1;
12169 }
66412260
RH
12170 if (env->pstate & PSTATE_PAN) {
12171 return ARMMMUIdx_E10_1_PAN;
12172 }
b9f6033c
RH
12173 return ARMMMUIdx_E10_1;
12174 case 2:
b9f6033c 12175 /* TODO: ARMv8.4-SecEL2 */
6003d980
RH
12176 /* Note that TGE does not apply at EL2. */
12177 if ((env->cp15.hcr_el2 & HCR_E2H) && arm_el_is_aa64(env, 2)) {
66412260
RH
12178 if (env->pstate & PSTATE_PAN) {
12179 return ARMMMUIdx_E20_2_PAN;
12180 }
6003d980
RH
12181 return ARMMMUIdx_E20_2;
12182 }
b9f6033c
RH
12183 return ARMMMUIdx_E2;
12184 case 3:
12185 return ARMMMUIdx_SE3;
12186 default:
12187 g_assert_not_reached();
65e4655c 12188 }
50494a27
RH
12189}
12190
164690b2
RH
12191ARMMMUIdx arm_mmu_idx(CPUARMState *env)
12192{
12193 return arm_mmu_idx_el(env, arm_current_el(env));
12194}
12195
50494a27
RH
12196int cpu_mmu_index(CPUARMState *env, bool ifetch)
12197{
12198 return arm_to_core_mmu_idx(arm_mmu_idx(env));
65e4655c
RH
12199}
12200
64be86ab
RH
12201#ifndef CONFIG_USER_ONLY
12202ARMMMUIdx arm_stage1_mmu_idx(CPUARMState *env)
12203{
12204 return stage_1_mmu_idx(arm_mmu_idx(env));
12205}
12206#endif
12207
fdd1b228
RH
12208static uint32_t rebuild_hflags_common(CPUARMState *env, int fp_el,
12209 ARMMMUIdx mmu_idx, uint32_t flags)
12210{
12211 flags = FIELD_DP32(flags, TBFLAG_ANY, FPEXC_EL, fp_el);
12212 flags = FIELD_DP32(flags, TBFLAG_ANY, MMUIDX,
12213 arm_to_core_mmu_idx(mmu_idx));
12214
fdd1b228
RH
12215 if (arm_singlestep_active(env)) {
12216 flags = FIELD_DP32(flags, TBFLAG_ANY, SS_ACTIVE, 1);
12217 }
12218 return flags;
12219}
12220
43eccfb6
RH
12221static uint32_t rebuild_hflags_common_32(CPUARMState *env, int fp_el,
12222 ARMMMUIdx mmu_idx, uint32_t flags)
12223{
8061a649
RH
12224 bool sctlr_b = arm_sctlr_b(env);
12225
12226 if (sctlr_b) {
12227 flags = FIELD_DP32(flags, TBFLAG_A32, SCTLR_B, 1);
12228 }
12229 if (arm_cpu_data_is_big_endian_a32(env, sctlr_b)) {
12230 flags = FIELD_DP32(flags, TBFLAG_ANY, BE_DATA, 1);
12231 }
43eccfb6
RH
12232 flags = FIELD_DP32(flags, TBFLAG_A32, NS, !access_secure_reg(env));
12233
12234 return rebuild_hflags_common(env, fp_el, mmu_idx, flags);
12235}
12236
6e33ced5
RH
12237static uint32_t rebuild_hflags_m32(CPUARMState *env, int fp_el,
12238 ARMMMUIdx mmu_idx)
12239{
12240 uint32_t flags = 0;
12241
12242 if (arm_v7m_is_handler_mode(env)) {
79cabf1f 12243 flags = FIELD_DP32(flags, TBFLAG_M32, HANDLER, 1);
6e33ced5
RH
12244 }
12245
12246 /*
12247 * v8M always applies stack limit checks unless CCR.STKOFHFNMIGN
12248 * is suppressing them because the requested execution priority
12249 * is less than 0.
12250 */
12251 if (arm_feature(env, ARM_FEATURE_V8) &&
12252 !((mmu_idx & ARM_MMU_IDX_M_NEGPRI) &&
12253 (env->v7m.ccr[env->v7m.secure] & R_V7M_CCR_STKOFHFNMIGN_MASK))) {
79cabf1f 12254 flags = FIELD_DP32(flags, TBFLAG_M32, STACKCHECK, 1);
6e33ced5
RH
12255 }
12256
12257 return rebuild_hflags_common_32(env, fp_el, mmu_idx, flags);
12258}
12259
83f4baef
RH
12260static uint32_t rebuild_hflags_aprofile(CPUARMState *env)
12261{
12262 int flags = 0;
12263
12264 flags = FIELD_DP32(flags, TBFLAG_ANY, DEBUG_TARGET_EL,
12265 arm_debug_target_el(env));
12266 return flags;
12267}
12268
c747224c
RH
12269static uint32_t rebuild_hflags_a32(CPUARMState *env, int fp_el,
12270 ARMMMUIdx mmu_idx)
12271{
83f4baef 12272 uint32_t flags = rebuild_hflags_aprofile(env);
0a54d68e
RH
12273
12274 if (arm_el_is_aa64(env, 1)) {
12275 flags = FIELD_DP32(flags, TBFLAG_A32, VFPEN, 1);
12276 }
5bb0a20b
MZ
12277
12278 if (arm_current_el(env) < 2 && env->cp15.hstr_el2 &&
12279 (arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
12280 flags = FIELD_DP32(flags, TBFLAG_A32, HSTR_ACTIVE, 1);
12281 }
12282
83f4baef 12283 return rebuild_hflags_common_32(env, fp_el, mmu_idx, flags);
c747224c
RH
12284}
12285
d4d7503a
RH
12286static uint32_t rebuild_hflags_a64(CPUARMState *env, int el, int fp_el,
12287 ARMMMUIdx mmu_idx)
a9e01311 12288{
83f4baef 12289 uint32_t flags = rebuild_hflags_aprofile(env);
d4d7503a 12290 ARMMMUIdx stage1 = stage_1_mmu_idx(mmu_idx);
b830a5ee 12291 uint64_t tcr = regime_tcr(env, mmu_idx)->raw_tcr;
d4d7503a
RH
12292 uint64_t sctlr;
12293 int tbii, tbid;
b9adaa70 12294
d4d7503a 12295 flags = FIELD_DP32(flags, TBFLAG_ANY, AARCH64_STATE, 1);
cd208a1c 12296
339370b9 12297 /* Get control bits for tagged addresses. */
b830a5ee
RH
12298 tbid = aa64_va_parameter_tbi(tcr, mmu_idx);
12299 tbii = tbid & ~aa64_va_parameter_tbid(tcr, mmu_idx);
5d8634f5 12300
d4d7503a
RH
12301 flags = FIELD_DP32(flags, TBFLAG_A64, TBII, tbii);
12302 flags = FIELD_DP32(flags, TBFLAG_A64, TBID, tbid);
12303
12304 if (cpu_isar_feature(aa64_sve, env_archcpu(env))) {
12305 int sve_el = sve_exception_el(env, el);
12306 uint32_t zcr_len;
5d8634f5 12307
d4d7503a
RH
12308 /*
12309 * If SVE is disabled, but FP is enabled,
12310 * then the effective len is 0.
12311 */
12312 if (sve_el != 0 && fp_el == 0) {
12313 zcr_len = 0;
12314 } else {
12315 zcr_len = sve_zcr_len_for_el(env, el);
5d8634f5 12316 }
d4d7503a
RH
12317 flags = FIELD_DP32(flags, TBFLAG_A64, SVEEXC_EL, sve_el);
12318 flags = FIELD_DP32(flags, TBFLAG_A64, ZCR_LEN, zcr_len);
12319 }
1db5e96c 12320
aaec1432 12321 sctlr = regime_sctlr(env, stage1);
1db5e96c 12322
8061a649
RH
12323 if (arm_cpu_data_is_big_endian_a64(el, sctlr)) {
12324 flags = FIELD_DP32(flags, TBFLAG_ANY, BE_DATA, 1);
12325 }
12326
d4d7503a
RH
12327 if (cpu_isar_feature(aa64_pauth, env_archcpu(env))) {
12328 /*
12329 * In order to save space in flags, we record only whether
12330 * pauth is "inactive", meaning all insns are implemented as
12331 * a nop, or "active" when some action must be performed.
12332 * The decision of which action to take is left to a helper.
12333 */
12334 if (sctlr & (SCTLR_EnIA | SCTLR_EnIB | SCTLR_EnDA | SCTLR_EnDB)) {
12335 flags = FIELD_DP32(flags, TBFLAG_A64, PAUTH_ACTIVE, 1);
1db5e96c 12336 }
d4d7503a 12337 }
0816ef1b 12338
d4d7503a
RH
12339 if (cpu_isar_feature(aa64_bti, env_archcpu(env))) {
12340 /* Note that SCTLR_EL[23].BT == SCTLR_BT1. */
12341 if (sctlr & (el == 0 ? SCTLR_BT0 : SCTLR_BT1)) {
12342 flags = FIELD_DP32(flags, TBFLAG_A64, BT, 1);
0816ef1b 12343 }
d4d7503a 12344 }
08f1434a 12345
cc28fc30 12346 /* Compute the condition for using AccType_UNPRIV for LDTR et al. */
7a8014ab
RH
12347 if (!(env->pstate & PSTATE_UAO)) {
12348 switch (mmu_idx) {
12349 case ARMMMUIdx_E10_1:
12350 case ARMMMUIdx_E10_1_PAN:
12351 case ARMMMUIdx_SE10_1:
12352 case ARMMMUIdx_SE10_1_PAN:
12353 /* TODO: ARMv8.3-NV */
cc28fc30 12354 flags = FIELD_DP32(flags, TBFLAG_A64, UNPRIV, 1);
7a8014ab
RH
12355 break;
12356 case ARMMMUIdx_E20_2:
12357 case ARMMMUIdx_E20_2_PAN:
12358 /* TODO: ARMv8.4-SecEL2 */
12359 /*
12360 * Note that EL20_2 is gated by HCR_EL2.E2H == 1, but EL20_0 is
12361 * gated by HCR_EL2.<E2H,TGE> == '11', and so is LDTR.
12362 */
12363 if (env->cp15.hcr_el2 & HCR_TGE) {
12364 flags = FIELD_DP32(flags, TBFLAG_A64, UNPRIV, 1);
12365 }
12366 break;
12367 default:
12368 break;
cc28fc30 12369 }
cc28fc30
RH
12370 }
12371
d4d7503a
RH
12372 return rebuild_hflags_common(env, fp_el, mmu_idx, flags);
12373}
12374
3d74e2e9
RH
12375static uint32_t rebuild_hflags_internal(CPUARMState *env)
12376{
12377 int el = arm_current_el(env);
12378 int fp_el = fp_exception_el(env, el);
164690b2 12379 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
3d74e2e9
RH
12380
12381 if (is_a64(env)) {
12382 return rebuild_hflags_a64(env, el, fp_el, mmu_idx);
12383 } else if (arm_feature(env, ARM_FEATURE_M)) {
12384 return rebuild_hflags_m32(env, fp_el, mmu_idx);
12385 } else {
12386 return rebuild_hflags_a32(env, fp_el, mmu_idx);
12387 }
12388}
12389
12390void arm_rebuild_hflags(CPUARMState *env)
12391{
12392 env->hflags = rebuild_hflags_internal(env);
12393}
12394
14f3c588
RH
12395void HELPER(rebuild_hflags_m32)(CPUARMState *env, int el)
12396{
12397 int fp_el = fp_exception_el(env, el);
12398 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
12399
12400 env->hflags = rebuild_hflags_m32(env, fp_el, mmu_idx);
12401}
12402
f80741d1
AB
12403/*
12404 * If we have triggered a EL state change we can't rely on the
12405 * translator having passed it too us, we need to recompute.
12406 */
12407void HELPER(rebuild_hflags_a32_newel)(CPUARMState *env)
12408{
12409 int el = arm_current_el(env);
12410 int fp_el = fp_exception_el(env, el);
12411 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
12412 env->hflags = rebuild_hflags_a32(env, fp_el, mmu_idx);
12413}
12414
14f3c588
RH
12415void HELPER(rebuild_hflags_a32)(CPUARMState *env, int el)
12416{
12417 int fp_el = fp_exception_el(env, el);
12418 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
12419
12420 env->hflags = rebuild_hflags_a32(env, fp_el, mmu_idx);
12421}
12422
12423void HELPER(rebuild_hflags_a64)(CPUARMState *env, int el)
12424{
12425 int fp_el = fp_exception_el(env, el);
12426 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
12427
12428 env->hflags = rebuild_hflags_a64(env, el, fp_el, mmu_idx);
12429}
12430
0ee8b24a
PMD
12431static inline void assert_hflags_rebuild_correctly(CPUARMState *env)
12432{
12433#ifdef CONFIG_DEBUG_TCG
12434 uint32_t env_flags_current = env->hflags;
12435 uint32_t env_flags_rebuilt = rebuild_hflags_internal(env);
12436
12437 if (unlikely(env_flags_current != env_flags_rebuilt)) {
12438 fprintf(stderr, "TCG hflags mismatch (current:0x%08x rebuilt:0x%08x)\n",
12439 env_flags_current, env_flags_rebuilt);
12440 abort();
12441 }
12442#endif
12443}
12444
d4d7503a
RH
12445void cpu_get_tb_cpu_state(CPUARMState *env, target_ulong *pc,
12446 target_ulong *cs_base, uint32_t *pflags)
12447{
e979972a
RH
12448 uint32_t flags = env->hflags;
12449 uint32_t pstate_for_ss;
d4d7503a 12450
9b253fe5 12451 *cs_base = 0;
0ee8b24a 12452 assert_hflags_rebuild_correctly(env);
3d74e2e9 12453
e979972a 12454 if (FIELD_EX32(flags, TBFLAG_ANY, AARCH64_STATE)) {
d4d7503a 12455 *pc = env->pc;
d4d7503a 12456 if (cpu_isar_feature(aa64_bti, env_archcpu(env))) {
08f1434a
RH
12457 flags = FIELD_DP32(flags, TBFLAG_A64, BTYPE, env->btype);
12458 }
60e12c37 12459 pstate_for_ss = env->pstate;
a9e01311
RH
12460 } else {
12461 *pc = env->regs[15];
6e33ced5
RH
12462
12463 if (arm_feature(env, ARM_FEATURE_M)) {
9550d1bd
RH
12464 if (arm_feature(env, ARM_FEATURE_M_SECURITY) &&
12465 FIELD_EX32(env->v7m.fpccr[M_REG_S], V7M_FPCCR, S)
12466 != env->v7m.secure) {
79cabf1f 12467 flags = FIELD_DP32(flags, TBFLAG_M32, FPCCR_S_WRONG, 1);
9550d1bd
RH
12468 }
12469
12470 if ((env->v7m.fpccr[env->v7m.secure] & R_V7M_FPCCR_ASPEN_MASK) &&
12471 (!(env->v7m.control[M_REG_S] & R_V7M_CONTROL_FPCA_MASK) ||
12472 (env->v7m.secure &&
12473 !(env->v7m.control[M_REG_S] & R_V7M_CONTROL_SFPA_MASK)))) {
12474 /*
12475 * ASPEN is set, but FPCA/SFPA indicate that there is no
12476 * active FP context; we must create a new FP context before
12477 * executing any FP insn.
12478 */
79cabf1f 12479 flags = FIELD_DP32(flags, TBFLAG_M32, NEW_FP_CTXT_NEEDED, 1);
9550d1bd
RH
12480 }
12481
12482 bool is_secure = env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_S_MASK;
12483 if (env->v7m.fpccr[is_secure] & R_V7M_FPCCR_LSPACT_MASK) {
79cabf1f 12484 flags = FIELD_DP32(flags, TBFLAG_M32, LSPACT, 1);
9550d1bd 12485 }
6e33ced5 12486 } else {
bbad7c62
RH
12487 /*
12488 * Note that XSCALE_CPAR shares bits with VECSTRIDE.
12489 * Note that VECLEN+VECSTRIDE are RES0 for M-profile.
12490 */
12491 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
12492 flags = FIELD_DP32(flags, TBFLAG_A32,
12493 XSCALE_CPAR, env->cp15.c15_cpar);
12494 } else {
12495 flags = FIELD_DP32(flags, TBFLAG_A32, VECLEN,
12496 env->vfp.vec_len);
12497 flags = FIELD_DP32(flags, TBFLAG_A32, VECSTRIDE,
12498 env->vfp.vec_stride);
12499 }
0a54d68e
RH
12500 if (env->vfp.xregs[ARM_VFP_FPEXC] & (1 << 30)) {
12501 flags = FIELD_DP32(flags, TBFLAG_A32, VFPEN, 1);
12502 }
6e33ced5
RH
12503 }
12504
79cabf1f
RH
12505 flags = FIELD_DP32(flags, TBFLAG_AM32, THUMB, env->thumb);
12506 flags = FIELD_DP32(flags, TBFLAG_AM32, CONDEXEC, env->condexec_bits);
60e12c37 12507 pstate_for_ss = env->uncached_cpsr;
d4d7503a 12508 }
a9e01311 12509
60e12c37
RH
12510 /*
12511 * The SS_ACTIVE and PSTATE_SS bits correspond to the state machine
a9e01311
RH
12512 * states defined in the ARM ARM for software singlestep:
12513 * SS_ACTIVE PSTATE.SS State
12514 * 0 x Inactive (the TB flag for SS is always 0)
12515 * 1 0 Active-pending
12516 * 1 1 Active-not-pending
fdd1b228 12517 * SS_ACTIVE is set in hflags; PSTATE_SS is computed every TB.
a9e01311 12518 */
60e12c37
RH
12519 if (FIELD_EX32(flags, TBFLAG_ANY, SS_ACTIVE) &&
12520 (pstate_for_ss & PSTATE_SS)) {
12521 flags = FIELD_DP32(flags, TBFLAG_ANY, PSTATE_SS, 1);
a9e01311 12522 }
a9e01311 12523
b9adaa70 12524 *pflags = flags;
a9e01311 12525}
0ab5953b
RH
12526
12527#ifdef TARGET_AARCH64
12528/*
12529 * The manual says that when SVE is enabled and VQ is widened the
12530 * implementation is allowed to zero the previously inaccessible
12531 * portion of the registers. The corollary to that is that when
12532 * SVE is enabled and VQ is narrowed we are also allowed to zero
12533 * the now inaccessible portion of the registers.
12534 *
12535 * The intent of this is that no predicate bit beyond VQ is ever set.
12536 * Which means that some operations on predicate registers themselves
12537 * may operate on full uint64_t or even unrolled across the maximum
12538 * uint64_t[4]. Performing 4 bits of host arithmetic unconditionally
12539 * may well be cheaper than conditionals to restrict the operation
12540 * to the relevant portion of a uint16_t[16].
12541 */
12542void aarch64_sve_narrow_vq(CPUARMState *env, unsigned vq)
12543{
12544 int i, j;
12545 uint64_t pmask;
12546
12547 assert(vq >= 1 && vq <= ARM_MAX_VQ);
2fc0cc0e 12548 assert(vq <= env_archcpu(env)->sve_max_vq);
0ab5953b
RH
12549
12550 /* Zap the high bits of the zregs. */
12551 for (i = 0; i < 32; i++) {
12552 memset(&env->vfp.zregs[i].d[2 * vq], 0, 16 * (ARM_MAX_VQ - vq));
12553 }
12554
12555 /* Zap the high bits of the pregs and ffr. */
12556 pmask = 0;
12557 if (vq & 3) {
12558 pmask = ~(-1ULL << (16 * (vq & 3)));
12559 }
12560 for (j = vq / 4; j < ARM_MAX_VQ / 4; j++) {
12561 for (i = 0; i < 17; ++i) {
12562 env->vfp.pregs[i].p[j] &= pmask;
12563 }
12564 pmask = 0;
12565 }
12566}
12567
12568/*
12569 * Notice a change in SVE vector size when changing EL.
12570 */
9a05f7b6
RH
12571void aarch64_sve_change_el(CPUARMState *env, int old_el,
12572 int new_el, bool el0_a64)
0ab5953b 12573{
2fc0cc0e 12574 ARMCPU *cpu = env_archcpu(env);
0ab5953b 12575 int old_len, new_len;
9a05f7b6 12576 bool old_a64, new_a64;
0ab5953b
RH
12577
12578 /* Nothing to do if no SVE. */
cd208a1c 12579 if (!cpu_isar_feature(aa64_sve, cpu)) {
0ab5953b
RH
12580 return;
12581 }
12582
12583 /* Nothing to do if FP is disabled in either EL. */
12584 if (fp_exception_el(env, old_el) || fp_exception_el(env, new_el)) {
12585 return;
12586 }
12587
12588 /*
12589 * DDI0584A.d sec 3.2: "If SVE instructions are disabled or trapped
12590 * at ELx, or not available because the EL is in AArch32 state, then
12591 * for all purposes other than a direct read, the ZCR_ELx.LEN field
12592 * has an effective value of 0".
12593 *
12594 * Consider EL2 (aa64, vq=4) -> EL0 (aa32) -> EL1 (aa64, vq=0).
12595 * If we ignore aa32 state, we would fail to see the vq4->vq0 transition
12596 * from EL2->EL1. Thus we go ahead and narrow when entering aa32 so that
12597 * we already have the correct register contents when encountering the
12598 * vq0->vq0 transition between EL0->EL1.
12599 */
9a05f7b6
RH
12600 old_a64 = old_el ? arm_el_is_aa64(env, old_el) : el0_a64;
12601 old_len = (old_a64 && !sve_exception_el(env, old_el)
0ab5953b 12602 ? sve_zcr_len_for_el(env, old_el) : 0);
9a05f7b6
RH
12603 new_a64 = new_el ? arm_el_is_aa64(env, new_el) : el0_a64;
12604 new_len = (new_a64 && !sve_exception_el(env, new_el)
0ab5953b
RH
12605 ? sve_zcr_len_for_el(env, new_el) : 0);
12606
12607 /* When changing vector length, clear inaccessible state. */
12608 if (new_len < old_len) {
12609 aarch64_sve_narrow_vq(env, new_len + 1);
12610 }
12611}
12612#endif