]> git.proxmox.com Git - mirror_qemu.git/blame - target/arm/helper.c
hw/arm: versal: Generate xlnx-versal-virt zdma FDT nodes
[mirror_qemu.git] / target / arm / helper.c
CommitLineData
ed3baad1
PMD
1/*
2 * ARM generic helpers.
3 *
4 * This code is licensed under the GNU GPL v2 or later.
5 *
6 * SPDX-License-Identifier: GPL-2.0-or-later
7 */
db725815 8
74c21bd0 9#include "qemu/osdep.h"
63159601 10#include "qemu/units.h"
181962fd 11#include "target/arm/idau.h"
194cbc49 12#include "trace.h"
b5ff1b31 13#include "cpu.h"
ccd38087 14#include "internals.h"
022c62cb 15#include "exec/gdbstub.h"
2ef6175a 16#include "exec/helper-proto.h"
1de7afc9 17#include "qemu/host-utils.h"
db725815 18#include "qemu/main-loop.h"
1de7afc9 19#include "qemu/bitops.h"
eb0ecd5a 20#include "qemu/crc32c.h"
0442428a 21#include "qemu/qemu-print.h"
63c91552 22#include "exec/exec-all.h"
eb0ecd5a 23#include <zlib.h> /* For crc32 */
64552b6b 24#include "hw/irq.h"
f1672e6f 25#include "hw/semihosting/semihost.h"
b2e23725 26#include "sysemu/cpus.h"
f3a9b694 27#include "sysemu/kvm.h"
2a609df8 28#include "sysemu/tcg.h"
9d2b5a58 29#include "qemu/range.h"
7f7b4e7a 30#include "qapi/qapi-commands-machine-target.h"
de390645
RH
31#include "qapi/error.h"
32#include "qemu/guest-random.h"
91f78c58
PMD
33#ifdef CONFIG_TCG
34#include "arm_ldst.h"
7aab5a8c 35#include "exec/cpu_ldst.h"
91f78c58 36#endif
0b03bdfc 37
352c98e5
LV
38#define ARM_CPU_FREQ 1000000000 /* FIXME: 1 GHz, should be configurable */
39
4a501606 40#ifndef CONFIG_USER_ONLY
7c2cb42b 41
37785977 42static bool get_phys_addr_lpae(CPUARMState *env, target_ulong address,
03ae85f8 43 MMUAccessType access_type, ARMMMUIdx mmu_idx,
37785977 44 hwaddr *phys_ptr, MemTxAttrs *txattrs, int *prot,
da909b2c 45 target_ulong *page_size_ptr,
5b2d261d 46 ARMMMUFaultInfo *fi, ARMCacheAttrs *cacheattrs);
4a501606
PM
47#endif
48
affdb64d
PM
49static void switch_mode(CPUARMState *env, int mode);
50
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
c4241c7d 533static void dacr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
c983fe6c 534{
2fc0cc0e 535 ARMCPU *cpu = env_archcpu(env);
00c8cb0a 536
8d5c773e 537 raw_write(env, ri, value);
d10eb08f 538 tlb_flush(CPU(cpu)); /* Flush TLB as domain not tracked in TLB */
c983fe6c
PM
539}
540
c4241c7d 541static void fcse_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
08de207b 542{
2fc0cc0e 543 ARMCPU *cpu = env_archcpu(env);
00c8cb0a 544
8d5c773e 545 if (raw_read(env, ri) != value) {
08de207b
PM
546 /* Unlike real hardware the qemu TLB uses virtual addresses,
547 * not modified virtual addresses, so this causes a TLB flush.
548 */
d10eb08f 549 tlb_flush(CPU(cpu));
8d5c773e 550 raw_write(env, ri, value);
08de207b 551 }
08de207b 552}
c4241c7d
PM
553
554static void contextidr_write(CPUARMState *env, const ARMCPRegInfo *ri,
555 uint64_t value)
08de207b 556{
2fc0cc0e 557 ARMCPU *cpu = env_archcpu(env);
00c8cb0a 558
452a0955 559 if (raw_read(env, ri) != value && !arm_feature(env, ARM_FEATURE_PMSA)
014406b5 560 && !extended_addresses_enabled(env)) {
08de207b
PM
561 /* For VMSA (when not using the LPAE long descriptor page table
562 * format) this register includes the ASID, so do a TLB flush.
563 * For PMSA it is purely a process ID and no action is needed.
564 */
d10eb08f 565 tlb_flush(CPU(cpu));
08de207b 566 }
8d5c773e 567 raw_write(env, ri, value);
08de207b
PM
568}
569
b4ab8ce9
PM
570/* IS variants of TLB operations must affect all cores */
571static void tlbiall_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
572 uint64_t value)
573{
29a0af61 574 CPUState *cs = env_cpu(env);
b4ab8ce9
PM
575
576 tlb_flush_all_cpus_synced(cs);
577}
578
579static void tlbiasid_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
580 uint64_t value)
581{
29a0af61 582 CPUState *cs = env_cpu(env);
b4ab8ce9
PM
583
584 tlb_flush_all_cpus_synced(cs);
585}
586
587static void tlbimva_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
588 uint64_t value)
589{
29a0af61 590 CPUState *cs = env_cpu(env);
b4ab8ce9
PM
591
592 tlb_flush_page_all_cpus_synced(cs, value & TARGET_PAGE_MASK);
593}
594
595static void tlbimvaa_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
596 uint64_t value)
597{
29a0af61 598 CPUState *cs = env_cpu(env);
b4ab8ce9
PM
599
600 tlb_flush_page_all_cpus_synced(cs, value & TARGET_PAGE_MASK);
601}
602
603/*
604 * Non-IS variants of TLB operations are upgraded to
605 * IS versions if we are at NS EL1 and HCR_EL2.FB is set to
606 * force broadcast of these operations.
607 */
608static bool tlb_force_broadcast(CPUARMState *env)
609{
610 return (env->cp15.hcr_el2 & HCR_FB) &&
611 arm_current_el(env) == 1 && arm_is_secure_below_el3(env);
612}
613
c4241c7d
PM
614static void tlbiall_write(CPUARMState *env, const ARMCPRegInfo *ri,
615 uint64_t value)
d929823f
PM
616{
617 /* Invalidate all (TLBIALL) */
527db2be 618 CPUState *cs = env_cpu(env);
00c8cb0a 619
b4ab8ce9 620 if (tlb_force_broadcast(env)) {
527db2be
RH
621 tlb_flush_all_cpus_synced(cs);
622 } else {
623 tlb_flush(cs);
b4ab8ce9 624 }
d929823f
PM
625}
626
c4241c7d
PM
627static void tlbimva_write(CPUARMState *env, const ARMCPRegInfo *ri,
628 uint64_t value)
d929823f
PM
629{
630 /* Invalidate single TLB entry by MVA and ASID (TLBIMVA) */
527db2be 631 CPUState *cs = env_cpu(env);
31b030d4 632
527db2be 633 value &= TARGET_PAGE_MASK;
b4ab8ce9 634 if (tlb_force_broadcast(env)) {
527db2be
RH
635 tlb_flush_page_all_cpus_synced(cs, value);
636 } else {
637 tlb_flush_page(cs, value);
b4ab8ce9 638 }
d929823f
PM
639}
640
c4241c7d
PM
641static void tlbiasid_write(CPUARMState *env, const ARMCPRegInfo *ri,
642 uint64_t value)
d929823f
PM
643{
644 /* Invalidate by ASID (TLBIASID) */
527db2be 645 CPUState *cs = env_cpu(env);
00c8cb0a 646
b4ab8ce9 647 if (tlb_force_broadcast(env)) {
527db2be
RH
648 tlb_flush_all_cpus_synced(cs);
649 } else {
650 tlb_flush(cs);
b4ab8ce9 651 }
d929823f
PM
652}
653
c4241c7d
PM
654static void tlbimvaa_write(CPUARMState *env, const ARMCPRegInfo *ri,
655 uint64_t value)
d929823f
PM
656{
657 /* Invalidate single entry by MVA, all ASIDs (TLBIMVAA) */
527db2be 658 CPUState *cs = env_cpu(env);
31b030d4 659
527db2be 660 value &= TARGET_PAGE_MASK;
b4ab8ce9 661 if (tlb_force_broadcast(env)) {
527db2be
RH
662 tlb_flush_page_all_cpus_synced(cs, value);
663 } else {
664 tlb_flush_page(cs, value);
b4ab8ce9 665 }
fa439fc5
PM
666}
667
541ef8c2
SS
668static void tlbiall_nsnh_write(CPUARMState *env, const ARMCPRegInfo *ri,
669 uint64_t value)
670{
29a0af61 671 CPUState *cs = env_cpu(env);
541ef8c2 672
0336cbf8 673 tlb_flush_by_mmuidx(cs,
01b98b68 674 ARMMMUIdxBit_E10_1 |
452ef8cb 675 ARMMMUIdxBit_E10_1_PAN |
01b98b68 676 ARMMMUIdxBit_E10_0 |
97fa9350 677 ARMMMUIdxBit_Stage2);
541ef8c2
SS
678}
679
680static void tlbiall_nsnh_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
681 uint64_t value)
682{
29a0af61 683 CPUState *cs = env_cpu(env);
541ef8c2 684
a67cf277 685 tlb_flush_by_mmuidx_all_cpus_synced(cs,
01b98b68 686 ARMMMUIdxBit_E10_1 |
452ef8cb 687 ARMMMUIdxBit_E10_1_PAN |
01b98b68 688 ARMMMUIdxBit_E10_0 |
97fa9350 689 ARMMMUIdxBit_Stage2);
541ef8c2
SS
690}
691
692static void tlbiipas2_write(CPUARMState *env, const ARMCPRegInfo *ri,
693 uint64_t value)
694{
695 /* Invalidate by IPA. This has to invalidate any structures that
696 * contain only stage 2 translation information, but does not need
697 * to apply to structures that contain combined stage 1 and stage 2
698 * translation information.
699 * This must NOP if EL2 isn't implemented or SCR_EL3.NS is zero.
700 */
29a0af61 701 CPUState *cs = env_cpu(env);
541ef8c2
SS
702 uint64_t pageaddr;
703
704 if (!arm_feature(env, ARM_FEATURE_EL2) || !(env->cp15.scr_el3 & SCR_NS)) {
705 return;
706 }
707
708 pageaddr = sextract64(value << 12, 0, 40);
709
97fa9350 710 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_Stage2);
541ef8c2
SS
711}
712
713static void tlbiipas2_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
714 uint64_t value)
715{
29a0af61 716 CPUState *cs = env_cpu(env);
541ef8c2
SS
717 uint64_t pageaddr;
718
719 if (!arm_feature(env, ARM_FEATURE_EL2) || !(env->cp15.scr_el3 & SCR_NS)) {
720 return;
721 }
722
723 pageaddr = sextract64(value << 12, 0, 40);
724
a67cf277 725 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
97fa9350 726 ARMMMUIdxBit_Stage2);
541ef8c2
SS
727}
728
729static void tlbiall_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
730 uint64_t value)
731{
29a0af61 732 CPUState *cs = env_cpu(env);
541ef8c2 733
e013b741 734 tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_E2);
541ef8c2
SS
735}
736
737static void tlbiall_hyp_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
738 uint64_t value)
739{
29a0af61 740 CPUState *cs = env_cpu(env);
541ef8c2 741
e013b741 742 tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_E2);
541ef8c2
SS
743}
744
745static void tlbimva_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
746 uint64_t value)
747{
29a0af61 748 CPUState *cs = env_cpu(env);
541ef8c2
SS
749 uint64_t pageaddr = value & ~MAKE_64BIT_MASK(0, 12);
750
e013b741 751 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_E2);
541ef8c2
SS
752}
753
754static void tlbimva_hyp_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
755 uint64_t value)
756{
29a0af61 757 CPUState *cs = env_cpu(env);
541ef8c2
SS
758 uint64_t pageaddr = value & ~MAKE_64BIT_MASK(0, 12);
759
a67cf277 760 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
e013b741 761 ARMMMUIdxBit_E2);
541ef8c2
SS
762}
763
e9aa6c21 764static const ARMCPRegInfo cp_reginfo[] = {
54bf36ed
FA
765 /* Define the secure and non-secure FCSE identifier CP registers
766 * separately because there is no secure bank in V8 (no _EL3). This allows
767 * the secure register to be properly reset and migrated. There is also no
768 * v8 EL1 version of the register so the non-secure instance stands alone.
769 */
9c513e78 770 { .name = "FCSEIDR",
54bf36ed
FA
771 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
772 .access = PL1_RW, .secure = ARM_CP_SECSTATE_NS,
773 .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_ns),
774 .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
9c513e78 775 { .name = "FCSEIDR_S",
54bf36ed
FA
776 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
777 .access = PL1_RW, .secure = ARM_CP_SECSTATE_S,
778 .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_s),
d4e6df63 779 .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
54bf36ed
FA
780 /* Define the secure and non-secure context identifier CP registers
781 * separately because there is no secure bank in V8 (no _EL3). This allows
782 * the secure register to be properly reset and migrated. In the
783 * non-secure case, the 32-bit register will have reset and migration
784 * disabled during registration as it is handled by the 64-bit instance.
785 */
786 { .name = "CONTEXTIDR_EL1", .state = ARM_CP_STATE_BOTH,
014406b5 787 .opc0 = 3, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
54bf36ed
FA
788 .access = PL1_RW, .secure = ARM_CP_SECSTATE_NS,
789 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[1]),
790 .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
9c513e78 791 { .name = "CONTEXTIDR_S", .state = ARM_CP_STATE_AA32,
54bf36ed
FA
792 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
793 .access = PL1_RW, .secure = ARM_CP_SECSTATE_S,
794 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_s),
d4e6df63 795 .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
9449fdf6
PM
796 REGINFO_SENTINEL
797};
798
799static const ARMCPRegInfo not_v8_cp_reginfo[] = {
800 /* NB: Some of these registers exist in v8 but with more precise
801 * definitions that don't use CP_ANY wildcards (mostly in v8_cp_reginfo[]).
802 */
803 /* MMU Domain access control / MPU write buffer control */
0c17d68c
FA
804 { .name = "DACR",
805 .cp = 15, .opc1 = CP_ANY, .crn = 3, .crm = CP_ANY, .opc2 = CP_ANY,
806 .access = PL1_RW, .resetvalue = 0,
807 .writefn = dacr_write, .raw_writefn = raw_write,
808 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
809 offsetoflow32(CPUARMState, cp15.dacr_ns) } },
a903c449
EI
810 /* ARMv7 allocates a range of implementation defined TLB LOCKDOWN regs.
811 * For v6 and v5, these mappings are overly broad.
4fdd17dd 812 */
a903c449
EI
813 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 0,
814 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
815 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 1,
816 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
817 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 4,
818 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
819 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 8,
4fdd17dd 820 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
c4804214
PM
821 /* Cache maintenance ops; some of this space may be overridden later. */
822 { .name = "CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
823 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
824 .type = ARM_CP_NOP | ARM_CP_OVERRIDE },
e9aa6c21
PM
825 REGINFO_SENTINEL
826};
827
7d57f408
PM
828static const ARMCPRegInfo not_v6_cp_reginfo[] = {
829 /* Not all pre-v6 cores implemented this WFI, so this is slightly
830 * over-broad.
831 */
832 { .name = "WFI_v5", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = 2,
833 .access = PL1_W, .type = ARM_CP_WFI },
834 REGINFO_SENTINEL
835};
836
837static const ARMCPRegInfo not_v7_cp_reginfo[] = {
838 /* Standard v6 WFI (also used in some pre-v6 cores); not in v7 (which
839 * is UNPREDICTABLE; we choose to NOP as most implementations do).
840 */
841 { .name = "WFI_v6", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
842 .access = PL1_W, .type = ARM_CP_WFI },
34f90529
PM
843 /* L1 cache lockdown. Not architectural in v6 and earlier but in practice
844 * implemented in 926, 946, 1026, 1136, 1176 and 11MPCore. StrongARM and
845 * OMAPCP will override this space.
846 */
847 { .name = "DLOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 0,
848 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_data),
849 .resetvalue = 0 },
850 { .name = "ILOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 1,
851 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_insn),
852 .resetvalue = 0 },
776d4e5c
PM
853 /* v6 doesn't have the cache ID registers but Linux reads them anyway */
854 { .name = "DUMMY", .cp = 15, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = CP_ANY,
7a0e58fa 855 .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 856 .resetvalue = 0 },
50300698
PM
857 /* We don't implement pre-v7 debug but most CPUs had at least a DBGDIDR;
858 * implementing it as RAZ means the "debug architecture version" bits
859 * will read as a reserved value, which should cause Linux to not try
860 * to use the debug hardware.
861 */
862 { .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0,
863 .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
995939a6
PM
864 /* MMU TLB control. Note that the wildcarding means we cover not just
865 * the unified TLB ops but also the dside/iside/inner-shareable variants.
866 */
867 { .name = "TLBIALL", .cp = 15, .crn = 8, .crm = CP_ANY,
868 .opc1 = CP_ANY, .opc2 = 0, .access = PL1_W, .writefn = tlbiall_write,
7a0e58fa 869 .type = ARM_CP_NO_RAW },
995939a6
PM
870 { .name = "TLBIMVA", .cp = 15, .crn = 8, .crm = CP_ANY,
871 .opc1 = CP_ANY, .opc2 = 1, .access = PL1_W, .writefn = tlbimva_write,
7a0e58fa 872 .type = ARM_CP_NO_RAW },
995939a6
PM
873 { .name = "TLBIASID", .cp = 15, .crn = 8, .crm = CP_ANY,
874 .opc1 = CP_ANY, .opc2 = 2, .access = PL1_W, .writefn = tlbiasid_write,
7a0e58fa 875 .type = ARM_CP_NO_RAW },
995939a6
PM
876 { .name = "TLBIMVAA", .cp = 15, .crn = 8, .crm = CP_ANY,
877 .opc1 = CP_ANY, .opc2 = 3, .access = PL1_W, .writefn = tlbimvaa_write,
7a0e58fa 878 .type = ARM_CP_NO_RAW },
a903c449
EI
879 { .name = "PRRR", .cp = 15, .crn = 10, .crm = 2,
880 .opc1 = 0, .opc2 = 0, .access = PL1_RW, .type = ARM_CP_NOP },
881 { .name = "NMRR", .cp = 15, .crn = 10, .crm = 2,
882 .opc1 = 0, .opc2 = 1, .access = PL1_RW, .type = ARM_CP_NOP },
7d57f408
PM
883 REGINFO_SENTINEL
884};
885
c4241c7d
PM
886static void cpacr_write(CPUARMState *env, const ARMCPRegInfo *ri,
887 uint64_t value)
2771db27 888{
f0aff255
FA
889 uint32_t mask = 0;
890
891 /* In ARMv8 most bits of CPACR_EL1 are RES0. */
892 if (!arm_feature(env, ARM_FEATURE_V8)) {
893 /* ARMv7 defines bits for unimplemented coprocessors as RAZ/WI.
894 * ASEDIS [31] and D32DIS [30] are both UNK/SBZP without VFP.
895 * TRCDIS [28] is RAZ/WI since we do not implement a trace macrocell.
896 */
7fbc6a40 897 if (cpu_isar_feature(aa32_vfp_simd, env_archcpu(env))) {
f0aff255
FA
898 /* VFP coprocessor: cp10 & cp11 [23:20] */
899 mask |= (1 << 31) | (1 << 30) | (0xf << 20);
900
901 if (!arm_feature(env, ARM_FEATURE_NEON)) {
902 /* ASEDIS [31] bit is RAO/WI */
903 value |= (1 << 31);
904 }
905
906 /* VFPv3 and upwards with NEON implement 32 double precision
907 * registers (D0-D31).
908 */
a6627f5f 909 if (!cpu_isar_feature(aa32_simd_r32, env_archcpu(env))) {
f0aff255
FA
910 /* D32DIS [30] is RAO/WI if D16-31 are not implemented. */
911 value |= (1 << 30);
912 }
913 }
914 value &= mask;
2771db27 915 }
fc1120a7
PM
916
917 /*
918 * For A-profile AArch32 EL3 (but not M-profile secure mode), if NSACR.CP10
919 * is 0 then CPACR.{CP11,CP10} ignore writes and read as 0b00.
920 */
921 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
922 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
923 value &= ~(0xf << 20);
924 value |= env->cp15.cpacr_el1 & (0xf << 20);
925 }
926
7ebd5f2e 927 env->cp15.cpacr_el1 = value;
2771db27
PM
928}
929
fc1120a7
PM
930static uint64_t cpacr_read(CPUARMState *env, const ARMCPRegInfo *ri)
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 uint64_t value = env->cp15.cpacr_el1;
937
938 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
939 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
940 value &= ~(0xf << 20);
941 }
942 return value;
943}
944
945
5deac39c
PM
946static void cpacr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
947{
948 /* Call cpacr_write() so that we reset with the correct RAO bits set
949 * for our CPU features.
950 */
951 cpacr_write(env, ri, 0);
952}
953
3f208fd7
PM
954static CPAccessResult cpacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
955 bool isread)
c6f19164
GB
956{
957 if (arm_feature(env, ARM_FEATURE_V8)) {
958 /* Check if CPACR accesses are to be trapped to EL2 */
959 if (arm_current_el(env) == 1 &&
960 (env->cp15.cptr_el[2] & CPTR_TCPAC) && !arm_is_secure(env)) {
961 return CP_ACCESS_TRAP_EL2;
962 /* Check if CPACR accesses are to be trapped to EL3 */
963 } else if (arm_current_el(env) < 3 &&
964 (env->cp15.cptr_el[3] & CPTR_TCPAC)) {
965 return CP_ACCESS_TRAP_EL3;
966 }
967 }
968
969 return CP_ACCESS_OK;
970}
971
3f208fd7
PM
972static CPAccessResult cptr_access(CPUARMState *env, const ARMCPRegInfo *ri,
973 bool isread)
c6f19164
GB
974{
975 /* Check if CPTR accesses are set to trap to EL3 */
976 if (arm_current_el(env) == 2 && (env->cp15.cptr_el[3] & CPTR_TCPAC)) {
977 return CP_ACCESS_TRAP_EL3;
978 }
979
980 return CP_ACCESS_OK;
981}
982
7d57f408
PM
983static const ARMCPRegInfo v6_cp_reginfo[] = {
984 /* prefetch by MVA in v6, NOP in v7 */
985 { .name = "MVA_prefetch",
986 .cp = 15, .crn = 7, .crm = 13, .opc1 = 0, .opc2 = 1,
987 .access = PL1_W, .type = ARM_CP_NOP },
6df99dec
SS
988 /* We need to break the TB after ISB to execute self-modifying code
989 * correctly and also to take any pending interrupts immediately.
990 * So use arm_cp_write_ignore() function instead of ARM_CP_NOP flag.
991 */
7d57f408 992 { .name = "ISB", .cp = 15, .crn = 7, .crm = 5, .opc1 = 0, .opc2 = 4,
6df99dec 993 .access = PL0_W, .type = ARM_CP_NO_RAW, .writefn = arm_cp_write_ignore },
091fd17c 994 { .name = "DSB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 4,
7d57f408 995 .access = PL0_W, .type = ARM_CP_NOP },
091fd17c 996 { .name = "DMB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 5,
7d57f408 997 .access = PL0_W, .type = ARM_CP_NOP },
06d76f31 998 { .name = "IFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 2,
6cd8a264 999 .access = PL1_RW,
b848ce2b
FA
1000 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ifar_s),
1001 offsetof(CPUARMState, cp15.ifar_ns) },
06d76f31
PM
1002 .resetvalue = 0, },
1003 /* Watchpoint Fault Address Register : should actually only be present
1004 * for 1136, 1176, 11MPCore.
1005 */
1006 { .name = "WFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 1,
1007 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0, },
34222fb8 1008 { .name = "CPACR", .state = ARM_CP_STATE_BOTH, .opc0 = 3,
c6f19164 1009 .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 2, .accessfn = cpacr_access,
7ebd5f2e 1010 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.cpacr_el1),
fc1120a7 1011 .resetfn = cpacr_reset, .writefn = cpacr_write, .readfn = cpacr_read },
7d57f408
PM
1012 REGINFO_SENTINEL
1013};
1014
7ece99b1
AL
1015/* Definitions for the PMU registers */
1016#define PMCRN_MASK 0xf800
1017#define PMCRN_SHIFT 11
f4efb4b2 1018#define PMCRLC 0x40
a1ed04dd
PM
1019#define PMCRDP 0x20
1020#define PMCRX 0x10
7ece99b1
AL
1021#define PMCRD 0x8
1022#define PMCRC 0x4
5ecdd3e4 1023#define PMCRP 0x2
7ece99b1 1024#define PMCRE 0x1
62d96ff4
PM
1025/*
1026 * Mask of PMCR bits writeable by guest (not including WO bits like C, P,
1027 * which can be written as 1 to trigger behaviour but which stay RAZ).
1028 */
1029#define PMCR_WRITEABLE_MASK (PMCRLC | PMCRDP | PMCRX | PMCRD | PMCRE)
7ece99b1 1030
033614c4
AL
1031#define PMXEVTYPER_P 0x80000000
1032#define PMXEVTYPER_U 0x40000000
1033#define PMXEVTYPER_NSK 0x20000000
1034#define PMXEVTYPER_NSU 0x10000000
1035#define PMXEVTYPER_NSH 0x08000000
1036#define PMXEVTYPER_M 0x04000000
1037#define PMXEVTYPER_MT 0x02000000
1038#define PMXEVTYPER_EVTCOUNT 0x0000ffff
1039#define PMXEVTYPER_MASK (PMXEVTYPER_P | PMXEVTYPER_U | PMXEVTYPER_NSK | \
1040 PMXEVTYPER_NSU | PMXEVTYPER_NSH | \
1041 PMXEVTYPER_M | PMXEVTYPER_MT | \
1042 PMXEVTYPER_EVTCOUNT)
1043
4b8afa1f
AL
1044#define PMCCFILTR 0xf8000000
1045#define PMCCFILTR_M PMXEVTYPER_M
1046#define PMCCFILTR_EL0 (PMCCFILTR | PMCCFILTR_M)
1047
7ece99b1
AL
1048static inline uint32_t pmu_num_counters(CPUARMState *env)
1049{
1050 return (env->cp15.c9_pmcr & PMCRN_MASK) >> PMCRN_SHIFT;
1051}
1052
1053/* Bits allowed to be set/cleared for PMCNTEN* and PMINTEN* */
1054static inline uint64_t pmu_counter_mask(CPUARMState *env)
1055{
1056 return (1 << 31) | ((1 << pmu_num_counters(env)) - 1);
1057}
1058
57a4a11b
AL
1059typedef struct pm_event {
1060 uint16_t number; /* PMEVTYPER.evtCount is 16 bits wide */
1061 /* If the event is supported on this CPU (used to generate PMCEID[01]) */
1062 bool (*supported)(CPUARMState *);
1063 /*
1064 * Retrieve the current count of the underlying event. The programmed
1065 * counters hold a difference from the return value from this function
1066 */
1067 uint64_t (*get_count)(CPUARMState *);
4e7beb0c
AL
1068 /*
1069 * Return how many nanoseconds it will take (at a minimum) for count events
1070 * to occur. A negative value indicates the counter will never overflow, or
1071 * that the counter has otherwise arranged for the overflow bit to be set
1072 * and the PMU interrupt to be raised on overflow.
1073 */
1074 int64_t (*ns_per_count)(uint64_t);
57a4a11b
AL
1075} pm_event;
1076
b2e23725
AL
1077static bool event_always_supported(CPUARMState *env)
1078{
1079 return true;
1080}
1081
0d4bfd7d
AL
1082static uint64_t swinc_get_count(CPUARMState *env)
1083{
1084 /*
1085 * SW_INCR events are written directly to the pmevcntr's by writes to
1086 * PMSWINC, so there is no underlying count maintained by the PMU itself
1087 */
1088 return 0;
1089}
1090
4e7beb0c
AL
1091static int64_t swinc_ns_per(uint64_t ignored)
1092{
1093 return -1;
1094}
1095
b2e23725
AL
1096/*
1097 * Return the underlying cycle count for the PMU cycle counters. If we're in
1098 * usermode, simply return 0.
1099 */
1100static uint64_t cycles_get_count(CPUARMState *env)
1101{
1102#ifndef CONFIG_USER_ONLY
1103 return muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
1104 ARM_CPU_FREQ, NANOSECONDS_PER_SECOND);
1105#else
1106 return cpu_get_host_ticks();
1107#endif
1108}
1109
1110#ifndef CONFIG_USER_ONLY
4e7beb0c
AL
1111static int64_t cycles_ns_per(uint64_t cycles)
1112{
1113 return (ARM_CPU_FREQ / NANOSECONDS_PER_SECOND) * cycles;
1114}
1115
b2e23725
AL
1116static bool instructions_supported(CPUARMState *env)
1117{
1118 return use_icount == 1 /* Precise instruction counting */;
1119}
1120
1121static uint64_t instructions_get_count(CPUARMState *env)
1122{
1123 return (uint64_t)cpu_get_icount_raw();
1124}
4e7beb0c
AL
1125
1126static int64_t instructions_ns_per(uint64_t icount)
1127{
1128 return cpu_icount_to_ns((int64_t)icount);
1129}
b2e23725
AL
1130#endif
1131
0727f63b
PM
1132static bool pmu_8_1_events_supported(CPUARMState *env)
1133{
1134 /* For events which are supported in any v8.1 PMU */
1135 return cpu_isar_feature(any_pmu_8_1, env_archcpu(env));
1136}
1137
15dd1ebd
PM
1138static bool pmu_8_4_events_supported(CPUARMState *env)
1139{
1140 /* For events which are supported in any v8.1 PMU */
1141 return cpu_isar_feature(any_pmu_8_4, env_archcpu(env));
1142}
1143
0727f63b
PM
1144static uint64_t zero_event_get_count(CPUARMState *env)
1145{
1146 /* For events which on QEMU never fire, so their count is always zero */
1147 return 0;
1148}
1149
1150static int64_t zero_event_ns_per(uint64_t cycles)
1151{
1152 /* An event which never fires can never overflow */
1153 return -1;
1154}
1155
57a4a11b 1156static const pm_event pm_events[] = {
0d4bfd7d
AL
1157 { .number = 0x000, /* SW_INCR */
1158 .supported = event_always_supported,
1159 .get_count = swinc_get_count,
4e7beb0c 1160 .ns_per_count = swinc_ns_per,
0d4bfd7d 1161 },
b2e23725
AL
1162#ifndef CONFIG_USER_ONLY
1163 { .number = 0x008, /* INST_RETIRED, Instruction architecturally executed */
1164 .supported = instructions_supported,
1165 .get_count = instructions_get_count,
4e7beb0c 1166 .ns_per_count = instructions_ns_per,
b2e23725
AL
1167 },
1168 { .number = 0x011, /* CPU_CYCLES, Cycle */
1169 .supported = event_always_supported,
1170 .get_count = cycles_get_count,
4e7beb0c 1171 .ns_per_count = cycles_ns_per,
0727f63b 1172 },
b2e23725 1173#endif
0727f63b
PM
1174 { .number = 0x023, /* STALL_FRONTEND */
1175 .supported = pmu_8_1_events_supported,
1176 .get_count = zero_event_get_count,
1177 .ns_per_count = zero_event_ns_per,
1178 },
1179 { .number = 0x024, /* STALL_BACKEND */
1180 .supported = pmu_8_1_events_supported,
1181 .get_count = zero_event_get_count,
1182 .ns_per_count = zero_event_ns_per,
1183 },
15dd1ebd
PM
1184 { .number = 0x03c, /* STALL */
1185 .supported = pmu_8_4_events_supported,
1186 .get_count = zero_event_get_count,
1187 .ns_per_count = zero_event_ns_per,
1188 },
57a4a11b
AL
1189};
1190
1191/*
1192 * Note: Before increasing MAX_EVENT_ID beyond 0x3f into the 0x40xx range of
1193 * events (i.e. the statistical profiling extension), this implementation
1194 * should first be updated to something sparse instead of the current
1195 * supported_event_map[] array.
1196 */
15dd1ebd 1197#define MAX_EVENT_ID 0x3c
57a4a11b
AL
1198#define UNSUPPORTED_EVENT UINT16_MAX
1199static uint16_t supported_event_map[MAX_EVENT_ID + 1];
1200
1201/*
bf8d0969
AL
1202 * Called upon CPU initialization to initialize PMCEID[01]_EL0 and build a map
1203 * of ARM event numbers to indices in our pm_events array.
57a4a11b
AL
1204 *
1205 * Note: Events in the 0x40XX range are not currently supported.
1206 */
bf8d0969 1207void pmu_init(ARMCPU *cpu)
57a4a11b 1208{
57a4a11b
AL
1209 unsigned int i;
1210
bf8d0969
AL
1211 /*
1212 * Empty supported_event_map and cpu->pmceid[01] before adding supported
1213 * events to them
1214 */
57a4a11b
AL
1215 for (i = 0; i < ARRAY_SIZE(supported_event_map); i++) {
1216 supported_event_map[i] = UNSUPPORTED_EVENT;
1217 }
bf8d0969
AL
1218 cpu->pmceid0 = 0;
1219 cpu->pmceid1 = 0;
57a4a11b
AL
1220
1221 for (i = 0; i < ARRAY_SIZE(pm_events); i++) {
1222 const pm_event *cnt = &pm_events[i];
1223 assert(cnt->number <= MAX_EVENT_ID);
1224 /* We do not currently support events in the 0x40xx range */
1225 assert(cnt->number <= 0x3f);
1226
bf8d0969 1227 if (cnt->supported(&cpu->env)) {
57a4a11b 1228 supported_event_map[cnt->number] = i;
67da43d6 1229 uint64_t event_mask = 1ULL << (cnt->number & 0x1f);
bf8d0969
AL
1230 if (cnt->number & 0x20) {
1231 cpu->pmceid1 |= event_mask;
1232 } else {
1233 cpu->pmceid0 |= event_mask;
1234 }
57a4a11b
AL
1235 }
1236 }
57a4a11b
AL
1237}
1238
5ecdd3e4
AL
1239/*
1240 * Check at runtime whether a PMU event is supported for the current machine
1241 */
1242static bool event_supported(uint16_t number)
1243{
1244 if (number > MAX_EVENT_ID) {
1245 return false;
1246 }
1247 return supported_event_map[number] != UNSUPPORTED_EVENT;
1248}
1249
3f208fd7
PM
1250static CPAccessResult pmreg_access(CPUARMState *env, const ARMCPRegInfo *ri,
1251 bool isread)
200ac0ef 1252{
3b163b01 1253 /* Performance monitor registers user accessibility is controlled
1fce1ba9
PM
1254 * by PMUSERENR. MDCR_EL2.TPM and MDCR_EL3.TPM allow configurable
1255 * trapping to EL2 or EL3 for other accesses.
200ac0ef 1256 */
1fce1ba9
PM
1257 int el = arm_current_el(env);
1258
6ecd0b6b 1259 if (el == 0 && !(env->cp15.c9_pmuserenr & 1)) {
fcd25206 1260 return CP_ACCESS_TRAP;
200ac0ef 1261 }
1fce1ba9
PM
1262 if (el < 2 && (env->cp15.mdcr_el2 & MDCR_TPM)
1263 && !arm_is_secure_below_el3(env)) {
1264 return CP_ACCESS_TRAP_EL2;
1265 }
1266 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) {
1267 return CP_ACCESS_TRAP_EL3;
1268 }
1269
fcd25206 1270 return CP_ACCESS_OK;
200ac0ef
PM
1271}
1272
6ecd0b6b
AB
1273static CPAccessResult pmreg_access_xevcntr(CPUARMState *env,
1274 const ARMCPRegInfo *ri,
1275 bool isread)
1276{
1277 /* ER: event counter read trap control */
1278 if (arm_feature(env, ARM_FEATURE_V8)
1279 && arm_current_el(env) == 0
1280 && (env->cp15.c9_pmuserenr & (1 << 3)) != 0
1281 && isread) {
1282 return CP_ACCESS_OK;
1283 }
1284
1285 return pmreg_access(env, ri, isread);
1286}
1287
1288static CPAccessResult pmreg_access_swinc(CPUARMState *env,
1289 const ARMCPRegInfo *ri,
1290 bool isread)
1291{
1292 /* SW: software increment write trap control */
1293 if (arm_feature(env, ARM_FEATURE_V8)
1294 && arm_current_el(env) == 0
1295 && (env->cp15.c9_pmuserenr & (1 << 1)) != 0
1296 && !isread) {
1297 return CP_ACCESS_OK;
1298 }
1299
1300 return pmreg_access(env, ri, isread);
1301}
1302
6ecd0b6b
AB
1303static CPAccessResult pmreg_access_selr(CPUARMState *env,
1304 const ARMCPRegInfo *ri,
1305 bool isread)
1306{
1307 /* ER: event counter read trap control */
1308 if (arm_feature(env, ARM_FEATURE_V8)
1309 && arm_current_el(env) == 0
1310 && (env->cp15.c9_pmuserenr & (1 << 3)) != 0) {
1311 return CP_ACCESS_OK;
1312 }
1313
1314 return pmreg_access(env, ri, isread);
1315}
1316
1317static CPAccessResult pmreg_access_ccntr(CPUARMState *env,
1318 const ARMCPRegInfo *ri,
1319 bool isread)
1320{
1321 /* CR: cycle counter read trap control */
1322 if (arm_feature(env, ARM_FEATURE_V8)
1323 && arm_current_el(env) == 0
1324 && (env->cp15.c9_pmuserenr & (1 << 2)) != 0
1325 && isread) {
1326 return CP_ACCESS_OK;
1327 }
1328
1329 return pmreg_access(env, ri, isread);
1330}
1331
033614c4
AL
1332/* Returns true if the counter (pass 31 for PMCCNTR) should count events using
1333 * the current EL, security state, and register configuration.
1334 */
1335static bool pmu_counter_enabled(CPUARMState *env, uint8_t counter)
87124fde 1336{
033614c4
AL
1337 uint64_t filter;
1338 bool e, p, u, nsk, nsu, nsh, m;
1339 bool enabled, prohibited, filtered;
1340 bool secure = arm_is_secure(env);
1341 int el = arm_current_el(env);
1342 uint8_t hpmn = env->cp15.mdcr_el2 & MDCR_HPMN;
87124fde 1343
cbbb3041
AJ
1344 if (!arm_feature(env, ARM_FEATURE_PMU)) {
1345 return false;
1346 }
1347
033614c4
AL
1348 if (!arm_feature(env, ARM_FEATURE_EL2) ||
1349 (counter < hpmn || counter == 31)) {
1350 e = env->cp15.c9_pmcr & PMCRE;
1351 } else {
1352 e = env->cp15.mdcr_el2 & MDCR_HPME;
87124fde 1353 }
033614c4 1354 enabled = e && (env->cp15.c9_pmcnten & (1 << counter));
87124fde 1355
033614c4
AL
1356 if (!secure) {
1357 if (el == 2 && (counter < hpmn || counter == 31)) {
1358 prohibited = env->cp15.mdcr_el2 & MDCR_HPMD;
1359 } else {
1360 prohibited = false;
1361 }
1362 } else {
1363 prohibited = arm_feature(env, ARM_FEATURE_EL3) &&
1364 (env->cp15.mdcr_el3 & MDCR_SPME);
1365 }
1366
1367 if (prohibited && counter == 31) {
1368 prohibited = env->cp15.c9_pmcr & PMCRDP;
1369 }
1370
5ecdd3e4
AL
1371 if (counter == 31) {
1372 filter = env->cp15.pmccfiltr_el0;
1373 } else {
1374 filter = env->cp15.c14_pmevtyper[counter];
1375 }
033614c4
AL
1376
1377 p = filter & PMXEVTYPER_P;
1378 u = filter & PMXEVTYPER_U;
1379 nsk = arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_NSK);
1380 nsu = arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_NSU);
1381 nsh = arm_feature(env, ARM_FEATURE_EL2) && (filter & PMXEVTYPER_NSH);
1382 m = arm_el_is_aa64(env, 1) &&
1383 arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_M);
1384
1385 if (el == 0) {
1386 filtered = secure ? u : u != nsu;
1387 } else if (el == 1) {
1388 filtered = secure ? p : p != nsk;
1389 } else if (el == 2) {
1390 filtered = !nsh;
1391 } else { /* EL3 */
1392 filtered = m != p;
1393 }
1394
5ecdd3e4
AL
1395 if (counter != 31) {
1396 /*
1397 * If not checking PMCCNTR, ensure the counter is setup to an event we
1398 * support
1399 */
1400 uint16_t event = filter & PMXEVTYPER_EVTCOUNT;
1401 if (!event_supported(event)) {
1402 return false;
1403 }
1404 }
1405
033614c4 1406 return enabled && !prohibited && !filtered;
87124fde 1407}
033614c4 1408
f4efb4b2
AL
1409static void pmu_update_irq(CPUARMState *env)
1410{
2fc0cc0e 1411 ARMCPU *cpu = env_archcpu(env);
f4efb4b2
AL
1412 qemu_set_irq(cpu->pmu_interrupt, (env->cp15.c9_pmcr & PMCRE) &&
1413 (env->cp15.c9_pminten & env->cp15.c9_pmovsr));
1414}
1415
5d05b9d4
AL
1416/*
1417 * Ensure c15_ccnt is the guest-visible count so that operations such as
1418 * enabling/disabling the counter or filtering, modifying the count itself,
1419 * etc. can be done logically. This is essentially a no-op if the counter is
1420 * not enabled at the time of the call.
1421 */
f2b2f53f 1422static void pmccntr_op_start(CPUARMState *env)
ec7b4ce4 1423{
b2e23725 1424 uint64_t cycles = cycles_get_count(env);
ec7b4ce4 1425
033614c4 1426 if (pmu_counter_enabled(env, 31)) {
5d05b9d4
AL
1427 uint64_t eff_cycles = cycles;
1428 if (env->cp15.c9_pmcr & PMCRD) {
1429 /* Increment once every 64 processor clock cycles */
1430 eff_cycles /= 64;
1431 }
1432
f4efb4b2
AL
1433 uint64_t new_pmccntr = eff_cycles - env->cp15.c15_ccnt_delta;
1434
1435 uint64_t overflow_mask = env->cp15.c9_pmcr & PMCRLC ? \
1436 1ull << 63 : 1ull << 31;
1437 if (env->cp15.c15_ccnt & ~new_pmccntr & overflow_mask) {
1438 env->cp15.c9_pmovsr |= (1 << 31);
1439 pmu_update_irq(env);
1440 }
1441
1442 env->cp15.c15_ccnt = new_pmccntr;
ec7b4ce4 1443 }
5d05b9d4
AL
1444 env->cp15.c15_ccnt_delta = cycles;
1445}
ec7b4ce4 1446
5d05b9d4
AL
1447/*
1448 * If PMCCNTR is enabled, recalculate the delta between the clock and the
1449 * guest-visible count. A call to pmccntr_op_finish should follow every call to
1450 * pmccntr_op_start.
1451 */
f2b2f53f 1452static void pmccntr_op_finish(CPUARMState *env)
5d05b9d4 1453{
033614c4 1454 if (pmu_counter_enabled(env, 31)) {
4e7beb0c
AL
1455#ifndef CONFIG_USER_ONLY
1456 /* Calculate when the counter will next overflow */
1457 uint64_t remaining_cycles = -env->cp15.c15_ccnt;
1458 if (!(env->cp15.c9_pmcr & PMCRLC)) {
1459 remaining_cycles = (uint32_t)remaining_cycles;
1460 }
1461 int64_t overflow_in = cycles_ns_per(remaining_cycles);
1462
1463 if (overflow_in > 0) {
1464 int64_t overflow_at = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
1465 overflow_in;
2fc0cc0e 1466 ARMCPU *cpu = env_archcpu(env);
4e7beb0c
AL
1467 timer_mod_anticipate_ns(cpu->pmu_timer, overflow_at);
1468 }
1469#endif
5d05b9d4 1470
4e7beb0c 1471 uint64_t prev_cycles = env->cp15.c15_ccnt_delta;
5d05b9d4
AL
1472 if (env->cp15.c9_pmcr & PMCRD) {
1473 /* Increment once every 64 processor clock cycles */
1474 prev_cycles /= 64;
1475 }
5d05b9d4 1476 env->cp15.c15_ccnt_delta = prev_cycles - env->cp15.c15_ccnt;
ec7b4ce4
AF
1477 }
1478}
1479
5ecdd3e4
AL
1480static void pmevcntr_op_start(CPUARMState *env, uint8_t counter)
1481{
1482
1483 uint16_t event = env->cp15.c14_pmevtyper[counter] & PMXEVTYPER_EVTCOUNT;
1484 uint64_t count = 0;
1485 if (event_supported(event)) {
1486 uint16_t event_idx = supported_event_map[event];
1487 count = pm_events[event_idx].get_count(env);
1488 }
1489
1490 if (pmu_counter_enabled(env, counter)) {
f4efb4b2
AL
1491 uint32_t new_pmevcntr = count - env->cp15.c14_pmevcntr_delta[counter];
1492
1493 if (env->cp15.c14_pmevcntr[counter] & ~new_pmevcntr & INT32_MIN) {
1494 env->cp15.c9_pmovsr |= (1 << counter);
1495 pmu_update_irq(env);
1496 }
1497 env->cp15.c14_pmevcntr[counter] = new_pmevcntr;
5ecdd3e4
AL
1498 }
1499 env->cp15.c14_pmevcntr_delta[counter] = count;
1500}
1501
1502static void pmevcntr_op_finish(CPUARMState *env, uint8_t counter)
1503{
1504 if (pmu_counter_enabled(env, counter)) {
4e7beb0c
AL
1505#ifndef CONFIG_USER_ONLY
1506 uint16_t event = env->cp15.c14_pmevtyper[counter] & PMXEVTYPER_EVTCOUNT;
1507 uint16_t event_idx = supported_event_map[event];
1508 uint64_t delta = UINT32_MAX -
1509 (uint32_t)env->cp15.c14_pmevcntr[counter] + 1;
1510 int64_t overflow_in = pm_events[event_idx].ns_per_count(delta);
1511
1512 if (overflow_in > 0) {
1513 int64_t overflow_at = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
1514 overflow_in;
2fc0cc0e 1515 ARMCPU *cpu = env_archcpu(env);
4e7beb0c
AL
1516 timer_mod_anticipate_ns(cpu->pmu_timer, overflow_at);
1517 }
1518#endif
1519
5ecdd3e4
AL
1520 env->cp15.c14_pmevcntr_delta[counter] -=
1521 env->cp15.c14_pmevcntr[counter];
1522 }
1523}
1524
5d05b9d4
AL
1525void pmu_op_start(CPUARMState *env)
1526{
5ecdd3e4 1527 unsigned int i;
5d05b9d4 1528 pmccntr_op_start(env);
5ecdd3e4
AL
1529 for (i = 0; i < pmu_num_counters(env); i++) {
1530 pmevcntr_op_start(env, i);
1531 }
5d05b9d4
AL
1532}
1533
1534void pmu_op_finish(CPUARMState *env)
1535{
5ecdd3e4 1536 unsigned int i;
5d05b9d4 1537 pmccntr_op_finish(env);
5ecdd3e4
AL
1538 for (i = 0; i < pmu_num_counters(env); i++) {
1539 pmevcntr_op_finish(env, i);
1540 }
5d05b9d4
AL
1541}
1542
033614c4
AL
1543void pmu_pre_el_change(ARMCPU *cpu, void *ignored)
1544{
1545 pmu_op_start(&cpu->env);
1546}
1547
1548void pmu_post_el_change(ARMCPU *cpu, void *ignored)
1549{
1550 pmu_op_finish(&cpu->env);
1551}
1552
4e7beb0c
AL
1553void arm_pmu_timer_cb(void *opaque)
1554{
1555 ARMCPU *cpu = opaque;
1556
1557 /*
1558 * Update all the counter values based on the current underlying counts,
1559 * triggering interrupts to be raised, if necessary. pmu_op_finish() also
1560 * has the effect of setting the cpu->pmu_timer to the next earliest time a
1561 * counter may expire.
1562 */
1563 pmu_op_start(&cpu->env);
1564 pmu_op_finish(&cpu->env);
1565}
1566
c4241c7d
PM
1567static void pmcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1568 uint64_t value)
200ac0ef 1569{
5d05b9d4 1570 pmu_op_start(env);
7c2cb42b
AF
1571
1572 if (value & PMCRC) {
1573 /* The counter has been reset */
1574 env->cp15.c15_ccnt = 0;
1575 }
1576
5ecdd3e4
AL
1577 if (value & PMCRP) {
1578 unsigned int i;
1579 for (i = 0; i < pmu_num_counters(env); i++) {
1580 env->cp15.c14_pmevcntr[i] = 0;
1581 }
1582 }
1583
62d96ff4
PM
1584 env->cp15.c9_pmcr &= ~PMCR_WRITEABLE_MASK;
1585 env->cp15.c9_pmcr |= (value & PMCR_WRITEABLE_MASK);
7c2cb42b 1586
5d05b9d4 1587 pmu_op_finish(env);
7c2cb42b
AF
1588}
1589
0d4bfd7d
AL
1590static void pmswinc_write(CPUARMState *env, const ARMCPRegInfo *ri,
1591 uint64_t value)
1592{
1593 unsigned int i;
1594 for (i = 0; i < pmu_num_counters(env); i++) {
1595 /* Increment a counter's count iff: */
1596 if ((value & (1 << i)) && /* counter's bit is set */
1597 /* counter is enabled and not filtered */
1598 pmu_counter_enabled(env, i) &&
1599 /* counter is SW_INCR */
1600 (env->cp15.c14_pmevtyper[i] & PMXEVTYPER_EVTCOUNT) == 0x0) {
1601 pmevcntr_op_start(env, i);
f4efb4b2
AL
1602
1603 /*
1604 * Detect if this write causes an overflow since we can't predict
1605 * PMSWINC overflows like we can for other events
1606 */
1607 uint32_t new_pmswinc = env->cp15.c14_pmevcntr[i] + 1;
1608
1609 if (env->cp15.c14_pmevcntr[i] & ~new_pmswinc & INT32_MIN) {
1610 env->cp15.c9_pmovsr |= (1 << i);
1611 pmu_update_irq(env);
1612 }
1613
1614 env->cp15.c14_pmevcntr[i] = new_pmswinc;
1615
0d4bfd7d
AL
1616 pmevcntr_op_finish(env, i);
1617 }
1618 }
1619}
1620
7c2cb42b
AF
1621static uint64_t pmccntr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1622{
5d05b9d4
AL
1623 uint64_t ret;
1624 pmccntr_op_start(env);
1625 ret = env->cp15.c15_ccnt;
1626 pmccntr_op_finish(env);
1627 return ret;
7c2cb42b
AF
1628}
1629
6b040780
WH
1630static void pmselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1631 uint64_t value)
1632{
1633 /* The value of PMSELR.SEL affects the behavior of PMXEVTYPER and
1634 * PMXEVCNTR. We allow [0..31] to be written to PMSELR here; in the
1635 * meanwhile, we check PMSELR.SEL when PMXEVTYPER and PMXEVCNTR are
1636 * accessed.
1637 */
1638 env->cp15.c9_pmselr = value & 0x1f;
1639}
1640
7c2cb42b
AF
1641static void pmccntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1642 uint64_t value)
1643{
5d05b9d4
AL
1644 pmccntr_op_start(env);
1645 env->cp15.c15_ccnt = value;
1646 pmccntr_op_finish(env);
200ac0ef 1647}
421c7ebd
PC
1648
1649static void pmccntr_write32(CPUARMState *env, const ARMCPRegInfo *ri,
1650 uint64_t value)
1651{
1652 uint64_t cur_val = pmccntr_read(env, NULL);
1653
1654 pmccntr_write(env, ri, deposit64(cur_val, 0, 32, value));
1655}
1656
0614601c
AF
1657static void pmccfiltr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1658 uint64_t value)
1659{
5d05b9d4 1660 pmccntr_op_start(env);
4b8afa1f
AL
1661 env->cp15.pmccfiltr_el0 = value & PMCCFILTR_EL0;
1662 pmccntr_op_finish(env);
1663}
1664
1665static void pmccfiltr_write_a32(CPUARMState *env, const ARMCPRegInfo *ri,
1666 uint64_t value)
1667{
1668 pmccntr_op_start(env);
1669 /* M is not accessible from AArch32 */
1670 env->cp15.pmccfiltr_el0 = (env->cp15.pmccfiltr_el0 & PMCCFILTR_M) |
1671 (value & PMCCFILTR);
5d05b9d4 1672 pmccntr_op_finish(env);
0614601c
AF
1673}
1674
4b8afa1f
AL
1675static uint64_t pmccfiltr_read_a32(CPUARMState *env, const ARMCPRegInfo *ri)
1676{
1677 /* M is not visible in AArch32 */
1678 return env->cp15.pmccfiltr_el0 & PMCCFILTR;
1679}
1680
c4241c7d 1681static void pmcntenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
200ac0ef
PM
1682 uint64_t value)
1683{
7ece99b1 1684 value &= pmu_counter_mask(env);
200ac0ef 1685 env->cp15.c9_pmcnten |= value;
200ac0ef
PM
1686}
1687
c4241c7d
PM
1688static void pmcntenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1689 uint64_t value)
200ac0ef 1690{
7ece99b1 1691 value &= pmu_counter_mask(env);
200ac0ef 1692 env->cp15.c9_pmcnten &= ~value;
200ac0ef
PM
1693}
1694
c4241c7d
PM
1695static void pmovsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1696 uint64_t value)
200ac0ef 1697{
599b71e2 1698 value &= pmu_counter_mask(env);
200ac0ef 1699 env->cp15.c9_pmovsr &= ~value;
f4efb4b2 1700 pmu_update_irq(env);
200ac0ef
PM
1701}
1702
327dd510
AL
1703static void pmovsset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1704 uint64_t value)
1705{
1706 value &= pmu_counter_mask(env);
1707 env->cp15.c9_pmovsr |= value;
f4efb4b2 1708 pmu_update_irq(env);
327dd510
AL
1709}
1710
5ecdd3e4
AL
1711static void pmevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
1712 uint64_t value, const uint8_t counter)
200ac0ef 1713{
5ecdd3e4
AL
1714 if (counter == 31) {
1715 pmccfiltr_write(env, ri, value);
1716 } else if (counter < pmu_num_counters(env)) {
1717 pmevcntr_op_start(env, counter);
1718
1719 /*
1720 * If this counter's event type is changing, store the current
1721 * underlying count for the new type in c14_pmevcntr_delta[counter] so
1722 * pmevcntr_op_finish has the correct baseline when it converts back to
1723 * a delta.
1724 */
1725 uint16_t old_event = env->cp15.c14_pmevtyper[counter] &
1726 PMXEVTYPER_EVTCOUNT;
1727 uint16_t new_event = value & PMXEVTYPER_EVTCOUNT;
1728 if (old_event != new_event) {
1729 uint64_t count = 0;
1730 if (event_supported(new_event)) {
1731 uint16_t event_idx = supported_event_map[new_event];
1732 count = pm_events[event_idx].get_count(env);
1733 }
1734 env->cp15.c14_pmevcntr_delta[counter] = count;
1735 }
1736
1737 env->cp15.c14_pmevtyper[counter] = value & PMXEVTYPER_MASK;
1738 pmevcntr_op_finish(env, counter);
1739 }
fdb86656
WH
1740 /* Attempts to access PMXEVTYPER are CONSTRAINED UNPREDICTABLE when
1741 * PMSELR value is equal to or greater than the number of implemented
1742 * counters, but not equal to 0x1f. We opt to behave as a RAZ/WI.
1743 */
5ecdd3e4
AL
1744}
1745
1746static uint64_t pmevtyper_read(CPUARMState *env, const ARMCPRegInfo *ri,
1747 const uint8_t counter)
1748{
1749 if (counter == 31) {
1750 return env->cp15.pmccfiltr_el0;
1751 } else if (counter < pmu_num_counters(env)) {
1752 return env->cp15.c14_pmevtyper[counter];
1753 } else {
1754 /*
1755 * We opt to behave as a RAZ/WI when attempts to access PMXEVTYPER
1756 * are CONSTRAINED UNPREDICTABLE. See comments in pmevtyper_write().
1757 */
1758 return 0;
1759 }
1760}
1761
1762static void pmevtyper_writefn(CPUARMState *env, const ARMCPRegInfo *ri,
1763 uint64_t value)
1764{
1765 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1766 pmevtyper_write(env, ri, value, counter);
1767}
1768
1769static void pmevtyper_rawwrite(CPUARMState *env, const ARMCPRegInfo *ri,
1770 uint64_t value)
1771{
1772 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1773 env->cp15.c14_pmevtyper[counter] = value;
1774
1775 /*
1776 * pmevtyper_rawwrite is called between a pair of pmu_op_start and
1777 * pmu_op_finish calls when loading saved state for a migration. Because
1778 * we're potentially updating the type of event here, the value written to
1779 * c14_pmevcntr_delta by the preceeding pmu_op_start call may be for a
1780 * different counter type. Therefore, we need to set this value to the
1781 * current count for the counter type we're writing so that pmu_op_finish
1782 * has the correct count for its calculation.
1783 */
1784 uint16_t event = value & PMXEVTYPER_EVTCOUNT;
1785 if (event_supported(event)) {
1786 uint16_t event_idx = supported_event_map[event];
1787 env->cp15.c14_pmevcntr_delta[counter] =
1788 pm_events[event_idx].get_count(env);
fdb86656
WH
1789 }
1790}
1791
5ecdd3e4
AL
1792static uint64_t pmevtyper_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
1793{
1794 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1795 return pmevtyper_read(env, ri, counter);
1796}
1797
1798static void pmxevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
1799 uint64_t value)
1800{
1801 pmevtyper_write(env, ri, value, env->cp15.c9_pmselr & 31);
1802}
1803
fdb86656
WH
1804static uint64_t pmxevtyper_read(CPUARMState *env, const ARMCPRegInfo *ri)
1805{
5ecdd3e4
AL
1806 return pmevtyper_read(env, ri, env->cp15.c9_pmselr & 31);
1807}
1808
1809static void pmevcntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1810 uint64_t value, uint8_t counter)
1811{
1812 if (counter < pmu_num_counters(env)) {
1813 pmevcntr_op_start(env, counter);
1814 env->cp15.c14_pmevcntr[counter] = value;
1815 pmevcntr_op_finish(env, counter);
1816 }
1817 /*
1818 * We opt to behave as a RAZ/WI when attempts to access PM[X]EVCNTR
1819 * are CONSTRAINED UNPREDICTABLE.
fdb86656 1820 */
5ecdd3e4
AL
1821}
1822
1823static uint64_t pmevcntr_read(CPUARMState *env, const ARMCPRegInfo *ri,
1824 uint8_t counter)
1825{
1826 if (counter < pmu_num_counters(env)) {
1827 uint64_t ret;
1828 pmevcntr_op_start(env, counter);
1829 ret = env->cp15.c14_pmevcntr[counter];
1830 pmevcntr_op_finish(env, counter);
1831 return ret;
fdb86656 1832 } else {
5ecdd3e4
AL
1833 /* We opt to behave as a RAZ/WI when attempts to access PM[X]EVCNTR
1834 * are CONSTRAINED UNPREDICTABLE. */
fdb86656
WH
1835 return 0;
1836 }
200ac0ef
PM
1837}
1838
5ecdd3e4
AL
1839static void pmevcntr_writefn(CPUARMState *env, const ARMCPRegInfo *ri,
1840 uint64_t value)
1841{
1842 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1843 pmevcntr_write(env, ri, value, counter);
1844}
1845
1846static uint64_t pmevcntr_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
1847{
1848 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1849 return pmevcntr_read(env, ri, counter);
1850}
1851
1852static void pmevcntr_rawwrite(CPUARMState *env, const ARMCPRegInfo *ri,
1853 uint64_t value)
1854{
1855 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1856 assert(counter < pmu_num_counters(env));
1857 env->cp15.c14_pmevcntr[counter] = value;
1858 pmevcntr_write(env, ri, value, counter);
1859}
1860
1861static uint64_t pmevcntr_rawread(CPUARMState *env, const ARMCPRegInfo *ri)
1862{
1863 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1864 assert(counter < pmu_num_counters(env));
1865 return env->cp15.c14_pmevcntr[counter];
1866}
1867
1868static void pmxevcntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1869 uint64_t value)
1870{
1871 pmevcntr_write(env, ri, value, env->cp15.c9_pmselr & 31);
1872}
1873
1874static uint64_t pmxevcntr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1875{
1876 return pmevcntr_read(env, ri, env->cp15.c9_pmselr & 31);
1877}
1878
c4241c7d 1879static void pmuserenr_write(CPUARMState *env, const ARMCPRegInfo *ri,
200ac0ef
PM
1880 uint64_t value)
1881{
6ecd0b6b
AB
1882 if (arm_feature(env, ARM_FEATURE_V8)) {
1883 env->cp15.c9_pmuserenr = value & 0xf;
1884 } else {
1885 env->cp15.c9_pmuserenr = value & 1;
1886 }
200ac0ef
PM
1887}
1888
c4241c7d
PM
1889static void pmintenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1890 uint64_t value)
200ac0ef
PM
1891{
1892 /* We have no event counters so only the C bit can be changed */
7ece99b1 1893 value &= pmu_counter_mask(env);
200ac0ef 1894 env->cp15.c9_pminten |= value;
f4efb4b2 1895 pmu_update_irq(env);
200ac0ef
PM
1896}
1897
c4241c7d
PM
1898static void pmintenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1899 uint64_t value)
200ac0ef 1900{
7ece99b1 1901 value &= pmu_counter_mask(env);
200ac0ef 1902 env->cp15.c9_pminten &= ~value;
f4efb4b2 1903 pmu_update_irq(env);
200ac0ef
PM
1904}
1905
c4241c7d
PM
1906static void vbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
1907 uint64_t value)
8641136c 1908{
a505d7fe
PM
1909 /* Note that even though the AArch64 view of this register has bits
1910 * [10:0] all RES0 we can only mask the bottom 5, to comply with the
1911 * architectural requirements for bits which are RES0 only in some
1912 * contexts. (ARMv8 would permit us to do no masking at all, but ARMv7
1913 * requires the bottom five bits to be RAZ/WI because they're UNK/SBZP.)
1914 */
855ea66d 1915 raw_write(env, ri, value & ~0x1FULL);
8641136c
NR
1916}
1917
64e0e2de
EI
1918static void scr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
1919{
ea22747c
RH
1920 /* Begin with base v8.0 state. */
1921 uint32_t valid_mask = 0x3fff;
2fc0cc0e 1922 ARMCPU *cpu = env_archcpu(env);
ea22747c
RH
1923
1924 if (arm_el_is_aa64(env, 3)) {
1925 value |= SCR_FW | SCR_AW; /* these two bits are RES1. */
1926 valid_mask &= ~SCR_NET;
1927 } else {
1928 valid_mask &= ~(SCR_RW | SCR_ST);
1929 }
64e0e2de
EI
1930
1931 if (!arm_feature(env, ARM_FEATURE_EL2)) {
1932 valid_mask &= ~SCR_HCE;
1933
1934 /* On ARMv7, SMD (or SCD as it is called in v7) is only
1935 * supported if EL2 exists. The bit is UNK/SBZP when
1936 * EL2 is unavailable. In QEMU ARMv7, we force it to always zero
1937 * when EL2 is unavailable.
4eb27640 1938 * On ARMv8, this bit is always available.
64e0e2de 1939 */
4eb27640
GB
1940 if (arm_feature(env, ARM_FEATURE_V7) &&
1941 !arm_feature(env, ARM_FEATURE_V8)) {
64e0e2de
EI
1942 valid_mask &= ~SCR_SMD;
1943 }
1944 }
2d7137c1
RH
1945 if (cpu_isar_feature(aa64_lor, cpu)) {
1946 valid_mask |= SCR_TLOR;
1947 }
ef682cdb
RH
1948 if (cpu_isar_feature(aa64_pauth, cpu)) {
1949 valid_mask |= SCR_API | SCR_APK;
1950 }
64e0e2de
EI
1951
1952 /* Clear all-context RES0 bits. */
1953 value &= valid_mask;
1954 raw_write(env, ri, value);
1955}
1956
630fcd4d
MZ
1957static CPAccessResult access_aa64_tid2(CPUARMState *env,
1958 const ARMCPRegInfo *ri,
1959 bool isread)
1960{
1961 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID2)) {
1962 return CP_ACCESS_TRAP_EL2;
1963 }
1964
1965 return CP_ACCESS_OK;
1966}
1967
c4241c7d 1968static uint64_t ccsidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
776d4e5c 1969{
2fc0cc0e 1970 ARMCPU *cpu = env_archcpu(env);
b85a1fd6
FA
1971
1972 /* Acquire the CSSELR index from the bank corresponding to the CCSIDR
1973 * bank
1974 */
1975 uint32_t index = A32_BANKED_REG_GET(env, csselr,
1976 ri->secure & ARM_CP_SECSTATE_S);
1977
1978 return cpu->ccsidr[index];
776d4e5c
PM
1979}
1980
c4241c7d
PM
1981static void csselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1982 uint64_t value)
776d4e5c 1983{
8d5c773e 1984 raw_write(env, ri, value & 0xf);
776d4e5c
PM
1985}
1986
1090b9c6
PM
1987static uint64_t isr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1988{
29a0af61 1989 CPUState *cs = env_cpu(env);
f7778444 1990 uint64_t hcr_el2 = arm_hcr_el2_eff(env);
1090b9c6 1991 uint64_t ret = 0;
7cf95aed
MZ
1992 bool allow_virt = (arm_current_el(env) == 1 &&
1993 (!arm_is_secure_below_el3(env) ||
1994 (env->cp15.scr_el3 & SCR_EEL2)));
1090b9c6 1995
7cf95aed 1996 if (allow_virt && (hcr_el2 & HCR_IMO)) {
636540e9
PM
1997 if (cs->interrupt_request & CPU_INTERRUPT_VIRQ) {
1998 ret |= CPSR_I;
1999 }
2000 } else {
2001 if (cs->interrupt_request & CPU_INTERRUPT_HARD) {
2002 ret |= CPSR_I;
2003 }
1090b9c6 2004 }
636540e9 2005
7cf95aed 2006 if (allow_virt && (hcr_el2 & HCR_FMO)) {
636540e9
PM
2007 if (cs->interrupt_request & CPU_INTERRUPT_VFIQ) {
2008 ret |= CPSR_F;
2009 }
2010 } else {
2011 if (cs->interrupt_request & CPU_INTERRUPT_FIQ) {
2012 ret |= CPSR_F;
2013 }
1090b9c6 2014 }
636540e9 2015
1090b9c6
PM
2016 /* External aborts are not possible in QEMU so A bit is always clear */
2017 return ret;
2018}
2019
93fbc983
MZ
2020static CPAccessResult access_aa64_tid1(CPUARMState *env, const ARMCPRegInfo *ri,
2021 bool isread)
2022{
2023 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID1)) {
2024 return CP_ACCESS_TRAP_EL2;
2025 }
2026
2027 return CP_ACCESS_OK;
2028}
2029
2030static CPAccessResult access_aa32_tid1(CPUARMState *env, const ARMCPRegInfo *ri,
2031 bool isread)
2032{
2033 if (arm_feature(env, ARM_FEATURE_V8)) {
2034 return access_aa64_tid1(env, ri, isread);
2035 }
2036
2037 return CP_ACCESS_OK;
2038}
2039
e9aa6c21 2040static const ARMCPRegInfo v7_cp_reginfo[] = {
7d57f408
PM
2041 /* the old v6 WFI, UNPREDICTABLE in v7 but we choose to NOP */
2042 { .name = "NOP", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
2043 .access = PL1_W, .type = ARM_CP_NOP },
200ac0ef
PM
2044 /* Performance monitors are implementation defined in v7,
2045 * but with an ARM recommended set of registers, which we
ac689a2e 2046 * follow.
200ac0ef
PM
2047 *
2048 * Performance registers fall into three categories:
2049 * (a) always UNDEF in PL0, RW in PL1 (PMINTENSET, PMINTENCLR)
2050 * (b) RO in PL0 (ie UNDEF on write), RW in PL1 (PMUSERENR)
2051 * (c) UNDEF in PL0 if PMUSERENR.EN==0, otherwise accessible (all others)
2052 * For the cases controlled by PMUSERENR we must set .access to PL0_RW
2053 * or PL0_RO as appropriate and then check PMUSERENR in the helper fn.
2054 */
2055 { .name = "PMCNTENSET", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 1,
7a0e58fa 2056 .access = PL0_RW, .type = ARM_CP_ALIAS,
8521466b 2057 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
fcd25206
PM
2058 .writefn = pmcntenset_write,
2059 .accessfn = pmreg_access,
2060 .raw_writefn = raw_write },
8521466b
AF
2061 { .name = "PMCNTENSET_EL0", .state = ARM_CP_STATE_AA64,
2062 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 1,
2063 .access = PL0_RW, .accessfn = pmreg_access,
2064 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten), .resetvalue = 0,
2065 .writefn = pmcntenset_write, .raw_writefn = raw_write },
200ac0ef 2066 { .name = "PMCNTENCLR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 2,
8521466b
AF
2067 .access = PL0_RW,
2068 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
fcd25206
PM
2069 .accessfn = pmreg_access,
2070 .writefn = pmcntenclr_write,
7a0e58fa 2071 .type = ARM_CP_ALIAS },
8521466b
AF
2072 { .name = "PMCNTENCLR_EL0", .state = ARM_CP_STATE_AA64,
2073 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 2,
2074 .access = PL0_RW, .accessfn = pmreg_access,
7a0e58fa 2075 .type = ARM_CP_ALIAS,
8521466b
AF
2076 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten),
2077 .writefn = pmcntenclr_write },
200ac0ef 2078 { .name = "PMOVSR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 3,
f4efb4b2 2079 .access = PL0_RW, .type = ARM_CP_IO,
e4e91a21 2080 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr),
fcd25206
PM
2081 .accessfn = pmreg_access,
2082 .writefn = pmovsr_write,
2083 .raw_writefn = raw_write },
978364f1
AF
2084 { .name = "PMOVSCLR_EL0", .state = ARM_CP_STATE_AA64,
2085 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 3,
2086 .access = PL0_RW, .accessfn = pmreg_access,
f4efb4b2 2087 .type = ARM_CP_ALIAS | ARM_CP_IO,
978364f1
AF
2088 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
2089 .writefn = pmovsr_write,
2090 .raw_writefn = raw_write },
200ac0ef 2091 { .name = "PMSWINC", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 4,
f4efb4b2
AL
2092 .access = PL0_W, .accessfn = pmreg_access_swinc,
2093 .type = ARM_CP_NO_RAW | ARM_CP_IO,
0d4bfd7d
AL
2094 .writefn = pmswinc_write },
2095 { .name = "PMSWINC_EL0", .state = ARM_CP_STATE_AA64,
2096 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 4,
f4efb4b2
AL
2097 .access = PL0_W, .accessfn = pmreg_access_swinc,
2098 .type = ARM_CP_NO_RAW | ARM_CP_IO,
0d4bfd7d 2099 .writefn = pmswinc_write },
6b040780
WH
2100 { .name = "PMSELR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 5,
2101 .access = PL0_RW, .type = ARM_CP_ALIAS,
2102 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmselr),
6ecd0b6b 2103 .accessfn = pmreg_access_selr, .writefn = pmselr_write,
6b040780
WH
2104 .raw_writefn = raw_write},
2105 { .name = "PMSELR_EL0", .state = ARM_CP_STATE_AA64,
2106 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 5,
6ecd0b6b 2107 .access = PL0_RW, .accessfn = pmreg_access_selr,
6b040780
WH
2108 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmselr),
2109 .writefn = pmselr_write, .raw_writefn = raw_write, },
200ac0ef 2110 { .name = "PMCCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 0,
169c8938 2111 .access = PL0_RW, .resetvalue = 0, .type = ARM_CP_ALIAS | ARM_CP_IO,
421c7ebd 2112 .readfn = pmccntr_read, .writefn = pmccntr_write32,
6ecd0b6b 2113 .accessfn = pmreg_access_ccntr },
8521466b
AF
2114 { .name = "PMCCNTR_EL0", .state = ARM_CP_STATE_AA64,
2115 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 0,
6ecd0b6b 2116 .access = PL0_RW, .accessfn = pmreg_access_ccntr,
8521466b 2117 .type = ARM_CP_IO,
980ebe87
AL
2118 .fieldoffset = offsetof(CPUARMState, cp15.c15_ccnt),
2119 .readfn = pmccntr_read, .writefn = pmccntr_write,
2120 .raw_readfn = raw_read, .raw_writefn = raw_write, },
4b8afa1f
AL
2121 { .name = "PMCCFILTR", .cp = 15, .opc1 = 0, .crn = 14, .crm = 15, .opc2 = 7,
2122 .writefn = pmccfiltr_write_a32, .readfn = pmccfiltr_read_a32,
2123 .access = PL0_RW, .accessfn = pmreg_access,
2124 .type = ARM_CP_ALIAS | ARM_CP_IO,
2125 .resetvalue = 0, },
8521466b
AF
2126 { .name = "PMCCFILTR_EL0", .state = ARM_CP_STATE_AA64,
2127 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 15, .opc2 = 7,
980ebe87 2128 .writefn = pmccfiltr_write, .raw_writefn = raw_write,
8521466b
AF
2129 .access = PL0_RW, .accessfn = pmreg_access,
2130 .type = ARM_CP_IO,
2131 .fieldoffset = offsetof(CPUARMState, cp15.pmccfiltr_el0),
2132 .resetvalue = 0, },
200ac0ef 2133 { .name = "PMXEVTYPER", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 1,
5ecdd3e4
AL
2134 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2135 .accessfn = pmreg_access,
fdb86656
WH
2136 .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
2137 { .name = "PMXEVTYPER_EL0", .state = ARM_CP_STATE_AA64,
2138 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 1,
5ecdd3e4
AL
2139 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2140 .accessfn = pmreg_access,
fdb86656 2141 .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
200ac0ef 2142 { .name = "PMXEVCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 2,
5ecdd3e4
AL
2143 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2144 .accessfn = pmreg_access_xevcntr,
2145 .writefn = pmxevcntr_write, .readfn = pmxevcntr_read },
2146 { .name = "PMXEVCNTR_EL0", .state = ARM_CP_STATE_AA64,
2147 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 2,
2148 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2149 .accessfn = pmreg_access_xevcntr,
2150 .writefn = pmxevcntr_write, .readfn = pmxevcntr_read },
200ac0ef 2151 { .name = "PMUSERENR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 0,
1fce1ba9 2152 .access = PL0_R | PL1_RW, .accessfn = access_tpm,
e4e91a21 2153 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmuserenr),
200ac0ef 2154 .resetvalue = 0,
d4e6df63 2155 .writefn = pmuserenr_write, .raw_writefn = raw_write },
8a83ffc2
AF
2156 { .name = "PMUSERENR_EL0", .state = ARM_CP_STATE_AA64,
2157 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 0,
1fce1ba9 2158 .access = PL0_R | PL1_RW, .accessfn = access_tpm, .type = ARM_CP_ALIAS,
8a83ffc2
AF
2159 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmuserenr),
2160 .resetvalue = 0,
2161 .writefn = pmuserenr_write, .raw_writefn = raw_write },
200ac0ef 2162 { .name = "PMINTENSET", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 1,
1fce1ba9 2163 .access = PL1_RW, .accessfn = access_tpm,
b7d793ad 2164 .type = ARM_CP_ALIAS | ARM_CP_IO,
e6ec5457 2165 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pminten),
200ac0ef 2166 .resetvalue = 0,
d4e6df63 2167 .writefn = pmintenset_write, .raw_writefn = raw_write },
e6ec5457
WH
2168 { .name = "PMINTENSET_EL1", .state = ARM_CP_STATE_AA64,
2169 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 1,
2170 .access = PL1_RW, .accessfn = access_tpm,
2171 .type = ARM_CP_IO,
2172 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2173 .writefn = pmintenset_write, .raw_writefn = raw_write,
2174 .resetvalue = 0x0 },
200ac0ef 2175 { .name = "PMINTENCLR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 2,
fc5f6856
AL
2176 .access = PL1_RW, .accessfn = access_tpm,
2177 .type = ARM_CP_ALIAS | ARM_CP_IO,
200ac0ef 2178 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
b061a82b 2179 .writefn = pmintenclr_write, },
978364f1
AF
2180 { .name = "PMINTENCLR_EL1", .state = ARM_CP_STATE_AA64,
2181 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 2,
fc5f6856
AL
2182 .access = PL1_RW, .accessfn = access_tpm,
2183 .type = ARM_CP_ALIAS | ARM_CP_IO,
978364f1
AF
2184 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2185 .writefn = pmintenclr_write },
7da845b0
PM
2186 { .name = "CCSIDR", .state = ARM_CP_STATE_BOTH,
2187 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 0,
630fcd4d
MZ
2188 .access = PL1_R,
2189 .accessfn = access_aa64_tid2,
2190 .readfn = ccsidr_read, .type = ARM_CP_NO_RAW },
7da845b0
PM
2191 { .name = "CSSELR", .state = ARM_CP_STATE_BOTH,
2192 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 2, .opc2 = 0,
630fcd4d
MZ
2193 .access = PL1_RW,
2194 .accessfn = access_aa64_tid2,
2195 .writefn = csselr_write, .resetvalue = 0,
b85a1fd6
FA
2196 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.csselr_s),
2197 offsetof(CPUARMState, cp15.csselr_ns) } },
776d4e5c
PM
2198 /* Auxiliary ID register: this actually has an IMPDEF value but for now
2199 * just RAZ for all cores:
2200 */
0ff644a7
PM
2201 { .name = "AIDR", .state = ARM_CP_STATE_BOTH,
2202 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 7,
93fbc983
MZ
2203 .access = PL1_R, .type = ARM_CP_CONST,
2204 .accessfn = access_aa64_tid1,
2205 .resetvalue = 0 },
f32cdad5
PM
2206 /* Auxiliary fault status registers: these also are IMPDEF, and we
2207 * choose to RAZ/WI for all cores.
2208 */
2209 { .name = "AFSR0_EL1", .state = ARM_CP_STATE_BOTH,
2210 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 0,
2211 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
2212 { .name = "AFSR1_EL1", .state = ARM_CP_STATE_BOTH,
2213 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 1,
2214 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
b0fe2427
PM
2215 /* MAIR can just read-as-written because we don't implement caches
2216 * and so don't need to care about memory attributes.
2217 */
2218 { .name = "MAIR_EL1", .state = ARM_CP_STATE_AA64,
2219 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
be693c87 2220 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[1]),
b0fe2427 2221 .resetvalue = 0 },
4cfb8ad8
PM
2222 { .name = "MAIR_EL3", .state = ARM_CP_STATE_AA64,
2223 .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 2, .opc2 = 0,
2224 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[3]),
2225 .resetvalue = 0 },
b0fe2427
PM
2226 /* For non-long-descriptor page tables these are PRRR and NMRR;
2227 * regardless they still act as reads-as-written for QEMU.
b0fe2427 2228 */
1281f8e3 2229 /* MAIR0/1 are defined separately from their 64-bit counterpart which
be693c87
GB
2230 * allows them to assign the correct fieldoffset based on the endianness
2231 * handled in the field definitions.
2232 */
a903c449 2233 { .name = "MAIR0", .state = ARM_CP_STATE_AA32,
b0fe2427 2234 .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0, .access = PL1_RW,
be693c87
GB
2235 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair0_s),
2236 offsetof(CPUARMState, cp15.mair0_ns) },
b0fe2427 2237 .resetfn = arm_cp_reset_ignore },
a903c449 2238 { .name = "MAIR1", .state = ARM_CP_STATE_AA32,
b0fe2427 2239 .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 1, .access = PL1_RW,
be693c87
GB
2240 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair1_s),
2241 offsetof(CPUARMState, cp15.mair1_ns) },
b0fe2427 2242 .resetfn = arm_cp_reset_ignore },
1090b9c6
PM
2243 { .name = "ISR_EL1", .state = ARM_CP_STATE_BOTH,
2244 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 0,
7a0e58fa 2245 .type = ARM_CP_NO_RAW, .access = PL1_R, .readfn = isr_read },
995939a6
PM
2246 /* 32 bit ITLB invalidates */
2247 { .name = "ITLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 0,
7a0e58fa 2248 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_write },
995939a6 2249 { .name = "ITLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 1,
7a0e58fa 2250 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write },
995939a6 2251 { .name = "ITLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 2,
7a0e58fa 2252 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiasid_write },
995939a6
PM
2253 /* 32 bit DTLB invalidates */
2254 { .name = "DTLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 0,
7a0e58fa 2255 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_write },
995939a6 2256 { .name = "DTLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 1,
7a0e58fa 2257 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write },
995939a6 2258 { .name = "DTLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 2,
7a0e58fa 2259 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiasid_write },
995939a6
PM
2260 /* 32 bit TLB invalidates */
2261 { .name = "TLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
7a0e58fa 2262 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_write },
995939a6 2263 { .name = "TLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
7a0e58fa 2264 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write },
995939a6 2265 { .name = "TLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
7a0e58fa 2266 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiasid_write },
995939a6 2267 { .name = "TLBIMVAA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
7a0e58fa 2268 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimvaa_write },
995939a6
PM
2269 REGINFO_SENTINEL
2270};
2271
2272static const ARMCPRegInfo v7mp_cp_reginfo[] = {
2273 /* 32 bit TLB invalidates, Inner Shareable */
2274 { .name = "TLBIALLIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
7a0e58fa 2275 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_is_write },
995939a6 2276 { .name = "TLBIMVAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
7a0e58fa 2277 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_is_write },
995939a6 2278 { .name = "TLBIASIDIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
7a0e58fa 2279 .type = ARM_CP_NO_RAW, .access = PL1_W,
fa439fc5 2280 .writefn = tlbiasid_is_write },
995939a6 2281 { .name = "TLBIMVAAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
7a0e58fa 2282 .type = ARM_CP_NO_RAW, .access = PL1_W,
fa439fc5 2283 .writefn = tlbimvaa_is_write },
e9aa6c21
PM
2284 REGINFO_SENTINEL
2285};
2286
327dd510
AL
2287static const ARMCPRegInfo pmovsset_cp_reginfo[] = {
2288 /* PMOVSSET is not implemented in v7 before v7ve */
2289 { .name = "PMOVSSET", .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 3,
2290 .access = PL0_RW, .accessfn = pmreg_access,
f4efb4b2 2291 .type = ARM_CP_ALIAS | ARM_CP_IO,
327dd510
AL
2292 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr),
2293 .writefn = pmovsset_write,
2294 .raw_writefn = raw_write },
2295 { .name = "PMOVSSET_EL0", .state = ARM_CP_STATE_AA64,
2296 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 3,
2297 .access = PL0_RW, .accessfn = pmreg_access,
f4efb4b2 2298 .type = ARM_CP_ALIAS | ARM_CP_IO,
327dd510
AL
2299 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
2300 .writefn = pmovsset_write,
2301 .raw_writefn = raw_write },
2302 REGINFO_SENTINEL
2303};
2304
c4241c7d
PM
2305static void teecr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2306 uint64_t value)
c326b979
PM
2307{
2308 value &= 1;
2309 env->teecr = value;
c326b979
PM
2310}
2311
3f208fd7
PM
2312static CPAccessResult teehbr_access(CPUARMState *env, const ARMCPRegInfo *ri,
2313 bool isread)
c326b979 2314{
dcbff19b 2315 if (arm_current_el(env) == 0 && (env->teecr & 1)) {
92611c00 2316 return CP_ACCESS_TRAP;
c326b979 2317 }
92611c00 2318 return CP_ACCESS_OK;
c326b979
PM
2319}
2320
2321static const ARMCPRegInfo t2ee_cp_reginfo[] = {
2322 { .name = "TEECR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 6, .opc2 = 0,
2323 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, teecr),
2324 .resetvalue = 0,
2325 .writefn = teecr_write },
2326 { .name = "TEEHBR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 6, .opc2 = 0,
2327 .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, teehbr),
92611c00 2328 .accessfn = teehbr_access, .resetvalue = 0 },
c326b979
PM
2329 REGINFO_SENTINEL
2330};
2331
4d31c596 2332static const ARMCPRegInfo v6k_cp_reginfo[] = {
e4fe830b
PM
2333 { .name = "TPIDR_EL0", .state = ARM_CP_STATE_AA64,
2334 .opc0 = 3, .opc1 = 3, .opc2 = 2, .crn = 13, .crm = 0,
2335 .access = PL0_RW,
54bf36ed 2336 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[0]), .resetvalue = 0 },
4d31c596
PM
2337 { .name = "TPIDRURW", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 2,
2338 .access = PL0_RW,
54bf36ed
FA
2339 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrurw_s),
2340 offsetoflow32(CPUARMState, cp15.tpidrurw_ns) },
e4fe830b
PM
2341 .resetfn = arm_cp_reset_ignore },
2342 { .name = "TPIDRRO_EL0", .state = ARM_CP_STATE_AA64,
2343 .opc0 = 3, .opc1 = 3, .opc2 = 3, .crn = 13, .crm = 0,
2344 .access = PL0_R|PL1_W,
54bf36ed
FA
2345 .fieldoffset = offsetof(CPUARMState, cp15.tpidrro_el[0]),
2346 .resetvalue = 0},
4d31c596
PM
2347 { .name = "TPIDRURO", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 3,
2348 .access = PL0_R|PL1_W,
54bf36ed
FA
2349 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidruro_s),
2350 offsetoflow32(CPUARMState, cp15.tpidruro_ns) },
e4fe830b 2351 .resetfn = arm_cp_reset_ignore },
54bf36ed 2352 { .name = "TPIDR_EL1", .state = ARM_CP_STATE_AA64,
e4fe830b 2353 .opc0 = 3, .opc1 = 0, .opc2 = 4, .crn = 13, .crm = 0,
4d31c596 2354 .access = PL1_RW,
54bf36ed
FA
2355 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[1]), .resetvalue = 0 },
2356 { .name = "TPIDRPRW", .opc1 = 0, .cp = 15, .crn = 13, .crm = 0, .opc2 = 4,
2357 .access = PL1_RW,
2358 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrprw_s),
2359 offsetoflow32(CPUARMState, cp15.tpidrprw_ns) },
2360 .resetvalue = 0 },
4d31c596
PM
2361 REGINFO_SENTINEL
2362};
2363
55d284af
PM
2364#ifndef CONFIG_USER_ONLY
2365
3f208fd7
PM
2366static CPAccessResult gt_cntfrq_access(CPUARMState *env, const ARMCPRegInfo *ri,
2367 bool isread)
00108f2d 2368{
75502672
PM
2369 /* CNTFRQ: not visible from PL0 if both PL0PCTEN and PL0VCTEN are zero.
2370 * Writable only at the highest implemented exception level.
2371 */
2372 int el = arm_current_el(env);
5bc84371
RH
2373 uint64_t hcr;
2374 uint32_t cntkctl;
75502672
PM
2375
2376 switch (el) {
2377 case 0:
5bc84371
RH
2378 hcr = arm_hcr_el2_eff(env);
2379 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2380 cntkctl = env->cp15.cnthctl_el2;
2381 } else {
2382 cntkctl = env->cp15.c14_cntkctl;
2383 }
2384 if (!extract32(cntkctl, 0, 2)) {
75502672
PM
2385 return CP_ACCESS_TRAP;
2386 }
2387 break;
2388 case 1:
2389 if (!isread && ri->state == ARM_CP_STATE_AA32 &&
2390 arm_is_secure_below_el3(env)) {
2391 /* Accesses from 32-bit Secure EL1 UNDEF (*not* trap to EL3!) */
2392 return CP_ACCESS_TRAP_UNCATEGORIZED;
2393 }
2394 break;
2395 case 2:
2396 case 3:
2397 break;
00108f2d 2398 }
75502672
PM
2399
2400 if (!isread && el < arm_highest_el(env)) {
2401 return CP_ACCESS_TRAP_UNCATEGORIZED;
2402 }
2403
00108f2d
PM
2404 return CP_ACCESS_OK;
2405}
2406
3f208fd7
PM
2407static CPAccessResult gt_counter_access(CPUARMState *env, int timeridx,
2408 bool isread)
00108f2d 2409{
0b6440af
EI
2410 unsigned int cur_el = arm_current_el(env);
2411 bool secure = arm_is_secure(env);
5bc84371 2412 uint64_t hcr = arm_hcr_el2_eff(env);
0b6440af 2413
5bc84371
RH
2414 switch (cur_el) {
2415 case 0:
2416 /* If HCR_EL2.<E2H,TGE> == '11': check CNTHCTL_EL2.EL0[PV]CTEN. */
2417 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2418 return (extract32(env->cp15.cnthctl_el2, timeridx, 1)
2419 ? CP_ACCESS_OK : CP_ACCESS_TRAP_EL2);
2420 }
0b6440af 2421
5bc84371
RH
2422 /* CNT[PV]CT: not visible from PL0 if EL0[PV]CTEN is zero */
2423 if (!extract32(env->cp15.c14_cntkctl, timeridx, 1)) {
2424 return CP_ACCESS_TRAP;
2425 }
2426
2427 /* If HCR_EL2.<E2H,TGE> == '10': check CNTHCTL_EL2.EL1PCTEN. */
2428 if (hcr & HCR_E2H) {
2429 if (timeridx == GTIMER_PHYS &&
2430 !extract32(env->cp15.cnthctl_el2, 10, 1)) {
2431 return CP_ACCESS_TRAP_EL2;
2432 }
2433 } else {
2434 /* If HCR_EL2.<E2H> == 0: check CNTHCTL_EL2.EL1PCEN. */
2435 if (arm_feature(env, ARM_FEATURE_EL2) &&
2436 timeridx == GTIMER_PHYS && !secure &&
2437 !extract32(env->cp15.cnthctl_el2, 1, 1)) {
2438 return CP_ACCESS_TRAP_EL2;
2439 }
2440 }
2441 break;
2442
2443 case 1:
2444 /* Check CNTHCTL_EL2.EL1PCTEN, which changes location based on E2H. */
2445 if (arm_feature(env, ARM_FEATURE_EL2) &&
2446 timeridx == GTIMER_PHYS && !secure &&
2447 (hcr & HCR_E2H
2448 ? !extract32(env->cp15.cnthctl_el2, 10, 1)
2449 : !extract32(env->cp15.cnthctl_el2, 0, 1))) {
2450 return CP_ACCESS_TRAP_EL2;
2451 }
2452 break;
0b6440af 2453 }
00108f2d
PM
2454 return CP_ACCESS_OK;
2455}
2456
3f208fd7
PM
2457static CPAccessResult gt_timer_access(CPUARMState *env, int timeridx,
2458 bool isread)
00108f2d 2459{
0b6440af
EI
2460 unsigned int cur_el = arm_current_el(env);
2461 bool secure = arm_is_secure(env);
5bc84371 2462 uint64_t hcr = arm_hcr_el2_eff(env);
0b6440af 2463
5bc84371
RH
2464 switch (cur_el) {
2465 case 0:
2466 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2467 /* If HCR_EL2.<E2H,TGE> == '11': check CNTHCTL_EL2.EL0[PV]TEN. */
2468 return (extract32(env->cp15.cnthctl_el2, 9 - timeridx, 1)
2469 ? CP_ACCESS_OK : CP_ACCESS_TRAP_EL2);
2470 }
0b6440af 2471
5bc84371
RH
2472 /*
2473 * CNT[PV]_CVAL, CNT[PV]_CTL, CNT[PV]_TVAL: not visible from
2474 * EL0 if EL0[PV]TEN is zero.
2475 */
2476 if (!extract32(env->cp15.c14_cntkctl, 9 - timeridx, 1)) {
2477 return CP_ACCESS_TRAP;
2478 }
2479 /* fall through */
2480
2481 case 1:
2482 if (arm_feature(env, ARM_FEATURE_EL2) &&
2483 timeridx == GTIMER_PHYS && !secure) {
2484 if (hcr & HCR_E2H) {
2485 /* If HCR_EL2.<E2H,TGE> == '10': check CNTHCTL_EL2.EL1PTEN. */
2486 if (!extract32(env->cp15.cnthctl_el2, 11, 1)) {
2487 return CP_ACCESS_TRAP_EL2;
2488 }
2489 } else {
2490 /* If HCR_EL2.<E2H> == 0: check CNTHCTL_EL2.EL1PCEN. */
2491 if (!extract32(env->cp15.cnthctl_el2, 1, 1)) {
2492 return CP_ACCESS_TRAP_EL2;
2493 }
2494 }
2495 }
2496 break;
0b6440af 2497 }
00108f2d
PM
2498 return CP_ACCESS_OK;
2499}
2500
2501static CPAccessResult gt_pct_access(CPUARMState *env,
3f208fd7
PM
2502 const ARMCPRegInfo *ri,
2503 bool isread)
00108f2d 2504{
3f208fd7 2505 return gt_counter_access(env, GTIMER_PHYS, isread);
00108f2d
PM
2506}
2507
2508static CPAccessResult gt_vct_access(CPUARMState *env,
3f208fd7
PM
2509 const ARMCPRegInfo *ri,
2510 bool isread)
00108f2d 2511{
3f208fd7 2512 return gt_counter_access(env, GTIMER_VIRT, isread);
00108f2d
PM
2513}
2514
3f208fd7
PM
2515static CPAccessResult gt_ptimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
2516 bool isread)
00108f2d 2517{
3f208fd7 2518 return gt_timer_access(env, GTIMER_PHYS, isread);
00108f2d
PM
2519}
2520
3f208fd7
PM
2521static CPAccessResult gt_vtimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
2522 bool isread)
00108f2d 2523{
3f208fd7 2524 return gt_timer_access(env, GTIMER_VIRT, isread);
00108f2d
PM
2525}
2526
b4d3978c 2527static CPAccessResult gt_stimer_access(CPUARMState *env,
3f208fd7
PM
2528 const ARMCPRegInfo *ri,
2529 bool isread)
b4d3978c
PM
2530{
2531 /* The AArch64 register view of the secure physical timer is
2532 * always accessible from EL3, and configurably accessible from
2533 * Secure EL1.
2534 */
2535 switch (arm_current_el(env)) {
2536 case 1:
2537 if (!arm_is_secure(env)) {
2538 return CP_ACCESS_TRAP;
2539 }
2540 if (!(env->cp15.scr_el3 & SCR_ST)) {
2541 return CP_ACCESS_TRAP_EL3;
2542 }
2543 return CP_ACCESS_OK;
2544 case 0:
2545 case 2:
2546 return CP_ACCESS_TRAP;
2547 case 3:
2548 return CP_ACCESS_OK;
2549 default:
2550 g_assert_not_reached();
2551 }
2552}
2553
55d284af
PM
2554static uint64_t gt_get_countervalue(CPUARMState *env)
2555{
7def8754
AJ
2556 ARMCPU *cpu = env_archcpu(env);
2557
2558 return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) / gt_cntfrq_period_ns(cpu);
55d284af
PM
2559}
2560
2561static void gt_recalc_timer(ARMCPU *cpu, int timeridx)
2562{
2563 ARMGenericTimer *gt = &cpu->env.cp15.c14_timer[timeridx];
2564
2565 if (gt->ctl & 1) {
2566 /* Timer enabled: calculate and set current ISTATUS, irq, and
2567 * reset timer to when ISTATUS next has to change
2568 */
edac4d8a
EI
2569 uint64_t offset = timeridx == GTIMER_VIRT ?
2570 cpu->env.cp15.cntvoff_el2 : 0;
55d284af
PM
2571 uint64_t count = gt_get_countervalue(&cpu->env);
2572 /* Note that this must be unsigned 64 bit arithmetic: */
edac4d8a 2573 int istatus = count - offset >= gt->cval;
55d284af 2574 uint64_t nexttick;
194cbc49 2575 int irqstate;
55d284af
PM
2576
2577 gt->ctl = deposit32(gt->ctl, 2, 1, istatus);
194cbc49
PM
2578
2579 irqstate = (istatus && !(gt->ctl & 2));
2580 qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate);
2581
55d284af
PM
2582 if (istatus) {
2583 /* Next transition is when count rolls back over to zero */
2584 nexttick = UINT64_MAX;
2585 } else {
2586 /* Next transition is when we hit cval */
edac4d8a 2587 nexttick = gt->cval + offset;
55d284af
PM
2588 }
2589 /* Note that the desired next expiry time might be beyond the
2590 * signed-64-bit range of a QEMUTimer -- in this case we just
2591 * set the timer for as far in the future as possible. When the
2592 * timer expires we will reset the timer for any remaining period.
2593 */
7def8754 2594 if (nexttick > INT64_MAX / gt_cntfrq_period_ns(cpu)) {
4a0245b6
AJ
2595 timer_mod_ns(cpu->gt_timer[timeridx], INT64_MAX);
2596 } else {
2597 timer_mod(cpu->gt_timer[timeridx], nexttick);
55d284af 2598 }
194cbc49 2599 trace_arm_gt_recalc(timeridx, irqstate, nexttick);
55d284af
PM
2600 } else {
2601 /* Timer disabled: ISTATUS and timer output always clear */
2602 gt->ctl &= ~4;
2603 qemu_set_irq(cpu->gt_timer_outputs[timeridx], 0);
bc72ad67 2604 timer_del(cpu->gt_timer[timeridx]);
194cbc49 2605 trace_arm_gt_recalc_disabled(timeridx);
55d284af
PM
2606 }
2607}
2608
0e3eca4c
EI
2609static void gt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri,
2610 int timeridx)
55d284af 2611{
2fc0cc0e 2612 ARMCPU *cpu = env_archcpu(env);
55d284af 2613
bc72ad67 2614 timer_del(cpu->gt_timer[timeridx]);
55d284af
PM
2615}
2616
c4241c7d 2617static uint64_t gt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
55d284af 2618{
c4241c7d 2619 return gt_get_countervalue(env);
55d284af
PM
2620}
2621
53d1f856
RH
2622static uint64_t gt_virt_cnt_offset(CPUARMState *env)
2623{
2624 uint64_t hcr;
2625
2626 switch (arm_current_el(env)) {
2627 case 2:
2628 hcr = arm_hcr_el2_eff(env);
2629 if (hcr & HCR_E2H) {
2630 return 0;
2631 }
2632 break;
2633 case 0:
2634 hcr = arm_hcr_el2_eff(env);
2635 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2636 return 0;
2637 }
2638 break;
2639 }
2640
2641 return env->cp15.cntvoff_el2;
2642}
2643
edac4d8a
EI
2644static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
2645{
53d1f856 2646 return gt_get_countervalue(env) - gt_virt_cnt_offset(env);
edac4d8a
EI
2647}
2648
c4241c7d 2649static void gt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
0e3eca4c 2650 int timeridx,
c4241c7d 2651 uint64_t value)
55d284af 2652{
194cbc49 2653 trace_arm_gt_cval_write(timeridx, value);
55d284af 2654 env->cp15.c14_timer[timeridx].cval = value;
2fc0cc0e 2655 gt_recalc_timer(env_archcpu(env), timeridx);
55d284af 2656}
c4241c7d 2657
0e3eca4c
EI
2658static uint64_t gt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri,
2659 int timeridx)
55d284af 2660{
53d1f856
RH
2661 uint64_t offset = 0;
2662
2663 switch (timeridx) {
2664 case GTIMER_VIRT:
8c94b071 2665 case GTIMER_HYPVIRT:
53d1f856
RH
2666 offset = gt_virt_cnt_offset(env);
2667 break;
2668 }
55d284af 2669
c4241c7d 2670 return (uint32_t)(env->cp15.c14_timer[timeridx].cval -
edac4d8a 2671 (gt_get_countervalue(env) - offset));
55d284af
PM
2672}
2673
c4241c7d 2674static void gt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
0e3eca4c 2675 int timeridx,
c4241c7d 2676 uint64_t value)
55d284af 2677{
53d1f856
RH
2678 uint64_t offset = 0;
2679
2680 switch (timeridx) {
2681 case GTIMER_VIRT:
8c94b071 2682 case GTIMER_HYPVIRT:
53d1f856
RH
2683 offset = gt_virt_cnt_offset(env);
2684 break;
2685 }
55d284af 2686
194cbc49 2687 trace_arm_gt_tval_write(timeridx, value);
edac4d8a 2688 env->cp15.c14_timer[timeridx].cval = gt_get_countervalue(env) - offset +
18084b2f 2689 sextract64(value, 0, 32);
2fc0cc0e 2690 gt_recalc_timer(env_archcpu(env), timeridx);
55d284af
PM
2691}
2692
c4241c7d 2693static void gt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
0e3eca4c 2694 int timeridx,
c4241c7d 2695 uint64_t value)
55d284af 2696{
2fc0cc0e 2697 ARMCPU *cpu = env_archcpu(env);
55d284af
PM
2698 uint32_t oldval = env->cp15.c14_timer[timeridx].ctl;
2699
194cbc49 2700 trace_arm_gt_ctl_write(timeridx, value);
d3afacc7 2701 env->cp15.c14_timer[timeridx].ctl = deposit64(oldval, 0, 2, value);
55d284af
PM
2702 if ((oldval ^ value) & 1) {
2703 /* Enable toggled */
2704 gt_recalc_timer(cpu, timeridx);
d3afacc7 2705 } else if ((oldval ^ value) & 2) {
55d284af
PM
2706 /* IMASK toggled: don't need to recalculate,
2707 * just set the interrupt line based on ISTATUS
2708 */
194cbc49
PM
2709 int irqstate = (oldval & 4) && !(value & 2);
2710
2711 trace_arm_gt_imask_toggle(timeridx, irqstate);
2712 qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate);
55d284af 2713 }
55d284af
PM
2714}
2715
0e3eca4c
EI
2716static void gt_phys_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2717{
2718 gt_timer_reset(env, ri, GTIMER_PHYS);
2719}
2720
2721static void gt_phys_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2722 uint64_t value)
2723{
2724 gt_cval_write(env, ri, GTIMER_PHYS, value);
2725}
2726
2727static uint64_t gt_phys_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2728{
2729 return gt_tval_read(env, ri, GTIMER_PHYS);
2730}
2731
2732static void gt_phys_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2733 uint64_t value)
2734{
2735 gt_tval_write(env, ri, GTIMER_PHYS, value);
2736}
2737
2738static void gt_phys_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2739 uint64_t value)
2740{
2741 gt_ctl_write(env, ri, GTIMER_PHYS, value);
2742}
2743
bb5972e4
RH
2744static int gt_phys_redir_timeridx(CPUARMState *env)
2745{
2746 switch (arm_mmu_idx(env)) {
2747 case ARMMMUIdx_E20_0:
2748 case ARMMMUIdx_E20_2:
452ef8cb 2749 case ARMMMUIdx_E20_2_PAN:
bb5972e4
RH
2750 return GTIMER_HYP;
2751 default:
2752 return GTIMER_PHYS;
2753 }
2754}
2755
2756static int gt_virt_redir_timeridx(CPUARMState *env)
2757{
2758 switch (arm_mmu_idx(env)) {
2759 case ARMMMUIdx_E20_0:
2760 case ARMMMUIdx_E20_2:
452ef8cb 2761 case ARMMMUIdx_E20_2_PAN:
bb5972e4
RH
2762 return GTIMER_HYPVIRT;
2763 default:
2764 return GTIMER_VIRT;
2765 }
2766}
2767
2768static uint64_t gt_phys_redir_cval_read(CPUARMState *env,
2769 const ARMCPRegInfo *ri)
2770{
2771 int timeridx = gt_phys_redir_timeridx(env);
2772 return env->cp15.c14_timer[timeridx].cval;
2773}
2774
2775static void gt_phys_redir_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2776 uint64_t value)
2777{
2778 int timeridx = gt_phys_redir_timeridx(env);
2779 gt_cval_write(env, ri, timeridx, value);
2780}
2781
2782static uint64_t gt_phys_redir_tval_read(CPUARMState *env,
2783 const ARMCPRegInfo *ri)
2784{
2785 int timeridx = gt_phys_redir_timeridx(env);
2786 return gt_tval_read(env, ri, timeridx);
2787}
2788
2789static void gt_phys_redir_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2790 uint64_t value)
2791{
2792 int timeridx = gt_phys_redir_timeridx(env);
2793 gt_tval_write(env, ri, timeridx, value);
2794}
2795
2796static uint64_t gt_phys_redir_ctl_read(CPUARMState *env,
2797 const ARMCPRegInfo *ri)
2798{
2799 int timeridx = gt_phys_redir_timeridx(env);
2800 return env->cp15.c14_timer[timeridx].ctl;
2801}
2802
2803static void gt_phys_redir_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2804 uint64_t value)
2805{
2806 int timeridx = gt_phys_redir_timeridx(env);
2807 gt_ctl_write(env, ri, timeridx, value);
2808}
2809
0e3eca4c
EI
2810static void gt_virt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2811{
2812 gt_timer_reset(env, ri, GTIMER_VIRT);
2813}
2814
2815static void gt_virt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2816 uint64_t value)
2817{
2818 gt_cval_write(env, ri, GTIMER_VIRT, value);
2819}
2820
2821static uint64_t gt_virt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2822{
2823 return gt_tval_read(env, ri, GTIMER_VIRT);
2824}
2825
2826static void gt_virt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2827 uint64_t value)
2828{
2829 gt_tval_write(env, ri, GTIMER_VIRT, value);
2830}
2831
2832static void gt_virt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2833 uint64_t value)
2834{
2835 gt_ctl_write(env, ri, GTIMER_VIRT, value);
2836}
2837
edac4d8a
EI
2838static void gt_cntvoff_write(CPUARMState *env, const ARMCPRegInfo *ri,
2839 uint64_t value)
2840{
2fc0cc0e 2841 ARMCPU *cpu = env_archcpu(env);
edac4d8a 2842
194cbc49 2843 trace_arm_gt_cntvoff_write(value);
edac4d8a
EI
2844 raw_write(env, ri, value);
2845 gt_recalc_timer(cpu, GTIMER_VIRT);
2846}
2847
bb5972e4
RH
2848static uint64_t gt_virt_redir_cval_read(CPUARMState *env,
2849 const ARMCPRegInfo *ri)
2850{
2851 int timeridx = gt_virt_redir_timeridx(env);
2852 return env->cp15.c14_timer[timeridx].cval;
2853}
2854
2855static void gt_virt_redir_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2856 uint64_t value)
2857{
2858 int timeridx = gt_virt_redir_timeridx(env);
2859 gt_cval_write(env, ri, timeridx, value);
2860}
2861
2862static uint64_t gt_virt_redir_tval_read(CPUARMState *env,
2863 const ARMCPRegInfo *ri)
2864{
2865 int timeridx = gt_virt_redir_timeridx(env);
2866 return gt_tval_read(env, ri, timeridx);
2867}
2868
2869static void gt_virt_redir_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2870 uint64_t value)
2871{
2872 int timeridx = gt_virt_redir_timeridx(env);
2873 gt_tval_write(env, ri, timeridx, value);
2874}
2875
2876static uint64_t gt_virt_redir_ctl_read(CPUARMState *env,
2877 const ARMCPRegInfo *ri)
2878{
2879 int timeridx = gt_virt_redir_timeridx(env);
2880 return env->cp15.c14_timer[timeridx].ctl;
2881}
2882
2883static void gt_virt_redir_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2884 uint64_t value)
2885{
2886 int timeridx = gt_virt_redir_timeridx(env);
2887 gt_ctl_write(env, ri, timeridx, value);
2888}
2889
b0e66d95
EI
2890static void gt_hyp_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2891{
2892 gt_timer_reset(env, ri, GTIMER_HYP);
2893}
2894
2895static void gt_hyp_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2896 uint64_t value)
2897{
2898 gt_cval_write(env, ri, GTIMER_HYP, value);
2899}
2900
2901static uint64_t gt_hyp_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2902{
2903 return gt_tval_read(env, ri, GTIMER_HYP);
2904}
2905
2906static void gt_hyp_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2907 uint64_t value)
2908{
2909 gt_tval_write(env, ri, GTIMER_HYP, value);
2910}
2911
2912static void gt_hyp_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2913 uint64_t value)
2914{
2915 gt_ctl_write(env, ri, GTIMER_HYP, value);
2916}
2917
b4d3978c
PM
2918static void gt_sec_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2919{
2920 gt_timer_reset(env, ri, GTIMER_SEC);
2921}
2922
2923static void gt_sec_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2924 uint64_t value)
2925{
2926 gt_cval_write(env, ri, GTIMER_SEC, value);
2927}
2928
2929static uint64_t gt_sec_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2930{
2931 return gt_tval_read(env, ri, GTIMER_SEC);
2932}
2933
2934static void gt_sec_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2935 uint64_t value)
2936{
2937 gt_tval_write(env, ri, GTIMER_SEC, value);
2938}
2939
2940static void gt_sec_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2941 uint64_t value)
2942{
2943 gt_ctl_write(env, ri, GTIMER_SEC, value);
2944}
2945
8c94b071
RH
2946static void gt_hv_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2947{
2948 gt_timer_reset(env, ri, GTIMER_HYPVIRT);
2949}
2950
2951static void gt_hv_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2952 uint64_t value)
2953{
2954 gt_cval_write(env, ri, GTIMER_HYPVIRT, value);
2955}
2956
2957static uint64_t gt_hv_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2958{
2959 return gt_tval_read(env, ri, GTIMER_HYPVIRT);
2960}
2961
2962static void gt_hv_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2963 uint64_t value)
2964{
2965 gt_tval_write(env, ri, GTIMER_HYPVIRT, value);
2966}
2967
2968static void gt_hv_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2969 uint64_t value)
2970{
2971 gt_ctl_write(env, ri, GTIMER_HYPVIRT, value);
2972}
2973
55d284af
PM
2974void arm_gt_ptimer_cb(void *opaque)
2975{
2976 ARMCPU *cpu = opaque;
2977
2978 gt_recalc_timer(cpu, GTIMER_PHYS);
2979}
2980
2981void arm_gt_vtimer_cb(void *opaque)
2982{
2983 ARMCPU *cpu = opaque;
2984
2985 gt_recalc_timer(cpu, GTIMER_VIRT);
2986}
2987
b0e66d95
EI
2988void arm_gt_htimer_cb(void *opaque)
2989{
2990 ARMCPU *cpu = opaque;
2991
2992 gt_recalc_timer(cpu, GTIMER_HYP);
2993}
2994
b4d3978c
PM
2995void arm_gt_stimer_cb(void *opaque)
2996{
2997 ARMCPU *cpu = opaque;
2998
2999 gt_recalc_timer(cpu, GTIMER_SEC);
3000}
3001
8c94b071
RH
3002void arm_gt_hvtimer_cb(void *opaque)
3003{
3004 ARMCPU *cpu = opaque;
3005
3006 gt_recalc_timer(cpu, GTIMER_HYPVIRT);
3007}
3008
96eec6b2
AJ
3009static void arm_gt_cntfrq_reset(CPUARMState *env, const ARMCPRegInfo *opaque)
3010{
3011 ARMCPU *cpu = env_archcpu(env);
3012
3013 cpu->env.cp15.c14_cntfrq = cpu->gt_cntfrq_hz;
3014}
3015
55d284af
PM
3016static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
3017 /* Note that CNTFRQ is purely reads-as-written for the benefit
3018 * of software; writing it doesn't actually change the timer frequency.
3019 * Our reset value matches the fixed frequency we implement the timer at.
3020 */
3021 { .name = "CNTFRQ", .cp = 15, .crn = 14, .crm = 0, .opc1 = 0, .opc2 = 0,
7a0e58fa 3022 .type = ARM_CP_ALIAS,
a7adc4b7
PM
3023 .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
3024 .fieldoffset = offsetoflow32(CPUARMState, cp15.c14_cntfrq),
a7adc4b7
PM
3025 },
3026 { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
3027 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
3028 .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
55d284af 3029 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
96eec6b2 3030 .resetfn = arm_gt_cntfrq_reset,
55d284af
PM
3031 },
3032 /* overall control: mostly access permissions */
a7adc4b7
PM
3033 { .name = "CNTKCTL", .state = ARM_CP_STATE_BOTH,
3034 .opc0 = 3, .opc1 = 0, .crn = 14, .crm = 1, .opc2 = 0,
55d284af
PM
3035 .access = PL1_RW,
3036 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntkctl),
3037 .resetvalue = 0,
3038 },
3039 /* per-timer control */
3040 { .name = "CNTP_CTL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
9ff9dd3c 3041 .secure = ARM_CP_SECSTATE_NS,
daf1dc5f 3042 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
a7adc4b7
PM
3043 .accessfn = gt_ptimer_access,
3044 .fieldoffset = offsetoflow32(CPUARMState,
3045 cp15.c14_timer[GTIMER_PHYS].ctl),
bb5972e4
RH
3046 .readfn = gt_phys_redir_ctl_read, .raw_readfn = raw_read,
3047 .writefn = gt_phys_redir_ctl_write, .raw_writefn = raw_write,
a7adc4b7 3048 },
9c513e78 3049 { .name = "CNTP_CTL_S",
9ff9dd3c
PM
3050 .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
3051 .secure = ARM_CP_SECSTATE_S,
daf1dc5f 3052 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
9ff9dd3c
PM
3053 .accessfn = gt_ptimer_access,
3054 .fieldoffset = offsetoflow32(CPUARMState,
3055 cp15.c14_timer[GTIMER_SEC].ctl),
3056 .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
3057 },
a7adc4b7
PM
3058 { .name = "CNTP_CTL_EL0", .state = ARM_CP_STATE_AA64,
3059 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 1,
daf1dc5f 3060 .type = ARM_CP_IO, .access = PL0_RW,
a7adc4b7 3061 .accessfn = gt_ptimer_access,
55d284af
PM
3062 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
3063 .resetvalue = 0,
bb5972e4
RH
3064 .readfn = gt_phys_redir_ctl_read, .raw_readfn = raw_read,
3065 .writefn = gt_phys_redir_ctl_write, .raw_writefn = raw_write,
55d284af
PM
3066 },
3067 { .name = "CNTV_CTL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 1,
daf1dc5f 3068 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
a7adc4b7
PM
3069 .accessfn = gt_vtimer_access,
3070 .fieldoffset = offsetoflow32(CPUARMState,
3071 cp15.c14_timer[GTIMER_VIRT].ctl),
bb5972e4
RH
3072 .readfn = gt_virt_redir_ctl_read, .raw_readfn = raw_read,
3073 .writefn = gt_virt_redir_ctl_write, .raw_writefn = raw_write,
a7adc4b7
PM
3074 },
3075 { .name = "CNTV_CTL_EL0", .state = ARM_CP_STATE_AA64,
3076 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 1,
daf1dc5f 3077 .type = ARM_CP_IO, .access = PL0_RW,
a7adc4b7 3078 .accessfn = gt_vtimer_access,
55d284af
PM
3079 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
3080 .resetvalue = 0,
bb5972e4
RH
3081 .readfn = gt_virt_redir_ctl_read, .raw_readfn = raw_read,
3082 .writefn = gt_virt_redir_ctl_write, .raw_writefn = raw_write,
55d284af
PM
3083 },
3084 /* TimerValue views: a 32 bit downcounting view of the underlying state */
3085 { .name = "CNTP_TVAL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
9ff9dd3c 3086 .secure = ARM_CP_SECSTATE_NS,
daf1dc5f 3087 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
00108f2d 3088 .accessfn = gt_ptimer_access,
bb5972e4 3089 .readfn = gt_phys_redir_tval_read, .writefn = gt_phys_redir_tval_write,
55d284af 3090 },
9c513e78 3091 { .name = "CNTP_TVAL_S",
9ff9dd3c
PM
3092 .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
3093 .secure = ARM_CP_SECSTATE_S,
daf1dc5f 3094 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
9ff9dd3c
PM
3095 .accessfn = gt_ptimer_access,
3096 .readfn = gt_sec_tval_read, .writefn = gt_sec_tval_write,
3097 },
a7adc4b7
PM
3098 { .name = "CNTP_TVAL_EL0", .state = ARM_CP_STATE_AA64,
3099 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 0,
daf1dc5f 3100 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
0e3eca4c 3101 .accessfn = gt_ptimer_access, .resetfn = gt_phys_timer_reset,
bb5972e4 3102 .readfn = gt_phys_redir_tval_read, .writefn = gt_phys_redir_tval_write,
a7adc4b7 3103 },
55d284af 3104 { .name = "CNTV_TVAL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 0,
daf1dc5f 3105 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
00108f2d 3106 .accessfn = gt_vtimer_access,
bb5972e4 3107 .readfn = gt_virt_redir_tval_read, .writefn = gt_virt_redir_tval_write,
55d284af 3108 },
a7adc4b7
PM
3109 { .name = "CNTV_TVAL_EL0", .state = ARM_CP_STATE_AA64,
3110 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 0,
daf1dc5f 3111 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
0e3eca4c 3112 .accessfn = gt_vtimer_access, .resetfn = gt_virt_timer_reset,
bb5972e4 3113 .readfn = gt_virt_redir_tval_read, .writefn = gt_virt_redir_tval_write,
a7adc4b7 3114 },
55d284af
PM
3115 /* The counter itself */
3116 { .name = "CNTPCT", .cp = 15, .crm = 14, .opc1 = 0,
7a0e58fa 3117 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
00108f2d 3118 .accessfn = gt_pct_access,
a7adc4b7
PM
3119 .readfn = gt_cnt_read, .resetfn = arm_cp_reset_ignore,
3120 },
3121 { .name = "CNTPCT_EL0", .state = ARM_CP_STATE_AA64,
3122 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 1,
7a0e58fa 3123 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
d57b9ee8 3124 .accessfn = gt_pct_access, .readfn = gt_cnt_read,
55d284af
PM
3125 },
3126 { .name = "CNTVCT", .cp = 15, .crm = 14, .opc1 = 1,
7a0e58fa 3127 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
00108f2d 3128 .accessfn = gt_vct_access,
edac4d8a 3129 .readfn = gt_virt_cnt_read, .resetfn = arm_cp_reset_ignore,
a7adc4b7
PM
3130 },
3131 { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
3132 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
7a0e58fa 3133 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
d57b9ee8 3134 .accessfn = gt_vct_access, .readfn = gt_virt_cnt_read,
55d284af
PM
3135 },
3136 /* Comparison value, indicating when the timer goes off */
3137 { .name = "CNTP_CVAL", .cp = 15, .crm = 14, .opc1 = 2,
9ff9dd3c 3138 .secure = ARM_CP_SECSTATE_NS,
daf1dc5f 3139 .access = PL0_RW,
7a0e58fa 3140 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
55d284af 3141 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
b061a82b 3142 .accessfn = gt_ptimer_access,
bb5972e4
RH
3143 .readfn = gt_phys_redir_cval_read, .raw_readfn = raw_read,
3144 .writefn = gt_phys_redir_cval_write, .raw_writefn = raw_write,
a7adc4b7 3145 },
9c513e78 3146 { .name = "CNTP_CVAL_S", .cp = 15, .crm = 14, .opc1 = 2,
9ff9dd3c 3147 .secure = ARM_CP_SECSTATE_S,
daf1dc5f 3148 .access = PL0_RW,
9ff9dd3c
PM
3149 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
3150 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
3151 .accessfn = gt_ptimer_access,
3152 .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
3153 },
a7adc4b7
PM
3154 { .name = "CNTP_CVAL_EL0", .state = ARM_CP_STATE_AA64,
3155 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 2,
daf1dc5f 3156 .access = PL0_RW,
a7adc4b7
PM
3157 .type = ARM_CP_IO,
3158 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
12cde08a 3159 .resetvalue = 0, .accessfn = gt_ptimer_access,
bb5972e4
RH
3160 .readfn = gt_phys_redir_cval_read, .raw_readfn = raw_read,
3161 .writefn = gt_phys_redir_cval_write, .raw_writefn = raw_write,
55d284af
PM
3162 },
3163 { .name = "CNTV_CVAL", .cp = 15, .crm = 14, .opc1 = 3,
daf1dc5f 3164 .access = PL0_RW,
7a0e58fa 3165 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
55d284af 3166 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
b061a82b 3167 .accessfn = gt_vtimer_access,
bb5972e4
RH
3168 .readfn = gt_virt_redir_cval_read, .raw_readfn = raw_read,
3169 .writefn = gt_virt_redir_cval_write, .raw_writefn = raw_write,
a7adc4b7
PM
3170 },
3171 { .name = "CNTV_CVAL_EL0", .state = ARM_CP_STATE_AA64,
3172 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 2,
daf1dc5f 3173 .access = PL0_RW,
a7adc4b7
PM
3174 .type = ARM_CP_IO,
3175 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
3176 .resetvalue = 0, .accessfn = gt_vtimer_access,
bb5972e4
RH
3177 .readfn = gt_virt_redir_cval_read, .raw_readfn = raw_read,
3178 .writefn = gt_virt_redir_cval_write, .raw_writefn = raw_write,
55d284af 3179 },
b4d3978c
PM
3180 /* Secure timer -- this is actually restricted to only EL3
3181 * and configurably Secure-EL1 via the accessfn.
3182 */
3183 { .name = "CNTPS_TVAL_EL1", .state = ARM_CP_STATE_AA64,
3184 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 0,
3185 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW,
3186 .accessfn = gt_stimer_access,
3187 .readfn = gt_sec_tval_read,
3188 .writefn = gt_sec_tval_write,
3189 .resetfn = gt_sec_timer_reset,
3190 },
3191 { .name = "CNTPS_CTL_EL1", .state = ARM_CP_STATE_AA64,
3192 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 1,
3193 .type = ARM_CP_IO, .access = PL1_RW,
3194 .accessfn = gt_stimer_access,
3195 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].ctl),
3196 .resetvalue = 0,
3197 .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
3198 },
3199 { .name = "CNTPS_CVAL_EL1", .state = ARM_CP_STATE_AA64,
3200 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 2,
3201 .type = ARM_CP_IO, .access = PL1_RW,
3202 .accessfn = gt_stimer_access,
3203 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
3204 .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
3205 },
55d284af
PM
3206 REGINFO_SENTINEL
3207};
3208
bb5972e4
RH
3209static CPAccessResult e2h_access(CPUARMState *env, const ARMCPRegInfo *ri,
3210 bool isread)
3211{
3212 if (!(arm_hcr_el2_eff(env) & HCR_E2H)) {
3213 return CP_ACCESS_TRAP;
3214 }
3215 return CP_ACCESS_OK;
3216}
3217
55d284af 3218#else
26c4a83b
AB
3219
3220/* In user-mode most of the generic timer registers are inaccessible
3221 * however modern kernels (4.12+) allow access to cntvct_el0
55d284af 3222 */
26c4a83b
AB
3223
3224static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
3225{
7def8754
AJ
3226 ARMCPU *cpu = env_archcpu(env);
3227
26c4a83b
AB
3228 /* Currently we have no support for QEMUTimer in linux-user so we
3229 * can't call gt_get_countervalue(env), instead we directly
3230 * call the lower level functions.
3231 */
7def8754 3232 return cpu_get_clock() / gt_cntfrq_period_ns(cpu);
26c4a83b
AB
3233}
3234
6cc7a3ae 3235static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
26c4a83b
AB
3236 { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
3237 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
3238 .type = ARM_CP_CONST, .access = PL0_R /* no PL1_RW in linux-user */,
3239 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
3240 .resetvalue = NANOSECONDS_PER_SECOND / GTIMER_SCALE,
3241 },
3242 { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
3243 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
3244 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3245 .readfn = gt_virt_cnt_read,
3246 },
6cc7a3ae
PM
3247 REGINFO_SENTINEL
3248};
3249
55d284af
PM
3250#endif
3251
c4241c7d 3252static void par_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
4a501606 3253{
891a2fe7 3254 if (arm_feature(env, ARM_FEATURE_LPAE)) {
8d5c773e 3255 raw_write(env, ri, value);
891a2fe7 3256 } else if (arm_feature(env, ARM_FEATURE_V7)) {
8d5c773e 3257 raw_write(env, ri, value & 0xfffff6ff);
4a501606 3258 } else {
8d5c773e 3259 raw_write(env, ri, value & 0xfffff1ff);
4a501606 3260 }
4a501606
PM
3261}
3262
3263#ifndef CONFIG_USER_ONLY
3264/* get_phys_addr() isn't present for user-mode-only targets */
702a9357 3265
3f208fd7
PM
3266static CPAccessResult ats_access(CPUARMState *env, const ARMCPRegInfo *ri,
3267 bool isread)
92611c00
PM
3268{
3269 if (ri->opc2 & 4) {
87562e4f
PM
3270 /* The ATS12NSO* operations must trap to EL3 if executed in
3271 * Secure EL1 (which can only happen if EL3 is AArch64).
3272 * They are simply UNDEF if executed from NS EL1.
3273 * They function normally from EL2 or EL3.
92611c00 3274 */
87562e4f
PM
3275 if (arm_current_el(env) == 1) {
3276 if (arm_is_secure_below_el3(env)) {
3277 return CP_ACCESS_TRAP_UNCATEGORIZED_EL3;
3278 }
3279 return CP_ACCESS_TRAP_UNCATEGORIZED;
3280 }
92611c00
PM
3281 }
3282 return CP_ACCESS_OK;
3283}
3284
060e8a48 3285static uint64_t do_ats_write(CPUARMState *env, uint64_t value,
03ae85f8 3286 MMUAccessType access_type, ARMMMUIdx mmu_idx)
4a501606 3287{
a8170e5e 3288 hwaddr phys_addr;
4a501606
PM
3289 target_ulong page_size;
3290 int prot;
b7cc4e82 3291 bool ret;
01c097f7 3292 uint64_t par64;
1313e2d7 3293 bool format64 = false;
8bf5b6a9 3294 MemTxAttrs attrs = {};
e14b5a23 3295 ARMMMUFaultInfo fi = {};
5b2d261d 3296 ARMCacheAttrs cacheattrs = {};
4a501606 3297
5b2d261d 3298 ret = get_phys_addr(env, value, access_type, mmu_idx, &phys_addr, &attrs,
bc52bfeb 3299 &prot, &page_size, &fi, &cacheattrs);
1313e2d7 3300
0710b2fa
PM
3301 if (ret) {
3302 /*
3303 * Some kinds of translation fault must cause exceptions rather
3304 * than being reported in the PAR.
3305 */
3306 int current_el = arm_current_el(env);
3307 int target_el;
3308 uint32_t syn, fsr, fsc;
3309 bool take_exc = false;
3310
3311 if (fi.s1ptw && current_el == 1 && !arm_is_secure(env)
fee7aa46 3312 && arm_mmu_idx_is_stage1_of_2(mmu_idx)) {
0710b2fa
PM
3313 /*
3314 * Synchronous stage 2 fault on an access made as part of the
3315 * translation table walk for AT S1E0* or AT S1E1* insn
3316 * executed from NS EL1. If this is a synchronous external abort
3317 * and SCR_EL3.EA == 1, then we take a synchronous external abort
3318 * to EL3. Otherwise the fault is taken as an exception to EL2,
3319 * and HPFAR_EL2 holds the faulting IPA.
3320 */
3321 if (fi.type == ARMFault_SyncExternalOnWalk &&
3322 (env->cp15.scr_el3 & SCR_EA)) {
3323 target_el = 3;
3324 } else {
3325 env->cp15.hpfar_el2 = extract64(fi.s2addr, 12, 47) << 4;
3326 target_el = 2;
3327 }
3328 take_exc = true;
3329 } else if (fi.type == ARMFault_SyncExternalOnWalk) {
3330 /*
3331 * Synchronous external aborts during a translation table walk
3332 * are taken as Data Abort exceptions.
3333 */
3334 if (fi.stage2) {
3335 if (current_el == 3) {
3336 target_el = 3;
3337 } else {
3338 target_el = 2;
3339 }
3340 } else {
3341 target_el = exception_target_el(env);
3342 }
3343 take_exc = true;
3344 }
3345
3346 if (take_exc) {
3347 /* Construct FSR and FSC using same logic as arm_deliver_fault() */
3348 if (target_el == 2 || arm_el_is_aa64(env, target_el) ||
3349 arm_s1_regime_using_lpae_format(env, mmu_idx)) {
3350 fsr = arm_fi_to_lfsc(&fi);
3351 fsc = extract32(fsr, 0, 6);
3352 } else {
3353 fsr = arm_fi_to_sfsc(&fi);
3354 fsc = 0x3f;
3355 }
3356 /*
3357 * Report exception with ESR indicating a fault due to a
3358 * translation table walk for a cache maintenance instruction.
3359 */
3360 syn = syn_data_abort_no_iss(current_el == target_el,
3361 fi.ea, 1, fi.s1ptw, 1, fsc);
3362 env->exception.vaddress = value;
3363 env->exception.fsr = fsr;
3364 raise_exception(env, EXCP_DATA_ABORT, syn, target_el);
3365 }
3366 }
3367
1313e2d7
EI
3368 if (is_a64(env)) {
3369 format64 = true;
3370 } else if (arm_feature(env, ARM_FEATURE_LPAE)) {
3371 /*
3372 * ATS1Cxx:
3373 * * TTBCR.EAE determines whether the result is returned using the
3374 * 32-bit or the 64-bit PAR format
3375 * * Instructions executed in Hyp mode always use the 64bit format
3376 *
3377 * ATS1S2NSOxx uses the 64bit format if any of the following is true:
3378 * * The Non-secure TTBCR.EAE bit is set to 1
3379 * * The implementation includes EL2, and the value of HCR.VM is 1
3380 *
9d1bab33
PM
3381 * (Note that HCR.DC makes HCR.VM behave as if it is 1.)
3382 *
23463e0e 3383 * ATS1Hx always uses the 64bit format.
1313e2d7
EI
3384 */
3385 format64 = arm_s1_regime_using_lpae_format(env, mmu_idx);
3386
3387 if (arm_feature(env, ARM_FEATURE_EL2)) {
452ef8cb
RH
3388 if (mmu_idx == ARMMMUIdx_E10_0 ||
3389 mmu_idx == ARMMMUIdx_E10_1 ||
3390 mmu_idx == ARMMMUIdx_E10_1_PAN) {
9d1bab33 3391 format64 |= env->cp15.hcr_el2 & (HCR_VM | HCR_DC);
1313e2d7
EI
3392 } else {
3393 format64 |= arm_current_el(env) == 2;
3394 }
3395 }
3396 }
3397
3398 if (format64) {
5efe9ed4 3399 /* Create a 64-bit PAR */
01c097f7 3400 par64 = (1 << 11); /* LPAE bit always set */
b7cc4e82 3401 if (!ret) {
702a9357 3402 par64 |= phys_addr & ~0xfffULL;
8bf5b6a9
PM
3403 if (!attrs.secure) {
3404 par64 |= (1 << 9); /* NS */
3405 }
5b2d261d
AB
3406 par64 |= (uint64_t)cacheattrs.attrs << 56; /* ATTR */
3407 par64 |= cacheattrs.shareability << 7; /* SH */
4a501606 3408 } else {
5efe9ed4
PM
3409 uint32_t fsr = arm_fi_to_lfsc(&fi);
3410
702a9357 3411 par64 |= 1; /* F */
b7cc4e82 3412 par64 |= (fsr & 0x3f) << 1; /* FS */
0f7b791b
PM
3413 if (fi.stage2) {
3414 par64 |= (1 << 9); /* S */
3415 }
3416 if (fi.s1ptw) {
3417 par64 |= (1 << 8); /* PTW */
3418 }
4a501606
PM
3419 }
3420 } else {
b7cc4e82 3421 /* fsr is a DFSR/IFSR value for the short descriptor
702a9357
PM
3422 * translation table format (with WnR always clear).
3423 * Convert it to a 32-bit PAR.
3424 */
b7cc4e82 3425 if (!ret) {
702a9357
PM
3426 /* We do not set any attribute bits in the PAR */
3427 if (page_size == (1 << 24)
3428 && arm_feature(env, ARM_FEATURE_V7)) {
01c097f7 3429 par64 = (phys_addr & 0xff000000) | (1 << 1);
702a9357 3430 } else {
01c097f7 3431 par64 = phys_addr & 0xfffff000;
702a9357 3432 }
8bf5b6a9
PM
3433 if (!attrs.secure) {
3434 par64 |= (1 << 9); /* NS */
3435 }
702a9357 3436 } else {
5efe9ed4
PM
3437 uint32_t fsr = arm_fi_to_sfsc(&fi);
3438
b7cc4e82
PC
3439 par64 = ((fsr & (1 << 10)) >> 5) | ((fsr & (1 << 12)) >> 6) |
3440 ((fsr & 0xf) << 1) | 1;
702a9357 3441 }
4a501606 3442 }
060e8a48
PM
3443 return par64;
3444}
3445
3446static void ats_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
3447{
03ae85f8 3448 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
060e8a48 3449 uint64_t par64;
d3649702
PM
3450 ARMMMUIdx mmu_idx;
3451 int el = arm_current_el(env);
3452 bool secure = arm_is_secure_below_el3(env);
060e8a48 3453
d3649702
PM
3454 switch (ri->opc2 & 6) {
3455 case 0:
04b07d29 3456 /* stage 1 current state PL1: ATS1CPR, ATS1CPW, ATS1CPRP, ATS1CPWP */
d3649702
PM
3457 switch (el) {
3458 case 3:
127b2b08 3459 mmu_idx = ARMMMUIdx_SE3;
d3649702
PM
3460 break;
3461 case 2:
04b07d29
RH
3462 g_assert(!secure); /* TODO: ARMv8.4-SecEL2 */
3463 /* fall through */
d3649702 3464 case 1:
04b07d29
RH
3465 if (ri->crm == 9 && (env->uncached_cpsr & CPSR_PAN)) {
3466 mmu_idx = (secure ? ARMMMUIdx_SE10_1_PAN
3467 : ARMMMUIdx_Stage1_E1_PAN);
3468 } else {
3469 mmu_idx = secure ? ARMMMUIdx_SE10_1 : ARMMMUIdx_Stage1_E1;
3470 }
d3649702
PM
3471 break;
3472 default:
3473 g_assert_not_reached();
3474 }
3475 break;
3476 case 2:
3477 /* stage 1 current state PL0: ATS1CUR, ATS1CUW */
3478 switch (el) {
3479 case 3:
fba37aed 3480 mmu_idx = ARMMMUIdx_SE10_0;
d3649702
PM
3481 break;
3482 case 2:
2859d7b5 3483 mmu_idx = ARMMMUIdx_Stage1_E0;
d3649702
PM
3484 break;
3485 case 1:
fba37aed 3486 mmu_idx = secure ? ARMMMUIdx_SE10_0 : ARMMMUIdx_Stage1_E0;
d3649702
PM
3487 break;
3488 default:
3489 g_assert_not_reached();
3490 }
3491 break;
3492 case 4:
3493 /* stage 1+2 NonSecure PL1: ATS12NSOPR, ATS12NSOPW */
01b98b68 3494 mmu_idx = ARMMMUIdx_E10_1;
d3649702
PM
3495 break;
3496 case 6:
3497 /* stage 1+2 NonSecure PL0: ATS12NSOUR, ATS12NSOUW */
01b98b68 3498 mmu_idx = ARMMMUIdx_E10_0;
d3649702
PM
3499 break;
3500 default:
3501 g_assert_not_reached();
3502 }
3503
3504 par64 = do_ats_write(env, value, access_type, mmu_idx);
01c097f7
FA
3505
3506 A32_BANKED_CURRENT_REG_SET(env, par, par64);
4a501606 3507}
060e8a48 3508
14db7fe0
PM
3509static void ats1h_write(CPUARMState *env, const ARMCPRegInfo *ri,
3510 uint64_t value)
3511{
03ae85f8 3512 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
14db7fe0
PM
3513 uint64_t par64;
3514
e013b741 3515 par64 = do_ats_write(env, value, access_type, ARMMMUIdx_E2);
14db7fe0
PM
3516
3517 A32_BANKED_CURRENT_REG_SET(env, par, par64);
3518}
3519
3f208fd7
PM
3520static CPAccessResult at_s1e2_access(CPUARMState *env, const ARMCPRegInfo *ri,
3521 bool isread)
2a47df95
PM
3522{
3523 if (arm_current_el(env) == 3 && !(env->cp15.scr_el3 & SCR_NS)) {
3524 return CP_ACCESS_TRAP;
3525 }
3526 return CP_ACCESS_OK;
3527}
3528
060e8a48
PM
3529static void ats_write64(CPUARMState *env, const ARMCPRegInfo *ri,
3530 uint64_t value)
3531{
03ae85f8 3532 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
d3649702
PM
3533 ARMMMUIdx mmu_idx;
3534 int secure = arm_is_secure_below_el3(env);
3535
3536 switch (ri->opc2 & 6) {
3537 case 0:
3538 switch (ri->opc1) {
04b07d29
RH
3539 case 0: /* AT S1E1R, AT S1E1W, AT S1E1RP, AT S1E1WP */
3540 if (ri->crm == 9 && (env->pstate & PSTATE_PAN)) {
3541 mmu_idx = (secure ? ARMMMUIdx_SE10_1_PAN
3542 : ARMMMUIdx_Stage1_E1_PAN);
3543 } else {
3544 mmu_idx = secure ? ARMMMUIdx_SE10_1 : ARMMMUIdx_Stage1_E1;
3545 }
d3649702
PM
3546 break;
3547 case 4: /* AT S1E2R, AT S1E2W */
e013b741 3548 mmu_idx = ARMMMUIdx_E2;
d3649702
PM
3549 break;
3550 case 6: /* AT S1E3R, AT S1E3W */
127b2b08 3551 mmu_idx = ARMMMUIdx_SE3;
d3649702
PM
3552 break;
3553 default:
3554 g_assert_not_reached();
3555 }
3556 break;
3557 case 2: /* AT S1E0R, AT S1E0W */
fba37aed 3558 mmu_idx = secure ? ARMMMUIdx_SE10_0 : ARMMMUIdx_Stage1_E0;
d3649702
PM
3559 break;
3560 case 4: /* AT S12E1R, AT S12E1W */
fba37aed 3561 mmu_idx = secure ? ARMMMUIdx_SE10_1 : ARMMMUIdx_E10_1;
d3649702
PM
3562 break;
3563 case 6: /* AT S12E0R, AT S12E0W */
fba37aed 3564 mmu_idx = secure ? ARMMMUIdx_SE10_0 : ARMMMUIdx_E10_0;
d3649702
PM
3565 break;
3566 default:
3567 g_assert_not_reached();
3568 }
060e8a48 3569
d3649702 3570 env->cp15.par_el[1] = do_ats_write(env, value, access_type, mmu_idx);
060e8a48 3571}
4a501606
PM
3572#endif
3573
3574static const ARMCPRegInfo vapa_cp_reginfo[] = {
3575 { .name = "PAR", .cp = 15, .crn = 7, .crm = 4, .opc1 = 0, .opc2 = 0,
3576 .access = PL1_RW, .resetvalue = 0,
01c097f7
FA
3577 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.par_s),
3578 offsetoflow32(CPUARMState, cp15.par_ns) },
4a501606
PM
3579 .writefn = par_write },
3580#ifndef CONFIG_USER_ONLY
87562e4f 3581 /* This underdecoding is safe because the reginfo is NO_RAW. */
4a501606 3582 { .name = "ATS", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = CP_ANY,
92611c00 3583 .access = PL1_W, .accessfn = ats_access,
0710b2fa 3584 .writefn = ats_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
4a501606
PM
3585#endif
3586 REGINFO_SENTINEL
3587};
3588
18032bec
PM
3589/* Return basic MPU access permission bits. */
3590static uint32_t simple_mpu_ap_bits(uint32_t val)
3591{
3592 uint32_t ret;
3593 uint32_t mask;
3594 int i;
3595 ret = 0;
3596 mask = 3;
3597 for (i = 0; i < 16; i += 2) {
3598 ret |= (val >> i) & mask;
3599 mask <<= 2;
3600 }
3601 return ret;
3602}
3603
3604/* Pad basic MPU access permission bits to extended format. */
3605static uint32_t extended_mpu_ap_bits(uint32_t val)
3606{
3607 uint32_t ret;
3608 uint32_t mask;
3609 int i;
3610 ret = 0;
3611 mask = 3;
3612 for (i = 0; i < 16; i += 2) {
3613 ret |= (val & mask) << i;
3614 mask <<= 2;
3615 }
3616 return ret;
3617}
3618
c4241c7d
PM
3619static void pmsav5_data_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
3620 uint64_t value)
18032bec 3621{
7e09797c 3622 env->cp15.pmsav5_data_ap = extended_mpu_ap_bits(value);
18032bec
PM
3623}
3624
c4241c7d 3625static uint64_t pmsav5_data_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
18032bec 3626{
7e09797c 3627 return simple_mpu_ap_bits(env->cp15.pmsav5_data_ap);
18032bec
PM
3628}
3629
c4241c7d
PM
3630static void pmsav5_insn_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
3631 uint64_t value)
18032bec 3632{
7e09797c 3633 env->cp15.pmsav5_insn_ap = extended_mpu_ap_bits(value);
18032bec
PM
3634}
3635
c4241c7d 3636static uint64_t pmsav5_insn_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
18032bec 3637{
7e09797c 3638 return simple_mpu_ap_bits(env->cp15.pmsav5_insn_ap);
18032bec
PM
3639}
3640
6cb0b013
PC
3641static uint64_t pmsav7_read(CPUARMState *env, const ARMCPRegInfo *ri)
3642{
3643 uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
3644
3645 if (!u32p) {
3646 return 0;
3647 }
3648
1bc04a88 3649 u32p += env->pmsav7.rnr[M_REG_NS];
6cb0b013
PC
3650 return *u32p;
3651}
3652
3653static void pmsav7_write(CPUARMState *env, const ARMCPRegInfo *ri,
3654 uint64_t value)
3655{
2fc0cc0e 3656 ARMCPU *cpu = env_archcpu(env);
6cb0b013
PC
3657 uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
3658
3659 if (!u32p) {
3660 return;
3661 }
3662
1bc04a88 3663 u32p += env->pmsav7.rnr[M_REG_NS];
d10eb08f 3664 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
6cb0b013
PC
3665 *u32p = value;
3666}
3667
6cb0b013
PC
3668static void pmsav7_rgnr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3669 uint64_t value)
3670{
2fc0cc0e 3671 ARMCPU *cpu = env_archcpu(env);
6cb0b013
PC
3672 uint32_t nrgs = cpu->pmsav7_dregion;
3673
3674 if (value >= nrgs) {
3675 qemu_log_mask(LOG_GUEST_ERROR,
3676 "PMSAv7 RGNR write >= # supported regions, %" PRIu32
3677 " > %" PRIu32 "\n", (uint32_t)value, nrgs);
3678 return;
3679 }
3680
3681 raw_write(env, ri, value);
3682}
3683
3684static const ARMCPRegInfo pmsav7_cp_reginfo[] = {
69ceea64
PM
3685 /* Reset for all these registers is handled in arm_cpu_reset(),
3686 * because the PMSAv7 is also used by M-profile CPUs, which do
3687 * not register cpregs but still need the state to be reset.
3688 */
6cb0b013
PC
3689 { .name = "DRBAR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 0,
3690 .access = PL1_RW, .type = ARM_CP_NO_RAW,
3691 .fieldoffset = offsetof(CPUARMState, pmsav7.drbar),
69ceea64
PM
3692 .readfn = pmsav7_read, .writefn = pmsav7_write,
3693 .resetfn = arm_cp_reset_ignore },
6cb0b013
PC
3694 { .name = "DRSR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 2,
3695 .access = PL1_RW, .type = ARM_CP_NO_RAW,
3696 .fieldoffset = offsetof(CPUARMState, pmsav7.drsr),
69ceea64
PM
3697 .readfn = pmsav7_read, .writefn = pmsav7_write,
3698 .resetfn = arm_cp_reset_ignore },
6cb0b013
PC
3699 { .name = "DRACR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 4,
3700 .access = PL1_RW, .type = ARM_CP_NO_RAW,
3701 .fieldoffset = offsetof(CPUARMState, pmsav7.dracr),
69ceea64
PM
3702 .readfn = pmsav7_read, .writefn = pmsav7_write,
3703 .resetfn = arm_cp_reset_ignore },
6cb0b013
PC
3704 { .name = "RGNR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 2, .opc2 = 0,
3705 .access = PL1_RW,
1bc04a88 3706 .fieldoffset = offsetof(CPUARMState, pmsav7.rnr[M_REG_NS]),
69ceea64
PM
3707 .writefn = pmsav7_rgnr_write,
3708 .resetfn = arm_cp_reset_ignore },
6cb0b013
PC
3709 REGINFO_SENTINEL
3710};
3711
18032bec
PM
3712static const ARMCPRegInfo pmsav5_cp_reginfo[] = {
3713 { .name = "DATA_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
7a0e58fa 3714 .access = PL1_RW, .type = ARM_CP_ALIAS,
7e09797c 3715 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
18032bec
PM
3716 .readfn = pmsav5_data_ap_read, .writefn = pmsav5_data_ap_write, },
3717 { .name = "INSN_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
7a0e58fa 3718 .access = PL1_RW, .type = ARM_CP_ALIAS,
7e09797c 3719 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
18032bec
PM
3720 .readfn = pmsav5_insn_ap_read, .writefn = pmsav5_insn_ap_write, },
3721 { .name = "DATA_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 2,
3722 .access = PL1_RW,
7e09797c
PM
3723 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
3724 .resetvalue = 0, },
18032bec
PM
3725 { .name = "INSN_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 3,
3726 .access = PL1_RW,
7e09797c
PM
3727 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
3728 .resetvalue = 0, },
ecce5c3c
PM
3729 { .name = "DCACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
3730 .access = PL1_RW,
3731 .fieldoffset = offsetof(CPUARMState, cp15.c2_data), .resetvalue = 0, },
3732 { .name = "ICACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 1,
3733 .access = PL1_RW,
3734 .fieldoffset = offsetof(CPUARMState, cp15.c2_insn), .resetvalue = 0, },
06d76f31 3735 /* Protection region base and size registers */
e508a92b
PM
3736 { .name = "946_PRBS0", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0,
3737 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3738 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[0]) },
3739 { .name = "946_PRBS1", .cp = 15, .crn = 6, .crm = 1, .opc1 = 0,
3740 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3741 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[1]) },
3742 { .name = "946_PRBS2", .cp = 15, .crn = 6, .crm = 2, .opc1 = 0,
3743 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3744 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[2]) },
3745 { .name = "946_PRBS3", .cp = 15, .crn = 6, .crm = 3, .opc1 = 0,
3746 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3747 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[3]) },
3748 { .name = "946_PRBS4", .cp = 15, .crn = 6, .crm = 4, .opc1 = 0,
3749 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3750 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[4]) },
3751 { .name = "946_PRBS5", .cp = 15, .crn = 6, .crm = 5, .opc1 = 0,
3752 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3753 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[5]) },
3754 { .name = "946_PRBS6", .cp = 15, .crn = 6, .crm = 6, .opc1 = 0,
3755 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3756 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[6]) },
3757 { .name = "946_PRBS7", .cp = 15, .crn = 6, .crm = 7, .opc1 = 0,
3758 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3759 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[7]) },
18032bec
PM
3760 REGINFO_SENTINEL
3761};
3762
c4241c7d
PM
3763static void vmsa_ttbcr_raw_write(CPUARMState *env, const ARMCPRegInfo *ri,
3764 uint64_t value)
ecce5c3c 3765{
11f136ee 3766 TCR *tcr = raw_ptr(env, ri);
2ebcebe2
PM
3767 int maskshift = extract32(value, 0, 3);
3768
e389be16
FA
3769 if (!arm_feature(env, ARM_FEATURE_V8)) {
3770 if (arm_feature(env, ARM_FEATURE_LPAE) && (value & TTBCR_EAE)) {
3771 /* Pre ARMv8 bits [21:19], [15:14] and [6:3] are UNK/SBZP when
3772 * using Long-desciptor translation table format */
3773 value &= ~((7 << 19) | (3 << 14) | (0xf << 3));
3774 } else if (arm_feature(env, ARM_FEATURE_EL3)) {
3775 /* In an implementation that includes the Security Extensions
3776 * TTBCR has additional fields PD0 [4] and PD1 [5] for
3777 * Short-descriptor translation table format.
3778 */
3779 value &= TTBCR_PD1 | TTBCR_PD0 | TTBCR_N;
3780 } else {
3781 value &= TTBCR_N;
3782 }
e42c4db3 3783 }
e389be16 3784
b6af0975 3785 /* Update the masks corresponding to the TCR bank being written
11f136ee 3786 * Note that we always calculate mask and base_mask, but
e42c4db3 3787 * they are only used for short-descriptor tables (ie if EAE is 0);
11f136ee
FA
3788 * for long-descriptor tables the TCR fields are used differently
3789 * and the mask and base_mask values are meaningless.
e42c4db3 3790 */
11f136ee
FA
3791 tcr->raw_tcr = value;
3792 tcr->mask = ~(((uint32_t)0xffffffffu) >> maskshift);
3793 tcr->base_mask = ~((uint32_t)0x3fffu >> maskshift);
ecce5c3c
PM
3794}
3795
c4241c7d
PM
3796static void vmsa_ttbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3797 uint64_t value)
d4e6df63 3798{
2fc0cc0e 3799 ARMCPU *cpu = env_archcpu(env);
ab638a32 3800 TCR *tcr = raw_ptr(env, ri);
00c8cb0a 3801
d4e6df63
PM
3802 if (arm_feature(env, ARM_FEATURE_LPAE)) {
3803 /* With LPAE the TTBCR could result in a change of ASID
3804 * via the TTBCR.A1 bit, so do a TLB flush.
3805 */
d10eb08f 3806 tlb_flush(CPU(cpu));
d4e6df63 3807 }
ab638a32
RH
3808 /* Preserve the high half of TCR_EL1, set via TTBCR2. */
3809 value = deposit64(tcr->raw_tcr, 0, 32, value);
c4241c7d 3810 vmsa_ttbcr_raw_write(env, ri, value);
d4e6df63
PM
3811}
3812
ecce5c3c
PM
3813static void vmsa_ttbcr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
3814{
11f136ee
FA
3815 TCR *tcr = raw_ptr(env, ri);
3816
3817 /* Reset both the TCR as well as the masks corresponding to the bank of
3818 * the TCR being reset.
3819 */
3820 tcr->raw_tcr = 0;
3821 tcr->mask = 0;
3822 tcr->base_mask = 0xffffc000u;
ecce5c3c
PM
3823}
3824
d06dc933 3825static void vmsa_tcr_el12_write(CPUARMState *env, const ARMCPRegInfo *ri,
cb2e37df
PM
3826 uint64_t value)
3827{
2fc0cc0e 3828 ARMCPU *cpu = env_archcpu(env);
11f136ee 3829 TCR *tcr = raw_ptr(env, ri);
00c8cb0a 3830
cb2e37df 3831 /* For AArch64 the A1 bit could result in a change of ASID, so TLB flush. */
d10eb08f 3832 tlb_flush(CPU(cpu));
11f136ee 3833 tcr->raw_tcr = value;
cb2e37df
PM
3834}
3835
327ed10f
PM
3836static void vmsa_ttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3837 uint64_t value)
3838{
93f379b0
RH
3839 /* If the ASID changes (with a 64-bit write), we must flush the TLB. */
3840 if (cpreg_field_is_64bit(ri) &&
3841 extract64(raw_read(env, ri) ^ value, 48, 16) != 0) {
2fc0cc0e 3842 ARMCPU *cpu = env_archcpu(env);
d10eb08f 3843 tlb_flush(CPU(cpu));
327ed10f
PM
3844 }
3845 raw_write(env, ri, value);
3846}
3847
ed30da8e
RH
3848static void vmsa_tcr_ttbr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
3849 uint64_t value)
3850{
d06dc933
RH
3851 /*
3852 * If we are running with E2&0 regime, then an ASID is active.
3853 * Flush if that might be changing. Note we're not checking
3854 * TCR_EL2.A1 to know if this is really the TTBRx_EL2 that
3855 * holds the active ASID, only checking the field that might.
3856 */
3857 if (extract64(raw_read(env, ri) ^ value, 48, 16) &&
3858 (arm_hcr_el2_eff(env) & HCR_E2H)) {
3859 tlb_flush_by_mmuidx(env_cpu(env),
452ef8cb
RH
3860 ARMMMUIdxBit_E20_2 |
3861 ARMMMUIdxBit_E20_2_PAN |
3862 ARMMMUIdxBit_E20_0);
d06dc933 3863 }
ed30da8e
RH
3864 raw_write(env, ri, value);
3865}
3866
b698e9cf
EI
3867static void vttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3868 uint64_t value)
3869{
2fc0cc0e 3870 ARMCPU *cpu = env_archcpu(env);
b698e9cf
EI
3871 CPUState *cs = CPU(cpu);
3872
97fa9350
RH
3873 /*
3874 * A change in VMID to the stage2 page table (Stage2) invalidates
3875 * the combined stage 1&2 tlbs (EL10_1 and EL10_0).
3876 */
b698e9cf 3877 if (raw_read(env, ri) != value) {
0336cbf8 3878 tlb_flush_by_mmuidx(cs,
01b98b68 3879 ARMMMUIdxBit_E10_1 |
452ef8cb 3880 ARMMMUIdxBit_E10_1_PAN |
01b98b68 3881 ARMMMUIdxBit_E10_0 |
97fa9350 3882 ARMMMUIdxBit_Stage2);
b698e9cf
EI
3883 raw_write(env, ri, value);
3884 }
3885}
3886
8e5d75c9 3887static const ARMCPRegInfo vmsa_pmsa_cp_reginfo[] = {
18032bec 3888 { .name = "DFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
7a0e58fa 3889 .access = PL1_RW, .type = ARM_CP_ALIAS,
4a7e2d73 3890 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dfsr_s),
b061a82b 3891 offsetoflow32(CPUARMState, cp15.dfsr_ns) }, },
18032bec 3892 { .name = "IFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
88ca1c2d
FA
3893 .access = PL1_RW, .resetvalue = 0,
3894 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.ifsr_s),
3895 offsetoflow32(CPUARMState, cp15.ifsr_ns) } },
8e5d75c9
PC
3896 { .name = "DFAR", .cp = 15, .opc1 = 0, .crn = 6, .crm = 0, .opc2 = 0,
3897 .access = PL1_RW, .resetvalue = 0,
3898 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.dfar_s),
3899 offsetof(CPUARMState, cp15.dfar_ns) } },
3900 { .name = "FAR_EL1", .state = ARM_CP_STATE_AA64,
3901 .opc0 = 3, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 0,
3902 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[1]),
3903 .resetvalue = 0, },
3904 REGINFO_SENTINEL
3905};
3906
3907static const ARMCPRegInfo vmsa_cp_reginfo[] = {
6cd8a264
RH
3908 { .name = "ESR_EL1", .state = ARM_CP_STATE_AA64,
3909 .opc0 = 3, .crn = 5, .crm = 2, .opc1 = 0, .opc2 = 0,
3910 .access = PL1_RW,
d81c519c 3911 .fieldoffset = offsetof(CPUARMState, cp15.esr_el[1]), .resetvalue = 0, },
327ed10f 3912 { .name = "TTBR0_EL1", .state = ARM_CP_STATE_BOTH,
7dd8c9af
FA
3913 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 0,
3914 .access = PL1_RW, .writefn = vmsa_ttbr_write, .resetvalue = 0,
3915 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
3916 offsetof(CPUARMState, cp15.ttbr0_ns) } },
327ed10f 3917 { .name = "TTBR1_EL1", .state = ARM_CP_STATE_BOTH,
7dd8c9af
FA
3918 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 1,
3919 .access = PL1_RW, .writefn = vmsa_ttbr_write, .resetvalue = 0,
3920 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
3921 offsetof(CPUARMState, cp15.ttbr1_ns) } },
cb2e37df
PM
3922 { .name = "TCR_EL1", .state = ARM_CP_STATE_AA64,
3923 .opc0 = 3, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
d06dc933 3924 .access = PL1_RW, .writefn = vmsa_tcr_el12_write,
cb2e37df 3925 .resetfn = vmsa_ttbcr_reset, .raw_writefn = raw_write,
11f136ee 3926 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[1]) },
cb2e37df 3927 { .name = "TTBCR", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
7a0e58fa 3928 .access = PL1_RW, .type = ARM_CP_ALIAS, .writefn = vmsa_ttbcr_write,
b061a82b 3929 .raw_writefn = vmsa_ttbcr_raw_write,
11f136ee
FA
3930 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tcr_el[3]),
3931 offsetoflow32(CPUARMState, cp15.tcr_el[1])} },
18032bec
PM
3932 REGINFO_SENTINEL
3933};
3934
ab638a32
RH
3935/* Note that unlike TTBCR, writing to TTBCR2 does not require flushing
3936 * qemu tlbs nor adjusting cached masks.
3937 */
3938static const ARMCPRegInfo ttbcr2_reginfo = {
3939 .name = "TTBCR2", .cp = 15, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 3,
3940 .access = PL1_RW, .type = ARM_CP_ALIAS,
3941 .bank_fieldoffsets = { offsetofhigh32(CPUARMState, cp15.tcr_el[3]),
3942 offsetofhigh32(CPUARMState, cp15.tcr_el[1]) },
3943};
3944
c4241c7d
PM
3945static void omap_ticonfig_write(CPUARMState *env, const ARMCPRegInfo *ri,
3946 uint64_t value)
1047b9d7
PM
3947{
3948 env->cp15.c15_ticonfig = value & 0xe7;
3949 /* The OS_TYPE bit in this register changes the reported CPUID! */
3950 env->cp15.c0_cpuid = (value & (1 << 5)) ?
3951 ARM_CPUID_TI915T : ARM_CPUID_TI925T;
1047b9d7
PM
3952}
3953
c4241c7d
PM
3954static void omap_threadid_write(CPUARMState *env, const ARMCPRegInfo *ri,
3955 uint64_t value)
1047b9d7
PM
3956{
3957 env->cp15.c15_threadid = value & 0xffff;
1047b9d7
PM
3958}
3959
c4241c7d
PM
3960static void omap_wfi_write(CPUARMState *env, const ARMCPRegInfo *ri,
3961 uint64_t value)
1047b9d7
PM
3962{
3963 /* Wait-for-interrupt (deprecated) */
2fc0cc0e 3964 cpu_interrupt(env_cpu(env), CPU_INTERRUPT_HALT);
1047b9d7
PM
3965}
3966
c4241c7d
PM
3967static void omap_cachemaint_write(CPUARMState *env, const ARMCPRegInfo *ri,
3968 uint64_t value)
c4804214
PM
3969{
3970 /* On OMAP there are registers indicating the max/min index of dcache lines
3971 * containing a dirty line; cache flush operations have to reset these.
3972 */
3973 env->cp15.c15_i_max = 0x000;
3974 env->cp15.c15_i_min = 0xff0;
c4804214
PM
3975}
3976
18032bec
PM
3977static const ARMCPRegInfo omap_cp_reginfo[] = {
3978 { .name = "DFSR", .cp = 15, .crn = 5, .crm = CP_ANY,
3979 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_OVERRIDE,
d81c519c 3980 .fieldoffset = offsetoflow32(CPUARMState, cp15.esr_el[1]),
6cd8a264 3981 .resetvalue = 0, },
1047b9d7
PM
3982 { .name = "", .cp = 15, .crn = 15, .crm = 0, .opc1 = 0, .opc2 = 0,
3983 .access = PL1_RW, .type = ARM_CP_NOP },
3984 { .name = "TICONFIG", .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0,
3985 .access = PL1_RW,
3986 .fieldoffset = offsetof(CPUARMState, cp15.c15_ticonfig), .resetvalue = 0,
3987 .writefn = omap_ticonfig_write },
3988 { .name = "IMAX", .cp = 15, .crn = 15, .crm = 2, .opc1 = 0, .opc2 = 0,
3989 .access = PL1_RW,
3990 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_max), .resetvalue = 0, },
3991 { .name = "IMIN", .cp = 15, .crn = 15, .crm = 3, .opc1 = 0, .opc2 = 0,
3992 .access = PL1_RW, .resetvalue = 0xff0,
3993 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_min) },
3994 { .name = "THREADID", .cp = 15, .crn = 15, .crm = 4, .opc1 = 0, .opc2 = 0,
3995 .access = PL1_RW,
3996 .fieldoffset = offsetof(CPUARMState, cp15.c15_threadid), .resetvalue = 0,
3997 .writefn = omap_threadid_write },
3998 { .name = "TI925T_STATUS", .cp = 15, .crn = 15,
3999 .crm = 8, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
7a0e58fa 4000 .type = ARM_CP_NO_RAW,
1047b9d7
PM
4001 .readfn = arm_cp_read_zero, .writefn = omap_wfi_write, },
4002 /* TODO: Peripheral port remap register:
4003 * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt controller
4004 * base address at $rn & ~0xfff and map size of 0x200 << ($rn & 0xfff),
4005 * when MMU is off.
4006 */
c4804214 4007 { .name = "OMAP_CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
d4e6df63 4008 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
7a0e58fa 4009 .type = ARM_CP_OVERRIDE | ARM_CP_NO_RAW,
c4804214 4010 .writefn = omap_cachemaint_write },
34f90529
PM
4011 { .name = "C9", .cp = 15, .crn = 9,
4012 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW,
4013 .type = ARM_CP_CONST | ARM_CP_OVERRIDE, .resetvalue = 0 },
1047b9d7
PM
4014 REGINFO_SENTINEL
4015};
4016
c4241c7d
PM
4017static void xscale_cpar_write(CPUARMState *env, const ARMCPRegInfo *ri,
4018 uint64_t value)
1047b9d7 4019{
c0f4af17 4020 env->cp15.c15_cpar = value & 0x3fff;
1047b9d7
PM
4021}
4022
4023static const ARMCPRegInfo xscale_cp_reginfo[] = {
4024 { .name = "XSCALE_CPAR",
4025 .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
4026 .fieldoffset = offsetof(CPUARMState, cp15.c15_cpar), .resetvalue = 0,
4027 .writefn = xscale_cpar_write, },
2771db27
PM
4028 { .name = "XSCALE_AUXCR",
4029 .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 1, .access = PL1_RW,
4030 .fieldoffset = offsetof(CPUARMState, cp15.c1_xscaleauxcr),
4031 .resetvalue = 0, },
3b771579
PM
4032 /* XScale specific cache-lockdown: since we have no cache we NOP these
4033 * and hope the guest does not really rely on cache behaviour.
4034 */
4035 { .name = "XSCALE_LOCK_ICACHE_LINE",
4036 .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 0,
4037 .access = PL1_W, .type = ARM_CP_NOP },
4038 { .name = "XSCALE_UNLOCK_ICACHE",
4039 .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 1,
4040 .access = PL1_W, .type = ARM_CP_NOP },
4041 { .name = "XSCALE_DCACHE_LOCK",
4042 .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 0,
4043 .access = PL1_RW, .type = ARM_CP_NOP },
4044 { .name = "XSCALE_UNLOCK_DCACHE",
4045 .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 1,
4046 .access = PL1_W, .type = ARM_CP_NOP },
1047b9d7
PM
4047 REGINFO_SENTINEL
4048};
4049
4050static const ARMCPRegInfo dummy_c15_cp_reginfo[] = {
4051 /* RAZ/WI the whole crn=15 space, when we don't have a more specific
4052 * implementation of this implementation-defined space.
4053 * Ideally this should eventually disappear in favour of actually
4054 * implementing the correct behaviour for all cores.
4055 */
4056 { .name = "C15_IMPDEF", .cp = 15, .crn = 15,
4057 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
3671cd87 4058 .access = PL1_RW,
7a0e58fa 4059 .type = ARM_CP_CONST | ARM_CP_NO_RAW | ARM_CP_OVERRIDE,
d4e6df63 4060 .resetvalue = 0 },
18032bec
PM
4061 REGINFO_SENTINEL
4062};
4063
c4804214
PM
4064static const ARMCPRegInfo cache_dirty_status_cp_reginfo[] = {
4065 /* Cache status: RAZ because we have no cache so it's always clean */
4066 { .name = "CDSR", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 6,
7a0e58fa 4067 .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 4068 .resetvalue = 0 },
c4804214
PM
4069 REGINFO_SENTINEL
4070};
4071
4072static const ARMCPRegInfo cache_block_ops_cp_reginfo[] = {
4073 /* We never have a a block transfer operation in progress */
4074 { .name = "BXSR", .cp = 15, .crn = 7, .crm = 12, .opc1 = 0, .opc2 = 4,
7a0e58fa 4075 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 4076 .resetvalue = 0 },
30b05bba
PM
4077 /* The cache ops themselves: these all NOP for QEMU */
4078 { .name = "IICR", .cp = 15, .crm = 5, .opc1 = 0,
4079 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
4080 { .name = "IDCR", .cp = 15, .crm = 6, .opc1 = 0,
4081 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
4082 { .name = "CDCR", .cp = 15, .crm = 12, .opc1 = 0,
4083 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
4084 { .name = "PIR", .cp = 15, .crm = 12, .opc1 = 1,
4085 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
4086 { .name = "PDR", .cp = 15, .crm = 12, .opc1 = 2,
4087 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
4088 { .name = "CIDCR", .cp = 15, .crm = 14, .opc1 = 0,
4089 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
c4804214
PM
4090 REGINFO_SENTINEL
4091};
4092
4093static const ARMCPRegInfo cache_test_clean_cp_reginfo[] = {
4094 /* The cache test-and-clean instructions always return (1 << 30)
4095 * to indicate that there are no dirty cache lines.
4096 */
4097 { .name = "TC_DCACHE", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 3,
7a0e58fa 4098 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 4099 .resetvalue = (1 << 30) },
c4804214 4100 { .name = "TCI_DCACHE", .cp = 15, .crn = 7, .crm = 14, .opc1 = 0, .opc2 = 3,
7a0e58fa 4101 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 4102 .resetvalue = (1 << 30) },
c4804214
PM
4103 REGINFO_SENTINEL
4104};
4105
34f90529
PM
4106static const ARMCPRegInfo strongarm_cp_reginfo[] = {
4107 /* Ignore ReadBuffer accesses */
4108 { .name = "C9_READBUFFER", .cp = 15, .crn = 9,
4109 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
d4e6df63 4110 .access = PL1_RW, .resetvalue = 0,
7a0e58fa 4111 .type = ARM_CP_CONST | ARM_CP_OVERRIDE | ARM_CP_NO_RAW },
34f90529
PM
4112 REGINFO_SENTINEL
4113};
4114
731de9e6
EI
4115static uint64_t midr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4116{
2fc0cc0e 4117 ARMCPU *cpu = env_archcpu(env);
731de9e6
EI
4118 unsigned int cur_el = arm_current_el(env);
4119 bool secure = arm_is_secure(env);
4120
4121 if (arm_feature(&cpu->env, ARM_FEATURE_EL2) && !secure && cur_el == 1) {
4122 return env->cp15.vpidr_el2;
4123 }
4124 return raw_read(env, ri);
4125}
4126
06a7e647 4127static uint64_t mpidr_read_val(CPUARMState *env)
81bdde9d 4128{
2fc0cc0e 4129 ARMCPU *cpu = env_archcpu(env);
eb5e1d3c
PF
4130 uint64_t mpidr = cpu->mp_affinity;
4131
81bdde9d 4132 if (arm_feature(env, ARM_FEATURE_V7MP)) {
78dbbbe4 4133 mpidr |= (1U << 31);
81bdde9d
PM
4134 /* Cores which are uniprocessor (non-coherent)
4135 * but still implement the MP extensions set
a8e81b31 4136 * bit 30. (For instance, Cortex-R5).
81bdde9d 4137 */
a8e81b31
PC
4138 if (cpu->mp_is_up) {
4139 mpidr |= (1u << 30);
4140 }
81bdde9d 4141 }
c4241c7d 4142 return mpidr;
81bdde9d
PM
4143}
4144
06a7e647
EI
4145static uint64_t mpidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4146{
f0d574d6
EI
4147 unsigned int cur_el = arm_current_el(env);
4148 bool secure = arm_is_secure(env);
4149
4150 if (arm_feature(env, ARM_FEATURE_EL2) && !secure && cur_el == 1) {
4151 return env->cp15.vmpidr_el2;
4152 }
06a7e647
EI
4153 return mpidr_read_val(env);
4154}
4155
7ac681cf 4156static const ARMCPRegInfo lpae_cp_reginfo[] = {
a903c449 4157 /* NOP AMAIR0/1 */
b0fe2427
PM
4158 { .name = "AMAIR0", .state = ARM_CP_STATE_BOTH,
4159 .opc0 = 3, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 0,
a903c449 4160 .access = PL1_RW, .type = ARM_CP_CONST,
7ac681cf 4161 .resetvalue = 0 },
b0fe2427 4162 /* AMAIR1 is mapped to AMAIR_EL1[63:32] */
7ac681cf 4163 { .name = "AMAIR1", .cp = 15, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 1,
a903c449 4164 .access = PL1_RW, .type = ARM_CP_CONST,
7ac681cf 4165 .resetvalue = 0 },
891a2fe7 4166 { .name = "PAR", .cp = 15, .crm = 7, .opc1 = 0,
01c097f7
FA
4167 .access = PL1_RW, .type = ARM_CP_64BIT, .resetvalue = 0,
4168 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.par_s),
4169 offsetof(CPUARMState, cp15.par_ns)} },
891a2fe7 4170 { .name = "TTBR0", .cp = 15, .crm = 2, .opc1 = 0,
7a0e58fa 4171 .access = PL1_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
7dd8c9af
FA
4172 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
4173 offsetof(CPUARMState, cp15.ttbr0_ns) },
b061a82b 4174 .writefn = vmsa_ttbr_write, },
891a2fe7 4175 { .name = "TTBR1", .cp = 15, .crm = 2, .opc1 = 1,
7a0e58fa 4176 .access = PL1_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
7dd8c9af
FA
4177 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
4178 offsetof(CPUARMState, cp15.ttbr1_ns) },
b061a82b 4179 .writefn = vmsa_ttbr_write, },
7ac681cf
PM
4180 REGINFO_SENTINEL
4181};
4182
c4241c7d 4183static uint64_t aa64_fpcr_read(CPUARMState *env, const ARMCPRegInfo *ri)
b0d2b7d0 4184{
c4241c7d 4185 return vfp_get_fpcr(env);
b0d2b7d0
PM
4186}
4187
c4241c7d
PM
4188static void aa64_fpcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4189 uint64_t value)
b0d2b7d0
PM
4190{
4191 vfp_set_fpcr(env, value);
b0d2b7d0
PM
4192}
4193
c4241c7d 4194static uint64_t aa64_fpsr_read(CPUARMState *env, const ARMCPRegInfo *ri)
b0d2b7d0 4195{
c4241c7d 4196 return vfp_get_fpsr(env);
b0d2b7d0
PM
4197}
4198
c4241c7d
PM
4199static void aa64_fpsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4200 uint64_t value)
b0d2b7d0
PM
4201{
4202 vfp_set_fpsr(env, value);
b0d2b7d0
PM
4203}
4204
3f208fd7
PM
4205static CPAccessResult aa64_daif_access(CPUARMState *env, const ARMCPRegInfo *ri,
4206 bool isread)
c2b820fe 4207{
aaec1432 4208 if (arm_current_el(env) == 0 && !(arm_sctlr(env, 0) & SCTLR_UMA)) {
c2b820fe
PM
4209 return CP_ACCESS_TRAP;
4210 }
4211 return CP_ACCESS_OK;
4212}
4213
4214static void aa64_daif_write(CPUARMState *env, const ARMCPRegInfo *ri,
4215 uint64_t value)
4216{
4217 env->daif = value & PSTATE_DAIF;
4218}
4219
220f508f
RH
4220static uint64_t aa64_pan_read(CPUARMState *env, const ARMCPRegInfo *ri)
4221{
4222 return env->pstate & PSTATE_PAN;
4223}
4224
4225static void aa64_pan_write(CPUARMState *env, const ARMCPRegInfo *ri,
4226 uint64_t value)
4227{
4228 env->pstate = (env->pstate & ~PSTATE_PAN) | (value & PSTATE_PAN);
4229}
4230
4231static const ARMCPRegInfo pan_reginfo = {
4232 .name = "PAN", .state = ARM_CP_STATE_AA64,
4233 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 3,
4234 .type = ARM_CP_NO_RAW, .access = PL1_RW,
4235 .readfn = aa64_pan_read, .writefn = aa64_pan_write
4236};
4237
9eeb7a1c
RH
4238static uint64_t aa64_uao_read(CPUARMState *env, const ARMCPRegInfo *ri)
4239{
4240 return env->pstate & PSTATE_UAO;
4241}
4242
4243static void aa64_uao_write(CPUARMState *env, const ARMCPRegInfo *ri,
4244 uint64_t value)
4245{
4246 env->pstate = (env->pstate & ~PSTATE_UAO) | (value & PSTATE_UAO);
4247}
4248
4249static const ARMCPRegInfo uao_reginfo = {
4250 .name = "UAO", .state = ARM_CP_STATE_AA64,
4251 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 4,
4252 .type = ARM_CP_NO_RAW, .access = PL1_RW,
4253 .readfn = aa64_uao_read, .writefn = aa64_uao_write
4254};
4255
8af35c37 4256static CPAccessResult aa64_cacheop_access(CPUARMState *env,
3f208fd7
PM
4257 const ARMCPRegInfo *ri,
4258 bool isread)
8af35c37
PM
4259{
4260 /* Cache invalidate/clean: NOP, but EL0 must UNDEF unless
4261 * SCTLR_EL1.UCI is set.
4262 */
aaec1432 4263 if (arm_current_el(env) == 0 && !(arm_sctlr(env, 0) & SCTLR_UCI)) {
8af35c37
PM
4264 return CP_ACCESS_TRAP;
4265 }
4266 return CP_ACCESS_OK;
4267}
4268
dbb1fb27
AB
4269/* See: D4.7.2 TLB maintenance requirements and the TLB maintenance instructions
4270 * Page D4-1736 (DDI0487A.b)
4271 */
4272
b7e0730d
RH
4273static int vae1_tlbmask(CPUARMState *env)
4274{
85d0dc9f 4275 /* Since we exclude secure first, we may read HCR_EL2 directly. */
b7e0730d 4276 if (arm_is_secure_below_el3(env)) {
452ef8cb
RH
4277 return ARMMMUIdxBit_SE10_1 |
4278 ARMMMUIdxBit_SE10_1_PAN |
4279 ARMMMUIdxBit_SE10_0;
85d0dc9f
RH
4280 } else if ((env->cp15.hcr_el2 & (HCR_E2H | HCR_TGE))
4281 == (HCR_E2H | HCR_TGE)) {
452ef8cb
RH
4282 return ARMMMUIdxBit_E20_2 |
4283 ARMMMUIdxBit_E20_2_PAN |
4284 ARMMMUIdxBit_E20_0;
b7e0730d 4285 } else {
452ef8cb
RH
4286 return ARMMMUIdxBit_E10_1 |
4287 ARMMMUIdxBit_E10_1_PAN |
4288 ARMMMUIdxBit_E10_0;
b7e0730d
RH
4289 }
4290}
4291
fd3ed969
PM
4292static void tlbi_aa64_vmalle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4293 uint64_t value)
168aa23b 4294{
29a0af61 4295 CPUState *cs = env_cpu(env);
b7e0730d 4296 int mask = vae1_tlbmask(env);
dbb1fb27 4297
b7e0730d 4298 tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
168aa23b
PM
4299}
4300
b4ab8ce9
PM
4301static void tlbi_aa64_vmalle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4302 uint64_t value)
4303{
29a0af61 4304 CPUState *cs = env_cpu(env);
b7e0730d 4305 int mask = vae1_tlbmask(env);
b4ab8ce9
PM
4306
4307 if (tlb_force_broadcast(env)) {
527db2be
RH
4308 tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
4309 } else {
4310 tlb_flush_by_mmuidx(cs, mask);
b4ab8ce9 4311 }
b4ab8ce9
PM
4312}
4313
90c19cdf 4314static int alle1_tlbmask(CPUARMState *env)
168aa23b 4315{
90c19cdf
RH
4316 /*
4317 * Note that the 'ALL' scope must invalidate both stage 1 and
fd3ed969
PM
4318 * stage 2 translations, whereas most other scopes only invalidate
4319 * stage 1 translations.
4320 */
fd3ed969 4321 if (arm_is_secure_below_el3(env)) {
452ef8cb
RH
4322 return ARMMMUIdxBit_SE10_1 |
4323 ARMMMUIdxBit_SE10_1_PAN |
4324 ARMMMUIdxBit_SE10_0;
90c19cdf 4325 } else if (arm_feature(env, ARM_FEATURE_EL2)) {
452ef8cb
RH
4326 return ARMMMUIdxBit_E10_1 |
4327 ARMMMUIdxBit_E10_1_PAN |
4328 ARMMMUIdxBit_E10_0 |
4329 ARMMMUIdxBit_Stage2;
fd3ed969 4330 } else {
452ef8cb
RH
4331 return ARMMMUIdxBit_E10_1 |
4332 ARMMMUIdxBit_E10_1_PAN |
4333 ARMMMUIdxBit_E10_0;
fd3ed969 4334 }
168aa23b
PM
4335}
4336
85d0dc9f
RH
4337static int e2_tlbmask(CPUARMState *env)
4338{
4339 /* TODO: ARMv8.4-SecEL2 */
452ef8cb
RH
4340 return ARMMMUIdxBit_E20_0 |
4341 ARMMMUIdxBit_E20_2 |
4342 ARMMMUIdxBit_E20_2_PAN |
4343 ARMMMUIdxBit_E2;
85d0dc9f
RH
4344}
4345
90c19cdf
RH
4346static void tlbi_aa64_alle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4347 uint64_t value)
4348{
4349 CPUState *cs = env_cpu(env);
4350 int mask = alle1_tlbmask(env);
4351
4352 tlb_flush_by_mmuidx(cs, mask);
4353}
4354
fd3ed969 4355static void tlbi_aa64_alle2_write(CPUARMState *env, const ARMCPRegInfo *ri,
fa439fc5
PM
4356 uint64_t value)
4357{
85d0dc9f
RH
4358 CPUState *cs = env_cpu(env);
4359 int mask = e2_tlbmask(env);
fd3ed969 4360
85d0dc9f 4361 tlb_flush_by_mmuidx(cs, mask);
fd3ed969
PM
4362}
4363
43efaa33
PM
4364static void tlbi_aa64_alle3_write(CPUARMState *env, const ARMCPRegInfo *ri,
4365 uint64_t value)
4366{
2fc0cc0e 4367 ARMCPU *cpu = env_archcpu(env);
43efaa33
PM
4368 CPUState *cs = CPU(cpu);
4369
127b2b08 4370 tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_SE3);
43efaa33
PM
4371}
4372
fd3ed969
PM
4373static void tlbi_aa64_alle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4374 uint64_t value)
4375{
29a0af61 4376 CPUState *cs = env_cpu(env);
90c19cdf
RH
4377 int mask = alle1_tlbmask(env);
4378
4379 tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
fa439fc5
PM
4380}
4381
2bfb9d75
PM
4382static void tlbi_aa64_alle2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4383 uint64_t value)
4384{
29a0af61 4385 CPUState *cs = env_cpu(env);
85d0dc9f 4386 int mask = e2_tlbmask(env);
2bfb9d75 4387
85d0dc9f 4388 tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
2bfb9d75
PM
4389}
4390
43efaa33
PM
4391static void tlbi_aa64_alle3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4392 uint64_t value)
4393{
29a0af61 4394 CPUState *cs = env_cpu(env);
43efaa33 4395
127b2b08 4396 tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_SE3);
43efaa33
PM
4397}
4398
fd3ed969
PM
4399static void tlbi_aa64_vae2_write(CPUARMState *env, const ARMCPRegInfo *ri,
4400 uint64_t value)
fa439fc5 4401{
fd3ed969
PM
4402 /* Invalidate by VA, EL2
4403 * Currently handles both VAE2 and VALE2, since we don't support
4404 * flush-last-level-only.
4405 */
85d0dc9f
RH
4406 CPUState *cs = env_cpu(env);
4407 int mask = e2_tlbmask(env);
fd3ed969
PM
4408 uint64_t pageaddr = sextract64(value << 12, 0, 56);
4409
85d0dc9f 4410 tlb_flush_page_by_mmuidx(cs, pageaddr, mask);
fd3ed969
PM
4411}
4412
43efaa33
PM
4413static void tlbi_aa64_vae3_write(CPUARMState *env, const ARMCPRegInfo *ri,
4414 uint64_t value)
4415{
4416 /* Invalidate by VA, EL3
4417 * Currently handles both VAE3 and VALE3, since we don't support
4418 * flush-last-level-only.
4419 */
2fc0cc0e 4420 ARMCPU *cpu = env_archcpu(env);
43efaa33
PM
4421 CPUState *cs = CPU(cpu);
4422 uint64_t pageaddr = sextract64(value << 12, 0, 56);
4423
127b2b08 4424 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_SE3);
43efaa33
PM
4425}
4426
fd3ed969
PM
4427static void tlbi_aa64_vae1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4428 uint64_t value)
4429{
90c19cdf
RH
4430 CPUState *cs = env_cpu(env);
4431 int mask = vae1_tlbmask(env);
fa439fc5
PM
4432 uint64_t pageaddr = sextract64(value << 12, 0, 56);
4433
90c19cdf 4434 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr, mask);
fa439fc5
PM
4435}
4436
b4ab8ce9
PM
4437static void tlbi_aa64_vae1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4438 uint64_t value)
4439{
4440 /* Invalidate by VA, EL1&0 (AArch64 version).
4441 * Currently handles all of VAE1, VAAE1, VAALE1 and VALE1,
4442 * since we don't support flush-for-specific-ASID-only or
4443 * flush-last-level-only.
4444 */
90c19cdf
RH
4445 CPUState *cs = env_cpu(env);
4446 int mask = vae1_tlbmask(env);
b4ab8ce9
PM
4447 uint64_t pageaddr = sextract64(value << 12, 0, 56);
4448
4449 if (tlb_force_broadcast(env)) {
527db2be
RH
4450 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr, mask);
4451 } else {
4452 tlb_flush_page_by_mmuidx(cs, pageaddr, mask);
b4ab8ce9 4453 }
b4ab8ce9
PM
4454}
4455
fd3ed969
PM
4456static void tlbi_aa64_vae2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4457 uint64_t value)
fa439fc5 4458{
29a0af61 4459 CPUState *cs = env_cpu(env);
fd3ed969 4460 uint64_t pageaddr = sextract64(value << 12, 0, 56);
fa439fc5 4461
a67cf277 4462 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
e013b741 4463 ARMMMUIdxBit_E2);
fa439fc5
PM
4464}
4465
43efaa33
PM
4466static void tlbi_aa64_vae3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4467 uint64_t value)
4468{
29a0af61 4469 CPUState *cs = env_cpu(env);
43efaa33
PM
4470 uint64_t pageaddr = sextract64(value << 12, 0, 56);
4471
a67cf277 4472 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
127b2b08 4473 ARMMMUIdxBit_SE3);
43efaa33
PM
4474}
4475
cea66e91
PM
4476static void tlbi_aa64_ipas2e1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4477 uint64_t value)
4478{
4479 /* Invalidate by IPA. This has to invalidate any structures that
4480 * contain only stage 2 translation information, but does not need
4481 * to apply to structures that contain combined stage 1 and stage 2
4482 * translation information.
4483 * This must NOP if EL2 isn't implemented or SCR_EL3.NS is zero.
4484 */
2fc0cc0e 4485 ARMCPU *cpu = env_archcpu(env);
cea66e91
PM
4486 CPUState *cs = CPU(cpu);
4487 uint64_t pageaddr;
4488
4489 if (!arm_feature(env, ARM_FEATURE_EL2) || !(env->cp15.scr_el3 & SCR_NS)) {
4490 return;
4491 }
4492
4493 pageaddr = sextract64(value << 12, 0, 48);
4494
97fa9350 4495 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_Stage2);
cea66e91
PM
4496}
4497
4498static void tlbi_aa64_ipas2e1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4499 uint64_t value)
4500{
29a0af61 4501 CPUState *cs = env_cpu(env);
cea66e91
PM
4502 uint64_t pageaddr;
4503
4504 if (!arm_feature(env, ARM_FEATURE_EL2) || !(env->cp15.scr_el3 & SCR_NS)) {
4505 return;
4506 }
4507
4508 pageaddr = sextract64(value << 12, 0, 48);
4509
a67cf277 4510 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
97fa9350 4511 ARMMMUIdxBit_Stage2);
cea66e91
PM
4512}
4513
3f208fd7
PM
4514static CPAccessResult aa64_zva_access(CPUARMState *env, const ARMCPRegInfo *ri,
4515 bool isread)
aca3f40b 4516{
4351cb72
RH
4517 int cur_el = arm_current_el(env);
4518
4519 if (cur_el < 2) {
4520 uint64_t hcr = arm_hcr_el2_eff(env);
4521
4522 if (cur_el == 0) {
4523 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
4524 if (!(env->cp15.sctlr_el[2] & SCTLR_DZE)) {
4525 return CP_ACCESS_TRAP_EL2;
4526 }
4527 } else {
4528 if (!(env->cp15.sctlr_el[1] & SCTLR_DZE)) {
4529 return CP_ACCESS_TRAP;
4530 }
4531 if (hcr & HCR_TDZ) {
4532 return CP_ACCESS_TRAP_EL2;
4533 }
4534 }
4535 } else if (hcr & HCR_TDZ) {
4536 return CP_ACCESS_TRAP_EL2;
4537 }
aca3f40b
PM
4538 }
4539 return CP_ACCESS_OK;
4540}
4541
4542static uint64_t aa64_dczid_read(CPUARMState *env, const ARMCPRegInfo *ri)
4543{
2fc0cc0e 4544 ARMCPU *cpu = env_archcpu(env);
aca3f40b
PM
4545 int dzp_bit = 1 << 4;
4546
4547 /* DZP indicates whether DC ZVA access is allowed */
3f208fd7 4548 if (aa64_zva_access(env, NULL, false) == CP_ACCESS_OK) {
aca3f40b
PM
4549 dzp_bit = 0;
4550 }
4551 return cpu->dcz_blocksize | dzp_bit;
4552}
4553
3f208fd7
PM
4554static CPAccessResult sp_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
4555 bool isread)
f502cfc2 4556{
cdcf1405 4557 if (!(env->pstate & PSTATE_SP)) {
f502cfc2
PM
4558 /* Access to SP_EL0 is undefined if it's being used as
4559 * the stack pointer.
4560 */
4561 return CP_ACCESS_TRAP_UNCATEGORIZED;
4562 }
4563 return CP_ACCESS_OK;
4564}
4565
4566static uint64_t spsel_read(CPUARMState *env, const ARMCPRegInfo *ri)
4567{
4568 return env->pstate & PSTATE_SP;
4569}
4570
4571static void spsel_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
4572{
4573 update_spsel(env, val);
4574}
4575
137feaa9
FA
4576static void sctlr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4577 uint64_t value)
4578{
2fc0cc0e 4579 ARMCPU *cpu = env_archcpu(env);
137feaa9
FA
4580
4581 if (raw_read(env, ri) == value) {
4582 /* Skip the TLB flush if nothing actually changed; Linux likes
4583 * to do a lot of pointless SCTLR writes.
4584 */
4585 return;
4586 }
4587
06312feb
PM
4588 if (arm_feature(env, ARM_FEATURE_PMSA) && !cpu->has_mpu) {
4589 /* M bit is RAZ/WI for PMSA with no MPU implemented */
4590 value &= ~SCTLR_M;
4591 }
4592
137feaa9
FA
4593 raw_write(env, ri, value);
4594 /* ??? Lots of these bits are not implemented. */
4595 /* This may enable/disable the MMU, so do a TLB flush. */
d10eb08f 4596 tlb_flush(CPU(cpu));
2e5dcf36
RH
4597
4598 if (ri->type & ARM_CP_SUPPRESS_TB_END) {
4599 /*
4600 * Normally we would always end the TB on an SCTLR write; see the
4601 * comment in ARMCPRegInfo sctlr initialization below for why Xscale
4602 * is special. Setting ARM_CP_SUPPRESS_TB_END also stops the rebuild
4603 * of hflags from the translator, so do it here.
4604 */
4605 arm_rebuild_hflags(env);
4606 }
137feaa9
FA
4607}
4608
3f208fd7
PM
4609static CPAccessResult fpexc32_access(CPUARMState *env, const ARMCPRegInfo *ri,
4610 bool isread)
03fbf20f
PM
4611{
4612 if ((env->cp15.cptr_el[2] & CPTR_TFP) && arm_current_el(env) == 2) {
f2cae609 4613 return CP_ACCESS_TRAP_FP_EL2;
03fbf20f
PM
4614 }
4615 if (env->cp15.cptr_el[3] & CPTR_TFP) {
f2cae609 4616 return CP_ACCESS_TRAP_FP_EL3;
03fbf20f
PM
4617 }
4618 return CP_ACCESS_OK;
4619}
4620
a8d64e73
PM
4621static void sdcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4622 uint64_t value)
4623{
4624 env->cp15.mdcr_el3 = value & SDCR_VALID_MASK;
4625}
4626
b0d2b7d0
PM
4627static const ARMCPRegInfo v8_cp_reginfo[] = {
4628 /* Minimal set of EL0-visible registers. This will need to be expanded
4629 * significantly for system emulation of AArch64 CPUs.
4630 */
4631 { .name = "NZCV", .state = ARM_CP_STATE_AA64,
4632 .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 2,
4633 .access = PL0_RW, .type = ARM_CP_NZCV },
c2b820fe
PM
4634 { .name = "DAIF", .state = ARM_CP_STATE_AA64,
4635 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 2,
7a0e58fa 4636 .type = ARM_CP_NO_RAW,
c2b820fe
PM
4637 .access = PL0_RW, .accessfn = aa64_daif_access,
4638 .fieldoffset = offsetof(CPUARMState, daif),
4639 .writefn = aa64_daif_write, .resetfn = arm_cp_reset_ignore },
b0d2b7d0
PM
4640 { .name = "FPCR", .state = ARM_CP_STATE_AA64,
4641 .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 4,
b916c9c3 4642 .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END,
fe03d45f 4643 .readfn = aa64_fpcr_read, .writefn = aa64_fpcr_write },
b0d2b7d0
PM
4644 { .name = "FPSR", .state = ARM_CP_STATE_AA64,
4645 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 4,
b916c9c3 4646 .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END,
fe03d45f 4647 .readfn = aa64_fpsr_read, .writefn = aa64_fpsr_write },
b0d2b7d0
PM
4648 { .name = "DCZID_EL0", .state = ARM_CP_STATE_AA64,
4649 .opc0 = 3, .opc1 = 3, .opc2 = 7, .crn = 0, .crm = 0,
7a0e58fa 4650 .access = PL0_R, .type = ARM_CP_NO_RAW,
aca3f40b
PM
4651 .readfn = aa64_dczid_read },
4652 { .name = "DC_ZVA", .state = ARM_CP_STATE_AA64,
4653 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 1,
4654 .access = PL0_W, .type = ARM_CP_DC_ZVA,
4655#ifndef CONFIG_USER_ONLY
4656 /* Avoid overhead of an access check that always passes in user-mode */
4657 .accessfn = aa64_zva_access,
4658#endif
4659 },
0eef9d98
PM
4660 { .name = "CURRENTEL", .state = ARM_CP_STATE_AA64,
4661 .opc0 = 3, .opc1 = 0, .opc2 = 2, .crn = 4, .crm = 2,
4662 .access = PL1_R, .type = ARM_CP_CURRENTEL },
8af35c37
PM
4663 /* Cache ops: all NOPs since we don't emulate caches */
4664 { .name = "IC_IALLUIS", .state = ARM_CP_STATE_AA64,
4665 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
4666 .access = PL1_W, .type = ARM_CP_NOP },
4667 { .name = "IC_IALLU", .state = ARM_CP_STATE_AA64,
4668 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
4669 .access = PL1_W, .type = ARM_CP_NOP },
4670 { .name = "IC_IVAU", .state = ARM_CP_STATE_AA64,
4671 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 5, .opc2 = 1,
4672 .access = PL0_W, .type = ARM_CP_NOP,
4673 .accessfn = aa64_cacheop_access },
4674 { .name = "DC_IVAC", .state = ARM_CP_STATE_AA64,
4675 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
4676 .access = PL1_W, .type = ARM_CP_NOP },
4677 { .name = "DC_ISW", .state = ARM_CP_STATE_AA64,
4678 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
4679 .access = PL1_W, .type = ARM_CP_NOP },
4680 { .name = "DC_CVAC", .state = ARM_CP_STATE_AA64,
4681 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 1,
4682 .access = PL0_W, .type = ARM_CP_NOP,
4683 .accessfn = aa64_cacheop_access },
4684 { .name = "DC_CSW", .state = ARM_CP_STATE_AA64,
4685 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
4686 .access = PL1_W, .type = ARM_CP_NOP },
4687 { .name = "DC_CVAU", .state = ARM_CP_STATE_AA64,
4688 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 11, .opc2 = 1,
4689 .access = PL0_W, .type = ARM_CP_NOP,
4690 .accessfn = aa64_cacheop_access },
4691 { .name = "DC_CIVAC", .state = ARM_CP_STATE_AA64,
4692 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 1,
4693 .access = PL0_W, .type = ARM_CP_NOP,
4694 .accessfn = aa64_cacheop_access },
4695 { .name = "DC_CISW", .state = ARM_CP_STATE_AA64,
4696 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
4697 .access = PL1_W, .type = ARM_CP_NOP },
168aa23b
PM
4698 /* TLBI operations */
4699 { .name = "TLBI_VMALLE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 4700 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
7a0e58fa 4701 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 4702 .writefn = tlbi_aa64_vmalle1is_write },
168aa23b 4703 { .name = "TLBI_VAE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 4704 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
7a0e58fa 4705 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 4706 .writefn = tlbi_aa64_vae1is_write },
168aa23b 4707 { .name = "TLBI_ASIDE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 4708 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
7a0e58fa 4709 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 4710 .writefn = tlbi_aa64_vmalle1is_write },
168aa23b 4711 { .name = "TLBI_VAAE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 4712 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
7a0e58fa 4713 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 4714 .writefn = tlbi_aa64_vae1is_write },
168aa23b 4715 { .name = "TLBI_VALE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 4716 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
7a0e58fa 4717 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 4718 .writefn = tlbi_aa64_vae1is_write },
168aa23b 4719 { .name = "TLBI_VAALE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 4720 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
7a0e58fa 4721 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 4722 .writefn = tlbi_aa64_vae1is_write },
168aa23b 4723 { .name = "TLBI_VMALLE1", .state = ARM_CP_STATE_AA64,
6ab9f499 4724 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
7a0e58fa 4725 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 4726 .writefn = tlbi_aa64_vmalle1_write },
168aa23b 4727 { .name = "TLBI_VAE1", .state = ARM_CP_STATE_AA64,
6ab9f499 4728 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
7a0e58fa 4729 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 4730 .writefn = tlbi_aa64_vae1_write },
168aa23b 4731 { .name = "TLBI_ASIDE1", .state = ARM_CP_STATE_AA64,
6ab9f499 4732 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
7a0e58fa 4733 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 4734 .writefn = tlbi_aa64_vmalle1_write },
168aa23b 4735 { .name = "TLBI_VAAE1", .state = ARM_CP_STATE_AA64,
6ab9f499 4736 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
7a0e58fa 4737 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 4738 .writefn = tlbi_aa64_vae1_write },
168aa23b 4739 { .name = "TLBI_VALE1", .state = ARM_CP_STATE_AA64,
6ab9f499 4740 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
7a0e58fa 4741 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 4742 .writefn = tlbi_aa64_vae1_write },
168aa23b 4743 { .name = "TLBI_VAALE1", .state = ARM_CP_STATE_AA64,
6ab9f499 4744 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
7a0e58fa 4745 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 4746 .writefn = tlbi_aa64_vae1_write },
cea66e91
PM
4747 { .name = "TLBI_IPAS2E1IS", .state = ARM_CP_STATE_AA64,
4748 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1,
4749 .access = PL2_W, .type = ARM_CP_NO_RAW,
4750 .writefn = tlbi_aa64_ipas2e1is_write },
4751 { .name = "TLBI_IPAS2LE1IS", .state = ARM_CP_STATE_AA64,
4752 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5,
4753 .access = PL2_W, .type = ARM_CP_NO_RAW,
4754 .writefn = tlbi_aa64_ipas2e1is_write },
83ddf975
PM
4755 { .name = "TLBI_ALLE1IS", .state = ARM_CP_STATE_AA64,
4756 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
4757 .access = PL2_W, .type = ARM_CP_NO_RAW,
fd3ed969 4758 .writefn = tlbi_aa64_alle1is_write },
43efaa33
PM
4759 { .name = "TLBI_VMALLS12E1IS", .state = ARM_CP_STATE_AA64,
4760 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 6,
4761 .access = PL2_W, .type = ARM_CP_NO_RAW,
4762 .writefn = tlbi_aa64_alle1is_write },
cea66e91
PM
4763 { .name = "TLBI_IPAS2E1", .state = ARM_CP_STATE_AA64,
4764 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1,
4765 .access = PL2_W, .type = ARM_CP_NO_RAW,
4766 .writefn = tlbi_aa64_ipas2e1_write },
4767 { .name = "TLBI_IPAS2LE1", .state = ARM_CP_STATE_AA64,
4768 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5,
4769 .access = PL2_W, .type = ARM_CP_NO_RAW,
4770 .writefn = tlbi_aa64_ipas2e1_write },
83ddf975
PM
4771 { .name = "TLBI_ALLE1", .state = ARM_CP_STATE_AA64,
4772 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
4773 .access = PL2_W, .type = ARM_CP_NO_RAW,
fd3ed969 4774 .writefn = tlbi_aa64_alle1_write },
43efaa33
PM
4775 { .name = "TLBI_VMALLS12E1", .state = ARM_CP_STATE_AA64,
4776 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 6,
4777 .access = PL2_W, .type = ARM_CP_NO_RAW,
4778 .writefn = tlbi_aa64_alle1is_write },
19525524
PM
4779#ifndef CONFIG_USER_ONLY
4780 /* 64 bit address translation operations */
4781 { .name = "AT_S1E1R", .state = ARM_CP_STATE_AA64,
4782 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 0,
0710b2fa
PM
4783 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4784 .writefn = ats_write64 },
19525524
PM
4785 { .name = "AT_S1E1W", .state = ARM_CP_STATE_AA64,
4786 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 1,
0710b2fa
PM
4787 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4788 .writefn = ats_write64 },
19525524
PM
4789 { .name = "AT_S1E0R", .state = ARM_CP_STATE_AA64,
4790 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 2,
0710b2fa
PM
4791 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4792 .writefn = ats_write64 },
19525524
PM
4793 { .name = "AT_S1E0W", .state = ARM_CP_STATE_AA64,
4794 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 3,
0710b2fa
PM
4795 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4796 .writefn = ats_write64 },
2a47df95 4797 { .name = "AT_S12E1R", .state = ARM_CP_STATE_AA64,
7a379c7e 4798 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 4,
0710b2fa
PM
4799 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4800 .writefn = ats_write64 },
2a47df95 4801 { .name = "AT_S12E1W", .state = ARM_CP_STATE_AA64,
7a379c7e 4802 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 5,
0710b2fa
PM
4803 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4804 .writefn = ats_write64 },
2a47df95 4805 { .name = "AT_S12E0R", .state = ARM_CP_STATE_AA64,
7a379c7e 4806 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 6,
0710b2fa
PM
4807 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4808 .writefn = ats_write64 },
2a47df95 4809 { .name = "AT_S12E0W", .state = ARM_CP_STATE_AA64,
7a379c7e 4810 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 7,
0710b2fa
PM
4811 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4812 .writefn = ats_write64 },
2a47df95
PM
4813 /* AT S1E2* are elsewhere as they UNDEF from EL3 if EL2 is not present */
4814 { .name = "AT_S1E3R", .state = ARM_CP_STATE_AA64,
4815 .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 0,
0710b2fa
PM
4816 .access = PL3_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4817 .writefn = ats_write64 },
2a47df95
PM
4818 { .name = "AT_S1E3W", .state = ARM_CP_STATE_AA64,
4819 .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 1,
0710b2fa
PM
4820 .access = PL3_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4821 .writefn = ats_write64 },
c96fc9b5
EI
4822 { .name = "PAR_EL1", .state = ARM_CP_STATE_AA64,
4823 .type = ARM_CP_ALIAS,
4824 .opc0 = 3, .opc1 = 0, .crn = 7, .crm = 4, .opc2 = 0,
4825 .access = PL1_RW, .resetvalue = 0,
4826 .fieldoffset = offsetof(CPUARMState, cp15.par_el[1]),
4827 .writefn = par_write },
19525524 4828#endif
995939a6 4829 /* TLB invalidate last level of translation table walk */
9449fdf6 4830 { .name = "TLBIMVALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
7a0e58fa 4831 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_is_write },
9449fdf6 4832 { .name = "TLBIMVAALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
7a0e58fa 4833 .type = ARM_CP_NO_RAW, .access = PL1_W,
fa439fc5 4834 .writefn = tlbimvaa_is_write },
9449fdf6 4835 { .name = "TLBIMVAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
7a0e58fa 4836 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write },
9449fdf6 4837 { .name = "TLBIMVAAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
7a0e58fa 4838 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimvaa_write },
541ef8c2
SS
4839 { .name = "TLBIMVALH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
4840 .type = ARM_CP_NO_RAW, .access = PL2_W,
4841 .writefn = tlbimva_hyp_write },
4842 { .name = "TLBIMVALHIS",
4843 .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
4844 .type = ARM_CP_NO_RAW, .access = PL2_W,
4845 .writefn = tlbimva_hyp_is_write },
4846 { .name = "TLBIIPAS2",
4847 .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1,
4848 .type = ARM_CP_NO_RAW, .access = PL2_W,
4849 .writefn = tlbiipas2_write },
4850 { .name = "TLBIIPAS2IS",
4851 .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1,
4852 .type = ARM_CP_NO_RAW, .access = PL2_W,
4853 .writefn = tlbiipas2_is_write },
4854 { .name = "TLBIIPAS2L",
4855 .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5,
4856 .type = ARM_CP_NO_RAW, .access = PL2_W,
4857 .writefn = tlbiipas2_write },
4858 { .name = "TLBIIPAS2LIS",
4859 .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5,
4860 .type = ARM_CP_NO_RAW, .access = PL2_W,
4861 .writefn = tlbiipas2_is_write },
9449fdf6
PM
4862 /* 32 bit cache operations */
4863 { .name = "ICIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
4864 .type = ARM_CP_NOP, .access = PL1_W },
4865 { .name = "BPIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 6,
4866 .type = ARM_CP_NOP, .access = PL1_W },
4867 { .name = "ICIALLU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
4868 .type = ARM_CP_NOP, .access = PL1_W },
4869 { .name = "ICIMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 1,
4870 .type = ARM_CP_NOP, .access = PL1_W },
4871 { .name = "BPIALL", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 6,
4872 .type = ARM_CP_NOP, .access = PL1_W },
4873 { .name = "BPIMVA", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 7,
4874 .type = ARM_CP_NOP, .access = PL1_W },
4875 { .name = "DCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
4876 .type = ARM_CP_NOP, .access = PL1_W },
4877 { .name = "DCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
4878 .type = ARM_CP_NOP, .access = PL1_W },
4879 { .name = "DCCMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 1,
4880 .type = ARM_CP_NOP, .access = PL1_W },
4881 { .name = "DCCSW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
4882 .type = ARM_CP_NOP, .access = PL1_W },
4883 { .name = "DCCMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 11, .opc2 = 1,
4884 .type = ARM_CP_NOP, .access = PL1_W },
4885 { .name = "DCCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 1,
4886 .type = ARM_CP_NOP, .access = PL1_W },
4887 { .name = "DCCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
4888 .type = ARM_CP_NOP, .access = PL1_W },
4889 /* MMU Domain access control / MPU write buffer control */
0c17d68c
FA
4890 { .name = "DACR", .cp = 15, .opc1 = 0, .crn = 3, .crm = 0, .opc2 = 0,
4891 .access = PL1_RW, .resetvalue = 0,
4892 .writefn = dacr_write, .raw_writefn = raw_write,
4893 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
4894 offsetoflow32(CPUARMState, cp15.dacr_ns) } },
a0618a19 4895 { .name = "ELR_EL1", .state = ARM_CP_STATE_AA64,
7a0e58fa 4896 .type = ARM_CP_ALIAS,
a0618a19 4897 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 1,
6947f059
EI
4898 .access = PL1_RW,
4899 .fieldoffset = offsetof(CPUARMState, elr_el[1]) },
a65f1de9 4900 { .name = "SPSR_EL1", .state = ARM_CP_STATE_AA64,
7a0e58fa 4901 .type = ARM_CP_ALIAS,
a65f1de9 4902 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 0,
99a99c1f
SB
4903 .access = PL1_RW,
4904 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_SVC]) },
f502cfc2
PM
4905 /* We rely on the access checks not allowing the guest to write to the
4906 * state field when SPSel indicates that it's being used as the stack
4907 * pointer.
4908 */
4909 { .name = "SP_EL0", .state = ARM_CP_STATE_AA64,
4910 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 1, .opc2 = 0,
4911 .access = PL1_RW, .accessfn = sp_el0_access,
7a0e58fa 4912 .type = ARM_CP_ALIAS,
f502cfc2 4913 .fieldoffset = offsetof(CPUARMState, sp_el[0]) },
884b4dee
GB
4914 { .name = "SP_EL1", .state = ARM_CP_STATE_AA64,
4915 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 1, .opc2 = 0,
7a0e58fa 4916 .access = PL2_RW, .type = ARM_CP_ALIAS,
884b4dee 4917 .fieldoffset = offsetof(CPUARMState, sp_el[1]) },
f502cfc2
PM
4918 { .name = "SPSel", .state = ARM_CP_STATE_AA64,
4919 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 0,
7a0e58fa 4920 .type = ARM_CP_NO_RAW,
f502cfc2 4921 .access = PL1_RW, .readfn = spsel_read, .writefn = spsel_write },
03fbf20f
PM
4922 { .name = "FPEXC32_EL2", .state = ARM_CP_STATE_AA64,
4923 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 3, .opc2 = 0,
4924 .type = ARM_CP_ALIAS,
4925 .fieldoffset = offsetof(CPUARMState, vfp.xregs[ARM_VFP_FPEXC]),
4926 .access = PL2_RW, .accessfn = fpexc32_access },
6a43e0b6
PM
4927 { .name = "DACR32_EL2", .state = ARM_CP_STATE_AA64,
4928 .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 0, .opc2 = 0,
4929 .access = PL2_RW, .resetvalue = 0,
4930 .writefn = dacr_write, .raw_writefn = raw_write,
4931 .fieldoffset = offsetof(CPUARMState, cp15.dacr32_el2) },
4932 { .name = "IFSR32_EL2", .state = ARM_CP_STATE_AA64,
4933 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 0, .opc2 = 1,
4934 .access = PL2_RW, .resetvalue = 0,
4935 .fieldoffset = offsetof(CPUARMState, cp15.ifsr32_el2) },
4936 { .name = "SPSR_IRQ", .state = ARM_CP_STATE_AA64,
4937 .type = ARM_CP_ALIAS,
4938 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 0,
4939 .access = PL2_RW,
4940 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_IRQ]) },
4941 { .name = "SPSR_ABT", .state = ARM_CP_STATE_AA64,
4942 .type = ARM_CP_ALIAS,
4943 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 1,
4944 .access = PL2_RW,
4945 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_ABT]) },
4946 { .name = "SPSR_UND", .state = ARM_CP_STATE_AA64,
4947 .type = ARM_CP_ALIAS,
4948 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 2,
4949 .access = PL2_RW,
4950 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_UND]) },
4951 { .name = "SPSR_FIQ", .state = ARM_CP_STATE_AA64,
4952 .type = ARM_CP_ALIAS,
4953 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 3,
4954 .access = PL2_RW,
4955 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_FIQ]) },
a8d64e73
PM
4956 { .name = "MDCR_EL3", .state = ARM_CP_STATE_AA64,
4957 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 3, .opc2 = 1,
4958 .resetvalue = 0,
4959 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el3) },
4960 { .name = "SDCR", .type = ARM_CP_ALIAS,
4961 .cp = 15, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 1,
4962 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
4963 .writefn = sdcr_write,
4964 .fieldoffset = offsetoflow32(CPUARMState, cp15.mdcr_el3) },
b0d2b7d0
PM
4965 REGINFO_SENTINEL
4966};
4967
d42e3c26 4968/* Used to describe the behaviour of EL2 regs when EL2 does not exist. */
4771cd01 4969static const ARMCPRegInfo el3_no_el2_cp_reginfo[] = {
d79e0c06 4970 { .name = "VBAR_EL2", .state = ARM_CP_STATE_BOTH,
d42e3c26
EI
4971 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0,
4972 .access = PL2_RW,
4973 .readfn = arm_cp_read_zero, .writefn = arm_cp_write_ignore },
ce4afed8 4974 { .name = "HCR_EL2", .state = ARM_CP_STATE_BOTH,
7a0e58fa 4975 .type = ARM_CP_NO_RAW,
f149e3e8
EI
4976 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
4977 .access = PL2_RW,
ce4afed8 4978 .type = ARM_CP_CONST, .resetvalue = 0 },
831a2fca
PM
4979 { .name = "HACR_EL2", .state = ARM_CP_STATE_BOTH,
4980 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 7,
4981 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
68e78e33
PM
4982 { .name = "ESR_EL2", .state = ARM_CP_STATE_BOTH,
4983 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 0,
4984 .access = PL2_RW,
4985 .type = ARM_CP_CONST, .resetvalue = 0 },
c6f19164
GB
4986 { .name = "CPTR_EL2", .state = ARM_CP_STATE_BOTH,
4987 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 2,
4988 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
95f949ac
EI
4989 { .name = "MAIR_EL2", .state = ARM_CP_STATE_BOTH,
4990 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 0,
4991 .access = PL2_RW, .type = ARM_CP_CONST,
4992 .resetvalue = 0 },
4993 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
b5ede85b 4994 .cp = 15, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 1,
95f949ac 4995 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
2179ef95
PM
4996 { .name = "AMAIR_EL2", .state = ARM_CP_STATE_BOTH,
4997 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 0,
4998 .access = PL2_RW, .type = ARM_CP_CONST,
4999 .resetvalue = 0 },
55b53c71 5000 { .name = "HAMAIR1", .state = ARM_CP_STATE_AA32,
b5ede85b 5001 .cp = 15, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 1,
2179ef95
PM
5002 .access = PL2_RW, .type = ARM_CP_CONST,
5003 .resetvalue = 0 },
37cd6c24
PM
5004 { .name = "AFSR0_EL2", .state = ARM_CP_STATE_BOTH,
5005 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 0,
5006 .access = PL2_RW, .type = ARM_CP_CONST,
5007 .resetvalue = 0 },
5008 { .name = "AFSR1_EL2", .state = ARM_CP_STATE_BOTH,
5009 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 1,
5010 .access = PL2_RW, .type = ARM_CP_CONST,
5011 .resetvalue = 0 },
06ec4c8c
EI
5012 { .name = "TCR_EL2", .state = ARM_CP_STATE_BOTH,
5013 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 2,
5014 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
68e9c2fe
EI
5015 { .name = "VTCR_EL2", .state = ARM_CP_STATE_BOTH,
5016 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
5017 .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any,
5018 .type = ARM_CP_CONST, .resetvalue = 0 },
b698e9cf
EI
5019 { .name = "VTTBR", .state = ARM_CP_STATE_AA32,
5020 .cp = 15, .opc1 = 6, .crm = 2,
5021 .access = PL2_RW, .accessfn = access_el3_aa32ns,
5022 .type = ARM_CP_CONST | ARM_CP_64BIT, .resetvalue = 0 },
5023 { .name = "VTTBR_EL2", .state = ARM_CP_STATE_AA64,
5024 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 0,
5025 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
b9cb5323
EI
5026 { .name = "SCTLR_EL2", .state = ARM_CP_STATE_BOTH,
5027 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 0,
5028 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
ff05f37b
EI
5029 { .name = "TPIDR_EL2", .state = ARM_CP_STATE_BOTH,
5030 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 2,
5031 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
a57633c0
EI
5032 { .name = "TTBR0_EL2", .state = ARM_CP_STATE_AA64,
5033 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
5034 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
5035 { .name = "HTTBR", .cp = 15, .opc1 = 4, .crm = 2,
5036 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_CONST,
5037 .resetvalue = 0 },
0b6440af
EI
5038 { .name = "CNTHCTL_EL2", .state = ARM_CP_STATE_BOTH,
5039 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 1, .opc2 = 0,
5040 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
edac4d8a
EI
5041 { .name = "CNTVOFF_EL2", .state = ARM_CP_STATE_AA64,
5042 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 3,
5043 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
5044 { .name = "CNTVOFF", .cp = 15, .opc1 = 4, .crm = 14,
5045 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_CONST,
5046 .resetvalue = 0 },
b0e66d95
EI
5047 { .name = "CNTHP_CVAL_EL2", .state = ARM_CP_STATE_AA64,
5048 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 2,
5049 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
5050 { .name = "CNTHP_CVAL", .cp = 15, .opc1 = 6, .crm = 14,
5051 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_CONST,
5052 .resetvalue = 0 },
5053 { .name = "CNTHP_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
5054 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 0,
5055 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
5056 { .name = "CNTHP_CTL_EL2", .state = ARM_CP_STATE_BOTH,
5057 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 1,
5058 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
14cc7b54
SF
5059 { .name = "MDCR_EL2", .state = ARM_CP_STATE_BOTH,
5060 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 1,
d6c8cf81
PM
5061 .access = PL2_RW, .accessfn = access_tda,
5062 .type = ARM_CP_CONST, .resetvalue = 0 },
59e05530
EI
5063 { .name = "HPFAR_EL2", .state = ARM_CP_STATE_BOTH,
5064 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
5065 .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any,
5066 .type = ARM_CP_CONST, .resetvalue = 0 },
2a5a9abd
AF
5067 { .name = "HSTR_EL2", .state = ARM_CP_STATE_BOTH,
5068 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 3,
5069 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
cba517c3
PM
5070 { .name = "FAR_EL2", .state = ARM_CP_STATE_BOTH,
5071 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 0,
5072 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
5073 { .name = "HIFAR", .state = ARM_CP_STATE_AA32,
5074 .type = ARM_CP_CONST,
5075 .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 2,
5076 .access = PL2_RW, .resetvalue = 0 },
d42e3c26
EI
5077 REGINFO_SENTINEL
5078};
5079
ce4afed8
PM
5080/* Ditto, but for registers which exist in ARMv8 but not v7 */
5081static const ARMCPRegInfo el3_no_el2_v8_cp_reginfo[] = {
5082 { .name = "HCR2", .state = ARM_CP_STATE_AA32,
5083 .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 4,
5084 .access = PL2_RW,
5085 .type = ARM_CP_CONST, .resetvalue = 0 },
5086 REGINFO_SENTINEL
5087};
5088
f149e3e8
EI
5089static void hcr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
5090{
2fc0cc0e 5091 ARMCPU *cpu = env_archcpu(env);
03c76131
RH
5092 /* Begin with bits defined in base ARMv8.0. */
5093 uint64_t valid_mask = MAKE_64BIT_MASK(0, 34);
f149e3e8
EI
5094
5095 if (arm_feature(env, ARM_FEATURE_EL3)) {
5096 valid_mask &= ~HCR_HCD;
77077a83
JK
5097 } else if (cpu->psci_conduit != QEMU_PSCI_CONDUIT_SMC) {
5098 /* Architecturally HCR.TSC is RES0 if EL3 is not implemented.
5099 * However, if we're using the SMC PSCI conduit then QEMU is
5100 * effectively acting like EL3 firmware and so the guest at
5101 * EL2 should retain the ability to prevent EL1 from being
5102 * able to make SMC calls into the ersatz firmware, so in
5103 * that case HCR.TSC should be read/write.
5104 */
f149e3e8
EI
5105 valid_mask &= ~HCR_TSC;
5106 }
03c76131
RH
5107 if (cpu_isar_feature(aa64_vh, cpu)) {
5108 valid_mask |= HCR_E2H;
5109 }
2d7137c1
RH
5110 if (cpu_isar_feature(aa64_lor, cpu)) {
5111 valid_mask |= HCR_TLOR;
5112 }
ef682cdb
RH
5113 if (cpu_isar_feature(aa64_pauth, cpu)) {
5114 valid_mask |= HCR_API | HCR_APK;
5115 }
f149e3e8
EI
5116
5117 /* Clear RES0 bits. */
5118 value &= valid_mask;
5119
5120 /* These bits change the MMU setup:
5121 * HCR_VM enables stage 2 translation
5122 * HCR_PTW forbids certain page-table setups
5123 * HCR_DC Disables stage1 and enables stage2 translation
5124 */
ce4afed8 5125 if ((env->cp15.hcr_el2 ^ value) & (HCR_VM | HCR_PTW | HCR_DC)) {
d10eb08f 5126 tlb_flush(CPU(cpu));
f149e3e8 5127 }
ce4afed8 5128 env->cp15.hcr_el2 = value;
89430fc6
PM
5129
5130 /*
5131 * Updates to VI and VF require us to update the status of
5132 * virtual interrupts, which are the logical OR of these bits
5133 * and the state of the input lines from the GIC. (This requires
5134 * that we have the iothread lock, which is done by marking the
5135 * reginfo structs as ARM_CP_IO.)
5136 * Note that if a write to HCR pends a VIRQ or VFIQ it is never
5137 * possible for it to be taken immediately, because VIRQ and
5138 * VFIQ are masked unless running at EL0 or EL1, and HCR
5139 * can only be written at EL2.
5140 */
5141 g_assert(qemu_mutex_iothread_locked());
5142 arm_cpu_update_virq(cpu);
5143 arm_cpu_update_vfiq(cpu);
ce4afed8
PM
5144}
5145
5146static void hcr_writehigh(CPUARMState *env, const ARMCPRegInfo *ri,
5147 uint64_t value)
5148{
5149 /* Handle HCR2 write, i.e. write to high half of HCR_EL2 */
5150 value = deposit64(env->cp15.hcr_el2, 32, 32, value);
5151 hcr_write(env, NULL, value);
5152}
5153
5154static void hcr_writelow(CPUARMState *env, const ARMCPRegInfo *ri,
5155 uint64_t value)
5156{
5157 /* Handle HCR write, i.e. write to low half of HCR_EL2 */
5158 value = deposit64(env->cp15.hcr_el2, 0, 32, value);
5159 hcr_write(env, NULL, value);
f149e3e8
EI
5160}
5161
f7778444
RH
5162/*
5163 * Return the effective value of HCR_EL2.
5164 * Bits that are not included here:
5165 * RW (read from SCR_EL3.RW as needed)
5166 */
5167uint64_t arm_hcr_el2_eff(CPUARMState *env)
5168{
5169 uint64_t ret = env->cp15.hcr_el2;
5170
5171 if (arm_is_secure_below_el3(env)) {
5172 /*
5173 * "This register has no effect if EL2 is not enabled in the
5174 * current Security state". This is ARMv8.4-SecEL2 speak for
5175 * !(SCR_EL3.NS==1 || SCR_EL3.EEL2==1).
5176 *
5177 * Prior to that, the language was "In an implementation that
5178 * includes EL3, when the value of SCR_EL3.NS is 0 the PE behaves
5179 * as if this field is 0 for all purposes other than a direct
5180 * read or write access of HCR_EL2". With lots of enumeration
5181 * on a per-field basis. In current QEMU, this is condition
5182 * is arm_is_secure_below_el3.
5183 *
5184 * Since the v8.4 language applies to the entire register, and
5185 * appears to be backward compatible, use that.
5186 */
5187 ret = 0;
5188 } else if (ret & HCR_TGE) {
5189 /* These bits are up-to-date as of ARMv8.4. */
5190 if (ret & HCR_E2H) {
5191 ret &= ~(HCR_VM | HCR_FMO | HCR_IMO | HCR_AMO |
5192 HCR_BSU_MASK | HCR_DC | HCR_TWI | HCR_TWE |
5193 HCR_TID0 | HCR_TID2 | HCR_TPCP | HCR_TPU |
5194 HCR_TDZ | HCR_CD | HCR_ID | HCR_MIOCNCE);
5195 } else {
5196 ret |= HCR_FMO | HCR_IMO | HCR_AMO;
5197 }
5198 ret &= ~(HCR_SWIO | HCR_PTW | HCR_VF | HCR_VI | HCR_VSE |
5199 HCR_FB | HCR_TID1 | HCR_TID3 | HCR_TSC | HCR_TACR |
5200 HCR_TSW | HCR_TTLB | HCR_TVM | HCR_HCD | HCR_TRVM |
5201 HCR_TLOR);
5202 }
5203
5204 return ret;
5205}
5206
fc1120a7
PM
5207static void cptr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
5208 uint64_t value)
5209{
5210 /*
5211 * For A-profile AArch32 EL3, if NSACR.CP10
5212 * is 0 then HCPTR.{TCP11,TCP10} ignore writes and read as 1.
5213 */
5214 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
5215 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
5216 value &= ~(0x3 << 10);
5217 value |= env->cp15.cptr_el[2] & (0x3 << 10);
5218 }
5219 env->cp15.cptr_el[2] = value;
5220}
5221
5222static uint64_t cptr_el2_read(CPUARMState *env, const ARMCPRegInfo *ri)
5223{
5224 /*
5225 * For A-profile AArch32 EL3, if NSACR.CP10
5226 * is 0 then HCPTR.{TCP11,TCP10} ignore writes and read as 1.
5227 */
5228 uint64_t value = env->cp15.cptr_el[2];
5229
5230 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
5231 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
5232 value |= 0x3 << 10;
5233 }
5234 return value;
5235}
5236
4771cd01 5237static const ARMCPRegInfo el2_cp_reginfo[] = {
f149e3e8 5238 { .name = "HCR_EL2", .state = ARM_CP_STATE_AA64,
89430fc6 5239 .type = ARM_CP_IO,
f149e3e8
EI
5240 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
5241 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
c624ea0f 5242 .writefn = hcr_write },
ce4afed8 5243 { .name = "HCR", .state = ARM_CP_STATE_AA32,
89430fc6 5244 .type = ARM_CP_ALIAS | ARM_CP_IO,
ce4afed8
PM
5245 .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
5246 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
c624ea0f 5247 .writefn = hcr_writelow },
831a2fca
PM
5248 { .name = "HACR_EL2", .state = ARM_CP_STATE_BOTH,
5249 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 7,
5250 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3b685ba7 5251 { .name = "ELR_EL2", .state = ARM_CP_STATE_AA64,
7a0e58fa 5252 .type = ARM_CP_ALIAS,
3b685ba7
EI
5253 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 1,
5254 .access = PL2_RW,
5255 .fieldoffset = offsetof(CPUARMState, elr_el[2]) },
68e78e33 5256 { .name = "ESR_EL2", .state = ARM_CP_STATE_BOTH,
f2c30f42
EI
5257 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 0,
5258 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[2]) },
cba517c3 5259 { .name = "FAR_EL2", .state = ARM_CP_STATE_BOTH,
63b60551
EI
5260 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 0,
5261 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[2]) },
cba517c3
PM
5262 { .name = "HIFAR", .state = ARM_CP_STATE_AA32,
5263 .type = ARM_CP_ALIAS,
5264 .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 2,
5265 .access = PL2_RW,
5266 .fieldoffset = offsetofhigh32(CPUARMState, cp15.far_el[2]) },
3b685ba7 5267 { .name = "SPSR_EL2", .state = ARM_CP_STATE_AA64,
7a0e58fa 5268 .type = ARM_CP_ALIAS,
3b685ba7 5269 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 0,
99a99c1f
SB
5270 .access = PL2_RW,
5271 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_HYP]) },
d79e0c06 5272 { .name = "VBAR_EL2", .state = ARM_CP_STATE_BOTH,
d42e3c26
EI
5273 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0,
5274 .access = PL2_RW, .writefn = vbar_write,
5275 .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[2]),
5276 .resetvalue = 0 },
884b4dee
GB
5277 { .name = "SP_EL2", .state = ARM_CP_STATE_AA64,
5278 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 1, .opc2 = 0,
7a0e58fa 5279 .access = PL3_RW, .type = ARM_CP_ALIAS,
884b4dee 5280 .fieldoffset = offsetof(CPUARMState, sp_el[2]) },
c6f19164
GB
5281 { .name = "CPTR_EL2", .state = ARM_CP_STATE_BOTH,
5282 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 2,
5283 .access = PL2_RW, .accessfn = cptr_access, .resetvalue = 0,
fc1120a7
PM
5284 .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[2]),
5285 .readfn = cptr_el2_read, .writefn = cptr_el2_write },
95f949ac
EI
5286 { .name = "MAIR_EL2", .state = ARM_CP_STATE_BOTH,
5287 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 0,
5288 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[2]),
5289 .resetvalue = 0 },
5290 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
b5ede85b 5291 .cp = 15, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 1,
95f949ac
EI
5292 .access = PL2_RW, .type = ARM_CP_ALIAS,
5293 .fieldoffset = offsetofhigh32(CPUARMState, cp15.mair_el[2]) },
2179ef95
PM
5294 { .name = "AMAIR_EL2", .state = ARM_CP_STATE_BOTH,
5295 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 0,
5296 .access = PL2_RW, .type = ARM_CP_CONST,
5297 .resetvalue = 0 },
5298 /* HAMAIR1 is mapped to AMAIR_EL2[63:32] */
55b53c71 5299 { .name = "HAMAIR1", .state = ARM_CP_STATE_AA32,
b5ede85b 5300 .cp = 15, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 1,
2179ef95
PM
5301 .access = PL2_RW, .type = ARM_CP_CONST,
5302 .resetvalue = 0 },
37cd6c24
PM
5303 { .name = "AFSR0_EL2", .state = ARM_CP_STATE_BOTH,
5304 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 0,
5305 .access = PL2_RW, .type = ARM_CP_CONST,
5306 .resetvalue = 0 },
5307 { .name = "AFSR1_EL2", .state = ARM_CP_STATE_BOTH,
5308 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 1,
5309 .access = PL2_RW, .type = ARM_CP_CONST,
5310 .resetvalue = 0 },
06ec4c8c
EI
5311 { .name = "TCR_EL2", .state = ARM_CP_STATE_BOTH,
5312 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 2,
d06dc933
RH
5313 .access = PL2_RW, .writefn = vmsa_tcr_el12_write,
5314 /* no .raw_writefn or .resetfn needed as we never use mask/base_mask */
06ec4c8c 5315 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[2]) },
68e9c2fe
EI
5316 { .name = "VTCR", .state = ARM_CP_STATE_AA32,
5317 .cp = 15, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
bf06c112 5318 .type = ARM_CP_ALIAS,
68e9c2fe
EI
5319 .access = PL2_RW, .accessfn = access_el3_aa32ns,
5320 .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) },
5321 { .name = "VTCR_EL2", .state = ARM_CP_STATE_AA64,
5322 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
bf06c112
PM
5323 .access = PL2_RW,
5324 /* no .writefn needed as this can't cause an ASID change;
5325 * no .raw_writefn or .resetfn needed as we never use mask/base_mask
5326 */
68e9c2fe 5327 .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) },
b698e9cf
EI
5328 { .name = "VTTBR", .state = ARM_CP_STATE_AA32,
5329 .cp = 15, .opc1 = 6, .crm = 2,
5330 .type = ARM_CP_64BIT | ARM_CP_ALIAS,
5331 .access = PL2_RW, .accessfn = access_el3_aa32ns,
5332 .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2),
5333 .writefn = vttbr_write },
5334 { .name = "VTTBR_EL2", .state = ARM_CP_STATE_AA64,
5335 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 0,
5336 .access = PL2_RW, .writefn = vttbr_write,
5337 .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2) },
b9cb5323
EI
5338 { .name = "SCTLR_EL2", .state = ARM_CP_STATE_BOTH,
5339 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 0,
5340 .access = PL2_RW, .raw_writefn = raw_write, .writefn = sctlr_write,
5341 .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[2]) },
ff05f37b
EI
5342 { .name = "TPIDR_EL2", .state = ARM_CP_STATE_BOTH,
5343 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 2,
5344 .access = PL2_RW, .resetvalue = 0,
5345 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[2]) },
a57633c0
EI
5346 { .name = "TTBR0_EL2", .state = ARM_CP_STATE_AA64,
5347 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
ed30da8e 5348 .access = PL2_RW, .resetvalue = 0, .writefn = vmsa_tcr_ttbr_el2_write,
a57633c0
EI
5349 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
5350 { .name = "HTTBR", .cp = 15, .opc1 = 4, .crm = 2,
5351 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
a57633c0 5352 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
541ef8c2
SS
5353 { .name = "TLBIALLNSNH",
5354 .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
5355 .type = ARM_CP_NO_RAW, .access = PL2_W,
5356 .writefn = tlbiall_nsnh_write },
5357 { .name = "TLBIALLNSNHIS",
5358 .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
5359 .type = ARM_CP_NO_RAW, .access = PL2_W,
5360 .writefn = tlbiall_nsnh_is_write },
5361 { .name = "TLBIALLH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
5362 .type = ARM_CP_NO_RAW, .access = PL2_W,
5363 .writefn = tlbiall_hyp_write },
5364 { .name = "TLBIALLHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
5365 .type = ARM_CP_NO_RAW, .access = PL2_W,
5366 .writefn = tlbiall_hyp_is_write },
5367 { .name = "TLBIMVAH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
5368 .type = ARM_CP_NO_RAW, .access = PL2_W,
5369 .writefn = tlbimva_hyp_write },
5370 { .name = "TLBIMVAHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
5371 .type = ARM_CP_NO_RAW, .access = PL2_W,
5372 .writefn = tlbimva_hyp_is_write },
51da9014
EI
5373 { .name = "TLBI_ALLE2", .state = ARM_CP_STATE_AA64,
5374 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
5375 .type = ARM_CP_NO_RAW, .access = PL2_W,
fd3ed969 5376 .writefn = tlbi_aa64_alle2_write },
8742d49d
EI
5377 { .name = "TLBI_VAE2", .state = ARM_CP_STATE_AA64,
5378 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
5379 .type = ARM_CP_NO_RAW, .access = PL2_W,
fd3ed969 5380 .writefn = tlbi_aa64_vae2_write },
2bfb9d75
PM
5381 { .name = "TLBI_VALE2", .state = ARM_CP_STATE_AA64,
5382 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
5383 .access = PL2_W, .type = ARM_CP_NO_RAW,
5384 .writefn = tlbi_aa64_vae2_write },
5385 { .name = "TLBI_ALLE2IS", .state = ARM_CP_STATE_AA64,
5386 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
5387 .access = PL2_W, .type = ARM_CP_NO_RAW,
5388 .writefn = tlbi_aa64_alle2is_write },
8742d49d
EI
5389 { .name = "TLBI_VAE2IS", .state = ARM_CP_STATE_AA64,
5390 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
5391 .type = ARM_CP_NO_RAW, .access = PL2_W,
fd3ed969 5392 .writefn = tlbi_aa64_vae2is_write },
2bfb9d75
PM
5393 { .name = "TLBI_VALE2IS", .state = ARM_CP_STATE_AA64,
5394 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
5395 .access = PL2_W, .type = ARM_CP_NO_RAW,
5396 .writefn = tlbi_aa64_vae2is_write },
edac4d8a 5397#ifndef CONFIG_USER_ONLY
2a47df95
PM
5398 /* Unlike the other EL2-related AT operations, these must
5399 * UNDEF from EL3 if EL2 is not implemented, which is why we
5400 * define them here rather than with the rest of the AT ops.
5401 */
5402 { .name = "AT_S1E2R", .state = ARM_CP_STATE_AA64,
5403 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
5404 .access = PL2_W, .accessfn = at_s1e2_access,
0710b2fa 5405 .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC, .writefn = ats_write64 },
2a47df95
PM
5406 { .name = "AT_S1E2W", .state = ARM_CP_STATE_AA64,
5407 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
5408 .access = PL2_W, .accessfn = at_s1e2_access,
0710b2fa 5409 .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC, .writefn = ats_write64 },
14db7fe0
PM
5410 /* The AArch32 ATS1H* operations are CONSTRAINED UNPREDICTABLE
5411 * if EL2 is not implemented; we choose to UNDEF. Behaviour at EL3
5412 * with SCR.NS == 0 outside Monitor mode is UNPREDICTABLE; we choose
5413 * to behave as if SCR.NS was 1.
5414 */
5415 { .name = "ATS1HR", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
5416 .access = PL2_W,
0710b2fa 5417 .writefn = ats1h_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
14db7fe0
PM
5418 { .name = "ATS1HW", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
5419 .access = PL2_W,
0710b2fa 5420 .writefn = ats1h_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
0b6440af
EI
5421 { .name = "CNTHCTL_EL2", .state = ARM_CP_STATE_BOTH,
5422 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 1, .opc2 = 0,
5423 /* ARMv7 requires bit 0 and 1 to reset to 1. ARMv8 defines the
5424 * reset values as IMPDEF. We choose to reset to 3 to comply with
5425 * both ARMv7 and ARMv8.
5426 */
5427 .access = PL2_RW, .resetvalue = 3,
5428 .fieldoffset = offsetof(CPUARMState, cp15.cnthctl_el2) },
edac4d8a
EI
5429 { .name = "CNTVOFF_EL2", .state = ARM_CP_STATE_AA64,
5430 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 3,
5431 .access = PL2_RW, .type = ARM_CP_IO, .resetvalue = 0,
5432 .writefn = gt_cntvoff_write,
5433 .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
5434 { .name = "CNTVOFF", .cp = 15, .opc1 = 4, .crm = 14,
5435 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS | ARM_CP_IO,
5436 .writefn = gt_cntvoff_write,
5437 .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
b0e66d95
EI
5438 { .name = "CNTHP_CVAL_EL2", .state = ARM_CP_STATE_AA64,
5439 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 2,
5440 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
5441 .type = ARM_CP_IO, .access = PL2_RW,
5442 .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
5443 { .name = "CNTHP_CVAL", .cp = 15, .opc1 = 6, .crm = 14,
5444 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
5445 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_IO,
5446 .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
5447 { .name = "CNTHP_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
5448 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 0,
d44ec156 5449 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
b0e66d95
EI
5450 .resetfn = gt_hyp_timer_reset,
5451 .readfn = gt_hyp_tval_read, .writefn = gt_hyp_tval_write },
5452 { .name = "CNTHP_CTL_EL2", .state = ARM_CP_STATE_BOTH,
5453 .type = ARM_CP_IO,
5454 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 1,
5455 .access = PL2_RW,
5456 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].ctl),
5457 .resetvalue = 0,
5458 .writefn = gt_hyp_ctl_write, .raw_writefn = raw_write },
edac4d8a 5459#endif
14cc7b54
SF
5460 /* The only field of MDCR_EL2 that has a defined architectural reset value
5461 * is MDCR_EL2.HPMN which should reset to the value of PMCR_EL0.N; but we
5ecdd3e4 5462 * don't implement any PMU event counters, so using zero as a reset
14cc7b54
SF
5463 * value for MDCR_EL2 is okay
5464 */
5465 { .name = "MDCR_EL2", .state = ARM_CP_STATE_BOTH,
5466 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 1,
5467 .access = PL2_RW, .resetvalue = 0,
5468 .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el2), },
59e05530
EI
5469 { .name = "HPFAR", .state = ARM_CP_STATE_AA32,
5470 .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
5471 .access = PL2_RW, .accessfn = access_el3_aa32ns,
5472 .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
5473 { .name = "HPFAR_EL2", .state = ARM_CP_STATE_AA64,
5474 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
5475 .access = PL2_RW,
5476 .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
2a5a9abd
AF
5477 { .name = "HSTR_EL2", .state = ARM_CP_STATE_BOTH,
5478 .cp = 15, .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 3,
5479 .access = PL2_RW,
5480 .fieldoffset = offsetof(CPUARMState, cp15.hstr_el2) },
3b685ba7
EI
5481 REGINFO_SENTINEL
5482};
5483
ce4afed8
PM
5484static const ARMCPRegInfo el2_v8_cp_reginfo[] = {
5485 { .name = "HCR2", .state = ARM_CP_STATE_AA32,
89430fc6 5486 .type = ARM_CP_ALIAS | ARM_CP_IO,
ce4afed8
PM
5487 .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 4,
5488 .access = PL2_RW,
5489 .fieldoffset = offsetofhigh32(CPUARMState, cp15.hcr_el2),
5490 .writefn = hcr_writehigh },
5491 REGINFO_SENTINEL
5492};
5493
2f027fc5
PM
5494static CPAccessResult nsacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
5495 bool isread)
5496{
5497 /* The NSACR is RW at EL3, and RO for NS EL1 and NS EL2.
5498 * At Secure EL1 it traps to EL3.
5499 */
5500 if (arm_current_el(env) == 3) {
5501 return CP_ACCESS_OK;
5502 }
5503 if (arm_is_secure_below_el3(env)) {
5504 return CP_ACCESS_TRAP_EL3;
5505 }
5506 /* Accesses from EL1 NS and EL2 NS are UNDEF for write but allow reads. */
5507 if (isread) {
5508 return CP_ACCESS_OK;
5509 }
5510 return CP_ACCESS_TRAP_UNCATEGORIZED;
5511}
5512
60fb1a87
GB
5513static const ARMCPRegInfo el3_cp_reginfo[] = {
5514 { .name = "SCR_EL3", .state = ARM_CP_STATE_AA64,
5515 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 0,
5516 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.scr_el3),
5517 .resetvalue = 0, .writefn = scr_write },
f80741d1 5518 { .name = "SCR", .type = ARM_CP_ALIAS | ARM_CP_NEWEL,
60fb1a87 5519 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 0,
efe4a274
PM
5520 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
5521 .fieldoffset = offsetoflow32(CPUARMState, cp15.scr_el3),
b061a82b 5522 .writefn = scr_write },
60fb1a87
GB
5523 { .name = "SDER32_EL3", .state = ARM_CP_STATE_AA64,
5524 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 1,
5525 .access = PL3_RW, .resetvalue = 0,
5526 .fieldoffset = offsetof(CPUARMState, cp15.sder) },
5527 { .name = "SDER",
5528 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 1,
5529 .access = PL3_RW, .resetvalue = 0,
5530 .fieldoffset = offsetoflow32(CPUARMState, cp15.sder) },
60fb1a87 5531 { .name = "MVBAR", .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
efe4a274
PM
5532 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
5533 .writefn = vbar_write, .resetvalue = 0,
60fb1a87 5534 .fieldoffset = offsetof(CPUARMState, cp15.mvbar) },
7dd8c9af
FA
5535 { .name = "TTBR0_EL3", .state = ARM_CP_STATE_AA64,
5536 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 0,
f478847f 5537 .access = PL3_RW, .resetvalue = 0,
7dd8c9af 5538 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[3]) },
11f136ee
FA
5539 { .name = "TCR_EL3", .state = ARM_CP_STATE_AA64,
5540 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 2,
6459b94c
PM
5541 .access = PL3_RW,
5542 /* no .writefn needed as this can't cause an ASID change;
811595a2
PM
5543 * we must provide a .raw_writefn and .resetfn because we handle
5544 * reset and migration for the AArch32 TTBCR(S), which might be
5545 * using mask and base_mask.
6459b94c 5546 */
811595a2 5547 .resetfn = vmsa_ttbcr_reset, .raw_writefn = vmsa_ttbcr_raw_write,
11f136ee 5548 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[3]) },
81547d66 5549 { .name = "ELR_EL3", .state = ARM_CP_STATE_AA64,
7a0e58fa 5550 .type = ARM_CP_ALIAS,
81547d66
EI
5551 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 1,
5552 .access = PL3_RW,
5553 .fieldoffset = offsetof(CPUARMState, elr_el[3]) },
f2c30f42 5554 { .name = "ESR_EL3", .state = ARM_CP_STATE_AA64,
f2c30f42
EI
5555 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 2, .opc2 = 0,
5556 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[3]) },
63b60551
EI
5557 { .name = "FAR_EL3", .state = ARM_CP_STATE_AA64,
5558 .opc0 = 3, .opc1 = 6, .crn = 6, .crm = 0, .opc2 = 0,
5559 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[3]) },
81547d66 5560 { .name = "SPSR_EL3", .state = ARM_CP_STATE_AA64,
7a0e58fa 5561 .type = ARM_CP_ALIAS,
81547d66 5562 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 0,
99a99c1f
SB
5563 .access = PL3_RW,
5564 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_MON]) },
a1ba125c
EI
5565 { .name = "VBAR_EL3", .state = ARM_CP_STATE_AA64,
5566 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 0,
5567 .access = PL3_RW, .writefn = vbar_write,
5568 .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[3]),
5569 .resetvalue = 0 },
c6f19164
GB
5570 { .name = "CPTR_EL3", .state = ARM_CP_STATE_AA64,
5571 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 2,
5572 .access = PL3_RW, .accessfn = cptr_access, .resetvalue = 0,
5573 .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[3]) },
4cfb8ad8
PM
5574 { .name = "TPIDR_EL3", .state = ARM_CP_STATE_AA64,
5575 .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 2,
5576 .access = PL3_RW, .resetvalue = 0,
5577 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[3]) },
2179ef95
PM
5578 { .name = "AMAIR_EL3", .state = ARM_CP_STATE_AA64,
5579 .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 3, .opc2 = 0,
5580 .access = PL3_RW, .type = ARM_CP_CONST,
5581 .resetvalue = 0 },
37cd6c24
PM
5582 { .name = "AFSR0_EL3", .state = ARM_CP_STATE_BOTH,
5583 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 0,
5584 .access = PL3_RW, .type = ARM_CP_CONST,
5585 .resetvalue = 0 },
5586 { .name = "AFSR1_EL3", .state = ARM_CP_STATE_BOTH,
5587 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 1,
5588 .access = PL3_RW, .type = ARM_CP_CONST,
5589 .resetvalue = 0 },
43efaa33
PM
5590 { .name = "TLBI_ALLE3IS", .state = ARM_CP_STATE_AA64,
5591 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 0,
5592 .access = PL3_W, .type = ARM_CP_NO_RAW,
5593 .writefn = tlbi_aa64_alle3is_write },
5594 { .name = "TLBI_VAE3IS", .state = ARM_CP_STATE_AA64,
5595 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 1,
5596 .access = PL3_W, .type = ARM_CP_NO_RAW,
5597 .writefn = tlbi_aa64_vae3is_write },
5598 { .name = "TLBI_VALE3IS", .state = ARM_CP_STATE_AA64,
5599 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 5,
5600 .access = PL3_W, .type = ARM_CP_NO_RAW,
5601 .writefn = tlbi_aa64_vae3is_write },
5602 { .name = "TLBI_ALLE3", .state = ARM_CP_STATE_AA64,
5603 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 0,
5604 .access = PL3_W, .type = ARM_CP_NO_RAW,
5605 .writefn = tlbi_aa64_alle3_write },
5606 { .name = "TLBI_VAE3", .state = ARM_CP_STATE_AA64,
5607 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 1,
5608 .access = PL3_W, .type = ARM_CP_NO_RAW,
5609 .writefn = tlbi_aa64_vae3_write },
5610 { .name = "TLBI_VALE3", .state = ARM_CP_STATE_AA64,
5611 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 5,
5612 .access = PL3_W, .type = ARM_CP_NO_RAW,
5613 .writefn = tlbi_aa64_vae3_write },
0f1a3b24
FA
5614 REGINFO_SENTINEL
5615};
5616
e2cce18f
RH
5617#ifndef CONFIG_USER_ONLY
5618/* Test if system register redirection is to occur in the current state. */
5619static bool redirect_for_e2h(CPUARMState *env)
5620{
5621 return arm_current_el(env) == 2 && (arm_hcr_el2_eff(env) & HCR_E2H);
5622}
5623
5624static uint64_t el2_e2h_read(CPUARMState *env, const ARMCPRegInfo *ri)
5625{
5626 CPReadFn *readfn;
5627
5628 if (redirect_for_e2h(env)) {
5629 /* Switch to the saved EL2 version of the register. */
5630 ri = ri->opaque;
5631 readfn = ri->readfn;
5632 } else {
5633 readfn = ri->orig_readfn;
5634 }
5635 if (readfn == NULL) {
5636 readfn = raw_read;
5637 }
5638 return readfn(env, ri);
5639}
5640
5641static void el2_e2h_write(CPUARMState *env, const ARMCPRegInfo *ri,
5642 uint64_t value)
5643{
5644 CPWriteFn *writefn;
5645
5646 if (redirect_for_e2h(env)) {
5647 /* Switch to the saved EL2 version of the register. */
5648 ri = ri->opaque;
5649 writefn = ri->writefn;
5650 } else {
5651 writefn = ri->orig_writefn;
5652 }
5653 if (writefn == NULL) {
5654 writefn = raw_write;
5655 }
5656 writefn(env, ri, value);
5657}
5658
5659static void define_arm_vh_e2h_redirects_aliases(ARMCPU *cpu)
5660{
5661 struct E2HAlias {
5662 uint32_t src_key, dst_key, new_key;
5663 const char *src_name, *dst_name, *new_name;
5664 bool (*feature)(const ARMISARegisters *id);
5665 };
5666
5667#define K(op0, op1, crn, crm, op2) \
5668 ENCODE_AA64_CP_REG(CP_REG_ARM64_SYSREG_CP, crn, crm, op0, op1, op2)
5669
5670 static const struct E2HAlias aliases[] = {
5671 { K(3, 0, 1, 0, 0), K(3, 4, 1, 0, 0), K(3, 5, 1, 0, 0),
5672 "SCTLR", "SCTLR_EL2", "SCTLR_EL12" },
5673 { K(3, 0, 1, 0, 2), K(3, 4, 1, 1, 2), K(3, 5, 1, 0, 2),
5674 "CPACR", "CPTR_EL2", "CPACR_EL12" },
5675 { K(3, 0, 2, 0, 0), K(3, 4, 2, 0, 0), K(3, 5, 2, 0, 0),
5676 "TTBR0_EL1", "TTBR0_EL2", "TTBR0_EL12" },
5677 { K(3, 0, 2, 0, 1), K(3, 4, 2, 0, 1), K(3, 5, 2, 0, 1),
5678 "TTBR1_EL1", "TTBR1_EL2", "TTBR1_EL12" },
5679 { K(3, 0, 2, 0, 2), K(3, 4, 2, 0, 2), K(3, 5, 2, 0, 2),
5680 "TCR_EL1", "TCR_EL2", "TCR_EL12" },
5681 { K(3, 0, 4, 0, 0), K(3, 4, 4, 0, 0), K(3, 5, 4, 0, 0),
5682 "SPSR_EL1", "SPSR_EL2", "SPSR_EL12" },
5683 { K(3, 0, 4, 0, 1), K(3, 4, 4, 0, 1), K(3, 5, 4, 0, 1),
5684 "ELR_EL1", "ELR_EL2", "ELR_EL12" },
5685 { K(3, 0, 5, 1, 0), K(3, 4, 5, 1, 0), K(3, 5, 5, 1, 0),
5686 "AFSR0_EL1", "AFSR0_EL2", "AFSR0_EL12" },
5687 { K(3, 0, 5, 1, 1), K(3, 4, 5, 1, 1), K(3, 5, 5, 1, 1),
5688 "AFSR1_EL1", "AFSR1_EL2", "AFSR1_EL12" },
5689 { K(3, 0, 5, 2, 0), K(3, 4, 5, 2, 0), K(3, 5, 5, 2, 0),
5690 "ESR_EL1", "ESR_EL2", "ESR_EL12" },
5691 { K(3, 0, 6, 0, 0), K(3, 4, 6, 0, 0), K(3, 5, 6, 0, 0),
5692 "FAR_EL1", "FAR_EL2", "FAR_EL12" },
5693 { K(3, 0, 10, 2, 0), K(3, 4, 10, 2, 0), K(3, 5, 10, 2, 0),
5694 "MAIR_EL1", "MAIR_EL2", "MAIR_EL12" },
5695 { K(3, 0, 10, 3, 0), K(3, 4, 10, 3, 0), K(3, 5, 10, 3, 0),
5696 "AMAIR0", "AMAIR_EL2", "AMAIR_EL12" },
5697 { K(3, 0, 12, 0, 0), K(3, 4, 12, 0, 0), K(3, 5, 12, 0, 0),
5698 "VBAR", "VBAR_EL2", "VBAR_EL12" },
5699 { K(3, 0, 13, 0, 1), K(3, 4, 13, 0, 1), K(3, 5, 13, 0, 1),
5700 "CONTEXTIDR_EL1", "CONTEXTIDR_EL2", "CONTEXTIDR_EL12" },
5701 { K(3, 0, 14, 1, 0), K(3, 4, 14, 1, 0), K(3, 5, 14, 1, 0),
5702 "CNTKCTL", "CNTHCTL_EL2", "CNTKCTL_EL12" },
5703
5704 /*
5705 * Note that redirection of ZCR is mentioned in the description
5706 * of ZCR_EL2, and aliasing in the description of ZCR_EL1, but
5707 * not in the summary table.
5708 */
5709 { K(3, 0, 1, 2, 0), K(3, 4, 1, 2, 0), K(3, 5, 1, 2, 0),
5710 "ZCR_EL1", "ZCR_EL2", "ZCR_EL12", isar_feature_aa64_sve },
5711
5712 /* TODO: ARMv8.2-SPE -- PMSCR_EL2 */
5713 /* TODO: ARMv8.4-Trace -- TRFCR_EL2 */
5714 };
5715#undef K
5716
5717 size_t i;
5718
5719 for (i = 0; i < ARRAY_SIZE(aliases); i++) {
5720 const struct E2HAlias *a = &aliases[i];
5721 ARMCPRegInfo *src_reg, *dst_reg;
5722
5723 if (a->feature && !a->feature(&cpu->isar)) {
5724 continue;
5725 }
5726
5727 src_reg = g_hash_table_lookup(cpu->cp_regs, &a->src_key);
5728 dst_reg = g_hash_table_lookup(cpu->cp_regs, &a->dst_key);
5729 g_assert(src_reg != NULL);
5730 g_assert(dst_reg != NULL);
5731
5732 /* Cross-compare names to detect typos in the keys. */
5733 g_assert(strcmp(src_reg->name, a->src_name) == 0);
5734 g_assert(strcmp(dst_reg->name, a->dst_name) == 0);
5735
5736 /* None of the core system registers use opaque; we will. */
5737 g_assert(src_reg->opaque == NULL);
5738
5739 /* Create alias before redirection so we dup the right data. */
5740 if (a->new_key) {
5741 ARMCPRegInfo *new_reg = g_memdup(src_reg, sizeof(ARMCPRegInfo));
5742 uint32_t *new_key = g_memdup(&a->new_key, sizeof(uint32_t));
5743 bool ok;
5744
5745 new_reg->name = a->new_name;
5746 new_reg->type |= ARM_CP_ALIAS;
5747 /* Remove PL1/PL0 access, leaving PL2/PL3 R/W in place. */
5748 new_reg->access &= PL2_RW | PL3_RW;
5749
5750 ok = g_hash_table_insert(cpu->cp_regs, new_key, new_reg);
5751 g_assert(ok);
5752 }
5753
5754 src_reg->opaque = dst_reg;
5755 src_reg->orig_readfn = src_reg->readfn ?: raw_read;
5756 src_reg->orig_writefn = src_reg->writefn ?: raw_write;
5757 if (!src_reg->raw_readfn) {
5758 src_reg->raw_readfn = raw_read;
5759 }
5760 if (!src_reg->raw_writefn) {
5761 src_reg->raw_writefn = raw_write;
5762 }
5763 src_reg->readfn = el2_e2h_read;
5764 src_reg->writefn = el2_e2h_write;
5765 }
5766}
5767#endif
5768
3f208fd7
PM
5769static CPAccessResult ctr_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
5770 bool isread)
7da845b0 5771{
97475a89
RH
5772 int cur_el = arm_current_el(env);
5773
5774 if (cur_el < 2) {
5775 uint64_t hcr = arm_hcr_el2_eff(env);
5776
5777 if (cur_el == 0) {
5778 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
5779 if (!(env->cp15.sctlr_el[2] & SCTLR_UCT)) {
5780 return CP_ACCESS_TRAP_EL2;
5781 }
5782 } else {
5783 if (!(env->cp15.sctlr_el[1] & SCTLR_UCT)) {
5784 return CP_ACCESS_TRAP;
5785 }
5786 if (hcr & HCR_TID2) {
5787 return CP_ACCESS_TRAP_EL2;
5788 }
5789 }
5790 } else if (hcr & HCR_TID2) {
5791 return CP_ACCESS_TRAP_EL2;
5792 }
7da845b0 5793 }
630fcd4d
MZ
5794
5795 if (arm_current_el(env) < 2 && arm_hcr_el2_eff(env) & HCR_TID2) {
5796 return CP_ACCESS_TRAP_EL2;
5797 }
5798
7da845b0
PM
5799 return CP_ACCESS_OK;
5800}
5801
1424ca8d
DM
5802static void oslar_write(CPUARMState *env, const ARMCPRegInfo *ri,
5803 uint64_t value)
5804{
5805 /* Writes to OSLAR_EL1 may update the OS lock status, which can be
5806 * read via a bit in OSLSR_EL1.
5807 */
5808 int oslock;
5809
5810 if (ri->state == ARM_CP_STATE_AA32) {
5811 oslock = (value == 0xC5ACCE55);
5812 } else {
5813 oslock = value & 1;
5814 }
5815
5816 env->cp15.oslsr_el1 = deposit32(env->cp15.oslsr_el1, 1, 1, oslock);
5817}
5818
50300698 5819static const ARMCPRegInfo debug_cp_reginfo[] = {
50300698 5820 /* DBGDRAR, DBGDSAR: always RAZ since we don't implement memory mapped
10aae104
PM
5821 * debug components. The AArch64 version of DBGDRAR is named MDRAR_EL1;
5822 * unlike DBGDRAR it is never accessible from EL0.
5823 * DBGDSAR is deprecated and must RAZ from v8 anyway, so it has no AArch64
5824 * accessor.
50300698
PM
5825 */
5826 { .name = "DBGDRAR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 0,
91b0a238
PM
5827 .access = PL0_R, .accessfn = access_tdra,
5828 .type = ARM_CP_CONST, .resetvalue = 0 },
10aae104
PM
5829 { .name = "MDRAR_EL1", .state = ARM_CP_STATE_AA64,
5830 .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
91b0a238
PM
5831 .access = PL1_R, .accessfn = access_tdra,
5832 .type = ARM_CP_CONST, .resetvalue = 0 },
50300698 5833 { .name = "DBGDSAR", .cp = 14, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
91b0a238
PM
5834 .access = PL0_R, .accessfn = access_tdra,
5835 .type = ARM_CP_CONST, .resetvalue = 0 },
17a9eb53 5836 /* Monitor debug system control register; the 32-bit alias is DBGDSCRext. */
10aae104
PM
5837 { .name = "MDSCR_EL1", .state = ARM_CP_STATE_BOTH,
5838 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
d6c8cf81 5839 .access = PL1_RW, .accessfn = access_tda,
0e5e8935
PM
5840 .fieldoffset = offsetof(CPUARMState, cp15.mdscr_el1),
5841 .resetvalue = 0 },
5e8b12ff
PM
5842 /* MDCCSR_EL0, aka DBGDSCRint. This is a read-only mirror of MDSCR_EL1.
5843 * We don't implement the configurable EL0 access.
5844 */
5845 { .name = "MDCCSR_EL0", .state = ARM_CP_STATE_BOTH,
5846 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
7a0e58fa 5847 .type = ARM_CP_ALIAS,
d6c8cf81 5848 .access = PL1_R, .accessfn = access_tda,
b061a82b 5849 .fieldoffset = offsetof(CPUARMState, cp15.mdscr_el1), },
10aae104
PM
5850 { .name = "OSLAR_EL1", .state = ARM_CP_STATE_BOTH,
5851 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 4,
1424ca8d 5852 .access = PL1_W, .type = ARM_CP_NO_RAW,
187f678d 5853 .accessfn = access_tdosa,
1424ca8d
DM
5854 .writefn = oslar_write },
5855 { .name = "OSLSR_EL1", .state = ARM_CP_STATE_BOTH,
5856 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 4,
5857 .access = PL1_R, .resetvalue = 10,
187f678d 5858 .accessfn = access_tdosa,
1424ca8d 5859 .fieldoffset = offsetof(CPUARMState, cp15.oslsr_el1) },
5e8b12ff
PM
5860 /* Dummy OSDLR_EL1: 32-bit Linux will read this */
5861 { .name = "OSDLR_EL1", .state = ARM_CP_STATE_BOTH,
5862 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 4,
187f678d
PM
5863 .access = PL1_RW, .accessfn = access_tdosa,
5864 .type = ARM_CP_NOP },
5e8b12ff
PM
5865 /* Dummy DBGVCR: Linux wants to clear this on startup, but we don't
5866 * implement vector catch debug events yet.
5867 */
5868 { .name = "DBGVCR",
5869 .cp = 14, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
d6c8cf81
PM
5870 .access = PL1_RW, .accessfn = access_tda,
5871 .type = ARM_CP_NOP },
4d2ec4da
PM
5872 /* Dummy DBGVCR32_EL2 (which is only for a 64-bit hypervisor
5873 * to save and restore a 32-bit guest's DBGVCR)
5874 */
5875 { .name = "DBGVCR32_EL2", .state = ARM_CP_STATE_AA64,
5876 .opc0 = 2, .opc1 = 4, .crn = 0, .crm = 7, .opc2 = 0,
5877 .access = PL2_RW, .accessfn = access_tda,
5878 .type = ARM_CP_NOP },
5dbdc434
PM
5879 /* Dummy MDCCINT_EL1, since we don't implement the Debug Communications
5880 * Channel but Linux may try to access this register. The 32-bit
5881 * alias is DBGDCCINT.
5882 */
5883 { .name = "MDCCINT_EL1", .state = ARM_CP_STATE_BOTH,
5884 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0,
5885 .access = PL1_RW, .accessfn = access_tda,
5886 .type = ARM_CP_NOP },
50300698
PM
5887 REGINFO_SENTINEL
5888};
5889
5890static const ARMCPRegInfo debug_lpae_cp_reginfo[] = {
5891 /* 64 bit access versions of the (dummy) debug registers */
5892 { .name = "DBGDRAR", .cp = 14, .crm = 1, .opc1 = 0,
5893 .access = PL0_R, .type = ARM_CP_CONST|ARM_CP_64BIT, .resetvalue = 0 },
5894 { .name = "DBGDSAR", .cp = 14, .crm = 2, .opc1 = 0,
5895 .access = PL0_R, .type = ARM_CP_CONST|ARM_CP_64BIT, .resetvalue = 0 },
5896 REGINFO_SENTINEL
5897};
5898
60eed086
RH
5899/* Return the exception level to which exceptions should be taken
5900 * via SVEAccessTrap. If an exception should be routed through
5901 * AArch64.AdvSIMDFPAccessTrap, return 0; fp_exception_el should
5902 * take care of raising that exception.
5903 * C.f. the ARM pseudocode function CheckSVEEnabled.
5be5e8ed 5904 */
ced31551 5905int sve_exception_el(CPUARMState *env, int el)
5be5e8ed
RH
5906{
5907#ifndef CONFIG_USER_ONLY
c2ddb7cf
RH
5908 uint64_t hcr_el2 = arm_hcr_el2_eff(env);
5909
5910 if (el <= 1 && (hcr_el2 & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
60eed086
RH
5911 bool disabled = false;
5912
5913 /* The CPACR.ZEN controls traps to EL1:
5914 * 0, 2 : trap EL0 and EL1 accesses
5915 * 1 : trap only EL0 accesses
5916 * 3 : trap no accesses
5917 */
5918 if (!extract32(env->cp15.cpacr_el1, 16, 1)) {
5919 disabled = true;
5920 } else if (!extract32(env->cp15.cpacr_el1, 17, 1)) {
2de7ace2 5921 disabled = el == 0;
5be5e8ed 5922 }
60eed086
RH
5923 if (disabled) {
5924 /* route_to_el2 */
c2ddb7cf 5925 return hcr_el2 & HCR_TGE ? 2 : 1;
5be5e8ed 5926 }
5be5e8ed 5927
60eed086
RH
5928 /* Check CPACR.FPEN. */
5929 if (!extract32(env->cp15.cpacr_el1, 20, 1)) {
5930 disabled = true;
5931 } else if (!extract32(env->cp15.cpacr_el1, 21, 1)) {
2de7ace2 5932 disabled = el == 0;
5be5e8ed 5933 }
60eed086
RH
5934 if (disabled) {
5935 return 0;
5be5e8ed 5936 }
5be5e8ed
RH
5937 }
5938
60eed086
RH
5939 /* CPTR_EL2. Since TZ and TFP are positive,
5940 * they will be zero when EL2 is not present.
5941 */
2de7ace2 5942 if (el <= 2 && !arm_is_secure_below_el3(env)) {
60eed086
RH
5943 if (env->cp15.cptr_el[2] & CPTR_TZ) {
5944 return 2;
5945 }
5946 if (env->cp15.cptr_el[2] & CPTR_TFP) {
5947 return 0;
5948 }
5be5e8ed
RH
5949 }
5950
60eed086
RH
5951 /* CPTR_EL3. Since EZ is negative we must check for EL3. */
5952 if (arm_feature(env, ARM_FEATURE_EL3)
5953 && !(env->cp15.cptr_el[3] & CPTR_EZ)) {
5be5e8ed
RH
5954 return 3;
5955 }
5956#endif
5957 return 0;
5958}
5959
0df9142d
AJ
5960static uint32_t sve_zcr_get_valid_len(ARMCPU *cpu, uint32_t start_len)
5961{
6e553f2a 5962 uint32_t end_len;
0df9142d 5963
6e553f2a
RH
5964 end_len = start_len &= 0xf;
5965 if (!test_bit(start_len, cpu->sve_vq_map)) {
5966 end_len = find_last_bit(cpu->sve_vq_map, start_len);
5967 assert(end_len < start_len);
5968 }
5969 return end_len;
0df9142d
AJ
5970}
5971
0ab5953b
RH
5972/*
5973 * Given that SVE is enabled, return the vector length for EL.
5974 */
ced31551 5975uint32_t sve_zcr_len_for_el(CPUARMState *env, int el)
0ab5953b 5976{
2fc0cc0e 5977 ARMCPU *cpu = env_archcpu(env);
0ab5953b
RH
5978 uint32_t zcr_len = cpu->sve_max_vq - 1;
5979
5980 if (el <= 1) {
5981 zcr_len = MIN(zcr_len, 0xf & (uint32_t)env->vfp.zcr_el[1]);
5982 }
6a02a732 5983 if (el <= 2 && arm_feature(env, ARM_FEATURE_EL2)) {
0ab5953b
RH
5984 zcr_len = MIN(zcr_len, 0xf & (uint32_t)env->vfp.zcr_el[2]);
5985 }
6a02a732 5986 if (arm_feature(env, ARM_FEATURE_EL3)) {
0ab5953b
RH
5987 zcr_len = MIN(zcr_len, 0xf & (uint32_t)env->vfp.zcr_el[3]);
5988 }
0df9142d
AJ
5989
5990 return sve_zcr_get_valid_len(cpu, zcr_len);
0ab5953b
RH
5991}
5992
5be5e8ed
RH
5993static void zcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
5994 uint64_t value)
5995{
0ab5953b
RH
5996 int cur_el = arm_current_el(env);
5997 int old_len = sve_zcr_len_for_el(env, cur_el);
5998 int new_len;
5999
5be5e8ed 6000 /* Bits other than [3:0] are RAZ/WI. */
7b351d98 6001 QEMU_BUILD_BUG_ON(ARM_MAX_VQ > 16);
5be5e8ed 6002 raw_write(env, ri, value & 0xf);
0ab5953b
RH
6003
6004 /*
6005 * Because we arrived here, we know both FP and SVE are enabled;
6006 * otherwise we would have trapped access to the ZCR_ELn register.
6007 */
6008 new_len = sve_zcr_len_for_el(env, cur_el);
6009 if (new_len < old_len) {
6010 aarch64_sve_narrow_vq(env, new_len + 1);
6011 }
5be5e8ed
RH
6012}
6013
6014static const ARMCPRegInfo zcr_el1_reginfo = {
6015 .name = "ZCR_EL1", .state = ARM_CP_STATE_AA64,
6016 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 0,
11d7870b 6017 .access = PL1_RW, .type = ARM_CP_SVE,
5be5e8ed
RH
6018 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[1]),
6019 .writefn = zcr_write, .raw_writefn = raw_write
6020};
6021
6022static const ARMCPRegInfo zcr_el2_reginfo = {
6023 .name = "ZCR_EL2", .state = ARM_CP_STATE_AA64,
6024 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 0,
11d7870b 6025 .access = PL2_RW, .type = ARM_CP_SVE,
5be5e8ed
RH
6026 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[2]),
6027 .writefn = zcr_write, .raw_writefn = raw_write
6028};
6029
6030static const ARMCPRegInfo zcr_no_el2_reginfo = {
6031 .name = "ZCR_EL2", .state = ARM_CP_STATE_AA64,
6032 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 0,
11d7870b 6033 .access = PL2_RW, .type = ARM_CP_SVE,
5be5e8ed
RH
6034 .readfn = arm_cp_read_zero, .writefn = arm_cp_write_ignore
6035};
6036
6037static const ARMCPRegInfo zcr_el3_reginfo = {
6038 .name = "ZCR_EL3", .state = ARM_CP_STATE_AA64,
6039 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 2, .opc2 = 0,
11d7870b 6040 .access = PL3_RW, .type = ARM_CP_SVE,
5be5e8ed
RH
6041 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[3]),
6042 .writefn = zcr_write, .raw_writefn = raw_write
6043};
6044
9ee98ce8
PM
6045void hw_watchpoint_update(ARMCPU *cpu, int n)
6046{
6047 CPUARMState *env = &cpu->env;
6048 vaddr len = 0;
6049 vaddr wvr = env->cp15.dbgwvr[n];
6050 uint64_t wcr = env->cp15.dbgwcr[n];
6051 int mask;
6052 int flags = BP_CPU | BP_STOP_BEFORE_ACCESS;
6053
6054 if (env->cpu_watchpoint[n]) {
6055 cpu_watchpoint_remove_by_ref(CPU(cpu), env->cpu_watchpoint[n]);
6056 env->cpu_watchpoint[n] = NULL;
6057 }
6058
6059 if (!extract64(wcr, 0, 1)) {
6060 /* E bit clear : watchpoint disabled */
6061 return;
6062 }
6063
6064 switch (extract64(wcr, 3, 2)) {
6065 case 0:
6066 /* LSC 00 is reserved and must behave as if the wp is disabled */
6067 return;
6068 case 1:
6069 flags |= BP_MEM_READ;
6070 break;
6071 case 2:
6072 flags |= BP_MEM_WRITE;
6073 break;
6074 case 3:
6075 flags |= BP_MEM_ACCESS;
6076 break;
6077 }
6078
6079 /* Attempts to use both MASK and BAS fields simultaneously are
6080 * CONSTRAINED UNPREDICTABLE; we opt to ignore BAS in this case,
6081 * thus generating a watchpoint for every byte in the masked region.
6082 */
6083 mask = extract64(wcr, 24, 4);
6084 if (mask == 1 || mask == 2) {
6085 /* Reserved values of MASK; we must act as if the mask value was
6086 * some non-reserved value, or as if the watchpoint were disabled.
6087 * We choose the latter.
6088 */
6089 return;
6090 } else if (mask) {
6091 /* Watchpoint covers an aligned area up to 2GB in size */
6092 len = 1ULL << mask;
6093 /* If masked bits in WVR are not zero it's CONSTRAINED UNPREDICTABLE
6094 * whether the watchpoint fires when the unmasked bits match; we opt
6095 * to generate the exceptions.
6096 */
6097 wvr &= ~(len - 1);
6098 } else {
6099 /* Watchpoint covers bytes defined by the byte address select bits */
6100 int bas = extract64(wcr, 5, 8);
6101 int basstart;
6102
6103 if (bas == 0) {
6104 /* This must act as if the watchpoint is disabled */
6105 return;
6106 }
6107
6108 if (extract64(wvr, 2, 1)) {
6109 /* Deprecated case of an only 4-aligned address. BAS[7:4] are
6110 * ignored, and BAS[3:0] define which bytes to watch.
6111 */
6112 bas &= 0xf;
6113 }
6114 /* The BAS bits are supposed to be programmed to indicate a contiguous
6115 * range of bytes. Otherwise it is CONSTRAINED UNPREDICTABLE whether
6116 * we fire for each byte in the word/doubleword addressed by the WVR.
6117 * We choose to ignore any non-zero bits after the first range of 1s.
6118 */
6119 basstart = ctz32(bas);
6120 len = cto32(bas >> basstart);
6121 wvr += basstart;
6122 }
6123
6124 cpu_watchpoint_insert(CPU(cpu), wvr, len, flags,
6125 &env->cpu_watchpoint[n]);
6126}
6127
6128void hw_watchpoint_update_all(ARMCPU *cpu)
6129{
6130 int i;
6131 CPUARMState *env = &cpu->env;
6132
6133 /* Completely clear out existing QEMU watchpoints and our array, to
6134 * avoid possible stale entries following migration load.
6135 */
6136 cpu_watchpoint_remove_all(CPU(cpu), BP_CPU);
6137 memset(env->cpu_watchpoint, 0, sizeof(env->cpu_watchpoint));
6138
6139 for (i = 0; i < ARRAY_SIZE(cpu->env.cpu_watchpoint); i++) {
6140 hw_watchpoint_update(cpu, i);
6141 }
6142}
6143
6144static void dbgwvr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6145 uint64_t value)
6146{
2fc0cc0e 6147 ARMCPU *cpu = env_archcpu(env);
9ee98ce8
PM
6148 int i = ri->crm;
6149
6150 /* Bits [63:49] are hardwired to the value of bit [48]; that is, the
6151 * register reads and behaves as if values written are sign extended.
6152 * Bits [1:0] are RES0.
6153 */
6154 value = sextract64(value, 0, 49) & ~3ULL;
6155
6156 raw_write(env, ri, value);
6157 hw_watchpoint_update(cpu, i);
6158}
6159
6160static void dbgwcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6161 uint64_t value)
6162{
2fc0cc0e 6163 ARMCPU *cpu = env_archcpu(env);
9ee98ce8
PM
6164 int i = ri->crm;
6165
6166 raw_write(env, ri, value);
6167 hw_watchpoint_update(cpu, i);
6168}
6169
46747d15
PM
6170void hw_breakpoint_update(ARMCPU *cpu, int n)
6171{
6172 CPUARMState *env = &cpu->env;
6173 uint64_t bvr = env->cp15.dbgbvr[n];
6174 uint64_t bcr = env->cp15.dbgbcr[n];
6175 vaddr addr;
6176 int bt;
6177 int flags = BP_CPU;
6178
6179 if (env->cpu_breakpoint[n]) {
6180 cpu_breakpoint_remove_by_ref(CPU(cpu), env->cpu_breakpoint[n]);
6181 env->cpu_breakpoint[n] = NULL;
6182 }
6183
6184 if (!extract64(bcr, 0, 1)) {
6185 /* E bit clear : watchpoint disabled */
6186 return;
6187 }
6188
6189 bt = extract64(bcr, 20, 4);
6190
6191 switch (bt) {
6192 case 4: /* unlinked address mismatch (reserved if AArch64) */
6193 case 5: /* linked address mismatch (reserved if AArch64) */
6194 qemu_log_mask(LOG_UNIMP,
0221c8fd 6195 "arm: address mismatch breakpoint types not implemented\n");
46747d15
PM
6196 return;
6197 case 0: /* unlinked address match */
6198 case 1: /* linked address match */
6199 {
6200 /* Bits [63:49] are hardwired to the value of bit [48]; that is,
6201 * we behave as if the register was sign extended. Bits [1:0] are
6202 * RES0. The BAS field is used to allow setting breakpoints on 16
6203 * bit wide instructions; it is CONSTRAINED UNPREDICTABLE whether
6204 * a bp will fire if the addresses covered by the bp and the addresses
6205 * covered by the insn overlap but the insn doesn't start at the
6206 * start of the bp address range. We choose to require the insn and
6207 * the bp to have the same address. The constraints on writing to
6208 * BAS enforced in dbgbcr_write mean we have only four cases:
6209 * 0b0000 => no breakpoint
6210 * 0b0011 => breakpoint on addr
6211 * 0b1100 => breakpoint on addr + 2
6212 * 0b1111 => breakpoint on addr
6213 * See also figure D2-3 in the v8 ARM ARM (DDI0487A.c).
6214 */
6215 int bas = extract64(bcr, 5, 4);
6216 addr = sextract64(bvr, 0, 49) & ~3ULL;
6217 if (bas == 0) {
6218 return;
6219 }
6220 if (bas == 0xc) {
6221 addr += 2;
6222 }
6223 break;
6224 }
6225 case 2: /* unlinked context ID match */
6226 case 8: /* unlinked VMID match (reserved if no EL2) */
6227 case 10: /* unlinked context ID and VMID match (reserved if no EL2) */
6228 qemu_log_mask(LOG_UNIMP,
0221c8fd 6229 "arm: unlinked context breakpoint types not implemented\n");
46747d15
PM
6230 return;
6231 case 9: /* linked VMID match (reserved if no EL2) */
6232 case 11: /* linked context ID and VMID match (reserved if no EL2) */
6233 case 3: /* linked context ID match */
6234 default:
6235 /* We must generate no events for Linked context matches (unless
6236 * they are linked to by some other bp/wp, which is handled in
6237 * updates for the linking bp/wp). We choose to also generate no events
6238 * for reserved values.
6239 */
6240 return;
6241 }
6242
6243 cpu_breakpoint_insert(CPU(cpu), addr, flags, &env->cpu_breakpoint[n]);
6244}
6245
6246void hw_breakpoint_update_all(ARMCPU *cpu)
6247{
6248 int i;
6249 CPUARMState *env = &cpu->env;
6250
6251 /* Completely clear out existing QEMU breakpoints and our array, to
6252 * avoid possible stale entries following migration load.
6253 */
6254 cpu_breakpoint_remove_all(CPU(cpu), BP_CPU);
6255 memset(env->cpu_breakpoint, 0, sizeof(env->cpu_breakpoint));
6256
6257 for (i = 0; i < ARRAY_SIZE(cpu->env.cpu_breakpoint); i++) {
6258 hw_breakpoint_update(cpu, i);
6259 }
6260}
6261
6262static void dbgbvr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6263 uint64_t value)
6264{
2fc0cc0e 6265 ARMCPU *cpu = env_archcpu(env);
46747d15
PM
6266 int i = ri->crm;
6267
6268 raw_write(env, ri, value);
6269 hw_breakpoint_update(cpu, i);
6270}
6271
6272static void dbgbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6273 uint64_t value)
6274{
2fc0cc0e 6275 ARMCPU *cpu = env_archcpu(env);
46747d15
PM
6276 int i = ri->crm;
6277
6278 /* BAS[3] is a read-only copy of BAS[2], and BAS[1] a read-only
6279 * copy of BAS[0].
6280 */
6281 value = deposit64(value, 6, 1, extract64(value, 5, 1));
6282 value = deposit64(value, 8, 1, extract64(value, 7, 1));
6283
6284 raw_write(env, ri, value);
6285 hw_breakpoint_update(cpu, i);
6286}
6287
50300698 6288static void define_debug_regs(ARMCPU *cpu)
0b45451e 6289{
50300698
PM
6290 /* Define v7 and v8 architectural debug registers.
6291 * These are just dummy implementations for now.
0b45451e
PM
6292 */
6293 int i;
3ff6fc91 6294 int wrps, brps, ctx_cmps;
48eb3ae6
PM
6295 ARMCPRegInfo dbgdidr = {
6296 .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0,
d6c8cf81 6297 .access = PL0_R, .accessfn = access_tda,
4426d361 6298 .type = ARM_CP_CONST, .resetvalue = cpu->isar.dbgdidr,
48eb3ae6
PM
6299 };
6300
3ff6fc91 6301 /* Note that all these register fields hold "number of Xs minus 1". */
88ce6c6e
PM
6302 brps = arm_num_brps(cpu);
6303 wrps = arm_num_wrps(cpu);
6304 ctx_cmps = arm_num_ctx_cmps(cpu);
3ff6fc91
PM
6305
6306 assert(ctx_cmps <= brps);
48eb3ae6 6307
48eb3ae6 6308 define_one_arm_cp_reg(cpu, &dbgdidr);
50300698
PM
6309 define_arm_cp_regs(cpu, debug_cp_reginfo);
6310
6311 if (arm_feature(&cpu->env, ARM_FEATURE_LPAE)) {
6312 define_arm_cp_regs(cpu, debug_lpae_cp_reginfo);
6313 }
6314
88ce6c6e 6315 for (i = 0; i < brps; i++) {
0b45451e 6316 ARMCPRegInfo dbgregs[] = {
10aae104
PM
6317 { .name = "DBGBVR", .state = ARM_CP_STATE_BOTH,
6318 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 4,
d6c8cf81 6319 .access = PL1_RW, .accessfn = access_tda,
46747d15
PM
6320 .fieldoffset = offsetof(CPUARMState, cp15.dbgbvr[i]),
6321 .writefn = dbgbvr_write, .raw_writefn = raw_write
6322 },
10aae104
PM
6323 { .name = "DBGBCR", .state = ARM_CP_STATE_BOTH,
6324 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 5,
d6c8cf81 6325 .access = PL1_RW, .accessfn = access_tda,
46747d15
PM
6326 .fieldoffset = offsetof(CPUARMState, cp15.dbgbcr[i]),
6327 .writefn = dbgbcr_write, .raw_writefn = raw_write
6328 },
48eb3ae6
PM
6329 REGINFO_SENTINEL
6330 };
6331 define_arm_cp_regs(cpu, dbgregs);
6332 }
6333
88ce6c6e 6334 for (i = 0; i < wrps; i++) {
48eb3ae6 6335 ARMCPRegInfo dbgregs[] = {
10aae104
PM
6336 { .name = "DBGWVR", .state = ARM_CP_STATE_BOTH,
6337 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 6,
d6c8cf81 6338 .access = PL1_RW, .accessfn = access_tda,
9ee98ce8
PM
6339 .fieldoffset = offsetof(CPUARMState, cp15.dbgwvr[i]),
6340 .writefn = dbgwvr_write, .raw_writefn = raw_write
6341 },
10aae104
PM
6342 { .name = "DBGWCR", .state = ARM_CP_STATE_BOTH,
6343 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 7,
d6c8cf81 6344 .access = PL1_RW, .accessfn = access_tda,
9ee98ce8
PM
6345 .fieldoffset = offsetof(CPUARMState, cp15.dbgwcr[i]),
6346 .writefn = dbgwcr_write, .raw_writefn = raw_write
6347 },
6348 REGINFO_SENTINEL
0b45451e
PM
6349 };
6350 define_arm_cp_regs(cpu, dbgregs);
6351 }
6352}
6353
24183fb6
PM
6354static void define_pmu_regs(ARMCPU *cpu)
6355{
6356 /*
6357 * v7 performance monitor control register: same implementor
6358 * field as main ID register, and we implement four counters in
6359 * addition to the cycle count register.
6360 */
6361 unsigned int i, pmcrn = 4;
6362 ARMCPRegInfo pmcr = {
6363 .name = "PMCR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 0,
6364 .access = PL0_RW,
6365 .type = ARM_CP_IO | ARM_CP_ALIAS,
6366 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcr),
6367 .accessfn = pmreg_access, .writefn = pmcr_write,
6368 .raw_writefn = raw_write,
6369 };
6370 ARMCPRegInfo pmcr64 = {
6371 .name = "PMCR_EL0", .state = ARM_CP_STATE_AA64,
6372 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 0,
6373 .access = PL0_RW, .accessfn = pmreg_access,
6374 .type = ARM_CP_IO,
6375 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcr),
62d96ff4
PM
6376 .resetvalue = (cpu->midr & 0xff000000) | (pmcrn << PMCRN_SHIFT) |
6377 PMCRLC,
24183fb6
PM
6378 .writefn = pmcr_write, .raw_writefn = raw_write,
6379 };
6380 define_one_arm_cp_reg(cpu, &pmcr);
6381 define_one_arm_cp_reg(cpu, &pmcr64);
6382 for (i = 0; i < pmcrn; i++) {
6383 char *pmevcntr_name = g_strdup_printf("PMEVCNTR%d", i);
6384 char *pmevcntr_el0_name = g_strdup_printf("PMEVCNTR%d_EL0", i);
6385 char *pmevtyper_name = g_strdup_printf("PMEVTYPER%d", i);
6386 char *pmevtyper_el0_name = g_strdup_printf("PMEVTYPER%d_EL0", i);
6387 ARMCPRegInfo pmev_regs[] = {
6388 { .name = pmevcntr_name, .cp = 15, .crn = 14,
6389 .crm = 8 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7,
6390 .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS,
6391 .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn,
6392 .accessfn = pmreg_access },
6393 { .name = pmevcntr_el0_name, .state = ARM_CP_STATE_AA64,
6394 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 8 | (3 & (i >> 3)),
6395 .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access,
6396 .type = ARM_CP_IO,
6397 .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn,
6398 .raw_readfn = pmevcntr_rawread,
6399 .raw_writefn = pmevcntr_rawwrite },
6400 { .name = pmevtyper_name, .cp = 15, .crn = 14,
6401 .crm = 12 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7,
6402 .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS,
6403 .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn,
6404 .accessfn = pmreg_access },
6405 { .name = pmevtyper_el0_name, .state = ARM_CP_STATE_AA64,
6406 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 12 | (3 & (i >> 3)),
6407 .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access,
6408 .type = ARM_CP_IO,
6409 .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn,
6410 .raw_writefn = pmevtyper_rawwrite },
6411 REGINFO_SENTINEL
6412 };
6413 define_arm_cp_regs(cpu, pmev_regs);
6414 g_free(pmevcntr_name);
6415 g_free(pmevcntr_el0_name);
6416 g_free(pmevtyper_name);
6417 g_free(pmevtyper_el0_name);
6418 }
a6179538 6419 if (cpu_isar_feature(aa32_pmu_8_1, cpu)) {
24183fb6
PM
6420 ARMCPRegInfo v81_pmu_regs[] = {
6421 { .name = "PMCEID2", .state = ARM_CP_STATE_AA32,
6422 .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 4,
6423 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6424 .resetvalue = extract64(cpu->pmceid0, 32, 32) },
6425 { .name = "PMCEID3", .state = ARM_CP_STATE_AA32,
6426 .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 5,
6427 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6428 .resetvalue = extract64(cpu->pmceid1, 32, 32) },
6429 REGINFO_SENTINEL
6430 };
6431 define_arm_cp_regs(cpu, v81_pmu_regs);
6432 }
15dd1ebd
PM
6433 if (cpu_isar_feature(any_pmu_8_4, cpu)) {
6434 static const ARMCPRegInfo v84_pmmir = {
6435 .name = "PMMIR_EL1", .state = ARM_CP_STATE_BOTH,
6436 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 6,
6437 .access = PL1_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6438 .resetvalue = 0
6439 };
6440 define_one_arm_cp_reg(cpu, &v84_pmmir);
6441 }
24183fb6
PM
6442}
6443
96a8b92e
PM
6444/* We don't know until after realize whether there's a GICv3
6445 * attached, and that is what registers the gicv3 sysregs.
6446 * So we have to fill in the GIC fields in ID_PFR/ID_PFR1_EL1/ID_AA64PFR0_EL1
6447 * at runtime.
6448 */
6449static uint64_t id_pfr1_read(CPUARMState *env, const ARMCPRegInfo *ri)
6450{
2fc0cc0e 6451 ARMCPU *cpu = env_archcpu(env);
96a8b92e
PM
6452 uint64_t pfr1 = cpu->id_pfr1;
6453
6454 if (env->gicv3state) {
6455 pfr1 |= 1 << 28;
6456 }
6457 return pfr1;
6458}
6459
6460static uint64_t id_aa64pfr0_read(CPUARMState *env, const ARMCPRegInfo *ri)
6461{
2fc0cc0e 6462 ARMCPU *cpu = env_archcpu(env);
47576b94 6463 uint64_t pfr0 = cpu->isar.id_aa64pfr0;
96a8b92e
PM
6464
6465 if (env->gicv3state) {
6466 pfr0 |= 1 << 24;
6467 }
6468 return pfr0;
6469}
6470
2d7137c1
RH
6471/* Shared logic between LORID and the rest of the LOR* registers.
6472 * Secure state has already been delt with.
6473 */
6474static CPAccessResult access_lor_ns(CPUARMState *env)
6475{
6476 int el = arm_current_el(env);
6477
6478 if (el < 2 && (arm_hcr_el2_eff(env) & HCR_TLOR)) {
6479 return CP_ACCESS_TRAP_EL2;
6480 }
6481 if (el < 3 && (env->cp15.scr_el3 & SCR_TLOR)) {
6482 return CP_ACCESS_TRAP_EL3;
6483 }
6484 return CP_ACCESS_OK;
6485}
6486
6487static CPAccessResult access_lorid(CPUARMState *env, const ARMCPRegInfo *ri,
6488 bool isread)
6489{
6490 if (arm_is_secure_below_el3(env)) {
6491 /* Access ok in secure mode. */
6492 return CP_ACCESS_OK;
6493 }
6494 return access_lor_ns(env);
6495}
6496
6497static CPAccessResult access_lor_other(CPUARMState *env,
6498 const ARMCPRegInfo *ri, bool isread)
6499{
6500 if (arm_is_secure_below_el3(env)) {
6501 /* Access denied in secure mode. */
6502 return CP_ACCESS_TRAP;
6503 }
6504 return access_lor_ns(env);
6505}
6506
d8564ee4
RH
6507/*
6508 * A trivial implementation of ARMv8.1-LOR leaves all of these
6509 * registers fixed at 0, which indicates that there are zero
6510 * supported Limited Ordering regions.
6511 */
6512static const ARMCPRegInfo lor_reginfo[] = {
6513 { .name = "LORSA_EL1", .state = ARM_CP_STATE_AA64,
6514 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 0,
6515 .access = PL1_RW, .accessfn = access_lor_other,
6516 .type = ARM_CP_CONST, .resetvalue = 0 },
6517 { .name = "LOREA_EL1", .state = ARM_CP_STATE_AA64,
6518 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 1,
6519 .access = PL1_RW, .accessfn = access_lor_other,
6520 .type = ARM_CP_CONST, .resetvalue = 0 },
6521 { .name = "LORN_EL1", .state = ARM_CP_STATE_AA64,
6522 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 2,
6523 .access = PL1_RW, .accessfn = access_lor_other,
6524 .type = ARM_CP_CONST, .resetvalue = 0 },
6525 { .name = "LORC_EL1", .state = ARM_CP_STATE_AA64,
6526 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 3,
6527 .access = PL1_RW, .accessfn = access_lor_other,
6528 .type = ARM_CP_CONST, .resetvalue = 0 },
6529 { .name = "LORID_EL1", .state = ARM_CP_STATE_AA64,
6530 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 7,
6531 .access = PL1_R, .accessfn = access_lorid,
6532 .type = ARM_CP_CONST, .resetvalue = 0 },
6533 REGINFO_SENTINEL
6534};
6535
967aa94f
RH
6536#ifdef TARGET_AARCH64
6537static CPAccessResult access_pauth(CPUARMState *env, const ARMCPRegInfo *ri,
6538 bool isread)
6539{
6540 int el = arm_current_el(env);
6541
6542 if (el < 2 &&
6543 arm_feature(env, ARM_FEATURE_EL2) &&
6544 !(arm_hcr_el2_eff(env) & HCR_APK)) {
6545 return CP_ACCESS_TRAP_EL2;
6546 }
6547 if (el < 3 &&
6548 arm_feature(env, ARM_FEATURE_EL3) &&
6549 !(env->cp15.scr_el3 & SCR_APK)) {
6550 return CP_ACCESS_TRAP_EL3;
6551 }
6552 return CP_ACCESS_OK;
6553}
6554
6555static const ARMCPRegInfo pauth_reginfo[] = {
6556 { .name = "APDAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6557 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 0,
6558 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 6559 .fieldoffset = offsetof(CPUARMState, keys.apda.lo) },
967aa94f
RH
6560 { .name = "APDAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6561 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 1,
6562 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 6563 .fieldoffset = offsetof(CPUARMState, keys.apda.hi) },
967aa94f
RH
6564 { .name = "APDBKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6565 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 2,
6566 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 6567 .fieldoffset = offsetof(CPUARMState, keys.apdb.lo) },
967aa94f
RH
6568 { .name = "APDBKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6569 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 3,
6570 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 6571 .fieldoffset = offsetof(CPUARMState, keys.apdb.hi) },
967aa94f
RH
6572 { .name = "APGAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6573 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 0,
6574 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 6575 .fieldoffset = offsetof(CPUARMState, keys.apga.lo) },
967aa94f
RH
6576 { .name = "APGAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6577 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 1,
6578 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 6579 .fieldoffset = offsetof(CPUARMState, keys.apga.hi) },
967aa94f
RH
6580 { .name = "APIAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6581 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 0,
6582 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 6583 .fieldoffset = offsetof(CPUARMState, keys.apia.lo) },
967aa94f
RH
6584 { .name = "APIAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6585 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 1,
6586 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 6587 .fieldoffset = offsetof(CPUARMState, keys.apia.hi) },
967aa94f
RH
6588 { .name = "APIBKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6589 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 2,
6590 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 6591 .fieldoffset = offsetof(CPUARMState, keys.apib.lo) },
967aa94f
RH
6592 { .name = "APIBKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6593 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 3,
6594 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 6595 .fieldoffset = offsetof(CPUARMState, keys.apib.hi) },
967aa94f
RH
6596 REGINFO_SENTINEL
6597};
de390645
RH
6598
6599static uint64_t rndr_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
6600{
6601 Error *err = NULL;
6602 uint64_t ret;
6603
6604 /* Success sets NZCV = 0000. */
6605 env->NF = env->CF = env->VF = 0, env->ZF = 1;
6606
6607 if (qemu_guest_getrandom(&ret, sizeof(ret), &err) < 0) {
6608 /*
6609 * ??? Failed, for unknown reasons in the crypto subsystem.
6610 * The best we can do is log the reason and return the
6611 * timed-out indication to the guest. There is no reason
6612 * we know to expect this failure to be transitory, so the
6613 * guest may well hang retrying the operation.
6614 */
6615 qemu_log_mask(LOG_UNIMP, "%s: Crypto failure: %s",
6616 ri->name, error_get_pretty(err));
6617 error_free(err);
6618
6619 env->ZF = 0; /* NZCF = 0100 */
6620 return 0;
6621 }
6622 return ret;
6623}
6624
6625/* We do not support re-seeding, so the two registers operate the same. */
6626static const ARMCPRegInfo rndr_reginfo[] = {
6627 { .name = "RNDR", .state = ARM_CP_STATE_AA64,
6628 .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END | ARM_CP_IO,
6629 .opc0 = 3, .opc1 = 3, .crn = 2, .crm = 4, .opc2 = 0,
6630 .access = PL0_R, .readfn = rndr_readfn },
6631 { .name = "RNDRRS", .state = ARM_CP_STATE_AA64,
6632 .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END | ARM_CP_IO,
6633 .opc0 = 3, .opc1 = 3, .crn = 2, .crm = 4, .opc2 = 1,
6634 .access = PL0_R, .readfn = rndr_readfn },
6635 REGINFO_SENTINEL
6636};
0d57b499
BM
6637
6638#ifndef CONFIG_USER_ONLY
6639static void dccvap_writefn(CPUARMState *env, const ARMCPRegInfo *opaque,
6640 uint64_t value)
6641{
6642 ARMCPU *cpu = env_archcpu(env);
6643 /* CTR_EL0 System register -> DminLine, bits [19:16] */
6644 uint64_t dline_size = 4 << ((cpu->ctr >> 16) & 0xF);
6645 uint64_t vaddr_in = (uint64_t) value;
6646 uint64_t vaddr = vaddr_in & ~(dline_size - 1);
6647 void *haddr;
6648 int mem_idx = cpu_mmu_index(env, false);
6649
6650 /* This won't be crossing page boundaries */
6651 haddr = probe_read(env, vaddr, dline_size, mem_idx, GETPC());
6652 if (haddr) {
6653
6654 ram_addr_t offset;
6655 MemoryRegion *mr;
6656
6657 /* RCU lock is already being held */
6658 mr = memory_region_from_host(haddr, &offset);
6659
6660 if (mr) {
6661 memory_region_do_writeback(mr, offset, dline_size);
6662 }
6663 }
6664}
6665
6666static const ARMCPRegInfo dcpop_reg[] = {
6667 { .name = "DC_CVAP", .state = ARM_CP_STATE_AA64,
6668 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 1,
6669 .access = PL0_W, .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END,
6670 .accessfn = aa64_cacheop_access, .writefn = dccvap_writefn },
6671 REGINFO_SENTINEL
6672};
6673
6674static const ARMCPRegInfo dcpodp_reg[] = {
6675 { .name = "DC_CVADP", .state = ARM_CP_STATE_AA64,
6676 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 1,
6677 .access = PL0_W, .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END,
6678 .accessfn = aa64_cacheop_access, .writefn = dccvap_writefn },
6679 REGINFO_SENTINEL
6680};
6681#endif /*CONFIG_USER_ONLY*/
6682
967aa94f
RH
6683#endif
6684
cb570bd3
RH
6685static CPAccessResult access_predinv(CPUARMState *env, const ARMCPRegInfo *ri,
6686 bool isread)
6687{
6688 int el = arm_current_el(env);
6689
6690 if (el == 0) {
6691 uint64_t sctlr = arm_sctlr(env, el);
6692 if (!(sctlr & SCTLR_EnRCTX)) {
6693 return CP_ACCESS_TRAP;
6694 }
6695 } else if (el == 1) {
6696 uint64_t hcr = arm_hcr_el2_eff(env);
6697 if (hcr & HCR_NV) {
6698 return CP_ACCESS_TRAP_EL2;
6699 }
6700 }
6701 return CP_ACCESS_OK;
6702}
6703
6704static const ARMCPRegInfo predinv_reginfo[] = {
6705 { .name = "CFP_RCTX", .state = ARM_CP_STATE_AA64,
6706 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 4,
6707 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
6708 { .name = "DVP_RCTX", .state = ARM_CP_STATE_AA64,
6709 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 5,
6710 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
6711 { .name = "CPP_RCTX", .state = ARM_CP_STATE_AA64,
6712 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 7,
6713 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
6714 /*
6715 * Note the AArch32 opcodes have a different OPC1.
6716 */
6717 { .name = "CFPRCTX", .state = ARM_CP_STATE_AA32,
6718 .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 4,
6719 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
6720 { .name = "DVPRCTX", .state = ARM_CP_STATE_AA32,
6721 .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 5,
6722 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
6723 { .name = "CPPRCTX", .state = ARM_CP_STATE_AA32,
6724 .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 7,
6725 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
6726 REGINFO_SENTINEL
6727};
6728
957e6155
PM
6729static uint64_t ccsidr2_read(CPUARMState *env, const ARMCPRegInfo *ri)
6730{
6731 /* Read the high 32 bits of the current CCSIDR */
6732 return extract64(ccsidr_read(env, ri), 32, 32);
6733}
6734
6735static const ARMCPRegInfo ccsidr2_reginfo[] = {
6736 { .name = "CCSIDR2", .state = ARM_CP_STATE_BOTH,
6737 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 2,
6738 .access = PL1_R,
6739 .accessfn = access_aa64_tid2,
6740 .readfn = ccsidr2_read, .type = ARM_CP_NO_RAW },
6741 REGINFO_SENTINEL
6742};
6743
6a4ef4e5
MZ
6744static CPAccessResult access_aa64_tid3(CPUARMState *env, const ARMCPRegInfo *ri,
6745 bool isread)
6746{
6747 if ((arm_current_el(env) < 2) && (arm_hcr_el2_eff(env) & HCR_TID3)) {
6748 return CP_ACCESS_TRAP_EL2;
6749 }
6750
6751 return CP_ACCESS_OK;
6752}
6753
6754static CPAccessResult access_aa32_tid3(CPUARMState *env, const ARMCPRegInfo *ri,
6755 bool isread)
6756{
6757 if (arm_feature(env, ARM_FEATURE_V8)) {
6758 return access_aa64_tid3(env, ri, isread);
6759 }
6760
6761 return CP_ACCESS_OK;
6762}
6763
f96f3d5f
MZ
6764static CPAccessResult access_jazelle(CPUARMState *env, const ARMCPRegInfo *ri,
6765 bool isread)
6766{
6767 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID0)) {
6768 return CP_ACCESS_TRAP_EL2;
6769 }
6770
6771 return CP_ACCESS_OK;
6772}
6773
6774static const ARMCPRegInfo jazelle_regs[] = {
6775 { .name = "JIDR",
6776 .cp = 14, .crn = 0, .crm = 0, .opc1 = 7, .opc2 = 0,
6777 .access = PL1_R, .accessfn = access_jazelle,
6778 .type = ARM_CP_CONST, .resetvalue = 0 },
6779 { .name = "JOSCR",
6780 .cp = 14, .crn = 1, .crm = 0, .opc1 = 7, .opc2 = 0,
6781 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
6782 { .name = "JMCR",
6783 .cp = 14, .crn = 2, .crm = 0, .opc1 = 7, .opc2 = 0,
6784 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
6785 REGINFO_SENTINEL
6786};
6787
e2a1a461
RH
6788static const ARMCPRegInfo vhe_reginfo[] = {
6789 { .name = "CONTEXTIDR_EL2", .state = ARM_CP_STATE_AA64,
6790 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 1,
6791 .access = PL2_RW,
6792 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[2]) },
ed30da8e
RH
6793 { .name = "TTBR1_EL2", .state = ARM_CP_STATE_AA64,
6794 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 1,
6795 .access = PL2_RW, .writefn = vmsa_tcr_ttbr_el2_write,
6796 .fieldoffset = offsetof(CPUARMState, cp15.ttbr1_el[2]) },
8c94b071
RH
6797#ifndef CONFIG_USER_ONLY
6798 { .name = "CNTHV_CVAL_EL2", .state = ARM_CP_STATE_AA64,
6799 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 2,
6800 .fieldoffset =
6801 offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYPVIRT].cval),
6802 .type = ARM_CP_IO, .access = PL2_RW,
6803 .writefn = gt_hv_cval_write, .raw_writefn = raw_write },
6804 { .name = "CNTHV_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
6805 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 0,
6806 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
6807 .resetfn = gt_hv_timer_reset,
6808 .readfn = gt_hv_tval_read, .writefn = gt_hv_tval_write },
6809 { .name = "CNTHV_CTL_EL2", .state = ARM_CP_STATE_BOTH,
6810 .type = ARM_CP_IO,
6811 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 1,
6812 .access = PL2_RW,
6813 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYPVIRT].ctl),
6814 .writefn = gt_hv_ctl_write, .raw_writefn = raw_write },
bb5972e4
RH
6815 { .name = "CNTP_CTL_EL02", .state = ARM_CP_STATE_AA64,
6816 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 1,
6817 .type = ARM_CP_IO | ARM_CP_ALIAS,
6818 .access = PL2_RW, .accessfn = e2h_access,
6819 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
6820 .writefn = gt_phys_ctl_write, .raw_writefn = raw_write },
6821 { .name = "CNTV_CTL_EL02", .state = ARM_CP_STATE_AA64,
6822 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 1,
6823 .type = ARM_CP_IO | ARM_CP_ALIAS,
6824 .access = PL2_RW, .accessfn = e2h_access,
6825 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
6826 .writefn = gt_virt_ctl_write, .raw_writefn = raw_write },
6827 { .name = "CNTP_TVAL_EL02", .state = ARM_CP_STATE_AA64,
6828 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 0,
6829 .type = ARM_CP_NO_RAW | ARM_CP_IO | ARM_CP_ALIAS,
6830 .access = PL2_RW, .accessfn = e2h_access,
6831 .readfn = gt_phys_tval_read, .writefn = gt_phys_tval_write },
6832 { .name = "CNTV_TVAL_EL02", .state = ARM_CP_STATE_AA64,
6833 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 0,
6834 .type = ARM_CP_NO_RAW | ARM_CP_IO | ARM_CP_ALIAS,
6835 .access = PL2_RW, .accessfn = e2h_access,
6836 .readfn = gt_virt_tval_read, .writefn = gt_virt_tval_write },
6837 { .name = "CNTP_CVAL_EL02", .state = ARM_CP_STATE_AA64,
6838 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 2,
6839 .type = ARM_CP_IO | ARM_CP_ALIAS,
6840 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
6841 .access = PL2_RW, .accessfn = e2h_access,
6842 .writefn = gt_phys_cval_write, .raw_writefn = raw_write },
6843 { .name = "CNTV_CVAL_EL02", .state = ARM_CP_STATE_AA64,
6844 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 2,
6845 .type = ARM_CP_IO | ARM_CP_ALIAS,
6846 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
6847 .access = PL2_RW, .accessfn = e2h_access,
6848 .writefn = gt_virt_cval_write, .raw_writefn = raw_write },
8c94b071 6849#endif
e2a1a461
RH
6850 REGINFO_SENTINEL
6851};
6852
04b07d29
RH
6853#ifndef CONFIG_USER_ONLY
6854static const ARMCPRegInfo ats1e1_reginfo[] = {
6855 { .name = "AT_S1E1R", .state = ARM_CP_STATE_AA64,
6856 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 0,
6857 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
6858 .writefn = ats_write64 },
6859 { .name = "AT_S1E1W", .state = ARM_CP_STATE_AA64,
6860 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 1,
6861 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
6862 .writefn = ats_write64 },
6863 REGINFO_SENTINEL
6864};
6865
6866static const ARMCPRegInfo ats1cp_reginfo[] = {
6867 { .name = "ATS1CPRP",
6868 .cp = 15, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 0,
6869 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
6870 .writefn = ats_write },
6871 { .name = "ATS1CPWP",
6872 .cp = 15, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 1,
6873 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
6874 .writefn = ats_write },
6875 REGINFO_SENTINEL
6876};
6877#endif
6878
f6287c24
PM
6879/*
6880 * ACTLR2 and HACTLR2 map to ACTLR_EL1[63:32] and
6881 * ACTLR_EL2[63:32]. They exist only if the ID_MMFR4.AC2 field
6882 * is non-zero, which is never for ARMv7, optionally in ARMv8
6883 * and mandatorily for ARMv8.2 and up.
6884 * ACTLR2 is banked for S and NS if EL3 is AArch32. Since QEMU's
6885 * implementation is RAZ/WI we can ignore this detail, as we
6886 * do for ACTLR.
6887 */
6888static const ARMCPRegInfo actlr2_hactlr2_reginfo[] = {
6889 { .name = "ACTLR2", .state = ARM_CP_STATE_AA32,
6890 .cp = 15, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 3,
6891 .access = PL1_RW, .type = ARM_CP_CONST,
6892 .resetvalue = 0 },
6893 { .name = "HACTLR2", .state = ARM_CP_STATE_AA32,
6894 .cp = 15, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 3,
6895 .access = PL2_RW, .type = ARM_CP_CONST,
6896 .resetvalue = 0 },
6897 REGINFO_SENTINEL
6898};
6899
2ceb98c0
PM
6900void register_cp_regs_for_features(ARMCPU *cpu)
6901{
6902 /* Register all the coprocessor registers based on feature bits */
6903 CPUARMState *env = &cpu->env;
6904 if (arm_feature(env, ARM_FEATURE_M)) {
6905 /* M profile has no coprocessor registers */
6906 return;
6907 }
6908
e9aa6c21 6909 define_arm_cp_regs(cpu, cp_reginfo);
9449fdf6
PM
6910 if (!arm_feature(env, ARM_FEATURE_V8)) {
6911 /* Must go early as it is full of wildcards that may be
6912 * overridden by later definitions.
6913 */
6914 define_arm_cp_regs(cpu, not_v8_cp_reginfo);
6915 }
6916
7d57f408 6917 if (arm_feature(env, ARM_FEATURE_V6)) {
8515a092
PM
6918 /* The ID registers all have impdef reset values */
6919 ARMCPRegInfo v6_idregs[] = {
0ff644a7
PM
6920 { .name = "ID_PFR0", .state = ARM_CP_STATE_BOTH,
6921 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
6922 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 6923 .accessfn = access_aa32_tid3,
8515a092 6924 .resetvalue = cpu->id_pfr0 },
96a8b92e
PM
6925 /* ID_PFR1 is not a plain ARM_CP_CONST because we don't know
6926 * the value of the GIC field until after we define these regs.
6927 */
0ff644a7
PM
6928 { .name = "ID_PFR1", .state = ARM_CP_STATE_BOTH,
6929 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 1,
96a8b92e 6930 .access = PL1_R, .type = ARM_CP_NO_RAW,
6a4ef4e5 6931 .accessfn = access_aa32_tid3,
96a8b92e
PM
6932 .readfn = id_pfr1_read,
6933 .writefn = arm_cp_write_ignore },
0ff644a7
PM
6934 { .name = "ID_DFR0", .state = ARM_CP_STATE_BOTH,
6935 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 2,
6936 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 6937 .accessfn = access_aa32_tid3,
a6179538 6938 .resetvalue = cpu->isar.id_dfr0 },
0ff644a7
PM
6939 { .name = "ID_AFR0", .state = ARM_CP_STATE_BOTH,
6940 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 3,
6941 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 6942 .accessfn = access_aa32_tid3,
8515a092 6943 .resetvalue = cpu->id_afr0 },
0ff644a7
PM
6944 { .name = "ID_MMFR0", .state = ARM_CP_STATE_BOTH,
6945 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 4,
6946 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 6947 .accessfn = access_aa32_tid3,
10054016 6948 .resetvalue = cpu->isar.id_mmfr0 },
0ff644a7
PM
6949 { .name = "ID_MMFR1", .state = ARM_CP_STATE_BOTH,
6950 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 5,
6951 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 6952 .accessfn = access_aa32_tid3,
10054016 6953 .resetvalue = cpu->isar.id_mmfr1 },
0ff644a7
PM
6954 { .name = "ID_MMFR2", .state = ARM_CP_STATE_BOTH,
6955 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 6,
6956 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 6957 .accessfn = access_aa32_tid3,
10054016 6958 .resetvalue = cpu->isar.id_mmfr2 },
0ff644a7
PM
6959 { .name = "ID_MMFR3", .state = ARM_CP_STATE_BOTH,
6960 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 7,
6961 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 6962 .accessfn = access_aa32_tid3,
10054016 6963 .resetvalue = cpu->isar.id_mmfr3 },
0ff644a7
PM
6964 { .name = "ID_ISAR0", .state = ARM_CP_STATE_BOTH,
6965 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0,
6966 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 6967 .accessfn = access_aa32_tid3,
47576b94 6968 .resetvalue = cpu->isar.id_isar0 },
0ff644a7
PM
6969 { .name = "ID_ISAR1", .state = ARM_CP_STATE_BOTH,
6970 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 1,
6971 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 6972 .accessfn = access_aa32_tid3,
47576b94 6973 .resetvalue = cpu->isar.id_isar1 },
0ff644a7
PM
6974 { .name = "ID_ISAR2", .state = ARM_CP_STATE_BOTH,
6975 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
6976 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 6977 .accessfn = access_aa32_tid3,
47576b94 6978 .resetvalue = cpu->isar.id_isar2 },
0ff644a7
PM
6979 { .name = "ID_ISAR3", .state = ARM_CP_STATE_BOTH,
6980 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 3,
6981 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 6982 .accessfn = access_aa32_tid3,
47576b94 6983 .resetvalue = cpu->isar.id_isar3 },
0ff644a7
PM
6984 { .name = "ID_ISAR4", .state = ARM_CP_STATE_BOTH,
6985 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 4,
6986 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 6987 .accessfn = access_aa32_tid3,
47576b94 6988 .resetvalue = cpu->isar.id_isar4 },
0ff644a7
PM
6989 { .name = "ID_ISAR5", .state = ARM_CP_STATE_BOTH,
6990 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 5,
6991 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 6992 .accessfn = access_aa32_tid3,
47576b94 6993 .resetvalue = cpu->isar.id_isar5 },
e20d84c1
PM
6994 { .name = "ID_MMFR4", .state = ARM_CP_STATE_BOTH,
6995 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 6,
6996 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 6997 .accessfn = access_aa32_tid3,
10054016 6998 .resetvalue = cpu->isar.id_mmfr4 },
802abf40 6999 { .name = "ID_ISAR6", .state = ARM_CP_STATE_BOTH,
e20d84c1
PM
7000 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 7,
7001 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7002 .accessfn = access_aa32_tid3,
47576b94 7003 .resetvalue = cpu->isar.id_isar6 },
8515a092
PM
7004 REGINFO_SENTINEL
7005 };
7006 define_arm_cp_regs(cpu, v6_idregs);
7d57f408
PM
7007 define_arm_cp_regs(cpu, v6_cp_reginfo);
7008 } else {
7009 define_arm_cp_regs(cpu, not_v6_cp_reginfo);
7010 }
4d31c596
PM
7011 if (arm_feature(env, ARM_FEATURE_V6K)) {
7012 define_arm_cp_regs(cpu, v6k_cp_reginfo);
7013 }
5e5cf9e3 7014 if (arm_feature(env, ARM_FEATURE_V7MP) &&
452a0955 7015 !arm_feature(env, ARM_FEATURE_PMSA)) {
995939a6
PM
7016 define_arm_cp_regs(cpu, v7mp_cp_reginfo);
7017 }
327dd510
AL
7018 if (arm_feature(env, ARM_FEATURE_V7VE)) {
7019 define_arm_cp_regs(cpu, pmovsset_cp_reginfo);
7020 }
e9aa6c21 7021 if (arm_feature(env, ARM_FEATURE_V7)) {
776d4e5c 7022 ARMCPRegInfo clidr = {
7da845b0
PM
7023 .name = "CLIDR", .state = ARM_CP_STATE_BOTH,
7024 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 1,
630fcd4d
MZ
7025 .access = PL1_R, .type = ARM_CP_CONST,
7026 .accessfn = access_aa64_tid2,
7027 .resetvalue = cpu->clidr
776d4e5c 7028 };
776d4e5c 7029 define_one_arm_cp_reg(cpu, &clidr);
e9aa6c21 7030 define_arm_cp_regs(cpu, v7_cp_reginfo);
50300698 7031 define_debug_regs(cpu);
24183fb6 7032 define_pmu_regs(cpu);
7d57f408
PM
7033 } else {
7034 define_arm_cp_regs(cpu, not_v7_cp_reginfo);
e9aa6c21 7035 }
b0d2b7d0 7036 if (arm_feature(env, ARM_FEATURE_V8)) {
e20d84c1
PM
7037 /* AArch64 ID registers, which all have impdef reset values.
7038 * Note that within the ID register ranges the unused slots
7039 * must all RAZ, not UNDEF; future architecture versions may
7040 * define new registers here.
7041 */
e60cef86 7042 ARMCPRegInfo v8_idregs[] = {
96a8b92e
PM
7043 /* ID_AA64PFR0_EL1 is not a plain ARM_CP_CONST because we don't
7044 * know the right value for the GIC field until after we
7045 * define these regs.
7046 */
e60cef86
PM
7047 { .name = "ID_AA64PFR0_EL1", .state = ARM_CP_STATE_AA64,
7048 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 0,
96a8b92e 7049 .access = PL1_R, .type = ARM_CP_NO_RAW,
6a4ef4e5 7050 .accessfn = access_aa64_tid3,
96a8b92e
PM
7051 .readfn = id_aa64pfr0_read,
7052 .writefn = arm_cp_write_ignore },
e60cef86
PM
7053 { .name = "ID_AA64PFR1_EL1", .state = ARM_CP_STATE_AA64,
7054 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 1,
7055 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7056 .accessfn = access_aa64_tid3,
47576b94 7057 .resetvalue = cpu->isar.id_aa64pfr1},
e20d84c1
PM
7058 { .name = "ID_AA64PFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7059 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 2,
7060 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7061 .accessfn = access_aa64_tid3,
e20d84c1
PM
7062 .resetvalue = 0 },
7063 { .name = "ID_AA64PFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7064 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 3,
7065 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7066 .accessfn = access_aa64_tid3,
e20d84c1 7067 .resetvalue = 0 },
9516d772 7068 { .name = "ID_AA64ZFR0_EL1", .state = ARM_CP_STATE_AA64,
e20d84c1
PM
7069 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 4,
7070 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7071 .accessfn = access_aa64_tid3,
9516d772 7072 /* At present, only SVEver == 0 is defined anyway. */
e20d84c1
PM
7073 .resetvalue = 0 },
7074 { .name = "ID_AA64PFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7075 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 5,
7076 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7077 .accessfn = access_aa64_tid3,
e20d84c1
PM
7078 .resetvalue = 0 },
7079 { .name = "ID_AA64PFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7080 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 6,
7081 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7082 .accessfn = access_aa64_tid3,
e20d84c1
PM
7083 .resetvalue = 0 },
7084 { .name = "ID_AA64PFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7085 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 7,
7086 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7087 .accessfn = access_aa64_tid3,
e20d84c1 7088 .resetvalue = 0 },
e60cef86
PM
7089 { .name = "ID_AA64DFR0_EL1", .state = ARM_CP_STATE_AA64,
7090 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 0,
7091 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7092 .accessfn = access_aa64_tid3,
2a609df8 7093 .resetvalue = cpu->isar.id_aa64dfr0 },
e60cef86
PM
7094 { .name = "ID_AA64DFR1_EL1", .state = ARM_CP_STATE_AA64,
7095 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 1,
7096 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7097 .accessfn = access_aa64_tid3,
2a609df8 7098 .resetvalue = cpu->isar.id_aa64dfr1 },
e20d84c1
PM
7099 { .name = "ID_AA64DFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7100 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 2,
7101 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7102 .accessfn = access_aa64_tid3,
e20d84c1
PM
7103 .resetvalue = 0 },
7104 { .name = "ID_AA64DFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7105 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 3,
7106 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7107 .accessfn = access_aa64_tid3,
e20d84c1 7108 .resetvalue = 0 },
e60cef86
PM
7109 { .name = "ID_AA64AFR0_EL1", .state = ARM_CP_STATE_AA64,
7110 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 4,
7111 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7112 .accessfn = access_aa64_tid3,
e60cef86
PM
7113 .resetvalue = cpu->id_aa64afr0 },
7114 { .name = "ID_AA64AFR1_EL1", .state = ARM_CP_STATE_AA64,
7115 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 5,
7116 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7117 .accessfn = access_aa64_tid3,
e60cef86 7118 .resetvalue = cpu->id_aa64afr1 },
e20d84c1
PM
7119 { .name = "ID_AA64AFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7120 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 6,
7121 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7122 .accessfn = access_aa64_tid3,
e20d84c1
PM
7123 .resetvalue = 0 },
7124 { .name = "ID_AA64AFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7125 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 7,
7126 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7127 .accessfn = access_aa64_tid3,
e20d84c1 7128 .resetvalue = 0 },
e60cef86
PM
7129 { .name = "ID_AA64ISAR0_EL1", .state = ARM_CP_STATE_AA64,
7130 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 0,
7131 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7132 .accessfn = access_aa64_tid3,
47576b94 7133 .resetvalue = cpu->isar.id_aa64isar0 },
e60cef86
PM
7134 { .name = "ID_AA64ISAR1_EL1", .state = ARM_CP_STATE_AA64,
7135 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 1,
7136 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7137 .accessfn = access_aa64_tid3,
47576b94 7138 .resetvalue = cpu->isar.id_aa64isar1 },
e20d84c1
PM
7139 { .name = "ID_AA64ISAR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7140 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 2,
7141 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7142 .accessfn = access_aa64_tid3,
e20d84c1
PM
7143 .resetvalue = 0 },
7144 { .name = "ID_AA64ISAR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7145 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 3,
7146 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7147 .accessfn = access_aa64_tid3,
e20d84c1
PM
7148 .resetvalue = 0 },
7149 { .name = "ID_AA64ISAR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7150 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 4,
7151 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7152 .accessfn = access_aa64_tid3,
e20d84c1
PM
7153 .resetvalue = 0 },
7154 { .name = "ID_AA64ISAR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7155 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 5,
7156 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7157 .accessfn = access_aa64_tid3,
e20d84c1
PM
7158 .resetvalue = 0 },
7159 { .name = "ID_AA64ISAR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7160 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 6,
7161 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7162 .accessfn = access_aa64_tid3,
e20d84c1
PM
7163 .resetvalue = 0 },
7164 { .name = "ID_AA64ISAR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7165 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 7,
7166 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7167 .accessfn = access_aa64_tid3,
e20d84c1 7168 .resetvalue = 0 },
e60cef86
PM
7169 { .name = "ID_AA64MMFR0_EL1", .state = ARM_CP_STATE_AA64,
7170 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
7171 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7172 .accessfn = access_aa64_tid3,
3dc91ddb 7173 .resetvalue = cpu->isar.id_aa64mmfr0 },
e60cef86
PM
7174 { .name = "ID_AA64MMFR1_EL1", .state = ARM_CP_STATE_AA64,
7175 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 1,
7176 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7177 .accessfn = access_aa64_tid3,
3dc91ddb 7178 .resetvalue = cpu->isar.id_aa64mmfr1 },
64761e10 7179 { .name = "ID_AA64MMFR2_EL1", .state = ARM_CP_STATE_AA64,
e20d84c1
PM
7180 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 2,
7181 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7182 .accessfn = access_aa64_tid3,
64761e10 7183 .resetvalue = cpu->isar.id_aa64mmfr2 },
e20d84c1
PM
7184 { .name = "ID_AA64MMFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7185 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 3,
7186 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7187 .accessfn = access_aa64_tid3,
e20d84c1
PM
7188 .resetvalue = 0 },
7189 { .name = "ID_AA64MMFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7190 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 4,
7191 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7192 .accessfn = access_aa64_tid3,
e20d84c1
PM
7193 .resetvalue = 0 },
7194 { .name = "ID_AA64MMFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7195 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 5,
7196 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7197 .accessfn = access_aa64_tid3,
e20d84c1
PM
7198 .resetvalue = 0 },
7199 { .name = "ID_AA64MMFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7200 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 6,
7201 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7202 .accessfn = access_aa64_tid3,
e20d84c1
PM
7203 .resetvalue = 0 },
7204 { .name = "ID_AA64MMFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7205 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 7,
7206 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7207 .accessfn = access_aa64_tid3,
e20d84c1 7208 .resetvalue = 0 },
a50c0f51
PM
7209 { .name = "MVFR0_EL1", .state = ARM_CP_STATE_AA64,
7210 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0,
7211 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7212 .accessfn = access_aa64_tid3,
47576b94 7213 .resetvalue = cpu->isar.mvfr0 },
a50c0f51
PM
7214 { .name = "MVFR1_EL1", .state = ARM_CP_STATE_AA64,
7215 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1,
7216 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7217 .accessfn = access_aa64_tid3,
47576b94 7218 .resetvalue = cpu->isar.mvfr1 },
a50c0f51
PM
7219 { .name = "MVFR2_EL1", .state = ARM_CP_STATE_AA64,
7220 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2,
7221 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7222 .accessfn = access_aa64_tid3,
47576b94 7223 .resetvalue = cpu->isar.mvfr2 },
e20d84c1
PM
7224 { .name = "MVFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7225 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 3,
7226 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7227 .accessfn = access_aa64_tid3,
e20d84c1
PM
7228 .resetvalue = 0 },
7229 { .name = "MVFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7230 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 4,
7231 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7232 .accessfn = access_aa64_tid3,
e20d84c1
PM
7233 .resetvalue = 0 },
7234 { .name = "MVFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7235 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 5,
7236 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7237 .accessfn = access_aa64_tid3,
e20d84c1
PM
7238 .resetvalue = 0 },
7239 { .name = "MVFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7240 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 6,
7241 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7242 .accessfn = access_aa64_tid3,
e20d84c1
PM
7243 .resetvalue = 0 },
7244 { .name = "MVFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7245 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 7,
7246 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7247 .accessfn = access_aa64_tid3,
e20d84c1 7248 .resetvalue = 0 },
4054bfa9
AF
7249 { .name = "PMCEID0", .state = ARM_CP_STATE_AA32,
7250 .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 6,
7251 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
cad86737 7252 .resetvalue = extract64(cpu->pmceid0, 0, 32) },
4054bfa9
AF
7253 { .name = "PMCEID0_EL0", .state = ARM_CP_STATE_AA64,
7254 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 6,
7255 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
7256 .resetvalue = cpu->pmceid0 },
7257 { .name = "PMCEID1", .state = ARM_CP_STATE_AA32,
7258 .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 7,
7259 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
cad86737 7260 .resetvalue = extract64(cpu->pmceid1, 0, 32) },
4054bfa9
AF
7261 { .name = "PMCEID1_EL0", .state = ARM_CP_STATE_AA64,
7262 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 7,
7263 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
7264 .resetvalue = cpu->pmceid1 },
e60cef86
PM
7265 REGINFO_SENTINEL
7266 };
6c5c0fec
AB
7267#ifdef CONFIG_USER_ONLY
7268 ARMCPRegUserSpaceInfo v8_user_idregs[] = {
7269 { .name = "ID_AA64PFR0_EL1",
7270 .exported_bits = 0x000f000f00ff0000,
7271 .fixed_bits = 0x0000000000000011 },
7272 { .name = "ID_AA64PFR1_EL1",
7273 .exported_bits = 0x00000000000000f0 },
d040242e
AB
7274 { .name = "ID_AA64PFR*_EL1_RESERVED",
7275 .is_glob = true },
6c5c0fec
AB
7276 { .name = "ID_AA64ZFR0_EL1" },
7277 { .name = "ID_AA64MMFR0_EL1",
7278 .fixed_bits = 0x00000000ff000000 },
7279 { .name = "ID_AA64MMFR1_EL1" },
d040242e
AB
7280 { .name = "ID_AA64MMFR*_EL1_RESERVED",
7281 .is_glob = true },
6c5c0fec
AB
7282 { .name = "ID_AA64DFR0_EL1",
7283 .fixed_bits = 0x0000000000000006 },
7284 { .name = "ID_AA64DFR1_EL1" },
d040242e
AB
7285 { .name = "ID_AA64DFR*_EL1_RESERVED",
7286 .is_glob = true },
7287 { .name = "ID_AA64AFR*",
7288 .is_glob = true },
6c5c0fec
AB
7289 { .name = "ID_AA64ISAR0_EL1",
7290 .exported_bits = 0x00fffffff0fffff0 },
7291 { .name = "ID_AA64ISAR1_EL1",
7292 .exported_bits = 0x000000f0ffffffff },
d040242e
AB
7293 { .name = "ID_AA64ISAR*_EL1_RESERVED",
7294 .is_glob = true },
6c5c0fec
AB
7295 REGUSERINFO_SENTINEL
7296 };
7297 modify_arm_cp_regs(v8_idregs, v8_user_idregs);
7298#endif
be8e8128
GB
7299 /* RVBAR_EL1 is only implemented if EL1 is the highest EL */
7300 if (!arm_feature(env, ARM_FEATURE_EL3) &&
7301 !arm_feature(env, ARM_FEATURE_EL2)) {
7302 ARMCPRegInfo rvbar = {
7303 .name = "RVBAR_EL1", .state = ARM_CP_STATE_AA64,
7304 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
7305 .type = ARM_CP_CONST, .access = PL1_R, .resetvalue = cpu->rvbar
7306 };
7307 define_one_arm_cp_reg(cpu, &rvbar);
7308 }
e60cef86 7309 define_arm_cp_regs(cpu, v8_idregs);
b0d2b7d0
PM
7310 define_arm_cp_regs(cpu, v8_cp_reginfo);
7311 }
3b685ba7 7312 if (arm_feature(env, ARM_FEATURE_EL2)) {
f0d574d6 7313 uint64_t vmpidr_def = mpidr_read_val(env);
731de9e6
EI
7314 ARMCPRegInfo vpidr_regs[] = {
7315 { .name = "VPIDR", .state = ARM_CP_STATE_AA32,
7316 .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
7317 .access = PL2_RW, .accessfn = access_el3_aa32ns,
36476562
PM
7318 .resetvalue = cpu->midr, .type = ARM_CP_ALIAS,
7319 .fieldoffset = offsetoflow32(CPUARMState, cp15.vpidr_el2) },
731de9e6
EI
7320 { .name = "VPIDR_EL2", .state = ARM_CP_STATE_AA64,
7321 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
7322 .access = PL2_RW, .resetvalue = cpu->midr,
7323 .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) },
f0d574d6
EI
7324 { .name = "VMPIDR", .state = ARM_CP_STATE_AA32,
7325 .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
7326 .access = PL2_RW, .accessfn = access_el3_aa32ns,
36476562
PM
7327 .resetvalue = vmpidr_def, .type = ARM_CP_ALIAS,
7328 .fieldoffset = offsetoflow32(CPUARMState, cp15.vmpidr_el2) },
f0d574d6
EI
7329 { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_AA64,
7330 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
7331 .access = PL2_RW,
7332 .resetvalue = vmpidr_def,
7333 .fieldoffset = offsetof(CPUARMState, cp15.vmpidr_el2) },
731de9e6
EI
7334 REGINFO_SENTINEL
7335 };
7336 define_arm_cp_regs(cpu, vpidr_regs);
4771cd01 7337 define_arm_cp_regs(cpu, el2_cp_reginfo);
ce4afed8
PM
7338 if (arm_feature(env, ARM_FEATURE_V8)) {
7339 define_arm_cp_regs(cpu, el2_v8_cp_reginfo);
7340 }
be8e8128
GB
7341 /* RVBAR_EL2 is only implemented if EL2 is the highest EL */
7342 if (!arm_feature(env, ARM_FEATURE_EL3)) {
7343 ARMCPRegInfo rvbar = {
7344 .name = "RVBAR_EL2", .state = ARM_CP_STATE_AA64,
7345 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 1,
7346 .type = ARM_CP_CONST, .access = PL2_R, .resetvalue = cpu->rvbar
7347 };
7348 define_one_arm_cp_reg(cpu, &rvbar);
7349 }
d42e3c26
EI
7350 } else {
7351 /* If EL2 is missing but higher ELs are enabled, we need to
7352 * register the no_el2 reginfos.
7353 */
7354 if (arm_feature(env, ARM_FEATURE_EL3)) {
f0d574d6
EI
7355 /* When EL3 exists but not EL2, VPIDR and VMPIDR take the value
7356 * of MIDR_EL1 and MPIDR_EL1.
731de9e6
EI
7357 */
7358 ARMCPRegInfo vpidr_regs[] = {
7359 { .name = "VPIDR_EL2", .state = ARM_CP_STATE_BOTH,
7360 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
7361 .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any,
7362 .type = ARM_CP_CONST, .resetvalue = cpu->midr,
7363 .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) },
f0d574d6
EI
7364 { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_BOTH,
7365 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
7366 .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any,
7367 .type = ARM_CP_NO_RAW,
7368 .writefn = arm_cp_write_ignore, .readfn = mpidr_read },
731de9e6
EI
7369 REGINFO_SENTINEL
7370 };
7371 define_arm_cp_regs(cpu, vpidr_regs);
4771cd01 7372 define_arm_cp_regs(cpu, el3_no_el2_cp_reginfo);
ce4afed8
PM
7373 if (arm_feature(env, ARM_FEATURE_V8)) {
7374 define_arm_cp_regs(cpu, el3_no_el2_v8_cp_reginfo);
7375 }
d42e3c26 7376 }
3b685ba7 7377 }
81547d66 7378 if (arm_feature(env, ARM_FEATURE_EL3)) {
0f1a3b24 7379 define_arm_cp_regs(cpu, el3_cp_reginfo);
e24fdd23
PM
7380 ARMCPRegInfo el3_regs[] = {
7381 { .name = "RVBAR_EL3", .state = ARM_CP_STATE_AA64,
7382 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 1,
7383 .type = ARM_CP_CONST, .access = PL3_R, .resetvalue = cpu->rvbar },
7384 { .name = "SCTLR_EL3", .state = ARM_CP_STATE_AA64,
7385 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 0,
7386 .access = PL3_RW,
7387 .raw_writefn = raw_write, .writefn = sctlr_write,
7388 .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[3]),
7389 .resetvalue = cpu->reset_sctlr },
7390 REGINFO_SENTINEL
be8e8128 7391 };
e24fdd23
PM
7392
7393 define_arm_cp_regs(cpu, el3_regs);
81547d66 7394 }
2f027fc5
PM
7395 /* The behaviour of NSACR is sufficiently various that we don't
7396 * try to describe it in a single reginfo:
7397 * if EL3 is 64 bit, then trap to EL3 from S EL1,
7398 * reads as constant 0xc00 from NS EL1 and NS EL2
7399 * if EL3 is 32 bit, then RW at EL3, RO at NS EL1 and NS EL2
7400 * if v7 without EL3, register doesn't exist
7401 * if v8 without EL3, reads as constant 0xc00 from NS EL1 and NS EL2
7402 */
7403 if (arm_feature(env, ARM_FEATURE_EL3)) {
7404 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
7405 ARMCPRegInfo nsacr = {
7406 .name = "NSACR", .type = ARM_CP_CONST,
7407 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
7408 .access = PL1_RW, .accessfn = nsacr_access,
7409 .resetvalue = 0xc00
7410 };
7411 define_one_arm_cp_reg(cpu, &nsacr);
7412 } else {
7413 ARMCPRegInfo nsacr = {
7414 .name = "NSACR",
7415 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
7416 .access = PL3_RW | PL1_R,
7417 .resetvalue = 0,
7418 .fieldoffset = offsetof(CPUARMState, cp15.nsacr)
7419 };
7420 define_one_arm_cp_reg(cpu, &nsacr);
7421 }
7422 } else {
7423 if (arm_feature(env, ARM_FEATURE_V8)) {
7424 ARMCPRegInfo nsacr = {
7425 .name = "NSACR", .type = ARM_CP_CONST,
7426 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
7427 .access = PL1_R,
7428 .resetvalue = 0xc00
7429 };
7430 define_one_arm_cp_reg(cpu, &nsacr);
7431 }
7432 }
7433
452a0955 7434 if (arm_feature(env, ARM_FEATURE_PMSA)) {
6cb0b013
PC
7435 if (arm_feature(env, ARM_FEATURE_V6)) {
7436 /* PMSAv6 not implemented */
7437 assert(arm_feature(env, ARM_FEATURE_V7));
7438 define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
7439 define_arm_cp_regs(cpu, pmsav7_cp_reginfo);
7440 } else {
7441 define_arm_cp_regs(cpu, pmsav5_cp_reginfo);
7442 }
18032bec 7443 } else {
8e5d75c9 7444 define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
18032bec 7445 define_arm_cp_regs(cpu, vmsa_cp_reginfo);
4036b7d1
PM
7446 /* TTCBR2 is introduced with ARMv8.2-AA32HPD. */
7447 if (cpu_isar_feature(aa32_hpd, cpu)) {
ab638a32
RH
7448 define_one_arm_cp_reg(cpu, &ttbcr2_reginfo);
7449 }
18032bec 7450 }
c326b979
PM
7451 if (arm_feature(env, ARM_FEATURE_THUMB2EE)) {
7452 define_arm_cp_regs(cpu, t2ee_cp_reginfo);
7453 }
6cc7a3ae
PM
7454 if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) {
7455 define_arm_cp_regs(cpu, generic_timer_cp_reginfo);
7456 }
4a501606
PM
7457 if (arm_feature(env, ARM_FEATURE_VAPA)) {
7458 define_arm_cp_regs(cpu, vapa_cp_reginfo);
7459 }
c4804214
PM
7460 if (arm_feature(env, ARM_FEATURE_CACHE_TEST_CLEAN)) {
7461 define_arm_cp_regs(cpu, cache_test_clean_cp_reginfo);
7462 }
7463 if (arm_feature(env, ARM_FEATURE_CACHE_DIRTY_REG)) {
7464 define_arm_cp_regs(cpu, cache_dirty_status_cp_reginfo);
7465 }
7466 if (arm_feature(env, ARM_FEATURE_CACHE_BLOCK_OPS)) {
7467 define_arm_cp_regs(cpu, cache_block_ops_cp_reginfo);
7468 }
18032bec
PM
7469 if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
7470 define_arm_cp_regs(cpu, omap_cp_reginfo);
7471 }
34f90529
PM
7472 if (arm_feature(env, ARM_FEATURE_STRONGARM)) {
7473 define_arm_cp_regs(cpu, strongarm_cp_reginfo);
7474 }
1047b9d7
PM
7475 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
7476 define_arm_cp_regs(cpu, xscale_cp_reginfo);
7477 }
7478 if (arm_feature(env, ARM_FEATURE_DUMMY_C15_REGS)) {
7479 define_arm_cp_regs(cpu, dummy_c15_cp_reginfo);
7480 }
7ac681cf
PM
7481 if (arm_feature(env, ARM_FEATURE_LPAE)) {
7482 define_arm_cp_regs(cpu, lpae_cp_reginfo);
7483 }
873b73c0 7484 if (cpu_isar_feature(aa32_jazelle, cpu)) {
f96f3d5f
MZ
7485 define_arm_cp_regs(cpu, jazelle_regs);
7486 }
7884849c
PM
7487 /* Slightly awkwardly, the OMAP and StrongARM cores need all of
7488 * cp15 crn=0 to be writes-ignored, whereas for other cores they should
7489 * be read-only (ie write causes UNDEF exception).
7490 */
7491 {
00a29f3d
PM
7492 ARMCPRegInfo id_pre_v8_midr_cp_reginfo[] = {
7493 /* Pre-v8 MIDR space.
7494 * Note that the MIDR isn't a simple constant register because
7884849c
PM
7495 * of the TI925 behaviour where writes to another register can
7496 * cause the MIDR value to change.
97ce8d61
PC
7497 *
7498 * Unimplemented registers in the c15 0 0 0 space default to
7499 * MIDR. Define MIDR first as this entire space, then CTR, TCMTR
7500 * and friends override accordingly.
7884849c
PM
7501 */
7502 { .name = "MIDR",
97ce8d61 7503 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = CP_ANY,
7884849c 7504 .access = PL1_R, .resetvalue = cpu->midr,
d4e6df63 7505 .writefn = arm_cp_write_ignore, .raw_writefn = raw_write,
731de9e6 7506 .readfn = midr_read,
97ce8d61
PC
7507 .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
7508 .type = ARM_CP_OVERRIDE },
7884849c
PM
7509 /* crn = 0 op1 = 0 crm = 3..7 : currently unassigned; we RAZ. */
7510 { .name = "DUMMY",
7511 .cp = 15, .crn = 0, .crm = 3, .opc1 = 0, .opc2 = CP_ANY,
7512 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
7513 { .name = "DUMMY",
7514 .cp = 15, .crn = 0, .crm = 4, .opc1 = 0, .opc2 = CP_ANY,
7515 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
7516 { .name = "DUMMY",
7517 .cp = 15, .crn = 0, .crm = 5, .opc1 = 0, .opc2 = CP_ANY,
7518 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
7519 { .name = "DUMMY",
7520 .cp = 15, .crn = 0, .crm = 6, .opc1 = 0, .opc2 = CP_ANY,
7521 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
7522 { .name = "DUMMY",
7523 .cp = 15, .crn = 0, .crm = 7, .opc1 = 0, .opc2 = CP_ANY,
7524 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
7525 REGINFO_SENTINEL
7526 };
00a29f3d 7527 ARMCPRegInfo id_v8_midr_cp_reginfo[] = {
00a29f3d
PM
7528 { .name = "MIDR_EL1", .state = ARM_CP_STATE_BOTH,
7529 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 0,
731de9e6
EI
7530 .access = PL1_R, .type = ARM_CP_NO_RAW, .resetvalue = cpu->midr,
7531 .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
7532 .readfn = midr_read },
ac00c79f
SF
7533 /* crn = 0 op1 = 0 crm = 0 op2 = 4,7 : AArch32 aliases of MIDR */
7534 { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
7535 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
7536 .access = PL1_R, .resetvalue = cpu->midr },
7537 { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
7538 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 7,
7539 .access = PL1_R, .resetvalue = cpu->midr },
00a29f3d
PM
7540 { .name = "REVIDR_EL1", .state = ARM_CP_STATE_BOTH,
7541 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 6,
93fbc983
MZ
7542 .access = PL1_R,
7543 .accessfn = access_aa64_tid1,
7544 .type = ARM_CP_CONST, .resetvalue = cpu->revidr },
00a29f3d
PM
7545 REGINFO_SENTINEL
7546 };
7547 ARMCPRegInfo id_cp_reginfo[] = {
7548 /* These are common to v8 and pre-v8 */
7549 { .name = "CTR",
7550 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 1,
630fcd4d
MZ
7551 .access = PL1_R, .accessfn = ctr_el0_access,
7552 .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
00a29f3d
PM
7553 { .name = "CTR_EL0", .state = ARM_CP_STATE_AA64,
7554 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 0, .crm = 0,
7555 .access = PL0_R, .accessfn = ctr_el0_access,
7556 .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
7557 /* TCMTR and TLBTR exist in v8 but have no 64-bit versions */
7558 { .name = "TCMTR",
7559 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 2,
93fbc983
MZ
7560 .access = PL1_R,
7561 .accessfn = access_aa32_tid1,
7562 .type = ARM_CP_CONST, .resetvalue = 0 },
00a29f3d
PM
7563 REGINFO_SENTINEL
7564 };
8085ce63
PC
7565 /* TLBTR is specific to VMSA */
7566 ARMCPRegInfo id_tlbtr_reginfo = {
7567 .name = "TLBTR",
7568 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 3,
93fbc983
MZ
7569 .access = PL1_R,
7570 .accessfn = access_aa32_tid1,
7571 .type = ARM_CP_CONST, .resetvalue = 0,
8085ce63 7572 };
3281af81
PC
7573 /* MPUIR is specific to PMSA V6+ */
7574 ARMCPRegInfo id_mpuir_reginfo = {
7575 .name = "MPUIR",
7576 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
7577 .access = PL1_R, .type = ARM_CP_CONST,
7578 .resetvalue = cpu->pmsav7_dregion << 8
7579 };
7884849c
PM
7580 ARMCPRegInfo crn0_wi_reginfo = {
7581 .name = "CRN0_WI", .cp = 15, .crn = 0, .crm = CP_ANY,
7582 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_W,
7583 .type = ARM_CP_NOP | ARM_CP_OVERRIDE
7584 };
6c5c0fec
AB
7585#ifdef CONFIG_USER_ONLY
7586 ARMCPRegUserSpaceInfo id_v8_user_midr_cp_reginfo[] = {
7587 { .name = "MIDR_EL1",
7588 .exported_bits = 0x00000000ffffffff },
7589 { .name = "REVIDR_EL1" },
7590 REGUSERINFO_SENTINEL
7591 };
7592 modify_arm_cp_regs(id_v8_midr_cp_reginfo, id_v8_user_midr_cp_reginfo);
7593#endif
7884849c
PM
7594 if (arm_feature(env, ARM_FEATURE_OMAPCP) ||
7595 arm_feature(env, ARM_FEATURE_STRONGARM)) {
7596 ARMCPRegInfo *r;
7597 /* Register the blanket "writes ignored" value first to cover the
a703eda1
PC
7598 * whole space. Then update the specific ID registers to allow write
7599 * access, so that they ignore writes rather than causing them to
7600 * UNDEF.
7884849c
PM
7601 */
7602 define_one_arm_cp_reg(cpu, &crn0_wi_reginfo);
00a29f3d
PM
7603 for (r = id_pre_v8_midr_cp_reginfo;
7604 r->type != ARM_CP_SENTINEL; r++) {
7605 r->access = PL1_RW;
7606 }
7884849c
PM
7607 for (r = id_cp_reginfo; r->type != ARM_CP_SENTINEL; r++) {
7608 r->access = PL1_RW;
7884849c 7609 }
10006112 7610 id_mpuir_reginfo.access = PL1_RW;
3281af81 7611 id_tlbtr_reginfo.access = PL1_RW;
7884849c 7612 }
00a29f3d
PM
7613 if (arm_feature(env, ARM_FEATURE_V8)) {
7614 define_arm_cp_regs(cpu, id_v8_midr_cp_reginfo);
7615 } else {
7616 define_arm_cp_regs(cpu, id_pre_v8_midr_cp_reginfo);
7617 }
a703eda1 7618 define_arm_cp_regs(cpu, id_cp_reginfo);
452a0955 7619 if (!arm_feature(env, ARM_FEATURE_PMSA)) {
8085ce63 7620 define_one_arm_cp_reg(cpu, &id_tlbtr_reginfo);
3281af81
PC
7621 } else if (arm_feature(env, ARM_FEATURE_V7)) {
7622 define_one_arm_cp_reg(cpu, &id_mpuir_reginfo);
8085ce63 7623 }
7884849c
PM
7624 }
7625
97ce8d61 7626 if (arm_feature(env, ARM_FEATURE_MPIDR)) {
52264166
AB
7627 ARMCPRegInfo mpidr_cp_reginfo[] = {
7628 { .name = "MPIDR_EL1", .state = ARM_CP_STATE_BOTH,
7629 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 5,
7630 .access = PL1_R, .readfn = mpidr_read, .type = ARM_CP_NO_RAW },
7631 REGINFO_SENTINEL
7632 };
7633#ifdef CONFIG_USER_ONLY
7634 ARMCPRegUserSpaceInfo mpidr_user_cp_reginfo[] = {
7635 { .name = "MPIDR_EL1",
7636 .fixed_bits = 0x0000000080000000 },
7637 REGUSERINFO_SENTINEL
7638 };
7639 modify_arm_cp_regs(mpidr_cp_reginfo, mpidr_user_cp_reginfo);
7640#endif
97ce8d61
PC
7641 define_arm_cp_regs(cpu, mpidr_cp_reginfo);
7642 }
7643
2771db27 7644 if (arm_feature(env, ARM_FEATURE_AUXCR)) {
834a6c69
PM
7645 ARMCPRegInfo auxcr_reginfo[] = {
7646 { .name = "ACTLR_EL1", .state = ARM_CP_STATE_BOTH,
7647 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 1,
7648 .access = PL1_RW, .type = ARM_CP_CONST,
7649 .resetvalue = cpu->reset_auxcr },
7650 { .name = "ACTLR_EL2", .state = ARM_CP_STATE_BOTH,
7651 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 1,
7652 .access = PL2_RW, .type = ARM_CP_CONST,
7653 .resetvalue = 0 },
7654 { .name = "ACTLR_EL3", .state = ARM_CP_STATE_AA64,
7655 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 1,
7656 .access = PL3_RW, .type = ARM_CP_CONST,
7657 .resetvalue = 0 },
7658 REGINFO_SENTINEL
2771db27 7659 };
834a6c69 7660 define_arm_cp_regs(cpu, auxcr_reginfo);
f6287c24
PM
7661 if (cpu_isar_feature(aa32_ac2, cpu)) {
7662 define_arm_cp_regs(cpu, actlr2_hactlr2_reginfo);
0e0456ab 7663 }
2771db27
PM
7664 }
7665
d8ba780b 7666 if (arm_feature(env, ARM_FEATURE_CBAR)) {
d56974af
LM
7667 /*
7668 * CBAR is IMPDEF, but common on Arm Cortex-A implementations.
7669 * There are two flavours:
7670 * (1) older 32-bit only cores have a simple 32-bit CBAR
7671 * (2) 64-bit cores have a 64-bit CBAR visible to AArch64, plus a
7672 * 32-bit register visible to AArch32 at a different encoding
7673 * to the "flavour 1" register and with the bits rearranged to
7674 * be able to squash a 64-bit address into the 32-bit view.
7675 * We distinguish the two via the ARM_FEATURE_AARCH64 flag, but
7676 * in future if we support AArch32-only configs of some of the
7677 * AArch64 cores we might need to add a specific feature flag
7678 * to indicate cores with "flavour 2" CBAR.
7679 */
f318cec6
PM
7680 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
7681 /* 32 bit view is [31:18] 0...0 [43:32]. */
7682 uint32_t cbar32 = (extract64(cpu->reset_cbar, 18, 14) << 18)
7683 | extract64(cpu->reset_cbar, 32, 12);
7684 ARMCPRegInfo cbar_reginfo[] = {
7685 { .name = "CBAR",
7686 .type = ARM_CP_CONST,
d56974af
LM
7687 .cp = 15, .crn = 15, .crm = 3, .opc1 = 1, .opc2 = 0,
7688 .access = PL1_R, .resetvalue = cbar32 },
f318cec6
PM
7689 { .name = "CBAR_EL1", .state = ARM_CP_STATE_AA64,
7690 .type = ARM_CP_CONST,
7691 .opc0 = 3, .opc1 = 1, .crn = 15, .crm = 3, .opc2 = 0,
d56974af 7692 .access = PL1_R, .resetvalue = cpu->reset_cbar },
f318cec6
PM
7693 REGINFO_SENTINEL
7694 };
7695 /* We don't implement a r/w 64 bit CBAR currently */
7696 assert(arm_feature(env, ARM_FEATURE_CBAR_RO));
7697 define_arm_cp_regs(cpu, cbar_reginfo);
7698 } else {
7699 ARMCPRegInfo cbar = {
7700 .name = "CBAR",
7701 .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0,
7702 .access = PL1_R|PL3_W, .resetvalue = cpu->reset_cbar,
7703 .fieldoffset = offsetof(CPUARMState,
7704 cp15.c15_config_base_address)
7705 };
7706 if (arm_feature(env, ARM_FEATURE_CBAR_RO)) {
7707 cbar.access = PL1_R;
7708 cbar.fieldoffset = 0;
7709 cbar.type = ARM_CP_CONST;
7710 }
7711 define_one_arm_cp_reg(cpu, &cbar);
7712 }
d8ba780b
PC
7713 }
7714
91db4642
CLG
7715 if (arm_feature(env, ARM_FEATURE_VBAR)) {
7716 ARMCPRegInfo vbar_cp_reginfo[] = {
7717 { .name = "VBAR", .state = ARM_CP_STATE_BOTH,
7718 .opc0 = 3, .crn = 12, .crm = 0, .opc1 = 0, .opc2 = 0,
7719 .access = PL1_RW, .writefn = vbar_write,
7720 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.vbar_s),
7721 offsetof(CPUARMState, cp15.vbar_ns) },
7722 .resetvalue = 0 },
7723 REGINFO_SENTINEL
7724 };
7725 define_arm_cp_regs(cpu, vbar_cp_reginfo);
7726 }
7727
2771db27
PM
7728 /* Generic registers whose values depend on the implementation */
7729 {
7730 ARMCPRegInfo sctlr = {
5ebafdf3 7731 .name = "SCTLR", .state = ARM_CP_STATE_BOTH,
137feaa9
FA
7732 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
7733 .access = PL1_RW,
7734 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.sctlr_s),
7735 offsetof(CPUARMState, cp15.sctlr_ns) },
d4e6df63
PM
7736 .writefn = sctlr_write, .resetvalue = cpu->reset_sctlr,
7737 .raw_writefn = raw_write,
2771db27
PM
7738 };
7739 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
7740 /* Normally we would always end the TB on an SCTLR write, but Linux
7741 * arch/arm/mach-pxa/sleep.S expects two instructions following
7742 * an MMU enable to execute from cache. Imitate this behaviour.
7743 */
7744 sctlr.type |= ARM_CP_SUPPRESS_TB_END;
7745 }
7746 define_one_arm_cp_reg(cpu, &sctlr);
7747 }
5be5e8ed 7748
2d7137c1 7749 if (cpu_isar_feature(aa64_lor, cpu)) {
2d7137c1
RH
7750 define_arm_cp_regs(cpu, lor_reginfo);
7751 }
220f508f
RH
7752 if (cpu_isar_feature(aa64_pan, cpu)) {
7753 define_one_arm_cp_reg(cpu, &pan_reginfo);
7754 }
04b07d29
RH
7755#ifndef CONFIG_USER_ONLY
7756 if (cpu_isar_feature(aa64_ats1e1, cpu)) {
7757 define_arm_cp_regs(cpu, ats1e1_reginfo);
7758 }
7759 if (cpu_isar_feature(aa32_ats1e1, cpu)) {
7760 define_arm_cp_regs(cpu, ats1cp_reginfo);
7761 }
7762#endif
9eeb7a1c
RH
7763 if (cpu_isar_feature(aa64_uao, cpu)) {
7764 define_one_arm_cp_reg(cpu, &uao_reginfo);
7765 }
2d7137c1 7766
e2a1a461
RH
7767 if (arm_feature(env, ARM_FEATURE_EL2) && cpu_isar_feature(aa64_vh, cpu)) {
7768 define_arm_cp_regs(cpu, vhe_reginfo);
7769 }
7770
cd208a1c 7771 if (cpu_isar_feature(aa64_sve, cpu)) {
5be5e8ed
RH
7772 define_one_arm_cp_reg(cpu, &zcr_el1_reginfo);
7773 if (arm_feature(env, ARM_FEATURE_EL2)) {
7774 define_one_arm_cp_reg(cpu, &zcr_el2_reginfo);
7775 } else {
7776 define_one_arm_cp_reg(cpu, &zcr_no_el2_reginfo);
7777 }
7778 if (arm_feature(env, ARM_FEATURE_EL3)) {
7779 define_one_arm_cp_reg(cpu, &zcr_el3_reginfo);
7780 }
7781 }
967aa94f
RH
7782
7783#ifdef TARGET_AARCH64
7784 if (cpu_isar_feature(aa64_pauth, cpu)) {
7785 define_arm_cp_regs(cpu, pauth_reginfo);
7786 }
de390645
RH
7787 if (cpu_isar_feature(aa64_rndr, cpu)) {
7788 define_arm_cp_regs(cpu, rndr_reginfo);
7789 }
0d57b499
BM
7790#ifndef CONFIG_USER_ONLY
7791 /* Data Cache clean instructions up to PoP */
7792 if (cpu_isar_feature(aa64_dcpop, cpu)) {
7793 define_one_arm_cp_reg(cpu, dcpop_reg);
7794
7795 if (cpu_isar_feature(aa64_dcpodp, cpu)) {
7796 define_one_arm_cp_reg(cpu, dcpodp_reg);
7797 }
7798 }
7799#endif /*CONFIG_USER_ONLY*/
967aa94f 7800#endif
cb570bd3 7801
22e57073 7802 if (cpu_isar_feature(any_predinv, cpu)) {
cb570bd3
RH
7803 define_arm_cp_regs(cpu, predinv_reginfo);
7804 }
e2cce18f 7805
957e6155
PM
7806 if (cpu_isar_feature(any_ccidx, cpu)) {
7807 define_arm_cp_regs(cpu, ccsidr2_reginfo);
7808 }
7809
e2cce18f
RH
7810#ifndef CONFIG_USER_ONLY
7811 /*
7812 * Register redirections and aliases must be done last,
7813 * after the registers from the other extensions have been defined.
7814 */
7815 if (arm_feature(env, ARM_FEATURE_EL2) && cpu_isar_feature(aa64_vh, cpu)) {
7816 define_arm_vh_e2h_redirects_aliases(cpu);
7817 }
7818#endif
2ceb98c0
PM
7819}
7820
14969266
AF
7821void arm_cpu_register_gdb_regs_for_features(ARMCPU *cpu)
7822{
22169d41 7823 CPUState *cs = CPU(cpu);
14969266
AF
7824 CPUARMState *env = &cpu->env;
7825
6a669427
PM
7826 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
7827 gdb_register_coprocessor(cs, aarch64_fpu_gdb_get_reg,
7828 aarch64_fpu_gdb_set_reg,
7829 34, "aarch64-fpu.xml", 0);
7830 } else if (arm_feature(env, ARM_FEATURE_NEON)) {
22169d41 7831 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
56aebc89 7832 51, "arm-neon.xml", 0);
a6627f5f 7833 } else if (cpu_isar_feature(aa32_simd_r32, cpu)) {
22169d41 7834 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
56aebc89 7835 35, "arm-vfp3.xml", 0);
7fbc6a40 7836 } else if (cpu_isar_feature(aa32_vfp_simd, cpu)) {
22169d41 7837 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
56aebc89
PB
7838 19, "arm-vfp.xml", 0);
7839 }
200bf5b7
AB
7840 gdb_register_coprocessor(cs, arm_gdb_get_sysreg, arm_gdb_set_sysreg,
7841 arm_gen_dynamic_xml(cs),
7842 "system-registers.xml", 0);
40f137e1
PB
7843}
7844
777dc784
PM
7845/* Sort alphabetically by type name, except for "any". */
7846static gint arm_cpu_list_compare(gconstpointer a, gconstpointer b)
5adb4839 7847{
777dc784
PM
7848 ObjectClass *class_a = (ObjectClass *)a;
7849 ObjectClass *class_b = (ObjectClass *)b;
7850 const char *name_a, *name_b;
5adb4839 7851
777dc784
PM
7852 name_a = object_class_get_name(class_a);
7853 name_b = object_class_get_name(class_b);
51492fd1 7854 if (strcmp(name_a, "any-" TYPE_ARM_CPU) == 0) {
777dc784 7855 return 1;
51492fd1 7856 } else if (strcmp(name_b, "any-" TYPE_ARM_CPU) == 0) {
777dc784
PM
7857 return -1;
7858 } else {
7859 return strcmp(name_a, name_b);
5adb4839
PB
7860 }
7861}
7862
777dc784 7863static void arm_cpu_list_entry(gpointer data, gpointer user_data)
40f137e1 7864{
777dc784 7865 ObjectClass *oc = data;
51492fd1
AF
7866 const char *typename;
7867 char *name;
3371d272 7868
51492fd1
AF
7869 typename = object_class_get_name(oc);
7870 name = g_strndup(typename, strlen(typename) - strlen("-" TYPE_ARM_CPU));
0442428a 7871 qemu_printf(" %s\n", name);
51492fd1 7872 g_free(name);
777dc784
PM
7873}
7874
0442428a 7875void arm_cpu_list(void)
777dc784 7876{
777dc784
PM
7877 GSList *list;
7878
7879 list = object_class_get_list(TYPE_ARM_CPU, false);
7880 list = g_slist_sort(list, arm_cpu_list_compare);
0442428a
MA
7881 qemu_printf("Available CPUs:\n");
7882 g_slist_foreach(list, arm_cpu_list_entry, NULL);
777dc784 7883 g_slist_free(list);
40f137e1
PB
7884}
7885
78027bb6
CR
7886static void arm_cpu_add_definition(gpointer data, gpointer user_data)
7887{
7888 ObjectClass *oc = data;
7889 CpuDefinitionInfoList **cpu_list = user_data;
7890 CpuDefinitionInfoList *entry;
7891 CpuDefinitionInfo *info;
7892 const char *typename;
7893
7894 typename = object_class_get_name(oc);
7895 info = g_malloc0(sizeof(*info));
7896 info->name = g_strndup(typename,
7897 strlen(typename) - strlen("-" TYPE_ARM_CPU));
8ed877b7 7898 info->q_typename = g_strdup(typename);
78027bb6
CR
7899
7900 entry = g_malloc0(sizeof(*entry));
7901 entry->value = info;
7902 entry->next = *cpu_list;
7903 *cpu_list = entry;
7904}
7905
25a9d6ca 7906CpuDefinitionInfoList *qmp_query_cpu_definitions(Error **errp)
78027bb6
CR
7907{
7908 CpuDefinitionInfoList *cpu_list = NULL;
7909 GSList *list;
7910
7911 list = object_class_get_list(TYPE_ARM_CPU, false);
7912 g_slist_foreach(list, arm_cpu_add_definition, &cpu_list);
7913 g_slist_free(list);
7914
7915 return cpu_list;
7916}
7917
6e6efd61 7918static void add_cpreg_to_hashtable(ARMCPU *cpu, const ARMCPRegInfo *r,
51a79b03 7919 void *opaque, int state, int secstate,
9c513e78
AB
7920 int crm, int opc1, int opc2,
7921 const char *name)
6e6efd61
PM
7922{
7923 /* Private utility function for define_one_arm_cp_reg_with_opaque():
7924 * add a single reginfo struct to the hash table.
7925 */
7926 uint32_t *key = g_new(uint32_t, 1);
7927 ARMCPRegInfo *r2 = g_memdup(r, sizeof(ARMCPRegInfo));
7928 int is64 = (r->type & ARM_CP_64BIT) ? 1 : 0;
3f3c82a5
FA
7929 int ns = (secstate & ARM_CP_SECSTATE_NS) ? 1 : 0;
7930
9c513e78 7931 r2->name = g_strdup(name);
3f3c82a5
FA
7932 /* Reset the secure state to the specific incoming state. This is
7933 * necessary as the register may have been defined with both states.
7934 */
7935 r2->secure = secstate;
7936
7937 if (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1]) {
7938 /* Register is banked (using both entries in array).
7939 * Overwriting fieldoffset as the array is only used to define
7940 * banked registers but later only fieldoffset is used.
f5a0a5a5 7941 */
3f3c82a5
FA
7942 r2->fieldoffset = r->bank_fieldoffsets[ns];
7943 }
7944
7945 if (state == ARM_CP_STATE_AA32) {
7946 if (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1]) {
7947 /* If the register is banked then we don't need to migrate or
7948 * reset the 32-bit instance in certain cases:
7949 *
7950 * 1) If the register has both 32-bit and 64-bit instances then we
7951 * can count on the 64-bit instance taking care of the
7952 * non-secure bank.
7953 * 2) If ARMv8 is enabled then we can count on a 64-bit version
7954 * taking care of the secure bank. This requires that separate
7955 * 32 and 64-bit definitions are provided.
7956 */
7957 if ((r->state == ARM_CP_STATE_BOTH && ns) ||
7958 (arm_feature(&cpu->env, ARM_FEATURE_V8) && !ns)) {
7a0e58fa 7959 r2->type |= ARM_CP_ALIAS;
3f3c82a5
FA
7960 }
7961 } else if ((secstate != r->secure) && !ns) {
7962 /* The register is not banked so we only want to allow migration of
7963 * the non-secure instance.
7964 */
7a0e58fa 7965 r2->type |= ARM_CP_ALIAS;
58a1d8ce 7966 }
3f3c82a5
FA
7967
7968 if (r->state == ARM_CP_STATE_BOTH) {
7969 /* We assume it is a cp15 register if the .cp field is left unset.
7970 */
7971 if (r2->cp == 0) {
7972 r2->cp = 15;
7973 }
7974
f5a0a5a5 7975#ifdef HOST_WORDS_BIGENDIAN
3f3c82a5
FA
7976 if (r2->fieldoffset) {
7977 r2->fieldoffset += sizeof(uint32_t);
7978 }
f5a0a5a5 7979#endif
3f3c82a5 7980 }
f5a0a5a5
PM
7981 }
7982 if (state == ARM_CP_STATE_AA64) {
7983 /* To allow abbreviation of ARMCPRegInfo
7984 * definitions, we treat cp == 0 as equivalent to
7985 * the value for "standard guest-visible sysreg".
58a1d8ce
PM
7986 * STATE_BOTH definitions are also always "standard
7987 * sysreg" in their AArch64 view (the .cp value may
7988 * be non-zero for the benefit of the AArch32 view).
f5a0a5a5 7989 */
58a1d8ce 7990 if (r->cp == 0 || r->state == ARM_CP_STATE_BOTH) {
f5a0a5a5
PM
7991 r2->cp = CP_REG_ARM64_SYSREG_CP;
7992 }
7993 *key = ENCODE_AA64_CP_REG(r2->cp, r2->crn, crm,
7994 r2->opc0, opc1, opc2);
7995 } else {
51a79b03 7996 *key = ENCODE_CP_REG(r2->cp, is64, ns, r2->crn, crm, opc1, opc2);
f5a0a5a5 7997 }
6e6efd61
PM
7998 if (opaque) {
7999 r2->opaque = opaque;
8000 }
67ed771d
PM
8001 /* reginfo passed to helpers is correct for the actual access,
8002 * and is never ARM_CP_STATE_BOTH:
8003 */
8004 r2->state = state;
6e6efd61
PM
8005 /* Make sure reginfo passed to helpers for wildcarded regs
8006 * has the correct crm/opc1/opc2 for this reg, not CP_ANY:
8007 */
8008 r2->crm = crm;
8009 r2->opc1 = opc1;
8010 r2->opc2 = opc2;
8011 /* By convention, for wildcarded registers only the first
8012 * entry is used for migration; the others are marked as
7a0e58fa 8013 * ALIAS so we don't try to transfer the register
6e6efd61 8014 * multiple times. Special registers (ie NOP/WFI) are
7a0e58fa 8015 * never migratable and not even raw-accessible.
6e6efd61 8016 */
7a0e58fa
PM
8017 if ((r->type & ARM_CP_SPECIAL)) {
8018 r2->type |= ARM_CP_NO_RAW;
8019 }
8020 if (((r->crm == CP_ANY) && crm != 0) ||
6e6efd61
PM
8021 ((r->opc1 == CP_ANY) && opc1 != 0) ||
8022 ((r->opc2 == CP_ANY) && opc2 != 0)) {
1f163787 8023 r2->type |= ARM_CP_ALIAS | ARM_CP_NO_GDB;
6e6efd61
PM
8024 }
8025
375421cc
PM
8026 /* Check that raw accesses are either forbidden or handled. Note that
8027 * we can't assert this earlier because the setup of fieldoffset for
8028 * banked registers has to be done first.
8029 */
8030 if (!(r2->type & ARM_CP_NO_RAW)) {
8031 assert(!raw_accessors_invalid(r2));
8032 }
8033
6e6efd61
PM
8034 /* Overriding of an existing definition must be explicitly
8035 * requested.
8036 */
8037 if (!(r->type & ARM_CP_OVERRIDE)) {
8038 ARMCPRegInfo *oldreg;
8039 oldreg = g_hash_table_lookup(cpu->cp_regs, key);
8040 if (oldreg && !(oldreg->type & ARM_CP_OVERRIDE)) {
8041 fprintf(stderr, "Register redefined: cp=%d %d bit "
8042 "crn=%d crm=%d opc1=%d opc2=%d, "
8043 "was %s, now %s\n", r2->cp, 32 + 32 * is64,
8044 r2->crn, r2->crm, r2->opc1, r2->opc2,
8045 oldreg->name, r2->name);
8046 g_assert_not_reached();
8047 }
8048 }
8049 g_hash_table_insert(cpu->cp_regs, key, r2);
8050}
8051
8052
4b6a83fb
PM
8053void define_one_arm_cp_reg_with_opaque(ARMCPU *cpu,
8054 const ARMCPRegInfo *r, void *opaque)
8055{
8056 /* Define implementations of coprocessor registers.
8057 * We store these in a hashtable because typically
8058 * there are less than 150 registers in a space which
8059 * is 16*16*16*8*8 = 262144 in size.
8060 * Wildcarding is supported for the crm, opc1 and opc2 fields.
8061 * If a register is defined twice then the second definition is
8062 * used, so this can be used to define some generic registers and
8063 * then override them with implementation specific variations.
8064 * At least one of the original and the second definition should
8065 * include ARM_CP_OVERRIDE in its type bits -- this is just a guard
8066 * against accidental use.
f5a0a5a5
PM
8067 *
8068 * The state field defines whether the register is to be
8069 * visible in the AArch32 or AArch64 execution state. If the
8070 * state is set to ARM_CP_STATE_BOTH then we synthesise a
8071 * reginfo structure for the AArch32 view, which sees the lower
8072 * 32 bits of the 64 bit register.
8073 *
8074 * Only registers visible in AArch64 may set r->opc0; opc0 cannot
8075 * be wildcarded. AArch64 registers are always considered to be 64
8076 * bits; the ARM_CP_64BIT* flag applies only to the AArch32 view of
8077 * the register, if any.
4b6a83fb 8078 */
f5a0a5a5 8079 int crm, opc1, opc2, state;
4b6a83fb
PM
8080 int crmmin = (r->crm == CP_ANY) ? 0 : r->crm;
8081 int crmmax = (r->crm == CP_ANY) ? 15 : r->crm;
8082 int opc1min = (r->opc1 == CP_ANY) ? 0 : r->opc1;
8083 int opc1max = (r->opc1 == CP_ANY) ? 7 : r->opc1;
8084 int opc2min = (r->opc2 == CP_ANY) ? 0 : r->opc2;
8085 int opc2max = (r->opc2 == CP_ANY) ? 7 : r->opc2;
8086 /* 64 bit registers have only CRm and Opc1 fields */
8087 assert(!((r->type & ARM_CP_64BIT) && (r->opc2 || r->crn)));
f5a0a5a5
PM
8088 /* op0 only exists in the AArch64 encodings */
8089 assert((r->state != ARM_CP_STATE_AA32) || (r->opc0 == 0));
8090 /* AArch64 regs are all 64 bit so ARM_CP_64BIT is meaningless */
8091 assert((r->state != ARM_CP_STATE_AA64) || !(r->type & ARM_CP_64BIT));
8092 /* The AArch64 pseudocode CheckSystemAccess() specifies that op1
8093 * encodes a minimum access level for the register. We roll this
8094 * runtime check into our general permission check code, so check
8095 * here that the reginfo's specified permissions are strict enough
8096 * to encompass the generic architectural permission check.
8097 */
8098 if (r->state != ARM_CP_STATE_AA32) {
8099 int mask = 0;
8100 switch (r->opc1) {
b5bd7440
AB
8101 case 0:
8102 /* min_EL EL1, but some accessible to EL0 via kernel ABI */
8103 mask = PL0U_R | PL1_RW;
8104 break;
8105 case 1: case 2:
f5a0a5a5
PM
8106 /* min_EL EL1 */
8107 mask = PL1_RW;
8108 break;
8109 case 3:
8110 /* min_EL EL0 */
8111 mask = PL0_RW;
8112 break;
8113 case 4:
b4ecf60f 8114 case 5:
f5a0a5a5
PM
8115 /* min_EL EL2 */
8116 mask = PL2_RW;
8117 break;
f5a0a5a5
PM
8118 case 6:
8119 /* min_EL EL3 */
8120 mask = PL3_RW;
8121 break;
8122 case 7:
8123 /* min_EL EL1, secure mode only (we don't check the latter) */
8124 mask = PL1_RW;
8125 break;
8126 default:
8127 /* broken reginfo with out-of-range opc1 */
8128 assert(false);
8129 break;
8130 }
8131 /* assert our permissions are not too lax (stricter is fine) */
8132 assert((r->access & ~mask) == 0);
8133 }
8134
4b6a83fb
PM
8135 /* Check that the register definition has enough info to handle
8136 * reads and writes if they are permitted.
8137 */
8138 if (!(r->type & (ARM_CP_SPECIAL|ARM_CP_CONST))) {
8139 if (r->access & PL3_R) {
3f3c82a5
FA
8140 assert((r->fieldoffset ||
8141 (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
8142 r->readfn);
4b6a83fb
PM
8143 }
8144 if (r->access & PL3_W) {
3f3c82a5
FA
8145 assert((r->fieldoffset ||
8146 (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
8147 r->writefn);
4b6a83fb
PM
8148 }
8149 }
8150 /* Bad type field probably means missing sentinel at end of reg list */
8151 assert(cptype_valid(r->type));
8152 for (crm = crmmin; crm <= crmmax; crm++) {
8153 for (opc1 = opc1min; opc1 <= opc1max; opc1++) {
8154 for (opc2 = opc2min; opc2 <= opc2max; opc2++) {
f5a0a5a5
PM
8155 for (state = ARM_CP_STATE_AA32;
8156 state <= ARM_CP_STATE_AA64; state++) {
8157 if (r->state != state && r->state != ARM_CP_STATE_BOTH) {
8158 continue;
8159 }
3f3c82a5
FA
8160 if (state == ARM_CP_STATE_AA32) {
8161 /* Under AArch32 CP registers can be common
8162 * (same for secure and non-secure world) or banked.
8163 */
9c513e78
AB
8164 char *name;
8165
3f3c82a5
FA
8166 switch (r->secure) {
8167 case ARM_CP_SECSTATE_S:
8168 case ARM_CP_SECSTATE_NS:
8169 add_cpreg_to_hashtable(cpu, r, opaque, state,
9c513e78
AB
8170 r->secure, crm, opc1, opc2,
8171 r->name);
3f3c82a5
FA
8172 break;
8173 default:
9c513e78 8174 name = g_strdup_printf("%s_S", r->name);
3f3c82a5
FA
8175 add_cpreg_to_hashtable(cpu, r, opaque, state,
8176 ARM_CP_SECSTATE_S,
9c513e78
AB
8177 crm, opc1, opc2, name);
8178 g_free(name);
3f3c82a5
FA
8179 add_cpreg_to_hashtable(cpu, r, opaque, state,
8180 ARM_CP_SECSTATE_NS,
9c513e78 8181 crm, opc1, opc2, r->name);
3f3c82a5
FA
8182 break;
8183 }
8184 } else {
8185 /* AArch64 registers get mapped to non-secure instance
8186 * of AArch32 */
8187 add_cpreg_to_hashtable(cpu, r, opaque, state,
8188 ARM_CP_SECSTATE_NS,
9c513e78 8189 crm, opc1, opc2, r->name);
3f3c82a5 8190 }
f5a0a5a5 8191 }
4b6a83fb
PM
8192 }
8193 }
8194 }
8195}
8196
8197void define_arm_cp_regs_with_opaque(ARMCPU *cpu,
8198 const ARMCPRegInfo *regs, void *opaque)
8199{
8200 /* Define a whole list of registers */
8201 const ARMCPRegInfo *r;
8202 for (r = regs; r->type != ARM_CP_SENTINEL; r++) {
8203 define_one_arm_cp_reg_with_opaque(cpu, r, opaque);
8204 }
8205}
8206
6c5c0fec
AB
8207/*
8208 * Modify ARMCPRegInfo for access from userspace.
8209 *
8210 * This is a data driven modification directed by
8211 * ARMCPRegUserSpaceInfo. All registers become ARM_CP_CONST as
8212 * user-space cannot alter any values and dynamic values pertaining to
8213 * execution state are hidden from user space view anyway.
8214 */
8215void modify_arm_cp_regs(ARMCPRegInfo *regs, const ARMCPRegUserSpaceInfo *mods)
8216{
8217 const ARMCPRegUserSpaceInfo *m;
8218 ARMCPRegInfo *r;
8219
8220 for (m = mods; m->name; m++) {
d040242e
AB
8221 GPatternSpec *pat = NULL;
8222 if (m->is_glob) {
8223 pat = g_pattern_spec_new(m->name);
8224 }
6c5c0fec 8225 for (r = regs; r->type != ARM_CP_SENTINEL; r++) {
d040242e
AB
8226 if (pat && g_pattern_match_string(pat, r->name)) {
8227 r->type = ARM_CP_CONST;
8228 r->access = PL0U_R;
8229 r->resetvalue = 0;
8230 /* continue */
8231 } else if (strcmp(r->name, m->name) == 0) {
6c5c0fec
AB
8232 r->type = ARM_CP_CONST;
8233 r->access = PL0U_R;
8234 r->resetvalue &= m->exported_bits;
8235 r->resetvalue |= m->fixed_bits;
8236 break;
8237 }
8238 }
d040242e
AB
8239 if (pat) {
8240 g_pattern_spec_free(pat);
8241 }
6c5c0fec
AB
8242 }
8243}
8244
60322b39 8245const ARMCPRegInfo *get_arm_cp_reginfo(GHashTable *cpregs, uint32_t encoded_cp)
4b6a83fb 8246{
60322b39 8247 return g_hash_table_lookup(cpregs, &encoded_cp);
4b6a83fb
PM
8248}
8249
c4241c7d
PM
8250void arm_cp_write_ignore(CPUARMState *env, const ARMCPRegInfo *ri,
8251 uint64_t value)
4b6a83fb
PM
8252{
8253 /* Helper coprocessor write function for write-ignore registers */
4b6a83fb
PM
8254}
8255
c4241c7d 8256uint64_t arm_cp_read_zero(CPUARMState *env, const ARMCPRegInfo *ri)
4b6a83fb
PM
8257{
8258 /* Helper coprocessor write function for read-as-zero registers */
4b6a83fb
PM
8259 return 0;
8260}
8261
f5a0a5a5
PM
8262void arm_cp_reset_ignore(CPUARMState *env, const ARMCPRegInfo *opaque)
8263{
8264 /* Helper coprocessor reset function for do-nothing-on-reset registers */
8265}
8266
af393ffc 8267static int bad_mode_switch(CPUARMState *env, int mode, CPSRWriteType write_type)
37064a8b
PM
8268{
8269 /* Return true if it is not valid for us to switch to
8270 * this CPU mode (ie all the UNPREDICTABLE cases in
8271 * the ARM ARM CPSRWriteByInstr pseudocode).
8272 */
af393ffc
PM
8273
8274 /* Changes to or from Hyp via MSR and CPS are illegal. */
8275 if (write_type == CPSRWriteByInstr &&
8276 ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_HYP ||
8277 mode == ARM_CPU_MODE_HYP)) {
8278 return 1;
8279 }
8280
37064a8b
PM
8281 switch (mode) {
8282 case ARM_CPU_MODE_USR:
10eacda7 8283 return 0;
37064a8b
PM
8284 case ARM_CPU_MODE_SYS:
8285 case ARM_CPU_MODE_SVC:
8286 case ARM_CPU_MODE_ABT:
8287 case ARM_CPU_MODE_UND:
8288 case ARM_CPU_MODE_IRQ:
8289 case ARM_CPU_MODE_FIQ:
52ff951b
PM
8290 /* Note that we don't implement the IMPDEF NSACR.RFR which in v7
8291 * allows FIQ mode to be Secure-only. (In v8 this doesn't exist.)
8292 */
10eacda7
PM
8293 /* If HCR.TGE is set then changes from Monitor to NS PL1 via MSR
8294 * and CPS are treated as illegal mode changes.
8295 */
8296 if (write_type == CPSRWriteByInstr &&
10eacda7 8297 (env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON &&
7c208e0f 8298 (arm_hcr_el2_eff(env) & HCR_TGE)) {
10eacda7
PM
8299 return 1;
8300 }
37064a8b 8301 return 0;
e6c8fc07
PM
8302 case ARM_CPU_MODE_HYP:
8303 return !arm_feature(env, ARM_FEATURE_EL2)
2d2a4549 8304 || arm_current_el(env) < 2 || arm_is_secure_below_el3(env);
027fc527 8305 case ARM_CPU_MODE_MON:
58ae2d1f 8306 return arm_current_el(env) < 3;
37064a8b
PM
8307 default:
8308 return 1;
8309 }
8310}
8311
2f4a40e5
AZ
8312uint32_t cpsr_read(CPUARMState *env)
8313{
8314 int ZF;
6fbe23d5
PB
8315 ZF = (env->ZF == 0);
8316 return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) |
2f4a40e5
AZ
8317 (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27)
8318 | (env->thumb << 5) | ((env->condexec_bits & 3) << 25)
8319 | ((env->condexec_bits & 0xfc) << 8)
af519934 8320 | (env->GE << 16) | (env->daif & CPSR_AIF);
2f4a40e5
AZ
8321}
8322
50866ba5
PM
8323void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask,
8324 CPSRWriteType write_type)
2f4a40e5 8325{
6e8801f9
FA
8326 uint32_t changed_daif;
8327
2f4a40e5 8328 if (mask & CPSR_NZCV) {
6fbe23d5
PB
8329 env->ZF = (~val) & CPSR_Z;
8330 env->NF = val;
2f4a40e5
AZ
8331 env->CF = (val >> 29) & 1;
8332 env->VF = (val << 3) & 0x80000000;
8333 }
8334 if (mask & CPSR_Q)
8335 env->QF = ((val & CPSR_Q) != 0);
8336 if (mask & CPSR_T)
8337 env->thumb = ((val & CPSR_T) != 0);
8338 if (mask & CPSR_IT_0_1) {
8339 env->condexec_bits &= ~3;
8340 env->condexec_bits |= (val >> 25) & 3;
8341 }
8342 if (mask & CPSR_IT_2_7) {
8343 env->condexec_bits &= 3;
8344 env->condexec_bits |= (val >> 8) & 0xfc;
8345 }
8346 if (mask & CPSR_GE) {
8347 env->GE = (val >> 16) & 0xf;
8348 }
8349
6e8801f9
FA
8350 /* In a V7 implementation that includes the security extensions but does
8351 * not include Virtualization Extensions the SCR.FW and SCR.AW bits control
8352 * whether non-secure software is allowed to change the CPSR_F and CPSR_A
8353 * bits respectively.
8354 *
8355 * In a V8 implementation, it is permitted for privileged software to
8356 * change the CPSR A/F bits regardless of the SCR.AW/FW bits.
8357 */
f8c88bbc 8358 if (write_type != CPSRWriteRaw && !arm_feature(env, ARM_FEATURE_V8) &&
6e8801f9
FA
8359 arm_feature(env, ARM_FEATURE_EL3) &&
8360 !arm_feature(env, ARM_FEATURE_EL2) &&
8361 !arm_is_secure(env)) {
8362
8363 changed_daif = (env->daif ^ val) & mask;
8364
8365 if (changed_daif & CPSR_A) {
8366 /* Check to see if we are allowed to change the masking of async
8367 * abort exceptions from a non-secure state.
8368 */
8369 if (!(env->cp15.scr_el3 & SCR_AW)) {
8370 qemu_log_mask(LOG_GUEST_ERROR,
8371 "Ignoring attempt to switch CPSR_A flag from "
8372 "non-secure world with SCR.AW bit clear\n");
8373 mask &= ~CPSR_A;
8374 }
8375 }
8376
8377 if (changed_daif & CPSR_F) {
8378 /* Check to see if we are allowed to change the masking of FIQ
8379 * exceptions from a non-secure state.
8380 */
8381 if (!(env->cp15.scr_el3 & SCR_FW)) {
8382 qemu_log_mask(LOG_GUEST_ERROR,
8383 "Ignoring attempt to switch CPSR_F flag from "
8384 "non-secure world with SCR.FW bit clear\n");
8385 mask &= ~CPSR_F;
8386 }
8387
8388 /* Check whether non-maskable FIQ (NMFI) support is enabled.
8389 * If this bit is set software is not allowed to mask
8390 * FIQs, but is allowed to set CPSR_F to 0.
8391 */
8392 if ((A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_NMFI) &&
8393 (val & CPSR_F)) {
8394 qemu_log_mask(LOG_GUEST_ERROR,
8395 "Ignoring attempt to enable CPSR_F flag "
8396 "(non-maskable FIQ [NMFI] support enabled)\n");
8397 mask &= ~CPSR_F;
8398 }
8399 }
8400 }
8401
4cc35614
PM
8402 env->daif &= ~(CPSR_AIF & mask);
8403 env->daif |= val & CPSR_AIF & mask;
8404
f8c88bbc
PM
8405 if (write_type != CPSRWriteRaw &&
8406 ((env->uncached_cpsr ^ val) & mask & CPSR_M)) {
8c4f0eb9
PM
8407 if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_USR) {
8408 /* Note that we can only get here in USR mode if this is a
8409 * gdb stub write; for this case we follow the architectural
8410 * behaviour for guest writes in USR mode of ignoring an attempt
8411 * to switch mode. (Those are caught by translate.c for writes
8412 * triggered by guest instructions.)
8413 */
8414 mask &= ~CPSR_M;
8415 } else if (bad_mode_switch(env, val & CPSR_M, write_type)) {
81907a58
PM
8416 /* Attempt to switch to an invalid mode: this is UNPREDICTABLE in
8417 * v7, and has defined behaviour in v8:
8418 * + leave CPSR.M untouched
8419 * + allow changes to the other CPSR fields
8420 * + set PSTATE.IL
8421 * For user changes via the GDB stub, we don't set PSTATE.IL,
8422 * as this would be unnecessarily harsh for a user error.
37064a8b
PM
8423 */
8424 mask &= ~CPSR_M;
81907a58
PM
8425 if (write_type != CPSRWriteByGDBStub &&
8426 arm_feature(env, ARM_FEATURE_V8)) {
8427 mask |= CPSR_IL;
8428 val |= CPSR_IL;
8429 }
81e37284
PM
8430 qemu_log_mask(LOG_GUEST_ERROR,
8431 "Illegal AArch32 mode switch attempt from %s to %s\n",
8432 aarch32_mode_name(env->uncached_cpsr),
8433 aarch32_mode_name(val));
37064a8b 8434 } else {
81e37284
PM
8435 qemu_log_mask(CPU_LOG_INT, "%s %s to %s PC 0x%" PRIx32 "\n",
8436 write_type == CPSRWriteExceptionReturn ?
8437 "Exception return from AArch32" :
8438 "AArch32 mode switch from",
8439 aarch32_mode_name(env->uncached_cpsr),
8440 aarch32_mode_name(val), env->regs[15]);
37064a8b
PM
8441 switch_mode(env, val & CPSR_M);
8442 }
2f4a40e5
AZ
8443 }
8444 mask &= ~CACHED_CPSR_BITS;
8445 env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask);
8446}
8447
b26eefb6
PB
8448/* Sign/zero extend */
8449uint32_t HELPER(sxtb16)(uint32_t x)
8450{
8451 uint32_t res;
8452 res = (uint16_t)(int8_t)x;
8453 res |= (uint32_t)(int8_t)(x >> 16) << 16;
8454 return res;
8455}
8456
8457uint32_t HELPER(uxtb16)(uint32_t x)
8458{
8459 uint32_t res;
8460 res = (uint16_t)(uint8_t)x;
8461 res |= (uint32_t)(uint8_t)(x >> 16) << 16;
8462 return res;
8463}
8464
3670669c
PB
8465int32_t HELPER(sdiv)(int32_t num, int32_t den)
8466{
8467 if (den == 0)
8468 return 0;
686eeb93
AJ
8469 if (num == INT_MIN && den == -1)
8470 return INT_MIN;
3670669c
PB
8471 return num / den;
8472}
8473
8474uint32_t HELPER(udiv)(uint32_t num, uint32_t den)
8475{
8476 if (den == 0)
8477 return 0;
8478 return num / den;
8479}
8480
8481uint32_t HELPER(rbit)(uint32_t x)
8482{
42fedbca 8483 return revbit32(x);
3670669c
PB
8484}
8485
c47eaf9f 8486#ifdef CONFIG_USER_ONLY
b5ff1b31 8487
affdb64d 8488static void switch_mode(CPUARMState *env, int mode)
b5ff1b31 8489{
2fc0cc0e 8490 ARMCPU *cpu = env_archcpu(env);
a47dddd7
AF
8491
8492 if (mode != ARM_CPU_MODE_USR) {
8493 cpu_abort(CPU(cpu), "Tried to switch out of user mode\n");
8494 }
b5ff1b31
FB
8495}
8496
012a906b
GB
8497uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
8498 uint32_t cur_el, bool secure)
9e729b57
EI
8499{
8500 return 1;
8501}
8502
ce02049d
GB
8503void aarch64_sync_64_to_32(CPUARMState *env)
8504{
8505 g_assert_not_reached();
8506}
8507
b5ff1b31
FB
8508#else
8509
affdb64d 8510static void switch_mode(CPUARMState *env, int mode)
b5ff1b31
FB
8511{
8512 int old_mode;
8513 int i;
8514
8515 old_mode = env->uncached_cpsr & CPSR_M;
8516 if (mode == old_mode)
8517 return;
8518
8519 if (old_mode == ARM_CPU_MODE_FIQ) {
8520 memcpy (env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t));
8637c67f 8521 memcpy (env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t));
b5ff1b31
FB
8522 } else if (mode == ARM_CPU_MODE_FIQ) {
8523 memcpy (env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t));
8637c67f 8524 memcpy (env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t));
b5ff1b31
FB
8525 }
8526
f5206413 8527 i = bank_number(old_mode);
b5ff1b31 8528 env->banked_r13[i] = env->regs[13];
b5ff1b31
FB
8529 env->banked_spsr[i] = env->spsr;
8530
f5206413 8531 i = bank_number(mode);
b5ff1b31 8532 env->regs[13] = env->banked_r13[i];
b5ff1b31 8533 env->spsr = env->banked_spsr[i];
593cfa2b
PM
8534
8535 env->banked_r14[r14_bank_number(old_mode)] = env->regs[14];
8536 env->regs[14] = env->banked_r14[r14_bank_number(mode)];
b5ff1b31
FB
8537}
8538
0eeb17d6
GB
8539/* Physical Interrupt Target EL Lookup Table
8540 *
8541 * [ From ARM ARM section G1.13.4 (Table G1-15) ]
8542 *
8543 * The below multi-dimensional table is used for looking up the target
8544 * exception level given numerous condition criteria. Specifically, the
8545 * target EL is based on SCR and HCR routing controls as well as the
8546 * currently executing EL and secure state.
8547 *
8548 * Dimensions:
8549 * target_el_table[2][2][2][2][2][4]
8550 * | | | | | +--- Current EL
8551 * | | | | +------ Non-secure(0)/Secure(1)
8552 * | | | +--------- HCR mask override
8553 * | | +------------ SCR exec state control
8554 * | +--------------- SCR mask override
8555 * +------------------ 32-bit(0)/64-bit(1) EL3
8556 *
8557 * The table values are as such:
8558 * 0-3 = EL0-EL3
8559 * -1 = Cannot occur
8560 *
8561 * The ARM ARM target EL table includes entries indicating that an "exception
8562 * is not taken". The two cases where this is applicable are:
8563 * 1) An exception is taken from EL3 but the SCR does not have the exception
8564 * routed to EL3.
8565 * 2) An exception is taken from EL2 but the HCR does not have the exception
8566 * routed to EL2.
8567 * In these two cases, the below table contain a target of EL1. This value is
8568 * returned as it is expected that the consumer of the table data will check
8569 * for "target EL >= current EL" to ensure the exception is not taken.
8570 *
8571 * SCR HCR
8572 * 64 EA AMO From
8573 * BIT IRQ IMO Non-secure Secure
8574 * EL3 FIQ RW FMO EL0 EL1 EL2 EL3 EL0 EL1 EL2 EL3
8575 */
82c39f6a 8576static const int8_t target_el_table[2][2][2][2][2][4] = {
0eeb17d6
GB
8577 {{{{/* 0 0 0 0 */{ 1, 1, 2, -1 },{ 3, -1, -1, 3 },},
8578 {/* 0 0 0 1 */{ 2, 2, 2, -1 },{ 3, -1, -1, 3 },},},
8579 {{/* 0 0 1 0 */{ 1, 1, 2, -1 },{ 3, -1, -1, 3 },},
8580 {/* 0 0 1 1 */{ 2, 2, 2, -1 },{ 3, -1, -1, 3 },},},},
8581 {{{/* 0 1 0 0 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},
8582 {/* 0 1 0 1 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},},
8583 {{/* 0 1 1 0 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},
8584 {/* 0 1 1 1 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},},},},
8585 {{{{/* 1 0 0 0 */{ 1, 1, 2, -1 },{ 1, 1, -1, 1 },},
8586 {/* 1 0 0 1 */{ 2, 2, 2, -1 },{ 1, 1, -1, 1 },},},
8587 {{/* 1 0 1 0 */{ 1, 1, 1, -1 },{ 1, 1, -1, 1 },},
8588 {/* 1 0 1 1 */{ 2, 2, 2, -1 },{ 1, 1, -1, 1 },},},},
8589 {{{/* 1 1 0 0 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},
8590 {/* 1 1 0 1 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},},
8591 {{/* 1 1 1 0 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},
8592 {/* 1 1 1 1 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},},},},
8593};
8594
8595/*
8596 * Determine the target EL for physical exceptions
8597 */
012a906b
GB
8598uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
8599 uint32_t cur_el, bool secure)
0eeb17d6
GB
8600{
8601 CPUARMState *env = cs->env_ptr;
f7778444
RH
8602 bool rw;
8603 bool scr;
8604 bool hcr;
0eeb17d6 8605 int target_el;
2cde031f 8606 /* Is the highest EL AArch64? */
f7778444
RH
8607 bool is64 = arm_feature(env, ARM_FEATURE_AARCH64);
8608 uint64_t hcr_el2;
2cde031f
SS
8609
8610 if (arm_feature(env, ARM_FEATURE_EL3)) {
8611 rw = ((env->cp15.scr_el3 & SCR_RW) == SCR_RW);
8612 } else {
8613 /* Either EL2 is the highest EL (and so the EL2 register width
8614 * is given by is64); or there is no EL2 or EL3, in which case
8615 * the value of 'rw' does not affect the table lookup anyway.
8616 */
8617 rw = is64;
8618 }
0eeb17d6 8619
f7778444 8620 hcr_el2 = arm_hcr_el2_eff(env);
0eeb17d6
GB
8621 switch (excp_idx) {
8622 case EXCP_IRQ:
8623 scr = ((env->cp15.scr_el3 & SCR_IRQ) == SCR_IRQ);
f7778444 8624 hcr = hcr_el2 & HCR_IMO;
0eeb17d6
GB
8625 break;
8626 case EXCP_FIQ:
8627 scr = ((env->cp15.scr_el3 & SCR_FIQ) == SCR_FIQ);
f7778444 8628 hcr = hcr_el2 & HCR_FMO;
0eeb17d6
GB
8629 break;
8630 default:
8631 scr = ((env->cp15.scr_el3 & SCR_EA) == SCR_EA);
f7778444 8632 hcr = hcr_el2 & HCR_AMO;
0eeb17d6
GB
8633 break;
8634 };
8635
d1b31428
RH
8636 /*
8637 * For these purposes, TGE and AMO/IMO/FMO both force the
8638 * interrupt to EL2. Fold TGE into the bit extracted above.
8639 */
8640 hcr |= (hcr_el2 & HCR_TGE) != 0;
8641
0eeb17d6
GB
8642 /* Perform a table-lookup for the target EL given the current state */
8643 target_el = target_el_table[is64][scr][rw][hcr][secure][cur_el];
8644
8645 assert(target_el > 0);
8646
8647 return target_el;
8648}
8649
b59f479b
PMD
8650void arm_log_exception(int idx)
8651{
8652 if (qemu_loglevel_mask(CPU_LOG_INT)) {
8653 const char *exc = NULL;
8654 static const char * const excnames[] = {
8655 [EXCP_UDEF] = "Undefined Instruction",
8656 [EXCP_SWI] = "SVC",
8657 [EXCP_PREFETCH_ABORT] = "Prefetch Abort",
8658 [EXCP_DATA_ABORT] = "Data Abort",
8659 [EXCP_IRQ] = "IRQ",
8660 [EXCP_FIQ] = "FIQ",
8661 [EXCP_BKPT] = "Breakpoint",
8662 [EXCP_EXCEPTION_EXIT] = "QEMU v7M exception exit",
8663 [EXCP_KERNEL_TRAP] = "QEMU intercept of kernel commpage",
8664 [EXCP_HVC] = "Hypervisor Call",
8665 [EXCP_HYP_TRAP] = "Hypervisor Trap",
8666 [EXCP_SMC] = "Secure Monitor Call",
8667 [EXCP_VIRQ] = "Virtual IRQ",
8668 [EXCP_VFIQ] = "Virtual FIQ",
8669 [EXCP_SEMIHOST] = "Semihosting call",
8670 [EXCP_NOCP] = "v7M NOCP UsageFault",
8671 [EXCP_INVSTATE] = "v7M INVSTATE UsageFault",
8672 [EXCP_STKOF] = "v8M STKOF UsageFault",
8673 [EXCP_LAZYFP] = "v7M exception during lazy FP stacking",
8674 [EXCP_LSERR] = "v8M LSERR UsageFault",
8675 [EXCP_UNALIGNED] = "v7M UNALIGNED UsageFault",
8676 };
8677
8678 if (idx >= 0 && idx < ARRAY_SIZE(excnames)) {
8679 exc = excnames[idx];
8680 }
8681 if (!exc) {
8682 exc = "unknown";
8683 }
8684 qemu_log_mask(CPU_LOG_INT, "Taking exception %d [%s]\n", idx, exc);
8685 }
8686}
8687
a356dacf 8688/*
7aab5a8c
PMD
8689 * Function used to synchronize QEMU's AArch64 register set with AArch32
8690 * register set. This is necessary when switching between AArch32 and AArch64
8691 * execution state.
a356dacf 8692 */
7aab5a8c 8693void aarch64_sync_32_to_64(CPUARMState *env)
9ee6e8bb 8694{
7aab5a8c
PMD
8695 int i;
8696 uint32_t mode = env->uncached_cpsr & CPSR_M;
8697
8698 /* We can blanket copy R[0:7] to X[0:7] */
8699 for (i = 0; i < 8; i++) {
8700 env->xregs[i] = env->regs[i];
fd592d89 8701 }
70d74660 8702
9a223097 8703 /*
7aab5a8c
PMD
8704 * Unless we are in FIQ mode, x8-x12 come from the user registers r8-r12.
8705 * Otherwise, they come from the banked user regs.
fd592d89 8706 */
7aab5a8c
PMD
8707 if (mode == ARM_CPU_MODE_FIQ) {
8708 for (i = 8; i < 13; i++) {
8709 env->xregs[i] = env->usr_regs[i - 8];
8710 }
8711 } else {
8712 for (i = 8; i < 13; i++) {
8713 env->xregs[i] = env->regs[i];
8714 }
fd592d89 8715 }
9ee6e8bb 8716
7aab5a8c
PMD
8717 /*
8718 * Registers x13-x23 are the various mode SP and FP registers. Registers
8719 * r13 and r14 are only copied if we are in that mode, otherwise we copy
8720 * from the mode banked register.
8721 */
8722 if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
8723 env->xregs[13] = env->regs[13];
8724 env->xregs[14] = env->regs[14];
8725 } else {
8726 env->xregs[13] = env->banked_r13[bank_number(ARM_CPU_MODE_USR)];
8727 /* HYP is an exception in that it is copied from r14 */
8728 if (mode == ARM_CPU_MODE_HYP) {
8729 env->xregs[14] = env->regs[14];
95695eff 8730 } else {
7aab5a8c 8731 env->xregs[14] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)];
95695eff 8732 }
95695eff
PM
8733 }
8734
7aab5a8c
PMD
8735 if (mode == ARM_CPU_MODE_HYP) {
8736 env->xregs[15] = env->regs[13];
8737 } else {
8738 env->xregs[15] = env->banked_r13[bank_number(ARM_CPU_MODE_HYP)];
95695eff
PM
8739 }
8740
7aab5a8c
PMD
8741 if (mode == ARM_CPU_MODE_IRQ) {
8742 env->xregs[16] = env->regs[14];
8743 env->xregs[17] = env->regs[13];
8744 } else {
8745 env->xregs[16] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)];
8746 env->xregs[17] = env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)];
8747 }
95695eff 8748
7aab5a8c
PMD
8749 if (mode == ARM_CPU_MODE_SVC) {
8750 env->xregs[18] = env->regs[14];
8751 env->xregs[19] = env->regs[13];
8752 } else {
8753 env->xregs[18] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)];
8754 env->xregs[19] = env->banked_r13[bank_number(ARM_CPU_MODE_SVC)];
8755 }
95695eff 8756
7aab5a8c
PMD
8757 if (mode == ARM_CPU_MODE_ABT) {
8758 env->xregs[20] = env->regs[14];
8759 env->xregs[21] = env->regs[13];
8760 } else {
8761 env->xregs[20] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)];
8762 env->xregs[21] = env->banked_r13[bank_number(ARM_CPU_MODE_ABT)];
8763 }
e33cf0f8 8764
7aab5a8c
PMD
8765 if (mode == ARM_CPU_MODE_UND) {
8766 env->xregs[22] = env->regs[14];
8767 env->xregs[23] = env->regs[13];
8768 } else {
8769 env->xregs[22] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)];
8770 env->xregs[23] = env->banked_r13[bank_number(ARM_CPU_MODE_UND)];
e33cf0f8
PM
8771 }
8772
8773 /*
7aab5a8c
PMD
8774 * Registers x24-x30 are mapped to r8-r14 in FIQ mode. If we are in FIQ
8775 * mode, then we can copy from r8-r14. Otherwise, we copy from the
8776 * FIQ bank for r8-r14.
e33cf0f8 8777 */
7aab5a8c
PMD
8778 if (mode == ARM_CPU_MODE_FIQ) {
8779 for (i = 24; i < 31; i++) {
8780 env->xregs[i] = env->regs[i - 16]; /* X[24:30] <- R[8:14] */
8781 }
8782 } else {
8783 for (i = 24; i < 29; i++) {
8784 env->xregs[i] = env->fiq_regs[i - 24];
e33cf0f8 8785 }
7aab5a8c
PMD
8786 env->xregs[29] = env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)];
8787 env->xregs[30] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)];
e33cf0f8 8788 }
7aab5a8c
PMD
8789
8790 env->pc = env->regs[15];
e33cf0f8
PM
8791}
8792
9a223097 8793/*
7aab5a8c
PMD
8794 * Function used to synchronize QEMU's AArch32 register set with AArch64
8795 * register set. This is necessary when switching between AArch32 and AArch64
8796 * execution state.
de2db7ec 8797 */
7aab5a8c 8798void aarch64_sync_64_to_32(CPUARMState *env)
9ee6e8bb 8799{
7aab5a8c
PMD
8800 int i;
8801 uint32_t mode = env->uncached_cpsr & CPSR_M;
abc24d86 8802
7aab5a8c
PMD
8803 /* We can blanket copy X[0:7] to R[0:7] */
8804 for (i = 0; i < 8; i++) {
8805 env->regs[i] = env->xregs[i];
de2db7ec 8806 }
3f0cddee 8807
9a223097 8808 /*
7aab5a8c
PMD
8809 * Unless we are in FIQ mode, r8-r12 come from the user registers x8-x12.
8810 * Otherwise, we copy x8-x12 into the banked user regs.
de2db7ec 8811 */
7aab5a8c
PMD
8812 if (mode == ARM_CPU_MODE_FIQ) {
8813 for (i = 8; i < 13; i++) {
8814 env->usr_regs[i - 8] = env->xregs[i];
8815 }
8816 } else {
8817 for (i = 8; i < 13; i++) {
8818 env->regs[i] = env->xregs[i];
8819 }
fb602cb7
PM
8820 }
8821
9a223097 8822 /*
7aab5a8c
PMD
8823 * Registers r13 & r14 depend on the current mode.
8824 * If we are in a given mode, we copy the corresponding x registers to r13
8825 * and r14. Otherwise, we copy the x register to the banked r13 and r14
8826 * for the mode.
fb602cb7 8827 */
7aab5a8c
PMD
8828 if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
8829 env->regs[13] = env->xregs[13];
8830 env->regs[14] = env->xregs[14];
fb602cb7 8831 } else {
7aab5a8c 8832 env->banked_r13[bank_number(ARM_CPU_MODE_USR)] = env->xregs[13];
fb602cb7 8833
7aab5a8c
PMD
8834 /*
8835 * HYP is an exception in that it does not have its own banked r14 but
8836 * shares the USR r14
8837 */
8838 if (mode == ARM_CPU_MODE_HYP) {
8839 env->regs[14] = env->xregs[14];
8840 } else {
8841 env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)] = env->xregs[14];
8842 }
8843 }
fb602cb7 8844
7aab5a8c
PMD
8845 if (mode == ARM_CPU_MODE_HYP) {
8846 env->regs[13] = env->xregs[15];
fb602cb7 8847 } else {
7aab5a8c 8848 env->banked_r13[bank_number(ARM_CPU_MODE_HYP)] = env->xregs[15];
fb602cb7 8849 }
d02a8698 8850
7aab5a8c
PMD
8851 if (mode == ARM_CPU_MODE_IRQ) {
8852 env->regs[14] = env->xregs[16];
8853 env->regs[13] = env->xregs[17];
d02a8698 8854 } else {
7aab5a8c
PMD
8855 env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[16];
8856 env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[17];
d02a8698
PM
8857 }
8858
7aab5a8c
PMD
8859 if (mode == ARM_CPU_MODE_SVC) {
8860 env->regs[14] = env->xregs[18];
8861 env->regs[13] = env->xregs[19];
8862 } else {
8863 env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)] = env->xregs[18];
8864 env->banked_r13[bank_number(ARM_CPU_MODE_SVC)] = env->xregs[19];
fb602cb7
PM
8865 }
8866
7aab5a8c
PMD
8867 if (mode == ARM_CPU_MODE_ABT) {
8868 env->regs[14] = env->xregs[20];
8869 env->regs[13] = env->xregs[21];
8870 } else {
8871 env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)] = env->xregs[20];
8872 env->banked_r13[bank_number(ARM_CPU_MODE_ABT)] = env->xregs[21];
ce02049d
GB
8873 }
8874
8875 if (mode == ARM_CPU_MODE_UND) {
3a9148d0
SS
8876 env->regs[14] = env->xregs[22];
8877 env->regs[13] = env->xregs[23];
ce02049d 8878 } else {
593cfa2b 8879 env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)] = env->xregs[22];
3a9148d0 8880 env->banked_r13[bank_number(ARM_CPU_MODE_UND)] = env->xregs[23];
ce02049d
GB
8881 }
8882
8883 /* Registers x24-x30 are mapped to r8-r14 in FIQ mode. If we are in FIQ
8884 * mode, then we can copy to r8-r14. Otherwise, we copy to the
8885 * FIQ bank for r8-r14.
8886 */
8887 if (mode == ARM_CPU_MODE_FIQ) {
8888 for (i = 24; i < 31; i++) {
8889 env->regs[i - 16] = env->xregs[i]; /* X[24:30] -> R[8:14] */
8890 }
8891 } else {
8892 for (i = 24; i < 29; i++) {
8893 env->fiq_regs[i - 24] = env->xregs[i];
8894 }
8895 env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[29];
593cfa2b 8896 env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[30];
ce02049d
GB
8897 }
8898
8899 env->regs[15] = env->pc;
8900}
8901
dea8378b
PM
8902static void take_aarch32_exception(CPUARMState *env, int new_mode,
8903 uint32_t mask, uint32_t offset,
8904 uint32_t newpc)
8905{
4a2696c0
RH
8906 int new_el;
8907
dea8378b
PM
8908 /* Change the CPU state so as to actually take the exception. */
8909 switch_mode(env, new_mode);
4a2696c0
RH
8910 new_el = arm_current_el(env);
8911
dea8378b
PM
8912 /*
8913 * For exceptions taken to AArch32 we must clear the SS bit in both
8914 * PSTATE and in the old-state value we save to SPSR_<mode>, so zero it now.
8915 */
8916 env->uncached_cpsr &= ~PSTATE_SS;
8917 env->spsr = cpsr_read(env);
8918 /* Clear IT bits. */
8919 env->condexec_bits = 0;
8920 /* Switch to the new mode, and to the correct instruction set. */
8921 env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode;
8922 /* Set new mode endianness */
8923 env->uncached_cpsr &= ~CPSR_E;
4a2696c0 8924 if (env->cp15.sctlr_el[new_el] & SCTLR_EE) {
dea8378b
PM
8925 env->uncached_cpsr |= CPSR_E;
8926 }
829f9fd3
PM
8927 /* J and IL must always be cleared for exception entry */
8928 env->uncached_cpsr &= ~(CPSR_IL | CPSR_J);
dea8378b
PM
8929 env->daif |= mask;
8930
8931 if (new_mode == ARM_CPU_MODE_HYP) {
8932 env->thumb = (env->cp15.sctlr_el[2] & SCTLR_TE) != 0;
8933 env->elr_el[2] = env->regs[15];
8934 } else {
4a2696c0 8935 /* CPSR.PAN is normally preserved preserved unless... */
f8af1143 8936 if (cpu_isar_feature(aa32_pan, env_archcpu(env))) {
4a2696c0
RH
8937 switch (new_el) {
8938 case 3:
8939 if (!arm_is_secure_below_el3(env)) {
8940 /* ... the target is EL3, from non-secure state. */
8941 env->uncached_cpsr &= ~CPSR_PAN;
8942 break;
8943 }
8944 /* ... the target is EL3, from secure state ... */
8945 /* fall through */
8946 case 1:
8947 /* ... the target is EL1 and SCTLR.SPAN is 0. */
8948 if (!(env->cp15.sctlr_el[new_el] & SCTLR_SPAN)) {
8949 env->uncached_cpsr |= CPSR_PAN;
8950 }
8951 break;
8952 }
8953 }
dea8378b
PM
8954 /*
8955 * this is a lie, as there was no c1_sys on V4T/V5, but who cares
8956 * and we should just guard the thumb mode on V4
8957 */
8958 if (arm_feature(env, ARM_FEATURE_V4T)) {
8959 env->thumb =
8960 (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_TE) != 0;
8961 }
8962 env->regs[14] = env->regs[15] + offset;
8963 }
8964 env->regs[15] = newpc;
a8a79c7a 8965 arm_rebuild_hflags(env);
dea8378b
PM
8966}
8967
b9bc21ff
PM
8968static void arm_cpu_do_interrupt_aarch32_hyp(CPUState *cs)
8969{
8970 /*
8971 * Handle exception entry to Hyp mode; this is sufficiently
8972 * different to entry to other AArch32 modes that we handle it
8973 * separately here.
8974 *
8975 * The vector table entry used is always the 0x14 Hyp mode entry point,
8976 * unless this is an UNDEF/HVC/abort taken from Hyp to Hyp.
8977 * The offset applied to the preferred return address is always zero
8978 * (see DDI0487C.a section G1.12.3).
8979 * PSTATE A/I/F masks are set based only on the SCR.EA/IRQ/FIQ values.
8980 */
8981 uint32_t addr, mask;
8982 ARMCPU *cpu = ARM_CPU(cs);
8983 CPUARMState *env = &cpu->env;
8984
8985 switch (cs->exception_index) {
8986 case EXCP_UDEF:
8987 addr = 0x04;
8988 break;
8989 case EXCP_SWI:
8990 addr = 0x14;
8991 break;
8992 case EXCP_BKPT:
8993 /* Fall through to prefetch abort. */
8994 case EXCP_PREFETCH_ABORT:
8995 env->cp15.ifar_s = env->exception.vaddress;
8996 qemu_log_mask(CPU_LOG_INT, "...with HIFAR 0x%x\n",
8997 (uint32_t)env->exception.vaddress);
8998 addr = 0x0c;
8999 break;
9000 case EXCP_DATA_ABORT:
9001 env->cp15.dfar_s = env->exception.vaddress;
9002 qemu_log_mask(CPU_LOG_INT, "...with HDFAR 0x%x\n",
9003 (uint32_t)env->exception.vaddress);
9004 addr = 0x10;
9005 break;
9006 case EXCP_IRQ:
9007 addr = 0x18;
9008 break;
9009 case EXCP_FIQ:
9010 addr = 0x1c;
9011 break;
9012 case EXCP_HVC:
9013 addr = 0x08;
9014 break;
9015 case EXCP_HYP_TRAP:
9016 addr = 0x14;
9bbb4ef9 9017 break;
b9bc21ff
PM
9018 default:
9019 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
9020 }
9021
9022 if (cs->exception_index != EXCP_IRQ && cs->exception_index != EXCP_FIQ) {
2ed08180
PM
9023 if (!arm_feature(env, ARM_FEATURE_V8)) {
9024 /*
9025 * QEMU syndrome values are v8-style. v7 has the IL bit
9026 * UNK/SBZP for "field not valid" cases, where v8 uses RES1.
9027 * If this is a v7 CPU, squash the IL bit in those cases.
9028 */
9029 if (cs->exception_index == EXCP_PREFETCH_ABORT ||
9030 (cs->exception_index == EXCP_DATA_ABORT &&
9031 !(env->exception.syndrome & ARM_EL_ISV)) ||
9032 syn_get_ec(env->exception.syndrome) == EC_UNCATEGORIZED) {
9033 env->exception.syndrome &= ~ARM_EL_IL;
9034 }
9035 }
b9bc21ff
PM
9036 env->cp15.esr_el[2] = env->exception.syndrome;
9037 }
9038
9039 if (arm_current_el(env) != 2 && addr < 0x14) {
9040 addr = 0x14;
9041 }
9042
9043 mask = 0;
9044 if (!(env->cp15.scr_el3 & SCR_EA)) {
9045 mask |= CPSR_A;
9046 }
9047 if (!(env->cp15.scr_el3 & SCR_IRQ)) {
9048 mask |= CPSR_I;
9049 }
9050 if (!(env->cp15.scr_el3 & SCR_FIQ)) {
9051 mask |= CPSR_F;
9052 }
9053
9054 addr += env->cp15.hvbar;
9055
9056 take_aarch32_exception(env, ARM_CPU_MODE_HYP, mask, 0, addr);
9057}
9058
966f758c 9059static void arm_cpu_do_interrupt_aarch32(CPUState *cs)
b5ff1b31 9060{
97a8ea5a
AF
9061 ARMCPU *cpu = ARM_CPU(cs);
9062 CPUARMState *env = &cpu->env;
b5ff1b31
FB
9063 uint32_t addr;
9064 uint32_t mask;
9065 int new_mode;
9066 uint32_t offset;
16a906fd 9067 uint32_t moe;
b5ff1b31 9068
16a906fd 9069 /* If this is a debug exception we must update the DBGDSCR.MOE bits */
64b91e3f 9070 switch (syn_get_ec(env->exception.syndrome)) {
16a906fd
PM
9071 case EC_BREAKPOINT:
9072 case EC_BREAKPOINT_SAME_EL:
9073 moe = 1;
9074 break;
9075 case EC_WATCHPOINT:
9076 case EC_WATCHPOINT_SAME_EL:
9077 moe = 10;
9078 break;
9079 case EC_AA32_BKPT:
9080 moe = 3;
9081 break;
9082 case EC_VECTORCATCH:
9083 moe = 5;
9084 break;
9085 default:
9086 moe = 0;
9087 break;
9088 }
9089
9090 if (moe) {
9091 env->cp15.mdscr_el1 = deposit64(env->cp15.mdscr_el1, 2, 4, moe);
9092 }
9093
b9bc21ff
PM
9094 if (env->exception.target_el == 2) {
9095 arm_cpu_do_interrupt_aarch32_hyp(cs);
9096 return;
9097 }
9098
27103424 9099 switch (cs->exception_index) {
b5ff1b31
FB
9100 case EXCP_UDEF:
9101 new_mode = ARM_CPU_MODE_UND;
9102 addr = 0x04;
9103 mask = CPSR_I;
9104 if (env->thumb)
9105 offset = 2;
9106 else
9107 offset = 4;
9108 break;
9109 case EXCP_SWI:
9110 new_mode = ARM_CPU_MODE_SVC;
9111 addr = 0x08;
9112 mask = CPSR_I;
601d70b9 9113 /* The PC already points to the next instruction. */
b5ff1b31
FB
9114 offset = 0;
9115 break;
06c949e6 9116 case EXCP_BKPT:
9ee6e8bb
PB
9117 /* Fall through to prefetch abort. */
9118 case EXCP_PREFETCH_ABORT:
88ca1c2d 9119 A32_BANKED_CURRENT_REG_SET(env, ifsr, env->exception.fsr);
b848ce2b 9120 A32_BANKED_CURRENT_REG_SET(env, ifar, env->exception.vaddress);
3f1beaca 9121 qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x IFAR 0x%x\n",
88ca1c2d 9122 env->exception.fsr, (uint32_t)env->exception.vaddress);
b5ff1b31
FB
9123 new_mode = ARM_CPU_MODE_ABT;
9124 addr = 0x0c;
9125 mask = CPSR_A | CPSR_I;
9126 offset = 4;
9127 break;
9128 case EXCP_DATA_ABORT:
4a7e2d73 9129 A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr);
b848ce2b 9130 A32_BANKED_CURRENT_REG_SET(env, dfar, env->exception.vaddress);
3f1beaca 9131 qemu_log_mask(CPU_LOG_INT, "...with DFSR 0x%x DFAR 0x%x\n",
4a7e2d73 9132 env->exception.fsr,
6cd8a264 9133 (uint32_t)env->exception.vaddress);
b5ff1b31
FB
9134 new_mode = ARM_CPU_MODE_ABT;
9135 addr = 0x10;
9136 mask = CPSR_A | CPSR_I;
9137 offset = 8;
9138 break;
9139 case EXCP_IRQ:
9140 new_mode = ARM_CPU_MODE_IRQ;
9141 addr = 0x18;
9142 /* Disable IRQ and imprecise data aborts. */
9143 mask = CPSR_A | CPSR_I;
9144 offset = 4;
de38d23b
FA
9145 if (env->cp15.scr_el3 & SCR_IRQ) {
9146 /* IRQ routed to monitor mode */
9147 new_mode = ARM_CPU_MODE_MON;
9148 mask |= CPSR_F;
9149 }
b5ff1b31
FB
9150 break;
9151 case EXCP_FIQ:
9152 new_mode = ARM_CPU_MODE_FIQ;
9153 addr = 0x1c;
9154 /* Disable FIQ, IRQ and imprecise data aborts. */
9155 mask = CPSR_A | CPSR_I | CPSR_F;
de38d23b
FA
9156 if (env->cp15.scr_el3 & SCR_FIQ) {
9157 /* FIQ routed to monitor mode */
9158 new_mode = ARM_CPU_MODE_MON;
9159 }
b5ff1b31
FB
9160 offset = 4;
9161 break;
87a4b270
PM
9162 case EXCP_VIRQ:
9163 new_mode = ARM_CPU_MODE_IRQ;
9164 addr = 0x18;
9165 /* Disable IRQ and imprecise data aborts. */
9166 mask = CPSR_A | CPSR_I;
9167 offset = 4;
9168 break;
9169 case EXCP_VFIQ:
9170 new_mode = ARM_CPU_MODE_FIQ;
9171 addr = 0x1c;
9172 /* Disable FIQ, IRQ and imprecise data aborts. */
9173 mask = CPSR_A | CPSR_I | CPSR_F;
9174 offset = 4;
9175 break;
dbe9d163
FA
9176 case EXCP_SMC:
9177 new_mode = ARM_CPU_MODE_MON;
9178 addr = 0x08;
9179 mask = CPSR_A | CPSR_I | CPSR_F;
9180 offset = 0;
9181 break;
b5ff1b31 9182 default:
a47dddd7 9183 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
b5ff1b31
FB
9184 return; /* Never happens. Keep compiler happy. */
9185 }
e89e51a1
FA
9186
9187 if (new_mode == ARM_CPU_MODE_MON) {
9188 addr += env->cp15.mvbar;
137feaa9 9189 } else if (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_V) {
e89e51a1 9190 /* High vectors. When enabled, base address cannot be remapped. */
b5ff1b31 9191 addr += 0xffff0000;
8641136c
NR
9192 } else {
9193 /* ARM v7 architectures provide a vector base address register to remap
9194 * the interrupt vector table.
e89e51a1 9195 * This register is only followed in non-monitor mode, and is banked.
8641136c
NR
9196 * Note: only bits 31:5 are valid.
9197 */
fb6c91ba 9198 addr += A32_BANKED_CURRENT_REG_GET(env, vbar);
b5ff1b31 9199 }
dbe9d163
FA
9200
9201 if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON) {
9202 env->cp15.scr_el3 &= ~SCR_NS;
9203 }
9204
dea8378b 9205 take_aarch32_exception(env, new_mode, mask, offset, addr);
b5ff1b31
FB
9206}
9207
966f758c
PM
9208/* Handle exception entry to a target EL which is using AArch64 */
9209static void arm_cpu_do_interrupt_aarch64(CPUState *cs)
f3a9b694
PM
9210{
9211 ARMCPU *cpu = ARM_CPU(cs);
9212 CPUARMState *env = &cpu->env;
9213 unsigned int new_el = env->exception.target_el;
9214 target_ulong addr = env->cp15.vbar_el[new_el];
9215 unsigned int new_mode = aarch64_pstate_mode(new_el, true);
4a2696c0 9216 unsigned int old_mode;
0ab5953b
RH
9217 unsigned int cur_el = arm_current_el(env);
9218
9a05f7b6
RH
9219 /*
9220 * Note that new_el can never be 0. If cur_el is 0, then
9221 * el0_a64 is is_a64(), else el0_a64 is ignored.
9222 */
9223 aarch64_sve_change_el(env, cur_el, new_el, is_a64(env));
f3a9b694 9224
0ab5953b 9225 if (cur_el < new_el) {
3d6f7617
PM
9226 /* Entry vector offset depends on whether the implemented EL
9227 * immediately lower than the target level is using AArch32 or AArch64
9228 */
9229 bool is_aa64;
cb092fbb 9230 uint64_t hcr;
3d6f7617
PM
9231
9232 switch (new_el) {
9233 case 3:
9234 is_aa64 = (env->cp15.scr_el3 & SCR_RW) != 0;
9235 break;
9236 case 2:
cb092fbb
RH
9237 hcr = arm_hcr_el2_eff(env);
9238 if ((hcr & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
9239 is_aa64 = (hcr & HCR_RW) != 0;
9240 break;
9241 }
9242 /* fall through */
3d6f7617
PM
9243 case 1:
9244 is_aa64 = is_a64(env);
9245 break;
9246 default:
9247 g_assert_not_reached();
9248 }
9249
9250 if (is_aa64) {
f3a9b694
PM
9251 addr += 0x400;
9252 } else {
9253 addr += 0x600;
9254 }
9255 } else if (pstate_read(env) & PSTATE_SP) {
9256 addr += 0x200;
9257 }
9258
f3a9b694
PM
9259 switch (cs->exception_index) {
9260 case EXCP_PREFETCH_ABORT:
9261 case EXCP_DATA_ABORT:
9262 env->cp15.far_el[new_el] = env->exception.vaddress;
9263 qemu_log_mask(CPU_LOG_INT, "...with FAR 0x%" PRIx64 "\n",
9264 env->cp15.far_el[new_el]);
9265 /* fall through */
9266 case EXCP_BKPT:
9267 case EXCP_UDEF:
9268 case EXCP_SWI:
9269 case EXCP_HVC:
9270 case EXCP_HYP_TRAP:
9271 case EXCP_SMC:
4be42f40
PM
9272 if (syn_get_ec(env->exception.syndrome) == EC_ADVSIMDFPACCESSTRAP) {
9273 /*
9274 * QEMU internal FP/SIMD syndromes from AArch32 include the
9275 * TA and coproc fields which are only exposed if the exception
9276 * is taken to AArch32 Hyp mode. Mask them out to get a valid
9277 * AArch64 format syndrome.
9278 */
9279 env->exception.syndrome &= ~MAKE_64BIT_MASK(0, 20);
9280 }
f3a9b694
PM
9281 env->cp15.esr_el[new_el] = env->exception.syndrome;
9282 break;
9283 case EXCP_IRQ:
9284 case EXCP_VIRQ:
9285 addr += 0x80;
9286 break;
9287 case EXCP_FIQ:
9288 case EXCP_VFIQ:
9289 addr += 0x100;
9290 break;
f3a9b694
PM
9291 default:
9292 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
9293 }
9294
9295 if (is_a64(env)) {
4a2696c0 9296 old_mode = pstate_read(env);
f3a9b694
PM
9297 aarch64_save_sp(env, arm_current_el(env));
9298 env->elr_el[new_el] = env->pc;
9299 } else {
4a2696c0 9300 old_mode = cpsr_read(env);
f3a9b694
PM
9301 env->elr_el[new_el] = env->regs[15];
9302
9303 aarch64_sync_32_to_64(env);
9304
9305 env->condexec_bits = 0;
9306 }
4a2696c0
RH
9307 env->banked_spsr[aarch64_banked_spsr_index(new_el)] = old_mode;
9308
f3a9b694
PM
9309 qemu_log_mask(CPU_LOG_INT, "...with ELR 0x%" PRIx64 "\n",
9310 env->elr_el[new_el]);
9311
4a2696c0
RH
9312 if (cpu_isar_feature(aa64_pan, cpu)) {
9313 /* The value of PSTATE.PAN is normally preserved, except when ... */
9314 new_mode |= old_mode & PSTATE_PAN;
9315 switch (new_el) {
9316 case 2:
9317 /* ... the target is EL2 with HCR_EL2.{E2H,TGE} == '11' ... */
9318 if ((arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE))
9319 != (HCR_E2H | HCR_TGE)) {
9320 break;
9321 }
9322 /* fall through */
9323 case 1:
9324 /* ... the target is EL1 ... */
9325 /* ... and SCTLR_ELx.SPAN == 0, then set to 1. */
9326 if ((env->cp15.sctlr_el[new_el] & SCTLR_SPAN) == 0) {
9327 new_mode |= PSTATE_PAN;
9328 }
9329 break;
9330 }
9331 }
9332
f3a9b694
PM
9333 pstate_write(env, PSTATE_DAIF | new_mode);
9334 env->aarch64 = 1;
9335 aarch64_restore_sp(env, new_el);
a8a79c7a 9336 helper_rebuild_hflags_a64(env, new_el);
f3a9b694
PM
9337
9338 env->pc = addr;
9339
9340 qemu_log_mask(CPU_LOG_INT, "...to EL%d PC 0x%" PRIx64 " PSTATE 0x%x\n",
9341 new_el, env->pc, pstate_read(env));
966f758c
PM
9342}
9343
ed6e6ba9
AB
9344/*
9345 * Do semihosting call and set the appropriate return value. All the
9346 * permission and validity checks have been done at translate time.
9347 *
9348 * We only see semihosting exceptions in TCG only as they are not
9349 * trapped to the hypervisor in KVM.
9350 */
91f78c58 9351#ifdef CONFIG_TCG
ed6e6ba9
AB
9352static void handle_semihosting(CPUState *cs)
9353{
904c04de
PM
9354 ARMCPU *cpu = ARM_CPU(cs);
9355 CPUARMState *env = &cpu->env;
9356
9357 if (is_a64(env)) {
ed6e6ba9
AB
9358 qemu_log_mask(CPU_LOG_INT,
9359 "...handling as semihosting call 0x%" PRIx64 "\n",
9360 env->xregs[0]);
9361 env->xregs[0] = do_arm_semihosting(env);
4ff5ef9e 9362 env->pc += 4;
904c04de 9363 } else {
904c04de
PM
9364 qemu_log_mask(CPU_LOG_INT,
9365 "...handling as semihosting call 0x%x\n",
9366 env->regs[0]);
9367 env->regs[0] = do_arm_semihosting(env);
4ff5ef9e 9368 env->regs[15] += env->thumb ? 2 : 4;
904c04de
PM
9369 }
9370}
ed6e6ba9 9371#endif
904c04de 9372
966f758c
PM
9373/* Handle a CPU exception for A and R profile CPUs.
9374 * Do any appropriate logging, handle PSCI calls, and then hand off
9375 * to the AArch64-entry or AArch32-entry function depending on the
9376 * target exception level's register width.
9377 */
9378void arm_cpu_do_interrupt(CPUState *cs)
9379{
9380 ARMCPU *cpu = ARM_CPU(cs);
9381 CPUARMState *env = &cpu->env;
9382 unsigned int new_el = env->exception.target_el;
9383
531c60a9 9384 assert(!arm_feature(env, ARM_FEATURE_M));
966f758c
PM
9385
9386 arm_log_exception(cs->exception_index);
9387 qemu_log_mask(CPU_LOG_INT, "...from EL%d to EL%d\n", arm_current_el(env),
9388 new_el);
9389 if (qemu_loglevel_mask(CPU_LOG_INT)
9390 && !excp_is_internal(cs->exception_index)) {
6568da45 9391 qemu_log_mask(CPU_LOG_INT, "...with ESR 0x%x/0x%" PRIx32 "\n",
64b91e3f 9392 syn_get_ec(env->exception.syndrome),
966f758c
PM
9393 env->exception.syndrome);
9394 }
9395
9396 if (arm_is_psci_call(cpu, cs->exception_index)) {
9397 arm_handle_psci_call(cpu);
9398 qemu_log_mask(CPU_LOG_INT, "...handled as PSCI call\n");
9399 return;
9400 }
9401
ed6e6ba9
AB
9402 /*
9403 * Semihosting semantics depend on the register width of the code
9404 * that caused the exception, not the target exception level, so
9405 * must be handled here.
966f758c 9406 */
ed6e6ba9
AB
9407#ifdef CONFIG_TCG
9408 if (cs->exception_index == EXCP_SEMIHOST) {
9409 handle_semihosting(cs);
904c04de
PM
9410 return;
9411 }
ed6e6ba9 9412#endif
904c04de 9413
b5c53d1b
AL
9414 /* Hooks may change global state so BQL should be held, also the
9415 * BQL needs to be held for any modification of
9416 * cs->interrupt_request.
9417 */
9418 g_assert(qemu_mutex_iothread_locked());
9419
9420 arm_call_pre_el_change_hook(cpu);
9421
904c04de
PM
9422 assert(!excp_is_internal(cs->exception_index));
9423 if (arm_el_is_aa64(env, new_el)) {
966f758c
PM
9424 arm_cpu_do_interrupt_aarch64(cs);
9425 } else {
9426 arm_cpu_do_interrupt_aarch32(cs);
9427 }
f3a9b694 9428
bd7d00fc
PM
9429 arm_call_el_change_hook(cpu);
9430
f3a9b694
PM
9431 if (!kvm_enabled()) {
9432 cs->interrupt_request |= CPU_INTERRUPT_EXITTB;
9433 }
9434}
c47eaf9f 9435#endif /* !CONFIG_USER_ONLY */
0480f69a
PM
9436
9437/* Return the exception level which controls this address translation regime */
b9f6033c 9438static uint32_t regime_el(CPUARMState *env, ARMMMUIdx mmu_idx)
0480f69a
PM
9439{
9440 switch (mmu_idx) {
b9f6033c
RH
9441 case ARMMMUIdx_E20_0:
9442 case ARMMMUIdx_E20_2:
452ef8cb 9443 case ARMMMUIdx_E20_2_PAN:
97fa9350 9444 case ARMMMUIdx_Stage2:
e013b741 9445 case ARMMMUIdx_E2:
0480f69a 9446 return 2;
127b2b08 9447 case ARMMMUIdx_SE3:
0480f69a 9448 return 3;
fba37aed 9449 case ARMMMUIdx_SE10_0:
0480f69a 9450 return arm_el_is_aa64(env, 3) ? 1 : 3;
fba37aed 9451 case ARMMMUIdx_SE10_1:
452ef8cb 9452 case ARMMMUIdx_SE10_1_PAN:
2859d7b5
RH
9453 case ARMMMUIdx_Stage1_E0:
9454 case ARMMMUIdx_Stage1_E1:
452ef8cb 9455 case ARMMMUIdx_Stage1_E1_PAN:
b9f6033c
RH
9456 case ARMMMUIdx_E10_0:
9457 case ARMMMUIdx_E10_1:
452ef8cb 9458 case ARMMMUIdx_E10_1_PAN:
62593718
PM
9459 case ARMMMUIdx_MPrivNegPri:
9460 case ARMMMUIdx_MUserNegPri:
e7b921c2
PM
9461 case ARMMMUIdx_MPriv:
9462 case ARMMMUIdx_MUser:
62593718
PM
9463 case ARMMMUIdx_MSPrivNegPri:
9464 case ARMMMUIdx_MSUserNegPri:
66787c78 9465 case ARMMMUIdx_MSPriv:
66787c78 9466 case ARMMMUIdx_MSUser:
0480f69a
PM
9467 return 1;
9468 default:
9469 g_assert_not_reached();
9470 }
9471}
9472
aaec1432
RH
9473uint64_t arm_sctlr(CPUARMState *env, int el)
9474{
9475 /* Only EL0 needs to be adjusted for EL1&0 or EL2&0. */
9476 if (el == 0) {
9477 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, 0);
9478 el = (mmu_idx == ARMMMUIdx_E20_0 ? 2 : 1);
9479 }
9480 return env->cp15.sctlr_el[el];
9481}
c47eaf9f 9482
0480f69a 9483/* Return the SCTLR value which controls this address translation regime */
aaec1432 9484static inline uint64_t regime_sctlr(CPUARMState *env, ARMMMUIdx mmu_idx)
0480f69a
PM
9485{
9486 return env->cp15.sctlr_el[regime_el(env, mmu_idx)];
9487}
9488
aaec1432
RH
9489#ifndef CONFIG_USER_ONLY
9490
0480f69a
PM
9491/* Return true if the specified stage of address translation is disabled */
9492static inline bool regime_translation_disabled(CPUARMState *env,
9493 ARMMMUIdx mmu_idx)
9494{
29c483a5 9495 if (arm_feature(env, ARM_FEATURE_M)) {
ecf5e8ea 9496 switch (env->v7m.mpu_ctrl[regime_is_secure(env, mmu_idx)] &
3bef7012
PM
9497 (R_V7M_MPU_CTRL_ENABLE_MASK | R_V7M_MPU_CTRL_HFNMIENA_MASK)) {
9498 case R_V7M_MPU_CTRL_ENABLE_MASK:
9499 /* Enabled, but not for HardFault and NMI */
62593718 9500 return mmu_idx & ARM_MMU_IDX_M_NEGPRI;
3bef7012
PM
9501 case R_V7M_MPU_CTRL_ENABLE_MASK | R_V7M_MPU_CTRL_HFNMIENA_MASK:
9502 /* Enabled for all cases */
9503 return false;
9504 case 0:
9505 default:
9506 /* HFNMIENA set and ENABLE clear is UNPREDICTABLE, but
9507 * we warned about that in armv7m_nvic.c when the guest set it.
9508 */
9509 return true;
9510 }
29c483a5
MD
9511 }
9512
97fa9350 9513 if (mmu_idx == ARMMMUIdx_Stage2) {
9d1bab33
PM
9514 /* HCR.DC means HCR.VM behaves as 1 */
9515 return (env->cp15.hcr_el2 & (HCR_DC | HCR_VM)) == 0;
0480f69a 9516 }
3d0e3080
PM
9517
9518 if (env->cp15.hcr_el2 & HCR_TGE) {
9519 /* TGE means that NS EL0/1 act as if SCTLR_EL1.M is zero */
9520 if (!regime_is_secure(env, mmu_idx) && regime_el(env, mmu_idx) == 1) {
9521 return true;
9522 }
9523 }
9524
fee7aa46 9525 if ((env->cp15.hcr_el2 & HCR_DC) && arm_mmu_idx_is_stage1_of_2(mmu_idx)) {
9d1bab33
PM
9526 /* HCR.DC means SCTLR_EL1.M behaves as 0 */
9527 return true;
9528 }
9529
0480f69a
PM
9530 return (regime_sctlr(env, mmu_idx) & SCTLR_M) == 0;
9531}
9532
73462ddd
PC
9533static inline bool regime_translation_big_endian(CPUARMState *env,
9534 ARMMMUIdx mmu_idx)
9535{
9536 return (regime_sctlr(env, mmu_idx) & SCTLR_EE) != 0;
9537}
9538
c47eaf9f
PM
9539/* Return the TTBR associated with this translation regime */
9540static inline uint64_t regime_ttbr(CPUARMState *env, ARMMMUIdx mmu_idx,
9541 int ttbrn)
9542{
97fa9350 9543 if (mmu_idx == ARMMMUIdx_Stage2) {
c47eaf9f
PM
9544 return env->cp15.vttbr_el2;
9545 }
9546 if (ttbrn == 0) {
9547 return env->cp15.ttbr0_el[regime_el(env, mmu_idx)];
9548 } else {
9549 return env->cp15.ttbr1_el[regime_el(env, mmu_idx)];
9550 }
9551}
9552
9553#endif /* !CONFIG_USER_ONLY */
9554
0480f69a
PM
9555/* Return the TCR controlling this translation regime */
9556static inline TCR *regime_tcr(CPUARMState *env, ARMMMUIdx mmu_idx)
9557{
97fa9350 9558 if (mmu_idx == ARMMMUIdx_Stage2) {
68e9c2fe 9559 return &env->cp15.vtcr_el2;
0480f69a
PM
9560 }
9561 return &env->cp15.tcr_el[regime_el(env, mmu_idx)];
9562}
9563
8bd5c820
PM
9564/* Convert a possible stage1+2 MMU index into the appropriate
9565 * stage 1 MMU index
9566 */
9567static inline ARMMMUIdx stage_1_mmu_idx(ARMMMUIdx mmu_idx)
9568{
b9f6033c
RH
9569 switch (mmu_idx) {
9570 case ARMMMUIdx_E10_0:
9571 return ARMMMUIdx_Stage1_E0;
9572 case ARMMMUIdx_E10_1:
9573 return ARMMMUIdx_Stage1_E1;
452ef8cb
RH
9574 case ARMMMUIdx_E10_1_PAN:
9575 return ARMMMUIdx_Stage1_E1_PAN;
b9f6033c
RH
9576 default:
9577 return mmu_idx;
8bd5c820 9578 }
8bd5c820
PM
9579}
9580
0480f69a
PM
9581/* Return true if the translation regime is using LPAE format page tables */
9582static inline bool regime_using_lpae_format(CPUARMState *env,
9583 ARMMMUIdx mmu_idx)
9584{
9585 int el = regime_el(env, mmu_idx);
9586 if (el == 2 || arm_el_is_aa64(env, el)) {
9587 return true;
9588 }
9589 if (arm_feature(env, ARM_FEATURE_LPAE)
9590 && (regime_tcr(env, mmu_idx)->raw_tcr & TTBCR_EAE)) {
9591 return true;
9592 }
9593 return false;
9594}
9595
deb2db99
AR
9596/* Returns true if the stage 1 translation regime is using LPAE format page
9597 * tables. Used when raising alignment exceptions, whose FSR changes depending
9598 * on whether the long or short descriptor format is in use. */
9599bool arm_s1_regime_using_lpae_format(CPUARMState *env, ARMMMUIdx mmu_idx)
30901475 9600{
8bd5c820 9601 mmu_idx = stage_1_mmu_idx(mmu_idx);
deb2db99 9602
30901475
AB
9603 return regime_using_lpae_format(env, mmu_idx);
9604}
9605
c47eaf9f 9606#ifndef CONFIG_USER_ONLY
0480f69a
PM
9607static inline bool regime_is_user(CPUARMState *env, ARMMMUIdx mmu_idx)
9608{
9609 switch (mmu_idx) {
fba37aed 9610 case ARMMMUIdx_SE10_0:
b9f6033c 9611 case ARMMMUIdx_E20_0:
2859d7b5 9612 case ARMMMUIdx_Stage1_E0:
e7b921c2 9613 case ARMMMUIdx_MUser:
871bec7c 9614 case ARMMMUIdx_MSUser:
62593718
PM
9615 case ARMMMUIdx_MUserNegPri:
9616 case ARMMMUIdx_MSUserNegPri:
0480f69a
PM
9617 return true;
9618 default:
9619 return false;
01b98b68
RH
9620 case ARMMMUIdx_E10_0:
9621 case ARMMMUIdx_E10_1:
452ef8cb 9622 case ARMMMUIdx_E10_1_PAN:
0480f69a
PM
9623 g_assert_not_reached();
9624 }
9625}
9626
0fbf5238
AJ
9627/* Translate section/page access permissions to page
9628 * R/W protection flags
d76951b6
AJ
9629 *
9630 * @env: CPUARMState
9631 * @mmu_idx: MMU index indicating required translation regime
9632 * @ap: The 3-bit access permissions (AP[2:0])
9633 * @domain_prot: The 2-bit domain access permissions
0fbf5238
AJ
9634 */
9635static inline int ap_to_rw_prot(CPUARMState *env, ARMMMUIdx mmu_idx,
9636 int ap, int domain_prot)
9637{
554b0b09
PM
9638 bool is_user = regime_is_user(env, mmu_idx);
9639
9640 if (domain_prot == 3) {
9641 return PAGE_READ | PAGE_WRITE;
9642 }
9643
554b0b09
PM
9644 switch (ap) {
9645 case 0:
9646 if (arm_feature(env, ARM_FEATURE_V7)) {
9647 return 0;
9648 }
554b0b09
PM
9649 switch (regime_sctlr(env, mmu_idx) & (SCTLR_S | SCTLR_R)) {
9650 case SCTLR_S:
9651 return is_user ? 0 : PAGE_READ;
9652 case SCTLR_R:
9653 return PAGE_READ;
9654 default:
9655 return 0;
9656 }
9657 case 1:
9658 return is_user ? 0 : PAGE_READ | PAGE_WRITE;
9659 case 2:
87c3d486 9660 if (is_user) {
0fbf5238 9661 return PAGE_READ;
87c3d486 9662 } else {
554b0b09 9663 return PAGE_READ | PAGE_WRITE;
87c3d486 9664 }
554b0b09
PM
9665 case 3:
9666 return PAGE_READ | PAGE_WRITE;
9667 case 4: /* Reserved. */
9668 return 0;
9669 case 5:
0fbf5238 9670 return is_user ? 0 : PAGE_READ;
554b0b09 9671 case 6:
0fbf5238 9672 return PAGE_READ;
554b0b09 9673 case 7:
87c3d486 9674 if (!arm_feature(env, ARM_FEATURE_V6K)) {
554b0b09 9675 return 0;
87c3d486 9676 }
0fbf5238 9677 return PAGE_READ;
554b0b09 9678 default:
0fbf5238 9679 g_assert_not_reached();
554b0b09 9680 }
b5ff1b31
FB
9681}
9682
d76951b6
AJ
9683/* Translate section/page access permissions to page
9684 * R/W protection flags.
9685 *
d76951b6 9686 * @ap: The 2-bit simple AP (AP[2:1])
d8e052b3 9687 * @is_user: TRUE if accessing from PL0
d76951b6 9688 */
d8e052b3 9689static inline int simple_ap_to_rw_prot_is_user(int ap, bool is_user)
d76951b6 9690{
d76951b6
AJ
9691 switch (ap) {
9692 case 0:
9693 return is_user ? 0 : PAGE_READ | PAGE_WRITE;
9694 case 1:
9695 return PAGE_READ | PAGE_WRITE;
9696 case 2:
9697 return is_user ? 0 : PAGE_READ;
9698 case 3:
9699 return PAGE_READ;
9700 default:
9701 g_assert_not_reached();
9702 }
9703}
9704
d8e052b3
AJ
9705static inline int
9706simple_ap_to_rw_prot(CPUARMState *env, ARMMMUIdx mmu_idx, int ap)
9707{
9708 return simple_ap_to_rw_prot_is_user(ap, regime_is_user(env, mmu_idx));
9709}
9710
6ab1a5ee
EI
9711/* Translate S2 section/page access permissions to protection flags
9712 *
9713 * @env: CPUARMState
9714 * @s2ap: The 2-bit stage2 access permissions (S2AP)
9715 * @xn: XN (execute-never) bit
9716 */
9717static int get_S2prot(CPUARMState *env, int s2ap, int xn)
9718{
9719 int prot = 0;
9720
9721 if (s2ap & 1) {
9722 prot |= PAGE_READ;
9723 }
9724 if (s2ap & 2) {
9725 prot |= PAGE_WRITE;
9726 }
9727 if (!xn) {
dfda6837
SS
9728 if (arm_el_is_aa64(env, 2) || prot & PAGE_READ) {
9729 prot |= PAGE_EXEC;
9730 }
6ab1a5ee
EI
9731 }
9732 return prot;
9733}
9734
d8e052b3
AJ
9735/* Translate section/page access permissions to protection flags
9736 *
9737 * @env: CPUARMState
9738 * @mmu_idx: MMU index indicating required translation regime
9739 * @is_aa64: TRUE if AArch64
9740 * @ap: The 2-bit simple AP (AP[2:1])
9741 * @ns: NS (non-secure) bit
9742 * @xn: XN (execute-never) bit
9743 * @pxn: PXN (privileged execute-never) bit
9744 */
9745static int get_S1prot(CPUARMState *env, ARMMMUIdx mmu_idx, bool is_aa64,
9746 int ap, int ns, int xn, int pxn)
9747{
9748 bool is_user = regime_is_user(env, mmu_idx);
9749 int prot_rw, user_rw;
9750 bool have_wxn;
9751 int wxn = 0;
9752
97fa9350 9753 assert(mmu_idx != ARMMMUIdx_Stage2);
d8e052b3
AJ
9754
9755 user_rw = simple_ap_to_rw_prot_is_user(ap, true);
9756 if (is_user) {
9757 prot_rw = user_rw;
9758 } else {
81636b70
RH
9759 if (user_rw && regime_is_pan(env, mmu_idx)) {
9760 return 0;
9761 }
d8e052b3
AJ
9762 prot_rw = simple_ap_to_rw_prot_is_user(ap, false);
9763 }
9764
9765 if (ns && arm_is_secure(env) && (env->cp15.scr_el3 & SCR_SIF)) {
9766 return prot_rw;
9767 }
9768
9769 /* TODO have_wxn should be replaced with
9770 * ARM_FEATURE_V8 || (ARM_FEATURE_V7 && ARM_FEATURE_EL2)
9771 * when ARM_FEATURE_EL2 starts getting set. For now we assume all LPAE
9772 * compatible processors have EL2, which is required for [U]WXN.
9773 */
9774 have_wxn = arm_feature(env, ARM_FEATURE_LPAE);
9775
9776 if (have_wxn) {
9777 wxn = regime_sctlr(env, mmu_idx) & SCTLR_WXN;
9778 }
9779
9780 if (is_aa64) {
339370b9
RH
9781 if (regime_has_2_ranges(mmu_idx) && !is_user) {
9782 xn = pxn || (user_rw & PAGE_WRITE);
d8e052b3
AJ
9783 }
9784 } else if (arm_feature(env, ARM_FEATURE_V7)) {
9785 switch (regime_el(env, mmu_idx)) {
9786 case 1:
9787 case 3:
9788 if (is_user) {
9789 xn = xn || !(user_rw & PAGE_READ);
9790 } else {
9791 int uwxn = 0;
9792 if (have_wxn) {
9793 uwxn = regime_sctlr(env, mmu_idx) & SCTLR_UWXN;
9794 }
9795 xn = xn || !(prot_rw & PAGE_READ) || pxn ||
9796 (uwxn && (user_rw & PAGE_WRITE));
9797 }
9798 break;
9799 case 2:
9800 break;
9801 }
9802 } else {
9803 xn = wxn = 0;
9804 }
9805
9806 if (xn || (wxn && (prot_rw & PAGE_WRITE))) {
9807 return prot_rw;
9808 }
9809 return prot_rw | PAGE_EXEC;
9810}
9811
0480f69a
PM
9812static bool get_level1_table_address(CPUARMState *env, ARMMMUIdx mmu_idx,
9813 uint32_t *table, uint32_t address)
b2fa1797 9814{
0480f69a 9815 /* Note that we can only get here for an AArch32 PL0/PL1 lookup */
0480f69a 9816 TCR *tcr = regime_tcr(env, mmu_idx);
11f136ee 9817
11f136ee
FA
9818 if (address & tcr->mask) {
9819 if (tcr->raw_tcr & TTBCR_PD1) {
e389be16
FA
9820 /* Translation table walk disabled for TTBR1 */
9821 return false;
9822 }
aef878be 9823 *table = regime_ttbr(env, mmu_idx, 1) & 0xffffc000;
e389be16 9824 } else {
11f136ee 9825 if (tcr->raw_tcr & TTBCR_PD0) {
e389be16
FA
9826 /* Translation table walk disabled for TTBR0 */
9827 return false;
9828 }
aef878be 9829 *table = regime_ttbr(env, mmu_idx, 0) & tcr->base_mask;
e389be16
FA
9830 }
9831 *table |= (address >> 18) & 0x3ffc;
9832 return true;
b2fa1797
PB
9833}
9834
37785977
EI
9835/* Translate a S1 pagetable walk through S2 if needed. */
9836static hwaddr S1_ptw_translate(CPUARMState *env, ARMMMUIdx mmu_idx,
9837 hwaddr addr, MemTxAttrs txattrs,
37785977
EI
9838 ARMMMUFaultInfo *fi)
9839{
fee7aa46 9840 if (arm_mmu_idx_is_stage1_of_2(mmu_idx) &&
97fa9350 9841 !regime_translation_disabled(env, ARMMMUIdx_Stage2)) {
37785977
EI
9842 target_ulong s2size;
9843 hwaddr s2pa;
9844 int s2prot;
9845 int ret;
eadb2feb
PM
9846 ARMCacheAttrs cacheattrs = {};
9847 ARMCacheAttrs *pcacheattrs = NULL;
9848
9849 if (env->cp15.hcr_el2 & HCR_PTW) {
9850 /*
9851 * PTW means we must fault if this S1 walk touches S2 Device
9852 * memory; otherwise we don't care about the attributes and can
9853 * save the S2 translation the effort of computing them.
9854 */
9855 pcacheattrs = &cacheattrs;
9856 }
37785977 9857
97fa9350 9858 ret = get_phys_addr_lpae(env, addr, 0, ARMMMUIdx_Stage2, &s2pa,
eadb2feb 9859 &txattrs, &s2prot, &s2size, fi, pcacheattrs);
37785977 9860 if (ret) {
3b39d734 9861 assert(fi->type != ARMFault_None);
37785977
EI
9862 fi->s2addr = addr;
9863 fi->stage2 = true;
9864 fi->s1ptw = true;
9865 return ~0;
9866 }
eadb2feb
PM
9867 if (pcacheattrs && (pcacheattrs->attrs & 0xf0) == 0) {
9868 /* Access was to Device memory: generate Permission fault */
9869 fi->type = ARMFault_Permission;
9870 fi->s2addr = addr;
9871 fi->stage2 = true;
9872 fi->s1ptw = true;
9873 return ~0;
9874 }
37785977
EI
9875 addr = s2pa;
9876 }
9877 return addr;
9878}
9879
14577270 9880/* All loads done in the course of a page table walk go through here. */
a614e698 9881static uint32_t arm_ldl_ptw(CPUState *cs, hwaddr addr, bool is_secure,
3795a6de 9882 ARMMMUIdx mmu_idx, ARMMMUFaultInfo *fi)
ebca90e4 9883{
a614e698
EI
9884 ARMCPU *cpu = ARM_CPU(cs);
9885 CPUARMState *env = &cpu->env;
ebca90e4 9886 MemTxAttrs attrs = {};
3b39d734 9887 MemTxResult result = MEMTX_OK;
5ce4ff65 9888 AddressSpace *as;
3b39d734 9889 uint32_t data;
ebca90e4
PM
9890
9891 attrs.secure = is_secure;
5ce4ff65 9892 as = arm_addressspace(cs, attrs);
3795a6de 9893 addr = S1_ptw_translate(env, mmu_idx, addr, attrs, fi);
a614e698
EI
9894 if (fi->s1ptw) {
9895 return 0;
9896 }
73462ddd 9897 if (regime_translation_big_endian(env, mmu_idx)) {
3b39d734 9898 data = address_space_ldl_be(as, addr, attrs, &result);
73462ddd 9899 } else {
3b39d734 9900 data = address_space_ldl_le(as, addr, attrs, &result);
73462ddd 9901 }
3b39d734
PM
9902 if (result == MEMTX_OK) {
9903 return data;
9904 }
9905 fi->type = ARMFault_SyncExternalOnWalk;
9906 fi->ea = arm_extabort_type(result);
9907 return 0;
ebca90e4
PM
9908}
9909
37785977 9910static uint64_t arm_ldq_ptw(CPUState *cs, hwaddr addr, bool is_secure,
3795a6de 9911 ARMMMUIdx mmu_idx, ARMMMUFaultInfo *fi)
ebca90e4 9912{
37785977
EI
9913 ARMCPU *cpu = ARM_CPU(cs);
9914 CPUARMState *env = &cpu->env;
ebca90e4 9915 MemTxAttrs attrs = {};
3b39d734 9916 MemTxResult result = MEMTX_OK;
5ce4ff65 9917 AddressSpace *as;
9aea1ea3 9918 uint64_t data;
ebca90e4
PM
9919
9920 attrs.secure = is_secure;
5ce4ff65 9921 as = arm_addressspace(cs, attrs);
3795a6de 9922 addr = S1_ptw_translate(env, mmu_idx, addr, attrs, fi);
37785977
EI
9923 if (fi->s1ptw) {
9924 return 0;
9925 }
73462ddd 9926 if (regime_translation_big_endian(env, mmu_idx)) {
3b39d734 9927 data = address_space_ldq_be(as, addr, attrs, &result);
73462ddd 9928 } else {
3b39d734
PM
9929 data = address_space_ldq_le(as, addr, attrs, &result);
9930 }
9931 if (result == MEMTX_OK) {
9932 return data;
73462ddd 9933 }
3b39d734
PM
9934 fi->type = ARMFault_SyncExternalOnWalk;
9935 fi->ea = arm_extabort_type(result);
9936 return 0;
ebca90e4
PM
9937}
9938
b7cc4e82 9939static bool get_phys_addr_v5(CPUARMState *env, uint32_t address,
03ae85f8 9940 MMUAccessType access_type, ARMMMUIdx mmu_idx,
b7cc4e82 9941 hwaddr *phys_ptr, int *prot,
f989983e 9942 target_ulong *page_size,
e14b5a23 9943 ARMMMUFaultInfo *fi)
b5ff1b31 9944{
2fc0cc0e 9945 CPUState *cs = env_cpu(env);
f989983e 9946 int level = 1;
b5ff1b31
FB
9947 uint32_t table;
9948 uint32_t desc;
9949 int type;
9950 int ap;
e389be16 9951 int domain = 0;
dd4ebc2e 9952 int domain_prot;
a8170e5e 9953 hwaddr phys_addr;
0480f69a 9954 uint32_t dacr;
b5ff1b31 9955
9ee6e8bb
PB
9956 /* Pagetable walk. */
9957 /* Lookup l1 descriptor. */
0480f69a 9958 if (!get_level1_table_address(env, mmu_idx, &table, address)) {
e389be16 9959 /* Section translation fault if page walk is disabled by PD0 or PD1 */
f989983e 9960 fi->type = ARMFault_Translation;
e389be16
FA
9961 goto do_fault;
9962 }
a614e698 9963 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
3795a6de 9964 mmu_idx, fi);
3b39d734
PM
9965 if (fi->type != ARMFault_None) {
9966 goto do_fault;
9967 }
9ee6e8bb 9968 type = (desc & 3);
dd4ebc2e 9969 domain = (desc >> 5) & 0x0f;
0480f69a
PM
9970 if (regime_el(env, mmu_idx) == 1) {
9971 dacr = env->cp15.dacr_ns;
9972 } else {
9973 dacr = env->cp15.dacr_s;
9974 }
9975 domain_prot = (dacr >> (domain * 2)) & 3;
9ee6e8bb 9976 if (type == 0) {
601d70b9 9977 /* Section translation fault. */
f989983e 9978 fi->type = ARMFault_Translation;
9ee6e8bb
PB
9979 goto do_fault;
9980 }
f989983e
PM
9981 if (type != 2) {
9982 level = 2;
9983 }
dd4ebc2e 9984 if (domain_prot == 0 || domain_prot == 2) {
f989983e 9985 fi->type = ARMFault_Domain;
9ee6e8bb
PB
9986 goto do_fault;
9987 }
9988 if (type == 2) {
9989 /* 1Mb section. */
9990 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
9991 ap = (desc >> 10) & 3;
d4c430a8 9992 *page_size = 1024 * 1024;
9ee6e8bb
PB
9993 } else {
9994 /* Lookup l2 entry. */
554b0b09
PM
9995 if (type == 1) {
9996 /* Coarse pagetable. */
9997 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
9998 } else {
9999 /* Fine pagetable. */
10000 table = (desc & 0xfffff000) | ((address >> 8) & 0xffc);
10001 }
a614e698 10002 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
3795a6de 10003 mmu_idx, fi);
3b39d734
PM
10004 if (fi->type != ARMFault_None) {
10005 goto do_fault;
10006 }
9ee6e8bb
PB
10007 switch (desc & 3) {
10008 case 0: /* Page translation fault. */
f989983e 10009 fi->type = ARMFault_Translation;
9ee6e8bb
PB
10010 goto do_fault;
10011 case 1: /* 64k page. */
10012 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
10013 ap = (desc >> (4 + ((address >> 13) & 6))) & 3;
d4c430a8 10014 *page_size = 0x10000;
ce819861 10015 break;
9ee6e8bb
PB
10016 case 2: /* 4k page. */
10017 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
c10f7fc3 10018 ap = (desc >> (4 + ((address >> 9) & 6))) & 3;
d4c430a8 10019 *page_size = 0x1000;
ce819861 10020 break;
fc1891c7 10021 case 3: /* 1k page, or ARMv6/XScale "extended small (4k) page" */
554b0b09 10022 if (type == 1) {
fc1891c7
PM
10023 /* ARMv6/XScale extended small page format */
10024 if (arm_feature(env, ARM_FEATURE_XSCALE)
10025 || arm_feature(env, ARM_FEATURE_V6)) {
554b0b09 10026 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
fc1891c7 10027 *page_size = 0x1000;
554b0b09 10028 } else {
fc1891c7
PM
10029 /* UNPREDICTABLE in ARMv5; we choose to take a
10030 * page translation fault.
10031 */
f989983e 10032 fi->type = ARMFault_Translation;
554b0b09
PM
10033 goto do_fault;
10034 }
10035 } else {
10036 phys_addr = (desc & 0xfffffc00) | (address & 0x3ff);
fc1891c7 10037 *page_size = 0x400;
554b0b09 10038 }
9ee6e8bb 10039 ap = (desc >> 4) & 3;
ce819861
PB
10040 break;
10041 default:
9ee6e8bb
PB
10042 /* Never happens, but compiler isn't smart enough to tell. */
10043 abort();
ce819861 10044 }
9ee6e8bb 10045 }
0fbf5238
AJ
10046 *prot = ap_to_rw_prot(env, mmu_idx, ap, domain_prot);
10047 *prot |= *prot ? PAGE_EXEC : 0;
10048 if (!(*prot & (1 << access_type))) {
9ee6e8bb 10049 /* Access permission fault. */
f989983e 10050 fi->type = ARMFault_Permission;
9ee6e8bb
PB
10051 goto do_fault;
10052 }
10053 *phys_ptr = phys_addr;
b7cc4e82 10054 return false;
9ee6e8bb 10055do_fault:
f989983e
PM
10056 fi->domain = domain;
10057 fi->level = level;
b7cc4e82 10058 return true;
9ee6e8bb
PB
10059}
10060
b7cc4e82 10061static bool get_phys_addr_v6(CPUARMState *env, uint32_t address,
03ae85f8 10062 MMUAccessType access_type, ARMMMUIdx mmu_idx,
b7cc4e82 10063 hwaddr *phys_ptr, MemTxAttrs *attrs, int *prot,
f06cf243 10064 target_ulong *page_size, ARMMMUFaultInfo *fi)
9ee6e8bb 10065{
2fc0cc0e 10066 CPUState *cs = env_cpu(env);
f06cf243 10067 int level = 1;
9ee6e8bb
PB
10068 uint32_t table;
10069 uint32_t desc;
10070 uint32_t xn;
de9b05b8 10071 uint32_t pxn = 0;
9ee6e8bb
PB
10072 int type;
10073 int ap;
de9b05b8 10074 int domain = 0;
dd4ebc2e 10075 int domain_prot;
a8170e5e 10076 hwaddr phys_addr;
0480f69a 10077 uint32_t dacr;
8bf5b6a9 10078 bool ns;
9ee6e8bb
PB
10079
10080 /* Pagetable walk. */
10081 /* Lookup l1 descriptor. */
0480f69a 10082 if (!get_level1_table_address(env, mmu_idx, &table, address)) {
e389be16 10083 /* Section translation fault if page walk is disabled by PD0 or PD1 */
f06cf243 10084 fi->type = ARMFault_Translation;
e389be16
FA
10085 goto do_fault;
10086 }
a614e698 10087 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
3795a6de 10088 mmu_idx, fi);
3b39d734
PM
10089 if (fi->type != ARMFault_None) {
10090 goto do_fault;
10091 }
9ee6e8bb 10092 type = (desc & 3);
de9b05b8
PM
10093 if (type == 0 || (type == 3 && !arm_feature(env, ARM_FEATURE_PXN))) {
10094 /* Section translation fault, or attempt to use the encoding
10095 * which is Reserved on implementations without PXN.
10096 */
f06cf243 10097 fi->type = ARMFault_Translation;
9ee6e8bb 10098 goto do_fault;
de9b05b8
PM
10099 }
10100 if ((type == 1) || !(desc & (1 << 18))) {
10101 /* Page or Section. */
dd4ebc2e 10102 domain = (desc >> 5) & 0x0f;
9ee6e8bb 10103 }
0480f69a
PM
10104 if (regime_el(env, mmu_idx) == 1) {
10105 dacr = env->cp15.dacr_ns;
10106 } else {
10107 dacr = env->cp15.dacr_s;
10108 }
f06cf243
PM
10109 if (type == 1) {
10110 level = 2;
10111 }
0480f69a 10112 domain_prot = (dacr >> (domain * 2)) & 3;
dd4ebc2e 10113 if (domain_prot == 0 || domain_prot == 2) {
f06cf243
PM
10114 /* Section or Page domain fault */
10115 fi->type = ARMFault_Domain;
9ee6e8bb
PB
10116 goto do_fault;
10117 }
de9b05b8 10118 if (type != 1) {
9ee6e8bb
PB
10119 if (desc & (1 << 18)) {
10120 /* Supersection. */
10121 phys_addr = (desc & 0xff000000) | (address & 0x00ffffff);
4e42a6ca
SF
10122 phys_addr |= (uint64_t)extract32(desc, 20, 4) << 32;
10123 phys_addr |= (uint64_t)extract32(desc, 5, 4) << 36;
d4c430a8 10124 *page_size = 0x1000000;
b5ff1b31 10125 } else {
9ee6e8bb
PB
10126 /* Section. */
10127 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
d4c430a8 10128 *page_size = 0x100000;
b5ff1b31 10129 }
9ee6e8bb
PB
10130 ap = ((desc >> 10) & 3) | ((desc >> 13) & 4);
10131 xn = desc & (1 << 4);
de9b05b8 10132 pxn = desc & 1;
8bf5b6a9 10133 ns = extract32(desc, 19, 1);
9ee6e8bb 10134 } else {
de9b05b8
PM
10135 if (arm_feature(env, ARM_FEATURE_PXN)) {
10136 pxn = (desc >> 2) & 1;
10137 }
8bf5b6a9 10138 ns = extract32(desc, 3, 1);
9ee6e8bb
PB
10139 /* Lookup l2 entry. */
10140 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
a614e698 10141 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
3795a6de 10142 mmu_idx, fi);
3b39d734
PM
10143 if (fi->type != ARMFault_None) {
10144 goto do_fault;
10145 }
9ee6e8bb
PB
10146 ap = ((desc >> 4) & 3) | ((desc >> 7) & 4);
10147 switch (desc & 3) {
10148 case 0: /* Page translation fault. */
f06cf243 10149 fi->type = ARMFault_Translation;
b5ff1b31 10150 goto do_fault;
9ee6e8bb
PB
10151 case 1: /* 64k page. */
10152 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
10153 xn = desc & (1 << 15);
d4c430a8 10154 *page_size = 0x10000;
9ee6e8bb
PB
10155 break;
10156 case 2: case 3: /* 4k page. */
10157 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
10158 xn = desc & 1;
d4c430a8 10159 *page_size = 0x1000;
9ee6e8bb
PB
10160 break;
10161 default:
10162 /* Never happens, but compiler isn't smart enough to tell. */
10163 abort();
b5ff1b31 10164 }
9ee6e8bb 10165 }
dd4ebc2e 10166 if (domain_prot == 3) {
c0034328
JR
10167 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
10168 } else {
0480f69a 10169 if (pxn && !regime_is_user(env, mmu_idx)) {
de9b05b8
PM
10170 xn = 1;
10171 }
f06cf243
PM
10172 if (xn && access_type == MMU_INST_FETCH) {
10173 fi->type = ARMFault_Permission;
c0034328 10174 goto do_fault;
f06cf243 10175 }
9ee6e8bb 10176
d76951b6
AJ
10177 if (arm_feature(env, ARM_FEATURE_V6K) &&
10178 (regime_sctlr(env, mmu_idx) & SCTLR_AFE)) {
10179 /* The simplified model uses AP[0] as an access control bit. */
10180 if ((ap & 1) == 0) {
10181 /* Access flag fault. */
f06cf243 10182 fi->type = ARMFault_AccessFlag;
d76951b6
AJ
10183 goto do_fault;
10184 }
10185 *prot = simple_ap_to_rw_prot(env, mmu_idx, ap >> 1);
10186 } else {
10187 *prot = ap_to_rw_prot(env, mmu_idx, ap, domain_prot);
c0034328 10188 }
0fbf5238
AJ
10189 if (*prot && !xn) {
10190 *prot |= PAGE_EXEC;
10191 }
10192 if (!(*prot & (1 << access_type))) {
c0034328 10193 /* Access permission fault. */
f06cf243 10194 fi->type = ARMFault_Permission;
c0034328
JR
10195 goto do_fault;
10196 }
3ad493fc 10197 }
8bf5b6a9
PM
10198 if (ns) {
10199 /* The NS bit will (as required by the architecture) have no effect if
10200 * the CPU doesn't support TZ or this is a non-secure translation
10201 * regime, because the attribute will already be non-secure.
10202 */
10203 attrs->secure = false;
10204 }
9ee6e8bb 10205 *phys_ptr = phys_addr;
b7cc4e82 10206 return false;
b5ff1b31 10207do_fault:
f06cf243
PM
10208 fi->domain = domain;
10209 fi->level = level;
b7cc4e82 10210 return true;
b5ff1b31
FB
10211}
10212
1853d5a9 10213/*
a0e966c9 10214 * check_s2_mmu_setup
1853d5a9
EI
10215 * @cpu: ARMCPU
10216 * @is_aa64: True if the translation regime is in AArch64 state
10217 * @startlevel: Suggested starting level
10218 * @inputsize: Bitsize of IPAs
10219 * @stride: Page-table stride (See the ARM ARM)
10220 *
a0e966c9
EI
10221 * Returns true if the suggested S2 translation parameters are OK and
10222 * false otherwise.
1853d5a9 10223 */
a0e966c9
EI
10224static bool check_s2_mmu_setup(ARMCPU *cpu, bool is_aa64, int level,
10225 int inputsize, int stride)
1853d5a9 10226{
98d68ec2
EI
10227 const int grainsize = stride + 3;
10228 int startsizecheck;
10229
1853d5a9
EI
10230 /* Negative levels are never allowed. */
10231 if (level < 0) {
10232 return false;
10233 }
10234
98d68ec2
EI
10235 startsizecheck = inputsize - ((3 - level) * stride + grainsize);
10236 if (startsizecheck < 1 || startsizecheck > stride + 4) {
10237 return false;
10238 }
10239
1853d5a9 10240 if (is_aa64) {
3526423e 10241 CPUARMState *env = &cpu->env;
1853d5a9
EI
10242 unsigned int pamax = arm_pamax(cpu);
10243
10244 switch (stride) {
10245 case 13: /* 64KB Pages. */
10246 if (level == 0 || (level == 1 && pamax <= 42)) {
10247 return false;
10248 }
10249 break;
10250 case 11: /* 16KB Pages. */
10251 if (level == 0 || (level == 1 && pamax <= 40)) {
10252 return false;
10253 }
10254 break;
10255 case 9: /* 4KB Pages. */
10256 if (level == 0 && pamax <= 42) {
10257 return false;
10258 }
10259 break;
10260 default:
10261 g_assert_not_reached();
10262 }
3526423e
EI
10263
10264 /* Inputsize checks. */
10265 if (inputsize > pamax &&
10266 (arm_el_is_aa64(env, 1) || inputsize > 40)) {
10267 /* This is CONSTRAINED UNPREDICTABLE and we choose to fault. */
10268 return false;
10269 }
1853d5a9 10270 } else {
1853d5a9
EI
10271 /* AArch32 only supports 4KB pages. Assert on that. */
10272 assert(stride == 9);
10273
10274 if (level == 0) {
10275 return false;
10276 }
1853d5a9
EI
10277 }
10278 return true;
10279}
10280
5b2d261d
AB
10281/* Translate from the 4-bit stage 2 representation of
10282 * memory attributes (without cache-allocation hints) to
10283 * the 8-bit representation of the stage 1 MAIR registers
10284 * (which includes allocation hints).
10285 *
10286 * ref: shared/translation/attrs/S2AttrDecode()
10287 * .../S2ConvertAttrsHints()
10288 */
10289static uint8_t convert_stage2_attrs(CPUARMState *env, uint8_t s2attrs)
10290{
10291 uint8_t hiattr = extract32(s2attrs, 2, 2);
10292 uint8_t loattr = extract32(s2attrs, 0, 2);
10293 uint8_t hihint = 0, lohint = 0;
10294
10295 if (hiattr != 0) { /* normal memory */
10296 if ((env->cp15.hcr_el2 & HCR_CD) != 0) { /* cache disabled */
10297 hiattr = loattr = 1; /* non-cacheable */
10298 } else {
10299 if (hiattr != 1) { /* Write-through or write-back */
10300 hihint = 3; /* RW allocate */
10301 }
10302 if (loattr != 1) { /* Write-through or write-back */
10303 lohint = 3; /* RW allocate */
10304 }
10305 }
10306 }
10307
10308 return (hiattr << 6) | (hihint << 4) | (loattr << 2) | lohint;
10309}
c47eaf9f 10310#endif /* !CONFIG_USER_ONLY */
5b2d261d 10311
b830a5ee
RH
10312static int aa64_va_parameter_tbi(uint64_t tcr, ARMMMUIdx mmu_idx)
10313{
10314 if (regime_has_2_ranges(mmu_idx)) {
10315 return extract64(tcr, 37, 2);
10316 } else if (mmu_idx == ARMMMUIdx_Stage2) {
10317 return 0; /* VTCR_EL2 */
10318 } else {
10319 return extract32(tcr, 20, 1);
10320 }
10321}
10322
10323static int aa64_va_parameter_tbid(uint64_t tcr, ARMMMUIdx mmu_idx)
10324{
10325 if (regime_has_2_ranges(mmu_idx)) {
10326 return extract64(tcr, 51, 2);
10327 } else if (mmu_idx == ARMMMUIdx_Stage2) {
10328 return 0; /* VTCR_EL2 */
10329 } else {
10330 return extract32(tcr, 29, 1);
10331 }
10332}
10333
10334ARMVAParameters aa64_va_parameters(CPUARMState *env, uint64_t va,
10335 ARMMMUIdx mmu_idx, bool data)
ba97be9f
RH
10336{
10337 uint64_t tcr = regime_tcr(env, mmu_idx)->raw_tcr;
b830a5ee
RH
10338 bool epd, hpd, using16k, using64k;
10339 int select, tsz, tbi;
ba97be9f 10340
339370b9 10341 if (!regime_has_2_ranges(mmu_idx)) {
71d18164 10342 select = 0;
ba97be9f
RH
10343 tsz = extract32(tcr, 0, 6);
10344 using64k = extract32(tcr, 14, 1);
10345 using16k = extract32(tcr, 15, 1);
97fa9350 10346 if (mmu_idx == ARMMMUIdx_Stage2) {
ba97be9f 10347 /* VTCR_EL2 */
b830a5ee 10348 hpd = false;
ba97be9f 10349 } else {
ba97be9f
RH
10350 hpd = extract32(tcr, 24, 1);
10351 }
10352 epd = false;
ba97be9f 10353 } else {
71d18164
RH
10354 /*
10355 * Bit 55 is always between the two regions, and is canonical for
10356 * determining if address tagging is enabled.
10357 */
10358 select = extract64(va, 55, 1);
10359 if (!select) {
10360 tsz = extract32(tcr, 0, 6);
10361 epd = extract32(tcr, 7, 1);
10362 using64k = extract32(tcr, 14, 1);
10363 using16k = extract32(tcr, 15, 1);
71d18164 10364 hpd = extract64(tcr, 41, 1);
71d18164
RH
10365 } else {
10366 int tg = extract32(tcr, 30, 2);
10367 using16k = tg == 1;
10368 using64k = tg == 3;
10369 tsz = extract32(tcr, 16, 6);
10370 epd = extract32(tcr, 23, 1);
71d18164 10371 hpd = extract64(tcr, 42, 1);
71d18164 10372 }
ba97be9f
RH
10373 }
10374 tsz = MIN(tsz, 39); /* TODO: ARMv8.4-TTST */
10375 tsz = MAX(tsz, 16); /* TODO: ARMv8.2-LVA */
10376
b830a5ee
RH
10377 /* Present TBI as a composite with TBID. */
10378 tbi = aa64_va_parameter_tbi(tcr, mmu_idx);
10379 if (!data) {
10380 tbi &= ~aa64_va_parameter_tbid(tcr, mmu_idx);
10381 }
10382 tbi = (tbi >> select) & 1;
10383
ba97be9f
RH
10384 return (ARMVAParameters) {
10385 .tsz = tsz,
10386 .select = select,
10387 .tbi = tbi,
10388 .epd = epd,
10389 .hpd = hpd,
10390 .using16k = using16k,
10391 .using64k = using64k,
10392 };
10393}
10394
c47eaf9f 10395#ifndef CONFIG_USER_ONLY
ba97be9f
RH
10396static ARMVAParameters aa32_va_parameters(CPUARMState *env, uint32_t va,
10397 ARMMMUIdx mmu_idx)
10398{
10399 uint64_t tcr = regime_tcr(env, mmu_idx)->raw_tcr;
10400 uint32_t el = regime_el(env, mmu_idx);
10401 int select, tsz;
10402 bool epd, hpd;
10403
97fa9350 10404 if (mmu_idx == ARMMMUIdx_Stage2) {
ba97be9f
RH
10405 /* VTCR */
10406 bool sext = extract32(tcr, 4, 1);
10407 bool sign = extract32(tcr, 3, 1);
10408
10409 /*
10410 * If the sign-extend bit is not the same as t0sz[3], the result
10411 * is unpredictable. Flag this as a guest error.
10412 */
10413 if (sign != sext) {
10414 qemu_log_mask(LOG_GUEST_ERROR,
10415 "AArch32: VTCR.S / VTCR.T0SZ[3] mismatch\n");
10416 }
10417 tsz = sextract32(tcr, 0, 4) + 8;
10418 select = 0;
10419 hpd = false;
10420 epd = false;
10421 } else if (el == 2) {
10422 /* HTCR */
10423 tsz = extract32(tcr, 0, 3);
10424 select = 0;
10425 hpd = extract64(tcr, 24, 1);
10426 epd = false;
10427 } else {
10428 int t0sz = extract32(tcr, 0, 3);
10429 int t1sz = extract32(tcr, 16, 3);
10430
10431 if (t1sz == 0) {
10432 select = va > (0xffffffffu >> t0sz);
10433 } else {
10434 /* Note that we will detect errors later. */
10435 select = va >= ~(0xffffffffu >> t1sz);
10436 }
10437 if (!select) {
10438 tsz = t0sz;
10439 epd = extract32(tcr, 7, 1);
10440 hpd = extract64(tcr, 41, 1);
10441 } else {
10442 tsz = t1sz;
10443 epd = extract32(tcr, 23, 1);
10444 hpd = extract64(tcr, 42, 1);
10445 }
10446 /* For aarch32, hpd0 is not enabled without t2e as well. */
10447 hpd &= extract32(tcr, 6, 1);
10448 }
10449
10450 return (ARMVAParameters) {
10451 .tsz = tsz,
10452 .select = select,
10453 .epd = epd,
10454 .hpd = hpd,
10455 };
10456}
10457
b7cc4e82 10458static bool get_phys_addr_lpae(CPUARMState *env, target_ulong address,
03ae85f8 10459 MMUAccessType access_type, ARMMMUIdx mmu_idx,
b7cc4e82 10460 hwaddr *phys_ptr, MemTxAttrs *txattrs, int *prot,
da909b2c 10461 target_ulong *page_size_ptr,
5b2d261d 10462 ARMMMUFaultInfo *fi, ARMCacheAttrs *cacheattrs)
3dde962f 10463{
2fc0cc0e 10464 ARMCPU *cpu = env_archcpu(env);
1853d5a9 10465 CPUState *cs = CPU(cpu);
3dde962f 10466 /* Read an LPAE long-descriptor translation table. */
da909b2c 10467 ARMFaultType fault_type = ARMFault_Translation;
1b4093ea 10468 uint32_t level;
ba97be9f 10469 ARMVAParameters param;
3dde962f 10470 uint64_t ttbr;
dddb5223 10471 hwaddr descaddr, indexmask, indexmask_grainsize;
3dde962f 10472 uint32_t tableattrs;
36d820af 10473 target_ulong page_size;
3dde962f 10474 uint32_t attrs;
ba97be9f
RH
10475 int32_t stride;
10476 int addrsize, inputsize;
0480f69a 10477 TCR *tcr = regime_tcr(env, mmu_idx);
d8e052b3 10478 int ap, ns, xn, pxn;
88e8add8 10479 uint32_t el = regime_el(env, mmu_idx);
6109769a 10480 uint64_t descaddrmask;
6e99f762 10481 bool aarch64 = arm_el_is_aa64(env, el);
1bafc2ba 10482 bool guarded = false;
0480f69a
PM
10483
10484 /* TODO:
88e8add8
GB
10485 * This code does not handle the different format TCR for VTCR_EL2.
10486 * This code also does not support shareability levels.
10487 * Attribute and permission bit handling should also be checked when adding
10488 * support for those page table walks.
0480f69a 10489 */
6e99f762 10490 if (aarch64) {
ba97be9f
RH
10491 param = aa64_va_parameters(env, address, mmu_idx,
10492 access_type != MMU_INST_FETCH);
1b4093ea 10493 level = 0;
ba97be9f
RH
10494 addrsize = 64 - 8 * param.tbi;
10495 inputsize = 64 - param.tsz;
d0a2cbce 10496 } else {
ba97be9f 10497 param = aa32_va_parameters(env, address, mmu_idx);
1b4093ea 10498 level = 1;
97fa9350 10499 addrsize = (mmu_idx == ARMMMUIdx_Stage2 ? 40 : 32);
ba97be9f 10500 inputsize = addrsize - param.tsz;
2c8dd318 10501 }
3dde962f 10502
ba97be9f
RH
10503 /*
10504 * We determined the region when collecting the parameters, but we
10505 * have not yet validated that the address is valid for the region.
10506 * Extract the top bits and verify that they all match select.
36d820af
RH
10507 *
10508 * For aa32, if inputsize == addrsize, then we have selected the
10509 * region by exclusion in aa32_va_parameters and there is no more
10510 * validation to do here.
10511 */
10512 if (inputsize < addrsize) {
10513 target_ulong top_bits = sextract64(address, inputsize,
10514 addrsize - inputsize);
03f27724 10515 if (-top_bits != param.select) {
36d820af
RH
10516 /* The gap between the two regions is a Translation fault */
10517 fault_type = ARMFault_Translation;
10518 goto do_fault;
10519 }
3dde962f
PM
10520 }
10521
ba97be9f
RH
10522 if (param.using64k) {
10523 stride = 13;
10524 } else if (param.using16k) {
10525 stride = 11;
10526 } else {
10527 stride = 9;
10528 }
10529
3dde962f
PM
10530 /* Note that QEMU ignores shareability and cacheability attributes,
10531 * so we don't need to do anything with the SH, ORGN, IRGN fields
10532 * in the TTBCR. Similarly, TTBCR:A1 selects whether we get the
10533 * ASID from TTBR0 or TTBR1, but QEMU's TLB doesn't currently
10534 * implement any ASID-like capability so we can ignore it (instead
10535 * we will always flush the TLB any time the ASID is changed).
10536 */
ba97be9f 10537 ttbr = regime_ttbr(env, mmu_idx, param.select);
3dde962f 10538
0480f69a 10539 /* Here we should have set up all the parameters for the translation:
6e99f762 10540 * inputsize, ttbr, epd, stride, tbi
0480f69a
PM
10541 */
10542
ba97be9f 10543 if (param.epd) {
88e8add8
GB
10544 /* Translation table walk disabled => Translation fault on TLB miss
10545 * Note: This is always 0 on 64-bit EL2 and EL3.
10546 */
3dde962f
PM
10547 goto do_fault;
10548 }
10549
97fa9350 10550 if (mmu_idx != ARMMMUIdx_Stage2) {
1853d5a9
EI
10551 /* The starting level depends on the virtual address size (which can
10552 * be up to 48 bits) and the translation granule size. It indicates
10553 * the number of strides (stride bits at a time) needed to
10554 * consume the bits of the input address. In the pseudocode this is:
10555 * level = 4 - RoundUp((inputsize - grainsize) / stride)
10556 * where their 'inputsize' is our 'inputsize', 'grainsize' is
10557 * our 'stride + 3' and 'stride' is our 'stride'.
10558 * Applying the usual "rounded up m/n is (m+n-1)/n" and simplifying:
10559 * = 4 - (inputsize - stride - 3 + stride - 1) / stride
10560 * = 4 - (inputsize - 4) / stride;
10561 */
10562 level = 4 - (inputsize - 4) / stride;
10563 } else {
10564 /* For stage 2 translations the starting level is specified by the
10565 * VTCR_EL2.SL0 field (whose interpretation depends on the page size)
10566 */
1b4093ea
SS
10567 uint32_t sl0 = extract32(tcr->raw_tcr, 6, 2);
10568 uint32_t startlevel;
1853d5a9
EI
10569 bool ok;
10570
6e99f762 10571 if (!aarch64 || stride == 9) {
1853d5a9 10572 /* AArch32 or 4KB pages */
1b4093ea 10573 startlevel = 2 - sl0;
1853d5a9
EI
10574 } else {
10575 /* 16KB or 64KB pages */
1b4093ea 10576 startlevel = 3 - sl0;
1853d5a9
EI
10577 }
10578
10579 /* Check that the starting level is valid. */
6e99f762 10580 ok = check_s2_mmu_setup(cpu, aarch64, startlevel,
1b4093ea 10581 inputsize, stride);
1853d5a9 10582 if (!ok) {
da909b2c 10583 fault_type = ARMFault_Translation;
1853d5a9
EI
10584 goto do_fault;
10585 }
1b4093ea 10586 level = startlevel;
1853d5a9 10587 }
3dde962f 10588
dddb5223
SS
10589 indexmask_grainsize = (1ULL << (stride + 3)) - 1;
10590 indexmask = (1ULL << (inputsize - (stride * (4 - level)))) - 1;
3dde962f
PM
10591
10592 /* Now we can extract the actual base address from the TTBR */
2c8dd318 10593 descaddr = extract64(ttbr, 0, 48);
dddb5223 10594 descaddr &= ~indexmask;
3dde962f 10595
6109769a 10596 /* The address field in the descriptor goes up to bit 39 for ARMv7
dddb5223
SS
10597 * but up to bit 47 for ARMv8, but we use the descaddrmask
10598 * up to bit 39 for AArch32, because we don't need other bits in that case
10599 * to construct next descriptor address (anyway they should be all zeroes).
6109769a 10600 */
6e99f762 10601 descaddrmask = ((1ull << (aarch64 ? 48 : 40)) - 1) &
dddb5223 10602 ~indexmask_grainsize;
6109769a 10603
ebca90e4
PM
10604 /* Secure accesses start with the page table in secure memory and
10605 * can be downgraded to non-secure at any step. Non-secure accesses
10606 * remain non-secure. We implement this by just ORing in the NSTable/NS
10607 * bits at each step.
10608 */
10609 tableattrs = regime_is_secure(env, mmu_idx) ? 0 : (1 << 4);
3dde962f
PM
10610 for (;;) {
10611 uint64_t descriptor;
ebca90e4 10612 bool nstable;
3dde962f 10613
dddb5223 10614 descaddr |= (address >> (stride * (4 - level))) & indexmask;
2c8dd318 10615 descaddr &= ~7ULL;
ebca90e4 10616 nstable = extract32(tableattrs, 4, 1);
3795a6de 10617 descriptor = arm_ldq_ptw(cs, descaddr, !nstable, mmu_idx, fi);
3b39d734 10618 if (fi->type != ARMFault_None) {
37785977
EI
10619 goto do_fault;
10620 }
10621
3dde962f
PM
10622 if (!(descriptor & 1) ||
10623 (!(descriptor & 2) && (level == 3))) {
10624 /* Invalid, or the Reserved level 3 encoding */
10625 goto do_fault;
10626 }
6109769a 10627 descaddr = descriptor & descaddrmask;
3dde962f
PM
10628
10629 if ((descriptor & 2) && (level < 3)) {
037c13c5 10630 /* Table entry. The top five bits are attributes which may
3dde962f
PM
10631 * propagate down through lower levels of the table (and
10632 * which are all arranged so that 0 means "no effect", so
10633 * we can gather them up by ORing in the bits at each level).
10634 */
10635 tableattrs |= extract64(descriptor, 59, 5);
10636 level++;
dddb5223 10637 indexmask = indexmask_grainsize;
3dde962f
PM
10638 continue;
10639 }
10640 /* Block entry at level 1 or 2, or page entry at level 3.
10641 * These are basically the same thing, although the number
10642 * of bits we pull in from the vaddr varies.
10643 */
973a5434 10644 page_size = (1ULL << ((stride * (4 - level)) + 3));
3dde962f 10645 descaddr |= (address & (page_size - 1));
6ab1a5ee 10646 /* Extract attributes from the descriptor */
d615efac
IC
10647 attrs = extract64(descriptor, 2, 10)
10648 | (extract64(descriptor, 52, 12) << 10);
6ab1a5ee 10649
97fa9350 10650 if (mmu_idx == ARMMMUIdx_Stage2) {
6ab1a5ee
EI
10651 /* Stage 2 table descriptors do not include any attribute fields */
10652 break;
10653 }
10654 /* Merge in attributes from table descriptors */
037c13c5 10655 attrs |= nstable << 3; /* NS */
1bafc2ba 10656 guarded = extract64(descriptor, 50, 1); /* GP */
ba97be9f 10657 if (param.hpd) {
037c13c5
RH
10658 /* HPD disables all the table attributes except NSTable. */
10659 break;
10660 }
10661 attrs |= extract32(tableattrs, 0, 2) << 11; /* XN, PXN */
3dde962f
PM
10662 /* The sense of AP[1] vs APTable[0] is reversed, as APTable[0] == 1
10663 * means "force PL1 access only", which means forcing AP[1] to 0.
10664 */
037c13c5
RH
10665 attrs &= ~(extract32(tableattrs, 2, 1) << 4); /* !APT[0] => AP[1] */
10666 attrs |= extract32(tableattrs, 3, 1) << 5; /* APT[1] => AP[2] */
3dde962f
PM
10667 break;
10668 }
10669 /* Here descaddr is the final physical address, and attributes
10670 * are all in attrs.
10671 */
da909b2c 10672 fault_type = ARMFault_AccessFlag;
3dde962f
PM
10673 if ((attrs & (1 << 8)) == 0) {
10674 /* Access flag */
10675 goto do_fault;
10676 }
d8e052b3
AJ
10677
10678 ap = extract32(attrs, 4, 2);
d8e052b3 10679 xn = extract32(attrs, 12, 1);
d8e052b3 10680
97fa9350 10681 if (mmu_idx == ARMMMUIdx_Stage2) {
6ab1a5ee
EI
10682 ns = true;
10683 *prot = get_S2prot(env, ap, xn);
10684 } else {
10685 ns = extract32(attrs, 3, 1);
10686 pxn = extract32(attrs, 11, 1);
6e99f762 10687 *prot = get_S1prot(env, mmu_idx, aarch64, ap, ns, xn, pxn);
6ab1a5ee 10688 }
d8e052b3 10689
da909b2c 10690 fault_type = ARMFault_Permission;
d8e052b3 10691 if (!(*prot & (1 << access_type))) {
3dde962f
PM
10692 goto do_fault;
10693 }
3dde962f 10694
8bf5b6a9
PM
10695 if (ns) {
10696 /* The NS bit will (as required by the architecture) have no effect if
10697 * the CPU doesn't support TZ or this is a non-secure translation
10698 * regime, because the attribute will already be non-secure.
10699 */
10700 txattrs->secure = false;
10701 }
1bafc2ba
RH
10702 /* When in aarch64 mode, and BTI is enabled, remember GP in the IOTLB. */
10703 if (aarch64 && guarded && cpu_isar_feature(aa64_bti, cpu)) {
10704 txattrs->target_tlb_bit0 = true;
10705 }
5b2d261d
AB
10706
10707 if (cacheattrs != NULL) {
97fa9350 10708 if (mmu_idx == ARMMMUIdx_Stage2) {
5b2d261d
AB
10709 cacheattrs->attrs = convert_stage2_attrs(env,
10710 extract32(attrs, 0, 4));
10711 } else {
10712 /* Index into MAIR registers for cache attributes */
10713 uint8_t attrindx = extract32(attrs, 0, 3);
10714 uint64_t mair = env->cp15.mair_el[regime_el(env, mmu_idx)];
10715 assert(attrindx <= 7);
10716 cacheattrs->attrs = extract64(mair, attrindx * 8, 8);
10717 }
10718 cacheattrs->shareability = extract32(attrs, 6, 2);
10719 }
10720
3dde962f
PM
10721 *phys_ptr = descaddr;
10722 *page_size_ptr = page_size;
b7cc4e82 10723 return false;
3dde962f
PM
10724
10725do_fault:
da909b2c
PM
10726 fi->type = fault_type;
10727 fi->level = level;
37785977 10728 /* Tag the error as S2 for failed S1 PTW at S2 or ordinary S2. */
97fa9350 10729 fi->stage2 = fi->s1ptw || (mmu_idx == ARMMMUIdx_Stage2);
b7cc4e82 10730 return true;
3dde962f
PM
10731}
10732
f6bda88f
PC
10733static inline void get_phys_addr_pmsav7_default(CPUARMState *env,
10734 ARMMMUIdx mmu_idx,
10735 int32_t address, int *prot)
10736{
3a00d560
MD
10737 if (!arm_feature(env, ARM_FEATURE_M)) {
10738 *prot = PAGE_READ | PAGE_WRITE;
10739 switch (address) {
10740 case 0xF0000000 ... 0xFFFFFFFF:
10741 if (regime_sctlr(env, mmu_idx) & SCTLR_V) {
10742 /* hivecs execing is ok */
10743 *prot |= PAGE_EXEC;
10744 }
10745 break;
10746 case 0x00000000 ... 0x7FFFFFFF:
f6bda88f 10747 *prot |= PAGE_EXEC;
3a00d560
MD
10748 break;
10749 }
10750 } else {
10751 /* Default system address map for M profile cores.
10752 * The architecture specifies which regions are execute-never;
10753 * at the MPU level no other checks are defined.
10754 */
10755 switch (address) {
10756 case 0x00000000 ... 0x1fffffff: /* ROM */
10757 case 0x20000000 ... 0x3fffffff: /* SRAM */
10758 case 0x60000000 ... 0x7fffffff: /* RAM */
10759 case 0x80000000 ... 0x9fffffff: /* RAM */
10760 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
10761 break;
10762 case 0x40000000 ... 0x5fffffff: /* Peripheral */
10763 case 0xa0000000 ... 0xbfffffff: /* Device */
10764 case 0xc0000000 ... 0xdfffffff: /* Device */
10765 case 0xe0000000 ... 0xffffffff: /* System */
10766 *prot = PAGE_READ | PAGE_WRITE;
10767 break;
10768 default:
10769 g_assert_not_reached();
f6bda88f 10770 }
f6bda88f 10771 }
f6bda88f
PC
10772}
10773
29c483a5
MD
10774static bool pmsav7_use_background_region(ARMCPU *cpu,
10775 ARMMMUIdx mmu_idx, bool is_user)
10776{
10777 /* Return true if we should use the default memory map as a
10778 * "background" region if there are no hits against any MPU regions.
10779 */
10780 CPUARMState *env = &cpu->env;
10781
10782 if (is_user) {
10783 return false;
10784 }
10785
10786 if (arm_feature(env, ARM_FEATURE_M)) {
ecf5e8ea
PM
10787 return env->v7m.mpu_ctrl[regime_is_secure(env, mmu_idx)]
10788 & R_V7M_MPU_CTRL_PRIVDEFENA_MASK;
29c483a5
MD
10789 } else {
10790 return regime_sctlr(env, mmu_idx) & SCTLR_BR;
10791 }
10792}
10793
38aaa60c
PM
10794static inline bool m_is_ppb_region(CPUARMState *env, uint32_t address)
10795{
10796 /* True if address is in the M profile PPB region 0xe0000000 - 0xe00fffff */
10797 return arm_feature(env, ARM_FEATURE_M) &&
10798 extract32(address, 20, 12) == 0xe00;
10799}
10800
bf446a11
PM
10801static inline bool m_is_system_region(CPUARMState *env, uint32_t address)
10802{
10803 /* True if address is in the M profile system region
10804 * 0xe0000000 - 0xffffffff
10805 */
10806 return arm_feature(env, ARM_FEATURE_M) && extract32(address, 29, 3) == 0x7;
10807}
10808
f6bda88f 10809static bool get_phys_addr_pmsav7(CPUARMState *env, uint32_t address,
03ae85f8 10810 MMUAccessType access_type, ARMMMUIdx mmu_idx,
9375ad15 10811 hwaddr *phys_ptr, int *prot,
e5e40999 10812 target_ulong *page_size,
9375ad15 10813 ARMMMUFaultInfo *fi)
f6bda88f 10814{
2fc0cc0e 10815 ARMCPU *cpu = env_archcpu(env);
f6bda88f
PC
10816 int n;
10817 bool is_user = regime_is_user(env, mmu_idx);
10818
10819 *phys_ptr = address;
e5e40999 10820 *page_size = TARGET_PAGE_SIZE;
f6bda88f
PC
10821 *prot = 0;
10822
38aaa60c
PM
10823 if (regime_translation_disabled(env, mmu_idx) ||
10824 m_is_ppb_region(env, address)) {
10825 /* MPU disabled or M profile PPB access: use default memory map.
10826 * The other case which uses the default memory map in the
10827 * v7M ARM ARM pseudocode is exception vector reads from the vector
10828 * table. In QEMU those accesses are done in arm_v7m_load_vector(),
10829 * which always does a direct read using address_space_ldl(), rather
10830 * than going via this function, so we don't need to check that here.
10831 */
f6bda88f
PC
10832 get_phys_addr_pmsav7_default(env, mmu_idx, address, prot);
10833 } else { /* MPU enabled */
10834 for (n = (int)cpu->pmsav7_dregion - 1; n >= 0; n--) {
10835 /* region search */
10836 uint32_t base = env->pmsav7.drbar[n];
10837 uint32_t rsize = extract32(env->pmsav7.drsr[n], 1, 5);
10838 uint32_t rmask;
10839 bool srdis = false;
10840
10841 if (!(env->pmsav7.drsr[n] & 0x1)) {
10842 continue;
10843 }
10844
10845 if (!rsize) {
c9f9f124
MD
10846 qemu_log_mask(LOG_GUEST_ERROR,
10847 "DRSR[%d]: Rsize field cannot be 0\n", n);
f6bda88f
PC
10848 continue;
10849 }
10850 rsize++;
10851 rmask = (1ull << rsize) - 1;
10852
10853 if (base & rmask) {
c9f9f124
MD
10854 qemu_log_mask(LOG_GUEST_ERROR,
10855 "DRBAR[%d]: 0x%" PRIx32 " misaligned "
10856 "to DRSR region size, mask = 0x%" PRIx32 "\n",
10857 n, base, rmask);
f6bda88f
PC
10858 continue;
10859 }
10860
10861 if (address < base || address > base + rmask) {
9d2b5a58
PM
10862 /*
10863 * Address not in this region. We must check whether the
10864 * region covers addresses in the same page as our address.
10865 * In that case we must not report a size that covers the
10866 * whole page for a subsequent hit against a different MPU
10867 * region or the background region, because it would result in
10868 * incorrect TLB hits for subsequent accesses to addresses that
10869 * are in this MPU region.
10870 */
10871 if (ranges_overlap(base, rmask,
10872 address & TARGET_PAGE_MASK,
10873 TARGET_PAGE_SIZE)) {
10874 *page_size = 1;
10875 }
f6bda88f
PC
10876 continue;
10877 }
10878
10879 /* Region matched */
10880
10881 if (rsize >= 8) { /* no subregions for regions < 256 bytes */
10882 int i, snd;
10883 uint32_t srdis_mask;
10884
10885 rsize -= 3; /* sub region size (power of 2) */
10886 snd = ((address - base) >> rsize) & 0x7;
10887 srdis = extract32(env->pmsav7.drsr[n], snd + 8, 1);
10888
10889 srdis_mask = srdis ? 0x3 : 0x0;
10890 for (i = 2; i <= 8 && rsize < TARGET_PAGE_BITS; i *= 2) {
10891 /* This will check in groups of 2, 4 and then 8, whether
10892 * the subregion bits are consistent. rsize is incremented
10893 * back up to give the region size, considering consistent
10894 * adjacent subregions as one region. Stop testing if rsize
10895 * is already big enough for an entire QEMU page.
10896 */
10897 int snd_rounded = snd & ~(i - 1);
10898 uint32_t srdis_multi = extract32(env->pmsav7.drsr[n],
10899 snd_rounded + 8, i);
10900 if (srdis_mask ^ srdis_multi) {
10901 break;
10902 }
10903 srdis_mask = (srdis_mask << i) | srdis_mask;
10904 rsize++;
10905 }
10906 }
f6bda88f
PC
10907 if (srdis) {
10908 continue;
10909 }
e5e40999
PM
10910 if (rsize < TARGET_PAGE_BITS) {
10911 *page_size = 1 << rsize;
10912 }
f6bda88f
PC
10913 break;
10914 }
10915
10916 if (n == -1) { /* no hits */
29c483a5 10917 if (!pmsav7_use_background_region(cpu, mmu_idx, is_user)) {
f6bda88f 10918 /* background fault */
9375ad15 10919 fi->type = ARMFault_Background;
f6bda88f
PC
10920 return true;
10921 }
10922 get_phys_addr_pmsav7_default(env, mmu_idx, address, prot);
10923 } else { /* a MPU hit! */
10924 uint32_t ap = extract32(env->pmsav7.dracr[n], 8, 3);
bf446a11
PM
10925 uint32_t xn = extract32(env->pmsav7.dracr[n], 12, 1);
10926
10927 if (m_is_system_region(env, address)) {
10928 /* System space is always execute never */
10929 xn = 1;
10930 }
f6bda88f
PC
10931
10932 if (is_user) { /* User mode AP bit decoding */
10933 switch (ap) {
10934 case 0:
10935 case 1:
10936 case 5:
10937 break; /* no access */
10938 case 3:
10939 *prot |= PAGE_WRITE;
10940 /* fall through */
10941 case 2:
10942 case 6:
10943 *prot |= PAGE_READ | PAGE_EXEC;
10944 break;
8638f1ad
PM
10945 case 7:
10946 /* for v7M, same as 6; for R profile a reserved value */
10947 if (arm_feature(env, ARM_FEATURE_M)) {
10948 *prot |= PAGE_READ | PAGE_EXEC;
10949 break;
10950 }
10951 /* fall through */
f6bda88f
PC
10952 default:
10953 qemu_log_mask(LOG_GUEST_ERROR,
c9f9f124
MD
10954 "DRACR[%d]: Bad value for AP bits: 0x%"
10955 PRIx32 "\n", n, ap);
f6bda88f
PC
10956 }
10957 } else { /* Priv. mode AP bits decoding */
10958 switch (ap) {
10959 case 0:
10960 break; /* no access */
10961 case 1:
10962 case 2:
10963 case 3:
10964 *prot |= PAGE_WRITE;
10965 /* fall through */
10966 case 5:
10967 case 6:
10968 *prot |= PAGE_READ | PAGE_EXEC;
10969 break;
8638f1ad
PM
10970 case 7:
10971 /* for v7M, same as 6; for R profile a reserved value */
10972 if (arm_feature(env, ARM_FEATURE_M)) {
10973 *prot |= PAGE_READ | PAGE_EXEC;
10974 break;
10975 }
10976 /* fall through */
f6bda88f
PC
10977 default:
10978 qemu_log_mask(LOG_GUEST_ERROR,
c9f9f124
MD
10979 "DRACR[%d]: Bad value for AP bits: 0x%"
10980 PRIx32 "\n", n, ap);
f6bda88f
PC
10981 }
10982 }
10983
10984 /* execute never */
bf446a11 10985 if (xn) {
f6bda88f
PC
10986 *prot &= ~PAGE_EXEC;
10987 }
10988 }
10989 }
10990
9375ad15
PM
10991 fi->type = ARMFault_Permission;
10992 fi->level = 1;
f6bda88f
PC
10993 return !(*prot & (1 << access_type));
10994}
10995
35337cc3
PM
10996static bool v8m_is_sau_exempt(CPUARMState *env,
10997 uint32_t address, MMUAccessType access_type)
10998{
10999 /* The architecture specifies that certain address ranges are
11000 * exempt from v8M SAU/IDAU checks.
11001 */
11002 return
11003 (access_type == MMU_INST_FETCH && m_is_system_region(env, address)) ||
11004 (address >= 0xe0000000 && address <= 0xe0002fff) ||
11005 (address >= 0xe000e000 && address <= 0xe000efff) ||
11006 (address >= 0xe002e000 && address <= 0xe002efff) ||
11007 (address >= 0xe0040000 && address <= 0xe0041fff) ||
11008 (address >= 0xe00ff000 && address <= 0xe00fffff);
11009}
11010
787a7e76 11011void v8m_security_lookup(CPUARMState *env, uint32_t address,
35337cc3
PM
11012 MMUAccessType access_type, ARMMMUIdx mmu_idx,
11013 V8M_SAttributes *sattrs)
11014{
11015 /* Look up the security attributes for this address. Compare the
11016 * pseudocode SecurityCheck() function.
11017 * We assume the caller has zero-initialized *sattrs.
11018 */
2fc0cc0e 11019 ARMCPU *cpu = env_archcpu(env);
35337cc3 11020 int r;
181962fd
PM
11021 bool idau_exempt = false, idau_ns = true, idau_nsc = true;
11022 int idau_region = IREGION_NOTVALID;
72042435
PM
11023 uint32_t addr_page_base = address & TARGET_PAGE_MASK;
11024 uint32_t addr_page_limit = addr_page_base + (TARGET_PAGE_SIZE - 1);
35337cc3 11025
181962fd
PM
11026 if (cpu->idau) {
11027 IDAUInterfaceClass *iic = IDAU_INTERFACE_GET_CLASS(cpu->idau);
11028 IDAUInterface *ii = IDAU_INTERFACE(cpu->idau);
11029
11030 iic->check(ii, address, &idau_region, &idau_exempt, &idau_ns,
11031 &idau_nsc);
11032 }
35337cc3
PM
11033
11034 if (access_type == MMU_INST_FETCH && extract32(address, 28, 4) == 0xf) {
11035 /* 0xf0000000..0xffffffff is always S for insn fetches */
11036 return;
11037 }
11038
181962fd 11039 if (idau_exempt || v8m_is_sau_exempt(env, address, access_type)) {
35337cc3
PM
11040 sattrs->ns = !regime_is_secure(env, mmu_idx);
11041 return;
11042 }
11043
181962fd
PM
11044 if (idau_region != IREGION_NOTVALID) {
11045 sattrs->irvalid = true;
11046 sattrs->iregion = idau_region;
11047 }
11048
35337cc3
PM
11049 switch (env->sau.ctrl & 3) {
11050 case 0: /* SAU.ENABLE == 0, SAU.ALLNS == 0 */
11051 break;
11052 case 2: /* SAU.ENABLE == 0, SAU.ALLNS == 1 */
11053 sattrs->ns = true;
11054 break;
11055 default: /* SAU.ENABLE == 1 */
11056 for (r = 0; r < cpu->sau_sregion; r++) {
11057 if (env->sau.rlar[r] & 1) {
11058 uint32_t base = env->sau.rbar[r] & ~0x1f;
11059 uint32_t limit = env->sau.rlar[r] | 0x1f;
11060
11061 if (base <= address && limit >= address) {
72042435
PM
11062 if (base > addr_page_base || limit < addr_page_limit) {
11063 sattrs->subpage = true;
11064 }
35337cc3
PM
11065 if (sattrs->srvalid) {
11066 /* If we hit in more than one region then we must report
11067 * as Secure, not NS-Callable, with no valid region
11068 * number info.
11069 */
11070 sattrs->ns = false;
11071 sattrs->nsc = false;
11072 sattrs->sregion = 0;
11073 sattrs->srvalid = false;
11074 break;
11075 } else {
11076 if (env->sau.rlar[r] & 2) {
11077 sattrs->nsc = true;
11078 } else {
11079 sattrs->ns = true;
11080 }
11081 sattrs->srvalid = true;
11082 sattrs->sregion = r;
11083 }
9d2b5a58
PM
11084 } else {
11085 /*
11086 * Address not in this region. We must check whether the
11087 * region covers addresses in the same page as our address.
11088 * In that case we must not report a size that covers the
11089 * whole page for a subsequent hit against a different MPU
11090 * region or the background region, because it would result
11091 * in incorrect TLB hits for subsequent accesses to
11092 * addresses that are in this MPU region.
11093 */
11094 if (limit >= base &&
11095 ranges_overlap(base, limit - base + 1,
11096 addr_page_base,
11097 TARGET_PAGE_SIZE)) {
11098 sattrs->subpage = true;
11099 }
35337cc3
PM
11100 }
11101 }
11102 }
7e3f1223
TR
11103 break;
11104 }
35337cc3 11105
7e3f1223
TR
11106 /*
11107 * The IDAU will override the SAU lookup results if it specifies
11108 * higher security than the SAU does.
11109 */
11110 if (!idau_ns) {
11111 if (sattrs->ns || (!idau_nsc && sattrs->nsc)) {
11112 sattrs->ns = false;
11113 sattrs->nsc = idau_nsc;
181962fd 11114 }
35337cc3
PM
11115 }
11116}
11117
787a7e76 11118bool pmsav8_mpu_lookup(CPUARMState *env, uint32_t address,
54317c0f
PM
11119 MMUAccessType access_type, ARMMMUIdx mmu_idx,
11120 hwaddr *phys_ptr, MemTxAttrs *txattrs,
72042435
PM
11121 int *prot, bool *is_subpage,
11122 ARMMMUFaultInfo *fi, uint32_t *mregion)
54317c0f
PM
11123{
11124 /* Perform a PMSAv8 MPU lookup (without also doing the SAU check
11125 * that a full phys-to-virt translation does).
11126 * mregion is (if not NULL) set to the region number which matched,
11127 * or -1 if no region number is returned (MPU off, address did not
11128 * hit a region, address hit in multiple regions).
72042435
PM
11129 * We set is_subpage to true if the region hit doesn't cover the
11130 * entire TARGET_PAGE the address is within.
54317c0f 11131 */
2fc0cc0e 11132 ARMCPU *cpu = env_archcpu(env);
504e3cc3 11133 bool is_user = regime_is_user(env, mmu_idx);
62c58ee0 11134 uint32_t secure = regime_is_secure(env, mmu_idx);
504e3cc3
PM
11135 int n;
11136 int matchregion = -1;
11137 bool hit = false;
72042435
PM
11138 uint32_t addr_page_base = address & TARGET_PAGE_MASK;
11139 uint32_t addr_page_limit = addr_page_base + (TARGET_PAGE_SIZE - 1);
504e3cc3 11140
72042435 11141 *is_subpage = false;
504e3cc3
PM
11142 *phys_ptr = address;
11143 *prot = 0;
54317c0f
PM
11144 if (mregion) {
11145 *mregion = -1;
35337cc3
PM
11146 }
11147
504e3cc3
PM
11148 /* Unlike the ARM ARM pseudocode, we don't need to check whether this
11149 * was an exception vector read from the vector table (which is always
11150 * done using the default system address map), because those accesses
11151 * are done in arm_v7m_load_vector(), which always does a direct
11152 * read using address_space_ldl(), rather than going via this function.
11153 */
11154 if (regime_translation_disabled(env, mmu_idx)) { /* MPU disabled */
11155 hit = true;
11156 } else if (m_is_ppb_region(env, address)) {
11157 hit = true;
504e3cc3 11158 } else {
cff21316
PM
11159 if (pmsav7_use_background_region(cpu, mmu_idx, is_user)) {
11160 hit = true;
11161 }
11162
504e3cc3
PM
11163 for (n = (int)cpu->pmsav7_dregion - 1; n >= 0; n--) {
11164 /* region search */
11165 /* Note that the base address is bits [31:5] from the register
11166 * with bits [4:0] all zeroes, but the limit address is bits
11167 * [31:5] from the register with bits [4:0] all ones.
11168 */
62c58ee0
PM
11169 uint32_t base = env->pmsav8.rbar[secure][n] & ~0x1f;
11170 uint32_t limit = env->pmsav8.rlar[secure][n] | 0x1f;
504e3cc3 11171
62c58ee0 11172 if (!(env->pmsav8.rlar[secure][n] & 0x1)) {
504e3cc3
PM
11173 /* Region disabled */
11174 continue;
11175 }
11176
11177 if (address < base || address > limit) {
9d2b5a58
PM
11178 /*
11179 * Address not in this region. We must check whether the
11180 * region covers addresses in the same page as our address.
11181 * In that case we must not report a size that covers the
11182 * whole page for a subsequent hit against a different MPU
11183 * region or the background region, because it would result in
11184 * incorrect TLB hits for subsequent accesses to addresses that
11185 * are in this MPU region.
11186 */
11187 if (limit >= base &&
11188 ranges_overlap(base, limit - base + 1,
11189 addr_page_base,
11190 TARGET_PAGE_SIZE)) {
11191 *is_subpage = true;
11192 }
504e3cc3
PM
11193 continue;
11194 }
11195
72042435
PM
11196 if (base > addr_page_base || limit < addr_page_limit) {
11197 *is_subpage = true;
11198 }
11199
cff21316 11200 if (matchregion != -1) {
504e3cc3
PM
11201 /* Multiple regions match -- always a failure (unlike
11202 * PMSAv7 where highest-numbered-region wins)
11203 */
3f551b5b
PM
11204 fi->type = ARMFault_Permission;
11205 fi->level = 1;
504e3cc3
PM
11206 return true;
11207 }
11208
11209 matchregion = n;
11210 hit = true;
504e3cc3
PM
11211 }
11212 }
11213
11214 if (!hit) {
11215 /* background fault */
3f551b5b 11216 fi->type = ARMFault_Background;
504e3cc3
PM
11217 return true;
11218 }
11219
11220 if (matchregion == -1) {
11221 /* hit using the background region */
11222 get_phys_addr_pmsav7_default(env, mmu_idx, address, prot);
11223 } else {
62c58ee0
PM
11224 uint32_t ap = extract32(env->pmsav8.rbar[secure][matchregion], 1, 2);
11225 uint32_t xn = extract32(env->pmsav8.rbar[secure][matchregion], 0, 1);
504e3cc3
PM
11226
11227 if (m_is_system_region(env, address)) {
11228 /* System space is always execute never */
11229 xn = 1;
11230 }
11231
11232 *prot = simple_ap_to_rw_prot(env, mmu_idx, ap);
11233 if (*prot && !xn) {
11234 *prot |= PAGE_EXEC;
11235 }
11236 /* We don't need to look the attribute up in the MAIR0/MAIR1
11237 * registers because that only tells us about cacheability.
11238 */
54317c0f
PM
11239 if (mregion) {
11240 *mregion = matchregion;
11241 }
504e3cc3
PM
11242 }
11243
3f551b5b
PM
11244 fi->type = ARMFault_Permission;
11245 fi->level = 1;
504e3cc3
PM
11246 return !(*prot & (1 << access_type));
11247}
11248
54317c0f
PM
11249
11250static bool get_phys_addr_pmsav8(CPUARMState *env, uint32_t address,
11251 MMUAccessType access_type, ARMMMUIdx mmu_idx,
11252 hwaddr *phys_ptr, MemTxAttrs *txattrs,
72042435
PM
11253 int *prot, target_ulong *page_size,
11254 ARMMMUFaultInfo *fi)
54317c0f
PM
11255{
11256 uint32_t secure = regime_is_secure(env, mmu_idx);
11257 V8M_SAttributes sattrs = {};
72042435
PM
11258 bool ret;
11259 bool mpu_is_subpage;
54317c0f
PM
11260
11261 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
11262 v8m_security_lookup(env, address, access_type, mmu_idx, &sattrs);
11263 if (access_type == MMU_INST_FETCH) {
11264 /* Instruction fetches always use the MMU bank and the
11265 * transaction attribute determined by the fetch address,
11266 * regardless of CPU state. This is painful for QEMU
11267 * to handle, because it would mean we need to encode
11268 * into the mmu_idx not just the (user, negpri) information
11269 * for the current security state but also that for the
11270 * other security state, which would balloon the number
11271 * of mmu_idx values needed alarmingly.
11272 * Fortunately we can avoid this because it's not actually
11273 * possible to arbitrarily execute code from memory with
11274 * the wrong security attribute: it will always generate
11275 * an exception of some kind or another, apart from the
11276 * special case of an NS CPU executing an SG instruction
11277 * in S&NSC memory. So we always just fail the translation
11278 * here and sort things out in the exception handler
11279 * (including possibly emulating an SG instruction).
11280 */
11281 if (sattrs.ns != !secure) {
3f551b5b
PM
11282 if (sattrs.nsc) {
11283 fi->type = ARMFault_QEMU_NSCExec;
11284 } else {
11285 fi->type = ARMFault_QEMU_SFault;
11286 }
72042435 11287 *page_size = sattrs.subpage ? 1 : TARGET_PAGE_SIZE;
54317c0f
PM
11288 *phys_ptr = address;
11289 *prot = 0;
11290 return true;
11291 }
11292 } else {
11293 /* For data accesses we always use the MMU bank indicated
11294 * by the current CPU state, but the security attributes
11295 * might downgrade a secure access to nonsecure.
11296 */
11297 if (sattrs.ns) {
11298 txattrs->secure = false;
11299 } else if (!secure) {
11300 /* NS access to S memory must fault.
11301 * Architecturally we should first check whether the
11302 * MPU information for this address indicates that we
11303 * are doing an unaligned access to Device memory, which
11304 * should generate a UsageFault instead. QEMU does not
11305 * currently check for that kind of unaligned access though.
11306 * If we added it we would need to do so as a special case
11307 * for M_FAKE_FSR_SFAULT in arm_v7m_cpu_do_interrupt().
11308 */
3f551b5b 11309 fi->type = ARMFault_QEMU_SFault;
72042435 11310 *page_size = sattrs.subpage ? 1 : TARGET_PAGE_SIZE;
54317c0f
PM
11311 *phys_ptr = address;
11312 *prot = 0;
11313 return true;
11314 }
11315 }
11316 }
11317
72042435
PM
11318 ret = pmsav8_mpu_lookup(env, address, access_type, mmu_idx, phys_ptr,
11319 txattrs, prot, &mpu_is_subpage, fi, NULL);
72042435
PM
11320 *page_size = sattrs.subpage || mpu_is_subpage ? 1 : TARGET_PAGE_SIZE;
11321 return ret;
54317c0f
PM
11322}
11323
13689d43 11324static bool get_phys_addr_pmsav5(CPUARMState *env, uint32_t address,
03ae85f8 11325 MMUAccessType access_type, ARMMMUIdx mmu_idx,
53a4e5c5
PM
11326 hwaddr *phys_ptr, int *prot,
11327 ARMMMUFaultInfo *fi)
9ee6e8bb
PB
11328{
11329 int n;
11330 uint32_t mask;
11331 uint32_t base;
0480f69a 11332 bool is_user = regime_is_user(env, mmu_idx);
9ee6e8bb 11333
3279adb9
PM
11334 if (regime_translation_disabled(env, mmu_idx)) {
11335 /* MPU disabled. */
11336 *phys_ptr = address;
11337 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
11338 return false;
11339 }
11340
9ee6e8bb
PB
11341 *phys_ptr = address;
11342 for (n = 7; n >= 0; n--) {
554b0b09 11343 base = env->cp15.c6_region[n];
87c3d486 11344 if ((base & 1) == 0) {
554b0b09 11345 continue;
87c3d486 11346 }
554b0b09
PM
11347 mask = 1 << ((base >> 1) & 0x1f);
11348 /* Keep this shift separate from the above to avoid an
11349 (undefined) << 32. */
11350 mask = (mask << 1) - 1;
87c3d486 11351 if (((base ^ address) & ~mask) == 0) {
554b0b09 11352 break;
87c3d486 11353 }
9ee6e8bb 11354 }
87c3d486 11355 if (n < 0) {
53a4e5c5 11356 fi->type = ARMFault_Background;
b7cc4e82 11357 return true;
87c3d486 11358 }
9ee6e8bb 11359
03ae85f8 11360 if (access_type == MMU_INST_FETCH) {
7e09797c 11361 mask = env->cp15.pmsav5_insn_ap;
9ee6e8bb 11362 } else {
7e09797c 11363 mask = env->cp15.pmsav5_data_ap;
9ee6e8bb
PB
11364 }
11365 mask = (mask >> (n * 4)) & 0xf;
11366 switch (mask) {
11367 case 0:
53a4e5c5
PM
11368 fi->type = ARMFault_Permission;
11369 fi->level = 1;
b7cc4e82 11370 return true;
9ee6e8bb 11371 case 1:
87c3d486 11372 if (is_user) {
53a4e5c5
PM
11373 fi->type = ARMFault_Permission;
11374 fi->level = 1;
b7cc4e82 11375 return true;
87c3d486 11376 }
554b0b09
PM
11377 *prot = PAGE_READ | PAGE_WRITE;
11378 break;
9ee6e8bb 11379 case 2:
554b0b09 11380 *prot = PAGE_READ;
87c3d486 11381 if (!is_user) {
554b0b09 11382 *prot |= PAGE_WRITE;
87c3d486 11383 }
554b0b09 11384 break;
9ee6e8bb 11385 case 3:
554b0b09
PM
11386 *prot = PAGE_READ | PAGE_WRITE;
11387 break;
9ee6e8bb 11388 case 5:
87c3d486 11389 if (is_user) {
53a4e5c5
PM
11390 fi->type = ARMFault_Permission;
11391 fi->level = 1;
b7cc4e82 11392 return true;
87c3d486 11393 }
554b0b09
PM
11394 *prot = PAGE_READ;
11395 break;
9ee6e8bb 11396 case 6:
554b0b09
PM
11397 *prot = PAGE_READ;
11398 break;
9ee6e8bb 11399 default:
554b0b09 11400 /* Bad permission. */
53a4e5c5
PM
11401 fi->type = ARMFault_Permission;
11402 fi->level = 1;
b7cc4e82 11403 return true;
9ee6e8bb 11404 }
3ad493fc 11405 *prot |= PAGE_EXEC;
b7cc4e82 11406 return false;
9ee6e8bb
PB
11407}
11408
5b2d261d
AB
11409/* Combine either inner or outer cacheability attributes for normal
11410 * memory, according to table D4-42 and pseudocode procedure
11411 * CombineS1S2AttrHints() of ARM DDI 0487B.b (the ARMv8 ARM).
11412 *
11413 * NB: only stage 1 includes allocation hints (RW bits), leading to
11414 * some asymmetry.
11415 */
11416static uint8_t combine_cacheattr_nibble(uint8_t s1, uint8_t s2)
11417{
11418 if (s1 == 4 || s2 == 4) {
11419 /* non-cacheable has precedence */
11420 return 4;
11421 } else if (extract32(s1, 2, 2) == 0 || extract32(s1, 2, 2) == 2) {
11422 /* stage 1 write-through takes precedence */
11423 return s1;
11424 } else if (extract32(s2, 2, 2) == 2) {
11425 /* stage 2 write-through takes precedence, but the allocation hint
11426 * is still taken from stage 1
11427 */
11428 return (2 << 2) | extract32(s1, 0, 2);
11429 } else { /* write-back */
11430 return s1;
11431 }
11432}
11433
11434/* Combine S1 and S2 cacheability/shareability attributes, per D4.5.4
11435 * and CombineS1S2Desc()
11436 *
11437 * @s1: Attributes from stage 1 walk
11438 * @s2: Attributes from stage 2 walk
11439 */
11440static ARMCacheAttrs combine_cacheattrs(ARMCacheAttrs s1, ARMCacheAttrs s2)
11441{
11442 uint8_t s1lo = extract32(s1.attrs, 0, 4), s2lo = extract32(s2.attrs, 0, 4);
11443 uint8_t s1hi = extract32(s1.attrs, 4, 4), s2hi = extract32(s2.attrs, 4, 4);
11444 ARMCacheAttrs ret;
11445
11446 /* Combine shareability attributes (table D4-43) */
11447 if (s1.shareability == 2 || s2.shareability == 2) {
11448 /* if either are outer-shareable, the result is outer-shareable */
11449 ret.shareability = 2;
11450 } else if (s1.shareability == 3 || s2.shareability == 3) {
11451 /* if either are inner-shareable, the result is inner-shareable */
11452 ret.shareability = 3;
11453 } else {
11454 /* both non-shareable */
11455 ret.shareability = 0;
11456 }
11457
11458 /* Combine memory type and cacheability attributes */
11459 if (s1hi == 0 || s2hi == 0) {
11460 /* Device has precedence over normal */
11461 if (s1lo == 0 || s2lo == 0) {
11462 /* nGnRnE has precedence over anything */
11463 ret.attrs = 0;
11464 } else if (s1lo == 4 || s2lo == 4) {
11465 /* non-Reordering has precedence over Reordering */
11466 ret.attrs = 4; /* nGnRE */
11467 } else if (s1lo == 8 || s2lo == 8) {
11468 /* non-Gathering has precedence over Gathering */
11469 ret.attrs = 8; /* nGRE */
11470 } else {
11471 ret.attrs = 0xc; /* GRE */
11472 }
11473
11474 /* Any location for which the resultant memory type is any
11475 * type of Device memory is always treated as Outer Shareable.
11476 */
11477 ret.shareability = 2;
11478 } else { /* Normal memory */
11479 /* Outer/inner cacheability combine independently */
11480 ret.attrs = combine_cacheattr_nibble(s1hi, s2hi) << 4
11481 | combine_cacheattr_nibble(s1lo, s2lo);
11482
11483 if (ret.attrs == 0x44) {
11484 /* Any location for which the resultant memory type is Normal
11485 * Inner Non-cacheable, Outer Non-cacheable is always treated
11486 * as Outer Shareable.
11487 */
11488 ret.shareability = 2;
11489 }
11490 }
11491
11492 return ret;
11493}
11494
11495
702a9357
PM
11496/* get_phys_addr - get the physical address for this virtual address
11497 *
11498 * Find the physical address corresponding to the given virtual address,
11499 * by doing a translation table walk on MMU based systems or using the
11500 * MPU state on MPU based systems.
11501 *
b7cc4e82
PC
11502 * Returns false if the translation was successful. Otherwise, phys_ptr, attrs,
11503 * prot and page_size may not be filled in, and the populated fsr value provides
702a9357
PM
11504 * information on why the translation aborted, in the format of a
11505 * DFSR/IFSR fault register, with the following caveats:
11506 * * we honour the short vs long DFSR format differences.
11507 * * the WnR bit is never set (the caller must do this).
f6bda88f 11508 * * for PSMAv5 based systems we don't bother to return a full FSR format
702a9357
PM
11509 * value.
11510 *
11511 * @env: CPUARMState
11512 * @address: virtual address to get physical address for
11513 * @access_type: 0 for read, 1 for write, 2 for execute
d3649702 11514 * @mmu_idx: MMU index indicating required translation regime
702a9357 11515 * @phys_ptr: set to the physical address corresponding to the virtual address
8bf5b6a9 11516 * @attrs: set to the memory transaction attributes to use
702a9357
PM
11517 * @prot: set to the permissions for the page containing phys_ptr
11518 * @page_size: set to the size of the page containing phys_ptr
5b2d261d
AB
11519 * @fi: set to fault info if the translation fails
11520 * @cacheattrs: (if non-NULL) set to the cacheability/shareability attributes
702a9357 11521 */
ebae861f
PMD
11522bool get_phys_addr(CPUARMState *env, target_ulong address,
11523 MMUAccessType access_type, ARMMMUIdx mmu_idx,
11524 hwaddr *phys_ptr, MemTxAttrs *attrs, int *prot,
11525 target_ulong *page_size,
11526 ARMMMUFaultInfo *fi, ARMCacheAttrs *cacheattrs)
9ee6e8bb 11527{
452ef8cb
RH
11528 if (mmu_idx == ARMMMUIdx_E10_0 ||
11529 mmu_idx == ARMMMUIdx_E10_1 ||
11530 mmu_idx == ARMMMUIdx_E10_1_PAN) {
9b539263
EI
11531 /* Call ourselves recursively to do the stage 1 and then stage 2
11532 * translations.
0480f69a 11533 */
9b539263
EI
11534 if (arm_feature(env, ARM_FEATURE_EL2)) {
11535 hwaddr ipa;
11536 int s2_prot;
11537 int ret;
5b2d261d 11538 ARMCacheAttrs cacheattrs2 = {};
9b539263
EI
11539
11540 ret = get_phys_addr(env, address, access_type,
8bd5c820 11541 stage_1_mmu_idx(mmu_idx), &ipa, attrs,
bc52bfeb 11542 prot, page_size, fi, cacheattrs);
9b539263
EI
11543
11544 /* If S1 fails or S2 is disabled, return early. */
97fa9350 11545 if (ret || regime_translation_disabled(env, ARMMMUIdx_Stage2)) {
9b539263
EI
11546 *phys_ptr = ipa;
11547 return ret;
11548 }
11549
11550 /* S1 is done. Now do S2 translation. */
97fa9350 11551 ret = get_phys_addr_lpae(env, ipa, access_type, ARMMMUIdx_Stage2,
9b539263 11552 phys_ptr, attrs, &s2_prot,
da909b2c 11553 page_size, fi,
5b2d261d 11554 cacheattrs != NULL ? &cacheattrs2 : NULL);
9b539263
EI
11555 fi->s2addr = ipa;
11556 /* Combine the S1 and S2 perms. */
11557 *prot &= s2_prot;
5b2d261d
AB
11558
11559 /* Combine the S1 and S2 cache attributes, if needed */
11560 if (!ret && cacheattrs != NULL) {
9d1bab33
PM
11561 if (env->cp15.hcr_el2 & HCR_DC) {
11562 /*
11563 * HCR.DC forces the first stage attributes to
11564 * Normal Non-Shareable,
11565 * Inner Write-Back Read-Allocate Write-Allocate,
11566 * Outer Write-Back Read-Allocate Write-Allocate.
11567 */
11568 cacheattrs->attrs = 0xff;
11569 cacheattrs->shareability = 0;
11570 }
5b2d261d
AB
11571 *cacheattrs = combine_cacheattrs(*cacheattrs, cacheattrs2);
11572 }
11573
9b539263
EI
11574 return ret;
11575 } else {
11576 /*
11577 * For non-EL2 CPUs a stage1+stage2 translation is just stage 1.
11578 */
8bd5c820 11579 mmu_idx = stage_1_mmu_idx(mmu_idx);
9b539263 11580 }
0480f69a 11581 }
d3649702 11582
8bf5b6a9
PM
11583 /* The page table entries may downgrade secure to non-secure, but
11584 * cannot upgrade an non-secure translation regime's attributes
11585 * to secure.
11586 */
11587 attrs->secure = regime_is_secure(env, mmu_idx);
0995bf8c 11588 attrs->user = regime_is_user(env, mmu_idx);
8bf5b6a9 11589
0480f69a
PM
11590 /* Fast Context Switch Extension. This doesn't exist at all in v8.
11591 * In v7 and earlier it affects all stage 1 translations.
11592 */
97fa9350 11593 if (address < 0x02000000 && mmu_idx != ARMMMUIdx_Stage2
0480f69a
PM
11594 && !arm_feature(env, ARM_FEATURE_V8)) {
11595 if (regime_el(env, mmu_idx) == 3) {
11596 address += env->cp15.fcseidr_s;
11597 } else {
11598 address += env->cp15.fcseidr_ns;
11599 }
54bf36ed 11600 }
9ee6e8bb 11601
3279adb9 11602 if (arm_feature(env, ARM_FEATURE_PMSA)) {
c9f9f124 11603 bool ret;
f6bda88f 11604 *page_size = TARGET_PAGE_SIZE;
3279adb9 11605
504e3cc3
PM
11606 if (arm_feature(env, ARM_FEATURE_V8)) {
11607 /* PMSAv8 */
11608 ret = get_phys_addr_pmsav8(env, address, access_type, mmu_idx,
72042435 11609 phys_ptr, attrs, prot, page_size, fi);
504e3cc3 11610 } else if (arm_feature(env, ARM_FEATURE_V7)) {
3279adb9
PM
11611 /* PMSAv7 */
11612 ret = get_phys_addr_pmsav7(env, address, access_type, mmu_idx,
e5e40999 11613 phys_ptr, prot, page_size, fi);
3279adb9
PM
11614 } else {
11615 /* Pre-v7 MPU */
11616 ret = get_phys_addr_pmsav5(env, address, access_type, mmu_idx,
53a4e5c5 11617 phys_ptr, prot, fi);
3279adb9
PM
11618 }
11619 qemu_log_mask(CPU_LOG_MMU, "PMSA MPU lookup for %s at 0x%08" PRIx32
c9f9f124 11620 " mmu_idx %u -> %s (prot %c%c%c)\n",
709e4407
PM
11621 access_type == MMU_DATA_LOAD ? "reading" :
11622 (access_type == MMU_DATA_STORE ? "writing" : "execute"),
c9f9f124
MD
11623 (uint32_t)address, mmu_idx,
11624 ret ? "Miss" : "Hit",
11625 *prot & PAGE_READ ? 'r' : '-',
11626 *prot & PAGE_WRITE ? 'w' : '-',
11627 *prot & PAGE_EXEC ? 'x' : '-');
11628
11629 return ret;
f6bda88f
PC
11630 }
11631
3279adb9
PM
11632 /* Definitely a real MMU, not an MPU */
11633
0480f69a 11634 if (regime_translation_disabled(env, mmu_idx)) {
3279adb9 11635 /* MMU disabled. */
9ee6e8bb 11636 *phys_ptr = address;
3ad493fc 11637 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
d4c430a8 11638 *page_size = TARGET_PAGE_SIZE;
9ee6e8bb 11639 return 0;
0480f69a
PM
11640 }
11641
0480f69a 11642 if (regime_using_lpae_format(env, mmu_idx)) {
bc52bfeb
PM
11643 return get_phys_addr_lpae(env, address, access_type, mmu_idx,
11644 phys_ptr, attrs, prot, page_size,
11645 fi, cacheattrs);
0480f69a 11646 } else if (regime_sctlr(env, mmu_idx) & SCTLR_XP) {
bc52bfeb
PM
11647 return get_phys_addr_v6(env, address, access_type, mmu_idx,
11648 phys_ptr, attrs, prot, page_size, fi);
9ee6e8bb 11649 } else {
bc52bfeb 11650 return get_phys_addr_v5(env, address, access_type, mmu_idx,
f989983e 11651 phys_ptr, prot, page_size, fi);
9ee6e8bb
PB
11652 }
11653}
11654
0faea0c7
PM
11655hwaddr arm_cpu_get_phys_page_attrs_debug(CPUState *cs, vaddr addr,
11656 MemTxAttrs *attrs)
b5ff1b31 11657{
00b941e5 11658 ARMCPU *cpu = ARM_CPU(cs);
d3649702 11659 CPUARMState *env = &cpu->env;
a8170e5e 11660 hwaddr phys_addr;
d4c430a8 11661 target_ulong page_size;
b5ff1b31 11662 int prot;
b7cc4e82 11663 bool ret;
e14b5a23 11664 ARMMMUFaultInfo fi = {};
50494a27 11665 ARMMMUIdx mmu_idx = arm_mmu_idx(env);
b5ff1b31 11666
0faea0c7
PM
11667 *attrs = (MemTxAttrs) {};
11668
8bd5c820 11669 ret = get_phys_addr(env, addr, 0, mmu_idx, &phys_addr,
bc52bfeb 11670 attrs, &prot, &page_size, &fi, NULL);
b5ff1b31 11671
b7cc4e82 11672 if (ret) {
b5ff1b31 11673 return -1;
00b941e5 11674 }
b5ff1b31
FB
11675 return phys_addr;
11676}
11677
b5ff1b31 11678#endif
6ddbc6e4
PB
11679
11680/* Note that signed overflow is undefined in C. The following routines are
11681 careful to use unsigned types where modulo arithmetic is required.
11682 Failure to do so _will_ break on newer gcc. */
11683
11684/* Signed saturating arithmetic. */
11685
1654b2d6 11686/* Perform 16-bit signed saturating addition. */
6ddbc6e4
PB
11687static inline uint16_t add16_sat(uint16_t a, uint16_t b)
11688{
11689 uint16_t res;
11690
11691 res = a + b;
11692 if (((res ^ a) & 0x8000) && !((a ^ b) & 0x8000)) {
11693 if (a & 0x8000)
11694 res = 0x8000;
11695 else
11696 res = 0x7fff;
11697 }
11698 return res;
11699}
11700
1654b2d6 11701/* Perform 8-bit signed saturating addition. */
6ddbc6e4
PB
11702static inline uint8_t add8_sat(uint8_t a, uint8_t b)
11703{
11704 uint8_t res;
11705
11706 res = a + b;
11707 if (((res ^ a) & 0x80) && !((a ^ b) & 0x80)) {
11708 if (a & 0x80)
11709 res = 0x80;
11710 else
11711 res = 0x7f;
11712 }
11713 return res;
11714}
11715
1654b2d6 11716/* Perform 16-bit signed saturating subtraction. */
6ddbc6e4
PB
11717static inline uint16_t sub16_sat(uint16_t a, uint16_t b)
11718{
11719 uint16_t res;
11720
11721 res = a - b;
11722 if (((res ^ a) & 0x8000) && ((a ^ b) & 0x8000)) {
11723 if (a & 0x8000)
11724 res = 0x8000;
11725 else
11726 res = 0x7fff;
11727 }
11728 return res;
11729}
11730
1654b2d6 11731/* Perform 8-bit signed saturating subtraction. */
6ddbc6e4
PB
11732static inline uint8_t sub8_sat(uint8_t a, uint8_t b)
11733{
11734 uint8_t res;
11735
11736 res = a - b;
11737 if (((res ^ a) & 0x80) && ((a ^ b) & 0x80)) {
11738 if (a & 0x80)
11739 res = 0x80;
11740 else
11741 res = 0x7f;
11742 }
11743 return res;
11744}
11745
11746#define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16);
11747#define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16);
11748#define ADD8(a, b, n) RESULT(add8_sat(a, b), n, 8);
11749#define SUB8(a, b, n) RESULT(sub8_sat(a, b), n, 8);
11750#define PFX q
11751
11752#include "op_addsub.h"
11753
11754/* Unsigned saturating arithmetic. */
460a09c1 11755static inline uint16_t add16_usat(uint16_t a, uint16_t b)
6ddbc6e4
PB
11756{
11757 uint16_t res;
11758 res = a + b;
11759 if (res < a)
11760 res = 0xffff;
11761 return res;
11762}
11763
460a09c1 11764static inline uint16_t sub16_usat(uint16_t a, uint16_t b)
6ddbc6e4 11765{
4c4fd3f8 11766 if (a > b)
6ddbc6e4
PB
11767 return a - b;
11768 else
11769 return 0;
11770}
11771
11772static inline uint8_t add8_usat(uint8_t a, uint8_t b)
11773{
11774 uint8_t res;
11775 res = a + b;
11776 if (res < a)
11777 res = 0xff;
11778 return res;
11779}
11780
11781static inline uint8_t sub8_usat(uint8_t a, uint8_t b)
11782{
4c4fd3f8 11783 if (a > b)
6ddbc6e4
PB
11784 return a - b;
11785 else
11786 return 0;
11787}
11788
11789#define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16);
11790#define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16);
11791#define ADD8(a, b, n) RESULT(add8_usat(a, b), n, 8);
11792#define SUB8(a, b, n) RESULT(sub8_usat(a, b), n, 8);
11793#define PFX uq
11794
11795#include "op_addsub.h"
11796
11797/* Signed modulo arithmetic. */
11798#define SARITH16(a, b, n, op) do { \
11799 int32_t sum; \
db6e2e65 11800 sum = (int32_t)(int16_t)(a) op (int32_t)(int16_t)(b); \
6ddbc6e4
PB
11801 RESULT(sum, n, 16); \
11802 if (sum >= 0) \
11803 ge |= 3 << (n * 2); \
11804 } while(0)
11805
11806#define SARITH8(a, b, n, op) do { \
11807 int32_t sum; \
db6e2e65 11808 sum = (int32_t)(int8_t)(a) op (int32_t)(int8_t)(b); \
6ddbc6e4
PB
11809 RESULT(sum, n, 8); \
11810 if (sum >= 0) \
11811 ge |= 1 << n; \
11812 } while(0)
11813
11814
11815#define ADD16(a, b, n) SARITH16(a, b, n, +)
11816#define SUB16(a, b, n) SARITH16(a, b, n, -)
11817#define ADD8(a, b, n) SARITH8(a, b, n, +)
11818#define SUB8(a, b, n) SARITH8(a, b, n, -)
11819#define PFX s
11820#define ARITH_GE
11821
11822#include "op_addsub.h"
11823
11824/* Unsigned modulo arithmetic. */
11825#define ADD16(a, b, n) do { \
11826 uint32_t sum; \
11827 sum = (uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b); \
11828 RESULT(sum, n, 16); \
a87aa10b 11829 if ((sum >> 16) == 1) \
6ddbc6e4
PB
11830 ge |= 3 << (n * 2); \
11831 } while(0)
11832
11833#define ADD8(a, b, n) do { \
11834 uint32_t sum; \
11835 sum = (uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b); \
11836 RESULT(sum, n, 8); \
a87aa10b
AZ
11837 if ((sum >> 8) == 1) \
11838 ge |= 1 << n; \
6ddbc6e4
PB
11839 } while(0)
11840
11841#define SUB16(a, b, n) do { \
11842 uint32_t sum; \
11843 sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \
11844 RESULT(sum, n, 16); \
11845 if ((sum >> 16) == 0) \
11846 ge |= 3 << (n * 2); \
11847 } while(0)
11848
11849#define SUB8(a, b, n) do { \
11850 uint32_t sum; \
11851 sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \
11852 RESULT(sum, n, 8); \
11853 if ((sum >> 8) == 0) \
a87aa10b 11854 ge |= 1 << n; \
6ddbc6e4
PB
11855 } while(0)
11856
11857#define PFX u
11858#define ARITH_GE
11859
11860#include "op_addsub.h"
11861
11862/* Halved signed arithmetic. */
11863#define ADD16(a, b, n) \
11864 RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16)
11865#define SUB16(a, b, n) \
11866 RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16)
11867#define ADD8(a, b, n) \
11868 RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8)
11869#define SUB8(a, b, n) \
11870 RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8)
11871#define PFX sh
11872
11873#include "op_addsub.h"
11874
11875/* Halved unsigned arithmetic. */
11876#define ADD16(a, b, n) \
11877 RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16)
11878#define SUB16(a, b, n) \
11879 RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16)
11880#define ADD8(a, b, n) \
11881 RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8)
11882#define SUB8(a, b, n) \
11883 RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8)
11884#define PFX uh
11885
11886#include "op_addsub.h"
11887
11888static inline uint8_t do_usad(uint8_t a, uint8_t b)
11889{
11890 if (a > b)
11891 return a - b;
11892 else
11893 return b - a;
11894}
11895
11896/* Unsigned sum of absolute byte differences. */
11897uint32_t HELPER(usad8)(uint32_t a, uint32_t b)
11898{
11899 uint32_t sum;
11900 sum = do_usad(a, b);
11901 sum += do_usad(a >> 8, b >> 8);
11902 sum += do_usad(a >> 16, b >>16);
11903 sum += do_usad(a >> 24, b >> 24);
11904 return sum;
11905}
11906
11907/* For ARMv6 SEL instruction. */
11908uint32_t HELPER(sel_flags)(uint32_t flags, uint32_t a, uint32_t b)
11909{
11910 uint32_t mask;
11911
11912 mask = 0;
11913 if (flags & 1)
11914 mask |= 0xff;
11915 if (flags & 2)
11916 mask |= 0xff00;
11917 if (flags & 4)
11918 mask |= 0xff0000;
11919 if (flags & 8)
11920 mask |= 0xff000000;
11921 return (a & mask) | (b & ~mask);
11922}
11923
aa633469
PM
11924/* CRC helpers.
11925 * The upper bytes of val (above the number specified by 'bytes') must have
11926 * been zeroed out by the caller.
11927 */
eb0ecd5a
WN
11928uint32_t HELPER(crc32)(uint32_t acc, uint32_t val, uint32_t bytes)
11929{
11930 uint8_t buf[4];
11931
aa633469 11932 stl_le_p(buf, val);
eb0ecd5a
WN
11933
11934 /* zlib crc32 converts the accumulator and output to one's complement. */
11935 return crc32(acc ^ 0xffffffff, buf, bytes) ^ 0xffffffff;
11936}
11937
11938uint32_t HELPER(crc32c)(uint32_t acc, uint32_t val, uint32_t bytes)
11939{
11940 uint8_t buf[4];
11941
aa633469 11942 stl_le_p(buf, val);
eb0ecd5a
WN
11943
11944 /* Linux crc32c converts the output to one's complement. */
11945 return crc32c(acc, buf, bytes) ^ 0xffffffff;
11946}
a9e01311
RH
11947
11948/* Return the exception level to which FP-disabled exceptions should
11949 * be taken, or 0 if FP is enabled.
11950 */
ced31551 11951int fp_exception_el(CPUARMState *env, int cur_el)
a9e01311 11952{
55faa212 11953#ifndef CONFIG_USER_ONLY
a9e01311
RH
11954 /* CPACR and the CPTR registers don't exist before v6, so FP is
11955 * always accessible
11956 */
11957 if (!arm_feature(env, ARM_FEATURE_V6)) {
11958 return 0;
11959 }
11960
d87513c0
PM
11961 if (arm_feature(env, ARM_FEATURE_M)) {
11962 /* CPACR can cause a NOCP UsageFault taken to current security state */
11963 if (!v7m_cpacr_pass(env, env->v7m.secure, cur_el != 0)) {
11964 return 1;
11965 }
11966
11967 if (arm_feature(env, ARM_FEATURE_M_SECURITY) && !env->v7m.secure) {
11968 if (!extract32(env->v7m.nsacr, 10, 1)) {
11969 /* FP insns cause a NOCP UsageFault taken to Secure */
11970 return 3;
11971 }
11972 }
11973
11974 return 0;
11975 }
11976
a9e01311
RH
11977 /* The CPACR controls traps to EL1, or PL1 if we're 32 bit:
11978 * 0, 2 : trap EL0 and EL1/PL1 accesses
11979 * 1 : trap only EL0 accesses
11980 * 3 : trap no accesses
c2ddb7cf 11981 * This register is ignored if E2H+TGE are both set.
a9e01311 11982 */
c2ddb7cf
RH
11983 if ((arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
11984 int fpen = extract32(env->cp15.cpacr_el1, 20, 2);
11985
11986 switch (fpen) {
11987 case 0:
11988 case 2:
11989 if (cur_el == 0 || cur_el == 1) {
11990 /* Trap to PL1, which might be EL1 or EL3 */
11991 if (arm_is_secure(env) && !arm_el_is_aa64(env, 3)) {
11992 return 3;
11993 }
11994 return 1;
11995 }
11996 if (cur_el == 3 && !is_a64(env)) {
11997 /* Secure PL1 running at EL3 */
a9e01311
RH
11998 return 3;
11999 }
c2ddb7cf
RH
12000 break;
12001 case 1:
12002 if (cur_el == 0) {
12003 return 1;
12004 }
12005 break;
12006 case 3:
12007 break;
a9e01311 12008 }
a9e01311
RH
12009 }
12010
fc1120a7
PM
12011 /*
12012 * The NSACR allows A-profile AArch32 EL3 and M-profile secure mode
12013 * to control non-secure access to the FPU. It doesn't have any
12014 * effect if EL3 is AArch64 or if EL3 doesn't exist at all.
12015 */
12016 if ((arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
12017 cur_el <= 2 && !arm_is_secure_below_el3(env))) {
12018 if (!extract32(env->cp15.nsacr, 10, 1)) {
12019 /* FP insns act as UNDEF */
12020 return cur_el == 2 ? 2 : 1;
12021 }
12022 }
12023
a9e01311
RH
12024 /* For the CPTR registers we don't need to guard with an ARM_FEATURE
12025 * check because zero bits in the registers mean "don't trap".
12026 */
12027
12028 /* CPTR_EL2 : present in v7VE or v8 */
12029 if (cur_el <= 2 && extract32(env->cp15.cptr_el[2], 10, 1)
12030 && !arm_is_secure_below_el3(env)) {
12031 /* Trap FP ops at EL2, NS-EL1 or NS-EL0 to EL2 */
12032 return 2;
12033 }
12034
12035 /* CPTR_EL3 : present in v8 */
12036 if (extract32(env->cp15.cptr_el[3], 10, 1)) {
12037 /* Trap all FP ops to EL3 */
12038 return 3;
12039 }
55faa212 12040#endif
a9e01311
RH
12041 return 0;
12042}
12043
b9f6033c
RH
12044/* Return the exception level we're running at if this is our mmu_idx */
12045int arm_mmu_idx_to_el(ARMMMUIdx mmu_idx)
12046{
12047 if (mmu_idx & ARM_MMU_IDX_M) {
12048 return mmu_idx & ARM_MMU_IDX_M_PRIV;
12049 }
12050
12051 switch (mmu_idx) {
12052 case ARMMMUIdx_E10_0:
12053 case ARMMMUIdx_E20_0:
12054 case ARMMMUIdx_SE10_0:
12055 return 0;
12056 case ARMMMUIdx_E10_1:
452ef8cb 12057 case ARMMMUIdx_E10_1_PAN:
b9f6033c 12058 case ARMMMUIdx_SE10_1:
452ef8cb 12059 case ARMMMUIdx_SE10_1_PAN:
b9f6033c
RH
12060 return 1;
12061 case ARMMMUIdx_E2:
12062 case ARMMMUIdx_E20_2:
452ef8cb 12063 case ARMMMUIdx_E20_2_PAN:
b9f6033c
RH
12064 return 2;
12065 case ARMMMUIdx_SE3:
12066 return 3;
12067 default:
12068 g_assert_not_reached();
12069 }
12070}
12071
7aab5a8c 12072#ifndef CONFIG_TCG
65e4655c
RH
12073ARMMMUIdx arm_v7m_mmu_idx_for_secstate(CPUARMState *env, bool secstate)
12074{
7aab5a8c 12075 g_assert_not_reached();
65e4655c 12076}
7aab5a8c 12077#endif
65e4655c 12078
164690b2 12079ARMMMUIdx arm_mmu_idx_el(CPUARMState *env, int el)
65e4655c 12080{
65e4655c 12081 if (arm_feature(env, ARM_FEATURE_M)) {
50494a27 12082 return arm_v7m_mmu_idx_for_secstate(env, env->v7m.secure);
65e4655c
RH
12083 }
12084
6003d980 12085 /* See ARM pseudo-function ELIsInHost. */
b9f6033c
RH
12086 switch (el) {
12087 case 0:
b9f6033c
RH
12088 if (arm_is_secure_below_el3(env)) {
12089 return ARMMMUIdx_SE10_0;
12090 }
6003d980
RH
12091 if ((env->cp15.hcr_el2 & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)
12092 && arm_el_is_aa64(env, 2)) {
12093 return ARMMMUIdx_E20_0;
12094 }
b9f6033c
RH
12095 return ARMMMUIdx_E10_0;
12096 case 1:
12097 if (arm_is_secure_below_el3(env)) {
66412260
RH
12098 if (env->pstate & PSTATE_PAN) {
12099 return ARMMMUIdx_SE10_1_PAN;
12100 }
b9f6033c
RH
12101 return ARMMMUIdx_SE10_1;
12102 }
66412260
RH
12103 if (env->pstate & PSTATE_PAN) {
12104 return ARMMMUIdx_E10_1_PAN;
12105 }
b9f6033c
RH
12106 return ARMMMUIdx_E10_1;
12107 case 2:
b9f6033c 12108 /* TODO: ARMv8.4-SecEL2 */
6003d980
RH
12109 /* Note that TGE does not apply at EL2. */
12110 if ((env->cp15.hcr_el2 & HCR_E2H) && arm_el_is_aa64(env, 2)) {
66412260
RH
12111 if (env->pstate & PSTATE_PAN) {
12112 return ARMMMUIdx_E20_2_PAN;
12113 }
6003d980
RH
12114 return ARMMMUIdx_E20_2;
12115 }
b9f6033c
RH
12116 return ARMMMUIdx_E2;
12117 case 3:
12118 return ARMMMUIdx_SE3;
12119 default:
12120 g_assert_not_reached();
65e4655c 12121 }
50494a27
RH
12122}
12123
164690b2
RH
12124ARMMMUIdx arm_mmu_idx(CPUARMState *env)
12125{
12126 return arm_mmu_idx_el(env, arm_current_el(env));
12127}
12128
50494a27
RH
12129int cpu_mmu_index(CPUARMState *env, bool ifetch)
12130{
12131 return arm_to_core_mmu_idx(arm_mmu_idx(env));
65e4655c
RH
12132}
12133
64be86ab
RH
12134#ifndef CONFIG_USER_ONLY
12135ARMMMUIdx arm_stage1_mmu_idx(CPUARMState *env)
12136{
12137 return stage_1_mmu_idx(arm_mmu_idx(env));
12138}
12139#endif
12140
fdd1b228
RH
12141static uint32_t rebuild_hflags_common(CPUARMState *env, int fp_el,
12142 ARMMMUIdx mmu_idx, uint32_t flags)
12143{
12144 flags = FIELD_DP32(flags, TBFLAG_ANY, FPEXC_EL, fp_el);
12145 flags = FIELD_DP32(flags, TBFLAG_ANY, MMUIDX,
12146 arm_to_core_mmu_idx(mmu_idx));
12147
fdd1b228
RH
12148 if (arm_singlestep_active(env)) {
12149 flags = FIELD_DP32(flags, TBFLAG_ANY, SS_ACTIVE, 1);
12150 }
12151 return flags;
12152}
12153
43eccfb6
RH
12154static uint32_t rebuild_hflags_common_32(CPUARMState *env, int fp_el,
12155 ARMMMUIdx mmu_idx, uint32_t flags)
12156{
8061a649
RH
12157 bool sctlr_b = arm_sctlr_b(env);
12158
12159 if (sctlr_b) {
12160 flags = FIELD_DP32(flags, TBFLAG_A32, SCTLR_B, 1);
12161 }
12162 if (arm_cpu_data_is_big_endian_a32(env, sctlr_b)) {
12163 flags = FIELD_DP32(flags, TBFLAG_ANY, BE_DATA, 1);
12164 }
43eccfb6
RH
12165 flags = FIELD_DP32(flags, TBFLAG_A32, NS, !access_secure_reg(env));
12166
12167 return rebuild_hflags_common(env, fp_el, mmu_idx, flags);
12168}
12169
6e33ced5
RH
12170static uint32_t rebuild_hflags_m32(CPUARMState *env, int fp_el,
12171 ARMMMUIdx mmu_idx)
12172{
12173 uint32_t flags = 0;
12174
12175 if (arm_v7m_is_handler_mode(env)) {
79cabf1f 12176 flags = FIELD_DP32(flags, TBFLAG_M32, HANDLER, 1);
6e33ced5
RH
12177 }
12178
12179 /*
12180 * v8M always applies stack limit checks unless CCR.STKOFHFNMIGN
12181 * is suppressing them because the requested execution priority
12182 * is less than 0.
12183 */
12184 if (arm_feature(env, ARM_FEATURE_V8) &&
12185 !((mmu_idx & ARM_MMU_IDX_M_NEGPRI) &&
12186 (env->v7m.ccr[env->v7m.secure] & R_V7M_CCR_STKOFHFNMIGN_MASK))) {
79cabf1f 12187 flags = FIELD_DP32(flags, TBFLAG_M32, STACKCHECK, 1);
6e33ced5
RH
12188 }
12189
12190 return rebuild_hflags_common_32(env, fp_el, mmu_idx, flags);
12191}
12192
83f4baef
RH
12193static uint32_t rebuild_hflags_aprofile(CPUARMState *env)
12194{
12195 int flags = 0;
12196
12197 flags = FIELD_DP32(flags, TBFLAG_ANY, DEBUG_TARGET_EL,
12198 arm_debug_target_el(env));
12199 return flags;
12200}
12201
c747224c
RH
12202static uint32_t rebuild_hflags_a32(CPUARMState *env, int fp_el,
12203 ARMMMUIdx mmu_idx)
12204{
83f4baef 12205 uint32_t flags = rebuild_hflags_aprofile(env);
0a54d68e
RH
12206
12207 if (arm_el_is_aa64(env, 1)) {
12208 flags = FIELD_DP32(flags, TBFLAG_A32, VFPEN, 1);
12209 }
5bb0a20b
MZ
12210
12211 if (arm_current_el(env) < 2 && env->cp15.hstr_el2 &&
12212 (arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
12213 flags = FIELD_DP32(flags, TBFLAG_A32, HSTR_ACTIVE, 1);
12214 }
12215
83f4baef 12216 return rebuild_hflags_common_32(env, fp_el, mmu_idx, flags);
c747224c
RH
12217}
12218
d4d7503a
RH
12219static uint32_t rebuild_hflags_a64(CPUARMState *env, int el, int fp_el,
12220 ARMMMUIdx mmu_idx)
a9e01311 12221{
83f4baef 12222 uint32_t flags = rebuild_hflags_aprofile(env);
d4d7503a 12223 ARMMMUIdx stage1 = stage_1_mmu_idx(mmu_idx);
b830a5ee 12224 uint64_t tcr = regime_tcr(env, mmu_idx)->raw_tcr;
d4d7503a
RH
12225 uint64_t sctlr;
12226 int tbii, tbid;
b9adaa70 12227
d4d7503a 12228 flags = FIELD_DP32(flags, TBFLAG_ANY, AARCH64_STATE, 1);
cd208a1c 12229
339370b9 12230 /* Get control bits for tagged addresses. */
b830a5ee
RH
12231 tbid = aa64_va_parameter_tbi(tcr, mmu_idx);
12232 tbii = tbid & ~aa64_va_parameter_tbid(tcr, mmu_idx);
5d8634f5 12233
d4d7503a
RH
12234 flags = FIELD_DP32(flags, TBFLAG_A64, TBII, tbii);
12235 flags = FIELD_DP32(flags, TBFLAG_A64, TBID, tbid);
12236
12237 if (cpu_isar_feature(aa64_sve, env_archcpu(env))) {
12238 int sve_el = sve_exception_el(env, el);
12239 uint32_t zcr_len;
5d8634f5 12240
d4d7503a
RH
12241 /*
12242 * If SVE is disabled, but FP is enabled,
12243 * then the effective len is 0.
12244 */
12245 if (sve_el != 0 && fp_el == 0) {
12246 zcr_len = 0;
12247 } else {
12248 zcr_len = sve_zcr_len_for_el(env, el);
5d8634f5 12249 }
d4d7503a
RH
12250 flags = FIELD_DP32(flags, TBFLAG_A64, SVEEXC_EL, sve_el);
12251 flags = FIELD_DP32(flags, TBFLAG_A64, ZCR_LEN, zcr_len);
12252 }
1db5e96c 12253
aaec1432 12254 sctlr = regime_sctlr(env, stage1);
1db5e96c 12255
8061a649
RH
12256 if (arm_cpu_data_is_big_endian_a64(el, sctlr)) {
12257 flags = FIELD_DP32(flags, TBFLAG_ANY, BE_DATA, 1);
12258 }
12259
d4d7503a
RH
12260 if (cpu_isar_feature(aa64_pauth, env_archcpu(env))) {
12261 /*
12262 * In order to save space in flags, we record only whether
12263 * pauth is "inactive", meaning all insns are implemented as
12264 * a nop, or "active" when some action must be performed.
12265 * The decision of which action to take is left to a helper.
12266 */
12267 if (sctlr & (SCTLR_EnIA | SCTLR_EnIB | SCTLR_EnDA | SCTLR_EnDB)) {
12268 flags = FIELD_DP32(flags, TBFLAG_A64, PAUTH_ACTIVE, 1);
1db5e96c 12269 }
d4d7503a 12270 }
0816ef1b 12271
d4d7503a
RH
12272 if (cpu_isar_feature(aa64_bti, env_archcpu(env))) {
12273 /* Note that SCTLR_EL[23].BT == SCTLR_BT1. */
12274 if (sctlr & (el == 0 ? SCTLR_BT0 : SCTLR_BT1)) {
12275 flags = FIELD_DP32(flags, TBFLAG_A64, BT, 1);
0816ef1b 12276 }
d4d7503a 12277 }
08f1434a 12278
cc28fc30 12279 /* Compute the condition for using AccType_UNPRIV for LDTR et al. */
7a8014ab
RH
12280 if (!(env->pstate & PSTATE_UAO)) {
12281 switch (mmu_idx) {
12282 case ARMMMUIdx_E10_1:
12283 case ARMMMUIdx_E10_1_PAN:
12284 case ARMMMUIdx_SE10_1:
12285 case ARMMMUIdx_SE10_1_PAN:
12286 /* TODO: ARMv8.3-NV */
cc28fc30 12287 flags = FIELD_DP32(flags, TBFLAG_A64, UNPRIV, 1);
7a8014ab
RH
12288 break;
12289 case ARMMMUIdx_E20_2:
12290 case ARMMMUIdx_E20_2_PAN:
12291 /* TODO: ARMv8.4-SecEL2 */
12292 /*
12293 * Note that EL20_2 is gated by HCR_EL2.E2H == 1, but EL20_0 is
12294 * gated by HCR_EL2.<E2H,TGE> == '11', and so is LDTR.
12295 */
12296 if (env->cp15.hcr_el2 & HCR_TGE) {
12297 flags = FIELD_DP32(flags, TBFLAG_A64, UNPRIV, 1);
12298 }
12299 break;
12300 default:
12301 break;
cc28fc30 12302 }
cc28fc30
RH
12303 }
12304
d4d7503a
RH
12305 return rebuild_hflags_common(env, fp_el, mmu_idx, flags);
12306}
12307
3d74e2e9
RH
12308static uint32_t rebuild_hflags_internal(CPUARMState *env)
12309{
12310 int el = arm_current_el(env);
12311 int fp_el = fp_exception_el(env, el);
164690b2 12312 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
3d74e2e9
RH
12313
12314 if (is_a64(env)) {
12315 return rebuild_hflags_a64(env, el, fp_el, mmu_idx);
12316 } else if (arm_feature(env, ARM_FEATURE_M)) {
12317 return rebuild_hflags_m32(env, fp_el, mmu_idx);
12318 } else {
12319 return rebuild_hflags_a32(env, fp_el, mmu_idx);
12320 }
12321}
12322
12323void arm_rebuild_hflags(CPUARMState *env)
12324{
12325 env->hflags = rebuild_hflags_internal(env);
12326}
12327
14f3c588
RH
12328void HELPER(rebuild_hflags_m32)(CPUARMState *env, int el)
12329{
12330 int fp_el = fp_exception_el(env, el);
12331 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
12332
12333 env->hflags = rebuild_hflags_m32(env, fp_el, mmu_idx);
12334}
12335
f80741d1
AB
12336/*
12337 * If we have triggered a EL state change we can't rely on the
12338 * translator having passed it too us, we need to recompute.
12339 */
12340void HELPER(rebuild_hflags_a32_newel)(CPUARMState *env)
12341{
12342 int el = arm_current_el(env);
12343 int fp_el = fp_exception_el(env, el);
12344 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
12345 env->hflags = rebuild_hflags_a32(env, fp_el, mmu_idx);
12346}
12347
14f3c588
RH
12348void HELPER(rebuild_hflags_a32)(CPUARMState *env, int el)
12349{
12350 int fp_el = fp_exception_el(env, el);
12351 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
12352
12353 env->hflags = rebuild_hflags_a32(env, fp_el, mmu_idx);
12354}
12355
12356void HELPER(rebuild_hflags_a64)(CPUARMState *env, int el)
12357{
12358 int fp_el = fp_exception_el(env, el);
12359 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
12360
12361 env->hflags = rebuild_hflags_a64(env, el, fp_el, mmu_idx);
12362}
12363
0ee8b24a
PMD
12364static inline void assert_hflags_rebuild_correctly(CPUARMState *env)
12365{
12366#ifdef CONFIG_DEBUG_TCG
12367 uint32_t env_flags_current = env->hflags;
12368 uint32_t env_flags_rebuilt = rebuild_hflags_internal(env);
12369
12370 if (unlikely(env_flags_current != env_flags_rebuilt)) {
12371 fprintf(stderr, "TCG hflags mismatch (current:0x%08x rebuilt:0x%08x)\n",
12372 env_flags_current, env_flags_rebuilt);
12373 abort();
12374 }
12375#endif
12376}
12377
d4d7503a
RH
12378void cpu_get_tb_cpu_state(CPUARMState *env, target_ulong *pc,
12379 target_ulong *cs_base, uint32_t *pflags)
12380{
e979972a
RH
12381 uint32_t flags = env->hflags;
12382 uint32_t pstate_for_ss;
d4d7503a 12383
9b253fe5 12384 *cs_base = 0;
0ee8b24a 12385 assert_hflags_rebuild_correctly(env);
3d74e2e9 12386
e979972a 12387 if (FIELD_EX32(flags, TBFLAG_ANY, AARCH64_STATE)) {
d4d7503a 12388 *pc = env->pc;
d4d7503a 12389 if (cpu_isar_feature(aa64_bti, env_archcpu(env))) {
08f1434a
RH
12390 flags = FIELD_DP32(flags, TBFLAG_A64, BTYPE, env->btype);
12391 }
60e12c37 12392 pstate_for_ss = env->pstate;
a9e01311
RH
12393 } else {
12394 *pc = env->regs[15];
6e33ced5
RH
12395
12396 if (arm_feature(env, ARM_FEATURE_M)) {
9550d1bd
RH
12397 if (arm_feature(env, ARM_FEATURE_M_SECURITY) &&
12398 FIELD_EX32(env->v7m.fpccr[M_REG_S], V7M_FPCCR, S)
12399 != env->v7m.secure) {
79cabf1f 12400 flags = FIELD_DP32(flags, TBFLAG_M32, FPCCR_S_WRONG, 1);
9550d1bd
RH
12401 }
12402
12403 if ((env->v7m.fpccr[env->v7m.secure] & R_V7M_FPCCR_ASPEN_MASK) &&
12404 (!(env->v7m.control[M_REG_S] & R_V7M_CONTROL_FPCA_MASK) ||
12405 (env->v7m.secure &&
12406 !(env->v7m.control[M_REG_S] & R_V7M_CONTROL_SFPA_MASK)))) {
12407 /*
12408 * ASPEN is set, but FPCA/SFPA indicate that there is no
12409 * active FP context; we must create a new FP context before
12410 * executing any FP insn.
12411 */
79cabf1f 12412 flags = FIELD_DP32(flags, TBFLAG_M32, NEW_FP_CTXT_NEEDED, 1);
9550d1bd
RH
12413 }
12414
12415 bool is_secure = env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_S_MASK;
12416 if (env->v7m.fpccr[is_secure] & R_V7M_FPCCR_LSPACT_MASK) {
79cabf1f 12417 flags = FIELD_DP32(flags, TBFLAG_M32, LSPACT, 1);
9550d1bd 12418 }
6e33ced5 12419 } else {
bbad7c62
RH
12420 /*
12421 * Note that XSCALE_CPAR shares bits with VECSTRIDE.
12422 * Note that VECLEN+VECSTRIDE are RES0 for M-profile.
12423 */
12424 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
12425 flags = FIELD_DP32(flags, TBFLAG_A32,
12426 XSCALE_CPAR, env->cp15.c15_cpar);
12427 } else {
12428 flags = FIELD_DP32(flags, TBFLAG_A32, VECLEN,
12429 env->vfp.vec_len);
12430 flags = FIELD_DP32(flags, TBFLAG_A32, VECSTRIDE,
12431 env->vfp.vec_stride);
12432 }
0a54d68e
RH
12433 if (env->vfp.xregs[ARM_VFP_FPEXC] & (1 << 30)) {
12434 flags = FIELD_DP32(flags, TBFLAG_A32, VFPEN, 1);
12435 }
6e33ced5
RH
12436 }
12437
79cabf1f
RH
12438 flags = FIELD_DP32(flags, TBFLAG_AM32, THUMB, env->thumb);
12439 flags = FIELD_DP32(flags, TBFLAG_AM32, CONDEXEC, env->condexec_bits);
60e12c37 12440 pstate_for_ss = env->uncached_cpsr;
d4d7503a 12441 }
a9e01311 12442
60e12c37
RH
12443 /*
12444 * The SS_ACTIVE and PSTATE_SS bits correspond to the state machine
a9e01311
RH
12445 * states defined in the ARM ARM for software singlestep:
12446 * SS_ACTIVE PSTATE.SS State
12447 * 0 x Inactive (the TB flag for SS is always 0)
12448 * 1 0 Active-pending
12449 * 1 1 Active-not-pending
fdd1b228 12450 * SS_ACTIVE is set in hflags; PSTATE_SS is computed every TB.
a9e01311 12451 */
60e12c37
RH
12452 if (FIELD_EX32(flags, TBFLAG_ANY, SS_ACTIVE) &&
12453 (pstate_for_ss & PSTATE_SS)) {
12454 flags = FIELD_DP32(flags, TBFLAG_ANY, PSTATE_SS, 1);
a9e01311 12455 }
a9e01311 12456
b9adaa70 12457 *pflags = flags;
a9e01311 12458}
0ab5953b
RH
12459
12460#ifdef TARGET_AARCH64
12461/*
12462 * The manual says that when SVE is enabled and VQ is widened the
12463 * implementation is allowed to zero the previously inaccessible
12464 * portion of the registers. The corollary to that is that when
12465 * SVE is enabled and VQ is narrowed we are also allowed to zero
12466 * the now inaccessible portion of the registers.
12467 *
12468 * The intent of this is that no predicate bit beyond VQ is ever set.
12469 * Which means that some operations on predicate registers themselves
12470 * may operate on full uint64_t or even unrolled across the maximum
12471 * uint64_t[4]. Performing 4 bits of host arithmetic unconditionally
12472 * may well be cheaper than conditionals to restrict the operation
12473 * to the relevant portion of a uint16_t[16].
12474 */
12475void aarch64_sve_narrow_vq(CPUARMState *env, unsigned vq)
12476{
12477 int i, j;
12478 uint64_t pmask;
12479
12480 assert(vq >= 1 && vq <= ARM_MAX_VQ);
2fc0cc0e 12481 assert(vq <= env_archcpu(env)->sve_max_vq);
0ab5953b
RH
12482
12483 /* Zap the high bits of the zregs. */
12484 for (i = 0; i < 32; i++) {
12485 memset(&env->vfp.zregs[i].d[2 * vq], 0, 16 * (ARM_MAX_VQ - vq));
12486 }
12487
12488 /* Zap the high bits of the pregs and ffr. */
12489 pmask = 0;
12490 if (vq & 3) {
12491 pmask = ~(-1ULL << (16 * (vq & 3)));
12492 }
12493 for (j = vq / 4; j < ARM_MAX_VQ / 4; j++) {
12494 for (i = 0; i < 17; ++i) {
12495 env->vfp.pregs[i].p[j] &= pmask;
12496 }
12497 pmask = 0;
12498 }
12499}
12500
12501/*
12502 * Notice a change in SVE vector size when changing EL.
12503 */
9a05f7b6
RH
12504void aarch64_sve_change_el(CPUARMState *env, int old_el,
12505 int new_el, bool el0_a64)
0ab5953b 12506{
2fc0cc0e 12507 ARMCPU *cpu = env_archcpu(env);
0ab5953b 12508 int old_len, new_len;
9a05f7b6 12509 bool old_a64, new_a64;
0ab5953b
RH
12510
12511 /* Nothing to do if no SVE. */
cd208a1c 12512 if (!cpu_isar_feature(aa64_sve, cpu)) {
0ab5953b
RH
12513 return;
12514 }
12515
12516 /* Nothing to do if FP is disabled in either EL. */
12517 if (fp_exception_el(env, old_el) || fp_exception_el(env, new_el)) {
12518 return;
12519 }
12520
12521 /*
12522 * DDI0584A.d sec 3.2: "If SVE instructions are disabled or trapped
12523 * at ELx, or not available because the EL is in AArch32 state, then
12524 * for all purposes other than a direct read, the ZCR_ELx.LEN field
12525 * has an effective value of 0".
12526 *
12527 * Consider EL2 (aa64, vq=4) -> EL0 (aa32) -> EL1 (aa64, vq=0).
12528 * If we ignore aa32 state, we would fail to see the vq4->vq0 transition
12529 * from EL2->EL1. Thus we go ahead and narrow when entering aa32 so that
12530 * we already have the correct register contents when encountering the
12531 * vq0->vq0 transition between EL0->EL1.
12532 */
9a05f7b6
RH
12533 old_a64 = old_el ? arm_el_is_aa64(env, old_el) : el0_a64;
12534 old_len = (old_a64 && !sve_exception_el(env, old_el)
0ab5953b 12535 ? sve_zcr_len_for_el(env, old_el) : 0);
9a05f7b6
RH
12536 new_a64 = new_el ? arm_el_is_aa64(env, new_el) : el0_a64;
12537 new_len = (new_a64 && !sve_exception_el(env, new_el)
0ab5953b
RH
12538 ? sve_zcr_len_for_el(env, new_el) : 0);
12539
12540 /* When changing vector length, clear inaccessible state. */
12541 if (new_len < old_len) {
12542 aarch64_sve_narrow_vq(env, new_len + 1);
12543 }
12544}
12545#endif