]> git.proxmox.com Git - mirror_qemu.git/blame - target/arm/helper.c
target/arm: Split out alle1_tlbmask
[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) */
2fc0cc0e 617 ARMCPU *cpu = env_archcpu(env);
00c8cb0a 618
b4ab8ce9
PM
619 if (tlb_force_broadcast(env)) {
620 tlbiall_is_write(env, NULL, value);
621 return;
622 }
623
d10eb08f 624 tlb_flush(CPU(cpu));
d929823f
PM
625}
626
c4241c7d
PM
627static void tlbimva_write(CPUARMState *env, const ARMCPRegInfo *ri,
628 uint64_t value)
d929823f
PM
629{
630 /* Invalidate single TLB entry by MVA and ASID (TLBIMVA) */
2fc0cc0e 631 ARMCPU *cpu = env_archcpu(env);
31b030d4 632
b4ab8ce9
PM
633 if (tlb_force_broadcast(env)) {
634 tlbimva_is_write(env, NULL, value);
635 return;
636 }
637
31b030d4 638 tlb_flush_page(CPU(cpu), value & TARGET_PAGE_MASK);
d929823f
PM
639}
640
c4241c7d
PM
641static void tlbiasid_write(CPUARMState *env, const ARMCPRegInfo *ri,
642 uint64_t value)
d929823f
PM
643{
644 /* Invalidate by ASID (TLBIASID) */
2fc0cc0e 645 ARMCPU *cpu = env_archcpu(env);
00c8cb0a 646
b4ab8ce9
PM
647 if (tlb_force_broadcast(env)) {
648 tlbiasid_is_write(env, NULL, value);
649 return;
650 }
651
d10eb08f 652 tlb_flush(CPU(cpu));
d929823f
PM
653}
654
c4241c7d
PM
655static void tlbimvaa_write(CPUARMState *env, const ARMCPRegInfo *ri,
656 uint64_t value)
d929823f
PM
657{
658 /* Invalidate single entry by MVA, all ASIDs (TLBIMVAA) */
2fc0cc0e 659 ARMCPU *cpu = env_archcpu(env);
31b030d4 660
b4ab8ce9
PM
661 if (tlb_force_broadcast(env)) {
662 tlbimvaa_is_write(env, NULL, value);
663 return;
664 }
fa439fc5 665
b4ab8ce9 666 tlb_flush_page(CPU(cpu), value & TARGET_PAGE_MASK);
fa439fc5
PM
667}
668
541ef8c2
SS
669static void tlbiall_nsnh_write(CPUARMState *env, const ARMCPRegInfo *ri,
670 uint64_t value)
671{
29a0af61 672 CPUState *cs = env_cpu(env);
541ef8c2 673
0336cbf8 674 tlb_flush_by_mmuidx(cs,
8bd5c820
PM
675 ARMMMUIdxBit_S12NSE1 |
676 ARMMMUIdxBit_S12NSE0 |
677 ARMMMUIdxBit_S2NS);
541ef8c2
SS
678}
679
680static void tlbiall_nsnh_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
681 uint64_t value)
682{
29a0af61 683 CPUState *cs = env_cpu(env);
541ef8c2 684
a67cf277 685 tlb_flush_by_mmuidx_all_cpus_synced(cs,
8bd5c820
PM
686 ARMMMUIdxBit_S12NSE1 |
687 ARMMMUIdxBit_S12NSE0 |
688 ARMMMUIdxBit_S2NS);
541ef8c2
SS
689}
690
691static void tlbiipas2_write(CPUARMState *env, const ARMCPRegInfo *ri,
692 uint64_t value)
693{
694 /* Invalidate by IPA. This has to invalidate any structures that
695 * contain only stage 2 translation information, but does not need
696 * to apply to structures that contain combined stage 1 and stage 2
697 * translation information.
698 * This must NOP if EL2 isn't implemented or SCR_EL3.NS is zero.
699 */
29a0af61 700 CPUState *cs = env_cpu(env);
541ef8c2
SS
701 uint64_t pageaddr;
702
703 if (!arm_feature(env, ARM_FEATURE_EL2) || !(env->cp15.scr_el3 & SCR_NS)) {
704 return;
705 }
706
707 pageaddr = sextract64(value << 12, 0, 40);
708
8bd5c820 709 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_S2NS);
541ef8c2
SS
710}
711
712static void tlbiipas2_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
713 uint64_t value)
714{
29a0af61 715 CPUState *cs = env_cpu(env);
541ef8c2
SS
716 uint64_t pageaddr;
717
718 if (!arm_feature(env, ARM_FEATURE_EL2) || !(env->cp15.scr_el3 & SCR_NS)) {
719 return;
720 }
721
722 pageaddr = sextract64(value << 12, 0, 40);
723
a67cf277 724 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
8bd5c820 725 ARMMMUIdxBit_S2NS);
541ef8c2
SS
726}
727
728static void tlbiall_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
729 uint64_t value)
730{
29a0af61 731 CPUState *cs = env_cpu(env);
541ef8c2 732
8bd5c820 733 tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_S1E2);
541ef8c2
SS
734}
735
736static void tlbiall_hyp_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
737 uint64_t value)
738{
29a0af61 739 CPUState *cs = env_cpu(env);
541ef8c2 740
8bd5c820 741 tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_S1E2);
541ef8c2
SS
742}
743
744static void tlbimva_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
745 uint64_t value)
746{
29a0af61 747 CPUState *cs = env_cpu(env);
541ef8c2
SS
748 uint64_t pageaddr = value & ~MAKE_64BIT_MASK(0, 12);
749
8bd5c820 750 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_S1E2);
541ef8c2
SS
751}
752
753static void tlbimva_hyp_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
754 uint64_t value)
755{
29a0af61 756 CPUState *cs = env_cpu(env);
541ef8c2
SS
757 uint64_t pageaddr = value & ~MAKE_64BIT_MASK(0, 12);
758
a67cf277 759 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
8bd5c820 760 ARMMMUIdxBit_S1E2);
541ef8c2
SS
761}
762
e9aa6c21 763static const ARMCPRegInfo cp_reginfo[] = {
54bf36ed
FA
764 /* Define the secure and non-secure FCSE identifier CP registers
765 * separately because there is no secure bank in V8 (no _EL3). This allows
766 * the secure register to be properly reset and migrated. There is also no
767 * v8 EL1 version of the register so the non-secure instance stands alone.
768 */
9c513e78 769 { .name = "FCSEIDR",
54bf36ed
FA
770 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
771 .access = PL1_RW, .secure = ARM_CP_SECSTATE_NS,
772 .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_ns),
773 .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
9c513e78 774 { .name = "FCSEIDR_S",
54bf36ed
FA
775 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
776 .access = PL1_RW, .secure = ARM_CP_SECSTATE_S,
777 .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_s),
d4e6df63 778 .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
54bf36ed
FA
779 /* Define the secure and non-secure context identifier CP registers
780 * separately because there is no secure bank in V8 (no _EL3). This allows
781 * the secure register to be properly reset and migrated. In the
782 * non-secure case, the 32-bit register will have reset and migration
783 * disabled during registration as it is handled by the 64-bit instance.
784 */
785 { .name = "CONTEXTIDR_EL1", .state = ARM_CP_STATE_BOTH,
014406b5 786 .opc0 = 3, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
54bf36ed
FA
787 .access = PL1_RW, .secure = ARM_CP_SECSTATE_NS,
788 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[1]),
789 .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
9c513e78 790 { .name = "CONTEXTIDR_S", .state = ARM_CP_STATE_AA32,
54bf36ed
FA
791 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
792 .access = PL1_RW, .secure = ARM_CP_SECSTATE_S,
793 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_s),
d4e6df63 794 .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
9449fdf6
PM
795 REGINFO_SENTINEL
796};
797
798static const ARMCPRegInfo not_v8_cp_reginfo[] = {
799 /* NB: Some of these registers exist in v8 but with more precise
800 * definitions that don't use CP_ANY wildcards (mostly in v8_cp_reginfo[]).
801 */
802 /* MMU Domain access control / MPU write buffer control */
0c17d68c
FA
803 { .name = "DACR",
804 .cp = 15, .opc1 = CP_ANY, .crn = 3, .crm = CP_ANY, .opc2 = CP_ANY,
805 .access = PL1_RW, .resetvalue = 0,
806 .writefn = dacr_write, .raw_writefn = raw_write,
807 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
808 offsetoflow32(CPUARMState, cp15.dacr_ns) } },
a903c449
EI
809 /* ARMv7 allocates a range of implementation defined TLB LOCKDOWN regs.
810 * For v6 and v5, these mappings are overly broad.
4fdd17dd 811 */
a903c449
EI
812 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 0,
813 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
814 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 1,
815 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
816 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 4,
817 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
818 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 8,
4fdd17dd 819 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
c4804214
PM
820 /* Cache maintenance ops; some of this space may be overridden later. */
821 { .name = "CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
822 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
823 .type = ARM_CP_NOP | ARM_CP_OVERRIDE },
e9aa6c21
PM
824 REGINFO_SENTINEL
825};
826
7d57f408
PM
827static const ARMCPRegInfo not_v6_cp_reginfo[] = {
828 /* Not all pre-v6 cores implemented this WFI, so this is slightly
829 * over-broad.
830 */
831 { .name = "WFI_v5", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = 2,
832 .access = PL1_W, .type = ARM_CP_WFI },
833 REGINFO_SENTINEL
834};
835
836static const ARMCPRegInfo not_v7_cp_reginfo[] = {
837 /* Standard v6 WFI (also used in some pre-v6 cores); not in v7 (which
838 * is UNPREDICTABLE; we choose to NOP as most implementations do).
839 */
840 { .name = "WFI_v6", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
841 .access = PL1_W, .type = ARM_CP_WFI },
34f90529
PM
842 /* L1 cache lockdown. Not architectural in v6 and earlier but in practice
843 * implemented in 926, 946, 1026, 1136, 1176 and 11MPCore. StrongARM and
844 * OMAPCP will override this space.
845 */
846 { .name = "DLOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 0,
847 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_data),
848 .resetvalue = 0 },
849 { .name = "ILOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 1,
850 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_insn),
851 .resetvalue = 0 },
776d4e5c
PM
852 /* v6 doesn't have the cache ID registers but Linux reads them anyway */
853 { .name = "DUMMY", .cp = 15, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = CP_ANY,
7a0e58fa 854 .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 855 .resetvalue = 0 },
50300698
PM
856 /* We don't implement pre-v7 debug but most CPUs had at least a DBGDIDR;
857 * implementing it as RAZ means the "debug architecture version" bits
858 * will read as a reserved value, which should cause Linux to not try
859 * to use the debug hardware.
860 */
861 { .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0,
862 .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
995939a6
PM
863 /* MMU TLB control. Note that the wildcarding means we cover not just
864 * the unified TLB ops but also the dside/iside/inner-shareable variants.
865 */
866 { .name = "TLBIALL", .cp = 15, .crn = 8, .crm = CP_ANY,
867 .opc1 = CP_ANY, .opc2 = 0, .access = PL1_W, .writefn = tlbiall_write,
7a0e58fa 868 .type = ARM_CP_NO_RAW },
995939a6
PM
869 { .name = "TLBIMVA", .cp = 15, .crn = 8, .crm = CP_ANY,
870 .opc1 = CP_ANY, .opc2 = 1, .access = PL1_W, .writefn = tlbimva_write,
7a0e58fa 871 .type = ARM_CP_NO_RAW },
995939a6
PM
872 { .name = "TLBIASID", .cp = 15, .crn = 8, .crm = CP_ANY,
873 .opc1 = CP_ANY, .opc2 = 2, .access = PL1_W, .writefn = tlbiasid_write,
7a0e58fa 874 .type = ARM_CP_NO_RAW },
995939a6
PM
875 { .name = "TLBIMVAA", .cp = 15, .crn = 8, .crm = CP_ANY,
876 .opc1 = CP_ANY, .opc2 = 3, .access = PL1_W, .writefn = tlbimvaa_write,
7a0e58fa 877 .type = ARM_CP_NO_RAW },
a903c449
EI
878 { .name = "PRRR", .cp = 15, .crn = 10, .crm = 2,
879 .opc1 = 0, .opc2 = 0, .access = PL1_RW, .type = ARM_CP_NOP },
880 { .name = "NMRR", .cp = 15, .crn = 10, .crm = 2,
881 .opc1 = 0, .opc2 = 1, .access = PL1_RW, .type = ARM_CP_NOP },
7d57f408
PM
882 REGINFO_SENTINEL
883};
884
c4241c7d
PM
885static void cpacr_write(CPUARMState *env, const ARMCPRegInfo *ri,
886 uint64_t value)
2771db27 887{
f0aff255
FA
888 uint32_t mask = 0;
889
890 /* In ARMv8 most bits of CPACR_EL1 are RES0. */
891 if (!arm_feature(env, ARM_FEATURE_V8)) {
892 /* ARMv7 defines bits for unimplemented coprocessors as RAZ/WI.
893 * ASEDIS [31] and D32DIS [30] are both UNK/SBZP without VFP.
894 * TRCDIS [28] is RAZ/WI since we do not implement a trace macrocell.
895 */
896 if (arm_feature(env, ARM_FEATURE_VFP)) {
897 /* VFP coprocessor: cp10 & cp11 [23:20] */
898 mask |= (1 << 31) | (1 << 30) | (0xf << 20);
899
900 if (!arm_feature(env, ARM_FEATURE_NEON)) {
901 /* ASEDIS [31] bit is RAO/WI */
902 value |= (1 << 31);
903 }
904
905 /* VFPv3 and upwards with NEON implement 32 double precision
906 * registers (D0-D31).
907 */
908 if (!arm_feature(env, ARM_FEATURE_NEON) ||
909 !arm_feature(env, ARM_FEATURE_VFP3)) {
910 /* D32DIS [30] is RAO/WI if D16-31 are not implemented. */
911 value |= (1 << 30);
912 }
913 }
914 value &= mask;
2771db27 915 }
fc1120a7
PM
916
917 /*
918 * For A-profile AArch32 EL3 (but not M-profile secure mode), if NSACR.CP10
919 * is 0 then CPACR.{CP11,CP10} ignore writes and read as 0b00.
920 */
921 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
922 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
923 value &= ~(0xf << 20);
924 value |= env->cp15.cpacr_el1 & (0xf << 20);
925 }
926
7ebd5f2e 927 env->cp15.cpacr_el1 = value;
2771db27
PM
928}
929
fc1120a7
PM
930static uint64_t cpacr_read(CPUARMState *env, const ARMCPRegInfo *ri)
931{
932 /*
933 * For A-profile AArch32 EL3 (but not M-profile secure mode), if NSACR.CP10
934 * is 0 then CPACR.{CP11,CP10} ignore writes and read as 0b00.
935 */
936 uint64_t value = env->cp15.cpacr_el1;
937
938 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
939 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
940 value &= ~(0xf << 20);
941 }
942 return value;
943}
944
945
5deac39c
PM
946static void cpacr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
947{
948 /* Call cpacr_write() so that we reset with the correct RAO bits set
949 * for our CPU features.
950 */
951 cpacr_write(env, ri, 0);
952}
953
3f208fd7
PM
954static CPAccessResult cpacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
955 bool isread)
c6f19164
GB
956{
957 if (arm_feature(env, ARM_FEATURE_V8)) {
958 /* Check if CPACR accesses are to be trapped to EL2 */
959 if (arm_current_el(env) == 1 &&
960 (env->cp15.cptr_el[2] & CPTR_TCPAC) && !arm_is_secure(env)) {
961 return CP_ACCESS_TRAP_EL2;
962 /* Check if CPACR accesses are to be trapped to EL3 */
963 } else if (arm_current_el(env) < 3 &&
964 (env->cp15.cptr_el[3] & CPTR_TCPAC)) {
965 return CP_ACCESS_TRAP_EL3;
966 }
967 }
968
969 return CP_ACCESS_OK;
970}
971
3f208fd7
PM
972static CPAccessResult cptr_access(CPUARMState *env, const ARMCPRegInfo *ri,
973 bool isread)
c6f19164
GB
974{
975 /* Check if CPTR accesses are set to trap to EL3 */
976 if (arm_current_el(env) == 2 && (env->cp15.cptr_el[3] & CPTR_TCPAC)) {
977 return CP_ACCESS_TRAP_EL3;
978 }
979
980 return CP_ACCESS_OK;
981}
982
7d57f408
PM
983static const ARMCPRegInfo v6_cp_reginfo[] = {
984 /* prefetch by MVA in v6, NOP in v7 */
985 { .name = "MVA_prefetch",
986 .cp = 15, .crn = 7, .crm = 13, .opc1 = 0, .opc2 = 1,
987 .access = PL1_W, .type = ARM_CP_NOP },
6df99dec
SS
988 /* We need to break the TB after ISB to execute self-modifying code
989 * correctly and also to take any pending interrupts immediately.
990 * So use arm_cp_write_ignore() function instead of ARM_CP_NOP flag.
991 */
7d57f408 992 { .name = "ISB", .cp = 15, .crn = 7, .crm = 5, .opc1 = 0, .opc2 = 4,
6df99dec 993 .access = PL0_W, .type = ARM_CP_NO_RAW, .writefn = arm_cp_write_ignore },
091fd17c 994 { .name = "DSB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 4,
7d57f408 995 .access = PL0_W, .type = ARM_CP_NOP },
091fd17c 996 { .name = "DMB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 5,
7d57f408 997 .access = PL0_W, .type = ARM_CP_NOP },
06d76f31 998 { .name = "IFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 2,
6cd8a264 999 .access = PL1_RW,
b848ce2b
FA
1000 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ifar_s),
1001 offsetof(CPUARMState, cp15.ifar_ns) },
06d76f31
PM
1002 .resetvalue = 0, },
1003 /* Watchpoint Fault Address Register : should actually only be present
1004 * for 1136, 1176, 11MPCore.
1005 */
1006 { .name = "WFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 1,
1007 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0, },
34222fb8 1008 { .name = "CPACR", .state = ARM_CP_STATE_BOTH, .opc0 = 3,
c6f19164 1009 .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 2, .accessfn = cpacr_access,
7ebd5f2e 1010 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.cpacr_el1),
fc1120a7 1011 .resetfn = cpacr_reset, .writefn = cpacr_write, .readfn = cpacr_read },
7d57f408
PM
1012 REGINFO_SENTINEL
1013};
1014
7ece99b1
AL
1015/* Definitions for the PMU registers */
1016#define PMCRN_MASK 0xf800
1017#define PMCRN_SHIFT 11
f4efb4b2 1018#define PMCRLC 0x40
033614c4 1019#define PMCRDP 0x10
7ece99b1
AL
1020#define PMCRD 0x8
1021#define PMCRC 0x4
5ecdd3e4 1022#define PMCRP 0x2
7ece99b1
AL
1023#define PMCRE 0x1
1024
033614c4
AL
1025#define PMXEVTYPER_P 0x80000000
1026#define PMXEVTYPER_U 0x40000000
1027#define PMXEVTYPER_NSK 0x20000000
1028#define PMXEVTYPER_NSU 0x10000000
1029#define PMXEVTYPER_NSH 0x08000000
1030#define PMXEVTYPER_M 0x04000000
1031#define PMXEVTYPER_MT 0x02000000
1032#define PMXEVTYPER_EVTCOUNT 0x0000ffff
1033#define PMXEVTYPER_MASK (PMXEVTYPER_P | PMXEVTYPER_U | PMXEVTYPER_NSK | \
1034 PMXEVTYPER_NSU | PMXEVTYPER_NSH | \
1035 PMXEVTYPER_M | PMXEVTYPER_MT | \
1036 PMXEVTYPER_EVTCOUNT)
1037
4b8afa1f
AL
1038#define PMCCFILTR 0xf8000000
1039#define PMCCFILTR_M PMXEVTYPER_M
1040#define PMCCFILTR_EL0 (PMCCFILTR | PMCCFILTR_M)
1041
7ece99b1
AL
1042static inline uint32_t pmu_num_counters(CPUARMState *env)
1043{
1044 return (env->cp15.c9_pmcr & PMCRN_MASK) >> PMCRN_SHIFT;
1045}
1046
1047/* Bits allowed to be set/cleared for PMCNTEN* and PMINTEN* */
1048static inline uint64_t pmu_counter_mask(CPUARMState *env)
1049{
1050 return (1 << 31) | ((1 << pmu_num_counters(env)) - 1);
1051}
1052
57a4a11b
AL
1053typedef struct pm_event {
1054 uint16_t number; /* PMEVTYPER.evtCount is 16 bits wide */
1055 /* If the event is supported on this CPU (used to generate PMCEID[01]) */
1056 bool (*supported)(CPUARMState *);
1057 /*
1058 * Retrieve the current count of the underlying event. The programmed
1059 * counters hold a difference from the return value from this function
1060 */
1061 uint64_t (*get_count)(CPUARMState *);
4e7beb0c
AL
1062 /*
1063 * Return how many nanoseconds it will take (at a minimum) for count events
1064 * to occur. A negative value indicates the counter will never overflow, or
1065 * that the counter has otherwise arranged for the overflow bit to be set
1066 * and the PMU interrupt to be raised on overflow.
1067 */
1068 int64_t (*ns_per_count)(uint64_t);
57a4a11b
AL
1069} pm_event;
1070
b2e23725
AL
1071static bool event_always_supported(CPUARMState *env)
1072{
1073 return true;
1074}
1075
0d4bfd7d
AL
1076static uint64_t swinc_get_count(CPUARMState *env)
1077{
1078 /*
1079 * SW_INCR events are written directly to the pmevcntr's by writes to
1080 * PMSWINC, so there is no underlying count maintained by the PMU itself
1081 */
1082 return 0;
1083}
1084
4e7beb0c
AL
1085static int64_t swinc_ns_per(uint64_t ignored)
1086{
1087 return -1;
1088}
1089
b2e23725
AL
1090/*
1091 * Return the underlying cycle count for the PMU cycle counters. If we're in
1092 * usermode, simply return 0.
1093 */
1094static uint64_t cycles_get_count(CPUARMState *env)
1095{
1096#ifndef CONFIG_USER_ONLY
1097 return muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
1098 ARM_CPU_FREQ, NANOSECONDS_PER_SECOND);
1099#else
1100 return cpu_get_host_ticks();
1101#endif
1102}
1103
1104#ifndef CONFIG_USER_ONLY
4e7beb0c
AL
1105static int64_t cycles_ns_per(uint64_t cycles)
1106{
1107 return (ARM_CPU_FREQ / NANOSECONDS_PER_SECOND) * cycles;
1108}
1109
b2e23725
AL
1110static bool instructions_supported(CPUARMState *env)
1111{
1112 return use_icount == 1 /* Precise instruction counting */;
1113}
1114
1115static uint64_t instructions_get_count(CPUARMState *env)
1116{
1117 return (uint64_t)cpu_get_icount_raw();
1118}
4e7beb0c
AL
1119
1120static int64_t instructions_ns_per(uint64_t icount)
1121{
1122 return cpu_icount_to_ns((int64_t)icount);
1123}
b2e23725
AL
1124#endif
1125
57a4a11b 1126static const pm_event pm_events[] = {
0d4bfd7d
AL
1127 { .number = 0x000, /* SW_INCR */
1128 .supported = event_always_supported,
1129 .get_count = swinc_get_count,
4e7beb0c 1130 .ns_per_count = swinc_ns_per,
0d4bfd7d 1131 },
b2e23725
AL
1132#ifndef CONFIG_USER_ONLY
1133 { .number = 0x008, /* INST_RETIRED, Instruction architecturally executed */
1134 .supported = instructions_supported,
1135 .get_count = instructions_get_count,
4e7beb0c 1136 .ns_per_count = instructions_ns_per,
b2e23725
AL
1137 },
1138 { .number = 0x011, /* CPU_CYCLES, Cycle */
1139 .supported = event_always_supported,
1140 .get_count = cycles_get_count,
4e7beb0c 1141 .ns_per_count = cycles_ns_per,
b2e23725
AL
1142 }
1143#endif
57a4a11b
AL
1144};
1145
1146/*
1147 * Note: Before increasing MAX_EVENT_ID beyond 0x3f into the 0x40xx range of
1148 * events (i.e. the statistical profiling extension), this implementation
1149 * should first be updated to something sparse instead of the current
1150 * supported_event_map[] array.
1151 */
b2e23725 1152#define MAX_EVENT_ID 0x11
57a4a11b
AL
1153#define UNSUPPORTED_EVENT UINT16_MAX
1154static uint16_t supported_event_map[MAX_EVENT_ID + 1];
1155
1156/*
bf8d0969
AL
1157 * Called upon CPU initialization to initialize PMCEID[01]_EL0 and build a map
1158 * of ARM event numbers to indices in our pm_events array.
57a4a11b
AL
1159 *
1160 * Note: Events in the 0x40XX range are not currently supported.
1161 */
bf8d0969 1162void pmu_init(ARMCPU *cpu)
57a4a11b 1163{
57a4a11b
AL
1164 unsigned int i;
1165
bf8d0969
AL
1166 /*
1167 * Empty supported_event_map and cpu->pmceid[01] before adding supported
1168 * events to them
1169 */
57a4a11b
AL
1170 for (i = 0; i < ARRAY_SIZE(supported_event_map); i++) {
1171 supported_event_map[i] = UNSUPPORTED_EVENT;
1172 }
bf8d0969
AL
1173 cpu->pmceid0 = 0;
1174 cpu->pmceid1 = 0;
57a4a11b
AL
1175
1176 for (i = 0; i < ARRAY_SIZE(pm_events); i++) {
1177 const pm_event *cnt = &pm_events[i];
1178 assert(cnt->number <= MAX_EVENT_ID);
1179 /* We do not currently support events in the 0x40xx range */
1180 assert(cnt->number <= 0x3f);
1181
bf8d0969 1182 if (cnt->supported(&cpu->env)) {
57a4a11b 1183 supported_event_map[cnt->number] = i;
67da43d6 1184 uint64_t event_mask = 1ULL << (cnt->number & 0x1f);
bf8d0969
AL
1185 if (cnt->number & 0x20) {
1186 cpu->pmceid1 |= event_mask;
1187 } else {
1188 cpu->pmceid0 |= event_mask;
1189 }
57a4a11b
AL
1190 }
1191 }
57a4a11b
AL
1192}
1193
5ecdd3e4
AL
1194/*
1195 * Check at runtime whether a PMU event is supported for the current machine
1196 */
1197static bool event_supported(uint16_t number)
1198{
1199 if (number > MAX_EVENT_ID) {
1200 return false;
1201 }
1202 return supported_event_map[number] != UNSUPPORTED_EVENT;
1203}
1204
3f208fd7
PM
1205static CPAccessResult pmreg_access(CPUARMState *env, const ARMCPRegInfo *ri,
1206 bool isread)
200ac0ef 1207{
3b163b01 1208 /* Performance monitor registers user accessibility is controlled
1fce1ba9
PM
1209 * by PMUSERENR. MDCR_EL2.TPM and MDCR_EL3.TPM allow configurable
1210 * trapping to EL2 or EL3 for other accesses.
200ac0ef 1211 */
1fce1ba9
PM
1212 int el = arm_current_el(env);
1213
6ecd0b6b 1214 if (el == 0 && !(env->cp15.c9_pmuserenr & 1)) {
fcd25206 1215 return CP_ACCESS_TRAP;
200ac0ef 1216 }
1fce1ba9
PM
1217 if (el < 2 && (env->cp15.mdcr_el2 & MDCR_TPM)
1218 && !arm_is_secure_below_el3(env)) {
1219 return CP_ACCESS_TRAP_EL2;
1220 }
1221 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) {
1222 return CP_ACCESS_TRAP_EL3;
1223 }
1224
fcd25206 1225 return CP_ACCESS_OK;
200ac0ef
PM
1226}
1227
6ecd0b6b
AB
1228static CPAccessResult pmreg_access_xevcntr(CPUARMState *env,
1229 const ARMCPRegInfo *ri,
1230 bool isread)
1231{
1232 /* ER: event counter read trap control */
1233 if (arm_feature(env, ARM_FEATURE_V8)
1234 && arm_current_el(env) == 0
1235 && (env->cp15.c9_pmuserenr & (1 << 3)) != 0
1236 && isread) {
1237 return CP_ACCESS_OK;
1238 }
1239
1240 return pmreg_access(env, ri, isread);
1241}
1242
1243static CPAccessResult pmreg_access_swinc(CPUARMState *env,
1244 const ARMCPRegInfo *ri,
1245 bool isread)
1246{
1247 /* SW: software increment write trap control */
1248 if (arm_feature(env, ARM_FEATURE_V8)
1249 && arm_current_el(env) == 0
1250 && (env->cp15.c9_pmuserenr & (1 << 1)) != 0
1251 && !isread) {
1252 return CP_ACCESS_OK;
1253 }
1254
1255 return pmreg_access(env, ri, isread);
1256}
1257
6ecd0b6b
AB
1258static CPAccessResult pmreg_access_selr(CPUARMState *env,
1259 const ARMCPRegInfo *ri,
1260 bool isread)
1261{
1262 /* ER: event counter read trap control */
1263 if (arm_feature(env, ARM_FEATURE_V8)
1264 && arm_current_el(env) == 0
1265 && (env->cp15.c9_pmuserenr & (1 << 3)) != 0) {
1266 return CP_ACCESS_OK;
1267 }
1268
1269 return pmreg_access(env, ri, isread);
1270}
1271
1272static CPAccessResult pmreg_access_ccntr(CPUARMState *env,
1273 const ARMCPRegInfo *ri,
1274 bool isread)
1275{
1276 /* CR: cycle counter read trap control */
1277 if (arm_feature(env, ARM_FEATURE_V8)
1278 && arm_current_el(env) == 0
1279 && (env->cp15.c9_pmuserenr & (1 << 2)) != 0
1280 && isread) {
1281 return CP_ACCESS_OK;
1282 }
1283
1284 return pmreg_access(env, ri, isread);
1285}
1286
033614c4
AL
1287/* Returns true if the counter (pass 31 for PMCCNTR) should count events using
1288 * the current EL, security state, and register configuration.
1289 */
1290static bool pmu_counter_enabled(CPUARMState *env, uint8_t counter)
87124fde 1291{
033614c4
AL
1292 uint64_t filter;
1293 bool e, p, u, nsk, nsu, nsh, m;
1294 bool enabled, prohibited, filtered;
1295 bool secure = arm_is_secure(env);
1296 int el = arm_current_el(env);
1297 uint8_t hpmn = env->cp15.mdcr_el2 & MDCR_HPMN;
87124fde 1298
cbbb3041
AJ
1299 if (!arm_feature(env, ARM_FEATURE_PMU)) {
1300 return false;
1301 }
1302
033614c4
AL
1303 if (!arm_feature(env, ARM_FEATURE_EL2) ||
1304 (counter < hpmn || counter == 31)) {
1305 e = env->cp15.c9_pmcr & PMCRE;
1306 } else {
1307 e = env->cp15.mdcr_el2 & MDCR_HPME;
87124fde 1308 }
033614c4 1309 enabled = e && (env->cp15.c9_pmcnten & (1 << counter));
87124fde 1310
033614c4
AL
1311 if (!secure) {
1312 if (el == 2 && (counter < hpmn || counter == 31)) {
1313 prohibited = env->cp15.mdcr_el2 & MDCR_HPMD;
1314 } else {
1315 prohibited = false;
1316 }
1317 } else {
1318 prohibited = arm_feature(env, ARM_FEATURE_EL3) &&
1319 (env->cp15.mdcr_el3 & MDCR_SPME);
1320 }
1321
1322 if (prohibited && counter == 31) {
1323 prohibited = env->cp15.c9_pmcr & PMCRDP;
1324 }
1325
5ecdd3e4
AL
1326 if (counter == 31) {
1327 filter = env->cp15.pmccfiltr_el0;
1328 } else {
1329 filter = env->cp15.c14_pmevtyper[counter];
1330 }
033614c4
AL
1331
1332 p = filter & PMXEVTYPER_P;
1333 u = filter & PMXEVTYPER_U;
1334 nsk = arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_NSK);
1335 nsu = arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_NSU);
1336 nsh = arm_feature(env, ARM_FEATURE_EL2) && (filter & PMXEVTYPER_NSH);
1337 m = arm_el_is_aa64(env, 1) &&
1338 arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_M);
1339
1340 if (el == 0) {
1341 filtered = secure ? u : u != nsu;
1342 } else if (el == 1) {
1343 filtered = secure ? p : p != nsk;
1344 } else if (el == 2) {
1345 filtered = !nsh;
1346 } else { /* EL3 */
1347 filtered = m != p;
1348 }
1349
5ecdd3e4
AL
1350 if (counter != 31) {
1351 /*
1352 * If not checking PMCCNTR, ensure the counter is setup to an event we
1353 * support
1354 */
1355 uint16_t event = filter & PMXEVTYPER_EVTCOUNT;
1356 if (!event_supported(event)) {
1357 return false;
1358 }
1359 }
1360
033614c4 1361 return enabled && !prohibited && !filtered;
87124fde 1362}
033614c4 1363
f4efb4b2
AL
1364static void pmu_update_irq(CPUARMState *env)
1365{
2fc0cc0e 1366 ARMCPU *cpu = env_archcpu(env);
f4efb4b2
AL
1367 qemu_set_irq(cpu->pmu_interrupt, (env->cp15.c9_pmcr & PMCRE) &&
1368 (env->cp15.c9_pminten & env->cp15.c9_pmovsr));
1369}
1370
5d05b9d4
AL
1371/*
1372 * Ensure c15_ccnt is the guest-visible count so that operations such as
1373 * enabling/disabling the counter or filtering, modifying the count itself,
1374 * etc. can be done logically. This is essentially a no-op if the counter is
1375 * not enabled at the time of the call.
1376 */
f2b2f53f 1377static void pmccntr_op_start(CPUARMState *env)
ec7b4ce4 1378{
b2e23725 1379 uint64_t cycles = cycles_get_count(env);
ec7b4ce4 1380
033614c4 1381 if (pmu_counter_enabled(env, 31)) {
5d05b9d4
AL
1382 uint64_t eff_cycles = cycles;
1383 if (env->cp15.c9_pmcr & PMCRD) {
1384 /* Increment once every 64 processor clock cycles */
1385 eff_cycles /= 64;
1386 }
1387
f4efb4b2
AL
1388 uint64_t new_pmccntr = eff_cycles - env->cp15.c15_ccnt_delta;
1389
1390 uint64_t overflow_mask = env->cp15.c9_pmcr & PMCRLC ? \
1391 1ull << 63 : 1ull << 31;
1392 if (env->cp15.c15_ccnt & ~new_pmccntr & overflow_mask) {
1393 env->cp15.c9_pmovsr |= (1 << 31);
1394 pmu_update_irq(env);
1395 }
1396
1397 env->cp15.c15_ccnt = new_pmccntr;
ec7b4ce4 1398 }
5d05b9d4
AL
1399 env->cp15.c15_ccnt_delta = cycles;
1400}
ec7b4ce4 1401
5d05b9d4
AL
1402/*
1403 * If PMCCNTR is enabled, recalculate the delta between the clock and the
1404 * guest-visible count. A call to pmccntr_op_finish should follow every call to
1405 * pmccntr_op_start.
1406 */
f2b2f53f 1407static void pmccntr_op_finish(CPUARMState *env)
5d05b9d4 1408{
033614c4 1409 if (pmu_counter_enabled(env, 31)) {
4e7beb0c
AL
1410#ifndef CONFIG_USER_ONLY
1411 /* Calculate when the counter will next overflow */
1412 uint64_t remaining_cycles = -env->cp15.c15_ccnt;
1413 if (!(env->cp15.c9_pmcr & PMCRLC)) {
1414 remaining_cycles = (uint32_t)remaining_cycles;
1415 }
1416 int64_t overflow_in = cycles_ns_per(remaining_cycles);
1417
1418 if (overflow_in > 0) {
1419 int64_t overflow_at = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
1420 overflow_in;
2fc0cc0e 1421 ARMCPU *cpu = env_archcpu(env);
4e7beb0c
AL
1422 timer_mod_anticipate_ns(cpu->pmu_timer, overflow_at);
1423 }
1424#endif
5d05b9d4 1425
4e7beb0c 1426 uint64_t prev_cycles = env->cp15.c15_ccnt_delta;
5d05b9d4
AL
1427 if (env->cp15.c9_pmcr & PMCRD) {
1428 /* Increment once every 64 processor clock cycles */
1429 prev_cycles /= 64;
1430 }
5d05b9d4 1431 env->cp15.c15_ccnt_delta = prev_cycles - env->cp15.c15_ccnt;
ec7b4ce4
AF
1432 }
1433}
1434
5ecdd3e4
AL
1435static void pmevcntr_op_start(CPUARMState *env, uint8_t counter)
1436{
1437
1438 uint16_t event = env->cp15.c14_pmevtyper[counter] & PMXEVTYPER_EVTCOUNT;
1439 uint64_t count = 0;
1440 if (event_supported(event)) {
1441 uint16_t event_idx = supported_event_map[event];
1442 count = pm_events[event_idx].get_count(env);
1443 }
1444
1445 if (pmu_counter_enabled(env, counter)) {
f4efb4b2
AL
1446 uint32_t new_pmevcntr = count - env->cp15.c14_pmevcntr_delta[counter];
1447
1448 if (env->cp15.c14_pmevcntr[counter] & ~new_pmevcntr & INT32_MIN) {
1449 env->cp15.c9_pmovsr |= (1 << counter);
1450 pmu_update_irq(env);
1451 }
1452 env->cp15.c14_pmevcntr[counter] = new_pmevcntr;
5ecdd3e4
AL
1453 }
1454 env->cp15.c14_pmevcntr_delta[counter] = count;
1455}
1456
1457static void pmevcntr_op_finish(CPUARMState *env, uint8_t counter)
1458{
1459 if (pmu_counter_enabled(env, counter)) {
4e7beb0c
AL
1460#ifndef CONFIG_USER_ONLY
1461 uint16_t event = env->cp15.c14_pmevtyper[counter] & PMXEVTYPER_EVTCOUNT;
1462 uint16_t event_idx = supported_event_map[event];
1463 uint64_t delta = UINT32_MAX -
1464 (uint32_t)env->cp15.c14_pmevcntr[counter] + 1;
1465 int64_t overflow_in = pm_events[event_idx].ns_per_count(delta);
1466
1467 if (overflow_in > 0) {
1468 int64_t overflow_at = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
1469 overflow_in;
2fc0cc0e 1470 ARMCPU *cpu = env_archcpu(env);
4e7beb0c
AL
1471 timer_mod_anticipate_ns(cpu->pmu_timer, overflow_at);
1472 }
1473#endif
1474
5ecdd3e4
AL
1475 env->cp15.c14_pmevcntr_delta[counter] -=
1476 env->cp15.c14_pmevcntr[counter];
1477 }
1478}
1479
5d05b9d4
AL
1480void pmu_op_start(CPUARMState *env)
1481{
5ecdd3e4 1482 unsigned int i;
5d05b9d4 1483 pmccntr_op_start(env);
5ecdd3e4
AL
1484 for (i = 0; i < pmu_num_counters(env); i++) {
1485 pmevcntr_op_start(env, i);
1486 }
5d05b9d4
AL
1487}
1488
1489void pmu_op_finish(CPUARMState *env)
1490{
5ecdd3e4 1491 unsigned int i;
5d05b9d4 1492 pmccntr_op_finish(env);
5ecdd3e4
AL
1493 for (i = 0; i < pmu_num_counters(env); i++) {
1494 pmevcntr_op_finish(env, i);
1495 }
5d05b9d4
AL
1496}
1497
033614c4
AL
1498void pmu_pre_el_change(ARMCPU *cpu, void *ignored)
1499{
1500 pmu_op_start(&cpu->env);
1501}
1502
1503void pmu_post_el_change(ARMCPU *cpu, void *ignored)
1504{
1505 pmu_op_finish(&cpu->env);
1506}
1507
4e7beb0c
AL
1508void arm_pmu_timer_cb(void *opaque)
1509{
1510 ARMCPU *cpu = opaque;
1511
1512 /*
1513 * Update all the counter values based on the current underlying counts,
1514 * triggering interrupts to be raised, if necessary. pmu_op_finish() also
1515 * has the effect of setting the cpu->pmu_timer to the next earliest time a
1516 * counter may expire.
1517 */
1518 pmu_op_start(&cpu->env);
1519 pmu_op_finish(&cpu->env);
1520}
1521
c4241c7d
PM
1522static void pmcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1523 uint64_t value)
200ac0ef 1524{
5d05b9d4 1525 pmu_op_start(env);
7c2cb42b
AF
1526
1527 if (value & PMCRC) {
1528 /* The counter has been reset */
1529 env->cp15.c15_ccnt = 0;
1530 }
1531
5ecdd3e4
AL
1532 if (value & PMCRP) {
1533 unsigned int i;
1534 for (i = 0; i < pmu_num_counters(env); i++) {
1535 env->cp15.c14_pmevcntr[i] = 0;
1536 }
1537 }
1538
200ac0ef
PM
1539 /* only the DP, X, D and E bits are writable */
1540 env->cp15.c9_pmcr &= ~0x39;
1541 env->cp15.c9_pmcr |= (value & 0x39);
7c2cb42b 1542
5d05b9d4 1543 pmu_op_finish(env);
7c2cb42b
AF
1544}
1545
0d4bfd7d
AL
1546static void pmswinc_write(CPUARMState *env, const ARMCPRegInfo *ri,
1547 uint64_t value)
1548{
1549 unsigned int i;
1550 for (i = 0; i < pmu_num_counters(env); i++) {
1551 /* Increment a counter's count iff: */
1552 if ((value & (1 << i)) && /* counter's bit is set */
1553 /* counter is enabled and not filtered */
1554 pmu_counter_enabled(env, i) &&
1555 /* counter is SW_INCR */
1556 (env->cp15.c14_pmevtyper[i] & PMXEVTYPER_EVTCOUNT) == 0x0) {
1557 pmevcntr_op_start(env, i);
f4efb4b2
AL
1558
1559 /*
1560 * Detect if this write causes an overflow since we can't predict
1561 * PMSWINC overflows like we can for other events
1562 */
1563 uint32_t new_pmswinc = env->cp15.c14_pmevcntr[i] + 1;
1564
1565 if (env->cp15.c14_pmevcntr[i] & ~new_pmswinc & INT32_MIN) {
1566 env->cp15.c9_pmovsr |= (1 << i);
1567 pmu_update_irq(env);
1568 }
1569
1570 env->cp15.c14_pmevcntr[i] = new_pmswinc;
1571
0d4bfd7d
AL
1572 pmevcntr_op_finish(env, i);
1573 }
1574 }
1575}
1576
7c2cb42b
AF
1577static uint64_t pmccntr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1578{
5d05b9d4
AL
1579 uint64_t ret;
1580 pmccntr_op_start(env);
1581 ret = env->cp15.c15_ccnt;
1582 pmccntr_op_finish(env);
1583 return ret;
7c2cb42b
AF
1584}
1585
6b040780
WH
1586static void pmselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1587 uint64_t value)
1588{
1589 /* The value of PMSELR.SEL affects the behavior of PMXEVTYPER and
1590 * PMXEVCNTR. We allow [0..31] to be written to PMSELR here; in the
1591 * meanwhile, we check PMSELR.SEL when PMXEVTYPER and PMXEVCNTR are
1592 * accessed.
1593 */
1594 env->cp15.c9_pmselr = value & 0x1f;
1595}
1596
7c2cb42b
AF
1597static void pmccntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1598 uint64_t value)
1599{
5d05b9d4
AL
1600 pmccntr_op_start(env);
1601 env->cp15.c15_ccnt = value;
1602 pmccntr_op_finish(env);
200ac0ef 1603}
421c7ebd
PC
1604
1605static void pmccntr_write32(CPUARMState *env, const ARMCPRegInfo *ri,
1606 uint64_t value)
1607{
1608 uint64_t cur_val = pmccntr_read(env, NULL);
1609
1610 pmccntr_write(env, ri, deposit64(cur_val, 0, 32, value));
1611}
1612
0614601c
AF
1613static void pmccfiltr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1614 uint64_t value)
1615{
5d05b9d4 1616 pmccntr_op_start(env);
4b8afa1f
AL
1617 env->cp15.pmccfiltr_el0 = value & PMCCFILTR_EL0;
1618 pmccntr_op_finish(env);
1619}
1620
1621static void pmccfiltr_write_a32(CPUARMState *env, const ARMCPRegInfo *ri,
1622 uint64_t value)
1623{
1624 pmccntr_op_start(env);
1625 /* M is not accessible from AArch32 */
1626 env->cp15.pmccfiltr_el0 = (env->cp15.pmccfiltr_el0 & PMCCFILTR_M) |
1627 (value & PMCCFILTR);
5d05b9d4 1628 pmccntr_op_finish(env);
0614601c
AF
1629}
1630
4b8afa1f
AL
1631static uint64_t pmccfiltr_read_a32(CPUARMState *env, const ARMCPRegInfo *ri)
1632{
1633 /* M is not visible in AArch32 */
1634 return env->cp15.pmccfiltr_el0 & PMCCFILTR;
1635}
1636
c4241c7d 1637static void pmcntenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
200ac0ef
PM
1638 uint64_t value)
1639{
7ece99b1 1640 value &= pmu_counter_mask(env);
200ac0ef 1641 env->cp15.c9_pmcnten |= value;
200ac0ef
PM
1642}
1643
c4241c7d
PM
1644static void pmcntenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1645 uint64_t value)
200ac0ef 1646{
7ece99b1 1647 value &= pmu_counter_mask(env);
200ac0ef 1648 env->cp15.c9_pmcnten &= ~value;
200ac0ef
PM
1649}
1650
c4241c7d
PM
1651static void pmovsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1652 uint64_t value)
200ac0ef 1653{
599b71e2 1654 value &= pmu_counter_mask(env);
200ac0ef 1655 env->cp15.c9_pmovsr &= ~value;
f4efb4b2 1656 pmu_update_irq(env);
200ac0ef
PM
1657}
1658
327dd510
AL
1659static void pmovsset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1660 uint64_t value)
1661{
1662 value &= pmu_counter_mask(env);
1663 env->cp15.c9_pmovsr |= value;
f4efb4b2 1664 pmu_update_irq(env);
327dd510
AL
1665}
1666
5ecdd3e4
AL
1667static void pmevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
1668 uint64_t value, const uint8_t counter)
200ac0ef 1669{
5ecdd3e4
AL
1670 if (counter == 31) {
1671 pmccfiltr_write(env, ri, value);
1672 } else if (counter < pmu_num_counters(env)) {
1673 pmevcntr_op_start(env, counter);
1674
1675 /*
1676 * If this counter's event type is changing, store the current
1677 * underlying count for the new type in c14_pmevcntr_delta[counter] so
1678 * pmevcntr_op_finish has the correct baseline when it converts back to
1679 * a delta.
1680 */
1681 uint16_t old_event = env->cp15.c14_pmevtyper[counter] &
1682 PMXEVTYPER_EVTCOUNT;
1683 uint16_t new_event = value & PMXEVTYPER_EVTCOUNT;
1684 if (old_event != new_event) {
1685 uint64_t count = 0;
1686 if (event_supported(new_event)) {
1687 uint16_t event_idx = supported_event_map[new_event];
1688 count = pm_events[event_idx].get_count(env);
1689 }
1690 env->cp15.c14_pmevcntr_delta[counter] = count;
1691 }
1692
1693 env->cp15.c14_pmevtyper[counter] = value & PMXEVTYPER_MASK;
1694 pmevcntr_op_finish(env, counter);
1695 }
fdb86656
WH
1696 /* Attempts to access PMXEVTYPER are CONSTRAINED UNPREDICTABLE when
1697 * PMSELR value is equal to or greater than the number of implemented
1698 * counters, but not equal to 0x1f. We opt to behave as a RAZ/WI.
1699 */
5ecdd3e4
AL
1700}
1701
1702static uint64_t pmevtyper_read(CPUARMState *env, const ARMCPRegInfo *ri,
1703 const uint8_t counter)
1704{
1705 if (counter == 31) {
1706 return env->cp15.pmccfiltr_el0;
1707 } else if (counter < pmu_num_counters(env)) {
1708 return env->cp15.c14_pmevtyper[counter];
1709 } else {
1710 /*
1711 * We opt to behave as a RAZ/WI when attempts to access PMXEVTYPER
1712 * are CONSTRAINED UNPREDICTABLE. See comments in pmevtyper_write().
1713 */
1714 return 0;
1715 }
1716}
1717
1718static void pmevtyper_writefn(CPUARMState *env, const ARMCPRegInfo *ri,
1719 uint64_t value)
1720{
1721 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1722 pmevtyper_write(env, ri, value, counter);
1723}
1724
1725static void pmevtyper_rawwrite(CPUARMState *env, const ARMCPRegInfo *ri,
1726 uint64_t value)
1727{
1728 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1729 env->cp15.c14_pmevtyper[counter] = value;
1730
1731 /*
1732 * pmevtyper_rawwrite is called between a pair of pmu_op_start and
1733 * pmu_op_finish calls when loading saved state for a migration. Because
1734 * we're potentially updating the type of event here, the value written to
1735 * c14_pmevcntr_delta by the preceeding pmu_op_start call may be for a
1736 * different counter type. Therefore, we need to set this value to the
1737 * current count for the counter type we're writing so that pmu_op_finish
1738 * has the correct count for its calculation.
1739 */
1740 uint16_t event = value & PMXEVTYPER_EVTCOUNT;
1741 if (event_supported(event)) {
1742 uint16_t event_idx = supported_event_map[event];
1743 env->cp15.c14_pmevcntr_delta[counter] =
1744 pm_events[event_idx].get_count(env);
fdb86656
WH
1745 }
1746}
1747
5ecdd3e4
AL
1748static uint64_t pmevtyper_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
1749{
1750 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1751 return pmevtyper_read(env, ri, counter);
1752}
1753
1754static void pmxevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
1755 uint64_t value)
1756{
1757 pmevtyper_write(env, ri, value, env->cp15.c9_pmselr & 31);
1758}
1759
fdb86656
WH
1760static uint64_t pmxevtyper_read(CPUARMState *env, const ARMCPRegInfo *ri)
1761{
5ecdd3e4
AL
1762 return pmevtyper_read(env, ri, env->cp15.c9_pmselr & 31);
1763}
1764
1765static void pmevcntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1766 uint64_t value, uint8_t counter)
1767{
1768 if (counter < pmu_num_counters(env)) {
1769 pmevcntr_op_start(env, counter);
1770 env->cp15.c14_pmevcntr[counter] = value;
1771 pmevcntr_op_finish(env, counter);
1772 }
1773 /*
1774 * We opt to behave as a RAZ/WI when attempts to access PM[X]EVCNTR
1775 * are CONSTRAINED UNPREDICTABLE.
fdb86656 1776 */
5ecdd3e4
AL
1777}
1778
1779static uint64_t pmevcntr_read(CPUARMState *env, const ARMCPRegInfo *ri,
1780 uint8_t counter)
1781{
1782 if (counter < pmu_num_counters(env)) {
1783 uint64_t ret;
1784 pmevcntr_op_start(env, counter);
1785 ret = env->cp15.c14_pmevcntr[counter];
1786 pmevcntr_op_finish(env, counter);
1787 return ret;
fdb86656 1788 } else {
5ecdd3e4
AL
1789 /* We opt to behave as a RAZ/WI when attempts to access PM[X]EVCNTR
1790 * are CONSTRAINED UNPREDICTABLE. */
fdb86656
WH
1791 return 0;
1792 }
200ac0ef
PM
1793}
1794
5ecdd3e4
AL
1795static void pmevcntr_writefn(CPUARMState *env, const ARMCPRegInfo *ri,
1796 uint64_t value)
1797{
1798 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1799 pmevcntr_write(env, ri, value, counter);
1800}
1801
1802static uint64_t pmevcntr_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
1803{
1804 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1805 return pmevcntr_read(env, ri, counter);
1806}
1807
1808static void pmevcntr_rawwrite(CPUARMState *env, const ARMCPRegInfo *ri,
1809 uint64_t value)
1810{
1811 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1812 assert(counter < pmu_num_counters(env));
1813 env->cp15.c14_pmevcntr[counter] = value;
1814 pmevcntr_write(env, ri, value, counter);
1815}
1816
1817static uint64_t pmevcntr_rawread(CPUARMState *env, const ARMCPRegInfo *ri)
1818{
1819 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1820 assert(counter < pmu_num_counters(env));
1821 return env->cp15.c14_pmevcntr[counter];
1822}
1823
1824static void pmxevcntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1825 uint64_t value)
1826{
1827 pmevcntr_write(env, ri, value, env->cp15.c9_pmselr & 31);
1828}
1829
1830static uint64_t pmxevcntr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1831{
1832 return pmevcntr_read(env, ri, env->cp15.c9_pmselr & 31);
1833}
1834
c4241c7d 1835static void pmuserenr_write(CPUARMState *env, const ARMCPRegInfo *ri,
200ac0ef
PM
1836 uint64_t value)
1837{
6ecd0b6b
AB
1838 if (arm_feature(env, ARM_FEATURE_V8)) {
1839 env->cp15.c9_pmuserenr = value & 0xf;
1840 } else {
1841 env->cp15.c9_pmuserenr = value & 1;
1842 }
200ac0ef
PM
1843}
1844
c4241c7d
PM
1845static void pmintenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1846 uint64_t value)
200ac0ef
PM
1847{
1848 /* We have no event counters so only the C bit can be changed */
7ece99b1 1849 value &= pmu_counter_mask(env);
200ac0ef 1850 env->cp15.c9_pminten |= value;
f4efb4b2 1851 pmu_update_irq(env);
200ac0ef
PM
1852}
1853
c4241c7d
PM
1854static void pmintenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1855 uint64_t value)
200ac0ef 1856{
7ece99b1 1857 value &= pmu_counter_mask(env);
200ac0ef 1858 env->cp15.c9_pminten &= ~value;
f4efb4b2 1859 pmu_update_irq(env);
200ac0ef
PM
1860}
1861
c4241c7d
PM
1862static void vbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
1863 uint64_t value)
8641136c 1864{
a505d7fe
PM
1865 /* Note that even though the AArch64 view of this register has bits
1866 * [10:0] all RES0 we can only mask the bottom 5, to comply with the
1867 * architectural requirements for bits which are RES0 only in some
1868 * contexts. (ARMv8 would permit us to do no masking at all, but ARMv7
1869 * requires the bottom five bits to be RAZ/WI because they're UNK/SBZP.)
1870 */
855ea66d 1871 raw_write(env, ri, value & ~0x1FULL);
8641136c
NR
1872}
1873
64e0e2de
EI
1874static void scr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
1875{
ea22747c
RH
1876 /* Begin with base v8.0 state. */
1877 uint32_t valid_mask = 0x3fff;
2fc0cc0e 1878 ARMCPU *cpu = env_archcpu(env);
ea22747c
RH
1879
1880 if (arm_el_is_aa64(env, 3)) {
1881 value |= SCR_FW | SCR_AW; /* these two bits are RES1. */
1882 valid_mask &= ~SCR_NET;
1883 } else {
1884 valid_mask &= ~(SCR_RW | SCR_ST);
1885 }
64e0e2de
EI
1886
1887 if (!arm_feature(env, ARM_FEATURE_EL2)) {
1888 valid_mask &= ~SCR_HCE;
1889
1890 /* On ARMv7, SMD (or SCD as it is called in v7) is only
1891 * supported if EL2 exists. The bit is UNK/SBZP when
1892 * EL2 is unavailable. In QEMU ARMv7, we force it to always zero
1893 * when EL2 is unavailable.
4eb27640 1894 * On ARMv8, this bit is always available.
64e0e2de 1895 */
4eb27640
GB
1896 if (arm_feature(env, ARM_FEATURE_V7) &&
1897 !arm_feature(env, ARM_FEATURE_V8)) {
64e0e2de
EI
1898 valid_mask &= ~SCR_SMD;
1899 }
1900 }
2d7137c1
RH
1901 if (cpu_isar_feature(aa64_lor, cpu)) {
1902 valid_mask |= SCR_TLOR;
1903 }
ef682cdb
RH
1904 if (cpu_isar_feature(aa64_pauth, cpu)) {
1905 valid_mask |= SCR_API | SCR_APK;
1906 }
64e0e2de
EI
1907
1908 /* Clear all-context RES0 bits. */
1909 value &= valid_mask;
1910 raw_write(env, ri, value);
1911}
1912
630fcd4d
MZ
1913static CPAccessResult access_aa64_tid2(CPUARMState *env,
1914 const ARMCPRegInfo *ri,
1915 bool isread)
1916{
1917 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID2)) {
1918 return CP_ACCESS_TRAP_EL2;
1919 }
1920
1921 return CP_ACCESS_OK;
1922}
1923
c4241c7d 1924static uint64_t ccsidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
776d4e5c 1925{
2fc0cc0e 1926 ARMCPU *cpu = env_archcpu(env);
b85a1fd6
FA
1927
1928 /* Acquire the CSSELR index from the bank corresponding to the CCSIDR
1929 * bank
1930 */
1931 uint32_t index = A32_BANKED_REG_GET(env, csselr,
1932 ri->secure & ARM_CP_SECSTATE_S);
1933
1934 return cpu->ccsidr[index];
776d4e5c
PM
1935}
1936
c4241c7d
PM
1937static void csselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1938 uint64_t value)
776d4e5c 1939{
8d5c773e 1940 raw_write(env, ri, value & 0xf);
776d4e5c
PM
1941}
1942
1090b9c6
PM
1943static uint64_t isr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1944{
29a0af61 1945 CPUState *cs = env_cpu(env);
f7778444 1946 uint64_t hcr_el2 = arm_hcr_el2_eff(env);
1090b9c6 1947 uint64_t ret = 0;
7cf95aed
MZ
1948 bool allow_virt = (arm_current_el(env) == 1 &&
1949 (!arm_is_secure_below_el3(env) ||
1950 (env->cp15.scr_el3 & SCR_EEL2)));
1090b9c6 1951
7cf95aed 1952 if (allow_virt && (hcr_el2 & HCR_IMO)) {
636540e9
PM
1953 if (cs->interrupt_request & CPU_INTERRUPT_VIRQ) {
1954 ret |= CPSR_I;
1955 }
1956 } else {
1957 if (cs->interrupt_request & CPU_INTERRUPT_HARD) {
1958 ret |= CPSR_I;
1959 }
1090b9c6 1960 }
636540e9 1961
7cf95aed 1962 if (allow_virt && (hcr_el2 & HCR_FMO)) {
636540e9
PM
1963 if (cs->interrupt_request & CPU_INTERRUPT_VFIQ) {
1964 ret |= CPSR_F;
1965 }
1966 } else {
1967 if (cs->interrupt_request & CPU_INTERRUPT_FIQ) {
1968 ret |= CPSR_F;
1969 }
1090b9c6 1970 }
636540e9 1971
1090b9c6
PM
1972 /* External aborts are not possible in QEMU so A bit is always clear */
1973 return ret;
1974}
1975
93fbc983
MZ
1976static CPAccessResult access_aa64_tid1(CPUARMState *env, const ARMCPRegInfo *ri,
1977 bool isread)
1978{
1979 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID1)) {
1980 return CP_ACCESS_TRAP_EL2;
1981 }
1982
1983 return CP_ACCESS_OK;
1984}
1985
1986static CPAccessResult access_aa32_tid1(CPUARMState *env, const ARMCPRegInfo *ri,
1987 bool isread)
1988{
1989 if (arm_feature(env, ARM_FEATURE_V8)) {
1990 return access_aa64_tid1(env, ri, isread);
1991 }
1992
1993 return CP_ACCESS_OK;
1994}
1995
e9aa6c21 1996static const ARMCPRegInfo v7_cp_reginfo[] = {
7d57f408
PM
1997 /* the old v6 WFI, UNPREDICTABLE in v7 but we choose to NOP */
1998 { .name = "NOP", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
1999 .access = PL1_W, .type = ARM_CP_NOP },
200ac0ef
PM
2000 /* Performance monitors are implementation defined in v7,
2001 * but with an ARM recommended set of registers, which we
ac689a2e 2002 * follow.
200ac0ef
PM
2003 *
2004 * Performance registers fall into three categories:
2005 * (a) always UNDEF in PL0, RW in PL1 (PMINTENSET, PMINTENCLR)
2006 * (b) RO in PL0 (ie UNDEF on write), RW in PL1 (PMUSERENR)
2007 * (c) UNDEF in PL0 if PMUSERENR.EN==0, otherwise accessible (all others)
2008 * For the cases controlled by PMUSERENR we must set .access to PL0_RW
2009 * or PL0_RO as appropriate and then check PMUSERENR in the helper fn.
2010 */
2011 { .name = "PMCNTENSET", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 1,
7a0e58fa 2012 .access = PL0_RW, .type = ARM_CP_ALIAS,
8521466b 2013 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
fcd25206
PM
2014 .writefn = pmcntenset_write,
2015 .accessfn = pmreg_access,
2016 .raw_writefn = raw_write },
8521466b
AF
2017 { .name = "PMCNTENSET_EL0", .state = ARM_CP_STATE_AA64,
2018 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 1,
2019 .access = PL0_RW, .accessfn = pmreg_access,
2020 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten), .resetvalue = 0,
2021 .writefn = pmcntenset_write, .raw_writefn = raw_write },
200ac0ef 2022 { .name = "PMCNTENCLR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 2,
8521466b
AF
2023 .access = PL0_RW,
2024 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
fcd25206
PM
2025 .accessfn = pmreg_access,
2026 .writefn = pmcntenclr_write,
7a0e58fa 2027 .type = ARM_CP_ALIAS },
8521466b
AF
2028 { .name = "PMCNTENCLR_EL0", .state = ARM_CP_STATE_AA64,
2029 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 2,
2030 .access = PL0_RW, .accessfn = pmreg_access,
7a0e58fa 2031 .type = ARM_CP_ALIAS,
8521466b
AF
2032 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten),
2033 .writefn = pmcntenclr_write },
200ac0ef 2034 { .name = "PMOVSR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 3,
f4efb4b2 2035 .access = PL0_RW, .type = ARM_CP_IO,
e4e91a21 2036 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr),
fcd25206
PM
2037 .accessfn = pmreg_access,
2038 .writefn = pmovsr_write,
2039 .raw_writefn = raw_write },
978364f1
AF
2040 { .name = "PMOVSCLR_EL0", .state = ARM_CP_STATE_AA64,
2041 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 3,
2042 .access = PL0_RW, .accessfn = pmreg_access,
f4efb4b2 2043 .type = ARM_CP_ALIAS | ARM_CP_IO,
978364f1
AF
2044 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
2045 .writefn = pmovsr_write,
2046 .raw_writefn = raw_write },
200ac0ef 2047 { .name = "PMSWINC", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 4,
f4efb4b2
AL
2048 .access = PL0_W, .accessfn = pmreg_access_swinc,
2049 .type = ARM_CP_NO_RAW | ARM_CP_IO,
0d4bfd7d
AL
2050 .writefn = pmswinc_write },
2051 { .name = "PMSWINC_EL0", .state = ARM_CP_STATE_AA64,
2052 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 4,
f4efb4b2
AL
2053 .access = PL0_W, .accessfn = pmreg_access_swinc,
2054 .type = ARM_CP_NO_RAW | ARM_CP_IO,
0d4bfd7d 2055 .writefn = pmswinc_write },
6b040780
WH
2056 { .name = "PMSELR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 5,
2057 .access = PL0_RW, .type = ARM_CP_ALIAS,
2058 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmselr),
6ecd0b6b 2059 .accessfn = pmreg_access_selr, .writefn = pmselr_write,
6b040780
WH
2060 .raw_writefn = raw_write},
2061 { .name = "PMSELR_EL0", .state = ARM_CP_STATE_AA64,
2062 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 5,
6ecd0b6b 2063 .access = PL0_RW, .accessfn = pmreg_access_selr,
6b040780
WH
2064 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmselr),
2065 .writefn = pmselr_write, .raw_writefn = raw_write, },
200ac0ef 2066 { .name = "PMCCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 0,
169c8938 2067 .access = PL0_RW, .resetvalue = 0, .type = ARM_CP_ALIAS | ARM_CP_IO,
421c7ebd 2068 .readfn = pmccntr_read, .writefn = pmccntr_write32,
6ecd0b6b 2069 .accessfn = pmreg_access_ccntr },
8521466b
AF
2070 { .name = "PMCCNTR_EL0", .state = ARM_CP_STATE_AA64,
2071 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 0,
6ecd0b6b 2072 .access = PL0_RW, .accessfn = pmreg_access_ccntr,
8521466b 2073 .type = ARM_CP_IO,
980ebe87
AL
2074 .fieldoffset = offsetof(CPUARMState, cp15.c15_ccnt),
2075 .readfn = pmccntr_read, .writefn = pmccntr_write,
2076 .raw_readfn = raw_read, .raw_writefn = raw_write, },
4b8afa1f
AL
2077 { .name = "PMCCFILTR", .cp = 15, .opc1 = 0, .crn = 14, .crm = 15, .opc2 = 7,
2078 .writefn = pmccfiltr_write_a32, .readfn = pmccfiltr_read_a32,
2079 .access = PL0_RW, .accessfn = pmreg_access,
2080 .type = ARM_CP_ALIAS | ARM_CP_IO,
2081 .resetvalue = 0, },
8521466b
AF
2082 { .name = "PMCCFILTR_EL0", .state = ARM_CP_STATE_AA64,
2083 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 15, .opc2 = 7,
980ebe87 2084 .writefn = pmccfiltr_write, .raw_writefn = raw_write,
8521466b
AF
2085 .access = PL0_RW, .accessfn = pmreg_access,
2086 .type = ARM_CP_IO,
2087 .fieldoffset = offsetof(CPUARMState, cp15.pmccfiltr_el0),
2088 .resetvalue = 0, },
200ac0ef 2089 { .name = "PMXEVTYPER", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 1,
5ecdd3e4
AL
2090 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2091 .accessfn = pmreg_access,
fdb86656
WH
2092 .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
2093 { .name = "PMXEVTYPER_EL0", .state = ARM_CP_STATE_AA64,
2094 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 1,
5ecdd3e4
AL
2095 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2096 .accessfn = pmreg_access,
fdb86656 2097 .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
200ac0ef 2098 { .name = "PMXEVCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 2,
5ecdd3e4
AL
2099 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2100 .accessfn = pmreg_access_xevcntr,
2101 .writefn = pmxevcntr_write, .readfn = pmxevcntr_read },
2102 { .name = "PMXEVCNTR_EL0", .state = ARM_CP_STATE_AA64,
2103 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 2,
2104 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2105 .accessfn = pmreg_access_xevcntr,
2106 .writefn = pmxevcntr_write, .readfn = pmxevcntr_read },
200ac0ef 2107 { .name = "PMUSERENR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 0,
1fce1ba9 2108 .access = PL0_R | PL1_RW, .accessfn = access_tpm,
e4e91a21 2109 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmuserenr),
200ac0ef 2110 .resetvalue = 0,
d4e6df63 2111 .writefn = pmuserenr_write, .raw_writefn = raw_write },
8a83ffc2
AF
2112 { .name = "PMUSERENR_EL0", .state = ARM_CP_STATE_AA64,
2113 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 0,
1fce1ba9 2114 .access = PL0_R | PL1_RW, .accessfn = access_tpm, .type = ARM_CP_ALIAS,
8a83ffc2
AF
2115 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmuserenr),
2116 .resetvalue = 0,
2117 .writefn = pmuserenr_write, .raw_writefn = raw_write },
200ac0ef 2118 { .name = "PMINTENSET", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 1,
1fce1ba9 2119 .access = PL1_RW, .accessfn = access_tpm,
b7d793ad 2120 .type = ARM_CP_ALIAS | ARM_CP_IO,
e6ec5457 2121 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pminten),
200ac0ef 2122 .resetvalue = 0,
d4e6df63 2123 .writefn = pmintenset_write, .raw_writefn = raw_write },
e6ec5457
WH
2124 { .name = "PMINTENSET_EL1", .state = ARM_CP_STATE_AA64,
2125 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 1,
2126 .access = PL1_RW, .accessfn = access_tpm,
2127 .type = ARM_CP_IO,
2128 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2129 .writefn = pmintenset_write, .raw_writefn = raw_write,
2130 .resetvalue = 0x0 },
200ac0ef 2131 { .name = "PMINTENCLR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 2,
fc5f6856
AL
2132 .access = PL1_RW, .accessfn = access_tpm,
2133 .type = ARM_CP_ALIAS | ARM_CP_IO,
200ac0ef 2134 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
b061a82b 2135 .writefn = pmintenclr_write, },
978364f1
AF
2136 { .name = "PMINTENCLR_EL1", .state = ARM_CP_STATE_AA64,
2137 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 2,
fc5f6856
AL
2138 .access = PL1_RW, .accessfn = access_tpm,
2139 .type = ARM_CP_ALIAS | ARM_CP_IO,
978364f1
AF
2140 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2141 .writefn = pmintenclr_write },
7da845b0
PM
2142 { .name = "CCSIDR", .state = ARM_CP_STATE_BOTH,
2143 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 0,
630fcd4d
MZ
2144 .access = PL1_R,
2145 .accessfn = access_aa64_tid2,
2146 .readfn = ccsidr_read, .type = ARM_CP_NO_RAW },
7da845b0
PM
2147 { .name = "CSSELR", .state = ARM_CP_STATE_BOTH,
2148 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 2, .opc2 = 0,
630fcd4d
MZ
2149 .access = PL1_RW,
2150 .accessfn = access_aa64_tid2,
2151 .writefn = csselr_write, .resetvalue = 0,
b85a1fd6
FA
2152 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.csselr_s),
2153 offsetof(CPUARMState, cp15.csselr_ns) } },
776d4e5c
PM
2154 /* Auxiliary ID register: this actually has an IMPDEF value but for now
2155 * just RAZ for all cores:
2156 */
0ff644a7
PM
2157 { .name = "AIDR", .state = ARM_CP_STATE_BOTH,
2158 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 7,
93fbc983
MZ
2159 .access = PL1_R, .type = ARM_CP_CONST,
2160 .accessfn = access_aa64_tid1,
2161 .resetvalue = 0 },
f32cdad5
PM
2162 /* Auxiliary fault status registers: these also are IMPDEF, and we
2163 * choose to RAZ/WI for all cores.
2164 */
2165 { .name = "AFSR0_EL1", .state = ARM_CP_STATE_BOTH,
2166 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 0,
2167 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
2168 { .name = "AFSR1_EL1", .state = ARM_CP_STATE_BOTH,
2169 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 1,
2170 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
b0fe2427
PM
2171 /* MAIR can just read-as-written because we don't implement caches
2172 * and so don't need to care about memory attributes.
2173 */
2174 { .name = "MAIR_EL1", .state = ARM_CP_STATE_AA64,
2175 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
be693c87 2176 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[1]),
b0fe2427 2177 .resetvalue = 0 },
4cfb8ad8
PM
2178 { .name = "MAIR_EL3", .state = ARM_CP_STATE_AA64,
2179 .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 2, .opc2 = 0,
2180 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[3]),
2181 .resetvalue = 0 },
b0fe2427
PM
2182 /* For non-long-descriptor page tables these are PRRR and NMRR;
2183 * regardless they still act as reads-as-written for QEMU.
b0fe2427 2184 */
1281f8e3 2185 /* MAIR0/1 are defined separately from their 64-bit counterpart which
be693c87
GB
2186 * allows them to assign the correct fieldoffset based on the endianness
2187 * handled in the field definitions.
2188 */
a903c449 2189 { .name = "MAIR0", .state = ARM_CP_STATE_AA32,
b0fe2427 2190 .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0, .access = PL1_RW,
be693c87
GB
2191 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair0_s),
2192 offsetof(CPUARMState, cp15.mair0_ns) },
b0fe2427 2193 .resetfn = arm_cp_reset_ignore },
a903c449 2194 { .name = "MAIR1", .state = ARM_CP_STATE_AA32,
b0fe2427 2195 .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 1, .access = PL1_RW,
be693c87
GB
2196 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair1_s),
2197 offsetof(CPUARMState, cp15.mair1_ns) },
b0fe2427 2198 .resetfn = arm_cp_reset_ignore },
1090b9c6
PM
2199 { .name = "ISR_EL1", .state = ARM_CP_STATE_BOTH,
2200 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 0,
7a0e58fa 2201 .type = ARM_CP_NO_RAW, .access = PL1_R, .readfn = isr_read },
995939a6
PM
2202 /* 32 bit ITLB invalidates */
2203 { .name = "ITLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 0,
7a0e58fa 2204 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_write },
995939a6 2205 { .name = "ITLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 1,
7a0e58fa 2206 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write },
995939a6 2207 { .name = "ITLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 2,
7a0e58fa 2208 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiasid_write },
995939a6
PM
2209 /* 32 bit DTLB invalidates */
2210 { .name = "DTLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 0,
7a0e58fa 2211 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_write },
995939a6 2212 { .name = "DTLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 1,
7a0e58fa 2213 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write },
995939a6 2214 { .name = "DTLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 2,
7a0e58fa 2215 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiasid_write },
995939a6
PM
2216 /* 32 bit TLB invalidates */
2217 { .name = "TLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
7a0e58fa 2218 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_write },
995939a6 2219 { .name = "TLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
7a0e58fa 2220 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write },
995939a6 2221 { .name = "TLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
7a0e58fa 2222 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiasid_write },
995939a6 2223 { .name = "TLBIMVAA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
7a0e58fa 2224 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimvaa_write },
995939a6
PM
2225 REGINFO_SENTINEL
2226};
2227
2228static const ARMCPRegInfo v7mp_cp_reginfo[] = {
2229 /* 32 bit TLB invalidates, Inner Shareable */
2230 { .name = "TLBIALLIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
7a0e58fa 2231 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_is_write },
995939a6 2232 { .name = "TLBIMVAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
7a0e58fa 2233 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_is_write },
995939a6 2234 { .name = "TLBIASIDIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
7a0e58fa 2235 .type = ARM_CP_NO_RAW, .access = PL1_W,
fa439fc5 2236 .writefn = tlbiasid_is_write },
995939a6 2237 { .name = "TLBIMVAAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
7a0e58fa 2238 .type = ARM_CP_NO_RAW, .access = PL1_W,
fa439fc5 2239 .writefn = tlbimvaa_is_write },
e9aa6c21
PM
2240 REGINFO_SENTINEL
2241};
2242
327dd510
AL
2243static const ARMCPRegInfo pmovsset_cp_reginfo[] = {
2244 /* PMOVSSET is not implemented in v7 before v7ve */
2245 { .name = "PMOVSSET", .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 3,
2246 .access = PL0_RW, .accessfn = pmreg_access,
f4efb4b2 2247 .type = ARM_CP_ALIAS | ARM_CP_IO,
327dd510
AL
2248 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr),
2249 .writefn = pmovsset_write,
2250 .raw_writefn = raw_write },
2251 { .name = "PMOVSSET_EL0", .state = ARM_CP_STATE_AA64,
2252 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 3,
2253 .access = PL0_RW, .accessfn = pmreg_access,
f4efb4b2 2254 .type = ARM_CP_ALIAS | ARM_CP_IO,
327dd510
AL
2255 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
2256 .writefn = pmovsset_write,
2257 .raw_writefn = raw_write },
2258 REGINFO_SENTINEL
2259};
2260
c4241c7d
PM
2261static void teecr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2262 uint64_t value)
c326b979
PM
2263{
2264 value &= 1;
2265 env->teecr = value;
c326b979
PM
2266}
2267
3f208fd7
PM
2268static CPAccessResult teehbr_access(CPUARMState *env, const ARMCPRegInfo *ri,
2269 bool isread)
c326b979 2270{
dcbff19b 2271 if (arm_current_el(env) == 0 && (env->teecr & 1)) {
92611c00 2272 return CP_ACCESS_TRAP;
c326b979 2273 }
92611c00 2274 return CP_ACCESS_OK;
c326b979
PM
2275}
2276
2277static const ARMCPRegInfo t2ee_cp_reginfo[] = {
2278 { .name = "TEECR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 6, .opc2 = 0,
2279 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, teecr),
2280 .resetvalue = 0,
2281 .writefn = teecr_write },
2282 { .name = "TEEHBR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 6, .opc2 = 0,
2283 .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, teehbr),
92611c00 2284 .accessfn = teehbr_access, .resetvalue = 0 },
c326b979
PM
2285 REGINFO_SENTINEL
2286};
2287
4d31c596 2288static const ARMCPRegInfo v6k_cp_reginfo[] = {
e4fe830b
PM
2289 { .name = "TPIDR_EL0", .state = ARM_CP_STATE_AA64,
2290 .opc0 = 3, .opc1 = 3, .opc2 = 2, .crn = 13, .crm = 0,
2291 .access = PL0_RW,
54bf36ed 2292 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[0]), .resetvalue = 0 },
4d31c596
PM
2293 { .name = "TPIDRURW", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 2,
2294 .access = PL0_RW,
54bf36ed
FA
2295 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrurw_s),
2296 offsetoflow32(CPUARMState, cp15.tpidrurw_ns) },
e4fe830b
PM
2297 .resetfn = arm_cp_reset_ignore },
2298 { .name = "TPIDRRO_EL0", .state = ARM_CP_STATE_AA64,
2299 .opc0 = 3, .opc1 = 3, .opc2 = 3, .crn = 13, .crm = 0,
2300 .access = PL0_R|PL1_W,
54bf36ed
FA
2301 .fieldoffset = offsetof(CPUARMState, cp15.tpidrro_el[0]),
2302 .resetvalue = 0},
4d31c596
PM
2303 { .name = "TPIDRURO", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 3,
2304 .access = PL0_R|PL1_W,
54bf36ed
FA
2305 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidruro_s),
2306 offsetoflow32(CPUARMState, cp15.tpidruro_ns) },
e4fe830b 2307 .resetfn = arm_cp_reset_ignore },
54bf36ed 2308 { .name = "TPIDR_EL1", .state = ARM_CP_STATE_AA64,
e4fe830b 2309 .opc0 = 3, .opc1 = 0, .opc2 = 4, .crn = 13, .crm = 0,
4d31c596 2310 .access = PL1_RW,
54bf36ed
FA
2311 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[1]), .resetvalue = 0 },
2312 { .name = "TPIDRPRW", .opc1 = 0, .cp = 15, .crn = 13, .crm = 0, .opc2 = 4,
2313 .access = PL1_RW,
2314 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrprw_s),
2315 offsetoflow32(CPUARMState, cp15.tpidrprw_ns) },
2316 .resetvalue = 0 },
4d31c596
PM
2317 REGINFO_SENTINEL
2318};
2319
55d284af
PM
2320#ifndef CONFIG_USER_ONLY
2321
3f208fd7
PM
2322static CPAccessResult gt_cntfrq_access(CPUARMState *env, const ARMCPRegInfo *ri,
2323 bool isread)
00108f2d 2324{
75502672
PM
2325 /* CNTFRQ: not visible from PL0 if both PL0PCTEN and PL0VCTEN are zero.
2326 * Writable only at the highest implemented exception level.
2327 */
2328 int el = arm_current_el(env);
2329
2330 switch (el) {
2331 case 0:
2332 if (!extract32(env->cp15.c14_cntkctl, 0, 2)) {
2333 return CP_ACCESS_TRAP;
2334 }
2335 break;
2336 case 1:
2337 if (!isread && ri->state == ARM_CP_STATE_AA32 &&
2338 arm_is_secure_below_el3(env)) {
2339 /* Accesses from 32-bit Secure EL1 UNDEF (*not* trap to EL3!) */
2340 return CP_ACCESS_TRAP_UNCATEGORIZED;
2341 }
2342 break;
2343 case 2:
2344 case 3:
2345 break;
00108f2d 2346 }
75502672
PM
2347
2348 if (!isread && el < arm_highest_el(env)) {
2349 return CP_ACCESS_TRAP_UNCATEGORIZED;
2350 }
2351
00108f2d
PM
2352 return CP_ACCESS_OK;
2353}
2354
3f208fd7
PM
2355static CPAccessResult gt_counter_access(CPUARMState *env, int timeridx,
2356 bool isread)
00108f2d 2357{
0b6440af
EI
2358 unsigned int cur_el = arm_current_el(env);
2359 bool secure = arm_is_secure(env);
2360
00108f2d 2361 /* CNT[PV]CT: not visible from PL0 if ELO[PV]CTEN is zero */
0b6440af 2362 if (cur_el == 0 &&
00108f2d
PM
2363 !extract32(env->cp15.c14_cntkctl, timeridx, 1)) {
2364 return CP_ACCESS_TRAP;
2365 }
0b6440af
EI
2366
2367 if (arm_feature(env, ARM_FEATURE_EL2) &&
2368 timeridx == GTIMER_PHYS && !secure && cur_el < 2 &&
2369 !extract32(env->cp15.cnthctl_el2, 0, 1)) {
2370 return CP_ACCESS_TRAP_EL2;
2371 }
00108f2d
PM
2372 return CP_ACCESS_OK;
2373}
2374
3f208fd7
PM
2375static CPAccessResult gt_timer_access(CPUARMState *env, int timeridx,
2376 bool isread)
00108f2d 2377{
0b6440af
EI
2378 unsigned int cur_el = arm_current_el(env);
2379 bool secure = arm_is_secure(env);
2380
00108f2d
PM
2381 /* CNT[PV]_CVAL, CNT[PV]_CTL, CNT[PV]_TVAL: not visible from PL0 if
2382 * EL0[PV]TEN is zero.
2383 */
0b6440af 2384 if (cur_el == 0 &&
00108f2d
PM
2385 !extract32(env->cp15.c14_cntkctl, 9 - timeridx, 1)) {
2386 return CP_ACCESS_TRAP;
2387 }
0b6440af
EI
2388
2389 if (arm_feature(env, ARM_FEATURE_EL2) &&
2390 timeridx == GTIMER_PHYS && !secure && cur_el < 2 &&
2391 !extract32(env->cp15.cnthctl_el2, 1, 1)) {
2392 return CP_ACCESS_TRAP_EL2;
2393 }
00108f2d
PM
2394 return CP_ACCESS_OK;
2395}
2396
2397static CPAccessResult gt_pct_access(CPUARMState *env,
3f208fd7
PM
2398 const ARMCPRegInfo *ri,
2399 bool isread)
00108f2d 2400{
3f208fd7 2401 return gt_counter_access(env, GTIMER_PHYS, isread);
00108f2d
PM
2402}
2403
2404static CPAccessResult gt_vct_access(CPUARMState *env,
3f208fd7
PM
2405 const ARMCPRegInfo *ri,
2406 bool isread)
00108f2d 2407{
3f208fd7 2408 return gt_counter_access(env, GTIMER_VIRT, isread);
00108f2d
PM
2409}
2410
3f208fd7
PM
2411static CPAccessResult gt_ptimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
2412 bool isread)
00108f2d 2413{
3f208fd7 2414 return gt_timer_access(env, GTIMER_PHYS, isread);
00108f2d
PM
2415}
2416
3f208fd7
PM
2417static CPAccessResult gt_vtimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
2418 bool isread)
00108f2d 2419{
3f208fd7 2420 return gt_timer_access(env, GTIMER_VIRT, isread);
00108f2d
PM
2421}
2422
b4d3978c 2423static CPAccessResult gt_stimer_access(CPUARMState *env,
3f208fd7
PM
2424 const ARMCPRegInfo *ri,
2425 bool isread)
b4d3978c
PM
2426{
2427 /* The AArch64 register view of the secure physical timer is
2428 * always accessible from EL3, and configurably accessible from
2429 * Secure EL1.
2430 */
2431 switch (arm_current_el(env)) {
2432 case 1:
2433 if (!arm_is_secure(env)) {
2434 return CP_ACCESS_TRAP;
2435 }
2436 if (!(env->cp15.scr_el3 & SCR_ST)) {
2437 return CP_ACCESS_TRAP_EL3;
2438 }
2439 return CP_ACCESS_OK;
2440 case 0:
2441 case 2:
2442 return CP_ACCESS_TRAP;
2443 case 3:
2444 return CP_ACCESS_OK;
2445 default:
2446 g_assert_not_reached();
2447 }
2448}
2449
55d284af
PM
2450static uint64_t gt_get_countervalue(CPUARMState *env)
2451{
7def8754
AJ
2452 ARMCPU *cpu = env_archcpu(env);
2453
2454 return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) / gt_cntfrq_period_ns(cpu);
55d284af
PM
2455}
2456
2457static void gt_recalc_timer(ARMCPU *cpu, int timeridx)
2458{
2459 ARMGenericTimer *gt = &cpu->env.cp15.c14_timer[timeridx];
2460
2461 if (gt->ctl & 1) {
2462 /* Timer enabled: calculate and set current ISTATUS, irq, and
2463 * reset timer to when ISTATUS next has to change
2464 */
edac4d8a
EI
2465 uint64_t offset = timeridx == GTIMER_VIRT ?
2466 cpu->env.cp15.cntvoff_el2 : 0;
55d284af
PM
2467 uint64_t count = gt_get_countervalue(&cpu->env);
2468 /* Note that this must be unsigned 64 bit arithmetic: */
edac4d8a 2469 int istatus = count - offset >= gt->cval;
55d284af 2470 uint64_t nexttick;
194cbc49 2471 int irqstate;
55d284af
PM
2472
2473 gt->ctl = deposit32(gt->ctl, 2, 1, istatus);
194cbc49
PM
2474
2475 irqstate = (istatus && !(gt->ctl & 2));
2476 qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate);
2477
55d284af
PM
2478 if (istatus) {
2479 /* Next transition is when count rolls back over to zero */
2480 nexttick = UINT64_MAX;
2481 } else {
2482 /* Next transition is when we hit cval */
edac4d8a 2483 nexttick = gt->cval + offset;
55d284af
PM
2484 }
2485 /* Note that the desired next expiry time might be beyond the
2486 * signed-64-bit range of a QEMUTimer -- in this case we just
2487 * set the timer for as far in the future as possible. When the
2488 * timer expires we will reset the timer for any remaining period.
2489 */
7def8754 2490 if (nexttick > INT64_MAX / gt_cntfrq_period_ns(cpu)) {
4a0245b6
AJ
2491 timer_mod_ns(cpu->gt_timer[timeridx], INT64_MAX);
2492 } else {
2493 timer_mod(cpu->gt_timer[timeridx], nexttick);
55d284af 2494 }
194cbc49 2495 trace_arm_gt_recalc(timeridx, irqstate, nexttick);
55d284af
PM
2496 } else {
2497 /* Timer disabled: ISTATUS and timer output always clear */
2498 gt->ctl &= ~4;
2499 qemu_set_irq(cpu->gt_timer_outputs[timeridx], 0);
bc72ad67 2500 timer_del(cpu->gt_timer[timeridx]);
194cbc49 2501 trace_arm_gt_recalc_disabled(timeridx);
55d284af
PM
2502 }
2503}
2504
0e3eca4c
EI
2505static void gt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri,
2506 int timeridx)
55d284af 2507{
2fc0cc0e 2508 ARMCPU *cpu = env_archcpu(env);
55d284af 2509
bc72ad67 2510 timer_del(cpu->gt_timer[timeridx]);
55d284af
PM
2511}
2512
c4241c7d 2513static uint64_t gt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
55d284af 2514{
c4241c7d 2515 return gt_get_countervalue(env);
55d284af
PM
2516}
2517
53d1f856
RH
2518static uint64_t gt_virt_cnt_offset(CPUARMState *env)
2519{
2520 uint64_t hcr;
2521
2522 switch (arm_current_el(env)) {
2523 case 2:
2524 hcr = arm_hcr_el2_eff(env);
2525 if (hcr & HCR_E2H) {
2526 return 0;
2527 }
2528 break;
2529 case 0:
2530 hcr = arm_hcr_el2_eff(env);
2531 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2532 return 0;
2533 }
2534 break;
2535 }
2536
2537 return env->cp15.cntvoff_el2;
2538}
2539
edac4d8a
EI
2540static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
2541{
53d1f856 2542 return gt_get_countervalue(env) - gt_virt_cnt_offset(env);
edac4d8a
EI
2543}
2544
c4241c7d 2545static void gt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
0e3eca4c 2546 int timeridx,
c4241c7d 2547 uint64_t value)
55d284af 2548{
194cbc49 2549 trace_arm_gt_cval_write(timeridx, value);
55d284af 2550 env->cp15.c14_timer[timeridx].cval = value;
2fc0cc0e 2551 gt_recalc_timer(env_archcpu(env), timeridx);
55d284af 2552}
c4241c7d 2553
0e3eca4c
EI
2554static uint64_t gt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri,
2555 int timeridx)
55d284af 2556{
53d1f856
RH
2557 uint64_t offset = 0;
2558
2559 switch (timeridx) {
2560 case GTIMER_VIRT:
2561 offset = gt_virt_cnt_offset(env);
2562 break;
2563 }
55d284af 2564
c4241c7d 2565 return (uint32_t)(env->cp15.c14_timer[timeridx].cval -
edac4d8a 2566 (gt_get_countervalue(env) - offset));
55d284af
PM
2567}
2568
c4241c7d 2569static void gt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
0e3eca4c 2570 int timeridx,
c4241c7d 2571 uint64_t value)
55d284af 2572{
53d1f856
RH
2573 uint64_t offset = 0;
2574
2575 switch (timeridx) {
2576 case GTIMER_VIRT:
2577 offset = gt_virt_cnt_offset(env);
2578 break;
2579 }
55d284af 2580
194cbc49 2581 trace_arm_gt_tval_write(timeridx, value);
edac4d8a 2582 env->cp15.c14_timer[timeridx].cval = gt_get_countervalue(env) - offset +
18084b2f 2583 sextract64(value, 0, 32);
2fc0cc0e 2584 gt_recalc_timer(env_archcpu(env), timeridx);
55d284af
PM
2585}
2586
c4241c7d 2587static void gt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
0e3eca4c 2588 int timeridx,
c4241c7d 2589 uint64_t value)
55d284af 2590{
2fc0cc0e 2591 ARMCPU *cpu = env_archcpu(env);
55d284af
PM
2592 uint32_t oldval = env->cp15.c14_timer[timeridx].ctl;
2593
194cbc49 2594 trace_arm_gt_ctl_write(timeridx, value);
d3afacc7 2595 env->cp15.c14_timer[timeridx].ctl = deposit64(oldval, 0, 2, value);
55d284af
PM
2596 if ((oldval ^ value) & 1) {
2597 /* Enable toggled */
2598 gt_recalc_timer(cpu, timeridx);
d3afacc7 2599 } else if ((oldval ^ value) & 2) {
55d284af
PM
2600 /* IMASK toggled: don't need to recalculate,
2601 * just set the interrupt line based on ISTATUS
2602 */
194cbc49
PM
2603 int irqstate = (oldval & 4) && !(value & 2);
2604
2605 trace_arm_gt_imask_toggle(timeridx, irqstate);
2606 qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate);
55d284af 2607 }
55d284af
PM
2608}
2609
0e3eca4c
EI
2610static void gt_phys_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2611{
2612 gt_timer_reset(env, ri, GTIMER_PHYS);
2613}
2614
2615static void gt_phys_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2616 uint64_t value)
2617{
2618 gt_cval_write(env, ri, GTIMER_PHYS, value);
2619}
2620
2621static uint64_t gt_phys_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2622{
2623 return gt_tval_read(env, ri, GTIMER_PHYS);
2624}
2625
2626static void gt_phys_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2627 uint64_t value)
2628{
2629 gt_tval_write(env, ri, GTIMER_PHYS, value);
2630}
2631
2632static void gt_phys_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2633 uint64_t value)
2634{
2635 gt_ctl_write(env, ri, GTIMER_PHYS, value);
2636}
2637
2638static void gt_virt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2639{
2640 gt_timer_reset(env, ri, GTIMER_VIRT);
2641}
2642
2643static void gt_virt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2644 uint64_t value)
2645{
2646 gt_cval_write(env, ri, GTIMER_VIRT, value);
2647}
2648
2649static uint64_t gt_virt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2650{
2651 return gt_tval_read(env, ri, GTIMER_VIRT);
2652}
2653
2654static void gt_virt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2655 uint64_t value)
2656{
2657 gt_tval_write(env, ri, GTIMER_VIRT, value);
2658}
2659
2660static void gt_virt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2661 uint64_t value)
2662{
2663 gt_ctl_write(env, ri, GTIMER_VIRT, value);
2664}
2665
edac4d8a
EI
2666static void gt_cntvoff_write(CPUARMState *env, const ARMCPRegInfo *ri,
2667 uint64_t value)
2668{
2fc0cc0e 2669 ARMCPU *cpu = env_archcpu(env);
edac4d8a 2670
194cbc49 2671 trace_arm_gt_cntvoff_write(value);
edac4d8a
EI
2672 raw_write(env, ri, value);
2673 gt_recalc_timer(cpu, GTIMER_VIRT);
2674}
2675
b0e66d95
EI
2676static void gt_hyp_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2677{
2678 gt_timer_reset(env, ri, GTIMER_HYP);
2679}
2680
2681static void gt_hyp_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2682 uint64_t value)
2683{
2684 gt_cval_write(env, ri, GTIMER_HYP, value);
2685}
2686
2687static uint64_t gt_hyp_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2688{
2689 return gt_tval_read(env, ri, GTIMER_HYP);
2690}
2691
2692static void gt_hyp_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2693 uint64_t value)
2694{
2695 gt_tval_write(env, ri, GTIMER_HYP, value);
2696}
2697
2698static void gt_hyp_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2699 uint64_t value)
2700{
2701 gt_ctl_write(env, ri, GTIMER_HYP, value);
2702}
2703
b4d3978c
PM
2704static void gt_sec_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2705{
2706 gt_timer_reset(env, ri, GTIMER_SEC);
2707}
2708
2709static void gt_sec_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2710 uint64_t value)
2711{
2712 gt_cval_write(env, ri, GTIMER_SEC, value);
2713}
2714
2715static uint64_t gt_sec_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2716{
2717 return gt_tval_read(env, ri, GTIMER_SEC);
2718}
2719
2720static void gt_sec_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2721 uint64_t value)
2722{
2723 gt_tval_write(env, ri, GTIMER_SEC, value);
2724}
2725
2726static void gt_sec_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2727 uint64_t value)
2728{
2729 gt_ctl_write(env, ri, GTIMER_SEC, value);
2730}
2731
55d284af
PM
2732void arm_gt_ptimer_cb(void *opaque)
2733{
2734 ARMCPU *cpu = opaque;
2735
2736 gt_recalc_timer(cpu, GTIMER_PHYS);
2737}
2738
2739void arm_gt_vtimer_cb(void *opaque)
2740{
2741 ARMCPU *cpu = opaque;
2742
2743 gt_recalc_timer(cpu, GTIMER_VIRT);
2744}
2745
b0e66d95
EI
2746void arm_gt_htimer_cb(void *opaque)
2747{
2748 ARMCPU *cpu = opaque;
2749
2750 gt_recalc_timer(cpu, GTIMER_HYP);
2751}
2752
b4d3978c
PM
2753void arm_gt_stimer_cb(void *opaque)
2754{
2755 ARMCPU *cpu = opaque;
2756
2757 gt_recalc_timer(cpu, GTIMER_SEC);
2758}
2759
96eec6b2
AJ
2760static void arm_gt_cntfrq_reset(CPUARMState *env, const ARMCPRegInfo *opaque)
2761{
2762 ARMCPU *cpu = env_archcpu(env);
2763
2764 cpu->env.cp15.c14_cntfrq = cpu->gt_cntfrq_hz;
2765}
2766
55d284af
PM
2767static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
2768 /* Note that CNTFRQ is purely reads-as-written for the benefit
2769 * of software; writing it doesn't actually change the timer frequency.
2770 * Our reset value matches the fixed frequency we implement the timer at.
2771 */
2772 { .name = "CNTFRQ", .cp = 15, .crn = 14, .crm = 0, .opc1 = 0, .opc2 = 0,
7a0e58fa 2773 .type = ARM_CP_ALIAS,
a7adc4b7
PM
2774 .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
2775 .fieldoffset = offsetoflow32(CPUARMState, cp15.c14_cntfrq),
a7adc4b7
PM
2776 },
2777 { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
2778 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
2779 .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
55d284af 2780 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
96eec6b2 2781 .resetfn = arm_gt_cntfrq_reset,
55d284af
PM
2782 },
2783 /* overall control: mostly access permissions */
a7adc4b7
PM
2784 { .name = "CNTKCTL", .state = ARM_CP_STATE_BOTH,
2785 .opc0 = 3, .opc1 = 0, .crn = 14, .crm = 1, .opc2 = 0,
55d284af
PM
2786 .access = PL1_RW,
2787 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntkctl),
2788 .resetvalue = 0,
2789 },
2790 /* per-timer control */
2791 { .name = "CNTP_CTL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
9ff9dd3c 2792 .secure = ARM_CP_SECSTATE_NS,
daf1dc5f 2793 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
a7adc4b7
PM
2794 .accessfn = gt_ptimer_access,
2795 .fieldoffset = offsetoflow32(CPUARMState,
2796 cp15.c14_timer[GTIMER_PHYS].ctl),
0e3eca4c 2797 .writefn = gt_phys_ctl_write, .raw_writefn = raw_write,
a7adc4b7 2798 },
9c513e78 2799 { .name = "CNTP_CTL_S",
9ff9dd3c
PM
2800 .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
2801 .secure = ARM_CP_SECSTATE_S,
daf1dc5f 2802 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
9ff9dd3c
PM
2803 .accessfn = gt_ptimer_access,
2804 .fieldoffset = offsetoflow32(CPUARMState,
2805 cp15.c14_timer[GTIMER_SEC].ctl),
2806 .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
2807 },
a7adc4b7
PM
2808 { .name = "CNTP_CTL_EL0", .state = ARM_CP_STATE_AA64,
2809 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 1,
daf1dc5f 2810 .type = ARM_CP_IO, .access = PL0_RW,
a7adc4b7 2811 .accessfn = gt_ptimer_access,
55d284af
PM
2812 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
2813 .resetvalue = 0,
0e3eca4c 2814 .writefn = gt_phys_ctl_write, .raw_writefn = raw_write,
55d284af
PM
2815 },
2816 { .name = "CNTV_CTL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 1,
daf1dc5f 2817 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
a7adc4b7
PM
2818 .accessfn = gt_vtimer_access,
2819 .fieldoffset = offsetoflow32(CPUARMState,
2820 cp15.c14_timer[GTIMER_VIRT].ctl),
0e3eca4c 2821 .writefn = gt_virt_ctl_write, .raw_writefn = raw_write,
a7adc4b7
PM
2822 },
2823 { .name = "CNTV_CTL_EL0", .state = ARM_CP_STATE_AA64,
2824 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 1,
daf1dc5f 2825 .type = ARM_CP_IO, .access = PL0_RW,
a7adc4b7 2826 .accessfn = gt_vtimer_access,
55d284af
PM
2827 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
2828 .resetvalue = 0,
0e3eca4c 2829 .writefn = gt_virt_ctl_write, .raw_writefn = raw_write,
55d284af
PM
2830 },
2831 /* TimerValue views: a 32 bit downcounting view of the underlying state */
2832 { .name = "CNTP_TVAL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
9ff9dd3c 2833 .secure = ARM_CP_SECSTATE_NS,
daf1dc5f 2834 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
00108f2d 2835 .accessfn = gt_ptimer_access,
0e3eca4c 2836 .readfn = gt_phys_tval_read, .writefn = gt_phys_tval_write,
55d284af 2837 },
9c513e78 2838 { .name = "CNTP_TVAL_S",
9ff9dd3c
PM
2839 .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
2840 .secure = ARM_CP_SECSTATE_S,
daf1dc5f 2841 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
9ff9dd3c
PM
2842 .accessfn = gt_ptimer_access,
2843 .readfn = gt_sec_tval_read, .writefn = gt_sec_tval_write,
2844 },
a7adc4b7
PM
2845 { .name = "CNTP_TVAL_EL0", .state = ARM_CP_STATE_AA64,
2846 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 0,
daf1dc5f 2847 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
0e3eca4c
EI
2848 .accessfn = gt_ptimer_access, .resetfn = gt_phys_timer_reset,
2849 .readfn = gt_phys_tval_read, .writefn = gt_phys_tval_write,
a7adc4b7 2850 },
55d284af 2851 { .name = "CNTV_TVAL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 0,
daf1dc5f 2852 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
00108f2d 2853 .accessfn = gt_vtimer_access,
0e3eca4c 2854 .readfn = gt_virt_tval_read, .writefn = gt_virt_tval_write,
55d284af 2855 },
a7adc4b7
PM
2856 { .name = "CNTV_TVAL_EL0", .state = ARM_CP_STATE_AA64,
2857 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 0,
daf1dc5f 2858 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
0e3eca4c
EI
2859 .accessfn = gt_vtimer_access, .resetfn = gt_virt_timer_reset,
2860 .readfn = gt_virt_tval_read, .writefn = gt_virt_tval_write,
a7adc4b7 2861 },
55d284af
PM
2862 /* The counter itself */
2863 { .name = "CNTPCT", .cp = 15, .crm = 14, .opc1 = 0,
7a0e58fa 2864 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
00108f2d 2865 .accessfn = gt_pct_access,
a7adc4b7
PM
2866 .readfn = gt_cnt_read, .resetfn = arm_cp_reset_ignore,
2867 },
2868 { .name = "CNTPCT_EL0", .state = ARM_CP_STATE_AA64,
2869 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 1,
7a0e58fa 2870 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
d57b9ee8 2871 .accessfn = gt_pct_access, .readfn = gt_cnt_read,
55d284af
PM
2872 },
2873 { .name = "CNTVCT", .cp = 15, .crm = 14, .opc1 = 1,
7a0e58fa 2874 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
00108f2d 2875 .accessfn = gt_vct_access,
edac4d8a 2876 .readfn = gt_virt_cnt_read, .resetfn = arm_cp_reset_ignore,
a7adc4b7
PM
2877 },
2878 { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
2879 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
7a0e58fa 2880 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
d57b9ee8 2881 .accessfn = gt_vct_access, .readfn = gt_virt_cnt_read,
55d284af
PM
2882 },
2883 /* Comparison value, indicating when the timer goes off */
2884 { .name = "CNTP_CVAL", .cp = 15, .crm = 14, .opc1 = 2,
9ff9dd3c 2885 .secure = ARM_CP_SECSTATE_NS,
daf1dc5f 2886 .access = PL0_RW,
7a0e58fa 2887 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
55d284af 2888 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
b061a82b 2889 .accessfn = gt_ptimer_access,
0e3eca4c 2890 .writefn = gt_phys_cval_write, .raw_writefn = raw_write,
a7adc4b7 2891 },
9c513e78 2892 { .name = "CNTP_CVAL_S", .cp = 15, .crm = 14, .opc1 = 2,
9ff9dd3c 2893 .secure = ARM_CP_SECSTATE_S,
daf1dc5f 2894 .access = PL0_RW,
9ff9dd3c
PM
2895 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
2896 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
2897 .accessfn = gt_ptimer_access,
2898 .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
2899 },
a7adc4b7
PM
2900 { .name = "CNTP_CVAL_EL0", .state = ARM_CP_STATE_AA64,
2901 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 2,
daf1dc5f 2902 .access = PL0_RW,
a7adc4b7
PM
2903 .type = ARM_CP_IO,
2904 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
12cde08a 2905 .resetvalue = 0, .accessfn = gt_ptimer_access,
0e3eca4c 2906 .writefn = gt_phys_cval_write, .raw_writefn = raw_write,
55d284af
PM
2907 },
2908 { .name = "CNTV_CVAL", .cp = 15, .crm = 14, .opc1 = 3,
daf1dc5f 2909 .access = PL0_RW,
7a0e58fa 2910 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
55d284af 2911 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
b061a82b 2912 .accessfn = gt_vtimer_access,
0e3eca4c 2913 .writefn = gt_virt_cval_write, .raw_writefn = raw_write,
a7adc4b7
PM
2914 },
2915 { .name = "CNTV_CVAL_EL0", .state = ARM_CP_STATE_AA64,
2916 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 2,
daf1dc5f 2917 .access = PL0_RW,
a7adc4b7
PM
2918 .type = ARM_CP_IO,
2919 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
2920 .resetvalue = 0, .accessfn = gt_vtimer_access,
0e3eca4c 2921 .writefn = gt_virt_cval_write, .raw_writefn = raw_write,
55d284af 2922 },
b4d3978c
PM
2923 /* Secure timer -- this is actually restricted to only EL3
2924 * and configurably Secure-EL1 via the accessfn.
2925 */
2926 { .name = "CNTPS_TVAL_EL1", .state = ARM_CP_STATE_AA64,
2927 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 0,
2928 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW,
2929 .accessfn = gt_stimer_access,
2930 .readfn = gt_sec_tval_read,
2931 .writefn = gt_sec_tval_write,
2932 .resetfn = gt_sec_timer_reset,
2933 },
2934 { .name = "CNTPS_CTL_EL1", .state = ARM_CP_STATE_AA64,
2935 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 1,
2936 .type = ARM_CP_IO, .access = PL1_RW,
2937 .accessfn = gt_stimer_access,
2938 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].ctl),
2939 .resetvalue = 0,
2940 .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
2941 },
2942 { .name = "CNTPS_CVAL_EL1", .state = ARM_CP_STATE_AA64,
2943 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 2,
2944 .type = ARM_CP_IO, .access = PL1_RW,
2945 .accessfn = gt_stimer_access,
2946 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
2947 .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
2948 },
55d284af
PM
2949 REGINFO_SENTINEL
2950};
2951
2952#else
26c4a83b
AB
2953
2954/* In user-mode most of the generic timer registers are inaccessible
2955 * however modern kernels (4.12+) allow access to cntvct_el0
55d284af 2956 */
26c4a83b
AB
2957
2958static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
2959{
7def8754
AJ
2960 ARMCPU *cpu = env_archcpu(env);
2961
26c4a83b
AB
2962 /* Currently we have no support for QEMUTimer in linux-user so we
2963 * can't call gt_get_countervalue(env), instead we directly
2964 * call the lower level functions.
2965 */
7def8754 2966 return cpu_get_clock() / gt_cntfrq_period_ns(cpu);
26c4a83b
AB
2967}
2968
6cc7a3ae 2969static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
26c4a83b
AB
2970 { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
2971 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
2972 .type = ARM_CP_CONST, .access = PL0_R /* no PL1_RW in linux-user */,
2973 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
2974 .resetvalue = NANOSECONDS_PER_SECOND / GTIMER_SCALE,
2975 },
2976 { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
2977 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
2978 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2979 .readfn = gt_virt_cnt_read,
2980 },
6cc7a3ae
PM
2981 REGINFO_SENTINEL
2982};
2983
55d284af
PM
2984#endif
2985
c4241c7d 2986static void par_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
4a501606 2987{
891a2fe7 2988 if (arm_feature(env, ARM_FEATURE_LPAE)) {
8d5c773e 2989 raw_write(env, ri, value);
891a2fe7 2990 } else if (arm_feature(env, ARM_FEATURE_V7)) {
8d5c773e 2991 raw_write(env, ri, value & 0xfffff6ff);
4a501606 2992 } else {
8d5c773e 2993 raw_write(env, ri, value & 0xfffff1ff);
4a501606 2994 }
4a501606
PM
2995}
2996
2997#ifndef CONFIG_USER_ONLY
2998/* get_phys_addr() isn't present for user-mode-only targets */
702a9357 2999
3f208fd7
PM
3000static CPAccessResult ats_access(CPUARMState *env, const ARMCPRegInfo *ri,
3001 bool isread)
92611c00
PM
3002{
3003 if (ri->opc2 & 4) {
87562e4f
PM
3004 /* The ATS12NSO* operations must trap to EL3 if executed in
3005 * Secure EL1 (which can only happen if EL3 is AArch64).
3006 * They are simply UNDEF if executed from NS EL1.
3007 * They function normally from EL2 or EL3.
92611c00 3008 */
87562e4f
PM
3009 if (arm_current_el(env) == 1) {
3010 if (arm_is_secure_below_el3(env)) {
3011 return CP_ACCESS_TRAP_UNCATEGORIZED_EL3;
3012 }
3013 return CP_ACCESS_TRAP_UNCATEGORIZED;
3014 }
92611c00
PM
3015 }
3016 return CP_ACCESS_OK;
3017}
3018
060e8a48 3019static uint64_t do_ats_write(CPUARMState *env, uint64_t value,
03ae85f8 3020 MMUAccessType access_type, ARMMMUIdx mmu_idx)
4a501606 3021{
a8170e5e 3022 hwaddr phys_addr;
4a501606
PM
3023 target_ulong page_size;
3024 int prot;
b7cc4e82 3025 bool ret;
01c097f7 3026 uint64_t par64;
1313e2d7 3027 bool format64 = false;
8bf5b6a9 3028 MemTxAttrs attrs = {};
e14b5a23 3029 ARMMMUFaultInfo fi = {};
5b2d261d 3030 ARMCacheAttrs cacheattrs = {};
4a501606 3031
5b2d261d 3032 ret = get_phys_addr(env, value, access_type, mmu_idx, &phys_addr, &attrs,
bc52bfeb 3033 &prot, &page_size, &fi, &cacheattrs);
1313e2d7 3034
0710b2fa
PM
3035 if (ret) {
3036 /*
3037 * Some kinds of translation fault must cause exceptions rather
3038 * than being reported in the PAR.
3039 */
3040 int current_el = arm_current_el(env);
3041 int target_el;
3042 uint32_t syn, fsr, fsc;
3043 bool take_exc = false;
3044
3045 if (fi.s1ptw && current_el == 1 && !arm_is_secure(env)
3046 && (mmu_idx == ARMMMUIdx_S1NSE1 || mmu_idx == ARMMMUIdx_S1NSE0)) {
3047 /*
3048 * Synchronous stage 2 fault on an access made as part of the
3049 * translation table walk for AT S1E0* or AT S1E1* insn
3050 * executed from NS EL1. If this is a synchronous external abort
3051 * and SCR_EL3.EA == 1, then we take a synchronous external abort
3052 * to EL3. Otherwise the fault is taken as an exception to EL2,
3053 * and HPFAR_EL2 holds the faulting IPA.
3054 */
3055 if (fi.type == ARMFault_SyncExternalOnWalk &&
3056 (env->cp15.scr_el3 & SCR_EA)) {
3057 target_el = 3;
3058 } else {
3059 env->cp15.hpfar_el2 = extract64(fi.s2addr, 12, 47) << 4;
3060 target_el = 2;
3061 }
3062 take_exc = true;
3063 } else if (fi.type == ARMFault_SyncExternalOnWalk) {
3064 /*
3065 * Synchronous external aborts during a translation table walk
3066 * are taken as Data Abort exceptions.
3067 */
3068 if (fi.stage2) {
3069 if (current_el == 3) {
3070 target_el = 3;
3071 } else {
3072 target_el = 2;
3073 }
3074 } else {
3075 target_el = exception_target_el(env);
3076 }
3077 take_exc = true;
3078 }
3079
3080 if (take_exc) {
3081 /* Construct FSR and FSC using same logic as arm_deliver_fault() */
3082 if (target_el == 2 || arm_el_is_aa64(env, target_el) ||
3083 arm_s1_regime_using_lpae_format(env, mmu_idx)) {
3084 fsr = arm_fi_to_lfsc(&fi);
3085 fsc = extract32(fsr, 0, 6);
3086 } else {
3087 fsr = arm_fi_to_sfsc(&fi);
3088 fsc = 0x3f;
3089 }
3090 /*
3091 * Report exception with ESR indicating a fault due to a
3092 * translation table walk for a cache maintenance instruction.
3093 */
3094 syn = syn_data_abort_no_iss(current_el == target_el,
3095 fi.ea, 1, fi.s1ptw, 1, fsc);
3096 env->exception.vaddress = value;
3097 env->exception.fsr = fsr;
3098 raise_exception(env, EXCP_DATA_ABORT, syn, target_el);
3099 }
3100 }
3101
1313e2d7
EI
3102 if (is_a64(env)) {
3103 format64 = true;
3104 } else if (arm_feature(env, ARM_FEATURE_LPAE)) {
3105 /*
3106 * ATS1Cxx:
3107 * * TTBCR.EAE determines whether the result is returned using the
3108 * 32-bit or the 64-bit PAR format
3109 * * Instructions executed in Hyp mode always use the 64bit format
3110 *
3111 * ATS1S2NSOxx uses the 64bit format if any of the following is true:
3112 * * The Non-secure TTBCR.EAE bit is set to 1
3113 * * The implementation includes EL2, and the value of HCR.VM is 1
3114 *
9d1bab33
PM
3115 * (Note that HCR.DC makes HCR.VM behave as if it is 1.)
3116 *
23463e0e 3117 * ATS1Hx always uses the 64bit format.
1313e2d7
EI
3118 */
3119 format64 = arm_s1_regime_using_lpae_format(env, mmu_idx);
3120
3121 if (arm_feature(env, ARM_FEATURE_EL2)) {
3122 if (mmu_idx == ARMMMUIdx_S12NSE0 || mmu_idx == ARMMMUIdx_S12NSE1) {
9d1bab33 3123 format64 |= env->cp15.hcr_el2 & (HCR_VM | HCR_DC);
1313e2d7
EI
3124 } else {
3125 format64 |= arm_current_el(env) == 2;
3126 }
3127 }
3128 }
3129
3130 if (format64) {
5efe9ed4 3131 /* Create a 64-bit PAR */
01c097f7 3132 par64 = (1 << 11); /* LPAE bit always set */
b7cc4e82 3133 if (!ret) {
702a9357 3134 par64 |= phys_addr & ~0xfffULL;
8bf5b6a9
PM
3135 if (!attrs.secure) {
3136 par64 |= (1 << 9); /* NS */
3137 }
5b2d261d
AB
3138 par64 |= (uint64_t)cacheattrs.attrs << 56; /* ATTR */
3139 par64 |= cacheattrs.shareability << 7; /* SH */
4a501606 3140 } else {
5efe9ed4
PM
3141 uint32_t fsr = arm_fi_to_lfsc(&fi);
3142
702a9357 3143 par64 |= 1; /* F */
b7cc4e82 3144 par64 |= (fsr & 0x3f) << 1; /* FS */
0f7b791b
PM
3145 if (fi.stage2) {
3146 par64 |= (1 << 9); /* S */
3147 }
3148 if (fi.s1ptw) {
3149 par64 |= (1 << 8); /* PTW */
3150 }
4a501606
PM
3151 }
3152 } else {
b7cc4e82 3153 /* fsr is a DFSR/IFSR value for the short descriptor
702a9357
PM
3154 * translation table format (with WnR always clear).
3155 * Convert it to a 32-bit PAR.
3156 */
b7cc4e82 3157 if (!ret) {
702a9357
PM
3158 /* We do not set any attribute bits in the PAR */
3159 if (page_size == (1 << 24)
3160 && arm_feature(env, ARM_FEATURE_V7)) {
01c097f7 3161 par64 = (phys_addr & 0xff000000) | (1 << 1);
702a9357 3162 } else {
01c097f7 3163 par64 = phys_addr & 0xfffff000;
702a9357 3164 }
8bf5b6a9
PM
3165 if (!attrs.secure) {
3166 par64 |= (1 << 9); /* NS */
3167 }
702a9357 3168 } else {
5efe9ed4
PM
3169 uint32_t fsr = arm_fi_to_sfsc(&fi);
3170
b7cc4e82
PC
3171 par64 = ((fsr & (1 << 10)) >> 5) | ((fsr & (1 << 12)) >> 6) |
3172 ((fsr & 0xf) << 1) | 1;
702a9357 3173 }
4a501606 3174 }
060e8a48
PM
3175 return par64;
3176}
3177
3178static void ats_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
3179{
03ae85f8 3180 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
060e8a48 3181 uint64_t par64;
d3649702
PM
3182 ARMMMUIdx mmu_idx;
3183 int el = arm_current_el(env);
3184 bool secure = arm_is_secure_below_el3(env);
060e8a48 3185
d3649702
PM
3186 switch (ri->opc2 & 6) {
3187 case 0:
3188 /* stage 1 current state PL1: ATS1CPR, ATS1CPW */
3189 switch (el) {
3190 case 3:
3191 mmu_idx = ARMMMUIdx_S1E3;
3192 break;
3193 case 2:
3194 mmu_idx = ARMMMUIdx_S1NSE1;
3195 break;
3196 case 1:
3197 mmu_idx = secure ? ARMMMUIdx_S1SE1 : ARMMMUIdx_S1NSE1;
3198 break;
3199 default:
3200 g_assert_not_reached();
3201 }
3202 break;
3203 case 2:
3204 /* stage 1 current state PL0: ATS1CUR, ATS1CUW */
3205 switch (el) {
3206 case 3:
3207 mmu_idx = ARMMMUIdx_S1SE0;
3208 break;
3209 case 2:
3210 mmu_idx = ARMMMUIdx_S1NSE0;
3211 break;
3212 case 1:
3213 mmu_idx = secure ? ARMMMUIdx_S1SE0 : ARMMMUIdx_S1NSE0;
3214 break;
3215 default:
3216 g_assert_not_reached();
3217 }
3218 break;
3219 case 4:
3220 /* stage 1+2 NonSecure PL1: ATS12NSOPR, ATS12NSOPW */
3221 mmu_idx = ARMMMUIdx_S12NSE1;
3222 break;
3223 case 6:
3224 /* stage 1+2 NonSecure PL0: ATS12NSOUR, ATS12NSOUW */
3225 mmu_idx = ARMMMUIdx_S12NSE0;
3226 break;
3227 default:
3228 g_assert_not_reached();
3229 }
3230
3231 par64 = do_ats_write(env, value, access_type, mmu_idx);
01c097f7
FA
3232
3233 A32_BANKED_CURRENT_REG_SET(env, par, par64);
4a501606 3234}
060e8a48 3235
14db7fe0
PM
3236static void ats1h_write(CPUARMState *env, const ARMCPRegInfo *ri,
3237 uint64_t value)
3238{
03ae85f8 3239 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
14db7fe0
PM
3240 uint64_t par64;
3241
23463e0e 3242 par64 = do_ats_write(env, value, access_type, ARMMMUIdx_S1E2);
14db7fe0
PM
3243
3244 A32_BANKED_CURRENT_REG_SET(env, par, par64);
3245}
3246
3f208fd7
PM
3247static CPAccessResult at_s1e2_access(CPUARMState *env, const ARMCPRegInfo *ri,
3248 bool isread)
2a47df95
PM
3249{
3250 if (arm_current_el(env) == 3 && !(env->cp15.scr_el3 & SCR_NS)) {
3251 return CP_ACCESS_TRAP;
3252 }
3253 return CP_ACCESS_OK;
3254}
3255
060e8a48
PM
3256static void ats_write64(CPUARMState *env, const ARMCPRegInfo *ri,
3257 uint64_t value)
3258{
03ae85f8 3259 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
d3649702
PM
3260 ARMMMUIdx mmu_idx;
3261 int secure = arm_is_secure_below_el3(env);
3262
3263 switch (ri->opc2 & 6) {
3264 case 0:
3265 switch (ri->opc1) {
3266 case 0: /* AT S1E1R, AT S1E1W */
3267 mmu_idx = secure ? ARMMMUIdx_S1SE1 : ARMMMUIdx_S1NSE1;
3268 break;
3269 case 4: /* AT S1E2R, AT S1E2W */
3270 mmu_idx = ARMMMUIdx_S1E2;
3271 break;
3272 case 6: /* AT S1E3R, AT S1E3W */
3273 mmu_idx = ARMMMUIdx_S1E3;
3274 break;
3275 default:
3276 g_assert_not_reached();
3277 }
3278 break;
3279 case 2: /* AT S1E0R, AT S1E0W */
3280 mmu_idx = secure ? ARMMMUIdx_S1SE0 : ARMMMUIdx_S1NSE0;
3281 break;
3282 case 4: /* AT S12E1R, AT S12E1W */
2a47df95 3283 mmu_idx = secure ? ARMMMUIdx_S1SE1 : ARMMMUIdx_S12NSE1;
d3649702
PM
3284 break;
3285 case 6: /* AT S12E0R, AT S12E0W */
2a47df95 3286 mmu_idx = secure ? ARMMMUIdx_S1SE0 : ARMMMUIdx_S12NSE0;
d3649702
PM
3287 break;
3288 default:
3289 g_assert_not_reached();
3290 }
060e8a48 3291
d3649702 3292 env->cp15.par_el[1] = do_ats_write(env, value, access_type, mmu_idx);
060e8a48 3293}
4a501606
PM
3294#endif
3295
3296static const ARMCPRegInfo vapa_cp_reginfo[] = {
3297 { .name = "PAR", .cp = 15, .crn = 7, .crm = 4, .opc1 = 0, .opc2 = 0,
3298 .access = PL1_RW, .resetvalue = 0,
01c097f7
FA
3299 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.par_s),
3300 offsetoflow32(CPUARMState, cp15.par_ns) },
4a501606
PM
3301 .writefn = par_write },
3302#ifndef CONFIG_USER_ONLY
87562e4f 3303 /* This underdecoding is safe because the reginfo is NO_RAW. */
4a501606 3304 { .name = "ATS", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = CP_ANY,
92611c00 3305 .access = PL1_W, .accessfn = ats_access,
0710b2fa 3306 .writefn = ats_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
4a501606
PM
3307#endif
3308 REGINFO_SENTINEL
3309};
3310
18032bec
PM
3311/* Return basic MPU access permission bits. */
3312static uint32_t simple_mpu_ap_bits(uint32_t val)
3313{
3314 uint32_t ret;
3315 uint32_t mask;
3316 int i;
3317 ret = 0;
3318 mask = 3;
3319 for (i = 0; i < 16; i += 2) {
3320 ret |= (val >> i) & mask;
3321 mask <<= 2;
3322 }
3323 return ret;
3324}
3325
3326/* Pad basic MPU access permission bits to extended format. */
3327static uint32_t extended_mpu_ap_bits(uint32_t val)
3328{
3329 uint32_t ret;
3330 uint32_t mask;
3331 int i;
3332 ret = 0;
3333 mask = 3;
3334 for (i = 0; i < 16; i += 2) {
3335 ret |= (val & mask) << i;
3336 mask <<= 2;
3337 }
3338 return ret;
3339}
3340
c4241c7d
PM
3341static void pmsav5_data_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
3342 uint64_t value)
18032bec 3343{
7e09797c 3344 env->cp15.pmsav5_data_ap = extended_mpu_ap_bits(value);
18032bec
PM
3345}
3346
c4241c7d 3347static uint64_t pmsav5_data_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
18032bec 3348{
7e09797c 3349 return simple_mpu_ap_bits(env->cp15.pmsav5_data_ap);
18032bec
PM
3350}
3351
c4241c7d
PM
3352static void pmsav5_insn_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
3353 uint64_t value)
18032bec 3354{
7e09797c 3355 env->cp15.pmsav5_insn_ap = extended_mpu_ap_bits(value);
18032bec
PM
3356}
3357
c4241c7d 3358static uint64_t pmsav5_insn_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
18032bec 3359{
7e09797c 3360 return simple_mpu_ap_bits(env->cp15.pmsav5_insn_ap);
18032bec
PM
3361}
3362
6cb0b013
PC
3363static uint64_t pmsav7_read(CPUARMState *env, const ARMCPRegInfo *ri)
3364{
3365 uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
3366
3367 if (!u32p) {
3368 return 0;
3369 }
3370
1bc04a88 3371 u32p += env->pmsav7.rnr[M_REG_NS];
6cb0b013
PC
3372 return *u32p;
3373}
3374
3375static void pmsav7_write(CPUARMState *env, const ARMCPRegInfo *ri,
3376 uint64_t value)
3377{
2fc0cc0e 3378 ARMCPU *cpu = env_archcpu(env);
6cb0b013
PC
3379 uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
3380
3381 if (!u32p) {
3382 return;
3383 }
3384
1bc04a88 3385 u32p += env->pmsav7.rnr[M_REG_NS];
d10eb08f 3386 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
6cb0b013
PC
3387 *u32p = value;
3388}
3389
6cb0b013
PC
3390static void pmsav7_rgnr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3391 uint64_t value)
3392{
2fc0cc0e 3393 ARMCPU *cpu = env_archcpu(env);
6cb0b013
PC
3394 uint32_t nrgs = cpu->pmsav7_dregion;
3395
3396 if (value >= nrgs) {
3397 qemu_log_mask(LOG_GUEST_ERROR,
3398 "PMSAv7 RGNR write >= # supported regions, %" PRIu32
3399 " > %" PRIu32 "\n", (uint32_t)value, nrgs);
3400 return;
3401 }
3402
3403 raw_write(env, ri, value);
3404}
3405
3406static const ARMCPRegInfo pmsav7_cp_reginfo[] = {
69ceea64
PM
3407 /* Reset for all these registers is handled in arm_cpu_reset(),
3408 * because the PMSAv7 is also used by M-profile CPUs, which do
3409 * not register cpregs but still need the state to be reset.
3410 */
6cb0b013
PC
3411 { .name = "DRBAR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 0,
3412 .access = PL1_RW, .type = ARM_CP_NO_RAW,
3413 .fieldoffset = offsetof(CPUARMState, pmsav7.drbar),
69ceea64
PM
3414 .readfn = pmsav7_read, .writefn = pmsav7_write,
3415 .resetfn = arm_cp_reset_ignore },
6cb0b013
PC
3416 { .name = "DRSR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 2,
3417 .access = PL1_RW, .type = ARM_CP_NO_RAW,
3418 .fieldoffset = offsetof(CPUARMState, pmsav7.drsr),
69ceea64
PM
3419 .readfn = pmsav7_read, .writefn = pmsav7_write,
3420 .resetfn = arm_cp_reset_ignore },
6cb0b013
PC
3421 { .name = "DRACR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 4,
3422 .access = PL1_RW, .type = ARM_CP_NO_RAW,
3423 .fieldoffset = offsetof(CPUARMState, pmsav7.dracr),
69ceea64
PM
3424 .readfn = pmsav7_read, .writefn = pmsav7_write,
3425 .resetfn = arm_cp_reset_ignore },
6cb0b013
PC
3426 { .name = "RGNR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 2, .opc2 = 0,
3427 .access = PL1_RW,
1bc04a88 3428 .fieldoffset = offsetof(CPUARMState, pmsav7.rnr[M_REG_NS]),
69ceea64
PM
3429 .writefn = pmsav7_rgnr_write,
3430 .resetfn = arm_cp_reset_ignore },
6cb0b013
PC
3431 REGINFO_SENTINEL
3432};
3433
18032bec
PM
3434static const ARMCPRegInfo pmsav5_cp_reginfo[] = {
3435 { .name = "DATA_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
7a0e58fa 3436 .access = PL1_RW, .type = ARM_CP_ALIAS,
7e09797c 3437 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
18032bec
PM
3438 .readfn = pmsav5_data_ap_read, .writefn = pmsav5_data_ap_write, },
3439 { .name = "INSN_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
7a0e58fa 3440 .access = PL1_RW, .type = ARM_CP_ALIAS,
7e09797c 3441 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
18032bec
PM
3442 .readfn = pmsav5_insn_ap_read, .writefn = pmsav5_insn_ap_write, },
3443 { .name = "DATA_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 2,
3444 .access = PL1_RW,
7e09797c
PM
3445 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
3446 .resetvalue = 0, },
18032bec
PM
3447 { .name = "INSN_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 3,
3448 .access = PL1_RW,
7e09797c
PM
3449 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
3450 .resetvalue = 0, },
ecce5c3c
PM
3451 { .name = "DCACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
3452 .access = PL1_RW,
3453 .fieldoffset = offsetof(CPUARMState, cp15.c2_data), .resetvalue = 0, },
3454 { .name = "ICACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 1,
3455 .access = PL1_RW,
3456 .fieldoffset = offsetof(CPUARMState, cp15.c2_insn), .resetvalue = 0, },
06d76f31 3457 /* Protection region base and size registers */
e508a92b
PM
3458 { .name = "946_PRBS0", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0,
3459 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3460 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[0]) },
3461 { .name = "946_PRBS1", .cp = 15, .crn = 6, .crm = 1, .opc1 = 0,
3462 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3463 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[1]) },
3464 { .name = "946_PRBS2", .cp = 15, .crn = 6, .crm = 2, .opc1 = 0,
3465 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3466 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[2]) },
3467 { .name = "946_PRBS3", .cp = 15, .crn = 6, .crm = 3, .opc1 = 0,
3468 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3469 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[3]) },
3470 { .name = "946_PRBS4", .cp = 15, .crn = 6, .crm = 4, .opc1 = 0,
3471 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3472 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[4]) },
3473 { .name = "946_PRBS5", .cp = 15, .crn = 6, .crm = 5, .opc1 = 0,
3474 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3475 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[5]) },
3476 { .name = "946_PRBS6", .cp = 15, .crn = 6, .crm = 6, .opc1 = 0,
3477 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3478 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[6]) },
3479 { .name = "946_PRBS7", .cp = 15, .crn = 6, .crm = 7, .opc1 = 0,
3480 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3481 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[7]) },
18032bec
PM
3482 REGINFO_SENTINEL
3483};
3484
c4241c7d
PM
3485static void vmsa_ttbcr_raw_write(CPUARMState *env, const ARMCPRegInfo *ri,
3486 uint64_t value)
ecce5c3c 3487{
11f136ee 3488 TCR *tcr = raw_ptr(env, ri);
2ebcebe2
PM
3489 int maskshift = extract32(value, 0, 3);
3490
e389be16
FA
3491 if (!arm_feature(env, ARM_FEATURE_V8)) {
3492 if (arm_feature(env, ARM_FEATURE_LPAE) && (value & TTBCR_EAE)) {
3493 /* Pre ARMv8 bits [21:19], [15:14] and [6:3] are UNK/SBZP when
3494 * using Long-desciptor translation table format */
3495 value &= ~((7 << 19) | (3 << 14) | (0xf << 3));
3496 } else if (arm_feature(env, ARM_FEATURE_EL3)) {
3497 /* In an implementation that includes the Security Extensions
3498 * TTBCR has additional fields PD0 [4] and PD1 [5] for
3499 * Short-descriptor translation table format.
3500 */
3501 value &= TTBCR_PD1 | TTBCR_PD0 | TTBCR_N;
3502 } else {
3503 value &= TTBCR_N;
3504 }
e42c4db3 3505 }
e389be16 3506
b6af0975 3507 /* Update the masks corresponding to the TCR bank being written
11f136ee 3508 * Note that we always calculate mask and base_mask, but
e42c4db3 3509 * they are only used for short-descriptor tables (ie if EAE is 0);
11f136ee
FA
3510 * for long-descriptor tables the TCR fields are used differently
3511 * and the mask and base_mask values are meaningless.
e42c4db3 3512 */
11f136ee
FA
3513 tcr->raw_tcr = value;
3514 tcr->mask = ~(((uint32_t)0xffffffffu) >> maskshift);
3515 tcr->base_mask = ~((uint32_t)0x3fffu >> maskshift);
ecce5c3c
PM
3516}
3517
c4241c7d
PM
3518static void vmsa_ttbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3519 uint64_t value)
d4e6df63 3520{
2fc0cc0e 3521 ARMCPU *cpu = env_archcpu(env);
ab638a32 3522 TCR *tcr = raw_ptr(env, ri);
00c8cb0a 3523
d4e6df63
PM
3524 if (arm_feature(env, ARM_FEATURE_LPAE)) {
3525 /* With LPAE the TTBCR could result in a change of ASID
3526 * via the TTBCR.A1 bit, so do a TLB flush.
3527 */
d10eb08f 3528 tlb_flush(CPU(cpu));
d4e6df63 3529 }
ab638a32
RH
3530 /* Preserve the high half of TCR_EL1, set via TTBCR2. */
3531 value = deposit64(tcr->raw_tcr, 0, 32, value);
c4241c7d 3532 vmsa_ttbcr_raw_write(env, ri, value);
d4e6df63
PM
3533}
3534
ecce5c3c
PM
3535static void vmsa_ttbcr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
3536{
11f136ee
FA
3537 TCR *tcr = raw_ptr(env, ri);
3538
3539 /* Reset both the TCR as well as the masks corresponding to the bank of
3540 * the TCR being reset.
3541 */
3542 tcr->raw_tcr = 0;
3543 tcr->mask = 0;
3544 tcr->base_mask = 0xffffc000u;
ecce5c3c
PM
3545}
3546
cb2e37df
PM
3547static void vmsa_tcr_el1_write(CPUARMState *env, const ARMCPRegInfo *ri,
3548 uint64_t value)
3549{
2fc0cc0e 3550 ARMCPU *cpu = env_archcpu(env);
11f136ee 3551 TCR *tcr = raw_ptr(env, ri);
00c8cb0a 3552
cb2e37df 3553 /* For AArch64 the A1 bit could result in a change of ASID, so TLB flush. */
d10eb08f 3554 tlb_flush(CPU(cpu));
11f136ee 3555 tcr->raw_tcr = value;
cb2e37df
PM
3556}
3557
327ed10f
PM
3558static void vmsa_ttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3559 uint64_t value)
3560{
93f379b0
RH
3561 /* If the ASID changes (with a 64-bit write), we must flush the TLB. */
3562 if (cpreg_field_is_64bit(ri) &&
3563 extract64(raw_read(env, ri) ^ value, 48, 16) != 0) {
2fc0cc0e 3564 ARMCPU *cpu = env_archcpu(env);
d10eb08f 3565 tlb_flush(CPU(cpu));
327ed10f
PM
3566 }
3567 raw_write(env, ri, value);
3568}
3569
ed30da8e
RH
3570static void vmsa_tcr_ttbr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
3571 uint64_t value)
3572{
3573 /* TODO: There are ASID fields in here with HCR_EL2.E2H */
3574 raw_write(env, ri, value);
3575}
3576
b698e9cf
EI
3577static void vttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3578 uint64_t value)
3579{
2fc0cc0e 3580 ARMCPU *cpu = env_archcpu(env);
b698e9cf
EI
3581 CPUState *cs = CPU(cpu);
3582
3583 /* Accesses to VTTBR may change the VMID so we must flush the TLB. */
3584 if (raw_read(env, ri) != value) {
0336cbf8 3585 tlb_flush_by_mmuidx(cs,
8bd5c820
PM
3586 ARMMMUIdxBit_S12NSE1 |
3587 ARMMMUIdxBit_S12NSE0 |
3588 ARMMMUIdxBit_S2NS);
b698e9cf
EI
3589 raw_write(env, ri, value);
3590 }
3591}
3592
8e5d75c9 3593static const ARMCPRegInfo vmsa_pmsa_cp_reginfo[] = {
18032bec 3594 { .name = "DFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
7a0e58fa 3595 .access = PL1_RW, .type = ARM_CP_ALIAS,
4a7e2d73 3596 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dfsr_s),
b061a82b 3597 offsetoflow32(CPUARMState, cp15.dfsr_ns) }, },
18032bec 3598 { .name = "IFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
88ca1c2d
FA
3599 .access = PL1_RW, .resetvalue = 0,
3600 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.ifsr_s),
3601 offsetoflow32(CPUARMState, cp15.ifsr_ns) } },
8e5d75c9
PC
3602 { .name = "DFAR", .cp = 15, .opc1 = 0, .crn = 6, .crm = 0, .opc2 = 0,
3603 .access = PL1_RW, .resetvalue = 0,
3604 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.dfar_s),
3605 offsetof(CPUARMState, cp15.dfar_ns) } },
3606 { .name = "FAR_EL1", .state = ARM_CP_STATE_AA64,
3607 .opc0 = 3, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 0,
3608 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[1]),
3609 .resetvalue = 0, },
3610 REGINFO_SENTINEL
3611};
3612
3613static const ARMCPRegInfo vmsa_cp_reginfo[] = {
6cd8a264
RH
3614 { .name = "ESR_EL1", .state = ARM_CP_STATE_AA64,
3615 .opc0 = 3, .crn = 5, .crm = 2, .opc1 = 0, .opc2 = 0,
3616 .access = PL1_RW,
d81c519c 3617 .fieldoffset = offsetof(CPUARMState, cp15.esr_el[1]), .resetvalue = 0, },
327ed10f 3618 { .name = "TTBR0_EL1", .state = ARM_CP_STATE_BOTH,
7dd8c9af
FA
3619 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 0,
3620 .access = PL1_RW, .writefn = vmsa_ttbr_write, .resetvalue = 0,
3621 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
3622 offsetof(CPUARMState, cp15.ttbr0_ns) } },
327ed10f 3623 { .name = "TTBR1_EL1", .state = ARM_CP_STATE_BOTH,
7dd8c9af
FA
3624 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 1,
3625 .access = PL1_RW, .writefn = vmsa_ttbr_write, .resetvalue = 0,
3626 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
3627 offsetof(CPUARMState, cp15.ttbr1_ns) } },
cb2e37df
PM
3628 { .name = "TCR_EL1", .state = ARM_CP_STATE_AA64,
3629 .opc0 = 3, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
3630 .access = PL1_RW, .writefn = vmsa_tcr_el1_write,
3631 .resetfn = vmsa_ttbcr_reset, .raw_writefn = raw_write,
11f136ee 3632 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[1]) },
cb2e37df 3633 { .name = "TTBCR", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
7a0e58fa 3634 .access = PL1_RW, .type = ARM_CP_ALIAS, .writefn = vmsa_ttbcr_write,
b061a82b 3635 .raw_writefn = vmsa_ttbcr_raw_write,
11f136ee
FA
3636 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tcr_el[3]),
3637 offsetoflow32(CPUARMState, cp15.tcr_el[1])} },
18032bec
PM
3638 REGINFO_SENTINEL
3639};
3640
ab638a32
RH
3641/* Note that unlike TTBCR, writing to TTBCR2 does not require flushing
3642 * qemu tlbs nor adjusting cached masks.
3643 */
3644static const ARMCPRegInfo ttbcr2_reginfo = {
3645 .name = "TTBCR2", .cp = 15, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 3,
3646 .access = PL1_RW, .type = ARM_CP_ALIAS,
3647 .bank_fieldoffsets = { offsetofhigh32(CPUARMState, cp15.tcr_el[3]),
3648 offsetofhigh32(CPUARMState, cp15.tcr_el[1]) },
3649};
3650
c4241c7d
PM
3651static void omap_ticonfig_write(CPUARMState *env, const ARMCPRegInfo *ri,
3652 uint64_t value)
1047b9d7
PM
3653{
3654 env->cp15.c15_ticonfig = value & 0xe7;
3655 /* The OS_TYPE bit in this register changes the reported CPUID! */
3656 env->cp15.c0_cpuid = (value & (1 << 5)) ?
3657 ARM_CPUID_TI915T : ARM_CPUID_TI925T;
1047b9d7
PM
3658}
3659
c4241c7d
PM
3660static void omap_threadid_write(CPUARMState *env, const ARMCPRegInfo *ri,
3661 uint64_t value)
1047b9d7
PM
3662{
3663 env->cp15.c15_threadid = value & 0xffff;
1047b9d7
PM
3664}
3665
c4241c7d
PM
3666static void omap_wfi_write(CPUARMState *env, const ARMCPRegInfo *ri,
3667 uint64_t value)
1047b9d7
PM
3668{
3669 /* Wait-for-interrupt (deprecated) */
2fc0cc0e 3670 cpu_interrupt(env_cpu(env), CPU_INTERRUPT_HALT);
1047b9d7
PM
3671}
3672
c4241c7d
PM
3673static void omap_cachemaint_write(CPUARMState *env, const ARMCPRegInfo *ri,
3674 uint64_t value)
c4804214
PM
3675{
3676 /* On OMAP there are registers indicating the max/min index of dcache lines
3677 * containing a dirty line; cache flush operations have to reset these.
3678 */
3679 env->cp15.c15_i_max = 0x000;
3680 env->cp15.c15_i_min = 0xff0;
c4804214
PM
3681}
3682
18032bec
PM
3683static const ARMCPRegInfo omap_cp_reginfo[] = {
3684 { .name = "DFSR", .cp = 15, .crn = 5, .crm = CP_ANY,
3685 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_OVERRIDE,
d81c519c 3686 .fieldoffset = offsetoflow32(CPUARMState, cp15.esr_el[1]),
6cd8a264 3687 .resetvalue = 0, },
1047b9d7
PM
3688 { .name = "", .cp = 15, .crn = 15, .crm = 0, .opc1 = 0, .opc2 = 0,
3689 .access = PL1_RW, .type = ARM_CP_NOP },
3690 { .name = "TICONFIG", .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0,
3691 .access = PL1_RW,
3692 .fieldoffset = offsetof(CPUARMState, cp15.c15_ticonfig), .resetvalue = 0,
3693 .writefn = omap_ticonfig_write },
3694 { .name = "IMAX", .cp = 15, .crn = 15, .crm = 2, .opc1 = 0, .opc2 = 0,
3695 .access = PL1_RW,
3696 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_max), .resetvalue = 0, },
3697 { .name = "IMIN", .cp = 15, .crn = 15, .crm = 3, .opc1 = 0, .opc2 = 0,
3698 .access = PL1_RW, .resetvalue = 0xff0,
3699 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_min) },
3700 { .name = "THREADID", .cp = 15, .crn = 15, .crm = 4, .opc1 = 0, .opc2 = 0,
3701 .access = PL1_RW,
3702 .fieldoffset = offsetof(CPUARMState, cp15.c15_threadid), .resetvalue = 0,
3703 .writefn = omap_threadid_write },
3704 { .name = "TI925T_STATUS", .cp = 15, .crn = 15,
3705 .crm = 8, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
7a0e58fa 3706 .type = ARM_CP_NO_RAW,
1047b9d7
PM
3707 .readfn = arm_cp_read_zero, .writefn = omap_wfi_write, },
3708 /* TODO: Peripheral port remap register:
3709 * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt controller
3710 * base address at $rn & ~0xfff and map size of 0x200 << ($rn & 0xfff),
3711 * when MMU is off.
3712 */
c4804214 3713 { .name = "OMAP_CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
d4e6df63 3714 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
7a0e58fa 3715 .type = ARM_CP_OVERRIDE | ARM_CP_NO_RAW,
c4804214 3716 .writefn = omap_cachemaint_write },
34f90529
PM
3717 { .name = "C9", .cp = 15, .crn = 9,
3718 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW,
3719 .type = ARM_CP_CONST | ARM_CP_OVERRIDE, .resetvalue = 0 },
1047b9d7
PM
3720 REGINFO_SENTINEL
3721};
3722
c4241c7d
PM
3723static void xscale_cpar_write(CPUARMState *env, const ARMCPRegInfo *ri,
3724 uint64_t value)
1047b9d7 3725{
c0f4af17 3726 env->cp15.c15_cpar = value & 0x3fff;
1047b9d7
PM
3727}
3728
3729static const ARMCPRegInfo xscale_cp_reginfo[] = {
3730 { .name = "XSCALE_CPAR",
3731 .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
3732 .fieldoffset = offsetof(CPUARMState, cp15.c15_cpar), .resetvalue = 0,
3733 .writefn = xscale_cpar_write, },
2771db27
PM
3734 { .name = "XSCALE_AUXCR",
3735 .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 1, .access = PL1_RW,
3736 .fieldoffset = offsetof(CPUARMState, cp15.c1_xscaleauxcr),
3737 .resetvalue = 0, },
3b771579
PM
3738 /* XScale specific cache-lockdown: since we have no cache we NOP these
3739 * and hope the guest does not really rely on cache behaviour.
3740 */
3741 { .name = "XSCALE_LOCK_ICACHE_LINE",
3742 .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 0,
3743 .access = PL1_W, .type = ARM_CP_NOP },
3744 { .name = "XSCALE_UNLOCK_ICACHE",
3745 .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 1,
3746 .access = PL1_W, .type = ARM_CP_NOP },
3747 { .name = "XSCALE_DCACHE_LOCK",
3748 .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 0,
3749 .access = PL1_RW, .type = ARM_CP_NOP },
3750 { .name = "XSCALE_UNLOCK_DCACHE",
3751 .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 1,
3752 .access = PL1_W, .type = ARM_CP_NOP },
1047b9d7
PM
3753 REGINFO_SENTINEL
3754};
3755
3756static const ARMCPRegInfo dummy_c15_cp_reginfo[] = {
3757 /* RAZ/WI the whole crn=15 space, when we don't have a more specific
3758 * implementation of this implementation-defined space.
3759 * Ideally this should eventually disappear in favour of actually
3760 * implementing the correct behaviour for all cores.
3761 */
3762 { .name = "C15_IMPDEF", .cp = 15, .crn = 15,
3763 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
3671cd87 3764 .access = PL1_RW,
7a0e58fa 3765 .type = ARM_CP_CONST | ARM_CP_NO_RAW | ARM_CP_OVERRIDE,
d4e6df63 3766 .resetvalue = 0 },
18032bec
PM
3767 REGINFO_SENTINEL
3768};
3769
c4804214
PM
3770static const ARMCPRegInfo cache_dirty_status_cp_reginfo[] = {
3771 /* Cache status: RAZ because we have no cache so it's always clean */
3772 { .name = "CDSR", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 6,
7a0e58fa 3773 .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 3774 .resetvalue = 0 },
c4804214
PM
3775 REGINFO_SENTINEL
3776};
3777
3778static const ARMCPRegInfo cache_block_ops_cp_reginfo[] = {
3779 /* We never have a a block transfer operation in progress */
3780 { .name = "BXSR", .cp = 15, .crn = 7, .crm = 12, .opc1 = 0, .opc2 = 4,
7a0e58fa 3781 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 3782 .resetvalue = 0 },
30b05bba
PM
3783 /* The cache ops themselves: these all NOP for QEMU */
3784 { .name = "IICR", .cp = 15, .crm = 5, .opc1 = 0,
3785 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
3786 { .name = "IDCR", .cp = 15, .crm = 6, .opc1 = 0,
3787 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
3788 { .name = "CDCR", .cp = 15, .crm = 12, .opc1 = 0,
3789 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
3790 { .name = "PIR", .cp = 15, .crm = 12, .opc1 = 1,
3791 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
3792 { .name = "PDR", .cp = 15, .crm = 12, .opc1 = 2,
3793 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
3794 { .name = "CIDCR", .cp = 15, .crm = 14, .opc1 = 0,
3795 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
c4804214
PM
3796 REGINFO_SENTINEL
3797};
3798
3799static const ARMCPRegInfo cache_test_clean_cp_reginfo[] = {
3800 /* The cache test-and-clean instructions always return (1 << 30)
3801 * to indicate that there are no dirty cache lines.
3802 */
3803 { .name = "TC_DCACHE", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 3,
7a0e58fa 3804 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 3805 .resetvalue = (1 << 30) },
c4804214 3806 { .name = "TCI_DCACHE", .cp = 15, .crn = 7, .crm = 14, .opc1 = 0, .opc2 = 3,
7a0e58fa 3807 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 3808 .resetvalue = (1 << 30) },
c4804214
PM
3809 REGINFO_SENTINEL
3810};
3811
34f90529
PM
3812static const ARMCPRegInfo strongarm_cp_reginfo[] = {
3813 /* Ignore ReadBuffer accesses */
3814 { .name = "C9_READBUFFER", .cp = 15, .crn = 9,
3815 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
d4e6df63 3816 .access = PL1_RW, .resetvalue = 0,
7a0e58fa 3817 .type = ARM_CP_CONST | ARM_CP_OVERRIDE | ARM_CP_NO_RAW },
34f90529
PM
3818 REGINFO_SENTINEL
3819};
3820
731de9e6
EI
3821static uint64_t midr_read(CPUARMState *env, const ARMCPRegInfo *ri)
3822{
2fc0cc0e 3823 ARMCPU *cpu = env_archcpu(env);
731de9e6
EI
3824 unsigned int cur_el = arm_current_el(env);
3825 bool secure = arm_is_secure(env);
3826
3827 if (arm_feature(&cpu->env, ARM_FEATURE_EL2) && !secure && cur_el == 1) {
3828 return env->cp15.vpidr_el2;
3829 }
3830 return raw_read(env, ri);
3831}
3832
06a7e647 3833static uint64_t mpidr_read_val(CPUARMState *env)
81bdde9d 3834{
2fc0cc0e 3835 ARMCPU *cpu = env_archcpu(env);
eb5e1d3c
PF
3836 uint64_t mpidr = cpu->mp_affinity;
3837
81bdde9d 3838 if (arm_feature(env, ARM_FEATURE_V7MP)) {
78dbbbe4 3839 mpidr |= (1U << 31);
81bdde9d
PM
3840 /* Cores which are uniprocessor (non-coherent)
3841 * but still implement the MP extensions set
a8e81b31 3842 * bit 30. (For instance, Cortex-R5).
81bdde9d 3843 */
a8e81b31
PC
3844 if (cpu->mp_is_up) {
3845 mpidr |= (1u << 30);
3846 }
81bdde9d 3847 }
c4241c7d 3848 return mpidr;
81bdde9d
PM
3849}
3850
06a7e647
EI
3851static uint64_t mpidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
3852{
f0d574d6
EI
3853 unsigned int cur_el = arm_current_el(env);
3854 bool secure = arm_is_secure(env);
3855
3856 if (arm_feature(env, ARM_FEATURE_EL2) && !secure && cur_el == 1) {
3857 return env->cp15.vmpidr_el2;
3858 }
06a7e647
EI
3859 return mpidr_read_val(env);
3860}
3861
7ac681cf 3862static const ARMCPRegInfo lpae_cp_reginfo[] = {
a903c449 3863 /* NOP AMAIR0/1 */
b0fe2427
PM
3864 { .name = "AMAIR0", .state = ARM_CP_STATE_BOTH,
3865 .opc0 = 3, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 0,
a903c449 3866 .access = PL1_RW, .type = ARM_CP_CONST,
7ac681cf 3867 .resetvalue = 0 },
b0fe2427 3868 /* AMAIR1 is mapped to AMAIR_EL1[63:32] */
7ac681cf 3869 { .name = "AMAIR1", .cp = 15, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 1,
a903c449 3870 .access = PL1_RW, .type = ARM_CP_CONST,
7ac681cf 3871 .resetvalue = 0 },
891a2fe7 3872 { .name = "PAR", .cp = 15, .crm = 7, .opc1 = 0,
01c097f7
FA
3873 .access = PL1_RW, .type = ARM_CP_64BIT, .resetvalue = 0,
3874 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.par_s),
3875 offsetof(CPUARMState, cp15.par_ns)} },
891a2fe7 3876 { .name = "TTBR0", .cp = 15, .crm = 2, .opc1 = 0,
7a0e58fa 3877 .access = PL1_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
7dd8c9af
FA
3878 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
3879 offsetof(CPUARMState, cp15.ttbr0_ns) },
b061a82b 3880 .writefn = vmsa_ttbr_write, },
891a2fe7 3881 { .name = "TTBR1", .cp = 15, .crm = 2, .opc1 = 1,
7a0e58fa 3882 .access = PL1_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
7dd8c9af
FA
3883 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
3884 offsetof(CPUARMState, cp15.ttbr1_ns) },
b061a82b 3885 .writefn = vmsa_ttbr_write, },
7ac681cf
PM
3886 REGINFO_SENTINEL
3887};
3888
c4241c7d 3889static uint64_t aa64_fpcr_read(CPUARMState *env, const ARMCPRegInfo *ri)
b0d2b7d0 3890{
c4241c7d 3891 return vfp_get_fpcr(env);
b0d2b7d0
PM
3892}
3893
c4241c7d
PM
3894static void aa64_fpcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3895 uint64_t value)
b0d2b7d0
PM
3896{
3897 vfp_set_fpcr(env, value);
b0d2b7d0
PM
3898}
3899
c4241c7d 3900static uint64_t aa64_fpsr_read(CPUARMState *env, const ARMCPRegInfo *ri)
b0d2b7d0 3901{
c4241c7d 3902 return vfp_get_fpsr(env);
b0d2b7d0
PM
3903}
3904
c4241c7d
PM
3905static void aa64_fpsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3906 uint64_t value)
b0d2b7d0
PM
3907{
3908 vfp_set_fpsr(env, value);
b0d2b7d0
PM
3909}
3910
3f208fd7
PM
3911static CPAccessResult aa64_daif_access(CPUARMState *env, const ARMCPRegInfo *ri,
3912 bool isread)
c2b820fe 3913{
137feaa9 3914 if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_UMA)) {
c2b820fe
PM
3915 return CP_ACCESS_TRAP;
3916 }
3917 return CP_ACCESS_OK;
3918}
3919
3920static void aa64_daif_write(CPUARMState *env, const ARMCPRegInfo *ri,
3921 uint64_t value)
3922{
3923 env->daif = value & PSTATE_DAIF;
3924}
3925
8af35c37 3926static CPAccessResult aa64_cacheop_access(CPUARMState *env,
3f208fd7
PM
3927 const ARMCPRegInfo *ri,
3928 bool isread)
8af35c37
PM
3929{
3930 /* Cache invalidate/clean: NOP, but EL0 must UNDEF unless
3931 * SCTLR_EL1.UCI is set.
3932 */
137feaa9 3933 if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_UCI)) {
8af35c37
PM
3934 return CP_ACCESS_TRAP;
3935 }
3936 return CP_ACCESS_OK;
3937}
3938
dbb1fb27
AB
3939/* See: D4.7.2 TLB maintenance requirements and the TLB maintenance instructions
3940 * Page D4-1736 (DDI0487A.b)
3941 */
3942
b7e0730d
RH
3943static int vae1_tlbmask(CPUARMState *env)
3944{
3945 if (arm_is_secure_below_el3(env)) {
3946 return ARMMMUIdxBit_S1SE1 | ARMMMUIdxBit_S1SE0;
3947 } else {
3948 return ARMMMUIdxBit_S12NSE1 | ARMMMUIdxBit_S12NSE0;
3949 }
3950}
3951
fd3ed969
PM
3952static void tlbi_aa64_vmalle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
3953 uint64_t value)
168aa23b 3954{
29a0af61 3955 CPUState *cs = env_cpu(env);
b7e0730d 3956 int mask = vae1_tlbmask(env);
dbb1fb27 3957
b7e0730d 3958 tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
168aa23b
PM
3959}
3960
b4ab8ce9
PM
3961static void tlbi_aa64_vmalle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
3962 uint64_t value)
3963{
29a0af61 3964 CPUState *cs = env_cpu(env);
b7e0730d 3965 int mask = vae1_tlbmask(env);
b4ab8ce9
PM
3966
3967 if (tlb_force_broadcast(env)) {
09a86dfa 3968 tlbi_aa64_vmalle1is_write(env, NULL, value);
b4ab8ce9
PM
3969 return;
3970 }
3971
b7e0730d 3972 tlb_flush_by_mmuidx(cs, mask);
b4ab8ce9
PM
3973}
3974
90c19cdf 3975static int alle1_tlbmask(CPUARMState *env)
168aa23b 3976{
90c19cdf
RH
3977 /*
3978 * Note that the 'ALL' scope must invalidate both stage 1 and
fd3ed969
PM
3979 * stage 2 translations, whereas most other scopes only invalidate
3980 * stage 1 translations.
3981 */
fd3ed969 3982 if (arm_is_secure_below_el3(env)) {
90c19cdf
RH
3983 return ARMMMUIdxBit_S1SE1 | ARMMMUIdxBit_S1SE0;
3984 } else if (arm_feature(env, ARM_FEATURE_EL2)) {
3985 return ARMMMUIdxBit_S12NSE1 | ARMMMUIdxBit_S12NSE0 | ARMMMUIdxBit_S2NS;
fd3ed969 3986 } else {
90c19cdf 3987 return ARMMMUIdxBit_S12NSE1 | ARMMMUIdxBit_S12NSE0;
fd3ed969 3988 }
168aa23b
PM
3989}
3990
90c19cdf
RH
3991static void tlbi_aa64_alle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
3992 uint64_t value)
3993{
3994 CPUState *cs = env_cpu(env);
3995 int mask = alle1_tlbmask(env);
3996
3997 tlb_flush_by_mmuidx(cs, mask);
3998}
3999
fd3ed969 4000static void tlbi_aa64_alle2_write(CPUARMState *env, const ARMCPRegInfo *ri,
fa439fc5
PM
4001 uint64_t value)
4002{
2fc0cc0e 4003 ARMCPU *cpu = env_archcpu(env);
fd3ed969
PM
4004 CPUState *cs = CPU(cpu);
4005
8bd5c820 4006 tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_S1E2);
fd3ed969
PM
4007}
4008
43efaa33
PM
4009static void tlbi_aa64_alle3_write(CPUARMState *env, const ARMCPRegInfo *ri,
4010 uint64_t value)
4011{
2fc0cc0e 4012 ARMCPU *cpu = env_archcpu(env);
43efaa33
PM
4013 CPUState *cs = CPU(cpu);
4014
8bd5c820 4015 tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_S1E3);
43efaa33
PM
4016}
4017
fd3ed969
PM
4018static void tlbi_aa64_alle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4019 uint64_t value)
4020{
29a0af61 4021 CPUState *cs = env_cpu(env);
90c19cdf
RH
4022 int mask = alle1_tlbmask(env);
4023
4024 tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
fa439fc5
PM
4025}
4026
2bfb9d75
PM
4027static void tlbi_aa64_alle2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4028 uint64_t value)
4029{
29a0af61 4030 CPUState *cs = env_cpu(env);
2bfb9d75 4031
8bd5c820 4032 tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_S1E2);
2bfb9d75
PM
4033}
4034
43efaa33
PM
4035static void tlbi_aa64_alle3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4036 uint64_t value)
4037{
29a0af61 4038 CPUState *cs = env_cpu(env);
43efaa33 4039
8bd5c820 4040 tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_S1E3);
43efaa33
PM
4041}
4042
fd3ed969
PM
4043static void tlbi_aa64_vae2_write(CPUARMState *env, const ARMCPRegInfo *ri,
4044 uint64_t value)
fa439fc5 4045{
fd3ed969
PM
4046 /* Invalidate by VA, EL2
4047 * Currently handles both VAE2 and VALE2, since we don't support
4048 * flush-last-level-only.
4049 */
2fc0cc0e 4050 ARMCPU *cpu = env_archcpu(env);
fd3ed969
PM
4051 CPUState *cs = CPU(cpu);
4052 uint64_t pageaddr = sextract64(value << 12, 0, 56);
4053
8bd5c820 4054 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_S1E2);
fd3ed969
PM
4055}
4056
43efaa33
PM
4057static void tlbi_aa64_vae3_write(CPUARMState *env, const ARMCPRegInfo *ri,
4058 uint64_t value)
4059{
4060 /* Invalidate by VA, EL3
4061 * Currently handles both VAE3 and VALE3, since we don't support
4062 * flush-last-level-only.
4063 */
2fc0cc0e 4064 ARMCPU *cpu = env_archcpu(env);
43efaa33
PM
4065 CPUState *cs = CPU(cpu);
4066 uint64_t pageaddr = sextract64(value << 12, 0, 56);
4067
8bd5c820 4068 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_S1E3);
43efaa33
PM
4069}
4070
fd3ed969
PM
4071static void tlbi_aa64_vae1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4072 uint64_t value)
4073{
90c19cdf
RH
4074 CPUState *cs = env_cpu(env);
4075 int mask = vae1_tlbmask(env);
fa439fc5
PM
4076 uint64_t pageaddr = sextract64(value << 12, 0, 56);
4077
90c19cdf 4078 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr, mask);
fa439fc5
PM
4079}
4080
b4ab8ce9
PM
4081static void tlbi_aa64_vae1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4082 uint64_t value)
4083{
4084 /* Invalidate by VA, EL1&0 (AArch64 version).
4085 * Currently handles all of VAE1, VAAE1, VAALE1 and VALE1,
4086 * since we don't support flush-for-specific-ASID-only or
4087 * flush-last-level-only.
4088 */
90c19cdf
RH
4089 CPUState *cs = env_cpu(env);
4090 int mask = vae1_tlbmask(env);
b4ab8ce9
PM
4091 uint64_t pageaddr = sextract64(value << 12, 0, 56);
4092
4093 if (tlb_force_broadcast(env)) {
4094 tlbi_aa64_vae1is_write(env, NULL, value);
4095 return;
4096 }
4097
90c19cdf 4098 tlb_flush_page_by_mmuidx(cs, pageaddr, mask);
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,
8bd5c820 4108 ARMMMUIdxBit_S1E2);
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,
8bd5c820 4118 ARMMMUIdxBit_S1E3);
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
8bd5c820 4140 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_S2NS);
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,
8bd5c820 4156 ARMMMUIdxBit_S2NS);
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 */
8710static inline uint32_t regime_el(CPUARMState *env, ARMMMUIdx mmu_idx)
8711{
8712 switch (mmu_idx) {
8713 case ARMMMUIdx_S2NS:
8714 case ARMMMUIdx_S1E2:
8715 return 2;
8716 case ARMMMUIdx_S1E3:
8717 return 3;
8718 case ARMMMUIdx_S1SE0:
8719 return arm_el_is_aa64(env, 3) ? 1 : 3;
8720 case ARMMMUIdx_S1SE1:
8721 case ARMMMUIdx_S1NSE0:
8722 case ARMMMUIdx_S1NSE1:
62593718
PM
8723 case ARMMMUIdx_MPrivNegPri:
8724 case ARMMMUIdx_MUserNegPri:
e7b921c2
PM
8725 case ARMMMUIdx_MPriv:
8726 case ARMMMUIdx_MUser:
62593718
PM
8727 case ARMMMUIdx_MSPrivNegPri:
8728 case ARMMMUIdx_MSUserNegPri:
66787c78 8729 case ARMMMUIdx_MSPriv:
66787c78 8730 case ARMMMUIdx_MSUser:
0480f69a
PM
8731 return 1;
8732 default:
8733 g_assert_not_reached();
8734 }
8735}
8736
c47eaf9f
PM
8737#ifndef CONFIG_USER_ONLY
8738
0480f69a
PM
8739/* Return the SCTLR value which controls this address translation regime */
8740static inline uint32_t regime_sctlr(CPUARMState *env, ARMMMUIdx mmu_idx)
8741{
8742 return env->cp15.sctlr_el[regime_el(env, mmu_idx)];
8743}
8744
8745/* Return true if the specified stage of address translation is disabled */
8746static inline bool regime_translation_disabled(CPUARMState *env,
8747 ARMMMUIdx mmu_idx)
8748{
29c483a5 8749 if (arm_feature(env, ARM_FEATURE_M)) {
ecf5e8ea 8750 switch (env->v7m.mpu_ctrl[regime_is_secure(env, mmu_idx)] &
3bef7012
PM
8751 (R_V7M_MPU_CTRL_ENABLE_MASK | R_V7M_MPU_CTRL_HFNMIENA_MASK)) {
8752 case R_V7M_MPU_CTRL_ENABLE_MASK:
8753 /* Enabled, but not for HardFault and NMI */
62593718 8754 return mmu_idx & ARM_MMU_IDX_M_NEGPRI;
3bef7012
PM
8755 case R_V7M_MPU_CTRL_ENABLE_MASK | R_V7M_MPU_CTRL_HFNMIENA_MASK:
8756 /* Enabled for all cases */
8757 return false;
8758 case 0:
8759 default:
8760 /* HFNMIENA set and ENABLE clear is UNPREDICTABLE, but
8761 * we warned about that in armv7m_nvic.c when the guest set it.
8762 */
8763 return true;
8764 }
29c483a5
MD
8765 }
8766
0480f69a 8767 if (mmu_idx == ARMMMUIdx_S2NS) {
9d1bab33
PM
8768 /* HCR.DC means HCR.VM behaves as 1 */
8769 return (env->cp15.hcr_el2 & (HCR_DC | HCR_VM)) == 0;
0480f69a 8770 }
3d0e3080
PM
8771
8772 if (env->cp15.hcr_el2 & HCR_TGE) {
8773 /* TGE means that NS EL0/1 act as if SCTLR_EL1.M is zero */
8774 if (!regime_is_secure(env, mmu_idx) && regime_el(env, mmu_idx) == 1) {
8775 return true;
8776 }
8777 }
8778
9d1bab33
PM
8779 if ((env->cp15.hcr_el2 & HCR_DC) &&
8780 (mmu_idx == ARMMMUIdx_S1NSE0 || mmu_idx == ARMMMUIdx_S1NSE1)) {
8781 /* HCR.DC means SCTLR_EL1.M behaves as 0 */
8782 return true;
8783 }
8784
0480f69a
PM
8785 return (regime_sctlr(env, mmu_idx) & SCTLR_M) == 0;
8786}
8787
73462ddd
PC
8788static inline bool regime_translation_big_endian(CPUARMState *env,
8789 ARMMMUIdx mmu_idx)
8790{
8791 return (regime_sctlr(env, mmu_idx) & SCTLR_EE) != 0;
8792}
8793
c47eaf9f
PM
8794/* Return the TTBR associated with this translation regime */
8795static inline uint64_t regime_ttbr(CPUARMState *env, ARMMMUIdx mmu_idx,
8796 int ttbrn)
8797{
8798 if (mmu_idx == ARMMMUIdx_S2NS) {
8799 return env->cp15.vttbr_el2;
8800 }
8801 if (ttbrn == 0) {
8802 return env->cp15.ttbr0_el[regime_el(env, mmu_idx)];
8803 } else {
8804 return env->cp15.ttbr1_el[regime_el(env, mmu_idx)];
8805 }
8806}
8807
8808#endif /* !CONFIG_USER_ONLY */
8809
0480f69a
PM
8810/* Return the TCR controlling this translation regime */
8811static inline TCR *regime_tcr(CPUARMState *env, ARMMMUIdx mmu_idx)
8812{
8813 if (mmu_idx == ARMMMUIdx_S2NS) {
68e9c2fe 8814 return &env->cp15.vtcr_el2;
0480f69a
PM
8815 }
8816 return &env->cp15.tcr_el[regime_el(env, mmu_idx)];
8817}
8818
8bd5c820
PM
8819/* Convert a possible stage1+2 MMU index into the appropriate
8820 * stage 1 MMU index
8821 */
8822static inline ARMMMUIdx stage_1_mmu_idx(ARMMMUIdx mmu_idx)
8823{
8824 if (mmu_idx == ARMMMUIdx_S12NSE0 || mmu_idx == ARMMMUIdx_S12NSE1) {
8825 mmu_idx += (ARMMMUIdx_S1NSE0 - ARMMMUIdx_S12NSE0);
8826 }
8827 return mmu_idx;
8828}
8829
0480f69a
PM
8830/* Return true if the translation regime is using LPAE format page tables */
8831static inline bool regime_using_lpae_format(CPUARMState *env,
8832 ARMMMUIdx mmu_idx)
8833{
8834 int el = regime_el(env, mmu_idx);
8835 if (el == 2 || arm_el_is_aa64(env, el)) {
8836 return true;
8837 }
8838 if (arm_feature(env, ARM_FEATURE_LPAE)
8839 && (regime_tcr(env, mmu_idx)->raw_tcr & TTBCR_EAE)) {
8840 return true;
8841 }
8842 return false;
8843}
8844
deb2db99
AR
8845/* Returns true if the stage 1 translation regime is using LPAE format page
8846 * tables. Used when raising alignment exceptions, whose FSR changes depending
8847 * on whether the long or short descriptor format is in use. */
8848bool arm_s1_regime_using_lpae_format(CPUARMState *env, ARMMMUIdx mmu_idx)
30901475 8849{
8bd5c820 8850 mmu_idx = stage_1_mmu_idx(mmu_idx);
deb2db99 8851
30901475
AB
8852 return regime_using_lpae_format(env, mmu_idx);
8853}
8854
c47eaf9f 8855#ifndef CONFIG_USER_ONLY
0480f69a
PM
8856static inline bool regime_is_user(CPUARMState *env, ARMMMUIdx mmu_idx)
8857{
8858 switch (mmu_idx) {
8859 case ARMMMUIdx_S1SE0:
8860 case ARMMMUIdx_S1NSE0:
e7b921c2 8861 case ARMMMUIdx_MUser:
871bec7c 8862 case ARMMMUIdx_MSUser:
62593718
PM
8863 case ARMMMUIdx_MUserNegPri:
8864 case ARMMMUIdx_MSUserNegPri:
0480f69a
PM
8865 return true;
8866 default:
8867 return false;
8868 case ARMMMUIdx_S12NSE0:
8869 case ARMMMUIdx_S12NSE1:
8870 g_assert_not_reached();
8871 }
8872}
8873
0fbf5238
AJ
8874/* Translate section/page access permissions to page
8875 * R/W protection flags
d76951b6
AJ
8876 *
8877 * @env: CPUARMState
8878 * @mmu_idx: MMU index indicating required translation regime
8879 * @ap: The 3-bit access permissions (AP[2:0])
8880 * @domain_prot: The 2-bit domain access permissions
0fbf5238
AJ
8881 */
8882static inline int ap_to_rw_prot(CPUARMState *env, ARMMMUIdx mmu_idx,
8883 int ap, int domain_prot)
8884{
554b0b09
PM
8885 bool is_user = regime_is_user(env, mmu_idx);
8886
8887 if (domain_prot == 3) {
8888 return PAGE_READ | PAGE_WRITE;
8889 }
8890
554b0b09
PM
8891 switch (ap) {
8892 case 0:
8893 if (arm_feature(env, ARM_FEATURE_V7)) {
8894 return 0;
8895 }
554b0b09
PM
8896 switch (regime_sctlr(env, mmu_idx) & (SCTLR_S | SCTLR_R)) {
8897 case SCTLR_S:
8898 return is_user ? 0 : PAGE_READ;
8899 case SCTLR_R:
8900 return PAGE_READ;
8901 default:
8902 return 0;
8903 }
8904 case 1:
8905 return is_user ? 0 : PAGE_READ | PAGE_WRITE;
8906 case 2:
87c3d486 8907 if (is_user) {
0fbf5238 8908 return PAGE_READ;
87c3d486 8909 } else {
554b0b09 8910 return PAGE_READ | PAGE_WRITE;
87c3d486 8911 }
554b0b09
PM
8912 case 3:
8913 return PAGE_READ | PAGE_WRITE;
8914 case 4: /* Reserved. */
8915 return 0;
8916 case 5:
0fbf5238 8917 return is_user ? 0 : PAGE_READ;
554b0b09 8918 case 6:
0fbf5238 8919 return PAGE_READ;
554b0b09 8920 case 7:
87c3d486 8921 if (!arm_feature(env, ARM_FEATURE_V6K)) {
554b0b09 8922 return 0;
87c3d486 8923 }
0fbf5238 8924 return PAGE_READ;
554b0b09 8925 default:
0fbf5238 8926 g_assert_not_reached();
554b0b09 8927 }
b5ff1b31
FB
8928}
8929
d76951b6
AJ
8930/* Translate section/page access permissions to page
8931 * R/W protection flags.
8932 *
d76951b6 8933 * @ap: The 2-bit simple AP (AP[2:1])
d8e052b3 8934 * @is_user: TRUE if accessing from PL0
d76951b6 8935 */
d8e052b3 8936static inline int simple_ap_to_rw_prot_is_user(int ap, bool is_user)
d76951b6 8937{
d76951b6
AJ
8938 switch (ap) {
8939 case 0:
8940 return is_user ? 0 : PAGE_READ | PAGE_WRITE;
8941 case 1:
8942 return PAGE_READ | PAGE_WRITE;
8943 case 2:
8944 return is_user ? 0 : PAGE_READ;
8945 case 3:
8946 return PAGE_READ;
8947 default:
8948 g_assert_not_reached();
8949 }
8950}
8951
d8e052b3
AJ
8952static inline int
8953simple_ap_to_rw_prot(CPUARMState *env, ARMMMUIdx mmu_idx, int ap)
8954{
8955 return simple_ap_to_rw_prot_is_user(ap, regime_is_user(env, mmu_idx));
8956}
8957
6ab1a5ee
EI
8958/* Translate S2 section/page access permissions to protection flags
8959 *
8960 * @env: CPUARMState
8961 * @s2ap: The 2-bit stage2 access permissions (S2AP)
8962 * @xn: XN (execute-never) bit
8963 */
8964static int get_S2prot(CPUARMState *env, int s2ap, int xn)
8965{
8966 int prot = 0;
8967
8968 if (s2ap & 1) {
8969 prot |= PAGE_READ;
8970 }
8971 if (s2ap & 2) {
8972 prot |= PAGE_WRITE;
8973 }
8974 if (!xn) {
dfda6837
SS
8975 if (arm_el_is_aa64(env, 2) || prot & PAGE_READ) {
8976 prot |= PAGE_EXEC;
8977 }
6ab1a5ee
EI
8978 }
8979 return prot;
8980}
8981
d8e052b3
AJ
8982/* Translate section/page access permissions to protection flags
8983 *
8984 * @env: CPUARMState
8985 * @mmu_idx: MMU index indicating required translation regime
8986 * @is_aa64: TRUE if AArch64
8987 * @ap: The 2-bit simple AP (AP[2:1])
8988 * @ns: NS (non-secure) bit
8989 * @xn: XN (execute-never) bit
8990 * @pxn: PXN (privileged execute-never) bit
8991 */
8992static int get_S1prot(CPUARMState *env, ARMMMUIdx mmu_idx, bool is_aa64,
8993 int ap, int ns, int xn, int pxn)
8994{
8995 bool is_user = regime_is_user(env, mmu_idx);
8996 int prot_rw, user_rw;
8997 bool have_wxn;
8998 int wxn = 0;
8999
9000 assert(mmu_idx != ARMMMUIdx_S2NS);
9001
9002 user_rw = simple_ap_to_rw_prot_is_user(ap, true);
9003 if (is_user) {
9004 prot_rw = user_rw;
9005 } else {
9006 prot_rw = simple_ap_to_rw_prot_is_user(ap, false);
9007 }
9008
9009 if (ns && arm_is_secure(env) && (env->cp15.scr_el3 & SCR_SIF)) {
9010 return prot_rw;
9011 }
9012
9013 /* TODO have_wxn should be replaced with
9014 * ARM_FEATURE_V8 || (ARM_FEATURE_V7 && ARM_FEATURE_EL2)
9015 * when ARM_FEATURE_EL2 starts getting set. For now we assume all LPAE
9016 * compatible processors have EL2, which is required for [U]WXN.
9017 */
9018 have_wxn = arm_feature(env, ARM_FEATURE_LPAE);
9019
9020 if (have_wxn) {
9021 wxn = regime_sctlr(env, mmu_idx) & SCTLR_WXN;
9022 }
9023
9024 if (is_aa64) {
9025 switch (regime_el(env, mmu_idx)) {
9026 case 1:
9027 if (!is_user) {
9028 xn = pxn || (user_rw & PAGE_WRITE);
9029 }
9030 break;
9031 case 2:
9032 case 3:
9033 break;
9034 }
9035 } else if (arm_feature(env, ARM_FEATURE_V7)) {
9036 switch (regime_el(env, mmu_idx)) {
9037 case 1:
9038 case 3:
9039 if (is_user) {
9040 xn = xn || !(user_rw & PAGE_READ);
9041 } else {
9042 int uwxn = 0;
9043 if (have_wxn) {
9044 uwxn = regime_sctlr(env, mmu_idx) & SCTLR_UWXN;
9045 }
9046 xn = xn || !(prot_rw & PAGE_READ) || pxn ||
9047 (uwxn && (user_rw & PAGE_WRITE));
9048 }
9049 break;
9050 case 2:
9051 break;
9052 }
9053 } else {
9054 xn = wxn = 0;
9055 }
9056
9057 if (xn || (wxn && (prot_rw & PAGE_WRITE))) {
9058 return prot_rw;
9059 }
9060 return prot_rw | PAGE_EXEC;
9061}
9062
0480f69a
PM
9063static bool get_level1_table_address(CPUARMState *env, ARMMMUIdx mmu_idx,
9064 uint32_t *table, uint32_t address)
b2fa1797 9065{
0480f69a 9066 /* Note that we can only get here for an AArch32 PL0/PL1 lookup */
0480f69a 9067 TCR *tcr = regime_tcr(env, mmu_idx);
11f136ee 9068
11f136ee
FA
9069 if (address & tcr->mask) {
9070 if (tcr->raw_tcr & TTBCR_PD1) {
e389be16
FA
9071 /* Translation table walk disabled for TTBR1 */
9072 return false;
9073 }
aef878be 9074 *table = regime_ttbr(env, mmu_idx, 1) & 0xffffc000;
e389be16 9075 } else {
11f136ee 9076 if (tcr->raw_tcr & TTBCR_PD0) {
e389be16
FA
9077 /* Translation table walk disabled for TTBR0 */
9078 return false;
9079 }
aef878be 9080 *table = regime_ttbr(env, mmu_idx, 0) & tcr->base_mask;
e389be16
FA
9081 }
9082 *table |= (address >> 18) & 0x3ffc;
9083 return true;
b2fa1797
PB
9084}
9085
37785977
EI
9086/* Translate a S1 pagetable walk through S2 if needed. */
9087static hwaddr S1_ptw_translate(CPUARMState *env, ARMMMUIdx mmu_idx,
9088 hwaddr addr, MemTxAttrs txattrs,
37785977
EI
9089 ARMMMUFaultInfo *fi)
9090{
9091 if ((mmu_idx == ARMMMUIdx_S1NSE0 || mmu_idx == ARMMMUIdx_S1NSE1) &&
9092 !regime_translation_disabled(env, ARMMMUIdx_S2NS)) {
9093 target_ulong s2size;
9094 hwaddr s2pa;
9095 int s2prot;
9096 int ret;
eadb2feb
PM
9097 ARMCacheAttrs cacheattrs = {};
9098 ARMCacheAttrs *pcacheattrs = NULL;
9099
9100 if (env->cp15.hcr_el2 & HCR_PTW) {
9101 /*
9102 * PTW means we must fault if this S1 walk touches S2 Device
9103 * memory; otherwise we don't care about the attributes and can
9104 * save the S2 translation the effort of computing them.
9105 */
9106 pcacheattrs = &cacheattrs;
9107 }
37785977
EI
9108
9109 ret = get_phys_addr_lpae(env, addr, 0, ARMMMUIdx_S2NS, &s2pa,
eadb2feb 9110 &txattrs, &s2prot, &s2size, fi, pcacheattrs);
37785977 9111 if (ret) {
3b39d734 9112 assert(fi->type != ARMFault_None);
37785977
EI
9113 fi->s2addr = addr;
9114 fi->stage2 = true;
9115 fi->s1ptw = true;
9116 return ~0;
9117 }
eadb2feb
PM
9118 if (pcacheattrs && (pcacheattrs->attrs & 0xf0) == 0) {
9119 /* Access was to Device memory: generate Permission fault */
9120 fi->type = ARMFault_Permission;
9121 fi->s2addr = addr;
9122 fi->stage2 = true;
9123 fi->s1ptw = true;
9124 return ~0;
9125 }
37785977
EI
9126 addr = s2pa;
9127 }
9128 return addr;
9129}
9130
14577270 9131/* All loads done in the course of a page table walk go through here. */
a614e698 9132static uint32_t arm_ldl_ptw(CPUState *cs, hwaddr addr, bool is_secure,
3795a6de 9133 ARMMMUIdx mmu_idx, ARMMMUFaultInfo *fi)
ebca90e4 9134{
a614e698
EI
9135 ARMCPU *cpu = ARM_CPU(cs);
9136 CPUARMState *env = &cpu->env;
ebca90e4 9137 MemTxAttrs attrs = {};
3b39d734 9138 MemTxResult result = MEMTX_OK;
5ce4ff65 9139 AddressSpace *as;
3b39d734 9140 uint32_t data;
ebca90e4
PM
9141
9142 attrs.secure = is_secure;
5ce4ff65 9143 as = arm_addressspace(cs, attrs);
3795a6de 9144 addr = S1_ptw_translate(env, mmu_idx, addr, attrs, fi);
a614e698
EI
9145 if (fi->s1ptw) {
9146 return 0;
9147 }
73462ddd 9148 if (regime_translation_big_endian(env, mmu_idx)) {
3b39d734 9149 data = address_space_ldl_be(as, addr, attrs, &result);
73462ddd 9150 } else {
3b39d734 9151 data = address_space_ldl_le(as, addr, attrs, &result);
73462ddd 9152 }
3b39d734
PM
9153 if (result == MEMTX_OK) {
9154 return data;
9155 }
9156 fi->type = ARMFault_SyncExternalOnWalk;
9157 fi->ea = arm_extabort_type(result);
9158 return 0;
ebca90e4
PM
9159}
9160
37785977 9161static uint64_t arm_ldq_ptw(CPUState *cs, hwaddr addr, bool is_secure,
3795a6de 9162 ARMMMUIdx mmu_idx, ARMMMUFaultInfo *fi)
ebca90e4 9163{
37785977
EI
9164 ARMCPU *cpu = ARM_CPU(cs);
9165 CPUARMState *env = &cpu->env;
ebca90e4 9166 MemTxAttrs attrs = {};
3b39d734 9167 MemTxResult result = MEMTX_OK;
5ce4ff65 9168 AddressSpace *as;
9aea1ea3 9169 uint64_t data;
ebca90e4
PM
9170
9171 attrs.secure = is_secure;
5ce4ff65 9172 as = arm_addressspace(cs, attrs);
3795a6de 9173 addr = S1_ptw_translate(env, mmu_idx, addr, attrs, fi);
37785977
EI
9174 if (fi->s1ptw) {
9175 return 0;
9176 }
73462ddd 9177 if (regime_translation_big_endian(env, mmu_idx)) {
3b39d734 9178 data = address_space_ldq_be(as, addr, attrs, &result);
73462ddd 9179 } else {
3b39d734
PM
9180 data = address_space_ldq_le(as, addr, attrs, &result);
9181 }
9182 if (result == MEMTX_OK) {
9183 return data;
73462ddd 9184 }
3b39d734
PM
9185 fi->type = ARMFault_SyncExternalOnWalk;
9186 fi->ea = arm_extabort_type(result);
9187 return 0;
ebca90e4
PM
9188}
9189
b7cc4e82 9190static bool get_phys_addr_v5(CPUARMState *env, uint32_t address,
03ae85f8 9191 MMUAccessType access_type, ARMMMUIdx mmu_idx,
b7cc4e82 9192 hwaddr *phys_ptr, int *prot,
f989983e 9193 target_ulong *page_size,
e14b5a23 9194 ARMMMUFaultInfo *fi)
b5ff1b31 9195{
2fc0cc0e 9196 CPUState *cs = env_cpu(env);
f989983e 9197 int level = 1;
b5ff1b31
FB
9198 uint32_t table;
9199 uint32_t desc;
9200 int type;
9201 int ap;
e389be16 9202 int domain = 0;
dd4ebc2e 9203 int domain_prot;
a8170e5e 9204 hwaddr phys_addr;
0480f69a 9205 uint32_t dacr;
b5ff1b31 9206
9ee6e8bb
PB
9207 /* Pagetable walk. */
9208 /* Lookup l1 descriptor. */
0480f69a 9209 if (!get_level1_table_address(env, mmu_idx, &table, address)) {
e389be16 9210 /* Section translation fault if page walk is disabled by PD0 or PD1 */
f989983e 9211 fi->type = ARMFault_Translation;
e389be16
FA
9212 goto do_fault;
9213 }
a614e698 9214 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
3795a6de 9215 mmu_idx, fi);
3b39d734
PM
9216 if (fi->type != ARMFault_None) {
9217 goto do_fault;
9218 }
9ee6e8bb 9219 type = (desc & 3);
dd4ebc2e 9220 domain = (desc >> 5) & 0x0f;
0480f69a
PM
9221 if (regime_el(env, mmu_idx) == 1) {
9222 dacr = env->cp15.dacr_ns;
9223 } else {
9224 dacr = env->cp15.dacr_s;
9225 }
9226 domain_prot = (dacr >> (domain * 2)) & 3;
9ee6e8bb 9227 if (type == 0) {
601d70b9 9228 /* Section translation fault. */
f989983e 9229 fi->type = ARMFault_Translation;
9ee6e8bb
PB
9230 goto do_fault;
9231 }
f989983e
PM
9232 if (type != 2) {
9233 level = 2;
9234 }
dd4ebc2e 9235 if (domain_prot == 0 || domain_prot == 2) {
f989983e 9236 fi->type = ARMFault_Domain;
9ee6e8bb
PB
9237 goto do_fault;
9238 }
9239 if (type == 2) {
9240 /* 1Mb section. */
9241 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
9242 ap = (desc >> 10) & 3;
d4c430a8 9243 *page_size = 1024 * 1024;
9ee6e8bb
PB
9244 } else {
9245 /* Lookup l2 entry. */
554b0b09
PM
9246 if (type == 1) {
9247 /* Coarse pagetable. */
9248 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
9249 } else {
9250 /* Fine pagetable. */
9251 table = (desc & 0xfffff000) | ((address >> 8) & 0xffc);
9252 }
a614e698 9253 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
3795a6de 9254 mmu_idx, fi);
3b39d734
PM
9255 if (fi->type != ARMFault_None) {
9256 goto do_fault;
9257 }
9ee6e8bb
PB
9258 switch (desc & 3) {
9259 case 0: /* Page translation fault. */
f989983e 9260 fi->type = ARMFault_Translation;
9ee6e8bb
PB
9261 goto do_fault;
9262 case 1: /* 64k page. */
9263 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
9264 ap = (desc >> (4 + ((address >> 13) & 6))) & 3;
d4c430a8 9265 *page_size = 0x10000;
ce819861 9266 break;
9ee6e8bb
PB
9267 case 2: /* 4k page. */
9268 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
c10f7fc3 9269 ap = (desc >> (4 + ((address >> 9) & 6))) & 3;
d4c430a8 9270 *page_size = 0x1000;
ce819861 9271 break;
fc1891c7 9272 case 3: /* 1k page, or ARMv6/XScale "extended small (4k) page" */
554b0b09 9273 if (type == 1) {
fc1891c7
PM
9274 /* ARMv6/XScale extended small page format */
9275 if (arm_feature(env, ARM_FEATURE_XSCALE)
9276 || arm_feature(env, ARM_FEATURE_V6)) {
554b0b09 9277 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
fc1891c7 9278 *page_size = 0x1000;
554b0b09 9279 } else {
fc1891c7
PM
9280 /* UNPREDICTABLE in ARMv5; we choose to take a
9281 * page translation fault.
9282 */
f989983e 9283 fi->type = ARMFault_Translation;
554b0b09
PM
9284 goto do_fault;
9285 }
9286 } else {
9287 phys_addr = (desc & 0xfffffc00) | (address & 0x3ff);
fc1891c7 9288 *page_size = 0x400;
554b0b09 9289 }
9ee6e8bb 9290 ap = (desc >> 4) & 3;
ce819861
PB
9291 break;
9292 default:
9ee6e8bb
PB
9293 /* Never happens, but compiler isn't smart enough to tell. */
9294 abort();
ce819861 9295 }
9ee6e8bb 9296 }
0fbf5238
AJ
9297 *prot = ap_to_rw_prot(env, mmu_idx, ap, domain_prot);
9298 *prot |= *prot ? PAGE_EXEC : 0;
9299 if (!(*prot & (1 << access_type))) {
9ee6e8bb 9300 /* Access permission fault. */
f989983e 9301 fi->type = ARMFault_Permission;
9ee6e8bb
PB
9302 goto do_fault;
9303 }
9304 *phys_ptr = phys_addr;
b7cc4e82 9305 return false;
9ee6e8bb 9306do_fault:
f989983e
PM
9307 fi->domain = domain;
9308 fi->level = level;
b7cc4e82 9309 return true;
9ee6e8bb
PB
9310}
9311
b7cc4e82 9312static bool get_phys_addr_v6(CPUARMState *env, uint32_t address,
03ae85f8 9313 MMUAccessType access_type, ARMMMUIdx mmu_idx,
b7cc4e82 9314 hwaddr *phys_ptr, MemTxAttrs *attrs, int *prot,
f06cf243 9315 target_ulong *page_size, ARMMMUFaultInfo *fi)
9ee6e8bb 9316{
2fc0cc0e 9317 CPUState *cs = env_cpu(env);
f06cf243 9318 int level = 1;
9ee6e8bb
PB
9319 uint32_t table;
9320 uint32_t desc;
9321 uint32_t xn;
de9b05b8 9322 uint32_t pxn = 0;
9ee6e8bb
PB
9323 int type;
9324 int ap;
de9b05b8 9325 int domain = 0;
dd4ebc2e 9326 int domain_prot;
a8170e5e 9327 hwaddr phys_addr;
0480f69a 9328 uint32_t dacr;
8bf5b6a9 9329 bool ns;
9ee6e8bb
PB
9330
9331 /* Pagetable walk. */
9332 /* Lookup l1 descriptor. */
0480f69a 9333 if (!get_level1_table_address(env, mmu_idx, &table, address)) {
e389be16 9334 /* Section translation fault if page walk is disabled by PD0 or PD1 */
f06cf243 9335 fi->type = ARMFault_Translation;
e389be16
FA
9336 goto do_fault;
9337 }
a614e698 9338 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
3795a6de 9339 mmu_idx, fi);
3b39d734
PM
9340 if (fi->type != ARMFault_None) {
9341 goto do_fault;
9342 }
9ee6e8bb 9343 type = (desc & 3);
de9b05b8
PM
9344 if (type == 0 || (type == 3 && !arm_feature(env, ARM_FEATURE_PXN))) {
9345 /* Section translation fault, or attempt to use the encoding
9346 * which is Reserved on implementations without PXN.
9347 */
f06cf243 9348 fi->type = ARMFault_Translation;
9ee6e8bb 9349 goto do_fault;
de9b05b8
PM
9350 }
9351 if ((type == 1) || !(desc & (1 << 18))) {
9352 /* Page or Section. */
dd4ebc2e 9353 domain = (desc >> 5) & 0x0f;
9ee6e8bb 9354 }
0480f69a
PM
9355 if (regime_el(env, mmu_idx) == 1) {
9356 dacr = env->cp15.dacr_ns;
9357 } else {
9358 dacr = env->cp15.dacr_s;
9359 }
f06cf243
PM
9360 if (type == 1) {
9361 level = 2;
9362 }
0480f69a 9363 domain_prot = (dacr >> (domain * 2)) & 3;
dd4ebc2e 9364 if (domain_prot == 0 || domain_prot == 2) {
f06cf243
PM
9365 /* Section or Page domain fault */
9366 fi->type = ARMFault_Domain;
9ee6e8bb
PB
9367 goto do_fault;
9368 }
de9b05b8 9369 if (type != 1) {
9ee6e8bb
PB
9370 if (desc & (1 << 18)) {
9371 /* Supersection. */
9372 phys_addr = (desc & 0xff000000) | (address & 0x00ffffff);
4e42a6ca
SF
9373 phys_addr |= (uint64_t)extract32(desc, 20, 4) << 32;
9374 phys_addr |= (uint64_t)extract32(desc, 5, 4) << 36;
d4c430a8 9375 *page_size = 0x1000000;
b5ff1b31 9376 } else {
9ee6e8bb
PB
9377 /* Section. */
9378 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
d4c430a8 9379 *page_size = 0x100000;
b5ff1b31 9380 }
9ee6e8bb
PB
9381 ap = ((desc >> 10) & 3) | ((desc >> 13) & 4);
9382 xn = desc & (1 << 4);
de9b05b8 9383 pxn = desc & 1;
8bf5b6a9 9384 ns = extract32(desc, 19, 1);
9ee6e8bb 9385 } else {
de9b05b8
PM
9386 if (arm_feature(env, ARM_FEATURE_PXN)) {
9387 pxn = (desc >> 2) & 1;
9388 }
8bf5b6a9 9389 ns = extract32(desc, 3, 1);
9ee6e8bb
PB
9390 /* Lookup l2 entry. */
9391 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
a614e698 9392 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
3795a6de 9393 mmu_idx, fi);
3b39d734
PM
9394 if (fi->type != ARMFault_None) {
9395 goto do_fault;
9396 }
9ee6e8bb
PB
9397 ap = ((desc >> 4) & 3) | ((desc >> 7) & 4);
9398 switch (desc & 3) {
9399 case 0: /* Page translation fault. */
f06cf243 9400 fi->type = ARMFault_Translation;
b5ff1b31 9401 goto do_fault;
9ee6e8bb
PB
9402 case 1: /* 64k page. */
9403 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
9404 xn = desc & (1 << 15);
d4c430a8 9405 *page_size = 0x10000;
9ee6e8bb
PB
9406 break;
9407 case 2: case 3: /* 4k page. */
9408 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
9409 xn = desc & 1;
d4c430a8 9410 *page_size = 0x1000;
9ee6e8bb
PB
9411 break;
9412 default:
9413 /* Never happens, but compiler isn't smart enough to tell. */
9414 abort();
b5ff1b31 9415 }
9ee6e8bb 9416 }
dd4ebc2e 9417 if (domain_prot == 3) {
c0034328
JR
9418 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
9419 } else {
0480f69a 9420 if (pxn && !regime_is_user(env, mmu_idx)) {
de9b05b8
PM
9421 xn = 1;
9422 }
f06cf243
PM
9423 if (xn && access_type == MMU_INST_FETCH) {
9424 fi->type = ARMFault_Permission;
c0034328 9425 goto do_fault;
f06cf243 9426 }
9ee6e8bb 9427
d76951b6
AJ
9428 if (arm_feature(env, ARM_FEATURE_V6K) &&
9429 (regime_sctlr(env, mmu_idx) & SCTLR_AFE)) {
9430 /* The simplified model uses AP[0] as an access control bit. */
9431 if ((ap & 1) == 0) {
9432 /* Access flag fault. */
f06cf243 9433 fi->type = ARMFault_AccessFlag;
d76951b6
AJ
9434 goto do_fault;
9435 }
9436 *prot = simple_ap_to_rw_prot(env, mmu_idx, ap >> 1);
9437 } else {
9438 *prot = ap_to_rw_prot(env, mmu_idx, ap, domain_prot);
c0034328 9439 }
0fbf5238
AJ
9440 if (*prot && !xn) {
9441 *prot |= PAGE_EXEC;
9442 }
9443 if (!(*prot & (1 << access_type))) {
c0034328 9444 /* Access permission fault. */
f06cf243 9445 fi->type = ARMFault_Permission;
c0034328
JR
9446 goto do_fault;
9447 }
3ad493fc 9448 }
8bf5b6a9
PM
9449 if (ns) {
9450 /* The NS bit will (as required by the architecture) have no effect if
9451 * the CPU doesn't support TZ or this is a non-secure translation
9452 * regime, because the attribute will already be non-secure.
9453 */
9454 attrs->secure = false;
9455 }
9ee6e8bb 9456 *phys_ptr = phys_addr;
b7cc4e82 9457 return false;
b5ff1b31 9458do_fault:
f06cf243
PM
9459 fi->domain = domain;
9460 fi->level = level;
b7cc4e82 9461 return true;
b5ff1b31
FB
9462}
9463
1853d5a9 9464/*
a0e966c9 9465 * check_s2_mmu_setup
1853d5a9
EI
9466 * @cpu: ARMCPU
9467 * @is_aa64: True if the translation regime is in AArch64 state
9468 * @startlevel: Suggested starting level
9469 * @inputsize: Bitsize of IPAs
9470 * @stride: Page-table stride (See the ARM ARM)
9471 *
a0e966c9
EI
9472 * Returns true if the suggested S2 translation parameters are OK and
9473 * false otherwise.
1853d5a9 9474 */
a0e966c9
EI
9475static bool check_s2_mmu_setup(ARMCPU *cpu, bool is_aa64, int level,
9476 int inputsize, int stride)
1853d5a9 9477{
98d68ec2
EI
9478 const int grainsize = stride + 3;
9479 int startsizecheck;
9480
1853d5a9
EI
9481 /* Negative levels are never allowed. */
9482 if (level < 0) {
9483 return false;
9484 }
9485
98d68ec2
EI
9486 startsizecheck = inputsize - ((3 - level) * stride + grainsize);
9487 if (startsizecheck < 1 || startsizecheck > stride + 4) {
9488 return false;
9489 }
9490
1853d5a9 9491 if (is_aa64) {
3526423e 9492 CPUARMState *env = &cpu->env;
1853d5a9
EI
9493 unsigned int pamax = arm_pamax(cpu);
9494
9495 switch (stride) {
9496 case 13: /* 64KB Pages. */
9497 if (level == 0 || (level == 1 && pamax <= 42)) {
9498 return false;
9499 }
9500 break;
9501 case 11: /* 16KB Pages. */
9502 if (level == 0 || (level == 1 && pamax <= 40)) {
9503 return false;
9504 }
9505 break;
9506 case 9: /* 4KB Pages. */
9507 if (level == 0 && pamax <= 42) {
9508 return false;
9509 }
9510 break;
9511 default:
9512 g_assert_not_reached();
9513 }
3526423e
EI
9514
9515 /* Inputsize checks. */
9516 if (inputsize > pamax &&
9517 (arm_el_is_aa64(env, 1) || inputsize > 40)) {
9518 /* This is CONSTRAINED UNPREDICTABLE and we choose to fault. */
9519 return false;
9520 }
1853d5a9 9521 } else {
1853d5a9
EI
9522 /* AArch32 only supports 4KB pages. Assert on that. */
9523 assert(stride == 9);
9524
9525 if (level == 0) {
9526 return false;
9527 }
1853d5a9
EI
9528 }
9529 return true;
9530}
9531
5b2d261d
AB
9532/* Translate from the 4-bit stage 2 representation of
9533 * memory attributes (without cache-allocation hints) to
9534 * the 8-bit representation of the stage 1 MAIR registers
9535 * (which includes allocation hints).
9536 *
9537 * ref: shared/translation/attrs/S2AttrDecode()
9538 * .../S2ConvertAttrsHints()
9539 */
9540static uint8_t convert_stage2_attrs(CPUARMState *env, uint8_t s2attrs)
9541{
9542 uint8_t hiattr = extract32(s2attrs, 2, 2);
9543 uint8_t loattr = extract32(s2attrs, 0, 2);
9544 uint8_t hihint = 0, lohint = 0;
9545
9546 if (hiattr != 0) { /* normal memory */
9547 if ((env->cp15.hcr_el2 & HCR_CD) != 0) { /* cache disabled */
9548 hiattr = loattr = 1; /* non-cacheable */
9549 } else {
9550 if (hiattr != 1) { /* Write-through or write-back */
9551 hihint = 3; /* RW allocate */
9552 }
9553 if (loattr != 1) { /* Write-through or write-back */
9554 lohint = 3; /* RW allocate */
9555 }
9556 }
9557 }
9558
9559 return (hiattr << 6) | (hihint << 4) | (loattr << 2) | lohint;
9560}
c47eaf9f 9561#endif /* !CONFIG_USER_ONLY */
5b2d261d 9562
e737ed2a
RH
9563ARMVAParameters aa64_va_parameters_both(CPUARMState *env, uint64_t va,
9564 ARMMMUIdx mmu_idx)
ba97be9f
RH
9565{
9566 uint64_t tcr = regime_tcr(env, mmu_idx)->raw_tcr;
9567 uint32_t el = regime_el(env, mmu_idx);
8220af7e 9568 bool tbi, tbid, epd, hpd, using16k, using64k;
ba97be9f
RH
9569 int select, tsz;
9570
9571 /*
9572 * Bit 55 is always between the two regions, and is canonical for
9573 * determining if address tagging is enabled.
9574 */
9575 select = extract64(va, 55, 1);
9576
9577 if (el > 1) {
9578 tsz = extract32(tcr, 0, 6);
9579 using64k = extract32(tcr, 14, 1);
9580 using16k = extract32(tcr, 15, 1);
9581 if (mmu_idx == ARMMMUIdx_S2NS) {
9582 /* VTCR_EL2 */
8220af7e 9583 tbi = tbid = hpd = false;
ba97be9f
RH
9584 } else {
9585 tbi = extract32(tcr, 20, 1);
9586 hpd = extract32(tcr, 24, 1);
8220af7e 9587 tbid = extract32(tcr, 29, 1);
ba97be9f
RH
9588 }
9589 epd = false;
9590 } else if (!select) {
9591 tsz = extract32(tcr, 0, 6);
9592 epd = extract32(tcr, 7, 1);
9593 using64k = extract32(tcr, 14, 1);
9594 using16k = extract32(tcr, 15, 1);
9595 tbi = extract64(tcr, 37, 1);
9596 hpd = extract64(tcr, 41, 1);
8220af7e 9597 tbid = extract64(tcr, 51, 1);
ba97be9f
RH
9598 } else {
9599 int tg = extract32(tcr, 30, 2);
9600 using16k = tg == 1;
9601 using64k = tg == 3;
9602 tsz = extract32(tcr, 16, 6);
9603 epd = extract32(tcr, 23, 1);
9604 tbi = extract64(tcr, 38, 1);
9605 hpd = extract64(tcr, 42, 1);
8220af7e 9606 tbid = extract64(tcr, 52, 1);
ba97be9f
RH
9607 }
9608 tsz = MIN(tsz, 39); /* TODO: ARMv8.4-TTST */
9609 tsz = MAX(tsz, 16); /* TODO: ARMv8.2-LVA */
9610
9611 return (ARMVAParameters) {
9612 .tsz = tsz,
9613 .select = select,
9614 .tbi = tbi,
8220af7e 9615 .tbid = tbid,
ba97be9f
RH
9616 .epd = epd,
9617 .hpd = hpd,
9618 .using16k = using16k,
9619 .using64k = using64k,
9620 };
9621}
9622
e737ed2a
RH
9623ARMVAParameters aa64_va_parameters(CPUARMState *env, uint64_t va,
9624 ARMMMUIdx mmu_idx, bool data)
9625{
8220af7e
RH
9626 ARMVAParameters ret = aa64_va_parameters_both(env, va, mmu_idx);
9627
9628 /* Present TBI as a composite with TBID. */
9629 ret.tbi &= (data || !ret.tbid);
9630 return ret;
e737ed2a
RH
9631}
9632
c47eaf9f 9633#ifndef CONFIG_USER_ONLY
ba97be9f
RH
9634static ARMVAParameters aa32_va_parameters(CPUARMState *env, uint32_t va,
9635 ARMMMUIdx mmu_idx)
9636{
9637 uint64_t tcr = regime_tcr(env, mmu_idx)->raw_tcr;
9638 uint32_t el = regime_el(env, mmu_idx);
9639 int select, tsz;
9640 bool epd, hpd;
9641
9642 if (mmu_idx == ARMMMUIdx_S2NS) {
9643 /* VTCR */
9644 bool sext = extract32(tcr, 4, 1);
9645 bool sign = extract32(tcr, 3, 1);
9646
9647 /*
9648 * If the sign-extend bit is not the same as t0sz[3], the result
9649 * is unpredictable. Flag this as a guest error.
9650 */
9651 if (sign != sext) {
9652 qemu_log_mask(LOG_GUEST_ERROR,
9653 "AArch32: VTCR.S / VTCR.T0SZ[3] mismatch\n");
9654 }
9655 tsz = sextract32(tcr, 0, 4) + 8;
9656 select = 0;
9657 hpd = false;
9658 epd = false;
9659 } else if (el == 2) {
9660 /* HTCR */
9661 tsz = extract32(tcr, 0, 3);
9662 select = 0;
9663 hpd = extract64(tcr, 24, 1);
9664 epd = false;
9665 } else {
9666 int t0sz = extract32(tcr, 0, 3);
9667 int t1sz = extract32(tcr, 16, 3);
9668
9669 if (t1sz == 0) {
9670 select = va > (0xffffffffu >> t0sz);
9671 } else {
9672 /* Note that we will detect errors later. */
9673 select = va >= ~(0xffffffffu >> t1sz);
9674 }
9675 if (!select) {
9676 tsz = t0sz;
9677 epd = extract32(tcr, 7, 1);
9678 hpd = extract64(tcr, 41, 1);
9679 } else {
9680 tsz = t1sz;
9681 epd = extract32(tcr, 23, 1);
9682 hpd = extract64(tcr, 42, 1);
9683 }
9684 /* For aarch32, hpd0 is not enabled without t2e as well. */
9685 hpd &= extract32(tcr, 6, 1);
9686 }
9687
9688 return (ARMVAParameters) {
9689 .tsz = tsz,
9690 .select = select,
9691 .epd = epd,
9692 .hpd = hpd,
9693 };
9694}
9695
b7cc4e82 9696static bool get_phys_addr_lpae(CPUARMState *env, target_ulong address,
03ae85f8 9697 MMUAccessType access_type, ARMMMUIdx mmu_idx,
b7cc4e82 9698 hwaddr *phys_ptr, MemTxAttrs *txattrs, int *prot,
da909b2c 9699 target_ulong *page_size_ptr,
5b2d261d 9700 ARMMMUFaultInfo *fi, ARMCacheAttrs *cacheattrs)
3dde962f 9701{
2fc0cc0e 9702 ARMCPU *cpu = env_archcpu(env);
1853d5a9 9703 CPUState *cs = CPU(cpu);
3dde962f 9704 /* Read an LPAE long-descriptor translation table. */
da909b2c 9705 ARMFaultType fault_type = ARMFault_Translation;
1b4093ea 9706 uint32_t level;
ba97be9f 9707 ARMVAParameters param;
3dde962f 9708 uint64_t ttbr;
dddb5223 9709 hwaddr descaddr, indexmask, indexmask_grainsize;
3dde962f 9710 uint32_t tableattrs;
36d820af 9711 target_ulong page_size;
3dde962f 9712 uint32_t attrs;
ba97be9f
RH
9713 int32_t stride;
9714 int addrsize, inputsize;
0480f69a 9715 TCR *tcr = regime_tcr(env, mmu_idx);
d8e052b3 9716 int ap, ns, xn, pxn;
88e8add8 9717 uint32_t el = regime_el(env, mmu_idx);
ba97be9f 9718 bool ttbr1_valid;
6109769a 9719 uint64_t descaddrmask;
6e99f762 9720 bool aarch64 = arm_el_is_aa64(env, el);
1bafc2ba 9721 bool guarded = false;
0480f69a
PM
9722
9723 /* TODO:
88e8add8
GB
9724 * This code does not handle the different format TCR for VTCR_EL2.
9725 * This code also does not support shareability levels.
9726 * Attribute and permission bit handling should also be checked when adding
9727 * support for those page table walks.
0480f69a 9728 */
6e99f762 9729 if (aarch64) {
ba97be9f
RH
9730 param = aa64_va_parameters(env, address, mmu_idx,
9731 access_type != MMU_INST_FETCH);
1b4093ea 9732 level = 0;
88e8add8
GB
9733 /* If we are in 64-bit EL2 or EL3 then there is no TTBR1, so mark it
9734 * invalid.
9735 */
ba97be9f
RH
9736 ttbr1_valid = (el < 2);
9737 addrsize = 64 - 8 * param.tbi;
9738 inputsize = 64 - param.tsz;
d0a2cbce 9739 } else {
ba97be9f 9740 param = aa32_va_parameters(env, address, mmu_idx);
1b4093ea 9741 level = 1;
d0a2cbce 9742 /* There is no TTBR1 for EL2 */
ba97be9f
RH
9743 ttbr1_valid = (el != 2);
9744 addrsize = (mmu_idx == ARMMMUIdx_S2NS ? 40 : 32);
9745 inputsize = addrsize - param.tsz;
2c8dd318 9746 }
3dde962f 9747
ba97be9f
RH
9748 /*
9749 * We determined the region when collecting the parameters, but we
9750 * have not yet validated that the address is valid for the region.
9751 * Extract the top bits and verify that they all match select.
36d820af
RH
9752 *
9753 * For aa32, if inputsize == addrsize, then we have selected the
9754 * region by exclusion in aa32_va_parameters and there is no more
9755 * validation to do here.
9756 */
9757 if (inputsize < addrsize) {
9758 target_ulong top_bits = sextract64(address, inputsize,
9759 addrsize - inputsize);
9760 if (-top_bits != param.select || (param.select && !ttbr1_valid)) {
9761 /* The gap between the two regions is a Translation fault */
9762 fault_type = ARMFault_Translation;
9763 goto do_fault;
9764 }
3dde962f
PM
9765 }
9766
ba97be9f
RH
9767 if (param.using64k) {
9768 stride = 13;
9769 } else if (param.using16k) {
9770 stride = 11;
9771 } else {
9772 stride = 9;
9773 }
9774
3dde962f
PM
9775 /* Note that QEMU ignores shareability and cacheability attributes,
9776 * so we don't need to do anything with the SH, ORGN, IRGN fields
9777 * in the TTBCR. Similarly, TTBCR:A1 selects whether we get the
9778 * ASID from TTBR0 or TTBR1, but QEMU's TLB doesn't currently
9779 * implement any ASID-like capability so we can ignore it (instead
9780 * we will always flush the TLB any time the ASID is changed).
9781 */
ba97be9f 9782 ttbr = regime_ttbr(env, mmu_idx, param.select);
3dde962f 9783
0480f69a 9784 /* Here we should have set up all the parameters for the translation:
6e99f762 9785 * inputsize, ttbr, epd, stride, tbi
0480f69a
PM
9786 */
9787
ba97be9f 9788 if (param.epd) {
88e8add8
GB
9789 /* Translation table walk disabled => Translation fault on TLB miss
9790 * Note: This is always 0 on 64-bit EL2 and EL3.
9791 */
3dde962f
PM
9792 goto do_fault;
9793 }
9794
1853d5a9
EI
9795 if (mmu_idx != ARMMMUIdx_S2NS) {
9796 /* The starting level depends on the virtual address size (which can
9797 * be up to 48 bits) and the translation granule size. It indicates
9798 * the number of strides (stride bits at a time) needed to
9799 * consume the bits of the input address. In the pseudocode this is:
9800 * level = 4 - RoundUp((inputsize - grainsize) / stride)
9801 * where their 'inputsize' is our 'inputsize', 'grainsize' is
9802 * our 'stride + 3' and 'stride' is our 'stride'.
9803 * Applying the usual "rounded up m/n is (m+n-1)/n" and simplifying:
9804 * = 4 - (inputsize - stride - 3 + stride - 1) / stride
9805 * = 4 - (inputsize - 4) / stride;
9806 */
9807 level = 4 - (inputsize - 4) / stride;
9808 } else {
9809 /* For stage 2 translations the starting level is specified by the
9810 * VTCR_EL2.SL0 field (whose interpretation depends on the page size)
9811 */
1b4093ea
SS
9812 uint32_t sl0 = extract32(tcr->raw_tcr, 6, 2);
9813 uint32_t startlevel;
1853d5a9
EI
9814 bool ok;
9815
6e99f762 9816 if (!aarch64 || stride == 9) {
1853d5a9 9817 /* AArch32 or 4KB pages */
1b4093ea 9818 startlevel = 2 - sl0;
1853d5a9
EI
9819 } else {
9820 /* 16KB or 64KB pages */
1b4093ea 9821 startlevel = 3 - sl0;
1853d5a9
EI
9822 }
9823
9824 /* Check that the starting level is valid. */
6e99f762 9825 ok = check_s2_mmu_setup(cpu, aarch64, startlevel,
1b4093ea 9826 inputsize, stride);
1853d5a9 9827 if (!ok) {
da909b2c 9828 fault_type = ARMFault_Translation;
1853d5a9
EI
9829 goto do_fault;
9830 }
1b4093ea 9831 level = startlevel;
1853d5a9 9832 }
3dde962f 9833
dddb5223
SS
9834 indexmask_grainsize = (1ULL << (stride + 3)) - 1;
9835 indexmask = (1ULL << (inputsize - (stride * (4 - level)))) - 1;
3dde962f
PM
9836
9837 /* Now we can extract the actual base address from the TTBR */
2c8dd318 9838 descaddr = extract64(ttbr, 0, 48);
dddb5223 9839 descaddr &= ~indexmask;
3dde962f 9840
6109769a 9841 /* The address field in the descriptor goes up to bit 39 for ARMv7
dddb5223
SS
9842 * but up to bit 47 for ARMv8, but we use the descaddrmask
9843 * up to bit 39 for AArch32, because we don't need other bits in that case
9844 * to construct next descriptor address (anyway they should be all zeroes).
6109769a 9845 */
6e99f762 9846 descaddrmask = ((1ull << (aarch64 ? 48 : 40)) - 1) &
dddb5223 9847 ~indexmask_grainsize;
6109769a 9848
ebca90e4
PM
9849 /* Secure accesses start with the page table in secure memory and
9850 * can be downgraded to non-secure at any step. Non-secure accesses
9851 * remain non-secure. We implement this by just ORing in the NSTable/NS
9852 * bits at each step.
9853 */
9854 tableattrs = regime_is_secure(env, mmu_idx) ? 0 : (1 << 4);
3dde962f
PM
9855 for (;;) {
9856 uint64_t descriptor;
ebca90e4 9857 bool nstable;
3dde962f 9858
dddb5223 9859 descaddr |= (address >> (stride * (4 - level))) & indexmask;
2c8dd318 9860 descaddr &= ~7ULL;
ebca90e4 9861 nstable = extract32(tableattrs, 4, 1);
3795a6de 9862 descriptor = arm_ldq_ptw(cs, descaddr, !nstable, mmu_idx, fi);
3b39d734 9863 if (fi->type != ARMFault_None) {
37785977
EI
9864 goto do_fault;
9865 }
9866
3dde962f
PM
9867 if (!(descriptor & 1) ||
9868 (!(descriptor & 2) && (level == 3))) {
9869 /* Invalid, or the Reserved level 3 encoding */
9870 goto do_fault;
9871 }
6109769a 9872 descaddr = descriptor & descaddrmask;
3dde962f
PM
9873
9874 if ((descriptor & 2) && (level < 3)) {
037c13c5 9875 /* Table entry. The top five bits are attributes which may
3dde962f
PM
9876 * propagate down through lower levels of the table (and
9877 * which are all arranged so that 0 means "no effect", so
9878 * we can gather them up by ORing in the bits at each level).
9879 */
9880 tableattrs |= extract64(descriptor, 59, 5);
9881 level++;
dddb5223 9882 indexmask = indexmask_grainsize;
3dde962f
PM
9883 continue;
9884 }
9885 /* Block entry at level 1 or 2, or page entry at level 3.
9886 * These are basically the same thing, although the number
9887 * of bits we pull in from the vaddr varies.
9888 */
973a5434 9889 page_size = (1ULL << ((stride * (4 - level)) + 3));
3dde962f 9890 descaddr |= (address & (page_size - 1));
6ab1a5ee 9891 /* Extract attributes from the descriptor */
d615efac
IC
9892 attrs = extract64(descriptor, 2, 10)
9893 | (extract64(descriptor, 52, 12) << 10);
6ab1a5ee
EI
9894
9895 if (mmu_idx == ARMMMUIdx_S2NS) {
9896 /* Stage 2 table descriptors do not include any attribute fields */
9897 break;
9898 }
9899 /* Merge in attributes from table descriptors */
037c13c5 9900 attrs |= nstable << 3; /* NS */
1bafc2ba 9901 guarded = extract64(descriptor, 50, 1); /* GP */
ba97be9f 9902 if (param.hpd) {
037c13c5
RH
9903 /* HPD disables all the table attributes except NSTable. */
9904 break;
9905 }
9906 attrs |= extract32(tableattrs, 0, 2) << 11; /* XN, PXN */
3dde962f
PM
9907 /* The sense of AP[1] vs APTable[0] is reversed, as APTable[0] == 1
9908 * means "force PL1 access only", which means forcing AP[1] to 0.
9909 */
037c13c5
RH
9910 attrs &= ~(extract32(tableattrs, 2, 1) << 4); /* !APT[0] => AP[1] */
9911 attrs |= extract32(tableattrs, 3, 1) << 5; /* APT[1] => AP[2] */
3dde962f
PM
9912 break;
9913 }
9914 /* Here descaddr is the final physical address, and attributes
9915 * are all in attrs.
9916 */
da909b2c 9917 fault_type = ARMFault_AccessFlag;
3dde962f
PM
9918 if ((attrs & (1 << 8)) == 0) {
9919 /* Access flag */
9920 goto do_fault;
9921 }
d8e052b3
AJ
9922
9923 ap = extract32(attrs, 4, 2);
d8e052b3 9924 xn = extract32(attrs, 12, 1);
d8e052b3 9925
6ab1a5ee
EI
9926 if (mmu_idx == ARMMMUIdx_S2NS) {
9927 ns = true;
9928 *prot = get_S2prot(env, ap, xn);
9929 } else {
9930 ns = extract32(attrs, 3, 1);
9931 pxn = extract32(attrs, 11, 1);
6e99f762 9932 *prot = get_S1prot(env, mmu_idx, aarch64, ap, ns, xn, pxn);
6ab1a5ee 9933 }
d8e052b3 9934
da909b2c 9935 fault_type = ARMFault_Permission;
d8e052b3 9936 if (!(*prot & (1 << access_type))) {
3dde962f
PM
9937 goto do_fault;
9938 }
3dde962f 9939
8bf5b6a9
PM
9940 if (ns) {
9941 /* The NS bit will (as required by the architecture) have no effect if
9942 * the CPU doesn't support TZ or this is a non-secure translation
9943 * regime, because the attribute will already be non-secure.
9944 */
9945 txattrs->secure = false;
9946 }
1bafc2ba
RH
9947 /* When in aarch64 mode, and BTI is enabled, remember GP in the IOTLB. */
9948 if (aarch64 && guarded && cpu_isar_feature(aa64_bti, cpu)) {
9949 txattrs->target_tlb_bit0 = true;
9950 }
5b2d261d
AB
9951
9952 if (cacheattrs != NULL) {
9953 if (mmu_idx == ARMMMUIdx_S2NS) {
9954 cacheattrs->attrs = convert_stage2_attrs(env,
9955 extract32(attrs, 0, 4));
9956 } else {
9957 /* Index into MAIR registers for cache attributes */
9958 uint8_t attrindx = extract32(attrs, 0, 3);
9959 uint64_t mair = env->cp15.mair_el[regime_el(env, mmu_idx)];
9960 assert(attrindx <= 7);
9961 cacheattrs->attrs = extract64(mair, attrindx * 8, 8);
9962 }
9963 cacheattrs->shareability = extract32(attrs, 6, 2);
9964 }
9965
3dde962f
PM
9966 *phys_ptr = descaddr;
9967 *page_size_ptr = page_size;
b7cc4e82 9968 return false;
3dde962f
PM
9969
9970do_fault:
da909b2c
PM
9971 fi->type = fault_type;
9972 fi->level = level;
37785977
EI
9973 /* Tag the error as S2 for failed S1 PTW at S2 or ordinary S2. */
9974 fi->stage2 = fi->s1ptw || (mmu_idx == ARMMMUIdx_S2NS);
b7cc4e82 9975 return true;
3dde962f
PM
9976}
9977
f6bda88f
PC
9978static inline void get_phys_addr_pmsav7_default(CPUARMState *env,
9979 ARMMMUIdx mmu_idx,
9980 int32_t address, int *prot)
9981{
3a00d560
MD
9982 if (!arm_feature(env, ARM_FEATURE_M)) {
9983 *prot = PAGE_READ | PAGE_WRITE;
9984 switch (address) {
9985 case 0xF0000000 ... 0xFFFFFFFF:
9986 if (regime_sctlr(env, mmu_idx) & SCTLR_V) {
9987 /* hivecs execing is ok */
9988 *prot |= PAGE_EXEC;
9989 }
9990 break;
9991 case 0x00000000 ... 0x7FFFFFFF:
f6bda88f 9992 *prot |= PAGE_EXEC;
3a00d560
MD
9993 break;
9994 }
9995 } else {
9996 /* Default system address map for M profile cores.
9997 * The architecture specifies which regions are execute-never;
9998 * at the MPU level no other checks are defined.
9999 */
10000 switch (address) {
10001 case 0x00000000 ... 0x1fffffff: /* ROM */
10002 case 0x20000000 ... 0x3fffffff: /* SRAM */
10003 case 0x60000000 ... 0x7fffffff: /* RAM */
10004 case 0x80000000 ... 0x9fffffff: /* RAM */
10005 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
10006 break;
10007 case 0x40000000 ... 0x5fffffff: /* Peripheral */
10008 case 0xa0000000 ... 0xbfffffff: /* Device */
10009 case 0xc0000000 ... 0xdfffffff: /* Device */
10010 case 0xe0000000 ... 0xffffffff: /* System */
10011 *prot = PAGE_READ | PAGE_WRITE;
10012 break;
10013 default:
10014 g_assert_not_reached();
f6bda88f 10015 }
f6bda88f 10016 }
f6bda88f
PC
10017}
10018
29c483a5
MD
10019static bool pmsav7_use_background_region(ARMCPU *cpu,
10020 ARMMMUIdx mmu_idx, bool is_user)
10021{
10022 /* Return true if we should use the default memory map as a
10023 * "background" region if there are no hits against any MPU regions.
10024 */
10025 CPUARMState *env = &cpu->env;
10026
10027 if (is_user) {
10028 return false;
10029 }
10030
10031 if (arm_feature(env, ARM_FEATURE_M)) {
ecf5e8ea
PM
10032 return env->v7m.mpu_ctrl[regime_is_secure(env, mmu_idx)]
10033 & R_V7M_MPU_CTRL_PRIVDEFENA_MASK;
29c483a5
MD
10034 } else {
10035 return regime_sctlr(env, mmu_idx) & SCTLR_BR;
10036 }
10037}
10038
38aaa60c
PM
10039static inline bool m_is_ppb_region(CPUARMState *env, uint32_t address)
10040{
10041 /* True if address is in the M profile PPB region 0xe0000000 - 0xe00fffff */
10042 return arm_feature(env, ARM_FEATURE_M) &&
10043 extract32(address, 20, 12) == 0xe00;
10044}
10045
bf446a11
PM
10046static inline bool m_is_system_region(CPUARMState *env, uint32_t address)
10047{
10048 /* True if address is in the M profile system region
10049 * 0xe0000000 - 0xffffffff
10050 */
10051 return arm_feature(env, ARM_FEATURE_M) && extract32(address, 29, 3) == 0x7;
10052}
10053
f6bda88f 10054static bool get_phys_addr_pmsav7(CPUARMState *env, uint32_t address,
03ae85f8 10055 MMUAccessType access_type, ARMMMUIdx mmu_idx,
9375ad15 10056 hwaddr *phys_ptr, int *prot,
e5e40999 10057 target_ulong *page_size,
9375ad15 10058 ARMMMUFaultInfo *fi)
f6bda88f 10059{
2fc0cc0e 10060 ARMCPU *cpu = env_archcpu(env);
f6bda88f
PC
10061 int n;
10062 bool is_user = regime_is_user(env, mmu_idx);
10063
10064 *phys_ptr = address;
e5e40999 10065 *page_size = TARGET_PAGE_SIZE;
f6bda88f
PC
10066 *prot = 0;
10067
38aaa60c
PM
10068 if (regime_translation_disabled(env, mmu_idx) ||
10069 m_is_ppb_region(env, address)) {
10070 /* MPU disabled or M profile PPB access: use default memory map.
10071 * The other case which uses the default memory map in the
10072 * v7M ARM ARM pseudocode is exception vector reads from the vector
10073 * table. In QEMU those accesses are done in arm_v7m_load_vector(),
10074 * which always does a direct read using address_space_ldl(), rather
10075 * than going via this function, so we don't need to check that here.
10076 */
f6bda88f
PC
10077 get_phys_addr_pmsav7_default(env, mmu_idx, address, prot);
10078 } else { /* MPU enabled */
10079 for (n = (int)cpu->pmsav7_dregion - 1; n >= 0; n--) {
10080 /* region search */
10081 uint32_t base = env->pmsav7.drbar[n];
10082 uint32_t rsize = extract32(env->pmsav7.drsr[n], 1, 5);
10083 uint32_t rmask;
10084 bool srdis = false;
10085
10086 if (!(env->pmsav7.drsr[n] & 0x1)) {
10087 continue;
10088 }
10089
10090 if (!rsize) {
c9f9f124
MD
10091 qemu_log_mask(LOG_GUEST_ERROR,
10092 "DRSR[%d]: Rsize field cannot be 0\n", n);
f6bda88f
PC
10093 continue;
10094 }
10095 rsize++;
10096 rmask = (1ull << rsize) - 1;
10097
10098 if (base & rmask) {
c9f9f124
MD
10099 qemu_log_mask(LOG_GUEST_ERROR,
10100 "DRBAR[%d]: 0x%" PRIx32 " misaligned "
10101 "to DRSR region size, mask = 0x%" PRIx32 "\n",
10102 n, base, rmask);
f6bda88f
PC
10103 continue;
10104 }
10105
10106 if (address < base || address > base + rmask) {
9d2b5a58
PM
10107 /*
10108 * Address not in this region. We must check whether the
10109 * region covers addresses in the same page as our address.
10110 * In that case we must not report a size that covers the
10111 * whole page for a subsequent hit against a different MPU
10112 * region or the background region, because it would result in
10113 * incorrect TLB hits for subsequent accesses to addresses that
10114 * are in this MPU region.
10115 */
10116 if (ranges_overlap(base, rmask,
10117 address & TARGET_PAGE_MASK,
10118 TARGET_PAGE_SIZE)) {
10119 *page_size = 1;
10120 }
f6bda88f
PC
10121 continue;
10122 }
10123
10124 /* Region matched */
10125
10126 if (rsize >= 8) { /* no subregions for regions < 256 bytes */
10127 int i, snd;
10128 uint32_t srdis_mask;
10129
10130 rsize -= 3; /* sub region size (power of 2) */
10131 snd = ((address - base) >> rsize) & 0x7;
10132 srdis = extract32(env->pmsav7.drsr[n], snd + 8, 1);
10133
10134 srdis_mask = srdis ? 0x3 : 0x0;
10135 for (i = 2; i <= 8 && rsize < TARGET_PAGE_BITS; i *= 2) {
10136 /* This will check in groups of 2, 4 and then 8, whether
10137 * the subregion bits are consistent. rsize is incremented
10138 * back up to give the region size, considering consistent
10139 * adjacent subregions as one region. Stop testing if rsize
10140 * is already big enough for an entire QEMU page.
10141 */
10142 int snd_rounded = snd & ~(i - 1);
10143 uint32_t srdis_multi = extract32(env->pmsav7.drsr[n],
10144 snd_rounded + 8, i);
10145 if (srdis_mask ^ srdis_multi) {
10146 break;
10147 }
10148 srdis_mask = (srdis_mask << i) | srdis_mask;
10149 rsize++;
10150 }
10151 }
f6bda88f
PC
10152 if (srdis) {
10153 continue;
10154 }
e5e40999
PM
10155 if (rsize < TARGET_PAGE_BITS) {
10156 *page_size = 1 << rsize;
10157 }
f6bda88f
PC
10158 break;
10159 }
10160
10161 if (n == -1) { /* no hits */
29c483a5 10162 if (!pmsav7_use_background_region(cpu, mmu_idx, is_user)) {
f6bda88f 10163 /* background fault */
9375ad15 10164 fi->type = ARMFault_Background;
f6bda88f
PC
10165 return true;
10166 }
10167 get_phys_addr_pmsav7_default(env, mmu_idx, address, prot);
10168 } else { /* a MPU hit! */
10169 uint32_t ap = extract32(env->pmsav7.dracr[n], 8, 3);
bf446a11
PM
10170 uint32_t xn = extract32(env->pmsav7.dracr[n], 12, 1);
10171
10172 if (m_is_system_region(env, address)) {
10173 /* System space is always execute never */
10174 xn = 1;
10175 }
f6bda88f
PC
10176
10177 if (is_user) { /* User mode AP bit decoding */
10178 switch (ap) {
10179 case 0:
10180 case 1:
10181 case 5:
10182 break; /* no access */
10183 case 3:
10184 *prot |= PAGE_WRITE;
10185 /* fall through */
10186 case 2:
10187 case 6:
10188 *prot |= PAGE_READ | PAGE_EXEC;
10189 break;
8638f1ad
PM
10190 case 7:
10191 /* for v7M, same as 6; for R profile a reserved value */
10192 if (arm_feature(env, ARM_FEATURE_M)) {
10193 *prot |= PAGE_READ | PAGE_EXEC;
10194 break;
10195 }
10196 /* fall through */
f6bda88f
PC
10197 default:
10198 qemu_log_mask(LOG_GUEST_ERROR,
c9f9f124
MD
10199 "DRACR[%d]: Bad value for AP bits: 0x%"
10200 PRIx32 "\n", n, ap);
f6bda88f
PC
10201 }
10202 } else { /* Priv. mode AP bits decoding */
10203 switch (ap) {
10204 case 0:
10205 break; /* no access */
10206 case 1:
10207 case 2:
10208 case 3:
10209 *prot |= PAGE_WRITE;
10210 /* fall through */
10211 case 5:
10212 case 6:
10213 *prot |= PAGE_READ | PAGE_EXEC;
10214 break;
8638f1ad
PM
10215 case 7:
10216 /* for v7M, same as 6; for R profile a reserved value */
10217 if (arm_feature(env, ARM_FEATURE_M)) {
10218 *prot |= PAGE_READ | PAGE_EXEC;
10219 break;
10220 }
10221 /* fall through */
f6bda88f
PC
10222 default:
10223 qemu_log_mask(LOG_GUEST_ERROR,
c9f9f124
MD
10224 "DRACR[%d]: Bad value for AP bits: 0x%"
10225 PRIx32 "\n", n, ap);
f6bda88f
PC
10226 }
10227 }
10228
10229 /* execute never */
bf446a11 10230 if (xn) {
f6bda88f
PC
10231 *prot &= ~PAGE_EXEC;
10232 }
10233 }
10234 }
10235
9375ad15
PM
10236 fi->type = ARMFault_Permission;
10237 fi->level = 1;
f6bda88f
PC
10238 return !(*prot & (1 << access_type));
10239}
10240
35337cc3
PM
10241static bool v8m_is_sau_exempt(CPUARMState *env,
10242 uint32_t address, MMUAccessType access_type)
10243{
10244 /* The architecture specifies that certain address ranges are
10245 * exempt from v8M SAU/IDAU checks.
10246 */
10247 return
10248 (access_type == MMU_INST_FETCH && m_is_system_region(env, address)) ||
10249 (address >= 0xe0000000 && address <= 0xe0002fff) ||
10250 (address >= 0xe000e000 && address <= 0xe000efff) ||
10251 (address >= 0xe002e000 && address <= 0xe002efff) ||
10252 (address >= 0xe0040000 && address <= 0xe0041fff) ||
10253 (address >= 0xe00ff000 && address <= 0xe00fffff);
10254}
10255
787a7e76 10256void v8m_security_lookup(CPUARMState *env, uint32_t address,
35337cc3
PM
10257 MMUAccessType access_type, ARMMMUIdx mmu_idx,
10258 V8M_SAttributes *sattrs)
10259{
10260 /* Look up the security attributes for this address. Compare the
10261 * pseudocode SecurityCheck() function.
10262 * We assume the caller has zero-initialized *sattrs.
10263 */
2fc0cc0e 10264 ARMCPU *cpu = env_archcpu(env);
35337cc3 10265 int r;
181962fd
PM
10266 bool idau_exempt = false, idau_ns = true, idau_nsc = true;
10267 int idau_region = IREGION_NOTVALID;
72042435
PM
10268 uint32_t addr_page_base = address & TARGET_PAGE_MASK;
10269 uint32_t addr_page_limit = addr_page_base + (TARGET_PAGE_SIZE - 1);
35337cc3 10270
181962fd
PM
10271 if (cpu->idau) {
10272 IDAUInterfaceClass *iic = IDAU_INTERFACE_GET_CLASS(cpu->idau);
10273 IDAUInterface *ii = IDAU_INTERFACE(cpu->idau);
10274
10275 iic->check(ii, address, &idau_region, &idau_exempt, &idau_ns,
10276 &idau_nsc);
10277 }
35337cc3
PM
10278
10279 if (access_type == MMU_INST_FETCH && extract32(address, 28, 4) == 0xf) {
10280 /* 0xf0000000..0xffffffff is always S for insn fetches */
10281 return;
10282 }
10283
181962fd 10284 if (idau_exempt || v8m_is_sau_exempt(env, address, access_type)) {
35337cc3
PM
10285 sattrs->ns = !regime_is_secure(env, mmu_idx);
10286 return;
10287 }
10288
181962fd
PM
10289 if (idau_region != IREGION_NOTVALID) {
10290 sattrs->irvalid = true;
10291 sattrs->iregion = idau_region;
10292 }
10293
35337cc3
PM
10294 switch (env->sau.ctrl & 3) {
10295 case 0: /* SAU.ENABLE == 0, SAU.ALLNS == 0 */
10296 break;
10297 case 2: /* SAU.ENABLE == 0, SAU.ALLNS == 1 */
10298 sattrs->ns = true;
10299 break;
10300 default: /* SAU.ENABLE == 1 */
10301 for (r = 0; r < cpu->sau_sregion; r++) {
10302 if (env->sau.rlar[r] & 1) {
10303 uint32_t base = env->sau.rbar[r] & ~0x1f;
10304 uint32_t limit = env->sau.rlar[r] | 0x1f;
10305
10306 if (base <= address && limit >= address) {
72042435
PM
10307 if (base > addr_page_base || limit < addr_page_limit) {
10308 sattrs->subpage = true;
10309 }
35337cc3
PM
10310 if (sattrs->srvalid) {
10311 /* If we hit in more than one region then we must report
10312 * as Secure, not NS-Callable, with no valid region
10313 * number info.
10314 */
10315 sattrs->ns = false;
10316 sattrs->nsc = false;
10317 sattrs->sregion = 0;
10318 sattrs->srvalid = false;
10319 break;
10320 } else {
10321 if (env->sau.rlar[r] & 2) {
10322 sattrs->nsc = true;
10323 } else {
10324 sattrs->ns = true;
10325 }
10326 sattrs->srvalid = true;
10327 sattrs->sregion = r;
10328 }
9d2b5a58
PM
10329 } else {
10330 /*
10331 * Address not in this region. We must check whether the
10332 * region covers addresses in the same page as our address.
10333 * In that case we must not report a size that covers the
10334 * whole page for a subsequent hit against a different MPU
10335 * region or the background region, because it would result
10336 * in incorrect TLB hits for subsequent accesses to
10337 * addresses that are in this MPU region.
10338 */
10339 if (limit >= base &&
10340 ranges_overlap(base, limit - base + 1,
10341 addr_page_base,
10342 TARGET_PAGE_SIZE)) {
10343 sattrs->subpage = true;
10344 }
35337cc3
PM
10345 }
10346 }
10347 }
7e3f1223
TR
10348 break;
10349 }
35337cc3 10350
7e3f1223
TR
10351 /*
10352 * The IDAU will override the SAU lookup results if it specifies
10353 * higher security than the SAU does.
10354 */
10355 if (!idau_ns) {
10356 if (sattrs->ns || (!idau_nsc && sattrs->nsc)) {
10357 sattrs->ns = false;
10358 sattrs->nsc = idau_nsc;
181962fd 10359 }
35337cc3
PM
10360 }
10361}
10362
787a7e76 10363bool pmsav8_mpu_lookup(CPUARMState *env, uint32_t address,
54317c0f
PM
10364 MMUAccessType access_type, ARMMMUIdx mmu_idx,
10365 hwaddr *phys_ptr, MemTxAttrs *txattrs,
72042435
PM
10366 int *prot, bool *is_subpage,
10367 ARMMMUFaultInfo *fi, uint32_t *mregion)
54317c0f
PM
10368{
10369 /* Perform a PMSAv8 MPU lookup (without also doing the SAU check
10370 * that a full phys-to-virt translation does).
10371 * mregion is (if not NULL) set to the region number which matched,
10372 * or -1 if no region number is returned (MPU off, address did not
10373 * hit a region, address hit in multiple regions).
72042435
PM
10374 * We set is_subpage to true if the region hit doesn't cover the
10375 * entire TARGET_PAGE the address is within.
54317c0f 10376 */
2fc0cc0e 10377 ARMCPU *cpu = env_archcpu(env);
504e3cc3 10378 bool is_user = regime_is_user(env, mmu_idx);
62c58ee0 10379 uint32_t secure = regime_is_secure(env, mmu_idx);
504e3cc3
PM
10380 int n;
10381 int matchregion = -1;
10382 bool hit = false;
72042435
PM
10383 uint32_t addr_page_base = address & TARGET_PAGE_MASK;
10384 uint32_t addr_page_limit = addr_page_base + (TARGET_PAGE_SIZE - 1);
504e3cc3 10385
72042435 10386 *is_subpage = false;
504e3cc3
PM
10387 *phys_ptr = address;
10388 *prot = 0;
54317c0f
PM
10389 if (mregion) {
10390 *mregion = -1;
35337cc3
PM
10391 }
10392
504e3cc3
PM
10393 /* Unlike the ARM ARM pseudocode, we don't need to check whether this
10394 * was an exception vector read from the vector table (which is always
10395 * done using the default system address map), because those accesses
10396 * are done in arm_v7m_load_vector(), which always does a direct
10397 * read using address_space_ldl(), rather than going via this function.
10398 */
10399 if (regime_translation_disabled(env, mmu_idx)) { /* MPU disabled */
10400 hit = true;
10401 } else if (m_is_ppb_region(env, address)) {
10402 hit = true;
504e3cc3 10403 } else {
cff21316
PM
10404 if (pmsav7_use_background_region(cpu, mmu_idx, is_user)) {
10405 hit = true;
10406 }
10407
504e3cc3
PM
10408 for (n = (int)cpu->pmsav7_dregion - 1; n >= 0; n--) {
10409 /* region search */
10410 /* Note that the base address is bits [31:5] from the register
10411 * with bits [4:0] all zeroes, but the limit address is bits
10412 * [31:5] from the register with bits [4:0] all ones.
10413 */
62c58ee0
PM
10414 uint32_t base = env->pmsav8.rbar[secure][n] & ~0x1f;
10415 uint32_t limit = env->pmsav8.rlar[secure][n] | 0x1f;
504e3cc3 10416
62c58ee0 10417 if (!(env->pmsav8.rlar[secure][n] & 0x1)) {
504e3cc3
PM
10418 /* Region disabled */
10419 continue;
10420 }
10421
10422 if (address < base || address > limit) {
9d2b5a58
PM
10423 /*
10424 * Address not in this region. We must check whether the
10425 * region covers addresses in the same page as our address.
10426 * In that case we must not report a size that covers the
10427 * whole page for a subsequent hit against a different MPU
10428 * region or the background region, because it would result in
10429 * incorrect TLB hits for subsequent accesses to addresses that
10430 * are in this MPU region.
10431 */
10432 if (limit >= base &&
10433 ranges_overlap(base, limit - base + 1,
10434 addr_page_base,
10435 TARGET_PAGE_SIZE)) {
10436 *is_subpage = true;
10437 }
504e3cc3
PM
10438 continue;
10439 }
10440
72042435
PM
10441 if (base > addr_page_base || limit < addr_page_limit) {
10442 *is_subpage = true;
10443 }
10444
cff21316 10445 if (matchregion != -1) {
504e3cc3
PM
10446 /* Multiple regions match -- always a failure (unlike
10447 * PMSAv7 where highest-numbered-region wins)
10448 */
3f551b5b
PM
10449 fi->type = ARMFault_Permission;
10450 fi->level = 1;
504e3cc3
PM
10451 return true;
10452 }
10453
10454 matchregion = n;
10455 hit = true;
504e3cc3
PM
10456 }
10457 }
10458
10459 if (!hit) {
10460 /* background fault */
3f551b5b 10461 fi->type = ARMFault_Background;
504e3cc3
PM
10462 return true;
10463 }
10464
10465 if (matchregion == -1) {
10466 /* hit using the background region */
10467 get_phys_addr_pmsav7_default(env, mmu_idx, address, prot);
10468 } else {
62c58ee0
PM
10469 uint32_t ap = extract32(env->pmsav8.rbar[secure][matchregion], 1, 2);
10470 uint32_t xn = extract32(env->pmsav8.rbar[secure][matchregion], 0, 1);
504e3cc3
PM
10471
10472 if (m_is_system_region(env, address)) {
10473 /* System space is always execute never */
10474 xn = 1;
10475 }
10476
10477 *prot = simple_ap_to_rw_prot(env, mmu_idx, ap);
10478 if (*prot && !xn) {
10479 *prot |= PAGE_EXEC;
10480 }
10481 /* We don't need to look the attribute up in the MAIR0/MAIR1
10482 * registers because that only tells us about cacheability.
10483 */
54317c0f
PM
10484 if (mregion) {
10485 *mregion = matchregion;
10486 }
504e3cc3
PM
10487 }
10488
3f551b5b
PM
10489 fi->type = ARMFault_Permission;
10490 fi->level = 1;
504e3cc3
PM
10491 return !(*prot & (1 << access_type));
10492}
10493
54317c0f
PM
10494
10495static bool get_phys_addr_pmsav8(CPUARMState *env, uint32_t address,
10496 MMUAccessType access_type, ARMMMUIdx mmu_idx,
10497 hwaddr *phys_ptr, MemTxAttrs *txattrs,
72042435
PM
10498 int *prot, target_ulong *page_size,
10499 ARMMMUFaultInfo *fi)
54317c0f
PM
10500{
10501 uint32_t secure = regime_is_secure(env, mmu_idx);
10502 V8M_SAttributes sattrs = {};
72042435
PM
10503 bool ret;
10504 bool mpu_is_subpage;
54317c0f
PM
10505
10506 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
10507 v8m_security_lookup(env, address, access_type, mmu_idx, &sattrs);
10508 if (access_type == MMU_INST_FETCH) {
10509 /* Instruction fetches always use the MMU bank and the
10510 * transaction attribute determined by the fetch address,
10511 * regardless of CPU state. This is painful for QEMU
10512 * to handle, because it would mean we need to encode
10513 * into the mmu_idx not just the (user, negpri) information
10514 * for the current security state but also that for the
10515 * other security state, which would balloon the number
10516 * of mmu_idx values needed alarmingly.
10517 * Fortunately we can avoid this because it's not actually
10518 * possible to arbitrarily execute code from memory with
10519 * the wrong security attribute: it will always generate
10520 * an exception of some kind or another, apart from the
10521 * special case of an NS CPU executing an SG instruction
10522 * in S&NSC memory. So we always just fail the translation
10523 * here and sort things out in the exception handler
10524 * (including possibly emulating an SG instruction).
10525 */
10526 if (sattrs.ns != !secure) {
3f551b5b
PM
10527 if (sattrs.nsc) {
10528 fi->type = ARMFault_QEMU_NSCExec;
10529 } else {
10530 fi->type = ARMFault_QEMU_SFault;
10531 }
72042435 10532 *page_size = sattrs.subpage ? 1 : TARGET_PAGE_SIZE;
54317c0f
PM
10533 *phys_ptr = address;
10534 *prot = 0;
10535 return true;
10536 }
10537 } else {
10538 /* For data accesses we always use the MMU bank indicated
10539 * by the current CPU state, but the security attributes
10540 * might downgrade a secure access to nonsecure.
10541 */
10542 if (sattrs.ns) {
10543 txattrs->secure = false;
10544 } else if (!secure) {
10545 /* NS access to S memory must fault.
10546 * Architecturally we should first check whether the
10547 * MPU information for this address indicates that we
10548 * are doing an unaligned access to Device memory, which
10549 * should generate a UsageFault instead. QEMU does not
10550 * currently check for that kind of unaligned access though.
10551 * If we added it we would need to do so as a special case
10552 * for M_FAKE_FSR_SFAULT in arm_v7m_cpu_do_interrupt().
10553 */
3f551b5b 10554 fi->type = ARMFault_QEMU_SFault;
72042435 10555 *page_size = sattrs.subpage ? 1 : TARGET_PAGE_SIZE;
54317c0f
PM
10556 *phys_ptr = address;
10557 *prot = 0;
10558 return true;
10559 }
10560 }
10561 }
10562
72042435
PM
10563 ret = pmsav8_mpu_lookup(env, address, access_type, mmu_idx, phys_ptr,
10564 txattrs, prot, &mpu_is_subpage, fi, NULL);
72042435
PM
10565 *page_size = sattrs.subpage || mpu_is_subpage ? 1 : TARGET_PAGE_SIZE;
10566 return ret;
54317c0f
PM
10567}
10568
13689d43 10569static bool get_phys_addr_pmsav5(CPUARMState *env, uint32_t address,
03ae85f8 10570 MMUAccessType access_type, ARMMMUIdx mmu_idx,
53a4e5c5
PM
10571 hwaddr *phys_ptr, int *prot,
10572 ARMMMUFaultInfo *fi)
9ee6e8bb
PB
10573{
10574 int n;
10575 uint32_t mask;
10576 uint32_t base;
0480f69a 10577 bool is_user = regime_is_user(env, mmu_idx);
9ee6e8bb 10578
3279adb9
PM
10579 if (regime_translation_disabled(env, mmu_idx)) {
10580 /* MPU disabled. */
10581 *phys_ptr = address;
10582 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
10583 return false;
10584 }
10585
9ee6e8bb
PB
10586 *phys_ptr = address;
10587 for (n = 7; n >= 0; n--) {
554b0b09 10588 base = env->cp15.c6_region[n];
87c3d486 10589 if ((base & 1) == 0) {
554b0b09 10590 continue;
87c3d486 10591 }
554b0b09
PM
10592 mask = 1 << ((base >> 1) & 0x1f);
10593 /* Keep this shift separate from the above to avoid an
10594 (undefined) << 32. */
10595 mask = (mask << 1) - 1;
87c3d486 10596 if (((base ^ address) & ~mask) == 0) {
554b0b09 10597 break;
87c3d486 10598 }
9ee6e8bb 10599 }
87c3d486 10600 if (n < 0) {
53a4e5c5 10601 fi->type = ARMFault_Background;
b7cc4e82 10602 return true;
87c3d486 10603 }
9ee6e8bb 10604
03ae85f8 10605 if (access_type == MMU_INST_FETCH) {
7e09797c 10606 mask = env->cp15.pmsav5_insn_ap;
9ee6e8bb 10607 } else {
7e09797c 10608 mask = env->cp15.pmsav5_data_ap;
9ee6e8bb
PB
10609 }
10610 mask = (mask >> (n * 4)) & 0xf;
10611 switch (mask) {
10612 case 0:
53a4e5c5
PM
10613 fi->type = ARMFault_Permission;
10614 fi->level = 1;
b7cc4e82 10615 return true;
9ee6e8bb 10616 case 1:
87c3d486 10617 if (is_user) {
53a4e5c5
PM
10618 fi->type = ARMFault_Permission;
10619 fi->level = 1;
b7cc4e82 10620 return true;
87c3d486 10621 }
554b0b09
PM
10622 *prot = PAGE_READ | PAGE_WRITE;
10623 break;
9ee6e8bb 10624 case 2:
554b0b09 10625 *prot = PAGE_READ;
87c3d486 10626 if (!is_user) {
554b0b09 10627 *prot |= PAGE_WRITE;
87c3d486 10628 }
554b0b09 10629 break;
9ee6e8bb 10630 case 3:
554b0b09
PM
10631 *prot = PAGE_READ | PAGE_WRITE;
10632 break;
9ee6e8bb 10633 case 5:
87c3d486 10634 if (is_user) {
53a4e5c5
PM
10635 fi->type = ARMFault_Permission;
10636 fi->level = 1;
b7cc4e82 10637 return true;
87c3d486 10638 }
554b0b09
PM
10639 *prot = PAGE_READ;
10640 break;
9ee6e8bb 10641 case 6:
554b0b09
PM
10642 *prot = PAGE_READ;
10643 break;
9ee6e8bb 10644 default:
554b0b09 10645 /* Bad permission. */
53a4e5c5
PM
10646 fi->type = ARMFault_Permission;
10647 fi->level = 1;
b7cc4e82 10648 return true;
9ee6e8bb 10649 }
3ad493fc 10650 *prot |= PAGE_EXEC;
b7cc4e82 10651 return false;
9ee6e8bb
PB
10652}
10653
5b2d261d
AB
10654/* Combine either inner or outer cacheability attributes for normal
10655 * memory, according to table D4-42 and pseudocode procedure
10656 * CombineS1S2AttrHints() of ARM DDI 0487B.b (the ARMv8 ARM).
10657 *
10658 * NB: only stage 1 includes allocation hints (RW bits), leading to
10659 * some asymmetry.
10660 */
10661static uint8_t combine_cacheattr_nibble(uint8_t s1, uint8_t s2)
10662{
10663 if (s1 == 4 || s2 == 4) {
10664 /* non-cacheable has precedence */
10665 return 4;
10666 } else if (extract32(s1, 2, 2) == 0 || extract32(s1, 2, 2) == 2) {
10667 /* stage 1 write-through takes precedence */
10668 return s1;
10669 } else if (extract32(s2, 2, 2) == 2) {
10670 /* stage 2 write-through takes precedence, but the allocation hint
10671 * is still taken from stage 1
10672 */
10673 return (2 << 2) | extract32(s1, 0, 2);
10674 } else { /* write-back */
10675 return s1;
10676 }
10677}
10678
10679/* Combine S1 and S2 cacheability/shareability attributes, per D4.5.4
10680 * and CombineS1S2Desc()
10681 *
10682 * @s1: Attributes from stage 1 walk
10683 * @s2: Attributes from stage 2 walk
10684 */
10685static ARMCacheAttrs combine_cacheattrs(ARMCacheAttrs s1, ARMCacheAttrs s2)
10686{
10687 uint8_t s1lo = extract32(s1.attrs, 0, 4), s2lo = extract32(s2.attrs, 0, 4);
10688 uint8_t s1hi = extract32(s1.attrs, 4, 4), s2hi = extract32(s2.attrs, 4, 4);
10689 ARMCacheAttrs ret;
10690
10691 /* Combine shareability attributes (table D4-43) */
10692 if (s1.shareability == 2 || s2.shareability == 2) {
10693 /* if either are outer-shareable, the result is outer-shareable */
10694 ret.shareability = 2;
10695 } else if (s1.shareability == 3 || s2.shareability == 3) {
10696 /* if either are inner-shareable, the result is inner-shareable */
10697 ret.shareability = 3;
10698 } else {
10699 /* both non-shareable */
10700 ret.shareability = 0;
10701 }
10702
10703 /* Combine memory type and cacheability attributes */
10704 if (s1hi == 0 || s2hi == 0) {
10705 /* Device has precedence over normal */
10706 if (s1lo == 0 || s2lo == 0) {
10707 /* nGnRnE has precedence over anything */
10708 ret.attrs = 0;
10709 } else if (s1lo == 4 || s2lo == 4) {
10710 /* non-Reordering has precedence over Reordering */
10711 ret.attrs = 4; /* nGnRE */
10712 } else if (s1lo == 8 || s2lo == 8) {
10713 /* non-Gathering has precedence over Gathering */
10714 ret.attrs = 8; /* nGRE */
10715 } else {
10716 ret.attrs = 0xc; /* GRE */
10717 }
10718
10719 /* Any location for which the resultant memory type is any
10720 * type of Device memory is always treated as Outer Shareable.
10721 */
10722 ret.shareability = 2;
10723 } else { /* Normal memory */
10724 /* Outer/inner cacheability combine independently */
10725 ret.attrs = combine_cacheattr_nibble(s1hi, s2hi) << 4
10726 | combine_cacheattr_nibble(s1lo, s2lo);
10727
10728 if (ret.attrs == 0x44) {
10729 /* Any location for which the resultant memory type is Normal
10730 * Inner Non-cacheable, Outer Non-cacheable is always treated
10731 * as Outer Shareable.
10732 */
10733 ret.shareability = 2;
10734 }
10735 }
10736
10737 return ret;
10738}
10739
10740
702a9357
PM
10741/* get_phys_addr - get the physical address for this virtual address
10742 *
10743 * Find the physical address corresponding to the given virtual address,
10744 * by doing a translation table walk on MMU based systems or using the
10745 * MPU state on MPU based systems.
10746 *
b7cc4e82
PC
10747 * Returns false if the translation was successful. Otherwise, phys_ptr, attrs,
10748 * prot and page_size may not be filled in, and the populated fsr value provides
702a9357
PM
10749 * information on why the translation aborted, in the format of a
10750 * DFSR/IFSR fault register, with the following caveats:
10751 * * we honour the short vs long DFSR format differences.
10752 * * the WnR bit is never set (the caller must do this).
f6bda88f 10753 * * for PSMAv5 based systems we don't bother to return a full FSR format
702a9357
PM
10754 * value.
10755 *
10756 * @env: CPUARMState
10757 * @address: virtual address to get physical address for
10758 * @access_type: 0 for read, 1 for write, 2 for execute
d3649702 10759 * @mmu_idx: MMU index indicating required translation regime
702a9357 10760 * @phys_ptr: set to the physical address corresponding to the virtual address
8bf5b6a9 10761 * @attrs: set to the memory transaction attributes to use
702a9357
PM
10762 * @prot: set to the permissions for the page containing phys_ptr
10763 * @page_size: set to the size of the page containing phys_ptr
5b2d261d
AB
10764 * @fi: set to fault info if the translation fails
10765 * @cacheattrs: (if non-NULL) set to the cacheability/shareability attributes
702a9357 10766 */
ebae861f
PMD
10767bool get_phys_addr(CPUARMState *env, target_ulong address,
10768 MMUAccessType access_type, ARMMMUIdx mmu_idx,
10769 hwaddr *phys_ptr, MemTxAttrs *attrs, int *prot,
10770 target_ulong *page_size,
10771 ARMMMUFaultInfo *fi, ARMCacheAttrs *cacheattrs)
9ee6e8bb 10772{
0480f69a 10773 if (mmu_idx == ARMMMUIdx_S12NSE0 || mmu_idx == ARMMMUIdx_S12NSE1) {
9b539263
EI
10774 /* Call ourselves recursively to do the stage 1 and then stage 2
10775 * translations.
0480f69a 10776 */
9b539263
EI
10777 if (arm_feature(env, ARM_FEATURE_EL2)) {
10778 hwaddr ipa;
10779 int s2_prot;
10780 int ret;
5b2d261d 10781 ARMCacheAttrs cacheattrs2 = {};
9b539263
EI
10782
10783 ret = get_phys_addr(env, address, access_type,
8bd5c820 10784 stage_1_mmu_idx(mmu_idx), &ipa, attrs,
bc52bfeb 10785 prot, page_size, fi, cacheattrs);
9b539263
EI
10786
10787 /* If S1 fails or S2 is disabled, return early. */
10788 if (ret || regime_translation_disabled(env, ARMMMUIdx_S2NS)) {
10789 *phys_ptr = ipa;
10790 return ret;
10791 }
10792
10793 /* S1 is done. Now do S2 translation. */
10794 ret = get_phys_addr_lpae(env, ipa, access_type, ARMMMUIdx_S2NS,
10795 phys_ptr, attrs, &s2_prot,
da909b2c 10796 page_size, fi,
5b2d261d 10797 cacheattrs != NULL ? &cacheattrs2 : NULL);
9b539263
EI
10798 fi->s2addr = ipa;
10799 /* Combine the S1 and S2 perms. */
10800 *prot &= s2_prot;
5b2d261d
AB
10801
10802 /* Combine the S1 and S2 cache attributes, if needed */
10803 if (!ret && cacheattrs != NULL) {
9d1bab33
PM
10804 if (env->cp15.hcr_el2 & HCR_DC) {
10805 /*
10806 * HCR.DC forces the first stage attributes to
10807 * Normal Non-Shareable,
10808 * Inner Write-Back Read-Allocate Write-Allocate,
10809 * Outer Write-Back Read-Allocate Write-Allocate.
10810 */
10811 cacheattrs->attrs = 0xff;
10812 cacheattrs->shareability = 0;
10813 }
5b2d261d
AB
10814 *cacheattrs = combine_cacheattrs(*cacheattrs, cacheattrs2);
10815 }
10816
9b539263
EI
10817 return ret;
10818 } else {
10819 /*
10820 * For non-EL2 CPUs a stage1+stage2 translation is just stage 1.
10821 */
8bd5c820 10822 mmu_idx = stage_1_mmu_idx(mmu_idx);
9b539263 10823 }
0480f69a 10824 }
d3649702 10825
8bf5b6a9
PM
10826 /* The page table entries may downgrade secure to non-secure, but
10827 * cannot upgrade an non-secure translation regime's attributes
10828 * to secure.
10829 */
10830 attrs->secure = regime_is_secure(env, mmu_idx);
0995bf8c 10831 attrs->user = regime_is_user(env, mmu_idx);
8bf5b6a9 10832
0480f69a
PM
10833 /* Fast Context Switch Extension. This doesn't exist at all in v8.
10834 * In v7 and earlier it affects all stage 1 translations.
10835 */
10836 if (address < 0x02000000 && mmu_idx != ARMMMUIdx_S2NS
10837 && !arm_feature(env, ARM_FEATURE_V8)) {
10838 if (regime_el(env, mmu_idx) == 3) {
10839 address += env->cp15.fcseidr_s;
10840 } else {
10841 address += env->cp15.fcseidr_ns;
10842 }
54bf36ed 10843 }
9ee6e8bb 10844
3279adb9 10845 if (arm_feature(env, ARM_FEATURE_PMSA)) {
c9f9f124 10846 bool ret;
f6bda88f 10847 *page_size = TARGET_PAGE_SIZE;
3279adb9 10848
504e3cc3
PM
10849 if (arm_feature(env, ARM_FEATURE_V8)) {
10850 /* PMSAv8 */
10851 ret = get_phys_addr_pmsav8(env, address, access_type, mmu_idx,
72042435 10852 phys_ptr, attrs, prot, page_size, fi);
504e3cc3 10853 } else if (arm_feature(env, ARM_FEATURE_V7)) {
3279adb9
PM
10854 /* PMSAv7 */
10855 ret = get_phys_addr_pmsav7(env, address, access_type, mmu_idx,
e5e40999 10856 phys_ptr, prot, page_size, fi);
3279adb9
PM
10857 } else {
10858 /* Pre-v7 MPU */
10859 ret = get_phys_addr_pmsav5(env, address, access_type, mmu_idx,
53a4e5c5 10860 phys_ptr, prot, fi);
3279adb9
PM
10861 }
10862 qemu_log_mask(CPU_LOG_MMU, "PMSA MPU lookup for %s at 0x%08" PRIx32
c9f9f124 10863 " mmu_idx %u -> %s (prot %c%c%c)\n",
709e4407
PM
10864 access_type == MMU_DATA_LOAD ? "reading" :
10865 (access_type == MMU_DATA_STORE ? "writing" : "execute"),
c9f9f124
MD
10866 (uint32_t)address, mmu_idx,
10867 ret ? "Miss" : "Hit",
10868 *prot & PAGE_READ ? 'r' : '-',
10869 *prot & PAGE_WRITE ? 'w' : '-',
10870 *prot & PAGE_EXEC ? 'x' : '-');
10871
10872 return ret;
f6bda88f
PC
10873 }
10874
3279adb9
PM
10875 /* Definitely a real MMU, not an MPU */
10876
0480f69a 10877 if (regime_translation_disabled(env, mmu_idx)) {
3279adb9 10878 /* MMU disabled. */
9ee6e8bb 10879 *phys_ptr = address;
3ad493fc 10880 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
d4c430a8 10881 *page_size = TARGET_PAGE_SIZE;
9ee6e8bb 10882 return 0;
0480f69a
PM
10883 }
10884
0480f69a 10885 if (regime_using_lpae_format(env, mmu_idx)) {
bc52bfeb
PM
10886 return get_phys_addr_lpae(env, address, access_type, mmu_idx,
10887 phys_ptr, attrs, prot, page_size,
10888 fi, cacheattrs);
0480f69a 10889 } else if (regime_sctlr(env, mmu_idx) & SCTLR_XP) {
bc52bfeb
PM
10890 return get_phys_addr_v6(env, address, access_type, mmu_idx,
10891 phys_ptr, attrs, prot, page_size, fi);
9ee6e8bb 10892 } else {
bc52bfeb 10893 return get_phys_addr_v5(env, address, access_type, mmu_idx,
f989983e 10894 phys_ptr, prot, page_size, fi);
9ee6e8bb
PB
10895 }
10896}
10897
0faea0c7
PM
10898hwaddr arm_cpu_get_phys_page_attrs_debug(CPUState *cs, vaddr addr,
10899 MemTxAttrs *attrs)
b5ff1b31 10900{
00b941e5 10901 ARMCPU *cpu = ARM_CPU(cs);
d3649702 10902 CPUARMState *env = &cpu->env;
a8170e5e 10903 hwaddr phys_addr;
d4c430a8 10904 target_ulong page_size;
b5ff1b31 10905 int prot;
b7cc4e82 10906 bool ret;
e14b5a23 10907 ARMMMUFaultInfo fi = {};
50494a27 10908 ARMMMUIdx mmu_idx = arm_mmu_idx(env);
b5ff1b31 10909
0faea0c7
PM
10910 *attrs = (MemTxAttrs) {};
10911
8bd5c820 10912 ret = get_phys_addr(env, addr, 0, mmu_idx, &phys_addr,
bc52bfeb 10913 attrs, &prot, &page_size, &fi, NULL);
b5ff1b31 10914
b7cc4e82 10915 if (ret) {
b5ff1b31 10916 return -1;
00b941e5 10917 }
b5ff1b31
FB
10918 return phys_addr;
10919}
10920
b5ff1b31 10921#endif
6ddbc6e4
PB
10922
10923/* Note that signed overflow is undefined in C. The following routines are
10924 careful to use unsigned types where modulo arithmetic is required.
10925 Failure to do so _will_ break on newer gcc. */
10926
10927/* Signed saturating arithmetic. */
10928
1654b2d6 10929/* Perform 16-bit signed saturating addition. */
6ddbc6e4
PB
10930static inline uint16_t add16_sat(uint16_t a, uint16_t b)
10931{
10932 uint16_t res;
10933
10934 res = a + b;
10935 if (((res ^ a) & 0x8000) && !((a ^ b) & 0x8000)) {
10936 if (a & 0x8000)
10937 res = 0x8000;
10938 else
10939 res = 0x7fff;
10940 }
10941 return res;
10942}
10943
1654b2d6 10944/* Perform 8-bit signed saturating addition. */
6ddbc6e4
PB
10945static inline uint8_t add8_sat(uint8_t a, uint8_t b)
10946{
10947 uint8_t res;
10948
10949 res = a + b;
10950 if (((res ^ a) & 0x80) && !((a ^ b) & 0x80)) {
10951 if (a & 0x80)
10952 res = 0x80;
10953 else
10954 res = 0x7f;
10955 }
10956 return res;
10957}
10958
1654b2d6 10959/* Perform 16-bit signed saturating subtraction. */
6ddbc6e4
PB
10960static inline uint16_t sub16_sat(uint16_t a, uint16_t b)
10961{
10962 uint16_t res;
10963
10964 res = a - b;
10965 if (((res ^ a) & 0x8000) && ((a ^ b) & 0x8000)) {
10966 if (a & 0x8000)
10967 res = 0x8000;
10968 else
10969 res = 0x7fff;
10970 }
10971 return res;
10972}
10973
1654b2d6 10974/* Perform 8-bit signed saturating subtraction. */
6ddbc6e4
PB
10975static inline uint8_t sub8_sat(uint8_t a, uint8_t b)
10976{
10977 uint8_t res;
10978
10979 res = a - b;
10980 if (((res ^ a) & 0x80) && ((a ^ b) & 0x80)) {
10981 if (a & 0x80)
10982 res = 0x80;
10983 else
10984 res = 0x7f;
10985 }
10986 return res;
10987}
10988
10989#define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16);
10990#define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16);
10991#define ADD8(a, b, n) RESULT(add8_sat(a, b), n, 8);
10992#define SUB8(a, b, n) RESULT(sub8_sat(a, b), n, 8);
10993#define PFX q
10994
10995#include "op_addsub.h"
10996
10997/* Unsigned saturating arithmetic. */
460a09c1 10998static inline uint16_t add16_usat(uint16_t a, uint16_t b)
6ddbc6e4
PB
10999{
11000 uint16_t res;
11001 res = a + b;
11002 if (res < a)
11003 res = 0xffff;
11004 return res;
11005}
11006
460a09c1 11007static inline uint16_t sub16_usat(uint16_t a, uint16_t b)
6ddbc6e4 11008{
4c4fd3f8 11009 if (a > b)
6ddbc6e4
PB
11010 return a - b;
11011 else
11012 return 0;
11013}
11014
11015static inline uint8_t add8_usat(uint8_t a, uint8_t b)
11016{
11017 uint8_t res;
11018 res = a + b;
11019 if (res < a)
11020 res = 0xff;
11021 return res;
11022}
11023
11024static inline uint8_t sub8_usat(uint8_t a, uint8_t b)
11025{
4c4fd3f8 11026 if (a > b)
6ddbc6e4
PB
11027 return a - b;
11028 else
11029 return 0;
11030}
11031
11032#define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16);
11033#define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16);
11034#define ADD8(a, b, n) RESULT(add8_usat(a, b), n, 8);
11035#define SUB8(a, b, n) RESULT(sub8_usat(a, b), n, 8);
11036#define PFX uq
11037
11038#include "op_addsub.h"
11039
11040/* Signed modulo arithmetic. */
11041#define SARITH16(a, b, n, op) do { \
11042 int32_t sum; \
db6e2e65 11043 sum = (int32_t)(int16_t)(a) op (int32_t)(int16_t)(b); \
6ddbc6e4
PB
11044 RESULT(sum, n, 16); \
11045 if (sum >= 0) \
11046 ge |= 3 << (n * 2); \
11047 } while(0)
11048
11049#define SARITH8(a, b, n, op) do { \
11050 int32_t sum; \
db6e2e65 11051 sum = (int32_t)(int8_t)(a) op (int32_t)(int8_t)(b); \
6ddbc6e4
PB
11052 RESULT(sum, n, 8); \
11053 if (sum >= 0) \
11054 ge |= 1 << n; \
11055 } while(0)
11056
11057
11058#define ADD16(a, b, n) SARITH16(a, b, n, +)
11059#define SUB16(a, b, n) SARITH16(a, b, n, -)
11060#define ADD8(a, b, n) SARITH8(a, b, n, +)
11061#define SUB8(a, b, n) SARITH8(a, b, n, -)
11062#define PFX s
11063#define ARITH_GE
11064
11065#include "op_addsub.h"
11066
11067/* Unsigned modulo arithmetic. */
11068#define ADD16(a, b, n) do { \
11069 uint32_t sum; \
11070 sum = (uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b); \
11071 RESULT(sum, n, 16); \
a87aa10b 11072 if ((sum >> 16) == 1) \
6ddbc6e4
PB
11073 ge |= 3 << (n * 2); \
11074 } while(0)
11075
11076#define ADD8(a, b, n) do { \
11077 uint32_t sum; \
11078 sum = (uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b); \
11079 RESULT(sum, n, 8); \
a87aa10b
AZ
11080 if ((sum >> 8) == 1) \
11081 ge |= 1 << n; \
6ddbc6e4
PB
11082 } while(0)
11083
11084#define SUB16(a, b, n) do { \
11085 uint32_t sum; \
11086 sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \
11087 RESULT(sum, n, 16); \
11088 if ((sum >> 16) == 0) \
11089 ge |= 3 << (n * 2); \
11090 } while(0)
11091
11092#define SUB8(a, b, n) do { \
11093 uint32_t sum; \
11094 sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \
11095 RESULT(sum, n, 8); \
11096 if ((sum >> 8) == 0) \
a87aa10b 11097 ge |= 1 << n; \
6ddbc6e4
PB
11098 } while(0)
11099
11100#define PFX u
11101#define ARITH_GE
11102
11103#include "op_addsub.h"
11104
11105/* Halved signed arithmetic. */
11106#define ADD16(a, b, n) \
11107 RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16)
11108#define SUB16(a, b, n) \
11109 RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16)
11110#define ADD8(a, b, n) \
11111 RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8)
11112#define SUB8(a, b, n) \
11113 RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8)
11114#define PFX sh
11115
11116#include "op_addsub.h"
11117
11118/* Halved unsigned arithmetic. */
11119#define ADD16(a, b, n) \
11120 RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16)
11121#define SUB16(a, b, n) \
11122 RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16)
11123#define ADD8(a, b, n) \
11124 RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8)
11125#define SUB8(a, b, n) \
11126 RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8)
11127#define PFX uh
11128
11129#include "op_addsub.h"
11130
11131static inline uint8_t do_usad(uint8_t a, uint8_t b)
11132{
11133 if (a > b)
11134 return a - b;
11135 else
11136 return b - a;
11137}
11138
11139/* Unsigned sum of absolute byte differences. */
11140uint32_t HELPER(usad8)(uint32_t a, uint32_t b)
11141{
11142 uint32_t sum;
11143 sum = do_usad(a, b);
11144 sum += do_usad(a >> 8, b >> 8);
11145 sum += do_usad(a >> 16, b >>16);
11146 sum += do_usad(a >> 24, b >> 24);
11147 return sum;
11148}
11149
11150/* For ARMv6 SEL instruction. */
11151uint32_t HELPER(sel_flags)(uint32_t flags, uint32_t a, uint32_t b)
11152{
11153 uint32_t mask;
11154
11155 mask = 0;
11156 if (flags & 1)
11157 mask |= 0xff;
11158 if (flags & 2)
11159 mask |= 0xff00;
11160 if (flags & 4)
11161 mask |= 0xff0000;
11162 if (flags & 8)
11163 mask |= 0xff000000;
11164 return (a & mask) | (b & ~mask);
11165}
11166
aa633469
PM
11167/* CRC helpers.
11168 * The upper bytes of val (above the number specified by 'bytes') must have
11169 * been zeroed out by the caller.
11170 */
eb0ecd5a
WN
11171uint32_t HELPER(crc32)(uint32_t acc, uint32_t val, uint32_t bytes)
11172{
11173 uint8_t buf[4];
11174
aa633469 11175 stl_le_p(buf, val);
eb0ecd5a
WN
11176
11177 /* zlib crc32 converts the accumulator and output to one's complement. */
11178 return crc32(acc ^ 0xffffffff, buf, bytes) ^ 0xffffffff;
11179}
11180
11181uint32_t HELPER(crc32c)(uint32_t acc, uint32_t val, uint32_t bytes)
11182{
11183 uint8_t buf[4];
11184
aa633469 11185 stl_le_p(buf, val);
eb0ecd5a
WN
11186
11187 /* Linux crc32c converts the output to one's complement. */
11188 return crc32c(acc, buf, bytes) ^ 0xffffffff;
11189}
a9e01311
RH
11190
11191/* Return the exception level to which FP-disabled exceptions should
11192 * be taken, or 0 if FP is enabled.
11193 */
ced31551 11194int fp_exception_el(CPUARMState *env, int cur_el)
a9e01311 11195{
55faa212 11196#ifndef CONFIG_USER_ONLY
a9e01311 11197 int fpen;
a9e01311
RH
11198
11199 /* CPACR and the CPTR registers don't exist before v6, so FP is
11200 * always accessible
11201 */
11202 if (!arm_feature(env, ARM_FEATURE_V6)) {
11203 return 0;
11204 }
11205
d87513c0
PM
11206 if (arm_feature(env, ARM_FEATURE_M)) {
11207 /* CPACR can cause a NOCP UsageFault taken to current security state */
11208 if (!v7m_cpacr_pass(env, env->v7m.secure, cur_el != 0)) {
11209 return 1;
11210 }
11211
11212 if (arm_feature(env, ARM_FEATURE_M_SECURITY) && !env->v7m.secure) {
11213 if (!extract32(env->v7m.nsacr, 10, 1)) {
11214 /* FP insns cause a NOCP UsageFault taken to Secure */
11215 return 3;
11216 }
11217 }
11218
11219 return 0;
11220 }
11221
a9e01311
RH
11222 /* The CPACR controls traps to EL1, or PL1 if we're 32 bit:
11223 * 0, 2 : trap EL0 and EL1/PL1 accesses
11224 * 1 : trap only EL0 accesses
11225 * 3 : trap no accesses
11226 */
11227 fpen = extract32(env->cp15.cpacr_el1, 20, 2);
11228 switch (fpen) {
11229 case 0:
11230 case 2:
11231 if (cur_el == 0 || cur_el == 1) {
11232 /* Trap to PL1, which might be EL1 or EL3 */
11233 if (arm_is_secure(env) && !arm_el_is_aa64(env, 3)) {
11234 return 3;
11235 }
11236 return 1;
11237 }
11238 if (cur_el == 3 && !is_a64(env)) {
11239 /* Secure PL1 running at EL3 */
11240 return 3;
11241 }
11242 break;
11243 case 1:
11244 if (cur_el == 0) {
11245 return 1;
11246 }
11247 break;
11248 case 3:
11249 break;
11250 }
11251
fc1120a7
PM
11252 /*
11253 * The NSACR allows A-profile AArch32 EL3 and M-profile secure mode
11254 * to control non-secure access to the FPU. It doesn't have any
11255 * effect if EL3 is AArch64 or if EL3 doesn't exist at all.
11256 */
11257 if ((arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
11258 cur_el <= 2 && !arm_is_secure_below_el3(env))) {
11259 if (!extract32(env->cp15.nsacr, 10, 1)) {
11260 /* FP insns act as UNDEF */
11261 return cur_el == 2 ? 2 : 1;
11262 }
11263 }
11264
a9e01311
RH
11265 /* For the CPTR registers we don't need to guard with an ARM_FEATURE
11266 * check because zero bits in the registers mean "don't trap".
11267 */
11268
11269 /* CPTR_EL2 : present in v7VE or v8 */
11270 if (cur_el <= 2 && extract32(env->cp15.cptr_el[2], 10, 1)
11271 && !arm_is_secure_below_el3(env)) {
11272 /* Trap FP ops at EL2, NS-EL1 or NS-EL0 to EL2 */
11273 return 2;
11274 }
11275
11276 /* CPTR_EL3 : present in v8 */
11277 if (extract32(env->cp15.cptr_el[3], 10, 1)) {
11278 /* Trap all FP ops to EL3 */
11279 return 3;
11280 }
55faa212 11281#endif
a9e01311
RH
11282 return 0;
11283}
11284
7aab5a8c 11285#ifndef CONFIG_TCG
65e4655c
RH
11286ARMMMUIdx arm_v7m_mmu_idx_for_secstate(CPUARMState *env, bool secstate)
11287{
7aab5a8c 11288 g_assert_not_reached();
65e4655c 11289}
7aab5a8c 11290#endif
65e4655c 11291
164690b2 11292ARMMMUIdx arm_mmu_idx_el(CPUARMState *env, int el)
65e4655c 11293{
65e4655c 11294 if (arm_feature(env, ARM_FEATURE_M)) {
50494a27 11295 return arm_v7m_mmu_idx_for_secstate(env, env->v7m.secure);
65e4655c
RH
11296 }
11297
11298 if (el < 2 && arm_is_secure_below_el3(env)) {
50494a27
RH
11299 return ARMMMUIdx_S1SE0 + el;
11300 } else {
11301 return ARMMMUIdx_S12NSE0 + el;
65e4655c 11302 }
50494a27
RH
11303}
11304
164690b2
RH
11305ARMMMUIdx arm_mmu_idx(CPUARMState *env)
11306{
11307 return arm_mmu_idx_el(env, arm_current_el(env));
11308}
11309
50494a27
RH
11310int cpu_mmu_index(CPUARMState *env, bool ifetch)
11311{
11312 return arm_to_core_mmu_idx(arm_mmu_idx(env));
65e4655c
RH
11313}
11314
64be86ab
RH
11315#ifndef CONFIG_USER_ONLY
11316ARMMMUIdx arm_stage1_mmu_idx(CPUARMState *env)
11317{
11318 return stage_1_mmu_idx(arm_mmu_idx(env));
11319}
11320#endif
11321
fdd1b228
RH
11322static uint32_t rebuild_hflags_common(CPUARMState *env, int fp_el,
11323 ARMMMUIdx mmu_idx, uint32_t flags)
11324{
11325 flags = FIELD_DP32(flags, TBFLAG_ANY, FPEXC_EL, fp_el);
11326 flags = FIELD_DP32(flags, TBFLAG_ANY, MMUIDX,
11327 arm_to_core_mmu_idx(mmu_idx));
11328
fdd1b228
RH
11329 if (arm_singlestep_active(env)) {
11330 flags = FIELD_DP32(flags, TBFLAG_ANY, SS_ACTIVE, 1);
11331 }
11332 return flags;
11333}
11334
43eccfb6
RH
11335static uint32_t rebuild_hflags_common_32(CPUARMState *env, int fp_el,
11336 ARMMMUIdx mmu_idx, uint32_t flags)
11337{
8061a649
RH
11338 bool sctlr_b = arm_sctlr_b(env);
11339
11340 if (sctlr_b) {
11341 flags = FIELD_DP32(flags, TBFLAG_A32, SCTLR_B, 1);
11342 }
11343 if (arm_cpu_data_is_big_endian_a32(env, sctlr_b)) {
11344 flags = FIELD_DP32(flags, TBFLAG_ANY, BE_DATA, 1);
11345 }
43eccfb6
RH
11346 flags = FIELD_DP32(flags, TBFLAG_A32, NS, !access_secure_reg(env));
11347
11348 return rebuild_hflags_common(env, fp_el, mmu_idx, flags);
11349}
11350
6e33ced5
RH
11351static uint32_t rebuild_hflags_m32(CPUARMState *env, int fp_el,
11352 ARMMMUIdx mmu_idx)
11353{
11354 uint32_t flags = 0;
11355
0a54d68e
RH
11356 /* v8M always enables the fpu. */
11357 flags = FIELD_DP32(flags, TBFLAG_A32, VFPEN, 1);
11358
6e33ced5
RH
11359 if (arm_v7m_is_handler_mode(env)) {
11360 flags = FIELD_DP32(flags, TBFLAG_A32, HANDLER, 1);
11361 }
11362
11363 /*
11364 * v8M always applies stack limit checks unless CCR.STKOFHFNMIGN
11365 * is suppressing them because the requested execution priority
11366 * is less than 0.
11367 */
11368 if (arm_feature(env, ARM_FEATURE_V8) &&
11369 !((mmu_idx & ARM_MMU_IDX_M_NEGPRI) &&
11370 (env->v7m.ccr[env->v7m.secure] & R_V7M_CCR_STKOFHFNMIGN_MASK))) {
11371 flags = FIELD_DP32(flags, TBFLAG_A32, STACKCHECK, 1);
11372 }
11373
11374 return rebuild_hflags_common_32(env, fp_el, mmu_idx, flags);
11375}
11376
83f4baef
RH
11377static uint32_t rebuild_hflags_aprofile(CPUARMState *env)
11378{
11379 int flags = 0;
11380
11381 flags = FIELD_DP32(flags, TBFLAG_ANY, DEBUG_TARGET_EL,
11382 arm_debug_target_el(env));
11383 return flags;
11384}
11385
c747224c
RH
11386static uint32_t rebuild_hflags_a32(CPUARMState *env, int fp_el,
11387 ARMMMUIdx mmu_idx)
11388{
83f4baef 11389 uint32_t flags = rebuild_hflags_aprofile(env);
0a54d68e
RH
11390
11391 if (arm_el_is_aa64(env, 1)) {
11392 flags = FIELD_DP32(flags, TBFLAG_A32, VFPEN, 1);
11393 }
5bb0a20b
MZ
11394
11395 if (arm_current_el(env) < 2 && env->cp15.hstr_el2 &&
11396 (arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
11397 flags = FIELD_DP32(flags, TBFLAG_A32, HSTR_ACTIVE, 1);
11398 }
11399
83f4baef 11400 return rebuild_hflags_common_32(env, fp_el, mmu_idx, flags);
c747224c
RH
11401}
11402
d4d7503a
RH
11403static uint32_t rebuild_hflags_a64(CPUARMState *env, int el, int fp_el,
11404 ARMMMUIdx mmu_idx)
a9e01311 11405{
83f4baef 11406 uint32_t flags = rebuild_hflags_aprofile(env);
d4d7503a
RH
11407 ARMMMUIdx stage1 = stage_1_mmu_idx(mmu_idx);
11408 ARMVAParameters p0 = aa64_va_parameters_both(env, 0, stage1);
d4d7503a
RH
11409 uint64_t sctlr;
11410 int tbii, tbid;
b9adaa70 11411
d4d7503a 11412 flags = FIELD_DP32(flags, TBFLAG_ANY, AARCH64_STATE, 1);
cd208a1c 11413
d4d7503a
RH
11414 /* FIXME: ARMv8.1-VHE S2 translation regime. */
11415 if (regime_el(env, stage1) < 2) {
11416 ARMVAParameters p1 = aa64_va_parameters_both(env, -1, stage1);
11417 tbid = (p1.tbi << 1) | p0.tbi;
11418 tbii = tbid & ~((p1.tbid << 1) | p0.tbid);
11419 } else {
11420 tbid = p0.tbi;
11421 tbii = tbid & !p0.tbid;
11422 }
5d8634f5 11423
d4d7503a
RH
11424 flags = FIELD_DP32(flags, TBFLAG_A64, TBII, tbii);
11425 flags = FIELD_DP32(flags, TBFLAG_A64, TBID, tbid);
11426
11427 if (cpu_isar_feature(aa64_sve, env_archcpu(env))) {
11428 int sve_el = sve_exception_el(env, el);
11429 uint32_t zcr_len;
5d8634f5 11430
d4d7503a
RH
11431 /*
11432 * If SVE is disabled, but FP is enabled,
11433 * then the effective len is 0.
11434 */
11435 if (sve_el != 0 && fp_el == 0) {
11436 zcr_len = 0;
11437 } else {
11438 zcr_len = sve_zcr_len_for_el(env, el);
5d8634f5 11439 }
d4d7503a
RH
11440 flags = FIELD_DP32(flags, TBFLAG_A64, SVEEXC_EL, sve_el);
11441 flags = FIELD_DP32(flags, TBFLAG_A64, ZCR_LEN, zcr_len);
11442 }
1db5e96c 11443
d4d7503a 11444 sctlr = arm_sctlr(env, el);
1db5e96c 11445
8061a649
RH
11446 if (arm_cpu_data_is_big_endian_a64(el, sctlr)) {
11447 flags = FIELD_DP32(flags, TBFLAG_ANY, BE_DATA, 1);
11448 }
11449
d4d7503a
RH
11450 if (cpu_isar_feature(aa64_pauth, env_archcpu(env))) {
11451 /*
11452 * In order to save space in flags, we record only whether
11453 * pauth is "inactive", meaning all insns are implemented as
11454 * a nop, or "active" when some action must be performed.
11455 * The decision of which action to take is left to a helper.
11456 */
11457 if (sctlr & (SCTLR_EnIA | SCTLR_EnIB | SCTLR_EnDA | SCTLR_EnDB)) {
11458 flags = FIELD_DP32(flags, TBFLAG_A64, PAUTH_ACTIVE, 1);
1db5e96c 11459 }
d4d7503a 11460 }
0816ef1b 11461
d4d7503a
RH
11462 if (cpu_isar_feature(aa64_bti, env_archcpu(env))) {
11463 /* Note that SCTLR_EL[23].BT == SCTLR_BT1. */
11464 if (sctlr & (el == 0 ? SCTLR_BT0 : SCTLR_BT1)) {
11465 flags = FIELD_DP32(flags, TBFLAG_A64, BT, 1);
0816ef1b 11466 }
d4d7503a 11467 }
08f1434a 11468
d4d7503a
RH
11469 return rebuild_hflags_common(env, fp_el, mmu_idx, flags);
11470}
11471
3d74e2e9
RH
11472static uint32_t rebuild_hflags_internal(CPUARMState *env)
11473{
11474 int el = arm_current_el(env);
11475 int fp_el = fp_exception_el(env, el);
164690b2 11476 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
3d74e2e9
RH
11477
11478 if (is_a64(env)) {
11479 return rebuild_hflags_a64(env, el, fp_el, mmu_idx);
11480 } else if (arm_feature(env, ARM_FEATURE_M)) {
11481 return rebuild_hflags_m32(env, fp_el, mmu_idx);
11482 } else {
11483 return rebuild_hflags_a32(env, fp_el, mmu_idx);
11484 }
11485}
11486
11487void arm_rebuild_hflags(CPUARMState *env)
11488{
11489 env->hflags = rebuild_hflags_internal(env);
11490}
11491
14f3c588
RH
11492void HELPER(rebuild_hflags_m32)(CPUARMState *env, int el)
11493{
11494 int fp_el = fp_exception_el(env, el);
11495 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
11496
11497 env->hflags = rebuild_hflags_m32(env, fp_el, mmu_idx);
11498}
11499
f80741d1
AB
11500/*
11501 * If we have triggered a EL state change we can't rely on the
11502 * translator having passed it too us, we need to recompute.
11503 */
11504void HELPER(rebuild_hflags_a32_newel)(CPUARMState *env)
11505{
11506 int el = arm_current_el(env);
11507 int fp_el = fp_exception_el(env, el);
11508 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
11509 env->hflags = rebuild_hflags_a32(env, fp_el, mmu_idx);
11510}
11511
14f3c588
RH
11512void HELPER(rebuild_hflags_a32)(CPUARMState *env, int el)
11513{
11514 int fp_el = fp_exception_el(env, el);
11515 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
11516
11517 env->hflags = rebuild_hflags_a32(env, fp_el, mmu_idx);
11518}
11519
11520void HELPER(rebuild_hflags_a64)(CPUARMState *env, int el)
11521{
11522 int fp_el = fp_exception_el(env, el);
11523 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
11524
11525 env->hflags = rebuild_hflags_a64(env, el, fp_el, mmu_idx);
11526}
11527
0ee8b24a
PMD
11528static inline void assert_hflags_rebuild_correctly(CPUARMState *env)
11529{
11530#ifdef CONFIG_DEBUG_TCG
11531 uint32_t env_flags_current = env->hflags;
11532 uint32_t env_flags_rebuilt = rebuild_hflags_internal(env);
11533
11534 if (unlikely(env_flags_current != env_flags_rebuilt)) {
11535 fprintf(stderr, "TCG hflags mismatch (current:0x%08x rebuilt:0x%08x)\n",
11536 env_flags_current, env_flags_rebuilt);
11537 abort();
11538 }
11539#endif
11540}
11541
d4d7503a
RH
11542void cpu_get_tb_cpu_state(CPUARMState *env, target_ulong *pc,
11543 target_ulong *cs_base, uint32_t *pflags)
11544{
e979972a
RH
11545 uint32_t flags = env->hflags;
11546 uint32_t pstate_for_ss;
d4d7503a 11547
9b253fe5 11548 *cs_base = 0;
0ee8b24a 11549 assert_hflags_rebuild_correctly(env);
3d74e2e9 11550
e979972a 11551 if (FIELD_EX32(flags, TBFLAG_ANY, AARCH64_STATE)) {
d4d7503a 11552 *pc = env->pc;
d4d7503a 11553 if (cpu_isar_feature(aa64_bti, env_archcpu(env))) {
08f1434a
RH
11554 flags = FIELD_DP32(flags, TBFLAG_A64, BTYPE, env->btype);
11555 }
60e12c37 11556 pstate_for_ss = env->pstate;
a9e01311
RH
11557 } else {
11558 *pc = env->regs[15];
6e33ced5
RH
11559
11560 if (arm_feature(env, ARM_FEATURE_M)) {
9550d1bd
RH
11561 if (arm_feature(env, ARM_FEATURE_M_SECURITY) &&
11562 FIELD_EX32(env->v7m.fpccr[M_REG_S], V7M_FPCCR, S)
11563 != env->v7m.secure) {
11564 flags = FIELD_DP32(flags, TBFLAG_A32, FPCCR_S_WRONG, 1);
11565 }
11566
11567 if ((env->v7m.fpccr[env->v7m.secure] & R_V7M_FPCCR_ASPEN_MASK) &&
11568 (!(env->v7m.control[M_REG_S] & R_V7M_CONTROL_FPCA_MASK) ||
11569 (env->v7m.secure &&
11570 !(env->v7m.control[M_REG_S] & R_V7M_CONTROL_SFPA_MASK)))) {
11571 /*
11572 * ASPEN is set, but FPCA/SFPA indicate that there is no
11573 * active FP context; we must create a new FP context before
11574 * executing any FP insn.
11575 */
11576 flags = FIELD_DP32(flags, TBFLAG_A32, NEW_FP_CTXT_NEEDED, 1);
11577 }
11578
11579 bool is_secure = env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_S_MASK;
11580 if (env->v7m.fpccr[is_secure] & R_V7M_FPCCR_LSPACT_MASK) {
11581 flags = FIELD_DP32(flags, TBFLAG_A32, LSPACT, 1);
11582 }
6e33ced5 11583 } else {
bbad7c62
RH
11584 /*
11585 * Note that XSCALE_CPAR shares bits with VECSTRIDE.
11586 * Note that VECLEN+VECSTRIDE are RES0 for M-profile.
11587 */
11588 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
11589 flags = FIELD_DP32(flags, TBFLAG_A32,
11590 XSCALE_CPAR, env->cp15.c15_cpar);
11591 } else {
11592 flags = FIELD_DP32(flags, TBFLAG_A32, VECLEN,
11593 env->vfp.vec_len);
11594 flags = FIELD_DP32(flags, TBFLAG_A32, VECSTRIDE,
11595 env->vfp.vec_stride);
11596 }
0a54d68e
RH
11597 if (env->vfp.xregs[ARM_VFP_FPEXC] & (1 << 30)) {
11598 flags = FIELD_DP32(flags, TBFLAG_A32, VFPEN, 1);
11599 }
6e33ced5
RH
11600 }
11601
aad821ac 11602 flags = FIELD_DP32(flags, TBFLAG_A32, THUMB, env->thumb);
aad821ac 11603 flags = FIELD_DP32(flags, TBFLAG_A32, CONDEXEC, env->condexec_bits);
60e12c37 11604 pstate_for_ss = env->uncached_cpsr;
d4d7503a 11605 }
a9e01311 11606
60e12c37
RH
11607 /*
11608 * The SS_ACTIVE and PSTATE_SS bits correspond to the state machine
a9e01311
RH
11609 * states defined in the ARM ARM for software singlestep:
11610 * SS_ACTIVE PSTATE.SS State
11611 * 0 x Inactive (the TB flag for SS is always 0)
11612 * 1 0 Active-pending
11613 * 1 1 Active-not-pending
fdd1b228 11614 * SS_ACTIVE is set in hflags; PSTATE_SS is computed every TB.
a9e01311 11615 */
60e12c37
RH
11616 if (FIELD_EX32(flags, TBFLAG_ANY, SS_ACTIVE) &&
11617 (pstate_for_ss & PSTATE_SS)) {
11618 flags = FIELD_DP32(flags, TBFLAG_ANY, PSTATE_SS, 1);
a9e01311 11619 }
a9e01311 11620
b9adaa70 11621 *pflags = flags;
a9e01311 11622}
0ab5953b
RH
11623
11624#ifdef TARGET_AARCH64
11625/*
11626 * The manual says that when SVE is enabled and VQ is widened the
11627 * implementation is allowed to zero the previously inaccessible
11628 * portion of the registers. The corollary to that is that when
11629 * SVE is enabled and VQ is narrowed we are also allowed to zero
11630 * the now inaccessible portion of the registers.
11631 *
11632 * The intent of this is that no predicate bit beyond VQ is ever set.
11633 * Which means that some operations on predicate registers themselves
11634 * may operate on full uint64_t or even unrolled across the maximum
11635 * uint64_t[4]. Performing 4 bits of host arithmetic unconditionally
11636 * may well be cheaper than conditionals to restrict the operation
11637 * to the relevant portion of a uint16_t[16].
11638 */
11639void aarch64_sve_narrow_vq(CPUARMState *env, unsigned vq)
11640{
11641 int i, j;
11642 uint64_t pmask;
11643
11644 assert(vq >= 1 && vq <= ARM_MAX_VQ);
2fc0cc0e 11645 assert(vq <= env_archcpu(env)->sve_max_vq);
0ab5953b
RH
11646
11647 /* Zap the high bits of the zregs. */
11648 for (i = 0; i < 32; i++) {
11649 memset(&env->vfp.zregs[i].d[2 * vq], 0, 16 * (ARM_MAX_VQ - vq));
11650 }
11651
11652 /* Zap the high bits of the pregs and ffr. */
11653 pmask = 0;
11654 if (vq & 3) {
11655 pmask = ~(-1ULL << (16 * (vq & 3)));
11656 }
11657 for (j = vq / 4; j < ARM_MAX_VQ / 4; j++) {
11658 for (i = 0; i < 17; ++i) {
11659 env->vfp.pregs[i].p[j] &= pmask;
11660 }
11661 pmask = 0;
11662 }
11663}
11664
11665/*
11666 * Notice a change in SVE vector size when changing EL.
11667 */
9a05f7b6
RH
11668void aarch64_sve_change_el(CPUARMState *env, int old_el,
11669 int new_el, bool el0_a64)
0ab5953b 11670{
2fc0cc0e 11671 ARMCPU *cpu = env_archcpu(env);
0ab5953b 11672 int old_len, new_len;
9a05f7b6 11673 bool old_a64, new_a64;
0ab5953b
RH
11674
11675 /* Nothing to do if no SVE. */
cd208a1c 11676 if (!cpu_isar_feature(aa64_sve, cpu)) {
0ab5953b
RH
11677 return;
11678 }
11679
11680 /* Nothing to do if FP is disabled in either EL. */
11681 if (fp_exception_el(env, old_el) || fp_exception_el(env, new_el)) {
11682 return;
11683 }
11684
11685 /*
11686 * DDI0584A.d sec 3.2: "If SVE instructions are disabled or trapped
11687 * at ELx, or not available because the EL is in AArch32 state, then
11688 * for all purposes other than a direct read, the ZCR_ELx.LEN field
11689 * has an effective value of 0".
11690 *
11691 * Consider EL2 (aa64, vq=4) -> EL0 (aa32) -> EL1 (aa64, vq=0).
11692 * If we ignore aa32 state, we would fail to see the vq4->vq0 transition
11693 * from EL2->EL1. Thus we go ahead and narrow when entering aa32 so that
11694 * we already have the correct register contents when encountering the
11695 * vq0->vq0 transition between EL0->EL1.
11696 */
9a05f7b6
RH
11697 old_a64 = old_el ? arm_el_is_aa64(env, old_el) : el0_a64;
11698 old_len = (old_a64 && !sve_exception_el(env, old_el)
0ab5953b 11699 ? sve_zcr_len_for_el(env, old_el) : 0);
9a05f7b6
RH
11700 new_a64 = new_el ? arm_el_is_aa64(env, new_el) : el0_a64;
11701 new_len = (new_a64 && !sve_exception_el(env, new_el)
0ab5953b
RH
11702 ? sve_zcr_len_for_el(env, new_el) : 0);
11703
11704 /* When changing vector length, clear inaccessible state. */
11705 if (new_len < old_len) {
11706 aarch64_sve_narrow_vq(env, new_len + 1);
11707 }
11708}
11709#endif