]> git.proxmox.com Git - mirror_qemu.git/blame - target/arm/helper.c
target/arm: Reorganize ARMMMUIdx
[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"
9d2b5a58 28#include "qemu/range.h"
7f7b4e7a 29#include "qapi/qapi-commands-machine-target.h"
de390645
RH
30#include "qapi/error.h"
31#include "qemu/guest-random.h"
91f78c58
PMD
32#ifdef CONFIG_TCG
33#include "arm_ldst.h"
7aab5a8c 34#include "exec/cpu_ldst.h"
91f78c58 35#endif
0b03bdfc 36
352c98e5
LV
37#define ARM_CPU_FREQ 1000000000 /* FIXME: 1 GHz, should be configurable */
38
4a501606 39#ifndef CONFIG_USER_ONLY
7c2cb42b 40
37785977 41static bool get_phys_addr_lpae(CPUARMState *env, target_ulong address,
03ae85f8 42 MMUAccessType access_type, ARMMMUIdx mmu_idx,
37785977 43 hwaddr *phys_ptr, MemTxAttrs *txattrs, int *prot,
da909b2c 44 target_ulong *page_size_ptr,
5b2d261d 45 ARMMMUFaultInfo *fi, ARMCacheAttrs *cacheattrs);
4a501606
PM
46#endif
47
affdb64d
PM
48static void switch_mode(CPUARMState *env, int mode);
49
0ecb72a5 50static int vfp_gdb_get_reg(CPUARMState *env, uint8_t *buf, int reg)
56aebc89
PB
51{
52 int nregs;
53
54 /* VFP data registers are always little-endian. */
55 nregs = arm_feature(env, ARM_FEATURE_VFP3) ? 32 : 16;
56 if (reg < nregs) {
9a2b5256 57 stq_le_p(buf, *aa32_vfp_dreg(env, reg));
56aebc89
PB
58 return 8;
59 }
60 if (arm_feature(env, ARM_FEATURE_NEON)) {
61 /* Aliases for Q regs. */
62 nregs += 16;
63 if (reg < nregs) {
9a2b5256
RH
64 uint64_t *q = aa32_vfp_qreg(env, reg - 32);
65 stq_le_p(buf, q[0]);
66 stq_le_p(buf + 8, q[1]);
56aebc89
PB
67 return 16;
68 }
69 }
70 switch (reg - nregs) {
71 case 0: stl_p(buf, env->vfp.xregs[ARM_VFP_FPSID]); return 4;
b0a909a4 72 case 1: stl_p(buf, vfp_get_fpscr(env)); return 4;
56aebc89
PB
73 case 2: stl_p(buf, env->vfp.xregs[ARM_VFP_FPEXC]); return 4;
74 }
75 return 0;
76}
77
0ecb72a5 78static int vfp_gdb_set_reg(CPUARMState *env, uint8_t *buf, int reg)
56aebc89
PB
79{
80 int nregs;
81
82 nregs = arm_feature(env, ARM_FEATURE_VFP3) ? 32 : 16;
83 if (reg < nregs) {
9a2b5256 84 *aa32_vfp_dreg(env, reg) = ldq_le_p(buf);
56aebc89
PB
85 return 8;
86 }
87 if (arm_feature(env, ARM_FEATURE_NEON)) {
88 nregs += 16;
89 if (reg < nregs) {
9a2b5256
RH
90 uint64_t *q = aa32_vfp_qreg(env, reg - 32);
91 q[0] = ldq_le_p(buf);
92 q[1] = ldq_le_p(buf + 8);
56aebc89
PB
93 return 16;
94 }
95 }
96 switch (reg - nregs) {
97 case 0: env->vfp.xregs[ARM_VFP_FPSID] = ldl_p(buf); return 4;
b0a909a4 98 case 1: vfp_set_fpscr(env, ldl_p(buf)); return 4;
71b3c3de 99 case 2: env->vfp.xregs[ARM_VFP_FPEXC] = ldl_p(buf) & (1 << 30); return 4;
56aebc89
PB
100 }
101 return 0;
102}
103
6a669427
PM
104static int aarch64_fpu_gdb_get_reg(CPUARMState *env, uint8_t *buf, int reg)
105{
106 switch (reg) {
107 case 0 ... 31:
108 /* 128 bit FP register */
9a2b5256
RH
109 {
110 uint64_t *q = aa64_vfp_qreg(env, reg);
111 stq_le_p(buf, q[0]);
112 stq_le_p(buf + 8, q[1]);
113 return 16;
114 }
6a669427
PM
115 case 32:
116 /* FPSR */
117 stl_p(buf, vfp_get_fpsr(env));
118 return 4;
119 case 33:
120 /* FPCR */
121 stl_p(buf, vfp_get_fpcr(env));
122 return 4;
123 default:
124 return 0;
125 }
126}
127
128static int aarch64_fpu_gdb_set_reg(CPUARMState *env, uint8_t *buf, int reg)
129{
130 switch (reg) {
131 case 0 ... 31:
132 /* 128 bit FP register */
9a2b5256
RH
133 {
134 uint64_t *q = aa64_vfp_qreg(env, reg);
135 q[0] = ldq_le_p(buf);
136 q[1] = ldq_le_p(buf + 8);
137 return 16;
138 }
6a669427
PM
139 case 32:
140 /* FPSR */
141 vfp_set_fpsr(env, ldl_p(buf));
142 return 4;
143 case 33:
144 /* FPCR */
145 vfp_set_fpcr(env, ldl_p(buf));
146 return 4;
147 default:
148 return 0;
149 }
150}
151
c4241c7d 152static uint64_t raw_read(CPUARMState *env, const ARMCPRegInfo *ri)
d4e6df63 153{
375421cc 154 assert(ri->fieldoffset);
67ed771d 155 if (cpreg_field_is_64bit(ri)) {
c4241c7d 156 return CPREG_FIELD64(env, ri);
22d9e1a9 157 } else {
c4241c7d 158 return CPREG_FIELD32(env, ri);
22d9e1a9 159 }
d4e6df63
PM
160}
161
c4241c7d
PM
162static void raw_write(CPUARMState *env, const ARMCPRegInfo *ri,
163 uint64_t value)
d4e6df63 164{
375421cc 165 assert(ri->fieldoffset);
67ed771d 166 if (cpreg_field_is_64bit(ri)) {
22d9e1a9
PM
167 CPREG_FIELD64(env, ri) = value;
168 } else {
169 CPREG_FIELD32(env, ri) = value;
170 }
d4e6df63
PM
171}
172
11f136ee
FA
173static void *raw_ptr(CPUARMState *env, const ARMCPRegInfo *ri)
174{
175 return (char *)env + ri->fieldoffset;
176}
177
49a66191 178uint64_t read_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri)
721fae12 179{
59a1c327 180 /* Raw read of a coprocessor register (as needed for migration, etc). */
721fae12 181 if (ri->type & ARM_CP_CONST) {
59a1c327 182 return ri->resetvalue;
721fae12 183 } else if (ri->raw_readfn) {
59a1c327 184 return ri->raw_readfn(env, ri);
721fae12 185 } else if (ri->readfn) {
59a1c327 186 return ri->readfn(env, ri);
721fae12 187 } else {
59a1c327 188 return raw_read(env, ri);
721fae12 189 }
721fae12
PM
190}
191
59a1c327 192static void write_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri,
7900e9f1 193 uint64_t v)
721fae12
PM
194{
195 /* Raw write of a coprocessor register (as needed for migration, etc).
721fae12
PM
196 * Note that constant registers are treated as write-ignored; the
197 * caller should check for success by whether a readback gives the
198 * value written.
199 */
200 if (ri->type & ARM_CP_CONST) {
59a1c327 201 return;
721fae12 202 } else if (ri->raw_writefn) {
c4241c7d 203 ri->raw_writefn(env, ri, v);
721fae12 204 } else if (ri->writefn) {
c4241c7d 205 ri->writefn(env, ri, v);
721fae12 206 } else {
afb2530f 207 raw_write(env, ri, v);
721fae12 208 }
721fae12
PM
209}
210
200bf5b7
AB
211static int arm_gdb_get_sysreg(CPUARMState *env, uint8_t *buf, int reg)
212{
2fc0cc0e 213 ARMCPU *cpu = env_archcpu(env);
200bf5b7
AB
214 const ARMCPRegInfo *ri;
215 uint32_t key;
216
217 key = cpu->dyn_xml.cpregs_keys[reg];
218 ri = get_arm_cp_reginfo(cpu->cp_regs, key);
219 if (ri) {
220 if (cpreg_field_is_64bit(ri)) {
221 return gdb_get_reg64(buf, (uint64_t)read_raw_cp_reg(env, ri));
222 } else {
223 return gdb_get_reg32(buf, (uint32_t)read_raw_cp_reg(env, ri));
224 }
225 }
226 return 0;
227}
228
229static int arm_gdb_set_sysreg(CPUARMState *env, uint8_t *buf, int reg)
230{
231 return 0;
232}
233
375421cc
PM
234static bool raw_accessors_invalid(const ARMCPRegInfo *ri)
235{
236 /* Return true if the regdef would cause an assertion if you called
237 * read_raw_cp_reg() or write_raw_cp_reg() on it (ie if it is a
238 * program bug for it not to have the NO_RAW flag).
239 * NB that returning false here doesn't necessarily mean that calling
240 * read/write_raw_cp_reg() is safe, because we can't distinguish "has
241 * read/write access functions which are safe for raw use" from "has
242 * read/write access functions which have side effects but has forgotten
243 * to provide raw access functions".
244 * The tests here line up with the conditions in read/write_raw_cp_reg()
245 * and assertions in raw_read()/raw_write().
246 */
247 if ((ri->type & ARM_CP_CONST) ||
248 ri->fieldoffset ||
249 ((ri->raw_writefn || ri->writefn) && (ri->raw_readfn || ri->readfn))) {
250 return false;
251 }
252 return true;
253}
254
b698e4ee 255bool write_cpustate_to_list(ARMCPU *cpu, bool kvm_sync)
721fae12
PM
256{
257 /* Write the coprocessor state from cpu->env to the (index,value) list. */
258 int i;
259 bool ok = true;
260
261 for (i = 0; i < cpu->cpreg_array_len; i++) {
262 uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
263 const ARMCPRegInfo *ri;
b698e4ee 264 uint64_t newval;
59a1c327 265
60322b39 266 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
721fae12
PM
267 if (!ri) {
268 ok = false;
269 continue;
270 }
7a0e58fa 271 if (ri->type & ARM_CP_NO_RAW) {
721fae12
PM
272 continue;
273 }
b698e4ee
PM
274
275 newval = read_raw_cp_reg(&cpu->env, ri);
276 if (kvm_sync) {
277 /*
278 * Only sync if the previous list->cpustate sync succeeded.
279 * Rather than tracking the success/failure state for every
280 * item in the list, we just recheck "does the raw write we must
281 * have made in write_list_to_cpustate() read back OK" here.
282 */
283 uint64_t oldval = cpu->cpreg_values[i];
284
285 if (oldval == newval) {
286 continue;
287 }
288
289 write_raw_cp_reg(&cpu->env, ri, oldval);
290 if (read_raw_cp_reg(&cpu->env, ri) != oldval) {
291 continue;
292 }
293
294 write_raw_cp_reg(&cpu->env, ri, newval);
295 }
296 cpu->cpreg_values[i] = newval;
721fae12
PM
297 }
298 return ok;
299}
300
301bool write_list_to_cpustate(ARMCPU *cpu)
302{
303 int i;
304 bool ok = true;
305
306 for (i = 0; i < cpu->cpreg_array_len; i++) {
307 uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
308 uint64_t v = cpu->cpreg_values[i];
721fae12
PM
309 const ARMCPRegInfo *ri;
310
60322b39 311 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
721fae12
PM
312 if (!ri) {
313 ok = false;
314 continue;
315 }
7a0e58fa 316 if (ri->type & ARM_CP_NO_RAW) {
721fae12
PM
317 continue;
318 }
319 /* Write value and confirm it reads back as written
320 * (to catch read-only registers and partially read-only
321 * registers where the incoming migration value doesn't match)
322 */
59a1c327
PM
323 write_raw_cp_reg(&cpu->env, ri, v);
324 if (read_raw_cp_reg(&cpu->env, ri) != v) {
721fae12
PM
325 ok = false;
326 }
327 }
328 return ok;
329}
330
331static void add_cpreg_to_list(gpointer key, gpointer opaque)
332{
333 ARMCPU *cpu = opaque;
334 uint64_t regidx;
335 const ARMCPRegInfo *ri;
336
337 regidx = *(uint32_t *)key;
60322b39 338 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
721fae12 339
7a0e58fa 340 if (!(ri->type & (ARM_CP_NO_RAW|ARM_CP_ALIAS))) {
721fae12
PM
341 cpu->cpreg_indexes[cpu->cpreg_array_len] = cpreg_to_kvm_id(regidx);
342 /* The value array need not be initialized at this point */
343 cpu->cpreg_array_len++;
344 }
345}
346
347static void count_cpreg(gpointer key, gpointer opaque)
348{
349 ARMCPU *cpu = opaque;
350 uint64_t regidx;
351 const ARMCPRegInfo *ri;
352
353 regidx = *(uint32_t *)key;
60322b39 354 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
721fae12 355
7a0e58fa 356 if (!(ri->type & (ARM_CP_NO_RAW|ARM_CP_ALIAS))) {
721fae12
PM
357 cpu->cpreg_array_len++;
358 }
359}
360
361static gint cpreg_key_compare(gconstpointer a, gconstpointer b)
362{
cbf239b7
AR
363 uint64_t aidx = cpreg_to_kvm_id(*(uint32_t *)a);
364 uint64_t bidx = cpreg_to_kvm_id(*(uint32_t *)b);
721fae12 365
cbf239b7
AR
366 if (aidx > bidx) {
367 return 1;
368 }
369 if (aidx < bidx) {
370 return -1;
371 }
372 return 0;
721fae12
PM
373}
374
375void init_cpreg_list(ARMCPU *cpu)
376{
377 /* Initialise the cpreg_tuples[] array based on the cp_regs hash.
378 * Note that we require cpreg_tuples[] to be sorted by key ID.
379 */
57b6d95e 380 GList *keys;
721fae12
PM
381 int arraylen;
382
57b6d95e 383 keys = g_hash_table_get_keys(cpu->cp_regs);
721fae12
PM
384 keys = g_list_sort(keys, cpreg_key_compare);
385
386 cpu->cpreg_array_len = 0;
387
388 g_list_foreach(keys, count_cpreg, cpu);
389
390 arraylen = cpu->cpreg_array_len;
391 cpu->cpreg_indexes = g_new(uint64_t, arraylen);
392 cpu->cpreg_values = g_new(uint64_t, arraylen);
393 cpu->cpreg_vmstate_indexes = g_new(uint64_t, arraylen);
394 cpu->cpreg_vmstate_values = g_new(uint64_t, arraylen);
395 cpu->cpreg_vmstate_array_len = cpu->cpreg_array_len;
396 cpu->cpreg_array_len = 0;
397
398 g_list_foreach(keys, add_cpreg_to_list, cpu);
399
400 assert(cpu->cpreg_array_len == arraylen);
401
402 g_list_free(keys);
403}
404
68e9c2fe
EI
405/*
406 * Some registers are not accessible if EL3.NS=0 and EL3 is using AArch32 but
407 * they are accessible when EL3 is using AArch64 regardless of EL3.NS.
408 *
409 * access_el3_aa32ns: Used to check AArch32 register views.
410 * access_el3_aa32ns_aa64any: Used to check both AArch32/64 register views.
411 */
412static CPAccessResult access_el3_aa32ns(CPUARMState *env,
3f208fd7
PM
413 const ARMCPRegInfo *ri,
414 bool isread)
68e9c2fe
EI
415{
416 bool secure = arm_is_secure_below_el3(env);
417
418 assert(!arm_el_is_aa64(env, 3));
419 if (secure) {
420 return CP_ACCESS_TRAP_UNCATEGORIZED;
421 }
422 return CP_ACCESS_OK;
423}
424
425static CPAccessResult access_el3_aa32ns_aa64any(CPUARMState *env,
3f208fd7
PM
426 const ARMCPRegInfo *ri,
427 bool isread)
68e9c2fe
EI
428{
429 if (!arm_el_is_aa64(env, 3)) {
3f208fd7 430 return access_el3_aa32ns(env, ri, isread);
68e9c2fe
EI
431 }
432 return CP_ACCESS_OK;
433}
434
5513c3ab
PM
435/* Some secure-only AArch32 registers trap to EL3 if used from
436 * Secure EL1 (but are just ordinary UNDEF in other non-EL3 contexts).
437 * Note that an access from Secure EL1 can only happen if EL3 is AArch64.
438 * We assume that the .access field is set to PL1_RW.
439 */
440static CPAccessResult access_trap_aa32s_el1(CPUARMState *env,
3f208fd7
PM
441 const ARMCPRegInfo *ri,
442 bool isread)
5513c3ab
PM
443{
444 if (arm_current_el(env) == 3) {
445 return CP_ACCESS_OK;
446 }
447 if (arm_is_secure_below_el3(env)) {
448 return CP_ACCESS_TRAP_EL3;
449 }
450 /* This will be EL1 NS and EL2 NS, which just UNDEF */
451 return CP_ACCESS_TRAP_UNCATEGORIZED;
452}
453
187f678d
PM
454/* Check for traps to "powerdown debug" registers, which are controlled
455 * by MDCR.TDOSA
456 */
457static CPAccessResult access_tdosa(CPUARMState *env, const ARMCPRegInfo *ri,
458 bool isread)
459{
460 int el = arm_current_el(env);
30ac6339
PM
461 bool mdcr_el2_tdosa = (env->cp15.mdcr_el2 & MDCR_TDOSA) ||
462 (env->cp15.mdcr_el2 & MDCR_TDE) ||
7c208e0f 463 (arm_hcr_el2_eff(env) & HCR_TGE);
187f678d 464
30ac6339 465 if (el < 2 && mdcr_el2_tdosa && !arm_is_secure_below_el3(env)) {
187f678d
PM
466 return CP_ACCESS_TRAP_EL2;
467 }
468 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TDOSA)) {
469 return CP_ACCESS_TRAP_EL3;
470 }
471 return CP_ACCESS_OK;
472}
473
91b0a238
PM
474/* Check for traps to "debug ROM" registers, which are controlled
475 * by MDCR_EL2.TDRA for EL2 but by the more general MDCR_EL3.TDA for EL3.
476 */
477static CPAccessResult access_tdra(CPUARMState *env, const ARMCPRegInfo *ri,
478 bool isread)
479{
480 int el = arm_current_el(env);
30ac6339
PM
481 bool mdcr_el2_tdra = (env->cp15.mdcr_el2 & MDCR_TDRA) ||
482 (env->cp15.mdcr_el2 & MDCR_TDE) ||
7c208e0f 483 (arm_hcr_el2_eff(env) & HCR_TGE);
91b0a238 484
30ac6339 485 if (el < 2 && mdcr_el2_tdra && !arm_is_secure_below_el3(env)) {
91b0a238
PM
486 return CP_ACCESS_TRAP_EL2;
487 }
488 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TDA)) {
489 return CP_ACCESS_TRAP_EL3;
490 }
491 return CP_ACCESS_OK;
492}
493
d6c8cf81
PM
494/* Check for traps to general debug registers, which are controlled
495 * by MDCR_EL2.TDA for EL2 and MDCR_EL3.TDA for EL3.
496 */
497static CPAccessResult access_tda(CPUARMState *env, const ARMCPRegInfo *ri,
498 bool isread)
499{
500 int el = arm_current_el(env);
30ac6339
PM
501 bool mdcr_el2_tda = (env->cp15.mdcr_el2 & MDCR_TDA) ||
502 (env->cp15.mdcr_el2 & MDCR_TDE) ||
7c208e0f 503 (arm_hcr_el2_eff(env) & HCR_TGE);
d6c8cf81 504
30ac6339 505 if (el < 2 && mdcr_el2_tda && !arm_is_secure_below_el3(env)) {
d6c8cf81
PM
506 return CP_ACCESS_TRAP_EL2;
507 }
508 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TDA)) {
509 return CP_ACCESS_TRAP_EL3;
510 }
511 return CP_ACCESS_OK;
512}
513
1fce1ba9
PM
514/* Check for traps to performance monitor registers, which are controlled
515 * by MDCR_EL2.TPM for EL2 and MDCR_EL3.TPM for EL3.
516 */
517static CPAccessResult access_tpm(CPUARMState *env, const ARMCPRegInfo *ri,
518 bool isread)
519{
520 int el = arm_current_el(env);
521
522 if (el < 2 && (env->cp15.mdcr_el2 & MDCR_TPM)
523 && !arm_is_secure_below_el3(env)) {
524 return CP_ACCESS_TRAP_EL2;
525 }
526 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) {
527 return CP_ACCESS_TRAP_EL3;
528 }
529 return CP_ACCESS_OK;
530}
531
c4241c7d 532static void dacr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
c983fe6c 533{
2fc0cc0e 534 ARMCPU *cpu = env_archcpu(env);
00c8cb0a 535
8d5c773e 536 raw_write(env, ri, value);
d10eb08f 537 tlb_flush(CPU(cpu)); /* Flush TLB as domain not tracked in TLB */
c983fe6c
PM
538}
539
c4241c7d 540static void fcse_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
08de207b 541{
2fc0cc0e 542 ARMCPU *cpu = env_archcpu(env);
00c8cb0a 543
8d5c773e 544 if (raw_read(env, ri) != value) {
08de207b
PM
545 /* Unlike real hardware the qemu TLB uses virtual addresses,
546 * not modified virtual addresses, so this causes a TLB flush.
547 */
d10eb08f 548 tlb_flush(CPU(cpu));
8d5c773e 549 raw_write(env, ri, value);
08de207b 550 }
08de207b 551}
c4241c7d
PM
552
553static void contextidr_write(CPUARMState *env, const ARMCPRegInfo *ri,
554 uint64_t value)
08de207b 555{
2fc0cc0e 556 ARMCPU *cpu = env_archcpu(env);
00c8cb0a 557
452a0955 558 if (raw_read(env, ri) != value && !arm_feature(env, ARM_FEATURE_PMSA)
014406b5 559 && !extended_addresses_enabled(env)) {
08de207b
PM
560 /* For VMSA (when not using the LPAE long descriptor page table
561 * format) this register includes the ASID, so do a TLB flush.
562 * For PMSA it is purely a process ID and no action is needed.
563 */
d10eb08f 564 tlb_flush(CPU(cpu));
08de207b 565 }
8d5c773e 566 raw_write(env, ri, value);
08de207b
PM
567}
568
b4ab8ce9
PM
569/* IS variants of TLB operations must affect all cores */
570static void tlbiall_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
571 uint64_t value)
572{
29a0af61 573 CPUState *cs = env_cpu(env);
b4ab8ce9
PM
574
575 tlb_flush_all_cpus_synced(cs);
576}
577
578static void tlbiasid_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
579 uint64_t value)
580{
29a0af61 581 CPUState *cs = env_cpu(env);
b4ab8ce9
PM
582
583 tlb_flush_all_cpus_synced(cs);
584}
585
586static void tlbimva_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
587 uint64_t value)
588{
29a0af61 589 CPUState *cs = env_cpu(env);
b4ab8ce9
PM
590
591 tlb_flush_page_all_cpus_synced(cs, value & TARGET_PAGE_MASK);
592}
593
594static void tlbimvaa_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
595 uint64_t value)
596{
29a0af61 597 CPUState *cs = env_cpu(env);
b4ab8ce9
PM
598
599 tlb_flush_page_all_cpus_synced(cs, value & TARGET_PAGE_MASK);
600}
601
602/*
603 * Non-IS variants of TLB operations are upgraded to
604 * IS versions if we are at NS EL1 and HCR_EL2.FB is set to
605 * force broadcast of these operations.
606 */
607static bool tlb_force_broadcast(CPUARMState *env)
608{
609 return (env->cp15.hcr_el2 & HCR_FB) &&
610 arm_current_el(env) == 1 && arm_is_secure_below_el3(env);
611}
612
c4241c7d
PM
613static void tlbiall_write(CPUARMState *env, const ARMCPRegInfo *ri,
614 uint64_t value)
d929823f
PM
615{
616 /* Invalidate all (TLBIALL) */
527db2be 617 CPUState *cs = env_cpu(env);
00c8cb0a 618
b4ab8ce9 619 if (tlb_force_broadcast(env)) {
527db2be
RH
620 tlb_flush_all_cpus_synced(cs);
621 } else {
622 tlb_flush(cs);
b4ab8ce9 623 }
d929823f
PM
624}
625
c4241c7d
PM
626static void tlbimva_write(CPUARMState *env, const ARMCPRegInfo *ri,
627 uint64_t value)
d929823f
PM
628{
629 /* Invalidate single TLB entry by MVA and ASID (TLBIMVA) */
527db2be 630 CPUState *cs = env_cpu(env);
31b030d4 631
527db2be 632 value &= TARGET_PAGE_MASK;
b4ab8ce9 633 if (tlb_force_broadcast(env)) {
527db2be
RH
634 tlb_flush_page_all_cpus_synced(cs, value);
635 } else {
636 tlb_flush_page(cs, value);
b4ab8ce9 637 }
d929823f
PM
638}
639
c4241c7d
PM
640static void tlbiasid_write(CPUARMState *env, const ARMCPRegInfo *ri,
641 uint64_t value)
d929823f
PM
642{
643 /* Invalidate by ASID (TLBIASID) */
527db2be 644 CPUState *cs = env_cpu(env);
00c8cb0a 645
b4ab8ce9 646 if (tlb_force_broadcast(env)) {
527db2be
RH
647 tlb_flush_all_cpus_synced(cs);
648 } else {
649 tlb_flush(cs);
b4ab8ce9 650 }
d929823f
PM
651}
652
c4241c7d
PM
653static void tlbimvaa_write(CPUARMState *env, const ARMCPRegInfo *ri,
654 uint64_t value)
d929823f
PM
655{
656 /* Invalidate single entry by MVA, all ASIDs (TLBIMVAA) */
527db2be 657 CPUState *cs = env_cpu(env);
31b030d4 658
527db2be 659 value &= TARGET_PAGE_MASK;
b4ab8ce9 660 if (tlb_force_broadcast(env)) {
527db2be
RH
661 tlb_flush_page_all_cpus_synced(cs, value);
662 } else {
663 tlb_flush_page(cs, value);
b4ab8ce9 664 }
fa439fc5
PM
665}
666
541ef8c2
SS
667static void tlbiall_nsnh_write(CPUARMState *env, const ARMCPRegInfo *ri,
668 uint64_t value)
669{
29a0af61 670 CPUState *cs = env_cpu(env);
541ef8c2 671
0336cbf8 672 tlb_flush_by_mmuidx(cs,
01b98b68
RH
673 ARMMMUIdxBit_E10_1 |
674 ARMMMUIdxBit_E10_0 |
97fa9350 675 ARMMMUIdxBit_Stage2);
541ef8c2
SS
676}
677
678static void tlbiall_nsnh_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
679 uint64_t value)
680{
29a0af61 681 CPUState *cs = env_cpu(env);
541ef8c2 682
a67cf277 683 tlb_flush_by_mmuidx_all_cpus_synced(cs,
01b98b68
RH
684 ARMMMUIdxBit_E10_1 |
685 ARMMMUIdxBit_E10_0 |
97fa9350 686 ARMMMUIdxBit_Stage2);
541ef8c2
SS
687}
688
689static void tlbiipas2_write(CPUARMState *env, const ARMCPRegInfo *ri,
690 uint64_t value)
691{
692 /* Invalidate by IPA. This has to invalidate any structures that
693 * contain only stage 2 translation information, but does not need
694 * to apply to structures that contain combined stage 1 and stage 2
695 * translation information.
696 * This must NOP if EL2 isn't implemented or SCR_EL3.NS is zero.
697 */
29a0af61 698 CPUState *cs = env_cpu(env);
541ef8c2
SS
699 uint64_t pageaddr;
700
701 if (!arm_feature(env, ARM_FEATURE_EL2) || !(env->cp15.scr_el3 & SCR_NS)) {
702 return;
703 }
704
705 pageaddr = sextract64(value << 12, 0, 40);
706
97fa9350 707 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_Stage2);
541ef8c2
SS
708}
709
710static void tlbiipas2_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
711 uint64_t value)
712{
29a0af61 713 CPUState *cs = env_cpu(env);
541ef8c2
SS
714 uint64_t pageaddr;
715
716 if (!arm_feature(env, ARM_FEATURE_EL2) || !(env->cp15.scr_el3 & SCR_NS)) {
717 return;
718 }
719
720 pageaddr = sextract64(value << 12, 0, 40);
721
a67cf277 722 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
97fa9350 723 ARMMMUIdxBit_Stage2);
541ef8c2
SS
724}
725
726static void tlbiall_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
727 uint64_t value)
728{
29a0af61 729 CPUState *cs = env_cpu(env);
541ef8c2 730
e013b741 731 tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_E2);
541ef8c2
SS
732}
733
734static void tlbiall_hyp_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
735 uint64_t value)
736{
29a0af61 737 CPUState *cs = env_cpu(env);
541ef8c2 738
e013b741 739 tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_E2);
541ef8c2
SS
740}
741
742static void tlbimva_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
743 uint64_t value)
744{
29a0af61 745 CPUState *cs = env_cpu(env);
541ef8c2
SS
746 uint64_t pageaddr = value & ~MAKE_64BIT_MASK(0, 12);
747
e013b741 748 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_E2);
541ef8c2
SS
749}
750
751static void tlbimva_hyp_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
752 uint64_t value)
753{
29a0af61 754 CPUState *cs = env_cpu(env);
541ef8c2
SS
755 uint64_t pageaddr = value & ~MAKE_64BIT_MASK(0, 12);
756
a67cf277 757 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
e013b741 758 ARMMMUIdxBit_E2);
541ef8c2
SS
759}
760
e9aa6c21 761static const ARMCPRegInfo cp_reginfo[] = {
54bf36ed
FA
762 /* Define the secure and non-secure FCSE identifier CP registers
763 * separately because there is no secure bank in V8 (no _EL3). This allows
764 * the secure register to be properly reset and migrated. There is also no
765 * v8 EL1 version of the register so the non-secure instance stands alone.
766 */
9c513e78 767 { .name = "FCSEIDR",
54bf36ed
FA
768 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
769 .access = PL1_RW, .secure = ARM_CP_SECSTATE_NS,
770 .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_ns),
771 .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
9c513e78 772 { .name = "FCSEIDR_S",
54bf36ed
FA
773 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
774 .access = PL1_RW, .secure = ARM_CP_SECSTATE_S,
775 .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_s),
d4e6df63 776 .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
54bf36ed
FA
777 /* Define the secure and non-secure context identifier CP registers
778 * separately because there is no secure bank in V8 (no _EL3). This allows
779 * the secure register to be properly reset and migrated. In the
780 * non-secure case, the 32-bit register will have reset and migration
781 * disabled during registration as it is handled by the 64-bit instance.
782 */
783 { .name = "CONTEXTIDR_EL1", .state = ARM_CP_STATE_BOTH,
014406b5 784 .opc0 = 3, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
54bf36ed
FA
785 .access = PL1_RW, .secure = ARM_CP_SECSTATE_NS,
786 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[1]),
787 .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
9c513e78 788 { .name = "CONTEXTIDR_S", .state = ARM_CP_STATE_AA32,
54bf36ed
FA
789 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
790 .access = PL1_RW, .secure = ARM_CP_SECSTATE_S,
791 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_s),
d4e6df63 792 .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
9449fdf6
PM
793 REGINFO_SENTINEL
794};
795
796static const ARMCPRegInfo not_v8_cp_reginfo[] = {
797 /* NB: Some of these registers exist in v8 but with more precise
798 * definitions that don't use CP_ANY wildcards (mostly in v8_cp_reginfo[]).
799 */
800 /* MMU Domain access control / MPU write buffer control */
0c17d68c
FA
801 { .name = "DACR",
802 .cp = 15, .opc1 = CP_ANY, .crn = 3, .crm = CP_ANY, .opc2 = CP_ANY,
803 .access = PL1_RW, .resetvalue = 0,
804 .writefn = dacr_write, .raw_writefn = raw_write,
805 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
806 offsetoflow32(CPUARMState, cp15.dacr_ns) } },
a903c449
EI
807 /* ARMv7 allocates a range of implementation defined TLB LOCKDOWN regs.
808 * For v6 and v5, these mappings are overly broad.
4fdd17dd 809 */
a903c449
EI
810 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 0,
811 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
812 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 1,
813 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
814 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 4,
815 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
816 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 8,
4fdd17dd 817 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
c4804214
PM
818 /* Cache maintenance ops; some of this space may be overridden later. */
819 { .name = "CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
820 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
821 .type = ARM_CP_NOP | ARM_CP_OVERRIDE },
e9aa6c21
PM
822 REGINFO_SENTINEL
823};
824
7d57f408
PM
825static const ARMCPRegInfo not_v6_cp_reginfo[] = {
826 /* Not all pre-v6 cores implemented this WFI, so this is slightly
827 * over-broad.
828 */
829 { .name = "WFI_v5", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = 2,
830 .access = PL1_W, .type = ARM_CP_WFI },
831 REGINFO_SENTINEL
832};
833
834static const ARMCPRegInfo not_v7_cp_reginfo[] = {
835 /* Standard v6 WFI (also used in some pre-v6 cores); not in v7 (which
836 * is UNPREDICTABLE; we choose to NOP as most implementations do).
837 */
838 { .name = "WFI_v6", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
839 .access = PL1_W, .type = ARM_CP_WFI },
34f90529
PM
840 /* L1 cache lockdown. Not architectural in v6 and earlier but in practice
841 * implemented in 926, 946, 1026, 1136, 1176 and 11MPCore. StrongARM and
842 * OMAPCP will override this space.
843 */
844 { .name = "DLOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 0,
845 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_data),
846 .resetvalue = 0 },
847 { .name = "ILOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 1,
848 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_insn),
849 .resetvalue = 0 },
776d4e5c
PM
850 /* v6 doesn't have the cache ID registers but Linux reads them anyway */
851 { .name = "DUMMY", .cp = 15, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = CP_ANY,
7a0e58fa 852 .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 853 .resetvalue = 0 },
50300698
PM
854 /* We don't implement pre-v7 debug but most CPUs had at least a DBGDIDR;
855 * implementing it as RAZ means the "debug architecture version" bits
856 * will read as a reserved value, which should cause Linux to not try
857 * to use the debug hardware.
858 */
859 { .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0,
860 .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
995939a6
PM
861 /* MMU TLB control. Note that the wildcarding means we cover not just
862 * the unified TLB ops but also the dside/iside/inner-shareable variants.
863 */
864 { .name = "TLBIALL", .cp = 15, .crn = 8, .crm = CP_ANY,
865 .opc1 = CP_ANY, .opc2 = 0, .access = PL1_W, .writefn = tlbiall_write,
7a0e58fa 866 .type = ARM_CP_NO_RAW },
995939a6
PM
867 { .name = "TLBIMVA", .cp = 15, .crn = 8, .crm = CP_ANY,
868 .opc1 = CP_ANY, .opc2 = 1, .access = PL1_W, .writefn = tlbimva_write,
7a0e58fa 869 .type = ARM_CP_NO_RAW },
995939a6
PM
870 { .name = "TLBIASID", .cp = 15, .crn = 8, .crm = CP_ANY,
871 .opc1 = CP_ANY, .opc2 = 2, .access = PL1_W, .writefn = tlbiasid_write,
7a0e58fa 872 .type = ARM_CP_NO_RAW },
995939a6
PM
873 { .name = "TLBIMVAA", .cp = 15, .crn = 8, .crm = CP_ANY,
874 .opc1 = CP_ANY, .opc2 = 3, .access = PL1_W, .writefn = tlbimvaa_write,
7a0e58fa 875 .type = ARM_CP_NO_RAW },
a903c449
EI
876 { .name = "PRRR", .cp = 15, .crn = 10, .crm = 2,
877 .opc1 = 0, .opc2 = 0, .access = PL1_RW, .type = ARM_CP_NOP },
878 { .name = "NMRR", .cp = 15, .crn = 10, .crm = 2,
879 .opc1 = 0, .opc2 = 1, .access = PL1_RW, .type = ARM_CP_NOP },
7d57f408
PM
880 REGINFO_SENTINEL
881};
882
c4241c7d
PM
883static void cpacr_write(CPUARMState *env, const ARMCPRegInfo *ri,
884 uint64_t value)
2771db27 885{
f0aff255
FA
886 uint32_t mask = 0;
887
888 /* In ARMv8 most bits of CPACR_EL1 are RES0. */
889 if (!arm_feature(env, ARM_FEATURE_V8)) {
890 /* ARMv7 defines bits for unimplemented coprocessors as RAZ/WI.
891 * ASEDIS [31] and D32DIS [30] are both UNK/SBZP without VFP.
892 * TRCDIS [28] is RAZ/WI since we do not implement a trace macrocell.
893 */
894 if (arm_feature(env, ARM_FEATURE_VFP)) {
895 /* VFP coprocessor: cp10 & cp11 [23:20] */
896 mask |= (1 << 31) | (1 << 30) | (0xf << 20);
897
898 if (!arm_feature(env, ARM_FEATURE_NEON)) {
899 /* ASEDIS [31] bit is RAO/WI */
900 value |= (1 << 31);
901 }
902
903 /* VFPv3 and upwards with NEON implement 32 double precision
904 * registers (D0-D31).
905 */
906 if (!arm_feature(env, ARM_FEATURE_NEON) ||
907 !arm_feature(env, ARM_FEATURE_VFP3)) {
908 /* D32DIS [30] is RAO/WI if D16-31 are not implemented. */
909 value |= (1 << 30);
910 }
911 }
912 value &= mask;
2771db27 913 }
fc1120a7
PM
914
915 /*
916 * For A-profile AArch32 EL3 (but not M-profile secure mode), if NSACR.CP10
917 * is 0 then CPACR.{CP11,CP10} ignore writes and read as 0b00.
918 */
919 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
920 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
921 value &= ~(0xf << 20);
922 value |= env->cp15.cpacr_el1 & (0xf << 20);
923 }
924
7ebd5f2e 925 env->cp15.cpacr_el1 = value;
2771db27
PM
926}
927
fc1120a7
PM
928static uint64_t cpacr_read(CPUARMState *env, const ARMCPRegInfo *ri)
929{
930 /*
931 * For A-profile AArch32 EL3 (but not M-profile secure mode), if NSACR.CP10
932 * is 0 then CPACR.{CP11,CP10} ignore writes and read as 0b00.
933 */
934 uint64_t value = env->cp15.cpacr_el1;
935
936 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
937 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
938 value &= ~(0xf << 20);
939 }
940 return value;
941}
942
943
5deac39c
PM
944static void cpacr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
945{
946 /* Call cpacr_write() so that we reset with the correct RAO bits set
947 * for our CPU features.
948 */
949 cpacr_write(env, ri, 0);
950}
951
3f208fd7
PM
952static CPAccessResult cpacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
953 bool isread)
c6f19164
GB
954{
955 if (arm_feature(env, ARM_FEATURE_V8)) {
956 /* Check if CPACR accesses are to be trapped to EL2 */
957 if (arm_current_el(env) == 1 &&
958 (env->cp15.cptr_el[2] & CPTR_TCPAC) && !arm_is_secure(env)) {
959 return CP_ACCESS_TRAP_EL2;
960 /* Check if CPACR accesses are to be trapped to EL3 */
961 } else if (arm_current_el(env) < 3 &&
962 (env->cp15.cptr_el[3] & CPTR_TCPAC)) {
963 return CP_ACCESS_TRAP_EL3;
964 }
965 }
966
967 return CP_ACCESS_OK;
968}
969
3f208fd7
PM
970static CPAccessResult cptr_access(CPUARMState *env, const ARMCPRegInfo *ri,
971 bool isread)
c6f19164
GB
972{
973 /* Check if CPTR accesses are set to trap to EL3 */
974 if (arm_current_el(env) == 2 && (env->cp15.cptr_el[3] & CPTR_TCPAC)) {
975 return CP_ACCESS_TRAP_EL3;
976 }
977
978 return CP_ACCESS_OK;
979}
980
7d57f408
PM
981static const ARMCPRegInfo v6_cp_reginfo[] = {
982 /* prefetch by MVA in v6, NOP in v7 */
983 { .name = "MVA_prefetch",
984 .cp = 15, .crn = 7, .crm = 13, .opc1 = 0, .opc2 = 1,
985 .access = PL1_W, .type = ARM_CP_NOP },
6df99dec
SS
986 /* We need to break the TB after ISB to execute self-modifying code
987 * correctly and also to take any pending interrupts immediately.
988 * So use arm_cp_write_ignore() function instead of ARM_CP_NOP flag.
989 */
7d57f408 990 { .name = "ISB", .cp = 15, .crn = 7, .crm = 5, .opc1 = 0, .opc2 = 4,
6df99dec 991 .access = PL0_W, .type = ARM_CP_NO_RAW, .writefn = arm_cp_write_ignore },
091fd17c 992 { .name = "DSB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 4,
7d57f408 993 .access = PL0_W, .type = ARM_CP_NOP },
091fd17c 994 { .name = "DMB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 5,
7d57f408 995 .access = PL0_W, .type = ARM_CP_NOP },
06d76f31 996 { .name = "IFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 2,
6cd8a264 997 .access = PL1_RW,
b848ce2b
FA
998 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ifar_s),
999 offsetof(CPUARMState, cp15.ifar_ns) },
06d76f31
PM
1000 .resetvalue = 0, },
1001 /* Watchpoint Fault Address Register : should actually only be present
1002 * for 1136, 1176, 11MPCore.
1003 */
1004 { .name = "WFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 1,
1005 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0, },
34222fb8 1006 { .name = "CPACR", .state = ARM_CP_STATE_BOTH, .opc0 = 3,
c6f19164 1007 .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 2, .accessfn = cpacr_access,
7ebd5f2e 1008 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.cpacr_el1),
fc1120a7 1009 .resetfn = cpacr_reset, .writefn = cpacr_write, .readfn = cpacr_read },
7d57f408
PM
1010 REGINFO_SENTINEL
1011};
1012
7ece99b1
AL
1013/* Definitions for the PMU registers */
1014#define PMCRN_MASK 0xf800
1015#define PMCRN_SHIFT 11
f4efb4b2 1016#define PMCRLC 0x40
033614c4 1017#define PMCRDP 0x10
7ece99b1
AL
1018#define PMCRD 0x8
1019#define PMCRC 0x4
5ecdd3e4 1020#define PMCRP 0x2
7ece99b1
AL
1021#define PMCRE 0x1
1022
033614c4
AL
1023#define PMXEVTYPER_P 0x80000000
1024#define PMXEVTYPER_U 0x40000000
1025#define PMXEVTYPER_NSK 0x20000000
1026#define PMXEVTYPER_NSU 0x10000000
1027#define PMXEVTYPER_NSH 0x08000000
1028#define PMXEVTYPER_M 0x04000000
1029#define PMXEVTYPER_MT 0x02000000
1030#define PMXEVTYPER_EVTCOUNT 0x0000ffff
1031#define PMXEVTYPER_MASK (PMXEVTYPER_P | PMXEVTYPER_U | PMXEVTYPER_NSK | \
1032 PMXEVTYPER_NSU | PMXEVTYPER_NSH | \
1033 PMXEVTYPER_M | PMXEVTYPER_MT | \
1034 PMXEVTYPER_EVTCOUNT)
1035
4b8afa1f
AL
1036#define PMCCFILTR 0xf8000000
1037#define PMCCFILTR_M PMXEVTYPER_M
1038#define PMCCFILTR_EL0 (PMCCFILTR | PMCCFILTR_M)
1039
7ece99b1
AL
1040static inline uint32_t pmu_num_counters(CPUARMState *env)
1041{
1042 return (env->cp15.c9_pmcr & PMCRN_MASK) >> PMCRN_SHIFT;
1043}
1044
1045/* Bits allowed to be set/cleared for PMCNTEN* and PMINTEN* */
1046static inline uint64_t pmu_counter_mask(CPUARMState *env)
1047{
1048 return (1 << 31) | ((1 << pmu_num_counters(env)) - 1);
1049}
1050
57a4a11b
AL
1051typedef struct pm_event {
1052 uint16_t number; /* PMEVTYPER.evtCount is 16 bits wide */
1053 /* If the event is supported on this CPU (used to generate PMCEID[01]) */
1054 bool (*supported)(CPUARMState *);
1055 /*
1056 * Retrieve the current count of the underlying event. The programmed
1057 * counters hold a difference from the return value from this function
1058 */
1059 uint64_t (*get_count)(CPUARMState *);
4e7beb0c
AL
1060 /*
1061 * Return how many nanoseconds it will take (at a minimum) for count events
1062 * to occur. A negative value indicates the counter will never overflow, or
1063 * that the counter has otherwise arranged for the overflow bit to be set
1064 * and the PMU interrupt to be raised on overflow.
1065 */
1066 int64_t (*ns_per_count)(uint64_t);
57a4a11b
AL
1067} pm_event;
1068
b2e23725
AL
1069static bool event_always_supported(CPUARMState *env)
1070{
1071 return true;
1072}
1073
0d4bfd7d
AL
1074static uint64_t swinc_get_count(CPUARMState *env)
1075{
1076 /*
1077 * SW_INCR events are written directly to the pmevcntr's by writes to
1078 * PMSWINC, so there is no underlying count maintained by the PMU itself
1079 */
1080 return 0;
1081}
1082
4e7beb0c
AL
1083static int64_t swinc_ns_per(uint64_t ignored)
1084{
1085 return -1;
1086}
1087
b2e23725
AL
1088/*
1089 * Return the underlying cycle count for the PMU cycle counters. If we're in
1090 * usermode, simply return 0.
1091 */
1092static uint64_t cycles_get_count(CPUARMState *env)
1093{
1094#ifndef CONFIG_USER_ONLY
1095 return muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
1096 ARM_CPU_FREQ, NANOSECONDS_PER_SECOND);
1097#else
1098 return cpu_get_host_ticks();
1099#endif
1100}
1101
1102#ifndef CONFIG_USER_ONLY
4e7beb0c
AL
1103static int64_t cycles_ns_per(uint64_t cycles)
1104{
1105 return (ARM_CPU_FREQ / NANOSECONDS_PER_SECOND) * cycles;
1106}
1107
b2e23725
AL
1108static bool instructions_supported(CPUARMState *env)
1109{
1110 return use_icount == 1 /* Precise instruction counting */;
1111}
1112
1113static uint64_t instructions_get_count(CPUARMState *env)
1114{
1115 return (uint64_t)cpu_get_icount_raw();
1116}
4e7beb0c
AL
1117
1118static int64_t instructions_ns_per(uint64_t icount)
1119{
1120 return cpu_icount_to_ns((int64_t)icount);
1121}
b2e23725
AL
1122#endif
1123
57a4a11b 1124static const pm_event pm_events[] = {
0d4bfd7d
AL
1125 { .number = 0x000, /* SW_INCR */
1126 .supported = event_always_supported,
1127 .get_count = swinc_get_count,
4e7beb0c 1128 .ns_per_count = swinc_ns_per,
0d4bfd7d 1129 },
b2e23725
AL
1130#ifndef CONFIG_USER_ONLY
1131 { .number = 0x008, /* INST_RETIRED, Instruction architecturally executed */
1132 .supported = instructions_supported,
1133 .get_count = instructions_get_count,
4e7beb0c 1134 .ns_per_count = instructions_ns_per,
b2e23725
AL
1135 },
1136 { .number = 0x011, /* CPU_CYCLES, Cycle */
1137 .supported = event_always_supported,
1138 .get_count = cycles_get_count,
4e7beb0c 1139 .ns_per_count = cycles_ns_per,
b2e23725
AL
1140 }
1141#endif
57a4a11b
AL
1142};
1143
1144/*
1145 * Note: Before increasing MAX_EVENT_ID beyond 0x3f into the 0x40xx range of
1146 * events (i.e. the statistical profiling extension), this implementation
1147 * should first be updated to something sparse instead of the current
1148 * supported_event_map[] array.
1149 */
b2e23725 1150#define MAX_EVENT_ID 0x11
57a4a11b
AL
1151#define UNSUPPORTED_EVENT UINT16_MAX
1152static uint16_t supported_event_map[MAX_EVENT_ID + 1];
1153
1154/*
bf8d0969
AL
1155 * Called upon CPU initialization to initialize PMCEID[01]_EL0 and build a map
1156 * of ARM event numbers to indices in our pm_events array.
57a4a11b
AL
1157 *
1158 * Note: Events in the 0x40XX range are not currently supported.
1159 */
bf8d0969 1160void pmu_init(ARMCPU *cpu)
57a4a11b 1161{
57a4a11b
AL
1162 unsigned int i;
1163
bf8d0969
AL
1164 /*
1165 * Empty supported_event_map and cpu->pmceid[01] before adding supported
1166 * events to them
1167 */
57a4a11b
AL
1168 for (i = 0; i < ARRAY_SIZE(supported_event_map); i++) {
1169 supported_event_map[i] = UNSUPPORTED_EVENT;
1170 }
bf8d0969
AL
1171 cpu->pmceid0 = 0;
1172 cpu->pmceid1 = 0;
57a4a11b
AL
1173
1174 for (i = 0; i < ARRAY_SIZE(pm_events); i++) {
1175 const pm_event *cnt = &pm_events[i];
1176 assert(cnt->number <= MAX_EVENT_ID);
1177 /* We do not currently support events in the 0x40xx range */
1178 assert(cnt->number <= 0x3f);
1179
bf8d0969 1180 if (cnt->supported(&cpu->env)) {
57a4a11b 1181 supported_event_map[cnt->number] = i;
67da43d6 1182 uint64_t event_mask = 1ULL << (cnt->number & 0x1f);
bf8d0969
AL
1183 if (cnt->number & 0x20) {
1184 cpu->pmceid1 |= event_mask;
1185 } else {
1186 cpu->pmceid0 |= event_mask;
1187 }
57a4a11b
AL
1188 }
1189 }
57a4a11b
AL
1190}
1191
5ecdd3e4
AL
1192/*
1193 * Check at runtime whether a PMU event is supported for the current machine
1194 */
1195static bool event_supported(uint16_t number)
1196{
1197 if (number > MAX_EVENT_ID) {
1198 return false;
1199 }
1200 return supported_event_map[number] != UNSUPPORTED_EVENT;
1201}
1202
3f208fd7
PM
1203static CPAccessResult pmreg_access(CPUARMState *env, const ARMCPRegInfo *ri,
1204 bool isread)
200ac0ef 1205{
3b163b01 1206 /* Performance monitor registers user accessibility is controlled
1fce1ba9
PM
1207 * by PMUSERENR. MDCR_EL2.TPM and MDCR_EL3.TPM allow configurable
1208 * trapping to EL2 or EL3 for other accesses.
200ac0ef 1209 */
1fce1ba9
PM
1210 int el = arm_current_el(env);
1211
6ecd0b6b 1212 if (el == 0 && !(env->cp15.c9_pmuserenr & 1)) {
fcd25206 1213 return CP_ACCESS_TRAP;
200ac0ef 1214 }
1fce1ba9
PM
1215 if (el < 2 && (env->cp15.mdcr_el2 & MDCR_TPM)
1216 && !arm_is_secure_below_el3(env)) {
1217 return CP_ACCESS_TRAP_EL2;
1218 }
1219 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) {
1220 return CP_ACCESS_TRAP_EL3;
1221 }
1222
fcd25206 1223 return CP_ACCESS_OK;
200ac0ef
PM
1224}
1225
6ecd0b6b
AB
1226static CPAccessResult pmreg_access_xevcntr(CPUARMState *env,
1227 const ARMCPRegInfo *ri,
1228 bool isread)
1229{
1230 /* ER: event counter read trap control */
1231 if (arm_feature(env, ARM_FEATURE_V8)
1232 && arm_current_el(env) == 0
1233 && (env->cp15.c9_pmuserenr & (1 << 3)) != 0
1234 && isread) {
1235 return CP_ACCESS_OK;
1236 }
1237
1238 return pmreg_access(env, ri, isread);
1239}
1240
1241static CPAccessResult pmreg_access_swinc(CPUARMState *env,
1242 const ARMCPRegInfo *ri,
1243 bool isread)
1244{
1245 /* SW: software increment write trap control */
1246 if (arm_feature(env, ARM_FEATURE_V8)
1247 && arm_current_el(env) == 0
1248 && (env->cp15.c9_pmuserenr & (1 << 1)) != 0
1249 && !isread) {
1250 return CP_ACCESS_OK;
1251 }
1252
1253 return pmreg_access(env, ri, isread);
1254}
1255
6ecd0b6b
AB
1256static CPAccessResult pmreg_access_selr(CPUARMState *env,
1257 const ARMCPRegInfo *ri,
1258 bool isread)
1259{
1260 /* ER: event counter read trap control */
1261 if (arm_feature(env, ARM_FEATURE_V8)
1262 && arm_current_el(env) == 0
1263 && (env->cp15.c9_pmuserenr & (1 << 3)) != 0) {
1264 return CP_ACCESS_OK;
1265 }
1266
1267 return pmreg_access(env, ri, isread);
1268}
1269
1270static CPAccessResult pmreg_access_ccntr(CPUARMState *env,
1271 const ARMCPRegInfo *ri,
1272 bool isread)
1273{
1274 /* CR: cycle counter read trap control */
1275 if (arm_feature(env, ARM_FEATURE_V8)
1276 && arm_current_el(env) == 0
1277 && (env->cp15.c9_pmuserenr & (1 << 2)) != 0
1278 && isread) {
1279 return CP_ACCESS_OK;
1280 }
1281
1282 return pmreg_access(env, ri, isread);
1283}
1284
033614c4
AL
1285/* Returns true if the counter (pass 31 for PMCCNTR) should count events using
1286 * the current EL, security state, and register configuration.
1287 */
1288static bool pmu_counter_enabled(CPUARMState *env, uint8_t counter)
87124fde 1289{
033614c4
AL
1290 uint64_t filter;
1291 bool e, p, u, nsk, nsu, nsh, m;
1292 bool enabled, prohibited, filtered;
1293 bool secure = arm_is_secure(env);
1294 int el = arm_current_el(env);
1295 uint8_t hpmn = env->cp15.mdcr_el2 & MDCR_HPMN;
87124fde 1296
cbbb3041
AJ
1297 if (!arm_feature(env, ARM_FEATURE_PMU)) {
1298 return false;
1299 }
1300
033614c4
AL
1301 if (!arm_feature(env, ARM_FEATURE_EL2) ||
1302 (counter < hpmn || counter == 31)) {
1303 e = env->cp15.c9_pmcr & PMCRE;
1304 } else {
1305 e = env->cp15.mdcr_el2 & MDCR_HPME;
87124fde 1306 }
033614c4 1307 enabled = e && (env->cp15.c9_pmcnten & (1 << counter));
87124fde 1308
033614c4
AL
1309 if (!secure) {
1310 if (el == 2 && (counter < hpmn || counter == 31)) {
1311 prohibited = env->cp15.mdcr_el2 & MDCR_HPMD;
1312 } else {
1313 prohibited = false;
1314 }
1315 } else {
1316 prohibited = arm_feature(env, ARM_FEATURE_EL3) &&
1317 (env->cp15.mdcr_el3 & MDCR_SPME);
1318 }
1319
1320 if (prohibited && counter == 31) {
1321 prohibited = env->cp15.c9_pmcr & PMCRDP;
1322 }
1323
5ecdd3e4
AL
1324 if (counter == 31) {
1325 filter = env->cp15.pmccfiltr_el0;
1326 } else {
1327 filter = env->cp15.c14_pmevtyper[counter];
1328 }
033614c4
AL
1329
1330 p = filter & PMXEVTYPER_P;
1331 u = filter & PMXEVTYPER_U;
1332 nsk = arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_NSK);
1333 nsu = arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_NSU);
1334 nsh = arm_feature(env, ARM_FEATURE_EL2) && (filter & PMXEVTYPER_NSH);
1335 m = arm_el_is_aa64(env, 1) &&
1336 arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_M);
1337
1338 if (el == 0) {
1339 filtered = secure ? u : u != nsu;
1340 } else if (el == 1) {
1341 filtered = secure ? p : p != nsk;
1342 } else if (el == 2) {
1343 filtered = !nsh;
1344 } else { /* EL3 */
1345 filtered = m != p;
1346 }
1347
5ecdd3e4
AL
1348 if (counter != 31) {
1349 /*
1350 * If not checking PMCCNTR, ensure the counter is setup to an event we
1351 * support
1352 */
1353 uint16_t event = filter & PMXEVTYPER_EVTCOUNT;
1354 if (!event_supported(event)) {
1355 return false;
1356 }
1357 }
1358
033614c4 1359 return enabled && !prohibited && !filtered;
87124fde 1360}
033614c4 1361
f4efb4b2
AL
1362static void pmu_update_irq(CPUARMState *env)
1363{
2fc0cc0e 1364 ARMCPU *cpu = env_archcpu(env);
f4efb4b2
AL
1365 qemu_set_irq(cpu->pmu_interrupt, (env->cp15.c9_pmcr & PMCRE) &&
1366 (env->cp15.c9_pminten & env->cp15.c9_pmovsr));
1367}
1368
5d05b9d4
AL
1369/*
1370 * Ensure c15_ccnt is the guest-visible count so that operations such as
1371 * enabling/disabling the counter or filtering, modifying the count itself,
1372 * etc. can be done logically. This is essentially a no-op if the counter is
1373 * not enabled at the time of the call.
1374 */
f2b2f53f 1375static void pmccntr_op_start(CPUARMState *env)
ec7b4ce4 1376{
b2e23725 1377 uint64_t cycles = cycles_get_count(env);
ec7b4ce4 1378
033614c4 1379 if (pmu_counter_enabled(env, 31)) {
5d05b9d4
AL
1380 uint64_t eff_cycles = cycles;
1381 if (env->cp15.c9_pmcr & PMCRD) {
1382 /* Increment once every 64 processor clock cycles */
1383 eff_cycles /= 64;
1384 }
1385
f4efb4b2
AL
1386 uint64_t new_pmccntr = eff_cycles - env->cp15.c15_ccnt_delta;
1387
1388 uint64_t overflow_mask = env->cp15.c9_pmcr & PMCRLC ? \
1389 1ull << 63 : 1ull << 31;
1390 if (env->cp15.c15_ccnt & ~new_pmccntr & overflow_mask) {
1391 env->cp15.c9_pmovsr |= (1 << 31);
1392 pmu_update_irq(env);
1393 }
1394
1395 env->cp15.c15_ccnt = new_pmccntr;
ec7b4ce4 1396 }
5d05b9d4
AL
1397 env->cp15.c15_ccnt_delta = cycles;
1398}
ec7b4ce4 1399
5d05b9d4
AL
1400/*
1401 * If PMCCNTR is enabled, recalculate the delta between the clock and the
1402 * guest-visible count. A call to pmccntr_op_finish should follow every call to
1403 * pmccntr_op_start.
1404 */
f2b2f53f 1405static void pmccntr_op_finish(CPUARMState *env)
5d05b9d4 1406{
033614c4 1407 if (pmu_counter_enabled(env, 31)) {
4e7beb0c
AL
1408#ifndef CONFIG_USER_ONLY
1409 /* Calculate when the counter will next overflow */
1410 uint64_t remaining_cycles = -env->cp15.c15_ccnt;
1411 if (!(env->cp15.c9_pmcr & PMCRLC)) {
1412 remaining_cycles = (uint32_t)remaining_cycles;
1413 }
1414 int64_t overflow_in = cycles_ns_per(remaining_cycles);
1415
1416 if (overflow_in > 0) {
1417 int64_t overflow_at = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
1418 overflow_in;
2fc0cc0e 1419 ARMCPU *cpu = env_archcpu(env);
4e7beb0c
AL
1420 timer_mod_anticipate_ns(cpu->pmu_timer, overflow_at);
1421 }
1422#endif
5d05b9d4 1423
4e7beb0c 1424 uint64_t prev_cycles = env->cp15.c15_ccnt_delta;
5d05b9d4
AL
1425 if (env->cp15.c9_pmcr & PMCRD) {
1426 /* Increment once every 64 processor clock cycles */
1427 prev_cycles /= 64;
1428 }
5d05b9d4 1429 env->cp15.c15_ccnt_delta = prev_cycles - env->cp15.c15_ccnt;
ec7b4ce4
AF
1430 }
1431}
1432
5ecdd3e4
AL
1433static void pmevcntr_op_start(CPUARMState *env, uint8_t counter)
1434{
1435
1436 uint16_t event = env->cp15.c14_pmevtyper[counter] & PMXEVTYPER_EVTCOUNT;
1437 uint64_t count = 0;
1438 if (event_supported(event)) {
1439 uint16_t event_idx = supported_event_map[event];
1440 count = pm_events[event_idx].get_count(env);
1441 }
1442
1443 if (pmu_counter_enabled(env, counter)) {
f4efb4b2
AL
1444 uint32_t new_pmevcntr = count - env->cp15.c14_pmevcntr_delta[counter];
1445
1446 if (env->cp15.c14_pmevcntr[counter] & ~new_pmevcntr & INT32_MIN) {
1447 env->cp15.c9_pmovsr |= (1 << counter);
1448 pmu_update_irq(env);
1449 }
1450 env->cp15.c14_pmevcntr[counter] = new_pmevcntr;
5ecdd3e4
AL
1451 }
1452 env->cp15.c14_pmevcntr_delta[counter] = count;
1453}
1454
1455static void pmevcntr_op_finish(CPUARMState *env, uint8_t counter)
1456{
1457 if (pmu_counter_enabled(env, counter)) {
4e7beb0c
AL
1458#ifndef CONFIG_USER_ONLY
1459 uint16_t event = env->cp15.c14_pmevtyper[counter] & PMXEVTYPER_EVTCOUNT;
1460 uint16_t event_idx = supported_event_map[event];
1461 uint64_t delta = UINT32_MAX -
1462 (uint32_t)env->cp15.c14_pmevcntr[counter] + 1;
1463 int64_t overflow_in = pm_events[event_idx].ns_per_count(delta);
1464
1465 if (overflow_in > 0) {
1466 int64_t overflow_at = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
1467 overflow_in;
2fc0cc0e 1468 ARMCPU *cpu = env_archcpu(env);
4e7beb0c
AL
1469 timer_mod_anticipate_ns(cpu->pmu_timer, overflow_at);
1470 }
1471#endif
1472
5ecdd3e4
AL
1473 env->cp15.c14_pmevcntr_delta[counter] -=
1474 env->cp15.c14_pmevcntr[counter];
1475 }
1476}
1477
5d05b9d4
AL
1478void pmu_op_start(CPUARMState *env)
1479{
5ecdd3e4 1480 unsigned int i;
5d05b9d4 1481 pmccntr_op_start(env);
5ecdd3e4
AL
1482 for (i = 0; i < pmu_num_counters(env); i++) {
1483 pmevcntr_op_start(env, i);
1484 }
5d05b9d4
AL
1485}
1486
1487void pmu_op_finish(CPUARMState *env)
1488{
5ecdd3e4 1489 unsigned int i;
5d05b9d4 1490 pmccntr_op_finish(env);
5ecdd3e4
AL
1491 for (i = 0; i < pmu_num_counters(env); i++) {
1492 pmevcntr_op_finish(env, i);
1493 }
5d05b9d4
AL
1494}
1495
033614c4
AL
1496void pmu_pre_el_change(ARMCPU *cpu, void *ignored)
1497{
1498 pmu_op_start(&cpu->env);
1499}
1500
1501void pmu_post_el_change(ARMCPU *cpu, void *ignored)
1502{
1503 pmu_op_finish(&cpu->env);
1504}
1505
4e7beb0c
AL
1506void arm_pmu_timer_cb(void *opaque)
1507{
1508 ARMCPU *cpu = opaque;
1509
1510 /*
1511 * Update all the counter values based on the current underlying counts,
1512 * triggering interrupts to be raised, if necessary. pmu_op_finish() also
1513 * has the effect of setting the cpu->pmu_timer to the next earliest time a
1514 * counter may expire.
1515 */
1516 pmu_op_start(&cpu->env);
1517 pmu_op_finish(&cpu->env);
1518}
1519
c4241c7d
PM
1520static void pmcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1521 uint64_t value)
200ac0ef 1522{
5d05b9d4 1523 pmu_op_start(env);
7c2cb42b
AF
1524
1525 if (value & PMCRC) {
1526 /* The counter has been reset */
1527 env->cp15.c15_ccnt = 0;
1528 }
1529
5ecdd3e4
AL
1530 if (value & PMCRP) {
1531 unsigned int i;
1532 for (i = 0; i < pmu_num_counters(env); i++) {
1533 env->cp15.c14_pmevcntr[i] = 0;
1534 }
1535 }
1536
200ac0ef
PM
1537 /* only the DP, X, D and E bits are writable */
1538 env->cp15.c9_pmcr &= ~0x39;
1539 env->cp15.c9_pmcr |= (value & 0x39);
7c2cb42b 1540
5d05b9d4 1541 pmu_op_finish(env);
7c2cb42b
AF
1542}
1543
0d4bfd7d
AL
1544static void pmswinc_write(CPUARMState *env, const ARMCPRegInfo *ri,
1545 uint64_t value)
1546{
1547 unsigned int i;
1548 for (i = 0; i < pmu_num_counters(env); i++) {
1549 /* Increment a counter's count iff: */
1550 if ((value & (1 << i)) && /* counter's bit is set */
1551 /* counter is enabled and not filtered */
1552 pmu_counter_enabled(env, i) &&
1553 /* counter is SW_INCR */
1554 (env->cp15.c14_pmevtyper[i] & PMXEVTYPER_EVTCOUNT) == 0x0) {
1555 pmevcntr_op_start(env, i);
f4efb4b2
AL
1556
1557 /*
1558 * Detect if this write causes an overflow since we can't predict
1559 * PMSWINC overflows like we can for other events
1560 */
1561 uint32_t new_pmswinc = env->cp15.c14_pmevcntr[i] + 1;
1562
1563 if (env->cp15.c14_pmevcntr[i] & ~new_pmswinc & INT32_MIN) {
1564 env->cp15.c9_pmovsr |= (1 << i);
1565 pmu_update_irq(env);
1566 }
1567
1568 env->cp15.c14_pmevcntr[i] = new_pmswinc;
1569
0d4bfd7d
AL
1570 pmevcntr_op_finish(env, i);
1571 }
1572 }
1573}
1574
7c2cb42b
AF
1575static uint64_t pmccntr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1576{
5d05b9d4
AL
1577 uint64_t ret;
1578 pmccntr_op_start(env);
1579 ret = env->cp15.c15_ccnt;
1580 pmccntr_op_finish(env);
1581 return ret;
7c2cb42b
AF
1582}
1583
6b040780
WH
1584static void pmselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1585 uint64_t value)
1586{
1587 /* The value of PMSELR.SEL affects the behavior of PMXEVTYPER and
1588 * PMXEVCNTR. We allow [0..31] to be written to PMSELR here; in the
1589 * meanwhile, we check PMSELR.SEL when PMXEVTYPER and PMXEVCNTR are
1590 * accessed.
1591 */
1592 env->cp15.c9_pmselr = value & 0x1f;
1593}
1594
7c2cb42b
AF
1595static void pmccntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1596 uint64_t value)
1597{
5d05b9d4
AL
1598 pmccntr_op_start(env);
1599 env->cp15.c15_ccnt = value;
1600 pmccntr_op_finish(env);
200ac0ef 1601}
421c7ebd
PC
1602
1603static void pmccntr_write32(CPUARMState *env, const ARMCPRegInfo *ri,
1604 uint64_t value)
1605{
1606 uint64_t cur_val = pmccntr_read(env, NULL);
1607
1608 pmccntr_write(env, ri, deposit64(cur_val, 0, 32, value));
1609}
1610
0614601c
AF
1611static void pmccfiltr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1612 uint64_t value)
1613{
5d05b9d4 1614 pmccntr_op_start(env);
4b8afa1f
AL
1615 env->cp15.pmccfiltr_el0 = value & PMCCFILTR_EL0;
1616 pmccntr_op_finish(env);
1617}
1618
1619static void pmccfiltr_write_a32(CPUARMState *env, const ARMCPRegInfo *ri,
1620 uint64_t value)
1621{
1622 pmccntr_op_start(env);
1623 /* M is not accessible from AArch32 */
1624 env->cp15.pmccfiltr_el0 = (env->cp15.pmccfiltr_el0 & PMCCFILTR_M) |
1625 (value & PMCCFILTR);
5d05b9d4 1626 pmccntr_op_finish(env);
0614601c
AF
1627}
1628
4b8afa1f
AL
1629static uint64_t pmccfiltr_read_a32(CPUARMState *env, const ARMCPRegInfo *ri)
1630{
1631 /* M is not visible in AArch32 */
1632 return env->cp15.pmccfiltr_el0 & PMCCFILTR;
1633}
1634
c4241c7d 1635static void pmcntenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
200ac0ef
PM
1636 uint64_t value)
1637{
7ece99b1 1638 value &= pmu_counter_mask(env);
200ac0ef 1639 env->cp15.c9_pmcnten |= value;
200ac0ef
PM
1640}
1641
c4241c7d
PM
1642static void pmcntenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1643 uint64_t value)
200ac0ef 1644{
7ece99b1 1645 value &= pmu_counter_mask(env);
200ac0ef 1646 env->cp15.c9_pmcnten &= ~value;
200ac0ef
PM
1647}
1648
c4241c7d
PM
1649static void pmovsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1650 uint64_t value)
200ac0ef 1651{
599b71e2 1652 value &= pmu_counter_mask(env);
200ac0ef 1653 env->cp15.c9_pmovsr &= ~value;
f4efb4b2 1654 pmu_update_irq(env);
200ac0ef
PM
1655}
1656
327dd510
AL
1657static void pmovsset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1658 uint64_t value)
1659{
1660 value &= pmu_counter_mask(env);
1661 env->cp15.c9_pmovsr |= value;
f4efb4b2 1662 pmu_update_irq(env);
327dd510
AL
1663}
1664
5ecdd3e4
AL
1665static void pmevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
1666 uint64_t value, const uint8_t counter)
200ac0ef 1667{
5ecdd3e4
AL
1668 if (counter == 31) {
1669 pmccfiltr_write(env, ri, value);
1670 } else if (counter < pmu_num_counters(env)) {
1671 pmevcntr_op_start(env, counter);
1672
1673 /*
1674 * If this counter's event type is changing, store the current
1675 * underlying count for the new type in c14_pmevcntr_delta[counter] so
1676 * pmevcntr_op_finish has the correct baseline when it converts back to
1677 * a delta.
1678 */
1679 uint16_t old_event = env->cp15.c14_pmevtyper[counter] &
1680 PMXEVTYPER_EVTCOUNT;
1681 uint16_t new_event = value & PMXEVTYPER_EVTCOUNT;
1682 if (old_event != new_event) {
1683 uint64_t count = 0;
1684 if (event_supported(new_event)) {
1685 uint16_t event_idx = supported_event_map[new_event];
1686 count = pm_events[event_idx].get_count(env);
1687 }
1688 env->cp15.c14_pmevcntr_delta[counter] = count;
1689 }
1690
1691 env->cp15.c14_pmevtyper[counter] = value & PMXEVTYPER_MASK;
1692 pmevcntr_op_finish(env, counter);
1693 }
fdb86656
WH
1694 /* Attempts to access PMXEVTYPER are CONSTRAINED UNPREDICTABLE when
1695 * PMSELR value is equal to or greater than the number of implemented
1696 * counters, but not equal to 0x1f. We opt to behave as a RAZ/WI.
1697 */
5ecdd3e4
AL
1698}
1699
1700static uint64_t pmevtyper_read(CPUARMState *env, const ARMCPRegInfo *ri,
1701 const uint8_t counter)
1702{
1703 if (counter == 31) {
1704 return env->cp15.pmccfiltr_el0;
1705 } else if (counter < pmu_num_counters(env)) {
1706 return env->cp15.c14_pmevtyper[counter];
1707 } else {
1708 /*
1709 * We opt to behave as a RAZ/WI when attempts to access PMXEVTYPER
1710 * are CONSTRAINED UNPREDICTABLE. See comments in pmevtyper_write().
1711 */
1712 return 0;
1713 }
1714}
1715
1716static void pmevtyper_writefn(CPUARMState *env, const ARMCPRegInfo *ri,
1717 uint64_t value)
1718{
1719 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1720 pmevtyper_write(env, ri, value, counter);
1721}
1722
1723static void pmevtyper_rawwrite(CPUARMState *env, const ARMCPRegInfo *ri,
1724 uint64_t value)
1725{
1726 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1727 env->cp15.c14_pmevtyper[counter] = value;
1728
1729 /*
1730 * pmevtyper_rawwrite is called between a pair of pmu_op_start and
1731 * pmu_op_finish calls when loading saved state for a migration. Because
1732 * we're potentially updating the type of event here, the value written to
1733 * c14_pmevcntr_delta by the preceeding pmu_op_start call may be for a
1734 * different counter type. Therefore, we need to set this value to the
1735 * current count for the counter type we're writing so that pmu_op_finish
1736 * has the correct count for its calculation.
1737 */
1738 uint16_t event = value & PMXEVTYPER_EVTCOUNT;
1739 if (event_supported(event)) {
1740 uint16_t event_idx = supported_event_map[event];
1741 env->cp15.c14_pmevcntr_delta[counter] =
1742 pm_events[event_idx].get_count(env);
fdb86656
WH
1743 }
1744}
1745
5ecdd3e4
AL
1746static uint64_t pmevtyper_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
1747{
1748 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1749 return pmevtyper_read(env, ri, counter);
1750}
1751
1752static void pmxevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
1753 uint64_t value)
1754{
1755 pmevtyper_write(env, ri, value, env->cp15.c9_pmselr & 31);
1756}
1757
fdb86656
WH
1758static uint64_t pmxevtyper_read(CPUARMState *env, const ARMCPRegInfo *ri)
1759{
5ecdd3e4
AL
1760 return pmevtyper_read(env, ri, env->cp15.c9_pmselr & 31);
1761}
1762
1763static void pmevcntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1764 uint64_t value, uint8_t counter)
1765{
1766 if (counter < pmu_num_counters(env)) {
1767 pmevcntr_op_start(env, counter);
1768 env->cp15.c14_pmevcntr[counter] = value;
1769 pmevcntr_op_finish(env, counter);
1770 }
1771 /*
1772 * We opt to behave as a RAZ/WI when attempts to access PM[X]EVCNTR
1773 * are CONSTRAINED UNPREDICTABLE.
fdb86656 1774 */
5ecdd3e4
AL
1775}
1776
1777static uint64_t pmevcntr_read(CPUARMState *env, const ARMCPRegInfo *ri,
1778 uint8_t counter)
1779{
1780 if (counter < pmu_num_counters(env)) {
1781 uint64_t ret;
1782 pmevcntr_op_start(env, counter);
1783 ret = env->cp15.c14_pmevcntr[counter];
1784 pmevcntr_op_finish(env, counter);
1785 return ret;
fdb86656 1786 } else {
5ecdd3e4
AL
1787 /* We opt to behave as a RAZ/WI when attempts to access PM[X]EVCNTR
1788 * are CONSTRAINED UNPREDICTABLE. */
fdb86656
WH
1789 return 0;
1790 }
200ac0ef
PM
1791}
1792
5ecdd3e4
AL
1793static void pmevcntr_writefn(CPUARMState *env, const ARMCPRegInfo *ri,
1794 uint64_t value)
1795{
1796 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1797 pmevcntr_write(env, ri, value, counter);
1798}
1799
1800static uint64_t pmevcntr_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
1801{
1802 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1803 return pmevcntr_read(env, ri, counter);
1804}
1805
1806static void pmevcntr_rawwrite(CPUARMState *env, const ARMCPRegInfo *ri,
1807 uint64_t value)
1808{
1809 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1810 assert(counter < pmu_num_counters(env));
1811 env->cp15.c14_pmevcntr[counter] = value;
1812 pmevcntr_write(env, ri, value, counter);
1813}
1814
1815static uint64_t pmevcntr_rawread(CPUARMState *env, const ARMCPRegInfo *ri)
1816{
1817 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1818 assert(counter < pmu_num_counters(env));
1819 return env->cp15.c14_pmevcntr[counter];
1820}
1821
1822static void pmxevcntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1823 uint64_t value)
1824{
1825 pmevcntr_write(env, ri, value, env->cp15.c9_pmselr & 31);
1826}
1827
1828static uint64_t pmxevcntr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1829{
1830 return pmevcntr_read(env, ri, env->cp15.c9_pmselr & 31);
1831}
1832
c4241c7d 1833static void pmuserenr_write(CPUARMState *env, const ARMCPRegInfo *ri,
200ac0ef
PM
1834 uint64_t value)
1835{
6ecd0b6b
AB
1836 if (arm_feature(env, ARM_FEATURE_V8)) {
1837 env->cp15.c9_pmuserenr = value & 0xf;
1838 } else {
1839 env->cp15.c9_pmuserenr = value & 1;
1840 }
200ac0ef
PM
1841}
1842
c4241c7d
PM
1843static void pmintenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1844 uint64_t value)
200ac0ef
PM
1845{
1846 /* We have no event counters so only the C bit can be changed */
7ece99b1 1847 value &= pmu_counter_mask(env);
200ac0ef 1848 env->cp15.c9_pminten |= value;
f4efb4b2 1849 pmu_update_irq(env);
200ac0ef
PM
1850}
1851
c4241c7d
PM
1852static void pmintenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1853 uint64_t value)
200ac0ef 1854{
7ece99b1 1855 value &= pmu_counter_mask(env);
200ac0ef 1856 env->cp15.c9_pminten &= ~value;
f4efb4b2 1857 pmu_update_irq(env);
200ac0ef
PM
1858}
1859
c4241c7d
PM
1860static void vbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
1861 uint64_t value)
8641136c 1862{
a505d7fe
PM
1863 /* Note that even though the AArch64 view of this register has bits
1864 * [10:0] all RES0 we can only mask the bottom 5, to comply with the
1865 * architectural requirements for bits which are RES0 only in some
1866 * contexts. (ARMv8 would permit us to do no masking at all, but ARMv7
1867 * requires the bottom five bits to be RAZ/WI because they're UNK/SBZP.)
1868 */
855ea66d 1869 raw_write(env, ri, value & ~0x1FULL);
8641136c
NR
1870}
1871
64e0e2de
EI
1872static void scr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
1873{
ea22747c
RH
1874 /* Begin with base v8.0 state. */
1875 uint32_t valid_mask = 0x3fff;
2fc0cc0e 1876 ARMCPU *cpu = env_archcpu(env);
ea22747c
RH
1877
1878 if (arm_el_is_aa64(env, 3)) {
1879 value |= SCR_FW | SCR_AW; /* these two bits are RES1. */
1880 valid_mask &= ~SCR_NET;
1881 } else {
1882 valid_mask &= ~(SCR_RW | SCR_ST);
1883 }
64e0e2de
EI
1884
1885 if (!arm_feature(env, ARM_FEATURE_EL2)) {
1886 valid_mask &= ~SCR_HCE;
1887
1888 /* On ARMv7, SMD (or SCD as it is called in v7) is only
1889 * supported if EL2 exists. The bit is UNK/SBZP when
1890 * EL2 is unavailable. In QEMU ARMv7, we force it to always zero
1891 * when EL2 is unavailable.
4eb27640 1892 * On ARMv8, this bit is always available.
64e0e2de 1893 */
4eb27640
GB
1894 if (arm_feature(env, ARM_FEATURE_V7) &&
1895 !arm_feature(env, ARM_FEATURE_V8)) {
64e0e2de
EI
1896 valid_mask &= ~SCR_SMD;
1897 }
1898 }
2d7137c1
RH
1899 if (cpu_isar_feature(aa64_lor, cpu)) {
1900 valid_mask |= SCR_TLOR;
1901 }
ef682cdb
RH
1902 if (cpu_isar_feature(aa64_pauth, cpu)) {
1903 valid_mask |= SCR_API | SCR_APK;
1904 }
64e0e2de
EI
1905
1906 /* Clear all-context RES0 bits. */
1907 value &= valid_mask;
1908 raw_write(env, ri, value);
1909}
1910
630fcd4d
MZ
1911static CPAccessResult access_aa64_tid2(CPUARMState *env,
1912 const ARMCPRegInfo *ri,
1913 bool isread)
1914{
1915 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID2)) {
1916 return CP_ACCESS_TRAP_EL2;
1917 }
1918
1919 return CP_ACCESS_OK;
1920}
1921
c4241c7d 1922static uint64_t ccsidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
776d4e5c 1923{
2fc0cc0e 1924 ARMCPU *cpu = env_archcpu(env);
b85a1fd6
FA
1925
1926 /* Acquire the CSSELR index from the bank corresponding to the CCSIDR
1927 * bank
1928 */
1929 uint32_t index = A32_BANKED_REG_GET(env, csselr,
1930 ri->secure & ARM_CP_SECSTATE_S);
1931
1932 return cpu->ccsidr[index];
776d4e5c
PM
1933}
1934
c4241c7d
PM
1935static void csselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1936 uint64_t value)
776d4e5c 1937{
8d5c773e 1938 raw_write(env, ri, value & 0xf);
776d4e5c
PM
1939}
1940
1090b9c6
PM
1941static uint64_t isr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1942{
29a0af61 1943 CPUState *cs = env_cpu(env);
f7778444 1944 uint64_t hcr_el2 = arm_hcr_el2_eff(env);
1090b9c6 1945 uint64_t ret = 0;
7cf95aed
MZ
1946 bool allow_virt = (arm_current_el(env) == 1 &&
1947 (!arm_is_secure_below_el3(env) ||
1948 (env->cp15.scr_el3 & SCR_EEL2)));
1090b9c6 1949
7cf95aed 1950 if (allow_virt && (hcr_el2 & HCR_IMO)) {
636540e9
PM
1951 if (cs->interrupt_request & CPU_INTERRUPT_VIRQ) {
1952 ret |= CPSR_I;
1953 }
1954 } else {
1955 if (cs->interrupt_request & CPU_INTERRUPT_HARD) {
1956 ret |= CPSR_I;
1957 }
1090b9c6 1958 }
636540e9 1959
7cf95aed 1960 if (allow_virt && (hcr_el2 & HCR_FMO)) {
636540e9
PM
1961 if (cs->interrupt_request & CPU_INTERRUPT_VFIQ) {
1962 ret |= CPSR_F;
1963 }
1964 } else {
1965 if (cs->interrupt_request & CPU_INTERRUPT_FIQ) {
1966 ret |= CPSR_F;
1967 }
1090b9c6 1968 }
636540e9 1969
1090b9c6
PM
1970 /* External aborts are not possible in QEMU so A bit is always clear */
1971 return ret;
1972}
1973
93fbc983
MZ
1974static CPAccessResult access_aa64_tid1(CPUARMState *env, const ARMCPRegInfo *ri,
1975 bool isread)
1976{
1977 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID1)) {
1978 return CP_ACCESS_TRAP_EL2;
1979 }
1980
1981 return CP_ACCESS_OK;
1982}
1983
1984static CPAccessResult access_aa32_tid1(CPUARMState *env, const ARMCPRegInfo *ri,
1985 bool isread)
1986{
1987 if (arm_feature(env, ARM_FEATURE_V8)) {
1988 return access_aa64_tid1(env, ri, isread);
1989 }
1990
1991 return CP_ACCESS_OK;
1992}
1993
e9aa6c21 1994static const ARMCPRegInfo v7_cp_reginfo[] = {
7d57f408
PM
1995 /* the old v6 WFI, UNPREDICTABLE in v7 but we choose to NOP */
1996 { .name = "NOP", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
1997 .access = PL1_W, .type = ARM_CP_NOP },
200ac0ef
PM
1998 /* Performance monitors are implementation defined in v7,
1999 * but with an ARM recommended set of registers, which we
ac689a2e 2000 * follow.
200ac0ef
PM
2001 *
2002 * Performance registers fall into three categories:
2003 * (a) always UNDEF in PL0, RW in PL1 (PMINTENSET, PMINTENCLR)
2004 * (b) RO in PL0 (ie UNDEF on write), RW in PL1 (PMUSERENR)
2005 * (c) UNDEF in PL0 if PMUSERENR.EN==0, otherwise accessible (all others)
2006 * For the cases controlled by PMUSERENR we must set .access to PL0_RW
2007 * or PL0_RO as appropriate and then check PMUSERENR in the helper fn.
2008 */
2009 { .name = "PMCNTENSET", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 1,
7a0e58fa 2010 .access = PL0_RW, .type = ARM_CP_ALIAS,
8521466b 2011 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
fcd25206
PM
2012 .writefn = pmcntenset_write,
2013 .accessfn = pmreg_access,
2014 .raw_writefn = raw_write },
8521466b
AF
2015 { .name = "PMCNTENSET_EL0", .state = ARM_CP_STATE_AA64,
2016 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 1,
2017 .access = PL0_RW, .accessfn = pmreg_access,
2018 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten), .resetvalue = 0,
2019 .writefn = pmcntenset_write, .raw_writefn = raw_write },
200ac0ef 2020 { .name = "PMCNTENCLR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 2,
8521466b
AF
2021 .access = PL0_RW,
2022 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
fcd25206
PM
2023 .accessfn = pmreg_access,
2024 .writefn = pmcntenclr_write,
7a0e58fa 2025 .type = ARM_CP_ALIAS },
8521466b
AF
2026 { .name = "PMCNTENCLR_EL0", .state = ARM_CP_STATE_AA64,
2027 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 2,
2028 .access = PL0_RW, .accessfn = pmreg_access,
7a0e58fa 2029 .type = ARM_CP_ALIAS,
8521466b
AF
2030 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten),
2031 .writefn = pmcntenclr_write },
200ac0ef 2032 { .name = "PMOVSR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 3,
f4efb4b2 2033 .access = PL0_RW, .type = ARM_CP_IO,
e4e91a21 2034 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr),
fcd25206
PM
2035 .accessfn = pmreg_access,
2036 .writefn = pmovsr_write,
2037 .raw_writefn = raw_write },
978364f1
AF
2038 { .name = "PMOVSCLR_EL0", .state = ARM_CP_STATE_AA64,
2039 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 3,
2040 .access = PL0_RW, .accessfn = pmreg_access,
f4efb4b2 2041 .type = ARM_CP_ALIAS | ARM_CP_IO,
978364f1
AF
2042 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
2043 .writefn = pmovsr_write,
2044 .raw_writefn = raw_write },
200ac0ef 2045 { .name = "PMSWINC", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 4,
f4efb4b2
AL
2046 .access = PL0_W, .accessfn = pmreg_access_swinc,
2047 .type = ARM_CP_NO_RAW | ARM_CP_IO,
0d4bfd7d
AL
2048 .writefn = pmswinc_write },
2049 { .name = "PMSWINC_EL0", .state = ARM_CP_STATE_AA64,
2050 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 4,
f4efb4b2
AL
2051 .access = PL0_W, .accessfn = pmreg_access_swinc,
2052 .type = ARM_CP_NO_RAW | ARM_CP_IO,
0d4bfd7d 2053 .writefn = pmswinc_write },
6b040780
WH
2054 { .name = "PMSELR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 5,
2055 .access = PL0_RW, .type = ARM_CP_ALIAS,
2056 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmselr),
6ecd0b6b 2057 .accessfn = pmreg_access_selr, .writefn = pmselr_write,
6b040780
WH
2058 .raw_writefn = raw_write},
2059 { .name = "PMSELR_EL0", .state = ARM_CP_STATE_AA64,
2060 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 5,
6ecd0b6b 2061 .access = PL0_RW, .accessfn = pmreg_access_selr,
6b040780
WH
2062 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmselr),
2063 .writefn = pmselr_write, .raw_writefn = raw_write, },
200ac0ef 2064 { .name = "PMCCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 0,
169c8938 2065 .access = PL0_RW, .resetvalue = 0, .type = ARM_CP_ALIAS | ARM_CP_IO,
421c7ebd 2066 .readfn = pmccntr_read, .writefn = pmccntr_write32,
6ecd0b6b 2067 .accessfn = pmreg_access_ccntr },
8521466b
AF
2068 { .name = "PMCCNTR_EL0", .state = ARM_CP_STATE_AA64,
2069 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 0,
6ecd0b6b 2070 .access = PL0_RW, .accessfn = pmreg_access_ccntr,
8521466b 2071 .type = ARM_CP_IO,
980ebe87
AL
2072 .fieldoffset = offsetof(CPUARMState, cp15.c15_ccnt),
2073 .readfn = pmccntr_read, .writefn = pmccntr_write,
2074 .raw_readfn = raw_read, .raw_writefn = raw_write, },
4b8afa1f
AL
2075 { .name = "PMCCFILTR", .cp = 15, .opc1 = 0, .crn = 14, .crm = 15, .opc2 = 7,
2076 .writefn = pmccfiltr_write_a32, .readfn = pmccfiltr_read_a32,
2077 .access = PL0_RW, .accessfn = pmreg_access,
2078 .type = ARM_CP_ALIAS | ARM_CP_IO,
2079 .resetvalue = 0, },
8521466b
AF
2080 { .name = "PMCCFILTR_EL0", .state = ARM_CP_STATE_AA64,
2081 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 15, .opc2 = 7,
980ebe87 2082 .writefn = pmccfiltr_write, .raw_writefn = raw_write,
8521466b
AF
2083 .access = PL0_RW, .accessfn = pmreg_access,
2084 .type = ARM_CP_IO,
2085 .fieldoffset = offsetof(CPUARMState, cp15.pmccfiltr_el0),
2086 .resetvalue = 0, },
200ac0ef 2087 { .name = "PMXEVTYPER", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 1,
5ecdd3e4
AL
2088 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2089 .accessfn = pmreg_access,
fdb86656
WH
2090 .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
2091 { .name = "PMXEVTYPER_EL0", .state = ARM_CP_STATE_AA64,
2092 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 1,
5ecdd3e4
AL
2093 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2094 .accessfn = pmreg_access,
fdb86656 2095 .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
200ac0ef 2096 { .name = "PMXEVCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 2,
5ecdd3e4
AL
2097 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2098 .accessfn = pmreg_access_xevcntr,
2099 .writefn = pmxevcntr_write, .readfn = pmxevcntr_read },
2100 { .name = "PMXEVCNTR_EL0", .state = ARM_CP_STATE_AA64,
2101 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 2,
2102 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2103 .accessfn = pmreg_access_xevcntr,
2104 .writefn = pmxevcntr_write, .readfn = pmxevcntr_read },
200ac0ef 2105 { .name = "PMUSERENR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 0,
1fce1ba9 2106 .access = PL0_R | PL1_RW, .accessfn = access_tpm,
e4e91a21 2107 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmuserenr),
200ac0ef 2108 .resetvalue = 0,
d4e6df63 2109 .writefn = pmuserenr_write, .raw_writefn = raw_write },
8a83ffc2
AF
2110 { .name = "PMUSERENR_EL0", .state = ARM_CP_STATE_AA64,
2111 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 0,
1fce1ba9 2112 .access = PL0_R | PL1_RW, .accessfn = access_tpm, .type = ARM_CP_ALIAS,
8a83ffc2
AF
2113 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmuserenr),
2114 .resetvalue = 0,
2115 .writefn = pmuserenr_write, .raw_writefn = raw_write },
200ac0ef 2116 { .name = "PMINTENSET", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 1,
1fce1ba9 2117 .access = PL1_RW, .accessfn = access_tpm,
b7d793ad 2118 .type = ARM_CP_ALIAS | ARM_CP_IO,
e6ec5457 2119 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pminten),
200ac0ef 2120 .resetvalue = 0,
d4e6df63 2121 .writefn = pmintenset_write, .raw_writefn = raw_write },
e6ec5457
WH
2122 { .name = "PMINTENSET_EL1", .state = ARM_CP_STATE_AA64,
2123 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 1,
2124 .access = PL1_RW, .accessfn = access_tpm,
2125 .type = ARM_CP_IO,
2126 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2127 .writefn = pmintenset_write, .raw_writefn = raw_write,
2128 .resetvalue = 0x0 },
200ac0ef 2129 { .name = "PMINTENCLR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 2,
fc5f6856
AL
2130 .access = PL1_RW, .accessfn = access_tpm,
2131 .type = ARM_CP_ALIAS | ARM_CP_IO,
200ac0ef 2132 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
b061a82b 2133 .writefn = pmintenclr_write, },
978364f1
AF
2134 { .name = "PMINTENCLR_EL1", .state = ARM_CP_STATE_AA64,
2135 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 2,
fc5f6856
AL
2136 .access = PL1_RW, .accessfn = access_tpm,
2137 .type = ARM_CP_ALIAS | ARM_CP_IO,
978364f1
AF
2138 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2139 .writefn = pmintenclr_write },
7da845b0
PM
2140 { .name = "CCSIDR", .state = ARM_CP_STATE_BOTH,
2141 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 0,
630fcd4d
MZ
2142 .access = PL1_R,
2143 .accessfn = access_aa64_tid2,
2144 .readfn = ccsidr_read, .type = ARM_CP_NO_RAW },
7da845b0
PM
2145 { .name = "CSSELR", .state = ARM_CP_STATE_BOTH,
2146 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 2, .opc2 = 0,
630fcd4d
MZ
2147 .access = PL1_RW,
2148 .accessfn = access_aa64_tid2,
2149 .writefn = csselr_write, .resetvalue = 0,
b85a1fd6
FA
2150 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.csselr_s),
2151 offsetof(CPUARMState, cp15.csselr_ns) } },
776d4e5c
PM
2152 /* Auxiliary ID register: this actually has an IMPDEF value but for now
2153 * just RAZ for all cores:
2154 */
0ff644a7
PM
2155 { .name = "AIDR", .state = ARM_CP_STATE_BOTH,
2156 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 7,
93fbc983
MZ
2157 .access = PL1_R, .type = ARM_CP_CONST,
2158 .accessfn = access_aa64_tid1,
2159 .resetvalue = 0 },
f32cdad5
PM
2160 /* Auxiliary fault status registers: these also are IMPDEF, and we
2161 * choose to RAZ/WI for all cores.
2162 */
2163 { .name = "AFSR0_EL1", .state = ARM_CP_STATE_BOTH,
2164 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 0,
2165 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
2166 { .name = "AFSR1_EL1", .state = ARM_CP_STATE_BOTH,
2167 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 1,
2168 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
b0fe2427
PM
2169 /* MAIR can just read-as-written because we don't implement caches
2170 * and so don't need to care about memory attributes.
2171 */
2172 { .name = "MAIR_EL1", .state = ARM_CP_STATE_AA64,
2173 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
be693c87 2174 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[1]),
b0fe2427 2175 .resetvalue = 0 },
4cfb8ad8
PM
2176 { .name = "MAIR_EL3", .state = ARM_CP_STATE_AA64,
2177 .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 2, .opc2 = 0,
2178 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[3]),
2179 .resetvalue = 0 },
b0fe2427
PM
2180 /* For non-long-descriptor page tables these are PRRR and NMRR;
2181 * regardless they still act as reads-as-written for QEMU.
b0fe2427 2182 */
1281f8e3 2183 /* MAIR0/1 are defined separately from their 64-bit counterpart which
be693c87
GB
2184 * allows them to assign the correct fieldoffset based on the endianness
2185 * handled in the field definitions.
2186 */
a903c449 2187 { .name = "MAIR0", .state = ARM_CP_STATE_AA32,
b0fe2427 2188 .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0, .access = PL1_RW,
be693c87
GB
2189 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair0_s),
2190 offsetof(CPUARMState, cp15.mair0_ns) },
b0fe2427 2191 .resetfn = arm_cp_reset_ignore },
a903c449 2192 { .name = "MAIR1", .state = ARM_CP_STATE_AA32,
b0fe2427 2193 .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 1, .access = PL1_RW,
be693c87
GB
2194 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair1_s),
2195 offsetof(CPUARMState, cp15.mair1_ns) },
b0fe2427 2196 .resetfn = arm_cp_reset_ignore },
1090b9c6
PM
2197 { .name = "ISR_EL1", .state = ARM_CP_STATE_BOTH,
2198 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 0,
7a0e58fa 2199 .type = ARM_CP_NO_RAW, .access = PL1_R, .readfn = isr_read },
995939a6
PM
2200 /* 32 bit ITLB invalidates */
2201 { .name = "ITLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 0,
7a0e58fa 2202 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_write },
995939a6 2203 { .name = "ITLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 1,
7a0e58fa 2204 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write },
995939a6 2205 { .name = "ITLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 2,
7a0e58fa 2206 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiasid_write },
995939a6
PM
2207 /* 32 bit DTLB invalidates */
2208 { .name = "DTLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 0,
7a0e58fa 2209 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_write },
995939a6 2210 { .name = "DTLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 1,
7a0e58fa 2211 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write },
995939a6 2212 { .name = "DTLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 2,
7a0e58fa 2213 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiasid_write },
995939a6
PM
2214 /* 32 bit TLB invalidates */
2215 { .name = "TLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
7a0e58fa 2216 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_write },
995939a6 2217 { .name = "TLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
7a0e58fa 2218 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write },
995939a6 2219 { .name = "TLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
7a0e58fa 2220 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiasid_write },
995939a6 2221 { .name = "TLBIMVAA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
7a0e58fa 2222 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimvaa_write },
995939a6
PM
2223 REGINFO_SENTINEL
2224};
2225
2226static const ARMCPRegInfo v7mp_cp_reginfo[] = {
2227 /* 32 bit TLB invalidates, Inner Shareable */
2228 { .name = "TLBIALLIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
7a0e58fa 2229 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_is_write },
995939a6 2230 { .name = "TLBIMVAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
7a0e58fa 2231 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_is_write },
995939a6 2232 { .name = "TLBIASIDIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
7a0e58fa 2233 .type = ARM_CP_NO_RAW, .access = PL1_W,
fa439fc5 2234 .writefn = tlbiasid_is_write },
995939a6 2235 { .name = "TLBIMVAAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
7a0e58fa 2236 .type = ARM_CP_NO_RAW, .access = PL1_W,
fa439fc5 2237 .writefn = tlbimvaa_is_write },
e9aa6c21
PM
2238 REGINFO_SENTINEL
2239};
2240
327dd510
AL
2241static const ARMCPRegInfo pmovsset_cp_reginfo[] = {
2242 /* PMOVSSET is not implemented in v7 before v7ve */
2243 { .name = "PMOVSSET", .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 3,
2244 .access = PL0_RW, .accessfn = pmreg_access,
f4efb4b2 2245 .type = ARM_CP_ALIAS | ARM_CP_IO,
327dd510
AL
2246 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr),
2247 .writefn = pmovsset_write,
2248 .raw_writefn = raw_write },
2249 { .name = "PMOVSSET_EL0", .state = ARM_CP_STATE_AA64,
2250 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 3,
2251 .access = PL0_RW, .accessfn = pmreg_access,
f4efb4b2 2252 .type = ARM_CP_ALIAS | ARM_CP_IO,
327dd510
AL
2253 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
2254 .writefn = pmovsset_write,
2255 .raw_writefn = raw_write },
2256 REGINFO_SENTINEL
2257};
2258
c4241c7d
PM
2259static void teecr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2260 uint64_t value)
c326b979
PM
2261{
2262 value &= 1;
2263 env->teecr = value;
c326b979
PM
2264}
2265
3f208fd7
PM
2266static CPAccessResult teehbr_access(CPUARMState *env, const ARMCPRegInfo *ri,
2267 bool isread)
c326b979 2268{
dcbff19b 2269 if (arm_current_el(env) == 0 && (env->teecr & 1)) {
92611c00 2270 return CP_ACCESS_TRAP;
c326b979 2271 }
92611c00 2272 return CP_ACCESS_OK;
c326b979
PM
2273}
2274
2275static const ARMCPRegInfo t2ee_cp_reginfo[] = {
2276 { .name = "TEECR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 6, .opc2 = 0,
2277 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, teecr),
2278 .resetvalue = 0,
2279 .writefn = teecr_write },
2280 { .name = "TEEHBR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 6, .opc2 = 0,
2281 .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, teehbr),
92611c00 2282 .accessfn = teehbr_access, .resetvalue = 0 },
c326b979
PM
2283 REGINFO_SENTINEL
2284};
2285
4d31c596 2286static const ARMCPRegInfo v6k_cp_reginfo[] = {
e4fe830b
PM
2287 { .name = "TPIDR_EL0", .state = ARM_CP_STATE_AA64,
2288 .opc0 = 3, .opc1 = 3, .opc2 = 2, .crn = 13, .crm = 0,
2289 .access = PL0_RW,
54bf36ed 2290 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[0]), .resetvalue = 0 },
4d31c596
PM
2291 { .name = "TPIDRURW", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 2,
2292 .access = PL0_RW,
54bf36ed
FA
2293 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrurw_s),
2294 offsetoflow32(CPUARMState, cp15.tpidrurw_ns) },
e4fe830b
PM
2295 .resetfn = arm_cp_reset_ignore },
2296 { .name = "TPIDRRO_EL0", .state = ARM_CP_STATE_AA64,
2297 .opc0 = 3, .opc1 = 3, .opc2 = 3, .crn = 13, .crm = 0,
2298 .access = PL0_R|PL1_W,
54bf36ed
FA
2299 .fieldoffset = offsetof(CPUARMState, cp15.tpidrro_el[0]),
2300 .resetvalue = 0},
4d31c596
PM
2301 { .name = "TPIDRURO", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 3,
2302 .access = PL0_R|PL1_W,
54bf36ed
FA
2303 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidruro_s),
2304 offsetoflow32(CPUARMState, cp15.tpidruro_ns) },
e4fe830b 2305 .resetfn = arm_cp_reset_ignore },
54bf36ed 2306 { .name = "TPIDR_EL1", .state = ARM_CP_STATE_AA64,
e4fe830b 2307 .opc0 = 3, .opc1 = 0, .opc2 = 4, .crn = 13, .crm = 0,
4d31c596 2308 .access = PL1_RW,
54bf36ed
FA
2309 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[1]), .resetvalue = 0 },
2310 { .name = "TPIDRPRW", .opc1 = 0, .cp = 15, .crn = 13, .crm = 0, .opc2 = 4,
2311 .access = PL1_RW,
2312 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrprw_s),
2313 offsetoflow32(CPUARMState, cp15.tpidrprw_ns) },
2314 .resetvalue = 0 },
4d31c596
PM
2315 REGINFO_SENTINEL
2316};
2317
55d284af
PM
2318#ifndef CONFIG_USER_ONLY
2319
3f208fd7
PM
2320static CPAccessResult gt_cntfrq_access(CPUARMState *env, const ARMCPRegInfo *ri,
2321 bool isread)
00108f2d 2322{
75502672
PM
2323 /* CNTFRQ: not visible from PL0 if both PL0PCTEN and PL0VCTEN are zero.
2324 * Writable only at the highest implemented exception level.
2325 */
2326 int el = arm_current_el(env);
2327
2328 switch (el) {
2329 case 0:
2330 if (!extract32(env->cp15.c14_cntkctl, 0, 2)) {
2331 return CP_ACCESS_TRAP;
2332 }
2333 break;
2334 case 1:
2335 if (!isread && ri->state == ARM_CP_STATE_AA32 &&
2336 arm_is_secure_below_el3(env)) {
2337 /* Accesses from 32-bit Secure EL1 UNDEF (*not* trap to EL3!) */
2338 return CP_ACCESS_TRAP_UNCATEGORIZED;
2339 }
2340 break;
2341 case 2:
2342 case 3:
2343 break;
00108f2d 2344 }
75502672
PM
2345
2346 if (!isread && el < arm_highest_el(env)) {
2347 return CP_ACCESS_TRAP_UNCATEGORIZED;
2348 }
2349
00108f2d
PM
2350 return CP_ACCESS_OK;
2351}
2352
3f208fd7
PM
2353static CPAccessResult gt_counter_access(CPUARMState *env, int timeridx,
2354 bool isread)
00108f2d 2355{
0b6440af
EI
2356 unsigned int cur_el = arm_current_el(env);
2357 bool secure = arm_is_secure(env);
2358
00108f2d 2359 /* CNT[PV]CT: not visible from PL0 if ELO[PV]CTEN is zero */
0b6440af 2360 if (cur_el == 0 &&
00108f2d
PM
2361 !extract32(env->cp15.c14_cntkctl, timeridx, 1)) {
2362 return CP_ACCESS_TRAP;
2363 }
0b6440af
EI
2364
2365 if (arm_feature(env, ARM_FEATURE_EL2) &&
2366 timeridx == GTIMER_PHYS && !secure && cur_el < 2 &&
2367 !extract32(env->cp15.cnthctl_el2, 0, 1)) {
2368 return CP_ACCESS_TRAP_EL2;
2369 }
00108f2d
PM
2370 return CP_ACCESS_OK;
2371}
2372
3f208fd7
PM
2373static CPAccessResult gt_timer_access(CPUARMState *env, int timeridx,
2374 bool isread)
00108f2d 2375{
0b6440af
EI
2376 unsigned int cur_el = arm_current_el(env);
2377 bool secure = arm_is_secure(env);
2378
00108f2d
PM
2379 /* CNT[PV]_CVAL, CNT[PV]_CTL, CNT[PV]_TVAL: not visible from PL0 if
2380 * EL0[PV]TEN is zero.
2381 */
0b6440af 2382 if (cur_el == 0 &&
00108f2d
PM
2383 !extract32(env->cp15.c14_cntkctl, 9 - timeridx, 1)) {
2384 return CP_ACCESS_TRAP;
2385 }
0b6440af
EI
2386
2387 if (arm_feature(env, ARM_FEATURE_EL2) &&
2388 timeridx == GTIMER_PHYS && !secure && cur_el < 2 &&
2389 !extract32(env->cp15.cnthctl_el2, 1, 1)) {
2390 return CP_ACCESS_TRAP_EL2;
2391 }
00108f2d
PM
2392 return CP_ACCESS_OK;
2393}
2394
2395static CPAccessResult gt_pct_access(CPUARMState *env,
3f208fd7
PM
2396 const ARMCPRegInfo *ri,
2397 bool isread)
00108f2d 2398{
3f208fd7 2399 return gt_counter_access(env, GTIMER_PHYS, isread);
00108f2d
PM
2400}
2401
2402static CPAccessResult gt_vct_access(CPUARMState *env,
3f208fd7
PM
2403 const ARMCPRegInfo *ri,
2404 bool isread)
00108f2d 2405{
3f208fd7 2406 return gt_counter_access(env, GTIMER_VIRT, isread);
00108f2d
PM
2407}
2408
3f208fd7
PM
2409static CPAccessResult gt_ptimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
2410 bool isread)
00108f2d 2411{
3f208fd7 2412 return gt_timer_access(env, GTIMER_PHYS, isread);
00108f2d
PM
2413}
2414
3f208fd7
PM
2415static CPAccessResult gt_vtimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
2416 bool isread)
00108f2d 2417{
3f208fd7 2418 return gt_timer_access(env, GTIMER_VIRT, isread);
00108f2d
PM
2419}
2420
b4d3978c 2421static CPAccessResult gt_stimer_access(CPUARMState *env,
3f208fd7
PM
2422 const ARMCPRegInfo *ri,
2423 bool isread)
b4d3978c
PM
2424{
2425 /* The AArch64 register view of the secure physical timer is
2426 * always accessible from EL3, and configurably accessible from
2427 * Secure EL1.
2428 */
2429 switch (arm_current_el(env)) {
2430 case 1:
2431 if (!arm_is_secure(env)) {
2432 return CP_ACCESS_TRAP;
2433 }
2434 if (!(env->cp15.scr_el3 & SCR_ST)) {
2435 return CP_ACCESS_TRAP_EL3;
2436 }
2437 return CP_ACCESS_OK;
2438 case 0:
2439 case 2:
2440 return CP_ACCESS_TRAP;
2441 case 3:
2442 return CP_ACCESS_OK;
2443 default:
2444 g_assert_not_reached();
2445 }
2446}
2447
55d284af
PM
2448static uint64_t gt_get_countervalue(CPUARMState *env)
2449{
7def8754
AJ
2450 ARMCPU *cpu = env_archcpu(env);
2451
2452 return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) / gt_cntfrq_period_ns(cpu);
55d284af
PM
2453}
2454
2455static void gt_recalc_timer(ARMCPU *cpu, int timeridx)
2456{
2457 ARMGenericTimer *gt = &cpu->env.cp15.c14_timer[timeridx];
2458
2459 if (gt->ctl & 1) {
2460 /* Timer enabled: calculate and set current ISTATUS, irq, and
2461 * reset timer to when ISTATUS next has to change
2462 */
edac4d8a
EI
2463 uint64_t offset = timeridx == GTIMER_VIRT ?
2464 cpu->env.cp15.cntvoff_el2 : 0;
55d284af
PM
2465 uint64_t count = gt_get_countervalue(&cpu->env);
2466 /* Note that this must be unsigned 64 bit arithmetic: */
edac4d8a 2467 int istatus = count - offset >= gt->cval;
55d284af 2468 uint64_t nexttick;
194cbc49 2469 int irqstate;
55d284af
PM
2470
2471 gt->ctl = deposit32(gt->ctl, 2, 1, istatus);
194cbc49
PM
2472
2473 irqstate = (istatus && !(gt->ctl & 2));
2474 qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate);
2475
55d284af
PM
2476 if (istatus) {
2477 /* Next transition is when count rolls back over to zero */
2478 nexttick = UINT64_MAX;
2479 } else {
2480 /* Next transition is when we hit cval */
edac4d8a 2481 nexttick = gt->cval + offset;
55d284af
PM
2482 }
2483 /* Note that the desired next expiry time might be beyond the
2484 * signed-64-bit range of a QEMUTimer -- in this case we just
2485 * set the timer for as far in the future as possible. When the
2486 * timer expires we will reset the timer for any remaining period.
2487 */
7def8754 2488 if (nexttick > INT64_MAX / gt_cntfrq_period_ns(cpu)) {
4a0245b6
AJ
2489 timer_mod_ns(cpu->gt_timer[timeridx], INT64_MAX);
2490 } else {
2491 timer_mod(cpu->gt_timer[timeridx], nexttick);
55d284af 2492 }
194cbc49 2493 trace_arm_gt_recalc(timeridx, irqstate, nexttick);
55d284af
PM
2494 } else {
2495 /* Timer disabled: ISTATUS and timer output always clear */
2496 gt->ctl &= ~4;
2497 qemu_set_irq(cpu->gt_timer_outputs[timeridx], 0);
bc72ad67 2498 timer_del(cpu->gt_timer[timeridx]);
194cbc49 2499 trace_arm_gt_recalc_disabled(timeridx);
55d284af
PM
2500 }
2501}
2502
0e3eca4c
EI
2503static void gt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri,
2504 int timeridx)
55d284af 2505{
2fc0cc0e 2506 ARMCPU *cpu = env_archcpu(env);
55d284af 2507
bc72ad67 2508 timer_del(cpu->gt_timer[timeridx]);
55d284af
PM
2509}
2510
c4241c7d 2511static uint64_t gt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
55d284af 2512{
c4241c7d 2513 return gt_get_countervalue(env);
55d284af
PM
2514}
2515
53d1f856
RH
2516static uint64_t gt_virt_cnt_offset(CPUARMState *env)
2517{
2518 uint64_t hcr;
2519
2520 switch (arm_current_el(env)) {
2521 case 2:
2522 hcr = arm_hcr_el2_eff(env);
2523 if (hcr & HCR_E2H) {
2524 return 0;
2525 }
2526 break;
2527 case 0:
2528 hcr = arm_hcr_el2_eff(env);
2529 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2530 return 0;
2531 }
2532 break;
2533 }
2534
2535 return env->cp15.cntvoff_el2;
2536}
2537
edac4d8a
EI
2538static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
2539{
53d1f856 2540 return gt_get_countervalue(env) - gt_virt_cnt_offset(env);
edac4d8a
EI
2541}
2542
c4241c7d 2543static void gt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
0e3eca4c 2544 int timeridx,
c4241c7d 2545 uint64_t value)
55d284af 2546{
194cbc49 2547 trace_arm_gt_cval_write(timeridx, value);
55d284af 2548 env->cp15.c14_timer[timeridx].cval = value;
2fc0cc0e 2549 gt_recalc_timer(env_archcpu(env), timeridx);
55d284af 2550}
c4241c7d 2551
0e3eca4c
EI
2552static uint64_t gt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri,
2553 int timeridx)
55d284af 2554{
53d1f856
RH
2555 uint64_t offset = 0;
2556
2557 switch (timeridx) {
2558 case GTIMER_VIRT:
2559 offset = gt_virt_cnt_offset(env);
2560 break;
2561 }
55d284af 2562
c4241c7d 2563 return (uint32_t)(env->cp15.c14_timer[timeridx].cval -
edac4d8a 2564 (gt_get_countervalue(env) - offset));
55d284af
PM
2565}
2566
c4241c7d 2567static void gt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
0e3eca4c 2568 int timeridx,
c4241c7d 2569 uint64_t value)
55d284af 2570{
53d1f856
RH
2571 uint64_t offset = 0;
2572
2573 switch (timeridx) {
2574 case GTIMER_VIRT:
2575 offset = gt_virt_cnt_offset(env);
2576 break;
2577 }
55d284af 2578
194cbc49 2579 trace_arm_gt_tval_write(timeridx, value);
edac4d8a 2580 env->cp15.c14_timer[timeridx].cval = gt_get_countervalue(env) - offset +
18084b2f 2581 sextract64(value, 0, 32);
2fc0cc0e 2582 gt_recalc_timer(env_archcpu(env), timeridx);
55d284af
PM
2583}
2584
c4241c7d 2585static void gt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
0e3eca4c 2586 int timeridx,
c4241c7d 2587 uint64_t value)
55d284af 2588{
2fc0cc0e 2589 ARMCPU *cpu = env_archcpu(env);
55d284af
PM
2590 uint32_t oldval = env->cp15.c14_timer[timeridx].ctl;
2591
194cbc49 2592 trace_arm_gt_ctl_write(timeridx, value);
d3afacc7 2593 env->cp15.c14_timer[timeridx].ctl = deposit64(oldval, 0, 2, value);
55d284af
PM
2594 if ((oldval ^ value) & 1) {
2595 /* Enable toggled */
2596 gt_recalc_timer(cpu, timeridx);
d3afacc7 2597 } else if ((oldval ^ value) & 2) {
55d284af
PM
2598 /* IMASK toggled: don't need to recalculate,
2599 * just set the interrupt line based on ISTATUS
2600 */
194cbc49
PM
2601 int irqstate = (oldval & 4) && !(value & 2);
2602
2603 trace_arm_gt_imask_toggle(timeridx, irqstate);
2604 qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate);
55d284af 2605 }
55d284af
PM
2606}
2607
0e3eca4c
EI
2608static void gt_phys_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2609{
2610 gt_timer_reset(env, ri, GTIMER_PHYS);
2611}
2612
2613static void gt_phys_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2614 uint64_t value)
2615{
2616 gt_cval_write(env, ri, GTIMER_PHYS, value);
2617}
2618
2619static uint64_t gt_phys_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2620{
2621 return gt_tval_read(env, ri, GTIMER_PHYS);
2622}
2623
2624static void gt_phys_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2625 uint64_t value)
2626{
2627 gt_tval_write(env, ri, GTIMER_PHYS, value);
2628}
2629
2630static void gt_phys_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2631 uint64_t value)
2632{
2633 gt_ctl_write(env, ri, GTIMER_PHYS, value);
2634}
2635
2636static void gt_virt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2637{
2638 gt_timer_reset(env, ri, GTIMER_VIRT);
2639}
2640
2641static void gt_virt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2642 uint64_t value)
2643{
2644 gt_cval_write(env, ri, GTIMER_VIRT, value);
2645}
2646
2647static uint64_t gt_virt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2648{
2649 return gt_tval_read(env, ri, GTIMER_VIRT);
2650}
2651
2652static void gt_virt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2653 uint64_t value)
2654{
2655 gt_tval_write(env, ri, GTIMER_VIRT, value);
2656}
2657
2658static void gt_virt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2659 uint64_t value)
2660{
2661 gt_ctl_write(env, ri, GTIMER_VIRT, value);
2662}
2663
edac4d8a
EI
2664static void gt_cntvoff_write(CPUARMState *env, const ARMCPRegInfo *ri,
2665 uint64_t value)
2666{
2fc0cc0e 2667 ARMCPU *cpu = env_archcpu(env);
edac4d8a 2668
194cbc49 2669 trace_arm_gt_cntvoff_write(value);
edac4d8a
EI
2670 raw_write(env, ri, value);
2671 gt_recalc_timer(cpu, GTIMER_VIRT);
2672}
2673
b0e66d95
EI
2674static void gt_hyp_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2675{
2676 gt_timer_reset(env, ri, GTIMER_HYP);
2677}
2678
2679static void gt_hyp_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2680 uint64_t value)
2681{
2682 gt_cval_write(env, ri, GTIMER_HYP, value);
2683}
2684
2685static uint64_t gt_hyp_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2686{
2687 return gt_tval_read(env, ri, GTIMER_HYP);
2688}
2689
2690static void gt_hyp_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2691 uint64_t value)
2692{
2693 gt_tval_write(env, ri, GTIMER_HYP, value);
2694}
2695
2696static void gt_hyp_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2697 uint64_t value)
2698{
2699 gt_ctl_write(env, ri, GTIMER_HYP, value);
2700}
2701
b4d3978c
PM
2702static void gt_sec_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2703{
2704 gt_timer_reset(env, ri, GTIMER_SEC);
2705}
2706
2707static void gt_sec_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2708 uint64_t value)
2709{
2710 gt_cval_write(env, ri, GTIMER_SEC, value);
2711}
2712
2713static uint64_t gt_sec_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2714{
2715 return gt_tval_read(env, ri, GTIMER_SEC);
2716}
2717
2718static void gt_sec_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2719 uint64_t value)
2720{
2721 gt_tval_write(env, ri, GTIMER_SEC, value);
2722}
2723
2724static void gt_sec_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2725 uint64_t value)
2726{
2727 gt_ctl_write(env, ri, GTIMER_SEC, value);
2728}
2729
55d284af
PM
2730void arm_gt_ptimer_cb(void *opaque)
2731{
2732 ARMCPU *cpu = opaque;
2733
2734 gt_recalc_timer(cpu, GTIMER_PHYS);
2735}
2736
2737void arm_gt_vtimer_cb(void *opaque)
2738{
2739 ARMCPU *cpu = opaque;
2740
2741 gt_recalc_timer(cpu, GTIMER_VIRT);
2742}
2743
b0e66d95
EI
2744void arm_gt_htimer_cb(void *opaque)
2745{
2746 ARMCPU *cpu = opaque;
2747
2748 gt_recalc_timer(cpu, GTIMER_HYP);
2749}
2750
b4d3978c
PM
2751void arm_gt_stimer_cb(void *opaque)
2752{
2753 ARMCPU *cpu = opaque;
2754
2755 gt_recalc_timer(cpu, GTIMER_SEC);
2756}
2757
96eec6b2
AJ
2758static void arm_gt_cntfrq_reset(CPUARMState *env, const ARMCPRegInfo *opaque)
2759{
2760 ARMCPU *cpu = env_archcpu(env);
2761
2762 cpu->env.cp15.c14_cntfrq = cpu->gt_cntfrq_hz;
2763}
2764
55d284af
PM
2765static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
2766 /* Note that CNTFRQ is purely reads-as-written for the benefit
2767 * of software; writing it doesn't actually change the timer frequency.
2768 * Our reset value matches the fixed frequency we implement the timer at.
2769 */
2770 { .name = "CNTFRQ", .cp = 15, .crn = 14, .crm = 0, .opc1 = 0, .opc2 = 0,
7a0e58fa 2771 .type = ARM_CP_ALIAS,
a7adc4b7
PM
2772 .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
2773 .fieldoffset = offsetoflow32(CPUARMState, cp15.c14_cntfrq),
a7adc4b7
PM
2774 },
2775 { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
2776 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
2777 .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
55d284af 2778 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
96eec6b2 2779 .resetfn = arm_gt_cntfrq_reset,
55d284af
PM
2780 },
2781 /* overall control: mostly access permissions */
a7adc4b7
PM
2782 { .name = "CNTKCTL", .state = ARM_CP_STATE_BOTH,
2783 .opc0 = 3, .opc1 = 0, .crn = 14, .crm = 1, .opc2 = 0,
55d284af
PM
2784 .access = PL1_RW,
2785 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntkctl),
2786 .resetvalue = 0,
2787 },
2788 /* per-timer control */
2789 { .name = "CNTP_CTL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
9ff9dd3c 2790 .secure = ARM_CP_SECSTATE_NS,
daf1dc5f 2791 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
a7adc4b7
PM
2792 .accessfn = gt_ptimer_access,
2793 .fieldoffset = offsetoflow32(CPUARMState,
2794 cp15.c14_timer[GTIMER_PHYS].ctl),
0e3eca4c 2795 .writefn = gt_phys_ctl_write, .raw_writefn = raw_write,
a7adc4b7 2796 },
9c513e78 2797 { .name = "CNTP_CTL_S",
9ff9dd3c
PM
2798 .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
2799 .secure = ARM_CP_SECSTATE_S,
daf1dc5f 2800 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
9ff9dd3c
PM
2801 .accessfn = gt_ptimer_access,
2802 .fieldoffset = offsetoflow32(CPUARMState,
2803 cp15.c14_timer[GTIMER_SEC].ctl),
2804 .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
2805 },
a7adc4b7
PM
2806 { .name = "CNTP_CTL_EL0", .state = ARM_CP_STATE_AA64,
2807 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 1,
daf1dc5f 2808 .type = ARM_CP_IO, .access = PL0_RW,
a7adc4b7 2809 .accessfn = gt_ptimer_access,
55d284af
PM
2810 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
2811 .resetvalue = 0,
0e3eca4c 2812 .writefn = gt_phys_ctl_write, .raw_writefn = raw_write,
55d284af
PM
2813 },
2814 { .name = "CNTV_CTL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 1,
daf1dc5f 2815 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
a7adc4b7
PM
2816 .accessfn = gt_vtimer_access,
2817 .fieldoffset = offsetoflow32(CPUARMState,
2818 cp15.c14_timer[GTIMER_VIRT].ctl),
0e3eca4c 2819 .writefn = gt_virt_ctl_write, .raw_writefn = raw_write,
a7adc4b7
PM
2820 },
2821 { .name = "CNTV_CTL_EL0", .state = ARM_CP_STATE_AA64,
2822 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 1,
daf1dc5f 2823 .type = ARM_CP_IO, .access = PL0_RW,
a7adc4b7 2824 .accessfn = gt_vtimer_access,
55d284af
PM
2825 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
2826 .resetvalue = 0,
0e3eca4c 2827 .writefn = gt_virt_ctl_write, .raw_writefn = raw_write,
55d284af
PM
2828 },
2829 /* TimerValue views: a 32 bit downcounting view of the underlying state */
2830 { .name = "CNTP_TVAL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
9ff9dd3c 2831 .secure = ARM_CP_SECSTATE_NS,
daf1dc5f 2832 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
00108f2d 2833 .accessfn = gt_ptimer_access,
0e3eca4c 2834 .readfn = gt_phys_tval_read, .writefn = gt_phys_tval_write,
55d284af 2835 },
9c513e78 2836 { .name = "CNTP_TVAL_S",
9ff9dd3c
PM
2837 .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
2838 .secure = ARM_CP_SECSTATE_S,
daf1dc5f 2839 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
9ff9dd3c
PM
2840 .accessfn = gt_ptimer_access,
2841 .readfn = gt_sec_tval_read, .writefn = gt_sec_tval_write,
2842 },
a7adc4b7
PM
2843 { .name = "CNTP_TVAL_EL0", .state = ARM_CP_STATE_AA64,
2844 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 0,
daf1dc5f 2845 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
0e3eca4c
EI
2846 .accessfn = gt_ptimer_access, .resetfn = gt_phys_timer_reset,
2847 .readfn = gt_phys_tval_read, .writefn = gt_phys_tval_write,
a7adc4b7 2848 },
55d284af 2849 { .name = "CNTV_TVAL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 0,
daf1dc5f 2850 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
00108f2d 2851 .accessfn = gt_vtimer_access,
0e3eca4c 2852 .readfn = gt_virt_tval_read, .writefn = gt_virt_tval_write,
55d284af 2853 },
a7adc4b7
PM
2854 { .name = "CNTV_TVAL_EL0", .state = ARM_CP_STATE_AA64,
2855 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 0,
daf1dc5f 2856 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
0e3eca4c
EI
2857 .accessfn = gt_vtimer_access, .resetfn = gt_virt_timer_reset,
2858 .readfn = gt_virt_tval_read, .writefn = gt_virt_tval_write,
a7adc4b7 2859 },
55d284af
PM
2860 /* The counter itself */
2861 { .name = "CNTPCT", .cp = 15, .crm = 14, .opc1 = 0,
7a0e58fa 2862 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
00108f2d 2863 .accessfn = gt_pct_access,
a7adc4b7
PM
2864 .readfn = gt_cnt_read, .resetfn = arm_cp_reset_ignore,
2865 },
2866 { .name = "CNTPCT_EL0", .state = ARM_CP_STATE_AA64,
2867 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 1,
7a0e58fa 2868 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
d57b9ee8 2869 .accessfn = gt_pct_access, .readfn = gt_cnt_read,
55d284af
PM
2870 },
2871 { .name = "CNTVCT", .cp = 15, .crm = 14, .opc1 = 1,
7a0e58fa 2872 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
00108f2d 2873 .accessfn = gt_vct_access,
edac4d8a 2874 .readfn = gt_virt_cnt_read, .resetfn = arm_cp_reset_ignore,
a7adc4b7
PM
2875 },
2876 { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
2877 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
7a0e58fa 2878 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
d57b9ee8 2879 .accessfn = gt_vct_access, .readfn = gt_virt_cnt_read,
55d284af
PM
2880 },
2881 /* Comparison value, indicating when the timer goes off */
2882 { .name = "CNTP_CVAL", .cp = 15, .crm = 14, .opc1 = 2,
9ff9dd3c 2883 .secure = ARM_CP_SECSTATE_NS,
daf1dc5f 2884 .access = PL0_RW,
7a0e58fa 2885 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
55d284af 2886 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
b061a82b 2887 .accessfn = gt_ptimer_access,
0e3eca4c 2888 .writefn = gt_phys_cval_write, .raw_writefn = raw_write,
a7adc4b7 2889 },
9c513e78 2890 { .name = "CNTP_CVAL_S", .cp = 15, .crm = 14, .opc1 = 2,
9ff9dd3c 2891 .secure = ARM_CP_SECSTATE_S,
daf1dc5f 2892 .access = PL0_RW,
9ff9dd3c
PM
2893 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
2894 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
2895 .accessfn = gt_ptimer_access,
2896 .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
2897 },
a7adc4b7
PM
2898 { .name = "CNTP_CVAL_EL0", .state = ARM_CP_STATE_AA64,
2899 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 2,
daf1dc5f 2900 .access = PL0_RW,
a7adc4b7
PM
2901 .type = ARM_CP_IO,
2902 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
12cde08a 2903 .resetvalue = 0, .accessfn = gt_ptimer_access,
0e3eca4c 2904 .writefn = gt_phys_cval_write, .raw_writefn = raw_write,
55d284af
PM
2905 },
2906 { .name = "CNTV_CVAL", .cp = 15, .crm = 14, .opc1 = 3,
daf1dc5f 2907 .access = PL0_RW,
7a0e58fa 2908 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
55d284af 2909 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
b061a82b 2910 .accessfn = gt_vtimer_access,
0e3eca4c 2911 .writefn = gt_virt_cval_write, .raw_writefn = raw_write,
a7adc4b7
PM
2912 },
2913 { .name = "CNTV_CVAL_EL0", .state = ARM_CP_STATE_AA64,
2914 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 2,
daf1dc5f 2915 .access = PL0_RW,
a7adc4b7
PM
2916 .type = ARM_CP_IO,
2917 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
2918 .resetvalue = 0, .accessfn = gt_vtimer_access,
0e3eca4c 2919 .writefn = gt_virt_cval_write, .raw_writefn = raw_write,
55d284af 2920 },
b4d3978c
PM
2921 /* Secure timer -- this is actually restricted to only EL3
2922 * and configurably Secure-EL1 via the accessfn.
2923 */
2924 { .name = "CNTPS_TVAL_EL1", .state = ARM_CP_STATE_AA64,
2925 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 0,
2926 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW,
2927 .accessfn = gt_stimer_access,
2928 .readfn = gt_sec_tval_read,
2929 .writefn = gt_sec_tval_write,
2930 .resetfn = gt_sec_timer_reset,
2931 },
2932 { .name = "CNTPS_CTL_EL1", .state = ARM_CP_STATE_AA64,
2933 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 1,
2934 .type = ARM_CP_IO, .access = PL1_RW,
2935 .accessfn = gt_stimer_access,
2936 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].ctl),
2937 .resetvalue = 0,
2938 .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
2939 },
2940 { .name = "CNTPS_CVAL_EL1", .state = ARM_CP_STATE_AA64,
2941 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 2,
2942 .type = ARM_CP_IO, .access = PL1_RW,
2943 .accessfn = gt_stimer_access,
2944 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
2945 .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
2946 },
55d284af
PM
2947 REGINFO_SENTINEL
2948};
2949
2950#else
26c4a83b
AB
2951
2952/* In user-mode most of the generic timer registers are inaccessible
2953 * however modern kernels (4.12+) allow access to cntvct_el0
55d284af 2954 */
26c4a83b
AB
2955
2956static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
2957{
7def8754
AJ
2958 ARMCPU *cpu = env_archcpu(env);
2959
26c4a83b
AB
2960 /* Currently we have no support for QEMUTimer in linux-user so we
2961 * can't call gt_get_countervalue(env), instead we directly
2962 * call the lower level functions.
2963 */
7def8754 2964 return cpu_get_clock() / gt_cntfrq_period_ns(cpu);
26c4a83b
AB
2965}
2966
6cc7a3ae 2967static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
26c4a83b
AB
2968 { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
2969 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
2970 .type = ARM_CP_CONST, .access = PL0_R /* no PL1_RW in linux-user */,
2971 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
2972 .resetvalue = NANOSECONDS_PER_SECOND / GTIMER_SCALE,
2973 },
2974 { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
2975 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
2976 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2977 .readfn = gt_virt_cnt_read,
2978 },
6cc7a3ae
PM
2979 REGINFO_SENTINEL
2980};
2981
55d284af
PM
2982#endif
2983
c4241c7d 2984static void par_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
4a501606 2985{
891a2fe7 2986 if (arm_feature(env, ARM_FEATURE_LPAE)) {
8d5c773e 2987 raw_write(env, ri, value);
891a2fe7 2988 } else if (arm_feature(env, ARM_FEATURE_V7)) {
8d5c773e 2989 raw_write(env, ri, value & 0xfffff6ff);
4a501606 2990 } else {
8d5c773e 2991 raw_write(env, ri, value & 0xfffff1ff);
4a501606 2992 }
4a501606
PM
2993}
2994
2995#ifndef CONFIG_USER_ONLY
2996/* get_phys_addr() isn't present for user-mode-only targets */
702a9357 2997
3f208fd7
PM
2998static CPAccessResult ats_access(CPUARMState *env, const ARMCPRegInfo *ri,
2999 bool isread)
92611c00
PM
3000{
3001 if (ri->opc2 & 4) {
87562e4f
PM
3002 /* The ATS12NSO* operations must trap to EL3 if executed in
3003 * Secure EL1 (which can only happen if EL3 is AArch64).
3004 * They are simply UNDEF if executed from NS EL1.
3005 * They function normally from EL2 or EL3.
92611c00 3006 */
87562e4f
PM
3007 if (arm_current_el(env) == 1) {
3008 if (arm_is_secure_below_el3(env)) {
3009 return CP_ACCESS_TRAP_UNCATEGORIZED_EL3;
3010 }
3011 return CP_ACCESS_TRAP_UNCATEGORIZED;
3012 }
92611c00
PM
3013 }
3014 return CP_ACCESS_OK;
3015}
3016
060e8a48 3017static uint64_t do_ats_write(CPUARMState *env, uint64_t value,
03ae85f8 3018 MMUAccessType access_type, ARMMMUIdx mmu_idx)
4a501606 3019{
a8170e5e 3020 hwaddr phys_addr;
4a501606
PM
3021 target_ulong page_size;
3022 int prot;
b7cc4e82 3023 bool ret;
01c097f7 3024 uint64_t par64;
1313e2d7 3025 bool format64 = false;
8bf5b6a9 3026 MemTxAttrs attrs = {};
e14b5a23 3027 ARMMMUFaultInfo fi = {};
5b2d261d 3028 ARMCacheAttrs cacheattrs = {};
4a501606 3029
5b2d261d 3030 ret = get_phys_addr(env, value, access_type, mmu_idx, &phys_addr, &attrs,
bc52bfeb 3031 &prot, &page_size, &fi, &cacheattrs);
1313e2d7 3032
0710b2fa
PM
3033 if (ret) {
3034 /*
3035 * Some kinds of translation fault must cause exceptions rather
3036 * than being reported in the PAR.
3037 */
3038 int current_el = arm_current_el(env);
3039 int target_el;
3040 uint32_t syn, fsr, fsc;
3041 bool take_exc = false;
3042
3043 if (fi.s1ptw && current_el == 1 && !arm_is_secure(env)
2859d7b5
RH
3044 && (mmu_idx == ARMMMUIdx_Stage1_E1 ||
3045 mmu_idx == ARMMMUIdx_Stage1_E0)) {
0710b2fa
PM
3046 /*
3047 * Synchronous stage 2 fault on an access made as part of the
3048 * translation table walk for AT S1E0* or AT S1E1* insn
3049 * executed from NS EL1. If this is a synchronous external abort
3050 * and SCR_EL3.EA == 1, then we take a synchronous external abort
3051 * to EL3. Otherwise the fault is taken as an exception to EL2,
3052 * and HPFAR_EL2 holds the faulting IPA.
3053 */
3054 if (fi.type == ARMFault_SyncExternalOnWalk &&
3055 (env->cp15.scr_el3 & SCR_EA)) {
3056 target_el = 3;
3057 } else {
3058 env->cp15.hpfar_el2 = extract64(fi.s2addr, 12, 47) << 4;
3059 target_el = 2;
3060 }
3061 take_exc = true;
3062 } else if (fi.type == ARMFault_SyncExternalOnWalk) {
3063 /*
3064 * Synchronous external aborts during a translation table walk
3065 * are taken as Data Abort exceptions.
3066 */
3067 if (fi.stage2) {
3068 if (current_el == 3) {
3069 target_el = 3;
3070 } else {
3071 target_el = 2;
3072 }
3073 } else {
3074 target_el = exception_target_el(env);
3075 }
3076 take_exc = true;
3077 }
3078
3079 if (take_exc) {
3080 /* Construct FSR and FSC using same logic as arm_deliver_fault() */
3081 if (target_el == 2 || arm_el_is_aa64(env, target_el) ||
3082 arm_s1_regime_using_lpae_format(env, mmu_idx)) {
3083 fsr = arm_fi_to_lfsc(&fi);
3084 fsc = extract32(fsr, 0, 6);
3085 } else {
3086 fsr = arm_fi_to_sfsc(&fi);
3087 fsc = 0x3f;
3088 }
3089 /*
3090 * Report exception with ESR indicating a fault due to a
3091 * translation table walk for a cache maintenance instruction.
3092 */
3093 syn = syn_data_abort_no_iss(current_el == target_el,
3094 fi.ea, 1, fi.s1ptw, 1, fsc);
3095 env->exception.vaddress = value;
3096 env->exception.fsr = fsr;
3097 raise_exception(env, EXCP_DATA_ABORT, syn, target_el);
3098 }
3099 }
3100
1313e2d7
EI
3101 if (is_a64(env)) {
3102 format64 = true;
3103 } else if (arm_feature(env, ARM_FEATURE_LPAE)) {
3104 /*
3105 * ATS1Cxx:
3106 * * TTBCR.EAE determines whether the result is returned using the
3107 * 32-bit or the 64-bit PAR format
3108 * * Instructions executed in Hyp mode always use the 64bit format
3109 *
3110 * ATS1S2NSOxx uses the 64bit format if any of the following is true:
3111 * * The Non-secure TTBCR.EAE bit is set to 1
3112 * * The implementation includes EL2, and the value of HCR.VM is 1
3113 *
9d1bab33
PM
3114 * (Note that HCR.DC makes HCR.VM behave as if it is 1.)
3115 *
23463e0e 3116 * ATS1Hx always uses the 64bit format.
1313e2d7
EI
3117 */
3118 format64 = arm_s1_regime_using_lpae_format(env, mmu_idx);
3119
3120 if (arm_feature(env, ARM_FEATURE_EL2)) {
01b98b68 3121 if (mmu_idx == ARMMMUIdx_E10_0 || mmu_idx == ARMMMUIdx_E10_1) {
9d1bab33 3122 format64 |= env->cp15.hcr_el2 & (HCR_VM | HCR_DC);
1313e2d7
EI
3123 } else {
3124 format64 |= arm_current_el(env) == 2;
3125 }
3126 }
3127 }
3128
3129 if (format64) {
5efe9ed4 3130 /* Create a 64-bit PAR */
01c097f7 3131 par64 = (1 << 11); /* LPAE bit always set */
b7cc4e82 3132 if (!ret) {
702a9357 3133 par64 |= phys_addr & ~0xfffULL;
8bf5b6a9
PM
3134 if (!attrs.secure) {
3135 par64 |= (1 << 9); /* NS */
3136 }
5b2d261d
AB
3137 par64 |= (uint64_t)cacheattrs.attrs << 56; /* ATTR */
3138 par64 |= cacheattrs.shareability << 7; /* SH */
4a501606 3139 } else {
5efe9ed4
PM
3140 uint32_t fsr = arm_fi_to_lfsc(&fi);
3141
702a9357 3142 par64 |= 1; /* F */
b7cc4e82 3143 par64 |= (fsr & 0x3f) << 1; /* FS */
0f7b791b
PM
3144 if (fi.stage2) {
3145 par64 |= (1 << 9); /* S */
3146 }
3147 if (fi.s1ptw) {
3148 par64 |= (1 << 8); /* PTW */
3149 }
4a501606
PM
3150 }
3151 } else {
b7cc4e82 3152 /* fsr is a DFSR/IFSR value for the short descriptor
702a9357
PM
3153 * translation table format (with WnR always clear).
3154 * Convert it to a 32-bit PAR.
3155 */
b7cc4e82 3156 if (!ret) {
702a9357
PM
3157 /* We do not set any attribute bits in the PAR */
3158 if (page_size == (1 << 24)
3159 && arm_feature(env, ARM_FEATURE_V7)) {
01c097f7 3160 par64 = (phys_addr & 0xff000000) | (1 << 1);
702a9357 3161 } else {
01c097f7 3162 par64 = phys_addr & 0xfffff000;
702a9357 3163 }
8bf5b6a9
PM
3164 if (!attrs.secure) {
3165 par64 |= (1 << 9); /* NS */
3166 }
702a9357 3167 } else {
5efe9ed4
PM
3168 uint32_t fsr = arm_fi_to_sfsc(&fi);
3169
b7cc4e82
PC
3170 par64 = ((fsr & (1 << 10)) >> 5) | ((fsr & (1 << 12)) >> 6) |
3171 ((fsr & 0xf) << 1) | 1;
702a9357 3172 }
4a501606 3173 }
060e8a48
PM
3174 return par64;
3175}
3176
3177static void ats_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
3178{
03ae85f8 3179 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
060e8a48 3180 uint64_t par64;
d3649702
PM
3181 ARMMMUIdx mmu_idx;
3182 int el = arm_current_el(env);
3183 bool secure = arm_is_secure_below_el3(env);
060e8a48 3184
d3649702
PM
3185 switch (ri->opc2 & 6) {
3186 case 0:
3187 /* stage 1 current state PL1: ATS1CPR, ATS1CPW */
3188 switch (el) {
3189 case 3:
127b2b08 3190 mmu_idx = ARMMMUIdx_SE3;
d3649702
PM
3191 break;
3192 case 2:
2859d7b5 3193 mmu_idx = ARMMMUIdx_Stage1_E1;
d3649702
PM
3194 break;
3195 case 1:
fba37aed 3196 mmu_idx = secure ? ARMMMUIdx_SE10_1 : ARMMMUIdx_Stage1_E1;
d3649702
PM
3197 break;
3198 default:
3199 g_assert_not_reached();
3200 }
3201 break;
3202 case 2:
3203 /* stage 1 current state PL0: ATS1CUR, ATS1CUW */
3204 switch (el) {
3205 case 3:
fba37aed 3206 mmu_idx = ARMMMUIdx_SE10_0;
d3649702
PM
3207 break;
3208 case 2:
2859d7b5 3209 mmu_idx = ARMMMUIdx_Stage1_E0;
d3649702
PM
3210 break;
3211 case 1:
fba37aed 3212 mmu_idx = secure ? ARMMMUIdx_SE10_0 : ARMMMUIdx_Stage1_E0;
d3649702
PM
3213 break;
3214 default:
3215 g_assert_not_reached();
3216 }
3217 break;
3218 case 4:
3219 /* stage 1+2 NonSecure PL1: ATS12NSOPR, ATS12NSOPW */
01b98b68 3220 mmu_idx = ARMMMUIdx_E10_1;
d3649702
PM
3221 break;
3222 case 6:
3223 /* stage 1+2 NonSecure PL0: ATS12NSOUR, ATS12NSOUW */
01b98b68 3224 mmu_idx = ARMMMUIdx_E10_0;
d3649702
PM
3225 break;
3226 default:
3227 g_assert_not_reached();
3228 }
3229
3230 par64 = do_ats_write(env, value, access_type, mmu_idx);
01c097f7
FA
3231
3232 A32_BANKED_CURRENT_REG_SET(env, par, par64);
4a501606 3233}
060e8a48 3234
14db7fe0
PM
3235static void ats1h_write(CPUARMState *env, const ARMCPRegInfo *ri,
3236 uint64_t value)
3237{
03ae85f8 3238 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
14db7fe0
PM
3239 uint64_t par64;
3240
e013b741 3241 par64 = do_ats_write(env, value, access_type, ARMMMUIdx_E2);
14db7fe0
PM
3242
3243 A32_BANKED_CURRENT_REG_SET(env, par, par64);
3244}
3245
3f208fd7
PM
3246static CPAccessResult at_s1e2_access(CPUARMState *env, const ARMCPRegInfo *ri,
3247 bool isread)
2a47df95
PM
3248{
3249 if (arm_current_el(env) == 3 && !(env->cp15.scr_el3 & SCR_NS)) {
3250 return CP_ACCESS_TRAP;
3251 }
3252 return CP_ACCESS_OK;
3253}
3254
060e8a48
PM
3255static void ats_write64(CPUARMState *env, const ARMCPRegInfo *ri,
3256 uint64_t value)
3257{
03ae85f8 3258 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
d3649702
PM
3259 ARMMMUIdx mmu_idx;
3260 int secure = arm_is_secure_below_el3(env);
3261
3262 switch (ri->opc2 & 6) {
3263 case 0:
3264 switch (ri->opc1) {
3265 case 0: /* AT S1E1R, AT S1E1W */
fba37aed 3266 mmu_idx = secure ? ARMMMUIdx_SE10_1 : ARMMMUIdx_Stage1_E1;
d3649702
PM
3267 break;
3268 case 4: /* AT S1E2R, AT S1E2W */
e013b741 3269 mmu_idx = ARMMMUIdx_E2;
d3649702
PM
3270 break;
3271 case 6: /* AT S1E3R, AT S1E3W */
127b2b08 3272 mmu_idx = ARMMMUIdx_SE3;
d3649702
PM
3273 break;
3274 default:
3275 g_assert_not_reached();
3276 }
3277 break;
3278 case 2: /* AT S1E0R, AT S1E0W */
fba37aed 3279 mmu_idx = secure ? ARMMMUIdx_SE10_0 : ARMMMUIdx_Stage1_E0;
d3649702
PM
3280 break;
3281 case 4: /* AT S12E1R, AT S12E1W */
fba37aed 3282 mmu_idx = secure ? ARMMMUIdx_SE10_1 : ARMMMUIdx_E10_1;
d3649702
PM
3283 break;
3284 case 6: /* AT S12E0R, AT S12E0W */
fba37aed 3285 mmu_idx = secure ? ARMMMUIdx_SE10_0 : ARMMMUIdx_E10_0;
d3649702
PM
3286 break;
3287 default:
3288 g_assert_not_reached();
3289 }
060e8a48 3290
d3649702 3291 env->cp15.par_el[1] = do_ats_write(env, value, access_type, mmu_idx);
060e8a48 3292}
4a501606
PM
3293#endif
3294
3295static const ARMCPRegInfo vapa_cp_reginfo[] = {
3296 { .name = "PAR", .cp = 15, .crn = 7, .crm = 4, .opc1 = 0, .opc2 = 0,
3297 .access = PL1_RW, .resetvalue = 0,
01c097f7
FA
3298 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.par_s),
3299 offsetoflow32(CPUARMState, cp15.par_ns) },
4a501606
PM
3300 .writefn = par_write },
3301#ifndef CONFIG_USER_ONLY
87562e4f 3302 /* This underdecoding is safe because the reginfo is NO_RAW. */
4a501606 3303 { .name = "ATS", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = CP_ANY,
92611c00 3304 .access = PL1_W, .accessfn = ats_access,
0710b2fa 3305 .writefn = ats_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
4a501606
PM
3306#endif
3307 REGINFO_SENTINEL
3308};
3309
18032bec
PM
3310/* Return basic MPU access permission bits. */
3311static uint32_t simple_mpu_ap_bits(uint32_t val)
3312{
3313 uint32_t ret;
3314 uint32_t mask;
3315 int i;
3316 ret = 0;
3317 mask = 3;
3318 for (i = 0; i < 16; i += 2) {
3319 ret |= (val >> i) & mask;
3320 mask <<= 2;
3321 }
3322 return ret;
3323}
3324
3325/* Pad basic MPU access permission bits to extended format. */
3326static uint32_t extended_mpu_ap_bits(uint32_t val)
3327{
3328 uint32_t ret;
3329 uint32_t mask;
3330 int i;
3331 ret = 0;
3332 mask = 3;
3333 for (i = 0; i < 16; i += 2) {
3334 ret |= (val & mask) << i;
3335 mask <<= 2;
3336 }
3337 return ret;
3338}
3339
c4241c7d
PM
3340static void pmsav5_data_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
3341 uint64_t value)
18032bec 3342{
7e09797c 3343 env->cp15.pmsav5_data_ap = extended_mpu_ap_bits(value);
18032bec
PM
3344}
3345
c4241c7d 3346static uint64_t pmsav5_data_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
18032bec 3347{
7e09797c 3348 return simple_mpu_ap_bits(env->cp15.pmsav5_data_ap);
18032bec
PM
3349}
3350
c4241c7d
PM
3351static void pmsav5_insn_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
3352 uint64_t value)
18032bec 3353{
7e09797c 3354 env->cp15.pmsav5_insn_ap = extended_mpu_ap_bits(value);
18032bec
PM
3355}
3356
c4241c7d 3357static uint64_t pmsav5_insn_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
18032bec 3358{
7e09797c 3359 return simple_mpu_ap_bits(env->cp15.pmsav5_insn_ap);
18032bec
PM
3360}
3361
6cb0b013
PC
3362static uint64_t pmsav7_read(CPUARMState *env, const ARMCPRegInfo *ri)
3363{
3364 uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
3365
3366 if (!u32p) {
3367 return 0;
3368 }
3369
1bc04a88 3370 u32p += env->pmsav7.rnr[M_REG_NS];
6cb0b013
PC
3371 return *u32p;
3372}
3373
3374static void pmsav7_write(CPUARMState *env, const ARMCPRegInfo *ri,
3375 uint64_t value)
3376{
2fc0cc0e 3377 ARMCPU *cpu = env_archcpu(env);
6cb0b013
PC
3378 uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
3379
3380 if (!u32p) {
3381 return;
3382 }
3383
1bc04a88 3384 u32p += env->pmsav7.rnr[M_REG_NS];
d10eb08f 3385 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
6cb0b013
PC
3386 *u32p = value;
3387}
3388
6cb0b013
PC
3389static void pmsav7_rgnr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3390 uint64_t value)
3391{
2fc0cc0e 3392 ARMCPU *cpu = env_archcpu(env);
6cb0b013
PC
3393 uint32_t nrgs = cpu->pmsav7_dregion;
3394
3395 if (value >= nrgs) {
3396 qemu_log_mask(LOG_GUEST_ERROR,
3397 "PMSAv7 RGNR write >= # supported regions, %" PRIu32
3398 " > %" PRIu32 "\n", (uint32_t)value, nrgs);
3399 return;
3400 }
3401
3402 raw_write(env, ri, value);
3403}
3404
3405static const ARMCPRegInfo pmsav7_cp_reginfo[] = {
69ceea64
PM
3406 /* Reset for all these registers is handled in arm_cpu_reset(),
3407 * because the PMSAv7 is also used by M-profile CPUs, which do
3408 * not register cpregs but still need the state to be reset.
3409 */
6cb0b013
PC
3410 { .name = "DRBAR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 0,
3411 .access = PL1_RW, .type = ARM_CP_NO_RAW,
3412 .fieldoffset = offsetof(CPUARMState, pmsav7.drbar),
69ceea64
PM
3413 .readfn = pmsav7_read, .writefn = pmsav7_write,
3414 .resetfn = arm_cp_reset_ignore },
6cb0b013
PC
3415 { .name = "DRSR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 2,
3416 .access = PL1_RW, .type = ARM_CP_NO_RAW,
3417 .fieldoffset = offsetof(CPUARMState, pmsav7.drsr),
69ceea64
PM
3418 .readfn = pmsav7_read, .writefn = pmsav7_write,
3419 .resetfn = arm_cp_reset_ignore },
6cb0b013
PC
3420 { .name = "DRACR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 4,
3421 .access = PL1_RW, .type = ARM_CP_NO_RAW,
3422 .fieldoffset = offsetof(CPUARMState, pmsav7.dracr),
69ceea64
PM
3423 .readfn = pmsav7_read, .writefn = pmsav7_write,
3424 .resetfn = arm_cp_reset_ignore },
6cb0b013
PC
3425 { .name = "RGNR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 2, .opc2 = 0,
3426 .access = PL1_RW,
1bc04a88 3427 .fieldoffset = offsetof(CPUARMState, pmsav7.rnr[M_REG_NS]),
69ceea64
PM
3428 .writefn = pmsav7_rgnr_write,
3429 .resetfn = arm_cp_reset_ignore },
6cb0b013
PC
3430 REGINFO_SENTINEL
3431};
3432
18032bec
PM
3433static const ARMCPRegInfo pmsav5_cp_reginfo[] = {
3434 { .name = "DATA_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
7a0e58fa 3435 .access = PL1_RW, .type = ARM_CP_ALIAS,
7e09797c 3436 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
18032bec
PM
3437 .readfn = pmsav5_data_ap_read, .writefn = pmsav5_data_ap_write, },
3438 { .name = "INSN_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
7a0e58fa 3439 .access = PL1_RW, .type = ARM_CP_ALIAS,
7e09797c 3440 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
18032bec
PM
3441 .readfn = pmsav5_insn_ap_read, .writefn = pmsav5_insn_ap_write, },
3442 { .name = "DATA_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 2,
3443 .access = PL1_RW,
7e09797c
PM
3444 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
3445 .resetvalue = 0, },
18032bec
PM
3446 { .name = "INSN_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 3,
3447 .access = PL1_RW,
7e09797c
PM
3448 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
3449 .resetvalue = 0, },
ecce5c3c
PM
3450 { .name = "DCACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
3451 .access = PL1_RW,
3452 .fieldoffset = offsetof(CPUARMState, cp15.c2_data), .resetvalue = 0, },
3453 { .name = "ICACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 1,
3454 .access = PL1_RW,
3455 .fieldoffset = offsetof(CPUARMState, cp15.c2_insn), .resetvalue = 0, },
06d76f31 3456 /* Protection region base and size registers */
e508a92b
PM
3457 { .name = "946_PRBS0", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0,
3458 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3459 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[0]) },
3460 { .name = "946_PRBS1", .cp = 15, .crn = 6, .crm = 1, .opc1 = 0,
3461 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3462 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[1]) },
3463 { .name = "946_PRBS2", .cp = 15, .crn = 6, .crm = 2, .opc1 = 0,
3464 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3465 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[2]) },
3466 { .name = "946_PRBS3", .cp = 15, .crn = 6, .crm = 3, .opc1 = 0,
3467 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3468 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[3]) },
3469 { .name = "946_PRBS4", .cp = 15, .crn = 6, .crm = 4, .opc1 = 0,
3470 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3471 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[4]) },
3472 { .name = "946_PRBS5", .cp = 15, .crn = 6, .crm = 5, .opc1 = 0,
3473 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3474 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[5]) },
3475 { .name = "946_PRBS6", .cp = 15, .crn = 6, .crm = 6, .opc1 = 0,
3476 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3477 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[6]) },
3478 { .name = "946_PRBS7", .cp = 15, .crn = 6, .crm = 7, .opc1 = 0,
3479 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3480 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[7]) },
18032bec
PM
3481 REGINFO_SENTINEL
3482};
3483
c4241c7d
PM
3484static void vmsa_ttbcr_raw_write(CPUARMState *env, const ARMCPRegInfo *ri,
3485 uint64_t value)
ecce5c3c 3486{
11f136ee 3487 TCR *tcr = raw_ptr(env, ri);
2ebcebe2
PM
3488 int maskshift = extract32(value, 0, 3);
3489
e389be16
FA
3490 if (!arm_feature(env, ARM_FEATURE_V8)) {
3491 if (arm_feature(env, ARM_FEATURE_LPAE) && (value & TTBCR_EAE)) {
3492 /* Pre ARMv8 bits [21:19], [15:14] and [6:3] are UNK/SBZP when
3493 * using Long-desciptor translation table format */
3494 value &= ~((7 << 19) | (3 << 14) | (0xf << 3));
3495 } else if (arm_feature(env, ARM_FEATURE_EL3)) {
3496 /* In an implementation that includes the Security Extensions
3497 * TTBCR has additional fields PD0 [4] and PD1 [5] for
3498 * Short-descriptor translation table format.
3499 */
3500 value &= TTBCR_PD1 | TTBCR_PD0 | TTBCR_N;
3501 } else {
3502 value &= TTBCR_N;
3503 }
e42c4db3 3504 }
e389be16 3505
b6af0975 3506 /* Update the masks corresponding to the TCR bank being written
11f136ee 3507 * Note that we always calculate mask and base_mask, but
e42c4db3 3508 * they are only used for short-descriptor tables (ie if EAE is 0);
11f136ee
FA
3509 * for long-descriptor tables the TCR fields are used differently
3510 * and the mask and base_mask values are meaningless.
e42c4db3 3511 */
11f136ee
FA
3512 tcr->raw_tcr = value;
3513 tcr->mask = ~(((uint32_t)0xffffffffu) >> maskshift);
3514 tcr->base_mask = ~((uint32_t)0x3fffu >> maskshift);
ecce5c3c
PM
3515}
3516
c4241c7d
PM
3517static void vmsa_ttbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3518 uint64_t value)
d4e6df63 3519{
2fc0cc0e 3520 ARMCPU *cpu = env_archcpu(env);
ab638a32 3521 TCR *tcr = raw_ptr(env, ri);
00c8cb0a 3522
d4e6df63
PM
3523 if (arm_feature(env, ARM_FEATURE_LPAE)) {
3524 /* With LPAE the TTBCR could result in a change of ASID
3525 * via the TTBCR.A1 bit, so do a TLB flush.
3526 */
d10eb08f 3527 tlb_flush(CPU(cpu));
d4e6df63 3528 }
ab638a32
RH
3529 /* Preserve the high half of TCR_EL1, set via TTBCR2. */
3530 value = deposit64(tcr->raw_tcr, 0, 32, value);
c4241c7d 3531 vmsa_ttbcr_raw_write(env, ri, value);
d4e6df63
PM
3532}
3533
ecce5c3c
PM
3534static void vmsa_ttbcr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
3535{
11f136ee
FA
3536 TCR *tcr = raw_ptr(env, ri);
3537
3538 /* Reset both the TCR as well as the masks corresponding to the bank of
3539 * the TCR being reset.
3540 */
3541 tcr->raw_tcr = 0;
3542 tcr->mask = 0;
3543 tcr->base_mask = 0xffffc000u;
ecce5c3c
PM
3544}
3545
cb2e37df
PM
3546static void vmsa_tcr_el1_write(CPUARMState *env, const ARMCPRegInfo *ri,
3547 uint64_t value)
3548{
2fc0cc0e 3549 ARMCPU *cpu = env_archcpu(env);
11f136ee 3550 TCR *tcr = raw_ptr(env, ri);
00c8cb0a 3551
cb2e37df 3552 /* For AArch64 the A1 bit could result in a change of ASID, so TLB flush. */
d10eb08f 3553 tlb_flush(CPU(cpu));
11f136ee 3554 tcr->raw_tcr = value;
cb2e37df
PM
3555}
3556
327ed10f
PM
3557static void vmsa_ttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3558 uint64_t value)
3559{
93f379b0
RH
3560 /* If the ASID changes (with a 64-bit write), we must flush the TLB. */
3561 if (cpreg_field_is_64bit(ri) &&
3562 extract64(raw_read(env, ri) ^ value, 48, 16) != 0) {
2fc0cc0e 3563 ARMCPU *cpu = env_archcpu(env);
d10eb08f 3564 tlb_flush(CPU(cpu));
327ed10f
PM
3565 }
3566 raw_write(env, ri, value);
3567}
3568
ed30da8e
RH
3569static void vmsa_tcr_ttbr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
3570 uint64_t value)
3571{
3572 /* TODO: There are ASID fields in here with HCR_EL2.E2H */
3573 raw_write(env, ri, value);
3574}
3575
b698e9cf
EI
3576static void vttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3577 uint64_t value)
3578{
2fc0cc0e 3579 ARMCPU *cpu = env_archcpu(env);
b698e9cf
EI
3580 CPUState *cs = CPU(cpu);
3581
97fa9350
RH
3582 /*
3583 * A change in VMID to the stage2 page table (Stage2) invalidates
3584 * the combined stage 1&2 tlbs (EL10_1 and EL10_0).
3585 */
b698e9cf 3586 if (raw_read(env, ri) != value) {
0336cbf8 3587 tlb_flush_by_mmuidx(cs,
01b98b68
RH
3588 ARMMMUIdxBit_E10_1 |
3589 ARMMMUIdxBit_E10_0 |
97fa9350 3590 ARMMMUIdxBit_Stage2);
b698e9cf
EI
3591 raw_write(env, ri, value);
3592 }
3593}
3594
8e5d75c9 3595static const ARMCPRegInfo vmsa_pmsa_cp_reginfo[] = {
18032bec 3596 { .name = "DFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
7a0e58fa 3597 .access = PL1_RW, .type = ARM_CP_ALIAS,
4a7e2d73 3598 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dfsr_s),
b061a82b 3599 offsetoflow32(CPUARMState, cp15.dfsr_ns) }, },
18032bec 3600 { .name = "IFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
88ca1c2d
FA
3601 .access = PL1_RW, .resetvalue = 0,
3602 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.ifsr_s),
3603 offsetoflow32(CPUARMState, cp15.ifsr_ns) } },
8e5d75c9
PC
3604 { .name = "DFAR", .cp = 15, .opc1 = 0, .crn = 6, .crm = 0, .opc2 = 0,
3605 .access = PL1_RW, .resetvalue = 0,
3606 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.dfar_s),
3607 offsetof(CPUARMState, cp15.dfar_ns) } },
3608 { .name = "FAR_EL1", .state = ARM_CP_STATE_AA64,
3609 .opc0 = 3, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 0,
3610 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[1]),
3611 .resetvalue = 0, },
3612 REGINFO_SENTINEL
3613};
3614
3615static const ARMCPRegInfo vmsa_cp_reginfo[] = {
6cd8a264
RH
3616 { .name = "ESR_EL1", .state = ARM_CP_STATE_AA64,
3617 .opc0 = 3, .crn = 5, .crm = 2, .opc1 = 0, .opc2 = 0,
3618 .access = PL1_RW,
d81c519c 3619 .fieldoffset = offsetof(CPUARMState, cp15.esr_el[1]), .resetvalue = 0, },
327ed10f 3620 { .name = "TTBR0_EL1", .state = ARM_CP_STATE_BOTH,
7dd8c9af
FA
3621 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 0,
3622 .access = PL1_RW, .writefn = vmsa_ttbr_write, .resetvalue = 0,
3623 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
3624 offsetof(CPUARMState, cp15.ttbr0_ns) } },
327ed10f 3625 { .name = "TTBR1_EL1", .state = ARM_CP_STATE_BOTH,
7dd8c9af
FA
3626 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 1,
3627 .access = PL1_RW, .writefn = vmsa_ttbr_write, .resetvalue = 0,
3628 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
3629 offsetof(CPUARMState, cp15.ttbr1_ns) } },
cb2e37df
PM
3630 { .name = "TCR_EL1", .state = ARM_CP_STATE_AA64,
3631 .opc0 = 3, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
3632 .access = PL1_RW, .writefn = vmsa_tcr_el1_write,
3633 .resetfn = vmsa_ttbcr_reset, .raw_writefn = raw_write,
11f136ee 3634 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[1]) },
cb2e37df 3635 { .name = "TTBCR", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
7a0e58fa 3636 .access = PL1_RW, .type = ARM_CP_ALIAS, .writefn = vmsa_ttbcr_write,
b061a82b 3637 .raw_writefn = vmsa_ttbcr_raw_write,
11f136ee
FA
3638 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tcr_el[3]),
3639 offsetoflow32(CPUARMState, cp15.tcr_el[1])} },
18032bec
PM
3640 REGINFO_SENTINEL
3641};
3642
ab638a32
RH
3643/* Note that unlike TTBCR, writing to TTBCR2 does not require flushing
3644 * qemu tlbs nor adjusting cached masks.
3645 */
3646static const ARMCPRegInfo ttbcr2_reginfo = {
3647 .name = "TTBCR2", .cp = 15, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 3,
3648 .access = PL1_RW, .type = ARM_CP_ALIAS,
3649 .bank_fieldoffsets = { offsetofhigh32(CPUARMState, cp15.tcr_el[3]),
3650 offsetofhigh32(CPUARMState, cp15.tcr_el[1]) },
3651};
3652
c4241c7d
PM
3653static void omap_ticonfig_write(CPUARMState *env, const ARMCPRegInfo *ri,
3654 uint64_t value)
1047b9d7
PM
3655{
3656 env->cp15.c15_ticonfig = value & 0xe7;
3657 /* The OS_TYPE bit in this register changes the reported CPUID! */
3658 env->cp15.c0_cpuid = (value & (1 << 5)) ?
3659 ARM_CPUID_TI915T : ARM_CPUID_TI925T;
1047b9d7
PM
3660}
3661
c4241c7d
PM
3662static void omap_threadid_write(CPUARMState *env, const ARMCPRegInfo *ri,
3663 uint64_t value)
1047b9d7
PM
3664{
3665 env->cp15.c15_threadid = value & 0xffff;
1047b9d7
PM
3666}
3667
c4241c7d
PM
3668static void omap_wfi_write(CPUARMState *env, const ARMCPRegInfo *ri,
3669 uint64_t value)
1047b9d7
PM
3670{
3671 /* Wait-for-interrupt (deprecated) */
2fc0cc0e 3672 cpu_interrupt(env_cpu(env), CPU_INTERRUPT_HALT);
1047b9d7
PM
3673}
3674
c4241c7d
PM
3675static void omap_cachemaint_write(CPUARMState *env, const ARMCPRegInfo *ri,
3676 uint64_t value)
c4804214
PM
3677{
3678 /* On OMAP there are registers indicating the max/min index of dcache lines
3679 * containing a dirty line; cache flush operations have to reset these.
3680 */
3681 env->cp15.c15_i_max = 0x000;
3682 env->cp15.c15_i_min = 0xff0;
c4804214
PM
3683}
3684
18032bec
PM
3685static const ARMCPRegInfo omap_cp_reginfo[] = {
3686 { .name = "DFSR", .cp = 15, .crn = 5, .crm = CP_ANY,
3687 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_OVERRIDE,
d81c519c 3688 .fieldoffset = offsetoflow32(CPUARMState, cp15.esr_el[1]),
6cd8a264 3689 .resetvalue = 0, },
1047b9d7
PM
3690 { .name = "", .cp = 15, .crn = 15, .crm = 0, .opc1 = 0, .opc2 = 0,
3691 .access = PL1_RW, .type = ARM_CP_NOP },
3692 { .name = "TICONFIG", .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0,
3693 .access = PL1_RW,
3694 .fieldoffset = offsetof(CPUARMState, cp15.c15_ticonfig), .resetvalue = 0,
3695 .writefn = omap_ticonfig_write },
3696 { .name = "IMAX", .cp = 15, .crn = 15, .crm = 2, .opc1 = 0, .opc2 = 0,
3697 .access = PL1_RW,
3698 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_max), .resetvalue = 0, },
3699 { .name = "IMIN", .cp = 15, .crn = 15, .crm = 3, .opc1 = 0, .opc2 = 0,
3700 .access = PL1_RW, .resetvalue = 0xff0,
3701 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_min) },
3702 { .name = "THREADID", .cp = 15, .crn = 15, .crm = 4, .opc1 = 0, .opc2 = 0,
3703 .access = PL1_RW,
3704 .fieldoffset = offsetof(CPUARMState, cp15.c15_threadid), .resetvalue = 0,
3705 .writefn = omap_threadid_write },
3706 { .name = "TI925T_STATUS", .cp = 15, .crn = 15,
3707 .crm = 8, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
7a0e58fa 3708 .type = ARM_CP_NO_RAW,
1047b9d7
PM
3709 .readfn = arm_cp_read_zero, .writefn = omap_wfi_write, },
3710 /* TODO: Peripheral port remap register:
3711 * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt controller
3712 * base address at $rn & ~0xfff and map size of 0x200 << ($rn & 0xfff),
3713 * when MMU is off.
3714 */
c4804214 3715 { .name = "OMAP_CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
d4e6df63 3716 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
7a0e58fa 3717 .type = ARM_CP_OVERRIDE | ARM_CP_NO_RAW,
c4804214 3718 .writefn = omap_cachemaint_write },
34f90529
PM
3719 { .name = "C9", .cp = 15, .crn = 9,
3720 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW,
3721 .type = ARM_CP_CONST | ARM_CP_OVERRIDE, .resetvalue = 0 },
1047b9d7
PM
3722 REGINFO_SENTINEL
3723};
3724
c4241c7d
PM
3725static void xscale_cpar_write(CPUARMState *env, const ARMCPRegInfo *ri,
3726 uint64_t value)
1047b9d7 3727{
c0f4af17 3728 env->cp15.c15_cpar = value & 0x3fff;
1047b9d7
PM
3729}
3730
3731static const ARMCPRegInfo xscale_cp_reginfo[] = {
3732 { .name = "XSCALE_CPAR",
3733 .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
3734 .fieldoffset = offsetof(CPUARMState, cp15.c15_cpar), .resetvalue = 0,
3735 .writefn = xscale_cpar_write, },
2771db27
PM
3736 { .name = "XSCALE_AUXCR",
3737 .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 1, .access = PL1_RW,
3738 .fieldoffset = offsetof(CPUARMState, cp15.c1_xscaleauxcr),
3739 .resetvalue = 0, },
3b771579
PM
3740 /* XScale specific cache-lockdown: since we have no cache we NOP these
3741 * and hope the guest does not really rely on cache behaviour.
3742 */
3743 { .name = "XSCALE_LOCK_ICACHE_LINE",
3744 .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 0,
3745 .access = PL1_W, .type = ARM_CP_NOP },
3746 { .name = "XSCALE_UNLOCK_ICACHE",
3747 .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 1,
3748 .access = PL1_W, .type = ARM_CP_NOP },
3749 { .name = "XSCALE_DCACHE_LOCK",
3750 .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 0,
3751 .access = PL1_RW, .type = ARM_CP_NOP },
3752 { .name = "XSCALE_UNLOCK_DCACHE",
3753 .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 1,
3754 .access = PL1_W, .type = ARM_CP_NOP },
1047b9d7
PM
3755 REGINFO_SENTINEL
3756};
3757
3758static const ARMCPRegInfo dummy_c15_cp_reginfo[] = {
3759 /* RAZ/WI the whole crn=15 space, when we don't have a more specific
3760 * implementation of this implementation-defined space.
3761 * Ideally this should eventually disappear in favour of actually
3762 * implementing the correct behaviour for all cores.
3763 */
3764 { .name = "C15_IMPDEF", .cp = 15, .crn = 15,
3765 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
3671cd87 3766 .access = PL1_RW,
7a0e58fa 3767 .type = ARM_CP_CONST | ARM_CP_NO_RAW | ARM_CP_OVERRIDE,
d4e6df63 3768 .resetvalue = 0 },
18032bec
PM
3769 REGINFO_SENTINEL
3770};
3771
c4804214
PM
3772static const ARMCPRegInfo cache_dirty_status_cp_reginfo[] = {
3773 /* Cache status: RAZ because we have no cache so it's always clean */
3774 { .name = "CDSR", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 6,
7a0e58fa 3775 .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 3776 .resetvalue = 0 },
c4804214
PM
3777 REGINFO_SENTINEL
3778};
3779
3780static const ARMCPRegInfo cache_block_ops_cp_reginfo[] = {
3781 /* We never have a a block transfer operation in progress */
3782 { .name = "BXSR", .cp = 15, .crn = 7, .crm = 12, .opc1 = 0, .opc2 = 4,
7a0e58fa 3783 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 3784 .resetvalue = 0 },
30b05bba
PM
3785 /* The cache ops themselves: these all NOP for QEMU */
3786 { .name = "IICR", .cp = 15, .crm = 5, .opc1 = 0,
3787 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
3788 { .name = "IDCR", .cp = 15, .crm = 6, .opc1 = 0,
3789 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
3790 { .name = "CDCR", .cp = 15, .crm = 12, .opc1 = 0,
3791 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
3792 { .name = "PIR", .cp = 15, .crm = 12, .opc1 = 1,
3793 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
3794 { .name = "PDR", .cp = 15, .crm = 12, .opc1 = 2,
3795 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
3796 { .name = "CIDCR", .cp = 15, .crm = 14, .opc1 = 0,
3797 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
c4804214
PM
3798 REGINFO_SENTINEL
3799};
3800
3801static const ARMCPRegInfo cache_test_clean_cp_reginfo[] = {
3802 /* The cache test-and-clean instructions always return (1 << 30)
3803 * to indicate that there are no dirty cache lines.
3804 */
3805 { .name = "TC_DCACHE", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 3,
7a0e58fa 3806 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 3807 .resetvalue = (1 << 30) },
c4804214 3808 { .name = "TCI_DCACHE", .cp = 15, .crn = 7, .crm = 14, .opc1 = 0, .opc2 = 3,
7a0e58fa 3809 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 3810 .resetvalue = (1 << 30) },
c4804214
PM
3811 REGINFO_SENTINEL
3812};
3813
34f90529
PM
3814static const ARMCPRegInfo strongarm_cp_reginfo[] = {
3815 /* Ignore ReadBuffer accesses */
3816 { .name = "C9_READBUFFER", .cp = 15, .crn = 9,
3817 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
d4e6df63 3818 .access = PL1_RW, .resetvalue = 0,
7a0e58fa 3819 .type = ARM_CP_CONST | ARM_CP_OVERRIDE | ARM_CP_NO_RAW },
34f90529
PM
3820 REGINFO_SENTINEL
3821};
3822
731de9e6
EI
3823static uint64_t midr_read(CPUARMState *env, const ARMCPRegInfo *ri)
3824{
2fc0cc0e 3825 ARMCPU *cpu = env_archcpu(env);
731de9e6
EI
3826 unsigned int cur_el = arm_current_el(env);
3827 bool secure = arm_is_secure(env);
3828
3829 if (arm_feature(&cpu->env, ARM_FEATURE_EL2) && !secure && cur_el == 1) {
3830 return env->cp15.vpidr_el2;
3831 }
3832 return raw_read(env, ri);
3833}
3834
06a7e647 3835static uint64_t mpidr_read_val(CPUARMState *env)
81bdde9d 3836{
2fc0cc0e 3837 ARMCPU *cpu = env_archcpu(env);
eb5e1d3c
PF
3838 uint64_t mpidr = cpu->mp_affinity;
3839
81bdde9d 3840 if (arm_feature(env, ARM_FEATURE_V7MP)) {
78dbbbe4 3841 mpidr |= (1U << 31);
81bdde9d
PM
3842 /* Cores which are uniprocessor (non-coherent)
3843 * but still implement the MP extensions set
a8e81b31 3844 * bit 30. (For instance, Cortex-R5).
81bdde9d 3845 */
a8e81b31
PC
3846 if (cpu->mp_is_up) {
3847 mpidr |= (1u << 30);
3848 }
81bdde9d 3849 }
c4241c7d 3850 return mpidr;
81bdde9d
PM
3851}
3852
06a7e647
EI
3853static uint64_t mpidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
3854{
f0d574d6
EI
3855 unsigned int cur_el = arm_current_el(env);
3856 bool secure = arm_is_secure(env);
3857
3858 if (arm_feature(env, ARM_FEATURE_EL2) && !secure && cur_el == 1) {
3859 return env->cp15.vmpidr_el2;
3860 }
06a7e647
EI
3861 return mpidr_read_val(env);
3862}
3863
7ac681cf 3864static const ARMCPRegInfo lpae_cp_reginfo[] = {
a903c449 3865 /* NOP AMAIR0/1 */
b0fe2427
PM
3866 { .name = "AMAIR0", .state = ARM_CP_STATE_BOTH,
3867 .opc0 = 3, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 0,
a903c449 3868 .access = PL1_RW, .type = ARM_CP_CONST,
7ac681cf 3869 .resetvalue = 0 },
b0fe2427 3870 /* AMAIR1 is mapped to AMAIR_EL1[63:32] */
7ac681cf 3871 { .name = "AMAIR1", .cp = 15, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 1,
a903c449 3872 .access = PL1_RW, .type = ARM_CP_CONST,
7ac681cf 3873 .resetvalue = 0 },
891a2fe7 3874 { .name = "PAR", .cp = 15, .crm = 7, .opc1 = 0,
01c097f7
FA
3875 .access = PL1_RW, .type = ARM_CP_64BIT, .resetvalue = 0,
3876 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.par_s),
3877 offsetof(CPUARMState, cp15.par_ns)} },
891a2fe7 3878 { .name = "TTBR0", .cp = 15, .crm = 2, .opc1 = 0,
7a0e58fa 3879 .access = PL1_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
7dd8c9af
FA
3880 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
3881 offsetof(CPUARMState, cp15.ttbr0_ns) },
b061a82b 3882 .writefn = vmsa_ttbr_write, },
891a2fe7 3883 { .name = "TTBR1", .cp = 15, .crm = 2, .opc1 = 1,
7a0e58fa 3884 .access = PL1_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
7dd8c9af
FA
3885 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
3886 offsetof(CPUARMState, cp15.ttbr1_ns) },
b061a82b 3887 .writefn = vmsa_ttbr_write, },
7ac681cf
PM
3888 REGINFO_SENTINEL
3889};
3890
c4241c7d 3891static uint64_t aa64_fpcr_read(CPUARMState *env, const ARMCPRegInfo *ri)
b0d2b7d0 3892{
c4241c7d 3893 return vfp_get_fpcr(env);
b0d2b7d0
PM
3894}
3895
c4241c7d
PM
3896static void aa64_fpcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3897 uint64_t value)
b0d2b7d0
PM
3898{
3899 vfp_set_fpcr(env, value);
b0d2b7d0
PM
3900}
3901
c4241c7d 3902static uint64_t aa64_fpsr_read(CPUARMState *env, const ARMCPRegInfo *ri)
b0d2b7d0 3903{
c4241c7d 3904 return vfp_get_fpsr(env);
b0d2b7d0
PM
3905}
3906
c4241c7d
PM
3907static void aa64_fpsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3908 uint64_t value)
b0d2b7d0
PM
3909{
3910 vfp_set_fpsr(env, value);
b0d2b7d0
PM
3911}
3912
3f208fd7
PM
3913static CPAccessResult aa64_daif_access(CPUARMState *env, const ARMCPRegInfo *ri,
3914 bool isread)
c2b820fe 3915{
137feaa9 3916 if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_UMA)) {
c2b820fe
PM
3917 return CP_ACCESS_TRAP;
3918 }
3919 return CP_ACCESS_OK;
3920}
3921
3922static void aa64_daif_write(CPUARMState *env, const ARMCPRegInfo *ri,
3923 uint64_t value)
3924{
3925 env->daif = value & PSTATE_DAIF;
3926}
3927
8af35c37 3928static CPAccessResult aa64_cacheop_access(CPUARMState *env,
3f208fd7
PM
3929 const ARMCPRegInfo *ri,
3930 bool isread)
8af35c37
PM
3931{
3932 /* Cache invalidate/clean: NOP, but EL0 must UNDEF unless
3933 * SCTLR_EL1.UCI is set.
3934 */
137feaa9 3935 if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_UCI)) {
8af35c37
PM
3936 return CP_ACCESS_TRAP;
3937 }
3938 return CP_ACCESS_OK;
3939}
3940
dbb1fb27
AB
3941/* See: D4.7.2 TLB maintenance requirements and the TLB maintenance instructions
3942 * Page D4-1736 (DDI0487A.b)
3943 */
3944
b7e0730d
RH
3945static int vae1_tlbmask(CPUARMState *env)
3946{
3947 if (arm_is_secure_below_el3(env)) {
fba37aed 3948 return ARMMMUIdxBit_SE10_1 | ARMMMUIdxBit_SE10_0;
b7e0730d 3949 } else {
01b98b68 3950 return ARMMMUIdxBit_E10_1 | ARMMMUIdxBit_E10_0;
b7e0730d
RH
3951 }
3952}
3953
fd3ed969
PM
3954static void tlbi_aa64_vmalle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
3955 uint64_t value)
168aa23b 3956{
29a0af61 3957 CPUState *cs = env_cpu(env);
b7e0730d 3958 int mask = vae1_tlbmask(env);
dbb1fb27 3959
b7e0730d 3960 tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
168aa23b
PM
3961}
3962
b4ab8ce9
PM
3963static void tlbi_aa64_vmalle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
3964 uint64_t value)
3965{
29a0af61 3966 CPUState *cs = env_cpu(env);
b7e0730d 3967 int mask = vae1_tlbmask(env);
b4ab8ce9
PM
3968
3969 if (tlb_force_broadcast(env)) {
527db2be
RH
3970 tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
3971 } else {
3972 tlb_flush_by_mmuidx(cs, mask);
b4ab8ce9 3973 }
b4ab8ce9
PM
3974}
3975
90c19cdf 3976static int alle1_tlbmask(CPUARMState *env)
168aa23b 3977{
90c19cdf
RH
3978 /*
3979 * Note that the 'ALL' scope must invalidate both stage 1 and
fd3ed969
PM
3980 * stage 2 translations, whereas most other scopes only invalidate
3981 * stage 1 translations.
3982 */
fd3ed969 3983 if (arm_is_secure_below_el3(env)) {
fba37aed 3984 return ARMMMUIdxBit_SE10_1 | ARMMMUIdxBit_SE10_0;
90c19cdf 3985 } else if (arm_feature(env, ARM_FEATURE_EL2)) {
97fa9350 3986 return ARMMMUIdxBit_E10_1 | ARMMMUIdxBit_E10_0 | ARMMMUIdxBit_Stage2;
fd3ed969 3987 } else {
01b98b68 3988 return ARMMMUIdxBit_E10_1 | ARMMMUIdxBit_E10_0;
fd3ed969 3989 }
168aa23b
PM
3990}
3991
90c19cdf
RH
3992static void tlbi_aa64_alle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
3993 uint64_t value)
3994{
3995 CPUState *cs = env_cpu(env);
3996 int mask = alle1_tlbmask(env);
3997
3998 tlb_flush_by_mmuidx(cs, mask);
3999}
4000
fd3ed969 4001static void tlbi_aa64_alle2_write(CPUARMState *env, const ARMCPRegInfo *ri,
fa439fc5
PM
4002 uint64_t value)
4003{
2fc0cc0e 4004 ARMCPU *cpu = env_archcpu(env);
fd3ed969
PM
4005 CPUState *cs = CPU(cpu);
4006
e013b741 4007 tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_E2);
fd3ed969
PM
4008}
4009
43efaa33
PM
4010static void tlbi_aa64_alle3_write(CPUARMState *env, const ARMCPRegInfo *ri,
4011 uint64_t value)
4012{
2fc0cc0e 4013 ARMCPU *cpu = env_archcpu(env);
43efaa33
PM
4014 CPUState *cs = CPU(cpu);
4015
127b2b08 4016 tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_SE3);
43efaa33
PM
4017}
4018
fd3ed969
PM
4019static void tlbi_aa64_alle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4020 uint64_t value)
4021{
29a0af61 4022 CPUState *cs = env_cpu(env);
90c19cdf
RH
4023 int mask = alle1_tlbmask(env);
4024
4025 tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
fa439fc5
PM
4026}
4027
2bfb9d75
PM
4028static void tlbi_aa64_alle2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4029 uint64_t value)
4030{
29a0af61 4031 CPUState *cs = env_cpu(env);
2bfb9d75 4032
e013b741 4033 tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_E2);
2bfb9d75
PM
4034}
4035
43efaa33
PM
4036static void tlbi_aa64_alle3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4037 uint64_t value)
4038{
29a0af61 4039 CPUState *cs = env_cpu(env);
43efaa33 4040
127b2b08 4041 tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_SE3);
43efaa33
PM
4042}
4043
fd3ed969
PM
4044static void tlbi_aa64_vae2_write(CPUARMState *env, const ARMCPRegInfo *ri,
4045 uint64_t value)
fa439fc5 4046{
fd3ed969
PM
4047 /* Invalidate by VA, EL2
4048 * Currently handles both VAE2 and VALE2, since we don't support
4049 * flush-last-level-only.
4050 */
2fc0cc0e 4051 ARMCPU *cpu = env_archcpu(env);
fd3ed969
PM
4052 CPUState *cs = CPU(cpu);
4053 uint64_t pageaddr = sextract64(value << 12, 0, 56);
4054
e013b741 4055 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_E2);
fd3ed969
PM
4056}
4057
43efaa33
PM
4058static void tlbi_aa64_vae3_write(CPUARMState *env, const ARMCPRegInfo *ri,
4059 uint64_t value)
4060{
4061 /* Invalidate by VA, EL3
4062 * Currently handles both VAE3 and VALE3, since we don't support
4063 * flush-last-level-only.
4064 */
2fc0cc0e 4065 ARMCPU *cpu = env_archcpu(env);
43efaa33
PM
4066 CPUState *cs = CPU(cpu);
4067 uint64_t pageaddr = sextract64(value << 12, 0, 56);
4068
127b2b08 4069 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_SE3);
43efaa33
PM
4070}
4071
fd3ed969
PM
4072static void tlbi_aa64_vae1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4073 uint64_t value)
4074{
90c19cdf
RH
4075 CPUState *cs = env_cpu(env);
4076 int mask = vae1_tlbmask(env);
fa439fc5
PM
4077 uint64_t pageaddr = sextract64(value << 12, 0, 56);
4078
90c19cdf 4079 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr, mask);
fa439fc5
PM
4080}
4081
b4ab8ce9
PM
4082static void tlbi_aa64_vae1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4083 uint64_t value)
4084{
4085 /* Invalidate by VA, EL1&0 (AArch64 version).
4086 * Currently handles all of VAE1, VAAE1, VAALE1 and VALE1,
4087 * since we don't support flush-for-specific-ASID-only or
4088 * flush-last-level-only.
4089 */
90c19cdf
RH
4090 CPUState *cs = env_cpu(env);
4091 int mask = vae1_tlbmask(env);
b4ab8ce9
PM
4092 uint64_t pageaddr = sextract64(value << 12, 0, 56);
4093
4094 if (tlb_force_broadcast(env)) {
527db2be
RH
4095 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr, mask);
4096 } else {
4097 tlb_flush_page_by_mmuidx(cs, pageaddr, mask);
b4ab8ce9 4098 }
b4ab8ce9
PM
4099}
4100
fd3ed969
PM
4101static void tlbi_aa64_vae2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4102 uint64_t value)
fa439fc5 4103{
29a0af61 4104 CPUState *cs = env_cpu(env);
fd3ed969 4105 uint64_t pageaddr = sextract64(value << 12, 0, 56);
fa439fc5 4106
a67cf277 4107 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
e013b741 4108 ARMMMUIdxBit_E2);
fa439fc5
PM
4109}
4110
43efaa33
PM
4111static void tlbi_aa64_vae3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4112 uint64_t value)
4113{
29a0af61 4114 CPUState *cs = env_cpu(env);
43efaa33
PM
4115 uint64_t pageaddr = sextract64(value << 12, 0, 56);
4116
a67cf277 4117 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
127b2b08 4118 ARMMMUIdxBit_SE3);
43efaa33
PM
4119}
4120
cea66e91
PM
4121static void tlbi_aa64_ipas2e1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4122 uint64_t value)
4123{
4124 /* Invalidate by IPA. This has to invalidate any structures that
4125 * contain only stage 2 translation information, but does not need
4126 * to apply to structures that contain combined stage 1 and stage 2
4127 * translation information.
4128 * This must NOP if EL2 isn't implemented or SCR_EL3.NS is zero.
4129 */
2fc0cc0e 4130 ARMCPU *cpu = env_archcpu(env);
cea66e91
PM
4131 CPUState *cs = CPU(cpu);
4132 uint64_t pageaddr;
4133
4134 if (!arm_feature(env, ARM_FEATURE_EL2) || !(env->cp15.scr_el3 & SCR_NS)) {
4135 return;
4136 }
4137
4138 pageaddr = sextract64(value << 12, 0, 48);
4139
97fa9350 4140 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_Stage2);
cea66e91
PM
4141}
4142
4143static void tlbi_aa64_ipas2e1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4144 uint64_t value)
4145{
29a0af61 4146 CPUState *cs = env_cpu(env);
cea66e91
PM
4147 uint64_t pageaddr;
4148
4149 if (!arm_feature(env, ARM_FEATURE_EL2) || !(env->cp15.scr_el3 & SCR_NS)) {
4150 return;
4151 }
4152
4153 pageaddr = sextract64(value << 12, 0, 48);
4154
a67cf277 4155 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
97fa9350 4156 ARMMMUIdxBit_Stage2);
cea66e91
PM
4157}
4158
3f208fd7
PM
4159static CPAccessResult aa64_zva_access(CPUARMState *env, const ARMCPRegInfo *ri,
4160 bool isread)
aca3f40b
PM
4161{
4162 /* We don't implement EL2, so the only control on DC ZVA is the
4163 * bit in the SCTLR which can prohibit access for EL0.
4164 */
137feaa9 4165 if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_DZE)) {
aca3f40b
PM
4166 return CP_ACCESS_TRAP;
4167 }
4168 return CP_ACCESS_OK;
4169}
4170
4171static uint64_t aa64_dczid_read(CPUARMState *env, const ARMCPRegInfo *ri)
4172{
2fc0cc0e 4173 ARMCPU *cpu = env_archcpu(env);
aca3f40b
PM
4174 int dzp_bit = 1 << 4;
4175
4176 /* DZP indicates whether DC ZVA access is allowed */
3f208fd7 4177 if (aa64_zva_access(env, NULL, false) == CP_ACCESS_OK) {
aca3f40b
PM
4178 dzp_bit = 0;
4179 }
4180 return cpu->dcz_blocksize | dzp_bit;
4181}
4182
3f208fd7
PM
4183static CPAccessResult sp_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
4184 bool isread)
f502cfc2 4185{
cdcf1405 4186 if (!(env->pstate & PSTATE_SP)) {
f502cfc2
PM
4187 /* Access to SP_EL0 is undefined if it's being used as
4188 * the stack pointer.
4189 */
4190 return CP_ACCESS_TRAP_UNCATEGORIZED;
4191 }
4192 return CP_ACCESS_OK;
4193}
4194
4195static uint64_t spsel_read(CPUARMState *env, const ARMCPRegInfo *ri)
4196{
4197 return env->pstate & PSTATE_SP;
4198}
4199
4200static void spsel_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
4201{
4202 update_spsel(env, val);
4203}
4204
137feaa9
FA
4205static void sctlr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4206 uint64_t value)
4207{
2fc0cc0e 4208 ARMCPU *cpu = env_archcpu(env);
137feaa9
FA
4209
4210 if (raw_read(env, ri) == value) {
4211 /* Skip the TLB flush if nothing actually changed; Linux likes
4212 * to do a lot of pointless SCTLR writes.
4213 */
4214 return;
4215 }
4216
06312feb
PM
4217 if (arm_feature(env, ARM_FEATURE_PMSA) && !cpu->has_mpu) {
4218 /* M bit is RAZ/WI for PMSA with no MPU implemented */
4219 value &= ~SCTLR_M;
4220 }
4221
137feaa9
FA
4222 raw_write(env, ri, value);
4223 /* ??? Lots of these bits are not implemented. */
4224 /* This may enable/disable the MMU, so do a TLB flush. */
d10eb08f 4225 tlb_flush(CPU(cpu));
2e5dcf36
RH
4226
4227 if (ri->type & ARM_CP_SUPPRESS_TB_END) {
4228 /*
4229 * Normally we would always end the TB on an SCTLR write; see the
4230 * comment in ARMCPRegInfo sctlr initialization below for why Xscale
4231 * is special. Setting ARM_CP_SUPPRESS_TB_END also stops the rebuild
4232 * of hflags from the translator, so do it here.
4233 */
4234 arm_rebuild_hflags(env);
4235 }
137feaa9
FA
4236}
4237
3f208fd7
PM
4238static CPAccessResult fpexc32_access(CPUARMState *env, const ARMCPRegInfo *ri,
4239 bool isread)
03fbf20f
PM
4240{
4241 if ((env->cp15.cptr_el[2] & CPTR_TFP) && arm_current_el(env) == 2) {
f2cae609 4242 return CP_ACCESS_TRAP_FP_EL2;
03fbf20f
PM
4243 }
4244 if (env->cp15.cptr_el[3] & CPTR_TFP) {
f2cae609 4245 return CP_ACCESS_TRAP_FP_EL3;
03fbf20f
PM
4246 }
4247 return CP_ACCESS_OK;
4248}
4249
a8d64e73
PM
4250static void sdcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4251 uint64_t value)
4252{
4253 env->cp15.mdcr_el3 = value & SDCR_VALID_MASK;
4254}
4255
b0d2b7d0
PM
4256static const ARMCPRegInfo v8_cp_reginfo[] = {
4257 /* Minimal set of EL0-visible registers. This will need to be expanded
4258 * significantly for system emulation of AArch64 CPUs.
4259 */
4260 { .name = "NZCV", .state = ARM_CP_STATE_AA64,
4261 .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 2,
4262 .access = PL0_RW, .type = ARM_CP_NZCV },
c2b820fe
PM
4263 { .name = "DAIF", .state = ARM_CP_STATE_AA64,
4264 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 2,
7a0e58fa 4265 .type = ARM_CP_NO_RAW,
c2b820fe
PM
4266 .access = PL0_RW, .accessfn = aa64_daif_access,
4267 .fieldoffset = offsetof(CPUARMState, daif),
4268 .writefn = aa64_daif_write, .resetfn = arm_cp_reset_ignore },
b0d2b7d0
PM
4269 { .name = "FPCR", .state = ARM_CP_STATE_AA64,
4270 .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 4,
b916c9c3 4271 .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END,
fe03d45f 4272 .readfn = aa64_fpcr_read, .writefn = aa64_fpcr_write },
b0d2b7d0
PM
4273 { .name = "FPSR", .state = ARM_CP_STATE_AA64,
4274 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 4,
b916c9c3 4275 .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END,
fe03d45f 4276 .readfn = aa64_fpsr_read, .writefn = aa64_fpsr_write },
b0d2b7d0
PM
4277 { .name = "DCZID_EL0", .state = ARM_CP_STATE_AA64,
4278 .opc0 = 3, .opc1 = 3, .opc2 = 7, .crn = 0, .crm = 0,
7a0e58fa 4279 .access = PL0_R, .type = ARM_CP_NO_RAW,
aca3f40b
PM
4280 .readfn = aa64_dczid_read },
4281 { .name = "DC_ZVA", .state = ARM_CP_STATE_AA64,
4282 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 1,
4283 .access = PL0_W, .type = ARM_CP_DC_ZVA,
4284#ifndef CONFIG_USER_ONLY
4285 /* Avoid overhead of an access check that always passes in user-mode */
4286 .accessfn = aa64_zva_access,
4287#endif
4288 },
0eef9d98
PM
4289 { .name = "CURRENTEL", .state = ARM_CP_STATE_AA64,
4290 .opc0 = 3, .opc1 = 0, .opc2 = 2, .crn = 4, .crm = 2,
4291 .access = PL1_R, .type = ARM_CP_CURRENTEL },
8af35c37
PM
4292 /* Cache ops: all NOPs since we don't emulate caches */
4293 { .name = "IC_IALLUIS", .state = ARM_CP_STATE_AA64,
4294 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
4295 .access = PL1_W, .type = ARM_CP_NOP },
4296 { .name = "IC_IALLU", .state = ARM_CP_STATE_AA64,
4297 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
4298 .access = PL1_W, .type = ARM_CP_NOP },
4299 { .name = "IC_IVAU", .state = ARM_CP_STATE_AA64,
4300 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 5, .opc2 = 1,
4301 .access = PL0_W, .type = ARM_CP_NOP,
4302 .accessfn = aa64_cacheop_access },
4303 { .name = "DC_IVAC", .state = ARM_CP_STATE_AA64,
4304 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
4305 .access = PL1_W, .type = ARM_CP_NOP },
4306 { .name = "DC_ISW", .state = ARM_CP_STATE_AA64,
4307 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
4308 .access = PL1_W, .type = ARM_CP_NOP },
4309 { .name = "DC_CVAC", .state = ARM_CP_STATE_AA64,
4310 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 1,
4311 .access = PL0_W, .type = ARM_CP_NOP,
4312 .accessfn = aa64_cacheop_access },
4313 { .name = "DC_CSW", .state = ARM_CP_STATE_AA64,
4314 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
4315 .access = PL1_W, .type = ARM_CP_NOP },
4316 { .name = "DC_CVAU", .state = ARM_CP_STATE_AA64,
4317 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 11, .opc2 = 1,
4318 .access = PL0_W, .type = ARM_CP_NOP,
4319 .accessfn = aa64_cacheop_access },
4320 { .name = "DC_CIVAC", .state = ARM_CP_STATE_AA64,
4321 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 1,
4322 .access = PL0_W, .type = ARM_CP_NOP,
4323 .accessfn = aa64_cacheop_access },
4324 { .name = "DC_CISW", .state = ARM_CP_STATE_AA64,
4325 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
4326 .access = PL1_W, .type = ARM_CP_NOP },
168aa23b
PM
4327 /* TLBI operations */
4328 { .name = "TLBI_VMALLE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 4329 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
7a0e58fa 4330 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 4331 .writefn = tlbi_aa64_vmalle1is_write },
168aa23b 4332 { .name = "TLBI_VAE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 4333 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
7a0e58fa 4334 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 4335 .writefn = tlbi_aa64_vae1is_write },
168aa23b 4336 { .name = "TLBI_ASIDE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 4337 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
7a0e58fa 4338 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 4339 .writefn = tlbi_aa64_vmalle1is_write },
168aa23b 4340 { .name = "TLBI_VAAE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 4341 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
7a0e58fa 4342 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 4343 .writefn = tlbi_aa64_vae1is_write },
168aa23b 4344 { .name = "TLBI_VALE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 4345 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
7a0e58fa 4346 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 4347 .writefn = tlbi_aa64_vae1is_write },
168aa23b 4348 { .name = "TLBI_VAALE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 4349 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
7a0e58fa 4350 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 4351 .writefn = tlbi_aa64_vae1is_write },
168aa23b 4352 { .name = "TLBI_VMALLE1", .state = ARM_CP_STATE_AA64,
6ab9f499 4353 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
7a0e58fa 4354 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 4355 .writefn = tlbi_aa64_vmalle1_write },
168aa23b 4356 { .name = "TLBI_VAE1", .state = ARM_CP_STATE_AA64,
6ab9f499 4357 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
7a0e58fa 4358 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 4359 .writefn = tlbi_aa64_vae1_write },
168aa23b 4360 { .name = "TLBI_ASIDE1", .state = ARM_CP_STATE_AA64,
6ab9f499 4361 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
7a0e58fa 4362 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 4363 .writefn = tlbi_aa64_vmalle1_write },
168aa23b 4364 { .name = "TLBI_VAAE1", .state = ARM_CP_STATE_AA64,
6ab9f499 4365 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
7a0e58fa 4366 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 4367 .writefn = tlbi_aa64_vae1_write },
168aa23b 4368 { .name = "TLBI_VALE1", .state = ARM_CP_STATE_AA64,
6ab9f499 4369 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
7a0e58fa 4370 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 4371 .writefn = tlbi_aa64_vae1_write },
168aa23b 4372 { .name = "TLBI_VAALE1", .state = ARM_CP_STATE_AA64,
6ab9f499 4373 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
7a0e58fa 4374 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 4375 .writefn = tlbi_aa64_vae1_write },
cea66e91
PM
4376 { .name = "TLBI_IPAS2E1IS", .state = ARM_CP_STATE_AA64,
4377 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1,
4378 .access = PL2_W, .type = ARM_CP_NO_RAW,
4379 .writefn = tlbi_aa64_ipas2e1is_write },
4380 { .name = "TLBI_IPAS2LE1IS", .state = ARM_CP_STATE_AA64,
4381 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5,
4382 .access = PL2_W, .type = ARM_CP_NO_RAW,
4383 .writefn = tlbi_aa64_ipas2e1is_write },
83ddf975
PM
4384 { .name = "TLBI_ALLE1IS", .state = ARM_CP_STATE_AA64,
4385 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
4386 .access = PL2_W, .type = ARM_CP_NO_RAW,
fd3ed969 4387 .writefn = tlbi_aa64_alle1is_write },
43efaa33
PM
4388 { .name = "TLBI_VMALLS12E1IS", .state = ARM_CP_STATE_AA64,
4389 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 6,
4390 .access = PL2_W, .type = ARM_CP_NO_RAW,
4391 .writefn = tlbi_aa64_alle1is_write },
cea66e91
PM
4392 { .name = "TLBI_IPAS2E1", .state = ARM_CP_STATE_AA64,
4393 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1,
4394 .access = PL2_W, .type = ARM_CP_NO_RAW,
4395 .writefn = tlbi_aa64_ipas2e1_write },
4396 { .name = "TLBI_IPAS2LE1", .state = ARM_CP_STATE_AA64,
4397 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5,
4398 .access = PL2_W, .type = ARM_CP_NO_RAW,
4399 .writefn = tlbi_aa64_ipas2e1_write },
83ddf975
PM
4400 { .name = "TLBI_ALLE1", .state = ARM_CP_STATE_AA64,
4401 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
4402 .access = PL2_W, .type = ARM_CP_NO_RAW,
fd3ed969 4403 .writefn = tlbi_aa64_alle1_write },
43efaa33
PM
4404 { .name = "TLBI_VMALLS12E1", .state = ARM_CP_STATE_AA64,
4405 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 6,
4406 .access = PL2_W, .type = ARM_CP_NO_RAW,
4407 .writefn = tlbi_aa64_alle1is_write },
19525524
PM
4408#ifndef CONFIG_USER_ONLY
4409 /* 64 bit address translation operations */
4410 { .name = "AT_S1E1R", .state = ARM_CP_STATE_AA64,
4411 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 0,
0710b2fa
PM
4412 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4413 .writefn = ats_write64 },
19525524
PM
4414 { .name = "AT_S1E1W", .state = ARM_CP_STATE_AA64,
4415 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 1,
0710b2fa
PM
4416 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4417 .writefn = ats_write64 },
19525524
PM
4418 { .name = "AT_S1E0R", .state = ARM_CP_STATE_AA64,
4419 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 2,
0710b2fa
PM
4420 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4421 .writefn = ats_write64 },
19525524
PM
4422 { .name = "AT_S1E0W", .state = ARM_CP_STATE_AA64,
4423 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 3,
0710b2fa
PM
4424 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4425 .writefn = ats_write64 },
2a47df95 4426 { .name = "AT_S12E1R", .state = ARM_CP_STATE_AA64,
7a379c7e 4427 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 4,
0710b2fa
PM
4428 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4429 .writefn = ats_write64 },
2a47df95 4430 { .name = "AT_S12E1W", .state = ARM_CP_STATE_AA64,
7a379c7e 4431 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 5,
0710b2fa
PM
4432 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4433 .writefn = ats_write64 },
2a47df95 4434 { .name = "AT_S12E0R", .state = ARM_CP_STATE_AA64,
7a379c7e 4435 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 6,
0710b2fa
PM
4436 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4437 .writefn = ats_write64 },
2a47df95 4438 { .name = "AT_S12E0W", .state = ARM_CP_STATE_AA64,
7a379c7e 4439 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 7,
0710b2fa
PM
4440 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4441 .writefn = ats_write64 },
2a47df95
PM
4442 /* AT S1E2* are elsewhere as they UNDEF from EL3 if EL2 is not present */
4443 { .name = "AT_S1E3R", .state = ARM_CP_STATE_AA64,
4444 .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 0,
0710b2fa
PM
4445 .access = PL3_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4446 .writefn = ats_write64 },
2a47df95
PM
4447 { .name = "AT_S1E3W", .state = ARM_CP_STATE_AA64,
4448 .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 1,
0710b2fa
PM
4449 .access = PL3_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4450 .writefn = ats_write64 },
c96fc9b5
EI
4451 { .name = "PAR_EL1", .state = ARM_CP_STATE_AA64,
4452 .type = ARM_CP_ALIAS,
4453 .opc0 = 3, .opc1 = 0, .crn = 7, .crm = 4, .opc2 = 0,
4454 .access = PL1_RW, .resetvalue = 0,
4455 .fieldoffset = offsetof(CPUARMState, cp15.par_el[1]),
4456 .writefn = par_write },
19525524 4457#endif
995939a6 4458 /* TLB invalidate last level of translation table walk */
9449fdf6 4459 { .name = "TLBIMVALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
7a0e58fa 4460 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_is_write },
9449fdf6 4461 { .name = "TLBIMVAALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
7a0e58fa 4462 .type = ARM_CP_NO_RAW, .access = PL1_W,
fa439fc5 4463 .writefn = tlbimvaa_is_write },
9449fdf6 4464 { .name = "TLBIMVAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
7a0e58fa 4465 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write },
9449fdf6 4466 { .name = "TLBIMVAAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
7a0e58fa 4467 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimvaa_write },
541ef8c2
SS
4468 { .name = "TLBIMVALH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
4469 .type = ARM_CP_NO_RAW, .access = PL2_W,
4470 .writefn = tlbimva_hyp_write },
4471 { .name = "TLBIMVALHIS",
4472 .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
4473 .type = ARM_CP_NO_RAW, .access = PL2_W,
4474 .writefn = tlbimva_hyp_is_write },
4475 { .name = "TLBIIPAS2",
4476 .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1,
4477 .type = ARM_CP_NO_RAW, .access = PL2_W,
4478 .writefn = tlbiipas2_write },
4479 { .name = "TLBIIPAS2IS",
4480 .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1,
4481 .type = ARM_CP_NO_RAW, .access = PL2_W,
4482 .writefn = tlbiipas2_is_write },
4483 { .name = "TLBIIPAS2L",
4484 .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5,
4485 .type = ARM_CP_NO_RAW, .access = PL2_W,
4486 .writefn = tlbiipas2_write },
4487 { .name = "TLBIIPAS2LIS",
4488 .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5,
4489 .type = ARM_CP_NO_RAW, .access = PL2_W,
4490 .writefn = tlbiipas2_is_write },
9449fdf6
PM
4491 /* 32 bit cache operations */
4492 { .name = "ICIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
4493 .type = ARM_CP_NOP, .access = PL1_W },
4494 { .name = "BPIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 6,
4495 .type = ARM_CP_NOP, .access = PL1_W },
4496 { .name = "ICIALLU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
4497 .type = ARM_CP_NOP, .access = PL1_W },
4498 { .name = "ICIMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 1,
4499 .type = ARM_CP_NOP, .access = PL1_W },
4500 { .name = "BPIALL", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 6,
4501 .type = ARM_CP_NOP, .access = PL1_W },
4502 { .name = "BPIMVA", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 7,
4503 .type = ARM_CP_NOP, .access = PL1_W },
4504 { .name = "DCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
4505 .type = ARM_CP_NOP, .access = PL1_W },
4506 { .name = "DCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
4507 .type = ARM_CP_NOP, .access = PL1_W },
4508 { .name = "DCCMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 1,
4509 .type = ARM_CP_NOP, .access = PL1_W },
4510 { .name = "DCCSW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
4511 .type = ARM_CP_NOP, .access = PL1_W },
4512 { .name = "DCCMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 11, .opc2 = 1,
4513 .type = ARM_CP_NOP, .access = PL1_W },
4514 { .name = "DCCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 1,
4515 .type = ARM_CP_NOP, .access = PL1_W },
4516 { .name = "DCCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
4517 .type = ARM_CP_NOP, .access = PL1_W },
4518 /* MMU Domain access control / MPU write buffer control */
0c17d68c
FA
4519 { .name = "DACR", .cp = 15, .opc1 = 0, .crn = 3, .crm = 0, .opc2 = 0,
4520 .access = PL1_RW, .resetvalue = 0,
4521 .writefn = dacr_write, .raw_writefn = raw_write,
4522 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
4523 offsetoflow32(CPUARMState, cp15.dacr_ns) } },
a0618a19 4524 { .name = "ELR_EL1", .state = ARM_CP_STATE_AA64,
7a0e58fa 4525 .type = ARM_CP_ALIAS,
a0618a19 4526 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 1,
6947f059
EI
4527 .access = PL1_RW,
4528 .fieldoffset = offsetof(CPUARMState, elr_el[1]) },
a65f1de9 4529 { .name = "SPSR_EL1", .state = ARM_CP_STATE_AA64,
7a0e58fa 4530 .type = ARM_CP_ALIAS,
a65f1de9 4531 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 0,
99a99c1f
SB
4532 .access = PL1_RW,
4533 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_SVC]) },
f502cfc2
PM
4534 /* We rely on the access checks not allowing the guest to write to the
4535 * state field when SPSel indicates that it's being used as the stack
4536 * pointer.
4537 */
4538 { .name = "SP_EL0", .state = ARM_CP_STATE_AA64,
4539 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 1, .opc2 = 0,
4540 .access = PL1_RW, .accessfn = sp_el0_access,
7a0e58fa 4541 .type = ARM_CP_ALIAS,
f502cfc2 4542 .fieldoffset = offsetof(CPUARMState, sp_el[0]) },
884b4dee
GB
4543 { .name = "SP_EL1", .state = ARM_CP_STATE_AA64,
4544 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 1, .opc2 = 0,
7a0e58fa 4545 .access = PL2_RW, .type = ARM_CP_ALIAS,
884b4dee 4546 .fieldoffset = offsetof(CPUARMState, sp_el[1]) },
f502cfc2
PM
4547 { .name = "SPSel", .state = ARM_CP_STATE_AA64,
4548 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 0,
7a0e58fa 4549 .type = ARM_CP_NO_RAW,
f502cfc2 4550 .access = PL1_RW, .readfn = spsel_read, .writefn = spsel_write },
03fbf20f
PM
4551 { .name = "FPEXC32_EL2", .state = ARM_CP_STATE_AA64,
4552 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 3, .opc2 = 0,
4553 .type = ARM_CP_ALIAS,
4554 .fieldoffset = offsetof(CPUARMState, vfp.xregs[ARM_VFP_FPEXC]),
4555 .access = PL2_RW, .accessfn = fpexc32_access },
6a43e0b6
PM
4556 { .name = "DACR32_EL2", .state = ARM_CP_STATE_AA64,
4557 .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 0, .opc2 = 0,
4558 .access = PL2_RW, .resetvalue = 0,
4559 .writefn = dacr_write, .raw_writefn = raw_write,
4560 .fieldoffset = offsetof(CPUARMState, cp15.dacr32_el2) },
4561 { .name = "IFSR32_EL2", .state = ARM_CP_STATE_AA64,
4562 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 0, .opc2 = 1,
4563 .access = PL2_RW, .resetvalue = 0,
4564 .fieldoffset = offsetof(CPUARMState, cp15.ifsr32_el2) },
4565 { .name = "SPSR_IRQ", .state = ARM_CP_STATE_AA64,
4566 .type = ARM_CP_ALIAS,
4567 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 0,
4568 .access = PL2_RW,
4569 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_IRQ]) },
4570 { .name = "SPSR_ABT", .state = ARM_CP_STATE_AA64,
4571 .type = ARM_CP_ALIAS,
4572 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 1,
4573 .access = PL2_RW,
4574 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_ABT]) },
4575 { .name = "SPSR_UND", .state = ARM_CP_STATE_AA64,
4576 .type = ARM_CP_ALIAS,
4577 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 2,
4578 .access = PL2_RW,
4579 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_UND]) },
4580 { .name = "SPSR_FIQ", .state = ARM_CP_STATE_AA64,
4581 .type = ARM_CP_ALIAS,
4582 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 3,
4583 .access = PL2_RW,
4584 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_FIQ]) },
a8d64e73
PM
4585 { .name = "MDCR_EL3", .state = ARM_CP_STATE_AA64,
4586 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 3, .opc2 = 1,
4587 .resetvalue = 0,
4588 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el3) },
4589 { .name = "SDCR", .type = ARM_CP_ALIAS,
4590 .cp = 15, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 1,
4591 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
4592 .writefn = sdcr_write,
4593 .fieldoffset = offsetoflow32(CPUARMState, cp15.mdcr_el3) },
b0d2b7d0
PM
4594 REGINFO_SENTINEL
4595};
4596
d42e3c26 4597/* Used to describe the behaviour of EL2 regs when EL2 does not exist. */
4771cd01 4598static const ARMCPRegInfo el3_no_el2_cp_reginfo[] = {
d79e0c06 4599 { .name = "VBAR_EL2", .state = ARM_CP_STATE_BOTH,
d42e3c26
EI
4600 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0,
4601 .access = PL2_RW,
4602 .readfn = arm_cp_read_zero, .writefn = arm_cp_write_ignore },
ce4afed8 4603 { .name = "HCR_EL2", .state = ARM_CP_STATE_BOTH,
7a0e58fa 4604 .type = ARM_CP_NO_RAW,
f149e3e8
EI
4605 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
4606 .access = PL2_RW,
ce4afed8 4607 .type = ARM_CP_CONST, .resetvalue = 0 },
831a2fca
PM
4608 { .name = "HACR_EL2", .state = ARM_CP_STATE_BOTH,
4609 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 7,
4610 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
68e78e33
PM
4611 { .name = "ESR_EL2", .state = ARM_CP_STATE_BOTH,
4612 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 0,
4613 .access = PL2_RW,
4614 .type = ARM_CP_CONST, .resetvalue = 0 },
c6f19164
GB
4615 { .name = "CPTR_EL2", .state = ARM_CP_STATE_BOTH,
4616 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 2,
4617 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
95f949ac
EI
4618 { .name = "MAIR_EL2", .state = ARM_CP_STATE_BOTH,
4619 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 0,
4620 .access = PL2_RW, .type = ARM_CP_CONST,
4621 .resetvalue = 0 },
4622 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
b5ede85b 4623 .cp = 15, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 1,
95f949ac 4624 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
2179ef95
PM
4625 { .name = "AMAIR_EL2", .state = ARM_CP_STATE_BOTH,
4626 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 0,
4627 .access = PL2_RW, .type = ARM_CP_CONST,
4628 .resetvalue = 0 },
55b53c71 4629 { .name = "HAMAIR1", .state = ARM_CP_STATE_AA32,
b5ede85b 4630 .cp = 15, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 1,
2179ef95
PM
4631 .access = PL2_RW, .type = ARM_CP_CONST,
4632 .resetvalue = 0 },
37cd6c24
PM
4633 { .name = "AFSR0_EL2", .state = ARM_CP_STATE_BOTH,
4634 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 0,
4635 .access = PL2_RW, .type = ARM_CP_CONST,
4636 .resetvalue = 0 },
4637 { .name = "AFSR1_EL2", .state = ARM_CP_STATE_BOTH,
4638 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 1,
4639 .access = PL2_RW, .type = ARM_CP_CONST,
4640 .resetvalue = 0 },
06ec4c8c
EI
4641 { .name = "TCR_EL2", .state = ARM_CP_STATE_BOTH,
4642 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 2,
4643 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
68e9c2fe
EI
4644 { .name = "VTCR_EL2", .state = ARM_CP_STATE_BOTH,
4645 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
4646 .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any,
4647 .type = ARM_CP_CONST, .resetvalue = 0 },
b698e9cf
EI
4648 { .name = "VTTBR", .state = ARM_CP_STATE_AA32,
4649 .cp = 15, .opc1 = 6, .crm = 2,
4650 .access = PL2_RW, .accessfn = access_el3_aa32ns,
4651 .type = ARM_CP_CONST | ARM_CP_64BIT, .resetvalue = 0 },
4652 { .name = "VTTBR_EL2", .state = ARM_CP_STATE_AA64,
4653 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 0,
4654 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
b9cb5323
EI
4655 { .name = "SCTLR_EL2", .state = ARM_CP_STATE_BOTH,
4656 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 0,
4657 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
ff05f37b
EI
4658 { .name = "TPIDR_EL2", .state = ARM_CP_STATE_BOTH,
4659 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 2,
4660 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
a57633c0
EI
4661 { .name = "TTBR0_EL2", .state = ARM_CP_STATE_AA64,
4662 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
4663 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
4664 { .name = "HTTBR", .cp = 15, .opc1 = 4, .crm = 2,
4665 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_CONST,
4666 .resetvalue = 0 },
0b6440af
EI
4667 { .name = "CNTHCTL_EL2", .state = ARM_CP_STATE_BOTH,
4668 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 1, .opc2 = 0,
4669 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
edac4d8a
EI
4670 { .name = "CNTVOFF_EL2", .state = ARM_CP_STATE_AA64,
4671 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 3,
4672 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
4673 { .name = "CNTVOFF", .cp = 15, .opc1 = 4, .crm = 14,
4674 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_CONST,
4675 .resetvalue = 0 },
b0e66d95
EI
4676 { .name = "CNTHP_CVAL_EL2", .state = ARM_CP_STATE_AA64,
4677 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 2,
4678 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
4679 { .name = "CNTHP_CVAL", .cp = 15, .opc1 = 6, .crm = 14,
4680 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_CONST,
4681 .resetvalue = 0 },
4682 { .name = "CNTHP_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
4683 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 0,
4684 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
4685 { .name = "CNTHP_CTL_EL2", .state = ARM_CP_STATE_BOTH,
4686 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 1,
4687 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
14cc7b54
SF
4688 { .name = "MDCR_EL2", .state = ARM_CP_STATE_BOTH,
4689 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 1,
d6c8cf81
PM
4690 .access = PL2_RW, .accessfn = access_tda,
4691 .type = ARM_CP_CONST, .resetvalue = 0 },
59e05530
EI
4692 { .name = "HPFAR_EL2", .state = ARM_CP_STATE_BOTH,
4693 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
4694 .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any,
4695 .type = ARM_CP_CONST, .resetvalue = 0 },
2a5a9abd
AF
4696 { .name = "HSTR_EL2", .state = ARM_CP_STATE_BOTH,
4697 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 3,
4698 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
cba517c3
PM
4699 { .name = "FAR_EL2", .state = ARM_CP_STATE_BOTH,
4700 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 0,
4701 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
4702 { .name = "HIFAR", .state = ARM_CP_STATE_AA32,
4703 .type = ARM_CP_CONST,
4704 .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 2,
4705 .access = PL2_RW, .resetvalue = 0 },
d42e3c26
EI
4706 REGINFO_SENTINEL
4707};
4708
ce4afed8
PM
4709/* Ditto, but for registers which exist in ARMv8 but not v7 */
4710static const ARMCPRegInfo el3_no_el2_v8_cp_reginfo[] = {
4711 { .name = "HCR2", .state = ARM_CP_STATE_AA32,
4712 .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 4,
4713 .access = PL2_RW,
4714 .type = ARM_CP_CONST, .resetvalue = 0 },
4715 REGINFO_SENTINEL
4716};
4717
f149e3e8
EI
4718static void hcr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
4719{
2fc0cc0e 4720 ARMCPU *cpu = env_archcpu(env);
03c76131
RH
4721 /* Begin with bits defined in base ARMv8.0. */
4722 uint64_t valid_mask = MAKE_64BIT_MASK(0, 34);
f149e3e8
EI
4723
4724 if (arm_feature(env, ARM_FEATURE_EL3)) {
4725 valid_mask &= ~HCR_HCD;
77077a83
JK
4726 } else if (cpu->psci_conduit != QEMU_PSCI_CONDUIT_SMC) {
4727 /* Architecturally HCR.TSC is RES0 if EL3 is not implemented.
4728 * However, if we're using the SMC PSCI conduit then QEMU is
4729 * effectively acting like EL3 firmware and so the guest at
4730 * EL2 should retain the ability to prevent EL1 from being
4731 * able to make SMC calls into the ersatz firmware, so in
4732 * that case HCR.TSC should be read/write.
4733 */
f149e3e8
EI
4734 valid_mask &= ~HCR_TSC;
4735 }
03c76131
RH
4736 if (cpu_isar_feature(aa64_vh, cpu)) {
4737 valid_mask |= HCR_E2H;
4738 }
2d7137c1
RH
4739 if (cpu_isar_feature(aa64_lor, cpu)) {
4740 valid_mask |= HCR_TLOR;
4741 }
ef682cdb
RH
4742 if (cpu_isar_feature(aa64_pauth, cpu)) {
4743 valid_mask |= HCR_API | HCR_APK;
4744 }
f149e3e8
EI
4745
4746 /* Clear RES0 bits. */
4747 value &= valid_mask;
4748
4749 /* These bits change the MMU setup:
4750 * HCR_VM enables stage 2 translation
4751 * HCR_PTW forbids certain page-table setups
4752 * HCR_DC Disables stage1 and enables stage2 translation
4753 */
ce4afed8 4754 if ((env->cp15.hcr_el2 ^ value) & (HCR_VM | HCR_PTW | HCR_DC)) {
d10eb08f 4755 tlb_flush(CPU(cpu));
f149e3e8 4756 }
ce4afed8 4757 env->cp15.hcr_el2 = value;
89430fc6
PM
4758
4759 /*
4760 * Updates to VI and VF require us to update the status of
4761 * virtual interrupts, which are the logical OR of these bits
4762 * and the state of the input lines from the GIC. (This requires
4763 * that we have the iothread lock, which is done by marking the
4764 * reginfo structs as ARM_CP_IO.)
4765 * Note that if a write to HCR pends a VIRQ or VFIQ it is never
4766 * possible for it to be taken immediately, because VIRQ and
4767 * VFIQ are masked unless running at EL0 or EL1, and HCR
4768 * can only be written at EL2.
4769 */
4770 g_assert(qemu_mutex_iothread_locked());
4771 arm_cpu_update_virq(cpu);
4772 arm_cpu_update_vfiq(cpu);
ce4afed8
PM
4773}
4774
4775static void hcr_writehigh(CPUARMState *env, const ARMCPRegInfo *ri,
4776 uint64_t value)
4777{
4778 /* Handle HCR2 write, i.e. write to high half of HCR_EL2 */
4779 value = deposit64(env->cp15.hcr_el2, 32, 32, value);
4780 hcr_write(env, NULL, value);
4781}
4782
4783static void hcr_writelow(CPUARMState *env, const ARMCPRegInfo *ri,
4784 uint64_t value)
4785{
4786 /* Handle HCR write, i.e. write to low half of HCR_EL2 */
4787 value = deposit64(env->cp15.hcr_el2, 0, 32, value);
4788 hcr_write(env, NULL, value);
f149e3e8
EI
4789}
4790
f7778444
RH
4791/*
4792 * Return the effective value of HCR_EL2.
4793 * Bits that are not included here:
4794 * RW (read from SCR_EL3.RW as needed)
4795 */
4796uint64_t arm_hcr_el2_eff(CPUARMState *env)
4797{
4798 uint64_t ret = env->cp15.hcr_el2;
4799
4800 if (arm_is_secure_below_el3(env)) {
4801 /*
4802 * "This register has no effect if EL2 is not enabled in the
4803 * current Security state". This is ARMv8.4-SecEL2 speak for
4804 * !(SCR_EL3.NS==1 || SCR_EL3.EEL2==1).
4805 *
4806 * Prior to that, the language was "In an implementation that
4807 * includes EL3, when the value of SCR_EL3.NS is 0 the PE behaves
4808 * as if this field is 0 for all purposes other than a direct
4809 * read or write access of HCR_EL2". With lots of enumeration
4810 * on a per-field basis. In current QEMU, this is condition
4811 * is arm_is_secure_below_el3.
4812 *
4813 * Since the v8.4 language applies to the entire register, and
4814 * appears to be backward compatible, use that.
4815 */
4816 ret = 0;
4817 } else if (ret & HCR_TGE) {
4818 /* These bits are up-to-date as of ARMv8.4. */
4819 if (ret & HCR_E2H) {
4820 ret &= ~(HCR_VM | HCR_FMO | HCR_IMO | HCR_AMO |
4821 HCR_BSU_MASK | HCR_DC | HCR_TWI | HCR_TWE |
4822 HCR_TID0 | HCR_TID2 | HCR_TPCP | HCR_TPU |
4823 HCR_TDZ | HCR_CD | HCR_ID | HCR_MIOCNCE);
4824 } else {
4825 ret |= HCR_FMO | HCR_IMO | HCR_AMO;
4826 }
4827 ret &= ~(HCR_SWIO | HCR_PTW | HCR_VF | HCR_VI | HCR_VSE |
4828 HCR_FB | HCR_TID1 | HCR_TID3 | HCR_TSC | HCR_TACR |
4829 HCR_TSW | HCR_TTLB | HCR_TVM | HCR_HCD | HCR_TRVM |
4830 HCR_TLOR);
4831 }
4832
4833 return ret;
4834}
4835
fc1120a7
PM
4836static void cptr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
4837 uint64_t value)
4838{
4839 /*
4840 * For A-profile AArch32 EL3, if NSACR.CP10
4841 * is 0 then HCPTR.{TCP11,TCP10} ignore writes and read as 1.
4842 */
4843 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
4844 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
4845 value &= ~(0x3 << 10);
4846 value |= env->cp15.cptr_el[2] & (0x3 << 10);
4847 }
4848 env->cp15.cptr_el[2] = value;
4849}
4850
4851static uint64_t cptr_el2_read(CPUARMState *env, const ARMCPRegInfo *ri)
4852{
4853 /*
4854 * For A-profile AArch32 EL3, if NSACR.CP10
4855 * is 0 then HCPTR.{TCP11,TCP10} ignore writes and read as 1.
4856 */
4857 uint64_t value = env->cp15.cptr_el[2];
4858
4859 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
4860 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
4861 value |= 0x3 << 10;
4862 }
4863 return value;
4864}
4865
4771cd01 4866static const ARMCPRegInfo el2_cp_reginfo[] = {
f149e3e8 4867 { .name = "HCR_EL2", .state = ARM_CP_STATE_AA64,
89430fc6 4868 .type = ARM_CP_IO,
f149e3e8
EI
4869 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
4870 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
c624ea0f 4871 .writefn = hcr_write },
ce4afed8 4872 { .name = "HCR", .state = ARM_CP_STATE_AA32,
89430fc6 4873 .type = ARM_CP_ALIAS | ARM_CP_IO,
ce4afed8
PM
4874 .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
4875 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
c624ea0f 4876 .writefn = hcr_writelow },
831a2fca
PM
4877 { .name = "HACR_EL2", .state = ARM_CP_STATE_BOTH,
4878 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 7,
4879 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3b685ba7 4880 { .name = "ELR_EL2", .state = ARM_CP_STATE_AA64,
7a0e58fa 4881 .type = ARM_CP_ALIAS,
3b685ba7
EI
4882 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 1,
4883 .access = PL2_RW,
4884 .fieldoffset = offsetof(CPUARMState, elr_el[2]) },
68e78e33 4885 { .name = "ESR_EL2", .state = ARM_CP_STATE_BOTH,
f2c30f42
EI
4886 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 0,
4887 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[2]) },
cba517c3 4888 { .name = "FAR_EL2", .state = ARM_CP_STATE_BOTH,
63b60551
EI
4889 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 0,
4890 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[2]) },
cba517c3
PM
4891 { .name = "HIFAR", .state = ARM_CP_STATE_AA32,
4892 .type = ARM_CP_ALIAS,
4893 .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 2,
4894 .access = PL2_RW,
4895 .fieldoffset = offsetofhigh32(CPUARMState, cp15.far_el[2]) },
3b685ba7 4896 { .name = "SPSR_EL2", .state = ARM_CP_STATE_AA64,
7a0e58fa 4897 .type = ARM_CP_ALIAS,
3b685ba7 4898 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 0,
99a99c1f
SB
4899 .access = PL2_RW,
4900 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_HYP]) },
d79e0c06 4901 { .name = "VBAR_EL2", .state = ARM_CP_STATE_BOTH,
d42e3c26
EI
4902 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0,
4903 .access = PL2_RW, .writefn = vbar_write,
4904 .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[2]),
4905 .resetvalue = 0 },
884b4dee
GB
4906 { .name = "SP_EL2", .state = ARM_CP_STATE_AA64,
4907 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 1, .opc2 = 0,
7a0e58fa 4908 .access = PL3_RW, .type = ARM_CP_ALIAS,
884b4dee 4909 .fieldoffset = offsetof(CPUARMState, sp_el[2]) },
c6f19164
GB
4910 { .name = "CPTR_EL2", .state = ARM_CP_STATE_BOTH,
4911 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 2,
4912 .access = PL2_RW, .accessfn = cptr_access, .resetvalue = 0,
fc1120a7
PM
4913 .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[2]),
4914 .readfn = cptr_el2_read, .writefn = cptr_el2_write },
95f949ac
EI
4915 { .name = "MAIR_EL2", .state = ARM_CP_STATE_BOTH,
4916 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 0,
4917 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[2]),
4918 .resetvalue = 0 },
4919 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
b5ede85b 4920 .cp = 15, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 1,
95f949ac
EI
4921 .access = PL2_RW, .type = ARM_CP_ALIAS,
4922 .fieldoffset = offsetofhigh32(CPUARMState, cp15.mair_el[2]) },
2179ef95
PM
4923 { .name = "AMAIR_EL2", .state = ARM_CP_STATE_BOTH,
4924 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 0,
4925 .access = PL2_RW, .type = ARM_CP_CONST,
4926 .resetvalue = 0 },
4927 /* HAMAIR1 is mapped to AMAIR_EL2[63:32] */
55b53c71 4928 { .name = "HAMAIR1", .state = ARM_CP_STATE_AA32,
b5ede85b 4929 .cp = 15, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 1,
2179ef95
PM
4930 .access = PL2_RW, .type = ARM_CP_CONST,
4931 .resetvalue = 0 },
37cd6c24
PM
4932 { .name = "AFSR0_EL2", .state = ARM_CP_STATE_BOTH,
4933 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 0,
4934 .access = PL2_RW, .type = ARM_CP_CONST,
4935 .resetvalue = 0 },
4936 { .name = "AFSR1_EL2", .state = ARM_CP_STATE_BOTH,
4937 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 1,
4938 .access = PL2_RW, .type = ARM_CP_CONST,
4939 .resetvalue = 0 },
06ec4c8c
EI
4940 { .name = "TCR_EL2", .state = ARM_CP_STATE_BOTH,
4941 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 2,
6459b94c
PM
4942 .access = PL2_RW,
4943 /* no .writefn needed as this can't cause an ASID change;
4944 * no .raw_writefn or .resetfn needed as we never use mask/base_mask
4945 */
06ec4c8c 4946 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[2]) },
68e9c2fe
EI
4947 { .name = "VTCR", .state = ARM_CP_STATE_AA32,
4948 .cp = 15, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
bf06c112 4949 .type = ARM_CP_ALIAS,
68e9c2fe
EI
4950 .access = PL2_RW, .accessfn = access_el3_aa32ns,
4951 .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) },
4952 { .name = "VTCR_EL2", .state = ARM_CP_STATE_AA64,
4953 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
bf06c112
PM
4954 .access = PL2_RW,
4955 /* no .writefn needed as this can't cause an ASID change;
4956 * no .raw_writefn or .resetfn needed as we never use mask/base_mask
4957 */
68e9c2fe 4958 .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) },
b698e9cf
EI
4959 { .name = "VTTBR", .state = ARM_CP_STATE_AA32,
4960 .cp = 15, .opc1 = 6, .crm = 2,
4961 .type = ARM_CP_64BIT | ARM_CP_ALIAS,
4962 .access = PL2_RW, .accessfn = access_el3_aa32ns,
4963 .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2),
4964 .writefn = vttbr_write },
4965 { .name = "VTTBR_EL2", .state = ARM_CP_STATE_AA64,
4966 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 0,
4967 .access = PL2_RW, .writefn = vttbr_write,
4968 .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2) },
b9cb5323
EI
4969 { .name = "SCTLR_EL2", .state = ARM_CP_STATE_BOTH,
4970 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 0,
4971 .access = PL2_RW, .raw_writefn = raw_write, .writefn = sctlr_write,
4972 .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[2]) },
ff05f37b
EI
4973 { .name = "TPIDR_EL2", .state = ARM_CP_STATE_BOTH,
4974 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 2,
4975 .access = PL2_RW, .resetvalue = 0,
4976 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[2]) },
a57633c0
EI
4977 { .name = "TTBR0_EL2", .state = ARM_CP_STATE_AA64,
4978 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
ed30da8e 4979 .access = PL2_RW, .resetvalue = 0, .writefn = vmsa_tcr_ttbr_el2_write,
a57633c0
EI
4980 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
4981 { .name = "HTTBR", .cp = 15, .opc1 = 4, .crm = 2,
4982 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
a57633c0 4983 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
541ef8c2
SS
4984 { .name = "TLBIALLNSNH",
4985 .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
4986 .type = ARM_CP_NO_RAW, .access = PL2_W,
4987 .writefn = tlbiall_nsnh_write },
4988 { .name = "TLBIALLNSNHIS",
4989 .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
4990 .type = ARM_CP_NO_RAW, .access = PL2_W,
4991 .writefn = tlbiall_nsnh_is_write },
4992 { .name = "TLBIALLH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
4993 .type = ARM_CP_NO_RAW, .access = PL2_W,
4994 .writefn = tlbiall_hyp_write },
4995 { .name = "TLBIALLHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
4996 .type = ARM_CP_NO_RAW, .access = PL2_W,
4997 .writefn = tlbiall_hyp_is_write },
4998 { .name = "TLBIMVAH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
4999 .type = ARM_CP_NO_RAW, .access = PL2_W,
5000 .writefn = tlbimva_hyp_write },
5001 { .name = "TLBIMVAHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
5002 .type = ARM_CP_NO_RAW, .access = PL2_W,
5003 .writefn = tlbimva_hyp_is_write },
51da9014
EI
5004 { .name = "TLBI_ALLE2", .state = ARM_CP_STATE_AA64,
5005 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
5006 .type = ARM_CP_NO_RAW, .access = PL2_W,
fd3ed969 5007 .writefn = tlbi_aa64_alle2_write },
8742d49d
EI
5008 { .name = "TLBI_VAE2", .state = ARM_CP_STATE_AA64,
5009 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
5010 .type = ARM_CP_NO_RAW, .access = PL2_W,
fd3ed969 5011 .writefn = tlbi_aa64_vae2_write },
2bfb9d75
PM
5012 { .name = "TLBI_VALE2", .state = ARM_CP_STATE_AA64,
5013 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
5014 .access = PL2_W, .type = ARM_CP_NO_RAW,
5015 .writefn = tlbi_aa64_vae2_write },
5016 { .name = "TLBI_ALLE2IS", .state = ARM_CP_STATE_AA64,
5017 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
5018 .access = PL2_W, .type = ARM_CP_NO_RAW,
5019 .writefn = tlbi_aa64_alle2is_write },
8742d49d
EI
5020 { .name = "TLBI_VAE2IS", .state = ARM_CP_STATE_AA64,
5021 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
5022 .type = ARM_CP_NO_RAW, .access = PL2_W,
fd3ed969 5023 .writefn = tlbi_aa64_vae2is_write },
2bfb9d75
PM
5024 { .name = "TLBI_VALE2IS", .state = ARM_CP_STATE_AA64,
5025 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
5026 .access = PL2_W, .type = ARM_CP_NO_RAW,
5027 .writefn = tlbi_aa64_vae2is_write },
edac4d8a 5028#ifndef CONFIG_USER_ONLY
2a47df95
PM
5029 /* Unlike the other EL2-related AT operations, these must
5030 * UNDEF from EL3 if EL2 is not implemented, which is why we
5031 * define them here rather than with the rest of the AT ops.
5032 */
5033 { .name = "AT_S1E2R", .state = ARM_CP_STATE_AA64,
5034 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
5035 .access = PL2_W, .accessfn = at_s1e2_access,
0710b2fa 5036 .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC, .writefn = ats_write64 },
2a47df95
PM
5037 { .name = "AT_S1E2W", .state = ARM_CP_STATE_AA64,
5038 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
5039 .access = PL2_W, .accessfn = at_s1e2_access,
0710b2fa 5040 .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC, .writefn = ats_write64 },
14db7fe0
PM
5041 /* The AArch32 ATS1H* operations are CONSTRAINED UNPREDICTABLE
5042 * if EL2 is not implemented; we choose to UNDEF. Behaviour at EL3
5043 * with SCR.NS == 0 outside Monitor mode is UNPREDICTABLE; we choose
5044 * to behave as if SCR.NS was 1.
5045 */
5046 { .name = "ATS1HR", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
5047 .access = PL2_W,
0710b2fa 5048 .writefn = ats1h_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
14db7fe0
PM
5049 { .name = "ATS1HW", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
5050 .access = PL2_W,
0710b2fa 5051 .writefn = ats1h_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
0b6440af
EI
5052 { .name = "CNTHCTL_EL2", .state = ARM_CP_STATE_BOTH,
5053 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 1, .opc2 = 0,
5054 /* ARMv7 requires bit 0 and 1 to reset to 1. ARMv8 defines the
5055 * reset values as IMPDEF. We choose to reset to 3 to comply with
5056 * both ARMv7 and ARMv8.
5057 */
5058 .access = PL2_RW, .resetvalue = 3,
5059 .fieldoffset = offsetof(CPUARMState, cp15.cnthctl_el2) },
edac4d8a
EI
5060 { .name = "CNTVOFF_EL2", .state = ARM_CP_STATE_AA64,
5061 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 3,
5062 .access = PL2_RW, .type = ARM_CP_IO, .resetvalue = 0,
5063 .writefn = gt_cntvoff_write,
5064 .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
5065 { .name = "CNTVOFF", .cp = 15, .opc1 = 4, .crm = 14,
5066 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS | ARM_CP_IO,
5067 .writefn = gt_cntvoff_write,
5068 .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
b0e66d95
EI
5069 { .name = "CNTHP_CVAL_EL2", .state = ARM_CP_STATE_AA64,
5070 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 2,
5071 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
5072 .type = ARM_CP_IO, .access = PL2_RW,
5073 .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
5074 { .name = "CNTHP_CVAL", .cp = 15, .opc1 = 6, .crm = 14,
5075 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
5076 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_IO,
5077 .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
5078 { .name = "CNTHP_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
5079 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 0,
d44ec156 5080 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
b0e66d95
EI
5081 .resetfn = gt_hyp_timer_reset,
5082 .readfn = gt_hyp_tval_read, .writefn = gt_hyp_tval_write },
5083 { .name = "CNTHP_CTL_EL2", .state = ARM_CP_STATE_BOTH,
5084 .type = ARM_CP_IO,
5085 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 1,
5086 .access = PL2_RW,
5087 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].ctl),
5088 .resetvalue = 0,
5089 .writefn = gt_hyp_ctl_write, .raw_writefn = raw_write },
edac4d8a 5090#endif
14cc7b54
SF
5091 /* The only field of MDCR_EL2 that has a defined architectural reset value
5092 * is MDCR_EL2.HPMN which should reset to the value of PMCR_EL0.N; but we
5ecdd3e4 5093 * don't implement any PMU event counters, so using zero as a reset
14cc7b54
SF
5094 * value for MDCR_EL2 is okay
5095 */
5096 { .name = "MDCR_EL2", .state = ARM_CP_STATE_BOTH,
5097 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 1,
5098 .access = PL2_RW, .resetvalue = 0,
5099 .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el2), },
59e05530
EI
5100 { .name = "HPFAR", .state = ARM_CP_STATE_AA32,
5101 .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
5102 .access = PL2_RW, .accessfn = access_el3_aa32ns,
5103 .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
5104 { .name = "HPFAR_EL2", .state = ARM_CP_STATE_AA64,
5105 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
5106 .access = PL2_RW,
5107 .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
2a5a9abd
AF
5108 { .name = "HSTR_EL2", .state = ARM_CP_STATE_BOTH,
5109 .cp = 15, .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 3,
5110 .access = PL2_RW,
5111 .fieldoffset = offsetof(CPUARMState, cp15.hstr_el2) },
3b685ba7
EI
5112 REGINFO_SENTINEL
5113};
5114
ce4afed8
PM
5115static const ARMCPRegInfo el2_v8_cp_reginfo[] = {
5116 { .name = "HCR2", .state = ARM_CP_STATE_AA32,
89430fc6 5117 .type = ARM_CP_ALIAS | ARM_CP_IO,
ce4afed8
PM
5118 .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 4,
5119 .access = PL2_RW,
5120 .fieldoffset = offsetofhigh32(CPUARMState, cp15.hcr_el2),
5121 .writefn = hcr_writehigh },
5122 REGINFO_SENTINEL
5123};
5124
2f027fc5
PM
5125static CPAccessResult nsacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
5126 bool isread)
5127{
5128 /* The NSACR is RW at EL3, and RO for NS EL1 and NS EL2.
5129 * At Secure EL1 it traps to EL3.
5130 */
5131 if (arm_current_el(env) == 3) {
5132 return CP_ACCESS_OK;
5133 }
5134 if (arm_is_secure_below_el3(env)) {
5135 return CP_ACCESS_TRAP_EL3;
5136 }
5137 /* Accesses from EL1 NS and EL2 NS are UNDEF for write but allow reads. */
5138 if (isread) {
5139 return CP_ACCESS_OK;
5140 }
5141 return CP_ACCESS_TRAP_UNCATEGORIZED;
5142}
5143
60fb1a87
GB
5144static const ARMCPRegInfo el3_cp_reginfo[] = {
5145 { .name = "SCR_EL3", .state = ARM_CP_STATE_AA64,
5146 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 0,
5147 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.scr_el3),
5148 .resetvalue = 0, .writefn = scr_write },
f80741d1 5149 { .name = "SCR", .type = ARM_CP_ALIAS | ARM_CP_NEWEL,
60fb1a87 5150 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 0,
efe4a274
PM
5151 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
5152 .fieldoffset = offsetoflow32(CPUARMState, cp15.scr_el3),
b061a82b 5153 .writefn = scr_write },
60fb1a87
GB
5154 { .name = "SDER32_EL3", .state = ARM_CP_STATE_AA64,
5155 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 1,
5156 .access = PL3_RW, .resetvalue = 0,
5157 .fieldoffset = offsetof(CPUARMState, cp15.sder) },
5158 { .name = "SDER",
5159 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 1,
5160 .access = PL3_RW, .resetvalue = 0,
5161 .fieldoffset = offsetoflow32(CPUARMState, cp15.sder) },
60fb1a87 5162 { .name = "MVBAR", .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
efe4a274
PM
5163 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
5164 .writefn = vbar_write, .resetvalue = 0,
60fb1a87 5165 .fieldoffset = offsetof(CPUARMState, cp15.mvbar) },
7dd8c9af
FA
5166 { .name = "TTBR0_EL3", .state = ARM_CP_STATE_AA64,
5167 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 0,
f478847f 5168 .access = PL3_RW, .resetvalue = 0,
7dd8c9af 5169 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[3]) },
11f136ee
FA
5170 { .name = "TCR_EL3", .state = ARM_CP_STATE_AA64,
5171 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 2,
6459b94c
PM
5172 .access = PL3_RW,
5173 /* no .writefn needed as this can't cause an ASID change;
811595a2
PM
5174 * we must provide a .raw_writefn and .resetfn because we handle
5175 * reset and migration for the AArch32 TTBCR(S), which might be
5176 * using mask and base_mask.
6459b94c 5177 */
811595a2 5178 .resetfn = vmsa_ttbcr_reset, .raw_writefn = vmsa_ttbcr_raw_write,
11f136ee 5179 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[3]) },
81547d66 5180 { .name = "ELR_EL3", .state = ARM_CP_STATE_AA64,
7a0e58fa 5181 .type = ARM_CP_ALIAS,
81547d66
EI
5182 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 1,
5183 .access = PL3_RW,
5184 .fieldoffset = offsetof(CPUARMState, elr_el[3]) },
f2c30f42 5185 { .name = "ESR_EL3", .state = ARM_CP_STATE_AA64,
f2c30f42
EI
5186 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 2, .opc2 = 0,
5187 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[3]) },
63b60551
EI
5188 { .name = "FAR_EL3", .state = ARM_CP_STATE_AA64,
5189 .opc0 = 3, .opc1 = 6, .crn = 6, .crm = 0, .opc2 = 0,
5190 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[3]) },
81547d66 5191 { .name = "SPSR_EL3", .state = ARM_CP_STATE_AA64,
7a0e58fa 5192 .type = ARM_CP_ALIAS,
81547d66 5193 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 0,
99a99c1f
SB
5194 .access = PL3_RW,
5195 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_MON]) },
a1ba125c
EI
5196 { .name = "VBAR_EL3", .state = ARM_CP_STATE_AA64,
5197 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 0,
5198 .access = PL3_RW, .writefn = vbar_write,
5199 .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[3]),
5200 .resetvalue = 0 },
c6f19164
GB
5201 { .name = "CPTR_EL3", .state = ARM_CP_STATE_AA64,
5202 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 2,
5203 .access = PL3_RW, .accessfn = cptr_access, .resetvalue = 0,
5204 .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[3]) },
4cfb8ad8
PM
5205 { .name = "TPIDR_EL3", .state = ARM_CP_STATE_AA64,
5206 .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 2,
5207 .access = PL3_RW, .resetvalue = 0,
5208 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[3]) },
2179ef95
PM
5209 { .name = "AMAIR_EL3", .state = ARM_CP_STATE_AA64,
5210 .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 3, .opc2 = 0,
5211 .access = PL3_RW, .type = ARM_CP_CONST,
5212 .resetvalue = 0 },
37cd6c24
PM
5213 { .name = "AFSR0_EL3", .state = ARM_CP_STATE_BOTH,
5214 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 0,
5215 .access = PL3_RW, .type = ARM_CP_CONST,
5216 .resetvalue = 0 },
5217 { .name = "AFSR1_EL3", .state = ARM_CP_STATE_BOTH,
5218 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 1,
5219 .access = PL3_RW, .type = ARM_CP_CONST,
5220 .resetvalue = 0 },
43efaa33
PM
5221 { .name = "TLBI_ALLE3IS", .state = ARM_CP_STATE_AA64,
5222 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 0,
5223 .access = PL3_W, .type = ARM_CP_NO_RAW,
5224 .writefn = tlbi_aa64_alle3is_write },
5225 { .name = "TLBI_VAE3IS", .state = ARM_CP_STATE_AA64,
5226 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 1,
5227 .access = PL3_W, .type = ARM_CP_NO_RAW,
5228 .writefn = tlbi_aa64_vae3is_write },
5229 { .name = "TLBI_VALE3IS", .state = ARM_CP_STATE_AA64,
5230 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 5,
5231 .access = PL3_W, .type = ARM_CP_NO_RAW,
5232 .writefn = tlbi_aa64_vae3is_write },
5233 { .name = "TLBI_ALLE3", .state = ARM_CP_STATE_AA64,
5234 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 0,
5235 .access = PL3_W, .type = ARM_CP_NO_RAW,
5236 .writefn = tlbi_aa64_alle3_write },
5237 { .name = "TLBI_VAE3", .state = ARM_CP_STATE_AA64,
5238 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 1,
5239 .access = PL3_W, .type = ARM_CP_NO_RAW,
5240 .writefn = tlbi_aa64_vae3_write },
5241 { .name = "TLBI_VALE3", .state = ARM_CP_STATE_AA64,
5242 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 5,
5243 .access = PL3_W, .type = ARM_CP_NO_RAW,
5244 .writefn = tlbi_aa64_vae3_write },
0f1a3b24
FA
5245 REGINFO_SENTINEL
5246};
5247
3f208fd7
PM
5248static CPAccessResult ctr_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
5249 bool isread)
7da845b0
PM
5250{
5251 /* Only accessible in EL0 if SCTLR.UCT is set (and only in AArch64,
5252 * but the AArch32 CTR has its own reginfo struct)
5253 */
137feaa9 5254 if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_UCT)) {
7da845b0
PM
5255 return CP_ACCESS_TRAP;
5256 }
630fcd4d
MZ
5257
5258 if (arm_current_el(env) < 2 && arm_hcr_el2_eff(env) & HCR_TID2) {
5259 return CP_ACCESS_TRAP_EL2;
5260 }
5261
7da845b0
PM
5262 return CP_ACCESS_OK;
5263}
5264
1424ca8d
DM
5265static void oslar_write(CPUARMState *env, const ARMCPRegInfo *ri,
5266 uint64_t value)
5267{
5268 /* Writes to OSLAR_EL1 may update the OS lock status, which can be
5269 * read via a bit in OSLSR_EL1.
5270 */
5271 int oslock;
5272
5273 if (ri->state == ARM_CP_STATE_AA32) {
5274 oslock = (value == 0xC5ACCE55);
5275 } else {
5276 oslock = value & 1;
5277 }
5278
5279 env->cp15.oslsr_el1 = deposit32(env->cp15.oslsr_el1, 1, 1, oslock);
5280}
5281
50300698 5282static const ARMCPRegInfo debug_cp_reginfo[] = {
50300698 5283 /* DBGDRAR, DBGDSAR: always RAZ since we don't implement memory mapped
10aae104
PM
5284 * debug components. The AArch64 version of DBGDRAR is named MDRAR_EL1;
5285 * unlike DBGDRAR it is never accessible from EL0.
5286 * DBGDSAR is deprecated and must RAZ from v8 anyway, so it has no AArch64
5287 * accessor.
50300698
PM
5288 */
5289 { .name = "DBGDRAR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 0,
91b0a238
PM
5290 .access = PL0_R, .accessfn = access_tdra,
5291 .type = ARM_CP_CONST, .resetvalue = 0 },
10aae104
PM
5292 { .name = "MDRAR_EL1", .state = ARM_CP_STATE_AA64,
5293 .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
91b0a238
PM
5294 .access = PL1_R, .accessfn = access_tdra,
5295 .type = ARM_CP_CONST, .resetvalue = 0 },
50300698 5296 { .name = "DBGDSAR", .cp = 14, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
91b0a238
PM
5297 .access = PL0_R, .accessfn = access_tdra,
5298 .type = ARM_CP_CONST, .resetvalue = 0 },
17a9eb53 5299 /* Monitor debug system control register; the 32-bit alias is DBGDSCRext. */
10aae104
PM
5300 { .name = "MDSCR_EL1", .state = ARM_CP_STATE_BOTH,
5301 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
d6c8cf81 5302 .access = PL1_RW, .accessfn = access_tda,
0e5e8935
PM
5303 .fieldoffset = offsetof(CPUARMState, cp15.mdscr_el1),
5304 .resetvalue = 0 },
5e8b12ff
PM
5305 /* MDCCSR_EL0, aka DBGDSCRint. This is a read-only mirror of MDSCR_EL1.
5306 * We don't implement the configurable EL0 access.
5307 */
5308 { .name = "MDCCSR_EL0", .state = ARM_CP_STATE_BOTH,
5309 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
7a0e58fa 5310 .type = ARM_CP_ALIAS,
d6c8cf81 5311 .access = PL1_R, .accessfn = access_tda,
b061a82b 5312 .fieldoffset = offsetof(CPUARMState, cp15.mdscr_el1), },
10aae104
PM
5313 { .name = "OSLAR_EL1", .state = ARM_CP_STATE_BOTH,
5314 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 4,
1424ca8d 5315 .access = PL1_W, .type = ARM_CP_NO_RAW,
187f678d 5316 .accessfn = access_tdosa,
1424ca8d
DM
5317 .writefn = oslar_write },
5318 { .name = "OSLSR_EL1", .state = ARM_CP_STATE_BOTH,
5319 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 4,
5320 .access = PL1_R, .resetvalue = 10,
187f678d 5321 .accessfn = access_tdosa,
1424ca8d 5322 .fieldoffset = offsetof(CPUARMState, cp15.oslsr_el1) },
5e8b12ff
PM
5323 /* Dummy OSDLR_EL1: 32-bit Linux will read this */
5324 { .name = "OSDLR_EL1", .state = ARM_CP_STATE_BOTH,
5325 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 4,
187f678d
PM
5326 .access = PL1_RW, .accessfn = access_tdosa,
5327 .type = ARM_CP_NOP },
5e8b12ff
PM
5328 /* Dummy DBGVCR: Linux wants to clear this on startup, but we don't
5329 * implement vector catch debug events yet.
5330 */
5331 { .name = "DBGVCR",
5332 .cp = 14, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
d6c8cf81
PM
5333 .access = PL1_RW, .accessfn = access_tda,
5334 .type = ARM_CP_NOP },
4d2ec4da
PM
5335 /* Dummy DBGVCR32_EL2 (which is only for a 64-bit hypervisor
5336 * to save and restore a 32-bit guest's DBGVCR)
5337 */
5338 { .name = "DBGVCR32_EL2", .state = ARM_CP_STATE_AA64,
5339 .opc0 = 2, .opc1 = 4, .crn = 0, .crm = 7, .opc2 = 0,
5340 .access = PL2_RW, .accessfn = access_tda,
5341 .type = ARM_CP_NOP },
5dbdc434
PM
5342 /* Dummy MDCCINT_EL1, since we don't implement the Debug Communications
5343 * Channel but Linux may try to access this register. The 32-bit
5344 * alias is DBGDCCINT.
5345 */
5346 { .name = "MDCCINT_EL1", .state = ARM_CP_STATE_BOTH,
5347 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0,
5348 .access = PL1_RW, .accessfn = access_tda,
5349 .type = ARM_CP_NOP },
50300698
PM
5350 REGINFO_SENTINEL
5351};
5352
5353static const ARMCPRegInfo debug_lpae_cp_reginfo[] = {
5354 /* 64 bit access versions of the (dummy) debug registers */
5355 { .name = "DBGDRAR", .cp = 14, .crm = 1, .opc1 = 0,
5356 .access = PL0_R, .type = ARM_CP_CONST|ARM_CP_64BIT, .resetvalue = 0 },
5357 { .name = "DBGDSAR", .cp = 14, .crm = 2, .opc1 = 0,
5358 .access = PL0_R, .type = ARM_CP_CONST|ARM_CP_64BIT, .resetvalue = 0 },
5359 REGINFO_SENTINEL
5360};
5361
60eed086
RH
5362/* Return the exception level to which exceptions should be taken
5363 * via SVEAccessTrap. If an exception should be routed through
5364 * AArch64.AdvSIMDFPAccessTrap, return 0; fp_exception_el should
5365 * take care of raising that exception.
5366 * C.f. the ARM pseudocode function CheckSVEEnabled.
5be5e8ed 5367 */
ced31551 5368int sve_exception_el(CPUARMState *env, int el)
5be5e8ed
RH
5369{
5370#ifndef CONFIG_USER_ONLY
2de7ace2 5371 if (el <= 1) {
60eed086
RH
5372 bool disabled = false;
5373
5374 /* The CPACR.ZEN controls traps to EL1:
5375 * 0, 2 : trap EL0 and EL1 accesses
5376 * 1 : trap only EL0 accesses
5377 * 3 : trap no accesses
5378 */
5379 if (!extract32(env->cp15.cpacr_el1, 16, 1)) {
5380 disabled = true;
5381 } else if (!extract32(env->cp15.cpacr_el1, 17, 1)) {
2de7ace2 5382 disabled = el == 0;
5be5e8ed 5383 }
60eed086
RH
5384 if (disabled) {
5385 /* route_to_el2 */
5386 return (arm_feature(env, ARM_FEATURE_EL2)
7c208e0f 5387 && (arm_hcr_el2_eff(env) & HCR_TGE) ? 2 : 1);
5be5e8ed 5388 }
5be5e8ed 5389
60eed086
RH
5390 /* Check CPACR.FPEN. */
5391 if (!extract32(env->cp15.cpacr_el1, 20, 1)) {
5392 disabled = true;
5393 } else if (!extract32(env->cp15.cpacr_el1, 21, 1)) {
2de7ace2 5394 disabled = el == 0;
5be5e8ed 5395 }
60eed086
RH
5396 if (disabled) {
5397 return 0;
5be5e8ed 5398 }
5be5e8ed
RH
5399 }
5400
60eed086
RH
5401 /* CPTR_EL2. Since TZ and TFP are positive,
5402 * they will be zero when EL2 is not present.
5403 */
2de7ace2 5404 if (el <= 2 && !arm_is_secure_below_el3(env)) {
60eed086
RH
5405 if (env->cp15.cptr_el[2] & CPTR_TZ) {
5406 return 2;
5407 }
5408 if (env->cp15.cptr_el[2] & CPTR_TFP) {
5409 return 0;
5410 }
5be5e8ed
RH
5411 }
5412
60eed086
RH
5413 /* CPTR_EL3. Since EZ is negative we must check for EL3. */
5414 if (arm_feature(env, ARM_FEATURE_EL3)
5415 && !(env->cp15.cptr_el[3] & CPTR_EZ)) {
5be5e8ed
RH
5416 return 3;
5417 }
5418#endif
5419 return 0;
5420}
5421
0df9142d
AJ
5422static uint32_t sve_zcr_get_valid_len(ARMCPU *cpu, uint32_t start_len)
5423{
6e553f2a 5424 uint32_t end_len;
0df9142d 5425
6e553f2a
RH
5426 end_len = start_len &= 0xf;
5427 if (!test_bit(start_len, cpu->sve_vq_map)) {
5428 end_len = find_last_bit(cpu->sve_vq_map, start_len);
5429 assert(end_len < start_len);
5430 }
5431 return end_len;
0df9142d
AJ
5432}
5433
0ab5953b
RH
5434/*
5435 * Given that SVE is enabled, return the vector length for EL.
5436 */
ced31551 5437uint32_t sve_zcr_len_for_el(CPUARMState *env, int el)
0ab5953b 5438{
2fc0cc0e 5439 ARMCPU *cpu = env_archcpu(env);
0ab5953b
RH
5440 uint32_t zcr_len = cpu->sve_max_vq - 1;
5441
5442 if (el <= 1) {
5443 zcr_len = MIN(zcr_len, 0xf & (uint32_t)env->vfp.zcr_el[1]);
5444 }
6a02a732 5445 if (el <= 2 && arm_feature(env, ARM_FEATURE_EL2)) {
0ab5953b
RH
5446 zcr_len = MIN(zcr_len, 0xf & (uint32_t)env->vfp.zcr_el[2]);
5447 }
6a02a732 5448 if (arm_feature(env, ARM_FEATURE_EL3)) {
0ab5953b
RH
5449 zcr_len = MIN(zcr_len, 0xf & (uint32_t)env->vfp.zcr_el[3]);
5450 }
0df9142d
AJ
5451
5452 return sve_zcr_get_valid_len(cpu, zcr_len);
0ab5953b
RH
5453}
5454
5be5e8ed
RH
5455static void zcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
5456 uint64_t value)
5457{
0ab5953b
RH
5458 int cur_el = arm_current_el(env);
5459 int old_len = sve_zcr_len_for_el(env, cur_el);
5460 int new_len;
5461
5be5e8ed 5462 /* Bits other than [3:0] are RAZ/WI. */
7b351d98 5463 QEMU_BUILD_BUG_ON(ARM_MAX_VQ > 16);
5be5e8ed 5464 raw_write(env, ri, value & 0xf);
0ab5953b
RH
5465
5466 /*
5467 * Because we arrived here, we know both FP and SVE are enabled;
5468 * otherwise we would have trapped access to the ZCR_ELn register.
5469 */
5470 new_len = sve_zcr_len_for_el(env, cur_el);
5471 if (new_len < old_len) {
5472 aarch64_sve_narrow_vq(env, new_len + 1);
5473 }
5be5e8ed
RH
5474}
5475
5476static const ARMCPRegInfo zcr_el1_reginfo = {
5477 .name = "ZCR_EL1", .state = ARM_CP_STATE_AA64,
5478 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 0,
11d7870b 5479 .access = PL1_RW, .type = ARM_CP_SVE,
5be5e8ed
RH
5480 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[1]),
5481 .writefn = zcr_write, .raw_writefn = raw_write
5482};
5483
5484static const ARMCPRegInfo zcr_el2_reginfo = {
5485 .name = "ZCR_EL2", .state = ARM_CP_STATE_AA64,
5486 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 0,
11d7870b 5487 .access = PL2_RW, .type = ARM_CP_SVE,
5be5e8ed
RH
5488 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[2]),
5489 .writefn = zcr_write, .raw_writefn = raw_write
5490};
5491
5492static const ARMCPRegInfo zcr_no_el2_reginfo = {
5493 .name = "ZCR_EL2", .state = ARM_CP_STATE_AA64,
5494 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 0,
11d7870b 5495 .access = PL2_RW, .type = ARM_CP_SVE,
5be5e8ed
RH
5496 .readfn = arm_cp_read_zero, .writefn = arm_cp_write_ignore
5497};
5498
5499static const ARMCPRegInfo zcr_el3_reginfo = {
5500 .name = "ZCR_EL3", .state = ARM_CP_STATE_AA64,
5501 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 2, .opc2 = 0,
11d7870b 5502 .access = PL3_RW, .type = ARM_CP_SVE,
5be5e8ed
RH
5503 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[3]),
5504 .writefn = zcr_write, .raw_writefn = raw_write
5505};
5506
9ee98ce8
PM
5507void hw_watchpoint_update(ARMCPU *cpu, int n)
5508{
5509 CPUARMState *env = &cpu->env;
5510 vaddr len = 0;
5511 vaddr wvr = env->cp15.dbgwvr[n];
5512 uint64_t wcr = env->cp15.dbgwcr[n];
5513 int mask;
5514 int flags = BP_CPU | BP_STOP_BEFORE_ACCESS;
5515
5516 if (env->cpu_watchpoint[n]) {
5517 cpu_watchpoint_remove_by_ref(CPU(cpu), env->cpu_watchpoint[n]);
5518 env->cpu_watchpoint[n] = NULL;
5519 }
5520
5521 if (!extract64(wcr, 0, 1)) {
5522 /* E bit clear : watchpoint disabled */
5523 return;
5524 }
5525
5526 switch (extract64(wcr, 3, 2)) {
5527 case 0:
5528 /* LSC 00 is reserved and must behave as if the wp is disabled */
5529 return;
5530 case 1:
5531 flags |= BP_MEM_READ;
5532 break;
5533 case 2:
5534 flags |= BP_MEM_WRITE;
5535 break;
5536 case 3:
5537 flags |= BP_MEM_ACCESS;
5538 break;
5539 }
5540
5541 /* Attempts to use both MASK and BAS fields simultaneously are
5542 * CONSTRAINED UNPREDICTABLE; we opt to ignore BAS in this case,
5543 * thus generating a watchpoint for every byte in the masked region.
5544 */
5545 mask = extract64(wcr, 24, 4);
5546 if (mask == 1 || mask == 2) {
5547 /* Reserved values of MASK; we must act as if the mask value was
5548 * some non-reserved value, or as if the watchpoint were disabled.
5549 * We choose the latter.
5550 */
5551 return;
5552 } else if (mask) {
5553 /* Watchpoint covers an aligned area up to 2GB in size */
5554 len = 1ULL << mask;
5555 /* If masked bits in WVR are not zero it's CONSTRAINED UNPREDICTABLE
5556 * whether the watchpoint fires when the unmasked bits match; we opt
5557 * to generate the exceptions.
5558 */
5559 wvr &= ~(len - 1);
5560 } else {
5561 /* Watchpoint covers bytes defined by the byte address select bits */
5562 int bas = extract64(wcr, 5, 8);
5563 int basstart;
5564
5565 if (bas == 0) {
5566 /* This must act as if the watchpoint is disabled */
5567 return;
5568 }
5569
5570 if (extract64(wvr, 2, 1)) {
5571 /* Deprecated case of an only 4-aligned address. BAS[7:4] are
5572 * ignored, and BAS[3:0] define which bytes to watch.
5573 */
5574 bas &= 0xf;
5575 }
5576 /* The BAS bits are supposed to be programmed to indicate a contiguous
5577 * range of bytes. Otherwise it is CONSTRAINED UNPREDICTABLE whether
5578 * we fire for each byte in the word/doubleword addressed by the WVR.
5579 * We choose to ignore any non-zero bits after the first range of 1s.
5580 */
5581 basstart = ctz32(bas);
5582 len = cto32(bas >> basstart);
5583 wvr += basstart;
5584 }
5585
5586 cpu_watchpoint_insert(CPU(cpu), wvr, len, flags,
5587 &env->cpu_watchpoint[n]);
5588}
5589
5590void hw_watchpoint_update_all(ARMCPU *cpu)
5591{
5592 int i;
5593 CPUARMState *env = &cpu->env;
5594
5595 /* Completely clear out existing QEMU watchpoints and our array, to
5596 * avoid possible stale entries following migration load.
5597 */
5598 cpu_watchpoint_remove_all(CPU(cpu), BP_CPU);
5599 memset(env->cpu_watchpoint, 0, sizeof(env->cpu_watchpoint));
5600
5601 for (i = 0; i < ARRAY_SIZE(cpu->env.cpu_watchpoint); i++) {
5602 hw_watchpoint_update(cpu, i);
5603 }
5604}
5605
5606static void dbgwvr_write(CPUARMState *env, const ARMCPRegInfo *ri,
5607 uint64_t value)
5608{
2fc0cc0e 5609 ARMCPU *cpu = env_archcpu(env);
9ee98ce8
PM
5610 int i = ri->crm;
5611
5612 /* Bits [63:49] are hardwired to the value of bit [48]; that is, the
5613 * register reads and behaves as if values written are sign extended.
5614 * Bits [1:0] are RES0.
5615 */
5616 value = sextract64(value, 0, 49) & ~3ULL;
5617
5618 raw_write(env, ri, value);
5619 hw_watchpoint_update(cpu, i);
5620}
5621
5622static void dbgwcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
5623 uint64_t value)
5624{
2fc0cc0e 5625 ARMCPU *cpu = env_archcpu(env);
9ee98ce8
PM
5626 int i = ri->crm;
5627
5628 raw_write(env, ri, value);
5629 hw_watchpoint_update(cpu, i);
5630}
5631
46747d15
PM
5632void hw_breakpoint_update(ARMCPU *cpu, int n)
5633{
5634 CPUARMState *env = &cpu->env;
5635 uint64_t bvr = env->cp15.dbgbvr[n];
5636 uint64_t bcr = env->cp15.dbgbcr[n];
5637 vaddr addr;
5638 int bt;
5639 int flags = BP_CPU;
5640
5641 if (env->cpu_breakpoint[n]) {
5642 cpu_breakpoint_remove_by_ref(CPU(cpu), env->cpu_breakpoint[n]);
5643 env->cpu_breakpoint[n] = NULL;
5644 }
5645
5646 if (!extract64(bcr, 0, 1)) {
5647 /* E bit clear : watchpoint disabled */
5648 return;
5649 }
5650
5651 bt = extract64(bcr, 20, 4);
5652
5653 switch (bt) {
5654 case 4: /* unlinked address mismatch (reserved if AArch64) */
5655 case 5: /* linked address mismatch (reserved if AArch64) */
5656 qemu_log_mask(LOG_UNIMP,
0221c8fd 5657 "arm: address mismatch breakpoint types not implemented\n");
46747d15
PM
5658 return;
5659 case 0: /* unlinked address match */
5660 case 1: /* linked address match */
5661 {
5662 /* Bits [63:49] are hardwired to the value of bit [48]; that is,
5663 * we behave as if the register was sign extended. Bits [1:0] are
5664 * RES0. The BAS field is used to allow setting breakpoints on 16
5665 * bit wide instructions; it is CONSTRAINED UNPREDICTABLE whether
5666 * a bp will fire if the addresses covered by the bp and the addresses
5667 * covered by the insn overlap but the insn doesn't start at the
5668 * start of the bp address range. We choose to require the insn and
5669 * the bp to have the same address. The constraints on writing to
5670 * BAS enforced in dbgbcr_write mean we have only four cases:
5671 * 0b0000 => no breakpoint
5672 * 0b0011 => breakpoint on addr
5673 * 0b1100 => breakpoint on addr + 2
5674 * 0b1111 => breakpoint on addr
5675 * See also figure D2-3 in the v8 ARM ARM (DDI0487A.c).
5676 */
5677 int bas = extract64(bcr, 5, 4);
5678 addr = sextract64(bvr, 0, 49) & ~3ULL;
5679 if (bas == 0) {
5680 return;
5681 }
5682 if (bas == 0xc) {
5683 addr += 2;
5684 }
5685 break;
5686 }
5687 case 2: /* unlinked context ID match */
5688 case 8: /* unlinked VMID match (reserved if no EL2) */
5689 case 10: /* unlinked context ID and VMID match (reserved if no EL2) */
5690 qemu_log_mask(LOG_UNIMP,
0221c8fd 5691 "arm: unlinked context breakpoint types not implemented\n");
46747d15
PM
5692 return;
5693 case 9: /* linked VMID match (reserved if no EL2) */
5694 case 11: /* linked context ID and VMID match (reserved if no EL2) */
5695 case 3: /* linked context ID match */
5696 default:
5697 /* We must generate no events for Linked context matches (unless
5698 * they are linked to by some other bp/wp, which is handled in
5699 * updates for the linking bp/wp). We choose to also generate no events
5700 * for reserved values.
5701 */
5702 return;
5703 }
5704
5705 cpu_breakpoint_insert(CPU(cpu), addr, flags, &env->cpu_breakpoint[n]);
5706}
5707
5708void hw_breakpoint_update_all(ARMCPU *cpu)
5709{
5710 int i;
5711 CPUARMState *env = &cpu->env;
5712
5713 /* Completely clear out existing QEMU breakpoints and our array, to
5714 * avoid possible stale entries following migration load.
5715 */
5716 cpu_breakpoint_remove_all(CPU(cpu), BP_CPU);
5717 memset(env->cpu_breakpoint, 0, sizeof(env->cpu_breakpoint));
5718
5719 for (i = 0; i < ARRAY_SIZE(cpu->env.cpu_breakpoint); i++) {
5720 hw_breakpoint_update(cpu, i);
5721 }
5722}
5723
5724static void dbgbvr_write(CPUARMState *env, const ARMCPRegInfo *ri,
5725 uint64_t value)
5726{
2fc0cc0e 5727 ARMCPU *cpu = env_archcpu(env);
46747d15
PM
5728 int i = ri->crm;
5729
5730 raw_write(env, ri, value);
5731 hw_breakpoint_update(cpu, i);
5732}
5733
5734static void dbgbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
5735 uint64_t value)
5736{
2fc0cc0e 5737 ARMCPU *cpu = env_archcpu(env);
46747d15
PM
5738 int i = ri->crm;
5739
5740 /* BAS[3] is a read-only copy of BAS[2], and BAS[1] a read-only
5741 * copy of BAS[0].
5742 */
5743 value = deposit64(value, 6, 1, extract64(value, 5, 1));
5744 value = deposit64(value, 8, 1, extract64(value, 7, 1));
5745
5746 raw_write(env, ri, value);
5747 hw_breakpoint_update(cpu, i);
5748}
5749
50300698 5750static void define_debug_regs(ARMCPU *cpu)
0b45451e 5751{
50300698
PM
5752 /* Define v7 and v8 architectural debug registers.
5753 * These are just dummy implementations for now.
0b45451e
PM
5754 */
5755 int i;
3ff6fc91 5756 int wrps, brps, ctx_cmps;
48eb3ae6
PM
5757 ARMCPRegInfo dbgdidr = {
5758 .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0,
d6c8cf81
PM
5759 .access = PL0_R, .accessfn = access_tda,
5760 .type = ARM_CP_CONST, .resetvalue = cpu->dbgdidr,
48eb3ae6
PM
5761 };
5762
3ff6fc91 5763 /* Note that all these register fields hold "number of Xs minus 1". */
48eb3ae6
PM
5764 brps = extract32(cpu->dbgdidr, 24, 4);
5765 wrps = extract32(cpu->dbgdidr, 28, 4);
3ff6fc91
PM
5766 ctx_cmps = extract32(cpu->dbgdidr, 20, 4);
5767
5768 assert(ctx_cmps <= brps);
48eb3ae6
PM
5769
5770 /* The DBGDIDR and ID_AA64DFR0_EL1 define various properties
5771 * of the debug registers such as number of breakpoints;
5772 * check that if they both exist then they agree.
5773 */
5774 if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64)) {
5775 assert(extract32(cpu->id_aa64dfr0, 12, 4) == brps);
5776 assert(extract32(cpu->id_aa64dfr0, 20, 4) == wrps);
3ff6fc91 5777 assert(extract32(cpu->id_aa64dfr0, 28, 4) == ctx_cmps);
48eb3ae6 5778 }
0b45451e 5779
48eb3ae6 5780 define_one_arm_cp_reg(cpu, &dbgdidr);
50300698
PM
5781 define_arm_cp_regs(cpu, debug_cp_reginfo);
5782
5783 if (arm_feature(&cpu->env, ARM_FEATURE_LPAE)) {
5784 define_arm_cp_regs(cpu, debug_lpae_cp_reginfo);
5785 }
5786
48eb3ae6 5787 for (i = 0; i < brps + 1; i++) {
0b45451e 5788 ARMCPRegInfo dbgregs[] = {
10aae104
PM
5789 { .name = "DBGBVR", .state = ARM_CP_STATE_BOTH,
5790 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 4,
d6c8cf81 5791 .access = PL1_RW, .accessfn = access_tda,
46747d15
PM
5792 .fieldoffset = offsetof(CPUARMState, cp15.dbgbvr[i]),
5793 .writefn = dbgbvr_write, .raw_writefn = raw_write
5794 },
10aae104
PM
5795 { .name = "DBGBCR", .state = ARM_CP_STATE_BOTH,
5796 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 5,
d6c8cf81 5797 .access = PL1_RW, .accessfn = access_tda,
46747d15
PM
5798 .fieldoffset = offsetof(CPUARMState, cp15.dbgbcr[i]),
5799 .writefn = dbgbcr_write, .raw_writefn = raw_write
5800 },
48eb3ae6
PM
5801 REGINFO_SENTINEL
5802 };
5803 define_arm_cp_regs(cpu, dbgregs);
5804 }
5805
5806 for (i = 0; i < wrps + 1; i++) {
5807 ARMCPRegInfo dbgregs[] = {
10aae104
PM
5808 { .name = "DBGWVR", .state = ARM_CP_STATE_BOTH,
5809 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 6,
d6c8cf81 5810 .access = PL1_RW, .accessfn = access_tda,
9ee98ce8
PM
5811 .fieldoffset = offsetof(CPUARMState, cp15.dbgwvr[i]),
5812 .writefn = dbgwvr_write, .raw_writefn = raw_write
5813 },
10aae104
PM
5814 { .name = "DBGWCR", .state = ARM_CP_STATE_BOTH,
5815 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 7,
d6c8cf81 5816 .access = PL1_RW, .accessfn = access_tda,
9ee98ce8
PM
5817 .fieldoffset = offsetof(CPUARMState, cp15.dbgwcr[i]),
5818 .writefn = dbgwcr_write, .raw_writefn = raw_write
5819 },
5820 REGINFO_SENTINEL
0b45451e
PM
5821 };
5822 define_arm_cp_regs(cpu, dbgregs);
5823 }
5824}
5825
96a8b92e
PM
5826/* We don't know until after realize whether there's a GICv3
5827 * attached, and that is what registers the gicv3 sysregs.
5828 * So we have to fill in the GIC fields in ID_PFR/ID_PFR1_EL1/ID_AA64PFR0_EL1
5829 * at runtime.
5830 */
5831static uint64_t id_pfr1_read(CPUARMState *env, const ARMCPRegInfo *ri)
5832{
2fc0cc0e 5833 ARMCPU *cpu = env_archcpu(env);
96a8b92e
PM
5834 uint64_t pfr1 = cpu->id_pfr1;
5835
5836 if (env->gicv3state) {
5837 pfr1 |= 1 << 28;
5838 }
5839 return pfr1;
5840}
5841
5842static uint64_t id_aa64pfr0_read(CPUARMState *env, const ARMCPRegInfo *ri)
5843{
2fc0cc0e 5844 ARMCPU *cpu = env_archcpu(env);
47576b94 5845 uint64_t pfr0 = cpu->isar.id_aa64pfr0;
96a8b92e
PM
5846
5847 if (env->gicv3state) {
5848 pfr0 |= 1 << 24;
5849 }
5850 return pfr0;
5851}
5852
2d7137c1
RH
5853/* Shared logic between LORID and the rest of the LOR* registers.
5854 * Secure state has already been delt with.
5855 */
5856static CPAccessResult access_lor_ns(CPUARMState *env)
5857{
5858 int el = arm_current_el(env);
5859
5860 if (el < 2 && (arm_hcr_el2_eff(env) & HCR_TLOR)) {
5861 return CP_ACCESS_TRAP_EL2;
5862 }
5863 if (el < 3 && (env->cp15.scr_el3 & SCR_TLOR)) {
5864 return CP_ACCESS_TRAP_EL3;
5865 }
5866 return CP_ACCESS_OK;
5867}
5868
5869static CPAccessResult access_lorid(CPUARMState *env, const ARMCPRegInfo *ri,
5870 bool isread)
5871{
5872 if (arm_is_secure_below_el3(env)) {
5873 /* Access ok in secure mode. */
5874 return CP_ACCESS_OK;
5875 }
5876 return access_lor_ns(env);
5877}
5878
5879static CPAccessResult access_lor_other(CPUARMState *env,
5880 const ARMCPRegInfo *ri, bool isread)
5881{
5882 if (arm_is_secure_below_el3(env)) {
5883 /* Access denied in secure mode. */
5884 return CP_ACCESS_TRAP;
5885 }
5886 return access_lor_ns(env);
5887}
5888
967aa94f
RH
5889#ifdef TARGET_AARCH64
5890static CPAccessResult access_pauth(CPUARMState *env, const ARMCPRegInfo *ri,
5891 bool isread)
5892{
5893 int el = arm_current_el(env);
5894
5895 if (el < 2 &&
5896 arm_feature(env, ARM_FEATURE_EL2) &&
5897 !(arm_hcr_el2_eff(env) & HCR_APK)) {
5898 return CP_ACCESS_TRAP_EL2;
5899 }
5900 if (el < 3 &&
5901 arm_feature(env, ARM_FEATURE_EL3) &&
5902 !(env->cp15.scr_el3 & SCR_APK)) {
5903 return CP_ACCESS_TRAP_EL3;
5904 }
5905 return CP_ACCESS_OK;
5906}
5907
5908static const ARMCPRegInfo pauth_reginfo[] = {
5909 { .name = "APDAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
5910 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 0,
5911 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 5912 .fieldoffset = offsetof(CPUARMState, keys.apda.lo) },
967aa94f
RH
5913 { .name = "APDAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
5914 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 1,
5915 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 5916 .fieldoffset = offsetof(CPUARMState, keys.apda.hi) },
967aa94f
RH
5917 { .name = "APDBKEYLO_EL1", .state = ARM_CP_STATE_AA64,
5918 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 2,
5919 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 5920 .fieldoffset = offsetof(CPUARMState, keys.apdb.lo) },
967aa94f
RH
5921 { .name = "APDBKEYHI_EL1", .state = ARM_CP_STATE_AA64,
5922 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 3,
5923 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 5924 .fieldoffset = offsetof(CPUARMState, keys.apdb.hi) },
967aa94f
RH
5925 { .name = "APGAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
5926 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 0,
5927 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 5928 .fieldoffset = offsetof(CPUARMState, keys.apga.lo) },
967aa94f
RH
5929 { .name = "APGAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
5930 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 1,
5931 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 5932 .fieldoffset = offsetof(CPUARMState, keys.apga.hi) },
967aa94f
RH
5933 { .name = "APIAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
5934 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 0,
5935 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 5936 .fieldoffset = offsetof(CPUARMState, keys.apia.lo) },
967aa94f
RH
5937 { .name = "APIAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
5938 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 1,
5939 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 5940 .fieldoffset = offsetof(CPUARMState, keys.apia.hi) },
967aa94f
RH
5941 { .name = "APIBKEYLO_EL1", .state = ARM_CP_STATE_AA64,
5942 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 2,
5943 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 5944 .fieldoffset = offsetof(CPUARMState, keys.apib.lo) },
967aa94f
RH
5945 { .name = "APIBKEYHI_EL1", .state = ARM_CP_STATE_AA64,
5946 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 3,
5947 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 5948 .fieldoffset = offsetof(CPUARMState, keys.apib.hi) },
967aa94f
RH
5949 REGINFO_SENTINEL
5950};
de390645
RH
5951
5952static uint64_t rndr_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
5953{
5954 Error *err = NULL;
5955 uint64_t ret;
5956
5957 /* Success sets NZCV = 0000. */
5958 env->NF = env->CF = env->VF = 0, env->ZF = 1;
5959
5960 if (qemu_guest_getrandom(&ret, sizeof(ret), &err) < 0) {
5961 /*
5962 * ??? Failed, for unknown reasons in the crypto subsystem.
5963 * The best we can do is log the reason and return the
5964 * timed-out indication to the guest. There is no reason
5965 * we know to expect this failure to be transitory, so the
5966 * guest may well hang retrying the operation.
5967 */
5968 qemu_log_mask(LOG_UNIMP, "%s: Crypto failure: %s",
5969 ri->name, error_get_pretty(err));
5970 error_free(err);
5971
5972 env->ZF = 0; /* NZCF = 0100 */
5973 return 0;
5974 }
5975 return ret;
5976}
5977
5978/* We do not support re-seeding, so the two registers operate the same. */
5979static const ARMCPRegInfo rndr_reginfo[] = {
5980 { .name = "RNDR", .state = ARM_CP_STATE_AA64,
5981 .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END | ARM_CP_IO,
5982 .opc0 = 3, .opc1 = 3, .crn = 2, .crm = 4, .opc2 = 0,
5983 .access = PL0_R, .readfn = rndr_readfn },
5984 { .name = "RNDRRS", .state = ARM_CP_STATE_AA64,
5985 .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END | ARM_CP_IO,
5986 .opc0 = 3, .opc1 = 3, .crn = 2, .crm = 4, .opc2 = 1,
5987 .access = PL0_R, .readfn = rndr_readfn },
5988 REGINFO_SENTINEL
5989};
0d57b499
BM
5990
5991#ifndef CONFIG_USER_ONLY
5992static void dccvap_writefn(CPUARMState *env, const ARMCPRegInfo *opaque,
5993 uint64_t value)
5994{
5995 ARMCPU *cpu = env_archcpu(env);
5996 /* CTR_EL0 System register -> DminLine, bits [19:16] */
5997 uint64_t dline_size = 4 << ((cpu->ctr >> 16) & 0xF);
5998 uint64_t vaddr_in = (uint64_t) value;
5999 uint64_t vaddr = vaddr_in & ~(dline_size - 1);
6000 void *haddr;
6001 int mem_idx = cpu_mmu_index(env, false);
6002
6003 /* This won't be crossing page boundaries */
6004 haddr = probe_read(env, vaddr, dline_size, mem_idx, GETPC());
6005 if (haddr) {
6006
6007 ram_addr_t offset;
6008 MemoryRegion *mr;
6009
6010 /* RCU lock is already being held */
6011 mr = memory_region_from_host(haddr, &offset);
6012
6013 if (mr) {
6014 memory_region_do_writeback(mr, offset, dline_size);
6015 }
6016 }
6017}
6018
6019static const ARMCPRegInfo dcpop_reg[] = {
6020 { .name = "DC_CVAP", .state = ARM_CP_STATE_AA64,
6021 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 1,
6022 .access = PL0_W, .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END,
6023 .accessfn = aa64_cacheop_access, .writefn = dccvap_writefn },
6024 REGINFO_SENTINEL
6025};
6026
6027static const ARMCPRegInfo dcpodp_reg[] = {
6028 { .name = "DC_CVADP", .state = ARM_CP_STATE_AA64,
6029 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 1,
6030 .access = PL0_W, .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END,
6031 .accessfn = aa64_cacheop_access, .writefn = dccvap_writefn },
6032 REGINFO_SENTINEL
6033};
6034#endif /*CONFIG_USER_ONLY*/
6035
967aa94f
RH
6036#endif
6037
cb570bd3
RH
6038static CPAccessResult access_predinv(CPUARMState *env, const ARMCPRegInfo *ri,
6039 bool isread)
6040{
6041 int el = arm_current_el(env);
6042
6043 if (el == 0) {
6044 uint64_t sctlr = arm_sctlr(env, el);
6045 if (!(sctlr & SCTLR_EnRCTX)) {
6046 return CP_ACCESS_TRAP;
6047 }
6048 } else if (el == 1) {
6049 uint64_t hcr = arm_hcr_el2_eff(env);
6050 if (hcr & HCR_NV) {
6051 return CP_ACCESS_TRAP_EL2;
6052 }
6053 }
6054 return CP_ACCESS_OK;
6055}
6056
6057static const ARMCPRegInfo predinv_reginfo[] = {
6058 { .name = "CFP_RCTX", .state = ARM_CP_STATE_AA64,
6059 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 4,
6060 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
6061 { .name = "DVP_RCTX", .state = ARM_CP_STATE_AA64,
6062 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 5,
6063 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
6064 { .name = "CPP_RCTX", .state = ARM_CP_STATE_AA64,
6065 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 7,
6066 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
6067 /*
6068 * Note the AArch32 opcodes have a different OPC1.
6069 */
6070 { .name = "CFPRCTX", .state = ARM_CP_STATE_AA32,
6071 .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 4,
6072 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
6073 { .name = "DVPRCTX", .state = ARM_CP_STATE_AA32,
6074 .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 5,
6075 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
6076 { .name = "CPPRCTX", .state = ARM_CP_STATE_AA32,
6077 .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 7,
6078 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
6079 REGINFO_SENTINEL
6080};
6081
6a4ef4e5
MZ
6082static CPAccessResult access_aa64_tid3(CPUARMState *env, const ARMCPRegInfo *ri,
6083 bool isread)
6084{
6085 if ((arm_current_el(env) < 2) && (arm_hcr_el2_eff(env) & HCR_TID3)) {
6086 return CP_ACCESS_TRAP_EL2;
6087 }
6088
6089 return CP_ACCESS_OK;
6090}
6091
6092static CPAccessResult access_aa32_tid3(CPUARMState *env, const ARMCPRegInfo *ri,
6093 bool isread)
6094{
6095 if (arm_feature(env, ARM_FEATURE_V8)) {
6096 return access_aa64_tid3(env, ri, isread);
6097 }
6098
6099 return CP_ACCESS_OK;
6100}
6101
f96f3d5f
MZ
6102static CPAccessResult access_jazelle(CPUARMState *env, const ARMCPRegInfo *ri,
6103 bool isread)
6104{
6105 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID0)) {
6106 return CP_ACCESS_TRAP_EL2;
6107 }
6108
6109 return CP_ACCESS_OK;
6110}
6111
6112static const ARMCPRegInfo jazelle_regs[] = {
6113 { .name = "JIDR",
6114 .cp = 14, .crn = 0, .crm = 0, .opc1 = 7, .opc2 = 0,
6115 .access = PL1_R, .accessfn = access_jazelle,
6116 .type = ARM_CP_CONST, .resetvalue = 0 },
6117 { .name = "JOSCR",
6118 .cp = 14, .crn = 1, .crm = 0, .opc1 = 7, .opc2 = 0,
6119 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
6120 { .name = "JMCR",
6121 .cp = 14, .crn = 2, .crm = 0, .opc1 = 7, .opc2 = 0,
6122 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
6123 REGINFO_SENTINEL
6124};
6125
e2a1a461
RH
6126static const ARMCPRegInfo vhe_reginfo[] = {
6127 { .name = "CONTEXTIDR_EL2", .state = ARM_CP_STATE_AA64,
6128 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 1,
6129 .access = PL2_RW,
6130 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[2]) },
ed30da8e
RH
6131 { .name = "TTBR1_EL2", .state = ARM_CP_STATE_AA64,
6132 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 1,
6133 .access = PL2_RW, .writefn = vmsa_tcr_ttbr_el2_write,
6134 .fieldoffset = offsetof(CPUARMState, cp15.ttbr1_el[2]) },
e2a1a461
RH
6135 REGINFO_SENTINEL
6136};
6137
2ceb98c0
PM
6138void register_cp_regs_for_features(ARMCPU *cpu)
6139{
6140 /* Register all the coprocessor registers based on feature bits */
6141 CPUARMState *env = &cpu->env;
6142 if (arm_feature(env, ARM_FEATURE_M)) {
6143 /* M profile has no coprocessor registers */
6144 return;
6145 }
6146
e9aa6c21 6147 define_arm_cp_regs(cpu, cp_reginfo);
9449fdf6
PM
6148 if (!arm_feature(env, ARM_FEATURE_V8)) {
6149 /* Must go early as it is full of wildcards that may be
6150 * overridden by later definitions.
6151 */
6152 define_arm_cp_regs(cpu, not_v8_cp_reginfo);
6153 }
6154
7d57f408 6155 if (arm_feature(env, ARM_FEATURE_V6)) {
8515a092
PM
6156 /* The ID registers all have impdef reset values */
6157 ARMCPRegInfo v6_idregs[] = {
0ff644a7
PM
6158 { .name = "ID_PFR0", .state = ARM_CP_STATE_BOTH,
6159 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
6160 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 6161 .accessfn = access_aa32_tid3,
8515a092 6162 .resetvalue = cpu->id_pfr0 },
96a8b92e
PM
6163 /* ID_PFR1 is not a plain ARM_CP_CONST because we don't know
6164 * the value of the GIC field until after we define these regs.
6165 */
0ff644a7
PM
6166 { .name = "ID_PFR1", .state = ARM_CP_STATE_BOTH,
6167 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 1,
96a8b92e 6168 .access = PL1_R, .type = ARM_CP_NO_RAW,
6a4ef4e5 6169 .accessfn = access_aa32_tid3,
96a8b92e
PM
6170 .readfn = id_pfr1_read,
6171 .writefn = arm_cp_write_ignore },
0ff644a7
PM
6172 { .name = "ID_DFR0", .state = ARM_CP_STATE_BOTH,
6173 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 2,
6174 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 6175 .accessfn = access_aa32_tid3,
8515a092 6176 .resetvalue = cpu->id_dfr0 },
0ff644a7
PM
6177 { .name = "ID_AFR0", .state = ARM_CP_STATE_BOTH,
6178 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 3,
6179 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 6180 .accessfn = access_aa32_tid3,
8515a092 6181 .resetvalue = cpu->id_afr0 },
0ff644a7
PM
6182 { .name = "ID_MMFR0", .state = ARM_CP_STATE_BOTH,
6183 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 4,
6184 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 6185 .accessfn = access_aa32_tid3,
8515a092 6186 .resetvalue = cpu->id_mmfr0 },
0ff644a7
PM
6187 { .name = "ID_MMFR1", .state = ARM_CP_STATE_BOTH,
6188 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 5,
6189 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 6190 .accessfn = access_aa32_tid3,
8515a092 6191 .resetvalue = cpu->id_mmfr1 },
0ff644a7
PM
6192 { .name = "ID_MMFR2", .state = ARM_CP_STATE_BOTH,
6193 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 6,
6194 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 6195 .accessfn = access_aa32_tid3,
8515a092 6196 .resetvalue = cpu->id_mmfr2 },
0ff644a7
PM
6197 { .name = "ID_MMFR3", .state = ARM_CP_STATE_BOTH,
6198 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 7,
6199 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 6200 .accessfn = access_aa32_tid3,
8515a092 6201 .resetvalue = cpu->id_mmfr3 },
0ff644a7
PM
6202 { .name = "ID_ISAR0", .state = ARM_CP_STATE_BOTH,
6203 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0,
6204 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 6205 .accessfn = access_aa32_tid3,
47576b94 6206 .resetvalue = cpu->isar.id_isar0 },
0ff644a7
PM
6207 { .name = "ID_ISAR1", .state = ARM_CP_STATE_BOTH,
6208 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 1,
6209 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 6210 .accessfn = access_aa32_tid3,
47576b94 6211 .resetvalue = cpu->isar.id_isar1 },
0ff644a7
PM
6212 { .name = "ID_ISAR2", .state = ARM_CP_STATE_BOTH,
6213 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
6214 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 6215 .accessfn = access_aa32_tid3,
47576b94 6216 .resetvalue = cpu->isar.id_isar2 },
0ff644a7
PM
6217 { .name = "ID_ISAR3", .state = ARM_CP_STATE_BOTH,
6218 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 3,
6219 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 6220 .accessfn = access_aa32_tid3,
47576b94 6221 .resetvalue = cpu->isar.id_isar3 },
0ff644a7
PM
6222 { .name = "ID_ISAR4", .state = ARM_CP_STATE_BOTH,
6223 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 4,
6224 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 6225 .accessfn = access_aa32_tid3,
47576b94 6226 .resetvalue = cpu->isar.id_isar4 },
0ff644a7
PM
6227 { .name = "ID_ISAR5", .state = ARM_CP_STATE_BOTH,
6228 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 5,
6229 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 6230 .accessfn = access_aa32_tid3,
47576b94 6231 .resetvalue = cpu->isar.id_isar5 },
e20d84c1
PM
6232 { .name = "ID_MMFR4", .state = ARM_CP_STATE_BOTH,
6233 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 6,
6234 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 6235 .accessfn = access_aa32_tid3,
e20d84c1 6236 .resetvalue = cpu->id_mmfr4 },
802abf40 6237 { .name = "ID_ISAR6", .state = ARM_CP_STATE_BOTH,
e20d84c1
PM
6238 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 7,
6239 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 6240 .accessfn = access_aa32_tid3,
47576b94 6241 .resetvalue = cpu->isar.id_isar6 },
8515a092
PM
6242 REGINFO_SENTINEL
6243 };
6244 define_arm_cp_regs(cpu, v6_idregs);
7d57f408
PM
6245 define_arm_cp_regs(cpu, v6_cp_reginfo);
6246 } else {
6247 define_arm_cp_regs(cpu, not_v6_cp_reginfo);
6248 }
4d31c596
PM
6249 if (arm_feature(env, ARM_FEATURE_V6K)) {
6250 define_arm_cp_regs(cpu, v6k_cp_reginfo);
6251 }
5e5cf9e3 6252 if (arm_feature(env, ARM_FEATURE_V7MP) &&
452a0955 6253 !arm_feature(env, ARM_FEATURE_PMSA)) {
995939a6
PM
6254 define_arm_cp_regs(cpu, v7mp_cp_reginfo);
6255 }
327dd510
AL
6256 if (arm_feature(env, ARM_FEATURE_V7VE)) {
6257 define_arm_cp_regs(cpu, pmovsset_cp_reginfo);
6258 }
e9aa6c21 6259 if (arm_feature(env, ARM_FEATURE_V7)) {
200ac0ef 6260 /* v7 performance monitor control register: same implementor
ac689a2e
AL
6261 * field as main ID register, and we implement four counters in
6262 * addition to the cycle count register.
200ac0ef 6263 */
ac689a2e 6264 unsigned int i, pmcrn = 4;
200ac0ef
PM
6265 ARMCPRegInfo pmcr = {
6266 .name = "PMCR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 0,
8521466b 6267 .access = PL0_RW,
7a0e58fa 6268 .type = ARM_CP_IO | ARM_CP_ALIAS,
8521466b 6269 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcr),
fcd25206
PM
6270 .accessfn = pmreg_access, .writefn = pmcr_write,
6271 .raw_writefn = raw_write,
200ac0ef 6272 };
8521466b
AF
6273 ARMCPRegInfo pmcr64 = {
6274 .name = "PMCR_EL0", .state = ARM_CP_STATE_AA64,
6275 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 0,
6276 .access = PL0_RW, .accessfn = pmreg_access,
6277 .type = ARM_CP_IO,
6278 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcr),
ac689a2e 6279 .resetvalue = (cpu->midr & 0xff000000) | (pmcrn << PMCRN_SHIFT),
8521466b
AF
6280 .writefn = pmcr_write, .raw_writefn = raw_write,
6281 };
7c2cb42b 6282 define_one_arm_cp_reg(cpu, &pmcr);
8521466b 6283 define_one_arm_cp_reg(cpu, &pmcr64);
5ecdd3e4
AL
6284 for (i = 0; i < pmcrn; i++) {
6285 char *pmevcntr_name = g_strdup_printf("PMEVCNTR%d", i);
6286 char *pmevcntr_el0_name = g_strdup_printf("PMEVCNTR%d_EL0", i);
6287 char *pmevtyper_name = g_strdup_printf("PMEVTYPER%d", i);
6288 char *pmevtyper_el0_name = g_strdup_printf("PMEVTYPER%d_EL0", i);
6289 ARMCPRegInfo pmev_regs[] = {
62c7ec34 6290 { .name = pmevcntr_name, .cp = 15, .crn = 14,
5ecdd3e4
AL
6291 .crm = 8 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7,
6292 .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS,
6293 .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn,
6294 .accessfn = pmreg_access },
6295 { .name = pmevcntr_el0_name, .state = ARM_CP_STATE_AA64,
62c7ec34 6296 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 8 | (3 & (i >> 3)),
5ecdd3e4
AL
6297 .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access,
6298 .type = ARM_CP_IO,
6299 .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn,
6300 .raw_readfn = pmevcntr_rawread,
6301 .raw_writefn = pmevcntr_rawwrite },
62c7ec34 6302 { .name = pmevtyper_name, .cp = 15, .crn = 14,
5ecdd3e4
AL
6303 .crm = 12 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7,
6304 .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS,
6305 .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn,
6306 .accessfn = pmreg_access },
6307 { .name = pmevtyper_el0_name, .state = ARM_CP_STATE_AA64,
62c7ec34 6308 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 12 | (3 & (i >> 3)),
5ecdd3e4
AL
6309 .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access,
6310 .type = ARM_CP_IO,
6311 .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn,
6312 .raw_writefn = pmevtyper_rawwrite },
6313 REGINFO_SENTINEL
6314 };
6315 define_arm_cp_regs(cpu, pmev_regs);
6316 g_free(pmevcntr_name);
6317 g_free(pmevcntr_el0_name);
6318 g_free(pmevtyper_name);
6319 g_free(pmevtyper_el0_name);
6320 }
776d4e5c 6321 ARMCPRegInfo clidr = {
7da845b0
PM
6322 .name = "CLIDR", .state = ARM_CP_STATE_BOTH,
6323 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 1,
630fcd4d
MZ
6324 .access = PL1_R, .type = ARM_CP_CONST,
6325 .accessfn = access_aa64_tid2,
6326 .resetvalue = cpu->clidr
776d4e5c 6327 };
776d4e5c 6328 define_one_arm_cp_reg(cpu, &clidr);
e9aa6c21 6329 define_arm_cp_regs(cpu, v7_cp_reginfo);
50300698 6330 define_debug_regs(cpu);
7d57f408
PM
6331 } else {
6332 define_arm_cp_regs(cpu, not_v7_cp_reginfo);
e9aa6c21 6333 }
cad86737
AL
6334 if (FIELD_EX32(cpu->id_dfr0, ID_DFR0, PERFMON) >= 4 &&
6335 FIELD_EX32(cpu->id_dfr0, ID_DFR0, PERFMON) != 0xf) {
6336 ARMCPRegInfo v81_pmu_regs[] = {
6337 { .name = "PMCEID2", .state = ARM_CP_STATE_AA32,
6338 .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 4,
6339 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6340 .resetvalue = extract64(cpu->pmceid0, 32, 32) },
6341 { .name = "PMCEID3", .state = ARM_CP_STATE_AA32,
6342 .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 5,
6343 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6344 .resetvalue = extract64(cpu->pmceid1, 32, 32) },
6345 REGINFO_SENTINEL
6346 };
6347 define_arm_cp_regs(cpu, v81_pmu_regs);
6348 }
b0d2b7d0 6349 if (arm_feature(env, ARM_FEATURE_V8)) {
e20d84c1
PM
6350 /* AArch64 ID registers, which all have impdef reset values.
6351 * Note that within the ID register ranges the unused slots
6352 * must all RAZ, not UNDEF; future architecture versions may
6353 * define new registers here.
6354 */
e60cef86 6355 ARMCPRegInfo v8_idregs[] = {
96a8b92e
PM
6356 /* ID_AA64PFR0_EL1 is not a plain ARM_CP_CONST because we don't
6357 * know the right value for the GIC field until after we
6358 * define these regs.
6359 */
e60cef86
PM
6360 { .name = "ID_AA64PFR0_EL1", .state = ARM_CP_STATE_AA64,
6361 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 0,
96a8b92e 6362 .access = PL1_R, .type = ARM_CP_NO_RAW,
6a4ef4e5 6363 .accessfn = access_aa64_tid3,
96a8b92e
PM
6364 .readfn = id_aa64pfr0_read,
6365 .writefn = arm_cp_write_ignore },
e60cef86
PM
6366 { .name = "ID_AA64PFR1_EL1", .state = ARM_CP_STATE_AA64,
6367 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 1,
6368 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 6369 .accessfn = access_aa64_tid3,
47576b94 6370 .resetvalue = cpu->isar.id_aa64pfr1},
e20d84c1
PM
6371 { .name = "ID_AA64PFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
6372 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 2,
6373 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 6374 .accessfn = access_aa64_tid3,
e20d84c1
PM
6375 .resetvalue = 0 },
6376 { .name = "ID_AA64PFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
6377 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 3,
6378 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 6379 .accessfn = access_aa64_tid3,
e20d84c1 6380 .resetvalue = 0 },
9516d772 6381 { .name = "ID_AA64ZFR0_EL1", .state = ARM_CP_STATE_AA64,
e20d84c1
PM
6382 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 4,
6383 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 6384 .accessfn = access_aa64_tid3,
9516d772 6385 /* At present, only SVEver == 0 is defined anyway. */
e20d84c1
PM
6386 .resetvalue = 0 },
6387 { .name = "ID_AA64PFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
6388 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 5,
6389 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 6390 .accessfn = access_aa64_tid3,
e20d84c1
PM
6391 .resetvalue = 0 },
6392 { .name = "ID_AA64PFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
6393 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 6,
6394 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 6395 .accessfn = access_aa64_tid3,
e20d84c1
PM
6396 .resetvalue = 0 },
6397 { .name = "ID_AA64PFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
6398 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 7,
6399 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 6400 .accessfn = access_aa64_tid3,
e20d84c1 6401 .resetvalue = 0 },
e60cef86
PM
6402 { .name = "ID_AA64DFR0_EL1", .state = ARM_CP_STATE_AA64,
6403 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 0,
6404 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 6405 .accessfn = access_aa64_tid3,
d6f02ce3 6406 .resetvalue = cpu->id_aa64dfr0 },
e60cef86
PM
6407 { .name = "ID_AA64DFR1_EL1", .state = ARM_CP_STATE_AA64,
6408 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 1,
6409 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 6410 .accessfn = access_aa64_tid3,
e60cef86 6411 .resetvalue = cpu->id_aa64dfr1 },
e20d84c1
PM
6412 { .name = "ID_AA64DFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
6413 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 2,
6414 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 6415 .accessfn = access_aa64_tid3,
e20d84c1
PM
6416 .resetvalue = 0 },
6417 { .name = "ID_AA64DFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
6418 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 3,
6419 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 6420 .accessfn = access_aa64_tid3,
e20d84c1 6421 .resetvalue = 0 },
e60cef86
PM
6422 { .name = "ID_AA64AFR0_EL1", .state = ARM_CP_STATE_AA64,
6423 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 4,
6424 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 6425 .accessfn = access_aa64_tid3,
e60cef86
PM
6426 .resetvalue = cpu->id_aa64afr0 },
6427 { .name = "ID_AA64AFR1_EL1", .state = ARM_CP_STATE_AA64,
6428 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 5,
6429 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 6430 .accessfn = access_aa64_tid3,
e60cef86 6431 .resetvalue = cpu->id_aa64afr1 },
e20d84c1
PM
6432 { .name = "ID_AA64AFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
6433 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 6,
6434 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 6435 .accessfn = access_aa64_tid3,
e20d84c1
PM
6436 .resetvalue = 0 },
6437 { .name = "ID_AA64AFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
6438 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 7,
6439 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 6440 .accessfn = access_aa64_tid3,
e20d84c1 6441 .resetvalue = 0 },
e60cef86
PM
6442 { .name = "ID_AA64ISAR0_EL1", .state = ARM_CP_STATE_AA64,
6443 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 0,
6444 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 6445 .accessfn = access_aa64_tid3,
47576b94 6446 .resetvalue = cpu->isar.id_aa64isar0 },
e60cef86
PM
6447 { .name = "ID_AA64ISAR1_EL1", .state = ARM_CP_STATE_AA64,
6448 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 1,
6449 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 6450 .accessfn = access_aa64_tid3,
47576b94 6451 .resetvalue = cpu->isar.id_aa64isar1 },
e20d84c1
PM
6452 { .name = "ID_AA64ISAR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
6453 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 2,
6454 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 6455 .accessfn = access_aa64_tid3,
e20d84c1
PM
6456 .resetvalue = 0 },
6457 { .name = "ID_AA64ISAR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
6458 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 3,
6459 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 6460 .accessfn = access_aa64_tid3,
e20d84c1
PM
6461 .resetvalue = 0 },
6462 { .name = "ID_AA64ISAR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
6463 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 4,
6464 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 6465 .accessfn = access_aa64_tid3,
e20d84c1
PM
6466 .resetvalue = 0 },
6467 { .name = "ID_AA64ISAR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
6468 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 5,
6469 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 6470 .accessfn = access_aa64_tid3,
e20d84c1
PM
6471 .resetvalue = 0 },
6472 { .name = "ID_AA64ISAR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
6473 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 6,
6474 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 6475 .accessfn = access_aa64_tid3,
e20d84c1
PM
6476 .resetvalue = 0 },
6477 { .name = "ID_AA64ISAR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
6478 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 7,
6479 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 6480 .accessfn = access_aa64_tid3,
e20d84c1 6481 .resetvalue = 0 },
e60cef86
PM
6482 { .name = "ID_AA64MMFR0_EL1", .state = ARM_CP_STATE_AA64,
6483 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
6484 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 6485 .accessfn = access_aa64_tid3,
3dc91ddb 6486 .resetvalue = cpu->isar.id_aa64mmfr0 },
e60cef86
PM
6487 { .name = "ID_AA64MMFR1_EL1", .state = ARM_CP_STATE_AA64,
6488 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 1,
6489 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 6490 .accessfn = access_aa64_tid3,
3dc91ddb 6491 .resetvalue = cpu->isar.id_aa64mmfr1 },
e20d84c1
PM
6492 { .name = "ID_AA64MMFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
6493 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 2,
6494 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 6495 .accessfn = access_aa64_tid3,
e20d84c1
PM
6496 .resetvalue = 0 },
6497 { .name = "ID_AA64MMFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
6498 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 3,
6499 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 6500 .accessfn = access_aa64_tid3,
e20d84c1
PM
6501 .resetvalue = 0 },
6502 { .name = "ID_AA64MMFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
6503 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 4,
6504 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 6505 .accessfn = access_aa64_tid3,
e20d84c1
PM
6506 .resetvalue = 0 },
6507 { .name = "ID_AA64MMFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
6508 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 5,
6509 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 6510 .accessfn = access_aa64_tid3,
e20d84c1
PM
6511 .resetvalue = 0 },
6512 { .name = "ID_AA64MMFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
6513 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 6,
6514 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 6515 .accessfn = access_aa64_tid3,
e20d84c1
PM
6516 .resetvalue = 0 },
6517 { .name = "ID_AA64MMFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
6518 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 7,
6519 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 6520 .accessfn = access_aa64_tid3,
e20d84c1 6521 .resetvalue = 0 },
a50c0f51
PM
6522 { .name = "MVFR0_EL1", .state = ARM_CP_STATE_AA64,
6523 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0,
6524 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 6525 .accessfn = access_aa64_tid3,
47576b94 6526 .resetvalue = cpu->isar.mvfr0 },
a50c0f51
PM
6527 { .name = "MVFR1_EL1", .state = ARM_CP_STATE_AA64,
6528 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1,
6529 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 6530 .accessfn = access_aa64_tid3,
47576b94 6531 .resetvalue = cpu->isar.mvfr1 },
a50c0f51
PM
6532 { .name = "MVFR2_EL1", .state = ARM_CP_STATE_AA64,
6533 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2,
6534 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 6535 .accessfn = access_aa64_tid3,
47576b94 6536 .resetvalue = cpu->isar.mvfr2 },
e20d84c1
PM
6537 { .name = "MVFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
6538 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 3,
6539 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 6540 .accessfn = access_aa64_tid3,
e20d84c1
PM
6541 .resetvalue = 0 },
6542 { .name = "MVFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
6543 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 4,
6544 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 6545 .accessfn = access_aa64_tid3,
e20d84c1
PM
6546 .resetvalue = 0 },
6547 { .name = "MVFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
6548 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 5,
6549 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 6550 .accessfn = access_aa64_tid3,
e20d84c1
PM
6551 .resetvalue = 0 },
6552 { .name = "MVFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
6553 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 6,
6554 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 6555 .accessfn = access_aa64_tid3,
e20d84c1
PM
6556 .resetvalue = 0 },
6557 { .name = "MVFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
6558 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 7,
6559 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 6560 .accessfn = access_aa64_tid3,
e20d84c1 6561 .resetvalue = 0 },
4054bfa9
AF
6562 { .name = "PMCEID0", .state = ARM_CP_STATE_AA32,
6563 .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 6,
6564 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
cad86737 6565 .resetvalue = extract64(cpu->pmceid0, 0, 32) },
4054bfa9
AF
6566 { .name = "PMCEID0_EL0", .state = ARM_CP_STATE_AA64,
6567 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 6,
6568 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6569 .resetvalue = cpu->pmceid0 },
6570 { .name = "PMCEID1", .state = ARM_CP_STATE_AA32,
6571 .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 7,
6572 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
cad86737 6573 .resetvalue = extract64(cpu->pmceid1, 0, 32) },
4054bfa9
AF
6574 { .name = "PMCEID1_EL0", .state = ARM_CP_STATE_AA64,
6575 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 7,
6576 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6577 .resetvalue = cpu->pmceid1 },
e60cef86
PM
6578 REGINFO_SENTINEL
6579 };
6c5c0fec
AB
6580#ifdef CONFIG_USER_ONLY
6581 ARMCPRegUserSpaceInfo v8_user_idregs[] = {
6582 { .name = "ID_AA64PFR0_EL1",
6583 .exported_bits = 0x000f000f00ff0000,
6584 .fixed_bits = 0x0000000000000011 },
6585 { .name = "ID_AA64PFR1_EL1",
6586 .exported_bits = 0x00000000000000f0 },
d040242e
AB
6587 { .name = "ID_AA64PFR*_EL1_RESERVED",
6588 .is_glob = true },
6c5c0fec
AB
6589 { .name = "ID_AA64ZFR0_EL1" },
6590 { .name = "ID_AA64MMFR0_EL1",
6591 .fixed_bits = 0x00000000ff000000 },
6592 { .name = "ID_AA64MMFR1_EL1" },
d040242e
AB
6593 { .name = "ID_AA64MMFR*_EL1_RESERVED",
6594 .is_glob = true },
6c5c0fec
AB
6595 { .name = "ID_AA64DFR0_EL1",
6596 .fixed_bits = 0x0000000000000006 },
6597 { .name = "ID_AA64DFR1_EL1" },
d040242e
AB
6598 { .name = "ID_AA64DFR*_EL1_RESERVED",
6599 .is_glob = true },
6600 { .name = "ID_AA64AFR*",
6601 .is_glob = true },
6c5c0fec
AB
6602 { .name = "ID_AA64ISAR0_EL1",
6603 .exported_bits = 0x00fffffff0fffff0 },
6604 { .name = "ID_AA64ISAR1_EL1",
6605 .exported_bits = 0x000000f0ffffffff },
d040242e
AB
6606 { .name = "ID_AA64ISAR*_EL1_RESERVED",
6607 .is_glob = true },
6c5c0fec
AB
6608 REGUSERINFO_SENTINEL
6609 };
6610 modify_arm_cp_regs(v8_idregs, v8_user_idregs);
6611#endif
be8e8128
GB
6612 /* RVBAR_EL1 is only implemented if EL1 is the highest EL */
6613 if (!arm_feature(env, ARM_FEATURE_EL3) &&
6614 !arm_feature(env, ARM_FEATURE_EL2)) {
6615 ARMCPRegInfo rvbar = {
6616 .name = "RVBAR_EL1", .state = ARM_CP_STATE_AA64,
6617 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
6618 .type = ARM_CP_CONST, .access = PL1_R, .resetvalue = cpu->rvbar
6619 };
6620 define_one_arm_cp_reg(cpu, &rvbar);
6621 }
e60cef86 6622 define_arm_cp_regs(cpu, v8_idregs);
b0d2b7d0
PM
6623 define_arm_cp_regs(cpu, v8_cp_reginfo);
6624 }
3b685ba7 6625 if (arm_feature(env, ARM_FEATURE_EL2)) {
f0d574d6 6626 uint64_t vmpidr_def = mpidr_read_val(env);
731de9e6
EI
6627 ARMCPRegInfo vpidr_regs[] = {
6628 { .name = "VPIDR", .state = ARM_CP_STATE_AA32,
6629 .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
6630 .access = PL2_RW, .accessfn = access_el3_aa32ns,
36476562
PM
6631 .resetvalue = cpu->midr, .type = ARM_CP_ALIAS,
6632 .fieldoffset = offsetoflow32(CPUARMState, cp15.vpidr_el2) },
731de9e6
EI
6633 { .name = "VPIDR_EL2", .state = ARM_CP_STATE_AA64,
6634 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
6635 .access = PL2_RW, .resetvalue = cpu->midr,
6636 .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) },
f0d574d6
EI
6637 { .name = "VMPIDR", .state = ARM_CP_STATE_AA32,
6638 .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
6639 .access = PL2_RW, .accessfn = access_el3_aa32ns,
36476562
PM
6640 .resetvalue = vmpidr_def, .type = ARM_CP_ALIAS,
6641 .fieldoffset = offsetoflow32(CPUARMState, cp15.vmpidr_el2) },
f0d574d6
EI
6642 { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_AA64,
6643 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
6644 .access = PL2_RW,
6645 .resetvalue = vmpidr_def,
6646 .fieldoffset = offsetof(CPUARMState, cp15.vmpidr_el2) },
731de9e6
EI
6647 REGINFO_SENTINEL
6648 };
6649 define_arm_cp_regs(cpu, vpidr_regs);
4771cd01 6650 define_arm_cp_regs(cpu, el2_cp_reginfo);
ce4afed8
PM
6651 if (arm_feature(env, ARM_FEATURE_V8)) {
6652 define_arm_cp_regs(cpu, el2_v8_cp_reginfo);
6653 }
be8e8128
GB
6654 /* RVBAR_EL2 is only implemented if EL2 is the highest EL */
6655 if (!arm_feature(env, ARM_FEATURE_EL3)) {
6656 ARMCPRegInfo rvbar = {
6657 .name = "RVBAR_EL2", .state = ARM_CP_STATE_AA64,
6658 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 1,
6659 .type = ARM_CP_CONST, .access = PL2_R, .resetvalue = cpu->rvbar
6660 };
6661 define_one_arm_cp_reg(cpu, &rvbar);
6662 }
d42e3c26
EI
6663 } else {
6664 /* If EL2 is missing but higher ELs are enabled, we need to
6665 * register the no_el2 reginfos.
6666 */
6667 if (arm_feature(env, ARM_FEATURE_EL3)) {
f0d574d6
EI
6668 /* When EL3 exists but not EL2, VPIDR and VMPIDR take the value
6669 * of MIDR_EL1 and MPIDR_EL1.
731de9e6
EI
6670 */
6671 ARMCPRegInfo vpidr_regs[] = {
6672 { .name = "VPIDR_EL2", .state = ARM_CP_STATE_BOTH,
6673 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
6674 .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any,
6675 .type = ARM_CP_CONST, .resetvalue = cpu->midr,
6676 .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) },
f0d574d6
EI
6677 { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_BOTH,
6678 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
6679 .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any,
6680 .type = ARM_CP_NO_RAW,
6681 .writefn = arm_cp_write_ignore, .readfn = mpidr_read },
731de9e6
EI
6682 REGINFO_SENTINEL
6683 };
6684 define_arm_cp_regs(cpu, vpidr_regs);
4771cd01 6685 define_arm_cp_regs(cpu, el3_no_el2_cp_reginfo);
ce4afed8
PM
6686 if (arm_feature(env, ARM_FEATURE_V8)) {
6687 define_arm_cp_regs(cpu, el3_no_el2_v8_cp_reginfo);
6688 }
d42e3c26 6689 }
3b685ba7 6690 }
81547d66 6691 if (arm_feature(env, ARM_FEATURE_EL3)) {
0f1a3b24 6692 define_arm_cp_regs(cpu, el3_cp_reginfo);
e24fdd23
PM
6693 ARMCPRegInfo el3_regs[] = {
6694 { .name = "RVBAR_EL3", .state = ARM_CP_STATE_AA64,
6695 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 1,
6696 .type = ARM_CP_CONST, .access = PL3_R, .resetvalue = cpu->rvbar },
6697 { .name = "SCTLR_EL3", .state = ARM_CP_STATE_AA64,
6698 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 0,
6699 .access = PL3_RW,
6700 .raw_writefn = raw_write, .writefn = sctlr_write,
6701 .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[3]),
6702 .resetvalue = cpu->reset_sctlr },
6703 REGINFO_SENTINEL
be8e8128 6704 };
e24fdd23
PM
6705
6706 define_arm_cp_regs(cpu, el3_regs);
81547d66 6707 }
2f027fc5
PM
6708 /* The behaviour of NSACR is sufficiently various that we don't
6709 * try to describe it in a single reginfo:
6710 * if EL3 is 64 bit, then trap to EL3 from S EL1,
6711 * reads as constant 0xc00 from NS EL1 and NS EL2
6712 * if EL3 is 32 bit, then RW at EL3, RO at NS EL1 and NS EL2
6713 * if v7 without EL3, register doesn't exist
6714 * if v8 without EL3, reads as constant 0xc00 from NS EL1 and NS EL2
6715 */
6716 if (arm_feature(env, ARM_FEATURE_EL3)) {
6717 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
6718 ARMCPRegInfo nsacr = {
6719 .name = "NSACR", .type = ARM_CP_CONST,
6720 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
6721 .access = PL1_RW, .accessfn = nsacr_access,
6722 .resetvalue = 0xc00
6723 };
6724 define_one_arm_cp_reg(cpu, &nsacr);
6725 } else {
6726 ARMCPRegInfo nsacr = {
6727 .name = "NSACR",
6728 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
6729 .access = PL3_RW | PL1_R,
6730 .resetvalue = 0,
6731 .fieldoffset = offsetof(CPUARMState, cp15.nsacr)
6732 };
6733 define_one_arm_cp_reg(cpu, &nsacr);
6734 }
6735 } else {
6736 if (arm_feature(env, ARM_FEATURE_V8)) {
6737 ARMCPRegInfo nsacr = {
6738 .name = "NSACR", .type = ARM_CP_CONST,
6739 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
6740 .access = PL1_R,
6741 .resetvalue = 0xc00
6742 };
6743 define_one_arm_cp_reg(cpu, &nsacr);
6744 }
6745 }
6746
452a0955 6747 if (arm_feature(env, ARM_FEATURE_PMSA)) {
6cb0b013
PC
6748 if (arm_feature(env, ARM_FEATURE_V6)) {
6749 /* PMSAv6 not implemented */
6750 assert(arm_feature(env, ARM_FEATURE_V7));
6751 define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
6752 define_arm_cp_regs(cpu, pmsav7_cp_reginfo);
6753 } else {
6754 define_arm_cp_regs(cpu, pmsav5_cp_reginfo);
6755 }
18032bec 6756 } else {
8e5d75c9 6757 define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
18032bec 6758 define_arm_cp_regs(cpu, vmsa_cp_reginfo);
ab638a32
RH
6759 /* TTCBR2 is introduced with ARMv8.2-A32HPD. */
6760 if (FIELD_EX32(cpu->id_mmfr4, ID_MMFR4, HPDS) != 0) {
6761 define_one_arm_cp_reg(cpu, &ttbcr2_reginfo);
6762 }
18032bec 6763 }
c326b979
PM
6764 if (arm_feature(env, ARM_FEATURE_THUMB2EE)) {
6765 define_arm_cp_regs(cpu, t2ee_cp_reginfo);
6766 }
6cc7a3ae
PM
6767 if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) {
6768 define_arm_cp_regs(cpu, generic_timer_cp_reginfo);
6769 }
4a501606
PM
6770 if (arm_feature(env, ARM_FEATURE_VAPA)) {
6771 define_arm_cp_regs(cpu, vapa_cp_reginfo);
6772 }
c4804214
PM
6773 if (arm_feature(env, ARM_FEATURE_CACHE_TEST_CLEAN)) {
6774 define_arm_cp_regs(cpu, cache_test_clean_cp_reginfo);
6775 }
6776 if (arm_feature(env, ARM_FEATURE_CACHE_DIRTY_REG)) {
6777 define_arm_cp_regs(cpu, cache_dirty_status_cp_reginfo);
6778 }
6779 if (arm_feature(env, ARM_FEATURE_CACHE_BLOCK_OPS)) {
6780 define_arm_cp_regs(cpu, cache_block_ops_cp_reginfo);
6781 }
18032bec
PM
6782 if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
6783 define_arm_cp_regs(cpu, omap_cp_reginfo);
6784 }
34f90529
PM
6785 if (arm_feature(env, ARM_FEATURE_STRONGARM)) {
6786 define_arm_cp_regs(cpu, strongarm_cp_reginfo);
6787 }
1047b9d7
PM
6788 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
6789 define_arm_cp_regs(cpu, xscale_cp_reginfo);
6790 }
6791 if (arm_feature(env, ARM_FEATURE_DUMMY_C15_REGS)) {
6792 define_arm_cp_regs(cpu, dummy_c15_cp_reginfo);
6793 }
7ac681cf
PM
6794 if (arm_feature(env, ARM_FEATURE_LPAE)) {
6795 define_arm_cp_regs(cpu, lpae_cp_reginfo);
6796 }
f96f3d5f
MZ
6797 if (cpu_isar_feature(jazelle, cpu)) {
6798 define_arm_cp_regs(cpu, jazelle_regs);
6799 }
7884849c
PM
6800 /* Slightly awkwardly, the OMAP and StrongARM cores need all of
6801 * cp15 crn=0 to be writes-ignored, whereas for other cores they should
6802 * be read-only (ie write causes UNDEF exception).
6803 */
6804 {
00a29f3d
PM
6805 ARMCPRegInfo id_pre_v8_midr_cp_reginfo[] = {
6806 /* Pre-v8 MIDR space.
6807 * Note that the MIDR isn't a simple constant register because
7884849c
PM
6808 * of the TI925 behaviour where writes to another register can
6809 * cause the MIDR value to change.
97ce8d61
PC
6810 *
6811 * Unimplemented registers in the c15 0 0 0 space default to
6812 * MIDR. Define MIDR first as this entire space, then CTR, TCMTR
6813 * and friends override accordingly.
7884849c
PM
6814 */
6815 { .name = "MIDR",
97ce8d61 6816 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = CP_ANY,
7884849c 6817 .access = PL1_R, .resetvalue = cpu->midr,
d4e6df63 6818 .writefn = arm_cp_write_ignore, .raw_writefn = raw_write,
731de9e6 6819 .readfn = midr_read,
97ce8d61
PC
6820 .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
6821 .type = ARM_CP_OVERRIDE },
7884849c
PM
6822 /* crn = 0 op1 = 0 crm = 3..7 : currently unassigned; we RAZ. */
6823 { .name = "DUMMY",
6824 .cp = 15, .crn = 0, .crm = 3, .opc1 = 0, .opc2 = CP_ANY,
6825 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
6826 { .name = "DUMMY",
6827 .cp = 15, .crn = 0, .crm = 4, .opc1 = 0, .opc2 = CP_ANY,
6828 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
6829 { .name = "DUMMY",
6830 .cp = 15, .crn = 0, .crm = 5, .opc1 = 0, .opc2 = CP_ANY,
6831 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
6832 { .name = "DUMMY",
6833 .cp = 15, .crn = 0, .crm = 6, .opc1 = 0, .opc2 = CP_ANY,
6834 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
6835 { .name = "DUMMY",
6836 .cp = 15, .crn = 0, .crm = 7, .opc1 = 0, .opc2 = CP_ANY,
6837 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
6838 REGINFO_SENTINEL
6839 };
00a29f3d 6840 ARMCPRegInfo id_v8_midr_cp_reginfo[] = {
00a29f3d
PM
6841 { .name = "MIDR_EL1", .state = ARM_CP_STATE_BOTH,
6842 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 0,
731de9e6
EI
6843 .access = PL1_R, .type = ARM_CP_NO_RAW, .resetvalue = cpu->midr,
6844 .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
6845 .readfn = midr_read },
ac00c79f
SF
6846 /* crn = 0 op1 = 0 crm = 0 op2 = 4,7 : AArch32 aliases of MIDR */
6847 { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
6848 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
6849 .access = PL1_R, .resetvalue = cpu->midr },
6850 { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
6851 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 7,
6852 .access = PL1_R, .resetvalue = cpu->midr },
00a29f3d
PM
6853 { .name = "REVIDR_EL1", .state = ARM_CP_STATE_BOTH,
6854 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 6,
93fbc983
MZ
6855 .access = PL1_R,
6856 .accessfn = access_aa64_tid1,
6857 .type = ARM_CP_CONST, .resetvalue = cpu->revidr },
00a29f3d
PM
6858 REGINFO_SENTINEL
6859 };
6860 ARMCPRegInfo id_cp_reginfo[] = {
6861 /* These are common to v8 and pre-v8 */
6862 { .name = "CTR",
6863 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 1,
630fcd4d
MZ
6864 .access = PL1_R, .accessfn = ctr_el0_access,
6865 .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
00a29f3d
PM
6866 { .name = "CTR_EL0", .state = ARM_CP_STATE_AA64,
6867 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 0, .crm = 0,
6868 .access = PL0_R, .accessfn = ctr_el0_access,
6869 .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
6870 /* TCMTR and TLBTR exist in v8 but have no 64-bit versions */
6871 { .name = "TCMTR",
6872 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 2,
93fbc983
MZ
6873 .access = PL1_R,
6874 .accessfn = access_aa32_tid1,
6875 .type = ARM_CP_CONST, .resetvalue = 0 },
00a29f3d
PM
6876 REGINFO_SENTINEL
6877 };
8085ce63
PC
6878 /* TLBTR is specific to VMSA */
6879 ARMCPRegInfo id_tlbtr_reginfo = {
6880 .name = "TLBTR",
6881 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 3,
93fbc983
MZ
6882 .access = PL1_R,
6883 .accessfn = access_aa32_tid1,
6884 .type = ARM_CP_CONST, .resetvalue = 0,
8085ce63 6885 };
3281af81
PC
6886 /* MPUIR is specific to PMSA V6+ */
6887 ARMCPRegInfo id_mpuir_reginfo = {
6888 .name = "MPUIR",
6889 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
6890 .access = PL1_R, .type = ARM_CP_CONST,
6891 .resetvalue = cpu->pmsav7_dregion << 8
6892 };
7884849c
PM
6893 ARMCPRegInfo crn0_wi_reginfo = {
6894 .name = "CRN0_WI", .cp = 15, .crn = 0, .crm = CP_ANY,
6895 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_W,
6896 .type = ARM_CP_NOP | ARM_CP_OVERRIDE
6897 };
6c5c0fec
AB
6898#ifdef CONFIG_USER_ONLY
6899 ARMCPRegUserSpaceInfo id_v8_user_midr_cp_reginfo[] = {
6900 { .name = "MIDR_EL1",
6901 .exported_bits = 0x00000000ffffffff },
6902 { .name = "REVIDR_EL1" },
6903 REGUSERINFO_SENTINEL
6904 };
6905 modify_arm_cp_regs(id_v8_midr_cp_reginfo, id_v8_user_midr_cp_reginfo);
6906#endif
7884849c
PM
6907 if (arm_feature(env, ARM_FEATURE_OMAPCP) ||
6908 arm_feature(env, ARM_FEATURE_STRONGARM)) {
6909 ARMCPRegInfo *r;
6910 /* Register the blanket "writes ignored" value first to cover the
a703eda1
PC
6911 * whole space. Then update the specific ID registers to allow write
6912 * access, so that they ignore writes rather than causing them to
6913 * UNDEF.
7884849c
PM
6914 */
6915 define_one_arm_cp_reg(cpu, &crn0_wi_reginfo);
00a29f3d
PM
6916 for (r = id_pre_v8_midr_cp_reginfo;
6917 r->type != ARM_CP_SENTINEL; r++) {
6918 r->access = PL1_RW;
6919 }
7884849c
PM
6920 for (r = id_cp_reginfo; r->type != ARM_CP_SENTINEL; r++) {
6921 r->access = PL1_RW;
7884849c 6922 }
10006112 6923 id_mpuir_reginfo.access = PL1_RW;
3281af81 6924 id_tlbtr_reginfo.access = PL1_RW;
7884849c 6925 }
00a29f3d
PM
6926 if (arm_feature(env, ARM_FEATURE_V8)) {
6927 define_arm_cp_regs(cpu, id_v8_midr_cp_reginfo);
6928 } else {
6929 define_arm_cp_regs(cpu, id_pre_v8_midr_cp_reginfo);
6930 }
a703eda1 6931 define_arm_cp_regs(cpu, id_cp_reginfo);
452a0955 6932 if (!arm_feature(env, ARM_FEATURE_PMSA)) {
8085ce63 6933 define_one_arm_cp_reg(cpu, &id_tlbtr_reginfo);
3281af81
PC
6934 } else if (arm_feature(env, ARM_FEATURE_V7)) {
6935 define_one_arm_cp_reg(cpu, &id_mpuir_reginfo);
8085ce63 6936 }
7884849c
PM
6937 }
6938
97ce8d61 6939 if (arm_feature(env, ARM_FEATURE_MPIDR)) {
52264166
AB
6940 ARMCPRegInfo mpidr_cp_reginfo[] = {
6941 { .name = "MPIDR_EL1", .state = ARM_CP_STATE_BOTH,
6942 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 5,
6943 .access = PL1_R, .readfn = mpidr_read, .type = ARM_CP_NO_RAW },
6944 REGINFO_SENTINEL
6945 };
6946#ifdef CONFIG_USER_ONLY
6947 ARMCPRegUserSpaceInfo mpidr_user_cp_reginfo[] = {
6948 { .name = "MPIDR_EL1",
6949 .fixed_bits = 0x0000000080000000 },
6950 REGUSERINFO_SENTINEL
6951 };
6952 modify_arm_cp_regs(mpidr_cp_reginfo, mpidr_user_cp_reginfo);
6953#endif
97ce8d61
PC
6954 define_arm_cp_regs(cpu, mpidr_cp_reginfo);
6955 }
6956
2771db27 6957 if (arm_feature(env, ARM_FEATURE_AUXCR)) {
834a6c69
PM
6958 ARMCPRegInfo auxcr_reginfo[] = {
6959 { .name = "ACTLR_EL1", .state = ARM_CP_STATE_BOTH,
6960 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 1,
6961 .access = PL1_RW, .type = ARM_CP_CONST,
6962 .resetvalue = cpu->reset_auxcr },
6963 { .name = "ACTLR_EL2", .state = ARM_CP_STATE_BOTH,
6964 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 1,
6965 .access = PL2_RW, .type = ARM_CP_CONST,
6966 .resetvalue = 0 },
6967 { .name = "ACTLR_EL3", .state = ARM_CP_STATE_AA64,
6968 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 1,
6969 .access = PL3_RW, .type = ARM_CP_CONST,
6970 .resetvalue = 0 },
6971 REGINFO_SENTINEL
2771db27 6972 };
834a6c69 6973 define_arm_cp_regs(cpu, auxcr_reginfo);
0e0456ab
PM
6974 if (arm_feature(env, ARM_FEATURE_V8)) {
6975 /* HACTLR2 maps to ACTLR_EL2[63:32] and is not in ARMv7 */
6976 ARMCPRegInfo hactlr2_reginfo = {
6977 .name = "HACTLR2", .state = ARM_CP_STATE_AA32,
6978 .cp = 15, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 3,
6979 .access = PL2_RW, .type = ARM_CP_CONST,
6980 .resetvalue = 0
6981 };
6982 define_one_arm_cp_reg(cpu, &hactlr2_reginfo);
6983 }
2771db27
PM
6984 }
6985
d8ba780b 6986 if (arm_feature(env, ARM_FEATURE_CBAR)) {
d56974af
LM
6987 /*
6988 * CBAR is IMPDEF, but common on Arm Cortex-A implementations.
6989 * There are two flavours:
6990 * (1) older 32-bit only cores have a simple 32-bit CBAR
6991 * (2) 64-bit cores have a 64-bit CBAR visible to AArch64, plus a
6992 * 32-bit register visible to AArch32 at a different encoding
6993 * to the "flavour 1" register and with the bits rearranged to
6994 * be able to squash a 64-bit address into the 32-bit view.
6995 * We distinguish the two via the ARM_FEATURE_AARCH64 flag, but
6996 * in future if we support AArch32-only configs of some of the
6997 * AArch64 cores we might need to add a specific feature flag
6998 * to indicate cores with "flavour 2" CBAR.
6999 */
f318cec6
PM
7000 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
7001 /* 32 bit view is [31:18] 0...0 [43:32]. */
7002 uint32_t cbar32 = (extract64(cpu->reset_cbar, 18, 14) << 18)
7003 | extract64(cpu->reset_cbar, 32, 12);
7004 ARMCPRegInfo cbar_reginfo[] = {
7005 { .name = "CBAR",
7006 .type = ARM_CP_CONST,
d56974af
LM
7007 .cp = 15, .crn = 15, .crm = 3, .opc1 = 1, .opc2 = 0,
7008 .access = PL1_R, .resetvalue = cbar32 },
f318cec6
PM
7009 { .name = "CBAR_EL1", .state = ARM_CP_STATE_AA64,
7010 .type = ARM_CP_CONST,
7011 .opc0 = 3, .opc1 = 1, .crn = 15, .crm = 3, .opc2 = 0,
d56974af 7012 .access = PL1_R, .resetvalue = cpu->reset_cbar },
f318cec6
PM
7013 REGINFO_SENTINEL
7014 };
7015 /* We don't implement a r/w 64 bit CBAR currently */
7016 assert(arm_feature(env, ARM_FEATURE_CBAR_RO));
7017 define_arm_cp_regs(cpu, cbar_reginfo);
7018 } else {
7019 ARMCPRegInfo cbar = {
7020 .name = "CBAR",
7021 .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0,
7022 .access = PL1_R|PL3_W, .resetvalue = cpu->reset_cbar,
7023 .fieldoffset = offsetof(CPUARMState,
7024 cp15.c15_config_base_address)
7025 };
7026 if (arm_feature(env, ARM_FEATURE_CBAR_RO)) {
7027 cbar.access = PL1_R;
7028 cbar.fieldoffset = 0;
7029 cbar.type = ARM_CP_CONST;
7030 }
7031 define_one_arm_cp_reg(cpu, &cbar);
7032 }
d8ba780b
PC
7033 }
7034
91db4642
CLG
7035 if (arm_feature(env, ARM_FEATURE_VBAR)) {
7036 ARMCPRegInfo vbar_cp_reginfo[] = {
7037 { .name = "VBAR", .state = ARM_CP_STATE_BOTH,
7038 .opc0 = 3, .crn = 12, .crm = 0, .opc1 = 0, .opc2 = 0,
7039 .access = PL1_RW, .writefn = vbar_write,
7040 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.vbar_s),
7041 offsetof(CPUARMState, cp15.vbar_ns) },
7042 .resetvalue = 0 },
7043 REGINFO_SENTINEL
7044 };
7045 define_arm_cp_regs(cpu, vbar_cp_reginfo);
7046 }
7047
2771db27
PM
7048 /* Generic registers whose values depend on the implementation */
7049 {
7050 ARMCPRegInfo sctlr = {
5ebafdf3 7051 .name = "SCTLR", .state = ARM_CP_STATE_BOTH,
137feaa9
FA
7052 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
7053 .access = PL1_RW,
7054 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.sctlr_s),
7055 offsetof(CPUARMState, cp15.sctlr_ns) },
d4e6df63
PM
7056 .writefn = sctlr_write, .resetvalue = cpu->reset_sctlr,
7057 .raw_writefn = raw_write,
2771db27
PM
7058 };
7059 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
7060 /* Normally we would always end the TB on an SCTLR write, but Linux
7061 * arch/arm/mach-pxa/sleep.S expects two instructions following
7062 * an MMU enable to execute from cache. Imitate this behaviour.
7063 */
7064 sctlr.type |= ARM_CP_SUPPRESS_TB_END;
7065 }
7066 define_one_arm_cp_reg(cpu, &sctlr);
7067 }
5be5e8ed 7068
2d7137c1
RH
7069 if (cpu_isar_feature(aa64_lor, cpu)) {
7070 /*
7071 * A trivial implementation of ARMv8.1-LOR leaves all of these
7072 * registers fixed at 0, which indicates that there are zero
7073 * supported Limited Ordering regions.
7074 */
7075 static const ARMCPRegInfo lor_reginfo[] = {
7076 { .name = "LORSA_EL1", .state = ARM_CP_STATE_AA64,
7077 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 0,
7078 .access = PL1_RW, .accessfn = access_lor_other,
7079 .type = ARM_CP_CONST, .resetvalue = 0 },
7080 { .name = "LOREA_EL1", .state = ARM_CP_STATE_AA64,
7081 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 1,
7082 .access = PL1_RW, .accessfn = access_lor_other,
7083 .type = ARM_CP_CONST, .resetvalue = 0 },
7084 { .name = "LORN_EL1", .state = ARM_CP_STATE_AA64,
7085 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 2,
7086 .access = PL1_RW, .accessfn = access_lor_other,
7087 .type = ARM_CP_CONST, .resetvalue = 0 },
7088 { .name = "LORC_EL1", .state = ARM_CP_STATE_AA64,
7089 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 3,
7090 .access = PL1_RW, .accessfn = access_lor_other,
7091 .type = ARM_CP_CONST, .resetvalue = 0 },
7092 { .name = "LORID_EL1", .state = ARM_CP_STATE_AA64,
7093 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 7,
7094 .access = PL1_R, .accessfn = access_lorid,
7095 .type = ARM_CP_CONST, .resetvalue = 0 },
7096 REGINFO_SENTINEL
7097 };
7098 define_arm_cp_regs(cpu, lor_reginfo);
7099 }
7100
e2a1a461
RH
7101 if (arm_feature(env, ARM_FEATURE_EL2) && cpu_isar_feature(aa64_vh, cpu)) {
7102 define_arm_cp_regs(cpu, vhe_reginfo);
7103 }
7104
cd208a1c 7105 if (cpu_isar_feature(aa64_sve, cpu)) {
5be5e8ed
RH
7106 define_one_arm_cp_reg(cpu, &zcr_el1_reginfo);
7107 if (arm_feature(env, ARM_FEATURE_EL2)) {
7108 define_one_arm_cp_reg(cpu, &zcr_el2_reginfo);
7109 } else {
7110 define_one_arm_cp_reg(cpu, &zcr_no_el2_reginfo);
7111 }
7112 if (arm_feature(env, ARM_FEATURE_EL3)) {
7113 define_one_arm_cp_reg(cpu, &zcr_el3_reginfo);
7114 }
7115 }
967aa94f
RH
7116
7117#ifdef TARGET_AARCH64
7118 if (cpu_isar_feature(aa64_pauth, cpu)) {
7119 define_arm_cp_regs(cpu, pauth_reginfo);
7120 }
de390645
RH
7121 if (cpu_isar_feature(aa64_rndr, cpu)) {
7122 define_arm_cp_regs(cpu, rndr_reginfo);
7123 }
0d57b499
BM
7124#ifndef CONFIG_USER_ONLY
7125 /* Data Cache clean instructions up to PoP */
7126 if (cpu_isar_feature(aa64_dcpop, cpu)) {
7127 define_one_arm_cp_reg(cpu, dcpop_reg);
7128
7129 if (cpu_isar_feature(aa64_dcpodp, cpu)) {
7130 define_one_arm_cp_reg(cpu, dcpodp_reg);
7131 }
7132 }
7133#endif /*CONFIG_USER_ONLY*/
967aa94f 7134#endif
cb570bd3
RH
7135
7136 /*
7137 * While all v8.0 cpus support aarch64, QEMU does have configurations
7138 * that do not set ID_AA64ISAR1, e.g. user-only qemu-arm -cpu max,
7139 * which will set ID_ISAR6.
7140 */
7141 if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64)
7142 ? cpu_isar_feature(aa64_predinv, cpu)
7143 : cpu_isar_feature(aa32_predinv, cpu)) {
7144 define_arm_cp_regs(cpu, predinv_reginfo);
7145 }
2ceb98c0
PM
7146}
7147
14969266
AF
7148void arm_cpu_register_gdb_regs_for_features(ARMCPU *cpu)
7149{
22169d41 7150 CPUState *cs = CPU(cpu);
14969266
AF
7151 CPUARMState *env = &cpu->env;
7152
6a669427
PM
7153 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
7154 gdb_register_coprocessor(cs, aarch64_fpu_gdb_get_reg,
7155 aarch64_fpu_gdb_set_reg,
7156 34, "aarch64-fpu.xml", 0);
7157 } else if (arm_feature(env, ARM_FEATURE_NEON)) {
22169d41 7158 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
56aebc89
PB
7159 51, "arm-neon.xml", 0);
7160 } else if (arm_feature(env, ARM_FEATURE_VFP3)) {
22169d41 7161 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
56aebc89
PB
7162 35, "arm-vfp3.xml", 0);
7163 } else if (arm_feature(env, ARM_FEATURE_VFP)) {
22169d41 7164 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
56aebc89
PB
7165 19, "arm-vfp.xml", 0);
7166 }
200bf5b7
AB
7167 gdb_register_coprocessor(cs, arm_gdb_get_sysreg, arm_gdb_set_sysreg,
7168 arm_gen_dynamic_xml(cs),
7169 "system-registers.xml", 0);
40f137e1
PB
7170}
7171
777dc784
PM
7172/* Sort alphabetically by type name, except for "any". */
7173static gint arm_cpu_list_compare(gconstpointer a, gconstpointer b)
5adb4839 7174{
777dc784
PM
7175 ObjectClass *class_a = (ObjectClass *)a;
7176 ObjectClass *class_b = (ObjectClass *)b;
7177 const char *name_a, *name_b;
5adb4839 7178
777dc784
PM
7179 name_a = object_class_get_name(class_a);
7180 name_b = object_class_get_name(class_b);
51492fd1 7181 if (strcmp(name_a, "any-" TYPE_ARM_CPU) == 0) {
777dc784 7182 return 1;
51492fd1 7183 } else if (strcmp(name_b, "any-" TYPE_ARM_CPU) == 0) {
777dc784
PM
7184 return -1;
7185 } else {
7186 return strcmp(name_a, name_b);
5adb4839
PB
7187 }
7188}
7189
777dc784 7190static void arm_cpu_list_entry(gpointer data, gpointer user_data)
40f137e1 7191{
777dc784 7192 ObjectClass *oc = data;
51492fd1
AF
7193 const char *typename;
7194 char *name;
3371d272 7195
51492fd1
AF
7196 typename = object_class_get_name(oc);
7197 name = g_strndup(typename, strlen(typename) - strlen("-" TYPE_ARM_CPU));
0442428a 7198 qemu_printf(" %s\n", name);
51492fd1 7199 g_free(name);
777dc784
PM
7200}
7201
0442428a 7202void arm_cpu_list(void)
777dc784 7203{
777dc784
PM
7204 GSList *list;
7205
7206 list = object_class_get_list(TYPE_ARM_CPU, false);
7207 list = g_slist_sort(list, arm_cpu_list_compare);
0442428a
MA
7208 qemu_printf("Available CPUs:\n");
7209 g_slist_foreach(list, arm_cpu_list_entry, NULL);
777dc784 7210 g_slist_free(list);
40f137e1
PB
7211}
7212
78027bb6
CR
7213static void arm_cpu_add_definition(gpointer data, gpointer user_data)
7214{
7215 ObjectClass *oc = data;
7216 CpuDefinitionInfoList **cpu_list = user_data;
7217 CpuDefinitionInfoList *entry;
7218 CpuDefinitionInfo *info;
7219 const char *typename;
7220
7221 typename = object_class_get_name(oc);
7222 info = g_malloc0(sizeof(*info));
7223 info->name = g_strndup(typename,
7224 strlen(typename) - strlen("-" TYPE_ARM_CPU));
8ed877b7 7225 info->q_typename = g_strdup(typename);
78027bb6
CR
7226
7227 entry = g_malloc0(sizeof(*entry));
7228 entry->value = info;
7229 entry->next = *cpu_list;
7230 *cpu_list = entry;
7231}
7232
25a9d6ca 7233CpuDefinitionInfoList *qmp_query_cpu_definitions(Error **errp)
78027bb6
CR
7234{
7235 CpuDefinitionInfoList *cpu_list = NULL;
7236 GSList *list;
7237
7238 list = object_class_get_list(TYPE_ARM_CPU, false);
7239 g_slist_foreach(list, arm_cpu_add_definition, &cpu_list);
7240 g_slist_free(list);
7241
7242 return cpu_list;
7243}
7244
6e6efd61 7245static void add_cpreg_to_hashtable(ARMCPU *cpu, const ARMCPRegInfo *r,
51a79b03 7246 void *opaque, int state, int secstate,
9c513e78
AB
7247 int crm, int opc1, int opc2,
7248 const char *name)
6e6efd61
PM
7249{
7250 /* Private utility function for define_one_arm_cp_reg_with_opaque():
7251 * add a single reginfo struct to the hash table.
7252 */
7253 uint32_t *key = g_new(uint32_t, 1);
7254 ARMCPRegInfo *r2 = g_memdup(r, sizeof(ARMCPRegInfo));
7255 int is64 = (r->type & ARM_CP_64BIT) ? 1 : 0;
3f3c82a5
FA
7256 int ns = (secstate & ARM_CP_SECSTATE_NS) ? 1 : 0;
7257
9c513e78 7258 r2->name = g_strdup(name);
3f3c82a5
FA
7259 /* Reset the secure state to the specific incoming state. This is
7260 * necessary as the register may have been defined with both states.
7261 */
7262 r2->secure = secstate;
7263
7264 if (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1]) {
7265 /* Register is banked (using both entries in array).
7266 * Overwriting fieldoffset as the array is only used to define
7267 * banked registers but later only fieldoffset is used.
f5a0a5a5 7268 */
3f3c82a5
FA
7269 r2->fieldoffset = r->bank_fieldoffsets[ns];
7270 }
7271
7272 if (state == ARM_CP_STATE_AA32) {
7273 if (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1]) {
7274 /* If the register is banked then we don't need to migrate or
7275 * reset the 32-bit instance in certain cases:
7276 *
7277 * 1) If the register has both 32-bit and 64-bit instances then we
7278 * can count on the 64-bit instance taking care of the
7279 * non-secure bank.
7280 * 2) If ARMv8 is enabled then we can count on a 64-bit version
7281 * taking care of the secure bank. This requires that separate
7282 * 32 and 64-bit definitions are provided.
7283 */
7284 if ((r->state == ARM_CP_STATE_BOTH && ns) ||
7285 (arm_feature(&cpu->env, ARM_FEATURE_V8) && !ns)) {
7a0e58fa 7286 r2->type |= ARM_CP_ALIAS;
3f3c82a5
FA
7287 }
7288 } else if ((secstate != r->secure) && !ns) {
7289 /* The register is not banked so we only want to allow migration of
7290 * the non-secure instance.
7291 */
7a0e58fa 7292 r2->type |= ARM_CP_ALIAS;
58a1d8ce 7293 }
3f3c82a5
FA
7294
7295 if (r->state == ARM_CP_STATE_BOTH) {
7296 /* We assume it is a cp15 register if the .cp field is left unset.
7297 */
7298 if (r2->cp == 0) {
7299 r2->cp = 15;
7300 }
7301
f5a0a5a5 7302#ifdef HOST_WORDS_BIGENDIAN
3f3c82a5
FA
7303 if (r2->fieldoffset) {
7304 r2->fieldoffset += sizeof(uint32_t);
7305 }
f5a0a5a5 7306#endif
3f3c82a5 7307 }
f5a0a5a5
PM
7308 }
7309 if (state == ARM_CP_STATE_AA64) {
7310 /* To allow abbreviation of ARMCPRegInfo
7311 * definitions, we treat cp == 0 as equivalent to
7312 * the value for "standard guest-visible sysreg".
58a1d8ce
PM
7313 * STATE_BOTH definitions are also always "standard
7314 * sysreg" in their AArch64 view (the .cp value may
7315 * be non-zero for the benefit of the AArch32 view).
f5a0a5a5 7316 */
58a1d8ce 7317 if (r->cp == 0 || r->state == ARM_CP_STATE_BOTH) {
f5a0a5a5
PM
7318 r2->cp = CP_REG_ARM64_SYSREG_CP;
7319 }
7320 *key = ENCODE_AA64_CP_REG(r2->cp, r2->crn, crm,
7321 r2->opc0, opc1, opc2);
7322 } else {
51a79b03 7323 *key = ENCODE_CP_REG(r2->cp, is64, ns, r2->crn, crm, opc1, opc2);
f5a0a5a5 7324 }
6e6efd61
PM
7325 if (opaque) {
7326 r2->opaque = opaque;
7327 }
67ed771d
PM
7328 /* reginfo passed to helpers is correct for the actual access,
7329 * and is never ARM_CP_STATE_BOTH:
7330 */
7331 r2->state = state;
6e6efd61
PM
7332 /* Make sure reginfo passed to helpers for wildcarded regs
7333 * has the correct crm/opc1/opc2 for this reg, not CP_ANY:
7334 */
7335 r2->crm = crm;
7336 r2->opc1 = opc1;
7337 r2->opc2 = opc2;
7338 /* By convention, for wildcarded registers only the first
7339 * entry is used for migration; the others are marked as
7a0e58fa 7340 * ALIAS so we don't try to transfer the register
6e6efd61 7341 * multiple times. Special registers (ie NOP/WFI) are
7a0e58fa 7342 * never migratable and not even raw-accessible.
6e6efd61 7343 */
7a0e58fa
PM
7344 if ((r->type & ARM_CP_SPECIAL)) {
7345 r2->type |= ARM_CP_NO_RAW;
7346 }
7347 if (((r->crm == CP_ANY) && crm != 0) ||
6e6efd61
PM
7348 ((r->opc1 == CP_ANY) && opc1 != 0) ||
7349 ((r->opc2 == CP_ANY) && opc2 != 0)) {
1f163787 7350 r2->type |= ARM_CP_ALIAS | ARM_CP_NO_GDB;
6e6efd61
PM
7351 }
7352
375421cc
PM
7353 /* Check that raw accesses are either forbidden or handled. Note that
7354 * we can't assert this earlier because the setup of fieldoffset for
7355 * banked registers has to be done first.
7356 */
7357 if (!(r2->type & ARM_CP_NO_RAW)) {
7358 assert(!raw_accessors_invalid(r2));
7359 }
7360
6e6efd61
PM
7361 /* Overriding of an existing definition must be explicitly
7362 * requested.
7363 */
7364 if (!(r->type & ARM_CP_OVERRIDE)) {
7365 ARMCPRegInfo *oldreg;
7366 oldreg = g_hash_table_lookup(cpu->cp_regs, key);
7367 if (oldreg && !(oldreg->type & ARM_CP_OVERRIDE)) {
7368 fprintf(stderr, "Register redefined: cp=%d %d bit "
7369 "crn=%d crm=%d opc1=%d opc2=%d, "
7370 "was %s, now %s\n", r2->cp, 32 + 32 * is64,
7371 r2->crn, r2->crm, r2->opc1, r2->opc2,
7372 oldreg->name, r2->name);
7373 g_assert_not_reached();
7374 }
7375 }
7376 g_hash_table_insert(cpu->cp_regs, key, r2);
7377}
7378
7379
4b6a83fb
PM
7380void define_one_arm_cp_reg_with_opaque(ARMCPU *cpu,
7381 const ARMCPRegInfo *r, void *opaque)
7382{
7383 /* Define implementations of coprocessor registers.
7384 * We store these in a hashtable because typically
7385 * there are less than 150 registers in a space which
7386 * is 16*16*16*8*8 = 262144 in size.
7387 * Wildcarding is supported for the crm, opc1 and opc2 fields.
7388 * If a register is defined twice then the second definition is
7389 * used, so this can be used to define some generic registers and
7390 * then override them with implementation specific variations.
7391 * At least one of the original and the second definition should
7392 * include ARM_CP_OVERRIDE in its type bits -- this is just a guard
7393 * against accidental use.
f5a0a5a5
PM
7394 *
7395 * The state field defines whether the register is to be
7396 * visible in the AArch32 or AArch64 execution state. If the
7397 * state is set to ARM_CP_STATE_BOTH then we synthesise a
7398 * reginfo structure for the AArch32 view, which sees the lower
7399 * 32 bits of the 64 bit register.
7400 *
7401 * Only registers visible in AArch64 may set r->opc0; opc0 cannot
7402 * be wildcarded. AArch64 registers are always considered to be 64
7403 * bits; the ARM_CP_64BIT* flag applies only to the AArch32 view of
7404 * the register, if any.
4b6a83fb 7405 */
f5a0a5a5 7406 int crm, opc1, opc2, state;
4b6a83fb
PM
7407 int crmmin = (r->crm == CP_ANY) ? 0 : r->crm;
7408 int crmmax = (r->crm == CP_ANY) ? 15 : r->crm;
7409 int opc1min = (r->opc1 == CP_ANY) ? 0 : r->opc1;
7410 int opc1max = (r->opc1 == CP_ANY) ? 7 : r->opc1;
7411 int opc2min = (r->opc2 == CP_ANY) ? 0 : r->opc2;
7412 int opc2max = (r->opc2 == CP_ANY) ? 7 : r->opc2;
7413 /* 64 bit registers have only CRm and Opc1 fields */
7414 assert(!((r->type & ARM_CP_64BIT) && (r->opc2 || r->crn)));
f5a0a5a5
PM
7415 /* op0 only exists in the AArch64 encodings */
7416 assert((r->state != ARM_CP_STATE_AA32) || (r->opc0 == 0));
7417 /* AArch64 regs are all 64 bit so ARM_CP_64BIT is meaningless */
7418 assert((r->state != ARM_CP_STATE_AA64) || !(r->type & ARM_CP_64BIT));
7419 /* The AArch64 pseudocode CheckSystemAccess() specifies that op1
7420 * encodes a minimum access level for the register. We roll this
7421 * runtime check into our general permission check code, so check
7422 * here that the reginfo's specified permissions are strict enough
7423 * to encompass the generic architectural permission check.
7424 */
7425 if (r->state != ARM_CP_STATE_AA32) {
7426 int mask = 0;
7427 switch (r->opc1) {
b5bd7440
AB
7428 case 0:
7429 /* min_EL EL1, but some accessible to EL0 via kernel ABI */
7430 mask = PL0U_R | PL1_RW;
7431 break;
7432 case 1: case 2:
f5a0a5a5
PM
7433 /* min_EL EL1 */
7434 mask = PL1_RW;
7435 break;
7436 case 3:
7437 /* min_EL EL0 */
7438 mask = PL0_RW;
7439 break;
7440 case 4:
7441 /* min_EL EL2 */
7442 mask = PL2_RW;
7443 break;
7444 case 5:
7445 /* unallocated encoding, so not possible */
7446 assert(false);
7447 break;
7448 case 6:
7449 /* min_EL EL3 */
7450 mask = PL3_RW;
7451 break;
7452 case 7:
7453 /* min_EL EL1, secure mode only (we don't check the latter) */
7454 mask = PL1_RW;
7455 break;
7456 default:
7457 /* broken reginfo with out-of-range opc1 */
7458 assert(false);
7459 break;
7460 }
7461 /* assert our permissions are not too lax (stricter is fine) */
7462 assert((r->access & ~mask) == 0);
7463 }
7464
4b6a83fb
PM
7465 /* Check that the register definition has enough info to handle
7466 * reads and writes if they are permitted.
7467 */
7468 if (!(r->type & (ARM_CP_SPECIAL|ARM_CP_CONST))) {
7469 if (r->access & PL3_R) {
3f3c82a5
FA
7470 assert((r->fieldoffset ||
7471 (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
7472 r->readfn);
4b6a83fb
PM
7473 }
7474 if (r->access & PL3_W) {
3f3c82a5
FA
7475 assert((r->fieldoffset ||
7476 (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
7477 r->writefn);
4b6a83fb
PM
7478 }
7479 }
7480 /* Bad type field probably means missing sentinel at end of reg list */
7481 assert(cptype_valid(r->type));
7482 for (crm = crmmin; crm <= crmmax; crm++) {
7483 for (opc1 = opc1min; opc1 <= opc1max; opc1++) {
7484 for (opc2 = opc2min; opc2 <= opc2max; opc2++) {
f5a0a5a5
PM
7485 for (state = ARM_CP_STATE_AA32;
7486 state <= ARM_CP_STATE_AA64; state++) {
7487 if (r->state != state && r->state != ARM_CP_STATE_BOTH) {
7488 continue;
7489 }
3f3c82a5
FA
7490 if (state == ARM_CP_STATE_AA32) {
7491 /* Under AArch32 CP registers can be common
7492 * (same for secure and non-secure world) or banked.
7493 */
9c513e78
AB
7494 char *name;
7495
3f3c82a5
FA
7496 switch (r->secure) {
7497 case ARM_CP_SECSTATE_S:
7498 case ARM_CP_SECSTATE_NS:
7499 add_cpreg_to_hashtable(cpu, r, opaque, state,
9c513e78
AB
7500 r->secure, crm, opc1, opc2,
7501 r->name);
3f3c82a5
FA
7502 break;
7503 default:
9c513e78 7504 name = g_strdup_printf("%s_S", r->name);
3f3c82a5
FA
7505 add_cpreg_to_hashtable(cpu, r, opaque, state,
7506 ARM_CP_SECSTATE_S,
9c513e78
AB
7507 crm, opc1, opc2, name);
7508 g_free(name);
3f3c82a5
FA
7509 add_cpreg_to_hashtable(cpu, r, opaque, state,
7510 ARM_CP_SECSTATE_NS,
9c513e78 7511 crm, opc1, opc2, r->name);
3f3c82a5
FA
7512 break;
7513 }
7514 } else {
7515 /* AArch64 registers get mapped to non-secure instance
7516 * of AArch32 */
7517 add_cpreg_to_hashtable(cpu, r, opaque, state,
7518 ARM_CP_SECSTATE_NS,
9c513e78 7519 crm, opc1, opc2, r->name);
3f3c82a5 7520 }
f5a0a5a5 7521 }
4b6a83fb
PM
7522 }
7523 }
7524 }
7525}
7526
7527void define_arm_cp_regs_with_opaque(ARMCPU *cpu,
7528 const ARMCPRegInfo *regs, void *opaque)
7529{
7530 /* Define a whole list of registers */
7531 const ARMCPRegInfo *r;
7532 for (r = regs; r->type != ARM_CP_SENTINEL; r++) {
7533 define_one_arm_cp_reg_with_opaque(cpu, r, opaque);
7534 }
7535}
7536
6c5c0fec
AB
7537/*
7538 * Modify ARMCPRegInfo for access from userspace.
7539 *
7540 * This is a data driven modification directed by
7541 * ARMCPRegUserSpaceInfo. All registers become ARM_CP_CONST as
7542 * user-space cannot alter any values and dynamic values pertaining to
7543 * execution state are hidden from user space view anyway.
7544 */
7545void modify_arm_cp_regs(ARMCPRegInfo *regs, const ARMCPRegUserSpaceInfo *mods)
7546{
7547 const ARMCPRegUserSpaceInfo *m;
7548 ARMCPRegInfo *r;
7549
7550 for (m = mods; m->name; m++) {
d040242e
AB
7551 GPatternSpec *pat = NULL;
7552 if (m->is_glob) {
7553 pat = g_pattern_spec_new(m->name);
7554 }
6c5c0fec 7555 for (r = regs; r->type != ARM_CP_SENTINEL; r++) {
d040242e
AB
7556 if (pat && g_pattern_match_string(pat, r->name)) {
7557 r->type = ARM_CP_CONST;
7558 r->access = PL0U_R;
7559 r->resetvalue = 0;
7560 /* continue */
7561 } else if (strcmp(r->name, m->name) == 0) {
6c5c0fec
AB
7562 r->type = ARM_CP_CONST;
7563 r->access = PL0U_R;
7564 r->resetvalue &= m->exported_bits;
7565 r->resetvalue |= m->fixed_bits;
7566 break;
7567 }
7568 }
d040242e
AB
7569 if (pat) {
7570 g_pattern_spec_free(pat);
7571 }
6c5c0fec
AB
7572 }
7573}
7574
60322b39 7575const ARMCPRegInfo *get_arm_cp_reginfo(GHashTable *cpregs, uint32_t encoded_cp)
4b6a83fb 7576{
60322b39 7577 return g_hash_table_lookup(cpregs, &encoded_cp);
4b6a83fb
PM
7578}
7579
c4241c7d
PM
7580void arm_cp_write_ignore(CPUARMState *env, const ARMCPRegInfo *ri,
7581 uint64_t value)
4b6a83fb
PM
7582{
7583 /* Helper coprocessor write function for write-ignore registers */
4b6a83fb
PM
7584}
7585
c4241c7d 7586uint64_t arm_cp_read_zero(CPUARMState *env, const ARMCPRegInfo *ri)
4b6a83fb
PM
7587{
7588 /* Helper coprocessor write function for read-as-zero registers */
4b6a83fb
PM
7589 return 0;
7590}
7591
f5a0a5a5
PM
7592void arm_cp_reset_ignore(CPUARMState *env, const ARMCPRegInfo *opaque)
7593{
7594 /* Helper coprocessor reset function for do-nothing-on-reset registers */
7595}
7596
af393ffc 7597static int bad_mode_switch(CPUARMState *env, int mode, CPSRWriteType write_type)
37064a8b
PM
7598{
7599 /* Return true if it is not valid for us to switch to
7600 * this CPU mode (ie all the UNPREDICTABLE cases in
7601 * the ARM ARM CPSRWriteByInstr pseudocode).
7602 */
af393ffc
PM
7603
7604 /* Changes to or from Hyp via MSR and CPS are illegal. */
7605 if (write_type == CPSRWriteByInstr &&
7606 ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_HYP ||
7607 mode == ARM_CPU_MODE_HYP)) {
7608 return 1;
7609 }
7610
37064a8b
PM
7611 switch (mode) {
7612 case ARM_CPU_MODE_USR:
10eacda7 7613 return 0;
37064a8b
PM
7614 case ARM_CPU_MODE_SYS:
7615 case ARM_CPU_MODE_SVC:
7616 case ARM_CPU_MODE_ABT:
7617 case ARM_CPU_MODE_UND:
7618 case ARM_CPU_MODE_IRQ:
7619 case ARM_CPU_MODE_FIQ:
52ff951b
PM
7620 /* Note that we don't implement the IMPDEF NSACR.RFR which in v7
7621 * allows FIQ mode to be Secure-only. (In v8 this doesn't exist.)
7622 */
10eacda7
PM
7623 /* If HCR.TGE is set then changes from Monitor to NS PL1 via MSR
7624 * and CPS are treated as illegal mode changes.
7625 */
7626 if (write_type == CPSRWriteByInstr &&
10eacda7 7627 (env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON &&
7c208e0f 7628 (arm_hcr_el2_eff(env) & HCR_TGE)) {
10eacda7
PM
7629 return 1;
7630 }
37064a8b 7631 return 0;
e6c8fc07
PM
7632 case ARM_CPU_MODE_HYP:
7633 return !arm_feature(env, ARM_FEATURE_EL2)
2d2a4549 7634 || arm_current_el(env) < 2 || arm_is_secure_below_el3(env);
027fc527 7635 case ARM_CPU_MODE_MON:
58ae2d1f 7636 return arm_current_el(env) < 3;
37064a8b
PM
7637 default:
7638 return 1;
7639 }
7640}
7641
2f4a40e5
AZ
7642uint32_t cpsr_read(CPUARMState *env)
7643{
7644 int ZF;
6fbe23d5
PB
7645 ZF = (env->ZF == 0);
7646 return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) |
2f4a40e5
AZ
7647 (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27)
7648 | (env->thumb << 5) | ((env->condexec_bits & 3) << 25)
7649 | ((env->condexec_bits & 0xfc) << 8)
af519934 7650 | (env->GE << 16) | (env->daif & CPSR_AIF);
2f4a40e5
AZ
7651}
7652
50866ba5
PM
7653void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask,
7654 CPSRWriteType write_type)
2f4a40e5 7655{
6e8801f9
FA
7656 uint32_t changed_daif;
7657
2f4a40e5 7658 if (mask & CPSR_NZCV) {
6fbe23d5
PB
7659 env->ZF = (~val) & CPSR_Z;
7660 env->NF = val;
2f4a40e5
AZ
7661 env->CF = (val >> 29) & 1;
7662 env->VF = (val << 3) & 0x80000000;
7663 }
7664 if (mask & CPSR_Q)
7665 env->QF = ((val & CPSR_Q) != 0);
7666 if (mask & CPSR_T)
7667 env->thumb = ((val & CPSR_T) != 0);
7668 if (mask & CPSR_IT_0_1) {
7669 env->condexec_bits &= ~3;
7670 env->condexec_bits |= (val >> 25) & 3;
7671 }
7672 if (mask & CPSR_IT_2_7) {
7673 env->condexec_bits &= 3;
7674 env->condexec_bits |= (val >> 8) & 0xfc;
7675 }
7676 if (mask & CPSR_GE) {
7677 env->GE = (val >> 16) & 0xf;
7678 }
7679
6e8801f9
FA
7680 /* In a V7 implementation that includes the security extensions but does
7681 * not include Virtualization Extensions the SCR.FW and SCR.AW bits control
7682 * whether non-secure software is allowed to change the CPSR_F and CPSR_A
7683 * bits respectively.
7684 *
7685 * In a V8 implementation, it is permitted for privileged software to
7686 * change the CPSR A/F bits regardless of the SCR.AW/FW bits.
7687 */
f8c88bbc 7688 if (write_type != CPSRWriteRaw && !arm_feature(env, ARM_FEATURE_V8) &&
6e8801f9
FA
7689 arm_feature(env, ARM_FEATURE_EL3) &&
7690 !arm_feature(env, ARM_FEATURE_EL2) &&
7691 !arm_is_secure(env)) {
7692
7693 changed_daif = (env->daif ^ val) & mask;
7694
7695 if (changed_daif & CPSR_A) {
7696 /* Check to see if we are allowed to change the masking of async
7697 * abort exceptions from a non-secure state.
7698 */
7699 if (!(env->cp15.scr_el3 & SCR_AW)) {
7700 qemu_log_mask(LOG_GUEST_ERROR,
7701 "Ignoring attempt to switch CPSR_A flag from "
7702 "non-secure world with SCR.AW bit clear\n");
7703 mask &= ~CPSR_A;
7704 }
7705 }
7706
7707 if (changed_daif & CPSR_F) {
7708 /* Check to see if we are allowed to change the masking of FIQ
7709 * exceptions from a non-secure state.
7710 */
7711 if (!(env->cp15.scr_el3 & SCR_FW)) {
7712 qemu_log_mask(LOG_GUEST_ERROR,
7713 "Ignoring attempt to switch CPSR_F flag from "
7714 "non-secure world with SCR.FW bit clear\n");
7715 mask &= ~CPSR_F;
7716 }
7717
7718 /* Check whether non-maskable FIQ (NMFI) support is enabled.
7719 * If this bit is set software is not allowed to mask
7720 * FIQs, but is allowed to set CPSR_F to 0.
7721 */
7722 if ((A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_NMFI) &&
7723 (val & CPSR_F)) {
7724 qemu_log_mask(LOG_GUEST_ERROR,
7725 "Ignoring attempt to enable CPSR_F flag "
7726 "(non-maskable FIQ [NMFI] support enabled)\n");
7727 mask &= ~CPSR_F;
7728 }
7729 }
7730 }
7731
4cc35614
PM
7732 env->daif &= ~(CPSR_AIF & mask);
7733 env->daif |= val & CPSR_AIF & mask;
7734
f8c88bbc
PM
7735 if (write_type != CPSRWriteRaw &&
7736 ((env->uncached_cpsr ^ val) & mask & CPSR_M)) {
8c4f0eb9
PM
7737 if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_USR) {
7738 /* Note that we can only get here in USR mode if this is a
7739 * gdb stub write; for this case we follow the architectural
7740 * behaviour for guest writes in USR mode of ignoring an attempt
7741 * to switch mode. (Those are caught by translate.c for writes
7742 * triggered by guest instructions.)
7743 */
7744 mask &= ~CPSR_M;
7745 } else if (bad_mode_switch(env, val & CPSR_M, write_type)) {
81907a58
PM
7746 /* Attempt to switch to an invalid mode: this is UNPREDICTABLE in
7747 * v7, and has defined behaviour in v8:
7748 * + leave CPSR.M untouched
7749 * + allow changes to the other CPSR fields
7750 * + set PSTATE.IL
7751 * For user changes via the GDB stub, we don't set PSTATE.IL,
7752 * as this would be unnecessarily harsh for a user error.
37064a8b
PM
7753 */
7754 mask &= ~CPSR_M;
81907a58
PM
7755 if (write_type != CPSRWriteByGDBStub &&
7756 arm_feature(env, ARM_FEATURE_V8)) {
7757 mask |= CPSR_IL;
7758 val |= CPSR_IL;
7759 }
81e37284
PM
7760 qemu_log_mask(LOG_GUEST_ERROR,
7761 "Illegal AArch32 mode switch attempt from %s to %s\n",
7762 aarch32_mode_name(env->uncached_cpsr),
7763 aarch32_mode_name(val));
37064a8b 7764 } else {
81e37284
PM
7765 qemu_log_mask(CPU_LOG_INT, "%s %s to %s PC 0x%" PRIx32 "\n",
7766 write_type == CPSRWriteExceptionReturn ?
7767 "Exception return from AArch32" :
7768 "AArch32 mode switch from",
7769 aarch32_mode_name(env->uncached_cpsr),
7770 aarch32_mode_name(val), env->regs[15]);
37064a8b
PM
7771 switch_mode(env, val & CPSR_M);
7772 }
2f4a40e5
AZ
7773 }
7774 mask &= ~CACHED_CPSR_BITS;
7775 env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask);
7776}
7777
b26eefb6
PB
7778/* Sign/zero extend */
7779uint32_t HELPER(sxtb16)(uint32_t x)
7780{
7781 uint32_t res;
7782 res = (uint16_t)(int8_t)x;
7783 res |= (uint32_t)(int8_t)(x >> 16) << 16;
7784 return res;
7785}
7786
7787uint32_t HELPER(uxtb16)(uint32_t x)
7788{
7789 uint32_t res;
7790 res = (uint16_t)(uint8_t)x;
7791 res |= (uint32_t)(uint8_t)(x >> 16) << 16;
7792 return res;
7793}
7794
3670669c
PB
7795int32_t HELPER(sdiv)(int32_t num, int32_t den)
7796{
7797 if (den == 0)
7798 return 0;
686eeb93
AJ
7799 if (num == INT_MIN && den == -1)
7800 return INT_MIN;
3670669c
PB
7801 return num / den;
7802}
7803
7804uint32_t HELPER(udiv)(uint32_t num, uint32_t den)
7805{
7806 if (den == 0)
7807 return 0;
7808 return num / den;
7809}
7810
7811uint32_t HELPER(rbit)(uint32_t x)
7812{
42fedbca 7813 return revbit32(x);
3670669c
PB
7814}
7815
c47eaf9f 7816#ifdef CONFIG_USER_ONLY
b5ff1b31 7817
affdb64d 7818static void switch_mode(CPUARMState *env, int mode)
b5ff1b31 7819{
2fc0cc0e 7820 ARMCPU *cpu = env_archcpu(env);
a47dddd7
AF
7821
7822 if (mode != ARM_CPU_MODE_USR) {
7823 cpu_abort(CPU(cpu), "Tried to switch out of user mode\n");
7824 }
b5ff1b31
FB
7825}
7826
012a906b
GB
7827uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
7828 uint32_t cur_el, bool secure)
9e729b57
EI
7829{
7830 return 1;
7831}
7832
ce02049d
GB
7833void aarch64_sync_64_to_32(CPUARMState *env)
7834{
7835 g_assert_not_reached();
7836}
7837
b5ff1b31
FB
7838#else
7839
affdb64d 7840static void switch_mode(CPUARMState *env, int mode)
b5ff1b31
FB
7841{
7842 int old_mode;
7843 int i;
7844
7845 old_mode = env->uncached_cpsr & CPSR_M;
7846 if (mode == old_mode)
7847 return;
7848
7849 if (old_mode == ARM_CPU_MODE_FIQ) {
7850 memcpy (env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t));
8637c67f 7851 memcpy (env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t));
b5ff1b31
FB
7852 } else if (mode == ARM_CPU_MODE_FIQ) {
7853 memcpy (env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t));
8637c67f 7854 memcpy (env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t));
b5ff1b31
FB
7855 }
7856
f5206413 7857 i = bank_number(old_mode);
b5ff1b31 7858 env->banked_r13[i] = env->regs[13];
b5ff1b31
FB
7859 env->banked_spsr[i] = env->spsr;
7860
f5206413 7861 i = bank_number(mode);
b5ff1b31 7862 env->regs[13] = env->banked_r13[i];
b5ff1b31 7863 env->spsr = env->banked_spsr[i];
593cfa2b
PM
7864
7865 env->banked_r14[r14_bank_number(old_mode)] = env->regs[14];
7866 env->regs[14] = env->banked_r14[r14_bank_number(mode)];
b5ff1b31
FB
7867}
7868
0eeb17d6
GB
7869/* Physical Interrupt Target EL Lookup Table
7870 *
7871 * [ From ARM ARM section G1.13.4 (Table G1-15) ]
7872 *
7873 * The below multi-dimensional table is used for looking up the target
7874 * exception level given numerous condition criteria. Specifically, the
7875 * target EL is based on SCR and HCR routing controls as well as the
7876 * currently executing EL and secure state.
7877 *
7878 * Dimensions:
7879 * target_el_table[2][2][2][2][2][4]
7880 * | | | | | +--- Current EL
7881 * | | | | +------ Non-secure(0)/Secure(1)
7882 * | | | +--------- HCR mask override
7883 * | | +------------ SCR exec state control
7884 * | +--------------- SCR mask override
7885 * +------------------ 32-bit(0)/64-bit(1) EL3
7886 *
7887 * The table values are as such:
7888 * 0-3 = EL0-EL3
7889 * -1 = Cannot occur
7890 *
7891 * The ARM ARM target EL table includes entries indicating that an "exception
7892 * is not taken". The two cases where this is applicable are:
7893 * 1) An exception is taken from EL3 but the SCR does not have the exception
7894 * routed to EL3.
7895 * 2) An exception is taken from EL2 but the HCR does not have the exception
7896 * routed to EL2.
7897 * In these two cases, the below table contain a target of EL1. This value is
7898 * returned as it is expected that the consumer of the table data will check
7899 * for "target EL >= current EL" to ensure the exception is not taken.
7900 *
7901 * SCR HCR
7902 * 64 EA AMO From
7903 * BIT IRQ IMO Non-secure Secure
7904 * EL3 FIQ RW FMO EL0 EL1 EL2 EL3 EL0 EL1 EL2 EL3
7905 */
82c39f6a 7906static const int8_t target_el_table[2][2][2][2][2][4] = {
0eeb17d6
GB
7907 {{{{/* 0 0 0 0 */{ 1, 1, 2, -1 },{ 3, -1, -1, 3 },},
7908 {/* 0 0 0 1 */{ 2, 2, 2, -1 },{ 3, -1, -1, 3 },},},
7909 {{/* 0 0 1 0 */{ 1, 1, 2, -1 },{ 3, -1, -1, 3 },},
7910 {/* 0 0 1 1 */{ 2, 2, 2, -1 },{ 3, -1, -1, 3 },},},},
7911 {{{/* 0 1 0 0 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},
7912 {/* 0 1 0 1 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},},
7913 {{/* 0 1 1 0 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},
7914 {/* 0 1 1 1 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},},},},
7915 {{{{/* 1 0 0 0 */{ 1, 1, 2, -1 },{ 1, 1, -1, 1 },},
7916 {/* 1 0 0 1 */{ 2, 2, 2, -1 },{ 1, 1, -1, 1 },},},
7917 {{/* 1 0 1 0 */{ 1, 1, 1, -1 },{ 1, 1, -1, 1 },},
7918 {/* 1 0 1 1 */{ 2, 2, 2, -1 },{ 1, 1, -1, 1 },},},},
7919 {{{/* 1 1 0 0 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},
7920 {/* 1 1 0 1 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},},
7921 {{/* 1 1 1 0 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},
7922 {/* 1 1 1 1 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},},},},
7923};
7924
7925/*
7926 * Determine the target EL for physical exceptions
7927 */
012a906b
GB
7928uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
7929 uint32_t cur_el, bool secure)
0eeb17d6
GB
7930{
7931 CPUARMState *env = cs->env_ptr;
f7778444
RH
7932 bool rw;
7933 bool scr;
7934 bool hcr;
0eeb17d6 7935 int target_el;
2cde031f 7936 /* Is the highest EL AArch64? */
f7778444
RH
7937 bool is64 = arm_feature(env, ARM_FEATURE_AARCH64);
7938 uint64_t hcr_el2;
2cde031f
SS
7939
7940 if (arm_feature(env, ARM_FEATURE_EL3)) {
7941 rw = ((env->cp15.scr_el3 & SCR_RW) == SCR_RW);
7942 } else {
7943 /* Either EL2 is the highest EL (and so the EL2 register width
7944 * is given by is64); or there is no EL2 or EL3, in which case
7945 * the value of 'rw' does not affect the table lookup anyway.
7946 */
7947 rw = is64;
7948 }
0eeb17d6 7949
f7778444 7950 hcr_el2 = arm_hcr_el2_eff(env);
0eeb17d6
GB
7951 switch (excp_idx) {
7952 case EXCP_IRQ:
7953 scr = ((env->cp15.scr_el3 & SCR_IRQ) == SCR_IRQ);
f7778444 7954 hcr = hcr_el2 & HCR_IMO;
0eeb17d6
GB
7955 break;
7956 case EXCP_FIQ:
7957 scr = ((env->cp15.scr_el3 & SCR_FIQ) == SCR_FIQ);
f7778444 7958 hcr = hcr_el2 & HCR_FMO;
0eeb17d6
GB
7959 break;
7960 default:
7961 scr = ((env->cp15.scr_el3 & SCR_EA) == SCR_EA);
f7778444 7962 hcr = hcr_el2 & HCR_AMO;
0eeb17d6
GB
7963 break;
7964 };
7965
0eeb17d6
GB
7966 /* Perform a table-lookup for the target EL given the current state */
7967 target_el = target_el_table[is64][scr][rw][hcr][secure][cur_el];
7968
7969 assert(target_el > 0);
7970
7971 return target_el;
7972}
7973
b59f479b
PMD
7974void arm_log_exception(int idx)
7975{
7976 if (qemu_loglevel_mask(CPU_LOG_INT)) {
7977 const char *exc = NULL;
7978 static const char * const excnames[] = {
7979 [EXCP_UDEF] = "Undefined Instruction",
7980 [EXCP_SWI] = "SVC",
7981 [EXCP_PREFETCH_ABORT] = "Prefetch Abort",
7982 [EXCP_DATA_ABORT] = "Data Abort",
7983 [EXCP_IRQ] = "IRQ",
7984 [EXCP_FIQ] = "FIQ",
7985 [EXCP_BKPT] = "Breakpoint",
7986 [EXCP_EXCEPTION_EXIT] = "QEMU v7M exception exit",
7987 [EXCP_KERNEL_TRAP] = "QEMU intercept of kernel commpage",
7988 [EXCP_HVC] = "Hypervisor Call",
7989 [EXCP_HYP_TRAP] = "Hypervisor Trap",
7990 [EXCP_SMC] = "Secure Monitor Call",
7991 [EXCP_VIRQ] = "Virtual IRQ",
7992 [EXCP_VFIQ] = "Virtual FIQ",
7993 [EXCP_SEMIHOST] = "Semihosting call",
7994 [EXCP_NOCP] = "v7M NOCP UsageFault",
7995 [EXCP_INVSTATE] = "v7M INVSTATE UsageFault",
7996 [EXCP_STKOF] = "v8M STKOF UsageFault",
7997 [EXCP_LAZYFP] = "v7M exception during lazy FP stacking",
7998 [EXCP_LSERR] = "v8M LSERR UsageFault",
7999 [EXCP_UNALIGNED] = "v7M UNALIGNED UsageFault",
8000 };
8001
8002 if (idx >= 0 && idx < ARRAY_SIZE(excnames)) {
8003 exc = excnames[idx];
8004 }
8005 if (!exc) {
8006 exc = "unknown";
8007 }
8008 qemu_log_mask(CPU_LOG_INT, "Taking exception %d [%s]\n", idx, exc);
8009 }
8010}
8011
a356dacf 8012/*
7aab5a8c
PMD
8013 * Function used to synchronize QEMU's AArch64 register set with AArch32
8014 * register set. This is necessary when switching between AArch32 and AArch64
8015 * execution state.
a356dacf 8016 */
7aab5a8c 8017void aarch64_sync_32_to_64(CPUARMState *env)
9ee6e8bb 8018{
7aab5a8c
PMD
8019 int i;
8020 uint32_t mode = env->uncached_cpsr & CPSR_M;
8021
8022 /* We can blanket copy R[0:7] to X[0:7] */
8023 for (i = 0; i < 8; i++) {
8024 env->xregs[i] = env->regs[i];
fd592d89 8025 }
70d74660 8026
9a223097 8027 /*
7aab5a8c
PMD
8028 * Unless we are in FIQ mode, x8-x12 come from the user registers r8-r12.
8029 * Otherwise, they come from the banked user regs.
fd592d89 8030 */
7aab5a8c
PMD
8031 if (mode == ARM_CPU_MODE_FIQ) {
8032 for (i = 8; i < 13; i++) {
8033 env->xregs[i] = env->usr_regs[i - 8];
8034 }
8035 } else {
8036 for (i = 8; i < 13; i++) {
8037 env->xregs[i] = env->regs[i];
8038 }
fd592d89 8039 }
9ee6e8bb 8040
7aab5a8c
PMD
8041 /*
8042 * Registers x13-x23 are the various mode SP and FP registers. Registers
8043 * r13 and r14 are only copied if we are in that mode, otherwise we copy
8044 * from the mode banked register.
8045 */
8046 if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
8047 env->xregs[13] = env->regs[13];
8048 env->xregs[14] = env->regs[14];
8049 } else {
8050 env->xregs[13] = env->banked_r13[bank_number(ARM_CPU_MODE_USR)];
8051 /* HYP is an exception in that it is copied from r14 */
8052 if (mode == ARM_CPU_MODE_HYP) {
8053 env->xregs[14] = env->regs[14];
95695eff 8054 } else {
7aab5a8c 8055 env->xregs[14] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)];
95695eff 8056 }
95695eff
PM
8057 }
8058
7aab5a8c
PMD
8059 if (mode == ARM_CPU_MODE_HYP) {
8060 env->xregs[15] = env->regs[13];
8061 } else {
8062 env->xregs[15] = env->banked_r13[bank_number(ARM_CPU_MODE_HYP)];
95695eff
PM
8063 }
8064
7aab5a8c
PMD
8065 if (mode == ARM_CPU_MODE_IRQ) {
8066 env->xregs[16] = env->regs[14];
8067 env->xregs[17] = env->regs[13];
8068 } else {
8069 env->xregs[16] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)];
8070 env->xregs[17] = env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)];
8071 }
95695eff 8072
7aab5a8c
PMD
8073 if (mode == ARM_CPU_MODE_SVC) {
8074 env->xregs[18] = env->regs[14];
8075 env->xregs[19] = env->regs[13];
8076 } else {
8077 env->xregs[18] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)];
8078 env->xregs[19] = env->banked_r13[bank_number(ARM_CPU_MODE_SVC)];
8079 }
95695eff 8080
7aab5a8c
PMD
8081 if (mode == ARM_CPU_MODE_ABT) {
8082 env->xregs[20] = env->regs[14];
8083 env->xregs[21] = env->regs[13];
8084 } else {
8085 env->xregs[20] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)];
8086 env->xregs[21] = env->banked_r13[bank_number(ARM_CPU_MODE_ABT)];
8087 }
e33cf0f8 8088
7aab5a8c
PMD
8089 if (mode == ARM_CPU_MODE_UND) {
8090 env->xregs[22] = env->regs[14];
8091 env->xregs[23] = env->regs[13];
8092 } else {
8093 env->xregs[22] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)];
8094 env->xregs[23] = env->banked_r13[bank_number(ARM_CPU_MODE_UND)];
e33cf0f8
PM
8095 }
8096
8097 /*
7aab5a8c
PMD
8098 * Registers x24-x30 are mapped to r8-r14 in FIQ mode. If we are in FIQ
8099 * mode, then we can copy from r8-r14. Otherwise, we copy from the
8100 * FIQ bank for r8-r14.
e33cf0f8 8101 */
7aab5a8c
PMD
8102 if (mode == ARM_CPU_MODE_FIQ) {
8103 for (i = 24; i < 31; i++) {
8104 env->xregs[i] = env->regs[i - 16]; /* X[24:30] <- R[8:14] */
8105 }
8106 } else {
8107 for (i = 24; i < 29; i++) {
8108 env->xregs[i] = env->fiq_regs[i - 24];
e33cf0f8 8109 }
7aab5a8c
PMD
8110 env->xregs[29] = env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)];
8111 env->xregs[30] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)];
e33cf0f8 8112 }
7aab5a8c
PMD
8113
8114 env->pc = env->regs[15];
e33cf0f8
PM
8115}
8116
9a223097 8117/*
7aab5a8c
PMD
8118 * Function used to synchronize QEMU's AArch32 register set with AArch64
8119 * register set. This is necessary when switching between AArch32 and AArch64
8120 * execution state.
de2db7ec 8121 */
7aab5a8c 8122void aarch64_sync_64_to_32(CPUARMState *env)
9ee6e8bb 8123{
7aab5a8c
PMD
8124 int i;
8125 uint32_t mode = env->uncached_cpsr & CPSR_M;
abc24d86 8126
7aab5a8c
PMD
8127 /* We can blanket copy X[0:7] to R[0:7] */
8128 for (i = 0; i < 8; i++) {
8129 env->regs[i] = env->xregs[i];
de2db7ec 8130 }
3f0cddee 8131
9a223097 8132 /*
7aab5a8c
PMD
8133 * Unless we are in FIQ mode, r8-r12 come from the user registers x8-x12.
8134 * Otherwise, we copy x8-x12 into the banked user regs.
de2db7ec 8135 */
7aab5a8c
PMD
8136 if (mode == ARM_CPU_MODE_FIQ) {
8137 for (i = 8; i < 13; i++) {
8138 env->usr_regs[i - 8] = env->xregs[i];
8139 }
8140 } else {
8141 for (i = 8; i < 13; i++) {
8142 env->regs[i] = env->xregs[i];
8143 }
fb602cb7
PM
8144 }
8145
9a223097 8146 /*
7aab5a8c
PMD
8147 * Registers r13 & r14 depend on the current mode.
8148 * If we are in a given mode, we copy the corresponding x registers to r13
8149 * and r14. Otherwise, we copy the x register to the banked r13 and r14
8150 * for the mode.
fb602cb7 8151 */
7aab5a8c
PMD
8152 if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
8153 env->regs[13] = env->xregs[13];
8154 env->regs[14] = env->xregs[14];
fb602cb7 8155 } else {
7aab5a8c 8156 env->banked_r13[bank_number(ARM_CPU_MODE_USR)] = env->xregs[13];
fb602cb7 8157
7aab5a8c
PMD
8158 /*
8159 * HYP is an exception in that it does not have its own banked r14 but
8160 * shares the USR r14
8161 */
8162 if (mode == ARM_CPU_MODE_HYP) {
8163 env->regs[14] = env->xregs[14];
8164 } else {
8165 env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)] = env->xregs[14];
8166 }
8167 }
fb602cb7 8168
7aab5a8c
PMD
8169 if (mode == ARM_CPU_MODE_HYP) {
8170 env->regs[13] = env->xregs[15];
fb602cb7 8171 } else {
7aab5a8c 8172 env->banked_r13[bank_number(ARM_CPU_MODE_HYP)] = env->xregs[15];
fb602cb7 8173 }
d02a8698 8174
7aab5a8c
PMD
8175 if (mode == ARM_CPU_MODE_IRQ) {
8176 env->regs[14] = env->xregs[16];
8177 env->regs[13] = env->xregs[17];
d02a8698 8178 } else {
7aab5a8c
PMD
8179 env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[16];
8180 env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[17];
d02a8698
PM
8181 }
8182
7aab5a8c
PMD
8183 if (mode == ARM_CPU_MODE_SVC) {
8184 env->regs[14] = env->xregs[18];
8185 env->regs[13] = env->xregs[19];
8186 } else {
8187 env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)] = env->xregs[18];
8188 env->banked_r13[bank_number(ARM_CPU_MODE_SVC)] = env->xregs[19];
fb602cb7
PM
8189 }
8190
7aab5a8c
PMD
8191 if (mode == ARM_CPU_MODE_ABT) {
8192 env->regs[14] = env->xregs[20];
8193 env->regs[13] = env->xregs[21];
8194 } else {
8195 env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)] = env->xregs[20];
8196 env->banked_r13[bank_number(ARM_CPU_MODE_ABT)] = env->xregs[21];
ce02049d
GB
8197 }
8198
8199 if (mode == ARM_CPU_MODE_UND) {
3a9148d0
SS
8200 env->regs[14] = env->xregs[22];
8201 env->regs[13] = env->xregs[23];
ce02049d 8202 } else {
593cfa2b 8203 env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)] = env->xregs[22];
3a9148d0 8204 env->banked_r13[bank_number(ARM_CPU_MODE_UND)] = env->xregs[23];
ce02049d
GB
8205 }
8206
8207 /* Registers x24-x30 are mapped to r8-r14 in FIQ mode. If we are in FIQ
8208 * mode, then we can copy to r8-r14. Otherwise, we copy to the
8209 * FIQ bank for r8-r14.
8210 */
8211 if (mode == ARM_CPU_MODE_FIQ) {
8212 for (i = 24; i < 31; i++) {
8213 env->regs[i - 16] = env->xregs[i]; /* X[24:30] -> R[8:14] */
8214 }
8215 } else {
8216 for (i = 24; i < 29; i++) {
8217 env->fiq_regs[i - 24] = env->xregs[i];
8218 }
8219 env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[29];
593cfa2b 8220 env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[30];
ce02049d
GB
8221 }
8222
8223 env->regs[15] = env->pc;
8224}
8225
dea8378b
PM
8226static void take_aarch32_exception(CPUARMState *env, int new_mode,
8227 uint32_t mask, uint32_t offset,
8228 uint32_t newpc)
8229{
8230 /* Change the CPU state so as to actually take the exception. */
8231 switch_mode(env, new_mode);
8232 /*
8233 * For exceptions taken to AArch32 we must clear the SS bit in both
8234 * PSTATE and in the old-state value we save to SPSR_<mode>, so zero it now.
8235 */
8236 env->uncached_cpsr &= ~PSTATE_SS;
8237 env->spsr = cpsr_read(env);
8238 /* Clear IT bits. */
8239 env->condexec_bits = 0;
8240 /* Switch to the new mode, and to the correct instruction set. */
8241 env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode;
8242 /* Set new mode endianness */
8243 env->uncached_cpsr &= ~CPSR_E;
8244 if (env->cp15.sctlr_el[arm_current_el(env)] & SCTLR_EE) {
8245 env->uncached_cpsr |= CPSR_E;
8246 }
829f9fd3
PM
8247 /* J and IL must always be cleared for exception entry */
8248 env->uncached_cpsr &= ~(CPSR_IL | CPSR_J);
dea8378b
PM
8249 env->daif |= mask;
8250
8251 if (new_mode == ARM_CPU_MODE_HYP) {
8252 env->thumb = (env->cp15.sctlr_el[2] & SCTLR_TE) != 0;
8253 env->elr_el[2] = env->regs[15];
8254 } else {
8255 /*
8256 * this is a lie, as there was no c1_sys on V4T/V5, but who cares
8257 * and we should just guard the thumb mode on V4
8258 */
8259 if (arm_feature(env, ARM_FEATURE_V4T)) {
8260 env->thumb =
8261 (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_TE) != 0;
8262 }
8263 env->regs[14] = env->regs[15] + offset;
8264 }
8265 env->regs[15] = newpc;
a8a79c7a 8266 arm_rebuild_hflags(env);
dea8378b
PM
8267}
8268
b9bc21ff
PM
8269static void arm_cpu_do_interrupt_aarch32_hyp(CPUState *cs)
8270{
8271 /*
8272 * Handle exception entry to Hyp mode; this is sufficiently
8273 * different to entry to other AArch32 modes that we handle it
8274 * separately here.
8275 *
8276 * The vector table entry used is always the 0x14 Hyp mode entry point,
8277 * unless this is an UNDEF/HVC/abort taken from Hyp to Hyp.
8278 * The offset applied to the preferred return address is always zero
8279 * (see DDI0487C.a section G1.12.3).
8280 * PSTATE A/I/F masks are set based only on the SCR.EA/IRQ/FIQ values.
8281 */
8282 uint32_t addr, mask;
8283 ARMCPU *cpu = ARM_CPU(cs);
8284 CPUARMState *env = &cpu->env;
8285
8286 switch (cs->exception_index) {
8287 case EXCP_UDEF:
8288 addr = 0x04;
8289 break;
8290 case EXCP_SWI:
8291 addr = 0x14;
8292 break;
8293 case EXCP_BKPT:
8294 /* Fall through to prefetch abort. */
8295 case EXCP_PREFETCH_ABORT:
8296 env->cp15.ifar_s = env->exception.vaddress;
8297 qemu_log_mask(CPU_LOG_INT, "...with HIFAR 0x%x\n",
8298 (uint32_t)env->exception.vaddress);
8299 addr = 0x0c;
8300 break;
8301 case EXCP_DATA_ABORT:
8302 env->cp15.dfar_s = env->exception.vaddress;
8303 qemu_log_mask(CPU_LOG_INT, "...with HDFAR 0x%x\n",
8304 (uint32_t)env->exception.vaddress);
8305 addr = 0x10;
8306 break;
8307 case EXCP_IRQ:
8308 addr = 0x18;
8309 break;
8310 case EXCP_FIQ:
8311 addr = 0x1c;
8312 break;
8313 case EXCP_HVC:
8314 addr = 0x08;
8315 break;
8316 case EXCP_HYP_TRAP:
8317 addr = 0x14;
9bbb4ef9 8318 break;
b9bc21ff
PM
8319 default:
8320 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
8321 }
8322
8323 if (cs->exception_index != EXCP_IRQ && cs->exception_index != EXCP_FIQ) {
2ed08180
PM
8324 if (!arm_feature(env, ARM_FEATURE_V8)) {
8325 /*
8326 * QEMU syndrome values are v8-style. v7 has the IL bit
8327 * UNK/SBZP for "field not valid" cases, where v8 uses RES1.
8328 * If this is a v7 CPU, squash the IL bit in those cases.
8329 */
8330 if (cs->exception_index == EXCP_PREFETCH_ABORT ||
8331 (cs->exception_index == EXCP_DATA_ABORT &&
8332 !(env->exception.syndrome & ARM_EL_ISV)) ||
8333 syn_get_ec(env->exception.syndrome) == EC_UNCATEGORIZED) {
8334 env->exception.syndrome &= ~ARM_EL_IL;
8335 }
8336 }
b9bc21ff
PM
8337 env->cp15.esr_el[2] = env->exception.syndrome;
8338 }
8339
8340 if (arm_current_el(env) != 2 && addr < 0x14) {
8341 addr = 0x14;
8342 }
8343
8344 mask = 0;
8345 if (!(env->cp15.scr_el3 & SCR_EA)) {
8346 mask |= CPSR_A;
8347 }
8348 if (!(env->cp15.scr_el3 & SCR_IRQ)) {
8349 mask |= CPSR_I;
8350 }
8351 if (!(env->cp15.scr_el3 & SCR_FIQ)) {
8352 mask |= CPSR_F;
8353 }
8354
8355 addr += env->cp15.hvbar;
8356
8357 take_aarch32_exception(env, ARM_CPU_MODE_HYP, mask, 0, addr);
8358}
8359
966f758c 8360static void arm_cpu_do_interrupt_aarch32(CPUState *cs)
b5ff1b31 8361{
97a8ea5a
AF
8362 ARMCPU *cpu = ARM_CPU(cs);
8363 CPUARMState *env = &cpu->env;
b5ff1b31
FB
8364 uint32_t addr;
8365 uint32_t mask;
8366 int new_mode;
8367 uint32_t offset;
16a906fd 8368 uint32_t moe;
b5ff1b31 8369
16a906fd 8370 /* If this is a debug exception we must update the DBGDSCR.MOE bits */
64b91e3f 8371 switch (syn_get_ec(env->exception.syndrome)) {
16a906fd
PM
8372 case EC_BREAKPOINT:
8373 case EC_BREAKPOINT_SAME_EL:
8374 moe = 1;
8375 break;
8376 case EC_WATCHPOINT:
8377 case EC_WATCHPOINT_SAME_EL:
8378 moe = 10;
8379 break;
8380 case EC_AA32_BKPT:
8381 moe = 3;
8382 break;
8383 case EC_VECTORCATCH:
8384 moe = 5;
8385 break;
8386 default:
8387 moe = 0;
8388 break;
8389 }
8390
8391 if (moe) {
8392 env->cp15.mdscr_el1 = deposit64(env->cp15.mdscr_el1, 2, 4, moe);
8393 }
8394
b9bc21ff
PM
8395 if (env->exception.target_el == 2) {
8396 arm_cpu_do_interrupt_aarch32_hyp(cs);
8397 return;
8398 }
8399
27103424 8400 switch (cs->exception_index) {
b5ff1b31
FB
8401 case EXCP_UDEF:
8402 new_mode = ARM_CPU_MODE_UND;
8403 addr = 0x04;
8404 mask = CPSR_I;
8405 if (env->thumb)
8406 offset = 2;
8407 else
8408 offset = 4;
8409 break;
8410 case EXCP_SWI:
8411 new_mode = ARM_CPU_MODE_SVC;
8412 addr = 0x08;
8413 mask = CPSR_I;
601d70b9 8414 /* The PC already points to the next instruction. */
b5ff1b31
FB
8415 offset = 0;
8416 break;
06c949e6 8417 case EXCP_BKPT:
9ee6e8bb
PB
8418 /* Fall through to prefetch abort. */
8419 case EXCP_PREFETCH_ABORT:
88ca1c2d 8420 A32_BANKED_CURRENT_REG_SET(env, ifsr, env->exception.fsr);
b848ce2b 8421 A32_BANKED_CURRENT_REG_SET(env, ifar, env->exception.vaddress);
3f1beaca 8422 qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x IFAR 0x%x\n",
88ca1c2d 8423 env->exception.fsr, (uint32_t)env->exception.vaddress);
b5ff1b31
FB
8424 new_mode = ARM_CPU_MODE_ABT;
8425 addr = 0x0c;
8426 mask = CPSR_A | CPSR_I;
8427 offset = 4;
8428 break;
8429 case EXCP_DATA_ABORT:
4a7e2d73 8430 A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr);
b848ce2b 8431 A32_BANKED_CURRENT_REG_SET(env, dfar, env->exception.vaddress);
3f1beaca 8432 qemu_log_mask(CPU_LOG_INT, "...with DFSR 0x%x DFAR 0x%x\n",
4a7e2d73 8433 env->exception.fsr,
6cd8a264 8434 (uint32_t)env->exception.vaddress);
b5ff1b31
FB
8435 new_mode = ARM_CPU_MODE_ABT;
8436 addr = 0x10;
8437 mask = CPSR_A | CPSR_I;
8438 offset = 8;
8439 break;
8440 case EXCP_IRQ:
8441 new_mode = ARM_CPU_MODE_IRQ;
8442 addr = 0x18;
8443 /* Disable IRQ and imprecise data aborts. */
8444 mask = CPSR_A | CPSR_I;
8445 offset = 4;
de38d23b
FA
8446 if (env->cp15.scr_el3 & SCR_IRQ) {
8447 /* IRQ routed to monitor mode */
8448 new_mode = ARM_CPU_MODE_MON;
8449 mask |= CPSR_F;
8450 }
b5ff1b31
FB
8451 break;
8452 case EXCP_FIQ:
8453 new_mode = ARM_CPU_MODE_FIQ;
8454 addr = 0x1c;
8455 /* Disable FIQ, IRQ and imprecise data aborts. */
8456 mask = CPSR_A | CPSR_I | CPSR_F;
de38d23b
FA
8457 if (env->cp15.scr_el3 & SCR_FIQ) {
8458 /* FIQ routed to monitor mode */
8459 new_mode = ARM_CPU_MODE_MON;
8460 }
b5ff1b31
FB
8461 offset = 4;
8462 break;
87a4b270
PM
8463 case EXCP_VIRQ:
8464 new_mode = ARM_CPU_MODE_IRQ;
8465 addr = 0x18;
8466 /* Disable IRQ and imprecise data aborts. */
8467 mask = CPSR_A | CPSR_I;
8468 offset = 4;
8469 break;
8470 case EXCP_VFIQ:
8471 new_mode = ARM_CPU_MODE_FIQ;
8472 addr = 0x1c;
8473 /* Disable FIQ, IRQ and imprecise data aborts. */
8474 mask = CPSR_A | CPSR_I | CPSR_F;
8475 offset = 4;
8476 break;
dbe9d163
FA
8477 case EXCP_SMC:
8478 new_mode = ARM_CPU_MODE_MON;
8479 addr = 0x08;
8480 mask = CPSR_A | CPSR_I | CPSR_F;
8481 offset = 0;
8482 break;
b5ff1b31 8483 default:
a47dddd7 8484 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
b5ff1b31
FB
8485 return; /* Never happens. Keep compiler happy. */
8486 }
e89e51a1
FA
8487
8488 if (new_mode == ARM_CPU_MODE_MON) {
8489 addr += env->cp15.mvbar;
137feaa9 8490 } else if (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_V) {
e89e51a1 8491 /* High vectors. When enabled, base address cannot be remapped. */
b5ff1b31 8492 addr += 0xffff0000;
8641136c
NR
8493 } else {
8494 /* ARM v7 architectures provide a vector base address register to remap
8495 * the interrupt vector table.
e89e51a1 8496 * This register is only followed in non-monitor mode, and is banked.
8641136c
NR
8497 * Note: only bits 31:5 are valid.
8498 */
fb6c91ba 8499 addr += A32_BANKED_CURRENT_REG_GET(env, vbar);
b5ff1b31 8500 }
dbe9d163
FA
8501
8502 if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON) {
8503 env->cp15.scr_el3 &= ~SCR_NS;
8504 }
8505
dea8378b 8506 take_aarch32_exception(env, new_mode, mask, offset, addr);
b5ff1b31
FB
8507}
8508
966f758c
PM
8509/* Handle exception entry to a target EL which is using AArch64 */
8510static void arm_cpu_do_interrupt_aarch64(CPUState *cs)
f3a9b694
PM
8511{
8512 ARMCPU *cpu = ARM_CPU(cs);
8513 CPUARMState *env = &cpu->env;
8514 unsigned int new_el = env->exception.target_el;
8515 target_ulong addr = env->cp15.vbar_el[new_el];
8516 unsigned int new_mode = aarch64_pstate_mode(new_el, true);
0ab5953b
RH
8517 unsigned int cur_el = arm_current_el(env);
8518
9a05f7b6
RH
8519 /*
8520 * Note that new_el can never be 0. If cur_el is 0, then
8521 * el0_a64 is is_a64(), else el0_a64 is ignored.
8522 */
8523 aarch64_sve_change_el(env, cur_el, new_el, is_a64(env));
f3a9b694 8524
0ab5953b 8525 if (cur_el < new_el) {
3d6f7617
PM
8526 /* Entry vector offset depends on whether the implemented EL
8527 * immediately lower than the target level is using AArch32 or AArch64
8528 */
8529 bool is_aa64;
8530
8531 switch (new_el) {
8532 case 3:
8533 is_aa64 = (env->cp15.scr_el3 & SCR_RW) != 0;
8534 break;
8535 case 2:
8536 is_aa64 = (env->cp15.hcr_el2 & HCR_RW) != 0;
8537 break;
8538 case 1:
8539 is_aa64 = is_a64(env);
8540 break;
8541 default:
8542 g_assert_not_reached();
8543 }
8544
8545 if (is_aa64) {
f3a9b694
PM
8546 addr += 0x400;
8547 } else {
8548 addr += 0x600;
8549 }
8550 } else if (pstate_read(env) & PSTATE_SP) {
8551 addr += 0x200;
8552 }
8553
f3a9b694
PM
8554 switch (cs->exception_index) {
8555 case EXCP_PREFETCH_ABORT:
8556 case EXCP_DATA_ABORT:
8557 env->cp15.far_el[new_el] = env->exception.vaddress;
8558 qemu_log_mask(CPU_LOG_INT, "...with FAR 0x%" PRIx64 "\n",
8559 env->cp15.far_el[new_el]);
8560 /* fall through */
8561 case EXCP_BKPT:
8562 case EXCP_UDEF:
8563 case EXCP_SWI:
8564 case EXCP_HVC:
8565 case EXCP_HYP_TRAP:
8566 case EXCP_SMC:
4be42f40
PM
8567 if (syn_get_ec(env->exception.syndrome) == EC_ADVSIMDFPACCESSTRAP) {
8568 /*
8569 * QEMU internal FP/SIMD syndromes from AArch32 include the
8570 * TA and coproc fields which are only exposed if the exception
8571 * is taken to AArch32 Hyp mode. Mask them out to get a valid
8572 * AArch64 format syndrome.
8573 */
8574 env->exception.syndrome &= ~MAKE_64BIT_MASK(0, 20);
8575 }
f3a9b694
PM
8576 env->cp15.esr_el[new_el] = env->exception.syndrome;
8577 break;
8578 case EXCP_IRQ:
8579 case EXCP_VIRQ:
8580 addr += 0x80;
8581 break;
8582 case EXCP_FIQ:
8583 case EXCP_VFIQ:
8584 addr += 0x100;
8585 break;
f3a9b694
PM
8586 default:
8587 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
8588 }
8589
8590 if (is_a64(env)) {
8591 env->banked_spsr[aarch64_banked_spsr_index(new_el)] = pstate_read(env);
8592 aarch64_save_sp(env, arm_current_el(env));
8593 env->elr_el[new_el] = env->pc;
8594 } else {
8595 env->banked_spsr[aarch64_banked_spsr_index(new_el)] = cpsr_read(env);
f3a9b694
PM
8596 env->elr_el[new_el] = env->regs[15];
8597
8598 aarch64_sync_32_to_64(env);
8599
8600 env->condexec_bits = 0;
8601 }
8602 qemu_log_mask(CPU_LOG_INT, "...with ELR 0x%" PRIx64 "\n",
8603 env->elr_el[new_el]);
8604
8605 pstate_write(env, PSTATE_DAIF | new_mode);
8606 env->aarch64 = 1;
8607 aarch64_restore_sp(env, new_el);
a8a79c7a 8608 helper_rebuild_hflags_a64(env, new_el);
f3a9b694
PM
8609
8610 env->pc = addr;
8611
8612 qemu_log_mask(CPU_LOG_INT, "...to EL%d PC 0x%" PRIx64 " PSTATE 0x%x\n",
8613 new_el, env->pc, pstate_read(env));
966f758c
PM
8614}
8615
ed6e6ba9
AB
8616/*
8617 * Do semihosting call and set the appropriate return value. All the
8618 * permission and validity checks have been done at translate time.
8619 *
8620 * We only see semihosting exceptions in TCG only as they are not
8621 * trapped to the hypervisor in KVM.
8622 */
91f78c58 8623#ifdef CONFIG_TCG
ed6e6ba9
AB
8624static void handle_semihosting(CPUState *cs)
8625{
904c04de
PM
8626 ARMCPU *cpu = ARM_CPU(cs);
8627 CPUARMState *env = &cpu->env;
8628
8629 if (is_a64(env)) {
ed6e6ba9
AB
8630 qemu_log_mask(CPU_LOG_INT,
8631 "...handling as semihosting call 0x%" PRIx64 "\n",
8632 env->xregs[0]);
8633 env->xregs[0] = do_arm_semihosting(env);
4ff5ef9e 8634 env->pc += 4;
904c04de 8635 } else {
904c04de
PM
8636 qemu_log_mask(CPU_LOG_INT,
8637 "...handling as semihosting call 0x%x\n",
8638 env->regs[0]);
8639 env->regs[0] = do_arm_semihosting(env);
4ff5ef9e 8640 env->regs[15] += env->thumb ? 2 : 4;
904c04de
PM
8641 }
8642}
ed6e6ba9 8643#endif
904c04de 8644
966f758c
PM
8645/* Handle a CPU exception for A and R profile CPUs.
8646 * Do any appropriate logging, handle PSCI calls, and then hand off
8647 * to the AArch64-entry or AArch32-entry function depending on the
8648 * target exception level's register width.
8649 */
8650void arm_cpu_do_interrupt(CPUState *cs)
8651{
8652 ARMCPU *cpu = ARM_CPU(cs);
8653 CPUARMState *env = &cpu->env;
8654 unsigned int new_el = env->exception.target_el;
8655
531c60a9 8656 assert(!arm_feature(env, ARM_FEATURE_M));
966f758c
PM
8657
8658 arm_log_exception(cs->exception_index);
8659 qemu_log_mask(CPU_LOG_INT, "...from EL%d to EL%d\n", arm_current_el(env),
8660 new_el);
8661 if (qemu_loglevel_mask(CPU_LOG_INT)
8662 && !excp_is_internal(cs->exception_index)) {
6568da45 8663 qemu_log_mask(CPU_LOG_INT, "...with ESR 0x%x/0x%" PRIx32 "\n",
64b91e3f 8664 syn_get_ec(env->exception.syndrome),
966f758c
PM
8665 env->exception.syndrome);
8666 }
8667
8668 if (arm_is_psci_call(cpu, cs->exception_index)) {
8669 arm_handle_psci_call(cpu);
8670 qemu_log_mask(CPU_LOG_INT, "...handled as PSCI call\n");
8671 return;
8672 }
8673
ed6e6ba9
AB
8674 /*
8675 * Semihosting semantics depend on the register width of the code
8676 * that caused the exception, not the target exception level, so
8677 * must be handled here.
966f758c 8678 */
ed6e6ba9
AB
8679#ifdef CONFIG_TCG
8680 if (cs->exception_index == EXCP_SEMIHOST) {
8681 handle_semihosting(cs);
904c04de
PM
8682 return;
8683 }
ed6e6ba9 8684#endif
904c04de 8685
b5c53d1b
AL
8686 /* Hooks may change global state so BQL should be held, also the
8687 * BQL needs to be held for any modification of
8688 * cs->interrupt_request.
8689 */
8690 g_assert(qemu_mutex_iothread_locked());
8691
8692 arm_call_pre_el_change_hook(cpu);
8693
904c04de
PM
8694 assert(!excp_is_internal(cs->exception_index));
8695 if (arm_el_is_aa64(env, new_el)) {
966f758c
PM
8696 arm_cpu_do_interrupt_aarch64(cs);
8697 } else {
8698 arm_cpu_do_interrupt_aarch32(cs);
8699 }
f3a9b694 8700
bd7d00fc
PM
8701 arm_call_el_change_hook(cpu);
8702
f3a9b694
PM
8703 if (!kvm_enabled()) {
8704 cs->interrupt_request |= CPU_INTERRUPT_EXITTB;
8705 }
8706}
c47eaf9f 8707#endif /* !CONFIG_USER_ONLY */
0480f69a
PM
8708
8709/* Return the exception level which controls this address translation regime */
b9f6033c 8710static uint32_t regime_el(CPUARMState *env, ARMMMUIdx mmu_idx)
0480f69a
PM
8711{
8712 switch (mmu_idx) {
b9f6033c
RH
8713 case ARMMMUIdx_E20_0:
8714 case ARMMMUIdx_E20_2:
97fa9350 8715 case ARMMMUIdx_Stage2:
e013b741 8716 case ARMMMUIdx_E2:
0480f69a 8717 return 2;
127b2b08 8718 case ARMMMUIdx_SE3:
0480f69a 8719 return 3;
fba37aed 8720 case ARMMMUIdx_SE10_0:
0480f69a 8721 return arm_el_is_aa64(env, 3) ? 1 : 3;
fba37aed 8722 case ARMMMUIdx_SE10_1:
2859d7b5
RH
8723 case ARMMMUIdx_Stage1_E0:
8724 case ARMMMUIdx_Stage1_E1:
b9f6033c
RH
8725 case ARMMMUIdx_E10_0:
8726 case ARMMMUIdx_E10_1:
62593718
PM
8727 case ARMMMUIdx_MPrivNegPri:
8728 case ARMMMUIdx_MUserNegPri:
e7b921c2
PM
8729 case ARMMMUIdx_MPriv:
8730 case ARMMMUIdx_MUser:
62593718
PM
8731 case ARMMMUIdx_MSPrivNegPri:
8732 case ARMMMUIdx_MSUserNegPri:
66787c78 8733 case ARMMMUIdx_MSPriv:
66787c78 8734 case ARMMMUIdx_MSUser:
0480f69a
PM
8735 return 1;
8736 default:
8737 g_assert_not_reached();
8738 }
8739}
8740
c47eaf9f
PM
8741#ifndef CONFIG_USER_ONLY
8742
0480f69a
PM
8743/* Return the SCTLR value which controls this address translation regime */
8744static inline uint32_t regime_sctlr(CPUARMState *env, ARMMMUIdx mmu_idx)
8745{
8746 return env->cp15.sctlr_el[regime_el(env, mmu_idx)];
8747}
8748
8749/* Return true if the specified stage of address translation is disabled */
8750static inline bool regime_translation_disabled(CPUARMState *env,
8751 ARMMMUIdx mmu_idx)
8752{
29c483a5 8753 if (arm_feature(env, ARM_FEATURE_M)) {
ecf5e8ea 8754 switch (env->v7m.mpu_ctrl[regime_is_secure(env, mmu_idx)] &
3bef7012
PM
8755 (R_V7M_MPU_CTRL_ENABLE_MASK | R_V7M_MPU_CTRL_HFNMIENA_MASK)) {
8756 case R_V7M_MPU_CTRL_ENABLE_MASK:
8757 /* Enabled, but not for HardFault and NMI */
62593718 8758 return mmu_idx & ARM_MMU_IDX_M_NEGPRI;
3bef7012
PM
8759 case R_V7M_MPU_CTRL_ENABLE_MASK | R_V7M_MPU_CTRL_HFNMIENA_MASK:
8760 /* Enabled for all cases */
8761 return false;
8762 case 0:
8763 default:
8764 /* HFNMIENA set and ENABLE clear is UNPREDICTABLE, but
8765 * we warned about that in armv7m_nvic.c when the guest set it.
8766 */
8767 return true;
8768 }
29c483a5
MD
8769 }
8770
97fa9350 8771 if (mmu_idx == ARMMMUIdx_Stage2) {
9d1bab33
PM
8772 /* HCR.DC means HCR.VM behaves as 1 */
8773 return (env->cp15.hcr_el2 & (HCR_DC | HCR_VM)) == 0;
0480f69a 8774 }
3d0e3080
PM
8775
8776 if (env->cp15.hcr_el2 & HCR_TGE) {
8777 /* TGE means that NS EL0/1 act as if SCTLR_EL1.M is zero */
8778 if (!regime_is_secure(env, mmu_idx) && regime_el(env, mmu_idx) == 1) {
8779 return true;
8780 }
8781 }
8782
9d1bab33 8783 if ((env->cp15.hcr_el2 & HCR_DC) &&
2859d7b5 8784 (mmu_idx == ARMMMUIdx_Stage1_E0 || mmu_idx == ARMMMUIdx_Stage1_E1)) {
9d1bab33
PM
8785 /* HCR.DC means SCTLR_EL1.M behaves as 0 */
8786 return true;
8787 }
8788
0480f69a
PM
8789 return (regime_sctlr(env, mmu_idx) & SCTLR_M) == 0;
8790}
8791
73462ddd
PC
8792static inline bool regime_translation_big_endian(CPUARMState *env,
8793 ARMMMUIdx mmu_idx)
8794{
8795 return (regime_sctlr(env, mmu_idx) & SCTLR_EE) != 0;
8796}
8797
c47eaf9f
PM
8798/* Return the TTBR associated with this translation regime */
8799static inline uint64_t regime_ttbr(CPUARMState *env, ARMMMUIdx mmu_idx,
8800 int ttbrn)
8801{
97fa9350 8802 if (mmu_idx == ARMMMUIdx_Stage2) {
c47eaf9f
PM
8803 return env->cp15.vttbr_el2;
8804 }
8805 if (ttbrn == 0) {
8806 return env->cp15.ttbr0_el[regime_el(env, mmu_idx)];
8807 } else {
8808 return env->cp15.ttbr1_el[regime_el(env, mmu_idx)];
8809 }
8810}
8811
8812#endif /* !CONFIG_USER_ONLY */
8813
0480f69a
PM
8814/* Return the TCR controlling this translation regime */
8815static inline TCR *regime_tcr(CPUARMState *env, ARMMMUIdx mmu_idx)
8816{
97fa9350 8817 if (mmu_idx == ARMMMUIdx_Stage2) {
68e9c2fe 8818 return &env->cp15.vtcr_el2;
0480f69a
PM
8819 }
8820 return &env->cp15.tcr_el[regime_el(env, mmu_idx)];
8821}
8822
8bd5c820
PM
8823/* Convert a possible stage1+2 MMU index into the appropriate
8824 * stage 1 MMU index
8825 */
8826static inline ARMMMUIdx stage_1_mmu_idx(ARMMMUIdx mmu_idx)
8827{
b9f6033c
RH
8828 switch (mmu_idx) {
8829 case ARMMMUIdx_E10_0:
8830 return ARMMMUIdx_Stage1_E0;
8831 case ARMMMUIdx_E10_1:
8832 return ARMMMUIdx_Stage1_E1;
8833 default:
8834 return mmu_idx;
8bd5c820 8835 }
8bd5c820
PM
8836}
8837
0480f69a
PM
8838/* Return true if the translation regime is using LPAE format page tables */
8839static inline bool regime_using_lpae_format(CPUARMState *env,
8840 ARMMMUIdx mmu_idx)
8841{
8842 int el = regime_el(env, mmu_idx);
8843 if (el == 2 || arm_el_is_aa64(env, el)) {
8844 return true;
8845 }
8846 if (arm_feature(env, ARM_FEATURE_LPAE)
8847 && (regime_tcr(env, mmu_idx)->raw_tcr & TTBCR_EAE)) {
8848 return true;
8849 }
8850 return false;
8851}
8852
deb2db99
AR
8853/* Returns true if the stage 1 translation regime is using LPAE format page
8854 * tables. Used when raising alignment exceptions, whose FSR changes depending
8855 * on whether the long or short descriptor format is in use. */
8856bool arm_s1_regime_using_lpae_format(CPUARMState *env, ARMMMUIdx mmu_idx)
30901475 8857{
8bd5c820 8858 mmu_idx = stage_1_mmu_idx(mmu_idx);
deb2db99 8859
30901475
AB
8860 return regime_using_lpae_format(env, mmu_idx);
8861}
8862
c47eaf9f 8863#ifndef CONFIG_USER_ONLY
0480f69a
PM
8864static inline bool regime_is_user(CPUARMState *env, ARMMMUIdx mmu_idx)
8865{
8866 switch (mmu_idx) {
fba37aed 8867 case ARMMMUIdx_SE10_0:
b9f6033c 8868 case ARMMMUIdx_E20_0:
2859d7b5 8869 case ARMMMUIdx_Stage1_E0:
e7b921c2 8870 case ARMMMUIdx_MUser:
871bec7c 8871 case ARMMMUIdx_MSUser:
62593718
PM
8872 case ARMMMUIdx_MUserNegPri:
8873 case ARMMMUIdx_MSUserNegPri:
0480f69a
PM
8874 return true;
8875 default:
8876 return false;
01b98b68
RH
8877 case ARMMMUIdx_E10_0:
8878 case ARMMMUIdx_E10_1:
0480f69a
PM
8879 g_assert_not_reached();
8880 }
8881}
8882
0fbf5238
AJ
8883/* Translate section/page access permissions to page
8884 * R/W protection flags
d76951b6
AJ
8885 *
8886 * @env: CPUARMState
8887 * @mmu_idx: MMU index indicating required translation regime
8888 * @ap: The 3-bit access permissions (AP[2:0])
8889 * @domain_prot: The 2-bit domain access permissions
0fbf5238
AJ
8890 */
8891static inline int ap_to_rw_prot(CPUARMState *env, ARMMMUIdx mmu_idx,
8892 int ap, int domain_prot)
8893{
554b0b09
PM
8894 bool is_user = regime_is_user(env, mmu_idx);
8895
8896 if (domain_prot == 3) {
8897 return PAGE_READ | PAGE_WRITE;
8898 }
8899
554b0b09
PM
8900 switch (ap) {
8901 case 0:
8902 if (arm_feature(env, ARM_FEATURE_V7)) {
8903 return 0;
8904 }
554b0b09
PM
8905 switch (regime_sctlr(env, mmu_idx) & (SCTLR_S | SCTLR_R)) {
8906 case SCTLR_S:
8907 return is_user ? 0 : PAGE_READ;
8908 case SCTLR_R:
8909 return PAGE_READ;
8910 default:
8911 return 0;
8912 }
8913 case 1:
8914 return is_user ? 0 : PAGE_READ | PAGE_WRITE;
8915 case 2:
87c3d486 8916 if (is_user) {
0fbf5238 8917 return PAGE_READ;
87c3d486 8918 } else {
554b0b09 8919 return PAGE_READ | PAGE_WRITE;
87c3d486 8920 }
554b0b09
PM
8921 case 3:
8922 return PAGE_READ | PAGE_WRITE;
8923 case 4: /* Reserved. */
8924 return 0;
8925 case 5:
0fbf5238 8926 return is_user ? 0 : PAGE_READ;
554b0b09 8927 case 6:
0fbf5238 8928 return PAGE_READ;
554b0b09 8929 case 7:
87c3d486 8930 if (!arm_feature(env, ARM_FEATURE_V6K)) {
554b0b09 8931 return 0;
87c3d486 8932 }
0fbf5238 8933 return PAGE_READ;
554b0b09 8934 default:
0fbf5238 8935 g_assert_not_reached();
554b0b09 8936 }
b5ff1b31
FB
8937}
8938
d76951b6
AJ
8939/* Translate section/page access permissions to page
8940 * R/W protection flags.
8941 *
d76951b6 8942 * @ap: The 2-bit simple AP (AP[2:1])
d8e052b3 8943 * @is_user: TRUE if accessing from PL0
d76951b6 8944 */
d8e052b3 8945static inline int simple_ap_to_rw_prot_is_user(int ap, bool is_user)
d76951b6 8946{
d76951b6
AJ
8947 switch (ap) {
8948 case 0:
8949 return is_user ? 0 : PAGE_READ | PAGE_WRITE;
8950 case 1:
8951 return PAGE_READ | PAGE_WRITE;
8952 case 2:
8953 return is_user ? 0 : PAGE_READ;
8954 case 3:
8955 return PAGE_READ;
8956 default:
8957 g_assert_not_reached();
8958 }
8959}
8960
d8e052b3
AJ
8961static inline int
8962simple_ap_to_rw_prot(CPUARMState *env, ARMMMUIdx mmu_idx, int ap)
8963{
8964 return simple_ap_to_rw_prot_is_user(ap, regime_is_user(env, mmu_idx));
8965}
8966
6ab1a5ee
EI
8967/* Translate S2 section/page access permissions to protection flags
8968 *
8969 * @env: CPUARMState
8970 * @s2ap: The 2-bit stage2 access permissions (S2AP)
8971 * @xn: XN (execute-never) bit
8972 */
8973static int get_S2prot(CPUARMState *env, int s2ap, int xn)
8974{
8975 int prot = 0;
8976
8977 if (s2ap & 1) {
8978 prot |= PAGE_READ;
8979 }
8980 if (s2ap & 2) {
8981 prot |= PAGE_WRITE;
8982 }
8983 if (!xn) {
dfda6837
SS
8984 if (arm_el_is_aa64(env, 2) || prot & PAGE_READ) {
8985 prot |= PAGE_EXEC;
8986 }
6ab1a5ee
EI
8987 }
8988 return prot;
8989}
8990
d8e052b3
AJ
8991/* Translate section/page access permissions to protection flags
8992 *
8993 * @env: CPUARMState
8994 * @mmu_idx: MMU index indicating required translation regime
8995 * @is_aa64: TRUE if AArch64
8996 * @ap: The 2-bit simple AP (AP[2:1])
8997 * @ns: NS (non-secure) bit
8998 * @xn: XN (execute-never) bit
8999 * @pxn: PXN (privileged execute-never) bit
9000 */
9001static int get_S1prot(CPUARMState *env, ARMMMUIdx mmu_idx, bool is_aa64,
9002 int ap, int ns, int xn, int pxn)
9003{
9004 bool is_user = regime_is_user(env, mmu_idx);
9005 int prot_rw, user_rw;
9006 bool have_wxn;
9007 int wxn = 0;
9008
97fa9350 9009 assert(mmu_idx != ARMMMUIdx_Stage2);
d8e052b3
AJ
9010
9011 user_rw = simple_ap_to_rw_prot_is_user(ap, true);
9012 if (is_user) {
9013 prot_rw = user_rw;
9014 } else {
9015 prot_rw = simple_ap_to_rw_prot_is_user(ap, false);
9016 }
9017
9018 if (ns && arm_is_secure(env) && (env->cp15.scr_el3 & SCR_SIF)) {
9019 return prot_rw;
9020 }
9021
9022 /* TODO have_wxn should be replaced with
9023 * ARM_FEATURE_V8 || (ARM_FEATURE_V7 && ARM_FEATURE_EL2)
9024 * when ARM_FEATURE_EL2 starts getting set. For now we assume all LPAE
9025 * compatible processors have EL2, which is required for [U]WXN.
9026 */
9027 have_wxn = arm_feature(env, ARM_FEATURE_LPAE);
9028
9029 if (have_wxn) {
9030 wxn = regime_sctlr(env, mmu_idx) & SCTLR_WXN;
9031 }
9032
9033 if (is_aa64) {
9034 switch (regime_el(env, mmu_idx)) {
9035 case 1:
9036 if (!is_user) {
9037 xn = pxn || (user_rw & PAGE_WRITE);
9038 }
9039 break;
9040 case 2:
9041 case 3:
9042 break;
9043 }
9044 } else if (arm_feature(env, ARM_FEATURE_V7)) {
9045 switch (regime_el(env, mmu_idx)) {
9046 case 1:
9047 case 3:
9048 if (is_user) {
9049 xn = xn || !(user_rw & PAGE_READ);
9050 } else {
9051 int uwxn = 0;
9052 if (have_wxn) {
9053 uwxn = regime_sctlr(env, mmu_idx) & SCTLR_UWXN;
9054 }
9055 xn = xn || !(prot_rw & PAGE_READ) || pxn ||
9056 (uwxn && (user_rw & PAGE_WRITE));
9057 }
9058 break;
9059 case 2:
9060 break;
9061 }
9062 } else {
9063 xn = wxn = 0;
9064 }
9065
9066 if (xn || (wxn && (prot_rw & PAGE_WRITE))) {
9067 return prot_rw;
9068 }
9069 return prot_rw | PAGE_EXEC;
9070}
9071
0480f69a
PM
9072static bool get_level1_table_address(CPUARMState *env, ARMMMUIdx mmu_idx,
9073 uint32_t *table, uint32_t address)
b2fa1797 9074{
0480f69a 9075 /* Note that we can only get here for an AArch32 PL0/PL1 lookup */
0480f69a 9076 TCR *tcr = regime_tcr(env, mmu_idx);
11f136ee 9077
11f136ee
FA
9078 if (address & tcr->mask) {
9079 if (tcr->raw_tcr & TTBCR_PD1) {
e389be16
FA
9080 /* Translation table walk disabled for TTBR1 */
9081 return false;
9082 }
aef878be 9083 *table = regime_ttbr(env, mmu_idx, 1) & 0xffffc000;
e389be16 9084 } else {
11f136ee 9085 if (tcr->raw_tcr & TTBCR_PD0) {
e389be16
FA
9086 /* Translation table walk disabled for TTBR0 */
9087 return false;
9088 }
aef878be 9089 *table = regime_ttbr(env, mmu_idx, 0) & tcr->base_mask;
e389be16
FA
9090 }
9091 *table |= (address >> 18) & 0x3ffc;
9092 return true;
b2fa1797
PB
9093}
9094
37785977
EI
9095/* Translate a S1 pagetable walk through S2 if needed. */
9096static hwaddr S1_ptw_translate(CPUARMState *env, ARMMMUIdx mmu_idx,
9097 hwaddr addr, MemTxAttrs txattrs,
37785977
EI
9098 ARMMMUFaultInfo *fi)
9099{
2859d7b5 9100 if ((mmu_idx == ARMMMUIdx_Stage1_E0 || mmu_idx == ARMMMUIdx_Stage1_E1) &&
97fa9350 9101 !regime_translation_disabled(env, ARMMMUIdx_Stage2)) {
37785977
EI
9102 target_ulong s2size;
9103 hwaddr s2pa;
9104 int s2prot;
9105 int ret;
eadb2feb
PM
9106 ARMCacheAttrs cacheattrs = {};
9107 ARMCacheAttrs *pcacheattrs = NULL;
9108
9109 if (env->cp15.hcr_el2 & HCR_PTW) {
9110 /*
9111 * PTW means we must fault if this S1 walk touches S2 Device
9112 * memory; otherwise we don't care about the attributes and can
9113 * save the S2 translation the effort of computing them.
9114 */
9115 pcacheattrs = &cacheattrs;
9116 }
37785977 9117
97fa9350 9118 ret = get_phys_addr_lpae(env, addr, 0, ARMMMUIdx_Stage2, &s2pa,
eadb2feb 9119 &txattrs, &s2prot, &s2size, fi, pcacheattrs);
37785977 9120 if (ret) {
3b39d734 9121 assert(fi->type != ARMFault_None);
37785977
EI
9122 fi->s2addr = addr;
9123 fi->stage2 = true;
9124 fi->s1ptw = true;
9125 return ~0;
9126 }
eadb2feb
PM
9127 if (pcacheattrs && (pcacheattrs->attrs & 0xf0) == 0) {
9128 /* Access was to Device memory: generate Permission fault */
9129 fi->type = ARMFault_Permission;
9130 fi->s2addr = addr;
9131 fi->stage2 = true;
9132 fi->s1ptw = true;
9133 return ~0;
9134 }
37785977
EI
9135 addr = s2pa;
9136 }
9137 return addr;
9138}
9139
14577270 9140/* All loads done in the course of a page table walk go through here. */
a614e698 9141static uint32_t arm_ldl_ptw(CPUState *cs, hwaddr addr, bool is_secure,
3795a6de 9142 ARMMMUIdx mmu_idx, ARMMMUFaultInfo *fi)
ebca90e4 9143{
a614e698
EI
9144 ARMCPU *cpu = ARM_CPU(cs);
9145 CPUARMState *env = &cpu->env;
ebca90e4 9146 MemTxAttrs attrs = {};
3b39d734 9147 MemTxResult result = MEMTX_OK;
5ce4ff65 9148 AddressSpace *as;
3b39d734 9149 uint32_t data;
ebca90e4
PM
9150
9151 attrs.secure = is_secure;
5ce4ff65 9152 as = arm_addressspace(cs, attrs);
3795a6de 9153 addr = S1_ptw_translate(env, mmu_idx, addr, attrs, fi);
a614e698
EI
9154 if (fi->s1ptw) {
9155 return 0;
9156 }
73462ddd 9157 if (regime_translation_big_endian(env, mmu_idx)) {
3b39d734 9158 data = address_space_ldl_be(as, addr, attrs, &result);
73462ddd 9159 } else {
3b39d734 9160 data = address_space_ldl_le(as, addr, attrs, &result);
73462ddd 9161 }
3b39d734
PM
9162 if (result == MEMTX_OK) {
9163 return data;
9164 }
9165 fi->type = ARMFault_SyncExternalOnWalk;
9166 fi->ea = arm_extabort_type(result);
9167 return 0;
ebca90e4
PM
9168}
9169
37785977 9170static uint64_t arm_ldq_ptw(CPUState *cs, hwaddr addr, bool is_secure,
3795a6de 9171 ARMMMUIdx mmu_idx, ARMMMUFaultInfo *fi)
ebca90e4 9172{
37785977
EI
9173 ARMCPU *cpu = ARM_CPU(cs);
9174 CPUARMState *env = &cpu->env;
ebca90e4 9175 MemTxAttrs attrs = {};
3b39d734 9176 MemTxResult result = MEMTX_OK;
5ce4ff65 9177 AddressSpace *as;
9aea1ea3 9178 uint64_t data;
ebca90e4
PM
9179
9180 attrs.secure = is_secure;
5ce4ff65 9181 as = arm_addressspace(cs, attrs);
3795a6de 9182 addr = S1_ptw_translate(env, mmu_idx, addr, attrs, fi);
37785977
EI
9183 if (fi->s1ptw) {
9184 return 0;
9185 }
73462ddd 9186 if (regime_translation_big_endian(env, mmu_idx)) {
3b39d734 9187 data = address_space_ldq_be(as, addr, attrs, &result);
73462ddd 9188 } else {
3b39d734
PM
9189 data = address_space_ldq_le(as, addr, attrs, &result);
9190 }
9191 if (result == MEMTX_OK) {
9192 return data;
73462ddd 9193 }
3b39d734
PM
9194 fi->type = ARMFault_SyncExternalOnWalk;
9195 fi->ea = arm_extabort_type(result);
9196 return 0;
ebca90e4
PM
9197}
9198
b7cc4e82 9199static bool get_phys_addr_v5(CPUARMState *env, uint32_t address,
03ae85f8 9200 MMUAccessType access_type, ARMMMUIdx mmu_idx,
b7cc4e82 9201 hwaddr *phys_ptr, int *prot,
f989983e 9202 target_ulong *page_size,
e14b5a23 9203 ARMMMUFaultInfo *fi)
b5ff1b31 9204{
2fc0cc0e 9205 CPUState *cs = env_cpu(env);
f989983e 9206 int level = 1;
b5ff1b31
FB
9207 uint32_t table;
9208 uint32_t desc;
9209 int type;
9210 int ap;
e389be16 9211 int domain = 0;
dd4ebc2e 9212 int domain_prot;
a8170e5e 9213 hwaddr phys_addr;
0480f69a 9214 uint32_t dacr;
b5ff1b31 9215
9ee6e8bb
PB
9216 /* Pagetable walk. */
9217 /* Lookup l1 descriptor. */
0480f69a 9218 if (!get_level1_table_address(env, mmu_idx, &table, address)) {
e389be16 9219 /* Section translation fault if page walk is disabled by PD0 or PD1 */
f989983e 9220 fi->type = ARMFault_Translation;
e389be16
FA
9221 goto do_fault;
9222 }
a614e698 9223 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
3795a6de 9224 mmu_idx, fi);
3b39d734
PM
9225 if (fi->type != ARMFault_None) {
9226 goto do_fault;
9227 }
9ee6e8bb 9228 type = (desc & 3);
dd4ebc2e 9229 domain = (desc >> 5) & 0x0f;
0480f69a
PM
9230 if (regime_el(env, mmu_idx) == 1) {
9231 dacr = env->cp15.dacr_ns;
9232 } else {
9233 dacr = env->cp15.dacr_s;
9234 }
9235 domain_prot = (dacr >> (domain * 2)) & 3;
9ee6e8bb 9236 if (type == 0) {
601d70b9 9237 /* Section translation fault. */
f989983e 9238 fi->type = ARMFault_Translation;
9ee6e8bb
PB
9239 goto do_fault;
9240 }
f989983e
PM
9241 if (type != 2) {
9242 level = 2;
9243 }
dd4ebc2e 9244 if (domain_prot == 0 || domain_prot == 2) {
f989983e 9245 fi->type = ARMFault_Domain;
9ee6e8bb
PB
9246 goto do_fault;
9247 }
9248 if (type == 2) {
9249 /* 1Mb section. */
9250 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
9251 ap = (desc >> 10) & 3;
d4c430a8 9252 *page_size = 1024 * 1024;
9ee6e8bb
PB
9253 } else {
9254 /* Lookup l2 entry. */
554b0b09
PM
9255 if (type == 1) {
9256 /* Coarse pagetable. */
9257 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
9258 } else {
9259 /* Fine pagetable. */
9260 table = (desc & 0xfffff000) | ((address >> 8) & 0xffc);
9261 }
a614e698 9262 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
3795a6de 9263 mmu_idx, fi);
3b39d734
PM
9264 if (fi->type != ARMFault_None) {
9265 goto do_fault;
9266 }
9ee6e8bb
PB
9267 switch (desc & 3) {
9268 case 0: /* Page translation fault. */
f989983e 9269 fi->type = ARMFault_Translation;
9ee6e8bb
PB
9270 goto do_fault;
9271 case 1: /* 64k page. */
9272 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
9273 ap = (desc >> (4 + ((address >> 13) & 6))) & 3;
d4c430a8 9274 *page_size = 0x10000;
ce819861 9275 break;
9ee6e8bb
PB
9276 case 2: /* 4k page. */
9277 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
c10f7fc3 9278 ap = (desc >> (4 + ((address >> 9) & 6))) & 3;
d4c430a8 9279 *page_size = 0x1000;
ce819861 9280 break;
fc1891c7 9281 case 3: /* 1k page, or ARMv6/XScale "extended small (4k) page" */
554b0b09 9282 if (type == 1) {
fc1891c7
PM
9283 /* ARMv6/XScale extended small page format */
9284 if (arm_feature(env, ARM_FEATURE_XSCALE)
9285 || arm_feature(env, ARM_FEATURE_V6)) {
554b0b09 9286 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
fc1891c7 9287 *page_size = 0x1000;
554b0b09 9288 } else {
fc1891c7
PM
9289 /* UNPREDICTABLE in ARMv5; we choose to take a
9290 * page translation fault.
9291 */
f989983e 9292 fi->type = ARMFault_Translation;
554b0b09
PM
9293 goto do_fault;
9294 }
9295 } else {
9296 phys_addr = (desc & 0xfffffc00) | (address & 0x3ff);
fc1891c7 9297 *page_size = 0x400;
554b0b09 9298 }
9ee6e8bb 9299 ap = (desc >> 4) & 3;
ce819861
PB
9300 break;
9301 default:
9ee6e8bb
PB
9302 /* Never happens, but compiler isn't smart enough to tell. */
9303 abort();
ce819861 9304 }
9ee6e8bb 9305 }
0fbf5238
AJ
9306 *prot = ap_to_rw_prot(env, mmu_idx, ap, domain_prot);
9307 *prot |= *prot ? PAGE_EXEC : 0;
9308 if (!(*prot & (1 << access_type))) {
9ee6e8bb 9309 /* Access permission fault. */
f989983e 9310 fi->type = ARMFault_Permission;
9ee6e8bb
PB
9311 goto do_fault;
9312 }
9313 *phys_ptr = phys_addr;
b7cc4e82 9314 return false;
9ee6e8bb 9315do_fault:
f989983e
PM
9316 fi->domain = domain;
9317 fi->level = level;
b7cc4e82 9318 return true;
9ee6e8bb
PB
9319}
9320
b7cc4e82 9321static bool get_phys_addr_v6(CPUARMState *env, uint32_t address,
03ae85f8 9322 MMUAccessType access_type, ARMMMUIdx mmu_idx,
b7cc4e82 9323 hwaddr *phys_ptr, MemTxAttrs *attrs, int *prot,
f06cf243 9324 target_ulong *page_size, ARMMMUFaultInfo *fi)
9ee6e8bb 9325{
2fc0cc0e 9326 CPUState *cs = env_cpu(env);
f06cf243 9327 int level = 1;
9ee6e8bb
PB
9328 uint32_t table;
9329 uint32_t desc;
9330 uint32_t xn;
de9b05b8 9331 uint32_t pxn = 0;
9ee6e8bb
PB
9332 int type;
9333 int ap;
de9b05b8 9334 int domain = 0;
dd4ebc2e 9335 int domain_prot;
a8170e5e 9336 hwaddr phys_addr;
0480f69a 9337 uint32_t dacr;
8bf5b6a9 9338 bool ns;
9ee6e8bb
PB
9339
9340 /* Pagetable walk. */
9341 /* Lookup l1 descriptor. */
0480f69a 9342 if (!get_level1_table_address(env, mmu_idx, &table, address)) {
e389be16 9343 /* Section translation fault if page walk is disabled by PD0 or PD1 */
f06cf243 9344 fi->type = ARMFault_Translation;
e389be16
FA
9345 goto do_fault;
9346 }
a614e698 9347 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
3795a6de 9348 mmu_idx, fi);
3b39d734
PM
9349 if (fi->type != ARMFault_None) {
9350 goto do_fault;
9351 }
9ee6e8bb 9352 type = (desc & 3);
de9b05b8
PM
9353 if (type == 0 || (type == 3 && !arm_feature(env, ARM_FEATURE_PXN))) {
9354 /* Section translation fault, or attempt to use the encoding
9355 * which is Reserved on implementations without PXN.
9356 */
f06cf243 9357 fi->type = ARMFault_Translation;
9ee6e8bb 9358 goto do_fault;
de9b05b8
PM
9359 }
9360 if ((type == 1) || !(desc & (1 << 18))) {
9361 /* Page or Section. */
dd4ebc2e 9362 domain = (desc >> 5) & 0x0f;
9ee6e8bb 9363 }
0480f69a
PM
9364 if (regime_el(env, mmu_idx) == 1) {
9365 dacr = env->cp15.dacr_ns;
9366 } else {
9367 dacr = env->cp15.dacr_s;
9368 }
f06cf243
PM
9369 if (type == 1) {
9370 level = 2;
9371 }
0480f69a 9372 domain_prot = (dacr >> (domain * 2)) & 3;
dd4ebc2e 9373 if (domain_prot == 0 || domain_prot == 2) {
f06cf243
PM
9374 /* Section or Page domain fault */
9375 fi->type = ARMFault_Domain;
9ee6e8bb
PB
9376 goto do_fault;
9377 }
de9b05b8 9378 if (type != 1) {
9ee6e8bb
PB
9379 if (desc & (1 << 18)) {
9380 /* Supersection. */
9381 phys_addr = (desc & 0xff000000) | (address & 0x00ffffff);
4e42a6ca
SF
9382 phys_addr |= (uint64_t)extract32(desc, 20, 4) << 32;
9383 phys_addr |= (uint64_t)extract32(desc, 5, 4) << 36;
d4c430a8 9384 *page_size = 0x1000000;
b5ff1b31 9385 } else {
9ee6e8bb
PB
9386 /* Section. */
9387 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
d4c430a8 9388 *page_size = 0x100000;
b5ff1b31 9389 }
9ee6e8bb
PB
9390 ap = ((desc >> 10) & 3) | ((desc >> 13) & 4);
9391 xn = desc & (1 << 4);
de9b05b8 9392 pxn = desc & 1;
8bf5b6a9 9393 ns = extract32(desc, 19, 1);
9ee6e8bb 9394 } else {
de9b05b8
PM
9395 if (arm_feature(env, ARM_FEATURE_PXN)) {
9396 pxn = (desc >> 2) & 1;
9397 }
8bf5b6a9 9398 ns = extract32(desc, 3, 1);
9ee6e8bb
PB
9399 /* Lookup l2 entry. */
9400 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
a614e698 9401 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
3795a6de 9402 mmu_idx, fi);
3b39d734
PM
9403 if (fi->type != ARMFault_None) {
9404 goto do_fault;
9405 }
9ee6e8bb
PB
9406 ap = ((desc >> 4) & 3) | ((desc >> 7) & 4);
9407 switch (desc & 3) {
9408 case 0: /* Page translation fault. */
f06cf243 9409 fi->type = ARMFault_Translation;
b5ff1b31 9410 goto do_fault;
9ee6e8bb
PB
9411 case 1: /* 64k page. */
9412 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
9413 xn = desc & (1 << 15);
d4c430a8 9414 *page_size = 0x10000;
9ee6e8bb
PB
9415 break;
9416 case 2: case 3: /* 4k page. */
9417 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
9418 xn = desc & 1;
d4c430a8 9419 *page_size = 0x1000;
9ee6e8bb
PB
9420 break;
9421 default:
9422 /* Never happens, but compiler isn't smart enough to tell. */
9423 abort();
b5ff1b31 9424 }
9ee6e8bb 9425 }
dd4ebc2e 9426 if (domain_prot == 3) {
c0034328
JR
9427 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
9428 } else {
0480f69a 9429 if (pxn && !regime_is_user(env, mmu_idx)) {
de9b05b8
PM
9430 xn = 1;
9431 }
f06cf243
PM
9432 if (xn && access_type == MMU_INST_FETCH) {
9433 fi->type = ARMFault_Permission;
c0034328 9434 goto do_fault;
f06cf243 9435 }
9ee6e8bb 9436
d76951b6
AJ
9437 if (arm_feature(env, ARM_FEATURE_V6K) &&
9438 (regime_sctlr(env, mmu_idx) & SCTLR_AFE)) {
9439 /* The simplified model uses AP[0] as an access control bit. */
9440 if ((ap & 1) == 0) {
9441 /* Access flag fault. */
f06cf243 9442 fi->type = ARMFault_AccessFlag;
d76951b6
AJ
9443 goto do_fault;
9444 }
9445 *prot = simple_ap_to_rw_prot(env, mmu_idx, ap >> 1);
9446 } else {
9447 *prot = ap_to_rw_prot(env, mmu_idx, ap, domain_prot);
c0034328 9448 }
0fbf5238
AJ
9449 if (*prot && !xn) {
9450 *prot |= PAGE_EXEC;
9451 }
9452 if (!(*prot & (1 << access_type))) {
c0034328 9453 /* Access permission fault. */
f06cf243 9454 fi->type = ARMFault_Permission;
c0034328
JR
9455 goto do_fault;
9456 }
3ad493fc 9457 }
8bf5b6a9
PM
9458 if (ns) {
9459 /* The NS bit will (as required by the architecture) have no effect if
9460 * the CPU doesn't support TZ or this is a non-secure translation
9461 * regime, because the attribute will already be non-secure.
9462 */
9463 attrs->secure = false;
9464 }
9ee6e8bb 9465 *phys_ptr = phys_addr;
b7cc4e82 9466 return false;
b5ff1b31 9467do_fault:
f06cf243
PM
9468 fi->domain = domain;
9469 fi->level = level;
b7cc4e82 9470 return true;
b5ff1b31
FB
9471}
9472
1853d5a9 9473/*
a0e966c9 9474 * check_s2_mmu_setup
1853d5a9
EI
9475 * @cpu: ARMCPU
9476 * @is_aa64: True if the translation regime is in AArch64 state
9477 * @startlevel: Suggested starting level
9478 * @inputsize: Bitsize of IPAs
9479 * @stride: Page-table stride (See the ARM ARM)
9480 *
a0e966c9
EI
9481 * Returns true if the suggested S2 translation parameters are OK and
9482 * false otherwise.
1853d5a9 9483 */
a0e966c9
EI
9484static bool check_s2_mmu_setup(ARMCPU *cpu, bool is_aa64, int level,
9485 int inputsize, int stride)
1853d5a9 9486{
98d68ec2
EI
9487 const int grainsize = stride + 3;
9488 int startsizecheck;
9489
1853d5a9
EI
9490 /* Negative levels are never allowed. */
9491 if (level < 0) {
9492 return false;
9493 }
9494
98d68ec2
EI
9495 startsizecheck = inputsize - ((3 - level) * stride + grainsize);
9496 if (startsizecheck < 1 || startsizecheck > stride + 4) {
9497 return false;
9498 }
9499
1853d5a9 9500 if (is_aa64) {
3526423e 9501 CPUARMState *env = &cpu->env;
1853d5a9
EI
9502 unsigned int pamax = arm_pamax(cpu);
9503
9504 switch (stride) {
9505 case 13: /* 64KB Pages. */
9506 if (level == 0 || (level == 1 && pamax <= 42)) {
9507 return false;
9508 }
9509 break;
9510 case 11: /* 16KB Pages. */
9511 if (level == 0 || (level == 1 && pamax <= 40)) {
9512 return false;
9513 }
9514 break;
9515 case 9: /* 4KB Pages. */
9516 if (level == 0 && pamax <= 42) {
9517 return false;
9518 }
9519 break;
9520 default:
9521 g_assert_not_reached();
9522 }
3526423e
EI
9523
9524 /* Inputsize checks. */
9525 if (inputsize > pamax &&
9526 (arm_el_is_aa64(env, 1) || inputsize > 40)) {
9527 /* This is CONSTRAINED UNPREDICTABLE and we choose to fault. */
9528 return false;
9529 }
1853d5a9 9530 } else {
1853d5a9
EI
9531 /* AArch32 only supports 4KB pages. Assert on that. */
9532 assert(stride == 9);
9533
9534 if (level == 0) {
9535 return false;
9536 }
1853d5a9
EI
9537 }
9538 return true;
9539}
9540
5b2d261d
AB
9541/* Translate from the 4-bit stage 2 representation of
9542 * memory attributes (without cache-allocation hints) to
9543 * the 8-bit representation of the stage 1 MAIR registers
9544 * (which includes allocation hints).
9545 *
9546 * ref: shared/translation/attrs/S2AttrDecode()
9547 * .../S2ConvertAttrsHints()
9548 */
9549static uint8_t convert_stage2_attrs(CPUARMState *env, uint8_t s2attrs)
9550{
9551 uint8_t hiattr = extract32(s2attrs, 2, 2);
9552 uint8_t loattr = extract32(s2attrs, 0, 2);
9553 uint8_t hihint = 0, lohint = 0;
9554
9555 if (hiattr != 0) { /* normal memory */
9556 if ((env->cp15.hcr_el2 & HCR_CD) != 0) { /* cache disabled */
9557 hiattr = loattr = 1; /* non-cacheable */
9558 } else {
9559 if (hiattr != 1) { /* Write-through or write-back */
9560 hihint = 3; /* RW allocate */
9561 }
9562 if (loattr != 1) { /* Write-through or write-back */
9563 lohint = 3; /* RW allocate */
9564 }
9565 }
9566 }
9567
9568 return (hiattr << 6) | (hihint << 4) | (loattr << 2) | lohint;
9569}
c47eaf9f 9570#endif /* !CONFIG_USER_ONLY */
5b2d261d 9571
e737ed2a
RH
9572ARMVAParameters aa64_va_parameters_both(CPUARMState *env, uint64_t va,
9573 ARMMMUIdx mmu_idx)
ba97be9f
RH
9574{
9575 uint64_t tcr = regime_tcr(env, mmu_idx)->raw_tcr;
9576 uint32_t el = regime_el(env, mmu_idx);
8220af7e 9577 bool tbi, tbid, epd, hpd, using16k, using64k;
ba97be9f
RH
9578 int select, tsz;
9579
9580 /*
9581 * Bit 55 is always between the two regions, and is canonical for
9582 * determining if address tagging is enabled.
9583 */
9584 select = extract64(va, 55, 1);
9585
9586 if (el > 1) {
9587 tsz = extract32(tcr, 0, 6);
9588 using64k = extract32(tcr, 14, 1);
9589 using16k = extract32(tcr, 15, 1);
97fa9350 9590 if (mmu_idx == ARMMMUIdx_Stage2) {
ba97be9f 9591 /* VTCR_EL2 */
8220af7e 9592 tbi = tbid = hpd = false;
ba97be9f
RH
9593 } else {
9594 tbi = extract32(tcr, 20, 1);
9595 hpd = extract32(tcr, 24, 1);
8220af7e 9596 tbid = extract32(tcr, 29, 1);
ba97be9f
RH
9597 }
9598 epd = false;
9599 } else if (!select) {
9600 tsz = extract32(tcr, 0, 6);
9601 epd = extract32(tcr, 7, 1);
9602 using64k = extract32(tcr, 14, 1);
9603 using16k = extract32(tcr, 15, 1);
9604 tbi = extract64(tcr, 37, 1);
9605 hpd = extract64(tcr, 41, 1);
8220af7e 9606 tbid = extract64(tcr, 51, 1);
ba97be9f
RH
9607 } else {
9608 int tg = extract32(tcr, 30, 2);
9609 using16k = tg == 1;
9610 using64k = tg == 3;
9611 tsz = extract32(tcr, 16, 6);
9612 epd = extract32(tcr, 23, 1);
9613 tbi = extract64(tcr, 38, 1);
9614 hpd = extract64(tcr, 42, 1);
8220af7e 9615 tbid = extract64(tcr, 52, 1);
ba97be9f
RH
9616 }
9617 tsz = MIN(tsz, 39); /* TODO: ARMv8.4-TTST */
9618 tsz = MAX(tsz, 16); /* TODO: ARMv8.2-LVA */
9619
9620 return (ARMVAParameters) {
9621 .tsz = tsz,
9622 .select = select,
9623 .tbi = tbi,
8220af7e 9624 .tbid = tbid,
ba97be9f
RH
9625 .epd = epd,
9626 .hpd = hpd,
9627 .using16k = using16k,
9628 .using64k = using64k,
9629 };
9630}
9631
e737ed2a
RH
9632ARMVAParameters aa64_va_parameters(CPUARMState *env, uint64_t va,
9633 ARMMMUIdx mmu_idx, bool data)
9634{
8220af7e
RH
9635 ARMVAParameters ret = aa64_va_parameters_both(env, va, mmu_idx);
9636
9637 /* Present TBI as a composite with TBID. */
9638 ret.tbi &= (data || !ret.tbid);
9639 return ret;
e737ed2a
RH
9640}
9641
c47eaf9f 9642#ifndef CONFIG_USER_ONLY
ba97be9f
RH
9643static ARMVAParameters aa32_va_parameters(CPUARMState *env, uint32_t va,
9644 ARMMMUIdx mmu_idx)
9645{
9646 uint64_t tcr = regime_tcr(env, mmu_idx)->raw_tcr;
9647 uint32_t el = regime_el(env, mmu_idx);
9648 int select, tsz;
9649 bool epd, hpd;
9650
97fa9350 9651 if (mmu_idx == ARMMMUIdx_Stage2) {
ba97be9f
RH
9652 /* VTCR */
9653 bool sext = extract32(tcr, 4, 1);
9654 bool sign = extract32(tcr, 3, 1);
9655
9656 /*
9657 * If the sign-extend bit is not the same as t0sz[3], the result
9658 * is unpredictable. Flag this as a guest error.
9659 */
9660 if (sign != sext) {
9661 qemu_log_mask(LOG_GUEST_ERROR,
9662 "AArch32: VTCR.S / VTCR.T0SZ[3] mismatch\n");
9663 }
9664 tsz = sextract32(tcr, 0, 4) + 8;
9665 select = 0;
9666 hpd = false;
9667 epd = false;
9668 } else if (el == 2) {
9669 /* HTCR */
9670 tsz = extract32(tcr, 0, 3);
9671 select = 0;
9672 hpd = extract64(tcr, 24, 1);
9673 epd = false;
9674 } else {
9675 int t0sz = extract32(tcr, 0, 3);
9676 int t1sz = extract32(tcr, 16, 3);
9677
9678 if (t1sz == 0) {
9679 select = va > (0xffffffffu >> t0sz);
9680 } else {
9681 /* Note that we will detect errors later. */
9682 select = va >= ~(0xffffffffu >> t1sz);
9683 }
9684 if (!select) {
9685 tsz = t0sz;
9686 epd = extract32(tcr, 7, 1);
9687 hpd = extract64(tcr, 41, 1);
9688 } else {
9689 tsz = t1sz;
9690 epd = extract32(tcr, 23, 1);
9691 hpd = extract64(tcr, 42, 1);
9692 }
9693 /* For aarch32, hpd0 is not enabled without t2e as well. */
9694 hpd &= extract32(tcr, 6, 1);
9695 }
9696
9697 return (ARMVAParameters) {
9698 .tsz = tsz,
9699 .select = select,
9700 .epd = epd,
9701 .hpd = hpd,
9702 };
9703}
9704
b7cc4e82 9705static bool get_phys_addr_lpae(CPUARMState *env, target_ulong address,
03ae85f8 9706 MMUAccessType access_type, ARMMMUIdx mmu_idx,
b7cc4e82 9707 hwaddr *phys_ptr, MemTxAttrs *txattrs, int *prot,
da909b2c 9708 target_ulong *page_size_ptr,
5b2d261d 9709 ARMMMUFaultInfo *fi, ARMCacheAttrs *cacheattrs)
3dde962f 9710{
2fc0cc0e 9711 ARMCPU *cpu = env_archcpu(env);
1853d5a9 9712 CPUState *cs = CPU(cpu);
3dde962f 9713 /* Read an LPAE long-descriptor translation table. */
da909b2c 9714 ARMFaultType fault_type = ARMFault_Translation;
1b4093ea 9715 uint32_t level;
ba97be9f 9716 ARMVAParameters param;
3dde962f 9717 uint64_t ttbr;
dddb5223 9718 hwaddr descaddr, indexmask, indexmask_grainsize;
3dde962f 9719 uint32_t tableattrs;
36d820af 9720 target_ulong page_size;
3dde962f 9721 uint32_t attrs;
ba97be9f
RH
9722 int32_t stride;
9723 int addrsize, inputsize;
0480f69a 9724 TCR *tcr = regime_tcr(env, mmu_idx);
d8e052b3 9725 int ap, ns, xn, pxn;
88e8add8 9726 uint32_t el = regime_el(env, mmu_idx);
ba97be9f 9727 bool ttbr1_valid;
6109769a 9728 uint64_t descaddrmask;
6e99f762 9729 bool aarch64 = arm_el_is_aa64(env, el);
1bafc2ba 9730 bool guarded = false;
0480f69a
PM
9731
9732 /* TODO:
88e8add8
GB
9733 * This code does not handle the different format TCR for VTCR_EL2.
9734 * This code also does not support shareability levels.
9735 * Attribute and permission bit handling should also be checked when adding
9736 * support for those page table walks.
0480f69a 9737 */
6e99f762 9738 if (aarch64) {
ba97be9f
RH
9739 param = aa64_va_parameters(env, address, mmu_idx,
9740 access_type != MMU_INST_FETCH);
1b4093ea 9741 level = 0;
88e8add8
GB
9742 /* If we are in 64-bit EL2 or EL3 then there is no TTBR1, so mark it
9743 * invalid.
9744 */
ba97be9f
RH
9745 ttbr1_valid = (el < 2);
9746 addrsize = 64 - 8 * param.tbi;
9747 inputsize = 64 - param.tsz;
d0a2cbce 9748 } else {
ba97be9f 9749 param = aa32_va_parameters(env, address, mmu_idx);
1b4093ea 9750 level = 1;
d0a2cbce 9751 /* There is no TTBR1 for EL2 */
ba97be9f 9752 ttbr1_valid = (el != 2);
97fa9350 9753 addrsize = (mmu_idx == ARMMMUIdx_Stage2 ? 40 : 32);
ba97be9f 9754 inputsize = addrsize - param.tsz;
2c8dd318 9755 }
3dde962f 9756
ba97be9f
RH
9757 /*
9758 * We determined the region when collecting the parameters, but we
9759 * have not yet validated that the address is valid for the region.
9760 * Extract the top bits and verify that they all match select.
36d820af
RH
9761 *
9762 * For aa32, if inputsize == addrsize, then we have selected the
9763 * region by exclusion in aa32_va_parameters and there is no more
9764 * validation to do here.
9765 */
9766 if (inputsize < addrsize) {
9767 target_ulong top_bits = sextract64(address, inputsize,
9768 addrsize - inputsize);
9769 if (-top_bits != param.select || (param.select && !ttbr1_valid)) {
9770 /* The gap between the two regions is a Translation fault */
9771 fault_type = ARMFault_Translation;
9772 goto do_fault;
9773 }
3dde962f
PM
9774 }
9775
ba97be9f
RH
9776 if (param.using64k) {
9777 stride = 13;
9778 } else if (param.using16k) {
9779 stride = 11;
9780 } else {
9781 stride = 9;
9782 }
9783
3dde962f
PM
9784 /* Note that QEMU ignores shareability and cacheability attributes,
9785 * so we don't need to do anything with the SH, ORGN, IRGN fields
9786 * in the TTBCR. Similarly, TTBCR:A1 selects whether we get the
9787 * ASID from TTBR0 or TTBR1, but QEMU's TLB doesn't currently
9788 * implement any ASID-like capability so we can ignore it (instead
9789 * we will always flush the TLB any time the ASID is changed).
9790 */
ba97be9f 9791 ttbr = regime_ttbr(env, mmu_idx, param.select);
3dde962f 9792
0480f69a 9793 /* Here we should have set up all the parameters for the translation:
6e99f762 9794 * inputsize, ttbr, epd, stride, tbi
0480f69a
PM
9795 */
9796
ba97be9f 9797 if (param.epd) {
88e8add8
GB
9798 /* Translation table walk disabled => Translation fault on TLB miss
9799 * Note: This is always 0 on 64-bit EL2 and EL3.
9800 */
3dde962f
PM
9801 goto do_fault;
9802 }
9803
97fa9350 9804 if (mmu_idx != ARMMMUIdx_Stage2) {
1853d5a9
EI
9805 /* The starting level depends on the virtual address size (which can
9806 * be up to 48 bits) and the translation granule size. It indicates
9807 * the number of strides (stride bits at a time) needed to
9808 * consume the bits of the input address. In the pseudocode this is:
9809 * level = 4 - RoundUp((inputsize - grainsize) / stride)
9810 * where their 'inputsize' is our 'inputsize', 'grainsize' is
9811 * our 'stride + 3' and 'stride' is our 'stride'.
9812 * Applying the usual "rounded up m/n is (m+n-1)/n" and simplifying:
9813 * = 4 - (inputsize - stride - 3 + stride - 1) / stride
9814 * = 4 - (inputsize - 4) / stride;
9815 */
9816 level = 4 - (inputsize - 4) / stride;
9817 } else {
9818 /* For stage 2 translations the starting level is specified by the
9819 * VTCR_EL2.SL0 field (whose interpretation depends on the page size)
9820 */
1b4093ea
SS
9821 uint32_t sl0 = extract32(tcr->raw_tcr, 6, 2);
9822 uint32_t startlevel;
1853d5a9
EI
9823 bool ok;
9824
6e99f762 9825 if (!aarch64 || stride == 9) {
1853d5a9 9826 /* AArch32 or 4KB pages */
1b4093ea 9827 startlevel = 2 - sl0;
1853d5a9
EI
9828 } else {
9829 /* 16KB or 64KB pages */
1b4093ea 9830 startlevel = 3 - sl0;
1853d5a9
EI
9831 }
9832
9833 /* Check that the starting level is valid. */
6e99f762 9834 ok = check_s2_mmu_setup(cpu, aarch64, startlevel,
1b4093ea 9835 inputsize, stride);
1853d5a9 9836 if (!ok) {
da909b2c 9837 fault_type = ARMFault_Translation;
1853d5a9
EI
9838 goto do_fault;
9839 }
1b4093ea 9840 level = startlevel;
1853d5a9 9841 }
3dde962f 9842
dddb5223
SS
9843 indexmask_grainsize = (1ULL << (stride + 3)) - 1;
9844 indexmask = (1ULL << (inputsize - (stride * (4 - level)))) - 1;
3dde962f
PM
9845
9846 /* Now we can extract the actual base address from the TTBR */
2c8dd318 9847 descaddr = extract64(ttbr, 0, 48);
dddb5223 9848 descaddr &= ~indexmask;
3dde962f 9849
6109769a 9850 /* The address field in the descriptor goes up to bit 39 for ARMv7
dddb5223
SS
9851 * but up to bit 47 for ARMv8, but we use the descaddrmask
9852 * up to bit 39 for AArch32, because we don't need other bits in that case
9853 * to construct next descriptor address (anyway they should be all zeroes).
6109769a 9854 */
6e99f762 9855 descaddrmask = ((1ull << (aarch64 ? 48 : 40)) - 1) &
dddb5223 9856 ~indexmask_grainsize;
6109769a 9857
ebca90e4
PM
9858 /* Secure accesses start with the page table in secure memory and
9859 * can be downgraded to non-secure at any step. Non-secure accesses
9860 * remain non-secure. We implement this by just ORing in the NSTable/NS
9861 * bits at each step.
9862 */
9863 tableattrs = regime_is_secure(env, mmu_idx) ? 0 : (1 << 4);
3dde962f
PM
9864 for (;;) {
9865 uint64_t descriptor;
ebca90e4 9866 bool nstable;
3dde962f 9867
dddb5223 9868 descaddr |= (address >> (stride * (4 - level))) & indexmask;
2c8dd318 9869 descaddr &= ~7ULL;
ebca90e4 9870 nstable = extract32(tableattrs, 4, 1);
3795a6de 9871 descriptor = arm_ldq_ptw(cs, descaddr, !nstable, mmu_idx, fi);
3b39d734 9872 if (fi->type != ARMFault_None) {
37785977
EI
9873 goto do_fault;
9874 }
9875
3dde962f
PM
9876 if (!(descriptor & 1) ||
9877 (!(descriptor & 2) && (level == 3))) {
9878 /* Invalid, or the Reserved level 3 encoding */
9879 goto do_fault;
9880 }
6109769a 9881 descaddr = descriptor & descaddrmask;
3dde962f
PM
9882
9883 if ((descriptor & 2) && (level < 3)) {
037c13c5 9884 /* Table entry. The top five bits are attributes which may
3dde962f
PM
9885 * propagate down through lower levels of the table (and
9886 * which are all arranged so that 0 means "no effect", so
9887 * we can gather them up by ORing in the bits at each level).
9888 */
9889 tableattrs |= extract64(descriptor, 59, 5);
9890 level++;
dddb5223 9891 indexmask = indexmask_grainsize;
3dde962f
PM
9892 continue;
9893 }
9894 /* Block entry at level 1 or 2, or page entry at level 3.
9895 * These are basically the same thing, although the number
9896 * of bits we pull in from the vaddr varies.
9897 */
973a5434 9898 page_size = (1ULL << ((stride * (4 - level)) + 3));
3dde962f 9899 descaddr |= (address & (page_size - 1));
6ab1a5ee 9900 /* Extract attributes from the descriptor */
d615efac
IC
9901 attrs = extract64(descriptor, 2, 10)
9902 | (extract64(descriptor, 52, 12) << 10);
6ab1a5ee 9903
97fa9350 9904 if (mmu_idx == ARMMMUIdx_Stage2) {
6ab1a5ee
EI
9905 /* Stage 2 table descriptors do not include any attribute fields */
9906 break;
9907 }
9908 /* Merge in attributes from table descriptors */
037c13c5 9909 attrs |= nstable << 3; /* NS */
1bafc2ba 9910 guarded = extract64(descriptor, 50, 1); /* GP */
ba97be9f 9911 if (param.hpd) {
037c13c5
RH
9912 /* HPD disables all the table attributes except NSTable. */
9913 break;
9914 }
9915 attrs |= extract32(tableattrs, 0, 2) << 11; /* XN, PXN */
3dde962f
PM
9916 /* The sense of AP[1] vs APTable[0] is reversed, as APTable[0] == 1
9917 * means "force PL1 access only", which means forcing AP[1] to 0.
9918 */
037c13c5
RH
9919 attrs &= ~(extract32(tableattrs, 2, 1) << 4); /* !APT[0] => AP[1] */
9920 attrs |= extract32(tableattrs, 3, 1) << 5; /* APT[1] => AP[2] */
3dde962f
PM
9921 break;
9922 }
9923 /* Here descaddr is the final physical address, and attributes
9924 * are all in attrs.
9925 */
da909b2c 9926 fault_type = ARMFault_AccessFlag;
3dde962f
PM
9927 if ((attrs & (1 << 8)) == 0) {
9928 /* Access flag */
9929 goto do_fault;
9930 }
d8e052b3
AJ
9931
9932 ap = extract32(attrs, 4, 2);
d8e052b3 9933 xn = extract32(attrs, 12, 1);
d8e052b3 9934
97fa9350 9935 if (mmu_idx == ARMMMUIdx_Stage2) {
6ab1a5ee
EI
9936 ns = true;
9937 *prot = get_S2prot(env, ap, xn);
9938 } else {
9939 ns = extract32(attrs, 3, 1);
9940 pxn = extract32(attrs, 11, 1);
6e99f762 9941 *prot = get_S1prot(env, mmu_idx, aarch64, ap, ns, xn, pxn);
6ab1a5ee 9942 }
d8e052b3 9943
da909b2c 9944 fault_type = ARMFault_Permission;
d8e052b3 9945 if (!(*prot & (1 << access_type))) {
3dde962f
PM
9946 goto do_fault;
9947 }
3dde962f 9948
8bf5b6a9
PM
9949 if (ns) {
9950 /* The NS bit will (as required by the architecture) have no effect if
9951 * the CPU doesn't support TZ or this is a non-secure translation
9952 * regime, because the attribute will already be non-secure.
9953 */
9954 txattrs->secure = false;
9955 }
1bafc2ba
RH
9956 /* When in aarch64 mode, and BTI is enabled, remember GP in the IOTLB. */
9957 if (aarch64 && guarded && cpu_isar_feature(aa64_bti, cpu)) {
9958 txattrs->target_tlb_bit0 = true;
9959 }
5b2d261d
AB
9960
9961 if (cacheattrs != NULL) {
97fa9350 9962 if (mmu_idx == ARMMMUIdx_Stage2) {
5b2d261d
AB
9963 cacheattrs->attrs = convert_stage2_attrs(env,
9964 extract32(attrs, 0, 4));
9965 } else {
9966 /* Index into MAIR registers for cache attributes */
9967 uint8_t attrindx = extract32(attrs, 0, 3);
9968 uint64_t mair = env->cp15.mair_el[regime_el(env, mmu_idx)];
9969 assert(attrindx <= 7);
9970 cacheattrs->attrs = extract64(mair, attrindx * 8, 8);
9971 }
9972 cacheattrs->shareability = extract32(attrs, 6, 2);
9973 }
9974
3dde962f
PM
9975 *phys_ptr = descaddr;
9976 *page_size_ptr = page_size;
b7cc4e82 9977 return false;
3dde962f
PM
9978
9979do_fault:
da909b2c
PM
9980 fi->type = fault_type;
9981 fi->level = level;
37785977 9982 /* Tag the error as S2 for failed S1 PTW at S2 or ordinary S2. */
97fa9350 9983 fi->stage2 = fi->s1ptw || (mmu_idx == ARMMMUIdx_Stage2);
b7cc4e82 9984 return true;
3dde962f
PM
9985}
9986
f6bda88f
PC
9987static inline void get_phys_addr_pmsav7_default(CPUARMState *env,
9988 ARMMMUIdx mmu_idx,
9989 int32_t address, int *prot)
9990{
3a00d560
MD
9991 if (!arm_feature(env, ARM_FEATURE_M)) {
9992 *prot = PAGE_READ | PAGE_WRITE;
9993 switch (address) {
9994 case 0xF0000000 ... 0xFFFFFFFF:
9995 if (regime_sctlr(env, mmu_idx) & SCTLR_V) {
9996 /* hivecs execing is ok */
9997 *prot |= PAGE_EXEC;
9998 }
9999 break;
10000 case 0x00000000 ... 0x7FFFFFFF:
f6bda88f 10001 *prot |= PAGE_EXEC;
3a00d560
MD
10002 break;
10003 }
10004 } else {
10005 /* Default system address map for M profile cores.
10006 * The architecture specifies which regions are execute-never;
10007 * at the MPU level no other checks are defined.
10008 */
10009 switch (address) {
10010 case 0x00000000 ... 0x1fffffff: /* ROM */
10011 case 0x20000000 ... 0x3fffffff: /* SRAM */
10012 case 0x60000000 ... 0x7fffffff: /* RAM */
10013 case 0x80000000 ... 0x9fffffff: /* RAM */
10014 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
10015 break;
10016 case 0x40000000 ... 0x5fffffff: /* Peripheral */
10017 case 0xa0000000 ... 0xbfffffff: /* Device */
10018 case 0xc0000000 ... 0xdfffffff: /* Device */
10019 case 0xe0000000 ... 0xffffffff: /* System */
10020 *prot = PAGE_READ | PAGE_WRITE;
10021 break;
10022 default:
10023 g_assert_not_reached();
f6bda88f 10024 }
f6bda88f 10025 }
f6bda88f
PC
10026}
10027
29c483a5
MD
10028static bool pmsav7_use_background_region(ARMCPU *cpu,
10029 ARMMMUIdx mmu_idx, bool is_user)
10030{
10031 /* Return true if we should use the default memory map as a
10032 * "background" region if there are no hits against any MPU regions.
10033 */
10034 CPUARMState *env = &cpu->env;
10035
10036 if (is_user) {
10037 return false;
10038 }
10039
10040 if (arm_feature(env, ARM_FEATURE_M)) {
ecf5e8ea
PM
10041 return env->v7m.mpu_ctrl[regime_is_secure(env, mmu_idx)]
10042 & R_V7M_MPU_CTRL_PRIVDEFENA_MASK;
29c483a5
MD
10043 } else {
10044 return regime_sctlr(env, mmu_idx) & SCTLR_BR;
10045 }
10046}
10047
38aaa60c
PM
10048static inline bool m_is_ppb_region(CPUARMState *env, uint32_t address)
10049{
10050 /* True if address is in the M profile PPB region 0xe0000000 - 0xe00fffff */
10051 return arm_feature(env, ARM_FEATURE_M) &&
10052 extract32(address, 20, 12) == 0xe00;
10053}
10054
bf446a11
PM
10055static inline bool m_is_system_region(CPUARMState *env, uint32_t address)
10056{
10057 /* True if address is in the M profile system region
10058 * 0xe0000000 - 0xffffffff
10059 */
10060 return arm_feature(env, ARM_FEATURE_M) && extract32(address, 29, 3) == 0x7;
10061}
10062
f6bda88f 10063static bool get_phys_addr_pmsav7(CPUARMState *env, uint32_t address,
03ae85f8 10064 MMUAccessType access_type, ARMMMUIdx mmu_idx,
9375ad15 10065 hwaddr *phys_ptr, int *prot,
e5e40999 10066 target_ulong *page_size,
9375ad15 10067 ARMMMUFaultInfo *fi)
f6bda88f 10068{
2fc0cc0e 10069 ARMCPU *cpu = env_archcpu(env);
f6bda88f
PC
10070 int n;
10071 bool is_user = regime_is_user(env, mmu_idx);
10072
10073 *phys_ptr = address;
e5e40999 10074 *page_size = TARGET_PAGE_SIZE;
f6bda88f
PC
10075 *prot = 0;
10076
38aaa60c
PM
10077 if (regime_translation_disabled(env, mmu_idx) ||
10078 m_is_ppb_region(env, address)) {
10079 /* MPU disabled or M profile PPB access: use default memory map.
10080 * The other case which uses the default memory map in the
10081 * v7M ARM ARM pseudocode is exception vector reads from the vector
10082 * table. In QEMU those accesses are done in arm_v7m_load_vector(),
10083 * which always does a direct read using address_space_ldl(), rather
10084 * than going via this function, so we don't need to check that here.
10085 */
f6bda88f
PC
10086 get_phys_addr_pmsav7_default(env, mmu_idx, address, prot);
10087 } else { /* MPU enabled */
10088 for (n = (int)cpu->pmsav7_dregion - 1; n >= 0; n--) {
10089 /* region search */
10090 uint32_t base = env->pmsav7.drbar[n];
10091 uint32_t rsize = extract32(env->pmsav7.drsr[n], 1, 5);
10092 uint32_t rmask;
10093 bool srdis = false;
10094
10095 if (!(env->pmsav7.drsr[n] & 0x1)) {
10096 continue;
10097 }
10098
10099 if (!rsize) {
c9f9f124
MD
10100 qemu_log_mask(LOG_GUEST_ERROR,
10101 "DRSR[%d]: Rsize field cannot be 0\n", n);
f6bda88f
PC
10102 continue;
10103 }
10104 rsize++;
10105 rmask = (1ull << rsize) - 1;
10106
10107 if (base & rmask) {
c9f9f124
MD
10108 qemu_log_mask(LOG_GUEST_ERROR,
10109 "DRBAR[%d]: 0x%" PRIx32 " misaligned "
10110 "to DRSR region size, mask = 0x%" PRIx32 "\n",
10111 n, base, rmask);
f6bda88f
PC
10112 continue;
10113 }
10114
10115 if (address < base || address > base + rmask) {
9d2b5a58
PM
10116 /*
10117 * Address not in this region. We must check whether the
10118 * region covers addresses in the same page as our address.
10119 * In that case we must not report a size that covers the
10120 * whole page for a subsequent hit against a different MPU
10121 * region or the background region, because it would result in
10122 * incorrect TLB hits for subsequent accesses to addresses that
10123 * are in this MPU region.
10124 */
10125 if (ranges_overlap(base, rmask,
10126 address & TARGET_PAGE_MASK,
10127 TARGET_PAGE_SIZE)) {
10128 *page_size = 1;
10129 }
f6bda88f
PC
10130 continue;
10131 }
10132
10133 /* Region matched */
10134
10135 if (rsize >= 8) { /* no subregions for regions < 256 bytes */
10136 int i, snd;
10137 uint32_t srdis_mask;
10138
10139 rsize -= 3; /* sub region size (power of 2) */
10140 snd = ((address - base) >> rsize) & 0x7;
10141 srdis = extract32(env->pmsav7.drsr[n], snd + 8, 1);
10142
10143 srdis_mask = srdis ? 0x3 : 0x0;
10144 for (i = 2; i <= 8 && rsize < TARGET_PAGE_BITS; i *= 2) {
10145 /* This will check in groups of 2, 4 and then 8, whether
10146 * the subregion bits are consistent. rsize is incremented
10147 * back up to give the region size, considering consistent
10148 * adjacent subregions as one region. Stop testing if rsize
10149 * is already big enough for an entire QEMU page.
10150 */
10151 int snd_rounded = snd & ~(i - 1);
10152 uint32_t srdis_multi = extract32(env->pmsav7.drsr[n],
10153 snd_rounded + 8, i);
10154 if (srdis_mask ^ srdis_multi) {
10155 break;
10156 }
10157 srdis_mask = (srdis_mask << i) | srdis_mask;
10158 rsize++;
10159 }
10160 }
f6bda88f
PC
10161 if (srdis) {
10162 continue;
10163 }
e5e40999
PM
10164 if (rsize < TARGET_PAGE_BITS) {
10165 *page_size = 1 << rsize;
10166 }
f6bda88f
PC
10167 break;
10168 }
10169
10170 if (n == -1) { /* no hits */
29c483a5 10171 if (!pmsav7_use_background_region(cpu, mmu_idx, is_user)) {
f6bda88f 10172 /* background fault */
9375ad15 10173 fi->type = ARMFault_Background;
f6bda88f
PC
10174 return true;
10175 }
10176 get_phys_addr_pmsav7_default(env, mmu_idx, address, prot);
10177 } else { /* a MPU hit! */
10178 uint32_t ap = extract32(env->pmsav7.dracr[n], 8, 3);
bf446a11
PM
10179 uint32_t xn = extract32(env->pmsav7.dracr[n], 12, 1);
10180
10181 if (m_is_system_region(env, address)) {
10182 /* System space is always execute never */
10183 xn = 1;
10184 }
f6bda88f
PC
10185
10186 if (is_user) { /* User mode AP bit decoding */
10187 switch (ap) {
10188 case 0:
10189 case 1:
10190 case 5:
10191 break; /* no access */
10192 case 3:
10193 *prot |= PAGE_WRITE;
10194 /* fall through */
10195 case 2:
10196 case 6:
10197 *prot |= PAGE_READ | PAGE_EXEC;
10198 break;
8638f1ad
PM
10199 case 7:
10200 /* for v7M, same as 6; for R profile a reserved value */
10201 if (arm_feature(env, ARM_FEATURE_M)) {
10202 *prot |= PAGE_READ | PAGE_EXEC;
10203 break;
10204 }
10205 /* fall through */
f6bda88f
PC
10206 default:
10207 qemu_log_mask(LOG_GUEST_ERROR,
c9f9f124
MD
10208 "DRACR[%d]: Bad value for AP bits: 0x%"
10209 PRIx32 "\n", n, ap);
f6bda88f
PC
10210 }
10211 } else { /* Priv. mode AP bits decoding */
10212 switch (ap) {
10213 case 0:
10214 break; /* no access */
10215 case 1:
10216 case 2:
10217 case 3:
10218 *prot |= PAGE_WRITE;
10219 /* fall through */
10220 case 5:
10221 case 6:
10222 *prot |= PAGE_READ | PAGE_EXEC;
10223 break;
8638f1ad
PM
10224 case 7:
10225 /* for v7M, same as 6; for R profile a reserved value */
10226 if (arm_feature(env, ARM_FEATURE_M)) {
10227 *prot |= PAGE_READ | PAGE_EXEC;
10228 break;
10229 }
10230 /* fall through */
f6bda88f
PC
10231 default:
10232 qemu_log_mask(LOG_GUEST_ERROR,
c9f9f124
MD
10233 "DRACR[%d]: Bad value for AP bits: 0x%"
10234 PRIx32 "\n", n, ap);
f6bda88f
PC
10235 }
10236 }
10237
10238 /* execute never */
bf446a11 10239 if (xn) {
f6bda88f
PC
10240 *prot &= ~PAGE_EXEC;
10241 }
10242 }
10243 }
10244
9375ad15
PM
10245 fi->type = ARMFault_Permission;
10246 fi->level = 1;
f6bda88f
PC
10247 return !(*prot & (1 << access_type));
10248}
10249
35337cc3
PM
10250static bool v8m_is_sau_exempt(CPUARMState *env,
10251 uint32_t address, MMUAccessType access_type)
10252{
10253 /* The architecture specifies that certain address ranges are
10254 * exempt from v8M SAU/IDAU checks.
10255 */
10256 return
10257 (access_type == MMU_INST_FETCH && m_is_system_region(env, address)) ||
10258 (address >= 0xe0000000 && address <= 0xe0002fff) ||
10259 (address >= 0xe000e000 && address <= 0xe000efff) ||
10260 (address >= 0xe002e000 && address <= 0xe002efff) ||
10261 (address >= 0xe0040000 && address <= 0xe0041fff) ||
10262 (address >= 0xe00ff000 && address <= 0xe00fffff);
10263}
10264
787a7e76 10265void v8m_security_lookup(CPUARMState *env, uint32_t address,
35337cc3
PM
10266 MMUAccessType access_type, ARMMMUIdx mmu_idx,
10267 V8M_SAttributes *sattrs)
10268{
10269 /* Look up the security attributes for this address. Compare the
10270 * pseudocode SecurityCheck() function.
10271 * We assume the caller has zero-initialized *sattrs.
10272 */
2fc0cc0e 10273 ARMCPU *cpu = env_archcpu(env);
35337cc3 10274 int r;
181962fd
PM
10275 bool idau_exempt = false, idau_ns = true, idau_nsc = true;
10276 int idau_region = IREGION_NOTVALID;
72042435
PM
10277 uint32_t addr_page_base = address & TARGET_PAGE_MASK;
10278 uint32_t addr_page_limit = addr_page_base + (TARGET_PAGE_SIZE - 1);
35337cc3 10279
181962fd
PM
10280 if (cpu->idau) {
10281 IDAUInterfaceClass *iic = IDAU_INTERFACE_GET_CLASS(cpu->idau);
10282 IDAUInterface *ii = IDAU_INTERFACE(cpu->idau);
10283
10284 iic->check(ii, address, &idau_region, &idau_exempt, &idau_ns,
10285 &idau_nsc);
10286 }
35337cc3
PM
10287
10288 if (access_type == MMU_INST_FETCH && extract32(address, 28, 4) == 0xf) {
10289 /* 0xf0000000..0xffffffff is always S for insn fetches */
10290 return;
10291 }
10292
181962fd 10293 if (idau_exempt || v8m_is_sau_exempt(env, address, access_type)) {
35337cc3
PM
10294 sattrs->ns = !regime_is_secure(env, mmu_idx);
10295 return;
10296 }
10297
181962fd
PM
10298 if (idau_region != IREGION_NOTVALID) {
10299 sattrs->irvalid = true;
10300 sattrs->iregion = idau_region;
10301 }
10302
35337cc3
PM
10303 switch (env->sau.ctrl & 3) {
10304 case 0: /* SAU.ENABLE == 0, SAU.ALLNS == 0 */
10305 break;
10306 case 2: /* SAU.ENABLE == 0, SAU.ALLNS == 1 */
10307 sattrs->ns = true;
10308 break;
10309 default: /* SAU.ENABLE == 1 */
10310 for (r = 0; r < cpu->sau_sregion; r++) {
10311 if (env->sau.rlar[r] & 1) {
10312 uint32_t base = env->sau.rbar[r] & ~0x1f;
10313 uint32_t limit = env->sau.rlar[r] | 0x1f;
10314
10315 if (base <= address && limit >= address) {
72042435
PM
10316 if (base > addr_page_base || limit < addr_page_limit) {
10317 sattrs->subpage = true;
10318 }
35337cc3
PM
10319 if (sattrs->srvalid) {
10320 /* If we hit in more than one region then we must report
10321 * as Secure, not NS-Callable, with no valid region
10322 * number info.
10323 */
10324 sattrs->ns = false;
10325 sattrs->nsc = false;
10326 sattrs->sregion = 0;
10327 sattrs->srvalid = false;
10328 break;
10329 } else {
10330 if (env->sau.rlar[r] & 2) {
10331 sattrs->nsc = true;
10332 } else {
10333 sattrs->ns = true;
10334 }
10335 sattrs->srvalid = true;
10336 sattrs->sregion = r;
10337 }
9d2b5a58
PM
10338 } else {
10339 /*
10340 * Address not in this region. We must check whether the
10341 * region covers addresses in the same page as our address.
10342 * In that case we must not report a size that covers the
10343 * whole page for a subsequent hit against a different MPU
10344 * region or the background region, because it would result
10345 * in incorrect TLB hits for subsequent accesses to
10346 * addresses that are in this MPU region.
10347 */
10348 if (limit >= base &&
10349 ranges_overlap(base, limit - base + 1,
10350 addr_page_base,
10351 TARGET_PAGE_SIZE)) {
10352 sattrs->subpage = true;
10353 }
35337cc3
PM
10354 }
10355 }
10356 }
7e3f1223
TR
10357 break;
10358 }
35337cc3 10359
7e3f1223
TR
10360 /*
10361 * The IDAU will override the SAU lookup results if it specifies
10362 * higher security than the SAU does.
10363 */
10364 if (!idau_ns) {
10365 if (sattrs->ns || (!idau_nsc && sattrs->nsc)) {
10366 sattrs->ns = false;
10367 sattrs->nsc = idau_nsc;
181962fd 10368 }
35337cc3
PM
10369 }
10370}
10371
787a7e76 10372bool pmsav8_mpu_lookup(CPUARMState *env, uint32_t address,
54317c0f
PM
10373 MMUAccessType access_type, ARMMMUIdx mmu_idx,
10374 hwaddr *phys_ptr, MemTxAttrs *txattrs,
72042435
PM
10375 int *prot, bool *is_subpage,
10376 ARMMMUFaultInfo *fi, uint32_t *mregion)
54317c0f
PM
10377{
10378 /* Perform a PMSAv8 MPU lookup (without also doing the SAU check
10379 * that a full phys-to-virt translation does).
10380 * mregion is (if not NULL) set to the region number which matched,
10381 * or -1 if no region number is returned (MPU off, address did not
10382 * hit a region, address hit in multiple regions).
72042435
PM
10383 * We set is_subpage to true if the region hit doesn't cover the
10384 * entire TARGET_PAGE the address is within.
54317c0f 10385 */
2fc0cc0e 10386 ARMCPU *cpu = env_archcpu(env);
504e3cc3 10387 bool is_user = regime_is_user(env, mmu_idx);
62c58ee0 10388 uint32_t secure = regime_is_secure(env, mmu_idx);
504e3cc3
PM
10389 int n;
10390 int matchregion = -1;
10391 bool hit = false;
72042435
PM
10392 uint32_t addr_page_base = address & TARGET_PAGE_MASK;
10393 uint32_t addr_page_limit = addr_page_base + (TARGET_PAGE_SIZE - 1);
504e3cc3 10394
72042435 10395 *is_subpage = false;
504e3cc3
PM
10396 *phys_ptr = address;
10397 *prot = 0;
54317c0f
PM
10398 if (mregion) {
10399 *mregion = -1;
35337cc3
PM
10400 }
10401
504e3cc3
PM
10402 /* Unlike the ARM ARM pseudocode, we don't need to check whether this
10403 * was an exception vector read from the vector table (which is always
10404 * done using the default system address map), because those accesses
10405 * are done in arm_v7m_load_vector(), which always does a direct
10406 * read using address_space_ldl(), rather than going via this function.
10407 */
10408 if (regime_translation_disabled(env, mmu_idx)) { /* MPU disabled */
10409 hit = true;
10410 } else if (m_is_ppb_region(env, address)) {
10411 hit = true;
504e3cc3 10412 } else {
cff21316
PM
10413 if (pmsav7_use_background_region(cpu, mmu_idx, is_user)) {
10414 hit = true;
10415 }
10416
504e3cc3
PM
10417 for (n = (int)cpu->pmsav7_dregion - 1; n >= 0; n--) {
10418 /* region search */
10419 /* Note that the base address is bits [31:5] from the register
10420 * with bits [4:0] all zeroes, but the limit address is bits
10421 * [31:5] from the register with bits [4:0] all ones.
10422 */
62c58ee0
PM
10423 uint32_t base = env->pmsav8.rbar[secure][n] & ~0x1f;
10424 uint32_t limit = env->pmsav8.rlar[secure][n] | 0x1f;
504e3cc3 10425
62c58ee0 10426 if (!(env->pmsav8.rlar[secure][n] & 0x1)) {
504e3cc3
PM
10427 /* Region disabled */
10428 continue;
10429 }
10430
10431 if (address < base || address > limit) {
9d2b5a58
PM
10432 /*
10433 * Address not in this region. We must check whether the
10434 * region covers addresses in the same page as our address.
10435 * In that case we must not report a size that covers the
10436 * whole page for a subsequent hit against a different MPU
10437 * region or the background region, because it would result in
10438 * incorrect TLB hits for subsequent accesses to addresses that
10439 * are in this MPU region.
10440 */
10441 if (limit >= base &&
10442 ranges_overlap(base, limit - base + 1,
10443 addr_page_base,
10444 TARGET_PAGE_SIZE)) {
10445 *is_subpage = true;
10446 }
504e3cc3
PM
10447 continue;
10448 }
10449
72042435
PM
10450 if (base > addr_page_base || limit < addr_page_limit) {
10451 *is_subpage = true;
10452 }
10453
cff21316 10454 if (matchregion != -1) {
504e3cc3
PM
10455 /* Multiple regions match -- always a failure (unlike
10456 * PMSAv7 where highest-numbered-region wins)
10457 */
3f551b5b
PM
10458 fi->type = ARMFault_Permission;
10459 fi->level = 1;
504e3cc3
PM
10460 return true;
10461 }
10462
10463 matchregion = n;
10464 hit = true;
504e3cc3
PM
10465 }
10466 }
10467
10468 if (!hit) {
10469 /* background fault */
3f551b5b 10470 fi->type = ARMFault_Background;
504e3cc3
PM
10471 return true;
10472 }
10473
10474 if (matchregion == -1) {
10475 /* hit using the background region */
10476 get_phys_addr_pmsav7_default(env, mmu_idx, address, prot);
10477 } else {
62c58ee0
PM
10478 uint32_t ap = extract32(env->pmsav8.rbar[secure][matchregion], 1, 2);
10479 uint32_t xn = extract32(env->pmsav8.rbar[secure][matchregion], 0, 1);
504e3cc3
PM
10480
10481 if (m_is_system_region(env, address)) {
10482 /* System space is always execute never */
10483 xn = 1;
10484 }
10485
10486 *prot = simple_ap_to_rw_prot(env, mmu_idx, ap);
10487 if (*prot && !xn) {
10488 *prot |= PAGE_EXEC;
10489 }
10490 /* We don't need to look the attribute up in the MAIR0/MAIR1
10491 * registers because that only tells us about cacheability.
10492 */
54317c0f
PM
10493 if (mregion) {
10494 *mregion = matchregion;
10495 }
504e3cc3
PM
10496 }
10497
3f551b5b
PM
10498 fi->type = ARMFault_Permission;
10499 fi->level = 1;
504e3cc3
PM
10500 return !(*prot & (1 << access_type));
10501}
10502
54317c0f
PM
10503
10504static bool get_phys_addr_pmsav8(CPUARMState *env, uint32_t address,
10505 MMUAccessType access_type, ARMMMUIdx mmu_idx,
10506 hwaddr *phys_ptr, MemTxAttrs *txattrs,
72042435
PM
10507 int *prot, target_ulong *page_size,
10508 ARMMMUFaultInfo *fi)
54317c0f
PM
10509{
10510 uint32_t secure = regime_is_secure(env, mmu_idx);
10511 V8M_SAttributes sattrs = {};
72042435
PM
10512 bool ret;
10513 bool mpu_is_subpage;
54317c0f
PM
10514
10515 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
10516 v8m_security_lookup(env, address, access_type, mmu_idx, &sattrs);
10517 if (access_type == MMU_INST_FETCH) {
10518 /* Instruction fetches always use the MMU bank and the
10519 * transaction attribute determined by the fetch address,
10520 * regardless of CPU state. This is painful for QEMU
10521 * to handle, because it would mean we need to encode
10522 * into the mmu_idx not just the (user, negpri) information
10523 * for the current security state but also that for the
10524 * other security state, which would balloon the number
10525 * of mmu_idx values needed alarmingly.
10526 * Fortunately we can avoid this because it's not actually
10527 * possible to arbitrarily execute code from memory with
10528 * the wrong security attribute: it will always generate
10529 * an exception of some kind or another, apart from the
10530 * special case of an NS CPU executing an SG instruction
10531 * in S&NSC memory. So we always just fail the translation
10532 * here and sort things out in the exception handler
10533 * (including possibly emulating an SG instruction).
10534 */
10535 if (sattrs.ns != !secure) {
3f551b5b
PM
10536 if (sattrs.nsc) {
10537 fi->type = ARMFault_QEMU_NSCExec;
10538 } else {
10539 fi->type = ARMFault_QEMU_SFault;
10540 }
72042435 10541 *page_size = sattrs.subpage ? 1 : TARGET_PAGE_SIZE;
54317c0f
PM
10542 *phys_ptr = address;
10543 *prot = 0;
10544 return true;
10545 }
10546 } else {
10547 /* For data accesses we always use the MMU bank indicated
10548 * by the current CPU state, but the security attributes
10549 * might downgrade a secure access to nonsecure.
10550 */
10551 if (sattrs.ns) {
10552 txattrs->secure = false;
10553 } else if (!secure) {
10554 /* NS access to S memory must fault.
10555 * Architecturally we should first check whether the
10556 * MPU information for this address indicates that we
10557 * are doing an unaligned access to Device memory, which
10558 * should generate a UsageFault instead. QEMU does not
10559 * currently check for that kind of unaligned access though.
10560 * If we added it we would need to do so as a special case
10561 * for M_FAKE_FSR_SFAULT in arm_v7m_cpu_do_interrupt().
10562 */
3f551b5b 10563 fi->type = ARMFault_QEMU_SFault;
72042435 10564 *page_size = sattrs.subpage ? 1 : TARGET_PAGE_SIZE;
54317c0f
PM
10565 *phys_ptr = address;
10566 *prot = 0;
10567 return true;
10568 }
10569 }
10570 }
10571
72042435
PM
10572 ret = pmsav8_mpu_lookup(env, address, access_type, mmu_idx, phys_ptr,
10573 txattrs, prot, &mpu_is_subpage, fi, NULL);
72042435
PM
10574 *page_size = sattrs.subpage || mpu_is_subpage ? 1 : TARGET_PAGE_SIZE;
10575 return ret;
54317c0f
PM
10576}
10577
13689d43 10578static bool get_phys_addr_pmsav5(CPUARMState *env, uint32_t address,
03ae85f8 10579 MMUAccessType access_type, ARMMMUIdx mmu_idx,
53a4e5c5
PM
10580 hwaddr *phys_ptr, int *prot,
10581 ARMMMUFaultInfo *fi)
9ee6e8bb
PB
10582{
10583 int n;
10584 uint32_t mask;
10585 uint32_t base;
0480f69a 10586 bool is_user = regime_is_user(env, mmu_idx);
9ee6e8bb 10587
3279adb9
PM
10588 if (regime_translation_disabled(env, mmu_idx)) {
10589 /* MPU disabled. */
10590 *phys_ptr = address;
10591 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
10592 return false;
10593 }
10594
9ee6e8bb
PB
10595 *phys_ptr = address;
10596 for (n = 7; n >= 0; n--) {
554b0b09 10597 base = env->cp15.c6_region[n];
87c3d486 10598 if ((base & 1) == 0) {
554b0b09 10599 continue;
87c3d486 10600 }
554b0b09
PM
10601 mask = 1 << ((base >> 1) & 0x1f);
10602 /* Keep this shift separate from the above to avoid an
10603 (undefined) << 32. */
10604 mask = (mask << 1) - 1;
87c3d486 10605 if (((base ^ address) & ~mask) == 0) {
554b0b09 10606 break;
87c3d486 10607 }
9ee6e8bb 10608 }
87c3d486 10609 if (n < 0) {
53a4e5c5 10610 fi->type = ARMFault_Background;
b7cc4e82 10611 return true;
87c3d486 10612 }
9ee6e8bb 10613
03ae85f8 10614 if (access_type == MMU_INST_FETCH) {
7e09797c 10615 mask = env->cp15.pmsav5_insn_ap;
9ee6e8bb 10616 } else {
7e09797c 10617 mask = env->cp15.pmsav5_data_ap;
9ee6e8bb
PB
10618 }
10619 mask = (mask >> (n * 4)) & 0xf;
10620 switch (mask) {
10621 case 0:
53a4e5c5
PM
10622 fi->type = ARMFault_Permission;
10623 fi->level = 1;
b7cc4e82 10624 return true;
9ee6e8bb 10625 case 1:
87c3d486 10626 if (is_user) {
53a4e5c5
PM
10627 fi->type = ARMFault_Permission;
10628 fi->level = 1;
b7cc4e82 10629 return true;
87c3d486 10630 }
554b0b09
PM
10631 *prot = PAGE_READ | PAGE_WRITE;
10632 break;
9ee6e8bb 10633 case 2:
554b0b09 10634 *prot = PAGE_READ;
87c3d486 10635 if (!is_user) {
554b0b09 10636 *prot |= PAGE_WRITE;
87c3d486 10637 }
554b0b09 10638 break;
9ee6e8bb 10639 case 3:
554b0b09
PM
10640 *prot = PAGE_READ | PAGE_WRITE;
10641 break;
9ee6e8bb 10642 case 5:
87c3d486 10643 if (is_user) {
53a4e5c5
PM
10644 fi->type = ARMFault_Permission;
10645 fi->level = 1;
b7cc4e82 10646 return true;
87c3d486 10647 }
554b0b09
PM
10648 *prot = PAGE_READ;
10649 break;
9ee6e8bb 10650 case 6:
554b0b09
PM
10651 *prot = PAGE_READ;
10652 break;
9ee6e8bb 10653 default:
554b0b09 10654 /* Bad permission. */
53a4e5c5
PM
10655 fi->type = ARMFault_Permission;
10656 fi->level = 1;
b7cc4e82 10657 return true;
9ee6e8bb 10658 }
3ad493fc 10659 *prot |= PAGE_EXEC;
b7cc4e82 10660 return false;
9ee6e8bb
PB
10661}
10662
5b2d261d
AB
10663/* Combine either inner or outer cacheability attributes for normal
10664 * memory, according to table D4-42 and pseudocode procedure
10665 * CombineS1S2AttrHints() of ARM DDI 0487B.b (the ARMv8 ARM).
10666 *
10667 * NB: only stage 1 includes allocation hints (RW bits), leading to
10668 * some asymmetry.
10669 */
10670static uint8_t combine_cacheattr_nibble(uint8_t s1, uint8_t s2)
10671{
10672 if (s1 == 4 || s2 == 4) {
10673 /* non-cacheable has precedence */
10674 return 4;
10675 } else if (extract32(s1, 2, 2) == 0 || extract32(s1, 2, 2) == 2) {
10676 /* stage 1 write-through takes precedence */
10677 return s1;
10678 } else if (extract32(s2, 2, 2) == 2) {
10679 /* stage 2 write-through takes precedence, but the allocation hint
10680 * is still taken from stage 1
10681 */
10682 return (2 << 2) | extract32(s1, 0, 2);
10683 } else { /* write-back */
10684 return s1;
10685 }
10686}
10687
10688/* Combine S1 and S2 cacheability/shareability attributes, per D4.5.4
10689 * and CombineS1S2Desc()
10690 *
10691 * @s1: Attributes from stage 1 walk
10692 * @s2: Attributes from stage 2 walk
10693 */
10694static ARMCacheAttrs combine_cacheattrs(ARMCacheAttrs s1, ARMCacheAttrs s2)
10695{
10696 uint8_t s1lo = extract32(s1.attrs, 0, 4), s2lo = extract32(s2.attrs, 0, 4);
10697 uint8_t s1hi = extract32(s1.attrs, 4, 4), s2hi = extract32(s2.attrs, 4, 4);
10698 ARMCacheAttrs ret;
10699
10700 /* Combine shareability attributes (table D4-43) */
10701 if (s1.shareability == 2 || s2.shareability == 2) {
10702 /* if either are outer-shareable, the result is outer-shareable */
10703 ret.shareability = 2;
10704 } else if (s1.shareability == 3 || s2.shareability == 3) {
10705 /* if either are inner-shareable, the result is inner-shareable */
10706 ret.shareability = 3;
10707 } else {
10708 /* both non-shareable */
10709 ret.shareability = 0;
10710 }
10711
10712 /* Combine memory type and cacheability attributes */
10713 if (s1hi == 0 || s2hi == 0) {
10714 /* Device has precedence over normal */
10715 if (s1lo == 0 || s2lo == 0) {
10716 /* nGnRnE has precedence over anything */
10717 ret.attrs = 0;
10718 } else if (s1lo == 4 || s2lo == 4) {
10719 /* non-Reordering has precedence over Reordering */
10720 ret.attrs = 4; /* nGnRE */
10721 } else if (s1lo == 8 || s2lo == 8) {
10722 /* non-Gathering has precedence over Gathering */
10723 ret.attrs = 8; /* nGRE */
10724 } else {
10725 ret.attrs = 0xc; /* GRE */
10726 }
10727
10728 /* Any location for which the resultant memory type is any
10729 * type of Device memory is always treated as Outer Shareable.
10730 */
10731 ret.shareability = 2;
10732 } else { /* Normal memory */
10733 /* Outer/inner cacheability combine independently */
10734 ret.attrs = combine_cacheattr_nibble(s1hi, s2hi) << 4
10735 | combine_cacheattr_nibble(s1lo, s2lo);
10736
10737 if (ret.attrs == 0x44) {
10738 /* Any location for which the resultant memory type is Normal
10739 * Inner Non-cacheable, Outer Non-cacheable is always treated
10740 * as Outer Shareable.
10741 */
10742 ret.shareability = 2;
10743 }
10744 }
10745
10746 return ret;
10747}
10748
10749
702a9357
PM
10750/* get_phys_addr - get the physical address for this virtual address
10751 *
10752 * Find the physical address corresponding to the given virtual address,
10753 * by doing a translation table walk on MMU based systems or using the
10754 * MPU state on MPU based systems.
10755 *
b7cc4e82
PC
10756 * Returns false if the translation was successful. Otherwise, phys_ptr, attrs,
10757 * prot and page_size may not be filled in, and the populated fsr value provides
702a9357
PM
10758 * information on why the translation aborted, in the format of a
10759 * DFSR/IFSR fault register, with the following caveats:
10760 * * we honour the short vs long DFSR format differences.
10761 * * the WnR bit is never set (the caller must do this).
f6bda88f 10762 * * for PSMAv5 based systems we don't bother to return a full FSR format
702a9357
PM
10763 * value.
10764 *
10765 * @env: CPUARMState
10766 * @address: virtual address to get physical address for
10767 * @access_type: 0 for read, 1 for write, 2 for execute
d3649702 10768 * @mmu_idx: MMU index indicating required translation regime
702a9357 10769 * @phys_ptr: set to the physical address corresponding to the virtual address
8bf5b6a9 10770 * @attrs: set to the memory transaction attributes to use
702a9357
PM
10771 * @prot: set to the permissions for the page containing phys_ptr
10772 * @page_size: set to the size of the page containing phys_ptr
5b2d261d
AB
10773 * @fi: set to fault info if the translation fails
10774 * @cacheattrs: (if non-NULL) set to the cacheability/shareability attributes
702a9357 10775 */
ebae861f
PMD
10776bool get_phys_addr(CPUARMState *env, target_ulong address,
10777 MMUAccessType access_type, ARMMMUIdx mmu_idx,
10778 hwaddr *phys_ptr, MemTxAttrs *attrs, int *prot,
10779 target_ulong *page_size,
10780 ARMMMUFaultInfo *fi, ARMCacheAttrs *cacheattrs)
9ee6e8bb 10781{
01b98b68 10782 if (mmu_idx == ARMMMUIdx_E10_0 || mmu_idx == ARMMMUIdx_E10_1) {
9b539263
EI
10783 /* Call ourselves recursively to do the stage 1 and then stage 2
10784 * translations.
0480f69a 10785 */
9b539263
EI
10786 if (arm_feature(env, ARM_FEATURE_EL2)) {
10787 hwaddr ipa;
10788 int s2_prot;
10789 int ret;
5b2d261d 10790 ARMCacheAttrs cacheattrs2 = {};
9b539263
EI
10791
10792 ret = get_phys_addr(env, address, access_type,
8bd5c820 10793 stage_1_mmu_idx(mmu_idx), &ipa, attrs,
bc52bfeb 10794 prot, page_size, fi, cacheattrs);
9b539263
EI
10795
10796 /* If S1 fails or S2 is disabled, return early. */
97fa9350 10797 if (ret || regime_translation_disabled(env, ARMMMUIdx_Stage2)) {
9b539263
EI
10798 *phys_ptr = ipa;
10799 return ret;
10800 }
10801
10802 /* S1 is done. Now do S2 translation. */
97fa9350 10803 ret = get_phys_addr_lpae(env, ipa, access_type, ARMMMUIdx_Stage2,
9b539263 10804 phys_ptr, attrs, &s2_prot,
da909b2c 10805 page_size, fi,
5b2d261d 10806 cacheattrs != NULL ? &cacheattrs2 : NULL);
9b539263
EI
10807 fi->s2addr = ipa;
10808 /* Combine the S1 and S2 perms. */
10809 *prot &= s2_prot;
5b2d261d
AB
10810
10811 /* Combine the S1 and S2 cache attributes, if needed */
10812 if (!ret && cacheattrs != NULL) {
9d1bab33
PM
10813 if (env->cp15.hcr_el2 & HCR_DC) {
10814 /*
10815 * HCR.DC forces the first stage attributes to
10816 * Normal Non-Shareable,
10817 * Inner Write-Back Read-Allocate Write-Allocate,
10818 * Outer Write-Back Read-Allocate Write-Allocate.
10819 */
10820 cacheattrs->attrs = 0xff;
10821 cacheattrs->shareability = 0;
10822 }
5b2d261d
AB
10823 *cacheattrs = combine_cacheattrs(*cacheattrs, cacheattrs2);
10824 }
10825
9b539263
EI
10826 return ret;
10827 } else {
10828 /*
10829 * For non-EL2 CPUs a stage1+stage2 translation is just stage 1.
10830 */
8bd5c820 10831 mmu_idx = stage_1_mmu_idx(mmu_idx);
9b539263 10832 }
0480f69a 10833 }
d3649702 10834
8bf5b6a9
PM
10835 /* The page table entries may downgrade secure to non-secure, but
10836 * cannot upgrade an non-secure translation regime's attributes
10837 * to secure.
10838 */
10839 attrs->secure = regime_is_secure(env, mmu_idx);
0995bf8c 10840 attrs->user = regime_is_user(env, mmu_idx);
8bf5b6a9 10841
0480f69a
PM
10842 /* Fast Context Switch Extension. This doesn't exist at all in v8.
10843 * In v7 and earlier it affects all stage 1 translations.
10844 */
97fa9350 10845 if (address < 0x02000000 && mmu_idx != ARMMMUIdx_Stage2
0480f69a
PM
10846 && !arm_feature(env, ARM_FEATURE_V8)) {
10847 if (regime_el(env, mmu_idx) == 3) {
10848 address += env->cp15.fcseidr_s;
10849 } else {
10850 address += env->cp15.fcseidr_ns;
10851 }
54bf36ed 10852 }
9ee6e8bb 10853
3279adb9 10854 if (arm_feature(env, ARM_FEATURE_PMSA)) {
c9f9f124 10855 bool ret;
f6bda88f 10856 *page_size = TARGET_PAGE_SIZE;
3279adb9 10857
504e3cc3
PM
10858 if (arm_feature(env, ARM_FEATURE_V8)) {
10859 /* PMSAv8 */
10860 ret = get_phys_addr_pmsav8(env, address, access_type, mmu_idx,
72042435 10861 phys_ptr, attrs, prot, page_size, fi);
504e3cc3 10862 } else if (arm_feature(env, ARM_FEATURE_V7)) {
3279adb9
PM
10863 /* PMSAv7 */
10864 ret = get_phys_addr_pmsav7(env, address, access_type, mmu_idx,
e5e40999 10865 phys_ptr, prot, page_size, fi);
3279adb9
PM
10866 } else {
10867 /* Pre-v7 MPU */
10868 ret = get_phys_addr_pmsav5(env, address, access_type, mmu_idx,
53a4e5c5 10869 phys_ptr, prot, fi);
3279adb9
PM
10870 }
10871 qemu_log_mask(CPU_LOG_MMU, "PMSA MPU lookup for %s at 0x%08" PRIx32
c9f9f124 10872 " mmu_idx %u -> %s (prot %c%c%c)\n",
709e4407
PM
10873 access_type == MMU_DATA_LOAD ? "reading" :
10874 (access_type == MMU_DATA_STORE ? "writing" : "execute"),
c9f9f124
MD
10875 (uint32_t)address, mmu_idx,
10876 ret ? "Miss" : "Hit",
10877 *prot & PAGE_READ ? 'r' : '-',
10878 *prot & PAGE_WRITE ? 'w' : '-',
10879 *prot & PAGE_EXEC ? 'x' : '-');
10880
10881 return ret;
f6bda88f
PC
10882 }
10883
3279adb9
PM
10884 /* Definitely a real MMU, not an MPU */
10885
0480f69a 10886 if (regime_translation_disabled(env, mmu_idx)) {
3279adb9 10887 /* MMU disabled. */
9ee6e8bb 10888 *phys_ptr = address;
3ad493fc 10889 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
d4c430a8 10890 *page_size = TARGET_PAGE_SIZE;
9ee6e8bb 10891 return 0;
0480f69a
PM
10892 }
10893
0480f69a 10894 if (regime_using_lpae_format(env, mmu_idx)) {
bc52bfeb
PM
10895 return get_phys_addr_lpae(env, address, access_type, mmu_idx,
10896 phys_ptr, attrs, prot, page_size,
10897 fi, cacheattrs);
0480f69a 10898 } else if (regime_sctlr(env, mmu_idx) & SCTLR_XP) {
bc52bfeb
PM
10899 return get_phys_addr_v6(env, address, access_type, mmu_idx,
10900 phys_ptr, attrs, prot, page_size, fi);
9ee6e8bb 10901 } else {
bc52bfeb 10902 return get_phys_addr_v5(env, address, access_type, mmu_idx,
f989983e 10903 phys_ptr, prot, page_size, fi);
9ee6e8bb
PB
10904 }
10905}
10906
0faea0c7
PM
10907hwaddr arm_cpu_get_phys_page_attrs_debug(CPUState *cs, vaddr addr,
10908 MemTxAttrs *attrs)
b5ff1b31 10909{
00b941e5 10910 ARMCPU *cpu = ARM_CPU(cs);
d3649702 10911 CPUARMState *env = &cpu->env;
a8170e5e 10912 hwaddr phys_addr;
d4c430a8 10913 target_ulong page_size;
b5ff1b31 10914 int prot;
b7cc4e82 10915 bool ret;
e14b5a23 10916 ARMMMUFaultInfo fi = {};
50494a27 10917 ARMMMUIdx mmu_idx = arm_mmu_idx(env);
b5ff1b31 10918
0faea0c7
PM
10919 *attrs = (MemTxAttrs) {};
10920
8bd5c820 10921 ret = get_phys_addr(env, addr, 0, mmu_idx, &phys_addr,
bc52bfeb 10922 attrs, &prot, &page_size, &fi, NULL);
b5ff1b31 10923
b7cc4e82 10924 if (ret) {
b5ff1b31 10925 return -1;
00b941e5 10926 }
b5ff1b31
FB
10927 return phys_addr;
10928}
10929
b5ff1b31 10930#endif
6ddbc6e4
PB
10931
10932/* Note that signed overflow is undefined in C. The following routines are
10933 careful to use unsigned types where modulo arithmetic is required.
10934 Failure to do so _will_ break on newer gcc. */
10935
10936/* Signed saturating arithmetic. */
10937
1654b2d6 10938/* Perform 16-bit signed saturating addition. */
6ddbc6e4
PB
10939static inline uint16_t add16_sat(uint16_t a, uint16_t b)
10940{
10941 uint16_t res;
10942
10943 res = a + b;
10944 if (((res ^ a) & 0x8000) && !((a ^ b) & 0x8000)) {
10945 if (a & 0x8000)
10946 res = 0x8000;
10947 else
10948 res = 0x7fff;
10949 }
10950 return res;
10951}
10952
1654b2d6 10953/* Perform 8-bit signed saturating addition. */
6ddbc6e4
PB
10954static inline uint8_t add8_sat(uint8_t a, uint8_t b)
10955{
10956 uint8_t res;
10957
10958 res = a + b;
10959 if (((res ^ a) & 0x80) && !((a ^ b) & 0x80)) {
10960 if (a & 0x80)
10961 res = 0x80;
10962 else
10963 res = 0x7f;
10964 }
10965 return res;
10966}
10967
1654b2d6 10968/* Perform 16-bit signed saturating subtraction. */
6ddbc6e4
PB
10969static inline uint16_t sub16_sat(uint16_t a, uint16_t b)
10970{
10971 uint16_t res;
10972
10973 res = a - b;
10974 if (((res ^ a) & 0x8000) && ((a ^ b) & 0x8000)) {
10975 if (a & 0x8000)
10976 res = 0x8000;
10977 else
10978 res = 0x7fff;
10979 }
10980 return res;
10981}
10982
1654b2d6 10983/* Perform 8-bit signed saturating subtraction. */
6ddbc6e4
PB
10984static inline uint8_t sub8_sat(uint8_t a, uint8_t b)
10985{
10986 uint8_t res;
10987
10988 res = a - b;
10989 if (((res ^ a) & 0x80) && ((a ^ b) & 0x80)) {
10990 if (a & 0x80)
10991 res = 0x80;
10992 else
10993 res = 0x7f;
10994 }
10995 return res;
10996}
10997
10998#define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16);
10999#define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16);
11000#define ADD8(a, b, n) RESULT(add8_sat(a, b), n, 8);
11001#define SUB8(a, b, n) RESULT(sub8_sat(a, b), n, 8);
11002#define PFX q
11003
11004#include "op_addsub.h"
11005
11006/* Unsigned saturating arithmetic. */
460a09c1 11007static inline uint16_t add16_usat(uint16_t a, uint16_t b)
6ddbc6e4
PB
11008{
11009 uint16_t res;
11010 res = a + b;
11011 if (res < a)
11012 res = 0xffff;
11013 return res;
11014}
11015
460a09c1 11016static inline uint16_t sub16_usat(uint16_t a, uint16_t b)
6ddbc6e4 11017{
4c4fd3f8 11018 if (a > b)
6ddbc6e4
PB
11019 return a - b;
11020 else
11021 return 0;
11022}
11023
11024static inline uint8_t add8_usat(uint8_t a, uint8_t b)
11025{
11026 uint8_t res;
11027 res = a + b;
11028 if (res < a)
11029 res = 0xff;
11030 return res;
11031}
11032
11033static inline uint8_t sub8_usat(uint8_t a, uint8_t b)
11034{
4c4fd3f8 11035 if (a > b)
6ddbc6e4
PB
11036 return a - b;
11037 else
11038 return 0;
11039}
11040
11041#define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16);
11042#define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16);
11043#define ADD8(a, b, n) RESULT(add8_usat(a, b), n, 8);
11044#define SUB8(a, b, n) RESULT(sub8_usat(a, b), n, 8);
11045#define PFX uq
11046
11047#include "op_addsub.h"
11048
11049/* Signed modulo arithmetic. */
11050#define SARITH16(a, b, n, op) do { \
11051 int32_t sum; \
db6e2e65 11052 sum = (int32_t)(int16_t)(a) op (int32_t)(int16_t)(b); \
6ddbc6e4
PB
11053 RESULT(sum, n, 16); \
11054 if (sum >= 0) \
11055 ge |= 3 << (n * 2); \
11056 } while(0)
11057
11058#define SARITH8(a, b, n, op) do { \
11059 int32_t sum; \
db6e2e65 11060 sum = (int32_t)(int8_t)(a) op (int32_t)(int8_t)(b); \
6ddbc6e4
PB
11061 RESULT(sum, n, 8); \
11062 if (sum >= 0) \
11063 ge |= 1 << n; \
11064 } while(0)
11065
11066
11067#define ADD16(a, b, n) SARITH16(a, b, n, +)
11068#define SUB16(a, b, n) SARITH16(a, b, n, -)
11069#define ADD8(a, b, n) SARITH8(a, b, n, +)
11070#define SUB8(a, b, n) SARITH8(a, b, n, -)
11071#define PFX s
11072#define ARITH_GE
11073
11074#include "op_addsub.h"
11075
11076/* Unsigned modulo arithmetic. */
11077#define ADD16(a, b, n) do { \
11078 uint32_t sum; \
11079 sum = (uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b); \
11080 RESULT(sum, n, 16); \
a87aa10b 11081 if ((sum >> 16) == 1) \
6ddbc6e4
PB
11082 ge |= 3 << (n * 2); \
11083 } while(0)
11084
11085#define ADD8(a, b, n) do { \
11086 uint32_t sum; \
11087 sum = (uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b); \
11088 RESULT(sum, n, 8); \
a87aa10b
AZ
11089 if ((sum >> 8) == 1) \
11090 ge |= 1 << n; \
6ddbc6e4
PB
11091 } while(0)
11092
11093#define SUB16(a, b, n) do { \
11094 uint32_t sum; \
11095 sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \
11096 RESULT(sum, n, 16); \
11097 if ((sum >> 16) == 0) \
11098 ge |= 3 << (n * 2); \
11099 } while(0)
11100
11101#define SUB8(a, b, n) do { \
11102 uint32_t sum; \
11103 sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \
11104 RESULT(sum, n, 8); \
11105 if ((sum >> 8) == 0) \
a87aa10b 11106 ge |= 1 << n; \
6ddbc6e4
PB
11107 } while(0)
11108
11109#define PFX u
11110#define ARITH_GE
11111
11112#include "op_addsub.h"
11113
11114/* Halved signed arithmetic. */
11115#define ADD16(a, b, n) \
11116 RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16)
11117#define SUB16(a, b, n) \
11118 RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16)
11119#define ADD8(a, b, n) \
11120 RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8)
11121#define SUB8(a, b, n) \
11122 RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8)
11123#define PFX sh
11124
11125#include "op_addsub.h"
11126
11127/* Halved unsigned arithmetic. */
11128#define ADD16(a, b, n) \
11129 RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16)
11130#define SUB16(a, b, n) \
11131 RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16)
11132#define ADD8(a, b, n) \
11133 RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8)
11134#define SUB8(a, b, n) \
11135 RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8)
11136#define PFX uh
11137
11138#include "op_addsub.h"
11139
11140static inline uint8_t do_usad(uint8_t a, uint8_t b)
11141{
11142 if (a > b)
11143 return a - b;
11144 else
11145 return b - a;
11146}
11147
11148/* Unsigned sum of absolute byte differences. */
11149uint32_t HELPER(usad8)(uint32_t a, uint32_t b)
11150{
11151 uint32_t sum;
11152 sum = do_usad(a, b);
11153 sum += do_usad(a >> 8, b >> 8);
11154 sum += do_usad(a >> 16, b >>16);
11155 sum += do_usad(a >> 24, b >> 24);
11156 return sum;
11157}
11158
11159/* For ARMv6 SEL instruction. */
11160uint32_t HELPER(sel_flags)(uint32_t flags, uint32_t a, uint32_t b)
11161{
11162 uint32_t mask;
11163
11164 mask = 0;
11165 if (flags & 1)
11166 mask |= 0xff;
11167 if (flags & 2)
11168 mask |= 0xff00;
11169 if (flags & 4)
11170 mask |= 0xff0000;
11171 if (flags & 8)
11172 mask |= 0xff000000;
11173 return (a & mask) | (b & ~mask);
11174}
11175
aa633469
PM
11176/* CRC helpers.
11177 * The upper bytes of val (above the number specified by 'bytes') must have
11178 * been zeroed out by the caller.
11179 */
eb0ecd5a
WN
11180uint32_t HELPER(crc32)(uint32_t acc, uint32_t val, uint32_t bytes)
11181{
11182 uint8_t buf[4];
11183
aa633469 11184 stl_le_p(buf, val);
eb0ecd5a
WN
11185
11186 /* zlib crc32 converts the accumulator and output to one's complement. */
11187 return crc32(acc ^ 0xffffffff, buf, bytes) ^ 0xffffffff;
11188}
11189
11190uint32_t HELPER(crc32c)(uint32_t acc, uint32_t val, uint32_t bytes)
11191{
11192 uint8_t buf[4];
11193
aa633469 11194 stl_le_p(buf, val);
eb0ecd5a
WN
11195
11196 /* Linux crc32c converts the output to one's complement. */
11197 return crc32c(acc, buf, bytes) ^ 0xffffffff;
11198}
a9e01311
RH
11199
11200/* Return the exception level to which FP-disabled exceptions should
11201 * be taken, or 0 if FP is enabled.
11202 */
ced31551 11203int fp_exception_el(CPUARMState *env, int cur_el)
a9e01311 11204{
55faa212 11205#ifndef CONFIG_USER_ONLY
a9e01311 11206 int fpen;
a9e01311
RH
11207
11208 /* CPACR and the CPTR registers don't exist before v6, so FP is
11209 * always accessible
11210 */
11211 if (!arm_feature(env, ARM_FEATURE_V6)) {
11212 return 0;
11213 }
11214
d87513c0
PM
11215 if (arm_feature(env, ARM_FEATURE_M)) {
11216 /* CPACR can cause a NOCP UsageFault taken to current security state */
11217 if (!v7m_cpacr_pass(env, env->v7m.secure, cur_el != 0)) {
11218 return 1;
11219 }
11220
11221 if (arm_feature(env, ARM_FEATURE_M_SECURITY) && !env->v7m.secure) {
11222 if (!extract32(env->v7m.nsacr, 10, 1)) {
11223 /* FP insns cause a NOCP UsageFault taken to Secure */
11224 return 3;
11225 }
11226 }
11227
11228 return 0;
11229 }
11230
a9e01311
RH
11231 /* The CPACR controls traps to EL1, or PL1 if we're 32 bit:
11232 * 0, 2 : trap EL0 and EL1/PL1 accesses
11233 * 1 : trap only EL0 accesses
11234 * 3 : trap no accesses
11235 */
11236 fpen = extract32(env->cp15.cpacr_el1, 20, 2);
11237 switch (fpen) {
11238 case 0:
11239 case 2:
11240 if (cur_el == 0 || cur_el == 1) {
11241 /* Trap to PL1, which might be EL1 or EL3 */
11242 if (arm_is_secure(env) && !arm_el_is_aa64(env, 3)) {
11243 return 3;
11244 }
11245 return 1;
11246 }
11247 if (cur_el == 3 && !is_a64(env)) {
11248 /* Secure PL1 running at EL3 */
11249 return 3;
11250 }
11251 break;
11252 case 1:
11253 if (cur_el == 0) {
11254 return 1;
11255 }
11256 break;
11257 case 3:
11258 break;
11259 }
11260
fc1120a7
PM
11261 /*
11262 * The NSACR allows A-profile AArch32 EL3 and M-profile secure mode
11263 * to control non-secure access to the FPU. It doesn't have any
11264 * effect if EL3 is AArch64 or if EL3 doesn't exist at all.
11265 */
11266 if ((arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
11267 cur_el <= 2 && !arm_is_secure_below_el3(env))) {
11268 if (!extract32(env->cp15.nsacr, 10, 1)) {
11269 /* FP insns act as UNDEF */
11270 return cur_el == 2 ? 2 : 1;
11271 }
11272 }
11273
a9e01311
RH
11274 /* For the CPTR registers we don't need to guard with an ARM_FEATURE
11275 * check because zero bits in the registers mean "don't trap".
11276 */
11277
11278 /* CPTR_EL2 : present in v7VE or v8 */
11279 if (cur_el <= 2 && extract32(env->cp15.cptr_el[2], 10, 1)
11280 && !arm_is_secure_below_el3(env)) {
11281 /* Trap FP ops at EL2, NS-EL1 or NS-EL0 to EL2 */
11282 return 2;
11283 }
11284
11285 /* CPTR_EL3 : present in v8 */
11286 if (extract32(env->cp15.cptr_el[3], 10, 1)) {
11287 /* Trap all FP ops to EL3 */
11288 return 3;
11289 }
55faa212 11290#endif
a9e01311
RH
11291 return 0;
11292}
11293
b9f6033c
RH
11294/* Return the exception level we're running at if this is our mmu_idx */
11295int arm_mmu_idx_to_el(ARMMMUIdx mmu_idx)
11296{
11297 if (mmu_idx & ARM_MMU_IDX_M) {
11298 return mmu_idx & ARM_MMU_IDX_M_PRIV;
11299 }
11300
11301 switch (mmu_idx) {
11302 case ARMMMUIdx_E10_0:
11303 case ARMMMUIdx_E20_0:
11304 case ARMMMUIdx_SE10_0:
11305 return 0;
11306 case ARMMMUIdx_E10_1:
11307 case ARMMMUIdx_SE10_1:
11308 return 1;
11309 case ARMMMUIdx_E2:
11310 case ARMMMUIdx_E20_2:
11311 return 2;
11312 case ARMMMUIdx_SE3:
11313 return 3;
11314 default:
11315 g_assert_not_reached();
11316 }
11317}
11318
7aab5a8c 11319#ifndef CONFIG_TCG
65e4655c
RH
11320ARMMMUIdx arm_v7m_mmu_idx_for_secstate(CPUARMState *env, bool secstate)
11321{
7aab5a8c 11322 g_assert_not_reached();
65e4655c 11323}
7aab5a8c 11324#endif
65e4655c 11325
164690b2 11326ARMMMUIdx arm_mmu_idx_el(CPUARMState *env, int el)
65e4655c 11327{
65e4655c 11328 if (arm_feature(env, ARM_FEATURE_M)) {
50494a27 11329 return arm_v7m_mmu_idx_for_secstate(env, env->v7m.secure);
65e4655c
RH
11330 }
11331
b9f6033c
RH
11332 switch (el) {
11333 case 0:
11334 /* TODO: ARMv8.1-VHE */
11335 if (arm_is_secure_below_el3(env)) {
11336 return ARMMMUIdx_SE10_0;
11337 }
11338 return ARMMMUIdx_E10_0;
11339 case 1:
11340 if (arm_is_secure_below_el3(env)) {
11341 return ARMMMUIdx_SE10_1;
11342 }
11343 return ARMMMUIdx_E10_1;
11344 case 2:
11345 /* TODO: ARMv8.1-VHE */
11346 /* TODO: ARMv8.4-SecEL2 */
11347 return ARMMMUIdx_E2;
11348 case 3:
11349 return ARMMMUIdx_SE3;
11350 default:
11351 g_assert_not_reached();
65e4655c 11352 }
50494a27
RH
11353}
11354
164690b2
RH
11355ARMMMUIdx arm_mmu_idx(CPUARMState *env)
11356{
11357 return arm_mmu_idx_el(env, arm_current_el(env));
11358}
11359
50494a27
RH
11360int cpu_mmu_index(CPUARMState *env, bool ifetch)
11361{
11362 return arm_to_core_mmu_idx(arm_mmu_idx(env));
65e4655c
RH
11363}
11364
64be86ab
RH
11365#ifndef CONFIG_USER_ONLY
11366ARMMMUIdx arm_stage1_mmu_idx(CPUARMState *env)
11367{
11368 return stage_1_mmu_idx(arm_mmu_idx(env));
11369}
11370#endif
11371
fdd1b228
RH
11372static uint32_t rebuild_hflags_common(CPUARMState *env, int fp_el,
11373 ARMMMUIdx mmu_idx, uint32_t flags)
11374{
11375 flags = FIELD_DP32(flags, TBFLAG_ANY, FPEXC_EL, fp_el);
11376 flags = FIELD_DP32(flags, TBFLAG_ANY, MMUIDX,
11377 arm_to_core_mmu_idx(mmu_idx));
11378
fdd1b228
RH
11379 if (arm_singlestep_active(env)) {
11380 flags = FIELD_DP32(flags, TBFLAG_ANY, SS_ACTIVE, 1);
11381 }
11382 return flags;
11383}
11384
43eccfb6
RH
11385static uint32_t rebuild_hflags_common_32(CPUARMState *env, int fp_el,
11386 ARMMMUIdx mmu_idx, uint32_t flags)
11387{
8061a649
RH
11388 bool sctlr_b = arm_sctlr_b(env);
11389
11390 if (sctlr_b) {
11391 flags = FIELD_DP32(flags, TBFLAG_A32, SCTLR_B, 1);
11392 }
11393 if (arm_cpu_data_is_big_endian_a32(env, sctlr_b)) {
11394 flags = FIELD_DP32(flags, TBFLAG_ANY, BE_DATA, 1);
11395 }
43eccfb6
RH
11396 flags = FIELD_DP32(flags, TBFLAG_A32, NS, !access_secure_reg(env));
11397
11398 return rebuild_hflags_common(env, fp_el, mmu_idx, flags);
11399}
11400
6e33ced5
RH
11401static uint32_t rebuild_hflags_m32(CPUARMState *env, int fp_el,
11402 ARMMMUIdx mmu_idx)
11403{
11404 uint32_t flags = 0;
11405
11406 if (arm_v7m_is_handler_mode(env)) {
79cabf1f 11407 flags = FIELD_DP32(flags, TBFLAG_M32, HANDLER, 1);
6e33ced5
RH
11408 }
11409
11410 /*
11411 * v8M always applies stack limit checks unless CCR.STKOFHFNMIGN
11412 * is suppressing them because the requested execution priority
11413 * is less than 0.
11414 */
11415 if (arm_feature(env, ARM_FEATURE_V8) &&
11416 !((mmu_idx & ARM_MMU_IDX_M_NEGPRI) &&
11417 (env->v7m.ccr[env->v7m.secure] & R_V7M_CCR_STKOFHFNMIGN_MASK))) {
79cabf1f 11418 flags = FIELD_DP32(flags, TBFLAG_M32, STACKCHECK, 1);
6e33ced5
RH
11419 }
11420
11421 return rebuild_hflags_common_32(env, fp_el, mmu_idx, flags);
11422}
11423
83f4baef
RH
11424static uint32_t rebuild_hflags_aprofile(CPUARMState *env)
11425{
11426 int flags = 0;
11427
11428 flags = FIELD_DP32(flags, TBFLAG_ANY, DEBUG_TARGET_EL,
11429 arm_debug_target_el(env));
11430 return flags;
11431}
11432
c747224c
RH
11433static uint32_t rebuild_hflags_a32(CPUARMState *env, int fp_el,
11434 ARMMMUIdx mmu_idx)
11435{
83f4baef 11436 uint32_t flags = rebuild_hflags_aprofile(env);
0a54d68e
RH
11437
11438 if (arm_el_is_aa64(env, 1)) {
11439 flags = FIELD_DP32(flags, TBFLAG_A32, VFPEN, 1);
11440 }
5bb0a20b
MZ
11441
11442 if (arm_current_el(env) < 2 && env->cp15.hstr_el2 &&
11443 (arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
11444 flags = FIELD_DP32(flags, TBFLAG_A32, HSTR_ACTIVE, 1);
11445 }
11446
83f4baef 11447 return rebuild_hflags_common_32(env, fp_el, mmu_idx, flags);
c747224c
RH
11448}
11449
d4d7503a
RH
11450static uint32_t rebuild_hflags_a64(CPUARMState *env, int el, int fp_el,
11451 ARMMMUIdx mmu_idx)
a9e01311 11452{
83f4baef 11453 uint32_t flags = rebuild_hflags_aprofile(env);
d4d7503a
RH
11454 ARMMMUIdx stage1 = stage_1_mmu_idx(mmu_idx);
11455 ARMVAParameters p0 = aa64_va_parameters_both(env, 0, stage1);
d4d7503a
RH
11456 uint64_t sctlr;
11457 int tbii, tbid;
b9adaa70 11458
d4d7503a 11459 flags = FIELD_DP32(flags, TBFLAG_ANY, AARCH64_STATE, 1);
cd208a1c 11460
d4d7503a
RH
11461 /* FIXME: ARMv8.1-VHE S2 translation regime. */
11462 if (regime_el(env, stage1) < 2) {
11463 ARMVAParameters p1 = aa64_va_parameters_both(env, -1, stage1);
11464 tbid = (p1.tbi << 1) | p0.tbi;
11465 tbii = tbid & ~((p1.tbid << 1) | p0.tbid);
11466 } else {
11467 tbid = p0.tbi;
11468 tbii = tbid & !p0.tbid;
11469 }
5d8634f5 11470
d4d7503a
RH
11471 flags = FIELD_DP32(flags, TBFLAG_A64, TBII, tbii);
11472 flags = FIELD_DP32(flags, TBFLAG_A64, TBID, tbid);
11473
11474 if (cpu_isar_feature(aa64_sve, env_archcpu(env))) {
11475 int sve_el = sve_exception_el(env, el);
11476 uint32_t zcr_len;
5d8634f5 11477
d4d7503a
RH
11478 /*
11479 * If SVE is disabled, but FP is enabled,
11480 * then the effective len is 0.
11481 */
11482 if (sve_el != 0 && fp_el == 0) {
11483 zcr_len = 0;
11484 } else {
11485 zcr_len = sve_zcr_len_for_el(env, el);
5d8634f5 11486 }
d4d7503a
RH
11487 flags = FIELD_DP32(flags, TBFLAG_A64, SVEEXC_EL, sve_el);
11488 flags = FIELD_DP32(flags, TBFLAG_A64, ZCR_LEN, zcr_len);
11489 }
1db5e96c 11490
d4d7503a 11491 sctlr = arm_sctlr(env, el);
1db5e96c 11492
8061a649
RH
11493 if (arm_cpu_data_is_big_endian_a64(el, sctlr)) {
11494 flags = FIELD_DP32(flags, TBFLAG_ANY, BE_DATA, 1);
11495 }
11496
d4d7503a
RH
11497 if (cpu_isar_feature(aa64_pauth, env_archcpu(env))) {
11498 /*
11499 * In order to save space in flags, we record only whether
11500 * pauth is "inactive", meaning all insns are implemented as
11501 * a nop, or "active" when some action must be performed.
11502 * The decision of which action to take is left to a helper.
11503 */
11504 if (sctlr & (SCTLR_EnIA | SCTLR_EnIB | SCTLR_EnDA | SCTLR_EnDB)) {
11505 flags = FIELD_DP32(flags, TBFLAG_A64, PAUTH_ACTIVE, 1);
1db5e96c 11506 }
d4d7503a 11507 }
0816ef1b 11508
d4d7503a
RH
11509 if (cpu_isar_feature(aa64_bti, env_archcpu(env))) {
11510 /* Note that SCTLR_EL[23].BT == SCTLR_BT1. */
11511 if (sctlr & (el == 0 ? SCTLR_BT0 : SCTLR_BT1)) {
11512 flags = FIELD_DP32(flags, TBFLAG_A64, BT, 1);
0816ef1b 11513 }
d4d7503a 11514 }
08f1434a 11515
d4d7503a
RH
11516 return rebuild_hflags_common(env, fp_el, mmu_idx, flags);
11517}
11518
3d74e2e9
RH
11519static uint32_t rebuild_hflags_internal(CPUARMState *env)
11520{
11521 int el = arm_current_el(env);
11522 int fp_el = fp_exception_el(env, el);
164690b2 11523 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
3d74e2e9
RH
11524
11525 if (is_a64(env)) {
11526 return rebuild_hflags_a64(env, el, fp_el, mmu_idx);
11527 } else if (arm_feature(env, ARM_FEATURE_M)) {
11528 return rebuild_hflags_m32(env, fp_el, mmu_idx);
11529 } else {
11530 return rebuild_hflags_a32(env, fp_el, mmu_idx);
11531 }
11532}
11533
11534void arm_rebuild_hflags(CPUARMState *env)
11535{
11536 env->hflags = rebuild_hflags_internal(env);
11537}
11538
14f3c588
RH
11539void HELPER(rebuild_hflags_m32)(CPUARMState *env, int el)
11540{
11541 int fp_el = fp_exception_el(env, el);
11542 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
11543
11544 env->hflags = rebuild_hflags_m32(env, fp_el, mmu_idx);
11545}
11546
f80741d1
AB
11547/*
11548 * If we have triggered a EL state change we can't rely on the
11549 * translator having passed it too us, we need to recompute.
11550 */
11551void HELPER(rebuild_hflags_a32_newel)(CPUARMState *env)
11552{
11553 int el = arm_current_el(env);
11554 int fp_el = fp_exception_el(env, el);
11555 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
11556 env->hflags = rebuild_hflags_a32(env, fp_el, mmu_idx);
11557}
11558
14f3c588
RH
11559void HELPER(rebuild_hflags_a32)(CPUARMState *env, int el)
11560{
11561 int fp_el = fp_exception_el(env, el);
11562 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
11563
11564 env->hflags = rebuild_hflags_a32(env, fp_el, mmu_idx);
11565}
11566
11567void HELPER(rebuild_hflags_a64)(CPUARMState *env, int el)
11568{
11569 int fp_el = fp_exception_el(env, el);
11570 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
11571
11572 env->hflags = rebuild_hflags_a64(env, el, fp_el, mmu_idx);
11573}
11574
0ee8b24a
PMD
11575static inline void assert_hflags_rebuild_correctly(CPUARMState *env)
11576{
11577#ifdef CONFIG_DEBUG_TCG
11578 uint32_t env_flags_current = env->hflags;
11579 uint32_t env_flags_rebuilt = rebuild_hflags_internal(env);
11580
11581 if (unlikely(env_flags_current != env_flags_rebuilt)) {
11582 fprintf(stderr, "TCG hflags mismatch (current:0x%08x rebuilt:0x%08x)\n",
11583 env_flags_current, env_flags_rebuilt);
11584 abort();
11585 }
11586#endif
11587}
11588
d4d7503a
RH
11589void cpu_get_tb_cpu_state(CPUARMState *env, target_ulong *pc,
11590 target_ulong *cs_base, uint32_t *pflags)
11591{
e979972a
RH
11592 uint32_t flags = env->hflags;
11593 uint32_t pstate_for_ss;
d4d7503a 11594
9b253fe5 11595 *cs_base = 0;
0ee8b24a 11596 assert_hflags_rebuild_correctly(env);
3d74e2e9 11597
e979972a 11598 if (FIELD_EX32(flags, TBFLAG_ANY, AARCH64_STATE)) {
d4d7503a 11599 *pc = env->pc;
d4d7503a 11600 if (cpu_isar_feature(aa64_bti, env_archcpu(env))) {
08f1434a
RH
11601 flags = FIELD_DP32(flags, TBFLAG_A64, BTYPE, env->btype);
11602 }
60e12c37 11603 pstate_for_ss = env->pstate;
a9e01311
RH
11604 } else {
11605 *pc = env->regs[15];
6e33ced5
RH
11606
11607 if (arm_feature(env, ARM_FEATURE_M)) {
9550d1bd
RH
11608 if (arm_feature(env, ARM_FEATURE_M_SECURITY) &&
11609 FIELD_EX32(env->v7m.fpccr[M_REG_S], V7M_FPCCR, S)
11610 != env->v7m.secure) {
79cabf1f 11611 flags = FIELD_DP32(flags, TBFLAG_M32, FPCCR_S_WRONG, 1);
9550d1bd
RH
11612 }
11613
11614 if ((env->v7m.fpccr[env->v7m.secure] & R_V7M_FPCCR_ASPEN_MASK) &&
11615 (!(env->v7m.control[M_REG_S] & R_V7M_CONTROL_FPCA_MASK) ||
11616 (env->v7m.secure &&
11617 !(env->v7m.control[M_REG_S] & R_V7M_CONTROL_SFPA_MASK)))) {
11618 /*
11619 * ASPEN is set, but FPCA/SFPA indicate that there is no
11620 * active FP context; we must create a new FP context before
11621 * executing any FP insn.
11622 */
79cabf1f 11623 flags = FIELD_DP32(flags, TBFLAG_M32, NEW_FP_CTXT_NEEDED, 1);
9550d1bd
RH
11624 }
11625
11626 bool is_secure = env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_S_MASK;
11627 if (env->v7m.fpccr[is_secure] & R_V7M_FPCCR_LSPACT_MASK) {
79cabf1f 11628 flags = FIELD_DP32(flags, TBFLAG_M32, LSPACT, 1);
9550d1bd 11629 }
6e33ced5 11630 } else {
bbad7c62
RH
11631 /*
11632 * Note that XSCALE_CPAR shares bits with VECSTRIDE.
11633 * Note that VECLEN+VECSTRIDE are RES0 for M-profile.
11634 */
11635 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
11636 flags = FIELD_DP32(flags, TBFLAG_A32,
11637 XSCALE_CPAR, env->cp15.c15_cpar);
11638 } else {
11639 flags = FIELD_DP32(flags, TBFLAG_A32, VECLEN,
11640 env->vfp.vec_len);
11641 flags = FIELD_DP32(flags, TBFLAG_A32, VECSTRIDE,
11642 env->vfp.vec_stride);
11643 }
0a54d68e
RH
11644 if (env->vfp.xregs[ARM_VFP_FPEXC] & (1 << 30)) {
11645 flags = FIELD_DP32(flags, TBFLAG_A32, VFPEN, 1);
11646 }
6e33ced5
RH
11647 }
11648
79cabf1f
RH
11649 flags = FIELD_DP32(flags, TBFLAG_AM32, THUMB, env->thumb);
11650 flags = FIELD_DP32(flags, TBFLAG_AM32, CONDEXEC, env->condexec_bits);
60e12c37 11651 pstate_for_ss = env->uncached_cpsr;
d4d7503a 11652 }
a9e01311 11653
60e12c37
RH
11654 /*
11655 * The SS_ACTIVE and PSTATE_SS bits correspond to the state machine
a9e01311
RH
11656 * states defined in the ARM ARM for software singlestep:
11657 * SS_ACTIVE PSTATE.SS State
11658 * 0 x Inactive (the TB flag for SS is always 0)
11659 * 1 0 Active-pending
11660 * 1 1 Active-not-pending
fdd1b228 11661 * SS_ACTIVE is set in hflags; PSTATE_SS is computed every TB.
a9e01311 11662 */
60e12c37
RH
11663 if (FIELD_EX32(flags, TBFLAG_ANY, SS_ACTIVE) &&
11664 (pstate_for_ss & PSTATE_SS)) {
11665 flags = FIELD_DP32(flags, TBFLAG_ANY, PSTATE_SS, 1);
a9e01311 11666 }
a9e01311 11667
b9adaa70 11668 *pflags = flags;
a9e01311 11669}
0ab5953b
RH
11670
11671#ifdef TARGET_AARCH64
11672/*
11673 * The manual says that when SVE is enabled and VQ is widened the
11674 * implementation is allowed to zero the previously inaccessible
11675 * portion of the registers. The corollary to that is that when
11676 * SVE is enabled and VQ is narrowed we are also allowed to zero
11677 * the now inaccessible portion of the registers.
11678 *
11679 * The intent of this is that no predicate bit beyond VQ is ever set.
11680 * Which means that some operations on predicate registers themselves
11681 * may operate on full uint64_t or even unrolled across the maximum
11682 * uint64_t[4]. Performing 4 bits of host arithmetic unconditionally
11683 * may well be cheaper than conditionals to restrict the operation
11684 * to the relevant portion of a uint16_t[16].
11685 */
11686void aarch64_sve_narrow_vq(CPUARMState *env, unsigned vq)
11687{
11688 int i, j;
11689 uint64_t pmask;
11690
11691 assert(vq >= 1 && vq <= ARM_MAX_VQ);
2fc0cc0e 11692 assert(vq <= env_archcpu(env)->sve_max_vq);
0ab5953b
RH
11693
11694 /* Zap the high bits of the zregs. */
11695 for (i = 0; i < 32; i++) {
11696 memset(&env->vfp.zregs[i].d[2 * vq], 0, 16 * (ARM_MAX_VQ - vq));
11697 }
11698
11699 /* Zap the high bits of the pregs and ffr. */
11700 pmask = 0;
11701 if (vq & 3) {
11702 pmask = ~(-1ULL << (16 * (vq & 3)));
11703 }
11704 for (j = vq / 4; j < ARM_MAX_VQ / 4; j++) {
11705 for (i = 0; i < 17; ++i) {
11706 env->vfp.pregs[i].p[j] &= pmask;
11707 }
11708 pmask = 0;
11709 }
11710}
11711
11712/*
11713 * Notice a change in SVE vector size when changing EL.
11714 */
9a05f7b6
RH
11715void aarch64_sve_change_el(CPUARMState *env, int old_el,
11716 int new_el, bool el0_a64)
0ab5953b 11717{
2fc0cc0e 11718 ARMCPU *cpu = env_archcpu(env);
0ab5953b 11719 int old_len, new_len;
9a05f7b6 11720 bool old_a64, new_a64;
0ab5953b
RH
11721
11722 /* Nothing to do if no SVE. */
cd208a1c 11723 if (!cpu_isar_feature(aa64_sve, cpu)) {
0ab5953b
RH
11724 return;
11725 }
11726
11727 /* Nothing to do if FP is disabled in either EL. */
11728 if (fp_exception_el(env, old_el) || fp_exception_el(env, new_el)) {
11729 return;
11730 }
11731
11732 /*
11733 * DDI0584A.d sec 3.2: "If SVE instructions are disabled or trapped
11734 * at ELx, or not available because the EL is in AArch32 state, then
11735 * for all purposes other than a direct read, the ZCR_ELx.LEN field
11736 * has an effective value of 0".
11737 *
11738 * Consider EL2 (aa64, vq=4) -> EL0 (aa32) -> EL1 (aa64, vq=0).
11739 * If we ignore aa32 state, we would fail to see the vq4->vq0 transition
11740 * from EL2->EL1. Thus we go ahead and narrow when entering aa32 so that
11741 * we already have the correct register contents when encountering the
11742 * vq0->vq0 transition between EL0->EL1.
11743 */
9a05f7b6
RH
11744 old_a64 = old_el ? arm_el_is_aa64(env, old_el) : el0_a64;
11745 old_len = (old_a64 && !sve_exception_el(env, old_el)
0ab5953b 11746 ? sve_zcr_len_for_el(env, old_el) : 0);
9a05f7b6
RH
11747 new_a64 = new_el ? arm_el_is_aa64(env, new_el) : el0_a64;
11748 new_len = (new_a64 && !sve_exception_el(env, new_el)
0ab5953b
RH
11749 ? sve_zcr_len_for_el(env, new_el) : 0);
11750
11751 /* When changing vector length, clear inaccessible state. */
11752 if (new_len < old_len) {
11753 aarch64_sve_narrow_vq(env, new_len + 1);
11754 }
11755}
11756#endif